rlean-search 0.2.2

Type-aware search over Lean 4 theorems, lemmas, and axioms
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
//! Recursive-descent parser for a useful fragment of Lean 4 types and search patterns.

use crate::ast::{Binder, BinderKind, TypeExpr};
use crate::lexer::{Lexer, Token};
use thiserror::Error;

#[derive(Debug, Error, Clone)]
pub enum ParseError {
    #[error("unexpected token {found} (expected {expected})")]
    Unexpected {
        expected: String,
        found: String,
    },
    #[error("unexpected end of input while parsing {context}")]
    Eof { context: String },
    #[error("parse error: {0}")]
    Message(String),
}

pub type Result<T> = std::result::Result<T, ParseError>;

/// A user search pattern, optionally restricted to the conclusion (`|-`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchPattern {
    /// When true, match only against `TypeExpr::conclusion()`.
    pub conclusion_only: bool,
    pub expr: TypeExpr,
}

pub fn parse_type(input: &str) -> Result<TypeExpr> {
    let mut p = Parser::new(input);
    let t = p.parse_type()?;
    // Allow trailing junk lightly — but prefer clean parse
    Ok(t)
}

pub fn parse_search_pattern(input: &str) -> Result<SearchPattern> {
    let trimmed = input.trim();
    let (conclusion_only, rest) = if let Some(r) = trimmed.strip_prefix("|-") {
        (true, r.trim())
    } else if let Some(r) = trimmed.strip_prefix("") {
        (true, r.trim())
    } else {
        (false, trimmed)
    };
    let expr = parse_type(rest)?;
    Ok(SearchPattern {
        conclusion_only,
        expr,
    })
}

struct Parser {
    tokens: Vec<Token>,
    pos: usize,
}

impl Parser {
    fn new(input: &str) -> Self {
        let tokens = Lexer::tokenize(input);
        Self { tokens, pos: 0 }
    }

    fn peek(&self) -> &Token {
        self.tokens.get(self.pos).unwrap_or(&Token::Eof)
    }

    fn bump(&mut self) -> Token {
        let t = self.peek().clone();
        if !matches!(t, Token::Eof) {
            self.pos += 1;
        }
        t
    }

    fn expect(&mut self, expected: &Token) -> Result<()> {
        let found = self.bump();
        if &found == expected {
            Ok(())
        } else {
            Err(ParseError::Unexpected {
                expected: expected.to_string(),
                found: found.to_string(),
            })
        }
    }

    fn parse_type(&mut self) -> Result<TypeExpr> {
        self.parse_arrow()
    }

    /// Right-associative arrows: `A → B → C`
    fn parse_arrow(&mut self) -> Result<TypeExpr> {
        // Leading binder Pi: `(x : A) → B` or `{x : A} → B`
        if matches!(
            self.peek(),
            Token::LParen | Token::LBrace | Token::LBracket | Token::LStrict
        ) {
            // Could be parenthesized type OR binder then arrow. Lookahead.
            if let Some(binder) = self.try_parse_leading_pi_binder()? {
                if matches!(self.peek(), Token::Arrow) {
                    self.bump();
                    let body = self.parse_arrow()?;
                    return Ok(TypeExpr::Pi {
                        binder,
                        body: Box::new(body),
                    });
                }
                // Not an arrow — treat the binder group as a parenthesized/typed term is wrong.
                // Fall through: re-parse as atomic via backtrack-ish by reconstructing.
                // We already consumed the binder; if no arrow, interpret as the type inside
                // only when single default binder with type used as parenthesized expression —
                // actually `(A)` is not a binder. try_parse only succeeds for binders with names.
                // If we parsed `(x : A)` without arrow, it's still a term-ish raw.
                return Ok(TypeExpr::Raw(format_binder_raw(&binder_fallback_name(
                    &binder,
                ))));
            }
        }

        let left = self.parse_bin_expr(0)?;
        if matches!(self.peek(), Token::Arrow) {
            self.bump();
            let right = self.parse_arrow()?;
            Ok(TypeExpr::Arrow(Box::new(left), Box::new(right)))
        } else {
            Ok(left)
        }
    }

