[Haskell-cafe] Re: Type classes

Max Vasin max-appolo at mail.ru
Mon Mar 20 10:26:34 EST 2006

>>>>> "Stefan" == Stefan Holdermans <stefan at cs.uu.nl> writes:

Stefan> Max,
>>> class DatabaseEntry e where entryLabel :: e -> String formatEntry
>>> :: e -> String compareEntries :: e -> e -> Ordering
>>  Then I define
>>> data Entry = forall a. (DatabaseEntry a) => Entry a
>>> instance DatabaseEntry Entry where entryLabel (Entry e) =
>>> entryLabel e formatEntry (Entry e) = formatEntry e
>>  How can I define compareEntries for this instance?

Stefan> In general: you can't. The field of the Entry constructor has
Stefan> a existentially quantified typed. Given two arbitrary values
Stefan> of type Entry, this type may be instantiated with a different
Stefan> type for each value, so you cannot easily compare the fields.

Yes, this require something like multimethods in CLOS.

Stefan> If you extend the DatabaseEntry class such that it supplies a
Stefan> method that allows to produce some canonical representation
Stefan> for database entries suited for comparison, then you could
Stefan> take that road.

Stefan> Are you sure that your Entry type needs to be existentially
Stefan> quantified?

I want a map of entries (mapping lables to entries). Generally each style
can define its own data types for database objects. Converting entries from

data BibEntry = { beLabel :: String
                , beKind :: String 
                , beProperties :: Map.Map String String

is some sort of "static type checking" of the database.

Max Vasin.

More information about the Haskell-Cafe mailing list