4 1. Leggere ATTENTAMENTE, e magari stampare, la documentazione reperibile
7 http://mowgli.cs.unibo.it/~tassi/exercise-shannon.ma.html
9 2. Questa volta si fa sul serio:
10 l'esercizio proposto è molto difficile, occorre la vostra massima
11 concentrazione (leggi: niente cut&paste selvaggio, cercate di capire!)
19 Compilare i seguenti campi:
36 Non modificare quanto segue
38 include "nat/minus.ma".
39 definition if_then_else ≝ λT:Type.λe,t,f.match e return λ_.T with [ true ⇒ t | false ⇒ f].
40 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 }.
41 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 }.
42 interpretation "Formula if_then_else" 'if_then_else e t f = (if_then_else _ e t f).
43 definition max ≝ λn,m. if eqb (n - m) 0 then m else n.
44 definition min ≝ λn,m. if eqb (n - m) 0 then n else m.
49 Il linguaggio delle formule, dove gli atomi sono
50 rapperesentati da un numero naturale
52 inductive Formula : Type ≝
55 | FAtom: nat → Formula
56 | FAnd: Formula → Formula → Formula
57 | FOr: Formula → Formula → Formula
58 | FImpl: Formula → Formula → Formula
59 | FNot: Formula → Formula
65 La semantica di una formula `F` in un mondo `v`: `[[ F ]]_v`
67 let rec sem (v: nat → nat) (F: Formula) on F : nat ≝
71 | FAtom n ⇒ min (v n) 1
72 | FAnd F1 F2 ⇒ min (sem v F1) (sem v F2)
73 | FOr F1 F2 ⇒ max (sem v F1) (sem v F2)
74 | FImpl F1 F2 ⇒ max (1 - sem v F1) (sem v F2)
75 | FNot F1 ⇒ 1 - (sem v F1)
82 Non modificare quanto segue.
84 notation < "[[ \nbsp term 19 a \nbsp ]] \nbsp \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
85 notation > "[[ term 19 a ]] \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
86 notation > "[[ term 19 a ]]_ term 90 v" non associative with precedence 90 for @{ sem $v $a }.
87 interpretation "Semantic of Formula" 'semantics v a = (sem v a).
88 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.
93 L'operazione di sostituzione di una formula `G` al posto dell'atomo
94 `x` in una formula `F`: `F[G/x]`
97 let rec subst (x:nat) (G: Formula) (F: Formula) on F ≝
101 | FAtom n ⇒ if eqb n x then G else (FAtom n)
102 | FAnd F1 F2 ⇒ FAnd (subst x G F1) (subst x G F2)
103 | FOr F1 F2 ⇒ FOr (subst x G F1) (subst x G F2)
104 | FImpl F1 F2 ⇒ FImpl (subst x G F1) (subst x G F2)
105 | FNot F ⇒ FNot (subst x G F)
111 Non modificare quanto segue.
113 notation < "t [ \nbsp term 19 a / term 19 b \nbsp ]" non associative with precedence 19 for @{ 'substitution $b $a $t }.
114 notation > "t [ term 90 a / term 90 b]" non associative with precedence 19 for @{ 'substitution $b $a $t }.
115 interpretation "Substitution for Formula" 'substitution b a t = (subst b a t).
116 definition equiv ≝ λF1,F2. ∀v.[[ F1 ]]_v = [[ F2 ]]_v.
117 notation "hvbox(a \nbsp break mstyle color #0000ff (≡) \nbsp b)" non associative with precedence 45 for @{ 'equivF $a $b }.
118 notation > "a ≡ b" non associative with precedence 50 for @{ equiv $a $b }.
119 interpretation "equivalence for Formulas" 'equivF a b = (equiv a b).
120 lemma min_1_sem: ∀F,v.min 1 [[ F ]]_v = [[ F ]]_v. intros; cases (sem_bool F v); rewrite > H; reflexivity; qed.
121 lemma max_0_sem: ∀F,v.max [[ F ]]_v 0 = [[ F ]]_v. intros; cases (sem_bool F v); rewrite > H; reflexivity; qed.
124 La libreria di Matita
125 =====================
127 Per portare a termine l'esercitazione sono necessari i seguenti lemmi:
129 * lemma `decidable_eq_nat` : `∀x,y.x = y ∨ x ≠ y`
130 * lemma `not_eq_to_eqb_false` : `∀x,y.x ≠ y → eqb x y = false`
131 * lemma `eq_to_eqb_true` : `∀x,y.x = y → eqb x y = true`
132 * lemma `min_1_sem` : `∀F,v.min 1 [[ F ]]_v = [[ F ]]_v`
133 * lemma `max_0_sem` : `∀F,v.max [[ F ]]_v 0 = [[ F ]]_v`
135 Il teorema di espansione di Shannon
136 ===================================
138 Il teorema dice che data una formula `F`, e preso un atomo `x`, la seguente
139 formula ha in un mondo `v` la stessa semantica di `F`:
141 if eqb [[FAtom x]]_v 0 then F[FBot/x] else (F[FTop/x])
143 Ovvero, sostituisco l'atomo `x` con `FBot` se tale atomo è falso
144 nel mondo `v`, altrimenti lo sostituisco con `FTop`.
148 definition IFTE := λA,B,C:Formula. FOr (FAnd A B) (FAnd (FNot A) C).
151 ∀F,x,v. [[ FAtom x ]]_v = 0 → [[ F[FBot/x] ]]_v = [[ F ]]_v.
155 suppose ([[ FAtom x ]]_v = 0) (H).
156 we proceed by induction on F to prove ([[ F[FBot/x] ]]_v = [[ F ]]_v).
158 the thesis becomes ([[ FBot[FBot/x] ]]_v = [[ FBot ]]_v).
159 the thesis becomes ([[ FBot ]]_v = [[ FBot ]]_v).
162 the thesis becomes ([[ FTop[FBot/x] ]]_v = [[ FTop ]]_v).
163 the thesis becomes ([[ FTop ]]_v = [[ FTop ]]_v).
167 the thesis becomes ([[ (FAtom n)[FBot/x] ]]_v = [[ FAtom n ]]_v).
168 the thesis becomes ([[ if eqb n x then FBot else (FAtom n) ]]_v = [[ FAtom n ]]_v).
169 by decidable_eq_nat we proved (n = x ∨ n ≠ x) (H1).
170 we proceed by cases on H1 to prove ([[ if eqb n x then FBot else (FAtom n) ]]_v = [[ FAtom n ]]_v).
172 by H2, eq_to_eqb_true we proved (eqb n x = true) (H3).
174 ([[ if eqb n x then FBot else (FAtom n) ]]_v)
175 = ([[ if true then FBot else (FAtom n) ]]_v) by H3.
178 = ([[ FAtom x ]]_v) by H.
179 = ([[ FAtom n ]]_v) by H2.
182 by H2, not_eq_to_eqb_false we proved (eqb n x = false) (H3).
184 ([[ if eqb n x then FBot else (FAtom n) ]]_v)
185 = ([[ if false then FBot else (FAtom n) ]]_v) by H3.
190 by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
192 by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
193 the thesis becomes ([[ (FAnd f1 f2)[FBot/x] ]]_v = [[ FAnd f1 f2 ]]_v).
195 ([[ (FAnd f1 f2)[FBot/x] ]]_v)
196 = ([[ FAnd (f1[FBot/x]) (f2[FBot/x]) ]]_v).
197 = (min [[ f1[FBot/x] ]]_v [[ f2[FBot/x] ]]_v).
198 = (min [[ f1 ]]_v [[ f2[FBot/x] ]]_v) by H1.
199 = (min [[ f1 ]]_v [[ f2 ]]_v) by H2.
200 = ([[ FAnd f1 f2 ]]_v).
204 by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
206 by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
207 the thesis becomes ([[ (FOr f1 f2)[FBot/x] ]]_v = [[ FOr f1 f2 ]]_v).
209 ([[ (FOr f1 f2)[FBot/x] ]]_v)
210 = ([[ FOr (f1[FBot/x]) (f2[FBot/x]) ]]_v).
211 = (max [[ f1[FBot/x] ]]_v [[ f2[FBot/x] ]]_v).
212 = (max [[ f1 ]]_v [[ f2[FBot/x] ]]_v) by H1.
213 = (max [[ f1 ]]_v [[ f2 ]]_v) by H2.
214 = ([[ FOr f1 f2 ]]_v).
218 by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
220 by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
221 the thesis becomes ([[ (FImpl f1 f2)[FBot/x] ]]_v = [[ FImpl f1 f2 ]]_v).
223 ([[ (FImpl f1 f2)[FBot/x] ]]_v)
224 = ([[ FImpl (f1[FBot/x]) (f2[FBot/x]) ]]_v).
225 = (max (1 - [[ f1[FBot/x] ]]_v) [[ f2[FBot/x] ]]_v).
226 = (max (1 - [[ f1 ]]_v) [[ f2[FBot/x] ]]_v) by H1.
227 = (max (1 - [[ f1 ]]_v) [[ f2 ]]_v) by H2.
228 = ([[ FImpl f1 f2 ]]_v).
232 by induction hypothesis we know ([[ f[FBot/x] ]]_v = [[ f ]]_v) (H1).
233 the thesis becomes ([[ (FNot f)[FBot/x] ]]_v = [[ FNot f ]]_v).
235 ([[ (FNot f)[FBot/x] ]]_v)
236 = ([[ FNot (f[FBot/x]) ]]_v).
237 = (1 - [[ f[FBot/x] ]]_v).
238 = (1 - [[ f ]]_v) by H1.
244 ∀F,x,v. [[ FAtom x ]]_v = 1 → [[ F[FTop/x] ]]_v = [[ F ]]_v.
248 suppose ([[ FAtom x ]]_v = 1) (H).
249 we proceed by induction on F to prove ([[ F[FTop/x] ]]_v = [[ F ]]_v).
251 the thesis becomes ([[ FBot[FTop/x] ]]_v = [[ FBot ]]_v).
252 the thesis becomes ([[ FBot ]]_v = [[ FBot ]]_v).
255 the thesis becomes ([[ FTop[FTop/x] ]]_v = [[ FTop ]]_v).
256 the thesis becomes ([[ FTop ]]_v = [[ FTop ]]_v).
260 the thesis becomes ([[ (FAtom n)[FTop/x] ]]_v = [[ FAtom n ]]_v).
261 the thesis becomes ([[ if eqb n x then FTop else (FAtom n) ]]_v = [[ FAtom n ]]_v).
262 by decidable_eq_nat we proved (n = x ∨ n ≠ x) (H1).
263 we proceed by cases on H1 to prove ([[ if eqb n x then FTop else (FAtom n) ]]_v = [[ FAtom n ]]_v).
265 by H2, eq_to_eqb_true we proved (eqb n x = true) (H3).
267 ([[ if eqb n x then FTop else (FAtom n) ]]_v)
268 = ([[ if true then FTop else (FAtom n) ]]_v) by H3.
271 = ([[ FAtom x ]]_v) by H.
272 = ([[ FAtom n ]]_v) by H2.
275 by H2, not_eq_to_eqb_false we proved (eqb n x = false) (H3).
277 ([[ if eqb n x then FTop else (FAtom n) ]]_v)
278 = ([[ if false then FTop else (FAtom n) ]]_v) by H3.
283 by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
285 by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
286 the thesis becomes ([[ (FAnd f1 f2)[FTop/x] ]]_v = [[ FAnd f1 f2 ]]_v).
288 ([[ (FAnd f1 f2)[FTop/x] ]]_v)
289 = ([[ FAnd (f1[FTop/x]) (f2[FTop/x]) ]]_v).
290 = (min [[ f1[FTop/x] ]]_v [[ f2[FTop/x] ]]_v).
291 = (min [[ f1 ]]_v [[ f2[FTop/x] ]]_v) by H1.
292 = (min [[ f1 ]]_v [[ f2 ]]_v) by H2.
293 = ([[ FAnd f1 f2 ]]_v).
297 by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
299 by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
300 the thesis becomes ([[ (FOr f1 f2)[FTop/x] ]]_v = [[ FOr f1 f2 ]]_v).
302 ([[ (FOr f1 f2)[FTop/x] ]]_v)
303 = ([[ FOr (f1[FTop/x]) (f2[FTop/x]) ]]_v).
304 = (max [[ f1[FTop/x] ]]_v [[ f2[FTop/x] ]]_v).
305 = (max [[ f1 ]]_v [[ f2[FTop/x] ]]_v) by H1.
306 = (max [[ f1 ]]_v [[ f2 ]]_v) by H2.
307 = ([[ FOr f1 f2 ]]_v).
311 by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
313 by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
314 the thesis becomes ([[ (FImpl f1 f2)[FTop/x] ]]_v = [[ FImpl f1 f2 ]]_v).
316 ([[ (FImpl f1 f2)[FTop/x] ]]_v)
317 = ([[ FImpl (f1[FTop/x]) (f2[FTop/x]) ]]_v).
318 = (max (1 - [[ f1[FTop/x] ]]_v) [[ f2[FTop/x] ]]_v).
319 = (max (1 - [[ f1 ]]_v) [[ f2[FTop/x] ]]_v) by H1.
320 = (max (1 - [[ f1 ]]_v) [[ f2 ]]_v) by H2.
321 = ([[ FImpl f1 f2 ]]_v).
325 by induction hypothesis we know ([[ f[FTop/x] ]]_v = [[ f ]]_v) (H1).
326 the thesis becomes ([[ (FNot f)[FTop/x] ]]_v = [[ FNot f ]]_v).
328 ([[ (FNot f)[FTop/x] ]]_v)
329 = ([[ FNot (f[FTop/x]) ]]_v).
330 = (1 - [[ f[FTop/x] ]]_v).
331 = (1 - [[ f ]]_v) by H1.
337 ∀F,x. IFTE (FAtom x) (F[FTop/x]) (F[FBot/x]) ≡ F.
341 the thesis becomes ([[ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])]]_v = [[ F ]]_v).
342 by sem_bool we proved ([[ FAtom x]]_v = 0 ∨ [[ FAtom x]]_v = 1) (H).
343 we proceed by cases on H to prove ([[ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])]]_v = [[ F ]]_v).
346 ([[ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])]]_v)
347 = ([[ FOr (FAnd (FAtom x) (F[FTop/x])) (FAnd (FNot (FAtom x)) (F[FBot/x]))]]_v).
348 = (max [[ (FAnd (FAtom x) (F[FTop/x])) ]]_v [[ (FAnd (FNot (FAtom x)) (F[FBot/x]))]]_v).
349 = (max (min [[ FAtom x ]]_v [[ F[FTop/x] ]]_v) (min (1 - [[ FAtom x ]]_v) [[ F[FBot/x] ]]_v)).
350 = (max (min 0 [[ F[FTop/x] ]]_v) (min (1 - 0) [[ F[FBot/x] ]]_v)) by H.
351 = (max 0 (min 1 [[ F[FBot/x] ]]_v)).
352 = (max 0 [[ F[FBot/x] ]]_v) by min_1_sem.
353 = ([[ F[FBot/x] ]]_v).
354 = ([[ F ]]_v) by H1, shannon_false.
358 ([[ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])]]_v)
359 = ([[ FOr (FAnd (FAtom x) (F[FTop/x])) (FAnd (FNot (FAtom x)) (F[FBot/x]))]]_v).
360 = (max [[ (FAnd (FAtom x) (F[FTop/x])) ]]_v [[ (FAnd (FNot (FAtom x)) (F[FBot/x]))]]_v).
361 = (max (min [[ FAtom x ]]_v [[ F[FTop/x] ]]_v) (min (1 - [[ FAtom x ]]_v) [[ F[FBot/x] ]]_v)).
362 = (max (min 1 [[ F[FTop/x] ]]_v) (min (1 - 1) [[ F[FBot/x] ]]_v)) by H.
363 = (max (min 1 [[ F[FTop/x] ]]_v) (min 0 [[ F[FBot/x] ]]_v)).
364 = (max [[ F[FTop/x] ]]_v (min 0 [[ F[FBot/x] ]]_v)) by min_1_sem.
365 = (max [[ F[FTop/x] ]]_v 0).
366 = ([[ F[FTop/x] ]]_v) by max_0_sem.
367 = ([[ F ]]_v) by H1, shannon_true.
378 1. Ogni volta che nella finestra di destra compare un simbolo `∀` oppure un
379 simbolo `→` è opportuno usare il comando `assume` oppure `suppose` (che
380 vengono nuovamente spiegati in seguito).
382 2. Ogni caso (o sotto caso) della dimostrazione:
384 1. Inizia con una sequenza di comandi `assume` o `suppose` oppure
385 `by induction hypothesis we know`. Tale sequenza di comandi può anche
388 2. Continua poi con almeno un comando `the thesis becomes`.
390 3. Eventualmente seguono vari comandi `by ... we proved` per combinare le
391 ipotesi tra loro o utilizzare i teoremi già disponibili per generare nuove
394 4. Eventualmente uno o più comandi `we proceed by cases on (...) to prove (...)`.
396 5. Se necessario un comando `conclude` seguito da un numero anche
397 molto lungo di passi `= (...) by ... .` per rendere la parte
398 sinistra della vostra tesi uguale alla parte destra.
400 6. Ogni caso termina con `done`.
402 3. Ogni caso corrispondente a un nodo con sottoformule (FAnd/For/FNot)
403 avrà tante ipotesi induttive quante sono le sue sottoformule e tali
404 ipotesi sono necessarie per portare a termine la dimostrazione.
414 Questo è il caso più difficile di tutta la dimostrazione.
416 La tesi è `([[ if eqb [[ FAtom x ]]_v O then (FAtom n)[ FBot/x ] else (FAtom n[ FTop/x ]) ]]_v = [[ FAtom n ]]_ v)`
418 Per dimostrarla è necessario utilizzare il lemma `decidable_eq_nat` per
419 ottenere l'ipotesi agiuntiva `n = x ∨ n ≠ x` che chiameremo `H` e il lemma
420 `sem_bool` per ottenre l'ipotesi aggiuntiva `[[ FAtom x ]]_v = 0 ∨ [[ FAtom x ]]_v = 1`
423 Si procede poi per casi sull'ipotesi `H`, e in ogni suo sotto caso si procede
426 Nei casi in cui è presente l'ipotesi aggiuntiva `n ≠ x` è bene
427 ottenre tramite il lemma `not_eq_to_eqb_false` l'ipotesi aggiuntiva
430 Abbiamo quindi quattro casi, in tutti si procede con un comando `conclude`:
432 1. Caso in cui `n=x` e `[[ FAtom x ]]_v = 0`.
434 Utilizzando l'ipotesi `[[ FAtom x ]]_v = 0` e espandendo alcune definizioni
435 si ottiene che la parte sinistra della conclusione è
437 ([[ if eqb n x then FBot else (FAtom n) ]]_v)
439 Usando l'ipotesi `n = x`, poi il lemma `eqb_n_n` e espandendo alcune
440 definizioni si ottiene `0`. Tornando ad usare le due ipotesi
441 `n=x` e `[[ FAtom x ]]_v = 0` si ottiene una formula uguale al
442 lato destro della conclusione.
444 2. Caso in cui `n=x` e `[[ FAtom x ]]_v = 1`.
446 Analogo al caso precedente.
448 3. Caso in cui `n≠x` e `[[ FAtom x ]]_v = 0`.
450 Si ottiene l'ipotesi aggiuntiva `eqb n x = false` usando il lemma
451 `not_eq_to_eqb_false` insieme all'ipotesi `n ≠ x`. Usando il comando
452 conlude e l'ipotesi `[[ FAtom x ]]_v = 0`, la nuova ipotesi appena
453 ottenuta e espandendo alcune definizioni si ottiene una formula
454 uguale a quella di destra.
456 4. Caso in cui `n≠x` e `[[ FAtom x ]]_v = 1`.
458 Analogo al caso precedente.
460 I comandi da utilizzare
461 =======================
463 * `the thesis becomes (...).`
465 Afferma quale sia la tesi da dimostrare. Se ripetuto
466 permette di espandere le definizioni.
468 * `we proceed by cases on (...) to prove (...).`
470 Permette di andare per casi su una ipotesi (quando essa è della forma
471 `A ∨ B`) oppure su una espressione come `eqb n m`.
473 Esempio: `we proceed by cases on H to prove Q.`
475 Esempio: `we proceed by cases on (eqb x 0) to prove Q.`
479 Nelle dimostrazioni per casi o per induzioni si utulizza tale comando
480 per inizia la sotto prova relativa a un caso. Esempio: `case Fbot.`
484 Ogni caso di una dimostrazione deve essere terminato con il comando
487 * `assume ... : (...) .`
489 Assume una formula o un numero, ad esempio `assume n : (ℕ).` assume
490 un numero naturale `n`.
492 * `by ..., ..., ..., we proved (...) (...).`
494 Permette di comporre lemmi e ipotesi per ottenere nuove ipotesi.
495 Ad esempio `by H, H1 we prove (F ≡ G) (H2).` ottiene una nuova ipotesi
496 `H2` che dice che `F ≡ G` componendo insieme `H` e `H1`.
498 * `conclude (...) = (...) by ... .`
500 Il comando conclude lavora SOLO sulla parte sinistra della tesi. È il comando
501 con cui si inizia una catena di uguaglianze. La prima formula che si
502 scrive deve essere esattamente uguale alla parte sinistra della conclusione
503 originale. Esempio `conclude ([[ FAtom x ]]_v) = ([[ FAtom n ]]_v) by H.`
504 Se la giustificazione non è un lemma o una ipotesi ma la semplice espansione
505 di una definizione, la parte `by ...` deve essere omessa.
509 Continua un comando `conclude`, lavorando sempre sulla parte sinistra della