sva-formula 0.7.13

Laws in t and f: the normal form, the rule table, and the observations a law answers without sampling
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
// Concern: content-addresses a SpectralSum under the table version, and draws a seed at a step | Non-concern: what a hash keys (sva-engine) | IO: (&SpectralSum) -> Hash

use std::fmt;

use crate::closed_form::{
    Body, Bound, ClosedForm, Edge, Excitation, Fold, IndexId, ModalBank, Mode, Part, Rational,
    Series, Unary, Var,
};
use crate::complex::{C64, canonical};
use crate::env::NodeId;
use crate::lanes::Lanes;
use crate::run::Mirror;
use crate::spectral_sum::atom::{Singular, SpectralAtom};
use crate::spectral_sum::{Lane, SpectralSum};
use crate::table::TABLE_VERSION;

/// Two independently primed FNV-1a lanes: serving one closed form's samples for another is silent
/// corruption, so the width is set against birthday collisions rather than speed.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Hash(pub u64, pub u64);

impl fmt::Display for Hash {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:016x}{:016x}", self.0, self.1)
    }
}

pub fn hash_spectral_sum(n: &SpectralSum) -> Hash {
    hash_spectral_sum_under(n, TABLE_VERSION)
}

pub fn hash_closed_form(t: &ClosedForm) -> Hash {
    hash_closed_form_under(t, TABLE_VERSION)
}

/// The version is the first field after the tag, so a table bump retires every entry keyed
/// by one of these.
pub fn hash_spectral_sum_under(n: &SpectralSum, table_version: u64) -> Hash {
    let mut s = Sink::new(0x01, table_version);
    s.var(n.var);
    s.u64(n.lanes.len() as u64);
    for lane in &n.lanes {
        s.lane(lane);
    }
    s.finish()
}

pub fn hash_closed_form_under(t: &ClosedForm, table_version: u64) -> Hash {
    let mut s = Sink::new(0x02, table_version);
    s.var(t.var);
    s.formula(&t.body);
    s.finish()
}

/// A closed form that reads other nodes, each ref hashed as what `node` names it: the node's
/// own content, never the number a graph gave it.
pub fn hash_closed_form_with(t: &ClosedForm, node: &mut dyn FnMut(NodeId) -> Hash) -> Hash {
    let mut s = Sink::new(0x04, TABLE_VERSION);
    s.node = Some(node);
    s.var(t.var);
    s.formula(&t.body);
    s.finish()
}

/// The unit-interval value one seed draws at one whole step, wherever the pair is written.
pub fn draw(seed: u64, step: i64) -> f64 {
    mix(mix(seed ^ DRAWN) ^ step as u64) as f64 / u64::MAX as f64
}

const DRAWN: u64 = 0x9e37_79b9_7f4a_7c15;

/// The draw at the whole step nearest `key`, ties to even; `None` past any `i64` step.
pub fn draw_nearest(seed: u64, key: f64) -> Option<f64> {
    let step = key.round_ties_even();
    let held = step >= i64::MIN as f64 && step < i64::MAX as f64;
    held.then(|| draw(seed, step as i64))
}

struct Sink<'a> {
    lanes: Lanes<0>,
    node: Option<&'a mut dyn FnMut(NodeId) -> Hash>,
    /// The series indices bound around the term being hashed, innermost last: an index is
    /// hashed by which binder it names, never by the number a typing drew for it.
    bound: Vec<IndexId>,
}

