promises-0.3: Lazy demand-driven promises

Copyright(C) 2015 Edward Kmett
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Promise

Description

Lazy demand-driven promises.

Synopsis

Documentation

data Lazy s a Source

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) -> a -> IO a Source

runLazyIO_ :: (forall s. Promise s a -> Lazy s b) -> IO a Source

data Promise s a where Source

A lazy I-Var.

Constructors

Promise :: MVar a -> a -> Promise s a 

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"

demand :: Promise s a -> a Source

Demand the result of a promise.

data BrokenPromise Source

Thrown when the answer for an unfulfillable promise is demanded.

Constructors

BrokenPromise