sva-engine 0.7.11

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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// Concern: the formula each written call lowers to | Non-concern: the recursion over an Expr (mod.rs) | IO: (name, args) -> a Piece

use sva_ast::{Arg, ByteSpan, Expr};
use sva_formula::{
    Body, C64, Codomain, Edge, Fold, Held, NodeId, Origin, Part, Ty, Unary, Var, hash,
};
use sva_samples::physics::Varies;

use crate::arguments::{Argument, Called, Chosen};
use crate::cast::Cast;
use crate::error::EngineError;
use crate::instantiate::Cx;
use crate::lower::{Lowering, Piece};
use crate::time::Lattice;
use crate::typing::{Value, When};
use sva_ast::SERIES;

impl Lowering<'_> {
    pub(super) fn call(
        &mut self,
        name: &str,
        args: &[Arg],
        span: ByteSpan,
        cx: Cx,
        var: Var,
    ) -> Result<Piece, EngineError> {
        if name == SERIES {
            return self.sum(args, span, cx, var);
        }
        if name == sva_ast::INDEX {
            return Err(self.refused_at(
                "type.index_outside_read",
                "idx(...) names a sample index, which only an index read takes".to_string(),
                "read by index, as @x[idx(t - 0.5b)], or write the time itself",
                Some(span),
            ));
        }
        let written = args.iter().filter(|a| matches!(a, Arg::Pos(_))).count();
        let keys: Vec<String> = args
            .iter()
            .filter_map(|a| match a {
                Arg::Named(k, _) => Some(k.clone()),
                Arg::Pos(_) => None,
            })
            .collect();
        crate::overload::check_arity(name, written, &keys)
            .map_err(|m| self.refuse(name, &m, Some(span)))?;
        let mut chosen = Vec::new();
        let named = self.named_values(name, args, span, cx, &mut chosen)?;
        let view: Vec<(&str, f64)> = named.iter().map(|(k, v)| (k.as_str(), *v)).collect();
        let solved = sva_ast::FINITE_DIFFERENCE.contains(&name);
        if solved || sva_ast::MODAL.contains(&name) {
            let Some(numbers) = positional_values(self, args, cx, &mut chosen) else {
                return Err(EngineError::BadArity(name.to_string()));
            };
            if !solved {
                self.note_call(name, span, positional_named(name, &numbers, &named), chosen);
                return self.modal(name, &numbers, &view);
            }
            return self.solver(name, (&numbers, &view, args), span, chosen, (cx, var));
        }
        if name == "noise" {
            let Some(numbers) = positional_values(self, args, cx, &mut chosen) else {
                return Err(EngineError::BadArity(name.to_string()));
            };
            let [seed, ..] = numbers.as_slice() else {
                return Err(EngineError::BadArity(name.to_string()));
            };
            self.note_call(name, span, positional_named(name, &numbers, &named), chosen);
            return Ok(Piece::ClosedForm(Body::Series(Box::new(
                sva_formula::noise(
                    *seed as u64,
                    named_or(&view, "period", 1.0),
                    named_or(&view, "color", 0.0),
                ),
            ))));
        }
        self.note_call(name, span, written_named(&named), chosen);
        if let Some(cast) = Cast::from_name(name) {
            let cast = match cast {
                Cast::Stft { .. } => Cast::Stft {
                    window: self.step_count(args, "window", span, cx)?,
                    hop: self.step_count(args, "hop", span, cx)?,
                },
                other => other,
            };
            return self.cast(cast, args, span, cx, var);
        }
        if let Some(shape) = crate::vocabulary::shape(name) {
            return self.filter(shape, args, span, cx, var);
        }
        if name == "rand" {
            return self.drawn(args, span, cx, var);
        }
        let positional: Vec<&Expr> = args
            .iter()
            .filter_map(|a| match a {
                Arg::Pos(x) => Some(x),
                Arg::Named(..) => None,
            })
            .collect();
        if let Some(piece) = self.wave(name, &positional, &view, cx, var)? {
            return Ok(piece);
        }
        let mut pieces = Vec::with_capacity(positional.len());
        for x in &positional {
            pieces.push(self.walk(x, cx, var)?);
        }
        if let Some(held) = beside_infinity(name, &mut pieces) {
            return Ok(held);
        }
        if pieces.iter().any(|p| matches!(p, Piece::Value(_))) {
            return match (name, view.iter().find(|(k, _)| *k == "drive")) {
                ("sat", Some((_, drive))) => self.driven(pieces, *drive, span, var),
                ("crop", _) => self.sampled_crop(pieces, &view, span, var),
                _ => self.operation(name, pieces, Some(span), var),
            };
        }
        let bodies: Vec<Body> = pieces
            .into_iter()
            .map(|p| match p {
                Piece::ClosedForm(f) => f,
                Piece::Value(_) => unreachable!("every piece here stayed inside the closed form"),
            })
            .collect();
        self.image(name, bodies, &positional, &view, span, var)
    }

    /// The atoms a written name lowers to, once every operand is inside the same closed form.
    fn image(
        &mut self,
        name: &str,
        bodies: Vec<Body>,
        written: &[&Expr],
        named: &[(&str, f64)],
        span: ByteSpan,
        var: Var,
    ) -> Result<Piece, EngineError> {
        let read = |key: &str, fallback: f64| named_or(named, key, fallback);
        let arity = |want: usize| -> Result<(), EngineError> {
            match bodies.len() == want {
                true => Ok(()),
                false => Err(EngineError::BadArity(name.to_string())),
            }
        };
        if numeric(name) {
            let origin = self.typing.mark(self.node, Some(span));
            let two = bodies.len() == 2;
            return match arithmetic(name, bodies, named, var, origin) {
                Some(body) => self.folded(body, var),
                None if name == "pow" && two => Err(self.non_integer_power(written, span)),
                None => Err(EngineError::BadArity(name.to_string())),
            };
        }
        match name {
            "crop" => {
                arity(3)?;
                let (rise, fall) = shoulders_of(named);
                let Some((l, r)) = self.cropped(&bodies[1], &bodies[2], (rise, fall), var, span)?
                else {
                    return Ok(Piece::ClosedForm(Body::Const(C64::ZERO)));
                };
                let fall = match r {
                    Edge::PosInf => 0.0,
                    _ => fall,
                };
                let of = self.part(first(bodies), Some(span));
                Ok(Piece::ClosedForm(Body::Crop {
                    of,
                    l,
                    r,
                    rise,
                    fall,
                }))
            }
            "delta" => {
                arity(1)?;
                let at = self.part(first(bodies), Some(span));
                Ok(Piece::ClosedForm(Body::Delta {
                    at,
                    order: read("k", 0.0) as u16,
                }))
            }
            "pv" => {
                arity(1)?;
                let at = self.part(first(bodies), Some(span));
                Ok(Piece::ClosedForm(Body::Pv(at)))
            }
            "join" => {
                let parts = self.parts(bodies, span);
                Ok(Piece::ClosedForm(Body::Join(parts)))
            }
            "ch" => {
                arity(2)?;
                let Some(index) = super::constant_value(&bodies[1], var) else {
                    return Err(EngineError::BadArity(name.to_string()));
                };
                let of = self.part(first(bodies), Some(span));
                Ok(Piece::ClosedForm(Body::Channel(of, index as u8)))
            }
            other => Err(EngineError::UnknownBuiltin(other.to_string())),
        }
    }

    /// The product `arithmetic` writes, so the drive reaches the renderer and the identity.
    fn driven(
        &mut self,
        mut pieces: Vec<Piece>,
        drive: f64,
        span: ByteSpan,
        var: Var,
    ) -> Result<Piece, EngineError> {
        pieces.push(Piece::ClosedForm(Body::Const(C64::real(drive))));
        let product = self.operation("*", pieces, Some(span), var)?;
        self.operation("sat", vec![product], Some(span), var)
    }

    /// A closed form's window: its shoulders, refused alike, ride along as two numbers.
    fn sampled_crop(
        &mut self,
        mut pieces: Vec<Piece>,
        named: &[(&str, f64)],
        span: ByteSpan,
        var: Var,
    ) -> Result<Piece, EngineError> {
        let (rise, fall) = shoulders_of(named);
        if let [_, Piece::ClosedForm(l), Piece::ClosedForm(r)] = pieces.as_slice()
            && self.cropped(l, r, (rise, fall), var, span)?.is_none()
        {
            return Ok(Piece::ClosedForm(Body::Const(C64::ZERO)));
        }
        if rise > 0.0 || fall > 0.0 {
            for shoulder in [rise, fall] {
                pieces.push(Piece::ClosedForm(Body::Const(C64::real(shoulder))));
            }
        }
        self.operation("crop", pieces, Some(span), var)
    }

    fn non_integer_power(&self, written: &[&Expr], span: ByteSpan) -> EngineError {
        let [_, exponent] = written else {
            unreachable!("pow checked its arity before it lowered either operand")
        };
        self.refused_at(
            "type.non_integer_power",
            format!(
                "`pow` raises a signal to `{}`, which names no polynomial power.",
                sva_ast::render_expr(exponent)
            ),
            "write a whole exponent within 65535, or a positive constant base, which raises \
             as exp(x*ln(base))",
            Some(span),
        )
    }

    /// A window's bounds are two numbers, and a constant expression is one: every ref
    /// naming a number folds first, so `@variables/bar*3` is the second a bar names. Only a
    /// bound that moves with the free variable has no number to be.
    fn window(
        &mut self,
        l: &Body,
        r: &Body,
        var: Var,
        name: &str,
        span: ByteSpan,
    ) -> Result<(Edge, Edge), EngineError> {
        Ok((
            self.edge(l, var, name, "start", span)?,
            self.edge(r, var, name, "end", span)?,
        ))
    }

    /// A crop's window and shoulders, for either representation of its operand; `None`
    /// where the window opens at or after it closes, which is zero whatever it crops.
    fn cropped(
        &mut self,
        l: &Body,
        r: &Body,
        (rise, fall): (f64, f64),
        var: Var,
        span: ByteSpan,
    ) -> Result<Option<(Edge, Edge)>, EngineError> {
        let (l, r) = self.window(l, r, var, "crop", span)?;
        if l >= r {
            return Ok(None);
        }
        self.shoulders(rise, fall, r.value() - l.value(), span)?;
        Ok(Some((l, r)))
    }

    /// A shoulder opens inside the window it belongs to. Zero is a hard edge, a negative one
    /// opens before the window starts, and two that overlap add energy the term never had.
    fn shoulders(&self, rise: f64, fall: f64, span: f64, at: ByteSpan) -> Result<(), EngineError> {
        let refuse = |what: String| {
            Err(self.refused_at(
                "engine.bad_crop_shoulder",
                what,
                "write a rise and a fall at or above zero that together fit the window",
                Some(at),
            ))
        };
        if rise < 0.0 || fall < 0.0 {
            return refuse(format!(
                "`crop` reads rise={rise} and fall={fall}; a shoulder \
                 opens forward in time."
            ));
        }
        if rise + fall > span {
            return refuse(format!(
                "`crop`'s rise={rise} and fall={fall} are {} together, longer than the {span} \
                 the window itself runs.",
                rise + fall
            ));
        }
        Ok(())
    }

    fn edge(
        &self,
        body: &Body,
        var: Var,
        name: &str,
        which: &str,
        span: ByteSpan,
    ) -> Result<Edge, EngineError> {
        let folded = crate::refs::fold_constants(self.typing, body);
        let infinite = match *folded {
            Body::Const(c) if c.im == 0.0 && c.re.is_infinite() => Some(c.re),
            _ => None,
        };
        match infinite.or_else(|| super::constant_value(&folded, var)) {
            Some(x) => Ok(Edge::at(x)),
            None => Err(self.refused_at(
                "engine.non_constant_argument",
                format!("`{name}` reads `{which}` as a number, and this one moves."),
                "write a constant there; a bound that moves with t names no window",
                Some(span),
            )),
        }
    }

    fn parts(&mut self, bodies: Vec<Body>, span: ByteSpan) -> Vec<Part> {
        bodies
            .into_iter()
            .map(|b| self.part(b, Some(span)))
            .collect()
    }

    /// An argument that folds to inf, or to no number, names none any builtin reads.
    pub(super) fn finite_argument(
        &self,
        name: &str,
        key: &str,
        written: &Expr,
        folded: Option<f64>,
    ) -> Result<(), EngineError> {
        match folded {
            Some(v) if !v.is_finite() => Err(self.infinite(&format!(
                "`{name}` reads `{key}={}`, which is {v}, and an argument is a finite number",
                sva_ast::render_expr(written)
            ))),
            _ => Ok(()),
        }
    }

    /// Every named number a call reads. One that folds to inf is refused; one the vocabulary
    /// lets move is its builtin's to route when it folds to none; any other is refused.
    fn named_values(
        &self,
        name: &str,
        args: &[Arg],
        span: ByteSpan,
        cx: Cx,
        chosen: &mut Vec<Chosen>,
    ) -> Result<Vec<(String, f64)>, EngineError> {
        let mut out = Vec::new();
        for arg in args {
            let Arg::Named(key, value) = arg else {
                continue;
            };
            let folded = crate::loops::amount_choosing(self.inst, value, cx, chosen);
            self.finite_argument(name, key, value, folded)?;
            match folded.and_then(crate::loops::plain) {
                Some(v) => out.push((key.clone(), v)),
                None if crate::vocabulary::named_may_move(name, key) => {}
                None => {
                    return Err(self.refused_at(
                        "engine.non_constant_argument",
                        format!(
                            "`{name}` reads `{key}={}` as one number, and it names none.",
                            sva_ast::render_expr(value)
                        ),
                        "write a constant there: a number, a unit, or a ref naming one",
                        Some(span),
                    ));
                }
            }
        }
        Ok(out)
    }

    fn chosen_value(&self, e: &Expr, cx: Cx, chosen: &mut Vec<Chosen>) -> Option<f64> {
        crate::loops::plain(crate::loops::amount_choosing(self.inst, e, cx, chosen)?)
    }

    /// The numbers a call was lowered with, where it was written, and what each constant
    /// `min`/`max` inside them chose.
    fn note_call(
        &mut self,
        name: &str,
        at: ByteSpan,
        arguments: Vec<Argument>,
        chosen: Vec<Chosen>,
    ) {
        if arguments.is_empty() && chosen.is_empty() || !self.grid.is_rate() {
            return;
        }
        let call = (!arguments.is_empty()).then(|| Called {
            name: name.to_string(),
            at,
            arguments,
        });
        self.typing.note(self.node, call, chosen);
    }

    /// Checked in range before its grid is sized from it, then noted and registered. A
    /// varying parameter that names no number is a node the site reads every sample.
    fn solver(
        &mut self,
        name: &str,
        (numbers, view, args): (&[f64], &[(&str, f64)], &[Arg]),
        span: ByteSpan,
        chosen: Vec<Chosen>,
        (cx, var): (Cx, Var),
    ) -> Result<Piece, EngineError> {
        let [first, ..] = numbers else {
            return Err(EngineError::BadArity(name.to_string()));
        };
        let mut params = super::solvers::params(name, *first, view);
        let mut varying = Vec::new();
        for (key, varies) in sva_samples::physics::varying(name) {
            let written = args.iter().find_map(|a| match a {
                Arg::Named(k, x) if k == key => Some(x),
                _ => None,
            });
            let Some(written) = written.filter(|_| !view.iter().any(|(k, _)| k == key)) else {
                continue;
            };
            let id = self.automation((name, key), written, cx, var)?;
            if let Value::ClosedForm(form) = self.typing.value(id)
                && let Some(v) = super::constant_value(&form.body, form.var)
            {
                *super::field(&mut params, key).expect("a varying field") = v + 0.0;
                continue;
            }
            if *varies == Varies::Piecewise && !self.piecewise(id) {
                return Err(self.moving_energy(name, key, span));
            }
            varying.push((*key, id));
        }
        if !params.valid() {
            return Err(EngineError::refused(crate::error::Diagnostic {
                code: "engine.physics_out_of_range".to_string(),
                message: format!("`{name}` was given an argument outside the range it models"),
                location: crate::error::Located::at(name, Some(span)),
                help: "`sva-cli builtins` names every argument each solver takes".to_string(),
            }));
        }
        let handed = super::solvers::handed(&params)
            .into_iter()
            .filter(|(key, _)| !varying.iter().any(|(k, _)| k == key))
            .enumerate()
            .map(|(at, (key, value))| Argument {
                written: at == 0 || view.iter().any(|(k, _)| *k == key),
                name: key,
                value,
            })
            .collect();
        self.note_call(name, span, handed, chosen);
        let value = Value::Solver {
            params: Box::new(params),
            varying,
        };
        let ty = Ty::discrete(Held::Sampled, Codomain::Real);
        Ok(Piece::Value(self.register(value, ty, var)))
    }

    /// A stored-energy coefficient holds between jumps: numbers under windows that open and
    /// close without shoulders, and arithmetic over them.
    fn piecewise(&self, id: NodeId) -> bool {
        fn steps(f: &Body) -> bool {
            match f {
                Body::Const(_) => true,
                Body::Crop { of, rise, fall, .. } => {
                    *rise == 0.0 && *fall == 0.0 && steps(&of.body)
                }
                Body::Add(parts) | Body::Mul(parts) | Body::Fold(Fold::Max | Fold::Min, parts) => {
                    parts.iter().all(|p| steps(&p.body))
                }
                Body::Div(a, b) => steps(&a.body) && super::constant::is_constant(&b.body),
                _ => false,
            }
        }
        crate::refs::substituted_closed_form(self.typing, id).is_some_and(|f| steps(&f.body))
    }

    fn moving_energy(&self, name: &str, key: &str, span: ByteSpan) -> EngineError {
        self.refused_at(
            "engine.moving_energy_parameter",
            format!(
                "`{name}` stores energy in `{key}`, which may only jump: a coefficient that \
                 moves between jumps can pump energy into the scheme"
            ),
            "write it as numbers under crops that open and close without shoulders, as \
             k*step(t - t0)",
            Some(span),
        )
    }

    /// A number a call reads as an argument. A grid count is not one: `1sp` names no
    /// cutoff, so the same fold that reads a delay answers `None` for it here.
    pub(super) fn named_value(&self, e: &Expr, cx: Cx) -> Option<f64> {
        crate::loops::plain(crate::loops::amount(self.inst, e, cx)?)
    }

    /// A duration a cast counts in steps of the rate in use, which has to be a whole number of
    /// them; zero where it is not written.
    fn step_count(
        &self,
        args: &[Arg],
        key: &str,
        span: ByteSpan,
        cx: Cx,
    ) -> Result<usize, EngineError> {
        let Some(written) = args.iter().find_map(|a| match a {
            Arg::Named(k, x) if k == key => Some(x),
            _ => None,
        }) else {
            return Ok(0);
        };
        let count = crate::loops::time_of(self.inst, written, cx)
            .filter(|at| at.scale.is_zero())
            .and_then(|at| at.shift.div(self.grid.steps(crate::time::Q::ONE)?))
            .filter(|n| n.is_integer() && n.num() > 0);
        match count {
            Some(n) => Ok(n.num() as usize),
            None => Err(self.refused_at(
                "cast.window_off_the_lattice",
                format!(
                    "`stft`'s `{key}` is no whole number of steps at this rate: {}",
                    sva_ast::render_expr(written)
                ),
                "write it in sp, as window=2048sp",
                Some(span),
            )),
        }
    }

    /// `rand(key, seed=)` is white noise, one draw per step of the rate in use keyed by that
    /// step's index, read at the step nearest the key: a constant key is one number, `t` the
    /// noise itself, and any other time a read of it.
    fn drawn(
        &mut self,
        args: &[Arg],
        span: ByteSpan,
        cx: Cx,
        var: Var,
    ) -> Result<Piece, EngineError> {
        let bad = || EngineError::BadArity("rand".to_string());
        let (key, seed) = rand_arguments(args, |x| self.named_value(x, cx)).ok_or_else(bad)?;
        let ty = sva_formula::Ty::discrete(sva_formula::Held::Sampled, sva_formula::Codomain::Real);
        let (grid, at) = match crate::loops::time_of(self.inst, key, cx) {
            Some(at) if at.scale.is_zero() => {
                let drawn = noise_at(seed, at.shift, self.inst.rate());
                return Ok(Piece::ClosedForm(Body::Const(C64::real(drawn))));
            }
            Some(at) => {
                let unplaced = || EngineError::BadArity("rand".to_string());
                (self.grid.speed(at).ok_or_else(unplaced)?, When::At(at))
            }
            None => (self.grid, When::Moving(self.time(key, cx)?)),
        };
        let noise = self.typing.push(
            crate::typing::Node {
                name: self.node.to_string(),
                ty,
                var,
                value: Value::Noise(seed),
                grid,
            },
            None,
        );
        match at {
            When::At(crate::time::Affine::NOW) => Ok(Piece::Value(noise)),
            at => Ok(Piece::Value(self.reading(noise, at, span, var))),
        }
    }
}

