(sec:1.2)=
# Recursion #
Until now, we have encountered two types of logical formulas: facts and rules. There is a special kind of rule which deserves special attention: the rule which defines a relation in terms of itself. This idea of 'self-reference', which is called *recursion*, is also present in most procedural programming languages. Recursion is a bit difficult to grasp, but once you've mastered it, you can use it to write very elegant programs, e.g.
```text
IF N=0
THEN FAC:=1
ELSE FAC:=N*FAC(N-1).
```
is a recursive procedure for calculating the factorial of a given number, written in a Pascal-like procedural language. However, in such languages *iteration* (looping a pre-specified number of times) is usually preferred over recursion, because it uses memory more efficiently.
+++
In Prolog, however, recursion is the **only** looping structure[^1_]. (This does not necessarily mean that Prolog is always less efficient than a procedural language, because there are ways to write recursive loops that are just as efficient as iterative loops, as we will see in {numref}`sec:3.6`.) Perhaps the easiest way to think about recursion is the following: an arbitrarily large chain is described by describing how one link in the chain is connected to the next. For instance, let us define the relation of *reachability* in our underground example, where a station is reachable from another station if they are connected by one or more lines. We could define it by the following 20 ground facts:
```{swish} swish:1.1.1
```
Since any station is reachable from any other station by a route with at most two intermediate stations, we could instead use the following (non-recursive) definition:
```Prolog
reachable(X,Y):-connected(X,Y,L).
reachable(X,Y):-connected(X,Z,L1),connected(Z,Y,L2).
reachable(X,Y):-connected(X,Z1,L1),connected(Z1,Z2,L2),
connected(Z2,Y,L3).
```
Of course, if we were to define the reachability relation for the entire London underground, we would need a lot more, longer and longer rules. Recursion is a much more convenient and natural way to define such chains of arbitrary length:
```{swish} swish:1.1.2
---
source-text-start: 1.0.1
---
```
The reading of the second rule is as follows: '`Y` is reachable from `X` if `Z` is directly connected to `X` via line `L`, and `Y` is reachable from `Z`'.
```{figure} /src/fig/part_i/image006.svg
---
name: 'fig:1.3'
width: 100%
---
A proof tree for the query `?-reachable(bond_street,W)`.
```
+++
We can now use this recursive definition to prove that Leicester Square is reachable from Bond Street ({numref}`fig:1.3`). However, just as there are several routes from Bond Street to Leicester Square, there are several alternative proofs of the fact that Leicester Square is reachable from Bond Street. An alternative proof is given in {numref}`fig:1.4`. The difference between these two proofs is that in the first proof we use the fact
```Prolog
connected(oxford_circus,tottenham_court_road,central).
```
while in the second proof we use
```Prolog
connected(oxford_circus,piccadilly_circus,bakerloo).
```
There is no reason to prefer one over the other, but since Prolog searches the given formulas top-down, it will find the first proof before the second. Thus, the order of the clauses determines the order in which answers are found. As we will see in {numref}`Chapter %s`, it sometimes even determines whether any answers are found at all.
```{figure} /src/fig/part_i/image008.svg
---
name: 'fig:1.4'
width: 100%
---
Alternative proof tree for the query `?-reachable(bond_street,W)`.
```
```{exercise} ex:1.3
```
+++
In other words, Prolog's query-answering process is a *search process*, in which the answer depends on all the choices made earlier. A important point is that some of these choices may lead to a dead-end later. For example, if the recursive formula for the reachability relation had been tried before the non-recursive one, the bottom part of {numref}`fig:1.3` would have been as in {numref}`fig:1.5`. This proof tree cannot be completed, because there are no answers to the query `?-reachable(charing_cross,W)`, as can easily be checked.
Prolog has to recover from this failure by climbing up the tree, reconsidering previous choices. This search process, which is called *backtracking*, will be detailed in {numref}`Chapter %s`.
```{figure} /src/fig/part_i/image010.svg
---
name: 'fig:1.5'
width: 100%
---
A failing proof tree.
```
[^1_]: If we take Prolog's procedural behaviour into account, there are alternatives to recursive loops such as the so-called *failure-driven loop* (see {numref}`ex:7.5`).