aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2017-11-21 23:16:27 +0100
committerCamil Staps2017-11-21 23:16:27 +0100
commit76784d820a5f52d7541411100432336dd164e836 (patch)
treea5f6b3583144bf6291be5bc13849007993398fc0
parentInitial commit (diff)
Make App infix @
-rw-r--r--pf.icl38
1 files changed, 19 insertions, 19 deletions
diff --git a/pf.icl b/pf.icl
index c2e0ccb..0e8ff6a 100644
--- a/pf.icl
+++ b/pf.icl
@@ -7,7 +7,7 @@ import Data.List
= Lambda Ident Expr
| Ident Ident
| Literal Literal
- | App Expr Expr
+ | (@) infixl Expr Expr
:: Literal
= LitInt Int
@@ -19,7 +19,7 @@ hasIdent :: Ident Expr -> Bool
hasIdent id (Ident n) = id == n
hasIdent id (Literal _) = False
hasIdent id (Lambda n e) = id <> n && hasIdent id e
-hasIdent id (App f x) = hasIdent id f || hasIdent id x
+hasIdent id (f @ x) = hasIdent id f || hasIdent id x
print :: Expr -> String
print e = foldl (+++) "" (pr False e [])
@@ -35,9 +35,9 @@ where
getLambdas (Lambda x rhs) = let (is,e) = getLambdas rhs in ([x:is],e)
getLambdas e = ([], e)
- pr _ (Ident id) st = [id:st]
- pr _ (Literal l) st = pLit l st
- pr p (App f x) st = [if p "(" "":pr False f [" ":pr True x [if p ")" "":st]]]
+ pr _ (Ident id) st = [id:st]
+ pr _ (Literal l) st = pLit l st
+ pr p (f @ x) st = [if p "(" "":pr False f [" ":pr True x [if p ")" "":st]]]
pLit :: Literal [String] -> [String]
pLit (LitInt i) st = [toString i:st]
@@ -46,24 +46,24 @@ where
optim :: Expr -> Expr
optim (Lambda id e)
| hasIdent id e = case optim e of
- Ident id` -> if (id == id`) (Ident "id") (App (Ident "const") e)
- App f x -> case moveOutside id (App f x) of
- a=:(App f (Ident id`)) -> if (id == id`) f (App (Ident "const") a)
- a -> App (Ident "const") a
+ Ident id` -> if (id == id`) (Ident "id") (Ident "const" @ e)
+ f @ x -> case moveOutside id (f @ x) of
+ a=:(f @ Ident id`) -> if (id == id`) f (Ident "const" @ a)
+ a -> Ident "const" @ a
e -> Lambda id e
-| otherwise = App (Ident "const") (optim e)
+| otherwise = Ident "const" @ optim e
optim e = e
moveOutside :: Ident Expr -> Expr
moveOutside _ (Ident id) = Ident id
moveOutside _ (Literal l) = Literal l
-moveOutside id (App f x)
+moveOutside id (f @ x)
| hasIdent id f = case moveOutside id f of
- Ident id -> App (App (App (Ident "flip") (Ident "id")) x) (Ident id)
- App g (Ident id) -> App (App (App (Ident "flip") g) x) (Ident id)
+ Ident id -> Ident "flip" @ Ident "id" @ x @ Ident id
+ g @ Ident id -> Ident "flip" @ g @ x @ Ident id
| hasIdent id x = case moveOutside id x of
- e=:(Ident id) -> App f e
- App g e=:(Ident id) -> App (App (App (Ident "(o)") f) g) e
+ e=:(Ident id) -> f @ e
+ g @ e=:(Ident id) -> Ident "(o)" @ f @ g @ e
moveOutside _ (Lambda x e) = Lambda x e // TODO
Start = map do
@@ -77,10 +77,10 @@ Start = map do
, Lambda "x" (Lambda "y" (Lambda "z" (Ident "y")))
, Lambda "x" (Lambda "y" (Lambda "z" (Ident "z")))
, Lambda "x" (Lambda "y" (Lambda "z" (Literal (LitInt 37))))
- , Lambda "x" (Lambda "y" (App (Ident "x") (Ident "y")))
- , Lambda "x" (Lambda "y" (App (Ident "y") (Ident "x")))
- , Lambda "x" (Lambda "y" (App (Ident "y") (Literal (LitInt 10))))
- , Lambda "f" (Lambda "a" (Lambda "b" (Lambda "c" (Lambda "d" (App (App (App (App (Ident "f") (Ident "b")) (Ident "c")) (Ident "d")) (Ident "a"))))))
+ , Lambda "x" (Lambda "y" (Ident "x" @ Ident "y"))
+ , Lambda "x" (Lambda "y" (Ident "y" @ Ident "x"))
+ , Lambda "x" (Lambda "y" (Ident "y" @ Literal (LitInt 10)))
+ , Lambda "f" (Lambda "a" (Lambda "b" (Lambda "c" (Lambda "d" (Ident "f" @ Ident "b" @ Ident "c" @ Ident "d" @ Ident "a")))))
]
where
do e = (print e, " ==> ", print (optim e), "\n")