[Haskell-beginners] FRP

Ertugrul Söylemez es at ertes.de
Sun Sep 16 10:37:15 CEST 2012


Christopher Howard <christopher.howard at frigidcode.com> wrote:

> > Summary:  FRP is about handling time-varying values like they were
> > regular values.
>
> Ouch!... Ouch!... My head is beginning to explode!
>
> So, I think I understand the main idea you just explained, but now I
> am curious about how this black magic is possible. Presumably, these
> mystical "time-varying values" would actually have to be some kind of
> partially applied function that receives a time parameter. But how do
> we, as in your example, add a literal number to a function?

One very simple and naive way to implement FRP is this:

    newtype Behavior a = Behavior (Time -> a)

'Behavior' is the traditional name for a time-varying value.  Then a
constant is, as the name says, a constant:

    instance Applicative Behavior where
        pure = Behavior . const
        {- ... -}

To get the notation I have used you just need Haskell's fromInteger
magic:

    instance (Num a) => Num (Behavior a) where
        (+) = liftA2 (+)
        (-) = liftA2 (-)
        {- ... -}
        fromInteger = pure . fromInteger

Given that and a behavior that returns the current time,

    time :: Behavior Time
    time = Behavior id

you can now have

    10 + 2*time


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120916/32c12aa2/attachment.pgp>


More information about the Beginners mailing list