src/HOL/Numeral.thy
author haftmann
Fri, 19 Oct 2007 07:48:23 +0200
changeset 25089 04b8456f7754
parent 24630 351a308ab58d
child 25145 d432105e5bd0
permissions -rw-r--r--
dropped doubled proof
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
25089
04b8456f7754 dropped doubled proof
haftmann
parents: 24630
diff changeset
   328
lemmas double_eq_0_iff = double_zero
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   329
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   330
lemma le_imp_0_less: 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   331
  assumes le: "0 \<le> z"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   332
  shows "(0::int) < 1 + z"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   333
proof -
23389
aaca6a8e5414 tuned proofs: avoid implicit prems;
wenzelm
parents: 23365
diff changeset
   334
  have "0 \<le> z" by fact
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   335
  also have "... < z + 1" by (rule less_add_one) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   336
  also have "... = 1 + z" by (simp add: add_ac)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   337
  finally show "0 < 1 + z" .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   338
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   339
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   340
lemma odd_nonzero:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   341
  "1 + z + z \<noteq> (0::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   342
proof (cases z rule: int_cases)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   343
  case (nonneg n)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   344
  have le: "0 \<le> z+z" by (simp add: nonneg add_increasing) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   345
  thus ?thesis using  le_imp_0_less [OF le]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   346
    by (auto simp add: add_assoc) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   347
next
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   348
  case (neg n)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   349
  show ?thesis
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   350
  proof
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   351
    assume eq: "1 + z + z = 0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   352
    have "0 < 1 + (int n + int n)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   353
      by (simp add: le_imp_0_less add_increasing) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   354
    also have "... = - (1 + z + z)" 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   355
      by (simp add: neg add_assoc [symmetric]) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   356
    also have "... = 0" by (simp add: eq) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   357
    finally have "0<0" ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   358
    thus False by blast
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   359
  qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   360
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   361
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   362
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   363
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   364
lemma Ints_double_eq_0_iff:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   365
  assumes in_Ints: "a \<in> Ints"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   366
  shows "(a + a = 0) = (a = (0::'a::ring_char_0))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   367
proof -
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   368
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   369
  then obtain z where a: "a = of_int z" ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   370
  show ?thesis
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   371
  proof
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   372
    assume "a = 0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   373
    thus "a + a = 0" by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   374
  next
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   375
    assume eq: "a + a = 0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   376
    hence "of_int (z + z) = (of_int 0 :: 'a)" by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   377
    hence "z + z = 0" by (simp only: of_int_eq_iff)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   378
    hence "z = 0" by (simp only: double_eq_0_iff)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   379
    thus "a = 0" by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   380
  qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   381
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   382
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   383
lemma Ints_odd_nonzero:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   384
  assumes in_Ints: "a \<in> Ints"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   385
  shows "1 + a + a \<noteq> (0::'a::ring_char_0)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   386
proof -
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   387
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   388
  then obtain z where a: "a = of_int z" ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   389
  show ?thesis
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   390
  proof
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   391
    assume eq: "1 + a + a = 0"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   392
    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
   393
    hence "1 + z + z = 0" by (simp only: of_int_eq_iff)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   394
    with odd_nonzero show False by blast
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   395
  qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   396
