1.4. Sequent Calculus and Automated Proof Search

In the previous chapter we studied natural deduction, a formal system that represents logical reasoning precisely. In this chapter we change the question: instead of merely checking proofs, we want to build them automatically. Given a sequent \Gamma \vdash \varphi, is there an algorithm that decides whether it is provable and, if so, produces a proof? For intuitionistic propositional logic the answer is yes. The road to this algorithm goes through three stages: normal natural deduction, Gentzen's sequent calculus and, finally, a contraction-free variant of the sequent calculus proposed by Roy Dyckhoff, whose rules guarantee that the search terminates. At the end, we present a complete implementation of this algorithm in Haskell.

1.4.1. Why natural deduction is not an algorithm

Natural deduction does not, by itself, constitute an algorithm for building proofs. Put simply, the problem lies in the nondeterministic choice of which rule to apply next.

To understand the source of this nondeterminism, let us recall Per Martin-Löf's view of the meaning of propositions:

The meaning of a proposition is determined by [...] what counts as a

verification of it.

In terms of inference rules, this means that the meaning of a connective is determined by its introduction rules. The rule \frac{\Gamma \vdash \varphi_1 \quad \Gamma \vdash \varphi_2}{\Gamma \vdash \varphi_1 \land \varphi_2}

says exactly what is needed to verify a conjunction. If meaning is given by the introductions, the elimination rules must be justified in terms of them — they merely "undo" what an introduction has built. This tension between introduction and elimination rules is the source of nondeterminism: when building a proof bottom-up, when should we stop using introduction rules and start using elimination rules (or vice versa)?

Consider the task of deducing a formula \varphi from a context containing \varphi' \supset \varphi. It is natural to try to use this hypothesis via (\supset E), which yields the new goal \varphi'. But what happens if \varphi' has \varphi as a subformula? The process would likely try to prove \varphi again, leading to an endless loop.

As an example, take \{(B \supset A) \supset A\} \vdash A. The only hypothesis has conclusion A, so we try to use it, which requires B \supset A. By (\supset I), this requires A (now with B also in the context). And to obtain A, we use the hypothesis once again. Writing \mathcal{D} for the attempt to derive B \supset A, the search produces \mathcal{D} \;=\; \dfrac{\dfrac{\dfrac{}{(B \supset A) \supset A}\,\textsf{hyp} \quad \begin{array}{c}\vdots \\ B \supset A\end{array}}{A}\,{\supset}E}{B \supset A}\,{\supset}I

and the original goal becomes \dfrac{\dfrac{}{(B \supset A) \supset A}\,\textsf{hyp} \quad \begin{array}{c}\mathcal{D} \\ B \supset A\end{array}}{A}\,{\supset}E

Note that the branch marked with \vdots inside \mathcal{D} once again asks for a derivation of B \supset A, that is, a new copy of \mathcal{D}, and so on.

A naive search falls into an infinite loop. To avoid situations like this, we will restrict the shape of deductions so that they contain no redundancies. The resulting system is called normal natural deduction.

1.4.2. Normal natural deduction

The idea is to split the judgments of natural deduction into two forms:

  • \varphi\,\uparrow: \varphi has a deduction (it is verified by introduction rules, read bottom-up);

  • \varphi\,\downarrow: \varphi can be used in a deduction (it is extracted from hypotheses by elimination rules, read top-down).

The context consists only of formulas that can be used directly; we write \Gamma^{\downarrow} = \{\varphi_1\downarrow, \ldots, \varphi_n\downarrow\}.

Introduction rules produce \uparrow judgments and elimination rules produce \downarrow judgments, always from \downarrow major premises. For conjunction: \frac{\Gamma^{\downarrow} \vdash \varphi_1\uparrow \quad \Gamma^{\downarrow} \vdash \varphi_2\uparrow}{\Gamma^{\downarrow} \vdash \varphi_1 \land \varphi_2\uparrow} \qquad \frac{\Gamma^{\downarrow} \vdash \varphi_1 \land \varphi_2\downarrow}{\Gamma^{\downarrow} \vdash \varphi_1\downarrow} \qquad \frac{\Gamma^{\downarrow} \vdash \varphi_1 \land \varphi_2\downarrow}{\Gamma^{\downarrow} \vdash \varphi_2\downarrow}

