-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTypes.hs
76 lines (62 loc) · 1.8 KB
/
Types.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Types where
import Data.Map as M
data MoleculeType =
TBool
| TInt
| TLam MoleculeType MoleculeType -- Function type
deriving (Eq)
showType :: MoleculeType -> String
showType TBool = "Bool"
showType TInt = "Int"
showType (TLam t1 t2) = showType t1 ++ " -> " ++ showType t2
instance Show MoleculeType where
show = showType
type Name = String
data MoleculeValue =
VBool Bool
| VInt Int
| VLam Env Name MoleculeExpr
showValue :: MoleculeValue -> String
showValue (VBool b) = case b of
True -> "t"
False -> "f"
showValue (VInt n) = show n
showValue (VLam _ nm e) = "\\" ++ nm ++ " . " ++ show e
instance Show MoleculeValue where
show = showValue
data MoleculeExpr =
EVar Name
| ETrue
| EFalse
| EInt Int
| EAbs Name MoleculeExpr
| EApp MoleculeExpr MoleculeExpr
| MoleculeExpr :+: MoleculeExpr
| MoleculeExpr :|: MoleculeExpr
deriving Eq
-- naïve @TODO: Make better
showExpr :: MoleculeExpr -> String
showExpr (EVar nm) = nm
showExpr ETrue = "t"
showExpr EFalse = "f"
showExpr (EInt n) = show n
showExpr (a :+: b) = show a ++ " + " ++ show b
showExpr (a :|: b) = show a ++ " | " ++ show b
showExpr (EAbs nm e) = "\\" ++ nm ++ " . " ++ show e
-- NB. this in particular is ugly
showExpr (EApp e1 e2) = "(" ++ show e1 ++ ") (" ++ show e2 ++ ")"
instance Show MoleculeExpr where
show = showExpr
data MoleculeCrumb =
CPlus MoleculeExpr -- Came from (+ b)
| COr MoleculeExpr -- Came from (| a)
| CAbs Name -- Came from an abstraction
| CApp1 MoleculeExpr -- Came from first arg of application
| CApp2 MoleculeExpr -- Came from second arg of application
deriving (Show, Eq)
data MoleculeError =
TypeError String
| ParseError String
| RuntimeError String
deriving (Show, Eq)
type Env = M.Map Name MoleculeValue