/// The key and the seed of `rand(key, seed)` or `rand(key, seed=)`.
pub(crate) fn rand_arguments(
    args: &[Arg],
    number: impl Fn(&Expr) -> Option<f64>,
) -> Option<(&Expr, u64)> {
    let mut positional = args.iter().filter_map(|a| match a {
        Arg::Pos(x) => Some(x),
        Arg::Named(..) => None,
    });
    let key = positional.next()?;
    let seed = match positional.next() {
        Some(x) => number(x)?,
        None => match args.iter().find_map(|a| match a {
            Arg::Named(k, x) if k == "seed" => Some(x),
            _ => None,
        }) {
            Some(x) => number(x)?,
            None => 0.0,
        },
    };
    Some((key, seed as u64))
}

/// The noise's draw at the step of `rate` nearest an instant, ties to even.
pub(crate) fn noise_at(seed: u64, at: crate::time::Q, rate: u32) -> f64 {
    match at
        .mul(crate::time::Q::int(i64::from(rate)))
        .and_then(crate::time::nearest)
    {
        Some(step) => hash::draw(seed, step),
        None => f64::NAN,
    }
}

/// FORMAT 3.3's arithmetic row: a closed form of its operands alone, so one image serves a
/// lowered call and a folded constant.
pub(super) fn numeric(name: &str) -> bool {
    Unary::from_name(name).is_some() || matches!(name, "max" | "min" | "pow")
}

