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 HOL numeral expressions are mapped to integer literals
13 in target languages, using predefined target language
14 operations for abstract integer operations.
19 (OCaml "Big'_int.big'_int")
23 code_instance int :: eq
27 fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
28 true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
31 code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
32 (SML "raise/ Fail/ \"Pls\""
33 and "raise/ Fail/ \"Min\""
34 and "!((_);/ raise/ Fail/ \"Bit0\")"
35 and "!((_);/ raise/ Fail/ \"Bit1\")")
36 (OCaml "failwith/ \"Pls\""
37 and "failwith/ \"Min\""
38 and "!((_);/ failwith/ \"Bit0\")"
39 and "!((_);/ failwith/ \"Bit1\")")
40 (Haskell "error/ \"Pls\""
43 and "error/ \"Bit1\"")
44 (Scala "!error(\"Pls\")"
46 and "!error(\"Bit0\")"
47 and "!error(\"Bit1\")")
50 (SML "IntInf.- ((_), 1)")
51 (OCaml "Big'_int.pred'_big'_int")
52 (Haskell "!(_/ -/ 1)")
57 (SML "IntInf.+ ((_), 1)")
58 (OCaml "Big'_int.succ'_big'_int")
59 (Haskell "!(_/ +/ 1)")
63 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
64 (SML "IntInf.+ ((_), (_))")
65 (OCaml "Big'_int.add'_big'_int")
66 (Haskell infixl 6 "+")
70 code_const "uminus \<Colon> int \<Rightarrow> int"
72 (OCaml "Big'_int.minus'_big'_int")
77 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
78 (SML "IntInf.- ((_), (_))")
79 (OCaml "Big'_int.sub'_big'_int")
80 (Haskell infixl 6 "-")
84 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
85 (SML "IntInf.* ((_), (_))")
86 (OCaml "Big'_int.mult'_big'_int")
87 (Haskell infixl 7 "*")
92 (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
93 (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
94 (Haskell "divMod/ (abs _)/ (abs _)")
95 (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
96 (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
98 code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
99 (SML "!((_ : IntInf.int) = _)")
100 (OCaml "Big'_int.eq'_big'_int")
101 (Haskell infixl 4 "==")
102 (Scala infixl 5 "==")
105 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
106 (SML "IntInf.<= ((_), (_))")
107 (OCaml "Big'_int.le'_big'_int")
108 (Haskell infix 4 "<=")
109 (Scala infixl 4 "<=")
112 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
113 (SML "IntInf.< ((_), (_))")
114 (OCaml "Big'_int.lt'_big'_int")
115 (Haskell infix 4 "<")
119 code_const Code_Numeral.int_of
120 (SML "IntInf.fromInt")
122 (Haskell "toInteger")
123 (Scala "!_.as'_BigInt")
126 text {* Evaluation *}
128 code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
129 (Eval "HOLogic.mk'_number/ HOLogic.intT")