diff -r 4c02878e2fe0 -r 17c079699ea0 thys/LexerExt.thy --- a/thys/LexerExt.thy Tue Feb 28 00:26:34 2017 +0000 +++ b/thys/LexerExt.thy Tue Feb 28 13:35:12 2017 +0000 @@ -159,6 +159,7 @@ | STAR rexp | UPNTIMES rexp nat | NTIMES rexp nat +| FROMNTIMES rexp nat section {* Semantics of Regular Expressions *} @@ -173,7 +174,7 @@ | "L (STAR r) = (L r)\" | "L (UPNTIMES r n) = (\i\ {..n} . (L r) \ i)" | "L (NTIMES r n) = ((L r) \ n)" - +| "L (FROMNTIMES r n) = (\i\ {n..} . (L r) \ i)" section {* Nullable, Derivatives *} @@ -188,6 +189,7 @@ | "nullable (STAR r) = True" | "nullable (UPNTIMES r n) = True" | "nullable (NTIMES r n) = (if n = 0 then True else nullable r)" +| "nullable (FROMNTIMES r n) = (if n = 0 then True else nullable r)" fun der :: "char \ rexp \ rexp" @@ -205,7 +207,8 @@ | "der c (UPNTIMES r (Suc n)) = SEQ (der c r) (UPNTIMES r n)" | "der c (NTIMES r 0) = ZERO" | "der c (NTIMES r (Suc n)) = SEQ (der c r) (NTIMES r n)" - +| "der c (FROMNTIMES r 0) = SEQ (der c r) (STAR r)" +| "der c (FROMNTIMES r (Suc n)) = SEQ (der c r) (FROMNTIMES r n)" fun ders :: "string \ rexp \ rexp" @@ -306,8 +309,17 @@ apply(simp only: L.simps der.simps) apply(simp) apply(rule impI) -by (simp add: Der_Pow_subseteq sup_absorb1) - +apply (simp add: Der_Pow_subseteq sup_absorb1) +(* FROMNTIMES *) +apply(simp only: der.simps) +apply(simp only: L.simps) +apply(subst Der_star[symmetric]) +apply(subst Star_def2) +apply(auto)[1] +apply(simp only: der.simps) +apply(simp only: L.simps) +apply(simp add: Der_UNION) +by (smt Der_Pow Der_Pow_notin Der_Pow_subseteq SUP_cong Seq_UNION Suc_reduce_Union2 sup.absorb_iff1) lemma ders_correctness: shows "L (ders s r) = Ders s (L r)" @@ -379,14 +391,10 @@ | "\ v2 : r2 \ \ Right v2 : ALT r1 r2" | "\ Void : ONE" | "\ Char c : CHAR c" -| "\ Stars [] : STAR r" -| "\\ v : r; \ Stars vs : STAR r\ \ \ Stars (v # vs) : STAR r" -| "\ Stars [] : UPNTIMES r 0" -| "\\ v : r; \ Stars vs : UPNTIMES r n\ \ \ Stars (v # vs) : UPNTIMES r (Suc n)" -| "\\ Stars vs : UPNTIMES r n\ \ \ Stars vs : UPNTIMES r (Suc n)" -| "\ Stars [] : NTIMES r 0" -| "\\ v : r; \ Stars vs : NTIMES r n\ \ \ Stars (v # vs) : NTIMES r (Suc n)" - +| "\\v \ set vs. \ v : r\ \ \ Stars vs : STAR r" +| "\\v \ set vs. \ v : r; length vs \ n\ \ \ Stars vs : UPNTIMES r n" +| "\\v \ set vs. \ v : r; length vs = n\ \ \ Stars vs : NTIMES r n" +| "\\v \ set vs. \ v : r; length vs \ n\ \ \ Stars vs : FROMNTIMES r n" inductive_cases Prf_elims: "\ v : ZERO" @@ -396,68 +404,39 @@ "\ v : CHAR c" (* "\ vs : STAR r"*) +lemma Prf_STAR: + assumes "\v\set vs. \ v : r \ flat v \ L r" + shows "concat (map flat vs) \ L r\" +using assms +apply(induct vs) +apply(auto) +done + +lemma Prf_Pow: + assumes "\v\set vs. \ v : r \ flat v \ L r" + shows "concat (map flat vs) \ L r \ length vs" +using assms +apply(induct vs) +apply(auto simp add: Sequ_def) +done + lemma Prf_flat_L: assumes "\ v : r" shows "flat v \ L r" using assms apply(induct v r rule: Prf.induct) apply(auto simp add: Sequ_def) -apply(rotate_tac 2) -apply(rule_tac x="Suc x" in bexI) -apply(auto simp add: Sequ_def)[2] -done - -lemma Prf_Stars: - assumes "\v \ set vs. \ v : r" - shows "\ Stars vs : STAR r" -using assms -by(induct vs) (auto intro: Prf.intros) - -lemma Prf_Stars_NTIMES: - assumes "\v \ set vs. \ v : r" "(length vs) = n" - shows "\ Stars vs : NTIMES r n" -using assms -apply(induct vs arbitrary: n) -apply(auto intro: Prf.intros) -done - -lemma Prf_Stars_UPNTIMES: - assumes "\v \ set vs. \ v : r" "(length vs) = n" - shows "\ Stars vs : UPNTIMES r n" -using assms -apply(induct vs arbitrary: n) -apply(auto intro: Prf.intros) -done - -lemma Prf_UPNTIMES_bigger: - assumes "\ Stars vs : UPNTIMES r n" "n \ m" - shows "\ Stars vs : UPNTIMES r m" -using assms -apply(induct m arbitrary:) -apply(auto) -using Prf.intros(10) le_Suc_eq by blast - -lemma UPNTIMES_Stars: - assumes "\ v : UPNTIMES r n" - shows "\vs. v = Stars vs \ (\v \ set vs. \ v : r) \ length vs \ n" -using assms -apply(induct n arbitrary: v) -apply(erule Prf.cases) -apply(simp_all) -apply(erule Prf.cases) -apply(simp_all) -apply(auto) -using le_SucI by blast - -lemma NTIMES_Stars: - assumes "\ v : NTIMES r n" - shows "\vs. v = Stars vs \ (\v \ set vs. \ v : r) \ length vs = n" -using assms -apply(induct n arbitrary: v) -apply(erule Prf.cases) -apply(simp_all) -apply(erule Prf.cases) -apply(simp_all) -apply(auto) +apply(rule Prf_STAR) +apply(simp) +apply(rule_tac x="length vs" in bexI) +apply(rule Prf_Pow) +apply(simp) +apply(simp) +apply(rule Prf_Pow) +apply(simp) +apply(rule_tac x="length vs" in bexI) +apply(rule Prf_Pow) +apply(simp) +apply(simp) done lemma Star_string: @@ -502,14 +481,6 @@ apply(simp) done -lemma L_flat_Prf1: - assumes "\ v : r" shows "flat v \ L r" -using assms -apply(induct) -apply(auto simp add: Sequ_def) -apply(rule_tac x="Suc x" in bexI) -apply(auto simp add: Sequ_def)[2] -done lemma L_flat_Prf2: assumes "s \ L r" shows "\v. \ v : r \ flat v = s" @@ -523,34 +494,38 @@ apply(auto)[1] apply(rule_tac x="Stars vs" in exI) apply(simp) -apply (simp add: Prf_Stars) -apply(drule Star_string) -apply(auto) -apply(rule Star_val) -apply(auto) +apply(rule Prf.intros) +apply(simp) +using Star_string Star_val apply force apply(subgoal_tac "\vs::val list. concat (map flat vs) = s \ (\v \ set vs. \ v : r) \ (length vs = x)") apply(auto)[1] apply(rule_tac x="Stars vs" in exI) apply(simp) -apply(drule_tac n="length vs" in Prf_Stars_UPNTIMES) +apply(rule Prf.intros) apply(simp) -using Prf_UPNTIMES_bigger apply blast -apply(drule Star_Pow) -apply(auto) -using Star_val_length apply blast +apply(simp) +using Star_Pow Star_val_length apply blast apply(subgoal_tac "\vs::val list. concat (map flat vs) = s \ (\v \ set vs. \ v : r) \ (length vs = x2)") apply(auto)[1] apply(rule_tac x="Stars vs" in exI) apply(simp) -apply(rule Prf_Stars_NTIMES) +apply(rule Prf.intros) apply(simp) apply(simp) -using Star_Pow Star_val_length by blast - +using Star_Pow Star_val_length apply blast +apply(subgoal_tac "\vs::val list. concat (map flat vs) = s \ (\v \ set vs. \ v : r) \ (length vs = x)") +apply(auto)[1] +apply(rule_tac x="Stars vs" in exI) +apply(simp) +apply(rule Prf.intros) +apply(simp) +apply(simp) +using Star_Pow Star_val_length apply blast +done lemma L_flat_Prf: "L(r) = {flat v | v. \ v : r}" -using L_flat_Prf1 L_flat_Prf2 by blast +using Prf_flat_L L_flat_Prf2 by blast section {* Sulzmann and Lu functions *} @@ -564,7 +539,7 @@ | "mkeps(STAR r) = Stars []" | "mkeps(UPNTIMES r n) = Stars []" | "mkeps(NTIMES r n) = Stars (replicate n (mkeps r))" - +| "mkeps(FROMNTIMES r n) = Stars (replicate n (mkeps r))" fun injval :: "rexp \ char \ val \ val" where @@ -577,7 +552,7 @@ | "injval (STAR r) c (Seq v (Stars vs)) = Stars ((injval r c v) # vs)" | "injval (UPNTIMES r n) c (Seq v (Stars vs)) = Stars ((injval r c v) # vs)" | "injval (NTIMES r n) c (Seq v (Stars vs)) = Stars ((injval r c v) # vs)" - +| "injval (FROMNTIMES r n) c (Seq v (Stars vs)) = Stars ((injval r c v) # vs)" section {* Mkeps, injval *} @@ -587,13 +562,6 @@ using assms apply(induct r rule: mkeps.induct) apply(auto intro: Prf.intros) -using Prf.intros(8) Prf_UPNTIMES_bigger apply blast -apply(case_tac n) -apply(auto) -apply(rule Prf.intros) -apply(rule Prf_Stars_NTIMES) -apply(simp) -apply(simp) done lemma mkeps_flat: @@ -612,10 +580,9 @@ (* STAR *) apply(rotate_tac 2) apply(erule Prf.cases) -apply(simp_all)[7] +apply(simp_all) apply(auto) -apply (metis Prf.intros(6) Prf.intros(7)) -apply (metis Prf.intros(7)) +using Prf.intros(6) apply auto[1] (* UPNTIMES *) apply(case_tac x2) apply(simp) @@ -624,12 +591,11 @@ apply(erule Prf.cases) apply(simp_all) apply(clarify) -apply(drule UPNTIMES_Stars) +apply(rotate_tac 2) +apply(erule Prf.cases) +apply(simp_all) apply(clarify) -apply(simp) -apply(rule Prf.intros(9)) -apply(simp) -using Prf_Stars_UPNTIMES Prf_UPNTIMES_bigger apply blast +using Prf.intros(7) apply auto[1] (* NTIMES *) apply(case_tac x2) apply(simp) @@ -638,12 +604,32 @@ apply(erule Prf.cases) apply(simp_all) apply(clarify) -apply(drule NTIMES_Stars) +apply(rotate_tac 2) +apply(erule Prf.cases) +apply(simp_all) apply(clarify) +using Prf.intros(8) apply auto[1] +(* FROMNTIMES *) +apply(case_tac x2) apply(simp) -apply(rule Prf.intros) -apply(simp) -using Prf_Stars_NTIMES by blast +apply(erule Prf.cases) +apply(simp_all) +apply(clarify) +apply(rotate_tac 2) +apply(erule Prf.cases) +apply(simp_all) +apply(clarify) +using Prf.intros(9) apply auto[1] +apply(rotate_tac 1) +apply(erule Prf.cases) +apply(simp_all) +apply(clarify) +apply(rotate_tac 2) +apply(erule Prf.cases) +apply(simp_all) +apply(clarify) +using Prf.intros(9) by auto + lemma Prf_injval_flat: assumes "\ v : der c r" @@ -655,16 +641,21 @@ apply(rotate_tac 2) apply(erule Prf.cases) apply(simp_all) -apply(drule UPNTIMES_Stars) -apply(clarify) -apply(simp) -apply(drule NTIMES_Stars) -apply(clarify) -apply(simp) +apply(rotate_tac 2) +apply(erule Prf.cases) +apply(simp_all) +apply(rotate_tac 2) +apply(erule Prf.cases) +apply(simp_all) +apply(rotate_tac 2) +apply(erule Prf.cases) +apply(simp_all) +apply(rotate_tac 2) +apply(erule Prf.cases) +apply(simp_all) done - section {* Our Alternative Posix definition *} inductive @@ -689,7 +680,10 @@ \(\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ (s1 @ s\<^sub>3) \ L r \ s\<^sub>4 \ L (NTIMES r n))\ \ (s1 @ s2) \ NTIMES r (Suc n) \ Stars (v # vs)" | Posix_NTIMES2: "[] \ NTIMES r 0 \ Stars []" - +| Posix_FROMNTIMES1: "\s1 \ r \ v; s2 \ FROMNTIMES r n \ Stars vs; + \(\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ (s1 @ s\<^sub>3) \ L r \ s\<^sub>4 \ L (FROMNTIMES r n))\ + \ (s1 @ s2) \ FROMNTIMES r (Suc n) \ Stars (v # vs)" +| Posix_FROMNTIMES2: "[] \ FROMNTIMES r 0 \ Stars []" inductive_cases Posix_elims: "s \ ZERO \ v" @@ -707,6 +701,8 @@ apply(auto simp add: Sequ_def) apply(rule_tac x="Suc x" in bexI) apply(auto simp add: Sequ_def) +apply(rule_tac x="Suc x" in bexI) +apply(auto simp add: Sequ_def) done @@ -716,20 +712,29 @@ using assms apply(induct s r v rule: Posix.induct) apply(auto intro: Prf.intros) -using Prf.intros(8) Prf_UPNTIMES_bigger by blast - -lemma Posix_NTIMES_length: - assumes "s \ NTIMES r n \ Stars vs" - shows "length vs = n" -using assms -using NTIMES_Stars Posix1a val.inject(5) by blast - -lemma Posix_UPNTIMES_length: - assumes "s \ UPNTIMES r n \ Stars vs" - shows "length vs \ n" -using assms -using Posix1a UPNTIMES_Stars val.inject(5) by blast - +apply(rule Prf.intros) +apply(auto)[1] +apply(rotate_tac 3) +apply(erule Prf.cases) +apply(simp_all) +apply(rule Prf.intros) +apply(auto)[1] +apply(rotate_tac 3) +apply(erule Prf.cases) +apply(simp_all) +apply (smt Posix_UPNTIMES2 Posix_elims(2) Prf.simps le_0_eq le_Suc_eq length_0_conv nat_induct nullable.simps(3) nullable.simps(7) rexp.distinct(61) rexp.distinct(67) rexp.distinct(69) rexp.inject(5) val.inject(5) val.simps(29) val.simps(33) val.simps(35)) +apply(rule Prf.intros) +apply(auto)[1] +apply(rotate_tac 3) +apply(erule Prf.cases) +apply(simp_all) +apply (smt Prf.simps rexp.distinct(63) rexp.distinct(67) rexp.distinct(71) rexp.inject(6) val.distinct(17) val.distinct(9) val.inject(5) val.simps(29) val.simps(33) val.simps(35)) +apply(rule Prf.intros) +apply(auto)[1] +apply(rotate_tac 3) +apply(erule Prf.cases) +apply(simp_all) +using Prf.simps by blast lemma Posix_NTIMES_mkeps: assumes "[] \ r \ mkeps r" @@ -744,6 +749,20 @@ apply(rule assms) done +lemma Posix_FROMNTIMES_mkeps: + assumes "[] \ r \ mkeps r" + shows "[] \ FROMNTIMES r n \ Stars (replicate n (mkeps r))" +apply(induct n) +apply(simp) +apply (rule Posix_FROMNTIMES2) +apply(simp) +apply(subst append_Nil[symmetric]) +apply (rule Posix_FROMNTIMES1) +apply(auto) +apply(rule assms) +done + + lemma Posix_mkeps: assumes "nullable r" shows "[] \ r \ mkeps r" @@ -756,8 +775,21 @@ apply(case_tac n) apply(simp) apply (simp add: Posix_NTIMES2) +apply(simp) +apply(subst append.simps(1)[symmetric]) +apply(rule Posix.intros) +apply(auto) apply(rule Posix_NTIMES_mkeps) apply(simp) +apply(case_tac n) +apply(simp) +apply (simp add: Posix_FROMNTIMES2) +apply(simp) +apply(subst append.simps(1)[symmetric]) +apply(rule Posix.intros) +apply(auto) +apply(rule Posix_FROMNTIMES_mkeps) +apply(simp) done @@ -861,8 +893,58 @@ have IHs: "\v2. s1 \ r \ v2 \ v = v2" "\v2. s2 \ NTIMES r n \ v2 \ Stars vs = v2" by fact+ ultimately show "Stars (v # vs) = v2" by auto +next + case (Posix_FROMNTIMES2 r v2) + have "[] \ FROMNTIMES r 0 \ v2" by fact + then show "Stars [] = v2" by cases (auto simp add: Posix1) +next + case (Posix_FROMNTIMES1 s1 r v s2 n vs v2) + have "(s1 @ s2) \ FROMNTIMES r (Suc n) \ v2" + "s1 \ r \ v" "s2 \ (FROMNTIMES r n) \ Stars vs" + "\ (\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ s1 @ s\<^sub>3 \ L r \ s\<^sub>4 \ L (FROMNTIMES r n))" by fact+ + then obtain v' vs' where "v2 = Stars (v' # vs')" "s1 \ r \ v'" "s2 \ (FROMNTIMES r n) \ (Stars vs')" + apply(cases) apply (auto simp add: append_eq_append_conv2) + using Posix1(1) apply fastforce + by (metis Posix1(1) Posix_FROMNTIMES1.hyps(5) append_Nil2 self_append_conv2) + moreover + have IHs: "\v2. s1 \ r \ v2 \ v = v2" + "\v2. s2 \ FROMNTIMES r n \ v2 \ Stars vs = v2" by fact+ + ultimately show "Stars (v # vs) = v2" by auto qed +lemma NTIMES_Stars: + assumes "\ v : NTIMES r n" + shows "\vs. v = Stars vs \ (\v \ set vs. \ v : r) \ length vs = n" +using assms +apply(induct n arbitrary: v) +apply(erule Prf.cases) +apply(simp_all) +apply(erule Prf.cases) +apply(simp_all) +done + +lemma UPNTIMES_Stars: + assumes "\ v : UPNTIMES r n" + shows "\vs. v = Stars vs \ (\v \ set vs. \ v : r) \ length vs \ n" +using assms +apply(induct n arbitrary: v) +apply(erule Prf.cases) +apply(simp_all) +apply(erule Prf.cases) +apply(simp_all) +done + +lemma FROMNTIMES_Stars: + assumes "\ v : FROMNTIMES r n" + shows "\vs. v = Stars vs \ (\v \ set vs. \ v : r) \ n \ length vs" +using assms +apply(induct n arbitrary: v) +apply(erule Prf.cases) +apply(simp_all) +apply(erule Prf.cases) +apply(simp_all) +done + lemma Posix_injval: assumes "s \ (der c r) \ v" @@ -1025,7 +1107,7 @@ apply(simp) using Posix_elims(1) apply blast apply(simp) - apply(auto elim!: Posix_elims(1-5) simp add: der_correctness Der_def intro: Posix.intros) + apply(auto elim!: Posix_elims(1-5) simp add: der_correctness Der_def intro: Posix.intros) by (metis Posix1a UPNTIMES_Stars) then show "(c # s) \ UPNTIMES r n \ injval (UPNTIMES r n) c v" proof (cases) @@ -1087,6 +1169,61 @@ done then show "(c # s) \ NTIMES r n \ injval (NTIMES r n) c v" using cons by(simp) qed +next + case (FROMNTIMES r n) + have IH: "\s v. s \ der c r \ v \ (c # s) \ r \ injval r c v" by fact + have "s \ der c (FROMNTIMES r n) \ v" by fact + then consider + (null) v1 vs s1 s2 where + "v = Seq v1 (Stars vs)" "s = s1 @ s2" + "s1 \ der c r \ v1" "s2 \ (FROMNTIMES r 0) \ (Stars vs)" + "\ (\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ s1 @ s\<^sub>3 \ L (der c r) \ s\<^sub>4 \ L (FROMNTIMES r 0))" + | (cons) m v1 vs s1 s2 where + "n = Suc m" + "v = Seq v1 (Stars vs)" "s = s1 @ s2" + "s1 \ der c r \ v1" "s2 \ (FROMNTIMES r m) \ (Stars vs)" + "\ (\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ s1 @ s\<^sub>3 \ L (der c r) \ s\<^sub>4 \ L (FROMNTIMES r m))" + apply(case_tac n) + apply(simp) + apply(auto elim!: Posix_elims(1-5) simp add: der_correctness Der_def intro: Posix.intros) + defer + apply (metis FROMNTIMES_Stars Posix1a) + apply(rotate_tac 5) + apply(erule_tac Posix_elims(6)) + apply(auto) + apply(drule_tac x="v1" in meta_spec) + apply(simp) + apply(drule_tac x="v # vs" in meta_spec) + apply(simp) + apply(drule_tac x="s1" in meta_spec) + apply(simp) + apply(drule_tac x="s1a @ s2a" in meta_spec) + apply(simp) + apply(drule meta_mp) + defer + using Pow_in_Star apply blast + apply (meson Posix_FROMNTIMES2 append_is_Nil_conv self_append_conv) + sorry + then show "(c # s) \ FROMNTIMES r n \ injval (FROMNTIMES r n) c v" + proof (cases) + case cons + have "n = Suc m" by fact + moreover + have "s1 \ der c r \ v1" by fact + then have "(c # s1) \ r \ injval r c v1" using IH by simp + moreover + have "s2 \ FROMNTIMES r m \ Stars vs" by fact + moreover + have "\ (\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ s1 @ s\<^sub>3 \ L (der c r) \ s\<^sub>4 \ L (FROMNTIMES r m))" by fact + then have "\ (\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ (c # s1) @ s\<^sub>3 \ L r \ s\<^sub>4 \ L (FROMNTIMES r m))" + by (simp add: der_correctness Der_def) + ultimately + have "((c # s1) @ s2) \ FROMNTIMES r (Suc m) \ Stars (injval r c v1 # vs)" + apply(rule_tac Posix.intros(12)) + apply(simp_all) + done + then show "(c # s) \ FROMNTIMES r n \ injval (FROMNTIMES r n) c v" using cons by(simp) + qed qed