    /// Operator precedence climbing for infix operators.
    fn parse_bin_expr(&mut self, min_prec: u8) -> Result<TypeExpr> {
        let mut left = self.parse_app()?;

        loop {
            let op = match self.peek() {
                Token::Op(s) if is_infix_op(s) => s.clone(),
                // `⁻¹` as postfix handled in parse_app
                _ => break,
            };
            let prec = op_prec(&op);
            if prec < min_prec {
                break;
            }
            self.bump();
            // right-assoc for `^` and `→` (arrow handled elsewhere), `∘` sometimes
            let next_min = if is_right_assoc(&op) { prec } else { prec + 1 };
            let right = self.parse_bin_expr(next_min)?;
            left = TypeExpr::BinOp {
                op,
                left: Box::new(left),
                right: Box::new(right),
            };
        }
        Ok(left)
    }

    fn parse_app(&mut self) -> Result<TypeExpr> {
        // Prefix unary
        if let Token::Op(s) = self.peek() {
            if s == "¬" || s == "-" {
                let op = s.clone();
                self.bump();
                let arg = self.parse_app()?;
                return Ok(TypeExpr::UnaryOp {
                    op,
                    arg: Box::new(arg),
                });
            }
        }

        let mut expr = self.parse_atomic()?;

        // Postfix: ⁻¹ and projections .field
        loop {
            if let Token::Op(s) = self.peek() {
                if s == "⁻¹" || s == "'" {
                    let op = s.clone();
                    self.bump();
                    expr = TypeExpr::Postfix {
                        arg: Box::new(expr),
                        op,
                    };
                    continue;
                }
            }
            if matches!(self.peek(), Token::Dot) {
                self.bump();
                match self.bump() {
                    Token::Ident(f) => {
                        expr = TypeExpr::Proj {
                            base: Box::new(expr),
                            field: f,
                        };
                    }
                    Token::Nat(n) => {
                        expr = TypeExpr::Proj {
                            base: Box::new(expr),
                            field: n,
                        };
                    }
                    Token::Op(s) if s == ".." => {
                        // `x..` range sugar → raw-ish
                        expr = TypeExpr::Postfix {
                            arg: Box::new(expr),
                            op: "..".into(),
                        };
                    }
                    other => {
                        return Err(ParseError::Unexpected {
                            expected: "field name".into(),
                            found: other.to_string(),
                        });
                    }
                }
                continue;
            }
            break;
        }

        // Juxtaposition application
        while is_atomic_start(self.peek()) {
            // Don't consume infix ops as apps
            if let Token::Op(s) = self.peek() {
                if is_infix_op(s) {
                    break;
                }
            }
            if matches!(self.peek(), Token::Arrow | Token::Comma | Token::Assign) {
                break;
            }
            let arg = self.parse_atomic()?;
            // Allow postfix on arg already inside parse_atomic path — apply
            let mut arg = arg;
            loop {
                if let Token::Op(s) = self.peek() {
                    if s == "⁻¹" || s == "'" {
                        let op = s.clone();
                        self.bump();
                        arg = TypeExpr::Postfix {
                            arg: Box::new(arg),
                            op,
                        };
                        continue;
                    }
                }
                if matches!(self.peek(), Token::Dot) {
                    self.bump();
                    match self.bump() {
                        Token::Ident(f) => {
                            arg = TypeExpr::Proj {
                                base: Box::new(arg),
                                field: f,
                            };
                        }
                        Token::Nat(n) => {
                            arg = TypeExpr::Proj {
                                base: Box::new(arg),
                                field: n,
                            };
                        }
                        _ => break,
                    }
                    continue;
                }
                break;
            }
            expr = TypeExpr::App(Box::new(expr), Box::new(arg));
        }

        Ok(expr)
    }

