Zulip Chat Archive

Stream: metaprogramming / tactics

Topic: Qq function application


Moritz Doll (Feb 21 2024 at 09:56):

If I have a function f how do I qq-match against its application? I've tried variants of the code below, but nothing worked and I couldn't find an example in mathlib

import Lean
import Qq

open Lean Meta Qq

set_option autoImplicit true

variable {α : Q(Type u)}

def test (f : Q($α  $α)) (e : Q($α)) : MetaM Unit := do
  match e with
  | ~q($f $a) =>
    pure ()
  | _ => pure ()

Eric Wieser (Feb 21 2024 at 10:00):

~q matching, just like any other matching, doesn't let you match against things in the local context.

Eric Wieser (Feb 21 2024 at 10:07):

This works:

def test (f : Q($α  $α)) (e : Q($α)) : MetaM Unit := do
  let a  mkFreshExprMVarQ q($α)
  if let .defEq h  isDefEqQ q($e) q($f $a) then
    pure ()
  else
    pure ()

Eric Wieser (Feb 21 2024 at 10:08):

Though there is probably a trap there with metavariable depths


Last updated: May 02 2025 at 03:31 UTC