src/HOL/ex/Numeral.thy
author wenzelm
Thu Jul 02 17:34:14 2009 +0200 (2009-07-02)
changeset 31902 862ae16a799d
parent 31029 e564767f8f78
child 31998 2c7a24f74db9
permissions -rw-r--r--
renamed NamedThmsFun to Named_Thms;
simplified/unified names of instances of Named_Thms;
haftmann@28021
     1
(*  Title:      HOL/ex/Numeral.thy
haftmann@28021
     2
    Author:     Florian Haftmann
huffman@29947
     3
*)
haftmann@28021
     4
huffman@29947
     5
header {* An experimental alternative numeral representation. *}
haftmann@28021
     6
haftmann@28021
     7
theory Numeral
haftmann@28021
     8
imports Int Inductive
haftmann@28021
     9
begin
haftmann@28021
    10
haftmann@28021
    11
subsection {* The @{text num} type *}
haftmann@28021
    12
huffman@29943
    13
datatype num = One | Dig0 num | Dig1 num
huffman@29943
    14
huffman@29943
    15
text {* Increment function for type @{typ num} *}
huffman@29943
    16
haftmann@31021
    17
primrec inc :: "num \<Rightarrow> num" where
huffman@29943
    18
  "inc One = Dig0 One"
huffman@29943
    19
| "inc (Dig0 x) = Dig1 x"
huffman@29943
    20
| "inc (Dig1 x) = Dig0 (inc x)"
huffman@29943
    21
huffman@29943
    22
text {* Converting between type @{typ num} and type @{typ nat} *}
huffman@29943
    23
haftmann@31021
    24
primrec nat_of_num :: "num \<Rightarrow> nat" where
huffman@29943
    25
  "nat_of_num One = Suc 0"
huffman@29943
    26
| "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x"
huffman@29943
    27
| "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)"
huffman@29943
    28
haftmann@31021
    29
primrec num_of_nat :: "nat \<Rightarrow> num" where
huffman@29943
    30
  "num_of_nat 0 = One"
huffman@29943
    31
| "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
huffman@29943
    32
huffman@29945
    33
lemma nat_of_num_pos: "0 < nat_of_num x"
huffman@29943
    34
  by (induct x) simp_all
huffman@29943
    35
huffman@31028
    36
lemma nat_of_num_neq_0: "nat_of_num x \<noteq> 0"
huffman@29943
    37
  by (induct x) simp_all
huffman@29943
    38
huffman@29943
    39
lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
huffman@29943
    40
  by (induct x) simp_all
huffman@29943
    41
huffman@29943
    42
lemma num_of_nat_double:
huffman@29943
    43
  "0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)"
huffman@29943
    44
  by (induct n) simp_all
huffman@29943
    45
haftmann@28021
    46
text {*
huffman@29943
    47
  Type @{typ num} is isomorphic to the strictly positive
haftmann@28021
    48
  natural numbers.
haftmann@28021
    49
*}
haftmann@28021
    50
huffman@29943
    51
lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
huffman@29945
    52
  by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
haftmann@28021
    53
huffman@29943
    54
lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
huffman@29943
    55
  by (induct n) (simp_all add: nat_of_num_inc)
haftmann@28021
    56
huffman@29942
    57
lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
huffman@29942
    58
  apply safe
huffman@29943
    59
  apply (drule arg_cong [where f=num_of_nat])
huffman@29942
    60
  apply (simp add: nat_of_num_inverse)
haftmann@28021
    61
  done
haftmann@28021
    62
huffman@29943
    63
lemma num_induct [case_names One inc]:
huffman@29943
    64
  fixes P :: "num \<Rightarrow> bool"
huffman@29943
    65
  assumes One: "P One"
huffman@29943
    66
    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
huffman@29943
    67
  shows "P x"
huffman@29943
    68
proof -
huffman@29943
    69
  obtain n where n: "Suc n = nat_of_num x"
huffman@29943
    70
    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
huffman@29943
    71
  have "P (num_of_nat (Suc n))"
huffman@29943
    72
  proof (induct n)
huffman@29943
    73
    case 0 show ?case using One by simp
haftmann@28021
    74
  next
huffman@29943
    75
    case (Suc n)
huffman@29943
    76
    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
huffman@29943
    77
    then show "P (num_of_nat (Suc (Suc n)))" by simp
haftmann@28021
    78
  qed
huffman@29943
    79
  with n show "P x"
huffman@29943
    80
    by (simp add: nat_of_num_inverse)
haftmann@28021
    81
qed
haftmann@28021
    82
haftmann@28021
    83
text {*
haftmann@28021
    84
  From now on, there are two possible models for @{typ num}:
huffman@29943
    85
  as positive naturals (rule @{text "num_induct"})
haftmann@28021
    86
  and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}).
haftmann@28021
    87
haftmann@28021
    88
  It is not entirely clear in which context it is better to use
haftmann@28021
    89
  the one or the other, or whether the construction should be reversed.
haftmann@28021
    90
*}
haftmann@28021
    91
haftmann@28021
    92
huffman@29945
    93
subsection {* Numeral operations *}
haftmann@28021
    94
haftmann@28021
    95
ML {*
wenzelm@31902
    96
structure Dig_Simps = Named_Thms
wenzelm@31902
    97
(
wenzelm@31902
    98
  val name = "numeral"
wenzelm@31902
    99
  val description = "Simplification rules for numerals"
wenzelm@31902
   100
)
haftmann@28021
   101
*}
haftmann@28021
   102
wenzelm@31902
   103
setup Dig_Simps.setup
haftmann@28021
   104
huffman@29945
   105
instantiation num :: "{plus,times,ord}"
huffman@29945
   106
begin
haftmann@28021
   107
haftmann@28021
   108
definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where
haftmann@28562
   109
  [code del]: "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
haftmann@28021
   110
haftmann@28021
   111
definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where
haftmann@28562
   112
  [code del]: "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
haftmann@28021
   113
huffman@29945
   114
definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
huffman@29945
   115
  [code del]: "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
haftmann@28021
   116
huffman@29945
   117
definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
huffman@29945
   118
  [code del]: "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
haftmann@28021
   119
huffman@29945
   120
instance ..
haftmann@28021
   121
haftmann@28021
   122
end
haftmann@28021
   123
huffman@29945
   124
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
huffman@29945
   125
  unfolding plus_num_def
huffman@29945
   126
  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
huffman@29945
   127
huffman@29945
   128
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
huffman@29945
   129
  unfolding times_num_def
huffman@29945
   130
  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
haftmann@28021
   131
huffman@29945
   132
lemma Dig_plus [numeral, simp, code]:
huffman@29945
   133
  "One + One = Dig0 One"
huffman@29945
   134
  "One + Dig0 m = Dig1 m"
huffman@29945
   135
  "One + Dig1 m = Dig0 (m + One)"
huffman@29945
   136
  "Dig0 n + One = Dig1 n"
huffman@29945
   137
  "Dig0 n + Dig0 m = Dig0 (n + m)"
huffman@29945
   138
  "Dig0 n + Dig1 m = Dig1 (n + m)"
huffman@29945
   139
  "Dig1 n + One = Dig0 (n + One)"
huffman@29945
   140
  "Dig1 n + Dig0 m = Dig1 (n + m)"
huffman@29945
   141
  "Dig1 n + Dig1 m = Dig0 (n + m + One)"
