[Haskell-cafe] bug in ghci ?
Ivan Lazar Miljenovic
ivan.miljenovic at gmail.com
Thu Jul 8 09:37:10 EDT 2010
"Pasqualino \"Titto\" Assini" <tittoassini at gmail.com> writes:
> Hi,
>
> I just noticed that in ghci:
>
>
> data Test = Test String
>
> instance Show Test
>
> show $ Test "Hello"
Well, for starters you specify such a thing in ghci, so presumably you
loaded a file with these definitions.
> Will result in infinite recursion.
>
> Is this a known bug?
Not a bug. Let us consider the definition of the Show class:
class Show a where
showsPrec :: Int -> a -> ShowS
showsPrec _ x s = show x ++ s
-- | A specialised variant of 'showsPrec', using precedence context
-- zero, and returning an ordinary 'String'.
show :: a -> String
show x = shows x ""
showList :: [a] -> ShowS
showList ls s = showList__ shows ls s
Note that showsPrec and show are by default mutually recursive, and that
all the class methods have a default definition. Thus, just saying
"instance Show Test" like you have above is valid, but not very
helpful. This is why the documentation for the Show class specifies
that the minimal complete definition is either showsPrec or show:
defining at least one of these will cut out of the infinite recursion
loop.
Of course, a better question is: why are you writing your own instance
for Show? Unless your data type is complex, you should just have
"deriving (Show)" (as well as classes such as Eq, Ord and Read) attached
to your datatype definition.
--
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com
More information about the Haskell-Cafe
mailing list