Merge branch 'master' of https://github.com/dopefishh/cc1516
[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 evalStateT 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 //we have to cheat to decorate the error, can be done nicer?
125 unify t e = StateT $ \s0 -> let res = runStateT m s0 in case res of
126 Left err = Left $ decErr e err
127 Right t = Right t //note, t :: (Type, Gamma)
128 where m = typeExpr e >>= \tex-> unify t tex
129
130 //unify e (IdType i) = unify (IdType i) e
131 //note, don't use putIdent as that will call unify again
132 //unify (IdType i) e=:(IdType j) = modify (\(st, r)->('Map'.put j e st, r))
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
163
164 instance toString Gamma where
165 toString (mp, _) = concat
166 [concat [k, ": ", toString v, "\n"]\\(k, v) <- 'Map'.toList mp]