replace ding in map'
[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 (UnifyErrorStub t1 t2) = toString (UnifyError {line=0,col=0} t1 t2)
56 toString (UnifyError p t1 t2) = concat [
57 toString p,
58 "SemError: Cannot unify types. Expected: ",
59 toString t1, ". Given: ", toString t2]
60
61 sem :: AST -> SemOutput
62 sem (AST vd fd) = case runStateT m ('Map'.newMap, getRandomStream 1) of
63 Left e = Left [e]
64 Right ((vds, fds), gamma) = Right ((AST vds fds), gamma)
65 where
66 m :: Env ([VarDecl], [FunDecl])
67 m = (mapM semVarDecl vd) >>= \vds ->
68 mapM semFunDecl fd >>= \fds ->
69 pure (vds, fds)
70
71 semFunDecl :: FunDecl -> Env FunDecl
72 semFunDecl f = pure f
73
74 semVarDecl :: VarDecl -> Env VarDecl
75 semVarDecl (VarDecl pos type ident ex) = unify type ex
76 >>= \t-> putIdent ident t >>| (pure $ VarDecl pos t ident ex)
77
78 typeExpr :: Expr -> Env Type
79 typeExpr (IntExpr _ _) = pure IntType
80 typeExpr (CharExpr _ _) = pure CharType
81 typeExpr (BoolExpr _ _) = pure BoolType
82 typeExpr (Op1Expr p UnNegation expr) = unify BoolType expr
83 typeExpr (Op1Expr p UnMinus expr) = unify IntType expr
84 typeExpr (TupleExpr p (e1, e2)) = typeExpr e1
85 >>= \t1-> typeExpr e2 >>= \t2-> pure $ TupleType (t1, t2)
86 //Int
87 typeExpr (Op2Expr p e1 BiPlus e2) = unify IntType e1 >>| unify IntType e2
88 typeExpr (Op2Expr p e1 BiMinus e2) = unify IntType e1 >>| unify IntType e2
89 typeExpr (Op2Expr p e1 BiTimes e2) = unify IntType e1 >>| unify IntType e2
90 typeExpr (Op2Expr p e1 BiDivide e2) = unify IntType e1 >>| unify IntType e2
91 typeExpr (Op2Expr p e1 BiMod e2) = unify IntType e1 >>| unify IntType e2
92 //bool, char of int
93 typeExpr (Op2Expr p e1 BiEquals e2) = typeExpr e1 >>= \t1 -> unify t1 e2
94 >>| pure BoolType //todo, actually check t1 in Char,Bool,Int
95 typeExpr (Op2Expr p e1 BiUnEqual e2) = typeExpr (Op2Expr p e1 BiEquals e2)
96 //char of int
97 typeExpr (Op2Expr p e1 BiLesser e2) = typeExpr e1 >>= \t1 -> unify t1 e2
98 >>| pure BoolType //todo, actually check t1 in Char, Int
99 typeExpr (Op2Expr p e1 BiGreater e2) = typeExpr (Op2Expr p e1 BiLesser e2)
100 typeExpr (Op2Expr p e1 BiLesserEq e2) = typeExpr (Op2Expr p e1 BiLesser e2)
101 typeExpr (Op2Expr p e1 BiGreaterEq e2) = typeExpr (Op2Expr p e1 BiLesser e2)
102 //bool
103 typeExpr (Op2Expr p e1 BiAnd e2) = unify BoolType e1 >>| unify BoolType e2
104 typeExpr (Op2Expr p e1 BiOr e2) = unify BoolType e1 >>| unify BoolType e2
105 //a
106 typeExpr (Op2Expr p e1 BiCons e2) = typeExpr e1 >>= \t1-> typeExpr e2
107 >>= \t2-> unify (ListType t1) t2
108 typeExpr (EmptyListExpr p) = freshIdent >>= \frsh-> let t = IdType frsh in
109 putIdent frsh t >>| pure t
110 //typeExpr (FunExpr p FunCall) = undef
111 //typeExpr (VarExpr Pos VarDef) = undef //when checking var-expr, be sure to
112 //put the infered type in the context
113
114 class unify a :: Type a -> Env Type
115
116 instance unify Expr where
117 unify (_ ->> _) e = liftT $ Left $ ParseError (extrPos e)
118 "Expression cannot be a higher order function. Yet..."
119 unify VoidType e = liftT $ Left $ ParseError (extrPos e)
120 "Expression cannot be a Void type."
121 unify (IdType _) e = liftT $ Left $ ParseError (extrPos e)
122 "Expression cannot be an polymorf type."
123 unify VarType e = typeExpr e
124 unify (IdType i) e = undef
125 //we have to cheat to decorate the error, can be done nicer?
126 unify t e = StateT $ \s0 -> let res = runStateT m s0 in case res of
127 Left err = Left $ decErr e err
128 Right t = Right t //note, t :: (Type, Gamma)
129 where m = typeExpr e >>= \tex-> unify t tex
130
131 replace :: String Type -> Env Void
132 replace ident type = get >>= \(st, fr)->put ('Map'.fromList $
133 map (itupdate ident type) ('Map'.toList st), fr)
134 where
135 itupdate :: String Type (String, Type) -> (String, Type)
136 itupdate ident newtype ov=:(key, IdType type) = if (ident == type)
137 (key, newtype) ov
138 itupdate ident newtype (key, TupleType (t1, t2))
139 # (_, t1) = itupdate ident newtype (key, t1)
140 # (_, t2) = itupdate ident newtype (key, t2)
141 = (key, TupleType (t1, t2))
142 itupdate ident newtype (key, ListType t1)
143 # (_, t1) = itupdate ident newtype (key, t1)
144 = (key, ListType t1)
145 itupdate _ _ k = k
146
147 instance unify Type where
148 unify IntType IntType = pure IntType
149 unify BoolType BoolType = pure BoolType
150 unify CharType CharType = pure CharType
151 unify (IdType i) t=:(IdType j) = replace i t >>| pure t
152 unify t (IdType i) = unify (IdType i) t
153 unify (IdType i) t = replace i t >>| pure t
154 //unify (ListType t1) (ListType t2) = unify t1 t2
155 unify t1 t2 = liftT $ Left $ UnifyError zero t1 t2
156
157 instance zero Pos where
158 zero = {line=0,col=0}
159
160 decErr :: Expr SemError -> SemError
161 decErr e (UnifyError _ t1 t2) = UnifyError (extrPos e) t1 t2
162 decErr e (ParseError _ s) = ParseError (extrPos e) s
163 decErr e err = err
164
165 dc2 :: Expr (Either SemError a) -> Either SemError a
166 dc2 e (Right t) = Right t
167 dc2 e (Left err) = Left err
168
169 extrPos :: Expr -> Pos
170 extrPos (VarExpr p _) = p
171 extrPos (Op2Expr p _ _ _) = p
172 extrPos (Op1Expr p _ _) = p
173 extrPos (IntExpr p _) = p
174 extrPos (CharExpr p _) = p
175 extrPos (BoolExpr p _) = p
176 extrPos (FunExpr p _) = p
177 extrPos (EmptyListExpr p) = p
178 extrPos (TupleExpr p _) = p
179
180 instance toString Gamma where
181 toString (mp, _) = concat
182 [concat [k, ": ", toString v, "\n"]\\(k, v) <- 'Map'.toList mp]