thys/Re1.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 08 Jun 2015 14:37:19 +0100
changeset 78 279d0bc48308
parent 77 4b4c677501e7
child 79 ca8f9645db69
permissions -rw-r--r--
updated the Isabelle theories with the totality proof

   
theory Re1
  imports "Main" 
begin

section {* Sequential Composition of Sets *}

definition
  Sequ :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
where 
  "A ;; B = {s1 @ s2 | s1 s2. s1 \<in> A \<and> s2 \<in> B}"

text {* Two Simple Properties about Sequential Composition *}

lemma seq_empty [simp]:
  shows "A ;; {[]} = A"
  and   "{[]} ;; A = A"
by (simp_all add: Sequ_def)

lemma seq_null [simp]:
  shows "A ;; {} = {}"
  and   "{} ;; A = {}"
by (simp_all add: Sequ_def)

section {* Regular Expressions *}

datatype rexp =
  NULL
| EMPTY
| CHAR char
| SEQ rexp rexp
| ALT rexp rexp

fun SEQS :: "rexp \<Rightarrow> rexp list \<Rightarrow> rexp"
where
  "SEQS r [] = r"
| "SEQS r (r'#rs) = SEQ r (SEQS r' rs)"

section {* Semantics of Regular Expressions *}
 
fun
  L :: "rexp \<Rightarrow> string set"
where
  "L (NULL) = {}"
| "L (EMPTY) = {[]}"
| "L (CHAR c) = {[c]}"
| "L (SEQ r1 r2) = (L r1) ;; (L r2)"
| "L (ALT r1 r2) = (L r1) \<union> (L r2)"

fun
 nullable :: "rexp \<Rightarrow> bool"
where
  "nullable (NULL) = False"
| "nullable (EMPTY) = True"
| "nullable (CHAR c) = False"
| "nullable (ALT r1 r2) = (nullable r1 \<or> nullable r2)"
| "nullable (SEQ r1 r2) = (nullable r1 \<and> nullable r2)"

fun
 nullable_left :: "rexp \<Rightarrow> bool"
where
  "nullable_left (NULL) = False"
| "nullable_left (EMPTY) = True"
| "nullable_left (CHAR c) = False"
| "nullable_left (ALT r1 r2) = (nullable_left r1 \<or> nullable_left r2)"
| "nullable_left (SEQ r1 r2) = nullable_left r1"

lemma nullable_left:
  "nullable r \<Longrightarrow> nullable_left r"
apply(induct r)
apply(auto)
done


value "L(CHAR c)"
value "L(SEQ(CHAR c)(CHAR b))"

lemma nullable_correctness:
  shows "nullable r  \<longleftrightarrow> [] \<in> (L r)"
apply (induct r) 
apply(auto simp add: Sequ_def) 
done

section {* Values *}

datatype val = 
  Void
| Char char
| Seq val val
| Right val
| Left val

fun Seqs :: "val \<Rightarrow> val list \<Rightarrow> val"
where
  "Seqs v [] = v"
| "Seqs v (v'#vs) = Seq v (Seqs v' vs)"


section {* The string behind a value *}

fun flat :: "val \<Rightarrow> string"
where
  "flat(Void) = []"
| "flat(Char c) = [c]"
| "flat(Left v) = flat(v)"
| "flat(Right v) = flat(v)"
| "flat(Seq v1 v2) = flat(v1) @ flat(v2)"

fun flat_left :: "val \<Rightarrow> string"
where
  "flat_left(Void) = []"
| "flat_left(Char c) = [c]"
| "flat_left(Left v) = flat_left(v)"
| "flat_left(Right v) = flat_left(v)"
| "flat_left(Seq v1 v2) = flat_left(v1)"

fun flat_right :: "val \<Rightarrow> string"
where
  "flat_right(Void) = []"
| "flat_right(Char c) = [c]"
| "flat_right(Left v) = flat(v)"
| "flat_right(Right v) = flat(v)"
| "flat_right(Seq v1 v2) = flat(v2)"

fun head :: "val \<Rightarrow> string"
where
  "head(Void) = []"
| "head(Char c) = [c]"
| "head(Left v) = head(v)"
| "head(Right v) = head(v)"
| "head(Seq v1 v2) = head v1"

fun flats :: "val \<Rightarrow> string list"
where
  "flats(Void) = [[]]"
| "flats(Char c) = [[c]]"
| "flats(Left v) = flats(v)"
| "flats(Right v) = flats(v)"
| "flats(Seq v1 v2) = (flats v1) @ (flats v2)"

value "flats(Seq(Char c)(Char b))"

section {* Relation between values and regular expressions *}

inductive Prfs :: "string \<Rightarrow> val \<Rightarrow> rexp \<Rightarrow> bool" ("\<Turnstile>_ _ : _" [100, 100, 100] 100)
where
 "\<lbrakk>\<Turnstile>s1 v1 : r1; \<Turnstile>s2 v2 : r2\<rbrakk> \<Longrightarrow> \<Turnstile>(s1 @ s2) (Seq v1 v2) : SEQ r1 r2"
| "\<Turnstile>s v1 : r1 \<Longrightarrow> \<Turnstile>s (Left v1) : ALT r1 r2"
| "\<Turnstile>s v2 : r2 \<Longrightarrow> \<Turnstile>s (Right v2) : ALT r1 r2"
| "\<Turnstile>[] Void : EMPTY"
| "\<Turnstile>[c] (Char c) : CHAR c"

lemma Prfs_flat:
  "\<Turnstile>s v : r \<Longrightarrow> flat v = s"
apply(induct s v r rule: Prfs.induct)
apply(auto)
done


inductive Prfn :: "nat \<Rightarrow> val \<Rightarrow> rexp \<Rightarrow> bool" ("\<TTurnstile>_ _ : _" [100, 100, 100] 100)
where
 "\<lbrakk>\<TTurnstile>n1 v1 : r1; \<TTurnstile>n2 v2 : r2\<rbrakk> \<Longrightarrow> \<TTurnstile>(n1 + n2) (Seq v1 v2) : SEQ r1 r2"
| "\<TTurnstile>n v1 : r1 \<Longrightarrow> \<TTurnstile>n (Left v1) : ALT r1 r2"
| "\<TTurnstile>n v2 : r2 \<Longrightarrow> \<TTurnstile>n (Right v2) : ALT r1 r2"
| "\<TTurnstile>0 Void : EMPTY"
| "\<TTurnstile>1 (Char c) : CHAR c"

lemma Prfn_flat:
  "\<TTurnstile>n v : r \<Longrightarrow> length(flat v) = n"
apply(induct rule: Prfn.induct)
apply(auto)
done

inductive Prf :: "val \<Rightarrow> rexp \<Rightarrow> bool" ("\<turnstile> _ : _" [100, 100] 100)
where
 "\<lbrakk>\<turnstile> v1 : r1; \<turnstile> v2 : r2\<rbrakk> \<Longrightarrow> \<turnstile> Seq v1 v2 : SEQ r1 r2"
| "\<turnstile> v1 : r1 \<Longrightarrow> \<turnstile> Left v1 : ALT r1 r2"
| "\<turnstile> v2 : r2 \<Longrightarrow> \<turnstile> Right v2 : ALT r1 r2"
| "\<turnstile> Void : EMPTY"
| "\<turnstile> Char c : CHAR c"

lemma Prf_Prfn:
  shows "\<turnstile> v : r \<Longrightarrow> \<TTurnstile>(length (flat v)) v : r"
apply(induct v r rule: Prf.induct)
apply(auto intro: Prfn.intros)
by (metis One_nat_def Prfn.intros(5))

lemma Prfn_Prf:
  shows "\<TTurnstile>n v : r \<Longrightarrow> \<turnstile> v : r"
apply(induct n v r rule: Prfn.induct)
apply(auto intro: Prf.intros)
done

lemma Prf_Prfs:
  shows "\<turnstile> v : r \<Longrightarrow> \<Turnstile>(flat v) v : r"
apply(induct v r rule: Prf.induct)
apply(auto intro: Prfs.intros)
done

lemma Prfs_Prf:
  shows "\<Turnstile>s v : r \<Longrightarrow> \<turnstile> v : r"
apply(induct s v r rule: Prfs.induct)
apply(auto intro: Prf.intros)
done

fun mkeps :: "rexp \<Rightarrow> val"
where
  "mkeps(EMPTY) = Void"
| "mkeps(SEQ r1 r2) = Seq (mkeps r1) (mkeps r2)"
| "mkeps(ALT r1 r2) = (if nullable(r1) then Left (mkeps r1) else Right (mkeps r2))"

lemma mkeps_nullable:
  assumes "nullable(r)" shows "\<turnstile> mkeps r : r"
using assms
apply(induct rule: nullable.induct)
apply(auto intro: Prf.intros)
done

lemma mkeps_nullable_n:
  assumes "nullable(r)" shows "\<TTurnstile>0 (mkeps r) : r"
using assms
apply(induct rule: nullable.induct)
apply(auto intro: Prfn.intros)
apply(drule Prfn.intros(1))
apply(assumption)
apply(simp)
done

lemma mkeps_nullable_s:
  assumes "nullable(r)" shows "\<Turnstile>[] (mkeps r) : r"
using assms
apply(induct rule: nullable.induct)
apply(auto intro: Prfs.intros)
apply(drule Prfs.intros(1))
apply(assumption)
apply(simp)
done

lemma mkeps_flat:
  assumes "nullable(r)" shows "flat (mkeps r) = []"
using assms
apply(induct rule: nullable.induct)
apply(auto)
done

lemma mkeps_flat_left:
  assumes "nullable(r)" shows "flat_left (mkeps r) = []"
using assms
apply(induct rule: nullable.induct)
apply(auto)
done

text {*
  The value mkeps returns is always the correct POSIX
  value.
*}

lemma Prf_flat_L:
  assumes "\<turnstile> v : r" shows "flat v \<in> L r"