    fn parse_atomic(&mut self) -> Result<TypeExpr> {
        match self.peek().clone() {
            Token::Underscore => {
                self.bump();
                Ok(TypeExpr::Hole)
            }
            Token::NamedHole(n) => {
                self.bump();
                Ok(TypeExpr::NamedHole(n))
            }
            Token::Nat(n) => {
                self.bump();
                Ok(TypeExpr::NatLit(n))
            }
            Token::Literal(s) => {
                self.bump();
                Ok(TypeExpr::Literal(s))
            }
            Token::Ident(s) => {
                self.bump();
                // Sort / Type / Prop with optional level
                if s == "Prop" {
                    return Ok(TypeExpr::Sort {
                        name: "Prop".into(),
                        level: None,
                    });
                }
                if s == "Type" || s == "Sort" {
                    let level = if is_atomic_start(self.peek())
                        && !matches!(self.peek(), Token::Op(_))
                    {
                        // Type u / Type _ / Type*
                        if matches!(self.peek(), Token::Op(op) if op == "*") {
                            self.bump();
                            Some(Box::new(TypeExpr::Ident("*".into())))
                        } else if matches!(
                            self.peek(),
                            Token::Ident(_)
                                | Token::Underscore
                                | Token::NamedHole(_)
                                | Token::Nat(_)
                                | Token::LParen
                        ) {
                            // Only take a simple level atom, not a full app chain of term
                            match self.peek() {
                                Token::Ident(_)
                                | Token::Underscore
                                | Token::NamedHole(_)
                                | Token::Nat(_) => Some(Box::new(self.parse_level_atom()?)),
                                Token::LParen => Some(Box::new(self.parse_atomic()?)),
                                _ => None,
                            }
                        } else {
                            None
                        }
                    } else {
                        None
                    };
                    return Ok(TypeExpr::Sort { name: s, level });
                }
                Ok(TypeExpr::Ident(s))
            }
            Token::Forall => {
                self.bump();
                self.parse_quantifier(true)
            }
            Token::Exists => {
                self.bump();
                self.parse_quantifier(false)
            }
            Token::Fun => {
                self.bump();
                let binders = self.parse_binder_list_until_mapsto()?;
                if matches!(self.peek(), Token::MapsTo) {
                    self.bump();
                } else if matches!(self.peek(), Token::Arrow) {
                    // fun x → body sometimes
                    self.bump();
                } else {
                    // fun x => may use comma? rare
                }
                let body = self.parse_type()?;
                Ok(TypeExpr::Lambda {
                    binders,
                    body: Box::new(body),
                })
            }
            Token::LParen => {
                self.bump();
                // Empty `()`
                if matches!(self.peek(), Token::RParen) {
                    self.bump();
                    return Ok(TypeExpr::Raw("()".into()));
                }
                // Grouping / type ascription `(e)` or `(e : T)` or `(e : T) → ...` handled higher
                let inner = self.parse_type()?;
                // Type ascription: `(1 : M)`, `(Inv.inv : G → G)`
                let inner = if matches!(self.peek(), Token::Colon) {
                    self.bump();
                    let ty = self.parse_type()?;
                    TypeExpr::App(
                        Box::new(TypeExpr::App(
                            Box::new(TypeExpr::Ident("ascribe".into())),
                            Box::new(inner),
                        )),
                        Box::new(ty),
                    )
                } else {
                    inner
                };
                if matches!(self.peek(), Token::Comma) {
                    let mut parts = vec![inner];
                    while matches!(self.peek(), Token::Comma) {
                        self.bump();
                        parts.push(self.parse_type()?);
                    }
                    self.expect(&Token::RParen)?;
                    let s = parts
                        .iter()
                        .map(|p| p.surface())
                        .collect::<Vec<_>>()
                        .join(", ");
                    return Ok(TypeExpr::Raw(format!("({s})")));
                }
                self.expect(&Token::RParen)?;
                Ok(inner)
            }
            Token::LBrace => {
                // Implicit binder as term is rare; parse as binder group raw or type
                let binders = self.parse_brace_binder_group(BinderKind::Implicit)?;
                Ok(TypeExpr::Raw(format_binders_surface(&binders)))
            }
            Token::LBracket => {
                // Empty list `[]` or instance-like `[Group α]`
                self.bump();
                if matches!(self.peek(), Token::RBracket) {
                    self.bump();
                    return Ok(TypeExpr::Ident("[]".into()));
                }
                let inner = self.parse_type()?;
                // optional ascription inside brackets rare
                let inner = if matches!(self.peek(), Token::Colon) {
                    self.bump();
                    let ty = self.parse_type()?;
                    TypeExpr::App(
                        Box::new(TypeExpr::App(
                            Box::new(TypeExpr::Ident("ascribe".into())),
                            Box::new(inner),
                        )),
                        Box::new(ty),
                    )
                } else {
                    inner
                };
                self.expect(&Token::RBracket)?;
                Ok(TypeExpr::App(
                    Box::new(TypeExpr::Ident("inst".into())),
                    Box::new(inner),
                ))
            }
            Token::Pipe => {
                // Absolute value `|e|`
                self.bump();
                let inner = self.parse_bin_expr(0)?;
                if matches!(self.peek(), Token::Pipe) {
                    self.bump();
                    Ok(TypeExpr::App(
                        Box::new(TypeExpr::Ident("abs".into())),
                        Box::new(inner),
                    ))
                } else {
                    Ok(TypeExpr::UnaryOp {
                        op: "|".into(),
                        arg: Box::new(inner),
                    })
                }
            }
            Token::LStrict => {
                let binders = self.parse_brace_binder_group(BinderKind::StrictImplicit)?;
                Ok(TypeExpr::Raw(format_binders_surface(&binders)))
            }
            Token::Op(s) if s == "" || s == "" || s == "" => {
                // Big operators: keep as unary-ish application chain
                let op = s.clone();
                self.bump();
                // optional binder `(i ∈ s)` etc.
                let arg = if is_atomic_start(self.peek()) {
                    self.parse_app()?
                } else {
                    TypeExpr::Hole
                };
                Ok(TypeExpr::UnaryOp {
                    op,
                    arg: Box::new(arg),
                })
            }
            other => Err(ParseError::Unexpected {
                expected: "type expression".into(),
                found: other.to_string(),
            }),
        }
    }

