Built with Alectryon. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑ Ctrl+↓ to navigate, Ctrl+πŸ–±οΈ to focus. On Mac, use ⌘ instead of Ctrl.
Hover-Settings: Show types: Show goals:
/-

From https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/Lean4.20autocomplete

Will have to keep on fiddling the topic of meta-programming, syntax and dealing with files in Lean

related:

- https://github.com/utensil/LeanBlueprintExample/blob/main/Exe/Decls.lean
- https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/get.20filename.20.2B.20pos

-/

import Lean.Meta.Tactic.TryThis
open Lean.Meta.Tactic

namespace Mathlib.Tactic.imp

open Lean Elab Command FuzzyMatching System

Infer module name of source file name.

def moduleNameOfFileName (fname : FilePath) (rootDir : Option FilePath) : IO Name := do
  Lean.initSearchPath (<- Lean.findSysroot) [(<-IO.currentDir) / "lake-packages/mathlib/"]
  let fname ← IO.FS.realPath fname
  let rootDir ← match rootDir with
    | some rootDir => pure rootDir
    | none         => IO.currentDir
  let mut rootDir ← realPathNormalized rootDir
  if !rootDir.toString.endsWith System.FilePath.pathSeparator.toString then
    rootDir := ⟨rootDir.toString ++ System.FilePath.pathSeparator.toString⟩
  if !rootDir.toString.isPrefixOf fname.normalize.toString then
    throw $ IO.userError s!"input file '{fname}' must be contained in root directory ({rootDir})"
  -- NOTE: use `fname` instead of `fname.normalize` to preserve casing on all platforms
  let fnameSuffixComponents := fname.withExtension "" |>.components.drop (rootDir.components.length + 1)
  let modName    := fnameSuffixComponents.foldl Name.mkStr Name.anonymous
  pure modName

A syntax that allows strings to be printed without surrounding them by guillemets Β«...Β».

declare_syntax_cat write_me_output_stx

@[inherit_doc 
Warning: Parser.Category.write_me_output_stx does not have a doc string
] syntax "import "
ident: Parser.Parser
ident
:
write_me_output_stx: Parser.Category
write_me_output_stx

imp <string> tries to find a file matching Mathlib.<string>. It prints a list of candidates, taking the names from Mathlib.lean. If there is only one candidate, it suggests import Mathlib.<uniqueCandidate> as a Try this.

Using imp! <string> uses fuzzy-matching, rather than exact matching.

