add last exception for functype
[cc1516.git] / sem.icl
diff --git a/sem.icl b/sem.icl
index 833d543..e714dbf 100644 (file)
--- a/sem.icl
+++ b/sem.icl
@@ -50,8 +50,8 @@ 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)))
+                $ extend "isEmpty" (Forall ["a"] ((ListType (IdType "a")) ->> BoolType))
+                $ extend "read" (Forall [] (FuncType CharType))
                 zero
 
 sem :: AST -> Either [SemError] AST
@@ -102,10 +102,12 @@ instance Typeable Type where
     ftv (TupleType (t1, t2))    = ftv t1 ++ ftv t2
     ftv (ListType t)            = ftv t
     ftv (IdType tvar)           = [tvar]
+    ftv (FuncType t)            = ftv t
     ftv (t1 ->> t2)             = ftv t1 ++ ftv t2
     ftv _                       = []
     subst s (TupleType (t1, t2))= TupleType (subst s t1, subst s t2)
     subst s (ListType t1)       = ListType (subst s t1)
+    subst s (FuncType t)         = FuncType (subst s t)
     subst s (t1 ->> t2)         = (subst s t1) ->> (subst s t2)
     subst s t1=:(IdType tvar)   = 'Map'.findWithDefault t1 tvar s
     subst s t                   = t
@@ -141,6 +143,7 @@ unify (TupleType (ta1,ta2)) (TupleType (tb1,tb2)) = unify ta1 tb1 >>= \s1->
                                                     unify ta2 tb2 >>= \s2->
                                                     Right $ compose s1 s2
 unify (ListType t1) (ListType t2) = unify t1 t2
+unify (FuncType t1) (FuncType t2) = unify t1 t2
 unify t1 t2 | t1 == t2  = Right zero
             | otherwise = Left $ UnifyError zero t1 t2
 
@@ -178,7 +181,6 @@ 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
@@ -224,12 +226,13 @@ instance infer Expr where
         lookup f >>= \expected ->
         let accST = (\(s,ts) e->infer e >>= \(s_,et)->pure (compose s_ s,ts++[et])) in
         foldM accST (zero,[]) args >>= \(s1, argTs)->
-        fresh >>= \tv->
-        let given = foldr (->>) tv argTs in
-        lift (unify expected given) >>= \s2->
-        let fReturnType = subst s2 tv in
-        foldM foldFieldSelectors fReturnType fs >>= \returnType ->
-        pure (compose s2 s1, returnType)
+        fresh >>= \tv->case expected of
+                       FuncType t = pure (s1, t)
+                       _ = (let given = foldr (->>) tv argTs in
+                       lift (unify expected given) >>= \s2->
+                       let fReturnType = subst s2 tv in
+                       foldM foldFieldSelectors fReturnType fs >>= \returnType ->
+                       pure (compose s2 s1, returnType))
 
     IntExpr _ _ = pure $ (zero, IntType)
     BoolExpr _ _ = pure $ (zero, BoolType)