[Haskell-beginners] Trouble using MultiParamTypeClasses

Amy de Buitléir amy at nualeargais.ie
Tue Nov 22 17:03:32 CET 2011


I tried to make my previous example a bit more flexible, but I guess I was
over-confident. Here's my code:

-----
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies,
UndecidableInstances #-}

class Eq a => Graph g a | g -> a where
  nodes :: g a -> [a]
  neighbours :: g a -> a -> [a]

class (Graph g a, Eq a, Ord w) => WeightedGraph g w a | g -> w, g -> a where
  edges :: g w -> [(a, a, w)]

data MyGraph w a = MyGraph [(a, a, w)]

instance Eq a => Graph (MyGraph w) a where
  nodes _ = [] --stub
  neighbours _ _ = [] --stub

instance (Graph g a, Eq a, Ord w) => WeightedGraph (MyGraph w a) where
  edges = [] -- stub
-----

The error message is

temp2.hs:16:53:
    Kind mis-match
    The first argument of `WeightedGraph' should have kind `* -> *',
    but `MyGraph w a' has kind `*'
    In the instance declaration for `WeightedGraph (MyGraph w a)'
Failed, modules loaded: none.


It seems to me that in the WeightedGraph class, g should have the kind * -> * ->
*, and MyGraph has the kind * -> * -> *, so I'm not sure why I have a kind
mismatch, but I suspect that I've written the instance declaration wrong. Any
ideas how to fix it?




More information about the Beginners mailing list