restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
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;
21 in if j = 0 then l' else Suc l')"
23 have "2 = nat 2" by simp
25 apply (subst mult_2 [symmetric])
26 apply (auto simp add: Let_def divmod_int_mod_div not_le
27 nat_div_distrib nat_mult_distrib mult_div_cancel mod_2_not_eq_zero_eq_one_int)
28 apply (unfold `2 = nat 2`)
29 apply (subst nat_mod_distrib [symmetric])
34 lemma (in ring_1) of_int_code:
35 "of_int k = (if k = 0 then 0
36 else if k < 0 then - of_int (- k)
38 (l, j) = divmod_int k 2;
40 in if j = 0 then l' else l' + 1)"
42 from mod_div_equality have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
44 by (simp add: Let_def divmod_int_mod_div mod_2_not_eq_zero_eq_one_int
45 of_int_add [symmetric]) (simp add: * mult_commute)
48 declare of_int_code [code]
51 HOL numeral expressions are mapped to integer literals
52 in target languages, using predefined target language
53 operations for abstract integer operations.
58 (OCaml "Big'_int.big'_int")
63 code_instance int :: equal
68 (OCaml "Big'_int.zero'_big'_int")
73 fold (Numeral.add_code @{const_name Int.Pos}
74 false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
78 fold (Numeral.add_code @{const_name Int.Neg}
79 true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
82 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
83 (SML "IntInf.+ ((_), (_))")
84 (OCaml "Big'_int.add'_big'_int")
85 (Haskell infixl 6 "+")
89 code_const "uminus \<Colon> int \<Rightarrow> int"
91 (OCaml "Big'_int.minus'_big'_int")
96 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
97 (SML "IntInf.- ((_), (_))")
98 (OCaml "Big'_int.sub'_big'_int")
99 (Haskell infixl 6 "-")
104 (SML "IntInf.*/ (2,/ (_))")
105 (OCaml "Big'_int.mult'_big'_int/ 2")
111 (SML "!(raise/ Fail/ \"sub\")")
112 (OCaml "failwith/ \"sub\"")
113 (Haskell "error/ \"sub\"")
114 (Scala "!sys.error(\"sub\")")
116 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
117 (SML "IntInf.* ((_), (_))")
118 (OCaml "Big'_int.mult'_big'_int")
119 (Haskell infixl 7 "*")
124 (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
125 (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
126 (Haskell "divMod/ (abs _)/ (abs _)")
127 (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
128 (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
130 code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
131 (SML "!((_ : IntInf.int) = _)")
132 (OCaml "Big'_int.eq'_big'_int")
133 (Haskell infix 4 "==")
134 (Scala infixl 5 "==")
137 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
138 (SML "IntInf.<= ((_), (_))")
139 (OCaml "Big'_int.le'_big'_int")
140 (Haskell infix 4 "<=")
141 (Scala infixl 4 "<=")
144 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
145 (SML "IntInf.< ((_), (_))")
146 (OCaml "Big'_int.lt'_big'_int")
147 (Haskell infix 4 "<")
151 code_const Code_Numeral.int_of
152 (SML "IntInf.fromInt")
154 (Haskell "Prelude.toInteger")
155 (Scala "!_.as'_BigInt")
158 code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
159 (Eval "HOLogic.mk'_number/ HOLogic.intT")