Pointfree style is a great example of the flexibility that Haskell’s syntax can afford you in writing code that’s short and to the point. Since Haskell’s grammar requires very little in the way of additional punctuation to separate out different parts of an expression, pointfree style feels like a natural evolution of Haskell’s already generally minimalist style. The downside of both pointfree style specifically, and Haskell’s lightweight syntax in general, is that it relies on implicit rules about how things are parsed, rather than explicit punctuation where characters and symbols are used to tell both the compiler and the reader how to parse the code.

Thankfully, although these parsing rules are implicit and not written directly into the code you read or write, there are only a few of them. In this section we’ll start by looking at some concrete examples of how Haskell parses things in different circumstances, and the rules behind the choices it makes. Next, you’ll learn how to define your own custom operators. This will give you the opportunity to learn about two other important factors in how Haskell code gets parsed: binding precedence and associativity.

Let’s start with a simple example of something that does what we would expect:

​  ​ λ ​ ​ "the first number is " ​ <> show 1
​  ​ "the first number is 1" ​

We’ve written code like this before, and at the time we didn’t remark on it, but this is a good example of one of the implicit rules that helps Haskell avoid too many parentheses: passing an argument to a function always has higher precedence than passing that argument to an operator. In formal Haskell speak, we’d say that function application has a higher binding precedence than operator application. Without this rule, our example wouldn’t work because the (<>) operator would get passed to the function show instead of the string we get back when we say show 1 . This same rule also lets us call functions in the middle of a longer string of calls to operators:

​  ​ λ ​ ​ "the sum of " ​ <> show 1 <> ​ " and " ​ <> show 2 <> ​ " is " ​ <> show 3
​  ​ "the sum of 1 and 2 is 3" ​

The higher binding precedence of functions means we can omit the parentheses in this example, but there are other cases where it means we have to add some parentheses. For example, if we try show the sum of two numbers without parentheses, we’ll get an error:

​  λ show 1 + 2
​  
​  <interactive>:119:8: error:
​  • No instance for (Num String) arising from a use of ‘+’
​  • In the expression: show 1 + 2
​  In an equation for ‘it’: it = show 1 + 2

The problem here is the binding precedence of functions means show is called with 1 first, giving us the string "1" . Next, we try to add the string "1" to the numeric literal 2 . Haskell won’t let us add a number and a string together, so we get an error instead. We can introduce parentheses to solve this problem:

​  ​ λ ​ show (1 + 2)
​  ​ "3" ​

Parentheses override all other precedence and let us explicitly set the order in which things should be evaluated. Within the parentheses, all of the normal rules about precedence and binding still apply, but from the outside the entire parenthetical expression is treated as a single thing. In the case of our example, that means that even though function application has the highest precedence, the thing that’s being applied is the result of evaluating the entire parenthetical expression.

Function application having higher precedence than operator application is a helpful rule for resolving ambiguity in code like show 1 + 2 , but what happens if we try to pass the result of one function call into another function and introduce some ambiguity. As an example, let’s write a function that adds two numbers:

​  ​ λ ​ add a b = a + b

We could try to add the result of two other additions together by writing something like:

​  ​ λ ​ add add 1 2 add 3 4

As you can probably guess, this doesn’t work. If you try this you should get an error like this:

​  <interactive>:123:1: error:
​  • Non type-variable argument in the constraint: Num (a -> a -> a)
​  (Use FlexibleContexts to permit this)
​  • When checking the inferred type
​  it :: forall {a}.
​  (Num a, Num (a -> a -> a),
​  Num ((a -> a -> a) -> (a -> a -> a) -> a -> a -> a)) =>
​  a

We can add parentheses to fix our error:

​  ​ λ ​ add (add 1 2) (add 3 4)
​  10

Although this might be intuitive, there’s no particular reason parsing has to work this way. When faced with some ambiguity around what to parse, for example, because two things have the same precedence, the compiler needs to pick an order. We can visualize this by adding parentheses to help develop an intuition for the order that things get evaluated in.

Our original statement:

​  add add 1 2 add 3 4

Becomes:

​  ((((((add add) 1) 2) add) 3) 4)

In this approach, each time that there’s a function being called, we need to figure out how to prioritize what to evaluate first, so we start with the left and work our way “out” or to the right. Alternatively, we could have worked right-to-left. It’s hard to visualize exactly what this would look like, especially if you are accustomed to working in left-to-right languages. The fact that we write functions to the left of their arguments in Haskell means that parsing function application right-to-left would look strange indeed. We can imagine it would be something like this:

​  (add ((add 1 2) (add 3 4)))

The choice of whether to parse things left-to-right or right-to-left is known as its associativity . We would say that our first example is left associative and the second example is right associative . Normal function application in Haskell is left associative . Infix functions and operators have a bit more flexibility.

