| /- | |
| Copyright (c) 2020 Microsoft Corporation. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Leonardo de Moura | |
| -/ | |
| module | |
| prelude | |
| public import Lean.Parser.Term | |
| public section | |
| namespace Lean | |
| namespace Parser | |
| builtin_initialize registerBuiltinParserAttribute `builtin_doElem_parser ``Category.doElem | |
| builtin_initialize registerBuiltinDynamicParserAttribute `doElem_parser `doElem | |
| @[inline] def doElemParser (rbp : Nat := 0) : Parser := | |
| categoryParser `doElem rbp | |
| namespace Term | |
| def leftArrow : Parser := unicodeSymbol "← " "<- " | |
| @[builtin_term_parser] def nestedAction := leading_parser:minPrec | |
| leftArrow >> doElemParser | |
| def doSeqItem := leading_parser | |
| ppLine >> doElemParser >> optional "; " | |
| def doSeqIndent := leading_parser | |
| many1Indent doSeqItem | |
| def doSeqBracketed := leading_parser | |
| "{" >> withoutPosition (many1 doSeqItem) >> ppLine >> "}" | |
| /-- A `doSeq` is a sequence of `doElem`, the main argument after the `do` keyword and other | |
| do elements that take blocks. It can either have the form `"{" (doElem ";"?)* "}"` or | |
| `many1Indent (doElem ";"?)`, where `many1Indent` ensures that all the items are at | |
| the same or higher indentation level as the first line. -/ | |
| @[builtin_doc] def doSeq := | |
| withAntiquot (mkAntiquot "doSeq" decl_name% (isPseudoKind := true)) <| | |
| doSeqBracketed <|> doSeqIndent | |
| /-- `termBeforeDo` is defined as `withForbidden("do", term)`, which will parse a term but | |
| disallows `do` outside of a bracketing construct. This is used for parsers like `for _ in t do ...` | |
| or `unless t do ...`, where we do not want `t do ...` to be parsed as an application of `t` to a | |
| `do` block, which would otherwise be allowed. -/ | |
| def termBeforeDo := withForbidden "do" termParser | |
| attribute [run_builtin_parser_attribute_hooks] doSeq termBeforeDo | |
| builtin_initialize | |
| register_parser_alias doSeq | |
| register_parser_alias termBeforeDo | |
| def getDoElems (doSeq : DoSeq) : Array DoElem := | |
| if doSeq.raw.getKind == ``Parser.Term.doSeqBracketed then | |
| doSeq.raw[1].getArgs.map fun arg => ⟨arg[0]⟩ | |
| else if doSeq.raw.getKind == ``Parser.Term.doSeqIndent then | |
| doSeq.raw[0].getArgs.map fun arg => ⟨arg[0]⟩ | |
| else | |
| #[] | |
| def notFollowedByRedefinedTermToken := | |
| -- Remark: we don't currently support `open` and `set_option` in `do`-blocks, | |
| -- but we include them in the following list to fix the ambiguity where | |
| -- an "open" command follows the `do`-block. | |
| -- If we don't add `do`, then users would have to indent `do` blocks or use `{ ... }`. | |
| notFollowedBy ("set_option" <|> "open" <|> "if" <|> "match" <|> "match_expr" <|> "let" <|> "let_expr" <|> "have" <|> | |
| "do" <|> "dbg_trace" <|> "idbg" <|> "assert!" <|> "debug_assert!" <|> "for" <|> "unless" <|> "return" <|> symbol "try") | |
| "token at 'do' element" | |
| namespace InternalSyntax | |
| /-- | |
| Internal syntax used in the `if` and `unless` elaborators. Behaves like `pure PUnit.unit` but | |
| uses `()` if possible and gives better error messages. | |
| -/ | |
| scoped syntax (name := doSkip) "skip" : doElem | |
| end InternalSyntax | |
| @[builtin_doElem_parser] def doLet := leading_parser | |
| "let " >> optional "mut " >> letConfig >> letDecl | |
| @[builtin_doElem_parser] def doLetElse := leading_parser withPosition <| | |
| "let " >> optional "mut " >> letConfig >> termParser >> " := " >> termParser >> | |
| (checkColGe >> " | " >> doSeqIndent) >> optional (checkColGe >> doSeqIndent) | |
| @[builtin_doElem_parser] def doLetExpr := leading_parser withPosition <| | |
| "let_expr " >> matchExprPat >> " := " >> termParser >> | |
| (checkColGe >> " | " >> doSeqIndent) >> optional (checkColGe >> doSeqIndent) | |
| @[builtin_doElem_parser] def doLetMetaExpr := leading_parser withPosition <| | |
| "let_expr " >> matchExprPat >> leftArrow >> termParser >> | |
| (checkColGe >> " | " >> doSeqIndent) >> optional (checkColGe >> doSeqIndent) | |
| @[builtin_doElem_parser] def doLetRec := leading_parser | |
| group ("let " >> nonReservedSymbol "rec ") >> letRecDecls | |
| def doIdDecl := leading_parser | |
| atomic (ident >> optType >> ppSpace >> leftArrow) >> | |
| doElemParser | |
| def doPatDecl := leading_parser | |
| atomic (termParser >> optType >> ppSpace >> leftArrow) >> | |
| doElemParser >> optional ((checkColGe >> " | " >> doSeqIndent) >> optional (checkColGe >> doSeqIndent)) | |
| @[builtin_doElem_parser] def doLetArrow := leading_parser withPosition <| | |
| "let " >> optional "mut " >> letConfig >> (doIdDecl <|> doPatDecl) | |
| /- | |
| We use `letIdDeclNoBinders` to define `doReassign`. | |
| Motivations: | |
| - we do not reassign functions, | |
| - we do not want `hygieneInfo` case, and | |
| - avoid parser conflict | |
| -/ | |
| def letIdDeclNoBinders := leading_parser | |
| atomic (node ``letId ident >> pushNone >> optType >> " := ") >> termParser | |
| /-- `erased x := e` declares a verification-only variable; `mut` allows reassignment. -/ | |
| @[builtin_doElem_parser default+10] def doErased := leading_parser | |
| nonReservedSymbol "erased " (includeIdent := true) >> optional "mut " >> letIdDeclNoBinders | |
| /-- `erased x ← act` runs `act` and hides its result in a verification-only variable. -/ | |
| @[builtin_doElem_parser default+10] def doErasedArrow := leading_parser | |
| nonReservedSymbol "erased " (includeIdent := true) >> optional "mut " >> doIdDecl | |
| @[builtin_doElem_parser] def doReassign := leading_parser | |
| notFollowedByRedefinedTermToken >> (letIdDeclNoBinders <|> letPatDecl) | |
| -- `doReassignElse` clashes with the `doMatch` parser in | |
| -- `do match e with | x := x | none => pure ()` | |
| -- So we do not define it for back compat reasons. | |
| -- @[builtin_doElem_parser] def doReassignElse := leading_parser | |
| -- notFollowedByRedefinedTermToken >> | |
| -- (termParser >> " := " >> termParser >> (checkColGt >> " | " >> doSeqIndent) >> optional doSeqIndent) | |
| @[builtin_doElem_parser] def doReassignArrow := leading_parser | |
| notFollowedByRedefinedTermToken >> (doIdDecl <|> doPatDecl) | |
| @[builtin_doElem_parser] def doHave := leading_parser | |
| "have" >> Term.letConfig >> Term.letDecl | |
| /- | |
| In `do` blocks, we support `if` without an `else`. | |
| Thus, we use indentation to prevent examples such as | |
| ``` | |
| if c_1 then | |
| if c_2 then | |
| action_1 | |
| else | |
| action_2 | |
| ``` | |
| from being parsed as | |
| ``` | |
| if c_1 then { | |
| if c_2 then { | |
| action_1 | |
| } else { | |
| action_2 | |
| } | |
| } | |
| ``` | |
| We also have special support for `else if` because we don't want to write | |
| ``` | |
| if c_1 then | |
| action_1 | |
| else if c_2 then | |
| action_2 | |
| else | |
| action_3 | |
| ``` | |
| -/ | |
| def elseIf := atomic (group (withPosition ("else " >> checkLineEq >> " if "))) | |
| -- ensure `if $e then ...` still binds to `e:term` | |
| def doIfLetPure := leading_parser " := " >> termParser | |
| def doIfLetBind := leading_parser leftArrow >> termParser | |
| def doIfLet := leading_parser (withAnonymousAntiquot := false) | |
| "let " >> termParser >> (doIfLetPure <|> doIfLetBind) | |
| def doIfProp := leading_parser (withAnonymousAntiquot := false) | |
| optional (atomic (binderIdent >> " : ")) >> termParser | |
| def doIfCond := | |
| withAntiquot (mkAntiquot "doIfCond" decl_name% (anonymous := false) (isPseudoKind := true)) <| | |
| doIfLet <|> doIfProp | |
| -- must reset cache as `withPositionAfterLinebreak` will look at syntax not produced by this parser (or even | |
| -- current category call) | |
| @[builtin_doElem_parser] def doIf := leading_parser withResetCache <| withPositionAfterLinebreak <| ppRealGroup <| | |
| ppRealFill (ppIndent ("if " >> doIfCond >> " then") >> ppSpace >> doSeq) >> | |
| many (checkColGe "'else if' in 'do' must be indented" >> | |
| group (ppDedent ppSpace >> ppRealFill (elseIf >> doIfCond >> " then " >> doSeq))) >> | |
| optional (checkColGe "'else' in 'do' must be indented" >> | |
| ppDedent ppSpace >> ppRealFill ("else " >> doSeq)) | |
| @[builtin_doElem_parser] def doUnless := leading_parser | |
| "unless " >> withForbidden "do" termParser >> " do " >> doSeq | |
| def doForDecl := leading_parser | |
| optional (atomic (ident >> " : ")) >> termParser >> " in " >> | |
| withForbiddens #["do", "invariant", "decreasing"] termParser | |
| /-- | |
| The optional `invariant` clause of a loop, written like a `fun`: the invariant annotates the loop so | |
| `vcgen` reads it from the program, and mutable variables are referenced by name. | |
| On a `for` loop the clause is `invariant pref suff a b c => e`, with `pref` bound to the elements | |
| consumed so far and `suff` to the elements remaining. On a `repeat` or `while` loop it is | |
| `invariant exit a b c => e`, with `exit` bound to whether the loop has left. Any further binders, | |
| here `a b c`, bind the arguments of the assertion itself, such as the state of a state monad. | |
| -/ | |
| def doLoopInvariant := leading_parser | |
| ppIndent (ppLine >> nonReservedSymbol "invariant" >> | |
| withForbiddens #["do", "decreasing"] basicFun) | |
| /-- | |
| A `decreasing` clause gives a `repeat` or `while` loop its termination measure, which every | |
| iteration must lower. The measure is a term over the loop's mutable variables; the form | |
| `decreasing a b c => e` binds the arguments of the measure itself, such as the state of a state | |
| monad. | |
| -/ | |
| def doLoopDecreasing := leading_parser | |
| ppIndent (ppLine >> nonReservedSymbol "decreasing" >> | |
| withForbidden "do" (atomic basicFun <|> (ppSpace >> termParser))) | |
| /-- | |
| `for x in e do s` iterates over `e` assuming `e`'s type has an instance of the `ForIn` typeclass. | |
| `break` and `continue` are supported inside `for` loops. | |
| `for x in e, x2 in e2, ... do s` iterates over the given collections in parallel, | |
| until at least one of them is exhausted. | |
| The types of `e2` etc. must implement the `Std.ToStream` typeclass. | |
| -/ | |
| @[builtin_doElem_parser] def doFor := leading_parser | |
| "for " >> sepBy1 doForDecl ", " >> optional doLoopInvariant >> optional doLoopDecreasing >> | |
| " do " >> doSeq | |
| def dependentParam := leading_parser | |
| atomic ("(" >> nonReservedSymbol "dependent") >> " := " >> | |
| (trueVal <|> falseVal) >> ")" >> ppSpace | |
| def doMatchAlts := ppDedent <| matchAlts (rhsParser := doSeq) | |
| @[builtin_doElem_parser] def doMatch := leading_parser:leadPrec | |
| "match " >> optional dependentParam >> optional Term.generalizingParam >> optional Term.motive >> | |
| sepBy1 matchDiscr ", " >> " with" >> doMatchAlts | |
| def doMatchExprAlts := ppDedent <| matchExprAlts (rhsParser := doSeq) | |
| def optMetaFalse := | |
| optional (atomic ("(" >> nonReservedSymbol "meta" >> " := " >> nonReservedSymbol "false" >> ") ")) | |
| @[builtin_doElem_parser] def doMatchExpr := leading_parser:leadPrec | |
| "match_expr " >> optMetaFalse >> termParser >> " with" >> doMatchExprAlts | |
| def doCatch := leading_parser | |
| ppDedent ppLine >> atomic ("catch " >> binderIdent) >> optional (" : " >> termParser) >> darrow >> doSeq | |
| def doCatchMatch := leading_parser | |
| ppDedent ppLine >> "catch " >> doMatchAlts | |
| def doFinally := leading_parser | |
| ppDedent ppLine >> "finally " >> doSeq | |
| @[builtin_doElem_parser] def doTry := leading_parser | |
| "try " >> doSeq >> many (doCatch <|> doCatchMatch) >> optional doFinally | |
| /-- | |
| `do← s` (or ASCII `do<- s`) hands `s` to the enclosing wrapper as a body | |
| whose control-flow effects (`return`, `break`, `continue`, `mut`-variable | |
| reassignment) target the surrounding `do` block, not the body's local monad. | |
| The syntax is reminiscent of a nested action `(← s)`, but unlike a nested | |
| action, `s` is not run eagerly in the `do` block context before the wrapping | |
| function is called. The wrapping function decides when to run `s`, and code | |
| is inserted to forward `s`'s effects to the outer `do` block. | |
| Only valid as the last argument of a function application that itself appears | |
| as a statement of an outer `do` block, optionally wrapped in `fun` binders. | |
| Examples: | |
| * `f a₁ … aₙ (do← s)` | |
| * `f a₁ … aₙ (fun b₁ … bₖ => do← s)` | |
| -/ | |
| @[builtin_term_parser default+1] def doForward := leading_parser | |
| atomic ("do" >> checkNoWsBefore >> leftArrow) >> doSeq | |
| /-- `break` exits the surrounding `for` loop. -/ | |
| @[builtin_doElem_parser] def doBreak := leading_parser "break" | |
| /-- `continue` skips to the next iteration of the surrounding `for` loop. -/ | |
| @[builtin_doElem_parser] def doContinue := leading_parser "continue" | |
| /-- | |
| `return e` inside of a `do` block makes the surrounding block evaluate to `pure e`, | |
| skipping any further statements. | |
| Note that uses of the `do` keyword in other syntax like in `for _ in _ do` | |
| do not constitute a surrounding block in this sense; | |
| in supported editors, the corresponding `do` keyword of the surrounding block | |
| is highlighted when hovering over `return`. | |
| The term `e` can start on a later line if it starts in a column to the right of `return`. | |
| `return` not followed by such a term is equivalent to `return ()`. | |
| -/ | |
| @[builtin_doElem_parser] def doReturn := leading_parser:leadPrec | |
| withPosition ("return" >> optional (ppSpace >> checkColGt >> termParser)) | |
| /-- | |
| `dbg_trace e` prints `e` (which can be an interpolated string literal) to stderr. | |
| It should only be used for debugging. | |
| -/ | |
| @[builtin_doElem_parser] def doDbgTrace := leading_parser:leadPrec | |
| "dbg_trace " >> ((interpolatedStr termParser) <|> termParser) | |
| /-- | |
| *experimental* | |
| `idbg e` enables live inspection of program state from the editor. When placed in a `do` block, | |
| it captures all local variables in scope and the expression `e`, then: | |
| - **In the language server**: starts a TCP server on localhost waiting for the running program to | |
| connect; the editor will mark this part of the program as "in progress" during this wait but that | |
| will not block `lake build` of the project. | |
| - **In the compiled program**: on first execution of the `idbg` call site, connects to the server, | |
| receives the expression, compiles and evaluates it using the program's actual runtime values, and | |
| sends the `repr` result back. | |
| The result is displayed as an info diagnostic on the `idbg` keyword. The expression `e` can be | |
| edited while the program is running - each edit triggers re-elaboration of `e`, a new TCP exchange, | |
| and an updated result. This makes `idbg` a live REPL for inspecting and experimenting with | |
| program state at a specific point in execution. Only when `idbg` is inserted, moved, or removed does | |
| the program need to be recompiled and restarted. | |
| # Known Limitations | |
| * The program will poll the server for up to 10 minutes and needs to be killed manually | |
| otherwise. | |
| * Use of multiple `idbg` at once untested, likely too much overhead from overlapping imports without | |
| further changes. | |
| * `LEAN_PATH` must be properly set up so the compiled program can import its origin module. | |
| * Untested on Windows and macOS. | |
| -/ | |
| @[builtin_doElem_parser] def doIdbg := leading_parser:leadPrec | |
| withPosition ("idbg " >> termParser) | |
| /-- | |
| `assert! cond` panics if `cond` evaluates to `false`. | |
| -/ | |
| @[builtin_doElem_parser] def doAssert := leading_parser:leadPrec | |
| "assert! " >> termParser | |
| /-- | |
| `debug_assert! cond` panics if `cond` evaluates to `false` and the executing code has been built | |
| with debug assertions enabled (see the `debugAssertions` option). | |
| -/ | |
| @[builtin_doElem_parser] def doDebugAssert := leading_parser:leadPrec | |
| "debug_assert! " >> termParser | |
| /-- | |
| `assert P` states that `P` holds at this point in the program. The form `assert s => P s` binds the | |
| arguments of the assertion itself, such as the state of a state monad. `vcgen` reads the assertion | |
| from the program and proves it; at runtime the element does nothing. | |
| -/ | |
| @[builtin_doElem_parser default+10] def doAssertion := leading_parser:leadPrec | |
| nonReservedSymbol "assert" (includeIdent := true) >> | |
| (atomic basicFun <|> (ppSpace >> termParser)) | |
| @[builtin_doElem_parser] def doRepeat := leading_parser | |
| "repeat " >> optional doLoopInvariant >> optional doLoopDecreasing >> doSeq | |
| @[builtin_doElem_parser] def doWhile := leading_parser | |
| "while " >> withForbiddens #["do", "invariant", "decreasing"] doIfCond >> | |
| optional doLoopInvariant >> optional doLoopDecreasing >> " do " >> doSeq | |
| @[builtin_doElem_parser] def doRepeatUntil := leading_parser | |
| "repeat " >> optional doLoopInvariant >> optional doLoopDecreasing >> doSeq >> | |
| ppDedent ppLine >> "until " >> termParser | |
| /- | |
| We use `notFollowedBy` to avoid counterintuitive behavior. | |
| For example, the `if`-term parser doesn't enforce indentation restrictions, | |
| but we don't want it to be used when `doIf` fails. | |
| Note that parser priorities would not solve this problem | |
| since the `doIf` parser is failing while the `if` parser is succeeding. | |
| The first `notFollowedBy` prevents this problem. | |
| Consider the `doElem` `x := (a, b⟩`. | |
| It contains an error since we are using `⟩` instead of `)`. | |
| Thus, `doReassign` parser fails. | |
| However, `doExpr` would succeed consuming just `x`, | |
| and cryptic error message is generated after that. | |
| The second `notFollowedBy` prevents this problem. | |
| -/ | |
| @[builtin_doElem_parser] def doExpr := leading_parser | |
| notFollowedByRedefinedTermToken >> termParser >> | |
| notFollowedBy (symbol ":=" <|> leftArrow) | |
| "unexpected token after 'expr' in 'do' block" | |
| @[builtin_doElem_parser] def doNested := leading_parser | |
| "do " >> doSeq | |
| @[builtin_term_parser] def «do» := leading_parser:argPrec | |
| ppAllowUngrouped >> "do " >> doSeq | |
| /- | |
| macros for using `unless`, `for`, `try`, `return` as terms. | |
| They expand into `do unless ...`, `do for ...`, `do try ...`, and `do return ...` | |
| -/ | |
| /-- `unless e do s` is a nicer way to write `if !e do s`. -/ | |
| @[builtin_term_parser] def termUnless := leading_parser | |
| "unless " >> withForbidden "do" termParser >> " do " >> doSeq | |
| @[builtin_term_parser] def termFor := leading_parser | |
| "for " >> sepBy1 doForDecl ", " >> optional doLoopInvariant >> optional doLoopDecreasing >> | |
| " do " >> doSeq | |
| @[builtin_term_parser] def termTry := leading_parser | |
| "try " >> doSeq >> many (doCatch <|> doCatchMatch) >> optional doFinally | |
| /-- | |
| `return` used outside of `do` blocks creates an implicit block around it | |
| and thus is equivalent to `pure e`, but helps with avoiding parentheses. | |
| -/ | |
| @[builtin_term_parser] def termReturn := leading_parser:leadPrec | |
| withPosition ("return" >> optional (ppSpace >> checkColGt >> termParser)) | |
| end Term | |
| end Parser | |
| end Lean |