using assms
apply(induct v r rule: Prf.induct)
apply(auto simp add: Sequ_def)
done

lemma L_flat_Prf:
  "L(r) = {flat v | v. \<turnstile> v : r}"
apply(induct r)
apply(auto dest: Prf_flat_L simp add: Sequ_def)
apply (metis Prf.intros(4) flat.simps(1))
apply (metis Prf.intros(5) flat.simps(2))
apply (metis Prf.intros(1) flat.simps(5))
apply (metis Prf.intros(2) flat.simps(3))
apply (metis Prf.intros(3) flat.simps(4))
apply(erule Prf.cases)
apply(auto)
done

definition prefix :: "string \<Rightarrow> string \<Rightarrow> bool" ("_ \<sqsubset> _" [100, 100] 100)
where
  "s1 \<sqsubset> s2 \<equiv> \<exists>s3. s1 @ s3 = s2"

section {* Ordering of values *}

inductive ValOrd :: "val \<Rightarrow> rexp \<Rightarrow> val \<Rightarrow> bool" ("_ \<succ>_ _" [100, 100, 100] 100)
where
  "v2 \<succ>r2 v2' \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1 v2')" 
| "\<lbrakk>v1 \<succ>r1 v1'; v1 \<noteq> v1'\<rbrakk> \<Longrightarrow> (Seq v1 v2) \<succ>(SEQ r1 r2) (Seq v1' v2')" 
| "length (flat v1) \<ge> length (flat v2) \<Longrightarrow> (Left v1) \<succ>(ALT r1 r2) (Right v2)"
| "length (flat v2) > length (flat v1) \<Longrightarrow> (Right v2) \<succ>(ALT r1 r2) (Left v1)"
| "v2 \<succ>r2 v2' \<Longrightarrow> (Right v2) \<succ>(ALT r1 r2) (Right v2')"
| "v1 \<succ>r1 v1' \<Longrightarrow> (Left v1) \<succ>(ALT r1 r2) (Left v1')"
| "Void \<succ>EMPTY Void"
| "(Char c) \<succ>(CHAR c) (Char c)"

inductive ValOrd2 :: "val \<Rightarrow> val \<Rightarrow> bool" ("_ 2\<succ> _" [100, 100] 100)
where
  "v2 2\<succ> v2' \<Longrightarrow> (Seq v1 v2) 2\<succ> (Seq v1 v2')" 
| "\<lbrakk>v1 2\<succ> v1'; v1 \<noteq> v1'\<rbrakk> \<Longrightarrow> (Seq v1 v2) 2\<succ> (Seq v1' v2')" 
| "length (flat v1) \<ge> length (flat v2) \<Longrightarrow> (Left v1) 2\<succ> (Right v2)"
| "length (flat v2) > length (flat v1) \<Longrightarrow> (Right v2) 2\<succ> (Left v1)"
| "v2 2\<succ> v2' \<Longrightarrow> (Right v2) 2\<succ> (Right v2')"
| "v1 2\<succ> v1' \<Longrightarrow> (Left v1) 2\<succ> (Left v1')"
| "Void 2\<succ> Void"
| "(Char c) 2\<succ> (Char c)"

lemma Ord1:
  "v1 \<succ>r v2 \<Longrightarrow> v1 2\<succ> v2"
apply(induct rule: ValOrd.induct)
apply(auto intro: ValOrd2.intros)
done

lemma Ord2:
  "v1 2\<succ> v2 \<Longrightarrow> \<exists>r. v1 \<succ>r v2"
apply(induct v1 v2 rule: ValOrd2.induct)
apply(auto intro: ValOrd.intros)
done

lemma Ord3:
  "\<lbrakk>v1 2\<succ> v2; \<turnstile> v1 : r\<rbrakk> \<Longrightarrow> v1 \<succ>r v2"
apply(induct v1 v2 arbitrary: r rule: ValOrd2.induct)
apply(auto intro: ValOrd.intros elim: Prf.cases)
done


lemma ValOrd_refl:
  assumes "\<turnstile> v : r"
  shows "v \<succ>r v"
using assms
apply(induct)
apply(auto intro: ValOrd.intros)
done

lemma 
  "flat Void = []"
  "flat (Seq Void Void) = []"
apply(simp_all)
done


lemma ValOrd_total:
  shows "\<lbrakk>\<turnstile> v1 : r; \<turnstile> v2 : r\<rbrakk>  \<Longrightarrow> v1 \<succ>r v2 \<or> v2 \<succ>r v1"
apply(induct r arbitrary: v1 v2)
apply(auto)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(7))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(8))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(case_tac "v1a = v1b")
apply(simp)
apply(rule ValOrd.intros(1))
apply (metis ValOrd.intros(1))
apply(rule ValOrd.intros(2))
apply(auto)[2]
apply(erule contrapos_np)
apply(rule ValOrd.intros(2))
apply(auto)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis Ord1 Ord3 Prf.intros(2) ValOrd2.intros(6))
apply(rule ValOrd.intros)
apply(erule contrapos_np)
apply(rule ValOrd.intros)
apply (metis le_eq_less_or_eq neq_iff)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule ValOrd.intros)
apply(erule contrapos_np)
apply(rule ValOrd.intros)
apply (metis le_eq_less_or_eq neq_iff)
apply(rule ValOrd.intros)
apply(erule contrapos_np)
apply(rule ValOrd.intros)
by metis

lemma ValOrd_anti:
  shows "\<lbrakk>\<turnstile> v1 : r; \<turnstile> v2 : r; v1 \<succ>r v2; v2 \<succ>r v1\<rbrakk> \<Longrightarrow> v1 = v2"
apply(induct r arbitrary: v1 v2)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
done

lemma
  "s \<in> L r \<longrightarrow> (\<exists>vmax \<in> {v. \<turnstile> v : r \<and> flat v = s}. \<forall>v \<in> {v. \<turnstile> v : r \<and> flat v = s}. vmax 2\<succ> v)"
apply(induct s arbitrary: r rule: length_induct)
apply(induct_tac r rule: rexp.induct)
apply(rule impI)
apply(simp)
apply(simp)
apply(rule impI)
apply(simp)
apply(rule_tac x="Void" in exI)
apply(simp)
apply(rule conjI)
apply (metis Prf.intros(4))
apply(rule allI)
apply(rule impI)
apply(erule conjE)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd2.intros(7))
apply(simp)
apply(rule impI)
apply(rule_tac x="Char char" in exI)
apply(simp)
apply(rule conjI)
apply (metis Prf.intros)
apply(rule allI)
apply(rule impI)
apply(erule conjE)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd2.intros(8))
apply(simp)
apply(rule impI)
apply(simp add: Sequ_def)[1]
apply(erule exE)+
apply(erule conjE)+
apply(clarify)
defer
apply(simp)
apply(rule conjI)
apply(rule impI)
apply(simp)
apply(erule exE)
apply(clarify)
apply(rule_tac x="Left vmax" in exI)
apply(rule conjI)
apply (metis Prf.intros(2))
apply(simp)
apply(rule allI)
apply(rule impI)
apply(erule conjE)
apply(rotate_tac 5)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd2.intros(6))
apply(clarify)
apply (metis ValOrd2.intros(3) order_refl)
apply(rule impI)
apply(simp)
apply(erule exE)
apply(clarify)
apply(rule_tac x="Right vmax" in exI)
apply(simp)
apply(rule conjI)
apply (metis Prf.intros(3))
apply(rule allI)
apply(rule impI)
apply(erule conjE)+
apply(rotate_tac 5)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd2.intros(8))
apply(simp)
apply(rule impI)
apply(simp add: Sequ_def)[1]
apply(erule exE)+
apply(erule conjE)+
apply(clarify)


inductive ValOrd3 :: "val \<Rightarrow> rexp \<Rightarrow> val \<Rightarrow> bool" ("_ 3\<succ>_ _" [100, 100, 100] 100)
where
  "\<lbrakk>v2 3\<succ>r2 v2'; \<turnstile> v1 : r1\<rbrakk> \<Longrightarrow> (Seq v1 v2) 3\<succ>(SEQ r1 r2) (Seq v1 v2')" 
| "\<lbrakk>v1 3\<succ>r1 v1'; v1 \<noteq> v1'; flat v2 = flat v2'; \<turnstile> v2 : r2; \<turnstile> v2' : r2\<rbrakk> 
      \<Longrightarrow> (Seq v1 v2) 3\<succ>(SEQ r1 r2) (Seq v1' v2')" 
| "length (flat v1) \<ge> length (flat v2) \<Longrightarrow> (Left v1) 3\<succ>(ALT r1 r2) (Right v2)"
| "length (flat v2) > length (flat v1) \<Longrightarrow> (Right v2) 3\<succ>(ALT r1 r2) (Left v1)"
| "v2 3\<succ>r2 v2' \<Longrightarrow> (Right v2) 3\<succ>(ALT r1 r2) (Right v2')"
| "v1 3\<succ>r1 v1' \<Longrightarrow> (Left v1) 3\<succ>(ALT r1 r2) (Left v1')"
| "Void 3\<succ>EMPTY Void"
| "(Char c) 3\<succ>(CHAR c) (Char c)"


lemma "v1 3\<succ>r v2 \<Longrightarrow> v1 \<succ>r v2 \<and> \<turnstile> v1 : r \<and> \<turnstile> v2 : r \<and> flat v1 = flat v2"
apply(induct rule: ValOrd3.induct)
prefer 8
apply(simp)
apply (metis Prf.intros(5) ValOrd.intros(8))
prefer 7
apply(simp)
apply (metis Prf.intros(4) ValOrd.intros(7))
apply(simp)
apply (metis Prf.intros(1) ValOrd.intros(1))
apply(simp)




lemma ValOrd_trans:
  assumes "v1 \<succ>r v2" "v2 \<succ>r v3" "\<turnstile> v1 : r" "\<turnstile> v2 : r" "\<turnstile> v3 : r" 
     "flat v1 = flat v2" "flat v2 = flat v3"
  shows "v1 \<succ>r v3"
