Built with Alectryon, running Coq+SerAPI v8.19.0+0.19.3. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑ Ctrl+↓ to navigate, Ctrl+🖱️ to focus. On Mac, use instead of Ctrl.

Lecture 1: Hello World

We start by showing how to run Coq, introduce its three main sub-languages (Gallina, Vernacular, and LTac), and use it on simple examples.

At first glance, Coq can be viewed as a typed programming language, similar to OCaml or Haskell. For example, we can define the booleans in the usual way as a data type using the Inductive keyword. Terms like true and false are in a sub-language called "Gallina."

Inductive bool : Type :=
| true
| false.

Coq also provides commands like Check and Print, which are in a sub-language called "Vernacular", for checking the types of different terms, and for printing out their definition.

true : bool
Inductive bool : Set := true : bool | false : bool.
bool : Set
Inductive bool : Set := true : bool | false : bool.

We can also define simple functions using pattern matching. Note that every match must also have an end.

Definition negb (b:bool) : bool :=
  match b with
  | true => false
  | false => true
  end.

Definition andb (b1 b2:bool) : bool :=
  match b1 with
  | true => b2
  | false => false
  end. 

Definition orb (b1 b2:bool) : bool :=
  match b1 with
  | true => true
  | false => b2
  end.

In addition to simple programs, Coq also supports doing proofs. For example, we can prove a simple lemma about orb's behavior. Coq proofs are written using tactics such as reflexivity, which is in a sub-language called "LTac", that manipulate the proof state. A proof is completed when all goals have been shown.


orb true false = true

orb true false = true
reflexivity. Qed.
= false : bool

Most of the terms we have seen so far are computational objects which inhabit a universe called Set. Coq also has propositions which inhabit a universe called Prop. Hence, we can prove simple propositions. Below we show that A implies A. Note that implication is written as ->. We use the intros tactic to introduce the implication and the assumption tactic to discharge the proof goal of A.


forall A : Prop, A -> A

forall A : Prop, A -> A
A: Prop
H: A

A
assumption. Qed.

The type of an equality is a proposition, which may or may not be provable.

true = true : Prop

Of course, there are many propositions that we cannot prove, such as the following one, which is clearly bogus.

Definition bogus : Prop := true = false.


bogus

bogus

true = false
Abort.