Skip to content

Instantly share code, notes, and snippets.

@mpickering
Created March 17, 2017 12:22
Show Gist options
  • Save mpickering/7cc47cd6b96eacb22803b4f4147b3f61 to your computer and use it in GitHub Desktop.
Save mpickering/7cc47cd6b96eacb22803b4f4147b3f61 to your computer and use it in GitHub Desktop.
Static Argument Transformation
map f [] = []
map f (x:xs) = f x : map f xs
{-
written in this style, we can't inline `map` as it is recursive.
-}
map f xs = go xs
where
go [] = []
go (x:xs) = f x : go xs
{-
Now we can inline map which means that we can also inline `f` and sometimes produce better code.
-}
{-
Resulting Core - see m2U1 and m1U to compare
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 70, types: 108, coercions: 0}
Rec {
-- RHS size: {terms: 15, types: 17, coercions: 0}
m1
m1 =
\ @ a_ayf @ b_ayg f_aqr ds_dzw ->
case ds_dzw of _ {
[] -> [];
: x_aqt xs_aqu -> : (f_aqr x_aqt) (m1 f_aqr xs_aqu)
}
end Rec }
-- RHS size: {terms: 3, types: 3, coercions: 0}
m1U1
m1U1 = \ @ b_az2 _ -> True
-- RHS size: {terms: 3, types: 5, coercions: 0}
m1U
m1U = \ @ b_az2 -> m1 m1U1
-- RHS size: {terms: 18, types: 21, coercions: 0}
m2
m2 =
\ @ a_axX @ b_axY f_axC xs_axD ->
letrec {
go_szW
go_szW =
\ ds_dzp ->
case ds_dzp of _ {
[] -> [];
: x_axF xs1_axG -> : (f_axC x_axF) (go_szW xs1_axG)
}; } in
go_szW xs_axD
Rec {
-- RHS size: {terms: 11, types: 12, coercions: 0}
m2U1
m2U1 =
\ @ b_ayK ds_dzp ->
case ds_dzp of _ {
[] -> [];
: x_axF xs_axG -> : True (m2U1 xs_axG)
}
end Rec }
-- RHS size: {terms: 4, types: 5, coercions: 0}
m2U
m2U = \ @ b_ayK xs_axD -> m2U1 xs_axD
-}
module Map where
m1 :: (a -> b) -> [a] -> [b]
m1 f [] = []
m1 f (x:xs) = f x : m1 f xs
m2 :: (a -> b) -> [a] -> [b]
m2 f xs = go xs
where
go [] = []
go (x:xs) = f x : go xs
m1U = m1 (const True)
m2U = m2 (const True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment