make work temporarily
[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 Text
14
15 import ast
16
17 check :: [Function] -> Either [String] (Expression, Scheme)
18 check fs
19 # dups = filter (\x->length x > 1) (groupBy (\(Function i _ _) (Function j _ _)->i == j) fs)
20 | length dups > 0 = Left ["Duplicate functions: ":[toString n\\[(Function n _ _):_]<-dups]]
21 = case partition (\a->a=:(Function ['start'] _ _)) fs of
22 ([], _) = Left ["No start function defined"]
23 ([Function _ [] e], fs)
24 # e = foldr (\(Function i a e)->Let [(i, (foldr ((o) o Lambda) id a e))]) e fs
25 = (\x->(e, x)) <$> runInfer (infer preamble e)
26 ([Function _ _ _], _) = Left ["Start cannot have arguments"]
27
28 instance toString Scheme where
29 toString (Forall [] t) = toString t
30 toString (Forall as t) = concat ["A.", join " " (map toString as), ": ", toString t]
31
32 instance toString Type where
33 toString (TVar a) = toString a
34 toString TInt = "Int"
35 toString TBool = "Bool"
36 toString (a --> b) = concat ["(", toString a, ") -> ", toString b]
37
38 :: TypeEnv :== Map [Char] Scheme
39 preamble :: TypeEnv
40 preamble = fromList
41 [(['_if'], Forall [['_ift']]
42 $ TBool --> TVar ['_ift'] --> TVar ['_ift'] --> TVar ['_ift'])
43 ,(['_eq'], Forall [['_eq']] $ TInt --> TInt --> TBool)
44 ,(['_mul'], Forall [['_mul']] $ TInt --> TInt --> TInt)
45 ,(['_add'], Forall [['_add']] $ TInt --> TInt --> TInt)
46 ,(['_sub'], Forall [['_sub']] $ TInt --> TInt --> TInt)
47 ]
48 :: Subst :== Map [Char] Type
49
50 :: Infer a :== StateT [Int] (Either [String]) a
51 runInfer :: (Infer (Subst, Type)) -> Either [String] Scheme
52 runInfer i = uncurry ((o) (generalize newMap) o apply)
53 <$> evalStateT i [0..]
54
55 fresh :: Infer Type
56 fresh = getState >>= \[s:ss]->put ss >>| pure (TVar (['v':[c\\c<-:toString s]]))
57
58 (oo) infixl 9 :: Subst Subst -> Subst
59 (oo) s1 s2 = 'Data.Map'.union (apply s1 <$> s2) s1
60
61 class Substitutable a where
62 apply :: Subst a -> a
63 ftv :: a -> [[Char]]
64
65 instance Substitutable Type where
66 apply s t=:(TVar v) = fromMaybe t (get v s)
67 apply s (t1 --> t2) = apply s t1 --> apply s t2
68 apply _ x = x
69
70 ftv (TVar v) = [v]
71 ftv (t1 --> t2) = on union ftv t1 t2
72 ftv _ = []
73
74 instance Substitutable Scheme where
75 apply s (Forall as t) = Forall as $ apply (foldr del s as) t
76 ftv (Forall as t) = difference (ftv t) (removeDup as)
77
78 instance Substitutable TypeEnv where
79 apply s env = apply s <$> env
80 ftv env = ftv (elems env)
81
82 instance Substitutable [a] | Substitutable a where
83 apply s l = apply s <$> l
84 ftv t = foldr (union o ftv) [] t
85
86 occursCheck :: [Char] -> (a -> Bool) | Substitutable a
87 occursCheck a = isMember a o ftv
88
89 unify :: Type Type -> Infer Subst
90 unify (l --> r) (l` --> r`)
91 = unify l l`
92 >>= \s1->on unify (apply s1) r r`
93 >>= \s2->pure (s1 oo s2)
94 unify (TVar a) (TVar t)
95 | a == t = pure newMap
96 unify (TVar a) t
97 | occursCheck a t = liftT (Left ["Infinite type: ", toString a, " to ", toString t])
98 = pure (singleton a t)
99 unify t (TVar a) = unify (TVar a) t
100 unify TInt TInt = pure newMap
101 unify TBool TBool = pure newMap
102 unify t1 t2 = liftT (Left ["Cannot unify: ", toString t1, " with ", toString t2])
103
104 instantiate :: Scheme -> Infer Type
105 instantiate (Forall as t)
106 = sequence [fresh\\_<-as]
107 >>= \as`->pure (apply (fromList $ zip2 as as`) t)
108
109 generalize :: TypeEnv Type -> Scheme
110 generalize env t = Forall (difference (ftv t) (ftv env)) t
111
112 infer :: TypeEnv Expression -> Infer (Subst, Type)
113 infer env (Lit (Int _)) = pure (newMap, TInt)
114 infer env (Lit (Bool _)) = pure (newMap, TBool)
115 infer env (Var x) = case get x env of
116 Nothing = liftT (Left ["Unbound variable: ", toString x])
117 Just s = (\x->(newMap, x)) <$> instantiate s
118 infer env (App e1 e2)
119 = fresh
120 >>= \tv-> infer env e1
121 >>= \(s1, t1)->infer (apply s1 env) e2
122 >>= \(s2, t2)->unify (apply s2 t1) (t2 --> tv)
123 >>= \s3-> pure (s1 oo s2 oo s3, apply s3 tv)
124 infer env (Lambda x b)
125 = fresh
126 >>= \tv-> infer ('Data.Map'.put x (Forall [] tv) env) b
127 >>= \(s1, t1)->pure (s1, apply s1 tv --> t1)
128 //infer env (Let [(x, e1)] e2)
129 // = infer env e1
130 // >>= \(s1, t1)->infer ('Data.Map'.put x (generalize (apply s1 env) t1) env) e2
131 // >>= \(s2, t2)->pure (s1 oo s2, t2)
132 infer env (Let [(x, e1)] e2)
133 = fresh
134 >>= \tv-> let env` = 'Data.Map'.put x (Forall [] tv) env
135 in infer env` e1
136 >>= \(s1,t1)-> unify t1 tv
137 >>= \t->infer ('Data.Map'.put x (generalize (apply s1 env`) t1) env`) e2
138 >>= \(s2, t2)->pure (s1 oo s2, t2)
139 //infer env (Let xs e2)
140 // # (ns, bs) = unzip xs
141 // = sequence [fresh\\_<-ns]
142 // >>= \tvs-> let env` = foldr (uncurry putenv) env (zip2 ns tvs)
143 // in unzip <$> sequence (map infer env`) bs
144 // >>= \(ss,ts)-> let s = foldr (oo) newMap ss
145 // in //unify t1 tv
146 // >>= \t->infer ('Data.Map'.put x (generalize (apply s1 env`) t1) env`) e2
147 // >>= \(s2, t2)->pure (s1 oo s2, t2)
148 where
149 putenv :: [Char] -> (Type TypeEnv -> TypeEnv)
150 putenv k = 'Data.Map'.put k o Forall []