earthsci-ast 0.6.0

Rust implementation of EarthSciML Abstract Syntax Tree Format
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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
//! Canonical AST form per discretization RFC §5.4.
//!
//! Implements `canonicalize(expr) -> expr` and `canonical_json(expr) -> String`
//! such that two ASTs are canonically equal iff their `canonical_json` outputs
//! are byte-identical.
//!
//! See `docs/content/rfcs/discretization.md` §5.4.1–§5.4.7 for the normative rules.

use crate::types::{Expr, ExpressionNode};

/// Errors raised during canonicalization (per RFC §5.4.6 / §5.4.7).
#[derive(Debug, Clone, PartialEq)]
pub enum CanonicalizeError {
    /// `E_CANONICAL_NONFINITE` — NaN or ±Inf encountered (§5.4.6).
    NonFinite,
    /// `E_CANONICAL_DIVBY_ZERO` — `/(0, 0)` encountered (§5.4.7).
    DivByZero,
    /// `E_CANONICAL_UNSUPPORTED_FIELD` — an operator node reached during
    /// emission carries a field with no slot in the closed canonical JSON node
    /// encoding (`op`/`args`/`wrt`/`dim`/`fn`/`name`/`value`), so no faithful
    /// canonical JSON exists for it (see [`canonical_json`]). The `String`
    /// names the first offending field. Fail-closed to match Julia's
    /// `_emit_node_json` guard (`_NON_EMISSIBLE_FIELDS`) and the TS/Python
    /// siblings, rather than silently dropping the field and emitting ambiguous
    /// bytes.
    UnsupportedField(String),
}

impl std::fmt::Display for CanonicalizeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Render the stable cross-language error CODE only (the `String` payload
        // of `UnsupportedField` is diagnostic and lives in the `Debug`/variant,
        // matching how `NonFinite`/`DivByZero` render just their codes).
        match self {
            CanonicalizeError::NonFinite => write!(f, "E_CANONICAL_NONFINITE"),
            CanonicalizeError::DivByZero => write!(f, "E_CANONICAL_DIVBY_ZERO"),
            CanonicalizeError::UnsupportedField(_) => write!(f, "E_CANONICAL_UNSUPPORTED_FIELD"),
        }
    }
}

impl std::error::Error for CanonicalizeError {}

/// Canonicalize an [`Expr`] tree per RFC §5.4. Returns a new tree; input is not mutated.
pub fn canonicalize(expr: &Expr) -> Result<Expr, CanonicalizeError> {
    match expr {
        Expr::Integer(i) => Ok(Expr::Integer(*i)),
        Expr::Number(f) => {
            if !f.is_finite() {
                return Err(CanonicalizeError::NonFinite);
            }
            Ok(Expr::Number(*f))
        }
        Expr::Variable(s) => Ok(Expr::Variable(s.clone())),
        Expr::Operator(node) => canon_op(node),
    }
}

/// Emit the canonical on-wire JSON form of an expression (§5.4.6).
///
/// Calls `canonicalize` first, then serializes with sorted keys, no extraneous
/// whitespace, and the strict number formatting of §5.4.6.
pub fn canonical_json(expr: &Expr) -> Result<String, CanonicalizeError> {
    let c = canonicalize(expr)?;
    // `canonicalize` PRESERVES every field on pass-through (non-arithmetic)
    // nodes, so the returned tree may still carry a field with no slot in the
    // closed canonical JSON node encoding. Emitting only the emissible fields
    // would make structurally-different nodes byte-identical (the defect class
    // behind the `fn` bc-node bug). Fail closed instead, matching Julia's
    // `_emit_node_json` guard — see `validate_emissible`.
    validate_emissible(&c)?;
    Ok(emit_canonical_json(&c))
}

/// Reject any operator node reached through the `args` spine (the same nodes
/// `emit_node_json` recurses into) that carries a NON-EMISSIBLE field, per the
/// cross-binding canonical contract. Returns the code
/// `E_CANONICAL_UNSUPPORTED_FIELD` naming the first offending field.
///
/// This mirrors Julia's `_emit_node_json`, which checks each node's
/// `_NON_EMISSIBLE_FIELDS` before emitting and recurses only into `args`. A
/// non-emissible field carried by a node buried ONLY in a sidecar sub-tree
/// (`filter`/`expr`/…) is never emitted, hence never validated — exactly as in
/// Julia, since that node whose sidecar holds it is itself flagged first.
fn validate_emissible(e: &Expr) -> Result<(), CanonicalizeError> {
    if let Expr::Operator(n) = e {
        if let Some(field) = first_non_emissible_field(n) {
            return Err(CanonicalizeError::UnsupportedField(field.into()));
        }
        for a in &n.args {
            validate_emissible(a)?;
        }
    }
    Ok(())
}

