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