1
//! The fold predicate's obligations (0014 Stage 2): default-deny over both
2
//! registries, the five obligations on sample forms, and the per-head lemma
3
//! — every whitelisted head's eval, fed malformed constant inputs, answers a
4
//! datum or an error, never a placeholder, and leaves the table untouched.
5

            
6
use super::*;
7
use crate::ast::WasmType;
8
use crate::reader::Reader;
9
use crate::runtime::{Symbol, SymbolKind};
10

            
11
205
pub(super) fn form(source: &str) -> Expr {
12
205
    Reader::parse(source)
13
205
        .ok()
14
205
        .and_then(|program| program.exprs.into_iter().next())
15
205
        .unwrap_or(Expr::Nil)
16
205
}
17

            
18
/// The value a quoted source evaluates to.
19
11
pub(super) fn datum(source: &str) -> Expr {
20
11
    eval_value(&mut table(), &form(source)).unwrap_or(Expr::Nil)
21
11
}
22

            
23
39
pub(super) fn table() -> SymbolTable {
24
39
    SymbolTable::with_builtins_for_wasm()
25
39
}
26

            
27
9
pub(super) fn with_runtime(name: &str) -> SymbolTable {
28
9
    let mut symbols = table();
29
9
    symbols.define(
30
9
        Symbol::new(name, SymbolKind::Variable).with_value(Expr::WasmRuntime(WasmType::PairRef(
31
9
            crate::ast::PairElement::I32,
32
9
        ))),
33
9
    );
34
9
    symbols
35
9
}
36

            
37
5
pub(super) fn with_constant(name: &str, value: &str) -> SymbolTable {
38
5
    let mut symbols = table();
39
5
    symbols.define(Symbol::new(name, SymbolKind::Variable).with_value(datum(value)));
40
5
    symbols
41
5
}
42

            
43
/// `fold_value` on the live table, asserting the lemma's table obligation:
44
/// the table is as it was on `Ok` and on the "compile it" answer alike.
45
28
pub(super) fn fold_checked(symbols: &mut SymbolTable, source: &str) -> Option<Expr> {
46
28
    let before = symbols.state_digest();
47
28
    let folded = fold_value(symbols, &form(source));
48
28
    let after = symbols.state_digest();
49
28
    let folded = folded.unwrap_or_else(|err| panic!("{source}: {err}"));
50
28
    assert_eq!(
51
28
        strip_charge(&before),
52
28
        strip_charge(&after),
53
        "{source} changed the table"
54
    );
55
28
    folded
56
28
}
57

            
58
/// The digest without the work counter: evaluation charges work by design.
59
220
pub(super) fn strip_charge(digest: &str) -> String {
60
220
    digest
61
220
        .lines()
62
6820
        .filter(|line| !line.starts_with("charged:"))
63
        .collect::<Vec<_>>()
64
220
        .join("\n")
65
220
}
66

            
67
mod folding;
68
mod instances;
69
mod lemma;
70
mod probe;
71
mod refusal;