11

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?

AJF
  • 11,767
  • 2
  • 37
  • 64
  • 2
    Haskell proper may not allow it, but GHC has extensions that do https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/data-type-extensions.html – jamshidh May 04 '15 at 20:17

2 Answers2

16

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
Community
  • 1
  • 1
bheklilr
  • 53,530
  • 6
  • 107
  • 163
  • 6
    You don't need to enable `TypeOperators` for the infix constructor with `:` but can write `data A a b = a :> b deriving Show` – Michael May 04 '15 at 20:36
12

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.

MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
  • 1
    FYI: Haskell 2010 report use term “data constructor” rather than “value constructor”, and I remember some books before 2010 use “data constructor”, too. – SnowOnion Oct 16 '18 at 07:55