diff -r f9cdc295ccf7 -r f493a20feeb3 thys3/src/Lexer.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thys3/src/Lexer.thy Sat Apr 30 00:50:08 2022 +0100 @@ -0,0 +1,417 @@ + +theory Lexer + imports PosixSpec +begin + +section {* The Lexer Functions by Sulzmann and Lu (without simplification) *} + +fun + mkeps :: "rexp \ val" +where + "mkeps(ONE) = Void" +| "mkeps(SEQ r1 r2) = Seq (mkeps r1) (mkeps r2)" +| "mkeps(ALT r1 r2) = (if nullable(r1) then Left (mkeps r1) else Right (mkeps r2))" +| "mkeps(STAR r) = Stars []" + +fun injval :: "rexp \ char \ val \ val" +where + "injval (CH d) c Void = Char d" +| "injval (ALT r1 r2) c (Left v1) = Left(injval r1 c v1)" +| "injval (ALT r1 r2) c (Right v2) = Right(injval r2 c v2)" +| "injval (SEQ r1 r2) c (Seq v1 v2) = Seq (injval r1 c v1) v2" +| "injval (SEQ r1 r2) c (Left (Seq v1 v2)) = Seq (injval r1 c v1) v2" +| "injval (SEQ r1 r2) c (Right v2) = Seq (mkeps r1) (injval r2 c v2)" +| "injval (STAR r) c (Seq v (Stars vs)) = Stars ((injval r c v) # vs)" + +fun + lexer :: "rexp \ string \ val option" +where + "lexer r [] = (if nullable r then Some(mkeps r) else None)" +| "lexer r (c#s) = (case (lexer (der c r) s) of + None \ None + | Some(v) \ Some(injval r c v))" + + + +section {* Mkeps, Injval Properties *} + +lemma mkeps_nullable: + assumes "nullable(r)" + shows "\ mkeps r : r" +using assms +by (induct rule: nullable.induct) + (auto intro: Prf.intros) + +lemma mkeps_flat: + assumes "nullable(r)" + shows "flat (mkeps r) = []" +using assms +by (induct rule: nullable.induct) (auto) + +lemma Prf_injval_flat: + assumes "\ v : der c r" + shows "flat (injval r c v) = c # (flat v)" +using assms +apply(induct c r arbitrary: v rule: der.induct) +apply(auto elim!: Prf_elims intro: mkeps_flat split: if_splits) +done + +lemma Prf_injval: + assumes "\ v : der c r" + shows "\ (injval r c v) : r" +using assms +apply(induct r arbitrary: c v rule: rexp.induct) +apply(auto intro!: Prf.intros mkeps_nullable elim!: Prf_elims split: if_splits) +apply(simp add: Prf_injval_flat) +done + + + +text {* + Mkeps and injval produce, or preserve, Posix values. +*} + +lemma Posix_mkeps: + assumes "nullable r" + shows "[] \ r \ mkeps r" +using assms +apply(induct r rule: nullable.induct) +apply(auto intro: Posix.intros simp add: nullable_correctness Sequ_def) +apply(subst append.simps(1)[symmetric]) +apply(rule Posix.intros) +apply(auto) +done + +lemma Posix_injval: + assumes "s \ (der c r) \ v" + shows "(c # s) \ r \ (injval r c v)" +using assms +proof(induct r arbitrary: s v rule: rexp.induct) + case ZERO + have "s \ der c ZERO \ v" by fact + then have "s \ ZERO \ v" by simp + then have "False" by cases + then show "(c # s) \ ZERO \ (injval ZERO c v)" by simp +next + case ONE + have "s \ der c ONE \ v" by fact + then have "s \ ZERO \ v" by simp + then have "False" by cases + then show "(c # s) \ ONE \ (injval ONE c v)" by simp +next + case (CH d) + consider (eq) "c = d" | (ineq) "c \ d" by blast + then show "(c # s) \ (CH d) \ (injval (CH d) c v)" + proof (cases) + case eq + have "s \ der c (CH d) \ v" by fact + then have "s \ ONE \ v" using eq by simp + then have eqs: "s = [] \ v = Void" by cases simp + show "(c # s) \ CH d \ injval (CH d) c v" using eq eqs + by (auto intro: Posix.intros) + next + case ineq + have "s \ der c (CH d) \ v" by fact + then have "s \ ZERO \ v" using ineq by simp + then have "False" by cases + then show "(c # s) \ CH d \ injval (CH d) c v" by simp + qed +next + case (ALT r1 r2) + have IH1: "\s v. s \ der c r1 \ v \ (c # s) \ r1 \ injval r1 c v" by fact + have IH2: "\s v. s \ der c r2 \ v \ (c # s) \ r2 \ injval r2 c v" by fact + have "s \ der c (ALT r1 r2) \ v" by fact + then have "s \ ALT (der c r1) (der c r2) \ v" by simp + then consider (left) v' where "v = Left v'" "s \ der c r1 \ v'" + | (right) v' where "v = Right v'" "s \ L (der c r1)" "s \ der c r2 \ v'" + by cases auto + then show "(c # s) \ ALT r1 r2 \ injval (ALT r1 r2) c v" + proof (cases) + case left + have "s \ der c r1 \ v'" by fact + then have "(c # s) \ r1 \ injval r1 c v'" using IH1 by simp + then have "(c # s) \ ALT r1 r2 \ injval (ALT r1 r2) c (Left v')" by (auto intro: Posix.intros) + then show "(c # s) \ ALT r1 r2 \ injval (ALT r1 r2) c v" using left by simp + next + case right + have "s \ L (der c r1)" by fact + then have "c # s \ L r1" by (simp add: der_correctness Der_def) + moreover + have "s \ der c r2 \ v'" by fact + then have "(c # s) \ r2 \ injval r2 c v'" using IH2 by simp + ultimately have "(c # s) \ ALT r1 r2 \ injval (ALT r1 r2) c (Right v')" + by (auto intro: Posix.intros) + then show "(c # s) \ ALT r1 r2 \ injval (ALT r1 r2) c v" using right by simp + qed +next + case (SEQ r1 r2) + have IH1: "\s v. s \ der c r1 \ v \ (c # s) \ r1 \ injval r1 c v" by fact + have IH2: "\s v. s \ der c r2 \ v \ (c # s) \ r2 \ injval r2 c v" by fact + have "s \ der c (SEQ r1 r2) \ v" by fact + then consider + (left_nullable) v1 v2 s1 s2 where + "v = Left (Seq v1 v2)" "s = s1 @ s2" + "s1 \ der c r1 \ v1" "s2 \ r2 \ v2" "nullable r1" + "\ (\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ s1 @ s\<^sub>3 \ L (der c r1) \ s\<^sub>4 \ L r2)" + | (right_nullable) v1 s1 s2 where + "v = Right v1" "s = s1 @ s2" + "s \ der c r2 \ v1" "nullable r1" "s1 @ s2 \ L (SEQ (der c r1) r2)" + | (not_nullable) v1 v2 s1 s2 where + "v = Seq v1 v2" "s = s1 @ s2" + "s1 \ der c r1 \ v1" "s2 \ r2 \ v2" "\nullable r1" + "\ (\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = s2 \ s1 @ s\<^sub>3 \ L (der c r1) \ s\<^sub>4 \ L r2)" + by (force split: if_splits elim!: Posix_elims simp add: Sequ_def der_correctness Der_def) + then show "(c # s) \ SEQ r1 r2 \ injval (SEQ r1 r2) c v" + proof (cases) + case left_nullable + have "s1 \ der c r1 \ v1" by fact + then have "(c # s1) \ r1 \ injval r1 c v1" using IH1 by simp + 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 r1) \ s\<^sub>4 \ L r2)" 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 r1 \ s\<^sub>4 \ L r2)" by (simp add: der_correctness Der_def) + ultimately have "((c # s1) @ s2) \ SEQ r1 r2 \ Seq (injval r1 c v1) v2" using left_nullable by (rule_tac Posix.intros) + then show "(c # s) \ SEQ r1 r2 \ injval (SEQ r1 r2) c v" using left_nullable by simp + next + case right_nullable + have "nullable r1" by fact + then have "[] \ r1 \ (mkeps r1)" by (rule Posix_mkeps) + moreover + have "s \ der c r2 \ v1" by fact + then have "(c # s) \ r2 \ (injval r2 c v1)" using IH2 by simp + moreover + have "s1 @ s2 \ L (SEQ (der c r1) r2)" by fact + then have "\ (\s\<^sub>3 s\<^sub>4. s\<^sub>3 \ [] \ s\<^sub>3 @ s\<^sub>4 = c # s \ [] @ s\<^sub>3 \ L r1 \ s\<^sub>4 \ L r2)" using right_nullable + by(auto simp add: der_correctness Der_def append_eq_Cons_conv Sequ_def) + ultimately have "([] @ (c # s)) \ SEQ r1 r2 \ Seq (mkeps r1) (injval r2 c v1)" + by(rule Posix.intros) + then show "(c # s) \ SEQ r1 r2 \ injval (SEQ r1 r2) c v" using right_nullable by simp + next + case not_nullable + have "s1 \ der c r1 \ v1" by fact + then have "(c # s1) \ r1 \ injval r1 c v1" using IH1 by simp + 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 r1) \ s\<^sub>4 \ L r2)" 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 r1 \ s\<^sub>4 \ L r2)" by (simp add: der_correctness Der_def) + ultimately have "((c # s1) @ s2) \ SEQ r1 r2 \ Seq (injval r1 c v1) v2" using not_nullable + by (rule_tac Posix.intros) (simp_all) + then show "(c # s) \ SEQ r1 r2 \ injval (SEQ r1 r2) c v" using not_nullable by simp + qed +next + case (STAR r) + have IH: "\s v. s \ der c r \ v \ (c # s) \ r \ injval r c v" by fact + have "s \ der c (STAR r) \ v" by fact + then consider + (cons) v1 vs s1 s2 where + "v = Seq v1 (Stars vs)" "s = s1 @ s2" + "s1 \ der c r \ v1" "s2 \ (STAR r) \ (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 (STAR r))" + apply(auto elim!: Posix_elims(1-5) simp add: der_correctness Der_def intro: Posix.intros) + apply(rotate_tac 3) + apply(erule_tac Posix_elims(6)) + apply (simp add: Posix.intros(6)) + using Posix.intros(7) by blast + then show "(c # s) \ STAR r \ injval (STAR r) c v" + proof (cases) + case cons + have "s1 \ der c r \ v1" by fact + then have "(c # s1) \ r \ injval r c v1" using IH by simp + moreover + have "s2 \ STAR r \ Stars vs" by fact + moreover + have "(c # s1) \ r \ injval r c v1" by fact + then have "flat (injval r c v1) = (c # s1)" by (rule Posix1) + then have "flat (injval r c v1) \ []" by simp + 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 (STAR r))" 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 (STAR r))" + by (simp add: der_correctness Der_def) + ultimately + have "((c # s1) @ s2) \ STAR r \ Stars (injval r c v1 # vs)" by (rule Posix.intros) + then show "(c # s) \ STAR r \ injval (STAR r) c v" using cons by(simp) + qed +qed + + +section {* Lexer Correctness *} + + +lemma lexer_correct_None: + shows "s \ L r \ lexer r s = None" + apply(induct s arbitrary: r) + apply(simp) + apply(simp add: nullable_correctness) + apply(simp) + apply(drule_tac x="der a r" in meta_spec) + apply(auto) + apply(auto simp add: der_correctness Der_def) +done + +lemma lexer_correct_Some: + shows "s \ L r \ (\v. lexer r s = Some(v) \ s \ r \ v)" + apply(induct s arbitrary : r) + apply(simp only: lexer.simps) + apply(simp) + apply(simp add: nullable_correctness Posix_mkeps) + apply(drule_tac x="der a r" in meta_spec) + apply(simp (no_asm_use) add: der_correctness Der_def del: lexer.simps) + apply(simp del: lexer.simps) + apply(simp only: lexer.simps) + apply(case_tac "lexer (der a r) s = None") + apply(auto)[1] + apply(simp) + apply(erule exE) + apply(simp) + apply(rule iffI) + apply(simp add: Posix_injval) + apply(simp add: Posix1(1)) +done + +lemma lexer_correctness: + shows "(lexer r s = Some v) \ s \ r \ v" + and "(lexer r s = None) \ \(\v. s \ r \ v)" +using Posix1(1) Posix_determ lexer_correct_None lexer_correct_Some apply fastforce +using Posix1(1) lexer_correct_None lexer_correct_Some by blast + + +subsection {* A slight reformulation of the lexer algorithm using stacked functions*} + +fun flex :: "rexp \ (val \ val) => string \ (val \ val)" + where + "flex r f [] = f" +| "flex r f (c#s) = flex (der c r) (\v. f (injval r c v)) s" + +lemma flex_fun_apply: + shows "g (flex r f s v) = flex r (g o f) s v" + apply(induct s arbitrary: g f r v) + apply(simp_all add: comp_def) + by meson + +lemma flex_fun_apply2: + shows "g (flex r id s v) = flex r g s v" + by (simp add: flex_fun_apply) + + +lemma flex_append: + shows "flex r f (s1 @ s2) = flex (ders s1 r) (flex r f s1) s2" + apply(induct s1 arbitrary: s2 r f) + apply(simp_all) + done + +lemma lexer_flex: + shows "lexer r s = (if nullable (ders s r) + then Some(flex r id s (mkeps (ders s r))) else None)" + apply(induct s arbitrary: r) + apply(simp_all add: flex_fun_apply) + done + +lemma Posix_flex: + assumes "s2 \ (ders s1 r) \ v" + shows "(s1 @ s2) \ r \ flex r id s1 v" + using assms + apply(induct s1 arbitrary: r v s2) + apply(simp) + apply(simp) + apply(drule_tac x="der a r" in meta_spec) + apply(drule_tac x="v" in meta_spec) + apply(drule_tac x="s2" in meta_spec) + apply(simp) + using Posix_injval + apply(drule_tac Posix_injval) + apply(subst (asm) (5) flex_fun_apply) + apply(simp) + done + +lemma injval_inj: + assumes "\ a : (der c r)" "\ v : (der c r)" "injval r c a = injval r c v" + shows "a = v" + using assms + apply(induct r arbitrary: a c v) + apply(auto) + using Prf_elims(1) apply blast + using Prf_elims(1) apply blast + apply(case_tac "c = x") + apply(auto) + using Prf_elims(4) apply auto[1] + using Prf_elims(1) apply blast + prefer 2 + apply (smt Prf_elims(3) injval.simps(2) injval.simps(3) val.distinct(25) val.inject(3) val.inject(4)) + apply(case_tac "nullable r1") + apply(auto) + apply(erule Prf_elims) + apply(erule Prf_elims) + apply(erule Prf_elims) + apply(erule Prf_elims) + apply(auto) + apply (metis Prf_injval_flat list.distinct(1) mkeps_flat) + apply(erule Prf_elims) + apply(erule Prf_elims) + apply(auto) + using Prf_injval_flat mkeps_flat apply fastforce + apply(erule Prf_elims) + apply(erule Prf_elims) + apply(auto) + apply(erule Prf_elims) + apply(erule Prf_elims) + apply(auto) + apply (smt Prf_elims(6) injval.simps(7) list.inject val.inject(5)) + by (smt Prf_elims(6) injval.simps(7) list.inject val.inject(5)) + + + +lemma uu: + assumes "(c # s) \ r \ injval r c v" "\ v : (der c r)" + shows "s \ der c r \ v" + using assms + apply - + apply(subgoal_tac "lexer r (c # s) = Some (injval r c v)") + prefer 2 + using lexer_correctness(1) apply blast + apply(simp add: ) + apply(case_tac "lexer (der c r) s") + apply(simp) + apply(simp) + apply(case_tac "s \ der c r \ a") + prefer 2 + apply (simp add: lexer_correctness(1)) + apply(subgoal_tac "\ a : (der c r)") + prefer 2 + using Posix_Prf apply blast + using injval_inj by blast + + +lemma Posix_flex2: + assumes "(s1 @ s2) \ r \ flex r id s1 v" "\ v : ders s1 r" + shows "s2 \ (ders s1 r) \ v" + using assms + apply(induct s1 arbitrary: r v s2 rule: rev_induct) + apply(simp) + apply(simp) + apply(drule_tac x="r" in meta_spec) + apply(drule_tac x="injval (ders xs r) x v" in meta_spec) + apply(drule_tac x="x#s2" in meta_spec) + apply(simp add: flex_append ders_append) + using Prf_injval uu by blast + +lemma Posix_flex3: + assumes "s1 \ r \ flex r id s1 v" "\ v : ders s1 r" + shows "[] \ (ders s1 r) \ v" + using assms + by (simp add: Posix_flex2) + +lemma flex_injval: + shows "flex (der a r) (injval r a) s v = injval r a (flex (der a r) id s v)" + by (simp add: flex_fun_apply) + +lemma Prf_flex: + assumes "\ v : ders s r" + shows "\ flex r id s v : r" + using assms + apply(induct s arbitrary: v r) + apply(simp) + apply(simp) + by (simp add: Prf_injval flex_injval) + + +unused_thms + +end \ No newline at end of file