Coq calls logical statements propositions and
represents them as values of the type Prop. One can
introduce new propositions with declarations that are
similar to inductive type definitions. With inductively
defined propositions one can express inductively defined
relations. As example we take the definition of even
numbers. Using inference rules, we may define a unary
relation even as follows.
As it turns out, inference rules can be expressed as propositions. For even we obtain:
n:nat even n ====== ============== even 0 even(S(S n))
As it turns out, inference rules can be expressed as propositions. For even we obtain:
- even O
- forall n:nat, even n -> even(S(S n))
Inductive even : nat -> Prop :=
| even_0 : even O
| even_SS : forall n:nat, even n -> even(S(S n)).
The definition introduces several objects:
- A constructor even for propositions.
- Two proof constructors even_0 and even_SS.
- A proof constructur even_ind providing for inductive reasoning:
even_ind : forall P: nat->Prop, P 0 -> (forall n: nat, even n -> P n -> P(S(S n))) -> forall n: nat, even n -> P n
Example E0: even (S(S 0)).
apply even_SS. apply even_0. Qed.
Require Import Arith.
Proposition P1: forall n,
even n -> exists m, n = 2*m.
Proof.
apply even_ind.
exists 0. reflexivity.
intros. inversion H0. subst. exists (S x). ring.
Qed.
Proposition P2: forall n,
even(2*n).
Proof.
induction n.
apply even_0.
assert (A: 2* S n=S(S(2*n))). ring.
rewrite A. apply even_SS. apply IHn.
Qed.
Proposition P3: forall n,
even n <-> exists m, n = 2*m.
Proof.
intuition.
apply P1. apply H.
inversion H. subst. apply P2.
Qed.
Proposition P4: forall x,
even x -> x=0 \/ exists y, even y /\ x=S(S y).
Proof.
apply even_ind.
eauto.
eauto.
Qed.
Using the semicolon, we can give a shorter proof of P4.
Example E1: forall x, even x -> x=0 \/ exists y, even y /\ x=S(S y).
apply even_ind; eauto. Qed.
If we tell the eauto tactic to use the contructors of even,
we can shorten proofs considerably.
Hint Constructors even.
Example E2: even(120).
auto 120. Qed.
Example E3: forall n, even(2*n).
Proof with eauto.
induction n...
assert (A: 2* S n=S(S(2*n))). ring.
rewrite A...
Qed.
Here is an example that shows that the inversion tactic
knows about Proposition P4.
Example E4: ~even 1.
intro H. inversion H. Qed.
A more basic proof of E4 can be given with P4.
Example E5: ~even 1.
intro H. apply P4 in H. intuition.
inversion H0.
inversion H0. intuition. inversion H2.
Qed.
We can use the induction tactic to apply even_ind.
This is convenient if the claim doesn't yet match even_ind
since Coq will take care of the necessary generalization steps.
We demonstrate the use of the induction tactic with an
alternative proof of Proposition P1.
Example E6: forall n,
even n -> exists m, n = 2*m.
Proof.
intros n H. induction H.
eauto.
inversion IHeven. subst. exists (S x). ring.
Qed.
Inductive propositions have amazing expressivity.
Using universal quantification forall and implication
->, we can define conjunction, disjunction, True,
False, and existential quantification.
Inductive and (X Y : Prop) : Prop :=
| conj : X -> Y -> (and X Y).
Inductive or (X Y : Prop) : Prop :=
| or_introl : X -> or X Y
| or_intror : Y -> or X Y.
Inductive True : Prop :=
| I : True.
Inductive False : Prop := .
Inductive ex (X: Type) (P: X->Prop) : Prop :=
| ex_intro : forall x, P x -> ex X P.
Have a look at the inductive proof constructors
and_ind, or_ind, True_ind, False_ind and ex_ind.
They express familiar reasoning principles.
Recall that negation ~ and equivalence <-> are defined in Coq.
Recall that negation ~ and equivalence <-> are defined in Coq.
Definition not (X:Prop) : Prop := X -> False.
Definition equiv (X Y:Prop) : Prop := (X->Y) /\ (Y->X).
This page has been generated by coqdoc