-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
306 lines (267 loc) · 8.56 KB
/
main.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
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use list comprehension" #-}
{-# HLINT ignore "Use zipWithM" #-}
{-# HLINT ignore "Use section" #-}
import Control.Monad.Trans.State
import qualified Data.Bifunctor as Bifunctor
import qualified Data.Char as Char
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified System.Environment as Environment
type Name = String
data AST
= AstUnit
| AstBool Bool
| AstInt Int
| AstStr String
| Id Name
| Let Name AST AST
| Abs Name AST
| Apply AST AST
deriving (Read)
prettyAst :: AST -> String
prettyAst AstUnit = "()"
prettyAst (AstBool n) = show n
prettyAst (AstInt n) = show n
prettyAst (AstStr n) = show n
prettyAst (Id n) = n
prettyAst (Let n i1 i2) =
"let " ++ n ++ " = " ++ prettyAst i1 ++ " in " ++ prettyAst i2
prettyAst (Abs n i) = "\\" ++ n ++ " -> " ++ prettyAst i
prettyAst (Apply i1 i2) = "(" ++ prettyAst i1 ++ " " ++ prettyAst i2 ++ ")"
data Type
= TConst String
| TVar String
| TApply String [Type]
| TForall [String] Type
| TError String
deriving (Eq, Ord)
tUnit = TConst "()"
tBool = TConst "Bool"
tInt = TConst "Int"
tStr = TConst "Str"
tFn t0 t1 = TApply "->" [t0, t1]
prettyType' (TConst x) = x
prettyType' (TVar x) = x
prettyType' (TApply "->" [t0@(TApply "->" _), t1]) =
"(" ++ prettyType' t0 ++ ")->" ++ prettyType' t1
prettyType' (TApply "->" [t0, t1]) = prettyType' t0 ++ "->" ++ prettyType' t1
prettyType' (TApply n t) = n ++ " " ++ unwords (fmap prettyTypeR t)
prettyType' (TForall q t) = unwords q ++ " . " ++ prettyType' t
prettyType' (TError "") = "Error"
prettyType' (TError e) = "Error(" ++ e ++ ")"
prettyTypeR t@(TApply _ _) = "(" ++ prettyType' t ++ ")"
prettyTypeR t@(TForall _ _) = "(" ++ prettyType' t ++ ")"
prettyTypeR t = prettyType' t
prettyType t = ":" ++ prettyTypeR t
data ASTTC
= TcUnit Type
| TcBool Type Bool
| TcInt Type Int
| TcStr Type String
| TcId Type Name
| TcLet Type Name Type ASTTC ASTTC
| TcAbs Type Name ASTTC
| TcApply Type ASTTC ASTTC
typeOf :: ASTTC -> Type
typeOf (TcUnit t) = t
typeOf (TcBool t n) = t
typeOf (TcInt t n) = t
typeOf (TcStr t n) = t
typeOf (TcId t _) = t
typeOf (TcLet t n t' i1 i2) = t
typeOf (TcAbs t n i) = t
typeOf (TcApply t i1 i2) = t
prettyAstTc :: ASTTC -> String
prettyAstTc (TcUnit t) = "()" ++ prettyType t
prettyAstTc (TcBool t n) = show n ++ prettyType t
prettyAstTc (TcInt t n) = show n ++ prettyType t
prettyAstTc (TcStr t n) = show n ++ prettyType t
prettyAstTc (TcId t n) = n ++ prettyType t
prettyAstTc (TcLet t n t' i1 i2) =
"(let " ++
n ++
prettyType t' ++
" = " ++ prettyAstTc i1 ++ " in " ++ prettyAstTc i2 ++ ")" ++ prettyType t
prettyAstTc (TcAbs t n i) =
"(\\" ++ n ++ " -> " ++ prettyAstTc i ++ ")" ++ prettyType t
prettyAstTc (TcApply t i1 i2) =
"(" ++ prettyAstTc i1 ++ " " ++ prettyAstTc i2 ++ ")" ++ prettyType t
type Ctx = Map.Map String Type
flattenError (TError s) = TError ""
flattenError t = t
type TypeVars = Map.Map Type Type
type CheckState = (TypeVars, Int)
newvar :: State CheckState Type
newvar = do
(tv, st) <- get
let new = st + 1
put (tv, new)
pure $ TVar $ "t" ++ show new
makeRoot :: Type -> State CheckState Type
makeRoot (TApply n t) = do
t' <- mapM makeRoot t
pure $ TApply n t'
makeRoot t@(TVar x) = do
found <- find t
if t == found
then pure found
else makeRoot found
makeRoot t = pure t
find :: Type -> State CheckState Type
find t = do
(tv, _) <- get
case Map.lookup t tv of
Nothing -> pure t
Just t0 -> find t0
setTypeReference :: Type -> Type -> State CheckState ()
setTypeReference ta tb = do
(tv, st) <- get
put (Map.insert ta tb tv, st)
pure ()
unify :: Type -> Type -> State CheckState (Either String ())
unify a b = do
ta <- find a
tb <- find b
unify' ta tb
firstLeft :: [Either a b] -> Either a ()
firstLeft ((Left l):es) = Left l
firstLeft ((Right l):es) = firstLeft es
firstLeft [] = Right ()
isIn :: Type -> Type -> Bool
isIn ta tb
| ta == tb = True
isIn ta (TApply _ ts) = any (isIn ta) ts
isIn ta (TForall _ tb) = ta `isIn` tb
isIn _ _ = False
unify' :: Type -> Type -> State CheckState (Either String ())
unify' (TError e) _ = pure $ Left ""
unify' _ (TError e) = pure $ Left ""
unify' ta tb
| ta == tb = pure $ Right ()
unify' ta@(TApply c0 p0) tb@(TApply c1 p1) =
if c0 == c1 && length p0 == length p1
then do
m <- mapM (uncurry unify) (zip p0 p1)
pure $ firstLeft m
else pure $ Left "Different type structure"
unify' ta tb@(TVar _) = unify' tb ta
unify' ta@(TVar _) tb = do
if ta `isIn` tb
then pure $ Left "Infinite type"
else do
setTypeReference ta tb
pure $ Right ()
unify' ta tb =
pure $
Left $
"Type '" ++ prettyType' ta ++ "' does not match '" ++ prettyType' tb ++ "'"
replace :: Type -> (Type, Type) -> Type
replace (TApply n t) (sub, r) = TApply n (fmap (flip replace (sub, r)) t)
replace (TForall q t) (sub, r) =
error "TForall quantifier in replace should not happen"
replace t (sub, r)
| sub == t = r
| otherwise = t
inst :: Type -> State CheckState Type
inst (TForall quantifiers t) = do
substitutions <- mapM createSubstitution (fmap TVar quantifiers)
pure $ foldl replace t substitutions
where
createSubstitution :: Type -> State CheckState (Type, Type)
createSubstitution s = do
t <- newvar
pure (s, t)
inst t = pure t
-- TODO do this in a nicer way
collectNewTypeVars :: Int -> Type -> [Type]
collectNewTypeVars n t = Set.toList $ Set.fromList $ collectNewTypeVars' n t
where
collectNewTypeVars' :: Int -> Type -> [Type]
collectNewTypeVars' _ t@(TConst _) = []
collectNewTypeVars' n t@(TVar ('t':m)) =
if n < read m
then [t]
else []
collectNewTypeVars' n t@(TApply _ ts) = concatMap (collectNewTypeVars' n) ts
collectNewTypeVars' n t@(TForall _ p) = collectNewTypeVars' n p
collectNewTypeVars' _ t@(TError _) = []
qualify :: Int -> Type -> Type
qualify n t =
let newTypeVars :: [Type] = collectNewTypeVars n t
names = fmap (\i -> [Char.chr (97 + i)]) [0 ..]
substitutions :: [(Type, Type)] =
fmap (Bifunctor.second TVar) (zip newTypeVars names)
in TForall (take (length newTypeVars) names) $ foldl replace t substitutions
checkSt :: (Ctx, AST) -> State CheckState ASTTC
checkSt (ctx, AstUnit) = pure $ TcUnit tUnit
checkSt (ctx, AstBool b) = pure $ TcBool tBool b
checkSt (ctx, AstInt i) = pure $ TcInt tInt i
checkSt (ctx, AstStr s) = pure $ TcStr tStr s
checkSt (ctx, Id x) = do
case Map.lookup x ctx of
Nothing -> pure $ TcId (TError $ "Unbound identifier " ++ x) x
Just s -> do
t <- inst s
pure $ TcId t x
checkSt (ctx, Let x e0 e1) = do
(_, st) <- get
e0tc <- checkSt (ctx, e0)
-- TODO add x = newvar to the context before checking e0 to support recursive definitions
let t = flattenError (typeOf e0tc)
let s = qualify st t
let newCtx = Map.insert x s ctx
e1tc <- checkSt (newCtx, e1)
t'' <- makeRoot (flattenError (typeOf e1tc))
pure $ TcLet t'' x s e0tc e1tc
checkSt (ctx, Abs x e) = do
t <- newvar
let newCtx = Map.insert x t ctx
etc <- checkSt (newCtx, e)
t' <- makeRoot (tFn t (typeOf etc))
pure $ TcAbs t' x etc
checkSt (ctx, Apply e0 e1) = do
e0tc <- checkSt (ctx, e0)
let t0 = typeOf e0tc
e1tc <- checkSt (ctx, e1)
let t1 = typeOf e1tc
t' <- newvar
unifyResult <- unify t0 (tFn t1 t')
let t = either TError (const t') unifyResult
t'' <- makeRoot t
pure $ TcApply t'' e0tc e1tc
binaryOp t = tFn t (tFn t t)
tkvMap = TApply "Map" [TVar "k", TVar "v"]
builtin :: Ctx
builtin =
Map.fromList
[ ("+", binaryOp tInt)
, ("-", binaryOp tInt)
, ("++", binaryOp tStr)
, ("&&", binaryOp tBool)
, ("||", binaryOp tBool)
, ("Map.empty", TForall ["k", "v"] tkvMap)
, ( "Map.insert"
, TForall ["k", "v"] (tFn (TVar "k") (tFn (TVar "v") (tFn tkvMap tkvMap))))
, ("getLine", TApply "IO" [tStr])
, ("putStrLn", tFn tStr (TApply "IO" [tUnit]))
, ( ">>="
, TForall
["a", "b"]
(tFn
(TApply "IO" [TVar "a"])
(tFn
(tFn (TVar "a") (TApply "IO" [TVar "b"]))
(TApply "IO" [TVar "b"]))))
]
check :: AST -> (ASTTC, CheckState)
check p = runState (checkSt (builtin, p)) (Map.empty, 0)
checkProgram :: AST -> IO ()
checkProgram program = do
putStrLn $ prettyAst program
let (checkedProgram, st) = check program
putStrLn $ prettyAstTc checkedProgram
main = do
args <- Environment.getArgs
prg <- read <$> readFile (head args)
checkProgram prg