]> matita.cs.unibo.it Git - helm.git/blobdiff - matita/matita/lib/turing/universal/match_machines.ma
New notation for congruence
[helm.git] / matita / matita / lib / turing / universal / match_machines.ma
index ea7ab99db3994c1b5e53588fc52c42ab9191029e..6c7ccc9234a9ae0f52ef9009b797da06c4844c43 100644 (file)
@@ -40,10 +40,10 @@ if current (* x *) = #
  *)
  
 definition mark_next_tuple ≝ 
-  seq ? (adv_to_mark_r ? bar_or_grid)
+  adv_to_mark_r ? bar_or_grid ·
      (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
        (move_right_and_mark ?) (nop ?) tc_true).
-
+     
 definition R_mark_next_tuple ≝ 
   λt1,t2.
     ∀ls,c,rs1,rs2.
@@ -78,66 +78,63 @@ lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
    @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
    -Hloop
    #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
-   cases (Hleft … Hrs)
+   cases (proj2 ?? Hleft … Hrs)
    [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
-   | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
-     [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
+   | * * #_ #Hta #_ cases (tech_split STape (λc.is_bar (\fst c)) rs1)
+     [ #H1 %2 % [@H1]
+      lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
        [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
        | %
        | -Hta #Hta cases Hright
-         [ * #tb * whd in ⊢ (%→?); #Hcurrent
-           @False_ind cases (Hcurrent 〈grid,false〉 ?)
-           [ normalize in ⊢ (%→?); #Hfalse destruct (Hfalse)
-           | >Hta % ]
-         | * #tb * whd in ⊢ (%→?); #Hcurrent
-           cases (Hcurrent 〈grid,false〉 ?)
-           [  #_ #Htb whd in ⊢ (%→?); #Houtc
-             %2 %
-             [ @H1
-             | >Houtc >Htb >Hta % ]
-           | >Hta % ]
+         [ * #tb * whd in ⊢ (%→?); * * #c1 * >Hta 
+          whd in ⊢ ((??%?)→?); #H destruct (H) whd in ⊢ ((??%?)→?); #H destruct
+         | * #tb * whd in ⊢ (%→?); * #_ #Htb >Htb >Hta 
+           whd in ⊢ (%→?); #H @H
          ]
        ]
-    | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
-      % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
+    |* #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
+     % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
      lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
-     #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
+      [#x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
        #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
        >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
-     | whd in ⊢ (??%?); >Hc0 %
-     | >Hsplit >associative_append % ] -Hta #Hta
-       cases Hright
-       [ * #tb * whd in ⊢ (%→?); #Hta'
-         whd in ⊢ (%→?); #Htb
-         cases (Hta' c0 ?)
-         [ #_ #Htb' >Htb' in Htb; #Htb
-           generalize in match Hsplit; -Hsplit
-           cases rs4 in Hta;
-           [ #Hta #Hsplit >(Htb … Hta)
-             >(?:c0 = 〈bar,false〉)
-             [ @(ex_intro ?? grid) @(ex_intro ?? false)
-               % [ % [ % 
-               [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ] 
-               | (* Hc0 *) @daemon ]
-           | #r5 #rs5 >(eq_pair_fst_snd … r5)
-             #Hta #Hsplit >(Htb … Hta)
-             >(?:c0 = 〈bar,false〉)
-             [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
-               % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
-                     | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
-             | * #tb * whd in ⊢ (%→?); #Hta'
-               whd in ⊢ (%→?); #Htb
-               cases (Hta' c0 ?)
-               [ #Hfalse @False_ind >Hfalse in Hc0;
-                 #Hc0 destruct (Hc0)
-               | >Hta % ]
-]]]]
+      |whd in ⊢ (??%?); >Hc0 %
+      |>Hsplit >associative_append % 
+      ]-Hta #Hta
+     cases Hright -Hright 
+      [* whd in ⊢ (%→?); #tb * * * #c1 * >Hta -Hta 
+       whd in ⊢ (??%?→?); #H destruct (H) #Hc1 #Htb
+       whd in ⊢ (%→?); #Houtc 
+       cut (c1=〈bar,false〉)
+        [lapply Hc1 lapply Hsplit cases c1 #c1l #c1r #Hsplit
+         cases c1l normalize 
+          [#b #H destruct |2,3,5:#H destruct] 
+           #_ @eq_f @(Hrs1 … 〈c1l,c1r〉) >Hsplit @memb_append_l2 @memb_hd]
+       #Hcut lapply Hsplit -Hsplit
+       cases rs4 in Htb;
+        [#Htb lapply(Houtc … Htb) -Houtc #Houtc #Hsplit
+         @(ex_intro ?? grid) @(ex_intro ?? false) % 
+          [% [ % [<Hcut @Hsplit |@Hrs3 ] | % ] 
+          |>Houtc >Hcut % 
+          ]
+        |* #r5l #r5r #rs5 #Htb
+         lapply(Houtc … Htb) -Houtc #Houtc #Hsplit
+         @(ex_intro ?? r5l) @(ex_intro ?? r5r) % 
+          [%[%[<Hcut @Hsplit| @Hrs3] | % ]
+          |>Houtc >Hcut % 
+          ]
+        ]
+      |* whd in ⊢ (%→?); #tb * * 
+       #H @False_ind >Hta in H; #H lapply(H c0 (refl …))
+       >Hc0 #H destruct
+      ]
+    ]
+  ]
+]
 qed.
 
 definition init_current_on_match ≝ 
-  (seq ? (move_l ?)
-    (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
-      (seq ? (move_r ?) (mark ?)))).
+  move_l ? · adv_to_mark_l ? (λc:STape.is_grid (\fst c)) · move_r ? · mark ?.
           
 definition R_init_current_on_match ≝ λt1,t2.
   ∀l1,l2,c,rs. no_grids l1 → is_grid c = false → 
@@ -153,26 +150,28 @@ cases (sem_seq ????? (sem_move_l ?)
 #k * #outc * #Hloop #HR 
 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
 #l1 #l2 #c #rs #Hl1 #Hc #Hintape
-cases HR -HR #ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hintape) -Hta -Hintape 
+cases HR -HR #ta * whd in ⊢ (%→?); * #_ #Hta lapply (Hta … Hintape) -Hta -Hintape 
 generalize in match Hl1; cases l1
   [#Hl1 whd in ⊢ ((???(??%%%))→?); #Hta
-   * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Hta
-    [* >Hc #Htemp destruct (Htemp) ]
-   * #_ #Htc lapply (Htc [ ] 〈grid,false〉 ? (refl ??) (refl …) Hl1) 
-   whd in ⊢ ((???(??%%%))→?); -Htc #Htc
-   * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htc -Htd 
-   whd in ⊢ ((???(??%%%))→?); #Htd
-   whd in ⊢ (%→?); #Houtc lapply (Houtc … Htd) -Houtc #Houtc
+   * #tb * whd in ⊢ (%→?); * #_ #Htb cases (Htb … Hta) -Htb -Hta #_
+   (* [* >Hc #Htemp destruct (Htemp) ] *)
+   #Htb cases (Htb … Hc) -Htb #Htb #_ 
+   lapply (Htb [ ] 〈grid,false〉 ? (refl ??) (refl …) Hl1) 
+   whd in ⊢ ((???(??%%%))→?); -Htb #Htb
+   * #tc * whd in ⊢ (%→?); * #_ #Htc lapply (Htc … Htb) -Htb -Htc 
+   whd in ⊢ ((???(??%%%))→?); #Htc
+   whd in ⊢ (%→?); * #Houtc #_ lapply (Houtc … Htc) -Houtc #Houtc
    >Houtc % 
   |#d #tl #Htl whd in ⊢ ((???(??%%%))→?); #Hta
-   * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Htb
-    [* >(Htl … (memb_hd …)) #Htemp destruct (Htemp)]    
-   * #Hd >append_cons #Htb lapply (Htb … (refl ??) (refl …) ?)
+   * #tb * whd in ⊢ (%→?); * #_ #Htb cases (Htb … Hta) -Htb
+   #_ (* [* >(Htl … (memb_hd …)) #Htemp destruct (Htemp)]  *)
+   #Htb cases (Htb ?) -Htb [2: @Htl @memb_hd]
+   >append_cons #Htb #_ lapply (Htb … (refl ??) (refl …) ?)
     [#x #membx cases (memb_append … membx) -membx #membx
       [@Htl @memb_cons @membx | >(memb_single … membx) @Hc]]-Htb  #Htb
-   * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htb -Htc 
+   * #tc * whd in ⊢ (%→?); * #_ #Htc lapply (Htc … Htb) -Htb -Htc 
    >reverse_append >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
-   whd in ⊢ (%→?); #Houtc lapply (Houtc … Htc) -Houtc #Houtc 
+   whd in ⊢ (%→?); * #Houtc #_ lapply (Houtc … Htc) -Houtc #Houtc 
    >Houtc >reverse_cons >associative_append % 
   ]
 qed.   
@@ -239,10 +238,8 @@ qed.
 *)
 
 definition init_current ≝ 
-  seq ? (adv_to_mark_l ? (is_marked ?))
-    (seq ? (clear_mark ?)
-       (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
-          (seq ? (move_r ?) (mark ?)))).
+  adv_to_mark_l ? (is_marked ?) ·clear_mark ? ·
+    adv_to_mark_l ? (λc:STape.is_grid (\fst c)) · move_r ? · mark ?.
           
 definition R_init_current ≝ λt1,t2.
   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false → 
@@ -259,15 +256,15 @@ cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
              (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
 #k * #outc * #Hloop #HR 
 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
-cases HR -HR #ta * whd in ⊢ (%→?); #Hta 
-* #tb * whd in ⊢ (%→?); #Htb 
-* #tc * whd in ⊢ (%→?); #Htc 
-* #td * whd in ⊢ (%→%→?); #Htd #Houtc
+cases HR -HR #ta * whd in ⊢ (%→?); * #_ #Hta 
+* #tb * whd in ⊢ (%→?); * #_ #Htb 
+* #tc * whd in ⊢ (%→?); * #_ #Htc 
+* #td * whd in ⊢ (%→%→?); * #_ #Htd * #Houtc #_
 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
-cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
--Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
--Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ] 
--Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
+cases (Hta … Hintape) #_ -Hta #Hta cases (Hta (refl …)) -Hta 
+#Hta #_ lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
+-Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) #_ #Htc
+cases (Htc Hc) -Htc #Htc #_ lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
 -Htc #Htc lapply (Htd … Htc) -Htd
 >reverse_append >reverse_cons 
 >reverse_cons in Hc0; cases (reverse … l2)
@@ -283,14 +280,43 @@ qed.
 definition match_tuple_step ≝ 
   ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c))) 
    (single_finalTM ? 
-     (seq ? compare
+     (compare ·
       (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
         (nop ?)
-        (seq ? mark_next_tuple 
+        (mark_next_tuple ·
            (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
-             (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
+             (mark ?) (move_l ? · init_current) tc_true)) tc_true)))
     (nop ?) tc_true.
+    
+definition R_match_tuple_step_true_new ≝ λt1,t2.
+  ∃ls,cur,rs.t1 = midtape STape ls cur rs \wedge 
+  \fst cur ≠ grid ∧ 
+  (∀ls0,c,l1,l2,c1,l3,l4,rs0,n.
+   only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → 
+   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 → 
+   (* facciamo match *)
+   (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
+   t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
+         (l2@〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs0))
+   ∨
+   (* non facciamo match e marchiamo la prossima tupla *)
+   (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
+    ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
+    (* condizioni su l5 l6 l7 *)
+    t2 = midtape STape (〈grid,false〉::ls0) 〈c,true〉 
+          (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::
+           l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs0))
+   ∨  
+   (* non facciamo match e non c'è una prossima tupla:
+      non specifichiamo condizioni sul nastro di output, perché
+      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
+   (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
 
+(* universal version 
 definition R_match_tuple_step_true ≝ λt1,t2.
   ∀ls,cur,rs.t1 = midtape STape ls cur rs → 
   \fst cur ≠ grid ∧ 
@@ -318,7 +344,36 @@ definition R_match_tuple_step_true ≝ λt1,t2.
       non specifichiamo condizioni sul nastro di output, perché
       non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
-  
+*)
+
+definition R_match_tuple_step_true ≝ λt1,t2.
+  ∃ls,cur,rs.t1 = midtape STape ls cur rs \wedge 
+  \fst cur ≠ grid ∧ 
+  (∀ls0,c,l1,l2,c1,l3,l4,rs0,n.
+   only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → 
+   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 → 
+   (* facciamo match *)
+   (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
+   t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
+         (l2@〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs0))
+   ∨
+   (* non facciamo match e marchiamo la prossima tupla *)
+   (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
+    ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
+    (* condizioni su l5 l6 l7 *)
+    t2 = midtape STape (〈grid,false〉::ls0) 〈c,true〉 
+          (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::
+           l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs0))
+   ∨  
+   (* non facciamo match e non c'è una prossima tupla:
+      non specifichiamo condizioni sul nastro di output, perché
+      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
+   (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
+
 definition R_match_tuple_step_false ≝ λt1,t2.
   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
   
@@ -354,31 +409,31 @@ lemma sem_match_tuple_step:
              (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]
+ 2:#t1 #t2 #t3 whd in ⊢ (%→?); * #Hc #H #H1 whd #ls #c #rs #Ht1 %
+  [lapply(Hc c ?) [>Ht1 %] #Hgrid @injective_notb @Hgrid |>H1 @H]
 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Hcur
  * #tapec * whd in ⊢ (%→?); #Hcompare #Hor 
- #ls #cur #rs #Htapea >Htapea in Hcur; #Hcur cases (Hcur ? (refl ??)) 
- -Hcur #Hcur #Htapeb %
- [ % #Hfalse >Hfalse in Hcur; normalize #Hfalse1 destruct (Hfalse1)]
- #ls0 #c #l1 #l2 #c1 #l3 #l4 #rs0 #n #Hl1bitnull #Hl1marks #Hc #Hc1 #Hl3 #eqn
- #eqlen #Htable #Hls #Hcur #Hrs -Htapea >Hls in Htapeb; >Hcur >Hrs #Htapeb
+ cases Hcur * #c * -Hcur #Hcur #Hgrid #Htapeb cases (current_to_midtape … Hcur)
+ #ls * #rs #Htapea @(ex_intro … ls) @(ex_intro … c) @(ex_intro … rs) %
+  [%[@Htapea | cases (true_or_false (\fst c == grid))
+    [#eqc @False_ind >(\P eqc) in Hgrid; normalize #H destruct |#eqc @(\Pf eqc)]]] 
+ #ls0 #cur #l1 #l2 #c1 #l3 #l4 #rs0 #n #Hl1bitnull #Hl1marks #Hc #Hc1 #Hl3 #eqn
+ #eqlen #Htable #Hls -Hcur #Hcur #Hrs >Htapea in Htapeb; >Hls >Hcur >Hrs #Htapeb
  cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
- cases (Hcompare c c1 l1 l3 l2 (l4@〈grid,false〉::rs0) eqlen Hl1bitnull Hl3 Hl1marks … (refl …) Hc ?)  
+ cases (Hcompare cur c1 l1 l3 l2 (l4@〈grid,false〉::rs0) eqlen Hl1bitnull 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 *
+     [2: * #t3 * whd in ⊢ (%→?); * #H #_ @False_ind
+      lapply (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
+     |* #taped * whd in ⊢ (%→?); * #_ 
       #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped
       %
      ]
    |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec 
-    cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3) 
+    cut (〈cur,false〉::l1 ≠ 〈c1,false〉::l3) 
       [>H2 >H3 elim la
-        [@(not_to_not …H1) normalize #H destruct % 
+        [@(not_to_not …H1) normalize #H destruct (H) 
         |#x #tl @not_to_not normalize #H destruct // 
         ]
       ] #Hnoteq
@@ -390,9 +445,9 @@ lemma sem_match_tuple_step:
         ] 
       ] #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 #_
+     [* #taped * whd in ⊢ (%→?); * * #c0 * normalize in ⊢ (%→?); 
+      #Hdes destruct (Hdes) >(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 
@@ -419,14 +474,13 @@ lemma sem_match_tuple_step:
           >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
+            * #tapef * whd in ⊢ (%→?); * * #c0 *
+            normalize in ⊢ (%→?); #Hdes destruct (Hdes) >Hd2 
+            #Htemp destruct (Htemp) 
+           |* #tapef * whd in ⊢ (%→?); * #_ #Htapef 
             * #tapeg >Htapef -Htapef * 
             (* move_l *)
-            whd in ⊢ (%→?); 
-            #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
+            whd in ⊢ (%→?); * #_ #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
             >Htapeg -Htapeg
             (* init_current *)
              whd in ⊢ (%→?); #Htapeout
@@ -504,14 +558,13 @@ lemma sem_match_tuple_step:
                  ]
               ]
        |* #Hnobars #Htapee >Htapee -Htapee *
-         [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
-          cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
-          whd in ⊢ (%→?); #Htapeout %2 % 
+         [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); * #_ 
+          #Htapef >Htapef -Htapef
+          whd in ⊢ (%→?); * #Htapeout #_ %2 % 
           [% [//] whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
           | >(Htapeout … (refl …)) % ]
-         |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
-          cases (Htapef … (refl …)) -Htapef 
-          whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp) 
+         |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); 
+          * #Hc0 lapply(Hc0 … (refl … )) normalize in ⊢ (%→?); #Htemp destruct (Htemp) 
          ]
        |(* no marks in table *)
         #x #membx @(no_marks_in_table … Htable) 
@@ -603,7 +656,7 @@ 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
+[ whd in ⊢ (%→?); #Hleft
   #ls #cur #rs #Htb cases (Hleft … Htb) #Hgrid #Houtc %
   [ #_ @Houtc 
   | #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs 
@@ -612,14 +665,17 @@ lapply (sem_while … sem_match_tuple_step intape k outc Hloop) [%] -Hloop
   ]
 | (* 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
+  #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)
+    #Hcur cases Htc #ls' * #c' * #rs' * * >Htb #Hdes destruct (Hdes) 
+    #Hfalse @False_ind @(absurd … (is_grid_true … Hcur) Hfalse)
   | (* 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
+   #Hl1len #Htable 
+   cases Htc #ls' * #c' * #rs' * * >Htb #Hdes destruct (Hdes)
+   -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)
@@ -682,20 +738,142 @@ lapply (sem_while … sem_match_tuple_step intape k outc Hloop) [%] -Hloop
    | (* 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
+     [ #_ @daemon (* long normalize *) (* 
+          normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse)
+     *)
+     |2,3: #x #xs #_ @daemon (* long normalize *) (* 
+                     normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse) *) ]
+     #ls1 #cur1 #rs1 #Htc @daemon (* long normalize *) (* 
+                          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.
 
+(* 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.
+     
 definition R_match_tuple ≝ λt1,t2.
   ∀ls,c,l1,c1,l2,rs,n.
   is_bit c = true → is_bit c1 = true →
@@ -716,11 +894,12 @@ definition R_match_tuple ≝ λt1,t2.
    ∀l3,newc,mv,l4.
    〈bar,false〉::〈c1,false〉::l2 ≠ l3@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4). 
 
-(* we still haven't proved termination *)
-axiom sem_match_tuple0 : Realize ? match_tuple R_match_tuple0.
+lemma sem_match_tuple0 : GRealize ? match_tuple Pre_match_tuple R_match_tuple0.
+@WRealize_to_GRealize [@terminate_match_tuple | @wsem_match_tuple]
+qed.
 
-lemma sem_match_tuple : Realize ? match_tuple R_match_tuple.
-generalize in match sem_match_tuple0; @Realize_to_Realize
+lemma sem_match_tuple : GRealize ? match_tuple Pre_match_tuple R_match_tuple.
+generalize in match sem_match_tuple0; @GRealize_to_GRealize
 #t1 #t2 #HR #ls #c #l1 #c1 #l2 #rs #n #Hc #Hc1 #Hl1bitsnulls #Hl1marks #Hl1len #Htable #Ht1
 cases (HR … Ht1) -HR #_ #HR
 @(HR ??? [] … (refl ??) (refl ??) (refl ??) Hc Hc1 Hl1bitsnulls Hl1marks