elab (name := 
commandImp!: ParserDescr
commandImp!
)
Warning: unused variable `tk` note: this linter can be disabled with `set_option linter.unusedVariables false`
:"imp"
fz: Option (TSyntax Name.anonymous)
fz
:("!")?
na: TSyntax `ident
na
:(
colGt: optParam String "checkColGt" β†’ Parser.Parser
colGt
ident: Parser.Parser
ident
) :
command: Parser.Category
command
=> do let
inp: String
inp
:=
na: TSyntax `ident
na
.
getId: Ident β†’ Name
getId
.
toString: Name β†’ optParam Bool true β†’ String
toString
let
dat: Array String
dat
:=
(← IO.FS.lines ".lake/packages/mathlib/Mathlib.lean"): Array String
(←
IO.FS.lines: FilePath β†’ IO (Array String)
IO.FS.lines
(← IO.FS.lines ".lake/packages/mathlib/Mathlib.lean"): Array String
".lake/packages/mathlib/Mathlib.lean": String
".lake/packages/mathlib/Mathlib.lean"
(← IO.FS.lines ".lake/packages/mathlib/Mathlib.lean"): Array String
)
.
map: {Ξ± Ξ² : Type} β†’ (Ξ± β†’ Ξ²) β†’ Array Ξ± β†’ Array Ξ²
map
(
String.drop: String β†’ Nat β†’ String
String.drop
Β· 15) let
cands: Array String
cands
:=
dat: Array String
dat
.
filter: {Ξ± : Type} β†’ (Ξ± β†’ Bool) β†’ (as : Array Ξ±) β†’ optParam Nat 0 β†’ optParam Nat as.size β†’ Array Ξ±
filter
<| (if
fz: Option (TSyntax Name.anonymous)
fz
.
isSome: {Ξ± : Type} β†’ Option Ξ± β†’ Bool
isSome
then
fuzzyMatch: String β†’ String β†’ optParam Float 0.2 β†’ Bool
fuzzyMatch
else
String.isPrefixOf: String β†’ String β†’ Bool
String.isPrefixOf
)
inp: String
inp
liftTermElabM: {Ξ± : Type} β†’ TermElabM Ξ± β†’ CommandElabM Ξ±
liftTermElabM
do
cands: Array String
cands
.
toList: {Ξ± : Type} β†’ Array Ξ± β†’ List Ξ±
toList
.
forM: {m : Type β†’ Type} β†’ [inst : Monad m] β†’ {Ξ± : Type} β†’ List Ξ± β†’ (Ξ± β†’ m PUnit) β†’ m PUnit
forM
fun
imp: String
imp
=> do let
impS: Name
impS
←
moduleNameOfFileName: FilePath β†’ Option FilePath β†’ IO Name
moduleNameOfFileName
(
".lake/packages/mathlib/Mathlib/": String
".lake/packages/mathlib/Mathlib/"
++
imp: String
imp
.
replace: String β†’ String β†’ String β†’ String
replace
".": String
"."
"/": String
"/"
++
".lean": String
".lean"
)
".lake/packages/mathlib/": String
".lake/packages/mathlib/"
let
imp: TSyntax `write_me_output_stx
imp
:
TSyntax: SyntaxNodeKinds β†’ Type
TSyntax
`write_me_output_stx: Name
`write_me_output_stx
← `(write_me_output_stx| import $(
mkIdent: Name β†’ Ident
mkIdent
impS: Name
impS
)) let
stx: Syntax
stx
←
getRef: {m : Type β†’ Type} β†’ [self : MonadRef m] β†’ m Syntax
getRef
TryThis.addSuggestion: Syntax β†’ TryThis.Suggestion β†’ optParam (Option Syntax) none β†’ optParam String "Try this: " β†’ optParam (Option String) none β†’ MetaM Unit
TryThis.addSuggestion
stx: Syntax
stx
imp: TSyntax `write_me_output_stx
imp
@[inherit_doc
Warning: commandImp! does not have a doc string
] macro "imp!"
na: TSyntax `ident
na
:(
colGt: optParam String "checkColGt" β†’ Parser.Parser
colGt
ident: Parser.Parser
ident
) :
command: Parser.Category
command
=> `(
command: Parser.Category
command
| imp ! $
na: TSyntax `ident
na
) end Mathlib.Tactic.imp
Try this: import GradedAlgebra.Radical
Try this: import Category.TopCommRingCat
Try this: import HahnSeries.Basic
Try this: import HahnSeries.Multiplication
Try this: import BigOperators.Ring
Try this: import BigOperators.Ring.List
Try this: import BigOperators.Ring.Multiset
Try this: import BigOperators.RingEquiv
Try this: import Category.BoolRing
Try this: import Category.ModuleCat.ChangeOfRings
Try this: import Category.Ring.Adjunctions
Try this: import Category.Ring.Basic
Try this: import Category.Ring.Colimits
Try this: import Category.Ring.Constructions
Try this: import Category.Ring.FilteredColimits
Try this: import Category.Ring.Instances
Try this: import Category.Ring.Limits
Try this: import CharP.LocalRing
Try this: import DirectSum.Ring
Try this: import GroupPower.Ring
Try this: import GroupRingAction.Basic
Try this: import GroupRingAction.Invariant
Try this: import GroupRingAction.Subobjects
Try this: import Invertible.Ring
Try this: import MvPolynomial.CommRing
Try this: import Order.BigOperators.Ring.Finset
Try this: import Order.BigOperators.Ring.List
Try this: import Order.BigOperators.Ring.Multiset
Try this: import Order.Hom.Ring
Try this: import Order.Nonneg.Ring
Try this: import Order.Positive.Ring
Try this: import Order.Ring.Abs
Try this: import Order.Ring.Canonical
Try this: import Order.Ring.CharZero
Try this: import Order.Ring.Cone
Try this: import Order.Ring.Defs
Try this: import Order.Ring.InjSurj
Try this: import Order.Ring.Int
Try this: import Order.Ring.Lemmas
Try this: import Order.Ring.Nat
Try this: import Order.Ring.Pow
Try this: import Order.Ring.Star
Try this: import Order.Ring.Synonym
Try this: import Order.Ring.WithTop
Try this: import Polynomial.GroupRingAction
Try this: import Polynomial.RingDivision
Try this: import Ring.AddAut
Try this: import Ring.Aut
Try this: import Ring.Basic
Try this: import Ring.BooleanRing
Try this: import Ring.Center
Try this: import Ring.Centralizer
Try this: import Ring.CentroidHom
Try this: import Ring.Commute
Try this: import Ring.CompTypeclasses
Try this: import Ring.Defs
Try this: import Ring.Divisibility.Basic
Try this: import Ring.Divisibility.Lemmas
Try this: import Ring.Equiv
Try this: import Ring.Ext
Try this: import Ring.Fin
Try this: import Ring.Hom.Basic
Try this: import Ring.Hom.Defs
Try this: import Ring.Hom.IterateHom
Try this: import Ring.Idempotents
Try this: import Ring.Identities
Try this: import Ring.InjSurj
Try this: import Ring.Int
Try this: import Ring.MinimalAxioms
Try this: import Ring.Nat
Try this: import Ring.NegOnePow
Try this: import Ring.Opposite
Try this: import Ring.Parity
Try this: import Ring.Pi
Try this: import Ring.Prod
Try this: import Ring.Regular
Try this: import Ring.Semiconj
Try this: import Ring.Subring.Basic
Try this: import Ring.Subring.Order
Try this: import Ring.Subring.Pointwise
Try this: import Ring.Subring.Units
Try this: import Ring.Subsemiring.Basic
Try this: import Ring.Subsemiring.Order
Try this: import Ring.Subsemiring.Pointwise
Try this: import Ring.ULift
Try this: import Ring.Units
Try this: import Ring.WithZero
Try this: import RingQuot
Try this: import Morphisms.RingHomProperties
Try this: import Complex.RemovableSingularity
Try this: import Normed.Ring.Seminorm
Try this: import Additive.RuzsaCovering
Try this: import Ring
Try this: import Manifold.Sheaf.LocallyRingedSpace
Try this: import RingedSpace.Basic
Try this: import RingedSpace.LocallyRingedSpace
Try this: import RingedSpace.LocallyRingedSpace.HasColimits
Try this: import RingedSpace.OpenImmersion
Try this: import RingedSpace.PresheafedSpace
Try this: import RingedSpace.PresheafedSpace.Gluing
Try this: import RingedSpace.PresheafedSpace.HasColimits
Try this: import RingedSpace.SheafedSpace
Try this: import RingedSpace.Stalks
Try this: import GroupAction.Ring
Try this: import Dimension.DivisionRing
Try this: import Small.Ring
Try this: import Algebra.Ring.Basic
Try this: import Algebra.Ring.FreeCommRing
Try this: import Padics.RingHoms
Try this: import AdicCompletion.Algebra
Try this: import AdicCompletion.Basic
Try this: import Adjoin.Basic
Try this: import Adjoin.FG
Try this: import Adjoin.Field
Try this: import Adjoin.PowerBasis
Try this: import Adjoin.Tower
Try this: import AdjoinRoot
Try this: import AlgebraTower
Try this: import Algebraic
Try this: import AlgebraicIndependent
Try this: import Artinian
Try this: import Bezout
Try this: import Bialgebra.Basic
Try this: import Bialgebra.Equiv
Try this: import Bialgebra.Hom
Try this: import Binomial
Try this: import ChainOfDivisors
Try this: import ClassGroup
Try this: import Coalgebra.Basic
Try this: import Coalgebra.Equiv
Try this: import Coalgebra.Hom
Try this: import Complex
Try this: import Congruence
Try this: import Coprime.Basic
Try this: import Coprime.Ideal
Try this: import Coprime.Lemmas
Try this: import DedekindDomain.AdicValuation
Try this: import DedekindDomain.Basic
Try this: import DedekindDomain.Different
Try this: import DedekindDomain.Dvr
Try this: import DedekindDomain.Factorization
Try this: import DedekindDomain.FiniteAdeleRing
Try this: import DedekindDomain.Ideal
Try this: import DedekindDomain.IntegralClosure
Try this: import DedekindDomain.PID
Try this: import DedekindDomain.SInteger
Try this: import DedekindDomain.SelmerGroup
Try this: import Derivation.Basic
Try this: import Derivation.Lie
Try this: import Derivation.ToSquareZero
Try this: import DiscreteValuationRing.Basic
Try this: import DiscreteValuationRing.TFAE
Try this: import Discriminant
Try this: import EisensteinCriterion
Try this: import Etale.Basic
Try this: import EuclideanDomain
Try this: import Filtration
Try this: import FinitePresentation
Try this: import FiniteType
Try this: import Finiteness
Try this: import Fintype
Try this: import Flat.Algebra
Try this: import Flat.Basic
Try this: import Flat.Stability
Try this: import FractionalIdeal.Basic
Try this: import FractionalIdeal.Norm
Try this: import FractionalIdeal.Operations
Try this: import FreeCommRing
Try this: import FreeRing
Try this: import GradedAlgebra.Basic
Try this: import GradedAlgebra.HomogeneousIdeal
Try this: import GradedAlgebra.HomogeneousLocalization
Try this: import HahnSeries.Addition
Try this: import Polynomial.Dickson
Try this: import HahnSeries.PowerSeries
Try this: import HahnSeries.Summable
Try this: import Henselian
Try this: import HopfAlgebra
Try this: import Ideal.AssociatedPrime
Try this: import Ideal.Basic
Try this: import Ideal.Basis
Try this: import Ideal.Colon
Try this: import Ideal.Cotangent
Try this: import Ideal.IdempotentFG
Try this: import Ideal.IsPrimary
Try this: import Ideal.LocalRing
Try this: import Ideal.Maps
Try this: import Ideal.MinimalPrime
Try this: import Ideal.Norm
Try this: import Ideal.Operations
Try this: import Ideal.Over
Try this: import Ideal.Prod
Try this: import Ideal.Quotient
Try this: import Ideal.QuotientOperations
Try this: import Int.Basic
Try this: import IntegralClosure
Try this: import IntegralDomain
Try this: import IntegralRestrict
Try this: import IntegrallyClosed
Try this: import IsAdjoinRoot
Try this: import IsTensorProduct
Try this: import Jacobson
Try this: import JacobsonIdeal
Try this: import Kaehler
Try this: import LaurentSeries
Try this: import LittleWedderburn
Try this: import LocalProperties
Try this: import Localization.AsSubring
Try this: import Localization.AtPrime
Try this: import Localization.Away.AdjoinRoot
Try this: import Localization.Away.Basic
Try this: import Localization.BaseChange
Try this: import Localization.Basic
Try this: import Localization.Cardinality
Try this: import Localization.FractionRing
Try this: import Localization.Ideal
Try this: import Localization.Integer
Try this: import Localization.Integral
Try this: import Localization.InvSubmonoid
Try this: import Localization.LocalizationLocalization
Try this: import Localization.Module
Try this: import Localization.NormTrace
Try this: import Localization.NumDen
Try this: import Localization.Submodule
Try this: import MatrixAlgebra
Try this: import Multiplicity
Try this: import MvPolynomial.Basic
Try this: import MvPolynomial.Homogeneous
Try this: import MvPolynomial.Ideal
Try this: import MvPolynomial.NewtonIdentities
Try this: import MvPolynomial.Symmetric
Try this: import MvPolynomial.Tower
Try this: import MvPolynomial.WeightedHomogeneous
Try this: import MvPowerSeries.Basic
Try this: import MvPowerSeries.Inverse
Try this: import MvPowerSeries.Trunc
Try this: import Nakayama
Try this: import Nilpotent.Basic
Try this: import Nilpotent.Defs
Try this: import Nilpotent.Lemmas
Try this: import Noetherian
Try this: import NonUnitalSubring.Basic
Try this: import NonUnitalSubsemiring.Basic
Try this: import Norm
Try this: import NormTrace
Try this: import Nullstellensatz
Try this: import OreLocalization.Basic
Try this: import OreLocalization.OreSet
Try this: import Perfection
Try this: import PiTensorProduct
Try this: import Polynomial.Basic
Try this: import Polynomial.Bernstein
Try this: import Polynomial.Chebyshev
Try this: import Polynomial.Content
Try this: import Polynomial.Cyclotomic.Basic
Try this: import Polynomial.Cyclotomic.Eval
Try this: import Polynomial.Cyclotomic.Expand
Try this: import Polynomial.Cyclotomic.Roots
Try this: import TensorProduct.Basic
Try this: import Polynomial.Eisenstein.Basic
Try this: import Polynomial.Eisenstein.IsIntegral
Try this: import Polynomial.GaussLemma
Try this: import Polynomial.Hermite.Basic
Try this: import Polynomial.Hermite.Gaussian
Try this: import Polynomial.IntegralNormalization
Try this: import Polynomial.IrreducibleRing
Try this: import Polynomial.Nilpotent
Try this: import Polynomial.Opposites
Try this: import Polynomial.Pochhammer
Try this: import Polynomial.Quotient
Try this: import Polynomial.RationalRoot
Try this: import Polynomial.ScaleRoots
Try this: import Polynomial.Selmer
Try this: import Polynomial.SeparableDegree
Try this: import Polynomial.Tower
Try this: import Polynomial.Vieta
Try this: import PolynomialAlgebra
Try this: import PowerBasis
Try this: import PowerSeries.Basic
Try this: import PowerSeries.Derivative
Try this: import PowerSeries.Inverse
Try this: import PowerSeries.Order
Try this: import PowerSeries.Trunc
Try this: import PowerSeries.WellKnown
Try this: import Prime
Try this: import PrincipalIdealDomain
Try this: import QuotientNilpotent
Try this: import QuotientNoetherian
Try this: import ReesAlgebra
Try this: import RingHom.Finite
Try this: import RingHom.FiniteType
Try this: import RingHom.Integral
Try this: import RingHom.Surjective
Try this: import RingHomProperties
Try this: import RingInvo
Try this: import RootsOfUnity.Basic
Try this: import RootsOfUnity.Complex
Try this: import RootsOfUnity.Minpoly
Try this: import SimpleModule
Try this: import Smooth.Basic
Try this: import WittVector.FrobeniusFractionField
Try this: import Trace
Try this: import UniqueFactorizationDomain
Try this: import Unramified.Basic
Try this: import Unramified.Derivations
Try this: import Valuation.Basic
Try this: import Valuation.ExtendToLocalization
Try this: import Valuation.Integers
Try this: import Valuation.Integral
Try this: import Valuation.PrimeMultiplicity
Try this: import Valuation.Quotient
Try this: import Valuation.RamificationGroup
Try this: import Valuation.RankOne
Try this: import Valuation.ValuationRing
Try this: import Valuation.ValuationSubring
Try this: import WittVector.Basic
Try this: import WittVector.Compare
Try this: import WittVector.Defs
Try this: import WittVector.DiscreteValuationRing
Try this: import WittVector.Domain
Try this: import WittVector.Frobenius
Try this: import WittVector.WittPolynomial
Try this: import WittVector.Identities
Try this: import WittVector.InitTail
Try this: import WittVector.IsPoly
Try this: import WittVector.Isocrystal
Try this: import WittVector.MulCoeff
Try this: import WittVector.MulP
Try this: import WittVector.StructurePolynomial
Try this: import WittVector.Teichmuller
Try this: import WittVector.Truncated
Try this: import WittVector.Verschiebung
Try this: import Ring.PNat
Try this: import ZMod
Try this: import NoncommRing
Try this: import Ring
Try this: import Ring.Basic
Try this: import Algebra.Ring.Basic
Try this: import Ring.RingNF
Try this: import Algebra.InfiniteSum.Ring
Try this: import Algebra.Ring.Ideal
Try this: import Algebra.UniformRing