gantz_core 0.4.1

The core types and traits for gantz, an environment for creative systems.
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! The mid-level IR between a gantz graph and Steel code.
//!
//! A graph evaluation lowers to a [`Body`]: a sequence of [`Step`]s ending in
//! a [`Tail`]. The IR is Scheme-shaped - bodies are lexical scopes, branch
//! reconvergence is a [`Join`] point (a local fn whose parameters replace the
//! old phi variables), and a back-edge is a tail-[`Tail::Jump`] to a `rec`
//! join. Emission (`emit.rs`) is a mechanical walk; all graph reasoning
//! happens in lowering (`lower.rs`).
//!
//! Invariants are checked by [`validate`]:
//!
//! - every [`Var`] is bound before use, and never re-bound in scope;
//! - a [`Tail::Jump`] targets a lexically visible join with matching arity
//!   (its own definition only when `rec`);
//! - every path through a branch arm yields the arm's branch [`Step::Branch`]
//!   export arity (`dst.len()`), directly via [`Tail::Ret`] or through the
//!   join it jumps to.

use crate::node;
use std::collections::{BTreeMap, BTreeSet};

/// Identifies a join point: the smallest node id of the region that
/// reconverges at it (or the loop header for `rec` joins).
pub(crate) type JoinId = node::Id;

/// A reference to a bound value.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) enum Atom {
    Var(Var),
    /// The empty list `'()` - the "no value" placeholder (e.g. the export of
    /// a branch arm that does not produce it).
    Unit,
    /// The sentinel marking an *outlet* value that was never produced,
    /// distinguishing "did not fire" from "fired with `'()`". Only flows into
    /// outlet-feeding exports and the fired-signature tests that read them.
    Unfired,
}

/// A variable binding.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) enum Var {
    /// Output `output` of node `node`. Emitted as `node-{node}-o{output}`.
    Output { node: node::Id, output: usize },
    /// A dedicated binding for input `input` of node `node`, used where the
    /// value reaching that input varies by branch arm (a join parameter).
    /// Emitted as `node-{node}-i{input}`.
    Input { node: node::Id, input: usize },
    /// The whole-result binding of node `node` (a branching node's
    /// `(branch-ix value)` pair). Emitted as `node-{node}`. Bound by a
    /// [`Step::Branch`] with a [`Subject::Call`], or by enclosing glue for a
    /// [`Subject::PreBound`] dispatch.
    Result { node: node::Id },
}

/// An argument to a node fn call.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum Arg {
    One(Atom),
    /// Multiple sources target the same input: passed as `(list ...)` in
    /// topological source order.
    List(Vec<Atom>),
}

/// A call to a generated node fn.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct NodeCall {
    pub node: node::Id,
    /// One element per node input; `None` = unconnected for this variant.
    /// The variant's input mask is derived from the `Some`-ness of these.
    pub args: Vec<Option<Arg>>,
    /// The connected-outputs mask (selects the variant and the result shape).
    pub outputs: node::Conns,
    pub stateful: bool,
}

/// What a [`Step::Branch`] dispatches on.
#[derive(Debug)]
pub(crate) enum Subject {
    /// Call the branching node's fn, binding its `(branch-ix value)` pair.
    Call(NodeCall),
    /// The pair is already bound (as [`Var::Result`]) by enclosing glue -
    /// e.g. a nested level's evaluation result bridged into this body.
    PreBound { node: node::Id },
}

/// A single statement within a [`Body`].
#[derive(Debug)]
pub(crate) enum Step {
    /// Call a non-branching node fn, binding its connected outputs.
    Node { dst: Vec<Var>, call: NodeCall },
    /// Bind a delay node's stored (previous-evaluation) value as its output.
    /// Emitted at the top of a level body, before any node runs.
    DelayRead { node: node::Id },
    /// Store the value produced for a delay node's input, to be read on the
    /// *next* evaluation. Ordered like a node call (and conditional when the
    /// producing path is).
    DelayWrite { node: node::Id, arg: Arg },
    /// Define a join point. Visible to all later steps in this body, the
    /// body's tail, and (transitively) their nested arms and join bodies.
    Join(Join),
    /// Dispatch on a branching subject's `(branch-ix value)` pair. `dst`
    /// binds the values this statement exports to subsequent steps: every
    /// path through the arms yields `dst.len()` values.
    Branch {
        subject: Subject,
        dst: Vec<Var>,
        arms: Vec<Arm>,
    },
}

