Type operators

In standard Haskell, operator symbols may only be used at the value level.The only exception to this is the function arrow, (->), which is a built-in type operator. GHC expands the role of operators with the TypeOperators extension, which makes it possible to use an operator as the name of a type.

Defining type operators

By default, if you try to define a type whose name isn’t composed of letters, compilation fails and GHC suggests that you enable the TypeOperators extension. This happens regardless of whether you place the name in infix or prefix position.

An example of failing with infix notation:

λ> data a & b = Tuple a b

error:
    Illegal declaration of a type or class operator ‘&’
      Use TypeOperators to declare operators in type and declarations

An example of failing with prefix notation:

λ> data (&) a b = Tuple a b

error:
    Illegal declaration of a type or class operator ‘&’
      Use TypeOperators to declare operators in type and declarations

With the extension enabled, either of these declarations is permitted.

λ> :set -XTypeOperators

λ> data a & b = Tuple a b

Using type operators

You can use a type operator, in prefix notation, without any extensions enabled.

λ> x = Tuple 2 'c' :: (&) Int Char

However, you cannot use it in infix notation.

λ> x = Tuple 2 'c' :: Int & Char

error:
    Illegal operator ‘&’ in type ‘Int & Char’
      Use TypeOperators to allow operators in types

Using a type operator in infix notation requires enabling the TypeOperators extension.

λ> :set -XTypeOperators

λ> x = Tuple 2 'c' :: Int & Char

Example: Generics

GHC generics GHC.Generics defines types named (:+:) and (:*:).

Type Classes offers courses and projects to get you started and make you an expert in FP with Haskell. For $29/month, you get access to the complete archive and all the latest content.