考虑两个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
是Infix并相应地打印出来。::
语法似乎不允许插入。
有什么方法可以使编译器以W
为infix得出(明确为Z
提供show
实例除外(?所需的输出为
1 `Y` 2
3 `W` 4
当前不是。GADT构造函数仅在一组特定条件下被标记为infix:
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 Bug线程(