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

David Roundy droundy at abridgegame.org
Mon Jan 2 09:58:19 EST 2006

On Mon, Jan 02, 2006 at 01:53:51PM +0000, Joel Reymont wrote:
> Say you turned automatic generation of accessor functions off. You would
> still need accessor functions, right? What would they look like if you
> were to write them by hand?

They could look identical if you wrote them by hand, but you could also
choose to put them in a type class.  So you might write

data Foo = Foo { foo :: Int }
data FooBar = FooBar { foo :: Int, bar :: String }

class Foo f where
  foo :: f -> Int
instance Foo Foo where
  foo (Foo { foo=f }) = f
instance Foo FooBar where
  foo (FooBar { foo=f }) = f

bar :: FooBar -> String
bar (FooBar { bar=b }) = b

I believe it wouldn't be hard to write TH (or SYB, or DrIFT or something
else?)  code to do this sort of thing automatically.

Technically, you don't need accessor functions, as you can get by with
pattern matching, which is what this proposal is about.  If we remove the
accessor functions, they can always be written by hand, and we separate the
record-field namespace from the function namespace.  One might argue that
updater functions are as important as accessor functions, but they aren't
defined automatically in Haskell 98.  One might prefer to define

data Foo = Foo { foo :: Int }
set_foo :: Foo -> Int -> Foo
get_foo :: Foo -> Int

and perhaps even

modify_foo :: Foo -> (Int -> Int) -> Foo

which would eliminate the need for the (clumsy, in my opinion) { foo=f }
David Roundy http://www.darcs.net

More information about the Haskell mailing list