433 corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)" |
379 corollary nat_less_eq_zless: "0 \<le> w ==> (nat w < nat z) = (w<z)" |
434 by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) |
380 by (simp add: nat_le_eq_zle linorder_not_le [symmetric]) |
435 |
381 |
436 lemma zless_nat_conj: "(nat w < nat z) = (0 < z & w < z)" |
382 lemma zless_nat_conj: "(nat w < nat z) = (0 < z & w < z)" |
437 apply (cases w, cases z) |
383 apply (cases w, cases z) |
438 apply (simp add: nat le int_def Zero_int_def linorder_not_le [symmetric], arith) |
384 apply (simp add: nat le Zero_int_def linorder_not_le [symmetric], arith) |
439 done |
385 done |
|
386 |
|
387 lemma nonneg_eq_int_of_nat: "[| 0 \<le> z; !!m. z = int_of_nat m ==> P |] ==> P" |
|
388 by (blast dest: nat_0_le' sym) |
|
389 |
|
390 lemma nat_eq_iff': "(nat w = m) = (if 0 \<le> w then w = int_of_nat m else m=0)" |
|
391 by (cases w, simp add: nat le int_of_nat_def Zero_int_def, arith) |
|
392 |
|
393 corollary nat_eq_iff2': "(m = nat w) = (if 0 \<le> w then w = int_of_nat m else m=0)" |
|
394 by (simp only: eq_commute [of m] nat_eq_iff') |
|
395 |
|
396 lemma nat_less_iff': "0 \<le> w ==> (nat w < m) = (w < int_of_nat m)" |
|
397 apply (cases w) |
|
398 apply (simp add: nat le int_of_nat_def Zero_int_def linorder_not_le [symmetric], arith) |
|
399 done |
|
400 |
|
401 lemma int_of_nat_eq_iff: "(int_of_nat m = z) = (m = nat z & 0 \<le> z)" |
|
402 by (auto simp add: nat_eq_iff2') |
|
403 |
|
404 lemma zero_less_nat_eq [simp]: "(0 < nat z) = (0 < z)" |
|
405 by (insert zless_nat_conj [of 0], auto) |
|
406 |
|
407 lemma nat_add_distrib: |
|
408 "[| (0::int) \<le> z; 0 \<le> z' |] ==> nat (z+z') = nat z + nat z'" |
|
409 by (cases z, cases z', simp add: nat add le Zero_int_def) |
|
410 |
|
411 lemma nat_diff_distrib: |
|
412 "[| (0::int) \<le> z'; z' \<le> z |] ==> nat (z-z') = nat z - nat z'" |
|
413 by (cases z, cases z', |
|
414 simp add: nat add minus diff_minus le Zero_int_def) |
|
415 |
|
416 lemma nat_zminus_int_of_nat [simp]: "nat (- (int_of_nat n)) = 0" |
|
417 by (simp add: int_of_nat_def minus nat Zero_int_def) |
|
418 |
|
419 lemma zless_nat_eq_int_zless: "(m < nat z) = (int_of_nat m < z)" |
|
420 by (cases z, simp add: nat less int_of_nat_def, arith) |
|
421 |
|
422 |
|
423 subsection{*Lemmas about the Function @{term int} and Orderings*} |
|
424 |
|
425 lemma negative_zless_0': "- (int_of_nat (Suc n)) < 0" |
|
426 by (simp add: order_less_le del: of_nat_Suc) |
|
427 |
|
428 lemma negative_zless' [iff]: "- (int_of_nat (Suc n)) < int_of_nat m" |
|
429 by (rule negative_zless_0' [THEN order_less_le_trans], simp) |
|
430 |
|
431 lemma negative_zle_0': "- int_of_nat n \<le> 0" |
|
432 by (simp add: minus_le_iff) |
|
433 |
|
434 lemma negative_zle' [iff]: "- int_of_nat n \<le> int_of_nat m" |
|
435 by (rule order_trans [OF negative_zle_0' of_nat_0_le_iff]) |
|
436 |
|
437 lemma not_zle_0_negative' [simp]: "~ (0 \<le> - (int_of_nat (Suc n)))" |
|
438 by (subst le_minus_iff, simp del: of_nat_Suc) |
|
439 |
|
440 lemma int_zle_neg': "(int_of_nat n \<le> - int_of_nat m) = (n = 0 & m = 0)" |
|
441 by (simp add: int_of_nat_def le minus Zero_int_def) |
|
442 |
|
443 lemma not_int_zless_negative' [simp]: "~ (int_of_nat n < - int_of_nat m)" |
|
444 by (simp add: linorder_not_less) |
|
445 |
|
446 lemma negative_eq_positive' [simp]: |
|
447 "(- int_of_nat n = int_of_nat m) = (n = 0 & m = 0)" |
|
448 by (force simp add: order_eq_iff [of "- int_of_nat n"] int_zle_neg') |
|
449 |
|
450 lemma zle_iff_zadd: "(w \<le> z) = (\<exists>n. z = w + int_of_nat n)" |
|
451 proof (cases w, cases z, simp add: le add int_of_nat_def) |
|
452 fix a b c d |
|
453 assume "w = Abs_Integ (intrel `` {(a,b)})" "z = Abs_Integ (intrel `` {(c,d)})" |
|
454 show "(a+d \<le> c+b) = (\<exists>n. c+b = a+n+d)" |
|
455 proof |
|
456 assume "a + d \<le> c + b" |
|
457 thus "\<exists>n. c + b = a + n + d" |
|
458 by (auto intro!: exI [where x="c+b - (a+d)"]) |
|
459 next |
|
460 assume "\<exists>n. c + b = a + n + d" |
|
461 then obtain n where "c + b = a + n + d" .. |
|
462 thus "a + d \<le> c + b" by arith |
|
463 qed |
|
464 qed |
|
465 |
|
466 lemma abs_of_nat [simp]: "\<bar>of_nat n::'a::ordered_idom\<bar> = of_nat n" |
|
467 by (rule of_nat_0_le_iff [THEN abs_of_nonneg]) (* belongs in Nat.thy *) |
|
468 |
|
469 text{*This version is proved for all ordered rings, not just integers! |
|
470 It is proved here because attribute @{text arith_split} is not available |
|
471 in theory @{text Ring_and_Field}. |
|
472 But is it really better than just rewriting with @{text abs_if}?*} |
|
473 lemma abs_split [arith_split]: |
|
474 "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))" |
|
475 by (force dest: order_less_le_trans simp add: abs_if linorder_not_less) |
|
476 |
|
477 |
|
478 subsection {* Constants @{term neg} and @{term iszero} *} |
|
479 |
|
480 definition |
|
481 neg :: "'a\<Colon>ordered_idom \<Rightarrow> bool" |
|
482 where |
|
483 [code inline]: "neg Z \<longleftrightarrow> Z < 0" |
|
484 |
|
485 definition (*for simplifying equalities*) |
|
486 iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool" |
|
487 where |
|
488 "iszero z \<longleftrightarrow> z = 0" |
|
489 |
|
490 lemma not_neg_int_of_nat [simp]: "~ neg (int_of_nat n)" |
|
491 by (simp add: neg_def) |
|
492 |
|
493 lemma neg_zminus_int_of_nat [simp]: "neg (- (int_of_nat (Suc n)))" |
|
494 by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc) |
|
495 |
|
496 lemmas neg_eq_less_0 = neg_def |
|
497 |
|
498 lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)" |
|
499 by (simp add: neg_def linorder_not_less) |
|
500 |
|
501 |
|
502 subsection{*To simplify inequalities when Numeral1 can get simplified to 1*} |
|
503 |
|
504 lemma not_neg_0: "~ neg 0" |
|
505 by (simp add: One_int_def neg_def) |
|
506 |
|
507 lemma not_neg_1: "~ neg 1" |
|
508 by (simp add: neg_def linorder_not_less zero_le_one) |
|
509 |
|
510 lemma iszero_0: "iszero 0" |
|
511 by (simp add: iszero_def) |
|
512 |
|
513 lemma not_iszero_1: "~ iszero 1" |
|
514 by (simp add: iszero_def eq_commute) |
|
515 |
|
516 lemma neg_nat: "neg z ==> nat z = 0" |
|
517 by (simp add: neg_def order_less_imp_le) |
|
518 |
|
519 lemma not_neg_nat': "~ neg z ==> int_of_nat (nat z) = z" |
|
520 by (simp add: linorder_not_less neg_def) |
|
521 |
|
522 |
|
523 subsection{*The Set of Natural Numbers*} |
|
524 |
|
525 constdefs |
|
526 Nats :: "'a::semiring_1 set" |
|
527 "Nats == range of_nat" |
|
528 |
|
529 notation (xsymbols) |
|
530 Nats ("\<nat>") |
|
531 |
|
532 lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats" |
|
533 by (simp add: Nats_def) |
|
534 |
|
535 lemma Nats_0 [simp]: "0 \<in> Nats" |
|
536 apply (simp add: Nats_def) |
|
537 apply (rule range_eqI) |
|
538 apply (rule of_nat_0 [symmetric]) |
|
539 done |
|
540 |
|
541 lemma Nats_1 [simp]: "1 \<in> Nats" |
|
542 apply (simp add: Nats_def) |
|
543 apply (rule range_eqI) |
|
544 apply (rule of_nat_1 [symmetric]) |
|
545 done |
|
546 |
|
547 lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats" |
|
548 apply (auto simp add: Nats_def) |
|
549 apply (rule range_eqI) |
|
550 apply (rule of_nat_add [symmetric]) |
|
551 done |
|
552 |
|
553 lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats" |
|
554 apply (auto simp add: Nats_def) |
|
555 apply (rule range_eqI) |
|
556 apply (rule of_nat_mult [symmetric]) |
|
557 done |
|
558 |
|
559 lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)" |
|
560 proof |
|
561 fix n |
|
562 show "of_nat n = id n" by (induct n, simp_all) |
|
563 qed (* belongs in Nat.thy *) |
|
564 |
|
565 |
|
566 subsection{*Embedding of the Integers into any @{text ring_1}: |
|
567 @{term of_int}*} |
|
568 |
|
569 constdefs |
|
570 of_int :: "int => 'a::ring_1" |
|
571 "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })" |
|
572 |
|
573 |
|
574 lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j" |
|
575 proof - |
|
576 have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel" |
|
577 by (simp add: congruent_def compare_rls of_nat_add [symmetric] |
|
578 del: of_nat_add) |
|
579 thus ?thesis |
|
580 by (simp add: of_int_def UN_equiv_class [OF equiv_intrel]) |
|
581 qed |
|
582 |
|
583 lemma of_int_0 [simp]: "of_int 0 = 0" |
|
584 by (simp add: of_int Zero_int_def) |
|
585 |
|
586 lemma of_int_1 [simp]: "of_int 1 = 1" |
|
587 by (simp add: of_int One_int_def) |
|
588 |
|
589 lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z" |
|
590 by (cases w, cases z, simp add: compare_rls of_int add) |
|
591 |
|
592 lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)" |
|
593 by (cases z, simp add: compare_rls of_int minus) |
|
594 |
|
595 lemma of_int_diff [simp]: "of_int (w-z) = of_int w - of_int z" |
|
596 by (simp add: diff_minus) |
|
597 |
|
598 lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z" |
|
599 apply (cases w, cases z) |
|
600 apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib |
|
601 mult add_ac) |
|
602 done |
|
603 |
|
604 lemma of_int_le_iff [simp]: |
|
605 "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)" |
|
606 apply (cases w) |
|
607 apply (cases z) |
|
608 apply (simp add: compare_rls of_int le diff_int_def add minus |
|
609 of_nat_add [symmetric] del: of_nat_add) |
|
610 done |
|
611 |
|
612 text{*Special cases where either operand is zero*} |
|
613 lemmas of_int_0_le_iff [simp] = of_int_le_iff [of 0, simplified] |
|
614 lemmas of_int_le_0_iff [simp] = of_int_le_iff [of _ 0, simplified] |
|
615 |
|
616 |
|
617 lemma of_int_less_iff [simp]: |
|
618 "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)" |
|
619 by (simp add: linorder_not_le [symmetric]) |
|
620 |
|
621 text{*Special cases where either operand is zero*} |
|
622 lemmas of_int_0_less_iff [simp] = of_int_less_iff [of 0, simplified] |
|
623 lemmas of_int_less_0_iff [simp] = of_int_less_iff [of _ 0, simplified] |
|
624 |
|
625 text{*Class for unital rings with characteristic zero. |
|
626 Includes non-ordered rings like the complex numbers.*} |
|
627 axclass ring_char_0 < ring_1, semiring_char_0 |
|
628 |
|
629 lemma of_int_eq_iff [simp]: |
|
630 "(of_int w = (of_int z::'a::ring_char_0)) = (w = z)" |
|
631 apply (cases w, cases z, simp add: of_int) |
|
632 apply (simp only: diff_eq_eq diff_add_eq eq_diff_eq) |
|
633 apply (simp only: of_nat_add [symmetric] of_nat_eq_iff) |
|
634 done |
|
635 |
|
636 text{*Every @{text ordered_idom} has characteristic zero.*} |
|
637 instance ordered_idom < ring_char_0 .. |
|
638 |
|
639 text{*Special cases where either operand is zero*} |
|
640 lemmas of_int_0_eq_iff [simp] = of_int_eq_iff [of 0, simplified] |
|
641 lemmas of_int_eq_0_iff [simp] = of_int_eq_iff [of _ 0, simplified] |
|
642 |
|
643 lemma of_int_eq_id [simp]: "of_int = (id :: int => int)" |
|
644 proof |
|
645 fix z |
|
646 show "of_int z = id z" |
|
647 by (cases z) |
|
648 (simp add: of_int add minus int_of_nat_def diff_minus) |
|
649 qed |
|
650 |
|
651 |
|
652 subsection{*The Set of Integers*} |
|
653 |
|
654 constdefs |
|
655 Ints :: "'a::ring_1 set" |
|
656 "Ints == range of_int" |
|
657 |
|
658 notation (xsymbols) |
|
659 Ints ("\<int>") |
|
660 |
|
661 lemma Ints_0 [simp]: "0 \<in> Ints" |
|
662 apply (simp add: Ints_def) |
|
663 apply (rule range_eqI) |
|
664 apply (rule of_int_0 [symmetric]) |
|
665 done |
|
666 |
|
667 lemma Ints_1 [simp]: "1 \<in> Ints" |
|
668 apply (simp add: Ints_def) |
|
669 apply (rule range_eqI) |
|
670 apply (rule of_int_1 [symmetric]) |
|
671 done |
|
672 |
|
673 lemma Ints_add [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a+b \<in> Ints" |
|
674 apply (auto simp add: Ints_def) |
|
675 apply (rule range_eqI) |
|
676 apply (rule of_int_add [symmetric]) |
|
677 done |
|
678 |
|
679 lemma Ints_minus [simp]: "a \<in> Ints ==> -a \<in> Ints" |
|
680 apply (auto simp add: Ints_def) |
|
681 apply (rule range_eqI) |
|
682 apply (rule of_int_minus [symmetric]) |
|
683 done |
|
684 |
|
685 lemma Ints_diff [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a-b \<in> Ints" |
|
686 apply (auto simp add: Ints_def) |
|
687 apply (rule range_eqI) |
|
688 apply (rule of_int_diff [symmetric]) |
|
689 done |
|
690 |
|
691 lemma Ints_mult [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a*b \<in> Ints" |
|
692 apply (auto simp add: Ints_def) |
|
693 apply (rule range_eqI) |
|
694 apply (rule of_int_mult [symmetric]) |
|
695 done |
|
696 |
|
697 text{*Collapse nested embeddings*} |
|
698 lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n" |
|
699 by (induct n, auto) |
|
700 |
|
701 lemma Ints_cases [cases set: Ints]: |
|
702 assumes "q \<in> \<int>" |
|
703 obtains (of_int) z where "q = of_int z" |
|
704 unfolding Ints_def |
|
705 proof - |
|
706 from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def . |
|
707 then obtain z where "q = of_int z" .. |
|
708 then show thesis .. |
|
709 qed |
|
710 |
|
711 lemma Ints_induct [case_names of_int, induct set: Ints]: |
|
712 "q \<in> \<int> ==> (!!z. P (of_int z)) ==> P q" |
|
713 by (rule Ints_cases) auto |
|
714 |
|
715 |
|
716 (* int (Suc n) = 1 + int n *) |
|
717 |
|
718 |
|
719 |
|
720 subsection{*More Properties of @{term setsum} and @{term setprod}*} |
|
721 |
|
722 text{*By Jeremy Avigad*} |
|
723 |
|
724 |
|
725 lemma of_nat_setsum: "of_nat (setsum f A) = (\<Sum>x\<in>A. of_nat(f x))" |
|
726 apply (cases "finite A") |
|
727 apply (erule finite_induct, auto) |
|
728 done |
|
729 |
|
730 lemma of_int_setsum: "of_int (setsum f A) = (\<Sum>x\<in>A. of_int(f x))" |
|
731 apply (cases "finite A") |
|
732 apply (erule finite_induct, auto) |
|
733 done |
|
734 |
|
735 lemma of_nat_setprod: "of_nat (setprod f A) = (\<Prod>x\<in>A. of_nat(f x))" |
|
736 apply (cases "finite A") |
|
737 apply (erule finite_induct, auto) |
|
738 done |
|
739 |
|
740 lemma of_int_setprod: "of_int (setprod f A) = (\<Prod>x\<in>A. of_int(f x))" |
|
741 apply (cases "finite A") |
|
742 apply (erule finite_induct, auto) |
|
743 done |
|
744 |
|
745 lemma setprod_nonzero_nat: |
|
746 "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::nat)) ==> setprod f A \<noteq> 0" |
|
747 by (rule setprod_nonzero, auto) |
|
748 |
|
749 lemma setprod_zero_eq_nat: |
|
750 "finite A ==> (setprod f A = (0::nat)) = (\<exists>x \<in> A. f x = 0)" |
|
751 by (rule setprod_zero_eq, auto) |
|
752 |
|
753 lemma setprod_nonzero_int: |
|
754 "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::int)) ==> setprod f A \<noteq> 0" |
|
755 by (rule setprod_nonzero, auto) |
|
756 |
|
757 lemma setprod_zero_eq_int: |
|
758 "finite A ==> (setprod f A = (0::int)) = (\<exists>x \<in> A. f x = 0)" |
|
759 by (rule setprod_zero_eq, auto) |
|
760 |
|
761 |
|
762 subsection {* Further properties *} |
|
763 |
|
764 text{*Now we replace the case analysis rule by a more conventional one: |
|
765 whether an integer is negative or not.*} |
|
766 |
|
767 lemma zless_iff_Suc_zadd': |
|
768 "(w < z) = (\<exists>n. z = w + int_of_nat (Suc n))" |
|
769 apply (cases z, cases w) |
|
770 apply (auto simp add: le add int_of_nat_def linorder_not_le [symmetric]) |
|
771 apply (rename_tac a b c d) |
|
772 apply (rule_tac x="a+d - Suc(c+b)" in exI) |
|
773 apply arith |
|
774 done |
|
775 |
|
776 lemma negD': "x<0 ==> \<exists>n. x = - (int_of_nat (Suc n))" |
|
777 apply (cases x) |
|
778 apply (auto simp add: le minus Zero_int_def int_of_nat_def order_less_le) |
|
779 apply (rule_tac x="y - Suc x" in exI, arith) |
|
780 done |
|
781 |
|
782 theorem int_cases': |
|
783 "[|!! n. z = int_of_nat n ==> P; !! n. z = - (int_of_nat (Suc n)) ==> P |] ==> P" |
|
784 apply (cases "z < 0", blast dest!: negD') |
|
785 apply (simp add: linorder_not_less del: of_nat_Suc) |
|
786 apply (blast dest: nat_0_le' [THEN sym]) |
|
787 done |
|
788 |
|
789 theorem int_induct': |
|
790 "[|!! n. P (int_of_nat n); !!n. P (- (int_of_nat (Suc n))) |] ==> P z" |
|
791 by (cases z rule: int_cases') auto |
|
792 |
|
793 text{*Contributed by Brian Huffman*} |
|
794 theorem int_diff_cases' [case_names diff]: |
|
795 assumes prem: "!!m n. z = int_of_nat m - int_of_nat n ==> P" shows "P" |
|
796 apply (cases z rule: eq_Abs_Integ) |
|
797 apply (rule_tac m=x and n=y in prem) |
|
798 apply (simp add: int_of_nat_def diff_def minus add) |
|
799 done |
|
800 |
|
801 lemma of_nat_nat: "0 \<le> z ==> of_nat (nat z) = of_int z" |
|
802 by (cases z, simp add: nat le of_int Zero_int_def) |
|
803 |
|
804 lemmas zless_le = less_int_def [THEN meta_eq_to_obj_eq] |
|
805 |
|
806 |
|
807 subsection{*@{term int}: Embedding the Naturals into the Integers*} |
|
808 |
|
809 definition |
|
810 int :: "nat \<Rightarrow> int" |
|
811 where |
|
812 [code func del]: "int m = Abs_Integ (intrel `` {(m, 0)})" |
|
813 |
|
814 lemma inj_int: "inj int" |
|
815 by (simp add: inj_on_def int_def) |
|
816 |
|
817 lemma int_int_eq [iff]: "(int m = int n) = (m = n)" |
|
818 by (fast elim!: inj_int [THEN injD]) |
|
819 |
|
820 lemma zadd_int: "(int m) + (int n) = int (m + n)" |
|
821 by (simp add: int_def add) |
|
822 |
|
823 lemma zadd_int_left: "(int m) + (int n + z) = int (m + n) + z" |
|
824 by (simp add: zadd_int zadd_assoc [symmetric]) |
|
825 |
|
826 lemma int_mult: "int (m * n) = (int m) * (int n)" |
|
827 by (simp add: int_def mult) |
|
828 |
|
829 text{*Compatibility binding*} |
|
830 lemmas zmult_int = int_mult [symmetric] |
|
831 |
|
832 lemma int_eq_0_conv [simp]: "(int n = 0) = (n = 0)" |
|
833 by (simp add: Zero_int_def [folded int_def]) |
|
834 |
|
835 lemma zless_int [simp]: "(int m < int n) = (m<n)" |
|
836 by (simp add: le add int_def linorder_not_le [symmetric]) |
|
837 |
|
838 lemma int_less_0_conv [simp]: "~ (int k < 0)" |
|
839 by (simp add: Zero_int_def [folded int_def]) |
|
840 |
|
841 lemma zero_less_int_conv [simp]: "(0 < int n) = (0 < n)" |
|
842 by (simp add: Zero_int_def [folded int_def]) |
|
843 |
|
844 lemma zle_int [simp]: "(int m \<le> int n) = (m\<le>n)" |
|
845 by (simp add: linorder_not_less [symmetric]) |
|
846 |
|
847 lemma zero_zle_int [simp]: "(0 \<le> int n)" |
|
848 by (simp add: Zero_int_def [folded int_def]) |
|
849 |
|
850 lemma int_le_0_conv [simp]: "(int n \<le> 0) = (n = 0)" |
|
851 by (simp add: Zero_int_def [folded int_def]) |
|
852 |
|
853 lemma int_0 [simp]: "int 0 = (0::int)" |
|
854 by (simp add: Zero_int_def [folded int_def]) |
|
855 |
|
856 lemma int_1 [simp]: "int 1 = 1" |
|
857 by (simp add: One_int_def [folded int_def]) |
|
858 |
|
859 lemma int_Suc0_eq_1: "int (Suc 0) = 1" |
|
860 by (simp add: One_int_def [folded int_def]) |
|
861 |
|
862 lemma int_Suc: "int (Suc m) = 1 + (int m)" |
|
863 by (simp add: One_int_def [folded int_def] zadd_int) |
|
864 |
|
865 lemma nat_int [simp]: "nat(int n) = n" |
|
866 by (simp add: nat int_def) |
|
867 |
|
868 lemma int_nat_eq [simp]: "int (nat z) = (if 0 \<le> z then z else 0)" |
|
869 by (cases z, simp add: nat le int_def Zero_int_def) |
|
870 |
|
871 corollary nat_0_le: "0 \<le> z ==> int (nat z) = z" |
|
872 by simp |
440 |
873 |
441 lemma nonneg_eq_int: "[| 0 \<le> z; !!m. z = int m ==> P |] ==> P" |
874 lemma nonneg_eq_int: "[| 0 \<le> z; !!m. z = int m ==> P |] ==> P" |
442 by (blast dest: nat_0_le sym) |
875 by (blast dest: nat_0_le sym) |
443 |
876 |
444 lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)" |
877 lemma nat_eq_iff: "(nat w = m) = (if 0 \<le> w then w = int m else m=0)" |
518 qed |
935 qed |
519 |
936 |
520 lemma abs_int_eq [simp]: "abs (int m) = int m" |
937 lemma abs_int_eq [simp]: "abs (int m) = int m" |
521 by (simp add: abs_if) |
938 by (simp add: abs_if) |
522 |
939 |
523 text{*This version is proved for all ordered rings, not just integers! |
|
524 It is proved here because attribute @{text arith_split} is not available |
|
525 in theory @{text Ring_and_Field}. |
|
526 But is it really better than just rewriting with @{text abs_if}?*} |
|
527 lemma abs_split [arith_split]: |
|
528 "P(abs(a::'a::ordered_idom)) = ((0 \<le> a --> P a) & (a < 0 --> P(-a)))" |
|
529 by (force dest: order_less_le_trans simp add: abs_if linorder_not_less) |
|
530 |
|
531 |
|
532 subsection {* Constants @{term neg} and @{term iszero} *} |
|
533 |
|
534 definition |
|
535 neg :: "'a\<Colon>ordered_idom \<Rightarrow> bool" |
|
536 where |
|
537 [code inline]: "neg Z \<longleftrightarrow> Z < 0" |
|
538 |
|
539 definition (*for simplifying equalities*) |
|
540 iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool" |
|
541 where |
|
542 "iszero z \<longleftrightarrow> z = 0" |
|
543 |
|
544 lemma not_neg_int [simp]: "~ neg(int n)" |
940 lemma not_neg_int [simp]: "~ neg(int n)" |
545 by (simp add: neg_def) |
941 by (simp add: neg_def) |
546 |
942 |
547 lemma neg_zminus_int [simp]: "neg(- (int (Suc n)))" |
943 lemma neg_zminus_int [simp]: "neg(- (int (Suc n)))" |
548 by (simp add: neg_def neg_less_0_iff_less) |
944 by (simp add: neg_def neg_less_0_iff_less) |
549 |
945 |
550 lemmas neg_eq_less_0 = neg_def |
|
551 |
|
552 lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)" |
|
553 by (simp add: neg_def linorder_not_less) |
|
554 |
|
555 |
|
556 subsection{*To simplify inequalities when Numeral1 can get simplified to 1*} |
|
557 |
|
558 lemma not_neg_0: "~ neg 0" |
|
559 by (simp add: One_int_def neg_def) |
|
560 |
|
561 lemma not_neg_1: "~ neg 1" |
|
562 by (simp add: neg_def linorder_not_less zero_le_one) |
|
563 |
|
564 lemma iszero_0: "iszero 0" |
|
565 by (simp add: iszero_def) |
|
566 |
|
567 lemma not_iszero_1: "~ iszero 1" |
|
568 by (simp add: iszero_def eq_commute) |
|
569 |
|
570 lemma neg_nat: "neg z ==> nat z = 0" |
|
571 by (simp add: neg_def order_less_imp_le) |
|
572 |
|
573 lemma not_neg_nat: "~ neg z ==> int (nat z) = z" |
946 lemma not_neg_nat: "~ neg z ==> int (nat z) = z" |
574 by (simp add: linorder_not_less neg_def) |
947 by (simp add: linorder_not_less neg_def) |
575 |
|
576 |
|
577 subsection{*The Set of Natural Numbers*} |
|
578 |
|
579 constdefs |
|
580 Nats :: "'a::semiring_1 set" |
|
581 "Nats == range of_nat" |
|
582 |
|
583 notation (xsymbols) |
|
584 Nats ("\<nat>") |
|
585 |
|
586 lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats" |
|
587 by (simp add: Nats_def) |
|
588 |
|
589 lemma Nats_0 [simp]: "0 \<in> Nats" |
|
590 apply (simp add: Nats_def) |
|
591 apply (rule range_eqI) |
|
592 apply (rule of_nat_0 [symmetric]) |
|
593 done |
|
594 |
|
595 lemma Nats_1 [simp]: "1 \<in> Nats" |
|
596 apply (simp add: Nats_def) |
|
597 apply (rule range_eqI) |
|
598 apply (rule of_nat_1 [symmetric]) |
|
599 done |
|
600 |
|
601 lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats" |
|
602 apply (auto simp add: Nats_def) |
|
603 apply (rule range_eqI) |
|
604 apply (rule of_nat_add [symmetric]) |
|
605 done |
|
606 |
|
607 lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats" |
|
608 apply (auto simp add: Nats_def) |
|
609 apply (rule range_eqI) |
|
610 apply (rule of_nat_mult [symmetric]) |
|
611 done |
|
612 |
948 |
613 text{*Agreement with the specific embedding for the integers*} |
949 text{*Agreement with the specific embedding for the integers*} |
614 lemma int_eq_of_nat: "int = (of_nat :: nat => int)" |
950 lemma int_eq_of_nat: "int = (of_nat :: nat => int)" |
615 proof |
951 proof |
616 fix n |
952 fix n |
617 show "int n = of_nat n" by (induct n, simp_all add: int_Suc add_ac) |
953 show "int n = of_nat n" by (induct n, simp_all add: int_Suc add_ac) |
618 qed |
954 qed |
619 |
955 |
620 lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)" |
|
621 proof |
|
622 fix n |
|
623 show "of_nat n = id n" by (induct n, simp_all) |
|
624 qed |
|
625 |
|
626 |
|
627 subsection{*Embedding of the Integers into any @{text ring_1}: |
|
628 @{term of_int}*} |
|
629 |
|
630 constdefs |
|
631 of_int :: "int => 'a::ring_1" |
|
632 "of_int z == contents (\<Union>(i,j) \<in> Rep_Integ z. { of_nat i - of_nat j })" |
|
633 |
|
634 |
|
635 lemma of_int: "of_int (Abs_Integ (intrel `` {(i,j)})) = of_nat i - of_nat j" |
|
636 proof - |
|
637 have "(\<lambda>(i,j). { of_nat i - (of_nat j :: 'a) }) respects intrel" |
|
638 by (simp add: congruent_def compare_rls of_nat_add [symmetric] |
|
639 del: of_nat_add) |
|
640 thus ?thesis |
|
641 by (simp add: of_int_def UN_equiv_class [OF equiv_intrel]) |
|
642 qed |
|
643 |
|
644 lemma of_int_0 [simp]: "of_int 0 = 0" |
|
645 by (simp add: of_int Zero_int_def int_def) |
|
646 |
|
647 lemma of_int_1 [simp]: "of_int 1 = 1" |
|
648 by (simp add: of_int One_int_def int_def) |
|
649 |
|
650 lemma of_int_add [simp]: "of_int (w+z) = of_int w + of_int z" |
|
651 by (cases w, cases z, simp add: compare_rls of_int add) |
|
652 |
|
653 lemma of_int_minus [simp]: "of_int (-z) = - (of_int z)" |
|
654 by (cases z, simp add: compare_rls of_int minus) |
|
655 |
|
656 lemma of_int_diff [simp]: "of_int (w-z) = of_int w - of_int z" |
|
657 by (simp add: diff_minus) |
|
658 |
|
659 lemma of_int_mult [simp]: "of_int (w*z) = of_int w * of_int z" |
|
660 apply (cases w, cases z) |
|
661 apply (simp add: compare_rls of_int left_diff_distrib right_diff_distrib |
|
662 mult add_ac) |
|
663 done |
|
664 |
|
665 lemma of_int_le_iff [simp]: |
|
666 "(of_int w \<le> (of_int z::'a::ordered_idom)) = (w \<le> z)" |
|
667 apply (cases w) |
|
668 apply (cases z) |
|
669 apply (simp add: compare_rls of_int le diff_int_def add minus |
|
670 of_nat_add [symmetric] del: of_nat_add) |
|
671 done |
|
672 |
|
673 text{*Special cases where either operand is zero*} |
|
674 lemmas of_int_0_le_iff [simp] = of_int_le_iff [of 0, simplified] |
|
675 lemmas of_int_le_0_iff [simp] = of_int_le_iff [of _ 0, simplified] |
|
676 |
|
677 |
|
678 lemma of_int_less_iff [simp]: |
|
679 "(of_int w < (of_int z::'a::ordered_idom)) = (w < z)" |
|
680 by (simp add: linorder_not_le [symmetric]) |
|
681 |
|
682 text{*Special cases where either operand is zero*} |
|
683 lemmas of_int_0_less_iff [simp] = of_int_less_iff [of 0, simplified] |
|
684 lemmas of_int_less_0_iff [simp] = of_int_less_iff [of _ 0, simplified] |
|
685 |
|
686 text{*Class for unital rings with characteristic zero. |
|
687 Includes non-ordered rings like the complex numbers.*} |
|
688 axclass ring_char_0 < ring_1, semiring_char_0 |
|
689 |
|
690 lemma of_int_eq_iff [simp]: |
|
691 "(of_int w = (of_int z::'a::ring_char_0)) = (w = z)" |
|
692 apply (cases w, cases z, simp add: of_int) |
|
693 apply (simp only: diff_eq_eq diff_add_eq eq_diff_eq) |
|
694 apply (simp only: of_nat_add [symmetric] of_nat_eq_iff) |
|
695 done |
|
696 |
|
697 text{*Every @{text ordered_idom} has characteristic zero.*} |
|
698 instance ordered_idom < ring_char_0 .. |
|
699 |
|
700 text{*Special cases where either operand is zero*} |
|
701 lemmas of_int_0_eq_iff [simp] = of_int_eq_iff [of 0, simplified] |
|
702 lemmas of_int_eq_0_iff [simp] = of_int_eq_iff [of _ 0, simplified] |
|
703 |
|
704 lemma of_int_eq_id [simp]: "of_int = (id :: int => int)" |
|
705 proof |
|
706 fix z |
|
707 show "of_int z = id z" |
|
708 by (cases z) |
|
709 (simp add: of_int add minus int_eq_of_nat [symmetric] int_def diff_minus) |
|
710 qed |
|
711 |
|
712 |
|
713 subsection{*The Set of Integers*} |
|
714 |
|
715 constdefs |
|
716 Ints :: "'a::ring_1 set" |
|
717 "Ints == range of_int" |
|
718 |
|
719 notation (xsymbols) |
|
720 Ints ("\<int>") |
|
721 |
|
722 lemma Ints_0 [simp]: "0 \<in> Ints" |
|
723 apply (simp add: Ints_def) |
|
724 apply (rule range_eqI) |
|
725 apply (rule of_int_0 [symmetric]) |
|
726 done |
|
727 |
|
728 lemma Ints_1 [simp]: "1 \<in> Ints" |
|
729 apply (simp add: Ints_def) |
|
730 apply (rule range_eqI) |
|
731 apply (rule of_int_1 [symmetric]) |
|
732 done |
|
733 |
|
734 lemma Ints_add [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a+b \<in> Ints" |
|
735 apply (auto simp add: Ints_def) |
|
736 apply (rule range_eqI) |
|
737 apply (rule of_int_add [symmetric]) |
|
738 done |
|
739 |
|
740 lemma Ints_minus [simp]: "a \<in> Ints ==> -a \<in> Ints" |
|
741 apply (auto simp add: Ints_def) |
|
742 apply (rule range_eqI) |
|
743 apply (rule of_int_minus [symmetric]) |
|
744 done |
|
745 |
|
746 lemma Ints_diff [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a-b \<in> Ints" |
|
747 apply (auto simp add: Ints_def) |
|
748 apply (rule range_eqI) |
|
749 apply (rule of_int_diff [symmetric]) |
|
750 done |
|
751 |
|
752 lemma Ints_mult [simp]: "[|a \<in> Ints; b \<in> Ints|] ==> a*b \<in> Ints" |
|
753 apply (auto simp add: Ints_def) |
|
754 apply (rule range_eqI) |
|
755 apply (rule of_int_mult [symmetric]) |
|
756 done |
|
757 |
|
758 text{*Collapse nested embeddings*} |
|
759 lemma of_int_of_nat_eq [simp]: "of_int (of_nat n) = of_nat n" |
|
760 by (induct n, auto) |
|
761 |
|
762 lemma of_int_int_eq [simp]: "of_int (int n) = of_nat n" |
956 lemma of_int_int_eq [simp]: "of_int (int n) = of_nat n" |
763 by (simp add: int_eq_of_nat) |
957 by (simp add: int_eq_of_nat) |
764 |
958 |
765 lemma Ints_cases [cases set: Ints]: |
|
766 assumes "q \<in> \<int>" |
|
767 obtains (of_int) z where "q = of_int z" |
|
768 unfolding Ints_def |
|
769 proof - |
|
770 from `q \<in> \<int>` have "q \<in> range of_int" unfolding Ints_def . |
|
771 then obtain z where "q = of_int z" .. |
|
772 then show thesis .. |
|
773 qed |
|
774 |
|
775 lemma Ints_induct [case_names of_int, induct set: Ints]: |
|
776 "q \<in> \<int> ==> (!!z. P (of_int z)) ==> P q" |
|
777 by (rule Ints_cases) auto |
|
778 |
|
779 |
|
780 (* int (Suc n) = 1 + int n *) |
|
781 |
|
782 |
|
783 |
|
784 subsection{*More Properties of @{term setsum} and @{term setprod}*} |
|
785 |
|
786 text{*By Jeremy Avigad*} |
|
787 |
|
788 |
|
789 lemma of_nat_setsum: "of_nat (setsum f A) = (\<Sum>x\<in>A. of_nat(f x))" |
|
790 apply (cases "finite A") |
|
791 apply (erule finite_induct, auto) |
|
792 done |
|
793 |
|
794 lemma of_int_setsum: "of_int (setsum f A) = (\<Sum>x\<in>A. of_int(f x))" |
|
795 apply (cases "finite A") |
|
796 apply (erule finite_induct, auto) |
|
797 done |
|
798 |
|
799 lemma int_setsum: "int (setsum f A) = (\<Sum>x\<in>A. int(f x))" |
959 lemma int_setsum: "int (setsum f A) = (\<Sum>x\<in>A. int(f x))" |
800 by (simp add: int_eq_of_nat of_nat_setsum) |
960 by (simp add: int_eq_of_nat of_nat_setsum) |
801 |
961 |
802 lemma of_nat_setprod: "of_nat (setprod f A) = (\<Prod>x\<in>A. of_nat(f x))" |
|
803 apply (cases "finite A") |
|
804 apply (erule finite_induct, auto) |
|
805 done |
|
806 |
|
807 lemma of_int_setprod: "of_int (setprod f A) = (\<Prod>x\<in>A. of_int(f x))" |
|
808 apply (cases "finite A") |
|
809 apply (erule finite_induct, auto) |
|
810 done |
|
811 |
|
812 lemma int_setprod: "int (setprod f A) = (\<Prod>x\<in>A. int(f x))" |
962 lemma int_setprod: "int (setprod f A) = (\<Prod>x\<in>A. int(f x))" |
813 by (simp add: int_eq_of_nat of_nat_setprod) |
963 by (simp add: int_eq_of_nat of_nat_setprod) |
814 |
|
815 lemma setprod_nonzero_nat: |
|
816 "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::nat)) ==> setprod f A \<noteq> 0" |
|
817 by (rule setprod_nonzero, auto) |
|
818 |
|
819 lemma setprod_zero_eq_nat: |
|
820 "finite A ==> (setprod f A = (0::nat)) = (\<exists>x \<in> A. f x = 0)" |
|
821 by (rule setprod_zero_eq, auto) |
|
822 |
|
823 lemma setprod_nonzero_int: |
|
824 "finite A ==> (\<forall>x \<in> A. f x \<noteq> (0::int)) ==> setprod f A \<noteq> 0" |
|
825 by (rule setprod_nonzero, auto) |
|
826 |
|
827 lemma setprod_zero_eq_int: |
|
828 "finite A ==> (setprod f A = (0::int)) = (\<exists>x \<in> A. f x = 0)" |
|
829 by (rule setprod_zero_eq, auto) |
|
830 |
|
831 |
|
832 subsection {* Further properties *} |
|
833 |
964 |
834 text{*Now we replace the case analysis rule by a more conventional one: |
965 text{*Now we replace the case analysis rule by a more conventional one: |
835 whether an integer is negative or not.*} |
966 whether an integer is negative or not.*} |
836 |
967 |
837 lemma zless_iff_Suc_zadd: |
968 lemma zless_iff_Suc_zadd: |