-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST.hs
49 lines (38 loc) · 1.28 KB
/
AST.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# OPTIONS_GHC -Wall #-}
module AST where
type Name = String
type IsRec = Bool
recursive, nonRecursive :: IsRec
recursive = True
nonRecursive = False
data Expr a
= EVar Name -- Variables
| ENum Int -- Numbers
| EConstr Int Int -- Constructor tag arity
| EAp (Expr a) (Expr a) -- Applications
| ELet -- Let(rec) expressions
IsRec -- boolean with True = recursive,
[(a, Expr a)] -- Definitions
(Expr a) -- Body of let(rec)
| ECase -- Case expression
(Expr a) -- Expression to scrutinise
[Alter a] -- Alternatives
| ELam [a] (Expr a) -- Lambda abstractions
deriving (Show)
type CoreExpr = Expr Name
type Alter a = (Int, [a], Expr a)
type CoreAlt = Alter Name
type ScDefn a = (Name, [a], Expr a)
type CoreScDefn = ScDefn Name
type Program a = [ScDefn a]
type CoreProgram = Program Name
type LetDefn a = (a, Expr a)
type CoreLetDefn = LetDefn Name
bindersOf :: [(a,b)] -> [a]
bindersOf defns = [name | (name, _) <- defns]
rhssOf :: [(a,b)] -> [b]
rhssOf defns = [rhs | (_, rhs) <- defns]
isAtomicExpr :: Expr a -> Bool
isAtomicExpr (EVar _) = True
isAtomicExpr (ENum _) = True
isAtomicExpr _ = False