/// The first set field of `n` that has NO slot in the canonical JSON node
/// encoding, or `None` if the node is fully emissible.
///
/// EMISSIBLE (have a wire slot): `op`, `args`, `wrt`, `dim`, `fn`
/// (`broadcast_fn`), `name`, `value`. TOLERATED-AND-IGNORED (may be present, no
/// wire slot, NOT an error — matching Julia's `_CANONICAL_IGNORED_FIELDS`):
/// `arg`, `bindings`. Every OTHER `ExpressionNode` field is NON-EMISSIBLE.
///
/// SINGLE-SOURCE / FAIL-CLOSED: the leading `let ExpressionNode { … } = n;`
/// destructures EVERY field with no `..` rest pattern, so classifying each field
/// into one of the three groups is mandatory — adding a field to
/// `ExpressionNode` produces a missing-field compile error here until it is
/// deliberately classified, and therefore can never be silently dropped from
/// (nor wrongly slipped into) the canonical form. This is the Rust analogue of
/// Julia's derived `_NON_EMISSIBLE_FIELDS = setdiff(fieldnames(OpExpr),
/// emissible ∪ ignored)`; kept in lockstep with it.
fn first_non_emissible_field(n: &ExpressionNode) -> Option<&'static str> {
    let ExpressionNode {
        // EMISSIBLE — pinned wire slot, emitted by `emit_node_json`.
        op: _,
        args: _,
        wrt: _,
        dim: _,
        broadcast_fn: _, // JSON key `fn`
        name: _,
        value: _,
        units: _,
        // TOLERATED-AND-IGNORED — no wire slot, presence is NOT an error
        // (Julia's `_CANONICAL_IGNORED_FIELDS`).
        arg: _,
        bindings: _,
        // NON-EMISSIBLE — no wire slot; a set value fails canonical emission
        // closed. Checked below in struct-declaration order.
        int_var,
        lower,
        upper,
        expr,
        output_idx,
        ranges,
        reduce,
        semiring,
        join,
        filter,
        regions,
        values,
        shape,
        perm,
        axis,
        label,
        table,
        axes,
        output,
        id,
        manifold,
        distinct,
        key,
        expect_cadence,
        attrs,
    } = n;

    // JSON wire name reported where it differs from the Rust field name
    // (`int_var` serializes as `var`).
    if int_var.is_some() {
        return Some("var");
    }
    if lower.is_some() {
        return Some("lower");
    }
    if upper.is_some() {
        return Some("upper");
    }
    if expr.is_some() {
        return Some("expr");
    }
    if output_idx.is_some() {
        return Some("output_idx");
    }
    if ranges.is_some() {
        return Some("ranges");
    }
    if reduce.is_some() {
        return Some("reduce");
    }
    if semiring.is_some() {
        return Some("semiring");
    }
    if join.is_some() {
        return Some("join");
    }
    if filter.is_some() {
        return Some("filter");
    }
    if regions.is_some() {
        return Some("regions");
    }
    if values.is_some() {
        return Some("values");
    }
    if shape.is_some() {
        return Some("shape");
    }
    if perm.is_some() {
        return Some("perm");
    }
    if axis.is_some() {
        return Some("axis");
    }
    // `label` (skolem documentary relation tag) has NO canonical wire slot. It is
    // non-emissible in every sibling binding's derived set (it is a field of
    // Julia's `OpExpr`, so `setdiff` lands it in `_NON_EMISSIBLE_FIELDS`), so a
    // node carrying it must fail closed — previously `label` was omitted here and
    // silently DROPPED, making two `skolem` nodes that differ only in `label`
    // canonicalize to byte-identical JSON (bug fix).
    if label.is_some() {
        return Some("label");
    }
    if table.is_some() {
        return Some("table");
    }
    if axes.is_some() {
        return Some("axes");
    }
    if output.is_some() {
        return Some("output");
    }
    if id.is_some() {
        return Some("id");
    }
    if manifold.is_some() {
        return Some("manifold");
    }
    if distinct.is_some() {
        return Some("distinct");
    }
    if key.is_some() {
        return Some("key");
    }
    // `expect_cadence` (author cadence assertion) and `attrs` (open
    // rewrite-target op attributes) have no canonical wire slot either.
    // `tests/conformance/canonical/README.md` names both classes of field as
    // ones that MUST raise `E_CANONICAL_UNSUPPORTED_FIELD`; until 2026-08-31
    // neither could reach here, because the typed node dropped them at load.
    if expect_cadence.is_some() {
        return Some("expect_cadence");
    }
    if attrs.is_some() {
        return Some("attrs");
    }
    None
}

