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.
TypeOperators extension has been available since GHC 6.8.1 and implies the
ExplicitNamespaces extension.GHC User Manual documentation for
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:
An example of failing with prefix notation:
With the extension enabled, either of these declarations is permitted.
Using type operators
You can use a type operator, in prefix notation, without any extensions enabled.
However, you cannot use it in infix notation.
Using a type operator in infix notation requires enabling the
In Functortown, we discuss the following example using the
We emphasize that since type constructors are functions (at the type level), functor composition bears a great deal of similarity to function composition.Function composition:
(.) Function composition is often written using the infix operator
(.). If we wanted our code to emphasize the similarity as well, we might want to define
(.) at the type level as an alias for the
Compose type constructor.
GHC.Generics defines types named