src/HOL/List.ML
changeset 4502 337c073de95e
parent 4423 a129b817b58a
child 4605 579e0ef2df6b
equal deleted inserted replaced
4501:5f629ee2502b 4502:337c073de95e
   436 (** nth **)
   436 (** nth **)
   437 
   437 
   438 section "nth";
   438 section "nth";
   439 
   439 
   440 goal thy
   440 goal thy
   441   "!xs. nth n (xs@ys) = \
   441   "!xs. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
   442 \          (if n < length xs then nth n xs else nth (n - length xs) ys)";
       
   443 by (nat_ind_tac "n" 1);
   442 by (nat_ind_tac "n" 1);
   444  by (Asm_simp_tac 1);
   443  by (Asm_simp_tac 1);
   445  by (rtac allI 1);
   444  by (rtac allI 1);
   446  by (exhaust_tac "xs" 1);
   445  by (exhaust_tac "xs" 1);
   447   by (ALLGOALS Asm_simp_tac);
   446   by (ALLGOALS Asm_simp_tac);
   448 by (rtac allI 1);
   447 by (rtac allI 1);
   449 by (exhaust_tac "xs" 1);
   448 by (exhaust_tac "xs" 1);
   450  by (ALLGOALS Asm_simp_tac);
   449  by (ALLGOALS Asm_simp_tac);
   451 qed_spec_mp "nth_append";
   450 qed_spec_mp "nth_append";
   452 
   451 
   453 goal thy "!n. n < length xs --> nth n (map f xs) = f (nth n xs)";
   452 goal thy "!n. n < length xs --> (map f xs)!n = f(xs!n)";
   454 by (induct_tac "xs" 1);
   453 by (induct_tac "xs" 1);
   455 (* case [] *)
   454 (* case [] *)
   456 by (Asm_full_simp_tac 1);
   455 by (Asm_full_simp_tac 1);
   457 (* case x#xl *)
   456 (* case x#xl *)
   458 by (rtac allI 1);
   457 by (rtac allI 1);
   459 by (nat_ind_tac "n" 1);
   458 by (nat_ind_tac "n" 1);
   460 by (ALLGOALS Asm_full_simp_tac);
   459 by (ALLGOALS Asm_full_simp_tac);
   461 qed_spec_mp "nth_map";
   460 qed_spec_mp "nth_map";
   462 Addsimps [nth_map];
   461 Addsimps [nth_map];
   463 
   462 
   464 goal thy "!n. n < length xs --> list_all P xs --> P(nth n xs)";
   463 goal thy "!n. n < length xs --> list_all P xs --> P(xs!n)";
   465 by (induct_tac "xs" 1);
   464 by (induct_tac "xs" 1);
   466 (* case [] *)
   465 (* case [] *)
   467 by (Simp_tac 1);
   466 by (Simp_tac 1);
   468 (* case x#xl *)
   467 (* case x#xl *)
   469 by (rtac allI 1);
   468 by (rtac allI 1);
   470 by (nat_ind_tac "n" 1);
   469 by (nat_ind_tac "n" 1);
   471 by (ALLGOALS Asm_full_simp_tac);
   470 by (ALLGOALS Asm_full_simp_tac);
   472 qed_spec_mp "list_all_nth";
   471 qed_spec_mp "list_all_nth";
   473 
   472 
   474 goal thy "!n. n < length xs --> (nth n xs) mem xs";
   473 goal thy "!n. n < length xs --> xs!n mem xs";
   475 by (induct_tac "xs" 1);
   474 by (induct_tac "xs" 1);
   476 (* case [] *)
   475 (* case [] *)
   477 by (Simp_tac 1);
   476 by (Simp_tac 1);
   478 (* case x#xl *)
   477 (* case x#xl *)
   479 by (rtac allI 1);
   478 by (rtac allI 1);
   641 by (rtac allI 1);
   640 by (rtac allI 1);
   642 by (exhaust_tac "xs" 1);
   641 by (exhaust_tac "xs" 1);
   643 by (ALLGOALS Asm_simp_tac);
   642 by (ALLGOALS Asm_simp_tac);
   644 qed_spec_mp "drop_map";
   643 qed_spec_mp "drop_map";
   645 
   644 
   646 goal thy "!n i. i < n --> nth i (take n xs) = nth i xs";
   645 goal thy "!n i. i < n --> (take n xs)!i = xs!i";
   647 by (induct_tac "xs" 1);
   646 by (induct_tac "xs" 1);
   648  by (ALLGOALS Asm_simp_tac);
   647  by (ALLGOALS Asm_simp_tac);
   649 by (Clarify_tac 1);
   648 by (Clarify_tac 1);
   650 by (exhaust_tac "n" 1);
   649 by (exhaust_tac "n" 1);
   651  by (Blast_tac 1);
   650  by (Blast_tac 1);
   652 by (exhaust_tac "i" 1);
   651 by (exhaust_tac "i" 1);
   653 by (ALLGOALS Asm_full_simp_tac);
   652 by (ALLGOALS Asm_full_simp_tac);
   654 qed_spec_mp "nth_take";
   653 qed_spec_mp "nth_take";
   655 Addsimps [nth_take];
   654 Addsimps [nth_take];
   656 
   655 
   657 goal thy  "!xs i. n + i <= length xs --> nth i (drop n xs) = nth (n + i) xs";
   656 goal thy  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
   658 by (nat_ind_tac "n" 1);
   657 by (nat_ind_tac "n" 1);
   659  by (ALLGOALS Asm_simp_tac);
   658  by (ALLGOALS Asm_simp_tac);
   660 by (rtac allI 1);
   659 by (rtac allI 1);
   661 by (exhaust_tac "xs" 1);
   660 by (exhaust_tac "xs" 1);
   662  by (ALLGOALS Asm_simp_tac);
   661  by (ALLGOALS Asm_simp_tac);