mutual recursion type inference
[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.Map => qualified put, union, difference, find, updateAt
13 import Data.Maybe
14 import Data.Tuple
15 import Text
16
17 import ast, scc
18
19 import Text.GenPrint
20 import StdDebug
21
22 check :: [Function] -> Either [String] (Expression, [([Char], Scheme)])
23 check fs
24 # dups = filter (\x->length x > 1) (groupBy (\(Function i _ _) (Function j _ _)->i == j) fs)
25 | length dups > 0 = Left ["Duplicate functions: ":[toString n\\[(Function n _ _):_]<-dups]]
26 = case partition (\a->a=:(Function ['start'] _ _)) fs of
27 ([], _) = Left ["No start function defined"]
28 ([Function _ [] e], fs)
29 = (\x->(e, x)) <$> runInfer (infer preamble (makeExpression fs e))
30 ([Function _ _ _], _) = Left ["Start cannot have arguments"]
31
32 makeExpression :: [Function] Expression -> Expression
33 makeExpression fs start
34 = foldr mkExpr start $ scc [(l, vars e [])\\(l, e)<-nicefuns]
35 where
36 mkExpr :: [[Char]] -> (Expression -> Expression)
37 mkExpr scc = Let [(l, e)\\(l, e)<-nicefuns, s<-scc | s == l]
38
39 nicefuns :: [([Char], Expression)]
40 nicefuns = [(l, foldr ((o) o Lambda) id i e)\\(Function l i e)<-fs]
41
42 vars :: Expression [[Char]] -> [[Char]]
43 vars (Var v=:[m:_]) c = [v:c]
44 vars (App l r) c = vars l $ vars r c
45 vars (Lambda l e) c = [v\\v<-vars e c | v <> l]
46 vars (Let ns e) c = flatten
47 [ [v\\v<-vars e c | not (isMember v (map fst ns))]
48 : map (\(i, e)->[v\\v<-vars e [] | v <> i]) ns]
49 vars _ c = c
50
51 instance toString Scheme where
52 toString (Forall [] t) = toString t
53 toString (Forall as t) = concat ["A.", join " " (map toString as), ": ", toString t]
54
55 instance toString Type where
56 toString (TVar a) = toString a
57 toString TInt = "Int"
58 toString TBool = "Bool"
59 toString (a --> b) = concat ["(", toString a, ") -> ", toString b]
60
61 :: TypeEnv :== Map [Char] Scheme
62 preamble :: TypeEnv
63 preamble = fromList
64 [(['_if'], Forall [['_ift']]
65 $ TBool --> TVar ['_ift'] --> TVar ['_ift'] --> TVar ['_ift'])
66 ,(['_eq'], Forall [['_eq']] $ TInt --> TInt --> TBool)
67 ,(['_mul'], Forall [['_mul']] $ TInt --> TInt --> TInt)
68 ,(['_add'], Forall [['_add']] $ TInt --> TInt --> TInt)
69 ,(['_sub'], Forall [['_sub']] $ TInt --> TInt --> TInt)
70 ]
71 :: Subst :== Map [Char] Type
72
73 :: Infer a :== StateT [Int] (WriterT [([Char], Scheme)] (Either [String])) a
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 t1 t2 = err ["Cannot unify: ", toString t1, " with ", toString t2]
130
131 unifyl :: [Type] -> Infer Subst
132 unifyl [t1,t2:ts] = unify t1 t2 >>= \s->unifyl (map (apply s) [t2:ts])
133 unifyl _ = pure newMap
134
135 instantiate :: Scheme -> Infer Type
136 instantiate (Forall as t)
137 = sequence [fresh\\_<-as]
138 >>= \as`->pure (apply (fromList $ zip2 as as`) t)
139
140 generalize :: TypeEnv Type -> Scheme
141 generalize env t = Forall (difference (ftv t) (ftv env)) t
142
143 infer :: TypeEnv Expression -> Infer (Subst, Type)
144 infer env (Lit (Int _)) = pure (newMap, TInt)
145 infer env (Lit (Bool _)) = pure (newMap, TBool)
146 infer env (Var x) = case get x env of
147 Nothing = err ["Unbound variable: ", toString x]
148 Just s = (\x->(newMap, x)) <$> instantiate s
149 infer env (App e1 e2)
150 = fresh
151 >>= \tv-> infer env e1
152 >>= \(s1, t1)->infer (apply s1 env) e2
153 >>= \(s2, t2)->unify (apply s2 t1) (t2 --> tv)
154 >>= \s3-> pure (s1 oo s2 oo s3, apply s3 tv)
155 infer env (Lambda x b)
156 = fresh
157 >>= \tv-> infer ('Data.Map'.put x (Forall [] tv) env) b
158 >>= \(s1, t1)->pure (s1, apply s1 tv --> t1)
159 //Non recursion
160 //infer env (Let [(x, e1)] e2)
161 // = infer env e1
162 // >>= \(s1, t1)->infer ('Data.Map'.put x (generalize (apply s1 env) t1) env) e2
163 // >>= \(s2, t2)->pure (s1 oo s2, t2)
164 //Single recursion
165 //infer env (Let [(x, e1)] e2)
166 // = fresh
167 // >>= \tv-> let env` = 'Data.Map'.put x (Forall [] tv) env
168 // in infer env` e1
169 // >>= \(s1,t1)-> infer ('Data.Map'.put x (generalize (apply s1 env`) t1) env`) e2
170 // >>= \(s2, t2)->pure (s1 oo s2, t2)
171 //Multiple recursion
172 infer env (Let xs e2)
173 # (ns, bs) = unzip xs
174 = sequence [fresh\\_<-ns]
175 >>= \tvs-> let env` = foldr (\(k, v)->'Data.Map'.put k (Forall [] v)) env (zip2 ns tvs)
176 in unzip <$> sequence (map (infer env`) bs)
177 >>= \(ss,ts)-> unifyl ts
178 >>= \s-> liftT (tell [(n, generalize (apply s env`) t)\\t<-ts & n<-ns])
179 >>| let env`` = foldr (\(n, t) m->'Data.Map'.put n (generalize (apply s env`) t) m) env` (zip2 ns ts)
180 in infer env`` e2
181 >>= \(s2, t2)->pure (s oo s2, t2)