thys/ReStar.thy
changeset 89 9613e6ace30f
child 90 3c8cfdf95252
equal deleted inserted replaced
88:532bb9df225d 89:9613e6ace30f
       
     1    
       
     2 theory Re
       
     3   imports "Main" 
       
     4 begin
       
     5 
       
     6 
       
     7 section {* Sequential Composition of Sets *}
       
     8 
       
     9 definition
       
    10   Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
       
    11 where 
       
    12   "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"
       
    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 *}
       
    19 
       
    20 lemma seq_empty [simp]:
       
    21   shows "A ;; {[]} = A"
       
    22   and   "{[]} ;; A = A"
       
    23 by (simp_all add: Sequ_def)
       
    24 
       
    25 lemma seq_null [simp]:
       
    26   shows "A ;; {} = {}"
       
    27   and   "{} ;; A = {}"
       
    28 by (simp_all add: Sequ_def)
       
    29 
       
    30 lemma seq_image:
       
    31   assumes "\<forall>s1 s2. f (s1 @ s2) = (f s1) @ (f s2)"
       
    32   shows "f ` (A ;; B) = (f ` A) ;; (f ` B)"
       
    33 apply(auto simp add: Sequ_def image_def)
       
    34 apply(rule_tac x="f s1" in exI)
       
    35 apply(rule_tac x="f s2" in exI)
       
    36 using assms
       
    37 apply(auto)
       
    38 apply(rule_tac x="xa @ xb" in exI)
       
    39 using assms
       
    40 apply(auto)
       
    41 done
       
    42 
       
    43 section {* Kleene Star for Sets *}
       
    44 
       
    45 inductive_set
       
    46   Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
       
    47   for A :: "string set"
       
    48 where
       
    49   start[intro]: "[] \<in> A\<star>"
       
    50 | step[intro]:  "\<lbrakk>s1 \<in> A; s2 \<in> A\<star>\<rbrakk> \<Longrightarrow> s1 @ s2 \<in> A\<star>"
       
    51 
       
    52 fun 
       
    53   pow :: "string set \<Rightarrow> nat \<Rightarrow> string set" ("_ \<up> _" [100,100] 100)
       
    54 where
       
    55   "A \<up> 0 = {[]}"
       
    56 | "A \<up> (Suc n) = A ;; (A \<up> n)"  
       
    57 
       
    58 lemma star1: 
       
    59   shows "s \<in> A\<star> \<Longrightarrow> \<exists>n. s \<in> A \<up> n"
       
    60   apply(induct rule: Star.induct)
       
    61   apply (metis Re.pow.simps(1) insertI1)
       
    62   apply(auto)
       
    63   apply(rule_tac x="Suc n" in exI)
       
    64   apply(auto simp add: Sequ_def)
       
    65   done
       
    66 
       
    67 lemma star2:
       
    68   shows "s \<in> A \<up> n \<Longrightarrow> s \<in> A\<star>"
       
    69   apply(induct n arbitrary: s)
       
    70   apply (metis Re.pow.simps(1) Star.simps empty_iff insertE)
       
    71   apply(auto simp add: Sequ_def)
       
    72   done
       
    73 
       
    74 lemma star3:
       
    75   shows "A\<star> = (\<Union>i. A \<up> i)"
       
    76 using star1 star2
       
    77 apply(auto)
       
    78 done
       
    79 
       
    80 lemma star4:
       
    81   shows "s \<in> A \<up> n \<Longrightarrow> \<exists>ss. s = concat ss \<and> (\<forall>s' \<in> set ss. s' \<in> A)"
       
    82   apply(induct n arbitrary: s)
       
    83   apply(auto simp add: Sequ_def)
       
    84   apply(rule_tac x="[]" in exI)
       
    85   apply(auto)
       
    86   apply(drule_tac x="s2" in meta_spec)
       
    87   apply(auto)
       
    88 by (metis concat.simps(2) insertE set_simps(2))
       
    89 
       
    90 lemma star5:
       
    91   assumes "f [] = []"
       
    92   assumes "\<forall>s1 s2. f (s1 @ s2) = (f s1) @ (f s2)"
       
    93   shows "(f ` A) \<up> n = f ` (A \<up> n)"
       
    94 apply(induct n)
       
    95 apply(simp add: assms)
       
    96 apply(simp)
       
    97 apply(subst seq_image[OF assms(2)])
       
    98 apply(simp)
       
    99 done
       
   100 
       
   101 lemma star6:
       
   102   assumes "f [] = []"
       
   103   assumes "\<forall>s1 s2. f (s1 @ s2) = (f s1) @ (f s2)"
       
   104   shows "(f ` A)\<star> = f ` (A\<star>)"
       
   105 apply(simp add: star3)
       
   106 apply(simp add: image_UN)
       
   107 apply(subst star5[OF assms])
       
   108 apply(simp)
       
   109 done
       
   110 
       
   111 
       
   112 lemma star_decomp: 
       
   113   assumes a: "c # x \<in> A\<star>" 
       
   114   shows "\<exists>a b. x = a @ b \<and> c # a \<in> A \<and> b \<in> A\<star>"
       
   115 using a
       
   116 by (induct x\<equiv>"c # x" rule: Star.induct) 
       
   117    (auto simp add: append_eq_Cons_conv)
       
   118 
       
   119 section {* Regular Expressions *}
       
   120 
       
   121 datatype rexp =
       
   122   NULL
       
   123 | EMPTY
       
   124 | CHAR char
       
   125 | SEQ rexp rexp
       
   126 | ALT rexp rexp
       
   127 | STAR rexp
       
   128 
       
   129 section {* Semantics of Regular Expressions *}
       
   130  
       
   131 fun
       
   132   L :: "rexp \<Rightarrow> string set"
       
   133 where
       
   134   "L (NULL) = {}"
       
   135 | "L (EMPTY) = {[]}"
       
   136 | "L (CHAR c) = {[c]}"
       
   137 | "L (SEQ r1 r2) = (L r1) ;; (L r2)"
       
   138 | "L (ALT r1 r2) = (L r1) \<union> (L r2)"
       
   139 | "L (STAR r) = (L r)\<star>"
       
   140 
       
   141 fun
       
   142  nullable :: "rexp \<Rightarrow> bool"
       
   143 where
       
   144   "nullable (NULL) = False"
       
   145 | "nullable (EMPTY) = True"
       
   146 | "nullable (CHAR c) = False"
       
   147 | "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
       
   148 | "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"
       
   149 | "nullable (STAR r1) = True"
       
   150 
       
   151 lemma nullable_correctness:
       
   152   shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
       
   153 apply (induct r) 
       
   154 apply(auto simp add: Sequ_def) 
       
   155 done
       
   156 
       
   157 section {* Values *}
       
   158 
       
   159 datatype val = 
       
   160   Void
       
   161 | Char char
       
   162 | Seq val val
       
   163 | Right val
       
   164 | Left val
       
   165 | Star "val list"
       
   166 
       
   167 section {* The string behind a value *}
       
   168 
       
   169 fun 
       
   170   flat :: "val \<Rightarrow> string"
       
   171 where
       
   172   "flat (Void) = []"
       
   173 | "flat (Char c) = [c]"
       
   174 | "flat (Left v) = flat v"
       
   175 | "flat (Right v) = flat v"
       
   176 | "flat (Seq v1 v2) = (flat v1) @ (flat v2)"
       
   177 | "flat (Star []) = []"
       
   178 | "flat (Star (v#vs)) = (flat v) @ (flat (Star vs))" 
       
   179 
       
   180 section {* Relation between values and regular expressions *}
       
   181 
       
   182 inductive 
       
   183   Prf :: "val \<Rightarrow> rexp \<Rightarrow> bool" ("\<turnstile> _ : _" [100, 100] 100)
       
   184 where
       
   185  "\<lbrakk>\<turnstile> v1 : r1; \<turnstile> v2 : r2\<rbrakk> \<Longrightarrow> \<turnstile> Seq v1 v2 : SEQ r1 r2"
       
   186 | "\<turnstile> v1 : r1 \<Longrightarrow> \<turnstile> Left v1 : ALT r1 r2"
       
   187 | "\<turnstile> v2 : r2 \<Longrightarrow> \<turnstile> Right v2 : ALT r1 r2"
       
   188 | "\<turnstile> Void : EMPTY"
       
   189 | "\<turnstile> Char c : CHAR c"
       
   190 | "\<turnstile> Star [] : STAR r"
       
   191 | "\<lbrakk>\<turnstile> v : r; \<turnstile> Star vs : STAR r\<rbrakk> \<Longrightarrow> \<turnstile> Star (v#vs) : STAR r"
       
   192 
       
   193 lemma not_nullable_flat:
       
   194   assumes "\<turnstile> v : r" "\<not>nullable r"
       
   195   shows "flat v \<noteq> []"
       
   196 using assms
       
   197 apply(induct)
       
   198 apply(auto)
       
   199 done
       
   200 
       
   201 lemma Prf_flat_L:
       
   202   assumes "\<turnstile> v : r" shows "flat v \<in> L r"
       
   203 using assms
       
   204 apply(induct v r rule: Prf.induct)
       
   205 apply(auto simp add: Sequ_def)
       
   206 done
       
   207 
       
   208 
       
   209 lemma Prf_Star_flat_L:
       
   210   assumes "\<turnstile> v : STAR r" shows "flat v \<in> (L r)\<star>"
       
   211 using assms
       
   212 apply(induct v r\<equiv>"STAR r" arbitrary: r rule: Prf.induct)
       
   213 apply(auto)
       
   214 apply(simp add: star3)
       
   215 apply(auto)
       
   216 apply(rule_tac x="Suc x" in exI)
       
   217 apply(auto simp add: Sequ_def)
       
   218 apply(rule_tac x="flat v" in exI)
       
   219 apply(rule_tac x="flat (Star vs)" in exI)
       
   220 apply(auto)
       
   221 by (metis Prf_flat_L)
       
   222 
       
   223 lemma L_flat_Prf:
       
   224   "L(r) = {flat v | v. \<turnstile> v : r}"
       
   225 apply(induct r)
       
   226 apply(auto dest: Prf_flat_L simp add: Sequ_def)
       
   227 apply (metis Prf.intros(4) flat.simps(1))
       
   228 apply (metis Prf.intros(5) flat.simps(2))
       
   229 apply (metis Prf.intros(1) flat.simps(5))
       
   230 apply (metis Prf.intros(2) flat.simps(3))
       
   231 apply (metis Prf.intros(3) flat.simps(4))
       
   232 apply(erule Prf.cases)
       
   233 apply(auto)
       
   234 apply(simp add: star3)
       
   235 apply(auto)
       
   236 sorry
       
   237 
       
   238 section {* Greedy Ordering according to Frisch/Cardelli *}
       
   239 
       
   240 inductive 
       
   241   GrOrd :: "val \<Rightarrow> val \<Rightarrow> bool" ("_ gr\<succ> _")
       
   242 where 
       
   243   "v1 gr\<succ> v1' \<Longrightarrow> (Seq v1 v2) gr\<succ> (Seq v1' v2')"
       
   244 | "v2 gr\<succ> v2' \<Longrightarrow> (Seq v1 v2) gr\<succ> (Seq v1 v2')"
       
   245 | "v1 gr\<succ> v2 \<Longrightarrow> (Left v1) gr\<succ> (Left v2)"
       
   246 | "v1 gr\<succ> v2 \<Longrightarrow> (Right v1) gr\<succ> (Right v2)"
       
   247 | "(Left v2) gr\<succ>(Right v1)"
       
   248 | "(Char c) gr\<succ> (Char c)"
       
   249 | "(Void) gr\<succ> (Void)"
       
   250 
       
   251 lemma Gr_refl:
       
   252   assumes "\<turnstile> v : r"
       
   253   shows "v gr\<succ> v"
       
   254 using assms
       
   255 apply(induct)
       
   256 apply(auto intro: GrOrd.intros)
       
   257 done
       
   258 
       
   259 lemma Gr_total:
       
   260   assumes "\<turnstile> v1 : r" "\<turnstile> v2 : r"
       
   261   shows "v1 gr\<succ> v2 \<or> v2 gr\<succ> v1"
       
   262 using assms
       
   263 apply(induct v1 r arbitrary: v2 rule: Prf.induct)
       
   264 apply(rotate_tac 4)
       
   265 apply(erule Prf.cases)
       
   266 apply(simp_all)[5]
       
   267 apply(clarify)
       
   268 apply (metis GrOrd.intros(1) GrOrd.intros(2))
       
   269 apply(rotate_tac 2)
       
   270 apply(erule Prf.cases)
       
   271 apply(simp_all)
       
   272 apply(clarify)
       
   273 apply (metis GrOrd.intros(3))
       
   274 apply(clarify)
       
   275 apply (metis GrOrd.intros(5))
       
   276 apply(rotate_tac 2)
       
   277 apply(erule Prf.cases)
       
   278 apply(simp_all)
       
   279 apply(clarify)
       
   280 apply (metis GrOrd.intros(5))
       
   281 apply(clarify)
       
   282 apply (metis GrOrd.intros(4))
       
   283 apply(erule Prf.cases)
       
   284 apply(simp_all)
       
   285 apply (metis GrOrd.intros(7))
       
   286 apply(erule Prf.cases)
       
   287 apply(simp_all)
       
   288 apply (metis GrOrd.intros(6))
       
   289 done
       
   290 
       
   291 lemma Gr_trans: 
       
   292   assumes "v1 gr\<succ> v2" "v2 gr\<succ> v3" 
       
   293   and     "\<turnstile> v1 : r" "\<turnstile> v2 : r" "\<turnstile> v3 : r"
       
   294   shows "v1 gr\<succ> v3"
       
   295 using assms
       
   296 apply(induct r arbitrary: v1 v2 v3)
       
   297 apply(erule Prf.cases)
       
   298 apply(simp_all)[5]
       
   299 apply(erule Prf.cases)
       
   300 apply(simp_all)[5]
       
   301 apply(erule Prf.cases)
       
   302 apply(simp_all)[5]
       
   303 apply(erule Prf.cases)
       
   304 apply(simp_all)[5]
       
   305 apply(erule Prf.cases)
       
   306 apply(simp_all)[5]
       
   307 defer
       
   308 (* ALT case *)
       
   309 apply(erule Prf.cases)
       
   310 apply(simp_all (no_asm_use))[5]
       
   311 apply(erule Prf.cases)
       
   312 apply(simp_all (no_asm_use))[5]
       
   313 apply(erule Prf.cases)
       
   314 apply(simp_all (no_asm_use))[5]
       
   315 apply(clarify)
       
   316 apply(erule GrOrd.cases)
       
   317 apply(simp_all (no_asm_use))[7]
       
   318 apply(erule GrOrd.cases)
       
   319 apply(simp_all (no_asm_use))[7]
       
   320 apply (metis GrOrd.intros(3))
       
   321 apply(clarify)
       
   322 apply(erule GrOrd.cases)
       
   323 apply(simp_all (no_asm_use))[7]
       
   324 apply(erule GrOrd.cases)
       
   325 apply(simp_all (no_asm_use))[7]
       
   326 apply (metis GrOrd.intros(5))
       
   327 apply(erule Prf.cases)
       
   328 apply(simp_all (no_asm_use))[5]
       
   329 apply(clarify)
       
   330 apply(erule GrOrd.cases)
       
   331 apply(simp_all (no_asm_use))[7]
       
   332 apply(erule GrOrd.cases)
       
   333 apply(simp_all (no_asm_use))[7]
       
   334 apply (metis GrOrd.intros(5))
       
   335 apply(erule Prf.cases)
       
   336 apply(simp_all (no_asm_use))[5]
       
   337 apply(erule Prf.cases)
       
   338 apply(simp_all (no_asm_use))[5]
       
   339 apply(clarify)
       
   340 apply(erule GrOrd.cases)
       
   341 apply(simp_all (no_asm_use))[7]
       
   342 apply(clarify)
       
   343 apply(erule GrOrd.cases)
       
   344 apply(simp_all (no_asm_use))[7]
       
   345 apply(erule Prf.cases)
       
   346 apply(simp_all (no_asm_use))[5]
       
   347 apply(clarify)
       
   348 apply(erule GrOrd.cases)
       
   349 apply(simp_all (no_asm_use))[7]
       
   350 apply(erule GrOrd.cases)
       
   351 apply(simp_all (no_asm_use))[7]
       
   352 apply(clarify)
       
   353 apply(erule GrOrd.cases)
       
   354 apply(simp_all (no_asm_use))[7]
       
   355 apply(erule GrOrd.cases)
       
   356 apply(simp_all (no_asm_use))[7]
       
   357 apply (metis GrOrd.intros(4))
       
   358 (* SEQ case *)
       
   359 apply(erule Prf.cases)
       
   360 apply(simp_all (no_asm_use))[5]
       
   361 apply(erule Prf.cases)
       
   362 apply(simp_all (no_asm_use))[5]
       
   363 apply(erule Prf.cases)
       
   364 apply(simp_all (no_asm_use))[5]
       
   365 apply(clarify)
       
   366 apply(erule GrOrd.cases)
       
   367 apply(simp_all (no_asm_use))[7]
       
   368 apply(erule GrOrd.cases)
       
   369 apply(simp_all (no_asm_use))[7]
       
   370 apply(clarify)
       
   371 apply (metis GrOrd.intros(1))
       
   372 apply (metis GrOrd.intros(1))
       
   373 apply(erule GrOrd.cases)
       
   374 apply(simp_all (no_asm_use))[7]
       
   375 apply (metis GrOrd.intros(1))
       
   376 by (metis GrOrd.intros(1) Gr_refl)
       
   377 
       
   378 
       
   379 section {* Values Sets *}
       
   380 
       
   381 definition prefix :: "string \<Rightarrow> string \<Rightarrow> bool" ("_ \<sqsubseteq> _" [100, 100] 100)
       
   382 where
       
   383   "s1 \<sqsubseteq> s2 \<equiv> \<exists>s3. s1 @ s3 = s2"
       
   384 
       
   385 definition sprefix :: "string \<Rightarrow> string \<Rightarrow> bool" ("_ \<sqsubset> _" [100, 100] 100)
       
   386 where
       
   387   "s1 \<sqsubset> s2 \<equiv> (s1 \<sqsubseteq> s2 \<and> s1 \<noteq> s2)"
       
   388 
       
   389 lemma length_sprefix:
       
   390   "s1 \<sqsubset> s2 \<Longrightarrow> length s1 < length s2"
       
   391 unfolding sprefix_def prefix_def
       
   392 by (auto)
       
   393 
       
   394 definition Prefixes :: "string \<Rightarrow> string set" where
       
   395   "Prefixes s \<equiv> {sp. sp \<sqsubseteq> s}"
       
   396 
       
   397 definition Suffixes :: "string \<Rightarrow> string set" where
       
   398   "Suffixes s \<equiv> rev ` (Prefixes (rev s))"
       
   399 
       
   400 lemma Suffixes_in: 
       
   401   "\<exists>s1. s1 @ s2 = s3 \<Longrightarrow> s2 \<in> Suffixes s3"
       
   402 unfolding Suffixes_def Prefixes_def prefix_def image_def
       
   403 apply(auto)
       
   404 by (metis rev_rev_ident)
       
   405 
       
   406 lemma Prefixes_Cons:
       
   407   "Prefixes (c # s) = {[]} \<union> {c # sp | sp. sp \<in> Prefixes s}"
       
   408 unfolding Prefixes_def prefix_def
       
   409 apply(auto simp add: append_eq_Cons_conv) 
       
   410 done
       
   411 
       
   412 lemma finite_Prefixes:
       
   413   "finite (Prefixes s)"
       
   414 apply(induct s)
       
   415 apply(auto simp add: Prefixes_def prefix_def)[1]
       
   416 apply(simp add: Prefixes_Cons)
       
   417 done
       
   418 
       
   419 lemma finite_Suffixes:
       
   420   "finite (Suffixes s)"
       
   421 unfolding Suffixes_def
       
   422 apply(rule finite_imageI)
       
   423 apply(rule finite_Prefixes)
       
   424 done
       
   425 
       
   426 lemma prefix_Cons:
       
   427   "((c # s1) \<sqsubseteq> (c # s2)) = (s1 \<sqsubseteq> s2)"
       
   428 apply(auto simp add: prefix_def)
       
   429 done
       
   430 
       
   431 lemma prefix_append:
       
   432   "((s @ s1) \<sqsubseteq> (s @ s2)) = (s1 \<sqsubseteq> s2)"
       
   433 apply(induct s)
       
   434 apply(simp)
       
   435 apply(simp add: prefix_Cons)
       
   436 done
       
   437 
       
   438 
       
   439 definition Values :: "rexp \<Rightarrow> string \<Rightarrow> val set" where
       
   440   "Values r s \<equiv> {v. \<turnstile> v : r \<and> flat v \<sqsubseteq> s}"
       
   441 
       
   442 definition rest :: "val \<Rightarrow> string \<Rightarrow> string" where
       
   443   "rest v s \<equiv> drop (length (flat v)) s"
       
   444 
       
   445 lemma rest_Suffixes:
       
   446   "rest v s \<in> Suffixes s"
       
   447 unfolding rest_def
       
   448 by (metis Suffixes_in append_take_drop_id)
       
   449 
       
   450 
       
   451 lemma Values_recs:
       
   452   "Values (NULL) s = {}"
       
   453   "Values (EMPTY) s = {Void}"
       
   454   "Values (CHAR c) s = (if [c] \<sqsubseteq> s then {Char c} else {})" 
       
   455   "Values (ALT r1 r2) s = {Left v | v. v \<in> Values r1 s} \<union> {Right v | v. v \<in> Values r2 s}"
       
   456   "Values (SEQ r1 r2) s = {Seq v1 v2 | v1 v2. v1 \<in> Values r1 s \<and> v2 \<in> Values r2 (rest v1 s)}"
       
   457 unfolding Values_def
       
   458 apply(auto)
       
   459 (*NULL*)
       
   460 apply(erule Prf.cases)
       
   461 apply(simp_all)[5]
       
   462 (*EMPTY*)
       
   463 apply(erule Prf.cases)
       
   464 apply(simp_all)[5]
       
   465 apply(rule Prf.intros)
       
   466 apply (metis append_Nil prefix_def)
       
   467 (*CHAR*)
       
   468 apply(erule Prf.cases)
       
   469 apply(simp_all)[5]
       
   470 apply(rule Prf.intros)
       
   471 apply(erule Prf.cases)
       
   472 apply(simp_all)[5]
       
   473 (*ALT*)
       
   474 apply(erule Prf.cases)
       
   475 apply(simp_all)[5]
       
   476 apply (metis Prf.intros(2))
       
   477 apply (metis Prf.intros(3))
       
   478 (*SEQ*)
       
   479 apply(erule Prf.cases)
       
   480 apply(simp_all)[5]
       
   481 apply (simp add: append_eq_conv_conj prefix_def rest_def)
       
   482 apply (metis Prf.intros(1))
       
   483 apply (simp add: append_eq_conv_conj prefix_def rest_def)
       
   484 done
       
   485 
       
   486 lemma Values_finite:
       
   487   "finite (Values r s)"
       
   488 apply(induct r arbitrary: s)
       
   489 apply(simp_all add: Values_recs)
       
   490 thm finite_surj
       
   491 apply(rule_tac f="\<lambda>(x, y). Seq x y" and 
       
   492                A="{(v1, v2) | v1 v2. v1 \<in> Values r1 s \<and> v2 \<in> Values r2 (rest v1 s)}" in finite_surj)
       
   493 prefer 2
       
   494 apply(auto)[1]
       
   495 apply(rule_tac B="\<Union>sp \<in> Suffixes s. {(v1, v2). v1 \<in> Values r1 s \<and> v2 \<in> Values r2 sp}" in finite_subset)
       
   496 apply(auto)[1]
       
   497 apply (metis rest_Suffixes)
       
   498 apply(rule finite_UN_I)
       
   499 apply(rule finite_Suffixes)
       
   500 apply(simp)
       
   501 done
       
   502 
       
   503 section {* Sulzmann functions *}
       
   504 
       
   505 fun 
       
   506   mkeps :: "rexp \<Rightarrow> val"
       
   507 where
       
   508   "mkeps(EMPTY) = Void"
       
   509 | "mkeps(SEQ r1 r2) = Seq (mkeps r1) (mkeps r2)"
       
   510 | "mkeps(ALT r1 r2) = (if nullable(r1) then Left (mkeps r1) else Right (mkeps r2))"
       
   511 
       
   512 section {* Derivatives *}
       
   513 
       
   514 fun
       
   515  der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
       
   516 where
       
   517   "der c (NULL) = NULL"
       
   518 | "der c (EMPTY) = NULL"
       
   519 | "der c (CHAR c') = (if c = c' then EMPTY else NULL)"
       
   520 | "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
       
   521 | "der c (SEQ r1 r2) = 
       
   522      (if nullable r1
       
   523       then ALT (SEQ (der c r1) r2) (der c r2)
       
   524       else SEQ (der c r1) r2)"
       
   525 
       
   526 fun 
       
   527  ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
       
   528 where
       
   529   "ders [] r = r"
       
   530 | "ders (c # s) r = ders s (der c r)"
       
   531 
       
   532 
       
   533 section {* Injection function *}
       
   534 
       
   535 fun injval :: "rexp \<Rightarrow> char \<Rightarrow> val \<Rightarrow> val"
       
   536 where
       
   537   "injval (EMPTY) c Void = Char c"
       
   538 | "injval (CHAR d) c Void = Char d"
       
   539 | "injval (CHAR d) c (Char c') = Seq (Char d) (Char c')"
       
   540 | "injval (ALT r1 r2) c (Left v1) = Left(injval r1 c v1)"
       
   541 | "injval (ALT r1 r2) c (Right v2) = Right(injval r2 c v2)"
       
   542 | "injval (SEQ r1 r2) c (Char c') = Seq (Char c) (Char c')"
       
   543 | "injval (SEQ r1 r2) c (Seq v1 v2) = Seq (injval r1 c v1) v2"
       
   544 | "injval (SEQ r1 r2) c (Left (Seq v1 v2)) = Seq (injval r1 c v1) v2"
       
   545 | "injval (SEQ r1 r2) c (Right v2) = Seq (mkeps r1) (injval r2 c v2)"
       
   546 
       
   547 fun 
       
   548   lex :: "rexp \<Rightarrow> string \<Rightarrow> val option"
       
   549 where
       
   550   "lex r [] = (if nullable r then Some(mkeps r) else None)"
       
   551 | "lex r (c#s) = (case (lex (der c r) s) of  
       
   552                     None \<Rightarrow> None
       
   553                   | Some(v) \<Rightarrow> Some(injval r c v))"
       
   554 
       
   555 fun 
       
   556   lex2 :: "rexp \<Rightarrow> string \<Rightarrow> val"
       
   557 where
       
   558   "lex2 r [] = mkeps r"
       
   559 | "lex2 r (c#s) = injval r c (lex2 (der c r) s)"
       
   560 
       
   561 
       
   562 section {* Projection function *}
       
   563 
       
   564 fun projval :: "rexp \<Rightarrow> char \<Rightarrow> val \<Rightarrow> val"
       
   565 where
       
   566   "projval (CHAR d) c _ = Void"
       
   567 | "projval (ALT r1 r2) c (Left v1) = Left (projval r1 c v1)"
       
   568 | "projval (ALT r1 r2) c (Right v2) = Right (projval r2 c v2)"
       
   569 | "projval (SEQ r1 r2) c (Seq v1 v2) = 
       
   570      (if flat v1 = [] then Right(projval r2 c v2) 
       
   571       else if nullable r1 then Left (Seq (projval r1 c v1) v2)
       
   572                           else Seq (projval r1 c v1) v2)"
       
   573 
       
   574 
       
   575 
       
   576 lemma mkeps_nullable:
       
   577   assumes "nullable(r)" 
       
   578   shows "\<turnstile> mkeps r : r"
       
   579 using assms
       
   580 apply(induct rule: nullable.induct)
       
   581 apply(auto intro: Prf.intros)
       
   582 done
       
   583 
       
   584 lemma mkeps_flat:
       
   585   assumes "nullable(r)" 
       
   586   shows "flat (mkeps r) = []"
       
   587 using assms
       
   588 apply(induct rule: nullable.induct)
       
   589 apply(auto)
       
   590 done
       
   591 
       
   592 lemma v3:
       
   593   assumes "\<turnstile> v : der c r" 
       
   594   shows "\<turnstile> (injval r c v) : r"
       
   595 using assms
       
   596 apply(induct arbitrary: v rule: der.induct)
       
   597 apply(simp)
       
   598 apply(erule Prf.cases)
       
   599 apply(simp_all)[5]
       
   600 apply(simp)
       
   601 apply(erule Prf.cases)
       
   602 apply(simp_all)[5]
       
   603 apply(case_tac "c = c'")
       
   604 apply(simp)
       
   605 apply(erule Prf.cases)
       
   606 apply(simp_all)[5]
       
   607 apply (metis Prf.intros(5))
       
   608 apply(simp)
       
   609 apply(erule Prf.cases)
       
   610 apply(simp_all)[5]
       
   611 apply(simp)
       
   612 apply(erule Prf.cases)
       
   613 apply(simp_all)[5]
       
   614 apply (metis Prf.intros(2))
       
   615 apply (metis Prf.intros(3))
       
   616 apply(simp)
       
   617 apply(case_tac "nullable r1")
       
   618 apply(simp)
       
   619 apply(erule Prf.cases)
       
   620 apply(simp_all)[5]
       
   621 apply(auto)[1]
       
   622 apply(erule Prf.cases)
       
   623 apply(simp_all)[5]
       
   624 apply(auto)[1]
       
   625 apply (metis Prf.intros(1))
       
   626 apply(auto)[1]
       
   627 apply (metis Prf.intros(1) mkeps_nullable)
       
   628 apply(simp)
       
   629 apply(erule Prf.cases)
       
   630 apply(simp_all)[5]
       
   631 apply(auto)[1]
       
   632 apply(rule Prf.intros)
       
   633 apply(auto)[2]
       
   634 done
       
   635 
       
   636 lemma v3_proj:
       
   637   assumes "\<turnstile> v : r" and "\<exists>s. (flat v) = c # s"
       
   638   shows "\<turnstile> (projval r c v) : der c r"
       
   639 using assms
       
   640 apply(induct rule: Prf.induct)
       
   641 prefer 4
       
   642 apply(simp)
       
   643 prefer 4
       
   644 apply(simp)
       
   645 apply (metis Prf.intros(4))
       
   646 prefer 2
       
   647 apply(simp)
       
   648 apply (metis Prf.intros(2))
       
   649 prefer 2
       
   650 apply(simp)
       
   651 apply (metis Prf.intros(3))
       
   652 apply(auto)
       
   653 apply(rule Prf.intros)
       
   654 apply(simp)
       
   655 apply (metis Prf_flat_L nullable_correctness)
       
   656 apply(rule Prf.intros)
       
   657 apply(rule Prf.intros)
       
   658 apply (metis Cons_eq_append_conv)
       
   659 apply(simp)
       
   660 apply(rule Prf.intros)
       
   661 apply (metis Cons_eq_append_conv)
       
   662 apply(simp)
       
   663 done
       
   664 
       
   665 lemma v4:
       
   666   assumes "\<turnstile> v : der c r" 
       
   667   shows "flat (injval r c v) = c # (flat v)"
       
   668 using assms
       
   669 apply(induct arbitrary: v rule: der.induct)
       
   670 apply(simp)
       
   671 apply(erule Prf.cases)
       
   672 apply(simp_all)[5]
       
   673 apply(simp)
       
   674 apply(erule Prf.cases)
       
   675 apply(simp_all)[5]
       
   676 apply(simp)
       
   677 apply(case_tac "c = c'")
       
   678 apply(simp)
       
   679 apply(auto)[1]
       
   680 apply(erule Prf.cases)
       
   681 apply(simp_all)[5]
       
   682 apply(simp)
       
   683 apply(erule Prf.cases)
       
   684 apply(simp_all)[5]
       
   685 apply(simp)
       
   686 apply(erule Prf.cases)
       
   687 apply(simp_all)[5]
       
   688 apply(simp)
       
   689 apply(case_tac "nullable r1")
       
   690 apply(simp)
       
   691 apply(erule Prf.cases)
       
   692 apply(simp_all (no_asm_use))[5]
       
   693 apply(auto)[1]
       
   694 apply(erule Prf.cases)
       
   695 apply(simp_all)[5]
       
   696 apply(clarify)
       
   697 apply(simp only: injval.simps flat.simps)
       
   698 apply(auto)[1]
       
   699 apply (metis mkeps_flat)
       
   700 apply(simp)
       
   701 apply(erule Prf.cases)
       
   702 apply(simp_all)[5]
       
   703 done
       
   704 
       
   705 lemma v4_proj:
       
   706   assumes "\<turnstile> v : r" and "\<exists>s. (flat v) = c # s"
       
   707   shows "c # flat (projval r c v) = flat v"
       
   708 using assms
       
   709 apply(induct rule: Prf.induct)
       
   710 prefer 4
       
   711 apply(simp)
       
   712 prefer 4
       
   713 apply(simp)
       
   714 prefer 2
       
   715 apply(simp)
       
   716 prefer 2
       
   717 apply(simp)
       
   718 apply(auto)
       
   719 by (metis Cons_eq_append_conv)
       
   720 
       
   721 lemma v4_proj2:
       
   722   assumes "\<turnstile> v : r" and "(flat v) = c # s"
       
   723   shows "flat (projval r c v) = s"
       
   724 using assms
       
   725 by (metis list.inject v4_proj)
       
   726 
       
   727 
       
   728 section {* Alternative Posix definition *}
       
   729 
       
   730 inductive 
       
   731   PMatch :: "string \<Rightarrow> rexp \<Rightarrow> val \<Rightarrow> bool" ("_ \<in> _ \<rightarrow> _" [100, 100, 100] 100)
       
   732 where
       
   733   "[] \<in> EMPTY \<rightarrow> Void"
       
   734 | "[c] \<in> (CHAR c) \<rightarrow> (Char c)"
       
   735 | "s \<in> r1 \<rightarrow> v \<Longrightarrow> s \<in> (ALT r1 r2) \<rightarrow> (Left v)"
       
   736 | "\<lbrakk>s \<in> r2 \<rightarrow> v; s \<notin> L(r1)\<rbrakk> \<Longrightarrow> s \<in> (ALT r1 r2) \<rightarrow> (Right v)"
       
   737 | "\<lbrakk>s1 \<in> r1 \<rightarrow> v1; s2 \<in> r2 \<rightarrow> v2;
       
   738     \<not>(\<exists>s3 s4. s3 \<noteq> [] \<and> s3 @ s4 = s2 \<and> (s1 @ s3) \<in> L r1 \<and> s4 \<in> L r2)\<rbrakk> \<Longrightarrow> 
       
   739     (s1 @ s2) \<in> (SEQ r1 r2) \<rightarrow> (Seq v1 v2)"
       
   740 
       
   741 
       
   742 lemma PMatch_mkeps:
       
   743   assumes "nullable r"
       
   744   shows "[] \<in> r \<rightarrow> mkeps r"
       
   745 using assms
       
   746 apply(induct r)
       
   747 apply(auto)
       
   748 apply (metis PMatch.intros(1))
       
   749 apply(subst append.simps(1)[symmetric])
       
   750 apply (rule PMatch.intros)
       
   751 apply(simp)
       
   752 apply(simp)
       
   753 apply(auto)[1]
       
   754 apply (rule PMatch.intros)
       
   755 apply(simp)
       
   756 apply (rule PMatch.intros)
       
   757 apply(simp)
       
   758 apply (rule PMatch.intros)
       
   759 apply(simp)
       
   760 by (metis nullable_correctness)
       
   761 
       
   762 
       
   763 lemma PMatch1:
       
   764   assumes "s \<in> r \<rightarrow> v"
       
   765   shows "\<turnstile> v : r" "flat v = s"
       
   766 using assms
       
   767 apply(induct s r v rule: PMatch.induct)
       
   768 apply(auto)
       
   769 apply (metis Prf.intros(4))
       
   770 apply (metis Prf.intros(5))
       
   771 apply (metis Prf.intros(2))
       
   772 apply (metis Prf.intros(3))
       
   773 apply (metis Prf.intros(1))
       
   774 done
       
   775 
       
   776 lemma PMatch_Values:
       
   777   assumes "s \<in> r \<rightarrow> v"
       
   778   shows "v \<in> Values r s"
       
   779 using assms
       
   780 apply(simp add: Values_def PMatch1)
       
   781 by (metis append_Nil2 prefix_def)
       
   782 
       
   783 lemma PMatch2:
       
   784   assumes "s \<in> (der c r) \<rightarrow> v"
       
   785   shows "(c#s) \<in> r \<rightarrow> (injval r c v)"
       
   786 using assms
       
   787 apply(induct c r arbitrary: s v rule: der.induct)
       
   788 apply(auto)
       
   789 apply(erule PMatch.cases)
       
   790 apply(simp_all)[5]
       
   791 apply(erule PMatch.cases)
       
   792 apply(simp_all)[5]
       
   793 apply(case_tac "c = c'")
       
   794 apply(simp)
       
   795 apply(erule PMatch.cases)
       
   796 apply(simp_all)[5]
       
   797 apply (metis PMatch.intros(2))
       
   798 apply(simp)
       
   799 apply(erule PMatch.cases)
       
   800 apply(simp_all)[5]
       
   801 apply(erule PMatch.cases)
       
   802 apply(simp_all)[5]
       
   803 apply (metis PMatch.intros(3))
       
   804 apply(clarify)
       
   805 apply(rule PMatch.intros)
       
   806 apply metis
       
   807 apply(simp add: L_flat_Prf)
       
   808 apply(auto)[1]
       
   809 thm v3_proj
       
   810 apply(frule_tac c="c" in v3_proj)
       
   811 apply metis
       
   812 apply(drule_tac x="projval r1 c v" in spec)
       
   813 apply(drule mp)
       
   814 apply (metis v4_proj2)
       
   815 apply(simp)
       
   816 apply(case_tac "nullable r1")
       
   817 apply(simp)
       
   818 defer
       
   819 apply(simp)
       
   820 apply(erule PMatch.cases)
       
   821 apply(simp_all)[5]
       
   822 apply(clarify)
       
   823 apply(subst append.simps(2)[symmetric])
       
   824 apply(rule PMatch.intros)
       
   825 apply metis
       
   826 apply metis
       
   827 apply(auto)[1]
       
   828 apply(simp add: L_flat_Prf)
       
   829 apply(auto)[1]
       
   830 apply(frule_tac c="c" in v3_proj)
       
   831 apply metis
       
   832 apply(drule_tac x="s3" in spec)
       
   833 apply(drule mp)
       
   834 apply(rule_tac x="projval r1 c v" in exI)
       
   835 apply(rule conjI)
       
   836 apply (metis v4_proj2)
       
   837 apply(simp)
       
   838 apply metis
       
   839 (* nullable case *)
       
   840 apply(erule PMatch.cases)
       
   841 apply(simp_all)[5]
       
   842 apply(clarify)
       
   843 apply(erule PMatch.cases)
       
   844 apply(simp_all)[5]
       
   845 apply(clarify)
       
   846 apply(subst append.simps(2)[symmetric])
       
   847 apply(rule PMatch.intros)
       
   848 apply metis
       
   849 apply metis
       
   850 apply(auto)[1]
       
   851 apply(simp add: L_flat_Prf)
       
   852 apply(auto)[1]
       
   853 apply(frule_tac c="c" in v3_proj)
       
   854 apply metis
       
   855 apply(drule_tac x="s3" in spec)
       
   856 apply(drule mp)
       
   857 apply (metis v4_proj2)
       
   858 apply(simp)
       
   859 (* interesting case *)
       
   860 apply(clarify)
       
   861 apply(simp)
       
   862 thm L.simps
       
   863 apply(subst (asm) L.simps(4)[symmetric])
       
   864 apply(simp only: L_flat_Prf)
       
   865 apply(simp)
       
   866 apply(subst append.simps(1)[symmetric])
       
   867 apply(rule PMatch.intros)
       
   868 apply (metis PMatch_mkeps)
       
   869 apply metis
       
   870 apply(auto)
       
   871 apply(simp only: L_flat_Prf)
       
   872 apply(simp)
       
   873 apply(auto)
       
   874 apply(drule_tac x="Seq (projval r1 c v) vb" in spec)
       
   875 apply(drule mp)
       
   876 apply(simp)
       
   877 apply (metis append_Cons butlast_snoc last_snoc neq_Nil_conv rotate1.simps(2) v4_proj2)
       
   878 apply(subgoal_tac "\<turnstile> projval r1 c v : der c r1")
       
   879 apply (metis Prf.intros(1))
       
   880 apply(rule v3_proj)
       
   881 apply(simp)
       
   882 by (metis Cons_eq_append_conv)
       
   883 
       
   884 lemma lex_correct1:
       
   885   assumes "s \<notin> L r"
       
   886   shows "lex r s = None"
       
   887 using assms
       
   888 apply(induct s arbitrary: r)
       
   889 apply(simp)
       
   890 apply (metis nullable_correctness)
       
   891 apply(auto)
       
   892 apply(drule_tac x="der a r" in meta_spec)
       
   893 apply(drule meta_mp)
       
   894 apply(auto)
       
   895 apply(simp add: L_flat_Prf)
       
   896 by (metis v3 v4)
       
   897 
       
   898 
       
   899 lemma lex_correct2:
       
   900   assumes "s \<in> L r"
       
   901   shows "\<exists>v. lex r s = Some(v) \<and> \<turnstile> v : r \<and> flat v = s"
       
   902 using assms
       
   903 apply(induct s arbitrary: r)
       
   904 apply(simp)
       
   905 apply (metis mkeps_flat mkeps_nullable nullable_correctness)
       
   906 apply(drule_tac x="der a r" in meta_spec)
       
   907 apply(drule meta_mp)
       
   908 apply(simp add: L_flat_Prf)
       
   909 apply(auto)
       
   910 apply (metis v3_proj v4_proj2)
       
   911 apply (metis v3)
       
   912 apply(rule v4)
       
   913 by metis
       
   914 
       
   915 lemma lex_correct3:
       
   916   assumes "s \<in> L r"
       
   917   shows "\<exists>v. lex r s = Some(v) \<and> s \<in> r \<rightarrow> v"
       
   918 using assms
       
   919 apply(induct s arbitrary: r)
       
   920 apply(simp)
       
   921 apply (metis PMatch_mkeps nullable_correctness)
       
   922 apply(drule_tac x="der a r" in meta_spec)
       
   923 apply(drule meta_mp)
       
   924 apply(simp add: L_flat_Prf)
       
   925 apply(auto)
       
   926 apply (metis v3_proj v4_proj2)
       
   927 apply(rule PMatch2)
       
   928 apply(simp)
       
   929 done
       
   930 
       
   931 lemma lex_correct4:
       
   932   assumes "s \<in> L r"
       
   933   shows "s \<in> r \<rightarrow> (lex2 r s)"
       
   934 using assms
       
   935 apply(induct s arbitrary: r)
       
   936 apply(simp)
       
   937 apply (metis PMatch_mkeps nullable_correctness)
       
   938 apply(simp)
       
   939 apply(rule PMatch2)
       
   940 apply(drule_tac x="der a r" in meta_spec)
       
   941 apply(drule meta_mp)
       
   942 apply(simp add: L_flat_Prf)
       
   943 apply(auto)
       
   944 apply (metis v3_proj v4_proj2)
       
   945 done
       
   946 
       
   947 lemma 
       
   948   "lex2 (ALT (CHAR a) (ALT (CHAR b) (SEQ (CHAR a) (CHAR b)))) [a,b] = Right (Right (Seq (Char a) (Char b)))"
       
   949 apply(simp)
       
   950 done
       
   951 
       
   952 
       
   953 section {* Sulzmann's Ordering of values *}
       
   954 
       
   955 thm PMatch.intros
       
   956 
       
   957 inductive ValOrd :: "string \<Rightarrow> val \<Rightarrow> rexp \<Rightarrow> val \<Rightarrow> bool" ("_ \<turnstile> _ \<succ>_ _" [100, 100, 100, 100] 100)
       
   958 where
       
   959   "\<lbrakk>s2 \<turnstile> v2 \<succ>r2 v2'; flat v1 = s1\<rbrakk> \<Longrightarrow> (s1 @ s2) \<turnstile> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1 v2')" 
       
   960 | "\<lbrakk>s1 \<turnstile> v1 \<succ>r1 v1'; v1 \<noteq> v1'\<rbrakk> \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1' v2')" 
       
   961 | "\<lbrakk>flat v1 \<sqsubseteq> s; flat v2 \<sqsubseteq> flat v1\<rbrakk> \<Longrightarrow> s \<turnstile> (Left v1) \<succ>(ALT r1 r2) (Right v2)"
       
   962 | "\<lbrakk>flat v2 \<sqsubseteq> s; flat v1 \<sqsubset>  flat v2\<rbrakk> \<Longrightarrow> s \<turnstile> (Right v2) \<succ>(ALT r1 r2) (Left v1)"
       
   963 | "s \<turnstile> v2 \<succ>r2 v2' \<Longrightarrow> s \<turnstile> (Right v2) \<succ>(ALT r1 r2) (Right v2')"
       
   964 | "s \<turnstile> v1 \<succ>r1 v1' \<Longrightarrow> s \<turnstile> (Left v1) \<succ>(ALT r1 r2) (Left v1')"
       
   965 | "s \<turnstile> Void \<succ>EMPTY Void"
       
   966 | "(c#s) \<turnstile> (Char c) \<succ>(CHAR c) (Char c)"
       
   967 
       
   968 
       
   969 inductive ValOrd :: "val \<Rightarrow> rexp \<Rightarrow> val \<Rightarrow> bool" ("_ \<succ>_ _" [100, 100, 100] 100)
       
   970 where
       
   971   "v2 \<succ>r2 v2' \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1 v2')" 
       
   972 | "\<lbrakk>v1 \<succ>r1 v1'; v1 \<noteq> v1'\<rbrakk> \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1' v2')" 
       
   973 | "length (flat v1) \<ge> length (flat v2) \<Longrightarrow> (Left v1) \<succ>(ALT r1 r2) (Right v2)"
       
   974 | "length (flat v2) > length (flat v1) \<Longrightarrow> (Right v2) \<succ>(ALT r1 r2) (Left v1)"
       
   975 | "v2 \<succ>r2 v2' \<Longrightarrow> (Right v2) \<succ>(ALT r1 r2) (Right v2')"
       
   976 | "v1 \<succ>r1 v1' \<Longrightarrow> (Left v1) \<succ>(ALT r1 r2) (Left v1')"
       
   977 | "Void \<succ>EMPTY Void"
       
   978 | "(Char c) \<succ>(CHAR c) (Char c)"
       
   979 
       
   980 lemma ValOrd_PMatch:
       
   981   assumes "s \<in> r \<rightarrow> v1" "\<turnstile> v2 : r" "flat v2  \<sqsubseteq> s"
       
   982   shows "v1 \<succ>r v2"
       
   983 using assms
       
   984 apply(induct r arbitrary: s v1 v2 rule: rexp.induct)
       
   985 apply(erule Prf.cases)
       
   986 apply(simp_all)[5]
       
   987 apply(erule Prf.cases)
       
   988 apply(simp_all)[5]
       
   989 apply(erule PMatch.cases)
       
   990 apply(simp_all)[5]
       
   991 apply (metis ValOrd.intros(7))
       
   992 apply(erule Prf.cases)
       
   993 apply(simp_all)[5]
       
   994 apply(erule PMatch.cases)
       
   995 apply(simp_all)[5]
       
   996 apply (metis ValOrd.intros(8))
       
   997 defer
       
   998 apply(erule Prf.cases)
       
   999 apply(simp_all)[5]
       
  1000 apply(erule PMatch.cases)
       
  1001 apply(simp_all)[5]
       
  1002 apply (metis ValOrd.intros(6))
       
  1003 apply (metis PMatch1(2) Prf_flat_L ValOrd.intros(4) length_sprefix sprefix_def)
       
  1004 apply(clarify)
       
  1005 apply(erule PMatch.cases)
       
  1006 apply(simp_all)[5]
       
  1007 apply (metis PMatch1(2) ValOrd.intros(3) length_sprefix less_imp_le_nat order_refl sprefix_def)
       
  1008 apply(clarify)
       
  1009 apply (metis ValOrd.intros(5))
       
  1010 (* Seq case *)
       
  1011 apply(erule Prf.cases)
       
  1012 apply(simp_all)[5]
       
  1013 apply(auto)
       
  1014 apply(erule PMatch.cases)
       
  1015 apply(simp_all)[5]
       
  1016 apply(auto)
       
  1017 apply(case_tac "v1b = v1a")
       
  1018 apply(auto)
       
  1019 apply(simp add: prefix_def)
       
  1020 apply(auto)[1]
       
  1021 apply (metis PMatch1(2) ValOrd.intros(1) same_append_eq)
       
  1022 apply(simp add: prefix_def)
       
  1023 apply(auto)[1]
       
  1024 apply(simp add: append_eq_append_conv2)
       
  1025 apply(auto)
       
  1026 prefer 2
       
  1027 apply (metis ValOrd.intros(2))
       
  1028 prefer 2
       
  1029 apply (metis ValOrd.intros(2))
       
  1030 apply(case_tac "us = []")
       
  1031 apply(simp)
       
  1032 apply (metis ValOrd.intros(2) append_Nil2)
       
  1033 apply(drule_tac x="us" in spec)
       
  1034 apply(simp)
       
  1035 apply(drule_tac mp)
       
  1036 apply (metis Prf_flat_L)
       
  1037 apply(drule_tac x="s1 @ us" in meta_spec)
       
  1038 apply(drule_tac x="v1b" in meta_spec)
       
  1039 apply(drule_tac x="v1a" in meta_spec)
       
  1040 apply(drule_tac meta_mp)
       
  1041 
       
  1042 apply(simp)
       
  1043 apply(drule_tac meta_mp)
       
  1044 apply(simp)
       
  1045 apply(simp)
       
  1046 apply(simp)
       
  1047 apply(clarify)
       
  1048 apply (metis ValOrd.intros(6))
       
  1049 apply(clarify)
       
  1050 apply (metis PMatch1(2) ValOrd.intros(3) length_sprefix less_imp_le_nat order_refl sprefix_def)
       
  1051 apply(erule Prf.cases)
       
  1052 apply(simp_all)[5]
       
  1053 apply(clarify)
       
  1054 apply (metis PMatch1(2) Prf_flat_L ValOrd.intros(4) length_sprefix sprefix_def)
       
  1055 apply (metis ValOrd.intros(5))
       
  1056 (* Seq case *)
       
  1057 apply(erule Prf.cases)
       
  1058 apply(simp_all)[5]
       
  1059 apply(auto)
       
  1060 apply(case_tac "v1 = v1a")
       
  1061 apply(auto)
       
  1062 apply(simp add: prefix_def)
       
  1063 apply(auto)[1]
       
  1064 apply (metis PMatch1(2) ValOrd.intros(1) same_append_eq)
       
  1065 apply(simp add: prefix_def)
       
  1066 apply(auto)[1]
       
  1067 apply(frule PMatch1)
       
  1068 apply(frule PMatch1(2)[symmetric])
       
  1069 apply(clarify)
       
  1070 apply(simp add: append_eq_append_conv2)
       
  1071 apply(auto)
       
  1072 prefer 2
       
  1073 apply (metis ValOrd.intros(2))
       
  1074 prefer 2
       
  1075 apply (metis ValOrd.intros(2))
       
  1076 apply(case_tac "us = []")
       
  1077 apply(simp)
       
  1078 apply (metis ValOrd.intros(2) append_Nil2)
       
  1079 apply(drule_tac x="us" in spec)
       
  1080 apply(simp)
       
  1081 apply(drule mp)
       
  1082 apply (metis  Prf_flat_L)
       
  1083 apply(drule_tac x="v1a" in meta_spec)
       
  1084 apply(drule_tac meta_mp)
       
  1085 apply(simp)
       
  1086 apply(drule_tac meta_mp)
       
  1087 apply(simp)
       
  1088 
       
  1089 lemma ValOrd_PMatch:
       
  1090   assumes "s \<in> r \<rightarrow> v1" "\<turnstile> v2 : r" "flat v2  \<sqsubseteq> s"
       
  1091   shows "v1 \<succ>r v2"
       
  1092 using assms
       
  1093 apply(induct arbitrary: v2 rule: .induct)
       
  1094 apply(erule Prf.cases)
       
  1095 apply(simp_all)[5]
       
  1096 apply (metis ValOrd.intros(7))
       
  1097 apply(erule Prf.cases)
       
  1098 apply(simp_all)[5]
       
  1099 apply (metis ValOrd.intros(8))
       
  1100 apply(erule Prf.cases)
       
  1101 apply(simp_all)[5]
       
  1102 apply(clarify)
       
  1103 apply (metis ValOrd.intros(6))
       
  1104 apply(clarify)
       
  1105 apply (metis PMatch1(2) ValOrd.intros(3) length_sprefix less_imp_le_nat order_refl sprefix_def)
       
  1106 apply(erule Prf.cases)
       
  1107 apply(simp_all)[5]
       
  1108 apply(clarify)
       
  1109 apply (metis PMatch1(2) Prf_flat_L ValOrd.intros(4) length_sprefix sprefix_def)
       
  1110 apply (metis ValOrd.intros(5))
       
  1111 (* Seq case *)
       
  1112 apply(erule Prf.cases)
       
  1113 apply(simp_all)[5]
       
  1114 apply(auto)
       
  1115 apply(case_tac "v1 = v1a")
       
  1116 apply(auto)
       
  1117 apply(simp add: prefix_def)
       
  1118 apply(auto)[1]
       
  1119 apply (metis PMatch1(2) ValOrd.intros(1) same_append_eq)
       
  1120 apply(simp add: prefix_def)
       
  1121 apply(auto)[1]
       
  1122 apply(frule PMatch1)
       
  1123 apply(frule PMatch1(2)[symmetric])
       
  1124 apply(clarify)
       
  1125 apply(simp add: append_eq_append_conv2)
       
  1126 apply(auto)
       
  1127 prefer 2
       
  1128 apply (metis ValOrd.intros(2))
       
  1129 prefer 2
       
  1130 apply (metis ValOrd.intros(2))
       
  1131 apply(case_tac "us = []")
       
  1132 apply(simp)
       
  1133 apply (metis ValOrd.intros(2) append_Nil2)
       
  1134 apply(drule_tac x="us" in spec)
       
  1135 apply(simp)
       
  1136 apply(drule mp)
       
  1137 apply (metis  Prf_flat_L)
       
  1138 apply(drule_tac x="v1a" in meta_spec)
       
  1139 apply(drule_tac meta_mp)
       
  1140 apply(simp)
       
  1141 apply(drule_tac meta_mp)
       
  1142 apply(simp)
       
  1143 
       
  1144 apply (metis PMatch1(2) ValOrd.intros(1) same_append_eq)
       
  1145 apply(rule ValOrd.intros(2))
       
  1146 apply(auto)
       
  1147 apply(drule_tac x="v1a" in meta_spec)
       
  1148 apply(drule_tac meta_mp)
       
  1149 apply(simp)
       
  1150 apply(drule_tac meta_mp)
       
  1151 prefer 2
       
  1152 apply(simp)
       
  1153 thm append_eq_append_conv
       
  1154 apply(simp add: append_eq_append_conv2)
       
  1155 apply(auto)
       
  1156 apply (metis Prf_flat_L)
       
  1157 apply(case_tac "us = []")
       
  1158 apply(simp)
       
  1159 apply(drule_tac x="us" in spec)
       
  1160 apply(drule mp)
       
  1161 
       
  1162 
       
  1163 inductive ValOrd2 :: "val \<Rightarrow> val \<Rightarrow> bool" ("_ 2\<succ> _" [100, 100] 100)
       
  1164 where
       
  1165   "v2 2\<succ> v2' \<Longrightarrow> (Seq v1 v2) 2\<succ> (Seq v1 v2')" 
       
  1166 | "\<lbrakk>v1 2\<succ> v1'; v1 \<noteq> v1'\<rbrakk> \<Longrightarrow> (Seq v1 v2) 2\<succ> (Seq v1' v2')" 
       
  1167 | "length (flat v1) \<ge> length (flat v2) \<Longrightarrow> (Left v1) 2\<succ> (Right v2)"
       
  1168 | "length (flat v2) > length (flat v1) \<Longrightarrow> (Right v2) 2\<succ> (Left v1)"
       
  1169 | "v2 2\<succ> v2' \<Longrightarrow> (Right v2) 2\<succ> (Right v2')"
       
  1170 | "v1 2\<succ> v1' \<Longrightarrow> (Left v1) 2\<succ> (Left v1')"
       
  1171 | "Void 2\<succ> Void"
       
  1172 | "(Char c) 2\<succ> (Char c)"
       
  1173 
       
  1174 lemma Ord1:
       
  1175   "v1 \<succ>r v2 \<Longrightarrow> v1 2\<succ> v2"
       
  1176 apply(induct rule: ValOrd.induct)
       
  1177 apply(auto intro: ValOrd2.intros)
       
  1178 done
       
  1179 
       
  1180 lemma Ord2:
       
  1181   "v1 2\<succ> v2 \<Longrightarrow> \<exists>r. v1 \<succ>r v2"
       
  1182 apply(induct v1 v2 rule: ValOrd2.induct)
       
  1183 apply(auto intro: ValOrd.intros)
       
  1184 done
       
  1185 
       
  1186 lemma Ord3:
       
  1187   "\<lbrakk>v1 2\<succ> v2; \<turnstile> v1 : r\<rbrakk> \<Longrightarrow> v1 \<succ>r v2"
       
  1188 apply(induct v1 v2 arbitrary: r rule: ValOrd2.induct)
       
  1189 apply(auto intro: ValOrd.intros elim: Prf.cases)
       
  1190 done
       
  1191 
       
  1192 section {* Posix definition *}
       
  1193 
       
  1194 definition POSIX :: "val \<Rightarrow> rexp \<Rightarrow> bool" 
       
  1195 where
       
  1196   "POSIX v r \<equiv> (\<turnstile> v : r \<and> (\<forall>v'. (\<turnstile> v' : r \<and> flat v' \<sqsubseteq> flat v) \<longrightarrow> v \<succ>r v'))"
       
  1197 
       
  1198 lemma ValOrd_refl:
       
  1199   assumes "\<turnstile> v : r"
       
  1200   shows "v \<succ>r v"
       
  1201 using assms
       
  1202 apply(induct)
       
  1203 apply(auto intro: ValOrd.intros)
       
  1204 done
       
  1205 
       
  1206 lemma ValOrd_total:
       
  1207   shows "\<lbrakk>\<turnstile> v1 : r; \<turnstile> v2 : r\<rbrakk>  \<Longrightarrow> v1 \<succ>r v2 \<or> v2 \<succ>r v1"
       
  1208 apply(induct r arbitrary: v1 v2)
       
  1209 apply(auto)
       
  1210 apply(erule Prf.cases)
       
  1211 apply(simp_all)[5]
       
  1212 apply(erule Prf.cases)
       
  1213 apply(simp_all)[5]
       
  1214 apply(erule Prf.cases)
       
  1215 apply(simp_all)[5]
       
  1216 apply (metis ValOrd.intros(7))
       
  1217 apply(erule Prf.cases)
       
  1218 apply(simp_all)[5]
       
  1219 apply(erule Prf.cases)
       
  1220 apply(simp_all)[5]
       
  1221 apply (metis ValOrd.intros(8))
       
  1222 apply(erule Prf.cases)
       
  1223 apply(simp_all)[5]
       
  1224 apply(erule Prf.cases)
       
  1225 apply(simp_all)[5]
       
  1226 apply(clarify)
       
  1227 apply(case_tac "v1a = v1b")
       
  1228 apply(simp)
       
  1229 apply(rule ValOrd.intros(1))
       
  1230 apply (metis ValOrd.intros(1))
       
  1231 apply(rule ValOrd.intros(2))
       
  1232 apply(auto)[2]
       
  1233 apply(erule contrapos_np)
       
  1234 apply(rule ValOrd.intros(2))
       
  1235 apply(auto)
       
  1236 apply(erule Prf.cases)
       
  1237 apply(simp_all)[5]
       
  1238 apply(erule Prf.cases)
       
  1239 apply(simp_all)[5]
       
  1240 apply (metis Ord1 Ord3 Prf.intros(2) ValOrd2.intros(6))
       
  1241 apply(rule ValOrd.intros)
       
  1242 apply(erule contrapos_np)
       
  1243 apply(rule ValOrd.intros)
       
  1244 apply (metis le_eq_less_or_eq neq_iff)
       
  1245 apply(erule Prf.cases)
       
  1246 apply(simp_all)[5]
       
  1247 apply(rule ValOrd.intros)
       
  1248 apply(erule contrapos_np)
       
  1249 apply(rule ValOrd.intros)
       
  1250 apply (metis le_eq_less_or_eq neq_iff)
       
  1251 apply(rule ValOrd.intros)
       
  1252 apply(erule contrapos_np)
       
  1253 apply(rule ValOrd.intros)
       
  1254 by metis
       
  1255 
       
  1256 lemma ValOrd_anti:
       
  1257   shows "\<lbrakk>\<turnstile> v1 : r; \<turnstile> v2 : r; v1 \<succ>r v2; v2 \<succ>r v1\<rbrakk> \<Longrightarrow> v1 = v2"
       
  1258 apply(induct r arbitrary: v1 v2)
       
  1259 apply(erule Prf.cases)
       
  1260 apply(simp_all)[5]
       
  1261 apply(erule Prf.cases)
       
  1262 apply(simp_all)[5]
       
  1263 apply(erule Prf.cases)
       
  1264 apply(simp_all)[5]
       
  1265 apply(erule Prf.cases)
       
  1266 apply(simp_all)[5]
       
  1267 apply(erule Prf.cases)
       
  1268 apply(simp_all)[5]
       
  1269 apply(erule Prf.cases)
       
  1270 apply(simp_all)[5]
       
  1271 apply(erule Prf.cases)
       
  1272 apply(simp_all)[5]
       
  1273 apply(erule ValOrd.cases)
       
  1274 apply(simp_all)[8]
       
  1275 apply(erule ValOrd.cases)
       
  1276 apply(simp_all)[8]
       
  1277 apply(erule ValOrd.cases)
       
  1278 apply(simp_all)[8]
       
  1279 apply(erule Prf.cases)
       
  1280 apply(simp_all)[5]
       
  1281 apply(erule Prf.cases)
       
  1282 apply(simp_all)[5]
       
  1283 apply(erule ValOrd.cases)
       
  1284 apply(simp_all)[8]
       
  1285 apply(erule ValOrd.cases)
       
  1286 apply(simp_all)[8]
       
  1287 apply(erule ValOrd.cases)
       
  1288 apply(simp_all)[8]
       
  1289 apply(erule ValOrd.cases)
       
  1290 apply(simp_all)[8]
       
  1291 apply(erule Prf.cases)
       
  1292 apply(simp_all)[5]
       
  1293 apply(erule ValOrd.cases)
       
  1294 apply(simp_all)[8]
       
  1295 apply(erule ValOrd.cases)
       
  1296 apply(simp_all)[8]
       
  1297 apply(erule ValOrd.cases)
       
  1298 apply(simp_all)[8]
       
  1299 apply(erule ValOrd.cases)
       
  1300 apply(simp_all)[8]
       
  1301 done
       
  1302 
       
  1303 lemma POSIX_ALT_I1:
       
  1304   assumes "POSIX v1 r1" 
       
  1305   shows "POSIX (Left v1) (ALT r1 r2)"
       
  1306 using assms
       
  1307 unfolding POSIX_def
       
  1308 apply(auto)
       
  1309 apply (metis Prf.intros(2))
       
  1310 apply(rotate_tac 2)
       
  1311 apply(erule Prf.cases)
       
  1312 apply(simp_all)[5]
       
  1313 apply(auto)
       
  1314 apply(rule ValOrd.intros)
       
  1315 apply(auto)
       
  1316 apply(rule ValOrd.intros)
       
  1317 by (metis le_eq_less_or_eq length_sprefix sprefix_def)
       
  1318 
       
  1319 lemma POSIX_ALT_I2:
       
  1320   assumes "POSIX v2 r2" "\<forall>v'. \<turnstile> v' : r1 \<longrightarrow> length (flat v2) > length (flat v')"
       
  1321   shows "POSIX (Right v2) (ALT r1 r2)"
       
  1322 using assms
       
  1323 unfolding POSIX_def
       
  1324 apply(auto)
       
  1325 apply (metis Prf.intros)
       
  1326 apply(rotate_tac 3)
       
  1327 apply(erule Prf.cases)
       
  1328 apply(simp_all)[5]
       
  1329 apply(auto)
       
  1330 apply(rule ValOrd.intros)
       
  1331 apply metis
       
  1332 apply(rule ValOrd.intros)
       
  1333 apply metis
       
  1334 done
       
  1335 
       
  1336 thm PMatch.intros[no_vars]
       
  1337 
       
  1338 lemma POSIX_PMatch:
       
  1339   assumes "s \<in> r \<rightarrow> v" "\<turnstile> v' : r"
       
  1340   shows "length (flat v') \<le> length (flat v)" 
       
  1341 using assms
       
  1342 apply(induct arbitrary: s v v' rule: rexp.induct)
       
  1343 apply(erule Prf.cases)
       
  1344 apply(simp_all)[5]
       
  1345 apply(erule Prf.cases)
       
  1346 apply(simp_all)[5]
       
  1347 apply(erule Prf.cases)
       
  1348 apply(simp_all)[5]
       
  1349 apply(erule PMatch.cases)
       
  1350 apply(simp_all)[5]
       
  1351 defer
       
  1352 apply(erule Prf.cases)
       
  1353 apply(simp_all)[5]
       
  1354 apply(erule PMatch.cases)
       
  1355 apply(simp_all)[5]
       
  1356 apply(clarify)
       
  1357 apply(simp add: L_flat_Prf)
       
  1358 
       
  1359 apply(clarify)
       
  1360 apply (metis ValOrd.intros(8))
       
  1361 apply (metis POSIX_ALT_I1)
       
  1362 apply(rule POSIX_ALT_I2)
       
  1363 apply(simp)
       
  1364 apply(auto)[1]
       
  1365 apply(simp add: POSIX_def)
       
  1366 apply(frule PMatch1(1))
       
  1367 apply(frule PMatch1(2))
       
  1368 apply(simp)
       
  1369 
       
  1370 
       
  1371 lemma POSIX_PMatch:
       
  1372   assumes "s \<in> r \<rightarrow> v" 
       
  1373   shows "POSIX v r" 
       
  1374 using assms
       
  1375 apply(induct arbitrary: rule: PMatch.induct)
       
  1376 apply(auto)
       
  1377 apply(simp add: POSIX_def)
       
  1378 apply(auto)[1]
       
  1379 apply (metis Prf.intros(4))
       
  1380 apply(erule Prf.cases)
       
  1381 apply(simp_all)[5]
       
  1382 apply (metis ValOrd.intros(7))
       
  1383 apply(simp add: POSIX_def)
       
  1384 apply(auto)[1]
       
  1385 apply (metis Prf.intros(5))
       
  1386 apply(erule Prf.cases)
       
  1387 apply(simp_all)[5]
       
  1388 apply (metis ValOrd.intros(8))
       
  1389 apply (metis POSIX_ALT_I1)
       
  1390 apply(rule POSIX_ALT_I2)
       
  1391 apply(simp)
       
  1392 apply(auto)[1]
       
  1393 apply(simp add: POSIX_def)
       
  1394 apply(frule PMatch1(1))
       
  1395 apply(frule PMatch1(2))
       
  1396 apply(simp)
       
  1397 
       
  1398 
       
  1399 
       
  1400 lemma ValOrd_PMatch:
       
  1401   assumes "s \<in> r \<rightarrow> v1" "\<turnstile> v2 : r" "flat v2 = s"
       
  1402   shows "v1 \<succ>r v2"
       
  1403 using assms
       
  1404 apply(induct arbitrary: v2 rule: PMatch.induct)
       
  1405 apply(erule Prf.cases)
       
  1406 apply(simp_all)[5]
       
  1407 apply (metis ValOrd.intros(7))
       
  1408 apply(erule Prf.cases)
       
  1409 apply(simp_all)[5]
       
  1410 apply (metis ValOrd.intros(8))
       
  1411 apply(erule Prf.cases)
       
  1412 apply(simp_all)[5]
       
  1413 apply(clarify)
       
  1414 apply (metis ValOrd.intros(6))
       
  1415 apply(clarify)
       
  1416 apply (metis PMatch1(2) ValOrd.intros(3) order_refl)
       
  1417 apply(erule Prf.cases)
       
  1418 apply(simp_all)[5]
       
  1419 apply(clarify)
       
  1420 apply (metis Prf_flat_L)
       
  1421 apply (metis ValOrd.intros(5))
       
  1422 (* Seq case *)
       
  1423 apply(erule Prf.cases)
       
  1424 apply(simp_all)[5]
       
  1425 apply(auto)
       
  1426 apply(case_tac "v1 = v1a")
       
  1427 apply(auto)
       
  1428 apply (metis PMatch1(2) ValOrd.intros(1) same_append_eq)
       
  1429 apply(rule ValOrd.intros(2))
       
  1430 apply(auto)
       
  1431 apply(drule_tac x="v1a" in meta_spec)
       
  1432 apply(drule_tac meta_mp)
       
  1433 apply(simp)
       
  1434 apply(drule_tac meta_mp)
       
  1435 prefer 2
       
  1436 apply(simp)
       
  1437 apply(simp add: append_eq_append_conv2)
       
  1438 apply(auto)
       
  1439 apply (metis Prf_flat_L)
       
  1440 apply(case_tac "us = []")
       
  1441 apply(simp)
       
  1442 apply(drule_tac x="us" in spec)
       
  1443 apply(drule mp)
       
  1444 
       
  1445 thm L_flat_Prf
       
  1446 apply(simp add: L_flat_Prf)
       
  1447 thm append_eq_append_conv2
       
  1448 apply(simp add: append_eq_append_conv2)
       
  1449 apply(auto)
       
  1450 apply(drule_tac x="us" in spec)
       
  1451 apply(drule mp)
       
  1452 apply metis
       
  1453 apply (metis append_Nil2)
       
  1454 apply(case_tac "us = []")
       
  1455 apply(auto)
       
  1456 apply(drule_tac x="s2" in spec)
       
  1457 apply(drule mp)
       
  1458 
       
  1459 apply(auto)[1]
       
  1460 apply(drule_tac x="v1a" in meta_spec)
       
  1461 apply(simp)
       
  1462 
       
  1463 lemma refl_on_ValOrd:
       
  1464   "refl_on (Values r s) {(v1, v2). v1 \<succ>r v2 \<and> v1 \<in> Values r s \<and> v2 \<in> Values r s}"
       
  1465 unfolding refl_on_def
       
  1466 apply(auto)
       
  1467 apply(rule ValOrd_refl)
       
  1468 apply(simp add: Values_def)
       
  1469 done
       
  1470 
       
  1471 
       
  1472 section {* Posix definition *}
       
  1473 
       
  1474 definition POSIX :: "val \<Rightarrow> rexp \<Rightarrow> bool" 
       
  1475 where
       
  1476   "POSIX v r \<equiv> (\<turnstile> v : r \<and> (\<forall>v'. (\<turnstile> v' : r \<and> flat v = flat v') \<longrightarrow> v \<succ>r v'))"
       
  1477 
       
  1478 definition POSIX2 :: "val \<Rightarrow> rexp \<Rightarrow> bool" 
       
  1479 where
       
  1480   "POSIX2 v r \<equiv> (\<turnstile> v : r \<and> (\<forall>v'. (\<turnstile> v' : r \<and> flat v = flat v') \<longrightarrow> v 2\<succ> v'))"
       
  1481 
       
  1482 lemma "POSIX v r = POSIX2 v r"
       
  1483 unfolding POSIX_def POSIX2_def
       
  1484 apply(auto)
       
  1485 apply(rule Ord1)
       
  1486 apply(auto)
       
  1487 apply(rule Ord3)
       
  1488 apply(auto)
       
  1489 done
       
  1490 
       
  1491 section {* POSIX for some constructors *}
       
  1492 
       
  1493 lemma POSIX_SEQ1:
       
  1494   assumes "POSIX (Seq v1 v2) (SEQ r1 r2)" "\<turnstile> v1 : r1" "\<turnstile> v2 : r2"
       
  1495   shows "POSIX v1 r1"
       
  1496 using assms
       
  1497 unfolding POSIX_def
       
  1498 apply(auto)
       
  1499 apply(drule_tac x="Seq v' v2" in spec)
       
  1500 apply(simp)
       
  1501 apply(erule impE)
       
  1502 apply(rule Prf.intros)
       
  1503 apply(simp)
       
  1504 apply(simp)
       
  1505 apply(erule ValOrd.cases)
       
  1506 apply(simp_all)
       
  1507 apply(clarify)
       
  1508 by (metis ValOrd_refl)
       
  1509 
       
  1510 lemma POSIX_SEQ2:
       
  1511   assumes "POSIX (Seq v1 v2) (SEQ r1 r2)" "\<turnstile> v1 : r1" "\<turnstile> v2 : r2" 
       
  1512   shows "POSIX v2 r2"
       
  1513 using assms
       
  1514 unfolding POSIX_def
       
  1515 apply(auto)
       
  1516 apply(drule_tac x="Seq v1 v'" in spec)
       
  1517 apply(simp)
       
  1518 apply(erule impE)
       
  1519 apply(rule Prf.intros)
       
  1520 apply(simp)
       
  1521 apply(simp)
       
  1522 apply(erule ValOrd.cases)
       
  1523 apply(simp_all)
       
  1524 done
       
  1525 
       
  1526 lemma POSIX_ALT2:
       
  1527   assumes "POSIX (Left v1) (ALT r1 r2)"
       
  1528   shows "POSIX v1 r1"
       
  1529 using assms
       
  1530 unfolding POSIX_def
       
  1531 apply(auto)
       
  1532 apply(erule Prf.cases)
       
  1533 apply(simp_all)[5]
       
  1534 apply(drule_tac x="Left v'" in spec)
       
  1535 apply(simp)
       
  1536 apply(drule mp)
       
  1537 apply(rule Prf.intros)
       
  1538 apply(auto)
       
  1539 apply(erule ValOrd.cases)
       
  1540 apply(simp_all)
       
  1541 done
       
  1542 
       
  1543 lemma POSIX_ALT1a:
       
  1544   assumes "POSIX (Right v2) (ALT r1 r2)"
       
  1545   shows "POSIX v2 r2"
       
  1546 using assms
       
  1547 unfolding POSIX_def
       
  1548 apply(auto)
       
  1549 apply(erule Prf.cases)
       
  1550 apply(simp_all)[5]
       
  1551 apply(drule_tac x="Right v'" in spec)
       
  1552 apply(simp)
       
  1553 apply(drule mp)
       
  1554 apply(rule Prf.intros)
       
  1555 apply(auto)
       
  1556 apply(erule ValOrd.cases)
       
  1557 apply(simp_all)
       
  1558 done
       
  1559 
       
  1560 lemma POSIX_ALT1b:
       
  1561   assumes "POSIX (Right v2) (ALT r1 r2)"
       
  1562   shows "(\<forall>v'. (\<turnstile> v' : r2 \<and> flat v' = flat v2) \<longrightarrow> v2 \<succ>r2 v')"
       
  1563 using assms
       
  1564 apply(drule_tac POSIX_ALT1a)
       
  1565 unfolding POSIX_def
       
  1566 apply(auto)
       
  1567 done
       
  1568 
       
  1569 lemma POSIX_ALT_I1:
       
  1570   assumes "POSIX v1 r1" 
       
  1571   shows "POSIX (Left v1) (ALT r1 r2)"
       
  1572 using assms
       
  1573 unfolding POSIX_def
       
  1574 apply(auto)
       
  1575 apply (metis Prf.intros(2))
       
  1576 apply(rotate_tac 2)
       
  1577 apply(erule Prf.cases)
       
  1578 apply(simp_all)[5]
       
  1579 apply(auto)
       
  1580 apply(rule ValOrd.intros)
       
  1581 apply(auto)
       
  1582 apply(rule ValOrd.intros)
       
  1583 by simp
       
  1584 
       
  1585 lemma POSIX_ALT_I2:
       
  1586   assumes "POSIX v2 r2" "\<forall>v'. \<turnstile> v' : r1 \<longrightarrow> length (flat v2) > length (flat v')"
       
  1587   shows "POSIX (Right v2) (ALT r1 r2)"
       
  1588 using assms
       
  1589 unfolding POSIX_def
       
  1590 apply(auto)
       
  1591 apply (metis Prf.intros)
       
  1592 apply(rotate_tac 3)
       
  1593 apply(erule Prf.cases)
       
  1594 apply(simp_all)[5]
       
  1595 apply(auto)
       
  1596 apply(rule ValOrd.intros)
       
  1597 apply metis
       
  1598 done
       
  1599 
       
  1600 lemma mkeps_POSIX:
       
  1601   assumes "nullable r"
       
  1602   shows "POSIX (mkeps r) r"
       
  1603 using assms
       
  1604 apply(induct r)
       
  1605 apply(auto)[1]
       
  1606 apply(simp add: POSIX_def)
       
  1607 apply(auto)[1]
       
  1608 apply (metis Prf.intros(4))
       
  1609 apply(erule Prf.cases)
       
  1610 apply(simp_all)[5]
       
  1611 apply (metis ValOrd.intros)
       
  1612 apply(simp)
       
  1613 apply(auto)[1]
       
  1614 apply(simp add: POSIX_def)
       
  1615 apply(auto)[1]
       
  1616 apply (metis mkeps.simps(2) mkeps_nullable nullable.simps(5))
       
  1617 apply(rotate_tac 6)
       
  1618 apply(erule Prf.cases)
       
  1619 apply(simp_all)[5]
       
  1620 apply (simp add: mkeps_flat)
       
  1621 apply(case_tac "mkeps r1a = v1")
       
  1622 apply(simp)
       
  1623 apply (metis ValOrd.intros(1))
       
  1624 apply (rule ValOrd.intros(2))
       
  1625 apply metis
       
  1626 apply(simp)
       
  1627 (* ALT case *)
       
  1628 thm mkeps.simps
       
  1629 apply(simp)
       
  1630 apply(erule disjE)
       
  1631 apply(simp)
       
  1632 apply (metis POSIX_ALT_I1)
       
  1633 (* *)
       
  1634 apply(auto)[1]
       
  1635 thm  POSIX_ALT_I1
       
  1636 apply (metis POSIX_ALT_I1)
       
  1637 apply(simp (no_asm) add: POSIX_def)
       
  1638 apply(auto)[1]
       
  1639 apply(rule Prf.intros(3))
       
  1640 apply(simp only: POSIX_def)
       
  1641 apply(rotate_tac 4)
       
  1642 apply(erule Prf.cases)
       
  1643 apply(simp_all)[5]
       
  1644 thm mkeps_flat
       
  1645 apply(simp add: mkeps_flat)
       
  1646 apply(auto)[1]
       
  1647 thm Prf_flat_L nullable_correctness
       
  1648 apply (metis Prf_flat_L nullable_correctness)
       
  1649 apply(rule ValOrd.intros)
       
  1650 apply(subst (asm) POSIX_def)
       
  1651 apply(clarify)
       
  1652 apply(drule_tac x="v2" in spec)
       
  1653 by simp
       
  1654 
       
  1655 
       
  1656 
       
  1657 text {*
       
  1658   Injection value is related to r
       
  1659 *}
       
  1660 
       
  1661 
       
  1662 
       
  1663 text {*
       
  1664   The string behind the injection value is an added c
       
  1665 *}
       
  1666 
       
  1667 
       
  1668 lemma injval_inj: "inj_on (injval r c) {v. \<turnstile> v : der c r}"
       
  1669 apply(induct c r rule: der.induct)
       
  1670 unfolding inj_on_def
       
  1671 apply(auto)[1]
       
  1672 apply(erule Prf.cases)
       
  1673 apply(simp_all)[5]
       
  1674 apply(auto)[1]
       
  1675 apply(erule Prf.cases)
       
  1676 apply(simp_all)[5]
       
  1677 apply(auto)[1]
       
  1678 apply(erule Prf.cases)
       
  1679 apply(simp_all)[5]
       
  1680 apply(erule Prf.cases)
       
  1681 apply(simp_all)[5]
       
  1682 apply(erule Prf.cases)
       
  1683 apply(simp_all)[5]
       
  1684 apply(auto)[1]
       
  1685 apply(erule Prf.cases)
       
  1686 apply(simp_all)[5]
       
  1687 apply(erule Prf.cases)
       
  1688 apply(simp_all)[5]
       
  1689 apply(erule Prf.cases)
       
  1690 apply(simp_all)[5]
       
  1691 apply(auto)[1]
       
  1692 apply(erule Prf.cases)
       
  1693 apply(simp_all)[5]
       
  1694 apply(erule Prf.cases)
       
  1695 apply(simp_all)[5]
       
  1696 apply(clarify)
       
  1697 apply(erule Prf.cases)
       
  1698 apply(simp_all)[5]
       
  1699 apply(erule Prf.cases)
       
  1700 apply(simp_all)[5]
       
  1701 apply(clarify)
       
  1702 apply(erule Prf.cases)
       
  1703 apply(simp_all)[5]
       
  1704 apply(clarify)
       
  1705 apply (metis list.distinct(1) mkeps_flat v4)
       
  1706 apply(erule Prf.cases)
       
  1707 apply(simp_all)[5]
       
  1708 apply(clarify)
       
  1709 apply(rotate_tac 6)
       
  1710 apply(erule Prf.cases)
       
  1711 apply(simp_all)[5]
       
  1712 apply (metis list.distinct(1) mkeps_flat v4)
       
  1713 apply(erule Prf.cases)
       
  1714 apply(simp_all)[5]
       
  1715 apply(erule Prf.cases)
       
  1716 apply(simp_all)[5]
       
  1717 done
       
  1718 
       
  1719 lemma Values_nullable:
       
  1720   assumes "nullable r1"
       
  1721   shows "mkeps r1 \<in> Values r1 s"
       
  1722 using assms
       
  1723 apply(induct r1 arbitrary: s)
       
  1724 apply(simp_all)
       
  1725 apply(simp add: Values_recs)
       
  1726 apply(simp add: Values_recs)
       
  1727 apply(simp add: Values_recs)
       
  1728 apply(auto)[1]
       
  1729 done
       
  1730 
       
  1731 lemma Values_injval:
       
  1732   assumes "v \<in> Values (der c r) s"
       
  1733   shows "injval r c v \<in> Values r (c#s)"
       
  1734 using assms
       
  1735 apply(induct c r arbitrary: v s rule: der.induct)
       
  1736 apply(simp add: Values_recs)
       
  1737 apply(simp add: Values_recs)
       
  1738 apply(case_tac "c = c'")
       
  1739 apply(simp)
       
  1740 apply(simp add: Values_recs)
       
  1741 apply(simp add: prefix_def)
       
  1742 apply(simp)
       
  1743 apply(simp add: Values_recs)
       
  1744 apply(simp)
       
  1745 apply(simp add: Values_recs)
       
  1746 apply(auto)[1]
       
  1747 apply(case_tac "nullable r1")
       
  1748 apply(simp)
       
  1749 apply(simp add: Values_recs)
       
  1750 apply(auto)[1]
       
  1751 apply(simp add: rest_def)
       
  1752 apply(subst v4)
       
  1753 apply(simp add: Values_def)
       
  1754 apply(simp add: Values_def)
       
  1755 apply(rule Values_nullable)
       
  1756 apply(assumption)
       
  1757 apply(simp add: rest_def)
       
  1758 apply(subst mkeps_flat)
       
  1759 apply(assumption)
       
  1760 apply(simp)
       
  1761 apply(simp)
       
  1762 apply(simp add: Values_recs)
       
  1763 apply(auto)[1]
       
  1764 apply(simp add: rest_def)
       
  1765 apply(subst v4)
       
  1766 apply(simp add: Values_def)
       
  1767 apply(simp add: Values_def)
       
  1768 done
       
  1769 
       
  1770 lemma Values_projval:
       
  1771   assumes "v \<in> Values r (c#s)" "\<exists>s. flat v = c # s"
       
  1772   shows "projval r c v \<in> Values (der c r) s"
       
  1773 using assms
       
  1774 apply(induct r arbitrary: v s c rule: rexp.induct)
       
  1775 apply(simp add: Values_recs)
       
  1776 apply(simp add: Values_recs)
       
  1777 apply(case_tac "c = char")
       
  1778 apply(simp)
       
  1779 apply(simp add: Values_recs)
       
  1780 apply(simp)
       
  1781 apply(simp add: Values_recs)
       
  1782 apply(simp add: prefix_def)
       
  1783 apply(case_tac "nullable rexp1")
       
  1784 apply(simp)
       
  1785 apply(simp add: Values_recs)
       
  1786 apply(auto)[1]
       
  1787 apply(simp add: rest_def)
       
  1788 apply (metis hd_Cons_tl hd_append2 list.sel(1))
       
  1789 apply(simp add: rest_def)
       
  1790 apply(simp add: append_eq_Cons_conv)
       
  1791 apply(auto)[1]
       
  1792 apply(subst v4_proj2)
       
  1793 apply(simp add: Values_def)
       
  1794 apply(assumption)
       
  1795 apply(simp)
       
  1796 apply(simp)
       
  1797 apply(simp add: Values_recs)
       
  1798 apply(auto)[1]
       
  1799 apply(auto simp add: Values_def not_nullable_flat)[1]
       
  1800 apply(simp add: append_eq_Cons_conv)
       
  1801 apply(auto)[1]
       
  1802 apply(simp add: append_eq_Cons_conv)
       
  1803 apply(auto)[1]
       
  1804 apply(simp add: rest_def)
       
  1805 apply(subst v4_proj2)
       
  1806 apply(simp add: Values_def)
       
  1807 apply(assumption)
       
  1808 apply(simp)
       
  1809 apply(simp add: Values_recs)
       
  1810 apply(auto)[1]
       
  1811 done
       
  1812 
       
  1813 
       
  1814 definition "MValue v r s \<equiv> (v \<in> Values r s \<and> (\<forall>v' \<in> Values r s. v 2\<succ> v'))"
       
  1815 
       
  1816 lemma MValue_ALTE:
       
  1817   assumes "MValue v (ALT r1 r2) s"
       
  1818   shows "(\<exists>vl. v = Left vl \<and> MValue vl r1 s \<and> (\<forall>vr \<in> Values r2 s. length (flat vr) \<le> length (flat vl))) \<or> 
       
  1819          (\<exists>vr. v = Right vr \<and> MValue vr r2 s \<and> (\<forall>vl \<in> Values r1 s. length (flat vl) < length (flat vr)))"
       
  1820 using assms
       
  1821 apply(simp add: MValue_def)
       
  1822 apply(simp add: Values_recs)
       
  1823 apply(auto)
       
  1824 apply(drule_tac x="Left x" in bspec)
       
  1825 apply(simp)
       
  1826 apply(erule ValOrd2.cases)
       
  1827 apply(simp_all)
       
  1828 apply(drule_tac x="Right vr" in bspec)
       
  1829 apply(simp)
       
  1830 apply(erule ValOrd2.cases)
       
  1831 apply(simp_all)
       
  1832 apply(drule_tac x="Right x" in bspec)
       
  1833 apply(simp)
       
  1834 apply(erule ValOrd2.cases)
       
  1835 apply(simp_all)
       
  1836 apply(drule_tac x="Left vl" in bspec)
       
  1837 apply(simp)
       
  1838 apply(erule ValOrd2.cases)
       
  1839 apply(simp_all)
       
  1840 done
       
  1841 
       
  1842 lemma MValue_ALTI1:
       
  1843   assumes "MValue vl r1 s"  "\<forall>vr \<in> Values r2 s. length (flat vr) \<le> length (flat vl)"
       
  1844   shows "MValue (Left vl) (ALT r1 r2) s"
       
  1845 using assms
       
  1846 apply(simp add: MValue_def)
       
  1847 apply(simp add: Values_recs)
       
  1848 apply(auto)
       
  1849 apply(rule ValOrd2.intros)
       
  1850 apply metis
       
  1851 apply(rule ValOrd2.intros)
       
  1852 apply metis
       
  1853 done
       
  1854 
       
  1855 lemma MValue_ALTI2:
       
  1856   assumes "MValue vr r2 s"  "\<forall>vl \<in> Values r1 s. length (flat vl) < length (flat vr)"
       
  1857   shows "MValue (Right vr) (ALT r1 r2) s"
       
  1858 using assms
       
  1859 apply(simp add: MValue_def)
       
  1860 apply(simp add: Values_recs)
       
  1861 apply(auto)
       
  1862 apply(rule ValOrd2.intros)
       
  1863 apply metis
       
  1864 apply(rule ValOrd2.intros)
       
  1865 apply metis
       
  1866 done
       
  1867 
       
  1868 lemma t: "(c#xs = c#ys) \<Longrightarrow> xs = ys"
       
  1869 by (metis list.sel(3))
       
  1870 
       
  1871 lemma t2: "(xs = ys) \<Longrightarrow> (c#xs) = (c#ys)"
       
  1872 by (metis)
       
  1873 
       
  1874 lemma "\<not>(nullable r) \<Longrightarrow> \<not>(\<exists>v. \<turnstile> v : r \<and> flat v = [])"
       
  1875 by (metis Prf_flat_L nullable_correctness)
       
  1876 
       
  1877 
       
  1878 lemma LeftRight:
       
  1879   assumes "(Left v1) \<succ>(der c (ALT r1 r2)) (Right v2)"
       
  1880   and "\<turnstile> v1 : der c r1" "\<turnstile> v2 : der c r2" 
       
  1881   shows "(injval (ALT r1 r2) c (Left v1)) \<succ>(ALT r1 r2) (injval (ALT r1 r2) c (Right v2))"
       
  1882 using assms
       
  1883 apply(simp)
       
  1884 apply(erule ValOrd.cases)
       
  1885 apply(simp_all)[8]
       
  1886 apply(rule ValOrd.intros)
       
  1887 apply(clarify)
       
  1888 apply(subst v4)
       
  1889 apply(simp)
       
  1890 apply(subst v4)
       
  1891 apply(simp)
       
  1892 apply(simp)
       
  1893 done
       
  1894 
       
  1895 lemma RightLeft:
       
  1896   assumes "(Right v1) \<succ>(der c (ALT r1 r2)) (Left v2)"
       
  1897   and "\<turnstile> v1 : der c r2" "\<turnstile> v2 : der c r1" 
       
  1898   shows "(injval (ALT r1 r2) c (Right v1)) \<succ>(ALT r1 r2) (injval (ALT r1 r2) c (Left v2))"
       
  1899 using assms
       
  1900 apply(simp)
       
  1901 apply(erule ValOrd.cases)
       
  1902 apply(simp_all)[8]
       
  1903 apply(rule ValOrd.intros)
       
  1904 apply(clarify)
       
  1905 apply(subst v4)
       
  1906 apply(simp)
       
  1907 apply(subst v4)
       
  1908 apply(simp)
       
  1909 apply(simp)
       
  1910 done
       
  1911 
       
  1912 lemma h: 
       
  1913   assumes "nullable r1" "\<turnstile> v1 : der c r1"
       
  1914   shows "injval r1 c v1 \<succ>r1 mkeps r1"
       
  1915 using assms
       
  1916 apply(induct r1 arbitrary: v1 rule: der.induct)
       
  1917 apply(simp)
       
  1918 apply(simp)
       
  1919 apply(erule Prf.cases)
       
  1920 apply(simp_all)[5]
       
  1921 apply(simp)
       
  1922 apply(simp)
       
  1923 apply(erule Prf.cases)
       
  1924 apply(simp_all)[5]
       
  1925 apply(clarify)
       
  1926 apply(auto)[1]
       
  1927 apply (metis ValOrd.intros(6))
       
  1928 apply (metis ValOrd.intros(6))
       
  1929 apply (metis ValOrd.intros(3) le_add2 list.size(3) mkeps_flat monoid_add_class.add.right_neutral)
       
  1930 apply(auto)[1]
       
  1931 apply (metis ValOrd.intros(4) length_greater_0_conv list.distinct(1) list.size(3) mkeps_flat v4)
       
  1932 apply (metis ValOrd.intros(4) length_greater_0_conv list.distinct(1) list.size(3) mkeps_flat v4)
       
  1933 apply (metis ValOrd.intros(5))
       
  1934 apply(simp)
       
  1935 apply(erule Prf.cases)
       
  1936 apply(simp_all)[5]
       
  1937 apply(clarify)
       
  1938 apply(erule Prf.cases)
       
  1939 apply(simp_all)[5]
       
  1940 apply(clarify)
       
  1941 apply (metis ValOrd.intros(2) list.distinct(1) mkeps_flat v4)
       
  1942 apply(clarify)
       
  1943 by (metis ValOrd.intros(1))
       
  1944 
       
  1945 lemma LeftRightSeq:
       
  1946   assumes "(Left (Seq v1 v2)) \<succ>(der c (SEQ r1 r2)) (Right v3)"
       
  1947   and "nullable r1" "\<turnstile> v1 : der c r1"
       
  1948   shows "(injval (SEQ r1 r2) c (Seq v1 v2)) \<succ>(SEQ r1 r2) (injval (SEQ r1 r2) c (Right v2))"
       
  1949 using assms
       
  1950 apply(simp)
       
  1951 apply(erule ValOrd.cases)
       
  1952 apply(simp_all)[8]
       
  1953 apply(clarify)
       
  1954 apply(simp)
       
  1955 apply(rule ValOrd.intros(2))
       
  1956 prefer 2
       
  1957 apply (metis list.distinct(1) mkeps_flat v4)
       
  1958 by (metis h)
       
  1959 
       
  1960 lemma rr1: 
       
  1961   assumes "\<turnstile> v : r" "\<not>nullable r" 
       
  1962   shows "flat v \<noteq> []"
       
  1963 using assms
       
  1964 by (metis Prf_flat_L nullable_correctness)
       
  1965 
       
  1966 (* HERE *)
       
  1967 
       
  1968 lemma Prf_inj_test:
       
  1969   assumes "v1 \<succ>(der c r) v2" 
       
  1970           "v1 \<in> Values (der c r) s"
       
  1971           "v2 \<in> Values (der c r) s"
       
  1972           "injval r c v1 \<in> Values r (c#s)"
       
  1973           "injval r c v2 \<in> Values r (c#s)"
       
  1974   shows "(injval r c v1) 2\<succ>  (injval r c v2)"
       
  1975 using assms
       
  1976 apply(induct c r arbitrary: v1 v2 s rule: der.induct)
       
  1977 (* NULL case *)
       
  1978 apply(simp add: Values_recs)
       
  1979 (* EMPTY case *)
       
  1980 apply(simp add: Values_recs)
       
  1981 (* CHAR case *)
       
  1982 apply(case_tac "c = c'")
       
  1983 apply(simp)
       
  1984 apply(simp add: Values_recs)
       
  1985 apply (metis ValOrd2.intros(8))
       
  1986 apply(simp add: Values_recs)
       
  1987 (* ALT case *)
       
  1988 apply(simp)
       
  1989 apply(simp add: Values_recs)
       
  1990 apply(auto)[1]
       
  1991 apply(erule ValOrd.cases)
       
  1992 apply(simp_all)[8]
       
  1993 apply (metis ValOrd2.intros(6))
       
  1994 apply(erule ValOrd.cases)
       
  1995 apply(simp_all)[8]
       
  1996 apply(rule ValOrd2.intros)
       
  1997 apply(subst v4)
       
  1998 apply(simp add: Values_def)
       
  1999 apply(subst v4)
       
  2000 apply(simp add: Values_def)
       
  2001 apply(simp)
       
  2002 apply(erule ValOrd.cases)
       
  2003 apply(simp_all)[8]
       
  2004 apply(rule ValOrd2.intros)
       
  2005 apply(subst v4)
       
  2006 apply(simp add: Values_def)
       
  2007 apply(subst v4)
       
  2008 apply(simp add: Values_def)
       
  2009 apply(simp)
       
  2010 apply(erule ValOrd.cases)
       
  2011 apply(simp_all)[8]
       
  2012 apply (metis ValOrd2.intros(5))
       
  2013 (* SEQ case*)
       
  2014 apply(simp)
       
  2015 apply(case_tac "nullable r1")
       
  2016 apply(simp)
       
  2017 defer
       
  2018 apply(simp)
       
  2019 apply(simp add: Values_recs)
       
  2020 apply(auto)[1]
       
  2021 apply(erule ValOrd.cases)
       
  2022 apply(simp_all)[8]
       
  2023 apply(clarify)
       
  2024 apply(rule ValOrd2.intros)
       
  2025 apply(simp)
       
  2026 apply (metis Ord1)
       
  2027 apply(clarify)
       
  2028 apply(rule ValOrd2.intros)
       
  2029 apply(subgoal_tac "rest v1 (flat v1 @ flat v2) = flat v2")
       
  2030 apply(simp)
       
  2031 apply(subgoal_tac "rest (injval r1 c v1) (c # flat v1 @ flat v2) = flat v2")
       
  2032 apply(simp)
       
  2033 oops
       
  2034 
       
  2035 lemma Prf_inj_test:
       
  2036   assumes "v1 \<succ>(der c r) v2" 
       
  2037           "v1 \<in> Values (der c r) s"
       
  2038           "v2 \<in> Values (der c r) s"
       
  2039           "injval r c v1 \<in> Values r (c#s)"
       
  2040           "injval r c v2 \<in> Values r (c#s)"
       
  2041   shows "(injval r c v1) 2\<succ>  (injval r c v2)"
       
  2042 using assms
       
  2043 apply(induct c r arbitrary: v1 v2 s rule: der.induct)
       
  2044 (* NULL case *)
       
  2045 apply(simp add: Values_recs)
       
  2046 (* EMPTY case *)
       
  2047 apply(simp add: Values_recs)
       
  2048 (* CHAR case *)
       
  2049 apply(case_tac "c = c'")
       
  2050 apply(simp)
       
  2051 apply(simp add: Values_recs)
       
  2052 apply (metis ValOrd2.intros(8))
       
  2053 apply(simp add: Values_recs)
       
  2054 (* ALT case *)
       
  2055 apply(simp)
       
  2056 apply(simp add: Values_recs)
       
  2057 apply(auto)[1]
       
  2058 apply(erule ValOrd.cases)
       
  2059 apply(simp_all)[8]
       
  2060 apply (metis ValOrd2.intros(6))
       
  2061 apply(erule ValOrd.cases)
       
  2062 apply(simp_all)[8]
       
  2063 apply(rule ValOrd2.intros)
       
  2064 apply(subst v4)
       
  2065 apply(simp add: Values_def)
       
  2066 apply(subst v4)
       
  2067 apply(simp add: Values_def)
       
  2068 apply(simp)
       
  2069 apply(erule ValOrd.cases)
       
  2070 apply(simp_all)[8]
       
  2071 apply(rule ValOrd2.intros)
       
  2072 apply(subst v4)
       
  2073 apply(simp add: Values_def)
       
  2074 apply(subst v4)
       
  2075 apply(simp add: Values_def)
       
  2076 apply(simp)
       
  2077 apply(erule ValOrd.cases)
       
  2078 apply(simp_all)[8]
       
  2079 apply (metis ValOrd2.intros(5))
       
  2080 (* SEQ case*)
       
  2081 apply(simp)
       
  2082 apply(case_tac "nullable r1")
       
  2083 apply(simp)
       
  2084 defer
       
  2085 apply(simp)
       
  2086 apply(simp add: Values_recs)
       
  2087 apply(auto)[1]
       
  2088 apply(erule ValOrd.cases)
       
  2089 apply(simp_all)[8]
       
  2090 apply(clarify)
       
  2091 apply(rule ValOrd2.intros)
       
  2092 apply(simp)
       
  2093 apply (metis Ord1)
       
  2094 apply(clarify)
       
  2095 apply(rule ValOrd2.intros)
       
  2096 apply metis
       
  2097 using injval_inj
       
  2098 apply(simp add: Values_def inj_on_def)
       
  2099 apply metis
       
  2100 apply(simp add: Values_recs)
       
  2101 apply(auto)[1]
       
  2102 apply(erule ValOrd.cases)
       
  2103 apply(simp_all)[8]
       
  2104 apply(clarify)
       
  2105 apply(erule ValOrd.cases)
       
  2106 apply(simp_all)[8]
       
  2107 apply(clarify)
       
  2108 apply (metis Ord1 ValOrd2.intros(1))
       
  2109 apply(clarify)
       
  2110 apply(rule ValOrd2.intros(2))
       
  2111 apply metis
       
  2112 using injval_inj
       
  2113 apply(simp add: Values_def inj_on_def)
       
  2114 apply metis
       
  2115 apply(erule ValOrd.cases)
       
  2116 apply(simp_all)[8]
       
  2117 apply(rule ValOrd2.intros(2))
       
  2118 thm h
       
  2119 apply(rule Ord1)
       
  2120 apply(rule h)
       
  2121 apply(simp)
       
  2122 apply(simp add: Values_def)
       
  2123 apply(simp add: Values_def)
       
  2124 apply (metis list.distinct(1) mkeps_flat v4)
       
  2125 apply(erule ValOrd.cases)
       
  2126 apply(simp_all)[8]
       
  2127 apply(clarify)
       
  2128 apply(simp add: Values_def)
       
  2129 defer
       
  2130 apply(erule ValOrd.cases)
       
  2131 apply(simp_all)[8]
       
  2132 apply(clarify)
       
  2133 apply(rule ValOrd2.intros(1))
       
  2134 apply(rotate_tac 1)
       
  2135 apply(drule_tac x="v2" in meta_spec)
       
  2136 apply(rotate_tac 8)
       
  2137 apply(drule_tac x="v2'" in meta_spec)
       
  2138 apply(rotate_tac 8)
       
  2139 oops
       
  2140 
       
  2141 lemma POSIX_der:
       
  2142   assumes "POSIX v (der c r)" "\<turnstile> v : der c r"
       
  2143   shows "POSIX (injval r c v) r"
       
  2144 using assms
       
  2145 unfolding POSIX_def
       
  2146 apply(auto)
       
  2147 thm v3
       
  2148 apply (erule v3)
       
  2149 thm v4
       
  2150 apply(subst (asm) v4)
       
  2151 apply(assumption)
       
  2152 apply(drule_tac x="projval r c v'" in spec)
       
  2153 apply(drule mp)
       
  2154 apply(rule conjI)
       
  2155 thm v3_proj
       
  2156 apply(rule v3_proj)
       
  2157 apply(simp)
       
  2158 apply(rule_tac x="flat v" in exI)
       
  2159 apply(simp)
       
  2160 thm t
       
  2161 apply(rule_tac c="c" in  t)
       
  2162 apply(simp)
       
  2163 thm v4_proj
       
  2164 apply(subst v4_proj)
       
  2165 apply(simp)
       
  2166 apply(rule_tac x="flat v" in exI)
       
  2167 apply(simp)
       
  2168 apply(simp)
       
  2169 oops
       
  2170 
       
  2171 lemma POSIX_der:
       
  2172   assumes "POSIX v (der c r)" "\<turnstile> v : der c r"
       
  2173   shows "POSIX (injval r c v) r"
       
  2174 using assms
       
  2175 apply(induct c r arbitrary: v rule: der.induct)
       
  2176 (* null case*)
       
  2177 apply(simp add: POSIX_def)
       
  2178 apply(auto)[1]
       
  2179 apply(erule Prf.cases)
       
  2180 apply(simp_all)[5]
       
  2181 apply(erule Prf.cases)
       
  2182 apply(simp_all)[5]
       
  2183 (* empty case *)
       
  2184 apply(simp add: POSIX_def)
       
  2185 apply(auto)[1]
       
  2186 apply(erule Prf.cases)
       
  2187 apply(simp_all)[5]
       
  2188 apply(erule Prf.cases)
       
  2189 apply(simp_all)[5]
       
  2190 (* char case *)
       
  2191 apply(simp add: POSIX_def)
       
  2192 apply(case_tac "c = c'")
       
  2193 apply(auto)[1]
       
  2194 apply(erule Prf.cases)
       
  2195 apply(simp_all)[5]
       
  2196 apply (metis Prf.intros(5))
       
  2197 apply(erule Prf.cases)
       
  2198 apply(simp_all)[5]
       
  2199 apply(erule Prf.cases)
       
  2200 apply(simp_all)[5]
       
  2201 apply (metis ValOrd.intros(8))
       
  2202 apply(auto)[1]
       
  2203 apply(erule Prf.cases)
       
  2204 apply(simp_all)[5]
       
  2205 apply(erule Prf.cases)
       
  2206 apply(simp_all)[5]
       
  2207 (* alt case *)
       
  2208 apply(erule Prf.cases)
       
  2209 apply(simp_all)[5]
       
  2210 apply(clarify)
       
  2211 apply(simp (no_asm) add: POSIX_def)
       
  2212 apply(auto)[1]
       
  2213 apply (metis Prf.intros(2) v3)
       
  2214 apply(rotate_tac 4)
       
  2215 apply(erule Prf.cases)
       
  2216 apply(simp_all)[5]
       
  2217 apply (metis POSIX_ALT2 POSIX_def ValOrd.intros(6))
       
  2218 apply (metis ValOrd.intros(3) order_refl)
       
  2219 apply(simp (no_asm) add: POSIX_def)
       
  2220 apply(auto)[1]
       
  2221 apply (metis Prf.intros(3) v3)
       
  2222 apply(rotate_tac 4)
       
  2223 apply(erule Prf.cases)
       
  2224 apply(simp_all)[5]
       
  2225 defer
       
  2226 apply (metis POSIX_ALT1a POSIX_def ValOrd.intros(5))
       
  2227 prefer 2
       
  2228 apply(subst (asm) (5) POSIX_def)
       
  2229 apply(auto)[1]
       
  2230 apply(rotate_tac 5)
       
  2231 apply(erule Prf.cases)
       
  2232 apply(simp_all)[5]
       
  2233 apply(rule ValOrd.intros)
       
  2234 apply(subst (asm) v4)
       
  2235 apply(simp)
       
  2236 apply(drule_tac x="Left (projval r1a c v1)" in spec)
       
  2237 apply(clarify)
       
  2238 apply(drule mp)
       
  2239 apply(rule conjI)
       
  2240 apply (metis Prf.intros(2) v3_proj)
       
  2241 apply(simp)
       
  2242 apply (metis v4_proj2)
       
  2243 apply(erule ValOrd.cases)
       
  2244 apply(simp_all)[8]
       
  2245 apply (metis less_not_refl v4_proj2)
       
  2246 (* seq case *)
       
  2247 apply(case_tac "nullable r1")
       
  2248 defer
       
  2249 apply(simp add: POSIX_def)
       
  2250 apply(auto)[1]
       
  2251 apply(erule Prf.cases)
       
  2252 apply(simp_all)[5]
       
  2253 apply (metis Prf.intros(1) v3)
       
  2254 apply(erule Prf.cases)
       
  2255 apply(simp_all)[5]
       
  2256 apply(erule Prf.cases)
       
  2257 apply(simp_all)[5]
       
  2258 apply(clarify)
       
  2259 apply(subst (asm) (3) v4)
       
  2260 apply(simp)
       
  2261 apply(simp)
       
  2262 apply(subgoal_tac "flat v1a \<noteq> []")
       
  2263 prefer 2
       
  2264 apply (metis Prf_flat_L nullable_correctness)
       
  2265 apply(subgoal_tac "\<exists>s. flat v1a = c # s")
       
  2266 prefer 2
       
  2267 apply (metis append_eq_Cons_conv)
       
  2268 apply(auto)[1]
       
  2269 oops
       
  2270 
       
  2271 
       
  2272 lemma POSIX_ex: "\<turnstile> v : r \<Longrightarrow> \<exists>v. POSIX v r"
       
  2273 apply(induct r arbitrary: v)
       
  2274 apply(erule Prf.cases)
       
  2275 apply(simp_all)[5]
       
  2276 apply(erule Prf.cases)
       
  2277 apply(simp_all)[5]
       
  2278 apply(rule_tac x="Void" in exI)
       
  2279 apply(simp add: POSIX_def)
       
  2280 apply(auto)[1]
       
  2281 apply (metis Prf.intros(4))
       
  2282 apply(erule Prf.cases)
       
  2283 apply(simp_all)[5]
       
  2284 apply (metis ValOrd.intros(7))
       
  2285 apply(erule Prf.cases)
       
  2286 apply(simp_all)[5]
       
  2287 apply(rule_tac x="Char c" in exI)
       
  2288 apply(simp add: POSIX_def)
       
  2289 apply(auto)[1]
       
  2290 apply (metis Prf.intros(5))
       
  2291 apply(erule Prf.cases)
       
  2292 apply(simp_all)[5]
       
  2293 apply (metis ValOrd.intros(8))
       
  2294 apply(erule Prf.cases)
       
  2295 apply(simp_all)[5]
       
  2296 apply(auto)[1]
       
  2297 apply(drule_tac x="v1" in meta_spec)
       
  2298 apply(drule_tac x="v2" in meta_spec)
       
  2299 apply(auto)[1]
       
  2300 defer
       
  2301 apply(erule Prf.cases)
       
  2302 apply(simp_all)[5]
       
  2303 apply(auto)[1]
       
  2304 apply (metis POSIX_ALT_I1)
       
  2305 apply (metis POSIX_ALT_I1 POSIX_ALT_I2)
       
  2306 apply(case_tac "nullable r1a")
       
  2307 apply(rule_tac x="Seq (mkeps r1a) va" in exI)
       
  2308 apply(auto simp add: POSIX_def)[1]
       
  2309 apply (metis Prf.intros(1) mkeps_nullable)
       
  2310 apply(simp add: mkeps_flat)
       
  2311 apply(rotate_tac 7)
       
  2312 apply(erule Prf.cases)
       
  2313 apply(simp_all)[5]
       
  2314 apply(case_tac "mkeps r1 = v1a")
       
  2315 apply(simp)
       
  2316 apply (rule ValOrd.intros(1))
       
  2317 apply (metis append_Nil mkeps_flat)
       
  2318 apply (rule ValOrd.intros(2))
       
  2319 apply(drule mkeps_POSIX)
       
  2320 apply(simp add: POSIX_def)
       
  2321 oops
       
  2322 
       
  2323 lemma POSIX_ex2: "\<turnstile> v : r \<Longrightarrow> \<exists>v. POSIX v r \<and> \<turnstile> v : r"
       
  2324 apply(induct r arbitrary: v)
       
  2325 apply(erule Prf.cases)
       
  2326 apply(simp_all)[5]
       
  2327 apply(erule Prf.cases)
       
  2328 apply(simp_all)[5]
       
  2329 apply(rule_tac x="Void" in exI)
       
  2330 apply(simp add: POSIX_def)
       
  2331 apply(auto)[1]
       
  2332 oops
       
  2333 
       
  2334 lemma POSIX_ALT_cases:
       
  2335   assumes "\<turnstile> v : (ALT r1 r2)" "POSIX v (ALT r1 r2)"
       
  2336   shows "(\<exists>v1. v = Left v1 \<and> POSIX v1 r1) \<or> (\<exists>v2. v = Right v2 \<and> POSIX v2 r2)"
       
  2337 using assms
       
  2338 apply(erule_tac Prf.cases)
       
  2339 apply(simp_all)
       
  2340 unfolding POSIX_def
       
  2341 apply(auto)
       
  2342 apply (metis POSIX_ALT2 POSIX_def assms(2))
       
  2343 by (metis POSIX_ALT1b assms(2))
       
  2344 
       
  2345 lemma POSIX_ALT_cases2:
       
  2346   assumes "POSIX v (ALT r1 r2)" "\<turnstile> v : (ALT r1 r2)" 
       
  2347   shows "(\<exists>v1. v = Left v1 \<and> POSIX v1 r1) \<or> (\<exists>v2. v = Right v2 \<and> POSIX v2 r2)"
       
  2348 using assms POSIX_ALT_cases by auto
       
  2349 
       
  2350 lemma Prf_flat_empty:
       
  2351   assumes "\<turnstile> v : r" "flat v = []"
       
  2352   shows "nullable r"
       
  2353 using assms
       
  2354 apply(induct)
       
  2355 apply(auto)
       
  2356 done
       
  2357 
       
  2358 lemma POSIX_proj:
       
  2359   assumes "POSIX v r" "\<turnstile> v : r" "\<exists>s. flat v = c#s"
       
  2360   shows "POSIX (projval r c v) (der c r)"
       
  2361 using assms
       
  2362 apply(induct r c v arbitrary: rule: projval.induct)
       
  2363 defer
       
  2364 defer
       
  2365 defer
       
  2366 defer
       
  2367 apply(erule Prf.cases)
       
  2368 apply(simp_all)[5]
       
  2369 apply(erule Prf.cases)
       
  2370 apply(simp_all)[5]
       
  2371 apply(erule Prf.cases)
       
  2372 apply(simp_all)[5]
       
  2373 apply(erule Prf.cases)
       
  2374 apply(simp_all)[5]
       
  2375 apply(erule Prf.cases)
       
  2376 apply(simp_all)[5]
       
  2377 apply(erule Prf.cases)
       
  2378 apply(simp_all)[5]
       
  2379 apply(erule Prf.cases)
       
  2380 apply(simp_all)[5]
       
  2381 apply(erule Prf.cases)
       
  2382 apply(simp_all)[5]
       
  2383 apply(erule Prf.cases)
       
  2384 apply(simp_all)[5]
       
  2385 apply(erule Prf.cases)
       
  2386 apply(simp_all)[5]
       
  2387 apply(simp add: POSIX_def)
       
  2388 apply(auto)[1]
       
  2389 apply(erule Prf.cases)
       
  2390 apply(simp_all)[5]
       
  2391 oops
       
  2392 
       
  2393 lemma POSIX_proj:
       
  2394   assumes "POSIX v r" "\<turnstile> v : r" "\<exists>s. flat v = c#s"
       
  2395   shows "POSIX (projval r c v) (der c r)"
       
  2396 using assms
       
  2397 apply(induct r arbitrary: c v rule: rexp.induct)
       
  2398 apply(erule Prf.cases)
       
  2399 apply(simp_all)[5]
       
  2400 apply(erule Prf.cases)
       
  2401 apply(simp_all)[5]
       
  2402 apply(erule Prf.cases)
       
  2403 apply(simp_all)[5]
       
  2404 apply(simp add: POSIX_def)
       
  2405 apply(auto)[1]
       
  2406 apply(erule Prf.cases)
       
  2407 apply(simp_all)[5]
       
  2408 oops
       
  2409 
       
  2410 lemma POSIX_proj:
       
  2411   assumes "POSIX v r" "\<turnstile> v : r" "\<exists>s. flat v = c#s"
       
  2412   shows "POSIX (projval r c v) (der c r)"
       
  2413 using assms
       
  2414 apply(induct r c v arbitrary: rule: projval.induct)
       
  2415 defer
       
  2416 defer
       
  2417 defer
       
  2418 defer
       
  2419 apply(erule Prf.cases)
       
  2420 apply(simp_all)[5]
       
  2421 apply(erule Prf.cases)
       
  2422 apply(simp_all)[5]
       
  2423 apply(erule Prf.cases)
       
  2424 apply(simp_all)[5]
       
  2425 apply(erule Prf.cases)
       
  2426 apply(simp_all)[5]
       
  2427 apply(erule Prf.cases)
       
  2428 apply(simp_all)[5]
       
  2429 apply(erule Prf.cases)
       
  2430 apply(simp_all)[5]
       
  2431 apply(erule Prf.cases)
       
  2432 apply(simp_all)[5]
       
  2433 apply(erule Prf.cases)
       
  2434 apply(simp_all)[5]
       
  2435 apply(erule Prf.cases)
       
  2436 apply(simp_all)[5]
       
  2437 apply(erule Prf.cases)
       
  2438 apply(simp_all)[5]
       
  2439 apply(simp add: POSIX_def)
       
  2440 apply(auto)[1]
       
  2441 apply(erule Prf.cases)
       
  2442 apply(simp_all)[5]
       
  2443 oops
       
  2444 
       
  2445 lemma Prf_inj:
       
  2446   assumes "v1 \<succ>(der c r) v2" "\<turnstile> v1 : der c r" "\<turnstile> v2 : der c r" "flat v1 = flat v2"
       
  2447   shows "(injval r c v1) \<succ>r (injval r c v2)"
       
  2448 using assms
       
  2449 apply(induct arbitrary: v1 v2 rule: der.induct)
       
  2450 (* NULL case *)
       
  2451 apply(simp)
       
  2452 apply(erule ValOrd.cases)
       
  2453 apply(simp_all)[8]
       
  2454 (* EMPTY case *)
       
  2455 apply(erule ValOrd.cases)
       
  2456 apply(simp_all)[8]
       
  2457 (* CHAR case *)
       
  2458 apply(case_tac "c = c'")
       
  2459 apply(simp)
       
  2460 apply(erule ValOrd.cases)
       
  2461 apply(simp_all)[8]
       
  2462 apply(rule ValOrd.intros)
       
  2463 apply(simp)
       
  2464 apply(erule ValOrd.cases)
       
  2465 apply(simp_all)[8]
       
  2466 (* ALT case *)
       
  2467 apply(simp)
       
  2468 apply(erule ValOrd.cases)
       
  2469 apply(simp_all)[8]
       
  2470 apply(rule ValOrd.intros)
       
  2471 apply(subst v4)
       
  2472 apply(clarify)
       
  2473 apply(rotate_tac 3)
       
  2474 apply(erule Prf.cases)
       
  2475 apply(simp_all)[5]
       
  2476 apply(subst v4)
       
  2477 apply(clarify)
       
  2478 apply(rotate_tac 2)
       
  2479 apply(erule Prf.cases)
       
  2480 apply(simp_all)[5]
       
  2481 apply(simp)
       
  2482 apply(rule ValOrd.intros)
       
  2483 apply(clarify)
       
  2484 apply(rotate_tac 3)
       
  2485 apply(erule Prf.cases)
       
  2486 apply(simp_all)[5]
       
  2487 apply(clarify)
       
  2488 apply(erule Prf.cases)
       
  2489 apply(simp_all)[5]
       
  2490 apply(rule ValOrd.intros)
       
  2491 apply(clarify)
       
  2492 apply(erule Prf.cases)
       
  2493 apply(simp_all)[5]
       
  2494 apply(erule Prf.cases)
       
  2495 apply(simp_all)[5]
       
  2496 (* SEQ case*)
       
  2497 apply(simp)
       
  2498 apply(case_tac "nullable r1")
       
  2499 defer
       
  2500 apply(simp)
       
  2501 apply(erule ValOrd.cases)
       
  2502 apply(simp_all)[8]
       
  2503 apply(clarify)
       
  2504 apply(erule Prf.cases)
       
  2505 apply(simp_all)[5]
       
  2506 apply(erule Prf.cases)
       
  2507 apply(simp_all)[5]
       
  2508 apply(clarify)
       
  2509 apply(rule ValOrd.intros)
       
  2510 apply(simp)
       
  2511 oops
       
  2512 
       
  2513 
       
  2514 text {*
       
  2515   Injection followed by projection is the identity.
       
  2516 *}
       
  2517 
       
  2518 lemma proj_inj_id:
       
  2519   assumes "\<turnstile> v : der c r" 
       
  2520   shows "projval r c (injval r c v) = v"
       
  2521 using assms
       
  2522 apply(induct r arbitrary: c v rule: rexp.induct)
       
  2523 apply(simp)
       
  2524 apply(erule Prf.cases)
       
  2525 apply(simp_all)[5]
       
  2526 apply(simp)
       
  2527 apply(erule Prf.cases)
       
  2528 apply(simp_all)[5]
       
  2529 apply(simp)
       
  2530 apply(case_tac "c = char")
       
  2531 apply(simp)
       
  2532 apply(erule Prf.cases)
       
  2533 apply(simp_all)[5]
       
  2534 apply(simp)
       
  2535 apply(erule Prf.cases)
       
  2536 apply(simp_all)[5]
       
  2537 defer
       
  2538 apply(simp)
       
  2539 apply(erule Prf.cases)
       
  2540 apply(simp_all)[5]
       
  2541 apply(simp)
       
  2542 apply(case_tac "nullable rexp1")
       
  2543 apply(simp)
       
  2544 apply(erule Prf.cases)
       
  2545 apply(simp_all)[5]
       
  2546 apply(auto)[1]
       
  2547 apply(erule Prf.cases)
       
  2548 apply(simp_all)[5]
       
  2549 apply(auto)[1]
       
  2550 apply (metis list.distinct(1) v4)
       
  2551 apply(auto)[1]
       
  2552 apply (metis mkeps_flat)
       
  2553 apply(auto)
       
  2554 apply(erule Prf.cases)
       
  2555 apply(simp_all)[5]
       
  2556 apply(auto)[1]
       
  2557 apply(simp add: v4)
       
  2558 done
       
  2559 
       
  2560 text {* 
       
  2561 
       
  2562   HERE: Crucial lemma that does not go through in the sequence case. 
       
  2563 
       
  2564 *}
       
  2565 lemma v5:
       
  2566   assumes "\<turnstile> v : der c r" "POSIX v (der c r)"
       
  2567   shows "POSIX (injval r c v) r"
       
  2568 using assms
       
  2569 apply(induct arbitrary: v rule: der.induct)
       
  2570 (* NULL case *)
       
  2571 apply(simp)
       
  2572 apply(erule Prf.cases)
       
  2573 apply(simp_all)[5]
       
  2574 (* EMPTY case *)
       
  2575 apply(simp)
       
  2576 apply(erule Prf.cases)
       
  2577 apply(simp_all)[5]
       
  2578 (* CHAR case *)
       
  2579 apply(simp)
       
  2580 apply(case_tac "c = c'")
       
  2581 apply(auto simp add: POSIX_def)[1]
       
  2582 apply(erule Prf.cases)
       
  2583 apply(simp_all)[5]
       
  2584 oops