Advertisement
tinyevil

Untitled

Feb 24th, 2018
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1.  
  2. Module Pumping.
  3.  
  4. Fixpoint pumping_constant {T} (re : @reg_exp T) : nat :=
  5. match re with
  6. | EmptySet => 0
  7. | EmptyStr => 1
  8. | Char _ => 2
  9. | App re1 re2 =>
  10. pumping_constant re1 + pumping_constant re2
  11. | Union re1 re2 =>
  12. pumping_constant re1 + pumping_constant re2
  13. | Star re => S (pumping_constant re)
  14. end.
  15.  
  16. (** Next, it is useful to define an auxiliary function that repeats a
  17. string (appends it to itself) some number of times. *)
  18.  
  19. Fixpoint napp {T} (n : nat) (l : list T) : list T :=
  20. match n with
  21. | 0 => []
  22. | S n' => l ++ napp n' l
  23. end.
  24.  
  25. Lemma napp_plus: forall T (n m : nat) (l : list T),
  26. napp (n + m) l = napp n l ++ napp m l.
  27. Proof.
  28. intros T n m l.
  29. induction n as [|n IHn].
  30. - reflexivity.
  31. - simpl. rewrite IHn, app_assoc. reflexivity.
  32. Qed.
  33.  
  34. (** Now, the pumping lemma itself says that, if [s =~ re] and if the
  35. length of [s] is at least the pumping constant of [re], then [s]
  36. can be split into three substrings [s1 ++ s2 ++ s3] in such a way
  37. that [s2] can be repeated any number of times and the result, when
  38. combined with [s1] and [s3] will still match [re]. Since [s2] is
  39. also guaranteed not to be the empty string, this gives us
  40. a (constructive!) way to generate strings matching [re] that are
  41. as long as we like. *)
  42.  
  43. Lemma app_reassoc : forall {X} (a b c d:list X),
  44. a ++ b ++ c ++ d = ((a ++ b) ++ c) ++ d.
  45. Proof.
  46. intros X a b c d.
  47. rewrite app_assoc.
  48. rewrite app_assoc.
  49. reflexivity.
  50. Qed.
  51.  
  52. Lemma app_reassoc_2 : forall {X} (a b c d:list X),
  53. a ++ b ++ c ++ d = (a ++ b ++ c) ++ d.
  54. Proof.
  55. intros X a b c d.
  56. rewrite app_assoc.
  57. rewrite app_assoc.
  58. rewrite <- (app_assoc _ a b c).
  59. reflexivity.
  60. Qed.
  61.  
  62. Lemma star_napp : forall T (re : @reg_exp T) (s:list T) ,
  63. s =~ Star re -> forall m:nat, napp m s =~ Star re.
  64. Proof.
  65. intros T re s H m.
  66. induction m.
  67. - apply MStar0.
  68. - simpl. apply star_app.
  69. + apply H.
  70. + apply IHm.
  71. Qed.
  72.  
  73.  
  74. Import Coq.omega.Omega.
  75.  
  76. (* Added condition "length (s1 ++ s2 ) <= pumping_constant re"
  77. so now it is a true pumping lemma *)
  78. Lemma pumping2 : forall T (re : @reg_exp T) s,
  79. s =~ re ->
  80. pumping_constant re <= length s ->
  81. exists s1 s2 s3,
  82. s = s1 ++ s2 ++ s3 /\
  83. length (s1 ++ s2 ) <= pumping_constant re /\
  84. s2 <> [] /\
  85. forall m, s1 ++ napp m s2 ++ s3 =~ re.
  86. Proof.
  87. intros T re s Hmatch.
  88. induction Hmatch
  89. as [ | x | s1 re1 s2 re2 Hmatch1 IH1 Hmatch2 IH2
  90. | s1 re1 re2 Hmatch IH | re1 s2 re2 Hmatch IH
  91. | re | s1 s2 re Hmatch1 IH1 Hmatch2 IH2 ].
  92. - (* MEmpty *)
  93. simpl. omega.
  94. - (* MChar *)
  95. simpl. omega.
  96. - (* MApp *)
  97. simpl.
  98. intros Hconst.
  99. assert ( H : pumping_constant re1 <= length s1 \/ ~pumping_constant re1 <= length s1 ).
  100. { omega. }
  101. destruct H as [H'|H'].
  102. + assert (exists s2 s3 s4 : list T,
  103. s1 = s2 ++ s3 ++ s4 /\
  104. length (s2 ++ s3) <= pumping_constant re1 /\
  105. s3 <> [ ] /\ (forall m : nat, s2 ++ napp m s3 ++ s4 =~ re1)).
  106. { apply IH1. apply H'. }
  107. destruct H as [s3 [s4 [s5 [H1 [Hq [H2 H3]]]]]].
  108. clear IH1 IH2.
  109. exists s3, s4, (s5 ++ s2).
  110. repeat split.
  111. { rewrite app_reassoc.
  112. rewrite <- (app_assoc _ s3).
  113. rewrite <- H1.
  114. reflexivity. }
  115. { rewrite app_length in *. omega. }
  116. { apply H2. }
  117. { intros m. rewrite app_reassoc.
  118. apply MApp.
  119. * rewrite <- app_assoc.
  120. apply H3.
  121. * apply Hmatch2. }
  122. + assert (exists s1 s3 s4 : list T,
  123. s2 = s1 ++ s3 ++ s4 /\
  124. length (s1 ++ s3) <= pumping_constant re2 /\
  125. s3 <> [ ] /\ (forall m : nat, s1 ++ napp m s3 ++ s4 =~ re2)).
  126. { apply IH2. rewrite app_length in Hconst. omega. }
  127. destruct H as [s3 [s4 [s5 [H1 [Hq [H2 H3]]]]]].
  128. clear IH1 IH2.
  129. exists (s1 ++ s3), s4, s5.
  130. repeat split.
  131. { rewrite <- app_assoc.
  132. rewrite <- H1. reflexivity. }
  133. { rewrite <- app_assoc. rewrite app_length. omega. }
  134. { apply H2. }
  135. { intros m. rewrite <- app_assoc.
  136. apply MApp.
  137. * apply Hmatch1.
  138. * apply H3. }
  139. - intros Hconst.
  140. simpl in Hconst.
  141. assert (exists s2 s3 s4 : list T,
  142. s1 = s2 ++ s3 ++ s4 /\
  143. length (s2 ++ s3) <= pumping_constant re1 /\
  144. s3 <> [ ] /\ (forall m : nat, s2 ++ napp m s3 ++ s4 =~ re1)).
  145. { apply IH. omega. }
  146. destruct H as [s2 [s3 [s4 [H1 [Hq [H2 H3]]]]]].
  147. exists s2, s3, s4.
  148. repeat split.
  149. { apply H1. }
  150. { simpl. omega. }
  151. { apply H2. }
  152. { intros m. apply MUnionL. apply H3. }
  153. - intros Hconst.
  154. simpl in Hconst.
  155. assert (exists s1 s3 s4 : list T,
  156. s2 = s1 ++ s3 ++ s4 /\
  157. length (s1 ++ s3) <= pumping_constant re2 /\
  158. s3 <> [ ] /\ (forall m : nat, s1 ++ napp m s3 ++ s4 =~ re2)).
  159. { apply IH. omega. }
  160. destruct H as [s3 [s4 [s5 [H1 [Hq [H2 H3]]]]]].
  161. exists s3, s4, s5.
  162. repeat split.
  163. { apply H1. }
  164. { simpl. omega. }
  165. { apply H2. }
  166. { intros m. apply MUnionR. apply H3. }
  167. - intros Hconst. inversion Hconst.
  168. - simpl. intros Hconst.
  169. destruct s1.
  170. + simpl in Hconst. simpl in IH2.
  171. assert (exists s1 s3 s4 : list T,
  172. s2 = s1 ++ s3 ++ s4 /\
  173. length (s1 ++ s3) <= S (pumping_constant re) /\
  174. s3 <> [ ] /\ (forall m : nat, s1 ++ napp m s3 ++ s4 =~ Star re)).
  175. { apply IH2. apply Hconst. }
  176. destruct H as [s3 [s4 [s5 [H1 [Hq [H2 H3]]]]]].
  177. exists s3, s4, s5.
  178. repeat split.
  179. { simpl. apply H1. }
  180. { apply Hq. }
  181. { apply H2. }
  182. { apply H3. }
  183. + assert (H : pumping_constant re <= length (t :: s1) \/ ~pumping_constant re <= length (t :: s1)).
  184. { omega. }
  185. destruct H as [H'|H'].
  186. * assert (exists s2 s3 s4 : list T,
  187. t :: s1 = s2 ++ s3 ++ s4 /\
  188. length (s2 ++ s3) <= pumping_constant re /\
  189. s3 <> [ ] /\ (forall m : nat, s2 ++ napp m s3 ++ s4 =~ re)).
  190. { apply IH1. apply H'. }
  191. destruct H as [s3 [s4 [s5 [H1 [Hq [H2 H3]]]]]].
  192. exists s3, s4, (s5 ++ s2).
  193. repeat split.
  194. { rewrite app_reassoc_2. rewrite <- H1. reflexivity. }
  195. { omega. }
  196. { assumption. }
  197. { intros m. rewrite app_reassoc_2. apply MStarApp.
  198. - apply H3.
  199. - apply Hmatch2. }
  200. * exists [], (t::s1), s2.
  201. repeat split.
  202. { simpl. simpl in H'. omega. }
  203. { intros H. inversion H. }
  204. { intros m. simpl. apply star_app.
  205. - apply star_napp. rewrite <- (app_nil_r _ (t :: s1)).
  206. apply MStarApp.
  207. + apply Hmatch1.
  208. + apply MStar0.
  209. - apply Hmatch2. }
  210. Qed.
  211.  
  212. End Pumping.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement