Added some expression typechecking
[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
28 //we need to redefine this even though it is in Control.Monad.State
29 instance MonadTrans (StateT Gamma) where
30 liftT m = StateT \s-> m >>= \a-> return (a, s)
31
32 get = gets id
33
34 getRandomStream :: Int -> [String]
35 getRandomStream i = genIdents $ filter (isAlpha o toChar) (genRandInt i)
36 where
37 genIdents r = let (ic, r) = splitAt 5 r in [toString ic: genIdents r]
38
39 freshIdent :: Gamma -> (String, Gamma)
40 freshIdent (st, [ident:rest]) = case 'Map'.get ident st of
41 Nothing = (ident, (st, rest))
42 _ = freshIdent (st, rest)
43
44 putIdent :: String Type -> Env Void
45 putIdent i t = gets (\(st, r)->'Map'.get i st) >>= \mt -> case mt of
46 Nothing = modify (\(st, r)->('Map'.put i t st, r))
47 Just t2 = unify t t2 >>= \t3-> modify (\(st, r)->('Map'.put i t3 st, r))
48
49 instance toString SemError where
50 toString (ParseError p e) = concat [
51 toString p,"SemError: ParseError: ", e]
52 toString (Error e) = "SemError: " +++ e
53 toString (UnifyErrorStub t1 t2) = toString (UnifyError {line=0,col=0} t1 t2)
54 toString (UnifyError p t1 t2) = concat [
55 toString p,
56 "SemError: Cannot unify types. Expected: ",
57 toString t1, ". Given: ", toString t2]
58
59 sem :: AST -> SemOutput
60 sem (AST vd fd) = case evalStateT m ('Map'.newMap, getRandomStream 0) of
61 Left e = Left [e]
62 Right (vds, fds) = Right (AST vds fds)
63 where
64 m :: Env (([VarDecl], [FunDecl]))
65 m = (mapM semVarDecl vd) >>= \vds ->
66 mapM semFunDecl fd >>= \fds ->
67 pure (vds, fds)
68
69
70
71 splitEithers :: [Either a b] -> Either [a] [b]
72 splitEithers [] = Right []
73 splitEithers [Right x:xs] = splitEithers xs >>= \rest->Right [x:rest]
74 splitEithers xs = Left $ [x\\(Left x)<-xs]
75
76 semFunDecl :: FunDecl -> Env FunDecl
77 semFunDecl f = pure f
78
79 semVarDecl :: VarDecl -> Env VarDecl
80 semVarDecl (VarDecl pos type ident ex) = unify type ex
81 >>= \t-> putIdent ident t >>| (pure $ VarDecl pos t ident ex)
82
83 typeExpr :: Expr -> Env Type
84 typeExpr (IntExpr _ _) = pure IntType
85 typeExpr (CharExpr _ _) = pure CharType
86 typeExpr (BoolExpr _ _) = pure BoolType
87 typeExpr (Op1Expr p UnNegation expr) = unify BoolType expr
88 typeExpr (Op1Expr p UnMinus expr) = unify IntType expr
89 typeExpr (TupleExpr p (e1, e2)) = typeExpr e1
90 >>= \t1-> typeExpr e2 >>= \t2-> pure $ TupleType (t1, t2)
91 //Int
92 typeExpr (Op2Expr p e1 BiPlus e2) = unify IntType e1 >>| unify IntType e2
93 typeExpr (Op2Expr p e1 BiMinus e2) = unify IntType e1 >>| unify IntType e2
94 typeExpr (Op2Expr p e1 BiTimes e2) = unify IntType e1 >>| unify IntType e2
95 typeExpr (Op2Expr p e1 BiDivide e2) = unify IntType e1 >>| unify IntType e2
96 typeExpr (Op2Expr p e1 BiMod e2) = unify IntType e1 >>| unify IntType e2
97 //bool, char of int
98 typeExpr (Op2Expr p e1 BiEquals e2) = typeExpr e1 >>= \t1 -> unify t1 e2
99 >>| pure BoolType //todo, actually check t1 in Char,Bool,Int
100 typeExpr (Op2Expr p e1 BiUnEqual e2) = typeExpr (Op2Expr p e1 BiEquals e2)
101 //char of int
102 typeExpr (Op2Expr p e1 BiLesser e2) = typeExpr e1 >>= \t1 -> unify t1 e2
103 >>| pure BoolType //todo, actually check t1 in Char, Int
104 typeExpr (Op2Expr p e1 BiGreater e2) = typeExpr (Op2Expr p e1 BiLesser e2)
105 typeExpr (Op2Expr p e1 BiLesserEq e2) = typeExpr (Op2Expr p e1 BiLesser e2)
106 typeExpr (Op2Expr p e1 BiGreaterEq e2) = typeExpr (Op2Expr p e1 BiLesser e2)
107 //bool
108 typeExpr (Op2Expr p e1 BiAnd e2) = unify BoolType e1 >>| unify BoolType e2
109 typeExpr (Op2Expr p e1 BiOr e2) = unify BoolType e1 >>| unify BoolType e2
110 //a
111 typeExpr (Op2Expr p e1 BiCons e2) = typeExpr e1 >>= \t1-> typeExpr e2
112 >>= \t2-> unify (ListType t1) t2
113 //typeExpr (FunExpr p FunCall) = undef
114 typeExpr (EmptyListExpr p) = pure $ ListType IntType //we'll need quantified types
115 //typeExpr (VarExpr Pos VarDef) = undef //when checking var-expr, be sure to
116 //put the infered type in the context
117
118 class unify a :: Type a -> Env Type
119
120 instance unify Expr where
121 unify (_ ->> _) e = liftT $ Left $ ParseError (extrPos e)
122 "Expression cannot be a higher order function. Yet..."
123 unify VoidType e = liftT $ Left $ ParseError (extrPos e)
124 "Expression cannot be a Void type."
125 unify (IdType _) e = liftT $ Left $ ParseError (extrPos e)
126 "Expression cannot be an polymorf type."
127 unify VarType e = typeExpr e
128 //we have to cheat to decorate the error, can be done nicer?
129 unify t e = StateT $ \s0 -> let res = runStateT m s0 in case res of
130 Left err = Left $ decErr e err
131 Right t = Right t //note, t :: (Type, Gamma)
132 where m = typeExpr e >>= \tex-> unify t tex
133
134 instance unify Type where
135 unify IntType IntType = pure IntType
136 unify BoolType BoolType = pure BoolType
137 unify CharType CharType = pure CharType
138 unify (ListType t1) (ListType t2) = unify t1 t2
139 unify t1 t2 = liftT $ Left $ UnifyError zero t1 t2
140
141 instance zero Pos where
142 zero = {line=0,col=0}
143
144 decErr :: Expr SemError -> SemError
145 decErr e (UnifyError _ t1 t2) = UnifyError (extrPos e) t1 t2
146 decErr e (ParseError _ s) = ParseError (extrPos e) s
147 decErr e err = err
148
149 dc2 :: Expr (Either SemError a) -> Either SemError a
150 dc2 e (Right t) = Right t
151 dc2 e (Left err) = Left err
152
153 extrPos :: Expr -> Pos
154 extrPos (VarExpr p _) = p
155 extrPos (Op2Expr p _ _ _) = p
156 extrPos (Op1Expr p _ _) = p
157 extrPos (IntExpr p _) = p
158 extrPos (CharExpr p _) = p
159 extrPos (BoolExpr p _) = p
160 extrPos (FunExpr p _) = p
161 extrPos (EmptyListExpr p) = p
162 extrPos (TupleExpr p _) = p