impl<'a> Sink<'a> {
    fn new(tag: u8, table_version: u64) -> Sink<'a> {
        let mut s = Sink {
            lanes: Lanes::default(),
            node: None,
            bound: Vec::new(),
        };
        s.byte(tag);
        s.u64(table_version);
        s
    }

    fn byte(&mut self, b: u8) {
        self.lanes.word(u64::from(b));
    }

    fn u64(&mut self, v: u64) {
        for b in v.to_le_bytes() {
            self.byte(b);
        }
    }

    fn i64(&mut self, v: i64) {
        self.u64(v as u64);
    }

    fn f64(&mut self, v: f64) {
        self.u64(canonical(v));
    }

    fn c64(&mut self, v: C64) {
        let (re, im) = v.bits();
        self.u64(re);
        self.u64(im);
    }

    fn var(&mut self, v: Var) {
        self.byte(match v {
            Var::T => 0,
            Var::F => 1,
        });
    }

    fn edge(&mut self, e: Edge) {
        match e {
            Edge::NegInf => self.byte(0),
            Edge::At(bits) => {
                self.byte(1);
                self.u64(bits);
            }
            Edge::PosInf => self.byte(2),
        }
    }

    fn lane(&mut self, lane: &Lane) {
        self.u64(lane.atoms.len() as u64);
        for a in &lane.atoms {
            self.atom(a);
        }
        self.u64(lane.series.len() as u64);
        for s in &lane.series {
            self.series(s);
        }
        self.u64(lane.modal.len() as u64);
        for m in &lane.modal {
            self.modal(m);
        }
    }

    /// `Origin` is excluded: two spellings from different files must hash alike.
    fn atom(&mut self, a: &SpectralAtom) {
        self.c64(a.c);
        self.u64(u64::from(a.poly.degree));
        self.f64(a.poly.at);
        match a.exp {
            None => self.byte(0),
            Some(e) => {
                self.byte(1);
                self.f64(e.sigma);
                self.f64(e.omega);
            }
        }
        match a.gauss {
            None => self.byte(0),
            Some(g) => {
                self.byte(1);
                self.f64(g.a);
                self.f64(g.mu);
            }
        }
        match a.ind {
            None => self.byte(0),
            Some(i) => {
                self.byte(1);
                self.edge(i.l);
                self.edge(i.r);
            }
        }
        match a.pole {
            None => self.byte(0),
            Some(p) => {
                self.byte(1);
                self.c64(p.at);
                self.u64(u64::from(p.order));
                self.byte(u8::from(p.pv));
            }
        }
        match a.sing {
            Singular::Regular => self.byte(0),
            Singular::Delta { at, order } => {
                self.byte(1);
                self.f64(at);
                self.u64(u64::from(order));
            }
        }
    }

    fn series(&mut self, s: &Series) {
        self.i64(s.lo);
        match s.hi {
            Bound::Finite(n) => {
                self.byte(0);
                self.i64(n);
            }
            Bound::Infinite => self.byte(1),
        }
        self.bound.push(s.index);
        self.formula(&s.term.body);
        self.bound.pop();
    }

    fn amps(&mut self, amps: &[C64]) {
        self.u64(amps.len() as u64);
        for a in amps {
            self.c64(*a);
        }
    }

    fn modal(&mut self, m: &ModalBank) {
        self.u64(m.modes.len() as u64);
        for Mode {
            omega,
            tau,
            amp,
            phase,
        } in &m.modes
        {
            self.f64(*omega);
            self.f64(*tau);
            self.f64(*amp);
            self.f64(*phase);
        }
        match m.excite {
            Excitation::HammerPulse { f0, t0, contact } => {
                self.byte(0);
                self.f64(f0);
                self.f64(t0);
                self.f64(contact);
            }
            Excitation::Impulse { t0 } => {
                self.byte(1);
                self.f64(t0);
            }
        }
    }

    fn parts(&mut self, parts: &[Part]) {
        self.u64(parts.len() as u64);
        for p in parts {
            self.formula(&p.body);
        }
    }

    fn rational(&mut self, r: &Rational) {
        self.u64(r.zeros.len() as u64);
        for z in &r.zeros {
            self.c64(*z);
        }
        self.u64(r.poles.len() as u64);
        for p in &r.poles {
            self.c64(*p);
        }
        self.c64(r.gain);
    }

    fn formula(&mut self, f: &Body) {
        match f {
            Body::Const(c) => {
                self.byte(0x10);
                self.c64(*c);
            }
            Body::Line => self.byte(0x11),
            Body::Index(i) => {
                let depth = self.bound.iter().rev().position(|b| b == i);
                self.byte(0x12);
                self.u64(depth.expect("an index inside the series binding it") as u64);
            }
            Body::Param(p) => {
                self.byte(0x13);
                self.u64(u64::from(p.0));
            }
            Body::Node(n) => {
                self.byte(0x14);
                match self.node.as_mut().map(|named| named(*n)) {
                    Some(held) => {
                        self.u64(held.0);
                        self.u64(held.1);
                    }
                    None => self.u64(u64::from(n.0)),
                }
            }
            Body::Add(parts) => {
                self.byte(0x15);
                self.parts(parts);
            }
            Body::Mul(parts) => {
                self.byte(0x16);
                self.parts(parts);
            }
            Body::Div(a, b) => {
                self.byte(0x17);
                self.formula(&a.body);
                self.formula(&b.body);
            }
            Body::Pow(base, n) => {
                self.byte(0x18);
                self.formula(&base.body);
                self.i64(i64::from(*n));
            }
            Body::Apply(op, arg) => {
                self.byte(0x19);
                self.byte(unary_tag(*op));
                self.formula(&arg.body);
            }
            Body::Fold(op, args) => {
                self.byte(0x1a);
                self.byte(match op {
                    Fold::Max => 0,
                    Fold::Min => 1,
                    Fold::Mod => 2,
                });
                self.parts(args);
            }
            Body::Delta { at, order } => {
                self.byte(0x1b);
                self.formula(&at.body);
                self.u64(u64::from(*order));
            }
            Body::Pv(at) => {
                self.byte(0x1c);
                self.formula(&at.body);
            }
            Body::Warp { at, of } => {
                self.byte(0x27);
                self.formula(&at.body);
                self.formula(&of.body);
            }
            Body::Shift { by, of } => {
                self.byte(0x1d);
                self.f64(*by);
                self.formula(&of.body);
            }
            Body::Deriv { order, of } => {
                self.byte(0x1e);
                self.u64(u64::from(*order));
                self.formula(&of.body);
            }
            Body::Crop {
                of,
                l,
                r,
                rise,
                fall,
            } => {
                self.byte(0x1f);
                self.formula(&of.body);
                self.edge(*l);
                self.edge(*r);
                self.f64(*rise);
                self.f64(*fall);
            }
            Body::Join(parts) => {
                self.byte(0x21);
                self.parts(parts);
            }
            Body::Channel(of, k) => {
                self.byte(0x22);
                self.formula(&of.body);
                self.byte(*k);
            }
            Body::Rational(r) => {
                self.byte(0x23);
                self.rational(r);
            }
            Body::Series(s) => {
                self.byte(0x24);
                self.series(s);
            }
            Body::Modal(m) => {
                self.byte(0x25);
                self.modal(m);
            }
            Body::Run(run) => {
                self.byte(0x28);
                self.f64(run.offset);
                self.f64(run.step);
                self.i64(run.first);
                self.amps(&run.amps);
                match &run.mirror {
                    Mirror::None => self.byte(0),
                    Mirror::Conjugate => self.byte(1),
                    Mirror::Held(amps) => {
                        self.byte(2);
                        self.amps(amps);
                    }
                }
            }
            Body::Keyed { seed, of } => {
                self.byte(0x26);
                self.u64(*seed);
                self.formula(&of.body);
            }
        }
    }

    /// One avalanche past the lanes, so a short input still fills both words.
    fn finish(&self) -> Hash {
        let Hash(a, b) = self.lanes.finish();
        Hash(mix(a), mix(b))
    }
}

fn unary_tag(op: Unary) -> u8 {
    match op {
        Unary::Sin => 0,
        Unary::Cos => 1,
        Unary::Exp => 2,
        Unary::Tanh => 3,
        Unary::Sat => 4,
        Unary::Abs => 5,
        Unary::Log => 6,
        Unary::Sqrt => 7,
        Unary::Step => 8,
    }
}

fn mix(mut z: u64) -> u64 {
    z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
    z ^ (z >> 31)
}