Skip to content

Commit

Permalink
Refactor {,un}zipApp -> {,un}foldApp
Browse files Browse the repository at this point in the history
  • Loading branch information
j-hui committed Oct 10, 2022
1 parent eb24193 commit 33f173a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Codegen/Codegen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ genExpr (I.Let [(Nothing, d)] b _) = do
return (bodyVal, defStms ++ bodyStms)
genExpr I.Let{} = fail "Cannot handle mutually recursive bindings"
genExpr e@(I.App _ _ ty) = do
let (fn, args) = second (map fst) $ I.unzipApp e
let (fn, args) = second (map fst) $ I.unfoldApp e
-- args must be non-empty because a is an App
case fn of
-- I.Var _ _ -> do
Expand Down
2 changes: 1 addition & 1 deletion src/IR/DConToFunc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ createFunc tcon (dconid, I.VariantNamed params) = Just (func_name, lambda)
where
func_name = nameFunc dconid -- distinguish func name from fully applied dcon in IR
lambda = I.foldLambda (first Just <$> params) body
body = I.zipApp dcon args
body = I.foldApp dcon args
dcon = I.Data (fromId dconid) t
args = reverse $ zip (uncurry I.Var <$> params) ts
tconTyp = I.TCon tcon []
Expand Down
35 changes: 19 additions & 16 deletions src/IR/IR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ module IR.IR
, unfoldLambda
, extract
, inject
, zipApp
, unzipApp
, foldApp
, unfoldApp
, isValue
) where
import Common.Identifiers ( Binder
Expand All @@ -51,7 +51,7 @@ import Data.Maybe ( catMaybes
import qualified Data.Set as S
import Data.Set ( (\\) )
import IR.Types.Type ( Annotation
, Annotations
, Annotations
, pattern Arrow
, Type
, unfoldArrow
Expand Down Expand Up @@ -274,19 +274,19 @@ which, when unzipped, gives:
(Var f (A -> B -> C)) [(Var a A, B -> C), (Var b B, C)]
@@
'unzipApp' is the inverse of 'zipApp'.
'unfoldApp' is the inverse of 'foldApp'.
-}
unzipApp :: Expr t -> (Expr t, [(Expr t, t)])
unzipApp (App lhs rhs t) =
let (fn, args) = unzipApp lhs in (fn, args ++ [(rhs, t)])
unzipApp e = (e, [])
unfoldApp :: Expr t -> (Expr t, [(Expr t, t)])
unfoldApp (App lhs rhs t) =
let (fn, args) = unfoldApp lhs in (fn, args ++ [(rhs, t)])
unfoldApp e = (e, [])

{- | Apply a function to zero or more arguments.
'zipApp' is the inverse of 'unzipApp'.
'foldApp' is the inverse of 'unfoldApp'.
-}
zipApp :: Expr t -> [(Expr t, t)] -> Expr t
zipApp = foldr $ \(a, t) f -> App f a t
foldApp :: Expr t -> [(Expr t, t)] -> Expr t
foldApp = foldr $ \(a, t) f -> App f a t

-- | Collect a curried list of function arguments from a nesting of lambdas.
unfoldLambda :: Expr t -> ([Binder], Expr t)
Expand Down Expand Up @@ -429,7 +429,7 @@ instance Pretty (Program Type) where
instance Pretty (Expr ()) where
pretty a@App{} = pretty nm <+> hsep (parenz . fst <$> args)
where
(nm, args) = unzipApp a
(nm, args) = unfoldApp a
-- insert (usually) necessary parens
parenz :: Expr () -> Doc ann
parenz v@(Var _ _) = pretty v -- variables
Expand Down Expand Up @@ -463,7 +463,9 @@ instance Pretty (Expr ()) where
pretty (Prim New [r] _ ) = pretty "new" <+> pretty r
pretty (Prim Dup [r] _ ) = pretty "__dup" <+> parens (pretty r)
pretty (Prim Drop [e, r] _) =
pretty "__drop" <+> parens (line <> indent 2 (pretty e) <> line) <+> pretty r
pretty "__drop"
<+> parens (line <> indent 2 (pretty e) <> line)
<+> pretty r
pretty (Prim Deref [r] _) = pretty "deref" <+> parens (pretty r)
pretty (Prim Par es _) = pretty "par" <+> block dbar (map pretty es)
pretty (Prim Break [] _) = pretty "break"
Expand Down Expand Up @@ -561,9 +563,10 @@ instance Dumpy (Expr Type) where
-- Where to add binder?
arms = block bar (map arm as)
arm (a, e) = dumpy a <+> pretty "=" <+> braces (dumpy e)
dumpy (Prim New [r] t) = typeAnn t $ pretty "new" <+> dumpy r
dumpy (Prim Dup [r] t) = typeAnn t $ pretty "__dup" <+> dumpy r
dumpy (Prim Drop [e, r] t) = typeAnn t $ pretty "__drop" <+> parens (dumpy e) <+> parens (dumpy r)
dumpy (Prim New [r] t) = typeAnn t $ pretty "new" <+> dumpy r
dumpy (Prim Dup [r] t) = typeAnn t $ pretty "__dup" <+> dumpy r
dumpy (Prim Drop [e, r] t) =
typeAnn t $ pretty "__drop" <+> parens (dumpy e) <+> parens (dumpy r)
dumpy (Prim Deref [r] t) = typeAnn t $ pretty "deref" <+> dumpy r
dumpy (Prim Assign [l, r] t) =
typeAnn t $ parens $ dumpy l <+> larrow <+> braces (dumpy r)
Expand Down
8 changes: 4 additions & 4 deletions src/IR/SubstMagic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ substMagic p = everywhere $ mkT $ substMagicExpr p

-- | Replace applications to built-in names with corresponding primitives.
substMagicExpr :: Proxy t -> I.Expr t -> I.Expr t
substMagicExpr _ e = case I.unzipApp e of
(I.Var "new" _, (a, t) : ats) -> I.zipApp (I.Prim I.New [a] t) ats
(I.Var "dup" _, (a, t) : ats) -> I.zipApp (I.Prim I.Dup [a] t) ats
substMagicExpr _ e = case I.unfoldApp e of
(I.Var "new" _, (a, t) : ats) -> I.foldApp (I.Prim I.New [a] t) ats
(I.Var "dup" _, (a, t) : ats) -> I.foldApp (I.Prim I.Dup [a] t) ats
(I.Var "drop" _, (a, t) : (b, _) : ats) ->
I.zipApp (I.Prim I.Drop [a, b] t) ats
I.foldApp (I.Prim I.Drop [a, b] t) ats
_ -> e

0 comments on commit 33f173a

Please sign in to comment.