dive-eval 0.1.0

TLA+ expression and action evaluator with TLC's semantics
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! Adversarial facts for the optional producer proof, not a second evaluator.
#![allow(clippy::expect_used, clippy::panic, reason = "real evaluator fixtures")]

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use super::ProducerStability;
use crate::env::Sv;
use crate::{Evaluator, St};
use crate::{Host, HostControl, HostError, Interrupted, Model, Options};
use dive_ir::{Expr, StdOp};
use dive_value::Value;
use dive_value::{Ctx, DeferredKind, DeferredSet, ValueError};

fn input(extra: &str) -> (Evaluator, dive_ir::DefId, dive_ir::DefId, dive_ir::DefId) {
    let source = format!(
        "---- MODULE Stable ----\nEXTENDS Naturals, Sequences, TLC\nVARIABLE x\n{extra}\n====\n"
    );
    let analysis = dive_sema::analyze_source("Stable.tla", &source, &dive_sema::Options::default());
    assert!(analysis.accepted(), "{analysis:?}");
    let ir = Arc::new(dive_ir::lower(&analysis).expect("IR"));
    let init = ir.root_def("Init").expect("Init");
    let next = ir.root_def("Next").expect("Next");
    let pred = ir.root_def("P").expect("P");
    let evaluator = Evaluator::new(Model::new(ir), Options::tlc()).expect("evaluator");
    (evaluator, init, next, pred)
}

fn proof(
    evaluator: &Evaluator,
    init: dive_ir::DefId,
    next: dive_ir::DefId,
    pred: dive_ir::DefId,
) -> Option<ProducerStability> {
    let producer = |def| {
        (
            evaluator.ir().def(def).body,
            evaluator.scope(def).expect("scope"),
        )
    };
    evaluator
        .canonical_producer_stability_budgeted(
            &[producer(init), producer(next)],
            &[producer(pred)],
            None,
        )
        .expect("inspection without a run interruption")
}

#[test]
fn canonical_record_and_tuple_producers_are_not_a_scalar_admission_rule() {
    for initial in ["[a |-> 0, b |-> <<1, 2>>]", "<<0, [a |-> 1]>>"] {
        let (ev, init, next, pred) = input(&format!(
            "Init == x = {initial}\nNext == UNCHANGED x\nP == x=x"
        ));
        let certificate = proof(&ev, init, next, pred).expect("general aggregate closure");
        assert!(certificate.belongs_to(&ev));
    }
}

#[test]
fn nonliteral_function_constructors_do_not_inherit_literal_normalization_authority() {
    let (ev, init, next, pred) = input("Init == x=(1 :> 0)\nNext == UNCHANGED x\nP == x=x");
    assert!(proof(&ev, init, next, pred).is_none());
}

#[test]
fn sortseq_uses_the_real_pure_callback_and_literal_tuple_result() {
    let (ev, init, next, pred) = input(
        "Init == x=0\nNext == UNCHANGED x\nLess(a,b) == a<b\nP == (SortSeq(<<1,0>>,Less) = <<0,1>>) /\\ (x=x)",
    );
    let before = ev
        .eval_def(pred, &St::state(Arc::from([Some(Value::Int(0))])))
        .expect("actual sorting callback");
    assert_eq!(before, Value::TRUE);
    assert!(proof(&ev, init, next, pred).is_some());
    assert_eq!(
        ev.eval_def(pred, &St::state(Arc::from([Some(Value::Int(0))])))
            .expect("same kernel after inspection"),
        Value::TRUE
    );
}

#[test]
fn enumeration_yield_fact_does_not_relabel_a_raw_domain_canonical() {
    let (ev, init, next, pred) =
        input("Init == x=0\nNext == IF x=0 THEN x' \\in {1,2} ELSE x'=0\nP == x>=0");
    assert!(proof(&ev, init, next, pred).is_some());
    let (raw, init, next, pred) = input("Init == x={0,1}\nNext == UNCHANGED x\nP == 0 \\in x");
    assert!(proof(&raw, init, next, pred).is_none());
}

