Pruebas de length (repeat x n) = n
En Lean están definidas las funciones length y repeat tales que
- (length xs) es la longitud de la lista xs. Por ejemplo,
| 1 |      length [1,2,5,2] = 4 | 
- (repeat x n) es la lista que tiene el elemento x n veces. Por ejemplo,
| 1 |      repeat 7 3 = [7, 7, 7] | 
Demostrar que
| 1 |    length (repeat x n) = n | 
Para ello, completar la siguiente teoría de Lean:
| 1 2 3 4 5 6 7 8 9 10 11 | import data.list.basic open nat open list variable {α : Type} variable (x : α) variable (n : ℕ) example :   length (repeat x n) = n := sorry | 
[expand title=»Soluciones con Lean»]
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | import data.list.basic open nat open list set_option pp.structure_projections false variable {α : Type} variable (x : α) variable (n : ℕ) -- 1ª demostración example :   length (repeat x n) = n := begin   induction n with n HI,   { calc length (repeat x 0)           = length []                : congr_arg length (repeat.equations._eqn_1 x)       ... = 0                        : length.equations._eqn_1 },   { calc length (repeat x (succ n))          = length (x :: repeat x n)  : congr_arg length (repeat.equations._eqn_2 x n)      ... = length (repeat x n) + 1   : length.equations._eqn_2 x (repeat x n)      ... = n + 1                     : congr_arg2 (+) HI rfl      ... = succ n                    : rfl, }, end -- 2ª demostración example :   length (repeat x n) = n := begin   induction n with n HI,   { calc length (repeat x 0)           = length []                : rfl       ... = 0                        : rfl },   { calc length (repeat x (succ n))          = length (x :: repeat x n)  : rfl      ... = length (repeat x n) + 1   : rfl      ... = n + 1                     : by rw HI      ... = succ n                    : rfl, }, end -- 3ª demostración example : length (repeat x n) = n := begin   induction n with n HI,   { refl, },   { dsimp,     rw HI, }, end -- 4ª demostración example : length (repeat x n) = n := begin   induction n with n HI,   { simp, },   { simp [HI], }, end -- 5ª demostración example : length (repeat x n) = n := by induction n ; simp [*] -- 6ª demostración example : length (repeat x n) = n := nat.rec_on n   ( show length (repeat x 0) = 0, from       calc length (repeat x 0)            = length []                : rfl        ... = 0                        : rfl )   ( assume n,     assume HI : length (repeat x n) = n,     show length (repeat x (succ n)) = succ n, from       calc length (repeat x (succ n))            = length (x :: repeat x n) : rfl        ... = length (repeat x n) + 1  : rfl        ... = n + 1                    : by rw HI        ... = succ n                   : rfl ) -- 7ª demostración example : length (repeat x n) = n := nat.rec_on n   ( by simp )   ( λ n HI, by simp [HI]) -- 8ª demostración example : length (repeat x n) = n := length_repeat x n -- 9ª demostración example : length (repeat x n) = n := by simp -- 10ª demostración lemma length_repeat_1 :   ∀ n, length (repeat x n) = n | 0 := by calc length (repeat x 0)                = length ([] : list α)         : rfl            ... = 0                            : rfl | (n+1) := by calc length (repeat x (n + 1))                    = length (x :: repeat x n) : rfl                ... = length (repeat x n) + 1  : rfl                ... = n + 1                    : by rw length_repeat_1 -- 11ª demostración lemma length_repeat_2 :   ∀ n, length (repeat x n) = n | 0     := by simp | (n+1) := by simp [*] | 
Se puede interactuar con la prueba anterior en esta sesión con Lean.
En los comentarios se pueden escribir otras soluciones, escribiendo el código entre una línea con <pre lang="lean"> y otra con </pre>
[/expand]
[expand title=»Soluciones con Isabelle/HOL»]
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | theory "Pruebas_de_length_(repeat_x_n)_Ig_n" imports Main begin (* 1ª demostración⁾*) lemma "length (replicate n x) = n" proof (induct n)   have "length (replicate 0 x) = length ([] :: 'a list)"     by (simp only: replicate.simps(1))   also have "… = 0"     by (rule list.size(3))   finally show "length (replicate 0 x) = 0"     by this next   fix n   assume HI : "length (replicate n x) = n"   have "length (replicate (Suc n) x) =         length (x # replicate n x)"     by (simp only: replicate.simps(2))   also have "… = length (replicate n x) + 1"     by (simp only: list.size(4))   also have "… = Suc n"     by (simp only: HI)   finally show "length (replicate (Suc n) x) = Suc n"     by this qed (* 2ª demostración⁾*) lemma "length (replicate n x) = n" proof (induct n)   show "length (replicate 0 x) = 0"     by simp next   fix n   assume "length (replicate n x) = n"   then show "length (replicate (Suc n) x) = Suc n"     by simp qed (* 3ª demostración⁾*) lemma "length (replicate n x) = n" proof (induct n)   case 0   then show ?case by simp next   case (Suc n)   then show ?case by simp qed (* 4ª demostración⁾*) lemma "length (replicate n x) = n"   by (rule length_replicate) end | 
En los comentarios se pueden escribir otras soluciones, escribiendo el código entre una línea con <pre lang="isar"> y otra con </pre>
[/expand]