-
Notifications
You must be signed in to change notification settings - Fork 0
/
Head.hs
102 lines (85 loc) · 3.47 KB
/
Head.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module Head where
type Index = Int
type Id = String
data TI a = TI (Index -> (a, Index))
type Subst = [(Id, SimpleType)]
data Type = Forall SimpleType | U SimpleType deriving (Eq, Show)
data Assump = Id :>: ConstrainedType deriving (Eq, Show)
data ConstrainedType = Constrained Type SConstraint deriving (Eq, Show)
data SimpleType = TVar Id
| TArr SimpleType SimpleType
| TCon Id
| TApp SimpleType SimpleType
| TGen Int
| TSkolem Id
| TGADT Id
| TRigid Id
deriving Eq
data Expr = Var Id
| App Expr Expr
| Lam Id Expr
| Con Id
| If Expr Expr Expr
| Case Expr [(Pat,Expr)]
| Let (Id,Expr) Expr
| LetA (Id,ConstrainedType,Expr) Expr
deriving (Eq, Show)
data Pat = PVar Id
| PCon Id [Pat]
deriving (Eq, Show)
data SConstraint = TEq SimpleType SimpleType
| Unt [SimpleType] [Id] SConstraint
| SConj [SConstraint]
| E
deriving Eq
data Constraint = Simp SConstraint
| Impl [SimpleType] [Id] SConstraint Constraint
| Conj [Constraint]
deriving Eq
instance Show SimpleType where
show (TVar i) = i
show (TArr (TArr a b) t') = "("++show (TArr a b)++")"++"->"++show t'
show (TArr t t') = "(" ++ show t++"->"++show t'++")"
show (TCon i) = i
show (TApp c v) = showApp (listApp c v)
show (TGen n) = "tg" ++ show n
show (TSkolem i) = "skoem" ++ show i
show (TRigid i) = "rigid" ++ show i
show (TGADT i) = "GADT " ++ i
instance Show SConstraint where
show (TEq t t') = "(" ++ show t ++ " ~ " ++ show t' ++ ")"
show (Unt as bs c) = show as ++ "(Forall " ++ show bs ++ "." ++ showInLine c ++ ")"
show E = "e"
show (SConj [c]) = show c
show (SConj (c:cs)) = show c ++ "\n" ++ show (SConj cs)
instance Show Constraint where
show (Simp c) = showInLine c
show (Impl as bs c f) = show as ++ "(Forall " ++ show bs ++ "." ++ showInLine c ++ " imp " ++ showInLine' f ++ ")"
show (Conj [c]) = show c
show (Conj (c:cs)) = show c ++ "\n" ++ show (Conj cs)
showApp [] = ""
showApp [x] = x
showApp (x:xs) = if (tup x) then "(" ++ showTuple (take n xs) ++ showApp (drop n xs) else x ++ " "++ showApp xs where
n = (length x - 1)
listApp (TApp a b) (TApp a' b') = (listApp a b) ++ ["(" ++ show (TApp a' b') ++ ")"]
listApp (TApp a b) v = (listApp a b) ++ [show v]
listApp v (TApp a b) = [show v] ++ ["(" ++ show (TApp a b) ++ ")"]
listApp c v = [show c] ++ [show v]
tup a = a !! 0 == '(' && a !! 1 == ','
showTuple [x] = x ++ ")"
showTuple (x:xs) = x ++ "," ++ showTuple xs
showInLine (SConj []) = ""
showInLine (SConj (E:cs)) = showInLine (SConj (filter notEmpty cs))
showInLine (SConj (c:cs)) = showInLine c ++ if rs /= SConj [] then " ^ " ++ showInLine rs else "" where rs = (SConj (filter notEmpty cs))
showInLine c = show c
showInLine' (Conj []) = ""
showInLine' (Conj ((Simp E):cs)) = showInLine' (Conj (filter notEmpty' cs))
showInLine' (Conj (c:cs)) = showInLine' c ++ if rs /= Conj [] then " ^ " ++ showInLine' rs else "" where rs = (Conj (filter notEmpty' cs))
showInLine' (Simp c) = showInLine c
showInLine' c = show c
notEmpty E = False
notEmpty (SConj cs) = (filter (notEmpty) cs) /= []
notEmpty _ = True
notEmpty' (Simp a) = notEmpty a
notEmpty' (Conj cs) = (filter (notEmpty') cs) /= []
notEmpty' _ = True