polymorf emptylist
authorpimjager <pim@pimjager.nl>
Wed, 13 Apr 2016 09:17:54 +0000 (11:17 +0200)
committerpimjager <pim@pimjager.nl>
Wed, 13 Apr 2016 09:17:54 +0000 (11:17 +0200)
AST.dcl
sem.icl

diff --git a/AST.dcl b/AST.dcl
index ac1cf4e..f15690d 100644 (file)
--- a/AST.dcl
+++ b/AST.dcl
@@ -9,7 +9,7 @@ from StdOverloaded import class toString
 :: Type 
        = TupleType (Type, Type)
        | ListType Type
 :: Type 
        = TupleType (Type, Type)
        | ListType Type
-       | IdType String
+       | IdType String 
        | IntType 
        | BoolType
        | CharType
        | IntType 
        | BoolType
        | CharType
diff --git a/sem.icl b/sem.icl
index 6e177ec..8224a4f 100644 (file)
--- 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
 
 :: 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
 
 //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]
 
        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
 
 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)
 
         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
 
 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 (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
 
 //typeExpr (VarExpr Pos VarDef) = undef //when checking var-expr, be sure to
     //put the infered type in the context