1#[allow(unused_imports)]
6use crate::ast::{SimpleNodeKindExt, TreeNodeExt};
7use crate::tokens::{Span, StringPart};
8
9use super::functions::*;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
13pub enum BinderKind {
14 Default,
16 Implicit,
18 Instance,
20 StrictImplicit,
22}
23#[allow(dead_code)]
25#[allow(missing_docs)]
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct TreePathExt(pub Vec<usize>);
28impl TreePathExt {
29 #[allow(dead_code)]
31 pub fn root() -> Self {
32 TreePathExt(Vec::new())
33 }
34 #[allow(dead_code)]
36 pub fn child(&self, idx: usize) -> Self {
37 let mut v = self.0.clone();
38 v.push(idx);
39 TreePathExt(v)
40 }
41 #[allow(dead_code)]
43 pub fn depth(&self) -> usize {
44 self.0.len()
45 }
46}
47#[allow(dead_code)]
49#[allow(missing_docs)]
50#[derive(Debug, Clone, Default)]
51pub struct TreeStatsExt2 {
52 pub max_depth: usize,
54 pub node_count: usize,
56 pub leaf_count: usize,
58 pub max_branching: usize,
60}
61#[derive(Clone, Debug, PartialEq)]
63pub struct Binder {
64 pub name: String,
66 pub ty: Option<Box<Located<SurfaceExpr>>>,
68 pub info: BinderKind,
70}
71#[derive(Clone, Debug, PartialEq)]
80pub struct WhereClause {
81 pub name: String,
83 pub params: Vec<Binder>,
85 pub ty: Option<Located<SurfaceExpr>>,
87 pub val: Located<SurfaceExpr>,
89}
90impl WhereClause {
91 #[allow(dead_code)]
93 pub fn new(
94 name: String,
95 params: Vec<Binder>,
96 ty: Option<Located<SurfaceExpr>>,
97 val: Located<SurfaceExpr>,
98 ) -> Self {
99 Self {
100 name,
101 params,
102 ty,
103 val,
104 }
105 }
106}
107#[derive(Clone, Debug, PartialEq)]
115pub struct CalcStep {
116 pub lhs: Located<SurfaceExpr>,
118 pub rel: String,
120 pub rhs: Located<SurfaceExpr>,
122 pub proof: Located<SurfaceExpr>,
124}
125impl CalcStep {
126 #[allow(dead_code)]
128 pub fn new(
129 lhs: Located<SurfaceExpr>,
130 rel: String,
131 rhs: Located<SurfaceExpr>,
132 proof: Located<SurfaceExpr>,
133 ) -> Self {
134 Self {
135 lhs,
136 rel,
137 rhs,
138 proof,
139 }
140 }
141}
142#[derive(Clone, Debug, PartialEq)]
144pub enum SurfaceExpr {
145 Sort(SortKind),
147 Var(String),
149 App(Box<Located<SurfaceExpr>>, Box<Located<SurfaceExpr>>),
151 Lam(Vec<Binder>, Box<Located<SurfaceExpr>>),
153 Pi(Vec<Binder>, Box<Located<SurfaceExpr>>),
155 Let(
157 String,
158 Option<Box<Located<SurfaceExpr>>>,
159 Box<Located<SurfaceExpr>>,
160 Box<Located<SurfaceExpr>>,
161 ),
162 Lit(Literal),
164 Ann(Box<Located<SurfaceExpr>>, Box<Located<SurfaceExpr>>),
166 Hole,
168 Proj(Box<Located<SurfaceExpr>>, String),
170 If(
172 Box<Located<SurfaceExpr>>,
173 Box<Located<SurfaceExpr>>,
174 Box<Located<SurfaceExpr>>,
175 ),
176 Match(Box<Located<SurfaceExpr>>, Vec<MatchArm>),
178 Do(Vec<DoAction>),
180 Have(
182 String,
183 Box<Located<SurfaceExpr>>,
184 Box<Located<SurfaceExpr>>,
185 Box<Located<SurfaceExpr>>,
186 ),
187 Suffices(String, Box<Located<SurfaceExpr>>, Box<Located<SurfaceExpr>>),
189 Show(Box<Located<SurfaceExpr>>, Box<Located<SurfaceExpr>>),
191 NamedArg(Box<Located<SurfaceExpr>>, String, Box<Located<SurfaceExpr>>),
193 AnonymousCtor(Vec<Located<SurfaceExpr>>),
195 ListLit(Vec<Located<SurfaceExpr>>),
197 Tuple(Vec<Located<SurfaceExpr>>),
199 Return(Box<Located<SurfaceExpr>>),
201 StringInterp(Vec<StringPart>),
203 Range(
205 Option<Box<Located<SurfaceExpr>>>,
206 Option<Box<Located<SurfaceExpr>>>,
207 ),
208 ByTactic(Vec<Located<TacticRef>>),
210 Calc(Vec<CalcStep>),
212}
213impl SurfaceExpr {
214 #[allow(dead_code)]
216 pub fn var(name: &str) -> Self {
217 SurfaceExpr::Var(name.to_string())
218 }
219 #[allow(dead_code)]
221 pub fn nat(n: u64) -> Self {
222 SurfaceExpr::Lit(Literal::Nat(n))
223 }
224 #[allow(dead_code)]
226 pub fn string(s: &str) -> Self {
227 SurfaceExpr::Lit(Literal::String(s.to_string()))
228 }
229 #[allow(dead_code)]
231 pub fn float(v: f64) -> Self {
232 SurfaceExpr::Lit(Literal::Float(v))
233 }
234 #[allow(dead_code)]
236 pub fn is_hole(&self) -> bool {
237 matches!(self, SurfaceExpr::Hole)
238 }
239 #[allow(dead_code)]
241 pub fn is_var(&self) -> bool {
242 matches!(self, SurfaceExpr::Var(_))
243 }
244 #[allow(dead_code)]
246 pub fn as_var(&self) -> Option<&str> {
247 match self {
248 SurfaceExpr::Var(s) => Some(s),
249 _ => None,
250 }
251 }
252}
253#[derive(Clone, Debug, PartialEq)]
255pub struct MatchArm {
256 pub pattern: Located<Pattern>,
258 pub guard: Option<Located<SurfaceExpr>>,
260 pub rhs: Located<SurfaceExpr>,
262}
263#[allow(dead_code)]
265#[allow(missing_docs)]
266#[derive(Debug, Clone)]
267pub struct TreeZipper {
268 pub focus: TreeNodeExt,
270 pub context: Vec<(String, Vec<TreeNodeExt>, Vec<TreeNodeExt>)>,
272}
273impl TreeZipper {
274 #[allow(dead_code)]
276 pub fn new(tree: TreeNodeExt) -> Self {
277 TreeZipper {
278 focus: tree,
279 context: Vec::new(),
280 }
281 }
282 #[allow(dead_code)]
284 pub fn down(mut self, i: usize) -> Option<Self> {
285 if i >= self.focus.children.len() {
286 return None;
287 }
288 let mut children = self.focus.children.clone();
289 let child = children.remove(i);
290 let left = children[..i].to_vec();
291 let right = children[i..].to_vec();
292 self.context.push((self.focus.label.clone(), left, right));
293 Some(TreeZipper {
294 focus: child,
295 context: self.context,
296 })
297 }
298 #[allow(dead_code)]
300 pub fn depth(&self) -> usize {
301 self.context.len()
302 }
303 #[allow(dead_code)]
305 pub fn label(&self) -> &str {
306 &self.focus.label
307 }
308}
309#[derive(Clone, Debug, PartialEq)]
311pub enum Pattern {
312 Wild,
314 Var(String),
316 Ctor(String, Vec<Located<Pattern>>),
318 Lit(Literal),
320 Or(Box<Located<Pattern>>, Box<Located<Pattern>>),
322}
323#[derive(Clone, Debug, PartialEq)]
325#[allow(clippy::large_enum_variant)]
326pub enum Decl {
327 Axiom {
329 name: String,
331 univ_params: Vec<String>,
333 ty: Located<SurfaceExpr>,
335 attrs: Vec<AttributeKind>,
337 },
338 Definition {
340 name: String,
342 univ_params: Vec<String>,
344 ty: Option<Located<SurfaceExpr>>,
346 val: Located<SurfaceExpr>,
348 where_clauses: Vec<WhereClause>,
350 attrs: Vec<AttributeKind>,
352 },
353 Theorem {
355 name: String,
357 univ_params: Vec<String>,
359 ty: Located<SurfaceExpr>,
361 proof: Located<SurfaceExpr>,
363 where_clauses: Vec<WhereClause>,
365 attrs: Vec<AttributeKind>,
367 },
368 Inductive {
370 name: String,
372 univ_params: Vec<String>,
374 params: Vec<Binder>,
376 indices: Vec<Binder>,
378 ty: Located<SurfaceExpr>,
380 ctors: Vec<Constructor>,
382 },
383 Import {
385 path: Vec<String>,
387 },
388 Namespace {
390 name: String,
392 decls: Vec<Located<Decl>>,
394 },
395 Structure {
397 name: String,
399 univ_params: Vec<String>,
401 extends: Vec<String>,
403 fields: Vec<FieldDecl>,
405 },
406 ClassDecl {
408 name: String,
410 univ_params: Vec<String>,
412 extends: Vec<String>,
414 fields: Vec<FieldDecl>,
416 },
417 InstanceDecl {
419 name: Option<String>,
421 class_name: String,
423 ty: Located<SurfaceExpr>,
425 defs: Vec<(String, Located<SurfaceExpr>)>,
427 },
428 SectionDecl {
430 name: String,
432 decls: Vec<Located<Decl>>,
434 },
435 Variable {
437 binders: Vec<Binder>,
439 },
440 Open {
442 path: Vec<String>,
444 names: Vec<String>,
446 },
447 Attribute {
449 attrs: Vec<String>,
451 decl: Box<Located<Decl>>,
453 },
454 HashCmd {
456 cmd: String,
458 arg: Located<SurfaceExpr>,
460 },
461 Mutual {
463 decls: Vec<Located<Decl>>,
465 },
466 Derive {
468 instances: Vec<String>,
470 type_name: String,
472 },
473 NotationDecl {
475 kind: AstNotationKind,
477 prec: Option<u32>,
479 name: String,
481 notation: String,
483 },
484 Universe {
486 names: Vec<String>,
488 },
489}
490impl Decl {
491 #[allow(dead_code)]
493 pub fn name(&self) -> Option<&str> {
494 match self {
495 Decl::Axiom { name, .. }
496 | Decl::Definition { name, .. }
497 | Decl::Theorem { name, .. }
498 | Decl::Inductive { name, .. }
499 | Decl::Namespace { name, .. }
500 | Decl::Structure { name, .. }
501 | Decl::ClassDecl { name, .. }
502 | Decl::SectionDecl { name, .. } => Some(name),
503 Decl::InstanceDecl { name, .. } => name.as_deref(),
504 Decl::Derive { type_name, .. } => Some(type_name),
505 Decl::NotationDecl { name, .. } => Some(name),
506 _ => None,
507 }
508 }
509 #[allow(dead_code)]
511 pub fn typed_attrs(&self) -> &[AttributeKind] {
512 match self {
513 Decl::Axiom { attrs, .. }
514 | Decl::Definition { attrs, .. }
515 | Decl::Theorem { attrs, .. } => attrs,
516 _ => &[],
517 }
518 }
519 #[allow(dead_code)]
521 pub fn is_mutual(&self) -> bool {
522 matches!(self, Decl::Mutual { .. })
523 }
524}
525#[derive(Clone, Debug, PartialEq, Eq)]
527pub enum AstNotationKind {
528 Prefix,
530 Postfix,
532 Infixl,
534 Infixr,
536 Notation,
538}
539#[derive(Clone, Debug, PartialEq)]
541pub struct Located<T> {
542 pub value: T,
544 pub span: Span,
546}
547impl<T> Located<T> {
548 pub fn new(value: T, span: Span) -> Self {
550 Self { value, span }
551 }
552 #[allow(dead_code)]
554 pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Located<U> {
555 Located {
556 value: f(self.value),
557 span: self.span,
558 }
559 }
560}
561#[derive(Clone, Debug, PartialEq, Eq)]
566pub enum AttributeKind {
567 Simp,
569 Ext,
571 Instance,
573 Reducible,
575 Irreducible,
577 Inline,
579 NoInline,
581 SpecializeAttr,
583 Custom(String),
585}
586impl AttributeKind {
587 #[allow(dead_code)]
589 pub fn is_custom(&self) -> bool {
590 matches!(self, AttributeKind::Custom(_))
591 }
592 #[allow(dead_code)]
594 pub fn name(&self) -> &str {
595 match self {
596 AttributeKind::Simp => "simp",
597 AttributeKind::Ext => "ext",
598 AttributeKind::Instance => "instance",
599 AttributeKind::Reducible => "reducible",
600 AttributeKind::Irreducible => "irreducible",
601 AttributeKind::Inline => "inline",
602 AttributeKind::NoInline => "noinline",
603 AttributeKind::SpecializeAttr => "specialize",
604 AttributeKind::Custom(s) => s,
605 }
606 }
607}
608#[derive(Clone, Debug, PartialEq)]
610pub enum Literal {
611 Nat(u64),
613 Float(f64),
615 String(String),
617 Char(char),
619}
620#[derive(Clone, Debug, PartialEq)]
622pub enum DoAction {
623 Let(String, Located<SurfaceExpr>),
625 LetTyped(String, Located<SurfaceExpr>, Located<SurfaceExpr>),
627 Bind(String, Located<SurfaceExpr>),
629 Expr(Located<SurfaceExpr>),
631 Return(Located<SurfaceExpr>),
633}
634#[derive(Clone, Debug, PartialEq)]
636pub struct FieldDecl {
637 pub name: String,
639 pub ty: Located<SurfaceExpr>,
641 pub default: Option<Located<SurfaceExpr>>,
643}
644#[allow(dead_code)]
646#[allow(missing_docs)]
647pub struct IdentityTransformExt;
648#[derive(Clone, Debug, PartialEq)]
650pub struct Constructor {
651 pub name: String,
653 pub ty: Located<SurfaceExpr>,
655}
656#[allow(dead_code)]
658#[allow(missing_docs)]
659pub struct TransformMemo {
660 pub cache: std::collections::HashMap<u64, TreeNodeExt>,
662}
663impl TransformMemo {
664 #[allow(dead_code)]
666 pub fn new() -> Self {
667 TransformMemo {
668 cache: std::collections::HashMap::new(),
669 }
670 }
671 #[allow(dead_code)]
673 pub fn get(&self, key: u64) -> Option<&TreeNodeExt> {
674 self.cache.get(&key)
675 }
676 #[allow(dead_code)]
678 pub fn store(&mut self, key: u64, result: TreeNodeExt) {
679 self.cache.insert(key, result);
680 }
681 #[allow(dead_code)]
683 pub fn len(&self) -> usize {
684 self.cache.len()
685 }
686 #[allow(dead_code)]
688 pub fn is_empty(&self) -> bool {
689 self.cache.is_empty()
690 }
691}
692#[allow(dead_code)]
694#[allow(missing_docs)]
695#[derive(Debug, Default)]
696pub struct TreeStats {
697 pub nodes: usize,
699 pub leaves: usize,
701 pub max_depth: usize,
703 pub total_size: usize,
705}
706impl TreeStats {
707 #[allow(dead_code)]
709 pub fn new() -> Self {
710 TreeStats::default()
711 }
712 #[allow(dead_code)]
714 pub fn from_tree(root: &TreeNodeExt) -> Self {
715 let mut stats = TreeStats::new();
716 collect_stats(root, 0, &mut stats);
717 stats
718 }
719}
720#[allow(dead_code)]
722#[allow(missing_docs)]
723pub struct TreeCursor {
724 pub current: TreeNodeExt,
726 pub ancestors: Vec<TreeNodeExt>,
728}
729impl TreeCursor {
730 #[allow(dead_code)]
732 pub fn new(root: TreeNodeExt) -> Self {
733 TreeCursor {
734 current: root,
735 ancestors: Vec::new(),
736 }
737 }
738 #[allow(dead_code)]
740 pub fn kind(&self) -> &SimpleNodeKindExt {
741 &self.current.kind
742 }
743 #[allow(dead_code)]
745 pub fn label(&self) -> &str {
746 &self.current.label
747 }
748 #[allow(dead_code)]
750 pub fn child_count(&self) -> usize {
751 self.current.children.len()
752 }
753 #[allow(dead_code)]
755 pub fn depth(&self) -> usize {
756 self.ancestors.len()
757 }
758}
759#[allow(dead_code)]
761#[allow(missing_docs)]
762pub struct TreeZipperExt {
763 pub focus: TreeNodeExt,
765 pub path: TreePathExt,
767 pub ancestors: Vec<(
769 SimpleNodeKindExt,
770 String,
771 Vec<TreeNodeExt>,
772 Vec<TreeNodeExt>,
773 )>,
774}
775impl TreeZipperExt {
776 #[allow(dead_code)]
778 pub fn new(root: TreeNodeExt) -> Self {
779 TreeZipperExt {
780 focus: root,
781 path: TreePathExt::root(),
782 ancestors: Vec::new(),
783 }
784 }
785 #[allow(dead_code)]
787 pub fn down(mut self, i: usize) -> Option<Self> {
788 if i >= self.focus.children.len() {
789 return None;
790 }
791 let mut children = self.focus.children;
792 let child = children.remove(i);
793 let left: Vec<TreeNodeExt> = children.drain(..i.min(children.len())).collect();
794 let right: Vec<TreeNodeExt> = children;
795 self.ancestors
796 .push((self.focus.kind, self.focus.label, left, right));
797 self.path = self.path.child(i);
798 Some(TreeZipperExt {
799 focus: child,
800 path: self.path,
801 ancestors: self.ancestors,
802 })
803 }
804 #[allow(dead_code)]
806 pub fn up(mut self) -> Option<(Self, TreeNodeExt)> {
807 let (kind, label, left, right) = self.ancestors.pop()?;
808 let child = self.focus;
809 let mut children = left;
810 children.push(child.clone());
811 children.extend(right);
812 let mut new_path = self.path;
813 new_path.0.pop();
814 Some((
815 TreeZipperExt {
816 focus: TreeNodeExt {
817 kind,
818 label,
819 children,
820 },
821 path: new_path,
822 ancestors: self.ancestors,
823 },
824 child,
825 ))
826 }
827}
828#[allow(dead_code)]
830#[allow(missing_docs)]
831pub struct RenameTransformExt {
832 pub from: String,
834 pub to: String,
836 pub count: usize,
838}
839impl RenameTransformExt {
840 #[allow(dead_code)]
842 pub fn new(from: &str, to: &str) -> Self {
843 RenameTransformExt {
844 from: from.to_string(),
845 to: to.to_string(),
846 count: 0,
847 }
848 }
849}
850#[allow(dead_code)]
852#[allow(missing_docs)]
853pub struct NodeCache {
854 pub values: std::collections::HashMap<String, u64>,
856}
857impl NodeCache {
858 #[allow(dead_code)]
860 pub fn new() -> Self {
861 NodeCache {
862 values: std::collections::HashMap::new(),
863 }
864 }
865 #[allow(dead_code)]
867 pub fn store(&mut self, key: &str, val: u64) {
868 self.values.insert(key.to_string(), val);
869 }
870 #[allow(dead_code)]
872 pub fn get(&self, key: &str) -> Option<u64> {
873 self.values.get(key).copied()
874 }
875}
876#[allow(dead_code)]
878#[allow(missing_docs)]
879pub struct TransformMemoExt2 {
880 pub cache: std::collections::HashMap<String, String>,
882}
883impl TransformMemoExt2 {
884 #[allow(dead_code)]
886 pub fn new() -> Self {
887 TransformMemoExt2 {
888 cache: std::collections::HashMap::new(),
889 }
890 }
891 #[allow(dead_code)]
893 pub fn has(&self, label: &str) -> bool {
894 self.cache.contains_key(label)
895 }
896 #[allow(dead_code)]
898 pub fn get(&self, label: &str) -> Option<&str> {
899 self.cache.get(label).map(|s| s.as_str())
900 }
901 #[allow(dead_code)]
903 pub fn insert(&mut self, label: &str, result: &str) {
904 self.cache.insert(label.to_string(), result.to_string());
905 }
906 #[allow(dead_code)]
908 pub fn len(&self) -> usize {
909 self.cache.len()
910 }
911 #[allow(dead_code)]
913 pub fn is_empty(&self) -> bool {
914 self.cache.is_empty()
915 }
916}
917#[derive(Clone, Debug, PartialEq, Eq)]
919pub enum SortKind {
920 Type,
922 Prop,
924 TypeU(String),
926 SortU(String),
928}