2bef9ed9253d339af0c31c7824b25e7aaae7872e
[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 Data.Either
9 import Data.Func
10 import Data.List
11 import Data.Map => qualified put, union, difference, find, updateAt
12 import Data.Maybe
13 import Data.Tuple
14 import Text
15
16 import ast, scc
17
18 import Text.GenPrint
19 import StdDebug
20
21 check :: [Function] -> Either [String] (Expression, Scheme)
22 check fs
23 # dups = filter (\x->length x > 1) (groupBy (\(Function i _ _) (Function j _ _)->i == j) fs)
24 | length dups > 0 = Left ["Duplicate functions: ":[toString n\\[(Function n _ _):_]<-dups]]
25 = case partition (\a->a=:(Function ['start'] _ _)) fs of
26 ([], _) = Left ["No start function defined"]
27 ([Function _ [] e], fs)
28 // = (\x->(e, x)) <$> runInfer (infer preamble (makeExpression fs e))
29 = pure (makeExpression fs e, undef)
30 ([Function _ _ _], _) = Left ["Start cannot have arguments"]
31
32 makeExpression :: [Function] Expression -> Expression
33 makeExpression fs start
34 = mkExpr $ scc [(l, vars e [])\\(l, e)<-nicefuns]
35 where
36 mkExpr :: [[[Char]]] -> Expression
37 mkExpr t = trace_n (printToString t) start
38 nicefuns = [(l, foldr ((o) o Lambda) id i e)\\(Function l i e)<-fs]
39
40 vars :: Expression [[Char]] -> [[Char]]
41 vars (Var v=:[m:_]) c
42 | m <> '_' = [v:c]
43 vars (App l r) c = vars l $ vars r c
44 vars (Lambda l e) c = [v\\v<-vars e c | v <> l]
45 vars (Let ns e) c = vars e c // TODO
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 preamble :: TypeEnv
60 preamble = fromList
61 [(['_if'], Forall [['_ift']]
62 $ TBool --> TVar ['_ift'] --> TVar ['_ift'] --> TVar ['_ift'])
63 ,(['_eq'], Forall [['_eq']] $ TInt --> TInt --> TBool)
64 ,(['_mul'], Forall [['_mul']] $ TInt --> TInt --> TInt)
65 ,(['_add'], Forall [['_add']] $ TInt --> TInt --> TInt)
66 ,(['_sub'], Forall [['_sub']] $ TInt --> TInt --> TInt)
67 ]
68 :: Subst :== Map [Char] Type
69
70 :: Infer a :== StateT [Int] (Either [String]) a
71 runInfer :: (Infer (Subst, Type)) -> Either [String] Scheme
72 runInfer i = uncurry ((o) (generalize newMap) o apply)
73 <$> evalStateT i [0..]
74
75 fresh :: Infer Type
76 fresh = getState >>= \[s:ss]->put ss >>| pure (TVar (['v':[c\\c<-:toString s]]))
77
78 (oo) infixl 9 :: Subst Subst -> Subst
79 (oo) s1 s2 = 'Data.Map'.union (apply s1 <$> s2) s1
80
81 class Substitutable a where
82 apply :: Subst a -> a
83 ftv :: a -> [[Char]]
84
85 instance Substitutable Type where
86 apply s t=:(TVar v) = fromMaybe t (get v s)
87 apply s (t1 --> t2) = apply s t1 --> apply s t2
88 apply _ x = x
89
90 ftv (TVar v) = [v]
91 ftv (t1 --> t2) = on union ftv t1 t2
92 ftv _ = []
93
94 instance Substitutable Scheme where
95 apply s (Forall as t) = Forall as $ apply (foldr del s as) t
96 ftv (Forall as t) = difference (ftv t) (removeDup as)
97
98 instance Substitutable TypeEnv where
99 apply s env = apply s <$> env
100 ftv env = ftv (elems env)
101
102 instance Substitutable [a] | Substitutable a where
103 apply s l = apply s <$> l
104 ftv t = foldr (union o ftv) [] t
105
106 occursCheck :: [Char] -> (a -> Bool) | Substitutable a
107 occursCheck a = isMember a o ftv
108
109 unify :: Type Type -> Infer Subst
110 unify (l --> r) (l` --> r`)
111 = unify l l`
112 >>= \s1->on unify (apply s1) r r`
113 >>= \s2->pure (s1 oo s2)
114 unify (TVar a) (TVar t)
115 | a == t = pure newMap
116 unify (TVar a) t
117 | occursCheck a t = liftT (Left ["Infinite type: ", toString a, " to ", toString t])
118 = pure (singleton a t)
119 unify t (TVar a) = unify (TVar a) t
120 unify TInt TInt = pure newMap
121 unify TBool TBool = pure newMap
122 unify t1 t2 = liftT (Left ["Cannot unify: ", toString t1, " with ", toString t2])
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) = case get x env of
136 Nothing = liftT (Left ["Unbound variable: ", toString x])
137 Just s = (\x->(newMap, x)) <$> instantiate s
138 infer env (App e1 e2)
139 = fresh
140 >>= \tv-> infer env e1
141 >>= \(s1, t1)->infer (apply s1 env) e2
142 >>= \(s2, t2)->unify (apply s2 t1) (t2 --> tv)
143 >>= \s3-> pure (s1 oo s2 oo s3, apply s3 tv)
144 infer env (Lambda x b)
145 = fresh
146 >>= \tv-> infer ('Data.Map'.put x (Forall [] tv) env) b
147 >>= \(s1, t1)->pure (s1, apply s1 tv --> t1)
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)->pure (s1 oo s2, t2)
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)-> unify t1 tv
157 >>= \t->infer ('Data.Map'.put x (generalize (apply s1 env`) t1) env`) e2
158 >>= \(s2, t2)->pure (s1 oo s2, t2)
159 //infer env (Let xs e2)
160 // # (ns, bs) = unzip xs
161 // = sequence [fresh\\_<-ns]
162 // >>= \tvs-> let env` = foldr (uncurry putenv) env (zip2 ns tvs)
163 // in unzip <$> sequence (map infer env`) bs
164 // >>= \(ss,ts)-> let s = foldr (oo) newMap ss
165 // in //unify t1 tv
166 // >>= \t->infer ('Data.Map'.put x (generalize (apply s1 env`) t1) env`) e2
167 // >>= \(s2, t2)->pure (s1 oo s2, t2)
168 where
169 putenv :: [Char] -> (Type TypeEnv -> TypeEnv)
170 putenv k = 'Data.Map'.put k o Forall []
171
172 unifyl :: [Type] -> Infer Subst
173 unifyl [t1,t2:ts] = unify t1 t2 >>= \s->unifyl [t2:map (apply s) ts]
174 unifyl _ = pure newMap