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