+ ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
+
+include alias "basics/logic.ma".
+
+(*
+lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
+ ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →
+ f x1 x2 x3 x4 = f y1 y2 y3 y4.
+//
+qed-. *)
+
+lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
+ Some ? b = option_hd ? (l@[a]) .
+#A #l #a cases l normalize /2/
+qed.
+
+axiom tech_split2 : ∀A,l1,l2,l3,l4,x.
+ memb A x l1 = false → memb ? x l3 = false →
+ l1@x::l2 = l3@x::l4 → l1 = l3 ∧ l2 = l4.
+
+axiom injective_append : ∀A,l.injective … (λx.append A x l).
+
+lemma sem_match_tuple_step:
+ accRealize ? match_tuple_step (inr … (inl … (inr … 0)))
+ R_match_tuple_step_true R_match_tuple_step_false.
+@(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
+ (sem_seq … sem_compare
+ (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
+ (sem_nop …)
+ (sem_seq … sem_mark_next_tuple
+ (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
+ (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
+ (sem_nop ?) …)
+[(* is_grid: termination case *)
+ 2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
+ cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
+ [@injective_notb @Hgrid | <Heq @H1]
+|#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
+ * #tapec * #Hcompare #Hor
+ #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1bars #Hl1marks #Hc1 #Hl3 #eqn
+ #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
+ #notgridc -Htapea -Htapea1 -tapea #Htapeb
+ cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
+ cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen Hl1bars Hl3 Hl1marks … (refl …) Hc ?)
+ -Hcompare
+ [* #Htemp destruct (Htemp) #Htapec %1 % [%]
+ >Htapec in Hor; -Htapec *
+ [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
+ cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
+ |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
+ #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
+ %
+ ]
+ |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec
+ cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3)
+ [>H2 >H3 elim la
+ [@(not_to_not …H1) normalize #H destruct %
+ |#x #tl @not_to_not normalize #H destruct //
+ ]
+ ] #Hnoteq %2
+ cut (bit_or_null d' = true)
+ [cases la in H3;
+ [normalize in ⊢ (%→?); #H destruct //
+ |#x #tl #H @(Hl3 〈d',false〉)
+ normalize in H; destruct @memb_append_l2 @memb_hd
+ ]
+ ] #Hd'
+ >Htapec in Hor; -Htapec *
+ [* #taped * whd in ⊢ (%→?); #H @False_ind
+ cases (H … (refl …)) >(bit_or_null_not_grid ? Hd') #Htemp destruct (Htemp)
+ |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
+ #Htaped * #tapee * whd in ⊢ (%→?); #Htapee
+ <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
+ cases (Htapee … Htaped ???) -Htaped -Htapee
+ [* #rs3 * * (* we proceed by cases on rs4 *)
+ [(* rs4 is empty : the case is absurd since the tape
+ cannot end with a bar *)
+ * #d * #b * * * #Heq1 @False_ind
+ cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
+ >Hcut in Htable; >H3 >associative_append
+ normalize >Heq1 >Hcut <associative_append >Hcut
+ <associative_append #Htable @(absurd … Htable)
+ @last_of_table
+ |(* rs4 not empty *)
+ * #d2 #b2 #rs3' * #d * #b * * * #Heq1 #Hnobars
+ cut (memb STape 〈d2,b2〉 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::l4) = true)
+ [@memb_append_l2 @memb_cons
+ cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
+ >Hcut >H3 >associative_append @memb_append_l2
+ @memb_cons >Heq1 @memb_append_l2 @memb_cons @memb_hd] #d2intable
+ cut (is_grid d2 = false)
+ [@(no_grids_in_table … Htable … 〈d2,b2〉 d2intable)] #Hd2
+ cut (b2 = false)
+ [@(no_marks_in_table … Htable … 〈d2,b2〉 d2intable)] #Hb2
+ >Hb2 in Heq1; #Heq1 -Hb2 -b2
+ whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
+ [(* we know current is not grid *)
+ * #tapef * whd in ⊢ (%→?); #Htapef
+ cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp)
+ |* #tapef * whd in ⊢ (%→?); #Htapef
+ cases (Htapef … (refl …)) #_ -Htapef #Htapef
+ * #tapeg >Htapef -Htapef *
+ (* move_l *)
+ whd in ⊢ (%→?);
+ #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H #Htapeg
+ >Htapeg -Htapeg
+ (* init_current *)
+ whd in ⊢ (%→?); #Htapeout
+ %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
+ * #c00 #b00 #Hoption
+ lapply
+ (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
+ c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
+ [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
+ >associative_append
+ generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
+ whd in ⊢ (???(???%)); >associative_append >associative_append %
+ |>reverse_cons @Hoption
+ |cases la in H2;
+ [normalize in ⊢ (%→?); #Htemp destruct (Htemp)
+ @injective_notb @notgridc
+ |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
+ @bit_or_null_not_grid @(Hl1bars 〈c',false〉) @memb_append_l2 @memb_hd
+ ]
+ |cut (only_bits_or_nulls (la@(〈c',false〉::lb)))
+ [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
+ [#eqc0 >(\P eqc0) @Hc |@Hl1bars]
+ |#Hl1' #x #Hx @bit_or_null_not_grid @Hl1'
+ @memb_append_l1 @daemon
+ ]
+ |@daemon
+ |>reverse_append >reverse_cons >reverse_reverse
+ >reverse_append >reverse_reverse
+ >reverse_cons >reverse_append >reverse_reverse
+ >reverse_append >reverse_cons >reverse_reverse
+ >reverse_reverse
+ #Htapeout % [@Hnoteq]
+ @(ex_intro … d2)
+ cut (∃rs32.rs3 = lc@〈comma,false〉::rs32)
+ [ (*cases (tech_split STape (λc.c == 〈bar,false〉) l4)
+ [
+ | * #l41 * * #cbar #bfalse * #l42 * * #Hbar #Hl4 #Hl41
+ @(ex_intro ?? l41) >Hl4 in Heq1; #Heq1
+
+ cut (sublist … lc l3)
+ [ #x #Hx cases la in H3;
+ [ normalize #H3 destruct (H3) @Hx
+ | #p #la' normalize #Hla' destruct (Hla')
+ @memb_append_l2 @memb_cons @Hx ] ] #Hsublist*)
+ @daemon]
+ * #rs32 #Hrs3
+ (* cut
+ (〈c1,false〉::l3@〈comma,false〉::l4= la@〈d',false〉::rs3@〈bar,false〉::〈d2,b2〉::rs3')
+ [@daemon] #Hcut *)
+ cut (l4=rs32@〈bar,false〉::〈d2,false〉::rs3')
+ [ >Hrs3 in Heq1; @daemon ] #Hl4
+ @(ex_intro … rs32) @(ex_intro … rs3') %
+ [@Hl4
+ |>Htapeout @eq_f2
+ [@daemon
+ |(*>Hrs3 *)>append_cons
+ > (?:l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs
+ = (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::rs32@[〈bar,false〉])@〈d2,true〉::rs3'@〈grid,false〉::rs)
+ [|>associative_append normalize
+ >associative_append normalize
+ >associative_append normalize
+ >associative_append normalize
+ % ]
+ @eq_f2 [|%]
+ @(injective_append … (〈d2,false〉::rs3'))
+ >(?:(la@[〈c',false〉])@((((lb@[〈grid,false〉])@l2@[〈bar,false〉])@la)@[〈d',false〉])@rs3
+ =((la@〈c',false〉::lb)@([〈grid,false〉]@l2@[〈bar,false〉]@la@[〈d',false〉]@rs3)))
+ [|>associative_append >associative_append
+ >associative_append >associative_append >associative_append
+ >associative_append >associative_append % ]
+ <H2 normalize (* <Hrs3 *)
+ >associative_append >associative_append >associative_append
+ @eq_f normalize @eq_f >associative_append
+ >associative_append @eq_f normalize @eq_f
+ >(append_cons ? 〈d',false〉) >associative_append
+ <Heq1 >Hl4 <associative_append <append_cons
+ <H3
+ >associative_append normalize
+ >associative_append normalize %
+ ]
+ ]
+ ]
+ ]
+ ]
+ |* #Hnobars #Htapee >Htapee -Htapee *
+ [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
+ cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
+ whd in ⊢ (%→?); #Htapeout %2
+ >(Htapeout … (refl …)) %
+ [ %
+ [ @Hnoteq
+ | whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
+ ]
+ | %
+ ]
+ |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
+ cases (Htapef … (refl …)) -Htapef
+ whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
+ ]
+ |(* no marks in table *)
+ #x #membx @(no_marks_in_table … Htable)
+ @memb_append_l2 @memb_cons
+ cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
+ >H3 >associative_append @memb_append_l2 @memb_cons @membx
+ |(* no grids in table *)
+ #x #membx @(no_grids_in_table … Htable)
+ @memb_append_l2 @memb_cons
+ cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
+ >H3 >associative_append @memb_append_l2 @memb_cons @membx
+ |whd in ⊢ (??%?); >(bit_or_null_not_grid … Hd') >(bit_or_null_not_bar … Hd') %
+ ]
+ ]
+ |#x #membx @(no_marks_in_table … Htable)
+ @memb_append_l2 @memb_cons @memb_cons @memb_append_l1 @membx
+ |#x #membx @(no_marks_in_table … Htable)
+ cases (memb_append … membx) -membx #membx
+ [@memb_append_l1 @membx | @memb_append_l2 >(memb_single … membx) @memb_hd]
+ |>associative_append %
+ ]
+ ]
+qed.
+
+
+(*
+ MATCH TUPLE
+
+ scrolls through the tuples in the transition table until one matching the
+ current configuration is found
+*)
+
+definition match_tuple ≝ whileTM ? match_tuple_step (inr … (inl … (inr … 0))).
+
+definition R_match_tuple ≝ λt1,t2.
+ ∀ls,c,l1,c1,l2,rs,n.
+ is_bit c = true → only_bits_or_nulls l1 → is_bit c1 = true → n = |l1| →
+ table_TM (S n) (〈c1,false〉::l2) →
+ t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
+ (l1@〈grid,false〉::〈c1,true〉::l2@〈grid,false〉::rs) →
+ (* facciamo match *)
+ (∃l3,newc,mv,l4.
+ 〈c1,false〉::l2 = l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv@l4 ∧
+ t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
+ (l3@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv@l4@〈grid,false〉::rs))
+ ∨
+ (* non facciamo match su nessuna tupla;
+ non specifichiamo condizioni sul nastro di output, perché
+ non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
+ (current ? t2 = Some ? 〈grid,true〉 ∧
+ ∀l3,newc,mv,l4.
+ 〈c1,false〉::l2 ≠ l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv@l4).