









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










The theorems we have proven so far rely only on universal quantification (forall) and implication (→). These are in fact the only logical connectives built in to Coq. As we move on to stating and proving more interestings theorems, we’ll want to have all the other familiar logical connectives— conjunction, disjunction, negation, existential quantification—at our dis- posal. In this chapter, we will see how to define all these connectives using just forall, →, and Inductive.
The logical conjunction of propositions A and B is represented by the follow- ing inductively defined proposition. Inductive and (A B : Prop) : Prop := conj : A → B → (and A B). Note that, like the definition of ev, this definition is parameterized; however, in this case, the parameters are themselves propositions. The intuition behind this definition is simple: to construct evidence for and A B, we must provide evidence for A and evidence for B. More precisely:
70 9 Logical Connectives Notation "A ∧ B" := (and A B) : type_scope. (The type_scope annotation tells Coq that this notation will be appearing in propositions, not values.) Besides the elegance of building everything up from a tiny foundation, what’s nice about defining conjunction this way is that we can prove state- ments involving conjunction using the tactics that we already know. For ex- ample, if the goal statement is a conjuction, we can prove it by applying the single constructor conj, which (as can be seen from the type of conj) solves the current goal and leaves the two parts of the conjunction as subgoals to be proved separately. Theorem and_example : (ev 0) ∧ (ev 4). Proof. apply conj. Case "left". apply ev_0. Case "right". apply ev_SS. apply ev_SS. apply ev_0.! The split tactic is a convenient shorthand for apply conj. Conversely, the inversion tactic can be used to investigate a conjunction hypothesis in the context and calculate what evidence must have been used to build it. 9.1.2 EXERCISE [!]: Look at the proof of and_1 and prove and_2 in Logic.v. 9.1.3 EXERCISE [!!]: Prove that conjunction is associative. Theorem and_assoc : forall A B C : Prop, A ∧ (B ∧ C) → (A ∧ B) ∧ C. 9.1.4 EXERCISE [!!]: Now we can prove the other direction of the equivalence of even and ev: Theorem even_ev : forall n : nat, (even n → ev n) ∧ (even (S n) → ev (S n)). Notice that the left-hand conjunct here is the statement we are actually in- terested in; the right-hand conjunct is needed in order to make the induction hypothesis strong enough that we can carry out the reasoning in the induc- tive step. (To see why this is needed, try proving the left conjunct by itself and observe where things get stuck.)
72 9 Logical Connectives inversion H as [HA | HB]. Case "left". apply or_intror. apply HA. Case "right". apply or_introl. apply HB.! From here on, we’ll use the handy tactics left and right in place of apply or_introl and apply or_intror: Theorem or_commut′^ : forall A B : Prop, A ∨ B → B ∨ A. Proof. intros A B H. inversion H as [HA | HB]. Case "left". right. apply HA. Case "right". left. apply HB.! 9.3.2 EXERCISE [!!]: Using the proof of or_distributes_over_and_1 as a guide, prove or_distributes_over_and_2. 9.3.3 EXERCISE [!]: Prove the distributivity of ∧ and ∨ as an iff proposition (or_distributes_over_and). We’ve already seen several places where analogous structures can be found in Coq’s computational (Set) and logical (Prop) worlds. Here is one more: the boolean operators andb and orb are obviously analogs, in some sense, of the logical connectives ∧ and ∨. This analogy can be made more precise by the following theorems, which show how to “translate” know- ledge about andb and orb’s behaviors on certain inputs into propositional facts about those inputs. Theorem andb_true : forall b c, andb b c = true → b = true ∧ c = true. Theorem andb_false : forall b c, andb b c = false → b = false ∨ c = false. Theorem orb_true : forall b c, orb b c = true → b = true ∨ c = true. Theorem orb_false : forall b c, orb b c = false → b = false ∧ c = false. 9.3.4 EXERCISE [!]: The proof of andb_true is given in Logic.v. Fill in the other three.
Falsehood can be represented in Coq as an inductively defined proposition with no constructors.
9.5 Truth 73 Inductive False : Prop :=. 9.4.1 EXERCISE [!]: Can you predict what the induction principle False_ind will look like? Since False has no constructors, inverting it always yields zero subgoals, allowing us to immediately prove any goal. Theorem False_implies_nonsense : False → plus 2 2 = 5. Proof. intros contra. inversion contra.! Actually, since the proof of False_implies_nonsense doesn’t actually have anything to do with the specific nonsensical thing being proved; it can easily be generalized to work for an arbitrary P: Theorem ex_falso_quodlibet : forall (P:Prop), False → P. The Latin ex falso quodlibet means, literally, “from falsehood follows whatever you please.” This theorem is also known as the principle of explosion. Conversely, the only way to prove False is if there is already something nonsensical or contradictory in the context: Theorem nonsense_implies_False : plus 2 2 = 5 → False. Proof. intros contra. inversion contra.!
Since we have defined falsehood in Coq, we should also mention that it is, of course, possible to define truth in the same way. 9.5.1 EXERCISE [!!]: Define True as another inductively defined proposition. What induction principle will Coq generate for your definition? (The intution is that True should be a proposition for which it is trivial to give evidence. Alternatively, you may find it easiest to start with the induction principle and work backwards to the inductive definition.) However, unlike False, which we’ll use extensively, True is basically a theoretical curiosity: since it is trivial to prove as a goal, it carries no useful information as a hypothesis.
9.7 Inequality 75 considered as characterizations of classical logic (as opposed to constructive logic, which is what is “built in” to Coq). We can’t prove them in Coq, but we can consistently add any one of them as an unproven axiom if we wish to work in classical logic. Prove that these five propositions are equivalent. Definition peirce := forall P Q: Prop, ((P→Q)→P)→P. Definition classic := forall P:Prop, ~~P → P. Definition excluded_middle := forall P:Prop, P ∨~P. Definition de_morgan_not_and_not := forall P Q:Prop, ~(~P∧~Q) → P∨Q. Definition implies_to_or := forall P Q:Prop, (P→Q) → (~P∨Q).
Saying x <> y is just the same as saying ~(x = y). Notation "x <> y" := (~ (x = y)) : type_scope. Since inequality involves a negation, it again requires a little practice to be able to work with it fluently. Here is one very useful trick. If you are trying to prove a goal that is nonsensical (e.g., the goal state is false = true), apply the lemma ex_falso_quodlibet to change the goal to False. This makes it easier to use assumptions of the form ~P that are available in the context— in particular, assumptions of the form x<>y. 9.7.1 EXERCISE [!!]: Use Coq to read through the proof of this theorem. Theorem not_false_then_true : forall b : bool, b <> false → b = true. Use the same idea to prove that the numeric comparison function beq_nat yields false on unequal numbers. Theorem not_eq_false_beq : forall n n′^ : nat, n <> n′ → false = beq_nat n n′. 9.7.2 EXERCISE [!!!!, OPTIONAL]: The converse of beq_false_not_eq says that if beq_nat yields false then its arguments are unequal. Prove it. Theorem beq_false_not_eq : forall n m, false = beq_nat n m → n <> m.
76 9 Logical Connectives
Another extremely important logical connective is existential quantification. Inductive ex (X : Set) (P : X → Prop) : Prop := ex_intro : forall witness:X, P witness → ex X P. The intuition behind this definition is that, in order to give evidence for the assertion “there exists an x for which the proposition P holds" we must actually name a witness—a specific value x—and then give evidence for P x. We can use Coq’s notation definition facility to introduce more standard notation for writing existentially quantified propositions, exactly parallel to the built-in syntax for universally quantified propositions. Instead of writ- ing (ex nat (fun x => ev x)) to express the proposition that there exists some number that is even, for example, we can write exists x:nat, ev x. (The exact definition of the notation is in Logic.v, of course, but it is not necessary to understand the details). We can use the same tactics as always for manipulate existentials. For ex- ample, if to prove an existential, we apply the constructor ex_intro. Since the premise of ex_intro involves a variable (witness) that does not ap- pear in its conclusion, we need to explicitly give its value when we use apply. Example exists_example_1 : exists n, plus n (mult n n) = 6. Proof. apply ex_intro with (witness:=2). reflexivity.! Or, instead of writing apply ex_intro with (witness:=...), we can use the shorthand tactic exists .... Proof. exists 2. reflexivity.! Conversely, if we have an existential hypothesis in the context, we can eliminate it with destruct. Theorem exists_example_2 : forall n, (exists m, n = plus 4 m) → (exists o, n = plus 2 o). Proof. intros n H.
78 9 Logical Connectives The advantage of the second definition is that the induction principle that Coq derives for it is precisely the familiar principle of Leibniz equality: what we mean when we say “x and y are equal” is that every property on A that is true of x is also true of y. Check eq’_ind. ! (^) eq’_ind : forall (A : Set) (x : A) (P : A → Prop), P x → forall y : A, eq’ A x y → P y This is currently commented out in the .v for coq 8.2 compatibility reasons
See Logic.v for the text of this section.
See Logic.v for the text of this section. 9.11.1 EXERCISE [!!!]: Do the exercise marked p_provability in Logic.v.
A proposition parameterized over a number (like ev) can be thought of as a predicate—i.e., it defines a subset of nat, namely those numbers for which the proposition is provable. In the same way, a two-argument proposition can be thought of as a relation—i.e., it defines a set of pairs for which the proposition is provable. In this chapter, we explore the consequences of this observation.
We’ve already seen an inductive definition of one fundamental relation: equality. Another very useful one is the “less than or equal to” relation on numbers: Inductive le : nat → nat → Prop := | le_n : forall n, le n n | le_S : forall n m, (le n m) → (le n (S m)). Notation "m <= n" := (le m n). This definition should be fairly intuitive. It says that there are two ways to give evidence that one number is less than or equal to another: either observe that they are the same number, or give evidence that the first is less than or equal to the predecessor of the second. This is a fine definition of the <= relation, but we can streamline it a little by observing that the left-hand argument n is the same everywhere in the definition, so we can actually make it a “general parameter” to the whole definition, rather than an argument to each constructor. Inductive le (n:nat) : nat → Prop := | le_n : le n n | le_S : forall m, (le n m) → (le n (S m)).
10.2 Relations, in General 81 10.1.3 EXERCISE [!!!]: We can define three-place relations, four-place relations, etc., in just the same way as binary relations. For example, consider the fol- lowing three-place relation on numbers: Inductive R : nat → nat → nat → Prop := | c1 : R 0 0 0 | c2 : forall m n o, R m n o → R (S m) n (S o) | c3 : forall m n o, R m n o → R m (S n) (S o) | c4 : forall m n o, R (S m) (S n) (S (S o)) → R m n o | c5 : forall m n o, R m n o → R n m o.
See Logic.v for the text of this section. In logic.v, the theorem le_not_a_partial_function is a formal proof that le is not a partial function. Here is an informal proof: Theorem: le is not a partial function.
82 10 Relations Proof: Suppose le is a partial function. It is easy to show that le 0 1 and le 0 2. But by the definition of partial function this means 1 = 2, a contradiction. Thus, le is not a partial function.! 10.2.1 EXERCISE [!!]: Prove that the total relation is not a partial function, but that the empty relation is. 10.2.2 EXERCISE [!!]: Show the transitivity of lt directly, by induction on the derivation. 10.2.3 EXERCISE [!!]: Show the transitiveity of lt directly, by induction on o (the greatest variable). 10.2.4 EXERCISE [!]: Prove the following theorem about le: Theorem le_S_n : forall n m, (S n <= S m) → (n <= m). 10.2.5 EXERCISE [!!]: Write an informal proof of le_Sn_n: ∀n, ¬(n + 1 <= n). 10.2.6 EXERCISE [!]: Using your informal proof, construct a formal proof of le_Sn_n. 10.2.7 EXERCISE [!!]: Show that le is not symmetric. 10.2.8 EXERCISE [!!]: Show that le is antisymmetric.
Let’s pause briefly to record several facts about the <= and < relations and the ble_nat function that we are going to need later in the course. Theorem O_le_n : forall n, 0 <= n. Theorem le_plus : forall a b, a <= a + b. Theorem plus_lt : forall n1 n2 m, plus n1 n2 < m → n1 < m ∧ n2 < m. Theorem n_le_m__Sn_le_Sm : forall n m, n <= m → S n <= S m. Theorem lt_S : forall n m,