src/HOL/ex/Numeral.thy
author bulwahn
Fri Oct 21 11:17:14 2011 +0200 (2011-10-21)
changeset 45231 d85a2fdc586c
parent 45154 66e8a5812f41
child 45294 3c5d3d286055
permissions -rw-r--r--
replacing code_inline by code_unfold, removing obsolete code_unfold, code_inline del now that the ancient code generator is removed
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
wenzelm@33346
     8
imports Main
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"
haftmann@38054
    58
proof
haftmann@38054
    59
  assume "nat_of_num x = nat_of_num y"
haftmann@38054
    60
  then have "num_of_nat (nat_of_num x) = num_of_nat (nat_of_num y)" by simp
haftmann@38054
    61
  then show "x = y" by (simp add: nat_of_num_inverse)
haftmann@38054
    62
qed simp
haftmann@28021
    63
huffman@29943
    64
lemma num_induct [case_names One inc]:
huffman@29943
    65
  fixes P :: "num \<Rightarrow> bool"
huffman@29943
    66
  assumes One: "P One"
huffman@29943
    67
    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
huffman@29943
    68
  shows "P x"
huffman@29943
    69
proof -
huffman@29943
    70
  obtain n where n: "Suc n = nat_of_num x"
huffman@29943
    71
    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
huffman@29943
    72
  have "P (num_of_nat (Suc n))"
huffman@29943
    73
  proof (induct n)
huffman@29943
    74
    case 0 show ?case using One by simp
haftmann@28021
    75
  next
huffman@29943
    76
    case (Suc n)
huffman@29943
    77
    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
huffman@29943
    78
    then show "P (num_of_nat (Suc (Suc n)))" by simp
haftmann@28021
    79
  qed
huffman@29943
    80
  with n show "P x"
huffman@29943
    81
    by (simp add: nat_of_num_inverse)
haftmann@28021
    82
qed
haftmann@28021
    83
haftmann@28021
    84
text {*
haftmann@38054
    85
  From now on, there are two possible models for @{typ num}: as
haftmann@38054
    86
  positive naturals (rule @{text "num_induct"}) and as digit
haftmann@38054
    87
  representation (rules @{text "num.induct"}, @{text "num.cases"}).
haftmann@28021
    88
haftmann@38054
    89
  It is not entirely clear in which context it is better to use the
haftmann@38054
    90
  one or the other, or whether the construction should be reversed.
haftmann@28021
    91
*}
haftmann@28021
    92
haftmann@28021
    93
huffman@29945
    94
subsection {* Numeral operations *}
haftmann@28021
    95
haftmann@28021
    96
ML {*
wenzelm@31902
    97
structure Dig_Simps = Named_Thms
wenzelm@31902
    98
(
wenzelm@31902
    99
  val name = "numeral"
wenzelm@38764
   100
  val description = "simplification rules for numerals"
wenzelm@31902
   101
)
haftmann@28021
   102
*}
haftmann@28021
   103
wenzelm@31902
   104
setup Dig_Simps.setup
haftmann@28021
   105
huffman@29945
   106
instantiation num :: "{plus,times,ord}"
huffman@29945
   107
begin
haftmann@28021
   108
haftmann@28021
   109
definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where
haftmann@37765
   110
  "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
haftmann@28021
   111
haftmann@28021
   112
definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where
haftmann@37765
   113
  "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
haftmann@28021
   114
huffman@29945
   115
definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
haftmann@37765
   116
  "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
haftmann@28021
   117
huffman@29945
   118
definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
haftmann@37765
   119
  "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
haftmann@28021
   120
huffman@29945
   121
instance ..
haftmann@28021
   122
haftmann@28021
   123
end
haftmann@28021
   124
huffman@29945
   125
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
huffman@29945
   126
  unfolding plus_num_def
huffman@29945
   127
  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
huffman@29945
   128
huffman@29945
   129
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
huffman@29945
   130
  unfolding times_num_def
huffman@29945
   131
  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
haftmann@28021
   132
huffman@29945
   133
lemma Dig_plus [numeral, simp, code]:
huffman@29945
   134
  "One + One = Dig0 One"
huffman@29945
   135
  "One + Dig0 m = Dig1 m"
huffman@29945
   136
  "One + Dig1 m = Dig0 (m + One)"
huffman@29945
   137
  "Dig0 n + One = Dig1 n"
huffman@29945
   138
  "Dig0 n + Dig0 m = Dig0 (n + m)"
huffman@29945
   139
  "Dig0 n + Dig1 m = Dig1 (n + m)"
huffman@29945
   140
  "Dig1 n + One = Dig0 (n + One)"
huffman@29945
   141
  "Dig1 n + Dig0 m = Dig1 (n + m)"
huffman@29945
   142
  "Dig1 n + Dig1 m = Dig0 (n + m + One)"
huffman@29945
   143
  by (simp_all add: num_eq_iff nat_of_num_add)
haftmann@28021
   144
huffman@29945
   145
lemma Dig_times [numeral, simp, code]:
huffman@29945
   146
  "One * One = One"
huffman@29945
   147
  "One * Dig0 n = Dig0 n"
huffman@29945
   148
  "One * Dig1 n = Dig1 n"
huffman@29945
   149
  "Dig0 n * One = Dig0 n"
huffman@29945
   150
  "Dig0 n * Dig0 m = Dig0 (n * Dig0 m)"
huffman@29945
   151
  "Dig0 n * Dig1 m = Dig0 (n * Dig1 m)"
huffman@29945
   152
  "Dig1 n * One = Dig1 n"
huffman@29945
   153
  "Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)"
huffman@29945
   154
  "Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)"
huffman@29945
   155
  by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult
huffman@29945
   156
                    left_distrib right_distrib)
haftmann@28021
   157
huffman@29945
   158
lemma less_eq_num_code [numeral, simp, code]:
huffman@29945
   159
  "One \<le> n \<longleftrightarrow> True"
huffman@29945
   160
  "Dig0 m \<le> One \<longleftrightarrow> False"
huffman@29945
   161
  "Dig1 m \<le> One \<longleftrightarrow> False"
huffman@29945
   162
  "Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n"
huffman@29945
   163
  "Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
huffman@29945
   164
  "Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
huffman@29945
   165
  "Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n"
huffman@29945
   166
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
huffman@29945
   167
  by (auto simp add: less_eq_num_def less_num_def)
huffman@29945
   168
huffman@29945
   169
lemma less_num_code [numeral, simp, code]:
huffman@29945
   170
  "m < One \<longleftrightarrow> False"
huffman@29945
   171
  "One < One \<longleftrightarrow> False"
huffman@29945
   172
  "One < Dig0 n \<longleftrightarrow> True"
huffman@29945
   173
  "One < Dig1 n \<longleftrightarrow> True"
huffman@29945
   174
  "Dig0 m < Dig0 n \<longleftrightarrow> m < n"
huffman@29945
   175
  "Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n"
huffman@29945
   176
  "Dig1 m < Dig1 n \<longleftrightarrow> m < n"
huffman@29945
   177
  "Dig1 m < Dig0 n \<longleftrightarrow> m < n"
huffman@29945
   178
  using nat_of_num_pos [of n] nat_of_num_pos [of m]
huffman@29945
   179
  by (auto simp add: less_eq_num_def less_num_def)
huffman@29945
   180
huffman@29945
   181
text {* Rules using @{text One} and @{text inc} as constructors *}
haftmann@28021
   182
huffman@29945
   183
lemma add_One: "x + One = inc x"
huffman@29945
   184
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
huffman@29945
   185
huffman@29945
   186
lemma add_inc: "x + inc y = inc (x + y)"
huffman@29945
   187
  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
huffman@29945
   188
huffman@29945
   189
lemma mult_One: "x * One = x"
huffman@29945
   190
  by (simp add: num_eq_iff nat_of_num_mult)
huffman@29945
   191
huffman@29945
   192
lemma mult_inc: "x * inc y = x * y + x"
huffman@29945
   193
  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
huffman@29945
   194
huffman@29945
   195
