ProofObjectsThe Curry-Howard Correspondence

"Algorithms are the computational content of proofs." (Robert Harper)
Programming and proving in Coq are two sides of the same coin. Proofs manipulate evidence, much as programs manipulate data.
Question: If evidence is data, what are propositions themselves?
Answer: They are types!

Look again at the formal definition of the ev property.
Inductive ev : natProp :=
  | ev_0 : ev 0
  | ev_SS (n : nat) (H : ev n) : ev (S (S n)).
Suppose we introduce an alternative pronunciation of ":". Instead of "has type," we can say "is a proof of." For example, the second line in the definition of ev declares that ev_0 : ev 0. Instead of "ev_0 has type ev 0," we can say that "ev_0 is a proof of ev 0."

This pun between types and propositions -- between : as "has type" and : as "is a proof of" or "is evidence for" -- is called the Curry-Howard correspondence. It proposes a deep connection between the world of logic and the world of computation:
                 propositions  ~  types
                 proofs        ~  programs
See [Wadler 2015] for a brief history and up-to-date exposition.

Many useful insights follow from this connection. To begin with, it gives us a natural interpretation of the type of the ev_SS constructor:
Check ev_SS
  : n,
    ev n
    ev (S (S n)).
This can be read "ev_SS is a constructor that takes two arguments -- a number n and evidence for the proposition ev n -- and yields evidence for the proposition ev (S (S n))."

Now let's look again at a previous proof involving ev.
Theorem ev_4 : ev 4.
Proof.
  apply ev_SS. apply ev_SS. apply ev_0. Qed.
As with ordinary data values and functions, we can use the Print command to see the proof object that results from this proof script.
Print ev_4.
(* ===> ev_4 = ev_SS 2 (ev_SS 0 ev_0)
      : ev 4  *)

Indeed, we can also write down this proof object directly, without the need for a separate proof script:
Check (ev_SS 2 (ev_SS 0 ev_0))
  : ev 4.
Similarly, as we've seen, we can directly apply theorems to arguments in proof scripts:
Theorem ev_4': ev 4.
Proof.
  apply (ev_SS 2 (ev_SS 0 ev_0)).
Qed.

Proof Scripts

When we write a proof using tactics, what we are doing is instructing Coq to build a proof object under the hood. We can see this using Show Proof:
Theorem ev_4'' : ev 4.
Proof.
  Show Proof.
  apply ev_SS.
  Show Proof.
  apply ev_SS.
  Show Proof.
  apply ev_0.
  Show Proof.
Qed.

