[Haskell-cafe] Cannot update a field in a record with a polymorphic type.

Henning Thielemann lemming at henning-thielemann.de
Sun Sep 6 18:19:28 UTC 2020


On Sun, 6 Sep 2020, Ignat Insarov wrote:

> So, polymorphic types are not good enough to update. But see further:
>
>    λ data Z α = Z {z₁, z₂ ∷ α}
>    λ defaultZ = Z {z₁ = mempty, z₂ = mempty}
>    λ :type (defaultZ ∷ ∀ α. Monoid α ⇒ Z α) {z₁ = "c"}
>    (defaultZ ∷ ∀ α. Monoid α ⇒ Z α) {z₁ = "c"} :: Z [Char]
>
> — So when I have two fields it is suddenly fine to update a polymorphic field!

Z has only one type parameter. If you change the type of z1, you also 
change the type of z2. In your example with z1="c" you set the type of z1 
to String, but you also claim that the result is consistently typed, thus 
z2 must also have type String. Since z2 was not touched, it must have the 
same type in defaultZ. Thus GHCi can infer the type parameter of defaultZ.


More information about the Haskell-Cafe mailing list