Opened 7 years ago

Closed 7 years ago

#7199 closed bug (invalid)

Standalone deriving Show at GHCi prompt causes divergence when printing

Reported by: dpmulligan Owned by:
Priority: normal Milestone:
Component: GHCi Version: 7.4.1
Keywords: deriving, divergence, ghci Cc:
Operating System: Linux Architecture: x86
Type of failure: GHCi crash Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Deriving a show instance for a data type (defined either with the standard Haskell 98 syntax or with GADT syntax) within GHCi using the command:

instance Show <typename>

causes divergence/stack overflow when printing values of that type. The issue does not manifest itself when you use the standard deriving mechanism within a Haskell source file.

Minimum example necessary to replicate:

module Main (
  main
)
where

  -- At GHCi prompt type
  --   instance Show Bool'
  -- then
  --   True'
  -- to get a stack overflow
  data Bool'
    = True'
    | False'

  main = return ()

Change History (1)

comment:1 Changed 7 years ago by simonpj

difficulty: Unknown
Resolution: invalid
Status: newclosed

This code

  instance Show Bool'

does not derive a Show instance. To do that you need to either say

  data Bool' = T' | F'  deriving( Show )

or you can use standalone deriving (with -XStandaloneDeriving):

  deriving instance Show Bool'

By merely saying instance Show Bool' you are using ordinary Haskell 98 to define an instance, filling in the default methods as desribed in the language specficiation. So it's as if you'd typed

  instance Show Bool' where
    show x = showsPrec 0 x ""
    showsPrec _ x s = show x ++ s

(The default declarations come from the library.) Since each method is defined in terms of the other, the result diverges.

Simon

Note: See TracTickets for help on using tickets.