RA2014: Ejercicios de cuantificadores sobre listas con Isabelle/HOL
En la segunda parte de la clase de hoy del curso de Razonamiento automático se ha comentado las soluciones de la 5ª relación de ejercicios cuyo objetivo es demostrar con Isabelle/HOL propiedades de programas con cuantificadores sobre listas.
Los ejercicios y sus soluciones se muestran a continuación
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 |
header {* R5: Cuantificadores sobre listas *} theory R5 imports Main begin text {* --------------------------------------------------------------------- Ejercicio 1. Definir la función todos :: ('a ⇒ bool) ⇒ 'a list ⇒ bool tal que (todos p xs) se verifica si todos los elementos de la lista xs cumplen la propiedad p. Por ejemplo, se verifica todos (λx. 1 < length x) [[2,1,4],[1,3]] ¬todos (λx. 1 < length x) [[2,1,4],[3]] Nota: La función todos es equivalente a la predefinida list_all. --------------------------------------------------------------------- *} fun todos :: "('a ⇒ bool) ⇒ 'a list ⇒ bool" where "todos p [] = True" | "todos p (y#ys) = ((p y) ∧ (todos p ys))" value "todos (λx. 1 < length x) [[2,1,4],[1,3]]" -- "= True" value "todos (λx. 1 < length x) [[2,1,4],[3]]" -- "= False" text {* --------------------------------------------------------------------- Ejercicio 2. Definir la función algunos :: ('a ⇒ bool) ⇒ 'a list ⇒ bool tal que (algunos p xs) se verifica si algunos elementos de la lista xs cumplen la propiedad p. Por ejemplo, se verifica algunos (λx. 1 < length x) [[2,1,4],[3]] ¬algunos (λx. 1 < length x) [[],[3]]" Nota: La función algunos es equivalente a la predefinida list_ex. --------------------------------------------------------------------- *} fun algunos :: "('a ⇒ bool) ⇒ 'a list ⇒ bool" where "algunos p [] = False" | "algunos p (x#xs) = ((p x) ∨ (algunos p xs))" value "algunos (λx. 1 < length x) [[2,1,4],[3]]" -- "= True" value "algunos (λx. 1 < length x) [[],[3]]" -- "= False" text {* --------------------------------------------------------------------- Ejercicio 3.1. Demostrar o refutar automáticamente todos (λx. P x ∧ Q x) xs = (todos P xs ∧ todos Q xs) --------------------------------------------------------------------- *} -- "La demostración automática es" lemma "todos (λx. P x ∧ Q x) xs = (todos P xs ∧ todos Q xs)" by (induct xs) auto text {* --------------------------------------------------------------------- Ejercicio 3.2. Demostrar o refutar detalladamente todos (λx. P x ∧ Q x) xs = (todos P xs ∧ todos Q xs) --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma "todos (λx. P x ∧ Q x) xs = (todos P xs ∧ todos Q xs)" proof (induct xs) show "todos (λx. P x ∧ Q x) [] = (todos P [] ∧ todos Q [])" by simp next fix a xs assume "todos (λx. P x ∧ Q x) xs = (todos P xs ∧ todos Q xs)" thus "todos (λx. P x ∧ Q x) (a#xs) = (todos P (a#xs) ∧ todos Q (a#xs))" by auto qed -- "La demostración detallada es" lemma "todos (λx. P x ∧ Q x) xs = (todos P xs ∧ todos Q xs)" proof (induct xs) show "todos (λx. P x ∧ Q x) [] = (todos P [] ∧ todos Q [])" by simp next fix a xs assume HI: "todos (λx. P x ∧ Q x) xs = (todos P xs ∧ todos Q xs)" show "todos (λx. P x ∧ Q x) (a#xs) = (todos P (a#xs) ∧ todos Q (a#xs))" proof - have "todos (λx. P x ∧ Q x) (a#xs) = ((P a) ∧ (Q a) ∧ todos (λx. P x ∧ Q x) xs)" by simp also have "… = ((P a) ∧ (Q a) ∧ todos P xs ∧ todos Q xs)" using HI by simp also have "… = (((P a) ∧ todos P xs) ∧ ((Q a) ∧ todos Q xs))" by auto also have "… = (todos P (a#xs) ∧ todos Q (a#xs))" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 4.1. Demostrar o refutar automáticamente todos P (x @ y) = (todos P x ∧ todos P y) --------------------------------------------------------------------- *} -- "La demostración automática es" lemma todos_append: "todos P (x @ y) = (todos P x ∧ todos P y)" by (induct x) simp_all text {* --------------------------------------------------------------------- Ejercicio 4.2. Demostrar o refutar detalladamente todos P (x @ y) = (todos P x ∧ todos P y) --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma todos_append_2: "todos P (x @ y) = (todos P x ∧ todos P y)" proof (induct x) show "todos P ([] @ y) = (todos P [] ∧ todos P y)" by simp next fix a x assume "todos P (x @ y) = (todos P x ∧ todos P y)" thus "todos P ((a#x) @ y) = (todos P (a#x) ∧ todos P y)" by auto qed -- "La demostración detallada es" lemma todos_append_3: "todos P (x @ y) = (todos P x ∧ todos P y)" proof (induct x) show "todos P ([] @ y) = (todos P [] ∧ todos P y)" by simp next fix a x assume HI: "todos P (x @ y) = (todos P x ∧ todos P y)" show "todos P ((a#x) @ y) = (todos P (a#x) ∧ todos P y)" proof - have "todos P ((a#x) @ y) = ((P a) ∧ todos P (x@y))" by simp also have "… = ((P a) ∧ todos P x ∧ todos P y)" using HI by simp also have "… = (todos P (a#x) ∧ todos P y)" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 5.1. Demostrar o refutar automáticamente todos P (rev xs) = todos P xs --------------------------------------------------------------------- *} -- "La demostración automática es" lemma "todos P (rev xs) = todos P xs" by (induct xs) (auto simp add: todos_append) text {* --------------------------------------------------------------------- Ejercicio 5.2. Demostrar o refutar detalladamente todos P (rev xs) = todos P xs --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma "todos P (rev xs) = todos P xs" proof (induct xs) show "todos P (rev []) = todos P []" by simp next fix a xs assume "todos P (rev xs) = todos P xs" thus "todos P (rev (a#xs)) = todos P (a#xs)" by (auto simp add: todos_append) qed -- "La demostración detallada es" lemma "todos P (rev xs) = todos P xs" proof (induct xs) show "todos P (rev []) = todos P []" by simp next fix a xs assume HI: "todos P (rev xs) = todos P xs" show "todos P (rev (a#xs)) = todos P (a#xs)" proof - have "todos P (rev (a#xs)) = todos P ((rev xs)@[a])" by simp also have "… = (todos P (rev xs) ∧ todos P [a])" by (simp add: todos_append) also have "… = (todos P xs ∧ todos P [a])" using HI by simp also have "… = (todos P [a] ∧ todos P xs)" by auto also have "… = (P a ∧ todos P xs)" by simp also have "… = todos P (a#xs)" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 6. Demostrar o refutar: algunos (λx. P x ∧ Q x) xs = (algunos P xs ∧ algunos Q xs) --------------------------------------------------------------------- *} -- "Se busca un contraejemplo con nitpick" lemma "algunos (λx. P x ∧ Q x) xs = (algunos P xs ∧ algunos Q xs)" nitpick oops text {* El contraejemplo encontrado es Nitpick found a counterexample for card 'a = 3: Free variables: P = (λx. _)(a⇘1⇙ := True, a⇘2⇙ := True, a⇘3⇙ := False) Q = (λx. _)(a⇘1⇙ := False, a⇘2⇙ := False, a⇘3⇙ := True) xs = [a⇘3⇙, a⇘2⇙] *} text {* --------------------------------------------------------------------- Ejercicio 7.1. Demostrar o refutar automáticamente algunos P (map f xs) = algunos (P ∘ f) xs --------------------------------------------------------------------- *} -- "La demostración automática es" lemma "algunos P (map f xs) = algunos (P o f) xs" by (induct xs) simp_all -- "La demostración estructurada es" lemma "algunos P (map f xs) = algunos (P ∘ f) xs" proof (induct xs) show "algunos P (map f []) = algunos (P ∘ f) []" by simp next fix a xs assume "algunos P (map f xs) = algunos (P ∘ f) xs" thus "algunos P (map f (a#xs)) = algunos (P ∘ f) (a#xs)" by auto qed text {* --------------------------------------------------------------------- Ejercicio 7.2. Demostrar o refutar detalladamente algunos P (map f xs) = algunos (P ∘ f) xs --------------------------------------------------------------------- *} -- "La demostración detallada es" lemma "algunos P (map f xs) = algunos (P ∘ f) xs" proof (induct xs) show "algunos P (map f []) = algunos (P ∘ f) []" by simp next fix a xs assume HI: "algunos P (map f xs) = algunos (P ∘ f) xs" show "algunos P (map f (a#xs)) = algunos (P ∘ f) (a#xs)" proof - have "algunos P (map f (a#xs)) = algunos P ((f a)#(map f xs))" by simp also have "… = ((P (f a)) ∨ (algunos P (map f xs)))" by simp also have "… = (((P ∘ f) a) ∨ (algunos (P ∘ f) xs))" using HI by simp also have "… = algunos (P ∘ f) (a#xs)" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 8.1. Demostrar o refutar automáticamente algunos P (xs @ ys) = (algunos P xs ∨ algunos P ys) --------------------------------------------------------------------- *} -- "La demostración automática es" lemma algunos_append: "algunos P (xs @ ys) = (algunos P xs ∨ algunos P ys)" by (induct xs) simp_all text {* --------------------------------------------------------------------- Ejercicio 8.2. Demostrar o refutar detalladamente algunos P (xs @ ys) = (algunos P xs ∨ algunos P ys) --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma algunos_append_2: "algunos P (xs @ ys) = (algunos P xs ∨ algunos P ys)" proof (induct xs) show "algunos P ([] @ ys) = (algunos P [] ∨ algunos P ys)" by simp next fix a xs assume "algunos P (xs @ ys) = (algunos P xs ∨ algunos P ys)" thus "algunos P ((a#xs) @ ys) = (algunos P (a#xs) ∨ algunos P ys)" by auto qed -- "La demostración detallada es" lemma algunos_append_3: "algunos P (xs @ ys) = (algunos P xs ∨ algunos P ys)" proof (induct xs) show "algunos P ([] @ ys) = (algunos P [] ∨ algunos P ys)" by simp next fix a xs assume HI: "algunos P (xs @ ys) = (algunos P xs ∨ algunos P ys)" show "algunos P ((a#xs) @ ys) = (algunos P (a#xs) ∨ algunos P ys)" proof - have "algunos P ((a#xs) @ ys) = algunos P (a#(xs @ ys))" by simp also have "… = ((P a) ∨ algunos P (xs @ ys))" by simp also have "… = ((P a) ∨ algunos P xs ∨ algunos P ys)" using HI by simp also have "… = (algunos P (a#xs) ∨ algunos P ys)" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 9.1. Demostrar o refutar automáticamente algunos P (rev xs) = algunos P xs --------------------------------------------------------------------- *} -- "La demostración automática es" lemma "algunos P (rev xs) = algunos P xs" by (induct xs) (auto simp add: algunos_append) text {* --------------------------------------------------------------------- Ejercicio 9.2. Demostrar o refutar detalladamente algunos P (rev xs) = algunos P xs --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma "algunos P (rev xs) = algunos P xs" proof (induct xs) show "algunos P (rev []) = algunos P []" by simp next fix a xs assume "algunos P (rev xs) = algunos P xs" thus "algunos P (rev (a#xs)) = algunos P (a#xs)" by (auto simp add: algunos_append) qed -- "La demostración detallada es" lemma "algunos P (rev xs) = algunos P xs" proof (induct xs) show "algunos P (rev []) = algunos P []" by simp next fix a xs assume HI: "algunos P (rev xs) = algunos P xs" show "algunos P (rev (a#xs)) = algunos P (a#xs)" proof - have "algunos P (rev (a#xs)) = algunos P ((rev xs) @ [a])" by simp also have "… = (algunos P (rev xs) ∨ algunos P [a])" by (simp add: algunos_append) also have "… = (algunos P xs ∨ algunos P [a])" using HI by simp also have "… = (algunos P xs ∨ P a)" by simp also have "… = (P a ∨ algunos P xs)" by auto also have "… = algunos P (a#xs)" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 10. Encontrar un término no trivial Z tal que sea cierta la siguiente ecuación: algunos (λx. P x ∨ Q x) xs = Z y demostrar la equivalencia de forma automática y detallada. --------------------------------------------------------------------- *} text {* Solución: La ecuación se verifica eligiendo como Z el término algunos P xs ∨ algunos Q xs En efecto, *} lemma "algunos (λx. P x ∨ Q x) xs = (algunos P xs ∨ algunos Q xs)" by (induct xs) auto -- "De forma estructurada" lemma "algunos (λx. P x ∨ Q x) xs = (algunos P xs ∨ algunos Q xs)" proof (induct xs) show "algunos (λx. P x ∨ Q x) [] = (algunos P [] ∨ algunos Q [])" by simp next fix a xs assume "algunos (λx. (P x ∨ Q x)) xs = (algunos P xs ∨ algunos Q xs)" thus "algunos (λx. P x ∨ Q x) (a#xs) = (algunos P (a#xs) ∨ algunos Q (a#xs))" by auto qed -- "De forma detallada" lemma "algunos (λx. P x ∨ Q x) xs = (algunos P xs ∨ algunos Q xs)" proof (induct xs) show "algunos (λx. P x ∨ Q x) [] = (algunos P [] ∨ algunos Q [])" by simp next fix a xs assume HI: "algunos (λx. (P x ∨ Q x)) xs = (algunos P xs ∨ algunos Q xs)" show "algunos (λx. P x ∨ Q x) (a#xs) = (algunos P (a#xs) ∨ algunos Q (a#xs))" proof - have "algunos (λx. P x ∨ Q x) (a#xs) = ((P a) ∨ (Q a) ∨ algunos (λx. P x ∨ Q x) xs)" by simp also have "… = ((P a) ∨ (Q a) ∨ algunos P xs ∨ algunos Q xs)" using HI by simp also have "… = (((P a) ∨ algunos P xs) ∨ ((Q a) ∨ algunos Q xs))" by auto also have "… = (algunos P (a#xs) ∨ algunos Q (a#xs))" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 11.1. Demostrar o refutar automáticamente algunos P xs = (¬ todos (λx. (¬ P x)) xs) --------------------------------------------------------------------- *} -- "La demostración automática es" lemma "algunos P xs = (¬ todos (λx. (¬ P x)) xs)" by (induct xs) simp_all text {* --------------------------------------------------------------------- Ejercicio 11.2. Demostrar o refutar detalladamente algunos P xs = (¬ todos (λx. (¬ P x)) xs) --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma "algunos P xs = (¬ todos (λx. (¬ P x)) xs)" proof (induct xs) show "algunos P [] = (¬ todos (λx. (¬ P x)) [])" by simp next fix a xs assume "algunos P xs = (¬ todos (λx. (¬ P x)) xs)" thus "algunos P (a#xs) = (¬ todos (λx. (¬ P x)) (a#xs))" by auto qed -- "La demostración detallada es" lemma "algunos P xs = (¬ todos (λx. (¬ P x)) xs)" proof (induct xs) show "algunos P [] = (¬ todos (λx. (¬ P x)) [])" by simp next fix a xs assume HI: "algunos P xs = (¬ todos (λx. (¬ P x)) xs)" show "algunos P (a#xs) = (¬ todos (λx. (¬ P x)) (a#xs))" proof - have "algunos P (a#xs) = ((P a) ∨ algunos P xs)" by simp also have "… = ((P a) ∨ ¬ todos (λx. (¬ P x)) xs)" using HI by simp also have "… = (¬ (¬ (P a) ∧ todos (λx. (¬ P x)) xs))" by simp also have "… = (¬ todos (λx. (¬ P x)) (a#xs))" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 12. Definir la funcion primitiva recursiva estaEn :: 'a ⇒ 'a list ⇒ bool tal que (estaEn x xs) se verifica si el elemento x está en la lista xs. Por ejemplo, estaEn (2::nat) [3,2,4] = True estaEn (1::nat) [3,2,4] = False --------------------------------------------------------------------- *} fun estaEn :: "'a ⇒ 'a list ⇒ bool" where "estaEn x [] = False" | "estaEn x (a#xs) = (x=a ∨ estaEn x xs)" value "estaEn (2::nat) [3,2,4]" -- "= True" value "estaEn (1::nat) [3,2,4]" -- "= False" text {* --------------------------------------------------------------------- Ejercicio 13. Expresar la relación existente entre estaEn y algunos. Demostrar dicha relación de forma automática y detallada. --------------------------------------------------------------------- *} text {* Solución: La relación es estaEn y xs = algunos (λx. x=y) xs En efecto, *} lemma estaEn_algunos: "estaEn y xs = algunos (λx. x=y) xs" by (induct xs) auto -- "La demostración estructurada es" lemma estaEn_algunos_2: "estaEn y xs = algunos (λx. x=y) xs" proof (induct xs) show "estaEn y [] = algunos (λx. x=y) []" by simp next fix a xs assume "estaEn y xs = algunos (λx. x=y) xs" thus "estaEn y (a#xs) = algunos (λx. x=y) (a#xs)" by auto qed -- "La demostración detallada es" lemma estaEn_algunos_3: "estaEn y xs = algunos (λx. x=y) xs" proof (induct xs) show "estaEn y [] = algunos (λx. x=y) []" by simp next fix a xs assume HI: "estaEn y xs = algunos (λx. x=y) xs" show "estaEn y (a#xs) = algunos (λx. x=y) (a#xs)" proof - have "estaEn y (a#xs) = (y=a ∨ estaEn y xs)" by simp also have "… = (y=a ∨ algunos (λx. x=y) xs)" using HI by simp also have "… = (a=y ∨ algunos (λx. x=y) xs)" by auto also have "… = algunos (λx. x=y) (a#xs)" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 14. Definir la función primitiva recursiva sinDuplicados :: 'a list ⇒ bool tal que (sinDuplicados xs) se verifica si la lista xs no contiene duplicados. Por ejemplo, sinDuplicados [1::nat,4,2] = True sinDuplicados [1::nat,4,2,4] = False --------------------------------------------------------------------- *} fun uplicados :: "'a list ⇒ bool" where "uplicados [] = True" | "uplicados (a#xs) = ((¬ estaEn a xs) ∧ uplicados xs)" value "sinDuplicados [1::nat,4,2]" -- "= True" value "sinDuplicados [1::nat,4,2,4]" -- "= False" text {* --------------------------------------------------------------------- Ejercicio 15. Definir la función primitiva recursiva borraDuplicados :: 'a list ⇒ bool tal que (borraDuplicados xs) es la lista obtenida eliminando los elementos duplicados de la lista xs. Por ejemplo, borraDuplicados [1::nat,2,4,2,3] = [1,4,2,3] Nota: La función borraDuplicados es equivalente a la predefinida remdups. --------------------------------------------------------------------- *} fun borraDuplicados :: "'a list ⇒ 'a list" where "borraDuplicados [] = []" | "borraDuplicados (a#xs) = (if estaEn a xs then borraDuplicados xs else (a#borraDuplicados xs))" value "borraDuplicados [1::nat,2,4,2,3]" -- "= [1,4,2,3]" text {* --------------------------------------------------------------------- Ejercicio 16.1. Demostrar o refutar automáticamente length (borraDuplicados xs) ≤ length xs --------------------------------------------------------------------- *} -- "La demostración automática es" lemma length_borraDuplicados: "length (borraDuplicados xs) ≤ length xs" by (induct xs) simp_all text {* --------------------------------------------------------------------- Ejercicio 16.2. Demostrar o refutar detalladamente length (borraDuplicados xs) ≤ length xs --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma length_borraDuplicados_2: "length (borraDuplicados xs) ≤ length xs" proof (induct xs) show "length (borraDuplicados []) ≤ length []" by simp next fix a xs assume HI: "length (borraDuplicados xs) ≤ length xs" thus "length (borraDuplicados (a#xs)) ≤ length (a#xs)" proof (cases) assume "estaEn a xs" thus "length (borraDuplicados (a#xs)) ≤ length (a#xs)" using HI by auto next assume "(¬ estaEn a xs)" thus "length (borraDuplicados (a#xs)) ≤ length (a#xs)" using HI by auto qed qed -- "La demostración detallada es" lemma length_borraDuplicados_3: "length (borraDuplicados xs) ≤ length xs" proof (induct xs) show "length (borraDuplicados []) ≤ length []" by simp next fix a xs assume HI: "length (borraDuplicados xs) ≤ length xs" show "length (borraDuplicados (a#xs)) ≤ length (a#xs)" proof (cases) assume "estaEn a xs" hence "length (borraDuplicados (a#xs)) = length (borraDuplicados xs)" by simp also have "… ≤ length xs" using HI by simp also have "… ≤ length (a#xs)" by simp finally show ?thesis . next assume "(¬ estaEn a xs)" hence "length (borraDuplicados (a#xs)) = length (a#(borraDuplicados xs))" by simp also have "… = 1 + length (borraDuplicados xs)" by simp also have "… ≤ 1 + length xs" using HI by simp also have "… = length (a#xs)" by simp finally show ?thesis . qed qed text {* --------------------------------------------------------------------- Ejercicio 17.1. Demostrar o refutar automáticamente estaEn a (borraDuplicados xs) = estaEn a xs --------------------------------------------------------------------- *} -- "La demostración automática es" lemma estaEn_borraDuplicados: "estaEn a (borraDuplicados xs) = estaEn a xs" by (induct xs) auto text {* --------------------------------------------------------------------- Ejercicio 17.2. Demostrar o refutar detalladamente estaEn a (borraDuplicados xs) = estaEn a xs --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma estaEn_borraDuplicados_2: "estaEn a (borraDuplicados xs) = estaEn a xs" proof (induct xs) show "estaEn a (borraDuplicados []) = estaEn a []" by simp next fix b xs assume HI: "estaEn a (borraDuplicados xs) = estaEn a xs" show "estaEn a (borraDuplicados (b#xs)) = estaEn a (b#xs)" proof (rule iffI) assume c1: "estaEn a (borraDuplicados (b#xs))" show "estaEn a (b#xs)" proof (cases) assume "estaEn b xs" thus "estaEn a (b#xs)" using c1 HI by auto next assume "¬ estaEn b xs" thus "estaEn a (b#xs)" using c1 HI by auto qed next assume c2: "estaEn a (b#xs)" show "estaEn a (borraDuplicados (b#xs))" proof (cases) assume "a=b" thus "estaEn a (borraDuplicados (b#xs))" using HI by auto next assume "a≠b" thus "estaEn a (borraDuplicados (b#xs))" using `a≠b` c2 HI by auto qed qed qed -- "La demostración detallada es" lemma estaEn_borraDuplicados_3: "estaEn a (borraDuplicados xs) = estaEn a xs" proof (induct xs) show "estaEn a (borraDuplicados []) = estaEn a []" by simp next fix b xs assume HI: "estaEn a (borraDuplicados xs) = estaEn a xs" show "estaEn a (borraDuplicados (b#xs)) = estaEn a (b#xs)" proof (rule iffI) assume c1: "estaEn a (borraDuplicados (b#xs))" show "estaEn a (b#xs)" proof (cases) assume "estaEn b xs" hence "estaEn a (borraDuplicados xs)" using c1 by simp hence "estaEn a xs" using HI by simp thus "estaEn a (b#xs)" by simp next assume "¬ estaEn b xs" hence "estaEn a (b#(borraDuplicados xs))" using c1 by simp hence "a=b ∨ (estaEn a (borraDuplicados xs))" by simp hence "a=b ∨ (estaEn a xs)" using HI by simp thus "estaEn a (b#xs)" by simp qed next assume c2: "estaEn a (b#xs)" show "estaEn a (borraDuplicados (b#xs))" proof (cases) assume "a=b" thus "estaEn a (borraDuplicados (b#xs))" using HI by auto next assume "a≠b" hence "estaEn a xs" using c2 by simp hence "estaEn a (borraDuplicados xs)" using HI by simp thus "estaEn a (borraDuplicados (b#xs))" using `a≠b` by simp qed qed qed text {* --------------------------------------------------------------------- Ejercicio 18.1. Demostrar o refutar automáticamente sinDuplicados (borraDuplicados xs) --------------------------------------------------------------------- *} -- "La demostración automática" lemma sinDuplicados_borraDuplicados: "sinDuplicados (borraDuplicados xs)" by (induct xs) (auto simp add: estaEn_borraDuplicados) text {* --------------------------------------------------------------------- Ejercicio 18.2. Demostrar o refutar detalladamente sinDuplicados (borraDuplicados xs) --------------------------------------------------------------------- *} -- "La demostración estructurada es" lemma sinDuplicados_borraDuplicados_2: "sinDuplicados (borraDuplicados xs)" proof (induct xs) show "sinDuplicados (borraDuplicados [])" by simp next fix a xs assume HI: "sinDuplicados (borraDuplicados xs)" show "sinDuplicados (borraDuplicados (a#xs))" proof (cases) assume "estaEn a xs" thus "sinDuplicados (borraDuplicados (a#xs))" using HI by simp next assume "¬ estaEn a xs" thus "sinDuplicados (borraDuplicados (a#xs))" using `¬ estaEn a xs` HI by (auto simp add: estaEn_borraDuplicados) qed qed -- "La demostración detallada es" lemma sinDuplicados_borraDuplicados_3: "sinDuplicados (borraDuplicados xs)" proof (induct xs) show "sinDuplicados (borraDuplicados [])" by simp next fix a xs assume HI: "sinDuplicados (borraDuplicados xs)" show "sinDuplicados (borraDuplicados (a#xs))" proof (cases) assume "estaEn a xs" thus "sinDuplicados (borraDuplicados (a#xs))" using HI by simp next assume "¬ estaEn a xs" hence "¬ (estaEn a xs) ∧ sinDuplicados (borraDuplicados xs)" using HI by simp hence "¬ estaEn a (borraDuplicados xs) ∧ sinDuplicados (borraDuplicados xs)" by (simp add: estaEn_borraDuplicados) hence "sinDuplicados (a#borraDuplicados xs)" by simp thus "sinDuplicados (borraDuplicados (a#xs))" using `¬ estaEn a xs` by simp qed qed text {* --------------------------------------------------------------------- Ejercicio 19. Demostrar o refutar: borraDuplicados (rev xs) = rev (borraDuplicados xs) --------------------------------------------------------------------- *} -- "Se busca un contraejemplo con" lemma "borraDuplicados (rev xs) = rev (borraDuplicados xs)" quickcheck oops text {* El contraejemplo encontrado es xs = [3, 2, 3] En efecto, borraDuplicados (rev xs) = borraDuplicados (rev [3,2,3]) = [2,3] rev (borraDuplicados xs) = rev (borraDuplicados [3,2,3]) = [3,2] *} end |