]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/didactic/shannon.ma
ccbaf956d95a42c3105edf22adf8c5044bb5b5ca
[helm.git] / helm / software / matita / contribs / didactic / shannon.ma
1 (* Esercitazione di logica 29/10/2008. 
2
3    Note per gli esercizi: 
4    
5      http://www.cs.unibo.it/~tassi/exercise-duality.ma.html
6
7 *)
8
9 (* Esercizio 0
10    ===========
11
12    Compilare i seguenti campi:
13
14    Nome1: ...
15    Cognome1: ...
16    Matricola1: ...
17    Account1: ...
18
19    Nome2: ...
20    Cognome2: ...
21    Matricola2: ...
22    Account2: ...
23
24    Prima di abbandonare la postazione:
25
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
29
30    * mandatevi via email o stampate il file. Per stampare potete usare
31      usare l'editor gedit che offre la funzionalità di stampa
32 *)
33
34 (*DOCBEGIN
35
36 Il teorema di dualità
37 =====================
38
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`).
42
43 L'ingrediente principale è la funzione di dualizzazione di una formula `F`:
44    
45    * Scambia FTop con FBot e viceversa
46    
47    * Scambia il connettivo FAnd con FOr e viceversa
48    
49    * Sostituisce il connettivo FImpl con FAnd e nega la
50      prima sottoformula.
51    
52    Ad esempio la formula `A → (B ∧ ⊥)` viene dualizzata in
53    `¬A ∧ (B ∨ ⊤)`.
54
55 Per dimostrare il teorema di dualizzazione in modo agevole è necessario
56 definire altre nozioni:
57
58 * La funzione `negate` che presa una formula `F` ne nega gli atomi.
59   Ad esempio la formula `(A ∨ (⊤ → B))` deve diventare `¬A ∨ (⊤ → ¬B)`.
60    
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.
64    
65 DOCEND*)
66
67 (* ATTENZIONE
68    ==========
69    
70    Non modificare quanto segue 
71 *)
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. 
79
80 (* Ripasso
81    =======
82    
83    Il linguaggio delle formule, dove gli atomi sono
84    rapperesentati da un numero naturale
85 *)
86 inductive Formula : Type ≝
87 | FBot: Formula
88 | FTop: Formula
89 | FAtom: nat → Formula
90 | FAnd: Formula → Formula → Formula
91 | FOr: Formula → Formula → Formula
92 | FImpl: Formula → Formula → Formula
93 | FNot: Formula → Formula
94 .
95
96 (* Esercizio 1
97    ===========
98    
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
102    maggiore di 1.
103    
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`.
107 *) 
108 let rec sem (v: nat → nat) (F: Formula) on F : nat ≝
109  match F with
110   [ FBot ⇒ 0
111   | FTop ⇒ 1
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)
117   ]
118 .
119
120 (* ATTENZIONE
121    ==========
122    
123    Non modificare quanto segue.
124 *)
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).
129
130 definition v20 ≝ λx.
131        if eqb x 0 then 2
132   else if eqb x 1 then 1
133   else                 0.
134   
135 (* Test 1
136    ======
137    
138    La semantica della formula `(A ∨ C)` nel mondo `v20` in cui 
139    `A` vale `2` e `C` vale `0` deve valere `1`.
140    
141 *)    
142 eval normalize on [[FOr (FAtom 0) (FAtom 2)]]_v20. 
143
144 (*DOCBEGIN
145
146 La libreria di Matita
147 =====================
148
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:
152
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`
159
160 DOCEND*)
161
162 (* ATTENZIONE
163    ==========
164    
165    Non modificare quanto segue.
166 *)
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 ≝
174  match F with
175   [ FBot ⇒ FBot
176   | FTop ⇒ (*BEGIN*)FTop(*END*)
177   | FAtom n ⇒ if (*BEGIN*)eqb n x(*END*) then (*BEGIN*)G(*END*) else ((*BEGIN*)FAtom n(*END*))
178   (*BEGIN*)
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)
182   (*END*)
183   | FNot F ⇒ FNot (subst x G F)
184   ].
185   
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).
193
194 theorem shannon : 
195   ∀F,x,v. [[ if eqb [[FAtom x]]_v 0 then F[FBot/x] else (F[FTop/x]) ]]_v = [[F]]_v. 
196 intros; elim F;
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;]]
204 qed.
205
206   
207
208