src/HOL/Num.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62348 9a5f43dac883
child 62481 b5d8e57826df
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
huffman@47108
     1
(*  Title:      HOL/Num.thy
huffman@47108
     2
    Author:     Florian Haftmann
huffman@47108
     3
    Author:     Brian Huffman
huffman@47108
     4
*)
huffman@47108
     5
wenzelm@60758
     6
section \<open>Binary Numerals\<close>
huffman@47108
     7
huffman@47108
     8
theory Num
blanchet@58154
     9
imports BNF_Least_Fixpoint
huffman@47108
    10
begin
huffman@47108
    11
wenzelm@61799
    12
subsection \<open>The \<open>num\<close> type\<close>
huffman@47108
    13
blanchet@58310
    14
datatype num = One | Bit0 num | Bit1 num
huffman@47108
    15
wenzelm@60758
    16
text \<open>Increment function for type @{typ num}\<close>
huffman@47108
    17
huffman@47108
    18
primrec inc :: "num \<Rightarrow> num" where
huffman@47108
    19
  "inc One = Bit0 One" |
huffman@47108
    20
  "inc (Bit0 x) = Bit1 x" |
huffman@47108
    21
  "inc (Bit1 x) = Bit0 (inc x)"
huffman@47108
    22
wenzelm@60758
    23
text \<open>Converting between type @{typ num} and type @{typ nat}\<close>
huffman@47108
    24
huffman@47108
    25
primrec nat_of_num :: "num \<Rightarrow> nat" where
huffman@47108
    26
  "nat_of_num One = Suc 0" |
huffman@47108
    27
  "nat_of_num (Bit0 x) = nat_of_num x + nat_of_num x" |
huffman@47108
    28
  "nat_of_num (Bit1 x) = Suc (nat_of_num x + nat_of_num x)"
huffman@47108
    29
huffman@47108
    30
primrec num_of_nat :: "nat \<Rightarrow> num" where
huffman@47108
    31
  "num_of_nat 0 = One" |
huffman@47108
    32
  "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
huffman@47108
    33
huffman@47108
    34
lemma nat_of_num_pos: "0 < nat_of_num x"
huffman@47108
    35
  by (induct x) simp_all
huffman@47108
    36
huffman@47108
    37
lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0"
huffman@47108
    38
  by (induct x) simp_all
huffman@47108
    39
huffman@47108
    40
lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
huffman@47108
    41
  by (induct x) simp_all
huffman@47108
    42
huffman@47108
    43
lemma num_of_nat_double:
huffman@47108
    44
  "0 < n \<Longrightarrow> num_of_nat (n + n) = Bit0 (num_of_nat n)"
huffman@47108
    45
  by (induct n) simp_all
huffman@47108
    46
wenzelm@60758
    47
text \<open>
huffman@47108
    48
  Type @{typ num} is isomorphic to the strictly positive
huffman@47108
    49
  natural numbers.
wenzelm@60758
    50
\<close>
huffman@47108
    51
huffman@47108
    52
lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
huffman@47108
    53
  by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
huffman@47108
    54
huffman@47108
    55
lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
huffman@47108
    56
  by (induct n) (simp_all add: nat_of_num_inc)
huffman@47108
    57
huffman@47108
    58
lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
huffman@47108
    59
  apply safe
huffman@47108
    60
  apply (drule arg_cong [where f=num_of_nat])
huffman@47108
    61
  apply (simp add: nat_of_num_inverse)
huffman@47108
    62
  done
huffman@47108
    63
huffman@47108
    64
lemma num_induct [case_names One inc]:
huffman@47108
    65
  fixes P :: "num \<Rightarrow> bool"
huffman@47108
    66
  assumes One: "P One"
huffman@47108
    67
    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
huffman@47108
    68
  shows "P x"
huffman@47108
    69
proof -
huffman@47108
    70
  obtain n where n: "Suc n = nat_of_num x"
huffman@47108
    71
    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
huffman@47108
    72
  have "P (num_of_nat (Suc n))"
huffman@47108
    73
  proof (induct n)
huffman@47108
    74
    case 0 show ?case using One by simp
huffman@47108
    75
  next
huffman@47108
    76
    case (Suc n)
huffman@47108
    77
    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
huffman@47108
    78
    then show "P (num_of_nat (Suc (Suc n)))" by simp
huffman@47108
    79
  qed
huffman@47108
    80
  with n show "P x"
huffman@47108
    81
    by (simp add: nat_of_num_inverse)
huffman@47108
    82
qed
huffman@47108
    83
wenzelm@60758
    84
text \<open>
huffman@47108
    85
  From now on, there are two possible models for @{typ num}:
wenzelm@61799
    86
  as positive naturals (rule \<open>num_induct\<close>)
wenzelm@61799
    87
  and as digit representation (rules \<open>num.induct\<close>, \<open>num.cases\<close>).
wenzelm@60758
    88
\<close>
huffman@47108
    89
huffman@47108
    90
wenzelm@60758
    91
subsection \<open>Numeral operations\<close>
huffman@47108
    92
huffman@47108
    93
instantiation num :: "{plus,times,linorder}"
huffman@47108
    94
begin
huffman@47108
    95
huffman@47108
    96
definition [code del]:
huffman@47108
    97
  "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
huffman@47108
    98
huffman@47108
    99
definition [code del]:
huffman@47108
   100
  "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
huffman@47108
   101
huffman@47108
   102
definition [code del]:
huffman@47108
   103
  "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
huffman@47108
   104
huffman@47108
   105
definition [code del]:
huffman@47108
   106
  "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
huffman@47108
   107
huffman@47108
   108
instance
wenzelm@61169
   109
  by standard (auto simp add: less_num_def less_eq_num_def num_eq_iff)
huffman@47108
   110
huffman@47108
   111
end
huffman@47108
   112
huffman@47108
   113
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
huffman@47108
   114
  unfolding plus_num_def
huffman@47108
   115
  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
huffman@47108
   116
huffman@47108
   117
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
huffman@47108
   118
  unfolding times_num_def
huffman@47108
   119
  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
huffman@47108
   120
huffman@47108
   121
lemma add_num_simps [simp, code]:
huffman@47108
   122
  "One + One = Bit0 One"
huffman@47108
   123
  "One + Bit0 n = Bit1 n"
huffman@47108
   124
  "One + Bit1 n = Bit0 (n + One)"
huffman@47108
   125
  "Bit0 m + One = Bit1 m"
huffman@47108
   126
  "Bit0 m + Bit0 n = Bit0 (m + n)"
huffman@47108
   127
  "Bit0 m + Bit1 n = Bit1 (m + n)"
huffman@47108
   128
  "Bit1 m + One = Bit0 (m + One)"
huffman@47108
   129
  "Bit1 m + Bit0 n = Bit1 (m + n)"
huffman@47108
   130
  "Bit1 m + Bit1 n = Bit0 (m + n + One)"
huffman@47108
   131
  by (simp_all add: num_eq_iff nat_of_num_add)
huffman@47108
   132
huffman@47108
   133
lemma mult_num_simps [simp, code]:
huffman@47108
   134
  "m * One = m"
huffman@47108
   135
  "One * n = n"
huffman@47108
   136
  "Bit0 m * Bit0 n = Bit0 (Bit0 (m * n))"
huffman@47108
   137
  "Bit0 m * Bit1 n = Bit0 (m * Bit1 n)"
huffman@47108
   138
  "Bit1 m * Bit0 n = Bit0 (Bit1 m * n)"
huffman@47108
   139
  "Bit1 m * Bit1 n = Bit1 (m + n + Bit0 (m * n))"
huffman@47108
   140
  by (simp_all add: num_eq_iff nat_of_num_add
webertj@49962
   141
    nat_of_num_mult distrib_right distrib_left)
huffman@47108
   142
huffman@47108
   143
lemma eq_num_simps:
huffman@47108
   144
  "One = One \<longleftrightarrow> True"
huffman@47108
   145
  "One = Bit0 n \<longleftrightarrow> False"
huffman@47108
   146
  "One = Bit1 n \<longleftrightarrow> False"
huffman@47108
   147
  "Bit0 m = One \<longleftrightarrow> False"
huffman@47108
   148
  "Bit1 m = One \<longleftrightarrow> False"
huffman@47108
   149
  "Bit0 m = Bit0 n \<longleftrightarrow> m = n"
huffman@47108
   150
  "Bit0 m = Bit1 n \<longleftrightarrow> False"
huffman@47108
   151
  "Bit1 m = Bit0 n \<longleftrightarrow> False"
huffman@47108
   152
  "Bit1 m = Bit1 n \<longleftrightarrow> m = n"
huffman@47108
   153
  by simp_all
huffman@47108
   154
huffman@47108
   155
lemma le_num_simps [simp, code]:
huffman@47108
   156
  "One \<le> n \<longleftrightarrow> True"
huffman@47108
   157
  "Bit0 m \<le> One \<longleftrightarrow> False"
huffman@47108
   158
  "Bit1 m \<le> One \<longleftrightarrow> False"
huffman@47108
   159
  "Bit0 m \<le> Bit0 n \<longleftrightarrow> m \<le> n"
huffman@47108
   160
  "Bit0 m \<le> Bit1 n \<longleftrightarrow> m \<le> n"
huffman@47108
   161
  "Bit1 m \<le> Bit1 n \<longleftrightarrow> m \<le> n"
huffman@47108
   162
  "Bit1 m \<le> Bit0 n \<longleftrightarrow> m < n"
huffman@47108
   163
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
huffman@47108
   164
  by (auto simp add: less_eq_num_def less_num_def)
huffman@47108
   165
huffman@47108
   166
lemma less_num_simps [simp, code]:
huffman@47108
   167
  "m < One \<longleftrightarrow> False"
huffman@47108
   168
  "One < Bit0 n \<longleftrightarrow> True"
huffman@47108
   169
  "One < Bit1 n \<longleftrightarrow> True"
huffman@47108
   170
  "Bit0 m < Bit0 n \<longleftrightarrow> m < n"
huffman@47108
   171
  "Bit0 m < Bit1 n \<longleftrightarrow> m \<le> n"
huffman@47108
   172
  "Bit1 m < Bit1 n \<longleftrightarrow> m < n"
huffman@47108
   173
  "Bit1 m < Bit0 n \<longleftrightarrow> m < n"
