sim-expr-tree-calc 0.1.0

Bounded incremental calculation of ordinary SIM expressions and values.
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
use std::{
    collections::BTreeMap,
    sync::{
        Arc, Mutex, RwLock,
        atomic::{AtomicBool, AtomicUsize, Ordering},
    },
};

use sim_incremental_core::ObservationKind;
use sim_kernel::{
    Args, Callable, CapabilityName, Cx, DefaultFactory, Dir, EagerPolicy, Error, Expr, Object,
    ObjectCompat, Ref, StrictNames, Symbol, Table, Value,
    effect::{Effect, effect_abort_op_key, effect_resume_op_key, resolve_effect},
};
use sim_table_core::TablePath;

use crate::{CalcQuery, EXPR_TREE_REF, ExprTreeCalc, ExprTreeRefPolicy, calc::CalcState};

#[derive(Clone, Default)]
pub(super) struct TestRuntime {
    pub(super) source: Arc<Mutex<String>>,
    calls: Arc<Mutex<BTreeMap<String, usize>>>,
    pub(super) fail: Arc<AtomicBool>,
    pub(super) fail_attempts: Arc<AtomicUsize>,
}

impl TestRuntime {
    fn context(&self) -> Cx {
        let mut cx = strict_context();
        bind_callable(
            &mut cx,
            "probe",
            ProbeCallable {
                source: Arc::clone(&self.source),
                calls: Arc::clone(&self.calls),
            },
        );
        bind_callable(
            &mut cx,
            "concat",
            ConcatCallable {
                calls: Arc::clone(&self.calls),
            },
        );
        bind_callable(&mut cx, "lambda", LambdaCallable);
        bind_callable(&mut cx, "return-lambda", ReturnLambdaCallable);
        bind_callable(&mut cx, "return-dir", ReturnDirCallable);
        bind_callable(&mut cx, "return-opaque", ReturnOpaqueCallable);
        bind_callable(
            &mut cx,
            "fallible",
            FallibleCallable {
                fail: Arc::clone(&self.fail),
                attempts: Arc::clone(&self.fail_attempts),
            },
        );
        cx
    }

    pub(super) fn count(&self, key: &str) -> usize {
        self.calls
            .lock()
            .expect("call counter poisoned")
            .get(key)
            .copied()
            .unwrap_or(0)
    }
}

pub(super) fn runtime_calc(runtime: TestRuntime) -> ExprTreeCalc {
    ExprTreeCalc::with_context_factory(move || runtime.context())
}

pub(super) fn install_diamond(calc: &mut ExprTreeCalc) {
    calc.set_cell(path("/a"), probe_expr());
    calc.set_cell(
        path("/b"),
        call(
            "concat",
            vec![
                explicit_ref("/a"),
                Expr::String("-b".to_owned()),
                Expr::String("b".to_owned()),
            ],
        ),
    );
    calc.set_cell(
        path("/c"),
        call(
            "concat",
            vec![
                explicit_ref("/a"),
                Expr::String("-c".to_owned()),
                Expr::String("c".to_owned()),
            ],
        ),
    );
    calc.set_cell(
        path("/d"),
        call(
            "concat",
            vec![
                explicit_ref("/b"),
                explicit_ref("/c"),
                Expr::String("d".to_owned()),
            ],
        ),
    );
}

pub(super) fn probe_expr() -> Expr {
    call("probe", vec![])
}

pub(super) fn explicit_ref(reference: &str) -> Expr {
    Expr::Call {
        operator: Box::new(Expr::Symbol(Symbol::new(EXPR_TREE_REF))),
        args: vec![Expr::String(reference.to_owned())],
    }
}

pub(super) fn call(name: &str, args: Vec<Expr>) -> Expr {
    Expr::Call {
        operator: Box::new(Expr::Symbol(Symbol::new(name))),
        args,
    }
}

pub(super) fn path(input: &str) -> TablePath {
    TablePath::parse_absolute(input).unwrap()
}

pub(super) fn dependencies(
    calc: &mut ExprTreeCalc,
    key: &str,
) -> Vec<(CalcQuery, ObservationKind)> {
    calc.cell_dependencies(&path(key)).unwrap()
}

pub(super) fn strict_context() -> Cx {
    Cx::new(
        Arc::new(ExprTreeRefPolicy::new(StrictNames(EagerPolicy))),
        Arc::new(DefaultFactory),
    )
}