fn canon_op(node: &ExpressionNode) -> Result<Expr, CanonicalizeError> {
    let mut work = node.clone();
    // Canonicalize EVERY expression-bearing child — `args` plus the sidecar
    // bodies (`lower`/`upper`/`expr`/`filter`/`values`/`axes`/`key`/`bindings`),
    // enumerated once by `ExpressionNode::for_each_child_mut` (the crate's single
    // source of truth for which fields carry child `Expr`s). This guarantees a
    // non-finite float or `0/0` buried in an aggregate body, integral bound,
    // `filter` predicate, `table_lookup` axis, or template binding is caught by
    // the same `NonFinite`/`DivByZero` guards as one in `args` — previously only
    // `args` was descended, so such a NaN escaped the guard.
    //
    // Sidecar fields are NOT part of the emitted canonical JSON (see
    // `emit_node_json`, which emits only op/args/wrt/dim/fn/name/value/units — the
    // closed cross-binding field set the TS/Python/Julia siblings pin), so
    // normalizing the sidecar sub-trees here only strengthens the finiteness
    // guard and normalizes the returned tree. A node that still carries a
    // non-emissible sidecar field after canonicalization makes `canonical_json`
    // fail closed with `E_CANONICAL_UNSUPPORTED_FIELD` (see `validate_emissible`)
    // rather than emitting ambiguous bytes.
    let mut err: Option<CanonicalizeError> = None;
    work.for_each_child_mut(&mut |child| {
        if err.is_some() {
            return;
        }
        match canonicalize(&*child) {
            Ok(c) => *child = c,
            Err(e) => err = Some(e),
        }
    });
    if let Some(e) = err {
        return Err(e);
    }
    match work.op.as_str() {
        "+" => canon_add(&mut work),
        "*" => canon_mul(&mut work),
        "-" => canon_sub(&mut work),
        "/" => canon_div(&mut work),
        "neg" => canon_neg(&mut work),
        _ => Ok(Expr::operator(work)),
    }
}

fn canon_add(node: &mut ExpressionNode) -> Result<Expr, CanonicalizeError> {
    let flat = flatten_same_op(std::mem::take(&mut node.args), "+");
    let (mut others, _had_int_zero, had_float_zero) = partition_identity(flat, 0);
    if had_float_zero && !all_float_literals(&others) {
        others.push(Expr::Number(0.0));
    }
    if others.is_empty() {
        return Ok(if had_float_zero {
            Expr::Number(0.0)
        } else {
            Expr::Integer(0)
        });
    }
    if others.len() == 1 {
        return Ok(others.pop().unwrap());
    }
    sort_args(&mut others);
    Ok(Expr::operator(ExpressionNode {
        op: "+".into(),
        args: others,
        ..ExpressionNode::default()
    }))
}

fn canon_mul(node: &mut ExpressionNode) -> Result<Expr, CanonicalizeError> {
    let flat = flatten_same_op(std::mem::take(&mut node.args), "*");
    for a in &flat {
        if let Expr::Integer(0) = a {
            return Ok(Expr::Integer(0));
        }
        if let Expr::Number(f) = a
            && *f == 0.0
        {
            // Preserve signbit of zero.
            return Ok(Expr::Number(*f * 0.0_f64));
        }
    }
    let (mut others, _had_int_one, had_float_one) = partition_identity(flat, 1);
    if had_float_one && !all_float_literals(&others) {
        others.push(Expr::Number(1.0));
    }
    if others.is_empty() {
        return Ok(if had_float_one {
            Expr::Number(1.0)
        } else {
            Expr::Integer(1)
        });
    }
    if others.len() == 1 {
        return Ok(others.pop().unwrap());
    }
    sort_args(&mut others);
    Ok(Expr::operator(ExpressionNode {
        op: "*".into(),
        args: others,
        ..ExpressionNode::default()
    }))
}

fn canon_sub(node: &mut ExpressionNode) -> Result<Expr, CanonicalizeError> {
    if node.args.len() == 1 {
        // Tolerate unary -, prefer neg on the wire.
        let arg = node.args.pop().unwrap();
        return canon_neg_value(arg);
    }
    if node.args.len() == 2 {
        let b = node.args.pop().unwrap();
        let a = node.args.pop().unwrap();
        // -(0, x) -> neg(x)
        if is_zero_any(&a) {
            return canon_neg_value(b);
        }
        // -(x, 0) -> x (type-preserving: float-zero with int x promotes)
        if is_zero_any(&b) {
            if matches!(b, Expr::Number(_))
                && let Expr::Integer(i) = a
            {
                return Ok(Expr::Number(i as f64));
            }
            return Ok(a);
        }
        // Restore args.
        return Ok(Expr::operator(ExpressionNode {
            op: "-".into(),
            args: vec![a, b],
            ..ExpressionNode::default()
        }));
    }
    Ok(Expr::operator(std::mem::take(node)))
}

fn canon_div(node: &mut ExpressionNode) -> Result<Expr, CanonicalizeError> {
    if node.args.len() != 2 {
        return Ok(Expr::operator(std::mem::take(node)));
    }
    let b = node.args.pop().unwrap();
    let a = node.args.pop().unwrap();
    if is_zero_any(&a) && is_zero_any(&b) {
        return Err(CanonicalizeError::DivByZero);
    }
    if is_one_any(&b) {
        if matches!(b, Expr::Number(_))
            && let Expr::Integer(i) = a
        {
            return Ok(Expr::Number(i as f64));
        }
        return Ok(a);
    }
    if is_zero_any(&a) {
        return Ok(if matches!(a, Expr::Number(_)) {
            Expr::Number(0.0)
        } else {
            Expr::Integer(0)
        });
    }
    Ok(Expr::operator(ExpressionNode {
        op: "/".into(),
        args: vec![a, b],
        ..ExpressionNode::default()
    }))
}

