@@ -18,25 +18,36 @@ import Native.Lazy
19data Lazy a = L { force : () -> a }
19data Lazy a = L { force : () -> a }
21+{-| Delay a computation. This function memoizes results, so
21−{-| Memoize a thunk so it is evaluated at most once. -}
22+it guarantees that the computation will be evaluated at most once.
22lazy : (() -> a) -> Lazy a
24lazy : (() -> a) -> Lazy a
23lazy t = L { force = (Native.Lazy.lazy t) }
25lazy t = L { force = (Native.Lazy.lazy t) }
25−{-| Execute a lazy value. -}
27+{-| Evaluate a lazy value. It saves the result so the second time
28+you call `force` it does not run the computation again. -}
26force : Lazy a -> a
29force : Lazy a -> a
27force (L r) = r.force ()
30force (L r) = r.force ()
29−{-| Lazily apply a pure function to a lazy value. -}
32+{-| Lazily apply a function to a lazy value. The computation
33+will be delayed until you force the resulting value.
30map : (a -> b) -> Lazy a -> Lazy b
35map : (a -> b) -> Lazy a -> Lazy b
31map f t = lazy <| \() ->
36map f t = lazy <| \() ->
32 f . force <| t
37 f . force <| t
34−{-| Lazily apply a Lazy function to a Lazy value. -}
39+{-| Lazily apply a lazy function to a lazy value. This can
40+be used to lazily apply a function to many arguments:
43+f `map` a `apply` b `apply` c
35apply : Lazy (a -> b) -> Lazy a -> Lazy b
46apply : Lazy (a -> b) -> Lazy a -> Lazy b
36apply f x = lazy <| \() ->
47apply f x = lazy <| \() ->
37 (force f) (force x)
48 (force f) (force x)
39−{-| Lazily chain together Lazy computations. -}
50+{-| Lazily chain together lazy computations. -}
40bind : Lazy a -> (a -> Lazy b) -> Lazy b
51bind : Lazy a -> (a -> Lazy b) -> Lazy b
41bind x k = lazy <| \() ->
52bind x k = lazy <| \() ->
42 force . k . force <| x
53 force . k . force <| x