        {"id":742,"date":"2021-09-07T06:00:24","date_gmt":"2021-09-07T04:00:24","guid":{"rendered":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/?p=742"},"modified":"2021-09-08T10:56:00","modified_gmt":"2021-09-08T08:56:00","slug":"pruebas-de-length-repeat-x-n-n","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/pruebas-de-length-repeat-x-n-n\/","title":{"rendered":"Pruebas de length (repeat x n) = n"},"content":{"rendered":"<p>En Lean est\u00e1n definidas las funciones length y repeat tales que<\/p>\n<ul>\n<li>(length xs) es la longitud de la lista xs. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     length [1,2,5,2] = 4\n<\/pre>\n<ul>\n<li>(repeat x n) es la lista que tiene el elemento x n veces. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     repeat 7 3 = [7, 7, 7]\n<\/pre>\n<p>Demostrar que<\/p>\n<pre lang=\"text\">\n   length (repeat x n) = n\n<\/pre>\n<p>Para ello, completar la siguiente teor\u00eda de Lean:<\/p>\n<pre lang=\"lean\">\nimport data.list.basic\nopen nat\nopen list\n\nvariable {\u03b1 : Type}\nvariable (x : \u03b1)\nvariable (n : \u2115)\n\nexample :\n  length (repeat x n) = n :=\nsorry\n<\/pre>\n<p>[expand title=\u00bbSoluciones con Lean\u00bb]<\/p>\n<pre lang=\"lean\">\r\nimport data.list.basic\r\nopen nat\r\nopen list\r\n\r\nset_option pp.structure_projections false\r\n\r\nvariable {\u03b1 : Type}\r\nvariable (x : \u03b1)\r\nvariable (n : \u2115)\r\n\r\n-- 1\u00aa demostraci\u00f3n\r\nexample :\r\n  length (repeat x n) = n :=\r\nbegin\r\n  induction n with n HI,\r\n  { calc length (repeat x 0)\r\n          = length []                : congr_arg length (repeat.equations._eqn_1 x)\r\n      ... = 0                        : length.equations._eqn_1 },\r\n  { calc length (repeat x (succ n))\r\n         = length (x :: repeat x n)  : congr_arg length (repeat.equations._eqn_2 x n)\r\n     ... = length (repeat x n) + 1   : length.equations._eqn_2 x (repeat x n)\r\n     ... = n + 1                     : congr_arg2 (+) HI rfl\r\n     ... = succ n                    : rfl, },\r\nend\r\n\r\n-- 2\u00aa demostraci\u00f3n\r\nexample :\r\n  length (repeat x n) = n :=\r\nbegin\r\n  induction n with n HI,\r\n  { calc length (repeat x 0)\r\n          = length []                : rfl\r\n      ... = 0                        : rfl },\r\n  { calc length (repeat x (succ n))\r\n         = length (x :: repeat x n)  : rfl\r\n     ... = length (repeat x n) + 1   : rfl\r\n     ... = n + 1                     : by rw HI\r\n     ... = succ n                    : rfl, },\r\nend\r\n\r\n-- 3\u00aa demostraci\u00f3n\r\nexample : length (repeat x n) = n :=\r\nbegin\r\n  induction n with n HI,\r\n  { refl, },\r\n  { dsimp,\r\n    rw HI, },\r\nend\r\n\r\n-- 4\u00aa demostraci\u00f3n\r\nexample : length (repeat x n) = n :=\r\nbegin\r\n  induction n with n HI,\r\n  { simp, },\r\n  { simp [HI], },\r\nend\r\n\r\n-- 5\u00aa demostraci\u00f3n\r\nexample : length (repeat x n) = n :=\r\nby induction n ; simp [*]\r\n\r\n-- 6\u00aa demostraci\u00f3n\r\nexample : length (repeat x n) = n :=\r\nnat.rec_on n\r\n  ( show length (repeat x 0) = 0, from\r\n      calc length (repeat x 0)\r\n           = length []                : rfl\r\n       ... = 0                        : rfl )\r\n  ( assume n,\r\n    assume HI : length (repeat x n) = n,\r\n    show length (repeat x (succ n)) = succ n, from\r\n      calc length (repeat x (succ n))\r\n           = length (x :: repeat x n) : rfl\r\n       ... = length (repeat x n) + 1  : rfl\r\n       ... = n + 1                    : by rw HI\r\n       ... = succ n                   : rfl )\r\n\r\n-- 7\u00aa demostraci\u00f3n\r\nexample : length (repeat x n) = n :=\r\nnat.rec_on n\r\n  ( by simp )\r\n  ( \u03bb n HI, by simp [HI])\r\n\r\n-- 8\u00aa demostraci\u00f3n\r\nexample : length (repeat x n) = n :=\r\nlength_repeat x n\r\n\r\n-- 9\u00aa demostraci\u00f3n\r\nexample : length (repeat x n) = n :=\r\nby simp\r\n\r\n-- 10\u00aa demostraci\u00f3n\r\nlemma length_repeat_1 :\r\n  \u2200 n, length (repeat x n) = n\r\n| 0 := by calc length (repeat x 0)\r\n               = length ([] : list \u03b1)         : rfl\r\n           ... = 0                            : rfl\r\n| (n+1) := by calc length (repeat x (n + 1))\r\n                   = length (x :: repeat x n) : rfl\r\n               ... = length (repeat x n) + 1  : rfl\r\n               ... = n + 1                    : by rw length_repeat_1\r\n\r\n-- 11\u00aa demostraci\u00f3n\r\nlemma length_repeat_2 :\r\n  \u2200 n, length (repeat x n) = n\r\n| 0     := by simp\r\n| (n+1) := by simp [*]\r\n<\/pre>\n<p>Se puede interactuar con la prueba anterior en <a href=\"https:\/\/leanprover-community.github.io\/lean-web-editor\/#url=https:\/\/raw.githubusercontent.com\/jaalonso\/Calculemus\/main\/src\/Pruebas_de_length_(repeat_x_n)_Ig_n.lean\" rel=\"noopener noreferrer\" target=\"_blank\">esta sesi\u00f3n con Lean<\/a>.<\/p>\n<p>En los comentarios se pueden escribir otras soluciones, escribiendo el c\u00f3digo entre una l\u00ednea con &#60;pre lang=&quot;lean&quot;&#62; y otra con &#60;\/pre&#62;<br \/>\n[\/expand]<\/p>\n<p>[expand title=\u00bbSoluciones con Isabelle\/HOL\u00bb]<\/p>\n<pre lang=\"isar\">\r\ntheory \"Pruebas_de_length_(repeat_x_n)_Ig_n\"\r\nimports Main\r\nbegin\r\n\r\n(* 1\u00aa demostraci\u00f3n\u207e*)\r\nlemma \"length (replicate n x) = n\"\r\nproof (induct n)\r\n  have \"length (replicate 0 x) = length ([] :: 'a list)\"\r\n    by (simp only: replicate.simps(1))\r\n  also have \"\u2026 = 0\"\r\n    by (rule list.size(3))\r\n  finally show \"length (replicate 0 x) = 0\"\r\n    by this\r\nnext\r\n  fix n\r\n  assume HI : \"length (replicate n x) = n\"\r\n  have \"length (replicate (Suc n) x) =\r\n        length (x # replicate n x)\"\r\n    by (simp only: replicate.simps(2))\r\n  also have \"\u2026 = length (replicate n x) + 1\"\r\n    by (simp only: list.size(4))\r\n  also have \"\u2026 = Suc n\"\r\n    by (simp only: HI)\r\n  finally show \"length (replicate (Suc n) x) = Suc n\"\r\n    by this\r\nqed\r\n\r\n(* 2\u00aa demostraci\u00f3n\u207e*)\r\nlemma \"length (replicate n x) = n\"\r\nproof (induct n)\r\n  show \"length (replicate 0 x) = 0\"\r\n    by simp\r\nnext\r\n  fix n\r\n  assume \"length (replicate n x) = n\"\r\n  then show \"length (replicate (Suc n) x) = Suc n\"\r\n    by simp\r\nqed\r\n\r\n(* 3\u00aa demostraci\u00f3n\u207e*)\r\nlemma \"length (replicate n x) = n\"\r\nproof (induct n)\r\n  case 0\r\n  then show ?case by simp\r\nnext\r\n  case (Suc n)\r\n  then show ?case by simp\r\nqed\r\n\r\n(* 4\u00aa demostraci\u00f3n\u207e*)\r\nlemma \"length (replicate n x) = n\"\r\n  by (rule length_replicate)\r\n\r\nend\r\n<\/pre>\n<p>En los comentarios se pueden escribir otras soluciones, escribiendo el c\u00f3digo entre una l\u00ednea con &#60;pre lang=&quot;isar&quot;&#62; y otra con &#60;\/pre&#62;<br \/>\n[\/expand]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En Lean est\u00e1n definidas las funciones length y repeat tales que (length xs) es la longitud de la lista xs. Por ejemplo, length [1,2,5,2] = 4 (repeat x n) es la lista que tiene el elemento x n veces. Por ejemplo, repeat 7 3 = [7, 7, 7] Demostrar que length (repeat x n) = n Para ello, completar la siguiente teor\u00eda de Lean: import data.list.basic open nat open list variable {\u03b1 : Type} variable (x : \u03b1) variable (n : \u2115) example : length (repeat x n) = n := sorry [expand title=\u00bbSoluciones con Lean\u00bb] import data.list.basic open nat open list set_option pp.structure_projections false variable {\u03b1 : Type} variable (x : \u03b1) variable (n : \u2115) &#8212; 1\u00aa demostraci\u00f3n example : length (repeat x&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[105,227],"tags":[177,231,235,233,232,234,51,100,98,238,84,229,169,228,230,273,275,174,47,272,274,147,111,99],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/posts\/742"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/comments?post=742"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/posts\/742\/revisions"}],"predecessor-version":[{"id":754,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/posts\/742\/revisions\/754"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/media?parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/categories?post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/tags?post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}