+lemma sem_init_copy : Realize ? init_copy R_init_copy.
+#intape
+cases (sem_seq ????? sem_init_current_on_match
+ (sem_seq ????? (sem_move_r ?)
+ (sem_seq ????? (sem_adv_to_mark_r ? (is_marked ?))
+ (sem_adv_mark_r ?))) intape)
+#k * #outc * #Hloop #HR
+@(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
+#l1 #l2 #c #ls #d #rs #Hl1marks #Hl1grids #Hl2marks #Hc #Hintape
+cases HR -HR
+#ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hl1grids Hc Hintape) -Hta -Hintape #Hta
+* #tb * whd in ⊢ (%→?); * #_ #Htb lapply (Htb … Hta) -Htb -Hta
+generalize in match Hl1marks; -Hl1marks cases (list_last ? l1)
+ [#eql1 >eql1 #Hl1marks whd in ⊢ ((???%)→?); whd in ⊢ ((???(????%))→?); #Htb
+ * #tc * whd in ⊢ (%→?); * #_ #Htc lapply (Htc … Htb) -Htc -Htb *
+ [* whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)]
+ * * #_ #Htc #_ lapply (Htc … (refl …) (refl …) ?)
+ [#x #membx @Hl2marks @membx]
+ #Htc whd in ⊢ (%→?); * #Houtc #_ cases (Houtc (reverse ? l2@〈grid,false〉::〈c,true〉::〈grid,false〉::ls) comma)
+ -Houtc #Houtc lapply (Houtc … Htc) -Houtc -Htc #Houtc #_
+ >Houtc %
+ |* #c1 * #tl #eql1 >eql1 #Hl1marks >reverse_append >reverse_single
+ whd in ⊢ ((???%)→?); whd in ⊢ ((???(????%))→?);
+ >associative_append whd in ⊢ ((???(????%))→?); #Htb
+ * #tc * whd in ⊢ (%→?); * #_ #Htc lapply (Htc … Htb) -Htc -Htb *
+ [* >Hl1marks [#Htemp destruct (Htemp)] @memb_append_l2 @memb_hd]
+ * * #_ >append_cons <associative_append #Htc lapply (Htc … (refl …) (refl …) ?)
+ [#x #membx cases (memb_append … membx) -membx #membx
+ [cases (memb_append … membx) -membx #membx
+ [@Hl1marks @memb_append_l1 @daemon
+ |>(memb_single … membx) %
+ ]
+ |@Hl2marks @membx
+ ]]
+ -Htc #Htc #_ whd in ⊢ (%→?); * #Houtc #_ cases (Houtc (reverse (FinProd FSUnialpha FinBool) ((reverse STape tl@[〈grid,false〉])@l2)
+ @c1::〈c,true〉::〈grid,false〉::ls) comma)
+ -Houtc #Houtc lapply (Houtc … Htc) -Houtc -Htc #Houtc #_
+ >Houtc >reverse_append >reverse_append >reverse_single
+ >reverse_reverse >associative_append >associative_append
+ >associative_append %
+qed.
+
+(* OLD
+definition init_copy ≝
+ seq ? (adv_mark_r ?)
+ (seq ? init_current_on_match
+ (seq ? (move_r ?)
+ (adv_to_mark_r ? (is_marked ?)))).
+
+definition R_init_copy ≝ λt1,t2.
+ ∀l1,l2,c,l3,d,rs.
+ no_marks l1 → no_grids l1 →
+ no_marks l2 → no_grids l2 → is_grid c = false → is_grid d =false →
+ t1 = midtape STape (l1@〈grid,false〉::l2@〈c,false〉::〈grid,false〉::l3) 〈comma,true〉 (〈d,false〉::rs) →
+ t2 = midtape STape (〈comma,false〉::l1@〈grid,false〉::l2@〈c,true〉::〈grid,false〉::l3) 〈d,true〉 rs.
+
+lemma list_last: ∀A.∀l:list A.
+ l = [ ] ∨ ∃a,l1. l = l1@[a].
+#A #l <(reverse_reverse ? l) cases (reverse A l)
+ [%1 //
+ |#a #l1 %2 @(ex_intro ?? a) @(ex_intro ?? (reverse ? l1)) //
+ ]
+qed.
+
+lemma sem_init_copy : Realize ? init_copy R_init_copy.
+#intape
+cases (sem_seq ????? (sem_adv_mark_r ?)
+ (sem_seq ????? sem_init_current_on_match
+ (sem_seq ????? (sem_move_r ?)
+ (sem_adv_to_mark_r ? (is_marked ?)))) intape)
+#k * #outc * #Hloop #HR
+@(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
+#l1 #l2 #c #l3 #d #rs #Hl1marks #Hl1grids #Hl2marks #Hl2grids #Hc #Hd #Hintape
+cases HR -HR
+#ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hintape) -Hta -Hintape #Hta
+* #tb * whd in ⊢ (%→?);
+>append_cons #Htb lapply (Htb (〈comma,false〉::l1) l2 c … Hta)
+ [@Hd |@Hc |@Hl2grids
+ |#x #membx cases (orb_true_l … membx) -membx #membx
+ [>(\P membx) // | @Hl1grids @membx]
+ ] -Htb #Htb
+* #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc -Htb
+>reverse_append >reverse_cons cases (list_last ? l2)
+ [#Hl2 >Hl2 >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
+ whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd -Htc
+ [* whd in ⊢ ((??%?)→?); #Habs destruct (Habs)]
+ * #_ #Htf lapply (Htf … (refl …) (refl …) ?)
+ [#x >reverse_cons #membx cases (memb_append … membx) -membx #membx
+ [@Hl1marks @daemon |>(memb_single … membx) //]
+ -Htf
+ |#Htf >Htf >reverse_reverse >associative_append %
+ ]
+ |* #a * #l21 #Heq >Heq >reverse_append >reverse_single
+ >associative_append >associative_append >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
+ whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd -Htc
+ [* >Hl2marks [#Habs destruct (Habs) |>Heq @memb_append_l2 @memb_hd]]
+ * #_ <associative_append <associative_append #Htf lapply (Htf … (refl …) (refl …) ?)
+ [#x >reverse_cons #membx cases (memb_append … membx) -membx #membx
+ [cases (memb_append … membx) -membx #membx
+ [@Hl2marks >Heq @memb_append_l1 @daemon
+ |>(memb_single … membx) //]
+ |cases (memb_append … membx) -membx #membx
+ [@Hl1marks @daemon |>(memb_single … membx) //]
+ ]
+ | #Htf >Htf >reverse_append >reverse_reverse
+ >reverse_append >reverse_reverse >associative_append
+ >reverse_single >associative_append >associative_append
+ >associative_append %
+ ]
+ ]
+qed. *)
+
+definition exec_action ≝
+ init_copy · copy · move_r … · move_tape.
+
+definition map_move ≝
+ λc,mv.match c with [ null ⇒ None ? | _ ⇒ Some ? 〈c,false,move_of_unialpha mv〉 ].
+
+(* - aggiungere a legal_tape le condizioni
+ only_bits ls, rs; bit_or_null c
+ - ci vuole un lemma che dimostri
+ bit_or_null c1 = true bit_or_null mv = true
+ mv ≠ null → c1 ≠ null
+ dal fatto che c1 e mv sono contenuti nella table
+ *)
+
+definition Pre_exec_action ≝ λt1.
+ ∃n,curconfig,ls,rs,c0,c1,s0,s1,table1,newconfig,mv,table2.
+ table_TM n (table1@〈comma,false〉::〈s1,false〉::newconfig@〈c1,false〉::〈comma,false〉::〈mv,false〉::table2) ∧
+ no_marks curconfig ∧ only_bits (curconfig@[〈s0,false〉]) ∧
+ only_bits (〈s1,false〉::newconfig) ∧ bit_or_null c1 = true ∧
+ |curconfig| = |newconfig| ∧
+ legal_tape ls 〈c0,false〉 rs ∧
+ t1 = midtape STape (〈c0,false〉::curconfig@〈s0,false〉::〈grid,false〉::ls) 〈grid,false〉
+ (table1@〈comma,true〉::〈s1,false〉::newconfig@〈c1,false〉::〈comma,false〉::〈mv,false〉::table2@〈grid,false〉::rs).
+
+definition R_exec_action ≝ λt1,t2.
+ ∀n,curconfig,ls,rs,c0,c1,s0,s1,table1,newconfig,mv,table2.
+ table_TM n (table1@〈comma,false〉::〈s1,false〉::newconfig@〈c1,false〉::〈comma,false〉::〈mv,false〉::table2) →
+ no_marks curconfig → only_bits (curconfig@[〈s0,false〉]) →
+ only_bits (〈s1,false〉::newconfig) → bit_or_null c1 = true →
+ |curconfig| = |newconfig| →
+ legal_tape ls 〈c0,false〉 rs →
+ t1 = midtape STape (〈c0,false〉::curconfig@〈s0,false〉::〈grid,false〉::ls) 〈grid,false〉
+ (table1@〈comma,true〉::〈s1,false〉::newconfig@〈c1,false〉::〈comma,false〉::〈mv,false〉::table2@〈grid,false〉::rs) →
+ ∀t1'.t1' = lift_tape ls 〈c0,false〉 rs →
+ ∃ls1,rs1,c2.
+ t2 = midtape STape ls1 〈grid,false〉
+ (〈s1,false〉::newconfig@〈c2,false〉::〈grid,false〉::
+ table1@〈comma,false〉::〈s1,false〉::newconfig@〈c1,false〉::〈comma,false〉::〈mv,false〉::table2@〈grid,false〉::rs1) ∧
+ lift_tape ls1 〈c2,false〉 rs1 =
+ tape_move STape t1' (map_move c1 mv) ∧ legal_tape ls1 〈c2,false〉 rs1.
+
+(* move the following 2 lemmata to mono.ma *)
+lemma tape_move_left_eq :
+ ∀A.∀t:tape A.∀c.
+ tape_move ? t (Some ? 〈c,L〉) =
+ tape_move_left ? (left ? t) c (right ? t).
+//
+qed.
+
+lemma tape_move_right_eq :
+ ∀A.∀t:tape A.∀c.
+ tape_move ? t (Some ? 〈c,R〉) =
+ tape_move_right ? (left ? t) c (right ? t).
+//
+qed.
+
+lemma lift_tape_not_null :
+ ∀ls,c,bc,rs.c ≠ null → lift_tape ls 〈c,bc〉 rs = midtape ? ls 〈c,bc〉 rs.
+#ls #c #bc #rs cases c //
+#Hfalse @False_ind /2/
+qed.
+
+lemma merge_char_not_null :
+ ∀c1,c2.c1 ≠ null → merge_char c1 c2 ≠ null.
+#c1 #c2 @not_to_not cases c2
+[ #c1' normalize #Hfalse destruct (Hfalse)
+| normalize //
+| *: normalize #Hfalse destruct (Hfalse)
+]
+qed.
+
+lemma merge_char_null : ∀c.merge_char null c = c.
+* //
+qed.
+
+lemma merge_char_cases : ∀c1,c2.merge_char c1 c2 = c1 ∨ merge_char c1 c2 = c2.
+#c1 *
+[ #c1' %2 %
+| % %
+| *: %2 % ]
+qed.
+
+(* lemma merge_char_c_bit :
+ ∀c1,c2.is_bit c2 = true → merge_char c1 c2 = c2.
+#c1 *
+[ #c2' #_ %
+|*: normalize #Hfalse destruct (Hfalse) ]
+qed.
+
+lemma merge_char_c_bit :
+ ∀c1,c2.is_null c2 = true → merge_char c1 c2 = c1.
+#c1 *
+[ #c2' #_ %
+|*: normalize #Hfalse destruct (Hfalse) ]
+qed.
+
+*)
+
+(*lemma GRealize_to_Realize :
+ ∀sig,M,R.GRealize sig M (λx.True) R → Realize sig M R.
+#sig #M #R #HR #t @HR //
+qed.*)
+
+lemma sem_exec_action : GRealize ? exec_action Pre_exec_action R_exec_action.
+@(sem_seq_app_guarded … (Realize_to_GRealize … sem_init_copy) ???)
+[3:@(sem_seq_app_guarded … sem_copy)
+ [3:@(sem_seq_app_guarded … (Realize_to_GRealize … (sem_move_r STape)))
+ [3:@(Realize_to_GRealize … (sem_move_tape …))
+ |@(λt.True)
+ |4://
+ |5:@sub_reflexive]
+ |@(λt.True)
+ |4://
+ |5:@sub_reflexive]
+|4:#t1 #t2
+ * #n * #curconfig * #ls * #rs * #c0 * #c1 * #s0 * #s1 * #table1 * #newconfig
+ * #mv * #table2 * * * * * * *
+ #Htable #Hcurconfig1 #Hcurconfig2 #Hnewconfig #Hc1 #Hlen #Htape #Ht1
+ whd in ⊢ (%→?); >Ht1 #HR
+ lapply (HR (〈c0,false〉::curconfig) table1 s0 ls s1
+ (newconfig@〈c1,false〉::〈comma,false〉::〈mv,false〉::table2@〈grid,false〉::rs) ???? (refl ??))
+ [(*Hcurconfig2*) @daemon
+ |(*by Htable *) @daemon
+ |(*Hcurconfig2*) @daemon
+ |(*Hcurconfig1*) @daemon ]
+ -HR #Ht2 whd
+ %{(〈grid,false〉::ls)} %{s0} %{s1} %{c0} %{c1} %{(〈mv,false〉::table2@〈grid,false〉::rs)}
+ %{newconfig} %{(〈comma,false〉::reverse ? table1)} %{curconfig} >Ht2
+ % [ % [ % [ % [ % [ % [ % [ %
+ [ %
+ |(*by Htabel*) @daemon ]
+ |(*by Htable*) @daemon ]
+ |// ]
+ |>Hlen % ]
+ |@Hcurconfig2 ]
+ |@Hnewconfig ]
+ |cases Htape * * // ]
+ | // ]
+ |1,2:skip]
+#ta #outtape #HR #n #curconfig #ls #rs #c0 #c1 #s0 #s1 #table1 #newconfig #mv
+#table2 #Htable #Hcurconfig1 #Hcurconfig2 #Hnewconfig #Hbitnullc1 #Hlen #Htape
+#Hta #ta' #Hta'
+cases HR -HR #tb * whd in ⊢ (%→?); #Htb
+lapply (Htb (〈c0,false〉::curconfig) table1 s0 ls s1
+ (newconfig@〈c1,false〉::〈comma,false〉::〈mv,false〉::table2@〈grid,false〉::rs)
+ ???? Hta) -Htb
+[ (*Hcurconfig2*) @daemon
+| (*Htable*) @daemon
+| (* by Htape bit_or_null c0 = true, moreover Hcurconfig2 *) @daemon
+| (*Hcurconfig1*) @daemon ]
+#Htb * #tc * whd in ⊢ (%→?); #Htc
+lapply (Htc (〈grid,false〉::ls) s0 s1 c0 c1 (〈mv,false〉::table2@〈grid,false〉::rs) newconfig (〈comma,false〉::reverse ? table1) curconfig Htb ????????) -Htc
+[9:|*:(* bit_or_null c0,c1; |curconfig| = |newconfig|*) @daemon ]
+#Htc * #td * whd in ⊢ (%→?); * #_ #Htd lapply (Htd … Htc) -Htd whd in ⊢(???(??%%%)→?);#Htd
+whd in ⊢ (%→?); #Houtc whd in Htd:(???%); whd in Htd:(???(??%%%));
+lapply (Houtc rs n
+ (〈comma,false〉::〈c1,false〉::reverse ? newconfig@〈s1,false〉::〈comma,false〉::reverse ? table1)
+ mv table2 (merge_char c0 c1) (reverse ? newconfig@[〈s1,false〉]) ls ????????)
+[3: cases Htape -Htape * * #Hnomarks #Hbits #Hc0 #Hlsrs % [ % [ %
+ [ #x #Hx cases (orb_true_l … Hx) #Hx'
+ [ >(\P Hx') %
+ | @Hnomarks @memb_cons // ]
+ | @Hbits ]
+ | cases (merge_char_cases c0 c1) #Hmerge >Hmerge // ]
+ | cases (true_or_false (c0 == null)) #Hc0'
+ [ cases Hlsrs -Hlsrs
+ [ *
+ [ >(\P Hc0') * #Hfalse @False_ind /2/
+ | #Hlsnil % %2 // ]
+ | #Hrsnil %2 // ]
+ | % % @merge_char_not_null @(\Pf Hc0') ] ]
+|4:>Htd @(eq_f3 … (midtape ?))
+ [ @eq_f @eq_f >associative_append >associative_append %
+ | %
+ | % ]
+| %
+|| >reverse_cons >reverse_cons >reverse_append >reverse_reverse
+ >reverse_cons >reverse_cons >reverse_reverse
+ >associative_append >associative_append >associative_append
+ >associative_append >associative_append
+ @Htable
+| (* well formedness of table *) @daemon
+| (* Hnewconfig *) @daemon
+| (* bit_or_null mv = true (well formedness of table) *) @daemon
+| -Houtc * #ls1 * #rs1 * #newc * #Hnewtapelegal * #Houtc *
+ [ *
+ [ * #Hmv #Htapemove
+ @(ex_intro ?? ls1) @(ex_intro ?? rs1) @(ex_intro ?? newc)
+ %
+ [ %
+ [ >Houtc -Houtc >reverse_append
+ >reverse_reverse >reverse_single @eq_f
+ >reverse_cons >reverse_cons >reverse_append >reverse_cons
+ >reverse_cons >reverse_reverse >reverse_reverse
+ >associative_append >associative_append
+ >associative_append >associative_append
+ >associative_append >associative_append %
+ | >Hmv >Hta' >Htapemove
+ (* mv = bit false -→ c1 = bit ? *)
+ cut (∃c1'.c1 = bit c1') [ @daemon ] * #c1' #Hc1
+ >Hc1 >tape_move_left_eq >(legal_tape_left … Htape)
+ >(legal_tape_right … Htape) %
+ ]
+ | //
+ ]
+ | * #Hmv #Htapemove
+ @(ex_intro ?? ls1) @(ex_intro ?? rs1) @(ex_intro ?? newc) %
+ [ %
+ [ >Houtc -Houtc >reverse_append
+ >reverse_reverse >reverse_single @eq_f
+ >reverse_cons >reverse_cons >reverse_append >reverse_cons
+ >reverse_cons >reverse_reverse >reverse_reverse
+ >associative_append >associative_append
+ >associative_append >associative_append
+ >associative_append >associative_append %
+ |>Hmv >Hta' >Htapemove
+ cut (∃c1'.c1 = bit c1') [ @daemon ] * #c1' #Hc1
+ >Hc1 >tape_move_right_eq >(legal_tape_left … Htape)
+ >(legal_tape_right … Htape) %
+ ]
+ | //
+ ]
+ ]
+ | * * * #Hmv #Hlseq #Hrseq #Hnewc
+ @(ex_intro ?? ls1) @(ex_intro ?? rs1) @(ex_intro ?? newc) %
+ [ %
+ [ >Houtc -Houtc >reverse_append
+ >reverse_reverse >reverse_single @eq_f
+ >reverse_cons >reverse_cons >reverse_append >reverse_cons
+ >reverse_cons >reverse_reverse >reverse_reverse
+ >associative_append >associative_append
+ >associative_append >associative_append
+ >associative_append >associative_append %
+ |>Hmv >Hta' cases c1 in Hnewc;
+ [ #c1' whd in ⊢ (??%?→?);#Hnewc <Hnewc
+ >Hlseq >Hrseq whd in ⊢ (??%%);
+ >(legal_tape_left … Htape) >(legal_tape_right … Htape) %
+ | whd in ⊢ (??%?→?); #Hnewc >Hnewc >Hlseq >Hrseq %
+ |*: whd in ⊢ (??%?→?);#Hnewc <Hnewc
+ >Hlseq >Hrseq whd in ⊢ (??%%);
+ >(legal_tape_left … Htape) >(legal_tape_right … Htape) %
+ ]
+ ]
+ | //
+ ]
+ ]