vla-lang 0.10.1

The VLA language with nothing around it: forms, their expansion, the grid, the view record, and the machine that steps a grid
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
//! Recalculation's mechanism (KERNEL.7, slice 1, 2026-10-08): the formula
//! parser over the reference scanner, the dependency graph from `refers`,
//! evaluation in topological order, a cycle refused by name, Excel's error
//! values as a value kind, and a registry seam for functions, with the
//! language's own functions registered.
//!
//! It lands here, in the language, and not in `frazaro-core`, by the
//! three-crate order `Alonzo/CHARTER.md` section 4 and the roadmap's dated
//! note on `KERNEL.7` decide: an evaluator written into the bridges with
//! Excel's functions tangled in would make every game depend on the
//! bridges. So the mechanism and the day-one functions are the language's
//! ([`library::language`]); Excel's wider library registers through
//! [`Library`] from `frazaro-core`, by measurement and with a fixture per
//! function, and `frazaro calc` compares what it computes against the
//! values a host saved.
//!
//! An engine's grid is stepped by `crate::machine` (KERNEL.22,
//! 2026-10-09), over the same evaluator and the plan of shapes in
//! [`shape`]: every formula parsed once per shape and evaluated at each
//! cell's offset, a step with a budget in cells that yields and resumes
//! (`Alonzo/SPEC.md` section 4.4), the previous frame's `.last` twins being
//! ordinary sheets to it, each kept beside as an array over its extent that
//! a read indexes and a range walks ([`dense`], KERNEL.20), a run of a
//! shape's cells that read none of one another evaluated at once, node by
//! node (`vector`, KERNEL.20), and a changed cell followed to the cells that
//! read it, so that a frame computes those and keeps every other cell's
//! value (`reach`, KERNEL.20). [`Calc`] keeps its plan of texts and its
//! parse at evaluation, since it evaluates each formula once and an engine
//! every formula every frame. The plan of shapes prints itself on request,
//! its census (`census`, KERNEL.30): each shape's cells, levels and runs,
//! and the path each of its reads takes, so that a grid's cost is read
//! before a frame runs.
//!
//! What is not here, by name, each a slice of its own: dates, text
//! functions, lookups, dynamic arrays and spills, implicit intersection
//! and `@`, `LET` and `LAMBDA` (`KERNEL.8`); the rest of speed,
//! content-addressed evaluation among them (`KERNEL.20`). A formula that
//! reaches any
//! of them is not computed, and says so naming the function or the
//! construct ([`Reason`]), which is the honest static label of `HORIZON.md`
//! section 12.6; a cell that reads such a cell inherits the reason.

pub(crate) mod census;
pub mod dense;
pub mod eval;
pub mod formula;
pub mod graph;
pub mod library;
pub(crate) mod program;
pub(crate) mod reach;
pub mod shape;
pub mod value;
pub(crate) mod vector;

use std::cell::Cell as Slot;
use std::collections::HashMap;

use crate::messages::{raise, Refusal};
use crate::refers::quote_sheet;
use crate::sheet::{cell_ref, formula_text, shared_masters, Content, Workbook};

pub use dense::Dense;
pub use eval::{Arg, Computed, Ctx, Env, Form, Library, Reason, Strict};
pub use formula::{calls, parse, Expr};
pub use graph::{Area, Areas, CellId, Extent, Names, Plan};
pub use shape::{Place, Shape, Shapes};
pub use value::{agrees, ErrorKind, Value};

/// How far a step got: the cells evaluated so far in this frame, the
/// frame's total, and whether the frame is complete.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Progress {
    pub evaluated: usize,
    pub of: usize,
    pub done: bool,
}

/// One recalculation of one model under one library: the plan made once,
/// the cells evaluated in order, by steps or all at once.
pub struct Calc<'m> {
    model: &'m Workbook,
    library: &'m Library,
    names: Names,
    extents: Vec<Extent>,
    texts: HashMap<CellId, String>,
    plan: Plan,
    values: HashMap<CellId, Computed>,
    next: usize,
    here: Slot<CellId>,
}

