Zulip Chat Archive

Stream: new members

Topic: Access parent goal


๐š ๐š˜๐š“๐šŒ๐š’๐šŽ๐šŒ๐š‘ ๐š—๐šŠ๐š ๐š›๐š˜๐šŒ๐š”๐š’ (Nov 12 2024 at 02:23):

Suppose my tactic myTac is running in the position below. Is there some way it can access the parent MVarId, i.e., the thing with expected type P?

example : P := by
  have : Q := by myTac
  ...

Kyle Miller (Nov 12 2024 at 04:09):

A trick I know is to start off with refine (?_ : ?goal), and then you can refer to ?goal inside the have

Daniel Weber (Nov 12 2024 at 04:52):

This seems to work, but it's hacky

import Lean

open Lean Elab Tactic

example (test : O โ†’ Q) : P := by
  have : Q := by
    apply test
    run_tac do
      withMainContext do
        let mg โ† getMainGoal
        for (a, a') in (โ† getMCtx).eAssignment do
          if a == mg then continue
          let some (_, t, v, b) := a'.letFun? | continue
          let v โ† instantiateMVars v
          if (v.findMVar? (ยท == mg)).isSome then
            logInfo m!"{a} ({a'})"
        return
    sorry
  sorry

๐š ๐š˜๐š“๐šŒ๐š’๐šŽ๐šŒ๐š‘ ๐š—๐šŠ๐š ๐š›๐š˜๐šŒ๐š”๐š’ (Nov 12 2024 at 05:25):

Kyle Miller said:

A trick I know is to start off with refine (?_ : ?goal), and then you can refer to ?goal inside the have

Indeed! I was wondering if it's possible to do programmatically without adjusting the proof script, though.

๐š ๐š˜๐š“๐šŒ๐š’๐šŽ๐šŒ๐š‘ ๐š—๐šŠ๐š ๐š›๐š˜๐šŒ๐š”๐š’ (Nov 12 2024 at 05:27):

Daniel Weber said:

This seems to work, but it's hacky

import Lean

open Lean Elab Tactic

example (test : O โ†’ Q) : P := by
  have : Q := by
    apply test
    run_tac do
      withMainContext do
        let mg โ† getMainGoal
        for (a, a') in (โ† getMCtx).eAssignment do
          if a == mg then continue
          let some (_, t, v, b) := a'.letFun? | continue
          let v โ† instantiateMVars v
          if (v.findMVar? (ยท == mg)).isSome then
            logInfo m!"{a} ({a'})"
        return
    sorry
  sorry

Thanks! This does work, but relies on the by myTac appearing inside have, and the have elaborating to a let_fun. I guess the answer to the question of 'is there a mapping from mvars to 'parent mvars' in the MVarCtx is 'no'.

Daniel Weber (Nov 12 2024 at 05:28):

You can find the mvar with the maximal index which is assigned and contains the current goal, but then the parent would be the goal before apply, not the goal before the have. I assumed you were asking about have specifically

Daniel Weber (Nov 12 2024 at 08:15):

@Michael Bucko may I ask why you're randomly reacting to messages?

Michael Bucko (Nov 12 2024 at 08:51):

Daniel Weber schrieb:

Michael Bucko may I ask why you're randomly reacting to messages?

I'm not a bot :) I react after I read and find something interesting. And I read and code a lot.


Last updated: May 02 2025 at 03:31 UTC