about summary refs log tree commit diff
path: root/src/AST.hs
blob: 6f21d9e34bfb3804a4c8cee52f2ca552dcfa428a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- | 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