src/HOL/Numeral.thy
author haftmann
Sat, 05 Jan 2008 09:16:27 +0100
changeset 25836 f7771e4f7064
parent 25571 c9e39eafc7a0
child 25885 6fbc3f54f819
permissions -rw-r--r--
more instantiation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Numeral.thy
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     4
    Copyright   1994  University of Cambridge
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     5
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     6
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     7
header {* Arithmetic on Binary Integers *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     8
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     9
theory Numeral
23708
b5eb0b4dd17d clarified import
haftmann
parents: 23574
diff changeset
    10
imports Datatype IntDef
23574
42765aff66d6 added Tools/numeral.ML;
wenzelm
parents: 23389
diff changeset
    11
uses
42765aff66d6 added Tools/numeral.ML;
wenzelm
parents: 23389
diff changeset
    12
  ("Tools/numeral.ML")
42765aff66d6 added Tools/numeral.ML;
wenzelm
parents: 23389
diff changeset
    13
  ("Tools/numeral_syntax.ML")
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    14
begin
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    15
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    16
subsection {* Binary representation *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    17
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    18
text {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    19
  This formalization defines binary arithmetic in terms of the integers
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    20
  rather than using a datatype. This avoids multiple representations (leading
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    21
  zeroes, etc.)  See @{text "ZF/Tools/twos-compl.ML"}, function @{text
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    22
  int_of_binary}, for the numerical interpretation.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    23
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    24
  The representation expects that @{text "(m mod 2)"} is 0 or 1,
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    25
  even if m is negative;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    26
  For instance, @{text "-5 div 2 = -3"} and @{text "-5 mod 2 = 1"}; thus
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    27
  @{text "-5 = (-3)*2 + 1"}.
25491
5760991891dd comment
paulson
parents: 25145
diff changeset
    28
  
5760991891dd comment
paulson
parents: 25145
diff changeset
    29
  This two's complement binary representation derives from the paper 
5760991891dd comment
paulson
parents: 25145
diff changeset
    30
  "An Efficient Representation of Arithmetic for Term Rewriting" by
5760991891dd comment
paulson
parents: 25145
diff changeset
    31
  Dave Cohen and Phil Watson, Rewriting Techniques and Applications,
5760991891dd comment
paulson
parents: 25145
diff changeset
    32
  Springer LNCS 488 (240-251), 1991.
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    33
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    34
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    35
datatype bit = B0 | B1
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    36
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    37
text{*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    38
  Type @{typ bit} avoids the use of type @{typ bool}, which would make
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    39
  all of the rewrite rules higher-order.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    40
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    41
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    42
definition
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    43
  Pls :: int where
24166
7b28dc69bdbb new nbe implementation
haftmann
parents: 23855
diff changeset
    44
  [code func del]: "Pls = 0"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    45
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    46
definition
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    47
  Min :: int where
24166
7b28dc69bdbb new nbe implementation
haftmann
parents: 23855
diff changeset
    48
  [code func del]: "Min = - 1"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    49
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    50
definition
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    51
  Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    52
  [code func del]: "k BIT b = (case b of B0 \<Rightarrow> 0 | B1 \<Rightarrow> 1) + k + k"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    53
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    54
class number = type + -- {* for numeric types: nat, int, real, \dots *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    55
  fixes number_of :: "int \<Rightarrow> 'a"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    56
23574
42765aff66d6 added Tools/numeral.ML;
wenzelm
parents: 23389
diff changeset
    57
use "Tools/numeral.ML"
42765aff66d6 added Tools/numeral.ML;
wenzelm
parents: 23389
diff changeset
    58
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    59
syntax
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    60
  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    61
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    62
use "Tools/numeral_syntax.ML"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    63
setup NumeralSyntax.setup
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    64
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    65
abbreviation
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    66
  "Numeral0 \<equiv> number_of Pls"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    67
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    68
abbreviation
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    69
  "Numeral1 \<equiv> number_of (Pls BIT B1)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    70
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    71
lemma Let_number_of [simp]: "Let (number_of v) f = f (number_of v)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    72
  -- {* Unfold all @{text let}s involving constants *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    73
  unfolding Let_def ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    74
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    75
definition
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    76
  succ :: "int \<Rightarrow> int" where
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    77
  [code func del]: "succ k = k + 1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    78
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    79
definition
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    80
  pred :: "int \<Rightarrow> int" where
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    81
  [code func del]: "pred k = k - 1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    82
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    83
lemmas
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    84
  max_number_of [simp] = max_def
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    85
    [of "number_of u" "number_of v", standard, simp]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    86
and
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    87
  min_number_of [simp] = min_def 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    88
    [of "number_of u" "number_of v", standard, simp]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    89
  -- {* unfolding @{text minx} and @{text max} on numerals *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    90
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    91
lemmas numeral_simps = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    92
  succ_def pred_def Pls_def Min_def Bit_def
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    93
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    94
text {* Removal of leading zeroes *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    95
24166
7b28dc69bdbb new nbe implementation
haftmann
parents: 23855
diff changeset
    96
lemma Pls_0_eq [simp, code post]:
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    97
  "Pls BIT B0 = Pls"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    98
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    99
24166
7b28dc69bdbb new nbe implementation
haftmann
parents: 23855
diff changeset
   100
lemma Min_1_eq [simp, code post]:
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   101
  "Min BIT B1 = Min"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   102
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   103
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   104
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   105
subsection {* The Functions @{term succ}, @{term pred} and @{term uminus} *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   106
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   107
lemma succ_Pls [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   108
  "succ Pls = Pls BIT B1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   109
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   110
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   111
lemma succ_Min [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   112
  "succ Min = Pls"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   113
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   114
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   115
lemma succ_1 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   116
  "succ (k BIT B1) = succ k BIT B0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   117
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   118
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   119
lemma succ_0 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   120
  "succ (k BIT B0) = k BIT B1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   121
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   122
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   123
lemma pred_Pls [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   124
  "pred Pls = Min"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   125
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   126
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   127
lemma pred_Min [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   128
  "pred Min = Min BIT B0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   129
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   130
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   131
lemma pred_1 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   132
  "pred (k BIT B1) = k BIT B0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   133
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   134
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   135
lemma pred_0 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   136
  "pred (k BIT B0) = pred k BIT B1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   137
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   138
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   139
lemma minus_Pls [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   140
  "- Pls = Pls"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   141
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   142
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   143
lemma minus_Min [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   144
  "- Min = Pls BIT B1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   145
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   146
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   147
lemma minus_1 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   148
  "- (k BIT B1) = pred (- k) BIT B1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   149
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   150
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   151
lemma minus_0 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   152
  "- (k BIT B0) = (- k) BIT B0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   153
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   154
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   155
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   156
subsection {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   157
  Binary Addition and Multiplication: @{term "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   158
    and @{term "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   159
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   160
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   161
lemma add_Pls [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   162
  "Pls + k = k"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   163
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   165
lemma add_Min [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   166
  "Min + k = pred k"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   167
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   168
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   169
lemma add_BIT_11 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   170
  "(k BIT B1) + (l BIT B1) = (k + succ l) BIT B0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   171
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   172
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   173
lemma add_BIT_10 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   174
  "(k BIT B1) + (l BIT B0) = (k + l) BIT B1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   175
  unfolding numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   176
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   177
lemma add_BIT_0 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   178
  "(k BIT B0) + (l BIT b) = (k + l) BIT b"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   179
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   180
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   181
lemma add_Pls_right [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   182
  "k + Pls = k"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   183
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   184
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   185
lemma add_Min_right [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   186
  "k + Min = pred k"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   187
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   188
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   189
lemma mult_Pls [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   190
  "Pls * w = Pls"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   191
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   192
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   193
lemma mult_Min [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   194
  "Min * k = - k"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   195
  unfolding numeral_simps by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   196
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   197
lemma mult_num1 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   198
  "(k BIT B1) * l = ((k * l) BIT B0) + l"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   199
  unfolding numeral_simps int_distrib by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   200
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   201
lemma mult_num0 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   202
  "(k BIT B0) * l = (k * l) BIT B0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   203
  unfolding numeral_simps int_distrib by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   204
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   205
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   206
subsection {* Converting Numerals to Rings: @{term number_of} *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   207
25502
9200b36280c0 instance command as rudimentary class target
haftmann
parents: 25491
diff changeset
   208
class number_ring = number + comm_ring_1 +
9200b36280c0 instance command as rudimentary class target
haftmann
parents: 25491
diff changeset
   209
  assumes number_of_eq: "number_of k = of_int k"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   210
23855
haftmann
parents: 23708
diff changeset
   211
text {* self-embedding of the integers *}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   212
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25502
diff changeset
   213
instantiation int :: number_ring
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25502
diff changeset
   214
begin
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25502
diff changeset
   215
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25502
diff changeset
   216
definition
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25502
diff changeset
   217
  int_number_of_def [code func del]: "number_of w = (of_int w \<Colon> int)"
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25502
diff changeset
   218
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25502
diff changeset
   219
instance
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   220
  by intro_classes (simp only: int_number_of_def)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   221
25571
c9e39eafc7a0 instantiation target rather than legacy instance
haftmann
parents: 25502
diff changeset
   222
end
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   223
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   224
lemma number_of_is_id:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   225
  "number_of (k::int) = k"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   226
  unfolding int_number_of_def by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   227
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   228
lemma number_of_succ:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   229
  "number_of (succ k) = (1 + number_of k ::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   230
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   231
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   232
lemma number_of_pred:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   233
  "number_of (pred w) = (- 1 + number_of w ::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   234
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   235
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   236
lemma number_of_minus:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   237
  "number_of (uminus w) = (- (number_of w)::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   238
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   239
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   240
lemma number_of_add:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   241
  "number_of (v + w) = (number_of v + number_of w::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   242
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   243
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   244
lemma number_of_mult:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   245
  "number_of (v * w) = (number_of v * number_of w::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   246
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   247
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   248
text {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   249
  The correctness of shifting.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   250
  But it doesn't seem to give a measurable speed-up.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   251
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   252
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   253
lemma double_number_of_BIT:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   254
  "(1 + 1) * number_of w = (number_of (w BIT B0) ::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   255
  unfolding number_of_eq numeral_simps left_distrib by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   256
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   257
text {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   258
  Converting numerals 0 and 1 to their abstract versions.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   259
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   260
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   261
lemma numeral_0_eq_0 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   262
  "Numeral0 = (0::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   263
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   264
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   265
lemma numeral_1_eq_1 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   266
  "Numeral1 = (1::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   267
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   268
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   269
text {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   270
  Special-case simplification for small constants.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   271
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   272
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   273
text{*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   274
  Unary minus for the abstract constant 1. Cannot be inserted
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   275
  as a simprule until later: it is @{text number_of_Min} re-oriented!
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   276
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   277
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   278
lemma numeral_m1_eq_minus_1:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   279
  "(-1::'a::number_ring) = - 1"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   280
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   281
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   282
lemma mult_minus1 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   283
  "-1 * z = -(z::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   284
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   285
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   286
lemma mult_minus1_right [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   287
  "z * -1 = -(z::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   288
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   289
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   290
(*Negation of a coefficient*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   291
lemma minus_number_of_mult [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   292
   "- (number_of w) * z = number_of (uminus w) * (z::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   293
   unfolding number_of_eq by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   294
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   295
text {* Subtraction *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   296
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   297
lemma diff_number_of_eq:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   298
  "number_of v - number_of w =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   299
    (number_of (v + uminus w)::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   300
  unfolding number_of_eq by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   301
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   302
lemma number_of_Pls:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   303
  "number_of Pls = (0::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   304
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   305
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   306
lemma number_of_Min:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   307
  "number_of Min = (- 1::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   308
  unfolding number_of_eq numeral_simps by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   309
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   310
lemma number_of_BIT:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   311
  "number_of(w BIT x) = (case x of B0 => 0 | B1 => (1::'a::number_ring))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   312
    + (number_of w) + (number_of w)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   313
  unfolding number_of_eq numeral_simps by (simp split: bit.split)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   314
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   315
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   316
subsection {* Equality of Binary Numbers *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   317
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   318
text {* First version by Norbert Voelker *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   319
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   320
lemma eq_number_of_eq:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   321
  "((number_of x::'a::number_ring) = number_of y) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   322
   iszero (number_of (x + uminus y) :: 'a)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   323
  unfolding iszero_def number_of_add number_of_minus
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   324
  by (simp add: compare_rls)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   325
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   326
lemma iszero_number_of_Pls:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   327
  "iszero ((number_of Pls)::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   328
  unfolding iszero_def numeral_0_eq_0 ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   329
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   330
lemma nonzero_number_of_Min:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   331
  "~ iszero ((number_of Min)::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   332
  unfolding iszero_def numeral_m1_eq_minus_1 by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   333
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   334
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   335
subsection {* Comparisons, for Ordered Rings *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   336
25089
04b8456f7754 dropped doubled proof
haftmann
parents: 24630
diff changeset
   337
lemmas double_eq_0_iff = double_zero
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   338
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   339
lemma le_imp_0_less: 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   340
  assumes le: "0 \<le> z"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   341
  shows "(0::int) < 1 + z"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   342
proof -
23389
aaca6a8e5414 tuned proofs: avoid implicit prems;
wenzelm
parents: 23365
diff changeset
   343
  have "0 \<le> z" by fact
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   344
  also have "... < z + 1" by (rule less_add_one) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   345
  also have "... = 1 + z" by (simp add: add_ac)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   346
  finally show "0 < 1 + z" .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   347
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   348
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   349
lemma odd_nonzero:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   350
  "1 + z + z \<noteq> (0::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   351
proof (cases z rule: int_cases)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   352
  case (nonneg n)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   353
  have le: "0 \<le> z+z" by (simp add: nonneg add_increasing) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   354
  thus ?thesis using  le_imp_0_less [OF le]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   355
    by (auto simp add: add_assoc) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   356
next
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   357
  case (neg n)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   358
  show ?thesis
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   359
  proof
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   360
    assume eq: "1 + z + z = 0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   361
    have "0 < 1 + (int n + int n)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   362
      by (simp add: le_imp_0_less add_increasing) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   363
    also have "... = - (1 + z + z)" 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   364
      by (simp add: neg add_assoc [symmetric]) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   365
    also have "... = 0" by (simp add: eq) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   366
    finally have "0<0" ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   367
    thus False by blast
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   368
  qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   369
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   370
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   371
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   372
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   373
lemma Ints_double_eq_0_iff:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   374
  assumes in_Ints: "a \<in> Ints"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   375
  shows "(a + a = 0) = (a = (0::'a::ring_char_0))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   376
proof -
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   377
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   378
  then obtain z where a: "a = of_int z" ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   379
  show ?thesis
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   380
  proof
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   381
    assume "a = 0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   382
    thus "a + a = 0" by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   383
  next
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   384
    assume eq: "a + a = 0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   385
    hence "of_int (z + z) = (of_int 0 :: 'a)" by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   386
    hence "z + z = 0" by (simp only: of_int_eq_iff)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   387
    hence "z = 0" by (simp only: double_eq_0_iff)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   388
    thus "a = 0" by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   389
  qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   390
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   391
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   392
lemma Ints_odd_nonzero:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   393
  assumes in_Ints: "a \<in> Ints"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   394
  shows "1 + a + a \<noteq> (0::'a::ring_char_0)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   395
proof -
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   396
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   397
  then obtain z where a: "a = of_int z" ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   398
  show ?thesis
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   399
  proof
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   400
    assume eq: "1 + a + a = 0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   401
    hence "of_int (1 + z + z) = (of_int 0 :: 'a)" by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   402
    hence "1 + z + z = 0" by (simp only: of_int_eq_iff)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   403
    with odd_nonzero show False by blast
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   404
  qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   405
qed 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   406
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   407
lemma Ints_number_of:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   408
  "(number_of w :: 'a::number_ring) \<in> Ints"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   409
  unfolding number_of_eq Ints_def by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   410
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   411
lemma iszero_number_of_BIT:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   412
  "iszero (number_of (w BIT x)::'a) = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   413
   (x = B0 \<and> iszero (number_of w::'a::{ring_char_0,number_ring}))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   414
  by (simp add: iszero_def number_of_eq numeral_simps Ints_double_eq_0_iff 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   415
    Ints_odd_nonzero Ints_def split: bit.split)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   416
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   417
lemma iszero_number_of_0:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   418
  "iszero (number_of (w BIT B0) :: 'a::{ring_char_0,number_ring}) = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   419
  iszero (number_of w :: 'a)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   420
  by (simp only: iszero_number_of_BIT simp_thms)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   421
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   422
lemma iszero_number_of_1:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   423
  "~ iszero (number_of (w BIT B1)::'a::{ring_char_0,number_ring})"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   424
  by (simp add: iszero_number_of_BIT) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   425
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   426
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   427
subsection {* The Less-Than Relation *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   428
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   429
lemma less_number_of_eq_neg:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   430
  "((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   431
  = neg (number_of (x + uminus y) :: 'a)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   432
apply (subst less_iff_diff_less_0) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   433
apply (simp add: neg_def diff_minus number_of_add number_of_minus)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   434
done
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   435
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   436
text {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   437
  If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   438
  @{term Numeral0} IS @{term "number_of Pls"}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   439
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   440
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   441
lemma not_neg_number_of_Pls:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   442
  "~ neg (number_of Pls ::'a::{ordered_idom,number_ring})"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   443
  by (simp add: neg_def numeral_0_eq_0)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   444
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   445
lemma neg_number_of_Min:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   446
  "neg (number_of Min ::'a::{ordered_idom,number_ring})"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   447
  by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   448
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   449
lemma double_less_0_iff:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   450
  "(a + a < 0) = (a < (0::'a::ordered_idom))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   451
proof -
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   452
  have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   453
  also have "... = (a < 0)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   454
    by (simp add: mult_less_0_iff zero_less_two 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   455
                  order_less_not_sym [OF zero_less_two]) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   456
  finally show ?thesis .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   457
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   458
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   459
lemma odd_less_0:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   460
  "(1 + z + z < 0) = (z < (0::int))";
23365
f31794033ae1 removed constant int :: nat => int;
huffman
parents: 23307
diff changeset
   461
proof (cases z rule: int_cases)
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   462
  case (nonneg n)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   463
  thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   464
                             le_imp_0_less [THEN order_less_imp_le])  
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   465
next
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   466
  case (neg n)
23307
2fe3345035c7 modify proofs to avoid referring to int::nat=>int
huffman
parents: 23164
diff changeset
   467
  thus ?thesis by (simp del: of_nat_Suc of_nat_add
2fe3345035c7 modify proofs to avoid referring to int::nat=>int
huffman
parents: 23164
diff changeset
   468
    add: compare_rls of_nat_1 [symmetric] of_nat_add [symmetric])
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   469
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   470
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   471
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   472
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   473
lemma Ints_odd_less_0: 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   474
  assumes in_Ints: "a \<in> Ints"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   475
  shows "(1 + a + a < 0) = (a < (0::'a::ordered_idom))";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   476
proof -
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   477
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   478
  then obtain z where a: "a = of_int z" ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   479
  hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   480
    by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   481
  also have "... = (z < 0)" by (simp only: of_int_less_iff odd_less_0)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   482
  also have "... = (a < 0)" by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   483
  finally show ?thesis .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   484
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   485
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   486
lemma neg_number_of_BIT:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   487
  "neg (number_of (w BIT x)::'a) = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   488
  neg (number_of w :: 'a::{ordered_idom,number_ring})"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   489
  by (simp add: neg_def number_of_eq numeral_simps double_less_0_iff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   490
    Ints_odd_less_0 Ints_def split: bit.split)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   491
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   492
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   493
text {* Less-Than or Equals *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   494
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   495
text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   496
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   497
lemmas le_number_of_eq_not_less =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   498
  linorder_not_less [of "number_of w" "number_of v", symmetric, 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   499
  standard]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   500
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   501
lemma le_number_of_eq:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   502
    "((number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   503
     = (~ (neg (number_of (y + uminus x) :: 'a)))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   504
by (simp add: le_number_of_eq_not_less less_number_of_eq_neg)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   505
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   506
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   507
text {* Absolute value (@{term abs}) *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   508
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   509
lemma abs_number_of:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   510
  "abs(number_of x::'a::{ordered_idom,number_ring}) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   511
   (if number_of x < (0::'a) then -number_of x else number_of x)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   512
  by (simp add: abs_if)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   513
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   514
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   515
text {* Re-orientation of the equation nnn=x *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   516
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   517
lemma number_of_reorient:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   518
  "(number_of w = x) = (x = number_of w)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   519
  by auto
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   520
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   521
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   522
subsection {* Simplification of arithmetic operations on integer constants. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   523
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   524
lemmas arith_extra_simps [standard, simp] =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   525
  number_of_add [symmetric]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   526
  number_of_minus [symmetric] numeral_m1_eq_minus_1 [symmetric]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   527
  number_of_mult [symmetric]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   528
  diff_number_of_eq abs_number_of 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   529
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   530
text {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   531
  For making a minimal simpset, one must include these default simprules.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   532
  Also include @{text simp_thms}.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   533
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   534
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   535
lemmas arith_simps = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   536
  bit.distinct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   537
  Pls_0_eq Min_1_eq
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   538
  pred_Pls pred_Min pred_1 pred_0
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   539
  succ_Pls succ_Min succ_1 succ_0
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   540
  add_Pls add_Min add_BIT_0 add_BIT_10 add_BIT_11
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   541
  minus_Pls minus_Min minus_1 minus_0
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   542
  mult_Pls mult_Min mult_num1 mult_num0 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   543
  add_Pls_right add_Min_right
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   544
  abs_zero abs_one arith_extra_simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   545
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   546
text {* Simplification of relational operations *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   547
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   548
lemmas rel_simps [simp] = 
24392
000327cea3eb replace iszero_number_of_Pls with iszero_0 in rel_simps
huffman
parents: 24166
diff changeset
   549
  eq_number_of_eq iszero_0 nonzero_number_of_Min
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   550
  iszero_number_of_0 iszero_number_of_1
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   551
  less_number_of_eq_neg
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   552
  not_neg_number_of_Pls not_neg_0 not_neg_1 not_iszero_1
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   553
  neg_number_of_Min neg_number_of_BIT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   554
  le_number_of_eq
24392
000327cea3eb replace iszero_number_of_Pls with iszero_0 in rel_simps
huffman
parents: 24166
diff changeset
   555
(* iszero_number_of_Pls would never be used
000327cea3eb replace iszero_number_of_Pls with iszero_0 in rel_simps
huffman
parents: 24166
diff changeset
   556
   because its lhs simplifies to "iszero 0" *)
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   557
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   558
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   559
subsection {* Simplification of arithmetic when nested to the right. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   560
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   561
lemma add_number_of_left [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   562
  "number_of v + (number_of w + z) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   563
   (number_of(v + w) + z::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   564
  by (simp add: add_assoc [symmetric])
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   565
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   566
lemma mult_number_of_left [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   567
  "number_of v * (number_of w * z) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   568
   (number_of(v * w) * z::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   569
  by (simp add: mult_assoc [symmetric])
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   570
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   571
lemma add_number_of_diff1:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   572
  "number_of v + (number_of w - c) = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   573
  number_of(v + w) - (c::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   574
  by (simp add: diff_minus add_number_of_left)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   575
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   576
lemma add_number_of_diff2 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   577
  "number_of v + (c - number_of w) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   578
   number_of (v + uminus w) + (c::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   579
apply (subst diff_number_of_eq [symmetric])
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   580
apply (simp only: compare_rls)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   581
done
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   582
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   583
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   584
subsection {* Configuration of the code generator *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   585
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   586
instance int :: eq ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   587
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   588
code_datatype Pls Min Bit "number_of \<Colon> int \<Rightarrow> int"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   589
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   590
definition
23855
haftmann
parents: 23708
diff changeset
   591
  int_aux :: "nat \<Rightarrow> int \<Rightarrow> int" where
haftmann
parents: 23708
diff changeset
   592
  "int_aux n i = int n + i"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   593
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   594
lemma [code]:
23855
haftmann
parents: 23708
diff changeset
   595
  "int_aux 0 i  = i"
haftmann
parents: 23708
diff changeset
   596
  "int_aux (Suc n) i = int_aux n (i + 1)" -- {* tail recursive *}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   597
  by (simp add: int_aux_def)+
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   598
25145
d432105e5bd0 dropped superfluous inlining lemmas
haftmann
parents: 25089
diff changeset
   599
lemma [code, code unfold, code inline del]:
23855
haftmann
parents: 23708
diff changeset
   600
  "int n = int_aux n 0"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   601
  by (simp add: int_aux_def)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   602
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   603
definition
23855
haftmann
parents: 23708
diff changeset
   604
  nat_aux :: "int \<Rightarrow> nat \<Rightarrow> nat" where
haftmann
parents: 23708
diff changeset
   605
  "nat_aux i n = nat i + n"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   606
23855
haftmann
parents: 23708
diff changeset
   607
lemma [code]:
haftmann
parents: 23708
diff changeset
   608
  "nat_aux i n = (if i \<le> 0 then n else nat_aux (i - 1) (Suc n))"  -- {* tail recursive *}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   609
  by (auto simp add: nat_aux_def nat_eq_iff linorder_not_le order_less_imp_le
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   610
    dest: zless_imp_add1_zle)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   611
23855
haftmann
parents: 23708
diff changeset
   612
lemma [code]: "nat i = nat_aux i 0"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   613
  by (simp add: nat_aux_def)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   614
24166
7b28dc69bdbb new nbe implementation
haftmann
parents: 23855
diff changeset
   615
lemma zero_is_num_zero [code func, code inline, symmetric, code post]:
23855
haftmann
parents: 23708
diff changeset
   616
  "(0\<Colon>int) = Numeral0" 
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   617
  by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   618
24166
7b28dc69bdbb new nbe implementation
haftmann
parents: 23855
diff changeset
   619
lemma one_is_num_one [code func, code inline, symmetric, code post]:
23855
haftmann
parents: 23708
diff changeset
   620
  "(1\<Colon>int) = Numeral1" 
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   621
  by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   622
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   623
code_modulename SML
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   624
  IntDef Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   625
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   626
code_modulename OCaml
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   627
  IntDef Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   628
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   629
code_modulename Haskell
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   630
  IntDef Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   631
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   632
code_modulename SML
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   633
  Numeral Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   634
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   635
code_modulename OCaml
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   636
  Numeral Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   637
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   638
code_modulename Haskell
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   639
  Numeral Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   640
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   641
types_code
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   642
  "int" ("int")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   643
attach (term_of) {*
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24541
diff changeset
   644
val term_of_int = HOLogic.mk_number HOLogic.intT;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   645
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   646
attach (test) {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   647
fun gen_int i = one_of [~1, 1] * random_range 0 i;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   648
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   649
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   650
setup {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   651
let
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   652
24541
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   653
fun strip_number_of (@{term "Numeral.number_of :: int => int"} $ t) = t
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   654
  | strip_number_of t = t;
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   655
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   656
fun numeral_codegen thy defs gr dep module b t =
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   657
  let val i = HOLogic.dest_numeral (strip_number_of t)
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   658
  in
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   659
    SOME (fst (Codegen.invoke_tycodegen thy defs dep module false (gr, HOLogic.intT)),
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24541
diff changeset
   660
      Pretty.str (string_of_int i))
24541
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   661
  end handle TERM _ => NONE;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   662
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   663
in
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   664
24541
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   665
Codegen.add_codegen "numeral_codegen" numeral_codegen
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   666
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   667
end
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   668
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   669
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   670
consts_code
24541
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   671
  "number_of :: int \<Rightarrow> int"    ("(_)")
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   672
  "0 :: int"                   ("0")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   673
  "1 :: int"                   ("1")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   674
  "uminus :: int => int"       ("~")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   675
  "op + :: int => int => int"  ("(_ +/ _)")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   676
  "op * :: int => int => int"  ("(_ */ _)")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   677
  "op \<le> :: int => int => bool" ("(_ <=/ _)")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   678
  "op < :: int => int => bool" ("(_ </ _)")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   679
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   680
quickcheck_params [default_type = int]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   681
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   682
(*setup continues in theory Presburger*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   683
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   684
hide (open) const Pls Min B0 B1 succ pred
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   685
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   686
end