huffman@47108
   174
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
huffman@47108
   175
  by (auto simp add: less_eq_num_def less_num_def)
huffman@47108
   176
Andreas@61630
   177
lemma le_num_One_iff: "x \<le> num.One \<longleftrightarrow> x = num.One"
Andreas@61630
   178
by (simp add: antisym_conv)
Andreas@61630
   179
wenzelm@61799
   180
text \<open>Rules using \<open>One\<close> and \<open>inc\<close> as constructors\<close>
huffman@47108
   181
huffman@47108
   182
lemma add_One: "x + One = inc x"
huffman@47108
   183
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
huffman@47108
   184
huffman@47108
   185
lemma add_One_commute: "One + n = n + One"
huffman@47108
   186
  by (induct n) simp_all
huffman@47108
   187
huffman@47108
   188
lemma add_inc: "x + inc y = inc (x + y)"
huffman@47108
   189
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
huffman@47108
   190
huffman@47108
   191
lemma mult_inc: "x * inc y = x * y + x"
huffman@47108
   192
  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
huffman@47108
   193
wenzelm@60758
   194
text \<open>The @{const num_of_nat} conversion\<close>
huffman@47108
   195
huffman@47108
   196
lemma num_of_nat_One:
huffman@47300
   197
  "n \<le> 1 \<Longrightarrow> num_of_nat n = One"
huffman@47108
   198
  by (cases n) simp_all
huffman@47108
   199
huffman@47108
   200
lemma num_of_nat_plus_distrib:
huffman@47108
   201
  "0 < m \<Longrightarrow> 0 < n \<Longrightarrow> num_of_nat (m + n) = num_of_nat m + num_of_nat n"
huffman@47108
   202
  by (induct n) (auto simp add: add_One add_One_commute add_inc)
huffman@47108
   203
wenzelm@60758
   204
text \<open>A double-and-decrement function\<close>
huffman@47108
   205
huffman@47108
   206
primrec BitM :: "num \<Rightarrow> num" where
huffman@47108
   207
  "BitM One = One" |
huffman@47108
   208
  "BitM (Bit0 n) = Bit1 (BitM n)" |
huffman@47108
   209
  "BitM (Bit1 n) = Bit1 (Bit0 n)"
huffman@47108
   210
huffman@47108
   211
lemma BitM_plus_one: "BitM n + One = Bit0 n"
huffman@47108
   212
  by (induct n) simp_all
huffman@47108
   213
huffman@47108
   214
lemma one_plus_BitM: "One + BitM n = Bit0 n"
huffman@47108
   215
  unfolding add_One_commute BitM_plus_one ..
huffman@47108
   216
wenzelm@60758
   217
text \<open>Squaring and exponentiation\<close>
huffman@47108
   218
huffman@47108
   219
primrec sqr :: "num \<Rightarrow> num" where
huffman@47108
   220
  "sqr One = One" |
huffman@47108
   221
  "sqr (Bit0 n) = Bit0 (Bit0 (sqr n))" |
huffman@47108
   222
  "sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))"
huffman@47108
   223
huffman@47108
   224
primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
huffman@47108
   225
  "pow x One = x" |
huffman@47108
   226
  "pow x (Bit0 y) = sqr (pow x y)" |
huffman@47191
   227
  "pow x (Bit1 y) = sqr (pow x y) * x"
huffman@47108
   228
huffman@47108
   229
lemma nat_of_num_sqr: "nat_of_num (sqr x) = nat_of_num x * nat_of_num x"
huffman@47108
   230
  by (induct x, simp_all add: algebra_simps nat_of_num_add)
huffman@47108
   231
huffman@47108
   232
lemma sqr_conv_mult: "sqr x = x * x"
huffman@47108
   233
  by (simp add: num_eq_iff nat_of_num_sqr nat_of_num_mult)
huffman@47108
   234
huffman@47108
   235
wenzelm@60758
   236
subsection \<open>Binary numerals\<close>
huffman@47108
   237
wenzelm@60758
   238
text \<open>
huffman@47211
   239
  We embed binary representations into a generic algebraic
wenzelm@61799
   240
  structure using \<open>numeral\<close>.
wenzelm@60758
   241
\<close>
huffman@47108
   242
huffman@47108
   243
class numeral = one + semigroup_add
huffman@47108
   244
begin
huffman@47108
   245
huffman@47108
   246
primrec numeral :: "num \<Rightarrow> 'a" where
huffman@47108
   247
  numeral_One: "numeral One = 1" |
huffman@47108
   248
  numeral_Bit0: "numeral (Bit0 n) = numeral n + numeral n" |
huffman@47108
   249
  numeral_Bit1: "numeral (Bit1 n) = numeral n + numeral n + 1"
huffman@47108
   250
haftmann@50817
   251
lemma numeral_code [code]:
haftmann@50817
   252
  "numeral One = 1"
haftmann@50817
   253
  "numeral (Bit0 n) = (let m = numeral n in m + m)"
haftmann@50817
   254
  "numeral (Bit1 n) = (let m = numeral n in m + m + 1)"
haftmann@50817
   255
  by (simp_all add: Let_def)
haftmann@50817
   256
  
huffman@47108
   257
lemma one_plus_numeral_commute: "1 + numeral x = numeral x + 1"
huffman@47108
   258
  apply (induct x)
huffman@47108
   259
  apply simp
haftmann@57512
   260
  apply (simp add: add.assoc [symmetric], simp add: add.assoc)
haftmann@57512
   261
  apply (simp add: add.assoc [symmetric], simp add: add.assoc)
huffman@47108
   262
  done
huffman@47108
   263
huffman@47108
   264
lemma numeral_inc: "numeral (inc x) = numeral x + 1"
huffman@47108
   265
proof (induct x)
huffman@47108
   266
  case (Bit1 x)
huffman@47108
   267
  have "numeral x + (1 + numeral x) + 1 = numeral x + (numeral x + 1) + 1"
huffman@47108
   268
    by (simp only: one_plus_numeral_commute)
huffman@47108
   269
  with Bit1 show ?case
haftmann@57512
   270
    by (simp add: add.assoc)
huffman@47108
   271
qed simp_all
huffman@47108
   272
huffman@47108
   273
declare numeral.simps [simp del]
huffman@47108
   274
huffman@47108
   275
abbreviation "Numeral1 \<equiv> numeral One"
huffman@47108
   276
huffman@47108
   277
declare numeral_One [code_post]
huffman@47108
   278
huffman@47108
   279
end
huffman@47108
   280
wenzelm@60758
   281
text \<open>Numeral syntax.\<close>
huffman@47108
   282
huffman@47108
   283
syntax
huffman@47108
   284
  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
huffman@47108
   285
haftmann@58410
   286
ML_file "Tools/numeral.ML"
haftmann@58410
   287
wenzelm@60758
   288
parse_translation \<open>
wenzelm@52143
   289
  let
wenzelm@52143
   290
    fun numeral_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] =
wenzelm@52143
   291
          c $ numeral_tr [t] $ u
wenzelm@52143
   292
      | numeral_tr [Const (num, _)] =