We can’t control the binding precedence or associativity of normal function application in Haskell, but we can configure these for infix functions. This combination of properties is called its fixity , and we declare it using a fixity declaration . You’re most likely to see fixity declarations when creating custom operators.

Creating Custom Operators

Haskell allows you to define your own operators, and as you’ll see later on in chapter 6 , allows you to provide your own implementation of some commonly used operators. Used sparingly, custom operators can improve the experience of using a library, but be careful—overuse of operators can lead to unreadable and unmaintainable code.

Another benefit to creating your own operators, and the one we’ll focus on in this section, is that they can give you a useful insight into some of the parsing rules you’ve just learned about. In this section, we’ll implement a few different operators to get a feel for how they work, with a special focus on what we can learn about Haskell’s syntax as we’re creating them.

First things first, what is an operator? You’ve already seen that operators work a lot like regular Haskell functions, but there are a few important differences:

  1. Operators are infix by default, and can be made to work like regular functions when surrounded by parentheses. Regular functions need to be surrounded in backticks to be used infix.

  2. Functions have a higher binding precedence than operators, so when it’s ambiguous whether an argument belongs to a function or an operator, the function will always be chosen.

  3. A function can have any number of arguments, but a custom operator must always have exactly two arguments. We call these binary operators .

  4. Functions can be named using any letters. Operators must be named using symbols.

  5. A function whose name starts with an uppercase letter is a type constructor , which you’ll learn about when you start creating your own types . Similarly, operators that start with the colon symbol, : , are reserved for type constructors.

Haskell operators can be named using nearly any combination of ASCII and Unicode symbols, but they can’t contain letters or spaces. For the purposes of operator naming, the underscore character, _ , is considered a letter, so you can’t use it in your operator names. You also can’t use parentheses, () , or square brackets, [] , in your operator names. Angle brackets <> are allowed, and are popular when naming operators.

Let’s start by defining our own addition operator, which we’ll call (+++) . There are two ways that we can define an operator. First, we can write the definition as though we were writing the function in prefix form, with parentheses:

​  ​ module ​ ​ OperatorExample ​ ​ where ​
​  
​  (+++) a b = a + b

Alternatively, we can write it as though we were using it in infix form:

​  ​ module ​ ​ OperatorExample ​ ​ where ​
​  
​  a +++ b = a + b

The difference here is entirely stylistic. You can still use the operator as a normal infix operator even if you define it using the prefix form; you can also still use parentheses to call the operator in prefix form even if you’ve defined it using the infix style. We’ll use infix form for the examples in this section, but you can choose whichever you like better.

If you load this example up in ghci you can see that it does work as an operator and lets you add numbers. You can also still do all of the normal operator things you’d do, like use it in prefix form, or partially apply it:

​  ​ λ ​ :load ​ OperatorExample ​.hs
​  [1 ​ of ​ 1] ​ Compiling ​ ​ OperatorExample ​ ( ​ OperatorExample ​.hs, interpreted )
​  ​ Ok ​, one ​ module ​ ​ loaded. ​
​  ​ λ ​ ​ 1 ​ ​ +++ ​ ​ 1 ​
​  ​ 2 ​
​  ​ λ ​ ​ (+++) ​ ​ 1 ​ ​ 2 ​
​  ​ 3 ​
​  ​ λ ​ ​ increment ​ ​ = ​ ​ (+++ ​ ​ 1) ​
​  ​ λ ​ ​ increment ​ ​ 8 ​
​  ​ 9 ​
​  ​ λ ​ ​ another ​​ Incrementer ​ = (1 +++)
​  ​ λ ​ anotherIncrementer 0
​  1

This is looking pretty good, but if we try to use our new operator as part of a longer expression, we’ll see that our operator is giving us different results compared to the standard addition operator:

​  ​ λ ​ 1 +++ 2 * 3
​  9
​  ​ λ ​ 1 + 2 * 3
​  7

The problem is that our new operator has higher precedence than multiplication, so in our first example we’re calculating (1 +++ 2) * 3 . In normal arithmetic, multiplication has a higher precedence than addition, and in Haskell (*) has a higher precedence than (+) operator. That means that in our second example we’re calculating 1 + (2* 3) . We can fix this by adding a fixity declaration .

A fixity declaration has three parts. First, you declare the operator’s associativity:

  • infixl for left associativity
  • infixr for right associativity
  • infix if the operator is not associative

Next, you set the operators binding precedence. The binding precedence is a number from 0 (the lowest precedence) to 9 (the highest precedence). The last part of the fixity declaration is the name of the operator, or infix function, that you are declaring the fixity for.

Normal addition in Haskell is left associative and has a precedence of 6. You can find this out from the online documentation, or by looking at the info in ghci using the :info command:

