src/HOL/Library/Target_Numeral.thy
changeset 47108 2a1953f0d20d
child 47159 978c00c20a59
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Target_Numeral.thy	Sun Mar 25 20:15:39 2012 +0200
     1.3 @@ -0,0 +1,726 @@
     1.4 +theory Target_Numeral
     1.5 +imports Main Code_Nat
     1.6 +begin
     1.7 +
     1.8 +subsection {* Type of target language numerals *}
     1.9 +
    1.10 +typedef (open) int = "UNIV \<Colon> int set"
    1.11 +  morphisms int_of of_int ..
    1.12 +
    1.13 +hide_type (open) int
    1.14 +hide_const (open) of_int
    1.15 +
    1.16 +lemma int_eq_iff:
    1.17 +  "k = l \<longleftrightarrow> int_of k = int_of l"
    1.18 +  using int_of_inject [of k l] ..
    1.19 +
    1.20 +lemma int_eqI:
    1.21 +  "int_of k = int_of l \<Longrightarrow> k = l"
    1.22 +  using int_eq_iff [of k l] by simp
    1.23 +
    1.24 +lemma int_of_int [simp]:
    1.25 +  "int_of (Target_Numeral.of_int k) = k"
    1.26 +  using of_int_inverse [of k] by simp
    1.27 +
    1.28 +lemma of_int_of [simp]:
    1.29 +  "Target_Numeral.of_int (int_of k) = k"
    1.30 +  using int_of_inverse [of k] by simp
    1.31 +
    1.32 +hide_fact (open) int_eq_iff int_eqI
    1.33 +
    1.34 +instantiation Target_Numeral.int :: ring_1
    1.35 +begin
    1.36 +
    1.37 +definition
    1.38 +  "0 = Target_Numeral.of_int 0"
    1.39 +
    1.40 +lemma int_of_zero [simp]:
    1.41 +  "int_of 0 = 0"
    1.42 +  by (simp add: zero_int_def)
    1.43 +
    1.44 +definition
    1.45 +  "1 = Target_Numeral.of_int 1"
    1.46 +
    1.47 +lemma int_of_one [simp]:
    1.48 +  "int_of 1 = 1"
    1.49 +  by (simp add: one_int_def)
    1.50 +
    1.51 +definition
    1.52 +  "k + l = Target_Numeral.of_int (int_of k + int_of l)"
    1.53 +
    1.54 +lemma int_of_plus [simp]:
    1.55 +  "int_of (k + l) = int_of k + int_of l"
    1.56 +  by (simp add: plus_int_def)
    1.57 +
    1.58 +definition
    1.59 +  "- k = Target_Numeral.of_int (- int_of k)"
    1.60 +
    1.61 +lemma int_of_uminus [simp]:
    1.62 +  "int_of (- k) = - int_of k"
    1.63 +  by (simp add: uminus_int_def)
    1.64 +
    1.65 +definition
    1.66 +  "k - l = Target_Numeral.of_int (int_of k - int_of l)"
    1.67 +
    1.68 +lemma int_of_minus [simp]:
    1.69 +  "int_of (k - l) = int_of k - int_of l"
    1.70 +  by (simp add: minus_int_def)
    1.71 +
    1.72 +definition
    1.73 +  "k * l = Target_Numeral.of_int (int_of k * int_of l)"
    1.74 +
    1.75 +lemma int_of_times [simp]:
    1.76 +  "int_of (k * l) = int_of k * int_of l"
    1.77 +  by (simp add: times_int_def)
    1.78 +
    1.79 +instance proof
    1.80 +qed (auto simp add: Target_Numeral.int_eq_iff algebra_simps)
    1.81 +
    1.82 +end
    1.83 +
    1.84 +lemma int_of_of_nat [simp]:
    1.85 +  "int_of (of_nat n) = of_nat n"
    1.86 +  by (induct n) simp_all
    1.87 +
    1.88 +definition nat_of :: "Target_Numeral.int \<Rightarrow> nat" where
    1.89 +  "nat_of k = Int.nat (int_of k)"
    1.90 +
    1.91 +lemma nat_of_of_nat [simp]:
    1.92 +  "nat_of (of_nat n) = n"
    1.93 +  by (simp add: nat_of_def)
    1.94 +
    1.95 +lemma int_of_of_int [simp]:
    1.96 +  "int_of (of_int k) = k"
    1.97 +  by (induct k) (simp_all, simp only: neg_numeral_def numeral_One int_of_uminus int_of_one)
    1.98 +
    1.99 +lemma of_int_of_int [simp, code_abbrev]:
   1.100 +  "Target_Numeral.of_int = of_int"
   1.101 +  by rule (simp add: Target_Numeral.int_eq_iff)
   1.102 +
   1.103 +lemma int_of_numeral [simp]:
   1.104 +  "int_of (numeral k) = numeral k"
   1.105 +  using int_of_of_int [of "numeral k"] by simp
   1.106 +
   1.107 +lemma int_of_neg_numeral [simp]:
   1.108 +  "int_of (neg_numeral k) = neg_numeral k"
   1.109 +  by (simp only: neg_numeral_def int_of_uminus) simp
   1.110 +
   1.111 +lemma int_of_sub [simp]:
   1.112 +  "int_of (Num.sub k l) = Num.sub k l"
   1.113 +  by (simp only: Num.sub_def int_of_minus int_of_numeral)
   1.114 +
   1.115 +instantiation Target_Numeral.int :: "{ring_div, equal, linordered_idom}"
   1.116 +begin
   1.117 +
   1.118 +definition
   1.119 +  "k div l = of_int (int_of k div int_of l)"
   1.120 +
   1.121 +lemma int_of_div [simp]:
   1.122 +  "int_of (k div l) = int_of k div int_of l"
   1.123 +  by (simp add: div_int_def)
   1.124 +
   1.125 +definition
   1.126 +  "k mod l = of_int (int_of k mod int_of l)"
   1.127 +
   1.128 +lemma int_of_mod [simp]:
   1.129 +  "int_of (k mod l) = int_of k mod int_of l"
   1.130 +  by (simp add: mod_int_def)
   1.131 +
   1.132 +definition
   1.133 +  "\<bar>k\<bar> = of_int \<bar>int_of k\<bar>"
   1.134 +
   1.135 +lemma int_of_abs [simp]:
   1.136 +  "int_of \<bar>k\<bar> = \<bar>int_of k\<bar>"
   1.137 +  by (simp add: abs_int_def)
   1.138 +
   1.139 +definition
   1.140 +  "sgn k = of_int (sgn (int_of k))"
   1.141 +
   1.142 +lemma int_of_sgn [simp]:
   1.143 +  "int_of (sgn k) = sgn (int_of k)"
   1.144 +  by (simp add: sgn_int_def)
   1.145 +
   1.146 +definition
   1.147 +  "k \<le> l \<longleftrightarrow> int_of k \<le> int_of l"
   1.148 +
   1.149 +definition
   1.150 +  "k < l \<longleftrightarrow> int_of k < int_of l"
   1.151 +
   1.152 +definition
   1.153 +  "HOL.equal k l \<longleftrightarrow> HOL.equal (int_of k) (int_of l)"
   1.154 +
   1.155 +instance proof
   1.156 +qed (auto simp add: Target_Numeral.int_eq_iff algebra_simps
   1.157 +  less_eq_int_def less_int_def equal_int_def equal)
   1.158 +
   1.159 +end
   1.160 +
   1.161 +lemma int_of_min [simp]:
   1.162 +  "int_of (min k l) = min (int_of k) (int_of l)"
   1.163 +  by (simp add: min_def less_eq_int_def)
   1.164 +
   1.165 +lemma int_of_max [simp]:
   1.166 +  "int_of (max k l) = max (int_of k) (int_of l)"
   1.167 +  by (simp add: max_def less_eq_int_def)
   1.168 +
   1.169 +
   1.170 +subsection {* Code theorems for target language numerals *}
   1.171 +
   1.172 +text {* Constructors *}
   1.173 +
   1.174 +definition Pos :: "num \<Rightarrow> Target_Numeral.int" where
   1.175 +  [simp, code_abbrev]: "Pos = numeral"
   1.176 +
   1.177 +definition Neg :: "num \<Rightarrow> Target_Numeral.int" where
   1.178 +  [simp, code_abbrev]: "Neg = neg_numeral"
   1.179 +
   1.180 +code_datatype "0::Target_Numeral.int" Pos Neg
   1.181 +
   1.182 +
   1.183 +text {* Auxiliary operations *}
   1.184 +
   1.185 +definition dup :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int" where
   1.186 +  [simp]: "dup k = k + k"
   1.187 +
   1.188 +lemma dup_code [code]:
   1.189 +  "dup 0 = 0"
   1.190 +  "dup (Pos n) = Pos (Num.Bit0 n)"
   1.191 +  "dup (Neg n) = Neg (Num.Bit0 n)"
   1.192 +  unfolding Pos_def Neg_def neg_numeral_def
   1.193 +  by (simp_all add: numeral_Bit0)
   1.194 +
   1.195 +definition sub :: "num \<Rightarrow> num \<Rightarrow> Target_Numeral.int" where
   1.196 +  [simp]: "sub m n = numeral m - numeral n"
   1.197 +
   1.198 +lemma sub_code [code]:
   1.199 +  "sub Num.One Num.One = 0"
   1.200 +  "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
   1.201 +  "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
   1.202 +  "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
   1.203 +  "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
   1.204 +  "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
   1.205 +  "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
   1.206 +  "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
   1.207 +  "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
   1.208 +  unfolding sub_def dup_def numeral.simps Pos_def Neg_def
   1.209 +    neg_numeral_def numeral_BitM
   1.210 +  by (simp_all only: algebra_simps add.comm_neutral)
   1.211 +
   1.212 +
   1.213 +text {* Implementations *}
   1.214 +
   1.215 +lemma one_int_code [code, code_unfold]:
   1.216 +  "1 = Pos Num.One"
   1.217 +  by simp
   1.218 +
   1.219 +lemma plus_int_code [code]:
   1.220 +  "k + 0 = (k::Target_Numeral.int)"
   1.221 +  "0 + l = (l::Target_Numeral.int)"
   1.222 +  "Pos m + Pos n = Pos (m + n)"
   1.223 +  "Pos m + Neg n = sub m n"
   1.224 +  "Neg m + Pos n = sub n m"
   1.225 +  "Neg m + Neg n = Neg (m + n)"
   1.226 +  by simp_all
   1.227 +
   1.228 +lemma uminus_int_code [code]:
   1.229 +  "uminus 0 = (0::Target_Numeral.int)"
   1.230 +  "uminus (Pos m) = Neg m"
   1.231 +  "uminus (Neg m) = Pos m"
   1.232 +  by simp_all
   1.233 +
   1.234 +lemma minus_int_code [code]:
   1.235 +  "k - 0 = (k::Target_Numeral.int)"
   1.236 +  "0 - l = uminus (l::Target_Numeral.int)"
   1.237 +  "Pos m - Pos n = sub m n"
   1.238 +  "Pos m - Neg n = Pos (m + n)"
   1.239 +  "Neg m - Pos n = Neg (m + n)"
   1.240 +  "Neg m - Neg n = sub n m"
   1.241 +  by simp_all
   1.242 +
   1.243 +lemma times_int_code [code]:
   1.244 +  "k * 0 = (0::Target_Numeral.int)"
   1.245 +  "0 * l = (0::Target_Numeral.int)"
   1.246 +  "Pos m * Pos n = Pos (m * n)"
   1.247 +  "Pos m * Neg n = Neg (m * n)"
   1.248 +  "Neg m * Pos n = Neg (m * n)"
   1.249 +  "Neg m * Neg n = Pos (m * n)"
   1.250 +  by simp_all
   1.251 +
   1.252 +definition divmod :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int \<Rightarrow> Target_Numeral.int \<times> Target_Numeral.int" where
   1.253 +  "divmod k l = (k div l, k mod l)"
   1.254 +
   1.255 +lemma fst_divmod [simp]:
   1.256 +  "fst (divmod k l) = k div l"
   1.257 +  by (simp add: divmod_def)
   1.258 +
   1.259 +lemma snd_divmod [simp]:
   1.260 +  "snd (divmod k l) = k mod l"
   1.261 +  by (simp add: divmod_def)
   1.262 +
   1.263 +definition divmod_abs :: "Target_Numeral.int \<Rightarrow> Target_Numeral.int \<Rightarrow> Target_Numeral.int \<times> Target_Numeral.int" where
   1.264 +  "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
   1.265 +
   1.266 +lemma fst_divmod_abs [simp]:
   1.267 +  "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
   1.268 +  by (simp add: divmod_abs_def)
   1.269 +
   1.270 +lemma snd_divmod_abs [simp]:
   1.271 +  "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
   1.272 +  by (simp add: divmod_abs_def)
   1.273 +
   1.274 +lemma divmod_abs_terminate_code [code]:
   1.275 +  "divmod_abs (Neg k) (Neg l) = divmod_abs (Pos k) (Pos l)"
   1.276 +  "divmod_abs (Neg k) (Pos l) = divmod_abs (Pos k) (Pos l)"
   1.277 +  "divmod_abs (Pos k) (Neg l) = divmod_abs (Pos k) (Pos l)"
   1.278 +  "divmod_abs j 0 = (0, \<bar>j\<bar>)"
   1.279 +  "divmod_abs 0 j = (0, 0)"
   1.280 +  by (simp_all add: prod_eq_iff)
   1.281 +
   1.282 +lemma divmod_abs_rec_code [code]:
   1.283 +  "divmod_abs (Pos k) (Pos l) =
   1.284 +    (let j = sub k l in
   1.285 +       if j < 0 then (0, Pos k)
   1.286 +       else let (q, r) = divmod_abs j (Pos l) in (q + 1, r))"
   1.287 +  by (auto simp add: prod_eq_iff Target_Numeral.int_eq_iff Let_def prod_case_beta
   1.288 +    sub_non_negative sub_negative div_pos_pos_trivial mod_pos_pos_trivial div_pos_geq mod_pos_geq)
   1.289 +
   1.290 +lemma divmod_code [code]: "divmod k l =
   1.291 +  (if k = 0 then (0, 0) else if l = 0 then (0, k) else
   1.292 +  (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
   1.293 +    then divmod_abs k l
   1.294 +    else (let (r, s) = divmod_abs k l in
   1.295 +      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
   1.296 +proof -
   1.297 +  have aux1: "\<And>k l::int. sgn k = sgn l \<longleftrightarrow> k = 0 \<and> l = 0 \<or> 0 < l \<and> 0 < k \<or> l < 0 \<and> k < 0"
   1.298 +    by (auto simp add: sgn_if)
   1.299 +  have aux2: "\<And>q::int. - int_of k = int_of l * q \<longleftrightarrow> int_of k = int_of l * - q" by auto
   1.300 +  show ?thesis
   1.301 +    by (simp add: prod_eq_iff Target_Numeral.int_eq_iff prod_case_beta aux1)
   1.302 +      (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if zdiv_zminus2 zmod_zminus2 aux2)
   1.303 +qed
   1.304 +
   1.305 +lemma div_int_code [code]:
   1.306 +  "k div l = fst (divmod k l)"
   1.307 +  by simp
   1.308 +
   1.309 +lemma div_mod_code [code]:
   1.310 +  "k mod l = snd (divmod k l)"
   1.311 +  by simp
   1.312 +
   1.313 +lemma equal_int_code [code]:
   1.314 +  "HOL.equal 0 (0::Target_Numeral.int) \<longleftrightarrow> True"
   1.315 +  "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
   1.316 +  "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
   1.317 +  "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
   1.318 +  "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
   1.319 +  "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
   1.320 +  "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
   1.321 +  "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
   1.322 +  "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
   1.323 +  by (simp_all add: equal Target_Numeral.int_eq_iff)
   1.324 +
   1.325 +lemma equal_int_refl [code nbe]:
   1.326 +  "HOL.equal (k::Target_Numeral.int) k \<longleftrightarrow> True"
   1.327 +  by (fact equal_refl)
   1.328 +
   1.329 +lemma less_eq_int_code [code]:
   1.330 +  "0 \<le> (0::Target_Numeral.int) \<longleftrightarrow> True"
   1.331 +  "0 \<le> Pos l \<longleftrightarrow> True"
   1.332 +  "0 \<le> Neg l \<longleftrightarrow> False"
   1.333 +  "Pos k \<le> 0 \<longleftrightarrow> False"
   1.334 +  "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
   1.335 +  "Pos k \<le> Neg l \<longleftrightarrow> False"
   1.336 +  "Neg k \<le> 0 \<longleftrightarrow> True"
   1.337 +  "Neg k \<le> Pos l \<longleftrightarrow> True"
   1.338 +  "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
   1.339 +  by (simp_all add: less_eq_int_def)
   1.340 +
   1.341 +lemma less_int_code [code]:
   1.342 +  "0 < (0::Target_Numeral.int) \<longleftrightarrow> False"
   1.343 +  "0 < Pos l \<longleftrightarrow> True"
   1.344 +  "0 < Neg l \<longleftrightarrow> False"
   1.345 +  "Pos k < 0 \<longleftrightarrow> False"
   1.346 +  "Pos k < Pos l \<longleftrightarrow> k < l"
   1.347 +  "Pos k < Neg l \<longleftrightarrow> False"
   1.348 +  "Neg k < 0 \<longleftrightarrow> True"
   1.349 +  "Neg k < Pos l \<longleftrightarrow> True"
   1.350 +  "Neg k < Neg l \<longleftrightarrow> l < k"
   1.351 +  by (simp_all add: less_int_def)
   1.352 +
   1.353 +lemma nat_of_code [code]:
   1.354 +  "nat_of (Neg k) = 0"
   1.355 +  "nat_of 0 = 0"
   1.356 +  "nat_of (Pos k) = nat_of_num k"
   1.357 +  by (simp_all add: nat_of_def nat_of_num_numeral)
   1.358 +
   1.359 +lemma int_of_code [code]:
   1.360 +  "int_of (Neg k) = neg_numeral k"
   1.361 +  "int_of 0 = 0"
   1.362 +  "int_of (Pos k) = numeral k"
   1.363 +  by simp_all
   1.364 +
   1.365 +lemma of_int_code [code]:
   1.366 +  "Target_Numeral.of_int (Int.Neg k) = neg_numeral k"
   1.367 +  "Target_Numeral.of_int 0 = 0"
   1.368 +  "Target_Numeral.of_int (Int.Pos k) = numeral k"
   1.369 +  by simp_all
   1.370 +
   1.371 +definition num_of_int :: "Target_Numeral.int \<Rightarrow> num" where
   1.372 +  "num_of_int = num_of_nat \<circ> nat_of"
   1.373 +
   1.374 +lemma num_of_int_code [code]:
   1.375 +  "num_of_int k = (if k \<le> 1 then Num.One
   1.376 +     else let
   1.377 +       (l, j) = divmod k 2;
   1.378 +       l' = num_of_int l + num_of_int l
   1.379 +     in if j = 0 then l' else l' + Num.One)"
   1.380 +proof -
   1.381 +  {
   1.382 +    assume "int_of k mod 2 = 1"
   1.383 +    then have "nat (int_of k mod 2) = nat 1" by simp
   1.384 +    moreover assume *: "1 < int_of k"
   1.385 +    ultimately have **: "nat (int_of k) mod 2 = 1" by (simp add: nat_mod_distrib)
   1.386 +    have "num_of_nat (nat (int_of k)) =
   1.387 +      num_of_nat (2 * (nat (int_of k) div 2) + nat (int_of k) mod 2)"
   1.388 +      by simp
   1.389 +    then have "num_of_nat (nat (int_of k)) =
   1.390 +      num_of_nat (nat (int_of k) div 2 + nat (int_of k) div 2 + nat (int_of k) mod 2)"
   1.391 +      by (simp add: nat_mult_2)
   1.392 +    with ** have "num_of_nat (nat (int_of k)) =
   1.393 +      num_of_nat (nat (int_of k) div 2 + nat (int_of k) div 2 + 1)"
   1.394 +      by simp
   1.395 +  }
   1.396 +  note aux = this
   1.397 +  show ?thesis
   1.398 +    by (auto simp add: num_of_int_def nat_of_def Let_def prod_case_beta
   1.399 +      not_le Target_Numeral.int_eq_iff less_eq_int_def
   1.400 +      nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
   1.401 +       nat_mult_2 aux add_One)
   1.402 +qed
   1.403 +
   1.404 +hide_const (open) int_of nat_of Pos Neg sub dup divmod_abs num_of_int
   1.405 +
   1.406 +
   1.407 +subsection {* Serializer setup for target language numerals *}
   1.408 +
   1.409 +code_type Target_Numeral.int
   1.410 +  (SML "IntInf.int")
   1.411 +  (OCaml "Big'_int.big'_int")
   1.412 +  (Haskell "Integer")
   1.413 +  (Scala "BigInt")
   1.414 +  (Eval "int")
   1.415 +
   1.416 +code_instance Target_Numeral.int :: equal
   1.417 +  (Haskell -)
   1.418 +
   1.419 +code_const "0::Target_Numeral.int"
   1.420 +  (SML "0")
   1.421 +  (OCaml "Big'_int.zero'_big'_int")
   1.422 +  (Haskell "0")
   1.423 +  (Scala "BigInt(0)")
   1.424 +
   1.425 +setup {*
   1.426 +  fold (Numeral.add_code @{const_name Target_Numeral.Pos}
   1.427 +    false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
   1.428 +*}
   1.429 +
   1.430 +setup {*
   1.431 +  fold (Numeral.add_code @{const_name Target_Numeral.Neg}
   1.432 +    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
   1.433 +*}
   1.434 +
   1.435 +code_const "plus :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> _"
   1.436 +  (SML "IntInf.+ ((_), (_))")
   1.437 +  (OCaml "Big'_int.add'_big'_int")
   1.438 +  (Haskell infixl 6 "+")
   1.439 +  (Scala infixl 7 "+")
   1.440 +  (Eval infixl 8 "+")
   1.441 +
   1.442 +code_const "uminus :: Target_Numeral.int \<Rightarrow> _"
   1.443 +  (SML "IntInf.~")
   1.444 +  (OCaml "Big'_int.minus'_big'_int")
   1.445 +  (Haskell "negate")
   1.446 +  (Scala "!(- _)")
   1.447 +  (Eval "~/ _")
   1.448 +
   1.449 +code_const "minus :: Target_Numeral.int \<Rightarrow> _"
   1.450 +  (SML "IntInf.- ((_), (_))")
   1.451 +  (OCaml "Big'_int.sub'_big'_int")
   1.452 +  (Haskell infixl 6 "-")
   1.453 +  (Scala infixl 7 "-")
   1.454 +  (Eval infixl 8 "-")
   1.455 +
   1.456 +code_const Target_Numeral.dup
   1.457 +  (SML "IntInf.*/ (2,/ (_))")
   1.458 +  (OCaml "Big'_int.mult'_big'_int/ 2")
   1.459 +  (Haskell "!(2 * _)")
   1.460 +  (Scala "!(2 * _)")
   1.461 +  (Eval "!(2 * _)")
   1.462 +
   1.463 +code_const Target_Numeral.sub
   1.464 +  (SML "!(raise/ Fail/ \"sub\")")
   1.465 +  (OCaml "failwith/ \"sub\"")
   1.466 +  (Haskell "error/ \"sub\"")
   1.467 +  (Scala "!error(\"sub\")")
   1.468 +
   1.469 +code_const "times :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> _"
   1.470 +  (SML "IntInf.* ((_), (_))")
   1.471 +  (OCaml "Big'_int.mult'_big'_int")
   1.472 +  (Haskell infixl 7 "*")
   1.473 +  (Scala infixl 8 "*")
   1.474 +  (Eval infixl 9 "*")
   1.475 +
   1.476 +code_const Target_Numeral.divmod_abs
   1.477 +  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
   1.478 +  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
   1.479 +  (Haskell "divMod/ (abs _)/ (abs _)")
   1.480 +  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
   1.481 +  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
   1.482 +
   1.483 +code_const "HOL.equal :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
   1.484 +  (SML "!((_ : IntInf.int) = _)")
   1.485 +  (OCaml "Big'_int.eq'_big'_int")
   1.486 +  (Haskell infix 4 "==")
   1.487 +  (Scala infixl 5 "==")
   1.488 +  (Eval infixl 6 "=")
   1.489 +
   1.490 +code_const "less_eq :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
   1.491 +  (SML "IntInf.<= ((_), (_))")
   1.492 +  (OCaml "Big'_int.le'_big'_int")
   1.493 +  (Haskell infix 4 "<=")
   1.494 +  (Scala infixl 4 "<=")
   1.495 +  (Eval infixl 6 "<=")
   1.496 +
   1.497 +code_const "less :: Target_Numeral.int \<Rightarrow> _ \<Rightarrow> bool"
   1.498 +  (SML "IntInf.< ((_), (_))")
   1.499 +  (OCaml "Big'_int.lt'_big'_int")
   1.500 +  (Haskell infix 4 "<")
   1.501 +  (Scala infixl 4 "<")
   1.502 +  (Eval infixl 6 "<")
   1.503 +
   1.504 +ML {*
   1.505 +structure Target_Numeral =
   1.506 +struct
   1.507 +
   1.508 +val T = @{typ "Target_Numeral.int"};
   1.509 +
   1.510 +end;
   1.511 +*}
   1.512 +
   1.513 +code_reserved Eval Target_Numeral
   1.514 +
   1.515 +code_const "Code_Evaluation.term_of \<Colon> Target_Numeral.int \<Rightarrow> term"
   1.516 +  (Eval "HOLogic.mk'_number/ Target'_Numeral.T")
   1.517 +
   1.518 +code_modulename SML
   1.519 +  Target_Numeral Arith
   1.520 +
   1.521 +code_modulename OCaml
   1.522 +  Target_Numeral Arith
   1.523 +
   1.524 +code_modulename Haskell
   1.525 +  Target_Numeral Arith
   1.526 +
   1.527 +
   1.528 +subsection {* Implementation for @{typ int} *}
   1.529 +
   1.530 +code_datatype Target_Numeral.int_of
   1.531 +
   1.532 +lemma [code, code del]:
   1.533 +  "Target_Numeral.of_int = Target_Numeral.of_int" ..
   1.534 +
   1.535 +lemma [code]:
   1.536 +  "Target_Numeral.of_int (Target_Numeral.int_of k) = k"
   1.537 +  by (simp add: Target_Numeral.int_eq_iff)
   1.538 +
   1.539 +declare Int.Pos_def [code]
   1.540 +
   1.541 +lemma [code_abbrev]:
   1.542 +  "Target_Numeral.int_of (Target_Numeral.Pos k) = Int.Pos k"
   1.543 +  by simp
   1.544 +
   1.545 +declare Int.Neg_def [code]
   1.546 +
   1.547 +lemma [code_abbrev]:
   1.548 +  "Target_Numeral.int_of (Target_Numeral.Neg k) = Int.Neg k"
   1.549 +  by simp
   1.550 +
   1.551 +lemma [code]:
   1.552 +  "0 = Target_Numeral.int_of 0"
   1.553 +  by simp
   1.554 +
   1.555 +lemma [code]:
   1.556 +  "1 = Target_Numeral.int_of 1"
   1.557 +  by simp
   1.558 +
   1.559 +lemma [code]:
   1.560 +  "k + l = Target_Numeral.int_of (of_int k + of_int l)"
   1.561 +  by simp
   1.562 +
   1.563 +lemma [code]:
   1.564 +  "- k = Target_Numeral.int_of (- of_int k)"
   1.565 +  by simp
   1.566 +
   1.567 +lemma [code]:
   1.568 +  "k - l = Target_Numeral.int_of (of_int k - of_int l)"
   1.569 +  by simp
   1.570 +
   1.571 +lemma [code]:
   1.572 +  "Int.dup k = Target_Numeral.int_of (Target_Numeral.dup (of_int k))"
   1.573 +  by simp
   1.574 +
   1.575 +lemma [code, code del]:
   1.576 +  "Int.sub = Int.sub" ..
   1.577 +
   1.578 +lemma [code]:
   1.579 +  "k * l = Target_Numeral.int_of (of_int k * of_int l)"
   1.580 +  by simp
   1.581 +
   1.582 +lemma [code]:
   1.583 +  "pdivmod k l = map_pair Target_Numeral.int_of Target_Numeral.int_of
   1.584 +    (Target_Numeral.divmod_abs (of_int k) (of_int l))"
   1.585 +  by (simp add: prod_eq_iff pdivmod_def)
   1.586 +
   1.587 +lemma [code]:
   1.588 +  "k div l = Target_Numeral.int_of (of_int k div of_int l)"
   1.589 +  by simp
   1.590 +
   1.591 +lemma [code]:
   1.592 +  "k mod l = Target_Numeral.int_of (of_int k mod of_int l)"
   1.593 +  by simp
   1.594 +
   1.595 +lemma [code]:
   1.596 +  "HOL.equal k l = HOL.equal (of_int k :: Target_Numeral.int) (of_int l)"
   1.597 +  by (simp add: equal Target_Numeral.int_eq_iff)
   1.598 +
   1.599 +lemma [code]:
   1.600 +  "k \<le> l \<longleftrightarrow> (of_int k :: Target_Numeral.int) \<le> of_int l"
   1.601 +  by (simp add: less_eq_int_def)
   1.602 +
   1.603 +lemma [code]:
   1.604 +  "k < l \<longleftrightarrow> (of_int k :: Target_Numeral.int) < of_int l"
   1.605 +  by (simp add: less_int_def)
   1.606 +
   1.607 +lemma (in ring_1) of_int_code:
   1.608 +  "of_int k = (if k = 0 then 0
   1.609 +     else if k < 0 then - of_int (- k)
   1.610 +     else let
   1.611 +       (l, j) = divmod_int k 2;
   1.612 +       l' = 2 * of_int l
   1.613 +     in if j = 0 then l' else l' + 1)"
   1.614 +proof -
   1.615 +  from mod_div_equality have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
   1.616 +  show ?thesis
   1.617 +    by (simp add: Let_def divmod_int_mod_div mod_2_not_eq_zero_eq_one_int
   1.618 +      of_int_add [symmetric]) (simp add: * mult_commute)
   1.619 +qed
   1.620 +
   1.621 +declare of_int_code [code]
   1.622 +
   1.623 +
   1.624 +subsection {* Implementation for @{typ nat} *}
   1.625 +
   1.626 +definition of_nat :: "nat \<Rightarrow> Target_Numeral.int" where
   1.627 +  [code_abbrev]: "of_nat = Nat.of_nat"
   1.628 +
   1.629 +hide_const (open) of_nat
   1.630 +
   1.631 +lemma int_of_nat [simp]:
   1.632 +  "Target_Numeral.int_of (Target_Numeral.of_nat n) = of_nat n"
   1.633 +  by (simp add: of_nat_def)
   1.634 +
   1.635 +lemma [code abstype]:
   1.636 +  "Target_Numeral.nat_of (Target_Numeral.of_nat n) = n"
   1.637 +  by (simp add: nat_of_def)
   1.638 +
   1.639 +lemma [code_abbrev]:
   1.640 +  "nat (Int.Pos k) = nat_of_num k"
   1.641 +  by (simp add: nat_of_num_numeral)
   1.642 +
   1.643 +lemma [code abstract]:
   1.644 +  "Target_Numeral.of_nat 0 = 0"
   1.645 +  by (simp add: Target_Numeral.int_eq_iff)
   1.646 +
   1.647 +lemma [code abstract]:
   1.648 +  "Target_Numeral.of_nat 1 = 1"
   1.649 +  by (simp add: Target_Numeral.int_eq_iff)
   1.650 +
   1.651 +lemma [code abstract]:
   1.652 +  "Target_Numeral.of_nat (m + n) = of_nat m + of_nat n"
   1.653 +  by (simp add: Target_Numeral.int_eq_iff)
   1.654 +
   1.655 +lemma [code abstract]:
   1.656 +  "Target_Numeral.of_nat (Code_Nat.dup n) = Target_Numeral.dup (of_nat n)"
   1.657 +  by (simp add: Target_Numeral.int_eq_iff Code_Nat.dup_def)
   1.658 +
   1.659 +lemma [code, code del]:
   1.660 +  "Code_Nat.sub = Code_Nat.sub" ..
   1.661 +
   1.662 +lemma [code abstract]:
   1.663 +  "Target_Numeral.of_nat (m - n) = max 0 (of_nat m - of_nat n)"
   1.664 +  by (simp add: Target_Numeral.int_eq_iff)
   1.665 +
   1.666 +lemma [code abstract]:
   1.667 +  "Target_Numeral.of_nat (m * n) = of_nat m * of_nat n"
   1.668 +  by (simp add: Target_Numeral.int_eq_iff of_nat_mult)
   1.669 +
   1.670 +lemma [code abstract]:
   1.671 +  "Target_Numeral.of_nat (m div n) = of_nat m div of_nat n"
   1.672 +  by (simp add: Target_Numeral.int_eq_iff zdiv_int)
   1.673 +
   1.674 +lemma [code abstract]:
   1.675 +  "Target_Numeral.of_nat (m mod n) = of_nat m mod of_nat n"
   1.676 +  by (simp add: Target_Numeral.int_eq_iff zmod_int)
   1.677 +
   1.678 +lemma [code]:
   1.679 +  "Divides.divmod_nat m n = (m div n, m mod n)"
   1.680 +  by (simp add: prod_eq_iff)
   1.681 +
   1.682 +lemma [code]:
   1.683 +  "HOL.equal m n = HOL.equal (of_nat m :: Target_Numeral.int) (of_nat n)"
   1.684 +  by (simp add: equal Target_Numeral.int_eq_iff)
   1.685 +
   1.686 +lemma [code]:
   1.687 +  "m \<le> n \<longleftrightarrow> (of_nat m :: Target_Numeral.int) \<le> of_nat n"
   1.688 +  by (simp add: less_eq_int_def)
   1.689 +
   1.690 +lemma [code]:
   1.691 +  "m < n \<longleftrightarrow> (of_nat m :: Target_Numeral.int) < of_nat n"
   1.692 +  by (simp add: less_int_def)
   1.693 +
   1.694 +lemma num_of_nat_code [code]:
   1.695 +  "num_of_nat = Target_Numeral.num_of_int \<circ> Target_Numeral.of_nat"
   1.696 +  by (simp add: fun_eq_iff num_of_int_def of_nat_def)
   1.697 +
   1.698 +lemma (in semiring_1) of_nat_code:
   1.699 +  "of_nat n = (if n = 0 then 0
   1.700 +     else let
   1.701 +       (m, q) = divmod_nat n 2;
   1.702 +       m' = 2 * of_nat m
   1.703 +     in if q = 0 then m' else m' + 1)"
   1.704 +proof -
   1.705 +  from mod_div_equality have *: "of_nat n = of_nat (n div 2 * 2 + n mod 2)" by simp
   1.706 +  show ?thesis
   1.707 +    by (simp add: Let_def divmod_nat_div_mod mod_2_not_eq_zero_eq_one_nat
   1.708 +      of_nat_add [symmetric])
   1.709 +      (simp add: * mult_commute of_nat_mult add_commute)
   1.710 +qed
   1.711 +
   1.712 +declare of_nat_code [code]
   1.713 +
   1.714 +text {* Conversions between @{typ nat} and @{typ int} *}
   1.715 +
   1.716 +definition int :: "nat \<Rightarrow> int" where
   1.717 +  [code_abbrev]: "int = of_nat"
   1.718 +
   1.719 +hide_const (open) int
   1.720 +
   1.721 +lemma [code]:
   1.722 +  "Target_Numeral.int n = Target_Numeral.int_of (of_nat n)"
   1.723 +  by (simp add: int_def)
   1.724 +
   1.725 +lemma [code abstract]:
   1.726 +  "Target_Numeral.of_nat (nat k) = max 0 (Target_Numeral.of_int k)"
   1.727 +  by (simp add: of_nat_def of_int_of_nat max_def)
   1.728 +
   1.729 +end