Dear all,

lately I typed in the following expressions ...

Main> []
ERROR: Cannot find "show" function for:
*** Expression : []
*** Of type    : [a]

Main> reverse []
ERROR: Cannot find "show" function for:
*** Expression : reverse []
*** Of type    : [a]

Main> error "test"
ERROR: Cannot find "show" function for:
*** Expression : error "test"
*** Of type    : a

Each of the inputs is answered by an error message! Is this what we
would expect? Can you see why? As another example, consider the
following simple program.

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

> hd Empty                      =  error "hd: empty tree"
> hd (Node Empty a r)           =  a
> hd (Node l@(Node _ _ _) a r)  =  hd l

> tl Empty                      =  error "tl: empty tree"
> tl (Node Empty a r)           =  r
> tl (Node l@(Node _ _ _) a r)  =  Node (tl l) a r

As a start we type in some simple test cases

Main> hd (Node Empty 2 Empty)
2
Main> hd Empty
ERROR: Cannot find "show" function for:
*** Expression : hd Empty
*** Of type    : a

Main> tl (Node Empty 2 Empty)
ERROR: Cannot find "show" function for:
*** Expression : tl (Node Empty 2 Empty)
*** Of type    : Tree Integer

Main> tl Empty
ERROR: Cannot find "show" function for:
*** Expression : tl Empty
*** Of type    : Tree a

Yes, of course: the `deriving (Show)' clause is missing.

> data Tree a                   =  Empty
>                               |  Node (Tree a) a (Tree a)
>                                  deriving (Show)

We try again, only to find that somehow the base cases are not handled
properly.

Main> hd (Node Empty 2 Empty)
2
Main> hd Empty
ERROR: Cannot find "show" function for:
*** Expression : hd Empty
*** Of type    : a

Main> tl (Node Empty 2 Empty)
Empty
Main> tl Empty
ERROR: Cannot find "show" function for:
*** Expression : tl Empty
*** Of type    : Tree a

The solution to the problem is that Hugs refuses to print values of
polymorphic type!  We have

[] :: [a]
reverse [] :: [a]
error "test" :: a
hd Empty :: a
tl Empty :: Tree a

In each case the type involves a type variable.

Could Hugs be a bit more clever here? Good old Miranda distinguished
between scripts and command-level expressions. In scripts this
restriction is sensible, on the top-level it is not. So please, change
Hug's behaviour which is certainly a miracle to beginners and maybe
also to the experienced Haskell programmer.

Cheers, Ralf

Reply via email to