#[test]
fn a_future_owned_filter_is_unknown_before_any_state_is_generated() {
    let (ev, init, next, pred) =
        input("Init == x=0\nNext == x'={p \\in ({0} \\X {0}): p[1]=0}\nP == x=x");
    assert!(proof(&ev, init, next, pred).is_none());
    assert_eq!(ev.take_output(), Vec::<String>::new());
}

#[test]
fn reordered_intermediate_raw_sets_are_not_hidden_by_canonical_state_values() {
    let (ev, init, next, pred) =
        input("Init == x=0\nNext == UNCHANGED x\nP == LET s == {x,1} IN x \\in s");
    assert!(proof(&ev, init, next, pred).is_none());
}

struct Probe(Arc<AtomicUsize>);
impl DeferredSet for Probe {
    fn kind(&self) -> DeferredKind {
        self.0.fetch_add(1, Ordering::SeqCst);
        DeferredKind::Filter
    }
    fn elements(&self, _ctx: Ctx) -> Result<Vec<Value>, ValueError> {
        self.0.fetch_add(1, Ordering::SeqCst);
        Ok(vec![Value::Int(0)])
    }
    fn member(&self, _element: &Value, _ctx: Ctx) -> Option<Result<bool, ValueError>> {
        self.0.fetch_add(1, Ordering::SeqCst);
        Some(Ok(true))
    }
    fn key(&self) -> Vec<Value> {
        self.0.fetch_add(1, Ordering::SeqCst);
        Vec::new()
    }
    fn as_any(&self) -> Option<&dyn std::any::Any> {
        self.0.fetch_add(1, Ordering::SeqCst);
        Some(self)
    }
}
impl std::fmt::Debug for Probe {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Probe")
    }
}

#[test]
fn opaque_providers_stay_unknown_even_after_a_real_normalization_warmup() {
    let (ev, _, _, pred) = input("Init == x=0\nNext == UNCHANGED x\nP(a) == a=a");
    let calls = Arc::new(AtomicUsize::new(0));
    let value = Value::deferred(Arc::new(Probe(calls.clone())), Ctx::tlc());
    let mut scope = ev.scope(pred).expect("actual predicate scope");
    scope.env.bind(0, Sv::Val(value.clone()));
    let roots = [(ev.ir().def(pred).body, scope)];
    let before = calls.load(Ordering::SeqCst);
    assert!(
        ev.canonical_producer_stability_budgeted(&[], &roots, None)
            .expect("opaque storage is a semantic refusal")
            .is_none()
    );
    assert_eq!(calls.load(Ordering::SeqCst), before);
    let Value::Set(set) = &value else {
        panic!("deferred set")
    };
    let _ = set.elements(Ctx::tlc()).expect("actual warmup");
    let warmed = calls.load(Ordering::SeqCst);
    assert!(warmed > before, "the provider observation must be genuine");
    assert!(
        ev.canonical_producer_stability_budgeted(&[], &roots, None)
            .expect("opaque storage is a semantic refusal")
            .is_none()
    );
    assert_eq!(calls.load(Ordering::SeqCst), warmed);
}

#[test]
fn a_warm_thunk_does_not_hide_its_original_effectful_capture() {
    let (ev, _, _, _) =
        input("Init == x=0\nNext == UNCHANGED x\nF(a) == a /\\ a\nP == F(PrintT(TRUE))");
    let source = ev.ir().root_def("P").expect("P");
    let def = ev.ir().root_def("F").expect("F");
    let Expr::Apply { args, .. } = ev.ir().expr(ev.ir().def(source).body) else {
        panic!("real Apply")
    };
    let scope = ev
        .scope_apply(&ev.scope(source).expect("source scope"), def, args)
        .expect("actual context");
    let body = ev.ir().def(def).body;
    assert_eq!(
        ev.eval_scoped(&scope, body, &St::empty())
            .expect("ordinary warmup"),
        Value::TRUE
    );
    let warmed = ev.take_output();
    assert_eq!(warmed.len(), 1, "real PrintT once through the thunk cache");
    assert!(
        ev.canonical_producer_stability_budgeted(&[], &[(body, scope)], None)
            .expect("effectful capture is refused without evaluation")
            .is_none()
    );
    assert_eq!(ev.take_output(), Vec::<String>::new());
}

