13

考虑两个data声明:

{-# LANGUAGE GADTs #-}

data X = Int `Y` Int deriving Show

data Z where
        W :: Int -> Int -> Z  deriving Show

main = do
         print (1 `Y` 2)
         print (3 `W` 4)

运行上述程序会产生:

1 `Y` 2
W 3 4

所以派生show知道这Y是中缀并相应地打印它。::语法似乎不允许中缀。

有什么方法可以让编译器将 show for 派生为W中缀,(除了显式地为 提供一个show实例Z)?所需的输出是

1 `Y` 2
3 `W` 4
4

1 回答 1

13

不是现在。GADT 构造函数仅在一组特定条件下被标记为中缀:

Note [Infix GADT constructors]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We do not currently have syntax to declare an infix constructor in GADT syntax,
but it makes a (small) difference to the Show instance.  So as a slightly
ad-hoc solution, we regard a GADT data constructor as infix if
  a) it is an operator symbol
  b) it has two arguments
  c) there is a fixity declaration for it
For example:
   infix 6 (:--:)
   data T a where
     (:--:) :: t1 -> t2 -> T Int

所以对于像这样的非符号构造函数W,看起来你不走运,但如果你愿意让它成为符号,你可以添加一个固定性声明。

此模板 haskell 错误线程的提示)

于 2017-06-23T20:45:50.913 回答