[Haskell-beginners] How to explain this behaviour of ghc ?
micah at cowan.name
Fri Oct 10 18:00:14 EDT 2008
-----BEGIN PGP SIGNED MESSAGE-----
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
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----
More information about the Beginners