Version franaise
Home About Download Resources Contact us
Browse thread
Recursion on React.events.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel_Bünzli <daniel.buenzli@e...>
Subject: Re: Recursion on React.events.
> Daniel Bünzli's module is great, but sometimes a bit rough
> to get by, specifically on examples such as this one.

I would just like to point out that this has nothing to do with the
module per se but understanding frp in general and this is the reason
why I went to great length to document the semantics for each of the
combinators I provide --- something most frp libraries won't bother to
do, leaving you with testing or looking into the implementation for
understanding things when tricky simulateneity issues arise.

Thus to understand why your event didn't work you could have done the
following. Provided you understand the notations given here :

http://erratique.ch/software/react/doc/React#sem


1) Define a semantics for your primitive events and functions.

 [Calendar.schedule st]_t = Some () iff t = st
 [Calendar.now ()]_t = t


2) Reason on your expression, by applying the semantics of the
combinators and your primitives.

Let's try to look what happens at st for (regular_schedule st p)
assuming p > 0.

 [regular_schedule st p]_st =
 [E.switch E.never ee]_st
 with ee = E.map (fun () -> regular_schedule ...) (schedule st)

Since we have [schedule st]_st = Some (), by the semantics of E.map we
have [ee]_st = Some ev. Thus we are in the second case of the semantics
of E.switch (see doc) and the semantics of the switch reduces to the
semantics of ev, i.e.

 [E.switch E.never ee]_st =
 [regular_schedule (Calendar.add (Calendar.now ()) p) p]_st =
 [E.switch E.never ee']_st
 with ee' = E.map (fun () -> regular_schedule ..) (schedule (st + p))

Now by the semantics of schedule and E.map you know nothing will
happen on ee' before st + p ans since p > 0 we are in the first case
of the semantics of E.switch and the last switch reduces to the
semantics of E.never. To sum up :

 [regular_schedule st p]_st = [E.never]_st = None

So at st, nothing happens, as you witnessed. Applying the same
technique you could generalize the result for any t.

Pure equational reasoning, it's not that hard, or is it ?

Best,

Daniel

P.S. In my previous email

 let reschedule () = Calendar.add (Calendar.now ()) period

should read

 let reschedule () = schedule (Calendar.add (Calendar.now ()) period)

I misunderstood the type of Calendar.add.