From: pimjager Date: Wed, 13 Apr 2016 09:17:54 +0000 (+0200) Subject: polymorf emptylist X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=f5e125920d85bc53eda7b51f3f03e89fcaf3a0ce;p=cc1516.git polymorf emptylist --- diff --git a/AST.dcl b/AST.dcl index ac1cf4e..f15690d 100644 --- a/AST.dcl +++ b/AST.dcl @@ -9,7 +9,7 @@ from StdOverloaded import class toString :: Type = TupleType (Type, Type) | ListType Type - | IdType String + | IdType String | IntType | BoolType | CharType diff --git a/sem.icl b/sem.icl index 6e177ec..8224a4f 100644 --- a/sem.icl +++ b/sem.icl @@ -24,6 +24,7 @@ from parse import :: ParserOutput, :: Error :: Gamma :== ('Map'.Map String Type, [String]) :: Env a :== StateT Gamma (Either SemError) a +//StateT (Gamma -> Either SemError (a, Gamma)) //we need to redefine this even though it is in Control.Monad.State instance MonadTrans (StateT Gamma) where @@ -36,10 +37,11 @@ getRandomStream i = genIdents $ filter (isAlpha o toChar) (genRandInt i) where genIdents r = let (ic, r) = splitAt 5 r in [toString ic: genIdents r] -freshIdent :: Gamma -> (String, Gamma) -freshIdent (st, [ident:rest]) = case 'Map'.get ident st of - Nothing = (ident, (st, rest)) - _ = freshIdent (st, rest) +freshIdent :: Env String +freshIdent = get >>= \(st, [ident:rest])-> put (st, rest) + >>| case 'Map'.get ident st of + Nothing = pure ident + _ = freshIdent putIdent :: String Type -> Env Void putIdent i t = gets (\(st, r)->'Map'.get i st) >>= \mt -> case mt of @@ -66,13 +68,6 @@ where mapM semFunDecl fd >>= \fds -> pure (vds, fds) - - -splitEithers :: [Either a b] -> Either [a] [b] -splitEithers [] = Right [] -splitEithers [Right x:xs] = splitEithers xs >>= \rest->Right [x:rest] -splitEithers xs = Left $ [x\\(Left x)<-xs] - semFunDecl :: FunDecl -> Env FunDecl semFunDecl f = pure f @@ -111,7 +106,8 @@ typeExpr (Op2Expr p e1 BiOr e2) = unify BoolType e1 >>| unify BoolType e2 typeExpr (Op2Expr p e1 BiCons e2) = typeExpr e1 >>= \t1-> typeExpr e2 >>= \t2-> unify (ListType t1) t2 //typeExpr (FunExpr p FunCall) = undef -typeExpr (EmptyListExpr p) = pure $ ListType IntType //we'll need quantified types +typeExpr (EmptyListExpr p) = freshIdent >>= \frsh-> let t = IdType frsh in + putIdent frsh t >>| pure t //typeExpr (VarExpr Pos VarDef) = undef //when checking var-expr, be sure to //put the infered type in the context