The introduction rule reads: "if we have deductions of \varphi_1 and of \varphi_2, then we can deduce \varphi_1 \land \varphi_2". The elimination rules read: "if we can use \varphi_1 \land \varphi_2, then we can use \varphi_1" (and similarly for \varphi_2). For implication, the introduced hypothesis is added to the context as a usable formula, and the argument of modus ponens must be verified: \frac{\Gamma^{\downarrow}, \varphi_1\downarrow \vdash \varphi_2\uparrow}{\Gamma^{\downarrow} \vdash \varphi_1 \supset \varphi_2\uparrow} \qquad \frac{\Gamma^{\downarrow} \vdash \varphi_1 \supset \varphi_2\downarrow \quad \Gamma^{\downarrow} \vdash \varphi_1\uparrow}{\Gamma^{\downarrow} \vdash \varphi_2\downarrow}

For disjunction, the elimination produces a verification, since the conclusion \varphi is arbitrary: \frac{\Gamma^{\downarrow} \vdash \varphi_1\uparrow}{\Gamma^{\downarrow} \vdash \varphi_1 \lor \varphi_2\uparrow} \qquad \frac{\Gamma^{\downarrow} \vdash \varphi_2\uparrow}{\Gamma^{\downarrow} \vdash \varphi_1 \lor \varphi_2\uparrow} \qquad \frac{\Gamma^{\downarrow} \vdash \varphi_1 \lor \varphi_2\downarrow \quad \Gamma^{\downarrow}, \varphi_1\downarrow \vdash \varphi\uparrow \quad \Gamma^{\downarrow}, \varphi_2\downarrow \vdash \varphi\uparrow}{\Gamma^{\downarrow} \vdash \varphi\uparrow}

For the constants, we have the introduction of \top (with the side effect that there is no way to use \top as a hypothesis) and the elimination of \bot (with the side effect that there is no way to introduce \bot directly): \frac{}{\Gamma^{\downarrow} \vdash \top\uparrow} \qquad \frac{\Gamma^{\downarrow} \vdash \bot\downarrow}{\Gamma^{\downarrow} \vdash \varphi\uparrow}

