dive-core 0.1.0

The dive execution core for TLA+: value store, plan compiler, interpreter and successor generation
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Opt-in maps and observation of actual compiled instruction activations.
use crate::plan::{Code, Node};
use crate::{Error, Handle, Machine};
use dive_ir::{FileInfo, Origin};
use std::sync::Arc;

/// An actual compiled instruction, not a source-text projection.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Instruction {
    /// A value instruction.
    Value(u32),
    /// A generator instruction.
    Generator(u32),
}

/// A local's existing storage; inspection never evaluates a recipe.
#[derive(Clone, Debug)]
pub enum Binding {
    /// A flat activation slot, possibly still unforced/unbound.
    Slot(u32),
    /// A compiled alias, readable only when already a scalar/storage read.
    Alias(u32),
    /// No readable storage exists for this source local.
    Unavailable,
}

/// A source local and its actual compiled storage association.
#[derive(Clone, Debug)]
pub struct Local {
    /// Original source name.
    pub name: String,
    /// Existing storage association.
    pub binding: Binding,
}

/// An actual active compiler substitution, including dynamic INSTANCE contexts.
#[derive(Clone, Debug)]
pub struct Substitution {
    /// The declaration being substituted.
    pub declaration: String,
    /// Origin of the actual expression, if the binding is an expression.
    pub actual: Option<Origin>,
    /// Unique substitution identity in this compilation.
    pub identity: u32,
}

/// Source context captured while compiling an instruction.
#[derive(Clone, Debug)]
pub struct Location {
    /// Exact IR origin, including INSTANCE ownership.
    pub origin: Origin,
    /// Definition whose environment compiled the instruction.
    pub definition: String,
    /// Definition identity; names alone do not identify an activation.
    pub definition_id: usize,
    /// Actual compiler environment identity, distinguishing inlined call sites.
    pub environment_id: usize,
    /// Source locals associated with actual flat storage/aliases.
    pub locals: Vec<Local>,
    /// Calling compilation context, before inlining/substitution.
    pub caller: Option<usize>,
    /// Actual active substitution prefix, distinct from static IR instantiation.
    pub substitutions: Vec<Substitution>,
}

/// Optional instruction/source tables. Ordinary compilation allocates none.
#[derive(Clone, Debug, Default)]
pub struct SourceMap {
    /// Source files already loaded by the caller, never opened by inspection.
    pub files: Vec<FileInfo>,
    /// State-variable names in actual machine order.
    pub variables: Vec<String>,
    /// Source compilation contexts.
    pub locations: Vec<Location>,
    /// Value instruction to source context; synthetic unmapped nodes are None.
    pub values: Vec<Option<usize>>,
    /// Generator instruction to source context.
    pub generators: Vec<Option<usize>>,
    /// Actual invariant/property root instructions and their configured names.
    pub obligations: Vec<(u32, String)>,
    /// Instructions reachable from the actual compiled plan roots.
    pub executable: Vec<Instruction>,
}

impl SourceMap {
    /// Finds the retained context for an actual instruction.
    #[must_use]
    pub fn location(&self, instruction: Instruction) -> Option<&Location> {
        let index = match instruction {
            Instruction::Value(index) => self.values.get(index as usize),
            Instruction::Generator(index) => self.generators.get(index as usize),
        };
        index.and_then(|index| index.and_then(|index| self.locations.get(index)))
    }
}

/// Actual completion of an observed instruction.
#[derive(Clone, Copy, Debug)]
pub enum Returned<'a> {
    /// An actual returned value.
    Value(Handle),
    /// Completed generator traversal.
    Generated,
    /// The actual error before activation unwinding.
    Failed(&'a Error),
}

/// Explicit producer-only instruction observer. Replay machines stay unobserved.
pub trait Observer: Send + Sync + std::fmt::Debug {
    /// Publishes the real compiled map before search begins.
    ///
    /// # Errors
    /// Cancellation or an unsupported observation boundary.
    fn prepared(&self, map: &SourceMap) -> Result<(), Error>;
    /// Enters an actual instruction activation.
    ///
    /// # Errors
    /// Cancellation or an unsupported observation boundary.
    fn enter(&self, instruction: Instruction, code: &Code, machine: &Machine) -> Result<(), Error>;
    /// Observes a result while its activation remains live.
    ///
    /// # Errors
    /// Cancellation or an unsupported observation boundary.
    fn returned(
        &self,
        instruction: Instruction,
        code: &Code,
        machine: &Machine,
        result: Returned<'_>,
    ) -> Result<(), Error>;
    /// Leaves the matching activation, including on errors.
    fn leave(&self);
}

