src/HOL/Integ/Numeral.thy
author wenzelm
Fri, 17 Nov 2006 02:20:03 +0100
changeset 21404 eb85850d3eb7
parent 20900 c1ba49ade6a5
child 21779 6d44dbae4bfa
permissions -rw-r--r--
more robust syntax for definition/abbreviation/notation;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
     1
(*  Title:      HOL/Integ/Numeral.thy
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
     2
    ID:         $Id$
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
     4
    Copyright   1994  University of Cambridge
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
     5
*)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
     6
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
     7
header {* Arithmetic on Binary Integers *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
     8
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15013
diff changeset
     9
theory Numeral
15620
8ccdc8bc66a2 replaced bool by a new datatype "bit" for binary numerals
paulson
parents: 15140
diff changeset
    10
imports IntDef Datatype
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 15620
diff changeset
    11
uses "../Tools/numeral_syntax.ML"
15131
c69542757a4d New theory header syntax.
nipkow
parents: 15013
diff changeset
    12
begin
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    13
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    14
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    15
  This formalization defines binary arithmetic in terms of the integers
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    16
  rather than using a datatype. This avoids multiple representations (leading
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    17
  zeroes, etc.)  See @{text "ZF/Integ/twos-compl.ML"}, function @{text
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    18
  int_of_binary}, for the numerical interpretation.
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    19
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    20
  The representation expects that @{text "(m mod 2)"} is 0 or 1,
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    21
  even if m is negative;
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    22
  For instance, @{text "-5 div 2 = -3"} and @{text "-5 mod 2 = 1"}; thus
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    23
  @{text "-5 = (-3)*2 + 1"}.
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    24
*}
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    25
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    26
text{*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    27
  This datatype avoids the use of type @{typ bool}, which would make
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    28
  all of the rewrite rules higher-order.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    29
*}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    30
15620
8ccdc8bc66a2 replaced bool by a new datatype "bit" for binary numerals
paulson
parents: 15140
diff changeset
    31
datatype bit = B0 | B1
8ccdc8bc66a2 replaced bool by a new datatype "bit" for binary numerals
paulson
parents: 15140
diff changeset
    32
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    33
constdefs
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    34
  Pls :: int
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    35
  "Pls == 0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    36
  Min :: int
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    37
  "Min == - 1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    38
  Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90)
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    39
  "k BIT b == (case b of B0 \<Rightarrow> 0 | B1 \<Rightarrow> 1) + k + k"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    40
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    41
axclass
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    42
  number < type  -- {* for numeric types: nat, int, real, \dots *}
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    43
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    44
consts
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    45
  number_of :: "int \<Rightarrow> 'a::number"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    46
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    47
syntax
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    48
  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    49
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    50
setup NumeralSyntax.setup
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    51
19380
b808efaa5828 tuned syntax/abbreviations;
wenzelm
parents: 16417
diff changeset
    52
abbreviation
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    53
  "Numeral0 \<equiv> number_of Pls"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    54
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    55
abbreviation
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    56
  "Numeral1 \<equiv> number_of (Pls BIT B1)"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    57
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    58
lemma Let_number_of [simp]: "Let (number_of v) f = f (number_of v)"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    59
  -- {* Unfold all @{text let}s involving constants *}
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    60
  unfolding Let_def ..
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    61
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    62
lemma Let_0 [simp]: "Let 0 f = f 0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    63
  unfolding Let_def ..
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    64
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    65
lemma Let_1 [simp]: "Let 1 f = f 1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    66
  unfolding Let_def ..
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    67
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    68
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    69
  succ :: "int \<Rightarrow> int" where
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    70
  "succ k = k + 1"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    71
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    72
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    73
  pred :: "int \<Rightarrow> int" where
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    74
  "pred k = k - 1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    75
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    76
lemmas numeral_simps = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    77
  succ_def pred_def Pls_def Min_def Bit_def
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    78
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    79
text {* Removal of leading zeroes *}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    80
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20596
diff changeset
    81
lemma Pls_0_eq [simp, code func]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    82
  "Pls BIT B0 = Pls"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    83
  unfolding numeral_simps by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    84
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20596
diff changeset
    85
