Skip to content

Commit fbf00ff

Browse files
committed
chore: revert "feat: abstract metavariables when generalizing match motives (#8099) (#11696)"
This PR reverts #11696.
1 parent 0ad15fe commit fbf00ff

File tree

16 files changed

+149
-298
lines changed

16 files changed

+149
-298
lines changed

src/Init/Grind/Ring/CommSolver.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ def Poly.cancelVar (c : Int) (x : Var) (p : Poly) : Poly :=
766766
(fun _ _ _ _ => a.toPoly_k.pow k)
767767
(fun _ _ _ _ => a.toPoly_k.pow k)
768768
(fun _ _ _ => a.toPoly_k.pow k)
769-
a) = match (generalizing := false) a with
769+
a) = match a with
770770
| num n => Poly.num (n ^ k)
771771
| .intCast n => .num (n^k)
772772
| .natCast n => .num (n^k)

src/Lean/Elab/Match.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ private def generalize (discrs : Array Discr) (matchType : Expr) (altViews : Arr
886886
let matchType' ← forallBoundedTelescope matchType discrs.size fun ds type => do
887887
let type ← mkForallFVars ys type
888888
let (discrs', ds') := Array.unzip <| Array.zip discrExprs ds |>.filter fun (di, _) => di.isFVar
889-
let type type.replaceFVarsM discrs' ds'
889+
let type := type.replaceFVars discrs' ds'
890890
mkForallFVars ds type
891891
if (← isTypeCorrect matchType') then
892892
let discrs := discrs ++ ys.map fun y => { expr := y : Discr }
@@ -1119,11 +1119,11 @@ private def elabMatchAux (generalizing? : Option Bool) (discrStxs : Array Syntax
11191119
withRef altLHS.ref do
11201120
for d in altLHS.fvarDecls do
11211121
if d.hasExprMVar then
1122-
-- This code path is a vestige prior to fixing #8099, but it is still appears to be
1123-
-- important for testcase 1300.lean.
11241122
tryPostpone
11251123
withExistingLocalDecls altLHS.fvarDecls do
11261124
runPendingTacticsAt d.type
1125+
if (← instantiateMVars d.type).hasExprMVar then
1126+
throwMVarError m!"Invalid match expression: The type of pattern variable '{d.toExpr}' contains metavariables:{indentExpr d.type}"
11271127
for p in altLHS.patterns do
11281128
if (← Match.instantiatePatternMVars p).hasExprMVar then
11291129
tryPostpone

src/Lean/Elab/Tactic/Do/VCGen.lean

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,8 @@ where
196196
-- context = fun e => H ⊢ₛ wp⟦e⟧ Q
197197
let context ← withLocalDecl `e .default (mkApp m α) fun e => do
198198
mkLambdaFVars #[e] (goal.withNewProg e).toExpr
199-
return ← info.splitWith goal.toExpr (useSplitter := true) fun altSuff expAltType idx params => do
200-
burnOne
201-
let e ← mkFreshExprMVar (mkApp m α)
202-
unless ← isDefEq (goal.withNewProg e).toExpr expAltType do
203-
throwError "The alternative type {expAltType} returned by `splitWith` does not match {(goal.withNewProg e).toExpr}. This is a bug in `mvcgen`."
204-
let e ← instantiateMVarsIfMVarApp e
199+
return ← info.splitWithConstantMotive goal.toExpr (useSplitter := true) fun altSuff idx params => do
205200
let res ← liftMetaM <| rwIfOrMatcher idx e
206-
-- When `FunInd.rwMatcher` fails, it returns the original expression. We'd loop in that case,
207-
-- so we rather throw an error.
208-
if res.expr == e then
209-
throwError "`rwMatcher` failed to rewrite {indentExpr e}\n\
210-
Check the output of `trace.Elab.Tactic.Do.vcgen.split` for more info and submit a bug report."
211201
let goal' := goal.withNewProg res.expr
212202
let prf ← withAltCtx idx params <| onWPApp goal' (name ++ altSuff)
213203
let res ← Simp.mkCongrArg context res
@@ -253,7 +243,7 @@ where
253243
mkFreshExprSyntheticOpaqueMVar hypsTy (name.appendIndexAfter i)
254244

255245
let (joinPrf, joinGoal) ← forallBoundedTelescope joinTy numJoinParams fun joinParams _body => do
256-
let φ ← info.splitWith (mkSort .zero) fun _suff _expAltType idx altParams =>
246+
let φ ← info.splitWithConstantMotive (mkSort .zero) fun _suff idx altParams =>
257247
return mkAppN hypsMVars[idx]! (joinParams ++ altParams)
258248
withLocalDecl (← mkFreshUserName `h) .default φ fun h => do
259249
-- NB: `mkJoinGoal` is not quite `goal.withNewProg` because we only take 4 args and clear

src/Lean/Elab/Tactic/Do/VCGen/Basic.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace Lean.Elab.Tactic.Do
1616
open Lean Parser Elab Tactic Meta Do SpecAttr
1717

1818
builtin_initialize registerTraceClass `Elab.Tactic.Do.vcgen
19-
builtin_initialize registerTraceClass `Elab.Tactic.Do.vcgen.split
2019

2120
register_builtin_option mvcgen.warning : Bool := {
2221
defValue := true

src/Lean/Elab/Tactic/Do/VCGen/Split.lean

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ Authors: Sebastian Graf
66
module
77

88
prelude
9-
public import Lean.Meta.Tactic.Simp.Types
9+
public import Lean.Meta.Tactic.FunInd
1010
public import Lean.Meta.Match.MatcherApp.Transform
11-
public import Lean.Data.Array
12-
import Lean.Meta.Match.Rewrite
1311
import Lean.Meta.Tactic.Simp.Rewrite
1412
import Lean.Meta.Tactic.Assumption
1513

@@ -51,40 +49,36 @@ def altInfos (info : SplitInfo) : Array (Nat × Expr) := match info with
5149
| matcher matcherApp => matcherApp.altNumParams.mapIdx fun idx numParams =>
5250
(numParams, matcherApp.alts[idx]!)
5351

54-
def splitWith
52+
def splitWithConstantMotive
5553
{n} [MonadLiftT MetaM n] [MonadControlT MetaM n] [Monad n] [MonadError n] [MonadEnv n] [MonadLog n]
5654
[AddMessageContext n] [MonadOptions n]
57-
(info : SplitInfo) (resTy : Expr) (onAlt : Name → Expr → Nat → Array Expr → n Expr) (useSplitter := false) : n Expr := match info with
55+
(info : SplitInfo) (resTy : Expr) (onAlt : Name → Nat → Array Expr → n Expr) (useSplitter := false) : n Expr := match info with
5856
| ite e => do
5957
let u ← getLevel resTy
6058
let c := e.getArg! 1
6159
let h := e.getArg! 2
6260
if useSplitter then -- dite is the "splitter" for ite
6361
let n ← liftMetaM <| mkFreshUserName `h
64-
let t ← withLocalDecl n .default c fun h => do mkLambdaFVars #[h] (← onAlt `isTrue resTy 0 #[])
65-
let e ← withLocalDecl n .default (mkNot c) fun h => do mkLambdaFVars #[h] (← onAlt `isFalse resTy 1 #[])
62+
let t ← withLocalDecl n .default c fun h => do mkLambdaFVars #[h] (← onAlt `isTrue 0 #[])
63+
let e ← withLocalDecl n .default (mkNot c) fun h => do mkLambdaFVars #[h] (← onAlt `isFalse 1 #[])
6664
return mkApp5 (mkConst ``_root_.dite [u]) resTy c h t e
6765
else
68-
let t ← onAlt `isTrue resTy 0 #[]
69-
let e ← onAlt `isFalse resTy 1 #[]
66+
let t ← onAlt `isTrue 0 #[]
67+
let e ← onAlt `isFalse 1 #[]
7068
return mkApp5 (mkConst ``_root_.ite [u]) resTy c h t e
7169
| dite e => do
7270
let u ← getLevel resTy
7371
let c := e.getArg! 1
7472
let h := e.getArg! 2
7573
let n ← liftMetaM <| mkFreshUserName `h
76-
let t ← withLocalDecl n .default c fun h => do mkLambdaFVars #[h] (← onAlt `isTrue resTy 0 #[h])
77-
let e ← withLocalDecl n .default (mkNot c) fun h => do mkLambdaFVars #[h] (← onAlt `isFalse resTy 1 #[h])
74+
let t ← withLocalDecl n .default c fun h => do mkLambdaFVars #[h] (← onAlt `isTrue 0 #[h])
75+
let e ← withLocalDecl n .default (mkNot c) fun h => do mkLambdaFVars #[h] (← onAlt `isFalse 1 #[h])
7876
return mkApp5 (mkConst ``_root_.dite [u]) resTy c h t e
7977
| matcher matcherApp => do
80-
let mask := matcherApp.discrs.map (·.isFVar)
81-
let maskedDiscrs := Array.mask mask matcherApp.discrs
82-
let absMotiveBody ← resTy.abstractM maskedDiscrs
8378
(·.toExpr) <$> matcherApp.transform
8479
(useSplitter := useSplitter) (addEqualities := useSplitter) -- (freshenNames := true)
85-
(onMotive := fun xs _body => pure (absMotiveBody.instantiateRev (Array.mask mask xs)))
86-
(onAlt := fun idx expAltType params _alt => do
87-
onAlt ((`h).appendIndexAfter (idx+1)) expAltType idx params)
80+
(onMotive := fun _xs _motive => pure resTy)
81+
(onAlt := fun idx _ty params _alt => onAlt ((`h).appendIndexAfter (idx+1)) idx params)
8882

8983
def simpDiscrs? (info : SplitInfo) (e : Expr) : SimpM (Option Simp.Result) := match info with
9084
| dite _ | ite _ => return none -- Tricky because we need to simultaneously rewrite `[Decidable c]`
@@ -108,6 +102,6 @@ def rwIfOrMatcher (idx : Nat) (e : Expr) : MetaM Simp.Result := do
108102
let c := if idx = 0 then c else mkNot c
109103
let .some fv ← findLocalDeclWithType? c
110104
| throwError "Failed to find proof for if condition {c}"
111-
rwIfWith (mkFVar fv) e
105+
FunInd.rwIfWith (mkFVar fv) e
112106
else
113-
rwMatcher idx e
107+
FunInd.rwMatcher idx e

src/Lean/Meta/Basic.lean

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,13 +1097,6 @@ Similar to `Expr.abstract`, but handles metavariables correctly.
10971097
def _root_.Lean.Expr.abstractM (e : Expr) (xs : Array Expr) : MetaM Expr :=
10981098
e.abstractRangeM xs.size xs
10991099

1100-
/--
1101-
Replace occurrences of the free variables `fvars` in `e` with `vs`.
1102-
Similar to `Expr.replaceFVars`, but handles metavariables correctly.
1103-
-/
1104-
def _root_.Lean.Expr.replaceFVarsM (e : Expr) (fvars : Array Expr) (vs : Array Expr) : MetaM Expr :=
1105-
return (← e.abstractM fvars).instantiateRev vs
1106-
11071100
/--
11081101
Collect forward dependencies for the free variables in `toRevert`.
11091102
Recall that when reverting free variables `xs`, we must also revert their forward dependencies.

src/Lean/Meta/Match/Rewrite.lean

Lines changed: 0 additions & 149 deletions
This file was deleted.

src/Lean/Meta/Tactic/Congr.lean

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ def MVarId.congr? (mvarId : MVarId) : MetaM (Option (List MVarId)) :=
5151
let some congrThm ← mkCongrSimp? lhs.getAppFn (subsingletonInstImplicitRhs := false) (maxArgs? := lhs.getAppNumArgs) | return none
5252
applyCongrThm? mvarId congrThm
5353

54-
/--
55-
Try to apply a `simp` congruence theorem and throw an error if it fails.
56-
-/
57-
def MVarId.congr (mvarId : MVarId) : MetaM (List MVarId) := do
58-
let some mvarIds ← mvarId.congr? | throwError "Failed to apply `simp` congruence theorem"
59-
return mvarIds
60-
6154
/--
6255
Try to apply a `hcongr` congruence theorem, and then tries to close resulting goals
6356
using `Eq.refl`, `HEq.refl`, and assumption.

0 commit comments

Comments
 (0)