Skip to main content

logicaffeine_runtime/
payload.rs

1//! `RtPayload` — the `Send`-able value subset that crosses task (and OS-thread)
2//! boundaries via channels.
3//!
4//! The interpreter and VM heaps are `Rc`-based (`!Send`). A value moved through a
5//! channel is *materialized* into this owned, allocation-self-contained form on
6//! the sending side and *rebuilt* into the receiver's heap on the other side.
7//! The marshalling between `RuntimeValue` / the VM `Value` and `RtPayload` lives
8//! in `logicaffeine-compile` (which knows those representations); this crate only
9//! defines the wire shape and guarantees it is `Send`, which is what makes the
10//! M:N work-stealing driver sound.
11
12/// A self-contained, `Send` value that can move between tasks and threads.
13///
14/// This deliberately excludes `Rc`/`RefCell`-backed and closure values: those
15/// either are not `Send` or would alias another task's heap. CRDT shared cells
16/// (which are `Arc`-backed and shared rather than moved) get their own variant
17/// when the scheduler grows them in a later phase.
18#[derive(Debug, Clone, PartialEq)]
19pub enum RtPayload {
20    /// The unit / absence value.
21    Nothing,
22    Int(i64),
23    /// An exact integer that does not fit `i64`, carried as its sign + little-endian
24    /// magnitude bytes — a dependency-free, `Send` form of `BigInt` (reconstructed
25    /// with `BigInt::from_le_bytes` on the far side).
26    BigInt {
27        negative: bool,
28        magnitude: Vec<u8>,
29    },
30    /// An exact rational: the signed numerator and the (always positive) denominator,
31    /// each as little-endian magnitude bytes — a dependency-free, `Send` form of
32    /// `Rational` (reconstructed with `Rational::new` on the far side). `1/3` survives
33    /// here exactly, where a JSON `0.333…` would round.
34    Rational {
35        num_negative: bool,
36        num_magnitude: Vec<u8>,
37        den_magnitude: Vec<u8>,
38    },
39    /// An exact base-10 fixed-point number (money): its signed coefficient as
40    /// little-endian magnitude bytes plus the base-10 `scale` (decimal places) — a
41    /// dependency-free, `Send` form of `Decimal` (reconstructed with
42    /// `Decimal::from_le_bytes` on the far side). `19.99` survives exactly, scale and all.
43    Decimal {
44        negative: bool,
45        magnitude: Vec<u8>,
46        scale: u32,
47    },
48    /// An exact monetary amount: its `Decimal` amount (sign + LE magnitude + base-10 scale) plus the
49    /// ISO-4217 currency code. Reconstructed as `Money { Decimal::from_le_bytes, currency::by_code }`.
50    Money {
51        negative: bool,
52        magnitude: Vec<u8>,
53        scale: u32,
54        currency: String,
55    },
56    /// A 128-bit UUID — its 16 big-endian bytes verbatim. Reconstructed with `Uuid::from_bytes`.
57    Uuid([u8; 16]),
58    /// An exact complex number `re + im·i`: each part a rational (signed numerator + a
59    /// positive denominator) as little-endian magnitude bytes. Reconstructed with
60    /// `Complex::new` on the far side; `i·i = −1` survives exactly.
61    Complex {
62        re_negative: bool,
63        re_num: Vec<u8>,
64        re_den: Vec<u8>,
65        im_negative: bool,
66        im_num: Vec<u8>,
67        im_den: Vec<u8>,
68    },
69    /// A floating-point complex (LAPACK `Z`): the real and imaginary parts as plain `f64`s. The
70    /// inexact dual of `Complex` — it is what the sqrt-based complex decompositions run on.
71    ComplexF {
72        re: f64,
73        im: f64,
74    },
75    /// An element of ℤ/nℤ: its (non-negative) residue and modulus as little-endian magnitude
76    /// bytes. Reconstructed with `Modular::new` on the far side; the ring is preserved.
77    Modular {
78        value: Vec<u8>,
79        modulus: Vec<u8>,
80    },
81    /// A dimensioned physical quantity: its SI-base magnitude as an exact rational (signed
82    /// numerator + positive denominator, little-endian bytes), its dimension as the exponent
83    /// vector (`dim_num[i]/dim_den[i]` over the base axes), and the display unit's symbol. The far
84    /// side rebuilds the magnitude with `Rational`, the dimension with `Dimension::from_exps`, and
85    /// resolves the unit by symbol (falling back to the SI/dimension display for compound units).
86    Quantity {
87        num_negative: bool,
88        num_magnitude: Vec<u8>,
89        den_magnitude: Vec<u8>,
90        dim_num: Vec<i32>,
91        dim_den: Vec<i32>,
92        unit_symbol: String,
93    },
94    Float(f64),
95    Bool(bool),
96    Char(char),
97    /// An owned string (not an `Rc<str>`).
98    Text(String),
99    /// A fully-materialized sequence.
100    List(Vec<RtPayload>),
101    /// A fixed heterogeneous tuple.
102    Tuple(Vec<RtPayload>),
103    /// A set, materialized as its elements.
104    Set(Vec<RtPayload>),
105    /// A map, materialized as key/value pairs.
106    Map(Vec<(RtPayload, RtPayload)>),
107    /// A struct instance: its type name and named fields.
108    Struct {
109        type_name: String,
110        fields: Vec<(String, RtPayload)>,
111    },
112    /// An inductive (sum-type) value: its type, constructor, and arguments.
113    Inductive {
114        type_name: String,
115        constructor: String,
116        args: Vec<RtPayload>,
117    },
118    /// A duration, carried in its base unit.
119    Duration(i64),
120    /// A calendar date.
121    Date(i32),
122    /// A moment in time.
123    Moment(i64),
124    /// A calendar span (months + days).
125    Span {
126        months: i32,
127        days: i32,
128    },
129    /// A time-of-day.
130    Time(i64),
131    /// A fixed-width wrapping integer (`Word32`/`Word64`): its bit width (32 or 64) and the
132    /// value zero-extended to `u64`. Reconstructed via `WordVal::from_u64` on the far side.
133    Word {
134        width: u32,
135        bits: u64,
136    },
137    /// A channel handle (a `Pipe`) — an opaque scheduler token. `Send` (just an
138    /// id), so a channel can be passed as a spawn argument across worker threads;
139    /// the receiving task resolves it against the one shared scheduler.
140    Chan(crate::channel::ChanId),
141    /// A spawned-task handle — likewise an opaque `Send` scheduler token.
142    TaskHandle(crate::task::TaskId),
143    /// A remote-peer handle — its canonical relay topic. A `String` is trivially
144    /// `Send`, so a peer can be passed as a spawn argument across worker threads.
145    Peer(String),
146    /// A CRDT value carried as its KIND-TAGGED full convergent STATE (the bytes
147    /// `CrdtValue::delta_since_bytes(&VClock::new())` produces — a valid state-based delta). Unlike
148    /// the other variants, a CRDT does NOT move its `Rc`/`Arc` handle across the boundary (that would
149    /// alias one replica's live cell); it ships its STATE, and the far side rebuilds a fresh replica
150    /// and merges. State-based ⇒ re-applying the same bytes is IDEMPOTENT (per-replica MAX), so a
151    /// redelivered payload never double-counts — the property `Sync`/`Merge` over a lossy relay needs.
152    Crdt(Vec<u8>),
153    /// A real algebraic number (`kind` 0), a computable real (1) or an ordinal (2), as its
154    /// canonical `logicaffeine_base::exact_codec` bytes.
155    Exact { kind: u8, bytes: Vec<u8> },
156    /// A hyperreal (`kind` 0) or an infinite integer (1) with the infinitesimal it is written in:
157    /// the infinitesimal's name, the residue facts about its reciprocal, and the value, each as
158    /// canonical bytes.
159    Scaled {
160        kind: u8,
161        name: String,
162        residues: Vec<u8>,
163        bytes: Vec<u8>,
164    },
165    /// A value that depends on the residue of the infinite generator: the infinitesimal's name, the
166    /// residue facts, the modulus, and each class's value (`None` for a class the facts rule out).
167    Depends {
168        name: String,
169        residues: Vec<u8>,
170        modulus: u64,
171        branches: Vec<Option<RtPayload>>,
172    },
173    /// The answer of a search over every natural: its value, the cases it checked, how often it
174    /// changed, and the certificate that settled it when one did.
175    Converging {
176        value: Box<RtPayload>,
177        steps: u64,
178        revisions: u64,
179        certificate: Option<String>,
180    },
181}
182
183#[cfg(test)]
184#[path = "../../logicaffeine_tests/unit/logicaffeine_runtime/payload/tests.rs"]
185mod tests;