about summary refs log tree commit diff
path: root/src/AST.hs
diff options
context:
space:
mode:
authortzlil <tzlils@protonmail.com>2023-04-14 23:46:53 +0300
committertzlil <tzlils@protonmail.com>2023-04-14 23:46:53 +0300
commitfdf35536b66499884dd5b4e1740ac67e5cebb1a2 (patch)
treeb907edf782ebb58780d7fbfed084560626b94c74 /src/AST.hs
add homework material
Diffstat (limited to 'src/AST.hs')
-rw-r--r--src/AST.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/AST.hs b/src/AST.hs
new file mode 100644
index 0000000..456338e
--- /dev/null
+++ b/src/AST.hs
@@ -0,0 +1,28 @@
+-- | Abstract syntax tree for the untyped lambda calculus, plus some helpers.
+module AST (
+  Expr (..)
+) where
+
+-- | Lambda Expressions
+data Expr
+  = Var String
+  | Lam String Expr
+  | App Expr Expr
+  deriving (Eq, Ord)
+
+
+-- https://www.haskellforall.com/2020/11/pretty-print-syntax-trees-with-this-one.html
+showLam, showApp, showVar :: Expr -> String
+showLam (Lam i e) = "\\" ++ i ++ " . " ++ showLam e
+showLam e = showApp e
+
+showApp (App e1 e2) = showApp e1 ++ " " ++ showVar e2
+showApp e = showVar e
+
+showVar (Var i) = i
+showVar e = "(" ++ showLam e ++ ")"
+
+instance Show Expr where
+  show e = showLam e
+
+