/// The closed form a numeric call names, built once for the lowering and for the constant fold, so
/// an argument folded before a call and the same argument lowered agree by construction.
pub(super) fn arithmetic(
    name: &str,
    bodies: Vec<Body>,
    named: &[(&str, f64)],
    var: Var,
    origin: Origin,
) -> Option<Body> {
    let bodies = match <[Body; 1]>::try_from(bodies) {
        Ok([x]) => return unary(name, x, named, origin),
        Err(bodies) => bodies,
    };
    let [a, b] = <[Body; 2]>::try_from(bodies).ok()?;
    let fold = |op: Fold, a: Body, b: Body| {
        Body::Fold(op, vec![Part::new(origin, a), Part::new(origin, b)])
    };
    match name {
        "max" => Some(fold(Fold::Max, a, b)),
        "min" => Some(fold(Fold::Min, a, b)),
        "pow" => power(a, b, var, origin),
        _ => None,
    }
}

fn unary(name: &str, x: Body, named: &[(&str, f64)], origin: Origin) -> Option<Body> {
    let part = |f: Body| Part::new(origin, f);
    if name == "sat" {
        let drive = Body::Const(C64::real(named_or(named, "drive", 1.0)));
        let driven = Body::Mul(vec![part(x), part(drive)]);
        return Some(Body::Apply(Unary::Sat, part(driven)));
    }
    if name == "step"
        && let Some(at) = rising_zero(&x)
    {
        return Some(Body::Crop {
            of: part(Body::Const(C64::ONE)),
            l: Edge::at(at),
            r: Edge::PosInf,
            rise: 0.0,
            fall: 0.0,
        });
    }
    Unary::from_name(name).map(|op| Body::Apply(op, part(x)))
}