text {* A double-and-decrement function *}
haftmann@28021
   196
huffman@29945
   197
primrec DigM :: "num \<Rightarrow> num" where
huffman@29945
   198
  "DigM One = One"
haftmann@38054
   199
| "DigM (Dig0 n) = Dig1 (DigM n)"
haftmann@38054
   200
| "DigM (Dig1 n) = Dig1 (Dig0 n)"
haftmann@28021
   201
huffman@29945
   202
lemma DigM_plus_one: "DigM n + One = Dig0 n"
huffman@29945
   203
  by (induct n) simp_all
huffman@29945
   204
huffman@29945
   205
lemma add_One_commute: "One + n = n + One"
huffman@29945
   206
  by (induct n) simp_all
huffman@29945
   207
huffman@29945
   208
lemma one_plus_DigM: "One + DigM n = Dig0 n"
haftmann@38054
   209
  by (simp add: add_One_commute DigM_plus_one)
haftmann@28021
   210
huffman@29954
   211
text {* Squaring and exponentiation *}
huffman@29947
   212
huffman@29947
   213
primrec square :: "num \<Rightarrow> num" where
huffman@29947
   214
  "square One = One"
huffman@29947
   215
| "square (Dig0 n) = Dig0 (Dig0 (square n))"
huffman@29947
   216
| "square (Dig1 n) = Dig1 (Dig0 (square n + n))"
huffman@29947
   217
haftmann@38054
   218
primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
huffman@29954
   219
  "pow x One = x"
huffman@29954
   220
| "pow x (Dig0 y) = square (pow x y)"
huffman@29954
   221
| "pow x (Dig1 y) = x * square (pow x y)"
huffman@29947
   222
haftmann@28021
   223
haftmann@28021
   224
subsection {* Binary numerals *}
haftmann@28021
   225
haftmann@28021
   226
text {*
haftmann@28021
   227
  We embed binary representations into a generic algebraic
haftmann@29934
   228
  structure using @{text of_num}.
haftmann@28021
   229
*}
haftmann@28021
   230
haftmann@28021
   231
class semiring_numeral = semiring + monoid_mult
haftmann@28021
   232
begin
haftmann@28021
   233
haftmann@28021
   234
primrec of_num :: "num \<Rightarrow> 'a" where
huffman@31028
   235
  of_num_One [numeral]: "of_num One = 1"
haftmann@38054
   236
| "of_num (Dig0 n) = of_num n + of_num n"
haftmann@38054
   237
| "of_num (Dig1 n) = of_num n + of_num n + 1"
haftmann@28021
   238
haftmann@38054
   239
lemma of_num_inc: "of_num (inc n) = of_num n + 1"
haftmann@38054
   240
  by (induct n) (simp_all add: add_ac)
huffman@29943
   241
huffman@31028
   242
lemma of_num_add: "of_num (m + n) = of_num m + of_num n"
haftmann@38054
   243
  by (induct n rule: num_induct) (simp_all add: add_One add_inc of_num_inc add_ac)
huffman@31028
   244
huffman@31028
   245
lemma of_num_mult: "of_num (m * n) = of_num m * of_num n"
haftmann@38054
   246
  by (induct n rule: num_induct) (simp_all add: mult_One mult_inc of_num_add of_num_inc right_distrib)
huffman@31028
   247
haftmann@28021
   248
declare of_num.simps [simp del]
haftmann@28021
   249
haftmann@28021
   250
end
haftmann@28021
   251
haftmann@28021
   252
ML {*
huffman@31027
   253
fun mk_num k =
huffman@31027
   254
  if k > 1 then
huffman@31027
   255
    let
huffman@31027
   256
      val (l, b) = Integer.div_mod k 2;
huffman@31027
   257
      val bit = (if b = 0 then @{term Dig0} else @{term Dig1});
huffman@31027
   258
    in bit $ (mk_num l) end
huffman@31027
   259
  else if k = 1 then @{term One}
huffman@31027
   260
  else error ("mk_num " ^ string_of_int k);
haftmann@28021
   261
huffman@29942
   262
fun dest_num @{term One} = 1
haftmann@28021
   263
  | dest_num (@{term Dig0} $ n) = 2 * dest_num n
huffman@31027
   264
  | dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1
huffman@31027
   265
  | dest_num t = raise TERM ("dest_num", [t]);
haftmann@28021
   266
haftmann@38054
   267
fun mk_numeral phi T k = Morphism.term phi (Const (@{const_name of_num}, @{typ num} --> T))
haftmann@28021
   268
  $ mk_num k
haftmann@28021
   269
haftmann@38054
   270
fun dest_numeral phi (u $ t) =
haftmann@38054
   271
  if Term.aconv_untyped (u, Morphism.term phi (Const (@{const_name of_num}, dummyT)))
haftmann@38054
   272
  then (range_type (fastype_of u), dest_num t)
haftmann@38054
   273
  else raise TERM ("dest_numeral", [u, t]);
haftmann@28021
   274
*}
haftmann@28021
   275
haftmann@28021
   276
syntax
haftmann@28021
   277
  "_Numerals" :: "xnum \<Rightarrow> 'a"    ("_")
haftmann@28021
   278
haftmann@28021
   279
parse_translation {*
haftmann@28021
   280
let
haftmann@28021
   281
  fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2)
huffman@29942
   282
     of (0, 1) => Const (@{const_name One}, dummyT)
