Haskell: Why aren't infix type constructors allowed?

狂风中的少年 提交于 2019-12-01 15:16:08

问题


In the Haskell 98 report, I found this:

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 (->)).

No reasons as to why infix type constructors aren't allowed are given. In Agda and the like, infix type constructors are commonplace. Why not in Haskell?


回答1:


It's not part of the Haskell standard, but as jamshidh mentions it is still possible in GHC. The caveat is that data constructors (not type constructors) must start with a colon:

{-# LANGUAGE TypeOperators #-}

data a + b = a :+ b

f :: a + b -> a
f (a :+ b) = a

g :: a + b -> b
g (a :+ b) = b



回答2:


Just to be completely clear: Haskell 98 and Haskell 2000 both allow infix value constructors such as

data Complex r = r :+ r

Here the value constructor (:+) is infix, as in 5 :+ 7.

You only need the TypeOperators extension to have type constructors which are infix. For example,

data x ??! y = Left x | Right y

Here the type constructor (??!) is infix, as in Int ??! Bool.



来源:https://stackoverflow.com/questions/30039123/haskell-why-arent-infix-type-constructors-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!