Repository navigation
Expand file tree
/
Copy pathRust_Checked128_Setup.thy
More file actions
441 lines (391 loc) · 14.8 KB
/
Copy pathRust_Checked128_Setup.thy
File metadata and controls
441 lines (391 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
theory Rust_Checked128_Setup
imports
Rust_Base_Setup
"HOL-Library.Code_Target_Int"
"HOL-Library.Code_Target_Nat"
begin
text \<open>
Bounded numeric profile: integer and int map directly to i128, while nat maps
directly to u128. Values are Copy. Operations that would leave the selected
primitive range fail explicitly instead of wrapping; natural subtraction
retains Isabelle's saturation at zero.
\<close>
definition rust_checked_integer_power :: "integer \<Rightarrow> nat \<Rightarrow> integer" where
"rust_checked_integer_power x n = x ^ n"
definition rust_checked_nat_power :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
"rust_checked_nat_power x n = x ^ n"
lemma [code_unfold]: "(x :: integer) ^ n = rust_checked_integer_power x n"
by (simp add: rust_checked_integer_power_def)
lemma [code_unfold]: "(x :: nat) ^ n = rust_checked_nat_power x n"
by (simp add: rust_checked_nat_power_def)
code_identifier
code_module Int \<rightharpoonup> (Rust) Arith
| code_module Code_Target_Int \<rightharpoonup> (Rust) Arith
| code_module Code_Target_Nat \<rightharpoonup> (Rust) Arith
code_printing code_module Rust_Checked128 \<rightharpoonup> (Rust) \<open>
#[cold]
#[inline(never)]
fn overflow(operation: &str) -> ! {
panic!("Isabelle numeric value exceeds Checked128 during {operation}")
}
#[inline(always)]
pub fn add_int(a: i128, b: i128) -> i128 {
a.checked_add(b).unwrap_or_else(|| overflow("integer addition"))
}
#[inline(always)]
pub fn sub_int(a: i128, b: i128) -> i128 {
a.checked_sub(b).unwrap_or_else(|| overflow("integer subtraction"))
}
#[inline(always)]
pub fn mul_int(a: i128, b: i128) -> i128 {
a.checked_mul(b).unwrap_or_else(|| overflow("integer multiplication"))
}
#[inline(always)]
pub fn neg_int(value: i128) -> i128 {
value.checked_neg().unwrap_or_else(|| overflow("integer negation"))
}
#[inline(always)]
pub fn abs_int(value: i128) -> i128 {
value.checked_abs().unwrap_or_else(|| overflow("integer absolute value"))
}
#[inline]
pub fn div_mod_floor(dividend: i128, divisor: i128) -> (i128, i128) {
if divisor == 0 {
return (0, dividend);
}
if dividend == i128::MIN && divisor == -1 {
overflow("integer division");
}
let mut quotient = dividend / divisor;
let mut remainder = dividend % divisor;
if remainder != 0 && ((remainder < 0) != (divisor < 0)) {
quotient = quotient.checked_sub(1).unwrap_or_else(|| overflow("integer division"));
remainder = remainder
.checked_add(divisor)
.unwrap_or_else(|| overflow("integer remainder"));
}
(quotient, remainder)
}
#[inline(always)]
pub fn div_int(a: i128, b: i128) -> i128 {
div_mod_floor(a, b).0
}
#[inline(always)]
pub fn rem_int(a: i128, b: i128) -> i128 {
div_mod_floor(a, b).1
}
#[inline(always)]
pub fn duplicate(value: i128) -> i128 {
add_int(value, value)
}
#[inline]
pub fn divmod_abs(a: i128, b: i128) -> (i128, i128) {
let a = abs_int(a);
let b = abs_int(b);
if b == 0 { (0, a) } else { (a / b, a % b) }
}
#[inline(always)]
pub fn from_int(value: i128) -> u128 {
if value <= 0 { 0 } else { value as u128 }
}
#[inline(always)]
pub fn into_int(value: u128) -> i128 {
i128::try_from(value).unwrap_or_else(|_| overflow("nat-to-integer conversion"))
}
#[inline(always)]
pub fn add_nat(a: u128, b: u128) -> u128 {
a.checked_add(b).unwrap_or_else(|| overflow("natural addition"))
}
#[inline(always)]
pub fn sub_nat(a: u128, b: u128) -> u128 {
a.saturating_sub(b)
}
#[inline(always)]
pub fn mul_nat(a: u128, b: u128) -> u128 {
a.checked_mul(b).unwrap_or_else(|| overflow("natural multiplication"))
}
#[inline(always)]
pub fn div_nat(a: u128, b: u128) -> u128 {
if b == 0 { 0 } else { a / b }
}
#[inline(always)]
pub fn rem_nat(a: u128, b: u128) -> u128 {
if b == 0 { a } else { a % b }
}
#[inline]
pub fn pow_int(mut base: i128, mut exponent: u128) -> i128 {
let mut result = 1i128;
while exponent != 0 {
if exponent & 1 == 1 {
result = mul_int(result, base);
}
exponent >>= 1;
if exponent != 0 {
base = mul_int(base, base);
}
}
result
}
#[inline]
pub fn pow_nat(mut base: u128, mut exponent: u128) -> u128 {
let mut result = 1u128;
while exponent != 0 {
if exponent & 1 == 1 {
result = mul_nat(result, base);
}
exponent >>= 1;
if exponent != 0 {
base = mul_nat(base, base);
}
}
result
}
#[inline]
pub fn push_bit_int(amount: u128, value: i128) -> i128 {
match amount {
0 => value,
1..=126 => value
.checked_mul(1i128 << amount as u32)
.unwrap_or_else(|| overflow("integer left shift")),
127 if value == -1 => i128::MIN,
127 if value == 0 => 0,
_ if value == 0 => 0,
_ => overflow("integer left shift"),
}
}
#[inline(always)]
pub fn drop_bit_int(amount: u128, value: i128) -> i128 {
if amount >= 128 {
if value < 0 { -1 } else { 0 }
} else {
value >> amount as u32
}
}
#[inline]
pub fn take_bit_int(amount: u128, value: i128) -> i128 {
if amount >= 128 {
if value >= 0 { value } else { overflow("integer take_bit") }
} else if amount == 0 {
0
} else {
value & ((1i128 << amount as u32) - 1)
}
}
#[inline]
pub fn mask_int(amount: u128) -> i128 {
if amount > 127 {
overflow("integer mask")
} else if amount == 0 {
0
} else {
(1i128 << amount as u32) - 1
}
}
#[inline]
pub fn set_bit_int(amount: u128, value: i128) -> i128 {
if amount < 127 {
value | (1i128 << amount as u32)
} else if value < 0 {
value
} else {
overflow("integer set_bit")
}
}
#[inline]
pub fn unset_bit_int(amount: u128, value: i128) -> i128 {
if amount < 127 {
value & !(1i128 << amount as u32)
} else if value >= 0 {
value
} else {
overflow("integer unset_bit")
}
}
#[inline]
pub fn flip_bit_int(amount: u128, value: i128) -> i128 {
if amount < 127 {
value ^ (1i128 << amount as u32)
} else {
let _ = value;
overflow("integer flip_bit")
}
}
#[inline(always)]
pub fn bit_int(value: i128, amount: u128) -> bool {
if amount >= 128 { value < 0 } else { ((value >> amount as u32) & 1) == 1 }
}
#[inline]
pub fn push_bit_nat(amount: u128, value: u128) -> u128 {
if amount >= 128 {
if value == 0 { 0 } else { overflow("natural left shift") }
} else {
value
.checked_mul(1u128 << amount as u32)
.unwrap_or_else(|| overflow("natural left shift"))
}
}
#[inline(always)]
pub fn drop_bit_nat(amount: u128, value: u128) -> u128 {
if amount >= 128 { 0 } else { value >> amount as u32 }
}
#[inline(always)]
pub fn take_bit_nat(amount: u128, value: u128) -> u128 {
if amount >= 128 {
value
} else if amount == 0 {
0
} else {
value & ((1u128 << amount as u32) - 1)
}
}
#[inline]
pub fn mask_nat(amount: u128) -> u128 {
if amount > 128 {
overflow("natural mask")
} else if amount == 128 {
u128::MAX
} else if amount == 0 {
0
} else {
(1u128 << amount as u32) - 1
}
}
#[inline]
pub fn set_bit_nat(amount: u128, value: u128) -> u128 {
if amount >= 128 { overflow("natural set_bit") } else { value | (1u128 << amount as u32) }
}
#[inline(always)]
pub fn unset_bit_nat(amount: u128, value: u128) -> u128 {
if amount >= 128 { value } else { value & !(1u128 << amount as u32) }
}
#[inline]
pub fn flip_bit_nat(amount: u128, value: u128) -> u128 {
if amount >= 128 { overflow("natural flip_bit") } else { value ^ (1u128 << amount as u32) }
}
#[inline(always)]
pub fn bit_nat(value: u128, amount: u128) -> bool {
amount < 128 && ((value >> amount as u32) & 1) == 1
}
\<close>
code_reserved (Rust) Rust_Checked128
code_printing
type_constructor "integer" \<rightharpoonup> (Rust) "i128"
| type_constructor int \<rightharpoonup> (Rust) "i128"
| type_constructor nat \<rightharpoonup> (Rust) "u128"
code_printing
constant "0 :: integer" \<rightharpoonup> (Rust) "0i128"
| constant int_of_integer \<rightharpoonup> (Rust) "_"
| constant integer_of_int \<rightharpoonup> (Rust) "_"
| constant Code_Target_Nat.Nat \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::from'_int'(_')"
| constant Code_Numeral.nat_of_integer \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::from'_int'(_')"
| constant integer_of_nat \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::into'_int'(_')"
| constant "0 :: nat" \<rightharpoonup> (Rust) "0u128"
setup \<open>
let
val min_i128 = ~ (Integer.pow 127 2)
val max_i128 = Integer.pow 127 2 - 1
fun print_checked_int _ k =
if k < min_i128 orelse max_i128 < k then
error ("Rust_Checked128_Setup: integer literal outside i128: " ^ Int.toString k)
else if k = min_i128 then
"i128::MIN"
else if k < 0 then
"(-" ^ Int.toString (~ k) ^ "i128)"
else
Int.toString k ^ "i128"
in
Numeral.add_code \<^const_name>\<open>Code_Numeral.Pos\<close> I print_checked_int "Rust"
#> Numeral.add_code \<^const_name>\<open>Code_Numeral.Neg\<close> (~) print_checked_int "Rust"
end
\<close>
code_printing
constant "plus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::add'_int'(_, _')"
| constant "uminus :: integer \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::neg'_int'(_')"
| constant "minus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::sub'_int'(_, _')"
| constant Code_Numeral.dup \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::duplicate'(_')"
| constant Code_Numeral.sub \<rightharpoonup> (Rust) "panic'!(\"sub\")"
| constant "times :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::mul'_int'(_, _')"
| constant "divide :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::div'_int'(_, _')"
| constant "modulo :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::rem'_int'(_, _')"
| constant Code_Numeral.divmod_abs \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::divmod'_abs'(_, _')"
| constant "HOL.equal :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
(Rust) infix 4 "=="
| constant "less_eq :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
(Rust) infix 4 "<="
| constant "less :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
(Rust) infix 4 "<"
| constant "abs :: integer \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::abs'_int'(_')"
| constant "Bit_Operations.and :: integer \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) infixl 7 "&"
| constant "Bit_Operations.or :: integer \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) infixl 5 "|"
| constant "Bit_Operations.xor :: integer \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) infixl 6 "^"
| constant "Bit_Operations.not :: integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) "'! _"
code_printing
constant "plus :: nat \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::add'_nat'(_, _')"
| constant "minus :: nat \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::sub'_nat'(_, _')"
| constant "times :: nat \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::mul'_nat'(_, _')"
| constant "divide :: nat \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::div'_nat'(_, _')"
| constant "modulo :: nat \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::rem'_nat'(_, _')"
| constant "HOL.equal :: nat \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
(Rust) infix 4 "=="
| constant "less_eq :: nat \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
(Rust) infix 4 "<="
| constant "less :: nat \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
(Rust) infix 4 "<"
| constant rust_checked_integer_power \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::pow'_int'(_, _')"
| constant rust_checked_nat_power \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::pow'_nat'(_, _')"
| constant "Bit_Operations.push_bit :: nat \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::push'_bit'_int'(_, _')"
| constant "Bit_Operations.drop_bit :: nat \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::drop'_bit'_int'(_, _')"
| constant "Bit_Operations.take_bit :: nat \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::take'_bit'_int'(_, _')"
| constant "Bit_Operations.mask :: nat \<Rightarrow> integer" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::mask'_int'(_')"
| constant "Bit_Operations.set_bit :: nat \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::set'_bit'_int'(_, _')"
| constant "Bit_Operations.unset_bit :: nat \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::unset'_bit'_int'(_, _')"
| constant "Bit_Operations.flip_bit :: nat \<Rightarrow> integer \<Rightarrow> integer" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::flip'_bit'_int'(_, _')"
| constant "Bit_Operations.bit :: integer \<Rightarrow> nat \<Rightarrow> bool" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::bit'_int'(_, _')"
| constant "Bit_Operations.push_bit :: nat \<Rightarrow> nat \<Rightarrow> nat" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::push'_bit'_nat'(_, _')"
| constant "Bit_Operations.drop_bit :: nat \<Rightarrow> nat \<Rightarrow> nat" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::drop'_bit'_nat'(_, _')"
| constant "Bit_Operations.take_bit :: nat \<Rightarrow> nat \<Rightarrow> nat" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::take'_bit'_nat'(_, _')"
| constant "Bit_Operations.mask :: nat \<Rightarrow> nat" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::mask'_nat'(_')"
| constant "Bit_Operations.set_bit :: nat \<Rightarrow> nat \<Rightarrow> nat" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::set'_bit'_nat'(_, _')"
| constant "Bit_Operations.unset_bit :: nat \<Rightarrow> nat \<Rightarrow> nat" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::unset'_bit'_nat'(_, _')"
| constant "Bit_Operations.flip_bit :: nat \<Rightarrow> nat \<Rightarrow> nat" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::flip'_bit'_nat'(_, _')"
| constant "Bit_Operations.bit :: nat \<Rightarrow> nat \<Rightarrow> bool" \<rightharpoonup>
(Rust) "!crate::Rust'_Checked128::bit'_nat'(_, _')"
end