[Haskell-beginners] FRP

Miguel Negrao miguel.negrao-lists at friendlyvirus.org
Tue Oct 2 01:50:42 CEST 2012


A 01/10/2012, às 09:49, Heinrich Apfelmus escreveu:

> Miguel Negrao wrote:
>> Thanks for the explanation. I was wondering, how would one translate
>> this Yampa code into reactive-banana:
>> fallingBall :: Pos -> Vel -> SF () (Pos, Vel)
>> 	fallingBall y0 v0 = proc () -> do
>> 		v <- (v0 +) ˆ<< integral -< -9.81
>> 		y <- (y0 +) ˆ<< integral -< v
>> 		returnA -< (y, v)
>> fallingBall’ :: Pos -> Vel -> SF () ((Pos,Vel), Event (Pos,Vel))
>> fallingBall’ y0 v0 = proc () -> do
>> 	yv@(y, _) <- fallingBall y0 v0 -< ()
>> 	hit <- edge -< y <= 0
>> 	returnA -< (yv, hit ‘tag‘ yv)
>> bouncingBall :: Pos -> SF () (Pos, Vel)
>> bouncingBall y0 = bbAux y0 0.0
>> 	where
>> 		bbAux y0 v0 = switch (fallingBall’ y0 v0) $ \(y,v) -> bbAux y (-v)
>> Would it be possible to do this without dynamic event switching ?
>> What  about with the new event switching in v0.7 ? Also, is it possible (and
>> is it easy ?) to do looping such as it is done in Yampa using the the
>> loop arrow in reactive-banana/classic FRP ?
> 
> The Animation.hs example may help
> 
>  http://www.haskell.org/haskellwiki/Reactive-banana/Examples#animation
> 
> Essentially, the main difference is that reactive-banana doesn't have functions like  integral  or  edge  because they depend on an implicit time step. In reactive-banana, you have to manage time yourself, for instance by making a timer event from a wxTimer.
> 
> To calculate velocity and position, you can you use  accumE  on the timer event. To do collision detection, you can check whether the ball will move below the floor at each time step. Dynamic event switching is not needed here.
> 
> For a more complex example, have a look at Andreas Bernstein's paddle ball game
> 
>  https://github.com/bernstein/breakout

Yes, looking at the internals of Yampa I had seen that they have internal time management, but my question was more specifically if there was a way to have a function like bbAux which recursively switches into itself. Would it be possible with the new dynamic switching ? I find that way of expressing the discontinuous changes quite elegant. Even more elegant seems to be the instantaneous impulses (modeling of distributions or dirac deltas) although I couldn’t find any functioning code that implements it [1].

The breakout game code you mentioned is an excelent example of FRP in use, and best of all, it actually compiles !! I’ve lost count of the FRP programs I’ve tried to compile without success (mostly yampa or YFRP related). It was very instructional to see how it implements this kind of game logic in reactiva-banana. It’s also a good example of recursive definitions in reactiva-banana:  the ball velocity depends on the ball position and vice-versa.

Thank you for the link and the explanations,
Miguel

[1]
http://haskell.cs.yale.edu/wp-content/uploads/2011/01/icfp03.pdf
bouncing :: Position -> SF () (Position, Velocity)

bouncing y0 = proc () -> do rec
	y <- (y0 +) ^<< integralG -< yd_ni 
	hit <- edge -< y <= 0
	yd <- integralG -< -9.81 + impulse (hit ‘tag‘ (-2*leftLimit yd))
	yd_ni <- assertNoImpulseG -< yd
returnA -< (y, yd)


More information about the Beginners mailing list