[Haskell-beginners] Lifting over record syntax

Anthony Clayden anthony_clayden at clear.net.nz
Wed Oct 31 09:52:21 UTC 2018


On Tue, 30 Oct 2018 at 4:20 PM, Anthony Clayden wrote:


> > data Person = Person {name::String, age::Int} deriving Show
>
> >
> > Now, I can create maybe-people like in applicative style:
> >
> > Person <$> Just "John Doe" <*> Nothing
>
> ... field labels for building records only work
> in very restricted syntactic positions, ...
>
>
To tease out that remark a little:

Data constructor `Person` is first-class; we could go

person' = Person

person' <$> Just "John Doe" <*> Nothing

But the following two are nothing like equivalent; so record syntax is not
even referentially transparent:

Person{ name = "Jane Roe", age = 37 }         -- builds a Person record

person'{ name = "Jane Roe", age = 37 }

In the second, the token preceding the `{ ... }` is not a data constructor
(because it starts lower case), so is taken to be a variable/expression
denoting a value of type `Person`; and this is datatype update syntax. Why
of type `Person`? Because field labels `name` and `age` come from there,
and under H98 records, they can be associated only with a single type.

Then `person'` is the wrong type, and you'll get a type error.

Although both look like a name (of function type) adjacent to a term { in
braces}, neither is function application.


AntC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20181031/c96560eb/attachment.html>


More information about the Beginners mailing list