Diferencia entre revisiones de «Relación 2»
De Razonamiento automático (2018-19)
m (Protegió «Relación 2» ([Editar=Solo administradores] (indefinido) [Trasladar=Solo administradores] (indefinido))) |
|||
(No se muestran 27 ediciones intermedias de 15 usuarios) | |||
Línea 16: | Línea 16: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
− | (* pabalagon cammonagu juacanrod alfmarcua *) | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
+ | hugrubsan enrparalv gleherlop chrgencar giafus1 pabbergue alikan | ||
+ | aribatval *) | ||
fun sumaImpares :: "nat ⇒ nat" where | fun sumaImpares :: "nat ⇒ nat" where | ||
"sumaImpares 0 = 0" | | "sumaImpares 0 = 0" | | ||
Línea 25: | Línea 27: | ||
value "sumaImpares 5 = 25" | value "sumaImpares 5 = 25" | ||
+ | (* josgomrom4 marfruman1 benber antramhur *) | ||
+ | fun sumaImpares2 :: "nat ⇒ nat" where | ||
+ | "sumaImpares2 0 = 0" | | ||
+ | "sumaImpares2 (Suc n) = 2*n + 1 + sumaImpares2 n" | ||
+ | |||
+ | value "sumaImpares2 1 = 1" | ||
+ | value "sumaImpares2 3 = 9" | ||
+ | value "sumaImpares2 5 = 25" | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 31: | Línea 41: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
− | (* pabalagon cammonagu juacanrod *) | + | (* manperjim pabalagon cammonagu juacanrod josgomrom4 hugrubsan |
+ | enrparalv gleherlop benber chrgencar giafus1 alikan aribatval *) | ||
lemma "sumaImpares n = n*n" | lemma "sumaImpares n = n*n" | ||
− | apply(induction n) | + | apply (induction n) |
apply auto | apply auto | ||
done | done | ||
Línea 39: | Línea 50: | ||
(* pabalagon *) | (* pabalagon *) | ||
lemma "sumaImpares n = n*n" | lemma "sumaImpares n = n*n" | ||
− | apply(induction n) | + | apply (induction n) |
+ | apply simp | ||
apply simp | apply simp | ||
− | |||
done | done | ||
− | (* pabalagon *) | + | (* pabalagon antramhur *) |
lemma "sumaImpares n = n*n" | lemma "sumaImpares n = n*n" | ||
by (induction n) simp_all | by (induction n) simp_all | ||
− | (* alfmarcua *) | + | (* alfmarcua raffergon2 marfruman1 pabbergue *) |
lemma "sumaImpares n = n*n" | lemma "sumaImpares n = n*n" | ||
by (induction n) auto | by (induction n) auto | ||
Línea 61: | Línea 72: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
− | (* pabalagon cammonagu*) | + | (* manperjim pabalagon cammonagu chrgencar hugrubsan gleherlop benber |
+ | pabbergue aribatval *) | ||
fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where | fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where | ||
"sumaPotenciasDeDosMasUno 0 = 2" | | "sumaPotenciasDeDosMasUno 0 = 2" | | ||
− | "sumaPotenciasDeDosMasUno (Suc n) = 2^(n+1) + sumaPotenciasDeDosMasUno n" | + | "sumaPotenciasDeDosMasUno (Suc n) = |
+ | 2^(n+1) + sumaPotenciasDeDosMasUno n" | ||
− | + | value "sumaPotenciasDeDosMasUno 3 = 16" | |
+ | |||
+ | (* juacanrod josgomrom4 marfruman1 antramhur *) | ||
fun sumaPotenciasDeDosMasUno2 :: "nat ⇒ nat" where | fun sumaPotenciasDeDosMasUno2 :: "nat ⇒ nat" where | ||
"sumaPotenciasDeDosMasUno2 0 = 2" | | "sumaPotenciasDeDosMasUno2 0 = 2" | | ||
− | "sumaPotenciasDeDosMasUno2 (Suc n) = 2^(Suc n) + sumaPotenciasDeDosMasUno2 n" | + | "sumaPotenciasDeDosMasUno2 (Suc n) = |
+ | 2^(Suc n) + sumaPotenciasDeDosMasUno2 n" | ||
+ | |||
+ | value "sumaPotenciasDeDosMasUno2 3 = 16" | ||
− | (* alfmarcua *) | + | (* alfmarcua raffergon2 enrparalv alikan giafus1 *) |
fun sumaPotenciasDeDosMasUno3 :: "nat ⇒ nat" where | fun sumaPotenciasDeDosMasUno3 :: "nat ⇒ nat" where | ||
"sumaPotenciasDeDosMasUno3 0 = 2" | | "sumaPotenciasDeDosMasUno3 0 = 2" | | ||
"sumaPotenciasDeDosMasUno3 n = 2^n + sumaPotenciasDeDosMasUno3 (n-1)" | "sumaPotenciasDeDosMasUno3 n = 2^n + sumaPotenciasDeDosMasUno3 (n-1)" | ||
+ | |||
+ | value "sumaPotenciasDeDosMasUno3 3 = 16" | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 81: | Línea 101: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
− | (* pabalagon cammonagu juacanrod alfmarcua *) | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
+ | josgomrom4 hugrubsan enrparalv benber chrgencar gleherlop giafus1 | ||
+ | pabbergue alikan aribatval *) | ||
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | ||
− | apply(induction n) | + | apply (induction n) |
apply auto | apply auto | ||
done | done | ||
− | (* pabalagon *) | + | (* pabalagon antramhur *) |
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | ||
by (induction n) simp_all | by (induction n) simp_all | ||
+ | |||
+ | (* marfruman1 *) | ||
+ | lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | ||
+ | by (induct n) auto | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 99: | Línea 125: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
− | (* pabalagon cammonagu*) | + | (* manperjim pabalagon cammonagu josgomrom4 hugrubsan benber gleherlop |
+ | chrgencar pabbergue alikan antramhur aribatval *) | ||
fun copia :: "nat ⇒ 'a ⇒ 'a list" where | fun copia :: "nat ⇒ 'a ⇒ 'a list" where | ||
"copia 0 x = []" | | "copia 0 x = []" | | ||
"copia (Suc n) x = x#copia n x" | "copia (Suc n) x = x#copia n x" | ||
− | + | value "copia 3 x = [x,x,x]" | |
+ | |||
+ | (* juacanrod marfruman1 *) | ||
fun copia2 :: "nat ⇒ 'a ⇒ 'a list" where | fun copia2 :: "nat ⇒ 'a ⇒ 'a list" where | ||
"copia2 0 x = []" | | "copia2 0 x = []" | | ||
"copia2 (Suc n) x = [x] @ copia2 n x" | "copia2 (Suc n) x = [x] @ copia2 n x" | ||
− | (* alfmarcua *) | + | value "copia2 3 x = [x,x,x]" |
+ | |||
+ | (* manperjim alfmarcua raffergon2 enrparalv giafus1 *) | ||
fun copia3 :: "nat ⇒ 'a ⇒ 'a list" where | fun copia3 :: "nat ⇒ 'a ⇒ 'a list" where | ||
"copia3 0 x = []" | | "copia3 0 x = []" | | ||
"copia3 n x = x#(copia (n-1) x)" | "copia3 n x = x#(copia (n-1) x)" | ||
+ | |||
+ | value "copia3 3 x = [x,x,x]" | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 124: | Línea 157: | ||
----------------------------------------------------------------- *} | ----------------------------------------------------------------- *} | ||
− | (* pabalagon cammonagu juacanrod alfmarcua *) | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
+ | josgomrom4 marfruman1 hugrubsan enrparalv benber chrgencar giafus1 | ||
+ | gleherlop pabbergue alikan antramhur aribatval *) | ||
fun todos :: "('a ⇒ bool) ⇒ 'a list ⇒ bool" where | fun todos :: "('a ⇒ bool) ⇒ 'a list ⇒ bool" where | ||
"todos p [] = True" | | "todos p [] = True" | | ||
"todos p (x#xs) = (p x ∧ todos p xs)" | "todos p (x#xs) = (p x ∧ todos p xs)" | ||
+ | |||
+ | value "todos (λx. x>(1::nat)) [2,6,4] = True" | ||
+ | value "todos (λx. x>(2::nat)) [2,6,4] = False" | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 134: | Línea 172: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
− | (* pabalagon cammonagu juacanrod alfmarcua *) | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
+ | josgomrom4 hugrubsan enrparalv benber giafus1 pabbergue alikan | ||
+ | gleherlop chrgencar aribatval *) | ||
lemma "todos (λy. y=x) (copia n x)" | lemma "todos (λy. y=x) (copia n x)" | ||
− | apply(induction n) | + | apply (induction n) |
apply auto | apply auto | ||
done | done | ||
− | (* pabalagon *) | + | (* Comentario: La demostración anterior falla para copia3. *) |
+ | |||
+ | (* pabalagon antramhur *) | ||
lemma "todos (λy. y=x) (copia n x)" | lemma "todos (λy. y=x) (copia n x)" | ||
by (induction n) simp_all | by (induction n) simp_all | ||
Línea 152: | Línea 194: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
− | (* pabalagon cammonagu juacanrod alfmarcua *) | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
+ | josgomrom4 marfruman1 hugrubsan enrparalv benber chrgencar gleherlop | ||
+ | giafus1 pabbergue alikan antramhur aribatval *) | ||
fun amplia :: "'a list ⇒ 'a ⇒ 'a list" where | fun amplia :: "'a list ⇒ 'a ⇒ 'a list" where | ||
"amplia [] y = [y]" | | "amplia [] y = [y]" | | ||
"amplia (x#xs) y = x # amplia xs y" | "amplia (x#xs) y = x # amplia xs y" | ||
+ | |||
+ | value "amplia [d,a] t = [d,a,t]" | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 162: | Línea 208: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
− | (* pabalagon cammonagu juacanrod alfmarcua *) | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
+ | josgomrom4 hugrubsan gleherlop enrparalv benber chrgencar giafus1 | ||
+ | pabbergue alikan aribatval *) | ||
lemma "amplia xs y = xs @ [y]" | lemma "amplia xs y = xs @ [y]" | ||
− | apply(induction xs) | + | apply (induction xs) |
apply auto | apply auto | ||
done | done | ||
− | (* pabalagon *) | + | (* pabalagon antramhur *) |
lemma "amplia xs y = xs @ [y]" | lemma "amplia xs y = xs @ [y]" | ||
by (induction xs) simp_all | by (induction xs) simp_all | ||
+ | |||
+ | (* marfruman1 *) | ||
+ | lemma "amplia xs y = xs @ [y]" | ||
+ | by (induction xs) auto | ||
end | end | ||
</source> | </source> |
Revisión actual del 19:48 6 mar 2019
chapter {* R2: Razonamiento sobre programas en Isabelle/HOL *}
theory R2_Razonamiento_automatico_sobre_programas
imports Main
begin
declare [[names_short]]
text {* ---------------------------------------------------------------
Ejercicio 1.1. Definir la función
sumaImpares :: nat ⇒ nat
tal que (sumaImpares n) es la suma de los n primeros números
impares. Por ejemplo,
sumaImpares 5 = 25
------------------------------------------------------------------ *}
(* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2
hugrubsan enrparalv gleherlop chrgencar giafus1 pabbergue alikan
aribatval *)
fun sumaImpares :: "nat ⇒ nat" where
"sumaImpares 0 = 0" |
"sumaImpares n = 2*n-1 + sumaImpares (n-1)"
value "sumaImpares 1 = 1"
value "sumaImpares 3 = 9"
value "sumaImpares 5 = 25"
(* josgomrom4 marfruman1 benber antramhur *)
fun sumaImpares2 :: "nat ⇒ nat" where
"sumaImpares2 0 = 0" |
"sumaImpares2 (Suc n) = 2*n + 1 + sumaImpares2 n"
value "sumaImpares2 1 = 1"
value "sumaImpares2 3 = 9"
value "sumaImpares2 5 = 25"
text {* ---------------------------------------------------------------
Ejercicio 1.2. Demostrar que
sumaImpares n = n*n
------------------------------------------------------------------- *}
(* manperjim pabalagon cammonagu juacanrod josgomrom4 hugrubsan
enrparalv gleherlop benber chrgencar giafus1 alikan aribatval *)
lemma "sumaImpares n = n*n"
apply (induction n)
apply auto
done
(* pabalagon *)
lemma "sumaImpares n = n*n"
apply (induction n)
apply simp
apply simp
done
(* pabalagon antramhur *)
lemma "sumaImpares n = n*n"
by (induction n) simp_all
(* alfmarcua raffergon2 marfruman1 pabbergue *)
lemma "sumaImpares n = n*n"
by (induction n) auto
text {* ---------------------------------------------------------------
Ejercicio 2.1. Definir la función
sumaPotenciasDeDosMasUno :: nat ⇒ nat
tal que
(sumaPotenciasDeDosMasUno n) = 1 + 2^0 + 2^1 + 2^2 + ... + 2^n.
Por ejemplo,
sumaPotenciasDeDosMasUno 3 = 16
------------------------------------------------------------------ *}
(* manperjim pabalagon cammonagu chrgencar hugrubsan gleherlop benber
pabbergue aribatval *)
fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where
"sumaPotenciasDeDosMasUno 0 = 2" |
"sumaPotenciasDeDosMasUno (Suc n) =
2^(n+1) + sumaPotenciasDeDosMasUno n"
value "sumaPotenciasDeDosMasUno 3 = 16"
(* juacanrod josgomrom4 marfruman1 antramhur *)
fun sumaPotenciasDeDosMasUno2 :: "nat ⇒ nat" where
"sumaPotenciasDeDosMasUno2 0 = 2" |
"sumaPotenciasDeDosMasUno2 (Suc n) =
2^(Suc n) + sumaPotenciasDeDosMasUno2 n"
value "sumaPotenciasDeDosMasUno2 3 = 16"
(* alfmarcua raffergon2 enrparalv alikan giafus1 *)
fun sumaPotenciasDeDosMasUno3 :: "nat ⇒ nat" where
"sumaPotenciasDeDosMasUno3 0 = 2" |
"sumaPotenciasDeDosMasUno3 n = 2^n + sumaPotenciasDeDosMasUno3 (n-1)"
value "sumaPotenciasDeDosMasUno3 3 = 16"
text {* ---------------------------------------------------------------
Ejercicio 2.2. Demostrar que
sumaPotenciasDeDosMasUno n = 2^(n+1)
------------------------------------------------------------------- *}
(* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2
josgomrom4 hugrubsan enrparalv benber chrgencar gleherlop giafus1
pabbergue alikan aribatval *)
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)"
apply (induction n)
apply auto
done
(* pabalagon antramhur *)
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)"
by (induction n) simp_all
(* marfruman1 *)
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)"
by (induct n) auto
text {* ---------------------------------------------------------------
Ejercicio 3.1. Definir la función
copia :: nat ⇒ 'a ⇒ 'a list
tal que (copia n x) es la lista formado por n copias del elemento
x. Por ejemplo,
copia 3 x = [x,x,x]
------------------------------------------------------------------ *}
(* manperjim pabalagon cammonagu josgomrom4 hugrubsan benber gleherlop
chrgencar pabbergue alikan antramhur aribatval *)
fun copia :: "nat ⇒ 'a ⇒ 'a list" where
"copia 0 x = []" |
"copia (Suc n) x = x#copia n x"
value "copia 3 x = [x,x,x]"
(* juacanrod marfruman1 *)
fun copia2 :: "nat ⇒ 'a ⇒ 'a list" where
"copia2 0 x = []" |
"copia2 (Suc n) x = [x] @ copia2 n x"
value "copia2 3 x = [x,x,x]"
(* manperjim alfmarcua raffergon2 enrparalv giafus1 *)
fun copia3 :: "nat ⇒ 'a ⇒ 'a list" where
"copia3 0 x = []" |
"copia3 n x = x#(copia (n-1) x)"
value "copia3 3 x = [x,x,x]"
text {* ---------------------------------------------------------------
Ejercicio 3.2. Definir la función
todos :: ('a ⇒ bool) ⇒ 'a list ⇒ bool
tal que (todos p xs) se verifica si todos los elementos de xs cumplen
la propiedad p. Por ejemplo,
todos (λx. x>(1::nat)) [2,6,4] = True
todos (λx. x>(2::nat)) [2,6,4] = False
Nota: La conjunción se representa por ∧
----------------------------------------------------------------- *}
(* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2
josgomrom4 marfruman1 hugrubsan enrparalv benber chrgencar giafus1
gleherlop pabbergue alikan antramhur aribatval *)
fun todos :: "('a ⇒ bool) ⇒ 'a list ⇒ bool" where
"todos p [] = True" |
"todos p (x#xs) = (p x ∧ todos p xs)"
value "todos (λx. x>(1::nat)) [2,6,4] = True"
value "todos (λx. x>(2::nat)) [2,6,4] = False"
text {* ---------------------------------------------------------------
Ejercicio 3.3. Demostrar que todos los elementos de (copia n x) son
iguales a x.
------------------------------------------------------------------- *}
(* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2
josgomrom4 hugrubsan enrparalv benber giafus1 pabbergue alikan
gleherlop chrgencar aribatval *)
lemma "todos (λy. y=x) (copia n x)"
apply (induction n)
apply auto
done
(* Comentario: La demostración anterior falla para copia3. *)
(* pabalagon antramhur *)
lemma "todos (λy. y=x) (copia n x)"
by (induction n) simp_all
text {* ---------------------------------------------------------------
Ejercicio 4.1. Definir, recursivamente y sin usar (@), la función
amplia :: 'a list ⇒ 'a ⇒ 'a list
tal que (amplia xs y) es la lista obtenida añadiendo el elemento y al
final de la lista xs. Por ejemplo,
amplia [d,a] t = [d,a,t]
------------------------------------------------------------------ *}
(* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2
josgomrom4 marfruman1 hugrubsan enrparalv benber chrgencar gleherlop
giafus1 pabbergue alikan antramhur aribatval *)
fun amplia :: "'a list ⇒ 'a ⇒ 'a list" where
"amplia [] y = [y]" |
"amplia (x#xs) y = x # amplia xs y"
value "amplia [d,a] t = [d,a,t]"
text {* ---------------------------------------------------------------
Ejercicio 4.2. Demostrar que
amplia xs y = xs @ [y]
------------------------------------------------------------------- *}
(* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2
josgomrom4 hugrubsan gleherlop enrparalv benber chrgencar giafus1
pabbergue alikan aribatval *)
lemma "amplia xs y = xs @ [y]"
apply (induction xs)
apply auto
done
(* pabalagon antramhur *)
lemma "amplia xs y = xs @ [y]"
by (induction xs) simp_all
(* marfruman1 *)
lemma "amplia xs y = xs @ [y]"
by (induction xs) auto
end