    fn parse_level_atom(&mut self) -> Result<TypeExpr> {
        match self.bump() {
            Token::Ident(s) => Ok(TypeExpr::Ident(s)),
            Token::Nat(n) => Ok(TypeExpr::NatLit(n)),
            Token::Underscore => Ok(TypeExpr::Hole),
            Token::NamedHole(n) => Ok(TypeExpr::NamedHole(n)),
            other => Err(ParseError::Unexpected {
                expected: "universe level".into(),
                found: other.to_string(),
            }),
        }
    }

    fn parse_quantifier(&mut self, is_forall: bool) -> Result<TypeExpr> {
        let binders = self.parse_quantifier_binders()?;
        // optional comma
        if matches!(self.peek(), Token::Comma) {
            self.bump();
        }
        let body = self.parse_type()?;
        if is_forall {
            Ok(TypeExpr::Forall {
                binders,
                body: Box::new(body),
            })
        } else {
            Ok(TypeExpr::Exists {
                binders,
                body: Box::new(body),
            })
        }
    }

    fn parse_quantifier_binders(&mut self) -> Result<Vec<Binder>> {
        let mut binders = Vec::new();
        // ∀ x y : Nat, ...  or ∀ (x : Nat) (y : Nat), ... or ∀ x, ...
        loop {
            match self.peek() {
                Token::LParen | Token::LBrace | Token::LBracket | Token::LStrict => {
                    binders.push(self.parse_one_binder_group()?);
                }
                Token::Ident(_) | Token::Underscore => {
                    // bare names until `:` or `,`
                    let mut names = Vec::new();
                    while matches!(self.peek(), Token::Ident(_) | Token::Underscore) {
                        match self.bump() {
                            Token::Ident(n) => names.push(n),
                            Token::Underscore => names.push("_".into()),
                            _ => unreachable!(),
                        }
                        // stop if next would start body wrongly — if next is Op or known end
                        if matches!(
                            self.peek(),
                            Token::Colon
                                | Token::Comma
                                | Token::LParen
                                | Token::LBrace
                                | Token::LBracket
                                | Token::LStrict
                        ) {
                            break;
                        }
                        // also stop before another quantifier body keyword-less: if Op infix, it's body start without comma — rare
                        if matches!(self.peek(), Token::Op(_) | Token::Arrow) {
                            break;
                        }
                    }
                    let ty = if matches!(self.peek(), Token::Colon) {
                        self.bump();
                        Some(Box::new(self.parse_bin_expr(0)?))
                    } else {
                        None
                    };
                    binders.push(Binder {
                        kind: BinderKind::Default,
                        names,
                        ty,
                    });
                    // if next is another binder group continue; if comma break to body
                    if matches!(self.peek(), Token::Comma) {
                        break;
                    }
                    if !matches!(
                        self.peek(),
                        Token::LParen
                            | Token::LBrace
                            | Token::LBracket
                            | Token::LStrict
                            | Token::Ident(_)
                            | Token::Underscore
                    ) {
                        break;
                    }
                }
                Token::Comma => break,
                _ => break,
            }
            if matches!(self.peek(), Token::Comma) {
                break;
            }
        }
        if binders.is_empty() {
            return Err(ParseError::Message(
                "expected binders after quantifier".into(),
            ));
        }
        Ok(binders)
    }

