src/HOL/Nat.thy
changeset 60562 24af00b010cf
parent 60427 b4b672f09270
child 60636 ee18efe9b246
equal deleted inserted replaced
60560:ce7030d9191d 60562:24af00b010cf
   280   show "0 \<noteq> (1::nat)" unfolding One_nat_def by simp
   280   show "0 \<noteq> (1::nat)" unfolding One_nat_def by simp
   281   show "1 * n = n" unfolding One_nat_def by simp
   281   show "1 * n = n" unfolding One_nat_def by simp
   282   show "n * m = m * n" by (induct n) simp_all
   282   show "n * m = m * n" by (induct n) simp_all
   283   show "(n * m) * q = n * (m * q)" by (induct n) (simp_all add: add_mult_distrib)
   283   show "(n * m) * q = n * (m * q)" by (induct n) (simp_all add: add_mult_distrib)
   284   show "(n + m) * q = n * q + m * q" by (rule add_mult_distrib)
   284   show "(n + m) * q = n * q + m * q" by (rule add_mult_distrib)
       
   285 next
       
   286   fix k m n :: nat
       
   287   show "k * ((m::nat) - n) = (k * m) - (k * n)"
       
   288     by (induct m n rule: diff_induct) simp_all
   285 qed
   289 qed
   286 
   290 
   287 end
   291 end
       
   292 
       
   293 text {* Difference distributes over multiplication *}
       
   294 
       
   295 lemma diff_mult_distrib:
       
   296   "((m::nat) - n) * k = (m * k) - (n * k)"
       
   297   by (fact left_diff_distrib')
       
   298 
       
   299 lemma diff_mult_distrib2:
       
   300   "k * ((m::nat) - n) = (k * m) - (k * n)"
       
   301   by (fact right_diff_distrib')
       
   302 
   288 
   303 
   289 subsubsection {* Addition *}
   304 subsubsection {* Addition *}
   290 
   305 
   291 lemma nat_add_left_cancel:
   306 lemma nat_add_left_cancel:
   292   fixes k m n :: nat
   307   fixes k m n :: nat
   361 lemma diff_add_0: "n - (n + m) = (0::nat)"
   376 lemma diff_add_0: "n - (n + m) = (0::nat)"
   362   by (fact diff_add_zero)
   377   by (fact diff_add_zero)
   363 
   378 
   364 lemma diff_Suc_1 [simp]: "Suc n - 1 = n"
   379 lemma diff_Suc_1 [simp]: "Suc n - 1 = n"
   365   unfolding One_nat_def by simp
   380   unfolding One_nat_def by simp
   366 
       
   367 text {* Difference distributes over multiplication *}
       
   368 
       
   369 instance nat :: comm_semiring_1_diff_distrib
       
   370 proof
       
   371   fix k m n :: nat
       
   372   show "k * ((m::nat) - n) = (k * m) - (k * n)"
       
   373     by (induct m n rule: diff_induct) simp_all
       
   374 qed
       
   375 
       
   376 lemma diff_mult_distrib:
       
   377   "((m::nat) - n) * k = (m * k) - (n * k)"
       
   378   by (fact left_diff_distrib')
       
   379   
       
   380 lemma diff_mult_distrib2:
       
   381   "k * ((m::nat) - n) = (k * m) - (k * n)"
       
   382   by (fact right_diff_distrib')
       
   383 
       
   384 
   381 
   385 subsubsection {* Multiplication *}
   382 subsubsection {* Multiplication *}
   386 
   383 
   387 lemma add_mult_distrib2: "k * (m + n) = (k * m) + ((k * n)::nat)"
   384 lemma add_mult_distrib2: "k * (m + n) = (k * m) + ((k * n)::nat)"
   388   by (fact distrib_left)
   385   by (fact distrib_left)
   486   by (simp add: less_eq_Suc_le) (erule Suc_leD)
   483   by (simp add: less_eq_Suc_le) (erule Suc_leD)
   487 
   484 
   488 instance
   485 instance
   489 proof
   486 proof
   490   fix n m :: nat
   487   fix n m :: nat
   491   show "n < m \<longleftrightarrow> n \<le> m \<and> \<not> m \<le> n" 
   488   show "n < m \<longleftrightarrow> n \<le> m \<and> \<not> m \<le> n"
   492   proof (induct n arbitrary: m)
   489   proof (induct n arbitrary: m)
   493     case 0 then show ?case by (cases m) (simp_all add: less_eq_Suc_le)
   490     case 0 then show ?case by (cases m) (simp_all add: less_eq_Suc_le)
   494   next
   491   next
   495     case (Suc n) then show ?case by (cases m) (simp_all add: less_eq_Suc_le)
   492     case (Suc n) then show ?case by (cases m) (simp_all add: less_eq_Suc_le)
   496   qed
   493   qed
   548 
   545 
   549 lemma less_not_refl: "~ n < (n::nat)"
   546 lemma less_not_refl: "~ n < (n::nat)"
   550   by (rule order_less_irrefl)
   547   by (rule order_less_irrefl)
   551 
   548 
   552 lemma less_not_refl2: "n < m ==> m \<noteq> (n::nat)"
   549 lemma less_not_refl2: "n < m ==> m \<noteq> (n::nat)"
   553   by (rule not_sym) (rule less_imp_neq) 
   550   by (rule not_sym) (rule less_imp_neq)
   554 
   551 
   555 lemma less_not_refl3: "(s::nat) < t ==> s \<noteq> t"
   552 lemma less_not_refl3: "(s::nat) < t ==> s \<noteq> t"
   556   by (rule less_imp_neq)
   553   by (rule less_imp_neq)
   557 
   554 
   558 lemma less_irrefl_nat: "(n::nat) < n ==> R"
   555 lemma less_irrefl_nat: "(n::nat) < n ==> R"
   592 
   589 
   593 
   590 
   594 subsubsection {* Inductive (?) properties *}
   591 subsubsection {* Inductive (?) properties *}
   595 
   592 
   596 lemma Suc_lessI: "m < n ==> Suc m \<noteq> n ==> Suc m < n"
   593 lemma Suc_lessI: "m < n ==> Suc m \<noteq> n ==> Suc m < n"
   597   unfolding less_eq_Suc_le [of m] le_less by simp 
   594   unfolding less_eq_Suc_le [of m] le_less by simp
   598 
   595 
   599 lemma lessE:
   596 lemma lessE:
   600   assumes major: "i < k"
   597   assumes major: "i < k"
   601   and p1: "k = Suc i ==> P" and p2: "!!j. i < j ==> k = Suc j ==> P"
   598   and p1: "k = Suc i ==> P" and p2: "!!j. i < j ==> k = Suc j ==> P"
   602   shows P
   599   shows P
   781 apply(auto simp: gr0_conv_Suc)
   778 apply(auto simp: gr0_conv_Suc)
   782 apply (induct_tac m)
   779 apply (induct_tac m)
   783 apply (simp_all add: add_less_mono)
   780 apply (simp_all add: add_less_mono)
   784 done
   781 done
   785 
   782 
       
   783 text {* Addition is the inverse of subtraction:
       
   784   if @{term "n \<le> m"} then @{term "n + (m - n) = m"}. *}
       
   785 lemma add_diff_inverse_nat: "~  m < n ==> n + (m - n) = (m::nat)"
       
   786 by (induct m n rule: diff_induct) simp_all
       
   787 
       
   788 
   786 text{*The naturals form an ordered @{text semidom}*}
   789 text{*The naturals form an ordered @{text semidom}*}
   787 instance nat :: linordered_semidom
   790 instance nat :: linordered_semidom
   788 proof
   791 proof
   789   show "0 < (1::nat)" by simp
   792   show "0 < (1::nat)" by simp
   790   show "\<And>m n q :: nat. m \<le> n \<Longrightarrow> q + m \<le> q + n" by simp
   793   show "\<And>m n q :: nat. m \<le> n \<Longrightarrow> q + m \<le> q + n" by simp
   791   show "\<And>m n q :: nat. m < n \<Longrightarrow> 0 < q \<Longrightarrow> q * m < q * n" by (simp add: mult_less_mono2)
   794   show "\<And>m n q :: nat. m < n \<Longrightarrow> 0 < q \<Longrightarrow> q * m < q * n" by (simp add: mult_less_mono2)
   792   show "\<And>m n :: nat. m \<noteq> 0 \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> m * n \<noteq> 0" by simp
   795   show "\<And>m n :: nat. m \<noteq> 0 \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> m * n \<noteq> 0" by simp
   793 qed
   796   show "\<And>m n :: nat. n \<le> m ==> (m - n) + n = (m::nat)"
       
   797     by (simp add: add_diff_inverse_nat add.commute linorder_not_less)
       
   798 qed 
   794 
   799 
   795 
   800 
   796 subsubsection {* @{term min} and @{term max} *}
   801 subsubsection {* @{term min} and @{term max} *}
   797 
   802 
   798 lemma mono_Suc: "mono Suc"
   803 lemma mono_Suc: "mono Suc"
   987 text{* A compact version without explicit base case: *}
   992 text{* A compact version without explicit base case: *}
   988 lemma infinite_descent:
   993 lemma infinite_descent:
   989   "\<lbrakk> !!n::nat. \<not> P n \<Longrightarrow>  \<exists>m<n. \<not>  P m \<rbrakk> \<Longrightarrow>  P n"
   994   "\<lbrakk> !!n::nat. \<not> P n \<Longrightarrow>  \<exists>m<n. \<not>  P m \<rbrakk> \<Longrightarrow>  P n"
   990 by (induct n rule: less_induct) auto
   995 by (induct n rule: less_induct) auto
   991 
   996 
   992 lemma infinite_descent0[case_names 0 smaller]: 
   997 lemma infinite_descent0[case_names 0 smaller]:
   993   "\<lbrakk> P 0; !!n. n>0 \<Longrightarrow> \<not> P n \<Longrightarrow> (\<exists>m::nat. m < n \<and> \<not>P m) \<rbrakk> \<Longrightarrow> P n"
   998   "\<lbrakk> P 0; !!n. n>0 \<Longrightarrow> \<not> P n \<Longrightarrow> (\<exists>m::nat. m < n \<and> \<not>P m) \<rbrakk> \<Longrightarrow> P n"
   994 by (rule infinite_descent) (case_tac "n>0", auto)
   999 by (rule infinite_descent) (case_tac "n>0", auto)
   995 
  1000 
   996 text {*
  1001 text {*
   997 Infinite descent using a mapping to $\mathbb{N}$:
  1002 Infinite descent using a mapping to $\mathbb{N}$:
  1110     simp add: less_iff_Suc_add add_Suc_right [symmetric] ac_simps)
  1115     simp add: less_iff_Suc_add add_Suc_right [symmetric] ac_simps)
  1111 
  1116 
  1112 
  1117 
  1113 subsubsection {* More results about difference *}
  1118 subsubsection {* More results about difference *}
  1114 
  1119 
  1115 text {* Addition is the inverse of subtraction:
       
  1116   if @{term "n \<le> m"} then @{term "n + (m - n) = m"}. *}
       
  1117 lemma add_diff_inverse: "~  m < n ==> n + (m - n) = (m::nat)"
       
  1118 by (induct m n rule: diff_induct) simp_all
       
  1119 
       
  1120 lemma le_add_diff_inverse [simp]: "n \<le> m ==> n + (m - n) = (m::nat)"
       
  1121 by (simp add: add_diff_inverse linorder_not_less)
       
  1122 
       
  1123 lemma le_add_diff_inverse2 [simp]: "n \<le> m ==> (m - n) + n = (m::nat)"
       
  1124 by (simp add: add.commute)
       
  1125 
       
  1126 lemma Suc_diff_le: "n \<le> m ==> Suc m - n = Suc (m - n)"
  1120 lemma Suc_diff_le: "n \<le> m ==> Suc m - n = Suc (m - n)"
  1127 by (induct m n rule: diff_induct) simp_all
  1121 by (induct m n rule: diff_induct) simp_all
  1128 
  1122 
  1129 lemma diff_less_Suc: "m - n < Suc m"
  1123 lemma diff_less_Suc: "m - n < Suc m"
  1130 apply (induct m n rule: diff_induct)
  1124 apply (induct m n rule: diff_induct)
  1488   "of_nat (Suc n) \<noteq> 0"
  1482   "of_nat (Suc n) \<noteq> 0"
  1489   unfolding of_nat_eq_0_iff by simp
  1483   unfolding of_nat_eq_0_iff by simp
  1490 
  1484 
  1491 lemma of_nat_0_neq [simp]:
  1485 lemma of_nat_0_neq [simp]:
  1492   "0 \<noteq> of_nat (Suc n)"
  1486   "0 \<noteq> of_nat (Suc n)"
  1493   unfolding of_nat_0_eq_iff by simp  
  1487   unfolding of_nat_0_eq_iff by simp
  1494   
  1488 
  1495 end
  1489 end
  1496 
  1490 
  1497 context linordered_semidom
  1491 context linordered_semidom
  1498 begin
  1492 begin
  1499 
  1493 
  1768 by (simp split add: nat_diff_split)
  1762 by (simp split add: nat_diff_split)
  1769 
  1763 
  1770 lemma min_diff: "min (m - (i::nat)) (n - i) = min m n - i"
  1764 lemma min_diff: "min (m - (i::nat)) (n - i) = min m n - i"
  1771 by auto
  1765 by auto
  1772 
  1766 
  1773 lemma inj_on_diff_nat: 
  1767 lemma inj_on_diff_nat:
  1774   assumes k_le_n: "\<forall>n \<in> N. k \<le> (n::nat)"
  1768   assumes k_le_n: "\<forall>n \<in> N. k \<le> (n::nat)"
  1775   shows "inj_on (\<lambda>n. n - k) N"
  1769   shows "inj_on (\<lambda>n. n - k) N"
  1776 proof (rule inj_onI)
  1770 proof (rule inj_onI)
  1777   fix x y
  1771   fix x y
  1778   assume a: "x \<in> N" "y \<in> N" "x - k = y - k"
  1772   assume a: "x \<in> N" "y \<in> N" "x - k = y - k"
  1978 
  1972 
  1979 subsection {* Aliases *}
  1973 subsection {* Aliases *}
  1980 
  1974 
  1981 lemma nat_mult_1: "(1::nat) * n = n"
  1975 lemma nat_mult_1: "(1::nat) * n = n"
  1982   by (fact mult_1_left)
  1976   by (fact mult_1_left)
  1983  
  1977 
  1984 lemma nat_mult_1_right: "n * (1::nat) = n"
  1978 lemma nat_mult_1_right: "n * (1::nat) = n"
  1985   by (fact mult_1_right)
  1979   by (fact mult_1_right)
  1986 
  1980 
  1987 
  1981 
  1988 subsection {* Size of a datatype value *}
  1982 subsection {* Size of a datatype value *}