[Haskell] Extensible records: Static duck typing

John Meacham john at repetae.net
Tue Feb 5 07:24:05 EST 2008


On Tue, Feb 05, 2008 at 05:57:24AM -0500, Cale Gibbard wrote:
> On 05/02/2008, Bulat Ziganshin <bulat.ziganshin at gmail.com> wrote:
> > saveToFile x = writeToFile "data" (show x.getValue)
> 
> Heh, I had to read this a couple times to figure out that it wasn't
> just a blatant type error, and that (.) there doesn't mean function
> composition. :)
> 
> On the matter of extensible records, I really like the semantics of
> Daan Leijen's proposal here:
> http://research.microsoft.com/users/daan/download/papers/scopedlabels.pdf
> 
> However, the syntax could use some work. Using (.) as a record
> selector is out of the question. Personally, I think pt{x} for
> extracting the x field of pt seems not-so-unreasonable, and meshes
> well with the existing syntax for record updates.

The backwards compatable (and more clean conceptually IMHO) syntax I
came up with for implementing the scoped labels proposal for jhc (sadly,
not complete) was something like:

new record (x = 3,y = 4)
subtraction \r -> ( x = 3 | r - x)
replacement \r -> (x := 3 | r) (equivalent to the above) 
type (x::Int,y::Char)

degenerate cases:
empty record (|)
subtracting a label (| r - x)

a record can always be determined by the presence of a '|' within
parenthesis.

note that these are unambigious because '=' and '|' are both reserved
characters and cannot appear in parenthesis is this position otherwise.

now when it came to record selection I was deciding between a couple.

choice 1: use '.' as the current proposal suggests, but only when there
is no space around it.

choice 2: use ', declare that any identifier that _begins_ with ' always
refers to a label selection function

'x point

choice 3: use '#'.

none are fully backwards compatable. I am still not sure which I like
the best, ' has a lot of appeal to me as it is very simple to type and
lightweight visually.


note that instead of {} we use parens, the reason is that scoped labels
have much more in common with tuples than the current labeld field
mechanism so parens are a much more natural choice. you can think of
tuples as 'anonymous positional data types' and records as 'anonymous
labeled data types'. when thought about that way, parenthesis make a lot
more sense.

        John


-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell mailing list