equal
deleted
inserted
replaced
1 (* Title: HOL/Code_Numeral.thy |
1 (* Title: HOL/Code_Numeral.thy |
2 Author: Florian Haftmann, TU Muenchen |
2 Author: Florian Haftmann, TU Muenchen |
3 *) |
3 *) |
4 |
4 |
5 section {* Numeric types for code generation onto target language numerals only *} |
5 section \<open>Numeric types for code generation onto target language numerals only\<close> |
6 |
6 |
7 theory Code_Numeral |
7 theory Code_Numeral |
8 imports Nat_Transfer Divides Lifting |
8 imports Nat_Transfer Divides Lifting |
9 begin |
9 begin |
10 |
10 |
11 subsection {* Type of target language integers *} |
11 subsection \<open>Type of target language integers\<close> |
12 |
12 |
13 typedef integer = "UNIV \<Colon> int set" |
13 typedef integer = "UNIV \<Colon> int set" |
14 morphisms int_of_integer integer_of_int .. |
14 morphisms int_of_integer integer_of_int .. |
15 |
15 |
16 setup_lifting type_definition_integer |
16 setup_lifting type_definition_integer |
236 |
236 |
237 lemma integer_of_nat_numeral: |
237 lemma integer_of_nat_numeral: |
238 "integer_of_nat (numeral n) = numeral n" |
238 "integer_of_nat (numeral n) = numeral n" |
239 by transfer simp |
239 by transfer simp |
240 |
240 |
241 subsection {* Code theorems for target language integers *} |
241 subsection \<open>Code theorems for target language integers\<close> |
242 |
242 |
243 text {* Constructors *} |
243 text \<open>Constructors\<close> |
244 |
244 |
245 definition Pos :: "num \<Rightarrow> integer" |
245 definition Pos :: "num \<Rightarrow> integer" |
246 where |
246 where |
247 [simp, code_abbrev]: "Pos = numeral" |
247 [simp, code_abbrev]: "Pos = numeral" |
248 |
248 |
259 by (simp add: Neg_def [abs_def]) transfer_prover |
259 by (simp add: Neg_def [abs_def]) transfer_prover |
260 |
260 |
261 code_datatype "0::integer" Pos Neg |
261 code_datatype "0::integer" Pos Neg |
262 |
262 |
263 |
263 |
264 text {* Auxiliary operations *} |
264 text \<open>Auxiliary operations\<close> |
265 |
265 |
266 lift_definition dup :: "integer \<Rightarrow> integer" |
266 lift_definition dup :: "integer \<Rightarrow> integer" |
267 is "\<lambda>k::int. k + k" |
267 is "\<lambda>k::int. k + k" |
268 . |
268 . |
269 |
269 |
288 "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1" |
288 "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1" |
289 "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1" |
289 "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1" |
290 by (transfer, simp add: dbl_def dbl_inc_def dbl_dec_def)+ |
290 by (transfer, simp add: dbl_def dbl_inc_def dbl_dec_def)+ |
291 |
291 |
292 |
292 |
293 text {* Implementations *} |
293 text \<open>Implementations\<close> |
294 |
294 |
295 lemma one_integer_code [code, code_unfold]: |
295 lemma one_integer_code [code, code_unfold]: |
296 "1 = Pos Num.One" |
296 "1 = Pos Num.One" |
297 by simp |
297 by simp |
298 |
298 |
518 by (auto simp add: split_def Let_def integer_eq_iff zmult_div_cancel) |
518 by (auto simp add: split_def Let_def integer_eq_iff zmult_div_cancel) |
519 |
519 |
520 hide_const (open) Pos Neg sub dup divmod_abs |
520 hide_const (open) Pos Neg sub dup divmod_abs |
521 |
521 |
522 |
522 |
523 subsection {* Serializer setup for target language integers *} |
523 subsection \<open>Serializer setup for target language integers\<close> |
524 |
524 |
525 code_reserved Eval int Integer abs |
525 code_reserved Eval int Integer abs |
526 |
526 |
527 code_printing |
527 code_printing |
528 type_constructor integer \<rightharpoonup> |
528 type_constructor integer \<rightharpoonup> |
539 (SML) "!(0/ :/ IntInf.int)" |
539 (SML) "!(0/ :/ IntInf.int)" |
540 and (OCaml) "Big'_int.zero'_big'_int" |
540 and (OCaml) "Big'_int.zero'_big'_int" |
541 and (Haskell) "!(0/ ::/ Integer)" |
541 and (Haskell) "!(0/ ::/ Integer)" |
542 and (Scala) "BigInt(0)" |
542 and (Scala) "BigInt(0)" |
543 |
543 |
544 setup {* |
544 setup \<open> |
545 fold (fn target => |
545 fold (fn target => |
546 Numeral.add_code @{const_name Code_Numeral.Pos} I Code_Printer.literal_numeral target |
546 Numeral.add_code @{const_name Code_Numeral.Pos} I Code_Printer.literal_numeral target |
547 #> Numeral.add_code @{const_name Code_Numeral.Neg} (op ~) Code_Printer.literal_numeral target) |
547 #> Numeral.add_code @{const_name Code_Numeral.Neg} (op ~) Code_Printer.literal_numeral target) |
548 ["SML", "OCaml", "Haskell", "Scala"] |
548 ["SML", "OCaml", "Haskell", "Scala"] |
549 *} |
549 \<close> |
550 |
550 |
551 code_printing |
551 code_printing |
552 constant "plus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup> |
552 constant "plus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup> |
553 (SML) "IntInf.+ ((_), (_))" |
553 (SML) "IntInf.+ ((_), (_))" |
554 and (OCaml) "Big'_int.add'_big'_int" |
554 and (OCaml) "Big'_int.add'_big'_int" |
611 |
611 |
612 code_identifier |
612 code_identifier |
613 code_module Code_Numeral \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith |
613 code_module Code_Numeral \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith |
614 |
614 |
615 |
615 |
616 subsection {* Type of target language naturals *} |
616 subsection \<open>Type of target language naturals\<close> |
617 |
617 |
618 typedef natural = "UNIV \<Colon> nat set" |
618 typedef natural = "UNIV \<Colon> nat set" |
619 morphisms nat_of_natural natural_of_nat .. |
619 morphisms nat_of_natural natural_of_nat .. |
620 |
620 |
621 setup_lifting type_definition_natural |
621 setup_lifting type_definition_natural |
785 lemma [measure_function]: |
785 lemma [measure_function]: |
786 "is_measure nat_of_natural" |
786 "is_measure nat_of_natural" |
787 by (rule is_measure_trivial) |
787 by (rule is_measure_trivial) |
788 |
788 |
789 |
789 |
790 subsection {* Inductive representation of target language naturals *} |
790 subsection \<open>Inductive representation of target language naturals\<close> |
791 |
791 |
792 lift_definition Suc :: "natural \<Rightarrow> natural" |
792 lift_definition Suc :: "natural \<Rightarrow> natural" |
793 is Nat.Suc |
793 is Nat.Suc |
794 . |
794 . |
795 |
795 |
829 by transfer simp |
829 by transfer simp |
830 |
830 |
831 hide_const (open) Suc |
831 hide_const (open) Suc |
832 |
832 |
833 |
833 |
834 subsection {* Code refinement for target language naturals *} |
834 subsection \<open>Code refinement for target language naturals\<close> |
835 |
835 |
836 lift_definition Nat :: "integer \<Rightarrow> natural" |
836 lift_definition Nat :: "integer \<Rightarrow> natural" |
837 is nat |
837 is nat |
838 . |
838 . |
839 |
839 |