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 #

A lazy, demand-driven calculation that can create and fulfill promises.

Instances

Monad (Lazy s) # 

Methods

(>>=) :: Lazy s a -> (a -> Lazy s b) -> Lazy s b #

(>>) :: Lazy s a -> Lazy s b -> Lazy s b #

return :: a -> Lazy s a #

fail :: String -> Lazy s a #

Functor (Lazy s) # 

Methods

fmap :: (a -> b) -> Lazy s a -> Lazy s b #

(<$) :: a -> Lazy s b -> Lazy s a #

MonadFix (Lazy s) # 

Methods

mfix :: (a -> Lazy s a) -> Lazy s a #

Applicative (Lazy s) # 

Methods

pure :: a -> Lazy s a #

(<*>) :: Lazy s (a -> b) -> Lazy s a -> Lazy s b #

liftA2 :: (a -> b -> c) -> Lazy s a -> Lazy s b -> Lazy s c #

(*>) :: Lazy s a -> Lazy s b -> Lazy s b #

(<*) :: Lazy s a -> Lazy s b -> Lazy s a #

PrimMonad (Lazy s) # 

Associated Types

type PrimState (Lazy s :: * -> *) :: * #

Methods

primitive :: (State# (PrimState (Lazy s)) -> (#TupleRep [RuntimeRep], LiftedRep, State# (PrimState (Lazy s)), a#)) -> Lazy s a #

type PrimState (Lazy s) # 
type PrimState (Lazy s) = s

runLazy :: (forall s. Promise s a -> Lazy s b) -> a -> a #

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 #

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 #

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

data Promise s a where #

A lazy I-Var.

Constructors

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

promise :: a -> Lazy s (Promise s a) #

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) #

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 #

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 #

Demand the result of a promise.