[Haskell-cafe] Of types and constructors; a question of programming style

Alastair Reid alastair at reid-consulting-uk.ltd.uk
Thu Jul 8 05:35:22 EDT 2004

It sounds a little as though you want a family of n-ary union type 
constructor.  A crude first attempt would be:

  data Either2 t1 t2 = In1 t1 | In2 t2
  data Either3 t1 t2 t3 = In1 t1 | In2 t2 | In3 t3

but this would be a bit tedious to use because the names are a bit meaningless 
- you'd be relying heavily on type checking to keep you straight.  e.g., it's 
kinds hard to read:

  f :: Either String Bool -> Either String Bool 
  f (In1 "Fred") = In1 "Frederick"
  f (In2 m)      = In2 (not m)

Proposals for records gives you an easy way to define arbitrary product types 
with convenient syntax:

  { name = "Fred", married = True } :: { name :: String, married :: Bool }

Maybe what you really want here is a way to define union types with some 
convenient syntax.  In the following made up syntax, |[ ... |] contains a 
list of types to be unioned together with each type labelled by a constructor 

  f :: |[ Name :: String, Married :: Bool |] 
    -> |[ Name :: String, Married :: Bool |]
  f |[ Name = "Fred" |] = |[ Name = "Frederick |] 
  f [| Married = m |]   = [| Married = not m |]

I wonder if an extension like this would be generally useful?  (Would it be 
useful for defining a compiler/interpreter by first giving a simple language 
and then adding further operations?)

Alastair Reid

More information about the Haskell-Cafe mailing list