src/HOL/Integ/Numeral.thy
author haftmann
Sat, 19 May 2007 11:33:30 +0200
changeset 23024 70435ffe077d
parent 22997 d4f3b015b50b
permissions -rw-r--r--
fixed text
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
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    10
imports IntDef
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22921
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
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    14
subsection {* Binary representation *}
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    15
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    16
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    17
  This formalization defines binary arithmetic in terms of the integers
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    18
  rather than using a datatype. This avoids multiple representations (leading
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    19
  zeroes, etc.)  See @{text "ZF/Integ/twos-compl.ML"}, function @{text
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    20
  int_of_binary}, for the numerical interpretation.
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    21
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    22
  The representation expects that @{text "(m mod 2)"} is 0 or 1,
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    23
  even if m is negative;
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    24
  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
    25
  @{text "-5 = (-3)*2 + 1"}.
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    26
*}
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    27
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    28
datatype bit = B0 | B1
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    29
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    30
text{*
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    31
  Type @{typ bit} avoids the use of type @{typ bool}, which would make
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    32
  all of the rewrite rules higher-order.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    33
*}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    34
21779
6d44dbae4bfa authentic syntax for Pls/Min/Bit;
wenzelm
parents: 21404
diff changeset
    35
definition
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21779
diff changeset
    36
  Pls :: int where
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22801
diff changeset
    37
  [code func del]:"Pls = 0"
21779
6d44dbae4bfa authentic syntax for Pls/Min/Bit;
wenzelm
parents: 21404
diff changeset
    38
6d44dbae4bfa authentic syntax for Pls/Min/Bit;
wenzelm
parents: 21404
diff changeset
    39
definition
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21779
diff changeset
    40
  Min :: int where
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22801
diff changeset
    41
  [code func del]:"Min = - 1"
21779
6d44dbae4bfa authentic syntax for Pls/Min/Bit;
wenzelm
parents: 21404
diff changeset
    42
6d44dbae4bfa authentic syntax for Pls/Min/Bit;
wenzelm
parents: 21404
diff changeset
    43
definition
6d44dbae4bfa authentic syntax for Pls/Min/Bit;
wenzelm
parents: 21404
diff changeset
    44
  Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22801
diff changeset
    45
  [code func del]: "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
    46
22473
753123c89d72 explizit "type" superclass
haftmann
parents: 22187
diff changeset
    47
class number = type + -- {* for numeric types: nat, int, real, \dots *}
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21779
diff changeset
    48
  fixes number_of :: "int \<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
syntax
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    51
  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    52
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22921
diff changeset
    53
use "../Tools/numeral_syntax.ML"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    54
setup NumeralSyntax.setup
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    55
19380
b808efaa5828 tuned syntax/abbreviations;
wenzelm
parents: 16417
diff changeset
    56
abbreviation
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    57
  "Numeral0 \<equiv> number_of Pls"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    58
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    59
abbreviation
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    60
  "Numeral1 \<equiv> number_of (Pls BIT B1)"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    61
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    62
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
    63
  -- {* Unfold all @{text let}s involving constants *}
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    64
  unfolding Let_def ..
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    65
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    66
lemma Let_0 [simp]: "Let 0 f = f 0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    67
  unfolding Let_def ..
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    68
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    69
lemma Let_1 [simp]: "Let 1 f = f 1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    70
  unfolding Let_def ..
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    71
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    72
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    73
  succ :: "int \<Rightarrow> int" where
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22801
diff changeset
    74
  [code func del]: "succ k = k + 1"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    75
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    76
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 20900
diff changeset
    77
  pred :: "int \<Rightarrow> int" where
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22801
diff changeset
    78
  [code func del]: "pred k = k - 1"
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    79
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    80
lemmas
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    81
  max_number_of [simp] = max_def
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    82
    [of "number_of u" "number_of v", standard, simp]
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    83
and
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    84
  min_number_of [simp] = min_def 
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    85
    [of "number_of u" "number_of v", standard, simp]