huffman@29945
   142
  by (simp_all add: num_eq_iff nat_of_num_add)
haftmann@28021
   143
huffman@29945
   144
lemma Dig_times [numeral, simp, code]:
huffman@29945
   145
  "One * One = One"
huffman@29945
   146
  "One * Dig0 n = Dig0 n"
huffman@29945
   147
  "One * Dig1 n = Dig1 n"
huffman@29945
   148
  "Dig0 n * One = Dig0 n"
huffman@29945
   149
  "Dig0 n * Dig0 m = Dig0 (n * Dig0 m)"
huffman@29945
   150
  "Dig0 n * Dig1 m = Dig0 (n * Dig1 m)"
huffman@29945
   151
  "Dig1 n * One = Dig1 n"
huffman@29945
   152
  "Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)"
huffman@29945
   153
  "Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)"
huffman@29945
   154
  by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult
huffman@29945
   155
                    left_distrib right_distrib)
haftmann@28021
   156
huffman@29991
   157
lemma Dig_eq:
huffman@29991
   158
  "One = One \<longleftrightarrow> True"
huffman@29991
   159
  "One = Dig0 n \<longleftrightarrow> False"
huffman@29991
   160
  "One = Dig1 n \<longleftrightarrow> False"
huffman@29991
   161
  "Dig0 m = One \<longleftrightarrow> False"
huffman@29991
   162
  "Dig1 m = One \<longleftrightarrow> False"
huffman@29991
   163
  "Dig0 m = Dig0 n \<longleftrightarrow> m = n"
huffman@29991
   164
  "Dig0 m = Dig1 n \<longleftrightarrow> False"
huffman@29991
   165
  "Dig1 m = Dig0 n \<longleftrightarrow> False"
huffman@29991
   166
  "Dig1 m = Dig1 n \<longleftrightarrow> m = n"
huffman@29991
   167
  by simp_all
huffman@29991
   168
huffman@29945
   169
lemma less_eq_num_code [numeral, simp, code]:
huffman@29945
   170
  "One \<le> n \<longleftrightarrow> True"
huffman@29945
   171
  "Dig0 m \<le> One \<longleftrightarrow> False"
huffman@29945
   172
  "Dig1 m \<le> One \<longleftrightarrow> False"
huffman@29945
   173
  "Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n"
huffman@29945
   174
  "Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
huffman@29945
   175
  "Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
huffman@29945
   176
  "Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n"
huffman@29945
   177
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
huffman@29945
   178
  by (auto simp add: less_eq_num_def less_num_def)
huffman@29945
   179
huffman@29945
   180
lemma less_num_code [numeral, simp, code]:
huffman@29945
   181
  "m < One \<longleftrightarrow> False"
huffman@29945
   182
  "One < One \<longleftrightarrow> False"
huffman@29945
   183
  "One < Dig0 n \<longleftrightarrow> True"
huffman@29945
   184
  "One < Dig1 n \<longleftrightarrow> True"
huffman@29945
   185
  "Dig0 m < Dig0 n \<longleftrightarrow> m < n"
huffman@29945
   186
  "Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n"
huffman@29945
   187
  "Dig1 m < Dig1 n \<longleftrightarrow> m < n"
huffman@29945
   188
  "Dig1 m < Dig0 n \<longleftrightarrow> m < n"
huffman@29945
   189
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
huffman@29945
   190
  by (auto simp add: less_eq_num_def less_num_def)
huffman@29945
   191
huffman@29945
   192
text {* Rules using @{text One} and @{text inc} as constructors *}
haftmann@28021
   193
huffman@29945
   194
lemma add_One: "x + One = inc x"
huffman@29945
   195
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
huffman@29945
   196
huffman@29945
   197
lemma add_inc: "x + inc y = inc (x + y)"
huffman@29945
   198
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
huffman@29945
   199
huffman@29945
   200
lemma mult_One: "x * One = x"
huffman@29945
   201
  by (simp add: num_eq_iff nat_of_num_mult)
huffman@29945
   202
huffman@29945
   203
lemma mult_inc: "x * inc y = x * y + x"
huffman@29945
   204
  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
huffman@29945
   205
huffman@29945
   206
text {* A double-and-decrement function *}
haftmann@28021
   207
huffman@29945
   208
primrec DigM :: "num \<Rightarrow> num" where
huffman@29945
   209
  "DigM One = One"
huffman@29945
   210
  | "DigM (Dig0 n) = Dig1 (DigM n)"
huffman@29945
   211
  | "DigM (Dig1 n) = Dig1 (Dig0 n)"
haftmann@28021
   212
huffman@29945
   213
lemma DigM_plus_one: "DigM n + One = Dig0 n"
huffman@29945
   214
  by (induct n) simp_all
huffman@29945
   215
huffman@29945
   216
lemma add_One_commute: "One + n = n + One"
huffman@29945
   217
  by (induct n) simp_all
huffman@29945
   218
huffman@29945
   219
lemma one_plus_DigM: "One + DigM n = Dig0 n"
huffman@29945
   220
  unfolding add_One_commute DigM_plus_one ..
haftmann@28021
   221
huffman@29954
   222
text {* Squaring and exponentiation *}
huffman@29947
   223
huffman@29947
   224
primrec square :: "num \<Rightarrow> num" where
huffman@29947
   225
  "square One = One"
huffman@29947
   226
| "square (Dig0 n) = Dig0 (Dig0 (square n))"
huffman@29947
   227
| "square (Dig1 n) = Dig1 (Dig0 (square n + n))"
huffman@29947
   228
huffman@29954
   229
primrec pow :: "num \<Rightarrow> num \<Rightarrow> num"
huffman@29954
   230
where
huffman@29954
   231
  "pow x One = x"
huffman@29954
   232
| "pow x (Dig0 y) = square (pow x y)"
huffman@29954
   233
| "pow x (Dig1 y) = x * square (pow x y)"
huffman@29947
   234
haftmann@28021
   235
haftmann@28021
   236
subsection {* Binary numerals *}
haftmann@28021
   237
haftmann@28021
   238
text {*
haftmann@28021
   239
  We embed binary representations into a generic algebraic
haftmann@29934
   240
  structure using @{text of_num}.
haftmann@28021
   241
*}
haftmann@28021
   242
haftmann@28021
   243
class semiring_numeral = semiring + monoid_mult
haftmann@28021
   244
begin
haftmann@28021
   245
haftmann@28021
   246
primrec of_num :: "num \<Rightarrow> 'a" where
huffman@31028
   247
  of_num_One [numeral]: "of_num One = 1"
haftmann@28021
   248
  | "of_num (Dig0 n) = of_num n + of_num n"
haftmann@28021
   249
  | "of_num (Dig1 n) = of_num n + of_num n + 1"
haftmann@28021
   250
huffman@29943
   251
lemma of_num_inc: "of_num (inc x) = of_num x + 1"
huffman@29943
   252
  by (induct x) (simp_all add: add_ac)
huffman@29943
   253
huffman@31028
   254
lemma of_num_add: "of_num (m + n) = of_num m + of_num n"
huffman@31028
   255
  apply (induct n rule: num_induct)
huffman@31028
   256
  apply (simp_all add: add_One add_inc of_num_inc add_ac)
huffman@31028
   257
  done
huffman@31028
   258
huffman@31028
   259
lemma of_num_mult: "of_num (m * n) = of_num m * of_num n"
huffman@31028
   260
  apply (induct n rule: num_induct)
