]> matita.cs.unibo.it Git - helm.git/blob - weblib/tutorial/chapter9.ma
b3f46ae04282a96988fde5cd17d334c7fa48234c
[helm.git] / weblib / tutorial / chapter9.ma
1 (* 
2 \ 5h1\ 6Moves\ 5/h1\ 6We now define the move operation, that corresponds to the advancement of the 
3 state in response to the processing of an input character a. The intuition is 
4 clear: we have to look at points inside $e$ preceding the given character a,
5 let the point traverse the character, and broadcast it. All other points must 
6 be removed.
7
8 We can give a particularly elegant definition in terms of the
9 lifted operators of the previous section:
10 *)
11
12 include "tutorial/chapter8.ma".
13
14 let rec move (S: DeqSet) (x:S) (E: pitem S) on E : pre S ≝
15  match E with
16   [ pz ⇒ 〈 `∅, false 〉
17   | pe ⇒ 〈 ϵ, false 〉
18   | ps y ⇒ 〈 `y, false 〉
19   | pp y ⇒ 〈 `y, x == y 〉
20   | po e1 e2 ⇒ (move ? x e1) ⊕ (move ? x e2) 
21   | pc e1 e2 ⇒ (move ? x e1) ⊙ (move ? x e2)
22   | pk e ⇒ (move ? x e)^⊛ ].
23   
24 lemma move_plus: ∀S:DeqSet.∀x:S.∀i1,i2:pitem S.
25   move S x (i1 + i2) = (move ? x i1) ⊕ (move ? x i2).
26 // qed.
27
28 lemma move_cat: ∀S:DeqSet.∀x:S.∀i1,i2:pitem S.
29   move S x (i1 · i2) = (move ? x i1) ⊙ (move ? x i2).
30 // qed.
31
32 lemma move_star: ∀S:DeqSet.∀x:S.∀i:pitem S.
33   move S x i^* = (move ? x i)^⊛.
34 // qed.
35
36 (*
37 Example. Let us consider the item                      
38   
39                                (•a + ϵ)((•b)*•a + •b)b
40
41 and the two moves w.r.t. the characters a and b. 
42 For a, we have two possible positions (all other points gets erased); the innermost 
43 point stops in front of the final b, while the other one broadcast inside (b^*a + b)b, 
44 so
45  
46       move((•a + ϵ)((•b)*•a + •b)b,a) = 〈(a + ϵ)((•b)^*•a + •b)•b, false〉
47
48 For b, we have two positions too. The innermost point stops in front of the final b too, 
49 while the other point reaches the end of b* and must go back through b*a:  
50     
51       move((•a + ϵ)((•b)*•a + •b)b ,b) = 〈(a +  ϵ)((•b)*•a + b)•b, false〉
52
53 *)
54
55 definition pmove ≝ λS:DeqSet.λx:S.λe:pre S. move ? x (\fst e).
56
57 lemma pmove_def : ∀S:DeqSet.∀x:S.∀i:pitem S.∀b. 
58   pmove ? x 〈i,b〉 = move ? x i.
59 // qed.
60
61 lemma eq_to_eq_hd: ∀A.∀l1,l2:list A.∀a,b. 
62   a::l1 = b::l2 → a = b.
63 #A #l1 #l2 #a #b #H destruct //
64 qed. 
65
66 lemma same_kernel: ∀S:DeqSet.∀a:S.∀i:pitem S.
67   |\fst (move ? a i)| = |i|.
68 #S #a #i elim i //
69   [#i1 #i2 #H1 #H2 >move_cat >erase_odot //
70   |#i1 #i2 #H1 #H2 >move_plus whd in ⊢ (??%%); // 
71   ]
72 qed.
73
74 theorem move_ok:
75  ∀S:DeqSet.∀a:S.∀i:pitem S.∀w: word S. 
76    \sem{move ? a i} w ↔ \sem{i} (a::w).
77 #S #a #i elim i 
78   [normalize /2/
79   |normalize /2/
80   |normalize /2/
81   |normalize #x #w cases (true_or_false (a==x)) #H >H normalize
82     [>(\P H) % [* // #bot @False_ind //| #H1 destruct /2/]
83     |% [@False_ind |#H1 cases (\Pf H) #H2 @H2 destruct //]
84     ]
85   |#i1 #i2 #HI1 #HI2 #w >move_cat
86    @iff_trans[|@sem_odot] >same_kernel >sem_cat_w
87    @iff_trans[||@(iff_or_l … (HI2 w))] @iff_or_r 
88    @iff_trans[||@iff_sym @deriv_middot //]
89    @cat_ext_l @HI1
90   |#i1 #i2 #HI1 #HI2 #w >(sem_plus S i1 i2) >move_plus >sem_plus_w 
91    @iff_trans[|@sem_oplus] 
92    @iff_trans[|@iff_or_l [|@HI2]| @iff_or_r //]
93   |#i1 #HI1 #w >move_star 
94    @iff_trans[|@sem_ostar] >same_kernel >sem_star_w 
95    @iff_trans[||@iff_sym @deriv_middot //]
96    @cat_ext_l @HI1
97   ]
98 qed.
99     
100 notation > "x ↦* E" non associative with precedence 60 for @{moves ? $x $E}.
101 let rec moves (S : DeqSet) w e on w : pre S ≝
102  match w with
103   [ nil ⇒ e
104   | cons x w' ⇒ w' ↦* (move S x (\fst e))]. 
105
106 lemma moves_empty: ∀S:DeqSet.∀e:pre S. 
107   moves ? [ ] e = e.
108 // qed.
109
110 lemma moves_cons: ∀S:DeqSet.∀a:S.∀w.∀e:pre S. 
111   moves ? (a::w)  e = moves ? w (move S a (\fst e)).
112 // qed.
113
114 lemma moves_left : ∀S,a,w,e. 
115   moves S (w@[a]) e = move S a (\fst (moves S w e)). 
116 #S #a #w elim w // #x #tl #Hind #e >moves_cons >moves_cons //
117 qed.
118
119 lemma not_epsilon_sem: ∀S:DeqSet.∀a:S.∀w: word S. ∀e:pre S. 
120   iff ((a::w) ∈ e) ((a::w) ∈ \fst e).
121 #S #a #w * #i #b cases b normalize 
122   [% /2/ * // #H destruct |% normalize /2/]
123 qed.
124
125 lemma same_kernel_moves: ∀S:DeqSet.∀w.∀e:pre S.
126   |\fst (moves ? w e)| = |\fst e|.
127 #S #w elim w //
128 qed.
129
130 theorem decidable_sem: ∀S:DeqSet.∀w: word S. ∀e:pre S. 
131    (\snd (moves ? w e) = true) ↔ \sem{e} w.
132 #S #w elim w 
133  [* #i #b >moves_empty cases b % /2/
134  |#a #w1 #Hind #e >moves_cons
135   @iff_trans [||@iff_sym @not_epsilon_sem]
136   @iff_trans [||@move_ok] @Hind
137  ]
138 qed.
139
140 (************************ pit state ***************************)
141 definition pit_pre ≝ λS.λi.〈blank S (|i|), false〉. 
142
143 let rec occur (S: DeqSet) (i: re S) on i ≝  
144   match i with
145   [ z ⇒ [ ]
146   | e ⇒ [ ]
147   | s y ⇒ [y]
148   | o e1 e2 ⇒ unique_append ? (occur S e1) (occur S e2) 
149   | c e1 e2 ⇒ unique_append ? (occur S e1) (occur S e2) 
150   | k e ⇒ occur S e].
151
152 lemma not_occur_to_pit: ∀S,a.∀i:pitem S. memb S a (occur S (|i|)) ≠ true →
153   move S a i  = pit_pre S i.
154 #S #a #i elim i //
155   [#x normalize cases (a==x) normalize // #H @False_ind /2/
156   |#i1 #i2 #Hind1 #Hind2 #H >move_cat 
157    >Hind1 [2:@(not_to_not … H) #H1 @sublist_unique_append_l1 //]
158    >Hind2 [2:@(not_to_not … H) #H1 @sublist_unique_append_l2 //] //
159   |#i1 #i2 #Hind1 #Hind2 #H >move_plus 
160    >Hind1 [2:@(not_to_not … H) #H1 @sublist_unique_append_l1 //]
161    >Hind2 [2:@(not_to_not … H) #H1 @sublist_unique_append_l2 //] //
162   |#i #Hind #H >move_star >Hind // 
163   ]
164 qed.
165
166 lemma move_pit: ∀S,a,i. move S a (\fst (pit_pre S i)) = pit_pre S i.
167 #S #a #i elim i //
168   [#i1 #i2 #Hind1 #Hind2 >move_cat >Hind1 >Hind2 // 
169   |#i1 #i2 #Hind1 #Hind2 >move_plus >Hind1 >Hind2 // 
170   |#i #Hind >move_star >Hind //
171   ]
172 qed. 
173
174 lemma moves_pit: ∀S,w,i. moves S w (pit_pre S i) = pit_pre S i.
175 #S #w #i elim w // 
176 qed. 
177  
178 lemma to_pit: ∀S,w,e. ¬ sublist S w (occur S (|\fst e|)) →
179  moves S w e = pit_pre S (\fst e).
180 #S #w elim w
181   [#e * #H @False_ind @H normalize #a #abs @False_ind /2/
182   |#a #tl #Hind #e #H cases (true_or_false (memb S a (occur S (|\fst e|))))
183     [#Htrue >moves_cons whd in ⊢ (???%); <(same_kernel … a) 
184      @Hind >same_kernel @(not_to_not … H) #H1 #b #memb cases (orb_true_l … memb)
185       [#H2 >(\P H2) // |#H2 @H1 //]
186     |#Hfalse >moves_cons >not_occur_to_pit // >Hfalse /2/ 
187     ]
188   ]
189 qed.