haftmann@28021
   283
      | (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n
haftmann@28021
   284
      | (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n
haftmann@28021
   285
    else raise Match;
haftmann@28021
   286
  fun numeral_tr [Free (num, _)] =
haftmann@28021
   287
        let
wenzelm@42290
   288
          val {leading_zeros, value, ...} = Lexicon.read_xnum num;
haftmann@28021
   289
          val _ = leading_zeros = 0 andalso value > 0
haftmann@28021
   290
            orelse error ("Bad numeral: " ^ num);
haftmann@28021
   291
        in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end
haftmann@28021
   292
    | numeral_tr ts = raise TERM ("numeral_tr", ts);
wenzelm@35113
   293
in [(@{syntax_const "_Numerals"}, numeral_tr)] end
haftmann@28021
   294
*}
haftmann@28021
   295
wenzelm@39134
   296
typed_print_translation (advanced) {*
haftmann@28021
   297
let
haftmann@28021
   298
  fun dig b n = b + 2 * n; 
haftmann@28021
   299
  fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) =
haftmann@28021
   300
        dig 0 (int_of_num' n)
haftmann@28021
   301
    | int_of_num' (Const (@{const_syntax Dig1}, _) $ n) =
haftmann@28021
   302
        dig 1 (int_of_num' n)
huffman@29942
   303
    | int_of_num' (Const (@{const_syntax One}, _)) = 1;
wenzelm@42247
   304
  fun num_tr' ctxt T [n] =
haftmann@28021
   305
    let
haftmann@28021
   306
      val k = int_of_num' n;
wenzelm@35113
   307
      val t' = Syntax.const @{syntax_const "_Numerals"} $ Syntax.free ("#" ^ string_of_int k);
wenzelm@39134
   308
    in
wenzelm@39134
   309
      case T of
wenzelm@39134
   310
        Type (@{type_name fun}, [_, T']) =>
wenzelm@39134
   311
          if not (Config.get ctxt show_types) andalso can Term.dest_Type T' then t'
wenzelm@42248
   312
          else Syntax.const @{syntax_const "_constrain"} $ t' $ Syntax_Phases.term_of_typ ctxt T'
haftmann@28021
   313
      | T' => if T' = dummyT then t' else raise Match
haftmann@28021
   314
    end;
haftmann@28021
   315
in [(@{const_syntax of_num}, num_tr')] end
haftmann@28021
   316
*}
haftmann@28021
   317
haftmann@38054
   318
huffman@29945
   319
subsection {* Class-specific numeral rules *}
haftmann@28021
   320
huffman@29945
   321
subsubsection {* Class @{text semiring_numeral} *}
huffman@29945
   322
haftmann@28021
   323
context semiring_numeral
haftmann@28021
   324
begin
haftmann@28021
   325
huffman@29943
   326
abbreviation "Num1 \<equiv> of_num One"
haftmann@28021
   327
haftmann@28021
   328
text {*
haftmann@38054
   329
  Alas, there is still the duplication of @{term 1}, although the
haftmann@38054
   330
  duplicated @{term 0} has disappeared.  We could get rid of it by
haftmann@38054
   331
  replacing the constructor @{term 1} in @{typ num} by two
haftmann@38054
   332
  constructors @{text two} and @{text three}, resulting in a further
haftmann@28021
   333
  blow-up.  But it could be worth the effort.
haftmann@28021
   334
*}
haftmann@28021
   335
haftmann@28021
   336
lemma of_num_plus_one [numeral]:
huffman@29942
   337
  "of_num n + 1 = of_num (n + One)"
huffman@31028
   338
  by (simp only: of_num_add of_num_One)
haftmann@28021
   339
haftmann@28021
   340
lemma of_num_one_plus [numeral]:
huffman@31028
   341
  "1 + of_num n = of_num (One + n)"
huffman@31028
   342
  by (simp only: of_num_add of_num_One)
haftmann@28021
   343
haftmann@28021
   344
lemma of_num_plus [numeral]:
haftmann@28021
   345
  "of_num m + of_num n = of_num (m + n)"
haftmann@38054
   346
  by (simp only: of_num_add)
haftmann@28021
   347
haftmann@28021
   348
lemma of_num_times_one [numeral]:
haftmann@28021
   349
  "of_num n * 1 = of_num n"
haftmann@28021
   350
  by simp
haftmann@28021
   351
haftmann@28021
   352
lemma of_num_one_times [numeral]:
haftmann@28021
   353
  "1 * of_num n = of_num n"
haftmann@28021
   354
  by simp
haftmann@28021
   355
haftmann@28021
   356
lemma of_num_times [numeral]:
haftmann@28021
   357
  "of_num m * of_num n = of_num (m * n)"
huffman@31028
   358
  unfolding of_num_mult ..
haftmann@28021
   359
haftmann@28021
   360
end
haftmann@28021
   361
haftmann@38054
   362
haftmann@38054
   363
subsubsection {* Structures with a zero: class @{text semiring_1} *}
haftmann@28021
   364
haftmann@28021
   365
context semiring_1
haftmann@28021
   366
begin
haftmann@28021
   367
haftmann@28021
   368
subclass semiring_numeral ..
haftmann@28021
   369
haftmann@28021
   370
lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n"
haftmann@28021
   371
  by (induct n)
haftmann@28021
   372
    (simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac)
haftmann@28021
   373
haftmann@28021
   374
declare of_nat_1 [numeral]
haftmann@28021
   375
haftmann@28021
   376
lemma Dig_plus_zero [numeral]:
haftmann@28021
   377
  "0 + 1 = 1"
haftmann@28021
   378
  "0 + of_num n = of_num n"
haftmann@28021
   379
  "1 + 0 = 1"
haftmann@28021
   380
  "of_num n + 0 = of_num n"
haftmann@28021
   381
  by simp_all
haftmann@28021
   382
haftmann@28021
   383
lemma Dig_times_zero [numeral]:
haftmann@28021
   384
  "0 * 1 = 0"
haftmann@28021
   385
  "0 * of_num n = 0"
haftmann@28021
   386
  "1 * 0 = 0"
haftmann@28021
   387
  "of_num n * 0 = 0"
haftmann@28021
   388
  by simp_all
haftmann@28021
   389
haftmann@28021
   390
end
haftmann@28021
   391
haftmann@28021
   392
lemma nat_of_num_of_num: "nat_of_num = of_num"
haftmann@28021
   393
proof
haftmann@28021
   394
  fix n
huffman@29943
   395
  have "of_num n = nat_of_num n"
huffman@29943
   396
    by (induct n) (simp_all add: of_num.simps)
haftmann@28021
   397
  then show "nat_of_num n = of_num n" by simp
haftmann@28021
   398
qed
haftmann@28021
   399
haftmann@38054
   400
haftmann@38054
   401
subsubsection {* Equality: class @{text semiring_char_0} *}
haftmann@28021
   402
haftmann@28021
   403
context semiring_char_0
haftmann@28021
   404
begin
haftmann@28021
   405
huffman@31028
   406
lemma of_num_eq_iff [numeral]: "of_num m = of_num n \<longleftrightarrow> m = n"
haftmann@28021
   407
  unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric]
huffman@29943
   408
    of_nat_eq_iff num_eq_iff ..
haftmann@28021
   409
huffman@31028
   410
lemma of_num_eq_one_iff [numeral]: "of_num n = 1 \<longleftrightarrow> n = One"
huffman@31028
   411
  using of_num_eq_iff [of n One] by (simp add: of_num_One)
haftmann@28021
   412
huffman@31028
   413
lemma one_eq_of_num_iff [numeral]: "1 = of_num n \<longleftrightarrow> One = n"
huffman@31028
   414
  using of_num_eq_iff [of One n] by (simp add: of_num_One)
haftmann@28021
   415
haftmann@28021
   416
end
haftmann@28021
   417
haftmann@38054
   418
haftmann@38054
   419
subsubsection {* Comparisons: class @{text linordered_semidom} *}
haftmann@38054
   420
haftmann@38054
   421
text {*
haftmann@38054
   422
  Perhaps the underlying structure could even 
haftmann@38054
   423
  be more general than @{text linordered_semidom}.
haftmann@28021
   424
*}
haftmann@28021
   425
haftmann@35028
   426
context linordered_semidom
haftmann@28021
   427
begin
haftmann@28021
   428
huffman@29991
   429
lemma of_num_pos [numeral]: "0 < of_num n"
huffman@29991
   430
  by (induct n) (simp_all add: of_num.simps add_pos_pos)
huffman@29991
   431
haftmann@38054
   432
lemma of_num_not_zero [numeral]: "of_num n \<noteq> 0"
haftmann@38054
   433
  using of_num_pos [of n] by simp
haftmann@38054
   434
haftmann@28021
   435
lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n"
haftmann@28021
   436
proof -
haftmann@28021
   437
  have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n"
haftmann@28021
   438
    unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff ..
haftmann@28021
   439
  then show ?thesis by (simp add: of_nat_of_num)
haftmann@28021
   440
qed
haftmann@28021
   441
huffman@31028
   442
lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n \<le> One"
huffman@31028
   443
  using of_num_less_eq_iff [of n One] by (simp add: of_num_One)
haftmann@28021
   444
haftmann@28021
   445
lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n"
huffman@31028
   446
  using of_num_less_eq_iff [of One n] by (simp add: of_num_One)
haftmann@28021
   447
haftmann@28021
   448
lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n"
haftmann@28021
   449
proof -
haftmann@28021
   450
  have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n"
haftmann@28021
   451
    unfolding less_num_def nat_of_num_of_num of_nat_less_iff ..
haftmann@28021
   452
  then show ?thesis by (simp add: of_nat_of_num)
haftmann@28021
   453
qed
haftmann@28021
   454
haftmann@28021
   455
lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1"
huffman@31028
   456
  using of_num_less_iff [of n One] by (simp add: of_num_One)
haftmann@28021
   457
huffman@31028
   458
lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> One < n"
huffman@31028
   459
  using of_num_less_iff [of One n] by (simp add: of_num_One)
haftmann@28021
   460
huffman@29991
   461
lemma of_num_nonneg [numeral]: "0 \<le> of_num n"
huffman@29991
   462
  by (induct n) (simp_all add: of_num.simps add_nonneg_nonneg)
huffman@29991
   463
huffman@29991
   464
lemma of_num_less_zero_iff [numeral]: "\<not> of_num n < 0"
huffman@29991
   465
  by (simp add: not_less of_num_nonneg)
huffman@29991
   466
huffman@29991
   467
lemma of_num_le_zero_iff [numeral]: "\<not> of_num n \<le> 0"
huffman@29991
   468
  by (simp add: not_le of_num_pos)
huffman@29991
   469
huffman@29991
   470
end
huffman@29991
   471
haftmann@35028
   472
context linordered_idom
huffman@29991
   473
begin
huffman@29991
   474
huffman@30792
   475
lemma minus_of_num_less_of_num_iff: "- of_num m < of_num n"
huffman@29991
   476
proof -
huffman@29991
   477
  have "- of_num m < 0" by (simp add: of_num_pos)
huffman@29991
   478
  also have "0 < of_num n" by (simp add: of_num_pos)
huffman@29991
   479
  finally show ?thesis .
huffman@29991
   480
qed
huffman@29991
   481
haftmann@38054
   482
lemma minus_of_num_not_equal_of_num: "- of_num m \<noteq> of_num n"
haftmann@38054
   483
  using minus_of_num_less_of_num_iff [of m n] by simp
haftmann@38054
   484
huffman@30792
   485
lemma minus_of_num_less_one_iff: "- of_num n < 1"
huffman@31028
   486
  using minus_of_num_less_of_num_iff [of n One] by (simp add: of_num_One)
huffman@29991
   487
huffman@30792
   488
lemma minus_one_less_of_num_iff: "- 1 < of_num n"
huffman@31028
   489
  using minus_of_num_less_of_num_iff [of One n] by (simp add: of_num_One)
huffman@29991
   490
huffman@30792
   491
lemma minus_one_less_one_iff: "- 1 < 1"
huffman@31028
   492
  using minus_of_num_less_of_num_iff [of One One] by (simp add: of_num_One)
huffman@30792
   493
huffman@30792
   494
lemma minus_of_num_le_of_num_iff: "- of_num m \<le> of_num n"
huffman@29991
   495
  by (simp add: less_imp_le minus_of_num_less_of_num_iff)
huffman@29991
   496
huffman@30792
   497
lemma minus_of_num_le_one_iff: "- of_num n \<le> 1"
huffman@29991
   498
  by (simp add: less_imp_le minus_of_num_less_one_iff)
huffman@29991
   499
huffman@30792
   500
lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n"
huffman@29991
   501
  by (simp add: less_imp_le minus_one_less_of_num_iff)
huffman@29991
   502
huffman@30792
   503
lemma minus_one_le_one_iff: "- 1 \<le> 1"
huffman@30792
   504
  by (simp add: less_imp_le minus_one_less_one_iff)
huffman@30792
   505
huffman@30792
   506
lemma of_num_le_minus_of_num_iff: "\<not> of_num m \<le> - of_num n"
huffman@29991
   507
  by (simp add: not_le minus_of_num_less_of_num_iff)
huffman@29991
   508
huffman@30792
   509
lemma one_le_minus_of_num_iff: "\<not> 1 \<le> - of_num n"
huffman@29991
   510
  by (simp add: not_le minus_of_num_less_one_iff)
huffman@29991
   511
huffman@30792
   512
lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1"
huffman@29991
   513
  by (simp add: not_le minus_one_less_of_num_iff)
huffman@29991
   514
huffman@30792
   515
lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1"
huffman@30792
   516
  by (simp add: not_le minus_one_less_one_iff)
huffman@30792
   517
huffman@30792
   518
lemma of_num_less_minus_of_num_iff: "\<not> of_num m < - of_num n"
huffman@29991
   519
  by (simp add: not_less minus_of_num_le_of_num_iff)
huffman@29991
   520
huffman@30792
   521
lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n"
huffman@29991
   522
  by (simp add: not_less minus_of_num_le_one_iff)
huffman@29991
   523
huffman@30792
   524
lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1"
huffman@29991
   525
  by (simp add: not_less minus_one_le_of_num_iff)
huffman@29991
   526
huffman@30792
   527
lemma one_less_minus_one_iff: "\<not> 1 < - 1"
huffman@30792
   528
  by (simp add: not_less minus_one_le_one_iff)
huffman@30792
   529
huffman@30792
   530
lemmas le_signed_numeral_special [numeral] =
huffman@30792
   531
  minus_of_num_le_of_num_iff
huffman@30792
   532
  minus_of_num_le_one_iff
huffman@30792
   533
  minus_one_le_of_num_iff
huffman@30792
   534
  minus_one_le_one_iff
huffman@30792
   535
  of_num_le_minus_of_num_iff
huffman@30792
   536
  one_le_minus_of_num_iff
huffman@30792
   537
  of_num_le_minus_one_iff
huffman@30792
   538
  one_le_minus_one_iff
huffman@30792
   539
huffman@30792
   540
lemmas less_signed_numeral_special [numeral] =
huffman@30792
   541
  minus_of_num_less_of_num_iff
haftmann@38054
   542
  minus_of_num_not_equal_of_num
huffman@30792
   543
  minus_of_num_less_one_iff
huffman@30792
   544
  minus_one_less_of_num_iff
huffman@30792
   545
  minus_one_less_one_iff
huffman@30792
   546
  of_num_less_minus_of_num_iff
huffman@30792
   547
  one_less_minus_of_num_iff
huffman@30792
   548
  of_num_less_minus_one_iff
huffman@30792
   549
  one_less_minus_one_iff
huffman@30792
   550
haftmann@28021
   551
end
haftmann@28021
   552
haftmann@38054
   553
subsubsection {* Structures with subtraction: class @{text semiring_1_minus} *}
haftmann@28021
   554
haftmann@28021
   555
class semiring_minus = semiring + minus + zero +
haftmann@28021
   556
  assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a"
haftmann@28021
   557
  assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a"
haftmann@28021
   558
begin
haftmann@28021
   559
haftmann@28021
   560
lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b"
haftmann@28021
   561
  by (simp add: add_ac minus_inverts_plus1 [of b a])
haftmann@28021
   562
haftmann@28021
   563
lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b"
haftmann@28021
   564
  by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a])
haftmann@28021
   565
haftmann@28021
   566
end
haftmann@28021
   567
haftmann@28021
   568
class semiring_1_minus = semiring_1 + semiring_minus
haftmann@28021
   569
begin
haftmann@28021
   570
haftmann@28021
   571
lemma Dig_of_num_pos:
haftmann@28021
   572
  assumes "k + n = m"
haftmann@28021
   573
  shows "of_num m - of_num n = of_num k"
haftmann@28021
   574
  using assms by (simp add: of_num_plus minus_inverts_plus1)
haftmann@28021
   575
haftmann@28021
   576
lemma Dig_of_num_zero:
haftmann@28021
   577
  shows "of_num n - of_num n = 0"
haftmann@28021
   578
  by (rule minus_inverts_plus1) simp
haftmann@28021
   579
haftmann@28021
   580
lemma Dig_of_num_neg:
haftmann@28021
   581
  assumes "k + m = n"
haftmann@28021
   582
  shows "of_num m - of_num n = 0 - of_num k"
haftmann@28021
   583
  by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms)
haftmann@28021
   584
haftmann@28021
   585
lemmas Dig_plus_eval =
huffman@29942
   586
  of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject
haftmann@28021
   587
haftmann@28021
   588
simproc_setup numeral_minus ("of_num m - of_num n") = {*
haftmann@28021
   589
  let
haftmann@28021
   590
    (*TODO proper implicit use of morphism via pattern antiquotations*)
wenzelm@41489
   591
    fun cdest_of_num ct = (List.last o snd o Drule.strip_comb) ct;
haftmann@28021
   592
    fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n);
haftmann@28021
   593
    fun attach_num ct = (dest_num (Thm.term_of ct), ct);
haftmann@28021
   594
    fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t;
wenzelm@41228
   595
    val simplify = Raw_Simplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval});
