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