renamed ex/Numeral.thy to ex/Numeral_Representation.thy
authorhuffman
Tue Feb 21 09:17:53 2012 +0100 (2012-02-21)
changeset 46558fdb84c40e074
parent 46557 ae926869a311
child 46559 69a273fcd53a
renamed ex/Numeral.thy to ex/Numeral_Representation.thy
src/HOL/IsaMakefile
src/HOL/ex/Numeral.thy
src/HOL/ex/Numeral_Representation.thy
src/HOL/ex/ROOT.ML
     1.1 --- a/src/HOL/IsaMakefile	Tue Feb 21 08:15:42 2012 +0100
     1.2 +++ b/src/HOL/IsaMakefile	Tue Feb 21 09:17:53 2012 +0100
     1.3 @@ -1059,9 +1059,9 @@
     1.4    ex/Lagrange.thy ex/List_to_Set_Comprehension_Examples.thy		\
     1.5    ex/LocaleTest2.thy ex/MT.thy ex/MergeSort.thy ex/Meson_Test.thy	\
     1.6    ex/MonoidGroup.thy ex/Multiquote.thy ex/NatSum.thy			\
     1.7 -  ex/Normalization_by_Evaluation.thy ex/Numeral.thy ex/PER.thy		\
     1.8 -  ex/PresburgerEx.thy ex/Primrec.thy ex/Quickcheck_Examples.thy		\
     1.9 -  ex/Quickcheck_Lattice_Examples.thy					\
    1.10 +  ex/Normalization_by_Evaluation.thy ex/Numeral_Representation.thy	\
    1.11 +  ex/PER.thy ex/PresburgerEx.thy ex/Primrec.thy				\
    1.12 +  ex/Quickcheck_Examples.thy ex/Quickcheck_Lattice_Examples.thy		\
    1.13    ex/Quickcheck_Narrowing_Examples.thy ex/Quicksort.thy ex/ROOT.ML	\
    1.14    ex/Records.thy ex/ReflectionEx.thy ex/Refute_Examples.thy		\
    1.15    ex/SAT_Examples.thy ex/Serbian.thy ex/Set_Theory.thy			\
     2.1 --- a/src/HOL/ex/Numeral.thy	Tue Feb 21 08:15:42 2012 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,1116 +0,0 @@
     2.4 -(*  Title:      HOL/ex/Numeral.thy
     2.5 -    Author:     Florian Haftmann
     2.6 -*)
     2.7 -
     2.8 -header {* An experimental alternative numeral representation. *}
     2.9 -
    2.10 -theory Numeral
    2.11 -imports Main
    2.12 -begin
    2.13 -
    2.14 -subsection {* The @{text num} type *}
    2.15 -
    2.16 -datatype num = One | Dig0 num | Dig1 num
    2.17 -
    2.18 -text {* Increment function for type @{typ num} *}
    2.19 -
    2.20 -primrec inc :: "num \<Rightarrow> num" where
    2.21 -  "inc One = Dig0 One"
    2.22 -| "inc (Dig0 x) = Dig1 x"
    2.23 -| "inc (Dig1 x) = Dig0 (inc x)"
    2.24 -
    2.25 -text {* Converting between type @{typ num} and type @{typ nat} *}
    2.26 -
    2.27 -primrec nat_of_num :: "num \<Rightarrow> nat" where
    2.28 -  "nat_of_num One = Suc 0"
    2.29 -| "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x"
    2.30 -| "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)"
    2.31 -
    2.32 -primrec num_of_nat :: "nat \<Rightarrow> num" where
    2.33 -  "num_of_nat 0 = One"
    2.34 -| "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
    2.35 -
    2.36 -lemma nat_of_num_pos: "0 < nat_of_num x"
    2.37 -  by (induct x) simp_all
    2.38 -
    2.39 -lemma nat_of_num_neq_0: "nat_of_num x \<noteq> 0"
    2.40 -  by (induct x) simp_all
    2.41 -
    2.42 -lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
    2.43 -  by (induct x) simp_all
    2.44 -
    2.45 -lemma num_of_nat_double:
    2.46 -  "0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)"
    2.47 -  by (induct n) simp_all
    2.48 -
    2.49 -text {*
    2.50 -  Type @{typ num} is isomorphic to the strictly positive
    2.51 -  natural numbers.
    2.52 -*}
    2.53 -
    2.54 -lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
    2.55 -  by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
    2.56 -
    2.57 -lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
    2.58 -  by (induct n) (simp_all add: nat_of_num_inc)
    2.59 -
    2.60 -lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
    2.61 -proof
    2.62 -  assume "nat_of_num x = nat_of_num y"
    2.63 -  then have "num_of_nat (nat_of_num x) = num_of_nat (nat_of_num y)" by simp
    2.64 -  then show "x = y" by (simp add: nat_of_num_inverse)
    2.65 -qed simp
    2.66 -
    2.67 -lemma num_induct [case_names One inc]:
    2.68 -  fixes P :: "num \<Rightarrow> bool"
    2.69 -  assumes One: "P One"
    2.70 -    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
    2.71 -  shows "P x"
    2.72 -proof -
    2.73 -  obtain n where n: "Suc n = nat_of_num x"
    2.74 -    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
    2.75 -  have "P (num_of_nat (Suc n))"
    2.76 -  proof (induct n)
    2.77 -    case 0 show ?case using One by simp
    2.78 -  next
    2.79 -    case (Suc n)
    2.80 -    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
    2.81 -    then show "P (num_of_nat (Suc (Suc n)))" by simp
    2.82 -  qed
    2.83 -  with n show "P x"
    2.84 -    by (simp add: nat_of_num_inverse)
    2.85 -qed
    2.86 -
    2.87 -text {*
    2.88 -  From now on, there are two possible models for @{typ num}: as
    2.89 -  positive naturals (rule @{text "num_induct"}) and as digit
    2.90 -  representation (rules @{text "num.induct"}, @{text "num.cases"}).
    2.91 -
    2.92 -  It is not entirely clear in which context it is better to use the
    2.93 -  one or the other, or whether the construction should be reversed.
    2.94 -*}
    2.95 -
    2.96 -
    2.97 -subsection {* Numeral operations *}
    2.98 -
    2.99 -ML {*
   2.100 -structure Dig_Simps = Named_Thms
   2.101 -(
   2.102 -  val name = @{binding numeral}
   2.103 -  val description = "simplification rules for numerals"
   2.104 -)
   2.105 -*}
   2.106 -
   2.107 -setup Dig_Simps.setup
   2.108 -
   2.109 -instantiation num :: "{plus,times,ord}"
   2.110 -begin
   2.111 -
   2.112 -definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where
   2.113 -  "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
   2.114 -
   2.115 -definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where
   2.116 -  "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
   2.117 -
   2.118 -definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
   2.119 -  "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
   2.120 -
   2.121 -definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
   2.122 -  "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
   2.123 -
   2.124 -instance ..
   2.125 -
   2.126 -end
   2.127 -
   2.128 -lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
   2.129 -  unfolding plus_num_def
   2.130 -  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
   2.131 -
   2.132 -lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
   2.133 -  unfolding times_num_def
   2.134 -  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
   2.135 -
   2.136 -lemma Dig_plus [numeral, simp, code]:
   2.137 -  "One + One = Dig0 One"
   2.138 -  "One + Dig0 m = Dig1 m"
   2.139 -  "One + Dig1 m = Dig0 (m + One)"
   2.140 -  "Dig0 n + One = Dig1 n"
   2.141 -  "Dig0 n + Dig0 m = Dig0 (n + m)"
   2.142 -  "Dig0 n + Dig1 m = Dig1 (n + m)"
   2.143 -  "Dig1 n + One = Dig0 (n + One)"
   2.144 -  "Dig1 n + Dig0 m = Dig1 (n + m)"
   2.145 -  "Dig1 n + Dig1 m = Dig0 (n + m + One)"
   2.146 -  by (simp_all add: num_eq_iff nat_of_num_add)
   2.147 -
   2.148 -lemma Dig_times [numeral, simp, code]:
   2.149 -  "One * One = One"
   2.150 -  "One * Dig0 n = Dig0 n"
   2.151 -  "One * Dig1 n = Dig1 n"
   2.152 -  "Dig0 n * One = Dig0 n"
   2.153 -  "Dig0 n * Dig0 m = Dig0 (n * Dig0 m)"
   2.154 -  "Dig0 n * Dig1 m = Dig0 (n * Dig1 m)"
   2.155 -  "Dig1 n * One = Dig1 n"
   2.156 -  "Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)"
   2.157 -  "Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)"
   2.158 -  by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult
   2.159 -                    left_distrib right_distrib)
   2.160 -
   2.161 -lemma less_eq_num_code [numeral, simp, code]:
   2.162 -  "One \<le> n \<longleftrightarrow> True"
   2.163 -  "Dig0 m \<le> One \<longleftrightarrow> False"
   2.164 -  "Dig1 m \<le> One \<longleftrightarrow> False"
   2.165 -  "Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n"
   2.166 -  "Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
   2.167 -  "Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
   2.168 -  "Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n"
   2.169 -  using nat_of_num_pos [of n] nat_of_num_pos [of m]
   2.170 -  by (auto simp add: less_eq_num_def less_num_def)
   2.171 -
   2.172 -lemma less_num_code [numeral, simp, code]:
   2.173 -  "m < One \<longleftrightarrow> False"
   2.174 -  "One < One \<longleftrightarrow> False"
   2.175 -  "One < Dig0 n \<longleftrightarrow> True"
   2.176 -  "One < Dig1 n \<longleftrightarrow> True"
   2.177 -  "Dig0 m < Dig0 n \<longleftrightarrow> m < n"
   2.178 -  "Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n"
   2.179 -  "Dig1 m < Dig1 n \<longleftrightarrow> m < n"
   2.180 -  "Dig1 m < Dig0 n \<longleftrightarrow> m < n"
   2.181 -  using nat_of_num_pos [of n] nat_of_num_pos [of m]
   2.182 -  by (auto simp add: less_eq_num_def less_num_def)
   2.183 -
   2.184 -text {* Rules using @{text One} and @{text inc} as constructors *}
   2.185 -
   2.186 -lemma add_One: "x + One = inc x"
   2.187 -  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   2.188 -
   2.189 -lemma add_inc: "x + inc y = inc (x + y)"
   2.190 -  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   2.191 -
   2.192 -lemma mult_One: "x * One = x"
   2.193 -  by (simp add: num_eq_iff nat_of_num_mult)
   2.194 -
   2.195 -lemma mult_inc: "x * inc y = x * y + x"
   2.196 -  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
   2.197 -
   2.198 -text {* A double-and-decrement function *}
   2.199 -
   2.200 -primrec DigM :: "num \<Rightarrow> num" where
   2.201 -  "DigM One = One"
   2.202 -| "DigM (Dig0 n) = Dig1 (DigM n)"
   2.203 -| "DigM (Dig1 n) = Dig1 (Dig0 n)"
   2.204 -
   2.205 -lemma DigM_plus_one: "DigM n + One = Dig0 n"
   2.206 -  by (induct n) simp_all
   2.207 -
   2.208 -lemma add_One_commute: "One + n = n + One"
   2.209 -  by (induct n) simp_all
   2.210 -
   2.211 -lemma one_plus_DigM: "One + DigM n = Dig0 n"
   2.212 -  by (simp add: add_One_commute DigM_plus_one)
   2.213 -
   2.214 -text {* Squaring and exponentiation *}
   2.215 -
   2.216 -primrec square :: "num \<Rightarrow> num" where
   2.217 -  "square One = One"
   2.218 -| "square (Dig0 n) = Dig0 (Dig0 (square n))"
   2.219 -| "square (Dig1 n) = Dig1 (Dig0 (square n + n))"
   2.220 -
   2.221 -primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
   2.222 -  "pow x One = x"
   2.223 -| "pow x (Dig0 y) = square (pow x y)"
   2.224 -| "pow x (Dig1 y) = x * square (pow x y)"
   2.225 -
   2.226 -
   2.227 -subsection {* Binary numerals *}
   2.228 -
   2.229 -text {*
   2.230 -  We embed binary representations into a generic algebraic
   2.231 -  structure using @{text of_num}.
   2.232 -*}
   2.233 -
   2.234 -class semiring_numeral = semiring + monoid_mult
   2.235 -begin
   2.236 -
   2.237 -primrec of_num :: "num \<Rightarrow> 'a" where
   2.238 -  of_num_One [numeral]: "of_num One = 1"
   2.239 -| "of_num (Dig0 n) = of_num n + of_num n"
   2.240 -| "of_num (Dig1 n) = of_num n + of_num n + 1"
   2.241 -
   2.242 -lemma of_num_inc: "of_num (inc n) = of_num n + 1"
   2.243 -  by (induct n) (simp_all add: add_ac)
   2.244 -
   2.245 -lemma of_num_add: "of_num (m + n) = of_num m + of_num n"
   2.246 -  by (induct n rule: num_induct) (simp_all add: add_One add_inc of_num_inc add_ac)
   2.247 -
   2.248 -lemma of_num_mult: "of_num (m * n) = of_num m * of_num n"
   2.249 -  by (induct n rule: num_induct) (simp_all add: mult_One mult_inc of_num_add of_num_inc right_distrib)
   2.250 -
   2.251 -declare of_num.simps [simp del]
   2.252 -
   2.253 -end
   2.254 -
   2.255 -ML {*
   2.256 -fun mk_num k =
   2.257 -  if k > 1 then
   2.258 -    let
   2.259 -      val (l, b) = Integer.div_mod k 2;
   2.260 -      val bit = (if b = 0 then @{term Dig0} else @{term Dig1});
   2.261 -    in bit $ (mk_num l) end
   2.262 -  else if k = 1 then @{term One}
   2.263 -  else error ("mk_num " ^ string_of_int k);
   2.264 -
   2.265 -fun dest_num @{term One} = 1
   2.266 -  | dest_num (@{term Dig0} $ n) = 2 * dest_num n
   2.267 -  | dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1
   2.268 -  | dest_num t = raise TERM ("dest_num", [t]);
   2.269 -
   2.270 -fun mk_numeral phi T k = Morphism.term phi (Const (@{const_name of_num}, @{typ num} --> T))
   2.271 -  $ mk_num k
   2.272 -
   2.273 -fun dest_numeral phi (u $ t) =
   2.274 -  if Term.aconv_untyped (u, Morphism.term phi (Const (@{const_name of_num}, dummyT)))
   2.275 -  then (range_type (fastype_of u), dest_num t)
   2.276 -  else raise TERM ("dest_numeral", [u, t]);
   2.277 -*}
   2.278 -
   2.279 -syntax
   2.280 -  "_Numerals" :: "xnum_token \<Rightarrow> 'a"    ("_")
   2.281 -
   2.282 -parse_translation {*
   2.283 -let
   2.284 -  fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2)
   2.285 -     of (0, 1) => Const (@{const_name One}, dummyT)
   2.286 -      | (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n
   2.287 -      | (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n
   2.288 -    else raise Match;
   2.289 -  fun numeral_tr [Free (num, _)] =
   2.290 -        let
   2.291 -          val {leading_zeros, value, ...} = Lexicon.read_xnum num;
   2.292 -          val _ = leading_zeros = 0 andalso value > 0
   2.293 -            orelse error ("Bad numeral: " ^ num);
   2.294 -        in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end
   2.295 -    | numeral_tr ts = raise TERM ("numeral_tr", ts);
   2.296 -in [(@{syntax_const "_Numerals"}, numeral_tr)] end
   2.297 -*}
   2.298 -
   2.299 -typed_print_translation (advanced) {*
   2.300 -let
   2.301 -  fun dig b n = b + 2 * n; 
   2.302 -  fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) =
   2.303 -        dig 0 (int_of_num' n)
   2.304 -    | int_of_num' (Const (@{const_syntax Dig1}, _) $ n) =
   2.305 -        dig 1 (int_of_num' n)
   2.306 -    | int_of_num' (Const (@{const_syntax One}, _)) = 1;
   2.307 -  fun num_tr' ctxt T [n] =
   2.308 -    let
   2.309 -      val k = int_of_num' n;
   2.310 -      val t' = Syntax.const @{syntax_const "_Numerals"} $ Syntax.free ("#" ^ string_of_int k);
   2.311 -    in
   2.312 -      case T of
   2.313 -        Type (@{type_name fun}, [_, T']) =>
   2.314 -          if not (Config.get ctxt show_types) andalso can Term.dest_Type T' then t'
   2.315 -          else Syntax.const @{syntax_const "_constrain"} $ t' $ Syntax_Phases.term_of_typ ctxt T'
   2.316 -      | T' => if T' = dummyT then t' else raise Match
   2.317 -    end;
   2.318 -in [(@{const_syntax of_num}, num_tr')] end
   2.319 -*}
   2.320 -
   2.321 -
   2.322 -subsection {* Class-specific numeral rules *}
   2.323 -
   2.324 -subsubsection {* Class @{text semiring_numeral} *}
   2.325 -
   2.326 -context semiring_numeral
   2.327 -begin
   2.328 -
   2.329 -abbreviation "Num1 \<equiv> of_num One"
   2.330 -
   2.331 -text {*
   2.332 -  Alas, there is still the duplication of @{term 1}, although the
   2.333 -  duplicated @{term 0} has disappeared.  We could get rid of it by
   2.334 -  replacing the constructor @{term 1} in @{typ num} by two
   2.335 -  constructors @{text two} and @{text three}, resulting in a further
   2.336 -  blow-up.  But it could be worth the effort.
   2.337 -*}
   2.338 -
   2.339 -lemma of_num_plus_one [numeral]:
   2.340 -  "of_num n + 1 = of_num (n + One)"
   2.341 -  by (simp only: of_num_add of_num_One)
   2.342 -
   2.343 -lemma of_num_one_plus [numeral]:
   2.344 -  "1 + of_num n = of_num (One + n)"
   2.345 -  by (simp only: of_num_add of_num_One)
   2.346 -
   2.347 -lemma of_num_plus [numeral]:
   2.348 -  "of_num m + of_num n = of_num (m + n)"
   2.349 -  by (simp only: of_num_add)
   2.350 -
   2.351 -lemma of_num_times_one [numeral]:
   2.352 -  "of_num n * 1 = of_num n"
   2.353 -  by simp
   2.354 -
   2.355 -lemma of_num_one_times [numeral]:
   2.356 -  "1 * of_num n = of_num n"
   2.357 -  by simp
   2.358 -
   2.359 -lemma of_num_times [numeral]:
   2.360 -  "of_num m * of_num n = of_num (m * n)"
   2.361 -  unfolding of_num_mult ..
   2.362 -
   2.363 -end
   2.364 -
   2.365 -
   2.366 -subsubsection {* Structures with a zero: class @{text semiring_1} *}
   2.367 -
   2.368 -context semiring_1
   2.369 -begin
   2.370 -
   2.371 -subclass semiring_numeral ..
   2.372 -
   2.373 -lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n"
   2.374 -  by (induct n)
   2.375 -    (simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac)
   2.376 -
   2.377 -declare of_nat_1 [numeral]
   2.378 -
   2.379 -lemma Dig_plus_zero [numeral]:
   2.380 -  "0 + 1 = 1"
   2.381 -  "0 + of_num n = of_num n"
   2.382 -  "1 + 0 = 1"
   2.383 -  "of_num n + 0 = of_num n"
   2.384 -  by simp_all
   2.385 -
   2.386 -lemma Dig_times_zero [numeral]:
   2.387 -  "0 * 1 = 0"
   2.388 -  "0 * of_num n = 0"
   2.389 -  "1 * 0 = 0"
   2.390 -  "of_num n * 0 = 0"
   2.391 -  by simp_all
   2.392 -
   2.393 -end
   2.394 -
   2.395 -lemma nat_of_num_of_num: "nat_of_num = of_num"
   2.396 -proof
   2.397 -  fix n
   2.398 -  have "of_num n = nat_of_num n"
   2.399 -    by (induct n) (simp_all add: of_num.simps)
   2.400 -  then show "nat_of_num n = of_num n" by simp
   2.401 -qed
   2.402 -
   2.403 -
   2.404 -subsubsection {* Equality: class @{text semiring_char_0} *}
   2.405 -
   2.406 -context semiring_char_0
   2.407 -begin
   2.408 -
   2.409 -lemma of_num_eq_iff [numeral]: "of_num m = of_num n \<longleftrightarrow> m = n"
   2.410 -  unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric]
   2.411 -    of_nat_eq_iff num_eq_iff ..
   2.412 -
   2.413 -lemma of_num_eq_one_iff [numeral]: "of_num n = 1 \<longleftrightarrow> n = One"
   2.414 -  using of_num_eq_iff [of n One] by (simp add: of_num_One)
   2.415 -
   2.416 -lemma one_eq_of_num_iff [numeral]: "1 = of_num n \<longleftrightarrow> One = n"
   2.417 -  using of_num_eq_iff [of One n] by (simp add: of_num_One)
   2.418 -
   2.419 -end
   2.420 -
   2.421 -
   2.422 -subsubsection {* Comparisons: class @{text linordered_semidom} *}
   2.423 -
   2.424 -text {*
   2.425 -  Perhaps the underlying structure could even 
   2.426 -  be more general than @{text linordered_semidom}.
   2.427 -*}
   2.428 -
   2.429 -context linordered_semidom
   2.430 -begin
   2.431 -
   2.432 -lemma of_num_pos [numeral]: "0 < of_num n"
   2.433 -  by (induct n) (simp_all add: of_num.simps add_pos_pos)
   2.434 -
   2.435 -lemma of_num_not_zero [numeral]: "of_num n \<noteq> 0"
   2.436 -  using of_num_pos [of n] by simp
   2.437 -
   2.438 -lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n"
   2.439 -proof -
   2.440 -  have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n"
   2.441 -    unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff ..
   2.442 -  then show ?thesis by (simp add: of_nat_of_num)
   2.443 -qed
   2.444 -
   2.445 -lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n \<le> One"
   2.446 -  using of_num_less_eq_iff [of n One] by (simp add: of_num_One)
   2.447 -
   2.448 -lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n"
   2.449 -  using of_num_less_eq_iff [of One n] by (simp add: of_num_One)
   2.450 -
   2.451 -lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n"
   2.452 -proof -
   2.453 -  have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n"
   2.454 -    unfolding less_num_def nat_of_num_of_num of_nat_less_iff ..
   2.455 -  then show ?thesis by (simp add: of_nat_of_num)
   2.456 -qed
   2.457 -
   2.458 -lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1"
   2.459 -  using of_num_less_iff [of n One] by (simp add: of_num_One)
   2.460 -
   2.461 -lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> One < n"
   2.462 -  using of_num_less_iff [of One n] by (simp add: of_num_One)
   2.463 -
   2.464 -lemma of_num_nonneg [numeral]: "0 \<le> of_num n"
   2.465 -  by (induct n) (simp_all add: of_num.simps add_nonneg_nonneg)
   2.466 -
   2.467 -lemma of_num_less_zero_iff [numeral]: "\<not> of_num n < 0"
   2.468 -  by (simp add: not_less of_num_nonneg)
   2.469 -
   2.470 -lemma of_num_le_zero_iff [numeral]: "\<not> of_num n \<le> 0"
   2.471 -  by (simp add: not_le of_num_pos)
   2.472 -
   2.473 -end
   2.474 -
   2.475 -context linordered_idom
   2.476 -begin
   2.477 -
   2.478 -lemma minus_of_num_less_of_num_iff: "- of_num m < of_num n"
   2.479 -proof -
   2.480 -  have "- of_num m < 0" by (simp add: of_num_pos)
   2.481 -  also have "0 < of_num n" by (simp add: of_num_pos)
   2.482 -  finally show ?thesis .
   2.483 -qed
   2.484 -
   2.485 -lemma minus_of_num_not_equal_of_num: "- of_num m \<noteq> of_num n"
   2.486 -  using minus_of_num_less_of_num_iff [of m n] by simp
   2.487 -
   2.488 -lemma minus_of_num_less_one_iff: "- of_num n < 1"
   2.489 -  using minus_of_num_less_of_num_iff [of n One] by (simp add: of_num_One)
   2.490 -
   2.491 -lemma minus_one_less_of_num_iff: "- 1 < of_num n"
   2.492 -  using minus_of_num_less_of_num_iff [of One n] by (simp add: of_num_One)
   2.493 -
   2.494 -lemma minus_one_less_one_iff: "- 1 < 1"
   2.495 -  using minus_of_num_less_of_num_iff [of One One] by (simp add: of_num_One)
   2.496 -
   2.497 -lemma minus_of_num_le_of_num_iff: "- of_num m \<le> of_num n"
   2.498 -  by (simp add: less_imp_le minus_of_num_less_of_num_iff)
   2.499 -
   2.500 -lemma minus_of_num_le_one_iff: "- of_num n \<le> 1"
   2.501 -  by (simp add: less_imp_le minus_of_num_less_one_iff)
   2.502 -
   2.503 -lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n"
   2.504 -  by (simp add: less_imp_le minus_one_less_of_num_iff)
   2.505 -
   2.506 -lemma minus_one_le_one_iff: "- 1 \<le> 1"
   2.507 -  by (simp add: less_imp_le minus_one_less_one_iff)
   2.508 -
   2.509 -lemma of_num_le_minus_of_num_iff: "\<not> of_num m \<le> - of_num n"
   2.510 -  by (simp add: not_le minus_of_num_less_of_num_iff)
   2.511 -
   2.512 -lemma one_le_minus_of_num_iff: "\<not> 1 \<le> - of_num n"
   2.513 -  by (simp add: not_le minus_of_num_less_one_iff)
   2.514 -
   2.515 -lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1"
   2.516 -  by (simp add: not_le minus_one_less_of_num_iff)
   2.517 -
   2.518 -lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1"
   2.519 -  by (simp add: not_le minus_one_less_one_iff)
   2.520 -
   2.521 -lemma of_num_less_minus_of_num_iff: "\<not> of_num m < - of_num n"
   2.522 -  by (simp add: not_less minus_of_num_le_of_num_iff)
   2.523 -
   2.524 -lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n"
   2.525 -  by (simp add: not_less minus_of_num_le_one_iff)
   2.526 -
   2.527 -lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1"
   2.528 -  by (simp add: not_less minus_one_le_of_num_iff)
   2.529 -
   2.530 -lemma one_less_minus_one_iff: "\<not> 1 < - 1"
   2.531 -  by (simp add: not_less minus_one_le_one_iff)
   2.532 -
   2.533 -lemmas le_signed_numeral_special [numeral] =
   2.534 -  minus_of_num_le_of_num_iff
   2.535 -  minus_of_num_le_one_iff
   2.536 -  minus_one_le_of_num_iff
   2.537 -  minus_one_le_one_iff
   2.538 -  of_num_le_minus_of_num_iff
   2.539 -  one_le_minus_of_num_iff
   2.540 -  of_num_le_minus_one_iff
   2.541 -  one_le_minus_one_iff
   2.542 -
   2.543 -lemmas less_signed_numeral_special [numeral] =
   2.544 -  minus_of_num_less_of_num_iff
   2.545 -  minus_of_num_not_equal_of_num
   2.546 -  minus_of_num_less_one_iff
   2.547 -  minus_one_less_of_num_iff
   2.548 -  minus_one_less_one_iff
   2.549 -  of_num_less_minus_of_num_iff
   2.550 -  one_less_minus_of_num_iff
   2.551 -  of_num_less_minus_one_iff
   2.552 -  one_less_minus_one_iff
   2.553 -
   2.554 -end
   2.555 -
   2.556 -subsubsection {* Structures with subtraction: class @{text semiring_1_minus} *}
   2.557 -
   2.558 -class semiring_minus = semiring + minus + zero +
   2.559 -  assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a"
   2.560 -  assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a"
   2.561 -begin
   2.562 -
   2.563 -lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b"
   2.564 -  by (simp add: add_ac minus_inverts_plus1 [of b a])
   2.565 -
   2.566 -lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b"
   2.567 -  by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a])
   2.568 -
   2.569 -end
   2.570 -
   2.571 -class semiring_1_minus = semiring_1 + semiring_minus
   2.572 -begin
   2.573 -
   2.574 -lemma Dig_of_num_pos:
   2.575 -  assumes "k + n = m"
   2.576 -  shows "of_num m - of_num n = of_num k"
   2.577 -  using assms by (simp add: of_num_plus minus_inverts_plus1)
   2.578 -
   2.579 -lemma Dig_of_num_zero:
   2.580 -  shows "of_num n - of_num n = 0"
   2.581 -  by (rule minus_inverts_plus1) simp
   2.582 -
   2.583 -lemma Dig_of_num_neg:
   2.584 -  assumes "k + m = n"
   2.585 -  shows "of_num m - of_num n = 0 - of_num k"
   2.586 -  by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms)
   2.587 -
   2.588 -lemmas Dig_plus_eval =
   2.589 -  of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject
   2.590 -
   2.591 -simproc_setup numeral_minus ("of_num m - of_num n") = {*
   2.592 -  let
   2.593 -    (*TODO proper implicit use of morphism via pattern antiquotations*)
   2.594 -    fun cdest_of_num ct = (List.last o snd o Drule.strip_comb) ct;
   2.595 -    fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n);
   2.596 -    fun attach_num ct = (dest_num (Thm.term_of ct), ct);
   2.597 -    fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t;
   2.598 -    val simplify = Raw_Simplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval});
   2.599 -    fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq}
   2.600 -      OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"},
   2.601 -        [Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]];
   2.602 -  in fn phi => fn _ => fn ct => case try cdifference ct
   2.603 -   of NONE => (NONE)
   2.604 -    | SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0
   2.605 -        then Raw_Simplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct
   2.606 -        else mk_meta_eq (let
   2.607 -          val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j));
   2.608 -        in
   2.609 -          (if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck]
   2.610 -          else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl])
   2.611 -        end) end)
   2.612 -  end
   2.613 -*}
   2.614 -
   2.615 -lemma Dig_of_num_minus_zero [numeral]:
   2.616 -  "of_num n - 0 = of_num n"
   2.617 -  by (simp add: minus_inverts_plus1)
   2.618 -
   2.619 -lemma Dig_one_minus_zero [numeral]:
   2.620 -  "1 - 0 = 1"
   2.621 -  by (simp add: minus_inverts_plus1)
   2.622 -
   2.623 -lemma Dig_one_minus_one [numeral]:
   2.624 -  "1 - 1 = 0"
   2.625 -  by (simp add: minus_inverts_plus1)
   2.626 -
   2.627 -lemma Dig_of_num_minus_one [numeral]:
   2.628 -  "of_num (Dig0 n) - 1 = of_num (DigM n)"
   2.629 -  "of_num (Dig1 n) - 1 = of_num (Dig0 n)"
   2.630 -  by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
   2.631 -
   2.632 -lemma Dig_one_minus_of_num [numeral]:
   2.633 -  "1 - of_num (Dig0 n) = 0 - of_num (DigM n)"
   2.634 -  "1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)"
   2.635 -  by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
   2.636 -
   2.637 -end
   2.638 -
   2.639 -
   2.640 -subsubsection {* Structures with negation: class @{text ring_1} *}
   2.641 -
   2.642 -context ring_1
   2.643 -begin
   2.644 -
   2.645 -subclass semiring_1_minus proof
   2.646 -qed (simp_all add: algebra_simps)
   2.647 -
   2.648 -lemma Dig_zero_minus_of_num [numeral]:
   2.649 -  "0 - of_num n = - of_num n"
   2.650 -  by simp
   2.651 -
   2.652 -lemma Dig_zero_minus_one [numeral]:
   2.653 -  "0 - 1 = - 1"
   2.654 -  by simp
   2.655 -
   2.656 -lemma Dig_uminus_uminus [numeral]:
   2.657 -  "- (- of_num n) = of_num n"
   2.658 -  by simp
   2.659 -
   2.660 -lemma Dig_plus_uminus [numeral]:
   2.661 -  "of_num m + - of_num n = of_num m - of_num n"
   2.662 -  "- of_num m + of_num n = of_num n - of_num m"
   2.663 -  "- of_num m + - of_num n = - (of_num m + of_num n)"
   2.664 -  "of_num m - - of_num n = of_num m + of_num n"
   2.665 -  "- of_num m - of_num n = - (of_num m + of_num n)"
   2.666 -  "- of_num m - - of_num n = of_num n - of_num m"
   2.667 -  by (simp_all add: diff_minus add_commute)
   2.668 -
   2.669 -lemma Dig_times_uminus [numeral]:
   2.670 -  "- of_num n * of_num m = - (of_num n * of_num m)"
   2.671 -  "of_num n * - of_num m = - (of_num n * of_num m)"
   2.672 -  "- of_num n * - of_num m = of_num n * of_num m"
   2.673 -  by simp_all
   2.674 -
   2.675 -lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n"
   2.676 -by (induct n)
   2.677 -  (simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all)
   2.678 -
   2.679 -declare of_int_1 [numeral]
   2.680 -
   2.681 -end
   2.682 -
   2.683 -
   2.684 -subsubsection {* Structures with exponentiation *}
   2.685 -
   2.686 -lemma of_num_square: "of_num (square x) = of_num x * of_num x"
   2.687 -by (induct x)
   2.688 -   (simp_all add: of_num.simps of_num_add algebra_simps)
   2.689 -
   2.690 -lemma of_num_pow: "of_num (pow x y) = of_num x ^ of_num y"
   2.691 -by (induct y)
   2.692 -   (simp_all add: of_num.simps of_num_square of_num_mult power_add)
   2.693 -
   2.694 -lemma power_of_num [numeral]: "of_num x ^ of_num y = of_num (pow x y)"
   2.695 -  unfolding of_num_pow ..
   2.696 -
   2.697 -lemma power_zero_of_num [numeral]:
   2.698 -  "0 ^ of_num n = (0::'a::semiring_1)"
   2.699 -  using of_num_pos [where n=n and ?'a=nat]
   2.700 -  by (simp add: power_0_left)
   2.701 -
   2.702 -lemma power_minus_Dig0 [numeral]:
   2.703 -  fixes x :: "'a::ring_1"
   2.704 -  shows "(- x) ^ of_num (Dig0 n) = x ^ of_num (Dig0 n)"
   2.705 -  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
   2.706 -
   2.707 -lemma power_minus_Dig1 [numeral]:
   2.708 -  fixes x :: "'a::ring_1"
   2.709 -  shows "(- x) ^ of_num (Dig1 n) = - (x ^ of_num (Dig1 n))"
   2.710 -  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
   2.711 -
   2.712 -declare power_one [numeral]
   2.713 -
   2.714 -
   2.715 -subsubsection {* Greetings to @{typ nat}. *}
   2.716 -
   2.717 -instance nat :: semiring_1_minus proof
   2.718 -qed simp_all
   2.719 -
   2.720 -lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)"
   2.721 -  unfolding of_num_plus_one [symmetric] by simp
   2.722 -
   2.723 -lemma nat_number:
   2.724 -  "1 = Suc 0"
   2.725 -  "of_num One = Suc 0"
   2.726 -  "of_num (Dig0 n) = Suc (of_num (DigM n))"
   2.727 -  "of_num (Dig1 n) = Suc (of_num (Dig0 n))"
   2.728 -  by (simp_all add: of_num.simps DigM_plus_one Suc_of_num)
   2.729 -
   2.730 -declare diff_0_eq_0 [numeral]
   2.731 -
   2.732 -
   2.733 -subsection {* Proof tools setup *}
   2.734 -
   2.735 -subsubsection {* Numeral equations as default simplification rules *}
   2.736 -
   2.737 -declare (in semiring_numeral) of_num_One [simp]
   2.738 -declare (in semiring_numeral) of_num_plus_one [simp]
   2.739 -declare (in semiring_numeral) of_num_one_plus [simp]
   2.740 -declare (in semiring_numeral) of_num_plus [simp]
   2.741 -declare (in semiring_numeral) of_num_times [simp]
   2.742 -
   2.743 -declare (in semiring_1) of_nat_of_num [simp]
   2.744 -
   2.745 -declare (in semiring_char_0) of_num_eq_iff [simp]
   2.746 -declare (in semiring_char_0) of_num_eq_one_iff [simp]
   2.747 -declare (in semiring_char_0) one_eq_of_num_iff [simp]
   2.748 -
   2.749 -declare (in linordered_semidom) of_num_pos [simp]
   2.750 -declare (in linordered_semidom) of_num_not_zero [simp]
   2.751 -declare (in linordered_semidom) of_num_less_eq_iff [simp]
   2.752 -declare (in linordered_semidom) of_num_less_eq_one_iff [simp]
   2.753 -declare (in linordered_semidom) one_less_eq_of_num_iff [simp]
   2.754 -declare (in linordered_semidom) of_num_less_iff [simp]
   2.755 -declare (in linordered_semidom) of_num_less_one_iff [simp]
   2.756 -declare (in linordered_semidom) one_less_of_num_iff [simp]
   2.757 -declare (in linordered_semidom) of_num_nonneg [simp]
   2.758 -declare (in linordered_semidom) of_num_less_zero_iff [simp]
   2.759 -declare (in linordered_semidom) of_num_le_zero_iff [simp]
   2.760 -
   2.761 -declare (in linordered_idom) le_signed_numeral_special [simp]
   2.762 -declare (in linordered_idom) less_signed_numeral_special [simp]
   2.763 -
   2.764 -declare (in semiring_1_minus) Dig_of_num_minus_one [simp]
   2.765 -declare (in semiring_1_minus) Dig_one_minus_of_num [simp]
   2.766 -
   2.767 -declare (in ring_1) Dig_plus_uminus [simp]
   2.768 -declare (in ring_1) of_int_of_num [simp]
   2.769 -
   2.770 -declare power_of_num [simp]
   2.771 -declare power_zero_of_num [simp]
   2.772 -declare power_minus_Dig0 [simp]
   2.773 -declare power_minus_Dig1 [simp]
   2.774 -
   2.775 -declare Suc_of_num [simp]
   2.776 -
   2.777 -
   2.778 -subsubsection {* Reorientation of equalities *}
   2.779 -
   2.780 -setup {*
   2.781 -  Reorient_Proc.add
   2.782 -    (fn Const(@{const_name of_num}, _) $ _ => true
   2.783 -      | Const(@{const_name uminus}, _) $
   2.784 -          (Const(@{const_name of_num}, _) $ _) => true
   2.785 -      | _ => false)
   2.786 -*}
   2.787 -
   2.788 -simproc_setup reorient_num ("of_num n = x" | "- of_num m = y") = Reorient_Proc.proc
   2.789 -
   2.790 -
   2.791 -subsubsection {* Constant folding for multiplication in semirings *}
   2.792 -
   2.793 -context semiring_numeral
   2.794 -begin
   2.795 -
   2.796 -lemma mult_of_num_commute: "x * of_num n = of_num n * x"
   2.797 -by (induct n)
   2.798 -  (simp_all only: of_num.simps left_distrib right_distrib mult_1_left mult_1_right)
   2.799 -
   2.800 -definition
   2.801 -  "commutes_with a b \<longleftrightarrow> a * b = b * a"
   2.802 -
   2.803 -lemma commutes_with_commute: "commutes_with a b \<Longrightarrow> a * b = b * a"
   2.804 -unfolding commutes_with_def .
   2.805 -
   2.806 -lemma commutes_with_left_commute: "commutes_with a b \<Longrightarrow> a * (b * c) = b * (a * c)"
   2.807 -unfolding commutes_with_def by (simp only: mult_assoc [symmetric])
   2.808 -
   2.809 -lemma commutes_with_numeral: "commutes_with x (of_num n)" "commutes_with (of_num n) x"
   2.810 -unfolding commutes_with_def by (simp_all add: mult_of_num_commute)
   2.811 -
   2.812 -lemmas mult_ac_numeral =
   2.813 -  mult_assoc
   2.814 -  commutes_with_commute
   2.815 -  commutes_with_left_commute
   2.816 -  commutes_with_numeral
   2.817 -
   2.818 -end
   2.819 -
   2.820 -ML {*
   2.821 -structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
   2.822 -struct
   2.823 -  val assoc_ss = HOL_ss addsimps @{thms mult_ac_numeral}
   2.824 -  val eq_reflection = eq_reflection
   2.825 -  fun is_numeral (Const(@{const_name of_num}, _) $ _) = true
   2.826 -    | is_numeral _ = false;
   2.827 -end;
   2.828 -
   2.829 -structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
   2.830 -*}
   2.831 -
   2.832 -simproc_setup semiring_assoc_fold' ("(a::'a::semiring_numeral) * b") =
   2.833 -  {* fn phi => fn ss => fn ct =>
   2.834 -    Semiring_Times_Assoc.proc ss (Thm.term_of ct) *}
   2.835 -
   2.836 -
   2.837 -subsection {* Code generator setup for @{typ int} *}
   2.838 -
   2.839 -text {* Reversing standard setup *}
   2.840 -
   2.841 -lemma [code_unfold del]: "(0::int) \<equiv> Numeral0" by simp
   2.842 -lemma [code_unfold del]: "(1::int) \<equiv> Numeral1" by simp
   2.843 -declare zero_is_num_zero [code_unfold del]
   2.844 -declare one_is_num_one [code_unfold del]
   2.845 -  
   2.846 -lemma [code, code del]:
   2.847 -  "(1 :: int) = 1"
   2.848 -  "(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +"
   2.849 -  "(uminus :: int \<Rightarrow> int) = uminus"
   2.850 -  "(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -"
   2.851 -  "(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *"
   2.852 -  "(HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool) = HOL.equal"
   2.853 -  "(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>"
   2.854 -  "(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <"
   2.855 -  by rule+
   2.856 -
   2.857 -text {* Constructors *}
   2.858 -
   2.859 -definition Pls :: "num \<Rightarrow> int" where
   2.860 -  [simp, code_post]: "Pls n = of_num n"
   2.861 -
   2.862 -definition Mns :: "num \<Rightarrow> int" where
   2.863 -  [simp, code_post]: "Mns n = - of_num n"
   2.864 -
   2.865 -code_datatype "0::int" Pls Mns
   2.866 -
   2.867 -lemmas [code_unfold] = Pls_def [symmetric] Mns_def [symmetric]
   2.868 -
   2.869 -text {* Auxiliary operations *}
   2.870 -
   2.871 -definition dup :: "int \<Rightarrow> int" where
   2.872 -  [simp]: "dup k = k + k"
   2.873 -
   2.874 -lemma Dig_dup [code]:
   2.875 -  "dup 0 = 0"
   2.876 -  "dup (Pls n) = Pls (Dig0 n)"
   2.877 -  "dup (Mns n) = Mns (Dig0 n)"
   2.878 -  by (simp_all add: of_num.simps)
   2.879 -
   2.880 -definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
   2.881 -  [simp]: "sub m n = (of_num m - of_num n)"
   2.882 -
   2.883 -lemma Dig_sub [code]:
   2.884 -  "sub One One = 0"
   2.885 -  "sub (Dig0 m) One = of_num (DigM m)"
   2.886 -  "sub (Dig1 m) One = of_num (Dig0 m)"
   2.887 -  "sub One (Dig0 n) = - of_num (DigM n)"
   2.888 -  "sub One (Dig1 n) = - of_num (Dig0 n)"
   2.889 -  "sub (Dig0 m) (Dig0 n) = dup (sub m n)"
   2.890 -  "sub (Dig1 m) (Dig1 n) = dup (sub m n)"
   2.891 -  "sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1"
   2.892 -  "sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1"
   2.893 -  by (simp_all add: algebra_simps num_eq_iff nat_of_num_add)
   2.894 -
   2.895 -text {* Implementations *}
   2.896 -
   2.897 -lemma one_int_code [code]:
   2.898 -  "1 = Pls One"
   2.899 -  by simp
   2.900 -
   2.901 -lemma plus_int_code [code]:
   2.902 -  "k + 0 = (k::int)"
   2.903 -  "0 + l = (l::int)"
   2.904 -  "Pls m + Pls n = Pls (m + n)"
   2.905 -  "Pls m + Mns n = sub m n"
   2.906 -  "Mns m + Pls n = sub n m"
   2.907 -  "Mns m + Mns n = Mns (m + n)"
   2.908 -  by simp_all
   2.909 -
   2.910 -lemma uminus_int_code [code]:
   2.911 -  "uminus 0 = (0::int)"
   2.912 -  "uminus (Pls m) = Mns m"
   2.913 -  "uminus (Mns m) = Pls m"
   2.914 -  by simp_all
   2.915 -
   2.916 -lemma minus_int_code [code]:
   2.917 -  "k - 0 = (k::int)"
   2.918 -  "0 - l = uminus (l::int)"
   2.919 -  "Pls m - Pls n = sub m n"
   2.920 -  "Pls m - Mns n = Pls (m + n)"
   2.921 -  "Mns m - Pls n = Mns (m + n)"
   2.922 -  "Mns m - Mns n = sub n m"
   2.923 -  by simp_all
   2.924 -
   2.925 -lemma times_int_code [code]:
   2.926 -  "k * 0 = (0::int)"
   2.927 -  "0 * l = (0::int)"
   2.928 -  "Pls m * Pls n = Pls (m * n)"
   2.929 -  "Pls m * Mns n = Mns (m * n)"
   2.930 -  "Mns m * Pls n = Mns (m * n)"
   2.931 -  "Mns m * Mns n = Pls (m * n)"
   2.932 -  by simp_all
   2.933 -
   2.934 -lemma eq_int_code [code]:
   2.935 -  "HOL.equal 0 (0::int) \<longleftrightarrow> True"
   2.936 -  "HOL.equal 0 (Pls l) \<longleftrightarrow> False"
   2.937 -  "HOL.equal 0 (Mns l) \<longleftrightarrow> False"
   2.938 -  "HOL.equal (Pls k) 0 \<longleftrightarrow> False"
   2.939 -  "HOL.equal (Pls k) (Pls l) \<longleftrightarrow> HOL.equal k l"
   2.940 -  "HOL.equal (Pls k) (Mns l) \<longleftrightarrow> False"
   2.941 -  "HOL.equal (Mns k) 0 \<longleftrightarrow> False"
   2.942 -  "HOL.equal (Mns k) (Pls l) \<longleftrightarrow> False"
   2.943 -  "HOL.equal (Mns k) (Mns l) \<longleftrightarrow> HOL.equal k l"
   2.944 -  by (auto simp add: equal dest: sym)
   2.945 -
   2.946 -lemma [code nbe]:
   2.947 -  "HOL.equal (k::int) k \<longleftrightarrow> True"
   2.948 -  by (fact equal_refl)
   2.949 -
   2.950 -lemma less_eq_int_code [code]:
   2.951 -  "0 \<le> (0::int) \<longleftrightarrow> True"
   2.952 -  "0 \<le> Pls l \<longleftrightarrow> True"
   2.953 -  "0 \<le> Mns l \<longleftrightarrow> False"
   2.954 -  "Pls k \<le> 0 \<longleftrightarrow> False"
   2.955 -  "Pls k \<le> Pls l \<longleftrightarrow> k \<le> l"
   2.956 -  "Pls k \<le> Mns l \<longleftrightarrow> False"
   2.957 -  "Mns k \<le> 0 \<longleftrightarrow> True"
   2.958 -  "Mns k \<le> Pls l \<longleftrightarrow> True"
   2.959 -  "Mns k \<le> Mns l \<longleftrightarrow> l \<le> k"
   2.960 -  by simp_all
   2.961 -
   2.962 -lemma less_int_code [code]:
   2.963 -  "0 < (0::int) \<longleftrightarrow> False"
   2.964 -  "0 < Pls l \<longleftrightarrow> True"
   2.965 -  "0 < Mns l \<longleftrightarrow> False"
   2.966 -  "Pls k < 0 \<longleftrightarrow> False"
   2.967 -  "Pls k < Pls l \<longleftrightarrow> k < l"
   2.968 -  "Pls k < Mns l \<longleftrightarrow> False"
   2.969 -  "Mns k < 0 \<longleftrightarrow> True"
   2.970 -  "Mns k < Pls l \<longleftrightarrow> True"
   2.971 -  "Mns k < Mns l \<longleftrightarrow> l < k"
   2.972 -  by simp_all
   2.973 -
   2.974 -hide_const (open) sub dup
   2.975 -
   2.976 -text {* Pretty literals *}
   2.977 -
   2.978 -ML {*
   2.979 -local open Code_Thingol in
   2.980 -
   2.981 -fun add_code print target =
   2.982 -  let
   2.983 -    fun dest_num one' dig0' dig1' thm =
   2.984 -      let
   2.985 -        fun dest_dig (IConst (c, _)) = if c = dig0' then 0
   2.986 -              else if c = dig1' then 1
   2.987 -              else Code_Printer.eqn_error thm "Illegal numeral expression: illegal dig"
   2.988 -          | dest_dig _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal digit";
   2.989 -        fun dest_num (IConst (c, _)) = if c = one' then 1
   2.990 -              else Code_Printer.eqn_error thm "Illegal numeral expression: illegal leading digit"
   2.991 -          | dest_num (t1 `$ t2) = 2 * dest_num t2 + dest_dig t1
   2.992 -          | dest_num _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal term";
   2.993 -      in dest_num end;
   2.994 -    fun pretty sgn literals [one', dig0', dig1'] _ thm _ _ [(t, _)] =
   2.995 -      (Code_Printer.str o print literals o sgn o dest_num one' dig0' dig1' thm) t
   2.996 -    fun add_syntax (c, sgn) = Code_Target.add_const_syntax target c
   2.997 -      (SOME (Code_Printer.complex_const_syntax
   2.998 -        (1, ([@{const_name One}, @{const_name Dig0}, @{const_name Dig1}],
   2.999 -          pretty sgn))));
  2.1000 -  in
  2.1001 -    add_syntax (@{const_name Pls}, I)
  2.1002 -    #> add_syntax (@{const_name Mns}, (fn k => ~ k))
  2.1003 -  end;
  2.1004 -
  2.1005 -end
  2.1006 -*}
  2.1007 -
  2.1008 -hide_const (open) One Dig0 Dig1
  2.1009 -
  2.1010 -
  2.1011 -subsection {* Toy examples *}
  2.1012 -
  2.1013 -definition "foo \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat)"
  2.1014 -definition "bar \<longleftrightarrow> #4 * #2 + #7 \<ge> (#8 :: int) - #3"
  2.1015 -
  2.1016 -code_thms foo bar
  2.1017 -export_code foo bar checking SML OCaml? Haskell? Scala?
  2.1018 -
  2.1019 -text {* This is an ad-hoc @{text Code_Integer} setup. *}
  2.1020 -
  2.1021 -setup {*
  2.1022 -  fold (add_code Code_Printer.literal_numeral)
  2.1023 -    [Code_ML.target_SML, Code_ML.target_OCaml, Code_Haskell.target, Code_Scala.target]
  2.1024 -*}
  2.1025 -
  2.1026 -code_type int
  2.1027 -  (SML "IntInf.int")
  2.1028 -  (OCaml "Big'_int.big'_int")
  2.1029 -  (Haskell "Integer")
  2.1030 -  (Scala "BigInt")
  2.1031 -  (Eval "int")
  2.1032 -
  2.1033 -code_const "0::int"
  2.1034 -  (SML "0/ :/ IntInf.int")
  2.1035 -  (OCaml "Big'_int.zero")
  2.1036 -  (Haskell "0")
  2.1037 -  (Scala "BigInt(0)")
  2.1038 -  (Eval "0/ :/ int")
  2.1039 -
  2.1040 -code_const Int.pred
  2.1041 -  (SML "IntInf.- ((_), 1)")
  2.1042 -  (OCaml "Big'_int.pred'_big'_int")
  2.1043 -  (Haskell "!(_/ -/ 1)")
  2.1044 -  (Scala "!(_ -/ 1)")
  2.1045 -  (Eval "!(_/ -/ 1)")
  2.1046 -
  2.1047 -code_const Int.succ
  2.1048 -  (SML "IntInf.+ ((_), 1)")
  2.1049 -  (OCaml "Big'_int.succ'_big'_int")
  2.1050 -  (Haskell "!(_/ +/ 1)")
  2.1051 -  (Scala "!(_ +/ 1)")
  2.1052 -  (Eval "!(_/ +/ 1)")
  2.1053 -
  2.1054 -code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
  2.1055 -  (SML "IntInf.+ ((_), (_))")
  2.1056 -  (OCaml "Big'_int.add'_big'_int")
  2.1057 -  (Haskell infixl 6 "+")
  2.1058 -  (Scala infixl 7 "+")
  2.1059 -  (Eval infixl 8 "+")
  2.1060 -
  2.1061 -code_const "uminus \<Colon> int \<Rightarrow> int"
  2.1062 -  (SML "IntInf.~")
  2.1063 -  (OCaml "Big'_int.minus'_big'_int")
  2.1064 -  (Haskell "negate")
  2.1065 -  (Scala "!(- _)")
  2.1066 -  (Eval "~/ _")
  2.1067 -
  2.1068 -code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
  2.1069 -  (SML "IntInf.- ((_), (_))")
  2.1070 -  (OCaml "Big'_int.sub'_big'_int")
  2.1071 -  (Haskell infixl 6 "-")
  2.1072 -  (Scala infixl 7 "-")
  2.1073 -  (Eval infixl 8 "-")
  2.1074 -
  2.1075 -code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
  2.1076 -  (SML "IntInf.* ((_), (_))")
  2.1077 -  (OCaml "Big'_int.mult'_big'_int")
  2.1078 -  (Haskell infixl 7 "*")
  2.1079 -  (Scala infixl 8 "*")
  2.1080 -  (Eval infixl 9 "*")
  2.1081 -
  2.1082 -code_const pdivmod
  2.1083 -  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
  2.1084 -  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
  2.1085 -  (Haskell "divMod/ (abs _)/ (abs _)")
  2.1086 -  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
  2.1087 -  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
  2.1088 -
  2.1089 -code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
  2.1090 -  (SML "!((_ : IntInf.int) = _)")
  2.1091 -  (OCaml "Big'_int.eq'_big'_int")
  2.1092 -  (Haskell infix 4 "==")
  2.1093 -  (Scala infixl 5 "==")
  2.1094 -  (Eval infixl 6 "=")
  2.1095 -
  2.1096 -code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
  2.1097 -  (SML "IntInf.<= ((_), (_))")
  2.1098 -  (OCaml "Big'_int.le'_big'_int")
  2.1099 -  (Haskell infix 4 "<=")
  2.1100 -  (Scala infixl 4 "<=")
  2.1101 -  (Eval infixl 6 "<=")
  2.1102 -
  2.1103 -code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
  2.1104 -  (SML "IntInf.< ((_), (_))")
  2.1105 -  (OCaml "Big'_int.lt'_big'_int")
  2.1106 -  (Haskell infix 4 "<")
  2.1107 -  (Scala infixl 4 "<")
  2.1108 -  (Eval infixl 6 "<")
  2.1109 -
  2.1110 -code_const Code_Numeral.int_of
  2.1111 -  (SML "IntInf.fromInt")
  2.1112 -  (OCaml "_")
  2.1113 -  (Haskell "toInteger")
  2.1114 -  (Scala "!_.as'_BigInt")
  2.1115 -  (Eval "_")
  2.1116 -
  2.1117 -export_code foo bar checking SML OCaml? Haskell? Scala?
  2.1118 -
  2.1119 -end
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/ex/Numeral_Representation.thy	Tue Feb 21 09:17:53 2012 +0100
     3.3 @@ -0,0 +1,1116 @@
     3.4 +(*  Title:      HOL/ex/Numeral_Representation.thy
     3.5 +    Author:     Florian Haftmann
     3.6 +*)
     3.7 +
     3.8 +header {* An experimental alternative numeral representation. *}
     3.9 +
    3.10 +theory Numeral_Representation
    3.11 +imports Main
    3.12 +begin
    3.13 +
    3.14 +subsection {* The @{text num} type *}
    3.15 +
    3.16 +datatype num = One | Dig0 num | Dig1 num
    3.17 +
    3.18 +text {* Increment function for type @{typ num} *}
    3.19 +
    3.20 +primrec inc :: "num \<Rightarrow> num" where
    3.21 +  "inc One = Dig0 One"
    3.22 +| "inc (Dig0 x) = Dig1 x"
    3.23 +| "inc (Dig1 x) = Dig0 (inc x)"
    3.24 +
    3.25 +text {* Converting between type @{typ num} and type @{typ nat} *}
    3.26 +
    3.27 +primrec nat_of_num :: "num \<Rightarrow> nat" where
    3.28 +  "nat_of_num One = Suc 0"
    3.29 +| "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x"
    3.30 +| "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)"
    3.31 +
    3.32 +primrec num_of_nat :: "nat \<Rightarrow> num" where
    3.33 +  "num_of_nat 0 = One"
    3.34 +| "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)"
    3.35 +
    3.36 +lemma nat_of_num_pos: "0 < nat_of_num x"
    3.37 +  by (induct x) simp_all
    3.38 +
    3.39 +lemma nat_of_num_neq_0: "nat_of_num x \<noteq> 0"
    3.40 +  by (induct x) simp_all
    3.41 +
    3.42 +lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)"
    3.43 +  by (induct x) simp_all
    3.44 +
    3.45 +lemma num_of_nat_double:
    3.46 +  "0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)"
    3.47 +  by (induct n) simp_all
    3.48 +
    3.49 +text {*
    3.50 +  Type @{typ num} is isomorphic to the strictly positive
    3.51 +  natural numbers.
    3.52 +*}
    3.53 +
    3.54 +lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x"
    3.55 +  by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos)
    3.56 +
    3.57 +lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n"
    3.58 +  by (induct n) (simp_all add: nat_of_num_inc)
    3.59 +
    3.60 +lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y"
    3.61 +proof
    3.62 +  assume "nat_of_num x = nat_of_num y"
    3.63 +  then have "num_of_nat (nat_of_num x) = num_of_nat (nat_of_num y)" by simp
    3.64 +  then show "x = y" by (simp add: nat_of_num_inverse)
    3.65 +qed simp
    3.66 +
    3.67 +lemma num_induct [case_names One inc]:
    3.68 +  fixes P :: "num \<Rightarrow> bool"
    3.69 +  assumes One: "P One"
    3.70 +    and inc: "\<And>x. P x \<Longrightarrow> P (inc x)"
    3.71 +  shows "P x"
    3.72 +proof -
    3.73 +  obtain n where n: "Suc n = nat_of_num x"
    3.74 +    by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0)
    3.75 +  have "P (num_of_nat (Suc n))"
    3.76 +  proof (induct n)
    3.77 +    case 0 show ?case using One by simp
    3.78 +  next
    3.79 +    case (Suc n)
    3.80 +    then have "P (inc (num_of_nat (Suc n)))" by (rule inc)
    3.81 +    then show "P (num_of_nat (Suc (Suc n)))" by simp
    3.82 +  qed
    3.83 +  with n show "P x"
    3.84 +    by (simp add: nat_of_num_inverse)
    3.85 +qed
    3.86 +
    3.87 +text {*
    3.88 +  From now on, there are two possible models for @{typ num}: as
    3.89 +  positive naturals (rule @{text "num_induct"}) and as digit
    3.90 +  representation (rules @{text "num.induct"}, @{text "num.cases"}).
    3.91 +
    3.92 +  It is not entirely clear in which context it is better to use the
    3.93 +  one or the other, or whether the construction should be reversed.
    3.94 +*}
    3.95 +
    3.96 +
    3.97 +subsection {* Numeral operations *}
    3.98 +
    3.99 +ML {*
   3.100 +structure Dig_Simps = Named_Thms
   3.101 +(
   3.102 +  val name = @{binding numeral}
   3.103 +  val description = "simplification rules for numerals"
   3.104 +)
   3.105 +*}
   3.106 +
   3.107 +setup Dig_Simps.setup
   3.108 +
   3.109 +instantiation num :: "{plus,times,ord}"
   3.110 +begin
   3.111 +
   3.112 +definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where
   3.113 +  "m + n = num_of_nat (nat_of_num m + nat_of_num n)"
   3.114 +
   3.115 +definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where
   3.116 +  "m * n = num_of_nat (nat_of_num m * nat_of_num n)"
   3.117 +
   3.118 +definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
   3.119 +  "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n"
   3.120 +
   3.121 +definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where
   3.122 +  "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n"
   3.123 +
   3.124 +instance ..
   3.125 +
   3.126 +end
   3.127 +
   3.128 +lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y"
   3.129 +  unfolding plus_num_def
   3.130 +  by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
   3.131 +
   3.132 +lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y"
   3.133 +  unfolding times_num_def
   3.134 +  by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
   3.135 +
   3.136 +lemma Dig_plus [numeral, simp, code]:
   3.137 +  "One + One = Dig0 One"
   3.138 +  "One + Dig0 m = Dig1 m"
   3.139 +  "One + Dig1 m = Dig0 (m + One)"
   3.140 +  "Dig0 n + One = Dig1 n"
   3.141 +  "Dig0 n + Dig0 m = Dig0 (n + m)"
   3.142 +  "Dig0 n + Dig1 m = Dig1 (n + m)"
   3.143 +  "Dig1 n + One = Dig0 (n + One)"
   3.144 +  "Dig1 n + Dig0 m = Dig1 (n + m)"
   3.145 +  "Dig1 n + Dig1 m = Dig0 (n + m + One)"
   3.146 +  by (simp_all add: num_eq_iff nat_of_num_add)
   3.147 +
   3.148 +lemma Dig_times [numeral, simp, code]:
   3.149 +  "One * One = One"
   3.150 +  "One * Dig0 n = Dig0 n"
   3.151 +  "One * Dig1 n = Dig1 n"
   3.152 +  "Dig0 n * One = Dig0 n"
   3.153 +  "Dig0 n * Dig0 m = Dig0 (n * Dig0 m)"
   3.154 +  "Dig0 n * Dig1 m = Dig0 (n * Dig1 m)"
   3.155 +  "Dig1 n * One = Dig1 n"
   3.156 +  "Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)"
   3.157 +  "Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)"
   3.158 +  by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult
   3.159 +                    left_distrib right_distrib)
   3.160 +
   3.161 +lemma less_eq_num_code [numeral, simp, code]:
   3.162 +  "One \<le> n \<longleftrightarrow> True"
   3.163 +  "Dig0 m \<le> One \<longleftrightarrow> False"
   3.164 +  "Dig1 m \<le> One \<longleftrightarrow> False"
   3.165 +  "Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n"
   3.166 +  "Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
   3.167 +  "Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n"
   3.168 +  "Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n"
   3.169 +  using nat_of_num_pos [of n] nat_of_num_pos [of m]
   3.170 +  by (auto simp add: less_eq_num_def less_num_def)
   3.171 +
   3.172 +lemma less_num_code [numeral, simp, code]:
   3.173 +  "m < One \<longleftrightarrow> False"
   3.174 +  "One < One \<longleftrightarrow> False"
   3.175 +  "One < Dig0 n \<longleftrightarrow> True"
   3.176 +  "One < Dig1 n \<longleftrightarrow> True"
   3.177 +  "Dig0 m < Dig0 n \<longleftrightarrow> m < n"
   3.178 +  "Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n"
   3.179 +  "Dig1 m < Dig1 n \<longleftrightarrow> m < n"
   3.180 +  "Dig1 m < Dig0 n \<longleftrightarrow> m < n"
   3.181 +  using nat_of_num_pos [of n] nat_of_num_pos [of m]
   3.182 +  by (auto simp add: less_eq_num_def less_num_def)
   3.183 +
   3.184 +text {* Rules using @{text One} and @{text inc} as constructors *}
   3.185 +
   3.186 +lemma add_One: "x + One = inc x"
   3.187 +  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   3.188 +
   3.189 +lemma add_inc: "x + inc y = inc (x + y)"
   3.190 +  by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
   3.191 +
   3.192 +lemma mult_One: "x * One = x"
   3.193 +  by (simp add: num_eq_iff nat_of_num_mult)
   3.194 +
   3.195 +lemma mult_inc: "x * inc y = x * y + x"
   3.196 +  by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
   3.197 +
   3.198 +text {* A double-and-decrement function *}
   3.199 +
   3.200 +primrec DigM :: "num \<Rightarrow> num" where
   3.201 +  "DigM One = One"
   3.202 +| "DigM (Dig0 n) = Dig1 (DigM n)"
   3.203 +| "DigM (Dig1 n) = Dig1 (Dig0 n)"
   3.204 +
   3.205 +lemma DigM_plus_one: "DigM n + One = Dig0 n"
   3.206 +  by (induct n) simp_all
   3.207 +
   3.208 +lemma add_One_commute: "One + n = n + One"
   3.209 +  by (induct n) simp_all
   3.210 +
   3.211 +lemma one_plus_DigM: "One + DigM n = Dig0 n"
   3.212 +  by (simp add: add_One_commute DigM_plus_one)
   3.213 +
   3.214 +text {* Squaring and exponentiation *}
   3.215 +
   3.216 +primrec square :: "num \<Rightarrow> num" where
   3.217 +  "square One = One"
   3.218 +| "square (Dig0 n) = Dig0 (Dig0 (square n))"
   3.219 +| "square (Dig1 n) = Dig1 (Dig0 (square n + n))"
   3.220 +
   3.221 +primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" where
   3.222 +  "pow x One = x"
   3.223 +| "pow x (Dig0 y) = square (pow x y)"
   3.224 +| "pow x (Dig1 y) = x * square (pow x y)"
   3.225 +
   3.226 +
   3.227 +subsection {* Binary numerals *}
   3.228 +
   3.229 +text {*
   3.230 +  We embed binary representations into a generic algebraic
   3.231 +  structure using @{text of_num}.
   3.232 +*}
   3.233 +
   3.234 +class semiring_numeral = semiring + monoid_mult
   3.235 +begin
   3.236 +
   3.237 +primrec of_num :: "num \<Rightarrow> 'a" where
   3.238 +  of_num_One [numeral]: "of_num One = 1"
   3.239 +| "of_num (Dig0 n) = of_num n + of_num n"
   3.240 +| "of_num (Dig1 n) = of_num n + of_num n + 1"
   3.241 +
   3.242 +lemma of_num_inc: "of_num (inc n) = of_num n + 1"
   3.243 +  by (induct n) (simp_all add: add_ac)
   3.244 +
   3.245 +lemma of_num_add: "of_num (m + n) = of_num m + of_num n"
   3.246 +  by (induct n rule: num_induct) (simp_all add: add_One add_inc of_num_inc add_ac)
   3.247 +
   3.248 +lemma of_num_mult: "of_num (m * n) = of_num m * of_num n"
   3.249 +  by (induct n rule: num_induct) (simp_all add: mult_One mult_inc of_num_add of_num_inc right_distrib)
   3.250 +
   3.251 +declare of_num.simps [simp del]
   3.252 +
   3.253 +end
   3.254 +
   3.255 +ML {*
   3.256 +fun mk_num k =
   3.257 +  if k > 1 then
   3.258 +    let
   3.259 +      val (l, b) = Integer.div_mod k 2;
   3.260 +      val bit = (if b = 0 then @{term Dig0} else @{term Dig1});
   3.261 +    in bit $ (mk_num l) end
   3.262 +  else if k = 1 then @{term One}
   3.263 +  else error ("mk_num " ^ string_of_int k);
   3.264 +
   3.265 +fun dest_num @{term One} = 1
   3.266 +  | dest_num (@{term Dig0} $ n) = 2 * dest_num n
   3.267 +  | dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1
   3.268 +  | dest_num t = raise TERM ("dest_num", [t]);
   3.269 +
   3.270 +fun mk_numeral phi T k = Morphism.term phi (Const (@{const_name of_num}, @{typ num} --> T))
   3.271 +  $ mk_num k
   3.272 +
   3.273 +fun dest_numeral phi (u $ t) =
   3.274 +  if Term.aconv_untyped (u, Morphism.term phi (Const (@{const_name of_num}, dummyT)))
   3.275 +  then (range_type (fastype_of u), dest_num t)
   3.276 +  else raise TERM ("dest_numeral", [u, t]);
   3.277 +*}
   3.278 +
   3.279 +syntax
   3.280 +  "_Numerals" :: "xnum_token \<Rightarrow> 'a"    ("_")
   3.281 +
   3.282 +parse_translation {*
   3.283 +let
   3.284 +  fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2)
   3.285 +     of (0, 1) => Const (@{const_name One}, dummyT)
   3.286 +      | (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n
   3.287 +      | (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n
   3.288 +    else raise Match;
   3.289 +  fun numeral_tr [Free (num, _)] =
   3.290 +        let
   3.291 +          val {leading_zeros, value, ...} = Lexicon.read_xnum num;
   3.292 +          val _ = leading_zeros = 0 andalso value > 0
   3.293 +            orelse error ("Bad numeral: " ^ num);
   3.294 +        in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end
   3.295 +    | numeral_tr ts = raise TERM ("numeral_tr", ts);
   3.296 +in [(@{syntax_const "_Numerals"}, numeral_tr)] end
   3.297 +*}
   3.298 +
   3.299 +typed_print_translation (advanced) {*
   3.300 +let
   3.301 +  fun dig b n = b + 2 * n; 
   3.302 +  fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) =
   3.303 +        dig 0 (int_of_num' n)
   3.304 +    | int_of_num' (Const (@{const_syntax Dig1}, _) $ n) =
   3.305 +        dig 1 (int_of_num' n)
   3.306 +    | int_of_num' (Const (@{const_syntax One}, _)) = 1;
   3.307 +  fun num_tr' ctxt T [n] =
   3.308 +    let
   3.309 +      val k = int_of_num' n;
   3.310 +      val t' = Syntax.const @{syntax_const "_Numerals"} $ Syntax.free ("#" ^ string_of_int k);
   3.311 +    in
   3.312 +      case T of
   3.313 +        Type (@{type_name fun}, [_, T']) =>
   3.314 +          if not (Config.get ctxt show_types) andalso can Term.dest_Type T' then t'
   3.315 +          else Syntax.const @{syntax_const "_constrain"} $ t' $ Syntax_Phases.term_of_typ ctxt T'
   3.316 +      | T' => if T' = dummyT then t' else raise Match
   3.317 +    end;
   3.318 +in [(@{const_syntax of_num}, num_tr')] end
   3.319 +*}
   3.320 +
   3.321 +
   3.322 +subsection {* Class-specific numeral rules *}
   3.323 +
   3.324 +subsubsection {* Class @{text semiring_numeral} *}
   3.325 +
   3.326 +context semiring_numeral
   3.327 +begin
   3.328 +
   3.329 +abbreviation "Num1 \<equiv> of_num One"
   3.330 +
   3.331 +text {*
   3.332 +  Alas, there is still the duplication of @{term 1}, although the
   3.333 +  duplicated @{term 0} has disappeared.  We could get rid of it by
   3.334 +  replacing the constructor @{term 1} in @{typ num} by two
   3.335 +  constructors @{text two} and @{text three}, resulting in a further
   3.336 +  blow-up.  But it could be worth the effort.
   3.337 +*}
   3.338 +
   3.339 +lemma of_num_plus_one [numeral]:
   3.340 +  "of_num n + 1 = of_num (n + One)"
   3.341 +  by (simp only: of_num_add of_num_One)
   3.342 +
   3.343 +lemma of_num_one_plus [numeral]:
   3.344 +  "1 + of_num n = of_num (One + n)"
   3.345 +  by (simp only: of_num_add of_num_One)
   3.346 +
   3.347 +lemma of_num_plus [numeral]:
   3.348 +  "of_num m + of_num n = of_num (m + n)"
   3.349 +  by (simp only: of_num_add)
   3.350 +
   3.351 +lemma of_num_times_one [numeral]:
   3.352 +  "of_num n * 1 = of_num n"
   3.353 +  by simp
   3.354 +
   3.355 +lemma of_num_one_times [numeral]:
   3.356 +  "1 * of_num n = of_num n"
   3.357 +  by simp
   3.358 +
   3.359 +lemma of_num_times [numeral]:
   3.360 +  "of_num m * of_num n = of_num (m * n)"
   3.361 +  unfolding of_num_mult ..
   3.362 +
   3.363 +end
   3.364 +
   3.365 +
   3.366 +subsubsection {* Structures with a zero: class @{text semiring_1} *}
   3.367 +
   3.368 +context semiring_1
   3.369 +begin
   3.370 +
   3.371 +subclass semiring_numeral ..
   3.372 +
   3.373 +lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n"
   3.374 +  by (induct n)
   3.375 +    (simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac)
   3.376 +
   3.377 +declare of_nat_1 [numeral]
   3.378 +
   3.379 +lemma Dig_plus_zero [numeral]:
   3.380 +  "0 + 1 = 1"
   3.381 +  "0 + of_num n = of_num n"
   3.382 +  "1 + 0 = 1"
   3.383 +  "of_num n + 0 = of_num n"
   3.384 +  by simp_all
   3.385 +
   3.386 +lemma Dig_times_zero [numeral]:
   3.387 +  "0 * 1 = 0"
   3.388 +  "0 * of_num n = 0"
   3.389 +  "1 * 0 = 0"
   3.390 +  "of_num n * 0 = 0"
   3.391 +  by simp_all
   3.392 +
   3.393 +end
   3.394 +
   3.395 +lemma nat_of_num_of_num: "nat_of_num = of_num"
   3.396 +proof
   3.397 +  fix n
   3.398 +  have "of_num n = nat_of_num n"
   3.399 +    by (induct n) (simp_all add: of_num.simps)
   3.400 +  then show "nat_of_num n = of_num n" by simp
   3.401 +qed
   3.402 +
   3.403 +
   3.404 +subsubsection {* Equality: class @{text semiring_char_0} *}
   3.405 +
   3.406 +context semiring_char_0
   3.407 +begin
   3.408 +
   3.409 +lemma of_num_eq_iff [numeral]: "of_num m = of_num n \<longleftrightarrow> m = n"
   3.410 +  unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric]
   3.411 +    of_nat_eq_iff num_eq_iff ..
   3.412 +
   3.413 +lemma of_num_eq_one_iff [numeral]: "of_num n = 1 \<longleftrightarrow> n = One"
   3.414 +  using of_num_eq_iff [of n One] by (simp add: of_num_One)
   3.415 +
   3.416 +lemma one_eq_of_num_iff [numeral]: "1 = of_num n \<longleftrightarrow> One = n"
   3.417 +  using of_num_eq_iff [of One n] by (simp add: of_num_One)
   3.418 +
   3.419 +end
   3.420 +
   3.421 +
   3.422 +subsubsection {* Comparisons: class @{text linordered_semidom} *}
   3.423 +
   3.424 +text {*
   3.425 +  Perhaps the underlying structure could even 
   3.426 +  be more general than @{text linordered_semidom}.
   3.427 +*}
   3.428 +
   3.429 +context linordered_semidom
   3.430 +begin
   3.431 +
   3.432 +lemma of_num_pos [numeral]: "0 < of_num n"
   3.433 +  by (induct n) (simp_all add: of_num.simps add_pos_pos)
   3.434 +
   3.435 +lemma of_num_not_zero [numeral]: "of_num n \<noteq> 0"
   3.436 +  using of_num_pos [of n] by simp
   3.437 +
   3.438 +lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n"
   3.439 +proof -
   3.440 +  have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n"
   3.441 +    unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff ..
   3.442 +  then show ?thesis by (simp add: of_nat_of_num)
   3.443 +qed
   3.444 +
   3.445 +lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n \<le> One"
   3.446 +  using of_num_less_eq_iff [of n One] by (simp add: of_num_One)
   3.447 +
   3.448 +lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n"
   3.449 +  using of_num_less_eq_iff [of One n] by (simp add: of_num_One)
   3.450 +
   3.451 +lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n"
   3.452 +proof -
   3.453 +  have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n"
   3.454 +    unfolding less_num_def nat_of_num_of_num of_nat_less_iff ..
   3.455 +  then show ?thesis by (simp add: of_nat_of_num)
   3.456 +qed
   3.457 +
   3.458 +lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1"
   3.459 +  using of_num_less_iff [of n One] by (simp add: of_num_One)
   3.460 +
   3.461 +lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> One < n"
   3.462 +  using of_num_less_iff [of One n] by (simp add: of_num_One)
   3.463 +
   3.464 +lemma of_num_nonneg [numeral]: "0 \<le> of_num n"
   3.465 +  by (induct n) (simp_all add: of_num.simps add_nonneg_nonneg)
   3.466 +
   3.467 +lemma of_num_less_zero_iff [numeral]: "\<not> of_num n < 0"
   3.468 +  by (simp add: not_less of_num_nonneg)
   3.469 +
   3.470 +lemma of_num_le_zero_iff [numeral]: "\<not> of_num n \<le> 0"
   3.471 +  by (simp add: not_le of_num_pos)
   3.472 +
   3.473 +end
   3.474 +
   3.475 +context linordered_idom
   3.476 +begin
   3.477 +
   3.478 +lemma minus_of_num_less_of_num_iff: "- of_num m < of_num n"
   3.479 +proof -
   3.480 +  have "- of_num m < 0" by (simp add: of_num_pos)
   3.481 +  also have "0 < of_num n" by (simp add: of_num_pos)
   3.482 +  finally show ?thesis .
   3.483 +qed
   3.484 +
   3.485 +lemma minus_of_num_not_equal_of_num: "- of_num m \<noteq> of_num n"
   3.486 +  using minus_of_num_less_of_num_iff [of m n] by simp
   3.487 +
   3.488 +lemma minus_of_num_less_one_iff: "- of_num n < 1"
   3.489 +  using minus_of_num_less_of_num_iff [of n One] by (simp add: of_num_One)
   3.490 +
   3.491 +lemma minus_one_less_of_num_iff: "- 1 < of_num n"
   3.492 +  using minus_of_num_less_of_num_iff [of One n] by (simp add: of_num_One)
   3.493 +
   3.494 +lemma minus_one_less_one_iff: "- 1 < 1"
   3.495 +  using minus_of_num_less_of_num_iff [of One One] by (simp add: of_num_One)
   3.496 +
   3.497 +lemma minus_of_num_le_of_num_iff: "- of_num m \<le> of_num n"
   3.498 +  by (simp add: less_imp_le minus_of_num_less_of_num_iff)
   3.499 +
   3.500 +lemma minus_of_num_le_one_iff: "- of_num n \<le> 1"
   3.501 +  by (simp add: less_imp_le minus_of_num_less_one_iff)
   3.502 +
   3.503 +lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n"
   3.504 +  by (simp add: less_imp_le minus_one_less_of_num_iff)
   3.505 +
   3.506 +lemma minus_one_le_one_iff: "- 1 \<le> 1"
   3.507 +  by (simp add: less_imp_le minus_one_less_one_iff)
   3.508 +
   3.509 +lemma of_num_le_minus_of_num_iff: "\<not> of_num m \<le> - of_num n"
   3.510 +  by (simp add: not_le minus_of_num_less_of_num_iff)
   3.511 +
   3.512 +lemma one_le_minus_of_num_iff: "\<not> 1 \<le> - of_num n"
   3.513 +  by (simp add: not_le minus_of_num_less_one_iff)
   3.514 +
   3.515 +lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1"
   3.516 +  by (simp add: not_le minus_one_less_of_num_iff)
   3.517 +
   3.518 +lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1"
   3.519 +  by (simp add: not_le minus_one_less_one_iff)
   3.520 +
   3.521 +lemma of_num_less_minus_of_num_iff: "\<not> of_num m < - of_num n"
   3.522 +  by (simp add: not_less minus_of_num_le_of_num_iff)
   3.523 +
   3.524 +lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n"
   3.525 +  by (simp add: not_less minus_of_num_le_one_iff)
   3.526 +
   3.527 +lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1"
   3.528 +  by (simp add: not_less minus_one_le_of_num_iff)
   3.529 +
   3.530 +lemma one_less_minus_one_iff: "\<not> 1 < - 1"
   3.531 +  by (simp add: not_less minus_one_le_one_iff)
   3.532 +
   3.533 +lemmas le_signed_numeral_special [numeral] =
   3.534 +  minus_of_num_le_of_num_iff
   3.535 +  minus_of_num_le_one_iff
   3.536 +  minus_one_le_of_num_iff
   3.537 +  minus_one_le_one_iff
   3.538 +  of_num_le_minus_of_num_iff
   3.539 +  one_le_minus_of_num_iff
   3.540 +  of_num_le_minus_one_iff
   3.541 +  one_le_minus_one_iff
   3.542 +
   3.543 +lemmas less_signed_numeral_special [numeral] =
   3.544 +  minus_of_num_less_of_num_iff
   3.545 +  minus_of_num_not_equal_of_num
   3.546 +  minus_of_num_less_one_iff
   3.547 +  minus_one_less_of_num_iff
   3.548 +  minus_one_less_one_iff
   3.549 +  of_num_less_minus_of_num_iff
   3.550 +  one_less_minus_of_num_iff
   3.551 +  of_num_less_minus_one_iff
   3.552 +  one_less_minus_one_iff
   3.553 +
   3.554 +end
   3.555 +
   3.556 +subsubsection {* Structures with subtraction: class @{text semiring_1_minus} *}
   3.557 +
   3.558 +class semiring_minus = semiring + minus + zero +
   3.559 +  assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a"
   3.560 +  assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a"
   3.561 +begin
   3.562 +
   3.563 +lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b"
   3.564 +  by (simp add: add_ac minus_inverts_plus1 [of b a])
   3.565 +
   3.566 +lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b"
   3.567 +  by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a])
   3.568 +
   3.569 +end
   3.570 +
   3.571 +class semiring_1_minus = semiring_1 + semiring_minus
   3.572 +begin
   3.573 +
   3.574 +lemma Dig_of_num_pos:
   3.575 +  assumes "k + n = m"
   3.576 +  shows "of_num m - of_num n = of_num k"
   3.577 +  using assms by (simp add: of_num_plus minus_inverts_plus1)
   3.578 +
   3.579 +lemma Dig_of_num_zero:
   3.580 +  shows "of_num n - of_num n = 0"
   3.581 +  by (rule minus_inverts_plus1) simp
   3.582 +
   3.583 +lemma Dig_of_num_neg:
   3.584 +  assumes "k + m = n"
   3.585 +  shows "of_num m - of_num n = 0 - of_num k"
   3.586 +  by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms)
   3.587 +
   3.588 +lemmas Dig_plus_eval =
   3.589 +  of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject
   3.590 +
   3.591 +simproc_setup numeral_minus ("of_num m - of_num n") = {*
   3.592 +  let
   3.593 +    (*TODO proper implicit use of morphism via pattern antiquotations*)
   3.594 +    fun cdest_of_num ct = (List.last o snd o Drule.strip_comb) ct;
   3.595 +    fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n);
   3.596 +    fun attach_num ct = (dest_num (Thm.term_of ct), ct);
   3.597 +    fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t;
   3.598 +    val simplify = Raw_Simplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval});
   3.599 +    fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq}
   3.600 +      OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"},
   3.601 +        [Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]];
   3.602 +  in fn phi => fn _ => fn ct => case try cdifference ct
   3.603 +   of NONE => (NONE)
   3.604 +    | SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0
   3.605 +        then Raw_Simplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct
   3.606 +        else mk_meta_eq (let
   3.607 +          val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j));
   3.608 +        in
   3.609 +          (if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck]
   3.610 +          else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl])
   3.611 +        end) end)
   3.612 +  end
   3.613 +*}
   3.614 +
   3.615 +lemma Dig_of_num_minus_zero [numeral]:
   3.616 +  "of_num n - 0 = of_num n"
   3.617 +  by (simp add: minus_inverts_plus1)
   3.618 +
   3.619 +lemma Dig_one_minus_zero [numeral]:
   3.620 +  "1 - 0 = 1"
   3.621 +  by (simp add: minus_inverts_plus1)
   3.622 +
   3.623 +lemma Dig_one_minus_one [numeral]:
   3.624 +  "1 - 1 = 0"
   3.625 +  by (simp add: minus_inverts_plus1)
   3.626 +
   3.627 +lemma Dig_of_num_minus_one [numeral]:
   3.628 +  "of_num (Dig0 n) - 1 = of_num (DigM n)"
   3.629 +  "of_num (Dig1 n) - 1 = of_num (Dig0 n)"
   3.630 +  by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
   3.631 +
   3.632 +lemma Dig_one_minus_of_num [numeral]:
   3.633 +  "1 - of_num (Dig0 n) = 0 - of_num (DigM n)"
   3.634 +  "1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)"
   3.635 +  by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one)
   3.636 +
   3.637 +end
   3.638 +
   3.639 +
   3.640 +subsubsection {* Structures with negation: class @{text ring_1} *}
   3.641 +
   3.642 +context ring_1
   3.643 +begin
   3.644 +
   3.645 +subclass semiring_1_minus proof
   3.646 +qed (simp_all add: algebra_simps)
   3.647 +
   3.648 +lemma Dig_zero_minus_of_num [numeral]:
   3.649 +  "0 - of_num n = - of_num n"
   3.650 +  by simp
   3.651 +
   3.652 +lemma Dig_zero_minus_one [numeral]:
   3.653 +  "0 - 1 = - 1"
   3.654 +  by simp
   3.655 +
   3.656 +lemma Dig_uminus_uminus [numeral]:
   3.657 +  "- (- of_num n) = of_num n"
   3.658 +  by simp
   3.659 +
   3.660 +lemma Dig_plus_uminus [numeral]:
   3.661 +  "of_num m + - of_num n = of_num m - of_num n"
   3.662 +  "- of_num m + of_num n = of_num n - of_num m"
   3.663 +  "- of_num m + - of_num n = - (of_num m + of_num n)"
   3.664 +  "of_num m - - of_num n = of_num m + of_num n"
   3.665 +  "- of_num m - of_num n = - (of_num m + of_num n)"
   3.666 +  "- of_num m - - of_num n = of_num n - of_num m"
   3.667 +  by (simp_all add: diff_minus add_commute)
   3.668 +
   3.669 +lemma Dig_times_uminus [numeral]:
   3.670 +  "- of_num n * of_num m = - (of_num n * of_num m)"
   3.671 +  "of_num n * - of_num m = - (of_num n * of_num m)"
   3.672 +  "- of_num n * - of_num m = of_num n * of_num m"
   3.673 +  by simp_all
   3.674 +
   3.675 +lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n"
   3.676 +by (induct n)
   3.677 +  (simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all)
   3.678 +
   3.679 +declare of_int_1 [numeral]
   3.680 +
   3.681 +end
   3.682 +
   3.683 +
   3.684 +subsubsection {* Structures with exponentiation *}
   3.685 +
   3.686 +lemma of_num_square: "of_num (square x) = of_num x * of_num x"
   3.687 +by (induct x)
   3.688 +   (simp_all add: of_num.simps of_num_add algebra_simps)
   3.689 +
   3.690 +lemma of_num_pow: "of_num (pow x y) = of_num x ^ of_num y"
   3.691 +by (induct y)
   3.692 +   (simp_all add: of_num.simps of_num_square of_num_mult power_add)
   3.693 +
   3.694 +lemma power_of_num [numeral]: "of_num x ^ of_num y = of_num (pow x y)"
   3.695 +  unfolding of_num_pow ..
   3.696 +
   3.697 +lemma power_zero_of_num [numeral]:
   3.698 +  "0 ^ of_num n = (0::'a::semiring_1)"
   3.699 +  using of_num_pos [where n=n and ?'a=nat]
   3.700 +  by (simp add: power_0_left)
   3.701 +
   3.702 +lemma power_minus_Dig0 [numeral]:
   3.703 +  fixes x :: "'a::ring_1"
   3.704 +  shows "(- x) ^ of_num (Dig0 n) = x ^ of_num (Dig0 n)"
   3.705 +  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
   3.706 +
   3.707 +lemma power_minus_Dig1 [numeral]:
   3.708 +  fixes x :: "'a::ring_1"
   3.709 +  shows "(- x) ^ of_num (Dig1 n) = - (x ^ of_num (Dig1 n))"
   3.710 +  by (induct n rule: num_induct) (simp_all add: of_num.simps of_num_inc)
   3.711 +
   3.712 +declare power_one [numeral]
   3.713 +
   3.714 +
   3.715 +subsubsection {* Greetings to @{typ nat}. *}
   3.716 +
   3.717 +instance nat :: semiring_1_minus proof
   3.718 +qed simp_all
   3.719 +
   3.720 +lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)"
   3.721 +  unfolding of_num_plus_one [symmetric] by simp
   3.722 +
   3.723 +lemma nat_number:
   3.724 +  "1 = Suc 0"
   3.725 +  "of_num One = Suc 0"
   3.726 +  "of_num (Dig0 n) = Suc (of_num (DigM n))"
   3.727 +  "of_num (Dig1 n) = Suc (of_num (Dig0 n))"
   3.728 +  by (simp_all add: of_num.simps DigM_plus_one Suc_of_num)
   3.729 +
   3.730 +declare diff_0_eq_0 [numeral]
   3.731 +
   3.732 +
   3.733 +subsection {* Proof tools setup *}
   3.734 +
   3.735 +subsubsection {* Numeral equations as default simplification rules *}
   3.736 +
   3.737 +declare (in semiring_numeral) of_num_One [simp]
   3.738 +declare (in semiring_numeral) of_num_plus_one [simp]
   3.739 +declare (in semiring_numeral) of_num_one_plus [simp]
   3.740 +declare (in semiring_numeral) of_num_plus [simp]
   3.741 +declare (in semiring_numeral) of_num_times [simp]
   3.742 +
   3.743 +declare (in semiring_1) of_nat_of_num [simp]
   3.744 +
   3.745 +declare (in semiring_char_0) of_num_eq_iff [simp]
   3.746 +declare (in semiring_char_0) of_num_eq_one_iff [simp]
   3.747 +declare (in semiring_char_0) one_eq_of_num_iff [simp]
   3.748 +
   3.749 +declare (in linordered_semidom) of_num_pos [simp]
   3.750 +declare (in linordered_semidom) of_num_not_zero [simp]
   3.751 +declare (in linordered_semidom) of_num_less_eq_iff [simp]
   3.752 +declare (in linordered_semidom) of_num_less_eq_one_iff [simp]
   3.753 +declare (in linordered_semidom) one_less_eq_of_num_iff [simp]
   3.754 +declare (in linordered_semidom) of_num_less_iff [simp]
   3.755 +declare (in linordered_semidom) of_num_less_one_iff [simp]
   3.756 +declare (in linordered_semidom) one_less_of_num_iff [simp]
   3.757 +declare (in linordered_semidom) of_num_nonneg [simp]
   3.758 +declare (in linordered_semidom) of_num_less_zero_iff [simp]
   3.759 +declare (in linordered_semidom) of_num_le_zero_iff [simp]
   3.760 +
   3.761 +declare (in linordered_idom) le_signed_numeral_special [simp]
   3.762 +declare (in linordered_idom) less_signed_numeral_special [simp]
   3.763 +
   3.764 +declare (in semiring_1_minus) Dig_of_num_minus_one [simp]
   3.765 +declare (in semiring_1_minus) Dig_one_minus_of_num [simp]
   3.766 +
   3.767 +declare (in ring_1) Dig_plus_uminus [simp]
   3.768 +declare (in ring_1) of_int_of_num [simp]
   3.769 +
   3.770 +declare power_of_num [simp]
   3.771 +declare power_zero_of_num [simp]
   3.772 +declare power_minus_Dig0 [simp]
   3.773 +declare power_minus_Dig1 [simp]
   3.774 +
   3.775 +declare Suc_of_num [simp]
   3.776 +
   3.777 +
   3.778 +subsubsection {* Reorientation of equalities *}
   3.779 +
   3.780 +setup {*
   3.781 +  Reorient_Proc.add
   3.782 +    (fn Const(@{const_name of_num}, _) $ _ => true
   3.783 +      | Const(@{const_name uminus}, _) $
   3.784 +          (Const(@{const_name of_num}, _) $ _) => true
   3.785 +      | _ => false)
   3.786 +*}
   3.787 +
   3.788 +simproc_setup reorient_num ("of_num n = x" | "- of_num m = y") = Reorient_Proc.proc
   3.789 +
   3.790 +
   3.791 +subsubsection {* Constant folding for multiplication in semirings *}
   3.792 +
   3.793 +context semiring_numeral
   3.794 +begin
   3.795 +
   3.796 +lemma mult_of_num_commute: "x * of_num n = of_num n * x"
   3.797 +by (induct n)
   3.798 +  (simp_all only: of_num.simps left_distrib right_distrib mult_1_left mult_1_right)
   3.799 +
   3.800 +definition
   3.801 +  "commutes_with a b \<longleftrightarrow> a * b = b * a"
   3.802 +
   3.803 +lemma commutes_with_commute: "commutes_with a b \<Longrightarrow> a * b = b * a"
   3.804 +unfolding commutes_with_def .
   3.805 +
   3.806 +lemma commutes_with_left_commute: "commutes_with a b \<Longrightarrow> a * (b * c) = b * (a * c)"
   3.807 +unfolding commutes_with_def by (simp only: mult_assoc [symmetric])
   3.808 +
   3.809 +lemma commutes_with_numeral: "commutes_with x (of_num n)" "commutes_with (of_num n) x"
   3.810 +unfolding commutes_with_def by (simp_all add: mult_of_num_commute)
   3.811 +
   3.812 +lemmas mult_ac_numeral =
   3.813 +  mult_assoc
   3.814 +  commutes_with_commute
   3.815 +  commutes_with_left_commute
   3.816 +  commutes_with_numeral
   3.817 +
   3.818 +end
   3.819 +
   3.820 +ML {*
   3.821 +structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
   3.822 +struct
   3.823 +  val assoc_ss = HOL_ss addsimps @{thms mult_ac_numeral}
   3.824 +  val eq_reflection = eq_reflection
   3.825 +  fun is_numeral (Const(@{const_name of_num}, _) $ _) = true
   3.826 +    | is_numeral _ = false;
   3.827 +end;
   3.828 +
   3.829 +structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
   3.830 +*}
   3.831 +
   3.832 +simproc_setup semiring_assoc_fold' ("(a::'a::semiring_numeral) * b") =
   3.833 +  {* fn phi => fn ss => fn ct =>
   3.834 +    Semiring_Times_Assoc.proc ss (Thm.term_of ct) *}
   3.835 +
   3.836 +
   3.837 +subsection {* Code generator setup for @{typ int} *}
   3.838 +
   3.839 +text {* Reversing standard setup *}
   3.840 +
   3.841 +lemma [code_unfold del]: "(0::int) \<equiv> Numeral0" by simp
   3.842 +lemma [code_unfold del]: "(1::int) \<equiv> Numeral1" by simp
   3.843 +declare zero_is_num_zero [code_unfold del]
   3.844 +declare one_is_num_one [code_unfold del]
   3.845 +  
   3.846 +lemma [code, code del]:
   3.847 +  "(1 :: int) = 1"
   3.848 +  "(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +"
   3.849 +  "(uminus :: int \<Rightarrow> int) = uminus"
   3.850 +  "(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -"
   3.851 +  "(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *"
   3.852 +  "(HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool) = HOL.equal"
   3.853 +  "(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>"
   3.854 +  "(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <"
   3.855 +  by rule+
   3.856 +
   3.857 +text {* Constructors *}
   3.858 +
   3.859 +definition Pls :: "num \<Rightarrow> int" where
   3.860 +  [simp, code_post]: "Pls n = of_num n"
   3.861 +
   3.862 +definition Mns :: "num \<Rightarrow> int" where
   3.863 +  [simp, code_post]: "Mns n = - of_num n"
   3.864 +
   3.865 +code_datatype "0::int" Pls Mns
   3.866 +
   3.867 +lemmas [code_unfold] = Pls_def [symmetric] Mns_def [symmetric]
   3.868 +
   3.869 +text {* Auxiliary operations *}
   3.870 +
   3.871 +definition dup :: "int \<Rightarrow> int" where
   3.872 +  [simp]: "dup k = k + k"
   3.873 +
   3.874 +lemma Dig_dup [code]:
   3.875 +  "dup 0 = 0"
   3.876 +  "dup (Pls n) = Pls (Dig0 n)"
   3.877 +  "dup (Mns n) = Mns (Dig0 n)"
   3.878 +  by (simp_all add: of_num.simps)
   3.879 +
   3.880 +definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where
   3.881 +  [simp]: "sub m n = (of_num m - of_num n)"
   3.882 +
   3.883 +lemma Dig_sub [code]:
   3.884 +  "sub One One = 0"
   3.885 +  "sub (Dig0 m) One = of_num (DigM m)"
   3.886 +  "sub (Dig1 m) One = of_num (Dig0 m)"
   3.887 +  "sub One (Dig0 n) = - of_num (DigM n)"
   3.888 +  "sub One (Dig1 n) = - of_num (Dig0 n)"
   3.889 +  "sub (Dig0 m) (Dig0 n) = dup (sub m n)"
   3.890 +  "sub (Dig1 m) (Dig1 n) = dup (sub m n)"
   3.891 +  "sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1"
   3.892 +  "sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1"
   3.893 +  by (simp_all add: algebra_simps num_eq_iff nat_of_num_add)
   3.894 +
   3.895 +text {* Implementations *}
   3.896 +
   3.897 +lemma one_int_code [code]:
   3.898 +  "1 = Pls One"
   3.899 +  by simp
   3.900 +
   3.901 +lemma plus_int_code [code]:
   3.902 +  "k + 0 = (k::int)"
   3.903 +  "0 + l = (l::int)"
   3.904 +  "Pls m + Pls n = Pls (m + n)"
   3.905 +  "Pls m + Mns n = sub m n"
   3.906 +  "Mns m + Pls n = sub n m"
   3.907 +  "Mns m + Mns n = Mns (m + n)"
   3.908 +  by simp_all
   3.909 +
   3.910 +lemma uminus_int_code [code]:
   3.911 +  "uminus 0 = (0::int)"
   3.912 +  "uminus (Pls m) = Mns m"
   3.913 +  "uminus (Mns m) = Pls m"
   3.914 +  by simp_all
   3.915 +
   3.916 +lemma minus_int_code [code]:
   3.917 +  "k - 0 = (k::int)"
   3.918 +  "0 - l = uminus (l::int)"
   3.919 +  "Pls m - Pls n = sub m n"
   3.920 +  "Pls m - Mns n = Pls (m + n)"
   3.921 +  "Mns m - Pls n = Mns (m + n)"
   3.922 +  "Mns m - Mns n = sub n m"
   3.923 +  by simp_all
   3.924 +
   3.925 +lemma times_int_code [code]:
   3.926 +  "k * 0 = (0::int)"
   3.927 +  "0 * l = (0::int)"
   3.928 +  "Pls m * Pls n = Pls (m * n)"
   3.929 +  "Pls m * Mns n = Mns (m * n)"
   3.930 +  "Mns m * Pls n = Mns (m * n)"
   3.931 +  "Mns m * Mns n = Pls (m * n)"
   3.932 +  by simp_all
   3.933 +
   3.934 +lemma eq_int_code [code]:
   3.935 +  "HOL.equal 0 (0::int) \<longleftrightarrow> True"
   3.936 +  "HOL.equal 0 (Pls l) \<longleftrightarrow> False"
   3.937 +  "HOL.equal 0 (Mns l) \<longleftrightarrow> False"
   3.938 +  "HOL.equal (Pls k) 0 \<longleftrightarrow> False"
   3.939 +  "HOL.equal (Pls k) (Pls l) \<longleftrightarrow> HOL.equal k l"
   3.940 +  "HOL.equal (Pls k) (Mns l) \<longleftrightarrow> False"
   3.941 +  "HOL.equal (Mns k) 0 \<longleftrightarrow> False"
   3.942 +  "HOL.equal (Mns k) (Pls l) \<longleftrightarrow> False"
   3.943 +  "HOL.equal (Mns k) (Mns l) \<longleftrightarrow> HOL.equal k l"
   3.944 +  by (auto simp add: equal dest: sym)
   3.945 +
   3.946 +lemma [code nbe]:
   3.947 +  "HOL.equal (k::int) k \<longleftrightarrow> True"
   3.948 +  by (fact equal_refl)
   3.949 +
   3.950 +lemma less_eq_int_code [code]:
   3.951 +  "0 \<le> (0::int) \<longleftrightarrow> True"
   3.952 +  "0 \<le> Pls l \<longleftrightarrow> True"
   3.953 +  "0 \<le> Mns l \<longleftrightarrow> False"
   3.954 +  "Pls k \<le> 0 \<longleftrightarrow> False"
   3.955 +  "Pls k \<le> Pls l \<longleftrightarrow> k \<le> l"
   3.956 +  "Pls k \<le> Mns l \<longleftrightarrow> False"
   3.957 +  "Mns k \<le> 0 \<longleftrightarrow> True"
   3.958 +  "Mns k \<le> Pls l \<longleftrightarrow> True"
   3.959 +  "Mns k \<le> Mns l \<longleftrightarrow> l \<le> k"
   3.960 +  by simp_all
   3.961 +
   3.962 +lemma less_int_code [code]:
   3.963 +  "0 < (0::int) \<longleftrightarrow> False"
   3.964 +  "0 < Pls l \<longleftrightarrow> True"
   3.965 +  "0 < Mns l \<longleftrightarrow> False"
   3.966 +  "Pls k < 0 \<longleftrightarrow> False"
   3.967 +  "Pls k < Pls l \<longleftrightarrow> k < l"
   3.968 +  "Pls k < Mns l \<longleftrightarrow> False"
   3.969 +  "Mns k < 0 \<longleftrightarrow> True"
   3.970 +  "Mns k < Pls l \<longleftrightarrow> True"
   3.971 +  "Mns k < Mns l \<longleftrightarrow> l < k"
   3.972 +  by simp_all
   3.973 +
   3.974 +hide_const (open) sub dup
   3.975 +
   3.976 +text {* Pretty literals *}
   3.977 +
   3.978 +ML {*
   3.979 +local open Code_Thingol in
   3.980 +
   3.981 +fun add_code print target =
   3.982 +  let
   3.983 +    fun dest_num one' dig0' dig1' thm =
   3.984 +      let
   3.985 +        fun dest_dig (IConst (c, _)) = if c = dig0' then 0
   3.986 +              else if c = dig1' then 1
   3.987 +              else Code_Printer.eqn_error thm "Illegal numeral expression: illegal dig"
   3.988 +          | dest_dig _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal digit";
   3.989 +        fun dest_num (IConst (c, _)) = if c = one' then 1
   3.990 +              else Code_Printer.eqn_error thm "Illegal numeral expression: illegal leading digit"
   3.991 +          | dest_num (t1 `$ t2) = 2 * dest_num t2 + dest_dig t1
   3.992 +          | dest_num _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal term";
   3.993 +      in dest_num end;
   3.994 +    fun pretty sgn literals [one', dig0', dig1'] _ thm _ _ [(t, _)] =
   3.995 +      (Code_Printer.str o print literals o sgn o dest_num one' dig0' dig1' thm) t
   3.996 +    fun add_syntax (c, sgn) = Code_Target.add_const_syntax target c
   3.997 +      (SOME (Code_Printer.complex_const_syntax
   3.998 +        (1, ([@{const_name One}, @{const_name Dig0}, @{const_name Dig1}],
   3.999 +          pretty sgn))));
  3.1000 +  in
  3.1001 +    add_syntax (@{const_name Pls}, I)
  3.1002 +    #> add_syntax (@{const_name Mns}, (fn k => ~ k))
  3.1003 +  end;
  3.1004 +
  3.1005 +end
  3.1006 +*}
  3.1007 +
  3.1008 +hide_const (open) One Dig0 Dig1
  3.1009 +
  3.1010 +
  3.1011 +subsection {* Toy examples *}
  3.1012 +
  3.1013 +definition "foo \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat)"
  3.1014 +definition "bar \<longleftrightarrow> #4 * #2 + #7 \<ge> (#8 :: int) - #3"
  3.1015 +
  3.1016 +code_thms foo bar
  3.1017 +export_code foo bar checking SML OCaml? Haskell? Scala?
  3.1018 +
  3.1019 +text {* This is an ad-hoc @{text Code_Integer} setup. *}
  3.1020 +
  3.1021 +setup {*
  3.1022 +  fold (add_code Code_Printer.literal_numeral)
  3.1023 +    [Code_ML.target_SML, Code_ML.target_OCaml, Code_Haskell.target, Code_Scala.target]
  3.1024 +*}
  3.1025 +
  3.1026 +code_type int
  3.1027 +  (SML "IntInf.int")
  3.1028 +  (OCaml "Big'_int.big'_int")
  3.1029 +  (Haskell "Integer")
  3.1030 +  (Scala "BigInt")
  3.1031 +  (Eval "int")
  3.1032 +
  3.1033 +code_const "0::int"
  3.1034 +  (SML "0/ :/ IntInf.int")
  3.1035 +  (OCaml "Big'_int.zero")
  3.1036 +  (Haskell "0")
  3.1037 +  (Scala "BigInt(0)")
  3.1038 +  (Eval "0/ :/ int")
  3.1039 +
  3.1040 +code_const Int.pred
  3.1041 +  (SML "IntInf.- ((_), 1)")
  3.1042 +  (OCaml "Big'_int.pred'_big'_int")
  3.1043 +  (Haskell "!(_/ -/ 1)")
  3.1044 +  (Scala "!(_ -/ 1)")
  3.1045 +  (Eval "!(_/ -/ 1)")
  3.1046 +
  3.1047 +code_const Int.succ
  3.1048 +  (SML "IntInf.+ ((_), 1)")
  3.1049 +  (OCaml "Big'_int.succ'_big'_int")
  3.1050 +  (Haskell "!(_/ +/ 1)")
  3.1051 +  (Scala "!(_ +/ 1)")
  3.1052 +  (Eval "!(_/ +/ 1)")
  3.1053 +
  3.1054 +code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
  3.1055 +  (SML "IntInf.+ ((_), (_))")
  3.1056 +  (OCaml "Big'_int.add'_big'_int")
  3.1057 +  (Haskell infixl 6 "+")
  3.1058 +  (Scala infixl 7 "+")
  3.1059 +  (Eval infixl 8 "+")
  3.1060 +
  3.1061 +code_const "uminus \<Colon> int \<Rightarrow> int"
  3.1062 +  (SML "IntInf.~")
  3.1063 +  (OCaml "Big'_int.minus'_big'_int")
  3.1064 +  (Haskell "negate")
  3.1065 +  (Scala "!(- _)")
  3.1066 +  (Eval "~/ _")
  3.1067 +
  3.1068 +code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
  3.1069 +  (SML "IntInf.- ((_), (_))")
  3.1070 +  (OCaml "Big'_int.sub'_big'_int")
  3.1071 +  (Haskell infixl 6 "-")
  3.1072 +  (Scala infixl 7 "-")
  3.1073 +  (Eval infixl 8 "-")
  3.1074 +
  3.1075 +code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
  3.1076 +  (SML "IntInf.* ((_), (_))")
  3.1077 +  (OCaml "Big'_int.mult'_big'_int")
  3.1078 +  (Haskell infixl 7 "*")
  3.1079 +  (Scala infixl 8 "*")
  3.1080 +  (Eval infixl 9 "*")
  3.1081 +
  3.1082 +code_const pdivmod
  3.1083 +  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
  3.1084 +  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
  3.1085 +  (Haskell "divMod/ (abs _)/ (abs _)")
  3.1086 +  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
  3.1087 +  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
  3.1088 +
  3.1089 +code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
  3.1090 +  (SML "!((_ : IntInf.int) = _)")
  3.1091 +  (OCaml "Big'_int.eq'_big'_int")
  3.1092 +  (Haskell infix 4 "==")
  3.1093 +  (Scala infixl 5 "==")
  3.1094 +  (Eval infixl 6 "=")
  3.1095 +
  3.1096 +code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
  3.1097 +  (SML "IntInf.<= ((_), (_))")
  3.1098 +  (OCaml "Big'_int.le'_big'_int")
  3.1099 +  (Haskell infix 4 "<=")
  3.1100 +  (Scala infixl 4 "<=")
  3.1101 +  (Eval infixl 6 "<=")
  3.1102 +
  3.1103 +code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
  3.1104 +  (SML "IntInf.< ((_), (_))")
  3.1105 +  (OCaml "Big'_int.lt'_big'_int")
  3.1106 +  (Haskell infix 4 "<")
  3.1107 +  (Scala infixl 4 "<")
  3.1108 +  (Eval infixl 6 "<")
  3.1109 +
  3.1110 +code_const Code_Numeral.int_of
  3.1111 +  (SML "IntInf.fromInt")
  3.1112 +  (OCaml "_")
  3.1113 +  (Haskell "toInteger")
  3.1114 +  (Scala "!_.as'_BigInt")
  3.1115 +  (Eval "_")
  3.1116 +
  3.1117 +export_code foo bar checking SML OCaml? Haskell? Scala?
  3.1118 +
  3.1119 +end
     4.1 --- a/src/HOL/ex/ROOT.ML	Tue Feb 21 08:15:42 2012 +0100
     4.2 +++ b/src/HOL/ex/ROOT.ML	Tue Feb 21 09:17:53 2012 +0100
     4.3 @@ -17,7 +17,7 @@
     4.4  use_thys [
     4.5    "Iff_Oracle",
     4.6    "Coercion_Examples",
     4.7 -  "Numeral",
     4.8 +  "Numeral_Representation",
     4.9    "Higher_Order_Logic",
    4.10    "Abstract_NAT",
    4.11    "Guess",