All expression can now be typed.
[cc1516.git] / sem.icl
1 implementation module sem
2
3 import qualified Data.Map as Map
4 from Data.Func import $
5 import Data.Maybe
6 import Data.Either
7 import Data.Functor
8 import Control.Applicative
9 import Control.Monad
10 import Control.Monad.State
11 import Control.Monad.Identity
12 import Math.Random
13 import Control.Monad.Trans
14 import StdMisc
15 from StdFunc import id, const, o
16 import StdString
17 import StdTuple
18 import StdList
19
20 from Text import class Text(concat), instance Text String
21
22 import AST
23 from parse import :: ParserOutput, :: Error
24
25 :: Gamma :== ('Map'.Map String Type, [String])
26 :: Env a :== StateT Gamma (Either SemError) a
27 //StateT (Gamma -> Either SemError (a, Gamma))
28
29 //we need to redefine this even though it is in Control.Monad.State
30 instance MonadTrans (StateT Gamma) where
31 liftT m = StateT \s-> m >>= \a-> return (a, s)
32
33 get = gets id
34
35 getRandomStream :: Int -> [String]
36 getRandomStream i = genIdents $ filter (isAlpha o toChar) (genRandInt i)
37 where
38 genIdents r = let (ic, r2) = splitAt 5 r in [toString ic: genIdents r2]
39
40 freshIdent :: Env String
41 freshIdent = get >>= \(st, [ident:rest])-> put (st, rest)
42 >>| case 'Map'.get ident st of
43 Nothing = pure ident
44 _ = freshIdent
45
46 putIdent :: String Type -> Env Void
47 putIdent i t = gets (\(st, r)->'Map'.get i st) >>= \mt -> case mt of
48 Nothing = modify (\(st, r)->('Map'.put i t st, r))
49 Just t2 = unify t t2 >>= \t3-> modify (\(st, r)->('Map'.put i t3 st, r))
50
51 instance toString SemError where
52 toString (ParseError p e) = concat [
53 toString p,"SemError: ParseError: ", e]
54 toString (Error e) = "SemError: " +++ e
55 toString (UnifyError p t1 t2) = concat [
56 toString p,
57 "SemError: Cannot unify types. Expected: ",
58 toString t1, ". Given: ", toString t2]
59 toString (FieldSelectorError p t fs) = concat [
60 toString p,
61 "SemError: Cannot select ", toString fs, " from type: ",
62 toString t]
63 toString (OperatorError p o t) = concat [
64 toString p,
65 "SemError: No ", toString o, " for type ",
66 toString t]
67 toString (UndeclaredVariableError p ident) = concat [
68 toString p, "SemError: identifier: ", ident, " undefined."]
69
70 sem :: AST -> SemOutput
71 sem (AST vd fd) = case runStateT m ('Map'.newMap, getRandomStream 1) of
72 Left e = Left [e]
73 Right ((vds, fds), gamma) = Right ((AST vds fds), gamma)
74 where
75 m :: Env ([VarDecl], [FunDecl])
76 m = mapM semFunDecl fd >>= \fds ->
77 mapM semVarDecl vd >>= \vds ->
78 pure (vds, fds)
79
80 semFunDecl :: FunDecl -> Env FunDecl
81 semFunDecl fd=:(FunDecl p f _ mt vds stmts) = case mt of
82 Nothing = let t = IdType f in putIdent f t >>| pure fd
83 Just t = putIdent f t >>| pure fd
84
85 semVarDecl :: VarDecl -> Env VarDecl
86 semVarDecl (VarDecl pos type ident ex) = unify type ex
87 >>= \t-> putIdent ident t >>| (pure $ VarDecl pos t ident ex)
88
89 typeOp2 :: Expr Expr Op2 [Type] Type -> Env Type
90 typeOp2 e1 e2 op ts ret = typeExpr e1 >>= \t1-> typeExpr e2 >>= \t2->
91 unify t1 t2 >>= \t3->if (isMember t3 ts) (pure ret)
92 (liftT $ Left $ OperatorError (extrPos e1) op t3)
93
94 typeExpr :: Expr -> Env Type
95 typeExpr (IntExpr _ _) = pure IntType
96 typeExpr (CharExpr _ _) = pure CharType
97 typeExpr (BoolExpr _ _) = pure BoolType
98 typeExpr (Op1Expr p UnNegation expr) = unify BoolType expr
99 typeExpr (Op1Expr p UnMinus expr) = unify IntType expr
100 typeExpr (TupleExpr p (e1, e2)) = typeExpr e1
101 >>= \t1-> typeExpr e2 >>= \t2-> pure $ TupleType (t1, t2)
102 typeExpr (Op2Expr p e1 op e2)
103 | isMember op [BiPlus, BiMinus, BiTimes, BiDivide, BiMod] =
104 typeOp2 e1 e2 op [IntType] IntType
105 | isMember op [BiEquals, BiUnEqual] =
106 typeOp2 e1 e2 op [IntType, BoolType, CharType] BoolType
107 | isMember op [BiLesser, BiGreater, BiLesserEq, BiGreaterEq] =
108 typeOp2 e1 e2 op [IntType, CharType] BoolType
109 | isMember op [BiAnd, BiOr] =
110 typeOp2 e1 e2 op [BoolType] BoolType
111 | op == BiCons = typeExpr e1 >>= \t1-> typeExpr e2
112 >>= \t2-> unify (ListType t1) t2
113 typeExpr (EmptyListExpr p) = freshIdent >>= \frsh-> let t = IdType frsh in
114 putIdent frsh t >>| pure t
115 typeExpr (FunExpr p (FunCall f es)) = gets (\(st, r)->'Map'.get f st)
116 >>= \mt-> case mt of
117 Nothing = freshIdent >>= \frsh-> buildFunctionType frsh es
118 >>= \ft-> putIdent f ft >>| (pure $ IdType frsh)
119 Just t = unifyApp t es
120 typeExpr (VarExpr p (VarDef ident fs)) = gets (\(st, r)->'Map'.get ident st)
121 >>= \mt->case mt of
122 Nothing = liftT $ Left $ UndeclaredVariableError p ident
123 Just t = unify t fs
124
125 buildFunctionType :: String [Expr] -> Env Type
126 buildFunctionType frsh [] = let t = IdType frsh in putIdent frsh t >>| pure t
127 buildFunctionType frsh [e:es] = (->>) <$> typeExpr e <*> buildFunctionType frsh es
128
129 unifyApp :: Type [Expr] -> Env Type
130 unifyApp t [] = pure t //whoop whoop, functions can return functions
131 unifyApp (tf1 ->> tf2) [t1:ts] = unify tf1 t1 >>| unifyApp tf2 ts
132 unifyApp t1 t2 = liftT $ Left $ UnifyError zero t1 (IdType "[expressions, FIXME]")
133
134 class unify a :: Type a -> Env Type
135
136 instance unify [FieldSelector] where
137 unify t [] = pure t
138 unify (ListType t) [FieldHd:fs] = unify t fs
139 unify t=:(ListType _) [FieldTl:fs] = unify t fs
140 unify (TupleType (t, _)) [FieldFst:fs] = unify t fs
141 unify (TupleType (_, t)) [FieldSnd:fs] = unify t fs
142 unify t [fs:_] = liftT $ Left $ FieldSelectorError zero t fs
143
144 instance unify Expr where
145 unify (_ ->> _) e = liftT $ Left $ ParseError (extrPos e)
146 "Expression cannot be a higher order function. Yet..."
147 unify VoidType e = liftT $ Left $ ParseError (extrPos e)
148 "Expression cannot be a Void type."
149 unify (IdType _) e = liftT $ Left $ ParseError (extrPos e)
150 "Expression cannot be an polymorf type."
151 unify VarType e = typeExpr e
152 unify (IdType i) e = undef
153 //we have to cheat to decorate the error, can be done nicer?
154 unify t e = StateT $ \s0 -> let res = runStateT m s0 in case res of
155 Left err = Left $ decErr e err
156 Right t = Right t //note, t :: (Type, Gamma)
157 where m = typeExpr e >>= \tex-> unify t tex
158
159 replace :: String Type -> Env Void
160 replace ident type = get >>= \(st, fr)->put ('Map'.fromList $
161 map (itupdate ident type) ('Map'.toList st), fr)
162 where
163 itupdate :: String Type (String, Type) -> (String, Type)
164 itupdate ident newtype ov=:(key, IdType type) = if (ident == type)
165 (key, newtype) ov
166 itupdate ident newtype (key, TupleType (t1, t2))
167 # (_, t1) = itupdate ident newtype (key, t1)
168 # (_, t2) = itupdate ident newtype (key, t2)
169 = (key, TupleType (t1, t2))
170 itupdate ident newtype (key, ListType t1)
171 # (_, t1) = itupdate ident newtype (key, t1)
172 = (key, ListType t1)
173 itupdate _ _ k = k
174
175 instance unify Type where
176 unify IntType IntType = pure IntType
177 unify BoolType BoolType = pure BoolType
178 unify CharType CharType = pure CharType
179 unify (IdType i) t=:(IdType j) = replace i t >>| pure t
180 unify t (IdType i) = unify (IdType i) t
181 unify (IdType i) t = replace i t >>| pure t
182 unify (ListType t1) (ListType t2) = unify t1 t2 >>| (pure $ ListType t1)
183 unify (ta1 ->> ta2) (tb1 ->> tb2) = unify ta1 tb1 >>= \ta-> unify ta2 tb2
184 >>= \tb-> pure (ta ->> tb)
185 unify t1 t2 = liftT $ Left $ UnifyError zero t1 t2
186
187 instance zero Pos where
188 zero = {line=0,col=0}
189
190 decErr :: Expr SemError -> SemError
191 decErr e (UnifyError _ t1 t2) = UnifyError (extrPos e) t1 t2
192 decErr e (FieldSelectorError _ t fs) = FieldSelectorError (extrPos e) t fs
193 decErr e (ParseError _ s) = ParseError (extrPos e) s
194 decErr e err = err
195
196 dc2 :: Expr (Either SemError a) -> Either SemError a
197 dc2 e (Right t) = Right t
198 dc2 e (Left err) = Left err
199
200 extrPos :: Expr -> Pos
201 extrPos (VarExpr p _) = p
202 extrPos (Op2Expr p _ _ _) = p
203 extrPos (Op1Expr p _ _) = p
204 extrPos (IntExpr p _) = p
205 extrPos (CharExpr p _) = p
206 extrPos (BoolExpr p _) = p
207 extrPos (FunExpr p _) = p
208 extrPos (EmptyListExpr p) = p
209 extrPos (TupleExpr p _) = p
210
211 instance toString Gamma where
212 toString (mp, _) = concat
213 [concat [k, ": ", toString v, "\n"]\\(k, v) <- 'Map'.toList mp]
214
215
216 // class free a :: a -> Env [a]