[Haskell-beginners] Multi-parameter type classes
David Beacham
dbeacham at dbeacham.co.uk
Mon Nov 28 20:35:11 CET 2011
Hi all,
I've been reading through Okasaki's purely functional data structures
and noticed that the source he provides for the Set implementations uses
multi-parameter type classes, whereas those for the Heap structures does
not.
class Set s a where
empty :: s a
insert :: a -> s a -> s a
...
class Heap h where
empty :: Ord a => h a
insert :: Ord a => a -> h a -> h a
...
The only difference I can see is that the instance declarations for Set
contain the Ord constraint, whereas they are defined on the class
functions for Heap.
instance (Ord a) => Set RedBlackSet a where
...
I realise that the type of `a` needs to be constrained to Ord instances
at some point in the code, but is there any particular reason for doing
it differently in each case? If not, should one method be preferred over
the other?
Thanks for the help, David.
More information about the Beginners
mailing list