pub(crate) struct Guard(Arc<dyn Observer>);
impl Guard {
    pub(crate) fn enter(
        observer: &Arc<dyn Observer>,
        instruction: Instruction,
        code: &Code,
        machine: &Machine,
    ) -> Result<Self, Error> {
        observer.enter(instruction, code, machine)?;
        Ok(Self(Arc::clone(observer)))
    }
    pub(crate) fn returned(
        &self,
        instruction: Instruction,
        code: &Code,
        machine: &Machine,
        result: Returned<'_>,
    ) -> Result<(), Error> {
        self.0.returned(instruction, code, machine, result)
    }
}
impl Drop for Guard {
    fn drop(&mut self) {
        self.0.leave();
    }
}

struct Suspended<'a> {
    machine: &'a mut Machine,
    observer: Option<Arc<dyn Observer>>,
}
impl Drop for Suspended<'_> {
    fn drop(&mut self) {
        self.machine.debugger = self.observer.take();
    }
}

impl Machine {
    /// Runs nonproducer reconstruction without this machine's debugger capability.
    /// The original capability is restored when the operation returns or unwinds,
    /// including on errors. The controller and its stopped snapshots are untouched.
    pub fn without_debugger<T>(&mut self, operation: impl FnOnce(&mut Self) -> T) -> T {
        let observer = self.debugger.take();
        let suspended = Suspended {
            machine: self,
            observer,
        };
        operation(&mut *suspended.machine)
    }
}

/// Reads only an already-existing scalar/storage alias. Compound expressions,
/// unforced thunks and by-name recipes remain unavailable; no evaluator is called.
#[must_use]
pub fn read_binding(
    binding: &Binding,
    code: &Code,
    machine: &Machine,
    base: usize,
) -> Option<Handle> {
    let slot = |slot: u32| {
        base.checked_add(slot as usize)
            .and_then(|slot| machine.frame.get(slot))
            .copied()
    };
    match binding {
        Binding::Slot(index) => slot(*index),
        Binding::Alias(index) => match code.nodes.get(*index as usize)? {
            Node::Const(value) => Some(*value),
            Node::Var(index) => machine.s0.get(usize::from(*index)).copied(),
            Node::Primed(index) => machine.s1.get(usize::from(*index)).copied(),
            Node::Slot(index) | Node::Lazy { slot: index, .. } => slot(*index),
            _ => None,
        },
        Binding::Unavailable => None,
    }
}

