[Haskell-cafe] Families of type classes

Udo Stenzel u.stenzel at web.de
Sun Nov 6 13:50:12 EST 2005

Klaus Ostermann wrote:
> I am not a Haskell expert, and I am currently exploring type classes
> and need some advice.

The most important advice is probably to point out that a `class' in
Haskell is roughly comparable to an `interface' in Java, but not to a

> class Node n where
>   isConnectedTo :: Graph g n e => g -> n -> e -> Bool

This is not what you want.  The type says: "Every node can find out
whether it is connected to a given edge _in_any_type_of_graph_", which
is clearly impossible given that your Graph class has no methods.

Is your setting the notion of being a `Node' only makes sense in
connection with a type of `Graph'.  The right thing to so is probably to
drop the classes `Edge' and `Node' and put their methods into the
`Graph' class.

class Graph g n e | g -> n e where
	isConnectedTo :: g -> n -> e -> Bool
	n1 :: g -> e -> n
	n2 :: g -> e -> n

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20051106/3261b751/attachment.bin

More information about the Haskell-Cafe mailing list