lemma Min_1_eq [simp, code func]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    86
  "Min BIT B1 = Min"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    87
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    88
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    89
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    90
subsection {* The Functions @{term succ}, @{term pred} and @{term uminus} *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    91
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    92
lemma succ_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    93
  "succ Pls = Pls BIT B1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    94
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    95
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    96
lemma succ_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    97
  "succ Min = Pls"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    98
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    99
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   100
lemma succ_1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   101
  "succ (k BIT B1) = succ k BIT B0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   102
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   103
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   104
lemma succ_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   105
  "succ (k BIT B0) = k BIT B1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   106
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   107
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   108
lemma pred_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   109
  "pred Pls = Min"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   110
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   111
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   112
lemma pred_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   113
  "pred Min = Min BIT B0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   114
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   115
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   116
lemma pred_1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   117
  "pred (k BIT B1) = k BIT B0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   118
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   119
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   120
lemma pred_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   121
  "pred (k BIT B0) = pred k BIT B1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   122
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   123
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   124
lemma minus_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   125
  "- Pls = Pls"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   126
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   127
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   128
lemma minus_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   129
  "- Min = Pls BIT B1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   130
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   131
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   132
lemma minus_1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   133
  "- (k BIT B1) = pred (- k) BIT B1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   134
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   135
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   136
lemma minus_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   137
  "- (k BIT B0) = (- k) BIT B0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   138
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   139
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   140
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   141
subsection {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   142
  Binary Addition and Multiplication: @{term "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   143
    and @{term "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   144
*}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   145
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   146
lemma add_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   147
  "Pls + k = k"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   148
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   149
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   150
lemma add_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   151
  "Min + k = pred k"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   152
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   153
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   154
lemma add_BIT_11 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   155
  "(k BIT B1) + (l BIT B1) = (k + succ l) BIT B0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   156
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   157
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   158
lemma add_BIT_10 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   159
  "(k BIT B1) + (l BIT B0) = (k + l) BIT B1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   160
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   161
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   162
lemma add_BIT_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   163
  "(k BIT B0) + (l BIT b) = (k + l) BIT b"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   164
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   165
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   166
lemma add_Pls_right [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   167
  "k + Pls = k"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   168
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   169
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   170
lemma add_Min_right [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   171
  "k + Min = pred k"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   172
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   173
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   174
lemma mult_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   175
  "Pls * w = Pls"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   176
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   177
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   178
lemma mult_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   179
  "Min * k = - k"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   180
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   181
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   182
lemma mult_num1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   183
  "(k BIT B1) * l = ((k * l) BIT B0) + l"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   184
  unfolding numeral_simps int_distrib by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   185
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   186
lemma mult_num0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   187
  "(k BIT B0) * l = (k * l) BIT B0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   188
  unfolding numeral_simps int_distrib by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   189
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   190
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   191
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   192
subsection {* Converting Numerals to Rings: @{term number_of} *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   193
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   194
axclass number_ring \<subseteq> number, comm_ring_1
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   195
  number_of_eq: "number_of k = of_int k"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   196
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   197
lemma number_of_succ:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   198
  "number_of (succ k) = (1 + number_of k ::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   199
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   200
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   201
lemma number_of_pred:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   202
  "number_of (pred w) = (- 1 + number_of w ::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   203
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   204
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   205
lemma number_of_minus:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   206
  "number_of (uminus w) = (- (number_of w)::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   207
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   208
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   209
lemma number_of_add:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   210
  "number_of (v + w) = (number_of v + number_of w::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   211
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   212
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   213
lemma number_of_mult:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   214
  "number_of (v * w) = (number_of v * number_of w::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   215
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   216
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   217
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   218
  The correctness of shifting.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   219
  But it doesn't seem to give a measurable speed-up.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   220
*}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   221
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   222
lemma double_number_of_BIT:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   223
  "(1 + 1) * number_of w = (number_of (w BIT B0) ::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   224
  unfolding number_of_eq numeral_simps left_distrib by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   225
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   226
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   227
  Converting numerals 0 and 1 to their abstract versions.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   228
*}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   229
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   230
lemma numeral_0_eq_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   231
  "Numeral0 = (0::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   232
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   233
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   234
lemma numeral_1_eq_1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   235
  "Numeral1 = (1::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   236
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   237
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   238
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   239
  Special-case simplification for small constants.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   240
*}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   241
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   242
text{*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   243
  Unary minus for the abstract constant 1. Cannot be inserted
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   244
  as a simprule until later: it is @{text number_of_Min} re-oriented!
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   245
*}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   246
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   247
lemma numeral_m1_eq_minus_1:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   248
  "(-1::'a::number_ring) = - 1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   249
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   250
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   251
lemma mult_minus1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   252
  "-1 * z = -(z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   253
  unfolding number_of_eq numeral_simps by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   254
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   255
lemma mult_minus1_right [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   256
  "z * -1 = -(z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   257
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   258
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   259
(*Negation of a coefficient*)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   260
lemma minus_number_of_mult [simp]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   261
   "- (number_of w) * z = number_of (uminus w) * (z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   262
   unfolding number_of_eq by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   263
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   264
text {* Subtraction *}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   265
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   266
lemma diff_number_of_eq:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   267
  "number_of v - number_of w =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   268
    (number_of (v + uminus w)::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   269
  unfolding number_of_eq by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   270
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   271
lemma number_of_Pls:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   272
  "number_of Pls = (0::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   273
  unfolding number_of_eq numeral_simps by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   274
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   275
lemma number_of_Min:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   276
  "number_of Min = (- 1::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   277
  unfolding number_of_eq numeral_simps by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   278
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   279
lemma number_of_BIT:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   280
  "number_of(w BIT x) = (case x of B0 => 0 | B1 => (1::'a::number_ring))
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   281
    + (number_of w) + (number_of w)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   282
  unfolding number_of_eq numeral_simps by (simp split: bit.split)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   283
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   284
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   285
subsection {* Equality of Binary Numbers *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   286
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   287
text {* First version by Norbert Voelker *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   288
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   289
lemma eq_number_of_eq:
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   290
  "((number_of x::'a::number_ring) = number_of y) =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   291
   iszero (number_of (x + uminus y) :: 'a)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   292
  unfolding iszero_def number_of_add number_of_minus
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   293
  by (simp add: compare_rls)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   294
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   295
lemma iszero_number_of_Pls:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   296
  "iszero ((number_of Pls)::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   297
  unfolding iszero_def numeral_0_eq_0 ..
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   298
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   299
lemma nonzero_number_of_Min:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   300
  "~ iszero ((number_of Min)::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   301
  unfolding iszero_def numeral_m1_eq_minus_1 by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   302
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   303
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   304
subsection {* Comparisons, for Ordered Rings *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   305
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   306
lemma double_eq_0_iff:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   307
  "(a + a = 0) = (a = (0::'a::ordered_idom))"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   308
proof -
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   309
  have "a + a = (1 + 1) * a" unfolding left_distrib by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   310
  with zero_less_two [where 'a = 'a]
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   311
  show ?thesis by force
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   312
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   313
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   314
lemma le_imp_0_less: 
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   315
  assumes le: "0 \<le> z"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   316
  shows "(0::int) < 1 + z"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   317
proof -
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   318
  have "0 \<le> z" .
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   319
  also have "... < z + 1" by (rule less_add_one) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   320
  also have "... = 1 + z" by (simp add: add_ac)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   321
  finally show "0 < 1 + z" .
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   322
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   323
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   324
lemma odd_nonzero:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   325
  "1 + z + z \<noteq> (0::int)";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   326
proof (cases z rule: int_cases)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   327
  case (nonneg n)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   328
  have le: "0 \<le> z+z" by (simp add: nonneg add_increasing) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   329
  thus ?thesis using  le_imp_0_less [OF le]
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   330
    by (auto simp add: add_assoc) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   331
next
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   332
  case (neg n)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   333
  show ?thesis
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   334
  proof
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   335
    assume eq: "1 + z + z = 0"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   336
    have "0 < 1 + (int n + int n)"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   337
      by (simp add: le_imp_0_less add_increasing) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   338
    also have "... = - (1 + z + z)" 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   339
      by (simp add: neg add_assoc [symmetric]) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   340
    also have "... = 0" by (simp add: eq) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   341
    finally have "0<0" ..
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   342
    thus False by blast
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   343
  qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   344
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   345
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   346
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   347
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   348
lemma Ints_odd_nonzero:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   349
  assumes in_Ints: "a \<in> Ints"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   350
  shows "1 + a + a \<noteq> (0::'a::ordered_idom)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   351
proof -
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   352
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   353
  then obtain z where a: "a = of_int z" ..
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   354
  show ?thesis
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   355
  proof
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   356
    assume eq: "1 + a + a = 0"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   357
    hence "of_int (1 + z + z) = (of_int 0 :: 'a)" by (simp add: a)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   358
    hence "1 + z + z = 0" by (simp only: of_int_eq_iff)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   359
    with odd_nonzero show False by blast
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   360
  qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   361
qed 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   362
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   363
lemma Ints_number_of:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   364
  "(number_of w :: 'a::number_ring) \<in> Ints"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   365
  unfolding number_of_eq Ints_def by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   366
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   367
lemma iszero_number_of_BIT:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   368
  "iszero (number_of (w BIT x)::'a) = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   369
   (x = B0 \<and> iszero (number_of w::'a::{ordered_idom,number_ring}))"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   370
  by (simp add: iszero_def number_of_eq numeral_simps double_eq_0_iff 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   371
    Ints_odd_nonzero Ints_def split: bit.split)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   372
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   373
lemma iszero_number_of_0:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   374
  "iszero (number_of (w BIT B0) :: 'a::{ordered_idom,number_ring}) = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   375
  iszero (number_of w :: 'a)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   376
  by (simp only: iszero_number_of_BIT simp_thms)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   377
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   378
lemma iszero_number_of_1:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   379
  "~ iszero (number_of (w BIT B1)::'a::{ordered_idom,number_ring})"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   380
  by (simp add: iszero_number_of_BIT) 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   381
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   382
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   383
subsection {* The Less-Than Relation *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   384
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   385
lemma less_number_of_eq_neg:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   386
  "((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   387
  = neg (number_of (x + uminus y) :: 'a)"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   388
apply (subst less_iff_diff_less_0) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   389
apply (simp add: neg_def diff_minus number_of_add number_of_minus)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   390
done
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   391
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   392
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   393
  If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   394
  @{term Numeral0} IS @{term "number_of Pls"}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   395
*}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   396
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   397
lemma not_neg_number_of_Pls:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   398
  "~ neg (number_of Pls ::'a::{ordered_idom,number_ring})"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   399
  by (simp add: neg_def numeral_0_eq_0)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   400
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   401
lemma neg_number_of_Min:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   402
  "neg (number_of Min ::'a::{ordered_idom,number_ring})"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   403
  by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   404
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   405
lemma double_less_0_iff:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   406
  "(a + a < 0) = (a < (0::'a::ordered_idom))"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   407
proof -
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   408
  have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   409
  also have "... = (a < 0)"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   410
    by (simp add: mult_less_0_iff zero_less_two 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   411
                  order_less_not_sym [OF zero_less_two]) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   412
  finally show ?thesis .
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   413
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   414
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   415
lemma odd_less_0:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   416
  "(1 + z + z < 0) = (z < (0::int))";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   417
proof (cases z rule: int_cases)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   418
  case (nonneg n)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   419
  thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   420
                             le_imp_0_less [THEN order_less_imp_le])  
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   421
next
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   422
  case (neg n)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   423
  thus ?thesis by (simp del: int_Suc
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   424
    add: int_Suc0_eq_1 [symmetric] zadd_int compare_rls)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   425
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   426
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   427
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   428
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   429
lemma Ints_odd_less_0: 
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   430
  assumes in_Ints: "a \<in> Ints"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   431
  shows "(1 + a + a < 0) = (a < (0::'a::ordered_idom))";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   432
proof -
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   433
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   434
  then obtain z where a: "a = of_int z" ..
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   435
  hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   436
    by (simp add: a)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   437
  also have "... = (z < 0)" by (simp only: of_int_less_iff odd_less_0)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   438
  also have "... = (a < 0)" by (simp add: a)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   439
  finally show ?thesis .
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   440
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   441
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   442
lemma neg_number_of_BIT:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   443
  "neg (number_of (w BIT x)::'a) = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   444
  neg (number_of w :: 'a::{ordered_idom,number_ring})"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   445
  by (simp add: neg_def number_of_eq numeral_simps double_less_0_iff
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   446
    Ints_odd_less_0 Ints_def split: bit.split)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   447
20596
haftmann
parents: 20500
diff changeset
   448
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   449
text {* Less-Than or Equals *}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   450
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   451
text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   452
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   453
lemmas le_number_of_eq_not_less =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   454
  linorder_not_less [of "number_of w" "number_of v", symmetric, 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   455
  standard]
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   456
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   457
lemma le_number_of_eq:
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   458
    "((number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y)
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   459
     = (~ (neg (number_of (y + uminus x) :: 'a)))"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   460
by (simp add: le_number_of_eq_not_less less_number_of_eq_neg)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   461
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   462
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   463
text {* Absolute value (@{term abs}) *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   464
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   465
lemma abs_number_of:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   466
  "abs(number_of x::'a::{ordered_idom,number_ring}) =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   467
   (if number_of x < (0::'a) then -number_of x else number_of x)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   468
  by (simp add: abs_if)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   469
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   470
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   471
text {* Re-orientation of the equation nnn=x *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   472
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   473
lemma number_of_reorient:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   474
  "(number_of w = x) = (x = number_of w)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   475
  by auto
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   476
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   477
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   478
subsection {* Simplification of arithmetic operations on integer constants. *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   479
20900
c1ba49ade6a5 standardized facts;
wenzelm
parents: 20699
diff changeset
   480
lemmas arith_extra_simps [standard] =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   481
  number_of_add [symmetric]
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   482
  number_of_minus [symmetric] numeral_m1_eq_minus_1 [symmetric]
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   483
  number_of_mult [symmetric]
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   484
  diff_number_of_eq abs_number_of 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   486
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   487
  For making a minimal simpset, one must include these default simprules.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   488
  Also include @{text simp_thms}.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   489
*}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   490
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   491
lemmas arith_simps = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   492
  bit.distinct
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   493
  Pls_0_eq Min_1_eq
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   494
  pred_Pls pred_Min pred_1 pred_0
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   495
  succ_Pls succ_Min succ_1 succ_0
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   496
  add_Pls add_Min add_BIT_0 add_BIT_10 add_BIT_11
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   497
  minus_Pls minus_Min minus_1 minus_0
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   498
  mult_Pls mult_Min mult_num1 mult_num0 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   499
  add_Pls_right add_Min_right
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   500
  abs_zero abs_one arith_extra_simps
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   501
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   502
text {* Simplification of relational operations *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   503
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   504
lemmas rel_simps = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   505
  eq_number_of_eq iszero_number_of_Pls nonzero_number_of_Min
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   506
  iszero_number_of_0 iszero_number_of_1
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   507
  less_number_of_eq_neg
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   508
  not_neg_number_of_Pls not_neg_0 not_neg_1 not_iszero_1
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   509
  neg_number_of_Min neg_number_of_BIT
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   510
  le_number_of_eq
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   511
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   512
declare arith_extra_simps [simp]
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   513
declare rel_simps [simp]
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   514
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   515
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   516
subsection {* Simplification of arithmetic when nested to the right. *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   517
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   518
lemma add_number_of_left [simp]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   519
  "number_of v + (number_of w + z) =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   520
   (number_of(v + w) + z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   521
  by (simp add: add_assoc [symmetric])
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   522
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   523
lemma mult_number_of_left [simp]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   524
  "number_of v * (number_of w * z) =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   525
   (number_of(v * w) * z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   526
  by (simp add: mult_assoc [symmetric])
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   527
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   528
lemma add_number_of_diff1:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   529
  "number_of v + (number_of w - c) = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   530
  number_of(v + w) - (c::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   531
  by (simp add: diff_minus add_number_of_left)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   532
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   533
lemma add_number_of_diff2 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   534
  "number_of v + (c - number_of w) =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   535
   number_of (v + uminus w) + (c::'a::number_ring)"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   536
apply (subst diff_number_of_eq [symmetric])
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   537
apply (simp only: compare_rls)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   538
done
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   539
19380
b808efaa5828 tuned syntax/abbreviations;
wenzelm
parents: 16417
diff changeset
   540
20500
11da1ce8dbd8 hid succ, pred in Numeral.thy
haftmann
parents: 20485
diff changeset
   541
hide (open) const Pls Min B0 B1 succ pred
19380
b808efaa5828 tuned syntax/abbreviations;
wenzelm
parents: 16417
diff changeset
   542
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   543
end