X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=sem.icl;h=9eafed1db34b390b6da14949a536e86b10b7dfe5;hb=e33962666f8af6a34432d57180eb322f4543eb78;hp=75b55851e5f4bd1e6e04165ed7c45913676fd916;hpb=a5c4e896f3f80da310ebd2a562672a47b62d323a;p=cc1516.git diff --git a/sem.icl b/sem.icl index 75b5585..9eafed1 100644 --- a/sem.icl +++ b/sem.icl @@ -51,7 +51,10 @@ 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 [] CharType) + $ extend "read" (Forall [] (FuncType CharType)) + $ extend "1printchar" (Forall [] (CharType ->> VoidType)) + $ extend "1printint" (Forall [] (IntType ->> VoidType)) + $ extend "1printbool" (Forall [] (BoolType ->> VoidType)) zero sem :: AST -> Either [SemError] AST @@ -102,10 +105,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 +146,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 @@ -223,20 +229,21 @@ instance infer Expr where lookup f >>= \expected -> let accST = (\(s,ts,es) e->infer e >>= \(s_,et,e_)-> pure (compose s_ s,ts++[et],es++[e_])) in foldM accST (zero,[],[]) args >>= \(s1, argTs, args_)-> - fresh >>= \tv-> - let given = foldr (->>) tv argTs in - lift (unify expected given) >>= \s2-> - let fReturnType = subst s2 tv in - foldM foldFieldSelectors fReturnType fs >>= \returnType -> - (case f of - "print" = case head argTs of - IntType = pure "1printint" - CharType = pure "1printchar" - BoolType = pure "1printbool" - ListType (CharType) = pure "1printstr" - t = liftT $ Left $ SanityError p ("can not print " +++ toString t) - _ = pure f) >>= \newF-> - pure (compose s2 s1, returnType, FunExpr p newF args_ fs) + fresh >>= \tv->case expected of + FuncType t = pure (s1, t, e) + _ = (let given = foldr (->>) tv argTs in + lift (unify expected given) >>= \s2-> + let fReturnType = subst s2 tv in + foldM foldFieldSelectors fReturnType fs >>= \returnType -> + (case f of + "print" = case head argTs of + IntType = pure "1printint" + CharType = pure "1printchar" + BoolType = pure "1printbool" + ListType (CharType) = pure "1printstr" + t = liftT $ Left $ SanityError p ("can not print " +++ toString t) + _ = pure f) >>= \newF-> + pure (compose s2 s1, returnType, FunExpr p newF args_ fs)) IntExpr _ _ = pure $ (zero, IntType, e) BoolExpr _ _ = pure $ (zero, BoolType, e)