From fce54e789996970dfe1bd71914f0c6f6669415a0 Mon Sep 17 00:00:00 2001 From: sebastianselander Date: Mon, 6 Mar 2023 16:41:59 +0100 Subject: [PATCH] documented possible bad functions --- sample-programs/basic-5 | 13 ++----------- src/TypeChecker/TypeChecker.hs | 26 +++++++++++++++----------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/sample-programs/basic-5 b/sample-programs/basic-5 index 7175091..9a9a723 100644 --- a/sample-programs/basic-5 +++ b/sample-programs/basic-5 @@ -1,17 +1,8 @@ -- double : _Int -> _Int ; -- double n = n + n; -apply : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c ; -apply f x y = f x y ; - id : 'a -> 'a ; id x = x ; -add : _Int -> _Int -> _Int ; -add x y = x + y ; - -main : _Int -> _Int -> _Int ; -main = apply (id add) ; - -idadd : _Int -> _Int -> _Int ; -idadd = id add ; +main : ('a -> 'b -> 'c) ; +main = id ; diff --git a/src/TypeChecker/TypeChecker.hs b/src/TypeChecker/TypeChecker.hs index b99313d..af4734d 100644 --- a/src/TypeChecker/TypeChecker.hs +++ b/src/TypeChecker/TypeChecker.hs @@ -45,6 +45,7 @@ typecheck = run . checkPrg {- | Start by freshening the type variable of data types to avoid clash with other user defined polymorphic types +This might be wrong for type constructors that work over several variables -} freshenData :: Data -> Infer Data freshenData (Data (Constr name ts) constrs) = do @@ -59,18 +60,21 @@ freshenData (Data (Constr name ts) constrs) = do let new_ts = map (freshenType fr') ts let new_constrs = map (freshenConstr fr') constrs return $ Data (Constr name new_ts) new_constrs - where - freshenType :: Ident -> Type -> Type - freshenType iden = \case - (TPol _) -> TPol iden - (TArr a b) -> TArr (freshenType iden a) (freshenType iden b) - (TConstr (Constr a ts)) -> - TConstr (Constr a (map (freshenType iden) ts)) - rest -> rest - freshenConstr :: Ident -> Constructor -> Constructor - freshenConstr iden (Constructor name t) = - Constructor name (freshenType iden t) +{- | Freshen all polymorphic variables, regardless of name +| freshenType "d" (a -> b -> c) becomes (d -> d -> d) +-} +freshenType :: Ident -> Type -> Type +freshenType iden = \case + (TPol _) -> TPol iden + (TArr a b) -> TArr (freshenType iden a) (freshenType iden b) + (TConstr (Constr a ts)) -> + TConstr (Constr a (map (freshenType iden) ts)) + rest -> rest + +freshenConstr :: Ident -> Constructor -> Constructor +freshenConstr iden (Constructor name t) = + Constructor name (freshenType iden t) checkData :: Data -> Infer () checkData d = do