1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
16 include "basics/lists/listb.ma".
18 let rec move (S: DeqSet) (x:S) (E: pitem S) on E : pre S ≝
22 | ps y ⇒ 〈 `y, false 〉
23 | pp y ⇒ 〈 `y, x == y 〉
24 | po e1 e2 ⇒ (move ? x e1) ⊕ (move ? x e2)
25 | pc e1 e2 ⇒ (move ? x e1) ⊙ (move ? x e2)
26 | pk e ⇒ (move ? x e)^⊛ ].
28 lemma move_plus: ∀S:DeqSet.∀x:S.∀i1,i2:pitem S.
29 move S x (i1 + i2) = (move ? x i1) ⊕ (move ? x i2).
32 lemma move_cat: ∀S:DeqSet.∀x:S.∀i1,i2:pitem S.
33 move S x (i1 · i2) = (move ? x i1) ⊙ (move ? x i2).
36 lemma move_star: ∀S:DeqSet.∀x:S.∀i:pitem S.
37 move S x i^* = (move ? x i)^⊛.
40 definition pmove ≝ λS:DeqSet.λx:S.λe:pre S. move ? x (\fst e).
42 lemma pmove_def : ∀S:DeqSet.∀x:S.∀i:pitem S.∀b.
43 pmove ? x 〈i,b〉 = move ? x i.
46 lemma eq_to_eq_hd: ∀A.∀l1,l2:list A.∀a,b.
47 a::l1 = b::l2 → a = b.
48 #A #l1 #l2 #a #b #H destruct //
51 lemma same_kernel: ∀S:DeqSet.∀a:S.∀i:pitem S.
52 |\fst (move ? a i)| = |i|.
54 [#i1 #i2 >move_cat #H1 #H2 whd in ⊢ (???%); <H1 <H2 //
55 |#i1 #i2 >move_plus #H1 #H2 whd in ⊢ (???%); <H1 <H2 //
60 ∀S:DeqSet.∀a:S.∀i:pitem S.∀w: word S.
61 \sem{move ? a i} w ↔ \sem{i} (a::w).
66 |normalize #x #w cases (true_or_false (a==x)) #H >H normalize
67 [>(\P H) % [* // #bot @False_ind //| #H1 destruct /2/]
68 |% [@False_ind |#H1 cases (\Pf H) #H2 @H2 destruct //]
70 |#i1 #i2 #HI1 #HI2 #w >move_cat
71 @iff_trans[|@sem_odot] >same_kernel >sem_cat_w
72 @iff_trans[||@(iff_or_l … (HI2 w))] @iff_or_r
73 @iff_trans[||@iff_sym @deriv_middot //]
75 |#i1 #i2 #HI1 #HI2 #w >(sem_plus S i1 i2) >move_plus >sem_plus_w
76 @iff_trans[|@sem_oplus]
77 @iff_trans[|@iff_or_l [|@HI2]| @iff_or_r //]
78 |#i1 #HI1 #w >move_star
79 @iff_trans[|@sem_ostar] >same_kernel >sem_star_w
80 @iff_trans[||@iff_sym @deriv_middot //]
85 notation > "x ↦* E" non associative with precedence 60 for @{moves ? $x $E}.
86 let rec moves (S : DeqSet) w e on w : pre S ≝
89 | cons x w' ⇒ w' ↦* (move S x (\fst e))].
91 lemma moves_empty: ∀S:DeqSet.∀e:pre S.
95 lemma moves_cons: ∀S:DeqSet.∀a:S.∀w.∀e:pre S.
96 moves ? (a::w) e = moves ? w (move S a (\fst e)).
99 lemma moves_left : ∀S,a,w,e.
100 moves S (w@[a]) e = move S a (\fst (moves S w e)).
101 #S #a #w elim w // #x #tl #Hind #e >moves_cons >moves_cons //
104 lemma not_epsilon_sem: ∀S:DeqSet.∀a:S.∀w: word S. ∀e:pre S.
105 iff ((a::w) ∈ e) ((a::w) ∈ \fst e).
106 #S #a #w * #i #b cases b normalize
107 [% /2/ * // #H destruct |% normalize /2/]
110 lemma same_kernel_moves: ∀S:DeqSet.∀w.∀e:pre S.
111 |\fst (moves ? w e)| = |\fst e|.
115 theorem decidable_sem: ∀S:DeqSet.∀w: word S. ∀e:pre S.
116 (\snd (moves ? w e) = true) ↔ \sem{e} w.
118 [* #i #b >moves_empty cases b % /2/
119 |#a #w1 #Hind #e >moves_cons
120 @iff_trans [||@iff_sym @not_epsilon_sem]
121 @iff_trans [||@move_ok] @Hind
125 (* lemma not_true_to_false: ∀b.b≠true → b =false.
126 #b * cases b // #H @False_ind /2/
129 (************************ pit state ***************************)
130 definition pit_pre ≝ λS.λi.〈blank S (|i|), false〉.
132 let rec occur (S: DeqSet) (i: re S) on i ≝
137 | o e1 e2 ⇒ unique_append ? (occur S e1) (occur S e2)
138 | c e1 e2 ⇒ unique_append ? (occur S e1) (occur S e2)
141 lemma not_occur_to_pit: ∀S,a.∀i:pitem S. memb S a (occur S (|i|)) ≠ true →
142 move S a i = pit_pre S i.
144 [#x normalize cases (a==x) normalize // #H @False_ind /2/
145 |#i1 #i2 #Hind1 #Hind2 #H >move_cat
146 >Hind1 [2:@(not_to_not … H) #H1 @sublist_unique_append_l1 //]
147 >Hind2 [2:@(not_to_not … H) #H1 @sublist_unique_append_l2 //] //
148 |#i1 #i2 #Hind1 #Hind2 #H >move_plus
149 >Hind1 [2:@(not_to_not … H) #H1 @sublist_unique_append_l1 //]
150 >Hind2 [2:@(not_to_not … H) #H1 @sublist_unique_append_l2 //] //
151 |#i #Hind #H >move_star >Hind //
155 lemma move_pit: ∀S,a,i. move S a (\fst (pit_pre S i)) = pit_pre S i.
157 [#i1 #i2 #Hind1 #Hind2 >move_cat >Hind1 >Hind2 //
158 |#i1 #i2 #Hind1 #Hind2 >move_plus >Hind1 >Hind2 //
159 |#i #Hind >move_star >Hind //
163 lemma moves_pit: ∀S,w,i. moves S w (pit_pre S i) = pit_pre S i.
164 #S #w #i elim w // #a #tl >moves_cons //
167 lemma to_pit: ∀S,w,e. ¬ sublist S w (occur S (|\fst e|)) →
168 moves S w e = pit_pre S (\fst e).
170 [#e * #H @False_ind @H normalize #a #abs @False_ind /2/
171 |#a #tl #Hind #e #H cases (true_or_false (memb S a (occur S (|\fst e|))))
172 [#Htrue >moves_cons whd in ⊢ (???%); <(same_kernel … a)
173 @Hind >same_kernel @(not_to_not … H) #H1 #b #memb cases (orb_true_l … memb)
174 [#H2 >(\P H2) // |#H2 @H1 //]
175 |#Hfalse >moves_cons >not_occur_to_pit // >Hfalse /2/
181 definition cofinal ≝ λS.λp:(pre S)×(pre S).
182 \snd (\fst p) = \snd (\snd p).
184 theorem equiv_sem: ∀S:DeqSet.∀e1,e2:pre S.
185 \sem{e1} =1 \sem{e2} ↔ ∀w.cofinal ? 〈moves ? w e1,moves ? w e2〉.
188 cut (∀b1,b2. iff (b1 = true) (b2 = true) → (b1 = b2))
189 [* * // * #H1 #H2 [@sym_eq @H1 //| @H2 //]]
190 #Hcut @Hcut @iff_trans [|@decidable_sem]
191 @iff_trans [|@same_sem] @iff_sym @decidable_sem
192 |#H #w1 @iff_trans [||@decidable_sem] <H @iff_sym @decidable_sem]
195 definition occ ≝ λS.λe1,e2:pre S.
196 unique_append ? (occur S (|\fst e1|)) (occur S (|\fst e2|)).
198 lemma occ_enough: ∀S.∀e1,e2:pre S.
199 (∀w.(sublist S w (occ S e1 e2))→ cofinal ? 〈moves ? w e1,moves ? w e2〉)
200 →∀w.cofinal ? 〈moves ? w e1,moves ? w e2〉.
202 cases (decidable_sublist S w (occ S e1 e2)) [@H] -H #H
203 >to_pit [2: @(not_to_not … H) #H1 #a #memba @sublist_unique_append_l1 @H1 //]
204 >to_pit [2: @(not_to_not … H) #H1 #a #memba @sublist_unique_append_l2 @H1 //]
208 lemma equiv_sem_occ: ∀S.∀e1,e2:pre S.
209 (∀w.(sublist S w (occ S e1 e2))→ cofinal ? 〈moves ? w e1,moves ? w e2〉)
210 → \sem{e1}=1\sem{e2}.
211 #S #e1 #e2 #H @(proj2 … (equiv_sem …)) @occ_enough #w @H
214 definition sons ≝ λS:DeqSet.λl:list S.λp:(pre S)×(pre S).
215 map ?? (λa.〈move S a (\fst (\fst p)),move S a (\fst (\snd p))〉) l.
217 lemma memb_sons: ∀S,l.∀p,q:(pre S)×(pre S). memb ? p (sons ? l q) = true →
218 ∃a.(move ? a (\fst (\fst q)) = \fst p ∧
219 move ? a (\fst (\snd q)) = \snd p).
220 #S #l elim l [#p #q normalize in ⊢ (%→?); #abs @False_ind /2/]
221 #a #tl #Hind #p #q #H cases (orb_true_l … H) -H
222 [#H @(ex_intro … a) >(\P H) /2/ |#H @Hind @H]
225 definition is_bisim ≝ λS:DeqSet.λl:list ?.λalpha:list S.
226 ∀p:(pre S)×(pre S). memb ? p l = true → cofinal ? p ∧ (sublist ? (sons ? alpha p) l).
228 lemma bisim_to_sem: ∀S:DeqSet.∀l:list ?.∀e1,e2: pre S.
229 is_bisim S l (occ S e1 e2) → memb ? 〈e1,e2〉 l = true → \sem{e1}=1\sem{e2}.
230 #S #l #e1 #e2 #Hbisim #Hmemb @equiv_sem_occ
231 #w #Hsub @(proj1 … (Hbisim 〈moves S w e1,moves S w e2〉 ?))
232 lapply Hsub @(list_elim_left … w) [//]
233 #a #w1 #Hind #Hsub >moves_left >moves_left @(proj2 …(Hbisim …(Hind ?)))
234 [#x #Hx @Hsub @memb_append_l1 //
235 |cut (memb S a (occ S e1 e2) = true) [@Hsub @memb_append_l2 //] #occa
241 let rec bisim S l n (frontier,visited: list ?) on n ≝
243 [ O ⇒ 〈false,visited〉 (* assert false *)
246 [ nil ⇒ 〈true,visited〉
248 if beqb (\snd (\fst hd)) (\snd (\snd hd)) then
249 bisim S l m (unique_append ? (filter ? (λx.notb (memb ? x (hd::visited)))
250 (sons S l hd)) tl) (hd::visited)
255 lemma unfold_bisim: ∀S,l,n.∀frontier,visited: list ?.
256 bisim S l n frontier visited =
258 [ O ⇒ 〈false,visited〉 (* assert false *)
261 [ nil ⇒ 〈true,visited〉
263 if beqb (\snd (\fst hd)) (\snd (\snd hd)) then
264 bisim S l m (unique_append ? (filter ? (λx.notb(memb ? x (hd::visited)))
265 (sons S l hd)) tl) (hd::visited)
269 #S #l #n cases n // qed.
271 lemma bisim_never: ∀S,l.∀frontier,visited: list ?.
272 bisim S l O frontier visited = 〈false,visited〉.
273 #frontier #visited >unfold_bisim //
276 lemma bisim_end: ∀Sig,l,m.∀visited: list ?.
277 bisim Sig l (S m) [] visited = 〈true,visited〉.
278 #n #visisted >unfold_bisim //
281 lemma bisim_step_true: ∀Sig,l,m.∀p.∀frontier,visited: list ?.
282 beqb (\snd (\fst p)) (\snd (\snd p)) = true →
283 bisim Sig l (S m) (p::frontier) visited =
284 bisim Sig l m (unique_append ? (filter ? (λx.notb(memb ? x (p::visited)))
285 (sons Sig l p)) frontier) (p::visited).
286 #Sig #l #m #p #frontier #visited #test >unfold_bisim normalize nodelta >test //
289 lemma bisim_step_false: ∀Sig,l,m.∀p.∀frontier,visited: list ?.
290 beqb (\snd (\fst p)) (\snd (\snd p)) = false →
291 bisim Sig l (S m) (p::frontier) visited = 〈false,visited〉.
292 #Sig #l #m #p #frontier #visited #test >unfold_bisim normalize nodelta >test //
295 lemma notb_eq_true_l: ∀b. notb b = true → b = false.
296 #b cases b normalize //
299 let rec pitem_enum S (i:re S) on i ≝
303 | s y ⇒ [ps S y; pp S y]
304 | o i1 i2 ⇒ compose ??? (po S) (pitem_enum S i1) (pitem_enum S i2)
305 | c i1 i2 ⇒ compose ??? (pc S) (pitem_enum S i1) (pitem_enum S i2)
306 | k i ⇒ map ?? (pk S) (pitem_enum S i)
309 lemma pitem_enum_complete : ∀S.∀i:pitem S.
310 memb (DeqItem S) i (pitem_enum S (|i|)) = true.
313 |3,4:#c normalize >(\b (refl … c)) //
314 |5,6:#i1 #i2 #Hind1 #Hind2 @(memb_compose (DeqItem S) (DeqItem S)) //
315 |#i #Hind @(memb_map (DeqItem S)) //
319 definition pre_enum ≝ λS.λi:re S.
320 compose ??? (λi,b.〈i,b〉) (pitem_enum S i) [true;false].
322 lemma pre_enum_complete : ∀S.∀e:pre S.
323 memb ? e (pre_enum S (|\fst e|)) = true.
324 #S * #i #b @(memb_compose (DeqItem S) DeqBool ? (λi,b.〈i,b〉))
325 // cases b normalize //
328 definition space_enum ≝ λS.λi1,i2:re S.
329 compose ??? (λe1,e2.〈e1,e2〉) (pre_enum S i1) (pre_enum S i2).
331 lemma space_enum_complete : ∀S.∀e1,e2: pre S.
332 memb ? 〈e1,e2〉 (space_enum S (|\fst e1|) (|\fst e2|)) = true.
333 #S #e1 #e2 @(memb_compose … (λi,b.〈i,b〉))
336 definition all_reachable ≝ λS.λe1,e2:pre S.λl: list ?.
338 ∀p. memb ? p l = true →
339 ∃w.(moves S w e1 = \fst p) ∧ (moves S w e2 = \snd p).
341 definition disjoint ≝ λS:DeqSet.λl1,l2.
342 ∀p:S. memb S p l1 = true → memb S p l2 = false.
344 lemma bisim_correct: ∀S.∀e1,e2:pre S.\sem{e1}=1\sem{e2} →
345 ∀l,n.∀frontier,visited:list ((pre S)×(pre S)).
346 |space_enum S (|\fst e1|) (|\fst e2|)| < n + |visited|→
347 all_reachable S e1 e2 visited →
348 all_reachable S e1 e2 frontier →
349 disjoint ? frontier visited →
350 \fst (bisim S l n frontier visited) = true.
351 #Sig #e1 #e2 #same #l #n elim n
352 [#frontier #visited #abs * #unique #H @False_ind @(absurd … abs)
353 @le_to_not_lt @sublist_length // * #e11 #e21 #membp
354 cut ((|\fst e11| = |\fst e1|) ∧ (|\fst e21| = |\fst e2|))
355 [|* #H1 #H2 <H1 <H2 @space_enum_complete]
356 cases (H … membp) #w * #we1 #we2 <we1 <we2 % >same_kernel_moves //
357 |#m #HI * [#visited #vinv #finv >bisim_end //]
358 #p #front_tl #visited #Hn * #u_visited #r_visited * #u_frontier #r_frontier
360 cut (∃w.(moves ? w e1 = \fst p) ∧ (moves ? w e2 = \snd p))
361 [@(r_frontier … (memb_hd … ))] #rp
362 cut (beqb (\snd (\fst p)) (\snd (\snd p)) = true)
363 [cases rp #w * #fstp #sndp <fstp <sndp @(\b ?)
364 @(proj1 … (equiv_sem … )) @same] #ptest
365 >(bisim_step_true … ptest) @HI -HI
367 |% [whd in ⊢ (??%?); >(disjoint … (memb_hd …)) whd in ⊢ (??%?); //
368 |#p1 #H (cases (orb_true_l … H)) [#eqp >(\P eqp) // |@r_visited]
370 |whd % [@unique_append_unique @(andb_true_r … u_frontier)]
371 @unique_append_elim #q #H
372 [cases (memb_sons … (memb_filter_memb … H)) -H
373 #a * #m1 #m2 cases rp #w1 * #mw1 #mw2 @(ex_intro … (w1@[a]))
374 >moves_left >moves_left >mw1 >mw2 >m1 >m2 % //
375 |@r_frontier @memb_cons //
377 |@unique_append_elim #q #H
378 [@injective_notb @(filter_true … H)
379 |cut ((q==p) = false)
380 [|#Hpq whd in ⊢ (??%?); >Hpq @disjoint @memb_cons //]
381 cases (andb_true … u_frontier) #notp #_ @(\bf ?)
382 @(not_to_not … not_eq_true_false) #eqqp <notp <eqqp >H //
388 definition all_true ≝ λS.λl.∀p:(pre S) × (pre S). memb ? p l = true →
389 (beqb (\snd (\fst p)) (\snd (\snd p)) = true).
391 definition sub_sons ≝ λS,l,l1,l2.∀x:(pre S) × (pre S).
392 memb ? x l1 = true → sublist ? (sons ? l x) l2.
394 lemma bisim_complete:
395 ∀S,l,n.∀frontier,visited,visited_res:list ?.
397 sub_sons S l visited (frontier@visited) →
398 bisim S l n frontier visited = 〈true,visited_res〉 →
399 is_bisim S visited_res l ∧ sublist ? (frontier@visited) visited_res.
401 [#fron #vis #vis_res #_ #_ >bisim_never #H destruct
403 [(* case empty frontier *)
404 -Hind #vis #vis_res #allv #H normalize in ⊢ (%→?);
406 [#membp % [@(\P ?) @allv //| @H //]|#H1 @H1]
407 |#hd cases (true_or_false (beqb (\snd (\fst hd)) (\snd (\snd hd))))
408 [|(* case head of the frontier is non ok (absurd) *)
409 #H #tl #vis #vis_res #allv >(bisim_step_false … H) #_ #H1 destruct]
410 (* frontier = hd:: tl and hd is ok *)
411 #H #tl #visited #visited_res #allv >(bisim_step_true … H)
412 (* new_visited = hd::visited are all ok *)
413 cut (all_true S (hd::visited))
414 [#p #H1 cases (orb_true_l … H1) [#eqp >(\P eqp) @H |@allv]]
415 (* we now exploit the induction hypothesis *)
416 #allh #subH #bisim cases (Hind … allh … bisim) -bisim -Hind
417 [#H1 #H2 % // #p #membp @H2 -H2 cases (memb_append … membp) -membp #membp
418 [cases (orb_true_l … membp) -membp #membp
419 [@memb_append_l2 >(\P membp) @memb_hd
420 |@memb_append_l1 @sublist_unique_append_l2 //
422 |@memb_append_l2 @memb_cons //
424 |(* the only thing left to prove is the sub_sons invariant *)
425 #x #membx cases (orb_true_l … membx)
427 #eqhdx <(\P eqhdx) #xa #membxa
428 (* xa is a son of x; we must distinguish the case xa
429 was already visited form the case xa is new *)
430 cases (true_or_false … (memb ? xa (x::visited)))
431 [(* xa visited - trivial *) #membxa @memb_append_l2 //
432 |(* xa new *) #membxa @memb_append_l1 @sublist_unique_append_l1 @memb_filter_l
435 |(* case x in visited *)
436 #H1 #xa #membxa cases (memb_append … (subH x … H1 … membxa))
437 [#H2 (cases (orb_true_l … H2))
438 [#H3 @memb_append_l2 <(\P H3) @memb_hd
439 |#H3 @memb_append_l1 @sublist_unique_append_l2 @H3
441 |#H2 @memb_append_l2 @memb_cons @H2
448 definition equiv ≝ λSig.λre1,re2:re Sig.
449 let e1 ≝ •(blank ? re1) in
450 let e2 ≝ •(blank ? re2) in
451 let n ≝ S (length ? (space_enum Sig (|\fst e1|) (|\fst e2|))) in
452 let sig ≝ (occ Sig e1 e2) in
453 (bisim ? sig n [〈e1,e2〉] []).
455 theorem euqiv_sem : ∀Sig.∀e1,e2:re Sig.
456 \fst (equiv ? e1 e2) = true ↔ \sem{e1} =1 \sem{e2}.
458 [#H @eqP_trans [|@eqP_sym @re_embedding] @eqP_trans [||@re_embedding]
459 cut (equiv ? re1 re2 = 〈true,\snd (equiv ? re1 re2)〉)
461 cases (bisim_complete … Hcut)
462 [2,3: #p whd in ⊢ ((??%?)→?); #abs @False_ind /2/]
463 #Hbisim #Hsub @(bisim_to_sem … Hbisim)
465 |#H @(bisim_correct ? (•(blank ? re1)) (•(blank ? re2)))
466 [@eqP_trans [|@re_embedding] @eqP_trans [|@H] @eqP_sym @re_embedding
468 |% // #p whd in ⊢ ((??%?)→?); #abs @False_ind /2/
469 |% // #p #H >(memb_single … H) @(ex_intro … ϵ) /2/
475 definition eqbnat ≝ λn,m:nat. eqb n m.
477 lemma eqbnat_true : ∀n,m. eqbnat n m = true ↔ n = m.
478 #n #m % [@eqb_true_to_eq | @eq_to_eqb_true]
481 definition DeqNat ≝ mk_DeqSet nat eqbnat eqbnat_true.
483 definition a ≝ s DeqNat 0.
484 definition b ≝ s DeqNat 1.
485 definition c ≝ s DeqNat 2.
487 definition exp1 ≝ ((a·b)^*·a).
488 definition exp2 ≝ a·(b·a)^*.
489 definition exp4 ≝ (b·a)^*.
491 definition exp6 ≝ a·(a ·a ·b^* + b^* ).
492 definition exp7 ≝ a · a^* · b^*.
494 definition exp8 ≝ a·a·a·a·a·a·a·a·(a^* ).
495 definition exp9 ≝ (a·a·a + a·a·a·a·a)^*.
497 example ex1 : \fst (equiv ? (exp8+exp9) exp9) = true.