Tactic proofs are convenient, but they are not essential in Coq: in principle, we can always just construct the required evidence by hand. Then we can use Definition (rather than Theorem) to introduce a global name for this evidence.
Definition ev_4''' : ev 4 :=
  ev_SS 2 (ev_SS 0 ev_0).

Quantifiers, Implications, Functions

In Coq's computational universe (where data structures and programs live), there are two sorts of values that have arrows in their types: constructors introduced by Inductively defined data types, and functions.
Similarly, in Coq's logical universe (where we carry out proofs), there are two ways of giving evidence for an implication: constructors introduced by Inductively defined propositions, and... functions!

For example, consider this statement:
Theorem ev_plus4 : n, ev nev (4 + n).
Proof.
  intros n H. simpl.
  apply ev_SS.
  apply ev_SS.
  apply H.
Qed.
What is the proof object corresponding to ev_plus4?

We're looking for an expression whose type is n, ev n ev (4 + n) -- that is, a function that takes two arguments (one number and a piece of evidence) and returns a piece of evidence!
Here it is:
Definition ev_plus4' : n, ev nev (4 + n) :=
  fun (n : nat) ⇒ fun (H : ev n) ⇒
    ev_SS (S (S n)) (ev_SS n H).
Or equivalently:
Definition ev_plus4'' (n : nat) (H : ev n)
                    : ev (4 + n) :=
  ev_SS (S (S n)) (ev_SS n H).

Check ev_plus4'' : n : nat, ev nev (4 + n).

When we view the proposition being proved by ev_plus4 as a function type, one interesting point becomes apparent: The second argument's type, ev n, mentions the value of the first argument, n.
While such dependent types are not found in most mainstream programming languages, they can be quite useful in programming too, as the flurry of activity in the functional programming community over the past couple of decades demonstrates.

Notice that both implication () and quantification () correspond to functions on evidence. In fact, they are really the same thing: is just a shorthand for a degenerate use of where there is no dependency, i.e., no need to give a name to the type on the left-hand side of the arrow:
            (x:nat), nat
        = (_:nat), nat
        = natnat
Recall the definition of ev:
       Inductive ev : natProp :=
         | ev_0 : ev 0
         | ev_SS : n, ev nev (S (S n)).
What is the type of this expression?
       fun (n : nat) ⇒
         fun (H : ev n) ⇒
            ev_SS (2 + n) (ev_SS n H)
(1) n, ev n
(2) n, ev (2 + n)
(3) n, ev n ev n
(4) n, ev n ev (2 + n)
(5) n, ev n ev (4 + n)
(6) Not typeable
Check (fun (n : nat) ⇒
         fun (H : ev n) ⇒
            ev_SS (2 + n) (ev_SS n H))
       : n : nat, ev nev (4 + n).

Programming with Tactics

If we can build proofs by giving explicit terms rather than executing tactic scripts, you may be wondering whether we can build programs using tactics rather than by writing down explicit terms.
Naturally, the answer is yes!
Definition add1 : natnat.
intro n.
Show Proof.
apply S.
Show Proof.
apply n. Defined.

Print add1.
(* ==>
    add1 = fun n : nat => S n
         : nat -> nat
*)


Compute add1 2.
(* ==> 3 : nat *)

Logical Connectives as Inductive Types

Inductive definitions are powerful enough to express most of the logical connectives we have seen so far. Indeed, only universal quantification (with implication as a special case) is built into Coq; all the others are defined inductively.
Let's see how.

Conjunction

To prove that P Q holds, we must present evidence for both P and Q. Thus, it makes sense to define a proof object for P Q to consist of a pair of two proofs: one for P and another one for Q. This leads to the following definition.
Inductive and (P Q : Prop) : Prop :=
  | conj : PQand P Q.

Arguments conj [P] [Q].

Notation "P /\ Q" := (and P Q) : type_scope.
Notice the similarity with the definition of the prod type, given in chapter Poly; the only difference is that prod takes Type arguments, whereas and takes Prop arguments.
Print prod.
(* ===>
   Inductive prod (X Y : Type) : Type :=
   | pair : X -> Y -> X * Y. *)

This similarity should clarify why destruct and intros patterns can be used on a conjunctive hypothesis. Case analysis allows us to consider all possible ways in which P Q was proved -- here just one (the conj constructor).
Theorem proj1' : P Q,
  PQP.
Proof.
  intros P Q HPQ. destruct HPQ as [HP HQ]. apply HP.
  Show Proof.
Qed.
Similarly, the split tactic actually works for any inductively defined proposition with exactly one constructor. In particular, it works for and:
Lemma and_comm : P Q : Prop, PQQP.
Proof.
  intros P Q. split.
  - intros [HP HQ]. split.
    + apply HQ.
    + apply HP.
  - intros [HQ HP]. split.
    + apply HP.
    + apply HQ.
Qed.

This shows why the inductive definition of and can be manipulated by tactics as we've been doing. We can also use it to build proofs directly, using pattern-matching. For instance:
Definition proj1'' P Q (HPQ : PQ) : P :=
  match HPQ with
  | conj HP HQHP
  end.

Definition and_comm'_aux P Q (H : PQ) : QP :=
  match H with
  | conj HP HQconj HQ HP
  end.

Definition and_comm' P Q : PQQP :=
  conj (and_comm'_aux P Q) (and_comm'_aux Q P).

What is the type of this expression?
        fun P Q R (H1: and P Q) (H2: and Q R) ⇒
          match (H1,H2) with
          | (conj HP _, conj _ HR) ⇒ conj HP HR
          end.
(1) P Q R, P Q Q R P R
(2) P Q R, Q P R Q P R
(3) P Q R, P R
(4) P Q R, P Q Q R P R
(5) Not typeable
Check
  (fun P Q R (H1: and P Q) (H2: and Q R) ⇒
    match (H1,H2) with
    | (conj HP _, conj _ HR) ⇒ conj HP HR
    end) : P Q R, PQQRPR.

Disjunction

The inductive definition of disjunction uses two constructors, one for each side of the disjunct:
Inductive or (P Q : Prop) : Prop :=
  | or_introl : Por P Q
  | or_intror : Qor P Q.

Arguments or_introl [P] [Q].
Arguments or_intror [P] [Q].

Notation "P \/ Q" := (or P Q) : type_scope.
This declaration explains the behavior of the destruct tactic on a disjunctive hypothesis, since the generated subgoals match the shape of the or_introl and or_intror constructors.

Once again, we can also directly write proof objects for theorems involving or, without resorting to tactics.
Definition inj_l : (P Q : Prop), PPQ :=
  fun P Q HPor_introl HP.

Theorem inj_l' : (P Q : Prop), PPQ.
Proof.
  intros P Q HP. left. apply HP.
  Show Proof.
Qed.

Definition or_elim : (P Q R : Prop), (PQ) → (PR) → (QR) → R :=
  fun P Q R HPQ HPR HQR
    match HPQ with
    | or_introl HPHPR HP
    | or_intror HQHQR HQ
    end.

Theorem or_elim' : (P Q R : Prop), (PQ) → (PR) → (QR) → R.
Proof.
  intros P Q R HPQ HPR HQR.
  destruct HPQ as [HP | HQ].
  - apply HPR. apply HP.
  - apply HQR. apply HQ.
Qed.


What is the type of this expression?
       fun P Q H
         match H with
         | or_introl HP ⇒ @or_intror Q P HP
         | or_intror HQ ⇒ @or_introl Q P HQ
         end.
(1) P Q H, Q P H
(2) P Q, P Q P Q
(3) P Q H, P Q Q P H
(4) P Q, P Q Q P
(5) Not typeable
Check (fun P Q H
      match H with
      | or_introl HP ⇒ @or_intror Q P HP
      | or_intror HQ ⇒ @or_introl Q P HQ
      end) : P Q, PQQP.

Existential Quantification

To give evidence for an existential quantifier, we package a witness x together with a proof that x satisfies the property P:

Inductive ex {A : Type} (P : AProp) : Prop :=
  | ex_intro : x : A, P xex P.

Notation "'exists' x , p" :=
  (ex (fun xp))
    (at level 200, right associativity) : type_scope.

The more familiar form x, ev x desugars to an expression involving ex:
Check ex (fun nev n) : Prop.
Here's how to define an explicit proof object involving ex:
Definition some_nat_is_even : n, ev n :=
  ex_intro ev 4 (ev_SS 2 (ev_SS 0 ev_0)).

Which of the following propositions is proved by providing an explicit witness w using exist w?
(1) x: nat, ( n, x = S n) (x<>0)
(2) x: nat, (x<>0) ( n, x = S n)
(3) x: nat, (x=0) ~( n, x = S n)
(4) x: nat, x = 4 (x<>0)
(5) none of the above
Goal x: nat, (x<>0) → ( n, x = S n).
Proof.
intros. destruct x as [| x'].
- exfalso. apply H. reflexivity.
- x'. reflexivity.
Qed.

To destruct existentials in a proof term we simply use match:
Definition dist_exists_or_term (X:Type) (P Q : XProp) :
  ( x, P xQ x) → ( x, P x) ∨ ( x, Q x) :=
  fun Hmatch H with
           | ex_intro _ x Hx
               match Hx with
               | or_introl HPxor_introl (ex_intro _ x HPx)
               | or_intror HQxor_intror (ex_intro _ x HQx)
               end
           end.

True and False

The inductive definition of the True proposition is simple:
Inductive True : Prop :=
  | I : True.
It has one constructor (so every proof of True is the same, so being given a proof of True is not informative.)

False is equally simple -- indeed, so simple it may look syntactically wrong at first glance!
Inductive False : Prop := .
That is, False is an inductive type with no constructors -- i.e., no way to build evidence for it. For example, there is no way to complete the following definition such that it succeeds.
Fail
  Definition contra : False :=
  42.

But it is possible to destruct False by pattern matching. There can be no patterns that match it, since it has no constructors. So the pattern match also is so simple it may look syntactically wrong at first glance.
Definition false_implies_zero_eq_one : False → 0 = 1 :=
  fun contramatch contra with end.
Since there are no branches to evaluate, the match expression can be considered to have any type we want, including 0 = 1. Fortunately, it's impossible to ever cause the match to be evaluated, because we can never construct a value of type False to pass to the function.

Equality

Even Coq's equality relation is not built in. We can define it ourselves:
Inductive eq {X:Type} : XXProp :=
  | eq_refl : x, eq x x.

Notation "x == y" := (eq x y)
                       (at level 70, no associativity)
                     : type_scope.

Coq terms are "the same" if they are convertible according to a simple set of computation rules: evaluation of function applications, inlining of definitions, and simplification of matches.
Lemma four: 2 + 2 == 1 + 3.
Proof.
  apply eq_refl.
Qed.

reflexivity is essentially just apply eq_refl.
Definition four' : 2 + 2 == 1 + 3 :=
  eq_refl 4.

Definition singleton : (X:Type) (x:X), []++[x] == x::[] :=
  fun (X:Type) (x:X) ⇒ eq_refl [x].

We can also pattern-match on an equality proof:
Definition eq_add : (n1 n2 : nat), n1 == n2 → (S n1) == (S n2) :=
  fun n1 n2 Heq
    match Heq with
    | eq_refl neq_refl (S n)
    end.

A tactic-based proof runs into some difficulties if we try to use our usual repertoire of tactics, such as rewrite and reflexivity. Those work with *setoid* relations that Coq knows about, such as =, but not our ==. We could prove to Coq that == is a setoid, but a simpler way is to use destruct and apply instead.
Theorem eq_add' : (n1 n2 : nat), n1 == n2 → (S n1) == (S n2).
Proof.
  intros n1 n2 Heq.
  Fail rewrite Heq. (* doesn't work for _our_ == relation *)
  destruct Heq as [n]. (* n1 and n2 replaced by n in the goal! *)
  Fail reflexivity. (* doesn't work for _our_ == relation *)
  apply eq_refl.
Qed.

Which of the following is a correct proof object for the proposition
     x, x + 3 == 4 ?
(1) eq_refl 4
(2) ex_intro (z + 3 == 4) 1 (eq_refl 4)
(3) ex_intro (fun z (z + 3 == 4)) 1 (eq_refl 4)
(4) ex_intro (fun z (z + 3 == 4)) 1 (eq_refl 1)
(5) none of the above
Fail Definition quiz1 : x, x + 3 == 4
  := eq_refl 4.
Fail Definition quiz2 : x, x + 3 == 4
  := ex_intro (z + 3 == 4) 1 (eq_refl 4).
Definition quiz3 : x, x + 3 == 4
  := ex_intro (fun z ⇒ (z + 3 == 4)) 1 (eq_refl 4).
Fail Definition quiz4 : x, x + 3 == 4
  := ex_intro (fun z ⇒ (z + 3 == 4)) 1 (eq_refl 1).

Coq's Trusted Computing Base

The Coq typechecker is what actually checks our proofs. We have to trust it, but it's relatively small and straightforward.
For example, it rejects this broken proof:
Fail Definition or_bogus : P Q, PQP :=
  fun (P Q : Prop) (A : PQ) ⇒
    match A with
    | or_introl HH
    end.
And these:
Fail Fixpoint infinite_loop {X : Type} (n : nat) {struct n} : X :=
  infinite_loop n.

Fail Definition falso : False := infinite_loop 0.
Complex tactics can (in principle and occasionally in practice) produce invalid proof objects. Qed runs the type checker to detect such situations.