Copyright | (c) 2019 Rudy Matela |
---|---|
License | 3-Clause BSD (see the file LICENSE) |
Maintainer | Rudy Matela <rudy@matela.com.br> |
Safe Haskell | Safe |
Language | Haskell2010 |
Defines utilities for folding and unfolding Expr
s.
Documentation
fold :: [Expr] -> Expr Source #
O(n).
Folds a list of Expr
s into a single Expr
.
(cf. unfold
)
This always generates an ill-typed expression.
fold [val False, val True, val (1::Int)] [False,True,1] :: ill-typed # ExprList $ Bool #
This is useful when applying transformations on lists of Expr
s, such as
canonicalize
,
mapValues
or
canonicalVariations
.
> let ii = var "i" (undefined::Int) > let kk = var "k" (undefined::Int) > let qq = var "q" (undefined::Bool) > let notE = value "not" not > unfold . canonicalize . fold $ [ii,kk,notE :$ qq, notE :$ val False] [x :: Int,y :: Int,not p :: Bool,not False :: Bool]
foldPair :: (Expr, Expr) -> Expr Source #
O(1).
Folds a pair of Expr
values into a single Expr
.
(cf. unfoldPair
)
This always generates an ill-typed expression.
> foldPair (val False, val (1::Int)) (False,1) :: ill-typed # ExprPair $ Bool #
> foldPair (val (0::Int), val True) (0,True) :: ill-typed # ExprPair $ Int #
This is useful when applying transformations on pairs of Expr
s, such as
canonicalize
,
mapValues
or
canonicalVariations
.
> let ii = var "i" (undefined::Int) > let kk = var "k" (undefined::Int) > unfoldPair $ canonicalize $ foldPair (ii,kk) (x :: Int,y :: Int)
foldApp :: [Expr] -> Expr Source #
O(n).
Folds a list of Expr
with function application (:$
).
This reverses the effect of unfoldApp
.
foldApp [e0] = e0 foldApp [e0,e1] = e0 :$ e1 foldApp [e0,e1,e2] = e0 :$ e1 :$ e2 foldApp [e0,e1,e2,e3] = e0 :$ e1 :$ e2 :$ e3
Remember :$
is left-associative, so:
foldApp [e0] = e0 foldApp [e0,e1] = (e0 :$ e1) foldApp [e0,e1,e2] = ((e0 :$ e1) :$ e2) foldApp [e0,e1,e2,e3] = (((e0 :$ e1) :$ e2) :$ e3)
This function may produce an ill-typed expression.
unfoldApp :: Expr -> [Expr] Source #
O(n).
Unfold a function application Expr
into a list of function and
arguments.
unfoldApp $ e0 = [e0] unfoldApp $ e0 :$ e1 = [e0,e1] unfoldApp $ e0 :$ e1 :$ e2 = [e0,e1,e2] unfoldApp $ e0 :$ e1 :$ e2 :$ e3 = [e0,e1,e2,e3]
Remember :$
is left-associative, so:
unfoldApp e0 = [e0] unfoldApp (e0 :$ e1) = [e0,e1] unfoldApp ((e0 :$ e1) :$ e2) = [e0,e1,e2] unfoldApp (((e0 :$ e1) :$ e2) :$ e3) = [e0,e1,e2,e3]