fn canon_neg(node: &mut ExpressionNode) -> Result<Expr, CanonicalizeError> {
    if node.args.len() != 1 {
        return Ok(Expr::operator(std::mem::take(node)));
    }
    let arg = node.args.pop().unwrap();
    canon_neg_value(arg)
}

fn canon_neg_value(arg: Expr) -> Result<Expr, CanonicalizeError> {
    match arg {
        Expr::Integer(i) => Ok(Expr::Integer(-i)),
        Expr::Number(f) => Ok(Expr::Number(-f)),
        Expr::Operator(n) if n.op == "neg" && n.args.len() == 1 => Ok(n.args[0].clone()),
        other => Ok(Expr::operator(ExpressionNode {
            op: "neg".into(),
            args: vec![other],
            ..ExpressionNode::default()
        })),
    }
}

fn flatten_same_op(args: Vec<Expr>, op: &str) -> Vec<Expr> {
    let mut out = Vec::with_capacity(args.len());
    for a in args {
        match a {
            Expr::Operator(node) if node.op == op => out.extend(node.args.iter().cloned()),
            other => out.push(other),
        }
    }
    out
}

fn partition_identity(args: Vec<Expr>, identity: i64) -> (Vec<Expr>, bool, bool) {
    let mut others = Vec::with_capacity(args.len());
    let (mut had_int, mut had_float) = (false, false);
    for a in args {
        match &a {
            Expr::Integer(i) if *i == identity => {
                had_int = true;
                continue;
            }
            Expr::Number(f) if *f == identity as f64 => {
                had_float = true;
                continue;
            }
            _ => {}
        }
        others.push(a);
    }
    (others, had_int, had_float)
}

fn all_float_literals(args: &[Expr]) -> bool {
    !args.is_empty() && args.iter().all(|a| matches!(a, Expr::Number(_)))
}

fn is_zero_any(e: &Expr) -> bool {
    matches!(e, Expr::Integer(0)) || matches!(e, Expr::Number(f) if *f == 0.0)
}

fn is_one_any(e: &Expr) -> bool {
    matches!(e, Expr::Integer(1)) || matches!(e, Expr::Number(f) if *f == 1.0)
}

fn sort_args(args: &mut [Expr]) {
    // Memoize canonical JSON for non-leaf nodes to avoid quadratic work (§5.4.9).
    let mut cache: std::collections::HashMap<usize, String> = std::collections::HashMap::new();
    let mut indices: Vec<usize> = (0..args.len()).collect();
    indices.sort_by(|&i, &j| compare_exprs(&args[i], &args[j], i, j, &mut cache));
    let cloned: Vec<Expr> = indices.iter().map(|&i| args[i].clone()).collect();
    for (slot, e) in args.iter_mut().zip(cloned) {
        *slot = e;
    }
}

fn arg_tier(e: &Expr) -> u8 {
    match e {
        Expr::Integer(_) | Expr::Number(_) => 0,
        Expr::Variable(_) => 1,
        Expr::Operator(_) => 2,
    }
}

fn numeric_key(e: &Expr) -> f64 {
    match e {
        Expr::Integer(i) => *i as f64,
        Expr::Number(f) => *f,
        _ => 0.0,
    }
}

fn compare_exprs(
    a: &Expr,
    b: &Expr,
    ia: usize,
    ib: usize,
    cache: &mut std::collections::HashMap<usize, String>,
) -> std::cmp::Ordering {
    use std::cmp::Ordering;
    let (ta, tb) = (arg_tier(a), arg_tier(b));
    if ta != tb {
        return ta.cmp(&tb);
    }
    match ta {
        0 => {
            let av = numeric_key(a);
            let bv = numeric_key(b);
            match av.partial_cmp(&bv).unwrap_or(Ordering::Equal) {
                Ordering::Equal => {
                    // int before float at equal magnitude.
                    let af = matches!(a, Expr::Number(_));
                    let bf = matches!(b, Expr::Number(_));
                    af.cmp(&bf)
                }
                ord => ord,
            }
        }
        1 => match (a, b) {
            (Expr::Variable(x), Expr::Variable(y)) => x.cmp(y),
            _ => Ordering::Equal,
        },
        _ => {
            let aj = cache
                .entry(ia)
                .or_insert_with(|| emit_canonical_json(a))
                .clone();
            let bj = cache
                .entry(ib)
                .or_insert_with(|| emit_canonical_json(b))
                .clone();
            aj.cmp(&bj)
        }
    }
}

fn emit_canonical_json(e: &Expr) -> String {
    match e {
        Expr::Integer(i) => i.to_string(),
        Expr::Number(f) => format_canonical_float(*f),
        Expr::Variable(s) => json_string(s),
        Expr::Operator(n) => emit_node_json(n),
    }
}

