[Haskell-cafe] Haskell record extension?

Paul Johnson paul at cogito.org.uk
Fri Jun 15 18:51:22 EDT 2007

Andrew Coppin wrote:
> bf3 at telenet.be wrote:
>> I'm learning Haskell.
>> I was surprised that the following example did not compile:
>> data Vector2 = Vector2 { x :: Float, y :: Float }
>> data Vector3 = Vector3 { x :: Float, y :: Float, z :: Float }
>> error: "Multiple declarations of `Main.x'"
> AFAIK, GHC doesn't implement any fix for this. (I've been wrong before 
> tho...)
This is a feature, not a bug.  Haskell in general does not let you give 
two functions the same name (which is what you want to do).  This is 
true of all functions, not just the ones implicitly defined here.  Your 
"Vector2" type is pure syntactic sugar for:

data Vector2 = Vector2 Float Float
x, y :: Vector2 -> Float
x (Vector2 v _) = v
y (Vector2 _ v) = v

So now you also want

x (Vector3 v _ _) = v
   etc etc.

And no, you can't do that because "x" on its own might refer to either 
version, and its not clear which one you want.


More information about the Haskell-Cafe mailing list