1 (* Esercitazione di logica 29/10/2008. *)
6 Compilare i seguenti campi:
18 Prima di abbandonare la postazione:
20 * salvare il file (menu `File ▹ Save as ...`) nella directory (cartella)
21 /public/ con nome linguaggi_Account1.ma, ad esempio Mario Rossi, il cui
22 account è mrossi, deve salvare il file in /public/linguaggi_mrossi.ma
24 * mandatevi via email o stampate il file. Per stampare potete usare
25 usare l'editor gedit che offre la funzionalità di stampa
33 Il teorema di dualizzazione dice che date due formule `F1` ed `F2`,
34 se le due formule sono equivalenti (`F1 ≡ F2`) allora anche le
35 loro dualizzate lo sono (`dualize F1 ≡ dualize F2`).
37 L'ingrediente principale è la funzione di dualizzazione di una formula `F`:
39 * Scambia FTop con FBot e viceversa
41 * Scambia il connettivo FAnd con FOr e viceversa
43 * Sostituisce il connettivo FImpl con FAnd e nega la
46 Ad esempio la formula `A → (B ∧ ⊥)` viene dualizzata in
49 Per dimostrare il teorema di dualizzazione in modo agevole è necessario
50 definire altre nozioni:
52 * La funzione `negate` che presa una formula `F` ne nega gli atomi.
53 Ad esempio la formula `(A ∨ (⊤ → B))` deve diventare `¬A ∨ (⊤ → ¬B)`.
55 * La funzione `invert` permette di invertire un mondo `v`.
56 Ovvero, per ogni indice di atomo `i`, se `v i` restituisce
57 `1` allora `(invert v) i` restituisce `0` e viceversa.
64 Non modificare quanto segue
66 include "nat/minus.ma".
67 definition if_then_else ≝ λT:Type.λe,t,f.match e return λ_.T with [ true ⇒ t | false ⇒ f].
68 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 }.
69 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 }.
70 interpretation "Formula if_then_else" 'if_then_else e t f = (if_then_else _ e t f).
71 definition max ≝ λn,m. if eqb (n - m) 0 then m else n.
72 definition min ≝ λn,m. if eqb (n - m) 0 then n else m.
77 Il linguaggio delle formule, dove gli atomi sono
78 rapperesentati da un numero naturale
80 inductive Formula : Type ≝
83 | FAtom: nat → Formula
84 | FAnd: Formula → Formula → Formula
85 | FOr: Formula → Formula → Formula
86 | FImpl: Formula → Formula → Formula
87 | FNot: Formula → Formula
93 Modificare la funzione `sem` scritta nella precedente
94 esercitazione in modo che valga solo 0 oppure 1 nel caso degli
95 atomi, anche nel caso il mondo `v` restituisca un numero
98 Suggerimento: non è necessario usare il costrutto if_then_else
99 e tantomento il predicato di maggiore o uguale.
101 let rec sem (v: nat → nat) (F: Formula) on F ≝
105 | FAtom n ⇒ (*BEGIN*)min (v n) 1(*END*)
106 | FAnd F1 F2 ⇒ min (sem v F1) (sem v F2)
107 | FOr F1 F2 ⇒ max (sem v F1) (sem v F2)
108 | FImpl F1 F2 ⇒ max (1 - sem v F1) (sem v F2)
109 | FNot F1 ⇒ 1 - (sem v F1)
116 Non modificare quanto segue.
118 notation < "[[ \nbsp term 19 a \nbsp ]] \nbsp \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
119 notation > "[[ term 19 a ]] \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
120 notation > "[[ term 19 a ]]_ term 90 v" non associative with precedence 90 for @{ sem $v $a }.
121 interpretation "Semantic of Formula" 'semantics v a = (sem v a).
125 else if eqb x 1 then 1
131 La semantica della formula `(A ∨ C)` nel mondo `v20` in cui
132 `A` vale `2` e `C` vale `0` deve valere `1`.
135 eval normalize on [[For (FAtom 0) (FAtom 2)]]_v20.
139 La libreria di Matita
140 =====================
142 Gli strumenti per la dimostrazione assistita sono corredati da
143 librerie di teoremi già dimostrati. Per portare a termine l'esercitazione
144 sono necessari i seguenti lemmi:
146 * lemma `sem_bool` : `∀F,v. [[ F ]]_v = 0 ∨ [[ F ]]_v = 1`
147 * lemma `min_bool` : `∀n. min n 1 = 0 ∨ min n 1 = 1`
148 * lemma `min_max` : `∀F,G,v.min (1 - [[F]]_v) (1 - [[G]]_v) = 1 - max [[F]]_v [[G]]_v`
149 * lemma `max_min` : `∀F,G,v.max (1 - [[F]]_v) (1 - [[G]]_v) = 1 - min [[F]]_v [[G]]_v`
150 * lemma `equiv_rewrite` : `∀F1,F2,F3. F1 ≡ F2 → F1 ≡ F3 → F2 ≡ F3`
157 Non modificare quanto segue.
159 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.
160 lemma min_bool : ∀n. min n 1 = 0 ∨ min n 1 = 1. intros; cases n; [left;reflexivity] cases n1; right; reflexivity; qed.
161 lemma min_max : ∀F,G,v. min (1 - [[F]]_v) (1 - [[G]]_v) = 1 - max [[F]]_v [[G]]_v. intros; cases (sem_bool F v);cases (sem_bool G v); rewrite > H; rewrite >H1; simplify; reflexivity; qed.
162 lemma max_min : ∀F,G,v. max (1 - [[F]]_v) (1 - [[G]]_v) = 1 - min [[F]]_v [[G]]_v. intros; cases (sem_bool F v);cases (sem_bool G v); rewrite > H; rewrite >H1; simplify; reflexivity; qed.
167 Definire per ricorsione strutturale la funzione `negate`
168 che presa una formula `F` ne nega gli atomi.
170 Ad esempio la formula `(A ∨ (⊤ → B))` deve diventare
173 let rec negate (F: Formula) on F ≝
177 | FAtom n ⇒ FNot (FAtom n)
178 | FAnd F1 F2 ⇒ FAnd (negate F1) (negate F2)
179 | FOr F1 F2 ⇒ FOr (negate F1) (negate F2)
180 | FImpl F1 F2 ⇒ FImpl (negate F1) (negate F2)
181 | FNot F ⇒ FNot (negate F)
187 Testare la funzione `negate`. Il risultato atteso è:
189 FOr (FNot (FAtom O)) (FImpl FTop (FNot (FAtom 1)))
192 eval normalize on (negate (FOr (FAtom 0) (FImpl FTop (FAtom 1)))).
197 Non modificare quanto segue
199 definition equiv ≝ λF1,F2. ∀v.[[ F1 ]]_v = [[ F2 ]]_v.
200 notation "hvbox(a \nbsp break mstyle color #0000ff (≡) \nbsp b)" non associative with precedence 45 for @{ 'equivF $a $b }.
201 notation > "a ≡ b" non associative with precedence 50 for @{ equiv $a $b }.
202 interpretation "equivalence for Formulas" 'equivF a b = (equiv a b).
203 lemma equiv_rewrite : ∀F1,F2,F3. F1 ≡ F2 → F1 ≡ F3 → F2 ≡ F3. intros; intro; autobatch. qed.
208 Definire per ricorsione strutturale la funzione di
209 dualizzazione di una formula `F`. Tale funzione:
211 * Scambia FTop con FBot e viceversa
213 * Scambia il connettivo FAnd con FOr e viceversa
215 * Sostituisce il connettivo FImpl con FAnd e nega la
218 Ad esempio la formula `A → (B ∧ ⊥)` viene dualizzata in
221 let rec dualize (F : Formula) on F : Formula ≝
223 [ (*BEGIN*)FBot ⇒ FTop
226 | FAnd F1 F2 ⇒ FOr (dualize F1) (dualize F2)
227 | FOr F1 F2 ⇒ FAnd (dualize F1) (dualize F2)
228 | FImpl F1 F2 ⇒ FAnd (FNot (dualize F1)) (dualize F2)
229 | FNot F ⇒ FNot (dualize F)(*END*)
235 Testare la funzione `dualize`. Il risultato atteso è:
237 FAnd (FNot (FAtom O)) (FOr (FAtom 1) FTop)
240 eval normalize on (dualize (FImpl (FAtom 0) (FAnd (FAtom 1) FBot))).
245 La funzione `invert` permette di invertire un mondo `v`.
246 Ovvero, per ogni indice di atomo `i`, se `v i` restituisce
247 `1` allora `(invert v) i` restituisce `0` e viceversa.
251 λv:ℕ -> ℕ. λx. if eqb (min (v x) 1) 0 then 1 else 0.
253 interpretation "Inversione del mondo" 'invert v = (invert v).
257 Il linguaggio di dimostrazione di Matita
258 ========================================
260 Per dimostrare il lemma `negate_invert` in modo agevole è necessario
261 utilizzare il seguente comando:
265 Quando la conclusuine è `a = b` permette di cambiarla in `b = a`.
272 Dimostrare il lemma `negate_invert` che asserisce che
273 la semantica in un mondo `v` associato alla formula
274 negata di `F` e uguale alla semantica associata
275 a `F` in un mondo invertito.
278 ∀F:Formula.∀v:ℕ→ℕ.[[ negate F ]]_v=[[ F ]]_(invert v).
281 we proceed by induction on F to prove ([[ negate F ]]_v=[[ F ]]_(invert v)).
283 the thesis becomes ([[ negate FBot ]]_v=[[ FBot ]]_(invert v)).
286 the thesis becomes ([[ negate FTop ]]_v=[[ FTop ]]_(invert v)).
290 the thesis becomes ([[ negate (FAtom n) ]]_v=[[ FAtom n ]]_(invert v)).
291 the thesis becomes (1 - (min (v n) 1)= min (invert v n) 1).
292 the thesis becomes (1 - (min (v n) 1)= min (if eqb (min (v n) 1) 0 then 1 else 0) 1).
293 by min_bool we proved (min (v n) 1 = 0 ∨ min (v n) 1 = 1) (H1);
294 we proceed by cases on (H1) to prove (1 - (min (v n) 1)= min (if eqb (min (v n) 1) 0 then 1 else 0) 1).
302 (min (if eqb (min (v n) 1) O then 1 else O) 1)
303 = (min (if eqb 0 0 then 1 else O) 1) by H.
314 (min (if eqb (min (v n) 1) O then 1 else O) 1)
315 = (min (if eqb 1 0 then 1 else O) 1) by H.
321 by induction hypothesis we know
322 ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
324 by induction hypothesis we know
325 ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
327 ([[ negate (FAnd f f1) ]]_v=[[ FAnd f f1 ]]_(invert v)).
329 (min [[ negate f ]]_v [[ negate f1]]_v = [[ FAnd f f1 ]]_(invert v)).
331 (min [[ negate f ]]_v [[ negate f1]]_v)
332 = (min [[ f ]]_(invert v) [[ negate f1]]_v) by H.
333 = (min [[ f ]]_(invert v) [[ f1]]_(invert v)) by H1.
337 by induction hypothesis we know
338 ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
340 by induction hypothesis we know
341 ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
343 ([[ negate (FOr f f1) ]]_v=[[ FOr f f1 ]]_(invert v)).
345 (max [[ negate f ]]_v [[ negate f1]]_v = [[ FOr f f1 ]]_(invert v)).
347 (max [[ negate f ]]_v [[ negate f1]]_v)
348 = (max [[ f ]]_(invert v) [[ negate f1]]_v) by H.
349 = (max [[ f ]]_(invert v) [[ f1]]_(invert v)) by H1.
353 by induction hypothesis we know
354 ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
356 by induction hypothesis we know
357 ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
359 ([[ negate (FImpl f f1) ]]_v=[[ FImpl f f1 ]]_(invert v)).
361 (max (1 - [[ negate f ]]_v) [[ negate f1]]_v = [[ FImpl f f1 ]]_(invert v)).
363 (max (1 - [[ negate f ]]_v) [[ negate f1]]_v)
364 = (max (1 - [[ f ]]_(invert v)) [[ negate f1]]_v) by H.
365 = (max (1 - [[ f ]]_(invert v)) [[ f1]]_(invert v)) by H1.
369 by induction hypothesis we know
370 ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
372 ([[ negate (FNot f) ]]_v=[[ FNot f ]]_(invert v)).
374 (1 - [[ negate f ]]_v=[[ FNot f ]]_(invert v)).
375 conclude (1 - [[ negate f ]]_v) = (1 - [[f]]_(invert v)) by H.
382 Dimostrare che la funzione negate rispetta l'equivalenza.
385 ∀F:Formula.∀G:Formula.F ≡ G→negate F ≡ negate G.
389 the thesis becomes (negate F ≡ negate G).
390 the thesis becomes (∀v:ℕ→ℕ.[[ negate F ]]_v=[[ negate G ]]_v).
394 = [[ F ]]_(invert v) by negate_invert.
395 = [[ G ]]_(invert v) by H.
396 = [[ negate G ]]_v by negate_invert.
403 Dimostrare che per ogni formula `F`, `(negae F)` equivale a
404 dualizzarla e negarla.
406 lemma not_dualize_eq_negate:
407 ∀F:Formula.negate F ≡ FNot (dualize F).
409 the thesis becomes (∀v:ℕ→ℕ.[[negate F]]_v=[[FNot (dualize F)]]_v).
411 we proceed by induction on F to prove ([[negate F]]_v=[[FNot (dualize F)]]_v).
413 the thesis becomes ([[ negate FBot ]]_v=[[ FNot (dualize FBot) ]]_v).
416 the thesis becomes ([[ negate FTop ]]_v=[[ FNot (dualize FTop) ]]_v).
420 the thesis becomes ([[ negate (FAtom n) ]]_v=[[ FNot (dualize (FAtom n)) ]]_v).
424 by induction hypothesis we know
425 ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
427 by induction hypothesis we know
428 ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
430 ([[ negate (FAnd f f1) ]]_v=[[ FNot (dualize (FAnd f f1)) ]]_v).
432 (min [[ negate f ]]_v [[ negate f1 ]]_v=[[ FNot (dualize (FAnd f f1)) ]]_v).
434 (min [[ negate f ]]_v [[ negate f1 ]]_v)
435 = (min [[ FNot (dualize f) ]]_v [[ negate f1 ]]_v) by H.
436 = (min [[ FNot (dualize f) ]]_v [[ FNot (dualize f1) ]]_v) by H1.
437 = (min (1 - [[ dualize f ]]_v) (1 - [[ dualize f1 ]]_v)).
438 = (1 - (max [[ dualize f ]]_v [[ dualize f1 ]]_v)) by min_max.
442 by induction hypothesis we know
443 ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
445 by induction hypothesis we know
446 ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
448 ([[ negate (FOr f f1) ]]_v=[[ FNot (dualize (FOr f f1)) ]]_v).
450 (max [[ negate f ]]_v [[ negate f1 ]]_v=[[ FNot (dualize (FOr f f1)) ]]_v).
452 (max [[ negate f ]]_v [[ negate f1 ]]_v)
453 = (max [[ FNot (dualize f) ]]_v [[ negate f1 ]]_v) by H.
454 = (max [[ FNot (dualize f) ]]_v [[ FNot (dualize f1) ]]_v) by H1.
455 = (max (1 - [[ dualize f ]]_v) (1 - [[ dualize f1 ]]_v)).
456 = (1 - (min [[ dualize f ]]_v [[ dualize f1 ]]_v)) by max_min.
460 by induction hypothesis we know
461 ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
463 by induction hypothesis we know
464 ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
466 ([[ negate (FImpl f f1) ]]_v=[[ FNot (dualize (FImpl f f1)) ]]_v).
468 (max (1 - [[ negate f ]]_v) [[ negate f1 ]]_v=[[ FNot (dualize (FImpl f f1)) ]]_v).
470 (max (1-[[ negate f ]]_v) [[ negate f1 ]]_v)
471 = (max (1-[[ FNot (dualize f) ]]_v) [[ negate f1 ]]_v) by H.
472 = (max (1-[[ FNot (dualize f) ]]_v) [[ FNot (dualize f1) ]]_v) by H1.
473 = (max (1 - [[ FNot (dualize f) ]]_v) (1 - [[ dualize f1 ]]_v)).
474 = (1 - (min [[ FNot (dualize f) ]]_v [[ dualize f1 ]]_v)) by max_min.
478 by induction hypothesis we know
479 ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
481 ([[ negate (FNot f) ]]_v=[[ FNot (dualize (FNot f)) ]]_v).
483 (1 - [[ negate f ]]_v=[[ FNot (dualize (FNot f)) ]]_v).
484 conclude (1 - [[ negate f ]]_v) = (1 - [[ FNot (dualize f) ]]_v) by H.
491 Dimostrare che la negazione è iniettiva
494 ∀F,G:Formula.FNot F ≡ FNot G→F ≡ G.
497 suppose (FNot F ≡ FNot G) (H).
498 the thesis becomes (F ≡ G).
499 the thesis becomes (∀v:ℕ→ℕ.[[ F ]]_v=[[ G ]]_v).
501 by H we proved ([[ FNot F ]]_v=[[ FNot G ]]_v) (H1).
502 by sem_bool we proved ([[ F ]]_v=O∨[[ F ]]_v=1) (H2).
503 by sem_bool we proved ([[ G ]]_v=O∨[[ G ]]_v=1) (H3).
504 we proceed by cases on H2 to prove ([[ F ]]_v=[[ G ]]_v).
506 we proceed by cases on H3 to prove ([[ F ]]_v=[[ G ]]_v).
514 = (1 - [[G]]_v) by H5.
516 = [[ FNot F ]]_v by H1.
522 we proceed by cases on H3 to prove ([[ F ]]_v=[[ G ]]_v).
528 = (1 - [[G]]_v) by H5.
530 = [[ FNot F ]]_v by H1.
541 La prova del teorema di dualità
542 ===============================
544 Il teorema di dualità accennato a lezione dice che se due formule
545 `F1` ed `F2` sono equivalenti, allora anche le formule duali lo sono.
547 ∀F1,F2:Formula. F1 ≡ F2 → dualize F1 ≡ dualize F2.
549 Per dimostrare tale teorema è bene suddividere la prova in lemmi intermedi
551 1. lemma `negate_invert`, dimostrato per induzione su F, utilizzando
554 ∀F:Formula.∀v:ℕ→ℕ.[[ negate F ]]_v=[[ F ]]_(invert v).
556 2. lemma `negate_fun`, conseguenza di `negate_invert`
558 ∀F,G:Formula. F ≡ G → negate F ≡ negate G.
560 2. lemma `not_dualize_eq_negate`, dimostrato per induzione su F,
561 utilizzando `max_min` e `min_max`
563 ∀F:Formula. negate F ≡ FNot (dualize F)
565 4. lemma `not_inj`, conseguenza di `sem_bool`
567 ∀F,G:Formula. FNot F ≡ FNot G → F ≡ G
569 Una volta dimostrati tali lemmi la prova del teorema di dualità
570 procede come di seguito:
576 2. Utilizza `negate_fun` per ottenere
578 negate F1 ≡ negate F2
580 3. Utilizzando due volte il lemma `not_dualize_eq_negate` e il lemma
581 `equiv_rewrite` ottiene
583 FNot (dualize F1) ≡ FNot (dualize F2)
585 4. Conclude utilizzando il lemma `not_inj` per ottenere la tesi
587 dualize F1 ≡ dualize F2
594 Dimostrare il teorema di dualità
596 theorem duality: ∀F1,F2:Formula.F1 ≡ F2 → dualize F1 ≡ dualize F2.
599 suppose (F1 ≡ F2) (H).
600 the thesis becomes (dualize F1 ≡ dualize F2).
601 by negate_fun we proved (negate F1 ≡ negate F2) (H1).
602 by not_dualize_eq_negate, equiv_rewrite we proved (FNot (dualize F1) ≡ negate F2) (H2).
603 by not_dualize_eq_negate, equiv_rewrite we proved (FNot (dualize F1) ≡ FNot (dualize F2)) (H3).
604 by not_inj we proved (dualize F1 ≡ dualize F2) (H4).