Monday, February 6, 2012

Hybrid system modeling in AFRP

Continuing the exploration of Arrowized FRP, Adam implemented a model of a furnace (adapted from Example 15 in Michael S. Branicky's Introduction to Hybrid Systems) in Yampa. Together with the bouncing ball and breaking ball examples, these will serve as benchmarks a comparison of FRP languages as hosts for hybrid systems modeling.  Below is a comparison of Acumen and Yampa implementations of the furnace benchmark (note that rendering-specific parts of the FRP code have been omitted). Suggestions for improvements of the Yampa implementation are most welcome!

Acumen

Yampa

class Main(simulator)
 private
   state = "Off"; x  = 0; x' = 1
 end
 if state == "On"
   if x >= 25
     state = "Off"
   else
     x' [=] 40 - x
   end;
 end;
 if state == "Off"
   if x <= 21
     state = "On"
   else
     x' [=] 10 - x
   end;
 end
end
type State = (Bool, Double, Double)

thermostat :: State -> SF () State
thermostat i =
 switch (aux i)
 (\(mOut, xOut, vOut) ->
          thermostat (not mOut, xOut, vOut))

aux :: State -> SF () (State, Event State)
aux (isOn,xIn,x'In) = proc () -> do    
 rec x   <- (xIn +) ^<< integral -< x'
     x'  <- returnA -< if isOn
                       then 40 - x
                       else 10 - x
 tooHot  <- edge    -<     isOn && x >= 25
 tooCold <- edge    -< not isOn && x <= 21
 let s = (isOn, x, x')
 returnA -< (s, (rMerge tooHot tooCold) `tag` s)