sva-engine 0.7.12

Renders a resolved graph into per-node buffers a query can be asked of
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// Concern: gives every node one Ty and the value it lowered to | Non-concern: the per-term judgment (sva-formula), lowering (lower/) | IO: (Instances, Order) -> Ty per node

mod draft;

use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

use sva_formula::filter::Shape;
use sva_formula::{C64, ClosedForm, Codomain, Env, Held, NodeId, Origin, ParamId, Ty, Var, infer};
use sva_samples::Params;

use crate::arguments::{Arguments, Called, Chosen};
use crate::cache::Stored;
use crate::cast::Cast;
use crate::error::{Diagnostic, EngineError, Located};
use crate::instantiate::Instances;
use crate::lower;
use crate::schedule::Order;
use crate::time::Grid;

use draft::{Draft, Entry, Units};

/// A closed form is cast-free on one axis; every crossing is its own node.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    ClosedForm(ClosedForm),
    Cast(Cast, NodeId),
    Op {
        name: String,
        args: Vec<NodeId>,
    },
    SelfAt {
        at: When,
    },
    Read {
        source: NodeId,
        at: When,
        site: Origin,
    },
    Noise(u64),
    /// The signal and its three arguments, each a node of its own.
    Filter {
        shape: Shape,
        x: NodeId,
        cutoff: NodeId,
        q: NodeId,
        gain: NodeId,
    },
    Solver {
        params: Box<Params>,
        varying: Vec<(&'static str, NodeId)>,
    },
    /// Samples the store answered in place of the node's own source, which is never typed.
    Stored(Arc<Stored>),
}

/// A read's instant: the exact time `k*t + s` written; a closed form of `t` held as a node;
/// or an integer each sample evaluates.
#[derive(Clone, Debug, PartialEq)]
pub enum When {
    At(crate::time::Affine),
    Moving(NodeId),
    Index(crate::index::Index),
    Step(Step),
}

/// An integer no one map spells: exact indices, `idx(time, round)` of a time that moves, and
/// sums, negations and products of them.
#[derive(Clone, Debug, PartialEq)]
pub enum Step {
    Index(crate::index::Index),
    Nearest(NodeId, crate::index::Round),
    Add(Vec<Step>),
    Neg(Box<Step>),
    Mul(Vec<Step>),
}

impl Step {
    /// Every time that moves in it.
    pub(crate) fn times(&self, out: &mut Vec<NodeId>) {
        match self {
            Step::Index(_) => {}
            Step::Nearest(time, _) => out.push(*time),
            Step::Add(parts) | Step::Mul(parts) => parts.iter().for_each(|p| p.times(out)),
            Step::Neg(p) => p.times(out),
        }
    }
}

impl When {
    /// Every node it evaluates each sample.
    pub(crate) fn moving(&self) -> Vec<NodeId> {
        let mut out = Vec::new();
        match self {
            When::Moving(id) => out.push(*id),
            When::Step(step) => step.times(&mut out),
            When::At(_) | When::Index(_) => {}
        }
        out
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Node {
    pub name: String,
    pub ty: Ty,
    pub var: Var,
    pub value: Value,
    pub grid: Grid,
}

/// Every node keeps one id while held; a node lowered anew takes a free one.
#[derive(Debug, Default, PartialEq)]
pub struct Typing {
    nodes: Vec<Option<Node>>,
    free: Vec<u32>,
    arguments: BTreeMap<String, Arguments>,
    by_path: BTreeMap<String, NodeId>,
    /// Paths `by_path` holds for a file's one instance.
    aliases: BTreeSet<String>,
    copies: BTreeMap<String, BTreeMap<Grid, NodeId>>,
    lowering: BTreeSet<String>,
    files: BTreeMap<String, Vec<NodeId>>,
    origins: Vec<Option<(u32, Option<sva_ast::ByteSpan>)>>,
    free_origins: Vec<u32>,
    /// Each node name origins mark, and how many.
    sites: Vec<Option<(String, u32)>>,
    free_sites: Vec<u32>,
    site_ids: BTreeMap<String, u32>,
    pending: BTreeSet<NodeId>,
    indices: u32,
    sum: Option<(NodeId, Vec<SumSlot>)>,
    numbers: Numbers,
    /// Every node the latest draft lowered, in order.
    lowered: Vec<String>,
    units: BTreeMap<String, Units>,
    making: Vec<(String, Grid)>,
    draft: Draft,
}

/// Each node's folded number, derived from the nodes alone; a settle evicts it all.
#[derive(Debug, Default)]
struct Numbers(RefCell<BTreeMap<NodeId, Option<C64>>>);

impl PartialEq for Numbers {
    fn eq(&self, _: &Numbers) -> bool {
        true
    }
}

/// One term of a stream's note sum: its node, or the hull of the supports of those it retired.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum SumSlot {
    Node(NodeId),
    Retired(sva_samples::Extent),
}

impl Typing {
    /// `node` named by its terms in place.
    pub(crate) fn name_sum(&mut self, node: NodeId, slots: Vec<SumSlot>) {
        self.draft.sum = Some(Some((node, slots)));
    }