haftmann@38054
   596
    fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq}
haftmann@38054
   597
      OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"},
haftmann@38054
   598
        [Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]];
haftmann@28021
   599
  in fn phi => fn _ => fn ct => case try cdifference ct
haftmann@28021
   600
   of NONE => (NONE)
haftmann@28021
   601
    | SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0
wenzelm@41228
   602
        then Raw_Simplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct
haftmann@28021
   603
        else mk_meta_eq (let
haftmann@28021
   604
          val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j));
haftmann@28021
   605
        in
haftmann@28021
   606
          (if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck]
haftmann@28021
   607
          else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl])
haftmann@28021
   608
        end) end)
haftmann@28021
   609
  end
haftmann@28021
   610
*}
haftmann@28021
   611
haftmann@28021
   612
lemma Dig_of_num_minus_zero [numeral]:
haftmann@28021
   613
  "of_num n - 0 = of_num n"
haftmann@28021
   614
  by (simp add: minus_inverts_plus1)
haftmann@28021
   615
haftmann@28021
   616
lemma Dig_one_minus_zero [numeral]:
haftmann@28021
   617
  "1 - 0 = 1"
haftmann@28021
   618
  by (simp add: minus_inverts_plus1)
haftmann@28021
   619
haftmann@28021
   620
