leanfran-runtime 0.7.0

Runtime of the Rust code generated from Lean by leanfran
Documentation
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
//! `Int`: scalars in the range of `i32` on 64-bit machines and of 31-bit integers on 32-bit
//! machines (as in `lean.h`), `num-bigint` values in mpz objects outside it.

use crate::nat::{lean_alloc_mpz, lean_mpz_value, mk_nat, nat_value};
use crate::object::*;
use crate::panic::lean_internal_panic;
use num_bigint::BigInt;
use num_traits::{Signed, ToPrimitive, Zero};

#[cfg(target_pointer_width = "64")]
pub const LEAN_MAX_SMALL_INT: i64 = i32::MAX as i64;
#[cfg(target_pointer_width = "64")]
pub const LEAN_MIN_SMALL_INT: i64 = i32::MIN as i64;
#[cfg(not(target_pointer_width = "64"))]
pub const LEAN_MAX_SMALL_INT: i64 = (1 << 30) - 1;
#[cfg(not(target_pointer_width = "64"))]
pub const LEAN_MIN_SMALL_INT: i64 = -(1 << 30);

/// The scalar `Int` of `n`, in the range of the small integers.
#[inline]
fn box_small_int(n: i32) -> Obj {
    #[cfg(target_pointer_width = "64")]
    {
        lean_box(n as u32 as usize)
    }
    #[cfg(not(target_pointer_width = "64"))]
    {
        lean_box(n as usize)
    }
}

/// The value of a scalar `Int`.
#[inline]
fn unbox_small_int(a: Obj) -> i32 {
    #[cfg(target_pointer_width = "64")]
    {
        lean_unbox(a) as u32 as i32
    }
    #[cfg(not(target_pointer_width = "64"))]
    {
        (a.addr() as i32) >> 1
    }
}

/// The value of an `Int`.
#[inline]
pub(crate) unsafe fn int_value(o: Obj) -> BigInt {
    if lean_is_scalar(o) {
        BigInt::from(lean_scalar_to_int64(o))
    } else {
        lean_mpz_value(o).clone()
    }
}

/// An `Int` object for `v`.
pub(crate) unsafe fn mk_int(v: BigInt) -> Obj {
    match v.to_i64() {
        Some(n) if (LEAN_MIN_SMALL_INT..=LEAN_MAX_SMALL_INT).contains(&n) => {
            box_small_int(n as i32)
        }
        _ => lean_alloc_mpz(v),
    }
}

#[inline]
pub unsafe fn lean_scalar_to_int64(a: Obj) -> i64 {
    unbox_small_int(a) as i64
}

#[inline]
pub unsafe fn lean_scalar_to_int(a: Obj) -> i32 {
    unbox_small_int(a)
}

#[inline]
pub unsafe fn lean_int_to_int(n: i32) -> Obj {
    lean_int64_to_int(n as i64)
}

#[inline]
pub unsafe fn lean_int64_to_int(n: i64) -> Obj {
    if (LEAN_MIN_SMALL_INT..=LEAN_MAX_SMALL_INT).contains(&n) {
        box_small_int(n as i32)
    } else {
        lean_big_int64_to_int(n)
    }
}

pub unsafe fn lean_big_int_to_int(n: i32) -> Obj {
    lean_alloc_mpz(BigInt::from(n))
}

pub unsafe fn lean_big_size_t_to_int(n: usize) -> Obj {
    lean_alloc_mpz(BigInt::from(n))
}

pub unsafe fn lean_big_int64_to_int(n: i64) -> Obj {
    lean_alloc_mpz(BigInt::from(n))
}

/// An `Int` from its decimal digits (with an optional `-`).
pub unsafe fn lean_cstr_to_int(s: &str) -> Obj {
    match s.parse::<BigInt>() {
        Ok(v) => mk_int(v),
        Err(_) => lean_internal_panic("invalid integer literal"),
    }
}

/// `Int.ofNat` (consumes `a`).
#[inline]
pub unsafe fn lean_nat_to_int(a: Obj) -> Obj {
    if lean_is_scalar(a) {
        let v = lean_unbox(a);
        if v <= LEAN_MAX_SMALL_INT as usize {
            a
        } else {
            lean_big_size_t_to_int(v)
        }
    } else {
        a
    }
}

