forked from tromp/hs2blc
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntax.hs
399 lines (322 loc) · 11.2 KB
/
Syntax.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
module Syntax where
import Data.Char(chr, ord)
import Data.List(find, nub, union, intersect, (\\))
import SCC
type Id = String
type Alt = ([Pat], Rhs)
type Expl = (Id, Scheme, [Alt])
type Impl = (Id, [Alt])
type BindGroup = ([Expl], [[Impl]])
type Program = [BindGroup]
data Kind = Star | Kfun Kind Kind
deriving (Eq, Show)
class Assoc a where
assocKey :: a -> String
assoc :: String -> [a] -> Maybe a
assoc key [] = Nothing
assoc key (x:xs) = if assocKey x == key then Just x else assoc key xs
-----------------------------------------------------------------------------
-- Type: Types
-----------------------------------------------------------------------------
data Type = TVar Tyvar
| TCon Tycon
| TAp Type Type
| TGen Int
| TSynonym Synonym [Type]
deriving Eq
instance Show Type where
showsPrec _ (TVar v) = shows v
showsPrec _ (TCon c) = shows c
showsPrec _ (TSynonym syn []) = shows syn
showsPrec p (TSynonym syn ts) = showParen (p > 2) f
where f = shows syn . (' ':) . g
g = foldr1 (\l r -> l . (' ':) . r) (map (showsPrec 3) ts)
showsPrec _ (TGen n) = (chr (ord 'a' + n) :)
showsPrec p tap@(TAp _ _) =
case t of
TCon tc | tyconName tc == "[]"
-> ('[':) . showsPrec 0 t1 . (']':)
where [t1] = ts
TCon tc | tyconName tc == "(->)"
-> showParen (p > 0) $
showsPrec 1 t1 . (" -> " ++) . showsPrec 0 t2
where [t1, t2] = ts
TCon tc | tyconName tc == "(,)"
-> showParen True $
foldr1 (\f g -> f . (", " ++) . g)
(map (showsPrec 0) ts)
_ -> showParen (p > 2) $
foldr1 (\f g -> f . (' ':) . g)
(map (showsPrec 3) (t:ts))
where (t:ts) = fromTAp tap
fromTAp :: Type -> [Type]
fromTAp (TAp t1 t2) = fromTAp t1 ++ [t2]
fromTAp t = [t]
data Tyvar = Tyvar Id Kind deriving Eq
instance Show Tyvar where
show (Tyvar id _) = id
data Tycon = Tycon { tyconName::Id,
tyconKind::Kind,
tyconNumCon::Int,
tyconArities::[Int]
} deriving Eq
instance Show Tycon where
show tc = tyconName tc
data Synonym = Synonym Id Kind [Tyvar] Type deriving Eq
instance Show Synonym where
show (Synonym id _ _ _) = id
instance Assoc Tycon where
assocKey tc = tyconName tc
instance Assoc Synonym where
assocKey (Synonym i _ _ _) = i
unsynonym :: Synonym -> [Type] -> Type
unsynonym (Synonym _ _ vs t) ts = apply s t
where s = zip vs ts
tChar = TCon (Tycon "Char" Star 0 [])
tInt = TCon (Tycon "Int" Star 0 [])
tBool = TCon (Tycon "Bool" Star 2 [0,0])
tUnit = TCon (Tycon "()" Star 1 [0])
tList = TCon (Tycon "[]" (Kfun Star Star) 2 [2,0])
tArrow = TCon (Tycon "(->)" (Kfun Star (Kfun Star Star)) 0 [])
tString :: Type
tString = list tChar
preludeTycons :: [Tycon]
preludeTycons = [Tycon "()" Star 1 [0],
Tycon "Char" Star 0 [],
Tycon "Int" Star 0 [],
Tycon "Bool" Star 2 [0,0],
Tycon "[]" (Kfun Star Star) 2 [2,0],
Tycon "(->)" (Kfun Star (Kfun Star Star)) 0 []
]
preludeSynonyms :: [Synonym]
preludeSynonyms = [Synonym "String" Star [] (list tChar)
]
preludeConstrs :: [Const]
preludeConstrs = [Const { conName = i,
conArity = a,
conTag = tag,
conTycon = tycon,
conScheme = quantifyAll' t }
| (i, a, tag, TCon tycon, t) <- constrs]
where a = TVar (Tyvar "a" Star)
constrs = [("True", 0, 1, tBool, tBool),
("False", 0, 2, tBool, tBool),
(":", 2, 1, tList, a `fn` list a `fn` list a),
("[]", 0, 2, tList, list a)]
eTrue = Con con
where Just con = find (\c -> conName c == "True") preludeConstrs
eFalse = Con con
where Just con = find (\c -> conName c == "False") preludeConstrs
eCons = Con con
where Just con = find (\c -> conName c == ":") preludeConstrs
eNil = Con con
where Just con = find (\c -> conName c == "[]") preludeConstrs
infixr 4 `fn`
fn :: Type -> Type -> Type
a `fn` b = TAp (TAp tArrow a) b
list :: Type -> Type
list t = TAp tList t
pair :: Type -> Type -> Type
pair a b = TCon (tupTycon 2) `fn` a `fn` b
class HasKind t where
kind :: t -> Kind
instance HasKind Tyvar where
kind (Tyvar v k) = k
instance HasKind Tycon where
kind tc = tyconKind tc
instance HasKind Synonym where
kind (Synonym _ k _ _) = k
instance HasKind Type where
kind (TCon tc) = kind tc
kind (TVar u) = kind u
kind (TAp t _) = case (kind t) of
(Kfun _ k) -> k
kind (TSynonym syn ts) = kind (unsynonym syn ts)
type Subst = [(Tyvar, Type)]
class Types t where
apply :: Subst -> t -> t
tv :: t -> [Tyvar]
instance Types Type where
apply s (TVar u) = case lookup u s of
Just t -> t
Nothing -> TVar u
apply s (TAp l r) = TAp (apply s l) (apply s r)
apply s t = t
tv (TVar u) = [u]
tv (TAp l r) = tv l `union` tv r
tv t = []
instance Types a => Types [a] where
apply s = map (apply s)
tv = nub . concat . map tv
-- Predicates
data Qual t = [Pred] :=> t
deriving Eq
data Pred = IsIn Id Type
deriving Eq
instance Types t => Types (Qual t) where
apply s (ps :=> t) = apply s ps :=> apply s t
tv (ps :=> t) = tv ps `union` tv t
instance Types Pred where
apply s (IsIn i t) = IsIn i (apply s t)
tv (IsIn i t) = tv t
instance (Show t) => Show (Qual t) where
showsPrec _ ([] :=> t) = shows t
showsPrec _ (p :=> t) = showsContext . (" => " ++) . shows t
where showsContext = showParen True $
foldr1 (\f g -> f . (", " ++) . g) (map shows p)
instance Show Pred where
showsPrec _ (IsIn id t) = (id ++) . (' ':) . shows t
-- Type schemes
data Scheme = Forall [Kind] (Qual Type)
deriving Eq
instance Show Scheme where
showsPrec _ (Forall _ qt) = shows qt
instance Types Scheme where
apply s (Forall ks t) = Forall ks (apply s t)
tv (Forall ks t) = tv t
quantify :: [Tyvar] -> Qual Type -> Scheme
quantify vs qt = Forall ks (apply s qt)
where vs' = [ v | v <- tv qt, v `elem` vs ]
ks = map kind vs'
s = zip vs' (map TGen [0..])
quantifyAll :: Qual Type -> Scheme
quantifyAll t = quantify (tv t) t
quantifyAll' :: Type -> Scheme
quantifyAll' t = quantify (tv t) ([] :=> t)
toScheme :: Type -> Scheme
toScheme t = Forall [] ([] :=> t)
-- Assumptions
data Assump = Id :>: Scheme
instance Show Assump where
show (i :>: sc) = show i ++ " :: " ++ show sc
instance Types Assump where
apply s (i :>: sc) = i :>: (apply s sc)
tv (i :>: sc) = tv sc
findAssump :: Monad m => Id -> [Assump] -> m Scheme
findAssump id [] = fail ("unbound identifier: " ++ id)
findAssump id ((i:>:sc):as) = if i == id then return sc else findAssump id as
-- Literals
data Literal = LitInt Int
| LitChar String
| LitStr String
deriving Eq
instance Show Literal where
show (LitInt n) = show n
show (LitChar c) = c
show (LitStr s) = s
-- Patterns
data Pat = PVar Id
| PWildcard
| PAs Id Pat
| PLit Literal
| PCon Const [Pat]
data Expr = Var Id
| Lit Literal
| Con Const
| Ap Expr Expr
| Let BindGroup Expr
| Case Expr [(Pat, Rhs)]
| Lambda Alt
| ESign Expr Scheme
| RecPH Id
| ClassPH Pred
data Rhs = Rhs Expr
| Where BindGroup Rhs
| Guarded [(Expr, Expr)]
data Const = Const { conName::Id,
conArity::Int,
conTag::Int,
conTycon::Tycon,
conScheme::Scheme }
instance Eq Const where
c1 == c2 = conName c1 == conName c2
ap :: Expr -> [Expr] -> Expr
ap = foldl Ap
bindings :: BindGroup -> [Impl]
bindings (es, iss) = [(i, as) | (i, _, as) <- es] ++ concat iss
class HasVar t where
freeVars :: t -> [Id]
instance HasVar Expr where
freeVars (Var i) = [i]
freeVars (Ap e1 e2) = freeVars e1 `union` freeVars e2
freeVars (Let bg e) = fvBindGroup bg `union`
(freeVars e \\ map fst (bindings bg))
freeVars (Case e pses) = foldr union fve fvas
where fve = freeVars e
fvas = [freeVars e' \\ patVars p | (p, e') <- pses]
freeVars (Lambda a) = fvAlt a
freeVars (ESign e _) = freeVars e
freeVars _ = []
instance HasVar Rhs where
freeVars (Rhs e) = freeVars e
freeVars (Where bg rhs) =
fvBindGroup bg `union` (freeVars rhs \\ map fst (bindings bg))
freeVars (Guarded pairs) =
foldr union [] [freeVars e `union` freeVars e' | (e, e') <- pairs]
fvBindGroup :: BindGroup -> [Id]
fvBindGroup bg = fvAlts (concat altss) \\ is
where (is, altss) = unzip (bindings bg)
fvAlts :: [Alt] -> [Id]
fvAlts alts = foldl1 union (map fvAlt alts)
fvAlt :: Alt -> [Id]
fvAlt (ps, rhs) = freeVars rhs \\ concat (map patVars ps)
patVars :: Pat -> [Id]
patVars (PVar i) = [i]
patVars (PAs i p) = i : patVars p
patVars (PCon _ ps) = concat (map patVars ps)
patVars _ = []
tupcon :: Int -> Const
tupcon n = Const "(,)" n 1 tycon sc
where tycon = tupTycon n
tuptype = foldl TAp (TCon tycon) tvars
{-
tvars = [TVar (Tyvar ('v' : show i) Star) | i <- [0..n-1]]
scheme = quantifyAll (foldr fn tuptype tvars)
-}
tvars = [TGen i | i <- [0..n-1]]
sc = Forall (replicate n Star) ([] :=> foldr fn tuptype tvars)
tupTycon :: Int -> Tycon
tupTycon n = Tycon "(,)" (foldr Kfun Star (replicate n Star)) 1 [0]
tuple :: [Expr] -> Expr
tuple es = foldl Ap (Con $ tupcon $ length es) es
tupleSelector :: String -> Int -> Int -> Impl
tupleSelector id k n = (id, [([pat], Rhs expr)])
where pat = PCon (tupcon n) [PVar ('e' : show i) | i <- [0..n-1]]
expr = Var ('e' : show k)
-- type class
type Class = ([Id], [Inst], [Assump])
type Inst = (Qual Pred, Expr)
data ClassEnv = ClassEnv { classes :: Id -> Maybe Class,
defaults :: [Type],
impls :: [Impl],
expls :: [Expl],
assumps :: [Assump] }
type EnvTransformer = ClassEnv -> Maybe ClassEnv
idEnvTransformer :: EnvTransformer
idEnvTransformer ce = Just ce
infixr 5 <:>
(<:>) :: EnvTransformer -> EnvTransformer -> EnvTransformer
(f <:> g) ce = do ce' <- f ce
g ce'
-- SKI expression
data SKI = SAp SKI SKI
| SLit Literal
| SVar Id
| SCon Int Int
sap :: SKI -> [SKI] -> SKI
sap = foldl SAp
instance Show SKI where
show e = showsPrec 1 e ""
showsPrec _ (SVar i) = (i++)
showsPrec _ (SLit l) = shows l
showsPrec _ (SCon k n) = ('@':) . shows k . ('_':) . shows n
showsPrec _ (SAp e1 e2) = ('`':) . shows e1 . shows e2
-- showsPrec p (SAp e1 e2) = showParen (p > 0) $
-- showsPrec 0 e1 . (' ':) . showsPrec 1 e2
dependency :: [Impl] -> [[Impl]]
dependency bs = (map . map) (\v -> (v, lookup' v bs)) (reverse vss)
where vs = map fst bs
vss = scc [(v, fvAlts alts `intersect` vs) | (v, alts) <- bs]
lookup' key xs = case lookup key xs of
Just x -> x
Nothing -> error "cannot occur"