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