oxilean-parse 0.1.2

OxiLean parser - Concrete syntax to abstract syntax
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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

#[allow(unused_imports)]
use crate::ast::{SimpleNodeKindExt, TreeNodeExt};
use crate::tokens::{Span, StringPart};

use super::functions::*;

/// Binder kind.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BinderKind {
    /// Default (explicit argument)
    Default,
    /// Implicit argument: {x : alpha}
    Implicit,
    /// Instance argument: [x : alpha]
    Instance,
    /// Strict implicit: {{x : alpha}}
    StrictImplicit,
}
/// A path in a tree, represented as a sequence of child indices.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TreePathExt(pub Vec<usize>);
impl TreePathExt {
    /// Create an empty (root) path.
    #[allow(dead_code)]
    pub fn root() -> Self {
        TreePathExt(Vec::new())
    }
    /// Extend this path with a child index.
    #[allow(dead_code)]
    pub fn child(&self, idx: usize) -> Self {
        let mut v = self.0.clone();
        v.push(idx);
        TreePathExt(v)
    }
    /// Returns the depth of this path.
    #[allow(dead_code)]
    pub fn depth(&self) -> usize {
        self.0.len()
    }
}
/// Computes tree statistics (depth, breadth, leaf count).
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone, Default)]
pub struct TreeStatsExt2 {
    /// Maximum depth of the tree
    pub max_depth: usize,
    /// Total number of nodes
    pub node_count: usize,
    /// Total number of leaves
    pub leaf_count: usize,
    /// Maximum branching factor seen
    pub max_branching: usize,
}
/// Binder for lambda and Pi types.
#[derive(Clone, Debug, PartialEq)]
pub struct Binder {
    /// Variable name (can be _ for unused)
    pub name: String,
    /// Type annotation (optional for lambda, required for Pi)
    pub ty: Option<Box<Located<SurfaceExpr>>>,
    /// Binder info (implicit, instance, etc.)
    pub info: BinderKind,
}
/// A where clause for local definitions attached to a definition or theorem.
///
/// Example:
/// ```text
/// def foo (n : Nat) : Nat := bar n + baz n where
///   bar (x : Nat) : Nat := x + 1
///   baz (x : Nat) : Nat := x * 2
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct WhereClause {
    /// Name of the local definition.
    pub name: String,
    /// Parameters of the local definition.
    pub params: Vec<Binder>,
    /// Optional type annotation.
    pub ty: Option<Located<SurfaceExpr>>,
    /// Value (body) of the local definition.
    pub val: Located<SurfaceExpr>,
}
impl WhereClause {
    /// Create a new where clause.
    #[allow(dead_code)]
    pub fn new(
        name: String,
        params: Vec<Binder>,
        ty: Option<Located<SurfaceExpr>>,
        val: Located<SurfaceExpr>,
    ) -> Self {
        Self {
            name,
            params,
            ty,
            val,
        }
    }
}
/// A single step in a `calc` proof expression.
///
/// Example:
/// ```text
/// calc a = b := proof1
///      _ < c := proof2
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct CalcStep {
    /// Left-hand side of the step.
    pub lhs: Located<SurfaceExpr>,
    /// Relation symbol (e.g., `=`, `<`, `<=`).
    pub rel: String,
    /// Right-hand side of the step.
    pub rhs: Located<SurfaceExpr>,
    /// Proof/justification for this step.
    pub proof: Located<SurfaceExpr>,
}
impl CalcStep {
    /// Create a new calc step.
    #[allow(dead_code)]
    pub fn new(
        lhs: Located<SurfaceExpr>,
        rel: String,
        rhs: Located<SurfaceExpr>,
        proof: Located<SurfaceExpr>,
    ) -> Self {
        Self {
            lhs,
            rel,
            rhs,
            proof,
        }
    }
}
/// Surface-level expression (before elaboration).
#[derive(Clone, Debug, PartialEq)]
pub enum SurfaceExpr {
    /// Sort: Type, Prop, Type u, Sort u
    Sort(SortKind),
    /// Variable reference
    Var(String),
    /// Application: f a
    App(Box<Located<SurfaceExpr>>, Box<Located<SurfaceExpr>>),
    /// Lambda: fun (x : alpha) => body  or  lambda (x : alpha), body
    Lam(Vec<Binder>, Box<Located<SurfaceExpr>>),
    /// Pi type: forall (x : alpha), beta  or  (x : alpha) -> beta
    Pi(Vec<Binder>, Box<Located<SurfaceExpr>>),
    /// Let binding: let x : alpha := v in body
    Let(
        String,
        Option<Box<Located<SurfaceExpr>>>,
        Box<Located<SurfaceExpr>>,
        Box<Located<SurfaceExpr>>,
    ),
    /// Literal value
    Lit(Literal),
    /// Explicit type annotation: (e : tau)
    Ann(Box<Located<SurfaceExpr>>, Box<Located<SurfaceExpr>>),
    /// Placeholder: _
    Hole,
    /// Projection: e.n
    Proj(Box<Located<SurfaceExpr>>, String),
    /// If-then-else: if c then t else e
    If(
        Box<Located<SurfaceExpr>>,
        Box<Located<SurfaceExpr>>,
        Box<Located<SurfaceExpr>>,
    ),
    /// Match expression: match e with | pat => rhs | ...
    Match(Box<Located<SurfaceExpr>>, Vec<MatchArm>),
    /// Do notation: do { ... }
    Do(Vec<DoAction>),
    /// Have expression: have h : T := proof; body
    Have(
        String,
        Box<Located<SurfaceExpr>>,
        Box<Located<SurfaceExpr>>,
        Box<Located<SurfaceExpr>>,
    ),
    /// Suffices expression: suffices h : T by tactic; body
    Suffices(String, Box<Located<SurfaceExpr>>, Box<Located<SurfaceExpr>>),
    /// Show expression: show T from expr
    Show(Box<Located<SurfaceExpr>>, Box<Located<SurfaceExpr>>),
    /// Named argument application: f (x := e)
    NamedArg(Box<Located<SurfaceExpr>>, String, Box<Located<SurfaceExpr>>),
    /// Anonymous constructor: (langle) a, b, c (rangle)
    AnonymousCtor(Vec<Located<SurfaceExpr>>),
    /// List literal: [1, 2, 3]
    ListLit(Vec<Located<SurfaceExpr>>),
    /// Tuple: (a, b) or (a, b, c)
    Tuple(Vec<Located<SurfaceExpr>>),
    /// Return expression (used in `do` notation): `return e`
    Return(Box<Located<SurfaceExpr>>),
    /// String interpolation: `s!"hello {name}"`
    StringInterp(Vec<StringPart>),
    /// Range expression: `a..b`, `..b`, `a..`
    Range(
        Option<Box<Located<SurfaceExpr>>>,
        Option<Box<Located<SurfaceExpr>>>,
    ),
    /// By-tactic expression: `by tactic1; tactic2; ...`
    ByTactic(Vec<Located<TacticRef>>),
    /// Calc expression: calculational proof
    Calc(Vec<CalcStep>),
}
impl SurfaceExpr {
    /// Create a variable expression.
    #[allow(dead_code)]
    pub fn var(name: &str) -> Self {
        SurfaceExpr::Var(name.to_string())
    }
    /// Create a natural number literal expression.
    #[allow(dead_code)]
    pub fn nat(n: u64) -> Self {
        SurfaceExpr::Lit(Literal::Nat(n))
    }
    /// Create a string literal expression.
    #[allow(dead_code)]
    pub fn string(s: &str) -> Self {
        SurfaceExpr::Lit(Literal::String(s.to_string()))
    }
    /// Create a float literal expression.
    #[allow(dead_code)]
    pub fn float(v: f64) -> Self {
        SurfaceExpr::Lit(Literal::Float(v))
    }
    /// Check if this expression is a hole/placeholder.
    #[allow(dead_code)]
    pub fn is_hole(&self) -> bool {
        matches!(self, SurfaceExpr::Hole)
    }
    /// Check if this expression is a variable.
    #[allow(dead_code)]
    pub fn is_var(&self) -> bool {
        matches!(self, SurfaceExpr::Var(_))
    }
    /// Get the variable name if this is a variable.
    #[allow(dead_code)]
    pub fn as_var(&self) -> Option<&str> {
        match self {
            SurfaceExpr::Var(s) => Some(s),
            _ => None,
        }
    }
}
/// Match arm: pattern => expression
#[derive(Clone, Debug, PartialEq)]
pub struct MatchArm {
    /// Pattern
    pub pattern: Located<Pattern>,
    /// Guard (optional): | when condition
    pub guard: Option<Located<SurfaceExpr>>,
    /// Right-hand side
    pub rhs: Located<SurfaceExpr>,
}
/// A simple tree zipper for navigating trees.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct TreeZipper {
    /// Current focus node
    pub focus: TreeNodeExt,
    /// Context: (parent_label, left_siblings, right_siblings)
    pub context: Vec<(String, Vec<TreeNodeExt>, Vec<TreeNodeExt>)>,
}
impl TreeZipper {
    /// Create a zipper from a tree.
    #[allow(dead_code)]
    pub fn new(tree: TreeNodeExt) -> Self {
        TreeZipper {
            focus: tree,
            context: Vec::new(),
        }
    }
    /// Go down into the i-th child.
    #[allow(dead_code)]
    pub fn down(mut self, i: usize) -> Option<Self> {
        if i >= self.focus.children.len() {
            return None;
        }
        let mut children = self.focus.children.clone();
        let child = children.remove(i);
        let left = children[..i].to_vec();
        let right = children[i..].to_vec();
        self.context.push((self.focus.label.clone(), left, right));
        Some(TreeZipper {
            focus: child,
            context: self.context,
        })
    }
    /// Returns the depth (number of levels descended).
    #[allow(dead_code)]
    pub fn depth(&self) -> usize {
        self.context.len()
    }
    /// Returns the label of the current focus node.
    #[allow(dead_code)]
    pub fn label(&self) -> &str {
        &self.focus.label
    }
}
/// Pattern for match expressions.
#[derive(Clone, Debug, PartialEq)]
pub enum Pattern {
    /// Wildcard _
    Wild,
    /// Variable binding
    Var(String),
    /// Constructor application: C p1 p2 ...
    Ctor(String, Vec<Located<Pattern>>),
    /// Literal pattern
    Lit(Literal),
    /// Or pattern: p1 | p2
    Or(Box<Located<Pattern>>, Box<Located<Pattern>>),
}
/// Top-level declaration.
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum Decl {
    /// Axiom declaration
    Axiom {
        /// Name
        name: String,
        /// Universe parameters
        univ_params: Vec<String>,
        /// Type
        ty: Located<SurfaceExpr>,
        /// Attributes
        attrs: Vec<AttributeKind>,
    },
    /// Definition
    Definition {
        /// Name
        name: String,
        /// Universe parameters
        univ_params: Vec<String>,
        /// Type (optional, can be inferred)
        ty: Option<Located<SurfaceExpr>>,
        /// Value
        val: Located<SurfaceExpr>,
        /// Where clauses (local definitions)
        where_clauses: Vec<WhereClause>,
        /// Attributes
        attrs: Vec<AttributeKind>,
    },
    /// Theorem
    Theorem {
        /// Name
        name: String,
        /// Universe parameters
        univ_params: Vec<String>,
        /// Type (statement)
        ty: Located<SurfaceExpr>,
        /// Proof
        proof: Located<SurfaceExpr>,
        /// Where clauses (local definitions)
        where_clauses: Vec<WhereClause>,
        /// Attributes
        attrs: Vec<AttributeKind>,
    },
    /// Inductive type
    Inductive {
        /// Name
        name: String,
        /// Universe parameters
        univ_params: Vec<String>,
        /// Parameters (non-varying)
        params: Vec<Binder>,
        /// Indices (varying)
        indices: Vec<Binder>,
        /// Type
        ty: Located<SurfaceExpr>,
        /// Constructors
        ctors: Vec<Constructor>,
    },
    /// Import declaration
    Import {
        /// Module path
        path: Vec<String>,
    },
    /// Namespace declaration
    Namespace {
        /// Namespace name
        name: String,
        /// Declarations inside
        decls: Vec<Located<Decl>>,
    },
    /// Structure declaration
    Structure {
        /// Structure name
        name: String,
        /// Universe parameters
        univ_params: Vec<String>,
        /// Parent structures (extends)
        extends: Vec<String>,
        /// Fields
        fields: Vec<FieldDecl>,
    },
    /// Class declaration
    ClassDecl {
        /// Class name
        name: String,
        /// Universe parameters
        univ_params: Vec<String>,
        /// Parent classes (extends)
        extends: Vec<String>,
        /// Fields/methods
        fields: Vec<FieldDecl>,
    },
    /// Instance declaration
    InstanceDecl {
        /// Optional instance name
        name: Option<String>,
        /// Class being instantiated
        class_name: String,
        /// The instance type
        ty: Located<SurfaceExpr>,
        /// Method definitions
        defs: Vec<(String, Located<SurfaceExpr>)>,
    },
    /// Section declaration
    SectionDecl {
        /// Section name
        name: String,
        /// Declarations inside
        decls: Vec<Located<Decl>>,
    },
    /// Variable declaration
    Variable {
        /// Binders
        binders: Vec<Binder>,
    },
    /// Open declaration
    Open {
        /// Module path
        path: Vec<String>,
        /// Specific names to open (empty = open all)
        names: Vec<String>,
    },
    /// Attribute declaration
    Attribute {
        /// Attribute names
        attrs: Vec<String>,
        /// Inner declaration
        decl: Box<Located<Decl>>,
    },
    /// Hash command (#check, #eval, #print)
    HashCmd {
        /// Command name (e.g. "check", "eval", "print")
        cmd: String,
        /// Argument expression
        arg: Located<SurfaceExpr>,
    },
    /// Mutual recursive definitions
    Mutual {
        /// The mutually recursive declarations
        decls: Vec<Located<Decl>>,
    },
    /// Derive declaration: `deriving instance1, instance2 for TypeName`
    Derive {
        /// Instance strategies to derive
        instances: Vec<String>,
        /// Type name to derive for
        type_name: String,
    },
    /// Notation declaration
    NotationDecl {
        /// Kind of notation
        kind: AstNotationKind,
        /// Precedence (optional)
        prec: Option<u32>,
        /// Name or symbol
        name: String,
        /// Notation body
        notation: String,
    },
    /// Universe declaration: `universe u v w`
    Universe {
        /// Universe variable names
        names: Vec<String>,
    },
}
impl Decl {
    /// Get the name of this declaration, if it has one.
    #[allow(dead_code)]
    pub fn name(&self) -> Option<&str> {
        match self {
            Decl::Axiom { name, .. }
            | Decl::Definition { name, .. }
            | Decl::Theorem { name, .. }
            | Decl::Inductive { name, .. }
            | Decl::Namespace { name, .. }
            | Decl::Structure { name, .. }
            | Decl::ClassDecl { name, .. }
            | Decl::SectionDecl { name, .. } => Some(name),
            Decl::InstanceDecl { name, .. } => name.as_deref(),
            Decl::Derive { type_name, .. } => Some(type_name),
            Decl::NotationDecl { name, .. } => Some(name),
            _ => None,
        }
    }
    /// Get the attributes if this declaration has typed attributes.
    #[allow(dead_code)]
    pub fn typed_attrs(&self) -> &[AttributeKind] {
        match self {
            Decl::Axiom { attrs, .. }
            | Decl::Definition { attrs, .. }
            | Decl::Theorem { attrs, .. } => attrs,
            _ => &[],
        }
    }
    /// Check if this declaration is a mutual block.
    #[allow(dead_code)]
    pub fn is_mutual(&self) -> bool {
        matches!(self, Decl::Mutual { .. })
    }
}
/// Kind of notation declaration in the AST.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AstNotationKind {
    /// Prefix operator (e.g., `-x`)
    Prefix,
    /// Postfix operator (e.g., `x!`)
    Postfix,
    /// Left-associative infix operator
    Infixl,
    /// Right-associative infix operator
    Infixr,
    /// General notation
    Notation,
}
/// A located expression (expression with source span).
#[derive(Clone, Debug, PartialEq)]
pub struct Located<T> {
    /// The value
    pub value: T,
    /// Source span
    pub span: Span,
}
impl<T> Located<T> {
    /// Create a new located value.
    pub fn new(value: T, span: Span) -> Self {
        Self { value, span }
    }
    /// Map the inner value while preserving the span.
    #[allow(dead_code)]
    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Located<U> {
        Located {
            value: f(self.value),
            span: self.span,
        }
    }
}
/// Typed attribute kinds for declarations.
///
/// In Lean 4, attributes like `@[simp]`, `@[ext]`, `@[reducible]` control
/// how the elaborator and tactics treat definitions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AttributeKind {
    /// `@[simp]` - add to the simp set
    Simp,
    /// `@[ext]` - extensionality lemma
    Ext,
    /// `@[instance]` - typeclass instance
    Instance,
    /// `@[reducible]` - mark as reducible for definitional equality
    Reducible,
    /// `@[irreducible]` - mark as irreducible
    Irreducible,
    /// `@[inline]` - inline during compilation
    Inline,
    /// `@[noinline]` - do not inline
    NoInline,
    /// `@[specialize]` - specialization hint
    SpecializeAttr,
    /// Custom user attribute
    Custom(String),
}
impl AttributeKind {
    /// Check if this is a custom attribute.
    #[allow(dead_code)]
    pub fn is_custom(&self) -> bool {
        matches!(self, AttributeKind::Custom(_))
    }
    /// Get the name of the attribute as a string.
    #[allow(dead_code)]
    pub fn name(&self) -> &str {
        match self {
            AttributeKind::Simp => "simp",
            AttributeKind::Ext => "ext",
            AttributeKind::Instance => "instance",
            AttributeKind::Reducible => "reducible",
            AttributeKind::Irreducible => "irreducible",
            AttributeKind::Inline => "inline",
            AttributeKind::NoInline => "noinline",
            AttributeKind::SpecializeAttr => "specialize",
            AttributeKind::Custom(s) => s,
        }
    }
}
/// Literal values.
#[derive(Clone, Debug, PartialEq)]
pub enum Literal {
    /// Natural number
    Nat(u64),
    /// Float
    Float(f64),
    /// String
    String(String),
    /// Character
    Char(char),
}
/// Do notation action.
#[derive(Clone, Debug, PartialEq)]
pub enum DoAction {
    /// let x := e
    Let(String, Located<SurfaceExpr>),
    /// let x : tau := e
    LetTyped(String, Located<SurfaceExpr>, Located<SurfaceExpr>),
    /// x <- e
    Bind(String, Located<SurfaceExpr>),
    /// pure expression
    Expr(Located<SurfaceExpr>),
    /// return e
    Return(Located<SurfaceExpr>),
}
/// Field declaration for structures and classes.
#[derive(Clone, Debug, PartialEq)]
pub struct FieldDecl {
    /// Field name
    pub name: String,
    /// Field type
    pub ty: Located<SurfaceExpr>,
    /// Default value (optional)
    pub default: Option<Located<SurfaceExpr>>,
}
/// An identity transform that returns the input unchanged.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct IdentityTransformExt;
/// Constructor for inductive types.
#[derive(Clone, Debug, PartialEq)]
pub struct Constructor {
    /// Constructor name
    pub name: String,
    /// Constructor type
    pub ty: Located<SurfaceExpr>,
}
/// A memo table for tree transformations (cache-aside pattern).
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct TransformMemo {
    /// Cached results keyed by fingerprint
    pub cache: std::collections::HashMap<u64, TreeNodeExt>,
}
impl TransformMemo {
    /// Create a new empty memo table.
    #[allow(dead_code)]
    pub fn new() -> Self {
        TransformMemo {
            cache: std::collections::HashMap::new(),
        }
    }
    /// Look up a cached result.
    #[allow(dead_code)]
    pub fn get(&self, key: u64) -> Option<&TreeNodeExt> {
        self.cache.get(&key)
    }
    /// Store a result.
    #[allow(dead_code)]
    pub fn store(&mut self, key: u64, result: TreeNodeExt) {
        self.cache.insert(key, result);
    }
    /// Returns the number of cached entries.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.cache.len()
    }
    /// Returns true if empty.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.cache.is_empty()
    }
}
/// A tree statistics collector.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct TreeStats {
    /// Total node count
    pub nodes: usize,
    /// Leaf node count
    pub leaves: usize,
    /// Maximum depth
    pub max_depth: usize,
    /// Total tree size
    pub total_size: usize,
}
impl TreeStats {
    /// Create an empty stats record.
    #[allow(dead_code)]
    pub fn new() -> Self {
        TreeStats::default()
    }
    /// Compute stats from a tree.
    #[allow(dead_code)]
    pub fn from_tree(root: &TreeNodeExt) -> Self {
        let mut stats = TreeStats::new();
        collect_stats(root, 0, &mut stats);
        stats
    }
}
/// A zipper-based tree cursor for incremental editing.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct TreeCursor {
    /// Current node
    pub current: TreeNodeExt,
    /// Path back to root
    pub ancestors: Vec<TreeNodeExt>,
}
impl TreeCursor {
    /// Create a cursor at the root.
    #[allow(dead_code)]
    pub fn new(root: TreeNodeExt) -> Self {
        TreeCursor {
            current: root,
            ancestors: Vec::new(),
        }
    }
    /// Returns the kind of the current node.
    #[allow(dead_code)]
    pub fn kind(&self) -> &SimpleNodeKindExt {
        &self.current.kind
    }
    /// Returns the label of the current node.
    #[allow(dead_code)]
    pub fn label(&self) -> &str {
        &self.current.label
    }
    /// Returns the child count.
    #[allow(dead_code)]
    pub fn child_count(&self) -> usize {
        self.current.children.len()
    }
    /// Returns the depth.
    #[allow(dead_code)]
    pub fn depth(&self) -> usize {
        self.ancestors.len()
    }
}
/// A tree zipper for navigation.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct TreeZipperExt {
    /// The focused node
    pub focus: TreeNodeExt,
    /// The path to the focus
    pub path: TreePathExt,
    /// Ancestors (parent, sibling index, left siblings, right siblings)
    pub ancestors: Vec<(
        SimpleNodeKindExt,
        String,
        Vec<TreeNodeExt>,
        Vec<TreeNodeExt>,
    )>,
}
impl TreeZipperExt {
    /// Create a zipper from a root.
    #[allow(dead_code)]
    pub fn new(root: TreeNodeExt) -> Self {
        TreeZipperExt {
            focus: root,
            path: TreePathExt::root(),
            ancestors: Vec::new(),
        }
    }
    /// Move into the i-th child.
    #[allow(dead_code)]
    pub fn down(mut self, i: usize) -> Option<Self> {
        if i >= self.focus.children.len() {
            return None;
        }
        let mut children = self.focus.children;
        let child = children.remove(i);
        let left: Vec<TreeNodeExt> = children.drain(..i.min(children.len())).collect();
        let right: Vec<TreeNodeExt> = children;
        self.ancestors
            .push((self.focus.kind, self.focus.label, left, right));
        self.path = self.path.child(i);
        Some(TreeZipperExt {
            focus: child,
            path: self.path,
            ancestors: self.ancestors,
        })
    }
    /// Move back up to the parent.
    #[allow(dead_code)]
    pub fn up(mut self) -> Option<(Self, TreeNodeExt)> {
        let (kind, label, left, right) = self.ancestors.pop()?;
        let child = self.focus;
        let mut children = left;
        children.push(child.clone());
        children.extend(right);
        let mut new_path = self.path;
        new_path.0.pop();
        Some((
            TreeZipperExt {
                focus: TreeNodeExt {
                    kind,
                    label,
                    children,
                },
                path: new_path,
                ancestors: self.ancestors,
            },
            child,
        ))
    }
}
/// A transform that renames all leaf nodes matching a pattern.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct RenameTransformExt {
    /// Name to rename from
    pub from: String,
    /// Name to rename to
    pub to: String,
    /// Counter of renames performed
    pub count: usize,
}
impl RenameTransformExt {
    /// Create a new rename transform.
    #[allow(dead_code)]
    pub fn new(from: &str, to: &str) -> Self {
        RenameTransformExt {
            from: from.to_string(),
            to: to.to_string(),
            count: 0,
        }
    }
}
/// A simple tree cache that memoises node computations.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct NodeCache {
    /// Cached values by label
    pub values: std::collections::HashMap<String, u64>,
}
impl NodeCache {
    /// Create a new cache.
    #[allow(dead_code)]
    pub fn new() -> Self {
        NodeCache {
            values: std::collections::HashMap::new(),
        }
    }
    /// Store a value.
    #[allow(dead_code)]
    pub fn store(&mut self, key: &str, val: u64) {
        self.values.insert(key.to_string(), val);
    }
    /// Retrieve a value.
    #[allow(dead_code)]
    pub fn get(&self, key: &str) -> Option<u64> {
        self.values.get(key).copied()
    }
}
/// A memoized computation table for tree transformations.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct TransformMemoExt2 {
    /// Cache from label to transformed label
    pub cache: std::collections::HashMap<String, String>,
}
impl TransformMemoExt2 {
    /// Create a new empty memo table.
    #[allow(dead_code)]
    pub fn new() -> Self {
        TransformMemoExt2 {
            cache: std::collections::HashMap::new(),
        }
    }
    /// Check if a label is already memoized.
    #[allow(dead_code)]
    pub fn has(&self, label: &str) -> bool {
        self.cache.contains_key(label)
    }
    /// Get the memoized result for a label.
    #[allow(dead_code)]
    pub fn get(&self, label: &str) -> Option<&str> {
        self.cache.get(label).map(|s| s.as_str())
    }
    /// Insert a memoized result.
    #[allow(dead_code)]
    pub fn insert(&mut self, label: &str, result: &str) {
        self.cache.insert(label.to_string(), result.to_string());
    }
    /// Returns the number of memoized entries.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.cache.len()
    }
    /// Returns true if empty.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.cache.is_empty()
    }
}
/// Sort kinds.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SortKind {
    /// Type (Type 0)
    Type,
    /// Prop (Sort 0)
    Prop,
    /// Type u (explicit universe)
    TypeU(String),
    /// Sort u (explicit universe)
    SortU(String),
}