[Haskell-beginners] How to explain this behaviour of ghc ?

Micah Cowan micah at cowan.name
Fri Oct 10 18:00:14 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tillmann Rendel wrote:
> Hi Sylvain,
> 
> sylvain wrote:
>> the following code snippet
>>
>> data Test = A Int
>> instance Show Test
>> main = print (A 1)
>>
>> leads to:
>>
>> *Main> main
>> *** Exception: stack overflow
> 
> Since "instance Show Test" does not specify how method "show" should be
> implemented for datatype Test, "show = undefined" is assumed. Now, print calls
> show to create a string represention of (A 1), which produces the error
> message.

Actually, "show = undefined" isn't assumed. The problem is that the
default definitions for show and showsPrec are:

    showsPrec _ x s   = show x ++ s
    show x            = showsPrec 0 x ""

This allows you to define either one of showsPrec or show, and have a
reasonable default definition for the other one. However, if you leave
off the definition of _both_ of them, what you get is an infinite
recursion (hence the stack overflow).

- --
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer.
GNU Maintainer: wget, screen, teseq
http://micah.cowan.name/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFI79Bu7M8hyUobTrERAnTLAJ4i3jfeS8QQQBSXqBNVW0Ph77GYKQCeKzXn
BPo7VBKIn/7t4Z8FoU+FbYc=
=7ovS
-----END PGP SIGNATURE-----


More information about the Beginners mailing list