    fn summed(&self) -> Option<&(NodeId, Vec<SumSlot>)> {
        match &self.draft.sum {
            Some(staged) => staged.as_ref(),
            None => self.sum.as_ref(),
        }
    }

    /// The note sum, where it holds a term it retired.
    pub(crate) fn retired_sum(&self) -> Option<NodeId> {
        let (sum, slots) = self.summed()?;
        let retired = slots.iter().any(|slot| matches!(slot, SumSlot::Retired(_)));
        retired.then_some(*sum)
    }

    /// The edges a node's identity is hashed over.
    pub(crate) fn operands(&self, id: NodeId) -> Vec<NodeId> {
        match self.value(id) {
            Value::ClosedForm(form) => crate::refs::nodes_in(&form.body),
            Value::Cast(_, source) | Value::Read { source, .. } => vec![*source],
            Value::Op { args, .. } => args.clone(),
            Value::Filter {
                x, cutoff, q, gain, ..
            } => vec![*x, *cutoff, *q, *gain],
            Value::Solver { varying, .. } => varying.iter().map(|(_, a)| *a).collect(),
            Value::SelfAt { .. } | Value::Noise(_) | Value::Stored(_) => Vec::new(),
        }
    }

    /// Whether `id` reads `of`, however far down.
    pub(crate) fn reads(&self, id: NodeId, of: NodeId) -> bool {
        let (mut open, mut seen) = (vec![id], BTreeSet::new());
        while let Some(at) = open.pop() {
            if at == of {
                return true;
            }
            if seen.insert(at) {
                open.extend(self.operands(at));
            }
        }
        false
    }

    pub(crate) fn sum_slots(&self, node: NodeId) -> Option<&[SumSlot]> {
        self.summed()
            .filter(|(held, _)| *held == node)
            .map(|(_, slots)| slots.as_slice())
    }

    pub(crate) fn lowering(&mut self, path: &str) {
        self.lowered.push(path.to_string());
    }

    pub(crate) fn lowered(&self) -> &[String] {
        &self.lowered
    }

    pub(crate) fn next_index(&mut self) -> sva_formula::IndexId {
        self.indices += 1;
        sva_formula::IndexId(self.indices)
    }

    /// A call lowered twice is noted once, at its latest lowering.
    pub(crate) fn note(&mut self, node: &str, call: Option<Called>, chosen: Vec<Chosen>) {
        let mut held = self.arguments(node).cloned().unwrap_or_else(|| Arguments {
            node: node.to_string(),
            ..Arguments::default()
        });
        if let Some(call) = call {
            held.calls
                .retain(|c| (c.at.start, &c.name) != (call.at.start, &call.name));
            held.calls.push(call);
        }
        for one in chosen {
            held.chosen.retain(|c| c.at.start != one.at.start);
            held.chosen.push(one);
        }
        let old = self.draft.arguments.insert(node.to_string(), held);
        self.draft.journal.push(Entry::Noted(node.to_string(), old));
    }