#[test]
fn run_budget_and_foreign_scopes_stop_without_runtime_evaluation() {
    let (ev, init, next, pred) = input("Init == x=0\nNext == x'=1-x\nP == 1 \\div (x-x)=0");
    let root = (ev.ir().def(pred).body, ev.scope(pred).expect("scope"));
    let control = StopControl(AtomicUsize::new(0));
    let error = ev
        .canonical_producer_stability_budgeted(&[], &[root], Some(&control))
        .expect_err("the actual run control must stop proof work");
    assert_eq!(error.class(), crate::ErrorClass::Interrupted);
    assert_eq!(error.interruption(), Some(Interrupted::new(727)));
    assert_eq!(control.0.load(Ordering::SeqCst), 1);
    let foreign =
        Evaluator::new(Model::new(ev.ir().clone()), Options::tlc()).expect("independent Core");
    let root = (
        ev.ir().def(pred).body,
        foreign.scope(pred).expect("foreign scope"),
    );
    assert!(
        ev.canonical_producer_stability_budgeted(&[], &[root], None)
            .expect("foreign scope is a semantic refusal")
            .is_none()
    );
    assert!(
        proof(&ev, init, next, pred).is_some(),
        "failing primitive is inspected, not evaluated"
    );
    assert_eq!(ev.take_output(), Vec::<String>::new());
}

#[derive(Debug)]
struct StopControl(AtomicUsize);
impl HostControl for StopControl {
    fn check(&self) -> Result<(), Interrupted> {
        self.0.fetch_add(1, Ordering::SeqCst);
        Err(Interrupted::new(727))
    }
}

#[derive(Debug)]
struct Permission {
    calls: Arc<AtomicUsize>,
    deny: bool,
}
impl Host for Permission {
    fn check_native_access(&self, _op: StdOp) -> Result<(), HostError> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        if self.deny {
            Err(HostError {
                code: 2182,
                message: "permission sentinel".into(),
            })
        } else {
            Ok(())
        }
    }
}
#[derive(Debug)]
struct Continue(Arc<AtomicUsize>);
impl HostControl for Continue {
    fn check(&self) -> Result<(), Interrupted> {
        self.0.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }
}

#[test]
fn public_host_replacement_remains_visible_while_control_persists() {
    // Each evaluator-owned binder row charges loop_work; 2048 real rows
    // necessarily cross its 1024-row polling cadence. Literal domains and
    // TRUE bodies reach control before the only native call (x+1), even when
    // the replacement host denies every native operation.
    let domain = (0..2048)
        .map(|item| item.to_string())
        .collect::<Vec<_>>()
        .join(",");
    let (ev, init, next, pred) = input(&format!(
        "Init == x=0\nNext == UNCHANGED x\nP == IF (\\A item \\in {{{domain}}}: TRUE) THEN x+1 ELSE 0"
    ));
    let calls = Arc::new(AtomicUsize::new(0));
    let controls = Arc::new(AtomicUsize::new(0));
    ev.set_host(Arc::new(Permission {
        calls: calls.clone(),
        deny: false,
    }));
    let _scope = ev
        .host_control(Some(Arc::new(Continue(controls.clone()))))
        .expect("control");
    let producers = [init, next].map(|def| {
        (
            ev.ir().def(def).body,
            ev.scope(def).expect("actual producer scope"),
        )
    });
    assert!(
        ev.canonical_producer_stability_budgeted(&producers, &[], None)
            .expect("installed control permits metadata inspection")
            .is_some()
    );
    assert!(controls.load(Ordering::SeqCst) > 0);
    assert_eq!(calls.load(Ordering::SeqCst), 0);
    // Keep the original runtime-loop witness independent of proof polls.
    controls.store(0, Ordering::SeqCst);
    let state = St::state(Arc::from([Some(Value::Int(0))]));
    assert_eq!(
        ev.eval_def(pred, &state).expect("first host"),
        Value::Int(1)
    );
    let before = controls.load(Ordering::SeqCst);
    assert!(before > 0, "real binder work reached the active control");
    let _old = ev.replace_host(Some(Arc::new(Permission {
        calls: calls.clone(),
        deny: true,
    })));
    let error = ev
        .eval_def(pred, &state)
        .expect_err("replacement permission applies");
    assert!(error.to_string().contains("permission sentinel"));
    assert!(controls.load(Ordering::SeqCst) > before);
    assert_eq!(calls.load(Ordering::SeqCst), 2);
}