using assms
apply(induct r arbitrary: v1 v2 v3 s1 s2 s3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply (metis ValOrd.intros(1))
apply(clarify)
apply (metis ValOrd.intros(2))
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply (metis ValOrd.intros(2))
apply(clarify)
apply(case_tac "v1d = v1'a")
apply(simp)
apply (metis ValOrd_anti)
apply (rule ValOrd.intros(2))
prefer 2
apply(auto)[1]
apply metis
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply (metis Ord1 Ord3 Prf.intros(2) ValOrd2.intros(6))
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply (rule ValOrd.intros)
apply(clarify)
oops


lemma ValOrd_max:
  shows "L r \<noteq> {} \<Longrightarrow> \<exists>v. \<turnstile> v : r \<and> (\<forall>v'. (\<turnstile> v' : r \<longrightarrow> v' \<succ>r v \<longrightarrow> v = v'))" 
using assms
apply(induct r)
apply(auto)[3]
apply(rule_tac x="Void" in exI)
apply(rule conjI)
apply (metis Prf.intros(4))
apply(rule allI)
apply(rule impI)+
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(rule_tac x="Char char" in exI)
apply(rule conjI)
apply (metis Prf.intros(5))
apply(rule allI)
apply(rule impI)+
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(simp add: L.simps)
apply(auto simp: Sequ_def)[1]
apply(drule meta_mp)
apply (metis empty_iff)
apply(drule meta_mp)
apply (metis empty_iff)
apply(erule exE)+
apply(rule_tac x="Seq v va" in exI)
apply(rule conjI)
apply (metis Prf.intros(1))
apply(rule allI)
apply(rule impI)+
apply(rotate_tac 4)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply metis
apply(simp only: L.simps Lattices.bounded_lattice_bot_class.sup_eq_bot_iff HOL.de_Morgan_conj)
apply(erule disjE)
apply(drule meta_mp)
apply (metis empty_iff)
apply(erule exE)+
apply(rule exI)
apply(rule conjI)
apply(rule Prf.intros)
apply(erule conjE)+
apply(assumption)
apply(rule allI)
apply(rule impI)+
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(drule meta_mp)
apply (metis Prf_flat_L empty_iff)
apply(auto)[1]
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
oops

section {* Posix definition *}

definition POSIX :: "val \<Rightarrow> rexp \<Rightarrow> bool" 
where
  "POSIX v r \<equiv> (\<turnstile> v : r \<and> (\<forall>v'. (\<turnstile> v' : r \<and> flat v = flat v') \<longrightarrow> v \<succ>r v'))"

definition POSIX2 :: "val \<Rightarrow> rexp \<Rightarrow> bool" 
where
  "POSIX2 v r \<equiv> (\<turnstile> v : r \<and> (\<forall>v'. (\<turnstile> v' : r \<and> flat v = flat v') \<longrightarrow> v 2\<succ> v'))"

lemma "POSIX v r = POSIX2 v r"
unfolding POSIX_def POSIX2_def
apply(auto)
apply(rule Ord1)
apply(auto)
apply(rule Ord3)
apply(auto)
done

definition POSIXs :: "val \<Rightarrow> rexp \<Rightarrow> string \<Rightarrow> bool" 
where
  "POSIXs v r s \<equiv> (\<Turnstile>s v : r \<and> (\<forall>v'. (\<Turnstile>s v' : r \<longrightarrow> v 2\<succ> v')))"

definition POSIXn :: "val \<Rightarrow> rexp \<Rightarrow> nat \<Rightarrow> bool" 
where
  "POSIXn v r n \<equiv> (\<TTurnstile>n v : r \<and> (\<forall>v'. (\<TTurnstile>n v' : r \<longrightarrow> v 2\<succ> v')))"

lemma "POSIXn v r (length (flat v)) \<Longrightarrow> POSIX2 v r"
unfolding POSIXn_def POSIX2_def
apply(auto)
apply (metis Prfn_Prf)
by (metis Prf_Prfn)

lemma Prfs_POSIX:
  "POSIXs v r s \<Longrightarrow> \<Turnstile>s v: r \<and> flat v = s"
apply(simp add: POSIXs_def)
by (metis Prfs_flat)


lemma "POSIXs v r (flat v) =  POSIX2 v r"
unfolding POSIXs_def POSIX2_def
apply(auto)
apply (metis Prfs_Prf)
apply (metis Prf_Prfs)
apply (metis Prf_Prfs)
by (metis Prfs_Prf Prfs_flat)

lemma 
  assumes "POSIX v1 r1" "\<turnstile> v1' : r1"
  shows "Seq v1 v2 \<succ>(SEQ r1 r2) Seq v1' v2'"
using assms
apply(rule_tac ValOrd.intros)
apply(simp add: POSIX_def)
oops

lemma
"L r \<noteq> {} \<Longrightarrow> \<exists>v. POSIXs v r (flat v)"
apply(induct r arbitrary: )
apply(simp)
apply(rule_tac x="Void" in exI)
apply(simp add: POSIXs_def)
apply(rule conjI)
apply (metis Prfs.intros(4))
apply(auto)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply (metis ValOrd2.intros(7))
apply(simp)
apply(rule_tac x="Char char" in exI)
apply(simp add: POSIXs_def)
apply(rule conjI)
apply (metis Prfs.intros(5))
apply(auto)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply (metis ValOrd2.intros)
apply(auto simp add: Sequ_def)
apply(drule meta_mp)
apply (metis empty_iff)
apply(drule meta_mp)
apply (metis empty_iff)
apply(auto)
oops

section {* POSIX for some constructors *}

lemma POSIX_SEQ1:
  assumes "POSIX (Seq v1 v2) (SEQ r1 r2)" "\<turnstile> v1 : r1" "\<turnstile> v2 : r2"
  shows "POSIX v1 r1"
using assms
unfolding POSIX_def
apply(auto)
apply(drule_tac x="Seq v' v2" in spec)
apply(simp)
apply(erule impE)
apply(rule Prf.intros)
apply(simp)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)
apply(clarify)
by (metis ValOrd_refl)

lemma POSIXn_SEQ1:
  assumes "POSIXn (Seq v1 v2) (SEQ r1 r2) (n1 + n2)" "\<TTurnstile>n1 v1 : r1" "\<TTurnstile>n2 v2 : r2"
  shows "POSIXn v1 r1 n1"
using assms
unfolding POSIXn_def
apply(auto)
apply(drule_tac x="Seq v' v2" in spec)
apply(erule impE)
apply(rule Prfn.intros)
apply(simp)
apply(simp)
apply(erule ValOrd2.cases)
apply(simp_all)
apply(clarify)
by (metis Ord1 Prfn_Prf ValOrd_refl)

lemma POSIXs_SEQ1:
  assumes "POSIXs (Seq v1 v2) (SEQ r1 r2) (s1 @ s2)" "\<Turnstile>s1 v1 : r1" "\<Turnstile>s2 v2 : r2"
  shows "POSIXs v1 r1 s1"
using assms
unfolding POSIXs_def
apply(auto)
apply(drule_tac x="Seq v' v2" in spec)
apply(erule impE)
apply(rule Prfs.intros)
apply(simp)
apply(simp)
apply(erule ValOrd2.cases)
apply(simp_all)
apply(clarify)
by (metis Ord1 Prfs_Prf ValOrd_refl)

lemma POSIX_SEQ2:
  assumes "POSIX (Seq v1 v2) (SEQ r1 r2)" "\<turnstile> v1 : r1" "\<turnstile> v2 : r2" 
  shows "POSIX v2 r2"
using assms
unfolding POSIX_def
apply(auto)
apply(drule_tac x="Seq v1 v'" in spec)
apply(simp)
apply(erule impE)
apply(rule Prf.intros)
apply(simp)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)
done

lemma POSIXn_SEQ2:
  assumes "POSIXn (Seq v1 v2) (SEQ r1 r2) (n1 + n2)" "\<TTurnstile>n1 v1 : r1" "\<TTurnstile>n2 v2 : r2" 
  shows "POSIXn v2 r2 n2"
using assms
unfolding POSIXn_def
apply(auto)
apply(drule_tac x="Seq v1 v'" in spec)
apply(erule impE)
apply(rule Prfn.intros)
apply(simp)
apply(simp)
apply(erule ValOrd2.cases)
apply(simp_all)
done

lemma POSIXs_SEQ2:
  assumes "POSIXs (Seq v1 v2) (SEQ r1 r2) (s1 @ s2)" "\<Turnstile>s1 v1 : r1" "\<Turnstile>s2 v2 : r2" 
  shows "POSIXs v2 r2 s2"
using assms
unfolding POSIXs_def
apply(auto)
apply(drule_tac x="Seq v1 v'" in spec)
apply(erule impE)
apply(rule Prfs.intros)
apply(simp)
apply(simp)
apply(erule ValOrd2.cases)
apply(simp_all)
done

lemma POSIX_ALT2:
  assumes "POSIX (Left v1) (ALT r1 r2)"
  shows "POSIX v1 r1"
using assms
unfolding POSIX_def
apply(auto)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(drule_tac x="Left v'" in spec)
apply(simp)
apply(drule mp)
apply(rule Prf.intros)
apply(auto)
apply(erule ValOrd.cases)
apply(simp_all)
done

lemma POSIXn_ALT2:
  assumes "POSIXn (Left v1) (ALT r1 r2) n"
  shows "POSIXn v1 r1 n"
using assms
unfolding POSIXn_def
apply(auto)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(drule_tac x="Left v'" in spec)
apply(drule mp)
apply(rule Prfn.intros)
apply(auto)
apply(erule ValOrd2.cases)
apply(simp_all)
done

lemma POSIXs_ALT2:
  assumes "POSIXs (Left v1) (ALT r1 r2) s"
  shows "POSIXs v1 r1 s"