lemma Dig_one_minus_one [numeral]:
haftmann@28021
   621
  "1 - 1 = 0"
haftmann@28021
   622
  by (simp add: minus_inverts_plus1)
haftmann@28021
   623
haftmann@28021
   624
lemma Dig_of_num_minus_one [numeral]:
huffman@29941
   625
  "of_num (Dig0 n) - 1 = of_num (DigM n)"
haftmann@28021
   626
  "of_num (Dig1 n) - 1 = of_num (Dig0 n)"
huffman@29941
   627
  by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
haftmann@28021
   628
haftmann@28021
   629
lemma Dig_one_minus_of_num [numeral]:
huffman@29941
   630
  "1 - of_num (Dig0 n) = 0 - of_num (DigM n)"
haftmann@28021
   631
  "1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)"
huffman@29941
   632
  by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
haftmann@28021
   633
haftmann@28021
   634
end
haftmann@28021
   635
haftmann@38054
   636
haftmann@38054
   637
subsubsection {* Structures with negation: class @{text ring_1} *}
huffman@29945
   638
haftmann@28021
   639
context ring_1
haftmann@28021
   640
begin
haftmann@28021
   641
haftmann@38054
   642
subclass semiring_1_minus proof
haftmann@38054
   643
qed (simp_all add: algebra_simps)
haftmann@28021
   644
haftmann@28021
   645
lemma Dig_zero_minus_of_num [numeral]:
haftmann@28021
   646
  "0 - of_num n = - of_num n"
haftmann@28021
   647
  by simp
haftmann@28021
   648
haftmann@28021
   649
lemma Dig_zero_minus_one [numeral]:
haftmann@28021
   650
  "0 - 1 = - 1"
haftmann@28021
   651
  by simp
haftmann@28021
   652
haftmann@28021
   653
lemma Dig_uminus_uminus [numeral]:
haftmann@28021
   654
  "- (- of_num n) = of_num n"
haftmann@28021
   655
  by simp
haftmann@28021
   656
haftmann@28021
   657
lemma Dig_plus_uminus [numeral]:
haftmann@28021
   658
  "of_num m + - of_num n = of_num m - of_num n"
haftmann@28021
   659
  "- of_num m + of_num n = of_num n - of_num m"
haftmann@28021
   660
  "- of_num m + - of_num n = - (of_num m + of_num n)"
haftmann@28021
   661
  "of_num m - - of_num n = of_num m + of_num n"
haftmann@28021
   662
  "- of_num m - of_num n = - (of_num m + of_num n)"
haftmann@28021
   663
  "- of_num m - - of_num n = of_num n - of_num m"
haftmann@28021
   664
  by (simp_all add: diff_minus add_commute)
haftmann@28021
   665
haftmann@28021
   666
lemma Dig_times_uminus [numeral]:
haftmann@28021
   667
  "- of_num n * of_num m = - (of_num n * of_num m)"
haftmann@28021
   668
  "of_num n * - of_num m = - (of_num n * of_num m)"
haftmann@28021
   669
  "- of_num n * - of_num m = of_num n * of_num m"
huffman@31028
   670
  by simp_all
haftmann@28021
   671
haftmann@28021
   672
lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n"
haftmann@28021
   673
by (induct n)
haftmann@28021
   674
  (simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all)
haftmann@28021
   675
haftmann@28021
   676
declare of_int_1 [numeral]
haftmann@28021
   677
haftmann@28021
   678
end
haftmann@28021
   679
haftmann@38054
   680
haftmann@38054
   681
subsubsection {* Structures with exponentiation *}
huffman@29954
   682
huffman@29954
   683
lemma of_num_square: "of_num (square x) = of_num x * of_num x"
huffman@29954
   684
by (induct x)
huffman@31028
   685
   (simp_all add: of_num.simps of_num_add algebra_simps)
huffman@29954
   686
huffman@31028
   687
lemma of_num_pow: "of_num (pow x y) = of_num x ^ of_num y"
huffman@29954
   688
by (induct y)
huffman@31028
   689
   (simp_all add: of_num.simps of_num_square of_num_mult power_add)
huffman@29954
   690
huffman@31028
   691
lemma power_of_num [numeral]: "of_num x ^ of_num y = of_num (pow x y)"
huffman@31028
   692
  unfolding of_num_pow ..
huffman@29954
   693
huffman@29954
   694
lemma power_zero_of_num [numeral]:
huffman@31029
   695
  "0 ^ of_num n = (0::'a::semiring_1)"
huffman@29954
   696
  using of_num_pos [where n=n and ?'a=nat]
huffman@29954
   697
  by (simp add: power_0_left)
huffman@29954
   698
huffman@29954
   699
lemma power_minus_Dig0 [numeral]:
huffman@31029
   700
  fixes x :: "'a::ring_1"
huffman@29954
   701
  shows "(- x) ^ of_num (Dig0 n) = x ^ of_num (Dig0 n)"
huffman@31028
   702
  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
huffman@29954
   703
huffman@29954
   704
lemma power_minus_Dig1 [numeral]:
huffman@31029
   705
  fixes x :: "'a::ring_1"
huffman@29954
   706
  shows "(- x) ^ of_num (Dig1 n) = - (x ^ of_num (Dig1 n))"
huffman@31028
   707
  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
huffman@29954
   708
huffman@29954
   709
declare power_one [numeral]
huffman@29954
   710
huffman@29954
   711
haftmann@38054
   712
subsubsection {* Greetings to @{typ nat}. *}
haftmann@28021
   713
haftmann@38054
   714
instance nat :: semiring_1_minus proof
haftmann@38054
   715
qed simp_all
haftmann@28021
   716
huffman@29942
   717
lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)"
haftmann@28021
   718
  unfolding of_num_plus_one [symmetric] by simp
haftmann@28021
   719
haftmann@28021
   720
lemma nat_number:
haftmann@28021
   721
  "1 = Suc 0"
huffman@29942
   722
  "of_num One = Suc 0"
huffman@29941
   723
  "of_num (Dig0 n) = Suc (of_num (DigM n))"
haftmann@28021
   724
  "of_num (Dig1 n) = Suc (of_num (Dig0 n))"
huffman@29941
   725
  by (simp_all add: of_num.simps DigM_plus_one Suc_of_num)
haftmann@28021
   726
haftmann@28021
   727
declare diff_0_eq_0 [numeral]
haftmann@28021
   728
haftmann@28021
   729
haftmann@38054
   730
subsection {* Proof tools setup *}
haftmann@28021
   731
haftmann@38054
   732
subsubsection {* Numeral equations as default simplification rules *}
haftmann@28021
   733
huffman@31029
   734
declare (in semiring_numeral) of_num_One [simp]
huffman@31029
   735
declare (in semiring_numeral) of_num_plus_one [simp]
huffman@31029
   736
declare (in semiring_numeral) of_num_one_plus [simp]
huffman@31029
   737
declare (in semiring_numeral) of_num_plus [simp]
huffman@31029
   738
declare (in semiring_numeral) of_num_times [simp]
huffman@31029
   739
huffman@31029
   740
declare (in semiring_1) of_nat_of_num [simp]
huffman@31029
   741
