(Related to Using custom instance when deriving an instance via GeneralizedNewtypeDeriving). Suppose that I have :
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Main (main) where
data Cat = Cat String deriving (Show)
newtype Dolphin = Dolphin Cat deriving (Info,Show)
class Info a where
info :: a -> (a,String)
instance Info Cat where
info a = (a,show a)
main = do
print $ info (Cat "mammal")
--OK
--OUTPUT 1 : (Cat "mammal","Cat \"mammal\"")
print $ info (Dolphin $ Cat "mammal")
--NOT OK
--OUTPUT 2 : (Dolphin (Cat "mammal"),"Cat \"mammal\"")
print $ show (Dolphin $ Cat "mammal")
--OUTPUT 3 : "Dolphin (Cat \"mammal\")"
In output 2, we know that info
is operating on an argument of type Dolphin
(see the first element of the tuple), and yet it prints out "Cat \"mammal\""
for the second element of the tuple. Yet we know that show
executed on an argument of type Dolphin
should print out "Dolphin (Cat \"mammal\")"
(see output 3). So why does ghci (in output 2) print out "(Dolphin (Cat "mammal"),"Cat \"mammal\"")"
when I think it should print out "(Dolphin (Cat "mammal"),"Dolphin (Cat \"mammal\")")"
?