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