Copyright | (C) 2015 Edward Kmett |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Edward Kmett <ekmett@gmail.com> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
Data.Promise
Description
Lazy demand-driven promises.
- data Lazy s a
- runLazy :: (forall s. Promise s a -> Lazy s b) -> a -> a
- runLazy_ :: (forall s. Promise s a -> Lazy s b) -> a
- runLazyIO :: (forall s. Promise s a -> Lazy s b) -> a -> IO a
- runLazyIO_ :: (forall s. Promise s a -> Lazy s b) -> IO a
- data Promise s a where
- promise :: a -> Lazy s (Promise s a)
- promise_ :: Lazy s (Promise s a)
- (!=) :: Promise s a -> a -> Lazy s ()
- demand :: Promise s a -> a
- data BrokenPromise = BrokenPromise
Documentation
A lazy, demand-driven calculation that can create and fulfill promises.
runLazy :: (forall s. Promise s a -> Lazy s b) -> a -> a Source
Run a lazy computation. The final answer is given in the form of a promise to be fulfilled. If the promises is unfulfilled then an user supplied default value will be returned.
runLazy_ :: (forall s. Promise s a -> Lazy s b) -> a Source
Run a lazy computation. The final answer is given in the form of a promise to be fulfilled.
If the promises is unfulfilled then an BrokenPromise
will be thrown.
runLazy_
k ≡runLazy
k (throw
BrokenPromise
)
runLazyIO_ :: (forall s. Promise s a -> Lazy s b) -> IO a Source
promise :: a -> Lazy s (Promise s a) Source
Promise that by the end of the computation we'll provide a "real" answer, or we'll fall back and give you this answer
promise_ :: Lazy s (Promise s a) Source
Create an empty promise. If you observe the demanded answer of this promise then either by the end of the current lazy computation we'll provide a "real" answer, or you'll get an error.
promise_
≡promise
(throw
BrokenPromise
)
(!=) :: Promise s a -> a -> Lazy s () infixl 0 Source
Fulfill a promise. Each promise should only be fulfilled once.
>>>
runLazy_ $ \p -> p != "good"
"good"
>>>
runLazy_ $ \p -> do q <- promise_; p != "yay! " ++ demand q; q != "it works."
"yay! it works."
>>>
runLazy_ $ \p -> return ()
*** Exception: BrokenPromise
>>>
runLazy (\p -> return ()) "default"
"default"
data BrokenPromise Source
Thrown when the answer for an unfulfillable promise is demanded.
Constructors
BrokenPromise |
Instances