2013-12-09 21:16:04 +00:00
|
|
|
module Lazy ( force, lazy, map, apply, bind
|
2013-12-09 06:01:32 +00:00
|
|
|
) where
|
2013-12-07 20:22:57 +00:00
|
|
|
|
2013-12-09 06:01:32 +00:00
|
|
|
{-| Library for efficient Lazy evaluation.
|
2013-12-07 20:22:57 +00:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2013-12-09 06:39:11 +00:00
|
|
|
import Basics ((<|), (.))
|
2013-12-07 20:22:57 +00:00
|
|
|
import Native.Lazy
|
|
|
|
|
2013-12-09 21:16:04 +00:00
|
|
|
data Lazy a = L { force : () -> a }
|
2013-12-07 20:22:57 +00:00
|
|
|
|
2013-12-09 06:39:11 +00:00
|
|
|
{-| Memoize a thunk so it is evaluated at most once. -}
|
2013-12-09 21:16:04 +00:00
|
|
|
lazy : (() -> a) -> Lazy a
|
|
|
|
lazy t = L { force = (Native.Lazy.lazy t) }
|
2013-12-07 20:22:57 +00:00
|
|
|
|
2013-12-09 06:39:11 +00:00
|
|
|
{-| Execute a lazy value. -}
|
2013-12-09 21:16:04 +00:00
|
|
|
force : Lazy a -> a
|
|
|
|
force (L r) = r.force ()
|
2013-12-07 20:30:51 +00:00
|
|
|
|
2013-12-09 06:39:11 +00:00
|
|
|
{-| Lazily apply a pure function to a lazy value. -}
|
|
|
|
map : (a -> b) -> Lazy a -> Lazy b
|
2013-12-09 21:16:04 +00:00
|
|
|
map f t = lazy <| \() ->
|
|
|
|
f . force <| t
|
2013-12-09 06:39:11 +00:00
|
|
|
|
|
|
|
{-| Lazily apply a Lazy function to a Lazy value. -}
|
|
|
|
apply : Lazy (a -> b) -> Lazy a -> Lazy b
|
2013-12-09 21:16:04 +00:00
|
|
|
apply f x = lazy <| \() ->
|
|
|
|
(force f) (force x)
|
2013-12-09 06:39:11 +00:00
|
|
|
|
|
|
|
{-| Lazily chain together Lazy computations. -}
|
|
|
|
bind : Lazy a -> (a -> Lazy b) -> Lazy b
|
2013-12-09 21:16:04 +00:00
|
|
|
bind x k = lazy <| \() ->
|
|
|
|
force . k . force <| x
|