派生Show实例适用于ghci但现在使用ghc

时间:2015-01-08 17:20:30

标签: haskell

我试图制作一个简单的自定义类型" showable",在ghc 7.8.4下:

   data Tsil a = Nil | Snoc (Tsil a) a deriving Show

然而,虽然这在ghci下工作,

 Prelude> data Tsil a = Nil | Snoc (Tsil a) a deriving Show
 Prelude> Nil
 Nil
 Prelude> Snoc (Snoc Nil 2) 2
 Snoc (Snoc Nil 2) 2

它没有用ghc编译:

data Tsil a = Nil | Snoc (Tsil a) a deriving Show
main :: IO ()
main = do
     let a = Nil
     let b = (Snoc (Snoc Nil 2) 2)
     putStrLn $ show a
     putStrLn $ show b

[1 of 1] Compiling Main             ( snoc.hs, snoc.o )

snoc.hs:27:17:
    No instance for (Show a0) arising from a use of ‘show’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance Show a => Show (Tsil a) -- Defined at snoc.hs:4:46
      instance Show Double -- Defined in ‘GHC.Float’
      instance Show Float -- Defined in ‘GHC.Float’
      ...plus 25 others
    In the second argument of ‘($)’, namely ‘show a’
    In a stmt of a 'do' block: putStrLn $ show a
    In the expression:
      do { let a = Nil;
           let b = (Snoc (Snoc Nil 2) 2);
           putStrLn $ show a;
           putStrLn $ show b }

无法理解为什么,即使明确地推导出同样的情况也会发生:

instance (Show a) => Show (Tsil a) where
  show Nil = "Nil"
  show (Snoc x y) = "Snoc (" ++ show x ++ ") " ++ show y
抱歉,如果这是一个愚蠢的问题,谢谢你的帮助

0 个答案:

没有答案