src/HOL/Nat.thy
changeset 24196 f1dbfd7e3223
parent 24162 8dfd5dd65d82
child 24286 7619080e49f0
equal deleted inserted replaced
24195:7d1a16c77f7c 24196:f1dbfd7e3223
   111 
   111 
   112 text {* size of a datatype value *}
   112 text {* size of a datatype value *}
   113 
   113 
   114 class size = type +
   114 class size = type +
   115   fixes size :: "'a \<Rightarrow> nat"
   115   fixes size :: "'a \<Rightarrow> nat"
   116 
       
   117 text {* now we are ready to re-invent primitive types
       
   118   -- dependency on class size is hardwired into datatype package *}
       
   119 
       
   120 rep_datatype bool
       
   121   distinct True_not_False False_not_True
       
   122   induction bool_induct
       
   123 
       
   124 rep_datatype unit
       
   125   induction unit_induct
       
   126 
       
   127 rep_datatype prod
       
   128   inject Pair_eq
       
   129   induction prod_induct
       
   130 
       
   131 rep_datatype sum
       
   132   distinct Inl_not_Inr Inr_not_Inl
       
   133   inject Inl_eq Inr_eq
       
   134   induction sum_induct
       
   135 
   116 
   136 rep_datatype nat
   117 rep_datatype nat
   137   distinct  Suc_not_Zero Zero_not_Suc
   118   distinct  Suc_not_Zero Zero_not_Suc
   138   inject    Suc_Suc_eq
   119   inject    Suc_Suc_eq
   139   induction nat_induct
   120   induction nat_induct
  1099     "(n\<Colon>nat) < 0 \<longleftrightarrow> False"
  1080     "(n\<Colon>nat) < 0 \<longleftrightarrow> False"
  1100     "(n\<Colon>nat) < Suc m \<longleftrightarrow> n \<le> m"
  1081     "(n\<Colon>nat) < Suc m \<longleftrightarrow> n \<le> m"
  1101   using Suc_le_eq less_Suc_eq_le by simp_all
  1082   using Suc_le_eq less_Suc_eq_le by simp_all
  1102 
  1083 
  1103 
  1084 
       
  1085 subsection{*Embedding of the Naturals into any
       
  1086   @{text semiring_1}: @{term of_nat}*}
       
  1087 
       
  1088 context semiring_1
       
  1089 begin
       
  1090 
       
  1091 definition
       
  1092   of_nat_def: "of_nat = nat_rec \<^loc>0 (\<lambda>_. (op \<^loc>+) \<^loc>1)"
       
  1093 
       
  1094 end
       
  1095 
  1104 subsection {* Further Arithmetic Facts Concerning the Natural Numbers *}
  1096 subsection {* Further Arithmetic Facts Concerning the Natural Numbers *}
  1105 
  1097 
  1106 lemma subst_equals:
  1098 lemma subst_equals:
  1107   assumes 1: "t = s" and 2: "u = t"
  1099   assumes 1: "t = s" and 2: "u = t"
  1108   shows "u = s"
  1100   shows "u = s"
  1109   using 2 1 by (rule trans)
  1101   using 2 1 by (rule trans)
       
  1102 
  1110 
  1103 
  1111 use "arith_data.ML"
  1104 use "arith_data.ML"
  1112 declaration {* K arith_data_setup *}
  1105 declaration {* K arith_data_setup *}
  1113 
  1106 
  1114 use "Tools/lin_arith.ML"
  1107 use "Tools/lin_arith.ML"
  1271 lemmas add_diff_assoc2 = diff_add_assoc2[symmetric]
  1264 lemmas add_diff_assoc2 = diff_add_assoc2[symmetric]
  1272 declare diff_diff_left [simp]  add_diff_assoc [simp]  add_diff_assoc2[simp]
  1265 declare diff_diff_left [simp]  add_diff_assoc [simp]  add_diff_assoc2[simp]
  1273 
  1266 
  1274 text{*At present we prove no analogue of @{text not_less_Least} or @{text
  1267 text{*At present we prove no analogue of @{text not_less_Least} or @{text
  1275 Least_Suc}, since there appears to be no need.*}
  1268 Least_Suc}, since there appears to be no need.*}
       
  1269 
       
  1270 
       
  1271 subsection{*Embedding of the Naturals into any
       
  1272   @{text semiring_1}: @{term of_nat}*}
       
  1273 
       
  1274 context semiring_1
       
  1275 begin
       
  1276 
       
  1277 lemma of_nat_simps [simp, code]:
       
  1278   shows of_nat_0:   "of_nat 0 = \<^loc>0"
       
  1279     and of_nat_Suc: "of_nat (Suc m) = \<^loc>1 \<^loc>+ of_nat m"
       
  1280   unfolding of_nat_def by simp_all
       
  1281 
       
  1282 end
       
  1283 
       
  1284 lemma of_nat_id [simp]: "(of_nat n \<Colon> nat) = n"
       
  1285   by (induct n) auto
       
  1286 
       
  1287 lemma of_nat_1 [simp]: "of_nat 1 = 1"
       
  1288   by simp
       
  1289 
       
  1290 lemma of_nat_add [simp]: "of_nat (m + n) = of_nat m + of_nat n"
       
  1291   by (induct m) (simp_all add: add_ac)
       
  1292 
       
  1293 lemma of_nat_mult: "of_nat (m*n) = of_nat m * of_nat n"
       
  1294   by (induct m) (simp_all add: add_ac left_distrib)
       
  1295 
       
  1296 lemma zero_le_imp_of_nat: "0 \<le> (of_nat m::'a::ordered_semidom)"
       
  1297   apply (induct m, simp_all)
       
  1298   apply (erule order_trans)
       
  1299   apply (rule ord_le_eq_trans [OF _ add_commute])
       
  1300   apply (rule less_add_one [THEN order_less_imp_le])
       
  1301   done
       
  1302 
       
  1303 lemma less_imp_of_nat_less:
       
  1304     "m < n ==> of_nat m < (of_nat n::'a::ordered_semidom)"
       
  1305   apply (induct m n rule: diff_induct, simp_all)
       
  1306   apply (insert add_less_le_mono [OF zero_less_one zero_le_imp_of_nat], force)
       
  1307   done
       
  1308 
       
  1309 lemma of_nat_less_imp_less:
       
  1310     "of_nat m < (of_nat n::'a::ordered_semidom) ==> m < n"
       
  1311   apply (induct m n rule: diff_induct, simp_all)
       
  1312   apply (insert zero_le_imp_of_nat)
       
  1313   apply (force simp add: linorder_not_less [symmetric])
       
  1314   done
       
  1315 
       
  1316 lemma of_nat_less_iff [simp]:
       
  1317     "(of_nat m < (of_nat n::'a::ordered_semidom)) = (m<n)"
       
  1318   by (blast intro: of_nat_less_imp_less less_imp_of_nat_less)
       
  1319 
       
  1320 text{*Special cases where either operand is zero*}
       
  1321 
       
  1322 lemma of_nat_0_less_iff [simp]: "((0::'a::ordered_semidom) < of_nat n) = (0 < n)"
       
  1323   by (rule of_nat_less_iff [of 0, simplified])
       
  1324 
       
  1325 lemma of_nat_less_0_iff [simp]: "\<not> of_nat m < (0::'a::ordered_semidom)"
       
  1326   by (rule of_nat_less_iff [of _ 0, simplified])
       
  1327 
       
  1328 lemma of_nat_le_iff [simp]:
       
  1329     "(of_nat m \<le> (of_nat n::'a::ordered_semidom)) = (m \<le> n)"
       
  1330   by (simp add: linorder_not_less [symmetric])
       
  1331 
       
  1332 text{*Special cases where either operand is zero*}
       
  1333 lemma of_nat_0_le_iff [simp]: "(0::'a::ordered_semidom) \<le> of_nat n"
       
  1334   by (rule of_nat_le_iff [of 0, simplified])
       
  1335 lemma of_nat_le_0_iff [simp]: "(of_nat m \<le> (0::'a::ordered_semidom)) = (m = 0)"
       
  1336   by (rule of_nat_le_iff [of _ 0, simplified])
       
  1337 
       
  1338 text{*Class for unital semirings with characteristic zero.
       
  1339  Includes non-ordered rings like the complex numbers.*}
       
  1340 
       
  1341 class semiring_char_0 = semiring_1 +
       
  1342   assumes of_nat_eq_iff [simp]:
       
  1343     "(Nat.semiring_1.of_nat \<^loc>1 \<^loc>0 (op \<^loc>+) m = Nat.semiring_1.of_nat \<^loc>1 \<^loc>0 (op \<^loc>+)  n) = (m = n)"
       
  1344 
       
  1345 text{*Every @{text ordered_semidom} has characteristic zero.*}
       
  1346 instance ordered_semidom < semiring_char_0
       
  1347 by intro_classes (simp add: order_eq_iff)
       
  1348 
       
  1349 text{*Special cases where either operand is zero*}
       
  1350 lemma of_nat_0_eq_iff [simp]: "((0::'a::semiring_char_0) = of_nat n) = (0 = n)"
       
  1351   by (rule of_nat_eq_iff [of 0, simplified])
       
  1352 lemma of_nat_eq_0_iff [simp]: "(of_nat m = (0::'a::semiring_char_0)) = (m = 0)"
       
  1353   by (rule of_nat_eq_iff [of _ 0, simplified])
       
  1354 
       
  1355 lemma inj_of_nat: "inj (of_nat :: nat \<Rightarrow> 'a::semiring_char_0)"
       
  1356   by (simp add: inj_on_def)
       
  1357 
       
  1358 lemma of_nat_diff:
       
  1359     "n \<le> m ==> of_nat (m - n) = of_nat m - (of_nat n :: 'a::ring_1)"
       
  1360   by (simp del: of_nat_add
       
  1361     add: compare_rls of_nat_add [symmetric] split add: nat_diff_split)
       
  1362 
       
  1363 lemma abs_of_nat [simp]: "\<bar>of_nat n::'a::ordered_idom\<bar> = of_nat n"
       
  1364   by (rule of_nat_0_le_iff [THEN abs_of_nonneg])
       
  1365 
       
  1366 
       
  1367 subsection {*The Set of Natural Numbers*}
       
  1368 
       
  1369 definition
       
  1370   Nats  :: "'a::semiring_1 set"
       
  1371 where
       
  1372   "Nats = range of_nat"
       
  1373 
       
  1374 notation (xsymbols)
       
  1375   Nats  ("\<nat>")
       
  1376 
       
  1377 lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
       
  1378   by (simp add: Nats_def)
       
  1379 
       
  1380 lemma Nats_0 [simp]: "0 \<in> Nats"
       
  1381 apply (simp add: Nats_def)
       
  1382 apply (rule range_eqI)
       
  1383 apply (rule of_nat_0 [symmetric])
       
  1384 done
       
  1385 
       
  1386 lemma Nats_1 [simp]: "1 \<in> Nats"
       
  1387 apply (simp add: Nats_def)
       
  1388 apply (rule range_eqI)
       
  1389 apply (rule of_nat_1 [symmetric])
       
  1390 done
       
  1391 
       
  1392 lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats"
       
  1393 apply (auto simp add: Nats_def)
       
  1394 apply (rule range_eqI)
       
  1395 apply (rule of_nat_add [symmetric])
       
  1396 done
       
  1397 
       
  1398 lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats"
       
  1399 apply (auto simp add: Nats_def)
       
  1400 apply (rule range_eqI)
       
  1401 apply (rule of_nat_mult [symmetric])
       
  1402 done
       
  1403 
       
  1404 lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)"
       
  1405   by (auto simp add: expand_fun_eq)
       
  1406 
       
  1407 
       
  1408 instance nat :: distrib_lattice
       
  1409   "inf \<equiv> min"
       
  1410   "sup \<equiv> max"
       
  1411   by intro_classes (auto simp add: inf_nat_def sup_nat_def)
       
  1412 
       
  1413 
       
  1414 subsection {* Size function *}
       
  1415 
       
  1416 lemma nat_size [simp, code func]: "size (n\<Colon>nat) = n"
       
  1417   by (induct n) simp_all
       
  1418 
       
  1419 subsection {* legacy bindings *}
  1276 
  1420 
  1277 ML
  1421 ML
  1278 {*
  1422 {*
  1279 val pred_nat_trancl_eq_le = thm "pred_nat_trancl_eq_le";
  1423 val pred_nat_trancl_eq_le = thm "pred_nat_trancl_eq_le";
  1280 val nat_diff_split = thm "nat_diff_split";
  1424 val nat_diff_split = thm "nat_diff_split";
  1302 val diff_diff_right = thm "diff_diff_right";
  1446 val diff_diff_right = thm "diff_diff_right";
  1303 val diff_Suc_diff_eq1 = thm "diff_Suc_diff_eq1";
  1447 val diff_Suc_diff_eq1 = thm "diff_Suc_diff_eq1";
  1304 val diff_Suc_diff_eq2 = thm "diff_Suc_diff_eq2";
  1448 val diff_Suc_diff_eq2 = thm "diff_Suc_diff_eq2";
  1305 *}
  1449 *}
  1306 
  1450 
  1307 
       
  1308 subsection{*Embedding of the Naturals into any
       
  1309   @{text semiring_1}: @{term of_nat}*}
       
  1310 
       
  1311 consts of_nat :: "nat => 'a::semiring_1"
       
  1312 
       
  1313 primrec
       
  1314   of_nat_0:   "of_nat 0 = 0"
       
  1315   of_nat_Suc: "of_nat (Suc m) = 1 + of_nat m"
       
  1316 
       
  1317 lemma of_nat_id [simp]: "(of_nat n \<Colon> nat) = n"
       
  1318   by (induct n) auto
       
  1319 
       
  1320 lemma of_nat_1 [simp]: "of_nat 1 = 1"
       
  1321   by simp
       
  1322 
       
  1323 lemma of_nat_add [simp]: "of_nat (m+n) = of_nat m + of_nat n"
       
  1324   by (induct m) (simp_all add: add_ac)
       
  1325 
       
  1326 lemma of_nat_mult: "of_nat (m*n) = of_nat m * of_nat n"
       
  1327   by (induct m) (simp_all add: add_ac left_distrib)
       
  1328 
       
  1329 lemma zero_le_imp_of_nat: "0 \<le> (of_nat m::'a::ordered_semidom)"
       
  1330   apply (induct m, simp_all)
       
  1331   apply (erule order_trans)
       
  1332   apply (rule ord_le_eq_trans [OF _ add_commute])
       
  1333   apply (rule less_add_one [THEN order_less_imp_le])
       
  1334   done
       
  1335 
       
  1336 lemma less_imp_of_nat_less:
       
  1337     "m < n ==> of_nat m < (of_nat n::'a::ordered_semidom)"
       
  1338   apply (induct m n rule: diff_induct, simp_all)
       
  1339   apply (insert add_less_le_mono [OF zero_less_one zero_le_imp_of_nat], force)
       
  1340   done
       
  1341 
       
  1342 lemma of_nat_less_imp_less:
       
  1343     "of_nat m < (of_nat n::'a::ordered_semidom) ==> m < n"
       
  1344   apply (induct m n rule: diff_induct, simp_all)
       
  1345   apply (insert zero_le_imp_of_nat)
       
  1346   apply (force simp add: linorder_not_less [symmetric])
       
  1347   done
       
  1348 
       
  1349 lemma of_nat_less_iff [simp]:
       
  1350     "(of_nat m < (of_nat n::'a::ordered_semidom)) = (m<n)"
       
  1351   by (blast intro: of_nat_less_imp_less less_imp_of_nat_less)
       
  1352 
       
  1353 text{*Special cases where either operand is zero*}
       
  1354 
       
  1355 lemma of_nat_0_less_iff [simp]: "((0::'a::ordered_semidom) < of_nat n) = (0 < n)"
       
  1356   by (rule of_nat_less_iff [of 0, simplified])
       
  1357 
       
  1358 lemma of_nat_less_0_iff [simp]: "\<not> of_nat m < (0::'a::ordered_semidom)"
       
  1359   by (rule of_nat_less_iff [of _ 0, simplified])
       
  1360 
       
  1361 lemma of_nat_le_iff [simp]:
       
  1362     "(of_nat m \<le> (of_nat n::'a::ordered_semidom)) = (m \<le> n)"
       
  1363   by (simp add: linorder_not_less [symmetric])
       
  1364 
       
  1365 text{*Special cases where either operand is zero*}
       
  1366 lemma of_nat_0_le_iff [simp]: "(0::'a::ordered_semidom) \<le> of_nat n"
       
  1367   by (rule of_nat_le_iff [of 0, simplified])
       
  1368 lemma of_nat_le_0_iff [simp]: "(of_nat m \<le> (0::'a::ordered_semidom)) = (m = 0)"
       
  1369   by (rule of_nat_le_iff [of _ 0, simplified])
       
  1370 
       
  1371 text{*Class for unital semirings with characteristic zero.
       
  1372  Includes non-ordered rings like the complex numbers.*}
       
  1373 axclass semiring_char_0 < semiring_1
       
  1374   of_nat_eq_iff [simp]: "(of_nat m = of_nat n) = (m = n)"
       
  1375 
       
  1376 text{*Every @{text ordered_semidom} has characteristic zero.*}
       
  1377 instance ordered_semidom < semiring_char_0
       
  1378 by intro_classes (simp add: order_eq_iff)
       
  1379 
       
  1380 text{*Special cases where either operand is zero*}
       
  1381 lemma of_nat_0_eq_iff [simp]: "((0::'a::semiring_char_0) = of_nat n) = (0 = n)"
       
  1382   by (rule of_nat_eq_iff [of 0, simplified])
       
  1383 lemma of_nat_eq_0_iff [simp]: "(of_nat m = (0::'a::semiring_char_0)) = (m = 0)"
       
  1384   by (rule of_nat_eq_iff [of _ 0, simplified])
       
  1385 
       
  1386 lemma inj_of_nat: "inj (of_nat :: nat \<Rightarrow> 'a::semiring_char_0)"
       
  1387   by (simp add: inj_on_def)
       
  1388 
       
  1389 lemma of_nat_diff:
       
  1390     "n \<le> m ==> of_nat (m - n) = of_nat m - (of_nat n :: 'a::ring_1)"
       
  1391   by (simp del: of_nat_add
       
  1392     add: compare_rls of_nat_add [symmetric] split add: nat_diff_split)
       
  1393 
       
  1394 
       
  1395 subsection {*The Set of Natural Numbers*}
       
  1396 
       
  1397 definition
       
  1398   Nats  :: "'a::semiring_1 set"
       
  1399 where
       
  1400   "Nats = range of_nat"
       
  1401 
       
  1402 notation (xsymbols)
       
  1403   Nats  ("\<nat>")
       
  1404 
       
  1405 lemma of_nat_in_Nats [simp]: "of_nat n \<in> Nats"
       
  1406   by (simp add: Nats_def)
       
  1407 
       
  1408 lemma Nats_0 [simp]: "0 \<in> Nats"
       
  1409 apply (simp add: Nats_def)
       
  1410 apply (rule range_eqI)
       
  1411 apply (rule of_nat_0 [symmetric])
       
  1412 done
       
  1413 
       
  1414 lemma Nats_1 [simp]: "1 \<in> Nats"
       
  1415 apply (simp add: Nats_def)
       
  1416 apply (rule range_eqI)
       
  1417 apply (rule of_nat_1 [symmetric])
       
  1418 done
       
  1419 
       
  1420 lemma Nats_add [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a+b \<in> Nats"
       
  1421 apply (auto simp add: Nats_def)
       
  1422 apply (rule range_eqI)
       
  1423 apply (rule of_nat_add [symmetric])
       
  1424 done
       
  1425 
       
  1426 lemma Nats_mult [simp]: "[|a \<in> Nats; b \<in> Nats|] ==> a*b \<in> Nats"
       
  1427 apply (auto simp add: Nats_def)
       
  1428 apply (rule range_eqI)
       
  1429 apply (rule of_nat_mult [symmetric])
       
  1430 done
       
  1431 
       
  1432 lemma of_nat_eq_id [simp]: "of_nat = (id :: nat => nat)"
       
  1433   by (auto simp add: expand_fun_eq)
       
  1434 
       
  1435 
       
  1436 instance nat :: distrib_lattice
       
  1437   "inf \<equiv> min"
       
  1438   "sup \<equiv> max"
       
  1439   by intro_classes (auto simp add: inf_nat_def sup_nat_def)
       
  1440 
       
  1441 
       
  1442 subsection {* Size function *}
       
  1443 
       
  1444 lemma nat_size [simp, code func]: "size (n\<Colon>nat) = n"
       
  1445   by (induct n) simp_all
       
  1446 
       
  1447 end
  1451 end