Hypotheses can be used directly: \frac{}{\Gamma'^{\downarrow}, \varphi\downarrow, \Gamma^{\downarrow} \vdash \varphi\downarrow}

Only one piece is missing: how do we switch from elimination mode (\downarrow) to introduction mode (\uparrow)? For this we use the mode-switch rule: \frac{\Gamma^{\downarrow} \vdash \varphi\downarrow}{\Gamma^{\downarrow} \vdash \varphi\uparrow}

Note that there is no rule in the opposite direction. This is what prevents redundancies: a formula obtained by introduction (\uparrow) can never be used as the major premise of an elimination (which requires \downarrow).

1.4.2.1. Example

Let us deduce \{A \supset B \land C\downarrow\} \vdash (A \supset B) \land (A \supset C)\uparrow. The goal is a verified conjunction, so we apply the introduction rule, producing the goals A \supset B\uparrow and A \supset C\uparrow. Each is an implication, and by introduction we add A\downarrow to the context. Now we need B\uparrow; by the mode switch it suffices to have B\downarrow, which we obtain by projecting B \land C\downarrow, which in turn is obtained by applying the hypothesis A \supset B \land C\downarrow to A\uparrow — and the latter comes from A\downarrow via the mode switch. The branch for C is analogous.

To make the tree fit on the page, we present it in parts, omitting the contexts; next to each line we indicate the rule used, with \textsf{hyp} for the use of a hypothesis and {\downarrow\uparrow} for the mode switch. First, the derivation \mathcal{D}_B of B\uparrow from the hypotheses: \mathcal{D}_B \;=\; \dfrac{\dfrac{\dfrac{\dfrac{}{A \supset B \land C\downarrow}\,\textsf{hyp} \quad \dfrac{\dfrac{}{A\downarrow}\,\textsf{hyp}}{A\uparrow}\,{\downarrow\uparrow}}{B \land C\downarrow}\,{\supset}E}{B\downarrow}\,{\land}E_1}{B\uparrow}\,{\downarrow\uparrow}

The derivation \mathcal{D}_C of C\uparrow is identical, replacing the projection {\land}E_1 with {\land}E_2. With these, the complete derivation ends with two introductions of implication and one introduction of conjunction: \dfrac{\dfrac{\begin{array}{c}\mathcal{D}_B \\ B\uparrow\end{array}}{A \supset B\uparrow}\,{\supset}I \qquad \dfrac{\begin{array}{c}\mathcal{D}_C \\ C\uparrow\end{array}}{A \supset C\uparrow}\,{\supset}I}{(A \supset B) \land (A \supset C)\uparrow}\,{\land}I

Observe the structure of the derivation: in the lower part, only introduction rules, read bottom-up; in the upper part, only elimination rules, read top-down from the hypotheses; and the two parts meet at the applications of the mode-switch rule.

The system presented does not allow the construction of deductions containing redundancies. We therefore say that its deductions are normal, in the sense that they cannot be reduced. Normal natural deduction explicitly marks, through the mode-switch rule, the moment at which the proof stops using introduction rules and starts using elimination rules.

Even so, proof search in this system is awkward: derivations grow in two directions that must meet in the middle. Next we present a system similar to normal natural deduction, but in which the manipulation of the context is explicit and derivations grow in a single direction: the sequent calculus.

1.4.3. Sequent calculus

The sequent calculus is a system for building proofs developed by Gentzen in

  1. Our goal is to use it as an intermediate formalism for a proof construction algorithm. Unlike natural deduction, the sequent calculus allows the direct manipulation of sequents as a whole, and the context of hypotheses no longer plays a secondary (implicit) role in deductions:

  • the elimination rules now act directly on the context of hypotheses, and are called left rules;

  • the introduction rules act only on the conclusion of the sequent, and are called right rules.

Moreover, there is no change of direction in deductions: whereas in normal natural deduction introductions grow bottom-up and eliminations top-down, in the sequent calculus all rules are read bottom-up, decomposing the goal sequent into simpler sequents. Sequents are written \Gamma \Longrightarrow \varphi.

1.4.3.1. Rules

The initial sequent corresponds to the use of hypotheses: \frac{}{\Gamma, \varphi \Longrightarrow \varphi}\,(\textrm{Init})

For conjunction, the right rule is the same as in natural deduction; the left rules, instead of projecting an already deduced conjunction, add to the context one of the components of a conjunction present in it: \frac{\Gamma \Longrightarrow \varphi_1 \quad \Gamma \Longrightarrow \varphi_2}{\Gamma \Longrightarrow \varphi_1 \land \varphi_2}\,(\land R) \qquad \frac{\Gamma, \varphi_1 \land \varphi_2, \varphi_1 \Longrightarrow \varphi}{\Gamma, \varphi_1 \land \varphi_2 \Longrightarrow \varphi}\,(\land L_1) \qquad \frac{\Gamma, \varphi_1 \land \varphi_2, \varphi_2 \Longrightarrow \varphi}{\Gamma, \varphi_1 \land \varphi_2 \Longrightarrow \varphi}\,(\land L_2)

For implication, the right rule is the usual introduction. The left rule says that, to use \varphi_1 \supset \varphi_2, we must prove \varphi_1, after which we may add \varphi_2 to the context: \frac{\Gamma, \varphi_1 \Longrightarrow \varphi_2}{\Gamma \Longrightarrow \varphi_1 \supset \varphi_2}\,(\supset R) \qquad \frac{\Gamma, \varphi_1 \supset \varphi_2 \Longrightarrow \varphi_1 \quad \Gamma, \varphi_1 \supset \varphi_2, \varphi_2 \Longrightarrow \varphi}{\Gamma, \varphi_1 \supset \varphi_2 \Longrightarrow \varphi}\,(\supset L)

For disjunction, the right rules choose a side, and the left rule is case analysis: \frac{\Gamma \Longrightarrow \varphi_1}{\Gamma \Longrightarrow \varphi_1 \lor \varphi_2}\,(\lor R_1) \qquad \frac{\Gamma \Longrightarrow \varphi_2}{\Gamma \Longrightarrow \varphi_1 \lor \varphi_2}\,(\lor R_2) \qquad \frac{\Gamma, \varphi_1 \lor \varphi_2, \varphi_1 \Longrightarrow \varphi \quad \Gamma, \varphi_1 \lor \varphi_2, \varphi_2 \Longrightarrow \varphi}{\Gamma, \varphi_1 \lor \varphi_2 \Longrightarrow \varphi}\,(\lor L)

Finally, for the constants: \frac{}{\Gamma \Longrightarrow \top}\,(\top R) \qquad \frac{\Gamma \Longrightarrow \bot}{\Gamma \Longrightarrow \varphi}\,(\bot)

Note that, in the left rules above, the decomposed formula remains in the context of the premises. This corresponds to an implicit application of the contraction rule: the hypothesis can be used again later. As we shall see, this is precisely what threatens the termination of the search.

1.4.3.2. Example

Let us prove \Longrightarrow A \lor B \supset B \lor A. Reading the rules bottom-up: by (\supset R), the goal becomes A \lor B \Longrightarrow B \lor A; by (\lor L), we split into two cases, one with A and the other with B in the context; in each case we choose the appropriate side of the disjunction and close with the initial sequent. The two branches are \mathcal{D}_1 \;=\; \dfrac{\dfrac{}{A \lor B, A \Longrightarrow A}\,(\textrm{Init})}{A \lor B, A \Longrightarrow B \lor A}\,(\lor R_2) \qquad \mathcal{D}_2 \;=\; \dfrac{\dfrac{}{A \lor B, B \Longrightarrow B}\,(\textrm{Init})}{A \lor B, B \Longrightarrow B \lor A}\,(\lor R_1)

The two cases are then combined by (\lor L) and, finally, (\supset R) completes the proof: \dfrac{\dfrac{\begin{array}{c}\mathcal{D}_1 \\ A \lor B, A \Longrightarrow B \lor A\end{array} \qquad \begin{array}{c}\mathcal{D}_2 \\ A \lor B, B \Longrightarrow B \lor A\end{array}}{A \lor B \Longrightarrow B \lor A}\,(\lor L)}{\Longrightarrow A \lor B \supset B \lor A}\,(\supset R)

Notice that, unlike natural deduction, we never need to "invent" a formula that does not appear in the sequent: every formula in the premises is a subformula of a formula in the conclusion. This subformula property is the key to automated search.

1.4.3.3. Equivalence with natural deduction

The sequent calculus is equivalent to natural deduction, that is, \Gamma \Longrightarrow \varphi is derivable if and only if \Gamma \vdash \varphi is derivable. The proof involves two steps:

  1. showing that natural deduction is equivalent to normal natural deduction (which is essentially the cut-elimination theorem seen in the previous chapter);

  2. showing that normal natural deduction is equivalent to the sequent calculus.

It is not the aim of this course to study the details of this proof, but it is important to keep in mind that this equivalence exists: it allows us to use the sequent calculus to decide provability in natural deduction.

1.4.3.4. Termination and well-founded orders

The importance of this equivalence comes from the fact that we will see a version of the sequent calculus that can be viewed as a proof construction algorithm for propositional logic. The key to this algorithm is guaranteeing its termination. To do so, we must ensure that the "recursive calls" — the premises of each rule — are made on arguments that are smaller according to some well-founded order.

An order relation is well-founded if it has no infinite descending chains x_n > x_{n-1} > x_{n-2} > \cdots

If every descending chain is finite, an algorithm whose recursive calls always decrease the argument according to this order cannot run forever. The usual order on the natural numbers is the simplest example; we will see below that we need a somewhat more sophisticated order, on multisets.

So far we have studied two formalisms for building proofs in propositional logic: natural deduction and the sequent calculus. But how do we determine whether a sequent is provable or not? To use the sequent calculus as a decision procedure, we have to modify it to guarantee termination. We represent the new version using the notation \Gamma \longrightarrow \varphi, and we treat the context as a multiset of formulas (a set in which repetitions matter). The idea is that each rule "decreases" the size of the sequent, measured by the number of connectives.

1.4.4.1. Contraction-free rules

The rules for conjunction, disjunction and right implication are the same as before. The left rules, however, now consume the decomposed formula, which no longer appears in the premises: \frac{\Gamma \longrightarrow \varphi_1 \quad \Gamma \longrightarrow \varphi_2}{\Gamma \longrightarrow \varphi_1 \land \varphi_2} \qquad \frac{\Gamma, \varphi_1, \varphi_2 \longrightarrow \varphi}{\Gamma, \varphi_1 \land \varphi_2 \longrightarrow \varphi}

\frac{\Gamma \longrightarrow \varphi_1}{\Gamma \longrightarrow \varphi_1 \lor \varphi_2} \qquad \frac{\Gamma \longrightarrow \varphi_2}{\Gamma \longrightarrow \varphi_1 \lor \varphi_2} \qquad \frac{\Gamma, \varphi_1 \longrightarrow \varphi \quad \Gamma, \varphi_2 \longrightarrow \varphi}{\Gamma, \varphi_1 \lor \varphi_2 \longrightarrow \varphi}

\frac{\Gamma, \varphi_1 \longrightarrow \varphi_2}{\Gamma \longrightarrow \varphi_1 \supset \varphi_2}

For the constants, \top can be proved directly and can be removed from the context; \bot in the context closes the proof: \frac{}{\Gamma \longrightarrow \top} \qquad \frac{\Gamma \longrightarrow \varphi}{\Gamma, \top \longrightarrow \varphi} \qquad \frac{}{\Gamma, \bot \longrightarrow \varphi}

Finally, the hypothesis rule now applies only to variables; compound hypotheses will be decomposed by the other rules: \frac{}{\Gamma, A \longrightarrow A}

In all of these rules, the premises are strictly smaller than the conclusion. As a result, the proof construction process is guaranteed to terminate: it is not possible to keep producing ever smaller sequents indefinitely.

1.4.4.2. The problem with left implication

But what about the left rule for implication? The obvious contraction-free version would be \frac{\Gamma, \varphi_1 \supset \varphi_2 \longrightarrow \varphi_1 \quad \Gamma, \varphi_2 \longrightarrow \varphi}{\Gamma, \varphi_1 \supset \varphi_2 \longrightarrow \varphi}

and note that the first premise is not smaller than the conclusion: the hypothesis \varphi_1 \supset \varphi_2 must remain available (recall the example (B \supset A) \supset A, in which the hypothesis was used twice), and the goal \varphi_1 may be as large as \varphi. How can this problem be solved?

Dyckhoff's solution is to refine the rule according to the shape of the antecedent \varphi_1 of the implication. For each possible shape, a new rule is defined whose premises are smaller than the conclusion.

True antecedent. Since \top is always provable, \top \supset \varphi_2 is equivalent to \varphi_2: \frac{\Gamma, \varphi_2 \longrightarrow \varphi}{\Gamma, \top \supset \varphi_2 \longrightarrow \varphi}

Disjunctive antecedent. Using (\varphi_1 \lor \varphi_2) \supset \varphi_3 is equivalent to using the two implications separately: \frac{\Gamma, \varphi_1 \supset \varphi_3, \varphi_2 \supset \varphi_3 \longrightarrow \varphi}{\Gamma, (\varphi_1 \lor \varphi_2) \supset \varphi_3 \longrightarrow \varphi}

But is the premise really smaller than the conclusion? The formula \varphi_3 has been duplicated! The answer is yes, provided we consider an order on multisets: we are replacing one formula, (\varphi_1 \lor \varphi_2) \supset \varphi_3, with two strictly smaller formulas, \varphi_1 \supset \varphi_3 and \varphi_2 \supset \varphi_3. In the (Dershowitz-Manna) multiset order, replacing an element with any finite number of smaller elements always yields a smaller multiset, and this order is well-founded whenever the order on the elements is.

Conjunctive antecedent. Using (\varphi_1 \land \varphi_2) \supset \varphi_3 is equivalent to using its "curried" version: \frac{\Gamma, \varphi_1 \supset (\varphi_2 \supset \varphi_3) \longrightarrow \varphi}{\Gamma, (\varphi_1 \land \varphi_2) \supset \varphi_3 \longrightarrow \varphi}

Here the number of connectives is the same on both sides. To guarantee the decrease, it suffices to give conjunction weight 2 and the other connectives weight 1: the conclusion loses a \land (weight 2) and gains a \supset (weight 1).

Variable antecedent. If the variable A is in the context, we can apply modus ponens and replace A \supset \varphi_2 with \varphi_2: \frac{A \in \Gamma \quad \Gamma, \varphi_2 \longrightarrow \varphi}{\Gamma, A \supset \varphi_2 \longrightarrow \varphi}

Implicational antecedent. This is the critical case: \frac{\Gamma, \varphi_2 \supset \varphi_3, \varphi_1 \longrightarrow \varphi_2 \quad \Gamma, \varphi_3 \longrightarrow \varphi}{\Gamma, (\varphi_1 \supset \varphi_2) \supset \varphi_3 \longrightarrow \varphi}

To use (\varphi_1 \supset \varphi_2) \supset \varphi_3, we need to prove \varphi_1 \supset \varphi_2, that is, to prove \varphi_2 assuming \varphi_1. During this subproof, we might still need the original hypothesis — but Dyckhoff shows that the weaker version \varphi_2 \supset \varphi_3, which is smaller, suffices. The second premise uses \varphi_3, which is also smaller than the original hypothesis.

No rule is needed for \bot \supset \varphi_2: this hypothesis is trivially true and can simply be discarded (the implementation below includes this simplification, called L0).

This system, known as G4ip or the LJT calculus, is sound and complete with respect to intuitionistic propositional logic: a sequent is derivable in it if and only if it is derivable in the original sequent calculus and, therefore, in natural deduction. Since every rule decreases the sequent according to a well-founded order, exhaustive bottom-up search always terminates. The calculus can thus be understood as the specification of an algorithm that decides whether \Gamma \vdash \varphi.

1.4.5. Implementation in Haskell

The course repository contains a complete implementation of the algorithm in Haskell, in the directory haskell/src/ProofSearch, organized into the following modules:

  • Syntax/Formula.hs: types for formulas, sequents, rule names and proof trees;

  • Rules.hs: one function for each rule of the calculus;

  • Search.hs: the search algorithm;

  • Parser/SequentParser.hs and Pretty/ProofTree.hs: parsing and pretty-printing;

  • ProofSearch.hs: an interactive REPL, run with cabal run proof-search from the haskell/ directory.

1.4.5.1. Representation

Propositional formulas are represented by an algebraic type that follows the grammar directly:

data Formula
  = Atom String
  | Top
  | Bot
  | And Formula Formula
  | Or Formula Formula
  | Imp Formula Formula
  deriving (Eq, Ord)

A sequent consists of the context (antecedent), represented by a list, and the goal (succedent):

data Sequent = Sequent
  { antecedent :: [Formula],
    succedent :: Formula
  }

A proof tree records the rule applied, the sequent concluded and the subproofs of the premises — exactly the structure of a derivation:

data ProofTree = ProofTree
  { ptRule :: RuleName,
    ptConclusion :: Sequent,
    ptPremises :: [ProofTree]
  }

data RuleName
  = Ax | TopR | BotL
  | ImpR | AndR | AndL | OrL | OrR1 | OrR2
  | ImpLAtom   -- L1: P → Q with P ∈ Γ
  | ImpLTop    -- L4: ⊤ → C
  | ImpLBot    -- L0: ⊥ → C
  | ImpLAnd    -- L2: (A ∧ B) → C
  | ImpLOr     -- L3: (A ∨ B) → C
  | ImpLImp    -- L5: (A → B) → C

1.4.5.2. Rules as functions

Each rule is implemented as a function that takes a sequent and returns Nothing, if the rule is not applicable, or Just (rule, premises), where premises is the list of new sequents to be proved. The rules without premises (axiom, \top-R and \bot-L) return an empty list of subgoals:

tryAx :: Sequent -> Maybe (RuleName, [Sequent])
tryAx (Sequent gamma g@(Atom _))
  | g `elem` gamma = Just (Ax, [])
tryAx _ = Nothing

tryTopR :: Sequent -> Maybe (RuleName, [Sequent])
tryTopR (Sequent _ Top) = Just (TopR, [])
tryTopR _ = Nothing

tryBotL :: Sequent -> Maybe (RuleName, [Sequent])
tryBotL (Sequent gamma _)
  | Bot `elem` gamma = Just (BotL, [])
  | otherwise = Nothing

Note that tryAx applies only when the goal is atomic, as in the hypothesis rule of the contraction-free calculus. The right rules merely inspect the goal:

tryImpR :: Sequent -> Maybe (RuleName, [Sequent])
tryImpR (Sequent gamma (Imp a b)) =
  Just (ImpR, [Sequent (a : gamma) b])
tryImpR _ = Nothing

tryAndR :: Sequent -> Maybe (RuleName, [Sequent])
tryAndR (Sequent gamma (And a b)) =
  Just (AndR, [Sequent gamma a, Sequent gamma b])
tryAndR _ = Nothing

The left rules search the context for a formula of the appropriate shape, using a list comprehension that enumerates the positions of the context. Rule L1, for example, looks for an implication P → Q with P atomic and present in the context, and replaces it with Q — without contraction, the original implication disappears:

tryImpLAtom :: Sequent -> Maybe (RuleName, [Sequent])
tryImpLAtom (Sequent gamma g) =
  case [ (q, i) | (i, Imp p@(Atom _) q) <- zip [0 ..] gamma
                , p `elem` gamma ] of
    [] -> Nothing
    ((q, i) : _) ->
      let gamma' = replaceAt i q gamma
       in Just (ImpLAtom, [Sequent gamma' g])

Rules L2, L3, L4 and L0 follow the same pattern. Rule L3, for example, replaces (A ∨ B) → C with the two implications A → C and B → C:

tryImpLOr :: Sequent -> Maybe (RuleName, [Sequent])
tryImpLOr (Sequent gamma g) =
  case [ (a, b, c, i) | (i, Imp (Or a b) c) <- zip [0 ..] gamma ] of
    [] -> Nothing
    ((a, b, c, i) : _) ->
      let gamma' = replaceAtWith i [Imp a c, Imp b c] gamma
       in Just (ImpLOr, [Sequent gamma' g])

Rule L5 is handled differently: since it is not invertible (applying it to the wrong hypothesis may lead to a dead end, even if the sequent is provable), the implementation generates all of its possible applications, one for each implication (A → B) → C in the context:

impLImpCandidates :: Sequent -> [(RuleName, [Sequent])]
impLImpCandidates (Sequent gamma g) =
  [ (ImpLImp, [prem1, prem2])
  | (i, Imp (Imp a b) c) <- zip [0 ..] gamma,
    let base = removeAt i gamma
        prem1 = Sequent (Imp b c : base) (Imp a b)
        prem2 = Sequent (c : base) g
  ]

The first premise is written as Γ, B → C ⊢ A → B, which is Dyckhoff's original form; it is equivalent to the one presented earlier (\Gamma, \varphi_2 \supset \varphi_3, \varphi_1 \longrightarrow \varphi_2), because the search will apply \supset-R next, moving A into the context.

1.4.5.3. The search algorithm

With the rules in hand, the search algorithm is surprisingly short. The function candidates lists all possible rule applications for a sequent, in order of priority: first the invertible rules (whose application never turns a provable sequent into an unprovable one, and which therefore require no backtracking), then the non-invertible ones: \lor-R₁, \lor-R₂ and L5.

candidates :: Sequent -> [(RuleName, [Sequent])]
candidates s =
  concat
    [ maybeToList (tryAx s),
      maybeToList (tryTopR s),
      maybeToList (tryBotL s),
      maybeToList (tryImpR s),
      maybeToList (tryAndR s),
      maybeToList (tryAndL s),
      maybeToList (tryOrL s),
      maybeToList (tryImpLBot s),
      maybeToList (tryImpLTop s),
      maybeToList (tryImpLAnd s),
      maybeToList (tryImpLOr s),
      maybeToList (tryImpLAtom s),
      maybeToList (tryOrR1 s),
      maybeToList (tryOrR2 s),
      impLImpCandidates s
    ]

The search tries each candidate in order and returns the first one that succeeds. Trying a candidate means recursively searching for proofs of all of its subgoals; this is done with mapM in the Maybe monad, which fails as soon as any subgoal fails:

search :: Sequent -> Maybe ProofTree
search s = firstSuccess (map (attempt s) (candidates s))

firstSuccess :: [Maybe a] -> Maybe a
firstSuccess [] = Nothing
firstSuccess (Just x : _) = Just x
firstSuccess (Nothing : xs) = firstSuccess xs

attempt :: Sequent -> (RuleName, [Sequent]) -> Maybe ProofTree
attempt s (rn, subgoals) = do
  subproofs <- mapM search subgoals
  return $ ProofTree rn s subproofs

Thanks to Haskell's lazy evaluation, map (attempt s) (candidates s) does not evaluate every candidate: firstSuccess stops at the first Just. We thus get, for free, a depth-first search with backtracking. The termination of search is not evident from the structure of the code (the recursive calls are not on subterms of the argument), but it follows from Dyckhoff's argument: each subgoal is strictly smaller than the original sequent according to the weighted multiset order discussed above. This is a typical example of well-founded recursion, which we will study in Lean later in the course.

1.4.5.4. Sample runs

The REPL reads a sequent, runs the search and prints the proof tree it found, with the conclusion of each rule and its premises indented below it. The proof of the identity uses \supset-R and the axiom:

proof> |- P -> P
✓  Proof found:

[→-R] ⊢ P → P
└── [Ax] P ⊢ P
   (2 rule applications)

Modus ponens is handled by rule L1, which replaces P → Q with Q (since P ∈ Γ), followed by the axiom:

proof> P -> Q, P |- Q
✓  Proof found:

[L1 (P→Q, P∈Γ)] P → Q, P ⊢ Q
└── [Ax] Q, P ⊢ Q
   (2 rule applications)

For the S combinator, (P \supset Q \supset R) \supset (P \supset Q) \supset P \supset R, the search introduces the three hypotheses and applies L1 repeatedly:

proof> |- (P -> Q -> R) -> (P -> Q) -> P -> R
✓  Proof found:

[→-R] ⊢ (P → Q → R) → (P → Q) → P → R
└── [→-R] P → Q → R ⊢ (P → Q) → P → R
    └── [→-R] P → Q, P → Q → R ⊢ P → R
        └── [L1 (P→Q, P∈Γ)] P, P → Q, P → Q → R ⊢ R
            └── [L1 (P→Q, P∈Γ)] P, Q, P → Q → R ⊢ R
                └── [L1 (P→Q, P∈Γ)] P, Q, Q → R ⊢ R
                    └── [Ax] P, Q, R ⊢ R
   (7 rule applications)

On the other hand, the law of excluded middle is not provable, and since the search terminates, the program is able to assert this:

proof> |- P \/ (P -> Bot)
✗  Not provable in intuitionistic propositional logic.

The same happens with the sequent (B \supset A) \supset A \vdash A, which sent the naive search in natural deduction into an infinite loop: in the contraction-free calculus the search terminates and reports that it is not provable. The double negation of the excluded middle, \neg\neg(P \lor \neg P), on the other hand, is provable, and its proof exercises rules L3 and L5:

proof> |- ((P \/ (P -> Bot)) -> Bot) -> Bot
✓  Proof found:

[→-R] ⊢ ((P ∨ (P → ⊥)) → ⊥) → ⊥
└── [L3 ((A∨B)→C)] (P ∨ (P → ⊥)) → ⊥ ⊢ ⊥
    └── [L5 ((A→B)→C)] P → ⊥, (P → ⊥) → ⊥ ⊢ ⊥
        ├── [→-R] ⊥ → ⊥, P → ⊥ ⊢ P → ⊥
        │   └── [L0 (⊥→C, drop)] P, ⊥ → ⊥, P → ⊥ ⊢ ⊥
        │       └── [L1 (P→Q, P∈Γ)] P, P → ⊥ ⊢ ⊥
        │           └── [⊥-L] P, ⊥ ⊢ ⊥
        └── [⊥-L] ⊥, P → ⊥ ⊢ ⊥

1.4.5.5. Proof search in Lean

Decision procedures like this one are part of proof assistants. Lean's Mathlib library provides the itauto tactic, a decision procedure for intuitionistic propositional logic whose implementation is based on Dyckhoff's G4ip calculus. It automatically finds the proofs of the S combinator and of the double negation of the excluded middle:

namespace Ch03 example (P Q R : Prop) : (P → Q → R) → (P → Q) → P → R := P:PropQ:PropR:Prop⊢ (P → Q → R) → (P → Q) → P → R All goals completed! 🐙 example (P : Prop) : ¬¬(P ∨ ¬P) := P:Prop⊢ ¬¬(P ∨ ¬P) All goals completed! 🐙

For the excluded middle, the tactic fails and displays one of the states in which the search ran out of applicable rules (here, after choosing the right side of the disjunction and introducing the hypothesis P, it remains to prove False):

example (P : Prop) : P ∨ ¬P := P:Prop⊢ P ∨ ¬P itauto failed P:Proph0:P⊢ FalseAll goals completed! 🐙
itauto failed
P:Proph0:P⊢ False
end Ch03

Note that the tactic does not merely answer "yes" or "no": when it succeeds, it builds a proof term that is checked by Lean's kernel, exactly like the proof trees produced by the Haskell implementation.

1.4.5.6. References

  • MARTIN-LÖF, Per. On the meaning of the logical constants and the justifications of the logical laws. Nordic Journal of Philosophical Logic, 1(1):11–60, 1996.

  • GENTZEN, Gerhard. Untersuchungen über das logische Schließen. Mathematische Zeitschrift, 39:176–210, 405–431, 1935. English translation in M. E. Szabo (ed.), The Collected Papers of Gerhard Gentzen, pp. 68–131, North-Holland, 1969.

  • DYCKHOFF, Roy. Contraction-free sequent calculi for intuitionistic logic. Journal of Symbolic Logic, 57:795–807, 1992.

1.4.6. Exercises

  1. Build a normal natural deduction derivation of \{A \land B\downarrow\} \vdash B \land A\uparrow and of \{A \supset B\downarrow, B \supset C\downarrow\} \vdash A \supset C\uparrow.

  2. Build sequent calculus derivations (with \Longrightarrow) for: (a) \Longrightarrow A \land B \supset B \land A; (b) A \supset B, B \supset C \Longrightarrow A \supset C; (c) \Longrightarrow (A \lor B) \supset (A \supset C) \supset (B \supset C) \supset C.

  3. Show that rule L2 decreases the weight of the sequent when conjunction has weight 2 and the other connectives weight 1. What happens if all connectives have weight 1?

  4. Simulate the search algorithm by hand on the sequent P -> Q, Q -> R |- P -> R, indicating the rule chosen at each step.

  5. Explain why \lor-R₁ is not invertible, by exhibiting a provable sequent for which applying it produces an unprovable sequent.

  6. Extend the Haskell implementation with a biconditional connective Iff Formula Formula, treating it as an abbreviation for And (Imp a b) (Imp b a) before the search.

  7. The implementation returns only the first proof found. Modify search to return the list of all proofs, using the list monad instead of Maybe.

  8. Use Lean's itauto tactic to check which of the formulas below are intuitionistically provable: (a) ((A \supset B) \supset A) \supset A (Peirce's law); (b) \neg\neg\neg A \supset \neg A; (c) (\neg A \lor B) \supset (A \supset B); (d) (A \supset B) \supset (\neg A \lor B).