/// One arm of a [`Step::Branch`].
#[derive(Debug)]
pub(crate) struct Arm {
    /// The branch index selecting this arm.
    pub ix: usize,
    /// The output vars bound from the branch value (the arm's active
    /// outputs, ascending output index).
    pub binds: Vec<Var>,
    pub body: Body,
}

/// A join point: a local fn that reconvergent paths tail-call.
#[derive(Debug)]
pub(crate) struct Join {
    pub id: JoinId,
    /// Parameters carry the arm-varying values consumed by the body.
    pub params: Vec<Var>,
    /// Whether the join may jump to itself (a loop header).
    pub rec: bool,
    pub body: Body,
}

/// A lexical block: steps then a tail.
#[derive(Debug)]
pub(crate) struct Body {
    pub steps: Vec<Step>,
    pub tail: Tail,
}

/// How a body ends.
#[derive(Debug)]
pub(crate) enum Tail {
    /// Yield these values to the enclosing context (the branch statement's
    /// exports, or the overall body result).
    Ret(Vec<Atom>),
    /// Tail-call a join point.
    Jump { join: JoinId, args: Vec<Atom> },
}

/// A violation of the IR's scoping or arity invariants.
#[derive(Debug, Eq, PartialEq, thiserror::Error)]
pub(crate) enum Invalid {
    #[error("variable {0:?} referenced before binding")]
    UnboundVar(Var),
    #[error("variable {0:?} bound more than once in scope")]
    Rebound(Var),
    #[error("jump to unknown or out-of-scope join {0}")]
    UnknownJoin(JoinId),
    #[error("join {0} defined more than once in scope")]
    DuplicateJoin(JoinId),
    #[error("jump to join {join} with {got} args, expected {expected}")]
    JumpArity {
        join: JoinId,
        expected: usize,
        got: usize,
    },
    #[error("branch on node {node}: arm {arm} yields {got} values, expected {expected}")]
    ExportArity {
        node: node::Id,
        arm: usize,
        expected: usize,
        got: usize,
    },
    #[error("branch on node {0}: duplicate arm index {1}")]
    DuplicateArm(node::Id, usize),
}

/// A join's signature as seen by jumps and yield-arity resolution.
#[derive(Clone, Copy)]
struct JoinSig {
    arity: usize,
    /// The number of values a call to this join evaluates to, or `None`
    /// while the join is being validated (a `rec` join's self-jumps yield
    /// whatever the join yields, so they constrain nothing).
    yields: Option<usize>,
}

/// Lexical context threaded through validation. Cloned at scope forks (arms,
/// join bodies) so sibling scopes stay independent.
#[derive(Clone, Default)]
struct Scope {
    vars: BTreeSet<Var>,
    joins: BTreeMap<JoinId, JoinSig>,
}

impl Scope {
    fn bind(&mut self, var: Var) -> Result<(), Invalid> {
        if !self.vars.insert(var) {
            return Err(Invalid::Rebound(var));
        }
        Ok(())
    }

    fn check_atom(&self, atom: &Atom) -> Result<(), Invalid> {
        match atom {
            Atom::Unit | Atom::Unfired => Ok(()),
            Atom::Var(v) => self
                .vars
                .contains(v)
                .then_some(())
                .ok_or(Invalid::UnboundVar(*v)),
        }
    }

    fn check_arg(&self, arg: &Arg) -> Result<(), Invalid> {
        match arg {
            Arg::One(a) => self.check_atom(a),
            Arg::List(atoms) => atoms.iter().try_for_each(|a| self.check_atom(a)),
        }
    }

    fn check_call(&self, call: &NodeCall) -> Result<(), Invalid> {
        call.args
            .iter()
            .flatten()
            .try_for_each(|arg| self.check_arg(arg))
    }
}

/// Check the IR invariants for a whole body, given the number of values it is
/// expected to yield (0 for an entry fn body) and any vars bound by enclosing
/// glue (e.g. inlet params of a graph fn, or a pre-bound dispatch pair).
pub(crate) fn validate(body: &Body, yields: usize, pre_bound: &[Var]) -> Result<(), Invalid> {
    let mut scope = Scope::default();
    for &v in pre_bound {
        scope.bind(v)?;
    }
    let got = validate_body(body, &mut scope)?;
    if got.is_some_and(|got| got != yields) {
        // Reuse ExportArity with a sentinel node id for the top level.
        return Err(Invalid::ExportArity {
            node: node::Id::MAX,
            arm: 0,
            expected: yields,
            got: got.unwrap(),
        });
    }
    Ok(())
}

