sva-formula 0.7.17

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
// Concern: reads each ref of a written form as the form it names, each answer kept per node | Non-concern: which refs a caller lets it read (sva-engine) | IO: (NodeId) -> each reading of its form

use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::ops::Deref;
use std::rc::Rc;

use crate::affine::{Axis, Coeff, Reading, key_moves_read, polynomial_read};
use crate::closed_form::{Body, IndexId, Var, map_children, read_at, read_at_with, shift_line};
use crate::env::NodeId;
use crate::hash::{Hash, hash_time};
use crate::origin::Origin;
use crate::refusal::{Factor, Left, LeftReason};
use crate::series::IndexGrowth;
use crate::spectral_sum::SpectralSum;
use crate::spectral_sum::build::{lower, timeless};
use crate::spectral_sum::image::left;
use crate::spectral_sum::kink;
pub use crate::spectral_sum::kink::{Crossing, Line};

/// What a ref in a written form answers each reading of it with, as the form it names would.
pub trait Reads {
    fn lowered(&self, id: NodeId, origin: Origin, var: Var) -> Result<SpectralSum, Left>;
    fn polynomial(&self, id: NodeId, reading: Reading) -> Option<Vec<Coeff>>;
    fn timeless(&self, id: NodeId) -> bool;
    fn key_moves(&self, id: NodeId) -> bool;
    fn mentions_line(&self, id: NodeId) -> bool;
    fn axis(&self, id: NodeId) -> Option<Axis>;
    fn line(&self, id: NodeId) -> Option<Line>;
    fn kink(&self, id: NodeId) -> Option<(Body, Crossing)>;
    /// A node standing for `id` with `kink` read as `with` throughout, where it reaches one.
    fn replaced(&self, id: NodeId, scope: usize, kink: &Body, with: &Body) -> Option<NodeId>;
    /// A node standing for `id` read at `at`, the time written through its form.
    fn moved(&self, id: NodeId, at: &Body) -> Option<NodeId>;
    fn stand_for(&self, body: Body) -> Option<NodeId>;
    fn scope(&self) -> usize;
    /// The form `id` names as `read` reads it, one level down: its own refs still refs.
    fn view(&self, id: NodeId, read: &Read) -> Option<Form<'_>>;
    /// How the form `id` names grows in `k`, read at a time `k` moves.
    fn growth(&self, id: NodeId, at: &Body, k: IndexId) -> Option<IndexGrowth>;
}

/// How a ref is read where it stands: as written, at a time, or shifted.
pub enum Read<'t> {
    As,
    At(&'t Body),
    By(f64),
}

/// A ref read as nothing but itself: a name whose form is unknown here.
pub struct Opaque;

impl Reads for Opaque {
    fn lowered(&self, _: NodeId, origin: Origin, _: Var) -> Result<SpectralSum, Left> {
        Err(left(origin, Factor::Value, LeftReason::Unsubstituted))
    }

    fn polynomial(&self, _: NodeId, _: Reading) -> Option<Vec<Coeff>> {
        None
    }

    fn timeless(&self, _: NodeId) -> bool {
        false
    }

    fn key_moves(&self, _: NodeId) -> bool {
        true
    }

    fn mentions_line(&self, _: NodeId) -> bool {
        false
    }

    fn axis(&self, _: NodeId) -> Option<Axis> {
        None
    }

    fn line(&self, _: NodeId) -> Option<Line> {
        None
    }

    fn kink(&self, _: NodeId) -> Option<(Body, Crossing)> {
        None
    }

    fn replaced(&self, _: NodeId, _: usize, _: &Body, _: &Body) -> Option<NodeId> {
        None
    }

    fn moved(&self, _: NodeId, _: &Body) -> Option<NodeId> {
        None
    }

    fn stand_for(&self, _: Body) -> Option<NodeId> {
        None
    }

    fn scope(&self) -> usize {
        0
    }

