module Ivory.Stdlib.Control
( ifte
, when
, unless
, cond_, cond, (==>), Cond()
) where
import Ivory.Language
ifte :: ( IvoryStore a
, IvoryZero ('Stored a)
, GetAlloc eff ~ 'Scope s
) => IBool
-> Ivory eff a
-> Ivory eff a
-> Ivory eff a
ifte c t f = do
r <- local izero
ifte_ c
(t >>= store r)
(f >>= store r)
deref r
when :: IBool -> Ivory eff () -> Ivory eff ()
when c t = ifte_ c t (return ())
unless :: IBool -> Ivory eff () -> Ivory eff ()
unless c f = ifte_ c (return ()) f
data Cond eff a = Cond IBool (Ivory eff a)
(==>) :: IBool -> Ivory eff a -> Cond eff a
(==>) = Cond
infix 0 ==>
cond_ :: [Cond eff ()] -> Ivory eff ()
cond_ [] = return ()
cond_ ((Cond b f):cs) = ifte_ b f (cond_ cs)
cond :: ( IvoryStore a
, IvoryZero ('Stored a)
, GetAlloc eff ~ 'Scope s
) => [Cond eff a] -> Ivory eff a
cond as = do
r <- local izero
aux as r
deref r
where
aux [] _ = return ()
aux ((Cond b f):cs) r = ifte_ b (f >>= store r) (aux cs r)