{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ == 708
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
#endif
module Data.Express.Fold
( fold
, unfold
, foldPair
, unfoldPair
, foldTrio
, unfoldTrio
, foldApp
, unfoldApp
)
where
import Data.Express.Core
import Data.Express.Utils.Typeable
data ExprPair = ExprPair
foldApp :: [Expr] -> Expr
foldApp :: [Expr] -> Expr
foldApp = (Expr -> Expr -> Expr) -> [Expr] -> Expr
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 Expr -> Expr -> Expr
(:$)
foldPair :: (Expr,Expr) -> Expr
foldPair :: (Expr, Expr) -> Expr
foldPair (Expr
e1,Expr
e2) = String -> ExprPair -> Expr
forall a. Typeable a => String -> a -> Expr
value String
"," (ExprPair
forall a. HasCallStack => a
undefined :: ExprPair) Expr -> Expr -> Expr
:$ Expr
e1 Expr -> Expr -> Expr
:$ Expr
e2
unfoldPair :: Expr -> (Expr,Expr)
unfoldPair :: Expr -> (Expr, Expr)
unfoldPair (Value String
"," Dynamic
_ :$ Expr
e1 :$ Expr
e2) = (Expr
e1,Expr
e2)
unfoldPair (Value String
"(,)" Dynamic
_ :$ Expr
e1 :$ Expr
e2) = (Expr
e1,Expr
e2)
unfoldPair Expr
e = String -> String -> (Expr, Expr)
forall a. String -> String -> a
errorOn String
"unfoldPair" (String -> (Expr, Expr)) -> String -> (Expr, Expr)
forall a b. (a -> b) -> a -> b
$ String
"not an Expr pair: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Expr -> String
forall a. Show a => a -> String
show Expr
e
data ExprTrio = ExprTrio
foldTrio :: (Expr,Expr,Expr) -> Expr
foldTrio :: (Expr, Expr, Expr) -> Expr
foldTrio (Expr
e1,Expr
e2,Expr
e3) = String -> ExprTrio -> Expr
forall a. Typeable a => String -> a -> Expr
value String
",," (ExprTrio
forall a. HasCallStack => a
undefined :: ExprTrio) Expr -> Expr -> Expr
:$ Expr
e1 Expr -> Expr -> Expr
:$ Expr
e2 Expr -> Expr -> Expr
:$ Expr
e3
unfoldTrio :: Expr -> (Expr,Expr,Expr)
unfoldTrio :: Expr -> (Expr, Expr, Expr)
unfoldTrio (Value String
",," Dynamic
_ :$ Expr
e1 :$ Expr
e2 :$ Expr
e3) = (Expr
e1,Expr
e2,Expr
e3)
unfoldTrio (Value String
"(,,)" Dynamic
_ :$ Expr
e1 :$ Expr
e2 :$ Expr
e3) = (Expr
e1,Expr
e2,Expr
e3)
unfoldTrio Expr
e = String -> String -> (Expr, Expr, Expr)
forall a. String -> String -> a
errorOn String
"unfoldTrio" (String -> (Expr, Expr, Expr)) -> String -> (Expr, Expr, Expr)
forall a b. (a -> b) -> a -> b
$ String
"not an Expr trio: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Expr -> String
forall a. Show a => a -> String
show Expr
e
data ExprList = ExprList
fold :: [Expr] -> Expr
fold :: [Expr] -> Expr
fold [] = String -> ExprList -> Expr
forall a. Typeable a => String -> a -> Expr
value String
"[]" ExprList
ExprList
fold (Expr
e:[Expr]
es) = String -> ExprList -> Expr
forall a. Typeable a => String -> a -> Expr
value String
":" ExprList
ExprList Expr -> Expr -> Expr
:$ Expr
e Expr -> Expr -> Expr
:$ [Expr] -> Expr
fold [Expr]
es
unfold :: Expr -> [Expr]
unfold :: Expr -> [Expr]
unfold (Value String
"\"\"" Dynamic
_) = []
unfold (Value String
"[]" Dynamic
_) = []
unfold (Value String
":" Dynamic
_ :$ Expr
e :$ Expr
es) = Expr
e Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
: Expr -> [Expr]
unfold Expr
es
unfold Expr
e = String -> String -> [Expr]
forall a. String -> String -> a
errorOn String
"unfold" (String -> [Expr]) -> String -> [Expr]
forall a b. (a -> b) -> a -> b
$ String
"cannot unfold expression: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Expr -> String
forall a. Show a => a -> String
show Expr
e
#if __GLASGOW_HASKELL__ == 708
deriving instance Typeable ExprPair
deriving instance Typeable ExprTrio
deriving instance Typeable ExprList
#endif
errorOn :: String -> String -> a
errorOn :: forall a. String -> String -> a
errorOn String
fn String
msg = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"Data.Express." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
msg