    fn parse_binder_list_until_mapsto(&mut self) -> Result<Vec<Binder>> {
        let mut binders = Vec::new();
        while !matches!(
            self.peek(),
            Token::MapsTo | Token::Arrow | Token::Eof | Token::Comma
        ) {
            if matches!(
                self.peek(),
                Token::LParen | Token::LBrace | Token::LBracket | Token::LStrict
            ) {
                binders.push(self.parse_one_binder_group()?);
            } else if matches!(self.peek(), Token::Ident(_) | Token::Underscore) {
                let mut names = Vec::new();
                while matches!(self.peek(), Token::Ident(_) | Token::Underscore) {
                    match self.bump() {
                        Token::Ident(n) => names.push(n),
                        Token::Underscore => names.push("_".into()),
                        _ => unreachable!(),
                    }
                    if matches!(self.peek(), Token::Colon) {
                        break;
                    }
                    if matches!(self.peek(), Token::MapsTo | Token::Arrow) {
                        break;
                    }
                    if matches!(
                        self.peek(),
                        Token::LParen | Token::LBrace | Token::LBracket | Token::LStrict
                    ) {
                        break;
                    }
                }
                let ty = if matches!(self.peek(), Token::Colon) {
                    self.bump();
                    Some(Box::new(self.parse_bin_expr(0)?))
                } else {
                    None
                };
                binders.push(Binder {
                    kind: BinderKind::Default,
                    names,
                    ty,
                });
            } else {
                break;
            }
        }
        Ok(binders)
    }

    fn parse_one_binder_group(&mut self) -> Result<Binder> {
        match self.peek() {
            Token::LParen => self.parse_paren_binder(BinderKind::Default),
            Token::LBrace => self.parse_brace_binder_group(BinderKind::Implicit).map(|mut v| {
                v.pop().unwrap_or(Binder {
                    kind: BinderKind::Implicit,
                    names: vec!["_".into()],
                    ty: None,
                })
            }),
            Token::LBracket => self.parse_brace_binder_group(BinderKind::Instance).map(|mut v| {
                v.pop().unwrap_or(Binder {
                    kind: BinderKind::Instance,
                    names: vec!["_".into()],
                    ty: None,
                })
            }),
            Token::LStrict => self
                .parse_brace_binder_group(BinderKind::StrictImplicit)
                .map(|mut v| {
                    v.pop().unwrap_or(Binder {
                        kind: BinderKind::StrictImplicit,
                        names: vec!["_".into()],
                        ty: None,
                    })
                }),
            _ => Err(ParseError::Message("expected binder group".into())),
        }
    }

    fn parse_paren_binder(&mut self, kind: BinderKind) -> Result<Binder> {
        self.expect(&Token::LParen)?;
        // `(x y : T)` or `(x)` or `(_ : T)`
        let mut names = Vec::new();
        while matches!(self.peek(), Token::Ident(_) | Token::Underscore) {
            match self.bump() {
                Token::Ident(n) => names.push(n),
                Token::Underscore => names.push("_".into()),
                _ => unreachable!(),
            }
            if matches!(self.peek(), Token::Colon | Token::RParen) {
                break;
            }
        }
        let ty = if matches!(self.peek(), Token::Colon) {
            self.bump();
            Some(Box::new(self.parse_type()?))
        } else {
            None
        };
        self.expect(&Token::RParen)?;
        if names.is_empty() {
            // `(T)` was not a binder — but we already consumed. Represent type-only.
            if let Some(t) = ty {
                return Ok(Binder {
                    kind,
                    names: vec!["_".into()],
                    ty: Some(t),
                });
            }
        }
        Ok(Binder { kind, names, ty })
    }

