为什么我可以在 Haskell 中打印包含 5 个元素的元组而不是包含 6 个元素的元组?

Why can I print a tuple with 5 elements but not with 6 in Haskell?

我尝试打印以下元组

(1,2,3,4,5,6)

这给了我以下错误

ERROR - Cannot find "show" function for:
*** Expression : (1,2,3,4,5,6)
*** Of type    : (Integer,Integer,Integer,Integer,Integer,Integer)

但是如果我尝试使用以下元组

(1,2,3,4,5)

一切都很顺利。

我的猜测是 Haskell 只为最多五个元素的元组定义 Show(因为具有 5 个元素的元组显然是不同于具有 6 个元素的元组的另一种类型)。但不知怎的,我太愚蠢了,找不到证据。

所以我的问题是:

为什么会这样,如何打印包含 6 个元素的元组(因为我似乎无法将元组放在 instance 定义的头部)?

编辑:我正在使用拥抱

这是我在 ghci

上的测试
Prelude> (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,5 1,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100)

<interactive>:445:1:
    A 100-tuple is too large for GHC
      (max size is 62)
      Workaround: use nested tuples or define a data type

但是,打印 15 是极限

Prelude> (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

<interactive>:450:1:
    No instance for (Show
                       (t0,
                        t1,
                        t2,
                        t3,
                        t4,
                        t5,
                        t6,
                        t7,
                        t8,
                        t9,
                        t10,
                        t11,
                        t12,
                        t13,
                        t14,
                        t15))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for
      (Show
         (t0,
          t1,
          t2,
          t3,
          t4,
          t5,
          t6,
          t7,
          t8,
          t9,
          t10,
          t11,
          t12,
          t13,
          t14,
          t15))
    In a stmt of an interactive GHCi command: print it

不要使用拥抱。

它已经多年没有维护了,并且不支持现代 Haskell 实现应该支持的一半内容。 (拥抱支持"most of" Haskell '98,这是语言规范的先前修订版。)

您可能会在 Internet 上找到与 GHC 完美配合的示例代码,但与 Hugs 配合使用时却惨遭失败。您将花费很长时间试图弄清楚为什么您的代码不起作用,最终才意识到 "oh, it does work — just not with Hugs".

给你。为 6 元组包含此 Show 实例

-- Tuples   -- for ref, def'n from the Prelude
{-
instance  (Show a, Show b) => Show (a,b)  where
  showsPrec p (x,y) = showChar '(' . shows x . showChar ',' .
                                   shows y . showChar ')'


instance  (Read a, Read b) => Read (a,b)  where
    readsPrec p       = readParen False
                        (\r -> [((x,y), w) | ("(",s) <- lex r,
                                             (x,t)   <- reads s,
                                             (",",u) <- lex t,
                                             (y,v)   <- reads u,
                                             (")",w) <- lex v ] )
-}
-- Other tuples have similar Read and Show instances

instance  (Show a, Show b, Show c, Show d, Show e, Show f) 
      => Show (a,b,c,d,e,f)  where
  showsPrec p (x1,x2,x3,x4,x5,x6) = showChar '(' . shows x1 . showChar ',' .
                                   shows x2 . showChar ',' . 
                                   shows x3 . showChar ',' .
                                   shows x4 . showChar ',' .
                                   shows x5 . showChar ',' .
                                   shows x6 . showChar ')'