Merge branch 'EnvMonad'
[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) = undef
99 typeExpr (Op2Expr p e1 BiUnEqual e2) = undef
100 //char of int
101 typeExpr (Op2Expr p e1 BiLesser e2) = undef
102 typeExpr (Op2Expr p e1 BiGreater e2) = undef
103 typeExpr (Op2Expr p e1 BiLesserEq e2) = undef
104 typeExpr (Op2Expr p e1 BiGreaterEq e2) = undef
105 //bool
106 typeExpr (Op2Expr p e1 BiAnd e2) = undef
107 typeExpr (Op2Expr p e1 BiOr e2) = undef
108 //a
109 typeExpr (Op2Expr p e1 BiCons e2) = undef
110 //typeExpr (FunExpr Pos FunCall) = undef
111 //typeExpr (EmptyListExpr Pos) = undef
112 //typeExpr (VarExpr Pos VarDef) = undef //when checking var-expr, be sure to
113 //put the infered type
114 //in the context
115
116 class unify a :: Type a -> Env Type
117
118 instance unify Expr where
119 unify (_ ->> _) e = liftT $ Left $ ParseError (extrPos e)
120 "Expression cannot be a higher order function. Yet..."
121 unify VoidType e = liftT $ Left $ ParseError (extrPos e)
122 "Expression cannot be a Void type."
123 unify (IdType _) e = liftT $ Left $ ParseError (extrPos e)
124 "Expression cannot be an polymorf type."
125 unify VarType e = typeExpr e
126 //we have to cheat to decorate the error, can be done nicer?
127 unify t e = StateT $ \s0 -> let res = runStateT m s0 in case res of
128 Left err = Left $ decErr e err
129 Right t = Right t //note, t :: (Type, Gamma)
130 where m = typeExpr e >>= \tex-> unify t tex
131
132 instance unify Type where
133 unify IntType IntType = pure IntType
134 unify BoolType BoolType = pure BoolType
135 unify CharType CharType = pure CharType
136 unify t1 t2 = liftT $ Left $ UnifyError zero t1 t2
137
138 instance zero Pos where
139 zero = {line=0,col=0}
140
141 decErr :: Expr SemError -> SemError
142 decErr e (UnifyError _ t1 t2) = UnifyError (extrPos e) t1 t2
143 decErr e (ParseError _ s) = ParseError (extrPos e) s
144 decErr e err = err
145
146 dc2 :: Expr (Either SemError a) -> Either SemError a
147 dc2 e (Right t) = Right t
148 dc2 e (Left err) = Left err
149
150 extrPos :: Expr -> Pos
151 extrPos (VarExpr p _) = p
152 extrPos (Op2Expr p _ _ _) = p
153 extrPos (Op1Expr p _ _) = p
154 extrPos (IntExpr p _) = p
155 extrPos (CharExpr p _) = p
156 extrPos (BoolExpr p _) = p
157 extrPos (FunExpr p _) = p
158 extrPos (EmptyListExpr p) = p
159 extrPos (TupleExpr p _) = p