    fn parse_brace_binder_group(&mut self, kind: BinderKind) -> Result<Vec<Binder>> {
        let (open, close) = match kind {
            BinderKind::Implicit => (Token::LBrace, Token::RBrace),
            BinderKind::Instance => (Token::LBracket, Token::RBracket),
            BinderKind::StrictImplicit => (Token::LStrict, Token::RStrict),
            BinderKind::Default => (Token::LParen, Token::RParen),
        };
        self.expect(&open)?;
        let mut binders = Vec::new();
        // instance `[Group G]` may have no names with colon: Ident+ as type
        // Try names : type, or just type
        let mut names = Vec::new();
        let start_pos = self.pos;
        while matches!(self.peek(), Token::Ident(_) | Token::Underscore) {
            match self.bump() {
                Token::Ident(n) => names.push(n),
                Token::Underscore => names.push("_".into()),
                _ => unreachable!(),
            }
            if matches!(self.peek(), Token::Colon) {
                break;
            }
            // could be `Group G` type application without names
            if matches!(
                self.peek(),
                Token::RBrace | Token::RBracket | Token::RStrict | Token::RParen
            ) {
                break;
            }
            // continue collecting — ambiguous
        }
        if matches!(self.peek(), Token::Colon) {
            self.bump();
            let ty = self.parse_type()?;
            binders.push(Binder {
                kind,
                names,
                ty: Some(Box::new(ty)),
            });
        } else {
            // Rewind-ish: treat everything as type expression
            // If we collected names without colon, it's `Group α` type
            self.pos = start_pos;
            let ty = self.parse_type()?;
            binders.push(Binder {
                kind,
                names: vec![],
                ty: Some(Box::new(ty)),
            });
        }
        // optional more binder groups inside same braces rare
        self.expect(&close)?;
        Ok(binders)
    }

    /// Try to parse `(x : T)` as a Pi binder; returns None if it looks like grouping.
    fn try_parse_leading_pi_binder(&mut self) -> Result<Option<Binder>> {
        let save = self.pos;
        let kind = match self.peek() {
            Token::LParen => BinderKind::Default,
            Token::LBrace => BinderKind::Implicit,
            Token::LBracket => BinderKind::Instance,
            Token::LStrict => BinderKind::StrictImplicit,
            _ => return Ok(None),
        };
        // Need: open, name+, colon, type, close, arrow
        let open_tok = self.bump();
        let mut names = Vec::new();
        while matches!(self.peek(), Token::Ident(_) | Token::Underscore) {
            match self.bump() {
                Token::Ident(n) => names.push(n),
                Token::Underscore => names.push("_".into()),
                _ => unreachable!(),
            }
            if matches!(self.peek(), Token::Colon) {
                break;
            }
            // if we see something else before colon and only one "name", might be `(Nat)` grouping
            if !matches!(self.peek(), Token::Ident(_) | Token::Underscore | Token::Colon)
            {
                break;
            }
        }
        if names.is_empty() || !matches!(self.peek(), Token::Colon) {
            self.pos = save;
            return Ok(None);
        }
        self.bump(); // colon
        let ty = match self.parse_type() {
            Ok(t) => t,
            Err(_) => {
                self.pos = save;
                return Ok(None);
            }
        };
        let close_ok = match kind {
            BinderKind::Default => matches!(self.peek(), Token::RParen),
            BinderKind::Implicit => matches!(self.peek(), Token::RBrace),
            BinderKind::Instance => matches!(self.peek(), Token::RBracket),
            BinderKind::StrictImplicit => matches!(self.peek(), Token::RStrict),
        };
        if !close_ok {
            self.pos = save;
            return Ok(None);
        }
        self.bump();
        // Only treat as binder if arrow follows OR we already know it's binder form
        if matches!(self.peek(), Token::Arrow) {
            Ok(Some(Binder {
                kind,
                names,
                ty: Some(Box::new(ty)),
            }))
        } else {
            // `(x : T)` alone isn't a type; restore
            let _ = open_tok;
            self.pos = save;
            Ok(None)
        }
    }
}

fn is_atomic_start(tok: &Token) -> bool {
    match tok {
        Token::Ident(_)
        | Token::Nat(_)
        | Token::Literal(_)
        | Token::Underscore
        | Token::NamedHole(_)
        | Token::LParen
        | Token::LBrace
        | Token::LBracket
        | Token::LStrict
        | Token::Forall
        | Token::Exists
        | Token::Fun
        | Token::Pipe => true,
        Token::Op(s) => s == "" || s == "" || s == "" || s == "¬" || s == "-",
        _ => false,
    }
}

