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 W
infix,(除了显式提供show
实例Z
)?期望的输出是
1 `Y` 2
3 `W` 4
Run Code Online (Sandbox Code Playgroud)
ram*_*ion 13
不是现在.GADT构造函数仅在特定条件下标记为中缀:
Run Code Online (Sandbox Code Playgroud)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线程)