    fn view(&self, _: NodeId, _: &Read) -> Option<Form<'_>> {
        None
    }

    fn growth(&self, _: NodeId, _: &Body, _: IndexId) -> Option<IndexGrowth> {
        None
    }
}

/// Each reading of each node, kept by whoever holds the nodes for as long as they hold.
#[derive(Debug, Default)]
pub struct Kept {
    lowered: Each<(NodeId, Var), Result<SpectralSum, Left>>,
    polynomials: Each<(NodeId, Reading), Option<Vec<Coeff>>>,
    timeless: Each<NodeId, bool>,
    key_moves: Each<NodeId, bool>,
    mentions_line: Each<NodeId, bool>,
    axes: Each<NodeId, Axis>,
    lines: Each<NodeId, Option<Line>>,
    kinks: Each<NodeId, Option<(Body, Crossing)>>,
    views: Each<(NodeId, View), Rc<Body>>,
    growths: Each<(NodeId, IndexId, Hash), IndexGrowth>,
}

type Each<K, V> = RefCell<HashMap<K, V>>;

/// Which rewritten reading of a form a view is.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum View {
    At(Hash),
    By(u64),
}

impl Kept {
    /// Every answer about a node `gone` names let go; the rest kept.
    pub fn forget(&mut self, gone: &dyn Fn(NodeId) -> bool) {
        self.lowered.get_mut().retain(|(id, _), _| !gone(*id));
        self.polynomials.get_mut().retain(|(id, _), _| !gone(*id));
        self.timeless.get_mut().retain(|id, _| !gone(*id));
        self.key_moves.get_mut().retain(|id, _| !gone(*id));
        self.mentions_line.get_mut().retain(|id, _| !gone(*id));
        self.axes.get_mut().retain(|id, _| !gone(*id));
        self.lines.get_mut().retain(|id, _| !gone(*id));
        self.kinks.get_mut().retain(|id, _| !gone(*id));
        self.views.get_mut().retain(|(id, _), _| !gone(*id));
        self.growths.get_mut().retain(|(id, _, _), _| !gone(*id));
    }
}

/// The written form each node names, `None` where the caller reads no form through it.
pub type Written<'a> = &'a dyn Fn(NodeId) -> Option<(&'a Body, Origin)>;

/// Reads refs through `written`, keeping each node's answers in `kept`. A form rewritten
/// below a ref stands in as a node of its own, numbered from the top of the ids down, kept
/// only while this reading lasts.
pub struct Through<'a> {
    written: Written<'a>,
    kept: &'a Kept,
    standing: Kept,
    stand_ins: RefCell<Vec<Rc<Body>>>,
    replacing: RefCell<HashMap<(NodeId, usize), Option<NodeId>>>,
    moving: Each<NodeId, Vec<(Body, Option<NodeId>)>>,
    scopes: Cell<usize>,
}

/// A form a reader hands out: one a node names, or one standing in for a rewritten form.
pub enum Form<'a> {
    Node(&'a Body),
    Stand(Rc<Body>),
}

impl Deref for Form<'_> {
    type Target = Body;

    fn deref(&self) -> &Body {
        match self {
            Form::Node(body) => body,
            Form::Stand(body) => body,
        }
    }
}

