
Hi! I'm currently experimenting with a bibliography generation tool for LaTeX. It will (if it will be finished) use BibTeX databases but bibliography styles will be written in Haskell. I want styles to be able to transform database entries into some style specific data type, so I define
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? -- WBR, Max Vasin.

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?
In general: you can't. The field of the Entry constructor has a existentially quantified typed. Given two arbitrary values of type Entry, this type may be instantiated with a different type for each value, so you cannot easily compare the fields. If you extend the DatabaseEntry class such that it supplies a method that allows to produce some canonical representation for database entries suited for comparison, then you could take that road. Are you sure that your Entry type needs to be existentially quantified? HTH, Stefan

"Stefan" == Stefan Holdermans
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. -- WBR, Max Vasin.

see my discussion a few moments ago, in particular my posting http://www.haskell.org/pipermail/haskell-cafe/2006-March/014981.html as you by now already know from this thread, the link tells you that the only possible solution is to turn the two entries to be compared into something of the same type, which can only be done with another type class. i am using 'Show' for now and compare the strings, because it's really simple and i don't care about performance at this stage of the project. might bite me later, though. cheers, matthias On Mon, Mar 20, 2006 at 05:46:43PM +0300, Max Vasin wrote:
To: haskell-cafe@haskell.org From: Max Vasin
Date: Mon, 20 Mar 2006 17:46:43 +0300 Subject: [Haskell-cafe] Type classes Hi!
I'm currently experimenting with a bibliography generation tool for LaTeX. It will (if it will be finished) use BibTeX databases but bibliography styles will be written in Haskell. I want styles to be able to transform database entries into some style specific data type, so I define
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?
-- WBR, Max Vasin.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Institute of Information Systems, Humboldt-Universitaet zu Berlin web: http://www.wiwi.hu-berlin.de/~fis/ e-mail: fis@wiwi.hu-berlin.de tel: +49 30 2093-5742 fax: +49 30 2093-5741 office: Spandauer Strasse 1, R.324, 10178 Berlin, Germany pgp: AD67 CF64 7BB4 3B9A 6F25 0996 4D73 F1FD 8D32 9BAA
participants (3)
-
Matthias Fischmann
-
Max Vasin
-
Stefan Holdermans