huffman@31029
   742
declare (in semiring_char_0) of_num_eq_iff [simp]
huffman@31029
   743
declare (in semiring_char_0) of_num_eq_one_iff [simp]
huffman@31029
   744
declare (in semiring_char_0) one_eq_of_num_iff [simp]
huffman@31029
   745
haftmann@35028
   746
declare (in linordered_semidom) of_num_pos [simp]
haftmann@38054
   747
declare (in linordered_semidom) of_num_not_zero [simp]
haftmann@35028
   748
declare (in linordered_semidom) of_num_less_eq_iff [simp]
haftmann@35028
   749
declare (in linordered_semidom) of_num_less_eq_one_iff [simp]
haftmann@35028
   750
declare (in linordered_semidom) one_less_eq_of_num_iff [simp]
haftmann@35028
   751
declare (in linordered_semidom) of_num_less_iff [simp]
haftmann@35028
   752
declare (in linordered_semidom) of_num_less_one_iff [simp]
haftmann@35028
   753
declare (in linordered_semidom) one_less_of_num_iff [simp]
haftmann@35028
   754
declare (in linordered_semidom) of_num_nonneg [simp]
haftmann@35028
   755
declare (in linordered_semidom) of_num_less_zero_iff [simp]
haftmann@35028
   756
declare (in linordered_semidom) of_num_le_zero_iff [simp]
huffman@31029
   757
haftmann@35028
   758
declare (in linordered_idom) le_signed_numeral_special [simp]
haftmann@35028
   759
declare (in linordered_idom) less_signed_numeral_special [simp]
huffman@31029
   760
huffman@31029
   761
declare (in semiring_1_minus) Dig_of_num_minus_one [simp]
huffman@31029
   762
declare (in semiring_1_minus) Dig_one_minus_of_num [simp]
huffman@31029
   763
huffman@31029
   764
declare (in ring_1) Dig_plus_uminus [simp]
huffman@31029
   765
declare (in ring_1) of_int_of_num [simp]
huffman@31029
   766
huffman@31029
   767
declare power_of_num [simp]
huffman@31029
   768
declare power_zero_of_num [simp]
huffman@31029
   769
declare power_minus_Dig0 [simp]
huffman@31029
   770
declare power_minus_Dig1 [simp]
huffman@31029
   771
huffman@31029
   772
declare Suc_of_num [simp]
huffman@31029
   773
haftmann@28021
   774
huffman@31026
   775
subsubsection {* Reorientation of equalities *}
huffman@31025
   776
huffman@31025
   777
setup {*
wenzelm@33523
   778
  Reorient_Proc.add
huffman@31025
   779
    (fn Const(@{const_name of_num}, _) $ _ => true
huffman@31025
   780
      | Const(@{const_name uminus}, _) $
huffman@31025
   781
          (Const(@{const_name of_num}, _) $ _) => true
huffman@31025
   782
      | _ => false)
huffman@31025
   783
*}
huffman@31025
   784
wenzelm@33523
   785
simproc_setup reorient_num ("of_num n = x" | "- of_num m = y") = Reorient_Proc.proc
wenzelm@33523
   786
huffman@31025
   787
huffman@31026
   788
subsubsection {* Constant folding for multiplication in semirings *}
huffman@31026
   789
huffman@31026
   790
context semiring_numeral
huffman@31026
   791
begin
huffman@31026
   792
huffman@31026
   793
lemma mult_of_num_commute: "x * of_num n = of_num n * x"
huffman@31026
   794
by (induct n)
huffman@31026
   795
  (simp_all only: of_num.simps left_distrib right_distrib mult_1_left mult_1_right)
huffman@31026
   796
huffman@31026
   797
definition
huffman@31026
   798
  "commutes_with a b \<longleftrightarrow> a * b = b * a"
huffman@31026
   799
huffman@31026
   800
lemma commutes_with_commute: "commutes_with a b \<Longrightarrow> a * b = b * a"
huffman@31026
   801
unfolding commutes_with_def .
huffman@31026
   802
huffman@31026
   803
lemma commutes_with_left_commute: "commutes_with a b \<Longrightarrow> a * (b * c) = b * (a * c)"
huffman@31026
   804
unfolding commutes_with_def by (simp only: mult_assoc [symmetric])
huffman@31026
   805
huffman@31026
   806
lemma commutes_with_numeral: "commutes_with x (of_num n)" "commutes_with (of_num n) x"
huffman@31026
   807
unfolding commutes_with_def by (simp_all add: mult_of_num_commute)
huffman@31026
   808
huffman@31026
   809
lemmas mult_ac_numeral =
huffman@31026
   810
  mult_assoc
huffman@31026
   811
  commutes_with_commute
huffman@31026
   812
  commutes_with_left_commute
huffman@31026
   813
  commutes_with_numeral
huffman@31026
   814
huffman@31026
   815
end
huffman@31026
   816
huffman@31026
   817
ML {*
huffman@31026
   818
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
huffman@31026
   819
struct
huffman@31026
   820
  val assoc_ss = HOL_ss addsimps @{thms mult_ac_numeral}
huffman@31026
   821
  val eq_reflection = eq_reflection
huffman@31026
   822
  fun is_numeral (Const(@{const_name of_num}, _) $ _) = true
huffman@31026
   823
    | is_numeral _ = false;
huffman@31026
   824
end;
huffman@31026
   825
huffman@31026
   826
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
huffman@31026
   827
*}
huffman@31026
   828
huffman@31026
   829
simproc_setup semiring_assoc_fold' ("(a::'a::semiring_numeral) * b") =
huffman@31026
   830
  {* fn phi => fn ss => fn ct =>
huffman@31026
   831
    Semiring_Times_Assoc.proc ss (Thm.term_of ct) *}
huffman@31026
   832
huffman@31025
   833
haftmann@38054
   834
subsection {* Code generator setup for @{typ int} *}
haftmann@38054
   835
haftmann@38054
   836
text {* Reversing standard setup *}
haftmann@38054
   837
haftmann@38054
   838
lemma [code_unfold del]: "(0::int) \<equiv> Numeral0" by simp
haftmann@38054
   839
lemma [code_unfold del]: "(1::int) \<equiv> Numeral1" by simp
haftmann@38054
   840
declare zero_is_num_zero [code_unfold del]
haftmann@38054
   841
declare one_is_num_one [code_unfold del]
haftmann@38054
   842
  
haftmann@38054
   843
lemma [code, code del]:
haftmann@38054
   844
  "(1 :: int) = 1"
haftmann@38054
   845
  "(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +"
haftmann@38054
   846
  "(uminus :: int \<Rightarrow> int) = uminus"
haftmann@38054
   847
  "(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -"
haftmann@38054
   848
  "(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *"
haftmann@38857
   849
  "(HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool) = HOL.equal"
haftmann@38054
   850
  "(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>"
haftmann@38054
   851
  "(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <"
haftmann@38054
   852
  by rule+
haftmann@38054
   853
haftmann@38054
   854
text {* Constructors *}
haftmann@38054
   855
haftmann@38054
   856
definition Pls :: "num \<Rightarrow> int" where
haftmann@38054
   857
  [simp, code_post]: "Pls n = of_num n"
haftmann@38054
   858
haftmann@38054
   859
definition Mns :: "num \<Rightarrow> int" where
haftmann@38054
   860
  [simp, code_post]: "Mns n = - of_num n"
haftmann@38054
   861
haftmann@38054
   862
code_datatype "0::int" Pls Mns
haftmann@38054
   863
haftmann@38054
   864
lemmas [code_unfold] = Pls_def [symmetric] Mns_def [symmetric]
haftmann@38054
   865
haftmann@38054
   866
text {* Auxiliary operations *}
haftmann@38054
   867
haftmann@38054
   868
definition dup :: "int \<Rightarrow> int" where
haftmann@38054
   869
  [simp]: "dup k = k + k"
haftmann@38054
   870
haftmann@38054
   871
lemma Dig_dup [code]:
haftmann@38054
   872
  "dup 0 = 0"
