我可以在派生的Show中制作haskell GADT数据构造函数中缀吗?

hor*_*rsh 13 haskell infix-operator gadt deriving

考虑两个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)
Run Code Online (Sandbox Code Playgroud)

运行上述程序会产生:

1 `Y` 2
W 3 4
Run Code Online (Sandbox Code Playgroud)

所以派生show知道Y是中缀并相应地打印它.该::语法似乎并没有让infixness.

有没有办法让编译器派生为for Winfix,(除了显式提供show实例Z)?期望的输出是

1 `Y` 2
3 `W` 4
Run Code Online (Sandbox Code Playgroud)

ram*_*ion 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
Run Code Online (Sandbox Code Playgroud)

因此,对于非符号构造函数来说W,看起来你运气不好,但如果你愿意使它符号化,你可以添加一个固定声明.

(帽子提示这个模板haskell bug线程)

  • 也许a)应该放弃?如果用户指定了一个固定,可能二进制构造函数应该使用中缀,我猜. (3认同)