fix check
[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, builtin
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
30 makeExpression :: [Function] Expression -> Expression
31 makeExpression fs start = foldr mkExpr start $ scc [(l, vars e [])\\(l, e)<-nicefuns]
32 where
33 mkExpr :: [[Char]] -> (Expression -> Expression)
34 mkExpr scc = Let [(l, e)\\(l, e)<-nicefuns, s<-scc | s == l]
35
36 nicefuns :: [([Char], Expression)]
37 nicefuns = [(l, foldr (\x c->Lambda x o c) id i e)\\(Function l i e)<-fs]
38
39 vars :: Expression [[Char]] -> [[Char]]
40 vars (Var v=:[m:_]) c = [v:c]
41 vars (App l r) c = vars l $ vars r c
42 vars (Lambda l e) c = [v\\v<-vars e c | v <> l]
43 vars (Let ns e) c = flatten
44 [ [v\\v<-vars e c | not (isMember v (map fst ns))]
45 : map (\(i, e)->[v\\v<-vars e [] | v <> i]) ns]
46 vars _ c = c
47
48 instance toString Scheme where
49 toString (Forall [] t) = toString t
50 toString (Forall as t) = concat ["A.", join " " (map toString as), ": ", toString t]
51
52 instance toString Type where
53 toString (TVar a) = toString a
54 toString TInt = "Int"
55 toString TBool = "Bool"
56 toString (a --> b) = concat ["(", toString a, " -> ", toString b, ")"]
57
58 :: TypeEnv :== Map [Char] Scheme
59 :: Subst :== Map [Char] Type
60
61 :: Infer a :== StateT [Int] (WriterT [([Char], Scheme)] (Either [String])) a
62
63 runInfer :: (Infer (Subst, Type)) -> Either [String] [([Char], Scheme)]
64 runInfer i = case runWriterT (evalStateT i [0..]) of
65 Left e = Left e
66 Right ((s, t), w) = pure [(['start'], generalize newMap (apply s t)):w]
67
68 fresh :: Infer Type
69 fresh = getState >>= \[s:ss]->put ss >>| pure (TVar (['v':[c\\c<-:toString s]]))
70
71 (oo) infixl 9 :: Subst Subst -> Subst
72 (oo) s1 s2 = 'Data.Map'.union (apply s1 <$> s2) s1
73
74 class Substitutable a where
75 apply :: Subst a -> a
76 ftv :: a -> [[Char]]
77
78 instance Substitutable Type where
79 apply s t=:(TVar v) = fromMaybe t (get v s)
80 apply s (t1 --> t2) = apply s t1 --> apply s t2
81 apply _ x = x
82
83 ftv (TVar v) = [v]
84 ftv (t1 --> t2) = on union ftv t1 t2
85 ftv _ = []
86
87 instance Substitutable Scheme where
88 apply s (Forall as t) = Forall as $ apply (foldr del s as) t
89 ftv (Forall as t) = difference (ftv t) (removeDup as)
90
91 instance Substitutable TypeEnv where
92 apply s env = apply s <$> env
93 ftv env = ftv (elems env)
94
95 instance Substitutable [a] | Substitutable a where
96 apply s l = apply s <$> l
97 ftv t = foldr (union o ftv) [] t
98
99 occursCheck :: [Char] -> (a -> Bool) | Substitutable a
100 occursCheck a = isMember a o ftv
101
102 err :: [String] -> Infer a
103 err e = liftT (liftT (Left e))
104
105 unify :: Type Type -> Infer Subst
106 unify (l --> r) (l` --> r`)
107 = unify l l`
108 >>= \s1->on unify (apply s1) r r`
109 >>= \s2->pure (s1 oo s2)
110 unify (TVar a) (TVar t)
111 | a == t = pure newMap
112 unify (TVar a) t
113 | occursCheck a t = err ["Infinite type: ", toString a, " to ", toString t]
114 = pure (singleton a t)
115 unify t (TVar a) = unify (TVar a) t
116 unify TInt TInt = pure newMap
117 unify TBool TBool = pure newMap
118 unify t1 t2 = err ["Cannot unify: ", toString t1, " with ", toString t2]
119
120 unifyl :: [Type] -> Infer Subst
121 unifyl [t1,t2:ts] = unify t1 t2 >>= \s->unifyl (map (apply s) [t2:ts])
122 unifyl _ = pure newMap
123
124 instantiate :: Scheme -> Infer Type
125 instantiate (Forall as t)
126 = sequence [fresh\\_<-as]
127 >>= \as`->pure (apply (fromList $ zip2 as as`) t)
128
129 generalize :: TypeEnv Type -> Scheme
130 generalize env t = Forall (difference (ftv t) (ftv env)) t
131
132 infer :: TypeEnv Expression -> Infer (Subst, Type)
133 infer env (Lit (Int _)) = pure (newMap, TInt)
134 infer env (Lit (Bool _)) = pure (newMap, TBool)
135 infer env (Var x) = maybe (err ["Unbound variable: ", toString x])
136 (\s->tuple newMap <$> instantiate s) $ get x env
137 infer env (App e1 e2)
138 = fresh
139 >>= \tv-> infer env e1
140 >>= \(s1, t1)->infer (apply s1 env) e2
141 >>= \(s2, t2)->unify (apply s2 t1) (t2 --> tv)
142 >>= \s3-> pure (s3 oo s2 oo s1, apply s3 tv)
143 infer env (Lambda x b)
144 = fresh
145 >>= \tv-> infer ('Data.Map'.put x (Forall [] tv) env) b
146 >>= \(s1, t1)->pure (s1, apply s1 tv --> t1)
147 //Non recursion
148 //infer env (Let [(x, e1)] e2)
149 // = infer env e1
150 // >>= \(s1, t1)->infer ('Data.Map'.put x (generalize (apply s1 env) t1) env) e2
151 // >>= \(s2, t2)->liftT (tell [(x, Forall [] t1)])
152 // >>| pure (s1 oo s2, t2)
153 //Single recursion
154 //infer env (Let [(x, e1)] e2)
155 // = fresh
156 // >>= \tv-> let env` = 'Data.Map'.put x (Forall [] tv) env
157 // in infer env` e1
158 // >>= \(s1,t1)-> infer ('Data.Map'.put x (generalize (apply s1 env`) t1) env`) e2
159 // >>= \(s2, t2)->pure (s1 oo s2, t2)
160 //Multiple recursion
161 infer env (Let xs e2)
162 # (ns, bs) = unzip xs
163 = sequence [fresh\\_<-ns]
164 >>= \tvs-> let env` = foldr (\(k, v)->'Data.Map'.put k (Forall [] v)) env (zip2 ns tvs)
165 in unzip <$> sequence (map (infer env`) bs)
166 >>= \(ss,ts)-> unifyl ts
167 >>= \s-> liftT (tell [(n, generalize (apply s env`) t)\\t<-ts & n<-ns])
168 >>| let env`` = foldr (\(n, s, t) m->'Data.Map'.put n (generalize (apply s env`) t) m) env` (zip3 ns ss ts)
169 in infer env`` e2
170 >>= \(s2, t2)->pure (s oo s2, t2)