<div dir="ltr">Hi Ryan,<br><br>Thanks very much for these explanations. Clear and right on!<br><br>Best regards, - Conal<br><br>P.S. I'll be at ICFP and am looking forward to seeing folks there.<br><br><div class="gmail_quote">
2008/9/16 Ryan Ingram <span dir="ltr"><<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
The key insight is that Behavior a is not necessarily a time function;<br>
it's abstract. But you can treat it as if it was one by observing it<br>
with "at".<br>
<br>
In Conal's paper, the internal type of behavior is:<br>
<br>
> -- composition of types; like (.) at the type level<br>
> newtype O h g a = O (h (g a))<br>
<br>
> -- function type that can directly observe some constant functions<br>
> data Fun t a = K a | Fun (t -> a)<br>
<br>
> -- Behavior a ~~ Reactive (Fun Time a)<br>
> type Behavior = Reactive `O` Fun Time<br>
<br>
> -- Reactive has a current value and an event stream of values to switch to at particular times<br>
> -- Then an event is just a reactive that might not have a current value until some time in the future.<br>
> data Reactive a = Stepper a (Event a)<br>
> newtype Event a = Ev (Future (Reactive a))<br>
<br>
Now, at the internal level, you can write the primitive "time" as<br>
<br>
> time :: Behavior Time<br>
> time = O (pure (Fun id))<br>
<br>
with "pure" from the Applicative instance for Reactive:<br>
<br>
> pure x = Stepper x never<br>
<br>
"never" is a future that never occurs, so the reactive value never changes.<br>
<br>
From a users' point of view, all this is invisible--you only get a few<br>
observation functions (including "at"). Internally, however, constant<br>
behaviors, or behaviors that contain "steps" that are constant, can be<br>
evaluated extremely quickly; once the behavior returns K x, you know<br>
that the result can't change until the next event in the reactive<br>
stream. You only need to continuously evaluate the behavior if you<br>
get a "Fun" result. See sinkB on page 9 of the paper to understand<br>
how this is used to improve performance.<br>
<br>
The semantic function "at" drives the model; it allows you to describe<br>
the laws for the library to fulfill very succinctly:<br>
<br>
at (fmap f x) = fmap f (at x)<br>
at (pure x) = pure x<br>
at (f <*> x) = at f <*> at x<br>
at (return x) = return x<br>
at (m >>= f) = at m >>= at . f<br>
etc.<br>
<br>
Similarily, for Futures, we have "force :: Future a -> (Time, a)"<br>
<br>
force (fmap f z) = (t, f x) where (t,x) = force z<br>
force (pure x) = (minBound, x)<br>
force (ff <*> fx) = (max tf tx, f x) where (tf, f) = force ff ; (tx,<br>
x) = force fx<br>
force (return x) = (minBound, x)<br>
force (m >>= f) = (max tm tx, x) where (tm, v) = force m; (tx, x) = force (f v)<br>
etc.<br>
<br>
This gives the library user solid ground to stand on when reasoning<br>
about their code; it should do what they expect. And it gives the<br>
library author a very strong goal to shoot for: just fulfill these<br>
laws, and the code is correct! This allows radical redesigns of the<br>
internals of the system while maintaining a consistent and intuitive<br>
interface that reuses several classes that the user is hopefully<br>
already familiar with: monoids, functors, applicative functors, and<br>
monads.<br>
<br>
-- ryan<br>
<br>
2008/9/16 Daryoush Mehrtash <<a href="mailto:dmehrtash@gmail.com">dmehrtash@gmail.com</a>>:<br>
<div><div></div><div class="Wj3C7c">> ø I don't follow the "at" and "type B a". "Behavior a" itself is a<br>
> time function. At least in the version of the code that was<br>
> developed in Pual Hudak's Haskell School of Expression it was defined<br>
> as:<br>
><br>
>> newtype Behavior a<br>
>> = Behavior (([Maybe UserAction],[Time]) -> [a])<br>
><br>
> In a function like time you can see that the "at" function makes things simpler.<br>
><br>
> In the original version time was defined as:<br>
><br>
>> time :: Behavior Time<br>
>> time = Behavior (\(_,ts) -> ts)<br>
><br>
> In Conal's paper<br>
><br>
> time :: Behavior Time<br>
> at time = id<br>
><br>
> Comparing the two implementation of the time, it seems to me that "at"<br>
> and "type B a" has put the design on a more solid ground. But I don't<br>
> quite understand the thought process, or the principal behind what is<br>
> happening.<br>
><br>
> daryoush<br>
><br>
><br>
> On Mon, Sep 15, 2008 at 10:46 AM, Ryan Ingram <<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>> wrote:<br>
>> Here's a quick overview that might help you.<br>
>><br>
>> For a reactive behavior, we have two types to think about:<br>
>><br>
>> type B a = Time -> a<br>
>> (the semantic domain)<br>
>><br>
>> data Behavior a = ?<br>
>> (the library's implementation).<br>
>> at :: Behavior a -> B a<br>
>> (observation function)<br>
>><br>
>> This is really just classic "information hiding" as you would do with<br>
>> any abstract data type. Consider a simple "stack" data structure that<br>
>> supports push and pop.<br>
>><br>
>>> data S a = S<br>
>>> { popS :: Maybe (a, S a)<br>
>>> , pushS :: a -> S a<br>
>>> }<br>
>><br>
>>> data Stack a = ?<br>
>>> observeStack :: Stack a -> S a<br>
>><br>
>> As a library user, you don't really care about the implementation of<br>
>> Stack, just as a user of Conal's library doesn't really care about the<br>
>> implementation of Behavior. What you *do* care about is that you can<br>
>> think about it in the simpler terms of "Time -> a" which is the model<br>
>> he has chosen.<br>
>><br>
>> The rest of the library design comes from taking that model and<br>
>> thinking about what typeclasses and operations "Time -> a" should<br>
>> support, and creating typeclass morphisms between Behavior a and B a<br>
>> where necessary. For example:<br>
>><br>
>>> -- This makes (r -> a) into a functor over a; it is a generalization of Time -> a<br>
>>> instance Functor ((->) r) where<br>
>>> -- fmap :: (a -> b) -> (r -> a) -> (r -> b)<br>
>>> fmap f x = \r -> f (x r)<br>
>>> -- or, "fmap = (.)", if you're golfing :)<br>
>><br>
>> In order for the morphism between B and Behavior to make sense, you<br>
>> want this law to hold:<br>
>> fmap f (at behavior) = at (fmap f behavior)<br>
>> for all behavior :: Behavior a.<br>
>><br>
>> The fmap on the left applies to B which is (Time ->); the fmap on the<br>
>> right applies to Behavior.<br>
>><br>
>> Conal writes this law more elegantly like this:<br>
>>> instance(semantic) Functor Behavior where<br>
>>> fmap f . at = at . fmap f<br>
>><br>
>> As long as you as the user can think about behaviors generally as<br>
>> functions of Time, you can ignore the implementation details, and<br>
>> things that you expect to work should work. This drives the design of<br>
>> the entire library, with similar morphisms over many typeclasses<br>
>> between Event and E, Reactive and B, etc.<br>
>><br>
>> -- ryan<br>
>><br>
>> On Mon, Sep 15, 2008 at 10:13 AM, Daryoush Mehrtash <<a href="mailto:dmehrtash@gmail.com">dmehrtash@gmail.com</a>> wrote:<br>
>>> Interestingly, I was trying to read his paper when I realized that I<br>
>>> needed to figure out the meaning of denotational model, semantic<br>
>>> domain, semantic functions. Other Haskell books didn't talk about<br>
>>> design in those terms, but obviously for him this is how he is driving<br>
>>> his design. I am looking for a simpler tutorial, text book like<br>
>>> reference on the topic.<br>
>>><br>
>>> Daryoush<br>
>>><br>
>>> On Mon, Sep 15, 2008 at 1:33 AM, Ryan Ingram <<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>> wrote:<br>
>>>> I recommend reading Conal Elliott's "Efficient Functional Reactivity"<br>
>>>> paper for an in-depth real-world example.<br>
>>>><br>
>>>> <a href="http://www.conal.net/papers/simply-reactive" target="_blank">http://www.conal.net/papers/simply-reactive</a><br>
>>>><br>
>>>> -- ryan<br>
>>>><br>
>>>> On Sun, Sep 14, 2008 at 11:31 AM, Daryoush Mehrtash <<a href="mailto:dmehrtash@gmail.com">dmehrtash@gmail.com</a>> wrote:<br>
>>>>> I have been told that for a Haskell/Functional programmer the process<br>
>>>>> of design starts with defining Semantic Domain, Function, and<br>
>>>>> denotational model of the problem. I have done some googling on the<br>
>>>>> topic but haven't found a good reference on it. I would appreciate<br>
>>>>> any good references on the topic.<br>
>>>>><br>
>>>>> thanks,<br>
>>>>><br>
>>>>> daryoush<br>
>>>>><br>
>>>>> ps. I have found referneces like<br>
>>>>> <a href="http://en.wikibooks.org/wiki/Haskell/Denotational_semantics" target="_blank">http://en.wikibooks.org/wiki/Haskell/Denotational_semantics</a> which<br>
>>>>> talks about semantic domain for "the Haskell programs 10, 9+1, 2*5"<br>
>>>>> which doesn't do any good for me. I need something with a more real<br>
>>>>> examples.<br>
>>>>> _______________________________________________<br>
>>>>> Haskell-Cafe mailing list<br>
>>>>> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
>>>>> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
>>>>><br>
>>>><br>
>>><br>
>><br>
><br>
</div></div><br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>