    pub fn arguments(&self, node: &str) -> Option<&Arguments> {
        match self.draft.arguments.get(node) {
            Some(held) => Some(held),
            None if self.draft.hidden.contains(node) => None,
            None => self.arguments.get(node),
        }
    }

    pub fn ty(&self, n: NodeId) -> Ty {
        self.at(n).ty
    }

    pub fn var(&self, n: NodeId) -> Var {
        self.at(n).var
    }

    pub fn value(&self, n: NodeId) -> &Value {
        &self.at(n).value
    }

    pub fn name(&self, n: NodeId) -> &str {
        &self.at(n).name
    }

    pub fn grid(&self, n: NodeId) -> Grid {
        self.at(n).grid
    }

    pub(crate) fn copy(&self, path: &str, grid: Grid) -> Option<NodeId> {
        let key = (path.to_string(), grid);
        match self.draft.copies.get(&key) {
            Some(id) => Some(*id),
            None if self.draft.hidden.contains(path) => None,
            None => self
                .copies
                .get(path)
                .and_then(|held| held.get(&grid))
                .copied(),
        }
    }

    pub(crate) fn copied(&mut self, path: &str, grid: Grid, id: NodeId) {
        let key = (path.to_string(), grid);
        let old = self.draft.copies.insert(key.clone(), id);
        self.draft.journal.push(Entry::Copy(key, old));
    }

    /// `false` where a copy of `path` is already being lowered: a loop of refs.
    pub(crate) fn opened(&mut self, path: &str) -> bool {
        self.lowering.insert(path.to_string())
    }

    pub(crate) fn closed(&mut self, path: &str) {
        self.lowering.remove(path);
    }

    pub fn at(&self, n: NodeId) -> &Node {
        self.nodes[n.0 as usize]
            .as_ref()
            .expect("a node the typing holds")
    }

    pub fn ids(&self) -> impl Iterator<Item = NodeId> + '_ {
        let held = self.nodes.iter().enumerate();
        held.filter(|(_, n)| n.is_some())
            .map(|(at, _)| NodeId(at as u32))
    }

    pub(crate) fn len(&self) -> usize {
        self.nodes.len() - self.free.len()
    }

    pub fn id(&self, path: &str) -> Option<NodeId> {
        match self.draft.by_path.get(path) {
            Some(id) => Some(*id),
            None if self.draft.hidden.contains(path) => None,
            None => self.by_path.get(path).copied(),
        }
    }

    /// Every path named, the draft's over the rest.
    pub fn paths(&self) -> impl Iterator<Item = (&str, NodeId)> {
        let drafted = self.draft.by_path.iter();
        let kept = self.by_path.iter().filter(|(path, _)| {
            !self.draft.hidden.contains(*path) && !self.draft.by_path.contains_key(*path)
        });
        let all: BTreeMap<&String, &NodeId> = drafted.chain(kept).collect();
        all.into_iter().map(|(p, id)| (p.as_str(), *id))
    }

    /// The node a reading asks for, by instance path or by the file a composer wrote.
    pub fn resolve(&self, path: &str) -> Result<NodeId, EngineError> {
        if let Some(id) = self.id(path) {
            return Ok(id);
        }
        let held = self.files.get(path).cloned().unwrap_or_default();
        crate::instantiate::sole(
            path,
            held.into_iter()
                .map(|id| (self.name(id).to_string(), id))
                .collect(),
        )
    }

    /// Every `Origin` a term carries was stamped here.
    pub fn locate(&self, origin: Origin) -> Located {
        let held = self.origins.get(origin.token() as usize).copied().flatten();
        held.and_then(|(site, span)| {
            let (name, _) = self.sites[site as usize].as_ref()?;
            Some(Located::at(name.as_str(), span))
        })
        .unwrap_or_default()
    }