huffman@31028
   261
  apply (simp add: mult_One)
huffman@31028
   262
  apply (simp add: mult_inc of_num_add of_num_inc right_distrib)
huffman@31028
   263
  done
huffman@31028
   264
haftmann@28021
   265
declare of_num.simps [simp del]
haftmann@28021
   266
haftmann@28021
   267
end
haftmann@28021
   268
haftmann@28021
   269
text {*
haftmann@28021
   270
  ML stuff and syntax.
haftmann@28021
   271
*}
haftmann@28021
   272
haftmann@28021
   273
ML {*
huffman@31027
   274
fun mk_num k =
huffman@31027
   275
  if k > 1 then
huffman@31027
   276
    let
huffman@31027
   277
      val (l, b) = Integer.div_mod k 2;
huffman@31027
   278
      val bit = (if b = 0 then @{term Dig0} else @{term Dig1});
huffman@31027
   279
    in bit $ (mk_num l) end
huffman@31027
   280
  else if k = 1 then @{term One}
huffman@31027
   281
  else error ("mk_num " ^ string_of_int k);
haftmann@28021
   282
huffman@29942
   283
fun dest_num @{term One} = 1
haftmann@28021
   284
  | dest_num (@{term Dig0} $ n) = 2 * dest_num n
huffman@31027
   285
  | dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1
huffman@31027
   286
  | dest_num t = raise TERM ("dest_num", [t]);
haftmann@28021
   287
haftmann@28021
   288
(*FIXME these have to gain proper context via morphisms phi*)
haftmann@28021
   289
haftmann@28021
   290
fun mk_numeral T k = Const (@{const_name of_num}, @{typ num} --> T)
haftmann@28021
   291
  $ mk_num k
haftmann@28021
   292
haftmann@28021
   293
fun dest_numeral (Const (@{const_name of_num}, Type ("fun", [@{typ num}, T])) $ t) =
haftmann@28021
   294
  (T, dest_num t)
haftmann@28021
   295
*}
haftmann@28021
   296
haftmann@28021
   297
syntax
haftmann@28021
   298
  "_Numerals" :: "xnum \<Rightarrow> 'a"    ("_")
haftmann@28021
   299
haftmann@28021
   300
parse_translation {*
haftmann@28021
   301
let
haftmann@28021
   302
  fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2)
huffman@29942
   303
     of (0, 1) => Const (@{const_name One}, dummyT)
