3 ms·
In a similar vein, see[0] which talks about rewrite systems from a more formal viewpoint. On the practice side, GHC performs rewrite rules[1], an example of whi
by siraben 6y ago
In a similar vein, see[0] which talks about rewrite systems from a more formal viewpoint. On the practice side, GHC performs rewrite rules[1], an example of which is
"map/map" forall f g xs. map f (map g xs) = map (f.g) xs
Combining two calls to map into one. While this seems silly, terms like this can arise as a result of other compiler optimizations. GHC can even remove intermediate data structures[2] entirely, so writing factorial as
factorial n = product [1..n]
would result in an efficient first-order program without the list at all. Another example of where rewrite rules come in handy is linting[3]. It does seem that a lot of these rewrites are possible only in a pure language, otherwise the compiler would have to resort to analyzing side-effects.
[0] www.cs.tau.ac.il/~nachum/papers/taste-fixed.pdf
[1] https://downloads.haskell.org/~ghc/7.0.3/docs/html/users_guide/rewrite-rules.html https://downloads.haskell.org/~ghc/7.0.3/docs/html/users_gui...
[2] https://users.cs.northwestern.edu/~robby/courses/395-495-2017-winter/deforestation-short-cut.pdf https://users.cs.northwestern.edu/~robby/courses/395-495-201...
[3] https://github.com/ndmitchell/hlint https://github.com/ndmitchell/hlint
- bjoli 6y agoI played with these kinds of reductions in a subset of scheme, and the flow analysis you need to do to be able to do it safely is nontrivial at best depending on how you limit the semantics of your language. It was a lot easier to do with something like srfi-171 [0] due to their semantics. Fusing those steps is as simple as inlining. [0] transducers, like in clojure. https://srfi.schemers.org/srfi-171/srfi-171.html https://srfi.schemers.org/srfi-171/srfi-171.html . I am the srfi author.
- siraben 6y agoThat interesting. Reminds me of Haskell's Foldable[0] and Functor typeclasses, which abstracts over containers as well. [0] https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Foldable.html https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-...