    pub(crate) fn mark(&mut self, node: &str, span: Option<sva_ast::ByteSpan>) -> Origin {
        let site = match self.site_ids.get(node) {
            Some(&site) => site,
            None => {
                let held = Some((node.to_string(), 0));
                let site = match self.free_sites.pop() {
                    Some(site) => {
                        self.sites[site as usize] = held;
                        site
                    }
                    None => {
                        self.sites.push(held);
                        (self.sites.len() - 1) as u32
                    }
                };
                self.site_ids.insert(node.to_string(), site);
                site
            }
        };
        self.sites[site as usize].as_mut().expect("a site").1 += 1;
        let token = match self.free_origins.pop() {
            Some(token) => {
                self.origins[token as usize] = Some((site, span));
                token
            }
            None => {
                self.origins.push(Some((site, span)));
                (self.origins.len() - 1) as u32
            }
        };
        let unit = self.unit();
        self.draft.journal.push(Entry::Origin(token, unit));
        Origin::new(token)
    }

    pub(crate) fn seed(&mut self, path: &str, held: Held, grid: Grid) -> NodeId {
        if let Some(id) = self.id(path) {
            return id;
        }
        self.begin(path, grid);
        let id = self.push(
            Node {
                name: path.to_string(),
                ty: Ty {
                    dual: held.is_closed_form(),
                    ..Ty::discrete(held, Codomain::Real)
                },
                var: Var::T,
                value: Value::Op {
                    name: "loop".to_string(),
                    args: Vec::new(),
                },
                grid,
            },
            Some(path),
        );
        self.end();
        self.pending.insert(id);
        self.draft.journal.push(Entry::Pending(id));
        id
    }

    pub(crate) fn pending(&self, id: NodeId) -> bool {
        self.pending.contains(&id)
    }

    /// Settles a seed this draft made.
    pub(crate) fn settle(&mut self, id: NodeId, node: Node) {
        self.nodes[id.0 as usize] = Some(node);
        self.pending.remove(&id);
        self.numbers.0.get_mut().clear();
    }

    pub(crate) fn folded_number(&self, id: NodeId) -> Option<Option<C64>> {
        self.numbers.0.borrow().get(&id).copied()
    }

    pub(crate) fn fold_number(&self, id: NodeId, number: Option<C64>) {
        self.numbers.0.borrow_mut().insert(id, number);
    }

    pub(crate) fn alias(&mut self, path: &str, id: NodeId) {
        let old = self.draft.by_path.insert(path.to_string(), id);
        self.draft.journal.push(Entry::Path(path.to_string(), old));
    }

    pub(crate) fn push(&mut self, node: Node, path: Option<&str>) -> NodeId {
        let id = match self.free.pop() {
            Some(at) => {
                self.nodes[at as usize] = Some(node);
                NodeId(at)
            }
            None => {
                self.nodes.push(Some(node));
                NodeId((self.nodes.len() - 1) as u32)
            }
        };
        let unit = self.unit();
        self.draft.journal.push(Entry::Node(id, unit));
        if let Some(path) = path {
            self.alias(path, id);
        }
        id
    }

    /// What is made until `end` is `path`'s, on `grid`.
    pub(crate) fn begin(&mut self, path: &str, grid: Grid) {
        self.making.push((path.to_string(), grid));
    }

    pub(crate) fn end(&mut self) {
        self.making.pop();
    }

    fn unit(&self) -> (String, Grid) {
        self.making
            .last()
            .cloned()
            .unwrap_or_else(|| (String::new(), Grid::of(1)))
    }

    pub(crate) fn infer_closed_form(&self, form: &ClosedForm) -> Result<Ty, EngineError> {
        infer(form, &Table(&self.nodes)).map_err(|r| {
            EngineError::of_closed_form(
                &r,
                self.locate(r.origin),
                "write the subterm inside sample(...) to leave A deliberately",
            )
        })
    }
}

struct Table<'a>(&'a [Option<Node>]);

impl Env for Table<'_> {
    fn node(&self, id: NodeId) -> Ty {
        self.0[id.0 as usize]
            .as_ref()
            .expect("a node the typing holds")
            .ty
    }

    /// Substituted per instance before a closed form reaches sva-formula, so no term holds one.
    fn param(&self, _: ParamId) -> Ty {
        Ty::form(Var::T, true, Codomain::Real)
    }
}

