Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Recursive instance for Mu fmaps more than necessary #37

Closed
treeowl opened this issue May 11, 2017 · 2 comments
Closed

The Recursive instance for Mu fmaps more than necessary #37

treeowl opened this issue May 11, 2017 · 2 comments

Comments

@treeowl
Copy link
Collaborator

treeowl commented May 11, 2017

Manually specializing/inlining a few times reveals the opportunity to fuse three of the fmaps into one. I'm not sure of the cleanest way to do this yet, but one option seems to be

  project (Mu g) = g (fmap (let q x = Mu (\f -> f (fmap (cata f . q . project) x)) in q))

I have the feeling there may be several other similar opportunities lying around.

@gelisam
Copy link
Collaborator

gelisam commented Jul 15, 2018

It is beneficial to merge fmap f . fmap g into fmap (f . g) when each fmap has to traverse a large recursive structure. Since those fmaps are operating on the base functor, which is only a single step of the recursive structure, I don't expect those fmaps to be costly, so I am worried about increasing the complexity of the codebase without a corresponding increase in the performance.

Furthermore, I believe your proposed implementation actually decreases the performance :(

The current implementation is:

project :: Mu f -> f (Mu f)
project = cata (fmap embed)

cata :: (f a -> a) -> Mu f -> a
cata roll (Mu mk) = mk roll

embed :: f (Mu f) -> Mu f
embed fmu = Mu $ \roll -> roll $ fmap (cata roll) fmu

Whereas your version is basically this:

project :: Mu f -> f (Mu f)
project = cata (fmap go) where
  go fmu = Mu $ \roll -> roll $ fmap (cata roll . go . project) fmu

Notice that go looks a lot like embed:

embed fmu = Mu $ \roll -> roll $ fmap (cata roll) fmu
go    fmu = Mu $ \roll -> roll $ fmap (cata roll . go . project) fmu

except go does a lot more work per fmap! It gives the same result because go is embed, and embed is the inverse of project, so the go . project part is an expensive id.

@gelisam gelisam closed this as completed Jul 15, 2018
@treeowl
Copy link
Collaborator Author

treeowl commented Jul 16, 2018

I really don't remember what I did to find maps that I thought needed fusion. Hrmm.... If I figure it out, I'll let you know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants