[Haskell-cafe] Trouble understanding records and existential typesy

Stefan O'Rear stefanor at cox.net
Wed Jan 24 20:03:18 EST 2007


On Thu, Jan 25, 2007 at 11:34:55AM +1100, John Ky wrote:
> A while back I asked about OO programming in Haskell and discovered
> existential types.  I understood that existential types allowed me to write
> heterogeneous lists which seemed sufficient at the time.
> 
> Now trying to combine those ideas with records:
> 
> data AnyNode = forall a. Node a => AnyNode a
> 
> class Node -- yadda yadda
> 
> data Branch = Branch { name :: String, description :: String, children ::
> [AnyNode] }
> data Leaf = Leaf { name :: String, value :: String }
> 
> The problem here is I can't use the same 'name' field for both Branch and
> Leaf.  Ideally I'd like the name field in the Node class, but it doesn't
> seem that Haskell classes are for that sort of thing.

They are - it's the record system that's biting you.

Haskell-98 style records are widely acknowledged as sucking, and there are
something like half a dozen proposals all of which are widely acknowledged
as vastly superior.  Expect to be stuck with H98 records for the remainder
of time; see "bikeshed".

(Plea to SPJ: stop deliberating and flip a coin!)

Anyway, in the one of the proposals whose syntax I can mostly remember:

data AnyNode = forall a. a \ name => AnyNode { a | name :: String }

data Branch = Branch { name :: String, description :: String, children ::
  [AnyNode] }
data Leaf = Leaf { name :: String, value :: String }


More information about the Haskell-Cafe mailing list