How to use type constructor in infix form

I am looking through some Haskell documentation and found a statement

You can declare a constructor ( for type and data ) as an infix operator, and this can make your code more readable.

I can use the data constructor in the infix form as shown below:

Prelude> data List a = Empty | a :-> (List a) deriving Show Prelude> Prelude> let var1 = 10 :-> Empty Prelude> let var2 = 20 :-> var1 Prelude> let var3 = 30 :-> var2 Prelude> Prelude> var1 10 :-> Empty Prelude> Prelude> var2 20 :-> (10 :-> Empty) Prelude> Prelude> var3 30 :-> (20 :-> (10 :-> Empty)) 

My question is how to use type constructor in infix form, can anyone give me an example to figure this out?

+6
source share
2 answers
 > :set -XTypeOperators > data a :-> b = C (a -> b) > :t C id C id :: b :-> b 

Remember that his name must begin with : (roughly,: considered "capital").

Otherwise, use reverse measures, as in a `T` b .

+7
source

To extend the answer to @chi, with recent versions of GHC, the TypeOperators syntax has changed a bit: enter constructor names that would otherwise be infix type names (i.e. characters without a leading : still classified as constructor type names, which means the following code works and defines a constructor like infix + :

 {-# language TypeOperators #-} data a + b = L a | R b 
+4
source

All Articles