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