impl<'m> Calc<'m> {
    /// The plan over the model: every formula cell's text read off the
    /// grid (a shared formula's child as its master's text moved), the
    /// graph built from `refers`, the order found and every cycle named.
    /// Nothing is evaluated yet.
    pub fn new(model: &'m Workbook, library: &'m Library) -> Calc<'m> {
        let names = Names::of(model);
        let extents: Vec<Extent> = model.sheets.iter().map(|s| s.extent()).collect();
        let mut texts = HashMap::new();
        for (si, sheet) in model.sheets.iter().enumerate() {
            let masters = shared_masters(sheet);
            for (&(row, col), cell) in &sheet.cells {
                if let Some(text) = formula_text(&cell.content, row, col, &masters) {
                    texts.insert((si, row, col), text);
                }
            }
        }
        let plan = graph::plan(model, &names, &extents, &texts);
        let mut values = HashMap::with_capacity(texts.len());
        for cycle in &plan.cycles {
            for id in cycle {
                values.insert(*id, Computed::NotComputed(Reason::Cycle));
            }
        }
        Calc {
            model,
            library,
            names,
            extents,
            texts,
            plan,
            values,
            next: 0,
            here: Slot::new((0, 1, 1)),
        }
    }

    /// The plan made and every cell evaluated.
    pub fn run(model: &'m Workbook, library: &'m Library) -> Calc<'m> {
        let mut calc = Calc::new(model, library);
        calc.step(0);
        calc
    }

    /// The model.
    pub fn model(&self) -> &'m Workbook {
        self.model
    }

    /// The library.
    pub fn library(&self) -> &'m Library {
        self.library
    }

    /// The plan: the order and the cycles.
    pub fn plan(&self) -> &Plan {
        &self.plan
    }

    /// Every cycle, each its cells sorted, the cycles by their first cell.
    pub fn cycles(&self) -> &[Vec<CellId>] {
        &self.plan.cycles
    }

    /// How many formula cells the model holds.
    pub fn formulas(&self) -> usize {
        self.texts.len()
    }

    /// A formula cell's text without its `=`, or `None` for a value cell.
    pub fn formula(&self, id: CellId) -> Option<&str> {
        self.texts.get(&id).map(String::as_str)
    }

    /// Every formula cell, in sheet then row-major order.
    pub fn formula_cells(&self) -> Vec<CellId> {
        let mut ids: Vec<CellId> = self.texts.keys().copied().collect();
        ids.sort_unstable();
        ids
    }

    /// What a formula cell holds, once evaluated; `None` for a value cell
    /// or a cell the steps have not reached.
    pub fn computed(&self, id: CellId) -> Option<&Computed> {
        self.values.get(&id)
    }

    /// Evaluate the next cells of the order, at most `budget` of them, 0
    /// meaning every one: `SD-34`'s step, a budget in cells and never in
    /// time, so that the frame's values are the same whatever the host's
    /// chunking. The cells in a cycle count as done from the start.
    pub fn step(&mut self, budget: usize) -> Progress {
        let order_len = self.plan.order.len();
        let end = if budget == 0 {
            order_len
        } else {
            self.next.saturating_add(budget).min(order_len)
        };
        for i in self.next..end {
            let id = self.plan.order[i];
            let computed = self.evaluate(id);
            self.values.insert(id, computed);
        }
        self.next = end;
        self.progress()
    }

    /// Where the evaluation stands.
    pub fn progress(&self) -> Progress {
        let in_cycles = self.plan.cycles.iter().map(Vec::len).sum::<usize>();
        Progress {
            evaluated: self.next + in_cycles,
            of: self.plan.formulas(),
            done: self.next == self.plan.order.len(),
        }
    }

    /// Start the order again, every value forgotten but the cycles'.
    pub fn restart(&mut self) {
        self.values
            .retain(|_, v| matches!(v, Computed::NotComputed(Reason::Cycle)));
        self.next = 0;
    }

    fn evaluate(&self, id: CellId) -> Computed {
        let Some(text) = self.texts.get(&id) else {
            return Computed::NotComputed(Reason::Construct(String::new()));
        };
        self.here.set(id);
        let parsed = match formula::parse(text) {
            Ok(e) => e,
            Err(construct) => return Computed::NotComputed(Reason::Construct(construct)),
        };
        let ctx = Ctx::new(self, self.library);
        match ctx.eval(&parsed).and_then(|arg| ctx.one(&arg)) {
            Ok(v) => Computed::Value(v.settled()),
            Err(reason) => Computed::NotComputed(reason),
        }
    }

    /// The functions the formulas call that the library does not hold,
    /// with the first cell calling each, in sheet then row-major order:
    /// what the engine's `load` refuses by name before the first step.
    pub fn missing_functions(&self) -> Vec<(CellId, String)> {
        let mut seen: HashMap<String, CellId> = HashMap::new();
        for id in self.formula_cells() {
            for name in formula::calls(&self.texts[&id]) {
                if !self.library.has(&name) {
                    seen.entry(name).or_insert(id);
                }
            }
        }
        let mut out: Vec<(CellId, String)> = seen.into_iter().map(|(n, id)| (id, n)).collect();
        out.sort();
        out
    }

    /// `Sheet!A1` for a cell, the sheet quoted as a reference quotes it.
    pub fn spell_cell(&self, id: CellId) -> String {
        let sheet = self
            .model
            .sheets
            .get(id.0)
            .map(|s| s.name.as_str())
            .unwrap_or("");
        format!("{}!{}", quote_sheet(sheet), cell_ref(id.1, id.2))
    }

    /// The first thing that stops the whole grid from being stepped, as a
    /// refusal from the catalogue: a cycle, naming the cells that close it
    /// (`calc-cycle`), else the first function the library does not hold,
    /// naming the cell that calls it (`calc-function-not-computed`), else
    /// the first formula whose text this version does not read, naming the
    /// construct (`calc-construct-not-read`, KERNEL.22: the "unreadable
    /// reference" KERNEL.7's fifth decision named for an engine's load);
    /// `None` when every formula can be computed. An engine's `load` asks
    /// this once; `frazaro calc` never does, since a workbook is read as it
    /// is and each cell says what it can.
    pub fn first_refusal(&self) -> Option<Refusal> {
        if let Some(cycle) = self.plan.cycles.first() {
            let cells: Vec<String> = cycle.iter().map(|id| self.spell_cell(*id)).collect();
            return Some(raise("calc-cycle", &[("cells", &cells.join(", "))]));
        }
        if let Some((id, name)) = self.missing_functions().into_iter().next() {
            return Some(raise(
                "calc-function-not-computed",
                &[("cell", &self.spell_cell(id)), ("function", &name)],
            ));
        }
        let (id, construct) = self.formula_cells().into_iter().find_map(|id| {
            formula::parse(&self.texts[&id])
                .err()
                .map(|construct| (id, construct))
        })?;
        Some(formula::unread_refusal(&self.spell_cell(id), &construct))
    }
}