/// Dependencies first, so a ref reads a type already decided.
pub fn infer_all(inst: &Instances, order: &Order) -> Result<Typing, EngineError> {
    infer_over(inst, order, &BTreeMap::new())
}

/// Each node `stored` names stands as its samples, and nothing under it is typed.
pub(crate) fn infer_over(
    inst: &Instances,
    order: &Order,
    stored: &BTreeMap<String, Arc<Stored>>,
) -> Result<Typing, EngineError> {
    let mut typing = Typing::default();
    typing.lower(inst, &order.groups, stored)?;
    typing.commit(inst);
    Ok(typing)
}

impl Typing {
    /// Lowers `groups`, dependencies first, beside what is held, each path hidden first and each
    /// `stored` names standing as its samples.
    pub(crate) fn lower(
        &mut self,
        inst: &Instances,
        groups: &[Vec<String>],
        stored: &BTreeMap<String, Arc<Stored>>,
    ) -> Result<(), EngineError> {
        self.lowered.clear();
        self.hide(groups.iter().flatten().cloned());
        for group in groups {
            match (crate::schedule::is_loop(inst, group), group.as_slice()) {
                (false, [path]) if let Some(held) = stored.get(path) => {
                    self.begin(path, inst.grid());
                    self.push(standing(path, held), Some(path));
                    self.end();
                }
                (true, _) => settle_loop(self, inst, group)?,
                (false, _) => {
                    for path in group {
                        lower::node(path, inst, self)?;
                    }
                }
            }
        }
        Ok(())
    }
}

fn standing(path: &str, held: &Arc<Stored>) -> Node {
    Node {
        name: path.to_string(),
        ty: Ty {
            width: held.width,
            rate: held.rate,
            ..Ty::discrete(Held::Sampled, held.codomain)
        },
        var: Var::T,
        value: Value::Stored(Arc::clone(held)),
        grid: held.grid,
    }
}

const SEEDS: [Held; 2] = [Held::Form(Var::T), Held::Sampled];

/// A group's members type together, so the seed a pass starts from is a guess the pass
/// either reaches again or replaces with what its members held.
fn settle_loop(typing: &mut Typing, inst: &Instances, group: &[String]) -> Result<(), EngineError> {
    let mut seed = SEEDS[0];
    let mut refusal = None;
    for _ in 0..=SEEDS.len() {
        let mark = typing.checkpoint();
        for path in group {
            typing.seed(path, seed, inst.grid());
        }
        let walked = group
            .iter()
            .try_for_each(|path| lower::node(path, inst, typing).map(|_| ()));
        match walked {
            Ok(()) => match held_by(typing, group) {
                reached if reached == seed => return Ok(()),
                reached => seed = reached,
            },
            Err(e) => {
                refusal = Some(e);
                seed = elsewhere(seed);
            }
        }
        typing.rollback(mark);
    }
    Err(refusal.unwrap_or_else(|| mixed(group)))
}

/// The seed a refused pass did not try.
fn elsewhere(seed: Held) -> Held {
    match seed {
        Held::Sampled => Held::Form(Var::T),
        _ => Held::Sampled,
    }
}

/// One representation for the whole group: samples where any member reached them.
fn held_by(typing: &Typing, group: &[String]) -> Held {
    let sampled = group
        .iter()
        .filter_map(|path| typing.id(path))
        .any(|id| !typing.ty(id).is_closed_form());
    match sampled {
        true => Held::Sampled,
        false => Held::Form(Var::T),
    }
}

/// A group that settles in no representation holds both.
fn mixed(group: &[String]) -> EngineError {
    let at = group.first().map_or("", String::as_str);
    EngineError::refused(Diagnostic {
        code: "type.samples_in_closed_form".to_string(),
        message: format!(
            "the loop over `{}` holds a closed form and samples at once.",
            group.join("`, `")
        ),
        location: Located::at(at, None),
        help: "write sample(...) on the members that are closed forms, so the whole loop runs \
               on the grid"
            .to_string(),
    })
}