LF Terse source diff

Switch volume / audience

LF/Automation.lean

790790 
791791 end RegExp
792792 
793--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
793+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Basics.lean

979979 
980980 end Nat
981981 
982--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
982+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/CustomTactics.lean

1212 
1313 /-- Internal syntactic representation of a single inversion alternative. -/
1414 declare_syntax_cat invAlt
15-syntax " | " caseArg " => " tacticSeq : invAlt
15+syntax " | " "@"? caseArg " => " tacticSeq : invAlt
1616 
1717 /-- An inversion alternative matching one or more goal tags,
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+/-- Whether the alternative tag or goal name `tag` refers to the constructor `ctor`:
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+/-- Find the constructor, with its number of fields, that `tag` refers to. -/
218+def findCtor? (ctors : Array (Name × Nat)) (tag : Name) : Option (Name × Nat) :=
219+  ctors.find? (tagMatchesCtor tag ·.1)
220+
205221 /-- Given an inversion alternative and a list of goals,
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+  /-- With `@`, the first names were already given to the fields by [MVarId.inversion];
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+  /-- The constructors of the inductive type of `h`, with their numbers of fields. -/
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+  /-- If the first alternative matching `ctor` (by name or by wildcard) has the form `| @tag x ...`,
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+/-- As with `cases`, `@` names all of a constructor's fields, including implicit ones -/
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+/-- ...exactly as `cases` does -/
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+/-- Names beyond the fields go to the equations left behind by inversion, in context order -/
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+/-- Fields may be skipped with `_`, and alternatives may share a tactic -/
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+/-- Fields that are unified away still consume their names, as with `cases` -/
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+/-- `@` names survive the optional sequence of tactics on all goals -/
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+/-- Inversion on nonindexed `Type`s with `@` -/
532+example (n : Nat) : Nat := by
533+  inversion n with
534+  | @zero => exact zero
535+  | @succ n' => exact n'
536+
537+/-- error: too many variable names provided -/
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+/-- error: Invalid alternative name `stop`: Expected refl or step -/
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

LF/IndProp.lean

716716 --  The characterizing lemmas for `∈` are called
717717 --  `List.mem_nil_iff` and `List.mem_cons`.
718718 
719--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
719+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Induction.lean

445445 example (b c : Bool) : (b && c) = (c && b) := by
446446   cases b <;> cases c <;> rfl
447447 
448--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
448+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Lists.lean

576576 
577577 end Lists
578578 
579--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
579+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Logic.lean

13391339 --  Output:
13401340 --    Classical.em (p : Prop) : p ∨ ¬p
13411341 
1342--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
1342+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Poly.lean

687687 --  Output:
688688 --    fold_plus : List Nat → Nat → Nat
689689 
690--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
690+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Postscript.lean

6868 --    develops formalized mathematics using Lean and
6969 --    Mathlib.
7070 
71--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
71+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Preface.lean

488488 --  National Science Foundation under the NSF Expeditions
489489 --  grant 1521523, *The Science of Deep Specification*.
490490 
491--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
491+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Tactics.lean

722722 --    generalizing the listed local variables, giving a more
723723 --    general induction hypothesis
724724 
725--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
725+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/Typeclasses.lean

17231723 
17241724 end Reflection
17251725 
1726--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
1726+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF/UsingLean.lean

268268 theorem Nat.double_zero : double 0 = 0 := by rfl
269269 theorem Nat.double_succ (n : Nat) : (n + 1).double = n.double + 2 := by rfl
270270 
271--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
271+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC

LF.lean

1111 import LF.Typeclasses
1212 import LF.Postscript
1313 
14--- Source revision: e2b3e16, committed 2026-10-12 14:58 UTC
14+-- Source revision: 568cec7, committed 2026-10-12 20:20 UTC