+definition match_tuple ≝ whileTM ? match_tuple_step (inr … (inl … (inr … start_nop))).
+
+lemma is_grid_true : ∀c.is_grid c = true → c = grid.
+* normalize [ #b ] #H // destruct (H)
+qed.
+
+(* possible variante ?
+definition weakR_match_tuple ≝ λt1,t2.
+ (∀ls,cur,rs,b. t1 = midtape STape ls 〈grid,b〉 rs → t2 = t1) ∧
+ (∀c,l1,c1,l2,l3,ls0,rs0,n.
+ t1 = midtape STape (〈grid,false〉::ls0) 〈bit c,true〉 rs
+ (l1@〈grid,false〉::l2@〈bit c1,true〉::l3@〈grid,false〉::rs0) →
+ only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
+ table_TM (S n) (l2@〈c1,false〉::l3) →
+ (* facciamo match *)
+ (∃l4,newc,mv,l5.
+ 〈c1,false〉::l3 = l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
+ t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
+ (l2@l4@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
+ 〈grid,false〉::rs0))
+ ∨
+ (* 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〉 ∧
+ ∀l4,newc,mv,l5.
+ 〈c1,false〉::l3 ≠ l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).
+*)
+
+definition R_match_tuple0 ≝ λt1,t2.
+ ∀ls,cur,rs.
+ t1 = midtape STape ls cur rs →
+ (is_grid (\fst cur) = true → t2 = t1) ∧
+ (∀c,l1,c1,l2,l3,ls0,rs0,n.
+ ls = 〈grid,false〉::ls0 →
+ cur = 〈c,true〉 →
+ rs = l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈grid,false〉::rs0 →
+ is_bit c = true → is_bit c1 = true →
+ only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
+ table_TM (S n) (l2@〈bar,false〉::〈c1,false〉::l3) →
+ (* facciamo match *)
+ (∃l4,newc,mv,l5.
+ 〈bar,false〉::〈c1,false〉::l3 = l4@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
+ t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
+ (l2@l4@〈bar,false〉::〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
+ 〈grid,false〉::rs0))
+ ∨
+ (* 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〉 ∧
+ ∀l4,newc,mv,l5.
+ 〈bar,false〉::〈c1,false〉::l3 ≠ l4@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).
+
+axiom table_bit_after_bar :
+ ∀n,l1,c,l2.table_TM n (l1@〈bar,false〉::〈c,false〉::l2) → is_bit c = true.
+
+lemma wsem_match_tuple : WRealize ? match_tuple R_match_tuple0.
+#intape #k #outc #Hloop
+lapply (sem_while … sem_match_tuple_step intape k outc Hloop) [%] -Hloop
+* #ta * #Hstar @(star_ind_l ??????? Hstar)
+[ #tb whd in ⊢ (%→?); #Hleft
+ #ls #cur #rs #Htb cases (Hleft … Htb) #Hgrid #Houtc %
+ [ #_ @Houtc
+ | #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs
+ >Hcur in Hgrid; #Hgrid >(is_grid_true … Hgrid) normalize in ⊢ (%→?);
+ #Hc destruct (Hc)
+ ]
+| (* in the interesting case, we execute a true iteration, then we restart the
+ while cycle, finally we end with a false iteration *)
+ #tb #tc #td whd in ⊢ (%→?); #Htc
+ #Hstar1 #IH whd in ⊢ (%→?); #Hright lapply (IH Hright) -IH whd in ⊢ (%→?); #IH
+ #ls #cur #rs #Htb %
+ [ (* cur can't be true because we assume at least one iteration *)
+ #Hcur cases (Htc … Htb) * #Hfalse @False_ind @Hfalse @(is_grid_true … Hcur)
+ | (* current and a tuple are marked *)
+ #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs #Hc #Hc1 #Hl1bitnull #Hl1marks
+ #Hl1len #Htable cases (Htc … Htb) -Htc -Htb * #_ #Htc
+ (* expose the marked tuple in table *)
+ cut (∃la,lb,mv,lc.l3 = la@〈comma,false〉::lb@〈comma,false〉::mv::lc ∧
+ S n = |la| ∧ only_bits_or_nulls la)
+ [@daemon] * #la * #lb * #mv * #lc * * #Hl3 #Hlalen #Hlabitnull
+ >Hl3 in Htable; >append_cons #Htable
+ >(?: l2@〈bar,false〉::〈c1,true〉::l3@〈grid,false〉::rs0
+ = (l2@[〈bar,false〉])@〈c1,true〉::la@〈comma,false〉::(lb@〈comma,false〉::mv::
+ lc)@〈grid,false〉::rs0) in Hrs;
+ [| >associative_append normalize >Hl3
+ >associative_append normalize % ] #Hrs
+ cases (Htc ????????? Hl1bitnull Hl1marks ?? Hlabitnull Hl1len ? Htable Hls Hcur Hrs)
+ [5: <Hl1len @Hlalen
+ |4: whd in ⊢ (??%?); >Hc1 %
+ |3: whd in ⊢ (??%?); >Hc %
+ |-Htc *
+ [ (* case 1: match successful *)
+ * #Heq #Htc % %{[]} %{lb} %{mv} %{lc} destruct (Heq) %
+ [%
+ | cases (IH … Htc) -IH #Houtc #_ >(Houtc (refl ??))
+ >Htc @eq_f normalize >associative_append normalize
+ >associative_append normalize %
+ ]
+ | (* case 2: tuples don't match, we still have other tuples to try *)
+ * #Hdiff * #c2 * #l5 * #l6 * #Heqlblc #Htc
+ cases (IH ??? … Htc) -IH #_ #IH
+ (* by induction hypothesis *)
+ lapply (IH ? l1 c2 (l2@〈bar,false〉::〈c1,false〉::la@〈comma,false〉::l5) l6 ? rs0 n (refl ??) (refl ??) ???????)
+ [ generalize in match Htable;
+ >associative_append normalize
+ >associative_append normalize >Heqlblc
+ >associative_append normalize //
+ | @Hl1len
+ | @Hl1marks
+ | @Hl1bitnull
+ | (*???*) @daemon
+ | @Hc
+ | >associative_append normalize
+ >associative_append normalize
+ >associative_append %
+ |-IH *
+ [ (* the while finally matches a tuple *)
+ * #l7 * #newc * #mv0 * #l8 * #Hl7l8 #Houtc %
+ >Heqlblc @(ex_intro ?? (〈bar,false〉::〈c1,false〉::la@〈comma,false〉::l5@l7))
+ %{newc} %{mv0} %{l8} %
+ [ normalize >Hl7l8 >associative_append normalize
+ >associative_append %
+ | >Houtc @eq_f >associative_append normalize
+ >associative_append normalize >associative_append
+ normalize >associative_append %
+ ]
+ | (* the while fails finding a tuple: there are no matches in the whole table *)
+ * #Houtc #Hdiff1 %2 %
+ [ @Houtc
+ | #l50 #newc #mv0 #l51 >Heqlblc
+ @daemon
+ ]
+ ]
+ ]
+ ]
+ | (* match failed and there is no next tuple: the next while cycle will just exit *)
+ * * #Hdiff #Hnobars generalize in match (refl ? tc);
+ cases tc in ⊢ (???% → %);
+ [ #_ normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse)
+ |2,3: #x #xs #_ normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse) ]
+ #ls1 #cur1 #rs1 #Htc normalize in ⊢ (??%?→?); #Hcur1
+ cases (IH … Htc) -IH #IH #_ %2 %
+ [ destruct (Hcur1) >IH [ >Htc % | % ]
+ | #l4 #newc #mv0 #l5
+ (* no_bars except the first one, where the tuple does not match ⇒
+ no match *)
+ @daemon
+ ]
+ ]
+ ]
+qed.