adamc@45: (* Copyright (c) 2008, Adam Chlipala
adamc@45: *
adamc@45: * This work is licensed under a
adamc@45: * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@45: * Unported License.
adamc@45: * The license text is available at:
adamc@45: * http://creativecommons.org/licenses/by-nc-nd/3.0/
adamc@45: *)
adamc@45:
adamc@45: (* begin hide *)
adamc@45: Require Import List.
adamc@45:
adamc@45: Require Import Tactics.
adamc@45:
adamc@45: Set Implicit Arguments.
adamc@45: (* end hide *)
adamc@45:
adamc@45:
adamc@45: (** %\chapter{Inductive Predicates}% *)
adamc@45:
adamc@45: (** The so-called "Curry-Howard Correspondence" states a formal connection between functional programs and mathematical proofs. In the last chapter, we snuck in a first introduction to this subject in Coq. Witness the close similarity between the types [unit] and [True] from the standard library: *)
adamc@45:
adamc@45: Print unit.
adamc@45: (** [[
adamc@45:
adamc@45: Inductive unit : Set := tt : unit
adamc@45: ]] *)
adamc@45:
adamc@45: Print True.
adamc@45: (** [[
adamc@45:
adamc@45: Inductive True : Prop := I : True
adamc@45: ]] *)
adamc@45:
adamc@45: (** Recall that [unit] is the type with only one value, and [True] is the proposition that always holds. Despite this superficial difference between the two concepts, in both cases we can use the same inductive definition mechanism. The connection goes further than this. We see that we arrive at the definition of [True] by replacing [unit] by [True], [tt] by [I], and [Set] by [Prop]. The first two of these differences are superficial changes of names, while the third difference is the crucial one for separating programs from proofs. A term [T] of type [Set] is a type of programs, and a term of type [T] is a program. A term [T] of type [Prop] is a logical proposition, and its proofs are of type [T].
adamc@45:
adamc@45: [unit] has one value, [tt]. [True] has one proof, [I]. Why distinguish between these two types? Many people who have read about Curry-Howard in an abstract context and not put it to use in proof engineering answer that the two types in fact %\textit{%##should not##%}% be distinguished. There is a certain aesthetic appeal to this point of view, but I want to argue that it is best to treat Curry-Howard very loosely in practical proving. There are Coq-specific reasons for preferring the distinction, involving efficient compilation and avoidance of paradoxes in the presence of classical math, but I will argue that there is a more general principle that should lead us to avoid conflating programming and proving.
adamc@45:
adamc@45: The essence of the argument is roughly this: to an engineer, not all functions of type [A -> B] are created equal, but all proofs of a proposition [P -> Q] are. This idea is known as %\textit{%##proof irrelevance##%}%, and its formalizations in logics prevent us from distinguishing between alternate proofs of the same proposition. Proof irrelevance is compatible with, but not derivable in, Gallina. Apart from this theoretical concern, I will argue that it is most effective to do engineering with Coq by employing different techniques for programs versus proofs. Most of this book is organized around that distinction, describing how to program, by applying standard functional programming techniques in the presence of dependent types; and how to prove, by writing custom Ltac decision procedures.
adamc@45:
adamc@45: With that perspective in mind, this chapter is sort of a mirror image of the last chapter, introducing how to define predicates with inductive definitions. We will point out similarities in places, but much of the effective Coq user's bag of tricks is disjoint for predicates versus "datatypes." This chapter is also a covert introduction to dependent types, which are the foundation on which interesting inductive predicates are built, though we will rely on tactics to build dependently-typed proof terms for us for now. A future chapter introduces more manual application of dependent types. *)
adamc@45:
adamc@45:
adamc@45: (** * Propositional logic *)
adamc@45:
adamc@45: (** Let us begin with a brief tour through the definitions of the connectives for propositional logic. We will work within a Coq section that provides us with a set of propositional variables. In Coq parlance, these are just terms of type [Prop.] *)
adamc@45:
adamc@45: Section Propositional.
adamc@45: Variables P Q : Prop.
adamc@45:
adamc@45: (** In Coq, the most basic propositional connective is implication, written [->], which we have already used in almost every proof. Rather than being defined inductively, implication is built into Coq as the function type constructor.
adamc@45:
adamc@45: We have also already seen the definition of [True]. For a demonstration of a lower-level way of establishing proofs of inductive predicates, we turn to this trivial theorem. *)
adamc@45:
adamc@45: Theorem obvious : True.
adamc@45: apply I.
adamc@45: Qed.
adamc@45:
adamc@45: (** We may always use the [apply] tactic to take a proof step based on applying a particular constructor of the inductive predicate that we are trying to establish. Sometimes there is only one constructor that could possibly apply, in which case a shortcut is available: *)
adamc@45:
adamc@45: Theorem obvious' : True.
adamc@45: constructor.
adamc@45: Qed.
adamc@45:
adamc@45: (** There is also a predicate [False], which is the Curry-Howard mirror image of [Empty_set] from the last chapter. *)
adamc@45:
adamc@45: Print False.
adamc@45: (** [[
adamc@45:
adamc@45: Inductive False : Prop :=
adamc@45: ]] *)
adamc@45:
adamc@45: (** We can conclude anything from [False], doing case analysis on a proof of [False] in the same way we might do case analysis on, say, a natural number. Since there are no cases to consider, any such case analysis succeeds immediately in proving the goal. *)
adamc@45:
adamc@45: Theorem False_imp : False -> 2 + 2 = 5.
adamc@45: destruct 1.
adamc@45: Qed.
adamc@45:
adamc@45: (** In a consistent context, we can never build a proof of [False]. In inconsistent contexts that appear in the courses of proofs, it is usually easiest to proceed by demonstrating that inconsistency with an explicit proof of [False]. *)
adamc@45:
adamc@45: Theorem arith_neq : 2 + 2 = 5 -> 9 + 9 = 835.
adamc@45: intro.
adamc@45:
adamc@45: (** At this point, we have an inconsistent hypothesis [2 + 2 = 5], so the specific conclusion is not important. We use the [elimtype] tactic to state a proposition, telling Coq that we wish to construct a proof of the new proposition and then prove the original goal by case analysis on the structure of the new auxiliary proof. Since [False] has no constructors, [elimtype False] simply leaves us with the obligation to prove [False]. *)
adamc@45:
adamc@45: elimtype False.
adamc@45: (** [[
adamc@45:
adamc@45: H : 2 + 2 = 5
adamc@45: ============================
adamc@45: False
adamc@45: ]] *)
adamc@45:
adamc@45: (** For now, we will leave the details of this proof about arithmetic to [crush]. *)
adamc@45:
adamc@45: crush.
adamc@45: Qed.
adamc@45:
adamc@45: (** A related notion to [False] is logical negation. *)
adamc@45:
adamc@45: Print not.
adamc@45: (** [[
adamc@45:
adamc@45: not = fun A : Prop => A -> False
adamc@45: : Prop -> Prop
adamc@45: ]] *)
adamc@45:
adamc@45: (** We see that [not] is just shorthand for implication of [False]. We can use that fact explicitly in proofs. The syntax [~P] expands to [not P]. *)
adamc@45:
adamc@45: Theorem arith_neq' : ~ (2 + 2 = 5).
adamc@45: unfold not.
adamc@45:
adamc@45: (** [[
adamc@45:
adamc@45: ============================
adamc@45: 2 + 2 = 5 -> False
adamc@45: ]] *)
adamc@45:
adamc@45: crush.
adamc@45: Qed.
adamc@45:
adamc@45: (** We also have conjunction, which we introduced in the last chapter. *)
adamc@45:
adamc@45: Print and.
adamc@45: (** [[
adamc@45:
adamc@45: Inductive and (A : Prop) (B : Prop) : Prop := conj : A -> B -> A /\ B
adamc@45: ]] *)
adamc@45:
adamc@45: (** The interested reader can check that [and] has a Curry-Howard doppleganger called [prod], the type of pairs. However, it is generally most convenient to reason about conjunction using tactics. An explicit proof of commutativity of [and] illustrates the usual suspects for such tasks. [/\] is an infix shorthand for [and]. *)
adamc@45:
adamc@45: Theorem and_comm : P /\ Q -> Q /\ P.
adamc@45: (** We start by case analysis on the proof of [P /\ Q]. *)
adamc@45:
adamc@45: destruct 1.
adamc@45: (** [[
adamc@45:
adamc@45: H : P
adamc@45: H0 : Q
adamc@45: ============================
adamc@45: Q /\ P
adamc@45: ]] *)
adamc@45:
adamc@45: (** Every proof of a conjunction provides proofs for both conjuncts, so we get a single subgoal reflecting that. We can proceed by splitting this subgoal into a case for each conjunct of [Q /\ P]. *)
adamc@45:
adamc@45: split.
adamc@45: (** [[
adamc@45: 2 subgoals
adamc@45:
adamc@45: H : P
adamc@45: H0 : Q
adamc@45: ============================
adamc@45: Q
adamc@45:
adamc@45: subgoal 2 is:
adamc@45: P
adamc@45: ]] *)
adamc@45:
adamc@45: (** In each case, the conclusion is among our hypotheses, so the [assumption] tactic finishes the process. *)
adamc@45:
adamc@45: assumption.
adamc@45: assumption.
adamc@45: Qed.
adamc@45:
adamc@45: (** Coq disjunction is called [or] and abbreviated with the infix operator [\/]. *)
adamc@45:
adamc@45: Print or.
adamc@45: (** [[
adamc@45:
adamc@45: Inductive or (A : Prop) (B : Prop) : Prop :=
adamc@45: or_introl : A -> A \/ B | or_intror : B -> A \/ B
adamc@45: ]] *)
adamc@45:
adamc@45: (** We see that there are two ways to prove a disjunction: prove the first disjunct or prove the second. The Curry-Howard analogue of this is the Coq [sum] type. We can demonstrate the main tactics here with another proof of commutativity. *)
adamc@45:
adamc@45: Theorem or_comm : P \/ Q -> Q \/ P.
adamc@45: (** As in the proof for [and], we begin with case analysis, though this time we are met by two cases instead of one. *)
adamc@45: destruct 1.
adamc@45: (** [[
adamc@45:
adamc@45: 2 subgoals
adamc@45:
adamc@45: H : P
adamc@45: ============================
adamc@45: Q \/ P
adamc@45:
adamc@45: subgoal 2 is:
adamc@45: Q \/ P
adamc@45: ]] *)
adamc@45:
adamc@45: (** We can see that, in the first subgoal, we want to prove the disjunction by proving its second disjunct. The [right] tactic telegraphs this intent. *)
adamc@45:
adamc@45: right; assumption.
adamc@45:
adamc@45: (** The second subgoal has a symmetric proof.
adamc@45:
adamc@45: [[
adamc@45:
adamc@45: 1 subgoal
adamc@45:
adamc@45: H : Q
adamc@45: ============================
adamc@45: Q \/ P
adamc@45: ]] *)
adamc@45:
adamc@45: left; assumption.
adamc@45: Qed.
adamc@45:
adamc@45: End Propositional.
adamc@45: