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