#[allow(
    clippy::too_many_lines,
    reason = "one arm per actual instruction kind for debug-only graph reachability"
)]
pub(crate) fn reachable(plan: &crate::plan::Plan) -> Vec<Instruction> {
    use crate::plan::{GNode, LibArg, Unch};
    let mut pending = Vec::new();
    pending.extend(
        plan.init
            .iter()
            .chain(&plan.next)
            .map(|action| Instruction::Generator(action.g)),
    );
    for check in plan
        .invariants
        .iter()
        .chain(&plan.constraints)
        .chain(&plan.action_constraints)
        .chain(&plan.implied_inits)
        .chain(&plan.implied_actions)
        .chain(plan.view.iter())
    {
        pending.extend(
            check
                .conjuncts
                .iter()
                .map(|part| Instruction::Value(part.p)),
        );
    }
    let mut seen = std::collections::HashSet::new();
    while let Some(instruction) = pending.pop() {
        if !seen.insert(instruction) {
            continue;
        }
        let mut values = Vec::new();
        let mut generators = Vec::new();
        match instruction {
            Instruction::Value(index) => {
                if let Some(node) = plan.code.nodes.get(index as usize) {
                    match node {
                        Node::Const(_) | Node::Slot(_) | Node::Var(_) | Node::Primed(_) => {}
                        Node::Not(a)
                        | Node::Neg(a)
                        | Node::Powerset(a)
                        | Node::BigUnion(a)
                        | Node::Domain(a)
                        | Node::Spec(a)
                        | Node::Select(a, _) => values.push(*a),
                        Node::Lazy { body, .. }
                        | Node::Scope { body, .. }
                        | Node::SameVars { eq: body, .. }
                        | Node::Memo { body, .. } => values.push(*body),
                        Node::Implies(a, b)
                        | Node::Equiv(a, b)
                        | Node::Eq(a, b)
                        | Node::Neq(a, b)
                        | Node::In(a, b)
                        | Node::NotIn(a, b)
                        | Node::Subseteq(a, b)
                        | Node::Range(a, b)
                        | Node::FcnSet(a, b)
                        | Node::Apply(a, b)
                        | Node::Arith(_, a, b)
                        | Node::Cmp(_, a, b)
                        | Node::SetBin(_, a, b) => values.extend([*a, *b]),
                        Node::If(a, b, c) => values.extend([*a, *b, *c]),
                        Node::And(items)
                        | Node::Or(items)
                        | Node::Product(items)
                        | Node::Tuple(items)
                        | Node::Std(_, items)
                        | Node::SetEnum(items)
                        | Node::Lib { args: items, .. } => values.extend(items.iter().copied()),
                        Node::Record(items) | Node::RecSet(items) => {
                            values.extend(items.iter().map(|(_, p)| *p));
                        }
                        Node::Case(arms, other) => {
                            for (a, b) in arms {
                                values.extend([*a, *b]);
                            }
                            values.extend(*other);
                        }
                        Node::Filter { bind, pred, .. } => values.extend([bind.domain, *pred]),
                        Node::Map { binds, body }
                        | Node::FcnCons { binds, body }
                        | Node::Quant { binds, body, .. } => {
                            values.extend(binds.iter().map(|bind| bind.domain));
                            values.push(*body);
                        }
                        Node::Except { base, updates } => {
                            values.push(*base);
                            for update in updates {
                                values.extend(update.path.iter().copied());
                                values.push(update.value);
                            }
                        }
                        Node::Call { fun, args } => {
                            values.extend(args.iter().copied());
                            if let Some(fun) = plan.code.funs.get(*fun as usize) {
                                values.push(fun.body);
                            }
                        }
                        Node::LibHo(_, args) => {
                            for arg in args {
                                values.push(match arg {
                                    LibArg::Value(p) => *p,
                                    LibArg::Op { body, .. } => *body,
                                });
                            }
                        }
                        Node::Enabled { g, .. } => generators.push(*g),
                        Node::Composition { first, then, .. } => {
                            generators.extend([*first, *then]);
                        }
                        Node::InLazy { elem, set, .. } => values.extend([*elem, *set]),
                        Node::Beta { chain, fc, arg } => values.extend([*chain, *fc, *arg]),
                        Node::DomCheck { domain, body, .. } => values.extend([*domain, *body]),
                    }
                }
            }
            Instruction::Generator(index) => {
                if let Some(node) = plan.code.gnodes.get(index as usize) {
                    let mut unchanged = |items: &[Unch]| {
                        values.extend(items.iter().filter_map(|item| {
                            if let Unch::Verify(p) = item {
                                Some(*p)
                            } else {
                                None
                            }
                        }));
                    };
                    match node {
                        GNode::Composition { first, then, .. } => {
                            generators.extend([*first, *then]);
                        }
                        GNode::Conj(items) => {
                            for (g, p) in items {
                                generators.push(*g);
                                values.push(*p);
                            }
                        }
                        GNode::Disj(items) => generators.extend(items.iter().copied()),
                        GNode::Exists {
                            binds,
                            body,
                            body_p,
                        }
                        | GNode::Forall {
                            binds,
                            body,
                            body_p,
                        } => {
                            values.extend(binds.iter().map(|bind| bind.domain));
                            values.push(*body_p);
                            generators.push(*body);
                        }
                        GNode::If { cond, then, other } => {
                            values.push(*cond);
                            generators.extend([*then, *other]);
                        }
                        GNode::Case { arms, other } => {
                            for (p, g) in arms {
                                values.push(*p);
                                generators.push(*g);
                            }
                            generators.extend(*other);
                        }
                        GNode::Implies { lhs, rhs } => {
                            values.push(*lhs);
                            generators.push(*rhs);
                        }
                        GNode::Assign { rhs, .. } | GNode::Guard(rhs) => values.push(*rhs),
                        GNode::AssignIn { set, .. } => values.push(*set),
                        GNode::AssignInRaw { set, raw, .. } => {
                            values.push(*set);
                            raw_instructions(raw, &mut values);
                        }
                        GNode::Unchanged(items) => unchanged(items),
                        GNode::StepBox { action, vars } => {
                            unchanged(vars);
                            generators.push(*action);
                        }
                        GNode::StepAngle { action, same } => {
                            values.push(*same);
                            generators.push(*action);
                        }
                        GNode::FcnAction { binds, arg, body } => {
                            values.extend(binds.iter().map(|bind| bind.domain));
                            values.push(*arg);
                            generators.push(*body);
                        }
                        GNode::Scope { body, .. } => generators.push(*body),
                    }
                }
            }
        }
        pending.extend(values.into_iter().map(Instruction::Value));
        pending.extend(generators.into_iter().map(Instruction::Generator));
    }
    seen.into_iter().collect()
}

fn raw_instructions(raw: &crate::plan::Raw, values: &mut Vec<u32>) {
    use crate::plan::Raw;
    let mut pending = vec![raw];
    while let Some(raw) = pending.pop() {
        match raw {
            Raw::Leaf(node) | Raw::UnionOf(node) => values.push(*node),
            Raw::Cup(left, right) => pending.extend([&**left, &**right]),
            Raw::Union(items) => pending.extend(items.iter()),
            Raw::Filter { bind, pred, inner } => {
                values.extend([bind.domain, *pred]);
                pending.push(inner);
            }
            Raw::Cap(left, right, first, second) => {
                pending.extend([&**left, &**right]);
                values.extend([*first, *second]);
            }
            Raw::Minus(inner, other) => {
                pending.push(inner);
                values.push(*other);
            }
            Raw::Odometer(items, node) => {
                pending.extend(items.iter());
                values.push(*node);
            }
        }
    }
}