summaryrefslogtreecommitdiff
path: root/src/PGF
diff options
context:
space:
mode:
authorkrasimir <krasimir@chalmers.se>2009-09-11 14:42:45 +0000
committerkrasimir <krasimir@chalmers.se>2009-09-11 14:42:45 +0000
commit509d0cf8d8ad17f61025d11aecec576adbbd701b (patch)
tree78b3e60f47392ad5226516b6f1ed9fd658d80e1a /src/PGF
parent1cdf171251a56baf0867b65a95c9bd59801ff912 (diff)
added wrapper functions for expression manipulations in PGF. The Haskell API now uses the wrappers
Diffstat (limited to 'src/PGF')
-rw-r--r--src/PGF/Expr.hs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/PGF/Expr.hs b/src/PGF/Expr.hs
index ae9756cd8..97eb49f00 100644
--- a/src/PGF/Expr.hs
+++ b/src/PGF/Expr.hs
@@ -1,6 +1,11 @@
module PGF.Expr(Tree, Expr(..), Literal(..), Patt(..), Equation(..),
readExpr, showExpr, pExpr, ppExpr, ppPatt,
+ mkApp, unApp,
+ mkStr, unStr,
+ mkInt, unInt,
+ mkDouble, unDouble,
+
normalForm,
-- needed in the typechecker
@@ -81,6 +86,41 @@ showExpr vars = PP.render . ppExpr 0 vars
instance Read Expr where
readsPrec _ = RP.readP_to_S pExpr
+-- | Constructs an expression by applying a function to a list of expressions
+mkApp :: CId -> [Expr] -> Expr
+mkApp f es = foldl EApp (EFun f) es
+
+-- | Decomposes an expression into application of function
+unApp :: Expr -> Maybe (CId,[Expr])
+unApp = extract []
+ where
+ extract es (EFun f) = Just (f,es)
+ extract es (EApp e1 e2) = extract (e2:es) e1
+ extract es _ = Nothing
+
+-- | Constructs an expression from string literal
+mkStr :: String -> Expr
+mkStr s = ELit (LStr s)
+
+-- | Decomposes an expression into string literal
+unStr :: Expr -> Maybe String
+unStr (ELit (LStr s)) = Just s
+
+-- | Constructs an expression from integer literal
+mkInt :: Integer -> Expr
+mkInt i = ELit (LInt i)
+
+-- | Decomposes an expression into integer literal
+unInt :: Expr -> Maybe Integer
+unInt (ELit (LInt i)) = Just i
+
+-- | Constructs an expression from real number literal
+mkDouble :: Double -> Expr
+mkDouble f = ELit (LFlt f)
+
+-- | Decomposes an expression into real number literal
+unDouble :: Expr -> Maybe Double
+unDouble (ELit (LFlt f)) = Just f
-----------------------------------------------------
-- Parsing