pub(super) fn effect_context(capability: CapabilityName) -> Cx {
    let (mut cx, seat) = Cx::new_seated(
        Arc::new(ExprTreeRefPolicy::new(StrictNames(EagerPolicy))),
        Arc::new(DefaultFactory),
    );
    seat.grant(&mut cx, capability.clone()).unwrap();
    bind_callable(&mut cx, "effectful", EffectfulCallable { capability });
    cx
}

pub(super) fn lock_probe_context(state: Arc<RwLock<CalcState>>) -> Cx {
    let mut cx = strict_context();
    bind_callable(&mut cx, "lock-probe", LockProbeCallable { state });
    cx
}

pub(super) fn value_expr(value: Value) -> Expr {
    value
        .object()
        .as_expr(&mut strict_context())
        .expect("test value must expose an expression")
}

fn bind_callable<T>(cx: &mut Cx, name: &str, callable: T)
where
    T: Callable + ObjectCompat + 'static,
{
    let value = cx.factory().opaque(Arc::new(callable)).unwrap();
    cx.env_mut().define(Symbol::new(name), value);
}

fn string_arg(cx: &mut Cx, value: &Value) -> sim_kernel::Result<String> {
    match value.object().as_expr(cx)? {
        Expr::String(value) => Ok(value),
        other => Err(Error::Eval(format!(
            "expected string argument, got {other:?}"
        ))),
    }
}

struct ProbeCallable {
    source: Arc<Mutex<String>>,
    calls: Arc<Mutex<BTreeMap<String, usize>>>,
}

impl Callable for ProbeCallable {
    fn call(&self, cx: &mut Cx, _args: Args) -> sim_kernel::Result<Value> {
        *self
            .calls
            .lock()
            .unwrap()
            .entry("probe".to_owned())
            .or_default() += 1;
        cx.factory().string(self.source.lock().unwrap().clone())
    }
}

impl_test_callable!(ProbeCallable, "#<probe>");

struct ConcatCallable {
    calls: Arc<Mutex<BTreeMap<String, usize>>>,
}

impl Callable for ConcatCallable {
    fn call(&self, cx: &mut Cx, args: Args) -> sim_kernel::Result<Value> {
        let values = args.values();
        let Some((label, parts)) = values.split_last() else {
            return Err(Error::Eval("concat requires a counter label".to_owned()));
        };
        let label = string_arg(cx, label)?;
        *self
            .calls
            .lock()
            .unwrap()
            .entry(format!("concat-{label}"))
            .or_default() += 1;
        let mut out = String::new();
        for part in parts {
            out.push_str(&string_arg(cx, part)?);
        }
        cx.factory().string(out)
    }
}

impl_test_callable!(ConcatCallable, "#<concat>");

struct LambdaCallable;

impl Callable for LambdaCallable {
    fn call(&self, _cx: &mut Cx, args: Args) -> sim_kernel::Result<Value> {
        args.values()
            .first()
            .cloned()
            .ok_or_else(|| Error::Eval("lambda requires one argument".to_owned()))
    }
}

impl_test_callable!(LambdaCallable, "#<lambda>");

macro_rules! return_callable {
    ($type:ident, $value:expr, $display:expr) => {
        struct $type;

        impl Callable for $type {
            fn call(&self, cx: &mut Cx, _args: Args) -> sim_kernel::Result<Value> {
                cx.factory().opaque(Arc::new($value))
            }
        }

        impl_test_callable!($type, $display);
    };
}

return_callable!(ReturnLambdaCallable, LambdaCallable, "#<return-lambda>");
return_callable!(ReturnDirCallable, EmptyDir, "#<return-dir>");
return_callable!(ReturnOpaqueCallable, OpaqueMarker, "#<return-opaque>");

struct FallibleCallable {
    fail: Arc<AtomicBool>,
    attempts: Arc<AtomicUsize>,
}

impl Callable for FallibleCallable {
    fn call(&self, cx: &mut Cx, _args: Args) -> sim_kernel::Result<Value> {
        self.attempts.fetch_add(1, Ordering::AcqRel);
        if self.fail.load(Ordering::Acquire) {
            Err(Error::Eval("requested failure".to_owned()))
        } else {
            cx.factory().string("good".to_owned())
        }
    }
}

impl_test_callable!(FallibleCallable, "#<fallible>");

struct LockProbeCallable {
    state: Arc<RwLock<CalcState>>,
}