/// `Int.negSucc` (consumes `a`).
pub unsafe fn lean_int_neg_succ_of_nat(a: Obj) -> Obj {
    let v = nat_value(a);
    lean_dec(a);
    let n: BigInt = v + 1u32;
    mk_int(-n)
}

/// `Int.toNat` of a non-negative `Int` (consumes `a`).
pub unsafe fn lean_int_to_nat(a: Obj) -> Obj {
    if lean_is_scalar(a) {
        a
    } else {
        lean_big_int_to_nat(a)
    }
}

/// Consumes `a`.
pub unsafe fn lean_big_int_to_nat(a: Obj) -> Obj {
    let v = int_value(a);
    lean_dec(a);
    mk_nat(v)
}

#[inline]
pub unsafe fn lean_nat_abs(i: Obj) -> Obj {
    mk_nat(int_value(i).abs())
}

/* Arithmetic */

macro_rules! int_binop {
    ($name:ident, $big:ident, |$x:ident, $y:ident| $small:expr, |$bx:ident, $by:ident| $bigexpr:expr) => {
        #[inline]
        pub unsafe fn $name(a1: Obj, a2: Obj) -> Obj {
            if lean_is_scalar(a1) && lean_is_scalar(a2) {
                let $x = lean_scalar_to_int64(a1);
                let $y = lean_scalar_to_int64(a2);
                $small
            } else {
                $big(a1, a2)
            }
        }

        pub unsafe fn $big(a1: Obj, a2: Obj) -> Obj {
            let $bx = int_value(a1);
            let $by = int_value(a2);
            $bigexpr
        }
    };
}

int_binop!(
    lean_int_add,
    lean_int_big_add,
    |x, y| lean_int64_to_int(x + y),
    |x, y| mk_int(x + y)
);
int_binop!(
    lean_int_sub,
    lean_int_big_sub,
    |x, y| lean_int64_to_int(x - y),
    |x, y| mk_int(x - y)
);
int_binop!(
    lean_int_mul,
    lean_int_big_mul,
    |x, y| lean_int64_to_int(x * y),
    |x, y| mk_int(x * y)
);
int_binop!(
    lean_int_div,
    lean_int_big_div,
    |x, y| if y == 0 {
        lean_box(0)
    } else {
        lean_int64_to_int(x / y)
    },
    |x, y| if y.is_zero() {
        lean_box(0)
    } else {
        mk_int(x / y)
    }
);
int_binop!(
    lean_int_mod,
    lean_int_big_mod,
    |x, y| if y == 0 {
        lean_int64_to_int(x)
    } else {
        lean_int64_to_int(x % y)
    },
    |x, y| if y.is_zero() {
        mk_int(x)
    } else {
        mk_int(x % y)
    }
);
int_binop!(
    lean_int_ediv,
    lean_int_big_ediv,
    |x, y| if y == 0 {
        lean_box(0)
    } else {
        lean_int64_to_int(ediv(x, y))
    },
    |x, y| if y.is_zero() {
        lean_box(0)
    } else {
        mk_int(big_ediv(&x, &y))
    }
);
int_binop!(
    lean_int_emod,
    lean_int_big_emod,
    |x, y| if y == 0 {
        lean_int64_to_int(x)
    } else {
        lean_int64_to_int(emod(x, y))
    },
    |x, y| if y.is_zero() {
        mk_int(x)
    } else {
        mk_int(big_emod(&x, &y))
    }
);

#[inline]
pub unsafe fn lean_int_div_exact(a1: Obj, a2: Obj) -> Obj {
    lean_int_div(a1, a2)
}

pub unsafe fn lean_int_big_div_exact(a1: Obj, a2: Obj) -> Obj {
    lean_int_big_div(a1, a2)
}

fn ediv(n: i64, d: i64) -> i64 {
    let q = n / d;
    if n % d < 0 {
        if d > 0 {
            q - 1
        } else {
            q + 1
        }
    } else {
        q
    }
}

fn emod(n: i64, d: i64) -> i64 {
    let r = n % d;
    if r < 0 {
        if d > 0 {
            r + d
        } else {
            r - d
        }
    } else {
        r
    }
}