using assms
unfolding POSIXs_def
apply(auto)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(drule_tac x="Left v'" in spec)
apply(drule mp)
apply(rule Prfs.intros)
apply(auto)
apply(erule ValOrd2.cases)
apply(simp_all)
done

lemma POSIX_ALT1a:
  assumes "POSIX (Right v2) (ALT r1 r2)"
  shows "POSIX v2 r2"
using assms
unfolding POSIX_def
apply(auto)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(drule_tac x="Right v'" in spec)
apply(simp)
apply(drule mp)
apply(rule Prf.intros)
apply(auto)
apply(erule ValOrd.cases)
apply(simp_all)
done

lemma POSIXn_ALT1a:
  assumes "POSIXn (Right v2) (ALT r1 r2) n"
  shows "POSIXn v2 r2 n"
using assms
unfolding POSIXn_def
apply(auto)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(drule_tac x="Right v'" in spec)
apply(drule mp)
apply(rule Prfn.intros)
apply(auto)
apply(erule ValOrd2.cases)
apply(simp_all)
done

lemma POSIXs_ALT1a:
  assumes "POSIXs (Right v2) (ALT r1 r2) s"
  shows "POSIXs v2 r2 s"
using assms
unfolding POSIXs_def
apply(auto)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(drule_tac x="Right v'" in spec)
apply(drule mp)
apply(rule Prfs.intros)
apply(auto)
apply(erule ValOrd2.cases)
apply(simp_all)
done

lemma POSIX_ALT1b:
  assumes "POSIX (Right v2) (ALT r1 r2)"
  shows "(\<forall>v'. (\<turnstile> v' : r2 \<and> flat v' = flat v2) \<longrightarrow> v2 \<succ>r2 v')"
using assms
apply(drule_tac POSIX_ALT1a)
unfolding POSIX_def
apply(auto)
done

lemma POSIXn_ALT1b:
  assumes "POSIXn (Right v2) (ALT r1 r2) n"
  shows "(\<forall>v'. (\<TTurnstile>n v' : r2 \<longrightarrow> v2 2\<succ> v'))"
using assms
apply(drule_tac POSIXn_ALT1a)
unfolding POSIXn_def
apply(auto)
done

lemma POSIXs_ALT1b:
  assumes "POSIXs (Right v2) (ALT r1 r2) s"
  shows "(\<forall>v'. (\<Turnstile>s v' : r2 \<longrightarrow> v2 2\<succ> v'))"
using assms
apply(drule_tac POSIXs_ALT1a)
unfolding POSIXs_def
apply(auto)
done

lemma POSIX_ALT_I1:
  assumes "POSIX v1 r1" 
  shows "POSIX (Left v1) (ALT r1 r2)"
using assms
unfolding POSIX_def
apply(auto)
apply (metis Prf.intros(2))
apply(rotate_tac 2)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)
apply(rule ValOrd.intros)
apply(auto)
apply(rule ValOrd.intros)
by simp

lemma POSIXn_ALT_I1:
  assumes "POSIXn v1 r1 n" 
  shows "POSIXn (Left v1) (ALT r1 r2) n"
using assms
unfolding POSIXn_def
apply(auto)
apply (metis Prfn.intros(2))
apply(rotate_tac 2)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(auto)
apply(rule ValOrd2.intros)
apply(auto)
apply(rule ValOrd2.intros)
by (metis Prfn_flat order_refl)

lemma POSIXs_ALT_I1:
  assumes "POSIXs v1 r1 s" 
  shows "POSIXs (Left v1) (ALT r1 r2) s"
using assms
unfolding POSIXs_def
apply(auto)
apply (metis Prfs.intros(2))
apply(rotate_tac 2)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto)
apply(rule ValOrd2.intros)
apply(auto)
apply(rule ValOrd2.intros)
by (metis Prfs_flat order_refl)

lemma POSIX_ALT_I2:
  assumes "POSIX v2 r2" "\<forall>v'. \<turnstile> v' : r1 \<longrightarrow> length (flat v2) > length (flat v')"
  shows "POSIX (Right v2) (ALT r1 r2)"
using assms
unfolding POSIX_def
apply(auto)
apply (metis Prf.intros)
apply(rotate_tac 3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)
apply(rule ValOrd.intros)
apply metis
done

lemma POSIXs_ALT_I2:
  assumes "POSIXs v2 r2 s" "\<forall>s' v'. \<Turnstile>s' v' : r1 \<longrightarrow> length s > length s'"
  shows "POSIXs (Right v2) (ALT r1 r2) s"
using assms
unfolding POSIXs_def
apply(auto)
apply (metis Prfs.intros)
apply(rotate_tac 3)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto)
apply(rule ValOrd2.intros)
apply metis
done


lemma 
  "\<lbrakk>POSIX (mkeps r2) r2; nullable r2; \<not> nullable r1\<rbrakk>
   \<Longrightarrow> POSIX (Right (mkeps r2)) (ALT r1 r2)" 
apply(auto simp add: POSIX_def)
apply(rule Prf.intros(3))
apply(auto)
apply(rotate_tac 3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp add: mkeps_flat)
apply(auto)[1]
apply (metis Prf_flat_L nullable_correctness)
apply(rule ValOrd.intros)
apply(auto)
done

lemma mkeps_POSIX:
  assumes "nullable r"
  shows "POSIX (mkeps r) r"
using assms
apply(induct r)
apply(auto)[1]
apply(simp add: POSIX_def)
apply(auto)[1]
apply (metis Prf.intros(4))
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros)
apply(simp)
apply(auto)[1]
apply(simp add: POSIX_def)
apply(auto)[1]
apply (metis mkeps.simps(2) mkeps_nullable nullable.simps(5))
apply(rotate_tac 6)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (simp add: mkeps_flat)
apply(case_tac "mkeps r1a = v1")
apply(simp)
apply (metis ValOrd.intros(1))
apply (rule ValOrd.intros(2))
apply metis
apply(simp)
(* ALT case *)
thm mkeps.simps
apply(simp)
apply(erule disjE)
apply(simp)
apply (metis POSIX_ALT_I1)
(* *)
apply(auto)[1]
thm  POSIX_ALT_I1
apply (metis POSIX_ALT_I1)
apply(simp (no_asm) add: POSIX_def)
apply(auto)[1]
apply(rule Prf.intros(3))
apply(simp only: POSIX_def)
apply(rotate_tac 4)
apply(erule Prf.cases)
apply(simp_all)[5]
thm mkeps_flat
apply(simp add: mkeps_flat)
apply(auto)[1]
thm Prf_flat_L nullable_correctness
apply (metis Prf_flat_L nullable_correctness)
apply(rule ValOrd.intros)
apply(subst (asm) POSIX_def)
apply(clarify)
apply(drule_tac x="v2" in spec)
by simp


section {* Derivatives *}

fun
 der :: "char \<Rightarrow> rexp \<Rightarrow> rexp"
where
  "der c (NULL) = NULL"
| "der c (EMPTY) = NULL"
| "der c (CHAR c') = (if c = c' then EMPTY else NULL)"
| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)"
| "der c (SEQ r1 r2) = 
     (if nullable r1
      then ALT (SEQ (der c r1) r2) (der c r2)
      else SEQ (der c r1) r2)"

fun 
 ders :: "string \<Rightarrow> rexp \<Rightarrow> rexp"
where
  "ders [] r = r"
| "ders (c # s) r = ders s (der c r)"

section {* Injection function *}

fun injval :: "rexp \<Rightarrow> char \<Rightarrow> val \<Rightarrow> val"
where
  "injval (CHAR d) c Void = Char d"
| "injval (ALT r1 r2) c (Left v1) = Left(injval r1 c v1)"
| "injval (ALT r1 r2) c (Right v2) = Right(injval r2 c v2)"
| "injval (SEQ r1 r2) c (Seq v1 v2) = Seq (injval r1 c v1) v2"
| "injval (SEQ r1 r2) c (Left (Seq v1 v2)) = Seq (injval r1 c v1) v2"
| "injval (SEQ r1 r2) c (Right v2) = Seq (mkeps r1) (injval r2 c v2)"


section {* Projection function *}

fun projval :: "rexp \<Rightarrow> char \<Rightarrow> val \<Rightarrow> val"
where
  "projval (CHAR d) c _ = Void"
| "projval (ALT r1 r2) c (Left v1) = Left (projval r1 c v1)"
| "projval (ALT r1 r2) c (Right v2) = Right (projval r2 c v2)"
| "projval (SEQ r1 r2) c (Seq v1 v2) = 
     (if flat v1 = [] then Right(projval r2 c v2) 
      else if nullable r1 then Left (Seq (projval r1 c v1) v2)
                          else Seq (projval r1 c v1) v2)"

text {*
  Injection value is related to r
*}

lemma v3:
  assumes "\<turnstile> v : der c r" shows "\<turnstile> (injval r c v) : r"
using assms
apply(induct arbitrary: v rule: der.induct)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(case_tac "c = c'")
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis Prf.intros(5))
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis Prf.intros(2))
apply (metis Prf.intros(3))
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply (metis Prf.intros(1))
apply(auto)[1]
apply (metis Prf.intros(1) mkeps_nullable)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(rule Prf.intros)
apply(auto)[2]
done

lemma v3s:
  assumes "\<Turnstile>s v : der c r" shows "\<Turnstile>(c#s) (injval r c v) : r"
using assms
apply(induct arbitrary: s v rule: der.induct)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(case_tac "c = c'")
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply (metis Prfs.intros(5))
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply (metis Prfs.intros(2))
apply (metis Prfs.intros(3))
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto)[1]
apply (metis Prfs.intros(1) append_Cons)
apply(auto)[1]
apply (metis Prfs.intros(1) append_Nil mkeps_nullable_s)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto)[1]
by (metis Prfs.intros(1) append_Cons)

