merged, resolving trivial conflict;
authorwenzelm
Mon, 26 Mar 2012 10:56:56 +0200
changeset 47982f067afe98049
parent 47981 29e92b644d6c
parent 47979 db5026631799
child 47983 8493d5d0e9b6
merged, resolving trivial conflict;
src/HOL/Tools/Quotient/quotient_info.ML
src/HOL/Tools/numeral_syntax.ML
src/HOL/ex/Efficient_Nat_examples.thy
     1.1 --- a/Admin/isatest/isatest-settings	Fri Mar 23 20:32:43 2012 +0100
     1.2 +++ b/Admin/isatest/isatest-settings	Mon Mar 26 10:56:56 2012 +0200
     1.3 @@ -22,7 +22,8 @@
     1.4  bulwahn@in.tum.de \
     1.5  hoelzl@in.tum.de \
     1.6  krauss@in.tum.de \
     1.7 -noschinl@in.tum.de"
     1.8 +noschinl@in.tum.de \
     1.9 +kuncar@in.tum.de"
    1.10  
    1.11  LOGPREFIX=$HOME/log
    1.12  MASTERLOG=$LOGPREFIX/isatest.log
     2.1 --- a/Admin/isatest/pmail	Fri Mar 23 20:32:43 2012 +0100
     2.2 +++ b/Admin/isatest/pmail	Mon Mar 26 10:56:56 2012 +0200
     2.3 @@ -95,7 +95,7 @@
     2.4  
     2.5  case `uname` in
     2.6  	Linux)  for F in $@; do ATTACH="$ATTACH -a $F"; done
     2.7 -		cat "$BODY" | mail -s "$SUBJECT" $ATTACH "$TO"
     2.8 +		cat "$BODY" | mail -Ssmtp=mailbroy.informatik.tu-muenchen.de -s "$SUBJECT" $ATTACH "$TO"
     2.9  		;;
    2.10  	SunOS)
    2.11  		print_body "$SUBJECT" "$BODY" $@ | mail -t "$TO"
     3.1 --- a/NEWS	Fri Mar 23 20:32:43 2012 +0100
     3.2 +++ b/NEWS	Mon Mar 26 10:56:56 2012 +0200
     3.3 @@ -90,6 +90,30 @@
     3.4  
     3.5  *** HOL ***
     3.6  
     3.7 +* The representation of numerals has changed. We now have a datatype
     3.8 +"num" representing strictly positive binary numerals, along with
     3.9 +functions "numeral :: num => 'a" and "neg_numeral :: num => 'a" to
    3.10 +represent positive and negated numeric literals, respectively. (See
    3.11 +definitions in Num.thy.) Potential INCOMPATIBILITY; some user theories
    3.12 +may require adaptations:
    3.13 +
    3.14 +  - Theorems with number_ring or number_semiring constraints: These
    3.15 +    classes are gone; use comm_ring_1 or comm_semiring_1 instead.
    3.16 +
    3.17 +  - Theories defining numeric types: Remove number, number_semiring,
    3.18 +    and number_ring instances. Defer all theorems about numerals until
    3.19 +    after classes one and semigroup_add have been instantiated.
    3.20 +
    3.21 +  - Numeral-only simp rules: Replace each rule having a "number_of v"
    3.22 +    pattern with two copies, one for numeral and one for neg_numeral.
    3.23 +
    3.24 +  - Theorems about subclasses of semiring_1 or ring_1: These classes
    3.25 +    automatically support numerals now, so more simp rules and
    3.26 +    simprocs may now apply within the proof.
    3.27 +
    3.28 +  - Definitions and theorems using old constructors Pls/Min/Bit0/Bit1:
    3.29 +    Redefine using other integer operations.
    3.30 +
    3.31  * Type 'a set is now a proper type constructor (just as before
    3.32  Isabelle2008).  Definitions mem_def and Collect_def have disappeared.
    3.33  Non-trivial INCOMPATIBILITY.  For developments keeping predicates and
     4.1 --- a/etc/isar-keywords.el	Fri Mar 23 20:32:43 2012 +0100
     4.2 +++ b/etc/isar-keywords.el	Mon Mar 26 10:56:56 2012 +0200
     4.3 @@ -221,6 +221,7 @@
     4.4      "sect"
     4.5      "section"
     4.6      "setup"
     4.7 +    "setup_lifting"
     4.8      "show"
     4.9      "simproc_setup"
    4.10      "sledgehammer"
    4.11 @@ -518,13 +519,13 @@
    4.12      "print_translation"
    4.13      "quickcheck_generator"
    4.14      "quickcheck_params"
    4.15 -    "quotient_definition"
    4.16      "realizability"
    4.17      "realizers"
    4.18      "recdef"
    4.19      "record"
    4.20      "refute_params"
    4.21      "setup"
    4.22 +    "setup_lifting"
    4.23      "simproc_setup"
    4.24      "sledgehammer_params"
    4.25      "spark_end"
    4.26 @@ -563,6 +564,7 @@
    4.27      "nominal_inductive2"
    4.28      "nominal_primrec"
    4.29      "pcpodef"
    4.30 +    "quotient_definition"
    4.31      "quotient_type"
    4.32      "recdef_tc"
    4.33      "rep_datatype"
     5.1 --- a/src/HOL/Algebra/Group.thy	Fri Mar 23 20:32:43 2012 +0100
     5.2 +++ b/src/HOL/Algebra/Group.thy	Mon Mar 26 10:56:56 2012 +0200
     5.3 @@ -30,7 +30,7 @@
     5.4    where "Units G = {y. y \<in> carrier G & (\<exists>x \<in> carrier G. x \<otimes>\<^bsub>G\<^esub> y = \<one>\<^bsub>G\<^esub> & y \<otimes>\<^bsub>G\<^esub> x = \<one>\<^bsub>G\<^esub>)}"
     5.5  
     5.6  consts
     5.7 -  pow :: "[('a, 'm) monoid_scheme, 'a, 'b::number] => 'a"  (infixr "'(^')\<index>" 75)
     5.8 +  pow :: "[('a, 'm) monoid_scheme, 'a, 'b::semiring_1] => 'a"  (infixr "'(^')\<index>" 75)
     5.9  
    5.10  overloading nat_pow == "pow :: [_, 'a, nat] => 'a"
    5.11  begin
     6.1 --- a/src/HOL/Archimedean_Field.thy	Fri Mar 23 20:32:43 2012 +0100
     6.2 +++ b/src/HOL/Archimedean_Field.thy	Mon Mar 26 10:56:56 2012 +0200
     6.3 @@ -12,7 +12,7 @@
     6.4  
     6.5  text {* Archimedean fields have no infinite elements. *}
     6.6  
     6.7 -class archimedean_field = linordered_field + number_ring +
     6.8 +class archimedean_field = linordered_field +
     6.9    assumes ex_le_of_int: "\<exists>z. x \<le> of_int z"
    6.10  
    6.11  lemma ex_less_of_int:
    6.12 @@ -202,8 +202,11 @@
    6.13  lemma floor_one [simp]: "floor 1 = 1"
    6.14    using floor_of_int [of 1] by simp
    6.15  
    6.16 -lemma floor_number_of [simp]: "floor (number_of v) = number_of v"
    6.17 -  using floor_of_int [of "number_of v"] by simp
    6.18 +lemma floor_numeral [simp]: "floor (numeral v) = numeral v"
    6.19 +  using floor_of_int [of "numeral v"] by simp
    6.20 +
    6.21 +lemma floor_neg_numeral [simp]: "floor (neg_numeral v) = neg_numeral v"
    6.22 +  using floor_of_int [of "neg_numeral v"] by simp
    6.23  
    6.24  lemma zero_le_floor [simp]: "0 \<le> floor x \<longleftrightarrow> 0 \<le> x"
    6.25    by (simp add: le_floor_iff)
    6.26 @@ -211,7 +214,12 @@
    6.27  lemma one_le_floor [simp]: "1 \<le> floor x \<longleftrightarrow> 1 \<le> x"
    6.28    by (simp add: le_floor_iff)
    6.29  
    6.30 -lemma number_of_le_floor [simp]: "number_of v \<le> floor x \<longleftrightarrow> number_of v \<le> x"
    6.31 +lemma numeral_le_floor [simp]:
    6.32 +  "numeral v \<le> floor x \<longleftrightarrow> numeral v \<le> x"
    6.33 +  by (simp add: le_floor_iff)
    6.34 +
    6.35 +lemma neg_numeral_le_floor [simp]:
    6.36 +  "neg_numeral v \<le> floor x \<longleftrightarrow> neg_numeral v \<le> x"
    6.37    by (simp add: le_floor_iff)
    6.38  
    6.39  lemma zero_less_floor [simp]: "0 < floor x \<longleftrightarrow> 1 \<le> x"
    6.40 @@ -220,8 +228,12 @@
    6.41  lemma one_less_floor [simp]: "1 < floor x \<longleftrightarrow> 2 \<le> x"
    6.42    by (simp add: less_floor_iff)
    6.43  
    6.44 -lemma number_of_less_floor [simp]:
    6.45 -  "number_of v < floor x \<longleftrightarrow> number_of v + 1 \<le> x"
    6.46 +lemma numeral_less_floor [simp]:
    6.47 +  "numeral v < floor x \<longleftrightarrow> numeral v + 1 \<le> x"
    6.48 +  by (simp add: less_floor_iff)
    6.49 +
    6.50 +lemma neg_numeral_less_floor [simp]:
    6.51 +  "neg_numeral v < floor x \<longleftrightarrow> neg_numeral v + 1 \<le> x"
    6.52    by (simp add: less_floor_iff)
    6.53  
    6.54  lemma floor_le_zero [simp]: "floor x \<le> 0 \<longleftrightarrow> x < 1"
    6.55 @@ -230,8 +242,12 @@
    6.56  lemma floor_le_one [simp]: "floor x \<le> 1 \<longleftrightarrow> x < 2"
    6.57    by (simp add: floor_le_iff)
    6.58  
    6.59 -lemma floor_le_number_of [simp]:
    6.60 -  "floor x \<le> number_of v \<longleftrightarrow> x < number_of v + 1"
    6.61 +lemma floor_le_numeral [simp]:
    6.62 +  "floor x \<le> numeral v \<longleftrightarrow> x < numeral v + 1"
    6.63 +  by (simp add: floor_le_iff)
    6.64 +
    6.65 +lemma floor_le_neg_numeral [simp]:
    6.66 +  "floor x \<le> neg_numeral v \<longleftrightarrow> x < neg_numeral v + 1"
    6.67    by (simp add: floor_le_iff)
    6.68  
    6.69  lemma floor_less_zero [simp]: "floor x < 0 \<longleftrightarrow> x < 0"
    6.70 @@ -240,8 +256,12 @@
    6.71  lemma floor_less_one [simp]: "floor x < 1 \<longleftrightarrow> x < 1"
    6.72    by (simp add: floor_less_iff)
    6.73  
    6.74 -lemma floor_less_number_of [simp]:
    6.75 -  "floor x < number_of v \<longleftrightarrow> x < number_of v"
    6.76 +lemma floor_less_numeral [simp]:
    6.77 +  "floor x < numeral v \<longleftrightarrow> x < numeral v"
    6.78 +  by (simp add: floor_less_iff)
    6.79 +
    6.80 +lemma floor_less_neg_numeral [simp]:
    6.81 +  "floor x < neg_numeral v \<longleftrightarrow> x < neg_numeral v"
    6.82    by (simp add: floor_less_iff)
    6.83  
    6.84  text {* Addition and subtraction of integers *}
    6.85 @@ -249,9 +269,13 @@
    6.86  lemma floor_add_of_int [simp]: "floor (x + of_int z) = floor x + z"
    6.87    using floor_correct [of x] by (simp add: floor_unique)
    6.88  
    6.89 -lemma floor_add_number_of [simp]:
    6.90 -    "floor (x + number_of v) = floor x + number_of v"
    6.91 -  using floor_add_of_int [of x "number_of v"] by simp
    6.92 +lemma floor_add_numeral [simp]:
    6.93 +    "floor (x + numeral v) = floor x + numeral v"
    6.94 +  using floor_add_of_int [of x "numeral v"] by simp
    6.95 +
    6.96 +lemma floor_add_neg_numeral [simp]:
    6.97 +    "floor (x + neg_numeral v) = floor x + neg_numeral v"
    6.98 +  using floor_add_of_int [of x "neg_numeral v"] by simp
    6.99  
   6.100  lemma floor_add_one [simp]: "floor (x + 1) = floor x + 1"
   6.101    using floor_add_of_int [of x 1] by simp
   6.102 @@ -259,9 +283,13 @@
   6.103  lemma floor_diff_of_int [simp]: "floor (x - of_int z) = floor x - z"
   6.104    using floor_add_of_int [of x "- z"] by (simp add: algebra_simps)
   6.105  
   6.106 -lemma floor_diff_number_of [simp]:
   6.107 -  "floor (x - number_of v) = floor x - number_of v"
   6.108 -  using floor_diff_of_int [of x "number_of v"] by simp
   6.109 +lemma floor_diff_numeral [simp]:
   6.110 +  "floor (x - numeral v) = floor x - numeral v"
   6.111 +  using floor_diff_of_int [of x "numeral v"] by simp
   6.112 +
   6.113 +lemma floor_diff_neg_numeral [simp]:
   6.114 +  "floor (x - neg_numeral v) = floor x - neg_numeral v"
   6.115 +  using floor_diff_of_int [of x "neg_numeral v"] by simp
   6.116  
   6.117  lemma floor_diff_one [simp]: "floor (x - 1) = floor x - 1"
   6.118    using floor_diff_of_int [of x 1] by simp
   6.119 @@ -320,8 +348,11 @@
   6.120  lemma ceiling_one [simp]: "ceiling 1 = 1"
   6.121    using ceiling_of_int [of 1] by simp
   6.122  
   6.123 -lemma ceiling_number_of [simp]: "ceiling (number_of v) = number_of v"
   6.124 -  using ceiling_of_int [of "number_of v"] by simp
   6.125 +lemma ceiling_numeral [simp]: "ceiling (numeral v) = numeral v"
   6.126 +  using ceiling_of_int [of "numeral v"] by simp
   6.127 +
   6.128 +lemma ceiling_neg_numeral [simp]: "ceiling (neg_numeral v) = neg_numeral v"
   6.129 +  using ceiling_of_int [of "neg_numeral v"] by simp
   6.130  
   6.131  lemma ceiling_le_zero [simp]: "ceiling x \<le> 0 \<longleftrightarrow> x \<le> 0"
   6.132    by (simp add: ceiling_le_iff)
   6.133 @@ -329,8 +360,12 @@
   6.134  lemma ceiling_le_one [simp]: "ceiling x \<le> 1 \<longleftrightarrow> x \<le> 1"
   6.135    by (simp add: ceiling_le_iff)
   6.136  
   6.137 -lemma ceiling_le_number_of [simp]:
   6.138 -  "ceiling x \<le> number_of v \<longleftrightarrow> x \<le> number_of v"
   6.139 +lemma ceiling_le_numeral [simp]:
   6.140 +  "ceiling x \<le> numeral v \<longleftrightarrow> x \<le> numeral v"
   6.141 +  by (simp add: ceiling_le_iff)
   6.142 +
   6.143 +lemma ceiling_le_neg_numeral [simp]:
   6.144 +  "ceiling x \<le> neg_numeral v \<longleftrightarrow> x \<le> neg_numeral v"
   6.145    by (simp add: ceiling_le_iff)
   6.146  
   6.147  lemma ceiling_less_zero [simp]: "ceiling x < 0 \<longleftrightarrow> x \<le> -1"
   6.148 @@ -339,8 +374,12 @@
   6.149  lemma ceiling_less_one [simp]: "ceiling x < 1 \<longleftrightarrow> x \<le> 0"
   6.150    by (simp add: ceiling_less_iff)
   6.151  
   6.152 -lemma ceiling_less_number_of [simp]:
   6.153 -  "ceiling x < number_of v \<longleftrightarrow> x \<le> number_of v - 1"
   6.154 +lemma ceiling_less_numeral [simp]:
   6.155 +  "ceiling x < numeral v \<longleftrightarrow> x \<le> numeral v - 1"
   6.156 +  by (simp add: ceiling_less_iff)
   6.157 +
   6.158 +lemma ceiling_less_neg_numeral [simp]:
   6.159 +  "ceiling x < neg_numeral v \<longleftrightarrow> x \<le> neg_numeral v - 1"
   6.160    by (simp add: ceiling_less_iff)
   6.161  
   6.162  lemma zero_le_ceiling [simp]: "0 \<le> ceiling x \<longleftrightarrow> -1 < x"
   6.163 @@ -349,8 +388,12 @@
   6.164  lemma one_le_ceiling [simp]: "1 \<le> ceiling x \<longleftrightarrow> 0 < x"
   6.165    by (simp add: le_ceiling_iff)
   6.166  
   6.167 -lemma number_of_le_ceiling [simp]:
   6.168 -  "number_of v \<le> ceiling x\<longleftrightarrow> number_of v - 1 < x"
   6.169 +lemma numeral_le_ceiling [simp]:
   6.170 +  "numeral v \<le> ceiling x \<longleftrightarrow> numeral v - 1 < x"
   6.171 +  by (simp add: le_ceiling_iff)
   6.172 +
   6.173 +lemma neg_numeral_le_ceiling [simp]:
   6.174 +  "neg_numeral v \<le> ceiling x \<longleftrightarrow> neg_numeral v - 1 < x"
   6.175    by (simp add: le_ceiling_iff)
   6.176  
   6.177  lemma zero_less_ceiling [simp]: "0 < ceiling x \<longleftrightarrow> 0 < x"
   6.178 @@ -359,8 +402,12 @@
   6.179  lemma one_less_ceiling [simp]: "1 < ceiling x \<longleftrightarrow> 1 < x"
   6.180    by (simp add: less_ceiling_iff)
   6.181  
   6.182 -lemma number_of_less_ceiling [simp]:
   6.183 -  "number_of v < ceiling x \<longleftrightarrow> number_of v < x"
   6.184 +lemma numeral_less_ceiling [simp]:
   6.185 +  "numeral v < ceiling x \<longleftrightarrow> numeral v < x"
   6.186 +  by (simp add: less_ceiling_iff)
   6.187 +
   6.188 +lemma neg_numeral_less_ceiling [simp]:
   6.189 +  "neg_numeral v < ceiling x \<longleftrightarrow> neg_numeral v < x"
   6.190    by (simp add: less_ceiling_iff)
   6.191  
   6.192  text {* Addition and subtraction of integers *}
   6.193 @@ -368,9 +415,13 @@
   6.194  lemma ceiling_add_of_int [simp]: "ceiling (x + of_int z) = ceiling x + z"
   6.195    using ceiling_correct [of x] by (simp add: ceiling_unique)
   6.196  
   6.197 -lemma ceiling_add_number_of [simp]:
   6.198 -    "ceiling (x + number_of v) = ceiling x + number_of v"
   6.199 -  using ceiling_add_of_int [of x "number_of v"] by simp
   6.200 +lemma ceiling_add_numeral [simp]:
   6.201 +    "ceiling (x + numeral v) = ceiling x + numeral v"
   6.202 +  using ceiling_add_of_int [of x "numeral v"] by simp
   6.203 +
   6.204 +lemma ceiling_add_neg_numeral [simp]:
   6.205 +    "ceiling (x + neg_numeral v) = ceiling x + neg_numeral v"
   6.206 +  using ceiling_add_of_int [of x "neg_numeral v"] by simp
   6.207  
   6.208  lemma ceiling_add_one [simp]: "ceiling (x + 1) = ceiling x + 1"
   6.209    using ceiling_add_of_int [of x 1] by simp
   6.210 @@ -378,9 +429,13 @@
   6.211  lemma ceiling_diff_of_int [simp]: "ceiling (x - of_int z) = ceiling x - z"
   6.212    using ceiling_add_of_int [of x "- z"] by (simp add: algebra_simps)
   6.213  
   6.214 -lemma ceiling_diff_number_of [simp]:
   6.215 -  "ceiling (x - number_of v) = ceiling x - number_of v"
   6.216 -  using ceiling_diff_of_int [of x "number_of v"] by simp
   6.217 +lemma ceiling_diff_numeral [simp]:
   6.218 +  "ceiling (x - numeral v) = ceiling x - numeral v"
   6.219 +  using ceiling_diff_of_int [of x "numeral v"] by simp
   6.220 +
   6.221 +lemma ceiling_diff_neg_numeral [simp]:
   6.222 +  "ceiling (x - neg_numeral v) = ceiling x - neg_numeral v"
   6.223 +  using ceiling_diff_of_int [of x "neg_numeral v"] by simp
   6.224  
   6.225  lemma ceiling_diff_one [simp]: "ceiling (x - 1) = ceiling x - 1"
   6.226    using ceiling_diff_of_int [of x 1] by simp
     7.1 --- a/src/HOL/Code_Evaluation.thy	Fri Mar 23 20:32:43 2012 +0100
     7.2 +++ b/src/HOL/Code_Evaluation.thy	Mon Mar 26 10:56:56 2012 +0200
     7.3 @@ -146,33 +146,29 @@
     7.4    "term_of_num_semiring two = (\<lambda>_. dummy_term)"
     7.5  
     7.6  lemma (in term_syntax) term_of_num_semiring_code [code]:
     7.7 -  "term_of_num_semiring two k = (if k = 0 then termify Int.Pls
     7.8 +  "term_of_num_semiring two k = (
     7.9 +    if k = 1 then termify Num.One
    7.10      else (if k mod two = 0
    7.11 -      then termify Int.Bit0 <\<cdot>> term_of_num_semiring two (k div two)
    7.12 -      else termify Int.Bit1 <\<cdot>> term_of_num_semiring two (k div two)))"
    7.13 -  by (auto simp add: term_of_anything Const_def App_def term_of_num_semiring_def Let_def)
    7.14 +      then termify Num.Bit0 <\<cdot>> term_of_num_semiring two (k div two)
    7.15 +      else termify Num.Bit1 <\<cdot>> term_of_num_semiring two (k div two)))"
    7.16 +  by (auto simp add: term_of_anything Const_def App_def term_of_num_semiring_def)
    7.17  
    7.18  lemma (in term_syntax) term_of_nat_code [code]:
    7.19 -  "term_of (n::nat) = termify (number_of :: int \<Rightarrow> nat) <\<cdot>> term_of_num_semiring (2::nat) n"
    7.20 +  "term_of (n::nat) = (
    7.21 +    if n = 0 then termify (0 :: nat)
    7.22 +    else termify (numeral :: num \<Rightarrow> nat) <\<cdot>> term_of_num_semiring (2::nat) n)"
    7.23    by (simp only: term_of_anything)
    7.24  
    7.25  lemma (in term_syntax) term_of_code_numeral_code [code]:
    7.26 -  "term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num_semiring (2::code_numeral) k"
    7.27 +  "term_of (k::code_numeral) = (
    7.28 +    if k = 0 then termify (0 :: code_numeral)
    7.29 +    else termify (numeral :: num \<Rightarrow> code_numeral) <\<cdot>> term_of_num_semiring (2::code_numeral) k)"
    7.30    by (simp only: term_of_anything)
    7.31  
    7.32 -definition term_of_num_ring :: "'a\<Colon>ring_div \<Rightarrow> 'a \<Rightarrow> term" where
    7.33 -  "term_of_num_ring two = (\<lambda>_. dummy_term)"
    7.34 -
    7.35 -lemma (in term_syntax) term_of_num_ring_code [code]:
    7.36 -  "term_of_num_ring two k = (if k = 0 then termify Int.Pls
    7.37 -    else if k = -1 then termify Int.Min
    7.38 -    else if k mod two = 0 then termify Int.Bit0 <\<cdot>> term_of_num_ring two (k div two)
    7.39 -    else termify Int.Bit1 <\<cdot>> term_of_num_ring two (k div two))"
    7.40 -  by (auto simp add: term_of_anything Const_def App_def term_of_num_ring_def Let_def)
    7.41 -
    7.42  lemma (in term_syntax) term_of_int_code [code]:
    7.43    "term_of (k::int) = (if k = 0 then termify (0 :: int)
    7.44 -    else termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num_ring (2::int) k)"
    7.45 +    else if k < 0 then termify (neg_numeral :: num \<Rightarrow> int) <\<cdot>> term_of_num_semiring (2::int) (- k)
    7.46 +    else termify (numeral :: num \<Rightarrow> int) <\<cdot>> term_of_num_semiring (2::int) k)"
    7.47    by (simp only: term_of_anything)
    7.48  
    7.49  
    7.50 @@ -201,6 +197,6 @@
    7.51  
    7.52  
    7.53  hide_const dummy_term valapp
    7.54 -hide_const (open) Const App Abs Free termify valtermify term_of term_of_num_semiring term_of_num_ring tracing
    7.55 +hide_const (open) Const App Abs Free termify valtermify term_of term_of_num_semiring tracing
    7.56  
    7.57  end
     8.1 --- a/src/HOL/Code_Numeral.thy	Fri Mar 23 20:32:43 2012 +0100
     8.2 +++ b/src/HOL/Code_Numeral.thy	Mon Mar 26 10:56:56 2012 +0200
     8.3 @@ -123,25 +123,6 @@
     8.4    by (rule equal_refl)
     8.5  
     8.6  
     8.7 -subsection {* Code numerals as datatype of ints *}
     8.8 -
     8.9 -instantiation code_numeral :: number
    8.10 -begin
    8.11 -
    8.12 -definition
    8.13 -  "number_of = of_nat o nat"
    8.14 -
    8.15 -instance ..
    8.16 -
    8.17 -end
    8.18 -
    8.19 -lemma nat_of_number [simp]:
    8.20 -  "nat_of (number_of k) = number_of k"
    8.21 -  by (simp add: number_of_code_numeral_def nat_number_of_def number_of_is_id)
    8.22 -
    8.23 -code_datatype "number_of \<Colon> int \<Rightarrow> code_numeral"
    8.24 -
    8.25 -
    8.26  subsection {* Basic arithmetic *}
    8.27  
    8.28  instantiation code_numeral :: "{minus, linordered_semidom, semiring_div, linorder}"
    8.29 @@ -176,16 +157,17 @@
    8.30  
    8.31  end
    8.32  
    8.33 -lemma zero_code_numeral_code [code]:
    8.34 -  "(0\<Colon>code_numeral) = Numeral0"
    8.35 -  by (simp add: number_of_code_numeral_def Pls_def)
    8.36 +lemma nat_of_numeral [simp]: "nat_of (numeral k) = numeral k"
    8.37 +  by (induct k rule: num_induct) (simp_all add: numeral_inc)
    8.38  
    8.39 -lemma [code_abbrev]: "Numeral0 = (0\<Colon>code_numeral)"
    8.40 -  using zero_code_numeral_code ..
    8.41 +definition Num :: "num \<Rightarrow> code_numeral"
    8.42 +  where [simp, code_abbrev]: "Num = numeral"
    8.43 +
    8.44 +code_datatype "0::code_numeral" Num
    8.45  
    8.46  lemma one_code_numeral_code [code]:
    8.47    "(1\<Colon>code_numeral) = Numeral1"
    8.48 -  by (simp add: number_of_code_numeral_def Pls_def Bit1_def)
    8.49 +  by simp
    8.50  
    8.51  lemma [code_abbrev]: "Numeral1 = (1\<Colon>code_numeral)"
    8.52    using one_code_numeral_code ..
    8.53 @@ -194,15 +176,8 @@
    8.54    "of_nat n + of_nat m = of_nat (n + m)"
    8.55    by simp
    8.56  
    8.57 -definition subtract :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
    8.58 -  [simp]: "subtract = minus"
    8.59 -
    8.60 -lemma subtract_code [code nbe]:
    8.61 -  "subtract (of_nat n) (of_nat m) = of_nat (n - m)"
    8.62 -  by simp
    8.63 -
    8.64 -lemma minus_code_numeral_code [code]:
    8.65 -  "minus = subtract"
    8.66 +lemma minus_code_numeral_code [code nbe]:
    8.67 +  "of_nat n - of_nat m = of_nat (n - m)"
    8.68    by simp
    8.69  
    8.70  lemma times_code_numeral_code [code nbe]:
    8.71 @@ -281,7 +256,7 @@
    8.72  qed
    8.73  
    8.74  
    8.75 -hide_const (open) of_nat nat_of Suc subtract int_of
    8.76 +hide_const (open) of_nat nat_of Suc int_of
    8.77  
    8.78  
    8.79  subsection {* Code generator setup *}
    8.80 @@ -298,15 +273,21 @@
    8.81    (Haskell -)
    8.82  
    8.83  setup {*
    8.84 -  Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
    8.85 +  Numeral.add_code @{const_name Num}
    8.86      false Code_Printer.literal_naive_numeral "SML"
    8.87 -  #> fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
    8.88 +  #> fold (Numeral.add_code @{const_name Num}
    8.89      false Code_Printer.literal_numeral) ["OCaml", "Haskell", "Scala"]
    8.90  *}
    8.91  
    8.92  code_reserved SML Int int
    8.93  code_reserved Eval Integer
    8.94  
    8.95 +code_const "0::code_numeral"
    8.96 +  (SML "0")
    8.97 +  (OCaml "Big'_int.zero'_big'_int")
    8.98 +  (Haskell "0")
    8.99 +  (Scala "BigInt(0)")
   8.100 +
   8.101  code_const "plus \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   8.102    (SML "Int.+/ ((_),/ (_))")
   8.103    (OCaml "Big'_int.add'_big'_int")
   8.104 @@ -314,12 +295,12 @@
   8.105    (Scala infixl 7 "+")
   8.106    (Eval infixl 8 "+")
   8.107  
   8.108 -code_const "Code_Numeral.subtract \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   8.109 -  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
   8.110 -  (OCaml "Big'_int.max'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)/ Big'_int.zero'_big'_int")
   8.111 -  (Haskell "max/ (_/ -/ _)/ (0 :: Integer)")
   8.112 +code_const "minus \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   8.113 +  (SML "Int.max/ (0 : int,/ Int.-/ ((_),/ (_)))")
   8.114 +  (OCaml "Big'_int.max'_big'_int/ Big'_int.zero'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)")
   8.115 +  (Haskell "max/ (0 :: Integer)/ (_/ -/ _)")
   8.116    (Scala "!(_/ -/ _).max(0)")
   8.117 -  (Eval "Integer.max/ (_/ -/ _)/ 0")
   8.118 +  (Eval "Integer.max/ 0/ (_/ -/ _)")
   8.119  
   8.120  code_const "times \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   8.121    (SML "Int.*/ ((_),/ (_))")
     9.1 --- a/src/HOL/Codegenerator_Test/Generate_Pretty.thy	Fri Mar 23 20:32:43 2012 +0100
     9.2 +++ b/src/HOL/Codegenerator_Test/Generate_Pretty.thy	Mon Mar 26 10:56:56 2012 +0200
     9.3 @@ -10,9 +10,8 @@
     9.4  lemma [code, code del]: "nat_of_char = nat_of_char" ..
     9.5  lemma [code, code del]: "char_of_nat = char_of_nat" ..
     9.6  
     9.7 -declare Quickcheck_Narrowing.zero_code_int_code[code del]
     9.8 -declare Quickcheck_Narrowing.one_code_int_code[code del]
     9.9 -declare Quickcheck_Narrowing.int_of_code[code del]
    9.10 +declare Quickcheck_Narrowing.one_code_int_code [code del]
    9.11 +declare Quickcheck_Narrowing.int_of_code [code del]
    9.12  
    9.13  subsection {* Check whether generated code compiles *}
    9.14  
    10.1 --- a/src/HOL/Complex.thy	Fri Mar 23 20:32:43 2012 +0100
    10.2 +++ b/src/HOL/Complex.thy	Mon Mar 26 10:56:56 2012 +0200
    10.3 @@ -151,17 +151,6 @@
    10.4  
    10.5  subsection {* Numerals and Arithmetic *}
    10.6  
    10.7 -instantiation complex :: number_ring
    10.8 -begin
    10.9 -
   10.10 -definition complex_number_of_def:
   10.11 -  "number_of w = (of_int w \<Colon> complex)"
   10.12 -
   10.13 -instance
   10.14 -  by intro_classes (simp only: complex_number_of_def)
   10.15 -
   10.16 -end
   10.17 -
   10.18  lemma complex_Re_of_nat [simp]: "Re (of_nat n) = of_nat n"
   10.19    by (induct n) simp_all
   10.20  
   10.21 @@ -174,14 +163,24 @@
   10.22  lemma complex_Im_of_int [simp]: "Im (of_int z) = 0"
   10.23    by (cases z rule: int_diff_cases) simp
   10.24  
   10.25 -lemma complex_Re_number_of [simp]: "Re (number_of v) = number_of v"
   10.26 -  unfolding number_of_eq by (rule complex_Re_of_int)
   10.27 +lemma complex_Re_numeral [simp]: "Re (numeral v) = numeral v"
   10.28 +  using complex_Re_of_int [of "numeral v"] by simp
   10.29  
   10.30 -lemma complex_Im_number_of [simp]: "Im (number_of v) = 0"
   10.31 -  unfolding number_of_eq by (rule complex_Im_of_int)
   10.32 +lemma complex_Re_neg_numeral [simp]: "Re (neg_numeral v) = neg_numeral v"
   10.33 +  using complex_Re_of_int [of "neg_numeral v"] by simp
   10.34  
   10.35 -lemma Complex_eq_number_of [simp]:
   10.36 -  "(Complex a b = number_of w) = (a = number_of w \<and> b = 0)"
   10.37 +lemma complex_Im_numeral [simp]: "Im (numeral v) = 0"
   10.38 +  using complex_Im_of_int [of "numeral v"] by simp
   10.39 +
   10.40 +lemma complex_Im_neg_numeral [simp]: "Im (neg_numeral v) = 0"
   10.41 +  using complex_Im_of_int [of "neg_numeral v"] by simp
   10.42 +
   10.43 +lemma Complex_eq_numeral [simp]:
   10.44 +  "(Complex a b = numeral w) = (a = numeral w \<and> b = 0)"
   10.45 +  by (simp add: complex_eq_iff)
   10.46 +
   10.47 +lemma Complex_eq_neg_numeral [simp]:
   10.48 +  "(Complex a b = neg_numeral w) = (a = neg_numeral w \<and> b = 0)"
   10.49    by (simp add: complex_eq_iff)
   10.50  
   10.51  
   10.52 @@ -421,7 +420,10 @@
   10.53  lemma complex_i_not_one [simp]: "ii \<noteq> 1"
   10.54    by (simp add: complex_eq_iff)
   10.55  
   10.56 -lemma complex_i_not_number_of [simp]: "ii \<noteq> number_of w"
   10.57 +lemma complex_i_not_numeral [simp]: "ii \<noteq> numeral w"
   10.58 +  by (simp add: complex_eq_iff)
   10.59 +
   10.60 +lemma complex_i_not_neg_numeral [simp]: "ii \<noteq> neg_numeral w"
   10.61    by (simp add: complex_eq_iff)
   10.62  
   10.63  lemma i_mult_Complex [simp]: "ii * Complex a b = Complex (- b) a"
   10.64 @@ -505,7 +507,10 @@
   10.65  lemma complex_cnj_of_int [simp]: "cnj (of_int z) = of_int z"
   10.66    by (simp add: complex_eq_iff)
   10.67  
   10.68 -lemma complex_cnj_number_of [simp]: "cnj (number_of w) = number_of w"
   10.69 +lemma complex_cnj_numeral [simp]: "cnj (numeral w) = numeral w"
   10.70 +  by (simp add: complex_eq_iff)
   10.71 +
   10.72 +lemma complex_cnj_neg_numeral [simp]: "cnj (neg_numeral w) = neg_numeral w"
   10.73    by (simp add: complex_eq_iff)
   10.74  
   10.75  lemma complex_cnj_scaleR: "cnj (scaleR r x) = scaleR r (cnj x)"
   10.76 @@ -686,10 +691,10 @@
   10.77    "(of_nat n :: 'a::linordered_idom) < of_int x \<longleftrightarrow> int n < x"
   10.78    by (metis of_int_of_nat_eq of_int_less_iff)
   10.79  
   10.80 -lemma real_of_nat_less_number_of_iff [simp]: (* TODO: move *)
   10.81 -  "real (n::nat) < number_of w \<longleftrightarrow> n < number_of w"
   10.82 -  unfolding real_of_nat_def nat_number_of_def number_of_eq
   10.83 -  by (simp add: of_nat_less_of_int_iff zless_nat_eq_int_zless)
   10.84 +lemma real_of_nat_less_numeral_iff [simp]: (* TODO: move *)
   10.85 +  "real (n::nat) < numeral w \<longleftrightarrow> n < numeral w"
   10.86 +  using of_nat_less_of_int_iff [of n "numeral w", where 'a=real]
   10.87 +  by (simp add: real_of_nat_def zless_nat_eq_int_zless [symmetric])
   10.88  
   10.89  lemma arg_unique:
   10.90    assumes "sgn z = cis x" and "-pi < x" and "x \<le> pi"
    11.1 --- a/src/HOL/Decision_Procs/Approximation.thy	Fri Mar 23 20:32:43 2012 +0100
    11.2 +++ b/src/HOL/Decision_Procs/Approximation.thy	Mon Mar 26 10:56:56 2012 +0200
    11.3 @@ -1350,7 +1350,7 @@
    11.4        also have "\<dots> \<le> cos ((?ux - 2 * ?lpi))"
    11.5          using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0]
    11.6          by (simp only: real_of_float_minus real_of_int_minus real_of_one
    11.7 -            number_of_Min diff_minus mult_minus_left mult_1_left)
    11.8 +            minus_one [symmetric] diff_minus mult_minus_left mult_1_left)
    11.9        also have "\<dots> = cos ((- (?ux - 2 * ?lpi)))"
   11.10          unfolding real_of_float_minus cos_minus ..
   11.11        also have "\<dots> \<le> (ub_cos prec (- (?ux - 2 * ?lpi)))"
   11.12 @@ -1394,7 +1394,7 @@
   11.13        also have "\<dots> \<le> cos ((?lx + 2 * ?lpi))"
   11.14          using cos_monotone_0_pi'[OF lx_0 lx_le_x pi_x]
   11.15          by (simp only: real_of_float_minus real_of_int_minus real_of_one
   11.16 -          number_of_Min diff_minus mult_minus_left mult_1_left)
   11.17 +          minus_one [symmetric] diff_minus mult_minus_left mult_1_left)
   11.18        also have "\<dots> \<le> (ub_cos prec (?lx + 2 * ?lpi))"
   11.19          using lb_cos[OF lx_0 pi_lx] by simp
   11.20        finally show ?thesis unfolding u by (simp add: real_of_float_max)
   11.21 @@ -2117,7 +2117,8 @@
   11.22  lemma interpret_floatarith_num:
   11.23    shows "interpret_floatarith (Num (Float 0 0)) vs = 0"
   11.24    and "interpret_floatarith (Num (Float 1 0)) vs = 1"
   11.25 -  and "interpret_floatarith (Num (Float (number_of a) 0)) vs = number_of a" by auto
   11.26 +  and "interpret_floatarith (Num (Float (numeral a) 0)) vs = numeral a"
   11.27 +  and "interpret_floatarith (Num (Float (neg_numeral a) 0)) vs = neg_numeral a" by auto
   11.28  
   11.29  subsection "Implement approximation function"
   11.30  
    12.1 --- a/src/HOL/Decision_Procs/Cooper.thy	Fri Mar 23 20:32:43 2012 +0100
    12.2 +++ b/src/HOL/Decision_Procs/Cooper.thy	Mon Mar 26 10:56:56 2012 +0200
    12.3 @@ -1883,7 +1883,8 @@
    12.4        | SOME n => @{code Bound} n)
    12.5    | num_of_term vs @{term "0::int"} = @{code C} 0
    12.6    | num_of_term vs @{term "1::int"} = @{code C} 1
    12.7 -  | num_of_term vs (@{term "number_of :: int \<Rightarrow> int"} $ t) = @{code C} (HOLogic.dest_numeral t)
    12.8 +  | num_of_term vs (@{term "numeral :: _ \<Rightarrow> int"} $ t) = @{code C} (HOLogic.dest_num t)
    12.9 +  | num_of_term vs (@{term "neg_numeral :: _ \<Rightarrow> int"} $ t) = @{code C} (~(HOLogic.dest_num t))
   12.10    | num_of_term vs (Bound i) = @{code Bound} i
   12.11    | num_of_term vs (@{term "uminus :: int \<Rightarrow> int"} $ t') = @{code Neg} (num_of_term vs t')
   12.12    | num_of_term vs (@{term "op + :: int \<Rightarrow> int \<Rightarrow> int"} $ t1 $ t2) =
    13.1 --- a/src/HOL/Decision_Procs/Dense_Linear_Order.thy	Fri Mar 23 20:32:43 2012 +0100
    13.2 +++ b/src/HOL/Decision_Procs/Dense_Linear_Order.thy	Mon Mar 26 10:56:56 2012 +0200
    13.3 @@ -636,14 +636,8 @@
    13.4  
    13.5  interpretation class_dense_linordered_field: constr_dense_linorder
    13.6   "op <=" "op <"
    13.7 -   "\<lambda> x y. 1/2 * ((x::'a::{linordered_field,number_ring}) + y)"
    13.8 -proof (unfold_locales, dlo, dlo, auto)
    13.9 -  fix x y::'a assume lt: "x < y"
   13.10 -  from  less_half_sum[OF lt] show "x < (x + y) /2" by simp
   13.11 -next
   13.12 -  fix x y::'a assume lt: "x < y"
   13.13 -  from  gt_half_sum[OF lt] show "(x + y) /2 < y" by simp
   13.14 -qed
   13.15 +   "\<lambda> x y. 1/2 * ((x::'a::{linordered_field}) + y)"
   13.16 +by (unfold_locales, dlo, dlo, auto)
   13.17  
   13.18  declaration{*
   13.19  let
    14.1 --- a/src/HOL/Decision_Procs/Ferrack.thy	Fri Mar 23 20:32:43 2012 +0100
    14.2 +++ b/src/HOL/Decision_Procs/Ferrack.thy	Mon Mar 26 10:56:56 2012 +0200
    14.3 @@ -1732,7 +1732,7 @@
    14.4           (set U \<times> set U)"using mnz nnz th  
    14.5      apply (auto simp add: th add_divide_distrib algebra_simps split_def image_def)
    14.6      by (rule_tac x="(s,m)" in bexI,simp_all) 
    14.7 -  (rule_tac x="(t,n)" in bexI,simp_all)
    14.8 +  (rule_tac x="(t,n)" in bexI,simp_all add: mult_commute)
    14.9  next
   14.10    fix t n s m
   14.11    assume tnU: "(t,n) \<in> set U" and smU:"(s,m) \<in> set U" 
   14.12 @@ -1937,11 +1937,12 @@
   14.13    | num_of_term vs (@{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) = (case num_of_term vs t1
   14.14       of @{code C} i => @{code Mul} (i, num_of_term vs t2)
   14.15        | _ => error "num_of_term: unsupported multiplication")
   14.16 -  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "number_of :: int \<Rightarrow> int"} $ t')) =
   14.17 -     @{code C} (HOLogic.dest_numeral t')
   14.18 -  | num_of_term vs (@{term "number_of :: int \<Rightarrow> real"} $ t') =
   14.19 -     @{code C} (HOLogic.dest_numeral t')
   14.20 -  | num_of_term vs t = error ("num_of_term: unknown term");
   14.21 +  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ t') =
   14.22 +     (@{code C} (snd (HOLogic.dest_number t'))
   14.23 +       handle TERM _ => error ("num_of_term: unknown term"))
   14.24 +  | num_of_term vs t' =
   14.25 +     (@{code C} (snd (HOLogic.dest_number t'))
   14.26 +       handle TERM _ => error ("num_of_term: unknown term"));
   14.27  
   14.28  fun fm_of_term vs @{term True} = @{code T}
   14.29    | fm_of_term vs @{term False} = @{code F}
    15.1 --- a/src/HOL/Decision_Procs/MIR.thy	Fri Mar 23 20:32:43 2012 +0100
    15.2 +++ b/src/HOL/Decision_Procs/MIR.thy	Mon Mar 26 10:56:56 2012 +0200
    15.3 @@ -4901,7 +4901,7 @@
    15.4           (set U \<times> set U)"using mnz nnz th  
    15.5      apply (auto simp add: th add_divide_distrib algebra_simps split_def image_def)
    15.6      by (rule_tac x="(s,m)" in bexI,simp_all) 
    15.7 -  (rule_tac x="(t,n)" in bexI,simp_all)
    15.8 +  (rule_tac x="(t,n)" in bexI,simp_all add: mult_commute)
    15.9  next
   15.10    fix t n s m
   15.11    assume tnU: "(t,n) \<in> set U" and smU:"(s,m) \<in> set U" 
   15.12 @@ -5536,14 +5536,18 @@
   15.13        (case (num_of_term vs t1)
   15.14         of @{code C} i => @{code Mul} (i, num_of_term vs t2)
   15.15          | _ => error "num_of_term: unsupported Multiplication")
   15.16 -  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "number_of :: int \<Rightarrow> int"} $ t')) =
   15.17 -      @{code C} (HOLogic.dest_numeral t')
   15.18 +  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "numeral :: _ \<Rightarrow> int"} $ t')) =
   15.19 +      @{code C} (HOLogic.dest_num t')
   15.20 +  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "neg_numeral :: _ \<Rightarrow> int"} $ t')) =
   15.21 +      @{code C} (~ (HOLogic.dest_num t'))
   15.22    | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "floor :: real \<Rightarrow> int"} $ t')) =
   15.23        @{code Floor} (num_of_term vs t')
   15.24    | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "ceiling :: real \<Rightarrow> int"} $ t')) =
   15.25        @{code Neg} (@{code Floor} (@{code Neg} (num_of_term vs t')))
   15.26 -  | num_of_term vs (@{term "number_of :: int \<Rightarrow> real"} $ t') =
   15.27 -      @{code C} (HOLogic.dest_numeral t')
   15.28 +  | num_of_term vs (@{term "numeral :: _ \<Rightarrow> real"} $ t') =
   15.29 +      @{code C} (HOLogic.dest_num t')
   15.30 +  | num_of_term vs (@{term "neg_numeral :: _ \<Rightarrow> real"} $ t') =
   15.31 +      @{code C} (~ (HOLogic.dest_num t'))
   15.32    | num_of_term vs t = error ("num_of_term: unknown term " ^ Syntax.string_of_term @{context} t);
   15.33  
   15.34  fun fm_of_term vs @{term True} = @{code T}
   15.35 @@ -5554,8 +5558,10 @@
   15.36        @{code Le} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
   15.37    | fm_of_term vs (@{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
   15.38        @{code Eq} (@{code Sub} (num_of_term vs t1, num_of_term vs t2)) 
   15.39 -  | fm_of_term vs (@{term "op rdvd"} $ (@{term "real :: int \<Rightarrow> real"} $ (@{term "number_of :: int \<Rightarrow> int"} $ t1)) $ t2) =
   15.40 -      @{code Dvd} (HOLogic.dest_numeral t1, num_of_term vs t2)
   15.41 +  | fm_of_term vs (@{term "op rdvd"} $ (@{term "real :: int \<Rightarrow> real"} $ (@{term "numeral :: _ \<Rightarrow> int"} $ t1)) $ t2) =
   15.42 +      @{code Dvd} (HOLogic.dest_num t1, num_of_term vs t2)
   15.43 +  | fm_of_term vs (@{term "op rdvd"} $ (@{term "real :: int \<Rightarrow> real"} $ (@{term "neg_numeral :: _ \<Rightarrow> int"} $ t1)) $ t2) =
   15.44 +      @{code Dvd} (~ (HOLogic.dest_num t1), num_of_term vs t2)
   15.45    | fm_of_term vs (@{term "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ t1 $ t2) =
   15.46        @{code Iff} (fm_of_term vs t1, fm_of_term vs t2)
   15.47    | fm_of_term vs (@{term HOL.conj} $ t1 $ t2) =
    16.1 --- a/src/HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy	Fri Mar 23 20:32:43 2012 +0100
    16.2 +++ b/src/HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy	Mon Mar 26 10:56:56 2012 +0200
    16.3 @@ -25,7 +25,7 @@
    16.4  | "tmsize (CNP n c a) = 3 + polysize c + tmsize a "
    16.5  
    16.6    (* Semantics of terms tm *)
    16.7 -primrec Itm :: "'a::{field_char_0, field_inverse_zero, number_ring} list \<Rightarrow> 'a list \<Rightarrow> tm \<Rightarrow> 'a" where
    16.8 +primrec Itm :: "'a::{field_char_0, field_inverse_zero} list \<Rightarrow> 'a list \<Rightarrow> tm \<Rightarrow> 'a" where
    16.9    "Itm vs bs (CP c) = (Ipoly vs c)"
   16.10  | "Itm vs bs (Bound n) = bs!n"
   16.11  | "Itm vs bs (Neg a) = -(Itm vs bs a)"
   16.12 @@ -430,7 +430,7 @@
   16.13  by (induct p rule: fmsize.induct) simp_all
   16.14  
   16.15    (* Semantics of formulae (fm) *)
   16.16 -primrec Ifm ::"'a::{linordered_field_inverse_zero, number_ring} list \<Rightarrow> 'a list \<Rightarrow> fm \<Rightarrow> bool" where
   16.17 +primrec Ifm ::"'a::{linordered_field_inverse_zero} list \<Rightarrow> 'a list \<Rightarrow> fm \<Rightarrow> bool" where
   16.18    "Ifm vs bs T = True"
   16.19  | "Ifm vs bs F = False"
   16.20  | "Ifm vs bs (Lt a) = (Itm vs bs a < 0)"
   16.21 @@ -1937,7 +1937,7 @@
   16.22      
   16.23      also have "\<dots> \<longleftrightarrow> - (?a * ?s) + 2*?d*?r = 0" using d by simp 
   16.24      finally have ?thesis using c d 
   16.25 -      by (simp add: r[of "- (Itm vs (x # bs) s / (2 * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"] msubsteq_def Let_def evaldjf_ex del: one_add_one_is_two)
   16.26 +      by (simp add: r[of "- (Itm vs (x # bs) s / (2 * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"] msubsteq_def Let_def evaldjf_ex)
   16.27    }
   16.28    moreover
   16.29    {assume c: "?c \<noteq> 0" and d: "?d=0"
   16.30 @@ -1950,7 +1950,7 @@
   16.31        by (simp add: field_simps right_distrib[of "2*?c"] del: right_distrib)
   16.32      also have "\<dots> \<longleftrightarrow> - (?a * ?t) + 2*?c*?r = 0" using c by simp 
   16.33      finally have ?thesis using c d 
   16.34 -      by (simp add: r[of "- (?t/ (2*?c))"] msubsteq_def Let_def evaldjf_ex del: one_add_one_is_two)
   16.35 +      by (simp add: r[of "- (?t/ (2*?c))"] msubsteq_def Let_def evaldjf_ex)
   16.36    }
   16.37    moreover
   16.38    {assume c: "?c \<noteq> 0" and d: "?d\<noteq>0" hence dc: "?c * ?d *2 \<noteq> 0" by simp
   16.39 @@ -2019,7 +2019,7 @@
   16.40      
   16.41      also have "\<dots> \<longleftrightarrow> - (?a * ?s) + 2*?d*?r \<noteq> 0" using d by simp 
   16.42      finally have ?thesis using c d 
   16.43 -      by (simp add: r[of "- (Itm vs (x # bs) s / (2 * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"] msubstneq_def Let_def evaldjf_ex del: one_add_one_is_two)
   16.44 +      by (simp add: r[of "- (Itm vs (x # bs) s / (2 * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"] msubstneq_def Let_def evaldjf_ex)
   16.45    }
   16.46    moreover
   16.47    {assume c: "?c \<noteq> 0" and d: "?d=0"
   16.48 @@ -2032,7 +2032,7 @@
   16.49        by (simp add: field_simps right_distrib[of "2*?c"] del: right_distrib)
   16.50      also have "\<dots> \<longleftrightarrow> - (?a * ?t) + 2*?c*?r \<noteq> 0" using c by simp 
   16.51      finally have ?thesis using c d 
   16.52 -      by (simp add: r[of "- (?t/ (2*?c))"] msubstneq_def Let_def evaldjf_ex del: one_add_one_is_two)
   16.53 +      by (simp add: r[of "- (?t/ (2*?c))"] msubstneq_def Let_def evaldjf_ex)
   16.54    }
   16.55    moreover
   16.56    {assume c: "?c \<noteq> 0" and d: "?d\<noteq>0" hence dc: "?c * ?d *2 \<noteq> 0" by simp
   16.57 @@ -2616,10 +2616,10 @@
   16.58  using lp tnb
   16.59  by (simp add: msubst2_def msubstneg_nb msubstpos_nb conj_nb disj_nb lt_nb simpfm_bound0)
   16.60  
   16.61 -lemma mult_minus2_left: "-2 * (x::'a::number_ring) = - (2 * x)"
   16.62 +lemma mult_minus2_left: "-2 * (x::'a::comm_ring_1) = - (2 * x)"
   16.63    by simp
   16.64  
   16.65 -lemma mult_minus2_right: "(x::'a::number_ring) * -2 = - (x * 2)"
   16.66 +lemma mult_minus2_right: "(x::'a::comm_ring_1) * -2 = - (x * 2)"
   16.67    by simp
   16.68  
   16.69  lemma islin_qf: "islin p \<Longrightarrow> qfree p"
   16.70 @@ -3005,11 +3005,11 @@
   16.71  *} "parametric QE for linear Arithmetic over fields, Version 2"
   16.72  
   16.73  
   16.74 -lemma "\<exists>(x::'a::{linordered_field_inverse_zero, number_ring}). y \<noteq> -1 \<longrightarrow> (y + 1)*x < 0"
   16.75 -  apply (frpar type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "y::'a::{linordered_field_inverse_zero, number_ring}")
   16.76 +lemma "\<exists>(x::'a::{linordered_field_inverse_zero}). y \<noteq> -1 \<longrightarrow> (y + 1)*x < 0"
   16.77 +  apply (frpar type: "'a::{linordered_field_inverse_zero}" pars: "y::'a::{linordered_field_inverse_zero}")
   16.78    apply (simp add: field_simps)
   16.79    apply (rule spec[where x=y])
   16.80 -  apply (frpar type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "z::'a::{linordered_field_inverse_zero, number_ring}")
   16.81 +  apply (frpar type: "'a::{linordered_field_inverse_zero}" pars: "z::'a::{linordered_field_inverse_zero}")
   16.82    by simp
   16.83  
   16.84  text{* Collins/Jones Problem *}
   16.85 @@ -3030,11 +3030,11 @@
   16.86  oops
   16.87  *)
   16.88  
   16.89 -lemma "\<exists>(x::'a::{linordered_field_inverse_zero, number_ring}). y \<noteq> -1 \<longrightarrow> (y + 1)*x < 0"
   16.90 -  apply (frpar2 type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "y::'a::{linordered_field_inverse_zero, number_ring}")
   16.91 +lemma "\<exists>(x::'a::{linordered_field_inverse_zero}). y \<noteq> -1 \<longrightarrow> (y + 1)*x < 0"
   16.92 +  apply (frpar2 type: "'a::{linordered_field_inverse_zero}" pars: "y::'a::{linordered_field_inverse_zero}")
   16.93    apply (simp add: field_simps)
   16.94    apply (rule spec[where x=y])
   16.95 -  apply (frpar2 type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "z::'a::{linordered_field_inverse_zero, number_ring}")
   16.96 +  apply (frpar2 type: "'a::{linordered_field_inverse_zero}" pars: "z::'a::{linordered_field_inverse_zero}")
   16.97    by simp
   16.98  
   16.99  text{* Collins/Jones Problem *}
    17.1 --- a/src/HOL/Decision_Procs/cooper_tac.ML	Fri Mar 23 20:32:43 2012 +0100
    17.2 +++ b/src/HOL/Decision_Procs/cooper_tac.ML	Mon Mar 26 10:56:56 2012 +0200
    17.3 @@ -18,15 +18,12 @@
    17.4  val cooper_ss = @{simpset};
    17.5  
    17.6  val nT = HOLogic.natT;
    17.7 -val binarith = @{thms normalize_bin_simps};
    17.8 -val comp_arith = binarith @ @{thms simp_thms};
    17.9 +val comp_arith = @{thms simp_thms}
   17.10  
   17.11  val zdvd_int = @{thm zdvd_int};
   17.12  val zdiff_int_split = @{thm zdiff_int_split};
   17.13  val all_nat = @{thm all_nat};
   17.14  val ex_nat = @{thm ex_nat};
   17.15 -val number_of1 = @{thm number_of1};
   17.16 -val number_of2 = @{thm number_of2};
   17.17  val split_zdiv = @{thm split_zdiv};
   17.18  val split_zmod = @{thm split_zmod};
   17.19  val mod_div_equality' = @{thm mod_div_equality'};
   17.20 @@ -90,14 +87,13 @@
   17.21            [split_zdiv, split_zmod, split_div', @{thm "split_min"}, @{thm "split_max"}]
   17.22      (* Simp rules for changing (n::int) to int n *)
   17.23      val simpset1 = HOL_basic_ss
   17.24 -      addsimps [@{thm nat_number_of_def}, zdvd_int] @ map (fn r => r RS sym)
   17.25 -        [@{thm int_int_eq}, @{thm zle_int}, @{thm zless_int}, @{thm zadd_int}, @{thm zmult_int}]
   17.26 +      addsimps [zdvd_int] @ map (fn r => r RS sym)
   17.27 +        [@{thm int_numeral}, @{thm int_int_eq}, @{thm zle_int}, @{thm zless_int}, @{thm zadd_int}, @{thm zmult_int}]
   17.28        |> Splitter.add_split zdiff_int_split
   17.29      (*simp rules for elimination of int n*)
   17.30  
   17.31      val simpset2 = HOL_basic_ss
   17.32 -      addsimps [@{thm nat_0_le}, @{thm all_nat}, @{thm ex_nat},
   17.33 -        @{thm number_of1}, @{thm number_of2}, @{thm int_0}, @{thm int_1}]
   17.34 +      addsimps [@{thm nat_0_le}, @{thm all_nat}, @{thm ex_nat}, @{thm zero_le_numeral}, @{thm order_refl}(* FIXME: necessary? *), @{thm int_0}, @{thm int_1}]
   17.35        |> fold Simplifier.add_cong [@{thm conj_le_cong}, @{thm imp_le_cong}]
   17.36      (* simp rules for elimination of abs *)
   17.37      val simpset3 = HOL_basic_ss |> Splitter.add_split @{thm abs_split}
    18.1 --- a/src/HOL/Decision_Procs/ex/Dense_Linear_Order_Ex.thy	Fri Mar 23 20:32:43 2012 +0100
    18.2 +++ b/src/HOL/Decision_Procs/ex/Dense_Linear_Order_Ex.thy	Mon Mar 26 10:56:56 2012 +0200
    18.3 @@ -7,147 +7,147 @@
    18.4  begin
    18.5  
    18.6  lemma
    18.7 -  "\<exists>(y::'a::{linordered_field_inverse_zero, number_ring}) <2. x + 3* y < 0 \<and> x - y >0"
    18.8 +  "\<exists>(y::'a::{linordered_field_inverse_zero}) <2. x + 3* y < 0 \<and> x - y >0"
    18.9    by ferrack
   18.10  
   18.11 -lemma "~ (ALL x (y::'a::{linordered_field_inverse_zero, number_ring}). x < y --> 10*x < 11*y)"
   18.12 +lemma "~ (ALL x (y::'a::{linordered_field_inverse_zero}). x < y --> 10*x < 11*y)"
   18.13    by ferrack
   18.14  
   18.15 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. x < y --> (10*(x + 5*y + -1) < 60*y)"
   18.16 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y. x < y --> (10*(x + 5*y + -1) < 60*y)"
   18.17    by ferrack
   18.18  
   18.19 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y. x ~= y --> x < y"
   18.20 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y. x ~= y --> x < y"
   18.21    by ferrack
   18.22  
   18.23 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y. (x ~= y & 10*x ~= 9*y & 10*x < y) --> x < y"
   18.24 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y. (x ~= y & 10*x ~= 9*y & 10*x < y) --> x < y"
   18.25    by ferrack
   18.26  
   18.27 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. (x ~= y & 5*x <= y) --> 500*x <= 100*y"
   18.28 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y. (x ~= y & 5*x <= y) --> 500*x <= 100*y"
   18.29    by ferrack
   18.30  
   18.31 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX (y::'a::{linordered_field_inverse_zero, number_ring}). 4*x + 3*y <= 0 & 4*x + 3*y >= -1)"
   18.32 +lemma "ALL (x::'a::{linordered_field_inverse_zero}). (EX (y::'a::{linordered_field_inverse_zero}). 4*x + 3*y <= 0 & 4*x + 3*y >= -1)"
   18.33    by ferrack
   18.34  
   18.35 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) < 0. (EX (y::'a::{linordered_field_inverse_zero, number_ring}) > 0. 7*x + y > 0 & x - y <= 9)"
   18.36 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) < 0. (EX (y::'a::{linordered_field_inverse_zero}) > 0. 7*x + y > 0 & x - y <= 9)"
   18.37    by ferrack
   18.38  
   18.39 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}). (0 < x & x < 1) --> (ALL y > 1. x + y ~= 1)"
   18.40 +lemma "EX (x::'a::{linordered_field_inverse_zero}). (0 < x & x < 1) --> (ALL y > 1. x + y ~= 1)"
   18.41    by ferrack
   18.42  
   18.43 -lemma "EX x. (ALL (y::'a::{linordered_field_inverse_zero, number_ring}). y < 2 -->  2*(y - x) \<le> 0 )"
   18.44 +lemma "EX x. (ALL (y::'a::{linordered_field_inverse_zero}). y < 2 -->  2*(y - x) \<le> 0 )"
   18.45    by ferrack
   18.46  
   18.47 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}). x < 10 | x > 20 | (EX y. y>= 0 & y <= 10 & x+y = 20)"
   18.48 +lemma "ALL (x::'a::{linordered_field_inverse_zero}). x < 10 | x > 20 | (EX y. y>= 0 & y <= 10 & x+y = 20)"
   18.49    by ferrack
   18.50  
   18.51 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y z. x + y < z --> y >= z --> x < 0"
   18.52 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y z. x + y < z --> y >= z --> x < 0"
   18.53    by ferrack
   18.54  
   18.55 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y z. x + 7*y < 5* z & 5*y >= 7*z & x < 0"
   18.56 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y z. x + 7*y < 5* z & 5*y >= 7*z & x < 0"
   18.57    by ferrack
   18.58  
   18.59 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y z. abs (x + y) <= z --> (abs z = z)"
   18.60 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y z. abs (x + y) <= z --> (abs z = z)"
   18.61    by ferrack
   18.62  
   18.63 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y z. x + 7*y - 5* z < 0 & 5*y + 7*z + 3*x < 0"
   18.64 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y z. x + 7*y - 5* z < 0 & 5*y + 7*z + 3*x < 0"
   18.65    by ferrack
   18.66  
   18.67 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y z. (abs (5*x+3*y+z) <= 5*x+3*y+z & abs (5*x+3*y+z) >= - (5*x+3*y+z)) | (abs (5*x+3*y+z) >= 5*x+3*y+z & abs (5*x+3*y+z) <= - (5*x+3*y+z))"
   18.68 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y z. (abs (5*x+3*y+z) <= 5*x+3*y+z & abs (5*x+3*y+z) >= - (5*x+3*y+z)) | (abs (5*x+3*y+z) >= 5*x+3*y+z & abs (5*x+3*y+z) <= - (5*x+3*y+z))"
   18.69    by ferrack
   18.70  
   18.71 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. x < y --> (EX z>0. x+z = y)"
   18.72 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y. x < y --> (EX z>0. x+z = y)"
   18.73    by ferrack
   18.74  
   18.75 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. x < y --> (EX z>0. x+z = y)"
   18.76 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y. x < y --> (EX z>0. x+z = y)"
   18.77    by ferrack
   18.78  
   18.79 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. (EX z>0. abs (x - y) <= z )"
   18.80 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y. (EX z>0. abs (x - y) <= z )"
   18.81    by ferrack
   18.82  
   18.83 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y. (ALL z<0. (z < x --> z <= y) & (z > y --> z >= x))"
   18.84 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y. (ALL z<0. (z < x --> z <= y) & (z > y --> z >= x))"
   18.85    by ferrack
   18.86  
   18.87 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y. (ALL z>=0. abs (3*x+7*y) <= 2*z + 1)"
   18.88 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y. (ALL z>=0. abs (3*x+7*y) <= 2*z + 1)"
   18.89    by ferrack
   18.90  
   18.91 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y. (ALL z<0. (z < x --> z <= y) & (z > y --> z >= x))"
   18.92 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y. (ALL z<0. (z < x --> z <= y) & (z > y --> z >= x))"
   18.93    by ferrack
   18.94  
   18.95 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring})>0. (ALL y. (EX z. 13* abs z \<noteq> abs (12*y - x) & 5*x - 3*(abs y) <= 7*z))"
   18.96 +lemma "EX (x::'a::{linordered_field_inverse_zero})>0. (ALL y. (EX z. 13* abs z \<noteq> abs (12*y - x) & 5*x - 3*(abs y) <= 7*z))"
   18.97    by ferrack
   18.98  
   18.99 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}). abs (4*x + 17) < 4 & (ALL y . abs (x*34 - 34*y - 9) \<noteq> 0 \<longrightarrow> (EX z. 5*x - 3*abs y <= 7*z))"
  18.100 +lemma "EX (x::'a::{linordered_field_inverse_zero}). abs (4*x + 17) < 4 & (ALL y . abs (x*34 - 34*y - 9) \<noteq> 0 \<longrightarrow> (EX z. 5*x - 3*abs y <= 7*z))"
  18.101    by ferrack
  18.102  
  18.103 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX y > abs (23*x - 9). (ALL z > abs (3*y - 19* abs x). x+z > 2*y))"
  18.104 +lemma "ALL (x::'a::{linordered_field_inverse_zero}). (EX y > abs (23*x - 9). (ALL z > abs (3*y - 19* abs x). x+z > 2*y))"
  18.105    by ferrack
  18.106  
  18.107 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX y< abs (3*x - 1). (ALL z >= (3*abs x - 1). abs (12*x - 13*y + 19*z) > abs (23*x) ))"
  18.108 +lemma "ALL (x::'a::{linordered_field_inverse_zero}). (EX y< abs (3*x - 1). (ALL z >= (3*abs x - 1). abs (12*x - 13*y + 19*z) > abs (23*x) ))"
  18.109    by ferrack
  18.110  
  18.111 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}). abs x < 100 & (ALL y > x. (EX z<2*y - x. 5*x - 3*y <= 7*z))"
  18.112 +lemma "EX (x::'a::{linordered_field_inverse_zero}). abs x < 100 & (ALL y > x. (EX z<2*y - x. 5*x - 3*y <= 7*z))"
  18.113    by ferrack
  18.114  
  18.115 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y z w. 7*x<3*y --> 5*y < 7*z --> z < 2*w --> 7*(2*w-x) > 2*y"
  18.116 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y z w. 7*x<3*y --> 5*y < 7*z --> z < 2*w --> 7*(2*w-x) > 2*y"
  18.117    by ferrack
  18.118  
  18.119 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y z w. 5*x + 3*z - 17*w + abs (y - 8*x + z) <= 89"
  18.120 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y z w. 5*x + 3*z - 17*w + abs (y - 8*x + z) <= 89"
  18.121    by ferrack
  18.122  
  18.123 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y z w. 5*x + 3*z - 17*w + 7* (y - 8*x + z) <= max y (7*z - x + w)"
  18.124 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y z w. 5*x + 3*z - 17*w + 7* (y - 8*x + z) <= max y (7*z - x + w)"
  18.125    by ferrack
  18.126  
  18.127 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y z w. min (5*x + 3*z) (17*w) + 5* abs (y - 8*x + z) <= max y (7*z - x + w)"
  18.128 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y z w. min (5*x + 3*z) (17*w) + 5* abs (y - 8*x + z) <= max y (7*z - x + w)"
  18.129    by ferrack
  18.130  
  18.131 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y z. (EX w >= (x+y+z). w <= abs x + abs y + abs z)"
  18.132 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y z. (EX w >= (x+y+z). w <= abs x + abs y + abs z)"
  18.133    by ferrack
  18.134  
  18.135 -lemma "~(ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX y z w. 3* x + z*4 = 3*y & x + y < z & x> w & 3*x < w + y))"
  18.136 +lemma "~(ALL (x::'a::{linordered_field_inverse_zero}). (EX y z w. 3* x + z*4 = 3*y & x + y < z & x> w & 3*x < w + y))"
  18.137    by ferrack
  18.138  
  18.139 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. (EX z w. abs (x-y) = (z-w) & z*1234 < 233*x & w ~= y)"
  18.140 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y. (EX z w. abs (x-y) = (z-w) & z*1234 < 233*x & w ~= y)"
  18.141    by ferrack
  18.142  
  18.143 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX y z w. min (5*x + 3*z) (17*w) + 5* abs (y - 8*x + z) <= max y (7*z - x + w))"
  18.144 +lemma "ALL (x::'a::{linordered_field_inverse_zero}). (EX y z w. min (5*x + 3*z) (17*w) + 5* abs (y - 8*x + z) <= max y (7*z - x + w))"
  18.145    by ferrack
  18.146  
  18.147 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y z. (ALL w >= abs (x+y+z). w >= abs x + abs y + abs z)"
  18.148 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y z. (ALL w >= abs (x+y+z). w >= abs x + abs y + abs z)"
  18.149    by ferrack
  18.150  
  18.151 -lemma "EX z. (ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. (EX w >= (x+y+z). w <= abs x + abs y + abs z))"
  18.152 +lemma "EX z. (ALL (x::'a::{linordered_field_inverse_zero}) y. (EX w >= (x+y+z). w <= abs x + abs y + abs z))"
  18.153    by ferrack
  18.154  
  18.155 -lemma "EX z. (ALL (x::'a::{linordered_field_inverse_zero, number_ring}) < abs z. (EX y w. x< y & x < z & x> w & 3*x < w + y))"
  18.156 +lemma "EX z. (ALL (x::'a::{linordered_field_inverse_zero}) < abs z. (EX y w. x< y & x < z & x> w & 3*x < w + y))"
  18.157    by ferrack
  18.158  
  18.159 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. (EX z. (ALL w. abs (x-y) = abs (z-w) --> z < x & w ~= y))"
  18.160 +lemma "ALL (x::'a::{linordered_field_inverse_zero}) y. (EX z. (ALL w. abs (x-y) = abs (z-w) --> z < x & w ~= y))"
  18.161    by ferrack
  18.162  
  18.163 -lemma "EX y. (ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX z w. min (5*x + 3*z) (17*w) + 5* abs (y - 8*x + z) <= max y (7*z - x + w)))"
  18.164 +lemma "EX y. (ALL (x::'a::{linordered_field_inverse_zero}). (EX z w. min (5*x + 3*z) (17*w) + 5* abs (y - 8*x + z) <= max y (7*z - x + w)))"
  18.165    by ferrack
  18.166  
  18.167 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) z. (ALL w >= 13*x - 4*z. (EX y. w >= abs x + abs y + z))"
  18.168 +lemma "EX (x::'a::{linordered_field_inverse_zero}) z. (ALL w >= 13*x - 4*z. (EX y. w >= abs x + abs y + z))"
  18.169    by ferrack
  18.170  
  18.171 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}). (ALL y < x. (EX z > (x+y).
  18.172 +lemma "EX (x::'a::{linordered_field_inverse_zero}). (ALL y < x. (EX z > (x+y).
  18.173    (ALL w. 5*w + 10*x - z >= y --> w + 7*x + 3*z >= 2*y)))"
  18.174    by ferrack
  18.175  
  18.176 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}). (ALL y. (EX z > y.
  18.177 +lemma "EX (x::'a::{linordered_field_inverse_zero}). (ALL y. (EX z > y.
  18.178    (ALL w . w < 13 --> w + 10*x - z >= y --> 5*w + 7*x + 13*z >= 2*y)))"
  18.179    by ferrack
  18.180  
  18.181 -lemma "EX (x::'a::{linordered_field_inverse_zero, number_ring}) y z w. min (5*x + 3*z) (17*w) + 5* abs (y - 8*x + z) <= max y (7*z - x + w)"
  18.182 +lemma "EX (x::'a::{linordered_field_inverse_zero}) y z w. min (5*x + 3*z) (17*w) + 5* abs (y - 8*x + z) <= max y (7*z - x + w)"
  18.183    by ferrack
  18.184  
  18.185 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX y. (ALL z>19. y <= x + z & (EX w. abs (y - x) < w)))"
  18.186 +lemma "ALL (x::'a::{linordered_field_inverse_zero}). (EX y. (ALL z>19. y <= x + z & (EX w. abs (y - x) < w)))"
  18.187    by ferrack
  18.188  
  18.189 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX y. (ALL z>19. y <= x + z & (EX w. abs (x + z) < w - y)))"
  18.190 +lemma "ALL (x::'a::{linordered_field_inverse_zero}). (EX y. (ALL z>19. y <= x + z & (EX w. abs (x + z) < w - y)))"
  18.191    by ferrack
  18.192  
  18.193 -lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}). (EX y. abs y ~= abs x & (ALL z> max x y. (EX w. w ~= y & w ~= z & 3*w - z >= x + y)))"
  18.194 +lemma "ALL (x::'a::{linordered_field_inverse_zero}). (EX y. abs y ~= abs x & (ALL z> max x y. (EX w. w ~= y & w ~= z & 3*w - z >= x + y)))"
  18.195    by ferrack
  18.196  
  18.197  end
    19.1 --- a/src/HOL/Decision_Procs/ferrack_tac.ML	Fri Mar 23 20:32:43 2012 +0100
    19.2 +++ b/src/HOL/Decision_Procs/ferrack_tac.ML	Mon Mar 26 10:56:56 2012 +0200
    19.3 @@ -20,17 +20,13 @@
    19.4               in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
    19.5               end;
    19.6  
    19.7 -val binarith =
    19.8 -  @{thms normalize_bin_simps} @ @{thms pred_bin_simps} @ @{thms succ_bin_simps} @
    19.9 -  @{thms add_bin_simps} @ @{thms minus_bin_simps} @  @{thms mult_bin_simps};
   19.10 -val comp_arith = binarith @ @{thms simp_thms};
   19.11 +val binarith = @{thms arith_simps}
   19.12 +val comp_arith = binarith @ @{thms simp_thms}
   19.13  
   19.14  val zdvd_int = @{thm zdvd_int};
   19.15  val zdiff_int_split = @{thm zdiff_int_split};
   19.16  val all_nat = @{thm all_nat};
   19.17  val ex_nat = @{thm ex_nat};
   19.18 -val number_of1 = @{thm number_of1};
   19.19 -val number_of2 = @{thm number_of2};
   19.20  val split_zdiv = @{thm split_zdiv};
   19.21  val split_zmod = @{thm split_zmod};
   19.22  val mod_div_equality' = @{thm mod_div_equality'};
    20.1 --- a/src/HOL/Decision_Procs/mir_tac.ML	Fri Mar 23 20:32:43 2012 +0100
    20.2 +++ b/src/HOL/Decision_Procs/mir_tac.ML	Mon Mar 26 10:56:56 2012 +0200
    20.3 @@ -21,16 +21,15 @@
    20.4  end;
    20.5  
    20.6  val nT = HOLogic.natT;
    20.7 -  val nat_arith = [@{thm "add_nat_number_of"}, @{thm "diff_nat_number_of"},
    20.8 -                       @{thm "mult_nat_number_of"}, @{thm "eq_nat_number_of"}, @{thm "less_nat_number_of"}];
    20.9 +  val nat_arith = [@{thm diff_nat_numeral}];
   20.10  
   20.11    val comp_arith = [@{thm "Let_def"}, @{thm "if_False"}, @{thm "if_True"}, @{thm "add_0"},
   20.12 -                 @{thm "add_Suc"}, @{thm "add_number_of_left"}, @{thm "mult_number_of_left"},
   20.13 +                 @{thm "add_Suc"}, @{thm add_numeral_left}, @{thm mult_numeral_left(1)},
   20.14                   @{thm "Suc_eq_plus1"}] @
   20.15 -                 (map (fn th => th RS sym) [@{thm "numeral_1_eq_1"}, @{thm "numeral_0_eq_0"}])
   20.16 +                 (map (fn th => th RS sym) [@{thm "numeral_1_eq_1"}])
   20.17                   @ @{thms arith_simps} @ nat_arith @ @{thms rel_simps} 
   20.18    val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
   20.19 -             @{thm "real_of_nat_number_of"},
   20.20 +             @{thm real_of_nat_numeral},
   20.21               @{thm "real_of_nat_Suc"}, @{thm "real_of_nat_one"}, @{thm "real_of_one"},
   20.22               @{thm "real_of_int_zero"}, @{thm "real_of_nat_zero"},
   20.23               @{thm "divide_zero"}, 
   20.24 @@ -44,8 +43,6 @@
   20.25  val zdiff_int_split = @{thm "zdiff_int_split"};
   20.26  val all_nat = @{thm "all_nat"};
   20.27  val ex_nat = @{thm "ex_nat"};
   20.28 -val number_of1 = @{thm "number_of1"};
   20.29 -val number_of2 = @{thm "number_of2"};
   20.30  val split_zdiv = @{thm "split_zdiv"};
   20.31  val split_zmod = @{thm "split_zmod"};
   20.32  val mod_div_equality' = @{thm "mod_div_equality'"};
   20.33 @@ -113,15 +110,15 @@
   20.34              @{thm "split_min"}, @{thm "split_max"}]
   20.35      (* Simp rules for changing (n::int) to int n *)
   20.36      val simpset1 = HOL_basic_ss
   20.37 -      addsimps [@{thm "nat_number_of_def"}, @{thm "zdvd_int"}] @ map (fn r => r RS sym)
   20.38 +      addsimps [@{thm "zdvd_int"}] @ map (fn r => r RS sym)
   20.39          [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
   20.40 -         @{thm "zmult_int"}]
   20.41 +         @{thm nat_numeral}, @{thm "zmult_int"}]
   20.42        |> Splitter.add_split @{thm "zdiff_int_split"}
   20.43      (*simp rules for elimination of int n*)
   20.44  
   20.45      val simpset2 = HOL_basic_ss
   20.46 -      addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "number_of1"}, 
   20.47 -                @{thm "number_of2"}, @{thm "int_0"}, @{thm "int_1"}]
   20.48 +      addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm zero_le_numeral}, 
   20.49 +                @{thm "int_0"}, @{thm "int_1"}]
   20.50        |> fold Simplifier.add_cong [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
   20.51      (* simp rules for elimination of abs *)
   20.52      val ct = cterm_of thy (HOLogic.mk_Trueprop t)
    21.1 --- a/src/HOL/Deriv.thy	Fri Mar 23 20:32:43 2012 +0100
    21.2 +++ b/src/HOL/Deriv.thy	Mon Mar 26 10:56:56 2012 +0200
    21.3 @@ -186,7 +186,6 @@
    21.4  apply (erule DERIV_mult')
    21.5  apply (erule (1) DERIV_inverse')
    21.6  apply (simp add: ring_distribs nonzero_inverse_mult_distrib)
    21.7 -apply (simp add: mult_ac)
    21.8  done
    21.9  
   21.10  lemma DERIV_power_Suc:
    22.1 --- a/src/HOL/Divides.thy	Fri Mar 23 20:32:43 2012 +0100
    22.2 +++ b/src/HOL/Divides.thy	Mon Mar 26 10:56:56 2012 +0200
    22.3 @@ -1138,8 +1138,8 @@
    22.4  lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
    22.5  by (simp add: Suc3_eq_add_3)
    22.6  
    22.7 -lemmas Suc_div_eq_add3_div_number_of [simp] = Suc_div_eq_add3_div [of _ "number_of v"] for v
    22.8 -lemmas Suc_mod_eq_add3_mod_number_of [simp] = Suc_mod_eq_add3_mod [of _ "number_of v"] for v
    22.9 +lemmas Suc_div_eq_add3_div_numeral [simp] = Suc_div_eq_add3_div [of _ "numeral v"] for v
   22.10 +lemmas Suc_mod_eq_add3_mod_numeral [simp] = Suc_mod_eq_add3_mod [of _ "numeral v"] for v
   22.11  
   22.12  
   22.13  lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1" 
   22.14 @@ -1147,7 +1147,7 @@
   22.15  apply (simp_all add: mod_Suc)
   22.16  done
   22.17  
   22.18 -declare Suc_times_mod_eq [of "number_of w", simp] for w
   22.19 +declare Suc_times_mod_eq [of "numeral w", simp] for w
   22.20  
   22.21  lemma [simp]: "n div k \<le> (Suc n) div k"
   22.22  by (simp add: div_le_mono) 
   22.23 @@ -1177,17 +1177,22 @@
   22.24  apply (subst mod_Suc [of "m mod n"], simp) 
   22.25  done
   22.26  
   22.27 +lemma mod_2_not_eq_zero_eq_one_nat:
   22.28 +  fixes n :: nat
   22.29 +  shows "n mod 2 \<noteq> 0 \<longleftrightarrow> n mod 2 = 1"
   22.30 +  by simp
   22.31 +
   22.32  
   22.33  subsection {* Division on @{typ int} *}
   22.34  
   22.35  definition divmod_int_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
   22.36      --{*definition of quotient and remainder*}
   22.37 -    [code]: "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
   22.38 +    "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
   22.39                 (if 0 < b then 0 \<le> r \<and> r < b else b < r \<and> r \<le> 0))"
   22.40  
   22.41  definition adjust :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" where
   22.42      --{*for the division algorithm*}
   22.43 -    [code]: "adjust b = (\<lambda>(q, r). if 0 \<le> r - b then (2 * q + 1, r - b)
   22.44 +    "adjust b = (\<lambda>(q, r). if 0 \<le> r - b then (2 * q + 1, r - b)
   22.45                           else (2 * q, r))"
   22.46  
   22.47  text{*algorithm for the case @{text "a\<ge>0, b>0"}*}
   22.48 @@ -1318,11 +1323,11 @@
   22.49  text{*And positive divisors*}
   22.50  
   22.51  lemma adjust_eq [simp]:
   22.52 -     "adjust b (q,r) = 
   22.53 -      (let diff = r-b in  
   22.54 -        if 0 \<le> diff then (2*q + 1, diff)   
   22.55 +     "adjust b (q, r) = 
   22.56 +      (let diff = r - b in  
   22.57 +        if 0 \<le> diff then (2 * q + 1, diff)   
   22.58                       else (2*q, r))"
   22.59 -by (simp add: Let_def adjust_def)
   22.60 +  by (simp add: Let_def adjust_def)
   22.61  
   22.62  declare posDivAlg.simps [simp del]
   22.63  
   22.64 @@ -1420,6 +1425,9 @@
   22.65  
   22.66  text {* Tool setup *}
   22.67  
   22.68 +(* FIXME: Theorem list add_0s doesn't exist, because Numeral0 has gone. *)
   22.69 +lemmas add_0s = add_0_left add_0_right
   22.70 +
   22.71  ML {*
   22.72  structure Cancel_Div_Mod_Int = Cancel_Div_Mod
   22.73  (
   22.74 @@ -1674,16 +1682,6 @@
   22.75    by (rule divmod_int_rel_mod [of a b q r],
   22.76      simp add: divmod_int_rel_def)
   22.77  
   22.78 -lemmas arithmetic_simps =
   22.79 -  arith_simps
   22.80 -  add_special
   22.81 -  add_0_left
   22.82 -  add_0_right
   22.83 -  mult_zero_left
   22.84 -  mult_zero_right
   22.85 -  mult_1_left
   22.86 -  mult_1_right
   22.87 -
   22.88  (* simprocs adapted from HOL/ex/Binary.thy *)
   22.89  ML {*
   22.90  local
   22.91 @@ -1694,7 +1692,7 @@
   22.92    val less = @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"}
   22.93    val le = @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"}
   22.94    val simps = @{thms arith_simps} @ @{thms rel_simps} @
   22.95 -    map (fn th => th RS sym) [@{thm numeral_0_eq_0}, @{thm numeral_1_eq_1}]
   22.96 +    map (fn th => th RS sym) [@{thm numeral_1_eq_1}]
   22.97    fun prove ctxt goal = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop goal)
   22.98      (K (ALLGOALS (full_simp_tac (HOL_basic_ss addsimps simps))));
   22.99    fun binary_proc proc ss ct =
  22.100 @@ -1717,14 +1715,25 @@
  22.101  end
  22.102  *}
  22.103  
  22.104 -simproc_setup binary_int_div ("number_of m div number_of n :: int") =
  22.105 +simproc_setup binary_int_div
  22.106 +  ("numeral m div numeral n :: int" |
  22.107 +   "numeral m div neg_numeral n :: int" |
  22.108 +   "neg_numeral m div numeral n :: int" |
  22.109 +   "neg_numeral m div neg_numeral n :: int") =
  22.110    {* K (divmod_proc @{thm int_div_pos_eq} @{thm int_div_neg_eq}) *}
  22.111  
  22.112 -simproc_setup binary_int_mod ("number_of m mod number_of n :: int") =
  22.113 +simproc_setup binary_int_mod
  22.114 +  ("numeral m mod numeral n :: int" |
  22.115 +   "numeral m mod neg_numeral n :: int" |
  22.116 +   "neg_numeral m mod numeral n :: int" |
  22.117 +   "neg_numeral m mod neg_numeral n :: int") =
  22.118    {* K (divmod_proc @{thm int_mod_pos_eq} @{thm int_mod_neg_eq}) *}
  22.119  
  22.120 -lemmas posDivAlg_eqn_number_of [simp] = posDivAlg_eqn [of "number_of v" "number_of w"] for v w
  22.121 -lemmas negDivAlg_eqn_number_of [simp] = negDivAlg_eqn [of "number_of v" "number_of w"] for v w
  22.122 +lemmas posDivAlg_eqn_numeral [simp] =
  22.123 +    posDivAlg_eqn [of "numeral v" "numeral w", OF zero_less_numeral] for v w
  22.124 +
  22.125 +lemmas negDivAlg_eqn_numeral [simp] =
  22.126 +    negDivAlg_eqn [of "numeral v" "neg_numeral w", OF zero_less_numeral] for v w
  22.127  
  22.128  
  22.129  text{*Special-case simplification *}
  22.130 @@ -1741,12 +1750,25 @@
  22.131  (** The last remaining special cases for constant arithmetic:
  22.132      1 div z and 1 mod z **)
  22.133  
  22.134 -lemmas div_pos_pos_1_number_of [simp] = div_pos_pos [OF zero_less_one, of "number_of w"] for w
  22.135 -lemmas div_pos_neg_1_number_of [simp] = div_pos_neg [OF zero_less_one, of "number_of w"] for w
  22.136 -lemmas mod_pos_pos_1_number_of [simp] = mod_pos_pos [OF zero_less_one, of "number_of w"] for w
  22.137 -lemmas mod_pos_neg_1_number_of [simp] = mod_pos_neg [OF zero_less_one, of "number_of w"] for w
  22.138 -lemmas posDivAlg_eqn_1_number_of [simp] = posDivAlg_eqn [of concl: 1 "number_of w"] for w
  22.139 -lemmas negDivAlg_eqn_1_number_of [simp] = negDivAlg_eqn [of concl: 1 "number_of w"] for w
  22.140 +lemmas div_pos_pos_1_numeral [simp] =
  22.141 +  div_pos_pos [OF zero_less_one, of "numeral w", OF zero_le_numeral] for w
  22.142 +
  22.143 +lemmas div_pos_neg_1_numeral [simp] =
  22.144 +  div_pos_neg [OF zero_less_one, of "neg_numeral w",
  22.145 +  OF neg_numeral_less_zero] for w
  22.146 +
  22.147 +lemmas mod_pos_pos_1_numeral [simp] =
  22.148 +  mod_pos_pos [OF zero_less_one, of "numeral w", OF zero_le_numeral] for w
  22.149 +
  22.150 +lemmas mod_pos_neg_1_numeral [simp] =
  22.151 +  mod_pos_neg [OF zero_less_one, of "neg_numeral w",
  22.152 +  OF neg_numeral_less_zero] for w
  22.153 +
  22.154 +lemmas posDivAlg_eqn_1_numeral [simp] =
  22.155 +    posDivAlg_eqn [of concl: 1 "numeral w", OF zero_less_numeral] for w
  22.156 +
  22.157 +lemmas negDivAlg_eqn_1_numeral [simp] =
  22.158 +    negDivAlg_eqn [of concl: 1 "numeral w", OF zero_less_numeral] for w
  22.159  
  22.160  
  22.161  subsubsection {* Monotonicity in the First Argument (Dividend) *}
  22.162 @@ -1928,6 +1950,11 @@
  22.163  (* REVISIT: should this be generalized to all semiring_div types? *)
  22.164  lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
  22.165  
  22.166 +lemma zmod_zdiv_equality':
  22.167 +  "(m\<Colon>int) mod n = m - (m div n) * n"
  22.168 +  by (rule_tac P="%x. m mod n = x - (m div n) * n" in subst [OF mod_div_equality [of _ n]])
  22.169 +    arith
  22.170 +
  22.171  
  22.172  subsubsection {* Proving  @{term "a div (b*c) = (a div b) div c"} *}
  22.173  
  22.174 @@ -1989,6 +2016,26 @@
  22.175  apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN divmod_int_rel_mod])
  22.176  done
  22.177  
  22.178 +lemma div_pos_geq:
  22.179 +  fixes k l :: int
  22.180 +  assumes "0 < l" and "l \<le> k"
  22.181 +  shows "k div l = (k - l) div l + 1"
  22.182 +proof -
  22.183 +  have "k = (k - l) + l" by simp
  22.184 +  then obtain j where k: "k = j + l" ..
  22.185 +  with assms show ?thesis by simp
  22.186 +qed
  22.187 +
  22.188 +lemma mod_pos_geq:
  22.189 +  fixes k l :: int
  22.190 +  assumes "0 < l" and "l \<le> k"
  22.191 +  shows "k mod l = (k - l) mod l"
  22.192 +proof -
  22.193 +  have "k = (k - l) + l" by simp
  22.194 +  then obtain j where k: "k = j + l" ..
  22.195 +  with assms show ?thesis by simp
  22.196 +qed
  22.197 +
  22.198  
  22.199  subsubsection {* Splitting Rules for div and mod *}
  22.200  
  22.201 @@ -2046,9 +2093,9 @@
  22.202  
  22.203  text {* Enable (lin)arith to deal with @{const div} and @{const mod}
  22.204    when these are applied to some constant that is of the form
  22.205 -  @{term "number_of k"}: *}
  22.206 -declare split_zdiv [of _ _ "number_of k", arith_split] for k
  22.207 -declare split_zmod [of _ _ "number_of k", arith_split] for k
  22.208 +  @{term "numeral k"}: *}
  22.209 +declare split_zdiv [of _ _ "numeral k", arith_split] for k
  22.210 +declare split_zmod [of _ _ "numeral k", arith_split] for k
  22.211  
  22.212  
  22.213  subsubsection {* Speeding up the Division Algorithm with Shifting *}
  22.214 @@ -2090,19 +2137,19 @@
  22.215        minus_add_distrib [symmetric] mult_minus_right)
  22.216  qed
  22.217  
  22.218 -lemma zdiv_number_of_Bit0 [simp]:
  22.219 -     "number_of (Int.Bit0 v) div number_of (Int.Bit0 w) =  
  22.220 -          number_of v div (number_of w :: int)"
  22.221 -by (simp only: number_of_eq numeral_simps) (simp add: mult_2 [symmetric])
  22.222 -
  22.223 -lemma zdiv_number_of_Bit1 [simp]:
  22.224 -     "number_of (Int.Bit1 v) div number_of (Int.Bit0 w) =  
  22.225 -          (if (0::int) \<le> number_of w                    
  22.226 -           then number_of v div (number_of w)     
  22.227 -           else (number_of v + (1::int)) div (number_of w))"
  22.228 -apply (simp only: number_of_eq numeral_simps UNIV_I split: split_if) 
  22.229 -apply (simp add: pos_zdiv_mult_2 neg_zdiv_mult_2 add_ac mult_2 [symmetric])
  22.230 -done
  22.231 +(* FIXME: add rules for negative numerals *)
  22.232 +lemma zdiv_numeral_Bit0 [simp]:
  22.233 +  "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) =
  22.234 +    numeral v div (numeral w :: int)"
  22.235 +  unfolding numeral.simps unfolding mult_2 [symmetric]
  22.236 +  by (rule div_mult_mult1, simp)
  22.237 +
  22.238 +lemma zdiv_numeral_Bit1 [simp]:
  22.239 +  "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) =  
  22.240 +    (numeral v div (numeral w :: int))"
  22.241 +  unfolding numeral.simps
  22.242 +  unfolding mult_2 [symmetric] add_commute [of _ 1]
  22.243 +  by (rule pos_zdiv_mult_2, simp)
  22.244  
  22.245  
  22.246  subsubsection {* Computing mod by Shifting (proofs resemble those for div) *}
  22.247 @@ -2138,24 +2185,19 @@
  22.248       (simp add: diff_minus add_ac)
  22.249  qed
  22.250  
  22.251 -lemma zmod_number_of_Bit0 [simp]:
  22.252 -     "number_of (Int.Bit0 v) mod number_of (Int.Bit0 w) =  
  22.253 -      (2::int) * (number_of v mod number_of w)"
  22.254 -apply (simp only: number_of_eq numeral_simps) 
  22.255 -apply (simp add: mod_mult_mult1 pos_zmod_mult_2 
  22.256 -                 neg_zmod_mult_2 add_ac mult_2 [symmetric])
  22.257 -done
  22.258 -
  22.259 -lemma zmod_number_of_Bit1 [simp]:
  22.260 -     "number_of (Int.Bit1 v) mod number_of (Int.Bit0 w) =  
  22.261 -      (if (0::int) \<le> number_of w  
  22.262 -                then 2 * (number_of v mod number_of w) + 1     
  22.263 -                else 2 * ((number_of v + (1::int)) mod number_of w) - 1)"
  22.264 -apply (simp only: number_of_eq numeral_simps) 
  22.265 -apply (simp add: mod_mult_mult1 pos_zmod_mult_2 
  22.266 -                 neg_zmod_mult_2 add_ac mult_2 [symmetric])
  22.267 -done
  22.268 -
  22.269 +(* FIXME: add rules for negative numerals *)
  22.270 +lemma zmod_numeral_Bit0 [simp]:
  22.271 +  "numeral (Num.Bit0 v) mod numeral (Num.Bit0 w) =  
  22.272 +    (2::int) * (numeral v mod numeral w)"
  22.273 +  unfolding numeral_Bit0 [of v] numeral_Bit0 [of w]
  22.274 +  unfolding mult_2 [symmetric] by (rule mod_mult_mult1)
  22.275 +
  22.276 +lemma zmod_numeral_Bit1 [simp]:
  22.277 +  "numeral (Num.Bit1 v) mod numeral (Num.Bit0 w) =
  22.278 +    2 * (numeral v mod numeral w) + (1::int)"
  22.279 +  unfolding numeral_Bit1 [of v] numeral_Bit0 [of w]
  22.280 +  unfolding mult_2 [symmetric] add_commute [of _ 1]
  22.281 +  by (rule pos_zmod_mult_2, simp)
  22.282  
  22.283  lemma zdiv_eq_0_iff:
  22.284   "(i::int) div k = 0 \<longleftrightarrow> k=0 \<or> 0\<le>i \<and> i<k \<or> i\<le>0 \<and> k<i" (is "?L = ?R")
  22.285 @@ -2233,8 +2275,11 @@
  22.286  
  22.287  subsubsection {* The Divides Relation *}
  22.288  
  22.289 -lemmas zdvd_iff_zmod_eq_0_number_of [simp] =
  22.290 -  dvd_eq_mod_eq_0 [of "number_of x" "number_of y"] for x y :: int
  22.291 +lemmas zdvd_iff_zmod_eq_0_numeral [simp] =
  22.292 +  dvd_eq_mod_eq_0 [of "numeral x::int" "numeral y::int"]
  22.293 +  dvd_eq_mod_eq_0 [of "numeral x::int" "neg_numeral y::int"]
  22.294 +  dvd_eq_mod_eq_0 [of "neg_numeral x::int" "numeral y::int"]
  22.295 +  dvd_eq_mod_eq_0 [of "neg_numeral x::int" "neg_numeral y::int"] for x y
  22.296  
  22.297  lemma zdvd_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"
  22.298    by (rule dvd_mod) (* TODO: remove *)
  22.299 @@ -2242,6 +2287,12 @@
  22.300  lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"
  22.301    by (rule dvd_mod_imp_dvd) (* TODO: remove *)
  22.302  
  22.303 +lemmas dvd_eq_mod_eq_0_numeral [simp] =
  22.304 +  dvd_eq_mod_eq_0 [of "numeral x" "numeral y"] for x y
  22.305 +
  22.306 +
  22.307 +subsubsection {* Further properties *}
  22.308 +
  22.309  lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)"
  22.310    using zmod_zdiv_equality[where a="m" and b="n"]
  22.311    by (simp add: algebra_simps)
  22.312 @@ -2408,42 +2459,31 @@
  22.313    thus  ?lhs by simp
  22.314  qed
  22.315  
  22.316 -lemma div_nat_number_of [simp]:
  22.317 -     "(number_of v :: nat)  div  number_of v' =  
  22.318 -          (if neg (number_of v :: int) then 0  
  22.319 -           else nat (number_of v div number_of v'))"
  22.320 -  unfolding nat_number_of_def number_of_is_id neg_def
  22.321 +lemma div_nat_numeral [simp]:
  22.322 +  "(numeral v :: nat) div numeral v' = nat (numeral v div numeral v')"
  22.323    by (simp add: nat_div_distrib)
  22.324  
  22.325 -lemma one_div_nat_number_of [simp]:
  22.326 -     "Suc 0 div number_of v' = nat (1 div number_of v')" 
  22.327 -  by (simp del: semiring_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric] semiring_numeral_1_eq_1 [symmetric]) 
  22.328 -
  22.329 -lemma mod_nat_number_of [simp]:
  22.330 -     "(number_of v :: nat)  mod  number_of v' =  
  22.331 -        (if neg (number_of v :: int) then 0  
  22.332 -         else if neg (number_of v' :: int) then number_of v  
  22.333 -         else nat (number_of v mod number_of v'))"
  22.334 -  unfolding nat_number_of_def number_of_is_id neg_def
  22.335 +lemma one_div_nat_numeral [simp]:
  22.336 +  "Suc 0 div numeral v' = nat (1 div numeral v')"
  22.337 +  by (subst nat_div_distrib, simp_all)
  22.338 +
  22.339 +lemma mod_nat_numeral [simp]:
  22.340 +  "(numeral v :: nat) mod numeral v' = nat (numeral v mod numeral v')"
  22.341    by (simp add: nat_mod_distrib)
  22.342  
  22.343 -lemma one_mod_nat_number_of [simp]:
  22.344 -     "Suc 0 mod number_of v' =  
  22.345 -        (if neg (number_of v' :: int) then Suc 0
  22.346 -         else nat (1 mod number_of v'))"
  22.347 -by (simp del: semiring_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric] semiring_numeral_1_eq_1 [symmetric]) 
  22.348 -
  22.349 -lemmas dvd_eq_mod_eq_0_number_of [simp] =
  22.350 -  dvd_eq_mod_eq_0 [of "number_of x" "number_of y"] for x y
  22.351 -
  22.352 -
  22.353 -subsubsection {* Nitpick *}
  22.354 -
  22.355 -lemma zmod_zdiv_equality':
  22.356 -"(m\<Colon>int) mod n = m - (m div n) * n"
  22.357 -by (rule_tac P="%x. m mod n = x - (m div n) * n"
  22.358 -    in subst [OF mod_div_equality [of _ n]])
  22.359 -   arith
  22.360 +lemma one_mod_nat_numeral [simp]:
  22.361 +  "Suc 0 mod numeral v' = nat (1 mod numeral v')"
  22.362 +  by (subst nat_mod_distrib) simp_all
  22.363 +
  22.364 +lemma mod_2_not_eq_zero_eq_one_int:
  22.365 +  fixes k :: int
  22.366 +  shows "k mod 2 \<noteq> 0 \<longleftrightarrow> k mod 2 = 1"
  22.367 +  by auto
  22.368 +
  22.369 +
  22.370 +subsubsection {* Tools setup *}
  22.371 +
  22.372 +text {* Nitpick *}
  22.373  
  22.374  lemmas [nitpick_unfold] = dvd_eq_mod_eq_0 mod_div_equality' zmod_zdiv_equality'
  22.375  
  22.376 @@ -2461,7 +2501,7 @@
  22.377    apsnd ((op *) (sgn l)) (if 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0
  22.378      then pdivmod k l
  22.379      else (let (r, s) = pdivmod k l in
  22.380 -      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  22.381 +       if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  22.382  proof -
  22.383    have aux: "\<And>q::int. - k = l * q \<longleftrightarrow> k = l * - q" by auto
  22.384    show ?thesis
  22.385 @@ -2481,45 +2521,6 @@
  22.386    then show ?thesis by (simp add: divmod_int_pdivmod)
  22.387  qed
  22.388  
  22.389 -context ring_1
  22.390 -begin
  22.391 -
  22.392 -lemma of_int_num [code]:
  22.393 -  "of_int k = (if k = 0 then 0 else if k < 0 then
  22.394 -     - of_int (- k) else let
  22.395 -       (l, m) = divmod_int k 2;
  22.396 -       l' = of_int l
  22.397 -     in if m = 0 then l' + l' else l' + l' + 1)"
  22.398 -proof -
  22.399 -  have aux1: "k mod (2\<Colon>int) \<noteq> (0\<Colon>int) \<Longrightarrow> 
  22.400 -    of_int k = of_int (k div 2 * 2 + 1)"
  22.401 -  proof -
  22.402 -    have "k mod 2 < 2" by (auto intro: pos_mod_bound)
  22.403 -    moreover have "0 \<le> k mod 2" by (auto intro: pos_mod_sign)
  22.404 -    moreover assume "k mod 2 \<noteq> 0"
  22.405 -    ultimately have "k mod 2 = 1" by arith
  22.406 -    moreover have "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
  22.407 -    ultimately show ?thesis by auto
  22.408 -  qed
  22.409 -  have aux2: "\<And>x. of_int 2 * x = x + x"
  22.410 -  proof -
  22.411 -    fix x
  22.412 -    have int2: "(2::int) = 1 + 1" by arith
  22.413 -    show "of_int 2 * x = x + x"
  22.414 -    unfolding int2 of_int_add left_distrib by simp
  22.415 -  qed
  22.416 -  have aux3: "\<And>x. x * of_int 2 = x + x"
  22.417 -  proof -
  22.418 -    fix x
  22.419 -    have int2: "(2::int) = 1 + 1" by arith
  22.420 -    show "x * of_int 2 = x + x" 
  22.421 -    unfolding int2 of_int_add right_distrib by simp
  22.422 -  qed
  22.423 -  from aux1 show ?thesis by (auto simp add: divmod_int_mod_div Let_def aux2 aux3)
  22.424 -qed
  22.425 -
  22.426 -end
  22.427 -
  22.428  code_modulename SML
  22.429    Divides Arith
  22.430  
    23.1 --- a/src/HOL/Imperative_HOL/ex/Imperative_Quicksort.thy	Fri Mar 23 20:32:43 2012 +0100
    23.2 +++ b/src/HOL/Imperative_HOL/ex/Imperative_Quicksort.thy	Mon Mar 26 10:56:56 2012 +0200
    23.3 @@ -6,7 +6,7 @@
    23.4  
    23.5  theory Imperative_Quicksort
    23.6  imports
    23.7 -  Imperative_HOL
    23.8 +  "~~/src/HOL/Imperative_HOL/Imperative_HOL"
    23.9    Subarray
   23.10    "~~/src/HOL/Library/Multiset"
   23.11    "~~/src/HOL/Library/Efficient_Nat"
   23.12 @@ -593,8 +593,8 @@
   23.13  proof (induct a l r p arbitrary: h rule: part1.induct)
   23.14    case (1 a l r p)
   23.15    thus ?case unfolding part1.simps [of a l r]
   23.16 -  apply (auto intro!: success_intros del: success_ifI simp add: not_le)
   23.17 -  apply (auto intro!: effect_intros effect_swapI)
   23.18 +  apply (auto intro!: success_intros simp add: not_le)
   23.19 +  apply (auto intro!: effect_intros)
   23.20    done
   23.21  qed
   23.22  
    24.1 --- a/src/HOL/Imperative_HOL/ex/Imperative_Reverse.thy	Fri Mar 23 20:32:43 2012 +0100
    24.2 +++ b/src/HOL/Imperative_HOL/ex/Imperative_Reverse.thy	Mon Mar 26 10:56:56 2012 +0200
    24.3 @@ -5,7 +5,7 @@
    24.4  header {* An imperative in-place reversal on arrays *}
    24.5  
    24.6  theory Imperative_Reverse
    24.7 -imports Subarray Imperative_HOL
    24.8 +imports Subarray "~~/src/HOL/Imperative_HOL/Imperative_HOL"
    24.9  begin
   24.10  
   24.11  fun swap :: "'a\<Colon>heap array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> unit Heap" where
   24.12 @@ -107,7 +107,7 @@
   24.13    shows "Array.get h' a = List.rev (Array.get h a)"
   24.14    using rev2_rev'[OF assms] rev_length[OF assms] assms
   24.15      by (cases "Array.length h a = 0", auto simp add: Array.length_def
   24.16 -      subarray_def sublist'_all rev.simps[where j=0] elim!: effect_elims)
   24.17 +      subarray_def rev.simps[where j=0] elim!: effect_elims)
   24.18    (drule sym[of "List.length (Array.get h a)"], simp)
   24.19  
   24.20  definition "example = (Array.make 10 id \<guillemotright>= (\<lambda>a. rev a 0 9))"
   24.21 @@ -115,3 +115,4 @@
   24.22  export_code example checking SML SML_imp OCaml? OCaml_imp? Haskell? Scala? Scala_imp?
   24.23  
   24.24  end
   24.25 +
    25.1 --- a/src/HOL/Imperative_HOL/ex/SatChecker.thy	Fri Mar 23 20:32:43 2012 +0100
    25.2 +++ b/src/HOL/Imperative_HOL/ex/SatChecker.thy	Mon Mar 26 10:56:56 2012 +0200
    25.3 @@ -702,15 +702,7 @@
    25.4                  else raise(''No empty clause''))
    25.5    }"
    25.6  
    25.7 -section {* Code generation setup *}
    25.8 -
    25.9 -code_type ProofStep
   25.10 -  (SML "MinisatProofStep.ProofStep")
   25.11 -
   25.12 -code_const ProofDone and Root and Conflict and Delete and Xstep
   25.13 -  (SML "MinisatProofStep.ProofDone" and "MinisatProofStep.Root ((_),/ (_))" and "MinisatProofStep.Conflict ((_),/ (_))" and "MinisatProofStep.Delete" and "MinisatProofStep.Xstep ((_),/ (_))")
   25.14 -
   25.15 -export_code checker tchecker lchecker in SML
   25.16 +export_code checker tchecker lchecker checking SML
   25.17  
   25.18  end
   25.19  
    26.1 --- a/src/HOL/Imperative_HOL/ex/Subarray.thy	Fri Mar 23 20:32:43 2012 +0100
    26.2 +++ b/src/HOL/Imperative_HOL/ex/Subarray.thy	Mon Mar 26 10:56:56 2012 +0200
    26.3 @@ -5,7 +5,7 @@
    26.4  header {* Theorems about sub arrays *}
    26.5  
    26.6  theory Subarray
    26.7 -imports Array Sublist
    26.8 +imports "~~/src/HOL/Imperative_HOL/Array" Sublist
    26.9  begin
   26.10  
   26.11  definition subarray :: "nat \<Rightarrow> nat \<Rightarrow> ('a::heap) array \<Rightarrow> heap \<Rightarrow> 'a list" where
    27.1 --- a/src/HOL/Import/HOL_Light/HOLLightInt.thy	Fri Mar 23 20:32:43 2012 +0100
    27.2 +++ b/src/HOL/Import/HOL_Light/HOLLightInt.thy	Mon Mar 26 10:56:56 2012 +0200
    27.3 @@ -40,7 +40,7 @@
    27.4  
    27.5  lemma DEF_int_mul:
    27.6    "op * = (\<lambda>u ua. floor (real u * real ua))"
    27.7 -  by (metis floor_number_of number_of_is_id number_of_real_def real_eq_of_int real_of_int_mult)
    27.8 +  by (metis floor_real_of_int real_of_int_mult)
    27.9  
   27.10  lemma DEF_int_abs:
   27.11    "abs = (\<lambda>u. floor (abs (real u)))"
   27.12 @@ -72,7 +72,7 @@
   27.13  
   27.14  lemma INT_IMAGE:
   27.15    "(\<exists>n. x = int n) \<or> (\<exists>n. x = - int n)"
   27.16 -  by (metis number_of_eq number_of_is_id of_int_of_nat)
   27.17 +  by (metis of_int_eq_id id_def of_int_of_nat)
   27.18  
   27.19  lemma DEF_int_pow:
   27.20    "op ^ = (\<lambda>u ua. floor (real u ^ ua))"
    28.1 --- a/src/HOL/Int.thy	Fri Mar 23 20:32:43 2012 +0100
    28.2 +++ b/src/HOL/Int.thy	Mon Mar 26 10:56:56 2012 +0200
    28.3 @@ -6,10 +6,9 @@
    28.4  header {* The Integers as Equivalence Classes over Pairs of Natural Numbers *} 
    28.5  
    28.6  theory Int
    28.7 -imports Equiv_Relations Nat Wellfounded
    28.8 +imports Equiv_Relations Wellfounded
    28.9  uses
   28.10    ("Tools/numeral.ML")
   28.11 -  ("Tools/numeral_syntax.ML")
   28.12    ("Tools/int_arith.ML")
   28.13  begin
   28.14  
   28.15 @@ -323,15 +322,20 @@
   28.16  lemma of_int_of_nat_eq [simp]: "of_int (int n) = of_nat n"
   28.17  by (induct n) auto
   28.18  
   28.19 +lemma of_int_numeral [simp, code_post]: "of_int (numeral k) = numeral k"
   28.20 +  by (simp add: of_nat_numeral [symmetric] of_int_of_nat_eq [symmetric])
   28.21 +
   28.22 +lemma of_int_neg_numeral [simp, code_post]: "of_int (neg_numeral k) = neg_numeral k"
   28.23 +  unfolding neg_numeral_def neg_numeral_class.neg_numeral_def
   28.24 +  by (simp only: of_int_minus of_int_numeral)
   28.25 +
   28.26  lemma of_int_power:
   28.27    "of_int (z ^ n) = of_int z ^ n"
   28.28    by (induct n) simp_all
   28.29  
   28.30  end
   28.31  
   28.32 -text{*Class for unital rings with characteristic zero.
   28.33 - Includes non-ordered rings like the complex numbers.*}
   28.34 -class ring_char_0 = ring_1 + semiring_char_0
   28.35 +context ring_char_0
   28.36  begin
   28.37  
   28.38  lemma of_int_eq_iff [simp]:
   28.39 @@ -579,230 +583,27 @@
   28.40  apply (simp add: int_def minus add diff_minus)
   28.41  done
   28.42  
   28.43 -
   28.44 -subsection {* Binary representation *}
   28.45 -
   28.46 -text {*
   28.47 -  This formalization defines binary arithmetic in terms of the integers
   28.48 -  rather than using a datatype. This avoids multiple representations (leading
   28.49 -  zeroes, etc.)  See @{text "ZF/Tools/twos-compl.ML"}, function @{text
   28.50 -  int_of_binary}, for the numerical interpretation.
   28.51 -
   28.52 -  The representation expects that @{text "(m mod 2)"} is 0 or 1,
   28.53 -  even if m is negative;
   28.54 -  For instance, @{text "-5 div 2 = -3"} and @{text "-5 mod 2 = 1"}; thus
   28.55 -  @{text "-5 = (-3)*2 + 1"}.
   28.56 -  
   28.57 -  This two's complement binary representation derives from the paper 
   28.58 -  "An Efficient Representation of Arithmetic for Term Rewriting" by
   28.59 -  Dave Cohen and Phil Watson, Rewriting Techniques and Applications,
   28.60 -  Springer LNCS 488 (240-251), 1991.
   28.61 -*}
   28.62 -
   28.63 -subsubsection {* The constructors @{term Bit0}, @{term Bit1}, @{term Pls} and @{term Min} *}
   28.64 -
   28.65 -definition Pls :: int where
   28.66 -  "Pls = 0"
   28.67 -
   28.68 -definition Min :: int where
   28.69 -  "Min = - 1"
   28.70 -
   28.71 -definition Bit0 :: "int \<Rightarrow> int" where
   28.72 -  "Bit0 k = k + k"
   28.73 -
   28.74 -definition Bit1 :: "int \<Rightarrow> int" where
   28.75 -  "Bit1 k = 1 + k + k"
   28.76 -
   28.77 -class number = -- {* for numeric types: nat, int, real, \dots *}
   28.78 -  fixes number_of :: "int \<Rightarrow> 'a"
   28.79 -
   28.80 -use "Tools/numeral.ML"
   28.81 -
   28.82 -syntax
   28.83 -  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
   28.84 -
   28.85 -use "Tools/numeral_syntax.ML"
   28.86 -setup Numeral_Syntax.setup
   28.87 -
   28.88 -abbreviation
   28.89 -  "Numeral0 \<equiv> number_of Pls"
   28.90 -
   28.91 -abbreviation
   28.92 -  "Numeral1 \<equiv> number_of (Bit1 Pls)"
   28.93 -
   28.94 -lemma Let_number_of [simp]: "Let (number_of v) f = f (number_of v)"
   28.95 +lemma Let_numeral [simp]: "Let (numeral v) f = f (numeral v)"
   28.96    -- {* Unfold all @{text let}s involving constants *}
   28.97    unfolding Let_def ..
   28.98  
   28.99 -definition succ :: "int \<Rightarrow> int" where
  28.100 -  "succ k = k + 1"
  28.101 +lemma Let_neg_numeral [simp]: "Let (neg_numeral v) f = f (neg_numeral v)"
  28.102 +  -- {* Unfold all @{text let}s involving constants *}
  28.103 +  unfolding Let_def ..
  28.104  
  28.105 -definition pred :: "int \<Rightarrow> int" where
  28.106 -  "pred k = k - 1"
  28.107 +text {* Unfold @{text min} and @{text max} on numerals. *}
  28.108  
  28.109 -lemmas max_number_of [simp] = max_def [of "number_of u" "number_of v"]
  28.110 -  and min_number_of [simp] = min_def [of "number_of u" "number_of v"]
  28.111 -  for u v
  28.112 -  -- {* unfolding @{text minx} and @{text max} on numerals *}
  28.113 +lemmas max_number_of [simp] =
  28.114 +  max_def [of "numeral u" "numeral v"]
  28.115 +  max_def [of "numeral u" "neg_numeral v"]
  28.116 +  max_def [of "neg_numeral u" "numeral v"]
  28.117 +  max_def [of "neg_numeral u" "neg_numeral v"] for u v
  28.118  
  28.119 -lemmas numeral_simps = 
  28.120 -  succ_def pred_def Pls_def Min_def Bit0_def Bit1_def
  28.121 -
  28.122 -text {* Removal of leading zeroes *}
  28.123 -
  28.124 -lemma Bit0_Pls [simp, code_post]:
  28.125 -  "Bit0 Pls = Pls"
  28.126 -  unfolding numeral_simps by simp
  28.127 -
  28.128 -lemma Bit1_Min [simp, code_post]:
  28.129 -  "Bit1 Min = Min"
  28.130 -  unfolding numeral_simps by simp
  28.131 -
  28.132 -lemmas normalize_bin_simps =
  28.133 -  Bit0_Pls Bit1_Min
  28.134 -
  28.135 -
  28.136 -subsubsection {* Successor and predecessor functions *}
  28.137 -
  28.138 -text {* Successor *}
  28.139 -
  28.140 -lemma succ_Pls:
  28.141 -  "succ Pls = Bit1 Pls"
  28.142 -  unfolding numeral_simps by simp
  28.143 -
  28.144 -lemma succ_Min:
  28.145 -  "succ Min = Pls"
  28.146 -  unfolding numeral_simps by simp
  28.147 -
  28.148 -lemma succ_Bit0:
  28.149 -  "succ (Bit0 k) = Bit1 k"
  28.150 -  unfolding numeral_simps by simp
  28.151 -
  28.152 -lemma succ_Bit1:
  28.153 -  "succ (Bit1 k) = Bit0 (succ k)"
  28.154 -  unfolding numeral_simps by simp
  28.155 -
  28.156 -lemmas succ_bin_simps [simp] =
  28.157 -  succ_Pls succ_Min succ_Bit0 succ_Bit1
  28.158 -
  28.159 -text {* Predecessor *}
  28.160 -
  28.161 -lemma pred_Pls:
  28.162 -  "pred Pls = Min"
  28.163 -  unfolding numeral_simps by simp
  28.164 -
  28.165 -lemma pred_Min:
  28.166 -  "pred Min = Bit0 Min"
  28.167 -  unfolding numeral_simps by simp
  28.168 -
  28.169 -lemma pred_Bit0:
  28.170 -  "pred (Bit0 k) = Bit1 (pred k)"
  28.171 -  unfolding numeral_simps by simp 
  28.172 -
  28.173 -lemma pred_Bit1:
  28.174 -  "pred (Bit1 k) = Bit0 k"
  28.175 -  unfolding numeral_simps by simp
  28.176 -
  28.177 -lemmas pred_bin_simps [simp] =
  28.178 -  pred_Pls pred_Min pred_Bit0 pred_Bit1
  28.179 -
  28.180 -
  28.181 -subsubsection {* Binary arithmetic *}
  28.182 -
  28.183 -text {* Addition *}
  28.184 -
  28.185 -lemma add_Pls:
  28.186 -  "Pls + k = k"
  28.187 -  unfolding numeral_simps by simp
  28.188 -
  28.189 -lemma add_Min:
  28.190 -  "Min + k = pred k"
  28.191 -  unfolding numeral_simps by simp
  28.192 -
  28.193 -lemma add_Bit0_Bit0:
  28.194 -  "(Bit0 k) + (Bit0 l) = Bit0 (k + l)"
  28.195 -  unfolding numeral_simps by simp
  28.196 -
  28.197 -lemma add_Bit0_Bit1:
  28.198 -  "(Bit0 k) + (Bit1 l) = Bit1 (k + l)"
  28.199 -  unfolding numeral_simps by simp
  28.200 -
  28.201 -lemma add_Bit1_Bit0:
  28.202 -  "(Bit1 k) + (Bit0 l) = Bit1 (k + l)"
  28.203 -  unfolding numeral_simps by simp
  28.204 -
  28.205 -lemma add_Bit1_Bit1:
  28.206 -  "(Bit1 k) + (Bit1 l) = Bit0 (k + succ l)"
  28.207 -  unfolding numeral_simps by simp
  28.208 -
  28.209 -lemma add_Pls_right:
  28.210 -  "k + Pls = k"
  28.211 -  unfolding numeral_simps by simp
  28.212 -
  28.213 -lemma add_Min_right:
  28.214 -  "k + Min = pred k"
  28.215 -  unfolding numeral_simps by simp
  28.216 -
  28.217 -lemmas add_bin_simps [simp] =
  28.218 -  add_Pls add_Min add_Pls_right add_Min_right
  28.219 -  add_Bit0_Bit0 add_Bit0_Bit1 add_Bit1_Bit0 add_Bit1_Bit1
  28.220 -
  28.221 -text {* Negation *}
  28.222 -
  28.223 -lemma minus_Pls:
  28.224 -  "- Pls = Pls"
  28.225 -  unfolding numeral_simps by simp
  28.226 -
  28.227 -lemma minus_Min:
  28.228 -  "- Min = Bit1 Pls"
  28.229 -  unfolding numeral_simps by simp
  28.230 -
  28.231 -lemma minus_Bit0:
  28.232 -  "- (Bit0 k) = Bit0 (- k)"
  28.233 -  unfolding numeral_simps by simp
  28.234 -
  28.235 -lemma minus_Bit1:
  28.236 -  "- (Bit1 k) = Bit1 (pred (- k))"
  28.237 -  unfolding numeral_simps by simp
  28.238 -
  28.239 -lemmas minus_bin_simps [simp] =
  28.240 -  minus_Pls minus_Min minus_Bit0 minus_Bit1
  28.241 -
  28.242 -text {* Subtraction *}
  28.243 -
  28.244 -lemma diff_bin_simps [simp]:
  28.245 -  "k - Pls = k"
  28.246 -  "k - Min = succ k"
  28.247 -  "Pls - (Bit0 l) = Bit0 (Pls - l)"
  28.248 -  "Pls - (Bit1 l) = Bit1 (Min - l)"
  28.249 -  "Min - (Bit0 l) = Bit1 (Min - l)"
  28.250 -  "Min - (Bit1 l) = Bit0 (Min - l)"
  28.251 -  "(Bit0 k) - (Bit0 l) = Bit0 (k - l)"
  28.252 -  "(Bit0 k) - (Bit1 l) = Bit1 (pred k - l)"
  28.253 -  "(Bit1 k) - (Bit0 l) = Bit1 (k - l)"
  28.254 -  "(Bit1 k) - (Bit1 l) = Bit0 (k - l)"
  28.255 -  unfolding numeral_simps by simp_all
  28.256 -
  28.257 -text {* Multiplication *}
  28.258 -
  28.259 -lemma mult_Pls:
  28.260 -  "Pls * w = Pls"
  28.261 -  unfolding numeral_simps by simp
  28.262 -
  28.263 -lemma mult_Min:
  28.264 -  "Min * k = - k"
  28.265 -  unfolding numeral_simps by simp
  28.266 -
  28.267 -lemma mult_Bit0:
  28.268 -  "(Bit0 k) * l = Bit0 (k * l)"
  28.269 -  unfolding numeral_simps int_distrib by simp
  28.270 -
  28.271 -lemma mult_Bit1:
  28.272 -  "(Bit1 k) * l = (Bit0 (k * l)) + l"
  28.273 -  unfolding numeral_simps int_distrib by simp
  28.274 -
  28.275 -lemmas mult_bin_simps [simp] =
  28.276 -  mult_Pls mult_Min mult_Bit0 mult_Bit1
  28.277 +lemmas min_number_of [simp] =
  28.278 +  min_def [of "numeral u" "numeral v"]
  28.279 +  min_def [of "numeral u" "neg_numeral v"]
  28.280 +  min_def [of "neg_numeral u" "numeral v"]
  28.281 +  min_def [of "neg_numeral u" "neg_numeral v"] for u v
  28.282  
  28.283  
  28.284  subsubsection {* Binary comparisons *}
  28.285 @@ -812,7 +613,7 @@
  28.286  lemma even_less_0_iff:
  28.287    "a + a < 0 \<longleftrightarrow> a < (0::'a::linordered_idom)"
  28.288  proof -
  28.289 -  have "a + a < 0 \<longleftrightarrow> (1+1)*a < 0" by (simp add: left_distrib)
  28.290 +  have "a + a < 0 \<longleftrightarrow> (1+1)*a < 0" by (simp add: left_distrib del: one_add_one)
  28.291    also have "(1+1)*a < 0 \<longleftrightarrow> a < 0"
  28.292      by (simp add: mult_less_0_iff zero_less_two 
  28.293                    order_less_not_sym [OF zero_less_two])
  28.294 @@ -824,7 +625,7 @@
  28.295    shows "(0::int) < 1 + z"
  28.296  proof -
  28.297    have "0 \<le> z" by fact
  28.298 -  also have "... < z + 1" by (rule less_add_one) 
  28.299 +  also have "... < z + 1" by (rule less_add_one)
  28.300    also have "... = 1 + z" by (simp add: add_ac)
  28.301    finally show "0 < 1 + z" .
  28.302  qed
  28.303 @@ -841,276 +642,6 @@
  28.304      add: algebra_simps of_nat_1 [where 'a=int, symmetric] of_nat_add [symmetric])
  28.305  qed
  28.306  
  28.307 -lemma bin_less_0_simps:
  28.308 -  "Pls < 0 \<longleftrightarrow> False"
  28.309 -  "Min < 0 \<longleftrightarrow> True"
  28.310 -  "Bit0 w < 0 \<longleftrightarrow> w < 0"
  28.311 -  "Bit1 w < 0 \<longleftrightarrow> w < 0"
  28.312 -  unfolding numeral_simps
  28.313 -  by (simp_all add: even_less_0_iff odd_less_0_iff)
  28.314 -
  28.315 -lemma less_bin_lemma: "k < l \<longleftrightarrow> k - l < (0::int)"
  28.316 -  by simp
  28.317 -
  28.318 -lemma le_iff_pred_less: "k \<le> l \<longleftrightarrow> pred k < l"
  28.319 -  unfolding numeral_simps
  28.320 -  proof
  28.321 -    have "k - 1 < k" by simp
  28.322 -    also assume "k \<le> l"
  28.323 -    finally show "k - 1 < l" .
  28.324 -  next
  28.325 -    assume "k - 1 < l"
  28.326 -    hence "(k - 1) + 1 \<le> l" by (rule zless_imp_add1_zle)
  28.327 -    thus "k \<le> l" by simp
  28.328 -  qed
  28.329 -
  28.330 -lemma succ_pred: "succ (pred x) = x"
  28.331 -  unfolding numeral_simps by simp
  28.332 -
  28.333 -text {* Less-than *}
  28.334 -
  28.335 -lemma less_bin_simps [simp]:
  28.336 -  "Pls < Pls \<longleftrightarrow> False"
  28.337 -  "Pls < Min \<longleftrightarrow> False"
  28.338 -  "Pls < Bit0 k \<longleftrightarrow> Pls < k"
  28.339 -  "Pls < Bit1 k \<longleftrightarrow> Pls \<le> k"
  28.340 -  "Min < Pls \<longleftrightarrow> True"
  28.341 -  "Min < Min \<longleftrightarrow> False"
  28.342 -  "Min < Bit0 k \<longleftrightarrow> Min < k"
  28.343 -  "Min < Bit1 k \<longleftrightarrow> Min < k"
  28.344 -  "Bit0 k < Pls \<longleftrightarrow> k < Pls"
  28.345 -  "Bit0 k < Min \<longleftrightarrow> k \<le> Min"
  28.346 -  "Bit1 k < Pls \<longleftrightarrow> k < Pls"
  28.347 -  "Bit1 k < Min \<longleftrightarrow> k < Min"
  28.348 -  "Bit0 k < Bit0 l \<longleftrightarrow> k < l"
  28.349 -  "Bit0 k < Bit1 l \<longleftrightarrow> k \<le> l"
  28.350 -  "Bit1 k < Bit0 l \<longleftrightarrow> k < l"
  28.351 -  "Bit1 k < Bit1 l \<longleftrightarrow> k < l"
  28.352 -  unfolding le_iff_pred_less
  28.353 -    less_bin_lemma [of Pls]
  28.354 -    less_bin_lemma [of Min]
  28.355 -    less_bin_lemma [of "k"]
  28.356 -    less_bin_lemma [of "Bit0 k"]
  28.357 -    less_bin_lemma [of "Bit1 k"]
  28.358 -    less_bin_lemma [of "pred Pls"]
  28.359 -    less_bin_lemma [of "pred k"]
  28.360 -  by (simp_all add: bin_less_0_simps succ_pred)
  28.361 -
  28.362 -text {* Less-than-or-equal *}
  28.363 -
  28.364 -lemma le_bin_simps [simp]:
  28.365 -  "Pls \<le> Pls \<longleftrightarrow> True"
  28.366 -  "Pls \<le> Min \<longleftrightarrow> False"
  28.367 -  "Pls \<le> Bit0 k \<longleftrightarrow> Pls \<le> k"
  28.368 -  "Pls \<le> Bit1 k \<longleftrightarrow> Pls \<le> k"
  28.369 -  "Min \<le> Pls \<longleftrightarrow> True"
  28.370 -  "Min \<le> Min \<longleftrightarrow> True"
  28.371 -  "Min \<le> Bit0 k \<longleftrightarrow> Min < k"
  28.372 -  "Min \<le> Bit1 k \<longleftrightarrow> Min \<le> k"
  28.373 -  "Bit0 k \<le> Pls \<longleftrightarrow> k \<le> Pls"
  28.374 -  "Bit0 k \<le> Min \<longleftrightarrow> k \<le> Min"
  28.375 -  "Bit1 k \<le> Pls \<longleftrightarrow> k < Pls"
  28.376 -  "Bit1 k \<le> Min \<longleftrightarrow> k \<le> Min"
  28.377 -  "Bit0 k \<le> Bit0 l \<longleftrightarrow> k \<le> l"
  28.378 -  "Bit0 k \<le> Bit1 l \<longleftrightarrow> k \<le> l"
  28.379 -  "Bit1 k \<le> Bit0 l \<longleftrightarrow> k < l"
  28.380 -  "Bit1 k \<le> Bit1 l \<longleftrightarrow> k \<le> l"
  28.381 -  unfolding not_less [symmetric]
  28.382 -  by (simp_all add: not_le)
  28.383 -
  28.384 -text {* Equality *}
  28.385 -
  28.386 -lemma eq_bin_simps [simp]:
  28.387 -  "Pls = Pls \<longleftrightarrow> True"
  28.388 -  "Pls = Min \<longleftrightarrow> False"
  28.389 -  "Pls = Bit0 l \<longleftrightarrow> Pls = l"
  28.390 -  "Pls = Bit1 l \<longleftrightarrow> False"
  28.391 -  "Min = Pls \<longleftrightarrow> False"
  28.392 -  "Min = Min \<longleftrightarrow> True"
  28.393 -  "Min = Bit0 l \<longleftrightarrow> False"
  28.394 -  "Min = Bit1 l \<longleftrightarrow> Min = l"
  28.395 -  "Bit0 k = Pls \<longleftrightarrow> k = Pls"
  28.396 -  "Bit0 k = Min \<longleftrightarrow> False"
  28.397 -  "Bit1 k = Pls \<longleftrightarrow> False"
  28.398 -  "Bit1 k = Min \<longleftrightarrow> k = Min"
  28.399 -  "Bit0 k = Bit0 l \<longleftrightarrow> k = l"
  28.400 -  "Bit0 k = Bit1 l \<longleftrightarrow> False"
  28.401 -  "Bit1 k = Bit0 l \<longleftrightarrow> False"
  28.402 -  "Bit1 k = Bit1 l \<longleftrightarrow> k = l"
  28.403 -  unfolding order_eq_iff [where 'a=int]
  28.404 -  by (simp_all add: not_less)
  28.405 -
  28.406 -
  28.407 -subsection {* Converting Numerals to Rings: @{term number_of} *}
  28.408 -
  28.409 -class number_ring = number + comm_ring_1 +
  28.410 -  assumes number_of_eq: "number_of k = of_int k"
  28.411 -
  28.412 -class number_semiring = number + comm_semiring_1 +
  28.413 -  assumes number_of_int: "number_of (int n) = of_nat n"
  28.414 -
  28.415 -instance number_ring \<subseteq> number_semiring
  28.416 -proof
  28.417 -  fix n show "number_of (int n) = (of_nat n :: 'a)"
  28.418 -    unfolding number_of_eq by (rule of_int_of_nat_eq)
  28.419 -qed
  28.420 -
  28.421 -text {* self-embedding of the integers *}
  28.422 -
  28.423 -instantiation int :: number_ring
  28.424 -begin
  28.425 -
  28.426 -definition
  28.427 -  int_number_of_def: "number_of w = (of_int w \<Colon> int)"
  28.428 -
  28.429 -instance proof
  28.430 -qed (simp only: int_number_of_def)
  28.431 -
  28.432 -end
  28.433 -
  28.434 -lemma number_of_is_id:
  28.435 -  "number_of (k::int) = k"
  28.436 -  unfolding int_number_of_def by simp
  28.437 -
  28.438 -lemma number_of_succ:
  28.439 -  "number_of (succ k) = (1 + number_of k ::'a::number_ring)"
  28.440 -  unfolding number_of_eq numeral_simps by simp
  28.441 -
  28.442 -lemma number_of_pred:
  28.443 -  "number_of (pred w) = (- 1 + number_of w ::'a::number_ring)"
  28.444 -  unfolding number_of_eq numeral_simps by simp
  28.445 -
  28.446 -lemma number_of_minus:
  28.447 -  "number_of (uminus w) = (- (number_of w)::'a::number_ring)"
  28.448 -  unfolding number_of_eq by (rule of_int_minus)
  28.449 -
  28.450 -lemma number_of_add:
  28.451 -  "number_of (v + w) = (number_of v + number_of w::'a::number_ring)"
  28.452 -  unfolding number_of_eq by (rule of_int_add)
  28.453 -
  28.454 -lemma number_of_diff:
  28.455 -  "number_of (v - w) = (number_of v - number_of w::'a::number_ring)"
  28.456 -  unfolding number_of_eq by (rule of_int_diff)
  28.457 -
  28.458 -lemma number_of_mult:
  28.459 -  "number_of (v * w) = (number_of v * number_of w::'a::number_ring)"
  28.460 -  unfolding number_of_eq by (rule of_int_mult)
  28.461 -
  28.462 -text {*
  28.463 -  The correctness of shifting.
  28.464 -  But it doesn't seem to give a measurable speed-up.
  28.465 -*}
  28.466 -
  28.467 -lemma double_number_of_Bit0:
  28.468 -  "(1 + 1) * number_of w = (number_of (Bit0 w) ::'a::number_ring)"
  28.469 -  unfolding number_of_eq numeral_simps left_distrib by simp
  28.470 -
  28.471 -text {*
  28.472 -  Converting numerals 0 and 1 to their abstract versions.
  28.473 -*}
  28.474 -
  28.475 -lemma semiring_numeral_0_eq_0 [simp, code_post]:
  28.476 -  "Numeral0 = (0::'a::number_semiring)"
  28.477 -  using number_of_int [where 'a='a and n=0]
  28.478 -  unfolding numeral_simps by simp
  28.479 -
  28.480 -lemma semiring_numeral_1_eq_1 [simp, code_post]:
  28.481 -  "Numeral1 = (1::'a::number_semiring)"
  28.482 -  using number_of_int [where 'a='a and n=1]
  28.483 -  unfolding numeral_simps by simp
  28.484 -
  28.485 -lemma numeral_0_eq_0: (* FIXME delete candidate *)
  28.486 -  "Numeral0 = (0::'a::number_ring)"
  28.487 -  by (rule semiring_numeral_0_eq_0)
  28.488 -
  28.489 -lemma numeral_1_eq_1: (* FIXME delete candidate *)
  28.490 -  "Numeral1 = (1::'a::number_ring)"
  28.491 -  by (rule semiring_numeral_1_eq_1)
  28.492 -
  28.493 -text {*
  28.494 -  Special-case simplification for small constants.
  28.495 -*}
  28.496 -
  28.497 -text{*
  28.498 -  Unary minus for the abstract constant 1. Cannot be inserted
  28.499 -  as a simprule until later: it is @{text number_of_Min} re-oriented!
  28.500 -*}
  28.501 -
  28.502 -lemma numeral_m1_eq_minus_1:
  28.503 -  "(-1::'a::number_ring) = - 1"
  28.504 -  unfolding number_of_eq numeral_simps by simp
  28.505 -
  28.506 -lemma mult_minus1 [simp]:
  28.507 -  "-1 * z = -(z::'a::number_ring)"
  28.508 -  unfolding number_of_eq numeral_simps by simp
  28.509 -
  28.510 -lemma mult_minus1_right [simp]:
  28.511 -  "z * -1 = -(z::'a::number_ring)"
  28.512 -  unfolding number_of_eq numeral_simps by simp
  28.513 -
  28.514 -(*Negation of a coefficient*)
  28.515 -lemma minus_number_of_mult [simp]:
  28.516 -   "- (number_of w) * z = number_of (uminus w) * (z::'a::number_ring)"
  28.517 -   unfolding number_of_eq by simp
  28.518 -
  28.519 -text {* Subtraction *}
  28.520 -
  28.521 -lemma diff_number_of_eq:
  28.522 -  "number_of v - number_of w =
  28.523 -    (number_of (v + uminus w)::'a::number_ring)"
  28.524 -  unfolding number_of_eq by simp
  28.525 -
  28.526 -lemma number_of_Pls:
  28.527 -  "number_of Pls = (0::'a::number_ring)"
  28.528 -  unfolding number_of_eq numeral_simps by simp
  28.529 -
  28.530 -lemma number_of_Min:
  28.531 -  "number_of Min = (- 1::'a::number_ring)"
  28.532 -  unfolding number_of_eq numeral_simps by simp
  28.533 -
  28.534 -lemma number_of_Bit0:
  28.535 -  "number_of (Bit0 w) = (0::'a::number_ring) + (number_of w) + (number_of w)"
  28.536 -  unfolding number_of_eq numeral_simps by simp
  28.537 -
  28.538 -lemma number_of_Bit1:
  28.539 -  "number_of (Bit1 w) = (1::'a::number_ring) + (number_of w) + (number_of w)"
  28.540 -  unfolding number_of_eq numeral_simps by simp
  28.541 -
  28.542 -
  28.543 -subsubsection {* Equality of Binary Numbers *}
  28.544 -
  28.545 -text {* First version by Norbert Voelker *}
  28.546 -
  28.547 -definition (*for simplifying equalities*) iszero :: "'a\<Colon>semiring_1 \<Rightarrow> bool" where
  28.548 -  "iszero z \<longleftrightarrow> z = 0"
  28.549 -
  28.550 -lemma iszero_0: "iszero 0"
  28.551 -  by (simp add: iszero_def)
  28.552 -
  28.553 -lemma iszero_Numeral0: "iszero (Numeral0 :: 'a::number_ring)"
  28.554 -  by (simp add: iszero_0)
  28.555 -
  28.556 -lemma not_iszero_1: "\<not> iszero 1"
  28.557 -  by (simp add: iszero_def)
  28.558 -
  28.559 -lemma not_iszero_Numeral1: "\<not> iszero (Numeral1 :: 'a::number_ring)"
  28.560 -  by (simp add: not_iszero_1)
  28.561 -
  28.562 -lemma eq_number_of_eq [simp]:
  28.563 -  "((number_of x::'a::number_ring) = number_of y) =
  28.564 -     iszero (number_of (x + uminus y) :: 'a)"
  28.565 -unfolding iszero_def number_of_add number_of_minus
  28.566 -by (simp add: algebra_simps)
  28.567 -
  28.568 -lemma iszero_number_of_Pls:
  28.569 -  "iszero ((number_of Pls)::'a::number_ring)"
  28.570 -unfolding iszero_def numeral_0_eq_0 ..
  28.571 -
  28.572 -lemma nonzero_number_of_Min:
  28.573 -  "~ iszero ((number_of Min)::'a::number_ring)"
  28.574 -unfolding iszero_def numeral_m1_eq_minus_1 by simp
  28.575 -
  28.576 -
  28.577  subsubsection {* Comparisons, for Ordered Rings *}
  28.578  
  28.579  lemmas double_eq_0_iff = double_zero
  28.580 @@ -1137,129 +668,6 @@
  28.581    qed
  28.582  qed
  28.583  
  28.584 -lemma iszero_number_of_Bit0:
  28.585 -  "iszero (number_of (Bit0 w)::'a) = 
  28.586 -   iszero (number_of w::'a::{ring_char_0,number_ring})"
  28.587 -proof -
  28.588 -  have "(of_int w + of_int w = (0::'a)) \<Longrightarrow> (w = 0)"
  28.589 -  proof -
  28.590 -    assume eq: "of_int w + of_int w = (0::'a)"
  28.591 -    then have "of_int (w + w) = (of_int 0 :: 'a)" by simp
  28.592 -    then have "w + w = 0" by (simp only: of_int_eq_iff)
  28.593 -    then show "w = 0" by (simp only: double_eq_0_iff)
  28.594 -  qed
  28.595 -  thus ?thesis
  28.596 -    by (auto simp add: iszero_def number_of_eq numeral_simps)
  28.597 -qed
  28.598 -
  28.599 -lemma iszero_number_of_Bit1:
  28.600 -  "~ iszero (number_of (Bit1 w)::'a::{ring_char_0,number_ring})"
  28.601 -proof -
  28.602 -  have "1 + of_int w + of_int w \<noteq> (0::'a)"
  28.603 -  proof
  28.604 -    assume eq: "1 + of_int w + of_int w = (0::'a)"
  28.605 -    hence "of_int (1 + w + w) = (of_int 0 :: 'a)" by simp 
  28.606 -    hence "1 + w + w = 0" by (simp only: of_int_eq_iff)
  28.607 -    with odd_nonzero show False by blast
  28.608 -  qed
  28.609 -  thus ?thesis
  28.610 -    by (auto simp add: iszero_def number_of_eq numeral_simps)
  28.611 -qed
  28.612 -
  28.613 -lemmas iszero_simps [simp] =
  28.614 -  iszero_0 not_iszero_1
  28.615 -  iszero_number_of_Pls nonzero_number_of_Min
  28.616 -  iszero_number_of_Bit0 iszero_number_of_Bit1
  28.617 -(* iszero_number_of_Pls would never normally be used
  28.618 -   because its lhs simplifies to "iszero 0" *)
  28.619 -
  28.620 -text {* Less-Than or Equals *}
  28.621 -
  28.622 -text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
  28.623 -
  28.624 -lemmas le_number_of_eq_not_less =
  28.625 -  linorder_not_less [of "number_of w" "number_of v", symmetric] for w v
  28.626 -
  28.627 -
  28.628 -text {* Absolute value (@{term abs}) *}
  28.629 -
  28.630 -lemma abs_number_of:
  28.631 -  "abs(number_of x::'a::{linordered_idom,number_ring}) =
  28.632 -   (if number_of x < (0::'a) then -number_of x else number_of x)"
  28.633 -  by (simp add: abs_if)
  28.634 -
  28.635 -
  28.636 -text {* Re-orientation of the equation nnn=x *}
  28.637 -
  28.638 -lemma number_of_reorient:
  28.639 -  "(number_of w = x) = (x = number_of w)"
  28.640 -  by auto
  28.641 -
  28.642 -
  28.643 -subsubsection {* Simplification of arithmetic operations on integer constants. *}
  28.644 -
  28.645 -lemmas arith_extra_simps [simp] =
  28.646 -  number_of_add [symmetric]
  28.647 -  number_of_minus [symmetric]
  28.648 -  numeral_m1_eq_minus_1 [symmetric]
  28.649 -  number_of_mult [symmetric]
  28.650 -  diff_number_of_eq abs_number_of
  28.651 -
  28.652 -text {*
  28.653 -  For making a minimal simpset, one must include these default simprules.
  28.654 -  Also include @{text simp_thms}.
  28.655 -*}
  28.656 -
  28.657 -lemmas arith_simps = 
  28.658 -  normalize_bin_simps pred_bin_simps succ_bin_simps
  28.659 -  add_bin_simps minus_bin_simps mult_bin_simps
  28.660 -  abs_zero abs_one arith_extra_simps
  28.661 -
  28.662 -text {* Simplification of relational operations *}
  28.663 -
  28.664 -lemma less_number_of [simp]:
  28.665 -  "(number_of x::'a::{linordered_idom,number_ring}) < number_of y \<longleftrightarrow> x < y"
  28.666 -  unfolding number_of_eq by (rule of_int_less_iff)
  28.667 -
  28.668 -lemma le_number_of [simp]:
  28.669 -  "(number_of x::'a::{linordered_idom,number_ring}) \<le> number_of y \<longleftrightarrow> x \<le> y"
  28.670 -  unfolding number_of_eq by (rule of_int_le_iff)
  28.671 -
  28.672 -lemma eq_number_of [simp]:
  28.673 -  "(number_of x::'a::{ring_char_0,number_ring}) = number_of y \<longleftrightarrow> x = y"
  28.674 -  unfolding number_of_eq by (rule of_int_eq_iff)
  28.675 -
  28.676 -lemmas rel_simps =
  28.677 -  less_number_of less_bin_simps
  28.678 -  le_number_of le_bin_simps
  28.679 -  eq_number_of_eq eq_bin_simps
  28.680 -  iszero_simps
  28.681 -
  28.682 -
  28.683 -subsubsection {* Simplification of arithmetic when nested to the right. *}
  28.684 -
  28.685 -lemma add_number_of_left [simp]:
  28.686 -  "number_of v + (number_of w + z) =
  28.687 -   (number_of(v + w) + z::'a::number_ring)"
  28.688 -  by (simp add: add_assoc [symmetric])
  28.689 -
  28.690 -lemma mult_number_of_left [simp]:
  28.691 -  "number_of v * (number_of w * z) =
  28.692 -   (number_of(v * w) * z::'a::number_ring)"
  28.693 -  by (simp add: mult_assoc [symmetric])
  28.694 -
  28.695 -lemma add_number_of_diff1:
  28.696 -  "number_of v + (number_of w - c) = 
  28.697 -  number_of(v + w) - (c::'a::number_ring)"
  28.698 -  by (simp add: diff_minus)
  28.699 -
  28.700 -lemma add_number_of_diff2 [simp]:
  28.701 -  "number_of v + (c - number_of w) =
  28.702 -   number_of (v + uminus w) + (c::'a::number_ring)"
  28.703 -by (simp add: algebra_simps diff_number_of_eq [symmetric])
  28.704 -
  28.705 -
  28.706 -
  28.707  
  28.708  subsection {* The Set of Integers *}
  28.709  
  28.710 @@ -1363,14 +771,8 @@
  28.711    qed
  28.712  qed 
  28.713  
  28.714 -lemma Ints_number_of [simp]:
  28.715 -  "(number_of w :: 'a::number_ring) \<in> Ints"
  28.716 -  unfolding number_of_eq Ints_def by simp
  28.717 -
  28.718 -lemma Nats_number_of [simp]:
  28.719 -  "Int.Pls \<le> w \<Longrightarrow> (number_of w :: 'a::number_ring) \<in> Nats"
  28.720 -unfolding Int.Pls_def number_of_eq
  28.721 -by (simp only: of_nat_nat [symmetric] of_nat_in_Nats)
  28.722 +lemma Nats_numeral [simp]: "numeral w \<in> Nats"
  28.723 +  using of_nat_in_Nats [of "numeral w"] by simp
  28.724  
  28.725  lemma Ints_odd_less_0: 
  28.726    assumes in_Ints: "a \<in> Ints"
  28.727 @@ -1412,100 +814,16 @@
  28.728  lemmas int_setprod = of_nat_setprod [where 'a=int]
  28.729  
  28.730  
  28.731 -subsection{*Inequality Reasoning for the Arithmetic Simproc*}
  28.732 -
  28.733 -lemma add_numeral_0: "Numeral0 + a = (a::'a::number_ring)"
  28.734 -by simp 
  28.735 -
  28.736 -lemma add_numeral_0_right: "a + Numeral0 = (a::'a::number_ring)"
  28.737 -by simp
  28.738 -
  28.739 -lemma mult_numeral_1: "Numeral1 * a = (a::'a::number_ring)"
  28.740 -by simp 
  28.741 -
  28.742 -lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::number_ring)"
  28.743 -by simp
  28.744 -
  28.745 -lemma divide_numeral_1: "a / Numeral1 = (a::'a::{number_ring,field})"
  28.746 -by simp
  28.747 -
  28.748 -lemma inverse_numeral_1:
  28.749 -  "inverse Numeral1 = (Numeral1::'a::{number_ring,field})"
  28.750 -by simp
  28.751 -
  28.752 -text{*Theorem lists for the cancellation simprocs. The use of binary numerals
  28.753 -for 0 and 1 reduces the number of special cases.*}
  28.754 -
  28.755 -lemmas add_0s = add_numeral_0 add_numeral_0_right
  28.756 -lemmas mult_1s = mult_numeral_1 mult_numeral_1_right 
  28.757 -                 mult_minus1 mult_minus1_right
  28.758 -
  28.759 -
  28.760 -subsection{*Special Arithmetic Rules for Abstract 0 and 1*}
  28.761 -
  28.762 -text{*Arithmetic computations are defined for binary literals, which leaves 0
  28.763 -and 1 as special cases. Addition already has rules for 0, but not 1.
  28.764 -Multiplication and unary minus already have rules for both 0 and 1.*}
  28.765 -
  28.766 -
  28.767 -lemma binop_eq: "[|f x y = g x y; x = x'; y = y'|] ==> f x' y' = g x' y'"
  28.768 -by simp
  28.769 -
  28.770 -
  28.771 -lemmas add_number_of_eq = number_of_add [symmetric]
  28.772 -
  28.773 -text{*Allow 1 on either or both sides*}
  28.774 -lemma semiring_one_add_one_is_two: "1 + 1 = (2::'a::number_semiring)"
  28.775 -  using number_of_int [where 'a='a and n="Suc (Suc 0)"]
  28.776 -  by (simp add: numeral_simps)
  28.777 -
  28.778 -lemma one_add_one_is_two: "1 + 1 = (2::'a::number_ring)"
  28.779 -by (rule semiring_one_add_one_is_two)
  28.780 -
  28.781 -lemmas add_special =
  28.782 -    one_add_one_is_two
  28.783 -    binop_eq [of "op +", OF add_number_of_eq numeral_1_eq_1 refl]
  28.784 -    binop_eq [of "op +", OF add_number_of_eq refl numeral_1_eq_1]
  28.785 -
  28.786 -text{*Allow 1 on either or both sides (1-1 already simplifies to 0)*}
  28.787 -lemmas diff_special =
  28.788 -    binop_eq [of "op -", OF diff_number_of_eq numeral_1_eq_1 refl]
  28.789 -    binop_eq [of "op -", OF diff_number_of_eq refl numeral_1_eq_1]
  28.790 -
  28.791 -text{*Allow 0 or 1 on either side with a binary numeral on the other*}
  28.792 -lemmas eq_special =
  28.793 -    binop_eq [of "op =", OF eq_number_of_eq numeral_0_eq_0 refl]
  28.794 -    binop_eq [of "op =", OF eq_number_of_eq numeral_1_eq_1 refl]
  28.795 -    binop_eq [of "op =", OF eq_number_of_eq refl numeral_0_eq_0]
  28.796 -    binop_eq [of "op =", OF eq_number_of_eq refl numeral_1_eq_1]
  28.797 -
  28.798 -text{*Allow 0 or 1 on either side with a binary numeral on the other*}
  28.799 -lemmas less_special =
  28.800 -  binop_eq [of "op <", OF less_number_of numeral_0_eq_0 refl]
  28.801 -  binop_eq [of "op <", OF less_number_of numeral_1_eq_1 refl]
  28.802 -  binop_eq [of "op <", OF less_number_of refl numeral_0_eq_0]
  28.803 -  binop_eq [of "op <", OF less_number_of refl numeral_1_eq_1]
  28.804 -
  28.805 -text{*Allow 0 or 1 on either side with a binary numeral on the other*}
  28.806 -lemmas le_special =
  28.807 -    binop_eq [of "op \<le>", OF le_number_of numeral_0_eq_0 refl]
  28.808 -    binop_eq [of "op \<le>", OF le_number_of numeral_1_eq_1 refl]
  28.809 -    binop_eq [of "op \<le>", OF le_number_of refl numeral_0_eq_0]
  28.810 -    binop_eq [of "op \<le>", OF le_number_of refl numeral_1_eq_1]
  28.811 -
  28.812 -lemmas arith_special[simp] = 
  28.813 -       add_special diff_special eq_special less_special le_special
  28.814 -
  28.815 -
  28.816  text {* Legacy theorems *}
  28.817  
  28.818  lemmas zle_int = of_nat_le_iff [where 'a=int]
  28.819  lemmas int_int_eq = of_nat_eq_iff [where 'a=int]
  28.820 +lemmas numeral_1_eq_1 = numeral_One
  28.821  
  28.822  subsection {* Setting up simplification procedures *}
  28.823  
  28.824  lemmas int_arith_rules =
  28.825 -  neg_le_iff_le numeral_0_eq_0 numeral_1_eq_1
  28.826 +  neg_le_iff_le numeral_One
  28.827    minus_zero diff_minus left_minus right_minus
  28.828    mult_zero_left mult_zero_right mult_1_left mult_1_right
  28.829    mult_minus_left mult_minus_right
  28.830 @@ -1513,56 +831,39 @@
  28.831    of_nat_0 of_nat_1 of_nat_Suc of_nat_add of_nat_mult
  28.832    of_int_0 of_int_1 of_int_add of_int_mult
  28.833  
  28.834 +use "Tools/numeral.ML"
  28.835  use "Tools/int_arith.ML"
  28.836  declaration {* K Int_Arith.setup *}
  28.837  
  28.838 -simproc_setup fast_arith ("(m::'a::{linordered_idom,number_ring}) < n" |
  28.839 -  "(m::'a::{linordered_idom,number_ring}) <= n" |
  28.840 -  "(m::'a::{linordered_idom,number_ring}) = n") =
  28.841 +simproc_setup fast_arith ("(m::'a::linordered_idom) < n" |
  28.842 +  "(m::'a::linordered_idom) <= n" |
  28.843 +  "(m::'a::linordered_idom) = n") =
  28.844    {* fn _ => fn ss => fn ct => Lin_Arith.simproc ss (term_of ct) *}
  28.845  
  28.846  setup {*
  28.847    Reorient_Proc.add
  28.848 -    (fn Const (@{const_name number_of}, _) $ _ => true | _ => false)
  28.849 +    (fn Const (@{const_name numeral}, _) $ _ => true
  28.850 +    | Const (@{const_name neg_numeral}, _) $ _ => true
  28.851 +    | _ => false)
  28.852  *}
  28.853  
  28.854 -simproc_setup reorient_numeral ("number_of w = x") = Reorient_Proc.proc
  28.855 +simproc_setup reorient_numeral
  28.856 +  ("numeral w = x" | "neg_numeral w = y") = Reorient_Proc.proc
  28.857  
  28.858  
  28.859  subsection{*Lemmas About Small Numerals*}
  28.860  
  28.861 -lemma of_int_m1 [simp]: "of_int -1 = (-1 :: 'a :: number_ring)"
  28.862 -proof -
  28.863 -  have "(of_int -1 :: 'a) = of_int (- 1)" by simp
  28.864 -  also have "... = - of_int 1" by (simp only: of_int_minus)
  28.865 -  also have "... = -1" by simp
  28.866 -  finally show ?thesis .
  28.867 -qed
  28.868 -
  28.869 -lemma abs_minus_one [simp]: "abs (-1) = (1::'a::{linordered_idom,number_ring})"
  28.870 -by (simp add: abs_if)
  28.871 -
  28.872  lemma abs_power_minus_one [simp]:
  28.873 -  "abs(-1 ^ n) = (1::'a::{linordered_idom,number_ring})"
  28.874 +  "abs(-1 ^ n) = (1::'a::linordered_idom)"
  28.875  by (simp add: power_abs)
  28.876  
  28.877 -lemma of_int_number_of_eq [simp]:
  28.878 -     "of_int (number_of v) = (number_of v :: 'a :: number_ring)"
  28.879 -by (simp add: number_of_eq) 
  28.880 -
  28.881  text{*Lemmas for specialist use, NOT as default simprules*}
  28.882  (* TODO: see if semiring duplication can be removed without breaking proofs *)
  28.883 -lemma semiring_mult_2: "2 * z = (z+z::'a::number_semiring)"
  28.884 -unfolding semiring_one_add_one_is_two [symmetric] left_distrib by simp
  28.885 +lemma mult_2: "2 * z = (z+z::'a::semiring_1)"
  28.886 +unfolding one_add_one [symmetric] left_distrib by simp
  28.887  
  28.888 -lemma semiring_mult_2_right: "z * 2 = (z+z::'a::number_semiring)"
  28.889 -by (subst mult_commute, rule semiring_mult_2)
  28.890 -
  28.891 -lemma mult_2: "2 * z = (z+z::'a::number_ring)"
  28.892 -by (rule semiring_mult_2)
  28.893 -
  28.894 -lemma mult_2_right: "z * 2 = (z+z::'a::number_ring)"
  28.895 -by (rule semiring_mult_2_right)
  28.896 +lemma mult_2_right: "z * 2 = (z+z::'a::semiring_1)"
  28.897 +unfolding one_add_one [symmetric] right_distrib by simp
  28.898  
  28.899  
  28.900  subsection{*More Inequality Reasoning*}
  28.901 @@ -1608,7 +909,7 @@
  28.902  
  28.903  text{*This simplifies expressions of the form @{term "int n = z"} where
  28.904        z is an integer literal.*}
  28.905 -lemmas int_eq_iff_number_of [simp] = int_eq_iff [of _ "number_of v"] for v
  28.906 +lemmas int_eq_iff_numeral [simp] = int_eq_iff [of _ "numeral v"] for v
  28.907  
  28.908  lemma split_nat [arith_split]:
  28.909    "P(nat(i::int)) = ((\<forall>n. i = int n \<longrightarrow> P n) & (i < 0 \<longrightarrow> P 0))"
  28.910 @@ -1853,12 +1154,14 @@
  28.911        by (simp add: mn)
  28.912      finally have "2*\<bar>n\<bar> \<le> 1" .
  28.913      thus "False" using 0
  28.914 -      by auto
  28.915 +      by arith
  28.916    qed
  28.917    thus ?thesis using 0
  28.918      by auto
  28.919  qed
  28.920  
  28.921 +ML_val {* @{const_name neg_numeral} *}
  28.922 +
  28.923  lemma pos_zmult_eq_1_iff_lemma: "(m * n = 1) ==> m = (1::int) | m = -1"
  28.924  by (insert abs_zmult_eq_1 [of m n], arith)
  28.925  
  28.926 @@ -1894,125 +1197,170 @@
  28.927  
  28.928  text{*These distributive laws move literals inside sums and differences.*}
  28.929  
  28.930 -lemmas left_distrib_number_of [simp] = left_distrib [of _ _ "number_of v"] for v
  28.931 -lemmas right_distrib_number_of [simp] = right_distrib [of "number_of v"] for v
  28.932 -lemmas left_diff_distrib_number_of [simp] = left_diff_distrib [of _ _ "number_of v"] for v
  28.933 -lemmas right_diff_distrib_number_of [simp] = right_diff_distrib [of "number_of v"] for v
  28.934 +lemmas left_distrib_numeral [simp] = left_distrib [of _ _ "numeral v"] for v
  28.935 +lemmas right_distrib_numeral [simp] = right_distrib [of "numeral v"] for v
  28.936 +lemmas left_diff_distrib_numeral [simp] = left_diff_distrib [of _ _ "numeral v"] for v
  28.937 +lemmas right_diff_distrib_numeral [simp] = right_diff_distrib [of "numeral v"] for v
  28.938  
  28.939  text{*These are actually for fields, like real: but where else to put them?*}
  28.940  
  28.941 -lemmas zero_less_divide_iff_number_of [simp, no_atp] = zero_less_divide_iff [of "number_of w"] for w
  28.942 -lemmas divide_less_0_iff_number_of [simp, no_atp] = divide_less_0_iff [of "number_of w"] for w
  28.943 -lemmas zero_le_divide_iff_number_of [simp, no_atp] = zero_le_divide_iff [of "number_of w"] for w
  28.944 -lemmas divide_le_0_iff_number_of [simp, no_atp] = divide_le_0_iff [of "number_of w"] for w
  28.945 +lemmas zero_less_divide_iff_numeral [simp, no_atp] = zero_less_divide_iff [of "numeral w"] for w
  28.946 +lemmas divide_less_0_iff_numeral [simp, no_atp] = divide_less_0_iff [of "numeral w"] for w
  28.947 +lemmas zero_le_divide_iff_numeral [simp, no_atp] = zero_le_divide_iff [of "numeral w"] for w
  28.948 +lemmas divide_le_0_iff_numeral [simp, no_atp] = divide_le_0_iff [of "numeral w"] for w
  28.949  
  28.950  
  28.951  text {*Replaces @{text "inverse #nn"} by @{text "1/#nn"}.  It looks
  28.952    strange, but then other simprocs simplify the quotient.*}
  28.953  
  28.954 -lemmas inverse_eq_divide_number_of [simp] = inverse_eq_divide [of "number_of w"] for w
  28.955 +lemmas inverse_eq_divide_numeral [simp] =
  28.956 +  inverse_eq_divide [of "numeral w"] for w
  28.957 +
  28.958 +lemmas inverse_eq_divide_neg_numeral [simp] =
  28.959 +  inverse_eq_divide [of "neg_numeral w"] for w
  28.960  
  28.961  text {*These laws simplify inequalities, moving unary minus from a term
  28.962  into the literal.*}
  28.963  
  28.964 -lemmas less_minus_iff_number_of [simp, no_atp] = less_minus_iff [of "number_of v"] for v
  28.965 -lemmas le_minus_iff_number_of [simp, no_atp] = le_minus_iff [of "number_of v"] for v
  28.966 -lemmas equation_minus_iff_number_of [simp, no_atp] = equation_minus_iff [of "number_of v"] for v
  28.967 -lemmas minus_less_iff_number_of [simp, no_atp] = minus_less_iff [of _ "number_of v"] for v
  28.968 -lemmas minus_le_iff_number_of [simp, no_atp] = minus_le_iff [of _ "number_of v"] for v
  28.969 -lemmas minus_equation_iff_number_of [simp, no_atp] = minus_equation_iff [of _ "number_of v"] for v
  28.970 +lemmas le_minus_iff_numeral [simp, no_atp] =
  28.971 +  le_minus_iff [of "numeral v"]
  28.972 +  le_minus_iff [of "neg_numeral v"] for v
  28.973 +
  28.974 +lemmas equation_minus_iff_numeral [simp, no_atp] =
  28.975 +  equation_minus_iff [of "numeral v"]
  28.976 +  equation_minus_iff [of "neg_numeral v"] for v
  28.977 +
  28.978 +lemmas minus_less_iff_numeral [simp, no_atp] =
  28.979 +  minus_less_iff [of _ "numeral v"]
  28.980 +  minus_less_iff [of _ "neg_numeral v"] for v
  28.981 +
  28.982 +lemmas minus_le_iff_numeral [simp, no_atp] =
  28.983 +  minus_le_iff [of _ "numeral v"]
  28.984 +  minus_le_iff [of _ "neg_numeral v"] for v
  28.985 +
  28.986 +lemmas minus_equation_iff_numeral [simp, no_atp] =
  28.987 +  minus_equation_iff [of _ "numeral v"]
  28.988 +  minus_equation_iff [of _ "neg_numeral v"] for v
  28.989  
  28.990  text{*To Simplify Inequalities Where One Side is the Constant 1*}
  28.991  
  28.992  lemma less_minus_iff_1 [simp,no_atp]:
  28.993 -  fixes b::"'b::{linordered_idom,number_ring}"
  28.994 +  fixes b::"'b::linordered_idom"
  28.995    shows "(1 < - b) = (b < -1)"
  28.996  by auto
  28.997  
  28.998  lemma le_minus_iff_1 [simp,no_atp]:
  28.999 -  fixes b::"'b::{linordered_idom,number_ring}"
 28.1000 +  fixes b::"'b::linordered_idom"
 28.1001    shows "(1 \<le> - b) = (b \<le> -1)"
 28.1002  by auto
 28.1003  
 28.1004  lemma equation_minus_iff_1 [simp,no_atp]:
 28.1005 -  fixes b::"'b::number_ring"
 28.1006 +  fixes b::"'b::ring_1"
 28.1007    shows "(1 = - b) = (b = -1)"
 28.1008  by (subst equation_minus_iff, auto)
 28.1009  
 28.1010  lemma minus_less_iff_1 [simp,no_atp]:
 28.1011 -  fixes a::"'b::{linordered_idom,number_ring}"
 28.1012 +  fixes a::"'b::linordered_idom"
 28.1013    shows "(- a < 1) = (-1 < a)"
 28.1014  by auto
 28.1015  
 28.1016  lemma minus_le_iff_1 [simp,no_atp]:
 28.1017 -  fixes a::"'b::{linordered_idom,number_ring}"
 28.1018 +  fixes a::"'b::linordered_idom"
 28.1019    shows "(- a \<le> 1) = (-1 \<le> a)"
 28.1020  by auto
 28.1021  
 28.1022  lemma minus_equation_iff_1 [simp,no_atp]:
 28.1023 -  fixes a::"'b::number_ring"
 28.1024 +  fixes a::"'b::ring_1"
 28.1025    shows "(- a = 1) = (a = -1)"
 28.1026  by (subst minus_equation_iff, auto)
 28.1027  
 28.1028  
 28.1029  text {*Cancellation of constant factors in comparisons (@{text "<"} and @{text "\<le>"}) *}
 28.1030  
 28.1031 -lemmas mult_less_cancel_left_number_of [simp, no_atp] = mult_less_cancel_left [of "number_of v"] for v
 28.1032 -lemmas mult_less_cancel_right_number_of [simp, no_atp] = mult_less_cancel_right [of _ "number_of v"] for v
 28.1033 -lemmas mult_le_cancel_left_number_of [simp, no_atp] = mult_le_cancel_left [of "number_of v"] for v
 28.1034 -lemmas mult_le_cancel_right_number_of [simp, no_atp] = mult_le_cancel_right [of _ "number_of v"] for v
 28.1035 +lemmas mult_less_cancel_left_numeral [simp, no_atp] = mult_less_cancel_left [of "numeral v"] for v
 28.1036 +lemmas mult_less_cancel_right_numeral [simp, no_atp] = mult_less_cancel_right [of _ "numeral v"] for v
 28.1037 +lemmas mult_le_cancel_left_numeral [simp, no_atp] = mult_le_cancel_left [of "numeral v"] for v
 28.1038 +lemmas mult_le_cancel_right_numeral [simp, no_atp] = mult_le_cancel_right [of _ "numeral v"] for v
 28.1039  
 28.1040  
 28.1041  text {*Multiplying out constant divisors in comparisons (@{text "<"}, @{text "\<le>"} and @{text "="}) *}
 28.1042  
 28.1043 -lemmas le_divide_eq_number_of1 [simp] = le_divide_eq [of _ _ "number_of w"] for w
 28.1044 -lemmas divide_le_eq_number_of1 [simp] = divide_le_eq [of _ "number_of w"] for w
 28.1045 -lemmas less_divide_eq_number_of1 [simp] = less_divide_eq [of _ _ "number_of w"] for w
 28.1046 -lemmas divide_less_eq_number_of1 [simp] = divide_less_eq [of _ "number_of w"] for w
 28.1047 -lemmas eq_divide_eq_number_of1 [simp] = eq_divide_eq [of _ _ "number_of w"] for w
 28.1048 -lemmas divide_eq_eq_number_of1 [simp] = divide_eq_eq [of _ "number_of w"] for w
 28.1049 +lemmas le_divide_eq_numeral1 [simp] =
 28.1050 +  pos_le_divide_eq [of "numeral w", OF zero_less_numeral]
 28.1051 +  neg_le_divide_eq [of "neg_numeral w", OF neg_numeral_less_zero] for w
 28.1052  
 28.1053 +lemmas divide_le_eq_numeral1 [simp] =
 28.1054 +  pos_divide_le_eq [of "numeral w", OF zero_less_numeral]
 28.1055 +  neg_divide_le_eq [of "neg_numeral w", OF neg_numeral_less_zero] for w
 28.1056 +
 28.1057 +lemmas less_divide_eq_numeral1 [simp] =
 28.1058 +  pos_less_divide_eq [of "numeral w", OF zero_less_numeral]
 28.1059 +  neg_less_divide_eq [of "neg_numeral w", OF neg_numeral_less_zero] for w
 28.1060 +
 28.1061 +lemmas divide_less_eq_numeral1 [simp] =
 28.1062 +  pos_divide_less_eq [of "numeral w", OF zero_less_numeral]
 28.1063 +  neg_divide_less_eq [of "neg_numeral w", OF neg_numeral_less_zero] for w
 28.1064 +
 28.1065 +lemmas eq_divide_eq_numeral1 [simp] =
 28.1066 +  eq_divide_eq [of _ _ "numeral w"]
 28.1067 +  eq_divide_eq [of _ _ "neg_numeral w"] for w
 28.1068 +
 28.1069 +lemmas divide_eq_eq_numeral1 [simp] =
 28.1070 +  divide_eq_eq [of _ "numeral w"]
 28.1071 +  divide_eq_eq [of _ "neg_numeral w"] for w
 28.1072  
 28.1073  subsubsection{*Optional Simplification Rules Involving Constants*}
 28.1074  
 28.1075  text{*Simplify quotients that are compared with a literal constant.*}
 28.1076  
 28.1077 -lemmas le_divide_eq_number_of = le_divide_eq [of "number_of w"] for w
 28.1078 -lemmas divide_le_eq_number_of = divide_le_eq [of _ _ "number_of w"] for w
 28.1079 -lemmas less_divide_eq_number_of = less_divide_eq [of "number_of w"] for w
 28.1080 -lemmas divide_less_eq_number_of = divide_less_eq [of _ _ "number_of w"] for w
 28.1081 -lemmas eq_divide_eq_number_of = eq_divide_eq [of "number_of w"] for w
 28.1082 -lemmas divide_eq_eq_number_of = divide_eq_eq [of _ _ "number_of w"] for w
 28.1083 +lemmas le_divide_eq_numeral =
 28.1084 +  le_divide_eq [of "numeral w"]
 28.1085 +  le_divide_eq [of "neg_numeral w"] for w
 28.1086 +
 28.1087 +lemmas divide_le_eq_numeral =
 28.1088 +  divide_le_eq [of _ _ "numeral w"]
 28.1089 +  divide_le_eq [of _ _ "neg_numeral w"] for w
 28.1090 +
 28.1091 +lemmas less_divide_eq_numeral =
 28.1092 +  less_divide_eq [of "numeral w"]
 28.1093 +  less_divide_eq [of "neg_numeral w"] for w
 28.1094 +
 28.1095 +lemmas divide_less_eq_numeral =
 28.1096 +  divide_less_eq [of _ _ "numeral w"]
 28.1097 +  divide_less_eq [of _ _ "neg_numeral w"] for w
 28.1098 +
 28.1099 +lemmas eq_divide_eq_numeral =
 28.1100 +  eq_divide_eq [of "numeral w"]
 28.1101 +  eq_divide_eq [of "neg_numeral w"] for w
 28.1102 +
 28.1103 +lemmas divide_eq_eq_numeral =
 28.1104 +  divide_eq_eq [of _ _ "numeral w"]
 28.1105 +  divide_eq_eq [of _ _ "neg_numeral w"] for w
 28.1106  
 28.1107  
 28.1108  text{*Not good as automatic simprules because they cause case splits.*}
 28.1109  lemmas divide_const_simps =
 28.1110 -  le_divide_eq_number_of divide_le_eq_number_of less_divide_eq_number_of
 28.1111 -  divide_less_eq_number_of eq_divide_eq_number_of divide_eq_eq_number_of
 28.1112 +  le_divide_eq_numeral divide_le_eq_numeral less_divide_eq_numeral
 28.1113 +  divide_less_eq_numeral eq_divide_eq_numeral divide_eq_eq_numeral
 28.1114    le_divide_eq_1 divide_le_eq_1 less_divide_eq_1 divide_less_eq_1
 28.1115  
 28.1116  text{*Division By @{text "-1"}*}
 28.1117  
 28.1118 -lemma divide_minus1 [simp]:
 28.1119 -     "x/-1 = -(x::'a::{field_inverse_zero, number_ring})"
 28.1120 -by simp
 28.1121 +lemma divide_minus1 [simp]: "(x::'a::field) / -1 = - x"
 28.1122 +  unfolding minus_one [symmetric]
 28.1123 +  unfolding nonzero_minus_divide_right [OF one_neq_zero, symmetric]
 28.1124 +  by simp
 28.1125  
 28.1126 -lemma minus1_divide [simp]:
 28.1127 -     "-1 / (x::'a::{field_inverse_zero, number_ring}) = - (1/x)"
 28.1128 -by (simp add: divide_inverse)
 28.1129 +lemma minus1_divide [simp]: "-1 / (x::'a::field) = - (1 / x)"
 28.1130 +  unfolding minus_one [symmetric] by (rule divide_minus_left)
 28.1131  
 28.1132  lemma half_gt_zero_iff:
 28.1133 -     "(0 < r/2) = (0 < (r::'a::{linordered_field_inverse_zero,number_ring}))"
 28.1134 +     "(0 < r/2) = (0 < (r::'a::linordered_field_inverse_zero))"
 28.1135  by auto
 28.1136  
 28.1137  lemmas half_gt_zero [simp] = half_gt_zero_iff [THEN iffD2]
 28.1138  
 28.1139 -lemma divide_Numeral1:
 28.1140 -  "(x::'a::{field, number_ring}) / Numeral1 = x"
 28.1141 -  by simp
 28.1142 -
 28.1143 -lemma divide_Numeral0:
 28.1144 -  "(x::'a::{field_inverse_zero, number_ring}) / Numeral0 = 0"
 28.1145 +lemma divide_Numeral1: "(x::'a::field) / Numeral1 = x"
 28.1146    by simp
 28.1147  
 28.1148  
 28.1149 @@ -2211,128 +1559,154 @@
 28.1150  
 28.1151  subsection {* Configuration of the code generator *}
 28.1152  
 28.1153 -code_datatype Pls Min Bit0 Bit1 "number_of \<Colon> int \<Rightarrow> int"
 28.1154 +text {* Constructors *}
 28.1155  
 28.1156 -lemmas pred_succ_numeral_code [code] =
 28.1157 -  pred_bin_simps succ_bin_simps
 28.1158 +definition Pos :: "num \<Rightarrow> int" where
 28.1159 +  [simp, code_abbrev]: "Pos = numeral"
 28.1160  
 28.1161 -lemmas plus_numeral_code [code] =
 28.1162 -  add_bin_simps
 28.1163 -  arith_extra_simps(1) [where 'a = int]
 28.1164 +definition Neg :: "num \<Rightarrow> int" where
 28.1165 +  [simp, code_abbrev]: "Neg = neg_numeral"
 28.1166  
 28.1167 -lemmas minus_numeral_code [code] =
 28.1168 -  minus_bin_simps
 28.1169 -  arith_extra_simps(2) [where 'a = int]
 28.1170 -  arith_extra_simps(5) [where 'a = int]
 28.1171 +code_datatype "0::int" Pos Neg
 28.1172  
 28.1173 -lemmas times_numeral_code [code] =
 28.1174 -  mult_bin_simps
 28.1175 -  arith_extra_simps(4) [where 'a = int]
 28.1176 +
 28.1177 +text {* Auxiliary operations *}
 28.1178 +
 28.1179 +definition dup :: "int \<Rightarrow> int" where
 28.1180 +  [simp]: "dup k = k + k"
 28.1181 +
 28.1182 +lemma dup_code [code]:
 28.1183 +  "dup 0 = 0"
 28.1184 +  "dup (Pos n) = Pos (Num.Bit0 n)"
 28.1185 +  "dup (Neg n) = Neg (Num.Bit0 n)"
 28.1186 +  unfolding Pos_def Neg_def neg_numeral_def
 28.1187 +  by (simp_all add: numeral_Bit0)
 28.1188 +
 28.1189 +definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
 28.1190 +  [simp]: "sub m n = numeral m - numeral n"
 28.1191 +
 28.1192 +lemma sub_code [code]:
 28.1193 +  "sub Num.One Num.One = 0"
 28.1194 +  "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
 28.1195 +  "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
 28.1196 +  "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
 28.1197 +  "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
 28.1198 +  "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
 28.1199 +  "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
 28.1200 +  "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
 28.1201 +  "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
 28.1202 +  unfolding sub_def dup_def numeral.simps Pos_def Neg_def
 28.1203 +    neg_numeral_def numeral_BitM
 28.1204 +  by (simp_all only: algebra_simps)
 28.1205 +
 28.1206 +
 28.1207 +text {* Implementations *}
 28.1208 +
 28.1209 +lemma one_int_code [code, code_unfold]:
 28.1210 +  "1 = Pos Num.One"
 28.1211 +  by simp
 28.1212 +
 28.1213 +lemma plus_int_code [code]:
 28.1214 +  "k + 0 = (k::int)"
 28.1215 +  "0 + l = (l::int)"
 28.1216 +  "Pos m + Pos n = Pos (m + n)"
 28.1217 +  "Pos m + Neg n = sub m n"
 28.1218 +  "Neg m + Pos n = sub n m"
 28.1219 +  "Neg m + Neg n = Neg (m + n)"
 28.1220 +  by simp_all
 28.1221 +
 28.1222 +lemma uminus_int_code [code]:
 28.1223 +  "uminus 0 = (0::int)"
 28.1224 +  "uminus (Pos m) = Neg m"
 28.1225 +  "uminus (Neg m) = Pos m"
 28.1226 +  by simp_all
 28.1227 +
 28.1228 +lemma minus_int_code [code]:
 28.1229 +  "k - 0 = (k::int)"
 28.1230 +  "0 - l = uminus (l::int)"
 28.1231 +  "Pos m - Pos n = sub m n"
 28.1232 +  "Pos m - Neg n = Pos (m + n)"
 28.1233 +  "Neg m - Pos n = Neg (m + n)"
 28.1234 +  "Neg m - Neg n = sub n m"
 28.1235 +  by simp_all
 28.1236 +
 28.1237 +lemma times_int_code [code]:
 28.1238 +  "k * 0 = (0::int)"
 28.1239 +  "0 * l = (0::int)"
 28.1240 +  "Pos m * Pos n = Pos (m * n)"
 28.1241 +  "Pos m * Neg n = Neg (m * n)"
 28.1242 +  "Neg m * Pos n = Neg (m * n)"
 28.1243 +  "Neg m * Neg n = Pos (m * n)"
 28.1244 +  by simp_all
 28.1245  
 28.1246  instantiation int :: equal
 28.1247  begin
 28.1248  
 28.1249  definition
 28.1250 -  "HOL.equal k l \<longleftrightarrow> k - l = (0\<Colon>int)"
 28.1251 +  "HOL.equal k l \<longleftrightarrow> k = (l::int)"
 28.1252  
 28.1253 -instance by default (simp add: equal_int_def)
 28.1254 +instance by default (rule equal_int_def)
 28.1255  
 28.1256  end
 28.1257  
 28.1258 -lemma eq_number_of_int_code [code]:
 28.1259 -  "HOL.equal (number_of k \<Colon> int) (number_of l) \<longleftrightarrow> HOL.equal k l"
 28.1260 -  unfolding equal_int_def number_of_is_id ..
 28.1261 +lemma equal_int_code [code]:
 28.1262 +  "HOL.equal 0 (0::int) \<longleftrightarrow> True"
 28.1263 +  "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
 28.1264 +  "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
 28.1265 +  "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
 28.1266 +  "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
 28.1267 +  "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
 28.1268 +  "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
 28.1269 +  "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
 28.1270 +  "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
 28.1271 +  by (auto simp add: equal)
 28.1272  
 28.1273 -lemma eq_int_code [code]:
 28.1274 -  "HOL.equal Int.Pls Int.Pls \<longleftrightarrow> True"
 28.1275 -  "HOL.equal Int.Pls Int.Min \<longleftrightarrow> False"
 28.1276 -  "HOL.equal Int.Pls (Int.Bit0 k2) \<longleftrightarrow> HOL.equal Int.Pls k2"
 28.1277 -  "HOL.equal Int.Pls (Int.Bit1 k2) \<longleftrightarrow> False"
 28.1278 -  "HOL.equal Int.Min Int.Pls \<longleftrightarrow> False"
 28.1279 -  "HOL.equal Int.Min Int.Min \<longleftrightarrow> True"
 28.1280 -  "HOL.equal Int.Min (Int.Bit0 k2) \<longleftrightarrow> False"
 28.1281 -  "HOL.equal Int.Min (Int.Bit1 k2) \<longleftrightarrow> HOL.equal Int.Min k2"
 28.1282 -  "HOL.equal (Int.Bit0 k1) Int.Pls \<longleftrightarrow> HOL.equal k1 Int.Pls"
 28.1283 -  "HOL.equal (Int.Bit1 k1) Int.Pls \<longleftrightarrow> False"
 28.1284 -  "HOL.equal (Int.Bit0 k1) Int.Min \<longleftrightarrow> False"
 28.1285 -  "HOL.equal (Int.Bit1 k1) Int.Min \<longleftrightarrow> HOL.equal k1 Int.Min"
 28.1286 -  "HOL.equal (Int.Bit0 k1) (Int.Bit0 k2) \<longleftrightarrow> HOL.equal k1 k2"
 28.1287 -  "HOL.equal (Int.Bit0 k1) (Int.Bit1 k2) \<longleftrightarrow> False"
 28.1288 -  "HOL.equal (Int.Bit1 k1) (Int.Bit0 k2) \<longleftrightarrow> False"
 28.1289 -  "HOL.equal (Int.Bit1 k1) (Int.Bit1 k2) \<longleftrightarrow> HOL.equal k1 k2"
 28.1290 -  unfolding equal_eq by simp_all
 28.1291 -
 28.1292 -lemma eq_int_refl [code nbe]:
 28.1293 +lemma equal_int_refl [code nbe]:
 28.1294    "HOL.equal (k::int) k \<longleftrightarrow> True"
 28.1295 -  by (rule equal_refl)
 28.1296 -
 28.1297 -lemma less_eq_number_of_int_code [code]:
 28.1298 -  "(number_of k \<Colon> int) \<le> number_of l \<longleftrightarrow> k \<le> l"
 28.1299 -  unfolding number_of_is_id ..
 28.1300 +  by (fact equal_refl)
 28.1301  
 28.1302  lemma less_eq_int_code [code]:
 28.1303 -  "Int.Pls \<le> Int.Pls \<longleftrightarrow> True"
 28.1304 -  "Int.Pls \<le> Int.Min \<longleftrightarrow> False"
 28.1305 -  "Int.Pls \<le> Int.Bit0 k \<longleftrightarrow> Int.Pls \<le> k"
 28.1306 -  "Int.Pls \<le> Int.Bit1 k \<longleftrightarrow> Int.Pls \<le> k"
 28.1307 -  "Int.Min \<le> Int.Pls \<longleftrightarrow> True"
 28.1308 -  "Int.Min \<le> Int.Min \<longleftrightarrow> True"
 28.1309 -  "Int.Min \<le> Int.Bit0 k \<longleftrightarrow> Int.Min < k"
 28.1310 -  "Int.Min \<le> Int.Bit1 k \<longleftrightarrow> Int.Min \<le> k"
 28.1311 -  "Int.Bit0 k \<le> Int.Pls \<longleftrightarrow> k \<le> Int.Pls"
 28.1312 -  "Int.Bit1 k \<le> Int.Pls \<longleftrightarrow> k < Int.Pls"
 28.1313 -  "Int.Bit0 k \<le> Int.Min \<longleftrightarrow> k \<le> Int.Min"
 28.1314 -  "Int.Bit1 k \<le> Int.Min \<longleftrightarrow> k \<le> Int.Min"
 28.1315 -  "Int.Bit0 k1 \<le> Int.Bit0 k2 \<longleftrightarrow> k1 \<le> k2"
 28.1316 -  "Int.Bit0 k1 \<le> Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
 28.1317 -  "Int.Bit1 k1 \<le> Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
 28.1318 -  "Int.Bit1 k1 \<le> Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
 28.1319 +  "0 \<le> (0::int) \<longleftrightarrow> True"
 28.1320 +  "0 \<le> Pos l \<longleftrightarrow> True"
 28.1321 +  "0 \<le> Neg l \<longleftrightarrow> False"
 28.1322 +  "Pos k \<le> 0 \<longleftrightarrow> False"
 28.1323 +  "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
 28.1324 +  "Pos k \<le> Neg l \<longleftrightarrow> False"
 28.1325 +  "Neg k \<le> 0 \<longleftrightarrow> True"
 28.1326 +  "Neg k \<le> Pos l \<longleftrightarrow> True"
 28.1327 +  "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
 28.1328    by simp_all
 28.1329  
 28.1330 -lemma less_number_of_int_code [code]:
 28.1331 -  "(number_of k \<Colon> int) < number_of l \<longleftrightarrow> k < l"
 28.1332 -  unfolding number_of_is_id ..
 28.1333 -
 28.1334  lemma less_int_code [code]:
 28.1335 -  "Int.Pls < Int.Pls \<longleftrightarrow> False"
 28.1336 -  "Int.Pls < Int.Min \<longleftrightarrow> False"
 28.1337 -  "Int.Pls < Int.Bit0 k \<longleftrightarrow> Int.Pls < k"
 28.1338 -  "Int.Pls < Int.Bit1 k \<longleftrightarrow> Int.Pls \<le> k"
 28.1339 -  "Int.Min < Int.Pls \<longleftrightarrow> True"
 28.1340 -  "Int.Min < Int.Min \<longleftrightarrow> False"
 28.1341 -  "Int.Min < Int.Bit0 k \<longleftrightarrow> Int.Min < k"
 28.1342 -  "Int.Min < Int.Bit1 k \<longleftrightarrow> Int.Min < k"
 28.1343 -  "Int.Bit0 k < Int.Pls \<longleftrightarrow> k < Int.Pls"
 28.1344 -  "Int.Bit1 k < Int.Pls \<longleftrightarrow> k < Int.Pls"
 28.1345 -  "Int.Bit0 k < Int.Min \<longleftrightarrow> k \<le> Int.Min"
 28.1346 -  "Int.Bit1 k < Int.Min \<longleftrightarrow> k < Int.Min"
 28.1347 -  "Int.Bit0 k1 < Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
 28.1348 -  "Int.Bit0 k1 < Int.Bit1 k2 \<longleftrightarrow> k1 \<le> k2"
 28.1349 -  "Int.Bit1 k1 < Int.Bit0 k2 \<longleftrightarrow> k1 < k2"
 28.1350 -  "Int.Bit1 k1 < Int.Bit1 k2 \<longleftrightarrow> k1 < k2"
 28.1351 +  "0 < (0::int) \<longleftrightarrow> False"
 28.1352 +  "0 < Pos l \<longleftrightarrow> True"
 28.1353 +  "0 < Neg l \<longleftrightarrow> False"
 28.1354 +  "Pos k < 0 \<longleftrightarrow> False"
 28.1355 +  "Pos k < Pos l \<longleftrightarrow> k < l"
 28.1356 +  "Pos k < Neg l \<longleftrightarrow> False"
 28.1357 +  "Neg k < 0 \<longleftrightarrow> True"
 28.1358 +  "Neg k < Pos l \<longleftrightarrow> True"
 28.1359 +  "Neg k < Neg l \<longleftrightarrow> l < k"
 28.1360    by simp_all
 28.1361  
 28.1362 -definition
 28.1363 -  nat_aux :: "int \<Rightarrow> nat \<Rightarrow> nat" where
 28.1364 -  "nat_aux i n = nat i + n"
 28.1365 +lemma nat_numeral [simp, code_abbrev]:
 28.1366 +  "nat (numeral k) = numeral k"
 28.1367 +  by (simp add: nat_eq_iff)
 28.1368  
 28.1369 -lemma [code]:
 28.1370 -  "nat_aux i n = (if i \<le> 0 then n else nat_aux (i - 1) (Suc n))"  -- {* tail recursive *}
 28.1371 -  by (auto simp add: nat_aux_def nat_eq_iff linorder_not_le order_less_imp_le
 28.1372 -    dest: zless_imp_add1_zle)
 28.1373 +lemma nat_code [code]:
 28.1374 +  "nat (Int.Neg k) = 0"
 28.1375 +  "nat 0 = 0"
 28.1376 +  "nat (Int.Pos k) = nat_of_num k"
 28.1377 +  by (simp_all add: nat_of_num_numeral nat_numeral)
 28.1378  
 28.1379 -lemma [code]: "nat i = nat_aux i 0"
 28.1380 -  by (simp add: nat_aux_def)
 28.1381 +lemma (in ring_1) of_int_code [code]:
 28.1382 +  "of_int (Int.Neg k) = neg_numeral k"
 28.1383 +  "of_int 0 = 0"
 28.1384 +  "of_int (Int.Pos k) = numeral k"
 28.1385 +  by simp_all
 28.1386  
 28.1387 -hide_const (open) nat_aux
 28.1388  
 28.1389 -lemma zero_is_num_zero [code, code_unfold]:
 28.1390 -  "(0\<Colon>int) = Numeral0" 
 28.1391 -  by simp
 28.1392 -
 28.1393 -lemma one_is_num_one [code, code_unfold]:
 28.1394 -  "(1\<Colon>int) = Numeral1" 
 28.1395 -  by simp
 28.1396 +text {* Serializer setup *}
 28.1397  
 28.1398  code_modulename SML
 28.1399    Int Arith
 28.1400 @@ -2345,7 +1719,7 @@
 28.1401  
 28.1402  quickcheck_params [default_type = int]
 28.1403  
 28.1404 -hide_const (open) Pls Min Bit0 Bit1 succ pred
 28.1405 +hide_const (open) Pos Neg sub dup
 28.1406  
 28.1407  
 28.1408  subsection {* Legacy theorems *}
 28.1409 @@ -2378,3 +1752,4 @@
 28.1410  lemmas zpower_int = int_power [symmetric]
 28.1411  
 28.1412  end
 28.1413 +
    29.1 --- a/src/HOL/IsaMakefile	Fri Mar 23 20:32:43 2012 +0100
    29.2 +++ b/src/HOL/IsaMakefile	Mon Mar 26 10:56:56 2012 +0200
    29.3 @@ -195,6 +195,7 @@
    29.4    Meson.thy \
    29.5    Metis.thy \
    29.6    Nat.thy \
    29.7 +  Num.thy \
    29.8    Option.thy \
    29.9    Orderings.thy \
   29.10    Partial_Function.thy \
   29.11 @@ -341,7 +342,6 @@
   29.12    Tools/Nitpick/nitpick_util.ML \
   29.13    Tools/numeral.ML \
   29.14    Tools/numeral_simprocs.ML \
   29.15 -  Tools/numeral_syntax.ML \
   29.16    Tools/Predicate_Compile/core_data.ML \
   29.17    Tools/Predicate_Compile/mode_inference.ML \
   29.18    Tools/Predicate_Compile/predicate_compile_aux.ML \
   29.19 @@ -444,24 +444,25 @@
   29.20    Library/Bit.thy Library/Boolean_Algebra.thy Library/Cardinality.thy	\
   29.21    Library/Char_nat.thy Library/Code_Char.thy Library/Code_Char_chr.thy	\
   29.22    Library/Code_Char_ord.thy Library/Code_Integer.thy			\
   29.23 -  Library/Code_Natural.thy Library/Code_Prolog.thy			\
   29.24 +  Library/Code_Nat.thy Library/Code_Natural.thy				\
   29.25 +  Library/Efficient_Nat.thy Library/Code_Prolog.thy			\
   29.26    Library/Code_Real_Approx_By_Float.thy					\
   29.27    Tools/Predicate_Compile/code_prolog.ML Library/ContNotDenum.thy	\
   29.28    Library/Cset.thy Library/Cset_Monad.thy Library/Continuity.thy	\
   29.29    Library/Convex.thy Library/Countable.thy				\
   29.30 +  Library/Dlist.thy Library/Dlist_Cset.thy Library/Eval_Witness.thy	\
   29.31    Library/DAList.thy Library/Dlist.thy Library/Dlist_Cset.thy 		\
   29.32 -  Library/Efficient_Nat.thy Library/Eval_Witness.thy			\
   29.33 +  Library/Eval_Witness.thy						\
   29.34    Library/Extended_Real.thy Library/Extended_Nat.thy Library/Float.thy	\
   29.35    Library/Formal_Power_Series.thy Library/Fraction_Field.thy		\
   29.36    Library/FrechetDeriv.thy Library/Cset.thy Library/FuncSet.thy		\
   29.37 -  Library/Function_Algebras.thy						\
   29.38 -  Library/Fundamental_Theorem_Algebra.thy Library/Glbs.thy		\
   29.39 -  Library/Indicator_Function.thy Library/Infinite_Set.thy		\
   29.40 -  Library/Inner_Product.thy Library/Kleene_Algebra.thy			\
   29.41 -  Library/LaTeXsugar.thy Library/Lattice_Algebras.thy			\
   29.42 -  Library/Lattice_Syntax.thy Library/Library.thy Library/List_Cset.thy	\
   29.43 -  Library/List_Prefix.thy Library/List_lexord.thy Library/Mapping.thy	\
   29.44 -  Library/Monad_Syntax.thy						\
   29.45 +  Library/Function_Algebras.thy Library/Fundamental_Theorem_Algebra.thy	\
   29.46 +  Library/Glbs.thy Library/Indicator_Function.thy			\
   29.47 +  Library/Infinite_Set.thy Library/Inner_Product.thy			\
   29.48 +  Library/Kleene_Algebra.thy Library/LaTeXsugar.thy			\
   29.49 +  Library/Lattice_Algebras.thy Library/Lattice_Syntax.thy		\
   29.50 +  Library/Library.thy Library/List_Cset.thy Library/List_Prefix.thy	\
   29.51 +  Library/List_lexord.thy Library/Mapping.thy Library/Monad_Syntax.thy	\
   29.52    Library/Multiset.thy Library/Nat_Bijection.thy			\
   29.53    Library/Numeral_Type.thy Library/Old_Recdef.thy			\
   29.54    Library/OptionalSugar.thy Library/Order_Relation.thy			\
   29.55 @@ -479,7 +480,7 @@
   29.56    Library/State_Monad.thy Library/Ramsey.thy				\
   29.57    Library/Reflection.thy Library/Sublist_Order.thy			\
   29.58    Library/Sum_of_Squares.thy Library/Sum_of_Squares/sos_wrapper.ML	\
   29.59 -  Library/Sum_of_Squares/sum_of_squares.ML				\
   29.60 +  Library/Sum_of_Squares/sum_of_squares.ML Library/Target_Numeral.thy	\
   29.61    Library/Transitive_Closure_Table.thy Library/Univ_Poly.thy		\
   29.62    Library/Wfrec.thy Library/While_Combinator.thy Library/Zorn.thy	\
   29.63    $(SRC)/Tools/adhoc_overloading.ML Library/positivstellensatz.ML	\
   29.64 @@ -758,11 +759,11 @@
   29.65  
   29.66  HOL-Library-Codegenerator_Test: HOL-Library $(LOG)/HOL-Library-Codegenerator_Test.gz
   29.67  
   29.68 -$(LOG)/HOL-Library-Codegenerator_Test.gz: $(OUT)/HOL-Library		\
   29.69 -  Codegenerator_Test/ROOT.ML 						\
   29.70 -  Codegenerator_Test/Candidates.thy					\
   29.71 -  Codegenerator_Test/Candidates_Pretty.thy				\
   29.72 -  Codegenerator_Test/Generate.thy					\
   29.73 +$(LOG)/HOL-Library-Codegenerator_Test.gz: $(OUT)/HOL-Library \
   29.74 +  Codegenerator_Test/ROOT.ML \
   29.75 +  Codegenerator_Test/Candidates.thy \
   29.76 +  Codegenerator_Test/Candidates_Pretty.thy \
   29.77 +  Codegenerator_Test/Generate.thy \
   29.78    Codegenerator_Test/Generate_Pretty.thy
   29.79  	@$(ISABELLE_TOOL) usedir -d false -g false -i false $(OUT)/HOL-Library Codegenerator_Test
   29.80  
   29.81 @@ -920,6 +921,10 @@
   29.82  HOL-Imperative_HOL: HOL $(LOG)/HOL-Imperative_HOL.gz
   29.83  
   29.84  $(LOG)/HOL-Imperative_HOL.gz: $(OUT)/HOL \
   29.85 +  Library/Code_Integer.thy \
   29.86 +  Library/Code_Nat.thy \
   29.87 +  Library/Code_Natural.thy \
   29.88 +  Library/Efficient_Nat.thy \
   29.89    Imperative_HOL/Array.thy \
   29.90    Imperative_HOL/Heap.thy \
   29.91    Imperative_HOL/Heap_Monad.thy \
   29.92 @@ -943,6 +948,10 @@
   29.93  HOL-Decision_Procs: HOL $(LOG)/HOL-Decision_Procs.gz
   29.94  
   29.95  $(LOG)/HOL-Decision_Procs.gz: $(OUT)/HOL \
   29.96 +  Library/Code_Integer.thy \
   29.97 +  Library/Code_Nat.thy \
   29.98 +  Library/Code_Natural.thy \
   29.99 +  Library/Efficient_Nat.thy \
  29.100    Decision_Procs/Approximation.thy \
  29.101    Decision_Procs/Commutative_Ring.thy \
  29.102    Decision_Procs/Commutative_Ring_Complete.thy \
  29.103 @@ -991,9 +1000,12 @@
  29.104  HOL-Proofs-Extraction: HOL-Proofs $(LOG)/HOL-Proofs-Extraction.gz
  29.105  
  29.106  $(LOG)/HOL-Proofs-Extraction.gz: $(OUT)/HOL-Proofs		\
  29.107 -  Library/Efficient_Nat.thy Proofs/Extraction/Euclid.thy	\
  29.108 +  Library/Code_Integer.thy Library/Code_Nat.thy			\
  29.109 +  Library/Code_Natural.thy Library/Efficient_Nat.thy		\
  29.110 +  Proofs/Extraction/Euclid.thy					\
  29.111    Proofs/Extraction/Greatest_Common_Divisor.thy			\
  29.112 -  Proofs/Extraction/Higman.thy Proofs/Extraction/Higman_Extraction.thy	\
  29.113 +  Proofs/Extraction/Higman.thy					\
  29.114 +  Proofs/Extraction/Higman_Extraction.thy			\
  29.115    Proofs/Extraction/Pigeonhole.thy				\
  29.116    Proofs/Extraction/QuotRem.thy Proofs/Extraction/ROOT.ML	\
  29.117    Proofs/Extraction/Util.thy Proofs/Extraction/Warshall.thy	\
  29.118 @@ -1113,15 +1125,17 @@
  29.119  HOL-ex: HOL $(LOG)/HOL-ex.gz
  29.120  
  29.121  $(LOG)/HOL-ex.gz: $(OUT)/HOL Decision_Procs/Commutative_Ring.thy	\
  29.122 +  Library/Code_Integer.thy Library/Code_Nat.thy				\
  29.123 +  Library/Code_Natural.thy Library/Efficient_Nat.thy			\
  29.124    Number_Theory/Primes.thy ex/Abstract_NAT.thy ex/Antiquote.thy		\
  29.125    ex/Arith_Examples.thy ex/Arithmetic_Series_Complex.thy ex/BT.thy	\
  29.126    ex/BinEx.thy ex/Binary.thy ex/Birthday_Paradox.thy ex/CTL.thy		\
  29.127    ex/Case_Product.thy ex/Chinese.thy ex/Classical.thy			\
  29.128 -  ex/Coercion_Examples.thy ex/Coherent.thy				\
  29.129 -  ex/Dedekind_Real.thy ex/Efficient_Nat_examples.thy			\
  29.130 +  ex/Code_Nat_examples.thy						\
  29.131 +  ex/Coercion_Examples.thy ex/Coherent.thy ex/Dedekind_Real.thy		\
  29.132    ex/Eval_Examples.thy ex/Executable_Relation.thy ex/Fundefs.thy	\
  29.133    ex/Gauge_Integration.thy ex/Groebner_Examples.thy ex/Guess.thy	\
  29.134 -  ex/HarmonicSeries.thy ex/Hebrew.thy ex/Hex_Bin_Examples.thy 		\
  29.135 +  ex/HarmonicSeries.thy ex/Hebrew.thy ex/Hex_Bin_Examples.thy		\
  29.136    ex/Higher_Order_Logic.thy ex/Iff_Oracle.thy ex/Induction_Schema.thy	\
  29.137    ex/Interpretation_with_Defs.thy ex/Intuitionistic.thy			\
  29.138    ex/Lagrange.thy ex/List_to_Set_Comprehension_Examples.thy		\
    30.1 --- a/src/HOL/Library/BigO.thy	Fri Mar 23 20:32:43 2012 +0100
    30.2 +++ b/src/HOL/Library/BigO.thy	Mon Mar 26 10:56:56 2012 +0200
    30.3 @@ -132,7 +132,6 @@
    30.4    apply (simp add: abs_triangle_ineq)
    30.5    apply (simp add: order_less_le)
    30.6    apply (rule mult_nonneg_nonneg)
    30.7 -  apply (rule add_nonneg_nonneg)
    30.8    apply auto
    30.9    apply (rule_tac x = "%n. if (abs (f n)) <  abs (g n) then x n else 0" 
   30.10       in exI)
   30.11 @@ -150,11 +149,8 @@
   30.12    apply (rule abs_triangle_ineq)
   30.13    apply (simp add: order_less_le)
   30.14    apply (rule mult_nonneg_nonneg)
   30.15 -  apply (rule add_nonneg_nonneg)
   30.16 -  apply (erule order_less_imp_le)+
   30.17 +  apply (erule order_less_imp_le)
   30.18    apply simp
   30.19 -  apply (rule ext)
   30.20 -  apply (auto simp add: if_splits linorder_not_le)
   30.21    done
   30.22  
   30.23  lemma bigo_plus_subset2 [intro]: "A <= O(f) ==> B <= O(f) ==> A \<oplus> B <= O(f)"
    31.1 --- a/src/HOL/Library/Binomial.thy	Fri Mar 23 20:32:43 2012 +0100
    31.2 +++ b/src/HOL/Library/Binomial.thy	Mon Mar 26 10:56:56 2012 +0200
    31.3 @@ -350,7 +350,7 @@
    31.4      have eq: "(- (1\<Colon>'a)) ^ n = setprod (\<lambda>i. - 1) {0 .. n - 1}"
    31.5        by auto
    31.6      from n0 have ?thesis 
    31.7 -      by (simp add: pochhammer_def gbinomial_def field_simps eq setprod_timesf[symmetric])}
    31.8 +      by (simp add: pochhammer_def gbinomial_def field_simps eq setprod_timesf[symmetric] del: minus_one) (* FIXME: del: minus_one *)}
    31.9    ultimately show ?thesis by blast
   31.10  qed
   31.11  
   31.12 @@ -417,8 +417,8 @@
   31.13      from eq[symmetric]
   31.14      have ?thesis using kn
   31.15        apply (simp add: binomial_fact[OF kn, where ?'a = 'a] 
   31.16 -        gbinomial_pochhammer field_simps pochhammer_Suc_setprod)
   31.17 -      apply (simp add: pochhammer_Suc_setprod fact_altdef_nat h of_nat_setprod setprod_timesf[symmetric] eq' del: One_nat_def power_Suc)
   31.18 +        gbinomial_pochhammer field_simps pochhammer_Suc_setprod del: minus_one)
   31.19 +      apply (simp add: pochhammer_Suc_setprod fact_altdef_nat h of_nat_setprod setprod_timesf[symmetric] eq' del: One_nat_def power_Suc del: minus_one)
   31.20        unfolding setprod_Un_disjoint[OF th0, unfolded eq3, of "of_nat:: nat \<Rightarrow> 'a"] eq[unfolded h]
   31.21        unfolding mult_assoc[symmetric] 
   31.22        unfolding setprod_timesf[symmetric]
    32.1 --- a/src/HOL/Library/Bit.thy	Fri Mar 23 20:32:43 2012 +0100
    32.2 +++ b/src/HOL/Library/Bit.thy	Mon Mar 26 10:56:56 2012 +0200
    32.3 @@ -96,27 +96,18 @@
    32.4  
    32.5  subsection {* Numerals at type @{typ bit} *}
    32.6  
    32.7 -instantiation bit :: number_ring
    32.8 -begin
    32.9 -
   32.10 -definition number_of_bit_def:
   32.11 -  "(number_of w :: bit) = of_int w"
   32.12 -
   32.13 -instance proof
   32.14 -qed (rule number_of_bit_def)
   32.15 -
   32.16 -end
   32.17 -
   32.18  text {* All numerals reduce to either 0 or 1. *}
   32.19  
   32.20  lemma bit_minus1 [simp]: "-1 = (1 :: bit)"
   32.21 -  by (simp only: number_of_Min uminus_bit_def)
   32.22 +  by (simp only: minus_one [symmetric] uminus_bit_def)
   32.23  
   32.24 -lemma bit_number_of_even [simp]: "number_of (Int.Bit0 w) = (0 :: bit)"
   32.25 -  by (simp only: number_of_Bit0 add_0_left bit_add_self)
   32.26 +lemma bit_neg_numeral [simp]: "(neg_numeral w :: bit) = numeral w"
   32.27 +  by (simp only: neg_numeral_def uminus_bit_def)
   32.28  
   32.29 -lemma bit_number_of_odd [simp]: "number_of (Int.Bit1 w) = (1 :: bit)"
   32.30 -  by (simp only: number_of_Bit1 add_assoc bit_add_self
   32.31 -                 monoid_add_class.add_0_right)
   32.32 +lemma bit_numeral_even [simp]: "numeral (Num.Bit0 w) = (0 :: bit)"
   32.33 +  by (simp only: numeral_Bit0 bit_add_self)
   32.34 +
   32.35 +lemma bit_numeral_odd [simp]: "numeral (Num.Bit1 w) = (1 :: bit)"
   32.36 +  by (simp only: numeral_Bit1 bit_add_self add_0_left)
   32.37  
   32.38  end
    33.1 --- a/src/HOL/Library/Cardinality.thy	Fri Mar 23 20:32:43 2012 +0100
    33.2 +++ b/src/HOL/Library/Cardinality.thy	Mon Mar 26 10:56:56 2012 +0200
    33.3 @@ -5,7 +5,7 @@
    33.4  header {* Cardinality of types *}
    33.5  
    33.6  theory Cardinality
    33.7 -imports Main
    33.8 +imports "~~/src/HOL/Main"
    33.9  begin
   33.10  
   33.11  subsection {* Preliminary lemmas *}
    34.1 --- a/src/HOL/Library/Code_Integer.thy	Fri Mar 23 20:32:43 2012 +0100
    34.2 +++ b/src/HOL/Library/Code_Integer.thy	Mon Mar 26 10:56:56 2012 +0200
    34.3 @@ -9,6 +9,43 @@
    34.4  begin
    34.5  
    34.6  text {*
    34.7 +  Representation-ignorant code equations for conversions.
    34.8 +*}
    34.9 +
   34.10 +lemma nat_code [code]:
   34.11 +  "nat k = (if k \<le> 0 then 0 else
   34.12 +     let
   34.13 +       (l, j) = divmod_int k 2;
   34.14 +       l' = 2 * nat l
   34.15 +     in if j = 0 then l' else Suc l')"
   34.16 +proof -
   34.17 +  have "2 = nat 2" by simp
   34.18 +  show ?thesis
   34.19 +    apply (auto simp add: Let_def divmod_int_mod_div not_le
   34.20 +     nat_div_distrib nat_mult_distrib mult_div_cancel mod_2_not_eq_zero_eq_one_int)
   34.21 +    apply (unfold `2 = nat 2`)
   34.22 +    apply (subst nat_mod_distrib [symmetric])
   34.23 +    apply simp_all
   34.24 +  done
   34.25 +qed
   34.26 +
   34.27 +lemma (in ring_1) of_int_code:
   34.28 +  "of_int k = (if k = 0 then 0
   34.29 +     else if k < 0 then - of_int (- k)
   34.30 +     else let
   34.31 +       (l, j) = divmod_int k 2;
   34.32 +       l' = 2 * of_int l
   34.33 +     in if j = 0 then l' else l' + 1)"
   34.34 +proof -
   34.35 +  from mod_div_equality have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
   34.36 +  show ?thesis
   34.37 +    by (simp add: Let_def divmod_int_mod_div mod_2_not_eq_zero_eq_one_int
   34.38 +      of_int_add [symmetric]) (simp add: * mult_commute)
   34.39 +qed
   34.40 +
   34.41 +declare of_int_code [code]
   34.42 +
   34.43 +text {*
   34.44    HOL numeral expressions are mapped to integer literals
   34.45    in target languages, using predefined target language
   34.46    operations for abstract integer operations.
   34.47 @@ -24,43 +61,22 @@
   34.48  code_instance int :: equal
   34.49    (Haskell -)
   34.50  
   34.51 +code_const "0::int"
   34.52 +  (SML "0")
   34.53 +  (OCaml "Big'_int.zero'_big'_int")
   34.54 +  (Haskell "0")
   34.55 +  (Scala "BigInt(0)")
   34.56 +
   34.57  setup {*
   34.58 -  fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
   34.59 +  fold (Numeral.add_code @{const_name Int.Pos}
   34.60 +    false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
   34.61 +*}
   34.62 +
   34.63 +setup {*
   34.64 +  fold (Numeral.add_code @{const_name Int.Neg}
   34.65      true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
   34.66  *}
   34.67  
   34.68 -code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
   34.69 -  (SML "raise/ Fail/ \"Pls\""
   34.70 -     and "raise/ Fail/ \"Min\""
   34.71 -     and "!((_);/ raise/ Fail/ \"Bit0\")"
   34.72 -     and "!((_);/ raise/ Fail/ \"Bit1\")")
   34.73 -  (OCaml "failwith/ \"Pls\""
   34.74 -     and "failwith/ \"Min\""
   34.75 -     and "!((_);/ failwith/ \"Bit0\")"
   34.76 -     and "!((_);/ failwith/ \"Bit1\")")
   34.77 -  (Haskell "error/ \"Pls\""
   34.78 -     and "error/ \"Min\""
   34.79 -     and "error/ \"Bit0\""
   34.80 -     and "error/ \"Bit1\"")
   34.81 -  (Scala "!error(\"Pls\")"
   34.82 -     and "!error(\"Min\")"
   34.83 -     and "!error(\"Bit0\")"
   34.84 -     and "!error(\"Bit1\")")
   34.85 -
   34.86 -code_const Int.pred
   34.87 -  (SML "IntInf.- ((_), 1)")
   34.88 -  (OCaml "Big'_int.pred'_big'_int")
   34.89 -  (Haskell "!(_/ -/ 1)")
   34.90 -  (Scala "!(_ -/ 1)")
   34.91 -  (Eval "!(_/ -/ 1)")
   34.92 -
   34.93 -code_const Int.succ
   34.94 -  (SML "IntInf.+ ((_), 1)")
   34.95 -  (OCaml "Big'_int.succ'_big'_int")
   34.96 -  (Haskell "!(_/ +/ 1)")
   34.97 -  (Scala "!(_ +/ 1)")
   34.98 -  (Eval "!(_/ +/ 1)")
   34.99 -
  34.100  code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
  34.101    (SML "IntInf.+ ((_), (_))")
  34.102    (OCaml "Big'_int.add'_big'_int")
  34.103 @@ -82,6 +98,19 @@
  34.104    (Scala infixl 7 "-")
  34.105    (Eval infixl 8 "-")
  34.106  
  34.107 +code_const Int.dup
  34.108 +  (SML "IntInf.*/ (2,/ (_))")
  34.109 +  (OCaml "Big'_int.mult'_big'_int/ 2")
  34.110 +  (Haskell "!(2 * _)")
  34.111 +  (Scala "!(2 * _)")
  34.112 +  (Eval "!(2 * _)")
  34.113 +
  34.114 +code_const Int.sub
  34.115 +  (SML "!(raise/ Fail/ \"sub\")")
  34.116 +  (OCaml "failwith/ \"sub\"")
  34.117 +  (Haskell "error/ \"sub\"")
  34.118 +  (Scala "!error(\"sub\")")
  34.119 +
  34.120  code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
  34.121    (SML "IntInf.* ((_), (_))")
  34.122    (OCaml "Big'_int.mult'_big'_int")
  34.123 @@ -124,9 +153,7 @@
  34.124    (Scala "!_.as'_BigInt")
  34.125    (Eval "_")
  34.126  
  34.127 -text {* Evaluation *}
  34.128 -
  34.129  code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
  34.130    (Eval "HOLogic.mk'_number/ HOLogic.intT")
  34.131  
  34.132 -end
  34.133 \ No newline at end of file
  34.134 +end
    35.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.2 +++ b/src/HOL/Library/Code_Nat.thy	Mon Mar 26 10:56:56 2012 +0200
    35.3 @@ -0,0 +1,258 @@
    35.4 +(*  Title:      HOL/Library/Code_Nat.thy
    35.5 +    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
    35.6 +*)
    35.7 +
    35.8 +header {* Implementation of natural numbers as binary numerals *}
    35.9 +
   35.10 +theory Code_Nat
   35.11 +imports Main
   35.12 +begin
   35.13 +
   35.14 +text {*
   35.15 +  When generating code for functions on natural numbers, the
   35.16 +  canonical representation using @{term "0::nat"} and
   35.17 +  @{term Suc} is unsuitable for computations involving large
   35.18 +  numbers.  This theory refines the representation of
   35.19 +  natural numbers for code generation to use binary
   35.20 +  numerals, which do not grow linear in size but logarithmic.
   35.21 +*}
   35.22 +
   35.23 +subsection {* Representation *}
   35.24 +
   35.25 +lemma [code_abbrev]:
   35.26 +  "nat_of_num = numeral"
   35.27 +  by (fact nat_of_num_numeral)
   35.28 +
   35.29 +code_datatype "0::nat" nat_of_num
   35.30 +
   35.31 +lemma [code]:
   35.32 +  "num_of_nat 0 = Num.One"
   35.33 +  "num_of_nat (nat_of_num k) = k"
   35.34 +  by (simp_all add: nat_of_num_inverse)
   35.35 +
   35.36 +lemma [code]:
   35.37 +  "(1\<Colon>nat) = Numeral1"
   35.38 +  by simp
   35.39 +
   35.40 +lemma [code_abbrev]: "Numeral1 = (1\<Colon>nat)"
   35.41 +  by simp
   35.42 +
   35.43 +lemma [code]:
   35.44 +  "Suc n = n + 1"
   35.45 +  by simp
   35.46 +
   35.47 +
   35.48 +subsection {* Basic arithmetic *}
   35.49 +
   35.50 +lemma [code, code del]:
   35.51 +  "(plus :: nat \<Rightarrow> _) = plus" ..
   35.52 +
   35.53 +lemma plus_nat_code [code]:
   35.54 +  "nat_of_num k + nat_of_num l = nat_of_num (k + l)"
   35.55 +  "m + 0 = (m::nat)"
   35.56 +  "0 + n = (n::nat)"
   35.57 +  by (simp_all add: nat_of_num_numeral)
   35.58 +
   35.59 +text {* Bounded subtraction needs some auxiliary *}
   35.60 +
   35.61 +definition dup :: "nat \<Rightarrow> nat" where
   35.62 +  "dup n = n + n"
   35.63 +
   35.64 +lemma dup_code [code]:
   35.65 +  "dup 0 = 0"
   35.66 +  "dup (nat_of_num k) = nat_of_num (Num.Bit0 k)"
   35.67 +  unfolding Num_def by (simp_all add: dup_def numeral_Bit0)
   35.68 +
   35.69 +definition sub :: "num \<Rightarrow> num \<Rightarrow> nat option" where
   35.70 +  "sub k l = (if k \<ge> l then Some (numeral k - numeral l) else None)"
   35.71 +
   35.72 +lemma sub_code [code]:
   35.73 +  "sub Num.One Num.One = Some 0"
   35.74 +  "sub (Num.Bit0 m) Num.One = Some (nat_of_num (Num.BitM m))"
   35.75 +  "sub (Num.Bit1 m) Num.One = Some (nat_of_num (Num.Bit0 m))"
   35.76 +  "sub Num.One (Num.Bit0 n) = None"
   35.77 +  "sub Num.One (Num.Bit1 n) = None"
   35.78 +  "sub (Num.Bit0 m) (Num.Bit0 n) = Option.map dup (sub m n)"
   35.79 +  "sub (Num.Bit1 m) (Num.Bit1 n) = Option.map dup (sub m n)"
   35.80 +  "sub (Num.Bit1 m) (Num.Bit0 n) = Option.map (\<lambda>q. dup q + 1) (sub m n)"
   35.81 +  "sub (Num.Bit0 m) (Num.Bit1 n) = (case sub m n of None \<Rightarrow> None
   35.82 +     | Some q \<Rightarrow> if q = 0 then None else Some (dup q - 1))"
   35.83 +  apply (auto simp add: nat_of_num_numeral
   35.84 +    Num.dbl_def Num.dbl_inc_def Num.dbl_dec_def
   35.85 +    Let_def le_imp_diff_is_add BitM_plus_one sub_def dup_def)
   35.86 +  apply (simp_all add: sub_non_positive)
   35.87 +  apply (simp_all add: sub_non_negative [symmetric, where ?'a = int])
   35.88 +  done
   35.89 +
   35.90 +lemma [code, code del]:
   35.91 +  "(minus :: nat \<Rightarrow> _) = minus" ..
   35.92 +
   35.93 +lemma minus_nat_code [code]:
   35.94 +  "nat_of_num k - nat_of_num l = (case sub k l of None \<Rightarrow> 0 | Some j \<Rightarrow> j)"
   35.95 +  "m - 0 = (m::nat)"
   35.96 +  "0 - n = (0::nat)"
   35.97 +  by (simp_all add: nat_of_num_numeral sub_non_positive sub_def)
   35.98 +
   35.99 +lemma [code, code del]:
  35.100 +  "(times :: nat \<Rightarrow> _) = times" ..
  35.101 +
  35.102 +lemma times_nat_code [code]:
  35.103 +  "nat_of_num k * nat_of_num l = nat_of_num (k * l)"
  35.104 +  "m * 0 = (0::nat)"
  35.105 +  "0 * n = (0::nat)"
  35.106 +  by (simp_all add: nat_of_num_numeral)
  35.107 +
  35.108 +lemma [code, code del]:
  35.109 +  "(HOL.equal :: nat \<Rightarrow> _) = HOL.equal" ..
  35.110 +
  35.111 +lemma equal_nat_code [code]:
  35.112 +  "HOL.equal 0 (0::nat) \<longleftrightarrow> True"
  35.113 +  "HOL.equal 0 (nat_of_num l) \<longleftrightarrow> False"
  35.114 +  "HOL.equal (nat_of_num k) 0 \<longleftrightarrow> False"
  35.115 +  "HOL.equal (nat_of_num k) (nat_of_num l) \<longleftrightarrow> HOL.equal k l"
  35.116 +  by (simp_all add: nat_of_num_numeral equal)
  35.117 +
  35.118 +lemma equal_nat_refl [code nbe]:
  35.119 +  "HOL.equal (n::nat) n \<longleftrightarrow> True"
  35.120 +  by (rule equal_refl)
  35.121 +
  35.122 +lemma [code, code del]:
  35.123 +  "(less_eq :: nat \<Rightarrow> _) = less_eq" ..
  35.124 +
  35.125 +lemma less_eq_nat_code [code]:
  35.126 +  "0 \<le> (n::nat) \<longleftrightarrow> True"
  35.127 +  "nat_of_num k \<le> 0 \<longleftrightarrow> False"
  35.128 +  "nat_of_num k \<le> nat_of_num l \<longleftrightarrow> k \<le> l"
  35.129 +  by (simp_all add: nat_of_num_numeral)
  35.130 +
  35.131 +lemma [code, code del]:
  35.132 +  "(less :: nat \<Rightarrow> _) = less" ..
  35.133 +
  35.134 +lemma less_nat_code [code]:
  35.135 +  "(m::nat) < 0 \<longleftrightarrow> False"
  35.136 +  "0 < nat_of_num l \<longleftrightarrow> True"
  35.137 +  "nat_of_num k < nat_of_num l \<longleftrightarrow> k < l"
  35.138 +  by (simp_all add: nat_of_num_numeral)
  35.139 +
  35.140 +
  35.141 +subsection {* Conversions *}
  35.142 +
  35.143 +lemma [code, code del]:
  35.144 +  "of_nat = of_nat" ..
  35.145 +
  35.146 +lemma of_nat_code [code]:
  35.147 +  "of_nat 0 = 0"
  35.148 +  "of_nat (nat_of_num k) = numeral k"
  35.149 +  by (simp_all add: nat_of_num_numeral)
  35.150 +
  35.151 +
  35.152 +subsection {* Case analysis *}
  35.153 +
  35.154 +text {*
  35.155 +  Case analysis on natural numbers is rephrased using a conditional
  35.156 +  expression:
  35.157 +*}
  35.158 +
  35.159 +lemma [code, code_unfold]:
  35.160 +  "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
  35.161 +  by (auto simp add: fun_eq_iff dest!: gr0_implies_Suc)
  35.162 +
  35.163 +
  35.164 +subsection {* Preprocessors *}
  35.165 +
  35.166 +text {*
  35.167 +  The term @{term "Suc n"} is no longer a valid pattern.
  35.168 +  Therefore, all occurrences of this term in a position
  35.169 +  where a pattern is expected (i.e.~on the left-hand side of a recursion
  35.170 +  equation) must be eliminated.
  35.171 +  This can be accomplished by applying the following transformation rules:
  35.172 +*}
  35.173 +
  35.174 +lemma Suc_if_eq: "(\<And>n. f (Suc n) \<equiv> h n) \<Longrightarrow> f 0 \<equiv> g \<Longrightarrow>
  35.175 +  f n \<equiv> if n = 0 then g else h (n - 1)"
  35.176 +  by (rule eq_reflection) (cases n, simp_all)
  35.177 +
  35.178 +text {*
  35.179 +  The rules above are built into a preprocessor that is plugged into
  35.180 +  the code generator. Since the preprocessor for introduction rules
  35.181 +  does not know anything about modes, some of the modes that worked
  35.182 +  for the canonical representation of natural numbers may no longer work.
  35.183 +*}
  35.184 +
  35.185 +(*<*)
  35.186 +setup {*
  35.187 +let
  35.188 +
  35.189 +fun remove_suc thy thms =
  35.190 +  let
  35.191 +    val vname = singleton (Name.variant_list (map fst
  35.192 +      (fold (Term.add_var_names o Thm.full_prop_of) thms []))) "n";
  35.193 +    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
  35.194 +    fun lhs_of th = snd (Thm.dest_comb
  35.195 +      (fst (Thm.dest_comb (cprop_of th))));
  35.196 +    fun rhs_of th = snd (Thm.dest_comb (cprop_of th));
  35.197 +    fun find_vars ct = (case term_of ct of
  35.198 +        (Const (@{const_name Suc}, _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
  35.199 +      | _ $ _ =>
  35.200 +        let val (ct1, ct2) = Thm.dest_comb ct
  35.201 +        in 
  35.202 +          map (apfst (fn ct => Thm.apply ct ct2)) (find_vars ct1) @
  35.203 +          map (apfst (Thm.apply ct1)) (find_vars ct2)
  35.204 +        end
  35.205 +      | _ => []);
  35.206 +    val eqs = maps
  35.207 +      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
  35.208 +    fun mk_thms (th, (ct, cv')) =
  35.209 +      let
  35.210 +        val th' =
  35.211 +          Thm.implies_elim
  35.212 +           (Conv.fconv_rule (Thm.beta_conversion true)
  35.213 +             (Drule.instantiate'
  35.214 +               [SOME (ctyp_of_term ct)] [SOME (Thm.lambda cv ct),
  35.215 +                 SOME (Thm.lambda cv' (rhs_of th)), NONE, SOME cv']
  35.216 +               @{thm Suc_if_eq})) (Thm.forall_intr cv' th)
  35.217 +      in
  35.218 +        case map_filter (fn th'' =>
  35.219 +            SOME (th'', singleton
  35.220 +              (Variable.trade (K (fn [th'''] => [th''' RS th']))
  35.221 +                (Variable.global_thm_context th'')) th'')
  35.222 +          handle THM _ => NONE) thms of
  35.223 +            [] => NONE
  35.224 +          | thps =>
  35.225 +              let val (ths1, ths2) = split_list thps
  35.226 +              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
  35.227 +      end
  35.228 +  in get_first mk_thms eqs end;
  35.229 +
  35.230 +fun eqn_suc_base_preproc thy thms =
  35.231 +  let
  35.232 +    val dest = fst o Logic.dest_equals o prop_of;
  35.233 +    val contains_suc = exists_Const (fn (c, _) => c = @{const_name Suc});
  35.234 +  in
  35.235 +    if forall (can dest) thms andalso exists (contains_suc o dest) thms
  35.236 +      then thms |> perhaps_loop (remove_suc thy) |> (Option.map o map) Drule.zero_var_indexes
  35.237 +       else NONE
  35.238 +  end;
  35.239 +
  35.240 +val eqn_suc_preproc = Code_Preproc.simple_functrans eqn_suc_base_preproc;
  35.241 +
  35.242 +in
  35.243 +
  35.244 +  Code_Preproc.add_functrans ("eqn_Suc", eqn_suc_preproc)
  35.245 +
  35.246 +end;
  35.247 +*}
  35.248 +(*>*)
  35.249 +
  35.250 +code_modulename SML
  35.251 +  Code_Nat Arith
  35.252 +
  35.253 +code_modulename OCaml
  35.254 +  Code_Nat Arith
  35.255 +
  35.256 +code_modulename Haskell
  35.257 +  Code_Nat Arith
  35.258 +
  35.259 +hide_const (open) dup sub
  35.260 +
  35.261 +end
    36.1 --- a/src/HOL/Library/Code_Natural.thy	Fri Mar 23 20:32:43 2012 +0100
    36.2 +++ b/src/HOL/Library/Code_Natural.thy	Mon Mar 26 10:56:56 2012 +0200
    36.3 @@ -106,22 +106,26 @@
    36.4    (Scala "Natural")
    36.5  
    36.6  setup {*
    36.7 -  fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
    36.8 +  fold (Numeral.add_code @{const_name Code_Numeral.Num}
    36.9      false Code_Printer.literal_alternative_numeral) ["Haskell", "Scala"]
   36.10  *}
   36.11  
   36.12  code_instance code_numeral :: equal
   36.13    (Haskell -)
   36.14  
   36.15 -code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   36.16 +code_const "0::code_numeral"
   36.17 +  (Haskell "0")
   36.18 +  (Scala "Natural(0)")
   36.19 +
   36.20 +code_const "plus \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   36.21    (Haskell infixl 6 "+")
   36.22    (Scala infixl 7 "+")
   36.23  
   36.24 -code_const "op - \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   36.25 +code_const "minus \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   36.26    (Haskell infixl 6 "-")
   36.27    (Scala infixl 7 "-")
   36.28  
   36.29 -code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   36.30 +code_const "times \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   36.31    (Haskell infixl 7 "*")
   36.32    (Scala infixl 8 "*")
   36.33  
   36.34 @@ -133,11 +137,11 @@
   36.35    (Haskell infix 4 "==")
   36.36    (Scala infixl 5 "==")
   36.37  
   36.38 -code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   36.39 +code_const "less_eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   36.40    (Haskell infix 4 "<=")
   36.41    (Scala infixl 4 "<=")
   36.42  
   36.43 -code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   36.44 +code_const "less \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   36.45    (Haskell infix 4 "<")
   36.46    (Scala infixl 4 "<")
   36.47  
    37.1 --- a/src/HOL/Library/Code_Prolog.thy	Fri Mar 23 20:32:43 2012 +0100
    37.2 +++ b/src/HOL/Library/Code_Prolog.thy	Mon Mar 26 10:56:56 2012 +0200
    37.3 @@ -11,8 +11,10 @@
    37.4  
    37.5  section {* Setup for Numerals *}
    37.6  
    37.7 -setup {* Predicate_Compile_Data.ignore_consts [@{const_name number_of}] *}
    37.8 -setup {* Predicate_Compile_Data.keep_functions [@{const_name number_of}] *}
    37.9 +setup {* Predicate_Compile_Data.ignore_consts
   37.10 +  [@{const_name numeral}, @{const_name neg_numeral}] *}
   37.11 +
   37.12 +setup {* Predicate_Compile_Data.keep_functions
   37.13 +  [@{const_name numeral}, @{const_name neg_numeral}] *}
   37.14  
   37.15  end
   37.16 -
    38.1 --- a/src/HOL/Library/Code_Real_Approx_By_Float.thy	Fri Mar 23 20:32:43 2012 +0100
    38.2 +++ b/src/HOL/Library/Code_Real_Approx_By_Float.thy	Mon Mar 26 10:56:56 2012 +0200
    38.3 @@ -129,10 +129,24 @@
    38.4  lemma of_int_eq_real_of_int[code_unfold]: "of_int = real_of_int"
    38.5    unfolding real_of_int_def ..
    38.6  
    38.7 +lemma [code_unfold del]:
    38.8 +  "0 \<equiv> (of_rat 0 :: real)"
    38.9 +  by simp
   38.10 +
   38.11 +lemma [code_unfold del]:
   38.12 +  "1 \<equiv> (of_rat 1 :: real)"
   38.13 +  by simp
   38.14 +
   38.15 +lemma [code_unfold del]:
   38.16 +  "numeral k \<equiv> (of_rat (numeral k) :: real)"
   38.17 +  by simp
   38.18 +
   38.19 +lemma [code_unfold del]:
   38.20 +  "neg_numeral k \<equiv> (of_rat (neg_numeral k) :: real)"
   38.21 +  by simp
   38.22 +
   38.23  hide_const (open) real_of_int
   38.24  
   38.25 -declare number_of_real_code [code_unfold del]
   38.26 -
   38.27  notepad
   38.28  begin
   38.29    have "cos (pi/2) = 0" by (rule cos_pi_half)
    39.1 --- a/src/HOL/Library/Efficient_Nat.thy	Fri Mar 23 20:32:43 2012 +0100
    39.2 +++ b/src/HOL/Library/Efficient_Nat.thy	Mon Mar 26 10:56:56 2012 +0200
    39.3 @@ -5,175 +5,16 @@
    39.4  header {* Implementation of natural numbers by target-language integers *}
    39.5  
    39.6  theory Efficient_Nat
    39.7 -imports Code_Integer Main
    39.8 +imports Code_Nat Code_Integer Main
    39.9  begin
   39.10  
   39.11  text {*
   39.12 -  When generating code for functions on natural numbers, the
   39.13 -  canonical representation using @{term "0::nat"} and
   39.14 -  @{term Suc} is unsuitable for computations involving large
   39.15 -  numbers.  The efficiency of the generated code can be improved
   39.16 +  The efficiency of the generated code for natural numbers can be improved
   39.17    drastically by implementing natural numbers by target-language
   39.18    integers.  To do this, just include this theory.
   39.19  *}
   39.20  
   39.21 -subsection {* Basic arithmetic *}
   39.22 -
   39.23 -text {*
   39.24 -  Most standard arithmetic functions on natural numbers are implemented
   39.25 -  using their counterparts on the integers:
   39.26 -*}
   39.27 -
   39.28 -code_datatype number_nat_inst.number_of_nat
   39.29 -
   39.30 -lemma zero_nat_code [code, code_unfold]:
   39.31 -  "0 = (Numeral0 :: nat)"
   39.32 -  by simp
   39.33 -
   39.34 -lemma one_nat_code [code, code_unfold]:
   39.35 -  "1 = (Numeral1 :: nat)"
   39.36 -  by simp
   39.37 -
   39.38 -lemma Suc_code [code]:
   39.39 -  "Suc n = n + 1"
   39.40 -  by simp
   39.41 -
   39.42 -lemma plus_nat_code [code]:
   39.43 -  "n + m = nat (of_nat n + of_nat m)"
   39.44 -  by simp
   39.45 -
   39.46 -lemma minus_nat_code [code]:
   39.47 -  "n - m = nat (of_nat n - of_nat m)"
   39.48 -  by simp
   39.49 -
   39.50 -lemma times_nat_code [code]:
   39.51 -  "n * m = nat (of_nat n * of_nat m)"
   39.52 -  unfolding of_nat_mult [symmetric] by simp
   39.53 -
   39.54 -lemma divmod_nat_code [code]:
   39.55 -  "divmod_nat n m = map_pair nat nat (pdivmod (of_nat n) (of_nat m))"
   39.56 -  by (simp add: map_pair_def split_def pdivmod_def nat_div_distrib nat_mod_distrib divmod_nat_div_mod)
   39.57 -
   39.58 -lemma eq_nat_code [code]:
   39.59 -  "HOL.equal n m \<longleftrightarrow> HOL.equal (of_nat n \<Colon> int) (of_nat m)"
   39.60 -  by (simp add: equal)
   39.61 -
   39.62 -lemma eq_nat_refl [code nbe]:
   39.63 -  "HOL.equal (n::nat) n \<longleftrightarrow> True"
   39.64 -  by (rule equal_refl)
   39.65 -
   39.66 -lemma less_eq_nat_code [code]:
   39.67 -  "n \<le> m \<longleftrightarrow> (of_nat n \<Colon> int) \<le> of_nat m"
   39.68 -  by simp
   39.69 -
   39.70 -lemma less_nat_code [code]:
   39.71 -  "n < m \<longleftrightarrow> (of_nat n \<Colon> int) < of_nat m"
   39.72 -  by simp
   39.73 -
   39.74 -subsection {* Case analysis *}
   39.75 -
   39.76 -text {*
   39.77 -  Case analysis on natural numbers is rephrased using a conditional
   39.78 -  expression:
   39.79 -*}
   39.80 -
   39.81 -lemma [code, code_unfold]:
   39.82 -  "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
   39.83 -  by (auto simp add: fun_eq_iff dest!: gr0_implies_Suc)
   39.84 -
   39.85 -
   39.86 -subsection {* Preprocessors *}
   39.87 -
   39.88 -text {*
   39.89 -  In contrast to @{term "Suc n"}, the term @{term "n + (1::nat)"} is no longer
   39.90 -  a constructor term. Therefore, all occurrences of this term in a position
   39.91 -  where a pattern is expected (i.e.\ on the left-hand side of a recursion
   39.92 -  equation or in the arguments of an inductive relation in an introduction
   39.93 -  rule) must be eliminated.
   39.94 -  This can be accomplished by applying the following transformation rules:
   39.95 -*}
   39.96 -
   39.97 -lemma Suc_if_eq: "(\<And>n. f (Suc n) \<equiv> h n) \<Longrightarrow> f 0 \<equiv> g \<Longrightarrow>
   39.98 -  f n \<equiv> if n = 0 then g else h (n - 1)"
   39.99 -  by (rule eq_reflection) (cases n, simp_all)
  39.100 -
  39.101 -lemma Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
  39.102 -  by (cases n) simp_all
  39.103 -
  39.104 -text {*
  39.105 -  The rules above are built into a preprocessor that is plugged into
  39.106 -  the code generator. Since the preprocessor for introduction rules
  39.107 -  does not know anything about modes, some of the modes that worked
  39.108 -  for the canonical representation of natural numbers may no longer work.
  39.109 -*}
  39.110 -
  39.111 -(*<*)
  39.112 -setup {*
  39.113 -let
  39.114 -
  39.115 -fun remove_suc thy thms =
  39.116 -  let
  39.117 -    val vname = singleton (Name.variant_list (map fst
  39.118 -      (fold (Term.add_var_names o Thm.full_prop_of) thms []))) "n";
  39.119 -    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
  39.120 -    fun lhs_of th = snd (Thm.dest_comb
  39.121 -      (fst (Thm.dest_comb (cprop_of th))));
  39.122 -    fun rhs_of th = snd (Thm.dest_comb (cprop_of th));
  39.123 -    fun find_vars ct = (case term_of ct of
  39.124 -        (Const (@{const_name Suc}, _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
  39.125 -      | _ $ _ =>
  39.126 -        let val (ct1, ct2) = Thm.dest_comb ct
  39.127 -        in 
  39.128 -          map (apfst (fn ct => Thm.apply ct ct2)) (find_vars ct1) @
  39.129 -          map (apfst (Thm.apply ct1)) (find_vars ct2)
  39.130 -        end
  39.131 -      | _ => []);
  39.132 -    val eqs = maps
  39.133 -      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
  39.134 -    fun mk_thms (th, (ct, cv')) =
  39.135 -      let
  39.136 -        val th' =
  39.137 -          Thm.implies_elim
  39.138 -           (Conv.fconv_rule (Thm.beta_conversion true)
  39.139 -             (Drule.instantiate'
  39.140 -               [SOME (ctyp_of_term ct)] [SOME (Thm.lambda cv ct),
  39.141 -                 SOME (Thm.lambda cv' (rhs_of th)), NONE, SOME cv']
  39.142 -               @{thm Suc_if_eq})) (Thm.forall_intr cv' th)
  39.143 -      in
  39.144 -        case map_filter (fn th'' =>
  39.145 -            SOME (th'', singleton
  39.146 -              (Variable.trade (K (fn [th'''] => [th''' RS th']))
  39.147 -                (Variable.global_thm_context th'')) th'')
  39.148 -          handle THM _ => NONE) thms of
  39.149 -            [] => NONE
  39.150 -          | thps =>
  39.151 -              let val (ths1, ths2) = split_list thps
  39.152 -              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
  39.153 -      end
  39.154 -  in get_first mk_thms eqs end;
  39.155 -
  39.156 -fun eqn_suc_base_preproc thy thms =
  39.157 -  let
  39.158 -    val dest = fst o Logic.dest_equals o prop_of;
  39.159 -    val contains_suc = exists_Const (fn (c, _) => c = @{const_name Suc});
  39.160 -  in
  39.161 -    if forall (can dest) thms andalso exists (contains_suc o dest) thms
  39.162 -      then thms |> perhaps_loop (remove_suc thy) |> (Option.map o map) Drule.zero_var_indexes
  39.163 -       else NONE
  39.164 -  end;
  39.165 -
  39.166 -val eqn_suc_preproc = Code_Preproc.simple_functrans eqn_suc_base_preproc;
  39.167 -
  39.168 -in
  39.169 -
  39.170 -  Code_Preproc.add_functrans ("eqn_Suc", eqn_suc_preproc)
  39.171 -
  39.172 -end;
  39.173 -*}
  39.174 -(*>*)
  39.175 -
  39.176 -
  39.177 -subsection {* Target language setup *}
  39.178 +subsection {* Target language fundamentals *}
  39.179  
  39.180  text {*
  39.181    For ML, we map @{typ nat} to target language integers, where we
  39.182 @@ -282,47 +123,32 @@
  39.183  code_instance nat :: equal
  39.184    (Haskell -)
  39.185  
  39.186 -text {*
  39.187 -  Natural numerals.
  39.188 -*}
  39.189 -
  39.190 -lemma [code_abbrev]:
  39.191 -  "number_nat_inst.number_of_nat i = nat (number_of i)"
  39.192 -  -- {* this interacts as desired with @{thm nat_number_of_def} *}
  39.193 -  by (simp add: number_nat_inst.number_of_nat)
  39.194 -
  39.195  setup {*
  39.196 -  fold (Numeral.add_code @{const_name number_nat_inst.number_of_nat}
  39.197 +  fold (Numeral.add_code @{const_name nat_of_num}
  39.198      false Code_Printer.literal_positive_numeral) ["SML", "OCaml", "Haskell", "Scala"]
  39.199  *}
  39.200  
  39.201 +code_const "0::nat"
  39.202 +  (SML "0")
  39.203 +  (OCaml "Big'_int.zero'_big'_int")
  39.204 +  (Haskell "0")
  39.205 +  (Scala "Nat(0)")
  39.206 +
  39.207 +
  39.208 +subsection {* Conversions *}
  39.209 +
  39.210  text {*
  39.211    Since natural numbers are implemented
  39.212 -  using integers in ML, the coercion function @{const "of_nat"} of type
  39.213 +  using integers in ML, the coercion function @{term "int"} of type
  39.214    @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function.
  39.215    For the @{const nat} function for converting an integer to a natural
  39.216 -  number, we give a specific implementation using an ML function that
  39.217 +  number, we give a specific implementation using an ML expression that
  39.218    returns its input value, provided that it is non-negative, and otherwise
  39.219    returns @{text "0"}.
  39.220  *}
  39.221  
  39.222  definition int :: "nat \<Rightarrow> int" where
  39.223 -  [code del, code_abbrev]: "int = of_nat"
  39.224 -
  39.225 -lemma int_code' [code]:
  39.226 -  "int (number_of l) = (if neg (number_of l \<Colon> int) then 0 else number_of l)"
  39.227 -  unfolding int_nat_number_of [folded int_def] ..
  39.228 -
  39.229 -lemma nat_code' [code]:
  39.230 -  "nat (number_of l) = (if neg (number_of l \<Colon> int) then 0 else number_of l)"
  39.231 -  unfolding nat_number_of_def number_of_is_id neg_def by simp
  39.232 -
  39.233 -lemma of_nat_int: (* FIXME delete candidate *)
  39.234 -  "of_nat = int" by (simp add: int_def)
  39.235 -
  39.236 -lemma of_nat_aux_int [code_unfold]:
  39.237 -  "of_nat_aux (\<lambda>i. i + 1) k 0 = int k"
  39.238 -  by (simp add: int_def Nat.of_nat_code)
  39.239 +  [code_abbrev]: "int = of_nat"
  39.240  
  39.241  code_const int
  39.242    (SML "_")
  39.243 @@ -331,7 +157,7 @@
  39.244  code_const nat
  39.245    (SML "IntInf.max/ (0,/ _)")
  39.246    (OCaml "Big'_int.max'_big'_int/ Big'_int.zero'_big'_int")
  39.247 -  (Eval "Integer.max/ _/ 0")
  39.248 +  (Eval "Integer.max/ 0")
  39.249  
  39.250  text {* For Haskell and Scala, things are slightly different again. *}
  39.251  
  39.252 @@ -339,7 +165,26 @@
  39.253    (Haskell "toInteger" and "fromInteger")
  39.254    (Scala "!_.as'_BigInt" and "Nat")
  39.255  
  39.256 -text {* Conversion from and to code numerals. *}
  39.257 +text {* Alternativ implementation for @{const of_nat} *}
  39.258 +
  39.259 +lemma [code]:
  39.260 +  "of_nat n = (if n = 0 then 0 else
  39.261 +     let
  39.262 +       (q, m) = divmod_nat n 2;
  39.263 +       q' = 2 * of_nat q
  39.264 +     in if m = 0 then q' else q' + 1)"
  39.265 +proof -
  39.266 +  from mod_div_equality have *: "of_nat n = of_nat (n div 2 * 2 + n mod 2)" by simp
  39.267 +  show ?thesis
  39.268 +    apply (simp add: Let_def divmod_nat_div_mod mod_2_not_eq_zero_eq_one_nat
  39.269 +      of_nat_mult
  39.270 +      of_nat_add [symmetric])
  39.271 +    apply (auto simp add: of_nat_mult)
  39.272 +    apply (simp add: * of_nat_mult add_commute mult_commute)
  39.273 +    done
  39.274 +qed
  39.275 +
  39.276 +text {* Conversion from and to code numerals *}
  39.277  
  39.278  code_const Code_Numeral.of_nat
  39.279    (SML "IntInf.toInt")
  39.280 @@ -355,21 +200,38 @@
  39.281    (Scala "!Nat(_.as'_BigInt)")
  39.282    (Eval "_")
  39.283  
  39.284 -text {* Using target language arithmetic operations whenever appropriate *}
  39.285  
  39.286 -code_const "op + \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
  39.287 -  (SML "IntInf.+ ((_), (_))")
  39.288 +subsection {* Target language arithmetic *}
  39.289 +
  39.290 +code_const "plus \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
  39.291 +  (SML "IntInf.+/ ((_),/ (_))")
  39.292    (OCaml "Big'_int.add'_big'_int")
  39.293    (Haskell infixl 6 "+")
  39.294    (Scala infixl 7 "+")
  39.295    (Eval infixl 8 "+")
  39.296  
  39.297 -code_const "op - \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
  39.298 +code_const "minus \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
  39.299 +  (SML "IntInf.max/ (0, IntInf.-/ ((_),/ (_)))")
  39.300 +  (OCaml "Big'_int.max'_big'_int/ Big'_int.zero'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)")
  39.301    (Haskell infixl 6 "-")
  39.302    (Scala infixl 7 "-")
  39.303 +  (Eval "Integer.max/ 0/ (_ -/ _)")
  39.304  
  39.305 -code_const "op * \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
  39.306 -  (SML "IntInf.* ((_), (_))")
  39.307 +code_const Code_Nat.dup
  39.308 +  (SML "IntInf.*/ (2,/ (_))")
  39.309 +  (OCaml "Big'_int.mult'_big'_int/ 2")
  39.310 +  (Haskell "!(2 * _)")
  39.311 +  (Scala "!(2 * _)")
  39.312 +  (Eval "!(2 * _)")
  39.313 +
  39.314 +code_const Code_Nat.sub
  39.315 +  (SML "!(raise/ Fail/ \"sub\")")
  39.316 +  (OCaml "failwith/ \"sub\"")
  39.317 +  (Haskell "error/ \"sub\"")
  39.318 +  (Scala "!error(\"sub\")")
  39.319 +
  39.320 +code_const "times \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
  39.321 +  (SML "IntInf.*/ ((_),/ (_))")
  39.322    (OCaml "Big'_int.mult'_big'_int")
  39.323    (Haskell infixl 7 "*")
  39.324    (Scala infixl 8 "*")
  39.325 @@ -389,22 +251,28 @@
  39.326    (Scala infixl 5 "==")
  39.327    (Eval infixl 6 "=")
  39.328  
  39.329 -code_const "op \<le> \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
  39.330 -  (SML "IntInf.<= ((_), (_))")
  39.331 +code_const "less_eq \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
  39.332 +  (SML "IntInf.<=/ ((_),/ (_))")
  39.333    (OCaml "Big'_int.le'_big'_int")
  39.334    (Haskell infix 4 "<=")
  39.335    (Scala infixl 4 "<=")
  39.336    (Eval infixl 6 "<=")
  39.337  
  39.338 -code_const "op < \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
  39.339 -  (SML "IntInf.< ((_), (_))")
  39.340 +code_const "less \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
  39.341 +  (SML "IntInf.</ ((_),/ (_))")
  39.342    (OCaml "Big'_int.lt'_big'_int")
  39.343    (Haskell infix 4 "<")
  39.344    (Scala infixl 4 "<")
  39.345    (Eval infixl 6 "<")
  39.346  
  39.347 +code_const Num.num_of_nat
  39.348 +  (SML "!(raise/ Fail/ \"num'_of'_nat\")")
  39.349 +  (OCaml "failwith/ \"num'_of'_nat\"")
  39.350 +  (Haskell "error/ \"num'_of'_nat\"")
  39.351 +  (Scala "!error(\"num'_of'_nat\")")
  39.352  
  39.353 -text {* Evaluation *}
  39.354 +
  39.355 +subsection {* Evaluation *}
  39.356  
  39.357  lemma [code, code del]:
  39.358    "(Code_Evaluation.term_of \<Colon> nat \<Rightarrow> term) = Code_Evaluation.term_of" ..
  39.359 @@ -412,14 +280,14 @@
  39.360  code_const "Code_Evaluation.term_of \<Colon> nat \<Rightarrow> term"
  39.361    (SML "HOLogic.mk'_number/ HOLogic.natT")
  39.362  
  39.363 -text {* Evaluation with @{text "Quickcheck_Narrowing"} does not work, as
  39.364 +text {*
  39.365 +  FIXME -- Evaluation with @{text "Quickcheck_Narrowing"} does not work, as
  39.366    @{text "code_module"} is very aggressive leading to bad Haskell code.
  39.367    Therefore, we simply deactivate the narrowing-based quickcheck from here on.
  39.368  *}
  39.369  
  39.370  declare [[quickcheck_narrowing_active = false]] 
  39.371  
  39.372 -text {* Module names *}
  39.373  
  39.374  code_modulename SML
  39.375    Efficient_Nat Arith
  39.376 @@ -430,6 +298,6 @@
  39.377  code_modulename Haskell
  39.378    Efficient_Nat Arith
  39.379  
  39.380 -hide_const int
  39.381 +hide_const (open) int
  39.382  
  39.383  end
    40.1 --- a/src/HOL/Library/Extended_Nat.thy	Fri Mar 23 20:32:43 2012 +0100
    40.2 +++ b/src/HOL/Library/Extended_Nat.thy	Mon Mar 26 10:56:56 2012 +0200
    40.3 @@ -61,19 +61,17 @@
    40.4  primrec the_enat :: "enat \<Rightarrow> nat"
    40.5    where "the_enat (enat n) = n"
    40.6  
    40.7 +
    40.8  subsection {* Constructors and numbers *}
    40.9  
   40.10 -instantiation enat :: "{zero, one, number}"
   40.11 +instantiation enat :: "{zero, one}"
   40.12  begin
   40.13  
   40.14  definition
   40.15    "0 = enat 0"
   40.16  
   40.17  definition
   40.18 -  [code_unfold]: "1 = enat 1"
   40.19 -
   40.20 -definition
   40.21 -  [code_unfold, code del]: "number_of k = enat (number_of k)"
   40.22 +  "1 = enat 1"
   40.23  
   40.24  instance ..
   40.25  
   40.26 @@ -82,15 +80,12 @@
   40.27  definition eSuc :: "enat \<Rightarrow> enat" where
   40.28    "eSuc i = (case i of enat n \<Rightarrow> enat (Suc n) | \<infinity> \<Rightarrow> \<infinity>)"
   40.29  
   40.30 -lemma enat_0: "enat 0 = 0"
   40.31 +lemma enat_0 [code_post]: "enat 0 = 0"
   40.32    by (simp add: zero_enat_def)
   40.33  
   40.34 -lemma enat_1: "enat 1 = 1"
   40.35 +lemma enat_1 [code_post]: "enat 1 = 1"
   40.36    by (simp add: one_enat_def)
   40.37  
   40.38 -lemma enat_number: "enat (number_of k) = number_of k"
   40.39 -  by (simp add: number_of_enat_def)
   40.40 -
   40.41  lemma one_eSuc: "1 = eSuc 0"
   40.42    by (simp add: zero_enat_def one_enat_def eSuc_def)
   40.43  
   40.44 @@ -100,16 +95,6 @@
   40.45  lemma i0_ne_infinity [simp]: "0 \<noteq> (\<infinity>::enat)"
   40.46    by (simp add: zero_enat_def)
   40.47  
   40.48 -lemma zero_enat_eq [simp]:
   40.49 -  "number_of k = (0\<Colon>enat) \<longleftrightarrow> number_of k = (0\<Colon>nat)"
   40.50 -  "(0\<Colon>enat) = number_of k \<longleftrightarrow> number_of k = (0\<Colon>nat)"
   40.51 -  unfolding zero_enat_def number_of_enat_def by simp_all
   40.52 -
   40.53 -lemma one_enat_eq [simp]:
   40.54 -  "number_of k = (1\<Colon>enat) \<longleftrightarrow> number_of k = (1\<Colon>nat)"
   40.55 -  "(1\<Colon>enat) = number_of k \<longleftrightarrow> number_of k = (1\<Colon>nat)"
   40.56 -  unfolding one_enat_def number_of_enat_def by simp_all
   40.57 -
   40.58  lemma zero_one_enat_neq [simp]:
   40.59    "\<not> 0 = (1\<Colon>enat)"
   40.60    "\<not> 1 = (0\<Colon>enat)"
   40.61 @@ -121,18 +106,9 @@
   40.62  lemma i1_ne_infinity [simp]: "1 \<noteq> (\<infinity>::enat)"
   40.63    by (simp add: one_enat_def)
   40.64  
   40.65 -lemma infinity_ne_number [simp]: "(\<infinity>::enat) \<noteq> number_of k"
   40.66 -  by (simp add: number_of_enat_def)
   40.67 -
   40.68 -lemma number_ne_infinity [simp]: "number_of k \<noteq> (\<infinity>::enat)"
   40.69 -  by (simp add: number_of_enat_def)
   40.70 -
   40.71  lemma eSuc_enat: "eSuc (enat n) = enat (Suc n)"
   40.72    by (simp add: eSuc_def)
   40.73  
   40.74 -lemma eSuc_number_of: "eSuc (number_of k) = enat (Suc (number_of k))"
   40.75 -  by (simp add: eSuc_enat number_of_enat_def)
   40.76 -
   40.77  lemma eSuc_infinity [simp]: "eSuc \<infinity> = \<infinity>"
   40.78    by (simp add: eSuc_def)
   40.79  
   40.80 @@ -145,11 +121,6 @@
   40.81  lemma eSuc_inject [simp]: "eSuc m = eSuc n \<longleftrightarrow> m = n"
   40.82    by (simp add: eSuc_def split: enat.splits)
   40.83  
   40.84 -lemma number_of_enat_inject [simp]:
   40.85 -  "(number_of k \<Colon> enat) = number_of l \<longleftrightarrow> (number_of k \<Colon> nat) = number_of l"
   40.86 -  by (simp add: number_of_enat_def)
   40.87 -
   40.88 -
   40.89  subsection {* Addition *}
   40.90  
   40.91  instantiation enat :: comm_monoid_add
   40.92 @@ -177,16 +148,6 @@
   40.93  
   40.94  end
   40.95  
   40.96 -lemma plus_enat_number [simp]:
   40.97 -  "(number_of k \<Colon> enat) + number_of l = (if k < Int.Pls then number_of l
   40.98 -    else if l < Int.Pls then number_of k else number_of (k + l))"
   40.99 -  unfolding number_of_enat_def plus_enat_simps nat_arith(1) if_distrib [symmetric, of _ enat] ..
  40.100 -
  40.101 -lemma eSuc_number [simp]:
  40.102 -  "eSuc (number_of k) = (if neg (number_of k \<Colon> int) then 1 else number_of (Int.succ k))"
  40.103 -  unfolding eSuc_number_of
  40.104 -  unfolding one_enat_def number_of_enat_def Suc_nat_number_of if_distrib [symmetric] ..
  40.105 -
  40.106  lemma eSuc_plus_1:
  40.107    "eSuc n = n + 1"
  40.108    by (cases n) (simp_all add: eSuc_enat one_enat_def)
  40.109 @@ -261,12 +222,6 @@
  40.110    apply (simp add: plus_1_eSuc eSuc_enat)
  40.111    done
  40.112  
  40.113 -instance enat :: number_semiring
  40.114 -proof
  40.115 -  fix n show "number_of (int n) = (of_nat n :: enat)"
  40.116 -    unfolding number_of_enat_def number_of_int of_nat_id of_nat_eq_enat ..
  40.117 -qed
  40.118 -
  40.119  instance enat :: semiring_char_0 proof
  40.120    have "inj enat" by (rule injI) simp
  40.121    then show "inj (\<lambda>n. of_nat n :: enat)" by (simp add: of_nat_eq_enat)
  40.122 @@ -279,6 +234,25 @@
  40.123    by (auto simp add: times_enat_def zero_enat_def split: enat.split)
  40.124  
  40.125  
  40.126 +subsection {* Numerals *}
  40.127 +
  40.128 +lemma numeral_eq_enat:
  40.129 +  "numeral k = enat (numeral k)"
  40.130 +  using of_nat_eq_enat [of "numeral k"] by simp
  40.131 +
  40.132 +lemma enat_numeral [code_abbrev]:
  40.133 +  "enat (numeral k) = numeral k"
  40.134 +  using numeral_eq_enat ..
  40.135 +
  40.136 +lemma infinity_ne_numeral [simp]: "(\<infinity>::enat) \<noteq> numeral k"
  40.137 +  by (simp add: numeral_eq_enat)
  40.138 +
  40.139 +lemma numeral_ne_infinity [simp]: "numeral k \<noteq> (\<infinity>::enat)"
  40.140 +  by (simp add: numeral_eq_enat)
  40.141 +
  40.142 +lemma eSuc_numeral [simp]: "eSuc (numeral k) = numeral (k + Num.One)"
  40.143 +  by (simp only: eSuc_plus_1 numeral_plus_one)
  40.144 +
  40.145  subsection {* Subtraction *}
  40.146  
  40.147  instantiation enat :: minus
  40.148 @@ -292,13 +266,13 @@
  40.149  
  40.150  end
  40.151  
  40.152 -lemma idiff_enat_enat [simp,code]: "enat a - enat b = enat (a - b)"
  40.153 +lemma idiff_enat_enat [simp, code]: "enat a - enat b = enat (a - b)"
  40.154    by (simp add: diff_enat_def)
  40.155  
  40.156 -lemma idiff_infinity [simp,code]: "\<infinity> - n = (\<infinity>::enat)"
  40.157 +lemma idiff_infinity [simp, code]: "\<infinity> - n = (\<infinity>::enat)"
  40.158    by (simp add: diff_enat_def)
  40.159  
  40.160 -lemma idiff_infinity_right [simp,code]: "enat a - \<infinity> = 0"
  40.161 +lemma idiff_infinity_right [simp, code]: "enat a - \<infinity> = 0"
  40.162    by (simp add: diff_enat_def)
  40.163  
  40.164  lemma idiff_0 [simp]: "(0::enat) - n = 0"
  40.165 @@ -344,13 +318,13 @@
  40.166    "(\<infinity>::enat) < q \<longleftrightarrow> False"
  40.167    by (simp_all add: less_eq_enat_def less_enat_def split: enat.splits)
  40.168  
  40.169 -lemma number_of_le_enat_iff[simp]:
  40.170 -  shows "number_of m \<le> enat n \<longleftrightarrow> number_of m \<le> n"
  40.171 -by (auto simp: number_of_enat_def)
  40.172 +lemma numeral_le_enat_iff[simp]:
  40.173 +  shows "numeral m \<le> enat n \<longleftrightarrow> numeral m \<le> n"
  40.174 +by (auto simp: numeral_eq_enat)
  40.175  
  40.176 -lemma number_of_less_enat_iff[simp]:
  40.177 -  shows "number_of m < enat n \<longleftrightarrow> number_of m < n"
  40.178 -by (auto simp: number_of_enat_def)
  40.179 +lemma numeral_less_enat_iff[simp]:
  40.180 +  shows "numeral m < enat n \<longleftrightarrow> numeral m < n"
  40.181 +by (auto simp: numeral_eq_enat)
  40.182  
  40.183  lemma enat_ord_code [code]:
  40.184    "enat m \<le> enat n \<longleftrightarrow> m \<le> n"
  40.185 @@ -375,10 +349,15 @@
  40.186      by (simp split: enat.splits)
  40.187  qed
  40.188  
  40.189 +(* BH: These equations are already proven generally for any type in
  40.190 +class linordered_semidom. However, enat is not in that class because
  40.191 +it does not have the cancellation property. Would it be worthwhile to
  40.192 +a generalize linordered_semidom to a new class that includes enat? *)
  40.193 +
  40.194  lemma enat_ord_number [simp]:
  40.195 -  "(number_of m \<Colon> enat) \<le> number_of n \<longleftrightarrow> (number_of m \<Colon> nat) \<le> number_of n"
  40.196 -  "(number_of m \<Colon> enat) < number_of n \<longleftrightarrow> (number_of m \<Colon> nat) < number_of n"
  40.197 -  by (simp_all add: number_of_enat_def)
  40.198 +  "(numeral m \<Colon> enat) \<le> numeral n \<longleftrightarrow> (numeral m \<Colon> nat) \<le> numeral n"
  40.199 +  "(numeral m \<Colon> enat) < numeral n \<longleftrightarrow> (numeral m \<Colon> nat) < numeral n"
  40.200 +  by (simp_all add: numeral_eq_enat)
  40.201  
  40.202  lemma i0_lb [simp]: "(0\<Colon>enat) \<le> n"
  40.203    by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
  40.204 @@ -525,10 +504,10 @@
  40.205    val find_first = find_first_t []
  40.206    val trans_tac = Numeral_Simprocs.trans_tac
  40.207    val norm_ss = HOL_basic_ss addsimps
  40.208 -    @{thms add_ac semiring_numeral_0_eq_0 add_0_left add_0_right}
  40.209 +    @{thms add_ac add_0_left add_0_right}
  40.210    fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
  40.211    fun simplify_meta_eq ss cancel_th th =
  40.212 -    Arith_Data.simplify_meta_eq @{thms semiring_numeral_0_eq_0} ss
  40.213 +    Arith_Data.simplify_meta_eq [] ss
  40.214        ([th, cancel_th] MRS trans)
  40.215    fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
  40.216  end
  40.217 @@ -646,7 +625,7 @@
  40.218  
  40.219  subsection {* Traditional theorem names *}
  40.220  
  40.221 -lemmas enat_defs = zero_enat_def one_enat_def number_of_enat_def eSuc_def
  40.222 +lemmas enat_defs = zero_enat_def one_enat_def eSuc_def
  40.223    plus_enat_def less_eq_enat_def less_enat_def
  40.224  
  40.225  end
    41.1 --- a/src/HOL/Library/Extended_Real.thy	Fri Mar 23 20:32:43 2012 +0100
    41.2 +++ b/src/HOL/Library/Extended_Real.thy	Mon Mar 26 10:56:56 2012 +0200
    41.3 @@ -124,11 +124,6 @@
    41.4    fix x :: ereal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
    41.5  qed auto
    41.6  
    41.7 -instantiation ereal :: number
    41.8 -begin
    41.9 -definition [simp]: "number_of x = ereal (number_of x)"
   41.10 -instance ..
   41.11 -end
   41.12  
   41.13  instantiation ereal :: abs
   41.14  begin
   41.15 @@ -671,6 +666,14 @@
   41.16    using assms
   41.17    by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
   41.18  
   41.19 +instance ereal :: numeral ..
   41.20 +
   41.21 +lemma numeral_eq_ereal [simp]: "numeral w = ereal (numeral w)"
   41.22 +  apply (induct w rule: num_induct)
   41.23 +  apply (simp only: numeral_One one_ereal_def)
   41.24 +  apply (simp only: numeral_inc ereal_plus_1)
   41.25 +  done
   41.26 +
   41.27  lemma ereal_le_epsilon:
   41.28    fixes x y :: ereal
   41.29    assumes "ALL e. 0 < e --> x <= y + e"
   41.30 @@ -781,8 +784,8 @@
   41.31    shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
   41.32    by (induct n) (auto simp: one_ereal_def)
   41.33  
   41.34 -lemma ereal_power_number_of[simp]:
   41.35 -  "(number_of num :: ereal) ^ n = ereal (number_of num ^ n)"
   41.36 +lemma ereal_power_numeral[simp]:
   41.37 +  "(numeral num :: ereal) ^ n = ereal (numeral num ^ n)"
   41.38    by (induct n) (auto simp: one_ereal_def)
   41.39  
   41.40  lemma zero_le_power_ereal[simp]:
   41.41 @@ -1730,8 +1733,8 @@
   41.42    "ereal_of_enat m \<le> ereal_of_enat n \<longleftrightarrow> m \<le> n"
   41.43  by (cases m n rule: enat2_cases) auto
   41.44  
   41.45 -lemma number_of_le_ereal_of_enat_iff[simp]:
   41.46 -  shows "number_of m \<le> ereal_of_enat n \<longleftrightarrow> number_of m \<le> n"
   41.47 +lemma numeral_le_ereal_of_enat_iff[simp]:
   41.48 +  shows "numeral m \<le> ereal_of_enat n \<longleftrightarrow> numeral m \<le> n"
   41.49  by (cases n) (auto dest: natceiling_le intro: natceiling_le_eq[THEN iffD1])
   41.50  
   41.51  lemma ereal_of_enat_ge_zero_cancel_iff[simp]:
    42.1 --- a/src/HOL/Library/Float.thy	Fri Mar 23 20:32:43 2012 +0100
    42.2 +++ b/src/HOL/Library/Float.thy	Mon Mar 26 10:56:56 2012 +0200
    42.3 @@ -41,18 +41,6 @@
    42.4  instance ..
    42.5  end
    42.6  
    42.7 -instantiation float :: number
    42.8 -begin
    42.9 -definition number_of_float where "number_of n = Float n 0"
   42.10 -instance ..
   42.11 -end
   42.12 -
   42.13 -lemma number_of_float_Float:
   42.14 -  "number_of k = Float (number_of k) 0"
   42.15 -  by (simp add: number_of_float_def number_of_is_id)
   42.16 -
   42.17 -declare number_of_float_Float [symmetric, code_abbrev]
   42.18 -
   42.19  lemma real_of_float_simp[simp]: "real (Float a b) = real a * pow2 b"
   42.20    unfolding real_of_float_def using of_float.simps .
   42.21  
   42.22 @@ -63,12 +51,9 @@
   42.23  lemma Float_num[simp]: shows
   42.24     "real (Float 1 0) = 1" and "real (Float 1 1) = 2" and "real (Float 1 2) = 4" and
   42.25     "real (Float 1 -1) = 1/2" and "real (Float 1 -2) = 1/4" and "real (Float 1 -3) = 1/8" and
   42.26 -   "real (Float -1 0) = -1" and "real (Float (number_of n) 0) = number_of n"
   42.27 +   "real (Float -1 0) = -1" and "real (Float (numeral n) 0) = numeral n"
   42.28    by auto
   42.29  
   42.30 -lemma float_number_of[simp]: "real (number_of x :: float) = number_of x"
   42.31 -  by (simp only:number_of_float_def Float_num[unfolded number_of_is_id])
   42.32 -
   42.33  lemma float_number_of_int[simp]: "real (Float n 0) = real n"
   42.34    by simp
   42.35  
   42.36 @@ -349,6 +334,21 @@
   42.37      by (cases a, cases b) (simp add: plus_float.simps)
   42.38  qed
   42.39  
   42.40 +instance float :: numeral ..
   42.41 +
   42.42 +lemma Float_add_same_scale: "Float x e + Float y e = Float (x + y) e"
   42.43 +  by (simp add: plus_float.simps)
   42.44 +
   42.45 +(* FIXME: define other constant for code_unfold_post *)
   42.46 +lemma numeral_float_Float (*[code_unfold_post]*):
   42.47 +  "numeral k = Float (numeral k) 0"
   42.48 +  by (induct k, simp_all only: numeral.simps one_float_def
   42.49 +    Float_add_same_scale)
   42.50 +
   42.51 +lemma float_number_of[simp]: "real (numeral x :: float) = numeral x"
   42.52 +  by (simp only: numeral_float_Float Float_num)
   42.53 +
   42.54 +
   42.55  instance float :: comm_monoid_mult
   42.56  proof (intro_classes)
   42.57    fix a b c :: float
   42.58 @@ -555,6 +555,7 @@
   42.59    show ?thesis unfolding real_of_float_nge0_exp[OF P] divide_inverse by auto
   42.60  qed
   42.61  
   42.62 +(* BROKEN
   42.63  lemma bitlen_Pls: "bitlen (Int.Pls) = Int.Pls" by (subst Pls_def, subst Pls_def, simp)
   42.64  
   42.65  lemma bitlen_Min: "bitlen (Int.Min) = Int.Bit1 Int.Pls" by (subst Min_def, simp add: Bit1_def) 
   42.66 @@ -588,6 +589,7 @@
   42.67  
   42.68  lemma bitlen_number_of: "bitlen (number_of w) = number_of (bitlen w)"
   42.69    by (simp add: number_of_is_id)
   42.70 +BH *)
   42.71  
   42.72  lemma [code]: "bitlen x = 
   42.73       (if x = 0  then 0 
   42.74 @@ -722,12 +724,12 @@
   42.75      hence "real x / real y < 1" using `0 < y` and `0 \<le> x` by auto
   42.76  
   42.77      from real_of_int_div4[of "?X" y]
   42.78 -    have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power real_number_of .
   42.79 +    have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power real_numeral .
   42.80      also have "\<dots> < 1 * 2^?l" using `real x / real y < 1` by (rule mult_strict_right_mono, auto)
   42.81      finally have "?X div y < 2^?l" unfolding real_of_int_less_iff[of _ "2^?l", symmetric] by auto
   42.82      hence "?X div y + 1 \<le> 2^?l" by auto
   42.83      hence "real (?X div y + 1) * inverse (2^?l) \<le> 2^?l * inverse (2^?l)"
   42.84 -      unfolding real_of_int_le_iff[of _ "2^?l", symmetric] real_of_int_power real_number_of
   42.85 +      unfolding real_of_int_le_iff[of _ "2^?l", symmetric] real_of_int_power real_numeral
   42.86        by (rule mult_right_mono, auto)
   42.87      hence "real (?X div y + 1) * inverse (2^?l) \<le> 1" by auto
   42.88      thus ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_not_P[OF False]
   42.89 @@ -796,12 +798,12 @@
   42.90      qed
   42.91  
   42.92      from real_of_int_div4[of "?X" y]
   42.93 -    have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power real_number_of .
   42.94 +    have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power real_numeral .
   42.95      also have "\<dots> < 1/2 * 2^?l" using `real x / real y < 1/2` by (rule mult_strict_right_mono, auto)
   42.96      finally have "?X div y * 2 < 2^?l" unfolding real_of_int_less_iff[of _ "2^?l", symmetric] by auto
   42.97      hence "?X div y + 1 < 2^?l" using `0 < ?X div y` by auto
   42.98      hence "real (?X div y + 1) * inverse (2^?l) < 2^?l * inverse (2^?l)"
   42.99 -      unfolding real_of_int_less_iff[of _ "2^?l", symmetric] real_of_int_power real_number_of
  42.100 +      unfolding real_of_int_less_iff[of _ "2^?l", symmetric] real_of_int_power real_numeral
  42.101        by (rule mult_strict_right_mono, auto)
  42.102      hence "real (?X div y + 1) * inverse (2^?l) < 1" by auto
  42.103      thus ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_not_P[OF False]
  42.104 @@ -1195,7 +1197,7 @@
  42.105      case True
  42.106      have "real (m div 2^(nat ?l)) * pow2 ?l \<le> real m"
  42.107      proof -
  42.108 -      have "real (m div 2^(nat ?l)) * pow2 ?l = real (2^(nat ?l) * (m div 2^(nat ?l)))" unfolding real_of_int_mult real_of_int_power real_number_of unfolding pow2_int[symmetric] 
  42.109 +      have "real (m div 2^(nat ?l)) * pow2 ?l = real (2^(nat ?l) * (m div 2^(nat ?l)))" unfolding real_of_int_mult real_of_int_power real_numeral unfolding pow2_int[symmetric] 
  42.110          using `?l > 0` by auto
  42.111        also have "\<dots> \<le> real (2^(nat ?l) * (m div 2^(nat ?l)) + m mod 2^(nat ?l))" unfolding real_of_int_add by auto
  42.112        also have "\<dots> = real m" unfolding zmod_zdiv_equality[symmetric] ..
  42.113 @@ -1262,7 +1264,7 @@
  42.114      hence me_eq: "pow2 (-e) = pow2 (int (nat (-e)))" by auto
  42.115      have "real (Float (m div (2 ^ (nat (-e)))) 0) = real (m div 2 ^ (nat (-e)))" unfolding real_of_float_simp by auto
  42.116      also have "\<dots> \<le> real m / real ((2::int) ^ (nat (-e)))" using real_of_int_div4 .
  42.117 -    also have "\<dots> = real m * inverse (2 ^ (nat (-e)))" unfolding real_of_int_power real_number_of divide_inverse ..
  42.118 +    also have "\<dots> = real m * inverse (2 ^ (nat (-e)))" unfolding real_of_int_power real_numeral divide_inverse ..
  42.119      also have "\<dots> = real (Float m e)" unfolding real_of_float_simp me_eq pow2_int pow2_neg[of e] ..
  42.120      finally show ?thesis unfolding Float floor_fl.simps if_not_P[OF `\<not> 0 \<le> e`] .
  42.121    next
  42.122 @@ -1290,7 +1292,7 @@
  42.123      case False
  42.124      hence me_eq: "pow2 (-e) = pow2 (int (nat (-e)))" by auto
  42.125      have "real (Float m e) = real m * inverse (2 ^ (nat (-e)))" unfolding real_of_float_simp me_eq pow2_int pow2_neg[of e] ..
  42.126 -    also have "\<dots> = real m / real ((2::int) ^ (nat (-e)))" unfolding real_of_int_power real_number_of divide_inverse ..
  42.127 +    also have "\<dots> = real m / real ((2::int) ^ (nat (-e)))" unfolding real_of_int_power real_numeral divide_inverse ..
  42.128      also have "\<dots> \<le> 1 + real (m div 2 ^ (nat (-e)))" using real_of_int_div3[unfolded diff_le_eq] .
  42.129      also have "\<dots> = real (Float (m div (2 ^ (nat (-e))) + 1) 0)" unfolding real_of_float_simp by auto
  42.130      finally show ?thesis unfolding Float ceiling_fl.simps if_not_P[OF `\<not> 0 \<le> e`] .
    43.1 --- a/src/HOL/Library/Formal_Power_Series.thy	Fri Mar 23 20:32:43 2012 +0100
    43.2 +++ b/src/HOL/Library/Formal_Power_Series.thy	Mon Mar 26 10:56:56 2012 +0200
    43.3 @@ -392,25 +392,13 @@
    43.4  
    43.5  instance fps :: (idom) idom ..
    43.6  
    43.7 -instantiation fps :: (comm_ring_1) number_ring
    43.8 -begin
    43.9 -definition number_of_fps_def: "(number_of k::'a fps) = of_int k"
   43.10 -
   43.11 -instance proof
   43.12 -qed (rule number_of_fps_def)
   43.13 -end
   43.14 -
   43.15 -lemma number_of_fps_const: "(number_of k::('a::comm_ring_1) fps) = fps_const (of_int k)"
   43.16 -  
   43.17 -proof(induct k rule: int_induct [where k=0])
   43.18 -  case base thus ?case unfolding number_of_fps_def of_int_0 by simp
   43.19 -next
   43.20 -  case (step1 i) thus ?case unfolding number_of_fps_def 
   43.21 -    by (simp add: fps_const_add[symmetric] del: fps_const_add)
   43.22 -next
   43.23 -  case (step2 i) thus ?case unfolding number_of_fps_def 
   43.24 -    by (simp add: fps_const_sub[symmetric] del: fps_const_sub)
   43.25 -qed
   43.26 +lemma numeral_fps_const: "numeral k = fps_const (numeral k)"
   43.27 +  by (induct k, simp_all only: numeral.simps fps_const_1_eq_1
   43.28 +    fps_const_add [symmetric])
   43.29 +
   43.30 +lemma neg_numeral_fps_const: "neg_numeral k = fps_const (neg_numeral k)"
   43.31 +  by (simp only: neg_numeral_def numeral_fps_const fps_const_neg)
   43.32 +
   43.33  subsection{* The eXtractor series X*}
   43.34  
   43.35  lemma minus_one_power_iff: "(- (1::'a :: {comm_ring_1})) ^ n = (if even n then 1 else - 1)"
   43.36 @@ -1119,7 +1107,7 @@
   43.37    have eq: "(1 + X) * ?r = 1"
   43.38      unfolding minus_one_power_iff
   43.39      by (auto simp add: field_simps fps_eq_iff)
   43.40 -  show ?thesis by (auto simp add: eq intro: fps_inverse_unique)
   43.41 +  show ?thesis by (auto simp add: eq intro: fps_inverse_unique simp del: minus_one)
   43.42  qed
   43.43  
   43.44  
   43.45 @@ -1157,8 +1145,11 @@
   43.46    "fps_const (a::'a::{comm_ring_1}) oo b = fps_const (a)"
   43.47    by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
   43.48  
   43.49 -lemma number_of_compose[simp]: "(number_of k::('a::{comm_ring_1}) fps) oo b = number_of k"
   43.50 -  unfolding number_of_fps_const by simp
   43.51 +lemma numeral_compose[simp]: "(numeral k::('a::{comm_ring_1}) fps) oo b = numeral k"
   43.52 +  unfolding numeral_fps_const by simp
   43.53 +
   43.54 +lemma neg_numeral_compose[simp]: "(neg_numeral k::('a::{comm_ring_1}) fps) oo b = neg_numeral k"
   43.55 +  unfolding neg_numeral_fps_const by simp
   43.56  
   43.57  lemma X_fps_compose_startby0[simp]: "a$0 = 0 \<Longrightarrow> X oo a = (a :: ('a :: comm_ring_1) fps)"
   43.58    by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum_delta
   43.59 @@ -2568,7 +2559,7 @@
   43.60    (is "inverse ?l = ?r")
   43.61  proof-
   43.62    have th: "?l * ?r = 1"
   43.63 -    by (auto simp add: field_simps fps_eq_iff minus_one_power_iff)
   43.64 +    by (auto simp add: field_simps fps_eq_iff minus_one_power_iff simp del: minus_one)
   43.65    have th': "?l $ 0 \<noteq> 0" by (simp add: )
   43.66    from fps_inverse_unique[OF th' th] show ?thesis .
   43.67  qed
   43.68 @@ -2765,7 +2756,7 @@
   43.69  proof-
   43.70    have th: "?r$0 \<noteq> 0" by simp
   43.71    have th': "fps_deriv (inverse ?r) = fps_const (- 1) * inverse ?r / (1 + X)"
   43.72 -    by (simp add: fps_inverse_deriv[OF th] fps_divide_def power2_eq_square mult_commute fps_const_neg[symmetric] del: fps_const_neg)
   43.73 +    by (simp add: fps_inverse_deriv[OF th] fps_divide_def power2_eq_square mult_commute fps_const_neg[symmetric] del: fps_const_neg minus_one)
   43.74    have eq: "inverse ?r $ 0 = 1"
   43.75      by (simp add: fps_inverse_def)
   43.76    from iffD1[OF fps_binomial_ODE_unique[of "inverse (1 + X)" "- 1"] th'] eq
   43.77 @@ -2855,7 +2846,7 @@
   43.78            unfolding m1nk 
   43.79            
   43.80            unfolding m h pochhammer_Suc_setprod
   43.81 -          apply (simp add: field_simps del: fact_Suc id_def)
   43.82 +          apply (simp add: field_simps del: fact_Suc id_def minus_one)
   43.83            unfolding fact_altdef_nat id_def
   43.84            unfolding of_nat_setprod
   43.85            unfolding setprod_timesf[symmetric]
   43.86 @@ -3162,28 +3153,25 @@
   43.87  lemma fps_const_minus: "fps_const (c::'a::group_add) - fps_const d = fps_const (c - d)"
   43.88    by (simp add: fps_eq_iff fps_const_def)
   43.89  
   43.90 -lemma fps_number_of_fps_const: "number_of i = fps_const (number_of i :: 'a:: {comm_ring_1, number_ring})"
   43.91 -  apply (subst (2) number_of_eq)
   43.92 -apply(rule int_induct [of _ 0])
   43.93 -apply (simp_all add: number_of_fps_def)
   43.94 -by (simp_all add: fps_const_add[symmetric] fps_const_minus[symmetric])
   43.95 +lemma fps_numeral_fps_const: "numeral i = fps_const (numeral i :: 'a:: {comm_ring_1})"
   43.96 +  by (fact numeral_fps_const) (* FIXME: duplicate *)
   43.97  
   43.98  lemma fps_cos_Eii:
   43.99    "fps_cos c = (E (ii * c) + E (- ii * c)) / fps_const 2"
  43.100  proof-
  43.101    have th: "fps_cos c + fps_cos c = fps_cos c * fps_const 2" 
  43.102 -    by (simp add: fps_eq_iff fps_number_of_fps_const complex_number_of_def[symmetric])
  43.103 +    by (simp add: numeral_fps_const)
  43.104    show ?thesis
  43.105    unfolding Eii_sin_cos minus_mult_commute
  43.106 -  by (simp add: fps_sin_even fps_cos_odd fps_number_of_fps_const
  43.107 -    fps_divide_def fps_const_inverse th complex_number_of_def[symmetric])
  43.108 +  by (simp add: fps_sin_even fps_cos_odd numeral_fps_const
  43.109 +    fps_divide_def fps_const_inverse th)
  43.110  qed
  43.111  
  43.112  lemma fps_sin_Eii:
  43.113    "fps_sin c = (E (ii * c) - E (- ii * c)) / fps_const (2*ii)"
  43.114  proof-
  43.115    have th: "fps_const \<i> * fps_sin c + fps_const \<i> * fps_sin c = fps_sin c * fps_const (2 * ii)" 
  43.116 -    by (simp add: fps_eq_iff fps_number_of_fps_const complex_number_of_def[symmetric])
  43.117 +    by (simp add: fps_eq_iff numeral_fps_const)
  43.118    show ?thesis
  43.119    unfolding Eii_sin_cos minus_mult_commute
  43.120    by (simp add: fps_sin_even fps_cos_odd fps_divide_def fps_const_inverse th)
    44.1 --- a/src/HOL/Library/Numeral_Type.thy	Fri Mar 23 20:32:43 2012 +0100
    44.2 +++ b/src/HOL/Library/Numeral_Type.thy	Mon Mar 26 10:56:56 2012 +0200
    44.3 @@ -66,7 +66,6 @@
    44.4      by simp
    44.5  qed
    44.6  
    44.7 -
    44.8  subsection {* Locales for for modular arithmetic subtypes *}
    44.9  
   44.10  locale mod_type =
   44.11 @@ -137,8 +136,8 @@
   44.12  
   44.13  locale mod_ring = mod_type n Rep Abs
   44.14    for n :: int
   44.15 -  and Rep :: "'a::{number_ring} \<Rightarrow> int"
   44.16 -  and Abs :: "int \<Rightarrow> 'a::{number_ring}"
   44.17 +  and Rep :: "'a::{comm_ring_1} \<Rightarrow> int"
   44.18 +  and Abs :: "int \<Rightarrow> 'a::{comm_ring_1}"
   44.19  begin
   44.20  
   44.21  lemma of_nat_eq: "of_nat k = Abs (int k mod n)"
   44.22 @@ -152,13 +151,14 @@
   44.23  apply (simp add: Rep_simps of_nat_eq diff_def zmod_simps)
   44.24  done
   44.25  
   44.26 -lemma Rep_number_of:
   44.27 -  "Rep (number_of w) = number_of w mod n"
   44.28 -by (simp add: number_of_eq of_int_eq Rep_Abs_mod)
   44.29 +lemma Rep_numeral:
   44.30 +  "Rep (numeral w) = numeral w mod n"
   44.31 +using of_int_eq [of "numeral w"]
   44.32 +by (simp add: Rep_inject_sym Rep_Abs_mod)
   44.33  
   44.34 -lemma iszero_number_of:
   44.35 -  "iszero (number_of w::'a) \<longleftrightarrow> number_of w mod n = 0"
   44.36 -by (simp add: Rep_simps number_of_eq of_int_eq iszero_def zero_def)
   44.37 +lemma iszero_numeral:
   44.38 +  "iszero (numeral w::'a) \<longleftrightarrow> numeral w mod n = 0"
   44.39 +by (simp add: Rep_inject_sym Rep_numeral Rep_0 iszero_def)
   44.40  
   44.41  lemma cases:
   44.42    assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P"
   44.43 @@ -175,14 +175,14 @@
   44.44  end
   44.45  
   44.46  
   44.47 -subsection {* Number ring instances *}
   44.48 +subsection {* Ring class instances *}
   44.49  
   44.50  text {*
   44.51 -  Unfortunately a number ring instance is not possible for
   44.52 +  Unfortunately @{text ring_1} instance is not possible for
   44.53    @{typ num1}, since 0 and 1 are not distinct.
   44.54  *}
   44.55  
   44.56 -instantiation num1 :: "{comm_ring,comm_monoid_mult,number}"
   44.57 +instantiation num1 :: "{comm_ring,comm_monoid_mult,numeral}"
   44.58  begin
   44.59  
   44.60  lemma num1_eq_iff: "(x::num1) = (y::num1) \<longleftrightarrow> True"
   44.61 @@ -252,22 +252,10 @@
   44.62  done
   44.63  
   44.64  instance bit0 :: (finite) comm_ring_1
   44.65 -  by (rule bit0.comm_ring_1)+
   44.66 +  by (rule bit0.comm_ring_1)
   44.67  
   44.68  instance bit1 :: (finite) comm_ring_1
   44.69 -  by (rule bit1.comm_ring_1)+
   44.70 -
   44.71 -instantiation bit0 and bit1 :: (finite) number_ring
   44.72 -begin
   44.73 -
   44.74 -definition "(number_of w :: _ bit0) = of_int w"
   44.75 -
   44.76 -definition "(number_of w :: _ bit1) = of_int w"
   44.77 -
   44.78 -instance proof
   44.79 -qed (rule number_of_bit0_def number_of_bit1_def)+
   44.80 -
   44.81 -end
   44.82 +  by (rule bit1.comm_ring_1)
   44.83  
   44.84  interpretation bit0:
   44.85    mod_ring "int CARD('a::finite bit0)"
   44.86 @@ -289,9 +277,11 @@
   44.87  lemmas bit0_induct [case_names of_int, induct type: bit0] = bit0.induct
   44.88  lemmas bit1_induct [case_names of_int, induct type: bit1] = bit1.induct
   44.89  
   44.90 -lemmas bit0_iszero_number_of [simp] = bit0.iszero_number_of
   44.91 -lemmas bit1_iszero_number_of [simp] = bit1.iszero_number_of
   44.92 +lemmas bit0_iszero_numeral [simp] = bit0.iszero_numeral
   44.93 +lemmas bit1_iszero_numeral [simp] = bit1.iszero_numeral
   44.94  
   44.95 +declare eq_numeral_iff_iszero [where 'a="('a::finite) bit0", standard, simp]
   44.96 +declare eq_numeral_iff_iszero [where 'a="('a::finite) bit1", standard, simp]
   44.97  
   44.98  subsection {* Syntax *}
   44.99  
    45.1 --- a/src/HOL/Library/Poly_Deriv.thy	Fri Mar 23 20:32:43 2012 +0100
    45.2 +++ b/src/HOL/Library/Poly_Deriv.thy	Mon Mar 26 10:56:56 2012 +0200
    45.3 @@ -71,7 +71,8 @@
    45.4  apply (subst power_Suc)
    45.5  apply (subst pderiv_mult)
    45.6  apply (erule ssubst)
    45.7 -apply (simp add: smult_add_left algebra_simps)
    45.8 +apply (simp only: of_nat_Suc smult_add_left smult_1_left)
    45.9 +apply (simp add: algebra_simps) (* FIXME *)
   45.10  done
   45.11  
   45.12  lemma DERIV_cmult2: "DERIV f x :> D ==> DERIV (%x. (f x) * c :: real) x :> D * c"
    46.1 --- a/src/HOL/Library/Polynomial.thy	Fri Mar 23 20:32:43 2012 +0100
    46.2 +++ b/src/HOL/Library/Polynomial.thy	Mon Mar 26 10:56:56 2012 +0200
    46.3 @@ -662,17 +662,6 @@
    46.4  
    46.5  instance poly :: (comm_ring_1) comm_ring_1 ..
    46.6  
    46.7 -instantiation poly :: (comm_ring_1) number_ring
    46.8 -begin
    46.9 -
   46.10 -definition
   46.11 -  "number_of k = (of_int k :: 'a poly)"
   46.12 -
   46.13 -instance
   46.14 -  by default (rule number_of_poly_def)
   46.15 -
   46.16 -end
   46.17 -
   46.18  
   46.19  subsection {* Polynomials form an integral domain *}
   46.20  
   46.21 @@ -1052,12 +1041,12 @@
   46.22  lemma poly_div_minus_left [simp]:
   46.23    fixes x y :: "'a::field poly"
   46.24    shows "(- x) div y = - (x div y)"
   46.25 -  using div_smult_left [of "- 1::'a"] by simp
   46.26 +  using div_smult_left [of "- 1::'a"] by (simp del: minus_one) (* FIXME *)
   46.27  
   46.28  lemma poly_mod_minus_left [simp]:
   46.29    fixes x y :: "'a::field poly"
   46.30    shows "(- x) mod y = - (x mod y)"
   46.31 -  using mod_smult_left [of "- 1::'a"] by simp
   46.32 +  using mod_smult_left [of "- 1::'a"] by (simp del: minus_one) (* FIXME *)
   46.33  
   46.34  lemma pdivmod_rel_smult_right:
   46.35    "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
   46.36 @@ -1075,12 +1064,12 @@
   46.37    fixes x y :: "'a::field poly"
   46.38    shows "x div (- y) = - (x div y)"
   46.39    using div_smult_right [of "- 1::'a"]
   46.40 -  by (simp add: nonzero_inverse_minus_eq)
   46.41 +  by (simp add: nonzero_inverse_minus_eq del: minus_one) (* FIXME *)
   46.42  
   46.43  lemma poly_mod_minus_right [simp]:
   46.44    fixes x y :: "'a::field poly"
   46.45    shows "x mod (- y) = x mod y"
   46.46 -  using mod_smult_right [of "- 1::'a"] by simp
   46.47 +  using mod_smult_right [of "- 1::'a"] by (simp del: minus_one) (* FIXME *)
   46.48  
   46.49  lemma pdivmod_rel_mult:
   46.50    "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
    47.1 --- a/src/HOL/Library/Predicate_Compile_Alternative_Defs.thy	Fri Mar 23 20:32:43 2012 +0100
    47.2 +++ b/src/HOL/Library/Predicate_Compile_Alternative_Defs.thy	Mon Mar 26 10:56:56 2012 +0200
    47.3 @@ -54,8 +54,8 @@
    47.4  
    47.5  section {* Setup for Numerals *}
    47.6  
    47.7 -setup {* Predicate_Compile_Data.ignore_consts [@{const_name number_of}] *}
    47.8 -setup {* Predicate_Compile_Data.keep_functions [@{const_name number_of}] *}
    47.9 +setup {* Predicate_Compile_Data.ignore_consts [@{const_name numeral}, @{const_name neg_numeral}] *}
   47.10 +setup {* Predicate_Compile_Data.keep_functions [@{const_name numeral}, @{const_name neg_numeral}] *}
   47.11  
   47.12  setup {* Predicate_Compile_Data.ignore_consts [@{const_name div}, @{const_name mod}, @{const_name times}] *}
   47.13  
    48.1 --- a/src/HOL/Library/Quotient_List.thy	Fri Mar 23 20:32:43 2012 +0100
    48.2 +++ b/src/HOL/Library/Quotient_List.thy	Mon Mar 26 10:56:56 2012 +0200
    48.3 @@ -8,8 +8,6 @@
    48.4  imports Main Quotient_Syntax
    48.5  begin
    48.6  
    48.7 -declare [[map list = list_all2]]
    48.8 -
    48.9  lemma map_id [id_simps]:
   48.10    "map id = id"
   48.11    by (fact List.map.id)
   48.12 @@ -75,6 +73,8 @@
   48.13      by (induct xs ys rule: list_induct2') auto
   48.14  qed
   48.15  
   48.16 +declare [[map list = (list_all2, list_quotient)]]
   48.17 +
   48.18  lemma cons_prs [quot_preserve]:
   48.19    assumes q: "Quotient R Abs Rep"
   48.20    shows "(Rep ---> (map Rep) ---> (map Abs)) (op #) = (op #)"
    49.1 --- a/src/HOL/Library/Quotient_Option.thy	Fri Mar 23 20:32:43 2012 +0100
    49.2 +++ b/src/HOL/Library/Quotient_Option.thy	Mon Mar 26 10:56:56 2012 +0200
    49.3 @@ -16,8 +16,6 @@
    49.4  | "option_rel R None (Some x) = False"
    49.5  | "option_rel R (Some x) (Some y) = R x y"
    49.6  
    49.7 -declare [[map option = option_rel]]
    49.8 -
    49.9  lemma option_rel_unfold:
   49.10    "option_rel R x y = (case (x, y) of (None, None) \<Rightarrow> True
   49.11      | (Some x, Some y) \<Rightarrow> R x y
   49.12 @@ -65,6 +63,8 @@
   49.13    apply (simp add: option_rel_unfold split: option.split)
   49.14    done
   49.15  
   49.16 +declare [[map option = (option_rel, option_quotient)]]
   49.17 +
   49.18  lemma option_None_rsp [quot_respect]:
   49.19    assumes q: "Quotient R Abs Rep"
   49.20    shows "option_rel R None None"
    50.1 --- a/src/HOL/Library/Quotient_Product.thy	Fri Mar 23 20:32:43 2012 +0100
    50.2 +++ b/src/HOL/Library/Quotient_Product.thy	Mon Mar 26 10:56:56 2012 +0200
    50.3 @@ -13,8 +13,6 @@
    50.4  where
    50.5    "prod_rel R1 R2 = (\<lambda>(a, b) (c, d). R1 a c \<and> R2 b d)"
    50.6  
    50.7 -declare [[map prod = prod_rel]]
    50.8 -
    50.9  lemma prod_rel_apply [simp]:
   50.10    "prod_rel R1 R2 (a, b) (c, d) \<longleftrightarrow> R1 a c \<and> R2 b d"
   50.11    by (simp add: prod_rel_def)
   50.12 @@ -45,6 +43,8 @@
   50.13    apply (auto simp add: split_paired_all)
   50.14    done
   50.15  
   50.16 +declare [[map prod = (prod_rel, prod_quotient)]]
   50.17 +
   50.18  lemma Pair_rsp [quot_respect]:
   50.19    assumes q1: "Quotient R1 Abs1 Rep1"
   50.20    assumes q2: "Quotient R2 Abs2 Rep2"
    51.1 --- a/src/HOL/Library/Quotient_Set.thy	Fri Mar 23 20:32:43 2012 +0100
    51.2 +++ b/src/HOL/Library/Quotient_Set.thy	Mon Mar 26 10:56:56 2012 +0200
    51.3 @@ -26,6 +26,8 @@
    51.4      by auto (metis rep_abs_rsp[OF assms] assms[simplified Quotient_def])+
    51.5  qed
    51.6  
    51.7 +declare [[map set = (set_rel, set_quotient)]]
    51.8 +
    51.9  lemma empty_set_rsp[quot_respect]:
   51.10    "set_rel R {} {}"
   51.11    unfolding set_rel_def by simp
    52.1 --- a/src/HOL/Library/Quotient_Sum.thy	Fri Mar 23 20:32:43 2012 +0100
    52.2 +++ b/src/HOL/Library/Quotient_Sum.thy	Mon Mar 26 10:56:56 2012 +0200
    52.3 @@ -16,8 +16,6 @@
    52.4  | "sum_rel R1 R2 (Inr a2) (Inl b1) = False"
    52.5  | "sum_rel R1 R2 (Inr a2) (Inr b2) = R2 a2 b2"
    52.6  
    52.7 -declare [[map sum = sum_rel]]
    52.8 -
    52.9  lemma sum_rel_unfold:
   52.10    "sum_rel R1 R2 x y = (case (x, y) of (Inl x, Inl y) \<Rightarrow> R1 x y
   52.11      | (Inr x, Inr y) \<Rightarrow> R2 x y
   52.12 @@ -67,6 +65,8 @@
   52.13    apply (simp add: sum_rel_unfold comp_def split: sum.split)
   52.14    done
   52.15  
   52.16 +declare [[map sum = (sum_rel, sum_quotient)]]
   52.17 +
   52.18  lemma sum_Inl_rsp [quot_respect]:
   52.19    assumes q1: "Quotient R1 Abs1 Rep1"
   52.20    assumes q2: "Quotient R2 Abs2 Rep2"
    53.1 --- a/src/HOL/Library/ROOT.ML	Fri Mar 23 20:32:43 2012 +0100
    53.2 +++ b/src/HOL/Library/ROOT.ML	Mon Mar 26 10:56:56 2012 +0200
    53.3 @@ -4,4 +4,4 @@
    53.4  use_thys ["Library", "List_Cset", "List_Prefix", "List_lexord", "Sublist_Order",
    53.5    "Product_Lattice",
    53.6    "Code_Char_chr", "Code_Char_ord", "Code_Integer", "Efficient_Nat"(*, "Code_Prolog"*),
    53.7 -  "Code_Real_Approx_By_Float" ];
    53.8 +  "Code_Real_Approx_By_Float", "Target_Numeral"];
    54.1 --- a/src/HOL/Library/Saturated.thy	Fri Mar 23 20:32:43 2012 +0100
    54.2 +++ b/src/HOL/Library/Saturated.thy	Mon Mar 26 10:56:56 2012 +0200
    54.3 @@ -157,20 +157,16 @@
    54.4    "nat_of (Sat n :: ('a::len) sat) = min (len_of TYPE('a)) n"
    54.5    by (rule nat_of_Abs_sat' [unfolded Abs_sat'_eq_of_nat])
    54.6  
    54.7 -instantiation sat :: (len) number_semiring
    54.8 -begin
    54.9 +lemma [code_abbrev]:
   54.10 +  "of_nat (numeral k) = (numeral k :: 'a::len sat)"
   54.11 +  by simp
   54.12  
   54.13 -definition
   54.14 -  number_of_sat_def [code del]: "number_of = Sat \<circ> nat"
   54.15 -
   54.16 -instance
   54.17 -  by default (simp add: number_of_sat_def)
   54.18 -
   54.19 -end
   54.20 +definition sat_of_nat :: "nat \<Rightarrow> ('a::len) sat"
   54.21 +  where [code_abbrev]: "sat_of_nat = of_nat"
   54.22  
   54.23  lemma [code abstract]:
   54.24 -  "nat_of (number_of n :: ('a::len) sat) = min (nat n) (len_of TYPE('a))"
   54.25 -  unfolding number_of_sat_def by simp
   54.26 +  "nat_of (sat_of_nat n :: ('a::len) sat) = min (len_of TYPE('a)) n"
   54.27 +  by (simp add: sat_of_nat_def)
   54.28  
   54.29  instance sat :: (len) finite
   54.30  proof
   54.31 @@ -252,4 +248,6 @@
   54.32  
   54.33  end
   54.34  
   54.35 +hide_const (open) sat_of_nat
   54.36 +
   54.37  end
    55.1 --- a/src/HOL/Library/Sum_of_Squares/sum_of_squares.ML	Fri Mar 23 20:32:43 2012 +0100
    55.2 +++ b/src/HOL/Library/Sum_of_Squares/sum_of_squares.ML	Mon Mar 26 10:56:56 2012 +0200
    55.3 @@ -866,10 +866,11 @@
    55.4     @{term "op / :: real => _"}, @{term "inverse :: real => _"},
    55.5     @{term "op ^ :: real => _"}, @{term "abs :: real => _"},
    55.6     @{term "min :: real => _"}, @{term "max :: real => _"},
    55.7 -   @{term "0::real"}, @{term "1::real"}, @{term "number_of :: int => real"},
    55.8 -   @{term "number_of :: int => nat"},
    55.9 -   @{term "Int.Bit0"}, @{term "Int.Bit1"},
   55.10 -   @{term "Int.Pls"}, @{term "Int.Min"}];
   55.11 +   @{term "0::real"}, @{term "1::real"},
   55.12 +   @{term "numeral :: num => nat"},
   55.13 +   @{term "numeral :: num => real"},
   55.14 +   @{term "neg_numeral :: num => real"},
   55.15 +   @{term "Num.Bit0"}, @{term "Num.Bit1"}, @{term "Num.One"}];
   55.16  
   55.17  fun check_sos kcts ct =
   55.18   let
    56.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    56.2 +++ b/src/HOL/Library/Target_Numeral.thy	Mon Mar 26 10:56:56 2012 +0200
    56.3 @@ -0,0 +1,726 @@
    56.4 +theory Target_Numeral
    56.5 +imports Main Code_Nat
    56.6 +begin
    56.7 +
    56.8 +subsection {* Type of target language numerals *}
    56.9 +
   56.10 +typedef (open) int = "UNIV \<Colon> int set"
   56.11 +  morphisms int_of of_int ..
   56.12 +
   56.13 +hide_type (open) int
   56.14 +hide_const (open) of_int
   56.15 +
   56.16 +lemma int_eq_iff:
   56.17 +  "k = l \<longleftrightarrow> int_of k = int_of l"
   56.18 +  using int_of_inject [of k l] ..
   56.19 +
   56.20 +lemma int_eqI:
   56.21 +  "int_of k = int_of l \<Longrightarrow> k = l"
   56.22 +  using int_eq_iff [of k l] by simp
   56.23 +
   56.24 +lemma int_of_int [simp]:
   56.25 +  "int_of (Target_Numeral.of_int k) = k"
   56.26 +  using of_int_inverse [of k] by simp
   56.27 +
   56.28 +lemma of_int_of [simp]:
   56.29 +  "Target_Numeral.of_int (int_of k) = k"
   56.30 +  using int_of_inverse [of k] by simp
   56.31 +
   56.32 +hide_fact (open) int_eq_iff int_eqI
   56.33 +
   56.34 +instantiation Target_Numeral.int :: ring_1
   56.35 +begin
   56.36 +
   56.37 +definition
   56.38 +  "0 = Target_Numeral.of_int 0"
   56.39 +
   56.40 +lemma int_of_zero [simp]:
   56.41 +  "int_of 0 = 0"
   56.42 +  by (simp add: zero_int_def)
   56.43 +
   56.44 +definition
   56.45 +  "1 = Target_Numeral.of_int 1"
   56.46 +
   56.47 +lemma int_of_one [simp]:
   56.48 +  "int_of 1 = 1"
   56.49 +  by (simp add: one_int_def)
   56.50 +
   56.51 +definition
   56.52 +  "k + l = Target_Numeral.of_int (int_of k + int_of l)"
   56.53 +
   56.54 +lemma int_of_plus [simp]:
   56.55 +  "int_of (k + l) = int_of k + int_of l"
   56.56 +  by (simp add: plus_int_def)
   56.57 +
   56.58 +definition
   56.59 +  "- k = Target_Numeral.of_int (- int_of k)"
   56.60 +
   56.61 +lemma int_of_uminus [simp]:
   56.62 +  "int_of (- k) = - int_of k"
   56.63 +  by (simp add: uminus_int_def)
   56.64 +
   56.65 +definition
   56.66 +  "k - l = Target_Numeral.of_int (int_of k - int_of l)"
   56.67 +
   56.68 +lemma int_of_minus [simp]:
   56.69 +  "int_of (k - l) = int_of k - int_of l"
   56.70 +  by (simp add: minus_int_def)
   56.71 +
   56.72 +definition
   56.73 +  "k * l = Target_Numeral.of_int (int_of k * int_of l)"
   56.74 +
   56.75 +lemma int_of_times [simp]:
   56.76 +  "int_of (k * l) = int_of k * int_of l"
   56.77 +  by (simp add: times_int_def)
   56.78 +
   56.79 +instance proof
   56.80 +qed (auto simp add: Target_Numeral.int_eq_iff algebra_simps)
   56.81 +
   56.82 +end
   56.83 +
   56.84 +lemma int_of_of_nat [simp]:
   56.85 +  "int_of (of_nat n) = of_nat n"
   56.86 +  by (induct n) simp_all
   56.87 +
   56.88 +definition nat_of :: "Target_Numeral.int \<Rightarrow> nat" where
   56.89 +  "nat_of k = Int.nat (int_of k)"
   56.90 +
   56.91 +lemma nat_of_of_nat [simp]:
   56.92 +  "nat_of (of_nat n) = n"
   56.93 +  by (simp add: nat_of_def)
   56.94 +
   56.95 +lemma int_of_of_int [simp]:
   56.96 +  "int_of (of_int k) = k"
   56.97 +  by (induct k) (simp_all, simp only: neg_numeral_def numeral_One int_of_uminus int_of_one)
   56.98 +
   56.99 +lemma of_int_of_int [simp, code_abbrev]:
  56.100 +  "Target_Numeral.of_int = of_int"
  56.101 +  by rule (simp add: Target_Numeral.int_eq_iff)
  56.102 +
  56.103 +lemma int_of_numeral [simp]:
  56.104 +  "int_of (numeral k) = numeral k"
  56.105 +  using int_of_of_int [of "numeral k"] by simp
  56.106 +
  56.107 +lemma int_of_neg_numeral [simp]:
  56.108 +  "int_of (neg_numeral k) = neg_numeral k"
  56.109 +  by (simp only: neg_numeral_def int_of_uminus) simp
  56.110 +
  56.111 +lemma int_of_sub [simp]:
  56.112 +  "int_of (Num.sub k l) = Num.sub k l"
  56.113 +  by (simp only: Num.sub_def int_of_minus int_of_numeral)
  56.114 +
  56.115 +instantiation Target_Numeral.int :: "{ring_div, equal, linordered_idom}"
  56.116 +begin
  56.117 +
  56.118 +definition
  56.119 +  "k div l = of_int (int_of k div int_of l)"
  56.120 +
  56.121 +lemma int_of_div [simp]:
  56.122 +  "int_of (k div l) = int_of k div int_of l"
  56.123 +  by (simp add: div_int_def)
  56.124 +
  56.125 +definition
  56.126 +  "k mod l = of_int (int_of k mod int_of l)"
  56.127 +
  56.128 +lemma int_of_mod [simp]:
  56.129 +  "int_of (k mod l) = int_of k mod int_of l"
  56.130 +  by (simp add: mod_int_def)
  56.131 +
  56.132 +definition
  56.133 +  "\<bar>k\<bar> = of_int \<bar>int_of k\<bar>"
  56.134 +
  56.135 +lemma int_of_abs [simp]:
  56.136 +  "int_of \<bar>k\<bar> = \<bar>int_of k\<bar>"
  56.137 +  by (simp add: abs_int_def)
  56.138 +
  56.139 +definition
  56.140 +  "sgn k = of_int (sgn (int_of k))"
  56.141 +
  56.142 +lemma int_of_sgn [simp]:
  56.143 +  "int_of (sgn k) = sgn (int_of k)"
  56.144 +  by (simp add: sgn_int_def)
  56.145 +
  56.146 +definition
  56.147 +  "k \<le> l \<longleftrightarrow> int_of k \<le> int_of l"
  56.148 +
  56.149 +definition
  56.150 +  "k < l \<longleftrightarrow> int_of k < int_of l"
  56.151 +
  56.152 +definition
  56.153 +  "HOL.equal k l \<longleftrightarrow> HOL.equal (int_of k) (int_of l)"
  56.154 +
  56.155 +instance proof
  56.156 +qed (auto simp add: Target_Numeral.int_eq_iff algebra_simps
  56.157 +  less_eq_int_def less_int_def equal_int_def equal)
  56.158 +
  56.159 +end
  56.160 +
  56.161 +lemma int_of_min [simp]:
  56.162 +  "int_of (min k l) = min (int_of k) (int_of l)"
  56.163 +  by (simp add: min_def less_eq_int_def)
  56.164 +
  56.165 +lemma int_of_max [simp]:
  56.166 +  "int_of (max k l) = max (int_of k) (int_of l)"
  56.167 +  by (simp add: max_def less_eq_int_def)
  56.168 +
  56.169 +
  56.170 +subsection {* Code theorems for target language numerals *}
  56.171 +
  56.172 +text {* Constructors *}
  56.173 +
  56.174 +definition Pos :: "num \<Rightarrow> Target_Numeral.int" where
  56.175 +  [simp, code_abbrev]: "Pos = numeral"
  56.176 +
  56.177 +definition Neg :: "num \<Rightarrow> Target_Numeral.int" where
  56.178 +  [simp, code_abbrev]: "Neg = neg_numeral"
  56.179 +
  56.180 +code_datatype "0::Target_Numeral.int" Pos Neg
  56.181 +
  56.182 +
  56.183 +text {* Auxiliary operations *}
  56.184 +
  56.185 +definition dup :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int" where
  56.186 +  [simp]: "dup k = k + k"
  56.187 +
  56.188 +lemma dup_code [code]:
  56.189 +  "dup 0 = 0"
  56.190 +  "dup (Pos n) = Pos (Num.Bit0 n)"
  56.191 +  "dup (Neg n) = Neg (Num.Bit0 n)"
  56.192 +  unfolding Pos_def Neg_def neg_numeral_def
  56.193 +  by (simp_all add: numeral_Bit0)
  56.194 +
  56.195 +definition sub :: "num \<Rightarrow> num \<Rightarrow> Target_Numeral.int" where
  56.196 +  [simp]: "sub m n = numeral m - numeral n"
  56.197 +
  56.198 +lemma sub_code [code]:
  56.199 +  "sub Num.One Num.One = 0"
  56.200 +  "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
  56.201 +  "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
  56.202 +  "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
  56.203 +  "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
  56.204 +  "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
  56.205 +  "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
  56.206 +  "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
  56.207 +  "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
  56.208 +  unfolding sub_def dup_def numeral.simps Pos_def Neg_def
  56.209 +    neg_numeral_def numeral_BitM
  56.210 +  by (simp_all only: algebra_simps add.comm_neutral)
  56.211 +
  56.212 +
  56.213 +text {* Implementations *}
  56.214 +
  56.215 +lemma one_int_code [code, code_unfold]:
  56.216 +  "1 = Pos Num.One"
  56.217 +  by simp
  56.218 +
  56.219 +lemma plus_int_code [code]:
  56.220 +  "k + 0 = (k::Target_Numeral.int)"
  56.221 +  "0 + l = (l::Target_Numeral.int)"
  56.222 +  "Pos m + Pos n = Pos (m + n)"
  56.223 +  "Pos m + Neg n = sub m n"
  56.224 +  "Neg m + Pos n = sub n m"
  56.225 +  "Neg m + Neg n = Neg (m + n)"
  56.226 +  by simp_all
  56.227 +
  56.228 +lemma uminus_int_code [code]:
  56.229 +  "uminus 0 = (0::Target_Numeral.int)"
  56.230 +  "uminus (Pos m) = Neg m"
  56.231 +  "uminus (Neg m) = Pos m"
  56.232 +  by simp_all
  56.233 +
  56.234 +lemma minus_int_code [code]:
  56.235 +  "k - 0 = (k::Target_Numeral.int)"
  56.236 +  "0 - l = uminus (l::Target_Numeral.int)"
  56.237 +  "Pos m - Pos n = sub m n"
  56.238 +  "Pos m - Neg n = Pos (m + n)"
  56.239 +  "Neg m - Pos n = Neg (m + n)"
  56.240 +  "Neg m - Neg n = sub n m"
  56.241 +  by simp_all
  56.242 +
  56.243 +lemma times_int_code [code]:
  56.244 +  "k * 0 = (0::Target_Numeral.int)"
  56.245 +  "0 * l = (0::Target_Numeral.int)"
  56.246 +  "Pos m * Pos n = Pos (m * n)"
  56.247 +  "Pos m * Neg n = Neg (m * n)"
  56.248 +  "Neg m * Pos n = Neg (m * n)"
  56.249 +  "Neg m * Neg n = Pos (m * n)"
  56.250 +  by simp_all
  56.251 +
  56.252 +definition divmod :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int \<Rightarrow> Target_Numeral.int \<times> Target_Numeral.int" where
  56.253 +  "divmod k l = (k div l, k mod l)"
  56.254 +
  56.255 +lemma fst_divmod [simp]:
  56.256 +  "fst (divmod k l) = k div l"
  56.257 +  by (simp add: divmod_def)
  56.258 +
  56.259 +lemma snd_divmod [simp]:
  56.260 +  "snd (divmod k l) = k mod l"
  56.261 +  by (simp add: divmod_def)
  56.262 +
  56.263 +definition divmod_abs :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int \<Rightarrow> Target_Numeral.int \<times> Target_Numeral.int" where
  56.264 +  "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
  56.265 +
  56.266 +lemma fst_divmod_abs [simp]:
  56.267 +  "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
  56.268 +  by (simp add: divmod_abs_def)
  56.269 +
  56.270 +lemma snd_divmod_abs [simp]:
  56.271 +  "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
  56.272 +  by (simp add: divmod_abs_def)
  56.273 +
  56.274 +lemma divmod_abs_terminate_code [code]:
  56.275 +  "divmod_abs (Neg k) (Neg l) = divmod_abs (Pos k) (Pos l)"
  56.276 +  "divmod_abs (Neg k) (Pos l) = divmod_abs (Pos k) (Pos l)"
  56.277 +  "divmod_abs (Pos k) (Neg l) = divmod_abs (Pos k) (Pos l)"
  56.278 +  "divmod_abs j 0 = (0, \<bar>j\<bar>)"
  56.279 +  "divmod_abs 0 j = (0, 0)"
  56.280 +  by (simp_all add: prod_eq_iff)
  56.281 +
  56.282 +lemma divmod_abs_rec_code [code]:
  56.283 +  "divmod_abs (Pos k) (Pos l) =
  56.284 +    (let j = sub k l in
  56.285 +       if j < 0 then (0, Pos k)
  56.286 +       else let (q, r) = divmod_abs j (Pos l) in (q + 1, r))"
  56.287 +  by (auto simp add: prod_eq_iff Target_Numeral.int_eq_iff Let_def prod_case_beta
  56.288 +    sub_non_negative sub_negative div_pos_pos_trivial mod_pos_pos_trivial div_pos_geq mod_pos_geq)
  56.289 +
  56.290 +lemma divmod_code [code]: "divmod k l =
  56.291 +  (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  56.292 +  (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
  56.293 +    then divmod_abs k l
  56.294 +    else (let (r, s) = divmod_abs k l in
  56.295 +      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  56.296 +proof -
  56.297 +  have aux1: "\<And>k l::int. sgn k = sgn l \<longleftrightarrow> k = 0 \<and> l = 0 \<or> 0 < l \<and> 0 < k \<or> l < 0 \<and> k < 0"
  56.298 +    by (auto simp add: sgn_if)
  56.299 +  have aux2: "\<And>q::int. - int_of k = int_of l * q \<longleftrightarrow> int_of k = int_of l * - q" by auto
  56.300 +  show ?thesis
  56.301 +    by (simp add: prod_eq_iff Target_Numeral.int_eq_iff prod_case_beta aux1)
  56.302 +      (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if zdiv_zminus2 zmod_zminus2 aux2)
  56.303 +qed
  56.304 +
  56.305 +lemma div_int_code [code]:
  56.306 +  "k div l = fst (divmod k l)"
  56.307 +  by simp
  56.308 +
  56.309 +lemma div_mod_code [code]:
  56.310 +  "k mod l = snd (divmod k l)"
  56.311 +  by simp
  56.312 +
  56.313 +lemma equal_int_code [code]:
  56.314 +  "HOL.equal 0 (0::Target_Numeral.int) \<longleftrightarrow> True"
  56.315 +  "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
  56.316 +  "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
  56.317 +  "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
  56.318 +  "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
  56.319 +  "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
  56.320 +  "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
  56.321 +  "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
  56.322 +  "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
  56.323 +  by (simp_all add: equal Target_Numeral.int_eq_iff)
  56.324 +
  56.325 +lemma equal_int_refl [code nbe]:
  56.326 +  "HOL.equal (k::Target_Numeral.int) k \<longleftrightarrow> True"
  56.327 +  by (fact equal_refl)
  56.328 +
  56.329 +lemma less_eq_int_code [code]:
  56.330 +  "0 \<le> (0::Target_Numeral.int) \<longleftrightarrow> True"
  56.331 +  "0 \<le> Pos l \<longleftrightarrow> True"
  56.332 +  "0 \<le> Neg l \<longleftrightarrow> False"
  56.333 +  "Pos k \<le> 0 \<longleftrightarrow> False"
  56.334 +  "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
  56.335 +  "Pos k \<le> Neg l \<longleftrightarrow> False"
  56.336 +  "Neg k \<le> 0 \<longleftrightarrow> True"
  56.337 +  "Neg k \<le> Pos l \<longleftrightarrow> True"
  56.338 +  "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
  56.339 +  by (simp_all add: less_eq_int_def)
  56.340 +
  56.341 +lemma less_int_code [code]:
  56.342 +  "0 < (0::Target_Numeral.int) \<longleftrightarrow> False"
  56.343 +  "0 < Pos l \<longleftrightarrow> True"
  56.344 +  "0 < Neg l \<longleftrightarrow> False"
  56.345 +  "Pos k < 0 \<longleftrightarrow> False"
  56.346 +  "Pos k < Pos l \<longleftrightarrow> k < l"
  56.347 +  "Pos k < Neg l \<longleftrightarrow> False"
  56.348 +  "Neg k < 0 \<longleftrightarrow> True"
  56.349 +  "Neg k < Pos l \<longleftrightarrow> True"
  56.350 +  "Neg k < Neg l \<longleftrightarrow> l < k"
  56.351 +  by (simp_all add: less_int_def)
  56.352 +
  56.353 +lemma nat_of_code [code]:
  56.354 +  "nat_of (Neg k) = 0"
  56.355 +  "nat_of 0 = 0"
  56.356 +  "nat_of (Pos k) = nat_of_num k"
  56.357 +  by (simp_all add: nat_of_def nat_of_num_numeral)
  56.358 +
  56.359 +lemma int_of_code [code]:
  56.360 +  "int_of (Neg k) = neg_numeral k"
  56.361 +  "int_of 0 = 0"
  56.362 +  "int_of (Pos k) = numeral k"
  56.363 +  by simp_all
  56.364 +
  56.365 +lemma of_int_code [code]:
  56.366 +  "Target_Numeral.of_int (Int.Neg k) = neg_numeral k"
  56.367 +  "Target_Numeral.of_int 0 = 0"
  56.368 +  "Target_Numeral.of_int (Int.Pos k) = numeral k"
  56.369 +  by simp_all
  56.370 +
  56.371 +definition num_of_int :: "Target_Numeral.int \<Rightarrow> num" where
  56.372 +  "num_of_int = num_of_nat \<circ> nat_of"
  56.373 +
  56.374 +lemma num_of_int_code [code]:
  56.375 +  "num_of_int k = (if k \<le> 1 then Num.One
  56.376 +     else let
  56.377 +       (l, j) = divmod k 2;
  56.378 +       l' = num_of_int l + num_of_int l
  56.379 +     in if j = 0 then l' else l' + Num.One)"
  56.380 +proof -
  56.381 +  {
  56.382 +    assume "int_of k mod 2 = 1"
  56.383 +    then have "nat (int_of k mod 2) = nat 1" by simp
  56.384 +    moreover assume *: "1 < int_of k"
  56.385 +    ultimately have **: "nat (int_of k) mod 2 = 1" by (simp add: nat_mod_distrib)
  56.386 +    have "num_of_nat (nat (int_of k)) =
  56.387 +      num_of_nat (2 * (nat (int_of k) div 2) + nat (int_of k) mod 2)"
  56.388 +      by simp
  56.389 +    then have "num_of_nat (nat (int_of k)) =
  56.390 +      num_of_nat (nat (int_of k) div 2 + nat (int_of k) div 2 + nat (int_of k) mod 2)"
  56.391 +      by (simp add: nat_mult_2)
  56.392 +    with ** have "num_of_nat (nat (int_of k)) =
  56.393 +      num_of_nat (nat (int_of k) div 2 + nat (int_of k) div 2 + 1)"
  56.394 +      by simp
  56.395 +  }
  56.396 +  note aux = this
  56.397 +  show ?thesis
  56.398 +    by (auto simp add: num_of_int_def nat_of_def Let_def prod_case_beta
  56.399 +      not_le Target_Numeral.int_eq_iff less_eq_int_def
  56.400 +      nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
  56.401 +       nat_mult_2 aux add_One)
  56.402 +qed
  56.403 +
  56.404 +hide_const (open) int_of nat_of Pos Neg sub dup divmod_abs num_of_int
  56.405 +
  56.406 +
  56.407 +subsection {* Serializer setup for target language numerals *}
  56.408 +
  56.409 +code_type Target_Numeral.int
  56.410 +  (SML "IntInf.int")
  56.411 +  (OCaml "Big'_int.big'_int")
  56.412 +  (Haskell "Integer")
  56.413 +  (Scala "BigInt")
  56.414 +  (Eval "int")
  56.415 +
  56.416 +code_instance Target_Numeral.int :: equal
  56.417 +  (Haskell -)
  56.418 +
  56.419 +code_const "0::Target_Numeral.int"
  56.420 +  (SML "0")
  56.421 +  (OCaml "Big'_int.zero'_big'_int")
  56.422 +  (Haskell "0")
  56.423 +  (Scala "BigInt(0)")
  56.424 +
  56.425 +setup {*
  56.426 +  fold (Numeral.add_code @{const_name Target_Numeral.Pos}
  56.427 +    false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
  56.428 +*}
  56.429 +
  56.430 +setup {*
  56.431 +  fold (Numeral.add_code @{const_name Target_Numeral.Neg}
  56.432 +    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
  56.433 +*}
  56.434 +
  56.435 +code_const "plus :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> _"
  56.436 +  (SML "IntInf.+ ((_), (_))")
  56.437 +  (OCaml "Big'_int.add'_big'_int")
  56.438 +  (Haskell infixl 6 "+")
  56.439 +  (Scala infixl 7 "+")
  56.440 +  (Eval infixl 8 "+")
  56.441 +
  56.442 +code_const "uminus :: Target_Numeral.int \<Rightarrow> _"
  56.443 +  (SML "IntInf.~")
  56.444 +  (OCaml "Big'_int.minus'_big'_int")
  56.445 +  (Haskell "negate")
  56.446 +  (Scala "!(- _)")
  56.447 +  (Eval "~/ _")
  56.448 +
  56.449 +code_const "minus :: Target_Numeral.int \<Rightarrow> _"
  56.450 +  (SML "IntInf.- ((_), (_))")
  56.451 +  (OCaml "Big'_int.sub'_big'_int")
  56.452 +  (Haskell infixl 6 "-")
  56.453 +  (Scala infixl 7 "-")
  56.454 +  (Eval infixl 8 "-")
  56.455 +
  56.456 +code_const Target_Numeral.dup
  56.457 +  (SML "IntInf.*/ (2,/ (_))")
  56.458 +  (OCaml "Big'_int.mult'_big'_int/ 2")
  56.459 +  (Haskell "!(2 * _)")
  56.460 +  (Scala "!(2 * _)")
  56.461 +  (Eval "!(2 * _)")
  56.462 +
  56.463 +code_const Target_Numeral.sub
  56.464 +  (SML "!(raise/ Fail/ \"sub\")")
  56.465 +  (OCaml "failwith/ \"sub\"")
  56.466 +  (Haskell "error/ \"sub\"")
  56.467 +  (Scala "!error(\"sub\")")
  56.468 +
  56.469 +code_const "times :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> _"
  56.470 +  (SML "IntInf.* ((_), (_))")
  56.471 +  (OCaml "Big'_int.mult'_big'_int")
  56.472 +  (Haskell infixl 7 "*")
  56.473 +  (Scala infixl 8 "*")
  56.474 +  (Eval infixl 9 "*")
  56.475 +
  56.476 +code_const Target_Numeral.divmod_abs
  56.477 +  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
  56.478 +  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
  56.479 +  (Haskell "divMod/ (abs _)/ (abs _)")
  56.480 +  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
  56.481 +  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
  56.482 +
  56.483 +code_const "HOL.equal :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
  56.484 +  (SML "!((_ : IntInf.int) = _)")
  56.485 +  (OCaml "Big'_int.eq'_big'_int")
  56.486 +  (Haskell infix 4 "==")
  56.487 +  (Scala infixl 5 "==")
  56.488 +  (Eval infixl 6 "=")
  56.489 +
  56.490 +code_const "less_eq :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
  56.491 +  (SML "IntInf.<= ((_), (_))")
  56.492 +  (OCaml "Big'_int.le'_big'_int")
  56.493 +  (Haskell infix 4 "<=")
  56.494 +  (Scala infixl 4 "<=")
  56.495 +  (Eval infixl 6 "<=")
  56.496 +
  56.497 +code_const "less :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
  56.498 +  (SML "IntInf.< ((_), (_))")
  56.499 +  (OCaml "Big'_int.lt'_big'_int")
  56.500 +  (Haskell infix 4 "<")
  56.501 +  (Scala infixl 4 "<")
  56.502 +  (Eval infixl 6 "<")
  56.503 +
  56.504 +ML {*
  56.505 +structure Target_Numeral =
  56.506 +struct
  56.507 +
  56.508 +val T = @{typ "Target_Numeral.int"};
  56.509 +
  56.510 +end;
  56.511 +*}
  56.512 +
  56.513 +code_reserved Eval Target_Numeral
  56.514 +
  56.515 +code_const "Code_Evaluation.term_of \<Colon> Target_Numeral.int \<Rightarrow> term"
  56.516 +  (Eval "HOLogic.mk'_number/ Target'_Numeral.T")
  56.517 +
  56.518 +code_modulename SML
  56.519 +  Target_Numeral Arith
  56.520 +
  56.521 +code_modulename OCaml
  56.522 +  Target_Numeral Arith
  56.523 +
  56.524 +code_modulename Haskell
  56.525 +  Target_Numeral Arith
  56.526 +
  56.527 +
  56.528 +subsection {* Implementation for @{typ int} *}
  56.529 +
  56.530 +code_datatype Target_Numeral.int_of
  56.531 +
  56.532 +lemma [code, code del]:
  56.533 +  "Target_Numeral.of_int = Target_Numeral.of_int" ..
  56.534 +
  56.535 +lemma [code]:
  56.536 +  "Target_Numeral.of_int (Target_Numeral.int_of k) = k"
  56.537 +  by (simp add: Target_Numeral.int_eq_iff)
  56.538 +
  56.539 +declare Int.Pos_def [code]
  56.540 +
  56.541 +lemma [code_abbrev]:
  56.542 +  "Target_Numeral.int_of (Target_Numeral.Pos k) = Int.Pos k"
  56.543 +  by simp
  56.544 +
  56.545 +declare Int.Neg_def [code]
  56.546 +
  56.547 +lemma [code_abbrev]:
  56.548 +  "Target_Numeral.int_of (Target_Numeral.Neg k) = Int.Neg k"
  56.549 +  by simp
  56.550 +
  56.551 +lemma [code]:
  56.552 +  "0 = Target_Numeral.int_of 0"
  56.553 +  by simp
  56.554 +
  56.555 +lemma [code]:
  56.556 +  "1 = Target_Numeral.int_of 1"
  56.557 +  by simp
  56.558 +
  56.559 +lemma [code]:
  56.560 +  "k + l = Target_Numeral.int_of (of_int k + of_int l)"
  56.561 +  by simp
  56.562 +
  56.563 +lemma [code]:
  56.564 +  "- k = Target_Numeral.int_of (- of_int k)"
  56.565 +  by simp
  56.566 +
  56.567 +lemma [code]:
  56.568 +  "k - l = Target_Numeral.int_of (of_int k - of_int l)"
  56.569 +  by simp
  56.570 +
  56.571 +lemma [code]:
  56.572 +  "Int.dup k = Target_Numeral.int_of (Target_Numeral.dup (of_int k))"
  56.573 +  by simp
  56.574 +
  56.575 +lemma [code, code del]:
  56.576 +  "Int.sub = Int.sub" ..
  56.577 +
  56.578 +lemma [code]:
  56.579 +  "k * l = Target_Numeral.int_of (of_int k * of_int l)"
  56.580 +  by simp
  56.581 +
  56.582 +lemma [code]:
  56.583 +  "pdivmod k l = map_pair Target_Numeral.int_of Target_Numeral.int_of
  56.584 +    (Target_Numeral.divmod_abs (of_int k) (of_int l))"
  56.585 +  by (simp add: prod_eq_iff pdivmod_def)
  56.586 +
  56.587 +lemma [code]:
  56.588 +  "k div l = Target_Numeral.int_of (of_int k div of_int l)"
  56.589 +  by simp
  56.590 +
  56.591 +lemma [code]:
  56.592 +  "k mod l = Target_Numeral.int_of (of_int k mod of_int l)"
  56.593 +  by simp
  56.594 +
  56.595 +lemma [code]:
  56.596 +  "HOL.equal k l = HOL.equal (of_int k :: Target_Numeral.int) (of_int l)"
  56.597 +  by (simp add: equal Target_Numeral.int_eq_iff)
  56.598 +
  56.599 +lemma [code]:
  56.600 +  "k \<le> l \<longleftrightarrow> (of_int k :: Target_Numeral.int) \<le> of_int l"
  56.601 +  by (simp add: less_eq_int_def)
  56.602 +
  56.603 +lemma [code]:
  56.604 +  "k < l \<longleftrightarrow> (of_int k :: Target_Numeral.int) < of_int l"
  56.605 +  by (simp add: less_int_def)
  56.606 +
  56.607 +lemma (in ring_1) of_int_code:
  56.608 +  "of_int k = (if k = 0 then 0
  56.609 +     else if k < 0 then - of_int (- k)
  56.610 +     else let
  56.611 +       (l, j) = divmod_int k 2;
  56.612 +       l' = 2 * of_int l
  56.613 +     in if j = 0 then l' else l' + 1)"
  56.614 +proof -
  56.615 +  from mod_div_equality have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
  56.616 +  show ?thesis
  56.617 +    by (simp add: Let_def divmod_int_mod_div mod_2_not_eq_zero_eq_one_int
  56.618 +      of_int_add [symmetric]) (simp add: * mult_commute)
  56.619 +qed
  56.620 +
  56.621 +declare of_int_code [code]
  56.622 +
  56.623 +
  56.624 +subsection {* Implementation for @{typ nat} *}
  56.625 +
  56.626 +definition of_nat :: "nat \<Rightarrow> Target_Numeral.int" where
  56.627 +  [code_abbrev]: "of_nat = Nat.of_nat"
  56.628 +
  56.629 +hide_const (open) of_nat
  56.630 +
  56.631 +lemma int_of_nat [simp]:
  56.632 +  "Target_Numeral.int_of (Target_Numeral.of_nat n) = of_nat n"
  56.633 +  by (simp add: of_nat_def)
  56.634 +
  56.635 +lemma [code abstype]:
  56.636 +  "Target_Numeral.nat_of (Target_Numeral.of_nat n) = n"
  56.637 +  by (simp add: nat_of_def)
  56.638 +
  56.639 +lemma [code_abbrev]:
  56.640 +  "nat (Int.Pos k) = nat_of_num k"
  56.641 +  by (simp add: nat_of_num_numeral)
  56.642 +
  56.643 +lemma [code abstract]:
  56.644 +  "Target_Numeral.of_nat 0 = 0"
  56.645 +  by (simp add: Target_Numeral.int_eq_iff)
  56.646 +
  56.647 +lemma [code abstract]:
  56.648 +  "Target_Numeral.of_nat 1 = 1"
  56.649 +  by (simp add: Target_Numeral.int_eq_iff)
  56.650 +
  56.651 +lemma [code abstract]:
  56.652 +  "Target_Numeral.of_nat (m + n) = of_nat m + of_nat n"
  56.653 +  by (simp add: Target_Numeral.int_eq_iff)
  56.654 +
  56.655 +lemma [code abstract]:
  56.656 +  "Target_Numeral.of_nat (Code_Nat.dup n) = Target_Numeral.dup (of_nat n)"
  56.657 +  by (simp add: Target_Numeral.int_eq_iff Code_Nat.dup_def)
  56.658 +
  56.659 +lemma [code, code del]:
  56.660 +  "Code_Nat.sub = Code_Nat.sub" ..
  56.661 +
  56.662 +lemma [code abstract]:
  56.663 +  "Target_Numeral.of_nat (m - n) = max 0 (of_nat m - of_nat n)"
  56.664 +  by (simp add: Target_Numeral.int_eq_iff)
  56.665 +
  56.666 +lemma [code abstract]:
  56.667 +  "Target_Numeral.of_nat (m * n) = of_nat m * of_nat n"
  56.668 +  by (simp add: Target_Numeral.int_eq_iff of_nat_mult)
  56.669 +
  56.670 +lemma [code abstract]:
  56.671 +  "Target_Numeral.of_nat (m div n) = of_nat m div of_nat n"
  56.672 +  by (simp add: Target_Numeral.int_eq_iff zdiv_int)
  56.673 +
  56.674 +lemma [code abstract]:
  56.675 +  "Target_Numeral.of_nat (m mod n) = of_nat m mod of_nat n"
  56.676 +  by (simp add: Target_Numeral.int_eq_iff zmod_int)
  56.677 +
  56.678 +lemma [code]:
  56.679 +  "Divides.divmod_nat m n = (m div n, m mod n)"
  56.680 +  by (simp add: prod_eq_iff)
  56.681 +
  56.682 +lemma [code]:
  56.683 +  "HOL.equal m n = HOL.equal (of_nat m :: Target_Numeral.int) (of_nat n)"
  56.684 +  by (simp add: equal Target_Numeral.int_eq_iff)
  56.685 +
  56.686 +lemma [code]:
  56.687 +  "m \<le> n \<longleftrightarrow> (of_nat m :: Target_Numeral.int) \<le> of_nat n"
  56.688 +  by (simp add: less_eq_int_def)
  56.689 +
  56.690 +lemma [code]:
  56.691 +  "m < n \<longleftrightarrow> (of_nat m :: Target_Numeral.int) < of_nat n"
  56.692 +  by (simp add: less_int_def)
  56.693 +
  56.694 +lemma num_of_nat_code [code]:
  56.695 +  "num_of_nat = Target_Numeral.num_of_int \<circ> Target_Numeral.of_nat"
  56.696 +  by (simp add: fun_eq_iff num_of_int_def of_nat_def)
  56.697 +
  56.698 +lemma (in semiring_1) of_nat_code:
  56.699 +  "of_nat n = (if n = 0 then 0
  56.700 +     else let
  56.701 +       (m, q) = divmod_nat n 2;
  56.702 +       m' = 2 * of_nat m
  56.703 +     in if q = 0 then m' else m' + 1)"
  56.704 +proof -
  56.705 +  from mod_div_equality have *: "of_nat n = of_nat (n div 2 * 2 + n mod 2)" by simp
  56.706 +  show ?thesis
  56.707 +    by (simp add: Let_def divmod_nat_div_mod mod_2_not_eq_zero_eq_one_nat
  56.708 +      of_nat_add [symmetric])
  56.709 +      (simp add: * mult_commute of_nat_mult add_commute)
  56.710 +qed
  56.711 +
  56.712 +declare of_nat_code [code]
  56.713 +
  56.714 +text {* Conversions between @{typ nat} and @{typ int} *}
  56.715 +
  56.716 +definition int :: "nat \<Rightarrow> int" where
  56.717 +  [code_abbrev]: "int = of_nat"
  56.718 +
  56.719 +hide_const (open) int
  56.720 +
  56.721 +lemma [code]:
  56.722 +  "Target_Numeral.int n = Target_Numeral.int_of (of_nat n)"
  56.723 +  by (simp add: int_def)
  56.724 +
  56.725 +lemma [code abstract]:
  56.726 +  "Target_Numeral.of_nat (nat k) = max 0 (Target_Numeral.of_int k)"
  56.727 +  by (simp add: of_nat_def of_int_of_nat max_def)
  56.728 +
  56.729 +end
    57.1 --- a/src/HOL/List.thy	Fri Mar 23 20:32:43 2012 +0100
    57.2 +++ b/src/HOL/List.thy	Mon Mar 26 10:56:56 2012 +0200
    57.3 @@ -2676,7 +2676,7 @@
    57.4  -- {* simp does not terminate! *}
    57.5  by (induct j) auto
    57.6  
    57.7 -lemmas upt_rec_number_of[simp] = upt_rec[of "number_of m" "number_of n"] for m n
    57.8 +lemmas upt_rec_numeral[simp] = upt_rec[of "numeral m" "numeral n"] for m n
    57.9  
   57.10  lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
   57.11  by (subst upt_rec) simp
   57.12 @@ -2791,13 +2791,17 @@
   57.13  lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
   57.14  by (cases n) simp_all
   57.15  
   57.16 -lemmas take_Cons_number_of = take_Cons'[of "number_of v"] for v
   57.17 -lemmas drop_Cons_number_of = drop_Cons'[of "number_of v"] for v
   57.18 -lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v"] for v
   57.19 -
   57.20 -declare take_Cons_number_of [simp] 
   57.21 -        drop_Cons_number_of [simp] 
   57.22 -        nth_Cons_number_of [simp] 
   57.23 +lemma take_Cons_numeral [simp]:
   57.24 +  "take (numeral v) (x # xs) = x # take (numeral v - 1) xs"
   57.25 +by (simp add: take_Cons')
   57.26 +
   57.27 +lemma drop_Cons_numeral [simp]:
   57.28 +  "drop (numeral v) (x # xs) = drop (numeral v - 1) xs"
   57.29 +by (simp add: drop_Cons')
   57.30 +
   57.31 +lemma nth_Cons_numeral [simp]:
   57.32 +  "(x # xs) ! numeral v = xs ! (numeral v - 1)"
   57.33 +by (simp add: nth_Cons')
   57.34  
   57.35  
   57.36  subsubsection {* @{text upto}: interval-list on @{typ int} *}
   57.37 @@ -2812,7 +2816,11 @@
   57.38  
   57.39  declare upto.simps[code, simp del]
   57.40  
   57.41 -lemmas upto_rec_number_of[simp] = upto.simps[of "number_of m" "number_of n"] for m n
   57.42 +lemmas upto_rec_numeral [simp] =
   57.43 +  upto.simps[of "numeral m" "numeral n"]
   57.44 +  upto.simps[of "numeral m" "neg_numeral n"]
   57.45 +  upto.simps[of "neg_numeral m" "numeral n"]
   57.46 +  upto.simps[of "neg_numeral m" "neg_numeral n"] for m n
   57.47  
   57.48  lemma upto_empty[simp]: "j < i \<Longrightarrow> [i..j] = []"
   57.49  by(simp add: upto.simps)
    58.1 --- a/src/HOL/Matrix_LP/ComputeFloat.thy	Fri Mar 23 20:32:43 2012 +0100
    58.2 +++ b/src/HOL/Matrix_LP/ComputeFloat.thy	Mon Mar 26 10:56:56 2012 +0200
    58.3 @@ -75,8 +75,11 @@
    58.4    ultimately show ?thesis by auto
    58.5  qed
    58.6  
    58.7 -lemma real_is_int_number_of[simp]: "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
    58.8 -  by (auto simp: real_is_int_def intro!: exI[of _ "number_of x"])
    58.9 +lemma real_is_int_numeral[simp]: "real_is_int (numeral x)"
   58.10 +  by (auto simp: real_is_int_def intro!: exI[of _ "numeral x"])
   58.11 +
   58.12 +lemma real_is_int_neg_numeral[simp]: "real_is_int (neg_numeral x)"
   58.13 +  by (auto simp: real_is_int_def intro!: exI[of _ "neg_numeral x"])
   58.14  
   58.15  lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
   58.16  by (simp add: int_of_real_def)
   58.17 @@ -87,7 +90,12 @@
   58.18    show ?thesis by (simp only: 1 int_of_real_real)
   58.19  qed
   58.20  
   58.21 -lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
   58.22 +lemma int_of_real_numeral[simp]: "int_of_real (numeral b) = numeral b"
   58.23 +  unfolding int_of_real_def
   58.24 +  by (intro some_equality)
   58.25 +     (auto simp add: real_of_int_inject[symmetric] simp del: real_of_int_inject)
   58.26 +
   58.27 +lemma int_of_real_neg_numeral[simp]: "int_of_real (neg_numeral b) = neg_numeral b"
   58.28    unfolding int_of_real_def
   58.29    by (intro some_equality)
   58.30       (auto simp add: real_of_int_inject[symmetric] simp del: real_of_int_inject)
   58.31 @@ -101,7 +109,7 @@
   58.32  lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
   58.33  by arith
   58.34  
   58.35 -lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
   58.36 +lemma norm_0_1: "(1::_::numeral) = Numeral1"
   58.37    by auto
   58.38  
   58.39  lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
   58.40 @@ -116,34 +124,21 @@
   58.41  lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
   58.42    by simp
   58.43  
   58.44 -lemma int_pow_0: "(a::int)^(Numeral0) = 1"
   58.45 +lemma int_pow_0: "(a::int)^0 = 1"
   58.46    by simp
   58.47  
   58.48  lemma int_pow_1: "(a::int)^(Numeral1) = a"
   58.49    by simp
   58.50  
   58.51 -lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
   58.52 -  by simp
   58.53 -
   58.54 -lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
   58.55 -  by simp
   58.56 -
   58.57 -lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
   58.58 +lemma one_eq_Numeral1_nring: "(1::'a::numeral) = Numeral1"
   58.59    by simp
   58.60  
   58.61  lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
   58.62    by simp
   58.63  
   58.64 -lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
   58.65 +lemma zpower_Pls: "(z::int)^0 = Numeral1"
   58.66    by simp
   58.67  
   58.68 -lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
   58.69 -proof -
   58.70 -  have 1:"((-1)::nat) = 0"
   58.71 -    by simp
   58.72 -  show ?thesis by (simp add: 1)
   58.73 -qed
   58.74 -
   58.75  lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
   58.76    by simp
   58.77  
   58.78 @@ -160,70 +155,8 @@
   58.79  
   58.80  lemma not_true_eq_false: "(~ True) = False" by simp
   58.81  
   58.82 -lemmas binarith =
   58.83 -  normalize_bin_simps
   58.84 -  pred_bin_simps succ_bin_simps
   58.85 -  add_bin_simps minus_bin_simps mult_bin_simps
   58.86 -
   58.87 -lemma int_eq_number_of_eq:
   58.88 -  "(((number_of v)::int)=(number_of w)) = iszero ((number_of (v + uminus w))::int)"
   58.89 -  by (rule eq_number_of_eq)
   58.90 -
   58.91 -lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
   58.92 -  by (simp only: iszero_number_of_Pls)
   58.93 -
   58.94 -lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
   58.95 -  by simp
   58.96 -
   58.97 -lemma int_iszero_number_of_Bit0: "iszero ((number_of (Int.Bit0 w))::int) = iszero ((number_of w)::int)"
   58.98 -  by simp
   58.99 -
  58.100 -lemma int_iszero_number_of_Bit1: "\<not> iszero ((number_of (Int.Bit1 w))::int)"
  58.101 -  by simp
  58.102 -
  58.103 -lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (x + (uminus y)))::int)"
  58.104 -  unfolding neg_def number_of_is_id by simp
  58.105 -
  58.106 -lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
  58.107 -  by simp
  58.108 -
  58.109 -lemma int_neg_number_of_Min: "neg (-1::int)"
  58.110 -  by simp
  58.111 -
  58.112 -lemma int_neg_number_of_Bit0: "neg ((number_of (Int.Bit0 w))::int) = neg ((number_of w)::int)"
  58.113 -  by simp
  58.114 -
  58.115 -lemma int_neg_number_of_Bit1: "neg ((number_of (Int.Bit1 w))::int) = neg ((number_of w)::int)"
  58.116 -  by simp
  58.117 -
  58.118 -lemma int_le_number_of_eq: "(((number_of x)::int) \<le> number_of y) = (\<not> neg ((number_of (y + (uminus x)))::int))"
  58.119 -  unfolding neg_def number_of_is_id by (simp add: not_less)
  58.120 -
  58.121 -lemmas intarithrel =
  58.122 -  int_eq_number_of_eq
  58.123 -  lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_Bit0
  58.124 -  lift_bool[OF int_iszero_number_of_Bit1] int_less_number_of_eq_neg nlift_bool[OF int_not_neg_number_of_Pls] lift_bool[OF int_neg_number_of_Min]
  58.125 -  int_neg_number_of_Bit0 int_neg_number_of_Bit1 int_le_number_of_eq
  58.126 -
  58.127 -lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (v + w)"
  58.128 -  by simp
  58.129 -
  58.130 -lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (v + (uminus w))"
  58.131 -  by simp
  58.132 -
  58.133 -lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (v * w)"
  58.134 -  by simp
  58.135 -
  58.136 -lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (uminus v)"
  58.137 -  by simp
  58.138 -
  58.139 -lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
  58.140 -
  58.141 -lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
  58.142 -
  58.143 -lemmas powerarith = nat_number_of zpower_number_of_even
  58.144 -  zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
  58.145 -  zpower_Pls zpower_Min
  58.146 +lemmas powerarith = nat_numeral zpower_numeral_even
  58.147 +  zpower_numeral_odd zpower_Pls
  58.148  
  58.149  definition float :: "(int \<times> int) \<Rightarrow> real" where
  58.150    "float = (\<lambda>(a, b). real a * 2 powr real b)"
  58.151 @@ -302,7 +235,8 @@
  58.152            float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
  58.153  
  58.154  (* for use with the compute oracle *)
  58.155 -lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
  58.156 +lemmas arith = arith_simps rel_simps diff_nat_numeral nat_0
  58.157 +  nat_neg_numeral powerarith floatarith not_false_eq_true not_true_eq_false
  58.158  
  58.159  use "~~/src/HOL/Tools/float_arith.ML"
  58.160  
    59.1 --- a/src/HOL/Matrix_LP/ComputeNumeral.thy	Fri Mar 23 20:32:43 2012 +0100
    59.2 +++ b/src/HOL/Matrix_LP/ComputeNumeral.thy	Mon Mar 26 10:56:56 2012 +0200
    59.3 @@ -2,145 +2,47 @@
    59.4  imports ComputeHOL ComputeFloat
    59.5  begin
    59.6  
    59.7 -(* normalization of bit strings *)
    59.8 -lemmas bitnorm = normalize_bin_simps
    59.9 -
   59.10 -(* neg for bit strings *)
   59.11 -lemma neg1: "neg Int.Pls = False" by (simp add: Int.Pls_def)
   59.12 -lemma neg2: "neg Int.Min = True" apply (subst Int.Min_def) by auto
   59.13 -lemma neg3: "neg (Int.Bit0 x) = neg x" apply (simp add: neg_def) apply (subst Bit0_def) by auto
   59.14 -lemma neg4: "neg (Int.Bit1 x) = neg x" apply (simp add: neg_def) apply (subst Bit1_def) by auto  
   59.15 -lemmas bitneg = neg1 neg2 neg3 neg4
   59.16 -
   59.17 -(* iszero for bit strings *)
   59.18 -lemma iszero1: "iszero Int.Pls = True" by (simp add: Int.Pls_def iszero_def)
   59.19 -lemma iszero2: "iszero Int.Min = False" apply (subst Int.Min_def) apply (subst iszero_def) by simp
   59.20 -lemma iszero3: "iszero (Int.Bit0 x) = iszero x" apply (subst Int.Bit0_def) apply (subst iszero_def)+ by auto
   59.21 -lemma iszero4: "iszero (Int.Bit1 x) = False" apply (subst Int.Bit1_def) apply (subst iszero_def)+  apply simp by arith
   59.22 -lemmas bitiszero = iszero1 iszero2 iszero3 iszero4
   59.23 -
   59.24 -(* lezero for bit strings *)
   59.25 -definition "lezero x \<longleftrightarrow> x \<le> 0"
   59.26 -lemma lezero1: "lezero Int.Pls = True" unfolding Int.Pls_def lezero_def by auto
   59.27 -lemma lezero2: "lezero Int.Min = True" unfolding Int.Min_def lezero_def by auto
   59.28 -lemma lezero3: "lezero (Int.Bit0 x) = lezero x" unfolding Int.Bit0_def lezero_def by auto
   59.29 -lemma lezero4: "lezero (Int.Bit1 x) = neg x" unfolding Int.Bit1_def lezero_def neg_def by auto
   59.30 -lemmas bitlezero = lezero1 lezero2 lezero3 lezero4
   59.31 -
   59.32  (* equality for bit strings *)
   59.33 -lemmas biteq = eq_bin_simps
   59.34 +lemmas biteq = eq_num_simps
   59.35  
   59.36  (* x < y for bit strings *)
   59.37 -lemmas bitless = less_bin_simps
   59.38 +lemmas bitless = less_num_simps
   59.39  
   59.40  (* x \<le> y for bit strings *)
   59.41 -lemmas bitle = le_bin_simps
   59.42 -
   59.43 -(* succ for bit strings *)
   59.44 -lemmas bitsucc = succ_bin_simps
   59.45 -
   59.46 -(* pred for bit strings *)
   59.47 -lemmas bitpred = pred_bin_simps
   59.48 -
   59.49 -(* unary minus for bit strings *)
   59.50 -lemmas bituminus = minus_bin_simps
   59.51 +lemmas bitle = le_num_simps
   59.52  
   59.53  (* addition for bit strings *)
   59.54 -lemmas bitadd = add_bin_simps
   59.55 +lemmas bitadd = add_num_simps
   59.56  
   59.57  (* multiplication for bit strings *) 
   59.58 -lemma mult_Pls_right: "x * Int.Pls = Int.Pls" by (simp add: Pls_def)
   59.59 -lemma mult_Min_right: "x * Int.Min = - x" by (subst mult_commute) simp 
   59.60 -lemma multb0x: "(Int.Bit0 x) * y = Int.Bit0 (x * y)" by (rule mult_Bit0)
   59.61 -lemma multxb0: "x * (Int.Bit0 y) = Int.Bit0 (x * y)" unfolding Bit0_def by simp
   59.62 -lemma multb1: "(Int.Bit1 x) * (Int.Bit1 y) = Int.Bit1 (Int.Bit0 (x * y) + x + y)"
   59.63 -  unfolding Bit0_def Bit1_def by (simp add: algebra_simps)
   59.64 -lemmas bitmul = mult_Pls mult_Min mult_Pls_right mult_Min_right multb0x multxb0 multb1
   59.65 +lemmas bitmul = mult_num_simps
   59.66  
   59.67 -lemmas bitarith = bitnorm bitiszero bitneg bitlezero biteq bitless bitle bitsucc bitpred bituminus bitadd bitmul 
   59.68 -
   59.69 -definition "nat_norm_number_of (x::nat) = x"
   59.70 -
   59.71 -lemma nat_norm_number_of: "nat_norm_number_of (number_of w) = (if lezero w then 0 else number_of w)"
   59.72 -  apply (simp add: nat_norm_number_of_def)
   59.73 -  unfolding lezero_def iszero_def neg_def
   59.74 -  apply (simp add: numeral_simps)
   59.75 -  done
   59.76 +lemmas bitarith = arith_simps
   59.77  
   59.78  (* Normalization of nat literals *)
   59.79 -lemma natnorm0: "(0::nat) = number_of (Int.Pls)" by auto
   59.80 -lemma natnorm1: "(1 :: nat) = number_of (Int.Bit1 Int.Pls)"  by auto 
   59.81 -lemmas natnorm = natnorm0 natnorm1 nat_norm_number_of
   59.82 -
   59.83 -(* Suc *)
   59.84 -lemma natsuc: "Suc (number_of x) = (if neg x then 1 else number_of (Int.succ x))" by (auto simp add: number_of_is_id)
   59.85 -
   59.86 -(* Addition for nat *)
   59.87 -lemma natadd: "number_of x + ((number_of y)::nat) = (if neg x then (number_of y) else (if neg y then number_of x else (number_of (x + y))))"
   59.88 -  unfolding nat_number_of_def number_of_is_id neg_def
   59.89 -  by auto
   59.90 -
   59.91 -(* Subtraction for nat *)
   59.92 -lemma natsub: "(number_of x) - ((number_of y)::nat) = 
   59.93 -  (if neg x then 0 else (if neg y then number_of x else (nat_norm_number_of (number_of (x + (- y))))))"
   59.94 -  unfolding nat_norm_number_of
   59.95 -  by (auto simp add: number_of_is_id neg_def lezero_def iszero_def Let_def nat_number_of_def)
   59.96 -
   59.97 -(* Multiplication for nat *)
   59.98 -lemma natmul: "(number_of x) * ((number_of y)::nat) = 
   59.99 -  (if neg x then 0 else (if neg y then 0 else number_of (x * y)))"
  59.100 -  unfolding nat_number_of_def number_of_is_id neg_def
  59.101 -  by (simp add: nat_mult_distrib)
  59.102 -
  59.103 -lemma nateq: "(((number_of x)::nat) = (number_of y)) = ((lezero x \<and> lezero y) \<or> (x = y))"
  59.104 -  by (auto simp add: iszero_def lezero_def neg_def number_of_is_id)
  59.105 -
  59.106 -lemma natless: "(((number_of x)::nat) < (number_of y)) = ((x < y) \<and> (\<not> (lezero y)))"
  59.107 -  by (simp add: lezero_def numeral_simps not_le)
  59.108 -
  59.109 -lemma natle: "(((number_of x)::nat) \<le> (number_of y)) = (y < x \<longrightarrow> lezero x)"
  59.110 -  by (auto simp add: number_of_is_id lezero_def nat_number_of_def)
  59.111 +lemmas natnorm = one_eq_Numeral1_nat
  59.112  
  59.113  fun natfac :: "nat \<Rightarrow> nat"
  59.114    where "natfac n = (if n = 0 then 1 else n * (natfac (n - 1)))"
  59.115  
  59.116 -lemmas compute_natarith = bitarith natnorm natsuc natadd natsub natmul nateq natless natle natfac.simps
  59.117 +lemmas compute_natarith =
  59.118 +  arith_simps rel_simps
  59.119 +  diff_nat_numeral nat_numeral nat_0 nat_neg_numeral
  59.120 +  numeral_1_eq_1 [symmetric]
  59.121 +  numeral_1_eq_Suc_0 [symmetric]
  59.122 +  Suc_numeral natfac.simps
  59.123  
  59.124 -lemma number_eq: "(((number_of x)::'a::{number_ring, linordered_idom}) = (number_of y)) = (x = y)"
  59.125 -  unfolding number_of_eq
  59.126 -  apply simp
  59.127 -  done
  59.128 +lemmas number_norm = numeral_1_eq_1[symmetric]
  59.129  
  59.130 -lemma number_le: "(((number_of x)::'a::{number_ring, linordered_idom}) \<le>  (number_of y)) = (x \<le> y)"
  59.131 -  unfolding number_of_eq
  59.132 -  apply simp
  59.133 -  done
  59.134 +lemmas compute_numberarith =
  59.135 +  arith_simps rel_simps number_norm
  59.136  
  59.137 -lemma number_less: "(((number_of x)::'a::{number_ring, linordered_idom}) <  (number_of y)) = (x < y)"
  59.138 -  unfolding number_of_eq 
  59.139 -  apply simp
  59.140 -  done
  59.141 +lemmas compute_num_conversions =
  59.142 +  real_of_nat_numeral real_of_nat_zero
  59.143 +  nat_numeral nat_0 nat_neg_numeral
  59.144 +  real_numeral real_of_int_zero
  59.145  
  59.146 -lemma number_diff: "((number_of x)::'a::{number_ring, linordered_idom}) - number_of y = number_of (x + (- y))"
  59.147 -  apply (subst diff_number_of_eq)
  59.148 -  apply simp
  59.149 -  done
  59.150 -
  59.151 -lemmas number_norm = number_of_Pls[symmetric] numeral_1_eq_1[symmetric]
  59.152 -
  59.153 -lemmas compute_numberarith = number_of_minus[symmetric] number_of_add[symmetric] number_diff number_of_mult[symmetric] number_norm number_eq number_le number_less
  59.154 -
  59.155 -lemma compute_real_of_nat_number_of: "real ((number_of v)::nat) = (if neg v then 0 else number_of v)"
  59.156 -  by (simp only: real_of_nat_number_of number_of_is_id)
  59.157 -
  59.158 -lemma compute_nat_of_int_number_of: "nat ((number_of v)::int) = (number_of v)"
  59.159 -  by simp
  59.160 -
  59.161 -lemmas compute_num_conversions = compute_real_of_nat_number_of compute_nat_of_int_number_of real_number_of
  59.162 -
  59.163 -lemmas zpowerarith = zpower_number_of_even
  59.164 -  zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
  59.165 -  zpower_Pls zpower_Min
  59.166 +lemmas zpowerarith = zpower_numeral_even zpower_numeral_odd zpower_Pls int_pow_1
  59.167  
  59.168  (* div, mod *)
  59.169  
  59.170 @@ -162,26 +64,19 @@
  59.171  
  59.172  (* collecting all the theorems *)
  59.173  
  59.174 -lemma even_Pls: "even (Int.Pls) = True"
  59.175 -  apply (unfold Pls_def even_def)
  59.176 +lemma even_0_int: "even (0::int) = True"
  59.177    by simp
  59.178  
  59.179 -lemma even_Min: "even (Int.Min) = False"
  59.180 -  apply (unfold Min_def even_def)
  59.181 +lemma even_One_int: "even (numeral Num.One :: int) = False"
  59.182    by simp
  59.183  
  59.184 -lemma even_B0: "even (Int.Bit0 x) = True"
  59.185 -  apply (unfold Bit0_def)
  59.186 +lemma even_Bit0_int: "even (numeral (Num.Bit0 x) :: int) = True"
  59.187    by simp
  59.188  
  59.189 -lemma even_B1: "even (Int.Bit1 x) = False"
  59.190 -  apply (unfold Bit1_def)
  59.191 +lemma even_Bit1_int: "even (numeral (Num.Bit1 x) :: int) = False"
  59.192    by simp
  59.193  
  59.194 -lemma even_number_of: "even ((number_of w)::int) = even w"
  59.195 -  by (simp only: number_of_is_id)
  59.196 -
  59.197 -lemmas compute_even = even_Pls even_Min even_B0 even_B1 even_number_of
  59.198 +lemmas compute_even = even_0_int even_One_int even_Bit0_int even_Bit1_int
  59.199  
  59.200  lemmas compute_numeral = compute_if compute_let compute_pair compute_bool 
  59.201                           compute_natarith compute_numberarith max_def min_def compute_num_conversions zpowerarith compute_div_mod compute_even
    60.1 --- a/src/HOL/Matrix_LP/SparseMatrix.thy	Fri Mar 23 20:32:43 2012 +0100
    60.2 +++ b/src/HOL/Matrix_LP/SparseMatrix.thy	Mon Mar 26 10:56:56 2012 +0200
    60.3 @@ -1029,9 +1029,7 @@
    60.4    sparse_row_matrix_pprt sorted_spvec_pprt_spmat sorted_spmat_pprt_spmat
    60.5    sparse_row_matrix_nprt sorted_spvec_nprt_spmat sorted_spmat_nprt_spmat
    60.6  
    60.7 -lemma zero_eq_Numeral0: "(0::_::number_ring) = Numeral0" by simp
    60.8 -
    60.9 -lemmas sparse_row_matrix_arith_simps[simplified zero_eq_Numeral0] = 
   60.10 +lemmas sparse_row_matrix_arith_simps = 
   60.11    mult_spmat.simps mult_spvec_spmat.simps 
   60.12    addmult_spvec.simps 
   60.13    smult_spvec_empty smult_spvec_cons
    61.1 --- a/src/HOL/Metis_Examples/Big_O.thy	Fri Mar 23 20:32:43 2012 +0100
    61.2 +++ b/src/HOL/Metis_Examples/Big_O.thy	Mon Mar 26 10:56:56 2012 +0200
    61.3 @@ -16,7 +16,7 @@
    61.4  
    61.5  subsection {* Definitions *}
    61.6  
    61.7 -definition bigo :: "('a => 'b\<Colon>{linordered_idom,number_ring}) => ('a => 'b) set" ("(1O'(_'))") where
    61.8 +definition bigo :: "('a => 'b\<Colon>linordered_idom) => ('a => 'b) set" ("(1O'(_'))") where
    61.9    "O(f\<Colon>('a => 'b)) == {h. \<exists>c. \<forall>x. abs (h x) <= c * abs (f x)}"
   61.10  
   61.11  lemma bigo_pos_const:
   61.12 @@ -180,7 +180,7 @@
   61.13   apply (rule_tac x = "c + c" in exI)
   61.14   apply auto
   61.15   apply (subgoal_tac "c * abs (f xa + g xa) <= (c + c) * abs (g xa)")
   61.16 -  apply (metis order_trans semiring_mult_2)
   61.17 +  apply (metis order_trans mult_2)
   61.18   apply (subgoal_tac "c * abs (f xa + g xa) <= c * (abs (f xa) + abs (g xa))")
   61.19    apply (erule order_trans)
   61.20    apply (simp add: ring_distribs)
   61.21 @@ -325,7 +325,7 @@
   61.22  by (metis bigo_mult2 set_plus_mono_b set_times_intro2 set_times_plus_distrib)
   61.23  
   61.24  lemma bigo_mult5: "\<forall>x. f x ~= 0 \<Longrightarrow>
   61.25 -    O(f * g) <= (f\<Colon>'a => ('b\<Colon>{linordered_field,number_ring})) *o O(g)"
   61.26 +    O(f * g) <= (f\<Colon>'a => ('b\<Colon>linordered_field)) *o O(g)"
   61.27  proof -
   61.28    assume a: "\<forall>x. f x ~= 0"
   61.29    show "O(f * g) <= f *o O(g)"
   61.30 @@ -351,21 +351,21 @@
   61.31  qed
   61.32  
   61.33  lemma bigo_mult6:
   61.34 -"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = (f\<Colon>'a \<Rightarrow> ('b\<Colon>{linordered_field,number_ring})) *o O(g)"
   61.35 +"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = (f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) *o O(g)"
   61.36  by (metis bigo_mult2 bigo_mult5 order_antisym)
   61.37  
   61.38  (*proof requires relaxing relevance: 2007-01-25*)
   61.39  declare bigo_mult6 [simp]
   61.40  
   61.41  lemma bigo_mult7:
   61.42 -"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) \<le> O(f\<Colon>'a \<Rightarrow> ('b\<Colon>{linordered_field,number_ring})) \<otimes> O(g)"
   61.43 +"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) \<le> O(f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) \<otimes> O(g)"
   61.44  by (metis bigo_refl bigo_mult6 set_times_mono3)
   61.45  
   61.46  declare bigo_mult6 [simp del]
   61.47  declare bigo_mult7 [intro!]
   61.48  
   61.49  lemma bigo_mult8:
   61.50 -"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = O(f\<Colon>'a \<Rightarrow> ('b\<Colon>{linordered_field,number_ring})) \<otimes> O(g)"
   61.51 +"\<forall>x. f x \<noteq> 0 \<Longrightarrow> O(f * g) = O(f\<Colon>'a \<Rightarrow> ('b\<Colon>linordered_field)) \<otimes> O(g)"
   61.52  by (metis bigo_mult bigo_mult7 order_antisym_conv)
   61.53  
   61.54  lemma bigo_minus [intro]: "f : O(g) \<Longrightarrow> - f : O(g)"
   61.55 @@ -405,14 +405,14 @@
   61.56  lemma bigo_const2 [intro]: "O(\<lambda>x. c) \<le> O(\<lambda>x. 1)"
   61.57  by (metis bigo_const1 bigo_elt_subset)
   61.58  
   61.59 -lemma bigo_const3: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> (\<lambda>x. 1) : O(\<lambda>x. c)"
   61.60 +lemma bigo_const3: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow> (\<lambda>x. 1) : O(\<lambda>x. c)"
   61.61  apply (simp add: bigo_def)
   61.62  by (metis abs_eq_0 left_inverse order_refl)
   61.63  
   61.64 -lemma bigo_const4: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> O(\<lambda>x. 1) <= O(\<lambda>x. c)"
   61.65 +lemma bigo_const4: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow> O(\<lambda>x. 1) <= O(\<lambda>x. c)"
   61.66  by (metis bigo_elt_subset bigo_const3)
   61.67  
   61.68 -lemma bigo_const [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
   61.69 +lemma bigo_const [simp]: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow>
   61.70      O(\<lambda>x. c) = O(\<lambda>x. 1)"
   61.71  by (metis bigo_const2 bigo_const4 equalityI)
   61.72  
   61.73 @@ -423,19 +423,19 @@
   61.74  lemma bigo_const_mult2: "O(\<lambda>x. c * f x) \<le> O(f)"
   61.75  by (rule bigo_elt_subset, rule bigo_const_mult1)
   61.76  
   61.77 -lemma bigo_const_mult3: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow> f : O(\<lambda>x. c * f x)"
   61.78 +lemma bigo_const_mult3: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow> f : O(\<lambda>x. c * f x)"
   61.79  apply (simp add: bigo_def)
   61.80  by (metis (no_types) abs_mult mult_assoc mult_1 order_refl left_inverse)
   61.81  
   61.82  lemma bigo_const_mult4:
   61.83 -"(c\<Colon>'a\<Colon>{linordered_field,number_ring}) \<noteq> 0 \<Longrightarrow> O(f) \<le> O(\<lambda>x. c * f x)"
   61.84 +"(c\<Colon>'a\<Colon>linordered_field) \<noteq> 0 \<Longrightarrow> O(f) \<le> O(\<lambda>x. c * f x)"
   61.85  by (metis bigo_elt_subset bigo_const_mult3)
   61.86  
   61.87 -lemma bigo_const_mult [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
   61.88 +lemma bigo_const_mult [simp]: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow>
   61.89      O(\<lambda>x. c * f x) = O(f)"
   61.90  by (metis equalityI bigo_const_mult2 bigo_const_mult4)
   61.91  
   61.92 -lemma bigo_const_mult5 [simp]: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
   61.93 +lemma bigo_const_mult5 [simp]: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow>
   61.94      (\<lambda>x. c) *o O(f) = O(f)"
   61.95    apply (auto del: subsetI)
   61.96    apply (rule order_trans)
   61.97 @@ -587,7 +587,7 @@
   61.98    apply assumption+
   61.99  done
  61.100  
  61.101 -lemma bigo_useful_const_mult: "(c\<Colon>'a\<Colon>{linordered_field,number_ring}) ~= 0 \<Longrightarrow>
  61.102 +lemma bigo_useful_const_mult: "(c\<Colon>'a\<Colon>linordered_field) ~= 0 \<Longrightarrow>
  61.103      (\<lambda>x. c) * f =o O(h) \<Longrightarrow> f =o O(h)"
  61.104    apply (rule subsetD)
  61.105    apply (subgoal_tac "(\<lambda>x. 1 / c) *o O(h) <= O(h)")
  61.106 @@ -696,7 +696,7 @@
  61.107  by (metis abs_ge_zero linorder_linear min_max.sup_absorb1 min_max.sup_commute)
  61.108  
  61.109  lemma bigo_lesso4:
  61.110 -  "f <o g =o O(k\<Colon>'a=>'b\<Colon>{linordered_field,number_ring}) \<Longrightarrow>
  61.111 +  "f <o g =o O(k\<Colon>'a=>'b\<Colon>{linordered_field}) \<Longrightarrow>
  61.112     g =o h +o O(k) \<Longrightarrow> f <o h =o O(k)"
  61.113  apply (unfold lesso_def)
  61.114  apply (drule set_plus_imp_minus)
    62.1 --- a/src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy	Fri Mar 23 20:32:43 2012 +0100
    62.2 +++ b/src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy	Mon Mar 26 10:56:56 2012 +0200
    62.3 @@ -207,6 +207,15 @@
    62.4      by (auto intro!: injI simp add: vec_eq_iff of_nat_index)
    62.5  qed
    62.6  
    62.7 +instance vec :: (numeral, finite) numeral ..
    62.8 +instance vec :: (semiring_numeral, finite) semiring_numeral ..
    62.9 +
   62.10 +lemma numeral_index [simp]: "numeral w $ i = numeral w"
   62.11 +  by (induct w, simp_all only: numeral.simps vector_add_component one_index)
   62.12 +
   62.13 +lemma neg_numeral_index [simp]: "neg_numeral w $ i = neg_numeral w"
   62.14 +  by (simp only: neg_numeral_def vector_uminus_component numeral_index)
   62.15 +
   62.16  instance vec :: (comm_ring_1, finite) comm_ring_1 ..
   62.17  instance vec :: (ring_char_0, finite) ring_char_0 ..
   62.18  
   62.19 @@ -222,7 +231,7 @@
   62.20    by (vector field_simps)
   62.21  lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
   62.22  lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
   62.23 -lemma vector_sneg_minus1: "-x = (- (1::'a::ring_1)) *s x" by vector
   62.24 +lemma vector_sneg_minus1: "-x = (-1::'a::ring_1) *s x" by vector
   62.25  lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
   62.26  lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
   62.27    by (vector field_simps)
    63.1 --- a/src/HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy	Fri Mar 23 20:32:43 2012 +0100
    63.2 +++ b/src/HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy	Mon Mar 26 10:56:56 2012 +0200
    63.3 @@ -281,7 +281,7 @@
    63.4  lemma scaleR_2:
    63.5    fixes x :: "'a::real_vector"
    63.6    shows "scaleR 2 x = x + x"
    63.7 -unfolding one_add_one_is_two [symmetric] scaleR_left_distrib by simp
    63.8 +unfolding one_add_one [symmetric] scaleR_left_distrib by simp
    63.9  
   63.10  lemma vector_choose_size: "0 <= c ==> \<exists>(x::'a::euclidean_space). norm x = c"
   63.11    apply (rule exI[where x="c *\<^sub>R basis 0 ::'a"]) using DIM_positive[where 'a='a] by auto
    64.1 --- a/src/HOL/Multivariate_Analysis/Determinants.thy	Fri Mar 23 20:32:43 2012 +0100
    64.2 +++ b/src/HOL/Multivariate_Analysis/Determinants.thy	Mon Mar 26 10:56:56 2012 +0200
    64.3 @@ -286,7 +286,7 @@
    64.4  proof-
    64.5    have tha: "\<And>(a::'a) b. a = b ==> b = - a ==> a = 0"
    64.6      by simp
    64.7 -  have th1: "of_int (-1) = - 1" by (metis of_int_1 of_int_minus number_of_Min)
    64.8 +  have th1: "of_int (-1) = - 1" by simp
    64.9    let ?p = "Fun.swap i j id"
   64.10    let ?A = "\<chi> i. A $ ?p i"
   64.11    from r have "A = ?A" by (simp add: vec_eq_iff row_def swap_def)
   64.12 @@ -1058,8 +1058,7 @@
   64.13    unfolding det_def UNIV_2
   64.14    unfolding setsum_over_permutations_insert[OF f12]
   64.15    unfolding permutes_sing
   64.16 -  apply (simp add: sign_swap_id sign_id swap_id_eq)
   64.17 -  by (simp add: arith_simps(31)[symmetric] del: arith_simps(31))
   64.18 +  by (simp add: sign_swap_id sign_id swap_id_eq)
   64.19  qed
   64.20  
   64.21  lemma det_3: "det (A::'a::comm_ring_1^3^3) =
   64.22 @@ -1079,9 +1078,7 @@
   64.23    unfolding setsum_over_permutations_insert[OF f23]
   64.24  
   64.25    unfolding permutes_sing
   64.26 -  apply (simp add: sign_swap_id permutation_swap_id sign_compose sign_id swap_id_eq)
   64.27 -  apply (simp add: arith_simps(31)[symmetric] del: arith_simps(31))
   64.28 -  by (simp add: field_simps)
   64.29 +  by (simp add: sign_swap_id permutation_swap_id sign_compose sign_id swap_id_eq)
   64.30  qed
   64.31  
   64.32  end
    65.1 --- a/src/HOL/Multivariate_Analysis/Norm_Arith.thy	Fri Mar 23 20:32:43 2012 +0100
    65.2 +++ b/src/HOL/Multivariate_Analysis/Norm_Arith.thy	Mon Mar 26 10:56:56 2012 +0200
    65.3 @@ -104,6 +104,17 @@
    65.4    "x \<noteq> y \<longleftrightarrow> \<not> (norm (x - y) \<le> 0)"
    65.5    using norm_ge_zero[of "x - y"] by auto
    65.6  
    65.7 +lemmas arithmetic_simps =
    65.8 +  arith_simps
    65.9 +  add_numeral_special
   65.10 +  add_neg_numeral_special
   65.11 +  add_0_left
   65.12 +  add_0_right
   65.13 +  mult_zero_left
   65.14 +  mult_zero_right
   65.15 +  mult_1_left
   65.16 +  mult_1_right
   65.17 +
   65.18  use "normarith.ML"
   65.19  
   65.20  method_setup norm = {* Scan.succeed (SIMPLE_METHOD' o NormArith.norm_arith_tac)
    66.1 --- a/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Fri Mar 23 20:32:43 2012 +0100
    66.2 +++ b/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Mon Mar 26 10:56:56 2012 +0200
    66.3 @@ -5786,7 +5786,7 @@
    66.4      { assume as:"dist a b > dist (f n x) (f n y)"
    66.5        then obtain Na Nb where "\<forall>m\<ge>Na. dist (f (r m) x) a < (dist a b - dist (f n x) (f n y)) / 2"
    66.6          and "\<forall>m\<ge>Nb. dist (f (r m) y) b < (dist a b - dist (f n x) (f n y)) / 2"
    66.7 -        using lima limb unfolding h_def LIMSEQ_def by (fastforce simp del: less_divide_eq_number_of1)
    66.8 +        using lima limb unfolding h_def LIMSEQ_def by (fastforce simp del: less_divide_eq_numeral1)
    66.9        hence "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) < dist a b - dist (f n x) (f n y)"
   66.10          apply(erule_tac x="Na+Nb+n" in allE)
   66.11          apply(erule_tac x="Na+Nb+n" in allE) apply simp
    67.1 --- a/src/HOL/Mutabelle/mutabelle_extra.ML	Fri Mar 23 20:32:43 2012 +0100
    67.2 +++ b/src/HOL/Mutabelle/mutabelle_extra.ML	Mon Mar 26 10:56:56 2012 +0200
    67.3 @@ -271,7 +271,7 @@
    67.4   @{const_name enum_prod_inst.enum_ex_prod},
    67.5   @{const_name Quickcheck.catch_match},
    67.6   @{const_name Quickcheck_Exhaustive.unknown},
    67.7 - @{const_name Int.Bit0}, @{const_name Int.Bit1}
    67.8 + @{const_name Num.Bit0}, @{const_name Num.Bit1}
    67.9   (*@{const_name "==>"}, @{const_name "=="}*)]
   67.10  
   67.11  val forbidden_mutant_consts =
    68.1 --- a/src/HOL/NSA/HyperDef.thy	Fri Mar 23 20:32:43 2012 +0100
    68.2 +++ b/src/HOL/NSA/HyperDef.thy	Mon Mar 26 10:56:56 2012 +0200
    68.3 @@ -346,8 +346,8 @@
    68.4    K (Lin_Arith.add_inj_thms [@{thm star_of_le} RS iffD2,
    68.5      @{thm star_of_less} RS iffD2, @{thm star_of_eq} RS iffD2]
    68.6    #> Lin_Arith.add_simps [@{thm star_of_zero}, @{thm star_of_one},
    68.7 -      @{thm star_of_number_of}, @{thm star_of_add}, @{thm star_of_minus},
    68.8 -      @{thm star_of_diff}, @{thm star_of_mult}]
    68.9 +      @{thm star_of_numeral}, @{thm star_of_neg_numeral}, @{thm star_of_add},
   68.10 +      @{thm star_of_minus}, @{thm star_of_diff}, @{thm star_of_mult}]
   68.11    #> Lin_Arith.add_inj_const (@{const_name "StarDef.star_of"}, @{typ "real \<Rightarrow> hypreal"}))
   68.12  *}
   68.13  
   68.14 @@ -419,10 +419,15 @@
   68.15        x ^ Suc (Suc 0) + y ^ Suc (Suc 0) + (hypreal_of_nat (Suc (Suc 0)))*x*y"
   68.16  by (simp add: right_distrib left_distrib)
   68.17  
   68.18 -lemma power_hypreal_of_real_number_of:
   68.19 -     "(number_of v :: hypreal) ^ n = hypreal_of_real ((number_of v) ^ n)"
   68.20 +lemma power_hypreal_of_real_numeral:
   68.21 +     "(numeral v :: hypreal) ^ n = hypreal_of_real ((numeral v) ^ n)"
   68.22  by simp
   68.23 -declare power_hypreal_of_real_number_of [of _ "number_of w", simp] for w
   68.24 +declare power_hypreal_of_real_numeral [of _ "numeral w", simp] for w
   68.25 +
   68.26 +lemma power_hypreal_of_real_neg_numeral:
   68.27 +     "(neg_numeral v :: hypreal) ^ n = hypreal_of_real ((neg_numeral v) ^ n)"
   68.28 +by simp
   68.29 +declare power_hypreal_of_real_neg_numeral [of _ "numeral w", simp] for w
   68.30  (*
   68.31  lemma hrealpow_HFinite:
   68.32    fixes x :: "'a::{real_normed_algebra,power} star"
   68.33 @@ -492,7 +497,7 @@
   68.34  by transfer (rule power_one)
   68.35  
   68.36  lemma hrabs_hyperpow_minus_one [simp]:
   68.37 -  "\<And>n. abs(-1 pow n) = (1::'a::{number_ring,linordered_idom} star)"
   68.38 +  "\<And>n. abs(-1 pow n) = (1::'a::{linordered_idom} star)"
   68.39  by transfer (rule abs_power_minus_one)
   68.40  
   68.41  lemma hyperpow_mult:
    69.1 --- a/src/HOL/NSA/NSA.thy	Fri Mar 23 20:32:43 2012 +0100
    69.2 +++ b/src/HOL/NSA/NSA.thy	Mon Mar 26 10:56:56 2012 +0200
    69.3 @@ -190,7 +190,7 @@
    69.4  lemma SReal_hypreal_of_real [simp]: "hypreal_of_real x \<in> Reals"
    69.5  by (simp add: Reals_eq_Standard)
    69.6  
    69.7 -lemma SReal_divide_number_of: "r \<in> Reals ==> r/(number_of w::hypreal) \<in> Reals"
    69.8 +lemma SReal_divide_numeral: "r \<in> Reals ==> r/(numeral w::hypreal) \<in> Reals"
    69.9  by simp
   69.10  
   69.11  text{*epsilon is not in Reals because it is an infinitesimal*}
   69.12 @@ -290,8 +290,8 @@
   69.13    "(hnorm (x::hypreal) \<in> HFinite) = (x \<in> HFinite)"
   69.14  by (simp add: HFinite_def)
   69.15  
   69.16 -lemma HFinite_number_of [simp]: "number_of w \<in> HFinite"
   69.17 -unfolding star_number_def by (rule HFinite_star_of)
   69.18 +lemma HFinite_numeral [simp]: "numeral w \<in> HFinite"
   69.19 +unfolding star_numeral_def by (rule HFinite_star_of)
   69.20  
   69.21  (** As always with numerals, 0 and 1 are special cases **)
   69.22  
   69.23 @@ -347,7 +347,7 @@
   69.24  apply (rule InfinitesimalI)
   69.25  apply (rule hypreal_sum_of_halves [THEN subst])
   69.26  apply (drule half_gt_zero)
   69.27 -apply (blast intro: hnorm_add_less SReal_divide_number_of dest: InfinitesimalD)
   69.28 +apply (blast intro: hnorm_add_less SReal_divide_numeral dest: InfinitesimalD)
   69.29  done
   69.30  
   69.31  lemma Infinitesimal_minus_iff [simp]: "(-x:Infinitesimal) = (x:Infinitesimal)"
   69.32 @@ -652,7 +652,7 @@
   69.33  (*reorientation simplification procedure: reorients (polymorphic)
   69.34    0 = x, 1 = x, nnn = x provided x isn't 0, 1 or a numeral.*)
   69.35  simproc_setup approx_reorient_simproc
   69.36 -  ("0 @= x" | "1 @= y" | "number_of w @= z") =
   69.37 +  ("0 @= x" | "1 @= y" | "numeral w @= z" | "neg_numeral w @= r") =
   69.38  {*
   69.39    let val rule = @{thm approx_reorient} RS eq_reflection
   69.40        fun proc phi ss ct = case term_of ct of
   69.41 @@ -957,9 +957,9 @@
   69.42       "x \<noteq> 0 ==> star_of x \<in> HFinite - Infinitesimal"
   69.43  by simp
   69.44  
   69.45 -lemma number_of_not_Infinitesimal [simp]:
   69.46 -     "number_of w \<noteq> (0::hypreal) ==> (number_of w :: hypreal) \<notin> Infinitesimal"
   69.47 -by (fast dest: Reals_number_of [THEN SReal_Infinitesimal_zero])
   69.48 +lemma numeral_not_Infinitesimal [simp]:
   69.49 +     "numeral w \<noteq> (0::hypreal) ==> (numeral w :: hypreal) \<notin> Infinitesimal"
   69.50 +by (fast dest: Reals_numeral [THEN SReal_Infinitesimal_zero])
   69.51  
   69.52  (*again: 1 is a special case, but not 0 this time*)
   69.53  lemma one_not_Infinitesimal [simp]:
   69.54 @@ -1024,31 +1024,31 @@
   69.55  apply simp
   69.56  done
   69.57  
   69.58 -lemma number_of_approx_iff [simp]:
   69.59 -     "(number_of v @= (number_of w :: 'a::{number,real_normed_vector} star)) =
   69.60 -      (number_of v = (number_of w :: 'a))"
   69.61 -apply (unfold star_number_def)
   69.62 +lemma numeral_approx_iff [simp]:
   69.63 +     "(numeral v @= (numeral w :: 'a::{numeral,real_normed_vector} star)) =
   69.64 +      (numeral v = (numeral w :: 'a))"
   69.65 +apply (unfold star_numeral_def)
   69.66  apply (rule star_of_approx_iff)
   69.67  done
   69.68  
   69.69  (*And also for 0 @= #nn and 1 @= #nn, #nn @= 0 and #nn @= 1.*)
   69.70  lemma [simp]:
   69.71 -  "(number_of w @= (0::'a::{number,real_normed_vector} star)) =
   69.72 -   (number_of w = (0::'a))"
   69.73 -  "((0::'a::{number,real_normed_vector} star) @= number_of w) =
   69.74 -   (number_of w = (0::'a))"
   69.75 -  "(number_of w @= (1::'b::{number,one,real_normed_vector} star)) =
   69.76 -   (number_of w = (1::'b))"
   69.77 -  "((1::'b::{number,one,real_normed_vector} star) @= number_of w) =
   69.78 -   (number_of w = (1::'b))"
   69.79 +  "(numeral w @= (0::'a::{numeral,real_normed_vector} star)) =
   69.80 +   (numeral w = (0::'a))"
   69.81 +  "((0::'a::{numeral,real_normed_vector} star) @= numeral w) =
   69.82 +   (numeral w = (0::'a))"
   69.83 +  "(numeral w @= (1::'b::{numeral,one,real_normed_vector} star)) =
   69.84 +   (numeral w = (1::'b))"
   69.85 +  "((1::'b::{numeral,one,real_normed_vector} star) @= numeral w) =
   69.86 +   (numeral w = (1::'b))"
   69.87    "~ (0 @= (1::'c::{zero_neq_one,real_normed_vector} star))"
   69.88    "~ (1 @= (0::'c::{zero_neq_one,real_normed_vector} star))"
   69.89 -apply (unfold star_number_def star_zero_def star_one_def)
   69.90 +apply (unfold star_numeral_def star_zero_def star_one_def)
   69.91  apply (unfold star_of_approx_iff)
   69.92  by (auto intro: sym)
   69.93  
   69.94 -lemma star_of_approx_number_of_iff [simp]:
   69.95 -     "(star_of k @= number_of w) = (k = number_of w)"
   69.96 +lemma star_of_approx_numeral_iff [simp]:
   69.97 +     "(star_of k @= numeral w) = (k = numeral w)"
   69.98  by (subst star_of_approx_iff [symmetric], auto)
   69.99  
  69.100  lemma star_of_approx_zero_iff [simp]: "(star_of k @= 0) = (k = 0)"
  69.101 @@ -1843,8 +1843,11 @@
  69.102  lemma st_add: "\<lbrakk>x \<in> HFinite; y \<in> HFinite\<rbrakk> \<Longrightarrow> st (x + y) = st x + st y"
  69.103  by (simp add: st_unique st_SReal st_approx_self approx_add)
  69.104  
  69.105 -lemma st_number_of [simp]: "st (number_of w) = number_of w"
  69.106 -by (rule Reals_number_of [THEN st_SReal_eq])
  69.107 +lemma st_numeral [simp]: "st (numeral w) = numeral w"
  69.108 +by (rule Reals_numeral [THEN st_SReal_eq])
  69.109 +
  69.110 +lemma st_neg_numeral [simp]: "st (neg_numeral w) = neg_numeral w"
  69.111 +by (rule Reals_neg_numeral [THEN st_SReal_eq])
  69.112  
  69.113  lemma st_0 [simp]: "st 0 = 0"
  69.114  by (simp add: st_SReal_eq)
    70.1 --- a/src/HOL/NSA/NSCA.thy	Fri Mar 23 20:32:43 2012 +0100
    70.2 +++ b/src/HOL/NSA/NSCA.thy	Mon Mar 26 10:56:56 2012 +0200
    70.3 @@ -32,14 +32,14 @@
    70.4       "hcmod (hcomplex_of_complex r) \<in> Reals"
    70.5  by (simp add: Reals_eq_Standard)
    70.6  
    70.7 -lemma SReal_hcmod_number_of [simp]: "hcmod (number_of w ::hcomplex) \<in> Reals"
    70.8 +lemma SReal_hcmod_numeral [simp]: "hcmod (numeral w ::hcomplex) \<in> Reals"
    70.9  by (simp add: Reals_eq_Standard)
   70.10  
   70.11  lemma SReal_hcmod_SComplex: "x \<in> SComplex ==> hcmod x \<in> Reals"
   70.12  by (simp add: Reals_eq_Standard)
   70.13  
   70.14 -lemma SComplex_divide_number_of:
   70.15 -     "r \<in> SComplex ==> r/(number_of w::hcomplex) \<in> SComplex"
   70.16 +lemma SComplex_divide_numeral:
   70.17 +     "r \<in> SComplex ==> r/(numeral w::hcomplex) \<in> SComplex"
   70.18  by simp
   70.19  
   70.20  lemma SComplex_UNIV_complex:
   70.21 @@ -211,9 +211,9 @@
   70.22        ==> hcomplex_of_complex x \<in> HFinite - Infinitesimal"
   70.23  by (rule SComplex_HFinite_diff_Infinitesimal, auto)
   70.24  
   70.25 -lemma number_of_not_Infinitesimal [simp]:
   70.26 -     "number_of w \<noteq> (0::hcomplex) ==> (number_of w::hcomplex) \<notin> Infinitesimal"
   70.27 -by (fast dest: Standard_number_of [THEN SComplex_Infinitesimal_zero])
   70.28 +lemma numeral_not_Infinitesimal [simp]:
   70.29 +     "numeral w \<noteq> (0::hcomplex) ==> (numeral w::hcomplex) \<notin> Infinitesimal"
   70.30 +by (fast dest: Standard_numeral [THEN SComplex_Infinitesimal_zero])
   70.31  
   70.32  lemma approx_SComplex_not_zero:
   70.33       "[| y \<in> SComplex; x @= y; y\<noteq> 0 |] ==> x \<noteq> 0"
   70.34 @@ -223,11 +223,11 @@
   70.35       "[|x \<in> SComplex; y \<in> SComplex|] ==> (x @= y) = (x = y)"
   70.36  by (auto simp add: Standard_def)
   70.37  
   70.38 -lemma number_of_Infinitesimal_iff [simp]:
   70.39 -     "((number_of w :: hcomplex) \<in> Infinitesimal) =
   70.40 -      (number_of w = (0::hcomplex))"
   70.41 +lemma numeral_Infinitesimal_iff [simp]:
   70.42 +     "((numeral w :: hcomplex) \<in> Infinitesimal) =
   70.43 +      (numeral w = (0::hcomplex))"
   70.44  apply (rule iffI)
   70.45 -apply (fast dest: Standard_number_of [THEN SComplex_Infinitesimal_zero])
   70.46 +apply (fast dest: Standard_numeral [THEN SComplex_Infinitesimal_zero])
   70.47  apply (simp (no_asm_simp))
   70.48  done
   70.49  
   70.50 @@ -441,8 +441,8 @@
   70.51       "[| x \<in> HFinite; y \<in> HFinite |] ==> stc (x + y) = stc(x) + stc(y)"
   70.52  by (simp add: stc_unique stc_SComplex stc_approx_self approx_add)
   70.53  
   70.54 -lemma stc_number_of [simp]: "stc (number_of w) = number_of w"
   70.55 -by (rule Standard_number_of [THEN stc_SComplex_eq])
   70.56 +lemma stc_numeral [simp]: "stc (numeral w) = numeral w"
   70.57 +by (rule Standard_numeral [THEN stc_SComplex_eq])
   70.58  
   70.59  lemma stc_zero [simp]: "stc 0 = 0"
   70.60  by simp
    71.1 --- a/src/HOL/NSA/NSComplex.thy	Fri Mar 23 20:32:43 2012 +0100
    71.2 +++ b/src/HOL/NSA/NSComplex.thy	Mon Mar 26 10:56:56 2012 +0200
    71.3 @@ -626,32 +626,38 @@
    71.4  
    71.5  subsection{*Numerals and Arithmetic*}
    71.6  
    71.7 -lemma hcomplex_number_of_def: "(number_of w :: hcomplex) == of_int w"
    71.8 -by transfer (rule number_of_eq [THEN eq_reflection])
    71.9 -
   71.10  lemma hcomplex_of_hypreal_eq_hcomplex_of_complex: 
   71.11       "hcomplex_of_hypreal (hypreal_of_real x) =  
   71.12        hcomplex_of_complex (complex_of_real x)"
   71.13  by transfer (rule refl)
   71.14  
   71.15 -lemma hcomplex_hypreal_number_of: 
   71.16 -  "hcomplex_of_complex (number_of w) = hcomplex_of_hypreal(number_of w)"
   71.17 -by transfer (rule of_real_number_of_eq [symmetric])
   71.18 +lemma hcomplex_hypreal_numeral:
   71.19 +  "hcomplex_of_complex (numeral w) = hcomplex_of_hypreal(numeral w)"
   71.20 +by transfer (rule of_real_numeral [symmetric])
   71.21  
   71.22 -lemma hcomplex_number_of_hcnj [simp]:
   71.23 -     "hcnj (number_of v :: hcomplex) = number_of v"
   71.24 -by transfer (rule complex_cnj_number_of)
   71.25 +lemma hcomplex_hypreal_neg_numeral:
   71.26 +  "hcomplex_of_complex (neg_numeral w) = hcomplex_of_hypreal(neg_numeral w)"
   71.27 +by transfer (rule of_real_neg_numeral [symmetric])
   71.28  
   71.29 -lemma hcomplex_number_of_hcmod [simp]: 
   71.30 -      "hcmod(number_of v :: hcomplex) = abs (number_of v :: hypreal)"
   71.31 -by transfer (rule norm_number_of)
   71.32 +lemma hcomplex_numeral_hcnj [simp]:
   71.33 +     "hcnj (numeral v :: hcomplex) = numeral v"
   71.34 +by transfer (rule complex_cnj_numeral)
   71.35  
   71.36 -lemma hcomplex_number_of_hRe [simp]: 
   71.37 -      "hRe(number_of v :: hcomplex) = number_of v"
   71.38 -by transfer (rule complex_Re_number_of)
   71.39 +lemma hcomplex_numeral_hcmod [simp]:
   71.40 +      "hcmod(numeral v :: hcomplex) = (numeral v :: hypreal)"
   71.41 +by transfer (rule norm_numeral)
   71.42  
   71.43 -lemma hcomplex_number_of_hIm [simp]: 
   71.44 -      "hIm(number_of v :: hcomplex) = 0"
   71.45 -by transfer (rule complex_Im_number_of)
   71.46 +lemma hcomplex_neg_numeral_hcmod [simp]: 
   71.47 +      "hcmod(neg_numeral v :: hcomplex) = (numeral v :: hypreal)"
   71.48 +by transfer (rule norm_neg_numeral)
   71.49  
   71.50 +lemma hcomplex_numeral_hRe [simp]: 
   71.51 +      "hRe(numeral v :: hcomplex) = numeral v"
   71.52 +by transfer (rule complex_Re_numeral)
   71.53 +
   71.54 +lemma hcomplex_numeral_hIm [simp]: 
   71.55 +      "hIm(numeral v :: hcomplex) = 0"
   71.56 +by transfer (rule complex_Im_numeral)
   71.57 +
   71.58 +(* TODO: add neg_numeral rules above *)
   71.59  end
    72.1 --- a/src/HOL/NSA/StarDef.thy	Fri Mar 23 20:32:43 2012 +0100
    72.2 +++ b/src/HOL/NSA/StarDef.thy	Mon Mar 26 10:56:56 2012 +0200
    72.3 @@ -522,16 +522,6 @@
    72.4  
    72.5  end
    72.6  
    72.7 -instantiation star :: (number) number
    72.8 -begin
    72.9 -
   72.10 -definition
   72.11 -  star_number_def:  "number_of b \<equiv> star_of (number_of b)"
   72.12 -
   72.13 -instance ..
   72.14 -
   72.15 -end
   72.16 -
   72.17  instance star :: (Rings.dvd) Rings.dvd ..
   72.18  
   72.19  instantiation star :: (Divides.div) Divides.div
   72.20 @@ -561,7 +551,7 @@
   72.21  end
   72.22  
   72.23  lemmas star_class_defs [transfer_unfold] =
   72.24 -  star_zero_def     star_one_def      star_number_def
   72.25 +  star_zero_def     star_one_def
   72.26    star_add_def      star_diff_def     star_minus_def
   72.27    star_mult_def     star_divide_def   star_inverse_def
   72.28    star_le_def       star_less_def     star_abs_def       star_sgn_def
   72.29 @@ -575,9 +565,6 @@
   72.30  lemma Standard_one: "1 \<in> Standard"
   72.31  by (simp add: star_one_def)
   72.32  
   72.33 -lemma Standard_number_of: "number_of b \<in> Standard"
   72.34 -by (simp add: star_number_def)
   72.35 -
   72.36  lemma Standard_add: "\<lbrakk>x \<in> Standard; y \<in> Standard\<rbrakk> \<Longrightarrow> x + y \<in> Standard"
   72.37  by (simp add: star_add_def)
   72.38  
   72.39 @@ -606,7 +593,7 @@
   72.40  by (simp add: star_mod_def)
   72.41  
   72.42  lemmas Standard_simps [simp] =
   72.43 -  Standard_zero  Standard_one  Standard_number_of
   72.44 +  Standard_zero  Standard_one
   72.45    Standard_add  Standard_diff  Standard_minus
   72.46    Standard_mult  Standard_divide  Standard_inverse
   72.47    Standard_abs  Standard_div  Standard_mod
   72.48 @@ -648,9 +635,6 @@
   72.49  lemma star_of_one: "star_of 1 = 1"
   72.50  by transfer (rule refl)
   72.51  
   72.52 -lemma star_of_number_of: "star_of (number_of x) = number_of x"
   72.53 -by transfer (rule refl)
   72.54 -
   72.55  text {* @{term star_of} preserves orderings *}
   72.56  
   72.57  lemma star_of_less: "(star_of x < star_of y) = (x < y)"
   72.58 @@ -682,34 +666,16 @@
   72.59  lemmas star_of_le_1   = star_of_le   [of _ 1, simplified star_of_one]
   72.60  lemmas star_of_eq_1   = star_of_eq   [of _ 1, simplified star_of_one]
   72.61  
   72.62 -text{*As above, for numerals*}
   72.63 -
   72.64 -lemmas star_of_number_less =
   72.65 -  star_of_less [of "number_of w", simplified star_of_number_of] for w
   72.66 -lemmas star_of_number_le   =
   72.67 -  star_of_le   [of "number_of w", simplified star_of_number_of] for w
   72.68 -lemmas star_of_number_eq   =
   72.69 -  star_of_eq   [of "number_of w", simplified star_of_number_of] for w
   72.70 -
   72.71 -lemmas star_of_less_number =
   72.72 -  star_of_less [of _ "number_of w", simplified star_of_number_of] for w
   72.73 -lemmas star_of_le_number   =
   72.74 -  star_of_le   [of _ "number_of w", simplified star_of_number_of] for w
   72.75 -lemmas star_of_eq_number   =
   72.76 -  star_of_eq   [of _ "number_of w", simplified star_of_number_of] for w
   72.77 -
   72.78  lemmas star_of_simps [simp] =
   72.79    star_of_add     star_of_diff    star_of_minus
   72.80    star_of_mult    star_of_divide  star_of_inverse
   72.81    star_of_div     star_of_mod     star_of_abs
   72.82 -  star_of_zero    star_of_one     star_of_number_of
   72.83 +  star_of_zero    star_of_one
   72.84    star_of_less    star_of_le      star_of_eq
   72.85    star_of_0_less  star_of_0_le    star_of_0_eq
   72.86    star_of_less_0  star_of_le_0    star_of_eq_0
   72.87    star_of_1_less  star_of_1_le    star_of_1_eq
   72.88    star_of_less_1  star_of_le_1    star_of_eq_1
   72.89 -  star_of_number_less star_of_number_le star_of_number_eq
   72.90 -  star_of_less_number star_of_le_number star_of_eq_number
   72.91  
   72.92  subsection {* Ordering and lattice classes *}
   72.93  
   72.94 @@ -984,9 +950,45 @@
   72.95  
   72.96  subsection {* Number classes *}
   72.97  
   72.98 +instance star :: (numeral) numeral ..
   72.99 +
  72.100 +lemma star_numeral_def [transfer_unfold]:
  72.101 +  "numeral k = star_of (numeral k)"
  72.102 +by (induct k, simp_all only: numeral.simps star_of_one star_of_add)
  72.103 +
  72.104 +lemma Standard_numeral [simp]: "numeral k \<in> Standard"
  72.105 +by (simp add: star_numeral_def)
  72.106 +
  72.107 +lemma star_of_numeral [simp]: "star_of (numeral k) = numeral k"
  72.108 +by transfer (rule refl)
  72.109 +
  72.110 +lemma star_neg_numeral_def [transfer_unfold]:
  72.111 +  "neg_numeral k = star_of (neg_numeral k)"
  72.112 +by (simp only: neg_numeral_def star_of_minus star_of_numeral)
  72.113 +
  72.114 +lemma Standard_neg_numeral [simp]: "neg_numeral k \<in> Standard"
  72.115 +by (simp add: star_neg_numeral_def)
  72.116 +
  72.117 +lemma star_of_neg_numeral [simp]: "star_of (neg_numeral k) = neg_numeral k"
  72.118 +by transfer (rule refl)
  72.119 +
  72.120  lemma star_of_nat_def [transfer_unfold]: "of_nat n = star_of (of_nat n)"
  72.121  by (induct n, simp_all)
  72.122  
  72.123 +lemmas star_of_compare_numeral [simp] =
  72.124 +  star_of_less [of "numeral k", simplified star_of_numeral]
  72.125 +  star_of_le   [of "numeral k", simplified star_of_numeral]
  72.126 +  star_of_eq   [of "numeral k", simplified star_of_numeral]
  72.127 +  star_of_less [of _ "numeral k", simplified star_of_numeral]
  72.128 +  star_of_le   [of _ "numeral k", simplified star_of_numeral]
  72.129 +  star_of_eq   [of _ "numeral k", simplified star_of_numeral]
  72.130 +  star_of_less [of "neg_numeral k", simplified star_of_numeral]
  72.131 +  star_of_le   [of "neg_numeral k", simplified star_of_numeral]
  72.132 +  star_of_eq   [of "neg_numeral k", simplified star_of_numeral]
  72.133 +  star_of_less [of _ "neg_numeral k", simplified star_of_numeral]
  72.134 +  star_of_le   [of _ "neg_numeral k", simplified star_of_numeral]
  72.135 +  star_of_eq   [of _ "neg_numeral k", simplified star_of_numeral] for k
  72.136 +
  72.137  lemma Standard_of_nat [simp]: "of_nat n \<in> Standard"
  72.138  by (simp add: star_of_nat_def)
  72.139  
  72.140 @@ -1010,11 +1012,6 @@
  72.141  
  72.142  instance star :: (ring_char_0) ring_char_0 ..
  72.143  
  72.144 -instance star :: (number_semiring) number_semiring
  72.145 -by (intro_classes, simp only: star_number_def star_of_nat_def number_of_int)
  72.146 -
  72.147 -instance star :: (number_ring) number_ring
  72.148 -by (intro_classes, simp only: star_number_def star_of_int_def number_of_eq)
  72.149  
  72.150  subsection {* Finite class *}
  72.151  
    73.1 --- a/src/HOL/Nat.thy	Fri Mar 23 20:32:43 2012 +0100
    73.2 +++ b/src/HOL/Nat.thy	Mon Mar 26 10:56:56 2012 +0200
    73.3 @@ -181,7 +181,7 @@
    73.4  begin
    73.5  
    73.6  definition
    73.7 -  One_nat_def [simp, code_post]: "1 = Suc 0"
    73.8 +  One_nat_def [simp]: "1 = Suc 0"
    73.9  
   73.10  primrec times_nat where
   73.11    mult_0:     "0 * n = (0\<Colon>nat)"
   73.12 @@ -1782,4 +1782,6 @@
   73.13  code_modulename Haskell
   73.14    Nat Arith
   73.15  
   73.16 +hide_const (open) of_nat_aux
   73.17 +
   73.18  end
    74.1 --- a/src/HOL/Nat_Numeral.thy	Fri Mar 23 20:32:43 2012 +0100
    74.2 +++ b/src/HOL/Nat_Numeral.thy	Mon Mar 26 10:56:56 2012 +0200
    74.3 @@ -15,31 +15,13 @@
    74.4    Arithmetic for naturals is reduced to that for the non-negative integers.
    74.5  *}
    74.6  
    74.7 -instantiation nat :: number_semiring
    74.8 -begin
    74.9 -
   74.10 -definition
   74.11 -  nat_number_of_def [code_unfold, code del]: "number_of v = nat (number_of v)"
   74.12 -
   74.13 -instance proof
   74.14 -  fix n show "number_of (int n) = (of_nat n :: nat)"
   74.15 -    unfolding nat_number_of_def number_of_eq by simp
   74.16 -qed
   74.17 - 
   74.18 -end
   74.19 -
   74.20 -lemma [code_post]:
   74.21 -  "nat (number_of v) = number_of v"
   74.22 -  unfolding nat_number_of_def ..
   74.23 -
   74.24 -
   74.25  subsection {* Special case: squares and cubes *}
   74.26  
   74.27  lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
   74.28 -  by (simp add: nat_number_of_def)
   74.29 +  by (simp add: nat_number(2-4))
   74.30  
   74.31  lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"
   74.32 -  by (simp add: nat_number_of_def)
   74.33 +  by (simp add: nat_number(2-4))
   74.34  
   74.35  context power
   74.36  begin
   74.37 @@ -93,26 +75,21 @@
   74.38    "(- a)\<twosuperior> = a\<twosuperior>"
   74.39    by (simp add: power2_eq_square)
   74.40  
   74.41 -text{*
   74.42 -  We cannot prove general results about the numeral @{term "-1"},
   74.43 -  so we have to use @{term "- 1"} instead.
   74.44 -*}
   74.45 -
   74.46  lemma power_minus1_even [simp]:
   74.47 -  "(- 1) ^ (2*n) = 1"
   74.48 +  "-1 ^ (2*n) = 1"
   74.49  proof (induct n)
   74.50    case 0 show ?case by simp
   74.51  next
   74.52 -  case (Suc n) then show ?case by (simp add: power_add)
   74.53 +  case (Suc n) then show ?case by (simp add: power_add power2_eq_square)
   74.54  qed
   74.55  
   74.56  lemma power_minus1_odd:
   74.57 -  "(- 1) ^ Suc (2*n) = - 1"
   74.58 +  "-1 ^ Suc (2*n) = -1"
   74.59    by simp
   74.60  
   74.61  lemma power_minus_even [simp]:
   74.62    "(-a) ^ (2*n) = a ^ (2*n)"
   74.63 -  by (simp add: power_minus [of a]) 
   74.64 +  by (simp add: power_minus [of a])
   74.65  
   74.66  end
   74.67  
   74.68 @@ -261,100 +238,31 @@
   74.69  end
   74.70  
   74.71  lemma power2_sum:
   74.72 -  fixes x y :: "'a::number_semiring"
   74.73 +  fixes x y :: "'a::comm_semiring_1"
   74.74    shows "(x + y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> + 2 * x * y"
   74.75 -  by (simp add: algebra_simps power2_eq_square semiring_mult_2_right)
   74.76 +  by (simp add: algebra_simps power2_eq_square mult_2_right)
   74.77  
   74.78  lemma power2_diff:
   74.79 -  fixes x y :: "'a::number_ring"
   74.80 +  fixes x y :: "'a::comm_ring_1"
   74.81    shows "(x - y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> - 2 * x * y"
   74.82    by (simp add: ring_distribs power2_eq_square mult_2) (rule mult_commute)
   74.83  
   74.84  
   74.85 -subsection {* Predicate for negative binary numbers *}
   74.86 -
   74.87 -definition neg  :: "int \<Rightarrow> bool" where
   74.88 -  "neg Z \<longleftrightarrow> Z < 0"
   74.89 -
   74.90 -lemma not_neg_int [simp]: "~ neg (of_nat n)"
   74.91 -by (simp add: neg_def)
   74.92 -
   74.93 -lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))"
   74.94 -by (simp add: neg_def del: of_nat_Suc)
   74.95 -
   74.96 -lemmas neg_eq_less_0 = neg_def
   74.97 -
   74.98 -lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
   74.99 -by (simp add: neg_def linorder_not_less)
  74.100 -
  74.101 -text{*To simplify inequalities when Numeral1 can get simplified to 1*}
  74.102 -
  74.103 -lemma not_neg_0: "~ neg 0"
  74.104 -by (simp add: One_int_def neg_def)
  74.105 -
  74.106 -lemma not_neg_1: "~ neg 1"
  74.107 -by (simp add: neg_def linorder_not_less)
  74.108 -
  74.109 -lemma neg_nat: "neg z ==> nat z = 0"
  74.110 -by (simp add: neg_def order_less_imp_le) 
  74.111 -
  74.112 -lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z"
  74.113 -by (simp add: linorder_not_less neg_def)
  74.114 -
  74.115 -text {*
  74.116 -  If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
  74.117 -  @{term Numeral0} IS @{term "number_of Pls"}
  74.118 -*}
  74.119 -
  74.120 -lemma not_neg_number_of_Pls: "~ neg (number_of Int.Pls)"
  74.121 -  by (simp add: neg_def)
  74.122 -
  74.123 -lemma neg_number_of_Min: "neg (number_of Int.Min)"
  74.124 -  by (simp add: neg_def)
  74.125 -
  74.126 -lemma neg_number_of_Bit0:
  74.127 -  "neg (number_of (Int.Bit0 w)) = neg (number_of w)"
  74.128 -  by (simp add: neg_def)
  74.129 -
  74.130 -lemma neg_number_of_Bit1:
  74.131 -  "neg (number_of (Int.Bit1 w)) = neg (number_of w)"
  74.132 -  by (simp add: neg_def)
  74.133 -
  74.134 -lemmas neg_simps [simp] =
  74.135 -  not_neg_0 not_neg_1
  74.136 -  not_neg_number_of_Pls neg_number_of_Min
  74.137 -  neg_number_of_Bit0 neg_number_of_Bit1
  74.138 -
  74.139 -
  74.140  subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
  74.141  
  74.142  declare nat_1 [simp]
  74.143  
  74.144 -lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
  74.145 -  by (simp add: nat_number_of_def)
  74.146 -
  74.147 -lemma nat_numeral_0_eq_0: "Numeral0 = (0::nat)" (* FIXME delete candidate *)
  74.148 -  by (fact semiring_numeral_0_eq_0)
  74.149 -
  74.150 -lemma nat_numeral_1_eq_1: "Numeral1 = (1::nat)" (* FIXME delete candidate *)
  74.151 -  by (fact semiring_numeral_1_eq_1)
  74.152 -
  74.153 -lemma Numeral1_eq1_nat:
  74.154 -  "(1::nat) = Numeral1"
  74.155 +lemma nat_neg_numeral [simp]: "nat (neg_numeral w) = 0"
  74.156    by simp
  74.157  
  74.158  lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
  74.159 -  by (simp only: nat_numeral_1_eq_1 One_nat_def)
  74.160 +  by simp
  74.161  
  74.162  
  74.163  subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
  74.164  
  74.165 -lemma int_nat_number_of [simp]:
  74.166 -     "int (number_of v) =  
  74.167 -         (if neg (number_of v :: int) then 0  
  74.168 -          else (number_of v :: int))"
  74.169 -  unfolding nat_number_of_def number_of_is_id neg_def
  74.170 -  by simp (* FIXME: redundant with of_nat_number_of_eq *)
  74.171 +lemma int_numeral: "int (numeral v) = numeral v"
  74.172 +  by (rule of_nat_numeral) (* already simp *)
  74.173  
  74.174  lemma nonneg_int_cases:
  74.175    fixes k :: int assumes "0 \<le> k" obtains n where "k = of_nat n"
  74.176 @@ -368,149 +276,51 @@
  74.177  done
  74.178  
  74.179  lemma Suc_nat_number_of_add:
  74.180 -     "Suc (number_of v + n) =  
  74.181 -        (if neg (number_of v :: int) then 1+n else number_of (Int.succ v) + n)"
  74.182 -  unfolding nat_number_of_def number_of_is_id neg_def numeral_simps
  74.183 -  by (simp add: Suc_nat_eq_nat_zadd1 add_ac)
  74.184 +  "Suc (numeral v + n) = numeral (v + Num.One) + n"
  74.185 +  by simp
  74.186  
  74.187 -lemma Suc_nat_number_of [simp]:
  74.188 -     "Suc (number_of v) =  
  74.189 -        (if neg (number_of v :: int) then 1 else number_of (Int.succ v))"
  74.190 -apply (cut_tac n = 0 in Suc_nat_number_of_add)
  74.191 -apply (simp cong del: if_weak_cong)
  74.192 -done
  74.193 -
  74.194 -
  74.195 -subsubsection{*Addition *}
  74.196 -
  74.197 -lemma add_nat_number_of [simp]:
  74.198 -     "(number_of v :: nat) + number_of v' =  
  74.199 -         (if v < Int.Pls then number_of v'  
  74.200 -          else if v' < Int.Pls then number_of v  
  74.201 -          else number_of (v + v'))"
  74.202 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.203 -  by (simp add: nat_add_distrib)
  74.204 -
  74.205 -lemma nat_number_of_add_1 [simp]:
  74.206 -  "number_of v + (1::nat) =
  74.207 -    (if v < Int.Pls then 1 else number_of (Int.succ v))"
  74.208 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.209 -  by (simp add: nat_add_distrib)
  74.210 -
  74.211 -lemma nat_1_add_number_of [simp]:
  74.212 -  "(1::nat) + number_of v =
  74.213 -    (if v < Int.Pls then 1 else number_of (Int.succ v))"
  74.214 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.215 -  by (simp add: nat_add_distrib)
  74.216 -
  74.217 -lemma nat_1_add_1 [simp]: "1 + 1 = (2::nat)"
  74.218 -  by (rule semiring_one_add_one_is_two)
  74.219 -
  74.220 -text {* TODO: replace simp rules above with these generic ones: *}
  74.221 -
  74.222 -lemma semiring_add_number_of:
  74.223 -  "\<lbrakk>Int.Pls \<le> v; Int.Pls \<le> v'\<rbrakk> \<Longrightarrow>
  74.224 -    (number_of v :: 'a::number_semiring) + number_of v' = number_of (v + v')"
  74.225 -  unfolding Int.Pls_def
  74.226 -  by (elim nonneg_int_cases,
  74.227 -    simp only: number_of_int of_nat_add [symmetric])
  74.228 -
  74.229 -lemma semiring_number_of_add_1:
  74.230 -  "Int.Pls \<le> v \<Longrightarrow>
  74.231 -    number_of v + (1::'a::number_semiring) = number_of (Int.succ v)"
  74.232 -  unfolding Int.Pls_def Int.succ_def
  74.233 -  by (elim nonneg_int_cases,
  74.234 -    simp only: number_of_int add_commute [where b=1] of_nat_Suc [symmetric])
  74.235 -
  74.236 -lemma semiring_1_add_number_of:
  74.237 -  "Int.Pls \<le> v \<Longrightarrow>
  74.238 -    (1::'a::number_semiring) + number_of v = number_of (Int.succ v)"
  74.239 -  unfolding Int.Pls_def Int.succ_def
  74.240 -  by (elim nonneg_int_cases,
  74.241 -    simp only: number_of_int add_commute [where b=1] of_nat_Suc [symmetric])
  74.242 +lemma Suc_numeral [simp]:
  74.243 +  "Suc (numeral v) = numeral (v + Num.One)"
  74.244 +  by simp
  74.245  
  74.246  
  74.247  subsubsection{*Subtraction *}
  74.248  
  74.249  lemma diff_nat_eq_if:
  74.250       "nat z - nat z' =  
  74.251 -        (if neg z' then nat z   
  74.252 +        (if z' < 0 then nat z   
  74.253           else let d = z-z' in     
  74.254 -              if neg d then 0 else nat d)"
  74.255 -by (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
  74.256 +              if d < 0 then 0 else nat d)"
  74.257 +by (simp add: Let_def nat_diff_distrib [symmetric])
  74.258  
  74.259 +(* Int.nat_diff_distrib has too-strong premises *)
  74.260 +lemma nat_diff_distrib': "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x - y) = nat x - nat y"
  74.261 +apply (rule int_int_eq [THEN iffD1], clarsimp)
  74.262 +apply (subst zdiff_int [symmetric])
  74.263 +apply (rule nat_mono, simp_all)
  74.264 +done
  74.265  
  74.266 -lemma diff_nat_number_of [simp]: 
  74.267 -     "(number_of v :: nat) - number_of v' =  
  74.268 -        (if v' < Int.Pls then number_of v  
  74.269 -         else let d = number_of (v + uminus v') in     
  74.270 -              if neg d then 0 else nat d)"
  74.271 -  unfolding nat_number_of_def number_of_is_id numeral_simps neg_def
  74.272 -  by auto
  74.273 +lemma diff_nat_numeral [simp]: 
  74.274 +  "(numeral v :: nat) - numeral v' = nat (numeral v - numeral v')"
  74.275 +  by (simp only: nat_diff_distrib' zero_le_numeral nat_numeral)
  74.276  
  74.277 -lemma nat_number_of_diff_1 [simp]:
  74.278 -  "number_of v - (1::nat) =
  74.279 -    (if v \<le> Int.Pls then 0 else number_of (Int.pred v))"
  74.280 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.281 -  by auto
  74.282 -
  74.283 -
  74.284 -subsubsection{*Multiplication *}
  74.285 -
  74.286 -lemma mult_nat_number_of [simp]:
  74.287 -     "(number_of v :: nat) * number_of v' =  
  74.288 -       (if v < Int.Pls then 0 else number_of (v * v'))"
  74.289 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.290 -  by (simp add: nat_mult_distrib)
  74.291 -
  74.292 -(* TODO: replace mult_nat_number_of with this next rule *)
  74.293 -lemma semiring_mult_number_of:
  74.294 -  "\<lbrakk>Int.Pls \<le> v; Int.Pls \<le> v'\<rbrakk> \<Longrightarrow>
  74.295 -    (number_of v :: 'a::number_semiring) * number_of v' = number_of (v * v')"
  74.296 -  unfolding Int.Pls_def
  74.297 -  by (elim nonneg_int_cases,
  74.298 -    simp only: number_of_int of_nat_mult [symmetric])
  74.299 +lemma nat_numeral_diff_1 [simp]:
  74.300 +  "numeral v - (1::nat) = nat (numeral v - 1)"
  74.301 +  using diff_nat_numeral [of v Num.One] by simp
  74.302  
  74.303  
  74.304  subsection{*Comparisons*}
  74.305  
  74.306 -subsubsection{*Equals (=) *}
  74.307 -
  74.308 -lemma eq_nat_number_of [simp]:
  74.309 -     "((number_of v :: nat) = number_of v') =  
  74.310 -      (if neg (number_of v :: int) then (number_of v' :: int) \<le> 0
  74.311 -       else if neg (number_of v' :: int) then (number_of v :: int) = 0
  74.312 -       else v = v')"
  74.313 -  unfolding nat_number_of_def number_of_is_id neg_def
  74.314 -  by auto
  74.315 -
  74.316 -
  74.317 -subsubsection{*Less-than (<) *}
  74.318 -
  74.319 -lemma less_nat_number_of [simp]:
  74.320 -  "(number_of v :: nat) < number_of v' \<longleftrightarrow>
  74.321 -    (if v < v' then Int.Pls < v' else False)"
  74.322 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.323 -  by auto
  74.324 -
  74.325 -
  74.326 -subsubsection{*Less-than-or-equal *}
  74.327 -
  74.328 -lemma le_nat_number_of [simp]:
  74.329 -  "(number_of v :: nat) \<le> number_of v' \<longleftrightarrow>
  74.330 -    (if v \<le> v' then True else v \<le> Int.Pls)"
  74.331 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.332 -  by auto
  74.333 -
  74.334 -(*Maps #n to n for n = 0, 1, 2*)
  74.335 -lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
  74.336 +(*Maps #n to n for n = 1, 2*)
  74.337 +lemmas numerals = numeral_1_eq_1 [where 'a=nat] numeral_2_eq_2
  74.338  
  74.339  
  74.340  subsection{*Powers with Numeric Exponents*}
  74.341  
  74.342  text{*Squares of literal numerals will be evaluated.*}
  74.343 -lemmas power2_eq_square_number_of [simp] =
  74.344 -  power2_eq_square [of "number_of w"] for w
  74.345 +(* FIXME: replace with more general rules for powers of numerals *)
  74.346 +lemmas power2_eq_square_numeral [simp] =
  74.347 +    power2_eq_square [of "numeral w"] for w
  74.348  
  74.349  
  74.350  text{*Simprules for comparisons where common factors can be cancelled.*}
  74.351 @@ -528,8 +338,8 @@
  74.352  by simp
  74.353  
  74.354  (*Expresses a natural number constant as the Suc of another one.
  74.355 -  NOT suitable for rewriting because n recurs in the condition.*)
  74.356 -lemmas expand_Suc = Suc_pred' [of "number_of v"] for v
  74.357 +  NOT suitable for rewriting because n recurs on the right-hand side.*)
  74.358 +lemmas expand_Suc = Suc_pred' [of "numeral v", OF zero_less_numeral] for v
  74.359  
  74.360  subsubsection{*Arith *}
  74.361  
  74.362 @@ -539,7 +349,7 @@
  74.363  lemma Suc_eq_plus1_left: "Suc n = 1 + n"
  74.364    unfolding One_nat_def by simp
  74.365  
  74.366 -(* These two can be useful when m = number_of... *)
  74.367 +(* These two can be useful when m = numeral... *)
  74.368  
  74.369  lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
  74.370    unfolding One_nat_def by (cases m) simp_all
  74.371 @@ -551,231 +361,108 @@
  74.372    unfolding One_nat_def by (cases m) simp_all
  74.373  
  74.374  
  74.375 -subsection{*Comparisons involving (0::nat) *}
  74.376 -
  74.377 -text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
  74.378 -
  74.379 -lemma eq_number_of_0 [simp]:
  74.380 -  "number_of v = (0::nat) \<longleftrightarrow> v \<le> Int.Pls"
  74.381 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.382 -  by auto
  74.383 -
  74.384 -lemma eq_0_number_of [simp]:
  74.385 -  "(0::nat) = number_of v \<longleftrightarrow> v \<le> Int.Pls"
  74.386 -by (rule trans [OF eq_sym_conv eq_number_of_0])
  74.387 -
  74.388 -lemma less_0_number_of [simp]:
  74.389 -   "(0::nat) < number_of v \<longleftrightarrow> Int.Pls < v"
  74.390 -  unfolding nat_number_of_def number_of_is_id numeral_simps
  74.391 -  by simp
  74.392 -
  74.393 -lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
  74.394 -  by (simp del: semiring_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric])
  74.395 -
  74.396 -
  74.397  subsection{*Comparisons involving  @{term Suc} *}
  74.398  
  74.399 -lemma eq_number_of_Suc [simp]:
  74.400 -     "(number_of v = Suc n) =  
  74.401 -        (let pv = number_of (Int.pred v) in  
  74.402 -         if neg pv then False else nat pv = n)"
  74.403 -apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
  74.404 -                  number_of_pred nat_number_of_def 
  74.405 -            split add: split_if)
  74.406 -apply (rule_tac x = "number_of v" in spec)
  74.407 -apply (auto simp add: nat_eq_iff)
  74.408 -done
  74.409 +lemma eq_numeral_Suc [simp]: "numeral v = Suc n \<longleftrightarrow> nat (numeral v - 1) = n"
  74.410 +  by (subst expand_Suc, simp only: nat.inject nat_numeral_diff_1)
  74.411  
  74.412 -lemma Suc_eq_number_of [simp]:
  74.413 -     "(Suc n = number_of v) =  
  74.414 -        (let pv = number_of (Int.pred v) in  
  74.415 -         if neg pv then False else nat pv = n)"
  74.416 -by (rule trans [OF eq_sym_conv eq_number_of_Suc])
  74.417 +lemma Suc_eq_numeral [simp]: "Suc n = numeral v \<longleftrightarrow> n = nat (numeral v - 1)"
  74.418 +  by (subst expand_Suc, simp only: nat.inject nat_numeral_diff_1)
  74.419  
  74.420 -lemma less_number_of_Suc [simp]:
  74.421 -     "(number_of v < Suc n) =  
  74.422 -        (let pv = number_of (Int.pred v) in  
  74.423 -         if neg pv then True else nat pv < n)"
  74.424 -apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
  74.425 -                  number_of_pred nat_number_of_def  
  74.426 -            split add: split_if)
  74.427 -apply (rule_tac x = "number_of v" in spec)
  74.428 -apply (auto simp add: nat_less_iff)
  74.429 -done
  74.430 +lemma less_numeral_Suc [simp]: "numeral v < Suc n \<longleftrightarrow> nat (numeral v - 1) < n"
  74.431 +  by (subst expand_Suc, simp only: Suc_less_eq nat_numeral_diff_1)
  74.432  
  74.433 -lemma less_Suc_number_of [simp]:
  74.434 -     "(Suc n < number_of v) =  
  74.435 -        (let pv = number_of (Int.pred v) in  
  74.436 -         if neg pv then False else n < nat pv)"
  74.437 -apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
  74.438 -                  number_of_pred nat_number_of_def
  74.439 -            split add: split_if)
  74.440 -apply (rule_tac x = "number_of v" in spec)
  74.441 -apply (auto simp add: zless_nat_eq_int_zless)
  74.442 -done
  74.443 +lemma less_Suc_numeral [simp]: "Suc n < numeral v \<longleftrightarrow> n < nat (numeral v - 1)"
  74.444 +  by (subst expand_Suc, simp only: Suc_less_eq nat_numeral_diff_1)
  74.445  
  74.446 -lemma le_number_of_Suc [simp]:
  74.447 -     "(number_of v <= Suc n) =  
  74.448 -        (let pv = number_of (Int.pred v) in  
  74.449 -         if neg pv then True else nat pv <= n)"
  74.450 -by (simp add: Let_def linorder_not_less [symmetric])
  74.451 +lemma le_numeral_Suc [simp]: "numeral v \<le> Suc n \<longleftrightarrow> nat (numeral v - 1) \<le> n"
  74.452 +  by (subst expand_Suc, simp only: Suc_le_mono nat_numeral_diff_1)
  74.453  
  74.454 -lemma le_Suc_number_of [simp]:
  74.455 -     "(Suc n <= number_of v) =  
  74.456 -        (let pv = number_of (Int.pred v) in  
  74.457 -         if neg pv then False else n <= nat pv)"
  74.458 -by (simp add: Let_def linorder_not_less [symmetric])
  74.459 -
  74.460 -
  74.461 -lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min"
  74.462 -by auto
  74.463 -
  74.464 +lemma le_Suc_numeral [simp]: "Suc n \<le> numeral v \<longleftrightarrow> n \<le> nat (numeral v - 1)"
  74.465 +  by (subst expand_Suc, simp only: Suc_le_mono nat_numeral_diff_1)
  74.466  
  74.467  
  74.468  subsection{*Max and Min Combined with @{term Suc} *}
  74.469  
  74.470 -lemma max_number_of_Suc [simp]:
  74.471 -     "max (Suc n) (number_of v) =  
  74.472 -        (let pv = number_of (Int.pred v) in  
  74.473 -         if neg pv then Suc n else Suc(max n (nat pv)))"
  74.474 -apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
  74.475 -            split add: split_if nat.split)
  74.476 -apply (rule_tac x = "number_of v" in spec) 
  74.477 -apply auto
  74.478 -done
  74.479 - 
  74.480 -lemma max_Suc_number_of [simp]:
  74.481 -     "max (number_of v) (Suc n) =  
  74.482 -        (let pv = number_of (Int.pred v) in  
  74.483 -         if neg pv then Suc n else Suc(max (nat pv) n))"
  74.484 -apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
  74.485 -            split add: split_if nat.split)
  74.486 -apply (rule_tac x = "number_of v" in spec) 
  74.487 -apply auto
  74.488 -done
  74.489 - 
  74.490 -lemma min_number_of_Suc [simp]:
  74.491 -     "min (Suc n) (number_of v) =  
  74.492 -        (let pv = number_of (Int.pred v) in  
  74.493 -         if neg pv then 0 else Suc(min n (nat pv)))"
  74.494 -apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
  74.495 -            split add: split_if nat.split)
  74.496 -apply (rule_tac x = "number_of v" in spec) 
  74.497 -apply auto
  74.498 -done
  74.499 - 
  74.500 -lemma min_Suc_number_of [simp]:
  74.501 -     "min (number_of v) (Suc n) =  
  74.502 -        (let pv = number_of (Int.pred v) in  
  74.503 -         if neg pv then 0 else Suc(min (nat pv) n))"
  74.504 -apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
  74.505 -            split add: split_if nat.split)
  74.506 -apply (rule_tac x = "number_of v" in spec) 
  74.507 -apply auto
  74.508 -done
  74.509 +lemma max_Suc_numeral [simp]:
  74.510 +  "max (Suc n) (numeral v) = Suc (max n (nat (numeral v - 1)))"
  74.511 +  by (subst expand_Suc, simp only: max_Suc_Suc nat_numeral_diff_1)
  74.512 +
  74.513 +lemma max_numeral_Suc [simp]:
  74.514 +  "max (numeral v) (Suc n) = Suc (max (nat (numeral v - 1)) n)"
  74.515 +  by (subst expand_Suc, simp only: max_Suc_Suc nat_numeral_diff_1)
  74.516 +
  74.517 +lemma min_Suc_numeral [simp]:
  74.518 +  "min (Suc n) (numeral v) = Suc (min n (nat (numeral v - 1)))"
  74.519 +  by (subst expand_Suc, simp only: min_Suc_Suc nat_numeral_diff_1)
  74.520 +
  74.521 +lemma min_numeral_Suc [simp]:
  74.522 +  "min (numeral v) (Suc n) = Suc (min (nat (numeral v - 1)) n)"
  74.523 +  by (subst expand_Suc, simp only: min_Suc_Suc nat_numeral_diff_1)
  74.524   
  74.525  subsection{*Literal arithmetic involving powers*}
  74.526  
  74.527 -lemma power_nat_number_of:
  74.528 -     "(number_of v :: nat) ^ n =  
  74.529 -       (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
  74.530 -by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
  74.531 -         split add: split_if cong: imp_cong)
  74.532 +(* TODO: replace with more generic rule for powers of numerals *)
  74.533 +lemma power_nat_numeral:
  74.534 +  "(numeral v :: nat) ^ n = nat ((numeral v :: int) ^ n)"
  74.535 +  by (simp only: nat_power_eq zero_le_numeral nat_numeral)
  74.536  
  74.537 -
  74.538 -lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w"] for w
  74.539 -declare power_nat_number_of_number_of [simp]
  74.540 -
  74.541 +lemmas power_nat_numeral_numeral = power_nat_numeral [of _ "numeral w"] for w
  74.542 +declare power_nat_numeral_numeral [simp]
  74.543  
  74.544  
  74.545  text{*For arbitrary rings*}
  74.546  
  74.547 -lemma power_number_of_even:
  74.548 +lemma power_numeral_even:
  74.549    fixes z :: "'a::monoid_mult"
  74.550 -  shows "z ^ number_of (Int.Bit0 w) = (let w = z ^ (number_of w) in w * w)"
  74.551 -by (cases "w \<ge> 0") (auto simp add: Let_def Bit0_def nat_number_of_def number_of_is_id
  74.552 -  nat_add_distrib power_add simp del: nat_number_of)
  74.553 +  shows "z ^ numeral (Num.Bit0 w) = (let w = z ^ (numeral w) in w * w)"
  74.554 +  unfolding numeral_Bit0 power_add Let_def ..
  74.555  
  74.556 -lemma power_number_of_odd:
  74.557 +lemma power_numeral_odd:
  74.558    fixes z :: "'a::monoid_mult"
  74.559 -  shows "z ^ number_of (Int.Bit1 w) = (if (0::int) <= number_of w
  74.560 -     then (let w = z ^ (number_of w) in z * w * w) else 1)"
  74.561 -unfolding Let_def Bit1_def nat_number_of_def number_of_is_id
  74.562 -apply (cases "0 <= w")
  74.563 -apply (simp only: mult_assoc nat_add_distrib power_add, simp)
  74.564 -apply (simp add: not_le mult_2 [symmetric] add_assoc)
  74.565 -done
  74.566 +  shows "z ^ numeral (Num.Bit1 w) = (let w = z ^ (numeral w) in z * w * w)"
  74.567 +  unfolding numeral_Bit1 One_nat_def add_Suc_right add_0_right
  74.568 +  unfolding power_Suc power_add Let_def mult_assoc ..
  74.569  
  74.570 -lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
  74.571 -lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
  74.572 +lemmas zpower_numeral_even = power_numeral_even [where 'a=int]
  74.573 +lemmas zpower_numeral_odd = power_numeral_odd [where 'a=int]
  74.574  
  74.575 -lemmas power_number_of_even_number_of [simp] =
  74.576 -    power_number_of_even [of "number_of v"] for v
  74.577 +lemmas power_numeral_even_numeral [simp] =
  74.578 +    power_numeral_even [of "numeral v"] for v
  74.579  
  74.580 -lemmas power_number_of_odd_number_of [simp] =
  74.581 -    power_number_of_odd [of "number_of v"] for v
  74.582 +lemmas power_numeral_odd_numeral [simp] =
  74.583 +    power_numeral_odd [of "numeral v"] for v
  74.584  
  74.585 -lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
  74.586 -  by (simp add: nat_number_of_def)
  74.587 +lemma nat_numeral_Bit0:
  74.588 +  "numeral (Num.Bit0 w) = (let n::nat = numeral w in n + n)"
  74.589 +  unfolding numeral_Bit0 Let_def ..
  74.590  
  74.591 -lemma nat_number_of_Min [no_atp]: "number_of Int.Min = (0::nat)"
  74.592 -  apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
  74.593 -  done
  74.594 -
  74.595 -lemma nat_number_of_Bit0:
  74.596 -    "number_of (Int.Bit0 w) = (let n::nat = number_of w in n + n)"
  74.597 -by (cases "w \<ge> 0") (auto simp add: Let_def Bit0_def nat_number_of_def number_of_is_id
  74.598 -  nat_add_distrib simp del: nat_number_of)
  74.599 -
  74.600 -lemma nat_number_of_Bit1:
  74.601 -  "number_of (Int.Bit1 w) =
  74.602 -    (if neg (number_of w :: int) then 0
  74.603 -     else let n = number_of w in Suc (n + n))"
  74.604 -unfolding Let_def Bit1_def nat_number_of_def number_of_is_id neg_def
  74.605 -apply (cases "w < 0")
  74.606 -apply (simp add: mult_2 [symmetric] add_assoc)
  74.607 -apply (simp only: nat_add_distrib, simp)
  74.608 -done
  74.609 +lemma nat_numeral_Bit1:
  74.610 +  "numeral (Num.Bit1 w) = (let n = numeral w in Suc (n + n))"
  74.611 +  unfolding numeral_Bit1 Let_def by simp
  74.612  
  74.613  lemmas eval_nat_numeral =
  74.614 -  nat_number_of_Bit0 nat_number_of_Bit1
  74.615 +  nat_numeral_Bit0 nat_numeral_Bit1
  74.616  
  74.617  lemmas nat_arith =
  74.618 -  add_nat_number_of
  74.619 -  diff_nat_number_of
  74.620 -  mult_nat_number_of
  74.621 -  eq_nat_number_of
  74.622 -  less_nat_number_of
  74.623 +  diff_nat_numeral
  74.624  
  74.625  lemmas semiring_norm =
  74.626 -  Let_def arith_simps nat_arith rel_simps neg_simps if_False
  74.627 -  if_True add_0 add_Suc add_number_of_left mult_number_of_left
  74.628 +  Let_def arith_simps nat_arith rel_simps
  74.629 +  if_False if_True
  74.630 +  add_0 add_Suc add_numeral_left
  74.631 +  add_neg_numeral_left mult_numeral_left
  74.632    numeral_1_eq_1 [symmetric] Suc_eq_plus1
  74.633 -  numeral_0_eq_0 [symmetric] numerals [symmetric]
  74.634 -  not_iszero_Numeral1
  74.635 +  eq_numeral_iff_iszero not_iszero_Numeral1
  74.636  
  74.637  lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
  74.638    by (fact Let_def)
  74.639  
  74.640 -lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring})"
  74.641 -  by (simp only: number_of_Min power_minus1_even)
  74.642 +lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::ring_1)"
  74.643 +  by (fact power_minus1_even) (* FIXME: duplicate *)
  74.644  
  74.645 -lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring})"
  74.646 -  by (simp only: number_of_Min power_minus1_odd)
  74.647 -
  74.648 -lemma nat_number_of_add_left:
  74.649 -     "number_of v + (number_of v' + (k::nat)) =  
  74.650 -         (if neg (number_of v :: int) then number_of v' + k  
  74.651 -          else if neg (number_of v' :: int) then number_of v + k  
  74.652 -          else number_of (v + v') + k)"
  74.653 -by (auto simp add: neg_def)
  74.654 -
  74.655 -lemma nat_number_of_mult_left:
  74.656 -     "number_of v * (number_of v' * (k::nat)) =  
  74.657 -         (if v < Int.Pls then 0
  74.658 -          else number_of (v * v') * k)"
  74.659 -by (auto simp add: not_less Pls_def nat_number_of_def number_of_is_id
  74.660 -  nat_mult_distrib simp del: nat_number_of)
  74.661 +lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::ring_1)"
  74.662 +  by (fact power_minus1_odd) (* FIXME: duplicate *)
  74.663  
  74.664  
  74.665  subsection{*Literal arithmetic and @{term of_nat}*}
  74.666 @@ -784,52 +471,18 @@
  74.667       "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
  74.668  by (simp only: mult_2 nat_add_distrib of_nat_add) 
  74.669  
  74.670 -lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
  74.671 -by (simp only: nat_number_of_def)
  74.672 -
  74.673 -lemma of_nat_number_of_lemma:
  74.674 -     "of_nat (number_of v :: nat) =  
  74.675 -         (if 0 \<le> (number_of v :: int) 
  74.676 -          then (number_of v :: 'a :: number_semiring)
  74.677 -          else 0)"
  74.678 -  by (auto simp add: int_number_of_def nat_number_of_def number_of_int
  74.679 -    elim!: nonneg_int_cases)
  74.680 -
  74.681 -lemma of_nat_number_of_eq [simp]:
  74.682 -     "of_nat (number_of v :: nat) =  
  74.683 -         (if neg (number_of v :: int) then 0  
  74.684 -          else (number_of v :: 'a :: number_semiring))"
  74.685 -  by (simp only: of_nat_number_of_lemma neg_def, simp)
  74.686 -
  74.687  
  74.688  subsubsection{*For simplifying @{term "Suc m - K"} and  @{term "K - Suc m"}*}
  74.689  
  74.690  text{*Where K above is a literal*}
  74.691  
  74.692 -lemma Suc_diff_eq_diff_pred: "Numeral0 < n ==> Suc m - n = m - (n - Numeral1)"
  74.693 +lemma Suc_diff_eq_diff_pred: "0 < n ==> Suc m - n = m - (n - Numeral1)"
  74.694  by (simp split: nat_diff_split)
  74.695  
  74.696 -text {*Now just instantiating @{text n} to @{text "number_of v"} does
  74.697 -  the right simplification, but with some redundant inequality
  74.698 -  tests.*}
  74.699 -lemma neg_number_of_pred_iff_0:
  74.700 -  "neg (number_of (Int.pred v)::int) = (number_of v = (0::nat))"
  74.701 -apply (subgoal_tac "neg (number_of (Int.pred v)) = (number_of v < Suc 0) ")
  74.702 -apply (simp only: less_Suc_eq_le le_0_eq)
  74.703 -apply (subst less_number_of_Suc, simp)
  74.704 -done
  74.705 -
  74.706  text{*No longer required as a simprule because of the @{text inverse_fold}
  74.707     simproc*}
  74.708 -lemma Suc_diff_number_of:
  74.709 -     "Int.Pls < v ==>
  74.710 -      Suc m - (number_of v) = m - (number_of (Int.pred v))"
  74.711 -apply (subst Suc_diff_eq_diff_pred)
  74.712 -apply simp
  74.713 -apply (simp del: semiring_numeral_1_eq_1)
  74.714 -apply (auto simp only: diff_nat_number_of less_0_number_of [symmetric]
  74.715 -                        neg_number_of_pred_iff_0)
  74.716 -done
  74.717 +lemma Suc_diff_numeral: "Suc m - (numeral v) = m - (numeral v - 1)"
  74.718 +  by (subst expand_Suc, simp only: diff_Suc_Suc)
  74.719  
  74.720  lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
  74.721  by (simp split: nat_diff_split)
  74.722 @@ -837,45 +490,22 @@
  74.723  
  74.724  subsubsection{*For @{term nat_case} and @{term nat_rec}*}
  74.725  
  74.726 -lemma nat_case_number_of [simp]:
  74.727 -     "nat_case a f (number_of v) =
  74.728 -        (let pv = number_of (Int.pred v) in
  74.729 -         if neg pv then a else f (nat pv))"
  74.730 -by (simp split add: nat.split add: Let_def neg_number_of_pred_iff_0)
  74.731 +lemma nat_case_numeral [simp]:
  74.732 +  "nat_case a f (numeral v) = (let pv = nat (numeral v - 1) in f pv)"
  74.733 +  by (subst expand_Suc, simp only: nat.cases nat_numeral_diff_1 Let_def)
  74.734  
  74.735  lemma nat_case_add_eq_if [simp]:
  74.736 -     "nat_case a f ((number_of v) + n) =
  74.737 -       (let pv = number_of (Int.pred v) in
  74.738 -         if neg pv then nat_case a f n else f (nat pv + n))"
  74.739 -apply (subst add_eq_if)
  74.740 -apply (simp split add: nat.split
  74.741 -            del: semiring_numeral_1_eq_1
  74.742 -            add: semiring_numeral_1_eq_1 [symmetric]
  74.743 -                 numeral_1_eq_Suc_0 [symmetric]
  74.744 -                 neg_number_of_pred_iff_0)
  74.745 -done
  74.746 +  "nat_case a f ((numeral v) + n) = (let pv = nat (numeral v - 1) in f (pv + n))"
  74.747 +  by (subst expand_Suc, simp only: nat.cases nat_numeral_diff_1 Let_def add_Suc)
  74.748  
  74.749 -lemma nat_rec_number_of [simp]:
  74.750 -     "nat_rec a f (number_of v) =
  74.751 -        (let pv = number_of (Int.pred v) in
  74.752 -         if neg pv then a else f (nat pv) (nat_rec a f (nat pv)))"
  74.753 -apply (case_tac " (number_of v) ::nat")
  74.754 -apply (simp_all (no_asm_simp) add: Let_def neg_number_of_pred_iff_0)
  74.755 -apply (simp split add: split_if_asm)
  74.756 -done
  74.757 +lemma nat_rec_numeral [simp]:
  74.758 +  "nat_rec a f (numeral v) = (let pv = nat (numeral v - 1) in f pv (nat_rec a f pv))"
  74.759 +  by (subst expand_Suc, simp only: nat_rec_Suc nat_numeral_diff_1 Let_def)
  74.760  
  74.761  lemma nat_rec_add_eq_if [simp]:
  74.762 -     "nat_rec a f (number_of v + n) =
  74.763 -        (let pv = number_of (Int.pred v) in
  74.764 -         if neg pv then nat_rec a f n
  74.765 -                   else f (nat pv + n) (nat_rec a f (nat pv + n)))"
  74.766 -apply (subst add_eq_if)
  74.767 -apply (simp split add: nat.split
  74.768 -            del: semiring_numeral_1_eq_1
  74.769 -            add: semiring_numeral_1_eq_1 [symmetric]
  74.770 -                 numeral_1_eq_Suc_0 [symmetric]
  74.771 -                 neg_number_of_pred_iff_0)
  74.772 -done
  74.773 +  "nat_rec a f (numeral v + n) =
  74.774 +    (let pv = nat (numeral v - 1) in f (pv + n) (nat_rec a f (pv + n)))"
  74.775 +  by (subst expand_Suc, simp only: nat_rec_Suc nat_numeral_diff_1 Let_def add_Suc)
  74.776  
  74.777  
  74.778  subsubsection{*Various Other Lemmas*}
  74.779 @@ -887,14 +517,14 @@
  74.780  
  74.781  text{*Lemmas for specialist use, NOT as default simprules*}
  74.782  lemma nat_mult_2: "2 * z = (z+z::nat)"
  74.783 -by (rule semiring_mult_2)
  74.784 +by (rule mult_2) (* FIXME: duplicate *)
  74.785  
  74.786  lemma nat_mult_2_right: "z * 2 = (z+z::nat)"
  74.787 -by (rule semiring_mult_2_right)
  74.788 +by (rule mult_2_right) (* FIXME: duplicate *)
  74.789  
  74.790  text{*Case analysis on @{term "n<2"}*}
  74.791  lemma less_2_cases: "(n::nat) < 2 ==> n = 0 | n = Suc 0"
  74.792 -by (auto simp add: nat_1_add_1 [symmetric])
  74.793 +by (auto simp add: numeral_2_eq_2)
  74.794  
  74.795  text{*Removal of Small Numerals: 0, 1 and (in additive positions) 2*}
  74.796  
  74.797 @@ -908,4 +538,8 @@
  74.798  lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
  74.799  by simp
  74.800  
  74.801 +text{*Legacy theorems*}
  74.802 +
  74.803 +lemmas nat_1_add_1 = one_add_one [where 'a=nat]
  74.804 +
  74.805  end
    75.1 --- a/src/HOL/Nitpick_Examples/Manual_Nits.thy	Fri Mar 23 20:32:43 2012 +0100
    75.2 +++ b/src/HOL/Nitpick_Examples/Manual_Nits.thy	Mon Mar 26 10:56:56 2012 +0200
    75.3 @@ -115,6 +115,7 @@
    75.4  "add_raw \<equiv> \<lambda>(x, y) (u, v). (x + (u\<Colon>nat), y + (v\<Colon>nat))"
    75.5  
    75.6  quotient_definition "add\<Colon>my_int \<Rightarrow> my_int \<Rightarrow> my_int" is add_raw
    75.7 +unfolding add_raw_def by auto
    75.8  
    75.9  lemma "add x y = add x x"
   75.10  nitpick [show_datatypes, expect = genuine]
    76.1 --- a/src/HOL/Nominal/Nominal.thy	Fri Mar 23 20:32:43 2012 +0100
    76.2 +++ b/src/HOL/Nominal/Nominal.thy	Mon Mar 26 10:56:56 2012 +0200
    76.3 @@ -3481,7 +3481,7 @@
    76.4  by (auto simp add: perm_nat_def)
    76.5  
    76.6  lemma numeral_nat_eqvt: 
    76.7 - shows "pi\<bullet>((number_of n)::nat) = number_of n" 
    76.8 + shows "pi\<bullet>((numeral n)::nat) = numeral n" 
    76.9  by (simp add: perm_nat_def perm_int_def)
   76.10  
   76.11  lemma max_nat_eqvt:
   76.12 @@ -3523,7 +3523,11 @@
   76.13  by (simp add: perm_int_def)
   76.14  
   76.15  lemma numeral_int_eqvt: 
   76.16 - shows "pi\<bullet>((number_of n)::int) = number_of n" 
   76.17 + shows "pi\<bullet>((numeral n)::int) = numeral n" 
   76.18 +by (simp add: perm_int_def perm_int_def)
   76.19 +
   76.20 +lemma neg_numeral_int_eqvt:
   76.21 + shows "pi\<bullet>((neg_numeral n)::int) = neg_numeral n"
   76.22  by (simp add: perm_int_def perm_int_def)
   76.23  
   76.24  lemma max_int_eqvt:
   76.25 @@ -3589,7 +3593,7 @@
   76.26  (* the lemmas numeral_nat_eqvt numeral_int_eqvt do not conform with the *)
   76.27  (* usual form of an eqvt-lemma, but they are needed for analysing       *)
   76.28  (* permutations on nats and ints *)
   76.29 -lemmas [eqvt_force] = numeral_nat_eqvt numeral_int_eqvt
   76.30 +lemmas [eqvt_force] = numeral_nat_eqvt numeral_int_eqvt neg_numeral_int_eqvt
   76.31  
   76.32  (***************************************)
   76.33  (* setup for the individial atom-kinds *)
    77.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    77.2 +++ b/src/HOL/Num.thy	Mon Mar 26 10:56:56 2012 +0200
    77.3 @@ -0,0 +1,1021 @@
    77.4 +(*  Title:      HOL/Num.thy
    77.5 +    Author:     Florian Haftmann
    77.6 +    Author:     Brian Huffman
    77.7 +*)
    77.8 +
    77.9 +header {* Binary Numerals *}
   77.10 +
   77.11 +theory Num
   77.12 +imports Datatype Power
   77.13 +begin
   77.14 +
   77.15 +subsection {* The @{text num} type *}
   77.16 +
   77.17 +datatype num = One | Bit0 num | Bit1 num
   77.18 +
   77.19 +text {* Increment function for type @{typ num} *}
   77.20 +
   77.21 +primrec inc :: "num \<Rightarrow> num" where
   77.22 +  "inc One = Bit0 One" |
   77.23 +  "inc (Bit0 x) = Bit1 x" |
   77.24 +  "inc (Bit1 x) = Bit0 (inc x)"
   77.25 +
   77.26 +text {* Converting between type @{typ num} and type @{typ nat} *}
   77.27 +
   77.28 +primrec nat_of_num :: "num \<Rightarrow> nat" where
   77.29 +  "nat_of_num One = Suc 0" |
   77.30 +  "nat_of_num (Bit0 x) = nat_of_num x + nat_of_num x" |
   77.31 +  "nat_of_num (Bit1 x) = Suc (nat_of_num x + nat_of_num x)"
   77.32 +
   77.33 +primrec num_of_nat :: "nat \<Rightarrow> num" where
   77.34 +  "num_of_nat 0 = One" |
   77.35 +  "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
   77.36 +
   77.37 +lemma nat_of_num_pos: "0 < nat_of_num x"
   77.38 +  by (induct x) simp_all
   77.39 +
   77.40 +lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0"
   77.41 +  by (induct x) simp_all
   77.42 +
   77.43 +lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
   77.44 +  by (induct x) simp_all
   77.45 +
   77.46 +lemma num_of_nat_double:
   77.47 +  "0 < n \<Longrightarrow> num_of_nat (n + n) = Bit0 (num_of_nat n)"
   77.48 +  by (induct n) simp_all
   77.49 +
   77.50 +text {*
   77.51 +  Type @{typ num} is isomorphic to the strictly positive
   77.52 +  natural numbers.
   77.53 +*}
   77.54 +
   77.55 +lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
   77.56 +  by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
   77.57 +
   77.58 +lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
   77.59 +  by (induct n) (simp_all add: nat_of_num_inc)
   77.60 +
   77.61 +lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
   77.62 +  apply safe
   77.63 +  apply (drule arg_cong [where f=num_of_nat])
   77.64 +  apply (simp add: nat_of_num_inverse)
   77.65 +  done
   77.66 +
   77.67 +lemma num_induct [case_names One inc]:
   77.68 +  fixes P :: "num \<Rightarrow> bool"
   77.69 +  assumes One: "P One"
   77.70 +    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
   77.71 +  shows "P x"
   77.72 +proof -
   77.73 +  obtain n where n: "Suc n = nat_of_num x"
   77.74 +    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
   77.75 +  have "P (num_of_nat (Suc n))"
   77.76 +  proof (induct n)
   77.77 +    case 0 show ?case using One by simp
   77.78 +  next
   77.79 +    case (Suc n)
   77.80 +    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
   77.81 +    then show "P (num_of_nat (Suc (Suc n)))" by simp
   77.82 +  qed
   77.83 +  with n show "P x"
   77.84 +    by (simp add: nat_of_num_inverse)
   77.85 +qed
   77.86 +
   77.87 +text {*
   77.88 +  From now on, there are two possible models for @{typ num}:
   77.89 +  as positive naturals (rule @{text "num_induct"})
   77.90 +  and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}).
   77.91 +*}
   77.92 +
   77.93 +
   77.94 +subsection {* Numeral operations *}
   77.95 +
   77.96 +instantiation num :: "{plus,times,linorder}"
   77.97 +begin
   77.98 +
   77.99 +definition [code del]:
  77.100 +  "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
  77.101 +
  77.102 +definition [code del]:
  77.103 +  "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
  77.104 +
  77.105 +definition [code del]:
  77.106 +  "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
  77.107 +
  77.108 +definition [code del]:
  77.109 +  "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
  77.110 +
  77.111 +instance
  77.112 +  by (default, auto simp add: less_num_def less_eq_num_def num_eq_iff)
  77.113 +
  77.114 +end
  77.115 +
  77.116 +lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
  77.117 +  unfolding plus_num_def
  77.118 +  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
  77.119 +
  77.120 +lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
  77.121 +  unfolding times_num_def
  77.122 +  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
  77.123 +
  77.124 +lemma add_num_simps [simp, code]:
  77.125 +  "One + One = Bit0 One"
  77.126 +  "One + Bit0 n = Bit1 n"
  77.127 +  "One + Bit1 n = Bit0 (n + One)"
  77.128 +  "Bit0 m + One = Bit1 m"
  77.129 +  "Bit0 m + Bit0 n = Bit0 (m + n)"
  77.130 +  "Bit0 m + Bit1 n = Bit1 (m + n)"
  77.131 +  "Bit1 m + One = Bit0 (m + One)"
  77.132 +  "Bit1 m + Bit0 n = Bit1 (m + n)"
  77.133 +  "Bit1 m + Bit1 n = Bit0 (m + n + One)"
  77.134 +  by (simp_all add: num_eq_iff nat_of_num_add)
  77.135 +
  77.136 +lemma mult_num_simps [simp, code]:
  77.137 +  "m * One = m"
  77.138 +  "One * n = n"
  77.139 +  "Bit0 m * Bit0 n = Bit0 (Bit0 (m * n))"
  77.140 +  "Bit0 m * Bit1 n = Bit0 (m * Bit1 n)"
  77.141 +  "Bit1 m * Bit0 n = Bit0 (Bit1 m * n)"
  77.142 +  "Bit1 m * Bit1 n = Bit1 (m + n + Bit0 (m * n))"
  77.143 +  by (simp_all add: num_eq_iff nat_of_num_add
  77.144 +    nat_of_num_mult left_distrib right_distrib)
  77.145 +
  77.146 +lemma eq_num_simps:
  77.147 +  "One = One \<longleftrightarrow> True"
  77.148 +  "One = Bit0 n \<longleftrightarrow> False"
  77.149 +  "One = Bit1 n \<longleftrightarrow> False"
  77.150 +  "Bit0 m = One \<longleftrightarrow> False"
  77.151 +  "Bit1 m = One \<longleftrightarrow> False"
  77.152 +  "Bit0 m = Bit0 n \<longleftrightarrow> m = n"
  77.153 +  "Bit0 m = Bit1 n \<longleftrightarrow> False"
  77.154 +  "Bit1 m = Bit0 n \<longleftrightarrow> False"
  77.155 +  "Bit1 m = Bit1 n \<longleftrightarrow> m = n"
  77.156 +  by simp_all
  77.157 +
  77.158 +lemma le_num_simps [simp, code]:
  77.159 +  "One \<le> n \<longleftrightarrow> True"
  77.160 +  "Bit0 m \<le> One \<longleftrightarrow> False"
  77.161 +  "Bit1 m \<le> One \<longleftrightarrow> False"
  77.162 +  "Bit0 m \<le> Bit0 n \<longleftrightarrow> m \<le> n"
  77.163 +  "Bit0 m \<le> Bit1 n \<longleftrightarrow> m \<le> n"
  77.164 +  "Bit1 m \<le> Bit1 n \<longleftrightarrow> m \<le> n"
  77.165 +  "Bit1 m \<le> Bit0 n \<longleftrightarrow> m < n"
  77.166 +  using nat_of_num_pos [of n] nat_of_num_pos [of m]
  77.167 +  by (auto simp add: less_eq_num_def less_num_def)
  77.168 +
  77.169 +lemma less_num_simps [simp, code]:
  77.170 +  "m < One \<longleftrightarrow> False"
  77.171 +  "One < Bit0 n \<longleftrightarrow> True"
  77.172 +  "One < Bit1 n \<longleftrightarrow> True"
  77.173 +  "Bit0 m < Bit0 n \<longleftrightarrow> m < n"
  77.174 +  "Bit0 m < Bit1 n \<longleftrightarrow> m \<le> n"
  77.175 +  "Bit1 m < Bit1 n \<longleftrightarrow> m < n"
  77.176 +  "Bit1 m < Bit0 n \<longleftrightarrow> m < n"
  77.177 +  using nat_of_num_pos [of n] nat_of_num_pos [of m]
  77.178 +  by (auto simp add: less_eq_num_def less_num_def)
  77.179 +
  77.180 +text {* Rules using @{text One} and @{text inc} as constructors *}
  77.181 +
  77.182 +lemma add_One: "x + One = inc x"
  77.183 +  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
  77.184 +
  77.185 +lemma add_One_commute: "One + n = n + One"
  77.186 +  by (induct n) simp_all
  77.187 +
  77.188 +lemma add_inc: "x + inc y = inc (x + y)"
  77.189 +  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
  77.190 +
  77.191 +lemma mult_inc: "x * inc y = x * y + x"
  77.192 +  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
  77.193 +
  77.194 +text {* The @{const num_of_nat} conversion *}
  77.195 +
  77.196 +lemma num_of_nat_One:
  77.197 +  "n \<le> 1 \<Longrightarrow> num_of_nat n = Num.One"
  77.198 +  by (cases n) simp_all
  77.199 +
  77.200 +lemma num_of_nat_plus_distrib:
  77.201 +  "0 < m \<Longrightarrow> 0 < n \<Longrightarrow> num_of_nat (m + n) = num_of_nat m + num_of_nat n"
  77.202 +  by (induct n) (auto simp add: add_One add_One_commute add_inc)
  77.203 +
  77.204 +text {* A double-and-decrement function *}
  77.205 +
  77.206 +primrec BitM :: "num \<Rightarrow> num" where
  77.207 +  "BitM One = One" |
  77.208 +  "BitM (Bit0 n) = Bit1 (BitM n)" |
  77.209 +  "BitM (Bit1 n) = Bit1 (Bit0 n)"
  77.210 +
  77.211 +lemma BitM_plus_one: "BitM n + One = Bit0 n"
  77.212 +  by (induct n) simp_all
  77.213 +
  77.214 +lemma one_plus_BitM: "One + BitM n = Bit0 n"
  77.215 +  unfolding add_One_commute BitM_plus_one ..
  77.216 +
  77.217 +text {* Squaring and exponentiation *}
  77.218 +
  77.219 +primrec sqr :: "num \<Rightarrow> num" where
  77.220 +  "sqr One = One" |
  77.221 +  "sqr (Bit0 n) = Bit0 (Bit0 (sqr n))" |
  77.222 +  "sqr (Bit1 n) = Bit1 (Bit0 (sqr n + n))"
  77.223 +
  77.224 +primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
  77.225 +  "pow x One = x" |
  77.226 +  "pow x (Bit0 y) = sqr (pow x y)" |
  77.227 +  "pow x (Bit1 y) = x * sqr (pow x y)"
  77.228 +
  77.229 +lemma nat_of_num_sqr: "nat_of_num (sqr x) = nat_of_num x * nat_of_num x"
  77.230 +  by (induct x, simp_all add: algebra_simps nat_of_num_add)
  77.231 +
  77.232 +lemma sqr_conv_mult: "sqr x = x * x"
  77.233 +  by (simp add: num_eq_iff nat_of_num_sqr nat_of_num_mult)
  77.234 +
  77.235 +
  77.236 +subsection {* Numary numerals *}
  77.237 +
  77.238 +text {*
  77.239 +  We embed numary representations into a generic algebraic
  77.240 +  structure using @{text numeral}.
  77.241 +*}
  77.242 +
  77.243 +class numeral = one + semigroup_add
  77.244 +begin
  77.245 +
  77.246 +primrec numeral :: "num \<Rightarrow> 'a" where
  77.247 +  numeral_One: "numeral One = 1" |
  77.248 +  numeral_Bit0: "numeral (Bit0 n) = numeral n + numeral n" |
  77.249 +  numeral_Bit1: "numeral (Bit1 n) = numeral n + numeral n + 1"
  77.250 +
  77.251 +lemma one_plus_numeral_commute: "1 + numeral x = numeral x + 1"
  77.252 +  apply (induct x)
  77.253 +  apply simp
  77.254 +  apply (simp add: add_assoc [symmetric], simp add: add_assoc)
  77.255 +  apply (simp add: add_assoc [symmetric], simp add: add_assoc)
  77.256 +  done
  77.257 +
  77.258 +lemma numeral_inc: "numeral (inc x) = numeral x + 1"
  77.259 +proof (induct x)
  77.260 +  case (Bit1 x)
  77.261 +  have "numeral x + (1 + numeral x) + 1 = numeral x + (numeral x + 1) + 1"
  77.262 +    by (simp only: one_plus_numeral_commute)
  77.263 +  with Bit1 show ?case
  77.264 +    by (simp add: add_assoc)
  77.265 +qed simp_all
  77.266 +
  77.267 +declare numeral.simps [simp del]
  77.268 +
  77.269 +abbreviation "Numeral1 \<equiv> numeral One"
  77.270 +
  77.271 +declare numeral_One [code_post]
  77.272 +
  77.273 +end
  77.274 +
  77.275 +text {* Negative numerals. *}
  77.276 +
  77.277 +class neg_numeral = numeral + group_add
  77.278 +begin
  77.279 +
  77.280 +definition neg_numeral :: "num \<Rightarrow> 'a" where
  77.281 +  "neg_numeral k = - numeral k"
  77.282 +
  77.283 +end
  77.284 +
  77.285 +text {* Numeral syntax. *}
  77.286 +
  77.287 +syntax
  77.288 +  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
  77.289 +
  77.290 +parse_translation {*
  77.291 +let
  77.292 +  fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2)
  77.293 +     of (0, 1) => Syntax.const @{const_name One}
  77.294 +      | (n, 0) => Syntax.const @{const_name Bit0} $ num_of_int n
  77.295 +      | (n, 1) => Syntax.const @{const_name Bit1} $ num_of_int n
  77.296 +    else raise Match;
  77.297 +  val pos = Syntax.const @{const_name numeral}
  77.298 +  val neg = Syntax.const @{const_name neg_numeral}
  77.299 +  val one = Syntax.const @{const_name Groups.one}
  77.300 +  val zero = Syntax.const @{const_name Groups.zero}
  77.301 +  fun numeral_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] =
  77.302 +        c $ numeral_tr [t] $ u
  77.303 +    | numeral_tr [Const (num, _)] =
  77.304 +        let
  77.305 +          val {value, ...} = Lexicon.read_xnum num;
  77.306 +        in
  77.307 +          if value = 0 then zero else
  77.308 +          if value > 0
  77.309 +          then pos $ num_of_int value
  77.310 +          else neg $ num_of_int (~value)
  77.311 +        end
  77.312 +    | numeral_tr ts = raise TERM ("numeral_tr", ts);
  77.313 +in [("_Numeral", numeral_tr)] end
  77.314 +*}
  77.315 +
  77.316 +typed_print_translation (advanced) {*
  77.317 +let
  77.318 +  fun dest_num (Const (@{const_syntax Bit0}, _) $ n) = 2 * dest_num n
  77.319 +    | dest_num (Const (@{const_syntax Bit1}, _) $ n) = 2 * dest_num n + 1
  77.320 +    | dest_num (Const (@{const_syntax One}, _)) = 1;
  77.321 +  fun num_tr' sign ctxt T [n] =
  77.322 +    let
  77.323 +      val k = dest_num n;
  77.324 +      val t' = Syntax.const @{syntax_const "_Numeral"} $
  77.325 +        Syntax.free (sign ^ string_of_int k);
  77.326 +    in
  77.327 +      case T of
  77.328 +        Type (@{type_name fun}, [_, T']) =>
  77.329 +          if not (Config.get ctxt show_types) andalso can Term.dest_Type T' then t'
  77.330 +          else Syntax.const @{syntax_const "_constrain"} $ t' $ Syntax_Phases.term_of_typ ctxt T'
  77.331 +      | T' => if T' = dummyT then t' else raise Match
  77.332 +    end;
  77.333 +in [(@{const_syntax numeral}, num_tr' ""),
  77.334 +    (@{const_syntax neg_numeral}, num_tr' "-")] end
  77.335 +*}
  77.336 +
  77.337 +subsection {* Class-specific numeral rules *}
  77.338 +
  77.339 +text {*
  77.340 +  @{const numeral} is a morphism.
  77.341 +*}
  77.342 +
  77.343 +subsubsection {* Structures with addition: class @{text numeral} *}
  77.344 +
  77.345 +context numeral
  77.346 +begin
  77.347 +
  77.348 +lemma numeral_add: "numeral (m + n) = numeral m + numeral n"
  77.349 +  by (induct n rule: num_induct)
  77.350 +     (simp_all only: numeral_One add_One add_inc numeral_inc add_assoc)
  77.351 +
  77.352 +lemma numeral_plus_numeral: "numeral m + numeral n = numeral (m + n)"
  77.353 +  by (rule numeral_add [symmetric])
  77.354 +
  77.355 +lemma numeral_plus_one: "numeral n + 1 = numeral (n + One)"
  77.356 +  using numeral_add [of n One] by (simp add: numeral_One)
  77.357 +
  77.358 +lemma one_plus_numeral: "1 + numeral n = numeral (One + n)"
  77.359 +  using numeral_add [of One n] by (simp add: numeral_One)
  77.360 +
  77.361 +lemma one_add_one: "1 + 1 = 2"
  77.362 +  using numeral_add [of One One] by (simp add: numeral_One)
  77.363 +
  77.364 +lemmas add_numeral_special =
  77.365 +  numeral_plus_one one_plus_numeral one_add_one
  77.366 +
  77.367 +end
  77.368 +
  77.369 +subsubsection {*
  77.370 +  Structures with negation: class @{text neg_numeral}
  77.371 +*}
  77.372 +
  77.373 +context neg_numeral
  77.374 +begin
  77.375 +
  77.376 +text {* Numerals form an abelian subgroup. *}
  77.377 +
  77.378 +inductive is_num :: "'a \<Rightarrow> bool" where
  77.379 +  "is_num 1" |
  77.380 +  "is_num x \<Longrightarrow> is_num (- x)" |
  77.381 +  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> is_num (x + y)"
  77.382 +
  77.383 +lemma is_num_numeral: "is_num (numeral k)"
  77.384 +  by (induct k, simp_all add: numeral.simps is_num.intros)
  77.385 +
  77.386 +lemma is_num_add_commute:
  77.387 +  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + y = y + x"
  77.388 +  apply (induct x rule: is_num.induct)
  77.389 +  apply (induct y rule: is_num.induct)
  77.390 +  apply simp
  77.391 +  apply (rule_tac a=x in add_left_imp_eq)
  77.392 +  apply (rule_tac a=x in add_right_imp_eq)
  77.393 +  apply (simp add: add_assoc minus_add_cancel)
  77.394 +  apply (simp add: add_assoc [symmetric], simp add: add_assoc)
  77.395 +  apply (rule_tac a=x in add_left_imp_eq)
  77.396 +  apply (rule_tac a=x in add_right_imp_eq)
  77.397 +  apply (simp add: add_assoc minus_add_cancel add_minus_cancel)
  77.398 +  apply (simp add: add_assoc, simp add: add_assoc [symmetric])
  77.399 +  done
  77.400 +
  77.401 +lemma is_num_add_left_commute:
  77.402 +  "\<lbrakk>is_num x; is_num y\<rbrakk> \<Longrightarrow> x + (y + z) = y + (x + z)"
  77.403 +  by (simp only: add_assoc [symmetric] is_num_add_commute)
  77.404 +
  77.405 +lemmas is_num_normalize =
  77.406 +  add_assoc is_num_add_commute is_num_add_left_commute
  77.407 +  is_num.intros is_num_numeral
  77.408 +  diff_minus minus_add add_minus_cancel minus_add_cancel
  77.409 +
  77.410 +definition dbl :: "'a \<Rightarrow> 'a" where "dbl x = x + x"
  77.411 +definition dbl_inc :: "'a \<Rightarrow> 'a" where "dbl_inc x = x + x + 1"
  77.412 +definition dbl_dec :: "'a \<Rightarrow> 'a" where "dbl_dec x = x + x - 1"
  77.413 +
  77.414 +definition sub :: "num \<Rightarrow> num \<Rightarrow> 'a" where
  77.415 +  "sub k l = numeral k - numeral l"
  77.416 +
  77.417 +lemma numeral_BitM: "numeral (BitM n) = numeral (Bit0 n) - 1"
  77.418 +  by (simp only: BitM_plus_one [symmetric] numeral_add numeral_One eq_diff_eq)
  77.419 +
  77.420 +lemma dbl_simps [simp]:
  77.421 +  "dbl (neg_numeral k) = neg_numeral (Bit0 k)"
  77.422 +  "dbl 0 = 0"
  77.423 +  "dbl 1 = 2"
  77.424 +  "dbl (numeral k) = numeral (Bit0 k)"
  77.425 +  unfolding dbl_def neg_numeral_def numeral.simps
  77.426 +  by (simp_all add: minus_add)
  77.427 +
  77.428 +lemma dbl_inc_simps [simp]:
  77.429 +  "dbl_inc (neg_numeral k) = neg_numeral (BitM k)"
  77.430 +  "dbl_inc 0 = 1"
  77.431 +  "dbl_inc 1 = 3"
  77.432 +  "dbl_inc (numeral k) = numeral (Bit1 k)"
  77.433 +  unfolding dbl_inc_def neg_numeral_def numeral.simps numeral_BitM
  77.434 +  by (simp_all add: is_num_normalize)
  77.435 +
  77.436 +lemma dbl_dec_simps [simp]:
  77.437 +  "dbl_dec (neg_numeral k) = neg_numeral (Bit1 k)"
  77.438 +  "dbl_dec 0 = -1"
  77.439 +  "dbl_dec 1 = 1"
  77.440 +  "dbl_dec (numeral k) = numeral (BitM k)"
  77.441 +  unfolding dbl_dec_def neg_numeral_def numeral.simps numeral_BitM
  77.442 +  by (simp_all add: is_num_normalize)
  77.443 +
  77.444 +lemma sub_num_simps [simp]:
  77.445 +  "sub One One = 0"
  77.446 +  "sub One (Bit0 l) = neg_numeral (BitM l)"
  77.447 +  "sub One (Bit1 l) = neg_numeral (Bit0 l)"
  77.448 +  "sub (Bit0 k) One = numeral (BitM k)"
  77.449 +  "sub (Bit1 k) One = numeral (Bit0 k)"
  77.450 +  "sub (Bit0 k) (Bit0 l) = dbl (sub k l)"
  77.451 +  "sub (Bit0 k) (Bit1 l) = dbl_dec (sub k l)"
  77.452 +  "sub (Bit1 k) (Bit0 l) = dbl_inc (sub k l)"
  77.453 +  "sub (Bit1 k) (Bit1 l) = dbl (sub k l)"
  77.454 +  unfolding dbl_def dbl_dec_def dbl_inc_def sub_def
  77.455 +  unfolding neg_numeral_def numeral.simps numeral_BitM
  77.456 +  by (simp_all add: is_num_normalize)
  77.457 +
  77.458 +lemma add_neg_numeral_simps:
  77.459 +  "numeral m + neg_numeral n = sub m n"
  77.460 +  "neg_numeral m + numeral n = sub n m"
  77.461 +  "neg_numeral m + neg_numeral n = neg_numeral (m + n)"
  77.462 +  unfolding sub_def diff_minus neg_numeral_def numeral_add numeral.simps
  77.463 +  by (simp_all add: is_num_normalize)
  77.464 +
  77.465 +lemma add_neg_numeral_special:
  77.466 +  "1 + neg_numeral m = sub One m"
  77.467 +  "neg_numeral m + 1 = sub One m"
  77.468 +  unfolding sub_def diff_minus neg_numeral_def numeral_add numeral.simps
  77.469 +  by (simp_all add: is_num_normalize)
  77.470 +
  77.471 +lemma diff_numeral_simps:
  77.472 +  "numeral m - numeral n = sub m n"
  77.473 +  "numeral m - neg_numeral n = numeral (m + n)"
  77.474 +  "neg_numeral m - numeral n = neg_numeral (m + n)"
  77.475 +  "neg_numeral m - neg_numeral n = sub n m"
  77.476 +  unfolding neg_numeral_def sub_def diff_minus numeral_add numeral.simps
  77.477 +  by (simp_all add: is_num_normalize)
  77.478 +
  77.479 +lemma diff_numeral_special:
  77.480 +  "1 - numeral n = sub One n"
  77.481 +  "1 - neg_numeral n = numeral (One + n)"
  77.482 +  "numeral m - 1 = sub m One"
  77.483 +  "neg_numeral m - 1 = neg_numeral (m + One)"
  77.484 +  unfolding neg_numeral_def sub_def diff_minus numeral_add numeral.simps
  77.485 +  by (simp_all add: is_num_normalize)
  77.486 +
  77.487 +lemma minus_one: "- 1 = -1"
  77.488 +  unfolding neg_numeral_def numeral.simps ..
  77.489 +
  77.490 +lemma minus_numeral: "- numeral n = neg_numeral n"
  77.491 +  unfolding neg_numeral_def ..
  77.492 +
  77.493 +lemma minus_neg_numeral: "- neg_numeral n = numeral n"
  77.494 +  unfolding neg_numeral_def by simp
  77.495 +
  77.496 +lemmas minus_numeral_simps [simp] =
  77.497 +  minus_one minus_numeral minus_neg_numeral
  77.498 +
  77.499 +end
  77.500 +
  77.501 +subsubsection {*
  77.502 +  Structures with multiplication: class @{text semiring_numeral}
  77.503 +*}
  77.504 +
  77.505 +class semiring_numeral = semiring + monoid_mult
  77.506 +begin
  77.507 +
  77.508 +subclass numeral ..
  77.509 +
  77.510 +lemma numeral_mult: "numeral (m * n) = numeral m * numeral n"
  77.511 +  apply (induct n rule: num_induct)
  77.512 +  apply (simp add: numeral_One)
  77.513 +  apply (simp add: mult_inc numeral_inc numeral_add numeral_inc right_distrib)
  77.514 +  done
  77.515 +
  77.516 +lemma numeral_times_numeral: "numeral m * numeral n = numeral (m * n)"
  77.517 +  by (rule numeral_mult [symmetric])
  77.518 +
  77.519 +end
  77.520 +
  77.521 +subsubsection {*
  77.522 +  Structures with a zero: class @{text semiring_1}
  77.523 +*}
  77.524 +
  77.525 +context semiring_1
  77.526 +begin
  77.527 +
  77.528 +subclass semiring_numeral ..
  77.529 +
  77.530 +lemma of_nat_numeral [simp]: "of_nat (numeral n) = numeral n"
  77.531 +  by (induct n,
  77.532 +    simp_all only: numeral.simps numeral_class.numeral.simps of_nat_add of_nat_1)
  77.533 +
  77.534 +end
  77.535 +
  77.536 +lemma nat_of_num_numeral: "nat_of_num = numeral"
  77.537 +proof
  77.538 +  fix n
  77.539 +  have "numeral n = nat_of_num n"
  77.540 +    by (induct n) (simp_all add: numeral.simps)
  77.541 +  then show "nat_of_num n = numeral n" by simp
  77.542 +qed
  77.543 +
  77.544 +subsubsection {*
  77.545 +  Equality: class @{text semiring_char_0}
  77.546 +*}
  77.547 +
  77.548 +context semiring_char_0
  77.549 +begin
  77.550 +
  77.551 +lemma numeral_eq_iff: "numeral m = numeral n \<longleftrightarrow> m = n"
  77.552 +  unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
  77.553 +    of_nat_eq_iff num_eq_iff ..
  77.554 +
  77.555 +lemma numeral_eq_one_iff: "numeral n = 1 \<longleftrightarrow> n = One"
  77.556 +  by (rule numeral_eq_iff [of n One, unfolded numeral_One])
  77.557 +
  77.558 +lemma one_eq_numeral_iff: "1 = numeral n \<longleftrightarrow> One = n"
  77.559 +  by (rule numeral_eq_iff [of One n, unfolded numeral_One])
  77.560 +
  77.561 +lemma numeral_neq_zero: "numeral n \<noteq> 0"
  77.562 +  unfolding of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
  77.563 +  by (simp add: nat_of_num_pos)
  77.564 +
  77.565 +lemma zero_neq_numeral: "0 \<noteq> numeral n"
  77.566 +  unfolding eq_commute [of 0] by (rule numeral_neq_zero)
  77.567 +
  77.568 +lemmas eq_numeral_simps [simp] =
  77.569 +  numeral_eq_iff
  77.570 +  numeral_eq_one_iff
  77.571 +  one_eq_numeral_iff
  77.572 +  numeral_neq_zero
  77.573 +  zero_neq_numeral
  77.574 +
  77.575 +end
  77.576 +
  77.577 +subsubsection {*
  77.578 +  Comparisons: class @{text linordered_semidom}
  77.579 +*}
  77.580 +
  77.581 +text {*  Could be perhaps more general than here. *}
  77.582 +
  77.583 +context linordered_semidom
  77.584 +begin
  77.585 +
  77.586 +lemma numeral_le_iff: "numeral m \<le> numeral n \<longleftrightarrow> m \<le> n"
  77.587 +proof -
  77.588 +  have "of_nat (numeral m) \<le> of_nat (numeral n) \<longleftrightarrow> m \<le> n"
  77.589 +    unfolding less_eq_num_def nat_of_num_numeral of_nat_le_iff ..
  77.590 +  then show ?thesis by simp
  77.591 +qed
  77.592 +
  77.593 +lemma one_le_numeral: "1 \<le> numeral n"
  77.594 +using numeral_le_iff [of One n] by (simp add: numeral_One)
  77.595 +
  77.596 +lemma numeral_le_one_iff: "numeral n \<le> 1 \<longleftrightarrow> n \<le> One"
  77.597 +using numeral_le_iff [of n One] by (simp add: numeral_One)
  77.598 +
  77.599 +lemma numeral_less_iff: "numeral m < numeral n \<longleftrightarrow> m < n"
  77.600 +proof -
  77.601 +  have "of_nat (numeral m) < of_nat (numeral n) \<longleftrightarrow> m < n"
  77.602 +    unfolding less_num_def nat_of_num_numeral of_nat_less_iff ..
  77.603 +  then show ?thesis by simp
  77.604 +qed
  77.605 +
  77.606 +lemma not_numeral_less_one: "\<not> numeral n < 1"
  77.607 +  using numeral_less_iff [of n One] by (simp add: numeral_One)
  77.608 +
  77.609 +lemma one_less_numeral_iff: "1 < numeral n \<longleftrightarrow> One < n"
  77.610 +  using numeral_less_iff [of One n] by (simp add: numeral_One)
  77.611 +
  77.612 +lemma zero_le_numeral: "0 \<le> numeral n"
  77.613 +  by (induct n) (simp_all add: numeral.simps)
  77.614 +
  77.615 +lemma zero_less_numeral: "0 < numeral n"
  77.616 +  by (induct n) (simp_all add: numeral.simps add_pos_pos)
  77.617 +
  77.618 +lemma not_numeral_le_zero: "\<not> numeral n \<le> 0"
  77.619 +  by (simp add: not_le zero_less_numeral)
  77.620 +
  77.621 +lemma not_numeral_less_zero: "\<not> numeral n < 0"
  77.622 +  by (simp add: not_less zero_le_numeral)
  77.623 +
  77.624 +lemmas le_numeral_extra =
  77.625 +  zero_le_one not_one_le_zero
  77.626 +  order_refl [of 0] order_refl [of 1]
  77.627 +
  77.628 +lemmas less_numeral_extra =
  77.629 +  zero_less_one not_one_less_zero
  77.630 +  less_irrefl [of 0] less_irrefl [of 1]
  77.631 +
  77.632 +lemmas le_numeral_simps [simp] =
  77.633 +  numeral_le_iff
  77.634 +  one_le_numeral
  77.635 +  numeral_le_one_iff
  77.636 +  zero_le_numeral
  77.637 +  not_numeral_le_zero
  77.638 +
  77.639 +lemmas less_numeral_simps [simp] =
  77.640 +  numeral_less_iff
  77.641 +  one_less_numeral_iff
  77.642 +  not_numeral_less_one
  77.643 +  zero_less_numeral
  77.644 +  not_numeral_less_zero
  77.645 +
  77.646 +end
  77.647 +
  77.648 +subsubsection {*
  77.649 +  Multiplication and negation: class @{text ring_1}
  77.650 +*}
  77.651 +
  77.652 +context ring_1
  77.653 +begin
  77.654 +
  77.655 +subclass neg_numeral ..
  77.656 +
  77.657 +lemma mult_neg_numeral_simps:
  77.658 +  "neg_numeral m * neg_numeral n = numeral (m * n)"
  77.659 +  "neg_numeral m * numeral n = neg_numeral (m * n)"
  77.660 +  "numeral m * neg_numeral n = neg_numeral (m * n)"
  77.661 +  unfolding neg_numeral_def mult_minus_left mult_minus_right
  77.662 +  by (simp_all only: minus_minus numeral_mult)
  77.663 +
  77.664 +lemma mult_minus1 [simp]: "-1 * z = - z"
  77.665 +  unfolding neg_numeral_def numeral.simps mult_minus_left by simp
  77.666 +
  77.667 +lemma mult_minus1_right [simp]: "z * -1 = - z"
  77.668 +  unfolding neg_numeral_def numeral.simps mult_minus_right by simp
  77.669 +
  77.670 +end
  77.671 +
  77.672 +subsubsection {*
  77.673 +  Equality using @{text iszero} for rings with non-zero characteristic
  77.674 +*}
  77.675 +
  77.676 +context ring_1
  77.677 +begin
  77.678 +
  77.679 +definition iszero :: "'a \<Rightarrow> bool"
  77.680 +  where "iszero z \<longleftrightarrow> z = 0"
  77.681 +
  77.682 +lemma iszero_0 [simp]: "iszero 0"
  77.683 +  by (simp add: iszero_def)
  77.684 +
  77.685 +lemma not_iszero_1 [simp]: "\<not> iszero 1"
  77.686 +  by (simp add: iszero_def)
  77.687 +
  77.688 +lemma not_iszero_Numeral1: "\<not> iszero Numeral1"
  77.689 +  by (simp add: numeral_One)
  77.690 +
  77.691 +lemma iszero_neg_numeral [simp]:
  77.692 +  "iszero (neg_numeral w) \<longleftrightarrow> iszero (numeral w)"
  77.693 +  unfolding iszero_def neg_numeral_def
  77.694 +  by (rule neg_equal_0_iff_equal)
  77.695 +
  77.696 +lemma eq_iff_iszero_diff: "x = y \<longleftrightarrow> iszero (x - y)"
  77.697 +  unfolding iszero_def by (rule eq_iff_diff_eq_0)
  77.698 +
  77.699 +text {* The @{text "eq_numeral_iff_iszero"} lemmas are not declared
  77.700 +@{text "[simp]"} by default, because for rings of characteristic zero,
  77.701 +better simp rules are possible. For a type like integers mod @{text
  77.702 +"n"}, type-instantiated versions of these rules should be added to the
  77.703 +simplifier, along with a type-specific rule for deciding propositions
  77.704 +of the form @{text "iszero (numeral w)"}.
  77.705 +
  77.706 +bh: Maybe it would not be so bad to just declare these as simp
  77.707 +rules anyway? I should test whether these rules take precedence over
  77.708 +the @{text "ring_char_0"} rules in the simplifier.
  77.709 +*}
  77.710 +
  77.711 +lemma eq_numeral_iff_iszero:
  77.712 +  "numeral x = numeral y \<longleftrightarrow> iszero (sub x y)"
  77.713 +  "numeral x = neg_numeral y \<longleftrightarrow> iszero (numeral (x + y))"
  77.714 +  "neg_numeral x = numeral y \<longleftrightarrow> iszero (numeral (x + y))"
  77.715 +  "neg_numeral x = neg_numeral y \<longleftrightarrow> iszero (sub y x)"
  77.716 +  "numeral x = 1 \<longleftrightarrow> iszero (sub x One)"
  77.717 +  "1 = numeral y \<longleftrightarrow> iszero (sub One y)"
  77.718 +  "neg_numeral x = 1 \<longleftrightarrow> iszero (numeral (x + One))"
  77.719 +  "1 = neg_numeral y \<longleftrightarrow> iszero (numeral (One + y))"
  77.720 +  "numeral x = 0 \<longleftrightarrow> iszero (numeral x)"
  77.721 +  "0 = numeral y \<longleftrightarrow> iszero (numeral y)"
  77.722 +  "neg_numeral x = 0 \<longleftrightarrow> iszero (numeral x)"
  77.723 +  "0 = neg_numeral y \<longleftrightarrow> iszero (numeral y)"
  77.724 +  unfolding eq_iff_iszero_diff diff_numeral_simps diff_numeral_special
  77.725 +  by simp_all
  77.726 +
  77.727 +end
  77.728 +
  77.729 +subsubsection {*
  77.730 +  Equality and negation: class @{text ring_char_0}
  77.731 +*}
  77.732 +
  77.733 +class ring_char_0 = ring_1 + semiring_char_0
  77.734 +begin
  77.735 +
  77.736 +lemma not_iszero_numeral [simp]: "\<not> iszero (numeral w)"
  77.737 +  by (simp add: iszero_def)
  77.738 +
  77.739 +lemma neg_numeral_eq_iff: "neg_numeral m = neg_numeral n \<longleftrightarrow> m = n"
  77.740 +  by (simp only: neg_numeral_def neg_equal_iff_equal numeral_eq_iff)
  77.741 +
  77.742 +lemma numeral_neq_neg_numeral: "numeral m \<noteq> neg_numeral n"
  77.743 +  unfolding neg_numeral_def eq_neg_iff_add_eq_0
  77.744 +  by (simp add: numeral_plus_numeral)
  77.745 +
  77.746 +lemma neg_numeral_neq_numeral: "neg_numeral m \<noteq> numeral n"
  77.747 +  by (rule numeral_neq_neg_numeral [symmetric])
  77.748 +
  77.749 +lemma zero_neq_neg_numeral: "0 \<noteq> neg_numeral n"
  77.750 +  unfolding neg_numeral_def neg_0_equal_iff_equal by simp
  77.751 +
  77.752 +lemma neg_numeral_neq_zero: "neg_numeral n \<noteq> 0"
  77.753 +  unfolding neg_numeral_def neg_equal_0_iff_equal by simp
  77.754 +
  77.755 +lemma one_neq_neg_numeral: "1 \<noteq> neg_numeral n"
  77.756 +  using numeral_neq_neg_numeral [of One n] by (simp add: numeral_One)
  77.757 +
  77.758 +lemma neg_numeral_neq_one: "neg_numeral n \<noteq> 1"
  77.759 +  using neg_numeral_neq_numeral [of n One] by (simp add: numeral_One)
  77.760 +
  77.761 +lemmas eq_neg_numeral_simps [simp] =
  77.762 +  neg_numeral_eq_iff
  77.763 +  numeral_neq_neg_numeral neg_numeral_neq_numeral
  77.764 +  one_neq_neg_numeral neg_numeral_neq_one
  77.765 +  zero_neq_neg_numeral neg_numeral_neq_zero
  77.766 +
  77.767 +end
  77.768 +
  77.769 +subsubsection {*
  77.770 +  Structures with negation and order: class @{text linordered_idom}
  77.771 +*}
  77.772 +
  77.773 +context linordered_idom
  77.774 +begin
  77.775 +
  77.776 +subclass ring_char_0 ..
  77.777 +
  77.778 +lemma neg_numeral_le_iff: "neg_numeral m \<le> neg_numeral n \<longleftrightarrow> n \<le> m"
  77.779 +  by (simp only: neg_numeral_def neg_le_iff_le numeral_le_iff)
  77.780 +
  77.781 +lemma neg_numeral_less_iff: "neg_numeral m < neg_numeral n \<longleftrightarrow> n < m"
  77.782 +  by (simp only: neg_numeral_def neg_less_iff_less numeral_less_iff)
  77.783 +
  77.784 +lemma neg_numeral_less_zero: "neg_numeral n < 0"
  77.785 +  by (simp only: neg_numeral_def neg_less_0_iff_less zero_less_numeral)
  77.786 +
  77.787 +lemma neg_numeral_le_zero: "neg_numeral n \<le> 0"
  77.788 +  by (simp only: neg_numeral_def neg_le_0_iff_le zero_le_numeral)
  77.789 +
  77.790 +lemma not_zero_less_neg_numeral: "\<not> 0 < neg_numeral n"
  77.791 +  by (simp only: not_less neg_numeral_le_zero)
  77.792 +
  77.793 +lemma not_zero_le_neg_numeral: "\<not> 0 \<le> neg_numeral n"
  77.794 +  by (simp only: not_le neg_numeral_less_zero)
  77.795 +
  77.796 +lemma neg_numeral_less_numeral: "neg_numeral m < numeral n"
  77.797 +  using neg_numeral_less_zero zero_less_numeral by (rule less_trans)
  77.798 +
  77.799 +lemma neg_numeral_le_numeral: "neg_numeral m \<le> numeral n"
  77.800 +  by (simp only: less_imp_le neg_numeral_less_numeral)
  77.801 +
  77.802 +lemma not_numeral_less_neg_numeral: "\<not> numeral m < neg_numeral n"
  77.803 +  by (simp only: not_less neg_numeral_le_numeral)
  77.804 +
  77.805 +lemma not_numeral_le_neg_numeral: "\<not> numeral m \<le> neg_numeral n"
  77.806 +  by (simp only: not_le neg_numeral_less_numeral)
  77.807 +  
  77.808 +lemma neg_numeral_less_one: "neg_numeral m < 1"
  77.809 +  by (rule neg_numeral_less_numeral [of m One, unfolded numeral_One])
  77.810 +
  77.811 +lemma neg_numeral_le_one: "neg_numeral m \<le> 1"
  77.812 +  by (rule neg_numeral_le_numeral [of m One, unfolded numeral_One])
  77.813 +
  77.814 +lemma not_one_less_neg_numeral: "\<not> 1 < neg_numeral m"
  77.815 +  by (simp only: not_less neg_numeral_le_one)
  77.816 +
  77.817 +lemma not_one_le_neg_numeral: "\<not> 1 \<le> neg_numeral m"
  77.818 +  by (simp only: not_le neg_numeral_less_one)
  77.819 +
  77.820 +lemma sub_non_negative:
  77.821 +  "sub n m \<ge> 0 \<longleftrightarrow> n \<ge> m"
  77.822 +  by (simp only: sub_def le_diff_eq) simp
  77.823 +
  77.824 +lemma sub_positive:
  77.825 +  "sub n m > 0 \<longleftrightarrow> n > m"
  77.826 +  by (simp only: sub_def less_diff_eq) simp
  77.827 +
  77.828 +lemma sub_non_positive:
  77.829 +  "sub n m \<le> 0 \<longleftrightarrow> n \<le> m"
  77.830 +  by (simp only: sub_def diff_le_eq) simp
  77.831 +
  77.832 +lemma sub_negative:
  77.833 +  "sub n m < 0 \<longleftrightarrow> n < m"
  77.834 +  by (simp only: sub_def diff_less_eq) simp
  77.835 +
  77.836 +lemmas le_neg_numeral_simps [simp] =
  77.837 +  neg_numeral_le_iff
  77.838 +  neg_numeral_le_numeral not_numeral_le_neg_numeral
  77.839 +  neg_numeral_le_zero not_zero_le_neg_numeral
  77.840 +  neg_numeral_le_one not_one_le_neg_numeral
  77.841 +
  77.842 +lemmas less_neg_numeral_simps [simp] =
  77.843 +  neg_numeral_less_iff
  77.844 +  neg_numeral_less_numeral not_numeral_less_neg_numeral
  77.845 +  neg_numeral_less_zero not_zero_less_neg_numeral
  77.846 +  neg_numeral_less_one not_one_less_neg_numeral
  77.847 +
  77.848 +lemma abs_numeral [simp]: "abs (numeral n) = numeral n"
  77.849 +  by simp
  77.850 +
  77.851 +lemma abs_neg_numeral [simp]: "abs (neg_numeral n) = numeral n"
  77.852 +  by (simp only: neg_numeral_def abs_minus_cancel abs_numeral)
  77.853 +
  77.854 +end
  77.855 +
  77.856 +subsubsection {*
  77.857 +  Natural numbers
  77.858 +*}
  77.859 +
  77.860 +lemma Suc_numeral [simp]: "Suc (numeral n) = numeral (n + One)"
  77.861 +  unfolding numeral_plus_one [symmetric] by simp
  77.862 +
  77.863 +lemma nat_number:
  77.864 +  "1 = Suc 0"
  77.865 +  "numeral One = Suc 0"
  77.866 +  "numeral (Bit0 n) = Suc (numeral (BitM n))"
  77.867 +  "numeral (Bit1 n) = Suc (numeral (Bit0 n))"
  77.868 +  by (simp_all add: numeral.simps BitM_plus_one)
  77.869 +
  77.870 +subsubsection {*
  77.871 +  Structures with exponentiation
  77.872 +*}
  77.873 +
  77.874 +context semiring_numeral
  77.875 +begin
  77.876 +
  77.877 +lemma numeral_sqr: "numeral (sqr n) = numeral n * numeral n"
  77.878 +  by (simp add: sqr_conv_mult numeral_mult)
  77.879 +
  77.880 +lemma numeral_pow: "numeral (pow m n) = numeral m ^ numeral n"
  77.881 +  by (induct n, simp_all add: numeral_class.numeral.simps
  77.882 +    power_add numeral_sqr numeral_mult)
  77.883 +
  77.884 +lemma power_numeral [simp]: "numeral m ^ numeral n = numeral (pow m n)"
  77.885 +  by (rule numeral_pow [symmetric])
  77.886 +
  77.887 +end
  77.888 +
  77.889 +context semiring_1
  77.890 +begin
  77.891 +
  77.892 +lemma power_zero_numeral [simp]: "(0::'a) ^ numeral n = 0"
  77.893 +  by (induct n, simp_all add: numeral_class.numeral.simps power_add)
  77.894 +
  77.895 +end
  77.896 +
  77.897 +context ring_1
  77.898 +begin
  77.899 +
  77.900 +lemma power_minus_Bit0: "(- x) ^ numeral (Bit0 n) = x ^ numeral (Bit0 n)"
  77.901 +  by (induct n, simp_all add: numeral_class.numeral.simps power_add)
  77.902 +
  77.903 +lemma power_minus_Bit1: "(- x) ^ numeral (Bit1 n) = - (x ^ numeral (Bit1 n))"
  77.904 +  by (simp only: nat_number(4) power_Suc power_minus_Bit0 mult_minus_left)
  77.905 +
  77.906 +lemma power_neg_numeral_Bit0 [simp]:
  77.907 +  "neg_numeral m ^ numeral (Bit0 n) = numeral (pow m (Bit0 n))"
  77.908 +  by (simp only: neg_numeral_def power_minus_Bit0 power_numeral)
  77.909 +
  77.910 +lemma power_neg_numeral_Bit1 [simp]:
  77.911 +  "neg_numeral m ^ numeral (Bit1 n) = neg_numeral (pow m (Bit1 n))"
  77.912 +  by (simp only: neg_numeral_def power_minus_Bit1 power_numeral pow.simps)
  77.913 +
  77.914 +end
  77.915 +
  77.916 +subsection {* Numeral equations as default simplification rules *}
  77.917 +
  77.918 +declare (in numeral) numeral_One [simp]
  77.919 +declare (in numeral) numeral_plus_numeral [simp]
  77.920 +declare (in numeral) add_numeral_special [simp]
  77.921 +declare (in neg_numeral) add_neg_numeral_simps [simp]
  77.922 +declare (in neg_numeral) add_neg_numeral_special [simp]
  77.923 +declare (in neg_numeral) diff_numeral_simps [simp]
  77.924 +declare (in neg_numeral) diff_numeral_special [simp]
  77.925 +declare (in semiring_numeral) numeral_times_numeral [simp]
  77.926 +declare (in ring_1) mult_neg_numeral_simps [simp]
  77.927 +
  77.928 +subsection {* Setting up simprocs *}
  77.929 +
  77.930 +lemma numeral_reorient:
  77.931 +  "(numeral w = x) = (x = numeral w)"
  77.932 +  by auto
  77.933 +
  77.934 +lemma mult_numeral_1: "Numeral1 * a = (a::'a::semiring_numeral)"
  77.935 +  by simp
  77.936 +
  77.937 +lemma mult_numeral_1_right: "a * Numeral1 = (a::'a::semiring_numeral)"
  77.938 +  by simp
  77.939 +
  77.940 +lemma divide_numeral_1: "a / Numeral1 = (a::'a::field)"
  77.941 +  by simp
  77.942 +
  77.943 +lemma inverse_numeral_1:
  77.944 +  "inverse Numeral1 = (Numeral1::'a::division_ring)"
  77.945 +  by simp
  77.946 +
  77.947 +text{*Theorem lists for the cancellation simprocs. The use of a numary
  77.948 +numeral for 1 reduces the number of special cases.*}
  77.949 +
  77.950 +lemmas mult_1s =
  77.951 +  mult_numeral_1 mult_numeral_1_right 
  77.952 +  mult_minus1 mult_minus1_right
  77.953 +
  77.954 +
  77.955 +subsubsection {* Simplification of arithmetic operations on integer constants. *}
  77.956 +
  77.957 +lemmas arith_special = (* already declared simp above *)
  77.958 +  add_numeral_special add_neg_numeral_special
  77.959 +  diff_numeral_special minus_one
  77.960 +
  77.961 +(* rules already in simpset *)
  77.962 +lemmas arith_extra_simps =
  77.963 +  numeral_plus_numeral add_neg_numeral_simps add_0_left add_0_right
  77.964 +  minus_numeral minus_neg_numeral minus_zero minus_one
  77.965 +  diff_numeral_simps diff_0 diff_0_right
  77.966 +  numeral_times_numeral mult_neg_numeral_simps
  77.967 +  mult_zero_left mult_zero_right
  77.968 +  abs_numeral abs_neg_numeral
  77.969 +
  77.970 +text {*
  77.971 +  For making a minimal simpset, one must include these default simprules.
  77.972 +  Also include @{text simp_thms}.
  77.973 +*}
  77.974 +
  77.975 +lemmas arith_simps =
  77.976 +  add_num_simps mult_num_simps sub_num_simps
  77.977 +  BitM.simps dbl_simps dbl_inc_simps dbl_dec_simps
  77.978 +  abs_zero abs_one arith_extra_simps
  77.979 +
  77.980 +text {* Simplification of relational operations *}
  77.981 +
  77.982 +lemmas eq_numeral_extra =
  77.983 +  zero_neq_one one_neq_zero
  77.984 +
  77.985 +lemmas rel_simps =
  77.986 +  le_num_simps less_num_simps eq_num_simps
  77.987 +  le_numeral_simps le_neg_numeral_simps le_numeral_extra
  77.988 +  less_numeral_simps less_neg_numeral_simps less_numeral_extra
  77.989 +  eq_numeral_simps eq_neg_numeral_simps eq_numeral_extra
  77.990 +
  77.991 +
  77.992 +subsubsection {* Simplification of arithmetic when nested to the right. *}
  77.993 +
  77.994 +lemma add_numeral_left [simp]:
  77.995 +  "numeral v + (numeral w + z) = (numeral(v + w) + z)"
  77.996 +  by (simp_all add: add_assoc [symmetric])
  77.997 +
  77.998 +lemma add_neg_numeral_left [simp]:
  77.999 +  "numeral v + (neg_numeral w + y) = (sub v w + y)"
 77.1000 +  "neg_numeral v + (numeral w + y) = (sub w v + y)"
 77.1001 +  "neg_numeral v + (neg_numeral w + y) = (neg_numeral(v + w) + y)"
 77.1002 +  by (simp_all add: add_assoc [symmetric])
 77.1003 +
 77.1004 +lemma mult_numeral_left [simp]:
 77.1005 +  "numeral v * (numeral w * z) = (numeral(v * w) * z :: 'a::semiring_numeral)"
 77.1006 +  "neg_numeral v * (numeral w * y) = (neg_numeral(v * w) * y :: 'b::ring_1)"
 77.1007 +  "numeral v * (neg_numeral w * y) = (neg_numeral(v * w) * y :: 'b::ring_1)"
 77.1008 +  "neg_numeral v * (neg_numeral w * y) = (numeral(v * w) * y :: 'b::ring_1)"
 77.1009 +  by (simp_all add: mult_assoc [symmetric])
 77.1010 +
 77.1011 +hide_const (open) One Bit0 Bit1 BitM inc pow sqr sub dbl dbl_inc dbl_dec
 77.1012 +
 77.1013 +subsection {* code module namespace *}
 77.1014 +
 77.1015 +code_modulename SML
 77.1016 +  Numeral Arith
 77.1017 +
 77.1018 +code_modulename OCaml
 77.1019 +  Numeral Arith
 77.1020 +
 77.1021 +code_modulename Haskell
 77.1022 +  Numeral Arith
 77.1023 +
 77.1024 +end
    78.1 --- a/src/HOL/Number_Theory/Primes.thy	Fri Mar 23 20:32:43 2012 +0100
    78.2 +++ b/src/HOL/Number_Theory/Primes.thy	Mon Mar 26 10:56:56 2012 +0200
    78.3 @@ -206,7 +206,7 @@
    78.4      "prime (p::nat) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> set [2..<p]. \<not> n dvd p)"
    78.5    by (auto simp add: prime_nat_code)
    78.6  
    78.7 -lemmas prime_nat_simp_number_of [simp] = prime_nat_simp [of "number_of m"] for m
    78.8 +lemmas prime_nat_simp_numeral [simp] = prime_nat_simp [of "numeral m"] for m
    78.9  
   78.10  lemma prime_int_code [code]:
   78.11    "prime (p::int) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> {1<..<p}. ~ n dvd p)" (is "?L = ?R")
   78.12 @@ -222,7 +222,7 @@
   78.13  lemma prime_int_simp: "prime (p::int) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> set [2..p - 1]. ~ n dvd p)"
   78.14    by (auto simp add: prime_int_code)
   78.15  
   78.16 -lemmas prime_int_simp_number_of [simp] = prime_int_simp [of "number_of m"] for m
   78.17 +lemmas prime_int_simp_numeral [simp] = prime_int_simp [of "numeral m"] for m
   78.18  
   78.19  lemma two_is_prime_nat [simp]: "prime (2::nat)"
   78.20    by simp
    79.1 --- a/src/HOL/Numeral_Simprocs.thy	Fri Mar 23 20:32:43 2012 +0100
    79.2 +++ b/src/HOL/Numeral_Simprocs.thy	Mon Mar 26 10:56:56 2012 +0200
    79.3 @@ -14,8 +14,8 @@
    79.4    ("Tools/nat_numeral_simprocs.ML")
    79.5  begin
    79.6  
    79.7 -declare split_div [of _ _ "number_of k", arith_split] for k
    79.8 -declare split_mod [of _ _ "number_of k", arith_split] for k
    79.9 +declare split_div [of _ _ "numeral k", arith_split] for k
   79.10 +declare split_mod [of _ _ "numeral k", arith_split] for k
   79.11  
   79.12  text {* For @{text combine_numerals} *}
   79.13  
   79.14 @@ -98,72 +98,74 @@
   79.15    ("(a::'a::comm_semiring_1_cancel) * b") =
   79.16    {* fn phi => Numeral_Simprocs.assoc_fold *}
   79.17  
   79.18 +(* TODO: see whether the type class can be generalized further *)
   79.19  simproc_setup int_combine_numerals
   79.20 -  ("(i::'a::number_ring) + j" | "(i::'a::number_ring) - j") =
   79.21 +  ("(i::'a::comm_ring_1) + j" | "(i::'a::comm_ring_1) - j") =
   79.22    {* fn phi => Numeral_Simprocs.combine_numerals *}
   79.23  
   79.24  simproc_setup field_combine_numerals
   79.25 -  ("(i::'a::{field_inverse_zero,ring_char_0,number_ring}) + j"
   79.26 -  |"(i::'a::{field_inverse_zero,ring_char_0,number_ring}) - j") =
   79.27 +  ("(i::'a::{field_inverse_zero,ring_char_0}) + j"
   79.28 +  |"(i::'a::{field_inverse_zero,ring_char_0}) - j") =
   79.29    {* fn phi => Numeral_Simprocs.field_combine_numerals *}
   79.30  
   79.31  simproc_setup inteq_cancel_numerals
   79.32 -  ("(l::'a::number_ring) + m = n"
   79.33 -  |"(l::'a::number_ring) = m + n"
   79.34 -  |"(l::'a::number_ring) - m = n"
   79.35 -  |"(l::'a::number_ring) = m - n"
   79.36 -  |"(l::'a::number_ring) * m = n"
   79.37 -  |"(l::'a::number_ring) = m * n"
   79.38 -  |"- (l::'a::number_ring) = m"
   79.39 -  |"(l::'a::number_ring) = - m") =
   79.40 +  ("(l::'a::comm_ring_1) + m = n"
   79.41 +  |"(l::'a::comm_ring_1) = m + n"
   79.42 +  |"(l::'a::comm_ring_1) - m = n"
   79.43 +  |"(l::'a::comm_ring_1) = m - n"
   79.44 +  |"(l::'a::comm_ring_1) * m = n"
   79.45 +  |"(l::'a::comm_ring_1) = m * n"
   79.46 +  |"- (l::'a::comm_ring_1) = m"
   79.47 +  |"(l::'a::comm_ring_1) = - m") =
   79.48    {* fn phi => Numeral_Simprocs.eq_cancel_numerals *}
   79.49  
   79.50  simproc_setup intless_cancel_numerals
   79.51 -  ("(l::'a::{linordered_idom,number_ring}) + m < n"
   79.52 -  |"(l::'a::{linordered_idom,number_ring}) < m + n"
   79.53 -  |"(l::'a::{linordered_idom,number_ring}) - m < n"
   79.54 -  |"(l::'a::{linordered_idom,number_ring}) < m - n"
   79.55 -  |"(l::'a::{linordered_idom,number_ring}) * m < n"
   79.56 -  |"(l::'a::{linordered_idom,number_ring}) < m * n"
   79.57 -  |"- (l::'a::{linordered_idom,number_ring}) < m"
   79.58 -  |"(l::'a::{linordered_idom,number_ring}) < - m") =
   79.59 +  ("(l::'a::linordered_idom) + m < n"
   79.60 +  |"(l::'a::linordered_idom) < m + n"
   79.61 +  |"(l::'a::linordered_idom) - m < n"
   79.62 +  |"(l::'a::linordered_idom) < m - n"
   79.63 +  |"(l::'a::linordered_idom) * m < n"
   79.64 +  |"(l::'a::linordered_idom) < m * n"
   79.65 +  |"- (l::'a::linordered_idom) < m"
   79.66 +  |"(l::'a::linordered_idom) < - m") =
   79.67    {* fn phi => Numeral_Simprocs.less_cancel_numerals *}
   79.68  
   79.69  simproc_setup intle_cancel_numerals
   79.70 -  ("(l::'a::{linordered_idom,number_ring}) + m \<le> n"
   79.71 -  |"(l::'a::{linordered_idom,number_ring}) \<le> m + n"
   79.72 -  |"(l::'a::{linordered_idom,number_ring}) - m \<le> n"
   79.73 -  |"(l::'a::{linordered_idom,number_ring}) \<le> m - n"
   79.74 -  |"(l::'a::{linordered_idom,number_ring}) * m \<le> n"
   79.75 -  |"(l::'a::{linordered_idom,number_ring}) \<le> m * n"
   79.76 -  |"- (l::'a::{linordered_idom,number_ring}) \<le> m"
   79.77 -  |"(l::'a::{linordered_idom,number_ring}) \<le> - m") =
   79.78 +  ("(l::'a::linordered_idom) + m \<le> n"
   79.79 +  |"(l::'a::linordered_idom) \<le> m + n"
   79.80 +  |"(l::'a::linordered_idom) - m \<le> n"
   79.81 +  |"(l::'a::linordered_idom) \<le> m - n"
   79.82 +  |"(l::'a::linordered_idom) * m \<le> n"
   79.83 +  |"(l::'a::linordered_idom) \<le> m * n"
   79.84 +  |"- (l::'a::linordered_idom) \<le> m"
   79.85 +  |"(l::'a::linordered_idom) \<le> - m") =
   79.86    {* fn phi => Numeral_Simprocs.le_cancel_numerals *}
   79.87  
   79.88  simproc_setup ring_eq_cancel_numeral_factor
   79.89 -  ("(l::'a::{idom,ring_char_0,number_ring}) * m = n"
   79.90 -  |"(l::'a::{idom,ring_char_0,number_ring}) = m * n") =
   79.91 +  ("(l::'a::{idom,ring_char_0}) * m = n"
   79.92 +  |"(l::'a::{idom,ring_char_0}) = m * n") =
   79.93    {* fn phi => Numeral_Simprocs.eq_cancel_numeral_factor *}
   79.94  
   79.95  simproc_setup ring_less_cancel_numeral_factor
   79.96 -  ("(l::'a::{linordered_idom,number_ring}) * m < n"
   79.97 -  |"(l::'a::{linordered_idom,number_ring}) < m * n") =
   79.98 +  ("(l::'a::linordered_idom) * m < n"
   79.99 +  |"(l::'a::linordered_idom) < m * n") =
  79.100    {* fn phi => Numeral_Simprocs.less_cancel_numeral_factor *}
  79.101  
  79.102  simproc_setup ring_le_cancel_numeral_factor
  79.103 -  ("(l::'a::{linordered_idom,number_ring}) * m <= n"
  79.104 -  |"(l::'a::{linordered_idom,number_ring}) <= m * n") =
  79.105 +  ("(l::'a::linordered_idom) * m <= n"
  79.106 +  |"(l::'a::linordered_idom) <= m * n") =
  79.107    {* fn phi => Numeral_Simprocs.le_cancel_numeral_factor *}
  79.108  
  79.109 +(* TODO: remove comm_ring_1 constraint if possible *)
  79.110  simproc_setup int_div_cancel_numeral_factors
  79.111 -  ("((l::'a::{semiring_div,ring_char_0,number_ring}) * m) div n"
  79.112 -  |"(l::'a::{semiring_div,ring_char_0,number_ring}) div (m * n)") =
  79.113 +  ("((l::'a::{semiring_div,comm_ring_1,ring_char_0}) * m) div n"
  79.114 +  |"(l::'a::{semiring_div,comm_ring_1,ring_char_0}) div (m * n)") =
  79.115    {* fn phi => Numeral_Simprocs.div_cancel_numeral_factor *}
  79.116  
  79.117  simproc_setup divide_cancel_numeral_factor
  79.118 -  ("((l::'a::{field_inverse_zero,ring_char_0,number_ring}) * m) / n"
  79.119 -  |"(l::'a::{field_inverse_zero,ring_char_0,number_ring}) / (m * n)"
  79.120 -  |"((number_of v)::'a::{field_inverse_zero,ring_char_0,number_ring}) / (number_of w)") =
  79.121 +  ("((l::'a::{field_inverse_zero,ring_char_0}) * m) / n"
  79.122 +  |"(l::'a::{field_inverse_zero,ring_char_0}) / (m * n)"
  79.123 +  |"((numeral v)::'a::{field_inverse_zero,ring_char_0}) / (numeral w)") =
  79.124    {* fn phi => Numeral_Simprocs.divide_cancel_numeral_factor *}
  79.125  
  79.126  simproc_setup ring_eq_cancel_factor
  79.127 @@ -270,19 +272,25 @@
  79.128    ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
  79.129    {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_factor *}
  79.130  
  79.131 +(* FIXME: duplicate rule warnings for:
  79.132 +  ring_distribs
  79.133 +  numeral_plus_numeral numeral_times_numeral
  79.134 +  numeral_eq_iff numeral_less_iff numeral_le_iff
  79.135 +  numeral_neq_zero zero_neq_numeral zero_less_numeral
  79.136 +  if_True if_False *)
  79.137  declaration {* 
  79.138 -  K (Lin_Arith.add_simps (@{thms neg_simps} @ [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}])
  79.139 -  #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1},
  79.140 +  K (Lin_Arith.add_simps ([@{thm Suc_numeral}, @{thm int_numeral}])
  79.141 +  #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_numeral}, @{thm Let_neg_numeral}, @{thm Let_0}, @{thm Let_1},
  79.142       @{thm nat_0}, @{thm nat_1},
  79.143 -     @{thm add_nat_number_of}, @{thm diff_nat_number_of}, @{thm mult_nat_number_of},
  79.144 -     @{thm eq_nat_number_of}, @{thm less_nat_number_of}, @{thm le_number_of_eq_not_less},
  79.145 -     @{thm le_Suc_number_of}, @{thm le_number_of_Suc},
  79.146 -     @{thm less_Suc_number_of}, @{thm less_number_of_Suc},
  79.147 -     @{thm Suc_eq_number_of}, @{thm eq_number_of_Suc},
  79.148 +     @{thm numeral_plus_numeral}, @{thm diff_nat_numeral}, @{thm numeral_times_numeral},
  79.149 +     @{thm numeral_eq_iff}, @{thm numeral_less_iff}, @{thm numeral_le_iff},
  79.150 +     @{thm le_Suc_numeral}, @{thm le_numeral_Suc},
  79.151 +     @{thm less_Suc_numeral}, @{thm less_numeral_Suc},
  79.152 +     @{thm Suc_eq_numeral}, @{thm eq_numeral_Suc},
  79.153       @{thm mult_Suc}, @{thm mult_Suc_right},
  79.154       @{thm add_Suc}, @{thm add_Suc_right},
  79.155 -     @{thm eq_number_of_0}, @{thm eq_0_number_of}, @{thm less_0_number_of},
  79.156 -     @{thm of_int_number_of_eq}, @{thm of_nat_number_of_eq}, @{thm nat_number_of},
  79.157 +     @{thm numeral_neq_zero}, @{thm zero_neq_numeral}, @{thm zero_less_numeral},
  79.158 +     @{thm of_int_numeral}, @{thm of_nat_numeral}, @{thm nat_numeral},
  79.159       @{thm if_True}, @{thm if_False}])
  79.160    #> Lin_Arith.add_simprocs
  79.161        [@{simproc semiring_assoc_fold},
    80.1 --- a/src/HOL/Parity.thy	Fri Mar 23 20:32:43 2012 +0100
    80.2 +++ b/src/HOL/Parity.thy	Mon Mar 26 10:56:56 2012 +0200
    80.3 @@ -45,9 +45,11 @@
    80.4  
    80.5  lemma odd_1_nat [simp]: "odd (1::nat)" by presburger
    80.6  
    80.7 -declare even_def[of "number_of v", simp] for v
    80.8 +(* TODO: proper simp rules for Num.Bit0, Num.Bit1 *)
    80.9 +declare even_def[of "numeral v", simp] for v
   80.10 +declare even_def[of "neg_numeral v", simp] for v
   80.11  
   80.12 -declare even_nat_def[of "number_of v", simp] for v
   80.13 +declare even_nat_def[of "numeral v", simp] for v
   80.14  
   80.15  subsection {* Even and odd are mutually exclusive *}
   80.16  
   80.17 @@ -197,18 +199,18 @@
   80.18    using minus_one_even_odd_power by blast
   80.19  
   80.20  lemma neg_one_even_odd_power:
   80.21 -     "(even x --> (-1::'a::{number_ring})^x = 1) &
   80.22 +     "(even x --> (-1::'a::{comm_ring_1})^x = 1) &
   80.23        (odd x --> (-1::'a)^x = -1)"
   80.24    apply (induct x)
   80.25    apply (simp, simp)
   80.26    done
   80.27  
   80.28  lemma neg_one_even_power [simp]:
   80.29 -    "even x ==> (-1::'a::{number_ring})^x = 1"
   80.30 +    "even x ==> (-1::'a::{comm_ring_1})^x = 1"
   80.31    using neg_one_even_odd_power by blast
   80.32  
   80.33  lemma neg_one_odd_power [simp]:
   80.34 -    "odd x ==> (-1::'a::{number_ring})^x = -1"
   80.35 +    "odd x ==> (-1::'a::{comm_ring_1})^x = -1"
   80.36    using neg_one_even_odd_power by blast
   80.37  
   80.38  lemma neg_power_if:
   80.39 @@ -347,27 +349,28 @@
   80.40  
   80.41  text {* Simplify, when the exponent is a numeral *}
   80.42  
   80.43 -lemmas power_0_left_number_of = power_0_left [of "number_of w"] for w
   80.44 -declare power_0_left_number_of [simp]
   80.45 +lemma power_0_left_numeral [simp]:
   80.46 +  "0 ^ numeral w = (0::'a::{power,semiring_0})"
   80.47 +by (simp add: power_0_left)
   80.48  
   80.49 -lemmas zero_le_power_eq_number_of [simp] =
   80.50 -    zero_le_power_eq [of _ "number_of w"] for w
   80.51 +lemmas zero_le_power_eq_numeral [simp] =
   80.52 +    zero_le_power_eq [of _ "numeral w"] for w
   80.53  
   80.54 -lemmas zero_less_power_eq_number_of [simp] =
   80.55 -    zero_less_power_eq [of _ "number_of w"] for w
   80.56 +lemmas zero_less_power_eq_numeral [simp] =
   80.57 +    zero_less_power_eq [of _ "numeral w"] for w
   80.58  
   80.59 -lemmas power_le_zero_eq_number_of [simp] =
   80.60 -    power_le_zero_eq [of _ "number_of w"] for w
   80.61 +lemmas power_le_zero_eq_numeral [simp] =
   80.62 +    power_le_zero_eq [of _ "numeral w"] for w
   80.63  
   80.64 -lemmas power_less_zero_eq_number_of [simp] =
   80.65 -    power_less_zero_eq [of _ "number_of w"] for w
   80.66 +lemmas power_less_zero_eq_numeral [simp] =
   80.67 +    power_less_zero_eq [of _ "numeral w"] for w
   80.68  
   80.69 -lemmas zero_less_power_nat_eq_number_of [simp] =
   80.70 -    zero_less_power_nat_eq [of _ "number_of w"] for w
   80.71 +lemmas zero_less_power_nat_eq_numeral [simp] =
   80.72 +    zero_less_power_nat_eq [of _ "numeral w"] for w
   80.73  
   80.74 -lemmas power_eq_0_iff_number_of [simp] = power_eq_0_iff [of _ "number_of w"] for w
   80.75 +lemmas power_eq_0_iff_numeral [simp] = power_eq_0_iff [of _ "numeral w"] for w
   80.76  
   80.77 -lemmas power_even_abs_number_of [simp] = power_even_abs [of "number_of w" _] for w
   80.78 +lemmas power_even_abs_numeral [simp] = power_even_abs [of "numeral w" _] for w
   80.79  
   80.80  
   80.81  subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
    81.1 --- a/src/HOL/Plain.thy	Fri Mar 23 20:32:43 2012 +0100
    81.2 +++ b/src/HOL/Plain.thy	Mon Mar 26 10:56:56 2012 +0200
    81.3 @@ -1,7 +1,7 @@
    81.4  header {* Plain HOL *}
    81.5  
    81.6  theory Plain
    81.7 -imports Datatype FunDef Extraction Metis
    81.8 +imports Datatype FunDef Extraction Metis Num
    81.9  begin
   81.10  
   81.11  text {*
    82.1 --- a/src/HOL/Predicate_Compile_Examples/Predicate_Compile_Tests.thy	Fri Mar 23 20:32:43 2012 +0100
    82.2 +++ b/src/HOL/Predicate_Compile_Examples/Predicate_Compile_Tests.thy	Mon Mar 26 10:56:56 2012 +0200
    82.3 @@ -334,7 +334,7 @@
    82.4  code_pred [dseq] one_or_two .
    82.5  code_pred [random_dseq] one_or_two .
    82.6  thm one_or_two.dseq_equation
    82.7 -values [expected "{Suc 0::nat, 2::nat}"] "{x. one_or_two x}"
    82.8 +values [expected "{1::nat, 2::nat}"] "{x. one_or_two x}"
    82.9  values [random_dseq 0,0,10] 3 "{x. one_or_two x}"
   82.10  
   82.11  inductive one_or_two' :: "nat => bool"
   82.12 @@ -442,7 +442,7 @@
   82.13  values "{ys. append [0, Suc 0, 2] ys [0, Suc 0, 2, 17, 0, 5]}"
   82.14  
   82.15  values [expected "{}" dseq 0] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
   82.16 -values [expected "{(([]::nat list), [Suc 0, 2, 3, 4, (5::nat)])}" dseq 1] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
   82.17 +values [expected "{(([]::nat list), [1, 2, 3, 4, (5::nat)])}" dseq 1] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
   82.18  values [dseq 4] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
   82.19  values [dseq 6] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
   82.20  values [random_dseq 1, 1, 4] 10 "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
   82.21 @@ -1241,8 +1241,8 @@
   82.22  values [expected "{2::nat}"] "{x. plus_nat_test x 7 9}"
   82.23  values [expected "{}"] "{x. plus_nat_test x 9 7}"
   82.24  values [expected "{(0::nat,0::nat)}"] "{(x, y). plus_nat_test x y 0}"
   82.25 -values [expected "{(0, Suc 0), (Suc 0, 0)}"] "{(x, y). plus_nat_test x y 1}"
   82.26 -values [expected "{(0, 5), (4, Suc 0), (3, 2), (2, 3), (Suc 0, 4), (5, 0)}"]
   82.27 +values [expected "{(0::nat, 1::nat), (1, 0)}"] "{(x, y). plus_nat_test x y 1}"
   82.28 +values [expected "{(0::nat, 5::nat), (4, 1), (3, 2), (2, 3), (1, 4), (5, 0)}"]
   82.29    "{(x, y). plus_nat_test x y 5}"
   82.30  
   82.31  inductive minus_nat_test :: "nat => nat => nat => bool"
   82.32 @@ -1259,7 +1259,7 @@
   82.33  values [expected "{5::nat}"] "{z. minus_nat_test 7 2 z}"
   82.34  values [expected "{16::nat}"] "{x. minus_nat_test x 7 9}"
   82.35  values [expected "{16::nat}"] "{x. minus_nat_test x 9 7}"
   82.36 -values [expected "{0, Suc 0, 2, 3}"] "{x. minus_nat_test x 3 0}"
   82.37 +values [expected "{0::nat, 1, 2, 3}"] "{x. minus_nat_test x 3 0}"
   82.38  values [expected "{0::nat}"] "{x. minus_nat_test x 0 0}"
   82.39  
   82.40  subsection {* Examples on int *}
    83.1 --- a/src/HOL/Presburger.thy	Fri Mar 23 20:32:43 2012 +0100
    83.2 +++ b/src/HOL/Presburger.thy	Mon Mar 26 10:56:56 2012 +0200
    83.3 @@ -374,18 +374,16 @@
    83.4    ((y \<le> x \<longrightarrow> P (int x - int y)) \<and> (x < y \<longrightarrow> P 0))"
    83.5    by (cases "y \<le> x") (simp_all add: zdiff_int)
    83.6  
    83.7 -lemma number_of1: "(0::int) <= number_of n \<Longrightarrow> (0::int) <= number_of (Int.Bit0 n) \<and> (0::int) <= number_of (Int.Bit1 n)"
    83.8 -by simp
    83.9 -
   83.10 -lemma number_of2: "(0::int) <= Numeral0" by simp
   83.11 -
   83.12  text {*
   83.13    \medskip Specific instances of congruence rules, to prevent
   83.14    simplifier from looping. *}
   83.15  
   83.16 -theorem imp_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<longrightarrow> P) = (0 <= x \<longrightarrow> P')" by simp
   83.17 +theorem imp_le_cong:
   83.18 +  "\<lbrakk>x = x'; 0 \<le> x' \<Longrightarrow> P = P'\<rbrakk> \<Longrightarrow> (0 \<le> (x::int) \<longrightarrow> P) = (0 \<le> x' \<longrightarrow> P')"
   83.19 +  by simp
   83.20  
   83.21 -theorem conj_le_cong: "(0 <= x \<Longrightarrow> P = P') \<Longrightarrow> (0 <= (x::int) \<and> P) = (0 <= x \<and> P')" 
   83.22 +theorem conj_le_cong:
   83.23 +  "\<lbrakk>x = x'; 0 \<le> x' \<Longrightarrow> P = P'\<rbrakk> \<Longrightarrow> (0 \<le> (x::int) \<and> P) = (0 \<le> x' \<and> P')"
   83.24    by (simp cong: conj_cong)
   83.25  
   83.26  use "Tools/Qelim/cooper.ML"
    84.1 --- a/src/HOL/Quickcheck_Examples/Quickcheck_Narrowing_Examples.thy	Fri Mar 23 20:32:43 2012 +0100
    84.2 +++ b/src/HOL/Quickcheck_Examples/Quickcheck_Narrowing_Examples.thy	Mon Mar 26 10:56:56 2012 +0200
    84.3 @@ -79,15 +79,14 @@
    84.4  quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]
    84.5  oops
    84.6  
    84.7 -(* FIXME: integer has strange representation! *)
    84.8  lemma "rev xs = xs"
    84.9  quickcheck[tester = narrowing, finite_types = false, default_type = int, expect = counterexample]
   84.10  oops
   84.11 -(*
   84.12 +
   84.13  lemma "rev xs = xs"
   84.14    quickcheck[tester = narrowing, finite_types = true, expect = counterexample]
   84.15  oops
   84.16 -*)
   84.17 +
   84.18  subsection {* Simple examples with functions *}
   84.19  
   84.20  lemma "map f xs = map g xs"
    85.1 --- a/src/HOL/Quickcheck_Narrowing.thy	Fri Mar 23 20:32:43 2012 +0100
    85.2 +++ b/src/HOL/Quickcheck_Narrowing.thy	Mon Mar 26 10:56:56 2012 +0200
    85.3 @@ -70,34 +70,15 @@
    85.4    "HOL.equal k l \<longleftrightarrow> HOL.equal (int_of k) (int_of l)"
    85.5  
    85.6  instance proof
    85.7 -qed (auto simp add: equal_code_int_def equal_int_def eq_int_refl)
    85.8 +qed (auto simp add: equal_code_int_def equal_int_def equal_int_refl)
    85.9  
   85.10  end
   85.11  
   85.12 -instantiation code_int :: number
   85.13 -begin
   85.14 -
   85.15 -definition
   85.16 -  "number_of = of_int"
   85.17 -
   85.18 -instance ..
   85.19 -
   85.20 -end
   85.21 -
   85.22 -lemma int_of_number [simp]:
   85.23 -  "int_of (number_of k) = number_of k"
   85.24 -  by (simp add: number_of_code_int_def number_of_is_id)
   85.25 -
   85.26 -
   85.27  definition nat_of :: "code_int => nat"
   85.28  where
   85.29    "nat_of i = nat (int_of i)"
   85.30 -
   85.31 -
   85.32 -code_datatype "number_of \<Colon> int \<Rightarrow> code_int"
   85.33    
   85.34 -  
   85.35 -instantiation code_int :: "{minus, linordered_semidom, semiring_div, linorder}"
   85.36 +instantiation code_int :: "{minus, linordered_semidom, semiring_div, neg_numeral, linorder}"
   85.37  begin
   85.38  
   85.39  definition [simp, code del]:
   85.40 @@ -110,6 +91,9 @@
   85.41    "n + m = of_int (int_of n + int_of m)"
   85.42  
   85.43  definition [simp, code del]:
   85.44 +  "- n = of_int (- int_of n)"
   85.45 +
   85.46 +definition [simp, code del]:
   85.47    "n - m = of_int (int_of n - int_of m)"
   85.48  
   85.49  definition [simp, code del]:
   85.50 @@ -127,34 +111,43 @@
   85.51  definition [simp, code del]:
   85.52    "n < m \<longleftrightarrow> int_of n < int_of m"
   85.53  
   85.54 -
   85.55  instance proof
   85.56  qed (auto simp add: code_int left_distrib zmult_zless_mono2)
   85.57  
   85.58  end
   85.59  
   85.60 -lemma zero_code_int_code [code, code_unfold]:
   85.61 -  "(0\<Colon>code_int) = Numeral0"
   85.62 -  by (simp add: number_of_code_int_def Pls_def)
   85.63 +lemma int_of_numeral [simp]:
   85.64 +  "int_of (numeral k) = numeral k"
   85.65 +  by (induct k) (simp_all only: numeral.simps plus_code_int_def
   85.66 +    one_code_int_def of_int_inverse UNIV_I)
   85.67 +
   85.68 +definition Num :: "num \<Rightarrow> code_int"
   85.69 +  where [code_abbrev]: "Num = numeral"
   85.70 +
   85.71 +lemma [code_abbrev]:
   85.72 +  "- numeral k = (neg_numeral k :: code_int)"
   85.73 +  by (unfold neg_numeral_def) simp
   85.74 +
   85.75 +code_datatype "0::code_int" Num
   85.76  
   85.77  lemma one_code_int_code [code, code_unfold]:
   85.78    "(1\<Colon>code_int) = Numeral1"
   85.79 -  by (simp add: number_of_code_int_def Pls_def Bit1_def)
   85.80 +  by (simp only: numeral.simps)
   85.81  
   85.82 -definition div_mod_code_int :: "code_int \<Rightarrow> code_int \<Rightarrow> code_int \<times> code_int" where
   85.83 -  [code del]: "div_mod_code_int n m = (n div m, n mod m)"
   85.84 +definition div_mod :: "code_int \<Rightarrow> code_int \<Rightarrow> code_int \<times> code_int" where
   85.85 +  [code del]: "div_mod n m = (n div m, n mod m)"
   85.86  
   85.87  lemma [code]:
   85.88 -  "div_mod_code_int n m = (if m = 0 then (0, n) else (n div m, n mod m))"
   85.89 -  unfolding div_mod_code_int_def by auto
   85.90 +  "div_mod n m = (if m = 0 then (0, n) else (n div m, n mod m))"
   85.91 +  unfolding div_mod_def by auto
   85.92  
   85.93  lemma [code]:
   85.94 -  "n div m = fst (div_mod_code_int n m)"
   85.95 -  unfolding div_mod_code_int_def by simp
   85.96 +  "n div m = fst (div_mod n m)"
   85.97 +  unfolding div_mod_def by simp
   85.98  
   85.99  lemma [code]:
  85.100 -  "n mod m = snd (div_mod_code_int n m)"
  85.101 -  unfolding div_mod_code_int_def by simp
  85.102 +  "n mod m = snd (div_mod n m)"
  85.103 +  unfolding div_mod_def by simp
  85.104  
  85.105  lemma int_of_code [code]:
  85.106    "int_of k = (if k = 0 then 0
  85.107 @@ -172,9 +165,12 @@
  85.108  code_instance code_numeral :: equal
  85.109    (Haskell_Quickcheck -)
  85.110  
  85.111 -setup {* fold (Numeral.add_code @{const_name number_code_int_inst.number_of_code_int}
  85.112 +setup {* fold (Numeral.add_code @{const_name Num}
  85.113    false Code_Printer.literal_numeral) ["Haskell_Quickcheck"]  *}
  85.114  
  85.115 +code_type code_int
  85.116 +  (Haskell_Quickcheck "Int")
  85.117 +
  85.118  code_const "0 \<Colon> code_int"
  85.119    (Haskell_Quickcheck "0")
  85.120  
  85.121 @@ -182,24 +178,23 @@
  85.122    (Haskell_Quickcheck "1")
  85.123  
  85.124  code_const "minus \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> code_int"
  85.125 -  (Haskell_Quickcheck "(_/ -/ _)")
  85.126 +  (Haskell_Quickcheck infixl 6 "-")
  85.127  
  85.128 -code_const div_mod_code_int
  85.129 +code_const div_mod
  85.130    (Haskell_Quickcheck "divMod")
  85.131  
  85.132  code_const "HOL.equal \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
  85.133    (Haskell_Quickcheck infix 4 "==")
  85.134  
  85.135 -code_const "op \<le> \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
  85.136 +code_const "less_eq \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
  85.137    (Haskell_Quickcheck infix 4 "<=")
  85.138  
  85.139 -code_const "op < \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
  85.140 +code_const "less \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
  85.141    (Haskell_Quickcheck infix 4 "<")
  85.142  
  85.143 -code_type code_int
  85.144 -  (Haskell_Quickcheck "Int")
  85.145 +code_abort of_int
  85.146  
  85.147 -code_abort of_int
  85.148 +hide_const (open) Num div_mod
  85.149  
  85.150  subsubsection {* Narrowing's deep representation of types and terms *}
  85.151  
    86.1 --- a/src/HOL/Quotient.thy	Fri Mar 23 20:32:43 2012 +0100
    86.2 +++ b/src/HOL/Quotient.thy	Mon Mar 26 10:56:56 2012 +0200
    86.3 @@ -9,7 +9,8 @@
    86.4  keywords
    86.5    "print_quotmaps" "print_quotients" "print_quotconsts" :: diag and
    86.6    "quotient_type" :: thy_goal and "/" and
    86.7 -  "quotient_definition" :: thy_decl
    86.8 +  "setup_lifting" :: thy_decl and
    86.9 +  "quotient_definition" :: thy_goal
   86.10  uses
   86.11    ("Tools/Quotient/quotient_info.ML")
   86.12    ("Tools/Quotient/quotient_type.ML")
   86.13 @@ -79,6 +80,10 @@
   86.14    shows "((op =) ===> (op =)) = (op =)"
   86.15    by (auto simp add: fun_eq_iff elim: fun_relE)
   86.16  
   86.17 +lemma fun_rel_eq_rel:
   86.18 +  shows "((op =) ===> R) = (\<lambda>f g. \<forall>x. R (f x) (g x))"
   86.19 +  by (simp add: fun_rel_def)
   86.20 +
   86.21  subsection {* set map (vimage) and set relation *}
   86.22  
   86.23  definition "set_rel R xs ys \<equiv> \<forall>x y. R x y \<longrightarrow> x \<in> xs \<longleftrightarrow> y \<in> ys"
   86.24 @@ -133,6 +138,18 @@
   86.25    unfolding Quotient_def
   86.26    by blast
   86.27  
   86.28 +lemma Quotient_refl1: 
   86.29 +  assumes a: "Quotient R Abs Rep" 
   86.30 +  shows "R r s \<Longrightarrow> R r r"
   86.31 +  using a unfolding Quotient_def 
   86.32 +  by fast
   86.33 +
   86.34 +lemma Quotient_refl2: 
   86.35 +  assumes a: "Quotient R Abs Rep" 
   86.36 +  shows "R r s \<Longrightarrow> R s s"
   86.37 +  using a unfolding Quotient_def 
   86.38 +  by fast
   86.39 +
   86.40  lemma Quotient_rel_rep:
   86.41    assumes a: "Quotient R Abs Rep"
   86.42    shows "R (Rep a) (Rep b) \<longleftrightarrow> a = b"
   86.43 @@ -259,6 +276,15 @@
   86.44    shows "R2 (f x) (g y)"
   86.45    using a by (auto elim: fun_relE)
   86.46  
   86.47 +lemma apply_rsp'':
   86.48 +  assumes "Quotient R Abs Rep"
   86.49 +  and "(R ===> S) f f"
   86.50 +  shows "S (f (Rep x)) (f (Rep x))"
   86.51 +proof -
   86.52 +  from assms(1) have "R (Rep x) (Rep x)" by (rule Quotient_rep_reflp)
   86.53 +  then show ?thesis using assms(2) by (auto intro: apply_rsp')
   86.54 +qed
   86.55 +
   86.56  subsection {* lemmas for regularisation of ball and bex *}
   86.57  
   86.58  lemma ball_reg_eqv:
   86.59 @@ -675,6 +701,153 @@
   86.60  
   86.61  end
   86.62  
   86.63 +subsection {* Quotient composition *}
   86.64 +
   86.65 +lemma OOO_quotient:
   86.66 +  fixes R1 :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   86.67 +  fixes Abs1 :: "'a \<Rightarrow> 'b" and Rep1 :: "'b \<Rightarrow> 'a"
   86.68 +  fixes Abs2 :: "'b \<Rightarrow> 'c" and Rep2 :: "'c \<Rightarrow> 'b"
   86.69 +  fixes R2' :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   86.70 +  fixes R2 :: "'b \<Rightarrow> 'b \<Rightarrow> bool"
   86.71 +  assumes R1: "Quotient R1 Abs1 Rep1"
   86.72 +  assumes R2: "Quotient R2 Abs2 Rep2"
   86.73 +  assumes Abs1: "\<And>x y. R2' x y \<Longrightarrow> R1 x x \<Longrightarrow> R1 y y \<Longrightarrow> R2 (Abs1 x) (Abs1 y)"
   86.74 +  assumes Rep1: "\<And>x y. R2 x y \<Longrightarrow> R2' (Rep1 x) (Rep1 y)"
   86.75 +  shows "Quotient (R1 OO R2' OO R1) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2)"
   86.76 +apply (rule QuotientI)
   86.77 +   apply (simp add: o_def Quotient_abs_rep [OF R2] Quotient_abs_rep [OF R1])
   86.78 +  apply simp
   86.79 +  apply (rule_tac b="Rep1 (Rep2 a)" in pred_compI)
   86.80 +   apply (rule Quotient_rep_reflp [OF R1])
   86.81 +  apply (rule_tac b="Rep1 (Rep2 a)" in pred_compI [rotated])
   86.82 +   apply (rule Quotient_rep_reflp [OF R1])
   86.83 +  apply (rule Rep1)
   86.84 +  apply (rule Quotient_rep_reflp [OF R2])
   86.85 + apply safe
   86.86 +    apply (rename_tac x y)
   86.87 +    apply (drule Abs1)
   86.88 +      apply (erule Quotient_refl2 [OF R1])
   86.89 +     apply (erule Quotient_refl1 [OF R1])
   86.90 +    apply (drule Quotient_refl1 [OF R2], drule Rep1)
   86.91 +    apply (subgoal_tac "R1 r (Rep1 (Abs1 x))")
   86.92 +     apply (rule_tac b="Rep1 (Abs1 x)" in pred_compI, assumption)
   86.93 +     apply (erule pred_compI)
   86.94 +     apply (erule Quotient_symp [OF R1, THEN sympD])
   86.95 +    apply (rule Quotient_rel[symmetric, OF R1, THEN iffD2])
   86.96 +    apply (rule conjI, erule Quotient_refl1 [OF R1])
   86.97 +    apply (rule conjI, rule Quotient_rep_reflp [OF R1])
   86.98 +    apply (subst Quotient_abs_rep [OF R1])
   86.99 +    apply (erule Quotient_rel_abs [OF R1])
  86.100 +   apply (rename_tac x y)
  86.101 +   apply (drule Abs1)
  86.102 +     apply (erule Quotient_refl2 [OF R1])
  86.103 +    apply (erule Quotient_refl1 [OF R1])
  86.104 +   apply (drule Quotient_refl2 [OF R2], drule Rep1)
  86.105 +   apply (subgoal_tac "R1 s (Rep1 (Abs1 y))")
  86.106 +    apply (rule_tac b="Rep1 (Abs1 y)" in pred_compI, assumption)
  86.107 +    apply (erule pred_compI)
  86.108 +    apply (erule Quotient_symp [OF R1, THEN sympD])
  86.109 +   apply (rule Quotient_rel[symmetric, OF R1, THEN iffD2])
  86.110 +   apply (rule conjI, erule Quotient_refl2 [OF R1])
  86.111 +   apply (rule conjI, rule Quotient_rep_reflp [OF R1])
  86.112 +   apply (subst Quotient_abs_rep [OF R1])
  86.113 +   apply (erule Quotient_rel_abs [OF R1, THEN sym])
  86.114 +  apply simp
  86.115 +  apply (rule Quotient_rel_abs [OF R2])
  86.116 +  apply (rule Quotient_rel_abs [OF R1, THEN ssubst], assumption)
  86.117 +  apply (rule Quotient_rel_abs [OF R1, THEN subst], assumption)
  86.118 +  apply (erule Abs1)
  86.119 +   apply (erule Quotient_refl2 [OF R1])
  86.120 +  apply (erule Quotient_refl1 [OF R1])
  86.121 + apply (rename_tac a b c d)
  86.122 + apply simp
  86.123 + apply (rule_tac b="Rep1 (Abs1 r)" in pred_compI)
  86.124 +  apply (rule Quotient_rel[symmetric, OF R1, THEN iffD2])
  86.125 +  apply (rule conjI, erule Quotient_refl1 [OF R1])
  86.126 +  apply (simp add: Quotient_abs_rep [OF R1] Quotient_rep_reflp [OF R1])
  86.127 + apply (rule_tac b="Rep1 (Abs1 s)" in pred_compI [rotated])
  86.128 +  apply (rule Quotient_rel[symmetric, OF R1, THEN iffD2])
  86.129 +  apply (simp add: Quotient_abs_rep [OF R1] Quotient_rep_reflp [OF R1])
  86.130 +  apply (erule Quotient_refl2 [OF R1])
  86.131 + apply (rule Rep1)
  86.132 + apply (drule Abs1)
  86.133 +   apply (erule Quotient_refl2 [OF R1])
  86.134 +  apply (erule Quotient_refl1 [OF R1])
  86.135 + apply (drule Abs1)
  86.136 +  apply (erule Quotient_refl2 [OF R1])
  86.137 + apply (erule Quotient_refl1 [OF R1])
  86.138 + apply (drule Quotient_rel_abs [OF R1])
  86.139 + apply (drule Quotient_rel_abs [OF R1])
  86.140 + apply (drule Quotient_rel_abs [OF R1])
  86.141 + apply (drule Quotient_rel_abs [OF R1])
  86.142 + apply simp
  86.143 + apply (rule Quotient_rel[symmetric, OF R2, THEN iffD2])
  86.144 + apply simp
  86.145 +done
  86.146 +
  86.147 +lemma OOO_eq_quotient:
  86.148 +  fixes R1 :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  86.149 +  fixes Abs1 :: "'a \<Rightarrow> 'b" and Rep1 :: "'b \<Rightarrow> 'a"
  86.150 +  fixes Abs2 :: "'b \<Rightarrow> 'c" and Rep2 :: "'c \<Rightarrow> 'b"
  86.151 +  assumes R1: "Quotient R1 Abs1 Rep1"
  86.152 +  assumes R2: "Quotient op= Abs2 Rep2"
  86.153 +  shows "Quotient (R1 OOO op=) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2)"
  86.154 +using assms
  86.155 +by (rule OOO_quotient) auto
  86.156 +
  86.157 +subsection {* Invariant *}
  86.158 +
  86.159 +definition invariant :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" 
  86.160 +  where "invariant R = (\<lambda>x y. R x \<and> x = y)"
  86.161 +
  86.162 +lemma invariant_to_eq:
  86.163 +  assumes "invariant P x y"
  86.164 +  shows "x = y"
  86.165 +using assms by (simp add: invariant_def)
  86.166 +
  86.167 +lemma fun_rel_eq_invariant:
  86.168 +  shows "((invariant R) ===> S) = (\<lambda>f g. \<forall>x. R x \<longrightarrow> S (f x) (g x))"
  86.169 +by (auto simp add: invariant_def fun_rel_def)
  86.170 +
  86.171 +lemma invariant_same_args:
  86.172 +  shows "invariant P x x \<equiv> P x"
  86.173 +using assms by (auto simp add: invariant_def)
  86.174 +
  86.175 +lemma copy_type_to_Quotient:
  86.176 +  assumes "type_definition Rep Abs UNIV"
  86.177 +  shows "Quotient (op =) Abs Rep"
  86.178 +proof -
  86.179 +  interpret type_definition Rep Abs UNIV by fact
  86.180 +  from Abs_inject Rep_inverse show ?thesis by (auto intro!: QuotientI)
  86.181 +qed
  86.182 +
  86.183 +lemma copy_type_to_equivp:
  86.184 +  fixes Abs :: "'a \<Rightarrow> 'b"
  86.185 +  and Rep :: "'b \<Rightarrow> 'a"
  86.186 +  assumes "type_definition Rep Abs (UNIV::'a set)"
  86.187 +  shows "equivp (op=::'a\<Rightarrow>'a\<Rightarrow>bool)"
  86.188 +by (rule identity_equivp)
  86.189 +
  86.190 +lemma invariant_type_to_Quotient:
  86.191 +  assumes "type_definition Rep Abs {x. P x}"
  86.192 +  shows "Quotient (invariant P) Abs Rep"
  86.193 +proof -
  86.194 +  interpret type_definition Rep Abs "{x. P x}" by fact
  86.195 +  from Rep Abs_inject Rep_inverse show ?thesis by (auto intro!: QuotientI simp: invariant_def)
  86.196 +qed
  86.197 +
  86.198 +lemma invariant_type_to_part_equivp:
  86.199 +  assumes "type_definition Rep Abs {x. P x}"
  86.200 +  shows "part_equivp (invariant P)"
  86.201 +proof (intro part_equivpI)
  86.202 +  interpret type_definition Rep Abs "{x. P x}" by fact
  86.203 +  show "\<exists>x. invariant P x x" using Rep by (auto simp: invariant_def)
  86.204 +next
  86.205 +  show "symp (invariant P)" by (auto intro: sympI simp: invariant_def)
  86.206 +next
  86.207 +  show "transp (invariant P)" by (auto intro: transpI simp: invariant_def)
  86.208 +qed
  86.209 +
  86.210  subsection {* ML setup *}
  86.211  
  86.212  text {* Auxiliary data for the quotient package *}
  86.213 @@ -682,8 +855,7 @@
  86.214  use "Tools/Quotient/quotient_info.ML"
  86.215  setup Quotient_Info.setup
  86.216  
  86.217 -declare [[map "fun" = fun_rel]]
  86.218 -declare [[map set = set_rel]]
  86.219 +declare [[map "fun" = (fun_rel, fun_quotient)]]
  86.220  
  86.221  lemmas [quot_thm] = fun_quotient
  86.222  lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp let_rsp id_rsp
  86.223 @@ -788,4 +960,6 @@
  86.224    map_fun (infixr "--->" 55) and
  86.225    fun_rel (infixr "===>" 55)
  86.226  
  86.227 +hide_const (open) invariant
  86.228 +
  86.229  end
    87.1 --- a/src/HOL/Quotient_Examples/DList.thy	Fri Mar 23 20:32:43 2012 +0100
    87.2 +++ b/src/HOL/Quotient_Examples/DList.thy	Mon Mar 26 10:56:56 2012 +0200
    87.3 @@ -88,45 +88,32 @@
    87.4  definition [simp]: "card_remdups = length \<circ> remdups"
    87.5  definition [simp]: "foldr_remdups f xs e = foldr f (remdups xs) e"
    87.6  
    87.7 -lemma [quot_respect]:
    87.8 -  "(dlist_eq) Nil Nil"
    87.9 -  "(dlist_eq ===> op =) List.member List.member"
   87.10 -  "(op = ===> dlist_eq ===> dlist_eq) Cons Cons"
   87.11 -  "(op = ===> dlist_eq ===> dlist_eq) removeAll removeAll"
   87.12 -  "(dlist_eq ===> op =) card_remdups card_remdups"
   87.13 -  "(dlist_eq ===> op =) remdups remdups"
   87.14 -  "(op = ===> dlist_eq ===> op =) foldr_remdups foldr_remdups"
   87.15 -  "(op = ===> dlist_eq ===> dlist_eq) map map"
   87.16 -  "(op = ===> dlist_eq ===> dlist_eq) filter filter"
   87.17 -  by (auto intro!: fun_relI simp add: remdups_filter)
   87.18 -     (metis (full_types) set_remdups remdups_eq_map_eq remdups_eq_member_eq)+
   87.19 -
   87.20  quotient_definition empty where "empty :: 'a dlist"
   87.21 -  is "Nil"
   87.22 +  is "Nil" done
   87.23  
   87.24  quotient_definition insert where "insert :: 'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist"
   87.25 -  is "Cons"
   87.26 +  is "Cons" by (metis (mono_tags) List.insert_def dlist_eq_def remdups.simps(2) set_remdups)
   87.27  
   87.28  quotient_definition "member :: 'a dlist \<Rightarrow> 'a \<Rightarrow> bool"
   87.29 -  is "List.member"
   87.30 +  is "List.member" by (metis dlist_eq_def remdups_eq_member_eq)
   87.31  
   87.32  quotient_definition foldr where "foldr :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b"
   87.33 -  is "foldr_remdups"
   87.34 +  is "foldr_remdups" by auto
   87.35  
   87.36  quotient_definition "remove :: 'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist"
   87.37 -  is "removeAll"
   87.38 +  is "removeAll" by force
   87.39  
   87.40  quotient_definition card where "card :: 'a dlist \<Rightarrow> nat"
   87.41 -  is "card_remdups"
   87.42 +  is "card_remdups" by fastforce
   87.43  
   87.44  quotient_definition map where "map :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b dlist"
   87.45 -  is "List.map :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"
   87.46 +  is "List.map :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list" by (metis dlist_eq_def remdups_eq_map_eq)
   87.47  
   87.48  quotient_definition filter where "filter :: ('a \<Rightarrow> bool) \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist"
   87.49 -  is "List.filter"
   87.50 +  is "List.filter" by (metis dlist_eq_def remdups_filter)
   87.51  
   87.52  quotient_definition "list_of_dlist :: 'a dlist \<Rightarrow> 'a list"
   87.53 -  is "remdups"
   87.54 +  is "remdups" by simp
   87.55  
   87.56  text {* lifted theorems *}
   87.57  
    88.1 --- a/src/HOL/Quotient_Examples/FSet.thy	Fri Mar 23 20:32:43 2012 +0100
    88.2 +++ b/src/HOL/Quotient_Examples/FSet.thy	Mon Mar 26 10:56:56 2012 +0200
    88.3 @@ -179,140 +179,6 @@
    88.4    by (rule quotient_compose_list_g, rule Quotient_fset, rule list_eq_equivp)
    88.5  
    88.6  
    88.7 -
    88.8 -subsection {* Respectfulness lemmas for list operations *}
    88.9 -
   88.10 -lemma list_equiv_rsp [quot_respect]:
   88.11 -  shows "(op \<approx> ===> op \<approx> ===> op =) op \<approx> op \<approx>"
   88.12 -  by (auto intro!: fun_relI)
   88.13 -
   88.14 -lemma append_rsp [quot_respect]:
   88.15 -  shows "(op \<approx> ===> op \<approx> ===> op \<approx>) append append"
   88.16 -  by (auto intro!: fun_relI)
   88.17 -
   88.18 -lemma sub_list_rsp [quot_respect]:
   88.19 -  shows "(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list"
   88.20 -  by (auto intro!: fun_relI)
   88.21 -
   88.22 -lemma member_rsp [quot_respect]:
   88.23 -  shows "(op \<approx> ===> op =) List.member List.member"
   88.24 -proof
   88.25 -  fix x y assume "x \<approx> y"
   88.26 -  then show "List.member x = List.member y"
   88.27 -    unfolding fun_eq_iff by simp
   88.28 -qed
   88.29 -
   88.30 -lemma nil_rsp [quot_respect]:
   88.31 -  shows "(op \<approx>) Nil Nil"
   88.32 -  by simp
   88.33 -
   88.34 -lemma cons_rsp [quot_respect]:
   88.35 -  shows "(op = ===> op \<approx> ===> op \<approx>) Cons Cons"
   88.36 -  by (auto intro!: fun_relI)
   88.37 -
   88.38 -lemma map_rsp [quot_respect]:
   88.39 -  shows "(op = ===> op \<approx> ===> op \<approx>) map map"
   88.40 -  by (auto intro!: fun_relI)
   88.41 -
   88.42 -lemma set_rsp [quot_respect]:
   88.43 -  "(op \<approx> ===> op =) set set"
   88.44 -  by (auto intro!: fun_relI)
   88.45 -
   88.46 -lemma inter_list_rsp [quot_respect]:
   88.47 -  shows "(op \<approx> ===> op \<approx> ===> op \<approx>) inter_list inter_list"
   88.48 -  by (auto intro!: fun_relI)
   88.49 -
   88.50 -lemma removeAll_rsp [quot_respect]:
   88.51 -  shows "(op = ===> op \<approx> ===> op \<approx>) removeAll removeAll"
   88.52 -  by (auto intro!: fun_relI)
   88.53 -
   88.54 -lemma diff_list_rsp [quot_respect]:
   88.55 -  shows "(op \<approx> ===> op \<approx> ===> op \<approx>) diff_list diff_list"
   88.56 -  by (auto intro!: fun_relI)
   88.57 -
   88.58 -lemma card_list_rsp [quot_respect]:
   88.59 -  shows "(op \<approx> ===> op =) card_list card_list"
   88.60 -  by (auto intro!: fun_relI)
   88.61 -
   88.62 -lemma filter_rsp [quot_respect]:
   88.63 -  shows "(op = ===> op \<approx> ===> op \<approx>) filter filter"
   88.64 -  by (auto intro!: fun_relI)
   88.65 -
   88.66 -lemma remdups_removeAll: (*FIXME move*)
   88.67 -  "remdups (removeAll x xs) = remove1 x (remdups xs)"
   88.68 -  by (induct xs) auto
   88.69 -
   88.70 -lemma member_commute_fold_once:
   88.71 -  assumes "rsp_fold f"
   88.72 -    and "x \<in> set xs"
   88.73 -  shows "fold_once f xs = fold_once f (removeAll x xs) \<circ> f x"
   88.74 -proof -
   88.75 -  from assms have "fold f (remdups xs) = fold f (remove1 x (remdups xs)) \<circ> f x"
   88.76 -    by (auto intro!: fold_remove1_split elim: rsp_foldE)
   88.77 -  then show ?thesis using `rsp_fold f` by (simp add: fold_once_fold_remdups remdups_removeAll)
   88.78 -qed
   88.79 -
   88.80 -lemma fold_once_set_equiv:
   88.81 -  assumes "xs \<approx> ys"
   88.82 -  shows "fold_once f xs = fold_once f ys"
   88.83 -proof (cases "rsp_fold f")
   88.84 -  case False then show ?thesis by simp
   88.85 -next
   88.86 -  case True
   88.87 -  then have "\<And>x y. x \<in> set (remdups xs) \<Longrightarrow> y \<in> set (remdups xs) \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
   88.88 -    by (rule rsp_foldE)
   88.89 -  moreover from assms have "multiset_of (remdups xs) = multiset_of (remdups ys)"
   88.90 -    by (simp add: set_eq_iff_multiset_of_remdups_eq)
   88.91 -  ultimately have "fold f (remdups xs) = fold f (remdups ys)"
   88.92 -    by (rule fold_multiset_equiv)
   88.93 -  with True show ?thesis by (simp add: fold_once_fold_remdups)
   88.94 -qed
   88.95 -
   88.96 -lemma fold_once_rsp [quot_respect]:
   88.97 -  shows "(op = ===> op \<approx> ===> op =) fold_once fold_once"
   88.98 -  unfolding fun_rel_def by (auto intro: fold_once_set_equiv) 
   88.99 -
  88.100 -lemma concat_rsp_pre:
  88.101 -  assumes a: "list_all2 op \<approx> x x'"
  88.102 -  and     b: "x' \<approx> y'"
  88.103 -  and     c: "list_all2 op \<approx> y' y"
  88.104 -  and     d: "\<exists>x\<in>set x. xa \<in> set x"
  88.105 -  shows "\<exists>x\<in>set y. xa \<in> set x"
  88.106 -proof -
  88.107 -  obtain xb where e: "xb \<in> set x" and f: "xa \<in> set xb" using d by auto
  88.108 -  have "\<exists>y. y \<in> set x' \<and> xb \<approx> y" by (rule list_all2_find_element[OF e a])
  88.109 -  then obtain ya where h: "ya \<in> set x'" and i: "xb \<approx> ya" by auto
  88.110 -  have "ya \<in> set y'" using b h by simp
  88.111 -  then have "\<exists>yb. yb \<in> set y \<and> ya \<approx> yb" using c by (rule list_all2_find_element)
  88.112 -  then show ?thesis using f i by auto
  88.113 -qed
  88.114 -
  88.115 -lemma concat_rsp [quot_respect]:
  88.116 -  shows "(list_all2 op \<approx> OOO op \<approx> ===> op \<approx>) concat concat"
  88.117 -proof (rule fun_relI, elim pred_compE)
  88.118 -  fix a b ba bb
  88.119 -  assume a: "list_all2 op \<approx> a ba"
  88.120 -  with list_symp [OF list_eq_symp] have a': "list_all2 op \<approx> ba a" by (rule sympE)
  88.121 -  assume b: "ba \<approx> bb"
  88.122 -  with list_eq_symp have b': "bb \<approx> ba" by (rule sympE)
  88.123 -  assume c: "list_all2 op \<approx> bb b"
  88.124 -  with list_symp [OF list_eq_symp] have c': "list_all2 op \<approx> b bb" by (rule sympE)
  88.125 -  have "\<forall>x. (\<exists>xa\<in>set a. x \<in> set xa) = (\<exists>xa\<in>set b. x \<in> set xa)" 
  88.126 -  proof
  88.127 -    fix x
  88.128 -    show "(\<exists>xa\<in>set a. x \<in> set xa) = (\<exists>xa\<in>set b. x \<in> set xa)" 
  88.129 -    proof
  88.130 -      assume d: "\<exists>xa\<in>set a. x \<in> set xa"
  88.131 -      show "\<exists>xa\<in>set b. x \<in> set xa" by (rule concat_rsp_pre[OF a b c d])
  88.132 -    next
  88.133 -      assume e: "\<exists>xa\<in>set b. x \<in> set xa"
  88.134 -      show "\<exists>xa\<in>set a. x \<in> set xa" by (rule concat_rsp_pre[OF c' b' a' e])
  88.135 -    qed
  88.136 -  qed
  88.137 -  then show "concat a \<approx> concat b" by auto
  88.138 -qed
  88.139 -
  88.140 -
  88.141  section {* Quotient definitions for fsets *}
  88.142  
  88.143  
  88.144 @@ -323,7 +189,7 @@
  88.145  
  88.146  quotient_definition
  88.147    "bot :: 'a fset" 
  88.148 -  is "Nil :: 'a list"
  88.149 +  is "Nil :: 'a list" done
  88.150  
  88.151  abbreviation
  88.152    empty_fset  ("{||}")
  88.153 @@ -332,7 +198,7 @@
  88.154  
  88.155  quotient_definition
  88.156    "less_eq_fset :: ('a fset \<Rightarrow> 'a fset \<Rightarrow> bool)"
  88.157 -  is "sub_list :: ('a list \<Rightarrow> 'a list \<Rightarrow> bool)"
  88.158 +  is "sub_list :: ('a list \<Rightarrow> 'a list \<Rightarrow> bool)" by simp
  88.159  
  88.160  abbreviation
  88.161    subset_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subseteq>|" 50)
  88.162 @@ -351,7 +217,7 @@
  88.163  
  88.164  quotient_definition
  88.165    "sup :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
  88.166 -  is "append :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
  88.167 +  is "append :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" by simp
  88.168  
  88.169  abbreviation
  88.170    union_fset (infixl "|\<union>|" 65)
  88.171 @@ -360,7 +226,7 @@
  88.172  
  88.173  quotient_definition
  88.174    "inf :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
  88.175 -  is "inter_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
  88.176 +  is "inter_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" by simp
  88.177  
  88.178  abbreviation
  88.179    inter_fset (infixl "|\<inter>|" 65)
  88.180 @@ -369,7 +235,7 @@
  88.181  
  88.182  quotient_definition
  88.183    "minus :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
  88.184 -  is "diff_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
  88.185 +  is "diff_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" by fastforce
  88.186  
  88.187  instance
  88.188  proof
  88.189 @@ -413,7 +279,7 @@
  88.190  
  88.191  quotient_definition
  88.192    "insert_fset :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
  88.193 -  is "Cons"
  88.194 +  is "Cons" by auto
  88.195  
  88.196  syntax
  88.197    "_insert_fset"     :: "args => 'a fset"  ("{|(_)|}")
  88.198 @@ -425,7 +291,7 @@
  88.199  quotient_definition
  88.200    fset_member
  88.201  where
  88.202 -  "fset_member :: 'a fset \<Rightarrow> 'a \<Rightarrow> bool" is "List.member"
  88.203 +  "fset_member :: 'a fset \<Rightarrow> 'a \<Rightarrow> bool" is "List.member" by fastforce
  88.204  
  88.205  abbreviation
  88.206    in_fset :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<in>|" 50)
  88.207 @@ -442,31 +308,84 @@
  88.208  
  88.209  quotient_definition
  88.210    "card_fset :: 'a fset \<Rightarrow> nat"
  88.211 -  is card_list
  88.212 +  is card_list by simp
  88.213  
  88.214  quotient_definition
  88.215    "map_fset :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset"
  88.216 -  is map
  88.217 +  is map by simp
  88.218  
  88.219  quotient_definition
  88.220    "remove_fset :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
  88.221 -  is removeAll
  88.222 +  is removeAll by simp
  88.223  
  88.224  quotient_definition
  88.225    "fset :: 'a fset \<Rightarrow> 'a set"
  88.226 -  is "set"
  88.227 +  is "set" by simp
  88.228 +
  88.229 +lemma fold_once_set_equiv:
  88.230 +  assumes "xs \<approx> ys"
  88.231 +  shows "fold_once f xs = fold_once f ys"
  88.232 +proof (cases "rsp_fold f")
  88.233 +  case False then show ?thesis by simp
  88.234 +next
  88.235 +  case True
  88.236 +  then have "\<And>x y. x \<in> set (remdups xs) \<Longrightarrow> y \<in> set (remdups xs) \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
  88.237 +    by (rule rsp_foldE)
  88.238 +  moreover from assms have "multiset_of (remdups xs) = multiset_of (remdups ys)"
  88.239 +    by (simp add: set_eq_iff_multiset_of_remdups_eq)
  88.240 +  ultimately have "fold f (remdups xs) = fold f (remdups ys)"
  88.241 +    by (rule fold_multiset_equiv)
  88.242 +  with True show ?thesis by (simp add: fold_once_fold_remdups)
  88.243 +qed
  88.244  
  88.245  quotient_definition
  88.246    "fold_fset :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b \<Rightarrow> 'b"
  88.247 -  is fold_once
  88.248 +  is fold_once by (rule fold_once_set_equiv)
  88.249 +
  88.250 +lemma concat_rsp_pre:
  88.251 +  assumes a: "list_all2 op \<approx> x x'"
  88.252 +  and     b: "x' \<approx> y'"
  88.253 +  and     c: "list_all2 op \<approx> y' y"
  88.254 +  and     d: "\<exists>x\<in>set x. xa \<in> set x"
  88.255 +  shows "\<exists>x\<in>set y. xa \<in> set x"
  88.256 +proof -
  88.257 +  obtain xb where e: "xb \<in> set x" and f: "xa \<in> set xb" using d by auto
  88.258 +  have "\<exists>y. y \<in> set x' \<and> xb \<approx> y" by (rule list_all2_find_element[OF e a])
  88.259 +  then obtain ya where h: "ya \<in> set x'" and i: "xb \<approx> ya" by auto
  88.260 +  have "ya \<in> set y'" using b h by simp
  88.261 +  then have "\<exists>yb. yb \<in> set y \<and> ya \<approx> yb" using c by (rule list_all2_find_element)
  88.262 +  then show ?thesis using f i by auto
  88.263 +qed
  88.264  
  88.265  quotient_definition
  88.266    "concat_fset :: ('a fset) fset \<Rightarrow> 'a fset"
  88.267 -  is concat
  88.268 +  is concat 
  88.269 +proof (elim pred_compE)
  88.270 +fix a b ba bb
  88.271 +  assume a: "list_all2 op \<approx> a ba"
  88.272 +  with list_symp [OF list_eq_symp] have a': "list_all2 op \<approx> ba a" by (rule sympE)
  88.273 +  assume b: "ba \<approx> bb"
  88.274 +  with list_eq_symp have b': "bb \<approx> ba" by (rule sympE)
  88.275 +  assume c: "list_all2 op \<approx> bb b"
  88.276 +  with list_symp [OF list_eq_symp] have c': "list_all2 op \<approx> b bb" by (rule sympE)
  88.277 +  have "\<forall>x. (\<exists>xa\<in>set a. x \<in> set xa) = (\<exists>xa\<in>set b. x \<in> set xa)" 
  88.278 +  proof
  88.279 +    fix x
  88.280 +    show "(\<exists>xa\<in>set a. x \<in> set xa) = (\<exists>xa\<in>set b. x \<in> set xa)" 
  88.281 +    proof
  88.282 +      assume d: "\<exists>xa\<in>set a. x \<in> set xa"
  88.283 +      show "\<exists>xa\<in>set b. x \<in> set xa" by (rule concat_rsp_pre[OF a b c d])
  88.284 +    next
  88.285 +      assume e: "\<exists>xa\<in>set b. x \<in> set xa"
  88.286 +      show "\<exists>xa\<in>set a. x \<in> set xa" by (rule concat_rsp_pre[OF c' b' a' e])
  88.287 +    qed
  88.288 +  qed
  88.289 +  then show "concat a \<approx> concat b" by auto
  88.290 +qed
  88.291  
  88.292  quotient_definition
  88.293    "filter_fset :: ('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
  88.294 -  is filter
  88.295 +  is filter by force
  88.296  
  88.297  
  88.298  subsection {* Compositional respectfulness and preservation lemmas *}
  88.299 @@ -538,7 +457,7 @@
  88.300  
  88.301  lemma append_rsp2 [quot_respect]:
  88.302    "(list_all2 op \<approx> OOO op \<approx> ===> list_all2 op \<approx> OOO op \<approx> ===> list_all2 op \<approx> OOO op \<approx>) append append"
  88.303 -  by (intro compositional_rsp3 append_rsp)
  88.304 +  by (intro compositional_rsp3)
  88.305       (auto intro!: fun_relI simp add: append_rsp2_pre)
  88.306  
  88.307  lemma map_rsp2 [quot_respect]:
  88.308 @@ -967,6 +886,20 @@
  88.309    (if rsp_fold f then if a |\<in>| A then fold_fset f A else fold_fset f A \<circ> f a else id)"
  88.310    by descending (simp add: fold_once_fold_remdups)
  88.311  
  88.312 +lemma remdups_removeAll:
  88.313 +  "remdups (removeAll x xs) = remove1 x (remdups xs)"
  88.314 +  by (induct xs) auto
  88.315 +
  88.316 +lemma member_commute_fold_once:
  88.317 +  assumes "rsp_fold f"
  88.318 +    and "x \<in> set xs"
  88.319 +  shows "fold_once f xs = fold_once f (removeAll x xs) \<circ> f x"
  88.320 +proof -
  88.321 +  from assms have "fold f (remdups xs) = fold f (remove1 x (remdups xs)) \<circ> f x"
  88.322 +    by (auto intro!: fold_remove1_split elim: rsp_foldE)
  88.323 +  then show ?thesis using `rsp_fold f` by (simp add: fold_once_fold_remdups remdups_removeAll)
  88.324 +qed
  88.325 +
  88.326  lemma in_commute_fold_fset:
  88.327    "rsp_fold f \<Longrightarrow> h |\<in>| b \<Longrightarrow> fold_fset f b = fold_fset f (remove_fset h b) \<circ> f h"
  88.328    by descending (simp add: member_commute_fold_once)
  88.329 @@ -1170,7 +1103,7 @@
  88.330        then have e': "List.member r a" using list_eq_def [simplified List.member_def [symmetric], of l r] b 
  88.331          by auto
  88.332        have f: "card_list (removeAll a l) = m" using e d by (simp)
  88.333 -      have g: "removeAll a l \<approx> removeAll a r" using removeAll_rsp b by simp
  88.334 +      have g: "removeAll a l \<approx> removeAll a r" using remove_fset_rsp b by simp
  88.335        have "(removeAll a l) \<approx>2 (removeAll a r)" by (rule Suc.hyps[OF f g])
  88.336        then have h: "(a # removeAll a l) \<approx>2 (a # removeAll a r)" by (rule list_eq2.intros(5))
  88.337        have i: "l \<approx>2 (a # removeAll a l)"
    89.1 --- a/src/HOL/Quotient_Examples/Lift_Fun.thy	Fri Mar 23 20:32:43 2012 +0100
    89.2 +++ b/src/HOL/Quotient_Examples/Lift_Fun.thy	Mon Mar 26 10:56:56 2012 +0200
    89.3 @@ -6,7 +6,7 @@
    89.4  
    89.5  
    89.6  theory Lift_Fun
    89.7 -imports Main
    89.8 +imports Main "~~/src/HOL/Library/Quotient_Syntax"
    89.9  begin
   89.10  
   89.11  text {* This file is meant as a test case for features introduced in the changeset 2d8949268303. 
   89.12 @@ -23,17 +23,17 @@
   89.13    by (simp add: identity_equivp)
   89.14  
   89.15  quotient_definition "comp' :: ('b \<rightarrow> 'c) \<rightarrow> ('a \<rightarrow> 'b) \<rightarrow> 'a \<rightarrow> 'c"  is
   89.16 -  "comp :: ('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c"
   89.17 +  "comp :: ('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c" done
   89.18  
   89.19  quotient_definition "fcomp' :: ('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c" is 
   89.20 -  fcomp
   89.21 +  fcomp done
   89.22  
   89.23  quotient_definition "map_fun' :: ('c \<rightarrow> 'a) \<rightarrow> ('b \<rightarrow> 'd) \<rightarrow> ('a \<rightarrow> 'b) \<rightarrow> 'c \<rightarrow> 'd" 
   89.24 -  is "map_fun::('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'c \<Rightarrow> 'd"
   89.25 +  is "map_fun::('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'c \<Rightarrow> 'd" done
   89.26  
   89.27 -quotient_definition "inj_on' :: ('a \<rightarrow> 'b) \<rightarrow> 'a set \<rightarrow> bool" is inj_on
   89.28 +quotient_definition "inj_on' :: ('a \<rightarrow> 'b) \<rightarrow> 'a set \<rightarrow> bool" is inj_on done
   89.29  
   89.30 -quotient_definition "bij_betw' :: ('a \<rightarrow> 'b) \<rightarrow> 'a set \<rightarrow> 'b set \<rightarrow> bool" is bij_betw
   89.31 +quotient_definition "bij_betw' :: ('a \<rightarrow> 'b) \<rightarrow> 'a set \<rightarrow> 'b set \<rightarrow> bool" is bij_betw done
   89.32  
   89.33  
   89.34  subsection {* Co/Contravariant type variables *} 
   89.35 @@ -47,7 +47,7 @@
   89.36    where "map_endofun' f g e = map_fun g f e"
   89.37  
   89.38  quotient_definition "map_endofun :: ('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a endofun \<Rightarrow> 'b endofun" is
   89.39 -  map_endofun'
   89.40 +  map_endofun' done
   89.41  
   89.42  text {* Registration of the map function for 'a endofun. *}
   89.43  
   89.44 @@ -63,7 +63,45 @@
   89.45      by (auto simp: map_endofun_def map_endofun'_def map_fun_def fun_eq_iff) (simp add: a o_assoc) 
   89.46  qed
   89.47  
   89.48 -quotient_definition "endofun_id_id :: ('a endofun) endofun" is "id :: ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)"
   89.49 +text {* Relator for 'a endofun. *}
   89.50 +
   89.51 +definition
   89.52 +  endofun_rel' :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> bool" 
   89.53 +where
   89.54 +  "endofun_rel' R = (\<lambda>f g. (R ===> R) f g)"
   89.55 +
   89.56 +quotient_definition "endofun_rel :: ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a endofun \<Rightarrow> 'b endofun \<Rightarrow> bool" is
   89.57 +  endofun_rel' done
   89.58 +
   89.59 +lemma endofun_quotient:
   89.60 +assumes a: "Quotient R Abs Rep"
   89.61 +shows "Quotient (endofun_rel R) (map_endofun Abs Rep) (map_endofun Rep Abs)"
   89.62 +proof (intro QuotientI)
   89.63 +  show "\<And>a. map_endofun Abs Rep (map_endofun Rep Abs a) = a"
   89.64 +    by (metis (hide_lams, no_types) a abs_o_rep id_apply map_endofun.comp map_endofun.id o_eq_dest_lhs)
   89.65 +next
   89.66 +  show "\<And>a. endofun_rel R (map_endofun Rep Abs a) (map_endofun Rep Abs a)"
   89.67 +  using fun_quotient[OF a a, THEN Quotient_rep_reflp]
   89.68 +  unfolding endofun_rel_def map_endofun_def map_fun_def o_def map_endofun'_def endofun_rel'_def id_def 
   89.69 +    by (metis (mono_tags) Quotient_endofun rep_abs_rsp)
   89.70 +next
   89.71 +  show "\<And>r s. endofun_rel R r s =
   89.72 +          (endofun_rel R r r \<and>
   89.73 +           endofun_rel R s s \<and> map_endofun Abs Rep r = map_endofun Abs Rep s)"
   89.74 +    apply(auto simp add: endofun_rel_def endofun_rel'_def map_endofun_def map_endofun'_def)
   89.75 +    using fun_quotient[OF a a,THEN Quotient_refl1]
   89.76 +    apply metis
   89.77 +    using fun_quotient[OF a a,THEN Quotient_refl2]
   89.78 +    apply metis
   89.79 +    using fun_quotient[OF a a, THEN Quotient_rel]
   89.80 +    apply metis
   89.81 +    using fun_quotient[OF a a, THEN Quotient_rel]
   89.82 +    by (smt Quotient_endofun rep_abs_rsp)
   89.83 +qed
   89.84 +
   89.85 +declare [[map endofun = (endofun_rel, endofun_quotient)]]
   89.86 +
   89.87 +quotient_definition "endofun_id_id :: ('a endofun) endofun" is "id :: ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)" done
   89.88  
   89.89  term  endofun_id_id
   89.90  thm  endofun_id_id_def
   89.91 @@ -73,7 +111,7 @@
   89.92  text {* We have to map "'a endofun" to "('a endofun') endofun", i.e., mapping (lifting)
   89.93    over a type variable which is a covariant and contravariant type variable. *}
   89.94  
   89.95 -quotient_definition "endofun'_id_id :: ('a endofun') endofun'" is endofun_id_id
   89.96 +quotient_definition "endofun'_id_id :: ('a endofun') endofun'" is endofun_id_id done
   89.97  
   89.98  term  endofun'_id_id
   89.99  thm  endofun'_id_id_def
    90.1 --- a/src/HOL/Quotient_Examples/Lift_RBT.thy	Fri Mar 23 20:32:43 2012 +0100
    90.2 +++ b/src/HOL/Quotient_Examples/Lift_RBT.thy	Mon Mar 26 10:56:56 2012 +0200
    90.3 @@ -15,21 +15,6 @@
    90.4    then show ?thesis ..
    90.5  qed
    90.6  
    90.7 -local_setup {* fn lthy =>
    90.8 -let
    90.9 -  val quotients = {qtyp = @{typ "('a, 'b) rbt"}, rtyp = @{typ "('a, 'b) RBT_Impl.rbt"},
   90.10 -    equiv_rel = @{term "dummy"}, equiv_thm = @{thm refl}}
   90.11 -  val qty_full_name = @{type_name "rbt"}
   90.12 -
   90.13 -  fun qinfo phi = Quotient_Info.transform_quotients phi quotients
   90.14 -  in lthy
   90.15 -    |> Local_Theory.declaration {syntax = false, pervasive = true}
   90.16 -        (fn phi => Quotient_Info.update_quotients qty_full_name (qinfo phi)
   90.17 -       #> Quotient_Info.update_abs_rep qty_full_name (Quotient_Info.transform_abs_rep phi
   90.18 -         {abs = @{term "RBT"}, rep = @{term "impl_of"}}))
   90.19 -  end
   90.20 -*}
   90.21 -
   90.22  lemma rbt_eq_iff:
   90.23    "t1 = t2 \<longleftrightarrow> impl_of t1 = impl_of t2"
   90.24    by (simp add: impl_of_inject)
   90.25 @@ -46,12 +31,12 @@
   90.26    "RBT (impl_of t) = t"
   90.27    by (simp add: impl_of_inverse)
   90.28  
   90.29 -
   90.30  subsection {* Primitive operations *}
   90.31  
   90.32 +setup_lifting type_definition_rbt
   90.33 +
   90.34  quotient_definition lookup where "lookup :: ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" is "RBT_Impl.lookup"
   90.35 -
   90.36 -declare lookup_def[unfolded map_fun_def comp_def id_def, code]
   90.37 +by simp
   90.38  
   90.39  (* FIXME: quotient_definition at the moment requires that types variables match exactly,
   90.40  i.e., sort constraints must be annotated to the constant being lifted.
   90.41 @@ -67,65 +52,38 @@
   90.42  *)
   90.43  
   90.44  quotient_definition empty where "empty :: ('a\<Colon>linorder, 'b) rbt"
   90.45 -is "(RBT_Impl.Empty :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt)"
   90.46 -
   90.47 -lemma impl_of_empty [code abstract]:
   90.48 -  "impl_of empty = RBT_Impl.Empty"
   90.49 -  by (simp add: empty_def RBT_inverse)
   90.50 +is "(RBT_Impl.Empty :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt)" by (simp add: empty_def)
   90.51  
   90.52  quotient_definition insert where "insert :: 'a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
   90.53 -is "RBT_Impl.insert"
   90.54 -
   90.55 -lemma impl_of_insert [code abstract]:
   90.56 -  "impl_of (insert k v t) = RBT_Impl.insert k v (impl_of t)"
   90.57 -  by (simp add: insert_def RBT_inverse)
   90.58 +is "RBT_Impl.insert" by simp
   90.59  
   90.60  quotient_definition delete where "delete :: 'a\<Colon>linorder \<Rightarrow> ('a, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
   90.61 -is "RBT_Impl.delete"
   90.62 -
   90.63 -lemma impl_of_delete [code abstract]:
   90.64 -  "impl_of (delete k t) = RBT_Impl.delete k (impl_of t)"
   90.65 -  by (simp add: delete_def RBT_inverse)
   90.66 +is "RBT_Impl.delete" by simp
   90.67  
   90.68  (* FIXME: unnecessary type annotations *)
   90.69  quotient_definition "entries :: ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) list"
   90.70 -is "RBT_Impl.entries :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt \<Rightarrow> ('a \<times> 'b) list"
   90.71 -
   90.72 -lemma [code]: "entries t = RBT_Impl.entries (impl_of t)"
   90.73 -unfolding entries_def map_fun_def comp_def id_def ..
   90.74 +is "RBT_Impl.entries :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt \<Rightarrow> ('a \<times> 'b) list" by simp
   90.75  
   90.76  (* FIXME: unnecessary type annotations *)
   90.77  quotient_definition "keys :: ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a list"
   90.78 -is "RBT_Impl.keys :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt \<Rightarrow> 'a list"
   90.79 +is "RBT_Impl.keys :: ('a\<Colon>linorder, 'b) RBT_Impl.rbt \<Rightarrow> 'a list" by simp
   90.80  
   90.81  quotient_definition "bulkload :: ('a\<Colon>linorder \<times> 'b) list \<Rightarrow> ('a, 'b) rbt"
   90.82 -is "RBT_Impl.bulkload"
   90.83 -
   90.84 -lemma impl_of_bulkload [code abstract]:
   90.85 -  "impl_of (bulkload xs) = RBT_Impl.bulkload xs"
   90.86 -  by (simp add: bulkload_def RBT_inverse)
   90.87 +is "RBT_Impl.bulkload" by simp
   90.88  
   90.89  quotient_definition "map_entry :: 'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
   90.90 -is "RBT_Impl.map_entry"
   90.91 -
   90.92 -lemma impl_of_map_entry [code abstract]:
   90.93 -  "impl_of (map_entry k f t) = RBT_Impl.map_entry k f (impl_of t)"
   90.94 -  by (simp add: map_entry_def RBT_inverse)
   90.95 +is "RBT_Impl.map_entry" by simp
   90.96  
   90.97  (* FIXME: unnecesary type annotations *)
   90.98  quotient_definition map where "map :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) rbt"
   90.99  is "RBT_Impl.map :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder, 'b) RBT_Impl.rbt \<Rightarrow> ('a, 'b) RBT_Impl.rbt"
  90.100 -
  90.101 -lemma impl_of_map [code abstract]:
  90.102 -  "impl_of (map f t) = RBT_Impl.map f (impl_of t)"
  90.103 -  by (simp add: map_def RBT_inverse)
  90.104 +by simp
  90.105  
  90.106  (* FIXME: unnecessary type annotations *)
  90.107 -quotient_definition fold where "fold :: ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" is "RBT_Impl.fold :: ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) RBT_Impl.rbt \<Rightarrow> 'c \<Rightarrow> 'c"
  90.108 +quotient_definition fold where "fold :: ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" 
  90.109 +is "RBT_Impl.fold :: ('a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a\<Colon>linorder, 'b) RBT_Impl.rbt \<Rightarrow> 'c \<Rightarrow> 'c" by simp
  90.110  
  90.111 -lemma [code]: "fold f t = RBT_Impl.fold f (impl_of t)"
  90.112 -unfolding fold_def map_fun_def comp_def id_def ..
  90.113 -
  90.114 +export_code lookup empty insert delete entries keys bulkload map_entry map fold in SML
  90.115  
  90.116  subsection {* Derived operations *}
  90.117  
  90.118 @@ -189,6 +147,10 @@
  90.119    "fold f t = List.fold (prod_case f) (entries t)"
  90.120    by (simp add: fold_def fun_eq_iff RBT_Impl.fold_def entries_impl_of)
  90.121  
  90.122 +lemma impl_of_empty:
  90.123 +  "impl_of empty = RBT_Impl.Empty"
  90.124 +  by (simp add: empty_def RBT_inverse)
  90.125 +
  90.126  lemma is_empty_empty [simp]:
  90.127    "is_empty t \<longleftrightarrow> t = empty"
  90.128    by (simp add: rbt_eq_iff is_empty_def impl_of_empty split: rbt.split)
  90.129 @@ -210,5 +172,4 @@
  90.130    by (simp add: keys_def RBT_Impl.keys_def distinct_entries)
  90.131  
  90.132  
  90.133 -
  90.134  end
  90.135 \ No newline at end of file
    91.1 --- a/src/HOL/Quotient_Examples/Lift_Set.thy	Fri Mar 23 20:32:43 2012 +0100
    91.2 +++ b/src/HOL/Quotient_Examples/Lift_Set.thy	Mon Mar 26 10:56:56 2012 +0200
    91.3 @@ -14,30 +14,12 @@
    91.4    morphisms member Set
    91.5    unfolding set_def by auto
    91.6  
    91.7 -text {* Here is some ML setup that should eventually be incorporated in the typedef command. *}
    91.8 -
    91.9 -local_setup {* fn lthy =>
   91.10 -  let
   91.11 -    val quotients =
   91.12 -      {qtyp = @{typ "'a set"}, rtyp = @{typ "'a => bool"},
   91.13 -        equiv_rel = @{term "dummy"}, equiv_thm = @{thm refl}}
   91.14 -    val qty_full_name = @{type_name "set"}
   91.15 -
   91.16 -    fun qinfo phi = Quotient_Info.transform_quotients phi quotients
   91.17 -  in
   91.18 -    lthy
   91.19 -    |> Local_Theory.declaration {syntax = false, pervasive = true}
   91.20 -        (fn phi =>
   91.21 -          Quotient_Info.update_quotients qty_full_name (qinfo phi) #>
   91.22 -          Quotient_Info.update_abs_rep qty_full_name
   91.23 -            (Quotient_Info.transform_abs_rep phi {abs = @{term "Set"}, rep = @{term "member"}}))
   91.24 -  end
   91.25 -*}
   91.26 +setup_lifting type_definition_set[unfolded set_def]
   91.27  
   91.28  text {* Now, we can employ quotient_definition to lift definitions. *}
   91.29  
   91.30  quotient_definition empty where "empty :: 'a set"
   91.31 -is "bot :: 'a \<Rightarrow> bool"
   91.32 +is "bot :: 'a \<Rightarrow> bool" done
   91.33  
   91.34  term "Lift_Set.empty"
   91.35  thm Lift_Set.empty_def
   91.36 @@ -46,10 +28,12 @@
   91.37    "insertp x P y \<longleftrightarrow> y = x \<or> P y"
   91.38  
   91.39  quotient_definition insert where "insert :: 'a => 'a set => 'a set"
   91.40 -is insertp
   91.41 +is insertp done
   91.42  
   91.43  term "Lift_Set.insert"
   91.44  thm Lift_Set.insert_def
   91.45  
   91.46 +export_code empty insert in SML
   91.47 +
   91.48  end
   91.49  
    92.1 --- a/src/HOL/Quotient_Examples/Quotient_Cset.thy	Fri Mar 23 20:32:43 2012 +0100
    92.2 +++ b/src/HOL/Quotient_Examples/Quotient_Cset.thy	Mon Mar 26 10:56:56 2012 +0200
    92.3 @@ -21,75 +21,50 @@
    92.4  
    92.5  subsection {* Operations *}
    92.6  
    92.7 -lemma [quot_respect]:
    92.8 -  "(op = ===> set_eq ===> op =) (op \<in>) (op \<in>)"
    92.9 -  "(op = ===> set_eq) Collect Collect"
   92.10 -  "(set_eq ===> op =) Set.is_empty Set.is_empty"
   92.11 -  "(op = ===> set_eq ===> set_eq) Set.insert Set.insert"
   92.12 -  "(op = ===> set_eq ===> set_eq) Set.remove Set.remove"
   92.13 -  "(op = ===> set_eq ===> set_eq) image image"
   92.14 -  "(op = ===> set_eq ===> set_eq) Set.project Set.project"
   92.15 -  "(set_eq ===> op =) Ball Ball"
   92.16 -  "(set_eq ===> op =) Bex Bex"
   92.17 -  "(set_eq ===> op =) Finite_Set.card Finite_Set.card"
   92.18 -  "(set_eq ===> set_eq ===> op =) (op \<subseteq>) (op \<subseteq>)"
   92.19 -  "(set_eq ===> set_eq ===> op =) (op \<subset>) (op \<subset>)"
   92.20 -  "(set_eq ===> set_eq ===> set_eq) (op \<inter>) (op \<inter>)"
   92.21 -  "(set_eq ===> set_eq ===> set_eq) (op \<union>) (op \<union>)"
   92.22 -  "set_eq {} {}"
   92.23 -  "set_eq UNIV UNIV"
   92.24 -  "(set_eq ===> set_eq) uminus uminus"
   92.25 -  "(set_eq ===> set_eq ===> set_eq) minus minus"
   92.26 -  "(set_eq ===> op =) Inf Inf"
   92.27 -  "(set_eq ===> op =) Sup Sup"
   92.28 -  "(op = ===> set_eq) List.set List.set"
   92.29 -  "(set_eq ===> (op = ===> set_eq) ===> set_eq) UNION UNION"
   92.30 -by (auto simp: fun_rel_eq)
   92.31 -
   92.32  quotient_definition "member :: 'a => 'a Quotient_Cset.set => bool"
   92.33 -is "op \<in>"
   92.34 +is "op \<in>" by simp
   92.35  quotient_definition "Set :: ('a => bool) => 'a Quotient_Cset.set"
   92.36 -is Collect
   92.37 +is Collect done
   92.38  quotient_definition is_empty where "is_empty :: 'a Quotient_Cset.set \<Rightarrow> bool"
   92.39 -is Set.is_empty
   92.40 +is Set.is_empty by simp 
   92.41  quotient_definition insert where "insert :: 'a \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set"
   92.42 -is Set.insert
   92.43 +is Set.insert by simp
   92.44  quotient_definition remove where "remove :: 'a \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set"
   92.45 -is Set.remove
   92.46 +is Set.remove by simp
   92.47  quotient_definition map where "map :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> 'b Quotient_Cset.set"
   92.48 -is image
   92.49 +is image by simp
   92.50  quotient_definition filter where "filter :: ('a \<Rightarrow> bool) \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set"
   92.51 -is Set.project
   92.52 +is Set.project by simp
   92.53  quotient_definition "forall :: 'a Quotient_Cset.set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   92.54 -is Ball
   92.55 +is Ball by simp
   92.56  quotient_definition "exists :: 'a Quotient_Cset.set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   92.57 -is Bex
   92.58 +is Bex by simp
   92.59  quotient_definition card where "card :: 'a Quotient_Cset.set \<Rightarrow> nat"
   92.60 -is Finite_Set.card
   92.61 +is Finite_Set.card by simp
   92.62  quotient_definition set where "set :: 'a list => 'a Quotient_Cset.set"
   92.63 -is List.set
   92.64 +is List.set done
   92.65  quotient_definition subset where "subset :: 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> bool"
   92.66 -is "op \<subseteq> :: 'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   92.67 +is "op \<subseteq> :: 'a set \<Rightarrow> 'a set \<Rightarrow> bool" by simp
   92.68  quotient_definition psubset where "psubset :: 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> bool"
   92.69 -is "op \<subset> :: 'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   92.70 +is "op \<subset> :: 'a set \<Rightarrow> 'a set \<Rightarrow> bool" by simp
   92.71  quotient_definition inter where "inter :: 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set"
   92.72 -is "op \<inter> :: 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"
   92.73 +is "op \<inter> :: 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" by simp
   92.74  quotient_definition union where "union :: 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set"
   92.75 -is "op \<union> :: 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"
   92.76 +is "op \<union> :: 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" by simp
   92.77  quotient_definition empty where "empty :: 'a Quotient_Cset.set"
   92.78 -is "{} :: 'a set"
   92.79 +is "{} :: 'a set" done
   92.80  quotient_definition UNIV where "UNIV :: 'a Quotient_Cset.set"
   92.81 -is "Set.UNIV :: 'a set"
   92.82 +is "Set.UNIV :: 'a set" done
   92.83  quotient_definition uminus where "uminus :: 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set"
   92.84 -is "uminus_class.uminus :: 'a set \<Rightarrow> 'a set"
   92.85 +is "uminus_class.uminus :: 'a set \<Rightarrow> 'a set" by simp
   92.86  quotient_definition minus where "minus :: 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set \<Rightarrow> 'a Quotient_Cset.set"
   92.87 -is "(op -) :: 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"
   92.88 +is "(op -) :: 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" by simp
   92.89  quotient_definition Inf where "Inf :: ('a :: Inf) Quotient_Cset.set \<Rightarrow> 'a"
   92.90 -is "Inf_class.Inf :: ('a :: Inf) set \<Rightarrow> 'a"
   92.91 +is "Inf_class.Inf :: ('a :: Inf) set \<Rightarrow> 'a" by simp
   92.92  quotient_definition Sup where "Sup :: ('a :: Sup) Quotient_Cset.set \<Rightarrow> 'a"
   92.93 -is "Sup_class.Sup :: ('a :: Sup) set \<Rightarrow> 'a"
   92.94 +is "Sup_class.Sup :: ('a :: Sup) set \<Rightarrow> 'a" by simp
   92.95  quotient_definition UNION where "UNION :: 'a Quotient_Cset.set \<Rightarrow> ('a \<Rightarrow> 'b Quotient_Cset.set) \<Rightarrow> 'b Quotient_Cset.set"
   92.96 -is "Complete_Lattices.UNION :: 'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> 'b set"
   92.97 +is "Complete_Lattices.UNION :: 'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> 'b set" by simp
   92.98  
   92.99  hide_const (open) is_empty insert remove map filter forall exists card
  92.100    set subset psubset inter union empty UNIV uminus minus Inf Sup UNION
    93.1 --- a/src/HOL/Quotient_Examples/Quotient_Int.thy	Fri Mar 23 20:32:43 2012 +0100
    93.2 +++ b/src/HOL/Quotient_Examples/Quotient_Int.thy	Mon Mar 26 10:56:56 2012 +0200
    93.3 @@ -22,10 +22,10 @@
    93.4  begin
    93.5  
    93.6  quotient_definition
    93.7 -  "0 \<Colon> int" is "(0\<Colon>nat, 0\<Colon>nat)"
    93.8 +  "0 \<Colon> int" is "(0\<Colon>nat, 0\<Colon>nat)" done
    93.9  
   93.10  quotient_definition
   93.11 -  "1 \<Colon> int" is "(1\<Colon>nat, 0\<Colon>nat)"
   93.12 +  "1 \<Colon> int" is "(1\<Colon>nat, 0\<Colon>nat)" done
   93.13  
   93.14  fun
   93.15    plus_int_raw :: "(nat \<times> nat) \<Rightarrow> (nat \<times> nat) \<Rightarrow> (nat \<times> nat)"
   93.16 @@ -33,7 +33,7 @@
   93.17    "plus_int_raw (x, y) (u, v) = (x + u, y + v)"
   93.18  
   93.19  quotient_definition
   93.20 -  "(op +) \<Colon> (int \<Rightarrow> int \<Rightarrow> int)" is "plus_int_raw"
   93.21 +  "(op +) \<Colon> (int \<Rightarrow> int \<Rightarrow> int)" is "plus_int_raw" by auto
   93.22  
   93.23  fun
   93.24    uminus_int_raw :: "(nat \<times> nat) \<Rightarrow> (nat \<times> nat)"
   93.25 @@ -41,7 +41,7 @@
   93.26    "uminus_int_raw (x, y) = (y, x)"
   93.27  
   93.28  quotient_definition
   93.29 -  "(uminus \<Colon> (int \<Rightarrow> int))" is "uminus_int_raw"
   93.30 +  "(uminus \<Colon> (int \<Rightarrow> int))" is "uminus_int_raw" by auto
   93.31  
   93.32  definition
   93.33    minus_int_def:  "z - w = z + (-w\<Colon>int)"
   93.34 @@ -51,8 +51,38 @@
   93.35  where
   93.36    "times_int_raw (x, y) (u, v) = (x*u + y*v, x*v + y*u)"
   93.37  
   93.38 +lemma times_int_raw_fst:
   93.39 +  assumes a: "x \<approx> z"
   93.40 +  shows "times_int_raw x y \<approx> times_int_raw z y"
   93.41 +  using a
   93.42 +  apply(cases x, cases y, cases z)
   93.43 +  apply(auto simp add: times_int_raw.simps intrel.simps)
   93.44 +  apply(rename_tac u v w x y z)
   93.45 +  apply(subgoal_tac "u*w + z*w = y*w + v*w  &  u*x + z*x = y*x + v*x")
   93.46 +  apply(simp add: mult_ac)
   93.47 +  apply(simp add: add_mult_distrib [symmetric])
   93.48 +done
   93.49 +
   93.50 +lemma times_int_raw_snd:
   93.51 +  assumes a: "x \<approx> z"
   93.52 +  shows "times_int_raw y x \<approx> times_int_raw y z"
   93.53 +  using a
   93.54 +  apply(cases x, cases y, cases z)
   93.55 +  apply(auto simp add: times_int_raw.simps intrel.simps)
   93.56 +  apply(rename_tac u v w x y z)
   93.57 +  apply(subgoal_tac "u*w + z*w = y*w + v*w  &  u*x + z*x = y*x + v*x")
   93.58 +  apply(simp add: mult_ac)
   93.59 +  apply(simp add: add_mult_distrib [symmetric])
   93.60 +done
   93.61 +
   93.62  quotient_definition
   93.63    "(op *) :: (int \<Rightarrow> int \<Rightarrow> int)" is "times_int_raw"
   93.64 +  apply(rule equivp_transp[OF int_equivp])
   93.65 +  apply(rule times_int_raw_fst)
   93.66 +  apply(assumption)
   93.67 +  apply(rule times_int_raw_snd)
   93.68 +  apply(assumption)
   93.69 +done
   93.70  
   93.71  fun
   93.72    le_int_raw :: "(nat \<times> nat) \<Rightarrow> (nat \<times> nat) \<Rightarrow> bool"
   93.73 @@ -60,7 +90,7 @@
   93.74    "le_int_raw (x, y) (u, v) = (x+v \<le> u+y)"
   93.75  
   93.76  quotient_definition
   93.77 -  le_int_def: "(op \<le>) :: int \<Rightarrow> int \<Rightarrow> bool" is "le_int_raw"
   93.78 +  le_int_def: "(op \<le>) :: int \<Rightarrow> int \<Rightarrow> bool" is "le_int_raw" by auto
   93.79  
   93.80  definition
   93.81    less_int_def: "(z\<Colon>int) < w = (z \<le> w \<and> z \<noteq> w)"
   93.82 @@ -75,47 +105,6 @@
   93.83  
   93.84  end
   93.85  
   93.86 -lemma [quot_respect]:
   93.87 -  shows "(op \<approx> ===> op \<approx> ===> op \<approx>) plus_int_raw plus_int_raw"
   93.88 -  and   "(op \<approx> ===> op \<approx>) uminus_int_raw uminus_int_raw"
   93.89 -  and   "(op \<approx> ===> op \<approx> ===> op =) le_int_raw le_int_raw"
   93.90 -  by (auto intro!: fun_relI)
   93.91 -
   93.92 -lemma times_int_raw_fst:
   93.93 -  assumes a: "x \<approx> z"
   93.94 -  shows "times_int_raw x y \<approx> times_int_raw z y"
   93.95 -  using a
   93.96 -  apply(cases x, cases y, cases z)
   93.97 -  apply(auto simp add: times_int_raw.simps intrel.simps)
   93.98 -  apply(rename_tac u v w x y z)
   93.99 -  apply(subgoal_tac "u*w + z*w = y*w + v*w  &  u*x + z*x = y*x + v*x")
  93.100 -  apply(simp add: mult_ac)
  93.101 -  apply(simp add: add_mult_distrib [symmetric])
  93.102 -  done
  93.103 -
  93.104 -lemma times_int_raw_snd:
  93.105 -  assumes a: "x \<approx> z"
  93.106 -  shows "times_int_raw y x \<approx> times_int_raw y z"
  93.107 -  using a
  93.108 -  apply(cases x, cases y, cases z)
  93.109 -  apply(auto simp add: times_int_raw.simps intrel.simps)
  93.110 -  apply(rename_tac u v w x y z)
  93.111 -  apply(subgoal_tac "u*w + z*w = y*w + v*w  &  u*x + z*x = y*x + v*x")
  93.112 -  apply(simp add: mult_ac)
  93.113 -  apply(simp add: add_mult_distrib [symmetric])
  93.114 -  done
  93.115 -
  93.116 -lemma [quot_respect]:
  93.117 -  shows "(op \<approx> ===> op \<approx> ===> op \<approx>) times_int_raw times_int_raw"
  93.118 -  apply(simp only: fun_rel_def)
  93.119 -  apply(rule allI | rule impI)+
  93.120 -  apply(rule equivp_transp[OF int_equivp])
  93.121 -  apply(rule times_int_raw_fst)
  93.122 -  apply(assumption)
  93.123 -  apply(rule times_int_raw_snd)
  93.124 -  apply(assumption)
  93.125 -  done
  93.126 -
  93.127  
  93.128  text{* The integers form a @{text comm_ring_1}*}
  93.129  
  93.130 @@ -165,11 +154,7 @@
  93.131    "int_of_nat_raw m = (m :: nat, 0 :: nat)"
  93.132  
  93.133  quotient_definition
  93.134 -  "int_of_nat :: nat \<Rightarrow> int" is "int_of_nat_raw"
  93.135 -
  93.136 -lemma[quot_respect]:
  93.137 -  shows "(op = ===> op \<approx>) int_of_nat_raw int_of_nat_raw"
  93.138 -  by (auto simp add: equivp_reflp [OF int_equivp])
  93.139 +  "int_of_nat :: nat \<Rightarrow> int" is "int_of_nat_raw" done
  93.140  
  93.141  lemma int_of_nat:
  93.142    shows "of_nat m = int_of_nat m"
  93.143 @@ -304,11 +289,7 @@
  93.144  quotient_definition
  93.145    "int_to_nat::int \<Rightarrow> nat"
  93.146  is
  93.147 -  "int_to_nat_raw"
  93.148 -
  93.149 -lemma [quot_respect]:
  93.150 -  shows "(intrel ===> op =) int_to_nat_raw int_to_nat_raw"
  93.151 -  by (auto iff: int_to_nat_raw_def)
  93.152 +  "int_to_nat_raw" unfolding int_to_nat_raw_def by force
  93.153  
  93.154  lemma nat_le_eq_zle:
  93.155    fixes w z::"int"
    94.1 --- a/src/HOL/Quotient_Examples/Quotient_Message.thy	Fri Mar 23 20:32:43 2012 +0100
    94.2 +++ b/src/HOL/Quotient_Examples/Quotient_Message.thy	Mon Mar 26 10:56:56 2012 +0200
    94.3 @@ -136,29 +136,25 @@
    94.4    "Nonce :: nat \<Rightarrow> msg"
    94.5  is
    94.6    "NONCE"
    94.7 +done
    94.8  
    94.9  quotient_definition
   94.10    "MPair :: msg \<Rightarrow> msg \<Rightarrow> msg"
   94.11  is
   94.12    "MPAIR"
   94.13 +by (rule MPAIR)
   94.14  
   94.15  quotient_definition
   94.16    "Crypt :: nat \<Rightarrow> msg \<Rightarrow> msg"
   94.17  is
   94.18    "CRYPT"
   94.19 +by (rule CRYPT)
   94.20  
   94.21  quotient_definition
   94.22    "Decrypt :: nat \<Rightarrow> msg \<Rightarrow> msg"
   94.23  is
   94.24    "DECRYPT"
   94.25 -
   94.26 -lemma [quot_respect]:
   94.27 -  shows "(op = ===> op \<sim> ===> op \<sim>) CRYPT CRYPT"
   94.28 -by (auto intro: CRYPT)
   94.29 -
   94.30 -lemma [quot_respect]:
   94.31 -  shows "(op = ===> op \<sim> ===> op \<sim>) DECRYPT DECRYPT"
   94.32 -by (auto intro: DECRYPT)
   94.33 +by (rule DECRYPT)
   94.34  
   94.35  text{*Establishing these two equations is the point of the whole exercise*}
   94.36  theorem CD_eq [simp]:
   94.37 @@ -175,25 +171,14 @@
   94.38     "nonces:: msg \<Rightarrow> nat set"
   94.39  is
   94.40    "freenonces"
   94.41 +by (rule msgrel_imp_eq_freenonces)
   94.42  
   94.43  text{*Now prove the four equations for @{term nonces}*}
   94.44  
   94.45 -lemma [quot_respect]:
   94.46 -  shows "(op \<sim> ===> op =) freenonces freenonces"
   94.47 -  by (auto simp add: msgrel_imp_eq_freenonces)
   94.48 -
   94.49 -lemma [quot_respect]:
   94.50 -  shows "(op = ===> op \<sim>) NONCE NONCE"
   94.51 -  by (auto simp add: NONCE)
   94.52 -
   94.53  lemma nonces_Nonce [simp]:
   94.54    shows "nonces (Nonce N) = {N}"
   94.55    by (lifting freenonces.simps(1))
   94.56  
   94.57 -lemma [quot_respect]:
   94.58 -  shows " (op \<sim> ===> op \<sim> ===> op \<sim>) MPAIR MPAIR"
   94.59 -  by (auto simp add: MPAIR)
   94.60 -
   94.61  lemma nonces_MPair [simp]:
   94.62    shows "nonces (MPair X Y) = nonces X \<union> nonces Y"
   94.63    by (lifting freenonces.simps(2))
   94.64 @@ -212,10 +197,7 @@
   94.65    "left:: msg \<Rightarrow> msg"
   94.66  is
   94.67    "freeleft"
   94.68 -
   94.69 -lemma [quot_respect]:
   94.70 -  shows "(op \<sim> ===> op \<sim>) freeleft freeleft"
   94.71 -  by (auto simp add: msgrel_imp_eqv_freeleft)
   94.72 +by (rule msgrel_imp_eqv_freeleft)
   94.73  
   94.74  lemma left_Nonce [simp]:
   94.75    shows "left (Nonce N) = Nonce N"
   94.76 @@ -239,13 +221,10 @@
   94.77    "right:: msg \<Rightarrow> msg"
   94.78  is
   94.79    "freeright"
   94.80 +by (rule msgrel_imp_eqv_freeright)
   94.81  
   94.82  text{*Now prove the four equations for @{term right}*}
   94.83  
   94.84 -lemma [quot_respect]:
   94.85 -  shows "(op \<sim> ===> op \<sim>) freeright freeright"
   94.86 -  by (auto simp add: msgrel_imp_eqv_freeright)
   94.87 -
   94.88  lemma right_Nonce [simp]:
   94.89    shows "right (Nonce N) = Nonce N"
   94.90    by (lifting freeright.simps(1))
   94.91 @@ -352,13 +331,10 @@
   94.92    "discrim:: msg \<Rightarrow> int"
   94.93  is
   94.94    "freediscrim"
   94.95 +by (rule msgrel_imp_eq_freediscrim)
   94.96  
   94.97  text{*Now prove the four equations for @{term discrim}*}
   94.98  
   94.99 -lemma [quot_respect]:
  94.100 -  shows "(op \<sim> ===> op =) freediscrim freediscrim"
  94.101 -  by (auto simp add: msgrel_imp_eq_freediscrim)
  94.102 -
  94.103  lemma discrim_Nonce [simp]:
  94.104    shows "discrim (Nonce N) = 0"
  94.105    by (lifting freediscrim.simps(1))
    95.1 --- a/src/HOL/Quotient_Examples/Quotient_Rat.thy	Fri Mar 23 20:32:43 2012 +0100
    95.2 +++ b/src/HOL/Quotient_Examples/Quotient_Rat.thy	Mon Mar 26 10:56:56 2012 +0200
    95.3 @@ -32,28 +32,29 @@
    95.4  begin
    95.5  
    95.6  quotient_definition
    95.7 -  "0 \<Colon> rat" is "(0\<Colon>int, 1\<Colon>int)"
    95.8 +  "0 \<Colon> rat" is "(0\<Colon>int, 1\<Colon>int)" by simp
    95.9  
   95.10  quotient_definition
   95.11 -  "1 \<Colon> rat" is "(1\<Colon>int, 1\<Colon>int)"
   95.12 +  "1 \<Colon> rat" is "(1\<Colon>int, 1\<Colon>int)" by simp
   95.13  
   95.14  fun times_rat_raw where
   95.15    "times_rat_raw (a :: int, b :: int) (c, d) = (a * c, b * d)"
   95.16  
   95.17  quotient_definition
   95.18 -  "(op *) :: (rat \<Rightarrow> rat \<Rightarrow> rat)" is times_rat_raw
   95.19 +  "(op *) :: (rat \<Rightarrow> rat \<Rightarrow> rat)" is times_rat_raw by (auto simp add: mult_assoc mult_left_commute)
   95.20  
   95.21  fun plus_rat_raw where
   95.22    "plus_rat_raw (a :: int, b :: int) (c, d) = (a * d + c * b, b * d)"
   95.23  
   95.24  quotient_definition
   95.25 -  "(op +) :: (rat \<Rightarrow> rat \<Rightarrow> rat)" is plus_rat_raw
   95.26 +  "(op +) :: (rat \<Rightarrow> rat \<Rightarrow> rat)" is plus_rat_raw 
   95.27 +  by (auto simp add: mult_commute mult_left_commute int_distrib(2))
   95.28  
   95.29  fun uminus_rat_raw where
   95.30    "uminus_rat_raw (a :: int, b :: int) = (-a, b)"
   95.31  
   95.32  quotient_definition
   95.33 -  "(uminus \<Colon> (rat \<Rightarrow> rat))" is "uminus_rat_raw"
   95.34 +  "(uminus \<Colon> (rat \<Rightarrow> rat))" is "uminus_rat_raw" by fastforce
   95.35  
   95.36  definition
   95.37    minus_rat_def: "a - b = a + (-b\<Colon>rat)"
   95.38 @@ -63,6 +64,32 @@
   95.39  
   95.40  quotient_definition
   95.41    "(op \<le>) :: rat \<Rightarrow> rat \<Rightarrow> bool" is "le_rat_raw"
   95.42 +proof -
   95.43 +  {
   95.44 +    fix a b c d e f g h :: int
   95.45 +    assume "a * f * (b * f) \<le> e * b * (b * f)"
   95.46 +    then have le: "a * f * b * f \<le> e * b * b * f" by simp
   95.47 +    assume nz: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0" "h \<noteq> 0"
   95.48 +    then have b2: "b * b > 0"
   95.49 +      by (metis linorder_neqE_linordered_idom mult_eq_0_iff not_square_less_zero)
   95.50 +    have f2: "f * f > 0" using nz(3)
   95.51 +      by (metis linorder_neqE_linordered_idom mult_eq_0_iff not_square_less_zero)
   95.52 +    assume eq: "a * d = c * b" "e * h = g * f"
   95.53 +    have "a * f * b * f * d * d \<le> e * b * b * f * d * d" using le nz(2)
   95.54 +      by (metis linorder_le_cases mult_right_mono mult_right_mono_neg)
   95.55 +    then have "c * f * f * d * (b * b) \<le> e * f * d * d * (b * b)" using eq
   95.56 +      by (metis (no_types) mult_assoc mult_commute)
   95.57 +    then have "c * f * f * d \<le> e * f * d * d" using b2
   95.58 +      by (metis leD linorder_le_less_linear mult_strict_right_mono)
   95.59 +    then have "c * f * f * d * h * h \<le> e * f * d * d * h * h" using nz(4)
   95.60 +      by (metis linorder_le_cases mult_right_mono mult_right_mono_neg)
   95.61 +    then have "c * h * (d * h) * (f * f) \<le> g * d * (d * h) * (f * f)" using eq
   95.62 +      by (metis (no_types) mult_assoc mult_commute)
   95.63 +    then have "c * h * (d * h) \<le> g * d * (d * h)" using f2
   95.64 +      by (metis leD linorder_le_less_linear mult_strict_right_mono)
   95.65 +  }
   95.66 +  then show "\<And>x y xa ya. x \<approx> y \<Longrightarrow> xa \<approx> ya \<Longrightarrow> le_rat_raw x xa = le_rat_raw y ya" by auto
   95.67 +qed
   95.68  
   95.69  definition
   95.70    less_rat_def: "(z\<Colon>rat) < w = (z \<le> w \<and> z \<noteq> w)"
   95.71 @@ -83,14 +110,7 @@
   95.72  where [simp]: "Fract_raw a b = (if b = 0 then (0, 1) else (a, b))"
   95.73  
   95.74  quotient_definition "Fract :: int \<Rightarrow> int \<Rightarrow> rat" is
   95.75 -  Fract_raw
   95.76 -
   95.77 -lemma [quot_respect]:
   95.78 -  "(op \<approx> ===> op \<approx> ===> op \<approx>) times_rat_raw times_rat_raw"
   95.79 -  "(op \<approx> ===> op \<approx> ===> op \<approx>) plus_rat_raw plus_rat_raw"
   95.80 -  "(op \<approx> ===> op \<approx>) uminus_rat_raw uminus_rat_raw"
   95.81 -  "(op = ===> op = ===> op \<approx>) Fract_raw Fract_raw"
   95.82 -  by (auto intro!: fun_relI simp add: mult_assoc mult_commute mult_left_commute int_distrib(2))
   95.83 +  Fract_raw by simp
   95.84  
   95.85  lemmas [simp] = Respects_def
   95.86  
   95.87 @@ -139,32 +159,17 @@
   95.88    apply auto
   95.89    done
   95.90  
   95.91 -instantiation rat :: number_ring
   95.92 -begin
   95.93 -
   95.94 -definition
   95.95 -  rat_number_of_def: "number_of w = Fract w 1"
   95.96 -
   95.97 -instance apply default
   95.98 -  unfolding rat_number_of_def of_int_rat ..
   95.99 -
  95.100 -end
  95.101 -
  95.102  instantiation rat :: field_inverse_zero begin
  95.103  
  95.104  fun rat_inverse_raw where
  95.105    "rat_inverse_raw (a :: int, b :: int) = (if a = 0 then (0, 1) else (b, a))"
  95.106  
  95.107  quotient_definition
  95.108 -  "inverse :: rat \<Rightarrow> rat" is rat_inverse_raw
  95.109 +  "inverse :: rat \<Rightarrow> rat" is rat_inverse_raw by (force simp add: mult_commute)
  95.110  
  95.111  definition
  95.112    divide_rat_def: "q / r = q * inverse (r::rat)"
  95.113  
  95.114 -lemma [quot_respect]:
  95.115 -  "(op \<approx> ===> op \<approx>) rat_inverse_raw rat_inverse_raw"
  95.116 -  by (auto intro!: fun_relI simp add: mult_commute)
  95.117 -
  95.118  instance proof
  95.119    fix q :: rat
  95.120    assume "q \<noteq> 0"
  95.121 @@ -179,34 +184,6 @@
  95.122  
  95.123  end
  95.124  
  95.125 -lemma [quot_respect]: "(op \<approx> ===> op \<approx> ===> op =) le_rat_raw le_rat_raw"
  95.126 -proof -
  95.127 -  {
  95.128 -    fix a b c d e f g h :: int
  95.129 -    assume "a * f * (b * f) \<le> e * b * (b * f)"
  95.130 -    then have le: "a * f * b * f \<le> e * b * b * f" by simp
  95.131 -    assume nz: "b \<noteq> 0" "d \<noteq> 0" "f \<noteq> 0" "h \<noteq> 0"
  95.132 -    then have b2: "b * b > 0"
  95.133 -      by (metis linorder_neqE_linordered_idom mult_eq_0_iff not_square_less_zero)
  95.134 -    have f2: "f * f > 0" using nz(3)
  95.135 -      by (metis linorder_neqE_linordered_idom mult_eq_0_iff not_square_less_zero)
  95.136 -    assume eq: "a * d = c * b" "e * h = g * f"
  95.137 -    have "a * f * b * f * d * d \<le> e * b * b * f * d * d" using le nz(2)
  95.138 -      by (metis linorder_le_cases mult_right_mono mult_right_mono_neg)
  95.139 -    then have "c * f * f * d * (b * b) \<le> e * f * d * d * (b * b)" using eq
  95.140 -      by (metis (no_types) mult_assoc mult_commute)
  95.141 -    then have "c * f * f * d \<le> e * f * d * d" using b2
  95.142 -      by (metis leD linorder_le_less_linear mult_strict_right_mono)
  95.143 -    then have "c * f * f * d * h * h \<le> e * f * d * d * h * h" using nz(4)
  95.144 -      by (metis linorder_le_cases mult_right_mono mult_right_mono_neg)
  95.145 -    then have "c * h * (d * h) * (f * f) \<le> g * d * (d * h) * (f * f)" using eq
  95.146 -      by (metis (no_types) mult_assoc mult_commute)
  95.147 -    then have "c * h * (d * h) \<le> g * d * (d * h)" using f2
  95.148 -      by (metis leD linorder_le_less_linear mult_strict_right_mono)
  95.149 -  }
  95.150 -  then show ?thesis by (auto intro!: fun_relI)
  95.151 -qed
  95.152 -
  95.153  instantiation rat :: linorder
  95.154  begin
  95.155  
    96.1 --- a/src/HOL/RComplete.thy	Fri Mar 23 20:32:43 2012 +0100
    96.2 +++ b/src/HOL/RComplete.thy	Mon Mar 26 10:56:56 2012 +0200
    96.3 @@ -129,26 +129,27 @@
    96.4  
    96.5  subsection{*Floor and Ceiling Functions from the Reals to the Integers*}
    96.6  
    96.7 -lemma number_of_less_real_of_int_iff [simp]:
    96.8 -     "((number_of n) < real (m::int)) = (number_of n < m)"
    96.9 +(* FIXME: theorems for negative numerals *)
   96.10 +lemma numeral_less_real_of_int_iff [simp]:
   96.11 +     "((numeral n) < real (m::int)) = (numeral n < m)"
   96.12  apply auto
   96.13  apply (rule real_of_int_less_iff [THEN iffD1])
   96.14  apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
   96.15  done
   96.16  
   96.17 -lemma number_of_less_real_of_int_iff2 [simp]:
   96.18 -     "(real (m::int) < (number_of n)) = (m < number_of n)"
   96.19 +lemma numeral_less_real_of_int_iff2 [simp]:
   96.20 +     "(real (m::int) < (numeral n)) = (m < numeral n)"
   96.21  apply auto
   96.22  apply (rule real_of_int_less_iff [THEN iffD1])
   96.23  apply (drule_tac [2] real_of_int_less_iff [THEN iffD2], auto)
   96.24  done
   96.25  
   96.26 -lemma number_of_le_real_of_int_iff [simp]:
   96.27 -     "((number_of n) \<le> real (m::int)) = (number_of n \<le> m)"
   96.28 +lemma numeral_le_real_of_int_iff [simp]:
   96.29 +     "((numeral n) \<le> real (m::int)) = (numeral n \<le> m)"
   96.30  by (simp add: linorder_not_less [symmetric])
   96.31  
   96.32 -lemma number_of_le_real_of_int_iff2 [simp]:
   96.33 -     "(real (m::int) \<le> (number_of n)) = (m \<le> number_of n)"
   96.34 +lemma numeral_le_real_of_int_iff2 [simp]:
   96.35 +     "(real (m::int) \<le> (numeral n)) = (m \<le> numeral n)"
   96.36  by (simp add: linorder_not_less [symmetric])
   96.37  
   96.38  lemma floor_real_of_nat [simp]: "floor (real (n::nat)) = int n"
   96.39 @@ -323,7 +324,7 @@
   96.40  lemma zero_le_natfloor [simp]: "0 <= natfloor x"
   96.41    by (unfold natfloor_def, simp)
   96.42  
   96.43 -lemma natfloor_number_of_eq [simp]: "natfloor (number_of n) = number_of n"
   96.44 +lemma natfloor_numeral_eq [simp]: "natfloor (numeral n) = numeral n"
   96.45    by (unfold natfloor_def, simp)
   96.46  
   96.47  lemma natfloor_real_of_nat [simp]: "natfloor(real n) = n"
   96.48 @@ -365,9 +366,9 @@
   96.49    apply (erule le_natfloor)
   96.50  done
   96.51  
   96.52 -lemma le_natfloor_eq_number_of [simp]:
   96.53 -    "~ neg((number_of n)::int) ==> 0 <= x ==>
   96.54 -      (number_of n <= natfloor x) = (number_of n <= x)"
   96.55 +lemma le_natfloor_eq_numeral [simp]:
   96.56 +    "~ neg((numeral n)::int) ==> 0 <= x ==>
   96.57 +      (numeral n <= natfloor x) = (numeral n <= x)"
   96.58    apply (subst le_natfloor_eq, assumption)
   96.59    apply simp
   96.60  done
   96.61 @@ -407,9 +408,9 @@
   96.62    unfolding real_of_int_of_nat_eq [symmetric] floor_add
   96.63    by (simp add: nat_add_distrib)
   96.64  
   96.65 -lemma natfloor_add_number_of [simp]:
   96.66 -    "~neg ((number_of n)::int) ==> 0 <= x ==>
   96.67 -      natfloor (x + number_of n) = natfloor x + number_of n"
   96.68 +lemma natfloor_add_numeral [simp]:
   96.69 +    "~neg ((numeral n)::int) ==> 0 <= x ==>
   96.70 +      natfloor (x + numeral n) = natfloor x + numeral n"
   96.71    by (simp add: natfloor_add [symmetric])
   96.72  
   96.73  lemma natfloor_add_one: "0 <= x ==> natfloor(x + 1) = natfloor x + 1"
   96.74 @@ -453,7 +454,7 @@
   96.75  lemma zero_le_natceiling [simp]: "0 <= natceiling x"
   96.76    by (unfold natceiling_def, simp)
   96.77  
   96.78 -lemma natceiling_number_of_eq [simp]: "natceiling (number_of n) = number_of n"
   96.79 +lemma natceiling_numeral_eq [simp]: "natceiling (numeral n) = numeral n"
   96.80    by (unfold natceiling_def, simp)
   96.81  
   96.82  lemma natceiling_real_of_nat [simp]: "natceiling(real n) = n"
   96.83 @@ -476,9 +477,9 @@
   96.84    unfolding natceiling_def real_of_nat_def
   96.85    by (simp add: nat_le_iff ceiling_le_iff)
   96.86  
   96.87 -lemma natceiling_le_eq_number_of [simp]:
   96.88 -    "~ neg((number_of n)::int) ==>
   96.89 -      (natceiling x <= number_of n) = (x <= number_of n)"
   96.90 +lemma natceiling_le_eq_numeral [simp]:
   96.91 +    "~ neg((numeral n)::int) ==>
   96.92 +      (natceiling x <= numeral n) = (x <= numeral n)"
   96.93    by (simp add: natceiling_le_eq)
   96.94  
   96.95  lemma natceiling_le_eq_one: "(natceiling x <= 1) = (x <= 1)"
   96.96 @@ -495,9 +496,9 @@
   96.97    unfolding real_of_int_of_nat_eq [symmetric] ceiling_add
   96.98    by (simp add: nat_add_distrib)
   96.99  
  96.100 -lemma natceiling_add_number_of [simp]:
  96.101 -    "~ neg ((number_of n)::int) ==> 0 <= x ==>
  96.102 -      natceiling (x + number_of n) = natceiling x + number_of n"
  96.103 +lemma natceiling_add_numeral [simp]:
  96.104 +    "~ neg ((numeral n)::int) ==> 0 <= x ==>
  96.105 +      natceiling (x + numeral n) = natceiling x + numeral n"
  96.106    by (simp add: natceiling_add [symmetric])
  96.107  
  96.108  lemma natceiling_add_one: "0 <= x ==> natceiling(x + 1) = natceiling x + 1"
    97.1 --- a/src/HOL/Rat.thy	Fri Mar 23 20:32:43 2012 +0100
    97.2 +++ b/src/HOL/Rat.thy	Mon Mar 26 10:56:56 2012 +0200
    97.3 @@ -230,35 +230,23 @@
    97.4  lemma Fract_of_int_eq: "Fract k 1 = of_int k"
    97.5    by (rule of_int_rat [symmetric])
    97.6  
    97.7 -instantiation rat :: number_ring
    97.8 -begin
    97.9 -
   97.10 -definition
   97.11 -  rat_number_of_def: "number_of w = Fract w 1"
   97.12 -
   97.13 -instance proof
   97.14 -qed (simp add: rat_number_of_def of_int_rat)
   97.15 -
   97.16 -end
   97.17 -
   97.18  lemma rat_number_collapse:
   97.19    "Fract 0 k = 0"
   97.20    "Fract 1 1 = 1"
   97.21 -  "Fract (number_of k) 1 = number_of k"
   97.22 +  "Fract (numeral w) 1 = numeral w"
   97.23 +  "Fract (neg_numeral w) 1 = neg_numeral w"
   97.24    "Fract k 0 = 0"
   97.25 -  by (cases "k = 0")
   97.26 -    (simp_all add: Zero_rat_def One_rat_def number_of_is_id number_of_eq of_int_rat eq_rat Fract_def)
   97.27 +  using Fract_of_int_eq [of "numeral w"]
   97.28 +  using Fract_of_int_eq [of "neg_numeral w"]
   97.29 +  by (simp_all add: Zero_rat_def One_rat_def eq_rat)
   97.30  
   97.31 -lemma rat_number_expand [code_unfold]:
   97.32 +lemma rat_number_expand:
   97.33    "0 = Fract 0 1"
   97.34    "1 = Fract 1 1"
   97.35 -  "number_of k = Fract (number_of k) 1"
   97.36 +  "numeral k = Fract (numeral k) 1"
   97.37 +  "neg_numeral k = Fract (neg_numeral k) 1"
   97.38    by (simp_all add: rat_number_collapse)
   97.39  
   97.40 -lemma iszero_rat [simp]:
   97.41 -  "iszero (number_of k :: rat) \<longleftrightarrow> iszero (number_of k :: int)"
   97.42 -  by (simp add: iszero_def rat_number_expand number_of_is_id eq_rat)
   97.43 -
   97.44  lemma Rat_cases_nonzero [case_names Fract 0]:
   97.45    assumes Fract: "\<And>a b. q = Fract a b \<Longrightarrow> b > 0 \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> coprime a b \<Longrightarrow> C"
   97.46    assumes 0: "q = 0 \<Longrightarrow> C"
   97.47 @@ -386,7 +374,8 @@
   97.48  lemma quotient_of_number [simp]:
   97.49    "quotient_of 0 = (0, 1)"
   97.50    "quotient_of 1 = (1, 1)"
   97.51 -  "quotient_of (number_of k) = (number_of k, 1)"
   97.52 +  "quotient_of (numeral k) = (numeral k, 1)"
   97.53 +  "quotient_of (neg_numeral k) = (neg_numeral k, 1)"
   97.54    by (simp_all add: rat_number_expand quotient_of_Fract)
   97.55  
   97.56  lemma quotient_of_eq: "quotient_of (Fract a b) = (p, q) \<Longrightarrow> Fract p q = Fract a b"
   97.57 @@ -453,19 +442,12 @@
   97.58  
   97.59  subsubsection {* Various *}
   97.60  
   97.61 +lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
   97.62 +  by (simp add: Fract_of_int_eq [symmetric])
   97.63 +
   97.64  lemma Fract_add_one: "n \<noteq> 0 ==> Fract (m + n) n = Fract m n + 1"
   97.65    by (simp add: rat_number_expand)
   97.66  
   97.67 -lemma Fract_of_int_quotient: "Fract k l = of_int k / of_int l"
   97.68 -  by (simp add: Fract_of_int_eq [symmetric])
   97.69 -
   97.70 -lemma Fract_number_of_quotient:
   97.71 -  "Fract (number_of k) (number_of l) = number_of k / number_of l"
   97.72 -  unfolding Fract_of_int_quotient number_of_is_id number_of_eq ..
   97.73 -
   97.74 -lemma Fract_1_number_of:
   97.75 -  "Fract 1 (number_of k) = 1 / number_of k"
   97.76 -  unfolding Fract_of_int_quotient number_of_eq by simp
   97.77  
   97.78  subsubsection {* The ordered field of rational numbers *}
   97.79  
   97.80 @@ -771,7 +753,8 @@
   97.81      (* not needed because x < (y::int) can be rewritten as x + 1 <= y: of_int_less_iff RS iffD2 *)
   97.82    #> Lin_Arith.add_simps [@{thm neg_less_iff_less},
   97.83        @{thm True_implies_equals},
   97.84 -      read_instantiate @{context} [(("a", 0), "(number_of ?v)")] @{thm right_distrib},
   97.85 +      read_instantiate @{context} [(("a", 0), "(numeral ?v)")] @{thm right_distrib},
   97.86 +      read_instantiate @{context} [(("a", 0), "(neg_numeral ?v)")] @{thm right_distrib},
   97.87        @{thm divide_1}, @{thm divide_zero_left},
   97.88        @{thm times_divide_eq_right}, @{thm times_divide_eq_left},
   97.89        @{thm minus_divide_left} RS sym, @{thm minus_divide_right} RS sym,
   97.90 @@ -895,9 +878,13 @@
   97.91  lemma of_rat_of_int_eq [simp]: "of_rat (of_int z) = of_int z"
   97.92  by (cases z rule: int_diff_cases) (simp add: of_rat_diff)
   97.93  
   97.94 -lemma of_rat_number_of_eq [simp]:
   97.95 -  "of_rat (number_of w) = (number_of w :: 'a::{number_ring,field_char_0})"
   97.96 -by (simp add: number_of_eq)
   97.97 +lemma of_rat_numeral_eq [simp]:
   97.98 +  "of_rat (numeral w) = numeral w"
   97.99 +using of_rat_of_int_eq [of "numeral w"] by simp
  97.100 +
  97.101 +lemma of_rat_neg_numeral_eq [simp]:
  97.102 +  "of_rat (neg_numeral w) = neg_numeral w"
  97.103 +using of_rat_of_int_eq [of "neg_numeral w"] by simp
  97.104  
  97.105  lemmas zero_rat = Zero_rat_def
  97.106  lemmas one_rat = One_rat_def
  97.107 @@ -935,9 +922,11 @@
  97.108  lemma Rats_of_nat [simp]: "of_nat n \<in> Rats"
  97.109  by (subst of_rat_of_nat_eq [symmetric], rule Rats_of_rat)
  97.110  
  97.111 -lemma Rats_number_of [simp]:
  97.112 -  "(number_of w::'a::{number_ring,field_char_0}) \<in> Rats"
  97.113 -by (subst of_rat_number_of_eq [symmetric], rule Rats_of_rat)
  97.114 +lemma Rats_number_of [simp]: "numeral w \<in> Rats"
  97.115 +by (subst of_rat_numeral_eq [symmetric], rule Rats_of_rat)
  97.116 +
  97.117 +lemma Rats_neg_number_of [simp]: "neg_numeral w \<in> Rats"
  97.118 +by (subst of_rat_neg_numeral_eq [symmetric], rule Rats_of_rat)
  97.119  
  97.120  lemma Rats_0 [simp]: "0 \<in> Rats"
  97.121  apply (unfold Rats_def)
  97.122 @@ -1032,6 +1021,8 @@
  97.123  
  97.124  subsection {* Implementation of rational numbers as pairs of integers *}
  97.125  
  97.126 +text {* Formal constructor *}
  97.127 +
  97.128  definition Frct :: "int \<times> int \<Rightarrow> rat" where
  97.129    [simp]: "Frct p = Fract (fst p) (snd p)"
  97.130  
  97.131 @@ -1039,17 +1030,45 @@
  97.132    "Frct (quotient_of q) = q"
  97.133    by (cases q) (auto intro: quotient_of_eq)
  97.134  
  97.135 -lemma Frct_code_post [code_post]:
  97.136 -  "Frct (0, k) = 0"
  97.137 -  "Frct (k, 0) = 0"
  97.138 -  "Frct (1, 1) = 1"
  97.139 -  "Frct (number_of k, 1) = number_of k"
  97.140 -  "Frct (1, number_of k) = 1 / number_of k"
  97.141 -  "Frct (number_of k, number_of l) = number_of k / number_of l"
  97.142 -  by (simp_all add: rat_number_collapse Fract_number_of_quotient Fract_1_number_of)
  97.143 +
  97.144 +text {* Numerals *}
  97.145  
  97.146  declare quotient_of_Fract [code abstract]
  97.147  
  97.148 +definition of_int :: "int \<Rightarrow> rat"
  97.149 +where
  97.150 +  [code_abbrev]: "of_int = Int.of_int"
  97.151 +hide_const (open) of_int
  97.152 +
  97.153 +lemma quotient_of_int [code abstract]:
  97.154 +  "quotient_of (Rat.of_int a) = (a, 1)"
  97.155 +  by (simp add: of_int_def of_int_rat quotient_of_Fract)
  97.156 +
  97.157 +lemma [code_unfold]:
  97.158 +  "numeral k = Rat.of_int (numeral k)"
  97.159 +  by (simp add: Rat.of_int_def)
  97.160 +
  97.161 +lemma [code_unfold]:
  97.162 +  "neg_numeral k = Rat.of_int (neg_numeral k)"
  97.163 +  by (simp add: Rat.of_int_def)
  97.164 +
  97.165 +lemma Frct_code_post [code_post]:
  97.166 +  "Frct (0, a) = 0"
  97.167 +  "Frct (a, 0) = 0"
  97.168 +  "Frct (1, 1) = 1"
  97.169 +  "Frct (numeral k, 1) = numeral k"
  97.170 +  "Frct (neg_numeral k, 1) = neg_numeral k"
  97.171 +  "Frct (1, numeral k) = 1 / numeral k"
  97.172 +  "Frct (1, neg_numeral k) = 1 / neg_numeral k"
  97.173 +  "Frct (numeral k, numeral l) = numeral k / numeral l"
  97.174 +  "Frct (numeral k, neg_numeral l) = numeral k / neg_numeral l"
  97.175 +  "Frct (neg_numeral k, numeral l) = neg_numeral k / numeral l"
  97.176 +  "Frct (neg_numeral k, neg_numeral l) = neg_numeral k / neg_numeral l"
  97.177 +  by (simp_all add: Fract_of_int_quotient)
  97.178 +
  97.179 +
  97.180 +text {* Operations *}
  97.181 +
  97.182  lemma rat_zero_code [code abstract]:
  97.183    "quotient_of 0 = (0, 1)"
  97.184    by (simp add: Zero_rat_def quotient_of_Fract normalize_def)
  97.185 @@ -1132,6 +1151,9 @@
  97.186    "of_rat p = (let (a, b) = quotient_of p in of_int a / of_int b)"
  97.187    by (cases p) (simp add: quotient_of_Fract of_rat_rat)
  97.188  
  97.189 +
  97.190 +text {* Quickcheck *}
  97.191 +
  97.192  definition (in term_syntax)
  97.193    valterm_fract :: "int \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> int \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> rat \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  97.194    [code_unfold]: "valterm_fract k l = Code_Evaluation.valtermify Fract {\<cdot>} k {\<cdot>} l"
  97.195 @@ -1212,7 +1234,6 @@
  97.196      (@{const_name plus_rat_inst.plus_rat}, @{const_name Nitpick.plus_frac}),
  97.197      (@{const_name times_rat_inst.times_rat}, @{const_name Nitpick.times_frac}),
  97.198      (@{const_name uminus_rat_inst.uminus_rat}, @{const_name Nitpick.uminus_frac}),
  97.199 -    (@{const_name number_rat_inst.number_of_rat}, @{const_name Nitpick.number_of_frac}),
  97.200      (@{const_name inverse_rat_inst.inverse_rat}, @{const_name Nitpick.inverse_frac}),
  97.201      (@{const_name ord_rat_inst.less_rat}, @{const_name Nitpick.less_frac}),
  97.202      (@{const_name ord_rat_inst.less_eq_rat}, @{const_name Nitpick.less_eq_frac}),
  97.203 @@ -1220,7 +1241,7 @@
  97.204  *}
  97.205  
  97.206  lemmas [nitpick_unfold] = inverse_rat_inst.inverse_rat
  97.207 -  number_rat_inst.number_of_rat one_rat_inst.one_rat ord_rat_inst.less_rat
  97.208 +  one_rat_inst.one_rat ord_rat_inst.less_rat
  97.209    ord_rat_inst.less_eq_rat plus_rat_inst.plus_rat times_rat_inst.times_rat
  97.210    uminus_rat_inst.uminus_rat zero_rat_inst.zero_rat
  97.211  
    98.1 --- a/src/HOL/RealDef.thy	Fri Mar 23 20:32:43 2012 +0100
    98.2 +++ b/src/HOL/RealDef.thy	Mon Mar 26 10:56:56 2012 +0200
    98.3 @@ -720,7 +720,9 @@
    98.4      unfolding less_eq_real_def less_real_def
    98.5      by (auto, drule (1) positive_add, simp add: positive_zero)
    98.6    show "a \<le> b \<Longrightarrow> c + a \<le> c + b"
    98.7 -    unfolding less_eq_real_def less_real_def by auto
    98.8 +    unfolding less_eq_real_def less_real_def by (auto simp: diff_minus) (* by auto *)
    98.9 +    (* FIXME: Procedure int_combine_numerals: c + b - (c + a) \<equiv> b + - a *)
   98.10 +    (* Should produce c + b - (c + a) \<equiv> b - a *)
   98.11    show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   98.12      by (rule sgn_real_def)
   98.13    show "a \<le> b \<or> b \<le> a"
   98.14 @@ -747,17 +749,6 @@
   98.15  
   98.16  end
   98.17  
   98.18 -instantiation real :: number_ring
   98.19 -begin
   98.20 -
   98.21 -definition
   98.22 -  "(number_of x :: real) = of_int x"
   98.23 -
   98.24 -instance proof
   98.25 -qed (rule number_of_real_def)
   98.26 -
   98.27 -end
   98.28 -
   98.29  lemma of_nat_Real: "of_nat x = Real (\<lambda>n. of_nat x)"
   98.30  apply (induct x)
   98.31  apply (simp add: zero_real_def)
   98.32 @@ -877,7 +868,7 @@
   98.33  by (erule contrapos_pp, simp add: not_less, erule Real_leI [OF Y])
   98.34  
   98.35  lemma of_nat_less_two_power:
   98.36 -  "of_nat n < (2::'a::{linordered_idom,number_ring}) ^ n"
   98.37 +  "of_nat n < (2::'a::linordered_idom) ^ n"
   98.38  apply (induct n)
   98.39  apply simp
   98.40  apply (subgoal_tac "(1::'a) \<le> 2 ^ n")
   98.41 @@ -1469,18 +1460,19 @@
   98.42  subsection{*Numerals and Arithmetic*}
   98.43  
   98.44  lemma [code_abbrev]:
   98.45 -  "real_of_int (number_of k) = number_of k"
   98.46 -  unfolding number_of_is_id number_of_real_def ..
   98.47 +  "real_of_int (numeral k) = numeral k"
   98.48 +  "real_of_int (neg_numeral k) = neg_numeral k"
   98.49 +  by simp_all
   98.50  
   98.51  text{*Collapse applications of @{term real} to @{term number_of}*}
   98.52 -lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
   98.53 -by (simp add: real_of_int_def)
   98.54 +lemma real_numeral [simp]:
   98.55 +  "real (numeral v :: int) = numeral v"
   98.56 +  "real (neg_numeral v :: int) = neg_numeral v"
   98.57 +by (simp_all add: real_of_int_def)
   98.58  
   98.59 -lemma real_of_nat_number_of [simp]:
   98.60 -     "real (number_of v :: nat) =  
   98.61 -        (if neg (number_of v :: int) then 0  
   98.62 -         else (number_of v :: real))"
   98.63 -by (simp add: real_of_int_of_nat_eq [symmetric])
   98.64 +lemma real_of_nat_numeral [simp]:
   98.65 +  "real (numeral v :: nat) = numeral v"
   98.66 +by (simp add: real_of_nat_def)
   98.67  
   98.68  declaration {*
   98.69    K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
   98.70 @@ -1491,7 +1483,7 @@
   98.71        @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
   98.72        @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
   98.73        @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
   98.74 -      @{thm real_of_nat_number_of}, @{thm real_number_of}]
   98.75 +      @{thm real_of_nat_numeral}, @{thm real_numeral(1)}, @{thm real_numeral(2)}]
   98.76    #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "nat \<Rightarrow> real"})
   98.77    #> Lin_Arith.add_inj_const (@{const_name real}, @{typ "int \<Rightarrow> real"}))
   98.78  *}
   98.79 @@ -1605,37 +1597,61 @@
   98.80  
   98.81  subsection {* Implementation of rational real numbers *}
   98.82  
   98.83 +text {* Formal constructor *}
   98.84 +
   98.85  definition Ratreal :: "rat \<Rightarrow> real" where
   98.86 -  [simp]: "Ratreal = of_rat"
   98.87 +  [code_abbrev, simp]: "Ratreal = of_rat"
   98.88  
   98.89  code_datatype Ratreal
   98.90  
   98.91 -lemma Ratreal_number_collapse [code_post]:
   98.92 -  "Ratreal 0 = 0"
   98.93 -  "Ratreal 1 = 1"
   98.94 -  "Ratreal (number_of k) = number_of k"
   98.95 -by simp_all
   98.96  
   98.97 -lemma zero_real_code [code, code_unfold]:
   98.98 +text {* Numerals *}
   98.99 +
  98.100 +lemma [code_abbrev]:
  98.101 +  "(of_rat (of_int a) :: real) = of_int a"
  98.102 +  by simp
  98.103 +
  98.104 +lemma [code_abbrev]:
  98.105 +  "(of_rat 0 :: real) = 0"
  98.106 +  by simp
  98.107 +
  98.108 +lemma [code_abbrev]:
  98.109 +  "(of_rat 1 :: real) = 1"
  98.110 +  by simp
  98.111 +
  98.112 +lemma [code_abbrev]:
  98.113 +  "(of_rat (numeral k) :: real) = numeral k"
  98.114 +  by simp
  98.115 +
  98.116 +lemma [code_abbrev]:
  98.117 +  "(of_rat (neg_numeral k) :: real) = neg_numeral k"
  98.118 +  by simp
  98.119 +
  98.120 +lemma [code_post]:
  98.121 +  "(of_rat (0 / r)  :: real) = 0"
  98.122 +  "(of_rat (r / 0)  :: real) = 0"
  98.123 +  "(of_rat (1 / 1)  :: real) = 1"
  98.124 +  "(of_rat (numeral k / 1) :: real) = numeral k"
  98.125 +  "(of_rat (neg_numeral k / 1) :: real) = neg_numeral k"
  98.126 +  "(of_rat (1 / numeral k) :: real) = 1 / numeral k"
  98.127 +  "(of_rat (1 / neg_numeral k) :: real) = 1 / neg_numeral k"
  98.128 +  "(of_rat (numeral k / numeral l)  :: real) = numeral k / numeral l"
  98.129 +  "(of_rat (numeral k / neg_numeral l)  :: real) = numeral k / neg_numeral l"
  98.130 +  "(of_rat (neg_numeral k / numeral l)  :: real) = neg_numeral k / numeral l"
  98.131 +  "(of_rat (neg_numeral k / neg_numeral l)  :: real) = neg_numeral k / neg_numeral l"
  98.132 +  by (simp_all add: of_rat_divide)
  98.133 +
  98.134 +
  98.135 +text {* Operations *}
  98.136 +
  98.137 +lemma zero_real_code [code]:
  98.138    "0 = Ratreal 0"
  98.139  by simp
  98.140  
  98.141 -lemma one_real_code [code, code_unfold]:
  98.142 +lemma one_real_code [code]:
  98.143    "1 = Ratreal 1"
  98.144  by simp
  98.145  
  98.146 -lemma number_of_real_code [code_unfold]:
  98.147 -  "number_of k = Ratreal (number_of k)"
  98.148 -by simp
  98.149 -
  98.150 -lemma Ratreal_number_of_quotient [code_post]:
  98.151 -  "Ratreal (number_of r) / Ratreal (number_of s) = number_of r / number_of s"
  98.152 -by simp
  98.153 -
  98.154 -lemma Ratreal_number_of_quotient2 [code_post]:
  98.155 -  "Ratreal (number_of r / number_of s) = number_of r / number_of s"
  98.156 -unfolding Ratreal_number_of_quotient [symmetric] Ratreal_def of_rat_divide ..
  98.157 -
  98.158  instantiation real :: equal
  98.159  begin
  98.160  
  98.161 @@ -1681,6 +1697,9 @@
  98.162  lemma real_floor_code [code]: "floor (Ratreal x) = floor x"
  98.163    by (metis Ratreal_def floor_le_iff floor_unique le_floor_iff of_int_floor_le of_rat_of_int_eq real_less_eq_code)
  98.164  
  98.165 +
  98.166 +text {* Quickcheck *}
  98.167 +
  98.168  definition (in term_syntax)
  98.169    valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
  98.170    [code_unfold]: "valterm_ratreal k = Code_Evaluation.valtermify Ratreal {\<cdot>} k"
  98.171 @@ -1741,14 +1760,12 @@
  98.172      (@{const_name plus_real_inst.plus_real}, @{const_name Nitpick.plus_frac}),
  98.173      (@{const_name times_real_inst.times_real}, @{const_name Nitpick.times_frac}),
  98.174      (@{const_name uminus_real_inst.uminus_real}, @{const_name Nitpick.uminus_frac}),
  98.175 -    (@{const_name number_real_inst.number_of_real}, @{const_name Nitpick.number_of_frac}),
  98.176      (@{const_name inverse_real_inst.inverse_real}, @{const_name Nitpick.inverse_frac}),
  98.177      (@{const_name ord_real_inst.less_real}, @{const_name Nitpick.less_frac}),
  98.178      (@{const_name ord_real_inst.less_eq_real}, @{const_name Nitpick.less_eq_frac})]
  98.179  *}
  98.180  
  98.181 -lemmas [nitpick_unfold] = inverse_real_inst.inverse_real
  98.182 -    number_real_inst.number_of_real one_real_inst.one_real
  98.183 +lemmas [nitpick_unfold] = inverse_real_inst.inverse_real one_real_inst.one_real
  98.184      ord_real_inst.less_real ord_real_inst.less_eq_real plus_real_inst.plus_real
  98.185      times_real_inst.times_real uminus_real_inst.uminus_real
  98.186      zero_real_inst.zero_real
    99.1 --- a/src/HOL/RealVector.thy	Fri Mar 23 20:32:43 2012 +0100
    99.2 +++ b/src/HOL/RealVector.thy	Mon Mar 26 10:56:56 2012 +0200
    99.3 @@ -303,9 +303,11 @@
    99.4  lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
    99.5  by (cases z rule: int_diff_cases, simp)
    99.6  
    99.7 -lemma of_real_number_of_eq:
    99.8 -  "of_real (number_of w) = (number_of w :: 'a::{number_ring,real_algebra_1})"
    99.9 -by (simp add: number_of_eq)
   99.10 +lemma of_real_numeral: "of_real (numeral w) = numeral w"
   99.11 +using of_real_of_int_eq [of "numeral w"] by simp
   99.12 +
   99.13 +lemma of_real_neg_numeral: "of_real (neg_numeral w) = neg_numeral w"
   99.14 +using of_real_of_int_eq [of "neg_numeral w"] by simp
   99.15  
   99.16  text{*Every real algebra has characteristic zero*}
   99.17  
   99.18 @@ -335,9 +337,11 @@
   99.19  lemma Reals_of_nat [simp]: "of_nat n \<in> Reals"
   99.20  by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
   99.21  
   99.22 -lemma Reals_number_of [simp]:
   99.23 -  "(number_of w::'a::{number_ring,real_algebra_1}) \<in> Reals"
   99.24 -by (subst of_real_number_of_eq [symmetric], rule Reals_of_real)
   99.25 +lemma Reals_numeral [simp]: "numeral w \<in> Reals"
   99.26 +by (subst of_real_numeral [symmetric], rule Reals_of_real)
   99.27 +
   99.28 +lemma Reals_neg_numeral [simp]: "neg_numeral w \<in> Reals"
   99.29 +by (subst of_real_neg_numeral [symmetric], rule Reals_of_real)
   99.30  
   99.31  lemma Reals_0 [simp]: "0 \<in> Reals"
   99.32  apply (unfold Reals_def)
   99.33 @@ -752,10 +756,13 @@
   99.34    "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
   99.35  unfolding of_real_def by simp
   99.36  
   99.37 -lemma norm_number_of [simp]:
   99.38 -  "norm (number_of w::'a::{number_ring,real_normed_algebra_1})
   99.39 -    = \<bar>number_of w\<bar>"
   99.40 -by (subst of_real_number_of_eq [symmetric], rule norm_of_real)
   99.41 +lemma norm_numeral [simp]:
   99.42 +  "norm (numeral w::'a::real_normed_algebra_1) = numeral w"
   99.43 +by (subst of_real_numeral [symmetric], subst norm_of_real, simp)
   99.44 +
   99.45 +lemma norm_neg_numeral [simp]:
   99.46 +  "norm (neg_numeral w::'a::real_normed_algebra_1) = numeral w"
   99.47 +by (subst of_real_neg_numeral [symmetric], subst norm_of_real, simp)
   99.48  
   99.49  lemma norm_of_int [simp]:
   99.50    "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
   100.1 --- a/src/HOL/SMT_Examples/SMT_Examples.thy	Fri Mar 23 20:32:43 2012 +0100
   100.2 +++ b/src/HOL/SMT_Examples/SMT_Examples.thy	Mon Mar 26 10:56:56 2012 +0200
   100.3 @@ -467,7 +467,7 @@
   100.4  lemma "(f g (x::'a::type) = (g x \<and> True)) \<or> (f g x = True) \<or> (g x = True)"
   100.5    by smt
   100.6  
   100.7 -lemma "id 3 = 3 \<and> id True = True" by (smt id_def)
   100.8 +lemma "id x = x \<and> id True = True" (* BROKEN by (smt id_def) *) oops
   100.9  
  100.10  lemma "i \<noteq> i1 \<and> i \<noteq> i2 \<Longrightarrow> ((f (i1 := v1)) (i2 := v2)) i = f i"
  100.11    using fun_upd_same fun_upd_apply
   101.1 --- a/src/HOL/SMT_Examples/SMT_Tests.thy	Fri Mar 23 20:32:43 2012 +0100
   101.2 +++ b/src/HOL/SMT_Examples/SMT_Tests.thy	Mon Mar 26 10:56:56 2012 +0200
   101.3 @@ -211,8 +211,8 @@
   101.4  
   101.5  lemma
   101.6    assumes "\<forall>x y. SMT.trigger [[SMT.pat (f x), SMT.pat (g y)]] (f x = g y)"
   101.7 -  shows "f 1 = g 2"
   101.8 -  using assms by smt
   101.9 +  shows "f a = g b"
  101.10 +  using assms (* BROKEN by smt *) oops
  101.11  
  101.12  lemma
  101.13    assumes "ALL x. SMT.trigger [[SMT.pat (P x)]] (P x --> Q x)"
  101.14 @@ -853,7 +853,7 @@
  101.15    using point.simps
  101.16    using [[smt_datatypes, smt_oracle]]
  101.17    using [[z3_options="AUTO_CONFIG=false"]]
  101.18 -  by smt+
  101.19 +  (* BROKEN by smt+ *) oops
  101.20  
  101.21  lemma
  101.22    "cy (p \<lparr> cx := a \<rparr>) = cy p"
  101.23 @@ -862,7 +862,7 @@
  101.24    using point.simps
  101.25    using [[smt_datatypes, smt_oracle]]
  101.26    using [[z3_options="AUTO_CONFIG=false"]]
  101.27 -  by smt+
  101.28 +  (* BROKEN by smt+ *) oops
  101.29  
  101.30  lemma
  101.31    "p1 = p2 \<longrightarrow> cx p1 = cx p2"
  101.32 @@ -891,7 +891,7 @@
  101.33    using point.simps bw_point.simps
  101.34    using [[smt_datatypes, smt_oracle]]
  101.35    using [[z3_options="AUTO_CONFIG=false"]]
  101.36 -  by smt+
  101.37 +  (* BROKEN by smt+ *) oops
  101.38  
  101.39  lemma
  101.40    "\<lparr> cx = 3, cy = 4, black = b \<rparr> \<lparr> black := w \<rparr> = \<lparr> cx = 3, cy = 4, black = w \<rparr>"
  101.41 @@ -905,7 +905,7 @@
  101.42    using point.simps bw_point.simps
  101.43    using [[smt_datatypes, smt_oracle]]
  101.44    using [[z3_options="AUTO_CONFIG=false"]]
  101.45 -  by smt
  101.46 +  (* BROKEN by smt *) oops
  101.47  
  101.48  
  101.49  subsubsection {* Type definitions *}
  101.50 @@ -919,7 +919,7 @@
  101.51    using n1_def n2_def n3_def nplus_def
  101.52    using [[smt_datatypes, smt_oracle]]
  101.53    using [[z3_options="AUTO_CONFIG=false"]]
  101.54 -  by smt+
  101.55 +  (* BROKEN by smt+ *) oops
  101.56  
  101.57  
  101.58  
   102.1 --- a/src/HOL/SPARK/SPARK.thy	Fri Mar 23 20:32:43 2012 +0100
   102.2 +++ b/src/HOL/SPARK/SPARK.thy	Mon Mar 26 10:56:56 2012 +0200
   102.3 @@ -145,7 +145,7 @@
   102.4        then have "bin = 0" "bit = 0"
   102.5          by (auto simp add: Bit_def bitval_def split add: bit.split_asm) arith
   102.6        then show ?thesis using 0 1 `y < 2 ^ n`
   102.7 -        by simp (simp add: Bit0_def int_or_Pls [unfolded Pls_def])
   102.8 +        by simp
   102.9      next
  102.10        case (Suc m)
  102.11        from 3 have "0 \<le> bin"
  102.12 @@ -188,7 +188,7 @@
  102.13        then have "bin = 0" "bit = 0"
  102.14          by (auto simp add: Bit_def bitval_def split add: bit.split_asm) arith
  102.15        then show ?thesis using 0 1 `y < 2 ^ n`
  102.16 -        by simp (simp add: Bit0_def int_xor_Pls [unfolded Pls_def])
  102.17 +        by simp
  102.18      next
  102.19        case (Suc m)
  102.20        from 3 have "0 \<le> bin"
  102.21 @@ -257,17 +257,17 @@
  102.22  proof (induct x arbitrary: n rule: bin_induct)
  102.23    case 1
  102.24    then show ?case
  102.25 -    by simp (simp add: Pls_def)
  102.26 +    by simp
  102.27  next
  102.28    case 2
  102.29    then show ?case
  102.30 -    by (simp, simp only: Min_def, simp add: m1mod2k)
  102.31 +    by (simp, simp add: m1mod2k)
  102.32  next
  102.33    case (3 bin bit)
  102.34    show ?case
  102.35    proof (cases n)
  102.36      case 0
  102.37 -    then show ?thesis by (simp add: int_and_extra_simps [unfolded Pls_def])
  102.38 +    then show ?thesis by (simp add: int_and_extra_simps)
  102.39    next
  102.40      case (Suc m)
  102.41      with 3 show ?thesis
   103.1 --- a/src/HOL/Semiring_Normalization.thy	Fri Mar 23 20:32:43 2012 +0100
   103.2 +++ b/src/HOL/Semiring_Normalization.thy	Mon Mar 26 10:56:56 2012 +0200
   103.3 @@ -116,7 +116,8 @@
   103.4    "x ^ (Suc q) = x * (x ^ q)"
   103.5    "x ^ (2*n) = (x ^ n) * (x ^ n)"
   103.6    "x ^ (Suc (2*n)) = x * ((x ^ n) * (x ^ n))"
   103.7 -  by (simp_all add: algebra_simps power_add power2_eq_square power_mult_distrib power_mult)
   103.8 +  by (simp_all add: algebra_simps power_add power2_eq_square
   103.9 +    power_mult_distrib power_mult del: one_add_one)
  103.10  
  103.11  lemmas normalizing_comm_semiring_1_axioms =
  103.12    comm_semiring_1_axioms [normalizer
  103.13 @@ -218,4 +219,13 @@
  103.14  
  103.15  hide_fact (open) normalizing_field_axioms normalizing_field_ops normalizing_field_rules
  103.16  
  103.17 +code_modulename SML
  103.18 +  Semiring_Normalization Arith
  103.19 +
  103.20 +code_modulename OCaml
  103.21 +  Semiring_Normalization Arith
  103.22 +
  103.23 +code_modulename Haskell
  103.24 +  Semiring_Normalization Arith
  103.25 +
  103.26  end
   104.1 --- a/src/HOL/Series.thy	Fri Mar 23 20:32:43 2012 +0100
   104.2 +++ b/src/HOL/Series.thy	Mon Mar 26 10:56:56 2012 +0200
   104.3 @@ -417,8 +417,8 @@
   104.4    shows "norm x < 1 \<Longrightarrow> summable (\<lambda>n. x ^ n)"
   104.5  by (rule geometric_sums [THEN sums_summable])
   104.6  
   104.7 -lemma half: "0 < 1 / (2::'a::{number_ring,linordered_field_inverse_zero})"
   104.8 -  by arith
   104.9 +lemma half: "0 < 1 / (2::'a::linordered_field)"
  104.10 +  by simp
  104.11  
  104.12  lemma power_half_series: "(\<lambda>n. (1/2::real)^Suc n) sums 1"
  104.13  proof -
   105.1 --- a/src/HOL/SetInterval.thy	Fri Mar 23 20:32:43 2012 +0100
   105.2 +++ b/src/HOL/SetInterval.thy	Mon Mar 26 10:56:56 2012 +0200
   105.3 @@ -1282,7 +1282,7 @@
   105.4  
   105.5  subsection {* The formula for arithmetic sums *}
   105.6  
   105.7 -lemma gauss_sum:
   105.8 +lemma gauss_sum: (* FIXME: rephrase in terms of "2" *)
   105.9    "((1::'a::comm_semiring_1) + 1)*(\<Sum>i\<in>{1..n}. of_nat i) =
  105.10     of_nat n*((of_nat n)+1)"
  105.11  proof (induct n)
  105.12 @@ -1290,7 +1290,7 @@
  105.13    show ?case by simp
  105.14  next
  105.15    case (Suc n)
  105.16 -  then show ?case by (simp add: algebra_simps)
  105.17 +  then show ?case by (simp add: algebra_simps del: one_add_one) (* FIXME *)
  105.18  qed
  105.19  
  105.20  theorem arith_series_general:
  105.21 @@ -1308,18 +1308,18 @@
  105.22      unfolding One_nat_def
  105.23      by (simp add: setsum_right_distrib atLeast0LessThan[symmetric] setsum_shift_lb_Suc0_0_upt mult_ac)
  105.24    also have "(1+1)*\<dots> = (1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..<n}. ?I i)"
  105.25 -    by (simp add: left_distrib right_distrib)
  105.26 +    by (simp add: left_distrib right_distrib del: one_add_one)
  105.27    also from ngt1 have "{1..<n} = {1..n - 1}"
  105.28      by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost)
  105.29    also from ngt1
  105.30    have "(1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..n - 1}. ?I i) = ((1+1)*?n*a + d*?I (n - 1)*?I n)"
  105.31      by (simp only: mult_ac gauss_sum [of "n - 1"], unfold One_nat_def)
  105.32         (simp add:  mult_ac trans [OF add_commute of_nat_Suc [symmetric]])
  105.33 -  finally show ?thesis by (simp add: algebra_simps)
  105.34 +  finally show ?thesis by (simp add: algebra_simps del: one_add_one)
  105.35  next
  105.36    assume "\<not>(n > 1)"
  105.37    hence "n = 1 \<or> n = 0" by auto
  105.38 -  thus ?thesis by (auto simp: algebra_simps)
  105.39 +  thus ?thesis by (auto simp: algebra_simps mult_2_right)
  105.40  qed
  105.41  
  105.42  lemma arith_series_nat:
   106.1 --- a/src/HOL/Tools/Nitpick/nitpick.ML	Fri Mar 23 20:32:43 2012 +0100
   106.2 +++ b/src/HOL/Tools/Nitpick/nitpick.ML	Mon Mar 26 10:56:56 2012 +0200
   106.3 @@ -206,7 +206,7 @@
   106.4  
   106.5  val syntactic_sorts =
   106.6    @{sort "{default,zero,one,plus,minus,uminus,times,inverse,abs,sgn,ord,equal}"} @
   106.7 -  @{sort number}
   106.8 +  @{sort numeral}
   106.9  fun has_tfree_syntactic_sort (TFree (_, S as _ :: _)) =
  106.10      subset (op =) (S, syntactic_sorts)
  106.11    | has_tfree_syntactic_sort _ = false
   107.1 --- a/src/HOL/Tools/Nitpick/nitpick_hol.ML	Fri Mar 23 20:32:43 2012 +0100
   107.2 +++ b/src/HOL/Tools/Nitpick/nitpick_hol.ML	Mon Mar 26 10:56:56 2012 +0200
   107.3 @@ -1636,30 +1636,32 @@
   107.4          (hol_ctxt as {thy, ctxt, stds, whacks, total_consts, case_names,
   107.5                        def_tables, ground_thm_table, ersatz_table, ...}) =
   107.6    let
   107.7 -    fun do_term depth Ts t =
   107.8 +    fun do_numeral depth Ts mult T t0 t1 =
   107.9 +      (if is_number_type ctxt T then
  107.10 +         let
  107.11 +           val j = mult * (HOLogic.dest_num t1)
  107.12 +                   |> T = nat_T ? Integer.max 0
  107.13 +           val s = numeral_prefix ^ signed_string_of_int j
  107.14 +         in
  107.15 +           if is_integer_like_type T then
  107.16 +             if is_standard_datatype thy stds T then Const (s, T)
  107.17 +             else funpow j (curry (op $) (suc_const T)) (zero_const T)
  107.18 +           else
  107.19 +             do_term depth Ts (Const (@{const_name of_int}, int_T --> T)
  107.20 +                               $ Const (s, int_T))
  107.21 +         end
  107.22 +         handle TERM _ => raise SAME ()
  107.23 +       else
  107.24 +         raise SAME ())
  107.25 +      handle SAME () => s_betapply [] (do_term depth Ts t0, do_term depth Ts t1)
  107.26 +    and do_term depth Ts t =
  107.27        case t of
  107.28 -        (t0 as Const (@{const_name Int.number_class.number_of},
  107.29 +        (t0 as Const (@{const_name Num.neg_numeral_class.neg_numeral},
  107.30                        Type (@{type_name fun}, [_, ran_T]))) $ t1 =>
  107.31 -        ((if is_number_type ctxt ran_T then
  107.32 -            let
  107.33 -              val j = t1 |> HOLogic.dest_numeral
  107.34 -                         |> ran_T = nat_T ? Integer.max 0
  107.35 -              val s = numeral_prefix ^ signed_string_of_int j
  107.36 -            in
  107.37 -              if is_integer_like_type ran_T then
  107.38 -                if is_standard_datatype thy stds ran_T then
  107.39 -                  Const (s, ran_T)
  107.40 -                else
  107.41 -                  funpow j (curry (op $) (suc_const ran_T)) (zero_const ran_T)
  107.42 -              else
  107.43 -                do_term depth Ts (Const (@{const_name of_int}, int_T --> ran_T)
  107.44 -                                  $ Const (s, int_T))
  107.45 -            end
  107.46 -            handle TERM _ => raise SAME ()
  107.47 -          else
  107.48 -            raise SAME ())
  107.49 -         handle SAME () =>
  107.50 -                s_betapply [] (do_term depth Ts t0, do_term depth Ts t1))
  107.51 +        do_numeral depth Ts ~1 ran_T t0 t1
  107.52 +      | (t0 as Const (@{const_name Num.numeral_class.numeral},
  107.53 +                      Type (@{type_name fun}, [_, ran_T]))) $ t1 =>
  107.54 +        do_numeral depth Ts 1 ran_T t0 t1
  107.55        | Const (@{const_name refl_on}, T) $ Const (@{const_name top}, _) $ t2 =>
  107.56          do_const depth Ts t (@{const_name refl'}, range_type T) [t2]
  107.57        | (t0 as Const (@{const_name Sigma}, Type (_, [T1, Type (_, [T2, T3])])))
   108.1 --- a/src/HOL/Tools/Predicate_Compile/predicate_compile_data.ML	Fri Mar 23 20:32:43 2012 +0100
   108.2 +++ b/src/HOL/Tools/Predicate_Compile/predicate_compile_data.ML	Mon Mar 26 10:56:56 2012 +0200
   108.3 @@ -240,10 +240,12 @@
   108.4    @{const_name Groups.one},  @{const_name Groups.plus},
   108.5    @{const_name Nat.ord_nat_inst.less_eq_nat},
   108.6    @{const_name Nat.ord_nat_inst.less_nat},
   108.7 +(* FIXME
   108.8    @{const_name number_nat_inst.number_of_nat},
   108.9 -  @{const_name Int.Bit0},
  108.10 -  @{const_name Int.Bit1},
  108.11 -  @{const_name Int.Pls},
  108.12 +*)
  108.13 +  @{const_name Num.Bit0},
  108.14 +  @{const_name Num.Bit1},
  108.15 +  @{const_name Num.One},
  108.16    @{const_name Int.zero_int_inst.zero_int},
  108.17    @{const_name List.filter},
  108.18    @{const_name HOL.If},
   109.1 --- a/src/HOL/Tools/Qelim/cooper.ML	Fri Mar 23 20:32:43 2012 +0100
   109.2 +++ b/src/HOL/Tools/Qelim/cooper.ML	Mon Mar 26 10:56:56 2012 +0200
   109.3 @@ -41,9 +41,9 @@
   109.4     @{term "Ex :: (int => _) => _"}, @{term "Ex :: (nat => _) => _"},
   109.5     @{term "All :: (int => _) => _"}, @{term "All :: (nat => _) => _"},
   109.6     @{term "nat"}, @{term "int"},
   109.7 -   @{term "Int.Bit0"}, @{term "Int.Bit1"},
   109.8 -   @{term "Int.Pls"}, @{term "Int.Min"},
   109.9 -   @{term "Int.number_of :: int => int"}, @{term "Int.number_of :: int => nat"},
  109.10 +   @{term "Num.One"}, @{term "Num.Bit0"}, @{term "Num.Bit1"},
  109.11 +   @{term "Num.numeral :: num => int"}, @{term "Num.numeral :: num => nat"},
  109.12 +   @{term "Num.neg_numeral :: num => int"},
  109.13     @{term "0::int"}, @{term "1::int"}, @{term "0::nat"}, @{term "1::nat"},
  109.14     @{term "True"}, @{term "False"}];
  109.15  
  109.16 @@ -595,8 +595,10 @@
  109.17    | num_of_term vs (Term.Bound i) = Proc.Bound i
  109.18    | num_of_term vs @{term "0::int"} = Proc.C 0
  109.19    | num_of_term vs @{term "1::int"} = Proc.C 1
  109.20 -  | num_of_term vs (t as Const (@{const_name number_of}, _) $ _) =
  109.21 +  | num_of_term vs (t as Const (@{const_name numeral}, _) $ _) =
  109.22        Proc.C (dest_number t)
  109.23 +  | num_of_term vs (t as Const (@{const_name neg_numeral}, _) $ _) =
  109.24 +      Proc.Neg (Proc.C (dest_number t))
  109.25    | num_of_term vs (Const (@{const_name Groups.uminus}, _) $ t') =
  109.26        Proc.Neg (num_of_term vs t')
  109.27    | num_of_term vs (Const (@{const_name Groups.plus}, _) $ t1 $ t2) =
  109.28 @@ -784,16 +786,16 @@
  109.29  
  109.30  local
  109.31  val ss1 = comp_ss
  109.32 -  addsimps @{thms simp_thms} @ [@{thm "nat_number_of_def"}, @{thm "zdvd_int"}]
  109.33 -      @ map (fn r => r RS sym)
  109.34 +  addsimps @{thms simp_thms} @ [@{thm "nat_numeral"} RS sym, @{thm "zdvd_int"}] 
  109.35 +      @ map (fn r => r RS sym) 
  109.36          [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
  109.37           @{thm "zmult_int"}]
  109.38    |> Splitter.add_split @{thm "zdiff_int_split"}
  109.39  
  109.40  val ss2 = HOL_basic_ss
  109.41 -  addsimps [@{thm "nat_0_le"}, @{thm "int_nat_number_of"},
  109.42 -            @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "number_of1"}, 
  109.43 -            @{thm "number_of2"}, @{thm "int_0"}, @{thm "int_1"}, @{thm "Suc_eq_plus1"}]
  109.44 +  addsimps [@{thm "nat_0_le"}, @{thm "int_numeral"},
  109.45 +            @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "zero_le_numeral"}, 
  109.46 +            @{thm "le_numeral_extra"(3)}, @{thm "int_0"}, @{thm "int_1"}, @{thm "Suc_eq_plus1"}]
  109.47    |> fold Simplifier.add_cong [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
  109.48  val div_mod_ss = HOL_basic_ss addsimps @{thms simp_thms}
  109.49    @ map (Thm.symmetric o mk_meta_eq) 
   110.1 --- a/src/HOL/Tools/Quickcheck/PNF_Narrowing_Engine.hs	Fri Mar 23 20:32:43 2012 +0100
   110.2 +++ b/src/HOL/Tools/Quickcheck/PNF_Narrowing_Engine.hs	Mon Mar 26 10:56:56 2012 +0200
   110.3 @@ -301,7 +301,7 @@
   110.4  checkOf (Generated_Code.Universal _ f _) = (\(t : ts) -> checkOf (f t) ts)
   110.5  checkOf (Generated_Code.Existential _ f _) = (\(t : ts) -> checkOf (f t) ts)
   110.6  
   110.7 -dummy = Generated_Code.Var [] (Generated_Code.SumOfProd [[]])
   110.8 +dummy = Generated_Code.Narrowing_variable [] (Generated_Code.Narrowing_sum_of_products [[]])
   110.9  
  110.10  treeOf :: Int -> Generated_Code.Property -> QuantTree
  110.11  treeOf n (Generated_Code.Property _) = Node uneval
   111.1 --- a/src/HOL/Tools/Quotient/quotient_def.ML	Fri Mar 23 20:32:43 2012 +0100
   111.2 +++ b/src/HOL/Tools/Quotient/quotient_def.ML	Mon Mar 26 10:56:56 2012 +0200
   111.3 @@ -6,13 +6,17 @@
   111.4  
   111.5  signature QUOTIENT_DEF =
   111.6  sig
   111.7 +  val add_quotient_def:
   111.8 +    ((binding * mixfix) * Attrib.binding) * (term * term) -> thm ->
   111.9 +    local_theory -> Quotient_Info.quotconsts * local_theory
  111.10 +
  111.11    val quotient_def:
  111.12      (binding * typ option * mixfix) option * (Attrib.binding * (term * term)) ->
  111.13 -    local_theory -> Quotient_Info.quotconsts * local_theory
  111.14 +    local_theory -> Proof.state
  111.15  
  111.16    val quotient_def_cmd:
  111.17      (binding * string option * mixfix) option * (Attrib.binding * (string * string)) ->
  111.18 -    local_theory -> Quotient_Info.quotconsts * local_theory
  111.19 +    local_theory -> Proof.state
  111.20  
  111.21    val lift_raw_const: typ list -> (string * term * mixfix) -> local_theory ->
  111.22      Quotient_Info.quotconsts * local_theory
  111.23 @@ -23,6 +27,130 @@
  111.24  
  111.25  (** Interface and Syntax Setup **)
  111.26  
  111.27 +(* Generation of the code certificate from the rsp theorem *)
  111.28 +
  111.29 +infix 0 MRSL
  111.30 +
  111.31 +fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm
  111.32 +
  111.33 +fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V)
  111.34 +  | get_body_types (U, V)  = (U, V)
  111.35 +
  111.36 +fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W)
  111.37 +  | get_binder_types _ = []
  111.38 +
  111.39 +fun unabs_def ctxt def = 
  111.40 +  let
  111.41 +    val (_, rhs) = Thm.dest_equals (cprop_of def)
  111.42 +    fun dest_abs (Abs (var_name, T, _)) = (var_name, T)
  111.43 +      | dest_abs tm = raise TERM("get_abs_var",[tm])
  111.44 +    val (var_name, T) = dest_abs (term_of rhs)
  111.45 +    val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt
  111.46 +    val thy = Proof_Context.theory_of ctxt'
  111.47 +    val refl_thm = Thm.reflexive (cterm_of thy (Free (hd new_var_names, T)))
  111.48 +  in
  111.49 +    Thm.combination def refl_thm |>
  111.50 +    singleton (Proof_Context.export ctxt' ctxt)
  111.51 +  end
  111.52 +
  111.53 +fun unabs_all_def ctxt def = 
  111.54 +  let
  111.55 +    val (_, rhs) = Thm.dest_equals (cprop_of def)
  111.56 +    val xs = strip_abs_vars (term_of rhs)
  111.57 +  in  
  111.58 +    fold (K (unabs_def ctxt)) xs def
  111.59 +  end
  111.60 +
  111.61 +val map_fun_unfolded = 
  111.62 +  @{thm map_fun_def[abs_def]} |>
  111.63 +  unabs_def @{context} |>
  111.64 +  unabs_def @{context} |>
  111.65 +  Local_Defs.unfold @{context} [@{thm comp_def}]
  111.66 +
  111.67 +fun unfold_fun_maps ctm =
  111.68 +  let
  111.69 +    fun unfold_conv ctm =
  111.70 +      case (Thm.term_of ctm) of
  111.71 +        Const (@{const_name "map_fun"}, _) $ _ $ _ => 
  111.72 +          (Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm
  111.73 +        | _ => Conv.all_conv ctm
  111.74 +    val try_beta_conv = Conv.try_conv (Thm.beta_conversion false)
  111.75 +  in
  111.76 +    (Conv.arg_conv (Conv.fun_conv unfold_conv then_conv try_beta_conv)) ctm
  111.77 +  end
  111.78 +
  111.79 +fun prove_rel ctxt rsp_thm (rty, qty) =
  111.80 +  let
  111.81 +    val ty_args = get_binder_types (rty, qty)
  111.82 +    fun disch_arg args_ty thm = 
  111.83 +      let
  111.84 +        val quot_thm = Quotient_Term.prove_quot_theorem ctxt args_ty
  111.85 +      in
  111.86 +        [quot_thm, thm] MRSL @{thm apply_rsp''}
  111.87 +      end
  111.88 +  in
  111.89 +    fold disch_arg ty_args rsp_thm
  111.90 +  end
  111.91 +
  111.92 +exception CODE_CERT_GEN of string
  111.93 +
  111.94 +fun simplify_code_eq ctxt def_thm = 
  111.95 +  Local_Defs.unfold ctxt [@{thm o_def}, @{thm map_fun_def}, @{thm id_def}] def_thm
  111.96 +
  111.97 +fun generate_code_cert ctxt def_thm rsp_thm (rty, qty) =
  111.98 +  let
  111.99 +    val quot_thm = Quotient_Term.prove_quot_theorem ctxt (get_body_types (rty, qty))
 111.100 +    val fun_rel = prove_rel ctxt rsp_thm (rty, qty)
 111.101 +    val abs_rep_thm = [quot_thm, fun_rel] MRSL @{thm Quotient_rep_abs}
 111.102 +    val abs_rep_eq = 
 111.103 +      case (HOLogic.dest_Trueprop o prop_of) fun_rel of
 111.104 +        Const (@{const_name HOL.eq}, _) $ _ $ _ => abs_rep_thm
 111.105 +        | Const (@{const_name invariant}, _) $ _ $ _ $ _ => abs_rep_thm RS @{thm invariant_to_eq}
 111.106 +        | _ => raise CODE_CERT_GEN "relation is neither equality nor invariant"
 111.107 +    val unfolded_def = Conv.fconv_rule unfold_fun_maps def_thm
 111.108 +    val unabs_def = unabs_all_def ctxt unfolded_def
 111.109 +    val rep = (snd o Thm.dest_comb o snd o Thm.dest_comb o cprop_of) quot_thm
 111.110 +    val rep_refl = Thm.reflexive rep RS @{thm meta_eq_to_obj_eq}
 111.111 +    val repped_eq = [rep_refl, unabs_def RS @{thm meta_eq_to_obj_eq}] MRSL @{thm cong}
 111.112 +    val code_cert = [repped_eq, abs_rep_eq] MRSL @{thm trans}
 111.113 +  in
 111.114 +    simplify_code_eq ctxt code_cert
 111.115 +  end
 111.116 +
 111.117 +fun define_code_cert def_thm rsp_thm (rty, qty) lthy = 
 111.118 +  let
 111.119 +    val quot_thm = Quotient_Term.prove_quot_theorem lthy (get_body_types (rty, qty))
 111.120 +  in
 111.121 +    if Quotient_Type.can_generate_code_cert quot_thm then
 111.122 +      let
 111.123 +        val code_cert = generate_code_cert lthy def_thm rsp_thm (rty, qty)
 111.124 +        val add_abs_eqn_attribute = 
 111.125 +          Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abs_eqn thm) I)
 111.126 +        val add_abs_eqn_attrib = Attrib.internal (K add_abs_eqn_attribute);
 111.127 +      in
 111.128 +        lthy
 111.129 +          |> (snd oo Local_Theory.note) ((Binding.empty, [add_abs_eqn_attrib]), [code_cert])
 111.130 +      end
 111.131 +    else
 111.132 +      lthy
 111.133 +  end
 111.134 +
 111.135 +fun define_code_eq def_thm lthy =
 111.136 +  let
 111.137 +    val unfolded_def = Conv.fconv_rule unfold_fun_maps def_thm
 111.138 +    val code_eq = unabs_all_def lthy unfolded_def
 111.139 +    val simp_code_eq = simplify_code_eq lthy code_eq
 111.140 +  in
 111.141 +    lthy
 111.142 +      |> (snd oo Local_Theory.note) ((Binding.empty, [Code.add_default_eqn_attrib]), [simp_code_eq])
 111.143 +  end
 111.144 +
 111.145 +fun define_code def_thm rsp_thm (rty, qty) lthy =
 111.146 +  if body_type rty = body_type qty then 
 111.147 +    define_code_eq def_thm lthy
 111.148 +  else 
 111.149 +    define_code_cert def_thm rsp_thm (rty, qty) lthy
 111.150 +
 111.151  (* The ML-interface for a quotient definition takes
 111.152     as argument:
 111.153  
 111.154 @@ -30,6 +158,7 @@
 111.155      - attributes
 111.156      - the new constant as term
 111.157      - the rhs of the definition as term
 111.158 +    - respectfulness theorem for the rhs
 111.159  
 111.160     It stores the qconst_info in the quotconsts data slot.
 111.161  
 111.162 @@ -45,7 +174,84 @@
 111.163        quote str ^ " differs from declaration " ^ name ^ pos)
 111.164    end
 111.165  
 111.166 -fun gen_quotient_def prep_vars prep_term (raw_var, ((name, atts), (lhs_raw, rhs_raw))) lthy =
 111.167 +fun add_quotient_def ((var, (name, atts)), (lhs, rhs)) rsp_thm lthy =
 111.168 +  let
 111.169 +    val rty = fastype_of rhs
 111.170 +    val qty = fastype_of lhs
 111.171 +    val absrep_trm = 
 111.172 +      Quotient_Term.absrep_fun lthy Quotient_Term.AbsF (rty, qty) $ rhs
 111.173 +    val prop = Syntax.check_term lthy (Logic.mk_equals (lhs, absrep_trm))
 111.174 +    val (_, prop') = Local_Defs.cert_def lthy prop
 111.175 +    val (_, newrhs) = Local_Defs.abs_def prop'
 111.176 +
 111.177 +    val ((trm, (_ , def_thm)), lthy') =
 111.178 +      Local_Theory.define (var, ((Thm.def_binding_optional (#1 var) name, atts), newrhs)) lthy
 111.179 +
 111.180 +    (* data storage *)
 111.181 +    val qconst_data = {qconst = trm, rconst = rhs, def = def_thm}
 111.182 +    fun get_rsp_thm_name (lhs_name, _) = Binding.suffix_name "_rsp" lhs_name
 111.183 +    
 111.184 +    val lthy'' = lthy'
 111.185 +      |> Local_Theory.declaration {syntax = false, pervasive = true}
 111.186 +        (fn phi =>
 111.187 +          (case Quotient_Info.transform_quotconsts phi qconst_data of
 111.188 +            qcinfo as {qconst = Const (c, _), ...} =>
 111.189 +              Quotient_Info.update_quotconsts c qcinfo
 111.190 +          | _ => I))
 111.191 +      |> (snd oo Local_Theory.note) 
 111.192 +        ((get_rsp_thm_name var, [Attrib.internal (K Quotient_Info.rsp_rules_add)]),
 111.193 +        [rsp_thm])
 111.194 +      |> define_code def_thm rsp_thm (rty, qty)
 111.195 +
 111.196 +  in
 111.197 +    (qconst_data, lthy'')
 111.198 +  end
 111.199 +
 111.200 +fun mk_readable_rsp_thm_eq tm lthy =
 111.201 +  let
 111.202 +    val ctm = cterm_of (Proof_Context.theory_of lthy) tm
 111.203 +    
 111.204 +    fun norm_fun_eq ctm = 
 111.205 +      let
 111.206 +        fun abs_conv2 cv = Conv.abs_conv (K (Conv.abs_conv (K cv) lthy)) lthy
 111.207 +        fun erase_quants ctm' =
 111.208 +          case (Thm.term_of ctm') of
 111.209 +            Const ("HOL.eq", _) $ _ $ _ => Conv.all_conv ctm'
 111.210 +            | _ => (Conv.binder_conv (K erase_quants) lthy then_conv 
 111.211 +              Conv.rewr_conv @{thm fun_eq_iff[symmetric, THEN eq_reflection]}) ctm'
 111.212 +      in
 111.213 +        (abs_conv2 erase_quants then_conv Thm.eta_conversion) ctm
 111.214 +      end
 111.215 +
 111.216 +    fun simp_arrows_conv ctm =
 111.217 +      let
 111.218 +        val unfold_conv = Conv.rewrs_conv 
 111.219 +          [@{thm fun_rel_eq_invariant[THEN eq_reflection]}, @{thm fun_rel_eq_rel[THEN eq_reflection]}, 
 111.220 +            @{thm fun_rel_def[THEN eq_reflection]}]
 111.221 +        val left_conv = simp_arrows_conv then_conv Conv.try_conv norm_fun_eq
 111.222 +        fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
 111.223 +      in
 111.224 +        case (Thm.term_of ctm) of
 111.225 +          Const (@{const_name "fun_rel"}, _) $ _ $ _ => 
 111.226 +            (binop_conv2  left_conv simp_arrows_conv then_conv unfold_conv) ctm
 111.227 +          | _ => Conv.all_conv ctm
 111.228 +      end
 111.229 +
 111.230 +    val unfold_ret_val_invs = Conv.bottom_conv 
 111.231 +      (K (Conv.try_conv (Conv.rewr_conv @{thm invariant_same_args}))) lthy 
 111.232 +    val simp_conv = Conv.arg_conv (Conv.fun2_conv simp_arrows_conv)
 111.233 +    val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]}
 111.234 +    val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy
 111.235 +    val beta_conv = Thm.beta_conversion true
 111.236 +    val eq_thm = 
 111.237 +      (simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs) ctm
 111.238 +  in
 111.239 +    Object_Logic.rulify(eq_thm RS Drule.equal_elim_rule2)
 111.240 +  end
 111.241 +
 111.242 +
 111.243 +
 111.244 +fun gen_quotient_def prep_vars prep_term (raw_var, (attr, (lhs_raw, rhs_raw))) lthy =
 111.245    let
 111.246      val (vars, ctxt) = prep_vars (the_list raw_var) lthy
 111.247      val T_opt = (case vars of [(_, SOME T, _)] => SOME T | _ => NONE)
 111.248 @@ -63,27 +269,50 @@
 111.249            if Variable.check_name binding = lhs_str then (binding, mx)
 111.250            else error_msg binding lhs_str
 111.251        | _ => raise Match)
 111.252 +    
 111.253 +    fun try_to_prove_refl thm = 
 111.254 +      let
 111.255 +        val lhs_eq =
 111.256 +          thm
 111.257 +          |> prop_of
 111.258 +          |> Logic.dest_implies
 111.259 +          |> fst
 111.260 +          |> strip_all_body
 111.261 +          |> try HOLogic.dest_Trueprop
 111.262 +      in
 111.263 +        case lhs_eq of
 111.264 +          SOME (Const ("HOL.eq", _) $ _ $ _) => SOME (@{thm refl} RS thm)
 111.265 +          | SOME _ => (case body_type (fastype_of lhs) of
 111.266 +            Type (typ_name, _) => ( SOME
 111.267 +              (#equiv_thm (the (Quotient_Info.lookup_quotients lthy typ_name)) 
 111.268 +                RS @{thm Equiv_Relations.equivp_reflp} RS thm)
 111.269 +              handle _ => NONE)
 111.270 +            | _ => NONE
 111.271 +            )
 111.272 +          | _ => NONE
 111.273 +      end
 111.274  
 111.275 -    val absrep_trm = Quotient_Term.absrep_fun lthy Quotient_Term.AbsF (fastype_of rhs, lhs_ty) $ rhs
 111.276 -    val prop = Syntax.check_term lthy (Logic.mk_equals (lhs, absrep_trm))
 111.277 -    val (_, prop') = Local_Defs.cert_def lthy prop
 111.278 -    val (_, newrhs) = Local_Defs.abs_def prop'
 111.279 +    val rsp_rel = Quotient_Term.equiv_relation lthy (fastype_of rhs, lhs_ty)
 111.280 +    val internal_rsp_tm = HOLogic.mk_Trueprop (Syntax.check_term lthy (rsp_rel $ rhs $ rhs))
 111.281 +    val readable_rsp_thm_eq = mk_readable_rsp_thm_eq internal_rsp_tm lthy
 111.282 +    val maybe_proven_rsp_thm = try_to_prove_refl readable_rsp_thm_eq
 111.283 +    val (readable_rsp_tm, _) = Logic.dest_implies (prop_of readable_rsp_thm_eq)
 111.284 +  
 111.285 +    fun after_qed thm_list lthy = 
 111.286 +      let
 111.287 +        val internal_rsp_thm =
 111.288 +          case thm_list of
 111.289 +            [] => the maybe_proven_rsp_thm
 111.290 +          | [[thm]] => Goal.prove ctxt [] [] internal_rsp_tm 
 111.291 +            (fn _ => rtac readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac [thm] 1)
 111.292 +      in
 111.293 +        snd (add_quotient_def ((var, attr), (lhs, rhs)) internal_rsp_thm lthy)
 111.294 +      end
 111.295  
 111.296 -    val ((trm, (_ , thm)), lthy') =
 111.297 -      Local_Theory.define (var, ((Thm.def_binding_optional (#1 var) name, atts), newrhs)) lthy
 111.298 -
 111.299 -    (* data storage *)
 111.300 -    val qconst_data = {qconst = trm, rconst = rhs, def = thm}
 111.301 -
 111.302 -    val lthy'' = lthy'
 111.303 -      |> Local_Theory.declaration {syntax = false, pervasive = true}
 111.304 -        (fn phi =>
 111.305 -          (case Quotient_Info.transform_quotconsts phi qconst_data of
 111.306 -            qcinfo as {qconst = Const (c, _), ...} =>
 111.307 -              Quotient_Info.update_quotconsts c qcinfo
 111.308 -          | _ => I));
 111.309    in
 111.310 -    (qconst_data, lthy'')
 111.311 +    case maybe_proven_rsp_thm of
 111.312 +      SOME _ => Proof.theorem NONE after_qed [] lthy
 111.313 +      | NONE =>  Proof.theorem NONE after_qed [[(readable_rsp_tm,[])]] lthy
 111.314    end
 111.315  
 111.316  fun check_term' cnstr ctxt =
 111.317 @@ -103,16 +332,19 @@
 111.318      val qty = Quotient_Term.derive_qtyp ctxt qtys rty
 111.319      val lhs = Free (qconst_name, qty)
 111.320    in
 111.321 -    quotient_def (SOME (Binding.name qconst_name, NONE, mx), (Attrib.empty_binding, (lhs, rconst))) ctxt
 111.322 +    (*quotient_def (SOME (Binding.name qconst_name, NONE, mx), (Attrib.empty_binding, (lhs, rconst))) ctxt*)
 111.323 +    ({qconst = lhs, rconst = lhs, def = @{thm refl}}, ctxt)
 111.324    end
 111.325  
 111.326 -(* command *)
 111.327 +(* parser and command *)
 111.328 +val quotdef_parser =
 111.329 +  Scan.option Parse_Spec.constdecl --
 111.330 +    Parse.!!! (Parse_Spec.opt_thm_name ":" -- (Parse.term --| @{keyword "is"} -- Parse.term))
 111.331  
 111.332  val _ =
 111.333 -  Outer_Syntax.local_theory @{command_spec "quotient_definition"}
 111.334 +  Outer_Syntax.local_theory_to_proof @{command_spec "quotient_definition"}
 111.335      "definition for constants over the quotient type"
 111.336 -    (Scan.option Parse_Spec.constdecl --
 111.337 -      Parse.!!! (Parse_Spec.opt_thm_name ":" -- (Parse.term --| @{keyword "is"} -- Parse.term))
 111.338 -      >> (snd oo quotient_def_cmd))
 111.339 +      (quotdef_parser >> quotient_def_cmd)
 111.340 +
 111.341  
 111.342  end; (* structure *)
   112.1 --- a/src/HOL/Tools/Quotient/quotient_info.ML	Fri Mar 23 20:32:43 2012 +0100
   112.2 +++ b/src/HOL/Tools/Quotient/quotient_info.ML	Mon Mar 26 10:56:56 2012 +0200
   112.3 @@ -6,7 +6,7 @@
   112.4  
   112.5  signature QUOTIENT_INFO =
   112.6  sig
   112.7 -  type quotmaps = {relmap: string}
   112.8 +  type quotmaps = {relmap: string, quot_thm: thm}
   112.9    val lookup_quotmaps: Proof.context -> string -> quotmaps option
  112.10    val lookup_quotmaps_global: theory -> string -> quotmaps option
  112.11    val print_quotmaps: Proof.context -> unit
  112.12 @@ -18,7 +18,7 @@
  112.13    val update_abs_rep: string -> abs_rep -> Context.generic -> Context.generic
  112.14    val print_abs_rep: Proof.context -> unit
  112.15    
  112.16 -  type quotients = {qtyp: typ, rtyp: typ, equiv_rel: term, equiv_thm: thm}
  112.17 +  type quotients = {qtyp: typ, rtyp: typ, equiv_rel: term, equiv_thm: thm, quot_thm: thm}
  112.18    val transform_quotients: morphism -> quotients -> quotients
  112.19    val lookup_quotients: Proof.context -> string -> quotients option
  112.20    val lookup_quotients_global: theory -> string -> quotients option
  112.21 @@ -54,7 +54,7 @@
  112.22  (* FIXME just one data slot (record) per program unit *)
  112.23  
  112.24  (* info about map- and rel-functions for a type *)
  112.25 -type quotmaps = {relmap: string}
  112.26 +type quotmaps = {relmap: string, quot_thm: thm}
  112.27  
  112.28  structure Quotmaps = Generic_Data
  112.29  (
  112.30 @@ -71,19 +71,24 @@
  112.31  
  112.32  val quotmaps_attribute_setup =
  112.33    Attrib.setup @{binding map}
  112.34 -    ((Args.type_name true --| Scan.lift @{keyword "="}) -- Args.const_proper true >>
  112.35 -      (fn (tyname, relname) =>
  112.36 -        let val minfo = {relmap = relname}
  112.37 +    ((Args.type_name true --| Scan.lift @{keyword "="}) --
  112.38 +      (Scan.lift @{keyword "("} |-- Args.const_proper true --| Scan.lift @{keyword ","} --
  112.39 +        Attrib.thm --| Scan.lift @{keyword ")"}) >>
  112.40 +      (fn (tyname, (relname, qthm)) =>
  112.41 +        let val minfo = {relmap = relname, quot_thm = qthm}
  112.42          in Thm.declaration_attribute (fn _ => Quotmaps.map (Symtab.update (tyname, minfo))) end))
  112.43      "declaration of map information"
  112.44  
  112.45  fun print_quotmaps ctxt =
  112.46    let
  112.47 -    fun prt_map (ty_name, {relmap}) =
  112.48 +    fun prt_map (ty_name, {relmap, quot_thm}) =
  112.49        Pretty.block (separate (Pretty.brk 2)
  112.50 -        (map Pretty.str
  112.51 -         ["type:", ty_name,
  112.52 -          "relation map:", relmap]))
  112.53 +         [Pretty.str "type:", 
  112.54 +          Pretty.str ty_name,
  112.55 +          Pretty.str "relation map:", 
  112.56 +          Pretty.str relmap,
  112.57 +          Pretty.str "quot. theorem:", 
  112.58 +          Syntax.pretty_term ctxt (prop_of quot_thm)])
  112.59    in
  112.60      map prt_map (Symtab.dest (Quotmaps.get (Context.Proof ctxt)))
  112.61      |> Pretty.big_list "maps for type constructors:"
  112.62 @@ -125,7 +130,7 @@
  112.63    end
  112.64  
  112.65  (* info about quotient types *)
  112.66 -type quotients = {qtyp: typ, rtyp: typ, equiv_rel: term, equiv_thm: thm}
  112.67 +type quotients = {qtyp: typ, rtyp: typ, equiv_rel: term, equiv_thm: thm, quot_thm: thm}
  112.68  
  112.69  structure Quotients = Generic_Data
  112.70  (
  112.71 @@ -135,11 +140,12 @@
  112.72    fun merge data = Symtab.merge (K true) data
  112.73  )
  112.74  
  112.75 -fun transform_quotients phi {qtyp, rtyp, equiv_rel, equiv_thm} =
  112.76 +fun transform_quotients phi {qtyp, rtyp, equiv_rel, equiv_thm, quot_thm} =
  112.77    {qtyp = Morphism.typ phi qtyp,
  112.78     rtyp = Morphism.typ phi rtyp,
  112.79     equiv_rel = Morphism.term phi equiv_rel,
  112.80 -   equiv_thm = Morphism.thm phi equiv_thm}
  112.81 +   equiv_thm = Morphism.thm phi equiv_thm,
  112.82 +   quot_thm = Morphism.thm phi quot_thm}
  112.83  
  112.84  val lookup_quotients = Symtab.lookup o Quotients.get o Context.Proof
  112.85  val lookup_quotients_global = Symtab.lookup o Quotients.get o Context.Theory
  112.86 @@ -151,7 +157,7 @@
  112.87  
  112.88  fun print_quotients ctxt =
  112.89    let
  112.90 -    fun prt_quot {qtyp, rtyp, equiv_rel, equiv_thm} =
  112.91 +    fun prt_quot {qtyp, rtyp, equiv_rel, equiv_thm, quot_thm} =
  112.92        Pretty.block (separate (Pretty.brk 2)
  112.93         [Pretty.str "quotient type:",
  112.94          Syntax.pretty_typ ctxt qtyp,
  112.95 @@ -160,7 +166,9 @@
  112.96          Pretty.str "relation:",
  112.97          Syntax.pretty_term ctxt equiv_rel,
  112.98          Pretty.str "equiv. thm:",
  112.99 -        Syntax.pretty_term ctxt (prop_of equiv_thm)])
 112.100 +        Syntax.pretty_term ctxt (prop_of equiv_thm),
 112.101 +        Pretty.str "quot. thm:",
 112.102 +        Syntax.pretty_term ctxt (prop_of quot_thm)])
 112.103    in
 112.104      map (prt_quot o snd) (Symtab.dest (Quotients.get (Context.Proof ctxt)))
 112.105      |> Pretty.big_list "quotients:"
   113.1 --- a/src/HOL/Tools/Quotient/quotient_term.ML	Fri Mar 23 20:32:43 2012 +0100
   113.2 +++ b/src/HOL/Tools/Quotient/quotient_term.ML	Mon Mar 26 10:56:56 2012 +0200
   113.3 @@ -20,6 +20,9 @@
   113.4    val equiv_relation: Proof.context -> typ * typ -> term
   113.5    val equiv_relation_chk: Proof.context -> typ * typ -> term
   113.6  
   113.7 +  val get_rel_from_quot_thm: thm -> term
   113.8 +  val prove_quot_theorem: Proof.context -> typ * typ -> thm
   113.9 +
  113.10    val regularize_trm: Proof.context -> term * term -> term
  113.11    val regularize_trm_chk: Proof.context -> term * term -> term
  113.12  
  113.13 @@ -72,9 +75,6 @@
  113.14  
  113.15  fun defined_mapfun_data ctxt s =
  113.16    Symtab.defined (Enriched_Type.entries ctxt) s
  113.17 -  
  113.18 -(* makes a Free out of a TVar *)
  113.19 -fun mk_Free (TVar ((x, i), _)) = Free (unprefix "'" x ^ string_of_int i, dummyT)
  113.20  
  113.21  (* looks up the (varified) rty and qty for
  113.22     a quotient definition
  113.23 @@ -279,35 +279,10 @@
  113.24      SOME map_data => Const (#relmap map_data, dummyT)
  113.25    | NONE => raise LIFT_MATCH ("get_relmap (no relation map function found for type " ^ s ^ ")"))
  113.26  
  113.27 -(* takes two type-environments and looks
  113.28 -   up in both of them the variable v, which
  113.29 -   must be listed in the environment
  113.30 -*)
  113.31 -fun double_lookup rtyenv qtyenv v =
  113.32 -  let
  113.33 -    val v' = fst (dest_TVar v)
  113.34 -  in
  113.35 -    (snd (the (Vartab.lookup rtyenv v')), snd (the (Vartab.lookup qtyenv v')))
  113.36 -  end
  113.37 -
  113.38 -fun mk_relmap ctxt vs rty =
  113.39 -  let
  113.40 -    val vs' = map (mk_Free) vs
  113.41 -
  113.42 -    fun mk_relmap_aux rty =
  113.43 -      case rty of
  113.44 -        TVar _ => mk_Free rty
  113.45 -      | Type (_, []) => HOLogic.eq_const rty
  113.46 -      | Type (s, tys) => list_comb (get_relmap ctxt s, map mk_relmap_aux tys)
  113.47 -      | _ => raise LIFT_MATCH ("mk_relmap (default)")
  113.48 -  in
  113.49 -    fold_rev Term.lambda vs' (mk_relmap_aux rty)
  113.50 -  end
  113.51 -
  113.52  fun get_equiv_rel thy s =
  113.53    (case Quotient_Info.lookup_quotients thy s of
  113.54      SOME qdata => #equiv_rel qdata
  113.55 -  | NONE => raise LIFT_MATCH ("get_quotdata (no quotient found for type " ^ s ^ ")"))
  113.56 +  | NONE => raise LIFT_MATCH ("get_equiv_rel (no quotient found for type " ^ s ^ ")"))
  113.57  
  113.58  fun equiv_match_err ctxt ty_pat ty =
  113.59    let
  113.60 @@ -336,11 +311,10 @@
  113.61            end
  113.62          else
  113.63            let
  113.64 -            val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty ctxt s'
  113.65 -            val rtyenv = match ctxt equiv_match_err rty_pat rty
  113.66 +            val (Type (_, rtys), qty_pat) = get_rty_qty ctxt s'
  113.67              val qtyenv = match ctxt equiv_match_err qty_pat qty
  113.68 -            val args_aux = map (double_lookup rtyenv qtyenv) vs
  113.69 -            val args = map (equiv_relation ctxt) args_aux
  113.70 +            val rtys' = map (Envir.subst_type qtyenv) rtys
  113.71 +            val args = map (equiv_relation ctxt) (tys ~~ rtys')
  113.72              val eqv_rel = get_equiv_rel ctxt s'
  113.73              val eqv_rel' = force_typ ctxt eqv_rel ([rty, rty] ---> @{typ bool})
  113.74            in
  113.75 @@ -348,8 +322,7 @@
  113.76              then eqv_rel'
  113.77              else
  113.78                let
  113.79 -                val rel_map = mk_relmap ctxt vs rty_pat
  113.80 -                val result = list_comb (rel_map, args)
  113.81 +                val result = list_comb (get_relmap ctxt s, args)
  113.82                in
  113.83                  mk_rel_compose (result, eqv_rel')
  113.84                end
  113.85 @@ -361,6 +334,84 @@
  113.86    equiv_relation ctxt (rty, qty)
  113.87    |> Syntax.check_term ctxt
  113.88  
  113.89 +(* generation of the Quotient theorem  *)
  113.90 +
  113.91 +exception CODE_GEN of string
  113.92 +
  113.93 +fun get_quot_thm ctxt s =
  113.94 +  let
  113.95 +    val thy = Proof_Context.theory_of ctxt
  113.96 +  in
  113.97 +    (case Quotient_Info.lookup_quotients ctxt s of
  113.98 +      SOME qdata => Thm.transfer thy (#quot_thm qdata)
  113.99 +    | NONE => raise CODE_GEN ("No quotient type " ^ quote s ^ " found."))
 113.100 +  end
 113.101 +
 113.102 +fun get_rel_quot_thm ctxt s =
 113.103 +   let
 113.104 +    val thy = Proof_Context.theory_of ctxt
 113.105 +  in
 113.106 +    (case Quotient_Info.lookup_quotmaps ctxt s of
 113.107 +      SOME map_data => Thm.transfer thy (#quot_thm map_data)
 113.108 +    | NONE => raise CODE_GEN ("get_relmap (no relation map function found for type " ^ s ^ ")"))
 113.109 +  end
 113.110 +
 113.111 +fun is_id_quot thm = (prop_of thm = prop_of @{thm identity_quotient})
 113.112 +
 113.113 +infix 0 MRSL
 113.114 +
 113.115 +fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm
 113.116 +
 113.117 +exception NOT_IMPL of string
 113.118 +
 113.119 +fun get_rel_from_quot_thm quot_thm = 
 113.120 +  let
 113.121 +    val (_ $ rel $ _ $ _) = (HOLogic.dest_Trueprop o prop_of) quot_thm
 113.122 +  in
 113.123 +    rel
 113.124 +  end
 113.125 +
 113.126 +fun mk_quot_thm_compose (rel_quot_thm, quot_thm) = 
 113.127 +  let
 113.128 +    val quot_thm_rel = get_rel_from_quot_thm quot_thm
 113.129 +  in
 113.130 +    if is_eq quot_thm_rel then [rel_quot_thm, quot_thm] MRSL @{thm OOO_eq_quotient}
 113.131 +    else raise NOT_IMPL "nested quotients: not implemented yet"
 113.132 +  end
 113.133 +
 113.134 +fun prove_quot_theorem ctxt (rty, qty) =
 113.135 +  if rty = qty
 113.136 +  then @{thm identity_quotient}
 113.137 +  else
 113.138 +    case (rty, qty) of
 113.139 +      (Type (s, tys), Type (s', tys')) =>
 113.140 +        if s = s'
 113.141 +        then
 113.142 +          let
 113.143 +            val args = map (prove_quot_theorem ctxt) (tys ~~ tys')
 113.144 +          in
 113.145 +            args MRSL (get_rel_quot_thm ctxt s)
 113.146 +          end
 113.147 +        else
 113.148 +          let
 113.149 +            val (Type (_, rtys), qty_pat) = get_rty_qty ctxt s'
 113.150 +            val qtyenv = match ctxt equiv_match_err qty_pat qty
 113.151 +            val rtys' = map (Envir.subst_type qtyenv) rtys
 113.152 +            val args = map (prove_quot_theorem ctxt) (tys ~~ rtys')
 113.153 +            val quot_thm = get_quot_thm ctxt s'
 113.154 +          in
 113.155 +            if forall is_id_quot args
 113.156 +            then
 113.157 +              quot_thm
 113.158 +            else
 113.159 +              let
 113.160 +                val rel_quot_thm = args MRSL (get_rel_quot_thm ctxt s)
 113.161 +              in
 113.162 +                mk_quot_thm_compose (rel_quot_thm, quot_thm)
 113.163 +             end
 113.164 +          end
 113.165 +    | _ => @{thm identity_quotient}
 113.166 +
 113.167  
 113.168  
 113.169  (*** Regularization ***)
   114.1 --- a/src/HOL/Tools/Quotient/quotient_type.ML	Fri Mar 23 20:32:43 2012 +0100
   114.2 +++ b/src/HOL/Tools/Quotient/quotient_type.ML	Mon Mar 26 10:56:56 2012 +0200
   114.3 @@ -6,6 +6,8 @@
   114.4  
   114.5  signature QUOTIENT_TYPE =
   114.6  sig
   114.7 +  val can_generate_code_cert: thm -> bool
   114.8 +  
   114.9    val add_quotient_type: ((string list * binding * mixfix) * (typ * term * bool) * 
  114.10      ((binding * binding) option)) * thm -> local_theory -> Quotient_Info.quotients * local_theory
  114.11  
  114.12 @@ -25,7 +27,7 @@
  114.13  val mem_def1 = @{lemma "y : Collect S ==> S y" by simp}
  114.14  val mem_def2 = @{lemma "S y ==> y : Collect S" by simp}
  114.15  
  114.16 -(* constructs the term lambda (c::rty => bool). EX (x::rty). c = rel x *)
  114.17 +(* constructs the term {c. EX (x::rty). rel x x \<and> c = Collect (rel x)} *)
  114.18  fun typedef_term rel rty lthy =
  114.19    let
  114.20      val [x, c] =
  114.21 @@ -76,6 +78,44 @@
  114.22      Goal.prove lthy [] [] goal
  114.23        (K (typedef_quot_type_tac equiv_thm typedef_info))
  114.24    end
  114.25 +   
  114.26 +fun can_generate_code_cert quot_thm  =
  114.27 +   case Quotient_Term.get_rel_from_quot_thm quot_thm of
  114.28 +      Const (@{const_name HOL.eq}, _) => true
  114.29 +      | Const (@{const_name invariant}, _) $ _  => true
  114.30 +      | _ => false
  114.31 +
  114.32 +fun define_abs_type quot_thm lthy =
  114.33 +  if can_generate_code_cert quot_thm then
  114.34 +    let
  114.35 +      val abs_type_thm = quot_thm RS @{thm Quotient_abs_rep}
  114.36 +      val add_abstype_attribute = 
  114.37 +          Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abstype thm) I)
  114.38 +        val add_abstype_attrib = Attrib.internal (K add_abstype_attribute);
  114.39 +    in
  114.40 +      lthy
  114.41 +        |> (snd oo Local_Theory.note) ((Binding.empty, [add_abstype_attrib]), [abs_type_thm])
  114.42 +    end
  114.43 +  else
  114.44 +    lthy
  114.45 +
  114.46 +fun init_quotient_infr quot_thm equiv_thm lthy =
  114.47 +  let
  114.48 +    val (_ $ rel $ abs $ rep) = (HOLogic.dest_Trueprop o prop_of) quot_thm
  114.49 +    val (qtyp, rtyp) = (dest_funT o fastype_of) rep
  114.50 +    val qty_full_name = (fst o dest_Type) qtyp
  114.51 +    val quotients = {qtyp = qtyp, rtyp = rtyp, equiv_rel = rel, equiv_thm = equiv_thm, 
  114.52 +      quot_thm = quot_thm }
  114.53 +    fun quot_info phi = Quotient_Info.transform_quotients phi quotients
  114.54 +    val abs_rep = {abs = abs, rep = rep}
  114.55 +    fun abs_rep_info phi = Quotient_Info.transform_abs_rep phi abs_rep
  114.56 +  in
  114.57 +    lthy
  114.58 +      |> Local_Theory.declaration {syntax = false, pervasive = true}
  114.59 +        (fn phi => Quotient_Info.update_quotients qty_full_name (quot_info phi)
  114.60 +          #> Quotient_Info.update_abs_rep qty_full_name (abs_rep_info phi))
  114.61 +      |> define_abs_type quot_thm
  114.62 +  end
  114.63  
  114.64  (* main function for constructing a quotient type *)
  114.65  fun add_quotient_type (((vs, qty_name, mx), (rty, rel, partial), opt_morphs), equiv_thm) lthy =
  114.66 @@ -86,7 +126,7 @@
  114.67        else equiv_thm RS @{thm equivp_implies_part_equivp}
  114.68  
  114.69      (* generates the typedef *)
  114.70 -    val ((qty_full_name, typedef_info), lthy1) =
  114.71 +    val ((_, typedef_info), lthy1) =
  114.72        typedef_make (vs, qty_name, mx, rel, rty) part_equiv lthy
  114.73  
  114.74      (* abs and rep functions from the typedef *)
  114.75 @@ -108,9 +148,9 @@
  114.76          NONE => (Binding.prefix_name "rep_" qty_name, Binding.prefix_name "abs_" qty_name)
  114.77        | SOME morphs => morphs)
  114.78  
  114.79 -    val ((abs_t, (_, abs_def)), lthy2) = lthy1
  114.80 +    val ((_, (_, abs_def)), lthy2) = lthy1
  114.81        |> Local_Theory.define ((abs_name, NoSyn), ((Thm.def_binding abs_name, []), abs_trm))
  114.82 -    val ((rep_t, (_, rep_def)), lthy3) = lthy2
  114.83 +    val ((_, (_, rep_def)), lthy3) = lthy2
  114.84        |> Local_Theory.define ((rep_name, NoSyn), ((Thm.def_binding rep_name, []), rep_trm))
  114.85  
  114.86      (* quot_type theorem *)
  114.87 @@ -126,15 +166,11 @@
  114.88      val equiv_thm_name = Binding.suffix_name "_equivp" qty_name
  114.89  
  114.90      (* storing the quotients *)
  114.91 -    val quotients = {qtyp = Abs_ty, rtyp = rty, equiv_rel = rel, equiv_thm = equiv_thm}
  114.92 -
  114.93 -    fun qinfo phi = Quotient_Info.transform_quotients phi quotients
  114.94 -    fun abs_rep phi = Quotient_Info.transform_abs_rep phi {abs = abs_t, rep = rep_t}
  114.95 +    val quotients = {qtyp = Abs_ty, rtyp = rty, equiv_rel = rel, equiv_thm = equiv_thm, 
  114.96 +      quot_thm = quotient_thm}
  114.97  
  114.98      val lthy4 = lthy3
  114.99 -      |> Local_Theory.declaration {syntax = false, pervasive = true}
 114.100 -        (fn phi => Quotient_Info.update_quotients qty_full_name (qinfo phi)
 114.101 -           #> Quotient_Info.update_abs_rep qty_full_name (abs_rep phi))
 114.102 +      |> init_quotient_infr quotient_thm equiv_thm
 114.103        |> (snd oo Local_Theory.note)
 114.104          ((equiv_thm_name,
 114.105            if partial then [] else [Attrib.internal (K Quotient_Info.equiv_rules_add)]),
 114.106 @@ -264,15 +300,43 @@
 114.107  
 114.108  val partial = Scan.optional (Parse.reserved "partial" -- @{keyword ":"} >> K true) false
 114.109  
 114.110 +val quotspec_parser =
 114.111 +  Parse.and_list1
 114.112 +    ((Parse.type_args -- Parse.binding) --
 114.113 +      (* FIXME Parse.type_args_constrained and standard treatment of sort constraints *)
 114.114 +      Parse.opt_mixfix -- (@{keyword "="} |-- Parse.typ) --
 114.115 +        (@{keyword "/"} |-- (partial -- Parse.term))  --
 114.116 +        Scan.option (@{keyword "morphisms"} |-- Parse.!!! (Parse.binding -- Parse.binding)))
 114.117 +
 114.118  val _ =
 114.119    Outer_Syntax.local_theory_to_proof @{command_spec "quotient_type"}
 114.120      "quotient type definitions (require equivalence proofs)"
 114.121 -    (Parse.and_list1
 114.122 -      ((Parse.type_args -- Parse.binding) --
 114.123 -        (* FIXME Parse.type_args_constrained and standard treatment of sort constraints *)
 114.124 -        Parse.opt_mixfix -- (@{keyword "="} |-- Parse.typ) --
 114.125 -          (@{keyword "/"} |-- (partial -- Parse.term))  --
 114.126 -          Scan.option (@{keyword "morphisms"} |-- Parse.!!! (Parse.binding -- Parse.binding)))
 114.127 -    >> quotient_type_cmd)
 114.128 +      (quotspec_parser >> quotient_type_cmd)
 114.129 +
 114.130 +(* Setup lifting using type_def_thm *)
 114.131 +
 114.132 +exception SETUP_LIFT_TYPE of string
 114.133 +
 114.134 +fun setup_lift_type typedef_thm =
 114.135 +  let
 114.136 +    val typedef_set = (snd o dest_comb o HOLogic.dest_Trueprop o prop_of) typedef_thm
 114.137 +    val (quot_thm, equivp_thm) = (case typedef_set of
 114.138 +      Const ("Orderings.top_class.top", _) => 
 114.139 +        (typedef_thm RS @{thm copy_type_to_Quotient}, 
 114.140 +         typedef_thm RS @{thm copy_type_to_equivp})
 114.141 +      | Const (@{const_name "Collect"}, _) $ Abs (_, _, _ $ Bound 0) => 
 114.142 +        (typedef_thm RS @{thm invariant_type_to_Quotient}, 
 114.143 +         typedef_thm RS @{thm invariant_type_to_part_equivp})
 114.144 +      | _ => raise SETUP_LIFT_TYPE "unsupported typedef theorem")
 114.145 +  in
 114.146 +    init_quotient_infr quot_thm equivp_thm
 114.147 +  end
 114.148 +
 114.149 +fun setup_lift_type_aux xthm lthy = setup_lift_type (singleton (Attrib.eval_thms lthy) xthm) lthy
 114.150 +
 114.151 +val _ = 
 114.152 +  Outer_Syntax.local_theory @{command_spec "setup_lifting"}
 114.153 +    "Setup lifting infrastracture" 
 114.154 +      (Parse_Spec.xthm >> (fn xthm => setup_lift_type_aux xthm))
 114.155  
 114.156  end;
   115.1 --- a/src/HOL/Tools/SMT/smt_normalize.ML	Fri Mar 23 20:32:43 2012 +0100
   115.2 +++ b/src/HOL/Tools/SMT/smt_normalize.ML	Mon Mar 26 10:56:56 2012 +0200
   115.3 @@ -451,7 +451,7 @@
   115.4  
   115.5    val nat_ops = simple_nat_ops @ mult_nat_ops
   115.6  
   115.7 -  val nat_consts = nat_ops @ [@{const number_of (nat)},
   115.8 +  val nat_consts = nat_ops @ [@{const numeral (nat)},
   115.9      @{const zero_class.zero (nat)}, @{const one_class.one (nat)}]
  115.10  
  115.11    val nat_int_coercions = [@{const of_nat (int)}, @{const nat}]
  115.12 @@ -466,7 +466,7 @@
  115.13    val expands = map mk_meta_eq @{lemma
  115.14      "0 = nat 0"
  115.15      "1 = nat 1"
  115.16 -    "(number_of :: int => nat) = (%i. nat (number_of i))"
  115.17 +    "(numeral :: num => nat) = (%i. nat (numeral i))"
  115.18      "op < = (%a b. int a < int b)"
  115.19      "op <= = (%a b. int a <= int b)"
  115.20      "Suc = (%a. nat (int a + 1))"
  115.21 @@ -493,8 +493,7 @@
  115.22      let
  115.23        val eq = SMT_Utils.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
  115.24        val ss = HOL_ss
  115.25 -        addsimps [@{thm Nat_Numeral.int_nat_number_of}]
  115.26 -        addsimps @{thms neg_simps}
  115.27 +        addsimps [@{thm Nat_Numeral.int_numeral}]
  115.28        fun tac _ = Simplifier.simp_tac (Simplifier.context ctxt ss) 1       
  115.29      in Goal.norm_result (Goal.prove_internal [] eq tac) end
  115.30  
  115.31 @@ -507,7 +506,7 @@
  115.32  
  115.33    fun int_conv ctxt ct =
  115.34      (case Thm.term_of ct of
  115.35 -      @{const of_nat (int)} $ (n as (@{const number_of (nat)} $ _)) =>
  115.36 +      @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
  115.37          Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
  115.38      | @{const of_nat (int)} $ _ =>
  115.39          (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
  115.40 @@ -549,23 +548,15 @@
  115.41      rewrite Numeral1 into 1
  115.42    *)
  115.43  
  115.44 -  fun is_strange_number ctxt (t as Const (@{const_name number_of}, _) $ _) =
  115.45 +  fun is_strange_number ctxt (t as Const (@{const_name neg_numeral}, _) $ _) =
  115.46          (case try HOLogic.dest_number t of
  115.47            SOME (_, i) => SMT_Builtin.is_builtin_num ctxt t andalso i < 2
  115.48          | NONE => false)
  115.49      | is_strange_number _ _ = false
  115.50  
  115.51    val pos_num_ss = HOL_ss
  115.52 -    addsimps [@{thm Int.number_of_minus}, @{thm Int.number_of_Min}]
  115.53 -    addsimps [@{thm Int.number_of_Pls}, @{thm Int.numeral_1_eq_1}]
  115.54 -    addsimps @{thms Int.pred_bin_simps}
  115.55 -    addsimps @{thms Int.normalize_bin_simps}
  115.56 -    addsimps @{lemma
  115.57 -      "Int.Min = - Int.Bit1 Int.Pls"
  115.58 -      "Int.Bit0 (- Int.Pls) = - Int.Pls"
  115.59 -      "Int.Bit0 (- k) = - Int.Bit0 k"
  115.60 -      "Int.Bit1 (- k) = - Int.Bit1 (Int.pred k)"
  115.61 -      by simp_all (simp add: pred_def)}
  115.62 +    addsimps [@{thm Num.numeral_One}]
  115.63 +    addsimps [@{thm Num.neg_numeral_def}]
  115.64  
  115.65    fun norm_num_conv ctxt =
  115.66      SMT_Utils.if_conv (is_strange_number ctxt)
   116.1 --- a/src/HOL/Tools/SMT/z3_proof_tools.ML	Fri Mar 23 20:32:43 2012 +0100
   116.2 +++ b/src/HOL/Tools/SMT/z3_proof_tools.ML	Mon Mar 26 10:56:56 2012 +0200
   116.3 @@ -334,14 +334,12 @@
   116.4  
   116.5    val basic_simpset = HOL_ss addsimps @{thms field_simps}
   116.6      addsimps [@{thm times_divide_eq_right}, @{thm times_divide_eq_left}]
   116.7 -    addsimps @{thms arith_special} addsimps @{thms less_bin_simps}
   116.8 -    addsimps @{thms le_bin_simps} addsimps @{thms eq_bin_simps}
   116.9 -    addsimps @{thms add_bin_simps} addsimps @{thms succ_bin_simps}
  116.10 -    addsimps @{thms minus_bin_simps} addsimps @{thms pred_bin_simps}
  116.11 -    addsimps @{thms mult_bin_simps} addsimps @{thms iszero_simps}
  116.12 +    addsimps @{thms arith_special} addsimps @{thms arith_simps}
  116.13 +    addsimps @{thms rel_simps}
  116.14      addsimps @{thms array_rules}
  116.15      addsimps @{thms term_true_def} addsimps @{thms term_false_def}
  116.16      addsimps @{thms z3div_def} addsimps @{thms z3mod_def}
  116.17 +    addsimprocs [@{simproc binary_int_div}, @{simproc binary_int_mod}]
  116.18      addsimprocs [
  116.19        Simplifier.simproc_global @{theory} "fast_int_arith" [
  116.20          "(m::int) < n", "(m::int) <= n", "(m::int) = n"] (K Lin_Arith.simproc),
   117.1 --- a/src/HOL/Tools/arith_data.ML	Fri Mar 23 20:32:43 2012 +0100
   117.2 +++ b/src/HOL/Tools/arith_data.ML	Mon Mar 26 10:56:56 2012 +0200
   117.3 @@ -68,7 +68,8 @@
   117.4  
   117.5  (* some specialized syntactic operations *)
   117.6  
   117.7 -fun mk_number T n = HOLogic.number_of_const T $ HOLogic.mk_numeral n;
   117.8 +fun mk_number T 1 = HOLogic.numeral_const T $ HOLogic.one_const
   117.9 +  | mk_number T n = HOLogic.mk_number T n;
  117.10  
  117.11  val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
  117.12  
   118.1 --- a/src/HOL/Tools/float_syntax.ML	Fri Mar 23 20:32:43 2012 +0100
   118.2 +++ b/src/HOL/Tools/float_syntax.ML	Mon Mar 26 10:56:56 2012 +0200
   118.3 @@ -18,12 +18,15 @@
   118.4  
   118.5  fun mk_number i =
   118.6    let
   118.7 -    fun mk 0 = Syntax.const @{const_syntax Int.Pls}
   118.8 -      | mk ~1 = Syntax.const @{const_syntax Int.Min}
   118.9 +    fun mk 1 = Syntax.const @{const_syntax Num.One}
  118.10        | mk i =
  118.11            let val (q, r) = Integer.div_mod i 2
  118.12            in HOLogic.mk_bit r $ (mk q) end;
  118.13 -  in Syntax.const @{const_syntax Int.number_of} $ mk i end;
  118.14 +  in
  118.15 +    if i = 0 then Syntax.const @{const_syntax Groups.zero}
  118.16 +    else if i > 0 then Syntax.const @{const_syntax Num.numeral} $ mk i
  118.17 +    else Syntax.const @{const_syntax Num.neg_numeral} $ mk (~i)
  118.18 +  end;
  118.19  
  118.20  fun mk_frac str =
  118.21    let
   119.1 --- a/src/HOL/Tools/hologic.ML	Fri Mar 23 20:32:43 2012 +0100
   119.2 +++ b/src/HOL/Tools/hologic.ML	Mon Mar 26 10:56:56 2012 +0200
   119.3 @@ -93,15 +93,15 @@
   119.4    val size_const: typ -> term
   119.5    val code_numeralT: typ
   119.6    val intT: typ
   119.7 -  val pls_const: term
   119.8 -  val min_const: term
   119.9 +  val one_const: term
  119.10    val bit0_const: term
  119.11    val bit1_const: term
  119.12    val mk_bit: int -> term
  119.13    val dest_bit: term -> int
  119.14    val mk_numeral: int -> term
  119.15 -  val dest_numeral: term -> int
  119.16 -  val number_of_const: typ -> term
  119.17 +  val dest_num: term -> int
  119.18 +  val numeral_const: typ -> term
  119.19 +  val neg_numeral_const: typ -> term
  119.20    val add_numerals: term -> (term * typ) list -> (term * typ) list
  119.21    val mk_number: typ -> int -> term
  119.22    val dest_number: term -> typ * int
  119.23 @@ -492,50 +492,54 @@
  119.24  val code_numeralT = Type ("Code_Numeral.code_numeral", []);
  119.25  
  119.26  
  119.27 -(* binary numerals and int -- non-unique representation due to leading zeros/ones! *)
  119.28 +(* binary numerals and int *)
  119.29  
  119.30 +val numT = Type ("Num.num", []);
  119.31  val intT = Type ("Int.int", []);
  119.32  
  119.33 -val pls_const = Const ("Int.Pls", intT)
  119.34 -and min_const = Const ("Int.Min", intT)
  119.35 -and bit0_const = Const ("Int.Bit0", intT --> intT)
  119.36 -and bit1_const = Const ("Int.Bit1", intT --> intT);
  119.37 +val one_const = Const ("Num.num.One", numT)
  119.38 +and bit0_const = Const ("Num.num.Bit0", numT --> numT)
  119.39 +and bit1_const = Const ("Num.num.Bit1", numT --> numT);
  119.40  
  119.41  fun mk_bit 0 = bit0_const
  119.42    | mk_bit 1 = bit1_const
  119.43    | mk_bit _ = raise TERM ("mk_bit", []);
  119.44  
  119.45 -fun dest_bit (Const ("Int.Bit0", _)) = 0
  119.46 -  | dest_bit (Const ("Int.Bit1", _)) = 1
  119.47 +fun dest_bit (Const ("Num.num.Bit0", _)) = 0
  119.48 +  | dest_bit (Const ("Num.num.Bit1", _)) = 1
  119.49    | dest_bit t = raise TERM ("dest_bit", [t]);
  119.50  
  119.51 -fun mk_numeral 0 = pls_const
  119.52 -  | mk_numeral ~1 = min_const
  119.53 -  | mk_numeral i =
  119.54 -      let val (q, r) = Integer.div_mod i 2;
  119.55 -      in mk_bit r $ mk_numeral q end;
  119.56 +fun mk_numeral i =
  119.57 +  let fun mk 1 = one_const
  119.58 +        | mk i = let val (q, r) = Integer.div_mod i 2 in mk_bit r $ mk q end
  119.59 +  in if i > 0 then mk i else raise TERM ("mk_numeral: " ^ string_of_int i, [])
  119.60 +  end
  119.61  
  119.62 -fun dest_numeral (Const ("Int.Pls", _)) = 0
  119.63 -  | dest_numeral (Const ("Int.Min", _)) = ~1
  119.64 -  | dest_numeral (Const ("Int.Bit0", _) $ bs) = 2 * dest_numeral bs
  119.65 -  | dest_numeral (Const ("Int.Bit1", _) $ bs) = 2 * dest_numeral bs + 1
  119.66 -  | dest_numeral t = raise TERM ("dest_numeral", [t]);
  119.67 +fun dest_num (Const ("Num.num.One", _)) = 1
  119.68 +  | dest_num (Const ("Num.num.Bit0", _) $ bs) = 2 * dest_num bs
  119.69 +  | dest_num (Const ("Num.num.Bit1", _) $ bs) = 2 * dest_num bs + 1
  119.70 +  | dest_num t = raise TERM ("dest_num", [t]);
  119.71  
  119.72 -fun number_of_const T = Const ("Int.number_class.number_of", intT --> T);
  119.73 +fun numeral_const T = Const ("Num.numeral_class.numeral", numT --> T);
  119.74 +fun neg_numeral_const T = Const ("Num.neg_numeral_class.neg_numeral", numT --> T);
  119.75  
  119.76 -fun add_numerals (Const ("Int.number_class.number_of", Type (_, [_, T])) $ t) = cons (t, T)
  119.77 +fun add_numerals (Const ("Num.numeral_class.numeral", Type (_, [_, T])) $ t) = cons (t, T)
  119.78    | add_numerals (t $ u) = add_numerals t #> add_numerals u
  119.79    | add_numerals (Abs (_, _, t)) = add_numerals t
  119.80    | add_numerals _ = I;
  119.81  
  119.82  fun mk_number T 0 = Const ("Groups.zero_class.zero", T)
  119.83    | mk_number T 1 = Const ("Groups.one_class.one", T)
  119.84 -  | mk_number T i = number_of_const T $ mk_numeral i;
  119.85 +  | mk_number T i =
  119.86 +    if i > 0 then numeral_const T $ mk_numeral i
  119.87 +    else neg_numeral_const T $ mk_numeral (~ i);
  119.88  
  119.89  fun dest_number (Const ("Groups.zero_class.zero", T)) = (T, 0)
  119.90    | dest_number (Const ("Groups.one_class.one", T)) = (T, 1)
  119.91 -  | dest_number (Const ("Int.number_class.number_of", Type ("fun", [_, T])) $ t) =
  119.92 -      (T, dest_numeral t)
  119.93 +  | dest_number (Const ("Num.numeral_class.numeral", Type ("fun", [_, T])) $ t) =
  119.94 +      (T, dest_num t)
  119.95 +  | dest_number (Const ("Num.neg_numeral_class.neg_numeral", Type ("fun", [_, T])) $ t) =
  119.96 +      (T, ~ (dest_num t))
  119.97    | dest_number t = raise TERM ("dest_number", [t]);
  119.98  
  119.99  
   120.1 --- a/src/HOL/Tools/int_arith.ML	Fri Mar 23 20:32:43 2012 +0100
   120.2 +++ b/src/HOL/Tools/int_arith.ML	Mon Mar 26 10:56:56 2012 +0200
   120.3 @@ -78,7 +78,7 @@
   120.4    proc = sproc, identifier = []}
   120.5  
   120.6  fun number_of thy T n =
   120.7 -  if not (Sign.of_sort thy (T, @{sort number}))
   120.8 +  if not (Sign.of_sort thy (T, @{sort numeral}))
   120.9    then raise CTERM ("number_of", [])
  120.10    else Numeral.mk_cnumber (Thm.ctyp_of thy T) n;
  120.11  
   121.1 --- a/src/HOL/Tools/lin_arith.ML	Fri Mar 23 20:32:43 2012 +0100
   121.2 +++ b/src/HOL/Tools/lin_arith.ML	Mon Mar 26 10:56:56 2012 +0200
   121.3 @@ -174,14 +174,17 @@
   121.4        | (NONE,  m') => apsnd (Rat.mult (Rat.inv m')) (demult (s, m)))
   121.5      (* terms that evaluate to numeric constants *)
   121.6      | demult (Const (@{const_name Groups.uminus}, _) $ t, m) = demult (t, Rat.neg m)
   121.7 -    | demult (Const (@{const_name Groups.zero}, _), m) = (NONE, Rat.zero)
   121.8 +    | demult (Const (@{const_name Groups.zero}, _), _) = (NONE, Rat.zero)
   121.9      | demult (Const (@{const_name Groups.one}, _), m) = (NONE, m)
  121.10 -    (*Warning: in rare cases number_of encloses a non-numeral,
  121.11 -      in which case dest_numeral raises TERM; hence all the handles below.
  121.12 +    (*Warning: in rare cases (neg_)numeral encloses a non-numeral,
  121.13 +      in which case dest_num raises TERM; hence all the handles below.
  121.14        Same for Suc-terms that turn out not to be numerals -
  121.15        although the simplifier should eliminate those anyway ...*)
  121.16 -    | demult (t as Const ("Int.number_class.number_of", _) $ n, m) =
  121.17 -      ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_numeral n)))
  121.18 +    | demult (t as Const ("Num.numeral_class.numeral", _) $ n, m) =
  121.19 +      ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_num n)))
  121.20 +        handle TERM _ => (SOME t, m))
  121.21 +    | demult (t as Const ("Num.neg_numeral_class.neg_numeral", _) $ n, m) =
  121.22 +      ((NONE, Rat.mult m (Rat.rat_of_int (~ (HOLogic.dest_num n))))
  121.23          handle TERM _ => (SOME t, m))
  121.24      | demult (t as Const (@{const_name Suc}, _) $ _, m) =
  121.25        ((NONE, Rat.mult m (Rat.rat_of_int (HOLogic.dest_nat t)))
  121.26 @@ -219,10 +222,13 @@
  121.27          (case demult inj_consts (all, m) of
  121.28             (NONE,   m') => (p, Rat.add i m')
  121.29           | (SOME u, m') => add_atom u m' pi)
  121.30 -    | poly (all as Const ("Int.number_class.number_of", Type(_,[_,T])) $ t, m, pi as (p, i)) =
  121.31 -        (let val k = HOLogic.dest_numeral t
  121.32 -            val k2 = if k < 0 andalso T = HOLogic.natT then 0 else k
  121.33 -        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k2))) end
  121.34 +    | poly (all as Const ("Num.numeral_class.numeral", Type(_,[_,_])) $ t, m, pi as (p, i)) =
  121.35 +        (let val k = HOLogic.dest_num t
  121.36 +        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int k))) end
  121.37 +        handle TERM _ => add_atom all m pi)
  121.38 +    | poly (all as Const ("Num.neg_numeral_class.neg_numeral", Type(_,[_,_])) $ t, m, pi as (p, i)) =
  121.39 +        (let val k = HOLogic.dest_num t
  121.40 +        in (p, Rat.add i (Rat.mult m (Rat.rat_of_int (~ k)))) end
  121.41          handle TERM _ => add_atom all m pi)
  121.42      | poly (all as Const f $ x, m, pi) =
  121.43          if member (op =) inj_consts f then poly (x, m, pi) else add_atom all m pi
  121.44 @@ -464,9 +470,9 @@
  121.45        in
  121.46          SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
  121.47        end
  121.48 -    (* ?P ((?n::nat) mod (number_of ?k)) =
  121.49 -         ((number_of ?k = 0 --> ?P ?n) & (~ (number_of ?k = 0) -->
  121.50 -           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P j))) *)
  121.51 +    (* ?P ((?n::nat) mod (numeral ?k)) =
  121.52 +         ((numeral ?k = 0 --> ?P ?n) & (~ (numeral ?k = 0) -->
  121.53 +           (ALL i j. j < numeral ?k --> ?n = numeral ?k * i + j --> ?P j))) *)
  121.54      | (Const ("Divides.div_class.mod", Type ("fun", [@{typ nat}, _])), [t1, t2]) =>
  121.55        let
  121.56          val rev_terms               = rev terms
  121.57 @@ -496,9 +502,9 @@
  121.58        in
  121.59          SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
  121.60        end
  121.61 -    (* ?P ((?n::nat) div (number_of ?k)) =
  121.62 -         ((number_of ?k = 0 --> ?P 0) & (~ (number_of ?k = 0) -->
  121.63 -           (ALL i j. j < number_of ?k --> ?n = number_of ?k * i + j --> ?P i))) *)
  121.64 +    (* ?P ((?n::nat) div (numeral ?k)) =
  121.65 +         ((numeral ?k = 0 --> ?P 0) & (~ (numeral ?k = 0) -->
  121.66 +           (ALL i j. j < numeral ?k --> ?n = numeral ?k * i + j --> ?P i))) *)
  121.67      | (Const ("Divides.div_class.div", Type ("fun", [@{typ nat}, _])), [t1, t2]) =>
  121.68        let
  121.69          val rev_terms               = rev terms
  121.70 @@ -528,14 +534,14 @@
  121.71        in
  121.72          SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
  121.73        end
  121.74 -    (* ?P ((?n::int) mod (number_of ?k)) =
  121.75 -         ((number_of ?k = 0 --> ?P ?n) &
  121.76 -          (0 < number_of ?k -->
  121.77 +    (* ?P ((?n::int) mod (numeral ?k)) =
  121.78 +         ((numeral ?k = 0 --> ?P ?n) &
  121.79 +          (0 < numeral ?k -->
  121.80              (ALL i j.
  121.81 -              0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P j)) &
  121.82 -          (number_of ?k < 0 -->
  121.83 +              0 <= j & j < numeral ?k & ?n = numeral ?k * i + j --> ?P j)) &
  121.84 +          (numeral ?k < 0 -->
  121.85              (ALL i j.
  121.86 -              number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P j))) *)
  121.87 +              numeral ?k < j & j <= 0 & ?n = numeral ?k * i + j --> ?P j))) *)
  121.88      | (Const ("Divides.div_class.mod",
  121.89          Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
  121.90        let
  121.91 @@ -582,14 +588,14 @@
  121.92        in
  121.93          SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
  121.94        end
  121.95 -    (* ?P ((?n::int) div (number_of ?k)) =
  121.96 -         ((number_of ?k = 0 --> ?P 0) &
  121.97 -          (0 < number_of ?k -->
  121.98 +    (* ?P ((?n::int) div (numeral ?k)) =
  121.99 +         ((numeral ?k = 0 --> ?P 0) &
 121.100 +          (0 < numeral ?k -->
 121.101              (ALL i j.
 121.102 -              0 <= j & j < number_of ?k & ?n = number_of ?k * i + j --> ?P i)) &
 121.103 -          (number_of ?k < 0 -->
 121.104 +              0 <= j & j < numeral ?k & ?n = numeral ?k * i + j --> ?P i)) &
 121.105 +          (numeral ?k < 0 -->
 121.106              (ALL i j.
 121.107 -              number_of ?k < j & j <= 0 & ?n = number_of ?k * i + j --> ?P i))) *)
 121.108 +              numeral ?k < j & j <= 0 & ?n = numeral ?k * i + j --> ?P i))) *)
 121.109      | (Const ("Divides.div_class.div",
 121.110          Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
 121.111        let
   122.1 --- a/src/HOL/Tools/nat_numeral_simprocs.ML	Fri Mar 23 20:32:43 2012 +0100
   122.2 +++ b/src/HOL/Tools/nat_numeral_simprocs.ML	Mon Mar 26 10:56:56 2012 +0200
   122.3 @@ -25,15 +25,16 @@
   122.4  structure Nat_Numeral_Simprocs : NAT_NUMERAL_SIMPROCS =
   122.5  struct
   122.6  
   122.7 -(*Maps n to #n for n = 0, 1, 2*)
   122.8 -val numeral_syms = [@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym, @{thm numeral_2_eq_2} RS sym];
   122.9 +(*Maps n to #n for n = 1, 2*)
  122.10 +val numeral_syms = [@{thm numeral_1_eq_1} RS sym, @{thm numeral_2_eq_2} RS sym];
  122.11  val numeral_sym_ss = HOL_basic_ss addsimps numeral_syms;
  122.12  
  122.13  val rename_numerals = simplify numeral_sym_ss o Thm.transfer @{theory};
  122.14  
  122.15  (*Utilities*)
  122.16  
  122.17 -fun mk_number n = HOLogic.number_of_const HOLogic.natT $ HOLogic.mk_numeral n;
  122.18 +fun mk_number 1 = HOLogic.numeral_const HOLogic.natT $ HOLogic.one_const
  122.19 +  | mk_number n = HOLogic.mk_number HOLogic.natT n;
  122.20  fun dest_number t = Int.max (0, snd (HOLogic.dest_number t));
  122.21  
  122.22  fun find_first_numeral past (t::terms) =
  122.23 @@ -59,14 +60,13 @@
  122.24  (** Other simproc items **)
  122.25  
  122.26  val bin_simps =
  122.27 -     [@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym,
  122.28 -      @{thm add_nat_number_of}, @{thm nat_number_of_add_left}, 
  122.29 -      @{thm diff_nat_number_of}, @{thm le_number_of_eq_not_less},
  122.30 -      @{thm mult_nat_number_of}, @{thm nat_number_of_mult_left}, 
  122.31 -      @{thm less_nat_number_of}, 
  122.32 +     [@{thm numeral_1_eq_1} RS sym,
  122.33 +      @{thm numeral_plus_numeral}, @{thm add_numeral_left},
  122.34 +      @{thm diff_nat_numeral}, @{thm diff_0_eq_0}, @{thm diff_0},
  122.35 +      @{thm numeral_times_numeral}, @{thm mult_numeral_left(1)},
  122.36        @{thm if_True}, @{thm if_False}, @{thm not_False_eq_True},
  122.37 -      @{thm Let_number_of}, @{thm nat_number_of}] @
  122.38 -     @{thms arith_simps} @ @{thms rel_simps} @ @{thms neg_simps};
  122.39 +      @{thm nat_0}, @{thm nat_numeral}, @{thm nat_neg_numeral}] @
  122.40 +     @{thms arith_simps} @ @{thms rel_simps};
  122.41  
  122.42  
  122.43  (*** CancelNumerals simprocs ***)
  122.44 @@ -115,7 +115,7 @@
  122.45        handle TERM _ => (k, t::ts);
  122.46  
  122.47  (*Code for testing whether numerals are already used in the goal*)
  122.48 -fun is_numeral (Const(@{const_name Int.number_of}, _) $ w) = true
  122.49 +fun is_numeral (Const(@{const_name Num.numeral}, _) $ w) = true
  122.50    | is_numeral _ = false;
  122.51  
  122.52  fun prod_has_numeral t = exists is_numeral (dest_prod t);
  122.53 @@ -147,7 +147,7 @@
  122.54  
  122.55  val simplify_meta_eq =
  122.56      Arith_Data.simplify_meta_eq
  122.57 -        ([@{thm nat_numeral_0_eq_0}, @{thm numeral_1_eq_Suc_0}, @{thm Nat.add_0}, @{thm Nat.add_0_right},
  122.58 +        ([@{thm numeral_1_eq_Suc_0}, @{thm Nat.add_0}, @{thm Nat.add_0_right},
  122.59            @{thm mult_0}, @{thm mult_0_right}, @{thm mult_1}, @{thm mult_1_right}] @ contra_rules);
  122.60  
  122.61  
   123.1 --- a/src/HOL/Tools/numeral.ML	Fri Mar 23 20:32:43 2012 +0100
   123.2 +++ b/src/HOL/Tools/numeral.ML	Mon Mar 26 10:56:56 2012 +0200
   123.3 @@ -16,16 +16,20 @@
   123.4  
   123.5  (* numeral *)
   123.6  
   123.7 -fun mk_cbit 0 = @{cterm "Int.Bit0"}
   123.8 -  | mk_cbit 1 = @{cterm "Int.Bit1"}
   123.9 +fun mk_cbit 0 = @{cterm "Num.Bit0"}
  123.10 +  | mk_cbit 1 = @{cterm "Num.Bit1"}
  123.11    | mk_cbit _ = raise CTERM ("mk_cbit", []);
  123.12  
  123.13 -fun mk_cnumeral 0 = @{cterm "Int.Pls"}
  123.14 -  | mk_cnumeral ~1 = @{cterm "Int.Min"}
  123.15 -  | mk_cnumeral i =
  123.16 +fun mk_cnumeral i =
  123.17 +  let
  123.18 +    fun mk 1 = @{cterm "Num.One"}
  123.19 +      | mk i =
  123.20        let val (q, r) = Integer.div_mod i 2 in
  123.21 -        Thm.apply (mk_cbit r) (mk_cnumeral q)
  123.22 -      end;
  123.23 +        Thm.apply (mk_cbit r) (mk q)
  123.24 +      end
  123.25 +  in
  123.26 +    if i > 0 then mk i else raise CTERM ("mk_cnumeral: negative input", [])
  123.27 +  end
  123.28  
  123.29  
  123.30  (* number *)
  123.31 @@ -38,8 +42,11 @@
  123.32  val one = @{cpat "1"};
  123.33  val oneT = Thm.ctyp_of_term one;
  123.34  
  123.35 -val number_of = @{cpat "number_of"};
  123.36 -val numberT = Thm.ctyp_of @{theory} (Term.range_type (Thm.typ_of (Thm.ctyp_of_term number_of)));
  123.37 +val numeral = @{cpat "numeral"};
  123.38 +val numeralT = Thm.ctyp_of @{theory} (Term.range_type (Thm.typ_of (Thm.ctyp_of_term numeral)));
  123.39 +
  123.40 +val neg_numeral = @{cpat "neg_numeral"};
  123.41 +val neg_numeralT = Thm.ctyp_of @{theory} (Term.range_type (Thm.typ_of (Thm.ctyp_of_term neg_numeral)));
  123.42  
  123.43  fun instT T V = Thm.instantiate_cterm ([(V, T)], []);
  123.44  
  123.45 @@ -47,7 +54,9 @@
  123.46  
  123.47  fun mk_cnumber T 0 = instT T zeroT zero
  123.48    | mk_cnumber T 1 = instT T oneT one
  123.49 -  | mk_cnumber T i = Thm.apply (instT T numberT number_of) (mk_cnumeral i);
  123.50 +  | mk_cnumber T i =
  123.51 +    if i > 0 then Thm.apply (instT T numeralT numeral) (mk_cnumeral i)
  123.52 +    else Thm.apply (instT T neg_numeralT neg_numeral) (mk_cnumeral (~i));
  123.53  
  123.54  end;
  123.55  
  123.56 @@ -58,27 +67,23 @@
  123.57  
  123.58  fun add_code number_of negative print target thy =
  123.59    let
  123.60 -    fun dest_numeral pls' min' bit0' bit1' thm =
  123.61 +    fun dest_numeral one' bit0' bit1' thm t =
  123.62        let
  123.63          fun dest_bit (IConst (c, _)) = if c = bit0' then 0
  123.64                else if c = bit1' then 1
  123.65                else Code_Printer.eqn_error thm "Illegal numeral expression: illegal bit"
  123.66            | dest_bit _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal bit";
  123.67 -        fun dest_num (IConst (c, _)) = if c = pls' then SOME 0
  123.68 -              else if c = min' then
  123.69 -                if negative then SOME ~1 else NONE
  123.70 +        fun dest_num (IConst (c, _)) = if c = one' then 1
  123.71                else Code_Printer.eqn_error thm "Illegal numeral expression: illegal leading digit"
  123.72 -          | dest_num (t1 `$ t2) =
  123.73 -              let val (n, b) = (dest_num t2, dest_bit t1)
  123.74 -              in case n of SOME n => SOME (2 * n + b) | NONE => NONE end
  123.75 +          | dest_num (t1 `$ t2) = 2 * dest_num t2 + dest_bit t1
  123.76            | dest_num _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal term";
  123.77 -      in dest_num end;
  123.78 -    fun pretty literals [pls', min', bit0', bit1'] _ thm _ _ [(t, _)] =
  123.79 -      (Code_Printer.str o print literals o the_default 0 o dest_numeral pls' min' bit0' bit1' thm) t;
  123.80 +      in if negative then ~ (dest_num t) else dest_num t end;
  123.81 +    fun pretty literals [one', bit0', bit1'] _ thm _ _ [(t, _)] =
  123.82 +      (Code_Printer.str o print literals o dest_numeral one' bit0' bit1' thm) t;
  123.83    in
  123.84      thy |> Code_Target.add_const_syntax target number_of
  123.85 -      (SOME (Code_Printer.complex_const_syntax (1, ([@{const_name Int.Pls}, @{const_name Int.Min},
  123.86 -        @{const_name Int.Bit0}, @{const_name Int.Bit1}], pretty))))
  123.87 +      (SOME (Code_Printer.complex_const_syntax (1, ([@{const_name Num.One},
  123.88 +        @{const_name Num.Bit0}, @{const_name Num.Bit1}], pretty))))
  123.89    end;
  123.90  
  123.91  end; (*local*)
   124.1 --- a/src/HOL/Tools/numeral_simprocs.ML	Fri Mar 23 20:32:43 2012 +0100
   124.2 +++ b/src/HOL/Tools/numeral_simprocs.ML	Mon Mar 26 10:56:56 2012 +0200
   124.3 @@ -66,6 +66,7 @@
   124.4  (* build product with trailing 1 rather than Numeral 1 in order to avoid the
   124.5     unnecessary restriction to type class number_ring
   124.6     which is not required for cancellation of common factors in divisions.
   124.7 +   UPDATE: this reasoning no longer applies (number_ring is gone)
   124.8  *)
   124.9  fun mk_prod T = 
  124.10    let val one = one_of T
  124.11 @@ -148,22 +149,24 @@
  124.12  
  124.13  (*This resembles Term_Ord.term_ord, but it puts binary numerals before other
  124.14    non-atomic terms.*)
  124.15 -local open Term 
  124.16 -in 
  124.17 -fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
  124.18 -      (case numterm_ord (t, u) of EQUAL => Term_Ord.typ_ord (T, U) | ord => ord)
  124.19 -  | numterm_ord
  124.20 -     (Const(@{const_name Int.number_of}, _) $ v, Const(@{const_name Int.number_of}, _) $ w) =
  124.21 -     num_ord (HOLogic.dest_numeral v, HOLogic.dest_numeral w)
  124.22 -  | numterm_ord (Const(@{const_name Int.number_of}, _) $ _, _) = LESS
  124.23 -  | numterm_ord (_, Const(@{const_name Int.number_of}, _) $ _) = GREATER
  124.24 -  | numterm_ord (t, u) =
  124.25 -      (case int_ord (size_of_term t, size_of_term u) of
  124.26 -        EQUAL =>
  124.27 +local open Term
  124.28 +in
  124.29 +fun numterm_ord (t, u) =
  124.30 +    case (try HOLogic.dest_number t, try HOLogic.dest_number u) of
  124.31 +      (SOME (_, i), SOME (_, j)) => num_ord (i, j)
  124.32 +    | (SOME _, NONE) => LESS
  124.33 +    | (NONE, SOME _) => GREATER
  124.34 +    | _ => (
  124.35 +      case (t, u) of
  124.36 +        (Abs (_, T, t), Abs(_, U, u)) =>
  124.37 +        (prod_ord numterm_ord Term_Ord.typ_ord ((t, T), (u, U)))
  124.38 +      | _ => (
  124.39 +        case int_ord (size_of_term t, size_of_term u) of
  124.40 +          EQUAL =>
  124.41            let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
  124.42 -            (case Term_Ord.hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
  124.43 +            (prod_ord Term_Ord.hd_ord numterms_ord ((f, ts), (g, us)))
  124.44            end
  124.45 -      | ord => ord)
  124.46 +        | ord => ord))
  124.47  and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
  124.48  end;
  124.49  
  124.50 @@ -171,16 +174,16 @@
  124.51  
  124.52  val num_ss = HOL_basic_ss |> Simplifier.set_termless numtermless;
  124.53  
  124.54 -(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic isn't complicated by the abstract 0 and 1.*)
  124.55 -val numeral_syms = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym];
  124.56 +(*Maps 1 to Numeral1 so that arithmetic isn't complicated by the abstract 1.*)
  124.57 +val numeral_syms = [@{thm numeral_1_eq_1} RS sym];
  124.58  
  124.59 -(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
  124.60 +(*Simplify 0+n, n+0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
  124.61  val add_0s =  @{thms add_0s};
  124.62  val mult_1s = @{thms mult_1s mult_1_left mult_1_right divide_1};
  124.63  
  124.64  (* For post-simplification of the rhs of simproc-generated rules *)
  124.65  val post_simps =
  124.66 -    [@{thm numeral_0_eq_0}, @{thm numeral_1_eq_1},
  124.67 +    [@{thm numeral_1_eq_1},
  124.68       @{thm add_0_left}, @{thm add_0_right},
  124.69       @{thm mult_zero_left}, @{thm mult_zero_right},
  124.70       @{thm mult_1_left}, @{thm mult_1_right},
  124.71 @@ -195,18 +198,24 @@
  124.72  
  124.73  (*To perform binary arithmetic.  The "left" rewriting handles patterns
  124.74    created by the Numeral_Simprocs, such as 3 * (5 * x). *)
  124.75 -val simps = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym,
  124.76 -                 @{thm add_number_of_left}, @{thm mult_number_of_left}] @
  124.77 -                @{thms arith_simps} @ @{thms rel_simps};
  124.78 -
  124.79 +val simps =
  124.80 +    [@{thm numeral_1_eq_1} RS sym] @
  124.81 +    @{thms add_numeral_left} @
  124.82 +    @{thms add_neg_numeral_left} @
  124.83 +    @{thms mult_numeral_left} @
  124.84 +    @{thms arith_simps} @ @{thms rel_simps};
  124.85 +    
  124.86  (*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
  124.87    during re-arrangement*)
  124.88  val non_add_simps =
  124.89 -  subtract Thm.eq_thm [@{thm add_number_of_left}, @{thm number_of_add} RS sym] simps;
  124.90 +  subtract Thm.eq_thm
  124.91 +    (@{thms add_numeral_left} @
  124.92 +     @{thms add_neg_numeral_left} @
  124.93 +     @{thms numeral_plus_numeral} @
  124.94 +     @{thms add_neg_numeral_simps}) simps;
  124.95  
  124.96  (*To evaluate binary negations of coefficients*)
  124.97 -val minus_simps = [@{thm numeral_m1_eq_minus_1} RS sym, @{thm number_of_minus} RS sym] @
  124.98 -                   @{thms minus_bin_simps} @ @{thms pred_bin_simps};
  124.99 +val minus_simps = [@{thm minus_zero}, @{thm minus_one}, @{thm minus_numeral}, @{thm minus_neg_numeral}];
 124.100  
 124.101  (*To let us treat subtraction as addition*)
 124.102  val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
 124.103 @@ -365,9 +374,7 @@
 124.104  
 124.105    (* simp_thms are necessary because some of the cancellation rules below
 124.106    (e.g. mult_less_cancel_left) introduce various logical connectives *)
 124.107 -  val numeral_simp_ss = HOL_basic_ss addsimps
 124.108 -    [@{thm eq_number_of_eq}, @{thm less_number_of}, @{thm le_number_of}] @ simps
 124.109 -     @ @{thms simp_thms}
 124.110 +  val numeral_simp_ss = HOL_basic_ss addsimps simps @ @{thms simp_thms}
 124.111    fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
 124.112    val simplify_meta_eq = Arith_Data.simplify_meta_eq
 124.113      ([@{thm Nat.add_0}, @{thm Nat.add_0_right}] @ post_simps)
 124.114 @@ -425,13 +432,16 @@
 124.115  val field_cancel_numeral_factors =
 124.116    map (prep_simproc @{theory})
 124.117     [("field_eq_cancel_numeral_factor",
 124.118 -     ["(l::'a::{field,number_ring}) * m = n",
 124.119 -      "(l::'a::{field,number_ring}) = m * n"],
 124.120 +     ["(l::'a::field) * m = n",
 124.121 +      "(l::'a::field) = m * n"],
 124.122       K EqCancelNumeralFactor.proc),
 124.123      ("field_cancel_numeral_factor",
 124.124 -     ["((l::'a::{field_inverse_zero,number_ring}) * m) / n",
 124.125 -      "(l::'a::{field_inverse_zero,number_ring}) / (m * n)",
 124.126 -      "((number_of v)::'a::{field_inverse_zero,number_ring}) / (number_of w)"],
 124.127 +     ["((l::'a::field_inverse_zero) * m) / n",
 124.128 +      "(l::'a::field_inverse_zero) / (m * n)",
 124.129 +      "((numeral v)::'a::field_inverse_zero) / (numeral w)",
 124.130 +      "((numeral v)::'a::field_inverse_zero) / (neg_numeral w)",
 124.131 +      "((neg_numeral v)::'a::field_inverse_zero) / (numeral w)",
 124.132 +      "((neg_numeral v)::'a::field_inverse_zero) / (neg_numeral w)"],
 124.133       K DivideCancelNumeralFactor.proc)]
 124.134  
 124.135  
 124.136 @@ -678,13 +688,13 @@
 124.137  
 124.138  val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"},
 124.139             @{thm "divide_Numeral1"},
 124.140 -           @{thm "divide_zero"}, @{thm "divide_Numeral0"},
 124.141 +           @{thm "divide_zero"}, @{thm divide_zero_left},
 124.142             @{thm "divide_divide_eq_left"}, 
 124.143             @{thm "times_divide_eq_left"}, @{thm "times_divide_eq_right"},
 124.144             @{thm "times_divide_times_eq"},
 124.145             @{thm "divide_divide_eq_right"},
 124.146             @{thm "diff_minus"}, @{thm "minus_divide_left"},
 124.147 -           @{thm "Numeral1_eq1_nat"}, @{thm "add_divide_distrib"} RS sym,
 124.148 +           @{thm "add_divide_distrib"} RS sym,
 124.149             @{thm field_divide_inverse} RS sym, @{thm inverse_divide}, 
 124.150             Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (Conv.rewr_conv (mk_meta_eq @{thm mult_commute}))))   
 124.151             (@{thm field_divide_inverse} RS sym)]
 124.152 @@ -699,8 +709,7 @@
 124.153        addsimprocs [add_frac_frac_simproc, add_frac_num_simproc, ord_frac_simproc]
 124.154        |> Simplifier.add_cong @{thm "if_weak_cong"})
 124.155    then_conv
 124.156 -  Simplifier.rewrite (HOL_basic_ss addsimps
 124.157 -    [@{thm numeral_1_eq_1},@{thm numeral_0_eq_0}] @ @{thms numerals(1-2)})
 124.158 +  Simplifier.rewrite (HOL_basic_ss addsimps [@{thm numeral_1_eq_1}])
 124.159  
 124.160  end
 124.161  
   125.1 --- a/src/HOL/Tools/numeral_syntax.ML	Fri Mar 23 20:32:43 2012 +0100
   125.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
   125.3 @@ -1,93 +0,0 @@
   125.4 -(*  Title:      HOL/Tools/numeral_syntax.ML
   125.5 -    Authors:    Markus Wenzel, TU Muenchen
   125.6 -
   125.7 -Concrete syntax for generic numerals -- preserves leading zeros/ones.
   125.8 -*)
   125.9 -
  125.10 -signature NUMERAL_SYNTAX =
  125.11 -sig
  125.12 -  val setup: theory -> theory
  125.13 -end;
  125.14 -
  125.15 -structure Numeral_Syntax: NUMERAL_SYNTAX =
  125.16 -struct
  125.17 -
  125.18 -(* parse translation *)
  125.19 -
  125.20 -local
  125.21 -
  125.22 -fun mk_bin num =
  125.23 -  let
  125.24 -    fun bit b bs = HOLogic.mk_bit b $ bs;
  125.25 -    fun mk 0 = Syntax.const @{const_name Int.Pls}
  125.26 -      | mk ~1 = Syntax.const @{const_name Int.Min}
  125.27 -      | mk i = let val (q, r) = Integer.div_mod i 2 in bit r (mk q) end;
  125.28 -  in mk (#value (Lexicon.read_xnum num)) end;
  125.29 -
  125.30 -in
  125.31 -
  125.32 -fun numeral_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] = c $ numeral_tr [t] $ u
  125.33 -  | numeral_tr [t as Const (num, _)] = Syntax.const @{const_syntax Int.number_of} $ mk_bin num
  125.34 -  | numeral_tr ts = raise TERM ("numeral_tr", ts);
  125.35 -
  125.36 -end;
  125.37 -
  125.38 -
  125.39 -(* print translation *)
  125.40 -
  125.41 -local
  125.42 -
  125.43 -fun dest_bin (Const (@{const_syntax Int.Pls}, _)) = []
  125.44 -  | dest_bin (Const (@{const_syntax Int.Min}, _)) = [~1]
  125.45 -  | dest_bin (Const (@{const_syntax Int.Bit0}, _) $ bs) = 0 :: dest_bin bs
  125.46 -  | dest_bin (Const (@{const_syntax Int.Bit1}, _) $ bs) = 1 :: dest_bin bs
  125.47 -  | dest_bin _ = raise Match;
  125.48 -
  125.49 -fun leading _ [] = 0
  125.50 -  | leading (i: int) (j :: js) = if i = j then 1 + leading i js else 0;
  125.51 -
  125.52 -fun int_of [] = 0
  125.53 -  | int_of (b :: bs) = b + 2 * int_of bs;
  125.54 -
  125.55 -fun dest_bin_str tm =
  125.56 -  let
  125.57 -    val rev_digs = dest_bin tm;
  125.58 -    val (sign, z) =
  125.59 -      (case rev rev_digs of
  125.60 -        ~1 :: bs => ("-", leading 1 bs)
  125.61 -      | bs => ("", leading 0 bs));
  125.62 -    val i = int_of rev_digs;
  125.63 -    val num = string_of_int (abs i);
  125.64 -  in
  125.65 -    if (i = 0 orelse i = 1) andalso z = 0 then raise Match
  125.66 -    else sign ^ implode (replicate z "0") ^ num
  125.67 -  end;
  125.68 -
  125.69 -fun syntax_numeral t =
  125.70 -  Syntax.const @{syntax_const "_Numeral"} $
  125.71 -    (Syntax.const @{syntax_const "_numeral"} $ Syntax.free (dest_bin_str t));
  125.72 -
  125.73 -in
  125.74 -
  125.75 -fun numeral_tr' ctxt (Type (@{type_name fun}, [_, T])) (t :: ts) =
  125.76 -      let val t' =
  125.77 -        if not (Config.get ctxt show_types) andalso can Term.dest_Type T then syntax_numeral t
  125.78 -        else
  125.79 -          Syntax.const @{syntax_const "_constrain"} $ syntax_numeral t $
  125.80 -            Syntax_Phases.term_of_typ ctxt T
  125.81 -      in list_comb (t', ts) end
  125.82 -  | numeral_tr' _ T (t :: ts) =
  125.83 -      if T = dummyT then list_comb (syntax_numeral t, ts)
  125.84 -      else raise Match
  125.85 -  | numeral_tr' _ _ _ = raise Match;
  125.86 -
  125.87 -end;
  125.88 -
  125.89 -
  125.90 -(* theory setup *)
  125.91 -
  125.92 -val setup =
  125.93 -  Sign.add_trfuns ([], [(@{syntax_const "_Numeral"}, numeral_tr)], [], []) #>
  125.94 -  Sign.add_advanced_trfunsT [(@{const_syntax Int.number_of}, numeral_tr')];
  125.95 -
  125.96 -end;
   126.1 --- a/src/HOL/Tools/semiring_normalizer.ML	Fri Mar 23 20:32:43 2012 +0100
   126.2 +++ b/src/HOL/Tools/semiring_normalizer.ML	Mon Mar 26 10:56:56 2012 +0200
   126.3 @@ -179,7 +179,7 @@
   126.4        (case Rat.quotient_of_rat x of (i, 1) => i | _ => error "int_of_rat: bad int"),
   126.5      conv = fn phi => fn _ => Simplifier.rewrite (HOL_basic_ss addsimps @{thms semiring_norm})
   126.6        then_conv Simplifier.rewrite (HOL_basic_ss addsimps
   126.7 -        (@{thms numeral_1_eq_1} @ @{thms numeral_0_eq_0} @ @{thms numerals(1-2)}))};
   126.8 +        @{thms numeral_1_eq_1})};
   126.9  
  126.10  fun field_funs key =
  126.11    let
  126.12 @@ -237,13 +237,13 @@
  126.13  val is_numeral = can dest_numeral;
  126.14  
  126.15  val numeral01_conv = Simplifier.rewrite
  126.16 -                         (HOL_basic_ss addsimps [@{thm numeral_1_eq_1}, @{thm numeral_0_eq_0}]);
  126.17 +                         (HOL_basic_ss addsimps [@{thm numeral_1_eq_1}]);
  126.18  val zero1_numeral_conv = 
  126.19 - Simplifier.rewrite (HOL_basic_ss addsimps [@{thm numeral_1_eq_1} RS sym, @{thm numeral_0_eq_0} RS sym]);
  126.20 + Simplifier.rewrite (HOL_basic_ss addsimps [@{thm numeral_1_eq_1} RS sym]);
  126.21  fun zerone_conv cv = zero1_numeral_conv then_conv cv then_conv numeral01_conv;
  126.22 -val natarith = [@{thm "add_nat_number_of"}, @{thm "diff_nat_number_of"},
  126.23 -                @{thm "mult_nat_number_of"}, @{thm "eq_nat_number_of"}, 
  126.24 -                @{thm "less_nat_number_of"}];
  126.25 +val natarith = [@{thm "numeral_plus_numeral"}, @{thm "diff_nat_numeral"},
  126.26 +                @{thm "numeral_times_numeral"}, @{thm "numeral_eq_iff"}, 
  126.27 +                @{thm "numeral_less_iff"}];
  126.28  
  126.29  val nat_add_conv = 
  126.30   zerone_conv 
  126.31 @@ -251,7 +251,7 @@
  126.32      (HOL_basic_ss 
  126.33         addsimps @{thms arith_simps} @ natarith @ @{thms rel_simps}
  126.34               @ [@{thm if_False}, @{thm if_True}, @{thm Nat.add_0}, @{thm add_Suc},
  126.35 -                 @{thm add_number_of_left}, @{thm Suc_eq_plus1}]
  126.36 +                 @{thm add_numeral_left}, @{thm Suc_eq_plus1}]
  126.37               @ map (fn th => th RS sym) @{thms numerals}));
  126.38  
  126.39  val zeron_tm = @{cterm "0::nat"};
   127.1 --- a/src/HOL/Transcendental.thy	Fri Mar 23 20:32:43 2012 +0100
   127.2 +++ b/src/HOL/Transcendental.thy	Mon Mar 26 10:56:56 2012 +0200
   127.3 @@ -2044,8 +2044,8 @@
   127.4    finally show ?thesis by auto
   127.5  qed
   127.6  
   127.7 -lemma tan_periodic_n[simp]: "tan (x + number_of n * pi) = tan x"
   127.8 -  using tan_periodic_int[of _ "number_of n" ] unfolding real_number_of .
   127.9 +lemma tan_periodic_n[simp]: "tan (x + numeral n * pi) = tan x"
  127.10 +  using tan_periodic_int[of _ "numeral n" ] unfolding real_numeral .
  127.11  
  127.12  subsection {* Inverse Trigonometric Functions *}
  127.13  
   128.1 --- a/src/HOL/Unix/Unix.thy	Fri Mar 23 20:32:43 2012 +0100
   128.2 +++ b/src/HOL/Unix/Unix.thy	Mon Mar 26 10:56:56 2012 +0200
   128.3 @@ -843,7 +843,9 @@
   128.4    neither owned nor writable by @{term user\<^isub>1}.
   128.5  *}
   128.6  
   128.7 -definition
   128.8 +
   128.9 +
  128.10 +definition invariant where 
  128.11    "invariant root path =
  128.12      (\<exists>att dir.
  128.13        access root path user\<^isub>1 {} = Some (Env att dir) \<and> dir \<noteq> empty \<and>
   129.1 --- a/src/HOL/Word/Bit_Int.thy	Fri Mar 23 20:32:43 2012 +0100
   129.2 +++ b/src/HOL/Word/Bit_Int.thy	Mon Mar 26 10:56:56 2012 +0200
   129.3 @@ -50,11 +50,13 @@
   129.4    unfolding int_not_def Bit_def by (cases b, simp_all)
   129.5  
   129.6  lemma int_not_simps [simp]:
   129.7 -  "NOT Int.Pls = Int.Min"
   129.8 -  "NOT Int.Min = Int.Pls"
   129.9 -  "NOT (Int.Bit0 w) = Int.Bit1 (NOT w)"
  129.10 -  "NOT (Int.Bit1 w) = Int.Bit0 (NOT w)"
  129.11 -  unfolding int_not_def Pls_def Min_def Bit0_def Bit1_def by simp_all
  129.12 +  "NOT (0::int) = -1"
  129.13 +  "NOT (1::int) = -2"
  129.14 +  "NOT (-1::int) = 0"
  129.15 +  "NOT (numeral w::int) = neg_numeral (w + Num.One)"
  129.16 +  "NOT (neg_numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)"
  129.17 +  "NOT (neg_numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)"
  129.18 +  unfolding int_not_def by simp_all
  129.19  
  129.20  lemma int_not_not [simp]: "NOT (NOT (x::int)) = x"
  129.21    unfolding int_not_def by simp
  129.22 @@ -65,12 +67,6 @@
  129.23  lemma int_and_m1 [simp]: "(-1::int) AND x = x"
  129.24    by (simp add: bitAND_int.simps)
  129.25  
  129.26 -lemma int_and_Pls [simp]: "Int.Pls AND x = Int.Pls"
  129.27 -  unfolding Pls_def by simp
  129.28 -
  129.29 -lemma int_and_Min [simp]: "Int.Min AND x = x"
  129.30 -  unfolding Min_def by simp
  129.31 -
  129.32  lemma Bit_eq_0_iff: "w BIT b = 0 \<longleftrightarrow> w = 0 \<and> b = 0"
  129.33    by (subst BIT_eq_iff [symmetric], simp)
  129.34  
  129.35 @@ -81,17 +77,10 @@
  129.36    "(x BIT b) AND (y BIT c) = (x AND y) BIT (b AND c)" 
  129.37    by (subst bitAND_int.simps, simp add: Bit_eq_0_iff Bit_eq_m1_iff)
  129.38  
  129.39 -lemma int_and_Bits2 [simp]: 
  129.40 -  "(Int.Bit0 x) AND (Int.Bit0 y) = Int.Bit0 (x AND y)"
  129.41 -  "(Int.Bit0 x) AND (Int.Bit1 y) = Int.Bit0 (x AND y)"
  129.42 -  "(Int.Bit1 x) AND (Int.Bit0 y) = Int.Bit0 (x AND y)"
  129.43 -  "(Int.Bit1 x) AND (Int.Bit1 y) = Int.Bit1 (x AND y)"
  129.44 -  unfolding BIT_simps [symmetric] int_and_Bits by simp_all
  129.45 -
  129.46 -lemma int_or_Pls [simp]: "Int.Pls OR x = x"
  129.47 +lemma int_or_zero [simp]: "(0::int) OR x = x"
  129.48    unfolding int_or_def by simp
  129.49  
  129.50 -lemma int_or_Min [simp]: "Int.Min OR x = Int.Min"
  129.51 +lemma int_or_minus1 [simp]: "(-1::int) OR x = -1"
  129.52    unfolding int_or_def by simp
  129.53  
  129.54  lemma bit_or_def: "(b::bit) OR c = NOT (NOT b AND NOT c)"
  129.55 @@ -101,14 +90,7 @@
  129.56    "(x BIT b) OR (y BIT c) = (x OR y) BIT (b OR c)"
  129.57    unfolding int_or_def bit_or_def by simp
  129.58  
  129.59 -lemma int_or_Bits2 [simp]: 
  129.60 -  "(Int.Bit0 x) OR (Int.Bit0 y) = Int.Bit0 (x OR y)"
  129.61 -  "(Int.Bit0 x) OR (Int.Bit1 y) = Int.Bit1 (x OR y)"
  129.62 -  "(Int.Bit1 x) OR (Int.Bit0 y) = Int.Bit1 (x OR y)"
  129.63 -  "(Int.Bit1 x) OR (Int.Bit1 y) = Int.Bit1 (x OR y)"
  129.64 -  unfolding int_or_def by simp_all
  129.65 -
  129.66 -lemma int_xor_Pls [simp]: "Int.Pls XOR x = x"
  129.67 +lemma int_xor_zero [simp]: "(0::int) XOR x = x"
  129.68    unfolding int_xor_def by simp
  129.69  
  129.70  lemma bit_xor_def: "(b::bit) XOR c = (b AND NOT c) OR (NOT b AND c)"
  129.71 @@ -118,13 +100,6 @@
  129.72    "(x BIT b) XOR (y BIT c) = (x XOR y) BIT (b XOR c)"
  129.73    unfolding int_xor_def bit_xor_def by simp
  129.74  
  129.75 -lemma int_xor_Bits2 [simp]: 
  129.76 -  "(Int.Bit0 x) XOR (Int.Bit0 y) = Int.Bit0 (x XOR y)"
  129.77 -  "(Int.Bit0 x) XOR (Int.Bit1 y) = Int.Bit1 (x XOR y)"
  129.78 -  "(Int.Bit1 x) XOR (Int.Bit0 y) = Int.Bit1 (x XOR y)"
  129.79 -  "(Int.Bit1 x) XOR (Int.Bit1 y) = Int.Bit0 (x XOR y)"
  129.80 -  unfolding BIT_simps [symmetric] int_xor_Bits by simp_all
  129.81 -
  129.82  subsubsection {* Binary destructors *}
  129.83  
  129.84  lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
  129.85 @@ -166,22 +141,22 @@
  129.86  
  129.87  subsubsection {* Derived properties *}
  129.88  
  129.89 -lemma int_xor_Min [simp]: "Int.Min XOR x = NOT x"
  129.90 +lemma int_xor_minus1 [simp]: "(-1::int) XOR x = NOT x"
  129.91    by (auto simp add: bin_eq_iff bin_nth_ops)
  129.92  
  129.93  lemma int_xor_extra_simps [simp]:
  129.94 -  "w XOR Int.Pls = w"
  129.95 -  "w XOR Int.Min = NOT w"
  129.96 +  "w XOR (0::int) = w"
  129.97 +  "w XOR (-1::int) = NOT w"
  129.98    by (auto simp add: bin_eq_iff bin_nth_ops)
  129.99  
 129.100  lemma int_or_extra_simps [simp]:
 129.101 -  "w OR Int.Pls = w"
 129.102 -  "w OR Int.Min = Int.Min"
 129.103 +  "w OR (0::int) = w"
 129.104 +  "w OR (-1::int) = -1"
 129.105    by (auto simp add: bin_eq_iff bin_nth_ops)
 129.106  
 129.107  lemma int_and_extra_simps [simp]:
 129.108 -  "w AND Int.Pls = Int.Pls"
 129.109 -  "w AND Int.Min = w"
 129.110 +  "w AND (0::int) = 0"
 129.111 +  "w AND (-1::int) = w"
 129.112    by (auto simp add: bin_eq_iff bin_nth_ops)
 129.113  
 129.114  (* commutativity of the above *)
 129.115 @@ -195,12 +170,12 @@
 129.116  lemma bin_ops_same [simp]:
 129.117    "(x::int) AND x = x" 
 129.118    "(x::int) OR x = x" 
 129.119 -  "(x::int) XOR x = Int.Pls"
 129.120 +  "(x::int) XOR x = 0"
 129.121    by (auto simp add: bin_eq_iff bin_nth_ops)
 129.122  
 129.123  lemmas bin_log_esimps = 
 129.124    int_and_extra_simps  int_or_extra_simps  int_xor_extra_simps
 129.125 -  int_and_Pls int_and_Min  int_or_Pls int_or_Min  int_xor_Pls int_xor_Min
 129.126 +  int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1
 129.127  
 129.128  (* basic properties of logical (bit-wise) operations *)
 129.129  
 129.130 @@ -262,6 +237,106 @@
 129.131  declare bin_ops_comm [simp] bbw_assocs [simp] 
 129.132  *)
 129.133  
 129.134 +subsubsection {* Simplification with numerals *}
 129.135 +
 129.136 +text {* Cases for @{text "0"} and @{text "-1"} are already covered by
 129.137 +  other simp rules. *}
 129.138 +
 129.139 +lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y"
 129.140 +  by (metis bin_rl_simp)
 129.141 +
 129.142 +lemma bin_rest_neg_numeral_BitM [simp]:
 129.143 +  "bin_rest (neg_numeral (Num.BitM w)) = neg_numeral w"
 129.144 +  by (simp only: BIT_bin_simps [symmetric] bin_rest_BIT)
 129.145 +
 129.146 +lemma bin_last_neg_numeral_BitM [simp]:
 129.147 +  "bin_last (neg_numeral (Num.BitM w)) = 1"
 129.148 +  by (simp only: BIT_bin_simps [symmetric] bin_last_BIT)
 129.149 +
 129.150 +text {* FIXME: The rule sets below are very large (24 rules for each
 129.151 +  operator). Is there a simpler way to do this? *}
 129.152 +
 129.153 +lemma int_and_numerals [simp]:
 129.154 +  "numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT 0"
 129.155 +  "numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT 0"
 129.156 +  "numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (numeral x AND numeral y) BIT 0"
 129.157 +  "numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = (numeral x AND numeral y) BIT 1"
 129.158 +  "numeral (Num.Bit0 x) AND neg_numeral (Num.Bit0 y) = (numeral x AND neg_numeral y) BIT 0"
 129.159 +  "numeral (Num.Bit0 x) AND neg_numeral (Num.Bit1 y) = (numeral x AND neg_numeral (y + Num.One)) BIT 0"
 129.160 +  "numeral (Num.Bit1 x) AND neg_numeral (Num.Bit0 y) = (numeral x AND neg_numeral y) BIT 0"
 129.161 +  "numeral (Num.Bit1 x) AND neg_numeral (Num.Bit1 y) = (numeral x AND neg_numeral (y + Num.One)) BIT 1"
 129.162 +  "neg_numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (neg_numeral x AND numeral y) BIT 0"
 129.163 +  "neg_numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (neg_numeral x AND numeral y) BIT 0"
 129.164 +  "neg_numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (neg_numeral (x + Num.One) AND numeral y) BIT 0"
 129.165 +  "neg_numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = (neg_numeral (x + Num.One) AND numeral y) BIT 1"
 129.166 +  "neg_numeral (Num.Bit0 x) AND neg_numeral (Num.Bit0 y) = (neg_numeral x AND neg_numeral y) BIT 0"
 129.167 +  "neg_numeral (Num.Bit0 x) AND neg_numeral (Num.Bit1 y) = (neg_numeral x AND neg_numeral (y + Num.One)) BIT 0"
 129.168 +  "neg_numeral (Num.Bit1 x) AND neg_numeral (Num.Bit0 y) = (neg_numeral (x + Num.One) AND neg_numeral y) BIT 0"
 129.169 +  "neg_numeral (Num.Bit1 x) AND neg_numeral (Num.Bit1 y) = (neg_numeral (x + Num.One) AND neg_numeral (y + Num.One)) BIT 1"
 129.170 +  "(1::int) AND numeral (Num.Bit0 y) = 0"
 129.171 +  "(1::int) AND numeral (Num.Bit1 y) = 1"
 129.172 +  "(1::int) AND neg_numeral (Num.Bit0 y) = 0"
 129.173 +  "(1::int) AND neg_numeral (Num.Bit1 y) = 1"
 129.174 +  "numeral (Num.Bit0 x) AND (1::int) = 0"
 129.175 +  "numeral (Num.Bit1 x) AND (1::int) = 1"
 129.176 +  "neg_numeral (Num.Bit0 x) AND (1::int) = 0"
 129.177 +  "neg_numeral (Num.Bit1 x) AND (1::int) = 1"
 129.178 +  by (rule bin_rl_eqI, simp, simp)+
 129.179 +
 129.180 +lemma int_or_numerals [simp]:
 129.181 +  "numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (numeral x OR numeral y) BIT 0"
 129.182 +  "numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = (numeral x OR numeral y) BIT 1"
 129.183 +  "numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = (numeral x OR numeral y) BIT 1"
 129.184 +  "numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = (numeral x OR numeral y) BIT 1"
 129.185 +  "numeral (Num.Bit0 x) OR neg_numeral (Num.Bit0 y) = (numeral x OR neg_numeral y) BIT 0"
 129.186 +  "numeral (Num.Bit0 x) OR neg_numeral (Num.Bit1 y) = (numeral x OR neg_numeral (y + Num.One)) BIT 1"
 129.187 +  "numeral (Num.Bit1 x) OR neg_numeral (Num.Bit0 y) = (numeral x OR neg_numeral y) BIT 1"
 129.188 +  "numeral (Num.Bit1 x) OR neg_numeral (Num.Bit1 y) = (numeral x OR neg_numeral (y + Num.One)) BIT 1"
 129.189 +  "neg_numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (neg_numeral x OR numeral y) BIT 0"
 129.190 +  "neg_numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = (neg_numeral x OR numeral y) BIT 1"
 129.191 +  "neg_numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = (neg_numeral (x + Num.One) OR numeral y) BIT 1"
 129.192 +  "neg_numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = (neg_numeral (x + Num.One) OR numeral y) BIT 1"
 129.193 +  "neg_numeral (Num.Bit0 x) OR neg_numeral (Num.Bit0 y) = (neg_numeral x OR neg_numeral y) BIT 0"
 129.194 +  "neg_numeral (Num.Bit0 x) OR neg_numeral (Num.Bit1 y) = (neg_numeral x OR neg_numeral (y + Num.One)) BIT 1"
 129.195 +  "neg_numeral (Num.Bit1 x) OR neg_numeral (Num.Bit0 y) = (neg_numeral (x + Num.One) OR neg_numeral y) BIT 1"
 129.196 +  "neg_numeral (Num.Bit1 x) OR neg_numeral (Num.Bit1 y) = (neg_numeral (x + Num.One) OR neg_numeral (y + Num.One)) BIT 1"
 129.197 +  "(1::int) OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)"
 129.198 +  "(1::int) OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)"
 129.199 +  "(1::int) OR neg_numeral (Num.Bit0 y) = neg_numeral (Num.BitM y)"
 129.200 +  "(1::int) OR neg_numeral (Num.Bit1 y) = neg_numeral (Num.Bit1 y)"
 129.201 +  "numeral (Num.Bit0 x) OR (1::int) = numeral (Num.Bit1 x)"
 129.202 +  "numeral (Num.Bit1 x) OR (1::int) = numeral (Num.Bit1 x)"
 129.203 +  "neg_numeral (Num.Bit0 x) OR (1::int) = neg_numeral (Num.BitM x)"
 129.204 +  "neg_numeral (Num.Bit1 x) OR (1::int) = neg_numeral (Num.Bit1 x)"
 129.205 +  by (rule bin_rl_eqI, simp, simp)+
 129.206 +
 129.207 +lemma int_xor_numerals [simp]:
 129.208 +  "numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (numeral x XOR numeral y) BIT 0"
 129.209 +  "numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = (numeral x XOR numeral y) BIT 1"
 129.210 +  "numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = (numeral x XOR numeral y) BIT 1"
 129.211 +  "numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (numeral x XOR numeral y) BIT 0"
 129.212 +  "numeral (Num.Bit0 x) XOR neg_numeral (Num.Bit0 y) = (numeral x XOR neg_numeral y) BIT 0"
 129.213 +  "numeral (Num.Bit0 x) XOR neg_numeral (Num.Bit1 y) = (numeral x XOR neg_numeral (y + Num.One)) BIT 1"
 129.214 +  "numeral (Num.Bit1 x) XOR neg_numeral (Num.Bit0 y) = (numeral x XOR neg_numeral y) BIT 1"
 129.215 +  "numeral (Num.Bit1 x) XOR neg_numeral (Num.Bit1 y) = (numeral x XOR neg_numeral (y + Num.One)) BIT 0"
 129.216 +  "neg_numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (neg_numeral x XOR numeral y) BIT 0"
 129.217 +  "neg_numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = (neg_numeral x XOR numeral y) BIT 1"
 129.218 +  "neg_numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = (neg_numeral (x + Num.One) XOR numeral y) BIT 1"
 129.219 +  "neg_numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (neg_numeral (x + Num.One) XOR numeral y) BIT 0"
 129.220 +  "neg_numeral (Num.Bit0 x) XOR neg_numeral (Num.Bit0 y) = (neg_numeral x XOR neg_numeral y) BIT 0"
 129.221 +  "neg_numeral (Num.Bit0 x) XOR neg_numeral (Num.Bit1 y) = (neg_numeral x XOR neg_numeral (y + Num.One)) BIT 1"
 129.222 +  "neg_numeral (Num.Bit1 x) XOR neg_numeral (Num.Bit0 y) = (neg_numeral (x + Num.One) XOR neg_numeral y) BIT 1"
 129.223 +  "neg_numeral (Num.Bit1 x) XOR neg_numeral (Num.Bit1 y) = (neg_numeral (x + Num.One) XOR neg_numeral (y + Num.One)) BIT 0"
 129.224 +  "(1::int) XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)"
 129.225 +  "(1::int) XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)"
 129.226 +  "(1::int) XOR neg_numeral (Num.Bit0 y) = neg_numeral (Num.BitM y)"
 129.227 +  "(1::int) XOR neg_numeral (Num.Bit1 y) = neg_numeral (Num.Bit0 (y + Num.One))"
 129.228 +  "numeral (Num.Bit0 x) XOR (1::int) = numeral (Num.Bit1 x)"
 129.229 +  "numeral (Num.Bit1 x) XOR (1::int) = numeral (Num.Bit0 x)"
 129.230 +  "neg_numeral (Num.Bit0 x) XOR (1::int) = neg_numeral (Num.BitM x)"
 129.231 +  "neg_numeral (Num.Bit1 x) XOR (1::int) = neg_numeral (Num.Bit0 (x + Num.One))"
 129.232 +  by (rule bin_rl_eqI, simp, simp)+
 129.233 +
 129.234  subsubsection {* Interactions with arithmetic *}
 129.235  
 129.236  lemma plus_and_or [rule_format]:
 129.237 @@ -282,7 +357,6 @@
 129.238    "bin_sign (y::int) = 0 ==> x <= x OR y"
 129.239    apply (induct y arbitrary: x rule: bin_induct)
 129.240      apply clarsimp
 129.241 -   apply (simp only: Min_def)
 129.242     apply clarsimp
 129.243    apply (case_tac x rule: bin_exhaust)
 129.244    apply (case_tac b)
 129.245 @@ -293,13 +367,20 @@
 129.246  lemmas int_and_le =
 129.247    xtr3 [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or]
 129.248  
 129.249 +lemma add_BIT_simps [simp]: (* FIXME: move *)
 129.250 +  "x BIT 0 + y BIT 0 = (x + y) BIT 0"
 129.251 +  "x BIT 0 + y BIT 1 = (x + y) BIT 1"
 129.252 +  "x BIT 1 + y BIT 0 = (x + y) BIT 1"
 129.253 +  "x BIT 1 + y BIT 1 = (x + y + 1) BIT 0"
 129.254 +  by (simp_all add: Bit_B0_2t Bit_B1_2t)
 129.255 +
 129.256  (* interaction between bit-wise and arithmetic *)
 129.257  (* good example of bin_induction *)
 129.258 -lemma bin_add_not: "x + NOT x = Int.Min"
 129.259 +lemma bin_add_not: "x + NOT x = (-1::int)"
 129.260    apply (induct x rule: bin_induct)
 129.261      apply clarsimp
 129.262     apply clarsimp
 129.263 -  apply (case_tac bit, auto simp: BIT_simps)
 129.264 +  apply (case_tac bit, auto)
 129.265    done
 129.266  
 129.267  subsubsection {* Truncating results of bit-wise operations *}
 129.268 @@ -418,8 +499,10 @@
 129.269  lemmas bin_sc_Suc_minus = 
 129.270    trans [OF bin_sc_minus [symmetric] bin_sc.Suc]
 129.271  
 129.272 -lemmas bin_sc_Suc_pred [simp] = 
 129.273 -  bin_sc_Suc_minus [of "number_of bin", simplified nobm1] for bin
 129.274 +lemma bin_sc_numeral [simp]:
 129.275 +  "bin_sc (numeral k) b w =
 129.276 +    bin_sc (numeral k - 1) b (bin_rest w) BIT bin_last w"
 129.277 +  by (subst expand_Suc, rule bin_sc.Suc)
 129.278  
 129.279  
 129.280  subsection {* Splitting and concatenation *}
   130.1 --- a/src/HOL/Word/Bit_Representation.thy	Fri Mar 23 20:32:43 2012 +0100
   130.2 +++ b/src/HOL/Word/Bit_Representation.thy	Mon Mar 26 10:56:56 2012 +0200
   130.3 @@ -47,41 +47,49 @@
   130.4    by (metis bin_rest_BIT bin_last_BIT)
   130.5  
   130.6  lemma BIT_bin_simps [simp]:
   130.7 -  "number_of w BIT 0 = number_of (Int.Bit0 w)"
   130.8 -  "number_of w BIT 1 = number_of (Int.Bit1 w)"
   130.9 -  unfolding Bit_def number_of_is_id numeral_simps by simp_all
  130.10 +  "numeral k BIT 0 = numeral (Num.Bit0 k)"
  130.11 +  "numeral k BIT 1 = numeral (Num.Bit1 k)"
  130.12 +  "neg_numeral k BIT 0 = neg_numeral (Num.Bit0 k)"
  130.13 +  "neg_numeral k BIT 1 = neg_numeral (Num.BitM k)"
  130.14 +  unfolding neg_numeral_def numeral.simps numeral_BitM
  130.15 +  unfolding Bit_def bitval_simps
  130.16 +  by (simp_all del: arith_simps add_numeral_special diff_numeral_special)
  130.17  
  130.18  lemma BIT_special_simps [simp]:
  130.19    shows "0 BIT 0 = 0" and "0 BIT 1 = 1" and "1 BIT 0 = 2" and "1 BIT 1 = 3"
  130.20    unfolding Bit_def by simp_all
  130.21  
  130.22 +lemma BitM_inc: "Num.BitM (Num.inc w) = Num.Bit1 w"
  130.23 +  by (induct w, simp_all)
  130.24 +
  130.25 +lemma expand_BIT:
  130.26 +  "numeral (Num.Bit0 w) = numeral w BIT 0"
  130.27 +  "numeral (Num.Bit1 w) = numeral w BIT 1"
  130.28 +  "neg_numeral (Num.Bit0 w) = neg_numeral w BIT 0"
  130.29 +  "neg_numeral (Num.Bit1 w) = neg_numeral (w + Num.One) BIT 1"
  130.30 +  unfolding add_One by (simp_all add: BitM_inc)
  130.31 +
  130.32  lemma bin_last_numeral_simps [simp]:
  130.33    "bin_last 0 = 0"
  130.34    "bin_last 1 = 1"
  130.35    "bin_last -1 = 1"
  130.36 -  "bin_last (number_of (Int.Bit0 w)) = 0"
  130.37 -  "bin_last (number_of (Int.Bit1 w)) = 1"
  130.38 -  unfolding bin_last_def by simp_all
  130.39 +  "bin_last Numeral1 = 1"
  130.40 +  "bin_last (numeral (Num.Bit0 w)) = 0"
  130.41 +  "bin_last (numeral (Num.Bit1 w)) = 1"
  130.42 +  "bin_last (neg_numeral (Num.Bit0 w)) = 0"
  130.43 +  "bin_last (neg_numeral (Num.Bit1 w)) = 1"
  130.44 +  unfolding expand_BIT bin_last_BIT by (simp_all add: bin_last_def)
  130.45  
  130.46  lemma bin_rest_numeral_simps [simp]:
  130.47    "bin_rest 0 = 0"
  130.48    "bin_rest 1 = 0"
  130.49    "bin_rest -1 = -1"
  130.50 -  "bin_rest (number_of (Int.Bit0 w)) = number_of w"
  130.51 -  "bin_rest (number_of (Int.Bit1 w)) = number_of w"
  130.52 -  unfolding bin_rest_def by simp_all
  130.53 -
  130.54 -lemma BIT_B0_eq_Bit0: "w BIT 0 = Int.Bit0 w"
  130.55 -  unfolding Bit_def Bit0_def by simp
  130.56 -
  130.57 -lemma BIT_B1_eq_Bit1: "w BIT 1 = Int.Bit1 w"
  130.58 -  unfolding Bit_def Bit1_def by simp
  130.59 -
  130.60 -lemmas BIT_simps = BIT_B0_eq_Bit0 BIT_B1_eq_Bit1
  130.61 -
  130.62 -lemma number_of_False_cong: 
  130.63 -  "False \<Longrightarrow> number_of x = number_of y"
  130.64 -  by (rule FalseE)
  130.65 +  "bin_rest Numeral1 = 0"
  130.66 +  "bin_rest (numeral (Num.Bit0 w)) = numeral w"
  130.67 +  "bin_rest (numeral (Num.Bit1 w)) = numeral w"
  130.68 +  "bin_rest (neg_numeral (Num.Bit0 w)) = neg_numeral w"
  130.69 +  "bin_rest (neg_numeral (Num.Bit1 w)) = neg_numeral (w + Num.One)"
  130.70 +  unfolding expand_BIT bin_rest_BIT by (simp_all add: bin_rest_def)
  130.71  
  130.72  lemma less_Bits: 
  130.73    "(v BIT b < w BIT c) = (v < w | v <= w & b = (0::bit) & c = (1::bit))"
  130.74 @@ -121,11 +129,7 @@
  130.75    done
  130.76  
  130.77  lemma bin_ex_rl: "EX w b. w BIT b = bin"
  130.78 -  apply (unfold Bit_def)
  130.79 -  apply (cases "even bin")
  130.80 -   apply (clarsimp simp: even_equiv_def)
  130.81 -   apply (auto simp: odd_equiv_def bitval_def split: bit.split)
  130.82 -  done
  130.83 +  by (metis bin_rl_simp)
  130.84  
  130.85  lemma bin_exhaust:
  130.86    assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q"
  130.87 @@ -144,18 +148,18 @@
  130.88    | Suc: "bin_nth w (Suc n) = bin_nth (bin_rest w) n"
  130.89  
  130.90  lemma bin_abs_lem:
  130.91 -  "bin = (w BIT b) ==> ~ bin = Int.Min --> ~ bin = Int.Pls -->
  130.92 +  "bin = (w BIT b) ==> bin ~= -1 --> bin ~= 0 -->
  130.93      nat (abs w) < nat (abs bin)"
  130.94    apply clarsimp
  130.95 -  apply (unfold Pls_def Min_def Bit_def)
  130.96 +  apply (unfold Bit_def)
  130.97    apply (cases b)
  130.98     apply (clarsimp, arith)
  130.99    apply (clarsimp, arith)
 130.100    done
 130.101  
 130.102  lemma bin_induct:
 130.103 -  assumes PPls: "P Int.Pls"
 130.104 -    and PMin: "P Int.Min"
 130.105 +  assumes PPls: "P 0"
 130.106 +    and PMin: "P -1"
 130.107      and PBit: "!!bin bit. P bin ==> P (bin BIT bit)"
 130.108    shows "P bin"
 130.109    apply (rule_tac P=P and a=bin and f1="nat o abs" 
 130.110 @@ -166,54 +170,22 @@
 130.111    apply (auto simp add : PPls PMin PBit)
 130.112    done
 130.113  
 130.114 -lemma numeral_induct:
 130.115 -  assumes Pls: "P Int.Pls"
 130.116 -  assumes Min: "P Int.Min"
 130.117 -  assumes Bit0: "\<And>w. \<lbrakk>P w; w \<noteq> Int.Pls\<rbrakk> \<Longrightarrow> P (Int.Bit0 w)"
 130.118 -  assumes Bit1: "\<And>w. \<lbrakk>P w; w \<noteq> Int.Min\<rbrakk> \<Longrightarrow> P (Int.Bit1 w)"
 130.119 -  shows "P x"
 130.120 -  apply (induct x rule: bin_induct)
 130.121 -    apply (rule Pls)
 130.122 -   apply (rule Min)
 130.123 -  apply (case_tac bit)
 130.124 -   apply (case_tac "bin = Int.Pls")
 130.125 -    apply (simp add: BIT_simps)
 130.126 -   apply (simp add: Bit0 BIT_simps)
 130.127 -  apply (case_tac "bin = Int.Min")
 130.128 -   apply (simp add: BIT_simps)
 130.129 -  apply (simp add: Bit1 BIT_simps)
 130.130 -  done
 130.131 -
 130.132 -lemma bin_rest_simps [simp]: 
 130.133 -  "bin_rest Int.Pls = Int.Pls"
 130.134 -  "bin_rest Int.Min = Int.Min"
 130.135 -  "bin_rest (Int.Bit0 w) = w"
 130.136 -  "bin_rest (Int.Bit1 w) = w"
 130.137 -  unfolding numeral_simps by (auto simp: bin_rest_def)
 130.138 -
 130.139 -lemma bin_last_simps [simp]: 
 130.140 -  "bin_last Int.Pls = (0::bit)"
 130.141 -  "bin_last Int.Min = (1::bit)"
 130.142 -  "bin_last (Int.Bit0 w) = (0::bit)"
 130.143 -  "bin_last (Int.Bit1 w) = (1::bit)"
 130.144 -  unfolding numeral_simps by (auto simp: bin_last_def z1pmod2)
 130.145 -
 130.146  lemma Bit_div2 [simp]: "(w BIT b) div 2 = w"
 130.147    unfolding bin_rest_def [symmetric] by (rule bin_rest_BIT)
 130.148  
 130.149  lemma bin_nth_lem [rule_format]:
 130.150    "ALL y. bin_nth x = bin_nth y --> x = y"
 130.151 -  apply (induct x rule: bin_induct [unfolded Pls_def Min_def])
 130.152 +  apply (induct x rule: bin_induct)
 130.153      apply safe
 130.154      apply (erule rev_mp)
 130.155 -    apply (induct_tac y rule: bin_induct [unfolded Pls_def Min_def])
 130.156 +    apply (induct_tac y rule: bin_induct)
 130.157        apply safe
 130.158        apply (drule_tac x=0 in fun_cong, force)
 130.159       apply (erule notE, rule ext, 
 130.160              drule_tac x="Suc x" in fun_cong, force)
 130.161      apply (drule_tac x=0 in fun_cong, force)
 130.162     apply (erule rev_mp)
 130.163 -   apply (induct_tac y rule: bin_induct [unfolded Pls_def Min_def])
 130.164 +   apply (induct_tac y rule: bin_induct)
 130.165       apply safe
 130.166       apply (drule_tac x=0 in fun_cong, force)
 130.167      apply (erule notE, rule ext, 
 130.168 @@ -244,15 +216,9 @@
 130.169  lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0"
 130.170    by (cases n) simp_all
 130.171  
 130.172 -lemma bin_nth_Pls [simp]: "~ bin_nth Int.Pls n"
 130.173 -  by (induct n) auto (* FIXME: delete *)
 130.174 -
 130.175  lemma bin_nth_minus1 [simp]: "bin_nth -1 n"
 130.176    by (induct n) auto
 130.177  
 130.178 -lemma bin_nth_Min [simp]: "bin_nth Int.Min n"
 130.179 -  by (induct n) auto (* FIXME: delete *)
 130.180 -
 130.181  lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = (1::bit))"
 130.182    by auto
 130.183  
 130.184 @@ -262,20 +228,20 @@
 130.185  lemma bin_nth_minus [simp]: "0 < n ==> bin_nth (w BIT b) n = bin_nth w (n - 1)"
 130.186    by (cases n) auto
 130.187  
 130.188 -lemma bin_nth_minus_Bit0 [simp]:
 130.189 -  "0 < n ==> bin_nth (number_of (Int.Bit0 w)) n = bin_nth (number_of w) (n - 1)"
 130.190 -  using bin_nth_minus [where w="number_of w" and b="(0::bit)"] by simp
 130.191 +lemma bin_nth_numeral:
 130.192 +  "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (numeral n - 1)"
 130.193 +  by (subst expand_Suc, simp only: bin_nth.simps)
 130.194  
 130.195 -lemma bin_nth_minus_Bit1 [simp]:
 130.196 -  "0 < n ==> bin_nth (number_of (Int.Bit1 w)) n = bin_nth (number_of w) (n - 1)"
 130.197 -  using bin_nth_minus [where w="number_of w" and b="(1::bit)"] by simp
 130.198 -
 130.199 -lemmas bin_nth_0 = bin_nth.simps(1)
 130.200 -lemmas bin_nth_Suc = bin_nth.simps(2)
 130.201 +lemmas bin_nth_numeral_simps [simp] =
 130.202 +  bin_nth_numeral [OF bin_rest_numeral_simps(2)]
 130.203 +  bin_nth_numeral [OF bin_rest_numeral_simps(5)]
 130.204 +  bin_nth_numeral [OF bin_rest_numeral_simps(6)]
 130.205 +  bin_nth_numeral [OF bin_rest_numeral_simps(7)]
 130.206 +  bin_nth_numeral [OF bin_rest_numeral_simps(8)]
 130.207  
 130.208  lemmas bin_nth_simps = 
 130.209 -  bin_nth_0 bin_nth_Suc bin_nth_zero bin_nth_minus1 bin_nth_minus
 130.210 -  bin_nth_minus_Bit0 bin_nth_minus_Bit1
 130.211 +  bin_nth.Z bin_nth.Suc bin_nth_zero bin_nth_minus1
 130.212 +  bin_nth_numeral_simps
 130.213  
 130.214  
 130.215  subsection {* Truncating binary integers *}
 130.216 @@ -286,9 +252,8 @@
 130.217  lemma bin_sign_simps [simp]:
 130.218    "bin_sign 0 = 0"
 130.219    "bin_sign 1 = 0"
 130.220 -  "bin_sign -1 = -1"
 130.221 -  "bin_sign (number_of (Int.Bit0 w)) = bin_sign (number_of w)"
 130.222 -  "bin_sign (number_of (Int.Bit1 w)) = bin_sign (number_of w)"
 130.223 +  "bin_sign (numeral k) = 0"
 130.224 +  "bin_sign (neg_numeral k) = -1"
 130.225    "bin_sign (w BIT b) = bin_sign w"
 130.226    unfolding bin_sign_def Bit_def bitval_def
 130.227    by (simp_all split: bit.split)
 130.228 @@ -309,17 +274,15 @@
 130.229    by (induct n arbitrary: w) auto
 130.230  
 130.231  lemma bintrunc_mod2p: "bintrunc n w = (w mod 2 ^ n)"
 130.232 -  apply (induct n arbitrary: w)
 130.233 -  apply simp
 130.234 +  apply (induct n arbitrary: w, clarsimp)
 130.235    apply (simp add: bin_last_def bin_rest_def Bit_def zmod_zmult2_eq)
 130.236    done
 130.237  
 130.238  lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ (Suc n) - 2 ^ n"
 130.239    apply (induct n arbitrary: w)
 130.240 -   apply clarsimp
 130.241 +   apply simp
 130.242     apply (subst mod_add_left_eq)
 130.243     apply (simp add: bin_last_def)
 130.244 -  apply simp
 130.245    apply (simp add: bin_last_def bin_rest_def Bit_def)
 130.246    apply (clarsimp simp: mod_mult_mult1 [symmetric] 
 130.247           zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]])
 130.248 @@ -342,20 +305,32 @@
 130.249  lemma bintrunc_Suc_numeral:
 130.250    "bintrunc (Suc n) 1 = 1"
 130.251    "bintrunc (Suc n) -1 = bintrunc n -1 BIT 1"
 130.252 -  "bintrunc (Suc n) (number_of (Int.Bit0 w)) = bintrunc n (number_of w) BIT 0"
 130.253 -  "bintrunc (Suc n) (number_of (Int.Bit1 w)) = bintrunc n (number_of w) BIT 1"
 130.254 +  "bintrunc (Suc n) (numeral (Num.Bit0 w)) = bintrunc n (numeral w) BIT 0"
 130.255 +  "bintrunc (Suc n) (numeral (Num.Bit1 w)) = bintrunc n (numeral w) BIT 1"
 130.256 +  "bintrunc (Suc n) (neg_numeral (Num.Bit0 w)) =
 130.257 +    bintrunc n (neg_numeral w) BIT 0"
 130.258 +  "bintrunc (Suc n) (neg_numeral (Num.Bit1 w)) =
 130.259 +    bintrunc n (neg_numeral (w + Num.One)) BIT 1"
 130.260    by simp_all
 130.261  
 130.262  lemma sbintrunc_0_numeral [simp]:
 130.263    "sbintrunc 0 1 = -1"
 130.264 -  "sbintrunc 0 (number_of (Int.Bit0 w)) = 0"
 130.265 -  "sbintrunc 0 (number_of (Int.Bit1 w)) = -1"
 130.266 +  "sbintrunc 0 (numeral (Num.Bit0 w)) = 0"
 130.267 +  "sbintrunc 0 (numeral (Num.Bit1 w)) = -1"
 130.268 +  "sbintrunc 0 (neg_numeral (Num.Bit0 w)) = 0"
 130.269 +  "sbintrunc 0 (neg_numeral (Num.Bit1 w)) = -1"
 130.270    by simp_all
 130.271  
 130.272  lemma sbintrunc_Suc_numeral:
 130.273    "sbintrunc (Suc n) 1 = 1"
 130.274 -  "sbintrunc (Suc n) (number_of (Int.Bit0 w)) = sbintrunc n (number_of w) BIT 0"
 130.275 -  "sbintrunc (Suc n) (number_of (Int.Bit1 w)) = sbintrunc n (number_of w) BIT 1"
 130.276 +  "sbintrunc (Suc n) (numeral (Num.Bit0 w)) =
 130.277 +    sbintrunc n (numeral w) BIT 0"
 130.278 +  "sbintrunc (Suc n) (numeral (Num.Bit1 w)) =
 130.279 +    sbintrunc n (numeral w) BIT 1"
 130.280 +  "sbintrunc (Suc n) (neg_numeral (Num.Bit0 w)) =
 130.281 +    sbintrunc n (neg_numeral w) BIT 0"
 130.282 +  "sbintrunc (Suc n) (neg_numeral (Num.Bit1 w)) =
 130.283 +    sbintrunc n (neg_numeral (w + Num.One)) BIT 1"
 130.284    by simp_all
 130.285  
 130.286  lemma bit_bool:
 130.287 @@ -366,7 +341,7 @@
 130.288  
 130.289  lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bin_nth bin n"
 130.290    apply (induct n arbitrary: bin)
 130.291 -   apply (case_tac bin rule: bin_exhaust, case_tac b, auto)+
 130.292 +  apply (case_tac bin rule: bin_exhaust, case_tac b, auto)
 130.293    done
 130.294  
 130.295  lemma nth_bintr: "bin_nth (bintrunc m w) n = (n < m & bin_nth w n)"
 130.296 @@ -388,14 +363,14 @@
 130.297    by (cases n) auto
 130.298  
 130.299  lemma bin_nth_Bit0:
 130.300 -  "bin_nth (number_of (Int.Bit0 w)) n \<longleftrightarrow>
 130.301 -    (\<exists>m. n = Suc m \<and> bin_nth (number_of w) m)"
 130.302 -  using bin_nth_Bit [where w="number_of w" and b="(0::bit)"] by simp
 130.303 +  "bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow>
 130.304 +    (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
 130.305 +  using bin_nth_Bit [where w="numeral w" and b="(0::bit)"] by simp
 130.306  
 130.307  lemma bin_nth_Bit1:
 130.308 -  "bin_nth (number_of (Int.Bit1 w)) n \<longleftrightarrow>
 130.309 -    n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (number_of w) m)"
 130.310 -  using bin_nth_Bit [where w="number_of w" and b="(1::bit)"] by simp
 130.311 +  "bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow>
 130.312 +    n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
 130.313 +  using bin_nth_Bit [where w="numeral w" and b="(1::bit)"] by simp
 130.314  
 130.315  lemma bintrunc_bintrunc_l:
 130.316    "n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)"
 130.317 @@ -422,72 +397,47 @@
 130.318    done
 130.319  
 130.320  lemmas bintrunc_Pls = 
 130.321 -  bintrunc.Suc [where bin="Int.Pls", simplified bin_last_simps bin_rest_simps]
 130.322 +  bintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
 130.323  
 130.324  lemmas bintrunc_Min [simp] = 
 130.325 -  bintrunc.Suc [where bin="Int.Min", simplified bin_last_simps bin_rest_simps]
 130.326 +  bintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
 130.327  
 130.328  lemmas bintrunc_BIT  [simp] = 
 130.329    bintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b
 130.330  
 130.331 -lemma bintrunc_Bit0 [simp]:
 130.332 -  "bintrunc (Suc n) (Int.Bit0 w) = Int.Bit0 (bintrunc n w)"
 130.333 -  using bintrunc_BIT [where b="(0::bit)"] by (simp add: BIT_simps)
 130.334 -
 130.335 -lemma bintrunc_Bit1 [simp]:
 130.336 -  "bintrunc (Suc n) (Int.Bit1 w) = Int.Bit1 (bintrunc n w)"
 130.337 -  using bintrunc_BIT [where b="(1::bit)"] by (simp add: BIT_simps)
 130.338 -
 130.339  lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT
 130.340 -  bintrunc_Bit0 bintrunc_Bit1
 130.341    bintrunc_Suc_numeral
 130.342  
 130.343  lemmas sbintrunc_Suc_Pls = 
 130.344 -  sbintrunc.Suc [where bin="Int.Pls", simplified bin_last_simps bin_rest_simps]
 130.345 +  sbintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
 130.346  
 130.347  lemmas sbintrunc_Suc_Min = 
 130.348 -  sbintrunc.Suc [where bin="Int.Min", simplified bin_last_simps bin_rest_simps]
 130.349 +  sbintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
 130.350  
 130.351  lemmas sbintrunc_Suc_BIT [simp] = 
 130.352    sbintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b
 130.353  
 130.354 -lemma sbintrunc_Suc_Bit0 [simp]:
 130.355 -  "sbintrunc (Suc n) (Int.Bit0 w) = Int.Bit0 (sbintrunc n w)"
 130.356 -  using sbintrunc_Suc_BIT [where b="(0::bit)"] by (simp add: BIT_simps)
 130.357 -
 130.358 -lemma sbintrunc_Suc_Bit1 [simp]:
 130.359 -  "sbintrunc (Suc n) (Int.Bit1 w) = Int.Bit1 (sbintrunc n w)"
 130.360 -  using sbintrunc_Suc_BIT [where b="(1::bit)"] by (simp add: BIT_simps)
 130.361 -
 130.362  lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT
 130.363 -  sbintrunc_Suc_Bit0 sbintrunc_Suc_Bit1
 130.364    sbintrunc_Suc_numeral
 130.365  
 130.366  lemmas sbintrunc_Pls = 
 130.367 -  sbintrunc.Z [where bin="Int.Pls", 
 130.368 -               simplified bin_last_simps bin_rest_simps bit.simps]
 130.369 +  sbintrunc.Z [where bin="0", 
 130.370 +               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps]
 130.371  
 130.372  lemmas sbintrunc_Min = 
 130.373 -  sbintrunc.Z [where bin="Int.Min", 
 130.374 -               simplified bin_last_simps bin_rest_simps bit.simps]
 130.375 +  sbintrunc.Z [where bin="-1",
 130.376 +               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps]
 130.377  
 130.378  lemmas sbintrunc_0_BIT_B0 [simp] = 
 130.379    sbintrunc.Z [where bin="w BIT (0::bit)", 
 130.380 -               simplified bin_last_simps bin_rest_simps bit.simps] for w
 130.381 +               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps] for w
 130.382  
 130.383  lemmas sbintrunc_0_BIT_B1 [simp] = 
 130.384    sbintrunc.Z [where bin="w BIT (1::bit)", 
 130.385 -               simplified bin_last_simps bin_rest_simps bit.simps] for w
 130.386 -
 130.387 -lemma sbintrunc_0_Bit0 [simp]: "sbintrunc 0 (Int.Bit0 w) = 0"
 130.388 -  using sbintrunc_0_BIT_B0 by simp
 130.389 -
 130.390 -lemma sbintrunc_0_Bit1 [simp]: "sbintrunc 0 (Int.Bit1 w) = -1"
 130.391 -  using sbintrunc_0_BIT_B1 by simp
 130.392 +               simplified bin_last_BIT bin_rest_numeral_simps bit.simps] for w
 130.393  
 130.394  lemmas sbintrunc_0_simps =
 130.395    sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1
 130.396 -  sbintrunc_0_Bit0 sbintrunc_0_Bit1
 130.397  
 130.398  lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs
 130.399  lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
 130.400 @@ -505,15 +455,6 @@
 130.401  lemmas sbintrunc_minus_simps = 
 130.402    sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]]
 130.403  
 130.404 -lemma bintrunc_n_Pls [simp]:
 130.405 -  "bintrunc n Int.Pls = Int.Pls"
 130.406 -  unfolding Pls_def by simp
 130.407 -
 130.408 -lemma sbintrunc_n_PM [simp]:
 130.409 -  "sbintrunc n Int.Pls = Int.Pls"
 130.410 -  "sbintrunc n Int.Min = Int.Min"
 130.411 -  unfolding Pls_def Min_def by simp_all
 130.412 -
 130.413  lemmas thobini1 = arg_cong [where f = "%w. w BIT b"] for b
 130.414  
 130.415  lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1]
 130.416 @@ -600,15 +541,39 @@
 130.417  lemmas nat_non0_gr = 
 130.418    trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl]
 130.419  
 130.420 -lemmas bintrunc_pred_simps [simp] = 
 130.421 -  bintrunc_minus_simps [of "number_of bin", simplified nobm1] for bin
 130.422 +lemma bintrunc_numeral:
 130.423 +  "bintrunc (numeral k) x =
 130.424 +    bintrunc (numeral k - 1) (bin_rest x) BIT bin_last x"
 130.425 +  by (subst expand_Suc, rule bintrunc.simps)
 130.426  
 130.427 -lemmas sbintrunc_pred_simps [simp] = 
 130.428 -  sbintrunc_minus_simps [of "number_of bin", simplified nobm1] for bin
 130.429 +lemma sbintrunc_numeral:
 130.430 +  "sbintrunc (numeral k) x =
 130.431 +    sbintrunc (numeral k - 1) (bin_rest x) BIT bin_last x"
 130.432 +  by (subst expand_Suc, rule sbintrunc.simps)
 130.433  
 130.434 -lemma no_bintr_alt:
 130.435 -  "number_of (bintrunc n w) = w mod 2 ^ n"
 130.436 -  by (simp add: number_of_eq bintrunc_mod2p)
 130.437 +lemma bintrunc_numeral_simps [simp]:
 130.438 +  "bintrunc (numeral k) (numeral (Num.Bit0 w)) =
 130.439 +    bintrunc (numeral k - 1) (numeral w) BIT 0"
 130.440 +  "bintrunc (numeral k) (numeral (Num.Bit1 w)) =
 130.441 +    bintrunc (numeral k - 1) (numeral w) BIT 1"
 130.442 +  "bintrunc (numeral k) (neg_numeral (Num.Bit0 w)) =
 130.443 +    bintrunc (numeral k - 1) (neg_numeral w) BIT 0"
 130.444 +  "bintrunc (numeral k) (neg_numeral (Num.Bit1 w)) =
 130.445 +    bintrunc (numeral k - 1) (neg_numeral (w + Num.One)) BIT 1"
 130.446 +  "bintrunc (numeral k) 1 = 1"
 130.447 +  by (simp_all add: bintrunc_numeral)
 130.448 +
 130.449 +lemma sbintrunc_numeral_simps [simp]:
 130.450 +  "sbintrunc (numeral k) (numeral (Num.Bit0 w)) =
 130.451 +    sbintrunc (numeral k - 1) (numeral w) BIT 0"
 130.452 +  "sbintrunc (numeral k) (numeral (Num.Bit1 w)) =
 130.453 +    sbintrunc (numeral k - 1) (numeral w) BIT 1"
 130.454 +  "sbintrunc (numeral k) (neg_numeral (Num.Bit0 w)) =
 130.455 +    sbintrunc (numeral k - 1) (neg_numeral w) BIT 0"
 130.456 +  "sbintrunc (numeral k) (neg_numeral (Num.Bit1 w)) =
 130.457 +    sbintrunc (numeral k - 1) (neg_numeral (w + Num.One)) BIT 1"
 130.458 +  "sbintrunc (numeral k) 1 = 1"
 130.459 +  by (simp_all add: sbintrunc_numeral)
 130.460  
 130.461  lemma no_bintr_alt1: "bintrunc n = (%w. w mod 2 ^ n :: int)"
 130.462    by (rule ext) (rule bintrunc_mod2p)
 130.463 @@ -620,19 +585,10 @@
 130.464    apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
 130.465    done
 130.466  
 130.467 -lemma no_bintr: 
 130.468 -  "number_of (bintrunc n w) = (number_of w mod 2 ^ n :: int)"
 130.469 -  by (simp add : bintrunc_mod2p number_of_eq)
 130.470 -
 130.471  lemma no_sbintr_alt2: 
 130.472    "sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
 130.473    by (rule ext) (simp add : sbintrunc_mod2p)
 130.474  
 130.475 -lemma no_sbintr: 
 130.476 -  "number_of (sbintrunc n w) = 
 130.477 -   ((number_of w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
 130.478 -  by (simp add : no_sbintr_alt2 number_of_eq)
 130.479 -
 130.480  lemma range_sbintrunc: 
 130.481    "range (sbintrunc n) = {i. - (2 ^ n) <= i & i < 2 ^ n}"
 130.482    apply (unfold no_sbintr_alt2)
 130.483 @@ -692,21 +648,20 @@
 130.484  
 130.485  lemmas m2pths = pos_mod_sign pos_mod_bound [OF zless2p]
 130.486  
 130.487 -lemma bintr_ge0: "(0 :: int) <= number_of (bintrunc n w)"
 130.488 -  by (simp add : no_bintr m2pths)
 130.489 +lemma bintr_ge0: "0 \<le> bintrunc n w"
 130.490 +  by (simp add: bintrunc_mod2p)
 130.491  
 130.492 -lemma bintr_lt2p: "number_of (bintrunc n w) < (2 ^ n :: int)"
 130.493 -  by (simp add : no_bintr m2pths)
 130.494 +lemma bintr_lt2p: "bintrunc n w < 2 ^ n"
 130.495 +  by (simp add: bintrunc_mod2p)
 130.496  
 130.497 -lemma bintr_Min: 
 130.498 -  "number_of (bintrunc n Int.Min) = (2 ^ n :: int) - 1"
 130.499 -  by (simp add : no_bintr m1mod2k)
 130.500 +lemma bintr_Min: "bintrunc n -1 = 2 ^ n - 1"
 130.501 +  by (simp add: bintrunc_mod2p m1mod2k)
 130.502  
 130.503 -lemma sbintr_ge: "(- (2 ^ n) :: int) <= number_of (sbintrunc n w)"
 130.504 -  by (simp add : no_sbintr m2pths)
 130.505 +lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w"
 130.506 +  by (simp add: sbintrunc_mod2p)
 130.507  
 130.508 -lemma sbintr_lt: "number_of (sbintrunc n w) < (2 ^ n :: int)"
 130.509 -  by (simp add : no_sbintr m2pths)
 130.510 +lemma sbintr_lt: "sbintrunc n w < 2 ^ n"
 130.511 +  by (simp add: sbintrunc_mod2p)
 130.512  
 130.513  lemma sign_Pls_ge_0: 
 130.514    "(bin_sign bin = 0) = (bin >= (0 :: int))"
 130.515 @@ -716,8 +671,6 @@
 130.516    "(bin_sign bin = -1) = (bin < (0 :: int))"
 130.517    unfolding bin_sign_def by simp
 130.518  
 130.519 -lemmas sign_Min_neg = trans [OF sign_Min_lt_0 neg_def [symmetric]] 
 130.520 -
 130.521  lemma bin_rest_trunc:
 130.522    "(bin_rest (bintrunc n bin)) = bintrunc (n - 1) (bin_rest bin)"
 130.523    by (induct n arbitrary: bin) auto
 130.524 @@ -789,7 +742,7 @@
 130.525  lemma [code]:
 130.526    "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) in (w1, w2 BIT bin_last w))"
 130.527    "bin_split 0 w = (w, 0)"
 130.528 -  by (simp_all add: Pls_def)
 130.529 +  by simp_all
 130.530  
 130.531  primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int" where
 130.532    Z: "bin_cat w 0 v = w"
 130.533 @@ -801,24 +754,17 @@
 130.534    "0 < n \<Longrightarrow> f ^^ n = f \<circ> f ^^ (n - 1)"
 130.535    by (cases n) simp_all
 130.536  
 130.537 -lemmas funpow_pred_simp [simp] =
 130.538 -  funpow_minus_simp [of "number_of bin", simplified nobm1] for bin
 130.539 +lemma funpow_numeral [simp]:
 130.540 +  "f ^^ numeral k = f \<circ> f ^^ (numeral k - 1)"
 130.541 +  by (subst expand_Suc, rule funpow.simps)
 130.542  
 130.543 -lemmas replicate_minus_simp = 
 130.544 -  trans [OF gen_minus [where f = "%n. replicate n x"] replicate.replicate_Suc] for x
 130.545 -
 130.546 -lemmas replicate_pred_simp [simp] =
 130.547 -  replicate_minus_simp [of "number_of bin", simplified nobm1] for bin
 130.548 -
 130.549 -lemmas power_Suc_no [simp] = power_Suc [of "number_of a"] for a
 130.550 +lemma replicate_numeral [simp]: (* TODO: move to List.thy *)
 130.551 +  "replicate (numeral k) x = x # replicate (numeral k - 1) x"
 130.552 +  by (subst expand_Suc, rule replicate_Suc)
 130.553  
 130.554  lemmas power_minus_simp = 
 130.555    trans [OF gen_minus [where f = "power f"] power_Suc] for f
 130.556  
 130.557 -lemmas power_pred_simp = 
 130.558 -  power_minus_simp [of "number_of bin", simplified nobm1] for bin
 130.559 -lemmas power_pred_simp_no [simp] = power_pred_simp [where f= "number_of f"] for f
 130.560 -
 130.561  lemma list_exhaust_size_gt0:
 130.562    assumes y: "\<And>a list. y = a # list \<Longrightarrow> P"
 130.563    shows "0 < length y \<Longrightarrow> P"
 130.564 @@ -839,11 +785,6 @@
 130.565    "y = xa # list ==> size y = Suc k ==> size list = k"
 130.566    by auto
 130.567  
 130.568 -lemma size_Cons_lem_eq_bin:
 130.569 -  "y = xa # list ==> size y = number_of (Int.succ k) ==> 
 130.570 -    size list = number_of k"
 130.571 -  by (auto simp: pred_def succ_def split add : split_if_asm)
 130.572 -
 130.573  lemmas ls_splits = prod.split prod.split_asm split_if_asm
 130.574  
 130.575  lemma not_B1_is_B0: "y \<noteq> (1::bit) \<Longrightarrow> y = (0::bit)"
   131.1 --- a/src/HOL/Word/Bool_List_Representation.thy	Fri Mar 23 20:32:43 2012 +0100
   131.2 +++ b/src/HOL/Word/Bool_List_Representation.thy	Mon Mar 26 10:56:56 2012 +0200
   131.3 @@ -106,13 +106,13 @@
   131.4    by (cases n) auto
   131.5  
   131.6  lemma bin_to_bl_aux_Bit0_minus_simp [simp]:
   131.7 -  "0 < n ==> bin_to_bl_aux n (number_of (Int.Bit0 w)) bl = 
   131.8 -    bin_to_bl_aux (n - 1) (number_of w) (False # bl)"
   131.9 +  "0 < n ==> bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = 
  131.10 +    bin_to_bl_aux (n - 1) (numeral w) (False # bl)"
  131.11    by (cases n) auto
  131.12  
  131.13  lemma bin_to_bl_aux_Bit1_minus_simp [simp]:
  131.14 -  "0 < n ==> bin_to_bl_aux n (number_of (Int.Bit1 w)) bl = 
  131.15 -    bin_to_bl_aux (n - 1) (number_of w) (True # bl)"
  131.16 +  "0 < n ==> bin_to_bl_aux n (numeral (Num.Bit1 w)) bl = 
  131.17 +    bin_to_bl_aux (n - 1) (numeral w) (True # bl)"
  131.18    by (cases n) auto
  131.19  
  131.20  text {* Link between bin and bool list. *}
  131.21 @@ -632,8 +632,13 @@
  131.22  lemmas takefill_minus_simps = takefill_Suc_cases [THEN [2] 
  131.23    takefill_minus [symmetric, THEN trans]]
  131.24  
  131.25 -lemmas takefill_pred_simps [simp] =
  131.26 -  takefill_minus_simps [where n="number_of bin", simplified nobm1] for bin
  131.27 +lemma takefill_numeral_Nil [simp]:
  131.28 +  "takefill fill (numeral k) [] = fill # takefill fill (numeral k - 1) []"
  131.29 +  by (subst expand_Suc, rule takefill_Suc_Nil)
  131.30 +
  131.31 +lemma takefill_numeral_Cons [simp]:
  131.32 +  "takefill fill (numeral k) (x # xs) = x # takefill fill (numeral k - 1) xs"
  131.33 +  by (subst expand_Suc, rule takefill_Suc_Cons)
  131.34  
  131.35  (* links with function bl_to_bin *)
  131.36  
  131.37 @@ -1031,11 +1036,11 @@
  131.38    bin_split.Suc [THEN [2] bin_split_minus [symmetric, THEN trans]]
  131.39  
  131.40  lemma bin_split_pred_simp [simp]: 
  131.41 -  "(0::nat) < number_of bin \<Longrightarrow>
  131.42 -  bin_split (number_of bin) w =
  131.43 -  (let (w1, w2) = bin_split (number_of (Int.pred bin)) (bin_rest w)
  131.44 +  "(0::nat) < numeral bin \<Longrightarrow>
  131.45 +  bin_split (numeral bin) w =
  131.46 +  (let (w1, w2) = bin_split (numeral bin - 1) (bin_rest w)
  131.47     in (w1, w2 BIT bin_last w))" 
  131.48 -  by (simp only: nobm1 bin_split_minus_simp)
  131.49 +  by (simp only: bin_split_minus_simp)
  131.50  
  131.51  lemma bin_rsplit_aux_simp_alt:
  131.52    "bin_rsplit_aux n m c bs =
   132.1 --- a/src/HOL/Word/Misc_Numeric.thy	Fri Mar 23 20:32:43 2012 +0100
   132.2 +++ b/src/HOL/Word/Misc_Numeric.thy	Mon Mar 26 10:56:56 2012 +0200
   132.3 @@ -5,7 +5,7 @@
   132.4  header {* Useful Numerical Lemmas *}
   132.5  
   132.6  theory Misc_Numeric
   132.7 -imports Main Parity
   132.8 +imports "~~/src/HOL/Main" "~~/src/HOL/Parity"
   132.9  begin
  132.10  
  132.11  lemma the_elemI: "y = {x} ==> the_elem y = x" 
  132.12 @@ -31,13 +31,6 @@
  132.13  
  132.14  lemma sum_imp_diff: "j = k + i ==> j - i = (k :: nat)" by arith
  132.15  
  132.16 -lemma nobm1:
  132.17 -  "0 < (number_of w :: nat) ==> 
  132.18 -   number_of w - (1 :: nat) = number_of (Int.pred w)" 
  132.19 -  apply (unfold nat_number_of_def One_nat_def nat_1 [symmetric] pred_def)
  132.20 -  apply (simp add: number_of_eq nat_diff_distrib [symmetric])
  132.21 -  done
  132.22 -
  132.23  lemma zless2: "0 < (2 :: int)" by arith
  132.24  
  132.25  lemmas zless2p [simp] = zless2 [THEN zero_less_power]
  132.26 @@ -46,7 +39,6 @@
  132.27  lemmas pos_mod_sign2 = zless2 [THEN pos_mod_sign [where b = "2::int"]]
  132.28  lemmas pos_mod_bound2 = zless2 [THEN pos_mod_bound [where b = "2::int"]]
  132.29  
  132.30 --- "the inverse(s) of @{text number_of}"
  132.31  lemma nmod2: "n mod (2::int) = 0 | n mod 2 = 1" by arith
  132.32  
  132.33  lemma emep1:
  132.34 @@ -283,15 +275,6 @@
  132.35    
  132.36  lemmas min_minus' [simp] = trans [OF min_max.inf_commute min_minus]
  132.37  
  132.38 -lemma nat_no_eq_iff: 
  132.39 -  "(number_of b :: int) >= 0 ==> (number_of c :: int) >= 0 ==> 
  132.40 -   (number_of b = (number_of c :: nat)) = (b = c)" 
  132.41 -  apply (unfold nat_number_of_def) 
  132.42 -  apply safe
  132.43 -  apply (drule (2) eq_nat_nat_iff [THEN iffD1])
  132.44 -  apply (simp add: number_of_eq)
  132.45 -  done
  132.46 -
  132.47  lemmas dme = box_equals [OF div_mod_equality add_0_right add_0_right]
  132.48  lemmas dtle = xtr3 [OF dme [symmetric] le_add1]
  132.49  lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] dtle]
   133.1 --- a/src/HOL/Word/Word.thy	Fri Mar 23 20:32:43 2012 +0100
   133.2 +++ b/src/HOL/Word/Word.thy	Mon Mar 26 10:56:56 2012 +0200
   133.3 @@ -20,17 +20,64 @@
   133.4  typedef (open) 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}"
   133.5    morphisms uint Abs_word by auto
   133.6  
   133.7 +lemma uint_nonnegative:
   133.8 +  "0 \<le> uint w"
   133.9 +  using word.uint [of w] by simp
  133.10 +
  133.11 +lemma uint_bounded:
  133.12 +  fixes w :: "'a::len0 word"
  133.13 +  shows "uint w < 2 ^ len_of TYPE('a)"
  133.14 +  using word.uint [of w] by simp
  133.15 +
  133.16 +lemma uint_idem:
  133.17 +  fixes w :: "'a::len0 word"
  133.18 +  shows "uint w mod 2 ^ len_of TYPE('a) = uint w"
  133.19 +  using uint_nonnegative uint_bounded by (rule mod_pos_pos_trivial)
  133.20 +
  133.21  definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" where
  133.22    -- {* representation of words using unsigned or signed bins, 
  133.23          only difference in these is the type class *}
  133.24 -  "word_of_int w = Abs_word (bintrunc (len_of TYPE ('a)) w)" 
  133.25 -
  133.26 -lemma uint_word_of_int [code]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = w mod 2 ^ len_of TYPE('a)"
  133.27 -  by (auto simp add: word_of_int_def bintrunc_mod2p intro: Abs_word_inverse)
  133.28 -
  133.29 -code_datatype word_of_int
  133.30 -
  133.31 -subsection {* Random instance *}
  133.32 +  "word_of_int k = Abs_word (k mod 2 ^ len_of TYPE('a))" 
  133.33 +
  133.34 +lemma uint_word_of_int:
  133.35 +  "uint (word_of_int k :: 'a::len0 word) = k mod 2 ^ len_of TYPE('a)"
  133.36 +  by (auto simp add: word_of_int_def intro: Abs_word_inverse)
  133.37 +
  133.38 +lemma word_of_int_uint:
  133.39 +  "word_of_int (uint w) = w"
  133.40 +  by (simp add: word_of_int_def uint_idem uint_inverse)
  133.41 +
  133.42 +lemma word_uint_eq_iff:
  133.43 +  "a = b \<longleftrightarrow> uint a = uint b"
  133.44 +  by (simp add: uint_inject)
  133.45 +
  133.46 +lemma word_uint_eqI:
  133.47 +  "uint a = uint b \<Longrightarrow> a = b"
  133.48 +  by (simp add: word_uint_eq_iff)
  133.49 +
  133.50 +
  133.51 +subsection {* Basic code generation setup *}
  133.52 +
  133.53 +definition Word :: "int \<Rightarrow> 'a::len0 word"
  133.54 +where
  133.55 +  [code_post]: "Word = word_of_int"
  133.56 +
  133.57 +lemma [code abstype]:
  133.58 +  "Word (uint w) = w"
  133.59 +  by (simp add: Word_def word_of_int_uint)
  133.60 +
  133.61 +declare uint_word_of_int [code abstract]
  133.62 +
  133.63 +instantiation word :: (len0) equal
  133.64 +begin
  133.65 +
  133.66 +definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where
  133.67 +  "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)"
  133.68 +
  133.69 +instance proof
  133.70 +qed (simp add: equal equal_word_def word_uint_eq_iff)
  133.71 +
  133.72 +end
  133.73  
  133.74  notation fcomp (infixl "\<circ>>" 60)
  133.75  notation scomp (infixl "\<circ>\<rightarrow>" 60)
  133.76 @@ -39,7 +86,7 @@
  133.77  begin
  133.78  
  133.79  definition
  133.80 -  "random_word i = Random.range (max i (2 ^ len_of TYPE('a))) \<circ>\<rightarrow> (\<lambda>k. Pair (
  133.81 +  "random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair (
  133.82       let j = word_of_int (Code_Numeral.int_of k) :: 'a word
  133.83       in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))"
  133.84  
  133.85 @@ -193,7 +240,7 @@
  133.86  where
  133.87    "word_pred a = word_of_int (uint a - 1)"
  133.88  
  133.89 -instantiation word :: (len0) "{number, Divides.div, comm_monoid_mult, comm_ring}"
  133.90 +instantiation word :: (len0) "{neg_numeral, Divides.div, comm_monoid_mult, comm_ring}"
  133.91  begin
  133.92  
  133.93  definition
  133.94 @@ -220,9 +267,6 @@
  133.95  definition
  133.96    word_mod_def: "a mod b = word_of_int (uint a mod uint b)"
  133.97  
  133.98 -definition
  133.99 -  word_number_of_def: "number_of w = word_of_int w"
 133.100 -
 133.101  lemmas word_arith_wis =
 133.102    word_add_def word_sub_wi word_mult_def word_minus_def 
 133.103    word_succ_def word_pred_def word_0_wi word_1_wi
 133.104 @@ -268,9 +312,6 @@
 133.105    apply (simp add: word_of_nat wi_hom_sub)
 133.106    done
 133.107  
 133.108 -instance word :: (len) number_ring
 133.109 -  by (default, simp add: word_number_of_def word_of_int)
 133.110 -
 133.111  definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) where
 133.112    "a udvd b = (EX n>=0. uint b = n * uint a)"
 133.113  
 133.114 @@ -284,7 +325,7 @@
 133.115    word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
 133.116  
 133.117  definition
 133.118 -  word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)"
 133.119 +  word_less_def: "a < b \<longleftrightarrow> uint a < uint b"
 133.120  
 133.121  instance
 133.122    by default (auto simp: word_less_def word_le_def)
 133.123 @@ -504,40 +545,55 @@
 133.124  
 133.125  lemmas td_sint = word_sint.td
 133.126  
 133.127 -lemma word_number_of_alt:
 133.128 -  "number_of b = word_of_int (number_of b)"
 133.129 -  by (simp add: number_of_eq word_number_of_def)
 133.130 -
 133.131 -declare word_number_of_alt [symmetric, code_abbrev]
 133.132 -
 133.133 -lemma word_no_wi: "number_of = word_of_int"
 133.134 -  by (auto simp: word_number_of_def)
 133.135 -
 133.136  lemma to_bl_def': 
 133.137    "(to_bl :: 'a :: len0 word => bool list) =
 133.138      bin_to_bl (len_of TYPE('a)) o uint"
 133.139    by (auto simp: to_bl_def)
 133.140  
 133.141 -lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w"] for w
 133.142 +lemmas word_reverse_no_def [simp] = word_reverse_def [of "numeral w"] for w
 133.143  
 133.144  lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)"
 133.145    by (fact uints_def [unfolded no_bintr_alt1])
 133.146  
 133.147 +lemma word_numeral_alt:
 133.148 +  "numeral b = word_of_int (numeral b)"
 133.149 +  by (induct b, simp_all only: numeral.simps word_of_int_homs)
 133.150 +
 133.151 +declare word_numeral_alt [symmetric, code_abbrev]
 133.152 +
 133.153 +lemma word_neg_numeral_alt:
 133.154 +  "neg_numeral b = word_of_int (neg_numeral b)"
 133.155 +  by (simp only: neg_numeral_def word_numeral_alt wi_hom_neg)
 133.156 +
 133.157 +declare word_neg_numeral_alt [symmetric, code_abbrev]
 133.158 +
 133.159  lemma uint_bintrunc [simp]:
 133.160 -  "uint (number_of bin :: 'a word) =
 133.161 -    bintrunc (len_of TYPE ('a :: len0)) (number_of bin)"
 133.162 -  unfolding word_number_of_alt by (rule word_ubin.eq_norm)
 133.163 +  "uint (numeral bin :: 'a word) = 
 133.164 +    bintrunc (len_of TYPE ('a :: len0)) (numeral bin)"
 133.165 +  unfolding word_numeral_alt by (rule word_ubin.eq_norm)
 133.166 +
 133.167 +lemma uint_bintrunc_neg [simp]: "uint (neg_numeral bin :: 'a word) = 
 133.168 +    bintrunc (len_of TYPE ('a :: len0)) (neg_numeral bin)"
 133.169 +  by (simp only: word_neg_numeral_alt word_ubin.eq_norm)
 133.170  
 133.171  lemma sint_sbintrunc [simp]:
 133.172 -  "sint (number_of bin :: 'a word) =
 133.173 -    sbintrunc (len_of TYPE ('a :: len) - 1) (number_of bin)"
 133.174 -  unfolding word_number_of_alt by (rule word_sbin.eq_norm)
 133.175 +  "sint (numeral bin :: 'a word) = 
 133.176 +    sbintrunc (len_of TYPE ('a :: len) - 1) (numeral bin)"
 133.177 +  by (simp only: word_numeral_alt word_sbin.eq_norm)
 133.178 +
 133.179 +lemma sint_sbintrunc_neg [simp]: "sint (neg_numeral bin :: 'a word) = 
 133.180 +    sbintrunc (len_of TYPE ('a :: len) - 1) (neg_numeral bin)"
 133.181 +  by (simp only: word_neg_numeral_alt word_sbin.eq_norm)
 133.182  
 133.183  lemma unat_bintrunc [simp]:
 133.184 -  "unat (number_of bin :: 'a :: len0 word) =
 133.185 -    nat (bintrunc (len_of TYPE('a)) (number_of bin))"
 133.186 -  unfolding unat_def nat_number_of_def 
 133.187 -  by (simp only: uint_bintrunc)
 133.188 +  "unat (numeral bin :: 'a :: len0 word) =
 133.189 +    nat (bintrunc (len_of TYPE('a)) (numeral bin))"
 133.190 +  by (simp only: unat_def uint_bintrunc)
 133.191 +
 133.192 +lemma unat_bintrunc_neg [simp]:
 133.193 +  "unat (neg_numeral bin :: 'a :: len0 word) =
 133.194 +    nat (bintrunc (len_of TYPE('a)) (neg_numeral bin))"
 133.195 +  by (simp only: unat_def uint_bintrunc_neg)
 133.196  
 133.197  lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 \<Longrightarrow> v = w"
 133.198    apply (unfold word_size)
 133.199 @@ -562,7 +618,7 @@
 133.200  
 133.201  lemma sign_uint_Pls [simp]: 
 133.202    "bin_sign (uint x) = 0"
 133.203 -  by (simp add: sign_Pls_ge_0 number_of_eq)
 133.204 +  by (simp add: sign_Pls_ge_0)
 133.205  
 133.206  lemma uint_m2p_neg: "uint (x::'a::len0 word) - 2 ^ len_of TYPE('a) < 0"
 133.207    by (simp only: diff_less_0_iff_less uint_lt2p)
 133.208 @@ -581,35 +637,43 @@
 133.209  lemma uint_nat: "uint w = int (unat w)"
 133.210    unfolding unat_def by auto
 133.211  
 133.212 -lemma uint_number_of:
 133.213 -  "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)"
 133.214 -  unfolding word_number_of_alt
 133.215 +lemma uint_numeral:
 133.216 +  "uint (numeral b :: 'a :: len0 word) = numeral b mod 2 ^ len_of TYPE('a)"
 133.217 +  unfolding word_numeral_alt
 133.218    by (simp only: int_word_uint)
 133.219  
 133.220 -lemma unat_number_of: 
 133.221 -  "bin_sign (number_of b) = 0 \<Longrightarrow> 
 133.222 -  unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)"
 133.223 +lemma uint_neg_numeral:
 133.224 +  "uint (neg_numeral b :: 'a :: len0 word) = neg_numeral b mod 2 ^ len_of TYPE('a)"
 133.225 +  unfolding word_neg_numeral_alt
 133.226 +  by (simp only: int_word_uint)
 133.227 +
 133.228 +lemma unat_numeral: 
 133.229 +  "unat (numeral b::'a::len0 word) = numeral b mod 2 ^ len_of TYPE ('a)"
 133.230    apply (unfold unat_def)
 133.231 -  apply (clarsimp simp only: uint_number_of)
 133.232 +  apply (clarsimp simp only: uint_numeral)
 133.233    apply (rule nat_mod_distrib [THEN trans])
 133.234 -    apply (erule sign_Pls_ge_0 [THEN iffD1])
 133.235 +    apply (rule zero_le_numeral)
 133.236     apply (simp_all add: nat_power_eq)
 133.237    done
 133.238  
 133.239 -lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + 
 133.240 +lemma sint_numeral: "sint (numeral b :: 'a :: len word) = (numeral b + 
 133.241      2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
 133.242      2 ^ (len_of TYPE('a) - 1)"
 133.243 -  unfolding word_number_of_alt by (rule int_word_sint)
 133.244 -
 133.245 -lemma word_of_int_0 [simp]: "word_of_int 0 = 0"
 133.246 +  unfolding word_numeral_alt by (rule int_word_sint)
 133.247 +
 133.248 +lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0"
 133.249    unfolding word_0_wi ..
 133.250  
 133.251 -lemma word_of_int_1 [simp]: "word_of_int 1 = 1"
 133.252 +lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1"
 133.253    unfolding word_1_wi ..
 133.254  
 133.255 -lemma word_of_int_bin [simp] : 
 133.256 -  "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)"
 133.257 -  unfolding word_number_of_alt ..
 133.258 +lemma word_of_int_numeral [simp] : 
 133.259 +  "(word_of_int (numeral bin) :: 'a :: len0 word) = (numeral bin)"
 133.260 +  unfolding word_numeral_alt ..
 133.261 +
 133.262 +lemma word_of_int_neg_numeral [simp]:
 133.263 +  "(word_of_int (neg_numeral bin) :: 'a :: len0 word) = (neg_numeral bin)"
 133.264 +  unfolding neg_numeral_def word_numeral_alt wi_hom_syms ..
 133.265  
 133.266  lemma word_int_case_wi: 
 133.267    "word_int_case f (word_of_int i :: 'b word) = 
 133.268 @@ -728,7 +792,7 @@
 133.269    unfolding word_reverse_def by (simp add : word_bl.Abs_inverse)
 133.270  
 133.271  lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w"
 133.272 -  by auto
 133.273 +  by (metis word_rev_rev)
 133.274  
 133.275  lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u"
 133.276    by simp
 133.277 @@ -762,8 +826,8 @@
 133.278    done
 133.279  
 133.280  lemma no_of_bl: 
 133.281 -  "(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)"
 133.282 -  unfolding word_size of_bl_def by (simp add: word_number_of_def)
 133.283 +  "(numeral bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) (numeral bin))"
 133.284 +  unfolding of_bl_def by simp
 133.285  
 133.286  lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)"
 133.287    unfolding word_size to_bl_def by auto
 133.288 @@ -775,9 +839,15 @@
 133.289    "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin"
 133.290    unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size)
 133.291  
 133.292 -lemma to_bl_no_bin [simp]:
 133.293 -  "to_bl (number_of bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) (number_of bin)"
 133.294 -  unfolding word_number_of_alt by (rule to_bl_of_bin)
 133.295 +lemma to_bl_numeral [simp]:
 133.296 +  "to_bl (numeral bin::'a::len0 word) =
 133.297 +    bin_to_bl (len_of TYPE('a)) (numeral bin)"
 133.298 +  unfolding word_numeral_alt by (rule to_bl_of_bin)
 133.299 +
 133.300 +lemma to_bl_neg_numeral [simp]:
 133.301 +  "to_bl (neg_numeral bin::'a::len0 word) =
 133.302 +    bin_to_bl (len_of TYPE('a)) (neg_numeral bin)"
 133.303 +  unfolding word_neg_numeral_alt by (rule to_bl_of_bin)
 133.304  
 133.305  lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w"
 133.306    unfolding uint_bl by (simp add : word_size)
 133.307 @@ -803,35 +873,29 @@
 133.308    by (auto simp add : uints_unats image_iff)
 133.309  
 133.310  lemmas bintr_num = word_ubin.norm_eq_iff
 133.311 -  [of "number_of a" "number_of b", symmetric, folded word_number_of_alt] for a b
 133.312 +  [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
 133.313  lemmas sbintr_num = word_sbin.norm_eq_iff
 133.314 -  [of "number_of a" "number_of b", symmetric, folded word_number_of_alt] for a b
 133.315 -
 133.316 -lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def]
 133.317 -lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def]
 133.318 -    
 133.319 -(* don't add these to simpset, since may want bintrunc n w to be simplified;
 133.320 -  may want these in reverse, but loop as simp rules, so use following *)
 133.321 +  [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
 133.322  
 133.323  lemma num_of_bintr':
 133.324 -  "bintrunc (len_of TYPE('a :: len0)) (number_of a) = (number_of b) \<Longrightarrow> 
 133.325 -    number_of a = (number_of b :: 'a word)"
 133.326 +  "bintrunc (len_of TYPE('a :: len0)) (numeral a) = (numeral b) \<Longrightarrow> 
 133.327 +    numeral a = (numeral b :: 'a word)"
 133.328    unfolding bintr_num by (erule subst, simp)
 133.329  
 133.330  lemma num_of_sbintr':
 133.331 -  "sbintrunc (len_of TYPE('a :: len) - 1) (number_of a) = (number_of b) \<Longrightarrow> 
 133.332 -    number_of a = (number_of b :: 'a word)"
 133.333 +  "sbintrunc (len_of TYPE('a :: len) - 1) (numeral a) = (numeral b) \<Longrightarrow> 
 133.334 +    numeral a = (numeral b :: 'a word)"
 133.335    unfolding sbintr_num by (erule subst, simp)
 133.336  
 133.337  lemma num_abs_bintr:
 133.338 -  "(number_of x :: 'a word) =
 133.339 -    word_of_int (bintrunc (len_of TYPE('a::len0)) (number_of x))"
 133.340 -  by (simp only: word_ubin.Abs_norm word_number_of_alt)
 133.341 +  "(numeral x :: 'a word) =
 133.342 +    word_of_int (bintrunc (len_of TYPE('a::len0)) (numeral x))"
 133.343 +  by (simp only: word_ubin.Abs_norm word_numeral_alt)
 133.344  
 133.345  lemma num_abs_sbintr:
 133.346 -  "(number_of x :: 'a word) =
 133.347 -    word_of_int (sbintrunc (len_of TYPE('a::len) - 1) (number_of x))"
 133.348 -  by (simp only: word_sbin.Abs_norm word_number_of_alt)
 133.349 +  "(numeral x :: 'a word) =
 133.350 +    word_of_int (sbintrunc (len_of TYPE('a::len) - 1) (numeral x))"
 133.351 +  by (simp only: word_sbin.Abs_norm word_numeral_alt)
 133.352  
 133.353  (** cast - note, no arg for new length, as it's determined by type of result,
 133.354    thus in "cast w = w, the type means cast to length of w! **)
 133.355 @@ -856,13 +920,14 @@
 133.356  (* for literal u(s)cast *)
 133.357  
 133.358  lemma ucast_bintr [simp]:
 133.359 -  "ucast (number_of w ::'a::len0 word) = 
 133.360 -   word_of_int (bintrunc (len_of TYPE('a)) (number_of w))"
 133.361 +  "ucast (numeral w ::'a::len0 word) = 
 133.362 +   word_of_int (bintrunc (len_of TYPE('a)) (numeral w))"
 133.363    unfolding ucast_def by simp
 133.364 +(* TODO: neg_numeral *)
 133.365  
 133.366  lemma scast_sbintr [simp]:
 133.367 -  "scast (number_of w ::'a::len word) = 
 133.368 -   word_of_int (sbintrunc (len_of TYPE('a) - Suc 0) (number_of w))"
 133.369 +  "scast (numeral w ::'a::len word) = 
 133.370 +   word_of_int (sbintrunc (len_of TYPE('a) - Suc 0) (numeral w))"
 133.371    unfolding scast_def by simp
 133.372  
 133.373  lemma source_size: "source_size (c::'a::len0 word \<Rightarrow> _) = len_of TYPE('a)"
 133.374 @@ -1016,8 +1081,8 @@
 133.375    done
 133.376  
 133.377  lemma ucast_down_no [OF refl]:
 133.378 -  "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (number_of bin) = number_of bin"
 133.379 -  unfolding word_number_of_alt by clarify (rule ucast_down_wi)
 133.380 +  "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (numeral bin) = numeral bin"
 133.381 +  unfolding word_numeral_alt by clarify (rule ucast_down_wi)
 133.382  
 133.383  lemma ucast_down_bl [OF refl]:
 133.384    "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl"
 133.385 @@ -1028,19 +1093,6 @@
 133.386  
 133.387  lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def
 133.388  
 133.389 -text {* Executable equality *}
 133.390 -
 133.391 -instantiation word :: (len0) equal
 133.392 -begin
 133.393 -
 133.394 -definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where
 133.395 -  "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)"
 133.396 -
 133.397 -instance proof
 133.398 -qed (simp add: equal equal_word_def)
 133.399 -
 133.400 -end
 133.401 -
 133.402  
 133.403  subsection {* Word Arithmetic *}
 133.404  
 133.405 @@ -1057,33 +1109,23 @@
 133.406    "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b"
 133.407    by (auto simp: udvd_def)
 133.408  
 133.409 -lemmas word_div_no [simp] = word_div_def [of "number_of a" "number_of b"] for a b
 133.410 -
 133.411 -lemmas word_mod_no [simp] = word_mod_def [of "number_of a" "number_of b"] for a b
 133.412 -
 133.413 -lemmas word_less_no [simp] = word_less_def [of "number_of a" "number_of b"] for a b
 133.414 -
 133.415 -lemmas word_le_no [simp] = word_le_def [of "number_of a" "number_of b"] for a b
 133.416 -
 133.417 -lemmas word_sless_no [simp] = word_sless_def [of "number_of a" "number_of b"] for a b
 133.418 -
 133.419 -lemmas word_sle_no [simp] = word_sle_def [of "number_of a" "number_of b"] for a b
 133.420 -
 133.421 -(* following two are available in class number_ring, 
 133.422 -  but convenient to have them here here;
 133.423 -  note - the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1
 133.424 -  are in the default simpset, so to use the automatic simplifications for
 133.425 -  (eg) sint (number_of bin) on sint 1, must do
 133.426 -  (simp add: word_1_no del: numeral_1_eq_1) 
 133.427 -  *)
 133.428 -lemma word_0_no: "(0::'a::len0 word) = Numeral0"
 133.429 -  by (simp add: word_number_of_alt)
 133.430 +lemmas word_div_no [simp] = word_div_def [of "numeral a" "numeral b"] for a b
 133.431 +
 133.432 +lemmas word_mod_no [simp] = word_mod_def [of "numeral a" "numeral b"] for a b
 133.433 +
 133.434 +lemmas word_less_no [simp] = word_less_def [of "numeral a" "numeral b"] for a b
 133.435 +
 133.436 +lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b
 133.437 +
 133.438 +lemmas word_sless_no [simp] = word_sless_def [of "numeral a" "numeral b"] for a b
 133.439 +
 133.440 +lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b
 133.441  
 133.442  lemma word_1_no: "(1::'a::len0 word) = Numeral1"
 133.443 -  by (simp add: word_number_of_alt)
 133.444 +  by (simp add: word_numeral_alt)
 133.445  
 133.446  lemma word_m1_wi: "-1 = word_of_int -1" 
 133.447 -  by (rule word_number_of_alt)
 133.448 +  by (rule word_neg_numeral_alt)
 133.449  
 133.450  lemma word_0_bl [simp]: "of_bl [] = 0"
 133.451    unfolding of_bl_def by simp
 133.452 @@ -1195,17 +1237,18 @@
 133.453  lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]]
 133.454  
 133.455  lemma word_pred_0_n1: "word_pred 0 = word_of_int -1"
 133.456 -  unfolding word_pred_def uint_eq_0 pred_def by simp
 133.457 +  unfolding word_pred_def uint_eq_0 by simp
 133.458  
 133.459  lemma succ_pred_no [simp]:
 133.460 -  "word_succ (number_of bin) = number_of (Int.succ bin) & 
 133.461 -    word_pred (number_of bin) = number_of (Int.pred bin)"
 133.462 -  unfolding word_number_of_def Int.succ_def Int.pred_def
 133.463 -  by (simp add: word_of_int_homs)
 133.464 +  "word_succ (numeral w) = numeral w + 1"
 133.465 +  "word_pred (numeral w) = numeral w - 1"
 133.466 +  "word_succ (neg_numeral w) = neg_numeral w + 1"
 133.467 +  "word_pred (neg_numeral w) = neg_numeral w - 1"
 133.468 +  unfolding word_succ_p1 word_pred_m1 by simp_all
 133.469  
 133.470  lemma word_sp_01 [simp] : 
 133.471    "word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0"
 133.472 -  unfolding word_0_no word_1_no by simp
 133.473 +  unfolding word_succ_p1 word_pred_m1 by simp_all
 133.474  
 133.475  (* alternative approach to lifting arithmetic equalities *)
 133.476  lemma word_of_int_Ex:
 133.477 @@ -1230,10 +1273,10 @@
 133.478  lemmas word_not_simps [simp] = 
 133.479    word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
 133.480  
 133.481 -lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))"
 133.482 -  unfolding word_less_def by auto
 133.483 -
 133.484 -lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y"] for y
 133.485 +lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> (y :: 'a :: len0 word)"
 133.486 +  by (simp add: less_le)
 133.487 +
 133.488 +lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y
 133.489  
 133.490  lemma word_sless_alt: "(a <s b) = (sint a < sint b)"
 133.491    unfolding word_sle_def word_sless_def
 133.492 @@ -1647,10 +1690,15 @@
 133.493  (* note that iszero_def is only for class comm_semiring_1_cancel,
 133.494     which requires word length >= 1, ie 'a :: len word *) 
 133.495  lemma iszero_word_no [simp]:
 133.496 -  "iszero (number_of bin :: 'a :: len word) = 
 133.497 -    iszero (bintrunc (len_of TYPE('a)) (number_of bin))"
 133.498 -  using word_ubin.norm_eq_iff [where 'a='a, of "number_of bin" 0]
 133.499 +  "iszero (numeral bin :: 'a :: len word) = 
 133.500 +    iszero (bintrunc (len_of TYPE('a)) (numeral bin))"
 133.501 +  using word_ubin.norm_eq_iff [where 'a='a, of "numeral bin" 0]
 133.502    by (simp add: iszero_def [symmetric])
 133.503 +    
 133.504 +text {* Use @{text iszero} to simplify equalities between word numerals. *}
 133.505 +
 133.506 +lemmas word_eq_numeral_iff_iszero [simp] =
 133.507 +  eq_numeral_iff_iszero [where 'a="'a::len word"]
 133.508  
 133.509  
 133.510  subsection "Word and nat"
 133.511 @@ -2023,10 +2071,10 @@
 133.512  
 133.513  lemma of_bl_length_less: 
 133.514    "length x = k \<Longrightarrow> k < len_of TYPE('a) \<Longrightarrow> (of_bl x :: 'a :: len word) < 2 ^ k"
 133.515 -  apply (unfold of_bl_def word_less_alt word_number_of_alt)
 133.516 +  apply (unfold of_bl_def word_less_alt word_numeral_alt)
 133.517    apply safe
 133.518    apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm 
 133.519 -                       del: word_of_int_bin)
 133.520 +                       del: word_of_int_numeral)
 133.521    apply (simp add: mod_pos_pos_trivial)
 133.522    apply (subst mod_pos_pos_trivial)
 133.523      apply (rule bl_to_bin_ge0)
 133.524 @@ -2073,22 +2121,38 @@
 133.525    unfolding word_log_defs wils1 by simp_all
 133.526  
 133.527  lemma word_no_log_defs [simp]:
 133.528 -  "NOT number_of a = (number_of (NOT a) :: 'a::len0 word)"
 133.529 -  "number_of a AND number_of b = (number_of (a AND b) :: 'a word)"
 133.530 -  "number_of a OR number_of b = (number_of (a OR b) :: 'a word)"
 133.531 -  "number_of a XOR number_of b = (number_of (a XOR b) :: 'a word)"
 133.532 -  unfolding word_no_wi word_wi_log_defs by simp_all
 133.533 +  "NOT (numeral a) = word_of_int (NOT (numeral a))"
 133.534 +  "NOT (neg_numeral a) = word_of_int (NOT (neg_numeral a))"
 133.535 +  "numeral a AND numeral b = word_of_int (numeral a AND numeral b)"
 133.536 +  "numeral a AND neg_numeral b = word_of_int (numeral a AND neg_numeral b)"
 133.537 +  "neg_numeral a AND numeral b = word_of_int (neg_numeral a AND numeral b)"
 133.538 +  "neg_numeral a AND neg_numeral b = word_of_int (neg_numeral a AND neg_numeral b)"
 133.539 +  "numeral a OR numeral b = word_of_int (numeral a OR numeral b)"
 133.540 +  "numeral a OR neg_numeral b = word_of_int (numeral a OR neg_numeral b)"
 133.541 +  "neg_numeral a OR numeral b = word_of_int (neg_numeral a OR numeral b)"
 133.542 +  "neg_numeral a OR neg_numeral b = word_of_int (neg_numeral a OR neg_numeral b)"
 133.543 +  "numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)"
 133.544 +  "numeral a XOR neg_numeral b = word_of_int (numeral a XOR neg_numeral b)"
 133.545 +  "neg_numeral a XOR numeral b = word_of_int (neg_numeral a XOR numeral b)"
 133.546 +  "neg_numeral a XOR neg_numeral b = word_of_int (neg_numeral a XOR neg_numeral b)"
 133.547 +  unfolding word_numeral_alt word_neg_numeral_alt word_wi_log_defs by simp_all
 133.548  
 133.549  text {* Special cases for when one of the arguments equals 1. *}
 133.550  
 133.551  lemma word_bitwise_1_simps [simp]:
 133.552    "NOT (1::'a::len0 word) = -2"
 133.553 -  "(1::'a word) AND number_of b = number_of (Int.Bit1 Int.Pls AND b)"
 133.554 -  "number_of a AND (1::'a word) = number_of (a AND Int.Bit1 Int.Pls)"
 133.555 -  "(1::'a word) OR number_of b = number_of (Int.Bit1 Int.Pls OR b)"
 133.556 -  "number_of a OR (1::'a word) = number_of (a OR Int.Bit1 Int.Pls)"
 133.557 -  "(1::'a word) XOR number_of b = number_of (Int.Bit1 Int.Pls XOR b)"
 133.558 -  "number_of a XOR (1::'a word) = number_of (a XOR Int.Bit1 Int.Pls)"
 133.559 +  "1 AND numeral b = word_of_int (1 AND numeral b)"
 133.560 +  "1 AND neg_numeral b = word_of_int (1 AND neg_numeral b)"
 133.561 +  "numeral a AND 1 = word_of_int (numeral a AND 1)"
 133.562 +  "neg_numeral a AND 1 = word_of_int (neg_numeral a AND 1)"
 133.563 +  "1 OR numeral b = word_of_int (1 OR numeral b)"
 133.564 +  "1 OR neg_numeral b = word_of_int (1 OR neg_numeral b)"
 133.565 +  "numeral a OR 1 = word_of_int (numeral a OR 1)"
 133.566 +  "neg_numeral a OR 1 = word_of_int (neg_numeral a OR 1)"
 133.567 +  "1 XOR numeral b = word_of_int (1 XOR numeral b)"
 133.568 +  "1 XOR neg_numeral b = word_of_int (1 XOR neg_numeral b)"
 133.569 +  "numeral a XOR 1 = word_of_int (numeral a XOR 1)"
 133.570 +  "neg_numeral a XOR 1 = word_of_int (neg_numeral a XOR 1)"
 133.571    unfolding word_1_no word_no_log_defs by simp_all
 133.572  
 133.573  lemma uint_or: "uint (x OR y) = (uint x) OR (uint y)"
 133.574 @@ -2123,10 +2187,15 @@
 133.575    unfolding word_test_bit_def
 133.576    by (simp add: nth_bintr [symmetric] word_ubin.eq_norm)
 133.577  
 133.578 -lemma test_bit_no [simp]:
 133.579 -  "(number_of w :: 'a::len0 word) !! n \<longleftrightarrow>
 133.580 -    n < len_of TYPE('a) \<and> bin_nth (number_of w) n"
 133.581 -  unfolding word_number_of_alt test_bit_wi ..
 133.582 +lemma test_bit_numeral [simp]:
 133.583 +  "(numeral w :: 'a::len0 word) !! n \<longleftrightarrow>
 133.584 +    n < len_of TYPE('a) \<and> bin_nth (numeral w) n"
 133.585 +  unfolding word_numeral_alt test_bit_wi ..
 133.586 +
 133.587 +lemma test_bit_neg_numeral [simp]:
 133.588 +  "(neg_numeral w :: 'a::len0 word) !! n \<longleftrightarrow>
 133.589 +    n < len_of TYPE('a) \<and> bin_nth (neg_numeral w) n"
 133.590 +  unfolding word_neg_numeral_alt test_bit_wi ..
 133.591  
 133.592  lemma test_bit_1 [simp]: "(1::'a::len word) !! n \<longleftrightarrow> n = 0"
 133.593    unfolding word_1_wi test_bit_wi by auto
 133.594 @@ -2134,6 +2203,9 @@
 133.595  lemma nth_0 [simp]: "~ (0::'a::len0 word) !! n"
 133.596    unfolding word_test_bit_def by simp
 133.597  
 133.598 +lemma nth_minus1 [simp]: "(-1::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a)"
 133.599 +  unfolding word_test_bit_def by (simp add: nth_bintr)
 133.600 +
 133.601  (* get from commutativity, associativity etc of int_and etc
 133.602    to same for word_and etc *)
 133.603  
 133.604 @@ -2294,9 +2366,13 @@
 133.605    "msb (word_of_int x::'a::len word) = bin_nth x (len_of TYPE('a) - 1)"
 133.606    unfolding word_msb_def by (simp add: word_sbin.eq_norm bin_sign_lem)
 133.607  
 133.608 -lemma word_msb_no [simp]:
 133.609 -  "msb (number_of w::'a::len word) = bin_nth (number_of w) (len_of TYPE('a) - 1)"
 133.610 -  unfolding word_number_of_alt by (rule msb_word_of_int)
 133.611 +lemma word_msb_numeral [simp]:
 133.612 +  "msb (numeral w::'a::len word) = bin_nth (numeral w) (len_of TYPE('a) - 1)"
 133.613 +  unfolding word_numeral_alt by (rule msb_word_of_int)
 133.614 +
 133.615 +lemma word_msb_neg_numeral [simp]:
 133.616 +  "msb (neg_numeral w::'a::len word) = bin_nth (neg_numeral w) (len_of TYPE('a) - 1)"
 133.617 +  unfolding word_neg_numeral_alt by (rule msb_word_of_int)
 133.618  
 133.619  lemma word_msb_0 [simp]: "\<not> msb (0::'a::len word)"
 133.620    unfolding word_msb_def by simp
 133.621 @@ -2420,9 +2496,13 @@
 133.622    unfolding sint_uint l_def
 133.623    by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric])
 133.624  
 133.625 -lemma word_lsb_no [simp]:
 133.626 -  "lsb (number_of bin :: 'a :: len word) = (bin_last (number_of bin) = 1)"
 133.627 -  unfolding word_lsb_alt test_bit_no by auto
 133.628 +lemma word_lsb_numeral [simp]:
 133.629 +  "lsb (numeral bin :: 'a :: len word) = (bin_last (numeral bin) = 1)"
 133.630 +  unfolding word_lsb_alt test_bit_numeral by simp
 133.631 +
 133.632 +lemma word_lsb_neg_numeral [simp]:
 133.633 +  "lsb (neg_numeral bin :: 'a :: len word) = (bin_last (neg_numeral bin) = 1)"
 133.634 +  unfolding word_lsb_alt test_bit_neg_numeral by simp
 133.635  
 133.636  lemma set_bit_word_of_int:
 133.637    "set_bit (word_of_int x) n b = word_of_int (bin_sc n (if b then 1 else 0) x)"
 133.638 @@ -2431,10 +2511,15 @@
 133.639    apply (simp add: word_size bin_nth_sc_gen word_ubin.eq_norm nth_bintr)
 133.640    done
 133.641  
 133.642 -lemma word_set_no [simp]:
 133.643 -  "set_bit (number_of bin::'a::len0 word) n b = 
 133.644 -    word_of_int (bin_sc n (if b then 1 else 0) (number_of bin))"
 133.645 -  unfolding word_number_of_alt by (rule set_bit_word_of_int)
 133.646 +lemma word_set_numeral [simp]:
 133.647 +  "set_bit (numeral bin::'a::len0 word) n b = 
 133.648 +    word_of_int (bin_sc n (if b then 1 else 0) (numeral bin))"
 133.649 +  unfolding word_numeral_alt by (rule set_bit_word_of_int)
 133.650 +
 133.651 +lemma word_set_neg_numeral [simp]:
 133.652 +  "set_bit (neg_numeral bin::'a::len0 word) n b = 
 133.653 +    word_of_int (bin_sc n (if b then 1 else 0) (neg_numeral bin))"
 133.654 +  unfolding word_neg_numeral_alt by (rule set_bit_word_of_int)
 133.655  
 133.656  lemma word_set_bit_0 [simp]:
 133.657    "set_bit 0 n b = word_of_int (bin_sc n (if b then 1 else 0) 0)"
 133.658 @@ -2445,11 +2530,11 @@
 133.659    unfolding word_1_wi by (rule set_bit_word_of_int)
 133.660  
 133.661  lemma setBit_no [simp]:
 133.662 -  "setBit (number_of bin) n = word_of_int (bin_sc n 1 (number_of bin))"
 133.663 +  "setBit (numeral bin) n = word_of_int (bin_sc n 1 (numeral bin))"
 133.664    by (simp add: setBit_def)
 133.665  
 133.666  lemma clearBit_no [simp]:
 133.667 -  "clearBit (number_of bin) n = word_of_int (bin_sc n 0 (number_of bin))"
 133.668 +  "clearBit (numeral bin) n = word_of_int (bin_sc n 0 (numeral bin))"
 133.669    by (simp add: clearBit_def)
 133.670  
 133.671  lemma to_bl_n1: 
 133.672 @@ -2512,7 +2597,7 @@
 133.673     apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 
 133.674     apply (rule box_equals) 
 133.675       apply (rule_tac [2] bintr_ariths (1))+ 
 133.676 -   apply (clarsimp simp add : number_of_is_id)
 133.677 +   apply simp
 133.678    apply simp
 133.679    done
 133.680  
 133.681 @@ -2547,15 +2632,19 @@
 133.682  
 133.683  lemma shiftl1_wi [simp]: "shiftl1 (word_of_int w) = word_of_int (w BIT 0)"
 133.684    unfolding shiftl1_def
 133.685 -  apply (simp only: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm)
 133.686 +  apply (simp add: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm)
 133.687    apply (subst refl [THEN bintrunc_BIT_I, symmetric])
 133.688    apply (subst bintrunc_bintrunc_min)
 133.689    apply simp
 133.690    done
 133.691  
 133.692 -lemma shiftl1_number [simp] :
 133.693 -  "shiftl1 (number_of w) = number_of (Int.Bit0 w)"
 133.694 -  unfolding word_number_of_alt shiftl1_wi by simp
 133.695 +lemma shiftl1_numeral [simp]:
 133.696 +  "shiftl1 (numeral w) = numeral (Num.Bit0 w)"
 133.697 +  unfolding word_numeral_alt shiftl1_wi by simp
 133.698 +
 133.699 +lemma shiftl1_neg_numeral [simp]:
 133.700 +  "shiftl1 (neg_numeral w) = neg_numeral (Num.Bit0 w)"
 133.701 +  unfolding word_neg_numeral_alt shiftl1_wi by simp
 133.702  
 133.703  lemma shiftl1_0 [simp] : "shiftl1 0 = 0"
 133.704    unfolding shiftl1_def by simp
 133.705 @@ -2704,8 +2793,8 @@
 133.706  
 133.707  subsubsection "shift functions in terms of lists of bools"
 133.708  
 133.709 -lemmas bshiftr1_no_bin [simp] = 
 133.710 -  bshiftr1_def [where w="number_of w", unfolded to_bl_no_bin] for w
 133.711 +lemmas bshiftr1_numeral [simp] = 
 133.712 +  bshiftr1_def [where w="numeral w", unfolded to_bl_numeral] for w
 133.713  
 133.714  lemma bshiftr1_bl: "to_bl (bshiftr1 b w) = b # butlast (to_bl w)"
 133.715    unfolding bshiftr1_def by (rule word_bl.Abs_inverse) simp
 133.716 @@ -2858,7 +2947,7 @@
 133.717    finally show ?thesis .
 133.718  qed
 133.719  
 133.720 -lemmas shiftl_number [simp] = shiftl_def [where w="number_of w"] for w
 133.721 +lemmas shiftl_numeral [simp] = shiftl_def [where w="numeral w"] for w
 133.722  
 133.723  lemma bl_shiftl:
 133.724    "to_bl (w << n) = drop n (to_bl w) @ replicate (min (size w) n) False"
 133.725 @@ -2885,27 +2974,29 @@
 133.726    by (induct n) (auto simp: shiftl1_2t)
 133.727  
 133.728  lemma shiftr1_bintr [simp]:
 133.729 -  "(shiftr1 (number_of w) :: 'a :: len0 word) =
 133.730 -    word_of_int (bin_rest (bintrunc (len_of TYPE ('a)) (number_of w)))"
 133.731 -  unfolding shiftr1_def word_number_of_alt
 133.732 +  "(shiftr1 (numeral w) :: 'a :: len0 word) =
 133.733 +    word_of_int (bin_rest (bintrunc (len_of TYPE ('a)) (numeral w)))"
 133.734 +  unfolding shiftr1_def word_numeral_alt
 133.735    by (simp add: word_ubin.eq_norm)
 133.736  
 133.737  lemma sshiftr1_sbintr [simp]:
 133.738 -  "(sshiftr1 (number_of w) :: 'a :: len word) =
 133.739 -    word_of_int (bin_rest (sbintrunc (len_of TYPE ('a) - 1) (number_of w)))"
 133.740 -  unfolding sshiftr1_def word_number_of_alt
 133.741 +  "(sshiftr1 (numeral w) :: 'a :: len word) =
 133.742 +    word_of_int (bin_rest (sbintrunc (len_of TYPE ('a) - 1) (numeral w)))"
 133.743 +  unfolding sshiftr1_def word_numeral_alt
 133.744    by (simp add: word_sbin.eq_norm)
 133.745  
 133.746  lemma shiftr_no [simp]:
 133.747 -  "(number_of w::'a::len0 word) >> n = word_of_int
 133.748 -    ((bin_rest ^^ n) (bintrunc (len_of TYPE('a)) (number_of w)))"
 133.749 +  (* FIXME: neg_numeral *)
 133.750 +  "(numeral w::'a::len0 word) >> n = word_of_int
 133.751 +    ((bin_rest ^^ n) (bintrunc (len_of TYPE('a)) (numeral w)))"
 133.752    apply (rule word_eqI)
 133.753    apply (auto simp: nth_shiftr nth_rest_power_bin nth_bintr word_size)
 133.754    done
 133.755  
 133.756  lemma sshiftr_no [simp]:
 133.757 -  "(number_of w::'a::len word) >>> n = word_of_int
 133.758 -    ((bin_rest ^^ n) (sbintrunc (len_of TYPE('a) - 1) (number_of w)))"
 133.759 +  (* FIXME: neg_numeral *)
 133.760 +  "(numeral w::'a::len word) >>> n = word_of_int
 133.761 +    ((bin_rest ^^ n) (sbintrunc (len_of TYPE('a) - 1) (numeral w)))"
 133.762    apply (rule word_eqI)
 133.763    apply (auto simp: nth_sshiftr nth_rest_power_bin nth_sbintr word_size)
 133.764     apply (subgoal_tac "na + n = len_of TYPE('a) - Suc 0", simp, simp)+
 133.765 @@ -3016,8 +3107,8 @@
 133.766  lemma and_mask_wi: "word_of_int i AND mask n = word_of_int (bintrunc n i)"
 133.767    by (auto simp add: nth_bintr word_size word_ops_nth_size word_eq_iff)
 133.768  
 133.769 -lemma and_mask_no: "number_of i AND mask n = word_of_int (bintrunc n (number_of i))"
 133.770 -  unfolding word_number_of_alt by (rule and_mask_wi)
 133.771 +lemma and_mask_no: "numeral i AND mask n = word_of_int (bintrunc n (numeral i))"
 133.772 +  unfolding word_numeral_alt by (rule and_mask_wi)
 133.773  
 133.774  lemma bl_and_mask':
 133.775    "to_bl (w AND mask n :: 'a :: len word) = 
 133.776 @@ -3046,7 +3137,7 @@
 133.777    by (simp add: int_mod_lem eq_sym_conv)
 133.778  
 133.779  lemma mask_eq_iff: "(w AND mask n) = w <-> uint w < 2 ^ n"
 133.780 -  apply (simp add: and_mask_bintr word_number_of_def)
 133.781 +  apply (simp add: and_mask_bintr)
 133.782    apply (simp add: word_ubin.inverse_norm)
 133.783    apply (simp add: eq_mod_iff bintrunc_mod2p min_def)
 133.784    apply (fast intro!: lt2p_lem)
 133.785 @@ -3073,17 +3164,17 @@
 133.786  
 133.787  lemma word_2p_lem: 
 133.788    "n < size w \<Longrightarrow> w < 2 ^ n = (uint (w :: 'a :: len word) < 2 ^ n)"
 133.789 -  apply (unfold word_size word_less_alt word_number_of_alt)
 133.790 +  apply (unfold word_size word_less_alt word_numeral_alt)
 133.791    apply (clarsimp simp add: word_of_int_power_hom word_uint.eq_norm 
 133.792                              int_mod_eq'
 133.793 -                  simp del: word_of_int_bin)
 133.794 +                  simp del: word_of_int_numeral)
 133.795    done
 133.796  
 133.797  lemma less_mask_eq: "x < 2 ^ n \<Longrightarrow> x AND mask n = (x :: 'a :: len word)"
 133.798 -  apply (unfold word_less_alt word_number_of_alt)
 133.799 +  apply (unfold word_less_alt word_numeral_alt)
 133.800    apply (clarsimp simp add: and_mask_mod_2p word_of_int_power_hom 
 133.801                              word_uint.eq_norm
 133.802 -                  simp del: word_of_int_bin)
 133.803 +                  simp del: word_of_int_numeral)
 133.804    apply (drule xtr8 [rotated])
 133.805    apply (rule int_mod_le)
 133.806    apply (auto simp add : mod_pos_pos_trivial)
 133.807 @@ -3126,7 +3217,7 @@
 133.808  
 133.809  lemmas revcast_def' = revcast_def [simplified]
 133.810  lemmas revcast_def'' = revcast_def' [simplified word_size]
 133.811 -lemmas revcast_no_def [simp] = revcast_def' [where w="number_of w", unfolded word_size] for w
 133.812 +lemmas revcast_no_def [simp] = revcast_def' [where w="numeral w", unfolded word_size] for w
 133.813  
 133.814  lemma to_bl_revcast: 
 133.815    "to_bl (revcast w :: 'a :: len0 word) = 
 133.816 @@ -3240,13 +3331,13 @@
 133.817  subsubsection "Slices"
 133.818  
 133.819  lemma slice1_no_bin [simp]:
 133.820 -  "slice1 n (number_of w :: 'b word) = of_bl (takefill False n (bin_to_bl (len_of TYPE('b :: len0)) (number_of w)))"
 133.821 -  by (simp add: slice1_def)
 133.822 +  "slice1 n (numeral w :: 'b word) = of_bl (takefill False n (bin_to_bl (len_of TYPE('b :: len0)) (numeral w)))"
 133.823 +  by (simp add: slice1_def) (* TODO: neg_numeral *)
 133.824  
 133.825  lemma slice_no_bin [simp]:
 133.826 -  "slice n (number_of w :: 'b word) = of_bl (takefill False (len_of TYPE('b :: len0) - n)
 133.827 -    (bin_to_bl (len_of TYPE('b :: len0)) (number_of w)))"
 133.828 -  by (simp add: slice_def word_size)
 133.829 +  "slice n (numeral w :: 'b word) = of_bl (takefill False (len_of TYPE('b :: len0) - n)
 133.830 +    (bin_to_bl (len_of TYPE('b :: len0)) (numeral w)))"
 133.831 +  by (simp add: slice_def word_size) (* TODO: neg_numeral *)
 133.832  
 133.833  lemma slice1_0 [simp] : "slice1 n 0 = 0"
 133.834    unfolding slice1_def by simp
 133.835 @@ -3383,9 +3474,9 @@
 133.836  lemmas word_cat_bin' = word_cat_def
 133.837  
 133.838  lemma word_rsplit_no:
 133.839 -  "(word_rsplit (number_of bin :: 'b :: len0 word) :: 'a word list) = 
 133.840 +  "(word_rsplit (numeral bin :: 'b :: len0 word) :: 'a word list) = 
 133.841      map word_of_int (bin_rsplit (len_of TYPE('a :: len)) 
 133.842 -      (len_of TYPE('b), bintrunc (len_of TYPE('b)) (number_of bin)))"
 133.843 +      (len_of TYPE('b), bintrunc (len_of TYPE('b)) (numeral bin)))"
 133.844    unfolding word_rsplit_def by (simp add: word_ubin.eq_norm)
 133.845  
 133.846  lemmas word_rsplit_no_cl [simp] = word_rsplit_no
 133.847 @@ -3580,15 +3671,14 @@
 133.848    done
 133.849  
 133.850  lemmas word_cat_bl_no_bin [simp] =
 133.851 -  word_cat_bl [where a="number_of a" 
 133.852 -                 and b="number_of b", 
 133.853 -               unfolded to_bl_no_bin]
 133.854 -  for a b
 133.855 +  word_cat_bl [where a="numeral a" and b="numeral b",
 133.856 +    unfolded to_bl_numeral]
 133.857 +  for a b (* FIXME: negative numerals, 0 and 1 *)
 133.858  
 133.859  lemmas word_split_bl_no_bin [simp] =
 133.860 -  word_split_bl_eq [where c="number_of c", unfolded to_bl_no_bin] for c
 133.861 -
 133.862 --- {* this odd result arises from the fact that the statement of the
 133.863 +  word_split_bl_eq [where c="numeral c", unfolded to_bl_numeral] for c
 133.864 +
 133.865 +text {* this odd result arises from the fact that the statement of the
 133.866        result implies that the decoded words are of the same type, 
 133.867        and therefore of the same length, as the original word *}
 133.868  
 133.869 @@ -3962,7 +4052,7 @@
 133.870  
 133.871  lemma word_rotr_rev:
 133.872    "word_rotr n w = word_reverse (word_rotl n (word_reverse w))"
 133.873 -  by (simp add: word_bl.Rep_inject [symmetric] to_bl_word_rev
 133.874 +  by (simp only: word_bl.Rep_inject [symmetric] to_bl_word_rev
 133.875                  to_bl_rotr to_bl_rotl rotater_rev)
 133.876    
 133.877  lemma word_roti_0 [simp]: "word_roti 0 w = w"
 133.878 @@ -4093,10 +4183,12 @@
 133.879    unfolding word_roti_def by auto
 133.880  
 133.881  lemmas word_rotr_dt_no_bin' [simp] = 
 133.882 -  word_rotr_dt [where w="number_of w", unfolded to_bl_no_bin] for w
 133.883 +  word_rotr_dt [where w="numeral w", unfolded to_bl_numeral] for w
 133.884 +  (* FIXME: negative numerals, 0 and 1 *)
 133.885  
 133.886  lemmas word_rotl_dt_no_bin' [simp] = 
 133.887 -  word_rotl_dt [where w="number_of w", unfolded to_bl_no_bin] for w
 133.888 +  word_rotl_dt [where w="numeral w", unfolded to_bl_numeral] for w
 133.889 +  (* FIXME: negative numerals, 0 and 1 *)
 133.890  
 133.891  declare word_roti_def [simp]
 133.892  
 133.893 @@ -4119,8 +4211,7 @@
 133.894       (simp add: max_word_def word_le_def int_word_uint int_mod_eq')
 133.895    
 133.896  lemma word_of_int_2p_len: "word_of_int (2 ^ len_of TYPE('a)) = (0::'a::len0 word)"
 133.897 -  by (subst word_uint.Abs_norm [symmetric]) 
 133.898 -     (simp add: word_of_int_hom_syms)
 133.899 +  by (subst word_uint.Abs_norm [symmetric]) simp
 133.900  
 133.901  lemma word_pow_0:
 133.902    "(2::'a::len word) ^ len_of TYPE('a) = 0"
 133.903 @@ -4304,10 +4395,7 @@
 133.904  lemma word_neq_0_conv:
 133.905    fixes w :: "'a :: len word"
 133.906    shows "(w \<noteq> 0) = (0 < w)"
 133.907 -proof -
 133.908 -  have "0 \<le> w" by (rule word_zero_le)
 133.909 -  thus ?thesis by (auto simp add: word_less_def)
 133.910 -qed
 133.911 +  unfolding word_gt_0 by simp
 133.912  
 133.913  lemma max_lt:
 133.914    "unat (max a b div c) = unat (max a b) div unat (c:: 'a :: len word)"
 133.915 @@ -4335,8 +4423,8 @@
 133.916    "b <= a \<Longrightarrow> unat (a - b) = unat a - unat b"
 133.917    by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib)
 133.918  
 133.919 -lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "number_of w"] for w
 133.920 -lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "number_of w"] for w
 133.921 +lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "numeral w"] for w
 133.922 +lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "numeral w"] for w
 133.923    
 133.924  lemma word_of_int_minus: 
 133.925    "word_of_int (2^len_of TYPE('a) - i) = (word_of_int (-i)::'a::len word)"
 133.926 @@ -4354,7 +4442,7 @@
 133.927  
 133.928  lemma word_le_less_eq:
 133.929    "(x ::'z::len word) \<le> y = (x = y \<or> x < y)"
 133.930 -  by (auto simp add: word_less_def)
 133.931 +  by (auto simp add: order_class.le_less)
 133.932  
 133.933  lemma mod_plus_cong:
 133.934    assumes 1: "(b::int) = b'"
 133.935 @@ -4523,17 +4611,15 @@
 133.936    "1 + n \<noteq> (0::'a::len word) \<Longrightarrow> unat (1 + n) = Suc (unat n)"
 133.937    by unat_arith
 133.938  
 133.939 -
 133.940  lemma word_no_1 [simp]: "(Numeral1::'a::len0 word) = 1"
 133.941    by (fact word_1_no [symmetric])
 133.942  
 133.943 -lemma word_no_0 [simp]: "(Numeral0::'a::len0 word) = 0"
 133.944 -  by (fact word_0_no [symmetric])
 133.945 -
 133.946  declare bin_to_bl_def [simp]
 133.947  
 133.948  
 133.949  use "~~/src/HOL/Word/Tools/smt_word.ML"
 133.950  setup {* SMT_Word.setup *}
 133.951  
 133.952 +hide_const (open) Word
 133.953 +
 133.954  end
   134.1 --- a/src/HOL/ex/Arith_Examples.thy	Fri Mar 23 20:32:43 2012 +0100
   134.2 +++ b/src/HOL/ex/Arith_Examples.thy	Mon Mar 26 10:56:56 2012 +0200
   134.3 @@ -218,10 +218,10 @@
   134.4  lemma "(0::int) < 1"
   134.5    by linarith
   134.6  
   134.7 -lemma "(47::nat) + 11 < 08 * 15"
   134.8 +lemma "(47::nat) + 11 < 8 * 15"
   134.9    by linarith
  134.10  
  134.11 -lemma "(47::int) + 11 < 08 * 15"
  134.12 +lemma "(47::int) + 11 < 8 * 15"
  134.13    by linarith
  134.14  
  134.15  text {* Splitting of inequalities of different type. *}
   135.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   135.2 +++ b/src/HOL/ex/Code_Nat_examples.thy	Mon Mar 26 10:56:56 2012 +0200
   135.3 @@ -0,0 +1,57 @@
   135.4 +(*  Title:      HOL/ex/Code_Nat_examples.thy
   135.5 +    Author:     Florian Haftmann, TU Muenchen
   135.6 +*)
   135.7 +
   135.8 +header {* Simple examples for Code\_Numeral\_Nat theory. *}
   135.9 +
  135.10 +theory Code_Nat_examples
  135.11 +imports Complex_Main "~~/src/HOL/Library/Efficient_Nat"
  135.12 +begin
  135.13 +
  135.14 +fun to_n :: "nat \<Rightarrow> nat list"
  135.15 +where
  135.16 +  "to_n 0 = []"
  135.17 +| "to_n (Suc 0) = []"
  135.18 +| "to_n (Suc (Suc 0)) = []"
  135.19 +| "to_n (Suc n) = n # to_n n"
  135.20 +
  135.21 +definition naive_prime :: "nat \<Rightarrow> bool"
  135.22 +where
  135.23 +  "naive_prime n \<longleftrightarrow> n \<ge> 2 \<and> filter (\<lambda>m. n mod m = 0) (to_n n) = []"
  135.24 +
  135.25 +primrec fac :: "nat \<Rightarrow> nat"
  135.26 +where
  135.27 +  "fac 0 = 1"
  135.28 +| "fac (Suc n) = Suc n * fac n"
  135.29 +
  135.30 +primrec harmonic :: "nat \<Rightarrow> rat"
  135.31 +where
  135.32 +  "harmonic 0 = 0"
  135.33 +| "harmonic (Suc n) = 1 / of_nat (Suc n) + harmonic n"
  135.34 +
  135.35 +lemma "harmonic 200 \<ge> 5"
  135.36 +  by eval
  135.37 +
  135.38 +lemma "(let (q, r) = quotient_of (harmonic 8) in q div r) \<ge> 2"
  135.39 +  by normalization
  135.40 +
  135.41 +lemma "naive_prime 89"
  135.42 +  by eval
  135.43 +
  135.44 +lemma "naive_prime 89"
  135.45 +  by normalization
  135.46 +
  135.47 +lemma "\<not> naive_prime 87"
  135.48 +  by eval
  135.49 +
  135.50 +lemma "\<not> naive_prime 87"
  135.51 +  by normalization
  135.52 +
  135.53 +lemma "fac 10 > 3000000"
  135.54 +  by eval
  135.55 +
  135.56 +lemma "fac 10 > 3000000"
  135.57 +  by normalization
  135.58 +
  135.59 +end
  135.60 +
   136.1 --- a/src/HOL/ex/Dedekind_Real.thy	Fri Mar 23 20:32:43 2012 +0100
   136.2 +++ b/src/HOL/ex/Dedekind_Real.thy	Mon Mar 26 10:56:56 2012 +0200
   136.3 @@ -1658,19 +1658,6 @@
   136.4  by (blast intro!: real_less_all_preal linorder_not_less [THEN iffD1])
   136.5  
   136.6  
   136.7 -subsection{*Numerals and Arithmetic*}
   136.8 -
   136.9 -instantiation real :: number_ring
  136.10 -begin
  136.11 -
  136.12 -definition
  136.13 -  real_number_of_def: "(number_of w :: real) = of_int w"
  136.14 -
  136.15 -instance
  136.16 -  by intro_classes (simp add: real_number_of_def)
  136.17 -
  136.18 -end
  136.19 -
  136.20  subsection {* Completeness of Positive Reals *}
  136.21  
  136.22  text {*
   137.1 --- a/src/HOL/ex/Efficient_Nat_examples.thy	Fri Mar 23 20:32:43 2012 +0100
   137.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
   137.3 @@ -1,56 +0,0 @@
   137.4 -(*  Title:      HOL/ex/Efficient_Nat_examples.thy
   137.5 -    Author:     Florian Haftmann, TU Muenchen
   137.6 -*)
   137.7 -
   137.8 -header {* Simple examples for Efficient\_Nat theory. *}
   137.9 -
  137.10 -theory Efficient_Nat_examples
  137.11 -imports Complex_Main "~~/src/HOL/Library/Efficient_Nat"
  137.12 -begin
  137.13 -
  137.14 -fun to_n :: "nat \<Rightarrow> nat list" where
  137.15 -  "to_n 0 = []"
  137.16 -  | "to_n (Suc 0) = []"
  137.17 -  | "to_n (Suc (Suc 0)) = []"
  137.18 -  | "to_n (Suc n) = n # to_n n"
  137.19 -
  137.20 -definition naive_prime :: "nat \<Rightarrow> bool" where
  137.21 -  "naive_prime n \<longleftrightarrow> n \<ge> 2 \<and> filter (\<lambda>m. n mod m = 0) (to_n n) = []"
  137.22 -
  137.23 -primrec fac :: "nat \<Rightarrow> nat" where
  137.24 -  "fac 0 = 1"
  137.25 -  | "fac (Suc n) = Suc n * fac n"
  137.26 -
  137.27 -primrec rat_of_nat :: "nat \<Rightarrow> rat" where
  137.28 -  "rat_of_nat 0 = 0"
  137.29 -  | "rat_of_nat (Suc n) = rat_of_nat n + 1"
  137.30 -
  137.31 -primrec harmonic :: "nat \<Rightarrow> rat" where
  137.32 -  "harmonic 0 = 0"
  137.33 -  | "harmonic (Suc n) = 1 / rat_of_nat (Suc n) + harmonic n"
  137.34 -
  137.35 -lemma "harmonic 200 \<ge> 5"
  137.36 -  by eval
  137.37 -
  137.38 -lemma "harmonic 20 \<ge> 3"
  137.39 -  by normalization
  137.40 -
  137.41 -lemma "naive_prime 89"
  137.42 -  by eval
  137.43 -
  137.44 -lemma "naive_prime 89"
  137.45 -  by normalization
  137.46 -
  137.47 -lemma "\<not> naive_prime 87"
  137.48 -  by eval
  137.49 -
  137.50 -lemma "\<not> naive_prime 87"
  137.51 -  by normalization
  137.52 -
  137.53 -lemma "fac 10 > 3000000"
  137.54 -  by eval
  137.55 -
  137.56 -lemma "fac 10 > 3000000"
  137.57 -  by normalization
  137.58 -
  137.59 -end
   138.1 --- a/src/HOL/ex/Executable_Relation.thy	Fri Mar 23 20:32:43 2012 +0100
   138.2 +++ b/src/HOL/ex/Executable_Relation.thy	Mon Mar 26 10:56:56 2012 +0200
   138.3 @@ -12,6 +12,7 @@
   138.4    "(x, y) : (rel_raw X R) = ((x = y \<and> x : X) \<or> (x, y) : R)"
   138.5  unfolding rel_raw_def by auto
   138.6  
   138.7 +
   138.8  lemma Id_raw:
   138.9    "Id = rel_raw UNIV {}"
  138.10  unfolding rel_raw_def by auto
  138.11 @@ -74,36 +75,34 @@
  138.12  
  138.13  lemmas rel_raw_of_set_eqI[intro!] = arg_cong[where f="rel_of_set"]
  138.14  
  138.15 -definition rel :: "'a set => ('a * 'a) set => 'a rel"
  138.16 -where
  138.17 -  "rel X R = rel_of_set (rel_raw X R)"
  138.18 +quotient_definition rel where "rel :: 'a set => ('a * 'a) set => 'a rel" is rel_raw done
  138.19  
  138.20  subsubsection {* Constant definitions on relations *}
  138.21  
  138.22  hide_const (open) converse rel_comp rtrancl Image
  138.23  
  138.24  quotient_definition member :: "'a * 'a => 'a rel => bool" where
  138.25 -  "member" is "Set.member :: 'a * 'a => ('a * 'a) set => bool"
  138.26 +  "member" is "Set.member :: 'a * 'a => ('a * 'a) set => bool" done
  138.27  
  138.28  quotient_definition converse :: "'a rel => 'a rel"
  138.29  where
  138.30 -  "converse" is "Relation.converse :: ('a * 'a) set => ('a * 'a) set"
  138.31 +  "converse" is "Relation.converse :: ('a * 'a) set => ('a * 'a) set" done
  138.32  
  138.33  quotient_definition union :: "'a rel => 'a rel => 'a rel"
  138.34  where
  138.35 -  "union" is "Set.union :: ('a * 'a) set => ('a * 'a) set => ('a * 'a) set"
  138.36 +  "union" is "Set.union :: ('a * 'a) set => ('a * 'a) set => ('a * 'a) set" done
  138.37  
  138.38  quotient_definition rel_comp :: "'a rel => 'a rel => 'a rel"
  138.39  where
  138.40 -  "rel_comp" is "Relation.rel_comp :: ('a * 'a) set => ('a * 'a) set => ('a * 'a) set"
  138.41 +  "rel_comp" is "Relation.rel_comp :: ('a * 'a) set => ('a * 'a) set => ('a * 'a) set" done
  138.42  
  138.43  quotient_definition rtrancl :: "'a rel => 'a rel"
  138.44  where
  138.45 -  "rtrancl" is "Transitive_Closure.rtrancl :: ('a * 'a) set => ('a * 'a) set"
  138.46 +  "rtrancl" is "Transitive_Closure.rtrancl :: ('a * 'a) set => ('a * 'a) set" done
  138.47  
  138.48  quotient_definition Image :: "'a rel => 'a set => 'a set"
  138.49  where
  138.50 -  "Image" is "Relation.Image :: ('a * 'a) set => 'a set => 'a set"
  138.51 +  "Image" is "Relation.Image :: ('a * 'a) set => 'a set => 'a set" done
  138.52  
  138.53  subsubsection {* Code generation *}
  138.54  
  138.55 @@ -111,33 +110,27 @@
  138.56  
  138.57  lemma [code]:
  138.58    "member (x, y) (rel X R) = ((x = y \<and> x : X) \<or> (x, y) : R)"
  138.59 -unfolding rel_def member_def
  138.60 -by (simp add: member_raw)
  138.61 +by (lifting member_raw)
  138.62  
  138.63  lemma [code]:
  138.64    "converse (rel X R) = rel X (R^-1)"
  138.65 -unfolding rel_def converse_def
  138.66 -by (simp add: converse_raw)
  138.67 +by (lifting converse_raw)
  138.68  
  138.69  lemma [code]:
  138.70    "union (rel X R) (rel Y S) = rel (X Un Y) (R Un S)"
  138.71 -unfolding rel_def union_def
  138.72 -by (simp add: union_raw)
  138.73 +by (lifting union_raw)
  138.74  
  138.75  lemma [code]:
  138.76     "rel_comp (rel X R) (rel Y S) = rel (X Int Y) (Set.project (%(x, y). y : Y) R Un (Set.project (%(x, y). x : X) S Un R O S))"
  138.77 -unfolding rel_def rel_comp_def
  138.78 -by (simp add: rel_comp_raw)
  138.79 +by (lifting rel_comp_raw)
  138.80  
  138.81  lemma [code]:
  138.82    "rtrancl (rel X R) = rel UNIV (R^+)"
  138.83 -unfolding rel_def rtrancl_def
  138.84 -by (simp add: rtrancl_raw)
  138.85 +by (lifting rtrancl_raw)
  138.86  
  138.87  lemma [code]:
  138.88    "Image (rel X R) S = (X Int S) Un (R `` S)"
  138.89 -unfolding rel_def Image_def
  138.90 -by (simp add: Image_raw)
  138.91 +by (lifting Image_raw)
  138.92  
  138.93  quickcheck_generator rel constructors: rel
  138.94  
   139.1 --- a/src/HOL/ex/Groebner_Examples.thy	Fri Mar 23 20:32:43 2012 +0100
   139.2 +++ b/src/HOL/ex/Groebner_Examples.thy	Mon Mar 26 10:56:56 2012 +0200
   139.3 @@ -31,7 +31,7 @@
   139.4      (Conv.arg_conv (Conv.arg1_conv (Semiring_Normalizer.semiring_normalize_conv @{context})))) *})
   139.5    by (rule refl)
   139.6  
   139.7 -lemma "((-3) ^ (Suc (Suc (Suc 0)))) == (X::'a::{number_ring})"
   139.8 +lemma "((-3) ^ (Suc (Suc (Suc 0)))) == (X::'a::{comm_ring_1})"
   139.9    apply (simp only: power_Suc power_0)
  139.10    apply (simp only: semiring_norm)
  139.11    oops
  139.12 @@ -58,7 +58,7 @@
  139.13    by algebra
  139.14  
  139.15  lemma
  139.16 -  fixes x::"'a::{idom,number_ring}"
  139.17 +  fixes x::"'a::{idom}"
  139.18    shows "x^2*y = x^2 & x*y^2 = y^2 \<longleftrightarrow>  x=1 & y=1 | x=0 & y=0"
  139.19    by algebra
  139.20  
  139.21 @@ -69,7 +69,7 @@
  139.22    "sq x == x*x"
  139.23  
  139.24  lemma
  139.25 -  fixes x1 :: "'a::{idom,number_ring}"
  139.26 +  fixes x1 :: "'a::{idom}"
  139.27    shows
  139.28    "(sq x1 + sq x2 + sq x3 + sq x4) * (sq y1 + sq y2 + sq y3 + sq y4) =
  139.29      sq (x1*y1 - x2*y2 - x3*y3 - x4*y4)  +
  139.30 @@ -79,7 +79,7 @@
  139.31    by (algebra add: sq_def)
  139.32  
  139.33  lemma
  139.34 -  fixes p1 :: "'a::{idom,number_ring}"
  139.35 +  fixes p1 :: "'a::{idom}"
  139.36    shows
  139.37    "(sq p1 + sq q1 + sq r1 + sq s1 + sq t1 + sq u1 + sq v1 + sq w1) *
  139.38     (sq p2 + sq q2 + sq r2 + sq s2 + sq t2 + sq u2 + sq v2 + sq w2)
   140.1 --- a/src/HOL/ex/Numeral_Representation.thy	Fri Mar 23 20:32:43 2012 +0100
   140.2 +++ b/src/HOL/ex/Numeral_Representation.thy	Mon Mar 26 10:56:56 2012 +0200
   140.3 @@ -2,7 +2,7 @@
   140.4      Author:     Florian Haftmann
   140.5  *)
   140.6  
   140.7 -header {* An experimental alternative numeral representation. *}
   140.8 +header {* First experiments for a numeral representation (now obsolete). *}
   140.9  
  140.10  theory Numeral_Representation
  140.11  imports Main
  140.12 @@ -498,7 +498,7 @@
  140.13    by (simp add: less_imp_le minus_of_num_less_one_iff)
  140.14  
  140.15  lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n"
  140.16 -  by (simp add: less_imp_le minus_one_less_of_num_iff)
  140.17 +  by (simp only: less_imp_le minus_one_less_of_num_iff)
  140.18  
  140.19  lemma minus_one_le_one_iff: "- 1 \<le> 1"
  140.20    by (simp add: less_imp_le minus_one_less_one_iff)
  140.21 @@ -510,7 +510,7 @@
  140.22    by (simp add: not_le minus_of_num_less_one_iff)
  140.23  
  140.24  lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1"
  140.25 -  by (simp add: not_le minus_one_less_of_num_iff)
  140.26 +  by (simp only: not_le minus_one_less_of_num_iff)
  140.27  
  140.28  lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1"
  140.29    by (simp add: not_le minus_one_less_one_iff)
  140.30 @@ -522,10 +522,10 @@
  140.31    by (simp add: not_less minus_of_num_le_one_iff)
  140.32  
  140.33  lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1"
  140.34 -  by (simp add: not_less minus_one_le_of_num_iff)
  140.35 +  by (simp only: not_less minus_one_le_of_num_iff)
  140.36  
  140.37  lemma one_less_minus_one_iff: "\<not> 1 < - 1"
  140.38 -  by (simp add: not_less minus_one_le_one_iff)
  140.39 +  by (simp only: not_less minus_one_le_one_iff)
  140.40  
  140.41  lemmas le_signed_numeral_special [numeral] =
  140.42    minus_of_num_le_of_num_iff
  140.43 @@ -835,10 +835,7 @@
  140.44  
  140.45  text {* Reversing standard setup *}
  140.46  
  140.47 -lemma [code_unfold del]: "(0::int) \<equiv> Numeral0" by simp
  140.48  lemma [code_unfold del]: "(1::int) \<equiv> Numeral1" by simp
  140.49 -declare zero_is_num_zero [code_unfold del]
  140.50 -declare one_is_num_one [code_unfold del]
  140.51    
  140.52  lemma [code, code del]:
  140.53    "(1 :: int) = 1"
  140.54 @@ -970,147 +967,5 @@
  140.55  
  140.56  hide_const (open) sub dup
  140.57  
  140.58 -text {* Pretty literals *}
  140.59 +end
  140.60  
  140.61 -ML {*
  140.62 -local open Code_Thingol in
  140.63 -
  140.64 -fun add_code print target =
  140.65 -  let
  140.66 -    fun dest_num one' dig0' dig1' thm =
  140.67 -      let
  140.68 -        fun dest_dig (IConst (c, _)) = if c = dig0' then 0
  140.69 -              else if c = dig1' then 1
  140.70 -              else Code_Printer.eqn_error thm "Illegal numeral expression: illegal dig"
  140.71 -          | dest_dig _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal digit";
  140.72 -        fun dest_num (IConst (c, _)) = if c = one' then 1
  140.73 -              else Code_Printer.eqn_error thm "Illegal numeral expression: illegal leading digit"
  140.74 -          | dest_num (t1 `$ t2) = 2 * dest_num t2 + dest_dig t1
  140.75 -          | dest_num _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal term";
  140.76 -      in dest_num end;
  140.77 -    fun pretty sgn literals [one', dig0', dig1'] _ thm _ _ [(t, _)] =
  140.78 -      (Code_Printer.str o print literals o sgn o dest_num one' dig0' dig1' thm) t
  140.79 -    fun add_syntax (c, sgn) = Code_Target.add_const_syntax target c
  140.80 -      (SOME (Code_Printer.complex_const_syntax
  140.81 -        (1, ([@{const_name One}, @{const_name Dig0}, @{const_name Dig1}],
  140.82 -          pretty sgn))));
  140.83 -  in
  140.84 -    add_syntax (@{const_name Pls}, I)
  140.85 -    #> add_syntax (@{const_name Mns}, (fn k => ~ k))
  140.86 -  end;
  140.87 -
  140.88 -end
  140.89 -*}
  140.90 -
  140.91 -hide_const (open) One Dig0 Dig1
  140.92 -
  140.93 -
  140.94 -subsection {* Toy examples *}
  140.95 -
  140.96 -definition "foo \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat)"
  140.97 -definition "bar \<longleftrightarrow> #4 * #2 + #7 \<ge> (#8 :: int) - #3"
  140.98 -
  140.99 -code_thms foo bar
 140.100 -export_code foo bar checking SML OCaml? Haskell? Scala?
 140.101 -
 140.102 -text {* This is an ad-hoc @{text Code_Integer} setup. *}
 140.103 -
 140.104 -setup {*
 140.105 -  fold (add_code Code_Printer.literal_numeral)
 140.106 -    [Code_ML.target_SML, Code_ML.target_OCaml, Code_Haskell.target, Code_Scala.target]
 140.107 -*}
 140.108 -
 140.109 -code_type int
 140.110 -  (SML "IntInf.int")
 140.111 -  (OCaml "Big'_int.big'_int")
 140.112 -  (Haskell "Integer")
 140.113 -  (Scala "BigInt")
 140.114 -  (Eval "int")
 140.115 -
 140.116 -code_const "0::int"
 140.117 -  (SML "0/ :/ IntInf.int")
 140.118 -  (OCaml "Big'_int.zero")
 140.119 -  (Haskell "0")
 140.120 -  (Scala "BigInt(0)")
 140.121 -  (Eval "0/ :/ int")
 140.122 -
 140.123 -code_const Int.pred
 140.124 -  (SML "IntInf.- ((_), 1)")
 140.125 -  (OCaml "Big'_int.pred'_big'_int")
 140.126 -  (Haskell "!(_/ -/ 1)")
 140.127 -  (Scala "!(_ -/ 1)")
 140.128 -  (Eval "!(_/ -/ 1)")
 140.129 -
 140.130 -code_const Int.succ
 140.131 -  (SML "IntInf.+ ((_), 1)")
 140.132 -  (OCaml "Big'_int.succ'_big'_int")
 140.133 -  (Haskell "!(_/ +/ 1)")
 140.134 -  (Scala "!(_ +/ 1)")
 140.135 -  (Eval "!(_/ +/ 1)")
 140.136 -
 140.137 -code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
 140.138 -  (SML "IntInf.+ ((_), (_))")
 140.139 -  (OCaml "Big'_int.add'_big'_int")
 140.140 -  (Haskell infixl 6 "+")
 140.141 -  (Scala infixl 7 "+")
 140.142 -  (Eval infixl 8 "+")
 140.143 -
 140.144 -code_const "uminus \<Colon> int \<Rightarrow> int"
 140.145 -  (SML "IntInf.~")
 140.146 -  (OCaml "Big'_int.minus'_big'_int")
 140.147 -  (Haskell "negate")
 140.148 -  (Scala "!(- _)")
 140.149 -  (Eval "~/ _")
 140.150 -
 140.151 -code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
 140.152 -  (SML "IntInf.- ((_), (_))")
 140.153 -  (OCaml "Big'_int.sub'_big'_int")
 140.154 -  (Haskell infixl 6 "-")
 140.155 -  (Scala infixl 7 "-")
 140.156 -  (Eval infixl 8 "-")
 140.157 -
 140.158 -code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
 140.159 -  (SML "IntInf.* ((_), (_))")
 140.160 -  (OCaml "Big'_int.mult'_big'_int")
 140.161 -  (Haskell infixl 7 "*")
 140.162 -  (Scala infixl 8 "*")
 140.163 -  (Eval infixl 9 "*")
 140.164 -
 140.165 -code_const pdivmod
 140.166 -  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
 140.167 -  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
 140.168 -  (Haskell "divMod/ (abs _)/ (abs _)")
 140.169 -  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
 140.170 -  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
 140.171 -
 140.172 -code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
 140.173 -  (SML "!((_ : IntInf.int) = _)")
 140.174 -  (OCaml "Big'_int.eq'_big'_int")
 140.175 -  (Haskell infix 4 "==")
 140.176 -  (Scala infixl 5 "==")
 140.177 -  (Eval infixl 6 "=")
 140.178 -
 140.179 -code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
 140.180 -  (SML "IntInf.<= ((_), (_))")
 140.181 -  (OCaml "Big'_int.le'_big'_int")
 140.182 -  (Haskell infix 4 "<=")
 140.183 -  (Scala infixl 4 "<=")
 140.184 -  (Eval infixl 6 "<=")
 140.185 -
 140.186 -code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
 140.187 -  (SML "IntInf.< ((_), (_))")
 140.188 -  (OCaml "Big'_int.lt'_big'_int")
 140.189 -  (Haskell infix 4 "<")
 140.190 -  (Scala infixl 4 "<")
 140.191 -  (Eval infixl 6 "<")
 140.192 -
 140.193 -code_const Code_Numeral.int_of
 140.194 -  (SML "IntInf.fromInt")
 140.195 -  (OCaml "_")
 140.196 -  (Haskell "toInteger")
 140.197 -  (Scala "!_.as'_BigInt")
 140.198 -  (Eval "_")
 140.199 -
 140.200 -export_code foo bar checking SML OCaml? Haskell? Scala?
 140.201 -
 140.202 -end
   141.1 --- a/src/HOL/ex/ROOT.ML	Fri Mar 23 20:32:43 2012 +0100
   141.2 +++ b/src/HOL/ex/ROOT.ML	Mon Mar 26 10:56:56 2012 +0200
   141.3 @@ -5,7 +5,7 @@
   141.4  
   141.5  no_document use_thys [
   141.6    "~~/src/HOL/Library/State_Monad",
   141.7 -  "Efficient_Nat_examples",
   141.8 +  "Code_Nat_examples",
   141.9    "~~/src/HOL/Library/FuncSet",
  141.10    "Eval_Examples",
  141.11    "Normalization_by_Evaluation",
   142.1 --- a/src/HOL/ex/ReflectionEx.thy	Fri Mar 23 20:32:43 2012 +0100
   142.2 +++ b/src/HOL/ex/ReflectionEx.thy	Mon Mar 26 10:56:56 2012 +0200
   142.3 @@ -143,7 +143,7 @@
   142.4  oops
   142.5  text{* Hmmm let's specialize @{text Inum_C} with numerals.*}
   142.6  
   142.7 -lemma Inum_number: "Inum (C (number_of t)) vs = number_of t" by simp
   142.8 +lemma Inum_number: "Inum (C (numeral t)) vs = numeral t" by simp
   142.9  lemmas Inum_eqs = Inum_Var Inum_Add Inum_Mul Inum_CN Inum_number
  142.10  
  142.11    text{* Second attempt *}
  142.12 @@ -155,7 +155,7 @@
  142.13  lemma "1 * (2* x + (y::nat) + 0 + 1) \<noteq> 0"
  142.14    apply (reify Inum_eqs ("1 * (2*x + (y::nat) + 0 + 1)"))
  142.15  oops
  142.16 -  text{* Oh!! 0 is not a variable \dots\ Oh! 0 is not a @{text "number_of"} \dots\ thing. The same for 1. So let's add those equations too *}
  142.17 +  text{* Oh!! 0 is not a variable \dots\ Oh! 0 is not a @{text "numeral"} \dots\ thing. The same for 1. So let's add those equations too *}
  142.18  
  142.19  lemma Inum_01: "Inum (C 0) vs = 0" "Inum (C 1) vs = 1" "Inum (C(Suc n)) vs = Suc n"
  142.20    by simp+
  142.21 @@ -312,9 +312,9 @@
  142.22    by simp
  142.23  lemma Irint_C1: "Irint (IC 1) vs = 1"
  142.24    by simp
  142.25 -lemma Irint_Cnumberof: "Irint (IC (number_of x)) vs = number_of x"
  142.26 +lemma Irint_Cnumeral: "Irint (IC (numeral x)) vs = numeral x"
  142.27    by simp
  142.28 -lemmas Irint_simps = Irint_Var Irint_Neg Irint_Add Irint_Sub Irint_Mult Irint_C0 Irint_C1 Irint_Cnumberof 
  142.29 +lemmas Irint_simps = Irint_Var Irint_Neg Irint_Add Irint_Sub Irint_Mult Irint_C0 Irint_C1 Irint_Cnumeral
  142.30  lemma "(3::int) * x + y*y - 9 + (- z) = 0"
  142.31    apply (reify Irint_simps ("(3::int) * x + y*y - 9 + (- z)"))
  142.32    oops
  142.33 @@ -348,10 +348,10 @@
  142.34  by simp
  142.35  lemma Irnat_C1: "Irnat (NC 1) is ls vs = 1"
  142.36  by simp
  142.37 -lemma Irnat_Cnumberof: "Irnat (NC (number_of x)) is ls vs = number_of x"
  142.38 +lemma Irnat_Cnumeral: "Irnat (NC (numeral x)) is ls vs = numeral x"
  142.39  by simp
  142.40  lemmas Irnat_simps = Irnat_Suc Irnat_Var Irnat_Neg Irnat_Add Irnat_Sub Irnat_Mult Irnat_lgth
  142.41 -  Irnat_C0 Irnat_C1 Irnat_Cnumberof
  142.42 +  Irnat_C0 Irnat_C1 Irnat_Cnumeral
  142.43  lemma "(Suc n) * length (([(3::int) * x + y*y - 9 + (- z)] @ []) @ xs) = length xs"
  142.44    apply (reify Irnat_simps Irlist.simps Irint_simps ("(Suc n) *length (([(3::int) * x + y*y - 9 + (- z)] @ []) @ xs)"))
  142.45    oops
   143.1 --- a/src/HOL/ex/Simproc_Tests.thy	Fri Mar 23 20:32:43 2012 +0100
   143.2 +++ b/src/HOL/ex/Simproc_Tests.thy	Mon Mar 26 10:56:56 2012 +0200
   143.3 @@ -5,7 +5,7 @@
   143.4  header {* Testing of arithmetic simprocs *}
   143.5  
   143.6  theory Simproc_Tests
   143.7 -imports Main
   143.8 +imports (*Main*) "../Numeral_Simprocs"
   143.9  begin
  143.10  
  143.11  text {*
  143.12 @@ -43,7 +43,7 @@
  143.13  possible. *)
  143.14  
  143.15  notepad begin
  143.16 -  fix a b c d oo uu i j k l u v w x y z :: "'a::number_ring"
  143.17 +  fix a b c d oo uu i j k l u v w x y z :: "'a::comm_ring_1"
  143.18    {
  143.19      assume "a + - b = u" have "(a + c) - (b + c) = u"
  143.20        by (tactic {* test [@{simproc int_combine_numerals}] *}) fact
  143.21 @@ -107,7 +107,7 @@
  143.22  subsection {* @{text inteq_cancel_numerals} *}
  143.23  
  143.24  notepad begin
  143.25 -  fix i j k u vv w y z w' y' z' :: "'a::number_ring"
  143.26 +  fix i j k u vv w y z w' y' z' :: "'a::comm_ring_1"
  143.27    {
  143.28      assume "u = 0" have "2*u = u"
  143.29        by (tactic {* test [@{simproc inteq_cancel_numerals}] *}) fact
  143.30 @@ -130,7 +130,7 @@
  143.31  subsection {* @{text intless_cancel_numerals} *}
  143.32  
  143.33  notepad begin
  143.34 -  fix b c i j k u y :: "'a::{linordered_idom,number_ring}"
  143.35 +  fix b c i j k u y :: "'a::linordered_idom"
  143.36    {
  143.37      assume "y < 2 * b" have "y - b < b"
  143.38        by (tactic {* test [@{simproc intless_cancel_numerals}] *}) fact
  143.39 @@ -151,7 +151,7 @@
  143.40  subsection {* @{text ring_eq_cancel_numeral_factor} *}
  143.41  
  143.42  notepad begin
  143.43 -  fix x y :: "'a::{idom,ring_char_0,number_ring}"
  143.44 +  fix x y :: "'a::{idom,ring_char_0}"
  143.45    {
  143.46      assume "3*x = 4*y" have "9*x = 12 * y"
  143.47        by (tactic {* test [@{simproc ring_eq_cancel_numeral_factor}] *}) fact
  143.48 @@ -176,7 +176,7 @@
  143.49  subsection {* @{text int_div_cancel_numeral_factors} *}
  143.50  
  143.51  notepad begin
  143.52 -  fix x y z :: "'a::{semiring_div,ring_char_0,number_ring}"
  143.53 +  fix x y z :: "'a::{semiring_div,comm_ring_1,ring_char_0}"
  143.54    {
  143.55      assume "(3*x) div (4*y) = z" have "(9*x) div (12*y) = z"
  143.56        by (tactic {* test [@{simproc int_div_cancel_numeral_factors}] *}) fact
  143.57 @@ -199,7 +199,7 @@
  143.58  subsection {* @{text ring_less_cancel_numeral_factor} *}
  143.59  
  143.60  notepad begin
  143.61 -  fix x y :: "'a::{linordered_idom,number_ring}"
  143.62 +  fix x y :: "'a::linordered_idom"
  143.63    {
  143.64      assume "3*x < 4*y" have "9*x < 12 * y"
  143.65        by (tactic {* test [@{simproc ring_less_cancel_numeral_factor}] *}) fact
  143.66 @@ -224,7 +224,7 @@
  143.67  subsection {* @{text ring_le_cancel_numeral_factor} *}
  143.68  
  143.69  notepad begin
  143.70 -  fix x y :: "'a::{linordered_idom,number_ring}"
  143.71 +  fix x y :: "'a::linordered_idom"
  143.72    {
  143.73      assume "3*x \<le> 4*y" have "9*x \<le> 12 * y"
  143.74        by (tactic {* test [@{simproc ring_le_cancel_numeral_factor}] *}) fact
  143.75 @@ -255,7 +255,7 @@
  143.76  subsection {* @{text divide_cancel_numeral_factor} *}
  143.77  
  143.78  notepad begin
  143.79 -  fix x y z :: "'a::{field_inverse_zero,ring_char_0,number_ring}"
  143.80 +  fix x y z :: "'a::{field_inverse_zero,ring_char_0}"
  143.81    {
  143.82      assume "(3*x) / (4*y) = z" have "(9*x) / (12 * y) = z"
  143.83        by (tactic {* test [@{simproc divide_cancel_numeral_factor}] *}) fact
  143.84 @@ -322,6 +322,9 @@
  143.85    }
  143.86  end
  143.87  
  143.88 +lemma shows "a*(b*c)/(y*z) = d*(b::'a::linordered_field_inverse_zero)*(x*a)/z"
  143.89 +oops -- "FIXME: need simproc to cover this case"
  143.90 +
  143.91  subsection {* @{text divide_cancel_factor} *}
  143.92  
  143.93  notepad begin
  143.94 @@ -393,7 +396,7 @@
  143.95  subsection {* @{text field_combine_numerals} *}
  143.96  
  143.97  notepad begin
  143.98 -  fix x y z uu :: "'a::{field_inverse_zero,ring_char_0,number_ring}"
  143.99 +  fix x y z uu :: "'a::{field_inverse_zero,ring_char_0}"
 143.100    {
 143.101      assume "5 / 6 * x = uu" have "x / 2 + x / 3 = uu"
 143.102        by (tactic {* test [@{simproc field_combine_numerals}] *}) fact
 143.103 @@ -415,7 +418,7 @@
 143.104  end
 143.105  
 143.106  lemma
 143.107 -  fixes x :: "'a::{linordered_field_inverse_zero,number_ring}"
 143.108 +  fixes x :: "'a::{linordered_field_inverse_zero}"
 143.109    shows "2/3 * x + x / 3 = uu"
 143.110  apply (tactic {* test [@{simproc field_combine_numerals}] *})?
 143.111  oops -- "FIXME: test fails"
 143.112 @@ -448,17 +451,12 @@
 143.113    }
 143.114  end
 143.115  
 143.116 -(*negative numerals: FAIL*)
 143.117 -lemma "Suc (i + j + -3 + k) = u"
 143.118 -apply (tactic {* test [@{simproc nat_combine_numerals}] *})?
 143.119 -oops
 143.120 -
 143.121  subsection {* @{text nateq_cancel_numerals} *}
 143.122  
 143.123  notepad begin
 143.124    fix i j k l oo u uu vv w y z w' y' z' :: "nat"
 143.125    {
 143.126 -    assume "Suc 0 * u = 0" have "2*u = u"
 143.127 +    assume "Suc 0 * u = 0" have "2*u = (u::nat)"
 143.128        by (tactic {* test [@{simproc nateq_cancel_numerals}] *}) fact
 143.129    next
 143.130      assume "Suc 0 * u = Suc 0" have "2*u = Suc (u)"
 143.131 @@ -504,7 +502,7 @@
 143.132  
 143.133  notepad begin
 143.134    fix length :: "'a \<Rightarrow> nat" and l1 l2 xs :: "'a" and f :: "nat \<Rightarrow> 'a"
 143.135 -  fix c i j k l oo u uu vv w y z w' y' z' :: "nat"
 143.136 +  fix c i j k l m oo u uu vv w y z w' y' z' :: "nat"
 143.137    {
 143.138      assume "0 < j" have "(2*length xs < 2*length xs + j)"
 143.139        by (tactic {* test [@{simproc natless_cancel_numerals}] *}) fact
 143.140 @@ -518,14 +516,6 @@
 143.141    next
 143.142      assume "0 < Suc 0 * (m * n) + u" have "(2*n*m) < (3*(m*n)) + u"
 143.143        by (tactic {* test [@{simproc natless_cancel_numerals}] *}) fact
 143.144 -  next
 143.145 -    (* FIXME: negative numerals fail
 143.146 -    have "(i + j + -23 + (k::nat)) < u + 15 + y"
 143.147 -      apply (tactic {* test [@{simproc natless_cancel_numerals}] *})?
 143.148 -      sorry
 143.149 -    have "(i + j + 3 + (k::nat)) < u + -15 + y"
 143.150 -      apply (tactic {* test [@{simproc natless_cancel_numerals}] *})?
 143.151 -      sorry*)
 143.152    }
 143.153  end
 143.154  
 143.155 @@ -611,17 +601,6 @@
 143.156    next
 143.157      assume "u + y - 0 = v" have "Suc (Suc (Suc (Suc (Suc (u + y))))) - 5 = v"
 143.158        by (tactic {* test [@{simproc natdiff_cancel_numerals}] *}) fact
 143.159 -  next
 143.160 -    (* FIXME: negative numerals fail
 143.161 -    have "(i + j + -12 + k) - 15 = y"
 143.162 -      apply (tactic {* test [@{simproc natdiff_cancel_numerals}] *})?
 143.163 -      sorry
 143.164 -    have "(i + j + 12 + k) - -15 = y"
 143.165 -      apply (tactic {* test [@{simproc natdiff_cancel_numerals}] *})?
 143.166 -      sorry
 143.167 -    have "(i + j + -12 + k) - -15 = y"
 143.168 -      apply (tactic {* test [@{simproc natdiff_cancel_numerals}] *})?
 143.169 -      sorry*)
 143.170    }
 143.171  end
 143.172  
   144.1 --- a/src/ZF/AC/AC_Equiv.thy	Fri Mar 23 20:32:43 2012 +0100
   144.2 +++ b/src/ZF/AC/AC_Equiv.thy	Mon Mar 26 10:56:56 2012 +0200
   144.3 @@ -162,11 +162,6 @@
   144.4       "[| f \<in> inj(A, B);  !!a. a \<in> A ==> f`a \<in> C |] ==> f \<in> inj(A,C)"
   144.5  by (unfold inj_def, blast intro: Pi_type) 
   144.6  
   144.7 -lemma nat_not_Finite: "~ Finite(nat)"
   144.8 -by (unfold Finite_def, blast dest: eqpoll_imp_lepoll ltI lt_not_lepoll)
   144.9 -
  144.10 -lemmas le_imp_lepoll = le_imp_subset [THEN subset_imp_lepoll]
  144.11 -
  144.12  (* ********************************************************************** *)
  144.13  (* Another elimination rule for \<exists>!                                       *)
  144.14  (* ********************************************************************** *)
  144.15 @@ -175,30 +170,18 @@
  144.16  by blast
  144.17  
  144.18  (* ********************************************************************** *)
  144.19 -(* image of a surjection                                                  *)
  144.20 -(* ********************************************************************** *)
  144.21 -
  144.22 -lemma surj_image_eq: "f \<in> surj(A, B) ==> f``A = B"
  144.23 -apply (unfold surj_def)
  144.24 -apply (erule CollectE)
  144.25 -apply (rule image_fun [THEN ssubst], assumption, rule subset_refl)
  144.26 -apply (blast dest: apply_type) 
  144.27 -done
  144.28 -
  144.29 -
  144.30 -(* ********************************************************************** *)
  144.31  (* Lemmas used in the proofs like WO? ==> AC?                             *)
  144.32  (* ********************************************************************** *)
  144.33  
  144.34  lemma first_in_B:
  144.35 -     "[| well_ord(\<Union>(A),r); 0\<notin>A; B \<in> A |] ==> (THE b. first(b,B,r)) \<in> B"
  144.36 +     "[| well_ord(\<Union>(A),r); 0 \<notin> A; B \<in> A |] ==> (THE b. first(b,B,r)) \<in> B"
  144.37  by (blast dest!: well_ord_imp_ex1_first
  144.38                      [THEN theI, THEN first_def [THEN def_imp_iff, THEN iffD1]])
  144.39  
  144.40 -lemma ex_choice_fun: "[| well_ord(\<Union>(A), R); 0\<notin>A |] ==> \<exists>f. f:(\<Pi> X \<in> A. X)"
  144.41 +lemma ex_choice_fun: "[| well_ord(\<Union>(A), R); 0 \<notin> A |] ==> \<exists>f. f \<in> (\<Pi> X \<in> A. X)"
  144.42  by (fast elim!: first_in_B intro!: lam_type)
  144.43  
  144.44 -lemma ex_choice_fun_Pow: "well_ord(A, R) ==> \<exists>f. f:(\<Pi> X \<in> Pow(A)-{0}. X)"
  144.45 +lemma ex_choice_fun_Pow: "well_ord(A, R) ==> \<exists>f. f \<in> (\<Pi> X \<in> Pow(A)-{0}. X)"
  144.46  by (fast elim!: well_ord_subset [THEN ex_choice_fun])
  144.47  
  144.48  
   145.1 --- a/src/ZF/AC/Cardinal_aux.thy	Fri Mar 23 20:32:43 2012 +0100
   145.2 +++ b/src/ZF/AC/Cardinal_aux.thy	Mon Mar 26 10:56:56 2012 +0200
   145.3 @@ -30,46 +30,32 @@
   145.4       "[| A \<prec> i; Ord(i) |] ==> \<exists>j. j<i & A \<approx> j"
   145.5  by (unfold lesspoll_def, blast dest!: lepoll_imp_ex_le_eqpoll elim!: leE)
   145.6  
   145.7 -lemma Inf_Ord_imp_InfCard_cardinal: "[| ~Finite(i); Ord(i) |] ==> InfCard(|i|)"
   145.8 -apply (unfold InfCard_def)
   145.9 -apply (rule conjI)
  145.10 -apply (rule Card_cardinal)
  145.11 -apply (rule Card_nat
  145.12 -            [THEN Card_def [THEN def_imp_iff, THEN iffD1, THEN ssubst]])
  145.13 -  -- "rewriting would loop!"
  145.14 -apply (rule well_ord_Memrel [THEN well_ord_lepoll_imp_Card_le], assumption)
  145.15 -apply (rule nat_le_infinite_Ord [THEN le_imp_lepoll], assumption+)
  145.16 -done
  145.17 -
  145.18 -text{*An alternative and more general proof goes like this: A and B are both
  145.19 -well-ordered (because they are injected into an ordinal), either @{term"A \<lesssim> B"}
  145.20 -or @{term"B \<lesssim> A"}.  Also both are equipollent to their cardinalities, so
  145.21 -(if A and B are infinite) then @{term"A \<union> B \<lesssim> |A\<oplus>B| \<longleftrightarrow> max(|A|,|B|) \<lesssim> i"}.
  145.22 -In fact, the correctly strengthened version of this theorem appears below.*}
  145.23 -lemma Un_lepoll_Inf_Ord_weak:
  145.24 -     "[|A \<approx> i; B \<approx> i; \<not> Finite(i); Ord(i)|] ==> A \<union> B \<lesssim> i"
  145.25 -apply (rule Un_lepoll_sum [THEN lepoll_trans])
  145.26 -apply (rule lepoll_imp_sum_lepoll_prod [THEN lepoll_trans])
  145.27 -apply (erule eqpoll_trans [THEN eqpoll_imp_lepoll])
  145.28 -apply (erule eqpoll_sym)
  145.29 -apply (rule subset_imp_lepoll [THEN lepoll_trans, THEN lepoll_trans])
  145.30 -apply (rule nat_2I [THEN OrdmemD], rule Ord_nat)
  145.31 -apply (rule nat_le_infinite_Ord [THEN le_imp_lepoll], assumption+)
  145.32 -apply (erule eqpoll_sym [THEN eqpoll_imp_lepoll])
  145.33 -apply (erule prod_eqpoll_cong [THEN eqpoll_imp_lepoll, THEN lepoll_trans],
  145.34 -       assumption)
  145.35 -apply (rule eqpoll_imp_lepoll)
  145.36 -apply (rule well_ord_Memrel [THEN well_ord_InfCard_square_eq], assumption)
  145.37 -apply (rule Inf_Ord_imp_InfCard_cardinal, assumption+)
  145.38 -done
  145.39 -
  145.40  lemma Un_eqpoll_Inf_Ord:
  145.41 -     "[| A \<approx> i; B \<approx> i; ~Finite(i); Ord(i) |] ==> A \<union> B \<approx> i"
  145.42 -apply (rule eqpollI)
  145.43 -apply (blast intro: Un_lepoll_Inf_Ord_weak)
  145.44 -apply (erule eqpoll_sym [THEN eqpoll_imp_lepoll, THEN lepoll_trans])
  145.45 -apply (rule Un_upper1 [THEN subset_imp_lepoll])
  145.46 -done
  145.47 +  assumes A: "A \<approx> i" and B: "B \<approx> i" and NFI: "\<not> Finite(i)" and i: "Ord(i)"
  145.48 +  shows "A \<union> B \<approx> i"
  145.49 +proof (rule eqpollI)
  145.50 +  have AB: "A \<approx> B" using A B by (blast intro: eqpoll_sym eqpoll_trans) 
  145.51 +  have "2 \<lesssim> nat" 
  145.52 +    by (rule subset_imp_lepoll) (rule OrdmemD [OF nat_2I Ord_nat]) 
  145.53 +  also have "... \<lesssim> i" 
  145.54 +    by (simp add: nat_le_infinite_Ord le_imp_lepoll NFI i)+
  145.55 +  also have "... \<approx> A" by (blast intro: eqpoll_sym A) 
  145.56 +  finally have "2 \<lesssim> A" .
  145.57 +  have ICI: "InfCard(|i|)" 
  145.58 +    by (simp add: Inf_Card_is_InfCard Finite_cardinal_iff NFI i) 
  145.59 +  have "A \<union> B \<lesssim> A + B" by (rule Un_lepoll_sum)
  145.60 +  also have "... \<lesssim> A \<times> B"
  145.61 +    by (rule lepoll_imp_sum_lepoll_prod [OF AB [THEN eqpoll_imp_lepoll] `2 \<lesssim> A`])
  145.62 +  also have "... \<approx> i \<times> i"
  145.63 +    by (blast intro: prod_eqpoll_cong eqpoll_imp_lepoll A B) 
  145.64 +  also have "... \<approx> i"
  145.65 +    by (blast intro: well_ord_InfCard_square_eq well_ord_Memrel ICI i) 
  145.66 +  finally show "A \<union> B \<lesssim> i" .
  145.67 +next  
  145.68 +  have "i \<approx> A" by (blast intro: A eqpoll_sym)
  145.69 +  also have "... \<lesssim> A \<union> B" by (blast intro: subset_imp_lepoll) 
  145.70 +  finally show "i \<lesssim> A \<union> B" .
  145.71 +qed
  145.72  
  145.73  schematic_lemma paired_bij: "?f \<in> bij({{y,z}. y \<in> x}, x)"
  145.74  apply (rule RepFun_bijective)
  145.75 @@ -82,8 +68,7 @@
  145.76  lemma ex_eqpoll_disjoint: "\<exists>B. B \<approx> A & B \<inter> C = 0"
  145.77  by (fast intro!: paired_eqpoll equals0I elim: mem_asym)
  145.78  
  145.79 -(*Finally we reach this result.  Surely there's a simpler proof, as sketched
  145.80 -  above?*)
  145.81 +(*Finally we reach this result.  Surely there's a simpler proof?*)
  145.82  lemma Un_lepoll_Inf_Ord:
  145.83       "[| A \<lesssim> i; B \<lesssim> i; ~Finite(i); Ord(i) |] ==> A \<union> B \<lesssim> i"
  145.84  apply (rule_tac A1 = i and C1 = i in ex_eqpoll_disjoint [THEN exE])
   146.1 --- a/src/ZF/Cardinal.thy	Fri Mar 23 20:32:43 2012 +0100
   146.2 +++ b/src/ZF/Cardinal.thy	Mon Mar 26 10:56:56 2012 +0200
   146.3 @@ -445,7 +445,7 @@
   146.4  
   146.5  (*Infinite unions of cardinals?  See Devlin, Lemma 6.7, page 98*)
   146.6  
   146.7 -lemma Card_cardinal: "Card(|A|)"
   146.8 +lemma Card_cardinal [iff]: "Card(|A|)"
   146.9  proof (unfold cardinal_def)
  146.10    show "Card(\<mu> i. i \<approx> A)"
  146.11      proof (cases "\<exists>i. Ord (i) & i \<approx> A")
  146.12 @@ -1105,6 +1105,9 @@
  146.13  lemma Finite_Pow_iff [iff]: "Finite(Pow(A)) \<longleftrightarrow> Finite(A)"
  146.14  by (blast intro: Finite_Pow Finite_Pow_imp_Finite)
  146.15  
  146.16 +lemma Finite_cardinal_iff:
  146.17 +  assumes i: "Ord(i)" shows "Finite(|i|) \<longleftrightarrow> Finite(i)"
  146.18 +  by (auto simp add: Finite_def) (blast intro: eqpoll_trans eqpoll_sym Ord_cardinal_eqpoll [OF i])+
  146.19  
  146.20  
  146.21  (*Krzysztof Grabczewski's proof that the converse of a finite, well-ordered
   147.1 --- a/src/ZF/CardinalArith.thy	Fri Mar 23 20:32:43 2012 +0100
   147.2 +++ b/src/ZF/CardinalArith.thy	Mon Mar 26 10:56:56 2012 +0200
   147.3 @@ -682,7 +682,7 @@
   147.4  apply (simp add: InfCard_is_Card [THEN Card_cardinal_eq])
   147.5  done
   147.6  
   147.7 -lemma Inf_Card_is_InfCard: "[| ~Finite(i); Card(i) |] ==> InfCard(i)"
   147.8 +lemma Inf_Card_is_InfCard: "[| Card(i); ~ Finite(i) |] ==> InfCard(i)"
   147.9  by (simp add: InfCard_def Card_is_Ord [THEN nat_le_infinite_Ord])
  147.10  
  147.11  subsubsection{*Toward's Kunen's Corollary 10.13 (1)*}
   148.1 --- a/src/ZF/Perm.thy	Fri Mar 23 20:32:43 2012 +0100
   148.2 +++ b/src/ZF/Perm.thy	Mon Mar 26 10:56:56 2012 +0200
   148.3 @@ -505,6 +505,9 @@
   148.4  apply (blast intro: apply_equality apply_Pair Pi_type)
   148.5  done
   148.6  
   148.7 +lemma surj_image_eq: "f \<in> surj(A, B) ==> f``A = B"
   148.8 +  by (auto simp add: surj_def image_fun) (blast dest: apply_type) 
   148.9 +
  148.10  lemma restrict_image [simp]: "restrict(f,A) `` B = f `` (A \<inter> B)"
  148.11  by (auto simp add: restrict_def)
  148.12