​  ​ λ ​ :info (+)
​  ​ type ​ ​ Num ​ :: * -> ​ Constraint ​
​  ​ class ​ ​ Num ​ a ​ where ​
​  (+) :: a -> a -> a
​  ...
​  ​ -- Defined in ‘GHC.Num’ ​
​  ​ infixl ​ 6 +

There’s a lot of extra information in this output that you won’t understand until chapter 3, when you learn more about types . What’s important for us is the last line of output:

​  ​ infixl ​ 6 +

This is the fixity declaration for addition. If we use the same fixity declaration for (+++) then we should see the same behavior in our example. Let’s give it a try:

​  ​ module ​ ​ OperatorExample ​ ​ where ​
​  
​  ​ infixl ​ 6 +++
​  a +++ b = a + b
​  ​ λ ​ :load ​ OperatorExample ​
​  [1 ​ of ​ 1] ​ Compiling ​ ​ OperatorExample ​ ( ​ OperatorExample ​.hs, interpreted )
​  ​ Ok ​, one ​ module ​ ​ loaded. ​
​  ​ λ ​ ​ 1 ​ ​ +++ ​ ​ 2 ​ ​ * ​ ​ 3 ​
​  ​ 7 ​

Now that we’ve lowered the binding precedence from the default of 9 to 6 , our new addition operator behaves like we would expect.

Copying the fixity declaration from (+) helped us get working code, but we don’t always have some existing work we can reference. Let’s look at some different fixity definitions and how they would have changed the behavior.

Since we’re going to be experimenting with several different versions of the same function, with different fixity declarations, it may be easier for you to work in ghci rather than making the changes in your file and re-loading it. As a matter of convenience you can enter the definition of your operator and its fixity declaration on the same line in ghci separated by a semicolon:

​  ​ λ ​ a +++ b = a + b; ​ infixl ​ 6 +++

You can use the up-arrow key to scroll backwards through history and re-edit this line to save yourself some typing. You can also type Control-r to search backwards through history to get back to this line if you’ve been running several different experiments. This can be a fast way to iteratively test different minor changes to a piece of code.

The first change we’ll try is changing the binding precedence from 6 to 7. The fixity of (*) is infixl 7 * , so in this example both operators will have the same precedence:

​  ​ λ ​ a +++ b = a + b; ​ infixl ​ 7 +++

If we run our first example, we’ll see that we’re once again adding before we multiply:

​  ​ λ ​ 1 +++ 2 * 3
​  9

However, if we re-order our example and have the multiplication on the left-hand side of the expression, when both operators have the same precedence, then we’ll multiply first:

​  ​ λ ​ 3 * 2 +++ 1
​  7

If the precedence of (+++) is higher or lower than (*) , then the output will be consistent regardless of the order in which we type the expression:

​  ​ λ ​ a +++ b = a + b; ​ infixl ​ 6 +++
​  ​ λ ​ 3 * 2 +++ 1
​  7
​  ​ λ ​ 1 +++ 2 * 3
​  7
​  ​ λ ​ a +++ b = a + b; ​ infixl ​ 8 +++
​  ​ λ ​ 3 * 2 +++ 1
​  9
​  ​ λ ​ 1 +++ 2 * 3
​  9

This example tells us that when the binding precedence for two left-associative operators is the same, then they are evaluated left-to-right. If we had two right-associative operators of the same precedence, then the evaluation will apply right-to-left. We can see an example of this if we define a right-associative version of (*) , (***) and re-run some of our examples using it:

​  ​ λ ​ a +++ b = a + b; ​ infixr ​ 7 +++
​  ​ λ ​ a *** b = a * b; ​ infixr ​ 7 ***
​  ​ λ ​ 1 +++ 2 *** 3
​  7
​  ​ λ ​ 3 *** 2 +++ 1
​  9
​  
​  ​ λ ​ a *** b = a * b; ​ infixl ​ 7 ***
​  ​ λ ​ a +++ b = a + b; ​ infixl ​ 7 +++
​  ​ λ ​ 1 +++ 2 *** 3
​  9
​  ​ λ ​ 3 *** 2 +++ 1
​  7

What if we have two operators with different associativity? As long as they have a different binding precedence, everything will work as we’d expect. For example, if we keep (***) left associative, and make (+++) right associative, you’ll see that the behavior remains the same when they have different precedence:

​  ​ λ ​ a *** b = a * b; ​ infixl ​ 7 ***
​  ​ λ ​ a +++ b = a + b; ​ infixr ​ 6 +++
​  ​ λ ​ 1 +++ 2 *** 3
​  7
​  ​ λ ​ 3 *** 2 +++ 1
​  7
​  ​ λ ​ a +++ b = a + b; ​ infixr ​ 8 +++
​  ​ λ ​ 1 +++ 2 *** 3
​  9
​  ​ λ ​ 3 *** 2 +++ 1
​  9

