1 (* Title: HOL/Library/Code_Integer.thy
2 Author: Florian Haftmann, TU Muenchen
5 header {* Pretty integer literals for code generation *}
12 HOL numeral expressions are mapped to integer literals
13 in target languages, using predefined target language
14 operations for abstract integer operations.
18 Preliminary: alternative representation of @{typ code_numeral}
19 for @{text Haskell} and @{text Scala}.
22 code_include Haskell "Natural" {*
23 newtype Natural = Natural Integer deriving (Eq, Show, Read);
25 instance Num Natural where {
26 fromInteger k = Natural (if k >= 0 then k else 0);
27 Natural n + Natural m = Natural (n + m);
28 Natural n - Natural m = fromInteger (n - m);
29 Natural n * Natural m = Natural (n * m);
32 negate n = error "negate Natural";
35 instance Ord Natural where {
36 Natural n <= Natural m = n <= m;
37 Natural n < Natural m = n < m;
40 instance Real Natural where {
41 toRational (Natural n) = toRational n;
44 instance Enum Natural where {
45 toEnum k = fromInteger (toEnum k);
46 fromEnum (Natural n) = fromEnum n;
49 instance Integral Natural where {
50 toInteger (Natural n) = n;
51 divMod n m = quotRem n m;
52 quotRem (Natural n) (Natural m)
53 | (m == 0) = (0, Natural n)
54 | otherwise = (Natural k, Natural l) where (k, l) = quotRem n m;
58 code_reserved Haskell Natural
60 code_include Scala "Natural" {*
65 def apply(numeral: BigInt): Natural = new Natural(numeral max 0)
66 def apply(numeral: Int): Natural = Natural(BigInt(numeral))
67 def apply(numeral: String): Natural = Natural(BigInt(numeral))
71 class Natural private(private val value: BigInt) {
73 override def hashCode(): Int = this.value.hashCode()
75 override def equals(that: Any): Boolean = that match {
76 case that: Natural => this equals that
80 override def toString(): String = this.value.toString
82 def equals(that: Natural): Boolean = this.value == that.value
84 def as_BigInt: BigInt = this.value
85 def as_Int: Int = if (this.value >= Int.MinValue && this.value <= Int.MaxValue)
87 else this.value.intValue
89 def +(that: Natural): Natural = new Natural(this.value + that.value)
90 def -(that: Natural): Natural = Natural(this.value - that.value)
91 def *(that: Natural): Natural = new Natural(this.value * that.value)
93 def /%(that: Natural): (Natural, Natural) = if (that.value == 0) (new Natural(0), this)
95 val (k, l) = this.value /% that.value
96 (new Natural(k), new Natural(l))
99 def <=(that: Natural): Boolean = this.value <= that.value
101 def <(that: Natural): Boolean = this.value < that.value
106 code_reserved Scala Natural
108 code_type code_numeral
109 (Haskell "Natural.Natural")
113 fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
114 false Code_Printer.literal_alternative_numeral) ["Haskell", "Scala"]
117 code_instance code_numeral :: eq
120 code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
121 (Haskell infixl 6 "+")
124 code_const "op - \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
125 (Haskell infixl 6 "-")
128 code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
129 (Haskell infixl 7 "*")
132 code_const div_mod_code_numeral
134 (Scala infixl 8 "/%")
136 code_const "eq_class.eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
137 (Haskell infixl 4 "==")
138 (Scala infixl 5 "==")
140 code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
141 (Haskell infix 4 "<=")
142 (Scala infixl 4 "<=")
144 code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
145 (Haskell infix 4 "<")
149 Setup for @{typ int} proper.
155 (OCaml "Big'_int.big'_int")
159 code_instance int :: eq
163 fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
164 true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
167 code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
168 (SML "raise/ Fail/ \"Pls\""
169 and "raise/ Fail/ \"Min\""
170 and "!((_);/ raise/ Fail/ \"Bit0\")"
171 and "!((_);/ raise/ Fail/ \"Bit1\")")
172 (OCaml "failwith/ \"Pls\""
173 and "failwith/ \"Min\""
174 and "!((_);/ failwith/ \"Bit0\")"
175 and "!((_);/ failwith/ \"Bit1\")")
176 (Haskell "error/ \"Pls\""
178 and "error/ \"Bit0\""
179 and "error/ \"Bit1\"")
180 (Scala "!error(\"Pls\")"
181 and "!error(\"Min\")"
182 and "!error(\"Bit0\")"
183 and "!error(\"Bit1\")")
186 (SML "IntInf.- ((_), 1)")
187 (OCaml "Big'_int.pred'_big'_int")
188 (Haskell "!(_/ -/ 1)")
193 (SML "IntInf.+ ((_), 1)")
194 (OCaml "Big'_int.succ'_big'_int")
195 (Haskell "!(_/ +/ 1)")
199 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
200 (SML "IntInf.+ ((_), (_))")
201 (OCaml "Big'_int.add'_big'_int")
202 (Haskell infixl 6 "+")
206 code_const "uminus \<Colon> int \<Rightarrow> int"
208 (OCaml "Big'_int.minus'_big'_int")
213 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
214 (SML "IntInf.- ((_), (_))")
215 (OCaml "Big'_int.sub'_big'_int")
216 (Haskell infixl 6 "-")
220 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
221 (SML "IntInf.* ((_), (_))")
222 (OCaml "Big'_int.mult'_big'_int")
223 (Haskell infixl 7 "*")
228 (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
229 (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
230 (Haskell "divMod/ (abs _)/ (abs _)")
231 (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
232 (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
234 code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
235 (SML "!((_ : IntInf.int) = _)")
236 (OCaml "Big'_int.eq'_big'_int")
237 (Haskell infixl 4 "==")
238 (Scala infixl 5 "==")
241 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
242 (SML "IntInf.<= ((_), (_))")
243 (OCaml "Big'_int.le'_big'_int")
244 (Haskell infix 4 "<=")
245 (Scala infixl 4 "<=")
248 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
249 (SML "IntInf.< ((_), (_))")
250 (OCaml "Big'_int.lt'_big'_int")
251 (Haskell infix 4 "<")
255 code_const Code_Numeral.int_of
256 (SML "IntInf.fromInt")
258 (Haskell "toInteger")
259 (Scala "!_.as'_BigInt")
262 text {* Evaluation *}
264 code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
265 (Eval "HOLogic.mk'_number/ HOLogic.intT")