Skip to content

Commit 3c2d1f3

Browse files
kim-emclaude
andcommitted
feat(Tactic/InferInstanceAsPercent): error on unification failure, guard getAppFn
Throw an error when source and expected types fail to unify, and check that the class heads match before comparing arguments pairwise (#1). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 299e855 commit 3c2d1f3

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

Mathlib/Tactic/InferInstanceAsPercent.lean

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,17 @@ noncomputable instance : Field (FiniteResidueField K) :=
180180
elab "inferInstanceAs% " source:term : term <= expectedType => do
181181
let sourceType ← elabType source
182182
-- Unify source with expected type to resolve metavariables (e.g., _ placeholders)
183-
discard <| withDefault <| isDefEq sourceType expectedType
183+
unless ← withDefault <| isDefEq sourceType expectedType do
184+
throwError "inferInstanceAs%: source type{indentExpr sourceType}\n\
185+
is not defeq to expected type{indentExpr expectedType}"
184186
let sourceType ← instantiateMVars sourceType
185187
let inst ← synthInstance sourceType
186188
let inst ← instantiateMVars inst
187189
let expectedType ← instantiateMVars expectedType
190+
-- Check that the class heads match before comparing arguments pairwise
191+
unless sourceType.getAppFn == expectedType.getAppFn do
192+
throwError "inferInstanceAs%: source type head{indentExpr sourceType.getAppFn}\n\
193+
does not match expected type head{indentExpr expectedType.getAppFn}"
188194
-- Build replacement table from differing carrier type arguments
189195
let replacements ← buildReplacements sourceType expectedType
190196
if replacements.isEmpty then return inst

0 commit comments

Comments
 (0)