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