lemma v3n:
  assumes "\<TTurnstile>n v : der c r" shows "\<TTurnstile>(Suc n) (injval r c v) : r"
using assms
apply(induct arbitrary: n v rule: der.induct)
apply(simp)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(case_tac "c = c'")
apply(simp)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply (metis One_nat_def Prfn.intros(5))
apply(simp)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply (metis Prfn.intros(2))
apply (metis Prfn.intros(3))
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(auto)[1]
apply (metis Prfn.intros(1) add.commute add_Suc_right)
apply(auto)[1]
apply (metis Prfn.intros(1) mkeps_nullable_n plus_nat.add_0)
apply(simp)
apply(erule Prfn.cases)
apply(simp_all)[5]
apply(auto)[1]
by (metis Prfn.intros(1) add_Suc)

lemma v3_proj:
  assumes "\<turnstile> v : r" and "\<exists>s. (flat v) = c # s"
  shows "\<turnstile> (projval r c v) : der c r"
using assms
apply(induct rule: Prf.induct)
prefer 4
apply(simp)
prefer 4
apply(simp)
apply (metis Prf.intros(4))
prefer 2
apply(simp)
apply (metis Prf.intros(2))
prefer 2
apply(simp)
apply (metis Prf.intros(3))
apply(auto)
apply(rule Prf.intros)
apply(simp)
apply (metis Prf_flat_L nullable_correctness)
apply(rule Prf.intros)
apply(rule Prf.intros)
apply (metis Cons_eq_append_conv)
apply(simp)
apply(rule Prf.intros)
apply (metis Cons_eq_append_conv)
apply(simp)
done

lemma v3s_proj:
  assumes "\<Turnstile>(c#s) v : r"
  shows "\<Turnstile>s (projval r c v) : der c r"
using assms
apply(induct s\<equiv>"c#s" v r arbitrary: s rule: Prfs.induct)
prefer 4
apply(simp)
apply (metis Prfs.intros(4))
prefer 2
apply(simp)
apply (metis Prfs.intros(2))
prefer 2
apply(simp)
apply (metis Prfs.intros(3))
apply(auto)
apply(rule Prfs.intros)
apply (metis Prfs_flat append_Nil)
prefer 2
apply(rule Prfs.intros)
apply(subst (asm) append_eq_Cons_conv)
apply(auto)[1]
apply (metis Prfs_flat)
apply(rule Prfs.intros)
apply metis
apply(simp)
apply(subst (asm) append_eq_Cons_conv)
apply(auto)[1]
apply (metis Prf_flat_L Prfs_Prf nullable_correctness)
apply (metis Prfs_flat list.distinct(1))
apply(subst (asm) append_eq_Cons_conv)
apply(auto)[1]
apply (metis Prfs_flat)
by (metis Prfs.intros(1))

text {*
  The string behind the injection value is an added c
*}

lemma v4s:
  assumes "\<Turnstile>s v : der c r" shows "flat (injval r c v) = c # (flat v)"
using assms
apply(induct arbitrary: s v rule: der.induct)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "c = c'")
apply(simp)
apply(auto)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
apply(erule Prfs.cases)
apply(simp_all (no_asm_use))[5]
apply(auto)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(clarify)
apply(simp only: injval.simps flat.simps)
apply(auto)[1]
apply (metis mkeps_flat)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
done

lemma v4:
  assumes "\<turnstile> v : der c r" shows "flat (injval r c v) = c # (flat v)"
using assms
apply(induct arbitrary: v rule: der.induct)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "c = c'")
apply(simp)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
apply(erule Prf.cases)
apply(simp_all (no_asm_use))[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(simp only: injval.simps flat.simps)
apply(auto)[1]
apply (metis mkeps_flat)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
done

lemma v4_flats:
  assumes "\<turnstile> v : der c r" "\<not>nullable r" shows "hd (flats (injval r c v)) \<noteq> []"
using assms
apply(induct arbitrary: v rule: der.induct)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "c = c'")
apply(simp)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
oops

lemma v4_flat_left:
  assumes "\<turnstile> v : der c r" "\<not>(nullable_left r)" shows "flat_left (injval r c v) = c # (flat_left v)"
using assms
apply(induct arbitrary: v rule: der.induct)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "c = c'")
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
apply(erule Prf.cases)
apply(simp_all (no_asm_use))[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(simp only: injval.simps)
apply (metis nullable_left)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
done


lemma v4_proj:
  assumes "\<turnstile> v : r" and "\<exists>s. (flat v) = c # s"
  shows "c # flat (projval r c v) = flat v"
using assms
apply(induct rule: Prf.induct)
prefer 4
apply(simp)
prefer 4
apply(simp)
prefer 2
apply(simp)
prefer 2
apply(simp)
apply(auto)
by (metis Cons_eq_append_conv)

lemma v4_proj2:
  assumes "\<turnstile> v : r" and "(flat v) = c # s"
  shows "flat (projval r c v) = s"
using assms
by (metis list.inject v4_proj)

lemma injval_inj: "inj_on (injval r c) {v. \<turnstile> v : der c r}"
apply(induct c r rule: der.induct)
unfolding inj_on_def
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply (metis list.distinct(1) mkeps_flat v4)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rotate_tac 6)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis list.distinct(1) mkeps_flat v4)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
done

lemma 
  assumes "POSIXs v (der c r) s" 
  shows "POSIXs (injval r c v) r (c # s)"
using assms
apply(induct c r arbitrary: v s rule: der.induct)
apply(auto simp add: POSIXs_def)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto simp add: POSIXs_def)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(case_tac "c = c'")
apply(auto simp add: POSIXs_def)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply (metis Prfs.intros(5))
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply (metis ValOrd2.intros(8))
apply(auto simp add: POSIXs_def)[1]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(frule Prfs_POSIX)
apply(drule conjunct1)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(rule POSIXs_ALT_I1)
apply (metis POSIXs_ALT2)
apply(rule POSIXs_ALT_I2)
apply (metis POSIXs_ALT1a)
apply(frule POSIXs_ALT1b)
apply(auto)
apply(frule POSIXs_ALT1a)
(* HERE *)
oops

lemma t: "(c#xs = c#ys) \<Longrightarrow> xs = ys"
by (metis list.sel(3))

lemma t2: "(xs = ys) \<Longrightarrow> (c#xs) = (c#ys)"
by (metis)

fun zeroable where
  "zeroable NULL = True"
| "zeroable EMPTY = False"
| "zeroable (CHAR c) = False"
| "zeroable (ALT r1 r2) = (zeroable r1 \<and> zeroable r2)"
| "zeroable (SEQ r1 r2) = (zeroable r1 \<or> zeroable r2)"

lemma "\<not>(nullable r) \<Longrightarrow> \<not>(\<exists>v. \<turnstile> v : r \<and> flat v = [])"
by (metis Prf_flat_L nullable_correctness)


lemma LeftRight:
  assumes "(Left v1) \<succ>(der c (ALT r1 r2)) (Right v2)"
  and "\<turnstile> v1 : der c r1" "\<turnstile> v2 : der c r2" 
  shows "(injval (ALT r1 r2) c (Left v1)) \<succ>(ALT r1 r2) (injval (ALT r1 r2) c (Right v2))"
using assms
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(rule ValOrd.intros)
apply(clarify)
apply(subst v4)
apply(simp)
apply(subst v4)
apply(simp)
apply(simp)
done

lemma RightLeft:
  assumes "(Right v1) \<succ>(der c (ALT r1 r2)) (Left v2)"
  and "\<turnstile> v1 : der c r2" "\<turnstile> v2 : der c r1" 
  shows "(injval (ALT r1 r2) c (Right v1)) \<succ>(ALT r1 r2) (injval (ALT r1 r2) c (Left v2))"
using assms
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(rule ValOrd.intros)
apply(clarify)
apply(subst v4)
apply(simp)
apply(subst v4)
apply(simp)
apply(simp)
done

lemma h: 
  assumes "nullable r1" "\<turnstile> v1 : der c r1"
  shows "injval r1 c v1 \<succ>r1 mkeps r1"
using assms
apply(induct r1 arbitrary: v1 rule: der.induct)
apply(simp)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(auto)[1]
apply (metis ValOrd.intros(6))
apply (metis ValOrd.intros(6))
apply (metis ValOrd.intros(3) le_add2 list.size(3) mkeps_flat monoid_add_class.add.right_neutral)
apply(auto)[1]
apply (metis ValOrd.intros(4) length_greater_0_conv list.distinct(1) list.size(3) mkeps_flat v4)
apply (metis ValOrd.intros(4) length_greater_0_conv list.distinct(1) list.size(3) mkeps_flat v4)
apply (metis ValOrd.intros(5))
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply (metis ValOrd.intros(2) list.distinct(1) mkeps_flat v4)
apply(clarify)
by (metis ValOrd.intros(1))

lemma LeftRightSeq:
  assumes "(Left (Seq v1 v2)) \<succ>(der c (SEQ r1 r2)) (Right v3)"
  and "nullable r1" "\<turnstile> v1 : der c r1"
  shows "(injval (SEQ r1 r2) c (Seq v1 v2)) \<succ>(SEQ r1 r2) (injval (SEQ r1 r2) c (Right v2))"
using assms
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(simp)
apply(rule ValOrd.intros(2))
prefer 2
apply (metis list.distinct(1) mkeps_flat v4)
by (metis h)

lemma rr1: 
  assumes "\<turnstile> v : r" "\<not>nullable r" 
  shows "flat v \<noteq> []"
using assms
by (metis Prf_flat_L nullable_correctness)

lemma rr2: "hd (flats v) \<noteq> [] \<Longrightarrow> flats v \<noteq> []"
apply(induct v)
apply(auto)
done

lemma rr3: "flats v = [] \<Longrightarrow> flat v = []"
apply(induct v)
apply(auto)
done

