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
31 Non modificare quanto segue
33 include "nat/minus.ma".
34 definition if_then_else ≝ λT:Type.λe,t,f.match e return λ_.T with [ true ⇒ t | false ⇒ f].
35 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 }.
36 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 }.
37 interpretation "Formula if_then_else" 'if_then_else e t f = (if_then_else _ e t f).
38 definition max ≝ λn,m. if eqb (n - m) 0 then m else n.
39 definition min ≝ λn,m. if eqb (n - m) 0 then n else m.
44 Il linguaggio delle formule, dove gli atomi sono
45 rapperesentati da un numero naturale
47 inductive Formula : Type ≝
50 | FAtom: nat → Formula
51 | FAnd: Formula → Formula → Formula
52 | FOr: Formula → Formula → Formula
53 | FImpl: Formula → Formula → Formula
54 | FNot: Formula → Formula
60 Modificare la funzione `sem` scritta nella precedente
61 esercitazione in modo che valga solo 0 oppure 1 nel caso degli
62 atomi, anche nel caso il mondo `v` restituisca un numero
65 let rec sem (v: nat → nat) (F: Formula) on F ≝
69 | FAtom n ⇒ (*BEGIN*)min (v n) 1(*END*)
70 | FAnd F1 F2 ⇒ min (sem v F1) (sem v F2)
71 | FOr F1 F2 ⇒ max (sem v F1) (sem v F2)
72 | FImpl F1 F2 ⇒ max (1 - sem v F1) (sem v F2)
73 | FNot F1 ⇒ 1 - (sem v F1)
80 Non modificare quanto segue.
82 notation < "[[ \nbsp term 19 a \nbsp ]] \nbsp \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
83 notation > "[[ term 19 a ]] \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
84 notation > "[[ term 19 a ]]_ term 90 v" non associative with precedence 90 for @{ sem $v $a }.
85 interpretation "Semantic of Formula" 'semantics v a = (sem v a).
89 else if eqb x 1 then 1
95 La semantica della formula `(A ∨ C)` nel mondo `v20` in cui
96 `A` vale `2` e `C` vale `0` deve valere `1`.
99 eval normalize on [[For (FAtom 0) (FAtom 2)]]_v20.
105 Non modificare quanto segue.
107 lemma sem_bool : ∀F,v. [[ F ]]_v = 0 ∨ [[ F ]]_v = 1.
108 intros; elim F; simplify;
111 |cases (v n);[left;|cases n1;right;]reflexivity;
112 |4,5,6: cases H; cases H1; rewrite > H2; rewrite > H3; simplify;
113 first [ left;reflexivity | right; reflexivity ].
114 |cases H; rewrite > H1; simplify;[right|left]reflexivity;]
116 lemma min_bool : ∀n. min n 1 = 0 ∨ min n 1 = 1.
117 intros; cases n; [left;reflexivity] cases n1; right; reflexivity;
119 lemma min_max : ∀F,G,v.
120 min (1 - [[F]]_v) (1 - [[G]]_v) = 1 - max [[F]]_v [[G]]_v.
121 intros; cases (sem_bool F v);cases (sem_bool G v); rewrite > H; rewrite >H1;
122 simplify; reflexivity;
124 lemma max_min : ∀F,G,v.
125 max (1 - [[F]]_v) (1 - [[G]]_v) = 1 - min [[F]]_v [[G]]_v.
126 intros; cases (sem_bool F v);cases (sem_bool G v); rewrite > H; rewrite >H1;
127 simplify; reflexivity;
133 Definire per ricorsione strutturale la funzione `negate`
134 che presa una formula `F` ne nega gli atomi.
136 Ad esempio la formula `(A ∨ (⊤ → B))` deve diventare
139 let rec negate (F: Formula) on F ≝
143 | FAtom n ⇒ FNot (FAtom n)
144 | FAnd F1 F2 ⇒ FAnd (negate F1) (negate F2)
145 | FOr F1 F2 ⇒ FOr (negate F1) (negate F2)
146 | FImpl F1 F2 ⇒ FImpl (negate F1) (negate F2)
147 | FNot F ⇒ FNot (negate F)
153 Testare la funzione `negate`. Il risultato atteso è:
155 FOr (FNot (FAtom O)) (FImpl FTop (FNot (FAtom 1)))
158 eval normalize on (negate (FOr (FAtom 0) (FImpl FTop (FAtom 1)))).
163 Non modificare quanto segue
165 definition equiv ≝ λF1,F2. ∀v.[[ F1 ]]_v = [[ F2 ]]_v.
166 notation "hvbox(a \nbsp break mstyle color #0000ff (≡) \nbsp b)" non associative with precedence 45 for @{ 'equivF $a $b }.
167 notation > "a ≡ b" non associative with precedence 50 for @{ equiv $a $b }.
168 interpretation "equivalence for Formulas" 'equivF a b = (equiv a b).
169 lemma equiv_rewrite : ∀F1,F2,F3. F1 ≡ F2 → F1 ≡ F3 → F2 ≡ F3. intros; intro; autobatch. qed.
174 Definire per ricorsione strutturale la funzione di
175 dualizzazione di una formula `F`. Tale funzione:
177 * Sambia FTop con FBot e viceversa
179 * Scambia il connettivo FAnd con FOr e viceversa
181 * Sostituisce il connettivo FImpl con FAnd e nega la
184 Ad esempio la formula `A → (B ∧ ⊥)` viene dualizzata in
187 let rec dualize (F : Formula) on F : Formula ≝
192 | FAnd F1 F2 ⇒ FOr (dualize F1) (dualize F2)
193 | FOr F1 F2 ⇒ FAnd (dualize F1) (dualize F2)
194 | FImpl F1 F2 ⇒ FAnd (FNot (dualize F1)) (dualize F2)
195 | FNot F ⇒ FNot (dualize F)
201 Testare la funzione `dualize`. Il risultato atteso è:
203 FAnd (FNot (FAtom O)) (FOr (FAtom 1) FTop)
206 eval normalize on (dualize (FImpl (FAtom 0) (FAnd (FAtom 1) FBot))).
211 La funzione `invert` permette di invertire un mondo `v`.
212 Ovvero, per ogni indice di atomo `i`, se `v i` restituisce
213 `1` allora `(invert v) i` restituisce `0` e viceversa.
216 λv:ℕ -> ℕ. λx. if eqb (min (v x) 1) 0 then 1 else 0.
218 interpretation "Inversione del mondo" 'invert v = (invert v).
222 Il linguaggio di dimostrazione di Matita
223 ========================================
225 Per dimostrare il lemma `negate_invert` in modo agevole è necessario
226 utilizzare il seguente comando:
230 Quando la conclusuine è `a = b` permette di cambiarla in `b = a`.
237 Dimostrare il lemma `negate_invert` che asserisce che
238 la semantica in un mondo `v` associato alla formula
239 negata di `F` e uguale alla semantica associata
240 a `F` in un mondo invertito.
243 ∀F:Formula.∀v:ℕ→ℕ.[[ negate F ]]_v=[[ F ]]_(invert v).
246 we proceed by induction on F to prove ([[ negate F ]]_v=[[ F ]]_(invert v)).
248 the thesis becomes ([[ negate FBot ]]_v=[[ FBot ]]_(invert v)).
251 the thesis becomes ([[ negate FTop ]]_v=[[ FTop ]]_(invert v)).
255 the thesis becomes ([[ negate (FAtom n) ]]_v=[[ FAtom n ]]_(invert v)).
256 the thesis becomes (1 - (min (v n) 1)= min (invert v n) 1).
257 the thesis becomes (1 - (min (v n) 1)= min (if eqb (min (v n) 1) 0 then 1 else 0) 1).
258 by min_bool we proved (min (v n) 1 = 0 ∨ min (v n) 1 = 1) (H1);
259 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).
267 (min (if eqb (min (v n) 1) O then 1 else O) 1)
268 = (min (if eqb 0 0 then 1 else O) 1) by H.
279 (min (if eqb (min (v n) 1) O then 1 else O) 1)
280 = (min (if eqb 1 0 then 1 else O) 1) by H.
286 by induction hypothesis we know
287 ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
289 by induction hypothesis we know
290 ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
292 ([[ negate (FAnd f f1) ]]_v=[[ FAnd f f1 ]]_(invert v)).
294 (min [[ negate f ]]_v [[ negate f1]]_v = [[ FAnd f f1 ]]_(invert v)).
296 (min [[ negate f ]]_v [[ negate f1]]_v)
297 = (min [[ f ]]_(invert v) [[ negate f1]]_v) by H.
298 = (min [[ f ]]_(invert v) [[ f1]]_(invert v)) by H1.
302 by induction hypothesis we know
303 ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
305 by induction hypothesis we know
306 ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
308 ([[ negate (FOr f f1) ]]_v=[[ FOr f f1 ]]_(invert v)).
310 (max [[ negate f ]]_v [[ negate f1]]_v = [[ FOr f f1 ]]_(invert v)).
312 (max [[ negate f ]]_v [[ negate f1]]_v)
313 = (max [[ f ]]_(invert v) [[ negate f1]]_v) by H.
314 = (max [[ f ]]_(invert v) [[ f1]]_(invert v)) by H1.
318 by induction hypothesis we know
319 ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
321 by induction hypothesis we know
322 ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
324 ([[ negate (FImpl f f1) ]]_v=[[ FImpl f f1 ]]_(invert v)).
326 (max (1 - [[ negate f ]]_v) [[ negate f1]]_v = [[ FImpl f f1 ]]_(invert v)).
328 (max (1 - [[ negate f ]]_v) [[ negate f1]]_v)
329 = (max (1 - [[ f ]]_(invert v)) [[ negate f1]]_v) by H.
330 = (max (1 - [[ f ]]_(invert v)) [[ f1]]_(invert v)) by H1.
334 by induction hypothesis we know
335 ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
337 ([[ negate (FNot f) ]]_v=[[ FNot f ]]_(invert v)).
339 (1 - [[ negate f ]]_v=[[ FNot f ]]_(invert v)).
340 conclude (1 - [[ negate f ]]_v) = (1 - [[f]]_(invert v)) by H.
347 Dimostrare che la funzione negate rispetta l'equivalenza.
350 ∀F:Formula.∀G:Formula.F ≡ G→negate F ≡ negate G.
354 the thesis becomes (negate F ≡ negate G).
355 the thesis becomes (∀v:ℕ→ℕ.[[ negate F ]]_v=[[ negate G ]]_v).
359 = [[ F ]]_(invert v) by negate_invert.
360 = [[ G ]]_(invert v) by H.
361 = [[ negate G ]]_v by negate_invert.
368 Dimostrare che per ogni formula `F`, `(negae F)` equivale a
369 dualizzarla e negarla.
371 lemma not_dualize_eq_negate:
372 ∀F:Formula.negate F ≡ FNot (dualize F).
374 the thesis becomes (∀v:ℕ→ℕ.[[negate F]]_v=[[FNot (dualize F)]]_v).
376 we proceed by induction on F to prove ([[negate F]]_v=[[FNot (dualize F)]]_v).
378 the thesis becomes ([[ negate FBot ]]_v=[[ FNot (dualize FBot) ]]_v).
381 the thesis becomes ([[ negate FTop ]]_v=[[ FNot (dualize FTop) ]]_v).
385 the thesis becomes ([[ negate (FAtom n) ]]_v=[[ FNot (dualize (FAtom n)) ]]_v).
389 by induction hypothesis we know
390 ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
392 by induction hypothesis we know
393 ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
395 ([[ negate (FAnd f f1) ]]_v=[[ FNot (dualize (FAnd f f1)) ]]_v).
397 (min [[ negate f ]]_v [[ negate f1 ]]_v=[[ FNot (dualize (FAnd f f1)) ]]_v).
399 (min [[ negate f ]]_v [[ negate f1 ]]_v)
400 = (min [[ FNot (dualize f) ]]_v [[ negate f1 ]]_v) by H.
401 = (min [[ FNot (dualize f) ]]_v [[ FNot (dualize f1) ]]_v) by H1.
402 = (min (1 - [[ dualize f ]]_v) (1 - [[ dualize f1 ]]_v)).
403 = (1 - (max [[ dualize f ]]_v [[ dualize f1 ]]_v)) by min_max.
407 by induction hypothesis we know
408 ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
410 by induction hypothesis we know
411 ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
413 ([[ negate (FOr f f1) ]]_v=[[ FNot (dualize (FOr f f1)) ]]_v).
415 (max [[ negate f ]]_v [[ negate f1 ]]_v=[[ FNot (dualize (FOr f f1)) ]]_v).
417 (max [[ negate f ]]_v [[ negate f1 ]]_v)
418 = (max [[ FNot (dualize f) ]]_v [[ negate f1 ]]_v) by H.
419 = (max [[ FNot (dualize f) ]]_v [[ FNot (dualize f1) ]]_v) by H1.
420 = (max (1 - [[ dualize f ]]_v) (1 - [[ dualize f1 ]]_v)).
421 = (1 - (min [[ dualize f ]]_v [[ dualize f1 ]]_v)) by max_min.
425 by induction hypothesis we know
426 ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
428 by induction hypothesis we know
429 ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
431 ([[ negate (FImpl f f1) ]]_v=[[ FNot (dualize (FImpl f f1)) ]]_v).
433 (max (1 - [[ negate f ]]_v) [[ negate f1 ]]_v=[[ FNot (dualize (FImpl f f1)) ]]_v).
435 (max (1-[[ negate f ]]_v) [[ negate f1 ]]_v)
436 = (max (1-[[ FNot (dualize f) ]]_v) [[ negate f1 ]]_v) by H.
437 = (max (1-[[ FNot (dualize f) ]]_v) [[ FNot (dualize f1) ]]_v) by H1.
438 = (max (1 - [[ FNot (dualize f) ]]_v) (1 - [[ dualize f1 ]]_v)).
439 = (1 - (min [[ FNot (dualize f) ]]_v [[ dualize f1 ]]_v)) by max_min.
443 by induction hypothesis we know
444 ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
446 ([[ negate (FNot f) ]]_v=[[ FNot (dualize (FNot f)) ]]_v).
448 (1 - [[ negate f ]]_v=[[ FNot (dualize (FNot f)) ]]_v).
449 conclude (1 - [[ negate f ]]_v) = (1 - [[ FNot (dualize f) ]]_v) by H.
456 Dimostrare che la negazione è iniettiva
459 ∀F:Formula.∀G:Formula.FNot F ≡ FNot G→F ≡ G.
462 suppose (FNot F ≡ FNot G) (H).
463 the thesis becomes (F ≡ G).
464 the thesis becomes (∀v:ℕ→ℕ.[[ F ]]_v=[[ G ]]_v).
466 by H we proved ([[ FNot F ]]_v=[[ FNot G ]]_v) (H1).
467 by sem_bool we proved ([[ F ]]_v=O∨[[ F ]]_v=1) (H2).
468 by sem_bool we proved ([[ G ]]_v=O∨[[ G ]]_v=1) (H3).
469 we proceed by cases on H2 to prove ([[ F ]]_v=[[ G ]]_v).
471 we proceed by cases on H3 to prove ([[ F ]]_v=[[ G ]]_v).
479 = (1 - [[G]]_v) by H5.
481 = [[ FNot F ]]_v by H1.
487 we proceed by cases on H3 to prove ([[ F ]]_v=[[ G ]]_v).
493 = (1 - [[G]]_v) by H5.
495 = [[ FNot F ]]_v by H1.
507 Dimostrare il teorema di dualità
510 ∀F1:Formula.∀F2:Formula.F1 ≡ F2 → dualize F1 ≡ dualize F2.
513 suppose (F1 ≡ F2) (H).
514 the thesis becomes (dualize F1 ≡ dualize F2).
515 by negate_fun we proved (negate F1 ≡ negate F2) (H1).
516 by not_dualize_eq_negate, equiv_rewrite we proved (FNot (dualize F1) ≡ negate F2) (H2).
517 by not_dualize_eq_negate, equiv_rewrite we proved (FNot (dualize F1) ≡ FNot (dualize F2)) (H3).
518 by not_inj we proved (dualize F1 ≡ dualize F2) (H4).