src/HOL/Nat.thy
changeset 44325 84696670feb1
parent 44278 1220ecb81e8f
child 44817 b63e445c8f6d
equal deleted inserted replaced
44324:d972b91ed09d 44325:84696670feb1
    20 
    20 
    21 subsection {* Type @{text ind} *}
    21 subsection {* Type @{text ind} *}
    22 
    22 
    23 typedecl ind
    23 typedecl ind
    24 
    24 
    25 axiomatization
    25 axiomatization Zero_Rep :: ind and Suc_Rep :: "ind => ind" where
    26   Zero_Rep :: ind and
       
    27   Suc_Rep :: "ind => ind"
       
    28 where
       
    29   -- {* the axiom of infinity in 2 parts *}
    26   -- {* the axiom of infinity in 2 parts *}
    30   Suc_Rep_inject:       "Suc_Rep x = Suc_Rep y ==> x = y" and
    27   Suc_Rep_inject:       "Suc_Rep x = Suc_Rep y ==> x = y" and
    31   Suc_Rep_not_Zero_Rep: "Suc_Rep x \<noteq> Zero_Rep"
    28   Suc_Rep_not_Zero_Rep: "Suc_Rep x \<noteq> Zero_Rep"
    32 
    29 
    33 subsection {* Type nat *}
    30 subsection {* Type nat *}
    34 
    31 
    35 text {* Type definition *}
    32 text {* Type definition *}
    36 
    33 
    37 inductive Nat :: "ind \<Rightarrow> bool"
    34 inductive Nat :: "ind \<Rightarrow> bool" where
    38 where
    35   Zero_RepI: "Nat Zero_Rep"
    39     Zero_RepI: "Nat Zero_Rep"
    36 | Suc_RepI: "Nat i \<Longrightarrow> Nat (Suc_Rep i)"
    40   | Suc_RepI: "Nat i \<Longrightarrow> Nat (Suc_Rep i)"
       
    41 
    37 
    42 typedef (open Nat) nat = "{n. Nat n}"
    38 typedef (open Nat) nat = "{n. Nat n}"
    43   using Nat.Zero_RepI by auto
    39   using Nat.Zero_RepI by auto
    44 
    40 
    45 lemma Nat_Rep_Nat:
    41 lemma Nat_Rep_Nat:
   140 subsection {* Arithmetic operators *}
   136 subsection {* Arithmetic operators *}
   141 
   137 
   142 instantiation nat :: "{minus, comm_monoid_add}"
   138 instantiation nat :: "{minus, comm_monoid_add}"
   143 begin
   139 begin
   144 
   140 
   145 primrec plus_nat
   141 primrec plus_nat where
   146 where
       
   147   add_0:      "0 + n = (n\<Colon>nat)"
   142   add_0:      "0 + n = (n\<Colon>nat)"
   148   | add_Suc:  "Suc m + n = Suc (m + n)"
   143 | add_Suc:  "Suc m + n = Suc (m + n)"
   149 
   144 
   150 lemma add_0_right [simp]: "m + 0 = (m::nat)"
   145 lemma add_0_right [simp]: "m + 0 = (m::nat)"
   151   by (induct m) simp_all
   146   by (induct m) simp_all
   152 
   147 
   153 lemma add_Suc_right [simp]: "m + Suc n = Suc (m + n)"
   148 lemma add_Suc_right [simp]: "m + Suc n = Suc (m + n)"
   156 declare add_0 [code]
   151 declare add_0 [code]
   157 
   152 
   158 lemma add_Suc_shift [code]: "Suc m + n = m + Suc n"
   153 lemma add_Suc_shift [code]: "Suc m + n = m + Suc n"
   159   by simp
   154   by simp
   160 
   155 
   161 primrec minus_nat
   156 primrec minus_nat where
   162 where
       
   163   diff_0 [code]: "m - 0 = (m\<Colon>nat)"
   157   diff_0 [code]: "m - 0 = (m\<Colon>nat)"
   164 | diff_Suc: "m - Suc n = (case m - n of 0 => 0 | Suc k => k)"
   158 | diff_Suc: "m - Suc n = (case m - n of 0 => 0 | Suc k => k)"
   165 
   159 
   166 declare diff_Suc [simp del]
   160 declare diff_Suc [simp del]
   167 
   161 
   186 begin
   180 begin
   187 
   181 
   188 definition
   182 definition
   189   One_nat_def [simp]: "1 = Suc 0"
   183   One_nat_def [simp]: "1 = Suc 0"
   190 
   184 
   191 primrec times_nat
   185 primrec times_nat where
   192 where
       
   193   mult_0:     "0 * n = (0\<Colon>nat)"
   186   mult_0:     "0 * n = (0\<Colon>nat)"
   194   | mult_Suc: "Suc m * n = n + (m * n)"
   187 | mult_Suc: "Suc m * n = n + (m * n)"
   195 
   188 
   196 lemma mult_0_right [simp]: "(m::nat) * 0 = 0"
   189 lemma mult_0_right [simp]: "(m::nat) * 0 = 0"
   197   by (induct m) simp_all
   190   by (induct m) simp_all
   198 
   191 
   199 lemma mult_Suc_right [simp]: "m * Suc n = m + (m * n)"
   192 lemma mult_Suc_right [simp]: "m * Suc n = m + (m * n)"
   362 instantiation nat :: linorder
   355 instantiation nat :: linorder
   363 begin
   356 begin
   364 
   357 
   365 primrec less_eq_nat where
   358 primrec less_eq_nat where
   366   "(0\<Colon>nat) \<le> n \<longleftrightarrow> True"
   359   "(0\<Colon>nat) \<le> n \<longleftrightarrow> True"
   367   | "Suc m \<le> n \<longleftrightarrow> (case n of 0 \<Rightarrow> False | Suc n \<Rightarrow> m \<le> n)"
   360 | "Suc m \<le> n \<longleftrightarrow> (case n of 0 \<Rightarrow> False | Suc n \<Rightarrow> m \<le> n)"
   368 
   361 
   369 declare less_eq_nat.simps [simp del]
   362 declare less_eq_nat.simps [simp del]
   370 lemma [code]: "(0\<Colon>nat) \<le> n \<longleftrightarrow> True" by (simp add: less_eq_nat.simps)
   363 lemma [code]: "(0\<Colon>nat) \<le> n \<longleftrightarrow> True" by (simp add: less_eq_nat.simps)
   371 lemma le0 [iff]: "0 \<le> (n\<Colon>nat)" by (simp add: less_eq_nat.simps)
   364 lemma le0 [iff]: "0 \<le> (n\<Colon>nat)" by (simp add: less_eq_nat.simps)
   372 
   365 
  1198 overloading
  1191 overloading
  1199   funpow == "compow :: nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)"
  1192   funpow == "compow :: nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)"
  1200 begin
  1193 begin
  1201 
  1194 
  1202 primrec funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
  1195 primrec funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
  1203     "funpow 0 f = id"
  1196   "funpow 0 f = id"
  1204   | "funpow (Suc n) f = f o funpow n f"
  1197 | "funpow (Suc n) f = f o funpow n f"
  1205 
  1198 
  1206 end
  1199 end
  1207 
  1200 
  1208 text {* for code generation *}
  1201 text {* for code generation *}
  1209 
  1202 
  1265 lemma of_nat_mult: "of_nat (m * n) = of_nat m * of_nat n"
  1258 lemma of_nat_mult: "of_nat (m * n) = of_nat m * of_nat n"
  1266   by (induct m) (simp_all add: add_ac left_distrib)
  1259   by (induct m) (simp_all add: add_ac left_distrib)
  1267 
  1260 
  1268 primrec of_nat_aux :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a" where
  1261 primrec of_nat_aux :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a" where
  1269   "of_nat_aux inc 0 i = i"
  1262   "of_nat_aux inc 0 i = i"
  1270   | "of_nat_aux inc (Suc n) i = of_nat_aux inc n (inc i)" -- {* tail recursive *}
  1263 | "of_nat_aux inc (Suc n) i = of_nat_aux inc n (inc i)" -- {* tail recursive *}
  1271 
  1264 
  1272 lemma of_nat_code:
  1265 lemma of_nat_code:
  1273   "of_nat n = of_nat_aux (\<lambda>i. i + 1) n 0"
  1266   "of_nat n = of_nat_aux (\<lambda>i. i + 1) n 0"
  1274 proof (induct n)
  1267 proof (induct n)
  1275   case 0 then show ?case by simp
  1268   case 0 then show ?case by simp