| author | haftmann | 
| Sun, 15 Nov 2020 10:13:03 +0000 | |
| changeset 72611 | c7bc3e70a8c7 | 
| parent 71991 | 8bff286878bf | 
| child 74592 | 3c587b7c3d5c | 
| permissions | -rw-r--r-- | 
| 47108 | 1 | (* Title: HOL/Num.thy | 
| 2 | Author: Florian Haftmann | |
| 3 | Author: Brian Huffman | |
| 4 | *) | |
| 5 | ||
| 60758 | 6 | section \<open>Binary Numerals\<close> | 
| 47108 | 7 | |
| 8 | theory Num | |
| 64178 | 9 | imports BNF_Least_Fixpoint Transfer | 
| 47108 | 10 | begin | 
| 11 | ||
| 61799 | 12 | subsection \<open>The \<open>num\<close> type\<close> | 
| 47108 | 13 | |
| 58310 | 14 | datatype num = One | Bit0 num | Bit1 num | 
| 47108 | 15 | |
| 69593 | 16 | text \<open>Increment function for type \<^typ>\<open>num\<close>\<close> | 
| 47108 | 17 | |
| 63654 | 18 | primrec inc :: "num \<Rightarrow> num" | 
| 19 | where | |
| 20 | "inc One = Bit0 One" | |
| 21 | | "inc (Bit0 x) = Bit1 x" | |
| 22 | | "inc (Bit1 x) = Bit0 (inc x)" | |
| 47108 | 23 | |
| 69593 | 24 | text \<open>Converting between type \<^typ>\<open>num\<close> and type \<^typ>\<open>nat\<close>\<close> | 
| 47108 | 25 | |
| 63654 | 26 | primrec nat_of_num :: "num \<Rightarrow> nat" | 
| 27 | where | |
| 28 | "nat_of_num One = Suc 0" | |
| 29 | | "nat_of_num (Bit0 x) = nat_of_num x + nat_of_num x" | |
| 30 | | "nat_of_num (Bit1 x) = Suc (nat_of_num x + nat_of_num x)" | |
| 47108 | 31 | |
| 63654 | 32 | primrec num_of_nat :: "nat \<Rightarrow> num" | 
| 33 | where | |
| 34 | "num_of_nat 0 = One" | |
| 35 | | "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)" | |
| 47108 | 36 | |
| 37 | lemma nat_of_num_pos: "0 < nat_of_num x" | |
| 38 | by (induct x) simp_all | |
| 39 | ||
| 40 | lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0" | |
| 41 | by (induct x) simp_all | |
| 42 | ||
| 43 | lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)" | |
| 44 | by (induct x) simp_all | |
| 45 | ||
| 63654 | 46 | lemma num_of_nat_double: "0 < n \<Longrightarrow> num_of_nat (n + n) = Bit0 (num_of_nat n)" | 
| 47108 | 47 | by (induct n) simp_all | 
| 48 | ||
| 69593 | 49 | text \<open>Type \<^typ>\<open>num\<close> is isomorphic to the strictly positive natural numbers.\<close> | 
| 47108 | 50 | |
| 51 | lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x" | |
| 52 | by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos) | |
| 53 | ||
| 54 | lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n" | |
| 55 | by (induct n) (simp_all add: nat_of_num_inc) | |
| 56 | ||
| 57 | lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y" | |
| 58 | apply safe | |
| 59 | apply (drule arg_cong [where f=num_of_nat]) | |
| 60 | apply (simp add: nat_of_num_inverse) | |
| 61 | done | |
| 62 | ||
| 63 | lemma num_induct [case_names One inc]: | |
| 64 | fixes P :: "num \<Rightarrow> bool" | |
| 65 | assumes One: "P One" | |
| 66 | and inc: "\<And>x. P x \<Longrightarrow> P (inc x)" | |
| 67 | shows "P x" | |
| 68 | proof - | |
| 69 | obtain n where n: "Suc n = nat_of_num x" | |
| 63654 | 70 | by (cases "nat_of_num x") (simp_all add: nat_of_num_neq_0) | 
| 47108 | 71 | have "P (num_of_nat (Suc n))" | 
| 72 | proof (induct n) | |
| 63654 | 73 | case 0 | 
| 74 | from One show ?case by simp | |
| 47108 | 75 | next | 
| 76 | case (Suc n) | |
| 77 | then have "P (inc (num_of_nat (Suc n)))" by (rule inc) | |
| 78 | then show "P (num_of_nat (Suc (Suc n)))" by simp | |
| 79 | qed | |
| 80 | with n show "P x" | |
| 81 | by (simp add: nat_of_num_inverse) | |
| 82 | qed | |
| 83 | ||
| 60758 | 84 | text \<open> | 
| 69593 | 85 | From now on, there are two possible models for \<^typ>\<open>num\<close>: as positive | 
| 63654 | 86 | naturals (rule \<open>num_induct\<close>) and as digit representation (rules | 
| 87 | \<open>num.induct\<close>, \<open>num.cases\<close>). | |
| 60758 | 88 | \<close> | 
| 47108 | 89 | |
| 90 | ||
| 60758 | 91 | subsection \<open>Numeral operations\<close> | 
| 47108 | 92 | |
| 93 | instantiation num :: "{plus,times,linorder}"
 | |
| 94 | begin | |
| 95 | ||
| 63654 | 96 | definition [code del]: "m + n = num_of_nat (nat_of_num m + nat_of_num n)" | 
| 47108 | 97 | |
| 63654 | 98 | definition [code del]: "m * n = num_of_nat (nat_of_num m * nat_of_num n)" | 
| 47108 | 99 | |
| 63654 | 100 | definition [code del]: "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n" | 
| 47108 | 101 | |
| 63654 | 102 | definition [code del]: "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n" | 
| 47108 | 103 | |
| 104 | instance | |
| 61169 | 105 | by standard (auto simp add: less_num_def less_eq_num_def num_eq_iff) | 
| 47108 | 106 | |
| 107 | end | |
| 108 | ||
| 109 | lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y" | |
| 110 | unfolding plus_num_def | |
| 111 | by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos) | |
| 112 | ||
| 113 | lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y" | |
| 114 | unfolding times_num_def | |
| 115 | by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos) | |
| 116 | ||
| 117 | lemma add_num_simps [simp, code]: | |
| 118 | "One + One = Bit0 One" | |
| 119 | "One + Bit0 n = Bit1 n" | |
| 120 | "One + Bit1 n = Bit0 (n + One)" | |
| 121 | "Bit0 m + One = Bit1 m" | |
| 122 | "Bit0 m + Bit0 n = Bit0 (m + n)" | |
| 123 | "Bit0 m + Bit1 n = Bit1 (m + n)" | |
| 124 | "Bit1 m + One = Bit0 (m + One)" | |
| 125 | "Bit1 m + Bit0 n = Bit1 (m + n)" | |
| 126 | "Bit1 m + Bit1 n = Bit0 (m + n + One)" | |
| 127 | by (simp_all add: num_eq_iff nat_of_num_add) | |
| 128 | ||
| 129 | lemma mult_num_simps [simp, code]: | |
| 130 | "m * One = m" | |
| 131 | "One * n = n" | |
| 132 | "Bit0 m * Bit0 n = Bit0 (Bit0 (m * n))" | |
| 133 | "Bit0 m * Bit1 n = Bit0 (m * Bit1 n)" | |
| 134 | "Bit1 m * Bit0 n = Bit0 (Bit1 m * n)" | |
| 135 | "Bit1 m * Bit1 n = Bit1 (m + n + Bit0 (m * n))" | |
| 63654 | 136 | by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult distrib_right distrib_left) | 
| 47108 | 137 | |
| 138 | lemma eq_num_simps: | |
| 139 | "One = One \<longleftrightarrow> True" | |
| 140 | "One = Bit0 n \<longleftrightarrow> False" | |
| 141 | "One = Bit1 n \<longleftrightarrow> False" | |
| 142 | "Bit0 m = One \<longleftrightarrow> False" | |
| 143 | "Bit1 m = One \<longleftrightarrow> False" | |
| 144 | "Bit0 m = Bit0 n \<longleftrightarrow> m = n" | |
| 145 | "Bit0 m = Bit1 n \<longleftrightarrow> False" | |
| 146 | "Bit1 m = Bit0 n \<longleftrightarrow> False" | |
| 147 | "Bit1 m = Bit1 n \<longleftrightarrow> m = n" | |
| 148 | by simp_all | |
| 149 | ||
| 150 | lemma le_num_simps [simp, code]: | |
| 151 | "One \<le> n \<longleftrightarrow> True" | |
| 152 | "Bit0 m \<le> One \<longleftrightarrow> False" | |
| 153 | "Bit1 m \<le> One \<longleftrightarrow> False" | |
| 154 | "Bit0 m \<le> Bit0 n \<longleftrightarrow> m \<le> n" | |
| 155 | "Bit0 m \<le> Bit1 n \<longleftrightarrow> m \<le> n" | |
| 156 | "Bit1 m \<le> Bit1 n \<longleftrightarrow> m \<le> n" | |
| 157 | "Bit1 m \<le> Bit0 n \<longleftrightarrow> m < n" | |
| 158 | using nat_of_num_pos [of n] nat_of_num_pos [of m] | |
| 159 | by (auto simp add: less_eq_num_def less_num_def) | |
| 160 | ||
| 161 | lemma less_num_simps [simp, code]: | |
| 162 | "m < One \<longleftrightarrow> False" | |
| 163 | "One < Bit0 n \<longleftrightarrow> True" | |
| 164 | "One < Bit1 n \<longleftrightarrow> True" | |
| 165 | "Bit0 m < Bit0 n \<longleftrightarrow> m < n" | |
| 166 | "Bit0 m < Bit1 n \<longleftrightarrow> m \<le> n" | |
| 167 | "Bit1 m < Bit1 n \<longleftrightarrow> m < n" | |
| 168 | "Bit1 m < Bit0 n \<longleftrightarrow> m < n" | |
| 169 | using nat_of_num_pos [of n] nat_of_num_pos [of m] | |
| 170 | by (auto simp add: less_eq_num_def less_num_def) | |
| 171 | ||
| 61630 | 172 | lemma le_num_One_iff: "x \<le> num.One \<longleftrightarrow> x = num.One" | 
| 63654 | 173 | by (simp add: antisym_conv) | 
| 61630 | 174 | |
| 63654 | 175 | text \<open>Rules using \<open>One\<close> and \<open>inc\<close> as constructors.\<close> | 
| 47108 | 176 | |
| 177 | lemma add_One: "x + One = inc x" | |
| 178 | by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc) | |
| 179 | ||
| 180 | lemma add_One_commute: "One + n = n + One" | |
| 181 | by (induct n) simp_all | |
| 182 | ||
| 183 | lemma add_inc: "x + inc y = inc (x + y)" | |
| 184 | by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc) | |
| 185 | ||
| 186 | lemma mult_inc: "x * inc y = x * y + x" | |
| 187 | by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc) | |
| 188 | ||
| 69593 | 189 | text \<open>The \<^const>\<open>num_of_nat\<close> conversion.\<close> | 
| 47108 | 190 | |
| 63654 | 191 | lemma num_of_nat_One: "n \<le> 1 \<Longrightarrow> num_of_nat n = One" | 
| 47108 | 192 | by (cases n) simp_all | 
| 193 | ||
| 194 | lemma num_of_nat_plus_distrib: | |
| 195 | "0 < m \<Longrightarrow> 0 < n \<Longrightarrow> num_of_nat (m + n) = num_of_nat m + num_of_nat n" | |
| 196 | by (induct n) (auto simp add: add_One add_One_commute add_inc) | |
| 197 | ||
| 63654 | 198 | text \<open>A double-and-decrement function.\<close> | 
| 47108 | 199 | |
| 63654 | 200 | primrec BitM :: "num \<Rightarrow> num" | 
| 201 | where | |
| 202 | "BitM One = One" | |
| 203 | | "BitM (Bit0 n) = Bit1 (BitM n)" | |
| 204 | | "BitM (Bit1 n) = Bit1 (Bit0 n)" | |
| 47108 | 205 | |
| 206 | lemma BitM_plus_one: "BitM n + One = Bit0 n" | |
| 207 | by (induct n) simp_all | |
| 208 | ||
| 209 | lemma one_plus_BitM: "One + BitM n = Bit0 n" | |
| 210 | unfolding add_One_commute BitM_plus_one .. | |
| 211 | ||
| 71991 | 212 | lemma BitM_inc_eq: | 
| 213 | \<open>Num.BitM (Num.inc n) = Num.Bit1 n\<close> | |
| 214 | by (induction n) simp_all | |
| 215 | ||
| 216 | lemma inc_BitM_eq: | |
| 217 | \<open>Num.inc (Num.BitM n) = num.Bit0 n\<close> | |
| 218 | by (simp add: BitM_plus_one[symmetric] add_One) | |
| 219 | ||
| 63654 | 220 | text \<open>Squaring and exponentiation.\<close> | 
| 47108 | 221 | |
| 63654 | 222 | primrec sqr :: "num \<Rightarrow> num" | 
| 223 | where | |
| 224 | "sqr One = One" | |
| 225 | | "sqr (Bit0 n) = Bit0 (Bit0 (sqr n))" | |
| 226 | | "sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))" | |
| 47108 | 227 | |
| 63654 | 228 | primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" | 
| 229 | where | |
| 230 | "pow x One = x" | |
| 231 | | "pow x (Bit0 y) = sqr (pow x y)" | |
| 232 | | "pow x (Bit1 y) = sqr (pow x y) * x" | |
| 47108 | 233 | |
| 234 | lemma nat_of_num_sqr: "nat_of_num (sqr x) = nat_of_num x * nat_of_num x" | |
| 63654 | 235 | by (induct x) (simp_all add: algebra_simps nat_of_num_add) | 
| 47108 | 236 | |
| 237 | lemma sqr_conv_mult: "sqr x = x * x" | |
| 238 | by (simp add: num_eq_iff nat_of_num_sqr nat_of_num_mult) | |
| 239 | ||
| 70226 | 240 | lemma num_double [simp]: | 
| 241 | "num.Bit0 num.One * n = num.Bit0 n" | |
| 242 | by (simp add: num_eq_iff nat_of_num_mult) | |
| 243 | ||
| 47108 | 244 | |
| 60758 | 245 | subsection \<open>Binary numerals\<close> | 
| 47108 | 246 | |
| 60758 | 247 | text \<open> | 
| 47211 | 248 | We embed binary representations into a generic algebraic | 
| 61799 | 249 | structure using \<open>numeral\<close>. | 
| 60758 | 250 | \<close> | 
| 47108 | 251 | |
| 252 | class numeral = one + semigroup_add | |
| 253 | begin | |
| 254 | ||
| 63654 | 255 | primrec numeral :: "num \<Rightarrow> 'a" | 
| 256 | where | |
| 257 | numeral_One: "numeral One = 1" | |
| 258 | | numeral_Bit0: "numeral (Bit0 n) = numeral n + numeral n" | |
| 259 | | numeral_Bit1: "numeral (Bit1 n) = numeral n + numeral n + 1" | |
| 47108 | 260 | |
| 50817 | 261 | lemma numeral_code [code]: | 
| 262 | "numeral One = 1" | |
| 263 | "numeral (Bit0 n) = (let m = numeral n in m + m)" | |
| 264 | "numeral (Bit1 n) = (let m = numeral n in m + m + 1)" | |
| 265 | by (simp_all add: Let_def) | |
| 63654 | 266 | |
| 47108 | 267 | lemma one_plus_numeral_commute: "1 + numeral x = numeral x + 1" | 
| 63654 | 268 | proof (induct x) | 
| 269 | case One | |
| 270 | then show ?case by simp | |
| 271 | next | |
| 272 | case Bit0 | |
| 273 | then show ?case by (simp add: add.assoc [symmetric]) (simp add: add.assoc) | |
| 274 | next | |
| 275 | case Bit1 | |
| 276 | then show ?case by (simp add: add.assoc [symmetric]) (simp add: add.assoc) | |
| 277 | qed | |
| 47108 | 278 | |
| 279 | lemma numeral_inc: "numeral (inc x) = numeral x + 1" | |
| 280 | proof (induct x) | |
| 63654 | 281 | case One | 
| 282 | then show ?case by simp | |
| 283 | next | |
| 284 | case Bit0 | |
| 285 | then show ?case by simp | |
| 286 | next | |
| 47108 | 287 | case (Bit1 x) | 
| 288 | have "numeral x + (1 + numeral x) + 1 = numeral x + (numeral x + 1) + 1" | |
| 289 | by (simp only: one_plus_numeral_commute) | |
| 290 | with Bit1 show ?case | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
55974diff
changeset | 291 | by (simp add: add.assoc) | 
| 63654 | 292 | qed | 
| 47108 | 293 | |
| 294 | declare numeral.simps [simp del] | |
| 295 | ||
| 296 | abbreviation "Numeral1 \<equiv> numeral One" | |
| 297 | ||
| 298 | declare numeral_One [code_post] | |
| 299 | ||
| 300 | end | |
| 301 | ||
| 60758 | 302 | text \<open>Numeral syntax.\<close> | 
| 47108 | 303 | |
| 304 | syntax | |
| 305 |   "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
 | |