impl Callable for LockProbeCallable {
    fn call(&self, cx: &mut Cx, _args: Args) -> sim_kernel::Result<Value> {
        let guard = self
            .state
            .try_write()
            .expect("calculator state lock or mutable borrow spanned SIM evaluation");
        drop(guard);
        cx.factory().string("unlocked".to_owned())
    }
}

impl_test_callable!(LockProbeCallable, "#<lock-probe>");

struct EffectfulCallable {
    capability: CapabilityName,
}

impl Callable for EffectfulCallable {
    fn call(&self, cx: &mut Cx, _args: Args) -> sim_kernel::Result<Value> {
        let effect = Effect::new(
            Symbol::qualified("expr-tree", "test-effect"),
            Ref::Symbol(Symbol::qualified("expr-tree", "test-subject")),
            Ref::Symbol(Symbol::qualified("expr-tree", "test-input")),
            Ref::Symbol(Symbol::qualified("core", "Any")),
            effect_resume_op_key(),
            effect_abort_op_key(),
        )
        .requiring(self.capability.clone());
        resolve_effect(cx, effect, |_cx, _effect| {
            Ok(Ref::Symbol(Symbol::qualified("expr-tree", "effect-ok")))
        })?;
        cx.factory().string("effect-ok".to_owned())
    }
}

impl_test_callable!(EffectfulCallable, "#<effectful>");

pub(super) struct OpaqueMarker;

impl Object for OpaqueMarker {
    fn display(&self, _cx: &mut Cx) -> sim_kernel::Result<String> {
        Ok("#<opaque-marker>".to_owned())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl ObjectCompat for OpaqueMarker {}

struct EmptyDir;

impl Object for EmptyDir {
    fn display(&self, _cx: &mut Cx) -> sim_kernel::Result<String> {
        Ok("#<empty-dir>".to_owned())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl ObjectCompat for EmptyDir {
    fn as_table_impl(&self) -> Option<&dyn Table> {
        Some(self)
    }

    fn as_dir(&self) -> Option<&dyn Dir> {
        Some(self)
    }
}

impl Table for EmptyDir {
    fn backend_symbol(&self) -> Symbol {
        Symbol::new("test/empty-dir")
    }

    fn get(&self, cx: &mut Cx, _key: Symbol) -> sim_kernel::Result<Value> {
        cx.factory().nil()
    }

    fn set(&self, _cx: &mut Cx, _key: Symbol, _value: Value) -> sim_kernel::Result<()> {
        Err(Error::Eval("empty test dir is immutable".to_owned()))
    }

    fn has(&self, _cx: &mut Cx, _key: Symbol) -> sim_kernel::Result<bool> {
        Ok(false)
    }

    fn del(&self, cx: &mut Cx, _key: Symbol) -> sim_kernel::Result<Value> {
        cx.factory().nil()
    }

    fn keys(&self, _cx: &mut Cx) -> sim_kernel::Result<Vec<Symbol>> {
        Ok(Vec::new())
    }

    fn entries(&self, _cx: &mut Cx) -> sim_kernel::Result<Vec<(Symbol, Value)>> {
        Ok(Vec::new())
    }

    fn len(&self, _cx: &mut Cx) -> sim_kernel::Result<usize> {
        Ok(0)
    }

    fn clear(&self, _cx: &mut Cx) -> sim_kernel::Result<()> {
        Ok(())
    }
}

impl Dir for EmptyDir {
    fn mkdir(&self, _cx: &mut Cx, _name: Symbol) -> sim_kernel::Result<Value> {
        Err(Error::Eval("empty test dir is immutable".to_owned()))
    }

    fn opendir(&self, _cx: &mut Cx, _name: Symbol) -> sim_kernel::Result<Option<Value>> {
        Ok(None)
    }

    fn rmdir(&self, _cx: &mut Cx, _name: Symbol) -> sim_kernel::Result<Value> {
        Err(Error::Eval("empty test dir is immutable".to_owned()))
    }

    fn is_dir(&self, _cx: &mut Cx, _name: Symbol) -> sim_kernel::Result<bool> {
        Ok(false)
    }
}

macro_rules! impl_test_callable {
    ($ty:ty, $display:expr) => {
        impl Object for $ty {
            fn display(&self, _cx: &mut Cx) -> sim_kernel::Result<String> {
                Ok($display.to_owned())
            }

            fn as_any(&self) -> &dyn std::any::Any {
                self
            }
        }

        impl ObjectCompat for $ty {
            fn as_callable(&self) -> Option<&dyn Callable> {
                Some(self)
            }
        }
    };
}

use impl_test_callable;