qed 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   397
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   398
lemma Ints_number_of:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   399
  "(number_of w :: 'a::number_ring) \<in> Ints"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   400
  unfolding number_of_eq Ints_def by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   401
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   402
lemma iszero_number_of_BIT:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   403
  "iszero (number_of (w BIT x)::'a) = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   404
   (x = B0 \<and> iszero (number_of w::'a::{ring_char_0,number_ring}))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   405
  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
   406
    Ints_odd_nonzero Ints_def split: bit.split)
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_0:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   409
  "iszero (number_of (w BIT B0) :: 'a::{ring_char_0,number_ring}) = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   410
  iszero (number_of w :: 'a)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   411
  by (simp only: iszero_number_of_BIT simp_thms)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   412
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   413
lemma iszero_number_of_1:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   414
  "~ iszero (number_of (w BIT B1)::'a::{ring_char_0,number_ring})"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   415
  by (simp add: iszero_number_of_BIT) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   416
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   417
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   418
subsection {* The Less-Than Relation *}
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 less_number_of_eq_neg:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   421
  "((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   422
  = neg (number_of (x + uminus y) :: 'a)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   423
apply (subst less_iff_diff_less_0) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   424
apply (simp add: neg_def diff_minus number_of_add number_of_minus)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   425
done
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   426
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   427
text {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   428
  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
   429
  @{term Numeral0} IS @{term "number_of Pls"}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   430
*}
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 not_neg_number_of_Pls:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   433
  "~ neg (number_of Pls ::'a::{ordered_idom,number_ring})"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   434
  by (simp add: neg_def numeral_0_eq_0)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   435
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   436
lemma neg_number_of_Min:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   437
  "neg (number_of Min ::'a::{ordered_idom,number_ring})"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   438
  by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   439
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   440
lemma double_less_0_iff:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   441
  "(a + a < 0) = (a < (0::'a::ordered_idom))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   442
proof -
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   443
  have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   444
  also have "... = (a < 0)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   445
    by (simp add: mult_less_0_iff zero_less_two 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   446
                  order_less_not_sym [OF zero_less_two]) 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   447
  finally show ?thesis .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   448
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   449
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   450
lemma odd_less_0:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   451
  "(1 + z + z < 0) = (z < (0::int))";
23365
f31794033ae1 removed constant int :: nat => int;
huffman
parents: 23307
diff changeset
   452
proof (cases z rule: int_cases)
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   453
  case (nonneg n)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   454
  thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   455
                             le_imp_0_less [THEN order_less_imp_le])  
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   456
next
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   457
  case (neg n)
23307
2fe3345035c7 modify proofs to avoid referring to int::nat=>int
huffman
parents: 23164
diff changeset
   458
  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
   459
    add: compare_rls of_nat_1 [symmetric] of_nat_add [symmetric])
23164
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
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   463
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   464
lemma Ints_odd_less_0: 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   465
  assumes in_Ints: "a \<in> Ints"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   466
  shows "(1 + a + a < 0) = (a < (0::'a::ordered_idom))";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   467
proof -
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   468
  from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   469
  then obtain z where a: "a = of_int z" ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   470
  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
   471
    by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   472
  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
   473
  also have "... = (a < 0)" by (simp add: a)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   474
  finally show ?thesis .
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   475
qed
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   476
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   477
lemma neg_number_of_BIT:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   478
  "neg (number_of (w BIT x)::'a) = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   479
  neg (number_of w :: 'a::{ordered_idom,number_ring})"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   480
  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
   481
    Ints_odd_less_0 Ints_def split: bit.split)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   482
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   483
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   484
text {* Less-Than or Equals *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   485
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   486
text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   487
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   488
lemmas le_number_of_eq_not_less =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   489
  linorder_not_less [of "number_of w" "number_of v", symmetric, 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   490
  standard]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   491
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   492
lemma le_number_of_eq:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   493
    "((number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   494
     = (~ (neg (number_of (y + uminus x) :: 'a)))"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   495
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
   496
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 {* Absolute value (@{term abs}) *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   499
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   500
lemma abs_number_of:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   501
  "abs(number_of x::'a::{ordered_idom,number_ring}) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   502
   (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
   503
  by (simp add: abs_if)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   504
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   505
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   506
text {* Re-orientation of the equation nnn=x *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   507
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   508
lemma number_of_reorient:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   509
  "(number_of w = x) = (x = number_of w)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   510
  by auto
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   511
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   512
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   513
subsection {* Simplification of arithmetic operations on integer constants. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   514
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   515
lemmas arith_extra_simps [standard, simp] =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   516
  number_of_add [symmetric]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   517
  number_of_minus [symmetric] numeral_m1_eq_minus_1 [symmetric]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   518
  number_of_mult [symmetric]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   519
  diff_number_of_eq abs_number_of 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   520
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   521
text {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   522
  For making a minimal simpset, one must include these default simprules.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   523
  Also include @{text simp_thms}.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   524
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   525
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   526
lemmas arith_simps = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   527
  bit.distinct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   528
  Pls_0_eq Min_1_eq
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   529
  pred_Pls pred_Min pred_1 pred_0
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   530
  succ_Pls succ_Min succ_1 succ_0
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   531
  add_Pls add_Min add_BIT_0 add_BIT_10 add_BIT_11
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   532
  minus_Pls minus_Min minus_1 minus_0
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   533
  mult_Pls mult_Min mult_num1 mult_num0 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   534
  add_Pls_right add_Min_right
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   535
  abs_zero abs_one arith_extra_simps
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   536
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   537
text {* Simplification of relational operations *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   538
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   539
lemmas rel_simps [simp] = 
24392
000327cea3eb replace iszero_number_of_Pls with iszero_0 in rel_simps
huffman
parents: 24166
diff changeset
   540
  eq_number_of_eq iszero_0 nonzero_number_of_Min
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   541
  iszero_number_of_0 iszero_number_of_1
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   542
  less_number_of_eq_neg
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   543
  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
   544
  neg_number_of_Min neg_number_of_BIT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   545
  le_number_of_eq
24392
000327cea3eb replace iszero_number_of_Pls with iszero_0 in rel_simps
huffman
parents: 24166
diff changeset
   546
(* 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
   547
   because its lhs simplifies to "iszero 0" *)
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   548
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   549
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   550
subsection {* Simplification of arithmetic when nested to the right. *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   551
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   552
lemma add_number_of_left [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   553
  "number_of v + (number_of w + z) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   554
   (number_of(v + w) + z::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   555
  by (simp add: add_assoc [symmetric])
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   556
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   557
lemma mult_number_of_left [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   558
  "number_of v * (number_of w * z) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   559
   (number_of(v * w) * z::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   560
  by (simp add: mult_assoc [symmetric])
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_diff1:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   563
  "number_of v + (number_of w - c) = 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   564
  number_of(v + w) - (c::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   565
  by (simp add: diff_minus add_number_of_left)
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 add_number_of_diff2 [simp]:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   568
  "number_of v + (c - number_of w) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   569
   number_of (v + uminus w) + (c::'a::number_ring)"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   570
apply (subst diff_number_of_eq [symmetric])
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   571
apply (simp only: compare_rls)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   572
done
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   573
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   574
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   575
subsection {* Configuration of the code generator *}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   576
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   577
instance int :: eq ..
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   578
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   579
code_datatype Pls Min Bit "number_of \<Colon> int \<Rightarrow> int"
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   580
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   581
definition
23855
haftmann
parents: 23708
diff changeset
   582
  int_aux :: "nat \<Rightarrow> int \<Rightarrow> int" where
haftmann
parents: 23708
diff changeset
   583
  "int_aux n i = int n + i"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   584
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   585
lemma [code]:
23855
haftmann
parents: 23708
diff changeset
   586
  "int_aux 0 i  = i"
haftmann
parents: 23708
diff changeset
   587
  "int_aux (Suc n) i = int_aux n (i + 1)" -- {* tail recursive *}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   588
  by (simp add: int_aux_def)+
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   589
23365
f31794033ae1 removed constant int :: nat => int;
huffman
parents: 23307
diff changeset
   590
lemma [code unfold]:
23855
haftmann
parents: 23708
diff changeset
   591
  "int n = int_aux n 0"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   592
  by (simp add: int_aux_def)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   593
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   594
definition
23855
haftmann
parents: 23708
diff changeset
   595
  nat_aux :: "int \<Rightarrow> nat \<Rightarrow> nat" where
haftmann
parents: 23708
diff changeset
   596
  "nat_aux i n = nat i + n"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   597
23855
haftmann
parents: 23708
diff changeset
   598
lemma [code]:
haftmann
parents: 23708
diff changeset
   599
  "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
   600
  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
   601
    dest: zless_imp_add1_zle)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   602
23855
haftmann
parents: 23708
diff changeset
   603
lemma [code]: "nat i = nat_aux i 0"
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   604
  by (simp add: nat_aux_def)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   605
24166
7b28dc69bdbb new nbe implementation
haftmann
parents: 23855
diff changeset
   606
lemma zero_is_num_zero [code func, code inline, symmetric, code post]:
23855
haftmann
parents: 23708
diff changeset
   607
  "(0\<Colon>int) = Numeral0" 
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   608
  by simp
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   609
24166
7b28dc69bdbb new nbe implementation
haftmann
parents: 23855
diff changeset
   610
lemma one_is_num_one [code func, code inline, symmetric, code post]:
23855
haftmann
parents: 23708
diff changeset
   611
  "(1\<Colon>int) = Numeral1" 
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   612
  by simp 
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   613
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   614
code_modulename SML
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   615
  IntDef Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   616
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   617
code_modulename OCaml
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   618
  IntDef Integer
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 Haskell
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 SML
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   624
  Numeral Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   625
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   626
code_modulename OCaml
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   627
  Numeral Integer
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   628
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   629
code_modulename Haskell
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   630
  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
types_code
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   633
  "int" ("int")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   634
attach (term_of) {*
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24541
diff changeset
   635
val term_of_int = HOLogic.mk_number HOLogic.intT;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   636
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   637
attach (test) {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   638
fun gen_int i = one_of [~1, 1] * random_range 0 i;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   639
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   640
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   641
setup {*
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   642
let
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   643
24541
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   644
fun strip_number_of (@{term "Numeral.number_of :: int => int"} $ t) = t
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   645
  | strip_number_of t = t;
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   646
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   647
fun numeral_codegen thy defs gr dep module b t =
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   648
  let val i = HOLogic.dest_numeral (strip_number_of t)
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   649
  in
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   650
    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
   651
      Pretty.str (string_of_int i))
24541
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   652
  end handle TERM _ => NONE;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   653
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   654
in
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   655
24541
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   656
Codegen.add_codegen "numeral_codegen" numeral_codegen
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   657
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   658
end
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   659
*}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   660
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   661
consts_code
24541
98c978a42a42 Generalized code generator for numerals.
berghofe
parents: 24392
diff changeset
   662
  "number_of :: int \<Rightarrow> int"    ("(_)")
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   663
  "0 :: int"                   ("0")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   664
  "1 :: int"                   ("1")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   665
  "uminus :: int => int"       ("~")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   666
  "op + :: int => int => int"  ("(_ +/ _)")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   667
  "op * :: int => int => int"  ("(_ */ _)")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   668
  "op \<le> :: int => int => bool" ("(_ <=/ _)")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   669
  "op < :: int => int => bool" ("(_ </ _)")
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   670
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   671
quickcheck_params [default_type = int]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   672
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   673
(*setup continues in theory Presburger*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   674
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   675
hide (open) const Pls Min B0 B1 succ pred
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   676
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   677
end