// Emit the canonical JSON object for an operator node. Infallible internal sort
// key: `canonical_json` runs `validate_emissible` BEFORE any emission, so every
// node reaching here is already known to be fully emissible.
//
// Emissible field set (CLOSED, cross-binding-pinned): exactly
// `op`/`args`/`wrt`/`dim`/`fn`/`name`/`value`. This is the identical set the
// sibling bindings serialize — Julia's `_EMISSIBLE_FIELDS` tuple, and the
// `emitNodeJson`/`_emit_node_json` emitters in TS/Python — and it is the set the
// cross-binding `tests/conformance/canonical/` fixtures pin. Extending it is a
// coordinated cross-binding format change, NEVER a Rust-local edit: emitting an
// extra field here would make this binding's `canonical_json` bytes diverge from
// the siblings and break the byte contract.
//
// The former field-coverage gap (a node's other set fields — `expr`/`ranges`/
// `reduce`/`semiring`/`join`/`filter`, `regions`/`values`, `table`/`axes`/
// `output`, `shape`/`perm`/`axis`, `id`/`manifold`, `key`/`distinct`, … — had
// no slot here, so two nodes differing ONLY in those fields produced identical
// canonical JSON) is now CLOSED by failing closed: `validate_emissible` rejects
// any such node with `E_CANONICAL_UNSUPPORTED_FIELD`, matching Julia.
fn emit_node_json(n: &ExpressionNode) -> String {
    let mut entries: Vec<(String, String)> = Vec::new();
    entries.push(("op".into(), json_string(&n.op)));
    let arg_parts: Vec<String> = n.args.iter().map(emit_canonical_json).collect();
    entries.push(("args".into(), format!("[{}]", arg_parts.join(","))));
    if let Some(ref s) = n.wrt {
        entries.push(("wrt".into(), json_string(s)));
    }
    if let Some(ref s) = n.dim {
        entries.push(("dim".into(), json_string(s)));
    }
    if let Some(ref s) = n.name {
        entries.push(("name".into(), json_string(s)));
    }
    if let Some(ref v) = n.value {
        entries.push(("value".into(), emit_canonical_json_value(v)));
    }
    if let Some(ref u) = n.units {
        entries.push(("units".into(), json_string(u)));
    }
    if let Some(ref s) = n.broadcast_fn {
        entries.push(("fn".into(), json_string(s)));
    }
    entries.sort_by(|a, b| a.0.cmp(&b.0));
    let body: Vec<String> = entries
        .into_iter()
        .map(|(k, v)| format!("{}:{}", json_string(&k), v))
        .collect();
    format!("{{{}}}", body.join(","))
}

fn json_string(s: &str) -> String {
    serde_json::to_string(s).unwrap_or_else(|_| format!("\"{s}\""))
}

// Canonicalize a `const`-op `value` JSON payload. Numbers carrying a fraction
// or exponent emit using `format_canonical_float`; integers emit verbatim;
// arrays recurse. Strings/objects/booleans/null fall through to serde_json's
// default rendering — `value` is constrained to numeric / nested-numeric in the
// schema, but we keep the fallback so a stray string doesn't crash canonical
// emission.
fn emit_canonical_json_value(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                i.to_string()
            } else if let Some(u) = n.as_u64() {
                u.to_string()
            } else if let Some(f) = n.as_f64() {
                format_canonical_float(f)
            } else {
                n.to_string()
            }
        }
        serde_json::Value::Array(arr) => {
            let parts: Vec<String> = arr.iter().map(emit_canonical_json_value).collect();
            format!("[{}]", parts.join(","))
        }
        _ => v.to_string(),
    }
}

/// Format a finite f64 per RFC §5.4.6.
pub fn format_canonical_float(f: f64) -> String {
    if !f.is_finite() {
        // Non-finite floats have NO canonical JSON form: `canonicalize` /
        // `canonical_json` reject them with `E_CANONICAL_NONFINITE` before any
        // value reaches here (the TS/Python/Julia siblings `throw`/`raise` at the
        // equivalent point). This helper returns `String`, so it cannot signal an
        // error; if an unguarded external caller does reach this branch, emit a
        // DISTINCT, human-readable token per value — never a valid JSON number, so
        // it can never masquerade as finite — instead of collapsing +Inf, -Inf,
        // and NaN to the same misleading `"NaN"`.
        return if f.is_nan() {
            "NaN".into()
        } else if f > 0.0 {
            "Infinity".into()
        } else {
            "-Infinity".into()
        };
    }
    if f == 0.0 {
        return if f.is_sign_negative() {
            "-0.0".into()
        } else {
            "0.0".into()
        };
    }
    let abs = f.abs();
    let use_exp = !(1e-6..1e21).contains(&abs);
    if use_exp {
        // Use Rust's shortest round-trip; format!("{:e}", f) emits e.g. "1e25", "3e-7"
        // (no leading + on exponent).
        let s = format!("{f:e}");
        // Strip leading + (Rust doesn't emit it but be safe) and leading exponent zeros.
        normalize_exponent(&s)
    } else {
        // Plain decimal — Rust's `Display` gives the shortest round-trip and, in
        // the `[1e-6, 1e21)` range, always emits plain decimal (never exponent
        // form). It may print an integer-valued float as e.g. `1` (no `.`), so add
        // a trailing `.0` when no decimal point is present.
        let s = format!("{f}");
        if !s.contains('.') {
            format!("{s}.0")
        } else {
            s
        }
    }
}

