Diferencia entre revisiones de «R1»
De Seminario de Lógica Computacional (2018)
m (Protegió «R1» ([edit=sysop] (indefinido) [move=sysop] (indefinido))) |
|||
Línea 2: | Línea 2: | ||
(* Relación 1: Programación funcional en Coq *) | (* Relación 1: Programación funcional en Coq *) | ||
− | + | (* -------------------------------------------------------------------- | |
− | |||
− | |||
− | |||
− | (* | ||
Ejercicio 1. Definir la función | Ejercicio 1. Definir la función | ||
nandb :: bool -> bool -> bool | nandb :: bool -> bool -> bool | ||
Línea 19: | Línea 15: | ||
Definition nandb (b1:bool) (b2:bool) : bool := | Definition nandb (b1:bool) (b2:bool) : bool := | ||
− | + | match b1 with | |
+ | | false => true | ||
+ | | true => negb b2 | ||
+ | end. | ||
Example prop_nandb1: (nandb true false) = true. | Example prop_nandb1: (nandb true false) = true. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_nandb2: (nandb false false) = true. | Example prop_nandb2: (nandb false false) = true. | ||
− | + | Proof. reflexivity. Qed. | |
− | |||
Example prop_nandb3: (nandb false true) = true. | Example prop_nandb3: (nandb false true) = true. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_nandb4: (nandb true true) = false. | Example prop_nandb4: (nandb true true) = false. | ||
− | + | Proof. reflexivity. Qed. | |
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
Línea 47: | Línea 45: | ||
Definition andb3 (x:bool) (y:bool) (z:bool) : bool := | Definition andb3 (x:bool) (y:bool) (z:bool) : bool := | ||
− | + | match x with | |
+ | | true => andb y z | ||
+ | | false => false | ||
+ | end. | ||
Example prop_andb31: (andb3 true true true) = true. | Example prop_andb31: (andb3 true true true) = true. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_andb32: (andb3 false true true) = false. | Example prop_andb32: (andb3 false true true) = false. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_andb33: (andb3 true false true) = false. | Example prop_andb33: (andb3 true false true) = false. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_andb34: (andb3 true true false) = false. | Example prop_andb34: (andb3 true true false) = false. | ||
− | + | Proof. reflexivity. Qed. | |
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
Línea 71: | Línea 72: | ||
Fixpoint factorial (n:nat) : nat := | Fixpoint factorial (n:nat) : nat := | ||
− | + | match n with | |
+ | | 0 => 1 | ||
+ | | S k => mult n (factorial k) | ||
+ | end. | ||
Example prop_factorial1: (factorial 3) = 6. | Example prop_factorial1: (factorial 3) = 6. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_factorial2: (factorial 5) = (mult 10 12). | Example prop_factorial2: (factorial 5) = (mult 10 12). | ||
− | + | Proof. reflexivity. Qed. | |
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
Línea 89: | Línea 93: | ||
(blt_nat 4 2) = false. | (blt_nat 4 2) = false. | ||
------------------------------------------------------------------ *) | ------------------------------------------------------------------ *) | ||
+ | |||
+ | (* Nota: Se puede usar las funciones beq_nat y leb del texto del tema 1 *) | ||
+ | Fixpoint beq_nat (n m : nat) : bool := | ||
+ | match n with | ||
+ | | O => match m with | ||
+ | | O => true | ||
+ | | S m' => false | ||
+ | end | ||
+ | | S n' => match m with | ||
+ | | O => false | ||
+ | | S m' => beq_nat n' m' | ||
+ | end | ||
+ | end. | ||
+ | |||
+ | Fixpoint leb (n m : nat) : bool := | ||
+ | match n with | ||
+ | | O => true | ||
+ | | S n' => | ||
+ | match m with | ||
+ | | O => false | ||
+ | | S m' => leb n' m' | ||
+ | end | ||
+ | end. | ||
Definition blt_nat (n m : nat) : bool := | Definition blt_nat (n m : nat) : bool := | ||
− | + | andb (leb n m) (negb (beq_nat n m)). | |
Example prop_blt_nat1: (blt_nat 2 2) = false. | Example prop_blt_nat1: (blt_nat 2 2) = false. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_blt_nat2: (blt_nat 2 4) = true. | Example prop_blt_nat2: (blt_nat 2 4) = true. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_blt_nat3: (blt_nat 4 2) = false. | Example prop_blt_nat3: (blt_nat 4 2) = false. | ||
− | + | Proof. reflexivity. Qed. | |
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
Línea 110: | Línea 137: | ||
Theorem plus_id_exercise: forall n m o : nat, | Theorem plus_id_exercise: forall n m o : nat, | ||
n = m -> m = o -> n + m = m + o. | n = m -> m = o -> n + m = m + o. | ||
− | + | Proof. | |
+ | intros n m o. | ||
+ | intros H1 H2. | ||
+ | rewrite -> H1. | ||
+ | rewrite -> H2. | ||
+ | reflexivity. | ||
+ | Qed. | ||
Línea 120: | Línea 153: | ||
------------------------------------------------------------------ *) | ------------------------------------------------------------------ *) | ||
+ | (* Nota: Se puede usar el lema plus_1_l del texto del tema 1 *) | ||
+ | Theorem plus_1_l : forall n:nat, 1 + n = S n. | ||
+ | Proof. | ||
+ | intros n. reflexivity. Qed. | ||
Theorem mult_S_1 : forall n m : nat, | Theorem mult_S_1 : forall n m : nat, | ||
m = S n -> | m = S n -> | ||
m * (1 + n) = m * m. | m * (1 + n) = m * m. | ||
− | + | Proof. | |
+ | intros n m. | ||
+ | intros H1. | ||
+ | rewrite -> plus_1_l. | ||
+ | rewrite -> H1. | ||
+ | reflexivity. | ||
+ | Qed. | ||
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
Línea 134: | Línea 177: | ||
Theorem andb_true_elim2 : forall b c : bool, | Theorem andb_true_elim2 : forall b c : bool, | ||
andb b c = true -> c = true. | andb b c = true -> c = true. | ||
− | + | Proof. | |
+ | intros b c. | ||
+ | destruct c. | ||
+ | { intros H. | ||
+ | { reflexivity. }} | ||
+ | { intros H. | ||
+ | { rewrite <- H. | ||
+ | { destruct b. | ||
+ | { reflexivity. } | ||
+ | { reflexivity. }}}} | ||
+ | Qed. | ||
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
− | Ejercicio 8. | + | Ejercicio 8. Demostrar que |
forall n : nat, | forall n : nat, | ||
beq_nat 0 (n + 1) = false. | beq_nat 0 (n + 1) = false. | ||
Línea 144: | Línea 197: | ||
Theorem zero_nbeq_plus_1: forall n : nat, | Theorem zero_nbeq_plus_1: forall n : nat, | ||
beq_nat 0 (n + 1) = false. | beq_nat 0 (n + 1) = false. | ||
− | + | Proof. | |
+ | intros [|n]. | ||
+ | - reflexivity. | ||
+ | - reflexivity. | ||
+ | Qed. | ||
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
Línea 154: | Línea 211: | ||
Theorem identity_fn_applied_twice : | Theorem identity_fn_applied_twice : | ||
− | forall (f : bool -> bool), | + | forall (f : bool -> bool), |
(forall (x : bool), f x = x) -> | (forall (x : bool), f x = x) -> | ||
forall (b : bool), f (f b) = b. | forall (b : bool), f (f b) = b. | ||
− | + | Proof. | |
+ | intros f. | ||
+ | intros x. | ||
+ | intros b. | ||
+ | rewrite -> x. | ||
+ | rewrite -> x. | ||
+ | reflexivity. | ||
+ | Qed. | ||
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
Línea 168: | Línea 232: | ||
forall (b c : bool), | forall (b c : bool), | ||
(andb b c = orb b c) -> b = c. | (andb b c = orb b c) -> b = c. | ||
− | + | Proof. | |
+ | intros b c. | ||
+ | destruct b. | ||
+ | simpl. intros H. rewrite -> H. reflexivity. | ||
+ | simpl. intros H. rewrite -> H. reflexivity. | ||
+ | Qed. | ||
(* --------------------------------------------------------------------- | (* --------------------------------------------------------------------- | ||
Línea 194: | Línea 263: | ||
Fixpoint nat2Anat (x:nat2) : nat := | Fixpoint nat2Anat (x:nat2) : nat := | ||
− | + | match x with | |
+ | | C => O | ||
+ | | D y => (nat2Anat y) * 2 | ||
+ | | SD y => S ((nat2Anat y) * 2) | ||
+ | end. | ||
Example prop_nat2Anat1: (nat2Anat (SD (SD C))) = 3. | Example prop_nat2Anat1: (nat2Anat (SD (SD C))) = 3. | ||
− | + | Proof. reflexivity. Qed. | |
Example prop_nat2Anat2: (nat2Anat (D (SD (SD C)))) = 6. | Example prop_nat2Anat2: (nat2Anat (D (SD (SD C)))) = 6. | ||
− | + | Proof. reflexivity. Qed. | |
</source> | </source> |
Revisión del 18:06 26 feb 2018
(* Relación 1: Programación funcional en Coq *)
(* --------------------------------------------------------------------
Ejercicio 1. Definir la función
nandb :: bool -> bool -> bool
tal que (nanb x y) se verifica si x e y no son verdaderos.
Demostrar las siguientes propiedades de nand
(nandb true false) = true.
(nandb false false) = true.
(nandb false true) = true.
(nandb true true) = false.
------------------------------------------------------------------ *)
Definition nandb (b1:bool) (b2:bool) : bool :=
match b1 with
| false => true
| true => negb b2
end.
Example prop_nandb1: (nandb true false) = true.
Proof. reflexivity. Qed.
Example prop_nandb2: (nandb false false) = true.
Proof. reflexivity. Qed.
Example prop_nandb3: (nandb false true) = true.
Proof. reflexivity. Qed.
Example prop_nandb4: (nandb true true) = false.
Proof. reflexivity. Qed.
(* ---------------------------------------------------------------------
Ejercicio 2.1. Definir la función
andb3 :: bool -> bool -> bool -> bool
tal que (andb3 x y z) se verifica si x, y y z son verdaderos.
Demostrar las siguientes propiedades de andb3
(andb3 true true true) = true.
(andb3 false true true) = false.
(andb3 true false true) = false.
(andb3 true true false) = false.
------------------------------------------------------------------ *)
Definition andb3 (x:bool) (y:bool) (z:bool) : bool :=
match x with
| true => andb y z
| false => false
end.
Example prop_andb31: (andb3 true true true) = true.
Proof. reflexivity. Qed.
Example prop_andb32: (andb3 false true true) = false.
Proof. reflexivity. Qed.
Example prop_andb33: (andb3 true false true) = false.
Proof. reflexivity. Qed.
Example prop_andb34: (andb3 true true false) = false.
Proof. reflexivity. Qed.
(* ---------------------------------------------------------------------
Ejercicio 3. Definir la función
factorial :: nat -> nat1
tal que (factorial n) es el factorial de n.
(factorial 3) = 6.
(factorial 5) = (mult 10 12).
------------------------------------------------------------------ *)
Fixpoint factorial (n:nat) : nat :=
match n with
| 0 => 1
| S k => mult n (factorial k)
end.
Example prop_factorial1: (factorial 3) = 6.
Proof. reflexivity. Qed.
Example prop_factorial2: (factorial 5) = (mult 10 12).
Proof. reflexivity. Qed.
(* ---------------------------------------------------------------------
Ejercicio 4. Definir la función
blt_nat :: nat -> nat -> bool
tal que (blt n m) se verifica si n es menor que m.
Demostrar las siguientes propiedades
(blt_nat 2 2) = false.
(blt_nat 2 4) = true.
(blt_nat 4 2) = false.
------------------------------------------------------------------ *)
(* Nota: Se puede usar las funciones beq_nat y leb del texto del tema 1 *)
Fixpoint beq_nat (n m : nat) : bool :=
match n with
| O => match m with
| O => true
| S m' => false
end
| S n' => match m with
| O => false
| S m' => beq_nat n' m'
end
end.
Fixpoint leb (n m : nat) : bool :=
match n with
| O => true
| S n' =>
match m with
| O => false
| S m' => leb n' m'
end
end.
Definition blt_nat (n m : nat) : bool :=
andb (leb n m) (negb (beq_nat n m)).
Example prop_blt_nat1: (blt_nat 2 2) = false.
Proof. reflexivity. Qed.
Example prop_blt_nat2: (blt_nat 2 4) = true.
Proof. reflexivity. Qed.
Example prop_blt_nat3: (blt_nat 4 2) = false.
Proof. reflexivity. Qed.
(* ---------------------------------------------------------------------
Ejercicio 5. Demostrar que
forall n m o : nat,
n = m -> m = o -> n + m = m + o.
------------------------------------------------------------------ *)
Theorem plus_id_exercise: forall n m o : nat,
n = m -> m = o -> n + m = m + o.
Proof.
intros n m o.
intros H1 H2.
rewrite -> H1.
rewrite -> H2.
reflexivity.
Qed.
(* ---------------------------------------------------------------------
Ejercicio 6. Demostrar que
forall n m : nat,
m = S n ->
m * (1 + n) = m * m.
------------------------------------------------------------------ *)
(* Nota: Se puede usar el lema plus_1_l del texto del tema 1 *)
Theorem plus_1_l : forall n:nat, 1 + n = S n.
Proof.
intros n. reflexivity. Qed.
Theorem mult_S_1 : forall n m : nat,
m = S n ->
m * (1 + n) = m * m.
Proof.
intros n m.
intros H1.
rewrite -> plus_1_l.
rewrite -> H1.
reflexivity.
Qed.
(* ---------------------------------------------------------------------
Ejercicio 7. Demostrar que
forall b c : bool,
andb b c = true -> c = true.
------------------------------------------------------------------ *)
Theorem andb_true_elim2 : forall b c : bool,
andb b c = true -> c = true.
Proof.
intros b c.
destruct c.
{ intros H.
{ reflexivity. }}
{ intros H.
{ rewrite <- H.
{ destruct b.
{ reflexivity. }
{ reflexivity. }}}}
Qed.
(* ---------------------------------------------------------------------
Ejercicio 8. Demostrar que
forall n : nat,
beq_nat 0 (n + 1) = false.
------------------------------------------------------------------ *)
Theorem zero_nbeq_plus_1: forall n : nat,
beq_nat 0 (n + 1) = false.
Proof.
intros [|n].
- reflexivity.
- reflexivity.
Qed.
(* ---------------------------------------------------------------------
Ejercicio 9. Demostrar que
forall (f : bool -> bool),
(forall (x : bool), f x = x) ->
forall (b : bool), f (f b) = b.
------------------------------------------------------------------ *)
Theorem identity_fn_applied_twice :
forall (f : bool -> bool),
(forall (x : bool), f x = x) ->
forall (b : bool), f (f b) = b.
Proof.
intros f.
intros x.
intros b.
rewrite -> x.
rewrite -> x.
reflexivity.
Qed.
(* ---------------------------------------------------------------------
Ejercicio 10. Demostrar que
forall (b c : bool),
(andb b c = orb b c) -> b = c.
------------------------------------------------------------------ *)
Theorem andb_eq_orb :
forall (b c : bool),
(andb b c = orb b c) -> b = c.
Proof.
intros b c.
destruct b.
simpl. intros H. rewrite -> H. reflexivity.
simpl. intros H. rewrite -> H. reflexivity.
Qed.
(* ---------------------------------------------------------------------
Ejercicio 11. En este ejercicio se considera la siguiente
representación de los números naturales
Inductive nat2 : Type :=
| C : nat2
| D : nat2 -> nat2
| SD : nat2 -> nat2.
donde C representa el cero, D el doble y SD el siguiente del doble.
Definir la función
nat2Anat :: nat2 -> nat
tal que (nat2Anat x) es el número natural representado por x.
Demostrar que
nat2Anat (SD (SD C)) = 3
nat2Anat (D (SD (SD C))) = 6.
------------------------------------------------------------------ *)
Inductive nat2 : Type :=
| C : nat2
| D : nat2 -> nat2
| SD : nat2 -> nat2.
Fixpoint nat2Anat (x:nat2) : nat :=
match x with
| C => O
| D y => (nat2Anat y) * 2
| SD y => S ((nat2Anat y) * 2)
end.
Example prop_nat2Anat1: (nat2Anat (SD (SD C))) = 3.
Proof. reflexivity. Qed.
Example prop_nat2Anat2: (nat2Anat (D (SD (SD C)))) = 6.
Proof. reflexivity. Qed.