/// What a projection asks of a recalculation (KERNEL.22): a formula cell's
/// computed value, or `None` for a value cell or a formula with no value
/// yet. [`Calc`] answers it, and so does an engine's machine, whose values
/// outlive any one recalculation; the view record prints its `value` rows
/// from either.
pub trait Values: std::fmt::Debug {
    fn computed(&self, id: CellId) -> Option<&Computed>;

    /// Whether some of a sheet's cells hold content newer than the model's
    /// (KERNEL.27): an engine's machine makes a twin's cells from its array
    /// only when a reader of the twin as a sheet asks, and the record and the
    /// plane read such a sheet's cells through [`Values::content`]. No sheet,
    /// for any other.
    fn lags(&self, _sheet: usize) -> bool {
        false
    }

    /// A cell's content where it is newer than the model's, on a sheet that
    /// [`Values::lags`]; `None` for the model's own.
    fn content(&self, _id: CellId) -> Option<Content> {
        None
    }

    /// A sheet's extent where the values keep it (KERNEL.37), so that a view
    /// reads its `extent` row without walking every cell of the sheet: the
    /// extent itself, `Some(None)` for a sheet with no cell, or `None` to
    /// have the view walk the sheet. [`Calc`] keeps every sheet's from its
    /// making, and an engine's machine from its load and every write and
    /// frame's end, for the evaluator ([`Env::extent`]).
    fn kept_extent(&self, _sheet: usize) -> Option<Extent> {
        None
    }
}

impl Values for Calc<'_> {
    fn computed(&self, id: CellId) -> Option<&Computed> {
        Calc::computed(self, id)
    }

    fn kept_extent(&self, sheet: usize) -> Option<Extent> {
        self.extents.get(sheet).copied()
    }
}

impl std::fmt::Debug for Calc<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Calc")
            .field("formulas", &self.texts.len())
            .field("cycles", &self.plan.cycles.len())
            .field("progress", &self.progress())
            .finish()
    }
}