fn normalize_exponent(s: &str) -> String {
    if let Some(idx) = s.find('e') {
        let (mant, exp) = s.split_at(idx);
        let exp = &exp[1..]; // strip 'e'
        let exp = exp.strip_prefix('+').unwrap_or(exp);
        let (sign, digits) = if let Some(rest) = exp.strip_prefix('-') {
            ("-", rest)
        } else {
            ("", exp)
        };
        let digits = digits.trim_start_matches('0');
        let digits = if digits.is_empty() { "0" } else { digits };
        format!("{mant}e{sign}{digits}")
    } else {
        s.into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn op(name: &str, args: Vec<Expr>) -> Expr {
        Expr::operator(ExpressionNode {
            op: name.into(),
            args,
            ..ExpressionNode::default()
        })
    }

    #[test]
    fn float_format_table() {
        let cases = [
            (1.0_f64, "1.0"),
            (-3.0, "-3.0"),
            (0.0, "0.0"),
            (-0.0_f64, "-0.0"),
            (2.5, "2.5"),
            (1e25, "1e25"),
            (5e-324, "5e-324"),
            (1e-7, "1e-7"),
        ];
        for (v, want) in cases {
            assert_eq!(format_canonical_float(v), want, "for {v}");
        }
        let mixed = 0.1_f64 + 0.2_f64;
        assert_eq!(format_canonical_float(mixed), "0.30000000000000004");
    }

    #[test]
    fn integer_emission() {
        for (v, want) in [(1_i64, "1"), (-42, "-42"), (0, "0")] {
            assert_eq!(canonical_json(&Expr::Integer(v)).unwrap(), want);
        }
    }

    #[test]
    fn nonfinite_errors() {
        for f in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            assert_eq!(
                canonicalize(&Expr::Number(f)).unwrap_err(),
                CanonicalizeError::NonFinite
            );
        }
    }

    #[test]
    fn worked_example() {
        // +(*(a, 0), b, +(a, 1)) -> +(1, "a", "b")
        let e = op(
            "+",
            vec![
                op("*", vec![Expr::Variable("a".into()), Expr::Integer(0)]),
                Expr::Variable("b".into()),
                op("+", vec![Expr::Variable("a".into()), Expr::Integer(1)]),
            ],
        );
        let got = canonical_json(&e).unwrap();
        assert_eq!(got, r#"{"args":[1,"a","b"],"op":"+"}"#);
    }

    #[test]
    fn flatten_basic() {
        let e = op(
            "+",
            vec![
                op(
                    "+",
                    vec![Expr::Variable("a".into()), Expr::Variable("b".into())],
                ),
                Expr::Variable("c".into()),
            ],
        );
        assert_eq!(
            canonical_json(&e).unwrap(),
            r#"{"args":["a","b","c"],"op":"+"}"#
        );
    }

    #[test]
    fn type_preserving_identity() {
        // *(1, x) -> "x"
        let e1 = op("*", vec![Expr::Integer(1), Expr::Variable("x".into())]);
        assert_eq!(canonical_json(&e1).unwrap(), r#""x""#);
        // *(1.0, x) keeps the 1.0
        let e2 = op("*", vec![Expr::Number(1.0), Expr::Variable("x".into())]);
        assert_eq!(
            canonical_json(&e2).unwrap(),
            r#"{"args":[1.0,"x"],"op":"*"}"#
        );
    }

    #[test]
    fn zero_annihilation_type_preserve() {
        // *(0, x) -> 0
        let e1 = op("*", vec![Expr::Integer(0), Expr::Variable("x".into())]);
        assert_eq!(canonical_json(&e1).unwrap(), "0");
        // *(0.0, x) -> 0.0
        let e2 = op("*", vec![Expr::Number(0.0), Expr::Variable("x".into())]);
        assert_eq!(canonical_json(&e2).unwrap(), "0.0");
        // *(-0.0, x) -> -0.0
        let e3 = op("*", vec![Expr::Number(-0.0), Expr::Variable("x".into())]);
        assert_eq!(canonical_json(&e3).unwrap(), "-0.0");
    }

    #[test]
    fn int_float_disambiguation() {
        let a = op("+", vec![Expr::Number(1.0), Expr::Number(2.5)]);
        let b = op("+", vec![Expr::Integer(1), Expr::Number(2.5)]);
        let ja = canonical_json(&a).unwrap();
        let jb = canonical_json(&b).unwrap();
        assert_ne!(ja, jb, "int/float distinction lost: {ja} == {jb}");
        assert!(ja.contains("1.0"), "float 1.0 not emitted as 1.0: {ja}");
    }

    #[test]
    fn neg_canonical() {
        let inner = op("neg", vec![Expr::Variable("x".into())]);
        let outer = op("neg", vec![inner]);
        assert_eq!(canonical_json(&outer).unwrap(), r#""x""#);
        let lit = op("neg", vec![Expr::Integer(5)]);
        assert_eq!(canonical_json(&lit).unwrap(), "-5");
        let sub = op("-", vec![Expr::Integer(0), Expr::Variable("x".into())]);
        assert_eq!(
            canonical_json(&sub).unwrap(),
            r#"{"args":["x"],"op":"neg"}"#
        );
    }

    #[test]
    fn div_zero_by_zero() {
        let e = op("/", vec![Expr::Integer(0), Expr::Integer(0)]);
        assert_eq!(canonicalize(&e).unwrap_err(), CanonicalizeError::DivByZero);
    }

    /// A node carrying any NON-EMISSIBLE field fails closed with the pinned
    /// cross-language code `E_CANONICAL_UNSUPPORTED_FIELD`, matching Julia's
    /// `_emit_node_json` guard. Covers a single-child sidecar (`filter`,
    /// `expr`), a scalar sidecar (`table`), and a bare-string sidecar (`id`).
    #[test]
    fn non_emissible_field_fails_closed() {
        // filter (Option<Box<Expr>>).
        let filtered = Expr::operator(ExpressionNode {
            op: "faq".into(),
            args: vec![Expr::Variable("x".into())],
            filter: Some(Box::new(Expr::Variable("p".into()))),
            ..ExpressionNode::default()
        });
        // expr (Option<Box<Expr>>).
        let bodied = Expr::operator(ExpressionNode {
            op: "faq".into(),
            args: vec![Expr::Variable("A".into())],
            expr: Some(Box::new(Expr::Variable("A".into()))),
            ..ExpressionNode::default()
        });
        // table (Option<String>).
        let tabled = Expr::operator(ExpressionNode {
            op: "table_lookup".into(),
            args: vec![],
            table: Some("t".into()),
            ..ExpressionNode::default()
        });
        // id (Option<String>).
        let ided = Expr::operator(ExpressionNode {
            op: "intersect_polygon".into(),
            args: vec![Expr::Variable("poly".into())],
            id: Some("g0".into()),
            ..ExpressionNode::default()
        });
        for e in [&filtered, &bodied, &tabled, &ided] {
            let err = canonical_json(e).unwrap_err();
            assert!(
                matches!(err, CanonicalizeError::UnsupportedField(_)),
                "expected UnsupportedField, got {err:?}"
            );
            assert_eq!(err.to_string(), "E_CANONICAL_UNSUPPORTED_FIELD");
        }
    }

    /// Regression (bug A): a `skolem` node's documentary `label` tag has no
    /// canonical wire slot, so a node carrying it MUST fail closed (matching
    /// Julia's derived `_NON_EMISSIBLE_FIELDS`). Previously `label` was omitted
    /// from the guard and silently DROPPED, so two `skolem` nodes differing only
    /// in `label` canonicalized to byte-identical JSON.
    #[test]
    fn skolem_label_fails_closed() {
        let edge = Expr::operator(ExpressionNode {
            op: "skolem".into(),
            args: vec![Expr::Variable("u".into()), Expr::Variable("v".into())],
            label: Some("edge".into()),
            ..ExpressionNode::default()
        });
        let err = canonical_json(&edge).unwrap_err();
        assert_eq!(err, CanonicalizeError::UnsupportedField("label".into()));
        assert_eq!(err.to_string(), "E_CANONICAL_UNSUPPORTED_FIELD");

        // The bare (label-less) skolem node still canonicalizes fine, so it is
        // specifically the `label` tag that fails closed — not the `skolem` op.
        let bare = Expr::operator(ExpressionNode {
            op: "skolem".into(),
            args: vec![Expr::Variable("u".into()), Expr::Variable("v".into())],
            ..ExpressionNode::default()
        });
        assert!(canonical_json(&bare).is_ok());
    }

    /// A non-emissible field on a node reached through the `args` spine (not
    /// just the root) is also caught — matching Julia's recursion into `args`.
    #[test]
    fn non_emissible_field_in_args_fails_closed() {
        let e = op(
            "+",
            vec![
                Expr::Variable("x".into()),
                Expr::operator(ExpressionNode {
                    op: "table_lookup".into(),
                    args: vec![],
                    table: Some("t".into()),
                    ..ExpressionNode::default()
                }),
            ],
        );
        assert_eq!(
            canonical_json(&e).unwrap_err().to_string(),
            "E_CANONICAL_UNSUPPORTED_FIELD"
        );
    }

    /// The EMISSIBLE `fn`/`name` fields still round-trip (they have a wire
    /// slot), and the TOLERATED-AND-IGNORED `arg`/`bindings` fields are NOT an
    /// error — they canonicalize, emitting the pinned fields only. Matches
    /// Julia's `_EMISSIBLE_FIELDS` / `_CANONICAL_IGNORED_FIELDS`.
    #[test]
    fn emissible_and_tolerated_fields_round_trip() {
        // `fn` (broadcast_fn) is emissible → emitted.
        let bc = Expr::operator(ExpressionNode {
            op: "broadcast".into(),
            args: vec![Expr::Variable("u".into())],
            broadcast_fn: Some("sin".into()),
            ..ExpressionNode::default()
        });
        let got = canonical_json(&bc).unwrap();
        assert!(got.contains(r#""fn":"sin""#), "fn not emitted: {got}");

        // `name` is emissible → emitted.
        let named = Expr::operator(ExpressionNode {
            op: "fn".into(),
            args: vec![Expr::Variable("x".into())],
            name: Some("m.f".into()),
            ..ExpressionNode::default()
        });
        assert!(
            canonical_json(&named).unwrap().contains(r#""name":"m.f""#),
            "name not emitted"
        );

        // `arg` and `bindings` are tolerated-and-ignored → no error, not emitted.
        let mut bindings = std::collections::HashMap::new();
        bindings.insert("t".to_string(), Expr::Variable("z".into()));
        let tolerated = Expr::operator(ExpressionNode {
            op: "argmax".into(),
            args: vec![Expr::Variable("x".into())],
            arg: Some("i".into()),
            bindings: Some(bindings),
            ..ExpressionNode::default()
        });
        let got = canonical_json(&tolerated).expect("tolerated fields must not error");
        assert!(!got.contains("\"arg\""), "arg leaked into emission: {got}");
        assert!(
            !got.contains("bindings"),
            "bindings leaked into emission: {got}"
        );
    }

    /// `canonicalize` alone STILL preserves every field on pass-through nodes
    /// (only `canonical_json` fails closed). The fail-close lives in
    /// `validate_emissible`, not in the tree rewrite.
    #[test]
    fn canonicalize_alone_preserves_fields() {
        let e = Expr::operator(ExpressionNode {
            op: "table_lookup".into(),
            args: vec![],
            table: Some("mytable".into()),
            output: Some(serde_json::json!(0)),
            ..ExpressionNode::default()
        });
        let c = canonicalize(&e).expect("canonicalize must not fail-close");
        match c {
            Expr::Operator(n) => {
                assert_eq!(n.table.as_deref(), Some("mytable"));
                assert_eq!(n.output, Some(serde_json::json!(0)));
            }
            other => panic!("expected preserved operator, got {other:?}"),
        }
        // But emission fails closed.
        assert_eq!(
            canonical_json(&e).unwrap_err().to_string(),
            "E_CANONICAL_UNSUPPORTED_FIELD"
        );
    }

    /// Conformance fixture consumer — the same fixture set is run by every
    /// binding's tests; passing here means this binding produces canonical
    /// output that matches the cross-binding contract.
    #[test]
    fn cross_binding_conformance_fixtures() {
        use std::path::PathBuf;
        let manifest_dir = env!("CARGO_MANIFEST_DIR");
        // pkg/earthsci-ast-rs -> repo root is 2 levels up.
        let repo_root: PathBuf = PathBuf::from(manifest_dir)
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .to_path_buf();
        let dir = repo_root
            .join("tests")
            .join("conformance")
            .join("canonical");
        let manifest_bytes = std::fs::read(dir.join("manifest.json")).expect("read manifest");
        let manifest: serde_json::Value =
            serde_json::from_slice(&manifest_bytes).expect("parse manifest");
        let fixtures = manifest["fixtures"].as_array().expect("fixtures array");
        assert!(!fixtures.is_empty(), "manifest has no fixtures");
        for f in fixtures {
            let id = f["id"].as_str().unwrap();
            let path = dir.join(f["path"].as_str().unwrap());
            let raw = std::fs::read(&path).expect("read fixture");
            let fixture: serde_json::Value = serde_json::from_slice(&raw).expect("parse fixture");
            let input_json = fixture["input"].clone();
            let expr: Expr = serde_json::from_value(input_json).expect("decode input as Expr");
            if let Some(code) = fixture.get("expect_error").and_then(|v| v.as_str()) {
                // Fail-closed fixture: `canonical_json` must return the pinned
                // stable error code (e.g. `E_CANONICAL_UNSUPPORTED_FIELD`).
                let err = canonical_json(&expr)
                    .expect_err(&format!("fixture {id}: expected error {code}, got Ok"));
                assert_eq!(
                    err.to_string(),
                    code,
                    "fixture {id}: error code mismatch (got {err}, want {code})"
                );
            } else {
                let got = canonical_json(&expr).expect("canonicalize");
                let want = fixture["expected"].as_str().unwrap();
                assert_eq!(got, want, "fixture {id}: got {got}, want {want}");
            }
        }
    }
}