#[test]
fn closed_predicate_quantified_intervals_do_not_widen_the_row_certificate() {
    let (ev, _, _, pred) =
        input("Init == x=0\nNext == UNCHANGED x\nP == \\A i \\in 1..3 : x[i] >= 0");
    let scope = ev.scope(pred).expect("actual scope");
    assert!(
        ev.closed_predicate_stability_budgeted(&scope, ev.ir().def(pred).body, None)
            .expect("inspection")
    );
    assert!(
        ev.canonical_producer_stability_budgeted(&[], &[(ev.ir().def(pred).body, scope)], None)
            .expect("original inspection")
            .is_none(),
        "old canonical producer contract unchanged"
    );
}

#[test]
fn closed_predicate_inspection_retains_unknown_history_effect_and_scope_refusals() {
    for predicate in [
        "LET s == {x,1} IN x \\in s",
        "[i \\in {x,1} |-> i] = x",
        "(CHOOSE i \\in {x,1} : TRUE) = x",
        "[i \\in 1..3 |-> i] = x",
        "(CHOOSE i \\in 1..3 : TRUE) = x",
        "[x EXCEPT ![1] = @+1] = x",
        "PrintT(x=x)",
        "ENABLED (x'=x)",
        "(\"a\" \\o \"b\") = x",
    ] {
        let (ev, _, _, pred) = input(&format!(
            "Init == x=0\nNext == UNCHANGED x\nP == {predicate}"
        ));
        let scope = ev.scope(pred).expect("actual scope");
        assert!(
            !ev.closed_predicate_stability_budgeted(&scope, ev.ir().def(pred).body, None)
                .expect("inspection"),
            "{predicate}"
        );
        assert!(ev.take_output().is_empty(), "inspection does not evaluate");
        let foreign =
            Evaluator::new(Model::new(ev.ir().clone()), Options::tlc()).expect("foreign evaluator");
        assert!(
            !foreign
                .closed_predicate_stability_budgeted(&scope, ev.ir().def(pred).body, None)
                .expect("foreign scope refuses")
        );
    }
}

#[test]
fn closed_predicate_scope_values_require_closed_scalar_keyed_function_storage() {
    let (ev, _, _, pred) = input("Init == x=0\nNext == UNCHANGED x\nP(a) == a=a");
    let body = ev.ir().def(pred).body;
    let function = Value::fcn(
        vec![
            (Value::Int(0), Value::tuple(vec![Value::Int(1)])),
            (Value::Int(2), Value::Bool(true)),
        ],
        ev.options().ctx,
    )
    .expect("closed general function");
    let mut scope = ev.scope(pred).expect("scope");
    scope.env.bind(0, Sv::Val(function));
    assert!(
        ev.closed_predicate_stability_budgeted(&scope, body, None)
            .expect("closed inspection")
    );
    assert!(
        ev.canonical_producer_stability_budgeted(&[], &[(body, scope.clone())], None)
            .expect("old inspection")
            .is_none()
    );
    for value in [
        Value::fcn(
            vec![(Value::tuple(vec![Value::Int(0)]), Value::Int(1))],
            ev.options().ctx,
        )
        .expect("aggregate key"),
        Value::fcn(
            vec![(
                Value::Int(0),
                Value::set_raw(vec![Value::Int(1)], ev.options().ctx),
            )],
            ev.options().ctx,
        )
        .expect("raw nested value"),
    ] {
        scope.env.bind(0, Sv::Val(value));
        assert!(
            !ev.closed_predicate_stability_budgeted(&scope, body, None)
                .expect("unproved storage")
        );
    }
}

