Monomorphizer, fixed problem with type of bind

This commit is contained in:
Rakarake 2023-03-31 17:53:56 +02:00
parent d097cd28e8
commit 15c18271ba
2 changed files with 14 additions and 10 deletions

View file

@ -1,5 +1,7 @@
const x y = x; const x y = x;
f x = (const x 'c'); id x = x;
f x = (id 5);
main = f 5; main = f 5;

View file

@ -119,7 +119,7 @@ getMonoFromPoly t = do env <- ask
-- Returns the annotated bind name. -- Returns the annotated bind name.
-- TODO: Redundancy? btype and t should always be the same. -- TODO: Redundancy? btype and t should always be the same.
morphBind :: M.Type -> T.Bind -> EnvM Ident morphBind :: M.Type -> T.Bind -> EnvM Ident
morphBind expectedType b@(T.Bind (Ident _, btype) args (exp, t)) = morphBind expectedType b@(T.Bind (Ident _, btype) args (exp, expt)) =
local (\env -> env { locals = Set.fromList (map fst args), local (\env -> env { locals = Set.fromList (map fst args),
polys = Map.fromList (mapTypes btype expectedType) polys = Map.fromList (mapTypes btype expectedType)
}) $ do }) $ do
@ -131,7 +131,8 @@ morphBind expectedType b@(T.Bind (Ident _, btype) args (exp, t)) =
-- Mark so that this bind will not be processed in recursive or cyclic -- Mark so that this bind will not be processed in recursive or cyclic
-- function calls -- function calls
markBind (coerce name') markBind (coerce name')
exp' <- morphExp expectedType exp expt' <- getMonoFromPoly expt
exp' <- morphExp expt' exp
-- Get monomorphic type sof args -- Get monomorphic type sof args
args' <- mapM convertArg args args' <- mapM convertArg args
addOutputBind $ M.Bind (coerce name', expectedType) addOutputBind $ M.Bind (coerce name', expectedType)
@ -145,11 +146,10 @@ convertArg (ident, t) = do t' <- getMonoFromPoly t
-- Morphs function applications, such as EApp and EAdd -- Morphs function applications, such as EApp and EAdd
morphApp :: M.Type -> T.ExpT -> T.ExpT -> EnvM M.Exp morphApp :: M.Type -> T.ExpT -> T.ExpT -> EnvM M.Exp
morphApp expectedType (e1, t1) (e2, t2)= do morphApp expectedType (e1, t1) (e2, t2)= do
t1' <- getMonoFromPoly t1
t2' <- getMonoFromPoly t2 t2' <- getMonoFromPoly t2
e2' <- morphExp t2' e2 e2' <- morphExp t2' e2
e1' <- morphExp (M.TFun t2' expectedType) e1 e1' <- morphExp (M.TFun t2' expectedType) e1
return $ M.EApp (e1', t1') (e2', t2') return $ M.EApp (e1', M.TFun t2' expectedType) (e2', t2')
addOutputData :: M.Data -> EnvM () addOutputData :: M.Data -> EnvM ()
addOutputData d@(M.Data (M.TData ident _) _) = modify $ Map.insert ident (Data d) addOutputData d@(M.Data (M.TData ident _) _) = modify $ Map.insert ident (Data d)
@ -209,10 +209,10 @@ morphExp expectedType exp = case exp of
morphExp t' exp morphExp t' exp
T.ECase (exp, t) bs -> do T.ECase (exp, t) bs -> do
t' <- getMonoFromPoly t t' <- getMonoFromPoly t
exp' <- morphExp t' exp
bs' <- mapM morphBranch bs bs' <- mapM morphBranch bs
exp' <- morphExp t' exp
return $ M.ECase (exp', t') bs' return $ M.ECase (exp', t') bs'
T.EVar ident@(Ident str) -> do T.EVar ident -> do
isLocal <- localExists ident isLocal <- localExists ident
if isLocal then do if isLocal then do
return $ M.EVar (coerce ident) return $ M.EVar (coerce ident)
@ -246,7 +246,8 @@ morphPattern = \case
T.PLit (lit, t) -> do t' <- getMonoFromPoly t T.PLit (lit, t) -> do t' <- getMonoFromPoly t
return $ M.PLit (convertLit lit, t') return $ M.PLit (convertLit lit, t')
T.PCatch -> return M.PCatch T.PCatch -> return M.PCatch
T.PEnum v -> return $ M.PEnum v -- Constructor ident
T.PEnum ident -> return $ M.PEnum ident
T.PInj ident ps -> do ps' <- mapM morphPattern ps T.PInj ident ps -> do ps' <- mapM morphPattern ps
return $ M.PInj ident ps' return $ M.PInj ident ps'
@ -258,8 +259,9 @@ newName t (T.Bind (Ident bindName, _) _ _) =
else Ident (bindName ++ "$" ++ newName' t) else Ident (bindName ++ "$" ++ newName' t)
where where
newName' :: M.Type -> String newName' :: M.Type -> String
newName' (M.TLit (Ident str)) = str newName' (M.TLit (Ident str)) = str
newName' (M.TFun t1 t2) = newName' t1 ++ "_" ++ newName' t2 newName' (M.TFun t1 t2) = newName' t1 ++ "_" ++ newName' t2
newName' (M.TData (Ident str) ts) = str ++ "." ++ foldl (\s t -> s ++ "," ++ newName' t) "" ts
-- Monomorphization step -- Monomorphization step
monomorphize :: T.Program -> O.Program monomorphize :: T.Program -> O.Program