The Future of Haskell discussion at the Haskell Workshop

Koen Claessen koen@cs.chalmers.se
Thu, 11 Sep 2003 12:08:50 +0200 (MET DST)


Karl-Filip Faxen wrote:

 | Yes, things are clearer and I rather like the idea.
 | The only thorny issue is that the update function for
 | field 'wibble' is formed from but not equal to the
 | field name itself.

This could be solved by having an abstract type Field
thusly (*):

  type Field r a

  set :: r -> Field r a -> a -> r
  get :: r -> Field r a -> a

The example would then look like:

  class Wibble r where
    wibble :: Field r Int
    wobble :: Field r String

  data Foo =
    MkFoo{ wibble :: Int
         , wobble :: String
         }
    deriving Wibble

What do you think of this?

The type Field can be implemented as:

  data Field r a = MkField (r -> a -> r) (r -> a)

  set rec (MkField f _) x = f rec x
  get rec (MkField _ g)   = g rec

Regards,
/Koen

----

(*) I prefer the following operators but I realize that
there are other people who are less fond of binary operator
symbols :-)

  type Field r a
  type Setting r

  (=:) :: Field r a -> a -> Setting r
  (!)  :: r -> Setting r -> r
  (?)  :: r -> Field r a -> a

Such that selecting the field wibble from a record rec would
look like:

  rec ? wibble

And setting the field wibble from the record rec to the
value val would look like:

  rec ! wibble =: val

The last should parse as:

  rec ! (wibble =: val)

/K