From 59da6d8864f94b43c974472909c5777e0c6419e1 Mon Sep 17 00:00:00 2001 From: Rakarake Date: Mon, 1 May 2023 16:14:01 +0200 Subject: [PATCH] Fixed printing bug in MorbIr, fixed Monomorphizer forgetting to output constructors on EInj --- sample-programs/mono-4.chrf | 12 ++++++++++++ src/Monomorphizer/Monomorphizer.hs | 5 ++++- src/Monomorphizer/MorbIr.hs | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 sample-programs/mono-4.chrf diff --git a/sample-programs/mono-4.chrf b/sample-programs/mono-4.chrf new file mode 100644 index 0000000..79d1495 --- /dev/null +++ b/sample-programs/mono-4.chrf @@ -0,0 +1,12 @@ +data Either (a b) where + Left : a -> Either (a b) + Right : b -> Either (a b) + +unwrap : Either (a a) -> a +unwrap x = case x of + Left y => y + Right y => y + +main : Int +main = unwrap (Left 3) + diff --git a/src/Monomorphizer/Monomorphizer.hs b/src/Monomorphizer/Monomorphizer.hs index 5b746e9..62aa601 100644 --- a/src/Monomorphizer/Monomorphizer.hs +++ b/src/Monomorphizer/Monomorphizer.hs @@ -64,7 +64,7 @@ Binds, Polymorphic Data types (monomorphized in a later step) and Marked bind, which means that it is in the process of monomorphization and should not be monomorphized again. -} -data Outputted = Marked | Complete M.Bind | Data M.Type T.Data +data Outputted = Marked | Complete M.Bind | Data M.Type T.Data deriving (Show) -- | Static environment. data Env = Env @@ -214,6 +214,7 @@ morphExp expectedType exp = case exp of T.ELit lit -> return $ M.ELit (convertLit lit) -- Constructor T.EInj ident -> do + morphCons expectedType ident return $ M.EVar ident T.EApp (e1, _t1) (e2, t2) -> do t2' <- getMonoFromPoly t2 @@ -234,6 +235,8 @@ morphExp expectedType exp = case exp of bs' <- mapM morphBranch bs exp' <- morphExp t' exp return $ M.ECase (exp', t') (catMaybes bs') + -- Ideally constructors should be EInj, though this code handles them + -- as well. T.EVar ident -> do isLocal <- localExists ident if isLocal diff --git a/src/Monomorphizer/MorbIr.hs b/src/Monomorphizer/MorbIr.hs index 20f9496..3e5db6b 100644 --- a/src/Monomorphizer/MorbIr.hs +++ b/src/Monomorphizer/MorbIr.hs @@ -175,6 +175,7 @@ instance Print Type where prt i = \case TLit uident -> prPrec i 1 (concatD [prt 0 uident]) TFun type_1 type_2 -> prPrec i 0 (concatD [prt 1 type_1, doc (showString "->"), prt 0 type_2]) + TData uident types -> prPrec i 1 (concatD [prt 0 uident, doc (showString "("), prt 0 types, doc (showString ")")]) instance Print Lit where prt i = \case