yay, binary ops
[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
68 sem :: AST -> SemOutput
69 sem (AST vd fd) = case runStateT m ('Map'.newMap, getRandomStream 1) of
70 Left e = Left [e]
71 Right ((vds, fds), gamma) = Right ((AST vds fds), gamma)
72 where
73 m :: Env ([VarDecl], [FunDecl])
74 m = (mapM semVarDecl vd) >>= \vds ->
75 mapM semFunDecl fd >>= \fds ->
76 pure (vds, fds)
77
78 semFunDecl :: FunDecl -> Env FunDecl
79 semFunDecl f = pure f
80
81 semVarDecl :: VarDecl -> Env VarDecl
82 semVarDecl (VarDecl pos type ident ex) = unify type ex
83 >>= \t-> putIdent ident t >>| (pure $ VarDecl pos t ident ex)
84
85 typeOp2 :: Expr Expr Op2 [Type] -> Env Type
86 typeOp2 e1 e2 op ts = typeExpr e1 >>= \t1-> typeExpr e2 >>= \t2->
87 unify t1 t2 >>= \t3->if (isMember t3 ts) (pure t3)
88 (liftT $ Left $ OperatorError (extrPos e1) op t3)
89
90 typeExpr :: Expr -> Env Type
91 typeExpr (IntExpr _ _) = pure IntType
92 typeExpr (CharExpr _ _) = pure CharType
93 typeExpr (BoolExpr _ _) = pure BoolType
94 typeExpr (Op1Expr p UnNegation expr) = unify BoolType expr
95 typeExpr (Op1Expr p UnMinus expr) = unify IntType expr
96 typeExpr (TupleExpr p (e1, e2)) = typeExpr e1
97 >>= \t1-> typeExpr e2 >>= \t2-> pure $ TupleType (t1, t2)
98 typeExpr (Op2Expr p e1 op e2)
99 | isMember op [BiPlus, BiMinus, BiTimes, BiDivide, BiMod] =
100 typeOp2 e1 e2 op [IntType]
101 | isMember op [BiEquals, BiUnEqual] =
102 typeOp2 e1 e2 op [IntType, BoolType, CharType]
103 | isMember op [BiLesser, BiGreater, BiLesserEq, BiGreaterEq] =
104 typeOp2 e1 e2 op [IntType, CharType]
105 | isMember op [BiAnd, BiOr] =
106 typeOp2 e1 e2 op [BoolType]
107 | op == BiCons = typeExpr e1 >>= \t1-> typeExpr e2
108 >>= \t2-> unify (ListType t1) t2
109 typeExpr (EmptyListExpr p) = freshIdent >>= \frsh-> let t = IdType frsh in
110 putIdent frsh t >>| pure t
111 typeExpr (FunExpr p (FunCall fid args)) = undef
112 //ignore field selectors
113 typeExpr (VarExpr p (VarDef ident fs)) = gets (\(st, r)->'Map'.get ident st)
114 >>= \mt->case mt of
115 Nothing = let t = IdType ident in putIdent ident t >>| pure t
116 Just t = unify t fs
117
118 class unify a :: Type a -> Env Type
119
120 instance unify [FieldSelector] where
121 unify t [] = pure t
122 unify (ListType t) [FieldHd:fs] = unify t fs
123 unify t=:(ListType _) [FieldTl:fs] = unify t fs
124 unify (TupleType (t, _)) [FieldFst:fs] = unify t fs
125 unify (TupleType (_, t)) [FieldSnd:fs] = unify t fs
126 unify t [fs:_] = liftT $ Left $ FieldSelectorError zero t fs
127
128 instance unify Expr where
129 unify (_ ->> _) e = liftT $ Left $ ParseError (extrPos e)
130 "Expression cannot be a higher order function. Yet..."
131 unify VoidType e = liftT $ Left $ ParseError (extrPos e)
132 "Expression cannot be a Void type."
133 unify (IdType _) e = liftT $ Left $ ParseError (extrPos e)
134 "Expression cannot be an polymorf type."
135 unify VarType e = typeExpr e
136 unify (IdType i) e = undef
137 //we have to cheat to decorate the error, can be done nicer?
138 unify t e = StateT $ \s0 -> let res = runStateT m s0 in case res of
139 Left err = Left $ decErr e err
140 Right t = Right t //note, t :: (Type, Gamma)
141 where m = typeExpr e >>= \tex-> unify t tex
142
143 replace :: String Type -> Env Void
144 replace ident type = get >>= \(st, fr)->put ('Map'.fromList $
145 map (itupdate ident type) ('Map'.toList st), fr)
146 where
147 itupdate :: String Type (String, Type) -> (String, Type)
148 itupdate ident newtype ov=:(key, IdType type) = if (ident == type)
149 (key, newtype) ov
150 itupdate ident newtype (key, TupleType (t1, t2))
151 # (_, t1) = itupdate ident newtype (key, t1)
152 # (_, t2) = itupdate ident newtype (key, t2)
153 = (key, TupleType (t1, t2))
154 itupdate ident newtype (key, ListType t1)
155 # (_, t1) = itupdate ident newtype (key, t1)
156 = (key, ListType t1)
157 itupdate _ _ k = k
158
159 instance unify Type where
160 unify IntType IntType = pure IntType
161 unify BoolType BoolType = pure BoolType
162 unify CharType CharType = pure CharType
163 unify (IdType i) t=:(IdType j) = replace i t >>| pure t
164 unify t (IdType i) = unify (IdType i) t
165 unify (IdType i) t = replace i t >>| pure t
166 unify (ListType t1) (ListType t2) = unify t1 t2 >>| (pure $ ListType t1)
167 unify t1 t2 = liftT $ Left $ UnifyError zero t1 t2
168
169 instance zero Pos where
170 zero = {line=0,col=0}
171
172 decErr :: Expr SemError -> SemError
173 decErr e (UnifyError _ t1 t2) = UnifyError (extrPos e) t1 t2
174 decErr e (FieldSelectorError _ t fs) = FieldSelectorError (extrPos e) t fs
175 decErr e (ParseError _ s) = ParseError (extrPos e) s
176 decErr e err = err
177
178 dc2 :: Expr (Either SemError a) -> Either SemError a
179 dc2 e (Right t) = Right t
180 dc2 e (Left err) = Left err
181
182 extrPos :: Expr -> Pos
183 extrPos (VarExpr p _) = p
184 extrPos (Op2Expr p _ _ _) = p
185 extrPos (Op1Expr p _ _) = p
186 extrPos (IntExpr p _) = p
187 extrPos (CharExpr p _) = p
188 extrPos (BoolExpr p _) = p
189 extrPos (FunExpr p _) = p
190 extrPos (EmptyListExpr p) = p
191 extrPos (TupleExpr p _) = p
192
193 instance toString Gamma where
194 toString (mp, _) = concat
195 [concat [k, ": ", toString v, "\n"]\\(k, v) <- 'Map'.toList mp]
196
197
198 // class free a :: a -> Env [a]