| 306 | ||
| 69605 | 307 | ML_file \<open>Tools/numeral.ML\<close> | 
| 58410 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58310diff
changeset | 308 | |
| 60758 | 309 | parse_translation \<open> | 
| 52143 | 310 | let | 
| 69593 | 311 | fun numeral_tr [(c as Const (\<^syntax_const>\<open>_constrain\<close>, _)) $ t $ u] = | 
| 52143 | 312 | c $ numeral_tr [t] $ u | 
| 313 | | numeral_tr [Const (num, _)] = | |
| 58421 | 314 | (Numeral.mk_number_syntax o #value o Lexicon.read_num) num | 
| 52143 | 315 |       | numeral_tr ts = raise TERM ("numeral_tr", ts);
 | 
| 69593 | 316 | in [(\<^syntax_const>\<open>_Numeral\<close>, K numeral_tr)] end | 
| 60758 | 317 | \<close> | 
| 47108 | 318 | |
| 60758 | 319 | typed_print_translation \<open> | 
| 52143 | 320 | let | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 321 | fun num_tr' ctxt T [n] = | 
| 52143 | 322 | let | 
| 62597 | 323 | val k = Numeral.dest_num_syntax n; | 
| 52187 | 324 | val t' = | 
| 69593 | 325 | Syntax.const \<^syntax_const>\<open>_Numeral\<close> $ | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 326 | Syntax.free (string_of_int k); | 
| 52143 | 327 | in | 
| 328 | (case T of | |
| 69593 | 329 | Type (\<^type_name>\<open>fun\<close>, [_, T']) => | 
| 52210 
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
 wenzelm parents: 
52187diff
changeset | 330 | if Printer.type_emphasis ctxt T' then | 
| 69593 | 331 | Syntax.const \<^syntax_const>\<open>_constrain\<close> $ t' $ | 
| 52210 
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
 wenzelm parents: 
52187diff
changeset | 332 | Syntax_Phases.term_of_typ ctxt T' | 
| 
0226035df99d
more explicit Printer.type_emphasis, depending on show_type_emphasis;
 wenzelm parents: 
52187diff
changeset | 333 | else t' | 
| 52187 | 334 | | _ => if T = dummyT then t' else raise Match) | 
| 52143 | 335 | end; | 
| 336 | in | |
| 69593 | 337 | [(\<^const_syntax>\<open>numeral\<close>, num_tr')] | 
| 52143 | 338 | end | 
| 60758 | 339 | \<close> | 
| 47108 | 340 | |
| 47228 | 341 | |
| 60758 | 342 | subsection \<open>Class-specific numeral rules\<close> | 
| 47108 | 343 | |
| 69593 | 344 | text \<open>\<^const>\<open>numeral\<close> is a morphism.\<close> | 
| 63654 | 345 | |
| 47108 | 346 | |
| 61799 | 347 | subsubsection \<open>Structures with addition: class \<open>numeral\<close>\<close> | 
| 47108 | 348 | |
| 349 | context numeral | |
| 350 | begin | |
| 351 | ||
| 352 | lemma numeral_add: "numeral (m + n) = numeral m + numeral n" | |
| 353 | by (induct n rule: num_induct) | |
| 63654 | 354 | (simp_all only: numeral_One add_One add_inc numeral_inc add.assoc) | 
| 47108 | 355 | |
| 356 | lemma numeral_plus_numeral: "numeral m + numeral n = numeral (m + n)" | |
| 357 | by (rule numeral_add [symmetric]) | |
| 358 | ||
| 359 | lemma numeral_plus_one: "numeral n + 1 = numeral (n + One)" | |
| 360 | using numeral_add [of n One] by (simp add: numeral_One) | |
| 361 | ||
| 362 | lemma one_plus_numeral: "1 + numeral n = numeral (One + n)" | |
| 363 | using numeral_add [of One n] by (simp add: numeral_One) | |
| 364 | ||
| 365 | lemma one_add_one: "1 + 1 = 2" | |
| 366 | using numeral_add [of One One] by (simp add: numeral_One) | |
| 367 | ||
| 368 | lemmas add_numeral_special = | |
| 369 | numeral_plus_one one_plus_numeral one_add_one | |
| 370 | ||
| 371 | end | |
| 372 | ||
| 63654 | 373 | |
| 374 | subsubsection \<open>Structures with negation: class \<open>neg_numeral\<close>\<close> | |
| 47108 | 375 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 376 | class neg_numeral = numeral + group_add | 
| 47108 | 377 | begin | 
| 378 | ||
| 63654 | 379 | lemma uminus_numeral_One: "- Numeral1 = - 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 380 | by (simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 381 | |
| 60758 | 382 | text \<open>Numerals form an abelian subgroup.\<close> | 
| 47108 | 383 | |
| 63654 | 384 | inductive is_num :: "'a \<Rightarrow> bool" | 
| 385 | where | |
| 386 | "is_num 1" | |
| 387 | | "is_num x \<Longrightarrow> is_num (- x)" | |
| 388 | | "is_num x \<Longrightarrow> is_num y \<Longrightarrow> is_num (x + y)" | |
| 47108 | 389 | |
| 390 | lemma is_num_numeral: "is_num (numeral k)" | |
| 63654 | 391 | by (induct k) (simp_all add: numeral.simps is_num.intros) | 
| 47108 | 392 | |
| 63654 | 393 | lemma is_num_add_commute: "is_num x \<Longrightarrow> is_num y \<Longrightarrow> x + y = y + x" | 
| 47108 | 394 | apply (induct x rule: is_num.induct) | 
| 63654 | 395 | apply (induct y rule: is_num.induct) | 
| 396 | apply simp | |
| 397 | apply (rule_tac a=x in add_left_imp_eq) | |
| 398 | apply (rule_tac a=x in add_right_imp_eq) | |
| 399 | apply (simp add: add.assoc) | |
| 400 | apply (simp add: add.assoc [symmetric]) | |
| 401 | apply (simp add: add.assoc) | |
| 402 | apply (rule_tac a=x in add_left_imp_eq) | |
| 403 | apply (rule_tac a=x in add_right_imp_eq) | |
| 404 | apply (simp add: add.assoc) | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
55974diff
changeset | 405 | apply (simp add: add.assoc) | 
| 63654 | 406 | apply (simp add: add.assoc [symmetric]) | 
| 47108 | 407 | done | 
| 408 | ||
| 63654 | 409 | lemma is_num_add_left_commute: "is_num x \<Longrightarrow> is_num y \<Longrightarrow> x + (y + z) = y + (x + z)" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
55974diff
changeset | 410 | by (simp only: add.assoc [symmetric] is_num_add_commute) | 
| 47108 | 411 | |
| 412 | lemmas is_num_normalize = | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
55974diff
changeset | 413 | add.assoc is_num_add_commute is_num_add_left_commute | 
| 47108 | 414 | is_num.intros is_num_numeral | 
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
53064diff
changeset | 415 | minus_add | 
| 47108 | 416 | |
| 63654 | 417 | definition dbl :: "'a \<Rightarrow> 'a" | 
| 418 | where "dbl x = x + x" | |
| 419 | ||
| 420 | definition dbl_inc :: "'a \<Rightarrow> 'a" | |
| 421 | where "dbl_inc x = x + x + 1" | |
| 47108 | 422 | |
| 63654 | 423 | definition dbl_dec :: "'a \<Rightarrow> 'a" | 
| 424 | where "dbl_dec x = x + x - 1" | |
| 425 | ||
| 426 | definition sub :: "num \<Rightarrow> num \<Rightarrow> 'a" | |
| 427 | where "sub k l = numeral k - numeral l" | |
| 47108 | 428 | |
| 429 | lemma numeral_BitM: "numeral (BitM n) = numeral (Bit0 n) - 1" | |
| 430 | by (simp only: BitM_plus_one [symmetric] numeral_add numeral_One eq_diff_eq) | |
| 431 | ||
| 71991 | 432 | lemma sub_inc_One_eq: | 
| 433 | \<open>Num.sub (Num.inc n) num.One = numeral n\<close> | |
| 434 | by (simp_all add: sub_def diff_eq_eq numeral_inc numeral.numeral_One) | |
| 435 | ||
| 47108 | 436 | lemma dbl_simps [simp]: | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 437 | "dbl (- numeral k) = - dbl (numeral k)" | 
| 47108 | 438 | "dbl 0 = 0" | 
| 439 | "dbl 1 = 2" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 440 | "dbl (- 1) = - 2" | 
| 47108 | 441 | "dbl (numeral k) = numeral (Bit0 k)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 442 | by (simp_all add: dbl_def numeral.simps minus_add) | 
| 47108 | 443 | |
| 444 | lemma dbl_inc_simps [simp]: | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 445 | "dbl_inc (- numeral k) = - dbl_dec (numeral k)" | 
| 47108 | 446 | "dbl_inc 0 = 1" | 
| 447 | "dbl_inc 1 = 3" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 448 | "dbl_inc (- 1) = - 1" | 
| 47108 | 449 | "dbl_inc (numeral k) = numeral (Bit1 k)" | 
| 63654 | 450 | by (simp_all add: dbl_inc_def dbl_dec_def numeral.simps numeral_BitM is_num_normalize algebra_simps | 
| 451 | del: add_uminus_conv_diff) | |
| 47108 | 452 | |
| 453 | lemma dbl_dec_simps [simp]: | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 454 | "dbl_dec (- numeral k) = - dbl_inc (numeral k)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 455 | "dbl_dec 0 = - 1" | 
| 47108 | 456 | "dbl_dec 1 = 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 457 | "dbl_dec (- 1) = - 3" | 
| 47108 | 458 | "dbl_dec (numeral k) = numeral (BitM k)" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 459 | by (simp_all add: dbl_dec_def dbl_inc_def numeral.simps numeral_BitM is_num_normalize) | 
| 47108 | 460 | |
| 461 | lemma sub_num_simps [simp]: | |
| 462 | "sub One One = 0" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 463 | "sub One (Bit0 l) = - numeral (BitM l)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 464 | "sub One (Bit1 l) = - numeral (Bit0 l)" | 
| 47108 | 465 | "sub (Bit0 k) One = numeral (BitM k)" | 
| 466 | "sub (Bit1 k) One = numeral (Bit0 k)" | |
| 467 | "sub (Bit0 k) (Bit0 l) = dbl (sub k l)" | |
| 468 | "sub (Bit0 k) (Bit1 l) = dbl_dec (sub k l)" | |
| 469 | "sub (Bit1 k) (Bit0 l) = dbl_inc (sub k l)" | |
| 470 | "sub (Bit1 k) (Bit1 l) = dbl (sub k l)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 471 | by (simp_all add: dbl_def dbl_dec_def dbl_inc_def sub_def numeral.simps | 
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
53064diff
changeset | 472 | numeral_BitM is_num_normalize del: add_uminus_conv_diff add: diff_conv_add_uminus) | 
| 47108 | 473 | |
| 474 | lemma add_neg_numeral_simps: | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 475 | "numeral m + - numeral n = sub m n" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 476 | "- numeral m + numeral n = sub n m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 477 | "- numeral m + - numeral n = - (numeral m + numeral n)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 478 | by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize | 
| 63654 | 479 | del: add_uminus_conv_diff add: diff_conv_add_uminus) | 
| 47108 | 480 | |
| 481 | lemma add_neg_numeral_special: | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 482 | "1 + - numeral m = sub One m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 483 | "- numeral m + 1 = sub One m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 484 | "numeral m + - 1 = sub m One" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 485 | "- 1 + numeral n = sub n One" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 486 | "- 1 + - numeral n = - numeral (inc n)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 487 | "- numeral m + - 1 = - numeral (inc m)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 488 | "1 + - 1 = 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 489 | "- 1 + 1 = 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 490 | "- 1 + - 1 = - 2" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 491 | by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize right_minus numeral_inc | 
| 63654 | 492 | del: add_uminus_conv_diff add: diff_conv_add_uminus) | 
| 47108 | 493 | |
| 494 | lemma diff_numeral_simps: | |
| 495 | "numeral m - numeral n = sub m n" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 496 | "numeral m - - numeral n = numeral (m + n)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 497 | "- numeral m - numeral n = - numeral (m + n)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 498 | "- numeral m - - numeral n = sub n m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 499 | by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize | 
| 63654 | 500 | del: add_uminus_conv_diff add: diff_conv_add_uminus) | 
| 47108 | 501 | |
| 502 | lemma diff_numeral_special: | |
| 503 | "1 - numeral n = sub One n" | |
| 504 | "numeral m - 1 = sub m One" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 505 | "1 - - numeral n = numeral (One + n)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 506 | "- numeral m - 1 = - numeral (m + One)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 507 | "- 1 - numeral n = - numeral (inc n)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 508 | "numeral m - - 1 = numeral (inc m)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 509 | "- 1 - - numeral n = sub n One" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 510 | "- numeral m - - 1 = sub One m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 511 | "1 - 1 = 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 512 | "- 1 - 1 = - 2" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 513 | "1 - - 1 = 2" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 514 | "- 1 - - 1 = 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 515 | by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize numeral_inc | 
| 63654 | 516 | del: add_uminus_conv_diff add: diff_conv_add_uminus) | 
| 47108 | 517 | |
| 518 | end | |
| 519 | ||
| 63654 | 520 | |
| 521 | subsubsection \<open>Structures with multiplication: class \<open>semiring_numeral\<close>\<close> | |
| 47108 | 522 | |
| 523 | class semiring_numeral = semiring + monoid_mult | |
| 524 | begin | |
| 525 | ||
| 526 | subclass numeral .. | |
| 527 | ||
| 528 | lemma numeral_mult: "numeral (m * n) = numeral m * numeral n" | |
| 63654 | 529 | by (induct n rule: num_induct) | 
| 530 | (simp_all add: numeral_One mult_inc numeral_inc numeral_add distrib_left) | |
| 47108 | 531 | |
| 532 | lemma numeral_times_numeral: "numeral m * numeral n = numeral (m * n)" | |
| 533 | by (rule numeral_mult [symmetric]) | |
| 534 | ||
| 53064 | 535 | lemma mult_2: "2 * z = z + z" | 
| 63654 | 536 | by (simp add: one_add_one [symmetric] distrib_right) | 
| 53064 | 537 | |
| 538 | lemma mult_2_right: "z * 2 = z + z" | |
| 63654 | 539 | by (simp add: one_add_one [symmetric] distrib_left) | 
| 53064 | 540 | |
| 66936 | 541 | lemma left_add_twice: | 
| 542 | "a + (a + b) = 2 * a + b" | |
| 543 | by (simp add: mult_2 ac_simps) | |
| 544 | ||
| 47108 | 545 | end | 
| 546 | ||
| 63654 | 547 | |
| 548 | subsubsection \<open>Structures with a zero: class \<open>semiring_1\<close>\<close> | |
| 47108 | 549 | |
| 550 | context semiring_1 | |
| 551 | begin | |
| 552 | ||
| 553 | subclass semiring_numeral .. | |
| 554 | ||
| 555 | lemma of_nat_numeral [simp]: "of_nat (numeral n) = numeral n" | |
| 63654 | 556 | by (induct n) (simp_all only: numeral.simps numeral_class.numeral.simps of_nat_add of_nat_1) | 
| 47108 | 557 | |
| 70927 | 558 | end | 
| 64178 | 559 | |
| 63654 | 560 | lemma nat_of_num_numeral [code_abbrev]: "nat_of_num = numeral" | 
| 47108 | 561 | proof | 
| 562 | fix n | |
| 563 | have "numeral n = nat_of_num n" | |
| 564 | by (induct n) (simp_all add: numeral.simps) | |
| 63654 | 565 | then show "nat_of_num n = numeral n" | 
| 566 | by simp | |
| 47108 | 567 | qed | 
| 568 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50817diff
changeset | 569 | lemma nat_of_num_code [code]: | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50817diff
changeset | 570 | "nat_of_num One = 1" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50817diff
changeset | 571 | "nat_of_num (Bit0 n) = (let m = nat_of_num n in m + m)" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50817diff
changeset | 572 | "nat_of_num (Bit1 n) = (let m = nat_of_num n in Suc (m + m))" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50817diff
changeset | 573 | by (simp_all add: Let_def) | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50817diff
changeset | 574 | |
| 63654 | 575 | |
| 576 | subsubsection \<open>Equality: class \<open>semiring_char_0\<close>\<close> | |
| 47108 | 577 | |
| 578 | context semiring_char_0 | |
| 579 | begin | |
| 580 | ||
| 581 | lemma numeral_eq_iff: "numeral m = numeral n \<longleftrightarrow> m = n" | |
| 63654 | 582 | by (simp only: of_nat_numeral [symmetric] nat_of_num_numeral [symmetric] | 
| 583 | of_nat_eq_iff num_eq_iff) | |
| 47108 | 584 | |
| 585 | lemma numeral_eq_one_iff: "numeral n = 1 \<longleftrightarrow> n = One" | |
| 586 | by (rule numeral_eq_iff [of n One, unfolded numeral_One]) | |
| 587 | ||
| 588 | lemma one_eq_numeral_iff: "1 = numeral n \<longleftrightarrow> One = n" | |
| 589 | by (rule numeral_eq_iff [of One n, unfolded numeral_One]) | |
| 590 | ||
| 591 | lemma numeral_neq_zero: "numeral n \<noteq> 0" | |
| 63654 | 592 | by (simp add: of_nat_numeral [symmetric] nat_of_num_numeral [symmetric] nat_of_num_pos) | 
| 47108 | 593 | |
| 594 | lemma zero_neq_numeral: "0 \<noteq> numeral n" | |
| 595 | unfolding eq_commute [of 0] by (rule numeral_neq_zero) | |
| 596 | ||
| 597 | lemmas eq_numeral_simps [simp] = | |
| 598 | numeral_eq_iff | |
| 599 | numeral_eq_one_iff | |
| 600 | one_eq_numeral_iff | |
| 601 | numeral_neq_zero | |
| 602 | zero_neq_numeral | |
| 603 | ||
| 604 | end | |
| 605 | ||
| 63654 | 606 | |
| 70270 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 607 | subsubsection \<open>Comparisons: class \<open>linordered_nonzero_semiring\<close>\<close> | 
| 47108 | 608 | |
| 70270 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 609 | context linordered_nonzero_semiring | 
| 47108 | 610 | begin | 
| 611 | ||
| 612 | lemma numeral_le_iff: "numeral m \<le> numeral n \<longleftrightarrow> m \<le> n" | |
| 613 | proof - | |
| 614 | have "of_nat (numeral m) \<le> of_nat (numeral n) \<longleftrightarrow> m \<le> n" | |
| 63654 | 615 | by (simp only: less_eq_num_def nat_of_num_numeral of_nat_le_iff) | 
| 47108 | 616 | then show ?thesis by simp | 
| 617 | qed | |
| 618 | ||
| 619 | lemma one_le_numeral: "1 \<le> numeral n" | |
| 70270 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 620 | using numeral_le_iff [of num.One n] by (simp add: numeral_One) | 
| 47108 | 621 | |
| 70270 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 622 | lemma numeral_le_one_iff: "numeral n \<le> 1 \<longleftrightarrow> n \<le> num.One" | 
| 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 623 | using numeral_le_iff [of n num.One] by (simp add: numeral_One) | 
| 47108 | 624 | |
| 625 | lemma numeral_less_iff: "numeral m < numeral n \<longleftrightarrow> m < n" | |
| 626 | proof - | |
| 627 | have "of_nat (numeral m) < of_nat (numeral n) \<longleftrightarrow> m < n" | |
| 628 | unfolding less_num_def nat_of_num_numeral of_nat_less_iff .. | |
| 629 | then show ?thesis by simp | |
| 630 | qed | |
| 631 | ||
| 632 | lemma not_numeral_less_one: "\<not> numeral n < 1" | |
| 70270 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 633 | using numeral_less_iff [of n num.One] by (simp add: numeral_One) | 
| 47108 | 634 | |
| 70270 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 635 | lemma one_less_numeral_iff: "1 < numeral n \<longleftrightarrow> num.One < n" | 
| 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 636 | using numeral_less_iff [of num.One n] by (simp add: numeral_One) | 
| 47108 | 637 | |
| 638 | lemma zero_le_numeral: "0 \<le> numeral n" | |
| 70270 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 639 | using dual_order.trans one_le_numeral zero_le_one by blast | 
| 47108 | 640 | |
| 641 | lemma zero_less_numeral: "0 < numeral n" | |
| 70270 
4065e3b0e5bf
Generalisations involving numerals; comparisons should now work for ennreal
 paulson <lp15@cam.ac.uk> parents: 
70226diff
changeset | 642 | using less_linear not_numeral_less_one order.strict_trans zero_less_one by blast | 
| 47108 | 643 | |
| 644 | lemma not_numeral_le_zero: "\<not> numeral n \<le> 0" | |
| 645 | by (simp add: not_le zero_less_numeral) | |
| 646 | ||
| 647 | lemma not_numeral_less_zero: "\<not> numeral n < 0" | |
| 648 | by (simp add: not_less zero_le_numeral) | |
| 649 | ||
| 650 | lemmas le_numeral_extra = | |
| 651 | zero_le_one not_one_le_zero | |
| 652 | order_refl [of 0] order_refl [of 1] | |
| 653 | ||
| 654 | lemmas less_numeral_extra = | |
| 655 | zero_less_one not_one_less_zero | |
| 656 | less_irrefl [of 0] less_irrefl [of 1] | |
| 657 | ||
| 658 | lemmas le_numeral_simps [simp] = | |
| 659 | numeral_le_iff | |
| 660 | one_le_numeral | |
| 661 | numeral_le_one_iff | |
| 662 | zero_le_numeral | |
| 663 | not_numeral_le_zero | |
| 664 | ||
| 665 | lemmas less_numeral_simps [simp] = | |
| 666 | numeral_less_iff | |
| 667 | one_less_numeral_iff | |
| 668 | not_numeral_less_one | |
| 669 | zero_less_numeral | |
| 670 | not_numeral_less_zero | |
| 671 | ||
| 61630 | 672 | lemma min_0_1 [simp]: | 
| 63654 | 673 | fixes min' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" | 
| 674 | defines "min' \<equiv> min" | |
| 675 | shows | |
| 676 | "min' 0 1 = 0" | |
| 677 | "min' 1 0 = 0" | |
| 678 | "min' 0 (numeral x) = 0" | |
| 679 | "min' (numeral x) 0 = 0" | |
| 680 | "min' 1 (numeral x) = 1" | |
| 681 | "min' (numeral x) 1 = 1" | |
| 682 | by (simp_all add: min'_def min_def le_num_One_iff) | |
| 61630 | 683 | |
| 63654 | 684 | lemma max_0_1 [simp]: | 
| 685 | fixes max' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" | |
| 686 | defines "max' \<equiv> max" | |
| 687 | shows | |
| 688 | "max' 0 1 = 1" | |
| 689 | "max' 1 0 = 1" | |
| 690 | "max' 0 (numeral x) = numeral x" | |
| 691 | "max' (numeral x) 0 = numeral x" | |
| 692 | "max' 1 (numeral x) = numeral x" | |
| 693 | "max' (numeral x) 1 = numeral x" | |
| 694 | by (simp_all add: max'_def max_def le_num_One_iff) | |
| 61630 | 695 | |
| 47108 | 696 | end | 
| 697 | ||
| 67116 | 698 | text \<open>Unfold \<open>min\<close> and \<open>max\<close> on numerals.\<close> | 
| 699 | ||
| 700 | lemmas max_number_of [simp] = | |
| 701 | max_def [of "numeral u" "numeral v"] | |
| 702 | max_def [of "numeral u" "- numeral v"] | |
| 703 | max_def [of "- numeral u" "numeral v"] | |
| 704 | max_def [of "- numeral u" "- numeral v"] for u v | |
| 705 | ||
| 706 | lemmas min_number_of [simp] = | |
| 707 | min_def [of "numeral u" "numeral v"] | |
| 708 | min_def [of "numeral u" "- numeral v"] | |
| 709 | min_def [of "- numeral u" "numeral v"] | |
| 710 | min_def [of "- numeral u" "- numeral v"] for u v | |
| 711 | ||
| 63654 | 712 | |
| 713 | subsubsection \<open>Multiplication and negation: class \<open>ring_1\<close>\<close> | |
| 47108 | 714 | |
| 715 | context ring_1 | |
| 716 | begin | |
| 717 | ||
| 718 | subclass neg_numeral .. | |
| 719 | ||
| 720 | lemma mult_neg_numeral_simps: | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 721 | "- numeral m * - numeral n = numeral (m * n)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 722 | "- numeral m * numeral n = - numeral (m * n)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 723 | "numeral m * - numeral n = - numeral (m * n)" | 
| 63654 | 724 | by (simp_all only: mult_minus_left mult_minus_right minus_minus numeral_mult) | 
| 47108 | 725 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 726 | lemma mult_minus1 [simp]: "- 1 * z = - z" | 
| 63654 | 727 | by (simp add: numeral.simps) | 
| 47108 | 728 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 729 | lemma mult_minus1_right [simp]: "z * - 1 = - z" | 
| 63654 | 730 | by (simp add: numeral.simps) | 
| 47108 | 731 | |
| 71758 | 732 | lemma minus_sub_one_diff_one [simp]: | 
| 733 | \<open>- sub m One - 1 = - numeral m\<close> | |
| 734 | proof - | |
| 735 | have \<open>sub m One + 1 = numeral m\<close> | |
| 736 | by (simp flip: eq_diff_eq add: diff_numeral_special) | |
| 737 | then have \<open>- (sub m One + 1) = - numeral m\<close> | |
| 738 | by simp | |
| 739 | then show ?thesis | |
| 740 | by simp | |
| 741 | qed | |
| 742 | ||
| 47108 | 743 | end | 
| 744 | ||
| 63654 | 745 | |
| 746 | subsubsection \<open>Equality using \<open>iszero\<close> for rings with non-zero characteristic\<close> | |
| 47108 | 747 | |
| 748 | context ring_1 | |
| 749 | begin | |
| 750 | ||
| 751 | definition iszero :: "'a \<Rightarrow> bool" | |
| 752 | where "iszero z \<longleftrightarrow> z = 0" | |
| 753 | ||
| 754 | lemma iszero_0 [simp]: "iszero 0" | |
| 755 | by (simp add: iszero_def) | |
| 756 | ||
| 757 | lemma not_iszero_1 [simp]: "\<not> iszero 1" | |
| 758 | by (simp add: iszero_def) | |
| 759 | ||
| 760 | lemma not_iszero_Numeral1: "\<not> iszero Numeral1" | |
| 761 | by (simp add: numeral_One) | |
| 762 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 763 | lemma not_iszero_neg_1 [simp]: "\<not> iszero (- 1)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 764 | by (simp add: iszero_def) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 765 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 766 | lemma not_iszero_neg_Numeral1: "\<not> iszero (- Numeral1)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 767 | by (simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 768 | |
| 63654 | 769 | lemma iszero_neg_numeral [simp]: "iszero (- numeral w) \<longleftrightarrow> iszero (numeral w)" | 
| 770 | unfolding iszero_def by (rule neg_equal_0_iff_equal) | |
| 47108 | 771 | |
| 772 | lemma eq_iff_iszero_diff: "x = y \<longleftrightarrow> iszero (x - y)" | |
| 773 | unfolding iszero_def by (rule eq_iff_diff_eq_0) | |
| 774 | ||
| 63654 | 775 | text \<open> | 
| 776 | The \<open>eq_numeral_iff_iszero\<close> lemmas are not declared \<open>[simp]\<close> by default, | |
| 777 | because for rings of characteristic zero, better simp rules are possible. | |
| 778 | For a type like integers mod \<open>n\<close>, type-instantiated versions of these rules | |
| 779 | should be added to the simplifier, along with a type-specific rule for | |
| 780 | deciding propositions of the form \<open>iszero (numeral w)\<close>. | |
| 47108 | 781 | |
| 63654 | 782 | bh: Maybe it would not be so bad to just declare these as simp rules anyway? | 
| 783 | I should test whether these rules take precedence over the \<open>ring_char_0\<close> | |
| 784 | rules in the simplifier. | |
| 60758 | 785 | \<close> | 
| 47108 | 786 | |
| 787 | lemma eq_numeral_iff_iszero: | |
| 788 | "numeral x = numeral y \<longleftrightarrow> iszero (sub x y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 789 | "numeral x = - numeral y \<longleftrightarrow> iszero (numeral (x + y))" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 790 | "- numeral x = numeral y \<longleftrightarrow> iszero (numeral (x + y))" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 791 | "- numeral x = - numeral y \<longleftrightarrow> iszero (sub y x)" | 
| 47108 | 792 | "numeral x = 1 \<longleftrightarrow> iszero (sub x One)" | 
| 793 | "1 = numeral y \<longleftrightarrow> iszero (sub One y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 794 | "- numeral x = 1 \<longleftrightarrow> iszero (numeral (x + One))" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 795 | "1 = - numeral y \<longleftrightarrow> iszero (numeral (One + y))" | 
| 47108 | 796 | "numeral x = 0 \<longleftrightarrow> iszero (numeral x)" | 
| 797 | "0 = numeral y \<longleftrightarrow> iszero (numeral y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 798 | "- numeral x = 0 \<longleftrightarrow> iszero (numeral x)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 799 | "0 = - numeral y \<longleftrightarrow> iszero (numeral y)" | 
| 47108 | 800 | unfolding eq_iff_iszero_diff diff_numeral_simps diff_numeral_special | 
| 801 | by simp_all | |
| 802 | ||
| 803 | end | |
| 804 | ||
| 63654 | 805 | |
| 806 | subsubsection \<open>Equality and negation: class \<open>ring_char_0\<close>\<close> | |
| 47108 | 807 | |
| 62481 
b5d8e57826df
tuned bootstrap order to provide type classes in a more sensible order
 haftmann parents: 
62348diff
changeset | 808 | context ring_char_0 | 
| 47108 | 809 | begin | 
| 810 | ||
| 811 | lemma not_iszero_numeral [simp]: "\<not> iszero (numeral w)" | |
| 812 | by (simp add: iszero_def) | |
| 813 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 814 | lemma neg_numeral_eq_iff: "- numeral m = - numeral n \<longleftrightarrow> m = n" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 815 | by simp | 
| 47108 | 816 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 817 | lemma numeral_neq_neg_numeral: "numeral m \<noteq> - numeral n" | 
| 63654 | 818 | by (simp add: eq_neg_iff_add_eq_0 numeral_plus_numeral) | 
| 47108 | 819 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 820 | lemma neg_numeral_neq_numeral: "- numeral m \<noteq> numeral n" | 
| 47108 | 821 | by (rule numeral_neq_neg_numeral [symmetric]) | 
| 822 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 823 | lemma zero_neq_neg_numeral: "0 \<noteq> - numeral n" | 
| 63654 | 824 | by simp | 
| 47108 | 825 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 826 | lemma neg_numeral_neq_zero: "- numeral n \<noteq> 0" | 
| 63654 | 827 | by simp | 
| 47108 | 828 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 829 | lemma one_neq_neg_numeral: "1 \<noteq> - numeral n" | 
| 47108 | 830 | using numeral_neq_neg_numeral [of One n] by (simp add: numeral_One) | 
| 831 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 832 | lemma neg_numeral_neq_one: "- numeral n \<noteq> 1" | 
| 47108 | 833 | using neg_numeral_neq_numeral [of n One] by (simp add: numeral_One) | 
| 834 | ||
| 63654 | 835 | lemma neg_one_neq_numeral: "- 1 \<noteq> numeral n" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 836 | using neg_numeral_neq_numeral [of One n] by (simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 837 | |
| 63654 | 838 | lemma numeral_neq_neg_one: "numeral n \<noteq> - 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 839 | using numeral_neq_neg_numeral [of n One] by (simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 840 | |
| 63654 | 841 | lemma neg_one_eq_numeral_iff: "- 1 = - numeral n \<longleftrightarrow> n = One" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 842 | using neg_numeral_eq_iff [of One n] by (auto simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 843 | |
| 63654 | 844 | lemma numeral_eq_neg_one_iff: "- numeral n = - 1 \<longleftrightarrow> n = One" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 845 | using neg_numeral_eq_iff [of n One] by (auto simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 846 | |
| 63654 | 847 | lemma neg_one_neq_zero: "- 1 \<noteq> 0" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 848 | by simp | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 849 | |
| 63654 | 850 | lemma zero_neq_neg_one: "0 \<noteq> - 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 851 | by simp | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 852 | |
| 63654 | 853 | lemma neg_one_neq_one: "- 1 \<noteq> 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 854 | using neg_numeral_neq_numeral [of One One] by (simp only: numeral_One not_False_eq_True) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 855 | |
| 63654 | 856 | lemma one_neq_neg_one: "1 \<noteq> - 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 857 | using numeral_neq_neg_numeral [of One One] by (simp only: numeral_One not_False_eq_True) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 858 | |
| 47108 | 859 | lemmas eq_neg_numeral_simps [simp] = | 
| 860 | neg_numeral_eq_iff | |
| 861 | numeral_neq_neg_numeral neg_numeral_neq_numeral | |
| 862 | one_neq_neg_numeral neg_numeral_neq_one | |
| 863 | zero_neq_neg_numeral neg_numeral_neq_zero | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 864 | neg_one_neq_numeral numeral_neq_neg_one | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 865 | neg_one_eq_numeral_iff numeral_eq_neg_one_iff | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 866 | neg_one_neq_zero zero_neq_neg_one | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 867 | neg_one_neq_one one_neq_neg_one | 
| 47108 | 868 | |
| 869 | end | |
| 870 | ||
| 62348 | 871 | |
| 63654 | 872 | subsubsection \<open>Structures with negation and order: class \<open>linordered_idom\<close>\<close> | 
| 47108 | 873 | |
| 874 | context linordered_idom | |
| 875 | begin | |
| 876 | ||
| 877 | subclass ring_char_0 .. | |
| 878 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 879 | lemma neg_numeral_le_iff: "- numeral m \<le> - numeral n \<longleftrightarrow> n \<le> m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 880 | by (simp only: neg_le_iff_le numeral_le_iff) | 
| 47108 | 881 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 882 | lemma neg_numeral_less_iff: "- numeral m < - numeral n \<longleftrightarrow> n < m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 883 | by (simp only: neg_less_iff_less numeral_less_iff) | 
| 47108 | 884 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 885 | lemma neg_numeral_less_zero: "- numeral n < 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 886 | by (simp only: neg_less_0_iff_less zero_less_numeral) | 
| 47108 | 887 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 888 | lemma neg_numeral_le_zero: "- numeral n \<le> 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 889 | by (simp only: neg_le_0_iff_le zero_le_numeral) | 
| 47108 | 890 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 891 | lemma not_zero_less_neg_numeral: "\<not> 0 < - numeral n" | 
| 47108 | 892 | by (simp only: not_less neg_numeral_le_zero) | 
| 893 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 894 | lemma not_zero_le_neg_numeral: "\<not> 0 \<le> - numeral n" | 
| 47108 | 895 | by (simp only: not_le neg_numeral_less_zero) | 
| 896 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 897 | lemma neg_numeral_less_numeral: "- numeral m < numeral n" | 
| 47108 | 898 | using neg_numeral_less_zero zero_less_numeral by (rule less_trans) | 
| 899 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 900 | lemma neg_numeral_le_numeral: "- numeral m \<le> numeral n" | 
| 47108 | 901 | by (simp only: less_imp_le neg_numeral_less_numeral) | 
| 902 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 903 | lemma not_numeral_less_neg_numeral: "\<not> numeral m < - numeral n" | 
| 47108 | 904 | by (simp only: not_less neg_numeral_le_numeral) | 
| 905 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 906 | lemma not_numeral_le_neg_numeral: "\<not> numeral m \<le> - numeral n" | 
| 47108 | 907 | by (simp only: not_le neg_numeral_less_numeral) | 
| 63654 | 908 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 909 | lemma neg_numeral_less_one: "- numeral m < 1" | 
| 47108 | 910 | by (rule neg_numeral_less_numeral [of m One, unfolded numeral_One]) | 
| 911 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 912 | lemma neg_numeral_le_one: "- numeral m \<le> 1" | 
| 47108 | 913 | by (rule neg_numeral_le_numeral [of m One, unfolded numeral_One]) | 
| 914 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 915 | lemma not_one_less_neg_numeral: "\<not> 1 < - numeral m" | 
| 47108 | 916 | by (simp only: not_less neg_numeral_le_one) | 
| 917 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 918 | lemma not_one_le_neg_numeral: "\<not> 1 \<le> - numeral m" | 
| 47108 | 919 | by (simp only: not_le neg_numeral_less_one) | 
| 920 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 921 | lemma not_numeral_less_neg_one: "\<not> numeral m < - 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 922 | using not_numeral_less_neg_numeral [of m One] by (simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 923 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 924 | lemma not_numeral_le_neg_one: "\<not> numeral m \<le> - 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 925 | using not_numeral_le_neg_numeral [of m One] by (simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 926 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 927 | lemma neg_one_less_numeral: "- 1 < numeral m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 928 | using neg_numeral_less_numeral [of One m] by (simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 929 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 930 | lemma neg_one_le_numeral: "- 1 \<le> numeral m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 931 | using neg_numeral_le_numeral [of One m] by (simp add: numeral_One) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 932 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 933 | lemma neg_numeral_less_neg_one_iff: "- numeral m < - 1 \<longleftrightarrow> m \<noteq> One" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 934 | by (cases m) simp_all | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 935 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 936 | lemma neg_numeral_le_neg_one: "- numeral m \<le> - 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 937 | by simp | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 938 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 939 | lemma not_neg_one_less_neg_numeral: "\<not> - 1 < - numeral m" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 940 | by simp | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 941 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 942 | lemma not_neg_one_le_neg_numeral_iff: "\<not> - 1 \<le> - numeral m \<longleftrightarrow> m \<noteq> One" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 943 | by (cases m) simp_all | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 944 | |
| 63654 | 945 | lemma sub_non_negative: "sub n m \<ge> 0 \<longleftrightarrow> n \<ge> m" | 
| 47108 | 946 | by (simp only: sub_def le_diff_eq) simp | 
| 947 | ||
| 63654 | 948 | lemma sub_positive: "sub n m > 0 \<longleftrightarrow> n > m" | 
| 47108 | 949 | by (simp only: sub_def less_diff_eq) simp | 
| 950 | ||
| 63654 | 951 | lemma sub_non_positive: "sub n m \<le> 0 \<longleftrightarrow> n \<le> m" | 
| 47108 | 952 | by (simp only: sub_def diff_le_eq) simp | 
| 953 | ||
| 63654 | 954 | lemma sub_negative: "sub n m < 0 \<longleftrightarrow> n < m" | 
| 47108 | 955 | by (simp only: sub_def diff_less_eq) simp | 
| 956 | ||
| 957 | lemmas le_neg_numeral_simps [simp] = | |
| 958 | neg_numeral_le_iff | |
| 959 | neg_numeral_le_numeral not_numeral_le_neg_numeral | |
| 960 | neg_numeral_le_zero not_zero_le_neg_numeral | |
| 961 | neg_numeral_le_one not_one_le_neg_numeral | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 962 | neg_one_le_numeral not_numeral_le_neg_one | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 963 | neg_numeral_le_neg_one not_neg_one_le_neg_numeral_iff | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 964 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 965 | lemma le_minus_one_simps [simp]: | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 966 | "- 1 \<le> 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 967 | "- 1 \<le> 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 968 | "\<not> 0 \<le> - 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 969 | "\<not> 1 \<le> - 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 970 | by simp_all | 
| 47108 | 971 | |
| 972 | lemmas less_neg_numeral_simps [simp] = | |
| 973 | neg_numeral_less_iff | |
| 974 | neg_numeral_less_numeral not_numeral_less_neg_numeral | |
| 975 | neg_numeral_less_zero not_zero_less_neg_numeral | |
| 976 | neg_numeral_less_one not_one_less_neg_numeral | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 977 | neg_one_less_numeral not_numeral_less_neg_one | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 978 | neg_numeral_less_neg_one_iff not_neg_one_less_neg_numeral | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 979 | |
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 980 | lemma less_minus_one_simps [simp]: | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 981 | "- 1 < 0" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 982 | "- 1 < 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 983 | "\<not> 0 < - 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 984 | "\<not> 1 < - 1" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 985 | by (simp_all add: less_le) | 
| 47108 | 986 | |
| 61944 | 987 | lemma abs_numeral [simp]: "\<bar>numeral n\<bar> = numeral n" | 
| 47108 | 988 | by simp | 
| 989 | ||
| 61944 | 990 | lemma abs_neg_numeral [simp]: "\<bar>- numeral n\<bar> = numeral n" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 991 | by (simp only: abs_minus_cancel abs_numeral) | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 992 | |
| 61944 | 993 | lemma abs_neg_one [simp]: "\<bar>- 1\<bar> = 1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 994 | by simp | 
| 47108 | 995 | |
| 996 | end | |
| 997 | ||
| 63654 | 998 | |
| 999 | subsubsection \<open>Natural numbers\<close> | |
| 47108 | 1000 | |
| 67959 | 1001 | lemma numeral_num_of_nat: | 
| 1002 | "numeral (num_of_nat n) = n" if "n > 0" | |
| 1003 | using that nat_of_num_numeral num_of_nat_inverse by simp | |
| 1004 | ||
| 47299 | 1005 | lemma Suc_1 [simp]: "Suc 1 = 2" | 
| 1006 | unfolding Suc_eq_plus1 by (rule one_add_one) | |
| 1007 | ||
| 47108 | 1008 | lemma Suc_numeral [simp]: "Suc (numeral n) = numeral (n + One)" | 
| 47299 | 1009 | unfolding Suc_eq_plus1 by (rule numeral_plus_one) | 
| 47108 | 1010 | |
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1011 | definition pred_numeral :: "num \<Rightarrow> nat" | 
| 67959 | 1012 | where "pred_numeral k = numeral k - 1" | 
| 1013 | ||
| 1014 | declare [[code drop: pred_numeral]] | |
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1015 | |
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1016 | lemma numeral_eq_Suc: "numeral k = Suc (pred_numeral k)" | 
| 63654 | 1017 | by (simp add: pred_numeral_def) | 
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1018 | |
| 47220 
52426c62b5d0
replace lemmas eval_nat_numeral with a simpler reformulation
 huffman parents: 
47218diff
changeset | 1019 | lemma eval_nat_numeral: | 
| 47108 | 1020 | "numeral One = Suc 0" | 
| 1021 | "numeral (Bit0 n) = Suc (numeral (BitM n))" | |
| 1022 | "numeral (Bit1 n) = Suc (numeral (Bit0 n))" | |
| 1023 | by (simp_all add: numeral.simps BitM_plus_one) | |
| 1024 | ||
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1025 | lemma pred_numeral_simps [simp]: | 
| 47300 | 1026 | "pred_numeral One = 0" | 
| 1027 | "pred_numeral (Bit0 k) = numeral (BitM k)" | |
| 1028 | "pred_numeral (Bit1 k) = numeral (Bit0 k)" | |
| 63654 | 1029 | by (simp_all only: pred_numeral_def eval_nat_numeral diff_Suc_Suc diff_0) | 
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1030 | |
| 67959 | 1031 | lemma pred_numeral_inc [simp]: | 
| 1032 | "pred_numeral (Num.inc k) = numeral k" | |
| 1033 | by (simp only: pred_numeral_def numeral_inc diff_add_inverse2) | |
| 1034 | ||
| 47192 
0c0501cb6da6
move many lemmas from Nat_Numeral.thy to Power.thy or Num.thy
 huffman parents: 
47191diff
changeset | 1035 | lemma numeral_2_eq_2: "2 = Suc (Suc 0)" | 
| 47220 
52426c62b5d0
replace lemmas eval_nat_numeral with a simpler reformulation
 huffman parents: 
47218diff
changeset | 1036 | by (simp add: eval_nat_numeral) | 
| 47192 
0c0501cb6da6
move many lemmas from Nat_Numeral.thy to Power.thy or Num.thy
 huffman parents: 
47191diff
changeset | 1037 | |
| 
0c0501cb6da6
move many lemmas from Nat_Numeral.thy to Power.thy or Num.thy
 huffman parents: 
47191diff
changeset | 1038 | lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))" | 
| 47220 
52426c62b5d0
replace lemmas eval_nat_numeral with a simpler reformulation
 huffman parents: 
47218diff
changeset | 1039 | by (simp add: eval_nat_numeral) | 
| 47192 
0c0501cb6da6
move many lemmas from Nat_Numeral.thy to Power.thy or Num.thy
 huffman parents: 
47191diff
changeset | 1040 | |
| 47207 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 huffman parents: 
47192diff
changeset | 1041 | lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0" | 
| 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 huffman parents: 
47192diff
changeset | 1042 | by (simp only: numeral_One One_nat_def) | 
| 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 huffman parents: 
47192diff
changeset | 1043 | |
| 63654 | 1044 | lemma Suc_nat_number_of_add: "Suc (numeral v + n) = numeral (v + One) + n" | 
| 47207 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 huffman parents: 
47192diff
changeset | 1045 | by simp | 
| 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 huffman parents: 
47192diff
changeset | 1046 | |
| 63654 | 1047 | lemma numerals: "Numeral1 = (1::nat)" "2 = Suc (Suc 0)" | 
| 1048 | by (rule numeral_One) (rule numeral_2_eq_2) | |
| 47207 
9368aa814518
move lemmas from Nat_Numeral to Int.thy and Num.thy
 huffman parents: 
47192diff
changeset | 1049 | |
| 63913 | 1050 | lemmas numeral_nat = eval_nat_numeral BitM.simps One_nat_def | 
| 1051 | ||
| 69593 | 1052 | text \<open>Comparisons involving \<^term>\<open>Suc\<close>.\<close> | 
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1053 | |
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1054 | lemma eq_numeral_Suc [simp]: "numeral k = Suc n \<longleftrightarrow> pred_numeral k = n" | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1055 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1056 | |
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1057 | lemma Suc_eq_numeral [simp]: "Suc n = numeral k \<longleftrightarrow> n = pred_numeral k" | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1058 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1059 | |
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1060 | lemma less_numeral_Suc [simp]: "numeral k < Suc n \<longleftrightarrow> pred_numeral k < n" | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1061 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1062 | |
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1063 | lemma less_Suc_numeral [simp]: "Suc n < numeral k \<longleftrightarrow> n < pred_numeral k" | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1064 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1065 | |
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1066 | lemma le_numeral_Suc [simp]: "numeral k \<le> Suc n \<longleftrightarrow> pred_numeral k \<le> n" | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1067 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1068 | |
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1069 | lemma le_Suc_numeral [simp]: "Suc n \<le> numeral k \<longleftrightarrow> n \<le> pred_numeral k" | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1070 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1071 | |
| 47218 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 huffman parents: 
47216diff
changeset | 1072 | lemma diff_Suc_numeral [simp]: "Suc n - numeral k = n - pred_numeral k" | 
| 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 huffman parents: 
47216diff
changeset | 1073 | by (simp add: numeral_eq_Suc) | 
| 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 huffman parents: 
47216diff
changeset | 1074 | |
| 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 huffman parents: 
47216diff
changeset | 1075 | lemma diff_numeral_Suc [simp]: "numeral k - Suc n = pred_numeral k - n" | 
| 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 huffman parents: 
47216diff
changeset | 1076 | by (simp add: numeral_eq_Suc) | 
| 
2b652cbadde1
new lemmas for simplifying subtraction on nat numerals
 huffman parents: 
47216diff
changeset | 1077 | |
| 63654 | 1078 | lemma max_Suc_numeral [simp]: "max (Suc n) (numeral k) = Suc (max n (pred_numeral k))" | 
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1079 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1080 | |
| 63654 | 1081 | lemma max_numeral_Suc [simp]: "max (numeral k) (Suc n) = Suc (max (pred_numeral k) n)" | 
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1082 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1083 | |
| 63654 | 1084 | lemma min_Suc_numeral [simp]: "min (Suc n) (numeral k) = Suc (min n (pred_numeral k))" | 
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1085 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1086 | |
| 63654 | 1087 | lemma min_numeral_Suc [simp]: "min (numeral k) (Suc n) = Suc (min (pred_numeral k) n)" | 
| 47209 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1088 | by (simp add: numeral_eq_Suc) | 
| 
4893907fe872
add constant pred_numeral k = numeral k - (1::nat);
 huffman parents: 
47207diff
changeset | 1089 | |
| 69593 | 1090 | text \<open>For \<^term>\<open>case_nat\<close> and \<^term>\<open>rec_nat\<close>.\<close> | 
| 47216 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1091 | |
| 63654 | 1092 | lemma case_nat_numeral [simp]: "case_nat a f (numeral v) = (let pv = pred_numeral v in f pv)" | 
| 47216 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1093 | by (simp add: numeral_eq_Suc) | 
| 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1094 | |
| 55415 | 1095 | lemma case_nat_add_eq_if [simp]: | 
| 1096 | "case_nat a f ((numeral v) + n) = (let pv = pred_numeral v in f (pv + n))" | |
| 47216 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1097 | by (simp add: numeral_eq_Suc) | 
| 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1098 | |
| 55415 | 1099 | lemma rec_nat_numeral [simp]: | 
| 63654 | 1100 | "rec_nat a f (numeral v) = (let pv = pred_numeral v in f pv (rec_nat a f pv))" | 
| 47216 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1101 | by (simp add: numeral_eq_Suc Let_def) | 
| 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1102 | |
| 55415 | 1103 | lemma rec_nat_add_eq_if [simp]: | 
| 63654 | 1104 | "rec_nat a f (numeral v + n) = (let pv = pred_numeral v in f (pv + n) (rec_nat a f (pv + n)))" | 
| 47216 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1105 | by (simp add: numeral_eq_Suc Let_def) | 
| 
4d0878d54ca5
move more theorems from Nat_Numeral.thy to Num.thy
 huffman parents: 
47211diff
changeset | 1106 | |
| 69593 | 1107 | text \<open>Case analysis on \<^term>\<open>n < 2\<close>.\<close> | 
| 47255 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1108 | lemma less_2_cases: "n < 2 \<Longrightarrow> n = 0 \<or> n = Suc 0" | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1109 | by (auto simp add: numeral_2_eq_2) | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1110 | |
| 71452 | 1111 | lemma less_2_cases_iff: "n < 2 \<longleftrightarrow> n = 0 \<or> n = Suc 0" | 
| 1112 | by (auto simp add: numeral_2_eq_2) | |
| 1113 | ||
| 63654 | 1114 | text \<open>Removal of Small Numerals: 0, 1 and (in additive positions) 2.\<close> | 
| 71452 | 1115 | text \<open>bh: Are these rules really a good idea? LCP: well, it already happens for 0 and 1!\<close> | 
| 47255 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1116 | |
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1117 | lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)" | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1118 | by simp | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1119 | |
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1120 | lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)" | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1121 | by simp | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1122 | |
| 60758 | 1123 | text \<open>Can be used to eliminate long strings of Sucs, but not by default.\<close> | 
| 47255 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1124 | lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n" | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1125 | by simp | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1126 | |
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1127 | lemmas nat_1_add_1 = one_add_one [where 'a=nat] (* legacy *) | 
| 
30a1692557b0
removed Nat_Numeral.thy, moving all theorems elsewhere
 huffman parents: 
47228diff
changeset | 1128 | |
| 71760 | 1129 | context semiring_numeral | 
| 1130 | begin | |
| 1131 | ||
| 1132 | lemma numeral_add_unfold_funpow: | |
| 1133 | \<open>numeral k + a = ((+) 1 ^^ numeral k) a\<close> | |
| 1134 | proof (rule sym, induction k arbitrary: a) | |
| 1135 | case One | |
| 1136 | then show ?case | |
| 1137 | by (simp add: numeral_One Num.numeral_One) | |
| 1138 | next | |
| 1139 | case (Bit0 k) | |
| 1140 | then show ?case | |
| 1141 | by (simp add: numeral_Bit0 Num.numeral_Bit0 ac_simps funpow_add) | |
| 1142 | next | |
| 1143 | case (Bit1 k) | |
| 1144 | then show ?case | |
| 1145 | by (simp add: numeral_Bit1 Num.numeral_Bit1 ac_simps funpow_add) | |
| 1146 | qed | |
| 1147 | ||
| 1148 | end | |
| 1149 | ||
| 1150 | context semiring_1 | |
| 1151 | begin | |
| 1152 | ||
| 1153 | lemma numeral_unfold_funpow: | |
| 1154 | \<open>numeral k = ((+) 1 ^^ numeral k) 0\<close> | |
| 1155 | using numeral_add_unfold_funpow [of k 0] by simp | |
| 1156 | ||
| 1157 | end | |
| 1158 | ||
| 1159 | context | |
| 1160 | includes lifting_syntax | |
| 1161 | begin | |
| 1162 | ||
| 1163 | lemma transfer_rule_numeral: | |
| 1164 | \<open>((=) ===> R) numeral numeral\<close> | |
| 1165 | if [transfer_rule]: \<open>R 0 0\<close> \<open>R 1 1\<close> | |
| 1166 | \<open>(R ===> R ===> R) (+) (+)\<close> | |
| 1167 |     for R :: \<open>'a::{semiring_numeral,monoid_add} \<Rightarrow> 'b::{semiring_numeral,monoid_add} \<Rightarrow> bool\<close>
 | |
| 1168 | proof - | |
| 1169 | have "((=) ===> R) (\<lambda>k. ((+) 1 ^^ numeral k) 0) (\<lambda>k. ((+) 1 ^^ numeral k) 0)" | |
| 1170 | by transfer_prover | |
| 1171 | moreover have \<open>numeral = (\<lambda>k. ((+) (1::'a) ^^ numeral k) 0)\<close> | |
| 1172 | using numeral_add_unfold_funpow [where ?'a = 'a, of _ 0] | |
| 1173 | by (simp add: fun_eq_iff) | |
| 1174 | moreover have \<open>numeral = (\<lambda>k. ((+) (1::'b) ^^ numeral k) 0)\<close> | |
| 1175 | using numeral_add_unfold_funpow [where ?'a = 'b, of _ 0] | |
| 1176 | by (simp add: fun_eq_iff) | |
| 1177 | ultimately show ?thesis | |
| 1178 | by simp | |
| 1179 | qed | |
| 1180 | ||
| 1181 | end | |
| 1182 | ||
| 47108 | 1183 | |
| 69593 | 1184 | subsection \<open>Particular lemmas concerning \<^term>\<open>2\<close>\<close> | 
| 58512 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1185 | |
| 59867 
58043346ca64
given up separate type classes demanding `inverse 0 = 0`
 haftmann parents: 
59621diff
changeset | 1186 | context linordered_field | 
| 58512 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1187 | begin | 
| 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1188 | |
| 62348 | 1189 | subclass field_char_0 .. | 
| 1190 | ||
| 63654 | 1191 | lemma half_gt_zero_iff: "0 < a / 2 \<longleftrightarrow> 0 < a" | 
| 58512 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1192 | by (auto simp add: field_simps) | 
| 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1193 | |
| 63654 | 1194 | lemma half_gt_zero [simp]: "0 < a \<Longrightarrow> 0 < a / 2" | 
| 58512 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1195 | by (simp add: half_gt_zero_iff) | 
| 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1196 | |
| 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1197 | end | 
| 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1198 | |
| 
dc4d76dfa8f0
moved lemmas out of Int.thy which have nothing to do with int
 haftmann parents: 
58421diff
changeset | 1199 | |
| 60758 | 1200 | subsection \<open>Numeral equations as default simplification rules\<close> | 
| 47108 | 1201 | |
| 1202 | declare (in numeral) numeral_One [simp] | |
| 1203 | declare (in numeral) numeral_plus_numeral [simp] | |
| 1204 | declare (in numeral) add_numeral_special [simp] | |
| 1205 | declare (in neg_numeral) add_neg_numeral_simps [simp] | |
| 1206 | declare (in neg_numeral) add_neg_numeral_special [simp] | |
| 1207 | declare (in neg_numeral) diff_numeral_simps [simp] | |
| 1208 | declare (in neg_numeral) diff_numeral_special [simp] | |
| 1209 | declare (in semiring_numeral) numeral_times_numeral [simp] | |
| 1210 | declare (in ring_1) mult_neg_numeral_simps [simp] | |
| 1211 | ||
| 67116 | 1212 | |
| 1213 | subsubsection \<open>Special Simplification for Constants\<close> | |
| 1214 | ||
| 1215 | text \<open>These distributive laws move literals inside sums and differences.\<close> | |
| 1216 | ||
| 1217 | lemmas distrib_right_numeral [simp] = distrib_right [of _ _ "numeral v"] for v | |
| 1218 | lemmas distrib_left_numeral [simp] = distrib_left [of "numeral v"] for v | |
| 1219 | lemmas left_diff_distrib_numeral [simp] = left_diff_distrib [of _ _ "numeral v"] for v | |
| 1220 | lemmas right_diff_distrib_numeral [simp] = right_diff_distrib [of "numeral v"] for v | |
| 1221 | ||
| 1222 | text \<open>These are actually for fields, like real\<close> | |
| 1223 | ||
| 1224 | lemmas zero_less_divide_iff_numeral [simp, no_atp] = zero_less_divide_iff [of "numeral w"] for w | |
| 1225 | lemmas divide_less_0_iff_numeral [simp, no_atp] = divide_less_0_iff [of "numeral w"] for w | |
| 1226 | lemmas zero_le_divide_iff_numeral [simp, no_atp] = zero_le_divide_iff [of "numeral w"] for w | |
| 1227 | lemmas divide_le_0_iff_numeral [simp, no_atp] = divide_le_0_iff [of "numeral w"] for w | |
| 1228 | ||
| 1229 | text \<open>Replaces \<open>inverse #nn\<close> by \<open>1/#nn\<close>. It looks | |
| 1230 | strange, but then other simprocs simplify the quotient.\<close> | |
| 1231 | ||
| 1232 | lemmas inverse_eq_divide_numeral [simp] = | |
| 1233 | inverse_eq_divide [of "numeral w"] for w | |
| 1234 | ||
| 1235 | lemmas inverse_eq_divide_neg_numeral [simp] = | |
| 1236 | inverse_eq_divide [of "- numeral w"] for w | |
| 1237 | ||
| 1238 | text \<open>These laws simplify inequalities, moving unary minus from a term | |
| 1239 | into the literal.\<close> | |
| 1240 | ||
| 1241 | lemmas equation_minus_iff_numeral [no_atp] = | |
| 1242 | equation_minus_iff [of "numeral v"] for v | |
| 1243 | ||
| 1244 | lemmas minus_equation_iff_numeral [no_atp] = | |
| 1245 | minus_equation_iff [of _ "numeral v"] for v | |
| 1246 | ||
| 1247 | lemmas le_minus_iff_numeral [no_atp] = | |
| 1248 | le_minus_iff [of "numeral v"] for v | |
| 1249 | ||
| 1250 | lemmas minus_le_iff_numeral [no_atp] = | |
| 1251 | minus_le_iff [of _ "numeral v"] for v | |
| 1252 | ||
| 1253 | lemmas less_minus_iff_numeral [no_atp] = | |
| 1254 | less_minus_iff [of "numeral v"] for v | |
| 1255 | ||
| 1256 | lemmas minus_less_iff_numeral [no_atp] = | |
| 1257 | minus_less_iff [of _ "numeral v"] for v | |
| 1258 | ||
| 1259 | (* FIXME maybe simproc *) | |
| 1260 | ||
| 1261 | text \<open>Cancellation of constant factors in comparisons (\<open><\<close> and \<open>\<le>\<close>)\<close> | |
| 1262 | ||
| 1263 | lemmas mult_less_cancel_left_numeral [simp, no_atp] = mult_less_cancel_left [of "numeral v"] for v | |
| 1264 | lemmas mult_less_cancel_right_numeral [simp, no_atp] = mult_less_cancel_right [of _ "numeral v"] for v | |
| 1265 | lemmas mult_le_cancel_left_numeral [simp, no_atp] = mult_le_cancel_left [of "numeral v"] for v | |
| 1266 | lemmas mult_le_cancel_right_numeral [simp, no_atp] = mult_le_cancel_right [of _ "numeral v"] for v | |
| 1267 | ||
| 1268 | text \<open>Multiplying out constant divisors in comparisons (\<open><\<close>, \<open>\<le>\<close> and \<open>=\<close>)\<close> | |
| 1269 | ||
| 1270 | named_theorems divide_const_simps "simplification rules to simplify comparisons involving constant divisors" | |
| 1271 | ||
| 1272 | lemmas le_divide_eq_numeral1 [simp,divide_const_simps] = | |
| 1273 | pos_le_divide_eq [of "numeral w", OF zero_less_numeral] | |
| 1274 | neg_le_divide_eq [of "- numeral w", OF neg_numeral_less_zero] for w | |
| 1275 | ||
| 1276 | lemmas divide_le_eq_numeral1 [simp,divide_const_simps] = | |
| 1277 | pos_divide_le_eq [of "numeral w", OF zero_less_numeral] | |
| 1278 | neg_divide_le_eq [of "- numeral w", OF neg_numeral_less_zero] for w | |
| 1279 | ||
| 1280 | lemmas less_divide_eq_numeral1 [simp,divide_const_simps] = | |
| 1281 | pos_less_divide_eq [of "numeral w", OF zero_less_numeral] | |
| 1282 | neg_less_divide_eq [of "- numeral w", OF neg_numeral_less_zero] for w | |
| 1283 | ||
| 1284 | lemmas divide_less_eq_numeral1 [simp,divide_const_simps] = | |
| 1285 | pos_divide_less_eq [of "numeral w", OF zero_less_numeral] | |
| 1286 | neg_divide_less_eq [of "- numeral w", OF neg_numeral_less_zero] for w | |
| 1287 | ||
| 1288 | lemmas eq_divide_eq_numeral1 [simp,divide_const_simps] = | |
| 1289 | eq_divide_eq [of _ _ "numeral w"] | |
| 1290 | eq_divide_eq [of _ _ "- numeral w"] for w | |
| 1291 | ||
| 1292 | lemmas divide_eq_eq_numeral1 [simp,divide_const_simps] = | |
| 1293 | divide_eq_eq [of _ "numeral w"] | |
| 1294 | divide_eq_eq [of _ "- numeral w"] for w | |
| 1295 | ||
| 1296 | ||
| 1297 | subsubsection \<open>Optional Simplification Rules Involving Constants\<close> | |
| 1298 | ||
| 1299 | text \<open>Simplify quotients that are compared with a literal constant.\<close> | |
| 1300 | ||
| 1301 | lemmas le_divide_eq_numeral [divide_const_simps] = | |
| 1302 | le_divide_eq [of "numeral w"] | |
| 1303 | le_divide_eq [of "- numeral w"] for w | |
| 1304 | ||
| 1305 | lemmas divide_le_eq_numeral [divide_const_simps] = | |
| 1306 | divide_le_eq [of _ _ "numeral w"] | |
| 1307 | divide_le_eq [of _ _ "- numeral w"] for w | |
| 1308 | ||
| 1309 | lemmas less_divide_eq_numeral [divide_const_simps] = | |
| 1310 | less_divide_eq [of "numeral w"] | |
| 1311 | less_divide_eq [of "- numeral w"] for w | |
| 1312 | ||
| 1313 | lemmas divide_less_eq_numeral [divide_const_simps] = | |
| 1314 | divide_less_eq [of _ _ "numeral w"] | |
| 1315 | divide_less_eq [of _ _ "- numeral w"] for w | |
| 1316 | ||
| 1317 | lemmas eq_divide_eq_numeral [divide_const_simps] = | |
| 1318 | eq_divide_eq [of "numeral w"] | |
| 1319 | eq_divide_eq [of "- numeral w"] for w | |
| 1320 | ||
| 1321 | lemmas divide_eq_eq_numeral [divide_const_simps] = | |
| 1322 | divide_eq_eq [of _ _ "numeral w"] | |
| 1323 | divide_eq_eq [of _ _ "- numeral w"] for w | |
| 1324 | ||
| 1325 | text \<open>Not good as automatic simprules because they cause case splits.\<close> | |
| 1326 | ||
| 1327 | lemmas [divide_const_simps] = | |
| 1328 | le_divide_eq_1 divide_le_eq_1 less_divide_eq_1 divide_less_eq_1 | |
| 1329 | ||
| 1330 | ||
| 60758 | 1331 | subsection \<open>Setting up simprocs\<close> | 
| 47108 | 1332 | |
| 63654 | 1333 | lemma mult_numeral_1: "Numeral1 * a = a" | 
| 1334 | for a :: "'a::semiring_numeral" | |
| 47108 | 1335 | by simp | 
| 1336 | ||
| 63654 | 1337 | lemma mult_numeral_1_right: "a * Numeral1 = a" | 
| 1338 | for a :: "'a::semiring_numeral" | |
| 47108 | 1339 | by simp | 
| 1340 | ||
| 63654 | 1341 | lemma divide_numeral_1: "a / Numeral1 = a" | 
| 1342 | for a :: "'a::field" | |
| 47108 | 1343 | by simp | 
| 1344 | ||
| 63654 | 1345 | lemma inverse_numeral_1: "inverse Numeral1 = (Numeral1::'a::division_ring)" | 
| 47108 | 1346 | by simp | 
| 1347 | ||
| 63654 | 1348 | text \<open> | 
| 1349 | Theorem lists for the cancellation simprocs. The use of a binary | |
| 1350 | numeral for 1 reduces the number of special cases. | |
| 1351 | \<close> | |
| 47108 | 1352 | |
| 68536 | 1353 | lemma mult_1s_semiring_numeral: | 
| 63654 | 1354 | "Numeral1 * a = a" | 
| 1355 | "a * Numeral1 = a" | |
| 68536 | 1356 | for a :: "'a::semiring_numeral" | 
| 1357 | by simp_all | |
| 1358 | ||
| 1359 | lemma mult_1s_ring_1: | |
| 63654 | 1360 | "- Numeral1 * b = - b" | 
| 1361 | "b * - Numeral1 = - b" | |
| 68536 | 1362 | for b :: "'a::ring_1" | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1363 | by simp_all | 
| 47108 | 1364 | |
| 68536 | 1365 | lemmas mult_1s = mult_1s_semiring_numeral mult_1s_ring_1 | 
| 1366 | ||
| 60758 | 1367 | setup \<open> | 
| 47226 | 1368 | Reorient_Proc.add | 
| 69593 | 1369 | (fn Const (\<^const_name>\<open>numeral\<close>, _) $ _ => true | 
| 1370 | | Const (\<^const_name>\<open>uminus\<close>, _) $ (Const (\<^const_name>\<open>numeral\<close>, _) $ _) => true | |
| 63654 | 1371 | | _ => false) | 
| 60758 | 1372 | \<close> | 
| 47226 | 1373 | |
| 63654 | 1374 | simproc_setup reorient_numeral ("numeral w = x" | "- numeral w = y") =
 | 
| 1375 | Reorient_Proc.proc | |
| 47226 | 1376 | |
| 47108 | 1377 | |
| 63654 | 1378 | subsubsection \<open>Simplification of arithmetic operations on integer constants\<close> | 
| 47108 | 1379 | |
| 1380 | lemmas arith_special = (* already declared simp above *) | |
| 1381 | add_numeral_special add_neg_numeral_special | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1382 | diff_numeral_special | 
| 47108 | 1383 | |
| 63654 | 1384 | lemmas arith_extra_simps = (* rules already in simpset *) | 
| 47108 | 1385 | numeral_plus_numeral add_neg_numeral_simps add_0_left add_0_right | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1386 | minus_zero | 
| 47108 | 1387 | diff_numeral_simps diff_0 diff_0_right | 
| 1388 | numeral_times_numeral mult_neg_numeral_simps | |
| 1389 | mult_zero_left mult_zero_right | |
| 1390 | abs_numeral abs_neg_numeral | |
| 1391 | ||
| 60758 | 1392 | text \<open> | 
| 47108 | 1393 | For making a minimal simpset, one must include these default simprules. | 
| 61799 | 1394 | Also include \<open>simp_thms\<close>. | 
| 60758 | 1395 | \<close> | 
| 47108 | 1396 | |
| 1397 | lemmas arith_simps = | |
| 1398 | add_num_simps mult_num_simps sub_num_simps | |
| 1399 | BitM.simps dbl_simps dbl_inc_simps dbl_dec_simps | |
| 1400 | abs_zero abs_one arith_extra_simps | |
| 1401 | ||
| 54249 | 1402 | lemmas more_arith_simps = | 
| 1403 | neg_le_iff_le | |
| 1404 | minus_zero left_minus right_minus | |
| 1405 | mult_1_left mult_1_right | |
| 1406 | mult_minus_left mult_minus_right | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
55974diff
changeset | 1407 | minus_add_distrib minus_minus mult.assoc | 
| 54249 | 1408 | |
| 1409 | lemmas of_nat_simps = | |
| 1410 | of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult | |
| 1411 | ||
| 63654 | 1412 | text \<open>Simplification of relational operations.\<close> | 
| 47108 | 1413 | |
| 1414 | lemmas eq_numeral_extra = | |
| 1415 | zero_neq_one one_neq_zero | |
| 1416 | ||
| 1417 | lemmas rel_simps = | |
| 1418 | le_num_simps less_num_simps eq_num_simps | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1419 | le_numeral_simps le_neg_numeral_simps le_minus_one_simps le_numeral_extra | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1420 | less_numeral_simps less_neg_numeral_simps less_minus_one_simps less_numeral_extra | 
| 47108 | 1421 | eq_numeral_simps eq_neg_numeral_simps eq_numeral_extra | 
| 1422 | ||
| 54249 | 1423 | lemma Let_numeral [simp]: "Let (numeral v) f = f (numeral v)" | 
| 61799 | 1424 | \<comment> \<open>Unfold all \<open>let\<close>s involving constants\<close> | 
| 54249 | 1425 | unfolding Let_def .. | 
| 1426 | ||
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1427 | lemma Let_neg_numeral [simp]: "Let (- numeral v) f = f (- numeral v)" | 
| 61799 | 1428 | \<comment> \<open>Unfold all \<open>let\<close>s involving constants\<close> | 
| 54249 | 1429 | unfolding Let_def .. | 
| 1430 | ||
| 60758 | 1431 | declaration \<open> | 
| 63654 | 1432 | let | 
| 59996 | 1433 | fun number_of ctxt T n = | 
| 69593 | 1434 | if not (Sign.of_sort (Proof_Context.theory_of ctxt) (T, \<^sort>\<open>numeral\<close>)) | 
| 54249 | 1435 |     then raise CTERM ("number_of", [])
 | 
| 59996 | 1436 | else Numeral.mk_cnumber (Thm.ctyp_of ctxt T) n; | 
| 54249 | 1437 | in | 
| 1438 | K ( | |
| 70356 
4a327c061870
streamlined setup for linear algebra, particularly removed redundant rule declarations
 haftmann parents: 
70270diff
changeset | 1439 | Lin_Arith.set_number_of number_of | 
| 63654 | 1440 | #> Lin_Arith.add_simps | 
| 70356 
4a327c061870
streamlined setup for linear algebra, particularly removed redundant rule declarations
 haftmann parents: 
70270diff
changeset | 1441 |       @{thms arith_simps more_arith_simps rel_simps pred_numeral_simps
 | 
| 
4a327c061870
streamlined setup for linear algebra, particularly removed redundant rule declarations
 haftmann parents: 
70270diff
changeset | 1442 | arith_special numeral_One of_nat_simps uminus_numeral_One | 
| 
4a327c061870
streamlined setup for linear algebra, particularly removed redundant rule declarations
 haftmann parents: 
70270diff
changeset | 1443 | Suc_numeral Let_numeral Let_neg_numeral Let_0 Let_1 | 
| 63654 | 1444 | le_Suc_numeral le_numeral_Suc less_Suc_numeral less_numeral_Suc | 
| 70356 
4a327c061870
streamlined setup for linear algebra, particularly removed redundant rule declarations
 haftmann parents: 
70270diff
changeset | 1445 | Suc_eq_numeral eq_numeral_Suc mult_Suc mult_Suc_right of_nat_numeral}) | 
| 54249 | 1446 | end | 
| 60758 | 1447 | \<close> | 
| 54249 | 1448 | |
| 47108 | 1449 | |
| 63654 | 1450 | subsubsection \<open>Simplification of arithmetic when nested to the right\<close> | 
| 47108 | 1451 | |
| 63654 | 1452 | lemma add_numeral_left [simp]: "numeral v + (numeral w + z) = (numeral(v + w) + z)" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
55974diff
changeset | 1453 | by (simp_all add: add.assoc [symmetric]) | 
| 47108 | 1454 | |
| 1455 | lemma add_neg_numeral_left [simp]: | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1456 | "numeral v + (- numeral w + y) = (sub v w + y)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1457 | "- numeral v + (numeral w + y) = (sub w v + y)" | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54249diff
changeset | 1458 | "- numeral v + (- numeral w + y) = (- numeral(v + w) + y)" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
55974diff
changeset | 1459 | by (simp_all add: add.assoc [symmetric]) | 
| 47108 | 1460 | |
| 68536 | 1461 | lemma mult_numeral_left_semiring_numeral: | 
| 47108 | 1462 | "numeral v * (numeral w * z) = (numeral(v * w) * z :: 'a::semiring_numeral)" | 
| 68536 | 1463 | by (simp add: mult.assoc [symmetric]) | 
| 1464 | ||
| 1465 | lemma mult_numeral_left_ring_1: | |
| 1466 | "- numeral v * (numeral w * y) = (- numeral(v * w) * y :: 'a::ring_1)" | |
| 1467 | "numeral v * (- numeral w * y) = (- numeral(v * w) * y :: 'a::ring_1)" | |
| 1468 | "- numeral v * (- numeral w * y) = (numeral(v * w) * y :: 'a::ring_1)" | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
55974diff
changeset | 1469 | by (simp_all add: mult.assoc [symmetric]) | 
| 47108 | 1470 | |
| 68536 | 1471 | lemmas mult_numeral_left [simp] = | 
| 1472 | mult_numeral_left_semiring_numeral | |
| 1473 | mult_numeral_left_ring_1 | |
| 1474 | ||
| 47108 | 1475 | hide_const (open) One Bit0 Bit1 BitM inc pow sqr sub dbl dbl_inc dbl_dec | 
| 1476 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50817diff
changeset | 1477 | |
| 63654 | 1478 | subsection \<open>Code module namespace\<close> | 
| 47108 | 1479 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52210diff
changeset | 1480 | code_identifier | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52210diff
changeset | 1481 | code_module Num \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith | 
| 47108 | 1482 | |
| 66283 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1483 | subsection \<open>Printing of evaluated natural numbers as numerals\<close> | 
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1484 | |
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1485 | lemma [code_post]: | 
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1486 | "Suc 0 = 1" | 
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1487 | "Suc 1 = 2" | 
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1488 | "Suc (numeral n) = numeral (Num.inc n)" | 
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1489 | by (simp_all add: numeral_inc) | 
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1490 | |
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1491 | lemmas [code_post] = Num.inc.simps | 
| 
adf3155c57e2
Printing natural numbers as numerals in evaluation
 eberlm <eberlm@in.tum.de> parents: 
64238diff
changeset | 1492 | |
| 47108 | 1493 | end |