/// The one operand a call of one argument reads, moved out of the operands it was lowered in.
fn first(bodies: Vec<Body>) -> Body {
    bodies
        .into_iter()
        .next()
        .expect("arity checked before the operand is read")
}

/// `min(x, inf)` is `x` and `min(x, -inf)` is `-inf`, `max` alike, whatever `x` holds.
fn beside_infinity(name: &str, pieces: &mut Vec<Piece>) -> Option<Piece> {
    let absorbs = match name {
        "min" => f64::NEG_INFINITY,
        "max" => f64::INFINITY,
        _ => return None,
    };
    let infinite = |p: &Piece| match p {
        Piece::ClosedForm(Body::Const(c)) if c.im == 0.0 && c.re.is_infinite() => Some(c.re),
        _ => None,
    };
    let at = pieces.iter().position(|p| infinite(p).is_some())?;
    if pieces.len() != 2 {
        return None;
    }
    let held = infinite(&pieces[at]).expect("an infinite operand");
    match held == absorbs {
        true => Some(pieces.swap_remove(at)),
        false => Some(pieces.swap_remove(1 - at)),
    }
}

/// Where `a*t + b` with `a > 0` crosses zero, so `step` of it is the crop opening there.
fn rising_zero(x: &Body) -> Option<f64> {
    let (a, b) = sva_formula::affine::exact_affine(x)?;
    let (a, b) = (a.is_real().then_some(a.re)?, b.is_real().then_some(b.re)?);
    let at = -b / a;
    (a > 0.0 && !at.is_nan()).then_some(at)
}

