Zulip Chat Archive

Stream: lean4

Topic: Writing Tactics: run tactic on a hypothesis


Anurudh Peduri (Jul 30 2023 at 20:30):

Hey all, I need some help writing a tactic that loops through all the hypotheses and runs some tactic on each of them.

For example, I want to run cases p on every p: Point in my local context.

import Lean.Elab.Tactic

abbrev Point := Int × Int

elab "get_coordinates" : tactic =>
  Lean.Elab.Tactic.withMainContext do
    let ctx  Lean.MonadLCtx.getLCtx
    ctx.forM $ fun (decl: Lean.LocalDecl) => do
      let declExpr := decl.toExpr -- Find the expression of the declaration.
      let declType  Lean.Meta.inferType declExpr -- Find the type.
      if ( Lean.Meta.isExprDefEq declType (Lean.Expr.const ``Point [])) then
        dbg_trace f!"! Point"
        -- TODO run `cases decl`
    return

How do I do this? I was looking through the metaprogramming-book, but couldn't find how to run a tactic on a decl that I have.

Kyle Miller (Jul 30 2023 at 20:58):

There's docs#Lean.MVarId.casesRec for doing cases on everything in the local context that satisfies some condition

Kyle Miller (Jul 30 2023 at 21:00):

Lean.MVarId.substEqs is an example usage of it.

Kyle Miller (Jul 30 2023 at 21:08):

docs#Lean.Elab.Tactic.liftMetaTactic is useful in place of withMainContext to get the main goal and to be able to replace it with the results of running casesRec

Anurudh Peduri (Jul 30 2023 at 21:42):

Oh wow, thanks @Kyle Miller! let me try this out.

Anurudh Peduri (Jul 30 2023 at 22:23):

thank you, it worked. :)


Last updated: Dec 20 2023 at 11:08 UTC