[Haskell-cafe] more sharing in generated code

Dennis Felsing dennis at felsin9.de
Sat Nov 3 11:52:15 CET 2012

On 2012-11-03T10:05+0100, Peter Divianszky wrote:
> Suppose we have a record update
>   r { x = f (r x)}

Hi Peter,

I think you mean this:

  r { x = f (x r) }

> 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.)

In GHC: In your example a new record is built, but all its entries (x in
this case) are shared. Here's how I found out:

  λ> let f = id
  λ> data R = R { x :: Int }
  λ> let r = R 0
  λ> let u = r { x = f (x r) }
  λ> :view r
  λ> :view u

:view is from ghc-vis[1], the resulting visualization is attached.

In this simple example, when you compile it with -O1 the compiler
figures out that u will always be the same as r and shares them. This
does not work when it's dynamic whether u will be identical to r.

Hope to help,

[1] http://hackage.haskell.org/package/ghc-vis
-------------- next part --------------
A non-text attachment was scrubbed...
Name: record_update.png
Type: image/png
Size: 3914 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121103/df373fc5/attachment.png>

More information about the Haskell-Cafe mailing list