haftmann@28021
   304
      | (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n
haftmann@28021
   305
      | (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n
haftmann@28021
   306
    else raise Match;
haftmann@28021
   307
  fun numeral_tr [Free (num, _)] =
haftmann@28021
   308
        let
haftmann@28021
   309
          val {leading_zeros, value, ...} = Syntax.read_xnum num;
haftmann@28021
   310
          val _ = leading_zeros = 0 andalso value > 0
haftmann@28021
   311
            orelse error ("Bad numeral: " ^ num);
haftmann@28021
   312
        in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end
haftmann@28021
   313
    | numeral_tr ts = raise TERM ("numeral_tr", ts);
haftmann@28021
   314
in [("_Numerals", numeral_tr)] end
haftmann@28021
   315
*}
haftmann@28021
   316
haftmann@28021
   317
typed_print_translation {*
haftmann@28021
   318
let
haftmann@28021
   319
  fun dig b n = b + 2 * n; 
haftmann@28021
   320
  fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) =
haftmann@28021
   321
        dig 0 (int_of_num' n)
haftmann@28021
   322
    | int_of_num' (Const (@{const_syntax Dig1}, _) $ n) =
haftmann@28021
   323
        dig 1 (int_of_num' n)
huffman@29942
   324
    | int_of_num' (Const (@{const_syntax One}, _)) = 1;
haftmann@28021
   325
  fun num_tr' show_sorts T [n] =
haftmann@28021
   326
    let
haftmann@28021
   327
      val k = int_of_num' n;
haftmann@28021
   328
      val t' = Syntax.const "_Numerals" $ Syntax.free ("#" ^ string_of_int k);
haftmann@28021
   329
    in case T
haftmann@28021
   330
     of Type ("fun", [_, T']) =>
haftmann@28021
   331
         if not (! show_types) andalso can Term.dest_Type T' then t'
haftmann@28021
   332
         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T'
haftmann@28021
   333
      | T' => if T' = dummyT then t' else raise Match
haftmann@28021
   334
    end;
haftmann@28021
   335
in [(@{const_syntax of_num}, num_tr')] end
haftmann@28021
   336
*}
haftmann@28021
   337
huffman@29945
   338
subsection {* Class-specific numeral rules *}
haftmann@28021
   339
haftmann@28021
   340
text {*
haftmann@28021
   341
  @{const of_num} is a morphism.
haftmann@28021
   342
*}
haftmann@28021
   343
huffman@29945
   344
subsubsection {* Class @{text semiring_numeral} *}
huffman@29945
   345
haftmann@28021
   346
context semiring_numeral
haftmann@28021
   347
begin
haftmann@28021
   348
huffman@29943
   349
abbreviation "Num1 \<equiv> of_num One"
haftmann@28021
   350
haftmann@28021
   351
text {*
haftmann@28021
   352
  Alas, there is still the duplication of @{term 1},
haftmann@28021
   353
  thought the duplicated @{term 0} has disappeared.
haftmann@28021
   354
  We could get rid of it by replacing the constructor
haftmann@28021
   355
  @{term 1} in @{typ num} by two constructors
haftmann@28021
   356
  @{text two} and @{text three}, resulting in a further
haftmann@28021
   357
  blow-up.  But it could be worth the effort.
haftmann@28021
   358
*}
haftmann@28021
   359
haftmann@28021
   360
lemma of_num_plus_one [numeral]:
huffman@29942
   361
  "of_num n + 1 = of_num (n + One)"
huffman@31028
   362
  by (simp only: of_num_add of_num_One)
haftmann@28021
   363
haftmann@28021
   364
lemma of_num_one_plus [numeral]:
huffman@31028
   365
  "1 + of_num n = of_num (One + n)"
huffman@31028
   366
  by (simp only: of_num_add of_num_One)
haftmann@28021
   367
haftmann@28021
   368
lemma of_num_plus [numeral]:
haftmann@28021
   369
  "of_num m + of_num n = of_num (m + n)"
huffman@31028
   370
  unfolding of_num_add ..
haftmann@28021
   371
haftmann@28021
   372
lemma of_num_times_one [numeral]:
haftmann@28021
   373
  "of_num n * 1 = of_num n"
haftmann@28021
   374
  by simp
haftmann@28021
   375
haftmann@28021
   376
lemma of_num_one_times [numeral]:
haftmann@28021
   377
  "1 * of_num n = of_num n"
haftmann@28021
   378
  by simp
haftmann@28021
   379
haftmann@28021
   380
lemma of_num_times [numeral]:
haftmann@28021
   381
  "of_num m * of_num n = of_num (m * n)"
huffman@31028
   382
  unfolding of_num_mult ..
haftmann@28021
   383
haftmann@28021
   384
end
haftmann@28021
   385
huffman@29945
   386
subsubsection {*
huffman@29947
   387
  Structures with a zero: class @{text semiring_1}
haftmann@28021
   388
*}
haftmann@28021
   389
haftmann@28021
   390
context semiring_1
haftmann@28021
   391
begin
haftmann@28021
   392
haftmann@28021
   393
subclass semiring_numeral ..
haftmann@28021
   394
haftmann@28021
   395
lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n"
haftmann@28021
   396
  by (induct n)
haftmann@28021
   397
    (simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac)
haftmann@28021
   398
haftmann@28021
   399
declare of_nat_1 [numeral]
haftmann@28021
   400
haftmann@28021
   401
lemma Dig_plus_zero [numeral]:
haftmann@28021
   402
  "0 + 1 = 1"
haftmann@28021
   403
  "0 + of_num n = of_num n"
haftmann@28021
   404
  "1 + 0 = 1"
haftmann@28021
   405
  "of_num n + 0 = of_num n"
haftmann@28021
   406
  by simp_all
haftmann@28021
   407
haftmann@28021
   408
lemma Dig_times_zero [numeral]:
haftmann@28021
   409
  "0 * 1 = 0"
haftmann@28021
   410
  "0 * of_num n = 0"
haftmann@28021
   411
  "1 * 0 = 0"
haftmann@28021
   412
  "of_num n * 0 = 0"
haftmann@28021
   413
  by simp_all
haftmann@28021
   414
haftmann@28021
   415
end
haftmann@28021
   416
haftmann@28021
   417
lemma nat_of_num_of_num: "nat_of_num = of_num"
haftmann@28021
   418
proof
haftmann@28021
   419
  fix n
huffman@29943
   420
  have "of_num n = nat_of_num n"
huffman@29943
   421
    by (induct n) (simp_all add: of_num.simps)
haftmann@28021
   422
  then show "nat_of_num n = of_num n" by simp
haftmann@28021
   423
qed
haftmann@28021
   424
huffman@29945
   425
subsubsection {*
huffman@29945
   426
  Equality: class @{text semiring_char_0}
haftmann@28021
   427
*}
haftmann@28021
   428
haftmann@28021
   429
context semiring_char_0
haftmann@28021
   430
begin
haftmann@28021
   431
huffman@31028
   432
lemma of_num_eq_iff [numeral]: "of_num m = of_num n \<longleftrightarrow> m = n"
haftmann@28021
   433
  unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric]
huffman@29943
   434
    of_nat_eq_iff num_eq_iff ..
haftmann@28021
   435
huffman@31028
   436
lemma of_num_eq_one_iff [numeral]: "of_num n = 1 \<longleftrightarrow> n = One"
huffman@31028
   437
  using of_num_eq_iff [of n One] by (simp add: of_num_One)
haftmann@28021
   438
huffman@31028
   439
lemma one_eq_of_num_iff [numeral]: "1 = of_num n \<longleftrightarrow> One = n"
huffman@31028
   440
  using of_num_eq_iff [of One n] by (simp add: of_num_One)
haftmann@28021
   441
haftmann@28021
   442
end
haftmann@28021
   443
huffman@29945
   444
subsubsection {*
huffman@29945
   445
  Comparisons: class @{text ordered_semidom}
haftmann@28021
   446
*}
haftmann@28021
   447
huffman@29945
   448
text {*  Could be perhaps more general than here. *}
huffman@29945
   449
haftmann@28021
   450
context ordered_semidom
haftmann@28021
   451
begin
haftmann@28021
   452
huffman@29991
   453
lemma of_num_pos [numeral]: "0 < of_num n"
huffman@29991
   454
  by (induct n) (simp_all add: of_num.simps add_pos_pos)
huffman@29991
   455
haftmann@28021
   456
lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n"
haftmann@28021
   457
proof -
haftmann@28021
   458
  have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n"
haftmann@28021
   459
    unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff ..
haftmann@28021
   460
  then show ?thesis by (simp add: of_nat_of_num)
haftmann@28021
   461
qed
haftmann@28021
   462
huffman@31028
   463
lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n \<le> One"
huffman@31028
   464
  using of_num_less_eq_iff [of n One] by (simp add: of_num_One)
haftmann@28021
   465
haftmann@28021
   466
lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n"
huffman@31028
   467
  using of_num_less_eq_iff [of One n] by (simp add: of_num_One)
haftmann@28021
   468
haftmann@28021
   469
lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n"
haftmann@28021
   470
proof -
haftmann@28021
   471
  have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n"
haftmann@28021
   472
    unfolding less_num_def nat_of_num_of_num of_nat_less_iff ..
haftmann@28021
   473
  then show ?thesis by (simp add: of_nat_of_num)
haftmann@28021
   474
qed
haftmann@28021
   475
haftmann@28021
   476
lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1"
huffman@31028
   477
  using of_num_less_iff [of n One] by (simp add: of_num_One)
haftmann@28021
   478
huffman@31028
   479
lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> One < n"
huffman@31028
   480
  using of_num_less_iff [of One n] by (simp add: of_num_One)
haftmann@28021
   481
huffman@29991
   482
lemma of_num_nonneg [numeral]: "0 \<le> of_num n"
huffman@29991
   483
  by (induct n) (simp_all add: of_num.simps add_nonneg_nonneg)
huffman@29991
   484
huffman@29991
   485
lemma of_num_less_zero_iff [numeral]: "\<not> of_num n < 0"
huffman@29991
   486
  by (simp add: not_less of_num_nonneg)
huffman@29991
   487
huffman@29991
   488
lemma of_num_le_zero_iff [numeral]: "\<not> of_num n \<le> 0"
huffman@29991
   489
  by (simp add: not_le of_num_pos)
huffman@29991
   490
huffman@29991
   491
end
huffman@29991
   492
huffman@29991
   493
context ordered_idom
huffman@29991
   494
begin
huffman@29991
   495
huffman@30792
   496
lemma minus_of_num_less_of_num_iff: "- of_num m < of_num n"
huffman@29991
   497
proof -
huffman@29991
   498
  have "- of_num m < 0" by (simp add: of_num_pos)
huffman@29991
   499
  also have "0 < of_num n" by (simp add: of_num_pos)
huffman@29991
   500
  finally show ?thesis .
huffman@29991
   501
qed
huffman@29991
   502
huffman@30792
   503
lemma minus_of_num_less_one_iff: "- of_num n < 1"
huffman@31028
   504
  using minus_of_num_less_of_num_iff [of n One] by (simp add: of_num_One)
huffman@29991
   505
huffman@30792
   506
lemma minus_one_less_of_num_iff: "- 1 < of_num n"
huffman@31028
   507
  using minus_of_num_less_of_num_iff [of One n] by (simp add: of_num_One)
huffman@29991
   508
huffman@30792
   509
lemma minus_one_less_one_iff: "- 1 < 1"
huffman@31028
   510
  using minus_of_num_less_of_num_iff [of One One] by (simp add: of_num_One)
huffman@30792
   511
huffman@30792
   512
lemma minus_of_num_le_of_num_iff: "- of_num m \<le> of_num n"
huffman@29991
   513
  by (simp add: less_imp_le minus_of_num_less_of_num_iff)
huffman@29991
   514
huffman@30792
   515
lemma minus_of_num_le_one_iff: "- of_num n \<le> 1"
huffman@29991
   516
  by (simp add: less_imp_le minus_of_num_less_one_iff)
huffman@29991
   517
huffman@30792
   518
lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n"
huffman@29991
   519
  by (simp add: less_imp_le minus_one_less_of_num_iff)
huffman@29991
   520
huffman@30792
   521
lemma minus_one_le_one_iff: "- 1 \<le> 1"
huffman@30792
   522
  by (simp add: less_imp_le minus_one_less_one_iff)
huffman@30792
   523
huffman@30792
   524
lemma of_num_le_minus_of_num_iff: "\<not> of_num m \<le> - of_num n"
huffman@29991
   525
  by (simp add: not_le minus_of_num_less_of_num_iff)
huffman@29991
   526
huffman@30792
   527
lemma one_le_minus_of_num_iff: "\<not> 1 \<le> - of_num n"
huffman@29991
   528
  by (simp add: not_le minus_of_num_less_one_iff)
huffman@29991
   529
huffman@30792
   530
lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1"
huffman@29991
   531
  by (simp add: not_le minus_one_less_of_num_iff)
huffman@29991
   532
huffman@30792
   533
lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1"
huffman@30792
   534
  by (simp add: not_le minus_one_less_one_iff)
huffman@30792
   535
huffman@30792
   536
lemma of_num_less_minus_of_num_iff: "\<not> of_num m < - of_num n"
huffman@29991
   537
  by (simp add: not_less minus_of_num_le_of_num_iff)
huffman@29991
   538
huffman@30792
   539
lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n"
huffman@29991
   540
  by (simp add: not_less minus_of_num_le_one_iff)
huffman@29991
   541
huffman@30792
   542
lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1"
huffman@29991
   543
  by (simp add: not_less minus_one_le_of_num_iff)
huffman@29991
   544
huffman@30792
   545
lemma one_less_minus_one_iff: "\<not> 1 < - 1"
huffman@30792
   546
  by (simp add: not_less minus_one_le_one_iff)
huffman@30792
   547
huffman@30792
   548
lemmas le_signed_numeral_special [numeral] =
huffman@30792
   549
  minus_of_num_le_of_num_iff
huffman@30792
   550
  minus_of_num_le_one_iff
huffman@30792
   551
  minus_one_le_of_num_iff
huffman@30792
   552
  minus_one_le_one_iff
huffman@30792
   553
  of_num_le_minus_of_num_iff
huffman@30792
   554
  one_le_minus_of_num_iff
huffman@30792
   555
  of_num_le_minus_one_iff
huffman@30792
   556
  one_le_minus_one_iff
huffman@30792
   557
huffman@30792
   558
lemmas less_signed_numeral_special [numeral] =
huffman@30792
   559
  minus_of_num_less_of_num_iff
huffman@30792
   560
  minus_of_num_less_one_iff
huffman@30792
   561
  minus_one_less_of_num_iff
huffman@30792
   562
  minus_one_less_one_iff
huffman@30792
   563
  of_num_less_minus_of_num_iff
huffman@30792
   564
  one_less_minus_of_num_iff
huffman@30792
   565
  of_num_less_minus_one_iff
huffman@30792
   566
  one_less_minus_one_iff
huffman@30792
   567
haftmann@28021
   568
end
haftmann@28021
   569
huffman@29945
   570
subsubsection {*
huffman@29947
   571
  Structures with subtraction: class @{text semiring_1_minus}
haftmann@28021
   572
*}
haftmann@28021
   573
haftmann@28021
   574
class semiring_minus = semiring + minus + zero +
haftmann@28021
   575
  assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a"
haftmann@28021
   576
  assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a"
haftmann@28021
   577
begin
haftmann@28021
   578
haftmann@28021
   579
lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b"
haftmann@28021
   580
  by (simp add: add_ac minus_inverts_plus1 [of b a])
haftmann@28021
   581
haftmann@28021
   582
lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b"
haftmann@28021
   583
  by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a])
haftmann@28021
   584
haftmann@28021
   585
end
haftmann@28021
   586
haftmann@28021
   587
class semiring_1_minus = semiring_1 + semiring_minus
haftmann@28021
   588
begin
haftmann@28021
   589
haftmann@28021
   590
lemma Dig_of_num_pos:
haftmann@28021
   591
  assumes "k + n = m"
haftmann@28021
   592
  shows "of_num m - of_num n = of_num k"
haftmann@28021
   593
  using assms by (simp add: of_num_plus minus_inverts_plus1)
haftmann@28021
   594
haftmann@28021
   595
lemma Dig_of_num_zero:
haftmann@28021
   596
  shows "of_num n - of_num n = 0"
haftmann@28021
   597
  by (rule minus_inverts_plus1) simp
haftmann@28021
   598
haftmann@28021
   599
lemma Dig_of_num_neg:
haftmann@28021
   600
  assumes "k + m = n"
haftmann@28021
   601
  shows "of_num m - of_num n = 0 - of_num k"
haftmann@28021
   602
  by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms)
haftmann@28021
   603
haftmann@28021
   604
lemmas Dig_plus_eval =
huffman@29942
   605
  of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject
haftmann@28021
   606
haftmann@28021
   607
simproc_setup numeral_minus ("of_num m - of_num n") = {*
haftmann@28021
   608
  let
haftmann@28021
   609
    (*TODO proper implicit use of morphism via pattern antiquotations*)
haftmann@28021
   610
    fun cdest_of_num ct = (snd o split_last o snd o Drule.strip_comb) ct;
haftmann@28021
   611
    fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n);
haftmann@28021
   612
    fun attach_num ct = (dest_num (Thm.term_of ct), ct);
haftmann@28021
   613
    fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t;
haftmann@28021
   614
    val simplify = MetaSimplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval});