impl Env for Calc<'_> {
    fn model(&self) -> &Workbook {
        self.model
    }

    fn here(&self) -> CellId {
        self.here.get()
    }

    fn read(&self, id: CellId) -> Result<Value, Reason> {
        if self.texts.contains_key(&id) {
            return match self.values.get(&id) {
                Some(Computed::Value(v)) => Ok(v.clone()),
                Some(Computed::NotComputed(r)) => Err(r.clone()),
                // Every dependency precedes its reader in the order; a
                // formula with no value yet can only be read from a cycle.
                None => Err(Reason::Cycle),
            };
        }
        let Some(cell) = self
            .model
            .sheets
            .get(id.0)
            .and_then(|s| s.cells.get(&(id.1, id.2)))
        else {
            return Ok(Value::Empty);
        };
        Ok(match &cell.content {
            Content::Text(t) => Value::Text(t.clone()),
            Content::Number(n) => Value::Number(*n),
            Content::Bool(b) => Value::Bool(*b),
            Content::Error(e) => Value::Error(ErrorKind::parse(e).unwrap_or(ErrorKind::Value)),
            _ => Value::Empty,
        })
    }

    fn extent(&self, sheet: usize) -> Extent {
        self.extents.get(sheet).copied().flatten()
    }

    fn name(&self, name: &str, scope: Option<usize>, home: usize) -> Option<&str> {
        self.names.resolve(name, scope, home)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sheet::{parse_a1_range, Cell, Sheet};

    /// A model from rows of `(sheet, address, content)`, a formula's text
    /// beginning with `=`.
    fn model(rows: &[(&str, &str, &str)]) -> Workbook {
        let mut wb = Workbook::new();
        for (sheet, addr, text) in rows {
            let i = wb.ensure_sheet(sheet);
            let range = parse_a1_range(addr).unwrap();
            let content = if let Some(f) = text.strip_prefix('=') {
                Content::Formula(f.to_string())
            } else if let Ok(n) = text.parse::<f64>() {
                Content::Number(n)
            } else if *text == "TRUE" || *text == "FALSE" {
                Content::Bool(*text == "TRUE")
            } else if let Some(e) = ErrorKind::parse(text) {
                Content::Error(e.text().to_string())
            } else {
                Content::Text(text.to_string())
            };
            wb.sheets[i].set(range.top, range.left, Cell { content, style: 0 });
        }
        wb
    }

    fn at(calc: &Calc<'_>, sheet: &str, addr: &str) -> Computed {
        let i = calc.model().find_sheet(sheet).unwrap();
        let r = parse_a1_range(addr).unwrap();
        calc.computed((i, r.top, r.left)).cloned().unwrap()
    }

    fn number(calc: &Calc<'_>, sheet: &str, addr: &str) -> f64 {
        match at(calc, sheet, addr) {
            Computed::Value(Value::Number(n)) => n,
            other => panic!("{sheet}!{addr}: {other:?}"),
        }
    }

    #[test]
    fn arithmetic_runs_in_dependency_order_whatever_the_cell_order() {
        let wb = model(&[
            ("S", "A3", "=A2*2"),
            ("S", "A2", "=A1+1"),
            ("S", "A1", "5"),
            ("S", "B1", "=A3-A2"),
        ]);
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        assert_eq!(number(&calc, "S", "A2"), 6.0);
        assert_eq!(number(&calc, "S", "A3"), 12.0);
        assert_eq!(number(&calc, "S", "B1"), 6.0);
        assert_eq!(calc.formulas(), 3);
        assert!(calc.cycles().is_empty());
        assert_eq!(
            calc.progress(),
            Progress {
                evaluated: 3,
                of: 3,
                done: true
            }
        );
    }

    #[test]
    fn a_budget_never_changes_a_value() {
        let wb = model(&[
            ("S", "A1", "1"),
            ("S", "A2", "=A1+1"),
            ("S", "A3", "=A2+1"),
            ("S", "A4", "=A3+1"),
            ("S", "A5", "=SUM(A1:A4)"),
        ]);
        let lib = library::language();
        let whole = Calc::run(&wb, &lib);
        let mut chunked = Calc::new(&wb, &lib);
        let p1 = chunked.step(2);
        assert_eq!(
            p1,
            Progress {
                evaluated: 2,
                of: 4,
                done: false
            }
        );
        assert!(
            chunked.computed((0, 4, 1)).is_none(),
            "A4 waits for the next step"
        );
        let p2 = chunked.step(1);
        assert!(!p2.done);
        let p3 = chunked.step(5);
        assert!(p3.done && p3.evaluated == 4);
        for id in whole.formula_cells() {
            assert_eq!(whole.computed(id), chunked.computed(id), "{id:?}");
        }
        assert_eq!(number(&whole, "S", "A5"), 10.0);
        chunked.restart();
        assert!(chunked.computed((0, 5, 1)).is_none());
        chunked.step(0);
        assert_eq!(number(&chunked, "S", "A5"), 10.0);
    }

    #[test]
    fn a_cycle_is_named_and_what_reads_it_is_not_computed() {
        let wb = model(&[
            ("S", "A1", "=B1+1"),
            ("S", "B1", "=A1+1"),
            ("S", "C1", "=A1*2"),
            ("S", "D1", "=D1"),
            ("S", "E1", "=1+1"),
        ]);
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        assert_eq!(
            calc.cycles(),
            &[vec![(0, 1, 1), (0, 1, 2)], vec![(0, 1, 4)]]
        );
        assert_eq!(at(&calc, "S", "A1"), Computed::NotComputed(Reason::Cycle));
        assert_eq!(at(&calc, "S", "C1"), Computed::NotComputed(Reason::Cycle));
        assert_eq!(at(&calc, "S", "D1"), Computed::NotComputed(Reason::Cycle));
        assert_eq!(number(&calc, "S", "E1"), 2.0);
        let refusal = calc.first_refusal().unwrap();
        assert_eq!(refusal.id, "calc-cycle");
        assert!(refusal.text.contains("S!A1, S!B1"), "{}", refusal.text);
        assert_eq!(
            calc.progress(),
            Progress {
                evaluated: 5,
                of: 5,
                done: true
            }
        );
    }

    #[test]
    fn a_cell_that_reads_itself_through_another_sheet_s_twin_is_no_cycle() {
        // The engine's previous frame: Screen.last holds values only.
        let wb = model(&[
            ("Screen", "B2", "=Screen.last!B2+1"),
            ("Screen.last", "B2", "41"),
        ]);
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        assert!(calc.cycles().is_empty());
        assert_eq!(number(&calc, "Screen", "B2"), 42.0);
    }

    #[test]
    fn ranges_names_and_spans_are_read_as_excel_reads_them() {
        let mut wb = model(&[
            ("Model", "B1", "1200"),
            ("Model", "B2", "800"),
            ("Model", "B3", "=B1-B2"),
            ("Model", "C1", "x"),
            ("Model", "C2", "TRUE"),
            ("Model", "D1", "=SUM(B:B)"),
            ("Model", "D2", "=SUM(B1:C2)"),
            ("Model", "D3", "=SUM(B1,\"5\",TRUE,C2)"),
            ("Model", "D4", "=B3*Rate"),
            ("Model", "D5", "=Local+1"),
            ("Model", "D6", "=SUM(Model:Scratch!B1)"),
            ("Model", "D7", "=Nothing"),
            ("Model", "D8", "=Missing!A1"),
            ("Model", "D9", "=MAX(B1:B3,C1)"),
            ("Model", "D10", "=MIN(C1:C2)"),
            ("Scratch", "B1", "7"),
            ("Scratch", "B2", "=Model!D1*2"),
        ]);
        wb.defined_names
            .push(("Rate".to_string(), "0.2".to_string()));
        wb.defined_names
            .push(("Model!Local".to_string(), "Model!$B$2".to_string()));
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        assert_eq!(number(&calc, "Model", "B3"), 400.0);
        assert_eq!(
            number(&calc, "Model", "D1"),
            2400.0,
            "the formula in B3 counts"
        );
        assert_eq!(
            number(&calc, "Model", "D2"),
            2000.0,
            "a text and a truth value in a range are skipped"
        );
        assert_eq!(
            number(&calc, "Model", "D3"),
            1206.0,
            "given directly they are coerced; C2 is read through a reference and skipped"
        );
        assert_eq!(number(&calc, "Model", "D4"), 80.0);
        assert_eq!(number(&calc, "Model", "D5"), 801.0);
        assert_eq!(
            number(&calc, "Model", "D6"),
            1207.0,
            "a span sums both sheets"
        );
        assert_eq!(
            at(&calc, "Model", "D7"),
            Computed::Value(Value::Error(ErrorKind::Name))
        );
        assert_eq!(
            at(&calc, "Model", "D8"),
            Computed::Value(Value::Error(ErrorKind::Ref))
        );
        assert_eq!(number(&calc, "Model", "D9"), 1200.0);
        assert_eq!(
            number(&calc, "Model", "D10"),
            0.0,
            "no number in the range is 0"
        );
        assert_eq!(
            number(&calc, "Scratch", "B2"),
            4800.0,
            "a sheet reads another's formula after it is computed"
        );
    }

    #[test]
    fn an_aggregate_folds_in_the_walk_s_order_and_keeps_the_first_answer() {
        // KERNEL.25: SUM, MIN, MAX, AND and OR fold as they walk instead of
        // collecting. The order is a range's walk, row by row: these four
        // cells sum to 0 by rows and to 1 by columns, as doubles.
        let wb = model(&[
            ("S", "A1", "1E16"),
            ("S", "B1", "1"),
            ("S", "A2", "-1E16"),
            ("S", "B2", "0"),
            ("S", "C1", "#N/A"),
            ("S", "C2", "=VLOOKUP(1,A1:A2,2)"),
            ("S", "D1", "=SUM(A1:B2)"),
            ("S", "D2", "=SUM(C1:C2)"),
            ("S", "D3", "=SUM(C1+0,C2:C2)"),
            ("S", "D4", "=AND(FALSE,C1)"),
            ("S", "D5", "=OR(TRUE,C1)"),
            ("S", "D6", "=MIN(Z1:Z3)"),
            ("S", "D7", "=MAX(Z1:Z3)"),
            ("S", "D8", "=MIN(A1:B2)"),
            ("S", "D9", "=MAX(A1:B2,-5)"),
            ("S", "D10", "=OR(A2:B2)"),
            ("S", "D11", "=AND(A1:B1,TRUE)"),
        ]);
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        let err = |k| Computed::Value(Value::Error(k));
        let by_rows = [1e16, 1.0, -1e16, 0.0].iter().fold(-0.0, |a, b| a + b);
        let by_columns = [1e16, -1e16, 1.0, 0.0].iter().fold(-0.0, |a, b| a + b);
        assert_ne!(
            by_rows, by_columns,
            "the fixture tells the two orders apart"
        );
        assert_eq!(number(&calc, "S", "D1"), by_rows);
        assert_eq!(number(&calc, "S", "D1"), 0.0);
        assert_eq!(
            at(&calc, "S", "D2"),
            Computed::NotComputed(Reason::Function("VLOOKUP".to_string())),
            "a range is walked to its end: the cell not computed answers after the error"
        );
        assert_eq!(
            at(&calc, "S", "D3"),
            err(ErrorKind::NA),
            "an error given directly answers before a later range is walked"
        );
        assert_eq!(at(&calc, "S", "D4"), err(ErrorKind::NA), "AND is strict");
        assert_eq!(at(&calc, "S", "D5"), err(ErrorKind::NA), "OR is strict");
        assert_eq!(number(&calc, "S", "D6"), 0.0, "MIN of no number");
        assert_eq!(number(&calc, "S", "D7"), 0.0, "MAX of no number");
        assert_eq!(number(&calc, "S", "D8"), -1e16);
        assert_eq!(number(&calc, "S", "D9"), 1e16);
        assert_eq!(
            at(&calc, "S", "D10"),
            Computed::Value(Value::Bool(true)),
            "-1E16 is true, 0 false"
        );
        assert_eq!(at(&calc, "S", "D11"), Computed::Value(Value::Bool(true)));
    }

    #[test]
    fn errors_are_values_and_propagate_as_excel_propagates_them() {
        let wb = model(&[
            ("S", "A1", "=1/0"),
            ("S", "A2", "=A1+1"),
            ("S", "A3", "=IF(A1>0,1,2)"),
            ("S", "A4", "=IF(TRUE,1,A1)"),
            ("S", "A5", "=SUM(A1:A2)"),
            ("S", "A6", "#N/A"),
            ("S", "A7", "=A6"),
            ("S", "A8", "=\"x\"+1"),
            ("S", "A9", "=-\"5\""),
            ("S", "A10", "=50%"),
            ("S", "A11", "=1&2"),
            ("S", "A12", "=2^10"),
            ("S", "A13", "=B99"),
            ("S", "A14", "=#REF!+1"),
        ]);
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        let err = |k| Computed::Value(Value::Error(k));
        assert_eq!(at(&calc, "S", "A1"), err(ErrorKind::Div0));
        assert_eq!(at(&calc, "S", "A2"), err(ErrorKind::Div0));
        assert_eq!(
            at(&calc, "S", "A3"),
            err(ErrorKind::Div0),
            "the condition's error"
        );
        assert_eq!(
            number(&calc, "S", "A4"),
            1.0,
            "the branch not taken is not computed"
        );
        assert_eq!(at(&calc, "S", "A5"), err(ErrorKind::Div0));
        assert_eq!(
            at(&calc, "S", "A7"),
            err(ErrorKind::NA),
            "an error typed into a cell"
        );
        assert_eq!(at(&calc, "S", "A8"), err(ErrorKind::Value));
        assert_eq!(number(&calc, "S", "A9"), -5.0);
        assert_eq!(number(&calc, "S", "A10"), 0.5);
        assert_eq!(
            at(&calc, "S", "A11"),
            Computed::Value(Value::Text("12".to_string()))
        );
        assert_eq!(number(&calc, "S", "A12"), 1024.0);
        assert_eq!(
            number(&calc, "S", "A13"),
            0.0,
            "an empty cell read is 0 in the cell"
        );
        assert_eq!(at(&calc, "S", "A14"), err(ErrorKind::Ref));
    }

    #[test]
    fn the_day_one_functions_compute_exactly() {
        let wb = model(&[
            ("S", "A1", "=IF(1>0,\"yes\",\"no\")"),
            ("S", "A2", "=IF(0)"),
            ("S", "A3", "=IF(FALSE,1)"),
            ("S", "A4", "=IF(TRUE,,1)"),
            ("S", "A5", "=AND(TRUE,1,\"TRUE\")"),
            ("S", "A6", "=AND(TRUE,0)"),
            ("S", "A7", "=OR(FALSE,0,2)"),
            ("S", "A8", "=NOT(0)"),
            ("S", "A9", "=AND(\"yes\")"),
            ("S", "A10", "=ABS(-3.5)"),
            ("S", "A11", "=INT(-1.5)"),
            ("S", "A12", "=MOD(-7,3)"),
            ("S", "A13", "=MOD(7,-3)"),
            ("S", "A14", "=MOD(7,0)"),
            ("S", "A15", "=ROW()"),
            ("S", "A16", "=COLUMN()"),
            ("S", "A17", "=ROW(C9)"),
            ("S", "A18", "=COLUMN(C9:D10)"),
            ("S", "A19", "=CHOOSE(2,\"a\",\"b\",\"c\")"),
            ("S", "A20", "=CHOOSE(4,\"a\",\"b\",\"c\")"),
            ("S", "A21", "=SIN(0)"),
            ("S", "A22", "=MOD(5.5,2)"),
            ("S", "A23", "=CHOOSE(1.9,\"a\",\"b\")"),
            ("S", "A24", "=AND()"),
            ("S", "B1", "=VLOOKUP(1,A1:A2,2)"),
            ("S", "B2", "=B1+1"),
            ("S", "B3", "=SUM(A1:A2 B1)"),
            ("S", "B4", "={1,2}"),
            ("S", "B5", "=A1:A2*2"),
        ]);
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        let text = |s: &str| Computed::Value(Value::Text(s.to_string()));
        let truth = |b: bool| Computed::Value(Value::Bool(b));
        let err = |k| Computed::Value(Value::Error(k));
        assert_eq!(at(&calc, "S", "A1"), text("yes"));
        assert_eq!(
            at(&calc, "S", "A2"),
            err(ErrorKind::Value),
            "IF wants a branch"
        );
        assert_eq!(at(&calc, "S", "A3"), truth(false), "no else is FALSE");
        assert_eq!(number(&calc, "S", "A4"), 0.0, "a branch left out is 0");
        assert_eq!(at(&calc, "S", "A5"), truth(true));
        assert_eq!(at(&calc, "S", "A6"), truth(false));
        assert_eq!(at(&calc, "S", "A7"), truth(true));
        assert_eq!(at(&calc, "S", "A8"), truth(true));
        assert_eq!(at(&calc, "S", "A9"), err(ErrorKind::Value));
        assert_eq!(number(&calc, "S", "A10"), 3.5);
        assert_eq!(number(&calc, "S", "A11"), -2.0);
        assert_eq!(number(&calc, "S", "A12"), 2.0, "the divisor's sign");
        assert_eq!(number(&calc, "S", "A13"), -2.0);
        assert_eq!(at(&calc, "S", "A14"), err(ErrorKind::Div0));
        assert_eq!(number(&calc, "S", "A15"), 15.0);
        assert_eq!(number(&calc, "S", "A16"), 1.0);
        assert_eq!(number(&calc, "S", "A17"), 9.0);
        assert_eq!(number(&calc, "S", "A18"), 3.0);
        assert_eq!(at(&calc, "S", "A19"), text("b"));
        assert_eq!(at(&calc, "S", "A20"), err(ErrorKind::Value));
        assert_eq!(number(&calc, "S", "A21"), 0.0);
        assert_eq!(number(&calc, "S", "A22"), 1.5);
        assert_eq!(
            at(&calc, "S", "A23"),
            text("a"),
            "the index is read down to a whole number"
        );
        assert_eq!(
            at(&calc, "S", "A24"),
            err(ErrorKind::Value),
            "nothing to AND"
        );
        assert_eq!(
            at(&calc, "S", "B1"),
            Computed::NotComputed(Reason::Function("VLOOKUP".to_string()))
        );
        assert_eq!(
            at(&calc, "S", "B2"),
            Computed::NotComputed(Reason::Function("VLOOKUP".to_string())),
            "a reader inherits the reason"
        );
        assert_eq!(
            at(&calc, "S", "B3"),
            Computed::NotComputed(Reason::Construct("intersection".to_string()))
        );
        assert_eq!(
            at(&calc, "S", "B4"),
            Computed::NotComputed(Reason::Construct("{".to_string()))
        );
        assert_eq!(
            at(&calc, "S", "B5"),
            Computed::NotComputed(Reason::Construct("A1:A2".to_string())),
            "a range where one value is wanted waits for KERNEL.8"
        );
        assert_eq!(
            calc.missing_functions(),
            vec![((0, 1, 2), "VLOOKUP".to_string())]
        );
        let refusal = calc.first_refusal().unwrap();
        assert_eq!(refusal.id, "calc-function-not-computed");
        assert!(
            refusal.text.contains("VLOOKUP") && refusal.text.contains("S!B1"),
            "{}",
            refusal.text
        );
        assert_eq!(at(&calc, "S", "A1").spell(), "\"yes\"");
        assert_eq!(at(&calc, "S", "B1").spell(), "(not-computed \"VLOOKUP\")");
    }

    #[test]
    fn a_construct_not_read_is_the_third_thing_a_load_refuses() {
        // KERNEL.22: after the cycle and the function, a formula whose text
        // this version does not read, naming the construct.
        let wb = model(&[
            ("S", "A1", "=1+1"),
            ("S", "B2", "={1,2}"),
            ("S", "C3", "=@A1"),
        ]);
        let lib = library::language();
        let calc = Calc::new(&wb, &lib);
        let r = calc.first_refusal().unwrap();
        assert_eq!(r.id, "calc-construct-not-read");
        assert!(
            r.text.contains("S!B2") && r.text.contains("reaches {"),
            "{}",
            r.text
        );
        // A function the library does not hold comes first.
        let wb = model(&[("S", "B2", "={1,2}"), ("S", "C3", "=NOW()")]);
        let calc = Calc::new(&wb, &lib);
        assert_eq!(
            calc.first_refusal().unwrap().id,
            "calc-function-not-computed"
        );
        let wb = model(&[("S", "A1", "=1+1")]);
        assert!(Calc::new(&wb, &lib).first_refusal().is_none());
    }

    #[test]
    fn a_chain_of_operators_evaluates_left_to_right_however_long() {
        // VLA.1: a chain goes round a loop instead of recursing once a
        // link, read as Excel reads it, left to right: the values the
        // recursion gave, at every length Excel holds.
        let long = format!("={}1", "1+".repeat(4095));
        let wb = model(&[
            ("S", "A1", "=10-1-1-1"),
            ("S", "A2", "=2^3^2"),
            ("S", "A3", "=1+2*3-4/2+5"),
            ("S", "A4", "=\"a\"&\"b\"&\"c\""),
            ("S", "A5", long.as_str()),
            ("S", "A6", "=1/0+1+1"),
            ("S", "A7", "=1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1"),
        ]);
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        assert_eq!(number(&calc, "S", "A1"), 7.0);
        assert_eq!(number(&calc, "S", "A2"), 64.0);
        assert_eq!(number(&calc, "S", "A3"), 10.0);
        assert_eq!(
            at(&calc, "S", "A4"),
            Computed::Value(Value::Text("abc".to_string()))
        );
        assert_eq!(number(&calc, "S", "A5"), 4096.0);
        assert_eq!(
            at(&calc, "S", "A6"),
            Computed::Value(Value::Error(ErrorKind::Div0))
        );
        // Past the sixteen links held in place, the rest on the heap.
        assert_eq!(number(&calc, "S", "A7"), 20.0);
    }

    #[test]
    fn excel_s_two_limits_are_refused_by_their_own_ids() {
        // VLA.1: past Excel's nesting or length limit an engine's load is
        // refused by the limit's own id, and frazaro calc, which never
        // refuses, shows the label in the cell.
        let deep = format!("={}1{}", "(".repeat(65), ")".repeat(65));
        let long = format!("={}1", "1+".repeat(4096));
        let lib = library::language();
        let wb = model(&[("S", "A1", deep.as_str())]);
        let r = Calc::new(&wb, &lib).first_refusal().unwrap();
        assert_eq!(r.id, "calc-formula-too-deep");
        assert_eq!(
            r.text,
            "The formula in S!A1 nests more than 64 levels deep, more than Excel itself allows, so the formula cannot be computed."
        );
        let wb = model(&[("S", "A1", long.as_str())]);
        let r = Calc::new(&wb, &lib).first_refusal().unwrap();
        assert_eq!(r.id, "calc-formula-too-long");
        assert_eq!(
            r.text,
            "The formula in S!A1 is longer than 8,192 characters, more than Excel itself allows, so the formula cannot be computed."
        );
        let calc = Calc::run(&wb, &lib);
        assert_eq!(
            at(&calc, "S", "A1"),
            Computed::NotComputed(Reason::Construct(formula::TOO_LONG.to_string()))
        );
    }

    #[test]
    fn a_logical_test_coerces_a_text_read_through_a_reference() {
        // Measured: Excel's save of the subset fixture
        // (scripts/recalc/subset_saved.xlsx, logic!C3, C6 and C13): IF
        // coerces the text TRUE found in a cell as it coerces a typed one and
        // refuses another text; AND reads the reference as a range and skips
        // the text. NOT follows IF's rule, which scripts/recalc/edges.txt
        // holds for an Excel save to witness.
        let mut wb = model(&[
            ("S", "C1", "=IF(A1,1,2)"),
            ("S", "C2", "=IF(A2,1,2)"),
            ("S", "C3", "=AND(A1)"),
            ("S", "C4", "=NOT(A1)"),
        ]);
        for (row, text) in [(1, "TRUE"), (2, "x")] {
            wb.sheets[0].set(
                row,
                1,
                Cell {
                    content: Content::Text(text.to_string()),
                    style: 0,
                },
            );
        }
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        let err = |k| Computed::Value(Value::Error(k));
        assert_eq!(number(&calc, "S", "C1"), 1.0);
        assert_eq!(at(&calc, "S", "C2"), err(ErrorKind::Value));
        assert_eq!(at(&calc, "S", "C3"), err(ErrorKind::Value));
        assert_eq!(at(&calc, "S", "C4"), Computed::Value(Value::Bool(false)));
    }

    #[test]
    fn shared_and_dynamic_formulas_are_read_through_their_masters() {
        let mut wb = Workbook::new();
        let i = wb.ensure_sheet("Output");
        let sheet: &mut Sheet = &mut wb.sheets[i];
        sheet.set(
            2,
            2,
            Cell {
                content: Content::Number(5.0),
                style: 0,
            },
        );
        sheet.set(
            3,
            2,
            Cell {
                content: Content::Formula("B2*2".to_string()),
                style: 0,
            },
        );
        sheet.set_formula(parse_a1_range("C2:C4").unwrap(), "B2+B3", 0);
        sheet.set_formula_dynamic(parse_a1_range("D2:D3").unwrap(), "B2*10", 0);
        let lib = library::language();
        let calc = Calc::run(&wb, &lib);
        assert_eq!(
            calc.formula((0, 3, 3)),
            Some("B3+B4"),
            "the child's text is the master's moved"
        );
        assert_eq!(number(&calc, "Output", "C2"), 15.0);
        assert_eq!(number(&calc, "Output", "C3"), 10.0);
        assert_eq!(number(&calc, "Output", "C4"), 0.0);
        assert_eq!(number(&calc, "Output", "D2"), 50.0);
        assert_eq!(number(&calc, "Output", "D3"), 100.0);
        assert_eq!(calc.spell_cell((0, 3, 3)), "Output!C3");
    }
}