impl<'a> Through<'a> {
    pub fn new(written: Written<'a>, kept: &'a Kept) -> Through<'a> {
        Through {
            written,
            kept,
            standing: Kept::default(),
            stand_ins: RefCell::default(),
            replacing: RefCell::default(),
            moving: RefCell::default(),
            scopes: Cell::new(0),
        }
    }

    fn stand_in(id: NodeId) -> Option<usize> {
        let at = u32::MAX - id.0;
        (at < u32::MAX / 2).then_some(at as usize)
    }

    fn form(&self, id: NodeId) -> Option<(Form<'a>, Origin)> {
        match Through::stand_in(id) {
            Some(at) => {
                let body = Rc::clone(self.stand_ins.borrow().get(at)?);
                Some((Form::Stand(body), Origin::UNKNOWN))
            }
            None => (self.written)(id).map(|(body, origin)| (Form::Node(body), origin)),
        }
    }

    fn kept(&self, id: NodeId) -> &Kept {
        match Through::stand_in(id) {
            Some(_) => &self.standing,
            None => self.kept,
        }
    }

    /// A node of its own standing for `body`, read through like any other.
    pub fn stand(&self, body: Body) -> NodeId {
        let mut held = self.stand_ins.borrow_mut();
        held.push(Rc::new(body));
        NodeId(u32::MAX - (held.len() - 1) as u32)
    }

    /// `with` handed the written form `id` names, where it names one.
    pub fn read<R>(&self, id: NodeId, with: impl FnOnce(&Body) -> R) -> Option<R> {
        self.form(id).map(|(form, _)| with(&form))
    }

    pub fn stands(id: NodeId) -> bool {
        Through::stand_in(id).is_some()
    }
}

fn once<K: std::hash::Hash + Eq, V: Clone>(
    table: &Each<K, V>,
    key: K,
    of: impl FnOnce() -> V,
) -> V {
    if let Some(held) = table.borrow().get(&key) {
        return held.clone();
    }
    let found = of();
    table.borrow_mut().insert(key, found.clone());
    found
}

impl Reads for Through<'_> {
    fn lowered(&self, id: NodeId, origin: Origin, var: Var) -> Result<SpectralSum, Left> {
        let Some((form, own)) = self.form(id) else {
            return Opaque.lowered(id, origin, var);
        };
        once(&self.kept(id).lowered, (id, var), || {
            lower(&form, own, var, self)
        })
    }

    fn polynomial(&self, id: NodeId, reading: Reading) -> Option<Vec<Coeff>> {
        let (form, _) = self.form(id)?;
        once(&self.kept(id).polynomials, (id, reading), || {
            polynomial_read(&form, reading, self)
        })
    }

    fn timeless(&self, id: NodeId) -> bool {
        let Some((form, _)) = self.form(id) else {
            return Opaque.timeless(id);
        };
        once(&self.kept(id).timeless, id, || timeless(&form, self))
    }

    fn key_moves(&self, id: NodeId) -> bool {
        let Some((form, _)) = self.form(id) else {
            return Opaque.key_moves(id);
        };
        once(&self.kept(id).key_moves, id, || key_moves_read(&form, self))
    }

    fn mentions_line(&self, id: NodeId) -> bool {
        let Some((form, _)) = self.form(id) else {
            return Opaque.mentions_line(id);
        };
        once(&self.kept(id).mentions_line, id, || {
            crate::series::mentions_line_read(&form, self)
        })
    }

    fn axis(&self, id: NodeId) -> Option<Axis> {
        let (form, _) = self.form(id)?;
        Some(once(&self.kept(id).axes, id, || {
            crate::affine::axis_read(&form, &Within(self), self)
        }))
    }

    fn line(&self, id: NodeId) -> Option<Line> {
        let (form, _) = self.form(id)?;
        once(&self.kept(id).lines, id, || kink::line(&form, self))
    }

    fn kink(&self, id: NodeId) -> Option<(Body, Crossing)> {
        let (form, _) = self.form(id)?;
        once(&self.kept(id).kinks, id, || kink::first(&form, self))
    }

    fn replaced(&self, id: NodeId, scope: usize, kink: &Body, with: &Body) -> Option<NodeId> {
        let (form, _) = self.form(id)?;
        if let Some(held) = self.replacing.borrow().get(&(id, scope)) {
            return *held;
        }
        let rewritten = match &*form == kink {
            true => Some(with.clone()),
            false => kink::in_time(&form)
                .then(|| map_children(&form, |p| kink::replaced_part(p, (scope, kink, with), self)))
                .filter(|body| body != &*form),
        };
        let stand = rewritten.map(|body| self.stand(body));
        self.replacing.borrow_mut().insert((id, scope), stand);
        stand
    }

    fn moved(&self, id: NodeId, at: &Body) -> Option<NodeId> {
        let held = self.moving.borrow().get(&id).and_then(|read| {
            read.iter()
                .find_map(|(time, stand)| (time == at).then_some(*stand))
        });
        if let Some(held) = held {
            return held;
        }
        let (form, _) = self.form(id)?;
        let moved = read_at_with(&form, at, &|node| self.moved(node, at));
        let stand = Some(self.stand(moved));
        let mut moving = self.moving.borrow_mut();
        moving.entry(id).or_default().push((at.clone(), stand));
        stand
    }

    fn stand_for(&self, body: Body) -> Option<NodeId> {
        Some(self.stand(body))
    }

    fn scope(&self) -> usize {
        self.scopes.set(self.scopes.get() + 1);
        self.scopes.get()
    }

    fn view(&self, id: NodeId, read: &Read) -> Option<Form<'_>> {
        let (form, _) = self.form(id)?;
        let key = match read {
            Read::As => return Some(form),
            Read::At(at) => View::At(hash_time(at)),
            Read::By(by) => View::By(by.to_bits()),
        };
        let views = &self.kept(id).views;
        if let Some(held) = views.borrow().get(&(id, key)) {
            return Some(Form::Stand(Rc::clone(held)));
        }
        let seen = Rc::new(match read {
            Read::As => unreachable!("a form read as written is handed out as it stands"),
            Read::At(at) => read_at(&form, at),
            Read::By(by) => shift_line(&form, *by),
        });
        views.borrow_mut().insert((id, key), Rc::clone(&seen));
        Some(Form::Stand(seen))
    }

    fn growth(&self, id: NodeId, at: &Body, k: IndexId) -> Option<IndexGrowth> {
        let (form, _) = self.form(id)?;
        Some(once(&self.kept(id).growths, (id, k, hash_time(at)), || {
            crate::series::growth(&read_at(&form, at), k, &Within(self))
        }))
    }
}