haftmann@28021
   615
    fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq} OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"},
haftmann@28021
   616
      [Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]];
haftmann@28021
   617
  in fn phi => fn _ => fn ct => case try cdifference ct
haftmann@28021
   618
   of NONE => (NONE)
haftmann@28021
   619
    | SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0
haftmann@28021
   620
        then MetaSimplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct
haftmann@28021
   621
        else mk_meta_eq (let
haftmann@28021
   622
          val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j));
haftmann@28021
   623
        in
haftmann@28021
   624
          (if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck]
haftmann@28021
   625
          else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl])
haftmann@28021
   626
        end) end)
haftmann@28021
   627
  end
haftmann@28021
   628
*}
haftmann@28021
   629
haftmann@28021
   630
lemma Dig_of_num_minus_zero [numeral]:
haftmann@28021
   631
  "of_num n - 0 = of_num n"
haftmann@28021
   632
  by (simp add: minus_inverts_plus1)
haftmann@28021
   633
haftmann@28021
   634
lemma Dig_one_minus_zero [numeral]:
haftmann@28021
   635
  "1 - 0 = 1"
haftmann@28021
   636
  by (simp add: minus_inverts_plus1)
haftmann@28021
   637
haftmann@28021
   638
lemma Dig_one_minus_one [numeral]:
haftmann@28021
   639
  "1 - 1 = 0"
haftmann@28021
   640
  by (simp add: minus_inverts_plus1)
haftmann@28021
   641
haftmann@28021
   642
lemma Dig_of_num_minus_one [numeral]:
huffman@29941
   643
  "of_num (Dig0 n) - 1 = of_num (DigM n)"
haftmann@28021
   644
  "of_num (Dig1 n) - 1 = of_num (Dig0 n)"
huffman@29941
   645
  by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
haftmann@28021
   646
haftmann@28021
   647
lemma Dig_one_minus_of_num [numeral]:
huffman@29941
   648
  "1 - of_num (Dig0 n) = 0 - of_num (DigM n)"