22187
a2c4861363d5 Allows evaluation of min/max o numerals.
nipkow
parents: 21834
diff changeset
    86
  -- {* unfolding @{text minx} and @{text max} on numerals *}
a2c4861363d5 Allows evaluation of min/max o numerals.
nipkow
parents: 21834
diff changeset
    87
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    88
lemmas numeral_simps = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    89
  succ_def pred_def Pls_def Min_def Bit_def
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
    90
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    91
text {* Removal of leading zeroes *}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    92
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    93
lemma Pls_0_eq [simp, normal post]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    94
  "Pls BIT B0 = Pls"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    95
  unfolding numeral_simps by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    96
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
    97
lemma Min_1_eq [simp, normal post]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    98
  "Min BIT B1 = Min"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
    99
  unfolding numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   100
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   101
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   102
subsection {* The Functions @{term succ}, @{term pred} and @{term uminus} *}
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_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   105
  "succ Pls = Pls 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 succ_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   109
  "succ Min = Pls"
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 succ_1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   113
  "succ (k BIT B1) = succ k 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 succ_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   117
  "succ (k BIT B0) = k BIT B1"
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_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   121
  "pred Pls = Min"
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 pred_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   125
  "pred Min = Min BIT B0"
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 pred_1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   129
  "pred (k BIT B1) = k BIT B0"
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 pred_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   133
  "pred (k BIT B0) = 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_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   137
  "- Pls = Pls"
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
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   140
lemma minus_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   141
  "- Min = Pls BIT B1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   142
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   143
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   144
lemma minus_1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   145
  "- (k BIT B1) = pred (- k) BIT B1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   146
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   147
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   148
lemma minus_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   149
  "- (k BIT B0) = (- k) BIT B0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   150
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   151
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   152
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   153
subsection {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   154
  Binary Addition and Multiplication: @{term "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   155
    and @{term "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   156
*}
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_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   159
  "Pls + k = k"
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_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   163
  "Min + k = pred k"
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_BIT_11 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   167
  "(k BIT B1) + (l BIT B1) = (k + succ l) BIT B0"
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_BIT_10 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   171
  "(k BIT B1) + (l BIT B0) = (k + l) BIT B1"
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 add_BIT_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   175
  "(k BIT B0) + (l BIT b) = (k + l) BIT b"
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 add_Pls_right [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   179
  "k + Pls = 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 add_Min_right [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   183
  "k + Min = pred k"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   184
  unfolding numeral_simps 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_Pls [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   187
  "Pls * w = Pls"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   188
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   189
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   190
lemma mult_Min [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   191
  "Min * k = - k"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   192
  unfolding numeral_simps by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   193
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   194
lemma mult_num1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   195
  "(k BIT B1) * l = ((k * l) BIT B0) + l"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   196
  unfolding numeral_simps int_distrib by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   197
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   198
lemma mult_num0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   199
  "(k BIT B0) * l = (k * l) BIT B0"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   200
  unfolding numeral_simps int_distrib by simp 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   201
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   202
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   203
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   204
subsection {* Converting Numerals to Rings: @{term number_of} *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   205
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   206
axclass number_ring \<subseteq> number, comm_ring_1
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   207
  number_of_eq: "number_of k = of_int k"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   208
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   209
text {* self-embedding of the intergers *}
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   210
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   211
instance int :: number_ring
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   212
  int_number_of_def: "number_of w \<equiv> of_int w"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   213
  by intro_classes (simp only: int_number_of_def)
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   214
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22801
diff changeset
   215
lemmas [code func del] = int_number_of_def
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   216
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   217
lemma number_of_is_id:
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   218
  "number_of (k::int) = k"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   219
  unfolding int_number_of_def by simp
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   220
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   221
lemma number_of_succ:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   222
  "number_of (succ k) = (1 + number_of k ::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   223
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   224
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   225
lemma number_of_pred:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   226
  "number_of (pred w) = (- 1 + number_of w ::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   227
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   228
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   229
lemma number_of_minus:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   230
  "number_of (uminus w) = (- (number_of w)::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   231
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   232
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   233
lemma number_of_add:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   234
  "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
   235
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   236
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   237
lemma number_of_mult:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   238
  "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
   239
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   240
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   241
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   242
  The correctness of shifting.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   243
  But it doesn't seem to give a measurable speed-up.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   244
*}
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
lemma double_number_of_BIT:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   247
  "(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
   248
  unfolding number_of_eq numeral_simps left_distrib by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   249
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   250
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   251
  Converting numerals 0 and 1 to their abstract versions.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   252
*}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   253
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   254
lemma numeral_0_eq_0 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   255
  "Numeral0 = (0::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   256
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   257
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   258
lemma numeral_1_eq_1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   259
  "Numeral1 = (1::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   260
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   261
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   262
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   263
  Special-case simplification for small constants.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   264
*}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   265
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   266
text{*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   267
  Unary minus for the abstract constant 1. Cannot be inserted
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   268
  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
   269
*}
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 numeral_m1_eq_minus_1:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   272
  "(-1::'a::number_ring) = - 1"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   273
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   274
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   275
lemma mult_minus1 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   276
  "-1 * z = -(z::'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 mult_minus1_right [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   280
  "z * -1 = -(z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   281
  unfolding number_of_eq numeral_simps by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   282
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   283
(*Negation of a coefficient*)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   284
lemma minus_number_of_mult [simp]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   285
   "- (number_of w) * z = number_of (uminus w) * (z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   286
   unfolding number_of_eq by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   287
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   288
text {* Subtraction *}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   289
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   290
lemma diff_number_of_eq:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   291
  "number_of v - number_of w =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   292
    (number_of (v + uminus w)::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   293
  unfolding number_of_eq by simp
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 number_of_Pls:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   296
  "number_of Pls = (0::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   297
  unfolding number_of_eq numeral_simps by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   298
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   299
lemma number_of_Min:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   300
  "number_of Min = (- 1::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   301
  unfolding number_of_eq numeral_simps by simp
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   302
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   303
lemma number_of_BIT:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   304
  "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
   305
    + (number_of w) + (number_of w)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   306
  unfolding number_of_eq numeral_simps by (simp split: bit.split)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   307
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   308
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   309
subsection {* Equality of Binary Numbers *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   310
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   311
text {* First version by Norbert Voelker *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   312
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   313
lemma eq_number_of_eq:
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   314
  "((number_of x::'a::number_ring) = number_of y) =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   315
   iszero (number_of (x + uminus y) :: 'a)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   316
  unfolding iszero_def number_of_add number_of_minus
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   317
  by (simp add: compare_rls)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   318
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   319
lemma iszero_number_of_Pls:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   320
  "iszero ((number_of Pls)::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   321
  unfolding iszero_def numeral_0_eq_0 ..
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   322
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   323
lemma nonzero_number_of_Min:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   324
  "~ iszero ((number_of Min)::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   325
  unfolding iszero_def numeral_m1_eq_minus_1 by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   326
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   327
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   328
subsection {* Comparisons, for Ordered Rings *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   329
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   330
lemma double_eq_0_iff:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   331
  "(a + a = 0) = (a = (0::'a::ordered_idom))"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   332
proof -
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   333
  have "a + a = (1 + 1) * a" unfolding left_distrib by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   334
  with zero_less_two [where 'a = 'a]
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   335
  show ?thesis by force
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   336
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   337
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   338
lemma le_imp_0_less: 
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   339
  assumes le: "0 \<le> z"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   340
  shows "(0::int) < 1 + z"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   341
proof -
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   342
  have "0 \<le> z" .
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   343
  also have "... < z + 1" by (rule less_add_one) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   344
  also have "... = 1 + z" by (simp add: add_ac)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   345
  finally show "0 < 1 + z" .
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   346
qed
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 odd_nonzero:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   349
  "1 + z + z \<noteq> (0::int)";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   350
proof (cases z rule: int_cases)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   351
  case (nonneg n)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   352
  have le: "0 \<le> z+z" by (simp add: nonneg add_increasing) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   353
  thus ?thesis using  le_imp_0_less [OF le]
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   354
    by (auto simp add: add_assoc) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   355
next
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   356
  case (neg n)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   357
  show ?thesis
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   358
  proof
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   359
    assume eq: "1 + z + z = 0"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   360
    have "0 < 1 + (int n + int n)"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   361
      by (simp add: le_imp_0_less add_increasing) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   362
    also have "... = - (1 + z + z)" 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   363
      by (simp add: neg add_assoc [symmetric]) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   364
    also have "... = 0" by (simp add: eq) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   365
    finally have "0<0" ..
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   366
    thus False by blast
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   367
  qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   368
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   369
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   370
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   371
22911
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   372
lemma Ints_double_eq_0_iff:
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   373
  assumes in_Ints: "a \<in> Ints"
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   374
  shows "(a + a = 0) = (a = (0::'a::ring_char_0))"
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   375
proof -
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   376
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   377
  then obtain z where a: "a = of_int z" ..
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   378
  show ?thesis
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   379
  proof
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   380
    assume "a = 0"
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   381
    thus "a + a = 0" by simp
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   382
  next
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   383
    assume eq: "a + a = 0"
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   384
    hence "of_int (z + z) = (of_int 0 :: 'a)" by (simp add: a)
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   385
    hence "z + z = 0" by (simp only: of_int_eq_iff)
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   386
    hence "z = 0" by (simp only: double_eq_0_iff)
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   387
    thus "a = 0" by (simp add: a)
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   388
  qed
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   389
qed
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   390
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   391
lemma Ints_odd_nonzero:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   392
  assumes in_Ints: "a \<in> Ints"
22911
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   393
  shows "1 + a + a \<noteq> (0::'a::ring_char_0)"
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   394
proof -
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   395
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   396
  then obtain z where a: "a = of_int z" ..
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   397
  show ?thesis
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   398
  proof
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   399
    assume eq: "1 + a + a = 0"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   400
    hence "of_int (1 + z + z) = (of_int 0 :: 'a)" by (simp add: a)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   401
    hence "1 + z + z = 0" by (simp only: of_int_eq_iff)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   402
    with odd_nonzero show False by blast
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   403
  qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   404
qed 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   405
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   406
lemma Ints_number_of:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   407
  "(number_of w :: 'a::number_ring) \<in> Ints"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   408
  unfolding number_of_eq Ints_def by simp
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   409
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   410
lemma iszero_number_of_BIT:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   411
  "iszero (number_of (w BIT x)::'a) = 
22911
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   412
   (x = B0 \<and> iszero (number_of w::'a::{ring_char_0,number_ring}))"
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   413
  by (simp add: iszero_def number_of_eq numeral_simps Ints_double_eq_0_iff 
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   414
    Ints_odd_nonzero Ints_def split: bit.split)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   415
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   416
lemma iszero_number_of_0:
22911
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   417
  "iszero (number_of (w BIT B0) :: 'a::{ring_char_0,number_ring}) = 
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   418
  iszero (number_of w :: 'a)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   419
  by (simp only: iszero_number_of_BIT simp_thms)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   420
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   421
lemma iszero_number_of_1:
22911
2f5e8d70a179 new axclass ring_char_0 for rings with characteristic 0, used for of_int_eq_iff and related lemmas
huffman
parents: 22845
diff changeset
   422
  "~ iszero (number_of (w BIT B1)::'a::{ring_char_0,number_ring})"
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   423
  by (simp add: iszero_number_of_BIT) 
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   424
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   425
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   426
subsection {* The Less-Than Relation *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   427
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   428
lemma less_number_of_eq_neg:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   429
  "((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   430
  = neg (number_of (x + uminus y) :: 'a)"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   431
apply (subst less_iff_diff_less_0) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   432
apply (simp add: neg_def diff_minus number_of_add number_of_minus)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   433
done
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   434
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   435
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   436
  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
   437
  @{term Numeral0} IS @{term "number_of Pls"}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   438
*}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   439
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   440
lemma not_neg_number_of_Pls:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   441
  "~ neg (number_of Pls ::'a::{ordered_idom,number_ring})"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   442
  by (simp add: neg_def numeral_0_eq_0)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   443
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   444
lemma neg_number_of_Min:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   445
  "neg (number_of Min ::'a::{ordered_idom,number_ring})"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   446
  by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   447
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   448
lemma double_less_0_iff:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   449
  "(a + a < 0) = (a < (0::'a::ordered_idom))"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   450
proof -
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   451
  have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   452
  also have "... = (a < 0)"
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   453
    by (simp add: mult_less_0_iff zero_less_two 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   454
                  order_less_not_sym [OF zero_less_two]) 
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   455
  finally show ?thesis .
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   456
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   457
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   458
lemma odd_less_0:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   459
  "(1 + z + z < 0) = (z < (0::int))";
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   460
proof (cases z rule: int_cases)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   461
  case (nonneg n)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   462
  thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   463
                             le_imp_0_less [THEN order_less_imp_le])  
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   464
next
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   465
  case (neg n)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   466
  thus ?thesis by (simp del: int_Suc
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   467
    add: int_Suc0_eq_1 [symmetric] zadd_int compare_rls)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   468
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   469
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   470
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   471
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   472
lemma Ints_odd_less_0: 
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   473
  assumes in_Ints: "a \<in> Ints"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   474
  shows "(1 + a + a < 0) = (a < (0::'a::ordered_idom))";
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   475
proof -
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   476
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   477
  then obtain z where a: "a = of_int z" ..
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   478
  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
   479
    by (simp add: a)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   480
  also have "... = (z < 0)" by (simp only: of_int_less_iff odd_less_0)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   481
  also have "... = (a < 0)" by (simp add: a)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   482
  finally show ?thesis .
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   483
qed
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   484
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   485
lemma neg_number_of_BIT:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   486
  "neg (number_of (w BIT x)::'a) = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   487
  neg (number_of w :: 'a::{ordered_idom,number_ring})"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   488
  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
   489
    Ints_odd_less_0 Ints_def split: bit.split)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   490
20596
haftmann
parents: 20500
diff changeset
   491
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   492
text {* Less-Than or Equals *}
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   493
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   494
text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   495
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   496
lemmas le_number_of_eq_not_less =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   497
  linorder_not_less [of "number_of w" "number_of v", symmetric, 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   498
  standard]
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   499
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   500
lemma le_number_of_eq:
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   501
    "((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
   502
     = (~ (neg (number_of (y + uminus x) :: 'a)))"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   503
by (simp add: le_number_of_eq_not_less less_number_of_eq_neg)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   504
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   505
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   506
text {* Absolute value (@{term abs}) *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   507
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   508
lemma abs_number_of:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   509
  "abs(number_of x::'a::{ordered_idom,number_ring}) =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   510
   (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
   511
  by (simp add: abs_if)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   512
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   513
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   514
text {* Re-orientation of the equation nnn=x *}
15013
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
lemma number_of_reorient:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   517
  "(number_of w = x) = (x = number_of w)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   518
  by auto
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   519
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   520
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   521
subsection {* Simplification of arithmetic operations on integer constants. *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   522
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   523
lemmas arith_extra_simps [standard, simp] =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   524
  number_of_add [symmetric]
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   525
  number_of_minus [symmetric] numeral_m1_eq_minus_1 [symmetric]
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   526
  number_of_mult [symmetric]
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   527
  diff_number_of_eq abs_number_of 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   528
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   529
text {*
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   530
  For making a minimal simpset, one must include these default simprules.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   531
  Also include @{text simp_thms}.
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   532
*}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   533
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   534
lemmas arith_simps = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   535
  bit.distinct
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   536
  Pls_0_eq Min_1_eq
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   537
  pred_Pls pred_Min pred_1 pred_0
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   538
  succ_Pls succ_Min succ_1 succ_0
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   539
  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
   540
  minus_Pls minus_Min minus_1 minus_0
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   541
  mult_Pls mult_Min mult_num1 mult_num0 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   542
  add_Pls_right add_Min_right
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   543
  abs_zero abs_one arith_extra_simps
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   544
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   545
text {* Simplification of relational operations *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   546
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   547
lemmas rel_simps [simp] = 
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   548
  eq_number_of_eq iszero_number_of_Pls nonzero_number_of_Min
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   549
  iszero_number_of_0 iszero_number_of_1
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   550
  less_number_of_eq_neg
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   551
  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
   552
  neg_number_of_Min neg_number_of_BIT
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   553
  le_number_of_eq
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   554
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   555
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   556
subsection {* Simplification of arithmetic when nested to the right. *}
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   557
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   558
lemma add_number_of_left [simp]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   559
  "number_of v + (number_of w + z) =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   560
   (number_of(v + w) + z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   561
  by (simp add: add_assoc [symmetric])
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   562
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   563
lemma mult_number_of_left [simp]:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   564
  "number_of v * (number_of w * z) =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   565
   (number_of(v * w) * z::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   566
  by (simp add: mult_assoc [symmetric])
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   567
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   568
lemma add_number_of_diff1:
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   569
  "number_of v + (number_of w - c) = 
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   570
  number_of(v + w) - (c::'a::number_ring)"
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   571
  by (simp add: diff_minus add_number_of_left)
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   572
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   573
lemma add_number_of_diff2 [simp]:
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   574
  "number_of v + (c - number_of w) =
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20355
diff changeset
   575
   number_of (v + uminus w) + (c::'a::number_ring)"
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   576
apply (subst diff_number_of_eq [symmetric])
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   577
apply (simp only: compare_rls)
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   578
done
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   579
19380
b808efaa5828 tuned syntax/abbreviations;
wenzelm
parents: 16417
diff changeset
   580
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   581
subsection {* Configuration of the code generator *}
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   582
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   583
instance int :: eq ..
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   584
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   585
code_datatype Pls Min Bit "number_of \<Colon> int \<Rightarrow> int"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   586
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   587
definition
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   588
  int_aux :: "int \<Rightarrow> nat \<Rightarrow> int" where
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   589
  "int_aux i n = (i + int n)"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   590
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   591
lemma [code]:
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   592
  "int_aux i 0 = i"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   593
  "int_aux i (Suc n) = int_aux (i + 1) n" -- {* tail recursive *}
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   594
  by (simp add: int_aux_def)+
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   595
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   596
lemma [code]:
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   597
  "int n = int_aux 0 n"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   598
  by (simp add: int_aux_def)
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   599
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   600
definition
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   601
  nat_aux :: "nat \<Rightarrow> int \<Rightarrow> nat" where
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   602
  "nat_aux n i = (n + nat i)"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   603
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   604
lemma [code]: "nat_aux n i = (if i <= 0 then n else nat_aux (Suc n) (i - 1))"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   605
  -- {* tail recursive *}
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   606
  by (auto simp add: nat_aux_def nat_eq_iff linorder_not_le order_less_imp_le
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   607
    dest: zless_imp_add1_zle)
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   608
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   609
lemma [code]: "nat i = nat_aux 0 i"
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   610
  by (simp add: nat_aux_def)
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   611
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   612
lemma zero_is_num_zero [code func, code inline, symmetric, normal post]:
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   613
  "(0\<Colon>int) = number_of Numeral.Pls" 
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   614
  by simp
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   615
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   616
lemma one_is_num_one [code func, code inline, symmetric, normal post]:
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   617
  "(1\<Colon>int) = number_of (Numeral.Pls BIT bit.B1)" 
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   618
  by simp 
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   619
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   620
code_modulename SML
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   621
  IntDef Integer
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   622
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   623
code_modulename OCaml
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   624
  IntDef Integer
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   625
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   626
code_modulename Haskell
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   627
  IntDef Integer
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   628
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   629
code_modulename SML
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   630
  Numeral Integer
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   631
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   632
code_modulename OCaml
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   633
  Numeral Integer
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   634
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   635
code_modulename Haskell
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   636
  Numeral Integer
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   637
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   638
(*FIXME: the IntInf.fromInt below hides a dependence on fixed-precision ints!*)
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   639
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   640
types_code
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   641
  "int" ("int")
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   642
attach (term_of) {*
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   643
val term_of_int = HOLogic.mk_number HOLogic.intT o IntInf.fromInt;
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   644
*}
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   645
attach (test) {*
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   646
fun gen_int i = one_of [~1, 1] * random_range 0 i;
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   647
*}
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   648
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   649
setup {*
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   650
let
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   651
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22921
diff changeset
   652
fun number_of_codegen thy defs gr dep module b (Const (@{const_name Numeral.number_of}, Type ("fun", [_, T])) $ t) =
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   653
      if T = HOLogic.intT then
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   654
        (SOME (fst (Codegen.invoke_tycodegen thy defs dep module false (gr, T)),
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   655
          (Pretty.str o IntInf.toString o HOLogic.dest_numeral) t) handle TERM _ => NONE)
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   656
      else if T = HOLogic.natT then
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   657
        SOME (Codegen.invoke_codegen thy defs dep module b (gr,
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   658
          Const ("IntDef.nat", HOLogic.intT --> HOLogic.natT) $
22997
d4f3b015b50b canonical prefixing of class constants
haftmann
parents: 22921
diff changeset
   659
            (Const (@{const_name Numeral.number_of}, HOLogic.intT --> HOLogic.intT) $ t)))
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   660
      else NONE
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   661
  | number_of_codegen _ _ _ _ _ _ _ = NONE;
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   662
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   663
in
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   664
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   665
Codegen.add_codegen "number_of_codegen" number_of_codegen
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   666
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   667
end
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   668
*}
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   669
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   670
consts_code
22921
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22911
diff changeset
   671
  "0 :: int"                   ("0")
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22911
diff changeset
   672
  "1 :: int"                   ("1")
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22911
diff changeset
   673
  "uminus :: int => int"       ("~")
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22911
diff changeset
   674
  "op + :: int => int => int"  ("(_ +/ _)")
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22911
diff changeset
   675
  "op * :: int => int => int"  ("(_ */ _)")
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22911
diff changeset
   676
  "op \<le> :: int => int => bool" ("(_ <=/ _)")
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22911
diff changeset
   677
  "op < :: int => int => bool" ("(_ </ _)")
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   678
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   679
quickcheck_params [default_type = int]
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   680
22921
475ff421a6a3 consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents: 22911
diff changeset
   681
(*setup continues in theory Presburger*)
22801
caffcb450ef4 cleaned up code generator setup for int
haftmann
parents: 22744
diff changeset
   682
20500
11da1ce8dbd8 hid succ, pred in Numeral.thy
haftmann
parents: 20485
diff changeset
   683
hide (open) const Pls Min B0 B1 succ pred
19380
b808efaa5828 tuned syntax/abbreviations;
wenzelm
parents: 16417
diff changeset
   684
15013
34264f5e4691 new treatment of binary numerals
paulson
parents:
diff changeset
   685
end