Built with Alectryon, running Coq+SerAPI v8.10.0+0.7.0. Coq sources are in this panel; goals and messages will appear in the other. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑ Ctrl+↓ to navigate, Ctrl+🖱️ to focus.
Set Warnings "-notation-overridden,-parsing". From LF Require Export Logic. Require Coq.omega.Omega. (* ################################################################# *)
In the Logic chapter, we looked at several ways of writing
propositions, including conjunction, disjunction, and existential
quantification. In this chapter, we bring yet another new tool
into the mix: inductive definitions.
In past chapters, we have seen two ways of stating that a number
n is even: We can say
(1) evenb n = true, or
(2) ∃ k, n = double k.
Yet another possibility is to say that n is even if we can
establish its evenness from the following rules:
- Rule ev_0: The number 0 is even.
- Rule ev_SS: If n is even, then S (S n) is even.
To illustrate how this new definition of evenness works,
let's imagine using it to show that 4 is even. By rule ev_SS,
it suffices to show that 2 is even. This, in turn, is again
guaranteed by rule ev_SS, as long as we can show that 0 is
even. But this last fact follows directly from the ev_0 rule.
We will see many definitions like this one during the rest
of the course. For purposes of informal discussions, it is
helpful to have a lightweight notation that makes them easy to
read and write. Inference rules are one such notation:
(ev_0) even 0
even n
(ev_SS) even (S (S n))
(ev_0) even 0
(ev_SS) even (S (S n))
Each of the textual rules above is reformatted here as an
inference rule; the intended reading is that, if the premises
above the line all hold, then the conclusion below the line
follows. For example, the rule ev_SS says that, if n
satisfies even, then S (S n) also does. If a rule has no
premises above the line, then its conclusion holds
unconditionally.
We can represent a proof using these rules by combining rule
applications into a proof tree. Here's how we might transcribe
the above proof that 4 is even:
(ev_0) even 0
(ev_SS) even 2
(ev_SS) even 4
(ev_0) even 0
(ev_SS) even 2
(ev_SS) even 4
(Why call this a "tree" (rather than a "stack", for example)?
Because, in general, inference rules can have multiple premises.
We will see examples of this shortly.
(* ================================================================= *)
Putting all of this together, we can translate the definition of
evenness into a formal Coq definition using an Inductive
declaration, where each constructor corresponds to an inference
rule:
Inductive even : nat -> Prop :=
| ev_0 : even 0
| ev_SS (n : nat) (H : even n) : even (S (S n)).
This definition is different in one crucial respect from previous
uses of Inductive: the thing we are defining is not a Type,
but rather a function from nat to Prop -- that is, a property
of numbers. We've already seen other inductive definitions that
result in functions -- for example, list, whose type is Type →
Type. What is really new here is that, because the nat
argument of even appears to the right of the colon, it is
allowed to take different values in the types of different
constructors: 0 in the type of ev_0 and S (S n) in the type
of ev_SS.
In contrast, the definition of list names the X parameter
globally, to the left of the colon, forcing the result of
nil and cons to be the same (list X). Had we tried to bring
nat to the left in defining even, we would have seen an
error:
In an Inductive definition, an argument to the type
constructor on the left of the colon is called a "parameter",
whereas an argument on the right is called an "index".
For example, in Inductive list (X : Type) := ..., X is a
parameter; in Inductive even : nat → Prop := ..., the
unnamed nat argument is an index.
We can think of the definition of even as defining a Coq
property even : nat → Prop, together with primitive theorems
ev_0 : even 0 and ev_SS : ∀ n, even n → even (S (S n)).
That definition can also be written as follows...
Inductive even : nat -> Prop :=
| ev_0 : even 0
| ev_SS : forall n, even n -> even (S (S n)).
... making explicit the type of the rule ev_SS.
Such "constructor theorems" have the same status as proven
theorems. In particular, we can use Coq's apply tactic with the
rule names to prove even for particular numbers...
even 4even 4even 2apply ev_0. Qed.even 0
... or we can use function application syntax:
even 4apply (ev_SS 2 (ev_SS 0 ev_0)). Qed.even 4
We can also prove theorems that have hypotheses involving even.
forall n : nat, even n -> even (4 + n)forall n : nat, even n -> even (4 + n)n:nateven n -> even (4 + n)n:nateven n -> even (S (S (S (S n))))n:natHn:even neven (S (S (S (S n))))n:natHn:even neven (S (S n))apply Hn. Qed.n:natHn:even neven n
forall n : nat, even (double n)(* FILL IN HERE *) Admitted.forall n : nat, even (double n)
☐
(* ################################################################# *)
Besides constructing evidence that numbers are even, we can also
reason about such evidence.
Introducing even with an Inductive declaration tells Coq not
only that the constructors ev_0 and ev_SS are valid ways to
build evidence that some number is even, but also that these two
constructors are the only ways to build evidence that numbers
are even (in the sense of even).
In other words, if someone gives us evidence E for the assertion
even n, then we know that E must have one of two shapes:
- E is ev_0 (and n is O), or
- E is ev_SS n' E' (and n is S (S n'), where E' is evidence for even n').
This suggests that it should be possible to analyze a
hypothesis of the form even n much as we do inductively defined
data structures; in particular, it should be possible to argue by
induction and case analysis on such evidence. Let's look at a
few examples to see what this means in practice.
(* ================================================================= *)
Suppose we are proving some fact involving a number n, and
we are given even n as a hypothesis. We already know how to
perform case analysis on n using destruct or induction,
generating separate subgoals for the case where n = O and the
case where n = S n' for some n'. But for some proofs we may
instead want to analyze the evidence that even n directly. As
a tool, we can prove our characterization of evidence for
even n, using destruct.
forall n : nat, even n -> n = 0 \/ (exists n' : nat, n = S (S n') /\ even n')forall n : nat, even n -> n = 0 \/ (exists n' : nat, n = S (S n') /\ even n')n:natE:even nn = 0 \/ (exists n' : nat, n = S (S n') /\ even n')0 = 0 \/ (exists n' : nat, 0 = S (S n') /\ even n')n':natE':even n'S (S n') = 0 \/ (exists n'0 : nat, S (S n') = S (S n'0) /\ even n'0)0 = 0 \/ (exists n' : nat, 0 = S (S n') /\ even n')reflexivity.0 = 0n':natE':even n'S (S n') = 0 \/ (exists n'0 : nat, S (S n') = S (S n'0) /\ even n'0)n':natE':even n'exists n'0 : nat, S (S n') = S (S n'0) /\ even n'0n':natE':even n'S (S n') = S (S n') /\ even n'n':natE':even n'S (S n') = S (S n')n':natE':even n'even n'apply E'. Qed.n':natE':even n'even n'
The following theorem can easily be proved using destruct on
evidence.
forall n : nat, even n -> even (Nat.pred (Nat.pred n))forall n : nat, even n -> even (Nat.pred (Nat.pred n))n:natE:even neven (Nat.pred (Nat.pred n))even (Nat.pred (Nat.pred 0))n':natE':even n'even (Nat.pred (Nat.pred (S (S n'))))even (Nat.pred (Nat.pred 0))apply ev_0.even 0n':natE':even n'even (Nat.pred (Nat.pred (S (S n'))))apply E'. Qed.n':natE':even n'even n'
However, this variation cannot easily be handled with destruct.
forall n : nat, even (S (S n)) -> even n
Intuitively, we know that evidence for the hypothesis cannot
consist just of the ev_0 constructor, since O and S are
different constructors of the type nat; hence, ev_SS is the
only case that applies. Unfortunately, destruct is not smart
enough to realize this, and it still generates two subgoals. Even
worse, in doing so, it keeps the final goal unchanged, failing to
provide any useful information for completing the proof.
forall n : nat, even (S (S n)) -> even nn:natE:even (S (S n))even nn:nateven nn, n':natE':even n'even n(* We must prove that [n] is even from no assumptions! *) Abort.n:nateven n
What happened, exactly? Calling destruct has the effect of
replacing all occurrences of the property argument by the values
that correspond to each constructor. This is enough in the case
of ev_minus2 because that argument n is mentioned directly
in the final goal. However, it doesn't help in the case of
evSS_ev since the term that gets replaced (S (S n)) is not
mentioned anywhere.
We could patch this proof by replacing the goal even n,
which does not mention the replaced term S (S n), by the
equivalent goal even (pred (pred (S (S n)))), which does mention
this term, after which destruct can make progress. But it is
more straightforward to use our inversion lemma.
forall n : nat, even (S (S n)) -> even nforall n : nat, even (S (S n)) -> even nn:natH:even (S (S n))even nn:natH:S (S n) = 0 \/ (exists n' : nat, S (S n) = S (S n') /\ even n')even nn:natH:S (S n) = 0even nn:natH:exists n' : nat, S (S n) = S (S n') /\ even n'even ndiscriminate H.n:natH:S (S n) = 0even nn:natH:exists n' : nat, S (S n) = S (S n') /\ even n'even nn, n':natHnm:S (S n) = S (S n')Hev:even n'even nn, n':natHnm:S (S n) = S (S n')Hev:even n'n = n' -> even nn, n':natHnm:S (S n) = S (S n')Hev:even n'Heq:n = n'even napply Hev. Qed.n, n':natHnm:S (S n) = S (S n')Hev:even n'Heq:n = n'even n'
Coq provides a tactic called inversion, which does the work of
our inversion lemma and more besides.
The inversion tactic can detect (1) that the first case
(n = 0) does not apply and (2) that the n' that appears in the
ev_SS case must be the same as n. It has an "as" variant
similar to destruct, allowing us to assign names rather than
have Coq choose them.
forall n : nat, even (S (S n)) -> even nforall n : nat, even (S (S n)) -> even nn:natE:even (S (S n))even n(* We are in the [E = ev_SS n' E'] case now. *) apply E'. Qed.n:natE:even (S (S n))n':natE':even nH:n' = neven n
The inversion tactic can apply the principle of explosion to
"obviously contradictory" hypotheses involving inductive
properties, something that takes a bit more work using our
inversion lemma. For example:
~ even 1~ even 1H:even 1FalseH:1 = 0 \/ (exists n' : nat, 1 = S (S n') /\ even n')FalseH:1 = 0Falsem:natHm:1 = S (S m)Falsediscriminate H.H:1 = 0Falsediscriminate Hm. Qed.m:natHm:1 = S (S m)False~ even 1inversion H. Qed.H:even 1False
Exercise: 1 star, standard (inversion_practice)
forall n : nat, even (S (S (S (S n)))) -> even n(* FILL IN HERE *) Admitted.forall n : nat, even (S (S (S (S n)))) -> even n
☐
even 5 -> 2 + 2 = 9(* FILL IN HERE *) Admitted.even 5 -> 2 + 2 = 9
☐
The inversion tactic does quite a bit of work. When
applied to equalities, as a special case, it does the work of both
discriminate and injection. In addition, it carries out the
intros and rewrites that are typically necessary in the case
of injection. It can also be applied, more generally, to analyze
evidence for inductively defined propositions. As examples, we'll
use it to reprove some theorems from Tactics.v.
forall n m o : nat, [n; m] = [o; o] -> [n] = [m]forall n m o : nat, [n; m] = [o; o] -> [n] = [m]n, m, o:natH:[n; m] = [o; o][n] = [m]reflexivity. Qed.n, m, o:natH:[n; m] = [o; o]H1:n = oH2:m = o[o] = [o]forall n : nat, S n = 0 -> 2 + 2 = 5forall n : nat, S n = 0 -> 2 + 2 = 5inversion contra. Qed.n:natcontra:S n = 02 + 2 = 5
Here's how inversion works in general. Suppose the name
H refers to an assumption P in the current context, where P
has been defined by an Inductive declaration. Then, for each of
the constructors of P, inversion H generates a subgoal in which
H has been replaced by the exact, specific conditions under
which this constructor could have been used to prove P. Some of
these subgoals will be self-contradictory; inversion throws
these away. The ones that are left represent the cases that must
be proved to establish the original goal. For those, inversion
adds all equations into the proof context that must hold of the
arguments given to P (e.g., S (S n') = n in the proof of
evSS_ev).
The ev_double exercise above shows that our new notion of
evenness is implied by the two earlier ones (since, by
even_bool_prop in chapter Logic, we already know that
those are equivalent to each other). To show that all three
coincide, we just need the following lemma.
forall n : nat, even n -> exists k : nat, n = double k(* WORKED IN CLASS *)forall n : nat, even n -> exists k : nat, n = double k
We could try to proceed by case analysis or induction on n. But
since even is mentioned in a premise, this strategy would
probably lead to a dead end, as in the previous section. Thus, it
seems better to first try inversion on the evidence for even.
Indeed, the first case can be solved trivially.
n:natE:even nexists k : nat, n = double kn:natE:even nH:0 = nexists k : nat, 0 = double kn:natE:even nn':natE':even n'H:S (S n') = nexists k : nat, S (S n') = double kn:natE:even nH:0 = nexists k : nat, 0 = double kreflexivity.n:natE:even nH:0 = n0 = double 0n:natE:even nn':natE':even n'H:S (S n') = nexists k : nat, S (S n') = double kn:natE:even nn':natE':even n'H:S (S n') = nexists k : nat, S (S n') = double k
Unfortunately, the second case is harder. We need to show ∃
k, S (S n') = double k, but the only available assumption is
E', which states that even n' holds. Since this isn't
directly useful, it seems that we are stuck and that performing
case analysis on E was a waste of time.
If we look more closely at our second goal, however, we can see
that something interesting happened: By performing case analysis
on E, we were able to reduce the original result to a similar
one that involves a different piece of evidence for even:
namely E'. More formally, we can finish our proof by showing
that
exists k', n' = double k',
which is the same as the original statement, but with n' instead
of n. Indeed, it is not difficult to convince Coq that this
intermediate result suffices.
n:natE:even nn':natE':even n'H:S (S n') = n(exists k' : nat, n' = double k') -> exists k : nat, S (S n') = double kn:natE:even nn':natE':even n'H:S (S n') = nI:(exists k' : nat, n' = double k') -> exists k : nat, S (S n') = double kexists k : nat, S (S n') = double kn:natE:even nn':natE':even n'H:S (S n') = n(exists k' : nat, n' = double k') -> exists k : nat, S (S n') = double kn:natE:even nn':natE':even n'H:S (S n') = nk':natHk':n' = double k'exists k : nat, S (S n') = double kn:natE:even nn':natE':even n'H:S (S n') = nk':natHk':n' = double k'exists k : nat, S (S (double k')) = double kreflexivity.n:natE:even nn':natE':even n'H:S (S n') = nk':natHk':n' = double k'S (S (double k')) = double (S k')n:natE:even nn':natE':even n'H:S (S n') = nI:(exists k' : nat, n' = double k') -> exists k : nat, S (S n') = double kexists k : nat, S (S n') = double kAbort. (* ================================================================= *)n:natE:even nn':natE':even n'H:S (S n') = nI:(exists k' : nat, n' = double k') -> exists k : nat, S (S n') = double kexists k' : nat, n' = double k'
If this looks familiar, it is no coincidence: We've
encountered similar problems in the Induction chapter, when
trying to use case analysis to prove results that required
induction. And once again the solution is... induction!
The behavior of induction on evidence is the same as its
behavior on data: It causes Coq to generate one subgoal for each
constructor that could have used to build that evidence, while
providing an induction hypotheses for each recursive occurrence of
the property in question.
To prove a property of n holds for all numbers for which even
n holds, we can use induction on even n. This requires us to
prove two things, corresponding to the two ways in which even n
could have been constructed. If it was constructed by ev_0, then
n=0, and the property must hold of 0. If it was constructed by
ev_SS, then the evidence of even n is of the form ev_SS n'
E', where n = S (S n') and E' is evidence for even n'. In
this case, the inductive hypothesis says that the property we are
trying to prove holds for n'.
Let's try our current lemma again:
forall n : nat, even n -> exists k : nat, n = double kforall n : nat, even n -> exists k : nat, n = double kn:natE:even nexists k : nat, n = double kexists k : nat, 0 = double kn':natE':even n'IH:exists k : nat, n' = double kexists k : nat, S (S n') = double kexists k : nat, 0 = double kreflexivity.0 = double 0(* E = ev_SS n' E' with IH : exists k', n' = double k' *)n':natE':even n'IH:exists k : nat, n' = double kexists k : nat, S (S n') = double kn':natE':even n'k':natHk':n' = double k'exists k : nat, S (S n') = double kn':natE':even n'k':natHk':n' = double k'exists k : nat, S (S (double k')) = double kreflexivity. Qed.n':natE':even n'k':natHk':n' = double k'S (S (double k')) = double (S k')
Here, we can see that Coq produced an IH that corresponds
to E', the single recursive occurrence of even in its own
definition. Since E' mentions n', the induction hypothesis
talks about n', as opposed to n or some other number.
The equivalence between the second and third definitions of
evenness now follows.
forall n : nat, even n <-> (exists k : nat, n = double k)forall n : nat, even n <-> (exists k : nat, n = double k)n:nateven n <-> (exists k : nat, n = double k)n:nateven n -> exists k : nat, n = double kn:nat(exists k : nat, n = double k) -> even napply ev_even.n:nateven n -> exists k : nat, n = double kn:nat(exists k : nat, n = double k) -> even nn, k:natHk:n = double keven napply ev_double. Qed.n, k:natHk:n = double keven (double k)
As we will see in later chapters, induction on evidence is a
recurring technique across many areas, and in particular when
formalizing the semantics of programming languages, where many
properties of interest are defined inductively.
The following exercises provide simple examples of this
technique, to help you familiarize yourself with it.
forall n m : nat, even n -> even m -> even (n + m)(* FILL IN HERE *) Admitted.forall n m : nat, even n -> even m -> even (n + m)
☐
Exercise: 4 stars, advanced, optional (even'_ev)
Inductive even' : nat -> Prop :=
| even'_0 : even' 0
| even'_2 : even' 2
| even'_sum n m (Hn : even' n) (Hm : even' m) : even' (n + m).
Prove that this definition is logically equivalent to the old
one. (You may want to look at the previous theorem when you get
to the induction step.)
forall n : nat, even' n <-> even n(* FILL IN HERE *) Admitted.forall n : nat, even' n <-> even n
☐
Exercise: 3 stars, advanced, recommended (ev_ev__ev)
forall n m : nat, even (n + m) -> even n -> even m(* FILL IN HERE *) Admitted.forall n m : nat, even (n + m) -> even n -> even m
☐
Exercise: 3 stars, standard, optional (ev_plus_plus)
forall n m p : nat, even (n + m) -> even (n + p) -> even (m + p)(* FILL IN HERE *) Admitted.forall n m p : nat, even (n + m) -> even (n + p) -> even (m + p)
☐
(* ################################################################# *)
A proposition parameterized by a number (such as even)
can be thought of as a property -- 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.
Module Playground.
One useful example is the "less than or equal to" relation on
numbers.
The following 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.
Inductive le : nat -> nat -> Prop := | le_n n : le n n | le_S n m (H : le n m) : le n (S m). Notation "m <= n" := (le m n).
Proofs of facts about ≤ using the constructors le_n and
le_S follow the same patterns as proofs about properties, like
even above. We can apply the constructors to prove ≤
goals (e.g., to show that 3<=3 or 3<=6), and we can use
tactics like inversion to extract information from ≤
hypotheses in the context (e.g., to prove that (2 ≤ 1) →
2+2=5.)
Here are some sanity checks on the definition. (Notice that,
although these are the same kind of simple "unit tests" as we gave
for the testing functions we wrote in the first few lectures, we
must construct their proofs explicitly -- simpl and
reflexivity don't do the job, because the proofs aren't just a
matter of simplifying computations.)
3 <= 3(* WORKED IN CLASS *) apply le_n. Qed.3 <= 33 <= 6(* WORKED IN CLASS *)3 <= 63 <= 53 <= 4apply le_n. Qed.3 <= 32 <= 1 -> 2 + 2 = 5(* WORKED IN CLASS *)2 <= 1 -> 2 + 2 = 5H:2 <= 12 + 2 = 5inversion H2. Qed.H:2 <= 1n, m:natH2:2 <= 0H1:n = 2H0:m = 02 + 2 = 5
The "strictly less than" relation n < m can now be defined
in terms of le.
End Playground. Definition lt (n m:nat) := le (S n) m. Notation "m < n" := (lt m n).
Here are a few more simple relations on numbers:
Inductive square_of : nat -> nat -> Prop := | sq n : square_of n (n * n). Inductive next_nat : nat -> nat -> Prop := | nn n : next_nat n (S n). Inductive next_even : nat -> nat -> Prop := | ne_1 n : even (S n) -> next_even n (S n) | ne_2 n (H : even (S (S n))) : next_even n (S (S n)).
Exercise: 2 stars, standard, optional (total_relation)
(* FILL IN HERE
[] *)
Exercise: 2 stars, standard, optional (empty_relation)
(* FILL IN HERE
[] *)
From the definition of le, we can sketch the behaviors of
destruct, inversion, and induction on a hypothesis H
providing evidence of the form le e1 e2. Doing destruct H
will generate two cases. In the first case, e1 = e2, and it
will replace instances of e2 with e1 in the goal and context.
In the second case, e2 = S n' for some n' for which le e1 n'
holds, and it will replace instances of e2 with S n'.
Doing inversion H will remove impossible cases and add generated
equalities to the context for further use. Doing induction H
will, in the second case, add the induction hypothesis that the
goal holds when e2 is replaced with n'.
Exercise: 3 stars, standard, optional (le_exercises)
forall m n o : nat, m <= n -> n <= o -> m <= o(* FILL IN HERE *) Admitted.forall m n o : nat, m <= n -> n <= o -> m <= oforall n : nat, 0 <= n(* FILL IN HERE *) Admitted.forall n : nat, 0 <= nforall n m : nat, n <= m -> S n <= S m(* FILL IN HERE *) Admitted.forall n m : nat, n <= m -> S n <= S mforall n m : nat, S n <= S m -> n <= m(* FILL IN HERE *) Admitted.forall n m : nat, S n <= S m -> n <= mforall a b : nat, a <= a + b(* FILL IN HERE *) Admitted.forall a b : nat, a <= a + bforall n1 n2 m : nat, n1 + n2 < m -> n1 < m /\ n2 < mforall n1 n2 m : nat, n1 + n2 < m -> n1 < m /\ n2 < m(* FILL IN HERE *) Admitted.forall n1 n2 m : nat, S (n1 + n2) <= m -> S n1 <= m /\ S n2 <= mforall n m : nat, n < m -> n < S m(* FILL IN HERE *) Admitted.forall n m : nat, n < m -> n < S mforall n m : nat, (n <=? m) = true -> n <= m(* FILL IN HERE *) Admitted.forall n m : nat, (n <=? m) = true -> n <= m
Hint: The next one may be easiest to prove by induction on m.
forall n m : nat, n <= m -> (n <=? m) = true(* FILL IN HERE *) Admitted.forall n m : nat, n <= m -> (n <=? m) = true
Hint: This one can easily be proved without using induction.
forall n m o : nat, (n <=? m) = true -> (m <=? o) = true -> (n <=? o) = true(* FILL IN HERE *) Admitted.forall n m o : nat, (n <=? m) = true -> (m <=? o) = true -> (n <=? o) = true
☐
forall n m : nat, (n <=? m) = true <-> n <= m(* FILL IN HERE *) Admitted.forall n m : nat, (n <=? m) = true <-> n <= m
☐
Module R.
Exercise: 3 stars, standard, recommended (R_provability)
Inductive R : nat -> nat -> nat -> Prop :=
| c1 : R 0 0 0
| c2 m n o (H : R m n o) : R (S m) n (S o)
| c3 m n o (H : R m n o) : R m (S n) (S o)
| c4 m n o (H : R (S m) (S n) (S (S o))) : R m n o
| c5 m n o (H : R m n o) : R n m o.
- Which of the following propositions are provable?
- R 1 1 2
- R 2 2 6
- If we dropped constructor c5 from the definition of R,
would the set of provable propositions change? Briefly (1
sentence) explain your answer.
- If we dropped constructor c4 from the definition of R, would the set of provable propositions change? Briefly (1 sentence) explain your answer.
(* Do not modify the following line: *) Definition manual_grade_for_R_provability : option (nat*string) := None.
☐
Exercise: 3 stars, standard, optional (R_fact)
Admitted.nat -> nat -> natforall m n o : nat, R m n o <-> fR m n = o(* FILL IN HERE *) Admitted.forall m n o : nat, R m n o <-> fR m n = o
☐
End R.
Exercise: 2 stars, advanced (subsequence)
- Define an inductive proposition subseq on list nat that
captures what it means to be a subsequence. (Hint: You'll need
three cases.)
- Prove subseq_refl that subsequence is reflexive, that is,
any list is a subsequence of itself.
- Prove subseq_app that for any lists l1, l2, and l3,
if l1 is a subsequence of l2, then l1 is also a subsequence
of l2 ++ l3.
- (Optional, harder) Prove subseq_trans that subsequence is transitive -- that is, if l1 is a subsequence of l2 and l2 is a subsequence of l3, then l1 is a subsequence of l3. Hint: choose your induction carefully!
Inductive subseq : list nat -> list nat -> Prop := (* FILL IN HERE *) .forall l : list nat, subseq l l(* FILL IN HERE *) Admitted.forall l : list nat, subseq l lforall l1 l2 l3 : list nat, subseq l1 l2 -> subseq l1 (l2 ++ l3)(* FILL IN HERE *) Admitted.forall l1 l2 l3 : list nat, subseq l1 l2 -> subseq l1 (l2 ++ l3)forall l1 l2 l3 : list nat, subseq l1 l2 -> subseq l2 l3 -> subseq l1 l3(* FILL IN HERE *) Admitted.forall l1 l2 l3 : list nat, subseq l1 l2 -> subseq l2 l3 -> subseq l1 l3
☐
Exercise: 2 stars, standard, optional (R_provability2)
- R 2 [1;0]
- R 1 [1;2;1;0]
- R 6 [3;2;1;0]
(* FILL IN HERE
[] *)
(* ################################################################# *)
The even property provides a simple example for
illustrating inductive definitions and the basic techniques for
reasoning about them, but it is not terribly exciting -- after
all, it is equivalent to the two non-inductive definitions of
evenness that we had already seen, and does not seem to offer any
concrete benefit over them.
To give a better sense of the power of inductive definitions, we
now show how to use them to model a classic concept in computer
science: regular expressions.
Regular expressions are a simple language for describing sets of
strings. Their syntax is defined as follows:
Inductive reg_exp {T : Type} : Type :=
| EmptySet
| EmptyStr
| Char (t : T)
| App (r1 r2 : reg_exp)
| Union (r1 r2 : reg_exp)
| Star (r : reg_exp).
Note that this definition is polymorphic: Regular
expressions in reg_exp T describe strings with characters drawn
from T -- that is, lists of elements of T.
(We depart slightly from standard practice in that we do not
require the type T to be finite. This results in a somewhat
different theory of regular expressions, but the difference is not
significant for our purposes.)
We connect regular expressions and strings via the following
rules, which define when a regular expression matches some
string:
- The expression EmptySet does not match any string.
- The expression EmptyStr matches the empty string [].
- The expression Char x matches the one-character string [x].
- If re1 matches s1, and re2 matches s2,
then App re1 re2 matches s1 ++ s2.
- If at least one of re1 and re2 matches s,
then Union re1 re2 matches s.
- Finally, if we can write some string s as the concatenation
of a sequence of strings s = s_1 ++ ... ++ s_k, and the
expression re matches each one of the strings s_i,
then Star re matches s.
We can easily translate this informal definition into an
Inductive one as follows:
Inductive exp_match {T} : list T -> reg_exp -> Prop :=
| MEmpty : exp_match [] EmptyStr
| MChar x : exp_match [x] (Char x)
| MApp s1 re1 s2 re2
(H1 : exp_match s1 re1)
(H2 : exp_match s2 re2) :
exp_match (s1 ++ s2) (App re1 re2)
| MUnionL s1 re1 re2
(H1 : exp_match s1 re1) :
exp_match s1 (Union re1 re2)
| MUnionR re1 s2 re2
(H2 : exp_match s2 re2) :
exp_match s2 (Union re1 re2)
| MStar0 re : exp_match [] (Star re)
| MStarApp s1 s2 re
(H1 : exp_match s1 re)
(H2 : exp_match s2 (Star re)) :
exp_match (s1 ++ s2) (Star re).
Again, for readability, we can also display this definition using
inference-rule notation. At the same time, let's introduce a more
readable infix notation.
Notation "s =~ re" := (exp_match s re) (at level 80).
(MEmpty) ☐ =~ EmptyStr
(MChar) x =~ Char x
(MApp) s1 ++ s2 =~ App re1 re2
(MUnionL) s1 =~ Union re1 re2
(MUnionR) s2 =~ Union re1 re2
(MStar0) ☐ =~ Star re
(MStarApp) s1 ++ s2 =~ Star re
Notice that these rules are not quite the same as the
informal ones that we gave at the beginning of the section.
First, we don't need to include a rule explicitly stating that no
string matches EmptySet; we just don't happen to include any
rule that would have the effect of some string matching
EmptySet. (Indeed, the syntax of inductive definitions doesn't
even allow us to give such a "negative rule.")
Second, the informal rules for Union and Star correspond
to two constructors each: MUnionL / MUnionR, and MStar0 /
MStarApp. The result is logically equivalent to the original
rules but more convenient to use in Coq, since the recursive
occurrences of exp_match are given as direct arguments to the
constructors, making it easier to perform induction on evidence.
(The exp_match_ex1 and exp_match_ex2 exercises below ask you
to prove that the constructors given in the inductive declaration
and the ones that would arise from a more literal transcription of
the informal rules are indeed equivalent.)
Let's illustrate these rules with a few examples.
[1] =~ Char 1apply MChar. Qed.[1] =~ Char 1[1; 2] =~ App (Char 1) (Char 2)[1; 2] =~ App (Char 1) (Char 2)[1] =~ Char 1[2] =~ Char 2apply MChar.[1] =~ Char 1apply MChar. Qed.[2] =~ Char 2
(Notice how the last example applies MApp to the strings
[1] and [2] directly. Since the goal mentions [1; 2]
instead of [1] ++ [2], Coq wouldn't be able to figure out how to
split the string on its own.)
Using inversion, we can also show that certain strings do not
match a regular expression:
~ ([1; 2] =~ Char 1)~ ([1; 2] =~ Char 1)inversion H. Qed.H:[1; 2] =~ Char 1False
We can define helper functions for writing down regular
expressions. The reg_exp_of_list function constructs a regular
expression that matches exactly the list that it receives as an
argument:
Fixpoint reg_exp_of_list {T} (l : list T) := match l with | [] => EmptyStr | x :: l' => App (Char x) (reg_exp_of_list l') end.[1; 2; 3] =~ reg_exp_of_list [1; 2; 3][1; 2; 3] =~ reg_exp_of_list [1; 2; 3][1; 2; 3] =~ App (Char 1) (App (Char 2) (App (Char 3) EmptyStr))[1] =~ Char 1[2; 3] =~ App (Char 2) (App (Char 3) EmptyStr)apply MChar.[1] =~ Char 1[2; 3] =~ App (Char 2) (App (Char 3) EmptyStr)[2] =~ Char 2[3] =~ App (Char 3) EmptyStrapply MChar.[2] =~ Char 2[3] =~ App (Char 3) EmptyStr[3] =~ Char 3[ ] =~ EmptyStrapply MChar.[3] =~ Char 3apply MEmpty. Qed.[ ] =~ EmptyStr
We can also prove general facts about exp_match. For instance,
the following lemma shows that every string s that matches re
also matches Star re.
forall (T : Type) (s : list T) (re : reg_exp), s =~ re -> s =~ Star reforall (T : Type) (s : list T) (re : reg_exp), s =~ re -> s =~ Star reT:Types:list Tre:reg_expH:s =~ res =~ Star reT:Types:list Tre:reg_expH:s =~ res ++ [ ] =~ Star reT:Types:list Tre:reg_expH:s =~ res =~ reT:Types:list Tre:reg_expH:s =~ re[ ] =~ Star reapply H.T:Types:list Tre:reg_expH:s =~ res =~ reapply MStar0. Qed.T:Types:list Tre:reg_expH:s =~ re[ ] =~ Star re
(Note the use of app_nil_r to change the goal of the theorem to
exactly the same shape expected by MStarApp.)
Exercise: 3 stars, standard (exp_match_ex1)
forall (T : Type) (s : list T), ~ (s =~ EmptySet)(* FILL IN HERE *) Admitted.forall (T : Type) (s : list T), ~ (s =~ EmptySet)forall (T : Type) (s : list T) (re1 re2 : reg_exp), s =~ re1 \/ s =~ re2 -> s =~ Union re1 re2(* FILL IN HERE *) Admitted.forall (T : Type) (s : list T) (re1 re2 : reg_exp), s =~ re1 \/ s =~ re2 -> s =~ Union re1 re2
The next lemma is stated in terms of the fold function from the
Poly chapter: If ss : list (list T) represents a sequence of
strings s1, ..., sn, then fold app ss [] is the result of
concatenating them all together.
forall (T : Type) (ss : list (list T)) (re : reg_exp), (forall s : list T, In s ss -> s =~ re) -> fold app ss [ ] =~ Star re(* FILL IN HERE *) Admitted.forall (T : Type) (ss : list (list T)) (re : reg_exp), (forall s : list T, In s ss -> s =~ re) -> fold app ss [ ] =~ Star re
☐
Exercise: 4 stars, standard, optional (reg_exp_of_list_spec)
forall (T : Type) (s1 s2 : list T), s1 =~ reg_exp_of_list s2 <-> s1 = s2(* FILL IN HERE *) Admitted.forall (T : Type) (s1 s2 : list T), s1 =~ reg_exp_of_list s2 <-> s1 = s2
☐
Since the definition of exp_match has a recursive
structure, we might expect that proofs involving regular
expressions will often require induction on evidence.
For example, suppose that we wanted to prove the following
intuitive result: If a regular expression re matches some string
s, then all elements of s must occur as character literals
somewhere in re.
To state this theorem, we first define a function re_chars that
lists all characters that occur in a regular expression:
Fixpoint re_chars {T} (re : reg_exp) : list T :=
match re with
| EmptySet => []
| EmptyStr => []
| Char x => [x]
| App re1 re2 => re_chars re1 ++ re_chars re2
| Union re1 re2 => re_chars re1 ++ re_chars re2
| Star re => re_chars re
end.
We can then phrase our theorem as follows:
forall (T : Type) (s : list T) (re : reg_exp) (x : T), s =~ re -> In x s -> In x (re_chars re)forall (T : Type) (s : list T) (re : reg_exp) (x : T), s =~ re -> In x s -> In x (re_chars re)T:Types:list Tre:reg_expx:THmatch:s =~ reHin:In x sIn x (re_chars re)(* WORKED IN CLASS *)T:Typex:THin:In x [ ]In x (re_chars EmptyStr)T:Typex, x':THin:In x [x']In x (re_chars (Char x'))T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x (s1 ++ s2)IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars (App re1 re2))T:Typex:Ts1:list Tre1, re2:reg_expHmatch:s1 =~ re1Hin:In x s1IH:In x s1 -> In x (re_chars re1)In x (re_chars (Union re1 re2))T:Typex:Tre1:reg_exps2:list Tre2:reg_expHmatch:s2 =~ re2Hin:In x s2IH:In x s2 -> In x (re_chars re2)In x (re_chars (Union re1 re2))T:Typex:Tre:reg_expHin:In x [ ]In x (re_chars (Star re))T:Typex:Ts1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reHin:In x (s1 ++ s2)IH1:In x s1 -> In x (re_chars re)IH2:In x s2 -> In x (re_chars (Star re))In x (re_chars (Star re))apply Hin.T:Typex:THin:In x [ ]In x (re_chars EmptyStr)apply Hin.T:Typex, x':THin:In x [x']In x (re_chars (Char x'))T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x (s1 ++ s2)IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars (App re1 re2))T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x (s1 ++ s2)IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars re1 ++ re_chars re2)T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x s1 \/ In x s2IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars re1) \/ In x (re_chars re2)T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x s1IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars re1) \/ In x (re_chars re2)T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x s2IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars re1) \/ In x (re_chars re2)T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x s1IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars re1) \/ In x (re_chars re2)apply (IH1 Hin).T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x s1IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars re1)T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x s2IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars re1) \/ In x (re_chars re2)apply (IH2 Hin).T:Typex:Ts1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2Hin:In x s2IH1:In x s1 -> In x (re_chars re1)IH2:In x s2 -> In x (re_chars re2)In x (re_chars re2)T:Typex:Ts1:list Tre1, re2:reg_expHmatch:s1 =~ re1Hin:In x s1IH:In x s1 -> In x (re_chars re1)In x (re_chars (Union re1 re2))T:Typex:Ts1:list Tre1, re2:reg_expHmatch:s1 =~ re1Hin:In x s1IH:In x s1 -> In x (re_chars re1)In x (re_chars re1 ++ re_chars re2)T:Typex:Ts1:list Tre1, re2:reg_expHmatch:s1 =~ re1Hin:In x s1IH:In x s1 -> In x (re_chars re1)In x (re_chars re1) \/ In x (re_chars re2)apply (IH Hin).T:Typex:Ts1:list Tre1, re2:reg_expHmatch:s1 =~ re1Hin:In x s1IH:In x s1 -> In x (re_chars re1)In x (re_chars re1)T:Typex:Tre1:reg_exps2:list Tre2:reg_expHmatch:s2 =~ re2Hin:In x s2IH:In x s2 -> In x (re_chars re2)In x (re_chars (Union re1 re2))T:Typex:Tre1:reg_exps2:list Tre2:reg_expHmatch:s2 =~ re2Hin:In x s2IH:In x s2 -> In x (re_chars re2)In x (re_chars re1 ++ re_chars re2)T:Typex:Tre1:reg_exps2:list Tre2:reg_expHmatch:s2 =~ re2Hin:In x s2IH:In x s2 -> In x (re_chars re2)In x (re_chars re1) \/ In x (re_chars re2)apply (IH Hin).T:Typex:Tre1:reg_exps2:list Tre2:reg_expHmatch:s2 =~ re2Hin:In x s2IH:In x s2 -> In x (re_chars re2)In x (re_chars re2)destruct Hin.T:Typex:Tre:reg_expHin:In x [ ]In x (re_chars (Star re))
Something interesting happens in the MStarApp case. We obtain
two induction hypotheses: One that applies when x occurs in
s1 (which matches re), and a second one that applies when x
occurs in s2 (which matches Star re). This is a good
illustration of why we need induction on evidence for exp_match,
rather than induction on the regular expression re: The latter
would only provide an induction hypothesis for strings that match
re, which would not allow us to reason about the case In x
s2.
T:Typex:Ts1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reHin:In x (s1 ++ s2)IH1:In x s1 -> In x (re_chars re)IH2:In x s2 -> In x (re_chars (Star re))In x (re_chars (Star re))T:Typex:Ts1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reHin:In x (s1 ++ s2)IH1:In x s1 -> In x (re_chars re)IH2:In x s2 -> In x (re_chars (Star re))In x (re_chars re)T:Typex:Ts1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reHin:In x s1 \/ In x s2IH1:In x s1 -> In x (re_chars re)IH2:In x s2 -> In x (re_chars (Star re))In x (re_chars re)T:Typex:Ts1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reHin:In x s1IH1:In x s1 -> In x (re_chars re)IH2:In x s2 -> In x (re_chars (Star re))In x (re_chars re)T:Typex:Ts1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reHin:In x s2IH1:In x s1 -> In x (re_chars re)IH2:In x s2 -> In x (re_chars (Star re))In x (re_chars re)apply (IH1 Hin).T:Typex:Ts1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reHin:In x s1IH1:In x s1 -> In x (re_chars re)IH2:In x s2 -> In x (re_chars (Star re))In x (re_chars re)apply (IH2 Hin). Qed.T:Typex:Ts1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reHin:In x s2IH1:In x s1 -> In x (re_chars re)IH2:In x s2 -> In x (re_chars (Star re))In x (re_chars re)
Exercise: 4 stars, standard (re_not_empty)
Admitted.re_not_empty:forall T0 : Type, reg_exp -> boolT:Typere:reg_expboolforall (T : Type) (re : reg_exp), (exists s : list T, s =~ re) <-> re_not_empty re = true(* FILL IN HERE *) Admitted.forall (T : Type) (re : reg_exp), (exists s : list T, s =~ re) <-> re_not_empty re = true
☐
(* ================================================================= *)
One potentially confusing feature of the induction tactic is
that it will let you try to perform an induction over a term that
isn't sufficiently general. The effect of this is to lose
information (much as destruct without an eqn: clause can do),
and leave you unable to complete the proof. Here's an example:
forall (T : Type) (s1 s2 : list T) (re : reg_exp), s1 =~ Star re -> s2 =~ Star re -> s1 ++ s2 =~ Star reforall (T : Type) (s1 s2 : list T) (re : reg_exp), s1 =~ Star re -> s2 =~ Star re -> s1 ++ s2 =~ Star reT:Types1, s2:list Tre:reg_expH1:s1 =~ Star res2 =~ Star re -> s1 ++ s2 =~ Star re
Just doing an inversion on H1 won't get us very far in
the recursive cases. (Try it!). So we need induction (on
evidence!). Here is a naive first attempt:
T:Types2:list Tre:reg_exps2 =~ EmptyStr -> [ ] ++ s2 =~ EmptyStrT:Types2:list Tre:reg_expx':Ts2 =~ Char x' -> [x'] ++ s2 =~ Char x'T:Types2:list Tre:reg_exps1:list Tre1:reg_exps2':list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2' =~ re2IH1:s2 =~ re1 -> s1 ++ s2 =~ re1IH2:s2 =~ re2 -> s2' ++ s2 =~ re2s2 =~ App re1 re2 -> (s1 ++ s2') ++ s2 =~ App re1 re2T:Types2:list Tre:reg_exps1:list Tre1, re2:reg_expHmatch:s1 =~ re1IH:s2 =~ re1 -> s1 ++ s2 =~ re1s2 =~ Union re1 re2 -> s1 ++ s2 =~ Union re1 re2T:Types2:list Tre, re1:reg_exps2':list Tre2:reg_expHmatch:s2' =~ re2IH:s2 =~ re2 -> s2' ++ s2 =~ re2s2 =~ Union re1 re2 -> s2' ++ s2 =~ Union re1 re2T:Types2:list Tre, re'':reg_exps2 =~ Star re'' -> [ ] ++ s2 =~ Star re''T:Types2:list Tre:reg_exps1, s2':list Tre'':reg_expHmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:s2 =~ re'' -> s1 ++ s2 =~ re''IH2:s2 =~ Star re'' -> s2' ++ s2 =~ Star re''s2 =~ Star re'' -> (s1 ++ s2') ++ s2 =~ Star re''
But now, although we get seven cases (as we would expect from the
definition of exp_match), we have lost a very important bit of
information from H1: the fact that s1 matched something of the
form Star re. This means that we have to give proofs for all
seven constructors of this definition, even though all but two of
them (MStar0 and MStarApp) are contradictory. We can still
get the proof to go through for a few constructors, such as
MEmpty...
T:Types2:list Tre:reg_exps2 =~ EmptyStr -> [ ] ++ s2 =~ EmptyStrT:Types2:list Tre:reg_exps2 =~ EmptyStr -> s2 =~ EmptyStrapply H.T:Types2:list Tre:reg_expH:s2 =~ EmptyStrs2 =~ EmptyStr
... but most cases get stuck. For MChar, for instance, we
must show that
s2 =~ Char x' -> x' :: s2 =~ Char x',
which is clearly impossible.
Abort.T:Types2:list Tre:reg_expx':Ts2 =~ Char x' -> [x'] ++ s2 =~ Char x'
The problem is that induction over a Prop hypothesis only works
properly with hypotheses that are completely general, i.e., ones
in which all the arguments are variables, as opposed to more
complex expressions, such as Star re.
(In this respect, induction on evidence behaves more like
destruct-without-eqn: than like inversion.)
An awkward way to solve this problem is "manually generalizing"
over the problematic expressions by adding explicit equality
hypotheses to the lemma:
forall (T : Type) (s1 s2 : list T) (re re' : reg_exp),
re' = Star re ->
s1 =~ re' -> s2 =~ Star re -> s1 ++ s2 =~ Star re
We can now proceed by performing induction over evidence directly,
because the argument to the first hypothesis is sufficiently
general, which means that we can discharge most cases by inverting
the re' = Star re equality in the context.
This idiom is so common that Coq provides a tactic to
automatically generate such equations for us, avoiding thus the
need for changing the statements of our theorems.
Abort.
The tactic remember e as x causes Coq to (1) replace all
occurrences of the expression e by the variable x, and (2) add
an equation x = e to the context. Here's how we can use it to
show the above result:
forall (T : Type) (s1 s2 : list T) (re : reg_exp), s1 =~ Star re -> s2 =~ Star re -> s1 ++ s2 =~ Star reforall (T : Type) (s1 s2 : list T) (re : reg_exp), s1 =~ Star re -> s2 =~ Star re -> s1 ++ s2 =~ Star reT:Types1, s2:list Tre:reg_expH1:s1 =~ Star res2 =~ Star re -> s1 ++ s2 =~ Star reT:Types1, s2:list Tre, re':reg_expHeqre':re' = Star reH1:s1 =~ re's2 =~ re' -> s1 ++ s2 =~ re'
We now have Heqre' : re' = Star re.
T:Types1:list Tre, re':reg_expHeqre':re' = Star reH1:s1 =~ re'forall s2 : list T, s2 =~ re' -> s1 ++ s2 =~ re'T:Typere:reg_expHeqre':EmptyStr = Star reforall s2 : list T, s2 =~ EmptyStr -> [ ] ++ s2 =~ EmptyStrT:Typere:reg_expx':THeqre':Char x' = Star reforall s2 : list T, s2 =~ Char x' -> [x'] ++ s2 =~ Char x'T:Typere, re1, re2:reg_expHeqre':App re1 re2 = Star res1, s2':list THmatch1:s1 =~ re1Hmatch2:s2' =~ re2IH1:re1 = Star re -> forall s2 : list T, s2 =~ re1 -> s1 ++ s2 =~ re1IH2:re2 = Star re -> forall s2 : list T, s2 =~ re2 -> s2' ++ s2 =~ re2forall s2 : list T, s2 =~ App re1 re2 -> (s1 ++ s2') ++ s2 =~ App re1 re2T:Typere, re1, re2:reg_expHeqre':Union re1 re2 = Star res1:list THmatch:s1 =~ re1IH:re1 = Star re -> forall s2 : list T, s2 =~ re1 -> s1 ++ s2 =~ re1forall s2 : list T, s2 =~ Union re1 re2 -> s1 ++ s2 =~ Union re1 re2T:Typere, re1, re2:reg_expHeqre':Union re1 re2 = Star res2':list THmatch:s2' =~ re2IH:re2 = Star re -> forall s2 : list T, s2 =~ re2 -> s2' ++ s2 =~ re2forall s2 : list T, s2 =~ Union re1 re2 -> s2' ++ s2 =~ Union re1 re2T:Typere, re'':reg_expHeqre':Star re'' = Star reforall s2 : list T, s2 =~ Star re'' -> [ ] ++ s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s2 : list T, s2 =~ re'' -> s1 ++ s2 =~ re''IH2:Star re'' = Star re -> forall s2 : list T, s2 =~ Star re'' -> s2' ++ s2 =~ Star re''forall s2 : list T, s2 =~ Star re'' -> (s1 ++ s2') ++ s2 =~ Star re''
The Heqre' is contradictory in most cases, allowing us to
conclude immediately.
discriminate.T:Typere:reg_expHeqre':EmptyStr = Star reforall s2 : list T, s2 =~ EmptyStr -> [ ] ++ s2 =~ EmptyStrdiscriminate.T:Typere:reg_expx':THeqre':Char x' = Star reforall s2 : list T, s2 =~ Char x' -> [x'] ++ s2 =~ Char x'discriminate.T:Typere, re1, re2:reg_expHeqre':App re1 re2 = Star res1, s2':list THmatch1:s1 =~ re1Hmatch2:s2' =~ re2IH1:re1 = Star re -> forall s2 : list T, s2 =~ re1 -> s1 ++ s2 =~ re1IH2:re2 = Star re -> forall s2 : list T, s2 =~ re2 -> s2' ++ s2 =~ re2forall s2 : list T, s2 =~ App re1 re2 -> (s1 ++ s2') ++ s2 =~ App re1 re2discriminate.T:Typere, re1, re2:reg_expHeqre':Union re1 re2 = Star res1:list THmatch:s1 =~ re1IH:re1 = Star re -> forall s2 : list T, s2 =~ re1 -> s1 ++ s2 =~ re1forall s2 : list T, s2 =~ Union re1 re2 -> s1 ++ s2 =~ Union re1 re2discriminate.T:Typere, re1, re2:reg_expHeqre':Union re1 re2 = Star res2':list THmatch:s2' =~ re2IH:re2 = Star re -> forall s2 : list T, s2 =~ re2 -> s2' ++ s2 =~ re2forall s2 : list T, s2 =~ Union re1 re2 -> s2' ++ s2 =~ Union re1 re2
The interesting cases are those that correspond to Star. Note
that the induction hypothesis IH2 on the MStarApp case
mentions an additional premise Star re'' = Star re', which
results from the equality generated by remember.
T:Typere, re'':reg_expHeqre':Star re'' = Star reforall s2 : list T, s2 =~ Star re'' -> [ ] ++ s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star rere'' = re -> forall s2 : list T, s2 =~ Star re'' -> [ ] ++ s2 =~ Star re''apply H.T:Typere, re'':reg_expHeqre':Star re'' = Star reHeqre'':re'' = res:list TH:s =~ Star re''[ ] ++ s =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s2 : list T, s2 =~ re'' -> s1 ++ s2 =~ re''IH2:Star re'' = Star re -> forall s2 : list T, s2 =~ Star re'' -> s2' ++ s2 =~ Star re''forall s2 : list T, s2 =~ Star re'' -> (s1 ++ s2') ++ s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s2 : list T, s2 =~ re'' -> s1 ++ s2 =~ re''IH2:Star re'' = Star re -> forall s2 : list T, s2 =~ Star re'' -> s2' ++ s2 =~ Star re''re'' = re -> forall s2 : list T, s2 =~ Star re'' -> (s1 ++ s2') ++ s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s2 : list T, s2 =~ re'' -> s1 ++ s2 =~ re''IH2:Star re'' = Star re -> forall s2 : list T, s2 =~ Star re'' -> s2' ++ s2 =~ Star re''H0:re'' = reforall s2 : list T, s2 =~ Star re'' -> (s1 ++ s2') ++ s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''(s1 ++ s2') ++ s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''s1 ++ s2' ++ s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''s1 =~ re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''s2' ++ s2 =~ Star re''apply Hmatch1.T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''s1 =~ re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''s2' ++ s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''Star re'' = Star reT:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''s2 =~ Star re''T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''Star re'' = Star rereflexivity.T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''Star re = Star reapply H1. Qed.T:Typere, re'':reg_expHeqre':Star re'' = Star res1, s2':list THmatch1:s1 =~ re''Hmatch2:s2' =~ Star re''IH1:re'' = Star re -> forall s0 : list T, s0 =~ re'' -> s1 ++ s0 =~ re''IH2:Star re'' = Star re -> forall s0 : list T, s0 =~ Star re'' -> s2' ++ s0 =~ Star re''H0:re'' = res2:list TH1:s2 =~ Star re''s2 =~ Star re''
The MStar'' lemma below (combined with its converse, the
MStar' exercise above), shows that our definition of exp_match
for Star is equivalent to the informal one given previously.
forall (T : Type) (s : list T) (re : reg_exp), s =~ Star re -> exists ss : list (list T), s = fold app ss [ ] /\ (forall s' : list T, In s' ss -> s' =~ re)(* FILL IN HERE *) Admitted.forall (T : Type) (s : list T) (re : reg_exp), s =~ Star re -> exists ss : list (list T), s = fold app ss [ ] /\ (forall s' : list T, In s' ss -> s' =~ re)
☐
Exercise: 5 stars, advanced (pumping)
Module Pumping. Fixpoint pumping_constant {T} (re : @reg_exp T) : nat := match re with | EmptySet => 0 | EmptyStr => 1 | Char _ => 2 | App re1 re2 => pumping_constant re1 + pumping_constant re2 | Union re1 re2 => pumping_constant re1 + pumping_constant re2 | Star _ => 1 end.
Next, it is useful to define an auxiliary function that repeats a
string (appends it to itself) some number of times.
Fixpoint napp {T} (n : nat) (l : list T) : list T := match n with | 0 => [] | S n' => l ++ napp n' l end.forall (T : Type) (n m : nat) (l : list T), napp (n + m) l = napp n l ++ napp m lforall (T : Type) (n m : nat) (l : list T), napp (n + m) l = napp n l ++ napp m lT:Typen, m:natl:list Tnapp (n + m) l = napp n l ++ napp m lT:Typem:natl:list Tnapp (0 + m) l = napp 0 l ++ napp m lT:Typen, m:natl:list TIHn:napp (n + m) l = napp n l ++ napp m lnapp (S n + m) l = napp (S n) l ++ napp m lreflexivity.T:Typem:natl:list Tnapp (0 + m) l = napp 0 l ++ napp m lT:Typen, m:natl:list TIHn:napp (n + m) l = napp n l ++ napp m lnapp (S n + m) l = napp (S n) l ++ napp m lT:Typen, m:natl:list TIHn:napp (n + m) l = napp n l ++ napp m ll ++ napp (n + m) l = (l ++ napp n l) ++ napp m lreflexivity. Qed.T:Typen, m:natl:list TIHn:napp (n + m) l = napp n l ++ napp m l(l ++ napp n l) ++ napp m l = (l ++ napp n l) ++ napp m l
Now, the pumping lemma itself says that, if s =~ re and if the
length of s is at least the pumping constant of re, then s
can be split into three substrings s1 ++ s2 ++ s3 in such a way
that s2 can be repeated any number of times and the result, when
combined with s1 and s3 will still match re. Since s2 is
also guaranteed not to be the empty string, this gives us
a (constructive!) way to generate strings matching re that are
as long as we like.
forall (T : Type) (re : reg_exp) (s : list T),
s =~ re ->
pumping_constant re <= length s ->
exists s1 s2 s3 : list T,
s = s1 ++ s2 ++ s3 /\
s2 <> [ ] /\
(forall m : nat, s1 ++ napp m s2 ++ s3 =~ re)
To streamline the proof (which you are to fill in), the omega
tactic, which is enabled by the following Require, is helpful in
several places for automatically completing tedious low-level
arguments involving equalities or inequalities over natural
numbers. We'll return to omega in a later chapter, but feel
free to experiment with it now if you like. The first case of the
induction gives an example of how it is used.
forall (T : Type) (re : reg_exp) (s : list T), s =~ re -> pumping_constant re <= length s -> exists s1 s2 s3 : list T, s = s1 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s1 ++ napp m s2 ++ s3 =~ re)forall (T : Type) (re : reg_exp) (s : list T), s =~ re -> pumping_constant re <= length s -> exists s1 s2 s3 : list T, s = s1 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s1 ++ napp m s2 ++ s3 =~ re)T:Typere:reg_exps:list THmatch:s =~ repumping_constant re <= length s -> exists s1 s2 s3 : list T, s = s1 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s1 ++ napp m s2 ++ s3 =~ re)T:Typepumping_constant EmptyStr <= length [ ] -> exists s1 s2 s3 : list T, [ ] = s1 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s1 ++ napp m s2 ++ s3 =~ EmptyStr)T:Typex:Tpumping_constant (Char x) <= length [x] -> exists s1 s2 s3 : list T, [x] = s1 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s1 ++ napp m s2 ++ s3 =~ Char x)T:Types1:list Tre1:reg_exps2:list Tre2:reg_expHmatch1:s1 =~ re1Hmatch2:s2 =~ re2IH1:pumping_constant re1 <= length s1 -> exists s0 s3 s4 : list T, s1 = s0 ++ s3 ++ s4 /\ s3 <> [ ] /\ (forall m : nat, s0 ++ napp m s3 ++ s4 =~ re1)IH2:pumping_constant re2 <= length s2 -> exists s0 s3 s4 : list T, s2 = s0 ++ s3 ++ s4 /\ s3 <> [ ] /\ (forall m : nat, s0 ++ napp m s3 ++ s4 =~ re2)pumping_constant (App re1 re2) <= length (s1 ++ s2) -> exists s0 s3 s4 : list T, s1 ++ s2 = s0 ++ s3 ++ s4 /\ s3 <> [ ] /\ (forall m : nat, s0 ++ napp m s3 ++ s4 =~ App re1 re2)T:Types1:list Tre1, re2:reg_expHmatch:s1 =~ re1IH:pumping_constant re1 <= length s1 -> exists s0 s2 s3 : list T, s1 = s0 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s0 ++ napp m s2 ++ s3 =~ re1)pumping_constant (Union re1 re2) <= length s1 -> exists s0 s2 s3 : list T, s1 = s0 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s0 ++ napp m s2 ++ s3 =~ Union re1 re2)T:Typere1:reg_exps2:list Tre2:reg_expHmatch:s2 =~ re2IH:pumping_constant re2 <= length s2 -> exists s1 s0 s3 : list T, s2 = s1 ++ s0 ++ s3 /\ s0 <> [ ] /\ (forall m : nat, s1 ++ napp m s0 ++ s3 =~ re2)pumping_constant (Union re1 re2) <= length s2 -> exists s1 s0 s3 : list T, s2 = s1 ++ s0 ++ s3 /\ s0 <> [ ] /\ (forall m : nat, s1 ++ napp m s0 ++ s3 =~ Union re1 re2)T:Typere:reg_exppumping_constant (Star re) <= length [ ] -> exists s1 s2 s3 : list T, [ ] = s1 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s1 ++ napp m s2 ++ s3 =~ Star re)T:Types1, s2:list Tre:reg_expHmatch1:s1 =~ reHmatch2:s2 =~ Star reIH1:pumping_constant re <= length s1 -> exists s0 s3 s4 : list T, s1 = s0 ++ s3 ++ s4 /\ s3 <> [ ] /\ (forall m : nat, s0 ++ napp m s3 ++ s4 =~ re)IH2:pumping_constant (Star re) <= length s2 -> exists s0 s3 s4 : list T, s2 = s0 ++ s3 ++ s4 /\ s3 <> [ ] /\ (forall m : nat, s0 ++ napp m s3 ++ s4 =~ Star re)pumping_constant (Star re) <= length (s1 ++ s2) -> exists s0 s3 s4 : list T, s1 ++ s2 = s0 ++ s3 ++ s4 /\ s3 <> [ ] /\ (forall m : nat, s0 ++ napp m s3 ++ s4 =~ Star re)T:Typepumping_constant EmptyStr <= length [ ] -> exists s1 s2 s3 : list T, [ ] = s1 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s1 ++ napp m s2 ++ s3 =~ EmptyStr)omega. (* FILL IN HERE *) Admitted. End Pumping.T:Type1 <= 0 -> exists s1 s2 s3 : list T, [ ] = s1 ++ s2 ++ s3 /\ s2 <> [ ] /\ (forall m : nat, s1 ++ napp m s2 ++ s3 =~ EmptyStr)
☐
(* ################################################################# *)
We've seen in the Logic chapter that we often need to
relate boolean computations to statements in Prop. But
performing this conversion as we did it there can result in
tedious proof scripts. Consider the proof of the following
theorem:
forall (n : nat) (l : list nat), filter (fun x : nat => n =? x) l <> [ ] -> In n lforall (n : nat) (l : list nat), filter (fun x : nat => n =? x) l <> [ ] -> In n ln:natl:list natfilter (fun x : nat => n =? x) l <> [ ] -> In n ln:natfilter (fun x : nat => n =? x) [ ] <> [ ] -> In n [ ]n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'filter (fun x : nat => n =? x) (m :: l') <> [ ] -> In n (m :: l')n:natfilter (fun x : nat => n =? x) [ ] <> [ ] -> In n [ ]n:nat[ ] <> [ ] -> Falsen:natH:[ ] <> [ ]Falsereflexivity.n:natH:[ ] <> [ ][ ] = [ ]n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'filter (fun x : nat => n =? x) (m :: l') <> [ ] -> In n (m :: l')n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'(if n =? m then m :: filter (fun x : nat => n =? x) l' else filter (fun x : nat => n =? x) l') <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:(n =? m) = truem :: filter (fun x : nat => n =? x) l' <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:(n =? m) = falsefilter (fun x : nat => n =? x) l' <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:(n =? m) = truem :: filter (fun x : nat => n =? x) l' <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:(n =? m) = truem = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n = mm = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n = mm = m \/ In m l'reflexivity.n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n = mm = mn, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:(n =? m) = falsefilter (fun x : nat => n =? x) l' <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:(n =? m) = falseH':filter (fun x : nat => n =? x) l' <> [ ]m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:(n =? m) = falseH':filter (fun x : nat => n =? x) l' <> [ ]In n l'apply H'. Qed.n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:(n =? m) = falseH':filter (fun x : nat => n =? x) l' <> [ ]filter (fun x : nat => n =? x) l' <> [ ]
In the first branch after destruct, we explicitly apply
the eqb_eq lemma to the equation generated by
destructing n =? m, to convert the assumption n =? m
= true into the assumption n = m; then we had to rewrite
using this assumption to complete the case.
We can streamline this by defining an inductive proposition that
yields a better case-analysis principle for n =? m.
Instead of generating an equation such as (n =? m) = true,
which is generally not directly useful, this principle gives us
right away the assumption we really need: n = m.
Inductive reflect (P : Prop) : bool -> Prop :=
| ReflectT (H : P) : reflect P true
| ReflectF (H : ~ P) : reflect P false.
The reflect property takes two arguments: a proposition
P and a boolean b. Intuitively, it states that the property
P is reflected in (i.e., equivalent to) the boolean b: that
is, P holds if and only if b = true. To see this, notice
that, by definition, the only way we can produce evidence for
reflect P true is by showing P and then using the ReflectT
constructor. If we invert this statement, this means that it
should be possible to extract evidence for P from a proof of
reflect P true. Similarly, the only way to show reflect P
false is by combining evidence for ¬ P with the ReflectF
constructor.
It is easy to formalize this intuition and show that the
statements P ↔ b = true and reflect P b are indeed
equivalent. First, the left-to-right implication:
forall (P : Prop) (b : bool), P <-> b = true -> reflect P b(* WORKED IN CLASS *)forall (P : Prop) (b : bool), P <-> b = true -> reflect P bP:Propb:boolH:P <-> b = truereflect P bP:PropH:P <-> true = truereflect P trueP:PropH:P <-> false = truereflect P falseP:PropH:P <-> true = truereflect P trueP:PropH:P <-> true = truePreflexivity.P:PropH:P <-> true = truetrue = trueP:PropH:P <-> false = truereflect P falseP:PropH:P <-> false = true~ PP:PropH:P <-> false = truefalse <> truediscriminate. Qed.P:PropH:P <-> false = trueH':false = trueFalse
Now you prove the right-to-left implication:
forall (P : Prop) (b : bool), reflect P b -> P <-> b = true(* FILL IN HERE *) Admitted.forall (P : Prop) (b : bool), reflect P b -> P <-> b = true
☐
The advantage of reflect over the normal "if and only if"
connective is that, by destructing a hypothesis or lemma of the
form reflect P b, we can perform case analysis on b while at
the same time generating appropriate hypothesis in the two
branches (P in the first subgoal and ¬ P in the second).
forall n m : nat, reflect (n = m) (n =? m)forall n m : nat, reflect (n = m) (n =? m)n, m:natreflect (n = m) (n =? m)n, m:natn = m <-> (n =? m) = truereflexivity. Qed.n, m:natn = m <-> n = m
A smoother proof of filter_not_empty_In now goes as follows.
Notice how the calls to destruct and apply are combined into a
single call to destruct.
(To see this clearly, look at the two proofs of
filter_not_empty_In with Coq and observe the differences in
proof state at the beginning of the first case of the
destruct.)
forall (n : nat) (l : list nat), filter (fun x : nat => n =? x) l <> [ ] -> In n lforall (n : nat) (l : list nat), filter (fun x : nat => n =? x) l <> [ ] -> In n ln:natl:list natfilter (fun x : nat => n =? x) l <> [ ] -> In n ln:natfilter (fun x : nat => n =? x) [ ] <> [ ] -> In n [ ]n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'filter (fun x : nat => n =? x) (m :: l') <> [ ] -> In n (m :: l')n:natfilter (fun x : nat => n =? x) [ ] <> [ ] -> In n [ ]n:nat[ ] <> [ ] -> Falsen:natH:[ ] <> [ ]Falsereflexivity.n:natH:[ ] <> [ ][ ] = [ ]n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'filter (fun x : nat => n =? x) (m :: l') <> [ ] -> In n (m :: l')n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'(if n =? m then m :: filter (fun x : nat => n =? x) l' else filter (fun x : nat => n =? x) l') <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n = mm :: filter (fun x : nat => n =? x) l' <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n <> mfilter (fun x : nat => n =? x) l' <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n = mm :: filter (fun x : nat => n =? x) l' <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n = mm = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n = mm = m \/ In m l'reflexivity.n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n = mm = mn, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n <> mfilter (fun x : nat => n =? x) l' <> [ ] -> m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n <> mH':filter (fun x : nat => n =? x) l' <> [ ]m = n \/ In n l'n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n <> mH':filter (fun x : nat => n =? x) l' <> [ ]In n l'apply H'. Qed.n, m:natl':list natIHl':filter (fun x : nat => n =? x) l' <> [ ] -> In n l'H:n <> mH':filter (fun x : nat => n =? x) l' <> [ ]filter (fun x : nat => n =? x) l' <> [ ]
Fixpoint count n l := match l with | [] => 0 | m :: l' => (if n =? m then 1 else 0) + count n l' end.forall (n : nat) (l : list nat), count n l = 0 -> ~ In n l(* FILL IN HERE *) Admitted.forall (n : nat) (l : list nat), count n l = 0 -> ~ In n l
☐
This small example shows how reflection gives us a small gain in
convenience; in larger developments, using reflect consistently
can often lead to noticeably shorter and clearer proof scripts.
We'll see many more examples in later chapters and in Programming
Language Foundations.
The use of the reflect property has been popularized by
SSReflect, a Coq library that has been used to formalize
important results in mathematics, including as the 4-color theorem
and the Feit-Thompson theorem. The name SSReflect stands for
small-scale reflection, i.e., the pervasive use of reflection to
simplify small proof steps with boolean computations.
(* ################################################################# *)
Exercise: 3 stars, standard, recommended (nostutter_defn)
Inductive nostutter {X:Type} : list X -> Prop :=
(* FILL IN HERE *)
.
Make sure each of these tests succeeds, but feel free to change
the suggested proof (in comments) if the given one doesn't work
for you. Your definition might be different from ours and still
be correct, in which case the examples might need a different
proof. (You'll notice that the suggested proofs use a number of
tactics we haven't talked about, to make them more robust to
different possible ways of defining nostutter. You can probably
just uncomment and use them as-is, but you can also prove each
example with more basic tactics.)
(* FILL IN HERE *) Admitted. (* Proof. repeat constructor; apply eqb_neq; auto. Qed. *)nostutter [3; 1; 4; 1; 5; 6](* FILL IN HERE *) Admitted. (* Proof. repeat constructor; apply eqb_neq; auto. Qed. *)nostutter [ ](* FILL IN HERE *) Admitted. (* Proof. repeat constructor; apply eqb_false; auto. Qed. *)nostutter [5](* FILL IN HERE *) Admitted. (* Proof. intro. repeat match goal with h: nostutter _ |- _ => inversion h; clear h; subst end. contradiction Hneq0; auto. Qed. *) (* Do not modify the following line: *) Definition manual_grade_for_nostutter : option (nat*string) := None.~ nostutter [3; 1; 1; 4]
☐
Exercise: 4 stars, advanced (filter_challenge)
(* FILL IN HERE *) (* Do not modify the following line: *) Definition manual_grade_for_filter_challenge : option (nat*string) := None.
☐
Exercise: 5 stars, advanced, optional (filter_challenge_2)
(* FILL IN HERE
[] *)
Exercise: 4 stars, standard, optional (palindromes)
- Define an inductive proposition pal on list X that
captures what it means to be a palindrome. (Hint: You'll need
three cases. Your definition should be based on the structure
of the list; just having a single constructor like
- Prove (pal_app_rev) that
- Prove (pal_rev that)
(* FILL IN HERE *) (* Do not modify the following line: *) Definition manual_grade_for_pal_pal_app_rev_pal_rev : option (nat*string) := None.
☐
Exercise: 5 stars, standard, optional (palindrome_converse)
(* FILL IN HERE
[] *)
Exercise: 4 stars, advanced, optional (NoDup)
(* Fixpoint In (A : Type) (x : A) (l : list A) : Prop :=
match l with
| [] => False
| x' :: l' => x' = x \/ In A x l'
end *)
Your first task is to use In to define a proposition disjoint X
l1 l2, which should be provable exactly when l1 and l2 are
lists (with elements of type X) that have no elements in
common.
(* FILL IN HERE *)
Next, use In to define an inductive proposition NoDup X
l, which should be provable exactly when l is a list (with
elements of type X) where every member is different from every
other. For example, NoDup nat [1;2;3;4] and NoDup
bool [] should be provable, while NoDup nat [1;2;1] and
NoDup bool [true;true] should not be.
(* FILL IN HERE *)
Finally, state and prove one or more interesting theorems relating
disjoint, NoDup and ++ (list append).
(* FILL IN HERE *) (* Do not modify the following line: *) Definition manual_grade_for_NoDup_disjoint_etc : option (nat*string) := None.
☐
Exercise: 4 stars, advanced, optional (pigeonhole_principle)
First prove an easy useful lemma.
forall (X : Type) (x : X) (l : list X), In x l -> exists l1 l2 : list X, l = l1 ++ x :: l2(* FILL IN HERE *) Admitted.forall (X : Type) (x : X) (l : list X), In x l -> exists l1 l2 : list X, l = l1 ++ x :: l2
Now define a property repeats such that repeats X l asserts
that l contains at least one repeated element (of type X).
Inductive repeats {X:Type} : list X -> Prop :=
(* FILL IN HERE *)
.
Now, here's a way to formalize the pigeonhole principle. Suppose
list l2 represents a list of pigeonhole labels, and list l1
represents the labels assigned to a list of items. If there are
more items than labels, at least two items must have the same
label -- i.e., list l1 must contain repeats.
This proof is much easier if you use the excluded_middle
hypothesis to show that In is decidable, i.e., ∀ x l, (In x
l) ∨ ¬ (In x l). However, it is also possible to make the proof
go through without assuming that In is decidable; if you
manage to do this, you will not need the excluded_middle
hypothesis.
forall (X : Type) (l1 l2 : list X), excluded_middle -> (forall x : X, In x l1 -> In x l2) -> length l2 < length l1 -> repeats l1forall (X : Type) (l1 l2 : list X), excluded_middle -> (forall x : X, In x l1 -> In x l2) -> length l2 < length l1 -> repeats l1X:Typel1:list Xforall l2 : list X, excluded_middle -> (forall x : X, In x l1 -> In x l2) -> length l2 < length l1 -> repeats l1(* FILL IN HERE *) Admitted. (* Do not modify the following line: *) Definition manual_grade_for_check_repeats : option (nat*string) := None.X:Typeforall l2 : list X, excluded_middle -> (forall x : X, In x [ ] -> In x l2) -> length l2 < length [ ] -> repeats [ ]X:Typex:Xl1':list XIHl1':forall l2 : list X, excluded_middle -> (forall x0 : X, In x0 l1' -> In x0 l2) -> length l2 < length l1' -> repeats l1'forall l2 : list X, excluded_middle -> (forall x0 : X, In x0 (x :: l1') -> In x0 l2) -> length l2 < length (x :: l1') -> repeats (x :: l1')
☐
(* ================================================================= *)
We have now defined a match relation over regular expressions and
polymorphic lists. We can use such a definition to manually prove that
a given regex matches a given string, but it does not give us a
program that we can run to determine a match autmatically.
It would be reasonable to hope that we can translate the definitions
of the inductive rules for constructing evidence of the match relation
into cases of a recursive function reflects the relation by recursing
on a given regex. However, it does not seem straightforward to define
such a function in which the given regex is a recursion variable
recognized by Coq. As a result, Coq will not accept that the function
always terminates.
Heavily-optimized regex matchers match a regex by translating a given
regex into a state machine and determining if the state machine
accepts a given string. However, regex matching can also be
implemented using an algorithm that operates purely on strings and
regexes without defining and maintaining additional datatypes, such as
state machines. We'll implemement such an algorithm, and verify that
its value reflects the match relation.
We will implement a regex matcher that matches strings represented
as lists of ASCII characters:
Require Export Coq.Strings.Ascii. Definition string := list ascii.
The Coq standard library contains a distinct inductive definition
of strings of ASCII characters. However, we will use the above
definition of strings as lists as ASCII characters in order to apply
the existing definition of the match relation.
We could also define a regex matcher over polymorphic lists, not lists
of ASCII characters specifically. The matching algorithm that we will
implement needs to be able to test equality of elements in a given
list, and thus needs to be given an equality-testing
function. Generalizing the definitions, theorems, and proofs that we
define for such a setting is a bit tedious, but workable.
The proof of correctness of the regex matcher will combine
properties of the regex-matching function with properties of the
match relation that do not depend on the matching function. We'll go
ahead and prove the latter class of properties now. Most of them have
straightforward proofs, which have been given to you, although there
are a few key lemmas that are left for you to prove.
Each provable Prop is equivalent to True.
forall P : Prop, P -> P <-> Trueforall P : Prop, P -> P <-> TrueP:PropH:PP <-> TrueP:PropH:PP -> TrueP:PropH:PTrue -> PP:PropH:PP -> Trueconstructor.P:PropH, H0:PTrueP:PropH:PTrue -> Papply H. Qed.P:PropH:PP
Each Prop whose negation is provable is equivalent to False.
forall P : Prop, ~ P -> P <-> Falseforall P : Prop, ~ P -> P <-> FalseP:PropH:~ PP <-> FalseP:PropH:~ PP -> FalseP:PropH:~ PFalse -> Papply H.P:PropH:~ PP -> FalseP:PropH:~ PFalse -> Pdestruct H0. Qed.P:PropH:~ PH0:FalseP
EmptySet matches no string.
forall s : string, s =~ EmptySet <-> Falseforall s : string, s =~ EmptySet <-> Falses:strings =~ EmptySet <-> Falses:string~ (s =~ EmptySet)s:strings =~ EmptySet -> Falseinversion H. Qed.s:stringH:s =~ EmptySetFalse
EmptyStr only matches the empty string.
forall s : string, s =~ EmptyStr <-> s = [ ]forall s : string, s =~ EmptyStr <-> s = [ ]s:strings =~ EmptyStr -> s = [ ]s:strings = [ ] -> s =~ EmptyStrs:strings =~ EmptyStr -> s = [ ]s:stringH:s =~ EmptyStrs = [ ]reflexivity.s:stringH:s =~ EmptyStrH0:[ ] = s[ ] = [ ]s:strings = [ ] -> s =~ EmptyStrs:stringH:s = [ ]s =~ EmptyStrapply MEmpty. Qed.s:stringH:s = [ ][ ] =~ EmptyStr
EmptyStr matches no non-empty string.
forall (a : ascii) (s : list ascii), a :: s =~ EmptyStr <-> Falseforall (a : ascii) (s : list ascii), a :: s =~ EmptyStr <-> Falsea:asciis:list asciia :: s =~ EmptyStr <-> Falsea:asciis:list ascii~ (a :: s =~ EmptyStr)a:asciis:list asciia :: s =~ EmptyStr -> Falseinversion H. Qed.a:asciis:list asciiH:a :: s =~ EmptyStrFalse
Char a matches no string that starts with a non-a character.
forall (a b : ascii) (s : list ascii), b <> a -> b :: s =~ Char a <-> Falseforall (a b : ascii) (s : list ascii), b <> a -> b :: s =~ Char a <-> Falsea, b:asciis:list asciiH:b <> ab :: s =~ Char a <-> Falsea, b:asciis:list asciiH:b <> a~ (b :: s =~ Char a)a, b:asciis:list asciiH:b <> ab :: s =~ Char a -> Falsea, b:asciis:list asciiH:b <> aH0:b :: s =~ Char aFalsea, b:asciis:list asciiH:b <> aH0:b :: s =~ Char ab = areflexivity. Qed.a, b:asciis:list asciiH:b <> aH0:b :: s =~ Char ax:asciiH2:x = bH3:[ ] = sH4:b = aa = a
If Char a matches a non-empty string, then the string's tail is empty.
forall (a : ascii) (s : list ascii), a :: s =~ Char a <-> s = [ ]forall (a : ascii) (s : list ascii), a :: s =~ Char a <-> s = [ ]a:asciis:list asciia :: s =~ Char a -> s = [ ]a:asciis:list asciis = [ ] -> a :: s =~ Char aa:asciis:list asciia :: s =~ Char a -> s = [ ]a:asciis:list asciiH:a :: s =~ Char as = [ ]reflexivity.a:asciis:list asciiH:a :: s =~ Char ax:asciiH1:x = aH2:[ ] = s[ ] = [ ]a:asciis:list asciis = [ ] -> a :: s =~ Char aa:asciis:list asciiH:s = [ ]a :: s =~ Char aapply MChar. Qed.a:asciis:list asciiH:s = [ ][a] =~ Char a
App re0 re1 matches string s iff s = s0 ++ s1, where s0
matches re0 and s1 matches re1.
forall (s : string) (re0 re1 : reg_exp), s =~ App re0 re1 <-> (exists s0 s1 : list ascii, s = s0 ++ s1 /\ (s0 =~ re0) /\ s1 =~ re1)forall (s : string) (re0 re1 : reg_exp), s =~ App re0 re1 <-> (exists s0 s1 : list ascii, s = s0 ++ s1 /\ (s0 =~ re0) /\ s1 =~ re1)s:stringre0, re1:reg_exps =~ App re0 re1 <-> (exists s0 s1 : list ascii, s = s0 ++ s1 /\ (s0 =~ re0) /\ s1 =~ re1)s:stringre0, re1:reg_exps =~ App re0 re1 -> exists s0 s1 : list ascii, s = s0 ++ s1 /\ (s0 =~ re0) /\ s1 =~ re1s:stringre0, re1:reg_exp(exists s0 s1 : list ascii, s = s0 ++ s1 /\ (s0 =~ re0) /\ s1 =~ re1) -> s =~ App re0 re1s:stringre0, re1:reg_exps =~ App re0 re1 -> exists s0 s1 : list ascii, s = s0 ++ s1 /\ (s0 =~ re0) /\ s1 =~ re1s:stringre0, re1:reg_expH:s =~ App re0 re1exists s0 s1 : list ascii, s = s0 ++ s1 /\ (s0 =~ re0) /\ s1 =~ re1s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1exists s0 s3 : list ascii, s1 ++ s2 = s0 ++ s3 /\ (s0 =~ re0) /\ s3 =~ re1s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1s1 ++ s2 = s1 ++ s2 /\ (s1 =~ re0) /\ s2 =~ re1s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1s1 ++ s2 = s1 ++ s2s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1(s1 =~ re0) /\ s2 =~ re1reflexivity.s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1s1 ++ s2 = s1 ++ s2s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1(s1 =~ re0) /\ s2 =~ re1s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1s1 =~ re0s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1s2 =~ re1apply H4.s:stringre0, re1:reg_expH:s =~ App re0 re1s1:list asciire2:reg_exps2:list asciire3:reg_expH3:s1 =~ re0H4:s2 =~ re1H0:s1 ++ s2 = sH1:re2 = re0H2:re3 = re1s2 =~ re1s:stringre0, re1:reg_exp(exists s0 s1 : list ascii, s = s0 ++ s1 /\ (s0 =~ re0) /\ s1 =~ re1) -> s =~ App re0 re1s:stringre0, re1:reg_exps0, s1:list asciiHapp:s = s0 ++ s1Hmat0:s0 =~ re0Hmat1:s1 =~ re1s =~ App re0 re1apply (MApp s0 _ s1 _ Hmat0 Hmat1). Qed.s:stringre0, re1:reg_exps0, s1:list asciiHapp:s = s0 ++ s1Hmat0:s0 =~ re0Hmat1:s1 =~ re1s0 ++ s1 =~ App re0 re1
Exercise: 3 stars, standard, optional (app_ne)
forall (a : ascii) (s : list ascii) (re0 re1 : reg_exp), a :: s =~ App re0 re1 <-> ([ ] =~ re0) /\ a :: s =~ re1 \/ (exists s0 s1 : list ascii, s = s0 ++ s1 /\ (a :: s0 =~ re0) /\ s1 =~ re1)(* FILL IN HERE *) Admitted.forall (a : ascii) (s : list ascii) (re0 re1 : reg_exp), a :: s =~ App re0 re1 <-> ([ ] =~ re0) /\ a :: s =~ re1 \/ (exists s0 s1 : list ascii, s = s0 ++ s1 /\ (a :: s0 =~ re0) /\ s1 =~ re1)
☐
s matches Union re0 re1 iff s matches re0 or s matches re1.
forall (s : string) (re0 re1 : reg_exp), s =~ Union re0 re1 <-> s =~ re0 \/ s =~ re1forall (s : string) (re0 re1 : reg_exp), s =~ Union re0 re1 <-> s =~ re0 \/ s =~ re1s:stringre0, re1:reg_exps =~ Union re0 re1 <-> s =~ re0 \/ s =~ re1s:stringre0, re1:reg_exps =~ Union re0 re1 -> s =~ re0 \/ s =~ re1s:stringre0, re1:reg_exps =~ re0 \/ s =~ re1 -> s =~ Union re0 re1s:stringre0, re1:reg_exps =~ Union re0 re1 -> s =~ re0 \/ s =~ re1s:stringre0, re1:reg_expH:s =~ Union re0 re1s =~ re0 \/ s =~ re1s:stringre0, re1:reg_expH:s =~ Union re0 re1s1:list asciire2, re3:reg_expH2:s =~ re0H0:s1 = sH1:re2 = re0H3:re3 = re1s =~ re0 \/ s =~ re1s:stringre0, re1:reg_expH:s =~ Union re0 re1re2:reg_exps2:list asciire3:reg_expH1:s =~ re1H0:s2 = sH2:re2 = re0H3:re3 = re1s =~ re0 \/ s =~ re1s:stringre0, re1:reg_expH:s =~ Union re0 re1s1:list asciire2, re3:reg_expH2:s =~ re0H0:s1 = sH1:re2 = re0H3:re3 = re1s =~ re0 \/ s =~ re1apply H2.s:stringre0, re1:reg_expH:s =~ Union re0 re1s1:list asciire2, re3:reg_expH2:s =~ re0H0:s1 = sH1:re2 = re0H3:re3 = re1s =~ re0s:stringre0, re1:reg_expH:s =~ Union re0 re1re2:reg_exps2:list asciire3:reg_expH1:s =~ re1H0:s2 = sH2:re2 = re0H3:re3 = re1s =~ re0 \/ s =~ re1apply H1.s:stringre0, re1:reg_expH:s =~ Union re0 re1re2:reg_exps2:list asciire3:reg_expH1:s =~ re1H0:s2 = sH2:re2 = re0H3:re3 = re1s =~ re1s:stringre0, re1:reg_exps =~ re0 \/ s =~ re1 -> s =~ Union re0 re1s:stringre0, re1:reg_expH:s =~ re0s =~ Union re0 re1s:stringre0, re1:reg_expH:s =~ re1s =~ Union re0 re1s:stringre0, re1:reg_expH:s =~ re0s =~ Union re0 re1apply H.s:stringre0, re1:reg_expH:s =~ re0s =~ re0s:stringre0, re1:reg_expH:s =~ re1s =~ Union re0 re1apply H. Qed.s:stringre0, re1:reg_expH:s =~ re1s =~ re1
Exercise: 3 stars, standard, optional (star_ne)
forall (a : ascii) (s : list ascii) (re : reg_exp), a :: s =~ Star re <-> (exists s0 s1 : list ascii, s = s0 ++ s1 /\ (a :: s0 =~ re) /\ s1 =~ Star re)(* FILL IN HERE *) Admitted.forall (a : ascii) (s : list ascii) (re : reg_exp), a :: s =~ Star re <-> (exists s0 s1 : list ascii, s = s0 ++ s1 /\ (a :: s0 =~ re) /\ s1 =~ Star re)
☐
The definition of our regex matcher will include two fixpoint
functions. The first function, given regex re, will evaluate to a
value that reflects whether re matches the empty string. The
function will satisfy the following property:
Definition refl_matches_eps m :=
forall re : @reg_exp ascii, reflect ([ ] =~ re) (m re).
Exercise: 2 stars, standard, optional (match_eps)
Admitted.match_eps:reg_exp -> boolre:reg_expbool
☐
Exercise: 3 stars, standard, optional (match_eps_refl)
refl_matches_eps match_eps(* FILL IN HERE *) Admitted.refl_matches_eps match_eps
☐
We'll define other functions that use match_eps. However, the
only property of match_eps that you'll need to use in all proofs
over these functions is match_eps_refl.
The key operation that will be performed by our regex matcher will
be to iteratively construct a sequence of regex derivatives. For each
character a and regex re, the derivative of re on a is a regex
that matches all suffixes of strings matched by re that start with
a. I.e., re' is a derivative of re on a if they satisfy the
following relation:
Definition is_der re (a : ascii) re' :=
forall s, a :: s =~ re <-> s =~ re'.
A function d derives strings if, given character a and regex
re, it evaluates to the derivative of re on a. I.e., d
satisfies the following property:
Definition derives d := forall a re, is_der re a (d a re).
Exercise: 3 stars, standard, optional (derive)
Admitted.derive:ascii -> reg_exp -> reg_expa:asciire:reg_expreg_exp
☐
The derive function should pass the following tests. Each test
establishes an equality between an expression that will be
evaluated by our regex matcher and the final value that must be
returned by the regex matcher. Each test is annotated with the
match fact that it reflects.
Example c := ascii_of_nat 99. Example d := ascii_of_nat 100.
"c" =~ EmptySet:
match_eps (derive c EmptySet) = false(* FILL IN HERE *) Admitted.match_eps (derive c EmptySet) = false
"c" =~ Char c:
match_eps (derive c (Char c)) = true(* FILL IN HERE *) Admitted.match_eps (derive c (Char c)) = true
"c" =~ Char d:
match_eps (derive c (Char d)) = false(* FILL IN HERE *) Admitted.match_eps (derive c (Char d)) = false
"c" =~ App (Char c) EmptyStr:
match_eps (derive c (App (Char c) EmptyStr)) = true(* FILL IN HERE *) Admitted.match_eps (derive c (App (Char c) EmptyStr)) = true
"c" =~ App EmptyStr (Char c):
match_eps (derive c (App EmptyStr (Char c))) = true(* FILL IN HERE *) Admitted.match_eps (derive c (App EmptyStr (Char c))) = true
"c" =~ Star c:
match_eps (derive c (Star (Char c))) = true(* FILL IN HERE *) Admitted.match_eps (derive c (Star (Char c))) = true
"cd" =~ App (Char c) (Char d):
match_eps (derive d (derive c (App (Char c) (Char d)))) = true(* FILL IN HERE *) Admitted.match_eps (derive d (derive c (App (Char c) (Char d)))) = true
"cd" =~ App (Char d) (Char c):
match_eps (derive d (derive c (App (Char d) (Char c)))) = false(* FILL IN HERE *) Admitted.match_eps (derive d (derive c (App (Char d) (Char c)))) = false
Exercise: 4 stars, standard, optional (derive_corr)
derives derive(* FILL IN HERE *) Admitted.derives derive
☐
We'll define the regex matcher using derive. However, the only
property of derive that you'll need to use in all proofs of
properties of the matcher is derive_corr.
A function m matches regexes if, given string s and regex re,
it evaluates to a value that reflects whether s is matched by
re. I.e., m holds the following property:
Definition matches_regex m : Prop :=
forall (s : string) re, reflect (s =~ re) (m s re).
Exercise: 2 stars, standard, optional (regex_match)
Admitted.regex_match:string -> reg_exp -> bools:stringre:reg_expbool
☐
Exercise: 3 stars, standard, optional (regex_refl)
matches_regex regex_match(* FILL IN HERE *) Admitted.matches_regex regex_match
☐
(* Wed Jan 9 12:02:45 EST 2019 *)