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