Diferencia entre revisiones de «Relación 3»
De Razonamiento automático (2015-16)
(Página creada con '<source lang="isar"> header {* R3: Razonamiento sobre programas en Isabelle/HOL *} theory R3 imports Main begin text {* ------------------------------------------------------...') |
|||
(No se muestran 2 ediciones intermedias de 2 usuarios) | |||
Línea 14: | Línea 14: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
+ | --"angfraalv jospalhid" | ||
fun sumaImpares :: "nat ⇒ nat" where | fun sumaImpares :: "nat ⇒ nat" where | ||
− | + | "sumaImpares 0 = 0" | | |
+ | "sumaImpares (Suc n) = ((2*(Suc n)) - 1) + sumaImpares n" | ||
value "sumaImpares 5" -- "= 25" | value "sumaImpares 5" -- "= 25" | ||
Línea 24: | Línea 26: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
+ | --"angfraalv jospalhid" | ||
lemma "sumaImpares n = n*n" | lemma "sumaImpares n = n*n" | ||
− | + | proof (induct n) | |
+ | show "sumaImpares 0 = 0*0" by simp | ||
+ | next | ||
+ | fix n | ||
+ | assume HI: "sumaImpares n = n*n" | ||
+ | have "sumaImpares (Suc n) = ((2*(Suc n)) - 1) + sumaImpares n" | ||
+ | by simp | ||
+ | also have "... = ((2*(Suc n)) - 1) + n*n " using HI by simp | ||
+ | also have "... = (Suc n) + (Suc n) + (n*n) - 1" by simp | ||
+ | also have "... = (Suc n) + n + (n*n)" by simp | ||
+ | also have "... = (Suc n) + ((Suc n)*n)" by simp | ||
+ | finally show "sumaImpares (Suc n) = (Suc n)*(Suc n)" by simp | ||
+ | qed | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 36: | Línea 51: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
+ | --"angfraalv jospalhid" | ||
fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where | fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where | ||
− | "sumaPotenciasDeDosMasUno n = | + | fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where |
+ | "sumaPotenciasDeDosMasUno 0 = 2 " | | ||
+ | "sumaPotenciasDeDosMasUno (Suc n) = sumaPotenciasDeDosMasUno n + 2^(Suc n)" | ||
value "sumaPotenciasDeDosMasUno 3" -- "= 16" | value "sumaPotenciasDeDosMasUno 3" -- "= 16" | ||
Línea 46: | Línea 64: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
+ | --"angfraalv jospalhid" | ||
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)" | ||
− | + | proof (induct n) | |
+ | show "sumaPotenciasDeDosMasUno 0 = 2^(0+1)" by simp | ||
+ | next | ||
+ | fix n | ||
+ | assume HI: "sumaPotenciasDeDosMasUno n = 2^(n+1)" | ||
+ | have "sumaPotenciasDeDosMasUno (Suc n) = sumaPotenciasDeDosMasUno n + 2^(Suc n)" | ||
+ | by simp | ||
+ | also have "... = 2^(n+1) + 2^(Suc n)" using HI by simp | ||
+ | also have "... = 2*(2^(Suc n))" by simp | ||
+ | finally show "sumaPotenciasDeDosMasUno (Suc n) = 2^((Suc n)+1)" by simp | ||
+ | qed | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 57: | Línea 86: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
+ | --"angfraalv jospalhid" | ||
fun copia :: "nat ⇒ 'a ⇒ 'a list" where | fun copia :: "nat ⇒ 'a ⇒ 'a list" where | ||
− | "copia n x = | + | "copia 0 _ = []" | |
+ | "copia (Suc n) x = x#copia n x" | ||
value "copia 3 x" -- "= [x,x,x]" | value "copia 3 x" -- "= [x,x,x]" | ||
Línea 72: | Línea 103: | ||
----------------------------------------------------------------- *} | ----------------------------------------------------------------- *} | ||
+ | --"angfraalv jospalhid" | ||
fun todos :: "('a ⇒ bool) ⇒ 'a list ⇒ bool" where | fun todos :: "('a ⇒ bool) ⇒ 'a list ⇒ bool" where | ||
− | "todos p xs = | + | "todos _ [] = True " | |
+ | "todos p (x#xs) = (p x ∧ todos p xs)" | ||
value "todos (λx. x>(1::nat)) [2,6,4]" -- "= True" | value "todos (λx. x>(1::nat)) [2,6,4]" -- "= True" | ||
Línea 83: | Línea 116: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
+ | --"angfraalv jospalhid" | ||
lemma "todos (λy. y=x) (copia n x)" | lemma "todos (λy. y=x) (copia n x)" | ||
− | + | proof (induct n) | |
+ | show "todos (λy. y=x) (copia 0 x)" by simp | ||
+ | next | ||
+ | fix n | ||
+ | assume HI: "todos (λy. y=x) (copia n x)" | ||
+ | have "todos (λy. y=x) (copia (Suc n) x) = (((λy. y=x) x) ∧ (todos (λy. y=x) (copia n x)))" by simp | ||
+ | also have "... = (todos (λy. y=x) (copia n x)) " by simp | ||
+ | finally show " todos (λy. y=x) (copia (Suc n) x)" using HI by auto | ||
+ | qed | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 93: | Línea 135: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
+ | --"angfraalv jospalhid" | ||
fun factR :: "nat ⇒ nat" where | fun factR :: "nat ⇒ nat" where | ||
− | "factR n = | + | "factR 0 = 1" | |
+ | "factR (Suc n) = Suc n * factR n" | ||
value "factR 4" -- "= 24" | value "factR 4" -- "= 24" | ||
Línea 113: | Línea 157: | ||
fun factI' :: "nat ⇒ nat ⇒ nat" where | fun factI' :: "nat ⇒ nat ⇒ nat" where | ||
"factI' 0 x = x" | "factI' 0 x = x" | ||
− | | "factI' (Suc n) x = factI' n (Suc n)*x" | + | | "factI' (Suc n) x = factI' n ((Suc n)*x)" |
fun factI :: "nat ⇒ nat" where | fun factI :: "nat ⇒ nat" where | ||
Línea 120: | Línea 164: | ||
value "factI 4" -- "= 24" | value "factI 4" -- "= 24" | ||
+ | --"jospalhid" | ||
lemma fact: "factI' n x = x * factR n" | lemma fact: "factI' n x = x * factR n" | ||
− | + | proof (induct n arbitrary: x) | |
+ | show "⋀x. factI' 0 x = x * factR 0" by simp | ||
+ | next | ||
+ | fix n | ||
+ | assume HI: "⋀x. factI' n x = x * factR n" | ||
+ | show "⋀x. factI' (Suc n) x = x * factR (Suc n)" | ||
+ | proof - | ||
+ | fix x | ||
+ | have "factI' (Suc n) x = factI' n ((Suc n)*x)" by simp | ||
+ | also have "... = (Suc n) * x * factR n" using HI by simp | ||
+ | also have "... = x * factR (Suc n)" by simp | ||
+ | finally show "factI' (Suc n) x = x * factR (Suc n)" by simp | ||
+ | qed | ||
+ | qed | ||
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 128: | Línea 186: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
+ | --"jospalhid" | ||
corollary "factI n = factR n" | corollary "factI n = factR n" | ||
− | + | by (simp add:fact) | |
text {* --------------------------------------------------------------- | text {* --------------------------------------------------------------- | ||
Línea 139: | Línea 198: | ||
------------------------------------------------------------------ *} | ------------------------------------------------------------------ *} | ||
+ | --"angfraalv jospalhid" | ||
fun amplia :: "'a list ⇒ 'a ⇒ 'a list" where | fun amplia :: "'a list ⇒ 'a ⇒ 'a list" where | ||
− | "amplia xs y = | + | "amplia [] y = [y]" | |
− | + | "amplia (x#xs) y = x#(amplia xs y)" | |
value "amplia [d,a] t" -- "= [d,a,t]" | value "amplia [d,a] t" -- "= [d,a,t]" | ||
Línea 149: | Línea 209: | ||
------------------------------------------------------------------- *} | ------------------------------------------------------------------- *} | ||
+ | --"jospalhid" | ||
lemma "amplia xs y = xs @ [y]" | lemma "amplia xs y = xs @ [y]" | ||
− | + | proof (induct xs) | |
+ | show "amplia [] y = [] @ [y]" by simp | ||
+ | next | ||
+ | fix a xs | ||
+ | assume HI: "amplia xs y = xs @ [y]" | ||
+ | have "amplia (a#xs) y = a#(amplia xs y)" by simp | ||
+ | also have "... =a # (xs @ [y])" using HI by simp | ||
+ | also have "... = (a#xs) @ [y]" by simp | ||
+ | finally show "amplia (a#xs) y = (a#xs) @ [y]" by simp | ||
+ | qed | ||
+ | |||
end | end | ||
</source> | </source> |
Revisión actual del 01:57 22 dic 2015
header {* R3: Razonamiento sobre programas en Isabelle/HOL *}
theory R3
imports Main
begin
text {* ---------------------------------------------------------------
Ejercicio 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
------------------------------------------------------------------ *}
--"angfraalv jospalhid"
fun sumaImpares :: "nat ⇒ nat" where
"sumaImpares 0 = 0" |
"sumaImpares (Suc n) = ((2*(Suc n)) - 1) + sumaImpares n"
value "sumaImpares 5" -- "= 25"
text {* ---------------------------------------------------------------
Ejercicio 2. Demostrar que
sumaImpares n = n*n
------------------------------------------------------------------- *}
--"angfraalv jospalhid"
lemma "sumaImpares n = n*n"
proof (induct n)
show "sumaImpares 0 = 0*0" by simp
next
fix n
assume HI: "sumaImpares n = n*n"
have "sumaImpares (Suc n) = ((2*(Suc n)) - 1) + sumaImpares n"
by simp
also have "... = ((2*(Suc n)) - 1) + n*n " using HI by simp
also have "... = (Suc n) + (Suc n) + (n*n) - 1" by simp
also have "... = (Suc n) + n + (n*n)" by simp
also have "... = (Suc n) + ((Suc n)*n)" by simp
finally show "sumaImpares (Suc n) = (Suc n)*(Suc n)" by simp
qed
text {* ---------------------------------------------------------------
Ejercicio 3. 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
------------------------------------------------------------------ *}
--"angfraalv jospalhid"
fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where
fun sumaPotenciasDeDosMasUno :: "nat ⇒ nat" where
"sumaPotenciasDeDosMasUno 0 = 2 " |
"sumaPotenciasDeDosMasUno (Suc n) = sumaPotenciasDeDosMasUno n + 2^(Suc n)"
value "sumaPotenciasDeDosMasUno 3" -- "= 16"
text {* ---------------------------------------------------------------
Ejercicio 4. Demostrar que
sumaPotenciasDeDosMasUno n = 2^(n+1)
------------------------------------------------------------------- *}
--"angfraalv jospalhid"
lemma "sumaPotenciasDeDosMasUno n = 2^(n+1)"
proof (induct n)
show "sumaPotenciasDeDosMasUno 0 = 2^(0+1)" by simp
next
fix n
assume HI: "sumaPotenciasDeDosMasUno n = 2^(n+1)"
have "sumaPotenciasDeDosMasUno (Suc n) = sumaPotenciasDeDosMasUno n + 2^(Suc n)"
by simp
also have "... = 2^(n+1) + 2^(Suc n)" using HI by simp
also have "... = 2*(2^(Suc n))" by simp
finally show "sumaPotenciasDeDosMasUno (Suc n) = 2^((Suc n)+1)" by simp
qed
text {* ---------------------------------------------------------------
Ejercicio 5. 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]
------------------------------------------------------------------ *}
--"angfraalv jospalhid"
fun copia :: "nat ⇒ 'a ⇒ 'a list" where
"copia 0 _ = []" |
"copia (Suc n) x = x#copia n x"
value "copia 3 x" -- "= [x,x,x]"
text {* ---------------------------------------------------------------
Ejercicio 6. 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 ∧
----------------------------------------------------------------- *}
--"angfraalv jospalhid"
fun todos :: "('a ⇒ bool) ⇒ 'a list ⇒ bool" where
"todos _ [] = 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 7. Demostrar que todos los elementos de (copia n x) son
iguales a x.
------------------------------------------------------------------- *}
--"angfraalv jospalhid"
lemma "todos (λy. y=x) (copia n x)"
proof (induct n)
show "todos (λy. y=x) (copia 0 x)" by simp
next
fix n
assume HI: "todos (λy. y=x) (copia n x)"
have "todos (λy. y=x) (copia (Suc n) x) = (((λy. y=x) x) ∧ (todos (λy. y=x) (copia n x)))" by simp
also have "... = (todos (λy. y=x) (copia n x)) " by simp
finally show " todos (λy. y=x) (copia (Suc n) x)" using HI by auto
qed
text {* ---------------------------------------------------------------
Ejercicio 8. Definir la función
factR :: nat ⇒ nat
tal que (factR n) es el factorial de n. Por ejemplo,
factR 4 = 24
------------------------------------------------------------------ *}
--"angfraalv jospalhid"
fun factR :: "nat ⇒ nat" where
"factR 0 = 1" |
"factR (Suc n) = Suc n * factR n"
value "factR 4" -- "= 24"
text {* ---------------------------------------------------------------
Ejercicio 9. Se considera la siguiente definición iterativa de la
función factorial
factI :: "nat ⇒ nat" where
factI n = factI' n 1
factI' :: nat ⇒ nat ⇒ nat" where
factI' 0 x = x
factI' (Suc n) x = factI' n (Suc n)*x
Demostrar que, para todo n y todo x, se tiene
factI' n x = x * factR n
------------------------------------------------------------------- *}
fun factI' :: "nat ⇒ nat ⇒ nat" where
"factI' 0 x = x"
| "factI' (Suc n) x = factI' n ((Suc n)*x)"
fun factI :: "nat ⇒ nat" where
"factI n = factI' n 1"
value "factI 4" -- "= 24"
--"jospalhid"
lemma fact: "factI' n x = x * factR n"
proof (induct n arbitrary: x)
show "⋀x. factI' 0 x = x * factR 0" by simp
next
fix n
assume HI: "⋀x. factI' n x = x * factR n"
show "⋀x. factI' (Suc n) x = x * factR (Suc n)"
proof -
fix x
have "factI' (Suc n) x = factI' n ((Suc n)*x)" by simp
also have "... = (Suc n) * x * factR n" using HI by simp
also have "... = x * factR (Suc n)" by simp
finally show "factI' (Suc n) x = x * factR (Suc n)" by simp
qed
qed
text {* ---------------------------------------------------------------
Ejercicio 10. Demostrar que
factI n = factR n
------------------------------------------------------------------- *}
--"jospalhid"
corollary "factI n = factR n"
by (simp add:fact)
text {* ---------------------------------------------------------------
Ejercicio 11. 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]
------------------------------------------------------------------ *}
--"angfraalv jospalhid"
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 12. Demostrar que
amplia xs y = xs @ [y]
------------------------------------------------------------------- *}
--"jospalhid"
lemma "amplia xs y = xs @ [y]"
proof (induct xs)
show "amplia [] y = [] @ [y]" by simp
next
fix a xs
assume HI: "amplia xs y = xs @ [y]"
have "amplia (a#xs) y = a#(amplia xs y)" by simp
also have "... =a # (xs @ [y])" using HI by simp
also have "... = (a#xs) @ [y]" by simp
finally show "amplia (a#xs) y = (a#xs) @ [y]" by simp
qed
end