fn big_ediv(n: &BigInt, d: &BigInt) -> BigInt {
    let q = n / d;
    if (n % d).is_negative() {
        if d.is_positive() {
            q - 1
        } else {
            q + 1
        }
    } else {
        q
    }
}

fn big_emod(n: &BigInt, d: &BigInt) -> BigInt {
    let r = n % d;
    if r.is_negative() {
        if d.is_positive() {
            r + d
        } else {
            r - d
        }
    } else {
        r
    }
}

#[inline]
pub unsafe fn lean_int_neg(a: Obj) -> Obj {
    if lean_is_scalar(a) {
        lean_int64_to_int(-lean_scalar_to_int64(a))
    } else {
        lean_int_big_neg(a)
    }
}

pub unsafe fn lean_int_big_neg(a: Obj) -> Obj {
    mk_int(-int_value(a))
}

/* Comparisons */

#[inline]
pub unsafe fn lean_int_eq(a1: Obj, a2: Obj) -> bool {
    if lean_is_scalar(a1) && lean_is_scalar(a2) {
        a1 == a2
    } else {
        lean_int_big_eq(a1, a2)
    }
}

pub unsafe fn lean_int_big_eq(a1: Obj, a2: Obj) -> bool {
    int_value(a1) == int_value(a2)
}

#[inline]
pub unsafe fn lean_int_ne(a1: Obj, a2: Obj) -> bool {
    !lean_int_eq(a1, a2)
}

#[inline]
pub unsafe fn lean_int_le(a1: Obj, a2: Obj) -> bool {
    if lean_is_scalar(a1) && lean_is_scalar(a2) {
        lean_scalar_to_int(a1) <= lean_scalar_to_int(a2)
    } else {
        lean_int_big_le(a1, a2)
    }
}

pub unsafe fn lean_int_big_le(a1: Obj, a2: Obj) -> bool {
    int_value(a1) <= int_value(a2)
}

#[inline]
pub unsafe fn lean_int_lt(a1: Obj, a2: Obj) -> bool {
    if lean_is_scalar(a1) && lean_is_scalar(a2) {
        lean_scalar_to_int(a1) < lean_scalar_to_int(a2)
    } else {
        lean_int_big_lt(a1, a2)
    }
}

pub unsafe fn lean_int_big_lt(a1: Obj, a2: Obj) -> bool {
    int_value(a1) < int_value(a2)
}

pub unsafe fn lean_int_big_nonneg(a: Obj) -> bool {
    !lean_mpz_value(a).is_negative()
}

#[inline]
pub unsafe fn lean_int_dec_eq(a1: Obj, a2: Obj) -> u8 {
    lean_int_eq(a1, a2) as u8
}

#[inline]
pub unsafe fn lean_int_dec_le(a1: Obj, a2: Obj) -> u8 {
    lean_int_le(a1, a2) as u8
}

#[inline]
pub unsafe fn lean_int_dec_lt(a1: Obj, a2: Obj) -> u8 {
    lean_int_lt(a1, a2) as u8
}

#[inline]
pub unsafe fn lean_int_dec_nonneg(a: Obj) -> u8 {
    if lean_is_scalar(a) {
        (lean_scalar_to_int(a) >= 0) as u8
    } else {
        lean_int_big_nonneg(a) as u8
    }
}

/* Bitwise operations on `Int` (two's complement semantics) */

pub unsafe fn lean_int_land(a1: Obj, a2: Obj) -> Obj {
    mk_int(int_value(a1) & int_value(a2))
}

pub unsafe fn lean_int_lor(a1: Obj, a2: Obj) -> Obj {
    mk_int(int_value(a1) | int_value(a2))
}

pub unsafe fn lean_int_xor(a1: Obj, a2: Obj) -> Obj {
    mk_int(int_value(a1) ^ int_value(a2))
}

pub unsafe fn lean_int_lnot(a: Obj) -> Obj {
    mk_int(!int_value(a))
}

/// `Int.shiftRight` by a `Nat` (arithmetic shift, rounding towards negative infinity).
pub unsafe fn lean_int_shift_right(a1: Obj, a2: Obj) -> Obj {
    match crate::nat::nat_to_usize(a2) {
        Some(s) => mk_int(int_value(a1) >> s),
        None => mk_int(if int_value(a1).is_negative() {
            BigInt::from(-1)
        } else {
            BigInt::zero()
        }),
    }
}