IndProp: Inductively Defined Propositionspart 2
Case Study: Regular Expressions
Inductive reg_exp (T : Type) : Type :=
| EmptySet
| EmptyStr
| Char (t : T)
| App (r1 r2 : reg_exp T)
| Union (r1 r2 : reg_exp T)
| Star (r : reg_exp T).
Arguments EmptySet {T}.
Arguments EmptyStr {T}.
Arguments Char {T} _.
Arguments App {T} _ _.
Arguments Union {T} _ _.
Arguments Star {T} _.
| EmptySet
| EmptyStr
| Char (t : T)
| App (r1 r2 : reg_exp T)
| Union (r1 r2 : reg_exp T)
| Star (r : reg_exp T).
Arguments EmptySet {T}.
Arguments EmptyStr {T}.
Arguments Char {T} _.
Arguments App {T} _ _.
Arguments Union {T} _ _.
Arguments Star {T} _.
Note that this definition is polymorphic: Regular
expressions in reg_exp T describe strings with characters drawn
from T -- which in this exercise we represent as lists with
elements from T.
(*(Technical aside: 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 present purposes.) *)
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 present purposes.) *)
We connect regular expressions and strings by defining when a
regular expression matches some string.
Informally this looks as follows:
We can easily translate this intuition into a set of rules,
where we write s =~ re for s matches regular expression re:
This directly corresponds to the following Inductive definition.
We use the notation s =~ re in place of exp_match s re.
(By "reserving" the notation before defining the Inductive,
we can use it in the definition.)
- 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.
(MEmpty) | |
[] =~ EmptyStr |
(MChar) | |
[x] =~ (Char x) |
s1 =~ re1 s2 =~ re2 | (MApp) |
(s1 ++ s2) =~ (App re1 re2) |
s1 =~ re1 | (MUnionL) |
s1 =~ (Union re1 re2) |
s2 =~ re2 | (MUnionR) |
s2 =~ (Union re1 re2) |
(MStar0) | |
[] =~ (Star re) |
s1 =~ re | |
s2 =~ (Star re) | (MStarApp) |
(s1 ++ s2) =~ (Star re) |
Reserved Notation "s =~ re" (at level 80).
Inductive exp_match {T} : list T → reg_exp T → Prop :=
| MEmpty : [] =~ EmptyStr
| MChar x : [x] =~ (Char x)
| MApp s1 re1 s2 re2
(H1 : s1 =~ re1)
(H2 : s2 =~ re2)
: (s1 ++ s2) =~ (App re1 re2)
| MUnionL s1 re1 re2
(H1 : s1 =~ re1)
: s1 =~ (Union re1 re2)
| MUnionR s2 re1 re2
(H2 : s2 =~ re2)
: s2 =~ (Union re1 re2)
| MStar0 re : [] =~ (Star re)
| MStarApp s1 s2 re
(H1 : s1 =~ re)
(H2 : s2 =~ (Star re))
: (s1 ++ s2) =~ (Star re)
where "s =~ re" := (exp_match s re).
Inductive exp_match {T} : list T → reg_exp T → Prop :=
| MEmpty : [] =~ EmptyStr
| MChar x : [x] =~ (Char x)
| MApp s1 re1 s2 re2
(H1 : s1 =~ re1)
(H2 : s2 =~ re2)
: (s1 ++ s2) =~ (App re1 re2)
| MUnionL s1 re1 re2
(H1 : s1 =~ re1)
: s1 =~ (Union re1 re2)
| MUnionR s2 re1 re2
(H2 : s2 =~ re2)
: s2 =~ (Union re1 re2)
| MStar0 re : [] =~ (Star re)
| MStarApp s1 s2 re
(H1 : s1 =~ re)
(H2 : s2 =~ (Star re))
: (s1 ++ s2) =~ (Star re)
where "s =~ re" := (exp_match s re).
Notice that these rules are not quite the same as the
intuition 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 intuition we gave for Union and Star correspond
to two constructors each: MUnionL / MUnionR, and MStar0 /
MStarApp. The result is logically equivalent to the original
intuition 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 intuition is indeed equivalent.)
Let's illustrate these rules with a few examples.
(Notice how the last example applies MApp to the string
[1] 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:
We can define helper functions for writing down regular
expressions. The reg_exp_of_list function constructs a regular
expression that matches exactly the string 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.
Example reg_exp_ex4 : [1; 2; 3] =~ reg_exp_of_list [1; 2; 3].
match l with
| [] ⇒ EmptyStr
| x :: l' ⇒ App (Char x) (reg_exp_of_list l')
end.
Example reg_exp_ex4 : [1; 2; 3] =~ reg_exp_of_list [1; 2; 3].
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.
(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)
The following lemmas show that the intuition about matching given at the beginning of the chapter can be obtained from the formal inductive definition.
Lemma EmptySet_is_empty : ∀ T (s : list T),
¬ (s =~ EmptySet).
Proof.
(* FILL IN HERE *) Admitted.
Lemma MUnion' : ∀ T (s : list T) (re1 re2 : reg_exp T),
s =~ re1 ∨ s =~ re2 →
s =~ Union re1 re2.
Proof.
(* FILL IN HERE *) Admitted.
¬ (s =~ EmptySet).
Proof.
(* FILL IN HERE *) Admitted.
Lemma MUnion' : ∀ T (s : list T) (re1 re2 : reg_exp T),
s =~ re1 ∨ s =~ re2 →
s =~ Union re1 re2.
Proof.
(* FILL IN HERE *) Admitted.
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.
Lemma MStar' : ∀ T (ss : list (list T)) (re : reg_exp T),
(∀ s, In s ss → s =~ re) →
fold app ss [] =~ Star re.
Proof.
(* FILL IN HERE *) Admitted.
☐
(∀ s, In s ss → s =~ re) →
fold app ss [] =~ Star re.
Proof.
(* FILL IN HERE *) Admitted.
☐
(* It turns out that the EmptyStr constructor is actually not
needed, since the regular expression matching the empty string can
also be defined from Star and EmptySet: *)
Definition EmptyStr' {T:Type} := @Star T (EmptySet).
(* State and prove that this EmptyStr' definition matches exactly
the same strings as the EmptyStr constructor. *)
(* FILL IN HERE *)
☐
needed, since the regular expression matching the empty string can
also be defined from Star and EmptySet: *)
Definition EmptyStr' {T:Type} := @Star T (EmptySet).
(* State and prove that this EmptyStr' definition matches exactly
the same strings as the EmptyStr constructor. *)
(* FILL IN HERE *)
☐
Fixpoint re_chars {T} (re : reg_exp T) : 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.
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.
The main theorem:
Theorem in_re_match : ∀ T (s : list T) (re : reg_exp T) (x : T),
s =~ re →
In x s →
In x (re_chars re).
Proof.
intros T s re x Hmatch Hin.
induction Hmatch
as [| x'
| s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | s2 re1 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2].
(* WORKED IN CLASS *)
- (* MEmpty *)
simpl in Hin. destruct Hin.
- (* MChar *)
simpl. simpl in Hin.
apply Hin.
- (* MApp *)
simpl.
s =~ re →
In x s →
In x (re_chars re).
Proof.
intros T s re x Hmatch Hin.
induction Hmatch
as [| x'
| s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | s2 re1 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2].
(* WORKED IN CLASS *)
- (* MEmpty *)
simpl in Hin. destruct Hin.
- (* MChar *)
simpl. simpl in Hin.
apply Hin.
- (* MApp *)
simpl.
Something interesting happens in the MApp case. We obtain
two induction hypotheses: One that applies when x occurs in
s1 (which matches re1), and a second one that applies when x
occurs in s2 (which matches re2).
rewrite In_app_iff in ×.
destruct Hin as [Hin | Hin].
+ (* In x s1 *)
left. apply (IH1 Hin).
+ (* In x s2 *)
right. apply (IH2 Hin).
- (* MUnionL *)
simpl. rewrite In_app_iff.
left. apply (IH Hin).
- (* MUnionR *)
simpl. rewrite In_app_iff.
right. apply (IH Hin).
- (* MStar0 *)
destruct Hin.
- (* MStarApp *)
simpl.
destruct Hin as [Hin | Hin].
+ (* In x s1 *)
left. apply (IH1 Hin).
+ (* In x s2 *)
right. apply (IH2 Hin).
- (* MUnionL *)
simpl. rewrite In_app_iff.
left. apply (IH Hin).
- (* MUnionR *)
simpl. rewrite In_app_iff.
right. apply (IH Hin).
- (* MStar0 *)
destruct Hin.
- (* MStarApp *)
simpl.
Here again we get two induction hypotheses, and they illustrate
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.
rewrite In_app_iff in Hin.
destruct Hin as [Hin | Hin].
+ (* In x s1 *)
apply (IH1 Hin).
+ (* In x s2 *)
apply (IH2 Hin).
Qed.
destruct Hin as [Hin | Hin].
+ (* In x s1 *)
apply (IH1 Hin).
+ (* In x s2 *)
apply (IH2 Hin).
Qed.
Exercise: 4 stars, standard, optional (re_not_empty)
Write a recursive function re_not_empty that tests whether a regular expression matches some string. Prove that your function is correct.
Fixpoint re_not_empty {T : Type} (re : reg_exp T) : bool
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
Lemma re_not_empty_correct : ∀ T (re : reg_exp T),
(∃ s, s =~ re) ↔ re_not_empty re = true.
Proof.
(* FILL IN HERE *) Admitted.
☐
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
Lemma re_not_empty_correct : ∀ T (re : reg_exp T),
(∃ s, s =~ re) ↔ re_not_empty re = true.
Proof.
(* FILL IN HERE *) Admitted.
☐
The remember Tactic
Lemma star_app: ∀ T (s1 s2 : list T) (re : reg_exp T),
s1 =~ Star re →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
s1 =~ Star re →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
Now, 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. (We can begin by generalizing s2, since it's pretty clear that we
are going to have to walk over both s1 and s2 in parallel.)
generalize dependent s2.
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2].
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2].
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...
- (* MEmpty *)
simpl. intros s2 H. apply H.
simpl. intros s2 H. apply H.
... but most cases get stuck. For MChar, for instance, we
must show
s2 =~ Char x' →
x'::s2 =~ Char x' which is clearly impossible.
s2 =~ Char x' →
x'::s2 =~ Char x' which is clearly impossible.
- (* MChar. *) intros s2 H. simpl. (* Stuck... *)
Abort.
Abort.
The problem here 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 like Star re.
(In this respect, induction on evidence behaves more like
destruct-without-eqn: than like inversion.)
A possible, but awkward, way to solve this problem is "manually
generalizing" over the problematic expressions by adding
explicit equality hypotheses to the lemma:
Lemma star_app: ∀ T (s1 s2 : list T) (re re' : reg_exp T),
re' = Star re →
s1 =~ re' →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
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 works, but it makes the statement of the lemma a bit ugly.
Fortunately, there is a better way...
Abort.
The tactic remember e as x eqn:Eq causes Coq to (1) replace all
occurrences of the expression e by the variable x, and (2) add
an equation Eq : x = e to the context. Here's how we can use it
to show the above result:
Lemma star_app: ∀ T (s1 s2 : list T) (re : reg_exp T),
s1 =~ Star re →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
remember (Star re) as re' eqn:Eq.
s1 =~ Star re →
s2 =~ Star re →
s1 ++ s2 =~ Star re.
Proof.
intros T s1 s2 re H1.
remember (Star re) as re' eqn:Eq.
We now have Eq : re' = Star re.
generalize dependent s2.
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2].
induction H1
as [|x'|s1 re1 s2' re2 Hmatch1 IH1 Hmatch2 IH2
|s1 re1 re2 Hmatch IH|re1 s2' re2 Hmatch IH
|re''|s1 s2' re'' Hmatch1 IH1 Hmatch2 IH2].
The Eq is contradictory in most cases, allowing us to
conclude immediately.
- (* MEmpty *) discriminate.
- (* MChar *) discriminate.
- (* MApp *) discriminate.
- (* MUnionL *) discriminate.
- (* MUnionR *) discriminate.
- (* MChar *) discriminate.
- (* MApp *) discriminate.
- (* MUnionL *) discriminate.
- (* MUnionR *) discriminate.
The interesting cases are those that correspond to Star.
- (* MStar0 *)
intros s H. apply H.
- (* MStarApp *)
injection Eq as Eq'.
intros s2 H1. rewrite <- app_assoc.
apply MStarApp.
+ apply Hmatch1.
+ apply IH2.
× rewrite Eq'. reflexivity.
× apply H1.
intros s H. apply H.
- (* MStarApp *)
injection Eq as Eq'.
intros s2 H1. rewrite <- app_assoc.
apply MStarApp.
+ apply Hmatch1.
+ apply IH2.
× rewrite Eq'. reflexivity.
× apply H1.
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.
Qed.
Exercise: 4 stars, standard (exp_match_ex2)
Lemma MStar'' : ∀ T (s : list T) (re : reg_exp T),
s =~ Star re →
∃ ss : list (list T),
s = fold app ss []
∧ ∀ s', In s' ss → s' =~ re.
Proof.
(* FILL IN HERE *) Admitted.
☐
s =~ Star re →
∃ ss : list (list T),
s = fold app ss []
∧ ∀ s', In s' ss → s' =~ re.
Proof.
(* FILL IN HERE *) Admitted.
☐
Exercise: 5 stars, advanced, optional (weak_pumping)
One of the first really interesting theorems in the theory of regular expressions is the so-called pumping lemma, which states, informally, that any sufficiently long string s matching a regular expression re can be "pumped" by repeating some middle section of s an arbitrary number of times to produce a new string also matching re. (For the sake of simplicity in this exercise, we consider a slightly weaker theorem than is usually stated in courses on automata theory -- hence the name weak_pumping.)
Module Pumping.
Fixpoint pumping_constant {T} (re : reg_exp T) : nat :=
match re with
| EmptySet ⇒ 1
| EmptyStr ⇒ 1
| Char _ ⇒ 2
| App re1 re2 ⇒
pumping_constant re1 + pumping_constant re2
| Union re1 re2 ⇒
pumping_constant re1 + pumping_constant re2
| Star r ⇒ pumping_constant r
end.
Fixpoint pumping_constant {T} (re : reg_exp T) : nat :=
match re with
| EmptySet ⇒ 1
| EmptyStr ⇒ 1
| Char _ ⇒ 2
| App re1 re2 ⇒
pumping_constant re1 + pumping_constant re2
| Union re1 re2 ⇒
pumping_constant re1 + pumping_constant re2
| Star r ⇒ pumping_constant r
end.
You may find these lemmas about the pumping constant useful when
proving the pumping lemma below.
Lemma pumping_constant_ge_1 :
∀ T (re : reg_exp T),
pumping_constant re ≥ 1.
Lemma pumping_constant_0_false :
∀ T (re : reg_exp T),
pumping_constant re = 0 → False.
∀ T (re : reg_exp T),
pumping_constant re ≥ 1.
Proof.
intros T re. induction re.
- (* EmptySet *)
apply le_n.
- (* EmptyStr *)
apply le_n.
- (* Char *)
apply le_S. apply le_n.
- (* App *)
simpl.
apply le_trans with (n:=pumping_constant re1).
apply IHre1. apply le_plus_l.
- (* Union *)
simpl.
apply le_trans with (n:=pumping_constant re1).
apply IHre1. apply le_plus_l.
- (* Star *)
simpl. apply IHre.
Qed.
intros T re. induction re.
- (* EmptySet *)
apply le_n.
- (* EmptyStr *)
apply le_n.
- (* Char *)
apply le_S. apply le_n.
- (* App *)
simpl.
apply le_trans with (n:=pumping_constant re1).
apply IHre1. apply le_plus_l.
- (* Union *)
simpl.
apply le_trans with (n:=pumping_constant re1).
apply IHre1. apply le_plus_l.
- (* Star *)
simpl. apply IHre.
Qed.
Lemma pumping_constant_0_false :
∀ T (re : reg_exp T),
pumping_constant re = 0 → False.
Proof.
intros T re H.
assert (Hp1 : pumping_constant re ≥ 1).
{ apply pumping_constant_ge_1. }
inversion Hp1 as [Hp1'| p Hp1' Hp1''].
- rewrite H in Hp1'. discriminate Hp1'.
- rewrite H in Hp1''. discriminate Hp1''.
Qed.
intros T re H.
assert (Hp1 : pumping_constant re ≥ 1).
{ apply pumping_constant_ge_1. }
inversion Hp1 as [Hp1'| p Hp1' Hp1''].
- rewrite H in Hp1'. discriminate Hp1'.
- rewrite H in Hp1''. discriminate Hp1''.
Qed.
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.
match n with
| 0 ⇒ []
| S n' ⇒ l ++ napp n' l
end.
This auxiliary lemma might also be useful in your proof of the
pumping lemma.
Lemma napp_plus: ∀ T (n m : nat) (l : list T),
napp (n + m) l = napp n l ++ napp m l.
Lemma napp_star :
∀ T m s1 s2 (re : reg_exp T),
s1 =~ re → s2 =~ Star re →
napp m s1 ++ s2 =~ Star re.
napp (n + m) l = napp n l ++ napp m l.
Proof.
intros T n m l.
induction n as [|n IHn].
- reflexivity.
- simpl. rewrite IHn, app_assoc. reflexivity.
Qed.
intros T n m l.
induction n as [|n IHn].
- reflexivity.
- simpl. rewrite IHn, app_assoc. reflexivity.
Qed.
Lemma napp_star :
∀ T m s1 s2 (re : reg_exp T),
s1 =~ re → s2 =~ Star re →
napp m s1 ++ s2 =~ Star re.
The (weak) 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.
Lemma weak_pumping : ∀ T (re : reg_exp T) s,
s =~ re →
pumping_constant re ≤ length s →
∃ s1 s2 s3,
s = s1 ++ s2 ++ s3 ∧
s2 ≠ [] ∧
∀ m, s1 ++ napp m s2 ++ s3 =~ re.
s =~ re →
pumping_constant re ≤ length s →
∃ s1 s2 s3,
s = s1 ++ s2 ++ s3 ∧
s2 ≠ [] ∧
∀ m, s1 ++ napp m s2 ++ s3 =~ re.
Complete the proof below. Several of the lemmas about le that
were in an optional exercise earlier in this chapter may also be
useful.
Proof.
intros T re s Hmatch.
induction Hmatch
as [ | x | s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | s2 re1 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2 ].
- (* MEmpty *)
simpl. intros contra. inversion contra.
(* FILL IN HERE *) Admitted.
☐
intros T re s Hmatch.
induction Hmatch
as [ | x | s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | s2 re1 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2 ].
- (* MEmpty *)
simpl. intros contra. inversion contra.
(* FILL IN HERE *) Admitted.
☐
Exercise: 5 stars, advanced, optional (pumping)
Now here is the usual version of the pumping lemma. In addition to requiring that s2 ≠ [], it also requires that length s1 + length s2 ≤ pumping_constant re.
Lemma pumping : ∀ T (re : reg_exp T) s,
s =~ re →
pumping_constant re ≤ length s →
∃ s1 s2 s3,
s = s1 ++ s2 ++ s3 ∧
s2 ≠ [] ∧
length s1 + length s2 ≤ pumping_constant re ∧
∀ m, s1 ++ napp m s2 ++ s3 =~ re.
s =~ re →
pumping_constant re ≤ length s →
∃ s1 s2 s3,
s = s1 ++ s2 ++ s3 ∧
s2 ≠ [] ∧
length s1 + length s2 ≤ pumping_constant re ∧
∀ m, s1 ++ napp m s2 ++ s3 =~ re.
You may want to copy your proof of weak_pumping below.
Proof.
intros T re s Hmatch.
induction Hmatch
as [ | x | s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | s2 re1 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2 ].
- (* MEmpty *)
simpl. intros contra. inversion contra.
(* FILL IN HERE *) Admitted.
End Pumping.
☐
intros T re s Hmatch.
induction Hmatch
as [ | x | s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
| s1 re1 re2 Hmatch IH | s2 re1 re2 Hmatch IH
| re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2 ].
- (* MEmpty *)
simpl. intros contra. inversion contra.
(* FILL IN HERE *) Admitted.
End Pumping.
☐
Case Study: Improving Reflection
Theorem filter_not_empty_In : ∀ n l,
filter (fun x ⇒ n =? x) l ≠ [] → In n l.
filter (fun x ⇒ n =? x) l ≠ [] → In n l.
Proof.
intros n l. induction l as [|m l' IHl'].
- (* l = nil *)
simpl. intros H. apply H. reflexivity.
- (* l = m :: l' *)
simpl. destruct (n =? m) eqn:H.
+ (* n =? m = true *)
intros _. rewrite eqb_eq in H. rewrite H.
left. reflexivity.
+ (* n =? m = false *)
intros H'. right. apply IHl'. apply H'.
Qed.
intros n l. induction l as [|m l' IHl'].
- (* l = nil *)
simpl. intros H. apply H. reflexivity.
- (* l = m :: l' *)
simpl. destruct (n =? m) eqn:H.
+ (* n =? m = true *)
intros _. rewrite eqb_eq in H. rewrite H.
left. reflexivity.
+ (* n =? m = false *)
intros H'. right. apply IHl'. apply H'.
Qed.
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 sort of reasoning by defining an inductive
proposition that yields a better case-analysis principle for n =?
m. Instead of generating the assumption (n =? m) = true, which
usually requires some massaging before we can use it, this
principle gives us right away the assumption we really need: n =
m.
Following the terminology introduced in Logic, we call this
the "reflection principle for equality on numbers," and we say
that the boolean n =? m is reflected in the proposition n =
m.
Inductive reflect (P : Prop) : bool → Prop :=
| ReflectT (H : P) : reflect P true
| ReflectF (H : ¬ P) : reflect P false.
| 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. It states that the property P
reflects (intuitively, is 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 we can extract evidence for P from a proof of
reflect P true.
Similarly, the only way to show reflect P false is by tagging
evidence for ¬ P with the ReflectF constructor.
To put this observation to work, we first prove that the
statements P ↔ b = true and reflect P b are indeed
equivalent. First, the left-to-right implication:
Theorem iff_reflect : ∀ P b, (P ↔ b = true) → reflect P b.
Proof.
(* WORKED IN CLASS *)
intros P b H. destruct b eqn:Eb.
- apply ReflectT. rewrite H. reflexivity.
- apply ReflectF. rewrite H. intros H'. discriminate.
Qed.
Proof.
(* WORKED IN CLASS *)
intros P b H. destruct b eqn:Eb.
- apply ReflectT. rewrite H. reflexivity.
- apply ReflectF. rewrite H. intros H'. discriminate.
Qed.
Lemma eqbP : ∀ n m, reflect (n = m) (n =? m).
Proof.
intros n m. apply iff_reflect. rewrite eqb_eq. reflexivity.
Qed.
Proof.
intros n m. apply iff_reflect. rewrite eqb_eq. reflexivity.
Qed.
The proof of filter_not_empty_In now goes as follows. Notice
how the calls to destruct and rewrite in the earlier proof of
this theorem are combined here into a single call to
destruct.
(To see this clearly, execute 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.)
Theorem filter_not_empty_In' : ∀ n l,
filter (fun x ⇒ n =? x) l ≠ [] →
In n l.
Proof.
intros n l. induction l as [|m l' IHl'].
- (* l = *)
simpl. intros H. apply H. reflexivity.
- (* l = m :: l' *)
simpl. destruct (eqbP n m) as [EQnm | NEQnm].
+ (* n = m *)
intros _. rewrite EQnm. left. reflexivity.
+ (* n <> m *)
intros H'. right. apply IHl'. apply H'.
Qed.
filter (fun x ⇒ n =? x) l ≠ [] →
In n l.
Proof.
intros n l. induction l as [|m l' IHl'].
- (* l = *)
simpl. intros H. apply H. reflexivity.
- (* l = m :: l' *)
simpl. destruct (eqbP n m) as [EQnm | NEQnm].
+ (* n = m *)
intros _. rewrite EQnm. left. reflexivity.
+ (* n <> m *)
intros H'. right. apply IHl'. apply H'.
Qed.
Fixpoint count n l :=
match l with
| [] ⇒ 0
| m :: l' ⇒ (if n =? m then 1 else 0) + count n l'
end.
Theorem eqbP_practice : ∀ n l,
count n l = 0 → ~(In n l).
Proof.
intros n l Hcount. induction l as [| m l' IHl'].
(* FILL IN HERE *) Admitted.
☐
match l with
| [] ⇒ 0
| m :: l' ⇒ (if n =? m then 1 else 0) + count n l'
end.
Theorem eqbP_practice : ∀ n l,
count n l = 0 → ~(In n l).
Proof.
intros n l Hcount. induction l as [| m l' IHl'].
(* FILL IN HERE *) Admitted.
☐
Additional Exercises
Exercise: 3 stars, standard, optional (nostutter_defn)
Formulating inductive definitions of properties is an important skill you'll need in this course. Try to solve this exercise without any help.
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.)
Example test_nostutter_1: nostutter [3;1;4;1;5;6].
(* FILL IN HERE *) Admitted.
(*
Proof. repeat constructor; apply eqb_neq; auto.
Qed.
*)
Example test_nostutter_2: nostutter (@nil nat).
(* FILL IN HERE *) Admitted.
(*
Proof. repeat constructor; apply eqb_neq; auto.
Qed.
*)
Example test_nostutter_3: nostutter [5].
(* FILL IN HERE *) Admitted.
(*
Proof. repeat constructor; auto. Qed.
*)
Example test_nostutter_4: not (nostutter [3;1;1;4]).
(* FILL IN HERE *) Admitted.
(*
Proof. intro.
repeat match goal with
h: nostutter _ ⊢ _ => inversion h; clear h; subst
end.
contradiction; auto. Qed.
*)
(* Do not modify the following line: *)
Definition manual_grade_for_nostutter : option (nat×string) := None.
☐
(* FILL IN HERE *) Admitted.
(*
Proof. repeat constructor; apply eqb_neq; auto.
Qed.
*)
Example test_nostutter_2: nostutter (@nil nat).
(* FILL IN HERE *) Admitted.
(*
Proof. repeat constructor; apply eqb_neq; auto.
Qed.
*)
Example test_nostutter_3: nostutter [5].
(* FILL IN HERE *) Admitted.
(*
Proof. repeat constructor; auto. Qed.
*)
Example test_nostutter_4: not (nostutter [3;1;1;4]).
(* FILL IN HERE *) Admitted.
(*
Proof. intro.
repeat match goal with
h: nostutter _ ⊢ _ => inversion h; clear h; subst
end.
contradiction; auto. Qed.
*)
(* Do not modify the following line: *)
Definition manual_grade_for_nostutter : option (nat×string) := None.
☐
Exercise: 4 stars, advanced, optional (filter_challenge)
Let's prove that our definition of filter from the Poly chapter matches an abstract specification. Here is the specification, written out informally in English:[1;4;6;2;3] is an in-order merge of
[1;6;2] and
[4;3]. Now, suppose we have a set X, a function test: X→bool, and a list l of type list X. Suppose further that l is an in-order merge of two lists, l1 and l2, such that every item in l1 satisfies test and no item in l2 satisfies test. Then filter test l = l1.
Inductive merge {X:Type} : list X → list X → list X → Prop :=
(* FILL IN HERE *)
.
Theorem merge_filter : ∀ (X : Set) (test: X→bool) (l l1 l2 : list X),
merge l1 l2 l →
All (fun n ⇒ test n = true) l1 →
All (fun n ⇒ test n = false) l2 →
filter test l = l1.
Proof.
(* FILL IN HERE *) Admitted.
(* FILL IN HERE *)
☐
(* FILL IN HERE *)
.
Theorem merge_filter : ∀ (X : Set) (test: X→bool) (l l1 l2 : list X),
merge l1 l2 l →
All (fun n ⇒ test n = true) l1 →
All (fun n ⇒ test n = false) l2 →
filter test l = l1.
Proof.
(* FILL IN HERE *) Admitted.
(* FILL IN HERE *)
☐
Exercise: 5 stars, advanced, optional (filter_challenge_2)
A different way to characterize the behavior of filter goes like this: Among all subsequences of l with the property that test evaluates to true on all their members, filter test l is the longest. Formalize this claim and prove it.
(* FILL IN HERE *)
☐
☐
Exercise: 4 stars, standard, optional (palindromes)
A palindrome is a sequence that reads the same backwards as forwards.- 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
c : ∀ l, l = rev l → pal l may seem obvious, but will not work very well.) - Prove (pal_app_rev) that
∀ l, pal (l ++ rev l). - Prove (pal_rev that)
∀ l, pal l → l = rev l.
Inductive pal {X:Type} : list X → Prop :=
(* FILL IN HERE *)
.
Theorem pal_app_rev : ∀ (X:Type) (l : list X),
pal (l ++ (rev l)).
Proof.
(* FILL IN HERE *) Admitted.
Theorem pal_rev : ∀ (X:Type) (l: list X) , pal l → l = rev l.
Proof.
(* FILL IN HERE *) Admitted.
☐
(* FILL IN HERE *)
.
Theorem pal_app_rev : ∀ (X:Type) (l : list X),
pal (l ++ (rev l)).
Proof.
(* FILL IN HERE *) Admitted.
Theorem pal_rev : ∀ (X:Type) (l: list X) , pal l → l = rev l.
Proof.
(* FILL IN HERE *) Admitted.
☐
Exercise: 5 stars, standard, optional (palindrome_converse)
Again, the converse direction is significantly more difficult, due to the lack of evidence. Using your definition of pal from the previous exercise, prove that∀ l, l = rev l → pal l.
Theorem palindrome_converse: ∀ {X: Type} (l: list X),
l = rev l → pal l.
Proof.
(* FILL IN HERE *) Admitted.
☐
l = rev l → pal l.
Proof.
(* FILL IN HERE *) Admitted.
☐
Exercise: 4 stars, advanced, optional (NoDup)
Recall the definition of the In property from the Logic chapter, which asserts that a value x appears at least once in a list l:
(* Fixpoint In (A : Type) (x : A) (l : list A) : Prop :=
match l with
| => False
| x' :: l' => x' = x \/ In A x l'
end *)
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.
☐
(* Do not modify the following line: *)
Definition manual_grade_for_NoDup_disjoint_etc : option (nat×string) := None.
☐
Exercise: 4 stars, advanced, optional (pigeonhole_principle)
The pigeonhole principle states a basic fact about counting: if we distribute more than n items into n pigeonholes, some pigeonhole must contain at least two items. As often happens, this apparently trivial fact about numbers requires non-trivial machinery to prove, but we now have enough...
Lemma in_split : ∀ (X:Type) (x:X) (l:list X),
In x l →
∃ l1 l2, l = l1 ++ x :: l2.
Proof.
(* FILL IN HERE *) Admitted.
In x l →
∃ l1 l2, l = l1 ++ x :: l2.
Proof.
(* FILL IN HERE *) Admitted.
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 *)
.
(* Do not modify the following line: *)
Definition manual_grade_for_check_repeats : option (nat×string) := None.
(* FILL IN HERE *)
.
(* Do not modify the following line: *)
Definition manual_grade_for_check_repeats : option (nat×string) := None.
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.
Theorem pigeonhole_principle: excluded_middle →
∀ (X:Type) (l1 l2:list X),
(∀ x, In x l1 → In x l2) →
length l2 < length l1 →
repeats l1.
Proof.
intros EM X l1. induction l1 as [|x l1' IHl1'].
(* FILL IN HERE *) Admitted.
☐
∀ (X:Type) (l1 l2:list X),
(∀ x, In x l1 → In x l2) →
length l2 < length l1 →
repeats l1.
Proof.
intros EM X l1. induction l1 as [|x l1' IHl1'].
(* FILL IN HERE *) Admitted.
☐
Extended Exercise: A Verified Regular-Expression Matcher
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.
Lemma provable_equiv_true : ∀ (P : Prop), P → (P ↔ True).
Proof.
intros.
split.
- intros. constructor.
- intros _. apply H.
Qed.
Proof.
intros.
split.
- intros. constructor.
- intros _. apply H.
Qed.
Each Prop whose negation is provable is equivalent to False.
Lemma not_equiv_false : ∀ (P : Prop), ¬P → (P ↔ False).
Proof.
intros.
split.
- apply H.
- intros. destruct H0.
Qed.
Proof.
intros.
split.
- apply H.
- intros. destruct H0.
Qed.
EmptySet matches no string.
Lemma null_matches_none : ∀ (s : string), (s =~ EmptySet) ↔ False.
Proof.
intros.
apply not_equiv_false.
unfold not. intros. inversion H.
Qed.
Proof.
intros.
apply not_equiv_false.
unfold not. intros. inversion H.
Qed.
EmptyStr only matches the empty string.
Lemma empty_matches_eps : ∀ (s : string), s =~ EmptyStr ↔ s = [ ].
Proof.
split.
- intros. inversion H. reflexivity.
- intros. rewrite H. apply MEmpty.
Qed.
Proof.
split.
- intros. inversion H. reflexivity.
- intros. rewrite H. apply MEmpty.
Qed.
EmptyStr matches no non-empty string.
Lemma empty_nomatch_ne : ∀ (a : ascii) s, (a :: s =~ EmptyStr) ↔ False.
Proof.
intros.
apply not_equiv_false.
unfold not. intros. inversion H.
Qed.
Proof.
intros.
apply not_equiv_false.
unfold not. intros. inversion H.
Qed.
Char a matches no string that starts with a non-a character.
Lemma char_nomatch_char :
∀ (a b : ascii) s, b ≠ a → (b :: s =~ Char a ↔ False).
Proof.
intros.
apply not_equiv_false.
unfold not.
intros.
apply H.
inversion H0.
reflexivity.
Qed.
∀ (a b : ascii) s, b ≠ a → (b :: s =~ Char a ↔ False).
Proof.
intros.
apply not_equiv_false.
unfold not.
intros.
apply H.
inversion H0.
reflexivity.
Qed.
If Char a matches a non-empty string, then the string's tail is empty.
Lemma char_eps_suffix : ∀ (a : ascii) s, a :: s =~ Char a ↔ s = [ ].
Proof.
split.
- intros. inversion H. reflexivity.
- intros. rewrite H. apply MChar.
Qed.
Proof.
split.
- intros. inversion H. reflexivity.
- intros. rewrite H. apply MChar.
Qed.
App re0 re1 matches string s iff s = s0 ++ s1, where s0
matches re0 and s1 matches re1.
Lemma app_exists : ∀ (s : string) re0 re1,
s =~ App re0 re1 ↔
∃ s0 s1, s = s0 ++ s1 ∧ s0 =~ re0 ∧ s1 =~ re1.
Proof.
intros.
split.
- intros. inversion H. ∃ s1, s2. split.
× reflexivity.
× split. apply H3. apply H4.
- intros [ s0 [ s1 [ Happ [ Hmat0 Hmat1 ] ] ] ].
rewrite Happ. apply (MApp s0 _ s1 _ Hmat0 Hmat1).
Qed.
s =~ App re0 re1 ↔
∃ s0 s1, s = s0 ++ s1 ∧ s0 =~ re0 ∧ s1 =~ re1.
Proof.
intros.
split.
- intros. inversion H. ∃ s1, s2. split.
× reflexivity.
× split. apply H3. apply H4.
- intros [ s0 [ s1 [ Happ [ Hmat0 Hmat1 ] ] ] ].
rewrite Happ. apply (MApp s0 _ s1 _ Hmat0 Hmat1).
Qed.
Exercise: 3 stars, standard, optional (app_ne)
App re0 re1 matches a::s iff re0 matches the empty string and a::s matches re1 or s=s0++s1, where a::s0 matches re0 and s1 matches re1.
Lemma app_ne : ∀ (a : ascii) s re0 re1,
a :: s =~ (App re0 re1) ↔
([ ] =~ re0 ∧ a :: s =~ re1) ∨
∃ s0 s1, s = s0 ++ s1 ∧ a :: s0 =~ re0 ∧ s1 =~ re1.
Proof.
(* FILL IN HERE *) Admitted.
☐
a :: s =~ (App re0 re1) ↔
([ ] =~ re0 ∧ a :: s =~ re1) ∨
∃ s0 s1, s = s0 ++ s1 ∧ a :: s0 =~ re0 ∧ s1 =~ re1.
Proof.
(* FILL IN HERE *) Admitted.
☐
Lemma union_disj : ∀ (s : string) re0 re1,
s =~ Union re0 re1 ↔ s =~ re0 ∨ s =~ re1.
Proof.
intros. split.
- intros. inversion H.
+ left. apply H2.
+ right. apply H1.
- intros [ H | H ].
+ apply MUnionL. apply H.
+ apply MUnionR. apply H.
Qed.
s =~ Union re0 re1 ↔ s =~ re0 ∨ s =~ re1.
Proof.
intros. split.
- intros. inversion H.
+ left. apply H2.
+ right. apply H1.
- intros [ H | H ].
+ apply MUnionL. apply H.
+ apply MUnionR. apply H.
Qed.
Exercise: 3 stars, standard, optional (star_ne)
a::s matches Star re iff s = s0 ++ s1, where a::s0 matches re and s1 matches Star re. Like app_ne, this observation is critical, so understand it, prove it, and keep it in mind.
Lemma star_ne : ∀ (a : ascii) s re,
a :: s =~ Star re ↔
∃ s0 s1, s = s0 ++ s1 ∧ a :: s0 =~ re ∧ s1 =~ Star re.
Proof.
(* FILL IN HERE *) Admitted.
☐
a :: s =~ Star re ↔
∃ s0 s1, s = s0 ++ s1 ∧ a :: s0 =~ re ∧ s1 =~ Star re.
Proof.
(* FILL IN HERE *) Admitted.
☐
Exercise: 2 stars, standard, optional (match_eps)
Complete the definition of match_eps so that it tests if a given regex matches the empty string:
Fixpoint match_eps (re: reg_exp ascii) : bool
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
☐
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
☐
Exercise: 3 stars, standard, optional (match_eps_refl)
Now, prove that match_eps indeed tests if a given regex matches the empty string. (Hint: You'll want to use the reflection lemmas ReflectT and ReflectF.)
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:
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:
Exercise: 3 stars, standard, optional (derive)
Define derive so that it derives strings. One natural implementation uses match_eps in some cases to determine if key regex's match the empty string.
Fixpoint derive (a : ascii) (re : reg_exp ascii) : reg_exp ascii
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
☐
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
☐
"c" =~ EmptySet:
"c" =~ Char c:
"c" =~ Char d:
"c" =~ App (Char c) EmptyStr:
Example test_der3 : match_eps (derive c (App (Char c) EmptyStr)) = true.
Proof.
(* FILL IN HERE *) Admitted.
Proof.
(* FILL IN HERE *) Admitted.
"c" =~ App EmptyStr (Char c):
Example test_der4 : match_eps (derive c (App EmptyStr (Char c))) = true.
Proof.
(* FILL IN HERE *) Admitted.
Proof.
(* FILL IN HERE *) Admitted.
"c" =~ Star c:
Example test_der5 : match_eps (derive c (Star (Char c))) = true.
Proof.
(* FILL IN HERE *) Admitted.
Proof.
(* FILL IN HERE *) Admitted.
"cd" =~ App (Char c) (Char d):
Example test_der6 :
match_eps (derive d (derive c (App (Char c) (Char d)))) = true.
Proof.
(* FILL IN HERE *) Admitted.
match_eps (derive d (derive c (App (Char c) (Char d)))) = true.
Proof.
(* FILL IN HERE *) Admitted.
"cd" =~ App (Char d) (Char c):
Example test_der7 :
match_eps (derive d (derive c (App (Char d) (Char c)))) = false.
Proof.
(* FILL IN HERE *) Admitted.
match_eps (derive d (derive c (App (Char d) (Char c)))) = false.
Proof.
(* FILL IN HERE *) Admitted.
Exercise: 4 stars, standard, optional (derive_corr)
Prove that derive in fact always derives strings.
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:
Exercise: 2 stars, standard, optional (regex_match)
Complete the definition of regex_match so that it matches regexes.
Fixpoint regex_match (s : string) (re : reg_exp ascii) : bool
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
☐
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
☐
Exercise: 3 stars, standard, optional (regex_match_correct)
Finally, prove that regex_match in fact matches regexes.
(* 2023-10-12 11:33 *)