#[test]
fn closed_predicate_checks_nested_function_context_and_stored_domain_canonicality() {
    let (ev, _, _, pred) = input("Init == x=0\nNext == UNCHANGED x\nP(a) == a=a");
    let body = ev.ir().def(pred).body;
    let mut scope = ev.scope(pred).expect("scope");
    let canonical = Value::fcn(
        vec![
            (Value::Int(0), Value::Int(1)),
            (Value::Int(2), Value::Int(2)),
        ],
        ev.options().ctx,
    )
    .expect("canonical function");
    for value in [
        canonical.clone(),
        Value::tuple(vec![canonical.clone()]),
        Value::tuple(vec![Value::tuple(vec![canonical])]),
    ] {
        scope.env.bind(0, Sv::Val(value));
        assert!(
            ev.closed_predicate_stability_budgeted(&scope, body, None)
                .expect("canonical inspection")
        );
    }
    let foreign = Value::fcn(
        vec![
            (Value::Int(0), Value::Int(1)),
            (Value::Int(2), Value::Int(2)),
        ],
        Ctx::dive(),
    )
    .expect("foreign ordering");
    for value in [
        foreign.clone(),
        Value::tuple(vec![foreign.clone()]),
        Value::tuple(vec![Value::tuple(vec![foreign])]),
    ] {
        scope.env.bind(0, Sv::Val(value));
        assert!(
            !ev.closed_predicate_stability_budgeted(&scope, body, None)
                .expect("foreign context declines")
        );
    }
    // Lossless wire values retain the supplied domain order and multiplicity.
    for bytes in [
        [0x2b, 2, 2, 4, 2, 20, 2, 0, 2, 0], // 2->10, 0->0: unsorted
        [0x2b, 2, 2, 4, 2, 20, 2, 4, 2, 0], // 2->10, 2->0: duplicate
    ] {
        let function = dive_value::codec::decode(&mut bytes.as_slice(), &mut |token| {
            ev.core.names.resolve(token)
        })
        .expect("lossless scalar function");
        let before = function
            .as_fcn()
            .expect("decoded function")
            .iter()
            .map(|(key, _)| key)
            .collect::<Vec<_>>();
        for value in [function.clone(), Value::tuple(vec![function.clone()])] {
            scope.env.bind(0, Sv::Val(value));
            assert!(
                !ev.closed_predicate_stability_budgeted(&scope, body, None)
                    .expect("uncanonical domain declines")
            );
        }
        let after = function.as_fcn().expect("decoded function");
        assert_eq!(
            before,
            after.iter().map(|(key, _)| key).collect::<Vec<_>>(),
            "inspection never repairs the domain"
        );
    }
}

#[test]
fn guarded_action_inspection_keeps_bounded_constructor_effect_and_foreign_scope_boundaries() {
    for (body, eligible) in [
        ("\\E i \\in 1..2: x'=[x EXCEPT ![i]=x[i]+1]", true),
        ("x'=[x EXCEPT ![1]=@+1]", true),
        ("x'=[x EXCEPT ![1][1]=0]", false),
        ("x'=[x EXCEPT ![1]=0, ![2]=0]", false),
        ("x'=[i \\in 1..2 |-> i]", true),
        ("x'=(CHOOSE i \\in 1..2: TRUE)", true),
        ("PrintT(TRUE) /\\ x'=x", false),
        ("ENABLED (x'=x)", false),
    ] {
        let (ev, _, _, pred) = input(&format!("Init == x=0\nNext == UNCHANGED x\nP == {body}"));
        let scope = ev.scope(pred).expect("scope");
        let expr = ev.ir().def(pred).body;
        assert_eq!(
            ev.guarded_action_stability_budgeted(&scope, expr, None)
                .expect("inspection"),
            eligible,
            "{body}"
        );
        assert!(
            !ev.closed_predicate_stability_budgeted(&scope, expr, None)
                .expect("ordinary proof unchanged"),
            "{body}"
        );
        assert_eq!(ev.take_output(), Vec::<String>::new());
        let foreign = Evaluator::new(Model::new(ev.ir().clone()), Options::tlc()).expect("foreign");
        assert!(
            !foreign
                .guarded_action_stability_budgeted(&scope, expr, None)
                .expect("foreign scope")
        );
    }
}