Diferencia entre revisiones de «Relación 2»
De Razonamiento automático (2018-19)
Línea 2: | Línea 2: | ||
chapter {* R2: Razonamiento sobre programas en Isabelle/HOL *} | chapter {* R2: Razonamiento sobre programas en Isabelle/HOL *} | ||
− | theory | + | theory R2_Razonamiento_automatico_sobre_programas_alu |
imports Main | imports Main | ||
begin | begin | ||
Línea 16: | Línea 16: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
− | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 hugrubsan enrparalv gleherlop chrgencar giafus1 pabbergue *) | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
+ | hugrubsan enrparalv gleherlop chrgencar giafus1 pabbergue *) | ||
fun sumaImpares :: "nat ⇒ nat" where | fun sumaImpares :: "nat ⇒ nat" where | ||
"sumaImpares 0 = 0" | | "sumaImpares 0 = 0" | | ||
"sumaImpares n = 2*n-1 + sumaImpares (n-1)" | "sumaImpares n = 2*n-1 + sumaImpares (n-1)" | ||
+ | value "sumaImpares 1 = 1" | ||
+ | value "sumaImpares 3 = 9" | ||
+ | value "sumaImpares 5 = 25" | ||
(* josgomrom4 marfruman1 benber *) | (* josgomrom4 marfruman1 benber *) | ||
Línea 36: | Línea 40: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
− | (* manperjim pabalagon cammonagu juacanrod josgomrom4 hugrubsan enrparalv gleherlop benber chrgencar giafus1 *) | + | (* manperjim pabalagon cammonagu juacanrod josgomrom4 hugrubsan |
+ | enrparalv gleherlop benber chrgencar giafus1 *) | ||
lemma "sumaImpares n = n*n" | lemma "sumaImpares n = n*n" | ||
apply (induction n) | apply (induction n) | ||
Línea 66: | Línea 71: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
− | (* manperjim pabalagon cammonagu chrgencar hugrubsan gleherlop benber pabbergue *) | + | (* manperjim pabalagon cammonagu chrgencar hugrubsan gleherlop benber |
+ | pabbergue *) | ||
fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where | fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where | ||
"sumaPotenciasDeDosMasUno 0 = 2" | | "sumaPotenciasDeDosMasUno 0 = 2" | | ||
"sumaPotenciasDeDosMasUno (Suc n) = | "sumaPotenciasDeDosMasUno (Suc n) = | ||
2^(n+1) + sumaPotenciasDeDosMasUno n" | 2^(n+1) + sumaPotenciasDeDosMasUno n" | ||
+ | |||
+ | value "sumaPotenciasDeDosMasUno 3 = 16" | ||
(* juacanrod josgomrom4 marfruman1*) | (* juacanrod josgomrom4 marfruman1*) | ||
Línea 77: | Línea 85: | ||
"sumaPotenciasDeDosMasUno2 (Suc n) = | "sumaPotenciasDeDosMasUno2 (Suc n) = | ||
2^(Suc n) + sumaPotenciasDeDosMasUno2 n" | 2^(Suc n) + sumaPotenciasDeDosMasUno2 n" | ||
+ | |||
+ | value "sumaPotenciasDeDosMasUno2 3 = 16" | ||
(* alfmarcua raffergon2 enrparalv alikan giafus1 *) | (* alfmarcua raffergon2 enrparalv alikan giafus1 *) | ||
Línea 82: | Línea 92: | ||
"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 88: | Línea 100: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
− | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
− | hugrubsan enrparalv benber chrgencar gleherlop giafus1 pabbergue *) | + | josgomrom4 hugrubsan enrparalv benber chrgencar gleherlop giafus1 |
+ | pabbergue *) | ||
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | ||
apply (induction n) | apply (induction n) | ||
Línea 111: | Línea 124: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
− | (* manperjim pabalagon cammonagu josgomrom4 hugrubsan benber gleherlop pabbergue *) | + | (* manperjim pabalagon cammonagu josgomrom4 hugrubsan benber gleherlop |
+ | pabbergue *) | ||
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 *) | (* juacanrod marfruman1 *) | ||
Línea 120: | Línea 136: | ||
"copia2 0 x = []" | | "copia2 0 x = []" | | ||
"copia2 (Suc n) x = [x] @ copia2 n x" | "copia2 (Suc n) x = [x] @ copia2 n x" | ||
+ | |||
+ | value "copia2 3 x = [x,x,x]" | ||
(* manperjim alfmarcua raffergon2 enrparalv giafus1 *) | (* manperjim alfmarcua raffergon2 enrparalv giafus1 *) | ||
Línea 125: | Línea 143: | ||
"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 136: | Línea 156: | ||
----------------------------------------------------------------- *} | ----------------------------------------------------------------- *} | ||
− | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
− | marfruman1 hugrubsan enrparalv benber chrgencar giafus1 gleherlop pabbergue *) | + | josgomrom4 marfruman1 hugrubsan enrparalv benber chrgencar giafus1 |
+ | gleherlop pabbergue *) | ||
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 147: | Línea 171: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
− | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
− | hugrubsan enrparalv benber giafus1 pabbergue *) | + | josgomrom4 hugrubsan enrparalv benber giafus1 pabbergue *) |
lemma "todos (λy. y=x) (copia n x)" | lemma "todos (λy. y=x) (copia n x)" | ||
apply (induction n) | apply (induction n) | ||
Línea 168: | Línea 192: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
− | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
− | marfruman1 hugrubsan enrparalv benber chrgencar gleherlop giafus1 pabbergue *) | + | josgomrom4 marfruman1 hugrubsan enrparalv benber chrgencar gleherlop |
+ | giafus1 pabbergue *) | ||
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 179: | Línea 206: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
− | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 josgomrom4 hugrubsan gleherlop enrparalv benber chrgencar giafus1 pabbergue *) | + | (* manperjim pabalagon cammonagu juacanrod alfmarcua raffergon2 |
+ | josgomrom4 hugrubsan gleherlop enrparalv benber chrgencar giafus1 | ||
+ | pabbergue *) | ||
lemma "amplia xs y = xs @ [y]" | lemma "amplia xs y = xs @ [y]" | ||
apply (induction xs) | apply (induction xs) |
Revisión del 11:09 10 ene 2019
chapter {* R2: Razonamiento sobre programas en Isabelle/HOL *}
theory R2_Razonamiento_automatico_sobre_programas_alu
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 *)
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 *)
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 *)
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 *)
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 *)
fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where
"sumaPotenciasDeDosMasUno 0 = 2" |
"sumaPotenciasDeDosMasUno (Suc n) =
2^(n+1) + sumaPotenciasDeDosMasUno n"
value "sumaPotenciasDeDosMasUno 3 = 16"
(* juacanrod josgomrom4 marfruman1*)
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 *)
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)"
apply (induction n)
apply auto
done
(* pabalagon *)
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
pabbergue *)
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 *)
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 *)
lemma "todos (λy. y=x) (copia n x)"
apply (induction n)
apply auto
done
(* Comentario: La demostración anterior falla para copia3. *)
(* pabalagon *)
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 *)
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 *)
lemma "amplia xs y = xs @ [y]"
apply (induction xs)
apply auto
done
(* pabalagon *)
lemma "amplia xs y = xs @ [y]"
by (induction xs) simp_all
(* marfruman1 *)
lemma "amplia xs y = xs @ [y]"
by (induction xs) auto
end