haftmann@28021
   649
  "1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)"
huffman@29941
   650
  by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
haftmann@28021
   651
haftmann@28021
   652
end
haftmann@28021
   653
huffman@29945
   654
subsubsection {*
huffman@29947
   655
  Structures with negation: class @{text ring_1}
huffman@29945
   656
*}
huffman@29945
   657
haftmann@28021
   658
context ring_1
haftmann@28021
   659
begin
haftmann@28021
   660
haftmann@28021
   661
subclass semiring_1_minus
nipkow@29667
   662
  proof qed (simp_all add: algebra_simps)
haftmann@28021
   663
haftmann@28021
   664
lemma Dig_zero_minus_of_num [numeral]:
haftmann@28021
   665
  "0 - of_num n = - of_num n"
haftmann@28021
   666
  by simp
haftmann@28021
   667
haftmann@28021
   668
lemma Dig_zero_minus_one [numeral]:
haftmann@28021
   669
  "0 - 1 = - 1"
haftmann@28021
   670
  by simp
haftmann@28021
   671
haftmann@28021
   672
lemma Dig_uminus_uminus [numeral]:
haftmann@28021
   673
  "- (- of_num n) = of_num n"
haftmann@28021
   674
  by simp
haftmann@28021
   675
haftmann@28021
   676
lemma Dig_plus_uminus [numeral]:
haftmann@28021
   677
  "of_num m + - of_num n = of_num m - of_num n"
haftmann@28021
   678
  "- of_num m + of_num n = of_num n - of_num m"
haftmann@28021
   679
  "- of_num m + - of_num n = - (of_num m + of_num n)"
haftmann@28021
   680
  "of_num m - - of_num n = of_num m + of_num n"
haftmann@28021
   681
  "- of_num m - of_num n = - (of_num m + of_num n)"
haftmann@28021
   682
  "- of_num m - - of_num n = of_num n - of_num m"
haftmann@28021
   683
  by (simp_all add: diff_minus add_commute)
haftmann@28021
   684
haftmann@28021
   685
lemma Dig_times_uminus [numeral]:
haftmann@28021
   686
  "- of_num n * of_num m = - (of_num n * of_num m)"
haftmann@28021
   687
  "of_num n * - of_num m = - (of_num n * of_num m)"
haftmann@28021
   688
  "- of_num n * - of_num m = of_num n * of_num m"
huffman@31028
   689
  by simp_all
haftmann@28021
   690
haftmann@28021
   691
lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n"
haftmann@28021
   692
by (induct n)
haftmann@28021
   693
  (simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all)
haftmann@28021
   694
haftmann@28021
   695
declare of_int_1 [numeral]
haftmann@28021
   696
haftmann@28021
   697
end
haftmann@28021
   698
huffman@29945
   699
subsubsection {*
huffman@29954
   700
  Structures with exponentiation
huffman@29954
   701
*}
huffman@29954
   702
huffman@29954
   703
lemma of_num_square: "of_num (square x) = of_num x * of_num x"
huffman@29954
   704
by (induct x)
huffman@31028
   705
   (simp_all add: of_num.simps of_num_add algebra_simps)
huffman@29954
   706
huffman@31028
   707
lemma of_num_pow: "of_num (pow x y) = of_num x ^ of_num y"
huffman@29954
   708
by (induct y)
huffman@31028
   709
   (simp_all add: of_num.simps of_num_square of_num_mult power_add)
huffman@29954
   710
huffman@31028
   711
lemma power_of_num [numeral]: "of_num x ^ of_num y = of_num (pow x y)"
huffman@31028
   712
  unfolding of_num_pow ..
huffman@29954
   713
huffman@29954
   714
lemma power_zero_of_num [numeral]:
huffman@31029
   715
  "0 ^ of_num n = (0::'a::semiring_1)"
huffman@29954
   716
  using of_num_pos [where n=n and ?'a=nat]
huffman@29954
   717
  by (simp add: power_0_left)
huffman@29954
   718
huffman@29954
   719
lemma power_minus_Dig0 [numeral]:
huffman@31029
   720
  fixes x :: "'a::ring_1"
huffman@29954
   721
  shows "(- x) ^ of_num (Dig0 n) = x ^ of_num (Dig0 n)"
huffman@31028
   722
  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
huffman@29954
   723
huffman@29954
   724
lemma power_minus_Dig1 [numeral]:
huffman@31029
   725
  fixes x :: "'a::ring_1"
huffman@29954
   726
  shows "(- x) ^ of_num (Dig1 n) = - (x ^ of_num (Dig1 n))"
huffman@31028
   727
  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
huffman@29954
   728
huffman@29954
   729
declare power_one [numeral]
huffman@29954
   730
huffman@29954
   731
huffman@29954
   732
subsubsection {*
haftmann@28021
   733
  Greetings to @{typ nat}.
haftmann@28021
   734
*}
haftmann@28021
   735
haftmann@28021
   736
instance nat :: semiring_1_minus proof qed simp_all
haftmann@28021
   737
huffman@29942
   738
lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)"
haftmann@28021
   739
  unfolding of_num_plus_one [symmetric] by simp
haftmann@28021
   740
haftmann@28021
   741
lemma nat_number:
haftmann@28021
   742
  "1 = Suc 0"
huffman@29942
   743
  "of_num One = Suc 0"
huffman@29941
   744
  "of_num (Dig0 n) = Suc (of_num (DigM n))"
haftmann@28021
   745
  "of_num (Dig1 n) = Suc (of_num (Dig0 n))"
huffman@29941
   746
  by (simp_all add: of_num.simps DigM_plus_one Suc_of_num)
haftmann@28021
   747
haftmann@28021
   748
declare diff_0_eq_0 [numeral]
haftmann@28021
   749
haftmann@28021
   750
haftmann@28021
   751
subsection {* Code generator setup for @{typ int} *}
haftmann@28021
   752
haftmann@28021
   753
definition Pls :: "num \<Rightarrow> int" where
haftmann@28021
   754
  [simp, code post]: "Pls n = of_num n"
haftmann@28021
   755
haftmann@28021
   756
definition Mns :: "num \<Rightarrow> int" where
haftmann@28021
   757
  [simp, code post]: "Mns n = - of_num n"
haftmann@28021
   758
haftmann@28021
   759
code_datatype "0::int" Pls Mns
haftmann@28021
   760
haftmann@28021
   761
lemmas [code inline] = Pls_def [symmetric] Mns_def [symmetric]
haftmann@28021
   762
haftmann@28021
   763
definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
haftmann@28562
   764
  [simp, code del]: "sub m n = (of_num m - of_num n)"
haftmann@28021
   765
haftmann@28021
   766
definition dup :: "int \<Rightarrow> int" where
haftmann@28562
   767
  [code del]: "dup k = 2 * k"
haftmann@28021
   768
haftmann@28021
   769
lemma Dig_sub [code]:
huffman@29942
   770
  "sub One One = 0"
huffman@29942
   771
  "sub (Dig0 m) One = of_num (DigM m)"
huffman@29942
   772
  "sub (Dig1 m) One = of_num (Dig0 m)"
huffman@29942
   773
  "sub One (Dig0 n) = - of_num (DigM n)"
huffman@29942
   774
  "sub One (Dig1 n) = - of_num (Dig0 n)"
