[Haskell-cafe] Typeclass vs. Prolog programming

Martin Sulzmann sulzmann at comp.nus.edu.sg
Mon Oct 2 06:21:34 EDT 2006

oleg at pobox.com writes:
 > There is a great temptation to read the following declarations
 > 	class Pred a b
 > 	instance (Ord b, Eq b) => Pred [Int] b
 > 	instance Pred Bool Bool
 > as a Prolog program:
 > 	pred([int],B) <- ord(B),eq(B).
 > 	pred(bool,bool).
 > (In Prolog, the names of variables are capitalized and the names of
 > constants begin with a lower-case letter. In Haskell type expressions,
 > it's the other way around).

Why not simply say that type classes have a natural interpretation
in terms of Constraint Handling Rules (CHRs). For example,
the above instances translate to

rule  Pred [Int] <==> Ord b, Eq b
rule  Pred Bool Bool <==> True

CHRs specify rewrite rules among constraints (from left to right).
A CHRs fires if we find a constraint which matches the left hand
side (compare this to Prolog where we use unification).

Why CHRs are great for reasoning about type classes is well-documented


More information about the Haskell-Cafe mailing list