[Haskell-cafe] Question about instance

John Velman velman at cox.net
Thu Jan 13 19:29:37 EST 2005

Instance with a context doesn't seem to work as I expect.

Here's the story:

I define an data type, Relation, and I want to make it an instance of
"Show", partly so I can debug and tinker with things interactively and have
ghci print something.

Here is my first try:
>import Data.Set
>type EN = String  -- element name
>type RN = String  -- relation name
>instance Show a => Show (Set a) where
>   show s = "mkSet " ++ show (setToList s) 
>data Relation  = Relation {name::RN, arity::Int, members::(Set [EN])}
>instance (Show a, Show i, Show b) => Show (Relation a i b)
>   where
>     show (Relation a i b) =
>                    a ++ "/" ++ (show i) ++ " "
>                    ++ (show b)

When I try to load this into ghci, I get:


*Main> :l test.hs
Compiling Main             ( test.hs, interpreted )

    Kind error: `Relation' is applied to too many type arguments
    When checking kinds in `Relation a i b'
    When checking kinds in `Show (Relation a i b)'
    In the instance declaration for `Show (Relation a i b)'
Failed, modules loaded: none.
But, when I define showRelation separately, then leave the context out of
the instance declaration with show = showRelation it works:

-----------(Everything down to the instance declaration is the same)
>instance  Show Relation 
>   where
>     show = showRelation
>showRelation:: Relation -> String
>showRelation (Relation a i b) =
>                    a ++ "/" ++ (show i) ++ " "
>                    ++ (show b)

Now I get:
----------- GHCI output:
Prelude> :l test.hs
Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> mkRelation1 "test" 2 [["one","two"], ["three","four"]]
test/2 mkSet [["one","two"],["three","four"]]
---------- End GHCI

Why does the original instance declaration result in failure and the
message "Kind error: `Relation' is applied to too many type arguments" (I
confess to not understanding 'kinds' too well.)

I've done a bit of tinkering with the original version, and have tried the
second version with a context in the instance declaration, but none of my
attmepts work.  The only one that worked was the one shown, with no context
in the instance declaration.  Needless to say (?), I've tried to
understand this from reading in the Haskell 98 report, `Haskell school of
Expression', and any place else I can think of, but I'm missing the point


John Velman

More information about the Haskell-Cafe mailing list