Elektrine lite

← Feed

@jonocarroll@fosstodon.org

2025-12-11 01:07 UTC

@prophet@functional.cafe ah, that makes sense - I was originally worried that I was shortcutting the result by assigning it to _ but now I see the same is true of an assigned but not-yet-evaluated value. Thank you! I'll update the post. I couldn't get a clear answer elsewhere but it sounds like you may know - are the rewrite rules like rev/rev/id and map/filter fusion generally implemented or are these purely up to the user to write/activate?

Replies (2)

  • @prophet@functional.cafe 2025-12-11 01:56

    @jonocarroll@fosstodon.org it's a hint to the optimizer! this means that it will generally try to apply the rule whenever possible, but it might not have a chance to do so depending on other optimizations it does. e.g. if you have reverse (id reverse list)) (with reverse defined via foldl) and the optimizer inlines the id first, you get reverse (reverse list), which is rewritten to list list but if it inlines the outer reversefirst, you will get foldl (:) [] (id reverse list) and (after inlining id) foldl (:) [] (reverse list), none of which will trigger your rewrite rule this is a contrived example and it's not quite as bad in reality since a lot of list functions have carefully chosen INLINE/INLINABLE pragmas to make sure that rewrite rules have enough opportunities to fire, but you still cannot generally rely on rewrite rules. i actually looked at the core (~optimizer output) of this code (the magical incantation for getting readable output is ghc -O2 -ddump-simpl -ddump-to-file -dsuppress-all -dsuppress-uniques) and it still contained two calls to reverse! (i'm not quite sure why though since it didn't seem to inline anything interesting) if you really want fusion, there are libraries like massiv (https://hackage.haskell.org/package/massiv) that can guarantee fusion by encoding into the types if an array is actually materialized in memory or just an intermediate step of the computation

    Open ##2450821

  • @jonocarroll@fosstodon.org 2025-12-16 03:32

    @prophet@functional.cafe updated - thanks again!

    Open ##2450823