
Can someone give an explanation of the use of record syntax in newtype declarations that helps one understand how it's meant to be interpreted? I understand the "facts" according to the language definition, but I just don't see why this notation was used; does it have any relationship with records? The only docs I can find that cover this are in the language report itself:
|A newtype declaration may use field-naming syntax, though of course there may only be one field. Thus: | newtype Age = Age { unAge :: Int } |brings into scope both a constructor and a de-constructor: | Age :: Int -> Age | unAge :: Age -> Int
I understand what this says, I just don't understand why Haskell does it this way - unAge ends up defining a function, using the notation used to declare a field in a record, and the type of the function is inferred from the type of the field and the constructor - it just seems a bit barmy. I'd appreciate anyone who can help, or a reference to some docs that make sense of this. Thanks t

On Wed, Jul 14, 2010 at 10:39:23AM +0100, Tom Doris wrote:
Can someone give an explanation of the use of record syntax in newtype declarations that helps one understand how it's meant to be interpreted? I understand the "facts" according to the language definition, but I just don't see why this notation was used; does it have any relationship with records?
Yes, the reason this notation is used is that it corresponds exactly to defining a record containing one field. Declaring field names in a record really just gives you some projection functions to get the fields out (and also some pattern-matching magic); declaring a field name in a newtype gives you a projection function to unwrap the newtype. Does that help? -Brent

Yes, makes sense, I always thought of the projection functions as being
secondary. Thanks
On 14 July 2010 11:00, Brent Yorgey
On Wed, Jul 14, 2010 at 10:39:23AM +0100, Tom Doris wrote:
Can someone give an explanation of the use of record syntax in newtype declarations that helps one understand how it's meant to be interpreted? I understand the "facts" according to the language definition, but I just don't see why this notation was used; does it have any relationship with records?
Yes, the reason this notation is used is that it corresponds exactly to defining a record containing one field. Declaring field names in a record really just gives you some projection functions to get the fields out (and also some pattern-matching magic); declaring a field name in a newtype gives you a projection function to unwrap the newtype.
Does that help?
-Brent _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 7/14/10 07:41 , Tom Doris wrote:
Yes, makes sense, I always thought of the projection functions as being secondary. Thanks
Sometimes they are; but take a look at the definitions of various state-like monads (State, Reader, Writer; ST also fits but has other things that might well confuse rather than enlighten). State is probably the easiest to understand, since it's just syntactic sugar for a tuple, -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkw+lawACgkQIn7hlCsL25X1dACbBB/E6tanorQ0Vht9pS4k3GiQ SLkAn2gTdkhN55Cl4MSCKHoCVVRcBMWX =wpVd -----END PGP SIGNATURE-----
participants (3)
-
Brandon S Allbery KF8NH
-
Brent Yorgey
-
Tom Doris