-include "induction_support.ma".
+(* Esercitazione di logica 22/10/2008.
+
+ Esercizio 0: compilare i seguenti campi
+
+ Nome1: ...
+ Cognome1: ...
+ Matricola1: ...
+ Account1: ...
+
+ Nome2: ...
+ Cognome2: ...
+ Matricola2: ...
+ Account2: ...
+ Prima di abbandonare la postazione:
+
+ * compilare il questionario in fondo al file
+
+ * salvare il file (menu 'File ▹ Save as ...') nella directory (cartella)
+ /public/ con nome linguaggi_Account1.ma, ad esempio Mario Rossi, il cui
+ account è mrossi deve salvare il file in /public/linguaggi_mrossi.ma
+*)
+
+(*DOCBEGIN
+
+ Come scrivere i simboli
+ =======================
+
+ Per inserire i simboli matematici è necessario digitare il loro nome
+ e poi premere CTRL-L. In generale i nomi dei simboli sono della forma
+ '\nome', ad esempio '\equiv'. Alcuni simboli molto frequenti hanno
+ dei sinonimi più comodi da digitare, per esemio ⇒ ha sia il nome
+ '\Rightarrow' sia '=>'.
+
+ Segue un elenco dei simboli più comuni e i loro nomi separati da virgola,
+ Se sono necessari dei simboli non riportati di seguito si può visualizzare
+ l'intera lista dal menù a tendina 'View ▹ TeX/UTF8 table'.
+
+ * → : \to, ->
+ * ⇒ : \Rightarrow, =>
+ * ℕ : \naturals
+ * ≝ : \def, :=
+ * ≡ : \equiv
+ * ∀ : \forall
+
+ La sintassi '∀v.P' significa "per tutti i 'v' vale 'P'".
+
+ La sintassi 'F → G' dove 'F' e 'G' sono proposizioni nel metalinguaggio
+ significa "'F' implica 'G'". Attenzione, il simbolo '⇒' (usato a lezione)
+ non ha lo stesso significato in Matita.
+
+ La sintassi 'ℕ → ℕ' è il tipo delle funzioni che preso un numero naturale
+ restituiscono un numero naturale.
+
+ LA sintassi ..
+ ==============
+ * applicazione
+ * match
+ * min/max a b
+ * sottrazione
+
+ I comandi per le definizioni
+ ============================
+
+ Esistono due tipi di definizioni: definizioni ricorsive tramite sintassi
+ simile a BNF, definizione di funzioni per ricorsione strutturale.
+
+ Definire una nuova sintassi astratta
+ ------------------------------------
+
+ Definizione
+
+DOCEND*)
+
+(* non modificare le seguenti tre righe *)
+include "nat/minus.ma".
+definition max : nat → nat → nat ≝ λa,b:nat.let rec max n m on n ≝ match n with [ O ⇒ b | S n ⇒ match m with [ O ⇒ a | S m ⇒ max n m]] in max a b.
+definition min : nat → nat → nat ≝ λa,b:nat.let rec min n m on n ≝ match n with [ O ⇒ a | S n ⇒ match m with [ O ⇒ b | S m ⇒ min n m]] in min a b.
+
+(* Esercizio 1: Definire l'albero di sintassi astratta delle formule *)
inductive Formula : Type ≝
| FBot: Formula
| FTop: (*BEGIN*)Formula(*END*)
-| FAtom: nat → Formula
-| FNot: Formula → Formula
-| FAnd: (*BEGIN*)Formula → Formula → Formula(*END*)
+| FAtom: nat → Formula (* usiamo i naturali al posto delle lettere *)
+| FAnd: Formula → Formula → Formula
| FOr: (*BEGIN*)Formula → Formula → Formula(*END*)
| FImpl: (*BEGIN*)Formula → Formula → Formula(*END*)
+| FNot: (*BEGIN*)Formula → Formula(*END*)
.
-let rec sem (v: nat -> nat) (F: Formula) on F ≝
+(* Esercizio 2: Data la funzione di valutazione per gli atomi 'v', definire la
+ funzione 'sem' per una generica formula 'F' che vi associa la semantica
+ (o denotazione) *)
+let rec sem (v: nat → nat) (F: Formula) on F ≝
match F with
[ FBot ⇒ 0
| FTop ⇒ (*BEGIN*)1(*END*)
+ (*BEGIN*)
| FAtom n ⇒ v n
- | FNot F1 ⇒ 1 - sem v F1
- | FAnd F1 F2 ⇒ min (sem v F1) (sem v F2)
+ (*END*)
+ | FAnd F1 F2 ⇒ (*BEGIN*)min (sem v F1) (sem v F2)(*END*)
(*BEGIN*)
| FOr F1 F2 ⇒ max (sem v F1) (sem v F2)
| FImpl F1 F2 ⇒ max (1 - sem v F1) (sem v F2)
(*END*)
+ | FNot F1 ⇒ 1 - (sem v F1)
]
.
-definition if_then_else ≝
- λe,t,f.match e return λ_.Formula with [ true ⇒ t | false ⇒ f].
-
-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 }.
+(* I comandi che seguono definiscono la seguente notazione:
-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 }.
+ if e then risultato1 else risultato2
+
+ Questa notazione permette di valutare l'espressione 'e'. Se questa
+ è vera restituisce 'risultato1', altrimenti restituisce 'risultato2'.
+
+ Un esempio di espressione è 'eqb n m', che confronta i due numeri naturali
+ 'n' ed 'm'.
+
+ * [[ formula ]]_v
+
+ Questa notazione utilizza la funzione 'sem' precedentemente definita, in
+ particolare '[[ f ]]_v' è una abbreviazione per 'sem v f'.
+ Non modificare le linee seguenti, saltare all'esercizio 3
+*)
+definition if_then_else ≝ λe,t,f.match e return λ_.Formula with [ true ⇒ t | false ⇒ f].
+notation > "'if' term 19 e 'then' term 19 t 'else' term 90 f" non associative with precedence 90 for @{ 'if_then_else $e $t $f }.
+notation < "'if' \nbsp term 19 e \nbsp 'then' \nbsp term 19 t \nbsp 'else' \nbsp term 90 f \nbsp" non associative with precedence 90 for @{ 'if_then_else $e $t $f }.
interpretation "Formula if_then_else" 'if_then_else e t f = (if_then_else e t f).
+notation < "[[ \nbsp term 19 a \nbsp ]] \nbsp \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
+notation > "[[ term 19 a ]] \sub term 90 v" non associative with precedence 90 for @{ 'semantics $v $a }.
+notation > "[[ term 19 a ]]_ term 90 v" non associative with precedence 90 for @{ sem $v $a }.
+interpretation "Semantic of Formula" 'semantics v a = (sem v a).
+
+(* Esercizio 3: Definire la funzione di sostituzione di una formula 'G' al posto
+ degli atomi uguali a 'x' in una formula 'F'. *)
let rec subst (x:nat) (G: Formula) (F: Formula) on F ≝
match F with
[ FBot ⇒ FBot
| FTop ⇒ (*BEGIN*)FTop(*END*)
- | FAtom n ⇒ if eqb n x then G else (*BEGIN*)(FAtom n)(*END*)
- | FNot F ⇒ FNot (subst x G F)
+ | FAtom n ⇒ if (*BEGIN*)eqb n x(*END*) then (*BEGIN*)G(*END*) else (*BEGIN*)(FAtom n)(*END*)
(*BEGIN*)
| FAnd F1 F2 ⇒ FAnd (subst x G F1) (subst x G F2)
| FOr F1 F2 ⇒ FOr (subst x G F1) (subst x G F2)
| FImpl F1 F2 ⇒ FImpl (subst x G F1) (subst x G F2)
(*END*)
+ | FNot F ⇒ FNot (subst x G F)
].
-definition equiv ≝ λv,F1,F2. sem v F1 = sem v F2.
-
-notation "hvbox(a \nbsp break mstyle color #0000ff (≡) \sub v \nbsp b)"
-non associative with precedence 45
-for @{ 'equivF $v $a $b }.
+(* I comandi che seguono definiscono la seguente notazione:
-notation > "a ≡_ term 90 v b" non associative with precedence 50
-for @{ equiv $v $a $b }.
+ * F [ G / x ]
+
+ Questa notazione utilizza la funzione 'subst' appena definita, in particolare
+ la scrittura 'F [ G /x ]' è una abbreviazione per 'subst x G F'.
+
+ * F ≡ G
+
+ Questa notazione è una abbreviazione per '∀v.[[ f ]]_v = [[ g ]]_v'.
+ Asserisce che for ogni funzione di valutazione 'v', la semantica di 'f'
+ in 'v' è uguale alla semantica di 'g' in 'v'.
-interpretation "equivalence for Formulas" 'equivF v a b = (equiv v a b).
+ Non modificare le linee seguenti, saltare all'esercizio 4
+*)
+notation < "t [ \nbsp term 19 a / term 19 b \nbsp ]" non associative with precedence 90 for @{ 'substitution $b $a $t }.
+notation > "t [ term 90 a / term 90 b]" non associative with precedence 90 for @{ 'substitution $b $a $t }.
+interpretation "Substitution for Formula" 'substitution b a t = (subst b a t).
+definition equiv ≝ λF1,F2. ∀v.[[ F1 ]]_v = [[ F2 ]]_v.
+notation "hvbox(a \nbsp break mstyle color #0000ff (≡) \nbsp b)" non associative with precedence 45 for @{ 'equivF $a $b }.
+notation > "a ≡ b" non associative with precedence 50 for @{ equiv $a $b }.
+interpretation "equivalence for Formulas" 'equivF a b = (equiv a b).
-theorem substitution:
- ∀F1,F2,F,x,v. equiv v F1 F2 → equiv v (subst x F1 F) (subst x F2 F).
-assume F1 : Formula.
-assume F2 : Formula.
+(* Esercizio 4: Prove the substitution theorem *)
+theorem substitution: ∀G1,G2,F,x. G1 ≡ G2 → F[G1/x] ≡ F[G2/x].
+assume G1 : Formula.
+assume G2 : Formula.
+(*BEGIN*)
assume F : Formula.
assume x : ℕ.
-assume v : (ℕ → ℕ).
-suppose (F1 ≡_v F2) (H).
-we proceed by induction on F to prove (subst x F1 F ≡_v subst x F2 F).
-case Bot.
- the thesis becomes (FBot ≡_v (subst x F2 FBot)).
- the thesis becomes (FBot ≡_v FBot).
- the thesis becomes (sem v FBot = sem v FBot).
- the thesis becomes (0 = sem v FBot).
+(*END*)
+suppose (G1 ≡ G2) (H).
+we proceed by induction on F to prove (F[ G1/x ] ≡ F[ G2/x ]).
+case FBot.
+ the thesis becomes (FBot ≡ FBot[ G2/x ]).
+ the thesis becomes (FBot ≡ FBot).
+ the thesis becomes (∀v.[[FBot]]_v = [[FBot]]_v).
+ assume v : (ℕ → ℕ).
+ the thesis becomes (0 = [[FBot]]_v).
the thesis becomes (0 = 0).
done.
-case Top.
+case FTop.
(*BEGIN*)
- the thesis becomes (FTop ≡_v FTop).
- the thesis becomes (sem v FTop = sem v FTop).
+ the thesis becomes (FTop ≡ FTop).
+ the thesis becomes (∀v. [[FTop]]_v = [[FTop]]_v).
+ assume v : (ℕ → ℕ).
the thesis becomes (1 = 1).
(*END*)
done.
-case Atom.
+case FAtom.
assume n : ℕ.
the thesis becomes
- (if eqb n x then F1 else (FAtom n) ≡_v subst x F2 (FAtom n)).
- the thesis becomes
- (if eqb n x then F1 else (FAtom n) ≡_v
- if eqb n x then F2 else (FAtom n)).
- we proceed by cases on (eqb n x) to prove True. (*CSC*)
- case True.
- the thesis becomes (F1 ≡_v F2).
+ (if eqb n x then G1 else (FAtom n) ≡ (FAtom n)[ G2/x ]).
+ the thesis becomes
+ (if eqb n x then G1 else (FAtom n) ≡
+ if eqb n x then G2 else (FAtom n)).
+ we proceed by cases on (eqb n x) to prove
+ (if eqb n x then G1 else (FAtom n) ≡
+ if eqb n x then G2 else (FAtom n)).
+ case true.
+ the thesis becomes (G1 ≡ G2).
done.
- case False.
- the thesis becomes (FAtom n ≡_v FAtom n).
- the thesis becomes (sem v (FAtom n) = sem v (FAtom n)).
+ case false.
+ (*BEGIN*)
+ the thesis becomes (FAtom n ≡ FAtom n).
+ the thesis becomes (∀v. [[FAtom n]]_v = [[FAtom n]]_v).
+ assume v : (ℕ → ℕ).
the thesis becomes (v n = v n).
+ (*END*)
done.
-case Not.
- assume (*BEGIN*)f : Formula.(*END*)
- by induction hypothesis we know (subst x F1 f ≡_v subst x F2 f) (IH).
- the thesis becomes (FNot (subst x F1 f) ≡_v FNot (subst x F2 f)).
- the thesis becomes (sem v (FNot (subst x F1 f)) = sem v (FNot (subst x F2 f))).
- the thesis becomes (1 - sem v (subst x F1 f) = sem v (FNot (subst x F2 f))).
- the thesis becomes (1 - sem v (subst x F1 f) = 1 - sem v (subst x F2 f)).
- by IH we proved (sem v (subst x F1 f) = sem v (subst x F2 f)) (IH1).
- conclude (1-sem v (subst x F1 f)) = (1-sem v (subst x F2 f)) by IH1.
- done.
-case And.
- assume f : Formula.
- by induction hypothesis we know (subst x F1 f ≡_v subst x F2 f) (IH).
- assume f1 : Formula.
- by induction hypothesis we know (subst x F1 f1 ≡_v subst x F2 f1) (IH1).
- by IH we proved (sem v (subst x F1 f) = sem v (subst x F2 f)) (IH2).
- by IH1 we proved (sem v (subst x F1 f1) = sem v (subst x F2 f1)) (IH3).
+case FAnd.
+ assume F1 : Formula.
+ by induction hypothesis we know (F1[ G1/x ] ≡ F1[ G2/x ]) (IH1).
+ assume F2 : Formula.
+ by induction hypothesis we know (F2[ G1/x ] ≡ F2[ G2/x ]) (IH2).
the thesis becomes
- (sem v (FAnd (subst x F1 f) (subst x F1 f1)) =
- sem v (FAnd (subst x F2 f) (subst x F2 f1))).
+ (∀v.[[ (FAnd F1 F2)[ G1/x ] ]]_v = [[ (FAnd F1 F2)[ G2/x ] ]]_v).
+ assume v : (ℕ → ℕ).
the thesis becomes
- (min (sem v (subst x F1 f)) (sem v (subst x F1 f1)) =
- min (sem v (subst x F2 f)) (sem v (subst x F2 f1))).
+ (min ([[ F1[ G1/x ] ]]_v) ([[ F2[ G1/x ] ]]_v) =
+ min ([[ F1[ G2/x ] ]]_v) ([[ F2[ G2/x ] ]]_v)).
+ by IH1 we proved (∀v1.[[ F1[ G1/x ] ]]_v1 = [[ F1[ G2/x ] ]]_v1) (IH11).
+ by (*BEGIN*)IH2(*END*) we proved (∀v2.[[ F2[ G1/x ] ]]_v2 = [[ F2[ G2/x ] ]]_v2) (IH22).
+ by IH11 we proved ([[ F1[ G1/x ] ]]_v = [[ F1[ G2/x ] ]]_v) (IH111).
+ by (*BEGIN*)IH22(*END*) we proved ([[ F2[ G1/x ] ]]_v = [[ F2[ G2/x ] ]]_v) (IH222).
conclude
- (min (sem v (subst x F1 f)) (sem v (subst x F1 f1)))
- = (min (sem v (subst x F2 f)) (sem v (subst x F1 f1))) by IH2.
- = (*BEGIN*)(min (sem v (subst x F2 f)) (sem v (subst x F2 f1)))(*END*) by (*BEGIN*)IH3(*END*).
+ (min ([[ F1[ G1/x ] ]]_v) ([[ F2[ G1/x ] ]]_v))
+ = (min ([[ F1[ G2/x ] ]]_v) ([[ F2[ G1/x ] ]]_v)) by IH222.
+ = (min ([[(F1[ G2/x ])]]_v) ([[(F2[ G2/x ])]]_v)) by (*BEGIN*)IH111(*END*).
+ (*END*)
done.
-(*BEGIN*)
-case Or.
- assume f : Formula.
- by induction hypothesis we know (subst x F1 f ≡_v subst x F2 f) (IH).
- assume f1 : Formula.
- by induction hypothesis we know (subst x F1 f1 ≡_v subst x F2 f1) (IH1).
- by IH we proved (sem v (subst x F1 f) = sem v (subst x F2 f)) (IH2).
- by IH1 we proved (sem v (subst x F1 f1) = sem v (subst x F2 f1)) (IH3).
+case FOr.
+ (*BEGIN*)
+ assume F1 : Formula.
+ by induction hypothesis we know (F1[ G1/x ] ≡ F1[ G2/x ]) (IH1).
+ assume F2 : Formula.
+ by induction hypothesis we know (F2[ G1/x ] ≡ F2[ G2/x ]) (IH2).
the thesis becomes
- (sem v (FOr (subst x F1 f) (subst x F1 f1)) =
- sem v (FOr (subst x F2 f) (subst x F2 f1))).
+ (∀v.[[ (FOr F1 F2)[ G1/x ] ]]_v = [[ (FOr F1 F2)[ G2/x ] ]]_v).
+ assume v : (ℕ → ℕ).
the thesis becomes
- (max (sem v (subst x F1 f)) (sem v (subst x F1 f1)) =
- max (sem v (subst x F2 f)) (sem v (subst x F2 f1))).
+ (max ([[ F1[ G1/x ] ]]_v) ([[ F2[ G1/x ] ]]_v) =
+ max ([[ F1[ G2/x ] ]]_v) ([[ F2[ G2/x ] ]]_v)).
+ by IH1 we proved (∀v1.[[ F1[ G1/x ] ]]_v1 = [[ F1[ G2/x ] ]]_v1) (IH11).
+ by IH2 we proved (∀v2.[[ F2[ G1/x ] ]]_v2 = [[ F2[ G2/x ] ]]_v2) (IH22).
+ by IH11 we proved ([[ F1[ G1/x ] ]]_v = [[ F1[ G2/x ] ]]_v) (IH111).
+ by IH22 we proved ([[ F2[ G1/x ] ]]_v = [[ F2[ G2/x ] ]]_v) (IH222).
conclude
- (max (sem v (subst x F1 f)) (sem v (subst x F1 f1)))
- = (max (sem v (subst x F2 f)) (sem v (subst x F1 f1))) by IH2.
- = (max (sem v (subst x F2 f)) (sem v (subst x F2 f1))) by IH3.
- done.
-case Implication.
- assume f : Formula.
- by induction hypothesis we know (subst x F1 f ≡_v subst x F2 f) (IH).
- assume f1 : Formula.
- by induction hypothesis we know (subst x F1 f1 ≡_v subst x F2 f1) (IH1).
+ (max ([[ F1[ G1/x ] ]]_v) ([[ F2[ G1/x ] ]]_v))
+ = (max ([[ F1[ G2/x ] ]]_v) ([[ F2[ G1/x ] ]]_v)) by IH222.
+ = (max ([[(F1[ G2/x ])]]_v) ([[(F2[ G2/x ])]]_v)) by IH111.
+ (*END*)
+ done.
+case FImpl.
+ (*BEGIN*)
+ assume F1 : Formula.
+ by induction hypothesis we know (F1[ G1/x ] ≡ F1[ G2/x ]) (IH1).
+ assume F2 : Formula.
+ by induction hypothesis we know (F2[ G1/x ] ≡ F2[ G2/x ]) (IH2).
the thesis becomes
- (max (1 - sem v (subst x F1 f)) (sem v (subst x F1 f1)) =
- max (1 - sem v (subst x F2 f)) (sem v (subst x F2 f1))).
- by IH we proved (sem v (subst x F1 f) = sem v (subst x F2 f)) (IH2).
- by IH1 we proved (sem v (subst x F1 f1) = sem v (subst x F2 f1)) (IH3).
+ (∀v.max (1 - [[ F1[ G1/x ] ]]_v) ([[ F2[ G1/x ] ]]_v) =
+ max (1 - [[ F1[ G2/x ] ]]_v) ([[ F2[ G2/x ] ]]_v)).
+ assume v : (ℕ → ℕ).
+ by IH1 we proved ([[ F1[ G1/x ] ]]_v = [[ F1[ G2/x ] ]]_v) (IH11).
+ by IH2 we proved ([[ F2[ G1/x ] ]]_v = [[ F2[ G2/x ] ]]_v) (IH22).
conclude
- (max (1-sem v (subst x F1 f)) (sem v (subst x F1 f1)))
- = (max (1-sem v (subst x F2 f)) (sem v (subst x F1 f1))) by IH2.
- = (max (1-sem v (subst x F2 f)) (sem v (subst x F2 f1))) by IH3.
+ (max (1-[[ F1[ G1/x ] ]]_v) ([[ F2[ G1/x ] ]]_v))
+ = (max (1-[[ F1[ G2/x ] ]]_v) ([[ F2[ G1/x ] ]]_v)) by IH11.
+ = (max (1-[[ F1[ G2/x ] ]]_v) ([[ F2[ G2/x ] ]]_v)) by IH22.
done.
-(*END*)
+case FNot.
+ (*BEGIN*)
+ assume F1 : Formula.
+ by induction hypothesis we know (F1[ G1/x ] ≡ F1[ G2/x ]) (IH).
+ the thesis becomes (FNot (F1[ G1/x ]) ≡ FNot (F1[ G2/x ])).
+ the thesis becomes (∀v.[[FNot (F1[ G1/x ])]]_v = [[FNot (F1[ G2/x ])]]_v).
+ assume v : (ℕ → ℕ).
+ the thesis becomes (1 - [[F1[ G1/x ]]]_v = [[FNot (F1[ G2/x ])]]_v).
+ the thesis becomes (1 - [[ F1[ G1/x ] ]]_v = 1 - [[ F1[ G2/x ] ]]_v).
+ by IH we proved (∀v1.[[ F1[ G1/x ] ]]_v1 = [[ F1[ G2/x ] ]]_v1) (IH1).
+ by IH1 we proved ([[ F1[ G1/x ] ]]_v = [[ F1[ G2/x ] ]]_v) (IH2).
+ conclude
+ (1-[[ F1[ G1/x ] ]]_v)
+ = (1-[[ F1[ G2/x ] ]]_v) by IH2.
+ (*END*)
+ done.
qed.
+
+(* Questionario
+
+ Compilare mettendo una X nella risposta scelta.
+
+ 1) Pensi che sia utile l'integrazione del corso con una attività di
+ laboratorio?
+
+ [ ] per niente [ ] poco [ ] molto
+ 2) Pensi che gli esercizi proposti ti siano stati utili a capire meglio
+ quanto visto a lezione?
+
+ [ ] per niente [ ] poco [ ] molto
+
+
+ 3) Gli esercizi erano
+
+ [ ] troppo facili [ ] alla tua portata [ ] impossibili
+
+
+ 4) Il tempo a disposizione è stato
+
+ [ ] poco [ ] giusto [ ] troppo
+
+
+ 5) Cose che miglioreresti nel software Matita
+
+ .........
+
+ 6) Suggerimenti sullo svolgimento delle attività in laboratorio
+
+ .........
+
+
+*)
+
+