lemma POSIXs_der:
  assumes "POSIXs v (der c r) s" "\<Turnstile>s v : der c r"
  shows "POSIXs (injval r c v) r (c#s)"
using assms
unfolding POSIXs_def
apply(auto)
thm v3s 
apply (erule v3s)
apply(drule_tac x="projval r c v'" in spec)
apply(drule mp)
thm v3s_proj
apply(rule v3s_proj)
apply(simp)
thm v3s_proj
apply(drule v3s_proj)
oops

lemma Prf_inj_test:
  assumes "v1 2\<succ> v2" "\<turnstile> v1 : der c r" "\<turnstile> v2 : der c r" 
  "length (flat v1) = length (flat v2)"
  shows "(injval r c v1) 2\<succ>  (injval r c v2)"
using assms
apply(induct c r arbitrary: v1 v2 rule: der.induct)
(* NULL case *)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
(* EMPTY case *)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
(* CHAR case *)
apply(case_tac "c = c'")
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
(* ALT case *)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[2]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[2]
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply(auto)[1]
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply(subst v4)
apply metis
apply(subst v4)
apply (metis)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[2]
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
(* SEQ case*)
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
defer
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd2.intros)
apply(simp)
apply(rule ValOrd2.intros)
apply(auto)[1]

using injval_inj
apply(simp add: inj_on_def)
apply metis
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply (metis ValOrd2.intros(1))
apply(rule ValOrd2.intros)
apply metis
using injval_inj
apply(simp add: inj_on_def)
apply metis
apply(clarify)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rule ValOrd2.intros)
apply(rule Ord1)
apply(rule h)
apply(simp)
apply(simp)
apply (metis list.distinct(1) mkeps_flat v4)
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rotate_tac 7)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
defer
apply(clarify)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply (metis ValOrd2.intros(1))
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply(simp)
apply (rule_tac ValOrd2.intros(2))
prefer 2
apply (metis list.distinct(1) mkeps_flat v4)


lemma Prf_inj_test:
  assumes "v1 2\<succ> v2" "\<Turnstile>s1 v1 : der c r" "\<Turnstile>s2 v2 : der c r"  
  shows "(injval r c v1) 2\<succ>  (injval r c v2)"
using assms
apply(induct c r arbitrary: s1 s2 v1 v2 rule: der.induct)
(* NULL case *)
apply(erule Prfs.cases)
apply(simp_all)[5]
(* EMPTY case *)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
(* CHAR case *)
apply(case_tac "c = c'")
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
(* ALT case *)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto)[2]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto)[2]
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply(auto)[1]
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply(subst v4)
apply (metis Prfs_Prf)
apply(subst v4)
apply (metis Prfs_Prf)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(auto)[2]
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply(subst v4)
apply (metis Prfs_Prf)
apply(subst v4)
apply (metis Prfs_Prf)
apply(simp)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(rule ValOrd2.intros)
apply metis
(* SEQ case*)
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
defer
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd2.intros)
apply(simp)
apply(rule ValOrd2.intros)
apply(auto)[1]
defer
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply (metis ValOrd2.intros(1))
apply(rule ValOrd2.intros)
apply metis
defer
apply(clarify)
apply(simp)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(clarify)
apply(rule ValOrd2.intros)
apply(rule Ord1)
apply(rule h)
apply(simp)
apply(simp)
apply (metis Prfs_Prf)
defer
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(clarify)
apply(rotate_tac 6)
apply(erule Prfs.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(clarify)
apply(simp)

apply(erule ValOrd2.cases)
apply(simp_all)[8]
apply(simp)
apply metis
using injval_inj
apply(simp add: inj_on_def)
apply metis
(* SEQ nullable case *)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd.intros(1))
apply(simp)
apply(rule ValOrd.intros(2))
apply metis
using injval_inj
apply(simp add: inj_on_def)
apply metis
apply(clarify)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(simp)
apply(rule ValOrd.intros(2))
apply (metis h)
apply (metis list.distinct(1) mkeps_flat v4)
(* last case *)
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rotate_tac 6)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
prefer 2
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd.intros(1))
apply(metis)
apply(rule ValOrd.intros(2))
prefer 2
thm mkeps_flat v4
apply (metis list.distinct(1) mkeps_flat v4)
oops


lemma Prf_inj_test:
  assumes "v1 \<succ>(der c r) v2" "\<turnstile> v1 : der c r" "\<turnstile> v2 : der c r"  
          "flat v1 = flat v2"
  shows "(injval r c v1) \<succ>r  (injval r c v2)"
using assms
apply(induct v1 arbitrary: r v2 taking: "length o flat" rule: measure_induct_rule)
apply(case_tac r)
(* NULL case *)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
(* EMPTY case *)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
(* CHAR case *)
apply(case_tac "c = char")
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(8))
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
(* ALT case *)
prefer 2
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply (rule ValOrd.intros(6))
apply(drule_tac x="v1b" in meta_spec)
apply(drule_tac x="rexp1" in meta_spec)
apply(drule_tac x="v1'" in meta_spec)
apply(drule_tac meta_mp)
apply(assumption)
apply(drule_tac meta_mp)
apply(assumption)
apply(drule_tac meta_mp)

apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply (metis ValOrd.intros(3) monoid_add_class.add.right_neutral order_refl v4)
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply (metis RightLeft der.simps(4) injval.simps(2) injval.simps(3))
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply (metis ValOrd.intros(5))
(* SEQ case *)
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
defer
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd.intros)
apply(simp)
apply(clarify)
apply(rule ValOrd.intros(2))
apply(rotate_tac 2)
apply(drule_tac x="v1c" in meta_spec)
apply(rotate_tac 9)
apply(drule_tac x="v1'" in meta_spec)
apply(drule_tac meta_mp)
apply(assumption)
apply(drule_tac meta_mp)
apply(assumption)
apply(drule_tac meta_mp)
apply(assumption)
apply(drule_tac meta_mp)
apply(simp)
apply (metis POSIX_SEQ1)
apply(simp)
apply (metis proj_inj_id)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rotate_tac 6)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd.intros(1))
apply(simp)
apply(clarify)
apply (metis POSIX_ALT2 POSIX_SEQ1 ValOrd.intros(2) proj_inj_id)
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply (metis Prf.intros(1) Prf.intros(2) ValOrd.intros(2) der.simps(5) h injval.simps(5) mkeps_flat proj_inj_id projval.simps(4) val.distinct(19))
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rotate_tac 7)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(simp)
apply(drule POSIX_ALT1a)
apply(rule ValOrd.intros(2))
defer
apply (metis list.distinct(1) mkeps_flat v4)
apply(rule ValOrd.intros(1))
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply (metis POSIX_ALT1a)

apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd.intros(1))
apply(simp)

apply(subgoal_tac "flats v1c \<noteq> []")

apply(simp)
apply(subst v4)
apply(simp)
apply(subst (asm) v4)
apply(simp)
apply(simp)
apply(simp add: prefix_def)
oops

lemma Prf_inj:
  assumes "v1 \<succ>(der c r) v2" "\<turnstile> v1 : der c r" "\<turnstile> v2 : der c r" (*"flat v1 = flat v2"*)
  shows "(injval r c v1) \<succ>r (injval r c v2)"
using assms
apply(induct c r arbitrary: v1 v2 rule: der.induct)
(* NULL case *)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
(* EMPTY case *)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
(* CHAR case *)
apply(case_tac "c = c'")
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(rule ValOrd.intros)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
(* ALT case *)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(rule ValOrd.intros)
apply(subst v4)
apply(clarify)
apply(rotate_tac 3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(subst v4)
apply(clarify)
apply(rotate_tac 2)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(rule ValOrd.intros)
apply(clarify)
apply(rotate_tac 3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(subst v4)
apply(simp)
apply(subst v4)
apply(simp)
apply(simp)
apply(rule ValOrd.intros)
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule ValOrd.intros)
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
(* SEQ case*)
apply(simp)
apply(case_tac "nullable r1")
apply(simp)
defer
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd.intros)
apply(simp)
apply(clarify)
apply(rule ValOrd.intros(2))
apply metis
using injval_inj
apply(simp add: inj_on_def)
apply metis
(* SEQ nullable case *)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd.intros(1))
apply(simp)
apply(rule ValOrd.intros(2))
apply metis
using injval_inj
apply(simp add: inj_on_def)
apply metis
apply(clarify)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(simp)
apply(rule ValOrd.intros(2))
apply (metis h)
apply (metis list.distinct(1) mkeps_flat v4)
(* last case *)
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rotate_tac 6)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
prefer 2
apply(clarify)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(rule ValOrd.intros(1))
apply(metis)
apply(rule ValOrd.intros(2))
prefer 2
thm mkeps_flat v4
apply (metis list.distinct(1) mkeps_flat v4)
oops


lemma POSIX_der:
  assumes "POSIX v (der c r)" "\<turnstile> v : der c r"
  shows "POSIX (injval r c v) r"
using assms
unfolding POSIX_def
apply(auto)
thm v3
apply (erule v3)
thm v4
apply(subst (asm) v4)
apply(assumption)
apply(drule_tac x="projval r c v'" in spec)
apply(drule mp)
apply(rule conjI)
thm v3_proj
apply(rule v3_proj)
apply(simp)
apply(rule_tac x="flat v" in exI)
apply(simp)
thm t
apply(rule_tac c="c" in  t)
apply(simp)
thm v4_proj
apply(subst v4_proj)
apply(simp)
apply(rule_tac x="flat v" in exI)
apply(simp)
apply(simp)
oops

lemma POSIX_der:
  assumes "POSIX v (der c r)" "\<turnstile> v : der c r"
  shows "POSIX (injval r c v) r"
