Skip to content

Commit

Permalink
feat: nary nomatch (#3285)
Browse files Browse the repository at this point in the history
Base for #3279

---------

Co-authored-by: Scott Morrison <[email protected]>
  • Loading branch information
leodemoura and kim-em authored Feb 9, 2024
1 parent 09a4399 commit 1f54722
Show file tree
Hide file tree
Showing 25 changed files with 12,163 additions and 5,791 deletions.
25 changes: 19 additions & 6 deletions src/Lean/Elab/Match.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1236,16 +1236,29 @@ where
builtin_initialize
registerTraceClass `Elab.match

-- leading_parser:leadPrec "nomatch " >> termParser
-- leading_parser:leadPrec "nomatch " >> sepBy1 termParser ", "
@[builtin_term_elab «nomatch»] def elabNoMatch : TermElab := fun stx expectedType? => do
match stx with
| `(nomatch $discrExpr) =>
if (← isAtomicDiscr discrExpr) then
| `(nomatch $discrs,*) =>
let discrs := discrs.getElems
if (← discrs.allM fun discr => isAtomicDiscr discr.raw) then
let expectedType ← waitExpectedType expectedType?
let discr := mkNode ``Lean.Parser.Term.matchDiscr #[mkNullNode, discrExpr]
elabMatchAux none #[discr] #[] mkNullNode expectedType
let discrs := discrs.map fun discr => mkNode ``Lean.Parser.Term.matchDiscr #[mkNullNode, discr.raw]
elabMatchAux none discrs #[] mkNullNode expectedType
else
let stxNew ← `(let_mvar% ?x := $discrExpr; nomatch ?x)
let rec loop (discrs : List Term) (discrsNew : Array Syntax) : TermElabM Term := do
match discrs with
| [] =>
return ⟨stx.setArg 1 (Syntax.mkSep discrsNew (mkAtomFrom stx ", "))⟩
| discr :: discrs =>
if (← isAtomicDiscr discr) then
loop discrs (discrsNew.push discr)
else
withFreshMacroScope do
let discrNew ← `(?x)
let r ← loop discrs (discrsNew.push discrNew)
`(let_mvar% ?x := $discr; $r)
let stxNew ← loop discrs.toList #[]
withMacroExpansion stx stxNew <| elabTerm stxNew expectedType?
| _ => throwUnsupportedSyntax

Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Parser/Term.lean
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ Empty match/ex falso. `nomatch e` is of arbitrary type `α : Sort u` if
Lean can show that an empty set of patterns is exhaustive given `e`'s type,
e.g. because it has no constructors.
-/
@[builtin_term_parser] def «nomatch» := leading_parser:leadPrec "nomatch " >> termParser
@[builtin_term_parser] def «nomatch» := leading_parser:leadPrec "nomatch " >> sepBy1 termParser ", "

def funImplicitBinder := withAntiquot (mkAntiquot "implicitBinder" ``implicitBinder) <|
atomic (lookahead ("{" >> many1 binderIdent >> (symbol " : " <|> "}"))) >> implicitBinder
Expand Down
6 changes: 5 additions & 1 deletion stage0/stdlib/Init.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

525 changes: 522 additions & 3 deletions stage0/stdlib/Init/Data/Array/Basic.c

Large diffs are not rendered by default.

458 changes: 431 additions & 27 deletions stage0/stdlib/Init/Notation.c

Large diffs are not rendered by default.

664 changes: 664 additions & 0 deletions stage0/stdlib/Init/Tactics.c

Large diffs are not rendered by default.

2,548 changes: 2,548 additions & 0 deletions stage0/stdlib/Init/TacticsExtra.c

Large diffs are not rendered by default.

1,015 changes: 435 additions & 580 deletions stage0/stdlib/Lean/Attributes.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stage0/stdlib/Lean/Compiler/IR/Basic.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion stage0/stdlib/Lean/Data/Json.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1f54722

Please sign in to comment.