Deforestation pass
CakeML currently does not have a deforestation pass. We would probably improve on benchmarks once this is implemented.
For implementation I'm considering
- Add primrec combinators as a primitive operation.
- Add a pass which detects primrec functions and rewrites them to use the combinators.
- Implement a simplication pass that merges combinators.
- Implement a pass that removes the combinators. This is essentially shortcut deforestation + warm fusion. https://dl.acm.org/doi/pdf/10.1145/224164.224223 This would remove the difficulty with doing higher order inlining.
The language in the paper is lazy and pure. Will these rewrites hold in a strict language with side effects?
I don't think the algorithm would work out of the box and need to be changed.
I think you should consider targeting purecake with this sort of thing, and not CakeML. The reason is that I suspect that these sort of rewrites enable things like
map f o map g = map (f o g)
To get something like this in CakeML, you first have to establish that f, g, and map are pure and total. It seems like a hassle to implement an analysis that does this well.