If the binding precedence is the same though, we’ll get an error. The statement is ambiguous and the parser doesn’t have any way to resolve the situation:

​  λ a +++ b = a + b; infixr 7 +++
​  λ a *** b = a * b; infixl 7 ***
​  λ 1 +++ 2 *** 3
​  
​  <interactive>:289:1: error:
​  Precedence parsing error
​  cannot mix ‘+++’ [infixr 7] and ‘***’ [infixl 7]
​  in the same infix expression

When the precedence of the operations is the same and they have different associativity, there are no rules that will tell the compiler how to evaluate the expression, so it gives up. We can still evaluate this if we use parentheses:

​  ​ λ ​ (1 +++ 2) *** 3
​  9
​  ​ λ ​ 1 +++ (2 *** 3)
​  7

Another case where we have to use parentheses is when an operator doesn’t define associativity at all. The equals operator, (==) is an example of a commonly used operator without associativity. Its fixity declaration is:

​  ​ infix ​ 4 ==

We can see this in action from ghci :

​  λ True == True == False
​  
​  <interactive>:294:1: error:
​  Precedence parsing error
​  cannot mix ‘==’ [infix 4] and ‘==’ [infix 4]
​  in the same infix expression

Just like when we have operators with different associativity, you have to use parentheses if you want to write an expression that uses multiple operators that are not associative:

​  ​ λ ​ ​ True ​ == (​ True ​ == ​ True ​)
​  ​ True ​
​  ​ λ ​ (​ True ​ == ​ True ​) == ​ True ​
​  ​ True ​

All of the examples of fixity declarations that we’ve looked at so far have focused on operators. You can also write a fixity declation for a standard function that is being used as an infix operator. For example, let’s write a division function that does the same thing as normal numeric division:

​  ​ module ​ ​ OperatorExample ​ ​ where ​
​  
​  divide = (/)

Since we haven’t given a specific fixity declaration to divide , when we use it as an infix operator it will have the usual operator fixity: a binding precedence of 9 and left associativity. The (/) operator is also left associative, but has a precedence of 7, the same as multiplication. That means that repeated calls to divide should give us the same result as repeated calls to (/) :

​  ​ λ ​ :load ​ OperatorExample ​.hs
​  [1 ​ of ​ 1] ​ Compiling ​ ​ OperatorExample ​ ( ​ OperatorExample ​.hs, interpreted )
​  ​ Ok ​, one ​ module ​ ​ loaded. ​
​  ​ λ ​ ​ 1 ​ ​ / ​ ​ 2 ​ ​ / ​ ​ 3 ​ ​ / ​ ​ 4 ​
​  ​ 4.1666666666666664e-2 ​
​  ​ λ ​ ​ 1 ​ ​ `divide` ​ ​ 2 ​ ​ `divide` ​ ​ 3 ​ ​ `divide` ​ ​ 4 ​
​  ​ 4.1666666666666664e-2 ​

We can give divide a fixity declaration, just like we’d do with an operator. For example, if we change divide to be right associative, then we can see that repeated calls will start giving us a very different answer:

​  ​ λ ​ divide = (/); ​ infixr ​ 9 `divide`
​  ​ λ ​ 1 `divide` 2 `divide` 3 `divide` 4
​  0.375

You’ll notice in this example that when we write a fixity declaration for a regular function, we need to put it in backticks. The fixity declaration doesn’t change anything about how the function will behave if it’s not being called infix. As an example, let’s set the precedence of divide to 0, and then call it a few times infix as well as calling it as a normal function. You’ll see that when it’s called infix it will have low precedence, but when it’s called as a normal function it will continue to have a higher precedence than any other operator:

​  ​ λ ​ divide = (/); ​ infixr ​ 0 `divide`
​  ​ λ ​ divide 1 2 * 10
​  5.0
​  ​ λ ​ 1 `divide` 2 * 10
​  5.0e-2

As you can see, the fixity rules for operators, and for functions that are being used in infix form, have a big impact on how your code is evaluated. Although you might only create custom operators occasionally, the rules for binding and associativity are important to keep in mind because they impact if, and when, you need to use parentheses or the ($) operator when you are writing programs. Throughout this book we will occasionally add parentheses or use the ($) when the binding and precedence rules mean that they aren’t strictly needed. This isn’t typical in most Haskell code. The common style in Haskell is to omit these things whenever you can. They are included in some examples in this book to add clarity. If you are looking at some example code later on in this book and find it confusing, the parsing rules around fixity are a common culprit. Consider returning to this section and reviewing the material to see if it makes the syntax of the example easier to follow.