0
votes

The following error is triggered:

ERROR - Cannot find "show" function for:
*** Expression : debugFunction getDebugTree
*** Of type    : Tree (Tree Int) -> Bool

when running debugFunction getDebugTree

data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Eq, Show)

getDebugTree :: Tree Int
getDebugTree = (Node 1 (Empty) (Node 2 Empty Empty))

debugFunction :: Show a => a -> Tree a -> Bool
debugFunction _ _ = True

I've read that

reverse []

ERROR: Cannot find "show" function for: * expression : reverse [] * of type : [a]

The top-level system for Hugs can only print values which belong to a type which can be shown, that is a type which belongs to the Show class. Now, the list type is an instance of Show, so what is wrong with reverse [] (which evaluates to [])? The problem is that the type of [] is polymorphic: [] :: [a] for all a. Not knowing a Hugs refuses to print a value of type [a]. Note that this behaviour applies to all polymorphic values. Given the definition

data Tree a = Empty | Node (Tree a) a (Tree a)

we have, on evaluating

Empty

the error message

ERROR: Cannot find "show" function for: * expression : Empty * of type : Tree a

Functions can be shown, but not very helpfully; printing any function results in

<< function>>

As far as I understand Hugs doesn't know a so it refuses to print it (even though I'm printing a Bool?) so I've tried to make a be an instance of Show, but it still doesn't work, what's the problem here?

1

1 Answers

3
votes

Look at the type of debugFunction.

debugFunction :: Show a => a -> Tree a -> Bool

debugFunction expects two arguments, the second of which is a Tree a. You're only passing it one. The type checker sees that you're passing a Tree Int as the first argument and infers a ~ Tree Int. So debugFunction getDebugTree is a function waiting for a second argument of type Tree (Tree Int).

debugFunction getDebugTree :: Tree (Tree Int) -> Bool

I suspect you intended to use getDebugTree as the second argument to debugFunction, which means you need to come up with an Int to use as the first one.

debugFunction 0 getDebugTree :: Bool

The error message about the missing Show instance comes from the REPL itself. It's attempting to print out the result of debugFunction getDebugTree, but it can't because functions can't be shown.