src/HOL/Code_Numeral.thy
 author blanchet Thu Mar 06 15:40:33 2014 +0100 (2014-03-06) changeset 55945 e96383acecf9 parent 55736 f1ed1e9cd080 child 56846 9df717fef2bb permissions -rw-r--r--
renamed 'fun_rel' to 'rel_fun'
1 (*  Title:      HOL/Code_Numeral.thy
2     Author:     Florian Haftmann, TU Muenchen
3 *)
5 header {* Numeric types for code generation onto target language numerals only *}
7 theory Code_Numeral
8 imports Nat_Transfer Divides Lifting
9 begin
11 subsection {* Type of target language integers *}
13 typedef integer = "UNIV \<Colon> int set"
14   morphisms int_of_integer integer_of_int ..
16 setup_lifting (no_code) type_definition_integer
18 lemma integer_eq_iff:
19   "k = l \<longleftrightarrow> int_of_integer k = int_of_integer l"
20   by transfer rule
22 lemma integer_eqI:
23   "int_of_integer k = int_of_integer l \<Longrightarrow> k = l"
24   using integer_eq_iff [of k l] by simp
26 lemma int_of_integer_integer_of_int [simp]:
27   "int_of_integer (integer_of_int k) = k"
28   by transfer rule
30 lemma integer_of_int_int_of_integer [simp]:
31   "integer_of_int (int_of_integer k) = k"
32   by transfer rule
34 instantiation integer :: ring_1
35 begin
37 lift_definition zero_integer :: integer
38   is "0 :: int"
39   .
41 declare zero_integer.rep_eq [simp]
43 lift_definition one_integer :: integer
44   is "1 :: int"
45   .
47 declare one_integer.rep_eq [simp]
49 lift_definition plus_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
50   is "plus :: int \<Rightarrow> int \<Rightarrow> int"
51   .
53 declare plus_integer.rep_eq [simp]
55 lift_definition uminus_integer :: "integer \<Rightarrow> integer"
56   is "uminus :: int \<Rightarrow> int"
57   .
59 declare uminus_integer.rep_eq [simp]
61 lift_definition minus_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
62   is "minus :: int \<Rightarrow> int \<Rightarrow> int"
63   .
65 declare minus_integer.rep_eq [simp]
67 lift_definition times_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
68   is "times :: int \<Rightarrow> int \<Rightarrow> int"
69   .
71 declare times_integer.rep_eq [simp]
73 instance proof
74 qed (transfer, simp add: algebra_simps)+
76 end
78 lemma [transfer_rule]:
79   "rel_fun HOL.eq pcr_integer (of_nat :: nat \<Rightarrow> int) (of_nat :: nat \<Rightarrow> integer)"
80   by (unfold of_nat_def [abs_def]) transfer_prover
82 lemma [transfer_rule]:
83   "rel_fun HOL.eq pcr_integer (\<lambda>k :: int. k :: int) (of_int :: int \<Rightarrow> integer)"
84 proof -
85   have "rel_fun HOL.eq pcr_integer (of_int :: int \<Rightarrow> int) (of_int :: int \<Rightarrow> integer)"
86     by (unfold of_int_of_nat [abs_def]) transfer_prover
87   then show ?thesis by (simp add: id_def)
88 qed
90 lemma [transfer_rule]:
91   "rel_fun HOL.eq pcr_integer (numeral :: num \<Rightarrow> int) (numeral :: num \<Rightarrow> integer)"
92 proof -
93   have "rel_fun HOL.eq pcr_integer (numeral :: num \<Rightarrow> int) (\<lambda>n. of_int (numeral n))"
94     by transfer_prover
95   then show ?thesis by simp
96 qed
98 lemma [transfer_rule]:
99   "rel_fun HOL.eq (rel_fun HOL.eq pcr_integer) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> int) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> integer)"
100   by (unfold Num.sub_def [abs_def]) transfer_prover
102 lemma int_of_integer_of_nat [simp]:
103   "int_of_integer (of_nat n) = of_nat n"
104   by transfer rule
106 lift_definition integer_of_nat :: "nat \<Rightarrow> integer"
107   is "of_nat :: nat \<Rightarrow> int"
108   .
110 lemma integer_of_nat_eq_of_nat [code]:
111   "integer_of_nat = of_nat"
112   by transfer rule
114 lemma int_of_integer_integer_of_nat [simp]:
115   "int_of_integer (integer_of_nat n) = of_nat n"
116   by transfer rule
118 lift_definition nat_of_integer :: "integer \<Rightarrow> nat"
119   is Int.nat
120   .
122 lemma nat_of_integer_of_nat [simp]:
123   "nat_of_integer (of_nat n) = n"
124   by transfer simp
126 lemma int_of_integer_of_int [simp]:
127   "int_of_integer (of_int k) = k"
128   by transfer simp
130 lemma nat_of_integer_integer_of_nat [simp]:
131   "nat_of_integer (integer_of_nat n) = n"
132   by transfer simp
134 lemma integer_of_int_eq_of_int [simp, code_abbrev]:
135   "integer_of_int = of_int"
136   by transfer (simp add: fun_eq_iff)
138 lemma of_int_integer_of [simp]:
139   "of_int (int_of_integer k) = (k :: integer)"
140   by transfer rule
142 lemma int_of_integer_numeral [simp]:
143   "int_of_integer (numeral k) = numeral k"
144   by transfer rule
146 lemma int_of_integer_sub [simp]:
147   "int_of_integer (Num.sub k l) = Num.sub k l"
148   by transfer rule
150 instantiation integer :: "{ring_div, equal, linordered_idom}"
151 begin
153 lift_definition div_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
154   is "Divides.div :: int \<Rightarrow> int \<Rightarrow> int"
155   .
157 declare div_integer.rep_eq [simp]
159 lift_definition mod_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
160   is "Divides.mod :: int \<Rightarrow> int \<Rightarrow> int"
161   .
163 declare mod_integer.rep_eq [simp]
165 lift_definition abs_integer :: "integer \<Rightarrow> integer"
166   is "abs :: int \<Rightarrow> int"
167   .
169 declare abs_integer.rep_eq [simp]
171 lift_definition sgn_integer :: "integer \<Rightarrow> integer"
172   is "sgn :: int \<Rightarrow> int"
173   .
175 declare sgn_integer.rep_eq [simp]
177 lift_definition less_eq_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
178   is "less_eq :: int \<Rightarrow> int \<Rightarrow> bool"
179   .
181 lift_definition less_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
182   is "less :: int \<Rightarrow> int \<Rightarrow> bool"
183   .
185 lift_definition equal_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
186   is "HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool"
187   .
189 instance proof
190 qed (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] mult_strict_right_mono linear)+
192 end
194 lemma [transfer_rule]:
195   "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (min :: _ \<Rightarrow> _ \<Rightarrow> int) (min :: _ \<Rightarrow> _ \<Rightarrow> integer)"
196   by (unfold min_def [abs_def]) transfer_prover
198 lemma [transfer_rule]:
199   "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (max :: _ \<Rightarrow> _ \<Rightarrow> int) (max :: _ \<Rightarrow> _ \<Rightarrow> integer)"
200   by (unfold max_def [abs_def]) transfer_prover
202 lemma int_of_integer_min [simp]:
203   "int_of_integer (min k l) = min (int_of_integer k) (int_of_integer l)"
204   by transfer rule
206 lemma int_of_integer_max [simp]:
207   "int_of_integer (max k l) = max (int_of_integer k) (int_of_integer l)"
208   by transfer rule
210 lemma nat_of_integer_non_positive [simp]:
211   "k \<le> 0 \<Longrightarrow> nat_of_integer k = 0"
212   by transfer simp
214 lemma of_nat_of_integer [simp]:
215   "of_nat (nat_of_integer k) = max 0 k"
216   by transfer auto
218 instance integer :: semiring_numeral_div
219   by intro_classes (transfer,
222     semiring_numeral_div_class.mult_div_cancel
223     semiring_numeral_div_class.div_less
224     semiring_numeral_div_class.mod_less
225     semiring_numeral_div_class.div_positive
226     semiring_numeral_div_class.mod_less_eq_dividend
227     semiring_numeral_div_class.pos_mod_bound
228     semiring_numeral_div_class.pos_mod_sign
229     semiring_numeral_div_class.mod_mult2_eq
230     semiring_numeral_div_class.div_mult2_eq
231     semiring_numeral_div_class.discrete)+
233 lemma integer_of_nat_0: "integer_of_nat 0 = 0"
234 by transfer simp
236 lemma integer_of_nat_1: "integer_of_nat 1 = 1"
237 by transfer simp
239 lemma integer_of_nat_numeral:
240   "integer_of_nat (numeral n) = numeral n"
241 by transfer simp
243 subsection {* Code theorems for target language integers *}
245 text {* Constructors *}
247 definition Pos :: "num \<Rightarrow> integer"
248 where
249   [simp, code_abbrev]: "Pos = numeral"
251 lemma [transfer_rule]:
252   "rel_fun HOL.eq pcr_integer numeral Pos"
253   by simp transfer_prover
255 definition Neg :: "num \<Rightarrow> integer"
256 where
257   [simp, code_abbrev]: "Neg n = - Pos n"
259 lemma [transfer_rule]:
260   "rel_fun HOL.eq pcr_integer (\<lambda>n. - numeral n) Neg"
261   by (simp add: Neg_def [abs_def]) transfer_prover
263 code_datatype "0::integer" Pos Neg
266 text {* Auxiliary operations *}
268 lift_definition dup :: "integer \<Rightarrow> integer"
269   is "\<lambda>k::int. k + k"
270   .
272 lemma dup_code [code]:
273   "dup 0 = 0"
274   "dup (Pos n) = Pos (Num.Bit0 n)"
275   "dup (Neg n) = Neg (Num.Bit0 n)"
276   by (transfer, simp only: numeral_Bit0 minus_add_distrib)+
278 lift_definition sub :: "num \<Rightarrow> num \<Rightarrow> integer"
279   is "\<lambda>m n. numeral m - numeral n :: int"
280   .
282 lemma sub_code [code]:
283   "sub Num.One Num.One = 0"
284   "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
285   "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
286   "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
287   "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
288   "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
289   "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
290   "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
291   "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
292   by (transfer, simp add: dbl_def dbl_inc_def dbl_dec_def)+
295 text {* Implementations *}
297 lemma one_integer_code [code, code_unfold]:
298   "1 = Pos Num.One"
299   by simp
301 lemma plus_integer_code [code]:
302   "k + 0 = (k::integer)"
303   "0 + l = (l::integer)"
304   "Pos m + Pos n = Pos (m + n)"
305   "Pos m + Neg n = sub m n"
306   "Neg m + Pos n = sub n m"
307   "Neg m + Neg n = Neg (m + n)"
308   by (transfer, simp)+
310 lemma uminus_integer_code [code]:
311   "uminus 0 = (0::integer)"
312   "uminus (Pos m) = Neg m"
313   "uminus (Neg m) = Pos m"
314   by simp_all
316 lemma minus_integer_code [code]:
317   "k - 0 = (k::integer)"
318   "0 - l = uminus (l::integer)"
319   "Pos m - Pos n = sub m n"
320   "Pos m - Neg n = Pos (m + n)"
321   "Neg m - Pos n = Neg (m + n)"
322   "Neg m - Neg n = sub n m"
323   by (transfer, simp)+
325 lemma abs_integer_code [code]:
326   "\<bar>k\<bar> = (if (k::integer) < 0 then - k else k)"
327   by simp
329 lemma sgn_integer_code [code]:
330   "sgn k = (if k = 0 then 0 else if (k::integer) < 0 then - 1 else 1)"
331   by simp
333 lemma times_integer_code [code]:
334   "k * 0 = (0::integer)"
335   "0 * l = (0::integer)"
336   "Pos m * Pos n = Pos (m * n)"
337   "Pos m * Neg n = Neg (m * n)"
338   "Neg m * Pos n = Neg (m * n)"
339   "Neg m * Neg n = Pos (m * n)"
340   by simp_all
342 definition divmod_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
343 where
344   "divmod_integer k l = (k div l, k mod l)"
346 lemma fst_divmod [simp]:
347   "fst (divmod_integer k l) = k div l"
350 lemma snd_divmod [simp]:
351   "snd (divmod_integer k l) = k mod l"
354 definition divmod_abs :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
355 where
356   "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
358 lemma fst_divmod_abs [simp]:
359   "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
362 lemma snd_divmod_abs [simp]:
363   "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
366 lemma divmod_abs_code [code]:
367   "divmod_abs (Pos k) (Pos l) = divmod k l"
368   "divmod_abs (Neg k) (Neg l) = divmod k l"
369   "divmod_abs (Neg k) (Pos l) = divmod k l"
370   "divmod_abs (Pos k) (Neg l) = divmod k l"
371   "divmod_abs j 0 = (0, \<bar>j\<bar>)"
372   "divmod_abs 0 j = (0, 0)"
375 lemma divmod_integer_code [code]:
376   "divmod_integer k l =
377     (if k = 0 then (0, 0) else if l = 0 then (0, k) else
378     (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
379       then divmod_abs k l
380       else (let (r, s) = divmod_abs k l in
381         if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
382 proof -
383   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"
384     by (auto simp add: sgn_if)
385   have aux2: "\<And>q::int. - int_of_integer k = int_of_integer l * q \<longleftrightarrow> int_of_integer k = int_of_integer l * - q" by auto
386   show ?thesis
387     by (simp add: prod_eq_iff integer_eq_iff case_prod_beta aux1)
388       (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if div_minus_right mod_minus_right aux2)
389 qed
391 lemma div_integer_code [code]:
392   "k div l = fst (divmod_integer k l)"
393   by simp
395 lemma mod_integer_code [code]:
396   "k mod l = snd (divmod_integer k l)"
397   by simp
399 lemma equal_integer_code [code]:
400   "HOL.equal 0 (0::integer) \<longleftrightarrow> True"
401   "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
402   "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
403   "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
404   "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
405   "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
406   "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
407   "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
408   "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
411 lemma equal_integer_refl [code nbe]:
412   "HOL.equal (k::integer) k \<longleftrightarrow> True"
413   by (fact equal_refl)
415 lemma less_eq_integer_code [code]:
416   "0 \<le> (0::integer) \<longleftrightarrow> True"
417   "0 \<le> Pos l \<longleftrightarrow> True"
418   "0 \<le> Neg l \<longleftrightarrow> False"
419   "Pos k \<le> 0 \<longleftrightarrow> False"
420   "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
421   "Pos k \<le> Neg l \<longleftrightarrow> False"
422   "Neg k \<le> 0 \<longleftrightarrow> True"
423   "Neg k \<le> Pos l \<longleftrightarrow> True"
424   "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
425   by simp_all
427 lemma less_integer_code [code]:
428   "0 < (0::integer) \<longleftrightarrow> False"
429   "0 < Pos l \<longleftrightarrow> True"
430   "0 < Neg l \<longleftrightarrow> False"
431   "Pos k < 0 \<longleftrightarrow> False"
432   "Pos k < Pos l \<longleftrightarrow> k < l"
433   "Pos k < Neg l \<longleftrightarrow> False"
434   "Neg k < 0 \<longleftrightarrow> True"
435   "Neg k < Pos l \<longleftrightarrow> True"
436   "Neg k < Neg l \<longleftrightarrow> l < k"
437   by simp_all
439 lift_definition integer_of_num :: "num \<Rightarrow> integer"
440   is "numeral :: num \<Rightarrow> int"
441   .
443 lemma integer_of_num [code]:
444   "integer_of_num num.One = 1"
445   "integer_of_num (num.Bit0 n) = (let k = integer_of_num n in k + k)"
446   "integer_of_num (num.Bit1 n) = (let k = integer_of_num n in k + k + 1)"
447   by (transfer, simp only: numeral.simps Let_def)+
449 lift_definition num_of_integer :: "integer \<Rightarrow> num"
450   is "num_of_nat \<circ> nat"
451   .
453 lemma num_of_integer_code [code]:
454   "num_of_integer k = (if k \<le> 1 then Num.One
455      else let
456        (l, j) = divmod_integer k 2;
457        l' = num_of_integer l;
458        l'' = l' + l'
459      in if j = 0 then l'' else l'' + Num.One)"
460 proof -
461   {
462     assume "int_of_integer k mod 2 = 1"
463     then have "nat (int_of_integer k mod 2) = nat 1" by simp
464     moreover assume *: "1 < int_of_integer k"
465     ultimately have **: "nat (int_of_integer k) mod 2 = 1" by (simp add: nat_mod_distrib)
466     have "num_of_nat (nat (int_of_integer k)) =
467       num_of_nat (2 * (nat (int_of_integer k) div 2) + nat (int_of_integer k) mod 2)"
468       by simp
469     then have "num_of_nat (nat (int_of_integer k)) =
470       num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + nat (int_of_integer k) mod 2)"
472     with ** have "num_of_nat (nat (int_of_integer k)) =
473       num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + 1)"
474       by simp
475   }
476   note aux = this
477   show ?thesis
478     by (auto simp add: num_of_integer_def nat_of_integer_def Let_def case_prod_beta
479       not_le integer_eq_iff less_eq_integer_def
480       nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
481        mult_2 [where 'a=nat] aux add_One)
482 qed
484 lemma nat_of_integer_code [code]:
485   "nat_of_integer k = (if k \<le> 0 then 0
486      else let
487        (l, j) = divmod_integer k 2;
488        l' = nat_of_integer l;
489        l'' = l' + l'
490      in if j = 0 then l'' else l'' + 1)"
491 proof -
492   obtain j where "k = integer_of_int j"
493   proof
494     show "k = integer_of_int (int_of_integer k)" by simp
495   qed
496   moreover have "2 * (j div 2) = j - j mod 2"
497     by (simp add: zmult_div_cancel mult_commute)
498   ultimately show ?thesis
499     by (auto simp add: split_def Let_def mod_integer_def nat_of_integer_def not_le
501       (auto simp add: mult_2 [symmetric])
502 qed
504 lemma int_of_integer_code [code]:
505   "int_of_integer k = (if k < 0 then - (int_of_integer (- k))
506      else if k = 0 then 0
507      else let
508        (l, j) = divmod_integer k 2;
509        l' = 2 * int_of_integer l
510      in if j = 0 then l' else l' + 1)"
511   by (auto simp add: split_def Let_def integer_eq_iff zmult_div_cancel)
513 lemma integer_of_int_code [code]:
514   "integer_of_int k = (if k < 0 then - (integer_of_int (- k))
515      else if k = 0 then 0
516      else let
517        (l, j) = divmod_int k 2;
518        l' = 2 * integer_of_int l
519      in if j = 0 then l' else l' + 1)"
520   by (auto simp add: split_def Let_def integer_eq_iff zmult_div_cancel)
522 hide_const (open) Pos Neg sub dup divmod_abs
525 subsection {* Serializer setup for target language integers *}
527 code_reserved Eval int Integer abs
529 code_printing
530   type_constructor integer \<rightharpoonup>
531     (SML) "IntInf.int"
532     and (OCaml) "Big'_int.big'_int"
534     and (Scala) "BigInt"
535     and (Eval) "int"
536 | class_instance integer :: equal \<rightharpoonup>
539 code_printing
540   constant "0::integer" \<rightharpoonup>
541     (SML) "0"
542     and (OCaml) "Big'_int.zero'_big'_int"
544     and (Scala) "BigInt(0)"
546 setup {*
548     false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
549 *}
551 setup {*
553     true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
554 *}
556 code_printing
557   constant "plus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
558     (SML) "IntInf.+ ((_), (_))"
560     and (Haskell) infixl 6 "+"
561     and (Scala) infixl 7 "+"
562     and (Eval) infixl 8 "+"
563 | constant "uminus :: integer \<Rightarrow> _" \<rightharpoonup>
564     (SML) "IntInf.~"
565     and (OCaml) "Big'_int.minus'_big'_int"
567     and (Scala) "!(- _)"
568     and (Eval) "~/ _"
569 | constant "minus :: integer \<Rightarrow> _" \<rightharpoonup>
570     (SML) "IntInf.- ((_), (_))"
571     and (OCaml) "Big'_int.sub'_big'_int"
572     and (Haskell) infixl 6 "-"
573     and (Scala) infixl 7 "-"
574     and (Eval) infixl 8 "-"
575 | constant Code_Numeral.dup \<rightharpoonup>
576     (SML) "IntInf.*/ (2,/ (_))"
577     and (OCaml) "Big'_int.mult'_big'_int/ (Big'_int.big'_int'_of'_int/ 2)"
578     and (Haskell) "!(2 * _)"
579     and (Scala) "!(2 * _)"
580     and (Eval) "!(2 * _)"
581 | constant Code_Numeral.sub \<rightharpoonup>
582     (SML) "!(raise/ Fail/ \"sub\")"
583     and (OCaml) "failwith/ \"sub\""
585     and (Scala) "!sys.error(\"sub\")"
586 | constant "times :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
587     (SML) "IntInf.* ((_), (_))"
588     and (OCaml) "Big'_int.mult'_big'_int"
589     and (Haskell) infixl 7 "*"
590     and (Scala) infixl 8 "*"
591     and (Eval) infixl 9 "*"
592 | constant Code_Numeral.divmod_abs \<rightharpoonup>
593     (SML) "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)"
594     and (OCaml) "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)"
595     and (Haskell) "divMod/ (abs _)/ (abs _)"
596     and (Scala) "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))"
597     and (Eval) "Integer.div'_mod/ (abs _)/ (abs _)"
598 | constant "HOL.equal :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
599     (SML) "!((_ : IntInf.int) = _)"
600     and (OCaml) "Big'_int.eq'_big'_int"
601     and (Haskell) infix 4 "=="
602     and (Scala) infixl 5 "=="
603     and (Eval) infixl 6 "="
604 | constant "less_eq :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
605     (SML) "IntInf.<= ((_), (_))"
606     and (OCaml) "Big'_int.le'_big'_int"
607     and (Haskell) infix 4 "<="
608     and (Scala) infixl 4 "<="
609     and (Eval) infixl 6 "<="
610 | constant "less :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
611     (SML) "IntInf.< ((_), (_))"
612     and (OCaml) "Big'_int.lt'_big'_int"
613     and (Haskell) infix 4 "<"
614     and (Scala) infixl 4 "<"
615     and (Eval) infixl 6 "<"
617 code_identifier
618   code_module Code_Numeral \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
621 subsection {* Type of target language naturals *}
623 typedef natural = "UNIV \<Colon> nat set"
624   morphisms nat_of_natural natural_of_nat ..
626 setup_lifting (no_code) type_definition_natural
628 lemma natural_eq_iff [termination_simp]:
629   "m = n \<longleftrightarrow> nat_of_natural m = nat_of_natural n"
630   by transfer rule
632 lemma natural_eqI:
633   "nat_of_natural m = nat_of_natural n \<Longrightarrow> m = n"
634   using natural_eq_iff [of m n] by simp
636 lemma nat_of_natural_of_nat_inverse [simp]:
637   "nat_of_natural (natural_of_nat n) = n"
638   by transfer rule
640 lemma natural_of_nat_of_natural_inverse [simp]:
641   "natural_of_nat (nat_of_natural n) = n"
642   by transfer rule
644 instantiation natural :: "{comm_monoid_diff, semiring_1}"
645 begin
647 lift_definition zero_natural :: natural
648   is "0 :: nat"
649   .
651 declare zero_natural.rep_eq [simp]
653 lift_definition one_natural :: natural
654   is "1 :: nat"
655   .
657 declare one_natural.rep_eq [simp]
659 lift_definition plus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
660   is "plus :: nat \<Rightarrow> nat \<Rightarrow> nat"
661   .
663 declare plus_natural.rep_eq [simp]
665 lift_definition minus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
666   is "minus :: nat \<Rightarrow> nat \<Rightarrow> nat"
667   .
669 declare minus_natural.rep_eq [simp]
671 lift_definition times_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
672   is "times :: nat \<Rightarrow> nat \<Rightarrow> nat"
673   .
675 declare times_natural.rep_eq [simp]
677 instance proof
678 qed (transfer, simp add: algebra_simps)+
680 end
682 lemma [transfer_rule]:
683   "rel_fun HOL.eq pcr_natural (\<lambda>n::nat. n) (of_nat :: nat \<Rightarrow> natural)"
684 proof -
685   have "rel_fun HOL.eq pcr_natural (of_nat :: nat \<Rightarrow> nat) (of_nat :: nat \<Rightarrow> natural)"
686     by (unfold of_nat_def [abs_def]) transfer_prover
687   then show ?thesis by (simp add: id_def)
688 qed
690 lemma [transfer_rule]:
691   "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (numeral :: num \<Rightarrow> natural)"
692 proof -
693   have "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (\<lambda>n. of_nat (numeral n))"
694     by transfer_prover
695   then show ?thesis by simp
696 qed
698 lemma nat_of_natural_of_nat [simp]:
699   "nat_of_natural (of_nat n) = n"
700   by transfer rule
702 lemma natural_of_nat_of_nat [simp, code_abbrev]:
703   "natural_of_nat = of_nat"
704   by transfer rule
706 lemma of_nat_of_natural [simp]:
707   "of_nat (nat_of_natural n) = n"
708   by transfer rule
710 lemma nat_of_natural_numeral [simp]:
711   "nat_of_natural (numeral k) = numeral k"
712   by transfer rule
714 instantiation natural :: "{semiring_div, equal, linordered_semiring}"
715 begin
717 lift_definition div_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
718   is "Divides.div :: nat \<Rightarrow> nat \<Rightarrow> nat"
719   .
721 declare div_natural.rep_eq [simp]
723 lift_definition mod_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
724   is "Divides.mod :: nat \<Rightarrow> nat \<Rightarrow> nat"
725   .
727 declare mod_natural.rep_eq [simp]
729 lift_definition less_eq_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
730   is "less_eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
731   .
733 declare less_eq_natural.rep_eq [termination_simp]
735 lift_definition less_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
736   is "less :: nat \<Rightarrow> nat \<Rightarrow> bool"
737   .
739 declare less_natural.rep_eq [termination_simp]
741 lift_definition equal_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
742   is "HOL.equal :: nat \<Rightarrow> nat \<Rightarrow> bool"
743   .
745 instance proof
746 qed (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] linear)+
748 end
750 lemma [transfer_rule]:
751   "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (min :: _ \<Rightarrow> _ \<Rightarrow> nat) (min :: _ \<Rightarrow> _ \<Rightarrow> natural)"
752   by (unfold min_def [abs_def]) transfer_prover
754 lemma [transfer_rule]:
755   "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (max :: _ \<Rightarrow> _ \<Rightarrow> nat) (max :: _ \<Rightarrow> _ \<Rightarrow> natural)"
756   by (unfold max_def [abs_def]) transfer_prover
758 lemma nat_of_natural_min [simp]:
759   "nat_of_natural (min k l) = min (nat_of_natural k) (nat_of_natural l)"
760   by transfer rule
762 lemma nat_of_natural_max [simp]:
763   "nat_of_natural (max k l) = max (nat_of_natural k) (nat_of_natural l)"
764   by transfer rule
766 lift_definition natural_of_integer :: "integer \<Rightarrow> natural"
767   is "nat :: int \<Rightarrow> nat"
768   .
770 lift_definition integer_of_natural :: "natural \<Rightarrow> integer"
771   is "of_nat :: nat \<Rightarrow> int"
772   .
774 lemma natural_of_integer_of_natural [simp]:
775   "natural_of_integer (integer_of_natural n) = n"
776   by transfer simp
778 lemma integer_of_natural_of_integer [simp]:
779   "integer_of_natural (natural_of_integer k) = max 0 k"
780   by transfer auto
782 lemma int_of_integer_of_natural [simp]:
783   "int_of_integer (integer_of_natural n) = of_nat (nat_of_natural n)"
784   by transfer rule
786 lemma integer_of_natural_of_nat [simp]:
787   "integer_of_natural (of_nat n) = of_nat n"
788   by transfer rule
790 lemma [measure_function]:
791   "is_measure nat_of_natural"
792   by (rule is_measure_trivial)
795 subsection {* Inductive representation of target language naturals *}
797 lift_definition Suc :: "natural \<Rightarrow> natural"
798   is Nat.Suc
799   .
801 declare Suc.rep_eq [simp]
803 rep_datatype "0::natural" Suc
804   by (transfer, fact nat.induct nat.inject nat.distinct)+
806 lemma natural_cases [case_names nat, cases type: natural]:
807   fixes m :: natural
808   assumes "\<And>n. m = of_nat n \<Longrightarrow> P"
809   shows P
810   using assms by transfer blast
812 lemma [simp, code]:
813   "natural_size = nat_of_natural"
814 proof (rule ext)
815   fix n
816   show "natural_size n = nat_of_natural n"
817     by (induct n) simp_all
818 qed
820 lemma [simp, code]:
821   "size = nat_of_natural"
822 proof (rule ext)
823   fix n
824   show "size n = nat_of_natural n"
825     by (induct n) simp_all
826 qed
828 lemma natural_decr [termination_simp]:
829   "n \<noteq> 0 \<Longrightarrow> nat_of_natural n - Nat.Suc 0 < nat_of_natural n"
830   by transfer simp
832 lemma natural_zero_minus_one:
833   "(0::natural) - 1 = 0"
834   by simp
836 lemma Suc_natural_minus_one:
837   "Suc n - 1 = n"
838   by transfer simp
840 hide_const (open) Suc
843 subsection {* Code refinement for target language naturals *}
845 lift_definition Nat :: "integer \<Rightarrow> natural"
846   is nat
847   .
849 lemma [code_post]:
850   "Nat 0 = 0"
851   "Nat 1 = 1"
852   "Nat (numeral k) = numeral k"
853   by (transfer, simp)+
855 lemma [code abstype]:
856   "Nat (integer_of_natural n) = n"
857   by transfer simp
859 lemma [code abstract]:
860   "integer_of_natural (natural_of_nat n) = of_nat n"
861   by simp
863 lemma [code abstract]:
864   "integer_of_natural (natural_of_integer k) = max 0 k"
865   by simp
867 lemma [code_abbrev]:
868   "natural_of_integer (Code_Numeral.Pos k) = numeral k"
869   by transfer simp
871 lemma [code abstract]:
872   "integer_of_natural 0 = 0"
873   by transfer simp
875 lemma [code abstract]:
876   "integer_of_natural 1 = 1"
877   by transfer simp
879 lemma [code abstract]:
880   "integer_of_natural (Code_Numeral.Suc n) = integer_of_natural n + 1"
881   by transfer simp
883 lemma [code]:
884   "nat_of_natural = nat_of_integer \<circ> integer_of_natural"
885   by transfer (simp add: fun_eq_iff)
887 lemma [code, code_unfold]:
888   "case_natural f g n = (if n = 0 then f else g (n - 1))"
889   by (cases n rule: natural.exhaust) (simp_all, simp add: Suc_def)
891 declare natural.rec [code del]
893 lemma [code abstract]:
894   "integer_of_natural (m + n) = integer_of_natural m + integer_of_natural n"
895   by transfer simp
897 lemma [code abstract]:
898   "integer_of_natural (m - n) = max 0 (integer_of_natural m - integer_of_natural n)"
899   by transfer simp
901 lemma [code abstract]:
902   "integer_of_natural (m * n) = integer_of_natural m * integer_of_natural n"
903   by transfer (simp add: of_nat_mult)
905 lemma [code abstract]:
906   "integer_of_natural (m div n) = integer_of_natural m div integer_of_natural n"
907   by transfer (simp add: zdiv_int)
909 lemma [code abstract]:
910   "integer_of_natural (m mod n) = integer_of_natural m mod integer_of_natural n"
911   by transfer (simp add: zmod_int)
913 lemma [code]:
914   "HOL.equal m n \<longleftrightarrow> HOL.equal (integer_of_natural m) (integer_of_natural n)"
915   by transfer (simp add: equal)
917 lemma [code nbe]:
918   "HOL.equal n (n::natural) \<longleftrightarrow> True"
921 lemma [code]:
922   "m \<le> n \<longleftrightarrow> integer_of_natural m \<le> integer_of_natural n"
923   by transfer simp
925 lemma [code]:
926   "m < n \<longleftrightarrow> integer_of_natural m < integer_of_natural n"
927   by transfer simp
929 hide_const (open) Nat
931 lifting_update integer.lifting
932 lifting_forget integer.lifting
934 lifting_update natural.lifting
935 lifting_forget natural.lifting
937 code_reflect Code_Numeral
938   datatypes natural = _
939   functions integer_of_natural natural_of_integer
941 end