Skip to content

feat: allow a general evalTac at evalSepTactics #7702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Lean/Elab/Tactic/BuiltinTactic.lean
Original file line number Diff line number Diff line change
@@ -33,8 +33,11 @@ open Language in
/--
Evaluates a tactic script in form of a syntax node with alternating tactics and separators as
children.
-/
partial def evalSepTactics : Tactic := goEven

The user can provide their own tactic evaluation function `evalTac`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please expand this comment with an example of why one might want to do this? Otherwise future maintainers will be absolutely puzzled.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write something like this:

Assume a tactic developer writes a new general modified tactic execution,
for example calling automation each step, displaying the goal in a specific
way, modifying the behavior of some tactics...

def customEvalTactic : Tactic :=

Then it is possible to create a tactic scope in which this tactic is called
on each line of the scope, while preserving the incremental elaboration by

@[tactic my_scope, incremental]
def myScopeElab : Tactic := fun stx => do
  withNarrowedArgTacticReuse 1 (
    withNarrowedArgTacticReuse 0 (
      withNarrowedArgTacticReuse 0 (
        (evalSepTactics customEvalTactic)
      )
    )
  ) stx

-/
@[specialize]
partial def evalSepTactics (evalTac : Tactic := evalTactic) : Tactic := goEven
where
-- `stx[0]` is the next tactic step, if any
goEven stx := do
@@ -64,7 +67,7 @@ where
-- compare `stx[0]` for `finished`/`next` reuse, focus on remainder of script
Term.withNarrowedTacticReuse (stx := stx) (fun stx => (stx[0], mkNullNode stx.getArgs[1:])) fun stxs => do
let some snap := (← readThe Term.Context).tacSnap?
| do evalTactic tac; goOdd stxs
| do evalTac tac; goOdd stxs
let mut reusableResult? := none
let mut oldNext? := none
if let some old := snap.old? then
@@ -87,15 +90,15 @@ where
next := #[{ stx? := stxs, task := next.resultD default }]
}
-- Run `tac` in a fresh info tree state and store resulting state in snapshot for
-- incremental reporting, then add back saved trees. Here we rely on `evalTactic`
-- incremental reporting, then add back saved trees. Here we rely on `evalTac`
-- producing at most one info tree as otherwise `getInfoTreeWithContext?` would panic.
let trees ← getResetInfoTrees
try
let (_, state) ← withRestoreOrSaveFull reusableResult?
-- set up nested reuse; `evalTactic` will check for `isIncrementalElab`
-- set up nested reuse; `evalTac` will check for `isIncrementalElab`
(tacSnap? := some { old? := oldInner?, new := inner }) do
Term.withReuseContext tac do
evalTactic tac
evalTac tac
finished.resolve {
diagnostics := (← Language.Snapshot.Diagnostics.ofMessageLog
(← Core.getAndEmptyMessageLog))
@@ -120,7 +123,7 @@ where
Term.withNarrowedTacticReuse (fun stx => (stx[0], mkNullNode stx.getArgs[1:])) goEven stx

@[builtin_tactic seq1] def evalSeq1 : Tactic := fun stx =>
evalSepTactics stx[0]
(evalSepTactics) stx[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just pass the argument explicitly, in which case we may as well remove the default value


@[builtin_tactic paren, builtin_incremental] def evalParen : Tactic :=
Term.withNarrowedArgTacticReuse 1 evalTactic