using assms
apply(induct c r arbitrary: v rule: der.induct)
(* null case*)
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
(* empty case *)
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
(* char case *)
apply(simp add: POSIX_def)
apply(case_tac "c = c'")
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis Prf.intros(5))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(8))
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
(* alt case *)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(simp (no_asm) add: POSIX_def)
apply(auto)[1]
apply (metis Prf.intros(2) v3)
apply(rotate_tac 4)
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis POSIX_ALT2 POSIX_def ValOrd.intros(6))
apply (metis ValOrd.intros(3) order_refl)
apply(simp (no_asm) add: POSIX_def)
apply(auto)[1]
apply (metis Prf.intros(3) v3)
apply(rotate_tac 4)
apply(erule Prf.cases)
apply(simp_all)[5]
defer
apply (metis POSIX_ALT1a POSIX_def ValOrd.intros(5))
prefer 2
apply(subst (asm) (5) POSIX_def)
apply(auto)[1]
apply(rotate_tac 5)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule ValOrd.intros)
apply(subst (asm) v4)
apply(simp)
apply(drule_tac x="Left (projval r1a c v1)" in spec)
apply(clarify)
apply(drule mp)
apply(rule conjI)
apply (metis Prf.intros(2) v3_proj)
apply(simp)
apply (metis v4_proj2)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply (metis less_not_refl v4_proj2)
(* seq case *)
apply(case_tac "nullable r1")
defer
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis Prf.intros(1) v3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(subst (asm) (3) v4)
apply(simp)
apply(simp)
apply(subgoal_tac "flat v1a \<noteq> []")
prefer 2
apply (metis Prf_flat_L nullable_correctness)
apply(subgoal_tac "\<exists>s. flat v1a = c # s")
prefer 2
apply (metis append_eq_Cons_conv)
apply(auto)[1]
 

apply(auto)
thm v3
apply (erule v3)
thm v4
apply(subst (asm) v4)
apply(assumption)
apply(drule_tac x="projval r c v'" in spec)
apply(drule mp)
apply(rule conjI)
thm v3_proj
apply(rule v3_proj)
apply(simp)
apply(rule_tac x="flat v" in exI)
apply(simp)
thm t
apply(rule_tac c="c" in  t)
apply(simp)
thm v4_proj
apply(subst v4_proj)
apply(simp)
apply(rule_tac x="flat v" in exI)
apply(simp)
apply(simp)
oops


lemma POSIX_ex: "\<turnstile> v : r \<Longrightarrow> \<exists>v. POSIX v r"
apply(induct r arbitrary: v)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule_tac x="Void" in exI)
apply(simp add: POSIX_def)
apply(auto)[1]
apply (metis Prf.intros(4))
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(7))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule_tac x="Char c" in exI)
apply(simp add: POSIX_def)
apply(auto)[1]
apply (metis Prf.intros(5))
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(8))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(drule_tac x="v1" in meta_spec)
apply(drule_tac x="v2" in meta_spec)
apply(auto)[1]
defer
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply (metis POSIX_ALT_I1)
apply (metis POSIX_ALT_I1 POSIX_ALT_I2)
apply(case_tac "nullable r1a")
apply(rule_tac x="Seq (mkeps r1a) va" in exI)
apply(auto simp add: POSIX_def)[1]
apply (metis Prf.intros(1) mkeps_nullable)
apply(simp add: mkeps_flat)
apply(rotate_tac 7)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(case_tac "mkeps r1 = v1a")
apply(simp)
apply (rule ValOrd.intros(1))
apply (metis append_Nil mkeps_flat)
apply (rule ValOrd.intros(2))
apply(drule mkeps_POSIX)
apply(simp add: POSIX_def)

apply metis
apply(simp)
apply(simp)
apply(erule disjE)
apply(simp)

apply(drule_tac x="v2" in spec)

lemma POSIX_ex2: "\<turnstile> v : r \<Longrightarrow> \<exists>v. POSIX v r \<and> \<turnstile> v : r"
apply(induct r arbitrary: v)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule_tac x="Void" in exI)
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(7))
apply (metis Prf.intros(4))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule_tac x="Char c" in exI)
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(8))
apply (metis Prf.intros(5))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(drule_tac x="v1" in meta_spec)
apply(drule_tac x="v2" in meta_spec)
apply(auto)[1]
apply(simp add: POSIX_def)
apply(auto)[1]
apply(rule ccontr)
apply(simp)
apply(drule_tac x="Seq v va" in spec)
apply(drule mp)
defer
apply (metis Prf.intros(1))
oops

lemma POSIX_ALT_cases:
  assumes "\<turnstile> v : (ALT r1 r2)" "POSIX v (ALT r1 r2)"
  shows "(\<exists>v1. v = Left v1 \<and> POSIX v1 r1) \<or> (\<exists>v2. v = Right v2 \<and> POSIX v2 r2)"
using assms
apply(erule_tac Prf.cases)
apply(simp_all)
unfolding POSIX_def
apply(auto)
apply (metis POSIX_ALT2 POSIX_def assms(2))
by (metis POSIX_ALT1b assms(2))

lemma POSIX_ALT_cases2:
  assumes "POSIX v (ALT r1 r2)" "\<turnstile> v : (ALT r1 r2)" 
  shows "(\<exists>v1. v = Left v1 \<and> POSIX v1 r1) \<or> (\<exists>v2. v = Right v2 \<and> POSIX v2 r2)"
using assms POSIX_ALT_cases by auto

lemma Prf_flat_empty:
  assumes "\<turnstile> v : r" "flat v = []"
  shows "nullable r"
using assms
apply(induct)
apply(auto)
done

lemma POSIX_proj:
  assumes "POSIX v r" "\<turnstile> v : r" "\<exists>s. flat v = c#s"
  shows "POSIX (projval r c v) (der c r)"
using assms
apply(induct r c v arbitrary: rule: projval.induct)
defer
defer
defer
defer
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(7))
apply(erule_tac [!] exE)
prefer 3
apply(frule POSIX_SEQ1)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(case_tac "flat v1 = []")
apply(subgoal_tac "nullable r1")
apply(simp)
prefer 2
apply(rule_tac v="v1" in Prf_flat_empty)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(frule POSIX_SEQ2)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(drule meta_mp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule ccontr)
apply(subgoal_tac "\<turnstile> val.Right (projval r2 c v2) : (ALT (SEQ (der c r1) r2) (der c r2))")
apply(rotate_tac 11)
apply(frule POSIX_ex)
apply(erule exE)
apply(drule POSIX_ALT_cases2)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(drule v3_proj)
apply(simp)
apply(simp)
apply(drule POSIX_ex)
apply(erule exE)
apply(frule POSIX_ALT_cases2)
apply(simp)
apply(simp)
apply(erule 
prefer 2
apply(case_tac "nullable r1")
prefer 2
apply(simp)
apply(rotate_tac 1)
apply(drule meta_mp)
apply(rule POSIX_SEQ1)
apply(assumption)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rotate_tac 7)
apply(drule meta_mp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rotate_tac 7)
apply(drule meta_mp)
apply (metis Cons_eq_append_conv)


apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp add: POSIX_def)
apply(simp)
apply(simp)
apply(simp_all)[5]
apply(simp add: POSIX_def)


lemma POSIX_proj:
  assumes "POSIX v r" "\<turnstile> v : r" "\<exists>s. flat v = c#s"
  shows "POSIX (projval r c v) (der c r)"
using assms
apply(induct r arbitrary: c v rule: rexp.induct)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(7))

apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(7))
apply(erule_tac [!] exE)
prefer 3
apply(frule POSIX_SEQ1)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(case_tac "flat v1 = []")
apply(subgoal_tac "nullable r1")
apply(simp)
prefer 2
apply(rule_tac v="v1" in Prf_flat_empty)
apply(erule Prf.cases)
apply(simp_all)[5]


lemma POSIX_proj:
  assumes "POSIX v r" "\<turnstile> v : r" "\<exists>s. flat v = c#s"
  shows "POSIX (projval r c v) (der c r)"
using assms
apply(induct r c v arbitrary: rule: projval.induct)
defer
defer
defer
defer
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(7))
apply(erule_tac [!] exE)
prefer 3
apply(frule POSIX_SEQ1)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(case_tac "flat v1 = []")
apply(subgoal_tac "nullable r1")
apply(simp)
prefer 2
apply(rule_tac v="v1" in Prf_flat_empty)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(rule ccontr)
apply(drule v3_proj)
apply(simp)
apply(simp)
apply(drule POSIX_ex)
apply(erule exE)
apply(frule POSIX_ALT_cases2)
apply(simp)
apply(simp)
apply(erule 
prefer 2
apply(case_tac "nullable r1")
prefer 2
apply(simp)
apply(rotate_tac 1)
apply(drule meta_mp)
apply(rule POSIX_SEQ1)
apply(assumption)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rotate_tac 7)
apply(drule meta_mp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rotate_tac 7)
apply(drule meta_mp)
apply (metis Cons_eq_append_conv)


apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp add: POSIX_def)
apply(simp)
apply(simp)
apply(simp_all)[5]
apply(simp add: POSIX_def)

done
(* NULL case *)
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(7))
apply(rotate_tac 4)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
prefer 2
apply(simp)
apply(frule POSIX_ALT1a)
apply(drule meta_mp)
apply(simp)
apply(drule meta_mp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule POSIX_ALT_I2)
apply(assumption)
apply(auto)[1]

thm v4_proj2
prefer 2
apply(subst (asm) (13) POSIX_def)

apply(drule_tac x="projval v2" in spec)
apply(auto)[1]
apply(drule mp)
apply(rule conjI)
apply(simp)
apply(simp)

apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
prefer 2
apply(clarify)
apply(subst (asm) (2) POSIX_def)

apply (metis ValOrd.intros(5))
apply(clarify)
apply(simp)
apply(rotate_tac 3)
apply(drule_tac c="c" in t2)
apply(subst (asm) v4_proj)
apply(simp)
apply(simp)
thm contrapos_np contrapos_nn
apply(erule contrapos_np)
apply(rule ValOrd.intros)
apply(subst  v4_proj2)
apply(simp)
apply(simp)
apply(subgoal_tac "\<not>(length (flat v1) < length (flat (projval r2a c v2a)))")
prefer 2
apply(erule contrapos_nn)
apply (metis nat_less_le v4_proj2)
apply(simp)