haftmann@38054
   873
  "dup (Pls n) = Pls (Dig0 n)"
haftmann@38054
   874
  "dup (Mns n) = Mns (Dig0 n)"
haftmann@38054
   875
  by (simp_all add: of_num.simps)
haftmann@38054
   876
haftmann@38054
   877
definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
haftmann@38054
   878
  [simp]: "sub m n = (of_num m - of_num n)"
haftmann@38054
   879
haftmann@38054
   880
lemma Dig_sub [code]:
haftmann@38054
   881
  "sub One One = 0"
haftmann@38054
   882
  "sub (Dig0 m) One = of_num (DigM m)"
haftmann@38054
   883
  "sub (Dig1 m) One = of_num (Dig0 m)"
haftmann@38054
   884
  "sub One (Dig0 n) = - of_num (DigM n)"
haftmann@38054
   885
  "sub One (Dig1 n) = - of_num (Dig0 n)"
haftmann@38054
   886
  "sub (Dig0 m) (Dig0 n) = dup (sub m n)"
haftmann@38054
   887
  "sub (Dig1 m) (Dig1 n) = dup (sub m n)"
haftmann@38054
   888
  "sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1"
haftmann@38054
   889
  "sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1"
haftmann@38054
   890
  by (simp_all add: algebra_simps num_eq_iff nat_of_num_add)
haftmann@38054
   891
haftmann@38054
   892
text {* Implementations *}
haftmann@38054
   893
haftmann@38054
   894
lemma one_int_code [code]:
haftmann@38054
   895
  "1 = Pls One"
haftmann@45154
   896
  by simp
haftmann@38054
   897
haftmann@38054
   898
lemma plus_int_code [code]:
haftmann@38054
   899
  "k + 0 = (k::int)"
haftmann@38054
   900
  "0 + l = (l::int)"
haftmann@38054
   901
  "Pls m + Pls n = Pls (m + n)"
haftmann@38054
   902
  "Pls m + Mns n = sub m n"
haftmann@38054
   903
  "Mns m + Pls n = sub n m"
haftmann@38054
   904
  "Mns m + Mns n = Mns (m + n)"
haftmann@38054
   905
  by simp_all
haftmann@38054
   906
haftmann@38054
   907
lemma uminus_int_code [code]:
haftmann@38054
   908
  "uminus 0 = (0::int)"
haftmann@38054
   909
  "uminus (Pls m) = Mns m"
haftmann@38054
   910
  "uminus (Mns m) = Pls m"
haftmann@38054
   911
  by simp_all
haftmann@38054
   912
haftmann@38054
   913
lemma minus_int_code [code]:
haftmann@38054
   914
  "k - 0 = (k::int)"
haftmann@38054
   915
  "0 - l = uminus (l::int)"
haftmann@38054
   916
  "Pls m - Pls n = sub m n"
haftmann@38054
   917
  "Pls m - Mns n = Pls (m + n)"
haftmann@38054
   918
  "Mns m - Pls n = Mns (m + n)"
haftmann@38054
   919
  "Mns m - Mns n = sub n m"
haftmann@38054
   920
  by simp_all
haftmann@38054
   921
haftmann@38054
   922
lemma times_int_code [code]:
haftmann@38054
   923
  "k * 0 = (0::int)"
haftmann@38054
   924
  "0 * l = (0::int)"
haftmann@38054
   925
  "Pls m * Pls n = Pls (m * n)"
haftmann@38054
   926
  "Pls m * Mns n = Mns (m * n)"
haftmann@38054
   927
  "Mns m * Pls n = Mns (m * n)"
haftmann@38054
   928
  "Mns m * Mns n = Pls (m * n)"
haftmann@38054
   929
  by simp_all
haftmann@38054
   930
haftmann@38054
   931
lemma eq_int_code [code]:
haftmann@38857
   932
  "HOL.equal 0 (0::int) \<longleftrightarrow> True"
haftmann@38857
   933
  "HOL.equal 0 (Pls l) \<longleftrightarrow> False"
haftmann@38857
   934
  "HOL.equal 0 (Mns l) \<longleftrightarrow> False"
haftmann@38857
   935
  "HOL.equal (Pls k) 0 \<longleftrightarrow> False"
haftmann@38857
   936
  "HOL.equal (Pls k) (Pls l) \<longleftrightarrow> HOL.equal k l"
haftmann@38857
   937
  "HOL.equal (Pls k) (Mns l) \<longleftrightarrow> False"
haftmann@38857
   938
  "HOL.equal (Mns k) 0 \<longleftrightarrow> False"
haftmann@38857
   939
  "HOL.equal (Mns k) (Pls l) \<longleftrightarrow> False"
haftmann@38857
   940
  "HOL.equal (Mns k) (Mns l) \<longleftrightarrow> HOL.equal k l"
haftmann@38857
   941
  by (auto simp add: equal dest: sym)
haftmann@38857
   942
haftmann@38857
   943
lemma [code nbe]:
haftmann@38857
   944
  "HOL.equal (k::int) k \<longleftrightarrow> True"
haftmann@38857
   945
  by (fact equal_refl)
haftmann@38054
   946
haftmann@38054
   947
lemma less_eq_int_code [code]:
haftmann@38054
   948
  "0 \<le> (0::int) \<longleftrightarrow> True"
haftmann@38054
   949
  "0 \<le> Pls l \<longleftrightarrow> True"
haftmann@38054
   950
  "0 \<le> Mns l \<longleftrightarrow> False"
haftmann@38054
   951
  "Pls k \<le> 0 \<longleftrightarrow> False"
haftmann@38054
   952
  "Pls k \<le> Pls l \<longleftrightarrow> k \<le> l"
haftmann@38054
   953
  "Pls k \<le> Mns l \<longleftrightarrow> False"
haftmann@38054
   954
  "Mns k \<le> 0 \<longleftrightarrow> True"
haftmann@38054
   955
  "Mns k \<le> Pls l \<longleftrightarrow> True"
haftmann@38054
   956
  "Mns k \<le> Mns l \<longleftrightarrow> l \<le> k"
haftmann@38054
   957
  by simp_all
haftmann@38054
   958
haftmann@38054
   959
lemma less_int_code [code]:
haftmann@38054
   960
  "0 < (0::int) \<longleftrightarrow> False"
haftmann@38054
   961
  "0 < Pls l \<longleftrightarrow> True"
haftmann@38054
   962
  "0 < Mns l \<longleftrightarrow> False"
haftmann@38054
   963
  "Pls k < 0 \<longleftrightarrow> False"
haftmann@38054
   964
  "Pls k < Pls l \<longleftrightarrow> k < l"
haftmann@38054
   965
  "Pls k < Mns l \<longleftrightarrow> False"
haftmann@38054
   966
  "Mns k < 0 \<longleftrightarrow> True"
haftmann@38054
   967
  "Mns k < Pls l \<longleftrightarrow> True"
haftmann@38054
   968
  "Mns k < Mns l \<longleftrightarrow> l < k"
haftmann@38054
   969
  by simp_all
haftmann@38054
   970
haftmann@38054
   971
hide_const (open) sub dup
haftmann@38054
   972
haftmann@38054
   973
text {* Pretty literals *}
haftmann@38054
   974
haftmann@38054
   975
