[Haskell] (small) records proposal for Haskell '06

David Roundy droundy at abridgegame.org
Wed Jan 4 10:51:07 EST 2006


On Tue, Jan 03, 2006 at 08:25:01PM -0500, Dylan Thurston wrote:
> On Mon, Jan 02, 2006 at 08:43:56AM -0500, David Roundy wrote:
> > data FooBar = Foo { foo :: Int } | FooBar = { foo :: Int, bar :: Int }
> > 
> > desugars to something like
> > 
> > data FooBar = Foo Int | FooBar Int Int
> > 
> > foo :: FooBar -> Int
> > foo (Foo f) = f
> > foo (FooBar f _) = f
> > bar :: FooBar -> Int
> > bar (Foo _) = error "bad Foo"
> > bar (FooBar _ b) = b
> 
> I'm sure you know this, but I want to be explicit for everyone: If you
> keep the pattern-matching syntax, this could be coded more elegantly
> by hand, without counting positions in a record, by

Indeed... 

> data FooBar = Foo { foo :: Int } | FooBar = { foo :: Int, bar :: Int }
> 
> foo :: FooBar -> Int
> foo (Foo { foo = f }) = f
> foo (FooBar { foo = f}) = f

This makes me wonder (this being a separate idea) whether one could't allow
pattern matching on the constructor

foo (_ { foo = f }) = f

In order for this to work, I think we'd have to declare a class for each
field name, so that we'd have a class so that the type of foo would be
something like

foo :: FieldName_foo a r => r -> a

...but this is on the (controversial) subject of possible record
replacements... which I am trying to avoid for the moment.  :) And this
sort of trick would involve type inference issues that I don't even want to
get close to.
-- 
David Roundy
http://www.darcs.net
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell/attachments/20060104/bd0f62e4/attachment.bin


More information about the Haskell mailing list