haftmann@28021
   775
  "sub (Dig0 m) (Dig0 n) = dup (sub m n)"
haftmann@28021
   776
  "sub (Dig1 m) (Dig1 n) = dup (sub m n)"
haftmann@28021
   777
  "sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1"
haftmann@28021
   778
  "sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1"
nipkow@29667
   779
  apply (simp_all add: dup_def algebra_simps)
huffman@29941
   780
  apply (simp_all add: of_num_plus one_plus_DigM)[4]
haftmann@28021
   781
  apply (simp_all add: of_num.simps)
haftmann@28021
   782
  done
haftmann@28021
   783
haftmann@28021
   784
lemma dup_code [code]:
haftmann@28021
   785
  "dup 0 = 0"
haftmann@28021
   786
  "dup (Pls n) = Pls (Dig0 n)"
haftmann@28021
   787
  "dup (Mns n) = Mns (Dig0 n)"
haftmann@28021
   788
  by (simp_all add: dup_def of_num.simps)
haftmann@28021
   789
  
haftmann@28562
   790
lemma [code, code del]:
haftmann@28021
   791
  "(1 :: int) = 1"
haftmann@28021
   792
  "(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +"
haftmann@28021
   793
  "(uminus :: int \<Rightarrow> int) = uminus"
haftmann@28021
   794
  "(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -"
haftmann@28021
   795
  "(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *"
haftmann@28367
   796
  "(eq_class.eq :: int \<Rightarrow> int \<Rightarrow> bool) = eq_class.eq"
haftmann@28021
   797
  "(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>"
haftmann@28021
   798
  "(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <"
haftmann@28021
   799
  by rule+
haftmann@28021
   800
haftmann@28021
   801
lemma one_int_code [code]:
huffman@29942
   802
  "1 = Pls One"
huffman@31028
   803
  by (simp add: of_num_One)
haftmann@28021
   804
haftmann@28021
   805
lemma plus_int_code [code]:
haftmann@28021
   806
  "k + 0 = (k::int)"
haftmann@28021
   807
  "0 + l = (l::int)"
haftmann@28021
   808
  "Pls m + Pls n = Pls (m + n)"
haftmann@28021
   809
  "Pls m - Pls n = sub m n"
haftmann@28021
   810
  "Mns m + Mns n = Mns (m + n)"
haftmann@28021
   811
  "Mns m - Mns n = sub n m"
huffman@31028
   812
  by (simp_all add: of_num_add)
haftmann@28021
   813
haftmann@28021
   814
lemma uminus_int_code [code]:
haftmann@28021
   815
  "uminus 0 = (0::int)"
haftmann@28021
   816
  "uminus (Pls m) = Mns m"
haftmann@28021
   817
  "uminus (Mns m) = Pls m"
haftmann@28021
   818
  by simp_all
haftmann@28021
   819
haftmann@28021
   820
lemma minus_int_code [code]:
haftmann@28021
   821
  "k - 0 = (k::int)"
haftmann@28021
   822
  "0 - l = uminus (l::int)"
haftmann@28021
   823
  "Pls m - Pls n = sub m n"
haftmann@28021
   824
  "Pls m - Mns n = Pls (m + n)"
haftmann@28021
   825
  "Mns m - Pls n = Mns (m + n)"
haftmann@28021
   826
  "Mns m - Mns n = sub n m"
huffman@31028
   827
  by (simp_all add: of_num_add)
haftmann@28021
   828
haftmann@28021
   829
lemma times_int_code [code]:
haftmann@28021
   830
  "k * 0 = (0::int)"
haftmann@28021
   831
  "0 * l = (0::int)"
haftmann@28021
   832
  "Pls m * Pls n = Pls (m * n)"
haftmann@28021
   833
  "Pls m * Mns n = Mns (m * n)"
haftmann@28021
   834
  "Mns m * Pls n = Mns (m * n)"
haftmann@28021
   835
  "Mns m * Mns n = Pls (m * n)"
huffman@31028
   836
  by (simp_all add: of_num_mult)
haftmann@28021
   837
haftmann@28021
   838
lemma eq_int_code [code]:
haftmann@28367
   839
  "eq_class.eq 0 (0::int) \<longleftrightarrow> True"
haftmann@28367
   840
  "eq_class.eq 0 (Pls l) \<longleftrightarrow> False"
haftmann@28367
   841
  "eq_class.eq 0 (Mns l) \<longleftrightarrow> False"
haftmann@28367
   842
  "eq_class.eq (Pls k) 0 \<longleftrightarrow> False"
haftmann@28367
   843
  "eq_class.eq (Pls k) (Pls l) \<longleftrightarrow> eq_class.eq k l"
haftmann@28367
   844
  "eq_class.eq (Pls k) (Mns l) \<longleftrightarrow> False"
haftmann@28367
   845
  "eq_class.eq (Mns k) 0 \<longleftrightarrow> False"
haftmann@28367
   846
  "eq_class.eq (Mns k) (Pls l) \<longleftrightarrow> False"
haftmann@28367
   847
  "eq_class.eq (Mns k) (Mns l) \<longleftrightarrow> eq_class.eq k l"
haftmann@28021
   848
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
haftmann@28367
   849
  by (simp_all add: of_num_eq_iff eq)
haftmann@28021
   850
haftmann@28021
   851
lemma less_eq_int_code [code]:
haftmann@28021
   852
  "0 \<le> (0::int) \<longleftrightarrow> True"
haftmann@28021
   853
  "0 \<le> Pls l \<longleftrightarrow> True"
haftmann@28021
   854
  "0 \<le> Mns l \<longleftrightarrow> False"
haftmann@28021
   855
  "Pls k \<le> 0 \<longleftrightarrow> False"
haftmann@28021
   856
  "Pls k \<le> Pls l \<longleftrightarrow> k \<le> l"
haftmann@28021
   857
  "Pls k \<le> Mns l \<longleftrightarrow> False"
haftmann@28021
   858
  "Mns k \<le> 0 \<longleftrightarrow> True"
haftmann@28021
   859
  "Mns k \<le> Pls l \<longleftrightarrow> True"
haftmann@28021
   860
  "Mns k \<le> Mns l \<longleftrightarrow> l \<le> k"
haftmann@28021
   861
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
haftmann@28021
   862
  by (simp_all add: of_num_less_eq_iff)
haftmann@28021
   863
haftmann@28021
   864
lemma less_int_code [code]:
haftmann@28021
   865
  "0 < (0::int) \<longleftrightarrow> False"
haftmann@28021
   866
  "0 < Pls l \<longleftrightarrow> True"
haftmann@28021
   867
  "0 < Mns l \<longleftrightarrow> False"
haftmann@28021
   868
  "Pls k < 0 \<longleftrightarrow> False"
haftmann@28021
   869
  "Pls k < Pls l \<longleftrightarrow> k < l"
haftmann@28021
   870
  "Pls k < Mns l \<longleftrightarrow> False"
haftmann@28021
   871
  "Mns k < 0 \<longleftrightarrow> True"
haftmann@28021
   872
  "Mns k < Pls l \<longleftrightarrow> True"
haftmann@28021
   873
  "Mns k < Mns l \<longleftrightarrow> l < k"
haftmann@28021
   874
  using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int]
haftmann@28021
   875
  by (simp_all add: of_num_less_iff)
haftmann@28021
   876
haftmann@28021
   877
