X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=matita%2Fmatita%2Flib%2Fturing%2Fmulti_universal%2Fmatch.ma;h=38dba8d729e416420c4ed6450884a7448e4689a1;hb=30f12b94fb7f9f201fb092a1b25a1c7e2f9b4564;hp=b153ef8c7c5929f318a0206d623e571b133a9f32;hpb=315610badd512e271f6e99011721a3b4d3e316fc;p=helm.git diff --git a/matita/matita/lib/turing/multi_universal/match.ma b/matita/matita/lib/turing/multi_universal/match.ma index b153ef8c7..38dba8d72 100644 --- a/matita/matita/lib/turing/multi_universal/match.ma +++ b/matita/matita/lib/turing/multi_universal/match.ma @@ -12,257 +12,564 @@ (* *) (**************************************************************************) -include "turing/turing.ma". -include "turing/inject.ma". -include "turing/while_multi.ma". +include "turing/multi_universal/compare.ma". +include "turing/multi_universal/par_test.ma". +include "turing/multi_universal/moves_2.ma". -definition compare_states ≝ initN 3. +definition Rtc_multi_true ≝ + λalpha,test,n,i.λt1,t2:Vector ? (S n). + (∃c. current alpha (nth i ? t1 (niltape ?)) = Some ? c ∧ test c = true) ∧ t2 = t1. + +definition Rtc_multi_false ≝ + λalpha,test,n,i.λt1,t2:Vector ? (S n). + (∀c. current alpha (nth i ? t1 (niltape ?)) = Some ? c → test c = false) ∧ t2 = t1. -definition comp0 : compare_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)). -definition comp1 : compare_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)). -definition comp2 : compare_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)). +lemma sem_test_char_multi : + ∀alpha,test,n,i.i ≤ n → + inject_TM ? (test_char ? test) n i ⊨ + [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ]. +#alpha #test #n #i #Hin #int +cases (acc_sem_inject … Hin (sem_test_char alpha test) int) +#k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ % +[ @Hloop +| #Hqtrue lapply (Htrue Hqtrue) * * * #c * + #Hcur #Htestc #Hnth_i #Hnth_j % + [ %{c} % // + | @(eq_vec … (niltape ?)) #i0 #Hi0 + cases (decidable_eq_nat i0 i) #Hi0i + [ >Hi0i @Hnth_i + | @sym_eq @Hnth_j @sym_not_eq // ] ] ] +| #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j % + [ @Htestc + | @(eq_vec … (niltape ?)) #i0 #Hi0 + cases (decidable_eq_nat i0 i) #Hi0i + [ >Hi0i @Hnth_i + | @sym_eq @Hnth_j @sym_not_eq // ] ] ] +qed. -(* +definition Rm_test_null_true ≝ + λalpha,n,i.λt1,t2:Vector ? (S n). + current alpha (nth i ? t1 (niltape ?)) ≠ None ? ∧ t2 = t1. + +definition Rm_test_null_false ≝ + λalpha,n,i.λt1,t2:Vector ? (S n). + current alpha (nth i ? t1 (niltape ?)) = None ? ∧ t2 = t1. -0) (x,x) → (x,x)(R,R) → 1 - (x,y≠x) → None 2 -1) (_,_) → None 1 -2) (_,_) → None 2 +lemma sem_test_null_multi : ∀alpha,n,i.i ≤ n → + inject_TM ? (test_null ?) n i ⊨ + [ tc_true : Rm_test_null_true alpha n i, Rm_test_null_false alpha n i ]. +#alpha #n #i #Hin #int +cases (acc_sem_inject … Hin (sem_test_null alpha) int) +#k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ % +[ @Hloop +| #Hqtrue lapply (Htrue Hqtrue) * * #Hcur #Hnth_i #Hnth_j % // + @(eq_vec … (niltape ?)) #i0 #Hi0 cases (decidable_eq_nat i0 i) #Hi0i + [ >Hi0i @sym_eq @Hnth_i | @sym_eq @Hnth_j @sym_not_eq // ] ] +| #Hqfalse lapply (Hfalse Hqfalse) * * #Hcur #Hnth_i #Hnth_j % + [ @Hcur + | @(eq_vec … (niltape ?)) #i0 #Hi0 cases (decidable_eq_nat i0 i) // + #Hi0i @sym_eq @Hnth_j @sym_not_eq // ] ] +qed. -*) +definition match_test ≝ λsrc,dst.λsig:DeqSet.λn.λv:Vector ? n. + match (nth src (option sig) v (None ?)) with + [ None ⇒ false + | Some x ⇒ notb (nth dst (DeqOption sig) v (None ?) == None ?) ]. -definition trans_compare_step ≝ - λi,j.λsig:FinSet.λn. - λp:compare_states × (Vector (option sig) (S n)). - let 〈q,a〉 ≝ p in - match pi1 … q with - [ O ⇒ match nth i ? a (None ?) with - [ None ⇒ 〈comp2,null_action ? n〉 - | Some ai ⇒ match nth j ? a (None ?) with - [ None ⇒ 〈comp2,null_action ? n〉 - | Some aj ⇒ if ai == aj - then 〈comp1,change_vec ? (S n) - (change_vec ? (S n) (null_action ? n) (Some ? 〈ai,R〉) i) - (Some ? 〈aj,R〉) j〉 - else 〈comp2,null_action ? n〉 ] - ] - | S q ⇒ match q with - [ O ⇒ (* 1 *) 〈comp1,null_action ? n〉 - | S _ ⇒ (* 2 *) 〈comp2,null_action ? n〉 ] ]. +definition mmove_states ≝ initN 2. -definition compare_step ≝ - λi,j,sig,n. - mk_mTM sig n compare_states (trans_compare_step i j sig n) - comp0 (λq.q == comp1 ∨ q == comp2). +definition mmove0 : mmove_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 2 (refl …)). +definition mmove1 : mmove_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 2 (refl …)). -definition R_comp_step_true ≝ - λi,j,sig,n.λint,outt: Vector (tape sig) (S n). - ∃x. - current ? (nth i ? int (niltape ?)) = Some ? x ∧ - current ? (nth j ? int (niltape ?)) = Some ? x ∧ - outt = change_vec ?? - (change_vec ?? int - (tape_move ? (nth i ? int (niltape ?)) (Some ? 〈x,R〉)) i) - (tape_move ? (nth j ? int (niltape ?)) (Some ? 〈x,R〉)) j. +definition trans_mmove ≝ + λi,sig,n,D. + λp:mmove_states × (Vector (option sig) (S n)). + let 〈q,a〉 ≝ p in match (pi1 … q) with + [ O ⇒ 〈mmove1,change_vec ? (S n) (null_action ? n) (〈None ?,D〉) i〉 + | S _ ⇒ 〈mmove1,null_action sig n〉 ]. -definition R_comp_step_false ≝ - λi,j:nat.λsig,n.λint,outt: Vector (tape sig) (S n). - (current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨ - current ? (nth i ? int (niltape ?)) = None ? ∨ - current ? (nth j ? int (niltape ?)) = None ?) ∧ outt = int. +definition mmove ≝ + λi,sig,n,D. + mk_mTM sig n mmove_states (trans_mmove i sig n D) + mmove0 (λq.q == mmove1). + +definition Rm_multi ≝ + λalpha,n,i,D.λt1,t2:Vector ? (S n). + t2 = change_vec ? (S n) t1 (tape_move alpha (nth i ? t1 (niltape ?)) D) i. + +lemma sem_move_multi : + ∀alpha,n,i,D.i ≤ n → + mmove i alpha n D ⊨ Rm_multi alpha n i D. +#alpha #n #i #D #Hin #int %{2} +%{(mk_mconfig ? mmove_states n mmove1 ?)} +[| % + [ whd in ⊢ (??%?); @eq_f whd in ⊢ (??%?); @eq_f % + | whd >tape_move_multi_def + <(change_vec_same … (ctapes …) i (niltape ?)) + >pmap_change tape_move_null_action % ] ] + qed. + +definition rewind ≝ λsrc,dst,sig,n. + parmove src dst sig n L · mmove src sig n R · mmove dst sig n R. -lemma comp_q0_q2_null : - ∀i,j,sig,n,v.i < S n → j < S n → - (nth i ? (current_chars ?? v) (None ?) = None ? ∨ - nth j ? (current_chars ?? v) (None ?) = None ?) → - step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) - = mk_mconfig ??? comp2 v. -#i #j #sig #n #v #Hi #Hj -whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?); -* #Hcurrent -[ @eq_f2 - [ whd in ⊢ (??(???%)?); >Hcurrent % - | whd in ⊢ (??(???????(???%))?); >Hcurrent @tape_move_null_action ] -| @eq_f2 - [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) // - | whd in ⊢ (??(???????(???%))?); >Hcurrent - cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ] -qed. +definition R_rewind ≝ λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n). + (∀x,x0,xs,rs. + nth src ? int (niltape ?) = midtape sig (xs@[x0]) x rs → + ∀ls0,y,y0,target,rs0.|xs| = |target| → + nth dst ? int (niltape ?) = midtape sig (target@y0::ls0) y rs0 → + outt = change_vec ?? + (change_vec ?? int (midtape sig [] x0 (reverse ? xs@x::rs)) src) + (midtape sig ls0 y0 (reverse ? target@y::rs0)) dst) ∧ + (∀x,rs.nth src ? int (niltape ?) = midtape sig [] x rs → + ∀ls0,y,rs0.nth dst ? int (niltape ?) = midtape sig ls0 y rs0 → + outt = int). -lemma comp_q0_q2_neq : - ∀i,j,sig,n,v.i < S n → j < S n → - nth i ? (current_chars ?? v) (None ?) ≠ nth j ? (current_chars ?? v) (None ?) → - step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) - = mk_mconfig ??? comp2 v. -#i #j #sig #n #v #Hi #Hj lapply (refl ? (nth i ?(current_chars ?? v)(None ?))) -cases (nth i ?? (None ?)) in ⊢ (???%→?); -[ #Hnth #_ @comp_q0_q2_null // % // -| #ai #Hai lapply (refl ? (nth j ?(current_chars ?? v)(None ?))) - cases (nth j ?? (None ?)) in ⊢ (???%→?); - [ #Hnth #_ @comp_q0_q2_null // %2 // - | #aj #Haj #Hneq - whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2 - [ whd in match (trans ????); >Hai >Haj - whd in ⊢ (??(???%)?); >(\bf ?) // @(not_to_not … Hneq) // - | whd in match (trans ????); >Hai >Haj - whd in ⊢ (??(???????(???%))?); >(\bf ?) /2 by not_to_not/ - @tape_move_null_action -] ] +theorem accRealize_to_Realize : + ∀sig,n.∀M:mTM sig n.∀Rtrue,Rfalse,acc. + M ⊨ [ acc: Rtrue, Rfalse ] → M ⊨ Rtrue ∪ Rfalse. +#sig #n #M #Rtrue #Rfalse #acc #HR #t +cases (HR t) #k * #outc * * #Hloop +#Htrue #Hfalse %{k} %{outc} % // +cases (true_or_false (cstate sig (states sig n M) n outc == acc)) #Hcase +[ % @Htrue @(\P Hcase) | %2 @Hfalse @(\Pf Hcase) ] qed. -lemma comp_q0_q1 : - ∀i,j,sig,n,v,a.i ≠ j → i < S n → j < S n → - nth i ? (current_chars ?? v) (None ?) = Some ? a → - nth j ? (current_chars ?? v) (None ?) = Some ? a → - step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) = - mk_mconfig ??? comp1 - (change_vec ? (S n) - (change_vec ?? v - (tape_move ? (nth i ? v (niltape ?)) (Some ? 〈a,R〉)) i) - (tape_move ? (nth j ? v (niltape ?)) (Some ? 〈a,R〉)) j). -#i #j #sig #n #v #a #Heq #Hi #Hj #Ha1 #Ha2 -whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2 -[ whd in match (trans ????); - >Ha1 >Ha2 whd in ⊢ (??(???%)?); >(\b ?) // -| whd in match (trans ????); - >Ha1 >Ha2 whd in ⊢ (??(???????(???%))?); >(\b ?) // - change with (change_vec ?????) in ⊢ (??(???????%)?); - <(change_vec_same … v j (niltape ?)) in ⊢ (??%?); - <(change_vec_same … v i (niltape ?)) in ⊢ (??%?); - >pmap_change >pmap_change >tape_move_null_action - @eq_f2 // @eq_f2 // >nth_change_vec_neq // -] +lemma sem_rewind : ∀src,dst,sig,n. + src ≠ dst → src < S n → dst < S n → + rewind src dst sig n ⊨ R_rewind src dst sig n. +#src #dst #sig #n #Hneq #Hsrc #Hdst +@(sem_seq_app sig n ????? (sem_parmoveL src dst sig n Hneq Hsrc Hdst) ?) +[| @(sem_seq_app sig n ????? (sem_move_multi … R ?) (sem_move_multi … R ?)) // + @le_S_S_to_le // ] +#ta #tb * #tc * * #Htc #_ * #td * whd in ⊢ (%→%→?); #Htd #Htb % +[ #x #x0 #xs #rs #Hmidta_src #ls0 #y #y0 #target #rs0 #Hlen #Hmidta_dst + >(Htc ??? Hmidta_src ls0 y (target@[y0]) rs0 ??) in Htd; + [|>Hmidta_dst // + |>length_append >length_append >Hlen % ] + >change_vec_commute [|@sym_not_eq //] + >change_vec_change_vec + >nth_change_vec_neq [|@sym_not_eq //] + >nth_change_vec // >reverse_append >reverse_single + >reverse_append >reverse_single normalize in match (tape_move ???); + >rev_append_def >append_nil #Htd >Htd in Htb; + >change_vec_change_vec >nth_change_vec // + cases ls0 [|#l1 #ls1] normalize in match (tape_move ???); // +| #x #rs #Hmidta_src #ls0 #y #rs0 #Hmidta_dst + lapply (Htc … Hmidta_src … (refl ??) Hmidta_dst) -Htc #Htc >Htc in Htd; + >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec + >nth_change_vec_neq [|@sym_not_eq //] + >nth_change_vec // lapply (refl ? ls0) cases ls0 in ⊢ (???%→%); + [ #Hls0 #Htd >Htd in Htb; + >nth_change_vec // >change_vec_change_vec + whd in match (tape_move ???);whd in match (tape_move ???); change_vec_same >change_vec_same // + | #l1 #ls1 #Hls0 #Htd >Htd in Htb; + >nth_change_vec // >change_vec_change_vec + whd in match (tape_move ???);whd in match (tape_move ???); change_vec_same >change_vec_same // +]] qed. -lemma sem_comp_step : - ∀i,j,sig,n.i ≠ j → i < S n → j < S n → - compare_step i j sig n ⊨ - [ comp1: R_comp_step_true i j sig n, - R_comp_step_false i j sig n ]. -#i #j #sig #n #Hneq #Hi #Hj #int -lapply (refl ? (current ? (nth i ? int (niltape ?)))) -cases (current ? (nth i ? int (niltape ?))) in ⊢ (???%→?); -[ #Hcuri %{2} % - [| % [ % - [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % comp_q0_q2_null /2/ %2 Ha >Hcurj % % % #H destruct (H) ] ] - | #b #Hb %{2} cases (true_or_false (a == b)) #Hab - [ % - [| % [ % - [whd in ⊢ (??%?); >(comp_q0_q1 … a Hneq Hi Hj) // - [>(\P Hab) (\P Hab) %{b} % // % // <(\P Hab) // ] - | * #H @False_ind @H % - ] ] - | % - [| % [ % - [whd in ⊢ (??%?); >comp_q0_q2_neq // - <(nth_vec_map ?? (current …) i ? int (niltape ?)) - <(nth_vec_map ?? (current …) j ? int (niltape ?)) >Ha >Hb - @(not_to_not ??? (\Pf Hab)) #H destruct (H) % - | normalize in ⊢ (%→?); #H destruct (H) ] - | #_ % // % % >Ha >Hb @(not_to_not ??? (\Pf Hab)) #H destruct (H) % ] ] +definition match_step ≝ λsrc,dst,sig,n. + compare src dst sig n · + (ifTM ?? (partest sig n (match_test src dst sig ?)) + (single_finalTM ?? + (rewind src dst sig n · (inject_TM ? (move_r ?) n dst))) + (nop …) + partest1). + +(* we assume the src is a midtape + we stop + if the dst is out of bounds (outt = int) + or dst.right is shorter than src.right (outt.current → None) + or src.right is a prefix of dst.right (out = just right of the common prefix) *) +definition R_match_step_false ≝ + λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n). + ∀ls,x,xs. + nth src ? int (niltape ?) = midtape sig ls x xs → + ((current sig (nth dst (tape sig) int (niltape sig)) = None ?) ∧ outt = int) ∨ + (∃ls0,rs0,xs0. nth dst ? int (niltape ?) = midtape sig ls0 x rs0 ∧ + xs = rs0@xs0 ∧ + current sig (nth dst (tape sig) outt (niltape sig)) = None ?) ∨ + (∃ls0,rs0. + nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧ + (* ∀rsj,c. + rs0 = c::rsj → *) + outt = change_vec ?? + (change_vec ?? int (mk_tape sig (reverse ? xs@x::ls) (None ?) [ ]) src) + (mk_tape sig (reverse ? xs@x::ls0) (option_hd ? rs0) (tail ? rs0)) dst). + +(* + we assume the src is a midtape [ ] s rs + if we iterate + then dst.current = Some ? s1 + and if s ≠ s1 then outt = int.dst.move_right() + and if s = s1 + then int.src.right and int.dst.right have a common prefix + and the heads of their suffixes are different + and outt = int.dst.move_right(). + + *) +definition R_match_step_true ≝ + λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n). + ∀s,rs.nth src ? int (niltape ?) = midtape ? [ ] s rs → + outt = change_vec ?? int + (tape_move_mono … (nth dst ? int (niltape ?)) (〈None ?,R〉)) dst ∧ + (current sig (nth dst (tape sig) int (niltape sig)) = Some ? s → + ∃xs,ci,rs',ls0,cj,rs0. + rs = xs@ci::rs' ∧ + nth dst ? int (niltape ?) = midtape sig ls0 s (xs@cj::rs0) ∧ + ci ≠ cj). + +axiom daemon : ∀X:Prop.X. + +lemma sem_match_step : + ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → + match_step src dst sig n ⊨ + [ inr ?? (inr ?? (inl … (inr ?? start_nop))) : + R_match_step_true src dst sig n, + R_match_step_false src dst sig n ]. +#src #dst #sig #n #Hneq #Hsrc #Hdst +@(acc_sem_seq_app sig n … (sem_compare src dst sig n Hneq Hsrc Hdst) + (acc_sem_if ? n … (sem_partest sig n (match_test src dst sig ?)) + (sem_seq … + (sem_rewind ???? Hneq Hsrc Hdst) + (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? ))) + (sem_nop …))) +[ #ta #tb #tc * lapply (refl ? (current ? (nth src ? ta (niltape ?)))) + cases (current ? (nth src ? ta (niltape ?))) in ⊢ (???%→%); + [ #Hcurta_src #Hcomp #_ * #td * >Hcomp [| % %2 %] + whd in ⊢ (%→?); * whd in ⊢ (??%?→?); + >(?:nth src ? (current_chars ?? ta) (None ?) = None ?) + [ normalize in ⊢ (%→?); #H destruct (H) + | @daemon ] + | #s #Hs lapply (refl ? (current ? (nth dst ? ta (niltape ?)))) + cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→%); + [ #Hcurta_dst #Hcomp #_ * #td * >Hcomp [| %2 %] + whd in ⊢ (%→?); * whd in ⊢ (??%?→?); + >(?:nth src ? (current_chars ?? ta) (None ?) = Some ? s) [|@daemon] + >(?:nth dst ? (current_chars ?? ta) (None ?) = None ?) [|@daemon] + normalize in ⊢ (%→?); #H destruct (H) + | #s0 #Hs0 + cases (current_to_midtape … Hs) #ls * #rs #Hmidta_src >Hmidta_src + cases (current_to_midtape … Hs0) #ls0 * #rs0 #Hmidta_dst >Hmidta_dst + cases (true_or_false (s == s0)) #Hss0 + [ lapply (\P Hss0) -Hss0 #Hss0 destruct (Hss0) + #_ #Hcomp cases (Hcomp ????? (refl ??) (refl ??)) -Hcomp [ * + [ * #rs' * #_ #Hcurtc_dst * #td * whd in ⊢ (%→?); * whd in ⊢ (??%?→?); + >(?:nth dst ? (current_chars ?? tc) (None ?) = None ?) [|@daemon] + cases (nth src ? (current_chars ?? tc) (None ?)) + [| #x ] normalize in ⊢ (%→?); #H destruct (H) + | * #rs0' * #_ #Hcurtc_src * #td * whd in ⊢ (%→?); * whd in ⊢ (??%?→?); + >(?:nth src ? (current_chars ?? tc) (None ?) = None ?) [|@daemon] + normalize in ⊢ (%→?); #H destruct (H) ] + | * #xs * #ci * #cj * #rs'' * #rs0' * * * #Hcicj #Hrs #Hrs0 + #Htc * #td * * #Hmatch #Htd destruct (Htd) * #te * * + >Htc >change_vec_commute // >nth_change_vec // + >change_vec_commute [|@sym_not_eq //] >nth_change_vec // #Hte #_ #Htb + #s' #rs' >Hmidta_src #H destruct (H) + lapply (Hte … (refl ??) … (refl ??) (refl ??)) -Hte + >change_vec_commute // >change_vec_change_vec + >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec #Hte + >Hte in Htb; * * #_ >nth_change_vec // #Htb1 + lapply (Htb1 … (refl ??)) -Htb1 #Htb1 #Htb2 % + [ @(eq_vec … (niltape ?)) #i #Hi + cases (true_or_false ((dst : DeqNat) == i)) #Hdsti + [ <(\P Hdsti) >Htb1 >nth_change_vec // >Hmidta_dst + >Hrs0 >reverse_reverse cases xs [|#r1 #rs1] % + | nth_change_vec_neq [| @(\Pf Hdsti)] + >Hrs0 >reverse_reverse >nth_change_vec_neq in ⊢ (???%); + change_vec_same % ] + | #_ >Hmidta_dst >Hrs0 + %{xs} %{ci} %{rs''} %{ls0} %{cj} %{rs0'} % // % // + ] + ] + | lapply (\Pf Hss0) -Hss0 #Hss0 #Htc cut (tc = ta) + [@Htc % % @(not_to_not ??? Hss0) #H destruct (H) %] + -Htc #Htc destruct (Htc) #_ * #td * whd in ⊢ (%→?); * #_ + #Htd destruct (Htd) * #te * * #_ #Hte * * #_ #Htb1 #Htb2 + #s1 #rs1 >Hmidta_src #H destruct (H) + lapply (Hte … Hmidta_src … Hmidta_dst) -Hte #Hte destruct (Hte) % + [ @(eq_vec … (niltape ?)) #i #Hi + cases (true_or_false ((dst : DeqNat) == i)) #Hdsti + [ <(\P Hdsti) >(Htb1 … Hmidta_dst) >nth_change_vec // >Hmidta_dst + cases rs0 [|#r2 #rs2] % + | nth_change_vec_neq [| @(\Pf Hdsti)] % ] + | >Hs0 #H destruct (H) @False_ind cases (Hss0) /2/ ] + ] ] ] -] +| #ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * #Htest #Htd destruct (Htd) + whd in ⊢ (%→?); #Htb destruct (Htb) #ls #x #xs #Hta_src + lapply (refl ? (current ? (nth dst ? ta (niltape ?)))) + cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?); + [ #Hcurta_dst % % % // @Hcomp1 %2 // + | #x0 #Hcurta_dst cases (current_to_midtape … Hcurta_dst) -Hcurta_dst + #ls0 * #rs0 #Hta_dst cases (true_or_false (x == x0)) #Hxx0 + [ lapply (\P Hxx0) -Hxx0 #Hxx0 destruct (Hxx0) + | >(?:tc=ta) in Htest; + [|@Hcomp1 % % >Hta_src >Hta_dst @(not_to_not ??? (\Pf Hxx0)) normalize + #Hxx0' destruct (Hxx0') % ] + whd in ⊢ (??%?→?); >(?:nth src ? (current_chars ?? ta) (None ?) = Some ? x) + [| @daemon ] + >(?:nth dst ? (current_chars ?? ta) (None ?) = Some ? x0) [|@daemon] + whd in ⊢ (??%?→?); #Hfalse destruct (Hfalse) ] -Hcomp1 + cases (Hcomp2 … Hta_src Hta_dst) [ * + [ * #rs' * #Hxs #Hcurtc % %2 %{ls0} %{rs0} %{rs'} % // % // + | * #rs0' * #Hxs #Htc %2 >Htc %{ls0} %{rs0'} % // ] + | * #xs0 * #ci * #cj * #rs' * #rs0' * * * + #Hci #Hxs #Hrs0 #Htc @False_ind + whd in Htest:(??%?); + >(?:nth src ? (current_chars ?? tc) (None ?) = Some ? ci) in Htest; [|@daemon] + >(?:nth dst ? (current_chars ?? tc) (None ?) = Some ? cj) [|@daemon] + normalize #H destruct (H) ] ] ] qed. -definition compare ≝ λi,j,sig,n. - whileTM … (compare_step i j sig n) comp1. +definition match_m ≝ λsrc,dst,sig,n. + whileTM … (match_step src dst sig n) + (inr ?? (inr ?? (inl … (inr ?? start_nop)))). -definition R_compare ≝ - λi,j,sig,n.λint,outt: Vector (tape sig) (S n). - (current sig (nth i (tape sig) int (niltape sig)) - ≠current sig (nth j (tape sig) int (niltape sig)) → - outt = int) ∧ - (∀ls,x,xs,ci,rs,ls0,cj,rs0. - nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) → - nth j ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj → +definition R_match_m ≝ + λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n). + ∀x,rs. + nth src ? int (niltape ?) = midtape sig [ ] x rs → + (current sig (nth dst (tape sig) int (niltape sig)) = None ? → outt = int) ∧ + (∀ls0,x0,rs0. + nth dst ? int (niltape ?) = midtape sig ls0 x0 rs0 → + (∃l,l1.x0::rs0 = l@x::rs@l1 ∧ outt = change_vec ?? - (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i) - (midtape sig (reverse ? xs@x::ls0) cj rs0) j). + (change_vec ?? int + (mk_tape sig (reverse ? rs@[x]) (None ?) [ ]) src) + (mk_tape sig ((reverse ? (l@x::rs))@ls0) (option_hd ? l1) (tail ? l1)) dst) ∨ + ∀l,l1.x0::rs0 ≠ l@x::rs@l1). -lemma wsem_compare : ∀i,j,sig,n.i ≠ j → i < S n → j < S n → - compare i j sig n ⊫ R_compare i j sig n. -#i #j #sig #n #Hneq #Hi #Hj #ta #k #outc #Hloop -lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) // +lemma not_sub_list_merge : + ∀T.∀a,b:list T. (∀l1.a ≠ b@l1) → (∀t,l,l1.a ≠ t::l@b@l1) → ∀l,l1.a ≠ l@b@l1. +#T #a #b #H1 #H2 #l elim l normalize // +qed. + +lemma not_sub_list_merge_2 : + ∀T:DeqSet.∀a,b:list T.∀t. (∀l1.t::a ≠ b@l1) → (∀l,l1.a ≠ l@b@l1) → ∀l,l1.t::a ≠ l@b@l1. +#T #a #b #t #H1 #H2 #l elim l // +#t0 #l1 #IH #l2 cases (true_or_false (t == t0)) #Htt0 +[ >(\P Htt0) % normalize #H destruct (H) cases (H2 l1 l2) /2/ +| normalize % #H destruct (H) cases (\Pf Htt0) /2/ ] +qed. + + +lemma wsem_match_m : ∀src,dst,sig,n. +src ≠ dst → src < S n → dst < S n → + match_m src dst sig n ⊫ R_match_m src dst sig n. +#src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop +lapply (sem_while … (sem_match_step src dst sig n Hneq Hsrc Hdst) … Hloop) // -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar -[ #tc whd in ⊢ (%→?); * * [ * - [ #Hcicj #Houtc % - [ #_ @Houtc - | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi #Hnthj - >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H % +[ #Hfalse #x #xs #Hmid_src + cases (Hfalse … Hmid_src) -Hfalse + [(* current dest = None *) * + [ * #Hcur_dst #Houtc % + [#_ >Houtc // + | #ls0 #x0 #rs0 #Hmid_dst >Hmid_dst in Hcur_dst; + normalize in ⊢ (%→?); #H destruct (H) + ] + | * #ls0 * #rs0 * #xs0 * * #Htc_dst #Hrs0 #HNone % + [ >Htc_dst normalize in ⊢ (%→?); #H destruct (H) + | #ls1 #x1 #rs1 >Htc_dst #H destruct (H) + >Hrs0 cases xs0 + [ % %{[ ]} %{[ ]} % [ >append_nil >append_nil %] + (* change false case + #cj #ls2 #H destruct (H) *) @daemon + | #x2 #xs2 %2 #l #l1 % #Habs lapply (eq_f ?? (length ?) ?? Habs) + >length_append whd in ⊢ (??%(??%)→?); >length_append + >length_append normalize >commutative_plus whd in ⊢ (???%→?); + #H destruct (H) lapply e0 >(plus_n_O (|rs1|)) in ⊢ (??%?→?); + >associative_plus >associative_plus + #e1 lapply (injective_plus_r ??? e1) whd in ⊢ (???%→?); + #e2 destruct (e2) + ] + ] + ] + |* #ls0 * #rs0 * #Hmid_dst #Houtc % + [ >Hmid_dst normalize in ⊢ (%→?); #H destruct (H) + |#ls1 #x1 #rs1 >Hmid_dst #H destruct (H) + %1 %{[ ]} %{rs0} % [%] + >reverse_cons >associative_append >Houtc % ] - | #Hci #Houtc % - [ #_ @Houtc - | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi >Hnthi in Hci; - normalize in ⊢ (%→?); #H destruct (H) ] ] - | #Hcj #Houtc % - [ #_ @Houtc - | #ls #x #xs #ci #rs #ls0 #cj #rs0 #_ #Hnthj >Hnthj in Hcj; - normalize in ⊢ (%→?); #H destruct (H) ] ] - | #tc #td #te * #x * * #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH * - #IH1 #IH2 % - [ >Hci >Hcj * #H @False_ind @H % - | #ls #c0 #xs #ci #rs #ls0 #cj #rs0 cases xs - [ #Hnthi #Hnthj #Hcicj >IH1 - [ >Hd @eq_f3 // - [ @eq_f3 // >(?:c0=x) [ >Hnthi % ] - >Hnthi in Hci;normalize #H destruct (H) % - | >(?:c0=x) [ >Hnthj % ] - >Hnthi in Hci;normalize #H destruct (H) % ] - | >Hd >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //] - >nth_change_vec // >Hnthi >Hnthj normalize @(not_to_not ??? Hcicj) - #H destruct (H) % ] - | #x0 #xs0 #Hnthi #Hnthj #Hcicj - >(IH2 (c0::ls) x0 xs0 ci rs (c0::ls0) cj rs0 … Hcicj) - [ >Hd >change_vec_commute in ⊢ (??%?); // - >change_vec_change_vec >change_vec_commute in ⊢ (??%?); // - @sym_not_eq // - | >Hd >nth_change_vec // >Hnthj normalize - >Hnthi in Hci;normalize #H destruct (H) % - | >Hd >nth_change_vec_neq [|@sym_not_eq //] >Hnthi - >nth_change_vec // normalize - >Hnthi in Hci;normalize #H destruct (H) % + ] +|-ta #ta #tc #Htrue #Hstar #IH #Hout lapply (IH Hout) -IH -Hout #IH whd + #x #xs #Hmidta_src + lapply (refl ? (current ? (nth dst ? ta (niltape ?)))) + cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?); + [#Hcurta_dst % + [#_ whd in Htrue; >Hmidta_src in Htrue; #Htrue + cases (Htrue ?? (refl ??)) -Htrue >Hcurta_dst + (* dovremmo sapere che ta.dst è sul margine destro, da cui la move non + ha effetto *) #_ cut (tc = ta) [@daemon] #Htc destruct (Htc) #_ + cases (IH … Hmidta_src) #Houtc #_ @Houtc // + |#ls0 #x0 #rs0 #Hmidta_dst >Hmidta_dst in Hcurta_dst; + normalize in ⊢ (%→?); #H destruct (H) + ] + | #c #Hcurta_dst % [ >Hcurta_dst #H destruct (H) ] + #ls0 #x0 #rs0 #Hmidta_dst >Hmidta_dst in Hcurta_dst; normalize in ⊢ (%→?); + #H destruct (H) whd in Htrue; >Hmidta_src in Htrue; #Htrue + cases (Htrue ?? (refl …)) -Htrue >Hmidta_dst #Htc + cases (true_or_false (x==c)) #eqx + [ lapply (\P eqx) -eqx #eqx destruct (eqx) + #Htrue cases (Htrue (refl ??)) -Htrue + #xs0 * #ci * #rs' * #ls1 * #cj * #rs1 * * #Hxs #H destruct (H) #Hcicj + >Htc in IH; whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //] + #IH cases (IH … Hmidta_src) -IH #_ >nth_change_vec // + cut (∃x1,xs1.xs0@cj::rs1 = x1::xs1) + [ cases xs0 [ %{cj} %{rs1} % | #x1 #xs1 %{x1} %{(xs1@cj::rs1)} % ] ] * #x1 * #xs1 + #Hxs1 >Hxs1 #IH cases (IH … (refl ??)) -IH + [ * #l * #l1 * #Hxs1' + >change_vec_commute // >change_vec_change_vec + #Houtc % %{(c::l)} %{l1} % + [ normalize reverse_cons >associative_append >change_vec_commute // @Houtc ] + | #H %2 #l #l1 >(?:l@c::xs@l1 = l@(c::xs)@l1) [|%] + @not_sub_list_merge + [ #l2 >Hxs associative_append #H2 lapply (append_l2_injective ????? (refl ??) H2) + #H3 lapply (cons_injective_l ????? H3) #H3 >H3 in Hcicj; * /2/ + |#t #l2 #l3 % normalize #H1 lapply (cons_injective_r ????? H1) + -H1 #H1 cases (H l2 l3) #H2 @H2 @H1 ] -]]] -qed. - -lemma terminate_compare : ∀i,j,sig,n,t. - i ≠ j → i < S n → j < S n → - compare i j sig n ↓ t. -#i #j #sig #n #t #Hneq #Hi #Hj -@(terminate_while … (sem_comp_step …)) // -<(change_vec_same … t i (niltape ?)) -cases (nth i (tape sig) t (niltape ?)) -[ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct -|2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct -| #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs - [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); - #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % - #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //] - >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H) - |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec // - normalize in ⊢ (%→?); #H destruct (H) #Hcur - >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH + ] + | (* in match_step_true manca il caso di fallimento immediato + (con i due current diversi) *) + @daemon + (* + #_ lapply (\Pf eqx) -eqx #eqx >Hmidta_dst + cases (Htrue ? (refl ??) eqx) -Htrue #Htb #Hendcx #_ + cases rs0 in Htb; + [ #_ %2 #l #l1 cases l + [ normalize cases xs + [ cases l1 + [ normalize % #H destruct (H) cases eqx /2/ + | #tmp1 #l2 normalize % #H destruct (H) ] + | #tmp1 #l2 normalize % #H destruct (H) ] + | #tmp1 #l2 normalize % #H destruct (H)cases l2 in e0; + [ normalize #H1 destruct (H1) + | #tmp2 #l3 normalize #H1 destruct (H1) ] + ] + | #r1 #rs1 normalize in ⊢ (???(????%?)→?); #Htb >Htb in IH; #IH + cases (IH ls x xs end rs ? Hnotend Hend Hnotstart) + [| >Htb >nth_change_vec_neq [|@sym_not_eq //] @Hmid_src ] -IH + #_ #IH cases (IH Hstart (c::ls0) r1 rs1 ?) + [|| >nth_change_vec // ] -IH + [ * #l * #l1 * #Hll1 #Hout % %{(c::l)} %{l1} % >Hll1 // + >reverse_cons >associative_append #cj0 #ls #Hl1 >(Hout ?? Hl1) + >change_vec_commute in ⊢ (??(???%??)?); // @sym_not_eq // + | #IH %2 @(not_sub_list_merge_2 ?? (x::xs)) normalize [|@IH] + #l1 % #H destruct (H) cases eqx /2/ + ] *) + ] ] ] qed. -lemma sem_compare : ∀i,j,sig,n. - i ≠ j → i < S n → j < S n → - compare i j sig n ⊨ R_compare i j sig n. -#i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize /2/ -qed. - +definition Pre_match_m ≝ + λsrc,sig,n,is_startc,is_endc.λt: Vector (tape sig) (S n). + ∃start,xs,end. + nth src (tape sig) t (niltape sig) = midtape ? [] start (xs@[end]) ∧ + is_startc start = true ∧ + (∀c.c ∈ (xs@[end]) = true → is_startc c = false) ∧ + (∀c.c ∈ (start::xs) = true → is_endc c = false) ∧ + is_endc end = true. + +lemma terminate_match_m : + ∀src,dst,sig,n,is_startc,is_endc,t. + src ≠ dst → src < S n → dst < S n → + Pre_match_m src sig n is_startc is_endc t → + match_m src dst sig n is_startc is_endc ↓ t. +#src #dst #sig #n #is_startc #is_endc #t #Hneq #Hsrc #Hdst * #start * #xs * #end +* * * * #Hmid_src #Hstart #Hnotstart #Hnotend #Hend +@(terminate_while … (sem_match_step src dst sig n is_startc is_endc Hneq Hsrc Hdst)) // +<(change_vec_same … t dst (niltape ?)) +lapply (refl ? (nth dst (tape sig) t (niltape ?))) +cases (nth dst (tape sig) t (niltape ?)) in ⊢ (???%→?); +[ #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //] + >Hmid_src #HR cases (HR ? (refl ??)) -HR + >nth_change_vec // >Htape_dst normalize in ⊢ (%→?); + * #H @False_ind @H % +| #x0 #xs0 #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //] + >Hmid_src #HR cases (HR ? (refl ??)) -HR + >nth_change_vec // >Htape_dst normalize in ⊢ (%→?); + * #H @False_ind @H % +| #x0 #xs0 #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //] + >Hmid_src #HR cases (HR ? (refl ??)) -HR + >nth_change_vec // >Htape_dst normalize in ⊢ (%→?); + * #H @False_ind @H % +| #ls #s #rs lapply s -s lapply ls -ls lapply Hmid_src lapply t -t elim rs + [#t #Hmid_src #ls #s #Hmid_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //] + >Hmid_src >nth_change_vec // >Hmid_dst #HR cases (HR ? (refl ??)) -HR #_ + #HR cases (HR Hstart Hnotstart) + cases (true_or_false (start == s)) #Hs + [ lapply (\P Hs) -Hs #Hs Hxs in Htrue; #Htrue + cases (Htrue [ ] start [ ] ? xs1 ? [ ] (refl ??) (refl ??) ?) + [ * #_ * #H @False_ind @H % ] + #c0 #Hc0 @Hnotend >(memb_single … Hc0) @memb_hd + | lapply (\Pf Hs) -Hs #Hs #Htrue #_ + cases (Htrue ? (refl ??) Hs) -Htrue #Ht1 #_ % + #t2 whd in ⊢ (%→?); #HR cases (HR start ?) + [ >Ht1 >nth_change_vec // normalize in ⊢ (%→?); * #H @False_ind @H % + | >Ht1 >nth_change_vec_neq [|@sym_not_eq //] + >nth_change_vec_neq [|@sym_not_eq //] >Hmid_src % ] + ] + |#r0 #rs0 #IH #t #Hmid_src #ls #s #Hmid_dst % #t1 whd in ⊢ (%→?); + >nth_change_vec_neq [|@sym_not_eq //] >Hmid_src + #Htrue cases (Htrue ? (refl ??)) -Htrue #_ #Htrue + <(change_vec_same … t1 dst (niltape ?)) + cases (Htrue Hstart Hnotstart) -Htrue + cases (true_or_false (start == s)) #Hs + [ lapply (\P Hs) -Hs #Hs Hrs + cut (∃y,ys. xs1 = y::ys) + [ lapply Hxs0notend lapply Hxs lapply xs0 elim xs + [ * + [ normalize #Hxs1 Hend #H1 destruct (H1) + ] + | #y #ys #IH0 * + [ normalize in ⊢ (%→?); #Hxs1 Hxs1 in Hxs; #Hxs >Hmid_src >Hmid_dst >Hxs >Hrs + %{ls} %{xs0} %{y} %{ys} %{xs2} + % [ % // | @Hcomp // ] ] + * #ls0 * #xs0 * #ci * #rs * #rs0 * * #Hmid_src' #Hmid_dst' #Hcomp + nth_change_vec // >Hs #Htrue destruct (Hs) + lapply (Htrue ??????? Hmid_src' Hmid_dst' ?) -Htrue + [ #c0 #Hc0 @Hnotend cases (orb_true_l … Hc0) -Hc0 #Hc0 + [ whd in ⊢ (??%?); >Hc0 % + | @memb_cons >Hmid_src in Hmid_src'; #Hmid_src' destruct (Hmid_src') + lapply e0 -e0 @(list_elim_left … rs) + [ #e0 destruct (e0) lapply (append_l1_injective_r ?????? e0) // + | #x1 #xs1 #_ >append_cons in ⊢ (???%→?); + e1 @memb_append_l1 @memb_append_l1 // ] ] + | * * #Hciendc cases rs0 in Hcomp; + [ #_ * #H @False_ind @H % + | #r1 #rs1 * [ >Hciendc #H destruct (H) ] + * #_ #Hcomp lapply (Hcomp ?? (refl ??)) -Hcomp #Hcomp #_ #Htrue + cases (Htrue ?? (refl ??) Hcomp) #Ht1 #_ >Ht1 @(IH ?? (s::ls) r0) + [ >nth_change_vec_neq [|@sym_not_eq //] + >nth_change_vec_neq [|@sym_not_eq //] @Hmid_src + | >nth_change_vec // >Hmid_dst % ] ] ] + | >Hmid_dst >nth_change_vec // lapply (\Pf Hs) -Hs #Hs #Htrue #_ + cases (Htrue ? (refl ??) Hs) #Ht1 #_ >Ht1 @(IH ?? (s::ls) r0) + [ >nth_change_vec_neq [|@sym_not_eq //] + >nth_change_vec_neq [|@sym_not_eq //] @Hmid_src + | >nth_change_vec // ] ] ] ] +qed. \ No newline at end of file