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,
Dennis

[1] http://hackage.haskell.org/package/ghc-vis

<<attachment: record_update.png>>

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to