+lemma min_1_sem: ∀F,v.min 1 [[ F ]]_v = [[ F ]]_v. intros; cases (sem_bool F v); rewrite > H; reflexivity; qed.
+lemma max_0_sem: ∀F,v.max [[ F ]]_v 0 = [[ F ]]_v. intros; cases (sem_bool F v); rewrite > H; reflexivity; qed.
+definition IFTE := λA,B,C:Formula. FOr (FAnd A B) (FAnd (FNot A) C).
+
+(*DOCBEGIN
+
+La libreria di Matita
+=====================
+
+Per portare a termine l'esercitazione sono necessari i seguenti lemmi:
+
+* lemma `decidable_eq_nat` : `∀x,y.x = y ∨ x ≠ y`
+* lemma `sem_bool` : `∀F,v. [[ F ]]_v = 0 ∨ [[ F ]]_v = 1`
+* lemma `not_eq_to_eqb_false` : `∀x,y.x ≠ y → eqb x y = false`
+* lemma `eq_to_eqb_true` : `∀x,y.x = y → eqb x y = true`
+* lemma `min_1_sem` : `∀F,v.min 1 [[ F ]]_v = [[ F ]]_v`
+* lemma `max_0_sem` : `∀F,v.max [[ F ]]_v 0 = [[ F ]]_v`
+
+Nota su `x = y` e `eqb x y`
+---------------------------
+
+Se vi siete mai chiesti la differenza tra `x = y` ed `eqb x y`
+quanto segue prova a chiarirla.
+
+Presi due numeri `x` e `y` in ℕ, dire che `x = y` significa i due numeri
+sono lo stesso numero, ovvero che se `x` è il numero `3`,
+anche `y` è il numero `3`.
+
+`eqb` è un funzione, un programma, che confronta due numeri naturali
+e restituisce `true` se sono uguali, `false` se sono diversi. L'utilizzo
+di tale programma è necessario per usare il costrutto (che è a sua volta
+un programma) `if E then A else B`, che lancia il programma `E`,
+e se il suo
+risultato è `true` si comporta come `A` altrimenti come `B`. Come
+ben sapete i programmi possono contenere errori. In particolare anche
+`eqb` potrebbe essere sbagliato, e per esempio restituire sempre `true`.
+I teoremi `eq_to_eqb_true` e
+`not_eq_to_eqb_false` sono la dimostrazione che il programma `eqb` è
+corretto, ovvero che che se `x = y` allora `eqb x y` restituisce `true`,
+se `x ≠ y` allora `eqb x y` restituisce `false`.
+
+Il teorema di espansione di Shannon
+===================================
+
+Si definisce un connettivo logico `IFTE A B C` come
+
+ FOr (FAnd A B) (FAnd (FNot A) C)
+
+Il teorema dice che data una formula `F`, e preso un atomo `x`, la seguente
+formula è equivalente a `F`:
+
+ IFTE (FAtom x) (F[FTop/x]) (F[FBot/x])
+
+Ovvero, fissato un mondo `v`, sostituisco l'atomo `x` con `FBot` se tale
+atomo è falso, lo sostituisco con `FTop` se è vero.
+
+La dimostrazione è composta da due lemmi, `shannon_false` e `shannon_true`.
+
+Vediamo solo la dimostrazione del primo, essendo il secondo del tutto analogo.
+Il lemma asserisce quanto segue:
+
+ ∀F,x,v. [[ FAtom x ]]_v = 0 → [[ F[FBot/x] ]]_v = [[ F ]]_v
+
+Una volta assunte la formula `F`, l'atomo `x`, il mondo `v` e aver
+supposto che `[[ FAtom x ]]_v = 0` si procede per induzione su `F`.
+I casi `FTop` e `FBot` sono banali. Nei casi `FAnd/FOr/FImpl/FNot`,
+una volta assunte le sottoformule e le relative ipotesi induttive,
+si conclude con una catena di uguaglianze.
+
+Il caso `FAtom` richiede maggiore cura. Assunto l'indice dell'atomo `n`,
+occorre utilizzare il lemma `decidable_eq_nat` per ottenere l'ipotesi
+aggiuntiva `n = x ∨ n ≠ x` (dove `x` è l'atomo su cui predica il teorema).
+Si procede per casi sull'ipotesi appena ottenuta.
+In entrambi i casi, usando i lemmi `eq_to_eqb_true` oppure `not_eq_to_eqb_false`
+si ottengolo le ipotesi aggiuntive `(eqb n x = true)` oppure `(eqb n x = false)`.
+Entrambi i casi si concludono con una catena di uguaglianze.
+
+Il teorema principale si dimostra utilizzando il lemma `sem_bool` per
+ottenre l'ipotesi `[[ FAtom x ]]_v = 0 ∨ [[ FAtom x ]]_v = 1` su cui
+si procede poi per casi. Entrambi i casi si concludono con
+una catena di uguaglianze che utilizza i lemmi dimostrati in precedenza
+e i lemmi `min_1_sem` oppure `max_0_sem`.
+
+DOCEND*)
+
+lemma shannon_false:
+ ∀F,x,v. [[ FAtom x ]]_v = 0 → [[ F[FBot/x] ]]_v = [[ F ]]_v.
+(*BEGIN*)
+assume F : Formula.
+assume x : ℕ.
+assume v : (ℕ → ℕ).
+suppose ([[ FAtom x ]]_v = 0) (H).
+we proceed by induction on F to prove ([[ F[FBot/x] ]]_v = [[ F ]]_v).
+case FBot.
+ the thesis becomes ([[ FBot[FBot/x] ]]_v = [[ FBot ]]_v).
+ the thesis becomes ([[ FBot ]]_v = [[ FBot ]]_v).
+ done.
+case FTop.
+ the thesis becomes ([[ FTop[FBot/x] ]]_v = [[ FTop ]]_v).
+ the thesis becomes ([[ FTop ]]_v = [[ FTop ]]_v).
+ done.
+case FAtom.
+ assume n : ℕ.
+ the thesis becomes ([[ (FAtom n)[FBot/x] ]]_v = [[ FAtom n ]]_v).
+ the thesis becomes ([[ if eqb n x then FBot else (FAtom n) ]]_v = [[ FAtom n ]]_v).
+ by decidable_eq_nat we proved (n = x ∨ n ≠ x) (H1).
+ we proceed by cases on H1 to prove ([[ if eqb n x then FBot else (FAtom n) ]]_v = [[ FAtom n ]]_v).
+ case Left.
+ by H2, eq_to_eqb_true we proved (eqb n x = true) (H3).
+ conclude
+ ([[ if eqb n x then FBot else (FAtom n) ]]_v)
+ = ([[ if true then FBot else (FAtom n) ]]_v) by H3.
+ = ([[ FBot ]]_v).
+ = 0.
+ = ([[ FAtom x ]]_v) by H.
+ = ([[ FAtom n ]]_v) by H2.
+ done.
+ case Right.
+ by H2, not_eq_to_eqb_false we proved (eqb n x = false) (H3).
+ conclude
+ ([[ if eqb n x then FBot else (FAtom n) ]]_v)
+ = ([[ if false then FBot else (FAtom n) ]]_v) by H3.
+ = ([[ FAtom n ]]_v).
+ done.
+case FAnd.
+ assume f1 : Formula.
+ by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
+ assume f2 : Formula.
+ by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
+ the thesis becomes ([[ (FAnd f1 f2)[FBot/x] ]]_v = [[ FAnd f1 f2 ]]_v).
+ conclude
+ ([[ (FAnd f1 f2)[FBot/x] ]]_v)
+ = ([[ FAnd (f1[FBot/x]) (f2[FBot/x]) ]]_v).
+ = (min [[ f1[FBot/x] ]]_v [[ f2[FBot/x] ]]_v).
+ = (min [[ f1 ]]_v [[ f2[FBot/x] ]]_v) by H1.
+ = (min [[ f1 ]]_v [[ f2 ]]_v) by H2.
+ = ([[ FAnd f1 f2 ]]_v).
+ done.
+case FOr.
+ assume f1 : Formula.
+ by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
+ assume f2 : Formula.
+ by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
+ the thesis becomes ([[ (FOr f1 f2)[FBot/x] ]]_v = [[ FOr f1 f2 ]]_v).
+ conclude
+ ([[ (FOr f1 f2)[FBot/x] ]]_v)
+ = ([[ FOr (f1[FBot/x]) (f2[FBot/x]) ]]_v).
+ = (max [[ f1[FBot/x] ]]_v [[ f2[FBot/x] ]]_v).
+ = (max [[ f1 ]]_v [[ f2[FBot/x] ]]_v) by H1.
+ = (max [[ f1 ]]_v [[ f2 ]]_v) by H2.
+ = ([[ FOr f1 f2 ]]_v).
+ done.
+case FImpl.
+ assume f1 : Formula.
+ by induction hypothesis we know ([[ f1[FBot/x] ]]_v = [[ f1 ]]_v) (H1).
+ assume f2 : Formula.
+ by induction hypothesis we know ([[ f2[FBot/x] ]]_v = [[ f2 ]]_v) (H2).
+ the thesis becomes ([[ (FImpl f1 f2)[FBot/x] ]]_v = [[ FImpl f1 f2 ]]_v).
+ conclude
+ ([[ (FImpl f1 f2)[FBot/x] ]]_v)
+ = ([[ FImpl (f1[FBot/x]) (f2[FBot/x]) ]]_v).
+ = (max (1 - [[ f1[FBot/x] ]]_v) [[ f2[FBot/x] ]]_v).
+ = (max (1 - [[ f1 ]]_v) [[ f2[FBot/x] ]]_v) by H1.
+ = (max (1 - [[ f1 ]]_v) [[ f2 ]]_v) by H2.
+ = ([[ FImpl f1 f2 ]]_v).
+ done.
+case FNot.
+ assume f : Formula.
+ by induction hypothesis we know ([[ f[FBot/x] ]]_v = [[ f ]]_v) (H1).
+ the thesis becomes ([[ (FNot f)[FBot/x] ]]_v = [[ FNot f ]]_v).
+ conclude
+ ([[ (FNot f)[FBot/x] ]]_v)
+ = ([[ FNot (f[FBot/x]) ]]_v).
+ = (1 - [[ f[FBot/x] ]]_v).
+ = (1 - [[ f ]]_v) by H1.
+ = ([[ FNot f ]]_v).
+ done.
+(*END*)
+qed.
+
+lemma shannon_true:
+ ∀F,x,v. [[ FAtom x ]]_v = 1 → [[ F[FTop/x] ]]_v = [[ F ]]_v.
+(*BEGIN*)
+assume F : Formula.
+assume x : ℕ.
+assume v : (ℕ → ℕ).
+suppose ([[ FAtom x ]]_v = 1) (H).
+we proceed by induction on F to prove ([[ F[FTop/x] ]]_v = [[ F ]]_v).
+case FBot.
+ the thesis becomes ([[ FBot[FTop/x] ]]_v = [[ FBot ]]_v).
+ the thesis becomes ([[ FBot ]]_v = [[ FBot ]]_v).
+ done.
+case FTop.
+ the thesis becomes ([[ FTop[FTop/x] ]]_v = [[ FTop ]]_v).
+ the thesis becomes ([[ FTop ]]_v = [[ FTop ]]_v).
+ done.
+case FAtom.
+ assume n : ℕ.
+ the thesis becomes ([[ (FAtom n)[FTop/x] ]]_v = [[ FAtom n ]]_v).
+ the thesis becomes ([[ if eqb n x then FTop else (FAtom n) ]]_v = [[ FAtom n ]]_v).
+ by decidable_eq_nat we proved (n = x ∨ n ≠ x) (H1).
+ we proceed by cases on H1 to prove ([[ if eqb n x then FTop else (FAtom n) ]]_v = [[ FAtom n ]]_v).
+ case Left.
+ by H2, eq_to_eqb_true we proved (eqb n x = true) (H3).
+ conclude
+ ([[ if eqb n x then FTop else (FAtom n) ]]_v)
+ = ([[ if true then FTop else (FAtom n) ]]_v) by H3.
+ = ([[ FTop ]]_v).
+ = 1.
+ = ([[ FAtom x ]]_v) by H.
+ = ([[ FAtom n ]]_v) by H2.
+ done.
+ case Right.
+ by H2, not_eq_to_eqb_false we proved (eqb n x = false) (H3).
+ conclude
+ ([[ if eqb n x then FTop else (FAtom n) ]]_v)
+ = ([[ if false then FTop else (FAtom n) ]]_v) by H3.
+ = ([[ FAtom n ]]_v).
+ done.
+case FAnd.
+ assume f1 : Formula.
+ by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
+ assume f2 : Formula.
+ by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
+ the thesis becomes ([[ (FAnd f1 f2)[FTop/x] ]]_v = [[ FAnd f1 f2 ]]_v).
+ conclude
+ ([[ (FAnd f1 f2)[FTop/x] ]]_v)
+ = ([[ FAnd (f1[FTop/x]) (f2[FTop/x]) ]]_v).
+ = (min [[ f1[FTop/x] ]]_v [[ f2[FTop/x] ]]_v).
+ = (min [[ f1 ]]_v [[ f2[FTop/x] ]]_v) by H1.
+ = (min [[ f1 ]]_v [[ f2 ]]_v) by H2.
+ = ([[ FAnd f1 f2 ]]_v).
+ done.
+case FOr.
+ assume f1 : Formula.
+ by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
+ assume f2 : Formula.
+ by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
+ the thesis becomes ([[ (FOr f1 f2)[FTop/x] ]]_v = [[ FOr f1 f2 ]]_v).
+ conclude
+ ([[ (FOr f1 f2)[FTop/x] ]]_v)
+ = ([[ FOr (f1[FTop/x]) (f2[FTop/x]) ]]_v).
+ = (max [[ f1[FTop/x] ]]_v [[ f2[FTop/x] ]]_v).
+ = (max [[ f1 ]]_v [[ f2[FTop/x] ]]_v) by H1.
+ = (max [[ f1 ]]_v [[ f2 ]]_v) by H2.
+ = ([[ FOr f1 f2 ]]_v).
+ done.
+case FImpl.
+ assume f1 : Formula.
+ by induction hypothesis we know ([[ f1[FTop/x] ]]_v = [[ f1 ]]_v) (H1).
+ assume f2 : Formula.
+ by induction hypothesis we know ([[ f2[FTop/x] ]]_v = [[ f2 ]]_v) (H2).
+ the thesis becomes ([[ (FImpl f1 f2)[FTop/x] ]]_v = [[ FImpl f1 f2 ]]_v).
+ conclude
+ ([[ (FImpl f1 f2)[FTop/x] ]]_v)
+ = ([[ FImpl (f1[FTop/x]) (f2[FTop/x]) ]]_v).
+ = (max (1 - [[ f1[FTop/x] ]]_v) [[ f2[FTop/x] ]]_v).
+ = (max (1 - [[ f1 ]]_v) [[ f2[FTop/x] ]]_v) by H1.
+ = (max (1 - [[ f1 ]]_v) [[ f2 ]]_v) by H2.
+ = ([[ FImpl f1 f2 ]]_v).
+ done.
+case FNot.
+ assume f : Formula.
+ by induction hypothesis we know ([[ f[FTop/x] ]]_v = [[ f ]]_v) (H1).
+ the thesis becomes ([[ (FNot f)[FTop/x] ]]_v = [[ FNot f ]]_v).
+ conclude
+ ([[ (FNot f)[FTop/x] ]]_v)
+ = ([[ FNot (f[FTop/x]) ]]_v).
+ = (1 - [[ f[FTop/x] ]]_v).
+ = (1 - [[ f ]]_v) by H1.
+ = ([[ FNot f ]]_v).
+ done.
+(*END*)
+qed.