Added default functions, isEmpty does not typecheck...
[cc1516.git] / sem.icl
diff --git a/sem.icl b/sem.icl
index 082cfe9..0da9c77 100644 (file)
--- a/sem.icl
+++ b/sem.icl
@@ -26,7 +26,6 @@ from Text import class Text(concat), instance Text String
 
 import AST
 
-
 :: Scheme = Forall [TVar] Type
 :: Gamma :== 'Map'.Map String Scheme //map from Variables! to types
 :: Typing a :== StateT (Gamma, [TVar]) (Either SemError) a
@@ -49,11 +48,17 @@ instance zero Gamma where
 variableStream :: [TVar]
 variableStream = map toString [1..]
 
+defaultGamma :: Gamma //includes all default functions
+defaultGamma = extend "print" (Forall ["a"] ((IdType "a") ->> VoidType))
+                $ extend "isEmpty" (Forall ["a"] (ListType (IdType "a") ->> BoolType))
+                $ extend "read" (Forall [] (IntType ->> (ListType CharType)))
+                zero
+
 sem :: AST -> Either [SemError] AST
 sem (AST fd) = case foldM (const $ hasNoDups fd) () fd 
                        >>| foldM (const isNiceMain) () fd
                        >>| hasMain fd
-                    >>| evalStateT (type fd) (zero, variableStream) of
+                    >>| evalStateT (type fd) (defaultGamma, variableStream) of
        Left e = Left [e]
     Right (_,fds) = Right (AST fds)
 where
@@ -173,6 +178,7 @@ generalize :: Type -> Typing Scheme
 generalize t = gamma >>= \g-> pure $ Forall (difference (ftv t) (ftv g)) t
 
 lookup :: String -> Typing Type 
+lookup "isEmpty" = ListType <$> fresh
 lookup k = gamma >>= \g-> case 'Map'.member k g of
     False = liftT (Left $ UndeclaredVariableError zero k)
     True = instantiate $ 'Map'.find k g
@@ -270,12 +276,15 @@ instance infer Stmt where
         pure (compose s3 $ compose s2 s1, subst s3 wht)
 
     AssStmt (VarDef k fs) e =
-        infer e >>= \(s1, et)->
-        applySubst s1 >>|
-        changeGamma (extend k (Forall [] et)) >>| //todo: fieldselectors
-        pure (s1, VoidType)
+        lookup k >>= \expected ->
+        infer e >>= \(s1, given)->
+        lift (unify expected given) >>= \s2->
+        let s = compose s2 s1 in
+        applySubst s >>|
+        changeGamma (extend k (Forall [] given)) >>| //todo: fieldselectors
+        pure (s, VoidType)
 
-    FunStmt f es = undef //what is this? 
+    FunStmt f es = pure (zero, VoidType) 
 
     ReturnStmt Nothing = pure (zero, VoidType)
     ReturnStmt (Just e) = infer e
@@ -345,25 +354,6 @@ instance type [a] | type a where
         applySubst (compose s2 s1) >>|
         pure (compose s2 s1, [v_:vs_])
 
-//        mapM processGamma dcls//
-
-////add the infered type in Gamma to AST constructs
-//class processGamma a :: a -> Typing a//
-
-//instance processGamma VarDecl where
-//    processGamma v=:(VarDecl p _ k e) = 
-//        gamma >>= \g -> case 'Map'.member k g of
-//            False = undef
-//            True  = instantiate ('Map'.find k g) >>= \t->
-//                    pure (VarDecl p (Just t) k e)//
-
-//instance processGamma FunDecl where
-//    processGamma v=:(FunDecl p k args _ vds stmts) = 
-//        gamma >>= \g -> case 'Map'.member k g of
-//            False = undef
-//            True  = instantiate ('Map'.find k g) >>= \t->
-//                    pure (FunDecl p k args (Just t) vds stmts) 
-
 introduce :: String -> Typing Type
 introduce k = 
     fresh >>= \tv ->