[Haskell-cafe] more sharing in generated code

Peter Divianszky divipp at gmail.com
Sat Nov 3 10:05:54 CET 2012


I have a question about sharing at the Haskell run-time level.

Suppose we have a record update

   r { x = f (r x)}

and suppose that most of the time f returns it's argument unchanged.

I have the following questions:

1. Does the generated code for the record update build an identical 
record when f returns it's argument unchanged instead of sharing the old 
one? (I guess yes.)

2. Can we prevent building identical records?

    Recently I've heard about Q-combinators.
    Central idea: Change (f :: a -> a) to (f' :: a -> Maybe a) returning
    Nothing when the value didn't change.
    Then we can replace the record update with smarter code which
    preserves more sharing.

    My question is: Can (or could) we enable more sharing without
    changing the source code?
    Ideally the compiler would generate record update code which
    checks poiter-equality of the updated field value to decide
    whether a copy of the original record is needed or it can be shared
    (given an optimisation flag enabled).


Background info:
There was a discussion on the Agda mailing list about decreasing
the Agda compiler memory-usage with more sharing[^1]. (The Agda compiler
is written in Haskell.) I had the above idea and I was advised
to ask about it on some Haskell mailing list.

[^1]: https://lists.chalmers.se/pipermail/agda/2012/004485.html

More information about the Haskell-Cafe mailing list