/// An integer exponent is a polynomial factor and stays one. A real exponent is
/// `exp(x*ln(b))`, which only a positive base has: a constant one folds here, a signal one
/// becomes that exponential, and anything else has no value to approximate.
fn power(base: Body, exponent: Body, var: Var, origin: Origin) -> Option<Body> {
    let part = |f: Body| Part::new(origin, f);
    let n = super::constant_value(&exponent, var);
    if let Some(n) = n.filter(|n| whole(*n)) {
        return Some(Body::Pow(part(base), n as i32));
    }
    let b = super::constant_value(&base, var).filter(|b| *b > 0.0)?;
    if let Some(n) = n {
        return Some(Body::Const(C64::real(b.powf(n))));
    }
    let rate = Body::Const(C64::real(b.ln()));
    let scaled = Body::Mul(vec![part(exponent), part(rate)]);
    Some(Body::Apply(Unary::Exp, part(scaled)))
}

/// Whether a real exponent names a polynomial power an atom's u16 order holds.
fn whole(n: f64) -> bool {
    n.fract() == 0.0 && n.abs() <= f64::from(u16::MAX)
}

fn shoulders_of(named: &[(&str, f64)]) -> (f64, f64) {
    (named_or(named, "rise", 0.0), named_or(named, "fall", 0.0))
}

