1212
1313 /
1414 declare_syntax_cat invAlt
15-syntax " | " caseArg " => " tacticSeq : invAlt
15+syntax " | " "@"? caseArg " => " tacticSeq : invAlt
1616
1717 /
1818 expanding to multiple single [invAlt]s. -/
1919 declare_syntax_cat invAlts
20-syntax (" | " caseArg)+ " => " tacticSeq : invAlts
20+syntax (" | " "@"? caseArg)+ " => " tacticSeq : invAlts
2121
2222 /
2323 `inversion h` behaves like `cases h` except that where `cases` would fail with
3838 This is the usual behavior of `cases`.
3939 * The form `inversion t with (tac ...) | tag₁ x ... => tac ... | ... | tagₙ z ... => tac ...`
4040 is supported, similar to that of `cases` and `induction`.
41+ The names `x ...` rename the *last* inaccessible hypotheses of the goal,
42+ which may include the equations left behind by inversion.
43+ * As with `cases`, the form `| @tag x ...` names *all* of the constructor's fields,
44+ including implicit ones, from left to right.
45+ Any names beyond the number of fields rename the remaining inaccessible hypotheses
46+ (e.g. the equations left behind by inversion) as above.
4147 -/
4248 syntax (name := inversion)
4349 "inversion " optConfig ident (" with " (tacticSeq)? (colGe invAlts)*)? : tactic
202208 else pure subgoals
203209 return subgoals
204210
211+/
212+ either `tag` is a suffix of the constructor's name (`step`, `le.step`),
213+ or `tag` starts with the constructor's short name (`nostutter2.zero`). -/
214+def tagMatchesCtor (tag ctor : Name) : Bool :=
215+ tag.isSuffixOf ctor || ctor.getSuffix == tag.getRoot
216+
217+/
218+def findCtor? (ctors : Array (Name × Nat)) (tag : Name) : Option (Name × Nat) :=
219+ ctors.find? (tagMatchesCtor tag ·.1)
220+
205221 /
206222 solve the tagged goal with the provided tactics,
207223 throwing an error if the goal cannot be found or solved. -/
208-def evalInvAlt (goals : List MVarId) (alt : TSyntax `invAlt) : TacticM (List MVarId) :=
224+def evalInvAlt (ctors : Array (Name × Nat)) (goals : List MVarId) (alt : TSyntax `invAlt) :
225+ TacticM (List MVarId) :=
209226 match alt with
210- | `(invAlt| | $tag:ident $vars:binderIdent* => $tactics:tacticSeq) => do
227+ | `(invAlt| | $[@%$at?]? $tag:ident $vars:binderIdent* => $tactics:tacticSeq) => do
211228 if let some goal ← goals.findM? (matchTag tag.getId <$> ·.getUserName) then
212- return [← trySolveGoal vars tactics goal]
229+ return [← trySolveGoal at?.isSome vars tactics goal]
213230 else throwError m!"Invalid alternative name `{tag.getId}`: {← errorMsg}"
214- | `(invAlt| | _ $vars:binderIdent* => $tactics:tacticSeq) => do
231+ | `(invAlt| | $[@%$at?]? _ $vars:binderIdent* => $tactics:tacticSeq) => do
215232 if !goals.isEmpty then
216- goals.mapM (trySolveGoal vars tactics)
233+ goals.mapM (trySolveGoal at?.isSome vars tactics)
217234 else throwErrorAt alt m!"Invalid wildcard alternative: {← errorMsg}"
218235 | _ => throwErrorAt alt "Could not parse inversion alternative"
219236 where
220237 matchTag tag name : Bool :=
221238 tag == name || tag.isPrefixOf name || tag.isSuffixOf name
222- trySolveGoal vars tactics goal : TacticM MVarId := do
239+ /
240+ record them for hovering, and rename inaccessible hypotheses with the rest. -/
241+ trySolveGoal explicit (vars : TSyntaxArray ``binderIdent) tactics goal : TacticM MVarId := do
242+ let vars ← if explicit then do
243+ let numFields := (findCtor? ctors (← goal.getUserName)).map (·.2) |>.getD 0
244+ saveFieldsInfo goal (vars.extract 0 numFields)
245+ pure (vars.extract numFields vars.size)
246+ else pure vars
223247 let goals ← renameInaccessibles goal vars >>= evalTacticAt tactics
224248 unless goals.isEmpty do
225249 reportUnsolvedGoals goals
229253 return if goals.isEmpty
230254 then m!"There are no unhandled alternatives"
231255 else m!"Expected {.orList goalMsgs}"
256+ saveFieldsInfo goal (vars : TSyntaxArray ``binderIdent) : TacticM Unit :=
257+ withSaveInfoContext <| goal.withContext do
258+ for var in vars do
259+ if let `(binderIdent| $x:ident) := var then
260+ if let some decl := (← getLCtx).findFromUserName? x.getId then
261+ addLocalVarInfo x decl.toExpr
232262
233263 @[tactic Lean.Parser.inversion, inherit_doc Lean.Parser.inversion]
234264 public meta def evalInversion : Tactic
235265 | `(tactic| inversion $config $h:ident $[with $[$tactics?:tacticSeq]? $[$alts?:invAlts]*]?) => do
236- let casesSubgoals ← (← getMainGoal).inversion (← getFVarId h) (← elabInversionConfig config)
266+ let h ← getFVarId h
267+ let expandedAlts ← Array.flatten <$> (alts?.getD #[]).mapM expandInvAlts
268+ let ctors ← withMainContext <| getCtors h
269+ let givenNames := ctors.map (givenNamesFor expandedAlts ·.1)
270+ let casesSubgoals ← (← getMainGoal).inversion h (← elabInversionConfig config) givenNames
237271 let mut goals ← casesSubgoals.toList.mapM setUserName
238272 if let some tactics := tactics?.join then
239273 let subgoals ← goals.mapM (evalTacticAt tactics)
240274 goals ← subgoals.flatten.filterM (not <$> ·.isAssignedOrDelayedAssigned)
241- if let some alts := alts? then
242- let expandedAlts ← Array.flatten <$> alts.mapM expandInvAlts
275+ if alts?.isSome then
243276 for alt in expandedAlts do
244- let solvedGoals ← evalInvAlt goals alt
277+ let solvedGoals ← evalInvAlt ctors goals alt
245278 goals := goals.removeAll solvedGoals
246279 unless goals.isEmpty do
247280 reportUnsolvedGoals goals
253286 subgoal.mvarId.setUserName name.getSuffix
254287 return subgoal.mvarId
255288 expandInvAlts
256- | `(invAlts| $[| $args:caseArg]* => $tactics:tacticSeq) =>
257- args.mapM (`(invAlt| | $(·) => $tactics))
289+ | `(invAlts| $[| $[@%$ats?]? $args:caseArg]* => $tactics:tacticSeq) =>
290+ (ats?.zip args).mapM fun (at?, arg) => `(invAlt| | $[@%$at?]? $arg => $tactics)
258291 | stx => throwErrorAt stx "Could not parse inversion alternatives"
292+ /
293+ getCtors h : TacticM (Array (Name × Nat)) := do
294+ let .const n _ := (← whnfD (← h.getType)).getAppFn | return #[]
295+ let some (.inductInfo val) := (← getEnv).find? n | return #[]
296+ val.ctors.toArray.mapM fun ctor => return (ctor, (← getConstInfoCtor ctor).numFields)
297+ /
298+ the names `x ...` that it gives to the constructor's fields. -/
299+ givenNamesFor (alts : Array (TSyntax `invAlt)) ctor : AltVarNames :=
300+ let alt? := alts.findSome? fun
301+ | `(invAlt| | $[@%$at?]? $tag:ident $vars:binderIdent* => $_) =>
302+ if tagMatchesCtor tag.getId ctor then some (at?.isSome, vars) else none
303+ | `(invAlt| | $[@%$at?]? _ $vars:binderIdent* => $_) => some (at?.isSome, vars)
304+ | _ => none
305+ match alt? with
306+ | some (true, vars) => { explicit := true, varNames := vars.toList.map (getNameOfIdent' ·.raw[0]) }
307+ | _ => {}
259308
260309 end Lean.Elab.Tactic
261310
420469 | zero => exact zero
421470 | succ n' => exact n'
422471
472+/-! ## `@` alternatives -/
473+
474+/
475+example (x y : Nat) (h : x ≤ y) : x ≤ y + 1 := by
476+ inversion h with
477+ | refl => exact le.step le.refl
478+ | @step m h' =>
479+ guard_hyp m : Nat
480+ guard_hyp h' : Nat.le x m
481+ exact le.step (le.step h')
482+
483+/
484+example (x y : Nat) (h : x ≤ y) : x ≤ y + 1 := by
485+ cases h with
486+ | refl => exact le.step le.refl
487+ | @step m h' =>
488+ guard_hyp m : Nat
489+ guard_hyp h' : Nat.le x m
490+ exact le.step (le.step h')
491+
492+/
493+example (f : Nat → Nat) (n m : Nat) (leq : n ≤ f m) : True := by
494+ inversion leq with
495+ | refl => trivial
496+ | @step k h' eq heq =>
497+ guard_hyp k : Nat
498+ guard_hyp h' : Nat.le n k
499+ guard_hyp eq : f m = succ k
500+ guard_hyp heq : leq ≍ le.step h'
501+ trivial
502+
503+/
504+example (f : Nat → Nat) (n m : Nat) (leq : n ≤ f m) : True := by
505+ inversion +clear leq with
506+ | @refl | @step _ _ =>
507+ trivial
508+
509+/
510+example : ¬ (NoStutter [3, 1, 1, 4]) := by
511+ intro contra
512+ inversion +clear contra with
513+ | @nostutter2 x y l _ h =>
514+ guard_hyp h : NoStutter [1, 1, 4]
515+ inversion +clear h with
516+ | @_ x y l hneq _ =>
517+ guard_hyp hneq : 1 ≠ 1
518+ apply hneq
519+ rfl
520+
521+/
522+example {α} {l : List α} (h : NoStutter l) (hl : l ≠ []) : l.length > 0 := by
523+ inversion h with
524+ try contradiction
525+ | @nostutter1 a => guard_hyp a : α; simp
526+ | @nostutter2 a b l' hab h' =>
527+ guard_hyp hab : a ≠ b
528+ guard_hyp h' : NoStutter (b :: l')
529+ simp
530+
531+/
532+example (n : Nat) : Nat := by
533+ inversion n with
534+ | @zero => exact zero
535+ | @succ n' => exact n'
536+
537+/
538+#guard_msgs(error) in
539+example (x y : Nat) (h : x ≤ y) : True := by
540+ inversion h with
541+ | refl => trivial
542+ | @step m h' e => trivial
543+
544+/
545+#guard_msgs(error) in
546+example (x y : Nat) (h : x ≤ y) : True := by
547+ inversion h with
548+ | @stop m h' => trivial
549+
423550 /-! # `lemma` tests -/
424551
425552 lemma doubleNegation : ∀ P, P → ¬ ¬ P := by