lemma [code inline del]: "(0::int) \<equiv> Numeral0" by simp
haftmann@28021
   878
lemma [code inline del]: "(1::int) \<equiv> Numeral1" by simp
haftmann@28021
   879
declare zero_is_num_zero [code inline del]
haftmann@28021
   880
declare one_is_num_one [code inline del]
haftmann@28021
   881
haftmann@28021
   882
hide (open) const sub dup
haftmann@28021
   883
haftmann@28021
   884
haftmann@28021
   885
subsection {* Numeral equations as default simplification rules *}
haftmann@28021
   886
huffman@31029
   887
declare (in semiring_numeral) of_num_One [simp]
huffman@31029
   888
declare (in semiring_numeral) of_num_plus_one [simp]
huffman@31029
   889
declare (in semiring_numeral) of_num_one_plus [simp]
huffman@31029
   890
declare (in semiring_numeral) of_num_plus [simp]
huffman@31029
   891
declare (in semiring_numeral) of_num_times [simp]
huffman@31029
   892
huffman@31029
   893
declare (in semiring_1) of_nat_of_num [simp]
huffman@31029
   894
huffman@31029
   895
declare (in semiring_char_0) of_num_eq_iff [simp]
huffman@31029
   896
declare (in semiring_char_0) of_num_eq_one_iff [simp]
huffman@31029
   897
declare (in semiring_char_0) one_eq_of_num_iff [simp]
huffman@31029
   898
huffman@31029
   899
declare (in ordered_semidom) of_num_pos [simp]
huffman@31029
   900
declare (in ordered_semidom) of_num_less_eq_iff [simp]
huffman@31029
   901
declare (in ordered_semidom) of_num_less_eq_one_iff [simp]
huffman@31029
   902
declare (in ordered_semidom) one_less_eq_of_num_iff [simp]
huffman@31029
   903
declare (in ordered_semidom) of_num_less_iff [simp]
huffman@31029
   904
declare (in ordered_semidom) of_num_less_one_iff [simp]
huffman@31029
   905
declare (in ordered_semidom) one_less_of_num_iff [simp]
huffman@31029
   906
declare (in ordered_semidom) of_num_nonneg [simp]
huffman@31029
   907
declare (in ordered_semidom) of_num_less_zero_iff [simp]
huffman@31029
   908
declare (in ordered_semidom) of_num_le_zero_iff [simp]
huffman@31029
   909
huffman@31029
   910
declare (in ordered_idom) le_signed_numeral_special [simp]
huffman@31029
   911
declare (in ordered_idom) less_signed_numeral_special [simp]
huffman@31029
   912
huffman@31029
   913
declare (in semiring_1_minus) Dig_of_num_minus_one [simp]
huffman@31029
   914
declare (in semiring_1_minus) Dig_one_minus_of_num [simp]
huffman@31029
   915
huffman@31029
   916
declare (in ring_1) Dig_plus_uminus [simp]
huffman@31029
   917
declare (in ring_1) of_int_of_num [simp]
huffman@31029
   918
huffman@31029
   919
declare power_of_num [simp]
huffman@31029
   920
declare power_zero_of_num [simp]
huffman@31029
   921
declare power_minus_Dig0 [simp]
huffman@31029
   922
declare power_minus_Dig1 [simp]
huffman@31029
   923
huffman@31029
   924
declare Suc_of_num [simp]
huffman@31029
   925
haftmann@28021
   926
thm numeral
haftmann@28021
   927
haftmann@28021
   928
huffman@31025
   929
subsection {* Simplification Procedures *}
huffman@31025
   930
huffman@31026
   931
subsubsection {* Reorientation of equalities *}
huffman@31025
   932
huffman@31025
   933
setup {*
huffman@31025
   934
  ReorientProc.add
huffman@31025
   935
    (fn Const(@{const_name of_num}, _) $ _ => true
huffman@31025
   936
      | Const(@{const_name uminus}, _) $
huffman@31025
   937
          (Const(@{const_name of_num}, _) $ _) => true
huffman@31025
   938
      | _ => false)
huffman@31025
   939
*}
huffman@31025
   940
huffman@31025
   941
simproc_setup reorient_num ("of_num n = x" | "- of_num m = y") = ReorientProc.proc
huffman@31025
   942
huffman@31026
   943
subsubsection {* Constant folding for multiplication in semirings *}
huffman@31026
   944
huffman@31026
   945
context semiring_numeral
huffman@31026
   946
begin
huffman@31026
   947
huffman@31026
   948
lemma mult_of_num_commute: "x * of_num n = of_num n * x"
huffman@31026
   949
by (induct n)
huffman@31026
   950
  (simp_all only: of_num.simps left_distrib right_distrib mult_1_left mult_1_right)
huffman@31026
   951
huffman@31026
   952
definition
huffman@31026
   953
  "commutes_with a b \<longleftrightarrow> a * b = b * a"
huffman@31026
   954
huffman@31026
   955
lemma commutes_with_commute: "commutes_with a b \<Longrightarrow> a * b = b * a"
huffman@31026
   956
unfolding commutes_with_def .
huffman@31026
   957
huffman@31026
   958
lemma commutes_with_left_commute: "commutes_with a b \<Longrightarrow> a * (b * c) = b * (a * c)"
huffman@31026
   959
unfolding commutes_with_def by (simp only: mult_assoc [symmetric])
huffman@31026
   960
huffman@31026
   961
lemma commutes_with_numeral: "commutes_with x (of_num n)" "commutes_with (of_num n) x"
huffman@31026
   962
unfolding commutes_with_def by (simp_all add: mult_of_num_commute)
huffman@31026
   963
huffman@31026
   964
lemmas mult_ac_numeral =
huffman@31026
   965
  mult_assoc
huffman@31026
   966
  commutes_with_commute
huffman@31026
   967
  commutes_with_left_commute
huffman@31026
   968
  commutes_with_numeral
huffman@31026
   969
huffman@31026
   970
end
huffman@31026
   971
huffman@31026
   972
ML {*
huffman@31026
   973
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
huffman@31026
   974
struct
huffman@31026
   975
  val assoc_ss = HOL_ss addsimps @{thms mult_ac_numeral}
huffman@31026
   976
  val eq_reflection = eq_reflection
huffman@31026
   977
  fun is_numeral (Const(@{const_name of_num}, _) $ _) = true
huffman@31026
   978
    | is_numeral _ = false;
huffman@31026
   979
end;
huffman@31026
   980
huffman@31026
   981
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
huffman@31026
   982
*}
huffman@31026
   983
huffman@31026
   984
simproc_setup semiring_assoc_fold' ("(a::'a::semiring_numeral) * b") =
huffman@31026
   985
  {* fn phi => fn ss => fn ct =>
huffman@31026
   986
    Semiring_Times_Assoc.proc ss (Thm.term_of ct) *}
huffman@31026
   987
huffman@31025
   988
huffman@31025
   989
subsection {* Toy examples *}
haftmann@28021
   990
haftmann@28021
   991
definition "bar \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat) \<and> #4 * #2 + #7 \<ge> (#8 :: int) - #3"
haftmann@28021
   992
code_thms bar
haftmann@28021
   993
export_code bar in Haskell file -
haftmann@28021
   994
export_code bar in OCaml module_name Foo file -
haftmann@28021
   995
ML {* @{code bar} *}
haftmann@28021
   996
haftmann@28021
   997
end