thys/ReStar.thy
changeset 106 489dfa0d7ec9
parent 105 80218dddbb15
child 107 6adda4a667b1
equal deleted inserted replaced
105:80218dddbb15 106:489dfa0d7ec9
     2 theory ReStar
     2 theory ReStar
     3   imports "Main" 
     3   imports "Main" 
     4 begin
     4 begin
     5 
     5 
     6 
     6 
     7 section {* Sequential Composition of Sets *}
     7 section {* Sequential Composition of Languages *}
     8 
     8 
     9 definition
     9 definition
    10   Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
    10   Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
    11 where 
    11 where 
    12   "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
    12   "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
    13 
    13 
    14 fun spow where
       
    15   "spow s 0 = []"
       
    16 | "spow s (Suc n) = s @ spow s n"
       
    17 
       
    18 text {* Two Simple Properties about Sequential Composition *}
    14 text {* Two Simple Properties about Sequential Composition *}
    19 
    15 
    20 lemma seq_empty [simp]:
    16 lemma seq_empty [simp]:
    21   shows "A ;; {[]} = A"
    17   shows "A ;; {[]} = A"
    22   and   "{[]} ;; A = A"
    18   and   "{[]} ;; A = A"
    25 lemma seq_null [simp]:
    21 lemma seq_null [simp]:
    26   shows "A ;; {} = {}"
    22   shows "A ;; {} = {}"
    27   and   "{} ;; A = {}"
    23   and   "{} ;; A = {}"
    28 by (simp_all add: Sequ_def)
    24 by (simp_all add: Sequ_def)
    29 
    25 
       
    26 
       
    27 section {* Semantic Derivatives of Languages *}
       
    28 
    30 definition
    29 definition
    31   Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
    30   Der :: "char \<Rightarrow> string set \<Rightarrow> string set"
    32 where
    31 where
    33   "Der c A \<equiv> {s. [c] @ s \<in> A}"
    32   "Der c A \<equiv> {s. [c] @ s \<in> A}"
    34 
    33 
    55 lemma Der_union [simp]:
    54 lemma Der_union [simp]:
    56   shows "Der c (A \<union> B) = Der c A \<union> Der c B"
    55   shows "Der c (A \<union> B) = Der c A \<union> Der c B"
    57 unfolding Der_def
    56 unfolding Der_def
    58 by auto
    57 by auto
    59 
    58 
    60 lemma Der_seq [simp]:
    59 lemma Der_Sequ [simp]:
    61   shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})"
    60   shows "Der c (A ;; B) = (Der c A) ;; B \<union> (if [] \<in> A then Der c B else {})"
    62 unfolding Der_def Sequ_def
    61 unfolding Der_def Sequ_def
    63 apply (auto simp add: Cons_eq_append_conv)
    62 by (auto simp add: Cons_eq_append_conv)
    64 done
    63 
    65 
    64 
    66 lemma seq_image:
    65 section {* Kleene Star for Languages *}
    67   assumes "\<forall>s1 s2. f (s1 @ s2) = (f s1) @ (f s2)"
       
    68   shows "f ` (A ;; B) = (f ` A) ;; (f ` B)"
       
    69 apply(auto simp add: Sequ_def image_def)
       
    70 apply(rule_tac x="f s1" in exI)
       
    71 apply(rule_tac x="f s2" in exI)
       
    72 using assms
       
    73 apply(auto)
       
    74 apply(rule_tac x="xa @ xb" in exI)
       
    75 using assms
       
    76 apply(auto)
       
    77 done
       
    78 
       
    79 section {* Kleene Star for Sets *}
       
    80 
    66 
    81 inductive_set
    67 inductive_set
    82   Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
    68   Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
    83   for A :: "string set"
    69   for A :: "string set"
    84 where
    70 where
    87 
    73 
    88 lemma star_cases:
    74 lemma star_cases:
    89   shows "A\<star> = {[]} \<union> A ;; A\<star>"
    75   shows "A\<star> = {[]} \<union> A ;; A\<star>"
    90 unfolding Sequ_def
    76 unfolding Sequ_def
    91 by (auto) (metis Star.simps)
    77 by (auto) (metis Star.simps)
    92 
       
    93 
       
    94 fun 
       
    95   pow :: "string set \<Rightarrow> nat \<Rightarrow> string set" ("_ \<up> _" [100,100] 100)
       
    96 where
       
    97   "A \<up> 0 = {[]}"
       
    98 | "A \<up> (Suc n) = A ;; (A \<up> n)"  
       
    99 
       
   100 lemma star1: 
       
   101   shows "s \<in> A\<star> \<Longrightarrow> \<exists>n. s \<in> A \<up> n"
       
   102   apply(induct rule: Star.induct)
       
   103   apply (metis pow.simps(1) insertI1)
       
   104   apply(auto)
       
   105   apply(rule_tac x="Suc n" in exI)
       
   106   apply(auto simp add: Sequ_def)
       
   107   done
       
   108 
       
   109 lemma star2:
       
   110   shows "s \<in> A \<up> n \<Longrightarrow> s \<in> A\<star>"
       
   111   apply(induct n arbitrary: s)
       
   112   apply (metis pow.simps(1) Star.simps empty_iff insertE)
       
   113   apply(auto simp add: Sequ_def)
       
   114   done
       
   115 
       
   116 lemma star3:
       
   117   shows "A\<star> = (\<Union>i. A \<up> i)"
       
   118 using star1 star2
       
   119 apply(auto)
       
   120 done
       
   121 
       
   122 lemma star4:
       
   123   shows "s \<in> A \<up> n \<Longrightarrow> \<exists>ss. s = concat ss \<and> (\<forall>s' \<in> set ss. s' \<in> A)"
       
   124   apply(induct n arbitrary: s)
       
   125   apply(auto simp add: Sequ_def)
       
   126   apply(rule_tac x="[]" in exI)
       
   127   apply(auto)
       
   128   apply(drule_tac x="s2" in meta_spec)
       
   129   apply(auto)
       
   130 by (metis concat.simps(2) insertE set_simps(2))
       
   131 
       
   132 lemma star5:
       
   133   assumes "f [] = []"
       
   134   assumes "\<forall>s1 s2. f (s1 @ s2) = (f s1) @ (f s2)"
       
   135   shows "(f ` A) \<up> n = f ` (A \<up> n)"
       
   136 apply(induct n)
       
   137 apply(simp add: assms)
       
   138 apply(simp)
       
   139 apply(subst seq_image[OF assms(2)])
       
   140 apply(simp)
       
   141 done
       
   142 
       
   143 lemma star6:
       
   144   assumes "f [] = []"
       
   145   assumes "\<forall>s1 s2. f (s1 @ s2) = (f s1) @ (f s2)"
       
   146   shows "(f ` A)\<star> = f ` (A\<star>)"
       
   147 apply(simp add: star3)
       
   148 apply(simp add: image_UN)
       
   149 apply(subst star5[OF assms])
       
   150 apply(simp)
       
   151 done
       
   152 
    78 
   153 lemma star_decomp: 
    79 lemma star_decomp: 
   154   assumes a: "c # x \<in> A\<star>" 
    80   assumes a: "c # x \<in> A\<star>" 
   155   shows "\<exists>a b. x = a @ b \<and> c # a \<in> A \<and> b \<in> A\<star>"
    81   shows "\<exists>a b. x = a @ b \<and> c # a \<in> A \<and> b \<in> A\<star>"
   156 using a
    82 using a
   172     by (auto dest: star_decomp)
    98     by (auto dest: star_decomp)
   173   finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" .
    99   finally show "Der c (A\<star>) = (Der c A) ;; A\<star>" .
   174 qed
   100 qed
   175 
   101 
   176 
   102 
   177 
       
   178 section {* Regular Expressions *}
   103 section {* Regular Expressions *}
   179 
   104 
   180 datatype rexp =
   105 datatype rexp =
   181   NULL
   106   NULL
   182 | EMPTY
   107 | EMPTY
   207 | "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
   132 | "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
   208 | "nullable (STAR r) = True"
   133 | "nullable (STAR r) = True"
   209 
   134 
   210 lemma nullable_correctness:
   135 lemma nullable_correctness:
   211   shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
   136   shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
   212 apply (induct r) 
   137 by (induct r) (auto simp add: Sequ_def) 
   213 apply(auto simp add: Sequ_def) 
       
   214 done
       
   215 
       
   216 
   138 
   217 
   139 
   218 section {* Values *}
   140 section {* Values *}
   219 
   141 
   220 datatype val = 
   142 datatype val = 
   236 | "flat (Right v) = flat v"
   158 | "flat (Right v) = flat v"
   237 | "flat (Seq v1 v2) = (flat v1) @ (flat v2)"
   159 | "flat (Seq v1 v2) = (flat v1) @ (flat v2)"
   238 | "flat (Stars []) = []"
   160 | "flat (Stars []) = []"
   239 | "flat (Stars (v#vs)) = (flat v) @ (flat (Stars vs))" 
   161 | "flat (Stars (v#vs)) = (flat v) @ (flat (Stars vs))" 
   240 
   162 
   241 lemma [simp]:
   163 lemma flat_Stars [simp]:
   242  "flat (Stars vs) = concat (map flat vs)"
   164  "flat (Stars vs) = concat (map flat vs)"
   243 apply(induct vs)
   165 by (induct vs) (auto)
   244 apply(auto)
   166 
   245 done
       
   246 
   167 
   247 section {* Relation between values and regular expressions *}
   168 section {* Relation between values and regular expressions *}
   248 
       
   249 (* non-problematic values...needed in order to fix the *) 
       
   250 (* proj lemma in Sulzmann & lu *)
       
   251 
       
   252 inductive 
       
   253   NPrf :: "val \<Rightarrow> rexp \<Rightarrow> bool" ("\<Turnstile> _ : _" [100, 100] 100)
       
   254 where
       
   255  "\<lbrakk>\<Turnstile> v1 : r1; \<Turnstile> v2 : r2\<rbrakk> \<Longrightarrow> \<Turnstile> Seq v1 v2 : SEQ r1 r2"
       
   256 | "\<Turnstile> v1 : r1 \<Longrightarrow> \<Turnstile> Left v1 : ALT r1 r2"
       
   257 | "\<Turnstile> v2 : r2 \<Longrightarrow> \<Turnstile> Right v2 : ALT r1 r2"
       
   258 | "\<Turnstile> Void : EMPTY"
       
   259 | "\<Turnstile> Char c : CHAR c"
       
   260 | "\<Turnstile> Stars [] : STAR r"
       
   261 | "\<lbrakk>\<Turnstile> v : r; \<Turnstile> Stars vs : STAR r; flat v \<noteq> []\<rbrakk> \<Longrightarrow> \<Turnstile> Stars (v # vs) : STAR r"
       
   262 
   169 
   263 inductive 
   170 inductive 
   264   Prf :: "val \<Rightarrow> rexp \<Rightarrow> bool" ("\<turnstile> _ : _" [100, 100] 100)
   171   Prf :: "val \<Rightarrow> rexp \<Rightarrow> bool" ("\<turnstile> _ : _" [100, 100] 100)
   265 where
   172 where
   266  "\<lbrakk>\<turnstile> v1 : r1; \<turnstile> v2 : r2\<rbrakk> \<Longrightarrow> \<turnstile> Seq v1 v2 : SEQ r1 r2"
   173  "\<lbrakk>\<turnstile> v1 : r1; \<turnstile> v2 : r2\<rbrakk> \<Longrightarrow> \<turnstile> Seq v1 v2 : SEQ r1 r2"
   269 | "\<turnstile> Void : EMPTY"
   176 | "\<turnstile> Void : EMPTY"
   270 | "\<turnstile> Char c : CHAR c"
   177 | "\<turnstile> Char c : CHAR c"
   271 | "\<turnstile> Stars [] : STAR r"
   178 | "\<turnstile> Stars [] : STAR r"
   272 | "\<lbrakk>\<turnstile> v : r; \<turnstile> Stars vs : STAR r\<rbrakk> \<Longrightarrow> \<turnstile> Stars (v # vs) : STAR r"
   179 | "\<lbrakk>\<turnstile> v : r; \<turnstile> Stars vs : STAR r\<rbrakk> \<Longrightarrow> \<turnstile> Stars (v # vs) : STAR r"
   273 
   180 
   274 lemma NPrf_imp_Prf:
       
   275   assumes "\<Turnstile> v : r" 
       
   276   shows "\<turnstile> v : r"
       
   277 using assms
       
   278 apply(induct)
       
   279 apply(auto intro: Prf.intros)
       
   280 done
       
   281 
       
   282 lemma NPrf_Prf_val:
       
   283   shows "\<turnstile> v : r \<Longrightarrow> \<exists>v'. flat v' = flat v \<and> \<Turnstile> v' : r"
       
   284   and   "\<turnstile> Stars vs : r \<Longrightarrow> \<exists>vs'. flat (Stars vs') = flat (Stars vs) \<and> \<Turnstile> Stars vs' : r"
       
   285 using assms
       
   286 apply(induct v and vs arbitrary: r and r rule: val.inducts)
       
   287 apply(auto)[1]
       
   288 apply(erule Prf.cases)
       
   289 apply(simp_all)[7]
       
   290 apply(rule_tac x="Void" in exI)
       
   291 apply(simp)
       
   292 apply(rule NPrf.intros)
       
   293 apply(erule Prf.cases)
       
   294 apply(simp_all)[7]
       
   295 apply(rule_tac x="Char c" in exI)
       
   296 apply(simp)
       
   297 apply(rule NPrf.intros)
       
   298 apply(erule Prf.cases)
       
   299 apply(simp_all)[7]
       
   300 apply(auto)[1]
       
   301 apply(drule_tac x="r1" in meta_spec)
       
   302 apply(drule_tac x="r2" in meta_spec)
       
   303 apply(simp)
       
   304 apply(auto)[1]
       
   305 apply(rule_tac x="Seq v' v'a" in exI)
       
   306 apply(simp)
       
   307 apply (metis NPrf.intros(1))
       
   308 apply(erule Prf.cases)
       
   309 apply(simp_all)[7]
       
   310 apply(clarify)
       
   311 apply(drule_tac x="r2" in meta_spec)
       
   312 apply(simp)
       
   313 apply(auto)[1]
       
   314 apply(rule_tac x="Right v'" in exI)
       
   315 apply(simp)
       
   316 apply (metis NPrf.intros)
       
   317 apply(erule Prf.cases)
       
   318 apply(simp_all)[7]
       
   319 apply(clarify)
       
   320 apply(drule_tac x="r1" in meta_spec)
       
   321 apply(simp)
       
   322 apply(auto)[1]
       
   323 apply(rule_tac x="Left v'" in exI)
       
   324 apply(simp)
       
   325 apply (metis NPrf.intros)
       
   326 apply(drule_tac x="r" in meta_spec)
       
   327 apply(simp)
       
   328 apply(auto)[1]
       
   329 apply(rule_tac x="Stars vs'" in exI)
       
   330 apply(simp)
       
   331 apply(rule_tac x="[]" in exI)
       
   332 apply(simp)
       
   333 apply(erule Prf.cases)
       
   334 apply(simp_all)[7]
       
   335 apply (metis NPrf.intros(6))
       
   336 apply(erule Prf.cases)
       
   337 apply(simp_all)[7]
       
   338 apply(auto)[1]
       
   339 apply(drule_tac x="ra" in meta_spec)
       
   340 apply(simp)
       
   341 apply(drule_tac x="STAR ra" in meta_spec)
       
   342 apply(simp)
       
   343 apply(auto)
       
   344 apply(case_tac "flat v = []")
       
   345 apply(rule_tac x="vs'" in exI)
       
   346 apply(simp)
       
   347 apply(rule_tac x="v' # vs'" in exI)
       
   348 apply(simp)
       
   349 apply(rule NPrf.intros)
       
   350 apply(auto)
       
   351 done
       
   352 
       
   353 lemma NPrf_Prf:
       
   354   shows "{flat v | v. \<turnstile> v : r} = {flat v | v. \<Turnstile> v : r}"
       
   355 apply(auto)
       
   356 apply (metis NPrf_Prf_val(1))
       
   357 by (metis NPrf_imp_Prf)
       
   358 
       
   359 
       
   360 lemma not_nullable_flat:
   181 lemma not_nullable_flat:
   361   assumes "\<turnstile> v : r" "\<not>nullable r"
   182   assumes "\<turnstile> v : r" "\<not> nullable r"
   362   shows "flat v \<noteq> []"
   183   shows "flat v \<noteq> []"
   363 using assms
   184 using assms
   364 apply(induct)
   185 by (induct) (auto)
   365 apply(auto)
       
   366 done
       
   367 
   186 
   368 lemma Prf_flat_L:
   187 lemma Prf_flat_L:
   369   assumes "\<turnstile> v : r" shows "flat v \<in> L r"
   188   assumes "\<turnstile> v : r" shows "flat v \<in> L r"
   370 using assms
   189 using assms
   371 apply(induct v r rule: Prf.induct)
   190 apply(induct v r rule: Prf.induct)
   372 apply(auto simp add: Sequ_def)
   191 apply(auto simp add: Sequ_def)
   373 done
   192 done
   374 
       
   375 lemma NPrf_flat_L:
       
   376   assumes "\<Turnstile> v : r" shows "flat v \<in> L r"
       
   377 using assms
       
   378 by (metis NPrf_imp_Prf Prf_flat_L)
       
   379 
   193 
   380 lemma Prf_Stars:
   194 lemma Prf_Stars:
   381   assumes "\<forall>v \<in> set vs. \<turnstile> v : r"
   195   assumes "\<forall>v \<in> set vs. \<turnstile> v : r"
   382   shows "\<turnstile> Stars vs : STAR r"
   196   shows "\<turnstile> Stars vs : STAR r"
   383 using assms
   197 using assms
   404 apply(induct ss)
   218 apply(induct ss)
   405 apply(auto)
   219 apply(auto)
   406 apply (metis empty_iff list.set(1))
   220 apply (metis empty_iff list.set(1))
   407 by (metis concat.simps(2) list.simps(9) set_ConsD)
   221 by (metis concat.simps(2) list.simps(9) set_ConsD)
   408 
   222 
   409 lemma Star_valN:
       
   410   assumes "\<forall>s\<in>set ss. \<exists>v. s = flat v \<and> \<Turnstile> v : r"
       
   411   shows "\<exists>vs. concat (map flat vs) = concat ss \<and> (\<forall>v\<in>set vs. \<Turnstile> v : r)"
       
   412 using assms
       
   413 apply(induct ss)
       
   414 apply(auto)
       
   415 apply (metis empty_iff list.set(1))
       
   416 by (metis concat.simps(2) list.simps(9) set_ConsD)
       
   417 
   223 
   418 lemma L_flat_Prf:
   224 lemma L_flat_Prf:
   419   "L(r) = {flat v | v. \<turnstile> v : r}"
   225   "L(r) = {flat v | v. \<turnstile> v : r}"
   420 apply(induct r)
   226 apply(induct r)
   421 apply(auto dest: Prf_flat_L simp add: Sequ_def)
   227 apply(auto dest: Prf_flat_L simp add: Sequ_def)
   436 apply(auto)
   242 apply(auto)
   437 apply(rule Star_val)
   243 apply(rule Star_val)
   438 apply(simp)
   244 apply(simp)
   439 done
   245 done
   440 
   246 
   441 lemma L_flat_NPrf:
       
   442   "L(r) = {flat v | v. \<Turnstile> v : r}"
       
   443 by (metis L_flat_Prf NPrf_Prf)
       
   444 
   247 
   445 section {* Values Sets *}
   248 section {* Values Sets *}
   446 
   249 
   447 definition prefix :: "string \<Rightarrow> string \<Rightarrow> bool" ("_ \<sqsubseteq> _" [100, 100] 100)
   250 definition prefix :: "string \<Rightarrow> string \<Rightarrow> bool" ("_ \<sqsubseteq> _" [100, 100] 100)
   448 where
   251 where
   515 
   318 
   516 
   319 
   517 definition Values :: "rexp \<Rightarrow> string \<Rightarrow> val set" where
   320 definition Values :: "rexp \<Rightarrow> string \<Rightarrow> val set" where
   518   "Values r s \<equiv> {v. \<turnstile> v : r \<and> flat v \<sqsubseteq> s}"
   321   "Values r s \<equiv> {v. \<turnstile> v : r \<and> flat v \<sqsubseteq> s}"
   519 
   322 
   520 definition SValues :: "rexp \<Rightarrow> string \<Rightarrow> val set" where
       
   521   "SValues r s \<equiv> {v. \<turnstile> v : r \<and> flat v = s}"
       
   522 
       
   523 definition NValues :: "rexp \<Rightarrow> string \<Rightarrow> val set" where
       
   524   "NValues r s \<equiv> {v. \<Turnstile> v : r \<and> flat v \<sqsubseteq> s}"
       
   525 
       
   526 lemma NValues_STAR_Nil:
       
   527   "NValues (STAR r) [] = {Stars []}"
       
   528 apply(auto simp add: NValues_def prefix_def)
       
   529 apply(erule NPrf.cases)
       
   530 apply(auto)
       
   531 by (metis NPrf.intros(6))
       
   532 
       
   533 
       
   534 definition rest :: "val \<Rightarrow> string \<Rightarrow> string" where
   323 definition rest :: "val \<Rightarrow> string \<Rightarrow> string" where
   535   "rest v s \<equiv> drop (length (flat v)) s"
   324   "rest v s \<equiv> drop (length (flat v)) s"
   536 
   325 
   537 lemma rest_Nil:
   326 lemma rest_Nil:
   538   "rest v [] = []"
   327   "rest v [] = []"
   541 
   330 
   542 lemma rest_Suffixes:
   331 lemma rest_Suffixes:
   543   "rest v s \<in> Suffixes s"
   332   "rest v s \<in> Suffixes s"
   544 unfolding rest_def
   333 unfolding rest_def
   545 by (metis Suffixes_in append_take_drop_id)
   334 by (metis Suffixes_in append_take_drop_id)
   546 
       
   547 lemma rest_SSuffixes:
       
   548   assumes "flat v \<noteq> []" "s \<noteq> []"
       
   549   shows "rest v s \<in> SSuffixes s"
       
   550 using assms
       
   551 unfolding rest_def
       
   552 thm SSuffixes_in
       
   553 apply(rule_tac SSuffixes_in)
       
   554 apply(rule_tac x="take (length (flat v)) s" in exI)
       
   555 apply(simp add: sprefix_def)
       
   556 done
       
   557 
       
   558 
   335 
   559 lemma Values_recs:
   336 lemma Values_recs:
   560   "Values (NULL) s = {}"
   337   "Values (NULL) s = {}"
   561   "Values (EMPTY) s = {Void}"
   338   "Values (EMPTY) s = {Void}"
   562   "Values (CHAR c) s = (if [c] \<sqsubseteq> s then {Char c} else {})" 
   339   "Values (CHAR c) s = (if [c] \<sqsubseteq> s then {Char c} else {})" 
   603 apply (metis Prf.intros(6))
   380 apply (metis Prf.intros(6))
   604 apply (metis append_Nil prefix_def)
   381 apply (metis append_Nil prefix_def)
   605 apply (metis Prf.intros(7))
   382 apply (metis Prf.intros(7))
   606 by (metis append_eq_conv_conj prefix_append prefix_def rest_def)
   383 by (metis append_eq_conv_conj prefix_append prefix_def rest_def)
   607 
   384 
   608 lemma NValues_recs:
       
   609   "NValues (NULL) s = {}"
       
   610   "NValues (EMPTY) s = {Void}"
       
   611   "NValues (CHAR c) s = (if [c] \<sqsubseteq> s then {Char c} else {})" 
       
   612   "NValues (ALT r1 r2) s = {Left v | v. v \<in> NValues r1 s} \<union> {Right v | v. v \<in> NValues r2 s}"
       
   613   "NValues (SEQ r1 r2) s = {Seq v1 v2 | v1 v2. v1 \<in> NValues r1 s \<and> v2 \<in> NValues r2 (rest v1 s)}"
       
   614   "NValues (STAR r) s = 
       
   615   {Stars []} \<union> {Stars (v # vs) | v vs. v \<in> NValues r s \<and> flat v \<noteq> [] \<and>  Stars vs \<in> NValues (STAR r) (rest v s)}"
       
   616 unfolding NValues_def
       
   617 apply(auto)
       
   618 (*NULL*)
       
   619 apply(erule NPrf.cases)
       
   620 apply(simp_all)[7]
       
   621 (*EMPTY*)
       
   622 apply(erule NPrf.cases)
       
   623 apply(simp_all)[7]
       
   624 apply(rule NPrf.intros)
       
   625 apply (metis append_Nil prefix_def)
       
   626 (*CHAR*)
       
   627 apply(erule NPrf.cases)
       
   628 apply(simp_all)[7]
       
   629 apply(rule NPrf.intros)
       
   630 apply(erule NPrf.cases)
       
   631 apply(simp_all)[7]
       
   632 (*ALT*)
       
   633 apply(erule NPrf.cases)
       
   634 apply(simp_all)[7]
       
   635 apply (metis NPrf.intros(2))
       
   636 apply (metis NPrf.intros(3))
       
   637 (*SEQ*)
       
   638 apply(erule NPrf.cases)
       
   639 apply(simp_all)[7]
       
   640 apply (simp add: append_eq_conv_conj prefix_def rest_def)
       
   641 apply (metis NPrf.intros(1))
       
   642 apply (simp add: append_eq_conv_conj prefix_def rest_def)
       
   643 (*STAR*)
       
   644 apply(erule NPrf.cases)
       
   645 apply(simp_all)
       
   646 apply(rule conjI)
       
   647 apply(simp add: prefix_def)
       
   648 apply(auto)[1]
       
   649 apply(simp add: prefix_def)
       
   650 apply(auto)[1]
       
   651 apply (metis append_eq_conv_conj rest_def)
       
   652 apply (metis NPrf.intros(6))
       
   653 apply (metis append_Nil prefix_def)
       
   654 apply (metis NPrf.intros(7))
       
   655 by (metis append_eq_conv_conj prefix_append prefix_def rest_def)
       
   656 
       
   657 lemma SValues_recs:
       
   658  "SValues (NULL) s = {}"
       
   659  "SValues (EMPTY) s = (if s = [] then {Void} else {})"
       
   660  "SValues (CHAR c) s = (if [c] = s then {Char c} else {})" 
       
   661  "SValues (ALT r1 r2) s = {Left v | v. v \<in> SValues r1 s} \<union> {Right v | v. v \<in> SValues r2 s}"
       
   662  "SValues (SEQ r1 r2) s = {Seq v1 v2 | v1 v2. \<exists>s1 s2. s = s1 @ s2 \<and> v1 \<in> SValues r1 s1 \<and> v2 \<in> SValues r2 s2}"
       
   663  "SValues (STAR r) s = (if s = [] then {Stars []} else {}) \<union> 
       
   664    {Stars (v # vs) | v vs. \<exists>s1 s2. s = s1 @ s2 \<and> v \<in> SValues r s1 \<and> Stars vs \<in> SValues (STAR r) s2}"
       
   665 unfolding SValues_def
       
   666 apply(auto)
       
   667 (*NULL*)
       
   668 apply(erule Prf.cases)
       
   669 apply(simp_all)[7]
       
   670 (*EMPTY*)
       
   671 apply(erule Prf.cases)
       
   672 apply(simp_all)[7]
       
   673 apply(rule Prf.intros)
       
   674 apply(erule Prf.cases)
       
   675 apply(simp_all)[7]
       
   676 (*CHAR*)
       
   677 apply(erule Prf.cases)
       
   678 apply(simp_all)[7]
       
   679 apply (metis Prf.intros(5))
       
   680 apply(erule Prf.cases)
       
   681 apply(simp_all)[7]
       
   682 (*ALT*)
       
   683 apply(erule Prf.cases)
       
   684 apply(simp_all)[7]
       
   685 apply metis
       
   686 apply(erule Prf.intros)
       
   687 apply(erule Prf.intros)
       
   688 (* SEQ case *)
       
   689 apply(erule Prf.cases)
       
   690 apply(simp_all)[7]
       
   691 apply (metis Prf.intros(1))
       
   692 (* STAR case *)
       
   693 apply(erule Prf.cases)
       
   694 apply(simp_all)[7]
       
   695 apply(rule Prf.intros)
       
   696 apply (metis Prf.intros(7))
       
   697 apply(erule Prf.cases)
       
   698 apply(simp_all)[7]
       
   699 apply (metis Prf.intros(7))
       
   700 by (metis Prf.intros(7))
       
   701 
       
   702 lemma finite_image_set2:
   385 lemma finite_image_set2:
   703   "finite {x. P x} \<Longrightarrow> finite {y. Q y} \<Longrightarrow> finite {(x, y) | x y. P x \<and> Q y}"
   386   "finite {x. P x} \<Longrightarrow> finite {y. Q y} \<Longrightarrow> finite {(x, y) | x y. P x \<and> Q y}"
   704   by (rule finite_subset [where B = "\<Union>x \<in> {x. P x}. \<Union>y \<in> {y. Q y}. {(x, y)}"]) auto
   387   by (rule finite_subset [where B = "\<Union>x \<in> {x. P x}. \<Union>y \<in> {y. Q y}. {(x, y)}"]) auto
   705 
       
   706 
       
   707 lemma NValues_finite_aux:
       
   708   "(\<lambda>(r, s). finite (NValues r s)) (r, s)"
       
   709 apply(rule wf_induct[of "measure size <*lex*> measure length",where P="\<lambda>(r, s). finite (NValues r s)"])
       
   710 apply (metis wf_lex_prod wf_measure)
       
   711 apply(auto)
       
   712 apply(case_tac a)
       
   713 apply(simp_all)
       
   714 apply(simp add: NValues_recs)
       
   715 apply(simp add: NValues_recs)
       
   716 apply(simp add: NValues_recs)
       
   717 apply(simp add: NValues_recs)
       
   718 apply(rule_tac f="\<lambda>(x, y). Seq x y" and 
       
   719                A="{(v1, v2) | v1 v2. v1 \<in> NValues rexp1 b \<and> v2 \<in> NValues rexp2 (rest v1 b)}" in finite_surj)
       
   720 prefer 2
       
   721 apply(auto)[1]
       
   722 apply(rule_tac B="\<Union>sp \<in> Suffixes b. {(v1, v2). v1 \<in> NValues rexp1 b \<and> v2 \<in> NValues rexp2 sp}" in finite_subset)
       
   723 apply(auto)[1]
       
   724 apply (metis rest_Suffixes)
       
   725 apply(rule finite_UN_I)
       
   726 apply(rule finite_Suffixes)
       
   727 apply(simp)
       
   728 apply(simp add: NValues_recs)
       
   729 apply(clarify)
       
   730 apply(subst NValues_recs)
       
   731 apply(simp)
       
   732 apply(rule_tac f="\<lambda>(v, vs). Stars (v # vs)" and 
       
   733                A="{(v, vs) | v vs. v \<in> NValues rexp b \<and> (flat v \<noteq> [] \<and> Stars vs \<in> NValues (STAR rexp) (rest v b))}" in finite_surj)
       
   734 prefer 2
       
   735 apply(auto)[1]
       
   736 apply(auto)
       
   737 apply(case_tac b)
       
   738 apply(simp)
       
   739 defer
       
   740 apply(rule_tac B="\<Union>sp \<in> SSuffixes b. {(v, vs) | v vs. v \<in> NValues rexp b \<and> Stars vs \<in> NValues (STAR rexp) sp}" in finite_subset)
       
   741 apply(auto)[1]
       
   742 apply(rule_tac x="rest aa (a # list)" in bexI)
       
   743 apply(simp)
       
   744 apply (rule rest_SSuffixes)
       
   745 apply(simp)
       
   746 apply(simp)
       
   747 apply(rule finite_UN_I)
       
   748 defer
       
   749 apply(frule_tac x="rexp" in spec)
       
   750 apply(drule_tac x="b" in spec)
       
   751 apply(drule conjunct1)
       
   752 apply(drule mp)
       
   753 apply(simp)
       
   754 apply(drule_tac x="STAR rexp" in spec)
       
   755 apply(drule_tac x="sp" in spec)
       
   756 apply(drule conjunct2)
       
   757 apply(drule mp)
       
   758 apply(simp)
       
   759 apply(simp add: prefix_def SPrefixes_def SSuffixes_def)
       
   760 apply(auto)[1]
       
   761 apply (metis length_Cons length_rev length_sprefix rev.simps(2))
       
   762 apply(simp)
       
   763 apply(rule finite_cartesian_product)
       
   764 apply(simp)
       
   765 apply(rule_tac f="Stars" in finite_imageD)
       
   766 prefer 2
       
   767 apply(auto simp add: inj_on_def)[1]
       
   768 apply (metis finite_subset image_Collect_subsetI)
       
   769 apply(simp add: rest_Nil)
       
   770 apply(simp add: NValues_STAR_Nil)
       
   771 apply(rule_tac B="{(v, vs). v \<in> NValues rexp [] \<and> vs = []}" in finite_subset)
       
   772 apply(auto)[1]
       
   773 apply(simp)
       
   774 apply(rule_tac B="Suffixes b" in finite_subset)
       
   775 apply(auto simp add: SSuffixes_def Suffixes_def Prefixes_def SPrefixes_def sprefix_def)[1]
       
   776 by (metis finite_Suffixes)
       
   777 
       
   778 lemma NValues_finite:
       
   779   "finite (NValues r s)"
       
   780 using NValues_finite_aux
       
   781 apply(simp)
       
   782 done
       
   783 
   388 
   784 section {* Sulzmann functions *}
   389 section {* Sulzmann functions *}
   785 
   390 
   786 fun 
   391 fun 
   787   mkeps :: "rexp \<Rightarrow> val"
   392   mkeps :: "rexp \<Rightarrow> val"
   936 apply(simp_all)[7]
   541 apply(simp_all)[7]
   937 apply(auto)
   542 apply(auto)
   938 apply (metis Prf.intros(6) Prf.intros(7))
   543 apply (metis Prf.intros(6) Prf.intros(7))
   939 by (metis Prf.intros(7))
   544 by (metis Prf.intros(7))
   940 
   545 
   941 lemma v3_proj:
   546 
   942   assumes "\<Turnstile> v : r" and "\<exists>s. (flat v) = c # s"
       
   943   shows "\<Turnstile> (projval r c v) : der c r"
       
   944 using assms
       
   945 apply(induct rule: NPrf.induct)
       
   946 prefer 4
       
   947 apply(simp)
       
   948 prefer 4
       
   949 apply(simp)
       
   950 apply (metis NPrf.intros(4))
       
   951 prefer 2
       
   952 apply(simp)
       
   953 apply (metis NPrf.intros(2))
       
   954 prefer 2
       
   955 apply(simp)
       
   956 apply (metis NPrf.intros(3))
       
   957 apply(auto)
       
   958 apply(rule NPrf.intros)
       
   959 apply(simp)
       
   960 apply (metis NPrf_imp_Prf not_nullable_flat)
       
   961 apply(rule NPrf.intros)
       
   962 apply(rule NPrf.intros)
       
   963 apply (metis Cons_eq_append_conv)
       
   964 apply(simp)
       
   965 apply(rule NPrf.intros)
       
   966 apply (metis Cons_eq_append_conv)
       
   967 apply(simp)
       
   968 (* Stars case *)
       
   969 apply(rule NPrf.intros)
       
   970 apply (metis Cons_eq_append_conv)
       
   971 apply(auto)
       
   972 done
       
   973 
   547 
   974 lemma v4:
   548 lemma v4:
   975   assumes "\<turnstile> v : der c r" 
   549   assumes "\<turnstile> v : der c r" 
   976   shows "flat (injval r c v) = c # (flat v)"
   550   shows "flat (injval r c v) = c # (flat v)"
   977 using assms
   551 using assms
  1016 apply(rotate_tac 2)
   590 apply(rotate_tac 2)
  1017 apply(erule Prf.cases)
   591 apply(erule Prf.cases)
  1018 apply(simp_all)[7]
   592 apply(simp_all)[7]
  1019 done
   593 done
  1020 
   594 
  1021 lemma v4_proj:
       
  1022   assumes "\<Turnstile> v : r" and "\<exists>s. (flat v) = c # s"
       
  1023   shows "c # flat (projval r c v) = flat v"
       
  1024 using assms
       
  1025 apply(induct rule: NPrf.induct)
       
  1026 prefer 4
       
  1027 apply(simp)
       
  1028 prefer 4
       
  1029 apply(simp)
       
  1030 prefer 2
       
  1031 apply(simp)
       
  1032 prefer 2
       
  1033 apply(simp)
       
  1034 apply(auto)
       
  1035 apply (metis Cons_eq_append_conv)
       
  1036 apply(simp add: append_eq_Cons_conv)
       
  1037 apply(auto)
       
  1038 done
       
  1039 
       
  1040 lemma v4_proj2:
       
  1041   assumes "\<Turnstile> v : r" and "(flat v) = c # s"
       
  1042   shows "flat (projval r c v) = s"
       
  1043 using assms
       
  1044 by (metis list.inject v4_proj)
       
  1045 
   595 
  1046 
   596 
  1047 section {* Our Alternative Posix definition *}
   597 section {* Our Alternative Posix definition *}
  1048 
   598 
  1049 inductive 
   599 inductive 
  1191 apply(erule PMatch.cases)
   741 apply(erule PMatch.cases)
  1192 apply(simp_all)[7]
   742 apply(simp_all)[7]
  1193 apply(clarify)
   743 apply(clarify)
  1194 by (metis PMatch1(2))
   744 by (metis PMatch1(2))
  1195 
   745 
  1196 lemma PMatch1N:
   746 
  1197   assumes "s \<in> r \<rightarrow> v"
       
  1198   shows "\<Turnstile> v : r" 
       
  1199 using assms
       
  1200 apply(induct s r v rule: PMatch.induct)
       
  1201 apply(auto)
       
  1202 apply (metis NPrf.intros(4))
       
  1203 apply (metis NPrf.intros(5))
       
  1204 apply (metis NPrf.intros(2))
       
  1205 apply (metis NPrf.intros(3))
       
  1206 apply (metis NPrf.intros(1))
       
  1207 apply(rule NPrf.intros)
       
  1208 apply(simp)
       
  1209 apply(simp)
       
  1210 apply(simp)
       
  1211 apply(rule NPrf.intros)
       
  1212 done
       
  1213 
   747 
  1214 lemma PMatch_Values:
   748 lemma PMatch_Values:
  1215   assumes "s \<in> r \<rightarrow> v"
   749   assumes "s \<in> r \<rightarrow> v"
  1216   shows "v \<in> Values r s"
   750   shows "v \<in> Values r s"
  1217 using assms
   751 using assms
  1218 apply(simp add: Values_def PMatch1)
   752 apply(simp add: Values_def PMatch1)
  1219 by (metis append_Nil2 prefix_def)
   753 by (metis append_Nil2 prefix_def)
  1220 
   754 
  1221 lemma PMatch2:
   755 (* a proof that does not need proj *)
       
   756 lemma PMatch2_roy_version:
  1222   assumes "s \<in> (der c r) \<rightarrow> v"
   757   assumes "s \<in> (der c r) \<rightarrow> v"
  1223   shows "(c#s) \<in> r \<rightarrow> (injval r c v)"
   758   shows "(c#s) \<in> r \<rightarrow> (injval r c v)"
  1224 using assms
   759 using assms
  1225 apply(induct c r arbitrary: s v rule: der.induct)
   760 apply(induct r arbitrary: s v c rule: rexp.induct)
  1226 apply(auto)
   761 apply(auto)
  1227 (* NULL case *)
   762 (* NULL case *)
  1228 apply(erule PMatch.cases)
   763 apply(erule PMatch.cases)
  1229 apply(simp_all)[7]
   764 apply(simp_all)[7]
  1230 (* EMPTY case *)
   765 (* EMPTY case *)
  1231 apply(erule PMatch.cases)
   766 apply(erule PMatch.cases)
  1232 apply(simp_all)[7]
   767 apply(simp_all)[7]
  1233 (* CHAR case *)
   768 (* CHAR case *)
  1234 apply(case_tac "c = c'")
   769 apply(case_tac "c = char")
  1235 apply(simp)
   770 apply(simp)
  1236 apply(erule PMatch.cases)
   771 apply(erule PMatch.cases)
  1237 apply(simp_all)[7]
   772 apply(simp_all)[7]
  1238 apply (metis PMatch.intros(2))
   773 apply (metis PMatch.intros(2))
  1239 apply(simp)
   774 apply(simp)
  1240 apply(erule PMatch.cases)
   775 apply(erule PMatch.cases)
  1241 apply(simp_all)[7]
   776 apply(simp_all)[7]
  1242 (* ALT case *)
   777 (* ALT case *)
       
   778 prefer 2
  1243 apply(erule PMatch.cases)
   779 apply(erule PMatch.cases)
  1244 apply(simp_all)[7]
   780 apply(simp_all)[7]
  1245 apply (metis PMatch.intros(3))
   781 apply (metis PMatch.intros(3))
  1246 apply(clarify)
   782 apply(clarify)
  1247 apply(rule PMatch.intros)
   783 apply(rule PMatch.intros)
  1248 apply metis
   784 apply metis
  1249 apply(simp add: der_correctness Der_def)
   785 apply(simp add: der_correctness Der_def)
  1250 (* SEQ case *)
   786 (* SEQ case *)
  1251 apply(case_tac "nullable r1")
   787 apply(case_tac "nullable rexp1")
  1252 apply(simp)
   788 apply(simp)
  1253 prefer 2
   789 prefer 2
  1254 (* not-nullbale case *)
   790 (* not-nullbale case *)
  1255 apply(simp)
   791 apply(simp)
  1256 apply(erule PMatch.cases)
   792 apply(erule PMatch.cases)
  1285 apply(auto)[1]
   821 apply(auto)[1]
  1286 apply(simp add: der_correctness Der_def)
   822 apply(simp add: der_correctness Der_def)
  1287 apply metis
   823 apply metis
  1288 (* right interesting case *)
   824 (* right interesting case *)
  1289 apply(clarify)
   825 apply(clarify)
  1290 apply(simp)
       
  1291 apply(subst (asm) L.simps(4)[symmetric])
       
  1292 apply(simp only: L_flat_Prf)
       
  1293 apply(simp)
       
  1294 apply(subst append.simps(1)[symmetric])
   826 apply(subst append.simps(1)[symmetric])
  1295 apply(rule PMatch.intros)
   827 apply(rule PMatch.intros)
  1296 apply (metis PMatch_mkeps)
   828 apply (metis PMatch_mkeps)
  1297 apply metis
   829 apply metis
  1298 apply(auto)
   830 apply(rule notI)
  1299 apply(simp only: L_flat_NPrf)
   831 apply(clarify)
  1300 apply(simp)
   832 apply(simp)
  1301 apply(auto)
   833 apply(simp add: der_correctness)
  1302 apply(drule_tac x="Seq (projval r1 c v) vb" in spec)
   834 apply(simp only: Der_def Sequ_def)
  1303 apply(drule mp)
       
  1304 apply(simp)
       
  1305 apply (metis append_Cons butlast_snoc list.sel(1) neq_Nil_conv rotate1.simps(2) v4_proj2)
       
  1306 apply(subgoal_tac "\<turnstile> projval r1 c v : der c r1")
       
  1307 apply (metis NPrf_imp_Prf Prf.intros(1))
       
  1308 apply(rule NPrf_imp_Prf)
       
  1309 apply(rule v3_proj)
       
  1310 apply(simp)
   835 apply(simp)
  1311 apply (metis Cons_eq_append_conv)
   836 apply (metis Cons_eq_append_conv)
  1312 (* Stars case *)
   837 (* Stars case *)
  1313 apply(erule PMatch.cases)
   838 apply(erule PMatch.cases)
  1314 apply(simp_all)[7]
   839 apply(simp_all)[7]
  1325 apply(simp)
   850 apply(simp)
  1326 apply(simp)
   851 apply(simp)
  1327 apply(simp)
   852 apply(simp)
  1328 apply (metis L.simps(6))
   853 apply (metis L.simps(6))
  1329 apply(subst v4)
   854 apply(subst v4)
  1330 apply (metis NPrf_imp_Prf PMatch1N)
   855 apply (metis PMatch1)
  1331 apply(simp)
   856 apply(simp)
  1332 apply(auto)[1]
   857 apply(auto)[1]
  1333 apply(drule_tac x="s\<^sub>3" in spec)
   858 apply(drule_tac x="s\<^sub>3" in spec)
  1334 apply(drule mp)
   859 apply(drule mp)
  1335 defer
   860 defer
  1336 apply metis
   861 apply metis
  1337 apply(clarify)
   862 apply(clarify)
  1338 apply(drule_tac x="s1" in meta_spec)
   863 apply(drule_tac x="s1" in meta_spec)
  1339 apply(drule_tac x="v1" in meta_spec)
   864 apply(drule_tac x="v1" in meta_spec)
       
   865 apply(drule_tac x="c" in meta_spec)
  1340 apply(simp)
   866 apply(simp)
  1341 apply(rotate_tac 2)
   867 apply(rotate_tac 2)
  1342 apply(drule PMatch.intros(6))
   868 apply(drule PMatch.intros(6))
  1343 apply(rule PMatch.intros(7))
   869 apply(rule PMatch.intros(7))
       
   870 (* HERE *)
  1344 apply (metis PMatch1(1) list.distinct(1) v4)
   871 apply (metis PMatch1(1) list.distinct(1) v4)
  1345 apply (metis Nil_is_append_conv)
   872 apply (metis Nil_is_append_conv)
  1346 apply(simp)
   873 apply(simp)
  1347 apply(subst der_correctness)
   874 apply(subst der_correctness)
  1348 apply(simp add: Der_def)
   875 apply(simp add: Der_def)
  1349 done
   876 done 
       
   877 
  1350 
   878 
  1351 lemma lex_correct1:
   879 lemma lex_correct1:
  1352   assumes "s \<notin> L r"
   880   assumes "s \<notin> L r"
  1353   shows "lex r s = None"
   881   shows "lex r s = None"
  1354 using assms
   882 using assms
  1370 apply(induct s arbitrary: r)
   898 apply(induct s arbitrary: r)
  1371 apply(simp)
   899 apply(simp)
  1372 apply (metis mkeps_flat mkeps_nullable nullable_correctness)
   900 apply (metis mkeps_flat mkeps_nullable nullable_correctness)
  1373 apply(drule_tac x="der a r" in meta_spec)
   901 apply(drule_tac x="der a r" in meta_spec)
  1374 apply(drule meta_mp)
   902 apply(drule meta_mp)
  1375 apply(simp add: L_flat_NPrf)
   903 apply(simp add: der_correctness Der_def)
  1376 apply(auto)
   904 apply(auto)
  1377 apply (metis v3_proj v4_proj2)
       
  1378 apply (metis v3)
   905 apply (metis v3)
  1379 apply(rule v4)
   906 apply(rule v4)
  1380 by metis
   907 by simp
  1381 
   908 
  1382 lemma lex_correct3:
   909 lemma lex_correct3:
  1383   assumes "s \<in> L r"
   910   assumes "s \<in> L r"
  1384   shows "\<exists>v. lex r s = Some(v) \<and> s \<in> r \<rightarrow> v"
   911   shows "\<exists>v. lex r s = Some(v) \<and> s \<in> r \<rightarrow> v"
  1385 using assms
   912 using assms
  1386 apply(induct s arbitrary: r)
   913 apply(induct s arbitrary: r)
  1387 apply(simp)
   914 apply(simp)
  1388 apply (metis PMatch_mkeps nullable_correctness)
   915 apply (metis PMatch_mkeps nullable_correctness)
  1389 apply(drule_tac x="der a r" in meta_spec)
   916 apply(drule_tac x="der a r" in meta_spec)
  1390 apply(drule meta_mp)
   917 apply(drule meta_mp)
  1391 apply(simp add: L_flat_NPrf)
   918 apply(simp add: der_correctness Der_def)
  1392 apply(auto)
   919 apply(auto)
  1393 apply (metis v3_proj v4_proj2)
   920 by (metis PMatch2_roy_version)
  1394 apply(rule PMatch2)
       
  1395 apply(simp)
       
  1396 done
       
  1397 
       
  1398 lemma lex_correct4:
       
  1399   assumes "s \<in> L r"
       
  1400   shows "\<exists>v. lex r s = Some(v) \<and> \<Turnstile> v : r \<and> flat v = s"
       
  1401 using lex_correct3[OF assms]
       
  1402 apply(auto)
       
  1403 apply (metis PMatch1N)
       
  1404 by (metis PMatch1(2))
       
  1405 
       
  1406 
   921 
  1407 lemma lex_correct5:
   922 lemma lex_correct5:
  1408   assumes "s \<in> L r"
   923   assumes "s \<in> L r"
  1409   shows "s \<in> r \<rightarrow> (lex2 r s)"
   924   shows "s \<in> r \<rightarrow> (lex2 r s)"
  1410 using assms
   925 using assms
  1411 apply(induct s arbitrary: r)
   926 apply(induct s arbitrary: r)
  1412 apply(simp)
   927 apply(simp)
  1413 apply (metis PMatch_mkeps nullable_correctness)
   928 apply (metis PMatch_mkeps nullable_correctness)
  1414 apply(simp)
   929 apply(simp)
  1415 apply(rule PMatch2)
   930 apply(rule PMatch2_roy_version)
  1416 apply(drule_tac x="der a r" in meta_spec)
   931 apply(drule_tac x="der a r" in meta_spec)
  1417 apply(drule meta_mp)
   932 apply(drule meta_mp)
  1418 apply(simp add: L_flat_NPrf)
   933 apply(simp add: der_correctness Der_def)
  1419 apply(auto)
   934 apply(auto)
  1420 apply (metis v3_proj v4_proj2)
       
  1421 done
   935 done
  1422 
   936 
  1423 lemma 
   937 lemma 
  1424   "lex2 (ALT (CHAR a) (ALT (CHAR b) (SEQ (CHAR a) (CHAR b)))) [a,b] = Right (Right (Seq (Char a) (Char b)))"
   938   "lex2 (ALT (CHAR a) (ALT (CHAR b) (SEQ (CHAR a) (CHAR b)))) [a,b] = Right (Right (Seq (Char a) (Char b)))"
  1425 apply(simp)
   939 apply(simp)
  1426 done
   940 done
  1427 
   941 
  1428 section {* Sulzmann's Ordering of values *}
   942 
       
   943 
       
   944 section {* Connection with Sulzmann's Ordering of values *}
  1429 
   945 
  1430 inductive ValOrd :: "val \<Rightarrow> rexp \<Rightarrow> val \<Rightarrow> bool" ("_ \<succ>_ _" [100, 100, 100] 100)
   946 inductive ValOrd :: "val \<Rightarrow> rexp \<Rightarrow> val \<Rightarrow> bool" ("_ \<succ>_ _" [100, 100, 100] 100)
  1431 where
   947 where
  1432   "v2 \<succ>r2 v2' \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1 v2')" 
   948   "v2 \<succ>r2 v2' \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1 v2')" 
  1433 | "\<lbrakk>v1 \<succ>r1 v1'; v1 \<noteq> v1'\<rbrakk> \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1' v2')" 
   949 | "\<lbrakk>v1 \<succ>r1 v1'; v1 \<noteq> v1'\<rbrakk> \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1' v2')" 
  1442 | "\<lbrakk>v1 \<succ>r v2; v1 \<noteq> v2\<rbrakk> \<Longrightarrow> (Stars (v1 # vs1)) \<succ>(STAR r) (Stars (v2 # vs2))"
   958 | "\<lbrakk>v1 \<succ>r v2; v1 \<noteq> v2\<rbrakk> \<Longrightarrow> (Stars (v1 # vs1)) \<succ>(STAR r) (Stars (v2 # vs2))"
  1443 | "(Stars vs1) \<succ>(STAR r) (Stars vs2) \<Longrightarrow> (Stars (v # vs1)) \<succ>(STAR r) (Stars (v # vs2))"
   959 | "(Stars vs1) \<succ>(STAR r) (Stars vs2) \<Longrightarrow> (Stars (v # vs1)) \<succ>(STAR r) (Stars (v # vs2))"
  1444 | "(Stars []) \<succ>(STAR r) (Stars [])"
   960 | "(Stars []) \<succ>(STAR r) (Stars [])"
  1445 
   961 
  1446 
   962 
       
   963 (* non-problematic values...needed in order to fix the *) 
       
   964 (* proj lemma in Sulzmann & lu *)
       
   965 
       
   966 inductive 
       
   967   NPrf :: "val \<Rightarrow> rexp \<Rightarrow> bool" ("\<Turnstile> _ : _" [100, 100] 100)
       
   968 where
       
   969  "\<lbrakk>\<Turnstile> v1 : r1; \<Turnstile> v2 : r2\<rbrakk> \<Longrightarrow> \<Turnstile> Seq v1 v2 : SEQ r1 r2"
       
   970 | "\<Turnstile> v1 : r1 \<Longrightarrow> \<Turnstile> Left v1 : ALT r1 r2"
       
   971 | "\<Turnstile> v2 : r2 \<Longrightarrow> \<Turnstile> Right v2 : ALT r1 r2"
       
   972 | "\<Turnstile> Void : EMPTY"
       
   973 | "\<Turnstile> Char c : CHAR c"
       
   974 | "\<Turnstile> Stars [] : STAR r"
       
   975 | "\<lbrakk>\<Turnstile> v : r; \<Turnstile> Stars vs : STAR r; flat v \<noteq> []\<rbrakk> \<Longrightarrow> \<Turnstile> Stars (v # vs) : STAR r"
       
   976 
       
   977 lemma NPrf_imp_Prf:
       
   978   assumes "\<Turnstile> v : r" 
       
   979   shows "\<turnstile> v : r"
       
   980 using assms
       
   981 apply(induct)
       
   982 apply(auto intro: Prf.intros)
       
   983 done
       
   984 
       
   985 lemma Star_valN:
       
   986   assumes "\<forall>s\<in>set ss. \<exists>v. s = flat v \<and> \<Turnstile> v : r"
       
   987   shows "\<exists>vs. concat (map flat vs) = concat ss \<and> (\<forall>v\<in>set vs. \<Turnstile> v : r)"
       
   988 using assms
       
   989 apply(induct ss)
       
   990 apply(auto)
       
   991 apply (metis empty_iff list.set(1))
       
   992 by (metis concat.simps(2) list.simps(9) set_ConsD)
       
   993 
       
   994 lemma NPrf_Prf_val:
       
   995   shows "\<turnstile> v : r \<Longrightarrow> \<exists>v'. flat v' = flat v \<and> \<Turnstile> v' : r"
       
   996   and   "\<turnstile> Stars vs : r \<Longrightarrow> \<exists>vs'. flat (Stars vs') = flat (Stars vs) \<and> \<Turnstile> Stars vs' : r"
       
   997 using assms
       
   998 apply(induct v and vs arbitrary: r and r rule: val.inducts)
       
   999 apply(auto)[1]
       
  1000 apply(erule Prf.cases)
       
  1001 apply(simp_all)[7]
       
  1002 apply(rule_tac x="Void" in exI)
       
  1003 apply(simp)
       
  1004 apply(rule NPrf.intros)
       
  1005 apply(erule Prf.cases)
       
  1006 apply(simp_all)[7]
       
  1007 apply(rule_tac x="Char c" in exI)
       
  1008 apply(simp)
       
  1009 apply(rule NPrf.intros)
       
  1010 apply(erule Prf.cases)
       
  1011 apply(simp_all)[7]
       
  1012 apply(auto)[1]
       
  1013 apply(drule_tac x="r1" in meta_spec)
       
  1014 apply(drule_tac x="r2" in meta_spec)
       
  1015 apply(simp)
       
  1016 apply(auto)[1]
       
  1017 apply(rule_tac x="Seq v' v'a" in exI)
       
  1018 apply(simp)
       
  1019 apply (metis NPrf.intros(1))
       
  1020 apply(erule Prf.cases)
       
  1021 apply(simp_all)[7]
       
  1022 apply(clarify)
       
  1023 apply(drule_tac x="r2" in meta_spec)
       
  1024 apply(simp)
       
  1025 apply(auto)[1]
       
  1026 apply(rule_tac x="Right v'" in exI)
       
  1027 apply(simp)
       
  1028 apply (metis NPrf.intros)
       
  1029 apply(erule Prf.cases)
       
  1030 apply(simp_all)[7]
       
  1031 apply(clarify)
       
  1032 apply(drule_tac x="r1" in meta_spec)
       
  1033 apply(simp)
       
  1034 apply(auto)[1]
       
  1035 apply(rule_tac x="Left v'" in exI)
       
  1036 apply(simp)
       
  1037 apply (metis NPrf.intros)
       
  1038 apply(drule_tac x="r" in meta_spec)
       
  1039 apply(simp)
       
  1040 apply(auto)[1]
       
  1041 apply(rule_tac x="Stars vs'" in exI)
       
  1042 apply(simp)
       
  1043 apply(rule_tac x="[]" in exI)
       
  1044 apply(simp)
       
  1045 apply(erule Prf.cases)
       
  1046 apply(simp_all)[7]
       
  1047 apply (metis NPrf.intros(6))
       
  1048 apply(erule Prf.cases)
       
  1049 apply(simp_all)[7]
       
  1050 apply(auto)[1]
       
  1051 apply(drule_tac x="ra" in meta_spec)
       
  1052 apply(simp)
       
  1053 apply(drule_tac x="STAR ra" in meta_spec)
       
  1054 apply(simp)
       
  1055 apply(auto)
       
  1056 apply(case_tac "flat v = []")
       
  1057 apply(rule_tac x="vs'" in exI)
       
  1058 apply(simp)
       
  1059 apply(rule_tac x="v' # vs'" in exI)
       
  1060 apply(simp)
       
  1061 apply(rule NPrf.intros)
       
  1062 apply(auto)
       
  1063 done
       
  1064 
       
  1065 lemma NPrf_Prf:
       
  1066   shows "{flat v | v. \<turnstile> v : r} = {flat v | v. \<Turnstile> v : r}"
       
  1067 apply(auto)
       
  1068 apply (metis NPrf_Prf_val(1))
       
  1069 by (metis NPrf_imp_Prf)
       
  1070 
       
  1071 lemma NPrf_flat_L:
       
  1072   assumes "\<Turnstile> v : r" shows "flat v \<in> L r"
       
  1073 using assms
       
  1074 by (metis NPrf_imp_Prf Prf_flat_L)
       
  1075 
       
  1076 
       
  1077 lemma L_flat_NPrf:
       
  1078   "L(r) = {flat v | v. \<Turnstile> v : r}"
       
  1079 by (metis L_flat_Prf NPrf_Prf)
       
  1080 
       
  1081 
       
  1082 
       
  1083 lemma v3_proj:
       
  1084   assumes "\<Turnstile> v : r" and "\<exists>s. (flat v) = c # s"
       
  1085   shows "\<Turnstile> (projval r c v) : der c r"
       
  1086 using assms
       
  1087 apply(induct rule: NPrf.induct)
       
  1088 prefer 4
       
  1089 apply(simp)
       
  1090 prefer 4
       
  1091 apply(simp)
       
  1092 apply (metis NPrf.intros(4))
       
  1093 prefer 2
       
  1094 apply(simp)
       
  1095 apply (metis NPrf.intros(2))
       
  1096 prefer 2
       
  1097 apply(simp)
       
  1098 apply (metis NPrf.intros(3))
       
  1099 apply(auto)
       
  1100 apply(rule NPrf.intros)
       
  1101 apply(simp)
       
  1102 apply (metis NPrf_imp_Prf not_nullable_flat)
       
  1103 apply(rule NPrf.intros)
       
  1104 apply(rule NPrf.intros)
       
  1105 apply (metis Cons_eq_append_conv)
       
  1106 apply(simp)
       
  1107 apply(rule NPrf.intros)
       
  1108 apply (metis Cons_eq_append_conv)
       
  1109 apply(simp)
       
  1110 (* Stars case *)
       
  1111 apply(rule NPrf.intros)
       
  1112 apply (metis Cons_eq_append_conv)
       
  1113 apply(auto)
       
  1114 done
       
  1115 
       
  1116 lemma v4_proj:
       
  1117   assumes "\<Turnstile> v : r" and "\<exists>s. (flat v) = c # s"
       
  1118   shows "c # flat (projval r c v) = flat v"
       
  1119 using assms
       
  1120 apply(induct rule: NPrf.induct)
       
  1121 prefer 4
       
  1122 apply(simp)
       
  1123 prefer 4
       
  1124 apply(simp)
       
  1125 prefer 2
       
  1126 apply(simp)
       
  1127 prefer 2
       
  1128 apply(simp)
       
  1129 apply(auto)
       
  1130 apply (metis Cons_eq_append_conv)
       
  1131 apply(simp add: append_eq_Cons_conv)
       
  1132 apply(auto)
       
  1133 done
       
  1134 
       
  1135 lemma v4_proj2:
       
  1136   assumes "\<Turnstile> v : r" and "(flat v) = c # s"
       
  1137   shows "flat (projval r c v) = s"
       
  1138 using assms
       
  1139 by (metis list.inject v4_proj)
       
  1140 
       
  1141 lemma PMatch1N:
       
  1142   assumes "s \<in> r \<rightarrow> v"
       
  1143   shows "\<Turnstile> v : r" 
       
  1144 using assms
       
  1145 apply(induct s r v rule: PMatch.induct)
       
  1146 apply(auto)
       
  1147 apply (metis NPrf.intros(4))
       
  1148 apply (metis NPrf.intros(5))
       
  1149 apply (metis NPrf.intros(2))
       
  1150 apply (metis NPrf.intros(3))
       
  1151 apply (metis NPrf.intros(1))
       
  1152 apply(rule NPrf.intros)
       
  1153 apply(simp)
       
  1154 apply(simp)
       
  1155 apply(simp)
       
  1156 apply(rule NPrf.intros)
       
  1157 done
       
  1158 
       
  1159 (* this version needs proj *)
       
  1160 lemma PMatch2:
       
  1161   assumes "s \<in> (der c r) \<rightarrow> v"
       
  1162   shows "(c#s) \<in> r \<rightarrow> (injval r c v)"
       
  1163 using assms
       
  1164 apply(induct c r arbitrary: s v rule: der.induct)
       
  1165 apply(auto)
       
  1166 (* NULL case *)
       
  1167 apply(erule PMatch.cases)
       
  1168 apply(simp_all)[7]
       
  1169 (* EMPTY case *)
       
  1170 apply(erule PMatch.cases)
       
  1171 apply(simp_all)[7]
       
  1172 (* CHAR case *)
       
  1173 apply(case_tac "c = c'")
       
  1174 apply(simp)
       
  1175 apply(erule PMatch.cases)
       
  1176 apply(simp_all)[7]
       
  1177 apply (metis PMatch.intros(2))
       
  1178 apply(simp)
       
  1179 apply(erule PMatch.cases)
       
  1180 apply(simp_all)[7]
       
  1181 (* ALT case *)
       
  1182 apply(erule PMatch.cases)
       
  1183 apply(simp_all)[7]
       
  1184 apply (metis PMatch.intros(3))
       
  1185 apply(clarify)
       
  1186 apply(rule PMatch.intros)
       
  1187 apply metis
       
  1188 apply(simp add: der_correctness Der_def)
       
  1189 (* SEQ case *)
       
  1190 apply(case_tac "nullable r1")
       
  1191 apply(simp)
       
  1192 prefer 2
       
  1193 (* not-nullbale case *)
       
  1194 apply(simp)
       
  1195 apply(erule PMatch.cases)
       
  1196 apply(simp_all)[7]
       
  1197 apply(clarify)
       
  1198 apply(subst append.simps(2)[symmetric])
       
  1199 apply(rule PMatch.intros)
       
  1200 apply metis
       
  1201 apply metis
       
  1202 apply(auto)[1]
       
  1203 apply(simp add: der_correctness Der_def)
       
  1204 apply(auto)[1]
       
  1205 (* nullable case *)
       
  1206 apply(erule PMatch.cases)
       
  1207 apply(simp_all)[7]
       
  1208 (* left case *)
       
  1209 apply(clarify)
       
  1210 apply(erule PMatch.cases)
       
  1211 apply(simp_all)[4]
       
  1212 prefer 2
       
  1213 apply(clarify)
       
  1214 prefer 2
       
  1215 apply(clarify)
       
  1216 apply(clarify)
       
  1217 apply(simp (no_asm))
       
  1218 apply(subst append.simps(2)[symmetric])
       
  1219 apply(rule PMatch.intros)
       
  1220 apply metis
       
  1221 apply metis
       
  1222 apply(erule contrapos_nn)
       
  1223 apply(erule exE)+
       
  1224 apply(auto)[1]
       
  1225 apply(simp add: der_correctness Der_def)
       
  1226 apply metis
       
  1227 (* right interesting case *)
       
  1228 apply(clarify)
       
  1229 apply(simp)
       
  1230 apply(subst (asm) L.simps(4)[symmetric])
       
  1231 apply(simp only: L_flat_Prf)
       
  1232 apply(simp)
       
  1233 apply(subst append.simps(1)[symmetric])
       
  1234 apply(rule PMatch.intros)
       
  1235 apply (metis PMatch_mkeps)
       
  1236 apply metis
       
  1237 apply(auto)
       
  1238 apply(simp only: L_flat_NPrf)
       
  1239 apply(simp)
       
  1240 apply(auto)
       
  1241 apply(drule_tac x="Seq (projval r1 c v) vb" in spec)
       
  1242 apply(drule mp)
       
  1243 apply(simp)
       
  1244 apply (metis append_Cons butlast_snoc list.sel(1) neq_Nil_conv rotate1.simps(2) v4_proj2)
       
  1245 apply(subgoal_tac "\<turnstile> projval r1 c v : der c r1")
       
  1246 apply (metis NPrf_imp_Prf Prf.intros(1))
       
  1247 apply(rule NPrf_imp_Prf)
       
  1248 apply(rule v3_proj)
       
  1249 apply(simp)
       
  1250 apply (metis Cons_eq_append_conv)
       
  1251 (* Stars case *)
       
  1252 apply(erule PMatch.cases)
       
  1253 apply(simp_all)[7]
       
  1254 apply(clarify)
       
  1255 apply(rotate_tac 2)
       
  1256 apply(frule_tac PMatch1)
       
  1257 apply(erule PMatch.cases)
       
  1258 apply(simp_all)[7]
       
  1259 apply(subst append.simps(2)[symmetric])
       
  1260 apply(rule PMatch.intros)
       
  1261 apply metis
       
  1262 apply(auto)[1]
       
  1263 apply(rule PMatch.intros)
       
  1264 apply(simp)
       
  1265 apply(simp)
       
  1266 apply(simp)
       
  1267 apply (metis L.simps(6))
       
  1268 apply(subst v4)
       
  1269 apply (metis NPrf_imp_Prf PMatch1N)
       
  1270 apply(simp)
       
  1271 apply(auto)[1]
       
  1272 apply(drule_tac x="s\<^sub>3" in spec)
       
  1273 apply(drule mp)
       
  1274 defer
       
  1275 apply metis
       
  1276 apply(clarify)
       
  1277 apply(drule_tac x="s1" in meta_spec)
       
  1278 apply(drule_tac x="v1" in meta_spec)
       
  1279 apply(simp)
       
  1280 apply(rotate_tac 2)
       
  1281 apply(drule PMatch.intros(6))
       
  1282 apply(rule PMatch.intros(7))
       
  1283 apply (metis PMatch1(1) list.distinct(1) v4)
       
  1284 apply (metis Nil_is_append_conv)
       
  1285 apply(simp)
       
  1286 apply(subst der_correctness)
       
  1287 apply(simp add: Der_def)
       
  1288 done 
       
  1289 
       
  1290 lemma lex_correct4:
       
  1291   assumes "s \<in> L r"
       
  1292   shows "\<exists>v. lex r s = Some(v) \<and> \<Turnstile> v : r \<and> flat v = s"
       
  1293 using lex_correct3[OF assms]
       
  1294 apply(auto)
       
  1295 apply (metis PMatch1N)
       
  1296 by (metis PMatch1(2))
       
  1297 
  1447 
  1298 
  1448 end
  1299 end