dive_value/ctx.rs
1//! Per-run value semantics: integer mode and value order.
2
3/// How integers behave.
4///
5/// Arbitrary precision is dive's default. TLC's 32-bit integers, with its overflow
6/// error, are always available and selected by `--int=tlc` (part of `--compat=tlc`).
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
8pub enum IntMode {
9 /// TLC's semantics: values are Java `int`s, arithmetic that leaves
10 /// `[-2^31, 2^31 - 1]` is `EC.TLC_MODULE_OVERFLOW`, and literals above `2^31 - 1`
11 /// are `EC.TLC_INTEGER_TOO_BIG`.
12 Tlc,
13 /// Mathematical integers: an inline 64-bit fast path that spills to a big integer.
14 #[default]
15 Big,
16}
17
18/// How names (strings, model values and record field names) are ordered. The order of
19/// every other value is built from it, so this decides set order, printing order and
20/// which element `CHOOSE` picks.
21///
22/// TLC orders names by their position in its global intern table
23/// (`util.UniqueString.compareTo` compares intern tokens), not by their characters.
24/// That order depends on the order in which SANY and TLC first met each name: adding
25/// an unused definition `U == "alpha"` can change `CHOOSE e \in {"zeta", "alpha"} : TRUE`.
26/// dive's default is a canonical, text-independent order; TLC's order is
27/// `--choose-order=tlc`, included in `--compat=tlc`.
28#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
29pub enum ValueOrder {
30 /// dive's canonical order (the default): names by the lexicographic order of their
31 /// UTF-8 bytes, which is the order of their Unicode scalar values. Total, stable,
32 /// and independent of module text and evaluation order.
33 #[default]
34 Canonical,
35 /// TLC's order (`--choose-order=tlc`): names by intern token. Exact when the
36 /// [`Interner`](crate::Interner) is fed names in SANY/TLC's encounter order.
37 Tlc,
38}
39
40/// The value semantics of one run. Cheap to copy; pass it to every operation that may
41/// compare, normalize, enumerate or print values.
42///
43/// All values compared within a run must have been built under the same `Ctx` and
44/// the same [`Interner`](crate::Interner): sets are kept sorted in the run's order.
45#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
46pub struct Ctx {
47 /// The integer semantics.
48 pub int_mode: IntMode,
49 /// The order of names, and so of all values.
50 pub order: ValueOrder,
51}
52
53impl Ctx {
54 /// The `--compat=tlc` semantics: 32-bit integers and TLC's intern order.
55 #[must_use]
56 pub const fn tlc() -> Self {
57 Self {
58 int_mode: IntMode::Tlc,
59 order: ValueOrder::Tlc,
60 }
61 }
62
63 /// dive's default semantics: big integers and the canonical order.
64 #[must_use]
65 pub const fn dive() -> Self {
66 Self {
67 int_mode: IntMode::Big,
68 order: ValueOrder::Canonical,
69 }
70 }
71}