--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/Derivatives.thy Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,370 @@
+section "Derivatives of regular expressions"
+
+(* Author: Christian Urban *)
+
+theory Derivatives
+imports Regular_Exp
+begin
+
+text{* This theory is based on work by Brozowski \cite{Brzozowski64} and Antimirov \cite{Antimirov95}. *}
+
+subsection {* Brzozowski's derivatives of regular expressions *}
+
+primrec
+ deriv :: "'a \<Rightarrow> 'a rexp \<Rightarrow> 'a rexp"
+where
+ "deriv c (Zero) = Zero"
+| "deriv c (One) = Zero"
+| "deriv c (Atom c') = (if c = c' then One else Zero)"
+| "deriv c (Plus r1 r2) = Plus (deriv c r1) (deriv c r2)"
+| "deriv c (Times r1 r2) =
+ (if nullable r1 then Plus (Times (deriv c r1) r2) (deriv c r2) else Times (deriv c r1) r2)"
+| "deriv c (Star r) = Times (deriv c r) (Star r)"
+
+primrec
+ derivs :: "'a list \<Rightarrow> 'a rexp \<Rightarrow> 'a rexp"
+where
+ "derivs [] r = r"
+| "derivs (c # s) r = derivs s (deriv c r)"
+
+
+lemma atoms_deriv_subset: "atoms (deriv x r) \<subseteq> atoms r"
+by (induction r) (auto)
+
+lemma atoms_derivs_subset: "atoms (derivs w r) \<subseteq> atoms r"
+by (induction w arbitrary: r) (auto dest: atoms_deriv_subset[THEN subsetD])
+
+lemma lang_deriv: "lang (deriv c r) = Deriv c (lang r)"
+by (induct r) (simp_all add: nullable_iff)
+
+lemma lang_derivs: "lang (derivs s r) = Derivs s (lang r)"
+by (induct s arbitrary: r) (simp_all add: lang_deriv)
+
+text {* A regular expression matcher: *}
+
+definition matcher :: "'a rexp \<Rightarrow> 'a list \<Rightarrow> bool" where
+"matcher r s = nullable (derivs s r)"
+
+lemma matcher_correctness: "matcher r s \<longleftrightarrow> s \<in> lang r"
+by (induct s arbitrary: r)
+ (simp_all add: nullable_iff lang_deriv matcher_def Deriv_def)
+
+
+subsection {* Antimirov's partial derivatives *}
+
+abbreviation
+ "Timess rs r \<equiv> (\<Union>r' \<in> rs. {Times r' r})"
+
+primrec
+ pderiv :: "'a \<Rightarrow> 'a rexp \<Rightarrow> 'a rexp set"
+where
+ "pderiv c Zero = {}"
+| "pderiv c One = {}"
+| "pderiv c (Atom c') = (if c = c' then {One} else {})"
+| "pderiv c (Plus r1 r2) = (pderiv c r1) \<union> (pderiv c r2)"
+| "pderiv c (Times r1 r2) =
+ (if nullable r1 then Timess (pderiv c r1) r2 \<union> pderiv c r2 else Timess (pderiv c r1) r2)"
+| "pderiv c (Star r) = Timess (pderiv c r) (Star r)"
+
+primrec
+ pderivs :: "'a list \<Rightarrow> 'a rexp \<Rightarrow> ('a rexp) set"
+where
+ "pderivs [] r = {r}"
+| "pderivs (c # s) r = \<Union> (pderivs s ` pderiv c r)"
+
+abbreviation
+ pderiv_set :: "'a \<Rightarrow> 'a rexp set \<Rightarrow> 'a rexp set"
+where
+ "pderiv_set c rs \<equiv> \<Union> (pderiv c ` rs)"
+
+abbreviation
+ pderivs_set :: "'a list \<Rightarrow> 'a rexp set \<Rightarrow> 'a rexp set"
+where
+ "pderivs_set s rs \<equiv> \<Union> (pderivs s ` rs)"
+
+lemma pderivs_append:
+ "pderivs (s1 @ s2) r = \<Union> (pderivs s2 ` pderivs s1 r)"
+by (induct s1 arbitrary: r) (simp_all)
+
+lemma pderivs_snoc:
+ shows "pderivs (s @ [c]) r = pderiv_set c (pderivs s r)"
+by (simp add: pderivs_append)
+
+lemma pderivs_simps [simp]:
+ shows "pderivs s Zero = (if s = [] then {Zero} else {})"
+ and "pderivs s One = (if s = [] then {One} else {})"
+ and "pderivs s (Plus r1 r2) = (if s = [] then {Plus r1 r2} else (pderivs s r1) \<union> (pderivs s r2))"
+by (induct s) (simp_all)
+
+lemma pderivs_Atom:
+ shows "pderivs s (Atom c) \<subseteq> {Atom c, One}"
+by (induct s) (simp_all)
+
+subsection {* Relating left-quotients and partial derivatives *}
+
+lemma Deriv_pderiv:
+ shows "Deriv c (lang r) = \<Union> (lang ` pderiv c r)"
+by (induct r) (auto simp add: nullable_iff conc_UNION_distrib)
+
+lemma Derivs_pderivs:
+ shows "Derivs s (lang r) = \<Union> (lang ` pderivs s r)"
+proof (induct s arbitrary: r)
+ case (Cons c s)
+ have ih: "\<And>r. Derivs s (lang r) = \<Union> (lang ` pderivs s r)" by fact
+ have "Derivs (c # s) (lang r) = Derivs s (Deriv c (lang r))" by simp
+ also have "\<dots> = Derivs s (\<Union> (lang ` pderiv c r))" by (simp add: Deriv_pderiv)
+ also have "\<dots> = Derivss s (lang ` (pderiv c r))"
+ by (auto simp add: Derivs_def)
+ also have "\<dots> = \<Union> (lang ` (pderivs_set s (pderiv c r)))"
+ using ih by auto
+ also have "\<dots> = \<Union> (lang ` (pderivs (c # s) r))" by simp
+ finally show "Derivs (c # s) (lang r) = \<Union> (lang ` pderivs (c # s) r)" .
+qed (simp add: Derivs_def)
+
+subsection {* Relating derivatives and partial derivatives *}
+
+lemma deriv_pderiv:
+ shows "\<Union> (lang ` (pderiv c r)) = lang (deriv c r)"
+unfolding lang_deriv Deriv_pderiv by simp
+
+lemma derivs_pderivs:
+ shows "\<Union> (lang ` (pderivs s r)) = lang (derivs s r)"
+unfolding lang_derivs Derivs_pderivs by simp
+
+
+subsection {* Finiteness property of partial derivatives *}
+
+definition
+ pderivs_lang :: "'a lang \<Rightarrow> 'a rexp \<Rightarrow> 'a rexp set"
+where
+ "pderivs_lang A r \<equiv> \<Union>x \<in> A. pderivs x r"
+
+lemma pderivs_lang_subsetI:
+ assumes "\<And>s. s \<in> A \<Longrightarrow> pderivs s r \<subseteq> C"
+ shows "pderivs_lang A r \<subseteq> C"
+using assms unfolding pderivs_lang_def by (rule UN_least)
+
+lemma pderivs_lang_union:
+ shows "pderivs_lang (A \<union> B) r = (pderivs_lang A r \<union> pderivs_lang B r)"
+by (simp add: pderivs_lang_def)
+
+lemma pderivs_lang_subset:
+ shows "A \<subseteq> B \<Longrightarrow> pderivs_lang A r \<subseteq> pderivs_lang B r"
+by (auto simp add: pderivs_lang_def)
+
+definition
+ "UNIV1 \<equiv> UNIV - {[]}"
+
+lemma pderivs_lang_Zero [simp]:
+ shows "pderivs_lang UNIV1 Zero = {}"
+unfolding UNIV1_def pderivs_lang_def by auto
+
+lemma pderivs_lang_One [simp]:
+ shows "pderivs_lang UNIV1 One = {}"
+unfolding UNIV1_def pderivs_lang_def by (auto split: if_splits)
+
+lemma pderivs_lang_Atom [simp]:
+ shows "pderivs_lang UNIV1 (Atom c) = {One}"
+unfolding UNIV1_def pderivs_lang_def
+apply(auto)
+apply(frule rev_subsetD)
+apply(rule pderivs_Atom)
+apply(simp)
+apply(case_tac xa)
+apply(auto split: if_splits)
+done
+
+lemma pderivs_lang_Plus [simp]:
+ shows "pderivs_lang UNIV1 (Plus r1 r2) = pderivs_lang UNIV1 r1 \<union> pderivs_lang UNIV1 r2"
+unfolding UNIV1_def pderivs_lang_def by auto
+
+
+text {* Non-empty suffixes of a string (needed for the cases of @{const Times} and @{const Star} below) *}
+
+definition
+ "PSuf s \<equiv> {v. v \<noteq> [] \<and> (\<exists>u. u @ v = s)}"
+
+lemma PSuf_snoc:
+ shows "PSuf (s @ [c]) = (PSuf s) @@ {[c]} \<union> {[c]}"
+unfolding PSuf_def conc_def
+by (auto simp add: append_eq_append_conv2 append_eq_Cons_conv)
+
+lemma PSuf_Union:
+ shows "(\<Union>v \<in> PSuf s @@ {[c]}. f v) = (\<Union>v \<in> PSuf s. f (v @ [c]))"
+by (auto simp add: conc_def)
+
+lemma pderivs_lang_snoc:
+ shows "pderivs_lang (PSuf s @@ {[c]}) r = (pderiv_set c (pderivs_lang (PSuf s) r))"
+unfolding pderivs_lang_def
+by (simp add: PSuf_Union pderivs_snoc)
+
+lemma pderivs_Times:
+ shows "pderivs s (Times r1 r2) \<subseteq> Timess (pderivs s r1) r2 \<union> (pderivs_lang (PSuf s) r2)"
+proof (induct s rule: rev_induct)
+ case (snoc c s)
+ have ih: "pderivs s (Times r1 r2) \<subseteq> Timess (pderivs s r1) r2 \<union> (pderivs_lang (PSuf s) r2)"
+ by fact
+ have "pderivs (s @ [c]) (Times r1 r2) = pderiv_set c (pderivs s (Times r1 r2))"
+ by (simp add: pderivs_snoc)
+ also have "\<dots> \<subseteq> pderiv_set c (Timess (pderivs s r1) r2 \<union> (pderivs_lang (PSuf s) r2))"
+ using ih by fast
+ also have "\<dots> = pderiv_set c (Timess (pderivs s r1) r2) \<union> pderiv_set c (pderivs_lang (PSuf s) r2)"
+ by (simp)
+ also have "\<dots> = pderiv_set c (Timess (pderivs s r1) r2) \<union> pderivs_lang (PSuf s @@ {[c]}) r2"
+ by (simp add: pderivs_lang_snoc)
+ also
+ have "\<dots> \<subseteq> pderiv_set c (Timess (pderivs s r1) r2) \<union> pderiv c r2 \<union> pderivs_lang (PSuf s @@ {[c]}) r2"
+ by auto
+ also
+ have "\<dots> \<subseteq> Timess (pderiv_set c (pderivs s r1)) r2 \<union> pderiv c r2 \<union> pderivs_lang (PSuf s @@ {[c]}) r2"
+ by (auto simp add: if_splits)
+ also have "\<dots> = Timess (pderivs (s @ [c]) r1) r2 \<union> pderiv c r2 \<union> pderivs_lang (PSuf s @@ {[c]}) r2"
+ by (simp add: pderivs_snoc)
+ also have "\<dots> \<subseteq> Timess (pderivs (s @ [c]) r1) r2 \<union> pderivs_lang (PSuf (s @ [c])) r2"
+ unfolding pderivs_lang_def by (auto simp add: PSuf_snoc)
+ finally show ?case .
+qed (simp)
+
+lemma pderivs_lang_Times_aux1:
+ assumes a: "s \<in> UNIV1"
+ shows "pderivs_lang (PSuf s) r \<subseteq> pderivs_lang UNIV1 r"
+using a unfolding UNIV1_def PSuf_def pderivs_lang_def by auto
+
+lemma pderivs_lang_Times_aux2:
+ assumes a: "s \<in> UNIV1"
+ shows "Timess (pderivs s r1) r2 \<subseteq> Timess (pderivs_lang UNIV1 r1) r2"
+using a unfolding pderivs_lang_def by auto
+
+lemma pderivs_lang_Times:
+ shows "pderivs_lang UNIV1 (Times r1 r2) \<subseteq> Timess (pderivs_lang UNIV1 r1) r2 \<union> pderivs_lang UNIV1 r2"
+apply(rule pderivs_lang_subsetI)
+apply(rule subset_trans)
+apply(rule pderivs_Times)
+using pderivs_lang_Times_aux1 pderivs_lang_Times_aux2
+apply(blast)
+done
+
+lemma pderivs_Star:
+ assumes a: "s \<noteq> []"
+ shows "pderivs s (Star r) \<subseteq> Timess (pderivs_lang (PSuf s) r) (Star r)"
+using a
+proof (induct s rule: rev_induct)
+ case (snoc c s)
+ have ih: "s \<noteq> [] \<Longrightarrow> pderivs s (Star r) \<subseteq> Timess (pderivs_lang (PSuf s) r) (Star r)" by fact
+ { assume asm: "s \<noteq> []"
+ have "pderivs (s @ [c]) (Star r) = pderiv_set c (pderivs s (Star r))" by (simp add: pderivs_snoc)
+ also have "\<dots> \<subseteq> pderiv_set c (Timess (pderivs_lang (PSuf s) r) (Star r))"
+ using ih[OF asm] by fast
+ also have "\<dots> \<subseteq> Timess (pderiv_set c (pderivs_lang (PSuf s) r)) (Star r) \<union> pderiv c (Star r)"
+ by (auto split: if_splits)
+ also have "\<dots> \<subseteq> Timess (pderivs_lang (PSuf (s @ [c])) r) (Star r) \<union> (Timess (pderiv c r) (Star r))"
+ by (simp only: PSuf_snoc pderivs_lang_snoc pderivs_lang_union)
+ (auto simp add: pderivs_lang_def)
+ also have "\<dots> = Timess (pderivs_lang (PSuf (s @ [c])) r) (Star r)"
+ by (auto simp add: PSuf_snoc PSuf_Union pderivs_snoc pderivs_lang_def)
+ finally have ?case .
+ }
+ moreover
+ { assume asm: "s = []"
+ then have ?case by (auto simp add: pderivs_lang_def pderivs_snoc PSuf_def)
+ }
+ ultimately show ?case by blast
+qed (simp)
+
+lemma pderivs_lang_Star:
+ shows "pderivs_lang UNIV1 (Star r) \<subseteq> Timess (pderivs_lang UNIV1 r) (Star r)"
+apply(rule pderivs_lang_subsetI)
+apply(rule subset_trans)
+apply(rule pderivs_Star)
+apply(simp add: UNIV1_def)
+apply(simp add: UNIV1_def PSuf_def)
+apply(auto simp add: pderivs_lang_def)
+done
+
+lemma finite_Timess [simp]:
+ assumes a: "finite A"
+ shows "finite (Timess A r)"
+using a by auto
+
+lemma finite_pderivs_lang_UNIV1:
+ shows "finite (pderivs_lang UNIV1 r)"
+apply(induct r)
+apply(simp_all add:
+ finite_subset[OF pderivs_lang_Times]
+ finite_subset[OF pderivs_lang_Star])
+done
+
+lemma pderivs_lang_UNIV:
+ shows "pderivs_lang UNIV r = pderivs [] r \<union> pderivs_lang UNIV1 r"
+unfolding UNIV1_def pderivs_lang_def
+by blast
+
+lemma finite_pderivs_lang_UNIV:
+ shows "finite (pderivs_lang UNIV r)"
+unfolding pderivs_lang_UNIV
+by (simp add: finite_pderivs_lang_UNIV1)
+
+lemma finite_pderivs_lang:
+ shows "finite (pderivs_lang A r)"
+by (metis finite_pderivs_lang_UNIV pderivs_lang_subset rev_finite_subset subset_UNIV)
+
+
+text{* The following relationship between the alphabetic width of regular expressions
+(called @{text awidth} below) and the number of partial derivatives was proved
+by Antimirov~\cite{Antimirov95} and formalized by Max Haslbeck. *}
+
+fun awidth :: "'a rexp \<Rightarrow> nat" where
+"awidth Zero = 0" |
+"awidth One = 0" |
+"awidth (Atom a) = 1" |
+"awidth (Plus r1 r2) = awidth r1 + awidth r2" |
+"awidth (Times r1 r2) = awidth r1 + awidth r2" |
+"awidth (Star r1) = awidth r1"
+
+lemma card_Timess_pderivs_lang_le:
+ "card (Timess (pderivs_lang A r) s) \<le> card (pderivs_lang A r)"
+by (metis card_image_le finite_pderivs_lang image_eq_UN)
+
+lemma card_pderivs_lang_UNIV1_le_awidth: "card (pderivs_lang UNIV1 r) \<le> awidth r"
+proof (induction r)
+ case (Plus r1 r2)
+ have "card (pderivs_lang UNIV1 (Plus r1 r2)) = card (pderivs_lang UNIV1 r1 \<union> pderivs_lang UNIV1 r2)" by simp
+ also have "\<dots> \<le> card (pderivs_lang UNIV1 r1) + card (pderivs_lang UNIV1 r2)"
+ by(simp add: card_Un_le)
+ also have "\<dots> \<le> awidth (Plus r1 r2)" using Plus.IH by simp
+ finally show ?case .
+next
+ case (Times r1 r2)
+ have "card (pderivs_lang UNIV1 (Times r1 r2)) \<le> card (Timess (pderivs_lang UNIV1 r1) r2 \<union> pderivs_lang UNIV1 r2)"
+ by (simp add: card_mono finite_pderivs_lang pderivs_lang_Times)
+ also have "\<dots> \<le> card (Timess (pderivs_lang UNIV1 r1) r2) + card (pderivs_lang UNIV1 r2)"
+ by (simp add: card_Un_le)
+ also have "\<dots> \<le> card (pderivs_lang UNIV1 r1) + card (pderivs_lang UNIV1 r2)"
+ by (simp add: card_Timess_pderivs_lang_le)
+ also have "\<dots> \<le> awidth (Times r1 r2)" using Times.IH by simp
+ finally show ?case .
+next
+ case (Star r)
+ have "card (pderivs_lang UNIV1 (Star r)) \<le> card (Timess (pderivs_lang UNIV1 r) (Star r))"
+ by (simp add: card_mono finite_pderivs_lang pderivs_lang_Star)
+ also have "\<dots> \<le> card (pderivs_lang UNIV1 r)" by (rule card_Timess_pderivs_lang_le)
+ also have "\<dots> \<le> awidth (Star r)" by (simp add: Star.IH)
+ finally show ?case .
+qed (auto)
+
+text{* Antimirov's Theorem 3.4: *}
+theorem card_pderivs_lang_UNIV_le_awidth: "card (pderivs_lang UNIV r) \<le> awidth r + 1"
+proof -
+ have "card (insert r (pderivs_lang UNIV1 r)) \<le> Suc (card (pderivs_lang UNIV1 r))"
+ by(auto simp: card_insert_if[OF finite_pderivs_lang_UNIV1])
+ also have "\<dots> \<le> Suc (awidth r)" by(simp add: card_pderivs_lang_UNIV1_le_awidth)
+ finally show ?thesis by(simp add: pderivs_lang_UNIV)
+qed
+
+text{* Antimirov's Corollary 3.5: *}
+corollary card_pderivs_lang_le_awidth: "card (pderivs_lang A r) \<le> awidth r + 1"
+by(rule order_trans[OF
+ card_mono[OF finite_pderivs_lang_UNIV pderivs_lang_subset[OF subset_UNIV]]
+ card_pderivs_lang_UNIV_le_awidth])
+
+end
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/Lexer.thy Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,493 @@
+(* Title: POSIX Lexing with Derivatives of Regular Expressions
+ Authors: Fahad Ausaf <fahad.ausaf at icloud.com>, 2016
+ Roy Dyckhoff <roy.dyckhoff at st-andrews.ac.uk>, 2016
+ Christian Urban <christian.urban at kcl.ac.uk>, 2016
+ Maintainer: Christian Urban <christian.urban at kcl.ac.uk>
+*)
+
+theory Lexer
+ imports Derivatives
+begin
+
+section {* Values *}
+
+datatype 'a val =
+ Void
+| Atm 'a
+| Seq "'a val" "'a val"
+| Right "'a val"
+| Left "'a val"
+| Stars "('a val) list"
+
+
+section {* The string behind a value *}
+
+fun
+ flat :: "'a val \<Rightarrow> 'a list"
+where
+ "flat (Void) = []"
+| "flat (Atm c) = [c]"
+| "flat (Left v) = flat v"
+| "flat (Right v) = flat v"
+| "flat (Seq v1 v2) = (flat v1) @ (flat v2)"
+| "flat (Stars []) = []"
+| "flat (Stars (v#vs)) = (flat v) @ (flat (Stars vs))"
+
+lemma flat_Stars [simp]:
+ "flat (Stars vs) = concat (map flat vs)"
+by (induct vs) (auto)
+
+section {* Relation between values and regular expressions *}
+
+inductive
+ Prf :: "'a val \<Rightarrow> 'a rexp \<Rightarrow> bool" ("\<turnstile> _ : _" [100, 100] 100)
+where
+ "\<lbrakk>\<turnstile> v1 : r1; \<turnstile> v2 : r2\<rbrakk> \<Longrightarrow> \<turnstile> Seq v1 v2 : Times r1 r2"
+| "\<turnstile> v1 : r1 \<Longrightarrow> \<turnstile> Left v1 : Plus r1 r2"
+| "\<turnstile> v2 : r2 \<Longrightarrow> \<turnstile> Right v2 : Plus r1 r2"
+| "\<turnstile> Void : One"
+| "\<turnstile> Atm c : Atom c"
+| "\<turnstile> Stars [] : Star r"
+| "\<lbrakk>\<turnstile> v : r; \<turnstile> Stars vs : Star r\<rbrakk> \<Longrightarrow> \<turnstile> Stars (v # vs) : Star r"
+
+inductive_cases Prf_elims:
+ "\<turnstile> v : Zero"
+ "\<turnstile> v : Times r1 r2"
+ "\<turnstile> v : Plus r1 r2"
+ "\<turnstile> v : One"
+ "\<turnstile> v : Atom c"
+(* "\<turnstile> vs : Star r"*)
+
+lemma Prf_flat_lang:
+ assumes "\<turnstile> v : r" shows "flat v \<in> lang r"
+using assms
+by(induct v r rule: Prf.induct) (auto)
+
+lemma Prf_Stars:
+ assumes "\<forall>v \<in> set vs. \<turnstile> v : r"
+ shows "\<turnstile> Stars vs : Star r"
+using assms
+by(induct vs) (auto intro: Prf.intros)
+
+lemma Star_string:
+ assumes "s \<in> star A"
+ shows "\<exists>ss. concat ss = s \<and> (\<forall>s \<in> set ss. s \<in> A)"
+using assms
+by (metis in_star_iff_concat set_mp)
+
+lemma Star_val:
+ assumes "\<forall>s\<in>set ss. \<exists>v. s = flat v \<and> \<turnstile> v : r"
+ shows "\<exists>vs. concat (map flat vs) = concat ss \<and> (\<forall>v\<in>set vs. \<turnstile> v : r)"
+using assms
+apply(induct ss)
+apply(auto)
+apply (metis empty_iff list.set(1))
+by (metis concat.simps(2) list.simps(9) set_ConsD)
+
+lemma L_flat_Prf1:
+ assumes "\<turnstile> v : r" shows "flat v \<in> lang r"
+using assms
+by (induct)(auto)
+
+lemma L_flat_Prf2:
+ assumes "s \<in> lang r" shows "\<exists>v. \<turnstile> v : r \<and> flat v = s"
+using assms
+apply(induct r arbitrary: s)
+apply(auto intro: Prf.intros)
+using Prf.intros(2) flat.simps(3) apply blast
+using Prf.intros(3) flat.simps(4) apply blast
+apply (metis Prf.intros(1) concE flat.simps(5))
+apply(subgoal_tac "\<exists>vs::('a val) list. concat (map flat vs) = s \<and> (\<forall>v \<in> set vs. \<turnstile> v : r)")
+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)
+done
+
+lemma L_flat_Prf:
+ "lang r = {flat v | v. \<turnstile> v : r}"
+using L_flat_Prf1 L_flat_Prf2 by blast
+
+
+section {* Sulzmann and Lu functions *}
+
+fun
+ mkeps :: "'a rexp \<Rightarrow> 'a val"
+where
+ "mkeps(One) = Void"
+| "mkeps(Times r1 r2) = Seq (mkeps r1) (mkeps r2)"
+| "mkeps(Plus r1 r2) = (if nullable(r1) then Left (mkeps r1) else Right (mkeps r2))"
+| "mkeps(Star r) = Stars []"
+
+fun injval :: "'a rexp \<Rightarrow> 'a \<Rightarrow> 'a val \<Rightarrow> 'a val"
+where
+ "injval (Atom d) c Void = Atm d"
+| "injval (Plus r1 r2) c (Left v1) = Left(injval r1 c v1)"
+| "injval (Plus r1 r2) c (Right v2) = Right(injval r2 c v2)"
+| "injval (Times r1 r2) c (Seq v1 v2) = Seq (injval r1 c v1) v2"
+| "injval (Times r1 r2) c (Left (Seq v1 v2)) = Seq (injval r1 c v1) v2"
+| "injval (Times 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)"
+
+
+section {* Mkeps, injval *}
+
+lemma mkeps_nullable:
+ assumes "nullable r"
+ shows "\<turnstile> mkeps r : r"
+using assms
+by (induct r)
+ (auto intro: Prf.intros)
+
+lemma mkeps_flat:
+ assumes "nullable r"
+ shows "flat (mkeps r) = []"
+using assms
+by (induct r) (auto)
+
+
+lemma Prf_injval:
+ assumes "\<turnstile> v : deriv c r"
+ shows "\<turnstile> (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)
+(* Star *)
+apply(rotate_tac 2)
+apply(erule Prf.cases)
+apply(simp_all)[7]
+apply(auto)
+apply (metis Prf.intros(6) Prf.intros(7))
+by (metis Prf.intros(7))
+
+lemma Prf_injval_flat:
+ assumes "\<turnstile> v : deriv c r"
+ shows "flat (injval r c v) = c # (flat v)"
+using assms
+apply(induct r arbitrary: v c)
+apply(auto elim!: Prf_elims split: if_splits)
+apply(metis mkeps_flat)
+apply(rotate_tac 2)
+apply(erule Prf.cases)
+apply(simp_all)[7]
+done
+
+(* HERE *)
+
+section {* Our Alternative Posix definition *}
+
+inductive
+ Posix :: "'a list \<Rightarrow> 'a rexp \<Rightarrow> 'a val \<Rightarrow> bool" ("_ \<in> _ \<rightarrow> _" [100, 100, 100] 100)
+where
+ Posix_One: "[] \<in> One \<rightarrow> Void"
+| Posix_Atom: "[c] \<in> (Atom c) \<rightarrow> (Atm c)"
+| Posix_Plus1: "s \<in> r1 \<rightarrow> v \<Longrightarrow> s \<in> (Plus r1 r2) \<rightarrow> (Left v)"
+| Posix_Plus2: "\<lbrakk>s \<in> r2 \<rightarrow> v; s \<notin> lang r1\<rbrakk> \<Longrightarrow> s \<in> (Plus r1 r2) \<rightarrow> (Right v)"
+| Posix_Times: "\<lbrakk>s1 \<in> r1 \<rightarrow> v1; s2 \<in> r2 \<rightarrow> v2;
+ \<not>(\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (s1 @ s\<^sub>3) \<in> lang r1 \<and> s\<^sub>4 \<in> lang r2)\<rbrakk> \<Longrightarrow>
+ (s1 @ s2) \<in> (Times r1 r2) \<rightarrow> (Seq v1 v2)"
+| Posix_Star1: "\<lbrakk>s1 \<in> r \<rightarrow> v; s2 \<in> Star r \<rightarrow> Stars vs; flat v \<noteq> [];
+ \<not>(\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (s1 @ s\<^sub>3) \<in> lang r \<and> s\<^sub>4 \<in> lang (Star r))\<rbrakk>
+ \<Longrightarrow> (s1 @ s2) \<in> Star r \<rightarrow> Stars (v # vs)"
+| Posix_Star2: "[] \<in> Star r \<rightarrow> Stars []"
+
+inductive_cases Posix_elims:
+ "s \<in> Zero \<rightarrow> v"
+ "s \<in> One \<rightarrow> v"
+ "s \<in> Atom c \<rightarrow> v"
+ "s \<in> Plus r1 r2 \<rightarrow> v"
+ "s \<in> Times r1 r2 \<rightarrow> v"
+ "s \<in> Star r \<rightarrow> v"
+
+lemma Posix1:
+ assumes "s \<in> r \<rightarrow> v"
+ shows "s \<in> lang r" "flat v = s"
+using assms
+by (induct s r v rule: Posix.induct) (auto)
+
+
+lemma Posix1a:
+ assumes "s \<in> r \<rightarrow> v"
+ shows "\<turnstile> v : r"
+using assms
+by (induct s r v rule: Posix.induct)(auto intro: Prf.intros)
+
+
+lemma Posix_mkeps:
+ assumes "nullable r"
+ shows "[] \<in> r \<rightarrow> mkeps r"
+using assms
+apply(induct r)
+apply(auto intro: Posix.intros simp add: nullable_iff)
+apply(subst append.simps(1)[symmetric])
+apply(rule Posix.intros)
+apply(auto)
+done
+
+
+lemma Posix_determ:
+ assumes "s \<in> r \<rightarrow> v1" "s \<in> r \<rightarrow> v2"
+ shows "v1 = v2"
+using assms
+proof (induct s r v1 arbitrary: v2 rule: Posix.induct)
+ case (Posix_One v2)
+ have "[] \<in> One \<rightarrow> v2" by fact
+ then show "Void = v2" by cases auto
+next
+ case (Posix_Atom c v2)
+ have "[c] \<in> Atom c \<rightarrow> v2" by fact
+ then show "Atm c = v2" by cases auto
+next
+ case (Posix_Plus1 s r1 v r2 v2)
+ have "s \<in> Plus r1 r2 \<rightarrow> v2" by fact
+ moreover
+ have "s \<in> r1 \<rightarrow> v" by fact
+ then have "s \<in> lang r1" by (simp add: Posix1)
+ ultimately obtain v' where eq: "v2 = Left v'" "s \<in> r1 \<rightarrow> v'" by cases auto
+ moreover
+ have IH: "\<And>v2. s \<in> r1 \<rightarrow> v2 \<Longrightarrow> v = v2" by fact
+ ultimately have "v = v'" by simp
+ then show "Left v = v2" using eq by simp
+next
+ case (Posix_Plus2 s r2 v r1 v2)
+ have "s \<in> Plus r1 r2 \<rightarrow> v2" by fact
+ moreover
+ have "s \<notin> lang r1" by fact
+ ultimately obtain v' where eq: "v2 = Right v'" "s \<in> r2 \<rightarrow> v'"
+ by cases (auto simp add: Posix1)
+ moreover
+ have IH: "\<And>v2. s \<in> r2 \<rightarrow> v2 \<Longrightarrow> v = v2" by fact
+ ultimately have "v = v'" by simp
+ then show "Right v = v2" using eq by simp
+next
+ case (Posix_Times s1 r1 v1 s2 r2 v2 v')
+ have "(s1 @ s2) \<in> Times r1 r2 \<rightarrow> v'"
+ "s1 \<in> r1 \<rightarrow> v1" "s2 \<in> r2 \<rightarrow> v2"
+ "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang r1 \<and> s\<^sub>4 \<in> lang r2)" by fact+
+ then obtain v1' v2' where "v' = Seq v1' v2'" "s1 \<in> r1 \<rightarrow> v1'" "s2 \<in> r2 \<rightarrow> v2'"
+ apply(cases) apply (auto simp add: append_eq_append_conv2)
+ using Posix1(1) by fastforce+
+ moreover
+ have IHs: "\<And>v1'. s1 \<in> r1 \<rightarrow> v1' \<Longrightarrow> v1 = v1'"
+ "\<And>v2'. s2 \<in> r2 \<rightarrow> v2' \<Longrightarrow> v2 = v2'" by fact+
+ ultimately show "Seq v1 v2 = v'" by simp
+next
+ case (Posix_Star1 s1 r v s2 vs v2)
+ have "(s1 @ s2) \<in> Star r \<rightarrow> v2"
+ "s1 \<in> r \<rightarrow> v" "s2 \<in> Star r \<rightarrow> Stars vs" "flat v \<noteq> []"
+ "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang r \<and> s\<^sub>4 \<in> lang (Star r))" by fact+
+ then obtain v' vs' where "v2 = Stars (v' # vs')" "s1 \<in> r \<rightarrow> v'" "s2 \<in> (Star r) \<rightarrow> (Stars vs')"
+ apply(cases) apply (auto simp add: append_eq_append_conv2)
+ using Posix1(1) apply fastforce
+ apply (metis Posix1(1) Posix_Star1.hyps(6) append_Nil append_Nil2)
+ using Posix1(2) by blast
+ moreover
+ have IHs: "\<And>v2. s1 \<in> r \<rightarrow> v2 \<Longrightarrow> v = v2"
+ "\<And>v2. s2 \<in> Star r \<rightarrow> v2 \<Longrightarrow> Stars vs = v2" by fact+
+ ultimately show "Stars (v # vs) = v2" by auto
+next
+ case (Posix_Star2 r v2)
+ have "[] \<in> Star r \<rightarrow> v2" by fact
+ then show "Stars [] = v2" by cases (auto simp add: Posix1)
+qed
+
+
+lemma Posix_injval:
+ assumes "s \<in> (deriv c r) \<rightarrow> v"
+ shows "(c # s) \<in> r \<rightarrow> (injval r c v)"
+using assms
+proof(induct r arbitrary: s v rule: rexp.induct)
+ case Zero
+ have "s \<in> deriv c Zero \<rightarrow> v" by fact
+ then have "s \<in> Zero \<rightarrow> v" by simp
+ then have "False" by cases
+ then show "(c # s) \<in> Zero \<rightarrow> (injval Zero c v)" by simp
+next
+ case One
+ have "s \<in> deriv c One \<rightarrow> v" by fact
+ then have "s \<in> Zero \<rightarrow> v" by simp
+ then have "False" by cases
+ then show "(c # s) \<in> One \<rightarrow> (injval One c v)" by simp
+next
+ case (Atom d)
+ consider (eq) "c = d" | (ineq) "c \<noteq> d" by blast
+ then show "(c # s) \<in> (Atom d) \<rightarrow> (injval (Atom d) c v)"
+ proof (cases)
+ case eq
+ have "s \<in> deriv c (Atom d) \<rightarrow> v" by fact
+ then have "s \<in> One \<rightarrow> v" using eq by simp
+ then have eqs: "s = [] \<and> v = Void" by cases simp
+ show "(c # s) \<in> Atom d \<rightarrow> injval (Atom d) c v" using eq eqs
+ by (auto intro: Posix.intros)
+ next
+ case ineq
+ have "s \<in> deriv c (Atom d) \<rightarrow> v" by fact
+ then have "s \<in> Zero \<rightarrow> v" using ineq by simp
+ then have "False" by cases
+ then show "(c # s) \<in> Atom d \<rightarrow> injval (Atom d) c v" by simp
+ qed
+next
+ case (Plus r1 r2)
+ have IH1: "\<And>s v. s \<in> deriv c r1 \<rightarrow> v \<Longrightarrow> (c # s) \<in> r1 \<rightarrow> injval r1 c v" by fact
+ have IH2: "\<And>s v. s \<in> deriv c r2 \<rightarrow> v \<Longrightarrow> (c # s) \<in> r2 \<rightarrow> injval r2 c v" by fact
+ have "s \<in> deriv c (Plus r1 r2) \<rightarrow> v" by fact
+ then have "s \<in> Plus (deriv c r1) (deriv c r2) \<rightarrow> v" by simp
+ then consider (left) v' where "v = Left v'" "s \<in> deriv c r1 \<rightarrow> v'"
+ | (right) v' where "v = Right v'" "s \<notin> lang (deriv c r1)" "s \<in> deriv c r2 \<rightarrow> v'"
+ by cases auto
+ then show "(c # s) \<in> Plus r1 r2 \<rightarrow> injval (Plus r1 r2) c v"
+ proof (cases)
+ case left
+ have "s \<in> deriv c r1 \<rightarrow> v'" by fact
+ then have "(c # s) \<in> r1 \<rightarrow> injval r1 c v'" using IH1 by simp
+ then have "(c # s) \<in> Plus r1 r2 \<rightarrow> injval (Plus r1 r2) c (Left v')" by (auto intro: Posix.intros)
+ then show "(c # s) \<in> Plus r1 r2 \<rightarrow> injval (Plus r1 r2) c v" using left by simp
+ next
+ case right
+ have "s \<notin> lang (deriv c r1)" by fact
+ then have "c # s \<notin> lang r1" by (simp add: lang_deriv Deriv_def)
+ moreover
+ have "s \<in> deriv c r2 \<rightarrow> v'" by fact
+ then have "(c # s) \<in> r2 \<rightarrow> injval r2 c v'" using IH2 by simp
+ ultimately have "(c # s) \<in> Plus r1 r2 \<rightarrow> injval (Plus r1 r2) c (Right v')"
+ by (auto intro: Posix.intros)
+ then show "(c # s) \<in> Plus r1 r2 \<rightarrow> injval (Plus r1 r2) c v" using right by simp
+ qed
+next
+ case (Times r1 r2)
+ have IH1: "\<And>s v. s \<in> deriv c r1 \<rightarrow> v \<Longrightarrow> (c # s) \<in> r1 \<rightarrow> injval r1 c v" by fact
+ have IH2: "\<And>s v. s \<in> deriv c r2 \<rightarrow> v \<Longrightarrow> (c # s) \<in> r2 \<rightarrow> injval r2 c v" by fact
+ have "s \<in> deriv c (Times r1 r2) \<rightarrow> v" by fact
+ then consider
+ (left_nullable) v1 v2 s1 s2 where
+ "v = Left (Seq v1 v2)" "s = s1 @ s2"
+ "s1 \<in> deriv c r1 \<rightarrow> v1" "s2 \<in> r2 \<rightarrow> v2" "nullable r1"
+ "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang (deriv c r1) \<and> s\<^sub>4 \<in> lang r2)"
+ | (right_nullable) v1 s1 s2 where
+ "v = Right v1" "s = s1 @ s2"
+ "s \<in> deriv c r2 \<rightarrow> v1" "nullable r1" "s1 @ s2 \<notin> lang (Times (deriv c r1) r2)"
+ | (not_nullable) v1 v2 s1 s2 where
+ "v = Seq v1 v2" "s = s1 @ s2"
+ "s1 \<in> deriv c r1 \<rightarrow> v1" "s2 \<in> r2 \<rightarrow> v2" "\<not>nullable r1"
+ "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang (deriv c r1) \<and> s\<^sub>4 \<in> lang r2)"
+ by (force split: if_splits elim!: Posix_elims simp add: lang_deriv Deriv_def)
+ then show "(c # s) \<in> Times r1 r2 \<rightarrow> injval (Times r1 r2) c v"
+ proof (cases)
+ case left_nullable
+ have "s1 \<in> deriv c r1 \<rightarrow> v1" by fact
+ then have "(c # s1) \<in> r1 \<rightarrow> injval r1 c v1" using IH1 by simp
+ moreover
+ have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang (deriv c r1) \<and> s\<^sub>4 \<in> lang r2)" by fact
+ then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (c # s1) @ s\<^sub>3 \<in> lang r1 \<and> s\<^sub>4 \<in> lang r2)"
+ by (simp add: lang_deriv Deriv_def)
+ ultimately have "((c # s1) @ s2) \<in> Times r1 r2 \<rightarrow> Seq (injval r1 c v1) v2" using left_nullable by (rule_tac Posix.intros)
+ then show "(c # s) \<in> Times r1 r2 \<rightarrow> injval (Times r1 r2) c v" using left_nullable by simp
+ next
+ case right_nullable
+ have "nullable r1" by fact
+ then have "[] \<in> r1 \<rightarrow> (mkeps r1)" by (rule Posix_mkeps)
+ moreover
+ have "s \<in> deriv c r2 \<rightarrow> v1" by fact
+ then have "(c # s) \<in> r2 \<rightarrow> (injval r2 c v1)" using IH2 by simp
+ moreover
+ have "s1 @ s2 \<notin> lang (Times (deriv c r1) r2)" by fact
+ then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = c # s \<and> [] @ s\<^sub>3 \<in> lang r1 \<and> s\<^sub>4 \<in> lang r2)"
+ using right_nullable
+ apply (auto simp add: lang_deriv Deriv_def append_eq_Cons_conv)
+ by (metis concI mem_Collect_eq)
+ ultimately have "([] @ (c # s)) \<in> Times r1 r2 \<rightarrow> Seq (mkeps r1) (injval r2 c v1)"
+ by(rule Posix.intros)
+ then show "(c # s) \<in> Times r1 r2 \<rightarrow> injval (Times r1 r2) c v" using right_nullable by simp
+ next
+ case not_nullable
+ have "s1 \<in> deriv c r1 \<rightarrow> v1" by fact
+ then have "(c # s1) \<in> r1 \<rightarrow> injval r1 c v1" using IH1 by simp
+ moreover
+ have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang (deriv c r1) \<and> s\<^sub>4 \<in> lang r2)" by fact
+ then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (c # s1) @ s\<^sub>3 \<in> lang r1 \<and> s\<^sub>4 \<in> lang r2)" by (simp add: lang_deriv Deriv_def)
+ ultimately have "((c # s1) @ s2) \<in> Times r1 r2 \<rightarrow> Seq (injval r1 c v1) v2" using not_nullable
+ by (rule_tac Posix.intros) (simp_all)
+ then show "(c # s) \<in> Times r1 r2 \<rightarrow> injval (Times r1 r2) c v" using not_nullable by simp
+ qed
+next
+ case (Star r)
+ have IH: "\<And>s v. s \<in> deriv c r \<rightarrow> v \<Longrightarrow> (c # s) \<in> r \<rightarrow> injval r c v" by fact
+ have "s \<in> deriv c (Star r) \<rightarrow> v" by fact
+ then consider
+ (cons) v1 vs s1 s2 where
+ "v = Seq v1 (Stars vs)" "s = s1 @ s2"
+ "s1 \<in> deriv c r \<rightarrow> v1" "s2 \<in> (Star r) \<rightarrow> (Stars vs)"
+ "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang (deriv c r) \<and> s\<^sub>4 \<in> lang (Star r))"
+ apply(auto elim!: Posix_elims(1-5) simp add: lang_deriv Deriv_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) \<in> Star r \<rightarrow> injval (Star r) c v"
+ proof (cases)
+ case cons
+ have "s1 \<in> deriv c r \<rightarrow> v1" by fact
+ then have "(c # s1) \<in> r \<rightarrow> injval r c v1" using IH by simp
+ moreover
+ have "s2 \<in> Star r \<rightarrow> Stars vs" by fact
+ moreover
+ have "(c # s1) \<in> r \<rightarrow> 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) \<noteq> []" by simp
+ moreover
+ have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang (deriv c r) \<and> s\<^sub>4 \<in> lang (Star r))" by fact
+ then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (c # s1) @ s\<^sub>3 \<in> lang r \<and> s\<^sub>4 \<in> lang (Star r))"
+ by (simp add: lang_deriv Deriv_def)
+ ultimately
+ have "((c # s1) @ s2) \<in> Star r \<rightarrow> Stars (injval r c v1 # vs)" by (rule Posix.intros)
+ then show "(c # s) \<in> Star r \<rightarrow> injval (Star r) c v" using cons by(simp)
+ qed
+qed
+
+
+section {* The Lexer by Sulzmann and Lu *}
+
+fun
+ lexer :: "'a rexp \<Rightarrow> 'a list \<Rightarrow> ('a val) option"
+where
+ "lexer r [] = (if nullable r then Some(mkeps r) else None)"
+| "lexer r (c#s) = (case (lexer (deriv c r) s) of
+ None \<Rightarrow> None
+ | Some(v) \<Rightarrow> Some(injval r c v))"
+
+
+lemma lexer_correct_None:
+ shows "s \<notin> lang r \<longleftrightarrow> lexer r s = None"
+using assms
+apply(induct s arbitrary: r)
+apply(simp add: nullable_iff)
+apply(drule_tac x="deriv a r" in meta_spec)
+apply(auto simp add: lang_deriv Deriv_def)
+done
+
+lemma lexer_correct_Some:
+ shows "s \<in> lang r \<longleftrightarrow> (\<exists>v. lexer r s = Some(v) \<and> s \<in> r \<rightarrow> v)"
+using assms
+apply(induct s arbitrary: r)
+apply(auto simp add: Posix_mkeps nullable_iff)[1]
+apply(drule_tac x="deriv a r" in meta_spec)
+apply(simp add: lang_deriv Deriv_def)
+apply(rule iffI)
+apply(auto intro: Posix_injval simp add: Posix1(1))
+done
+
+lemma lexer_correctness:
+ shows "(lexer r s = Some v) \<longleftrightarrow> s \<in> r \<rightarrow> v"
+ and "(lexer r s = None) \<longleftrightarrow> \<not>(\<exists>v. s \<in> r \<rightarrow> v)"
+apply(auto)
+using lexer_correct_None lexer_correct_Some apply fastforce
+using Posix1(1) Posix_determ lexer_correct_Some apply blast
+using Posix1(1) lexer_correct_None apply blast
+using lexer_correct_None lexer_correct_Some by blast
+
+
+end
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/README Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,53 @@
+Title:
+======
+POSIX Lexing with Derivatives of Regular Expressions
+
+
+Authors:
+========
+Fahad Ausaf <fahad.ausaf at icloud.com>, 2016
+Roy Dyckhoff <roy.dyckhoff at st-andrews.ac.uk>, 2016
+Christian Urban <christian.urban at kcl.ac.uk>, 2016
+
+
+Abstract:
+=========
+
+Brzozowski introduced the notion of derivatives for regular
+expressions. They can be used for a very simple regular expression
+matching algorithm. Sulzmann and Lu cleverly extended this algorithm
+in order to deal with POSIX matching, which is the underlying
+disambiguation strategy for regular expressions needed in
+lexers. Sulzmann and Lu have made available on-line what they call a
+``rigorous proof'' of the correctness of their algorithm w.r.t. their
+specification; regrettably, it appears to us to have unfillable
+gaps. In the first part of this paper we give our inductive definition
+of what a POSIX value is and show (i) that such a value is unique (for
+given regular expression and string being matched) and (ii) that
+Sulzmann and Lu's algorithm always generates such a value (provided
+that the regular expression matches the string). We also prove the
+correctness of an optimised version of the POSIX matching
+algorithm. Our definitions and proof are much simpler than those by
+Sulzmann and Lu and can be easily formalised in Isabelle/HOL. In the
+second part we analyse the correctness argument by Sulzmann and Lu and
+explain why the gaps in this argument cannot be filled easily.
+
+
+New Theories:
+=============
+
+ Lexer.thy
+ Simplifying.thy
+
+The repository can be checked using Isabelle 2016.
+
+ isabelle build -c -v -d . Posix-Lexing
+
+
+
+
+
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/ROOT Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,23 @@
+chapter AFP
+
+(* Session name, add to AFP group, list base session: *)
+session "Posix-Lexing" (AFP) = HOL +
+
+(* Timeout (in sec) in case of non-termination problems *)
+ options [timeout = 600]
+
+(* The top-level theories of the submission: *)
+ theories [document = false]
+ "Regular_Set"
+ "Regular_Exp"
+ "Derivatives"
+
+ theories
+ "Lexer"
+ "Simplifying"
+
+(* Dependencies on document source files: *)
+ document_files
+ "root.bib"
+ "root.tex"
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/Regular_Exp.thy Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,160 @@
+(* Author: Tobias Nipkow *)
+
+section "Regular expressions"
+
+theory Regular_Exp
+imports Regular_Set
+begin
+
+datatype (atoms: 'a) rexp =
+ is_Zero: Zero |
+ is_One: One |
+ Atom 'a |
+ Plus "('a rexp)" "('a rexp)" |
+ Times "('a rexp)" "('a rexp)" |
+ Star "('a rexp)"
+
+primrec lang :: "'a rexp => 'a lang" where
+"lang Zero = {}" |
+"lang One = {[]}" |
+"lang (Atom a) = {[a]}" |
+"lang (Plus r s) = (lang r) Un (lang s)" |
+"lang (Times r s) = conc (lang r) (lang s)" |
+"lang (Star r) = star(lang r)"
+
+primrec nullable :: "'a rexp \<Rightarrow> bool" where
+"nullable Zero = False" |
+"nullable One = True" |
+"nullable (Atom c) = False" |
+"nullable (Plus r1 r2) = (nullable r1 \<or> nullable r2)" |
+"nullable (Times r1 r2) = (nullable r1 \<and> nullable r2)" |
+"nullable (Star r) = True"
+
+lemma nullable_iff: "nullable r \<longleftrightarrow> [] \<in> lang r"
+by (induct r) (auto simp add: conc_def split: if_splits)
+
+text{* Composition on rhs usually complicates matters: *}
+lemma map_map_rexp:
+ "map_rexp f (map_rexp g r) = map_rexp (\<lambda>r. f (g r)) r"
+ unfolding rexp.map_comp o_def ..
+
+lemma map_rexp_ident[simp]: "map_rexp (\<lambda>x. x) = (\<lambda>r. r)"
+ unfolding id_def[symmetric] fun_eq_iff rexp.map_id id_apply by (intro allI refl)
+
+lemma atoms_lang: "w : lang r \<Longrightarrow> set w \<subseteq> atoms r"
+proof(induction r arbitrary: w)
+ case Times thus ?case by fastforce
+next
+ case Star thus ?case by (fastforce simp add: star_conv_concat)
+qed auto
+
+lemma lang_eq_ext: "(lang r = lang s) =
+ (\<forall>w \<in> lists(atoms r \<union> atoms s). w \<in> lang r \<longleftrightarrow> w \<in> lang s)"
+ by (auto simp: atoms_lang[unfolded subset_iff])
+
+lemma lang_eq_ext_Nil_fold_Deriv:
+ fixes r s
+ defines "\<BB> \<equiv> {(fold Deriv w (lang r), fold Deriv w (lang s))| w. w\<in>lists (atoms r \<union> atoms s)}"
+ shows "lang r = lang s \<longleftrightarrow> (\<forall>(K, L) \<in> \<BB>. [] \<in> K \<longleftrightarrow> [] \<in> L)"
+ unfolding lang_eq_ext \<BB>_def by (subst (1 2) in_fold_Deriv[of "[]", simplified, symmetric]) auto
+
+
+subsection {* Term ordering *}
+
+instantiation rexp :: (order) "{order}"
+begin
+
+fun le_rexp :: "('a::order) rexp \<Rightarrow> ('a::order) rexp \<Rightarrow> bool"
+where
+ "le_rexp Zero _ = True"
+| "le_rexp _ Zero = False"
+| "le_rexp One _ = True"
+| "le_rexp _ One = False"
+| "le_rexp (Atom a) (Atom b) = (a <= b)"
+| "le_rexp (Atom _) _ = True"
+| "le_rexp _ (Atom _) = False"
+| "le_rexp (Star r) (Star s) = le_rexp r s"
+| "le_rexp (Star _) _ = True"
+| "le_rexp _ (Star _) = False"
+| "le_rexp (Plus r r') (Plus s s') =
+ (if r = s then le_rexp r' s' else le_rexp r s)"
+| "le_rexp (Plus _ _) _ = True"
+| "le_rexp _ (Plus _ _) = False"
+| "le_rexp (Times r r') (Times s s') =
+ (if r = s then le_rexp r' s' else le_rexp r s)"
+
+(* The class instance stuff is by Dmitriy Traytel *)
+
+definition less_eq_rexp where "r \<le> s \<equiv> le_rexp r s"
+definition less_rexp where "r < s \<equiv> le_rexp r s \<and> r \<noteq> s"
+
+lemma le_rexp_Zero: "le_rexp r Zero \<Longrightarrow> r = Zero"
+by (induction r) auto
+
+lemma le_rexp_refl: "le_rexp r r"
+by (induction r) auto
+
+lemma le_rexp_antisym: "\<lbrakk>le_rexp r s; le_rexp s r\<rbrakk> \<Longrightarrow> r = s"
+by (induction r s rule: le_rexp.induct) (auto dest: le_rexp_Zero)
+
+lemma le_rexp_trans: "\<lbrakk>le_rexp r s; le_rexp s t\<rbrakk> \<Longrightarrow> le_rexp r t"
+proof (induction r s arbitrary: t rule: le_rexp.induct)
+ fix v t assume "le_rexp (Atom v) t" thus "le_rexp One t" by (cases t) auto
+next
+ fix s1 s2 t assume "le_rexp (Plus s1 s2) t" thus "le_rexp One t" by (cases t) auto
+next
+ fix s1 s2 t assume "le_rexp (Times s1 s2) t" thus "le_rexp One t" by (cases t) auto
+next
+ fix s t assume "le_rexp (Star s) t" thus "le_rexp One t" by (cases t) auto
+next
+ fix v u t assume "le_rexp (Atom v) (Atom u)" "le_rexp (Atom u) t"
+ thus "le_rexp (Atom v) t" by (cases t) auto
+next
+ fix v s1 s2 t assume "le_rexp (Plus s1 s2) t" thus "le_rexp (Atom v) t" by (cases t) auto
+next
+ fix v s1 s2 t assume "le_rexp (Times s1 s2) t" thus "le_rexp (Atom v) t" by (cases t) auto
+next
+ fix v s t assume "le_rexp (Star s) t" thus "le_rexp (Atom v) t" by (cases t) auto
+next
+ fix r s t
+ assume IH: "\<And>t. le_rexp r s \<Longrightarrow> le_rexp s t \<Longrightarrow> le_rexp r t"
+ and "le_rexp (Star r) (Star s)" "le_rexp (Star s) t"
+ thus "le_rexp (Star r) t" by (cases t) auto
+next
+ fix r s1 s2 t assume "le_rexp (Plus s1 s2) t" thus "le_rexp (Star r) t" by (cases t) auto
+next
+ fix r s1 s2 t assume "le_rexp (Times s1 s2) t" thus "le_rexp (Star r) t" by (cases t) auto
+next
+ fix r1 r2 s1 s2 t
+ assume "\<And>t. r1 = s1 \<Longrightarrow> le_rexp r2 s2 \<Longrightarrow> le_rexp s2 t \<Longrightarrow> le_rexp r2 t"
+ "\<And>t. r1 \<noteq> s1 \<Longrightarrow> le_rexp r1 s1 \<Longrightarrow> le_rexp s1 t \<Longrightarrow> le_rexp r1 t"
+ "le_rexp (Plus r1 r2) (Plus s1 s2)" "le_rexp (Plus s1 s2) t"
+ thus "le_rexp (Plus r1 r2) t" by (cases t) (auto split: split_if_asm intro: le_rexp_antisym)
+next
+ fix r1 r2 s1 s2 t assume "le_rexp (Times s1 s2) t" thus "le_rexp (Plus r1 r2) t" by (cases t) auto
+next
+ fix r1 r2 s1 s2 t
+ assume "\<And>t. r1 = s1 \<Longrightarrow> le_rexp r2 s2 \<Longrightarrow> le_rexp s2 t \<Longrightarrow> le_rexp r2 t"
+ "\<And>t. r1 \<noteq> s1 \<Longrightarrow> le_rexp r1 s1 \<Longrightarrow> le_rexp s1 t \<Longrightarrow> le_rexp r1 t"
+ "le_rexp (Times r1 r2) (Times s1 s2)" "le_rexp (Times s1 s2) t"
+ thus "le_rexp (Times r1 r2) t" by (cases t) (auto split: split_if_asm intro: le_rexp_antisym)
+qed auto
+
+instance proof
+qed (auto simp add: less_eq_rexp_def less_rexp_def
+ intro: le_rexp_refl le_rexp_antisym le_rexp_trans)
+
+end
+
+instantiation rexp :: (linorder) "{linorder}"
+begin
+
+lemma le_rexp_total: "le_rexp (r :: 'a :: linorder rexp) s \<or> le_rexp s r"
+by (induction r s rule: le_rexp.induct) auto
+
+instance proof
+qed (unfold less_eq_rexp_def less_rexp_def, rule le_rexp_total)
+
+end
+
+end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/Regular_Set.thy Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,481 @@
+(* Author: Tobias Nipkow, Alex Krauss, Christian Urban *)
+
+section "Regular sets"
+
+theory Regular_Set
+imports Main
+begin
+
+type_synonym 'a lang = "'a list set"
+
+definition conc :: "'a lang \<Rightarrow> 'a lang \<Rightarrow> 'a lang" (infixr "@@" 75) where
+"A @@ B = {xs@ys | xs ys. xs:A & ys:B}"
+
+text {* checks the code preprocessor for set comprehensions *}
+export_code conc checking SML
+
+overloading lang_pow == "compow :: nat \<Rightarrow> 'a lang \<Rightarrow> 'a lang"
+begin
+ primrec lang_pow :: "nat \<Rightarrow> 'a lang \<Rightarrow> 'a lang" where
+ "lang_pow 0 A = {[]}" |
+ "lang_pow (Suc n) A = A @@ (lang_pow n A)"
+end
+
+text {* for code generation *}
+
+definition lang_pow :: "nat \<Rightarrow> 'a lang \<Rightarrow> 'a lang" where
+ lang_pow_code_def [code_abbrev]: "lang_pow = compow"
+
+lemma [code]:
+ "lang_pow (Suc n) A = A @@ (lang_pow n A)"
+ "lang_pow 0 A = {[]}"
+ by (simp_all add: lang_pow_code_def)
+
+hide_const (open) lang_pow
+
+definition star :: "'a lang \<Rightarrow> 'a lang" where
+"star A = (\<Union>n. A ^^ n)"
+
+
+subsection{* @{term "op @@"} *}
+
+lemma concI[simp,intro]: "u : A \<Longrightarrow> v : B \<Longrightarrow> u@v : A @@ B"
+by (auto simp add: conc_def)
+
+lemma concE[elim]:
+assumes "w \<in> A @@ B"
+obtains u v where "u \<in> A" "v \<in> B" "w = u@v"
+using assms by (auto simp: conc_def)
+
+lemma conc_mono: "A \<subseteq> C \<Longrightarrow> B \<subseteq> D \<Longrightarrow> A @@ B \<subseteq> C @@ D"
+by (auto simp: conc_def)
+
+lemma conc_empty[simp]: shows "{} @@ A = {}" and "A @@ {} = {}"
+by auto
+
+lemma conc_epsilon[simp]: shows "{[]} @@ A = A" and "A @@ {[]} = A"
+by (simp_all add:conc_def)
+
+lemma conc_assoc: "(A @@ B) @@ C = A @@ (B @@ C)"
+by (auto elim!: concE) (simp only: append_assoc[symmetric] concI)
+
+lemma conc_Un_distrib:
+shows "A @@ (B \<union> C) = A @@ B \<union> A @@ C"
+and "(A \<union> B) @@ C = A @@ C \<union> B @@ C"
+by auto
+
+lemma conc_UNION_distrib:
+shows "A @@ UNION I M = UNION I (%i. A @@ M i)"
+and "UNION I M @@ A = UNION I (%i. M i @@ A)"
+by auto
+
+lemma conc_subset_lists: "A \<subseteq> lists S \<Longrightarrow> B \<subseteq> lists S \<Longrightarrow> A @@ B \<subseteq> lists S"
+by(fastforce simp: conc_def in_lists_conv_set)
+
+lemma Nil_in_conc[simp]: "[] \<in> A @@ B \<longleftrightarrow> [] \<in> A \<and> [] \<in> B"
+by (metis append_is_Nil_conv concE concI)
+
+lemma concI_if_Nil1: "[] \<in> A \<Longrightarrow> xs : B \<Longrightarrow> xs \<in> A @@ B"
+by (metis append_Nil concI)
+
+lemma conc_Diff_if_Nil1: "[] \<in> A \<Longrightarrow> A @@ B = (A - {[]}) @@ B \<union> B"
+by (fastforce elim: concI_if_Nil1)
+
+lemma concI_if_Nil2: "[] \<in> B \<Longrightarrow> xs : A \<Longrightarrow> xs \<in> A @@ B"
+by (metis append_Nil2 concI)
+
+lemma conc_Diff_if_Nil2: "[] \<in> B \<Longrightarrow> A @@ B = A @@ (B - {[]}) \<union> A"
+by (fastforce elim: concI_if_Nil2)
+
+lemma singleton_in_conc:
+ "[x] : A @@ B \<longleftrightarrow> [x] : A \<and> [] : B \<or> [] : A \<and> [x] : B"
+by (fastforce simp: Cons_eq_append_conv append_eq_Cons_conv
+ conc_Diff_if_Nil1 conc_Diff_if_Nil2)
+
+
+subsection{* @{term "A ^^ n"} *}
+
+lemma lang_pow_add: "A ^^ (n + m) = A ^^ n @@ A ^^ m"
+by (induct n) (auto simp: conc_assoc)
+
+lemma lang_pow_empty: "{} ^^ n = (if n = 0 then {[]} else {})"
+by (induct n) auto
+
+lemma lang_pow_empty_Suc[simp]: "({}::'a lang) ^^ Suc n = {}"
+by (simp add: lang_pow_empty)
+
+lemma conc_pow_comm:
+ shows "A @@ (A ^^ n) = (A ^^ n) @@ A"
+by (induct n) (simp_all add: conc_assoc[symmetric])
+
+lemma length_lang_pow_ub:
+ "ALL w : A. length w \<le> k \<Longrightarrow> w : A^^n \<Longrightarrow> length w \<le> k*n"
+by(induct n arbitrary: w) (fastforce simp: conc_def)+
+
+lemma length_lang_pow_lb:
+ "ALL w : A. length w \<ge> k \<Longrightarrow> w : A^^n \<Longrightarrow> length w \<ge> k*n"
+by(induct n arbitrary: w) (fastforce simp: conc_def)+
+
+lemma lang_pow_subset_lists: "A \<subseteq> lists S \<Longrightarrow> A ^^ n \<subseteq> lists S"
+by(induction n)(auto simp: conc_subset_lists[OF assms])
+
+
+subsection{* @{const star} *}
+
+lemma star_subset_lists: "A \<subseteq> lists S \<Longrightarrow> star A \<subseteq> lists S"
+unfolding star_def by(blast dest: lang_pow_subset_lists)
+
+lemma star_if_lang_pow[simp]: "w : A ^^ n \<Longrightarrow> w : star A"
+by (auto simp: star_def)
+
+lemma Nil_in_star[iff]: "[] : star A"
+proof (rule star_if_lang_pow)
+ show "[] : A ^^ 0" by simp
+qed
+
+lemma star_if_lang[simp]: assumes "w : A" shows "w : star A"
+proof (rule star_if_lang_pow)
+ show "w : A ^^ 1" using `w : A` by simp
+qed
+
+lemma append_in_starI[simp]:
+assumes "u : star A" and "v : star A" shows "u@v : star A"
+proof -
+ from `u : star A` obtain m where "u : A ^^ m" by (auto simp: star_def)
+ moreover
+ from `v : star A` obtain n where "v : A ^^ n" by (auto simp: star_def)
+ ultimately have "u@v : A ^^ (m+n)" by (simp add: lang_pow_add)
+ thus ?thesis by simp
+qed
+
+lemma conc_star_star: "star A @@ star A = star A"
+by (auto simp: conc_def)
+
+lemma conc_star_comm:
+ shows "A @@ star A = star A @@ A"
+unfolding star_def conc_pow_comm conc_UNION_distrib
+by simp
+
+lemma star_induct[consumes 1, case_names Nil append, induct set: star]:
+assumes "w : star A"
+ and "P []"
+ and step: "!!u v. u : A \<Longrightarrow> v : star A \<Longrightarrow> P v \<Longrightarrow> P (u@v)"
+shows "P w"
+proof -
+ { fix n have "w : A ^^ n \<Longrightarrow> P w"
+ by (induct n arbitrary: w) (auto intro: `P []` step star_if_lang_pow) }
+ with `w : star A` show "P w" by (auto simp: star_def)
+qed
+
+lemma star_empty[simp]: "star {} = {[]}"
+by (auto elim: star_induct)
+
+lemma star_epsilon[simp]: "star {[]} = {[]}"
+by (auto elim: star_induct)
+
+lemma star_idemp[simp]: "star (star A) = star A"
+by (auto elim: star_induct)
+
+lemma star_unfold_left: "star A = A @@ star A \<union> {[]}" (is "?L = ?R")
+proof
+ show "?L \<subseteq> ?R" by (rule, erule star_induct) auto
+qed auto
+
+lemma concat_in_star: "set ws \<subseteq> A \<Longrightarrow> concat ws : star A"
+by (induct ws) simp_all
+
+lemma in_star_iff_concat:
+ "w : star A = (EX ws. set ws \<subseteq> A & w = concat ws)"
+ (is "_ = (EX ws. ?R w ws)")
+proof
+ assume "w : star A" thus "EX ws. ?R w ws"
+ proof induct
+ case Nil have "?R [] []" by simp
+ thus ?case ..
+ next
+ case (append u v)
+ moreover
+ then obtain ws where "set ws \<subseteq> A \<and> v = concat ws" by blast
+ ultimately have "?R (u@v) (u#ws)" by auto
+ thus ?case ..
+ qed
+next
+ assume "EX us. ?R w us" thus "w : star A"
+ by (auto simp: concat_in_star)
+qed
+
+lemma star_conv_concat: "star A = {concat ws|ws. set ws \<subseteq> A}"
+by (fastforce simp: in_star_iff_concat)
+
+lemma star_insert_eps[simp]: "star (insert [] A) = star(A)"
+proof-
+ { fix us
+ have "set us \<subseteq> insert [] A \<Longrightarrow> EX vs. concat us = concat vs \<and> set vs \<subseteq> A"
+ (is "?P \<Longrightarrow> EX vs. ?Q vs")
+ proof
+ let ?vs = "filter (%u. u \<noteq> []) us"
+ show "?P \<Longrightarrow> ?Q ?vs" by (induct us) auto
+ qed
+ } thus ?thesis by (auto simp: star_conv_concat)
+qed
+
+lemma star_unfold_left_Nil: "star A = (A - {[]}) @@ (star A) \<union> {[]}"
+by (metis insert_Diff_single star_insert_eps star_unfold_left)
+
+lemma star_Diff_Nil_fold: "(A - {[]}) @@ star A = star A - {[]}"
+proof -
+ have "[] \<notin> (A - {[]}) @@ star A" by simp
+ thus ?thesis using star_unfold_left_Nil by blast
+qed
+
+lemma star_decom:
+ assumes a: "x \<in> star A" "x \<noteq> []"
+ shows "\<exists>a b. x = a @ b \<and> a \<noteq> [] \<and> a \<in> A \<and> b \<in> star A"
+using a by (induct rule: star_induct) (blast)+
+
+
+subsection {* Left-Quotients of languages *}
+
+definition Deriv :: "'a \<Rightarrow> 'a lang \<Rightarrow> 'a lang"
+where "Deriv x A = { xs. x#xs \<in> A }"
+
+definition Derivs :: "'a list \<Rightarrow> 'a lang \<Rightarrow> 'a lang"
+where "Derivs xs A = { ys. xs @ ys \<in> A }"
+
+abbreviation
+ Derivss :: "'a list \<Rightarrow> 'a lang set \<Rightarrow> 'a lang"
+where
+ "Derivss s As \<equiv> \<Union> (Derivs s ` As)"
+
+
+lemma Deriv_empty[simp]: "Deriv a {} = {}"
+ and Deriv_epsilon[simp]: "Deriv a {[]} = {}"
+ and Deriv_char[simp]: "Deriv a {[b]} = (if a = b then {[]} else {})"
+ and Deriv_union[simp]: "Deriv a (A \<union> B) = Deriv a A \<union> Deriv a B"
+ and Deriv_inter[simp]: "Deriv a (A \<inter> B) = Deriv a A \<inter> Deriv a B"
+ and Deriv_compl[simp]: "Deriv a (-A) = - Deriv a A"
+ and Deriv_Union[simp]: "Deriv a (Union M) = Union(Deriv a ` M)"
+ and Deriv_UN[simp]: "Deriv a (UN x:I. S x) = (UN x:I. Deriv a (S x))"
+by (auto simp: Deriv_def)
+
+lemma Der_conc [simp]:
+ shows "Deriv c (A @@ B) = (Deriv c A) @@ B \<union> (if [] \<in> A then Deriv c B else {})"
+unfolding Deriv_def conc_def
+by (auto simp add: Cons_eq_append_conv)
+
+lemma Deriv_star [simp]:
+ shows "Deriv c (star A) = (Deriv c A) @@ star A"
+proof -
+ have "Deriv c (star A) = Deriv c ({[]} \<union> A @@ star A)"
+ by (metis star_unfold_left sup.commute)
+ also have "... = Deriv c (A @@ star A)"
+ unfolding Deriv_union by (simp)
+ also have "... = (Deriv c A) @@ (star A) \<union> (if [] \<in> A then Deriv c (star A) else {})"
+ by simp
+ also have "... = (Deriv c A) @@ star A"
+ unfolding conc_def Deriv_def
+ using star_decom by (force simp add: Cons_eq_append_conv)
+ finally show "Deriv c (star A) = (Deriv c A) @@ star A" .
+qed
+
+lemma Deriv_diff[simp]:
+ shows "Deriv c (A - B) = Deriv c A - Deriv c B"
+by(auto simp add: Deriv_def)
+
+lemma Deriv_lists[simp]: "c : S \<Longrightarrow> Deriv c (lists S) = lists S"
+by(auto simp add: Deriv_def)
+
+lemma Derivs_simps [simp]:
+ shows "Derivs [] A = A"
+ and "Derivs (c # s) A = Derivs s (Deriv c A)"
+ and "Derivs (s1 @ s2) A = Derivs s2 (Derivs s1 A)"
+unfolding Derivs_def Deriv_def by auto
+
+lemma in_fold_Deriv: "v \<in> fold Deriv w L \<longleftrightarrow> w @ v \<in> L"
+ by (induct w arbitrary: L) (simp_all add: Deriv_def)
+
+lemma Derivs_alt_def: "Derivs w L = fold Deriv w L"
+ by (induct w arbitrary: L) simp_all
+
+
+subsection {* Shuffle product *}
+
+fun shuffle where
+ "shuffle [] ys = {ys}"
+| "shuffle xs [] = {xs}"
+| "shuffle (x # xs) (y # ys) =
+ {x # w | w . w \<in> shuffle xs (y # ys)} \<union>
+ {y # w | w . w \<in> shuffle (x # xs) ys}"
+
+lemma shuffle_empty2[simp]: "shuffle xs [] = {xs}"
+ by (cases xs) auto
+
+lemma Nil_in_shuffle[simp]: "[] \<in> shuffle xs ys \<longleftrightarrow> xs = [] \<and> ys = []"
+ by (induct xs ys rule: shuffle.induct) auto
+
+definition Shuffle (infixr "\<parallel>" 80) where
+ "Shuffle A B = \<Union>{shuffle xs ys | xs ys. xs \<in> A \<and> ys \<in> B}"
+
+lemma shuffleE:
+ "zs \<in> shuffle xs ys \<Longrightarrow>
+ (zs = xs \<Longrightarrow> ys = [] \<Longrightarrow> P) \<Longrightarrow>
+ (zs = ys \<Longrightarrow> xs = [] \<Longrightarrow> P) \<Longrightarrow>
+ (\<And>x xs' z zs'. xs = x # xs' \<Longrightarrow> zs = z # zs' \<Longrightarrow> x = z \<Longrightarrow> zs' \<in> shuffle xs' ys \<Longrightarrow> P) \<Longrightarrow>
+ (\<And>y ys' z zs'. ys = y # ys' \<Longrightarrow> zs = z # zs' \<Longrightarrow> y = z \<Longrightarrow> zs' \<in> shuffle xs ys' \<Longrightarrow> P) \<Longrightarrow> P"
+ by (induct xs ys rule: shuffle.induct) auto
+
+lemma Cons_in_shuffle_iff:
+ "z # zs \<in> shuffle xs ys \<longleftrightarrow>
+ (xs \<noteq> [] \<and> hd xs = z \<and> zs \<in> shuffle (tl xs) ys \<or>
+ ys \<noteq> [] \<and> hd ys = z \<and> zs \<in> shuffle xs (tl ys))"
+ by (induct xs ys rule: shuffle.induct) auto
+
+lemma Deriv_Shuffle[simp]:
+ "Deriv a (A \<parallel> B) = Deriv a A \<parallel> B \<union> A \<parallel> Deriv a B"
+ unfolding Shuffle_def Deriv_def by (fastforce simp: Cons_in_shuffle_iff neq_Nil_conv)
+
+lemma shuffle_subset_lists:
+ assumes "A \<subseteq> lists S" "B \<subseteq> lists S"
+ shows "A \<parallel> B \<subseteq> lists S"
+unfolding Shuffle_def proof safe
+ fix x and zs xs ys :: "'a list"
+ assume zs: "zs \<in> shuffle xs ys" "x \<in> set zs" and "xs \<in> A" "ys \<in> B"
+ with assms have "xs \<in> lists S" "ys \<in> lists S" by auto
+ with zs show "x \<in> S" by (induct xs ys arbitrary: zs rule: shuffle.induct) auto
+qed
+
+lemma Nil_in_Shuffle[simp]: "[] \<in> A \<parallel> B \<longleftrightarrow> [] \<in> A \<and> [] \<in> B"
+ unfolding Shuffle_def by force
+
+lemma shuffle_Un_distrib:
+shows "A \<parallel> (B \<union> C) = A \<parallel> B \<union> A \<parallel> C"
+and "A \<parallel> (B \<union> C) = A \<parallel> B \<union> A \<parallel> C"
+unfolding Shuffle_def by fast+
+
+lemma shuffle_UNION_distrib:
+shows "A \<parallel> UNION I M = UNION I (%i. A \<parallel> M i)"
+and "UNION I M \<parallel> A = UNION I (%i. M i \<parallel> A)"
+unfolding Shuffle_def by fast+
+
+lemma Shuffle_empty[simp]:
+ "A \<parallel> {} = {}"
+ "{} \<parallel> B = {}"
+ unfolding Shuffle_def by auto
+
+lemma Shuffle_eps[simp]:
+ "A \<parallel> {[]} = A"
+ "{[]} \<parallel> B = B"
+ unfolding Shuffle_def by auto
+
+
+subsection {* Arden's Lemma *}
+
+lemma arden_helper:
+ assumes eq: "X = A @@ X \<union> B"
+ shows "X = (A ^^ Suc n) @@ X \<union> (\<Union>m\<le>n. (A ^^ m) @@ B)"
+proof (induct n)
+ case 0
+ show "X = (A ^^ Suc 0) @@ X \<union> (\<Union>m\<le>0. (A ^^ m) @@ B)"
+ using eq by simp
+next
+ case (Suc n)
+ have ih: "X = (A ^^ Suc n) @@ X \<union> (\<Union>m\<le>n. (A ^^ m) @@ B)" by fact
+ also have "\<dots> = (A ^^ Suc n) @@ (A @@ X \<union> B) \<union> (\<Union>m\<le>n. (A ^^ m) @@ B)" using eq by simp
+ also have "\<dots> = (A ^^ Suc (Suc n)) @@ X \<union> ((A ^^ Suc n) @@ B) \<union> (\<Union>m\<le>n. (A ^^ m) @@ B)"
+ by (simp add: conc_Un_distrib conc_assoc[symmetric] conc_pow_comm)
+ also have "\<dots> = (A ^^ Suc (Suc n)) @@ X \<union> (\<Union>m\<le>Suc n. (A ^^ m) @@ B)"
+ by (auto simp add: le_Suc_eq)
+ finally show "X = (A ^^ Suc (Suc n)) @@ X \<union> (\<Union>m\<le>Suc n. (A ^^ m) @@ B)" .
+qed
+
+lemma Arden:
+ assumes "[] \<notin> A"
+ shows "X = A @@ X \<union> B \<longleftrightarrow> X = star A @@ B"
+proof
+ assume eq: "X = A @@ X \<union> B"
+ { fix w assume "w : X"
+ let ?n = "size w"
+ from `[] \<notin> A` have "ALL u : A. length u \<ge> 1"
+ by (metis Suc_eq_plus1 add_leD2 le_0_eq length_0_conv not_less_eq_eq)
+ hence "ALL u : A^^(?n+1). length u \<ge> ?n+1"
+ by (metis length_lang_pow_lb nat_mult_1)
+ hence "ALL u : A^^(?n+1)@@X. length u \<ge> ?n+1"
+ by(auto simp only: conc_def length_append)
+ hence "w \<notin> A^^(?n+1)@@X" by auto
+ hence "w : star A @@ B" using `w : X` using arden_helper[OF eq, where n="?n"]
+ by (auto simp add: star_def conc_UNION_distrib)
+ } moreover
+ { fix w assume "w : star A @@ B"
+ hence "EX n. w : A^^n @@ B" by(auto simp: conc_def star_def)
+ hence "w : X" using arden_helper[OF eq] by blast
+ } ultimately show "X = star A @@ B" by blast
+next
+ assume eq: "X = star A @@ B"
+ have "star A = A @@ star A \<union> {[]}"
+ by (rule star_unfold_left)
+ then have "star A @@ B = (A @@ star A \<union> {[]}) @@ B"
+ by metis
+ also have "\<dots> = (A @@ star A) @@ B \<union> B"
+ unfolding conc_Un_distrib by simp
+ also have "\<dots> = A @@ (star A @@ B) \<union> B"
+ by (simp only: conc_assoc)
+ finally show "X = A @@ X \<union> B"
+ using eq by blast
+qed
+
+
+lemma reversed_arden_helper:
+ assumes eq: "X = X @@ A \<union> B"
+ shows "X = X @@ (A ^^ Suc n) \<union> (\<Union>m\<le>n. B @@ (A ^^ m))"
+proof (induct n)
+ case 0
+ show "X = X @@ (A ^^ Suc 0) \<union> (\<Union>m\<le>0. B @@ (A ^^ m))"
+ using eq by simp
+next
+ case (Suc n)
+ have ih: "X = X @@ (A ^^ Suc n) \<union> (\<Union>m\<le>n. B @@ (A ^^ m))" by fact
+ also have "\<dots> = (X @@ A \<union> B) @@ (A ^^ Suc n) \<union> (\<Union>m\<le>n. B @@ (A ^^ m))" using eq by simp
+ also have "\<dots> = X @@ (A ^^ Suc (Suc n)) \<union> (B @@ (A ^^ Suc n)) \<union> (\<Union>m\<le>n. B @@ (A ^^ m))"
+ by (simp add: conc_Un_distrib conc_assoc)
+ also have "\<dots> = X @@ (A ^^ Suc (Suc n)) \<union> (\<Union>m\<le>Suc n. B @@ (A ^^ m))"
+ by (auto simp add: le_Suc_eq)
+ finally show "X = X @@ (A ^^ Suc (Suc n)) \<union> (\<Union>m\<le>Suc n. B @@ (A ^^ m))" .
+qed
+
+theorem reversed_Arden:
+ assumes nemp: "[] \<notin> A"
+ shows "X = X @@ A \<union> B \<longleftrightarrow> X = B @@ star A"
+proof
+ assume eq: "X = X @@ A \<union> B"
+ { fix w assume "w : X"
+ let ?n = "size w"
+ from `[] \<notin> A` have "ALL u : A. length u \<ge> 1"
+ by (metis Suc_eq_plus1 add_leD2 le_0_eq length_0_conv not_less_eq_eq)
+ hence "ALL u : A^^(?n+1). length u \<ge> ?n+1"
+ by (metis length_lang_pow_lb nat_mult_1)
+ hence "ALL u : X @@ A^^(?n+1). length u \<ge> ?n+1"
+ by(auto simp only: conc_def length_append)
+ hence "w \<notin> X @@ A^^(?n+1)" by auto
+ hence "w : B @@ star A" using `w : X` using reversed_arden_helper[OF eq, where n="?n"]
+ by (auto simp add: star_def conc_UNION_distrib)
+ } moreover
+ { fix w assume "w : B @@ star A"
+ hence "EX n. w : B @@ A^^n" by (auto simp: conc_def star_def)
+ hence "w : X" using reversed_arden_helper[OF eq] by blast
+ } ultimately show "X = B @@ star A" by blast
+next
+ assume eq: "X = B @@ star A"
+ have "star A = {[]} \<union> star A @@ A"
+ unfolding conc_star_comm[symmetric]
+ by(metis Un_commute star_unfold_left)
+ then have "B @@ star A = B @@ ({[]} \<union> star A @@ A)"
+ by metis
+ also have "\<dots> = B \<union> B @@ (star A @@ A)"
+ unfolding conc_Un_distrib by simp
+ also have "\<dots> = B \<union> (B @@ star A) @@ A"
+ by (simp only: conc_assoc)
+ finally show "X = X @@ A \<union> B"
+ using eq by blast
+qed
+
+end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/Simplifying.thy Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,239 @@
+(* Title: POSIX Lexing with Derivatives of Regular Expressions
+ Authors: Fahad Ausaf <fahad.ausaf at icloud.com>, 2016
+ Roy Dyckhoff <roy.dyckhoff at st-andrews.ac.uk>, 2016
+ Christian Urban <christian.urban at kcl.ac.uk>, 2016
+ Maintainer: Christian Urban <christian.urban at kcl.ac.uk>
+*)
+
+theory Simplifying
+ imports "Lexer"
+begin
+
+section {* Lexer including simplifications *}
+
+
+fun F_RIGHT where
+ "F_RIGHT f v = Right (f v)"
+
+fun F_LEFT where
+ "F_LEFT f v = Left (f v)"
+
+fun F_Plus where
+ "F_Plus f\<^sub>1 f\<^sub>2 (Right v) = Right (f\<^sub>2 v)"
+| "F_Plus f\<^sub>1 f\<^sub>2 (Left v) = Left (f\<^sub>1 v)"
+| "F_Plus f1 f2 v = v"
+
+
+fun F_Times1 where
+ "F_Times1 f\<^sub>1 f\<^sub>2 v = Seq (f\<^sub>1 Void) (f\<^sub>2 v)"
+
+fun F_Times2 where
+ "F_Times2 f\<^sub>1 f\<^sub>2 v = Seq (f\<^sub>1 v) (f\<^sub>2 Void)"
+
+fun F_Times where
+ "F_Times f\<^sub>1 f\<^sub>2 (Seq v\<^sub>1 v\<^sub>2) = Seq (f\<^sub>1 v\<^sub>1) (f\<^sub>2 v\<^sub>2)"
+| "F_Times f1 f2 v = v"
+
+fun simp_Plus where
+ "simp_Plus (Zero, f\<^sub>1) (r\<^sub>2, f\<^sub>2) = (r\<^sub>2, F_RIGHT f\<^sub>2)"
+| "simp_Plus (r\<^sub>1, f\<^sub>1) (Zero, f\<^sub>2) = (r\<^sub>1, F_LEFT f\<^sub>1)"
+| "simp_Plus (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2) = (Plus r\<^sub>1 r\<^sub>2, F_Plus f\<^sub>1 f\<^sub>2)"
+
+fun simp_Times where
+ "simp_Times (One, f\<^sub>1) (r\<^sub>2, f\<^sub>2) = (r\<^sub>2, F_Times1 f\<^sub>1 f\<^sub>2)"
+| "simp_Times (r\<^sub>1, f\<^sub>1) (One, f\<^sub>2) = (r\<^sub>1, F_Times2 f\<^sub>1 f\<^sub>2)"
+| "simp_Times (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2) = (Times r\<^sub>1 r\<^sub>2, F_Times f\<^sub>1 f\<^sub>2)"
+
+lemma simp_Times_simps[simp]:
+ "simp_Times p1 p2 = (if (fst p1 = One) then (fst p2, F_Times1 (snd p1) (snd p2))
+ else (if (fst p2 = One) then (fst p1, F_Times2 (snd p1) (snd p2))
+ else (Times (fst p1) (fst p2), F_Times (snd p1) (snd p2))))"
+by (induct p1 p2 rule: simp_Times.induct) (auto)
+
+lemma simp_Plus_simps[simp]:
+ "simp_Plus p1 p2 = (if (fst p1 = Zero) then (fst p2, F_RIGHT (snd p2))
+ else (if (fst p2 = Zero) then (fst p1, F_LEFT (snd p1))
+ else (Plus (fst p1) (fst p2), F_Plus (snd p1) (snd p2))))"
+by (induct p1 p2 rule: simp_Plus.induct) (auto)
+
+fun
+ simp :: "'a rexp \<Rightarrow> 'a rexp * ('a val \<Rightarrow> 'a val)"
+where
+ "simp (Plus r1 r2) = simp_Plus (simp r1) (simp r2)"
+| "simp (Times r1 r2) = simp_Times (simp r1) (simp r2)"
+| "simp r = (r, id)"
+
+fun
+ slexer :: "'a rexp \<Rightarrow> 'a list \<Rightarrow> ('a val) option"
+where
+ "slexer r [] = (if nullable r then Some(mkeps r) else None)"
+| "slexer r (c#s) = (let (rs, fr) = simp (deriv c r) in
+ (case (slexer rs s) of
+ None \<Rightarrow> None
+ | Some(v) \<Rightarrow> Some(injval r c (fr v))))"
+
+lemma slexer_better_simp:
+ "slexer r (c#s) = (case (slexer (fst (simp (deriv c r))) s) of
+ None \<Rightarrow> None
+ | Some(v) \<Rightarrow> Some(injval r c ((snd (simp (deriv c r))) v)))"
+by (auto split: prod.split option.split)
+
+
+lemma L_fst_simp:
+ shows "lang r = lang (fst (simp r))"
+using assms
+by (induct r) (auto)
+
+lemma Posix_simp:
+ assumes "s \<in> (fst (simp r)) \<rightarrow> v"
+ shows "s \<in> r \<rightarrow> ((snd (simp r)) v)"
+using assms
+proof(induct r arbitrary: s v rule: rexp.induct)
+ case (Plus r1 r2 s v)
+ have IH1: "\<And>s v. s \<in> fst (simp r1) \<rightarrow> v \<Longrightarrow> s \<in> r1 \<rightarrow> snd (simp r1) v" by fact
+ have IH2: "\<And>s v. s \<in> fst (simp r2) \<rightarrow> v \<Longrightarrow> s \<in> r2 \<rightarrow> snd (simp r2) v" by fact
+ have as: "s \<in> fst (simp (Plus r1 r2)) \<rightarrow> v" by fact
+ consider (Zero_Zero) "fst (simp r1) = Zero" "fst (simp r2) = Zero"
+ | (Zero_NZero) "fst (simp r1) = Zero" "fst (simp r2) \<noteq> Zero"
+ | (NZero_Zero) "fst (simp r1) \<noteq> Zero" "fst (simp r2) = Zero"
+ | (NZero_NZero) "fst (simp r1) \<noteq> Zero" "fst (simp r2) \<noteq> Zero" by auto
+ then show "s \<in> Plus r1 r2 \<rightarrow> snd (simp (Plus r1 r2)) v"
+ proof(cases)
+ case (Zero_Zero)
+ with as have "s \<in> Zero \<rightarrow> v" by simp
+ then show "s \<in> Plus r1 r2 \<rightarrow> snd (simp (Plus r1 r2)) v" by (rule Posix_elims(1))
+ next
+ case (Zero_NZero)
+ with as have "s \<in> fst (simp r2) \<rightarrow> v" by simp
+ with IH2 have "s \<in> r2 \<rightarrow> snd (simp r2) v" by simp
+ moreover
+ from Zero_NZero have "fst (simp r1) = Zero" by simp
+ then have "lang (fst (simp r1)) = {}" by simp
+ then have "lang r1 = {}" using L_fst_simp by auto
+ then have "s \<notin> lang r1" by simp
+ ultimately have "s \<in> Plus r1 r2 \<rightarrow> Right (snd (simp r2) v)" by (rule Posix_Plus2)
+ then show "s \<in> Plus r1 r2 \<rightarrow> snd (simp (Plus r1 r2)) v"
+ using Zero_NZero by simp
+ next
+ case (NZero_Zero)
+ with as have "s \<in> fst (simp r1) \<rightarrow> v" by simp
+ with IH1 have "s \<in> r1 \<rightarrow> snd (simp r1) v" by simp
+ then have "s \<in> Plus r1 r2 \<rightarrow> Left (snd (simp r1) v)" by (rule Posix_Plus1)
+ then show "s \<in> Plus r1 r2 \<rightarrow> snd (simp (Plus r1 r2)) v" using NZero_Zero by simp
+ next
+ case (NZero_NZero)
+ with as have "s \<in> Plus (fst (simp r1)) (fst (simp r2)) \<rightarrow> v" by simp
+ then consider (Left) v1 where "v = Left v1" "s \<in> (fst (simp r1)) \<rightarrow> v1"
+ | (Right) v2 where "v = Right v2" "s \<in> (fst (simp r2)) \<rightarrow> v2" "s \<notin> lang (fst (simp r1))"
+ by (erule_tac Posix_elims(4))
+ then show "s \<in> Plus r1 r2 \<rightarrow> snd (simp (Plus r1 r2)) v"
+ proof(cases)
+ case (Left)
+ then have "v = Left v1" "s \<in> r1 \<rightarrow> (snd (simp r1) v1)" using IH1 by simp_all
+ then show "s \<in> Plus r1 r2 \<rightarrow> snd (simp (Plus r1 r2)) v" using NZero_NZero
+ by (simp_all add: Posix_Plus1)
+ next
+ case (Right)
+ then have "v = Right v2" "s \<in> r2 \<rightarrow> (snd (simp r2) v2)" "s \<notin> lang r1" using IH2 L_fst_simp by auto
+ then show "s \<in> Plus r1 r2 \<rightarrow> snd (simp (Plus r1 r2)) v" using NZero_NZero
+ by (simp_all add: Posix_Plus2)
+ qed
+ qed
+next
+ case (Times r1 r2 s v)
+ have IH1: "\<And>s v. s \<in> fst (simp r1) \<rightarrow> v \<Longrightarrow> s \<in> r1 \<rightarrow> snd (simp r1) v" by fact
+ have IH2: "\<And>s v. s \<in> fst (simp r2) \<rightarrow> v \<Longrightarrow> s \<in> r2 \<rightarrow> snd (simp r2) v" by fact
+ have as: "s \<in> fst (simp (Times r1 r2)) \<rightarrow> v" by fact
+ consider (One_One) "fst (simp r1) = One" "fst (simp r2) = One"
+ | (One_NOne) "fst (simp r1) = One" "fst (simp r2) \<noteq> One"
+ | (NOne_One) "fst (simp r1) \<noteq> One" "fst (simp r2) = One"
+ | (NOne_NOne) "fst (simp r1) \<noteq> One" "fst (simp r2) \<noteq> One" by auto
+ then show "s \<in> Times r1 r2 \<rightarrow> snd (simp (Times r1 r2)) v"
+ proof(cases)
+ case (One_One)
+ with as have b: "s \<in> One \<rightarrow> v" by simp
+ from b have "s \<in> r1 \<rightarrow> snd (simp r1) v" using IH1 One_One by simp
+ moreover
+ from b have c: "s = []" "v = Void" using Posix_elims(2) by auto
+ moreover
+ have "[] \<in> One \<rightarrow> Void" by (simp add: Posix_One)
+ then have "[] \<in> fst (simp r2) \<rightarrow> Void" using One_One by simp
+ then have "[] \<in> r2 \<rightarrow> snd (simp r2) Void" using IH2 by simp
+ ultimately have "([] @ []) \<in> Times r1 r2 \<rightarrow> Seq (snd (simp r1) Void) (snd (simp r2) Void)"
+ using Posix_Times by blast
+ then show "s \<in> Times r1 r2 \<rightarrow> snd (simp (Times r1 r2)) v" using c One_One by simp
+ next
+ case (One_NOne)
+ with as have b: "s \<in> fst (simp r2) \<rightarrow> v" by simp
+ from b have "s \<in> r2 \<rightarrow> snd (simp r2) v" using IH2 One_NOne by simp
+ moreover
+ have "[] \<in> One \<rightarrow> Void" by (simp add: Posix_One)
+ then have "[] \<in> fst (simp r1) \<rightarrow> Void" using One_NOne by simp
+ then have "[] \<in> r1 \<rightarrow> snd (simp r1) Void" using IH1 by simp
+ moreover
+ from One_NOne(1) have "lang (fst (simp r1)) = {[]}" by simp
+ then have "lang r1 = {[]}" by (simp add: L_fst_simp[symmetric])
+ ultimately have "([] @ s) \<in> Times r1 r2 \<rightarrow> Seq (snd (simp r1) Void) (snd (simp r2) v)"
+ by(rule_tac Posix_Times) auto
+ then show "s \<in> Times r1 r2 \<rightarrow> snd (simp (Times r1 r2)) v" using One_NOne by simp
+ next
+ case (NOne_One)
+ with as have "s \<in> fst (simp r1) \<rightarrow> v" by simp
+ with IH1 have "s \<in> r1 \<rightarrow> snd (simp r1) v" by simp
+ moreover
+ have "[] \<in> One \<rightarrow> Void" by (simp add: Posix_One)
+ then have "[] \<in> fst (simp r2) \<rightarrow> Void" using NOne_One by simp
+ then have "[] \<in> r2 \<rightarrow> snd (simp r2) Void" using IH2 by simp
+ ultimately have "(s @ []) \<in> Times r1 r2 \<rightarrow> Seq (snd (simp r1) v) (snd (simp r2) Void)"
+ by(rule_tac Posix_Times) auto
+ then show "s \<in> Times r1 r2 \<rightarrow> snd (simp (Times r1 r2)) v" using NOne_One by simp
+ next
+ case (NOne_NOne)
+ with as have "s \<in> Times (fst (simp r1)) (fst (simp r2)) \<rightarrow> v" by simp
+ then obtain s1 s2 v1 v2 where eqs: "s = s1 @ s2" "v = Seq v1 v2"
+ "s1 \<in> (fst (simp r1)) \<rightarrow> v1" "s2 \<in> (fst (simp r2)) \<rightarrow> v2"
+ "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> lang r1 \<and> s\<^sub>4 \<in> lang r2)"
+ by (erule_tac Posix_elims(5)) (auto simp add: L_fst_simp[symmetric])
+ then have "s1 \<in> r1 \<rightarrow> (snd (simp r1) v1)" "s2 \<in> r2 \<rightarrow> (snd (simp r2) v2)"
+ using IH1 IH2 by auto
+ then show "s \<in> Times r1 r2 \<rightarrow> snd (simp (Times r1 r2)) v" using eqs NOne_NOne
+ by(auto intro: Posix_Times)
+ qed
+qed (simp_all)
+
+
+lemma slexer_correctness:
+ shows "slexer r s = lexer r s"
+proof(induct s arbitrary: r)
+ case Nil
+ show "slexer r [] = lexer r []" by simp
+next
+ case (Cons c s r)
+ have IH: "\<And>r. slexer r s = lexer r s" by fact
+ show "slexer r (c # s) = lexer r (c # s)"
+ proof (cases "s \<in> lang (deriv c r)")
+ case True
+ assume a1: "s \<in> lang (deriv c r)"
+ then obtain v1 where a2: "lexer (deriv c r) s = Some v1" "s \<in> deriv c r \<rightarrow> v1"
+ using lexer_correct_Some by auto
+ from a1 have "s \<in> lang (fst (simp (deriv c r)))" using L_fst_simp[symmetric] by auto
+ then obtain v2 where a3: "lexer (fst (simp (deriv c r))) s = Some v2" "s \<in> (fst (simp (deriv c r))) \<rightarrow> v2"
+ using lexer_correct_Some by auto
+ then have a4: "slexer (fst (simp (deriv c r))) s = Some v2" using IH by simp
+ from a3(2) have "s \<in> deriv c r \<rightarrow> (snd (simp (deriv c r))) v2" using Posix_simp by auto
+ with a2(2) have "v1 = (snd (simp (deriv c r))) v2" using Posix_determ by auto
+ with a2(1) a4 show "slexer r (c # s) = lexer r (c # s)" by (auto split: prod.split)
+ next
+ case False
+ assume b1: "s \<notin> lang (deriv c r)"
+ then have "lexer (deriv c r) s = None" using lexer_correct_None by auto
+ moreover
+ from b1 have "s \<notin> lang (fst (simp (deriv c r)))" using L_fst_simp[symmetric] by auto
+ then have "lexer (fst (simp (deriv c r))) s = None" using lexer_correct_None by auto
+ then have "slexer (fst (simp (deriv c r))) s = None" using IH by simp
+ ultimately show "slexer r (c # s) = lexer r (c # s)"
+ by (simp del: slexer.simps add: slexer_better_simp)
+ qed
+qed
+
+end
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/document/root.bib Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,10 @@
+
+@inproceedings{Sulzmann2014,
+ author = {M.~Sulzmann and K.~Lu},
+ title = {{POSIX} {R}egular {E}xpression {P}arsing with {D}erivatives},
+ booktitle = {Proc.~of the 12th International Conference on Functional and Logic Programming (FLOPS)},
+ pages = {203--220},
+ year = {2014},
+ volume = {8475},
+ series = {LNCS}
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AFP-Submission/document/root.tex Tue May 24 11:36:21 2016 +0100
@@ -0,0 +1,41 @@
+\documentclass[11pt,a4paper]{article}
+\usepackage{isabelle,isabellesym}
+
+% this should be the last package used
+\usepackage{pdfsetup}
+
+% urls in roman style, theory text in math-similar italics
+\urlstyle{rm}
+\isabellestyle{it}
+
+
+\begin{document}
+
+\title{POSIX Lexing with Derivatives of Regular Expressions}
+\author{Fahad Ausaf \and Roy Dyckhoff \and Christian Urban}
+\maketitle
+
+\begin{abstract}
+ Brzozowski introduced the notion of derivatives for regular
+ expressions. They can be used for a very simple regular expression
+ matching algorithm. Sulzmann and Lu \cite{Sulzmann2014} cleverly extended this algorithm
+ in order to deal with POSIX matching, which is the underlying
+ disambiguation strategy for regular expressions needed in
+ lexers. In this entry we give our inductive definition
+ of what a POSIX value is and show (i) that such a value is unique (for
+ given regular expression and string being matched) and (ii) that
+ Sulzmann and Lu's algorithm always generates such a value (provided
+ that the regular expression matches the string). We also prove the
+ correctness of an optimised version of the POSIX matching
+ algorithm.
+\end{abstract}
+
+\tableofcontents
+
+% include generated text of all theories
+\input{session}
+
+\bibliographystyle{abbrv}
+\bibliography{root}
+
+\end{document}