apply(blast)
thm contrapos_nn

apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rule ValOrd.intros)
apply(drule meta_mp)
apply(auto)[1]
apply (metis POSIX_ALT2 POSIX_def flat.simps(3))
apply metis
apply(clarify)
apply(rule ValOrd.intros)
apply(simp)
apply(simp add: POSIX_def)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rule ValOrd.intros)
apply(simp)

apply(drule meta_mp)
apply(auto)[1]
apply (metis POSIX_ALT2 POSIX_def flat.simps(3))
apply metis
apply(clarify)
apply(rule ValOrd.intros)
apply(simp)


done
(* EMPTY case *)
apply(simp add: POSIX_def)
apply(auto)[1]
apply(rotate_tac 3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(drule_tac c="c" in t2)
apply(subst (asm) v4_proj)
apply(auto)[2]

apply(erule ValOrd.cases)
apply(simp_all)[8]
(* CHAR case *)
apply(case_tac "c = c'")
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(rule ValOrd.intros)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
(* ALT case *)


unfolding POSIX_def
apply(auto)
thm v4

lemma Prf_inj:
  assumes "v1 \<succ>(der c r) v2" "\<turnstile> v1 : der c r" "\<turnstile> v2 : der c r" "flat v1 = flat v2"
  shows "(injval r c v1) \<succ>r (injval r c v2)"
using assms
apply(induct arbitrary: v1 v2 rule: der.induct)
(* NULL case *)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
(* EMPTY case *)
apply(erule ValOrd.cases)
apply(simp_all)[8]
(* CHAR case *)
apply(case_tac "c = c'")
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(rule ValOrd.intros)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
(* ALT case *)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(rule ValOrd.intros)
apply(subst v4)
apply(clarify)
apply(rotate_tac 3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(subst v4)
apply(clarify)
apply(rotate_tac 2)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(rule ValOrd.intros)
apply(clarify)
apply(rotate_tac 3)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule ValOrd.intros)
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
(* SEQ case*)
apply(simp)
apply(case_tac "nullable r1")
defer
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all)[8]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rule ValOrd.intros)
apply(simp)
apply(simp)
apply(rule ValOrd.intros(2))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
defer
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all del: injval.simps)[8]
apply(simp)
apply(clarify)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(rule ValOrd.intros(2))




done


txt {*
done
(* nullable case - unfinished *)
apply(simp)
apply(erule ValOrd.cases)
apply(simp_all del: injval.simps)[8]
apply(simp)
apply(clarify)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(simp)
apply(rule ValOrd.intros(2))
oops
*}
oops



text {*
  Injection followed by projection is the identity.
*}

lemma proj_inj_id:
  assumes "\<turnstile> v : der c r" 
  shows "projval r c (injval r c v) = v"
using assms
apply(induct r arbitrary: c v rule: rexp.induct)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "c = char")
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
defer
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(simp)
apply(case_tac "nullable rexp1")
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply (metis list.distinct(1) v4)
apply(auto)[1]
apply (metis mkeps_flat)
apply(auto)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(auto)[1]
apply(simp add: v4)
done

lemma "L r \<noteq> {} \<Longrightarrow> \<exists>v. POSIX3 v r"
apply(induct r)
apply(simp)
apply(simp add: POSIX3_def)
apply(rule_tac x="Void" in exI)
apply(auto)[1]
apply (metis Prf.intros(4))
apply (metis POSIX3_def flat.simps(1) mkeps.simps(1) mkeps_POSIX3 nullable.simps(2) order_refl)
apply(simp add: POSIX3_def)
apply(rule_tac x="Char char" in exI)
apply(auto)[1]
apply (metis Prf.intros(5))
apply(erule Prf.cases)
apply(simp_all)[5]
apply (metis ValOrd.intros(8))
apply(simp add: Sequ_def)
apply(auto)[1]
apply(drule meta_mp)
apply(auto)[2]
apply(drule meta_mp)
apply(auto)[2]
apply(rule_tac x="Seq v va" in exI)
apply(simp (no_asm) add: POSIX3_def)
apply(auto)[1]
apply (metis POSIX3_def Prf.intros(1))
apply(erule Prf.cases)
apply(simp_all)[5]
apply(clarify)
apply(case_tac "v  \<succ>r1a v1")
apply(rule ValOrd.intros(2))
apply(simp)
apply(case_tac "v = v1")
apply(rule ValOrd.intros(1))
apply(simp)
apply(simp)
apply (metis ValOrd_refl)
apply(simp add: POSIX3_def)
oops

lemma "\<exists>v. POSIX v r"
apply(induct r)
apply(rule exI)
apply(simp add: POSIX_def)
apply (metis (full_types) Prf_flat_L der.simps(1) der.simps(2) der.simps(3) flat.simps(1) nullable.simps(1) nullable_correctness proj_inj_id projval.simps(1) v3 v4)
apply(rule_tac x = "Void" in exI)
apply(simp add: POSIX_def)
apply (metis POSIX_def flat.simps(1) mkeps.simps(1) mkeps_POSIX nullable.simps(2))
apply(rule_tac x = "Char char" in exI)
apply(simp add: POSIX_def)
apply(auto) [1]
apply(erule Prf.cases)
apply(simp_all) [5]
apply (metis ValOrd.intros(8))
defer
apply(auto)
apply (metis POSIX_ALT_I1)
(* maybe it is too early to instantiate this existential quantifier *)
(* potentially this is the wrong POSIX value *)
apply(case_tac "r1 = NULL")
apply(simp add: POSIX_def)
apply(auto)[1]
apply (metis L.simps(1) L.simps(4) Prf_flat_L mkeps_flat nullable.simps(1) nullable.simps(2) nullable_correctness seq_null(2))
apply(case_tac "r1 = EMPTY")
apply(rule_tac x = "Seq Void va" in exI )
apply(simp (no_asm) add: POSIX_def)
apply(auto)
apply(erule Prf.cases)
apply(simp_all)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)
apply(rule ValOrd.intros(2))
apply(rule ValOrd.intros)
apply(case_tac "\<exists>c. r1 = CHAR c")
apply(auto)
apply(rule_tac x = "Seq (Char c) va" in exI )
apply(simp (no_asm) add: POSIX_def)
apply(auto)
apply(erule Prf.cases)
apply(simp_all)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)
apply(auto)[1]
apply(rule ValOrd.intros(2))
apply(rule ValOrd.intros)
apply(case_tac "\<exists>r1a r1b. r1 = ALT r1a r1b")
apply(auto)
oops (* not sure if this can be proved by induction *)

text {* 

  HERE: Crucial lemma that does not go through in the sequence case. 

*}
lemma v5:
  assumes "\<turnstile> v : der c r" "POSIX v (der c r)"
  shows "POSIX (injval r c v) r"
using assms
apply(induct arbitrary: v rule: der.induct)
(* NULL case *)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
(* EMPTY case *)
apply(simp)
apply(erule Prf.cases)
apply(simp_all)[5]
(* CHAR case *)
apply(simp)
apply(case_tac "c = c'")
apply(auto simp add: POSIX_def)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule ValOrd.intros)
apply(auto)[1]
apply(erule Prf.cases)
apply(simp_all)[5]
(* base cases done *)
(* ALT case *)
apply(erule Prf.cases)
apply(simp_all)[5]
using POSIX_ALT POSIX_ALT_I1 apply blast
apply(clarify)
apply(simp)
apply(rule POSIX_ALT_I2)
apply(drule POSIX_ALT1a)
apply metis
apply(auto)[1]
apply(subst v4)
apply(assumption)
apply(simp)
apply(drule POSIX_ALT1a)
apply(rotate_tac 1)
apply(drule_tac x="v2" in meta_spec)
apply(simp)

apply(rotate_tac 4)
apply(erule Prf.cases)
apply(simp_all)[5]
apply(rule ValOrd.intros)
apply(simp)
apply(subst (asm) v4)
apply(assumption)
apply(clarify)
thm POSIX_ALT1a POSIX_ALT1b POSIX_ALT_I2
apply(subst (asm) v4)
apply(auto simp add: POSIX_def)[1]
apply(subgoal_tac "POSIX v2 (der c r2)")
prefer 2
apply(auto simp add: POSIX_def)[1]
apply (metis POSIX_ALT1a POSIX_def flat.simps(4))
apply(frule POSIX_ALT1a)
apply(drule POSIX_ALT1b)
apply(rule POSIX_ALT_I2)
apply(rotate_tac 1)
apply(drule_tac x="v2" in meta_spec)
apply(simp)
apply(subgoal_tac "\<turnstile> Right (injval r2 c v2) : (ALT r1 r2)")
prefer 2
apply (metis Prf.intros(3) v3)
apply auto[1]
apply(subst v4)
apply(auto)[2]
apply(subst (asm) (4) POSIX_def)
apply(subst (asm) v4)
apply(drule_tac x="v2" in meta_spec)
apply(simp)

apply(auto)[2]

thm POSIX_ALT_I2
apply(rule POSIX_ALT_I2)

apply(rule ccontr)
apply(auto simp add: POSIX_def)[1]

apply(rule allI)
apply(rule impI)
apply(erule conjE)
thm POSIX_ALT_I2
apply(frule POSIX_ALT1a)
apply(drule POSIX_ALT1b)
apply(rule POSIX_ALT_I2)
apply auto[1]
apply(subst v4)
apply(auto)[2]
apply(rotate_tac 1)
apply(drule_tac x="v2" in meta_spec)
apply(simp)
apply(subst (asm) (4) POSIX_def)
apply(subst (asm) v4)
apply(auto)[2]
(* stuck in the ALT case *)