1 (* Title: HOL/Library/Code_Integer.thy
2 Author: Florian Haftmann, TU Muenchen
5 header {* Pretty integer literals for code generation *}
8 imports Main Code_Natural
12 Representation-ignorant code equations for conversions.
15 lemma nat_code [code]:
16 "nat k = (if k \<le> 0 then 0 else
18 (l, j) = divmod_int k 2;
20 in if j = 0 then l' else Suc l')"
22 have "2 = nat 2" by simp
24 apply (auto simp add: Let_def divmod_int_mod_div not_le
25 nat_div_distrib nat_mult_distrib mult_div_cancel mod_2_not_eq_zero_eq_one_int)
26 apply (unfold `2 = nat 2`)
27 apply (subst nat_mod_distrib [symmetric])
32 lemma (in ring_1) of_int_code:
33 "of_int k = (if k = 0 then 0
34 else if k < 0 then - of_int (- k)
36 (l, j) = divmod_int k 2;
38 in if j = 0 then l' else l' + 1)"
40 from mod_div_equality have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
42 by (simp add: Let_def divmod_int_mod_div mod_2_not_eq_zero_eq_one_int
43 of_int_add [symmetric]) (simp add: * mult_commute)
46 declare of_int_code [code]
49 HOL numeral expressions are mapped to integer literals
50 in target languages, using predefined target language
51 operations for abstract integer operations.
56 (OCaml "Big'_int.big'_int")
61 code_instance int :: equal
66 (OCaml "Big'_int.zero'_big'_int")
71 fold (Numeral.add_code @{const_name Int.Pos}
72 false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
76 fold (Numeral.add_code @{const_name Int.Neg}
77 true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
80 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
81 (SML "IntInf.+ ((_), (_))")
82 (OCaml "Big'_int.add'_big'_int")
83 (Haskell infixl 6 "+")
87 code_const "uminus \<Colon> int \<Rightarrow> int"
89 (OCaml "Big'_int.minus'_big'_int")
94 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
95 (SML "IntInf.- ((_), (_))")
96 (OCaml "Big'_int.sub'_big'_int")
97 (Haskell infixl 6 "-")
102 (SML "IntInf.*/ (2,/ (_))")
103 (OCaml "Big'_int.mult'_big'_int/ 2")
109 (SML "!(raise/ Fail/ \"sub\")")
110 (OCaml "failwith/ \"sub\"")
111 (Haskell "error/ \"sub\"")
112 (Scala "!error(\"sub\")")
114 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
115 (SML "IntInf.* ((_), (_))")
116 (OCaml "Big'_int.mult'_big'_int")
117 (Haskell infixl 7 "*")
122 (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
123 (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
124 (Haskell "divMod/ (abs _)/ (abs _)")
125 (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
126 (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
128 code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
129 (SML "!((_ : IntInf.int) = _)")
130 (OCaml "Big'_int.eq'_big'_int")
131 (Haskell infix 4 "==")
132 (Scala infixl 5 "==")
135 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
136 (SML "IntInf.<= ((_), (_))")
137 (OCaml "Big'_int.le'_big'_int")
138 (Haskell infix 4 "<=")
139 (Scala infixl 4 "<=")
142 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
143 (SML "IntInf.< ((_), (_))")
144 (OCaml "Big'_int.lt'_big'_int")
145 (Haskell infix 4 "<")
149 code_const Code_Numeral.int_of
150 (SML "IntInf.fromInt")
152 (Haskell "toInteger")
153 (Scala "!_.as'_BigInt")
156 code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
157 (Eval "HOLogic.mk'_number/ HOLogic.intT")