/// A named argument's number, or what the builtin takes when the call left it out.
pub(super) fn named_or(named: &[(&str, f64)], key: &str, fallback: f64) -> f64 {
    named
        .iter()
        .find(|(k, _)| *k == key)
        .map_or(fallback, |(_, v)| *v)
}

/// Every positional argument as a number, in written order. A positional this cannot fold
/// is not skipped: skipping one would move every argument after it into the wrong field.
fn positional_values(
    low: &Lowering,
    args: &[Arg],
    cx: Cx,
    chosen: &mut Vec<Chosen>,
) -> Option<Vec<f64>> {
    args.iter()
        .filter_map(|a| match a {
            Arg::Pos(x) => Some(low.chosen_value(x, cx, chosen)),
            Arg::Named(..) => None,
        })
        .collect()
}

fn written_named(named: &[(String, f64)]) -> Vec<Argument> {
    named
        .iter()
        .map(|(name, value)| Argument {
            name: name.clone(),
            value: *value,
            written: true,
        })
        .collect()
}

/// A modal bank's positionals under the names its signature gives them, then its named ones.
fn positional_named(name: &str, numbers: &[f64], named: &[(String, f64)]) -> Vec<Argument> {
    let params = crate::overload::signature(name).map_or(&[][..], |s| s.params);
    let positional = params.iter().zip(numbers).map(|(p, value)| Argument {
        name: p.name.to_string(),
        value: *value,
        written: true,
    });
    positional.chain(written_named(named)).collect()
}