+(* Test 3
+ ======
+
+ Testare la funzione `dualize`. Il risultato atteso è:
+
+ FAnd (FNot (FAtom O)) (FOr (FAtom 1) FTop)
+*)
+
+eval normalize on (dualize (FImpl (FAtom 0) (FAnd (FAtom 1) FBot))).
+
+(* Spiegazione
+ ===========
+
+ La funzione `invert` permette di invertire un mondo `v`.
+ Ovvero, per ogni indice di atomo `i`, se `v i` restituisce
+ `1` allora `(invert v) i` restituisce `0` e viceversa.
+
+*)
+definition invert ≝
+ λv:ℕ -> ℕ. λx. if eqb (min (v x) 1) 0 then 1 else 0.
+
+interpretation "Inversione del mondo" 'invert v = (invert v).
+
+(*DOCBEGIN
+
+Il linguaggio di dimostrazione di Matita
+========================================
+
+Per dimostrare il lemma `negate_invert` in modo agevole è necessario
+utilizzare il seguente comando:
+
+* `symmetry`
+
+ Quando la conclusuine è `a = b` permette di cambiarla in `b = a`.
+
+* by H1, H2 we proved P (H)
+
+ Il comando `by ... we proved` visto nella scorsa esercitazione
+ permette di utilizzare più ipotesi o lemmi alla volta
+ separandoli con una virgola.
+
+DOCEND*)
+
+(* Esercizio 4
+ ===========
+
+ Dimostrare il lemma `negate_invert` che asserisce che
+ la semantica in un mondo `v` associato alla formula
+ negata di `F` e uguale alla semantica associata
+ a `F` in un mondo invertito.
+*)
+lemma negate_invert:
+ ∀F:Formula.∀v:ℕ→ℕ.[[ negate F ]]_v=[[ F ]]_(invert v).
+assume F:Formula.
+assume v:(ℕ→ℕ).
+we proceed by induction on F to prove ([[ negate F ]]_v=[[ F ]]_(invert v)).
+ case FBot.
+ (*BEGIN*)
+ the thesis becomes ([[ negate FBot ]]_v=[[ FBot ]]_(invert v)).
+ (*END*)
+ done.
+ case FTop.
+ (*BEGIN*)
+ the thesis becomes ([[ negate FTop ]]_v=[[ FTop ]]_(invert v)).
+ (*END*)
+ done.
+ case FAtom.
+ assume n : ℕ.
+ the thesis becomes ([[ negate (FAtom n) ]]_v=[[ FAtom n ]]_(invert v)).
+ the thesis becomes (1 - (min (v n) 1)= min (invert v n) 1).
+ the thesis becomes (1 - (min (v n) 1)= min (if eqb (min (v n) 1) 0 then 1 else 0) 1).
+ by min_bool we proved ((*BEGIN*)min (v n) 1 = 0 ∨ min (v n) 1 = 1(*END*)) (H1);
+ 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).
+ case Left.
+ conclude
+ (1 - (min (v n) 1))
+ = (1 - 0) by H.
+ = 1.
+ symmetry.
+ conclude
+ (min (if eqb (min (v n) 1) O then 1 else O) 1)
+ = (min (if eqb 0 0 then 1 else O) 1) by H.
+ = (min 1 1).
+ = 1.
+ done.
+ case Right.
+ (*BEGIN*)
+ conclude
+ (1 - (min (v n) 1))
+ = (1 - 1) by H.
+ = 0.
+ symmetry.
+ conclude
+ (min (if eqb (min (v n) 1) O then 1 else O) 1)
+ = (min (if eqb 1 0 then 1 else O) 1) by H.
+ = (min 0 1).
+ = 0.
+ (*END*)
+ done.
+ case FAnd.
+ assume f : Formula.
+ by induction hypothesis we know
+ ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
+ assume f1 : Formula.
+ by induction hypothesis we know
+ ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
+ the thesis becomes
+ ([[ negate (FAnd f f1) ]]_v=[[ FAnd f f1 ]]_(invert v)).
+ the thesis becomes
+ (min [[ negate f ]]_v [[ negate f1]]_v = [[ FAnd f f1 ]]_(invert v)).
+ conclude
+ (min [[ negate f ]]_v [[ negate f1]]_v)
+ = (min [[ f ]]_(invert v) [[ negate f1]]_v) by H.
+ = (min [[ f ]]_(invert v) [[ f1]]_(invert v)) by (*BEGIN*)H1(*END*).
+ done.
+ case FOr.
+ (*BEGIN*)
+ assume f : Formula.
+ by induction hypothesis we know
+ ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
+ assume f1 : Formula.
+ by induction hypothesis we know
+ ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
+ the thesis becomes
+ ([[ negate (FOr f f1) ]]_v=[[ FOr f f1 ]]_(invert v)).
+ the thesis becomes
+ (max [[ negate f ]]_v [[ negate f1]]_v = [[ FOr f f1 ]]_(invert v)).
+ conclude
+ (max [[ negate f ]]_v [[ negate f1]]_v)
+ = (max [[ f ]]_(invert v) [[ negate f1]]_v) by H.
+ = (max [[ f ]]_(invert v) [[ f1]]_(invert v)) by H1.
+ (*END*)
+ done.
+ case FImpl.
+ (*BEGIN*)
+ assume f : Formula.
+ by induction hypothesis we know
+ ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
+ assume f1 : Formula.
+ by induction hypothesis we know
+ ([[ negate f1 ]]_v=[[ f1 ]]_(invert v)) (H1).
+ the thesis becomes
+ ([[ negate (FImpl f f1) ]]_v=[[ FImpl f f1 ]]_(invert v)).
+ the thesis becomes
+ (max (1 - [[ negate f ]]_v) [[ negate f1]]_v = [[ FImpl f f1 ]]_(invert v)).
+ conclude
+ (max (1 - [[ negate f ]]_v) [[ negate f1]]_v)
+ = (max (1 - [[ f ]]_(invert v)) [[ negate f1]]_v) by H.
+ = (max (1 - [[ f ]]_(invert v)) [[ f1]]_(invert v)) by H1.
+ (*END*)
+ done.
+ case FNot.
+ (*BEGIN*)
+ assume f : Formula.
+ by induction hypothesis we know
+ ([[ negate f ]]_v=[[ f ]]_(invert v)) (H).
+ the thesis becomes
+ ([[ negate (FNot f) ]]_v=[[ FNot f ]]_(invert v)).
+ the thesis becomes
+ (1 - [[ negate f ]]_v=[[ FNot f ]]_(invert v)).
+ conclude (1 - [[ negate f ]]_v) = (1 - [[f]]_(invert v)) by H.
+ (*END*)
+ done.
+qed.
+
+(* Esercizio 5
+ ===========
+
+ Dimostrare che la funzione negate rispetta l'equivalenza.
+*)
+lemma negate_fun:
+ ∀F:Formula.∀G:Formula.F ≡ G→negate F ≡ negate G.
+ (*BEGIN*)
+ assume F:Formula.
+ assume G:Formula.
+ suppose (F ≡ G) (H).
+ the thesis becomes (negate F ≡ negate G).
+ the thesis becomes (∀v:ℕ→ℕ.[[ negate F ]]_v=[[ negate G ]]_v).
+ (*END*)
+ assume v:(ℕ→ℕ).
+ conclude
+ [[ negate F ]]_v
+ = [[ F ]]_(invert v) by negate_invert.
+ = [[ G ]]_((*BEGIN*)invert v(*BEGIN*)) by (*BEGIN*)H(*BEGIN*).
+ = [[ negate G ]]_(*BEGIN*)v(*BEGIN*) by (*BEGIN*)negate_invert(*END*).
+ done.
+qed.
+
+(* Esercizio 6
+ ===========
+
+ Dimostrare che per ogni formula `F`, `(negae F)` equivale a
+ dualizzarla e negarla.
+*)
+lemma not_dualize_eq_negate:
+ ∀F:Formula.negate F ≡ FNot (dualize F).
+ (*BEGIN*)
+ assume F:Formula.
+ the thesis becomes (∀v:ℕ→ℕ.[[negate F]]_v=[[FNot (dualize F)]]_v).
+ (*END*)
+ assume v:(ℕ→ℕ).
+ we proceed by induction on F to prove ([[negate F]]_v=[[FNot (dualize F)]]_v).
+ case FBot.
+ (*BEGIN*)
+ the thesis becomes ([[ negate FBot ]]_v=[[ FNot (dualize FBot) ]]_v).
+ (*END*)
+ done.
+ case FTop.
+ (*BEGIN*)
+ the thesis becomes ([[ negate FTop ]]_v=[[ FNot (dualize FTop) ]]_v).
+ (*END*)
+ done.
+ case FAtom.
+ (*BEGIN*)
+ assume n : ℕ.
+ the thesis becomes ([[ negate (FAtom n) ]]_v=[[ FNot (dualize (FAtom n)) ]]_v).
+ (*END*)
+ done.
+ case FAnd.
+ assume f : Formula.
+ by induction hypothesis we know
+ ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
+ assume f1 : Formula.
+ by induction hypothesis we know
+ ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
+ the thesis becomes
+ ([[ negate (FAnd f f1) ]]_v=[[ FNot (dualize (FAnd f f1)) ]]_v).
+ the thesis becomes
+ (min [[ negate f ]]_v [[ negate f1 ]]_v=[[ FNot (dualize (FAnd f f1)) ]]_v).
+ conclude
+ (min [[ negate f ]]_v [[ negate f1 ]]_v)
+ = (min [[ FNot (dualize f) ]]_v [[ negate f1 ]]_v) by H.
+ = (min [[ FNot (dualize f) ]]_v [[ FNot (dualize f1) ]]_v) by H1.
+ = (min (1 - [[ dualize f ]]_v) (1 - [[ dualize f1 ]]_v)).
+ = (1 - (max [[ dualize f ]]_v [[ dualize f1 ]]_v)) by min_max.
+ done.
+ case FOr.
+ (*BEGIN*)
+ assume f : Formula.
+ by induction hypothesis we know
+ ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
+ assume f1 : Formula.
+ by induction hypothesis we know
+ ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
+ the thesis becomes
+ ([[ negate (FOr f f1) ]]_v=[[ FNot (dualize (FOr f f1)) ]]_v).
+ the thesis becomes
+ (max [[ negate f ]]_v [[ negate f1 ]]_v=[[ FNot (dualize (FOr f f1)) ]]_v).
+ conclude
+ (max [[ negate f ]]_v [[ negate f1 ]]_v)
+ = (max [[ FNot (dualize f) ]]_v [[ negate f1 ]]_v) by H.
+ = (max [[ FNot (dualize f) ]]_v [[ FNot (dualize f1) ]]_v) by H1.
+ = (max (1 - [[ dualize f ]]_v) (1 - [[ dualize f1 ]]_v)).
+ = (1 - (min [[ dualize f ]]_v [[ dualize f1 ]]_v)) by max_min.
+ (*END*)
+ done.
+ case FImpl.
+ (*BEGIN*)
+ assume f : Formula.
+ by induction hypothesis we know
+ ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
+ assume f1 : Formula.
+ by induction hypothesis we know
+ ([[ negate f1 ]]_v=[[ FNot (dualize f1) ]]_v) (H1).
+ the thesis becomes
+ ([[ negate (FImpl f f1) ]]_v=[[ FNot (dualize (FImpl f f1)) ]]_v).
+ the thesis becomes
+ (max (1 - [[ negate f ]]_v) [[ negate f1 ]]_v=[[ FNot (dualize (FImpl f f1)) ]]_v).
+ conclude
+ (max (1-[[ negate f ]]_v) [[ negate f1 ]]_v)
+ = (max (1-[[ FNot (dualize f) ]]_v) [[ negate f1 ]]_v) by H.
+ = (max (1-[[ FNot (dualize f) ]]_v) [[ FNot (dualize f1) ]]_v) by H1.
+ = (max (1 - [[ FNot (dualize f) ]]_v) (1 - [[ dualize f1 ]]_v)).
+ = (1 - (min [[ FNot (dualize f) ]]_v [[ dualize f1 ]]_v)) by max_min.
+ (*END*)
+ done.
+ case FNot.
+ (*BEGIN*)
+ assume f : Formula.
+ by induction hypothesis we know
+ ([[ negate f ]]_v=[[ FNot (dualize f) ]]_v) (H).
+ the thesis becomes
+ ([[ negate (FNot f) ]]_v=[[ FNot (dualize (FNot f)) ]]_v).
+ the thesis becomes
+ (1 - [[ negate f ]]_v=[[ FNot (dualize (FNot f)) ]]_v).
+ conclude (1 - [[ negate f ]]_v) = (1 - [[ FNot (dualize f) ]]_v) by H.
+ (*END*)
+ done.
+qed.
+
+(* Esercizio 7
+ ===========
+
+ Dimostrare che la negazione è iniettiva
+*)
+theorem not_inj:
+ ∀F,G:Formula.FNot F ≡ FNot G→F ≡ G.
+ (*BEGIN*)
+ assume F:Formula.
+ assume G:Formula.
+ suppose (FNot F ≡ FNot G) (H).
+ the thesis becomes (F ≡ G).
+ the thesis becomes (∀v:ℕ→ℕ.[[ F ]]_v=[[ G ]]_v).
+ (*END*)
+ assume v:(ℕ→ℕ).
+ by H we proved ([[ FNot F ]]_v=[[ FNot G ]]_v) (H1).
+ by sem_bool we proved ([[ F ]]_v=O ∨ [[ F ]]_v=1) (H2).
+ by (*BEGIN*)sem_bool(*END*) we proved ([[ G ]]_v=(*BEGIN*)O ∨ [[ G ]]_v=1(*END*)) (H3).
+ we proceed by cases on H2 to prove ([[ F ]]_v=[[ G ]]_v).
+ case Left.
+ we proceed by cases on H3 to prove ([[ F ]]_v=[[ G ]]_v).
+ case Left.
+ done.
+ case Right.
+ conclude
+ ([[ F ]]_v)
+ = 0 by H4;
+ = (1 - 1).
+ = (1 - [[G]]_v) by H5.
+ = [[ FNot G ]]_v.
+ = [[ FNot F ]]_v by H1.
+ = (1 - [[F]]_v).
+ = (1 - 0) by H4.
+ = 1.
+ done.
+ case Right.
+ (*BEGIN*)
+ we proceed by cases on H3 to prove ([[ F ]]_v=[[ G ]]_v).
+ case Left.
+ conclude
+ ([[ F ]]_v)
+ = 1 by H4;
+ = (1 - 0).
+ = (1 - [[G]]_v) by H5.
+ = [[ FNot G ]]_v.
+ = [[ FNot F ]]_v by H1.
+ = (1 - [[F]]_v).
+ = (1 - 1) by H4.
+ = 0.
+ done.
+ case Right.
+ (*END*)
+ done.
+qed.
+
+(*DOCBEGIN
+
+La prova del teorema di dualità
+===============================
+
+Il teorema di dualità accennato a lezione dice che se due formule
+`F1` ed `F2` sono equivalenti, allora anche le formule duali lo sono.
+
+ ∀F1,F2:Formula. F1 ≡ F2 → dualize F1 ≡ dualize F2.
+
+Per dimostrare tale teorema è bene suddividere la prova in lemmi intermedi
+
+1. lemma `negate_invert`, dimostrato per induzione su F, utilizzando
+ `min_bool`
+
+ ∀F:Formula.∀v:ℕ→ℕ.[[ negate F ]]_v=[[ F ]]_(invert v).
+
+2. lemma `negate_fun`, conseguenza di `negate_invert`
+
+ ∀F,G:Formula. F ≡ G → negate F ≡ negate G.
+
+2. lemma `not_dualize_eq_negate`, dimostrato per induzione su F,
+ utilizzando `max_min` e `min_max`
+
+ ∀F:Formula. negate F ≡ FNot (dualize F)
+
+4. lemma `not_inj`, conseguenza di `sem_bool`
+
+ ∀F,G:Formula. FNot F ≡ FNot G → F ≡ G