/// Types nothing a reader reads through: a ref sits where its form does, and no parameter
/// reaches a form read through.
struct Within<'r>(&'r dyn Reads);

impl crate::env::Env for Within<'_> {
    fn node(&self, _: NodeId) -> crate::ty::Ty {
        crate::ty::Ty::form(Var::T, false, crate::ty::Codomain::Complex)
    }

    fn param(&self, _: crate::env::ParamId) -> crate::ty::Ty {
        crate::ty::Ty::form(Var::T, false, crate::ty::Codomain::Complex)
    }

    fn reads(&self) -> &dyn Reads {
        self.0
    }
}

/// `f` as a reader of its shape sees it: a ref is the form it names, each level kept per node
/// and time as the reader walks into it.
pub fn looked<'b>(f: &'b Body, reads: &'b dyn Reads) -> Seen<'b> {
    let mut seen = Seen::Here(f);
    loop {
        let read = match &*seen {
            Body::Node(id) => Some((*id, Read::As)),
            Body::Warp { at, of } => match &*of.body {
                Body::Node(id) => Some((*id, Read::At(&at.body))),
                _ => None,
            },
            Body::Shift { by, of } => match &*of.body {
                Body::Node(id) => Some((*id, Read::By(*by))),
                _ => None,
            },
            _ => None,
        };
        let Some(form) = read.and_then(|(id, read)| reads.view(id, &read)) else {
            return seen;
        };
        seen = Seen::Form(form);
    }
}

/// A body as a reader sees it: written where it stands, or the form a ref there names.
pub enum Seen<'b> {
    Here(&'b Body),
    Form(Form<'b>),
}

impl Deref for Seen<'_> {
    type Target = Body;

    fn deref(&self) -> &Body {
        match self {
            Seen::Here(body) => body,
            Seen::Form(form) => form,
        }
    }
}