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