4 Compilare i seguenti campi:
21 Non modificare quanto segue
23 include "nat/minus.ma".
24 definition if_then_else ≝ λT:Type.λe,t,f.match e return λ_.T with [ true ⇒ t | false ⇒ f].
25 notation > "'if' term 19 e 'then' term 19 t 'else' term 90 f" non associative with precedence 19 for @{ 'if_then_else $e $t $f }.
26 notation < "'if' \nbsp term 19 e \nbsp 'then' \nbsp term 19 t \nbsp 'else' \nbsp term 90 f \nbsp" non associative with precedence 19 for @{ 'if_then_else $e $t $f }.
27 interpretation "Formula if_then_else" 'if_then_else e t f = (if_then_else _ e t f).
28 definition max ≝ λn,m. if eqb (n - m) 0 then m else n.
29 definition min ≝ λn,m. if eqb (n - m) 0 then n else m.
34 Il linguaggio delle formule, dove gli atomi sono
35 rapperesentati da un numero naturale
37 inductive Formula : Type ≝
40 | FAtom: nat → Formula
41 | FAnd: Formula → Formula → Formula
42 | FOr: Formula → Formula → Formula
43 | FImpl: Formula → Formula → Formula
44 | FNot: Formula → Formula
50 La semantica di una formula `F` in un mondo `v`: `[[ F ]]_v`
52 let rec sem (v: nat → nat) (F: Formula) on F : nat ≝
56 | FAtom n ⇒ min (v n) 1
57 | FAnd F1 F2 ⇒ min (sem v F1) (sem v F2)
58 | FOr F1 F2 ⇒ max (sem v F1) (sem v F2)
59 | FImpl F1 F2 ⇒ max (1 - sem v F1) (sem v F2)
60 | FNot F1 ⇒ 1 - (sem v F1)
67 Non modificare quanto segue.
69 notation < "[[ \nbsp term 19 a \nbsp ]] \nbsp \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
70 notation > "[[ term 19 a ]] \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
71 notation > "[[ term 19 a ]]_ term 90 v" non associative with precedence 90 for @{ sem $v $a }.
72 interpretation "Semantic of Formula" 'semantics v a = (sem v a).
73 lemma sem_bool : ∀F,v. [[ F ]]_v = 0 ∨ [[ F ]]_v = 1. intros; elim F; simplify; [left;reflexivity; |right;reflexivity; |cases (v n);[left;|cases n1;right;]reflexivity; |4,5,6: cases H; cases H1; rewrite > H2; rewrite > H3; simplify; first [ left;reflexivity | right; reflexivity ]. |cases H; rewrite > H1; simplify;[right|left]reflexivity;] qed.
78 L'operazione di sostituzione di una formula `G` al posto dell'atomo
79 `x` in una formula `F`: `F[G/x]`
82 let rec subst (x:nat) (G: Formula) (F: Formula) on F ≝
86 | FAtom n ⇒ if eqb n x then G else (FAtom n)
87 | FAnd F1 F2 ⇒ FAnd (subst x G F1) (subst x G F2)
88 | FOr F1 F2 ⇒ FOr (subst x G F1) (subst x G F2)
89 | FImpl F1 F2 ⇒ FImpl (subst x G F1) (subst x G F2)
90 | FNot F ⇒ FNot (subst x G F)
96 Non modificare quanto segue.
98 notation < "t [ \nbsp term 19 a / term 19 b \nbsp ]" non associative with precedence 19 for @{ 'substitution $b $a $t }.
99 notation > "t [ term 90 a / term 90 b]" non associative with precedence 19 for @{ 'substitution $b $a $t }.
100 interpretation "Substitution for Formula" 'substitution b a t = (subst b a t).
101 definition equiv ≝ λF1,F2. ∀v.[[ F1 ]]_v = [[ F2 ]]_v.
102 notation "hvbox(a \nbsp break mstyle color #0000ff (≡) \nbsp b)" non associative with precedence 45 for @{ 'equivF $a $b }.
103 notation > "a ≡ b" non associative with precedence 50 for @{ equiv $a $b }.
104 interpretation "equivalence for Formulas" 'equivF a b = (equiv a b).
105 lemma min_1_sem: ∀F,v.min 1 [[ F ]]_v = [[ F ]]_v. intros; cases (sem_bool F v); rewrite > H; reflexivity; qed.
106 lemma max_0_sem: ∀F,v.max [[ F ]]_v 0 = [[ F ]]_v. intros; cases (sem_bool F v); rewrite > H; reflexivity; qed.
107 definition IFTE := λA,B,C:Formula. FOr (FAnd A B) (FAnd (FNot A) C).
111 La libreria di Matita
112 =====================
114 Per portare a termine l'esercitazione sono necessari i seguenti lemmi:
116 * lemma `decidable_eq_nat` : `∀x,y.x = y ∨ x ≠ y`
117 * lemma `sem_bool` : `∀F,v. [[ F ]]_v = 0 ∨ [[ F ]]_v = 1`
118 * lemma `not_eq_to_eqb_false` : `∀x,y.x ≠ y → eqb x y = false`
119 * lemma `eq_to_eqb_true` : `∀x,y.x = y → eqb x y = true`
120 * lemma `min_1_sem` : `∀F,v.min 1 [[ F ]]_v = [[ F ]]_v`
121 * lemma `max_0_sem` : `∀F,v.max [[ F ]]_v 0 = [[ F ]]_v`
123 Nota su `x = y` e `eqb x y`
124 ---------------------------
126 Se vi siete mai chiesti la differenza tra `x = y` ed `eqb x y`
127 quanto segue prova a chiarirla.
129 Presi due numeri `x` e `y` in ℕ, dire che `x = y` significa i due numeri
130 sono lo stesso numero, ovvero che se `x` è il numero `3`,
131 anche `y` è il numero `3`.
133 `eqb` è un funzione, un programma, che confronta due numeri naturali
134 e restituisce `true` se sono uguali, `false` se sono diversi. L'utilizzo
135 di tale programma è necessario per usare il costrutto (che è a sua volta
136 un programma) `if E then A else B`, che lancia il programma `E`,
138 risultato è `true` si comporta come `A` altrimenti come `B`. Come
139 ben sapete i programmi possono contenere errori. In particolare anche
140 `eqb` potrebbe essere sbagliato, e per esempio restituire sempre `true`.
141 I teoremi `eq_to_eqb_true` e
142 `not_eq_to_eqb_false` sono la dimostrazione che il programma `eqb` è
143 corretto, ovvero che che se `x = y` allora `eqb x y` restituisce `true`,
144 se `x ≠ y` allora `eqb x y` restituisce `false`.
146 Il teorema di espansione di Shannon
147 ===================================
149 Si definisce un connettivo logico `IFTE A B C` come
151 FOr (FAnd A B) (FAnd (FNot A) C)
153 Il teorema dice che data una formula `F`, e preso un atomo `x`, la seguente
154 formula è equivalente a `F`:
156 IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])
158 Ovvero, fissato un mondo `v`, sostituisco l'atomo `x` con `FBot` se tale
159 atomo è falso, lo sostituisco con `FTop` se è vero.
161 La dimostrazione è composta da due lemmi, `shannon_false` e `shannon_true`.
163 Vediamo solo la dimostrazione del primo, essendo il secondo del tutto analogo.
164 Il lemma asserisce quanto segue:
166 ∀F,x,v. [[ FAtom x ]]_v = 0 → [[ F[FBot/x] ]]_v = [[ F ]]_v
168 Una volta assunte la formula `F`, l'atomo `x`, il mondo `v` e aver
169 supposto che `[[ FAtom x ]]_v = 0` si procede per induzione su `F`.
170 I casi `FTop` e `FBot` sono banali. Nei casi `FAnd/FOr/FImpl/FNot`,
171 una volta assunte le sottoformule e le relative ipotesi induttive,
172 si conclude con una catena di uguaglianze.
174 Il caso `FAtom` richiede maggiore cura. Assunto l'indice dell'atomo `n`,
175 occorre utilizzare il lemma `decidable_eq_nat` per ottenere l'ipotesi
176 aggiuntiva `n = x ∨ n ≠ x` (dove `x` è l'atomo su cui predica il teorema).
177 Si procede per casi sull'ipotesi appena ottenuta.
178 In entrambi i casi, usando i lemmi `eq_to_eqb_true` oppure `not_eq_to_eqb_false`
179 si ottengolo le ipotesi aggiuntive `(eqb n x = true)` oppure `(eqb n x = false)`.
180 Entrambi i casi si concludono con una catena di uguaglianze.
182 Il teorema principale si dimostra utilizzando il lemma `sem_bool` per
183 ottenre l'ipotesi `[[ FAtom x ]]_v = 0 ∨ [[ FAtom x ]]_v = 1` su cui
184 si procede poi per casi. Entrambi i casi si concludono con
185 una catena di uguaglianze che utilizza i lemmi dimostrati in precedenza
186 e i lemmi `min_1_sem` oppure `max_0_sem`.
191 ∀F,x,v. [[ FAtom x ]]_v = 0 → [[ F[FBot/x] ]]_v = [[ F ]]_v.
196 suppose ([[ FAtom x ]]_v = 0) (H).
197 we proceed by induction on F to prove ([[ F[FBot/x] ]]_v = [[ F ]]_v).
199 the thesis becomes ([[ FBot[FBot/x] ]]_v = [[ FBot ]]_v).
200 the thesis becomes ([[ FBot ]]_v = [[ FBot ]]_v).
203 the thesis becomes ([[ FTop[FBot/x] ]]_v = [[ FTop ]]_v).
204 the thesis becomes ([[ FTop ]]_v = [[ FTop ]]_v).
208 the thesis becomes ([[ (FAtom n)[FBot/x] ]]_v = [[ FAtom n ]]_v).
209 the thesis becomes ([[ if eqb n x then FBot else (FAtom n) ]]_v = [[ FAtom n ]]_v).
210 by decidable_eq_nat we proved (n = x ∨ n ≠ x) (H1).
211 we proceed by cases on H1 to prove ([[ if eqb n x then FBot else (FAtom n) ]]_v = [[ FAtom n ]]_v).
213 by H2, eq_to_eqb_true we proved (eqb n x = true) (H3).
215 ([[ if eqb n x then FBot else (FAtom n) ]]_v)
216 = ([[ if true then FBot else (FAtom n) ]]_v) by H3.
219 = ([[ FAtom x ]]_v) by H.
220 = ([[ FAtom n ]]_v) by H2.
223 by H2, not_eq_to_eqb_false we proved (eqb n x = false) (H3).
225 ([[ if eqb n x then FBot else (FAtom n) ]]_v)
226 = ([[ if false then FBot else (FAtom n) ]]_v) by H3.
231 by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
233 by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
234 the thesis becomes ([[ (FAnd f1 f2)[FBot/x] ]]_v = [[ FAnd f1 f2 ]]_v).
236 ([[ (FAnd f1 f2)[FBot/x] ]]_v)
237 = ([[ FAnd (f1[FBot/x]) (f2[FBot/x]) ]]_v).
238 = (min [[ f1[FBot/x] ]]_v [[ f2[FBot/x] ]]_v).
239 = (min [[ f1 ]]_v [[ f2[FBot/x] ]]_v) by H1.
240 = (min [[ f1 ]]_v [[ f2 ]]_v) by H2.
241 = ([[ FAnd f1 f2 ]]_v).
245 by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
247 by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
248 the thesis becomes ([[ (FOr f1 f2)[FBot/x] ]]_v = [[ FOr f1 f2 ]]_v).
250 ([[ (FOr f1 f2)[FBot/x] ]]_v)
251 = ([[ FOr (f1[FBot/x]) (f2[FBot/x]) ]]_v).
252 = (max [[ f1[FBot/x] ]]_v [[ f2[FBot/x] ]]_v).
253 = (max [[ f1 ]]_v [[ f2[FBot/x] ]]_v) by H1.
254 = (max [[ f1 ]]_v [[ f2 ]]_v) by H2.
255 = ([[ FOr f1 f2 ]]_v).
259 by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
261 by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
262 the thesis becomes ([[ (FImpl f1 f2)[FBot/x] ]]_v = [[ FImpl f1 f2 ]]_v).
264 ([[ (FImpl f1 f2)[FBot/x] ]]_v)
265 = ([[ FImpl (f1[FBot/x]) (f2[FBot/x]) ]]_v).
266 = (max (1 - [[ f1[FBot/x] ]]_v) [[ f2[FBot/x] ]]_v).
267 = (max (1 - [[ f1 ]]_v) [[ f2[FBot/x] ]]_v) by H1.
268 = (max (1 - [[ f1 ]]_v) [[ f2 ]]_v) by H2.
269 = ([[ FImpl f1 f2 ]]_v).
273 by induction hypothesis we know ([[ f[FBot/x] ]]_v = [[ f ]]_v) (H1).
274 the thesis becomes ([[ (FNot f)[FBot/x] ]]_v = [[ FNot f ]]_v).
276 ([[ (FNot f)[FBot/x] ]]_v)
277 = ([[ FNot (f[FBot/x]) ]]_v).
278 = (1 - [[ f[FBot/x] ]]_v).
279 = (1 - [[ f ]]_v) by H1.
286 ∀F,x,v. [[ FAtom x ]]_v = 1 → [[ F[FTop/x] ]]_v = [[ F ]]_v.
291 suppose ([[ FAtom x ]]_v = 1) (H).
292 we proceed by induction on F to prove ([[ F[FTop/x] ]]_v = [[ F ]]_v).
294 the thesis becomes ([[ FBot[FTop/x] ]]_v = [[ FBot ]]_v).
295 the thesis becomes ([[ FBot ]]_v = [[ FBot ]]_v).
298 the thesis becomes ([[ FTop[FTop/x] ]]_v = [[ FTop ]]_v).
299 the thesis becomes ([[ FTop ]]_v = [[ FTop ]]_v).
303 the thesis becomes ([[ (FAtom n)[FTop/x] ]]_v = [[ FAtom n ]]_v).
304 the thesis becomes ([[ if eqb n x then FTop else (FAtom n) ]]_v = [[ FAtom n ]]_v).
305 by decidable_eq_nat we proved (n = x ∨ n ≠ x) (H1).
306 we proceed by cases on H1 to prove ([[ if eqb n x then FTop else (FAtom n) ]]_v = [[ FAtom n ]]_v).
308 by H2, eq_to_eqb_true we proved (eqb n x = true) (H3).
310 ([[ if eqb n x then FTop else (FAtom n) ]]_v)
311 = ([[ if true then FTop else (FAtom n) ]]_v) by H3.
314 = ([[ FAtom x ]]_v) by H.
315 = ([[ FAtom n ]]_v) by H2.
318 by H2, not_eq_to_eqb_false we proved (eqb n x = false) (H3).
320 ([[ if eqb n x then FTop else (FAtom n) ]]_v)
321 = ([[ if false then FTop else (FAtom n) ]]_v) by H3.
326 by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
328 by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
329 the thesis becomes ([[ (FAnd f1 f2)[FTop/x] ]]_v = [[ FAnd f1 f2 ]]_v).
331 ([[ (FAnd f1 f2)[FTop/x] ]]_v)
332 = ([[ FAnd (f1[FTop/x]) (f2[FTop/x]) ]]_v).
333 = (min [[ f1[FTop/x] ]]_v [[ f2[FTop/x] ]]_v).
334 = (min [[ f1 ]]_v [[ f2[FTop/x] ]]_v) by H1.
335 = (min [[ f1 ]]_v [[ f2 ]]_v) by H2.
336 = ([[ FAnd f1 f2 ]]_v).
340 by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
342 by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
343 the thesis becomes ([[ (FOr f1 f2)[FTop/x] ]]_v = [[ FOr f1 f2 ]]_v).
345 ([[ (FOr f1 f2)[FTop/x] ]]_v)
346 = ([[ FOr (f1[FTop/x]) (f2[FTop/x]) ]]_v).
347 = (max [[ f1[FTop/x] ]]_v [[ f2[FTop/x] ]]_v).
348 = (max [[ f1 ]]_v [[ f2[FTop/x] ]]_v) by H1.
349 = (max [[ f1 ]]_v [[ f2 ]]_v) by H2.
350 = ([[ FOr f1 f2 ]]_v).
354 by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
356 by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
357 the thesis becomes ([[ (FImpl f1 f2)[FTop/x] ]]_v = [[ FImpl f1 f2 ]]_v).
359 ([[ (FImpl f1 f2)[FTop/x] ]]_v)
360 = ([[ FImpl (f1[FTop/x]) (f2[FTop/x]) ]]_v).
361 = (max (1 - [[ f1[FTop/x] ]]_v) [[ f2[FTop/x] ]]_v).
362 = (max (1 - [[ f1 ]]_v) [[ f2[FTop/x] ]]_v) by H1.
363 = (max (1 - [[ f1 ]]_v) [[ f2 ]]_v) by H2.
364 = ([[ FImpl f1 f2 ]]_v).
368 by induction hypothesis we know ([[ f[FTop/x] ]]_v = [[ f ]]_v) (H1).
369 the thesis becomes ([[ (FNot f)[FTop/x] ]]_v = [[ FNot f ]]_v).
371 ([[ (FNot f)[FTop/x] ]]_v)
372 = ([[ FNot (f[FTop/x]) ]]_v).
373 = (1 - [[ f[FTop/x] ]]_v).
374 = (1 - [[ f ]]_v) by H1.
381 ∀F,x. IFTE (FAtom x) (F[FTop/x]) (F[FBot/x]) ≡ F.
386 the thesis becomes ([[ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])]]_v = [[ F ]]_v).
387 by sem_bool we proved ([[ FAtom x]]_v = 0 ∨ [[ FAtom x]]_v = 1) (H).
388 we proceed by cases on H to prove ([[ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])]]_v = [[ F ]]_v).
391 ([[ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])]]_v)
392 = ([[ FOr (FAnd (FAtom x) (F[FTop/x])) (FAnd (FNot (FAtom x)) (F[FBot/x]))]]_v).
393 = (max [[ (FAnd (FAtom x) (F[FTop/x])) ]]_v [[ (FAnd (FNot (FAtom x)) (F[FBot/x]))]]_v).
394 = (max (min [[ FAtom x ]]_v [[ F[FTop/x] ]]_v) (min (1 - [[ FAtom x ]]_v) [[ F[FBot/x] ]]_v)).
395 = (max (min 0 [[ F[FTop/x] ]]_v) (min (1 - 0) [[ F[FBot/x] ]]_v)) by H.
396 = (max 0 (min 1 [[ F[FBot/x] ]]_v)).
397 = (max 0 [[ F[FBot/x] ]]_v) by min_1_sem.
398 = ([[ F[FBot/x] ]]_v).
399 = ([[ F ]]_v) by H1, shannon_false.
403 ([[ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])]]_v)
404 = ([[ FOr (FAnd (FAtom x) (F[FTop/x])) (FAnd (FNot (FAtom x)) (F[FBot/x]))]]_v).
405 = (max [[ (FAnd (FAtom x) (F[FTop/x])) ]]_v [[ (FAnd (FNot (FAtom x)) (F[FBot/x]))]]_v).
406 = (max (min [[ FAtom x ]]_v [[ F[FTop/x] ]]_v) (min (1 - [[ FAtom x ]]_v) [[ F[FBot/x] ]]_v)).
407 = (max (min 1 [[ F[FTop/x] ]]_v) (min (1 - 1) [[ F[FBot/x] ]]_v)) by H.
408 = (max (min 1 [[ F[FTop/x] ]]_v) (min 0 [[ F[FBot/x] ]]_v)).
409 = (max [[ F[FTop/x] ]]_v (min 0 [[ F[FBot/x] ]]_v)) by min_1_sem.
410 = (max [[ F[FTop/x] ]]_v 0).
411 = ([[ F[FTop/x] ]]_v) by max_0_sem.
412 = ([[ F ]]_v) by H1, shannon_true.
424 1. Ogni volta che nella finestra di destra compare un simbolo `∀` oppure un
425 simbolo `→` è opportuno usare il comando `assume` oppure `suppose`
426 oppure (se si è in un caso di una dimostrazione per induzione) il comando
427 `by induction hypothesis we know` (che vengono nuovamente spiegati in seguito).
429 2. Ogni caso (o sotto caso) della dimostrazione:
431 1. Inizia con una sequenza di comandi `assume` o `suppose` oppure
432 `by induction hypothesis we know`. Tale sequenza di comandi può anche
435 2. Continua poi con almeno un comando `the thesis becomes`.
437 3. Eventualmente seguono vari comandi `by ... we proved` per
438 utilizzare i teoremi già disponibili per generare nuove
441 4. Eventualmente uno o più comandi `we proceed by cases on (...) to prove (...)`.
443 5. Se necessario un comando `conclude` seguito da un numero anche
444 molto lungo di passi `= (...) by ... .` per rendere la parte
445 sinistra della vostra tesi uguale alla parte destra.
447 6. Ogni caso termina con `done`.
449 3. Ogni caso corrispondente a un nodo con sottoformule (FAnd/For/FNot)
450 avrà tante ipotesi induttive quante sono le sue sottoformule e tali
451 ipotesi sono necessarie per portare a termine la dimostrazione.
453 I comandi da utilizzare
454 =======================
456 * `the thesis becomes (...).`
458 Afferma quale sia la tesi da dimostrare. Se ripetuto
459 permette di espandere le definizioni.
461 * `we proceed by cases on (...) to prove (...).`
463 Permette di andare per casi su una ipotesi (quando essa è della forma
466 Esempio: `we proceed by cases on H to prove Q.`
470 Nelle dimostrazioni per casi o per induzioni si utulizza tale comando
471 per inizia la sotto prova relativa a un caso. Esempio: `case Fbot.`
475 Ogni caso di una dimostrazione deve essere terminato con il comando
478 * `assume ... : (...) .`
480 Assume una formula o un numero, ad esempio `assume n : (ℕ).` assume
481 un numero naturale `n`.
483 * `by ..., ..., ..., we proved (...) (...).`
485 Permette di comporre lemmi e ipotesi per ottenere nuove ipotesi.
486 Ad esempio `by H, H1 we prove (F ≡ G) (H2).` ottiene una nuova ipotesi
487 `H2` che dice che `F ≡ G` componendo insieme `H` e `H1`.
489 * `conclude (...) = (...) by ... .`
491 Il comando conclude lavora SOLO sulla parte sinistra della tesi. È il comando
492 con cui si inizia una catena di uguaglianze. La prima formula che si
493 scrive deve essere esattamente uguale alla parte sinistra della conclusione
494 originale. Esempio `conclude ([[ FAtom x ]]_v) = ([[ FAtom n ]]_v) by H.`
495 Se la giustificazione non è un lemma o una ipotesi ma la semplice espansione
496 di una definizione, la parte `by ...` deve essere omessa.
500 Continua un comando `conclude`, lavorando sempre sulla parte sinistra della