+(* termination *)
+lemma WF_mts_niltape:
+ WF ? (inv ? R_match_tuple_step_true) (niltape (FinProd FSUnialpha FinBool)).
+@wf #t1 whd in ⊢ (%→?); * #ls * #c * #rs * * #H destruct
+qed.
+
+lemma WF_mts_rightof:
+ ∀a,ls. WF ? (inv ? R_match_tuple_step_true) (rightof (FinProd FSUnialpha FinBool) a ls).
+#a #ls @wf #t1 whd in ⊢ (%→?); * #ls * #c * #rs * * #H destruct
+qed.
+
+lemma WF_mts_leftof:
+ ∀a,ls. WF ? (inv ? R_match_tuple_step_true) (leftof (FinProd FSUnialpha FinBool) a ls).
+#a #ls @wf #t1 whd in ⊢ (%→?); * #ls * #c * #rs * * #H destruct
+qed.
+
+lemma WF_cst_midtape_grid:
+ ∀ls,b,rs. WF ? (inv ? R_match_tuple_step_true)
+ (midtape (FinProd … FSUnialpha FinBool) ls 〈grid,b〉 rs).
+#ls #b #rs @wf #t1 whd in ⊢ (%→?); * #ls' * #c' * #rs' * * #H destruct
+* #Hfalse @False_ind @Hfalse %
+qed.
+
+definition Pre_match_tuple ≝ λt.
+ ∃ls,cur,rs. t = midtape STape ls cur rs ∧
+ (is_grid (\fst cur) = true ∨
+ (∃ls0,c,l1,l2,c1,l3,l4,rs0,n.
+ only_bits_or_nulls l1 ∧ no_marks l1 ∧
+ bit_or_null c = true ∧ bit_or_null c1 = true ∧
+ only_bits_or_nulls l3 ∧ S n = |l1| ∧|l1| = |l3| ∧
+ table_TM (S n) (l2@〈c1,false〉::l3@〈comma,false〉::l4) ∧
+ ls = 〈grid,false〉::ls0 ∧ cur = 〈c,true〉 ∧
+ rs = l1@〈grid,false〉::l2@〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs0)).
+
+lemma acc_Realize_to_acc_GRealize: ∀sig,M.∀q:states sig M.∀P,R1,R2.
+ M ⊨ [q:R1,R2] → accGRealize sig M q P R1 R2.
+#alpha #M #q #Pre #R1 #R2 #HR #t #HPre
+cases (HR t) -HR #k * #outc * * #Hloop #HRtrue #HRfalse
+@(ex_intro ?? k) @(ex_intro ?? outc) %
+ [ % [@Hloop] @HRtrue | @HRfalse]
+qed.
+
+
+lemma terminate_match_tuple:
+ ∀t. Pre_match_tuple t → Terminate ? match_tuple t.
+#t #HPre
+@(terminate_while_guarded ???
+ Pre_match_tuple …
+ (acc_Realize_to_acc_GRealize ??? Pre_match_tuple … sem_match_tuple_step)
+ … HPre) [%]
+ [-HPre -t #t1 #t2 #HPre cases HPre #ls * * #curl #curr * #rs * #Ht1 *
+ [(* absurd case *)
+ #Hgrid * #ls1 * #cur1 * #rs1 * * >Ht1 #Hdes destruct (Hdes)
+ #Habs @False_ind @(absurd ?? Habs) @(is_grid_true … Hgrid)
+ |* #ls0 * #c * #l1 * #l2 * #c1 * #l3 * #l4 * #rs0 * #n
+ * * * * * * * * * *
+ #Hl1 #Hmarksl1 #Hc #Hc1 #Hl3 #lenl1 #eqlen #Htable #Hls #Hcur #Hrs
+ * #ls1 * #cur1 * #rs1 * * >Ht1 #Hdes destruct (Hdes) #Hdes #H
+ lapply (H … Hl1 Hmarksl1 Hc Hc1 Hl3 lenl1 eqlen Htable Hls Hcur Hrs)
+ -H *
+ [* [ * #Hdes #Ht2 >Ht2
+ @ex_intro [2:@ex_intro [2: @ex_intro [2: % [%]|]|]|]
+ %1 %
+ |* #test * #c2 * #l5 * #l6 * #Hl4 #Ht2
+ cut (∃l7,l8. l6 = l7@〈comma,false 〉::l8 ∧ |l7| = |l1|) [@daemon]
+ * #l7 * #l8 * #Hl6 #eqlen1
+ @ex_intro [2:@ex_intro [2: @ex_intro [2: % [@Ht2]|]|]|] %2
+ @(ex_intro … ls0) @(ex_intro … c) @(ex_intro … l1)
+ @(ex_intro … (l2@〈c1,false〉::l3@〈comma,false〉::l5@[〈bar,false〉]))
+ @(ex_intro … c2) @(ex_intro … l7) @(ex_intro … l8)
+ @(ex_intro … rs0) @(ex_intro … n)
+ % [2: >Hl6 >associative_append >associative_append @eq_f @eq_f @eq_f
+ @eq_f >associative_append @eq_f @eq_f >associative_append % ]
+ % [2: %] % [2: %] % [2:@daemon] % [2: @sym_eq @eqlen1]
+ % [2: @lenl1] % [2: #x #memx @daemon]
+ % [2: @daemon] % [2: @Hc] % [2: @Hmarksl1] @Hl1
+ ]
+ |* * #_ #_ #H cases (current_to_midtape … H) #ls * #rs #Ht1
+ >Ht1 @ex_intro [2:@ex_intro [2: @ex_intro [2: % [%]|]|]|] %1 %
+ ]
+ ]
+ |cases HPre -HPre #ls * * #curl #curr * #rs * #Ht *
+ [#Hgrid >Ht >(is_grid_true … Hgrid) @WF_cst_midtape_grid
+ |* #ls0 * #c * #l1 * #l2 * #c1 * #l3 * #l4
+ cut (∃len. |l4| = len) [/2/] * #lenl4
+ lapply l4 lapply l3 lapply c1 lapply l2 lapply l1 lapply c lapply ls0 lapply Ht
+ lapply curr lapply curl lapply ls lapply rs lapply t -l4 -l3 -l2 -l1 -c1 -curr -curl -ls -t
+ -c -ls0 -rs
+ (* by induction on the length of l4 *)
+ @(nat_elim1 lenl4)
+ #len #Hind #t #rs #ls #cl #cr #Ht #ls0 #c #l1 #l2 #c1 #l3 #l4 #Hlen
+ * #rs0 * #n * * * * * * * * * *
+ #Hl1 #Hmarksl1 #Hc #Hc1 #Hl3 #lenl1 #eqlen #Htable #Hls #Hcur #Hrs
+ % #t1 >Ht whd in ⊢ (%→?); * #ls1 * #cur * #rs1 * * #Hdes destruct (Hdes)
+ #Hgrid #H lapply (H … Hl1 Hmarksl1 Hc Hc1 Hl3 lenl1 eqlen Htable Hls Hcur Hrs)
+ -H *
+ [* [ * #Hdes destruct (Hdes) #Ht1 >Ht1 @WF_cst_midtape_grid
+ | * #_ * #c2 * #l5 * #l6 * #Hl4 #Ht1
+ cut (∃l7,l8. l6 = l7@〈comma,false 〉::l8 ∧ |l7| = |l1|) [@daemon]
+ * #l7 * #l8 * #Hl6 #eqlen1
+ @(Hind … Ht1 ls0 c l1 (l2@〈c1,false〉::l3@〈comma,false〉::l5@[〈bar,false〉]) c2 l7 l8 … (refl …))
+ [<Hlen >Hl4 >Hl6 >length_append normalize in match (length … (cons …));
+ >length_append normalize in match (length … (cons …)); <plus_n_Sm
+ @le_S_S @daemon
+ |@(ex_intro … rs0) @(ex_intro … n) %
+ [2: >Hl6 >associative_append >associative_append @eq_f @eq_f @eq_f
+ @eq_f >associative_append @eq_f @eq_f >associative_append % ]
+ % [2: %] % [2: %] % [2:@daemon] % [2: @sym_eq @eqlen1]
+ % [2: @lenl1] % [2: #x #memx @daemon]
+ % [2: @daemon] % [2: @Hc] % [2: @Hmarksl1] @Hl1
+ ]
+ ]
+ |* * #_ #_ #H cases (current_to_midtape … H) #ls * #rs #Ht1
+ >Ht1 //
+ ]
+ ]
+qed.
+