/// Validate a body within `scope`, returning the number of values it yields,
/// or `None` when every path ends in a self-jump to an enclosing rec join
/// (the yield is then the join's own, constraining nothing).
fn validate_body(body: &Body, scope: &mut Scope) -> Result<Option<usize>, Invalid> {
    for step in &body.steps {
        match step {
            Step::Node { dst, call } => {
                scope.check_call(call)?;
                for &v in dst {
                    scope.bind(v)?;
                }
            }
            Step::DelayRead { node } => {
                scope.bind(Var::Output {
                    node: *node,
                    output: 0,
                })?;
            }
            Step::DelayWrite { node: _, arg } => {
                scope.check_arg(arg)?;
            }
            Step::Join(join) => {
                if scope.joins.contains_key(&join.id) {
                    return Err(Invalid::DuplicateJoin(join.id));
                }
                let mut inner = scope.clone();
                for &p in &join.params {
                    inner.bind(p)?;
                }
                // A rec join is visible within its own body with a deferred
                // (`None`) yield: self-jump paths yield whatever the join
                // yields, so they constrain nothing.
                if join.rec {
                    inner.joins.insert(
                        join.id,
                        JoinSig {
                            arity: join.params.len(),
                            yields: None,
                        },
                    );
                }
                let yields = validate_body(&join.body, &mut inner)?;
                scope.joins.insert(
                    join.id,
                    JoinSig {
                        arity: join.params.len(),
                        yields,
                    },
                );
            }
            Step::Branch { subject, dst, arms } => {
                let node = match subject {
                    Subject::Call(call) => {
                        scope.check_call(call)?;
                        call.node
                    }
                    Subject::PreBound { node } => {
                        let pair = Var::Result { node: *node };
                        if !scope.vars.contains(&pair) {
                            return Err(Invalid::UnboundVar(pair));
                        }
                        *node
                    }
                };
                let mut seen = BTreeSet::new();
                for arm in arms {
                    if !seen.insert(arm.ix) {
                        return Err(Invalid::DuplicateArm(node, arm.ix));
                    }
                    let mut inner = scope.clone();
                    for &b in &arm.binds {
                        inner.bind(b)?;
                    }
                    let yields = validate_body(&arm.body, &mut inner)?;
                    if yields.is_some_and(|got| got != dst.len()) {
                        return Err(Invalid::ExportArity {
                            node,
                            arm: arm.ix,
                            expected: dst.len(),
                            got: yields.unwrap(),
                        });
                    }
                }
                for &v in dst {
                    scope.bind(v)?;
                }
            }
        }
    }

    match &body.tail {
        Tail::Ret(atoms) => {
            for a in atoms {
                scope.check_atom(a)?;
            }
            Ok(Some(atoms.len()))
        }
        Tail::Jump { join, args } => {
            for a in args {
                scope.check_atom(a)?;
            }
            let sig = scope.joins.get(join).ok_or(Invalid::UnknownJoin(*join))?;
            if sig.arity != args.len() {
                return Err(Invalid::JumpArity {
                    join: *join,
                    expected: sig.arity,
                    got: args.len(),
                });
            }
            Ok(sig.yields)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn out(node: node::Id, output: usize) -> Var {
        Var::Output { node, output }
    }

    fn call(node: node::Id, args: Vec<Option<Arg>>, n_outputs: usize) -> NodeCall {
        NodeCall {
            node,
            args,
            outputs: node::Conns::connected(n_outputs).unwrap(),
            stateful: false,
        }
    }

    /// `(define node-0-o0 (node-fn-0)) (define node-1-o0 (node-fn-1 node-0-o0))`
    #[test]
    fn linear_chain_validates() {
        let body = Body {
            steps: vec![
                Step::Node {
                    dst: vec![out(0, 0)],
                    call: call(0, vec![], 1),
                },
                Step::Node {
                    dst: vec![out(1, 0)],
                    call: call(1, vec![Some(Arg::One(Atom::Var(out(0, 0))))], 1),
                },
            ],
            tail: Tail::Ret(vec![]),
        };
        validate(&body, 0, &[]).unwrap();
    }

    #[test]
    fn unbound_var_rejected() {
        let body = Body {
            steps: vec![Step::Node {
                dst: vec![out(1, 0)],
                call: call(1, vec![Some(Arg::One(Atom::Var(out(0, 0))))], 1),
            }],
            tail: Tail::Ret(vec![]),
        };
        assert_eq!(validate(&body, 0, &[]), Err(Invalid::UnboundVar(out(0, 0))));
    }

    #[test]
    fn rebound_var_rejected() {
        let body = Body {
            steps: vec![
                Step::Node {
                    dst: vec![out(0, 0)],
                    call: call(0, vec![], 1),
                },
                Step::Node {
                    dst: vec![out(0, 0)],
                    call: call(0, vec![], 1),
                },
            ],
            tail: Tail::Ret(vec![]),
        };
        assert_eq!(validate(&body, 0, &[]), Err(Invalid::Rebound(out(0, 0))));
    }

    /// A branch whose arms jump to a join; the join body consumes the param.
    #[test]
    fn branch_with_join_validates() {
        let param = Var::Input { node: 3, input: 0 };
        let join = Join {
            id: 3,
            params: vec![param],
            rec: false,
            body: Body {
                steps: vec![Step::Node {
                    dst: vec![out(3, 0)],
                    call: call(3, vec![Some(Arg::One(Atom::Var(param)))], 1),
                }],
                tail: Tail::Ret(vec![]),
            },
        };
        let arm = |ix: usize| Arm {
            ix,
            binds: vec![out(1, ix)],
            body: Body {
                steps: vec![],
                tail: Tail::Jump {
                    join: 3,
                    args: vec![Atom::Var(out(1, ix))],
                },
            },
        };
        let body = Body {
            steps: vec![
                Step::Join(join),
                Step::Branch {
                    subject: Subject::Call(call(1, vec![], 2)),
                    dst: vec![],
                    arms: vec![arm(0), arm(1)],
                },
            ],
            tail: Tail::Ret(vec![]),
        };
        validate(&body, 0, &[]).unwrap();
    }

    /// Arm yield arity must match the branch's export count, whether the arm
    /// rets directly or through a join.
    #[test]
    fn export_arity_mismatch_rejected() {
        let body = Body {
            steps: vec![Step::Branch {
                subject: Subject::Call(call(1, vec![], 2)),
                dst: vec![out(9, 0)],
                arms: vec![Arm {
                    ix: 0,
                    binds: vec![out(1, 0)],
                    body: Body {
                        steps: vec![],
                        tail: Tail::Ret(vec![]),
                    },
                }],
            }],
            tail: Tail::Ret(vec![]),
        };
        assert_eq!(
            validate(&body, 0, &[]),
            Err(Invalid::ExportArity {
                node: 1,
                arm: 0,
                expected: 1,
                got: 0,
            })
        );
    }

    /// Sibling arm scopes are independent: both arms may bind the same vars.
    #[test]
    fn sibling_arms_bind_same_vars() {
        let arm = |ix: usize| Arm {
            ix,
            binds: vec![out(1, 0)],
            body: Body {
                steps: vec![],
                tail: Tail::Ret(vec![Atom::Var(out(1, 0))]),
            },
        };
        let body = Body {
            steps: vec![Step::Branch {
                subject: Subject::Call(call(1, vec![], 1)),
                dst: vec![out(1, 0)],
                arms: vec![arm(0), arm(1)],
            }],
            tail: Tail::Ret(vec![]),
        };
        validate(&body, 0, &[]).unwrap();
    }

    #[test]
    fn jump_to_undefined_join_rejected() {
        let body = Body {
            steps: vec![],
            tail: Tail::Jump {
                join: 7,
                args: vec![],
            },
        };
        assert_eq!(validate(&body, 0, &[]), Err(Invalid::UnknownJoin(7)));
    }

    /// A rec join may jump to itself; a non-rec join's body cannot see itself.
    #[test]
    fn rec_join_self_jump() {
        let rec_join = |rec: bool| Body {
            steps: vec![Step::Join(Join {
                id: 5,
                params: vec![],
                rec,
                body: Body {
                    steps: vec![],
                    tail: Tail::Jump {
                        join: 5,
                        args: vec![],
                    },
                },
            })],
            tail: Tail::Ret(vec![]),
        };
        validate(&rec_join(true), 0, &[]).unwrap();
        assert_eq!(
            validate(&rec_join(false), 0, &[]),
            Err(Invalid::UnknownJoin(5))
        );
    }
}