1 (* Esercitazione di logica 29/10/2008.
5 http://www.cs.unibo.it/~tassi/exercise-duality.ma.html
12 Compilare i seguenti campi:
24 Prima di abbandonare la postazione:
26 * salvare il file (menu `File ▹ Save as ...`) nella directory (cartella)
27 /public/ con nome linguaggi_Account1.ma, ad esempio Mario Rossi, il cui
28 account è mrossi, deve salvare il file in /public/linguaggi_mrossi.ma
30 * mandatevi via email o stampate il file. Per stampare potete usare
31 usare l'editor gedit che offre la funzionalità di stampa
39 Il teorema di dualizzazione dice che date due formule `F1` ed `F2`,
40 se le due formule sono equivalenti (`F1 ≡ F2`) allora anche le
41 loro dualizzate lo sono (`dualize F1 ≡ dualize F2`).
43 L'ingrediente principale è la funzione di dualizzazione di una formula `F`:
45 * Scambia FTop con FBot e viceversa
47 * Scambia il connettivo FAnd con FOr e viceversa
49 * Sostituisce il connettivo FImpl con FAnd e nega la
52 Ad esempio la formula `A → (B ∧ ⊥)` viene dualizzata in
55 Per dimostrare il teorema di dualizzazione in modo agevole è necessario
56 definire altre nozioni:
58 * La funzione `negate` che presa una formula `F` ne nega gli atomi.
59 Ad esempio la formula `(A ∨ (⊤ → B))` deve diventare `¬A ∨ (⊤ → ¬B)`.
61 * La funzione `invert` permette di invertire un mondo `v`.
62 Ovvero, per ogni indice di atomo `i`, se `v i` restituisce
63 `1` allora `(invert v) i` restituisce `0` e viceversa.
70 Non modificare quanto segue
72 include "nat/minus.ma".
73 definition if_then_else ≝ λT:Type.λe,t,f.match e return λ_.T with [ true ⇒ t | false ⇒ f].
74 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 }.
75 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 }.
76 interpretation "Formula if_then_else" 'if_then_else e t f = (if_then_else _ e t f).
77 definition max ≝ λn,m. if eqb (n - m) 0 then m else n.
78 definition min ≝ λn,m. if eqb (n - m) 0 then n else m.
83 Il linguaggio delle formule, dove gli atomi sono
84 rapperesentati da un numero naturale
86 inductive Formula : Type ≝
89 | FAtom: nat → Formula
90 | FAnd: Formula → Formula → Formula
91 | FOr: Formula → Formula → Formula
92 | FImpl: Formula → Formula → Formula
93 | FNot: Formula → Formula
99 Modificare la funzione `sem` scritta nella precedente
100 esercitazione in modo che valga solo 0 oppure 1 nel caso degli
101 atomi, anche nel caso in cui il mondo `v` restituisca un numero
104 Suggerimento: non è necessario usare il costrutto if_then_else
105 e tantomento il predicato di maggiore o uguale. È invece possibile
106 usare la funzione `min`.
108 let rec sem (v: nat → nat) (F: Formula) on F : nat ≝
112 | FAtom n ⇒ (*BEGIN*)min (v n) 1(*END*)
113 | FAnd F1 F2 ⇒ min (sem v F1) (sem v F2)
114 | FOr F1 F2 ⇒ max (sem v F1) (sem v F2)
115 | FImpl F1 F2 ⇒ max (1 - sem v F1) (sem v F2)
116 | FNot F1 ⇒ 1 - (sem v F1)
123 Non modificare quanto segue.
125 notation < "[[ \nbsp term 19 a \nbsp ]] \nbsp \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
126 notation > "[[ term 19 a ]] \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
127 notation > "[[ term 19 a ]]_ term 90 v" non associative with precedence 90 for @{ sem $v $a }.
128 interpretation "Semantic of Formula" 'semantics v a = (sem v a).
132 else if eqb x 1 then 1
138 La semantica della formula `(A ∨ C)` nel mondo `v20` in cui
139 `A` vale `2` e `C` vale `0` deve valere `1`.
142 eval normalize on [[FOr (FAtom 0) (FAtom 2)]]_v20.
146 La libreria di Matita
147 =====================
149 Gli strumenti per la dimostrazione assistita sono corredati da
150 librerie di teoremi già dimostrati. Per portare a termine l'esercitazione
151 sono necessari i seguenti lemmi:
153 * lemma `sem_le_1` : `∀F,v. [[ F ]]_v ≤ 1`
154 * lemma `min_1_1` : `∀x. x ≤ 1 → 1 - (1 - x) = x`
155 * lemma `min_bool` : `∀n. min n 1 = 0 ∨ min n 1 = 1`
156 * lemma `min_max` : `∀F,G,v.min (1 - [[F]]_v) (1 - [[G]]_v) = 1 - max [[F]]_v [[G]]_v`
157 * lemma `max_min` : `∀F,G,v.max (1 - [[F]]_v) (1 - [[G]]_v) = 1 - min [[F]]_v [[G]]_v`
158 * lemma `equiv_rewrite` : `∀F1,F2,F3. F1 ≡ F2 → F1 ≡ F3 → F2 ≡ F3`
165 Non modificare quanto segue.
167 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.
168 lemma min_bool : ∀n. min n 1 = 0 ∨ min n 1 = 1. intros; cases n; [left;reflexivity] cases n1; right; reflexivity; qed.
169 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.
170 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.
171 lemma min_1_1 : ∀x.x ≤ 1 → 1 - (1 - x) = x. intros; inversion H; intros; destruct; [reflexivity;] rewrite < (le_n_O_to_eq ? H1); reflexivity;qed.
172 lemma sem_le_1 : ∀F,v.[[F]]_v ≤ 1. intros; cases (sem_bool F v); rewrite > H; [apply le_O_n|apply le_n]qed.
173 let rec subst (x:nat) (G: Formula) (F: Formula) on F ≝
176 | FTop ⇒ (*BEGIN*)FTop(*END*)
177 | FAtom n ⇒ if (*BEGIN*)eqb n x(*END*) then (*BEGIN*)G(*END*) else ((*BEGIN*)FAtom n(*END*))
179 | FAnd F1 F2 ⇒ FAnd (subst x G F1) (subst x G F2)
180 | FOr F1 F2 ⇒ FOr (subst x G F1) (subst x G F2)
181 | FImpl F1 F2 ⇒ FImpl (subst x G F1) (subst x G F2)
183 | FNot F ⇒ FNot (subst x G F)
186 notation < "t [ \nbsp term 19 a / term 19 b \nbsp ]" non associative with precedence 19 for @{ 'substitution $b $a $t }.
187 notation > "t [ term 90 a / term 90 b]" non associative with precedence 19 for @{ 'substitution $b $a $t }.
188 interpretation "Substitution for Formula" 'substitution b a t = (subst b a t).
189 definition equiv ≝ λF1,F2. ∀v.[[ F1 ]]_v = [[ F2 ]]_v.
190 notation "hvbox(a \nbsp break mstyle color #0000ff (≡) \nbsp b)" non associative with precedence 45 for @{ 'equivF $a $b }.
191 notation > "a ≡ b" non associative with precedence 50 for @{ equiv $a $b }.
192 interpretation "equivalence for Formulas" 'equivF a b = (equiv a b).
195 ∀F,x,v. [[ if eqb [[FAtom x]]_v 0 then F[FBot/x] else (F[FTop/x]) ]]_v = [[F]]_v.
197 [1,2: cases (eqb [[FAtom x]]_v 0); reflexivity;
198 |4,5,6: cases (eqb [[FAtom x]]_v 0) in H H1; simplify; intros; rewrite > H; rewrite > H1; reflexivity;
199 |7: cases (eqb [[FAtom x]]_v 0) in H; simplify; intros; rewrite > H; reflexivity;
200 | cases (sem_bool (FAtom x) v); rewrite > H; simplify;
201 cases (decidable_eq_nat n x); destruct H1;
202 [1,3: rewrite > eqb_n_n; simplify; rewrite >H;reflexivity;.
203 |*:simplify in H; rewrite > (not_eq_to_eqb_false ?? H1); simplify; reflexivity;]]