Haskell:为什么不允许使用中缀类型构造函数?
Haskell: Why aren't infix type constructors allowed?
在 Haskell 98 report 中,我发现了这个:
The syntax for Haskell type expressions is given above. Just as data values are built using data constructors, type values are built from type constructors. As with data constructors, the names of type constructors start with uppercase letters. Unlike data constructors, infix type constructors are not allowed (other than (->)).
没有给出关于为什么中缀类型构造函数不被允许的原因。在 Agda 等中,中缀类型构造函数很常见。为什么不在 Haskell 中?
它不是 Haskell 标准的一部分,但正如 提到的那样,它在 GHC 中仍然是可能的。需要注意的是,数据构造函数(不是类型构造函数)必须以冒号开头:
{-# LANGUAGE TypeOperators #-}
data a + b = a :+ b
f :: a + b -> a
f (a :+ b) = a
g :: a + b -> b
g (a :+ b) = b
完全清楚:Haskell 98 和 Haskell 2000 都允许中缀 值构造函数 例如
data Complex r = r :+ r
这里的值构造函数 (:+)
是中缀,如 5 :+ 7
.
您只需要 TypeOperators
扩展即可拥有 类型构造函数 ,它们是中缀。例如,
data x ??! y = Left x | Right y
这里的类型构造函数 (??!)
是中缀,如 Int ??! Bool
.
在 Haskell 98 report 中,我发现了这个:
The syntax for Haskell type expressions is given above. Just as data values are built using data constructors, type values are built from type constructors. As with data constructors, the names of type constructors start with uppercase letters. Unlike data constructors, infix type constructors are not allowed (other than (->)).
没有给出关于为什么中缀类型构造函数不被允许的原因。在 Agda 等中,中缀类型构造函数很常见。为什么不在 Haskell 中?
它不是 Haskell 标准的一部分,但正如
{-# LANGUAGE TypeOperators #-}
data a + b = a :+ b
f :: a + b -> a
f (a :+ b) = a
g :: a + b -> b
g (a :+ b) = b
完全清楚:Haskell 98 和 Haskell 2000 都允许中缀 值构造函数 例如
data Complex r = r :+ r
这里的值构造函数 (:+)
是中缀,如 5 :+ 7
.
您只需要 TypeOperators
扩展即可拥有 类型构造函数 ,它们是中缀。例如,
data x ??! y = Left x | Right y
这里的类型构造函数 (??!)
是中缀,如 Int ??! Bool
.