ML {*
haftmann@38054
   976
local open Code_Thingol in
haftmann@38054
   977
haftmann@38054
   978
fun add_code print target =
haftmann@38054
   979
  let
haftmann@38054
   980
    fun dest_num one' dig0' dig1' thm =
haftmann@38054
   981
      let
haftmann@38054
   982
        fun dest_dig (IConst (c, _)) = if c = dig0' then 0
haftmann@38054
   983
              else if c = dig1' then 1
haftmann@38054
   984
              else Code_Printer.eqn_error thm "Illegal numeral expression: illegal dig"
haftmann@38054
   985
          | dest_dig _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal digit";
haftmann@38054
   986
        fun dest_num (IConst (c, _)) = if c = one' then 1
haftmann@38054
   987
              else Code_Printer.eqn_error thm "Illegal numeral expression: illegal leading digit"
haftmann@38054
   988
          | dest_num (t1 `$ t2) = 2 * dest_num t2 + dest_dig t1
haftmann@38054
   989
          | dest_num _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal term";
haftmann@38054
   990
      in dest_num end;
haftmann@38054
   991
    fun pretty sgn literals [one', dig0', dig1'] _ thm _ _ [(t, _)] =
haftmann@38054
   992
      (Code_Printer.str o print literals o sgn o dest_num one' dig0' dig1' thm) t
haftmann@38923
   993
    fun add_syntax (c, sgn) = Code_Target.add_const_syntax target c
haftmann@38054
   994
      (SOME (Code_Printer.complex_const_syntax
haftmann@38054
   995
        (1, ([@{const_name One}, @{const_name Dig0}, @{const_name Dig1}],
haftmann@38054
   996
          pretty sgn))));
haftmann@38054
   997
  in
haftmann@38054
   998
    add_syntax (@{const_name Pls}, I)
haftmann@38054
   999
    #> add_syntax (@{const_name Mns}, (fn k => ~ k))
haftmann@38054
  1000
  end;
haftmann@38054
  1001
haftmann@38054
  1002
end
haftmann@38054
  1003
*}
haftmann@38054
  1004
haftmann@38054
  1005
hide_const (open) One Dig0 Dig1
haftmann@38054
  1006
haftmann@38054
  1007
huffman@31025
  1008
subsection {* Toy examples *}
haftmann@28021
  1009
haftmann@38054
  1010
definition "foo \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat)"
haftmann@38054
  1011
definition "bar \<longleftrightarrow> #4 * #2 + #7 \<ge> (#8 :: int) - #3"
haftmann@38054
  1012
haftmann@38054
  1013
code_thms foo bar
haftmann@38054
  1014
export_code foo bar checking SML OCaml? Haskell? Scala?
haftmann@38054
  1015
haftmann@38054
  1016
text {* This is an ad-hoc @{text Code_Integer} setup. *}
haftmann@38054
  1017
haftmann@38054
  1018
setup {*
haftmann@38054
  1019
  fold (add_code Code_Printer.literal_numeral)
haftmann@38054
  1020
    [Code_ML.target_SML, Code_ML.target_OCaml, Code_Haskell.target, Code_Scala.target]
haftmann@38054
  1021
*}
haftmann@38054
  1022
haftmann@38054
  1023
code_type int
haftmann@38054
  1024
  (SML "IntInf.int")
haftmann@38054
  1025
  (OCaml "Big'_int.big'_int")
haftmann@38054
  1026
  (Haskell "Integer")
haftmann@38054
  1027
  (Scala "BigInt")
haftmann@38054
  1028
  (Eval "int")
haftmann@38054
  1029
haftmann@38054
  1030
code_const "0::int"
haftmann@38054
  1031
  (SML "0/ :/ IntInf.int")
haftmann@38054
  1032
  (OCaml "Big'_int.zero")
haftmann@38054
  1033
  (Haskell "0")
haftmann@38054
  1034
  (Scala "BigInt(0)")
haftmann@38054
  1035
  (Eval "0/ :/ int")
haftmann@38054
  1036
haftmann@38054
  1037
code_const Int.pred
haftmann@38054
  1038
  (SML "IntInf.- ((_), 1)")
haftmann@38054
  1039
  (OCaml "Big'_int.pred'_big'_int")
haftmann@38054
  1040
  (Haskell "!(_/ -/ 1)")
haftmann@38773
  1041
  (Scala "!(_ -/ 1)")
haftmann@38054
  1042
  (Eval "!(_/ -/ 1)")
haftmann@38054
  1043
haftmann@38054
  1044
code_const Int.succ
haftmann@38054
  1045
  (SML "IntInf.+ ((_), 1)")
haftmann@38054
  1046
  (OCaml "Big'_int.succ'_big'_int")
haftmann@38054
  1047
  (Haskell "!(_/ +/ 1)")
haftmann@38773
  1048
  (Scala "!(_ +/ 1)")
haftmann@38054
  1049
  (Eval "!(_/ +/ 1)")
haftmann@38054
  1050
haftmann@38054
  1051
code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@38054
  1052
  (SML "IntInf.+ ((_), (_))")
haftmann@38054
  1053
  (OCaml "Big'_int.add'_big'_int")
haftmann@38054
  1054
  (Haskell infixl 6 "+")
haftmann@38054
  1055
  (Scala infixl 7 "+")
haftmann@38054
  1056
  (Eval infixl 8 "+")
haftmann@37826
  1057
haftmann@38054
  1058
code_const "uminus \<Colon> int \<Rightarrow> int"
haftmann@38054
  1059
  (SML "IntInf.~")
haftmann@38054
  1060
  (OCaml "Big'_int.minus'_big'_int")
haftmann@38054
  1061
  (Haskell "negate")
haftmann@38054
  1062
  (Scala "!(- _)")
haftmann@38054
  1063
  (Eval "~/ _")
haftmann@38054
  1064
haftmann@38054
  1065
code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@38054
  1066
  (SML "IntInf.- ((_), (_))")
haftmann@38054
  1067
  (OCaml "Big'_int.sub'_big'_int")
haftmann@38054
  1068
  (Haskell infixl 6 "-")
haftmann@38054
  1069
  (Scala infixl 7 "-")
haftmann@38054
  1070
  (Eval infixl 8 "-")
haftmann@38054
  1071
haftmann@38054
  1072
code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@38054
  1073
  (SML "IntInf.* ((_), (_))")
haftmann@38054
  1074
  (OCaml "Big'_int.mult'_big'_int")
haftmann@38054
  1075
  (Haskell infixl 7 "*")
haftmann@38054
  1076
  (Scala infixl 8 "*")
haftmann@38054
  1077
  (Eval infixl 9 "*")
haftmann@38054
  1078
haftmann@38054
  1079
code_const pdivmod
haftmann@38054
  1080
  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
haftmann@38054
  1081
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
haftmann@38054
  1082
  (Haskell "divMod/ (abs _)/ (abs _)")
haftmann@38054
  1083
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
haftmann@38054
  1084
  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
haftmann@37826
  1085
haftmann@38857
  1086
code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@38054
  1087
  (SML "!((_ : IntInf.int) = _)")
haftmann@38054
  1088
  (OCaml "Big'_int.eq'_big'_int")
haftmann@39272
  1089
  (Haskell infix 4 "==")
haftmann@38054
  1090
  (Scala infixl 5 "==")
haftmann@38054
  1091
  (Eval infixl 6 "=")
haftmann@38054
  1092
haftmann@38054
  1093
code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@38054
  1094
  (SML "IntInf.<= ((_), (_))")
haftmann@38054
  1095
  (OCaml "Big'_int.le'_big'_int")
haftmann@38054
  1096
  (Haskell infix 4 "<=")
haftmann@38054
  1097
  (Scala infixl 4 "<=")
haftmann@38054
  1098
  (Eval infixl 6 "<=")
haftmann@38054
  1099
haftmann@38054
  1100
code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@38054
  1101
  (SML "IntInf.< ((_), (_))")
haftmann@38054
  1102
  (OCaml "Big'_int.lt'_big'_int")
haftmann@38054
  1103
  (Haskell infix 4 "<")
haftmann@38054
  1104
  (Scala infixl 4 "<")
haftmann@38054
  1105
  (Eval infixl 6 "<")
haftmann@38054
  1106
haftmann@38054
  1107
code_const Code_Numeral.int_of
haftmann@38054
  1108
  (SML "IntInf.fromInt")
haftmann@38054
  1109
  (OCaml "_")
haftmann@38054
  1110
  (Haskell "toInteger")
haftmann@38054
  1111
  (Scala "!_.as'_BigInt")
haftmann@38054
  1112
  (Eval "_")
haftmann@38054
  1113
haftmann@38054
  1114
export_code foo bar checking SML OCaml? Haskell? Scala?
haftmann@28021
  1115
haftmann@28021
  1116
end