+lemma split_on_spec: ∀A:DeqSet.∀l,f,acc,res1,res2.
+ split_on A l f acc = 〈res1,res2〉 →
+ (∃l1. res1 = l1@acc ∧
+ reverse ? l1@res2 = l ∧
+ ∀x. memb ? x l1 =true → f x = false) ∧
+ ∀a,tl. res2 = a::tl → f a = true.
+#A #l #f elim l
+ [#acc #res1 #res2 normalize in ⊢ (%→?); #H destruct %
+ [@(ex_intro … []) % normalize [% % | #x #H destruct]
+ |#a #tl #H destruct
+ ]
+ |#a #tl #Hind #acc #res1 #res2 normalize in ⊢ (%→?);
+ cases (true_or_false (f a)) #Hfa >Hfa normalize in ⊢ (%→?);
+ #H destruct
+ [% [@(ex_intro … []) % normalize [% % | #x #H destruct]
+ |#a1 #tl1 #H destruct (H) //]
+ |cases (Hind (a::acc) res1 res2 H) * #l1 * *
+ #Hres1 #Htl #Hfalse #Htrue % [2:@Htrue] @(ex_intro … (l1@[a])) %
+ [% [>associative_append @Hres1 | >reverse_append <Htl % ]
+ |#x #Hmemx cases (memb_append ???? Hmemx)
+ [@Hfalse | #H >(memb_single … H) //]
+ ]
+ ]
+ ]
+qed.
+
+axiom mem_reverse: ∀A,l,x. mem A x (reverse ? l) → mem A x l.
+
+lemma split_on_spec_ex: ∀A:DeqSet.∀l,f.∃l1,l2.
+ l1@l2 = l ∧ (∀x:A. memb ? x l1 = true → f x = false) ∧
+ ∀a,tl. l2 = a::tl → f a = true.
+#A #l #f @(ex_intro … (reverse … (\fst (split_on A l f []))))
+@(ex_intro … (\snd (split_on A l f [])))
+cases (split_on_spec A l f [ ] ?? (eq_pair_fst_snd …)) * #l1 * *
+>append_nil #Hl1 >Hl1 #Hl #Hfalse #Htrue %
+ [% [@Hl|#x #memx @Hfalse <(reverse_reverse … l1) @memb_reverse //] | @Htrue]
+qed.
+
+(* versione esistenziale *)
+
+definition R_comp_step_true ≝ λt1,t2.
+ ∃ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls 〈c,true〉 rs ∧
+ ((* bit_or_null c = false *)
+ (bit_or_null c = false → t2 = midtape ? ls 〈c,false〉 rs) ∧
+ (* no marks in rs *)
+ (bit_or_null c = true →
+ (∀c.memb ? c rs = true → is_marked ? c = false) →
+ ∀a,l. (a::l) = reverse ? (〈c,true〉::rs) →
+ t2 = rightof (FinProd FSUnialpha FinBool) a (l@ls)) ∧
+ (∀l1,c0,l2.
+ bit_or_null c = true →
+ (∀c.memb ? c l1 = true → is_marked ? c = false) →
+ rs = l1@〈c0,true〉::l2 →
+ (c = c0 →
+ l2 = [ ] → (* test true but l2 is empty *)
+ t2 = rightof ? 〈c0,false〉 ((reverse ? l1)@〈c,true〉::ls)) ∧
+ (c = c0 →
+ ∀a,a0,b,l1',l2'. (* test true and l2 is not empty *)
+ 〈a,false〉::l1' = l1@[〈c0,false〉] →
+ l2 = 〈a0,b〉::l2' →
+ t2 = midtape ? (〈c,false〉::ls) 〈a,true〉 (l1'@〈a0,true〉::l2')) ∧
+ (c ≠ c0 →(* test false *)
+ t2 = midtape (FinProd … FSUnialpha FinBool)
+ ((reverse ? l1)@〈c,true〉::ls) 〈c0,false〉 l2))).
+
+definition R_comp_step_false ≝
+ λt1,t2.
+ ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs →
+ is_marked ? c = false ∧ t2 = t1.
+
+lemma is_marked_to_exists: ∀alpha,c. is_marked alpha c = true →
+ ∃c'. c = 〈c',true〉.
+#alpha * #c * [#_ @(ex_intro … c) //| normalize #H destruct]
+qed.
+
+lemma exists_current: ∀alpha,c,t.
+ current alpha t = Some alpha c → ∃ls,rs. t= midtape ? ls c rs.
+#alpha #c *
+ [whd in ⊢ (??%?→?); #H destruct
+ |#a #l whd in ⊢ (??%?→?); #H destruct
+ |#a #l whd in ⊢ (??%?→?); #H destruct
+ |#ls #c1 #rs whd in ⊢ (??%?→?); #H destruct
+ @(ex_intro … ls) @(ex_intro … rs) //
+ ]
+qed.
+
+lemma sem_comp_step :
+ accRealize ? comp_step (inr … (inl … (inr … start_nop)))
+ R_comp_step_true R_comp_step_false.
+@(acc_sem_if_app … (sem_test_char ? (is_marked ?))
+ (sem_comp_step_subcase FSUnialpha 〈bit false,true〉 ??
+ (sem_comp_step_subcase FSUnialpha 〈bit true,true〉 ??
+ (sem_comp_step_subcase FSUnialpha 〈null,true〉 ??
+ (sem_clear_mark …))))
+ (sem_nop …) …)
+[#intape #outape #ta #Hta #Htb cases Hta * #cm * #Hcur
+ cases (exists_current … Hcur) #ls * #rs #Hintape #cmark
+ cases (is_marked_to_exists … cmark) #c #Hcm
+ >Hintape >Hcm -Hintape -Hcm #Hta
+ @(ex_intro … ls) @(ex_intro … c) @(ex_intro …rs) % [//] lapply Hta -Hta
+ (* #ls #c #rs #Hintape whd in Hta;
+ >Hintape in Hta; * #_ -Hintape forse non serve *)
+ cases (true_or_false (c==bit false)) #Hc
+ [>(\P Hc) #Hta %
+ [%[whd in ⊢ ((??%?)→?); #Hdes destruct
+ |#Hc @(proj1 ?? (proj1 ?? (Htb … Hta) (refl …)))
+ ]
+ |#l1 #c0 #l2 #Hc @(proj2 ?? (proj1 ?? (Htb … Hta) (refl …)))
+ ]
+ |cases (true_or_false (c==bit true)) #Hc1
+ [>(\P Hc1) #Hta
+ cut (〈bit true, true〉 ≠ 〈bit false, true〉) [% #Hdes destruct] #Hneq %
+ [%[whd in ⊢ ((??%?)→?); #Hdes destruct
+ |#Hc @(proj1 … (proj1 ?? (proj2 ?? (Htb … Hta) Hneq … Hta) (refl …)))
+ ]
+ |#l1 #c0 #l2 #Hc @(proj2 ?? (proj1 ?? (proj2 ?? (Htb … Hta) Hneq … Hta)(refl …)))
+ ]
+ |cases (true_or_false (c==null)) #Hc2
+ [>(\P Hc2) #Hta
+ cut (〈null, true〉 ≠ 〈bit false, true〉) [% #Hdes destruct] #Hneq
+ cut (〈null, true〉 ≠ 〈bit true, true〉) [% #Hdes destruct] #Hneq1 %
+ [%[whd in ⊢ ((??%?)→?); #Hdes destruct
+ |#Hc @(proj1 … (proj1 ?? (proj2 ?? (proj2 ?? (Htb … Hta) Hneq … Hta) Hneq1 … Hta) (refl …)))
+ ]
+ |#l1 #c0 #l2 #Hc @(proj2 ?? (proj1 ?? (proj2 ?? (proj2 ?? (Htb … Hta) Hneq … Hta) Hneq1 … Hta) (refl …)))
+ ]
+ |#Hta cut (bit_or_null c = false)
+ [lapply Hc; lapply Hc1; lapply Hc2 -Hc -Hc1 -Hc2
+ cases c normalize [* normalize /2/] /2/] #Hcut %
+ [%[cases (Htb … Hta) #_ -Htb #Htb
+ cases (Htb … Hta) [2: % #H destruct (H) normalize in Hc; destruct] #_ -Htb #Htb
+ cases (Htb … Hta) [2: % #H destruct (H) normalize in Hc1; destruct] #_ -Htb #Htb
+ lapply (Htb ?) [% #H destruct (H) normalize in Hc2; destruct]
+ * #_ #Houttape #_ @(Houttape … Hta)
+ |>Hcut #H destruct
+ ]
+ |#l1 #c0 #l2 >Hcut #H destruct
+ ]
+ ]
+ ]
+ ]
+|#intape #outape #ta #Hta #Htb #ls #c #rs #Hintape
+ >Hintape in Hta; whd in ⊢ (%→?); * #Hmark #Hta % [@Hmark //]
+ whd in Htb; >Htb //
+]
+qed.
+
+(* old universal version
+
+definition R_comp_step_true ≝ λt1,t2.
+ ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls 〈c,true〉 rs →
+ (* bit_or_null c = false *)
+ (bit_or_null c = false → t2 = midtape ? ls 〈c,false〉 rs) ∧
+ (* no marks in rs *)
+ (bit_or_null c = true →
+ (∀c.memb ? c rs = true → is_marked ? c = false) →
+ ∀a,l. (a::l) = reverse ? (〈c,true〉::rs) →
+ t2 = rightof (FinProd FSUnialpha FinBool) a (l@ls)) ∧
+ (∀l1,c0,l2.
+ bit_or_null c = true →
+ (∀c.memb ? c l1 = true → is_marked ? c = false) →
+ rs = l1@〈c0,true〉::l2 →
+ (c = c0 →
+ l2 = [ ] → (* test true but l2 is empty *)
+ t2 = rightof ? 〈c0,false〉 ((reverse ? l1)@〈c,true〉::ls)) ∧
+ (c = c0 →
+ ∀a,a0,b,l1',l2'. (* test true and l2 is not empty *)
+ 〈a,false〉::l1' = l1@[〈c0,false〉] →
+ l2 = 〈a0,b〉::l2' →
+ t2 = midtape ? (〈c,false〉::ls) 〈a,true〉 (l1'@〈a0,true〉::l2')) ∧
+ (c ≠ c0 →(* test false *)
+ t2 = midtape (FinProd … FSUnialpha FinBool)
+ ((reverse ? l1)@〈c,true〉::ls) 〈c0,false〉 l2)).
+
+definition R_comp_step_false ≝
+ λt1,t2.
+ ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs →
+ is_marked ? c = false ∧ t2 = t1.
+
+(*
+lemma is_marked_to_exists: ∀alpha,c. is_marked alpha c = true →
+ ∃c'. c = 〈c',true〉.
+#alpha * c *)
+
+lemma sem_comp_step :
+ accRealize ? comp_step (inr … (inl … (inr … start_nop)))
+ R_comp_step_true R_comp_step_false.
+@(acc_sem_if_app … (sem_test_char ? (is_marked ?))
+ (sem_comp_step_subcase FSUnialpha 〈bit false,true〉 ??
+ (sem_comp_step_subcase FSUnialpha 〈bit true,true〉 ??
+ (sem_comp_step_subcase FSUnialpha 〈null,true〉 ??
+ (sem_clear_mark …))))
+ (sem_nop …) …)
+[#intape #outape #ta #Hta #Htb #ls #c #rs #Hintape whd in Hta;
+ >Hintape in Hta; * #_ -Hintape (* forse non serve *)
+ cases (true_or_false (c==bit false)) #Hc
+ [>(\P Hc) #Hta %
+ [%[whd in ⊢ ((??%?)→?); #Hdes destruct
+ |#Hc @(proj1 ?? (proj1 ?? (Htb … Hta) (refl …)))
+ ]
+ |#l1 #c0 #l2 #Hc @(proj2 ?? (proj1 ?? (Htb … Hta) (refl …)))
+ ]
+ |cases (true_or_false (c==bit true)) #Hc1
+ [>(\P Hc1) #Hta
+ cut (〈bit true, true〉 ≠ 〈bit false, true〉) [% #Hdes destruct] #Hneq %
+ [%[whd in ⊢ ((??%?)→?); #Hdes destruct
+ |#Hc @(proj1 … (proj1 ?? (proj2 ?? (Htb … Hta) Hneq … Hta) (refl …)))
+ ]
+ |#l1 #c0 #l2 #Hc @(proj2 ?? (proj1 ?? (proj2 ?? (Htb … Hta) Hneq … Hta)(refl …)))
+ ]
+ |cases (true_or_false (c==null)) #Hc2
+ [>(\P Hc2) #Hta
+ cut (〈null, true〉 ≠ 〈bit false, true〉) [% #Hdes destruct] #Hneq
+ cut (〈null, true〉 ≠ 〈bit true, true〉) [% #Hdes destruct] #Hneq1 %
+ [%[whd in ⊢ ((??%?)→?); #Hdes destruct
+ |#Hc @(proj1 … (proj1 ?? (proj2 ?? (proj2 ?? (Htb … Hta) Hneq … Hta) Hneq1 … Hta) (refl …)))
+ ]
+ |#l1 #c0 #l2 #Hc @(proj2 ?? (proj1 ?? (proj2 ?? (proj2 ?? (Htb … Hta) Hneq … Hta) Hneq1 … Hta) (refl …)))
+ ]
+ |#Hta cut (bit_or_null c = false)
+ [lapply Hc; lapply Hc1; lapply Hc2 -Hc -Hc1 -Hc2
+ cases c normalize [* normalize /2/] /2/] #Hcut %
+ [%[cases (Htb … Hta) #_ -Htb #Htb
+ cases (Htb … Hta) [2: % #H destruct (H) normalize in Hc; destruct] #_ -Htb #Htb
+ cases (Htb … Hta) [2: % #H destruct (H) normalize in Hc1; destruct] #_ -Htb #Htb
+ lapply (Htb ?) [% #H destruct (H) normalize in Hc2; destruct]
+ * #_ #Houttape #_ @(Houttape … Hta)
+ |>Hcut #H destruct
+ ]
+ |#l1 #c0 #l2 >Hcut #H destruct
+ ]
+ ]
+ ]
+ ]
+|#intape #outape #ta #Hta #Htb #ls #c #rs #Hintape
+ >Hintape in Hta; whd in ⊢ (%→?); * #Hmark #Hta % [@Hmark //]
+ whd in Htb; >Htb //
+]
+qed. *)
+
+(*