[Haskell-cafe] type inference and named fields

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Mon Jun 27 12:07:59 EDT 2005


Christian Maeder <maeder at tzi.de> writes:

> Typing fails in your original example:
>     voidcast v at VariantWithOne{} = v {field1 = Void}
> but not in the "lambda equivalent"
>     voidcast v at VariantWithOne{} =
>        (\ (VariantWithOne a) -> VariantWithOne Void) v

Hmm.  Yes, that was my original point.

> But
>     voidcast v at VariantWithOne{} = ( \ x -> case x of
>        VariantWithTwo a b -> VariantWithTwo Void b
>        VariantWithOne a -> VariantWithOne Void) v
> fails like the field notation.

Yes, this is a more accurate translation of the field notation to a
lambda expression, and therefore suffers the same problem.

My proposal was to allow
    voidcast v = v { field1 = P, field2 = Q }
to be interpreted as the lambda expression
    voidcast v = ( \ x -> case x of
       VariantWithTwo a b -> VariantWithTwo P Q
       VariantWithOne a -> VariantWithOne P) v
where the information about field2=Q is omitted from the clauses where
no field2 exists.  (I'm using P and Q instead of Void, to highlight
the apparent loss of information on the RHS of the pattern-match.)

> I think writing "VariantWithOne {field1 = Void}" instead of
> "v {field1 = Void}" is as clear as writing "Right r" again on the rhs
> instead of "x" from the pattern "x@(Right r)"

I can only repeat myself, that the field being updated (and
type-converted) is only one of many, and all other fields should
carry the same value in the updated structure as in the original.
There is no good way to write this at the moment.  If there were no
type-conversion, a field update would work just great.  But because
of the conversion, one is forced to use explicit construction.

Regards,
    Malcolm


More information about the Haskell-Cafe mailing list