fn is_infix_op(s: &str) -> bool {
    matches!(
        s,
        "=" | ""
            | "<"
            | ">"
            | ""
            | ""
            | "+"
            | "-"
            | "*"
            | "/"
            | "%"
            | "^"
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | "++"
            | "::"
            | "|>"
            | "<|"
            | "|>."
            | "$"
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
            | ""
    ) || s == ""
}

fn op_prec(op: &str) -> u8 {
    match op {
        "$" | "<|" | "|>" | "|>." => 1,
        "" => 2,
        "" => 3,
        "" => 4,
        "=" | "" | "<" | ">" | "" | "" | "" | "" | "" | "" | "" | "" | "" | ""
        | "" | "" => 5,
        "" | "++" => 6,
        "" => 7,
        "::" => 8,
        "+" | "-" => 9,
        "*" | "/" | "%" | "" | "" | "" => 10,
        "" => 11,
        "^" => 12,
        "" => 13,
        _ => 5,
    }
}

fn is_right_assoc(op: &str) -> bool {
    matches!(op, "^" | "" | "::" | "$" | "")
}

fn format_binders_surface(binders: &[Binder]) -> String {
    binders
        .iter()
        .map(|b| {
            let names = b.names.join(" ");
            let core = match &b.ty {
                Some(t) if names.is_empty() => t.surface(),
                Some(t) => format!("{names} : {}", t.surface()),
                None => names,
            };
            match b.kind {
                BinderKind::Default => format!("({core})"),
                BinderKind::Implicit => format!("{{{core}}}"),
                BinderKind::Instance => format!("[{core}]"),
                BinderKind::StrictImplicit => format!("{core}"),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

fn format_binder_raw(s: &str) -> String {
    s.to_string()
}

fn binder_fallback_name(b: &Binder) -> String {
    format_binders_surface(std::slice::from_ref(b))
}

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

    #[test]
    fn parse_eq_add() {
        let t = parse_type("n + m = m + n").unwrap();
        assert!(matches!(&t, TypeExpr::BinOp { op, .. } if op == "="));
        assert_eq!(t.conclusion().head_key(), "op:=");
    }

    #[test]
    fn parse_forall() {
        let t = parse_type("∀ (n m : Nat), n + m = m + n").unwrap();
        match t {
            TypeExpr::Forall { binders, body } => {
                assert_eq!(binders[0].names, vec!["n", "m"]);
                assert!(matches!(body.as_ref(), TypeExpr::BinOp { op, .. } if op == "="));
            }
            _ => panic!("expected forall"),
        }
    }

    #[test]
    fn parse_arrow_chain() {
        let t = parse_type("Nat → Nat → Prop").unwrap();
        match t {
            TypeExpr::Arrow(a, b) => {
                assert!(matches!(a.as_ref(), TypeExpr::Ident(s) if s == "Nat"));
                assert!(matches!(b.as_ref(), TypeExpr::Arrow(_, _)));
            }
            _ => panic!("expected arrow"),
        }
    }

    #[test]
    fn parse_pi_binder() {
        let t = parse_type("(n : Nat) → n + 0 = n").unwrap();
        assert!(matches!(t, TypeExpr::Pi { .. }));
    }

    #[test]
    fn parse_holes_and_named() {
        let t = parse_type("?a - ?a = 0").unwrap();
        match t {
            TypeExpr::BinOp { op, left, right } if op == "=" => {
                assert!(matches!(right.as_ref(), TypeExpr::NatLit(n) if n == "0"));
                match left.as_ref() {
                    TypeExpr::BinOp { op, left, right } if op == "-" => {
                        assert!(matches!(left.as_ref(), TypeExpr::NamedHole(a) if a == "a"));
                        assert!(matches!(right.as_ref(), TypeExpr::NamedHole(a) if a == "a"));
                    }
                    _ => panic!("expected subtraction"),
                }
            }
            _ => panic!("expected eq"),
        }
    }

    #[test]
    fn parse_search_turnstile() {
        let p = parse_search_pattern("|- tsum _ = _ * tsum _").unwrap();
        assert!(p.conclusion_only);
        assert!(matches!(p.expr, TypeExpr::BinOp { op, .. } if op == "="));
    }

    #[test]
    fn parse_iff_and() {
        let t = parse_type("p ∧ q ↔ q ∧ p").unwrap();
        assert!(matches!(t, TypeExpr::BinOp { op, .. } if op == ""));
    }
}