1.1. Introduction
This chapter motivates the course Logic Applied to Computer Science. The question that will guide the whole course is easy to state and hard to answer: how can we be sure that a program does what we claim it does? We begin by discussing the most common approach to this question — software testing — and its limits. We then see that the same dilemma arises in Mathematics, discuss the role (and the limitations) of large language models (LLMs) in building logical arguments, and finally introduce the idea of proof assistants and the Lean language, which will be our tool throughout the semester.
1.1.1. Software testing
The standard approach to software quality assurance is testing. We write a program, choose a set of inputs, run the program on those inputs and compare the results we get with the results we expect. Tests are extremely useful: they easily catch simple mistakes, they are cheap to write, and they can be automated and run after every change to the code.
Moreover, a large number of tests — whether written by hand or generated automatically, as in property-based testing tools — can exercise most of a program's behaviour. In practice, a well-designed test suite gives the programmer considerable confidence that the code behaves as expected.
There is, however, a fundamental limitation, pointed out by Edsger Dijkstra in one of his best-known remarks:
Program testing can be used to show the presence of bugs, but never to show
their absence.
The reason is simple: a typical program has an infinite (or astronomically large) set of possible inputs, and any test suite examines only finitely many of them. If a test fails, we know for certain that there is a bug; if every test passes, we only know that the program works for those inputs.
1.1.1.1. Testing in Mathematics
Testing is not exclusive to Computer Science. Mathematicians often run experiments — numerical computations, checking small cases — to validate their intuition before attempting to prove a result. A famous example shows how misleading this strategy can be.
Suppose a mathematician discovers, using numerical methods, that
\int_{0}^{\infty}\frac{\sin(t)}{t}\,\mathrm{d}t = \frac{\pi}{2}.
After some experimentation, she observes that
\int_{0}^{\infty}\frac{\sin(t)}{t}\cdot\frac{\sin(\frac{t}{101})}{\frac{t}{101}}\,\mathrm{d}t = \frac{\pi}{2}
and the next test confirms that
\int_{0}^{\infty}\frac{\sin(t)}{t}\cdot\frac{\sin(\frac{t}{201})}{\frac{t}{201}}\,\mathrm{d}t = \frac{\pi}{2}.
Feeling confident, she conjectures that, for every natural number n,
\int_{0}^{\infty}\frac{\sin(t)}{t}\cdot\frac{\sin(\frac{t}{100n + 1})}{\frac{t}{100n + 1}}\,\mathrm{d}t = \frac{\pi}{2}.
That confidence is soon shattered: for n = 15 the equality no longer
holds.Integrals of this kind are known as Borwein integrals and are a
classic example of a pattern that persists for many cases and then breaks
down. No finite number of successful tests for n = 1, 2, \ldots, 14 would
suffice to establish the general claim — and, indeed, the claim is false.
In Mathematics, then, tests are used to aid our understanding of problems and to formulate plausible conjectures. To be certain that a property of interest holds, mathematicians use proofs: arguments that, starting from accepted premises, establish the conclusion for all cases at once.
1.1.2. Testing and LLMs
A natural question nowadays is whether large language models (LLMs) can help us build such proofs. After all, LLMs produce text that looks very much like mathematical proofs and like correct code.
The problem is that LLMs have quite well-known examples of colossal failures in logical reasoning. Consider the following claim:
The number of factors (divisors) of
n!is2^{n-1}.
A "proof" of this claim produced by an LLM (Grok) circulated on the internet,
with every appearance of a rigorous mathematical argument: induction, algebraic
manipulation and a confident conclusion. The property, however, is false: for
n = 6 we have 6! = 720 = 2^4 \cdot 3^2 \cdot 5, whose number of divisors
is (4+1)(2+1)(1+1) = 30, whereas 2^{5} = 32.
There are many other cases of blatant failures of LLMs in logical reasoning. The lesson is not that LLMs are useless — they can be great assistants for suggesting ideas, strategies and even fragments of proofs — but rather that a text that looks like a proof is not, by itself, a proof. We need some mechanical and reliable way of checking arguments.
1.1.3. Program correctness
Just as in Mathematics, if we want to guarantee that a program has a property of interest, we must use proofs and not merely tests. We would like to state, for example, that "for every input list, the sorting function produces a sorted permutation of the input" and then prove that statement.
This immediately raises a new question: who guarantees that the proofs are correct? Hand-written proofs about real programs tend to be long, tedious and full of cases; it is very easy to forget a case or make a small mistake. In recent years, a growing body of work — in both Mathematics and Computer Science — has relied on so-called proof assistants to certify the validity of proofs.
1.1.4. Proof assistants
Proof assistants are programming languages that reduce the problem of checking proofs to the task of type checking a program. The central idea, which we will study in detail throughout the course under the name Curry-Howard isomorphism, is the following:
-
properties (propositions) are expressed as types;
-
programs that have those types correspond to proofs of those properties.
Thus, checking that a proof is correct amounts to checking that a program is well typed — something a compiler does automatically and reliably. The kernel responsible for this check is small and carefully designed, so that trust in the result depends only on trusting that kernel, and not on each individual proof.
1.1.5. The Lean language
In this course we will use the Lean language, which can be understood simultaneously as a functional programming language and as a proof assistant. Lean has a very expressive type system, capable of representing arbitrary logical properties. Hence we can specify programs simply by giving them a type that represents their correctness property. The Lean compiler can then check whether the implementation of the program meets its specification.
1.1.5.1. An example: the predecessor
Consider a function that returns the predecessor of a natural number:
namespace Ch00
def pred (n : ℕ) : ℕ :=
match n with
| .zero => 0
| .succ n' => n'
#eval pred 5
The type ℕ → ℕ of this function could easily be expressed in functional
languages such as Haskell or ML. However, this type cannot guarantee that the
function pred really implements the predecessor of natural numbers: the
constant function that always returns zero has exactly the same type!
Expressed in logic, the property we expect from the predecessor is:
\forall n.\, n \in \mathbb{N} \to (n \neq 0 \to \exists m.\, n = m + 1) \lor n = 0
that is, either n is zero, or there exists an m such that n = m + 1. In
Lean, we can write this property directly as the return type of the function.
The type {m : ℕ // n = m + 1} represents the naturals m together with a
proof that n = m + 1, and the operator ⊕' represents a sum (a "disjunction"
that carries data):
def predCorrect (n : ℕ) : {m : ℕ // n = m + 1} ⊕' (n = 0) :=
match n with
| 0 => PSum.inr rfl
| n' + 1 => PSum.inl ⟨n', rfl⟩
With this type, the Lean compiler guarantees that the implementation of
predCorrect satisfies the property stated above. If we try, for example,
to return a wrong value in the second case, the program simply does not compile,
because we will not be able to supply the required proof:
def predWrong (n : ℕ) : {m : ℕ // n = m + 1} ⊕' (n = 0) :=
match n with
| 0 => PSum.inr rfl
| n' + 1 => PSum.inl ⟨n' + 1, rfl⟩
Notice the change of perspective: the program's specification is no longer a comment or a set of tests, but has become part of the program's type. The type checker has become a proof checker.
end Ch00
1.1.6. Applications
Proof assistants have been used successfully to verify a variety of systems and non-trivial proofs. Among the best-known examples are:
-
Compilers: CompCert is a compiler for the C language whose correctness (preservation of the source program's semantics in the generated code) was formally proved in the Coq/Rocq proof assistant.
-
Operating systems: the seL4 microkernel and the CertiKOS system have formal proofs of functional correctness and of isolation properties.
-
Mathematical proofs: the Four Color Theorem and the Feit-Thompson Theorem (from group theory) have been completely formalized.
-
Financial protocols: smart contracts and cryptocurrency protocols have been formally verified, given the high cost of errors in these systems.
1.1.7. Course outline
The course content can be divided into three major blocks:
-
Foundations: a review of logic and functional programming; notions of formal semantics; the untyped and simply typed
\lambda-calculus; the Calculus of Constructions. -
Lean: an introduction to the Lean language; induction and recursion; programming with dependent types.
-
Applications: notions of formal semantics in Lean; Hoare logic and separation logic in Lean.
The following chapters begin with the first block: we review functional programming in Haskell and then study propositional logic from the intuitionistic point of view, which is the logical foundation of proof assistants.
1.1.7.1. Bibliography
The main references for the course are:
-
CHRISTIANSEN, David T. Functional Programming in Lean, 2023. Available online at the official Lean website.
-
NEDERPELT, Rob; GEUVERS, Herman. Type Theory and Formal Proof. Cambridge University Press, 2014.
-
Relevant papers on the topics covered, cited throughout the notes.
1.1.8. Exercises
-
Give an example of a function on lists of integers and of a test suite that passes successfully but fails to detect a bug present in the function. What would be needed to guarantee the absence of that bug?
-
Compute the number of divisors of
n!forn = 1, \ldots, 6and check for which values the claim "the number of divisors ofn!is2^{n-1}" holds. Why are the first few cases not enough to conclude the general claim? -
Explain, in your own words, the difference between the guarantees given by the types of
Ch00.predand ofCh00.predCorrect. -
Write, in the style of
Ch00.predCorrect, the type of a function that takes two natural numbers and returns the larger one, together with proofs that the result is greater than or equal to each of the arguments. (You do not need to implement the function yet; we will learn how to do so in the coming chapters.) -
Research one of the projects mentioned in the applications section (CompCert, seL4, Four Color Theorem or Feit-Thompson) and describe, in one paragraph, which property was verified and which proof assistant was used.