wenzelm@58421
   293
          (Numeral.mk_number_syntax o #value o Lexicon.read_num) num
wenzelm@52143
   294
      | numeral_tr ts = raise TERM ("numeral_tr", ts);
wenzelm@55974
   295
  in [(@{syntax_const "_Numeral"}, K numeral_tr)] end
wenzelm@60758
   296
\<close>
huffman@47108
   297
wenzelm@60758
   298
typed_print_translation \<open>
wenzelm@52143
   299
  let
wenzelm@52143
   300
    fun dest_num (Const (@{const_syntax Bit0}, _) $ n) = 2 * dest_num n
wenzelm@52143
   301
      | dest_num (Const (@{const_syntax Bit1}, _) $ n) = 2 * dest_num n + 1
wenzelm@52143
   302
      | dest_num (Const (@{const_syntax One}, _)) = 1;
haftmann@54489
   303
    fun num_tr' ctxt T [n] =
wenzelm@52143
   304
      let
wenzelm@52143
   305
        val k = dest_num n;
wenzelm@52187
   306
        val t' =
wenzelm@52187
   307
          Syntax.const @{syntax_const "_Numeral"} $
haftmann@54489
   308
            Syntax.free (string_of_int k);
wenzelm@52143
   309
      in
wenzelm@52143
   310
        (case T of
wenzelm@52143
   311
          Type (@{type_name fun}, [_, T']) =>
wenzelm@52210
   312
            if Printer.type_emphasis ctxt T' then
wenzelm@52210
   313
              Syntax.const @{syntax_const "_constrain"} $ t' $
wenzelm@52210
   314
                Syntax_Phases.term_of_typ ctxt T'
wenzelm@52210
   315
            else t'
wenzelm@52187
   316
        | _ => if T = dummyT then t' else raise Match)
wenzelm@52143
   317
      end;
wenzelm@52143
   318
  in
haftmann@54489
   319
   [(@{const_syntax numeral}, num_tr')]
wenzelm@52143
   320
  end
wenzelm@60758
   321
\<close>
huffman@47108
   322
huffman@47228
   323
wenzelm@60758
   324
subsection \<open>Class-specific numeral rules\<close>
huffman@47108
   325
wenzelm@60758
   326
text \<open>
huffman@47108
   327
  @{const numeral} is a morphism.
wenzelm@60758
   328
\<close>
huffman@47108
   329
wenzelm@61799
   330
subsubsection \<open>Structures with addition: class \<open>numeral\<close>\<close>
huffman@47108
   331
huffman@47108
   332
context numeral
huffman@47108
   333
begin
huffman@47108
   334
huffman@47108
   335
lemma numeral_add: "numeral (m + n) = numeral m + numeral n"
huffman@47108
   336
  by (induct n rule: num_induct)
haftmann@57512
   337
     (simp_all only: numeral_One add_One add_inc numeral_inc add.assoc)
huffman@47108
   338
huffman@47108
   339
lemma numeral_plus_numeral: "numeral m + numeral n = numeral (m + n)"
huffman@47108
   340
  by (rule numeral_add [symmetric])
huffman@47108
   341
huffman@47108
   342
lemma numeral_plus_one: "numeral n + 1 = numeral (n + One)"
huffman@47108
   343
  using numeral_add [of n One] by (simp add: numeral_One)
huffman@47108
   344
huffman@47108
   345
lemma one_plus_numeral: "1 + numeral n = numeral (One + n)"
huffman@47108
   346
  using numeral_add [of One n] by (simp add: numeral_One)
huffman@47108
   347
huffman@47108
   348
lemma one_add_one: "1 + 1 = 2"
huffman@47108
   349
  using numeral_add [of One One] by (simp add: numeral_One)
huffman@47108
   350
huffman@47108
   351
lemmas add_numeral_special =
huffman@47108
   352
  numeral_plus_one one_plus_numeral one_add_one
huffman@47108
   353
huffman@47108
   354
end
huffman@47108
   355
wenzelm@60758
   356
subsubsection \<open>
wenzelm@61799
   357
  Structures with negation: class \<open>neg_numeral\<close>
wenzelm@60758
   358
\<close>
huffman@47108
   359
haftmann@54489
   360
class neg_numeral = numeral + group_add
huffman@47108
   361
begin
huffman@47108
   362
haftmann@54489
   363
lemma uminus_numeral_One:
haftmann@54489
   364
  "- Numeral1 = - 1"
haftmann@54489
   365
  by (simp add: numeral_One)
haftmann@54489
   366
wenzelm@60758
   367
text \<open>Numerals form an abelian subgroup.\<close>
huffman@47108
   368
huffman@47108
   369
inductive is_num :: "'a \<Rightarrow> bool" where
huffman@47108
   370
  "is_num 1" |
huffman@47108
   371
  "is_num x \<Longrightarrow> is_num (- x)" |
huffman@47108
   372
  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> is_num (x + y)"
huffman@47108
   373
huffman@47108
   374
lemma is_num_numeral: "is_num (numeral k)"
huffman@47108
   375
  by (induct k, simp_all add: numeral.simps is_num.intros)
huffman@47108
   376
huffman@47108
   377
lemma is_num_add_commute:
huffman@47108
   378
  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + y = y + x"
huffman@47108
   379
  apply (induct x rule: is_num.induct)
huffman@47108
   380
  apply (induct y rule: is_num.induct)
huffman@47108
   381
  apply simp
huffman@47108
   382
  apply (rule_tac a=x in add_left_imp_eq)
huffman@47108
   383
  apply (rule_tac a=x in add_right_imp_eq)
haftmann@57512
   384
  apply (simp add: add.assoc)
haftmann@57512
   385
  apply (simp add: add.assoc [symmetric], simp add: add.assoc)
huffman@47108
   386
  apply (rule_tac a=x in add_left_imp_eq)
huffman@47108
   387
  apply (rule_tac a=x in add_right_imp_eq)
haftmann@57512
   388
  apply (simp add: add.assoc)
haftmann@57512
   389
  apply (simp add: add.assoc, simp add: add.assoc [symmetric])
huffman@47108
   390
  done
huffman@47108
   391
huffman@47108
   392
lemma is_num_add_left_commute:
huffman@47108
   393
  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + (y + z) = y + (x + z)"
haftmann@57512
   394
  by (simp only: add.assoc [symmetric] is_num_add_commute)
huffman@47108
   395
huffman@47108
   396
lemmas is_num_normalize =
haftmann@57512
   397
  add.assoc is_num_add_commute is_num_add_left_commute
huffman@47108
   398
  is_num.intros is_num_numeral
haftmann@54230
   399
  minus_add
huffman@47108
   400
huffman@47108
   401
definition dbl :: "'a \<Rightarrow> 'a" where "dbl x = x + x"
huffman@47108
   402
definition dbl_inc :: "'a \<Rightarrow> 'a" where "dbl_inc x = x + x + 1"
huffman@47108
   403
definition dbl_dec :: "'a \<Rightarrow> 'a" where "dbl_dec x = x + x - 1"
huffman@47108
   404
huffman@47108
   405
definition sub :: "num \<Rightarrow> num \<Rightarrow> 'a" where
huffman@47108
   406
  "sub k l = numeral k - numeral l"
huffman@47108
   407
huffman@47108
   408
lemma numeral_BitM: "numeral (BitM n) = numeral (Bit0 n) - 1"
huffman@47108
   409
  by (simp only: BitM_plus_one [symmetric] numeral_add numeral_One eq_diff_eq)
huffman@47108
   410
huffman@47108
   411
lemma dbl_simps [simp]:
haftmann@54489
   412
  "dbl (- numeral k) = - dbl (numeral k)"
huffman@47108
   413
  "dbl 0 = 0"
huffman@47108
   414
  "dbl 1 = 2"
haftmann@54489
   415
  "dbl (- 1) = - 2"
huffman@47108
   416
  "dbl (numeral k) = numeral (Bit0 k)"
haftmann@54489
   417
  by (simp_all add: dbl_def numeral.simps minus_add)
huffman@47108
   418
huffman@47108
   419
lemma dbl_inc_simps [simp]:
haftmann@54489
   420
  "dbl_inc (- numeral k) = - dbl_dec (numeral k)"
huffman@47108
   421
  "dbl_inc 0 = 1"
huffman@47108
   422
  "dbl_inc 1 = 3"
haftmann@54489
   423
  "dbl_inc (- 1) = - 1"
huffman@47108
   424
  "dbl_inc (numeral k) = numeral (Bit1 k)"
haftmann@54489
   425
  by (simp_all add: dbl_inc_def dbl_dec_def numeral.simps numeral_BitM is_num_normalize algebra_simps del: add_uminus_conv_diff)
huffman@47108
   426
huffman@47108
   427
lemma dbl_dec_simps [simp]:
haftmann@54489
   428
  "dbl_dec (- numeral k) = - dbl_inc (numeral k)"
haftmann@54489
   429
  "dbl_dec 0 = - 1"
huffman@47108
   430
  "dbl_dec 1 = 1"
haftmann@54489
   431
  "dbl_dec (- 1) = - 3"
huffman@47108
   432
  "dbl_dec (numeral k) = numeral (BitM k)"
haftmann@54489
   433
  by (simp_all add: dbl_dec_def dbl_inc_def numeral.simps numeral_BitM is_num_normalize)
huffman@47108
   434
huffman@47108
   435
lemma sub_num_simps [simp]:
huffman@47108
   436
  "sub One One = 0"
haftmann@54489
   437
  "sub One (Bit0 l) = - numeral (BitM l)"
haftmann@54489
   438
  "sub One (Bit1 l) = - numeral (Bit0 l)"
huffman@47108
   439
  "sub (Bit0 k) One = numeral (BitM k)"
huffman@47108
   440
  "sub (Bit1 k) One = numeral (Bit0 k)"
huffman@47108
   441
  "sub (Bit0 k) (Bit0 l) = dbl (sub k l)"
huffman@47108
   442
  "sub (Bit0 k) (Bit1 l) = dbl_dec (sub k l)"
huffman@47108
   443
  "sub (Bit1 k) (Bit0 l) = dbl_inc (sub k l)"
huffman@47108
   444
  "sub (Bit1 k) (Bit1 l) = dbl (sub k l)"
haftmann@54489
   445
  by (simp_all add: dbl_def dbl_dec_def dbl_inc_def sub_def numeral.simps
haftmann@54230
   446
    numeral_BitM is_num_normalize del: add_uminus_conv_diff add: diff_conv_add_uminus)
huffman@47108
   447
huffman@47108
   448
lemma add_neg_numeral_simps:
haftmann@54489
   449
  "numeral m + - numeral n = sub m n"
haftmann@54489
   450
  "- numeral m + numeral n = sub n m"
haftmann@54489
   451
  "- numeral m + - numeral n = - (numeral m + numeral n)"
haftmann@54489
   452
  by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize
haftmann@54230
   453
    del: add_uminus_conv_diff add: diff_conv_add_uminus)
huffman@47108
   454
huffman@47108
   455
lemma add_neg_numeral_special:
haftmann@54489
   456
  "1 + - numeral m = sub One m"
haftmann@54489
   457
  "- numeral m + 1 = sub One m"
haftmann@54489
   458
  "numeral m + - 1 = sub m One"
haftmann@54489
   459
  "- 1 + numeral n = sub n One"
haftmann@54489
   460
  "- 1 + - numeral n = - numeral (inc n)"
haftmann@54489
   461
  "- numeral m + - 1 = - numeral (inc m)"
haftmann@54489
   462
  "1 + - 1 = 0"
haftmann@54489
   463
  "- 1 + 1 = 0"
haftmann@54489
   464
  "- 1 + - 1 = - 2"
haftmann@54489
   465
  by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize right_minus numeral_inc
haftmann@54489
   466
    del: add_uminus_conv_diff add: diff_conv_add_uminus)
huffman@47108
   467
huffman@47108
   468
lemma diff_numeral_simps:
huffman@47108
   469
  "numeral m - numeral n = sub m n"
haftmann@54489
   470
  "numeral m - - numeral n = numeral (m + n)"
haftmann@54489
   471
  "- numeral m - numeral n = - numeral (m + n)"
haftmann@54489
   472
  "- numeral m - - numeral n = sub n m"
haftmann@54489
   473
  by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize
haftmann@54230
   474
    del: add_uminus_conv_diff add: diff_conv_add_uminus)
huffman@47108
   475
huffman@47108
   476
lemma diff_numeral_special:
huffman@47108
   477
  "1 - numeral n = sub One n"
huffman@47108
   478
  "numeral m - 1 = sub m One"
haftmann@54489
   479
  "1 - - numeral n = numeral (One + n)"
haftmann@54489
   480
  "- numeral m - 1 = - numeral (m + One)"
haftmann@54489
   481
  "- 1 - numeral n = - numeral (inc n)"
haftmann@54489
   482
  "numeral m - - 1 = numeral (inc m)"
haftmann@54489
   483
  "- 1 - - numeral n = sub n One"
haftmann@54489
   484
  "- numeral m - - 1 = sub One m"
haftmann@54489
   485
  "1 - 1 = 0"
haftmann@54489
   486
  "- 1 - 1 = - 2"
haftmann@54489
   487
  "1 - - 1 = 2"
haftmann@54489
   488
  "- 1 - - 1 = 0"
haftmann@54489
   489
  by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize numeral_inc
haftmann@54489
   490
    del: add_uminus_conv_diff add: diff_conv_add_uminus)
huffman@47108
   491
huffman@47108
   492
end
huffman@47108
   493
wenzelm@60758
   494
subsubsection \<open>
wenzelm@61799
   495
  Structures with multiplication: class \<open>semiring_numeral\<close>
wenzelm@60758
   496
\<close>
huffman@47108
   497
huffman@47108
   498
class semiring_numeral = semiring + monoid_mult
huffman@47108
   499
begin
huffman@47108
   500
huffman@47108
   501
subclass numeral ..
huffman@47108
   502
huffman@47108
   503
lemma numeral_mult: "numeral (m * n) = numeral m * numeral n"
huffman@47108
   504
  apply (induct n rule: num_induct)
huffman@47108
   505
  apply (simp add: numeral_One)
webertj@49962
   506
  apply (simp add: mult_inc numeral_inc numeral_add distrib_left)
huffman@47108
   507
  done
huffman@47108
   508
huffman@47108
   509
lemma numeral_times_numeral: "numeral m * numeral n = numeral (m * n)"
huffman@47108
   510
  by (rule numeral_mult [symmetric])
huffman@47108
   511
haftmann@53064
   512
lemma mult_2: "2 * z = z + z"
haftmann@53064
   513
  unfolding one_add_one [symmetric] distrib_right by simp
haftmann@53064
   514
haftmann@53064
   515
lemma mult_2_right: "z * 2 = z + z"
haftmann@53064
   516
  unfolding one_add_one [symmetric] distrib_left by simp
haftmann@53064
   517
huffman@47108
   518
end
huffman@47108
   519
wenzelm@60758
   520
subsubsection \<open>
wenzelm@61799
   521
  Structures with a zero: class \<open>semiring_1\<close>
wenzelm@60758
   522
\<close>
huffman@47108
   523
huffman@47108
   524
context semiring_1
huffman@47108
   525
begin
huffman@47108
   526
huffman@47108
   527
subclass semiring_numeral ..
huffman@47108
   528
huffman@47108
   529
lemma of_nat_numeral [simp]: "of_nat (numeral n) = numeral n"
huffman@47108
   530
  by (induct n,
huffman@47108
   531
    simp_all only: numeral.simps numeral_class.numeral.simps of_nat_add of_nat_1)
huffman@47108
   532
huffman@47108
   533
end
huffman@47108
   534
haftmann@51143
   535
lemma nat_of_num_numeral [code_abbrev]:
haftmann@51143
   536
  "nat_of_num = numeral"
huffman@47108
   537
proof
huffman@47108
   538
  fix n
huffman@47108
   539
  have "numeral n = nat_of_num n"
huffman@47108
   540
    by (induct n) (simp_all add: numeral.simps)
huffman@47108
   541
  then show "nat_of_num n = numeral n" by simp
huffman@47108
   542
qed
huffman@47108
   543
haftmann@51143
   544
lemma nat_of_num_code [code]:
haftmann@51143
   545
  "nat_of_num One = 1"
haftmann@51143
   546
  "nat_of_num (Bit0 n) = (let m = nat_of_num n in m + m)"
haftmann@51143
   547
  "nat_of_num (Bit1 n) = (let m = nat_of_num n in Suc (m + m))"
haftmann@51143
   548
  by (simp_all add: Let_def)
haftmann@51143
   549
wenzelm@60758
   550
subsubsection \<open>
wenzelm@61799
   551
  Equality: class \<open>semiring_char_0\<close>
wenzelm@60758
   552
\<close>
huffman@47108
   553
huffman@47108
   554
context semiring_char_0
huffman@47108
   555
begin
huffman@47108
   556
huffman@47108
   557
lemma numeral_eq_iff: "numeral m = numeral n \<longleftrightarrow> m = n"
huffman@47108
   558
  unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
huffman@47108
   559
    of_nat_eq_iff num_eq_iff ..
huffman@47108
   560
huffman@47108
   561
lemma numeral_eq_one_iff: "numeral n = 1 \<longleftrightarrow> n = One"
huffman@47108
   562
  by (rule numeral_eq_iff [of n One, unfolded numeral_One])
huffman@47108
   563
huffman@47108
   564
lemma one_eq_numeral_iff: "1 = numeral n \<longleftrightarrow> One = n"
huffman@47108
   565
  by (rule numeral_eq_iff [of One n, unfolded numeral_One])
huffman@47108
   566
huffman@47108
   567
lemma numeral_neq_zero: "numeral n \<noteq> 0"
huffman@47108
   568
  unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
huffman@47108
   569
  by (simp add: nat_of_num_pos)
huffman@47108
   570
huffman@47108
   571
lemma zero_neq_numeral: "0 \<noteq> numeral n"
huffman@47108
   572
  unfolding eq_commute [of 0] by (rule numeral_neq_zero)
huffman@47108
   573
huffman@47108
   574
lemmas eq_numeral_simps [simp] =
huffman@47108
   575
  numeral_eq_iff
huffman@47108
   576
  numeral_eq_one_iff
huffman@47108
   577
  one_eq_numeral_iff
huffman@47108
   578
  numeral_neq_zero
huffman@47108
   579
  zero_neq_numeral
huffman@47108
   580
huffman@47108
   581
end
huffman@47108
   582
wenzelm@60758
   583
subsubsection \<open>
wenzelm@61799
   584
  Comparisons: class \<open>linordered_semidom\<close>
wenzelm@60758
   585
\<close>
huffman@47108
   586
wenzelm@60758
   587
text \<open>Could be perhaps more general than here.\<close>
huffman@47108
   588
huffman@47108
   589
context linordered_semidom
huffman@47108
   590
begin
huffman@47108
   591
huffman@47108
   592
lemma numeral_le_iff: "numeral m \<le> numeral n \<longleftrightarrow> m \<le> n"
huffman@47108
   593
proof -
huffman@47108
   594
  have "of_nat (numeral m) \<le> of_nat (numeral n) \<longleftrightarrow> m \<le> n"
huffman@47108
   595
    unfolding less_eq_num_def nat_of_num_numeral of_nat_le_iff ..
huffman@47108
   596
  then show ?thesis by simp
huffman@47108
   597
qed
huffman@47108
   598
huffman@47108
   599
lemma one_le_numeral: "1 \<le> numeral n"
huffman@47108
   600
using numeral_le_iff [of One n] by (simp add: numeral_One)
huffman@47108
   601
huffman@47108
   602
lemma numeral_le_one_iff: "numeral n \<le> 1 \<longleftrightarrow> n \<le> One"
huffman@47108
   603
using numeral_le_iff [of n One] by (simp add: numeral_One)
huffman@47108
   604
huffman@47108
   605
lemma numeral_less_iff: "numeral m < numeral n \<longleftrightarrow> m < n"
huffman@47108
   606
proof -
huffman@47108
   607
  have "of_nat (numeral m) < of_nat (numeral n) \<longleftrightarrow> m < n"
huffman@47108
   608
    unfolding less_num_def nat_of_num_numeral of_nat_less_iff ..
huffman@47108
   609
  then show ?thesis by simp
huffman@47108
   610
qed
huffman@47108
   611
huffman@47108
   612
lemma not_numeral_less_one: "\<not> numeral n < 1"
huffman@47108
   613
  using numeral_less_iff [of n One] by (simp add: numeral_One)
huffman@47108
   614
huffman@47108
   615
lemma one_less_numeral_iff: "1 < numeral n \<longleftrightarrow> One < n"
huffman@47108
   616
  using numeral_less_iff [of One n] by (simp add: numeral_One)
huffman@47108
   617
huffman@47108
   618
lemma zero_le_numeral: "0 \<le> numeral n"
huffman@47108
   619
  by (induct n) (simp_all add: numeral.simps)
huffman@47108
   620
huffman@47108
   621
lemma zero_less_numeral: "0 < numeral n"
huffman@47108
   622
  by (induct n) (simp_all add: numeral.simps add_pos_pos)
huffman@47108
   623
huffman@47108
   624
lemma not_numeral_le_zero: "\<not> numeral n \<le> 0"
huffman@47108
   625
  by (simp add: not_le zero_less_numeral)
huffman@47108
   626
huffman@47108
   627
lemma not_numeral_less_zero: "\<not> numeral n < 0"
huffman@47108
   628
  by (simp add: not_less zero_le_numeral)
huffman@47108
   629
huffman@47108
   630
lemmas le_numeral_extra =
huffman@47108
   631
  zero_le_one not_one_le_zero
huffman@47108
   632
  order_refl [of 0] order_refl [of 1]
huffman@47108
   633
huffman@47108
   634
lemmas less_numeral_extra =
huffman@47108
   635
  zero_less_one not_one_less_zero
huffman@47108
   636
  less_irrefl [of 0] less_irrefl [of 1]
huffman@47108
   637
huffman@47108
   638
lemmas le_numeral_simps [simp] =
huffman@47108
   639
  numeral_le_iff
huffman@47108
   640
  one_le_numeral
huffman@47108
   641
  numeral_le_one_iff
huffman@47108
   642
  zero_le_numeral
huffman@47108
   643
  not_numeral_le_zero
huffman@47108
   644
huffman@47108
   645
lemmas less_numeral_simps [simp] =
huffman@47108
   646
  numeral_less_iff
huffman@47108
   647
  one_less_numeral_iff
huffman@47108
   648
  not_numeral_less_one
huffman@47108
   649
  zero_less_numeral
huffman@47108
   650
  not_numeral_less_zero
huffman@47108
   651
Andreas@61630
   652
lemma min_0_1 [simp]:
Andreas@61630
   653
  fixes min' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" defines "min' \<equiv> min" shows
Andreas@61630
   654
  "min' 0 1 = 0"
Andreas@61630
   655
  "min' 1 0 = 0"
Andreas@61630
   656
  "min' 0 (numeral x) = 0"
Andreas@61630
   657
  "min' (numeral x) 0 = 0"
Andreas@61630
   658
  "min' 1 (numeral x) = 1"
Andreas@61630
   659
  "min' (numeral x) 1 = 1"
Andreas@61630
   660
by(simp_all add: min'_def min_def le_num_One_iff)
Andreas@61630
   661
Andreas@61630
   662
lemma max_0_1 [simp]: 
Andreas@61630
   663
  fixes max' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" defines "max' \<equiv> max" shows
Andreas@61630
   664
  "max' 0 1 = 1"
Andreas@61630
   665
  "max' 1 0 = 1"
Andreas@61630
   666
  "max' 0 (numeral x) = numeral x"
Andreas@61630
   667
  "max' (numeral x) 0 = numeral x"
Andreas@61630
   668
  "max' 1 (numeral x) = numeral x"
Andreas@61630
   669
  "max' (numeral x) 1 = numeral x"
Andreas@61630
   670
by(simp_all add: max'_def max_def le_num_One_iff)
Andreas@61630
   671
huffman@47108
   672
end
huffman@47108
   673
wenzelm@60758
   674
subsubsection \<open>
wenzelm@61799
   675
  Multiplication and negation: class \<open>ring_1\<close>
wenzelm@60758
   676
\<close>
huffman@47108
   677
huffman@47108
   678
context ring_1
huffman@47108
   679
begin
huffman@47108
   680
huffman@47108
   681
subclass neg_numeral ..
huffman@47108
   682
huffman@47108
   683
lemma mult_neg_numeral_simps:
haftmann@54489
   684
  "- numeral m * - numeral n = numeral (m * n)"
haftmann@54489
   685
  "- numeral m * numeral n = - numeral (m * n)"
haftmann@54489
   686
  "numeral m * - numeral n = - numeral (m * n)"
haftmann@54489
   687
  unfolding mult_minus_left mult_minus_right
huffman@47108
   688
  by (simp_all only: minus_minus numeral_mult)
huffman@47108
   689
haftmann@54489
   690
lemma mult_minus1 [simp]: "- 1 * z = - z"
haftmann@54489
   691
  unfolding numeral.simps mult_minus_left by simp
huffman@47108
   692
haftmann@54489
   693
lemma mult_minus1_right [simp]: "z * - 1 = - z"
haftmann@54489
   694
  unfolding numeral.simps mult_minus_right by simp
huffman@47108
   695
huffman@47108
   696
end
huffman@47108
   697
wenzelm@60758
   698
subsubsection \<open>
wenzelm@61799
   699
  Equality using \<open>iszero\<close> for rings with non-zero characteristic
wenzelm@60758
   700
\<close>
huffman@47108
   701
huffman@47108
   702
context ring_1
huffman@47108
   703
begin
huffman@47108
   704
huffman@47108
   705
definition iszero :: "'a \<Rightarrow> bool"
huffman@47108
   706
  where "iszero z \<longleftrightarrow> z = 0"
huffman@47108
   707
huffman@47108
   708
lemma iszero_0 [simp]: "iszero 0"
huffman@47108
   709
  by (simp add: iszero_def)
huffman@47108
   710
huffman@47108
   711
lemma not_iszero_1 [simp]: "\<not> iszero 1"
huffman@47108
   712
  by (simp add: iszero_def)
huffman@47108
   713
huffman@47108
   714
lemma not_iszero_Numeral1: "\<not> iszero Numeral1"
huffman@47108
   715
  by (simp add: numeral_One)
huffman@47108
   716
haftmann@54489
   717
lemma not_iszero_neg_1 [simp]: "\<not> iszero (- 1)"
haftmann@54489
   718
  by (simp add: iszero_def)
haftmann@54489
   719
haftmann@54489
   720
lemma not_iszero_neg_Numeral1: "\<not> iszero (- Numeral1)"
haftmann@54489
   721
  by (simp add: numeral_One)
haftmann@54489
   722
huffman@47108
   723
lemma iszero_neg_numeral [simp]:
haftmann@54489
   724
  "iszero (- numeral w) \<longleftrightarrow> iszero (numeral w)"
haftmann@54489
   725
  unfolding iszero_def
huffman@47108
   726
  by (rule neg_equal_0_iff_equal)
huffman@47108
   727
huffman@47108
   728
lemma eq_iff_iszero_diff: "x = y \<longleftrightarrow> iszero (x - y)"
huffman@47108
   729
  unfolding iszero_def by (rule eq_iff_diff_eq_0)
huffman@47108
   730
wenzelm@61799
   731
text \<open>The \<open>eq_numeral_iff_iszero\<close> lemmas are not declared
wenzelm@61799
   732
\<open>[simp]\<close> by default, because for rings of characteristic zero,
wenzelm@61799
   733
better simp rules are possible. For a type like integers mod \<open>n\<close>, type-instantiated versions of these rules should be added to the
huffman@47108
   734
simplifier, along with a type-specific rule for deciding propositions
wenzelm@61799
   735
of the form \<open>iszero (numeral w)\<close>.
huffman@47108
   736
huffman@47108
   737
bh: Maybe it would not be so bad to just declare these as simp
huffman@47108
   738
rules anyway? I should test whether these rules take precedence over
wenzelm@61799
   739
the \<open>ring_char_0\<close> rules in the simplifier.
wenzelm@60758
   740
\<close>
huffman@47108
   741
huffman@47108
   742
lemma eq_numeral_iff_iszero:
huffman@47108
   743
  "numeral x = numeral y \<longleftrightarrow> iszero (sub x y)"
haftmann@54489
   744
  "numeral x = - numeral y \<longleftrightarrow> iszero (numeral (x + y))"
haftmann@54489
   745
  "- numeral x = numeral y \<longleftrightarrow> iszero (numeral (x + y))"
haftmann@54489
   746
  "- numeral x = - numeral y \<longleftrightarrow> iszero (sub y x)"
huffman@47108
   747
  "numeral x = 1 \<longleftrightarrow> iszero (sub x One)"
huffman@47108
   748
  "1 = numeral y \<longleftrightarrow> iszero (sub One y)"
haftmann@54489
   749
  "- numeral x = 1 \<longleftrightarrow> iszero (numeral (x + One))"
haftmann@54489
   750
  "1 = - numeral y \<longleftrightarrow> iszero (numeral (One + y))"
huffman@47108
   751
  "numeral x = 0 \<longleftrightarrow> iszero (numeral x)"
huffman@47108
   752
  "0 = numeral y \<longleftrightarrow> iszero (numeral y)"
haftmann@54489
   753
  "- numeral x = 0 \<longleftrightarrow> iszero (numeral x)"
haftmann@54489
   754
  "0 = - numeral y \<longleftrightarrow> iszero (numeral y)"
huffman@47108
   755
  unfolding eq_iff_iszero_diff diff_numeral_simps diff_numeral_special
huffman@47108
   756
  by simp_all
huffman@47108
   757
huffman@47108
   758
end
huffman@47108
   759
wenzelm@60758
   760
subsubsection \<open>
wenzelm@61799
   761
  Equality and negation: class \<open>ring_char_0\<close>
wenzelm@60758
   762
\<close>
huffman@47108
   763
huffman@47108
   764
class ring_char_0 = ring_1 + semiring_char_0
huffman@47108
   765
begin
huffman@47108
   766
huffman@47108
   767
lemma not_iszero_numeral [simp]: "\<not> iszero (numeral w)"
huffman@47108
   768
  by (simp add: iszero_def)
huffman@47108
   769
haftmann@54489
   770
lemma neg_numeral_eq_iff: "- numeral m = - numeral n \<longleftrightarrow> m = n"
haftmann@54489
   771
  by simp
huffman@47108
   772
haftmann@54489
   773
lemma numeral_neq_neg_numeral: "numeral m \<noteq> - numeral n"
haftmann@54489
   774
  unfolding eq_neg_iff_add_eq_0
huffman@47108
   775
  by (simp add: numeral_plus_numeral)
huffman@47108
   776
haftmann@54489
   777
lemma neg_numeral_neq_numeral: "- numeral m \<noteq> numeral n"
huffman@47108
   778
  by (rule numeral_neq_neg_numeral [symmetric])
huffman@47108
   779
haftmann@54489
   780
lemma zero_neq_neg_numeral: "0 \<noteq> - numeral n"
haftmann@54489
   781
  unfolding neg_0_equal_iff_equal by simp
huffman@47108
   782
haftmann@54489
   783
lemma neg_numeral_neq_zero: "- numeral n \<noteq> 0"
haftmann@54489
   784
  unfolding neg_equal_0_iff_equal by simp
huffman@47108
   785
haftmann@54489
   786
lemma one_neq_neg_numeral: "1 \<noteq> - numeral n"
huffman@47108
   787
  using numeral_neq_neg_numeral [of One n] by (simp add: numeral_One)
huffman@47108
   788
haftmann@54489
   789
lemma neg_numeral_neq_one: "- numeral n \<noteq> 1"
huffman@47108
   790
  using neg_numeral_neq_numeral [of n One] by (simp add: numeral_One)
huffman@47108
   791
haftmann@54489
   792
lemma neg_one_neq_numeral:
haftmann@54489
   793
  "- 1 \<noteq> numeral n"
haftmann@54489
   794
  using neg_numeral_neq_numeral [of One n] by (simp add: numeral_One)
haftmann@54489
   795
haftmann@54489
   796
lemma numeral_neq_neg_one:
haftmann@54489
   797
  "numeral n \<noteq> - 1"
haftmann@54489
   798
  using numeral_neq_neg_numeral [of n One] by (simp add: numeral_One)
haftmann@54489
   799
haftmann@54489
   800
lemma neg_one_eq_numeral_iff:
haftmann@54489
   801
  "- 1 = - numeral n \<longleftrightarrow> n = One"
haftmann@54489
   802
  using neg_numeral_eq_iff [of One n] by (auto simp add: numeral_One)
haftmann@54489
   803
haftmann@54489
   804
lemma numeral_eq_neg_one_iff:
haftmann@54489
   805
  "- numeral n = - 1 \<longleftrightarrow> n = One"
haftmann@54489
   806
  using neg_numeral_eq_iff [of n One] by (auto simp add: numeral_One)
haftmann@54489
   807
haftmann@54489
   808
lemma neg_one_neq_zero:
haftmann@54489
   809
  "- 1 \<noteq> 0"
haftmann@54489
   810
  by simp
haftmann@54489
   811
haftmann@54489
   812
lemma zero_neq_neg_one:
haftmann@54489
   813
  "0 \<noteq> - 1"
haftmann@54489
   814
  by simp
haftmann@54489
   815
haftmann@54489
   816
lemma neg_one_neq_one:
haftmann@54489
   817
  "- 1 \<noteq> 1"
haftmann@54489
   818
  using neg_numeral_neq_numeral [of One One] by (simp only: numeral_One not_False_eq_True)
haftmann@54489
   819
haftmann@54489
   820
lemma one_neq_neg_one:
haftmann@54489
   821
  "1 \<noteq> - 1"
haftmann@54489
   822
  using numeral_neq_neg_numeral [of One One] by (simp only: numeral_One not_False_eq_True)
haftmann@54489
   823
huffman@47108
   824
lemmas eq_neg_numeral_simps [simp] =
huffman@47108
   825
  neg_numeral_eq_iff
huffman@47108
   826
  numeral_neq_neg_numeral neg_numeral_neq_numeral
huffman@47108
   827
  one_neq_neg_numeral neg_numeral_neq_one
huffman@47108
   828
  zero_neq_neg_numeral neg_numeral_neq_zero
haftmann@54489
   829
  neg_one_neq_numeral numeral_neq_neg_one
haftmann@54489
   830
  neg_one_eq_numeral_iff numeral_eq_neg_one_iff
haftmann@54489
   831
  neg_one_neq_zero zero_neq_neg_one
haftmann@54489
   832
  neg_one_neq_one one_neq_neg_one
huffman@47108
   833
huffman@47108
   834
end
huffman@47108
   835
haftmann@62348
   836
text\<open>Also the class for fields with characteristic zero.\<close>
haftmann@62348
   837
haftmann@62348
   838
class field_char_0 = field + ring_char_0
haftmann@62348
   839
haftmann@62348
   840
wenzelm@60758
   841
subsubsection \<open>
wenzelm@61799
   842
  Structures with negation and order: class \<open>linordered_idom\<close>
wenzelm@60758
   843
\<close>
huffman@47108
   844
huffman@47108
   845
context linordered_idom
huffman@47108
   846
begin
huffman@47108
   847
huffman@47108
   848
subclass ring_char_0 ..
huffman@47108
   849
haftmann@54489
   850
lemma neg_numeral_le_iff: "- numeral m \<le> - numeral n \<longleftrightarrow> n \<le> m"
haftmann@54489
   851
  by (simp only: neg_le_iff_le numeral_le_iff)
huffman@47108
   852
haftmann@54489
   853
lemma neg_numeral_less_iff: "- numeral m < - numeral n \<longleftrightarrow> n < m"
haftmann@54489
   854
  by (simp only: neg_less_iff_less numeral_less_iff)
huffman@47108
   855
haftmann@54489
   856
lemma neg_numeral_less_zero: "- numeral n < 0"
haftmann@54489
   857
  by (simp only: neg_less_0_iff_less zero_less_numeral)
huffman@47108
   858
haftmann@54489
   859
lemma neg_numeral_le_zero: "- numeral n \<le> 0"
haftmann@54489
   860
  by (simp only: neg_le_0_iff_le zero_le_numeral)
huffman@47108
   861
haftmann@54489
   862
lemma not_zero_less_neg_numeral: "\<not> 0 < - numeral n"
huffman@47108
   863
  by (simp only: not_less neg_numeral_le_zero)
huffman@47108
   864
haftmann@54489
   865
lemma not_zero_le_neg_numeral: "\<not> 0 \<le> - numeral n"
huffman@47108
   866
  by (simp only: not_le neg_numeral_less_zero)
huffman@47108
   867
haftmann@54489
   868
lemma neg_numeral_less_numeral: "- numeral m < numeral n"
huffman@47108
   869
  using neg_numeral_less_zero zero_less_numeral by (rule less_trans)
huffman@47108
   870
haftmann@54489
   871
lemma neg_numeral_le_numeral: "- numeral m \<le> numeral n"
huffman@47108
   872
  by (simp only: less_imp_le neg_numeral_less_numeral)
huffman@47108
   873
haftmann@54489
   874
lemma not_numeral_less_neg_numeral: "\<not> numeral m < - numeral n"
huffman@47108
   875
  by (simp only: not_less neg_numeral_le_numeral)
huffman@47108
   876
haftmann@54489
   877
lemma not_numeral_le_neg_numeral: "\<not> numeral m \<le> - numeral n"
huffman@47108
   878
  by (simp only: not_le neg_numeral_less_numeral)
huffman@47108
   879
  
haftmann@54489
   880
lemma neg_numeral_less_one: "- numeral m < 1"
huffman@47108
   881
  by (rule neg_numeral_less_numeral [of m One, unfolded numeral_One])
huffman@47108
   882
haftmann@54489
   883
lemma neg_numeral_le_one: "- numeral m \<le> 1"
huffman@47108
   884
  by (rule neg_numeral_le_numeral [of m One, unfolded numeral_One])
huffman@47108
   885
haftmann@54489
   886
lemma not_one_less_neg_numeral: "\<not> 1 < - numeral m"
huffman@47108
   887
  by (simp only: not_less neg_numeral_le_one)
huffman@47108
   888
haftmann@54489
   889
lemma not_one_le_neg_numeral: "\<not> 1 \<le> - numeral m"
huffman@47108
   890
  by (simp only: not_le neg_numeral_less_one)
huffman@47108
   891
haftmann@54489
   892
lemma not_numeral_less_neg_one: "\<not> numeral m < - 1"
haftmann@54489
   893
  using not_numeral_less_neg_numeral [of m One] by (simp add: numeral_One)
haftmann@54489
   894
haftmann@54489
   895
lemma not_numeral_le_neg_one: "\<not> numeral m \<le> - 1"
haftmann@54489
   896
  using not_numeral_le_neg_numeral [of m One] by (simp add: numeral_One)
haftmann@54489
   897
haftmann@54489
   898
lemma neg_one_less_numeral: "- 1 < numeral m"
haftmann@54489
   899
  using neg_numeral_less_numeral [of One m] by (simp add: numeral_One)
haftmann@54489
   900
haftmann@54489
   901
lemma neg_one_le_numeral: "- 1 \<le> numeral m"
haftmann@54489
   902
  using neg_numeral_le_numeral [of One m] by (simp add: numeral_One)
haftmann@54489
   903
haftmann@54489
   904
lemma neg_numeral_less_neg_one_iff: "- numeral m < - 1 \<longleftrightarrow> m \<noteq> One"
haftmann@54489
   905
  by (cases m) simp_all
haftmann@54489
   906
haftmann@54489
   907
lemma neg_numeral_le_neg_one: "- numeral m \<le> - 1"
haftmann@54489
   908
  by simp
haftmann@54489
   909
haftmann@54489
   910
lemma not_neg_one_less_neg_numeral: "\<not> - 1 < - numeral m"
haftmann@54489
   911
  by simp
haftmann@54489
   912
haftmann@54489
   913
lemma not_neg_one_le_neg_numeral_iff: "\<not> - 1 \<le> - numeral m \<longleftrightarrow> m \<noteq> One"
haftmann@54489
   914
  by (cases m) simp_all
haftmann@54489
   915
huffman@47108
   916
lemma sub_non_negative:
huffman@47108
   917
  "sub n m \<ge> 0 \<longleftrightarrow> n \<ge> m"
huffman@47108
   918
  by (simp only: sub_def le_diff_eq) simp
huffman@47108
   919
huffman@47108
   920
lemma sub_positive:
huffman@47108
   921
  "sub n m > 0 \<longleftrightarrow> n > m"
huffman@47108
   922
  by (simp only: sub_def less_diff_eq) simp
huffman@47108
   923
huffman@47108
   924
lemma sub_non_positive:
huffman@47108
   925
  "sub n m \<le> 0 \<longleftrightarrow> n \<le> m"
huffman@47108
   926
  by (simp only: sub_def diff_le_eq) simp
huffman@47108
   927
huffman@47108
   928
lemma sub_negative:
huffman@47108
   929
  "sub n m < 0 \<longleftrightarrow> n < m"
huffman@47108
   930
  by (simp only: sub_def diff_less_eq) simp
huffman@47108
   931
huffman@47108
   932
lemmas le_neg_numeral_simps [simp] =
huffman@47108
   933
  neg_numeral_le_iff
huffman@47108
   934
  neg_numeral_le_numeral not_numeral_le_neg_numeral
huffman@47108
   935
  neg_numeral_le_zero not_zero_le_neg_numeral
huffman@47108
   936
  neg_numeral_le_one not_one_le_neg_numeral
haftmann@54489
   937
  neg_one_le_numeral not_numeral_le_neg_one
haftmann@54489
   938
  neg_numeral_le_neg_one not_neg_one_le_neg_numeral_iff
haftmann@54489
   939
haftmann@54489
   940
lemma le_minus_one_simps [simp]:
haftmann@54489
   941
  "- 1 \<le> 0"
haftmann@54489
   942
  "- 1 \<le> 1"
haftmann@54489
   943
  "\<not> 0 \<le> - 1"
haftmann@54489
   944
  "\<not> 1 \<le> - 1"
haftmann@54489
   945
  by simp_all
huffman@47108
   946
huffman@47108
   947
lemmas less_neg_numeral_simps [simp] =
huffman@47108
   948
  neg_numeral_less_iff
huffman@47108
   949
  neg_numeral_less_numeral not_numeral_less_neg_numeral
huffman@47108
   950
  neg_numeral_less_zero not_zero_less_neg_numeral
huffman@47108
   951
  neg_numeral_less_one not_one_less_neg_numeral
haftmann@54489
   952
  neg_one_less_numeral not_numeral_less_neg_one
haftmann@54489
   953
  neg_numeral_less_neg_one_iff not_neg_one_less_neg_numeral
haftmann@54489
   954
haftmann@54489
   955
lemma less_minus_one_simps [simp]:
haftmann@54489
   956
  "- 1 < 0"
haftmann@54489
   957
  "- 1 < 1"
haftmann@54489
   958
  "\<not> 0 < - 1"
haftmann@54489
   959
  "\<not> 1 < - 1"
haftmann@54489
   960
  by (simp_all add: less_le)
huffman@47108
   961
wenzelm@61944
   962
lemma abs_numeral [simp]: "\<bar>numeral n\<bar> = numeral n"
huffman@47108
   963
  by simp
huffman@47108
   964
wenzelm@61944
   965
lemma abs_neg_numeral [simp]: "\<bar>- numeral n\<bar> = numeral n"
haftmann@54489
   966
  by (simp only: abs_minus_cancel abs_numeral)
haftmann@54489
   967
wenzelm@61944
   968
lemma abs_neg_one [simp]: "\<bar>- 1\<bar> = 1"
haftmann@54489
   969
  by simp
huffman@47108
   970
huffman@47108
   971
end
huffman@47108
   972
wenzelm@60758
   973
subsubsection \<open>
huffman@47108
   974
  Natural numbers
wenzelm@60758
   975
\<close>
huffman@47108
   976
huffman@47299
   977
lemma Suc_1 [simp]: "Suc 1 = 2"
huffman@47299
   978
  unfolding Suc_eq_plus1 by (rule one_add_one)
huffman@47299
   979
huffman@47108
   980
lemma Suc_numeral [simp]: "Suc (numeral n) = numeral (n + One)"
huffman@47299
   981
  unfolding Suc_eq_plus1 by (rule numeral_plus_one)
huffman@47108
   982
huffman@47209
   983
definition pred_numeral :: "num \<Rightarrow> nat"
huffman@47209
   984
  where [code del]: "pred_numeral k = numeral k - 1"
huffman@47209
   985
huffman@47209
   986
lemma numeral_eq_Suc: "numeral k = Suc (pred_numeral k)"
huffman@47209
   987
  unfolding pred_numeral_def by simp
huffman@47209
   988
huffman@47220
   989
lemma eval_nat_numeral:
huffman@47108
   990
  "numeral One = Suc 0"
huffman@47108
   991
  "numeral (Bit0 n) = Suc (numeral (BitM n))"
huffman@47108
   992
  "numeral (Bit1 n) = Suc (numeral (Bit0 n))"
huffman@47108
   993
  by (simp_all add: numeral.simps BitM_plus_one)
huffman@47108
   994
huffman@47209
   995
lemma pred_numeral_simps [simp]:
huffman@47300
   996
  "pred_numeral One = 0"
huffman@47300
   997
  "pred_numeral (Bit0 k) = numeral (BitM k)"
huffman@47300
   998
  "pred_numeral (Bit1 k) = numeral (Bit0 k)"
huffman@47220
   999
  unfolding pred_numeral_def eval_nat_numeral
huffman@47209
  1000
  by (simp_all only: diff_Suc_Suc diff_0)
huffman@47209
  1001
huffman@47192
  1002
lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
huffman@47220
  1003
  by (simp add: eval_nat_numeral)
huffman@47192
  1004
huffman@47192
  1005
lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"
huffman@47220
  1006
  by (simp add: eval_nat_numeral)
huffman@47192
  1007
huffman@47207
  1008
lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
huffman@47207
  1009
  by (simp only: numeral_One One_nat_def)
huffman@47207
  1010
huffman@47207
  1011
lemma Suc_nat_number_of_add:
huffman@47300
  1012
  "Suc (numeral v + n) = numeral (v + One) + n"
huffman@47207
  1013
  by simp
huffman@47207
  1014
huffman@47207
  1015
(*Maps #n to n for n = 1, 2*)
huffman@47207
  1016
lemmas numerals = numeral_One [where 'a=nat] numeral_2_eq_2
huffman@47207
  1017
wenzelm@60758
  1018
text \<open>Comparisons involving @{term Suc}.\<close>
huffman@47209
  1019
huffman@47209
  1020
lemma eq_numeral_Suc [simp]: "numeral k = Suc n \<longleftrightarrow> pred_numeral k = n"
huffman@47209
  1021
  by (simp add: numeral_eq_Suc)
huffman@47209
  1022
huffman@47209
  1023
lemma Suc_eq_numeral [simp]: "Suc n = numeral k \<longleftrightarrow> n = pred_numeral k"
huffman@47209
  1024
  by (simp add: numeral_eq_Suc)
huffman@47209
  1025
huffman@47209
  1026
lemma less_numeral_Suc [simp]: "numeral k < Suc n \<longleftrightarrow> pred_numeral k < n"
huffman@47209
  1027
  by (simp add: numeral_eq_Suc)
huffman@47209
  1028
huffman@47209
  1029
lemma less_Suc_numeral [simp]: "Suc n < numeral k \<longleftrightarrow> n < pred_numeral k"
huffman@47209
  1030
  by (simp add: numeral_eq_Suc)
huffman@47209
  1031
huffman@47209
  1032
lemma le_numeral_Suc [simp]: "numeral k \<le> Suc n \<longleftrightarrow> pred_numeral k \<le> n"
huffman@47209
  1033
  by (simp add: numeral_eq_Suc)
huffman@47209
  1034
huffman@47209
  1035
lemma le_Suc_numeral [simp]: "Suc n \<le> numeral k \<longleftrightarrow> n \<le> pred_numeral k"
huffman@47209
  1036
  by (simp add: numeral_eq_Suc)
huffman@47209
  1037
huffman@47218
  1038
lemma diff_Suc_numeral [simp]: "Suc n - numeral k = n - pred_numeral k"
huffman@47218
  1039
  by (simp add: numeral_eq_Suc)
huffman@47218
  1040
huffman@47218
  1041
lemma diff_numeral_Suc [simp]: "numeral k - Suc n = pred_numeral k - n"
huffman@47218
  1042
  by (simp add: numeral_eq_Suc)
huffman@47218
  1043
huffman@47209
  1044
lemma max_Suc_numeral [simp]:
huffman@47209
  1045
  "max (Suc n) (numeral k) = Suc (max n (pred_numeral k))"
huffman@47209
  1046
  by (simp add: numeral_eq_Suc)
huffman@47209
  1047
huffman@47209
  1048
lemma max_numeral_Suc [simp]:
huffman@47209
  1049
  "max (numeral k) (Suc n) = Suc (max (pred_numeral k) n)"
huffman@47209
  1050
  by (simp add: numeral_eq_Suc)
huffman@47209
  1051
huffman@47209
  1052
lemma min_Suc_numeral [simp]:
huffman@47209
  1053
  "min (Suc n) (numeral k) = Suc (min n (pred_numeral k))"
huffman@47209
  1054
  by (simp add: numeral_eq_Suc)
huffman@47209
  1055
huffman@47209
  1056
lemma min_numeral_Suc [simp]:
huffman@47209
  1057
  "min (numeral k) (Suc n) = Suc (min (pred_numeral k) n)"
huffman@47209
  1058
  by (simp add: numeral_eq_Suc)
huffman@47209
  1059
wenzelm@60758
  1060
text \<open>For @{term case_nat} and @{term rec_nat}.\<close>
huffman@47216
  1061
blanchet@55415
  1062
lemma case_nat_numeral [simp]:
blanchet@55415
  1063
  "case_nat a f (numeral v) = (let pv = pred_numeral v in f pv)"
huffman@47216
  1064
  by (simp add: numeral_eq_Suc)
huffman@47216
  1065
blanchet@55415
  1066
lemma case_nat_add_eq_if [simp]:
blanchet@55415
  1067
  "case_nat a f ((numeral v) + n) = (let pv = pred_numeral v in f (pv + n))"
huffman@47216
  1068
  by (simp add: numeral_eq_Suc)
huffman@47216
  1069
blanchet@55415
  1070
lemma rec_nat_numeral [simp]:
blanchet@55415
  1071
  "rec_nat a f (numeral v) =
blanchet@55415
  1072
    (let pv = pred_numeral v in f pv (rec_nat a f pv))"
huffman@47216
  1073
  by (simp add: numeral_eq_Suc Let_def)
huffman@47216
  1074
blanchet@55415
  1075
lemma rec_nat_add_eq_if [simp]:
blanchet@55415
  1076
  "rec_nat a f (numeral v + n) =
blanchet@55415
  1077
    (let pv = pred_numeral v in f (pv + n) (rec_nat a f (pv + n)))"
huffman@47216
  1078
  by (simp add: numeral_eq_Suc Let_def)
huffman@47216
  1079
wenzelm@60758
  1080
text \<open>Case analysis on @{term "n < 2"}\<close>
huffman@47255
  1081
huffman@47255
  1082
lemma less_2_cases: "n < 2 \<Longrightarrow> n = 0 \<or> n = Suc 0"
huffman@47255
  1083
  by (auto simp add: numeral_2_eq_2)
huffman@47255
  1084
wenzelm@60758
  1085
text \<open>Removal of Small Numerals: 0, 1 and (in additive positions) 2\<close>
wenzelm@60758
  1086
text \<open>bh: Are these rules really a good idea?\<close>
huffman@47255
  1087
huffman@47255
  1088
lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
huffman@47255
  1089
  by simp
huffman@47255
  1090
huffman@47255
  1091
lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
huffman@47255
  1092
  by simp
huffman@47255
  1093
wenzelm@60758
  1094
text \<open>Can be used to eliminate long strings of Sucs, but not by default.\<close>
huffman@47255
  1095
huffman@47255
  1096
lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
huffman@47255
  1097
  by simp
huffman@47255
  1098
huffman@47255
  1099
lemmas nat_1_add_1 = one_add_one [where 'a=nat] (* legacy *)
huffman@47255
  1100
huffman@47108
  1101
wenzelm@60758
  1102
subsection \<open>Particular lemmas concerning @{term 2}\<close>
haftmann@58512
  1103
haftmann@59867
  1104
context linordered_field
haftmann@58512
  1105
begin
haftmann@58512
  1106
haftmann@62348
  1107
subclass field_char_0 ..
haftmann@62348
  1108
haftmann@58512
  1109
lemma half_gt_zero_iff:
haftmann@58512
  1110
  "0 < a / 2 \<longleftrightarrow> 0 < a" (is "?P \<longleftrightarrow> ?Q")
haftmann@58512
  1111
  by (auto simp add: field_simps)
haftmann@58512
  1112
haftmann@58512
  1113
lemma half_gt_zero [simp]:
haftmann@58512
  1114
  "0 < a \<Longrightarrow> 0 < a / 2"
haftmann@58512
  1115
  by (simp add: half_gt_zero_iff)
haftmann@58512
  1116
haftmann@58512
  1117
end
haftmann@58512
  1118
haftmann@58512
  1119
wenzelm@60758
  1120
subsection \<open>Numeral equations as default simplification rules\<close>
huffman@47108
  1121
huffman@47108
  1122
declare (in numeral) numeral_One [simp]
huffman@47108
  1123
declare (in numeral) numeral_plus_numeral [simp]
huffman@47108
  1124
declare (in numeral) add_numeral_special [simp]
huffman@47108
  1125
declare (in neg_numeral) add_neg_numeral_simps [simp]
huffman@47108
  1126
declare (in neg_numeral) add_neg_numeral_special [simp]
huffman@47108
  1127
declare (in neg_numeral) diff_numeral_simps [simp]
huffman@47108
  1128
declare (in neg_numeral) diff_numeral_special [simp]
huffman@47108
  1129
declare (in semiring_numeral) numeral_times_numeral [simp]
huffman@47108
  1130
declare (in ring_1) mult_neg_numeral_simps [simp]
huffman@47108
  1131
wenzelm@60758
  1132
subsection \<open>Setting up simprocs\<close>
huffman@47108
  1133
huffman@47108
  1134
lemma mult_numeral_1: "Numeral1 * a = (a::'a::semiring_numeral)"
huffman@47108
  1135
  by simp
huffman@47108
  1136
huffman@47108
  1137
lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::semiring_numeral)"
huffman@47108
  1138
  by simp
huffman@47108
  1139
huffman@47108
  1140
lemma divide_numeral_1: "a / Numeral1 = (a::'a::field)"
huffman@47108
  1141
  by simp
huffman@47108
  1142
huffman@47108
  1143
lemma inverse_numeral_1:
huffman@47108
  1144
  "inverse Numeral1 = (Numeral1::'a::division_ring)"
huffman@47108
  1145
  by simp
huffman@47108
  1146
wenzelm@60758
  1147
text\<open>Theorem lists for the cancellation simprocs. The use of a binary
wenzelm@60758
  1148
numeral for 1 reduces the number of special cases.\<close>
huffman@47108
  1149
haftmann@54489
  1150
lemma mult_1s:
haftmann@54489
  1151
  fixes a :: "'a::semiring_numeral"
haftmann@54489
  1152
    and b :: "'b::ring_1"
haftmann@54489
  1153
  shows "Numeral1 * a = a"
haftmann@54489
  1154
    "a * Numeral1 = a"
haftmann@54489
  1155
    "- Numeral1 * b = - b"
haftmann@54489
  1156
    "b * - Numeral1 = - b"
haftmann@54489
  1157
  by simp_all
huffman@47108
  1158
wenzelm@60758
  1159
setup \<open>
huffman@47226
  1160
  Reorient_Proc.add
huffman@47226
  1161
    (fn Const (@{const_name numeral}, _) $ _ => true
haftmann@54489
  1162
    | Const (@{const_name uminus}, _) $ (Const (@{const_name numeral}, _) $ _) => true
huffman@47226
  1163
    | _ => false)
wenzelm@60758
  1164
\<close>
huffman@47226
  1165
huffman@47226
  1166
simproc_setup reorient_numeral
haftmann@54489
  1167
  ("numeral w = x" | "- numeral w = y") = Reorient_Proc.proc
huffman@47226
  1168
huffman@47108
  1169
wenzelm@60758
  1170
subsubsection \<open>Simplification of arithmetic operations on integer constants.\<close>
huffman@47108
  1171
huffman@47108
  1172
lemmas arith_special = (* already declared simp above *)
huffman@47108
  1173
  add_numeral_special add_neg_numeral_special
haftmann@54489
  1174
  diff_numeral_special
huffman@47108
  1175
huffman@47108
  1176
(* rules already in simpset *)
huffman@47108
  1177
lemmas arith_extra_simps =
huffman@47108
  1178
  numeral_plus_numeral add_neg_numeral_simps add_0_left add_0_right
haftmann@54489
  1179
  minus_zero
huffman@47108
  1180
  diff_numeral_simps diff_0 diff_0_right
huffman@47108
  1181
  numeral_times_numeral mult_neg_numeral_simps
huffman@47108
  1182
  mult_zero_left mult_zero_right
huffman@47108
  1183
  abs_numeral abs_neg_numeral
huffman@47108
  1184
wenzelm@60758
  1185
text \<open>
huffman@47108
  1186
  For making a minimal simpset, one must include these default simprules.
wenzelm@61799
  1187
  Also include \<open>simp_thms\<close>.
wenzelm@60758
  1188
\<close>
huffman@47108
  1189
huffman@47108
  1190
lemmas arith_simps =
huffman@47108
  1191
  add_num_simps mult_num_simps sub_num_simps
huffman@47108
  1192
  BitM.simps dbl_simps dbl_inc_simps dbl_dec_simps
huffman@47108
  1193
  abs_zero abs_one arith_extra_simps
huffman@47108
  1194
haftmann@54249
  1195
lemmas more_arith_simps =
haftmann@54249
  1196
  neg_le_iff_le
haftmann@54249
  1197
  minus_zero left_minus right_minus
haftmann@54249
  1198
  mult_1_left mult_1_right
haftmann@54249
  1199
  mult_minus_left mult_minus_right
haftmann@57512
  1200
  minus_add_distrib minus_minus mult.assoc
haftmann@54249
  1201
haftmann@54249
  1202
lemmas of_nat_simps =
haftmann@54249
  1203
  of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult
haftmann@54249
  1204
wenzelm@60758
  1205
text \<open>Simplification of relational operations\<close>
huffman@47108
  1206
huffman@47108
  1207
lemmas eq_numeral_extra =
huffman@47108
  1208
  zero_neq_one one_neq_zero
huffman@47108
  1209
huffman@47108
  1210
lemmas rel_simps =
huffman@47108
  1211
  le_num_simps less_num_simps eq_num_simps
haftmann@54489
  1212
  le_numeral_simps le_neg_numeral_simps le_minus_one_simps le_numeral_extra
haftmann@54489
  1213
  less_numeral_simps less_neg_numeral_simps less_minus_one_simps less_numeral_extra
huffman@47108
  1214
  eq_numeral_simps eq_neg_numeral_simps eq_numeral_extra
huffman@47108
  1215
haftmann@54249
  1216
lemma Let_numeral [simp]: "Let (numeral v) f = f (numeral v)"
wenzelm@61799
  1217
  \<comment> \<open>Unfold all \<open>let\<close>s involving constants\<close>
haftmann@54249
  1218
  unfolding Let_def ..
haftmann@54249
  1219
haftmann@54489
  1220
lemma Let_neg_numeral [simp]: "Let (- numeral v) f = f (- numeral v)"
wenzelm@61799
  1221
  \<comment> \<open>Unfold all \<open>let\<close>s involving constants\<close>
haftmann@54249
  1222
  unfolding Let_def ..
haftmann@54249
  1223
wenzelm@60758
  1224
declaration \<open>
haftmann@54249
  1225
let 
wenzelm@59996
  1226
  fun number_of ctxt T n =
wenzelm@59996
  1227
    if not (Sign.of_sort (Proof_Context.theory_of ctxt) (T, @{sort numeral}))
haftmann@54249
  1228
    then raise CTERM ("number_of", [])
wenzelm@59996
  1229
    else Numeral.mk_cnumber (Thm.ctyp_of ctxt T) n;
haftmann@54249
  1230
in
haftmann@54249
  1231
  K (
haftmann@54249
  1232
    Lin_Arith.add_simps (@{thms arith_simps} @ @{thms more_arith_simps}
haftmann@54249
  1233
      @ @{thms rel_simps}
haftmann@54249
  1234
      @ @{thms pred_numeral_simps}
haftmann@54249
  1235
      @ @{thms arith_special numeral_One}
haftmann@54249
  1236
      @ @{thms of_nat_simps})
haftmann@54249
  1237
    #> Lin_Arith.add_simps [@{thm Suc_numeral},
haftmann@54249
  1238
      @{thm Let_numeral}, @{thm Let_neg_numeral}, @{thm Let_0}, @{thm Let_1},
haftmann@54249
  1239
      @{thm le_Suc_numeral}, @{thm le_numeral_Suc},
haftmann@54249
  1240
      @{thm less_Suc_numeral}, @{thm less_numeral_Suc},
haftmann@54249
  1241
      @{thm Suc_eq_numeral}, @{thm eq_numeral_Suc},
haftmann@54249
  1242
      @{thm mult_Suc}, @{thm mult_Suc_right},
haftmann@54249
  1243
      @{thm of_nat_numeral}]
haftmann@54249
  1244
    #> Lin_Arith.set_number_of number_of)
haftmann@54249
  1245
end
wenzelm@60758
  1246
\<close>
haftmann@54249
  1247
huffman@47108
  1248
wenzelm@60758
  1249
subsubsection \<open>Simplification of arithmetic when nested to the right.\<close>
huffman@47108
  1250
huffman@47108
  1251
lemma add_numeral_left [simp]:
huffman@47108
  1252
  "numeral v + (numeral w + z) = (numeral(v + w) + z)"
haftmann@57512
  1253
  by (simp_all add: add.assoc [symmetric])
huffman@47108
  1254
huffman@47108
  1255
lemma add_neg_numeral_left [simp]:
haftmann@54489
  1256
  "numeral v + (- numeral w + y) = (sub v w + y)"
haftmann@54489
  1257
  "- numeral v + (numeral w + y) = (sub w v + y)"
haftmann@54489
  1258
  "- numeral v + (- numeral w + y) = (- numeral(v + w) + y)"
haftmann@57512
  1259
  by (simp_all add: add.assoc [symmetric])
huffman@47108
  1260
huffman@47108
  1261
lemma mult_numeral_left [simp]:
huffman@47108
  1262
  "numeral v * (numeral w * z) = (numeral(v * w) * z :: 'a::semiring_numeral)"
haftmann@54489
  1263
  "- numeral v * (numeral w * y) = (- numeral(v * w) * y :: 'b::ring_1)"
haftmann@54489
  1264
  "numeral v * (- numeral w * y) = (- numeral(v * w) * y :: 'b::ring_1)"
haftmann@54489
  1265
  "- numeral v * (- numeral w * y) = (numeral(v * w) * y :: 'b::ring_1)"
haftmann@57512
  1266
  by (simp_all add: mult.assoc [symmetric])
huffman@47108
  1267
huffman@47108
  1268
hide_const (open) One Bit0 Bit1 BitM inc pow sqr sub dbl dbl_inc dbl_dec
huffman@47108
  1269
haftmann@51143
  1270
wenzelm@60758
  1271
subsection \<open>code module namespace\<close>
huffman@47108
  1272
haftmann@52435
  1273
code_identifier
haftmann@52435
  1274
  code_module Num \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
huffman@47108
  1275
huffman@47108
  1276
end