something something tuples
[minfp.git] / check.icl
1 implementation module check
2
3 import StdEnv
4
5 import Control.Monad => qualified join
6 import Control.Monad.State
7 import Control.Monad.Trans
8 import Control.Monad.Writer
9 import Data.Either
10 import Data.Func
11 import Data.List
12 import Data.Tuple
13 import Data.Map => qualified put, union, difference, find, updateAt
14 import Data.Maybe
15 import Text
16
17 import ast, scc
18
19 check :: [Function] -> Either [String] (Expression, [([Char], Scheme)])
20 check fs
21 # dups = filter (\x->length x > 1) (groupBy (\(Function i _ _) (Function j _ _)->i == j) fs)
22 | length dups > 0 = Left ["Duplicate functions: ":[toString n\\[(Function n _ _):_]<-dups]]
23 = case partition (\a->a=:(Function ['start'] _ _)) fs of
24 ([], _) = Left ["No start function defined"]
25 ([Function _ [] e:_], fs)
26 # e = makeExpression fs e
27 = (\x->(e, x)) <$> runInfer (infer (fromList builtin) e)
28 ([Function _ _ _:_], _) = Left ["Start cannot have arguments"]
29 where
30 builtin =
31 [(['_if'], Forall [['a']] $ TBool --> TVar ['a'] --> TVar ['a'] --> TVar ['a'])
32 ,(['_fst'], Forall [['a'], ['b']] $ TTuple (TVar ['a']) (TVar ['b']) --> TVar ['a'])
33 ,(['_snd'], Forall [['a'], ['b']] $ TTuple (TVar ['a']) (TVar ['b']) --> TVar ['b'])
34 ,(['_eq'], Forall [] $ TInt --> TInt --> TBool)
35 ,(['_mul'], Forall [] $ TInt --> TInt --> TInt)
36 ,(['_add'], Forall [] $ TInt --> TInt --> TInt)
37 ,(['_sub'], Forall [] $ TInt --> TInt --> TInt)
38 ]
39
40 makeExpression :: [Function] Expression -> Expression
41 makeExpression fs start = foldr mkExpr start $ scc [(l, vars e [])\\(l, e)<-nicefuns]
42 where
43 mkExpr :: [[Char]] -> (Expression -> Expression)
44 mkExpr scc = Let [(l, e)\\(l, e)<-nicefuns, s<-scc | s == l]
45
46 nicefuns :: [([Char], Expression)]
47 nicefuns = [(l, foldr (\x c->Lambda x o c) id i e)\\(Function l i e)<-fs]
48
49 vars :: Expression [[Char]] -> [[Char]]
50 vars (Var v=:[m:_]) c = [v:c]
51 vars (App l r) c = vars l $ vars r c
52 vars (Lambda l e) c = [v\\v<-vars e c | v <> l]
53 vars (Let ns e) c = flatten
54 [ [v\\v<-vars e c | not (isMember v (map fst ns))]
55 : map (\(i, e)->[v\\v<-vars e [] | v <> i]) ns]
56 vars _ c = c
57
58 instance toString Scheme where
59 toString (Forall [] t) = toString t
60 toString (Forall as t) = concat ["A.", join " " (map toString as), ": ", toString t]
61
62 instance toString Type where
63 toString (TVar a) = toString a
64 toString (TTuple a b) = concat ["(", toString a, ",", toString b, ")"]
65 toString TInt = "Int"
66 toString TBool = "Bool"
67 toString (a --> b) = concat ["(", toString a, " -> ", toString b, ")"]
68
69 :: TypeEnv :== Map [Char] Scheme
70 :: Subst :== Map [Char] Type
71
72 :: Infer a :== StateT [Int] (WriterT [([Char], Scheme)] (Either [String])) a
73
74 runInfer :: (Infer (Subst, Type)) -> Either [String] [([Char], Scheme)]
75 runInfer i = case runWriterT (evalStateT i [0..]) of
76 Left e = Left e
77 Right ((s, t), w) = pure [(['start'], generalize newMap (apply s t)):w]
78
79 fresh :: Infer Type
80 fresh = getState >>= \[s:ss]->put ss >>| pure (TVar (['v':[c\\c<-:toString s]]))
81
82 (oo) infixl 9 :: Subst Subst -> Subst
83 (oo) s1 s2 = 'Data.Map'.union (apply s1 <$> s2) s1
84
85 class Substitutable a where
86 apply :: Subst a -> a
87 ftv :: a -> [[Char]]
88
89 instance Substitutable Type where
90 apply s t=:(TVar v) = fromMaybe t (get v s)
91 apply s (t1 --> t2) = apply s t1 --> apply s t2
92 apply _ x = x
93
94 ftv (TVar v) = [v]
95 ftv (t1 --> t2) = on union ftv t1 t2
96 ftv _ = []
97
98 instance Substitutable Scheme where
99 apply s (Forall as t) = Forall as $ apply (foldr del s as) t
100 ftv (Forall as t) = difference (ftv t) (removeDup as)
101
102 instance Substitutable TypeEnv where
103 apply s env = apply s <$> env
104 ftv env = ftv (elems env)
105
106 instance Substitutable [a] | Substitutable a where
107 apply s l = apply s <$> l
108 ftv t = foldr (union o ftv) [] t
109
110 occursCheck :: [Char] -> (a -> Bool) | Substitutable a
111 occursCheck a = isMember a o ftv
112
113 err :: [String] -> Infer a
114 err e = liftT (liftT (Left e))
115
116 unify :: Type Type -> Infer Subst
117 unify (l --> r) (l` --> r`)
118 = unify l l`
119 >>= \s1->on unify (apply s1) r r`
120 >>= \s2->pure (s1 oo s2)
121 unify (TVar a) (TVar t)
122 | a == t = pure newMap
123 unify (TVar a) t
124 | occursCheck a t = err ["Infinite type: ", toString a, " to ", toString t]
125 = pure (singleton a t)
126 unify t (TVar a) = unify (TVar a) t
127 unify TInt TInt = pure newMap
128 unify TBool TBool = pure newMap
129 unify (TTuple l r) (TTuple l` r`)
130 = unify l l`
131 >>= \s1->on unify (apply s1) r r`
132 >>= \s2->pure (s1 oo s2)
133 unify t1 t2 = err ["Cannot unify: ", toString t1, " with ", toString t2]
134
135 unifyl :: [Type] -> Infer Subst
136 unifyl [t1,t2:ts] = unify t1 t2 >>= \s->unifyl (map (apply s) [t2:ts])
137 unifyl _ = pure newMap
138
139 instantiate :: Scheme -> Infer Type
140 instantiate (Forall as t)
141 = sequence [fresh\\_<-as]
142 >>= \as`->pure (apply (fromList $ zip2 as as`) t)
143
144 generalize :: TypeEnv Type -> Scheme
145 generalize env t = Forall (difference (ftv t) (ftv env)) t
146
147 infer :: TypeEnv Expression -> Infer (Subst, Type)
148 infer env (Lit (Int _)) = pure (newMap, TInt)
149 infer env (Lit (Bool _)) = pure (newMap, TBool)
150 infer env (Var x) = maybe (err ["Unbound variable: ", toString x])
151 (\s->tuple newMap <$> instantiate s) $ get x env
152 infer env (App e1 e2)
153 = fresh
154 >>= \tv-> infer env e1
155 >>= \(s1, t1)->infer (apply s1 env) e2
156 >>= \(s2, t2)->unify (apply s2 t1) (t2 --> tv)
157 >>= \s3-> pure (s3 oo s2 oo s1, apply s3 tv)
158 infer env (Tuple a b)
159 = infer env a
160 >>= \(s1, t1)->infer env b
161 >>= \(s2, t2)->pure (s1 oo s2, TTuple t1 t2)
162 infer env (Lambda x b)
163 = fresh
164 >>= \tv-> infer ('Data.Map'.put x (Forall [] tv) env) b
165 >>= \(s1, t1)->pure (s1, apply s1 tv --> t1)
166 //Non recursion
167 //infer env (Let [(x, e1)] e2)
168 // = infer env e1
169 // >>= \(s1, t1)->infer ('Data.Map'.put x (generalize (apply s1 env) t1) env) e2
170 // >>= \(s2, t2)->liftT (tell [(x, Forall [] t1)])
171 // >>| pure (s1 oo s2, t2)
172 //Single recursion
173 //infer env (Let [(x, e1)] e2)
174 // = fresh
175 // >>= \tv-> let env` = 'Data.Map'.put x (Forall [] tv) env
176 // in infer env` e1
177 // >>= \(s1,t1)-> infer ('Data.Map'.put x (generalize (apply s1 env`) t1) env`) e2
178 // >>= \(s2, t2)->pure (s1 oo s2, t2)
179 //Multiple recursion
180 infer env (Let xs e2)
181 # (ns, bs) = unzip xs
182 = sequence [fresh\\_<-ns]
183 >>= \tvs-> let env` = foldr (\(k, v)->'Data.Map'.put k (Forall [] v)) env (zip2 ns tvs)
184 in unzip <$> sequence (map (infer env`) bs)
185 >>= \(ss,ts)-> unifyl ts
186 >>= \s-> liftT (tell [(n, generalize (apply s env`) t)\\t<-ts & n<-ns])
187 >>| let env`` = foldr (\(n, s, t) m->'Data.Map'.put n (generalize (apply s env`) t) m) env` (zip3 ns ss ts)
188 in infer env`` e2
189 >>= \(s2, t2)->pure (s oo s2, t2)