fact stringlengths 6 3.84k | type stringclasses 11 values | library stringclasses 32 values | imports listlengths 1 14 | filename stringlengths 20 95 | symbolic_name stringlengths 1 90 | docstring stringlengths 7 20k ⌀ |
|---|---|---|---|---|---|---|
MaybeFunctionData.get (fData : MaybeFunctionData) : MetaM Expr :=
match fData with
| .letE f | .lam f => pure f
| .data d => d.toExpr | def | Tactic | [
"Qq",
"Mathlib.Tactic.FunProp.Mor",
"Mathlib.Tactic.FunProp.ToBatteries"
] | Mathlib/Tactic/FunProp/FunctionData.lean | MaybeFunctionData.get | Turn `MaybeFunctionData` to the function. |
getFunctionData? (f : Expr)
(unfoldPred : Name → Bool := fun _ => false) :
MetaM MaybeFunctionData := do
withConfig (fun cfg => { cfg with zeta := false, zetaDelta := false }) do
let unfold := fun e : Expr => do
if let some n := e.getAppFn'.constName? then
pure ((unfoldPred n) || (← isReducible n))
else
pure false
let .forallE xName xType _ _ ← instantiateMVars (← inferType f)
| throwError m!"fun_prop bug: function expected, got `{f} : {← inferType f}, \
type ctor {(← inferType f).ctorName}"
withLocalDeclD xName xType fun x => do
let fx' := (← Mor.whnfPred (f.beta #[x]).eta unfold) |> headBetaThroughLet
let f' ← mkLambdaFVars #[x] fx'
match fx' with
| .letE .. => return .letE f'
| .lam .. => return .lam f'
| _ => return .data (← getFunctionData f') | def | Tactic | [
"Qq",
"Mathlib.Tactic.FunProp.Mor",
"Mathlib.Tactic.FunProp.ToBatteries"
] | Mathlib/Tactic/FunProp/FunctionData.lean | getFunctionData | Get `FunctionData` for `f`. |
FunctionData.unfoldHeadFVar? (fData : FunctionData) : MetaM (Option Expr) := do
let .fvar id := fData.fn | return none
let some val ← id.getValue? | return none
let f ← withLCtx fData.lctx fData.insts do
mkLambdaFVars #[fData.mainVar] (headBetaThroughLet (Mor.mkAppN val fData.args))
return f | def | Tactic | [
"Qq",
"Mathlib.Tactic.FunProp.Mor",
"Mathlib.Tactic.FunProp.ToBatteries"
] | Mathlib/Tactic/FunProp/FunctionData.lean | FunctionData.unfoldHeadFVar | If head function is a let-fvar unfold it and return resulting function.
Return `none` otherwise. |
MorApplication where
/-- Of the form `⇑f` i.e. missing argument. -/
| underApplied
/-- Of the form `⇑f x` i.e. morphism and one argument is provided. -/
| exact
/-- Of the form `⇑f x y ...` i.e. additional applied arguments `y ...`. -/
| overApplied
/-- Not a morphism application. -/
| none
deriving Inhabited, BEq | inductive | Tactic | [
"Qq",
"Mathlib.Tactic.FunProp.Mor",
"Mathlib.Tactic.FunProp.ToBatteries"
] | Mathlib/Tactic/FunProp/FunctionData.lean | MorApplication | Type of morphism application. |
FunctionData.isMorApplication (f : FunctionData) : MetaM MorApplication := do
if let some name := f.fn.constName? then
if ← Mor.isCoeFunName name then
let info ← getConstInfo name
let arity := info.type.getNumHeadForalls
match compare arity f.args.size with
| .eq => return .exact
| .lt => return .overApplied
| .gt => return .underApplied
match h : f.args.size with
| 0 => return .none
| n + 1 =>
if f.args[n].coe.isSome then
return .exact
else if f.args.any (fun a => a.coe.isSome) then
return .overApplied
else
return .none | def | Tactic | [
"Qq",
"Mathlib.Tactic.FunProp.Mor",
"Mathlib.Tactic.FunProp.ToBatteries"
] | Mathlib/Tactic/FunProp/FunctionData.lean | FunctionData.isMorApplication | Is function body of `f` a morphism application? What kind? |
FunctionData.peeloffArgDecomposition (fData : FunctionData) : MetaM (Option (Expr × Expr)) := do
unless fData.args.size > 0 do return none
withLCtx fData.lctx fData.insts do
let n := fData.args.size
let x := fData.mainVar
let yₙ := fData.args[n-1]!
if yₙ.expr.containsFVar x.fvarId! then
return none
if fData.args.size = 1 &&
fData.mainVar == fData.fn then
return none
let gBody' := Mor.mkAppN fData.fn fData.args[:n-1]
let gBody' := if let some coe := yₙ.coe then coe.app gBody' else gBody'
let g' ← mkLambdaFVars #[x] gBody'
let f' := Expr.lam `f (← inferType gBody') (.app (.bvar 0) (yₙ.expr)) default
return (f',g') | def | Tactic | [
"Qq",
"Mathlib.Tactic.FunProp.Mor",
"Mathlib.Tactic.FunProp.ToBatteries"
] | Mathlib/Tactic/FunProp/FunctionData.lean | FunctionData.peeloffArgDecomposition | Decomposes `fun x => f y₁ ... yₙ` into `(fun g => g yₙ) ∘ (fun x y => f y₁ ... yₙ₋₁ y)`
Returns none if:
- `n=0`
- `yₙ` contains `x`
- `n=1` and `(fun x y => f y)` is identity function i.e. `x=f` |
FunctionData.nontrivialDecomposition (fData : FunctionData) : MetaM (Option (Expr × Expr)) := do
let mut lctx := fData.lctx
let insts := fData.insts
let x := fData.mainVar
let xId := x.fvarId!
let xName ← withLCtx lctx insts xId.getUserName
let fn := fData.fn
let mut args := fData.args
if fn.containsFVar xId then
return ← fData.peeloffArgDecomposition
let mut yVals : Array Expr := #[]
let mut yVars : Array Expr := #[]
for argId in fData.mainArgs do
let yVal := args[argId]!
let yVal' := yVal.expr
let yId ← withLCtx lctx insts mkFreshFVarId
let yType ← withLCtx lctx insts (inferType yVal')
if yType.containsFVar fData.mainVar.fvarId! then
return none
lctx := lctx.mkLocalDecl yId (xName.appendAfter (toString argId)) yType
let yVar := Expr.fvar yId
yVars := yVars.push yVar
yVals := yVals.push yVal'
args := args.set! argId ⟨yVar, yVal.coe⟩
let g ← withLCtx lctx insts do
mkLambdaFVars #[x] (← mkProdElem yVals)
let f ← withLCtx lctx insts do
(mkLambdaFVars yVars (Mor.mkAppN fn args))
>>=
mkUncurryFun yVars.size
let f' ← fData.toExpr
if (← withReducibleAndInstances <| isDefEq f' f <||> isDefEq f' g) then
return none
return (f, g) | def | Tactic | [
"Qq",
"Mathlib.Tactic.FunProp.Mor",
"Mathlib.Tactic.FunProp.ToBatteries"
] | Mathlib/Tactic/FunProp/FunctionData.lean | FunctionData.nontrivialDecomposition | Decompose function `f = (← fData.toExpr)` into composition of two functions.
Returns none if the decomposition would produce composition with identity function. |
FunctionData.decompositionOverArgs (fData : FunctionData) (args : Array Nat) :
MetaM (Option (Expr × Expr)) := do
unless isOrderedSubsetOf fData.mainArgs args do return none
unless ¬(fData.fn.containsFVar fData.mainVar.fvarId!) do return none
withLCtx fData.lctx fData.insts do
let gxs := args.map (fun i => fData.args[i]!.expr)
try
let gx ← mkProdElem gxs -- this can crash if we have dependent types
let g ← withLCtx fData.lctx fData.insts <| mkLambdaFVars #[fData.mainVar] gx
withLocalDeclD `y (← inferType gx) fun y => do
let ys ← mkProdSplitElem y gxs.size
let args' := (args.zip ys).foldl (init := fData.args)
(fun args' (i,y) => args'.set! i { expr := y, coe := args'[i]!.coe })
let f ← mkLambdaFVars #[y] (Mor.mkAppN fData.fn args')
return (f,g)
catch _ =>
return none | def | Tactic | [
"Qq",
"Mathlib.Tactic.FunProp.Mor",
"Mathlib.Tactic.FunProp.ToBatteries"
] | Mathlib/Tactic/FunProp/FunctionData.lean | FunctionData.decompositionOverArgs | Decompose function `fun x => f y₁ ... yₙ` over specified argument indices `#[i, j, ...]`.
The result is:
```
(fun (yᵢ',yⱼ',...) => f y₁ .. yᵢ' .. yⱼ' .. yₙ) ∘ (fun x => (yᵢ, yⱼ, ...))
```
This is not possible if `yₗ` for `l ∉ #[i,j,...]` still contains `x`.
In such case `none` is returned. |
isCoeFunName (name : Name) : CoreM Bool := do
let some info ← getCoeFnInfo? name | return false
return info.type == .coeFun | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | isCoeFunName | Is `name` a coercion from some function space to functions? |
isCoeFun (e : Expr) : MetaM Bool := do
let some (name, _) := e.getAppFn.const? | return false
let some info ← getCoeFnInfo? name | return false
return e.getAppNumArgs' + 1 == info.numArgs | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | isCoeFun | Is `e` a coercion from some function space to functions? |
App where
/-- morphism coercion -/
coe : Expr
/-- bundled morphism -/
fn : Expr
/-- morphism argument -/
arg : Expr | structure | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | App | Morphism application |
isMorApp? (e : Expr) : MetaM (Option App) := do
let .app (.app coe f) x := e | return none
if ← isCoeFun coe then
return some { coe := coe, fn := f, arg := x }
else
return none | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | isMorApp | Is `e` morphism application? |
partial whnfPred (e : Expr) (pred : Expr → MetaM Bool) :
MetaM Expr := do
whnfEasyCases e fun e => do
let e ← whnfCore e
if let some ⟨coe,f,x⟩ ← isMorApp? e then
let f ← whnfPred f pred
if (← getConfig).zeta then
return (coe.app f).app x
else
return ← mapLetTelescope f fun _ f' => pure ((coe.app f').app x)
if (← pred e) then
match (← unfoldDefinition? e) with
| some e => whnfPred e pred
| none => return e
else
return e | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | whnfPred | Weak normal head form of an expression involving morphism applications. Additionally, `pred`
can specify which when to unfold definitions.
For example calling this on `coe (f a) b` will put `f` in weak normal head form instead of `coe`. |
whnf (e : Expr) : MetaM Expr :=
whnfPred e (fun _ => return false) | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | whnf | Weak normal head form of an expression involving morphism applications.
For example calling this on `coe (f a) b` will put `f` in weak normal head form instead of `coe`. |
Arg where
/-- argument of type `α` -/
expr : Expr
/-- coercion `F → α → β` -/
coe : Option Expr := none
deriving Inhabited | structure | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | Arg | Argument of morphism application that stores corresponding coercion if necessary |
app (f : Expr) (arg : Arg) : Expr :=
match arg.coe with
| none => f.app arg.expr
| some coe => (coe.app f).app arg.expr | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | app | Morphism application |
partial withApp {α} (e : Expr) (k : Expr → Array Arg → MetaM α) : MetaM α :=
go e #[]
where
/-- -/
go : Expr → Array Arg → MetaM α
| .mdata _ b, as => go b as
| .app (.app c f) x, as => do
if ← isCoeFun c then
go f (as.push { coe := c, expr := x})
else
go (.app c f) (as.push { expr := x})
| .app (.proj n i f) x, as => do
let env ← getEnv
let info := getStructureInfo? env n |>.get!
let projFn := getProjFnForField? env n (info.fieldNames[i]!) |>.get!
let .app c f ← mkAppM projFn #[f] | panic! "bug in Mor.withApp"
go (.app (.app c f) x) as
| .app f a, as => go f (as.push { expr := a })
| f, as => k f as.reverse | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | withApp | Given `e = f a₁ a₂ ... aₙ`, returns `k f #[a₁, ..., aₙ]` where `f` can be bundled morphism. |
getAppFn (e : Expr) : MetaM Expr :=
match e with
| .mdata _ b => getAppFn b
| .app (.app c f) _ => do
if ← isCoeFun c then
getAppFn f
else
getAppFn (.app c f)
| .app f _ =>
getAppFn f
| e => return e | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | getAppFn | If the given expression is a sequence of morphism applications `f a₁ .. aₙ`, return `f`.
Otherwise return the input expression. |
getAppArgs (e : Expr) : MetaM (Array Arg) := withApp e fun _ xs => return xs | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | getAppArgs | Given `f a₁ a₂ ... aₙ`, returns `#[a₁, ..., aₙ]` where `f` can be bundled morphism. |
mkAppN (f : Expr) (xs : Array Arg) : Expr :=
xs.foldl (init := f) (fun f x =>
match x with
| ⟨x, .none⟩ => (f.app x)
| ⟨x, some coe⟩ => (coe.app f).app x) | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/Mor.lean | mkAppN | `mkAppN f #[a₀, ..., aₙ]` ==> `f a₀ a₁ .. aₙ` where `f` can be bundled morphism. |
LambdaTheoremArgs
/-- Identity theorem e.g. `Continuous fun x => x` -/
| id
/-- Constant theorem e.g. `Continuous fun x => y` -/
| const
/-- Apply theorem e.g. `Continuous fun (f : (x : X) → Y x => f x)` -/
| apply
/-- Composition theorem e.g. `Continuous f → Continuous g → Continuous fun x => f (g x)`
The numbers `fArgId` and `gArgId` store the argument index for `f` and `g` in the composition
theorem. -/
| comp (fArgId gArgId : Nat)
/-- Pi theorem e.g. `∀ y, Continuous (f · y) → Continuous fun x y => f x y` -/
| pi
deriving Inhabited, BEq, Repr, Hashable | inductive | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | LambdaTheoremArgs | Tag for one of the 5 basic lambda theorems, that also hold extra data for composition theorem |
LambdaTheoremType
/-- Identity theorem e.g. `Continuous fun x => x` -/
| id
/-- Constant theorem e.g. `Continuous fun x => y` -/
| const
/-- Apply theorem e.g. `Continuous fun (f : (x : X) → Y x => f x)` -/
| apply
/-- Composition theorem e.g. `Continuous f → Continuous g → Continuous fun x => f (g x)` -/
| comp
/-- Pi theorem e.g. `∀ y, Continuous (f · y) → Continuous fun x y => f x y` -/
| pi
deriving Inhabited, BEq, Repr, Hashable | inductive | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | LambdaTheoremType | Tag for one of the 5 basic lambda theorems |
LambdaTheoremArgs.type (t : LambdaTheoremArgs) : LambdaTheoremType :=
match t with
| .id => .id
| .const => .const
| .comp .. => .comp
| .apply => .apply
| .pi => .pi | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | LambdaTheoremArgs.type | Convert `LambdaTheoremArgs` to `LambdaTheoremType`. |
detectLambdaTheoremArgs (f : Expr) (ctxVars : Array Expr) :
MetaM (Option LambdaTheoremArgs) := do
let f ← forallTelescope (← inferType f) fun xs _ =>
mkLambdaFVars xs (mkAppN f xs).headBeta
match f with
| .lam _ _ xBody _ =>
unless xBody.hasLooseBVars do return some .const
match xBody with
| .bvar 0 => return some .id
| .app (.bvar 0) (.fvar _) => return some .apply
| .app (.fvar fId) (.app (.fvar gId) (.bvar 0)) =>
let some argId_f := ctxVars.findIdx? (fun x => x == (.fvar fId)) | return none
let some argId_g := ctxVars.findIdx? (fun x => x == (.fvar gId)) | return none
return some <| .comp argId_f argId_g
| .lam _ _ (.app (.app (.fvar _) (.bvar 1)) (.bvar 0)) _ =>
return some .pi
| _ => return none
| _ => return none | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | detectLambdaTheoremArgs | Decides whether `f` is a function corresponding to one of the lambda theorems. |
LambdaTheorem where
/-- Name of function property -/
funPropName : Name
/-- Name of lambda theorem -/
thmName : Name
/-- Type and important argument of the theorem. -/
thmArgs : LambdaTheoremArgs
deriving Inhabited, BEq | structure | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | LambdaTheorem | Structure holding information about lambda theorem. |
LambdaTheorems where
/-- map: function property name × theorem type → lambda theorem -/
theorems : Std.HashMap (Name × LambdaTheoremType) (Array LambdaTheorem) := {}
deriving Inhabited | structure | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | LambdaTheorems | Collection of lambda theorems |
LambdaTheorem.getProof (thm : LambdaTheorem) : MetaM Expr := do
mkConstWithFreshMVarLevels thm.thmName | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | LambdaTheorem.getProof | Return proof of lambda theorem |
LambdaTheoremsExt := SimpleScopedEnvExtension LambdaTheorem LambdaTheorems | abbrev | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | LambdaTheoremsExt | Environment extension storing lambda theorems. |
getLambdaTheorems (funPropName : Name) (type : LambdaTheoremType) :
CoreM (Array LambdaTheorem) := do
return (lambdaTheoremsExt.getState (← getEnv)).theorems.getD (funPropName,type) #[] | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | getLambdaTheorems | Environment extension storing all lambda theorems. -/
initialize lambdaTheoremsExt : LambdaTheoremsExt ←
registerSimpleScopedEnvExtension {
name := by exact decl_name%
initial := {}
addEntry := fun d e =>
{d with theorems :=
let es := d.theorems.getD (e.funPropName, e.thmArgs.type) #[]
d.theorems.insert (e.funPropName, e.thmArgs.type) (es.push e)}
}
/-- Get lambda theorems for particular function property `funPropName`. |
TheoremForm where
| uncurried | comp
deriving Inhabited, BEq, Repr | inductive | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | TheoremForm | Function theorems are stated in uncurried or compositional form.
uncurried
```
theorem Continuous_add : Continuous (fun x => x.1 + x.2)
```
compositional
```
theorem Continuous_add (hf : Continuous f) (hg : Continuous g) : Continuous (fun x => (f x) + (g x))
``` |
FunctionTheorem where
/-- function property name -/
funPropName : Name
/-- theorem name -/
thmOrigin : Origin
/-- function name -/
funOrigin : Origin
/-- array of argument indices about which this theorem is about -/
mainArgs : Array Nat
/-- total number of arguments applied to the function -/
appliedArgs : Nat
/-- priority -/
priority : Nat := eval_prio default
/-- form of the theorem, see documentation of TheoremForm -/
form : TheoremForm
deriving Inhabited, BEq
private local instance : Ord Name := ⟨Name.quickCmp⟩
set_option linter.style.docString.empty false in | structure | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | FunctionTheorem | TheoremForm to string -/
instance : ToString TheoremForm :=
⟨fun x => match x with | .uncurried => "simple" | .comp => "compositional"⟩
/-- theorem about specific function (either declared constant or free variable) |
FunctionTheorems where
/-- map: function name → function property → function theorem -/
theorems :
TreeMap Name (TreeMap Name (Array FunctionTheorem) compare) compare := {}
deriving Inhabited | structure | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | FunctionTheorems | null |
FunctionTheorem.getProof (thm : FunctionTheorem) : MetaM Expr := do
match thm.thmOrigin with
| .decl name => mkConstWithFreshMVarLevels name
| .fvar id => return .fvar id
set_option linter.style.docString.empty false in | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | FunctionTheorem.getProof | return proof of function theorem |
FunctionTheoremsExt := SimpleScopedEnvExtension FunctionTheorem FunctionTheorems | abbrev | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | FunctionTheoremsExt | null |
getTheoremsForFunction (funName : Name) (funPropName : Name) :
CoreM (Array FunctionTheorem) := do
return (functionTheoremsExt.getState (← getEnv)).theorems.getD funName {}
|>.getD funPropName #[] | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | getTheoremsForFunction | Extension storing all function theorems. -/
initialize functionTheoremsExt : FunctionTheoremsExt ←
registerSimpleScopedEnvExtension {
name := by exact decl_name%
initial := {}
addEntry := fun d e =>
{d with
theorems :=
d.theorems.alter e.funOrigin.name fun funProperties =>
let funProperties := funProperties.getD {}
funProperties.alter e.funPropName fun thms =>
let thms := thms.getD #[]
thms.push e}
}
set_option linter.style.docString.empty false in
/-- |
GeneralTheorem.getProof (thm : GeneralTheorem) : MetaM Expr := do
mkConstWithFreshMVarLevels thm.thmName | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | GeneralTheorem.getProof | Get proof of a theorem. |
GeneralTheoremsExt := SimpleScopedEnvExtension GeneralTheorem GeneralTheorems | abbrev | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | GeneralTheoremsExt | Extensions for transition or morphism theorems |
getTransitionTheorems (e : Expr) : FunPropM (Array GeneralTheorem) := do
let thms := (← get).transitionTheorems.theorems
let (candidates, thms) ← withConfig (fun cfg => { cfg with iota := false, zeta := false }) <|
thms.getMatch e false true
modify ({ · with transitionTheorems := ⟨thms⟩ })
return (← MonadExcept.ofExcept candidates).toArray | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | getTransitionTheorems | Environment extension for transition theorems. -/
initialize transitionTheoremsExt : GeneralTheoremsExt ←
registerSimpleScopedEnvExtension {
name := by exact decl_name%
initial := {}
addEntry := fun d e =>
{d with theorems := e.keys.foldl (fun thms (key, entry) =>
RefinedDiscrTree.insert thms key (entry, e)) d.theorems}
}
/-- Get transition theorems applicable to `e`.
For example calling on `e` equal to `Continuous f` might return theorems implying continuity
from linearity over finite-dimensional spaces or differentiability. |
getMorphismTheorems (e : Expr) : FunPropM (Array GeneralTheorem) := do
let thms := (← get).morTheorems.theorems
let (candidates, thms) ← withConfig (fun cfg => { cfg with iota := false, zeta := false }) <|
thms.getMatch e false true
modify ({ · with morTheorems := ⟨thms⟩ })
return (← MonadExcept.ofExcept candidates).toArray | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | getMorphismTheorems | Environment extension for morphism theorems. -/
initialize morTheoremsExt : GeneralTheoremsExt ←
registerSimpleScopedEnvExtension {
name := by exact decl_name%
initial := {}
addEntry := fun d e =>
{d with theorems := e.keys.foldl (fun thms (key, entry) =>
RefinedDiscrTree.insert thms key (entry, e)) d.theorems}
}
/-- Get morphism theorems applicable to `e`.
For example calling on `e` equal to `Continuous f` for `f : X→L[ℝ] Y` would return theorem
inferring continuity from the bundled morphism. |
Theorem where
| lam (thm : LambdaTheorem)
| function (thm : FunctionTheorem)
| mor (thm : GeneralTheorem)
| transition (thm : GeneralTheorem) | inductive | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | Theorem | There are four types of theorems:
- lam - theorem about basic lambda calculus terms
- function - theorem about a specific function(declared or free variable) in specific arguments
- mor - special theorems talking about bundled morphisms/DFunLike.coe
- transition - theorems inferring one function property from another
Examples:
- lam
```
theorem Continuous_id : Continuous fun x => x
theorem Continuous_comp (hf : Continuous f) (hg : Continuous g) : Continuous fun x => f (g x)
```
- function
```
theorem Continuous_add : Continuous (fun x => x.1 + x.2)
theorem Continuous_add (hf : Continuous f) (hg : Continuous g) :
Continuous (fun x => (f x) + (g x))
```
- mor - the head of function body has to be ``DFunLike.code
```
theorem ContDiff.clm_apply {f : E → F →L[𝕜] G} {g : E → F}
(hf : ContDiff 𝕜 n f) (hg : ContDiff 𝕜 n g) :
ContDiff 𝕜 n fun x => (f x) (g x)
theorem clm_linear {f : E →L[𝕜] F} : IsLinearMap 𝕜 f
```
- transition - the conclusion has to be in the form `P f` where `f` is a free variable
```
theorem linear_is_continuous [FiniteDimensional ℝ E] {f : E → F} (hf : IsLinearMap 𝕜 f) :
Continuous f
``` |
getTheoremFromConst (declName : Name) (prio : Nat := eval_prio default) : MetaM Theorem := do
let info ← getConstInfo declName
forallTelescope info.type fun xs b => do
let some (decl,f) ← getFunProp? b
| throwError "unrecognized function property `{← ppExpr b}`"
let funPropName := decl.funPropName
let fData? ←
withConfig (fun cfg => { cfg with zeta := false}) <| getFunctionData? f defaultUnfoldPred
if let some thmArgs ← detectLambdaTheoremArgs (← fData?.get) xs then
return .lam {
funPropName := funPropName
thmName := declName
thmArgs := thmArgs
}
let .data fData := fData?
| throwError s!"function in invalid form {← ppExpr f}"
match fData.fn with
| .const funName _ =>
let dec ← fData.nontrivialDecomposition
let form : TheoremForm := if dec.isSome || funName == ``Prod.mk then .comp else .uncurried
return .function {
funPropName := funPropName
thmOrigin := .decl declName
funOrigin := .decl funName
mainArgs := fData.mainArgs
appliedArgs := fData.args.size
priority := prio
form := form
}
| .fvar .. =>
let (_,_,b') ← forallMetaTelescope info.type
let keys ← RefinedDiscrTree.initializeLazyEntryWithEta b'
let thm : GeneralTheorem := {
funPropName := funPropName
thmName := declName
keys := keys
priority := prio
}
match (← fData.isMorApplication) with
| .exact => return .mor thm
| .underApplied | .overApplied =>
throwError "fun_prop theorem about morphism coercion has to be in fully applied form"
| .none =>
if fData.fn.isFVar && (fData.args.size == 1) &&
(fData.args[0]!.expr == fData.mainVar) then
return .transition thm
throwError "Not a valid `fun_prop` theorem!"
| _ =>
throwError "unrecognized theoremType `{← ppExpr b}`" | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | getTheoremFromConst | For a theorem declaration `declName` return `fun_prop` theorem. It correctly detects which
type of theorem it is. |
addTheorem (declName : Name) (attrKind : AttributeKind := .global)
(prio : Nat := eval_prio default) : MetaM Unit := do
match (← getTheoremFromConst declName prio) with
| .lam thm =>
trace[Meta.Tactic.fun_prop.attr] "\
lambda theorem: {thm.thmName}
function property: {thm.funPropName}
type: {repr thm.thmArgs.type}"
lambdaTheoremsExt.add thm attrKind
| .function thm =>
trace[Meta.Tactic.fun_prop.attr] "\
function theorem: {thm.thmOrigin.name}
function property: {thm.funPropName}
function name: {thm.funOrigin.name}
main arguments: {thm.mainArgs}
applied arguments: {thm.appliedArgs}
form: {toString thm.form} form"
functionTheoremsExt.add thm attrKind
| .mor thm =>
trace[Meta.Tactic.fun_prop.attr] "\
morphism theorem: {thm.thmName}
function property: {thm.funPropName}"
morTheoremsExt.add thm attrKind
| .transition thm =>
trace[Meta.Tactic.fun_prop.attr] "\
transition theorem: {thm.thmName}
function property: {thm.funPropName}"
transitionTheoremsExt.add thm attrKind | def | Tactic | [
"Mathlib.Tactic.FunProp.Decl",
"Mathlib.Tactic.FunProp.Types",
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Initialize",
"Mathlib.Lean.Meta.RefinedDiscrTree.Lookup"
] | Mathlib/Tactic/FunProp/Theorems.lean | addTheorem | Register theorem `declName` with `fun_prop`. |
isOrderedSubsetOf {α} [Inhabited α] [DecidableEq α] (a b : Array α) : Bool :=
Id.run do
if a.size > b.size then
return false
let mut i := 0
for h' : j in [0:b.size] do
if i = a.size then
break
if a[i]! = b[j] then
i := i+1
if i = a.size then
return true
else
return false | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | isOrderedSubsetOf | Check if `a` can be obtained by removing elements from `b`. |
_root_.Lean.Expr.swapBVars (e : Expr) (i j : Nat) : Expr :=
let swapBVarArray : Array Expr := Id.run do
let mut a : Array Expr := .mkEmpty e.looseBVarRange
for k in [0:e.looseBVarRange] do
a := a.push (.bvar (if k = i then j else if k = j then i else k))
a
e.instantiate swapBVarArray | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | _root_.Lean.Expr.swapBVars | Swaps bvars indices `i` and `j`
NOTE: the indices `i` and `j` do not correspond to the `n` in `bvar n`. Rather
they behave like indices in `Expr.lowerLooseBVars`, `Expr.liftLooseBVars`, etc.
TODO: This has to have a better implementation, but I'm still beyond confused with how bvar
indices work |
mkProdElem (xs : Array Expr) : MetaM Expr := do
match h : xs.size with
| 0 => return default
| 1 => return xs[0]
| n + 1 =>
xs[0:n].foldrM (init := xs[n]) fun x p => mkAppM ``Prod.mk #[x,p] | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | mkProdElem | For `#[x₁, .., xₙ]` create `(x₁, .., xₙ)`. |
mkProdProj (x : Expr) (i : Nat) (n : Nat) : MetaM Expr := do
match i, n with
| _, 0 => pure x
| _, 1 => pure x
| 0, _ => mkAppM ``Prod.fst #[x]
| i'+1, n'+1 => mkProdProj (← withTransparency .all <| mkAppM ``Prod.snd #[x]) i' n' | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | mkProdProj | For `(x₀, .., xₙ₋₁)` return `xᵢ` but as a product projection.
We need to know the total size of the product to be considered.
For example for `xyz : X × Y × Z`
- `mkProdProj xyz 1 3` returns `xyz.snd.fst`.
- `mkProdProj xyz 1 2` returns `xyz.snd`. |
mkProdSplitElem (xs : Expr) (n : Nat) : MetaM (Array Expr) :=
(Array.range n)
|>.mapM (fun i ↦ mkProdProj xs i n) | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | mkProdSplitElem | For an element of a product type(of size`n`) `xs` create an array of all possible projections
i.e. `#[xs.1, xs.2.1, xs.2.2.1, ..., xs.2..2]` |
mkUncurryFun (n : Nat) (f : Expr) : MetaM Expr := do
if n ≤ 1 then
return f
forallBoundedTelescope (← inferType f) n fun xs _ ↦ do
let xProdName : String ← xs.foldlM (init:="") fun n x ↦
do return (n ++ toString (← x.fvarId!.getUserName).eraseMacroScopes)
let xProdType ← inferType (← mkProdElem xs)
withLocalDecl (.mkSimple xProdName) default xProdType fun xProd ↦ do
let xs' ← mkProdSplitElem xProd n
mkLambdaFVars #[xProd] (← mkAppM' f xs').headBeta | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | mkUncurryFun | Uncurry function `f` in `n` arguments. |
etaExpand1 (f : Expr) : MetaM Expr := do
let f := f.eta
if f.isLambda then
return f
else
withDefault do forallBoundedTelescope (← inferType f) (.some 1) fun xs _ => do
mkLambdaFVars xs (mkAppN f xs) | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | etaExpand1 | Eta expand `f` in only one variable and reduce in others.
Examples:
```
f ==> fun x => f x
fun x y => f x y ==> fun x => f x
HAdd.hAdd y ==> fun x => HAdd.hAdd y x
HAdd.hAdd ==> fun x => HAdd.hAdd x
``` |
private betaThroughLetAux (f : Expr) (args : List Expr) : Expr :=
match f, args with
| f, [] => f
| .lam _ _ b _, a :: as => (betaThroughLetAux (b.instantiate1 a) as)
| .letE n t v b nondep, args => .letE n t v (betaThroughLetAux b args) nondep
| .mdata _ b, args => betaThroughLetAux b args
| f, args => mkAppN f args.toArray | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | betaThroughLetAux | Implementation of `betaThroughLet` |
betaThroughLet (f : Expr) (args : Array Expr) : Expr :=
betaThroughLetAux f args.toList | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | betaThroughLet | Apply the given arguments to `f`, beta-reducing if `f` is a lambda expression. This variant
does beta-reduction through let bindings without inlining them.
Example
```
beta' (fun x => let y := x * x; fun z => x + y + z) #[a,b]
==>
let y := a * a; a + y + b
``` |
headBetaThroughLet (e : Expr) : Expr :=
let f := e.getAppFn
if f.isHeadBetaTargetFn true then betaThroughLet f e.getAppArgs else e | def | Tactic | [
"Mathlib.Init"
] | Mathlib/Tactic/FunProp/ToBatteries.lean | headBetaThroughLet | Beta reduces head of an expression, `(fun x => e) a` ==> `e[x/a]`. This version applies
arguments through let bindings without inlining them.
Example
```
headBeta' ((fun x => let y := x * x; fun z => x + y + z) a b)
==>
let y := a * a; a + y + b
``` |
Origin where
/-- It is a constant defined in the environment. -/
| decl (name : Name)
/-- It is a free variable in the local context. -/
| fvar (fvarId : FVarId)
deriving Inhabited, BEq | inductive | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | Origin | Indicated origin of a function or a statement. |
Origin.name (origin : Origin) : Name :=
match origin with
| .decl name => name
| .fvar id => id.name | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | Origin.name | Name of the origin. |
Origin.getValue (origin : Origin) : MetaM Expr := do
match origin with
| .decl name => mkConstWithFreshMVarLevels name
| .fvar id => pure (.fvar id) | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | Origin.getValue | Get the expression specified by `origin`. |
ppOrigin {m} [Monad m] [MonadEnv m] [MonadError m] : Origin → m MessageData
| .decl n => return m!"{← mkConstWithLevelParams n}"
| .fvar n => return mkFVar n | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | ppOrigin | Pretty print `FunProp.Origin`. |
ppOrigin' (origin : Origin) : MetaM String := do
match origin with
| .fvar id => return s!"{← ppExpr (.fvar id)} : {← ppExpr (← inferType (.fvar id))}"
| _ => pure (toString origin.name) | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | ppOrigin' | Pretty print `FunProp.Origin`. Returns string unlike `ppOrigin`. |
FunctionData.getFnOrigin (fData : FunctionData) : Origin :=
match fData.fn with
| .fvar id => .fvar id
| .const name _ => .decl name
| _ => .decl Name.anonymous | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | FunctionData.getFnOrigin | Get origin of the head function. |
defaultNamesToUnfold : Array Name :=
#[`id, `Function.comp, `Function.HasUncurry.uncurry, `Function.uncurry] | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | defaultNamesToUnfold | Default names to be considered reducible by `fun_prop` |
Config where
/-- Maximum number of transitions between function properties. For example inferring continuity
from differentiability and then differentiability from smoothness (`ContDiff ℝ ∞`) requires
`maxTransitionDepth = 2`. The default value of one expects that transition theorems are
transitively closed e.g. there is a transition theorem that infers continuity directly from
smoothness.
Setting `maxTransitionDepth` to zero will disable all transition theorems. This can be very
useful when `fun_prop` should fail quickly. For example when using `fun_prop` as discharger in
`simp`.
-/
maxTransitionDepth := 1
/-- Maximum number of steps `fun_prop` can take. -/
maxSteps := 100000
deriving Inhabited, BEq | structure | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | Config | `fun_prop` configuration |
Context where
/-- fun_prop config -/
config : Config := {}
/-- Name to unfold -/
constToUnfold : TreeSet Name Name.quickCmp :=
.ofArray defaultNamesToUnfold _
/-- Custom discharger to satisfy theorem hypotheses. -/
disch : Expr → MetaM (Option Expr) := fun _ => pure none
/-- current transition depth -/
transitionDepth := 0 | structure | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | Context | `fun_prop` context |
GeneralTheorem where
/-- function property name -/
funPropName : Name
/-- theorem name -/
thmName : Name
/-- discrimination tree keys used to index this theorem -/
keys : List (RefinedDiscrTree.Key × RefinedDiscrTree.LazyEntry)
/-- priority -/
priority : Nat := eval_prio default
deriving Inhabited | structure | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | GeneralTheorem | General theorem about a function property used for transition and morphism theorems |
GeneralTheorems where
/-- Discrimination tree indexing theorems. -/
theorems : RefinedDiscrTree GeneralTheorem := {}
deriving Inhabited | structure | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | GeneralTheorems | Structure holding transition or morphism theorems for `fun_prop` tactic. |
State where
/-- Simp's cache is used as the `fun_prop` tactic is designed to be used inside of simp and
utilize its cache. It holds successful goals. -/
cache : Simp.Cache := {}
/-- Cache storing failed goals such that they are not tried again. -/
failureCache : ExprSet := {}
/-- Count the number of steps and stop when maxSteps is reached. -/
numSteps := 0
/-- Log progress and failures messages that should be displayed to the user at the end. -/
msgLog : List String := []
/-- `RefinedDiscrTree` is lazy, so we store the partially evaluated tree. -/
morTheorems : GeneralTheorems
/-- `RefinedDiscrTree` is lazy, so we store the partially evaluated tree. -/
transitionTheorems : GeneralTheorems | structure | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | State | `fun_prop` state |
Context.increaseTransitionDepth (ctx : Context) : Context :=
{ctx with transitionDepth := ctx.transitionDepth + 1} | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | Context.increaseTransitionDepth | Increase depth |
FunPropM := ReaderT FunProp.Context <| StateT FunProp.State MetaM
set_option linter.style.docString.empty false in | abbrev | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | FunPropM | Monad to run `fun_prop` tactic in. |
Result where
/-- -/
proof : Expr | structure | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | Result | Result of `funProp`, it is a proof of function property `P f` |
defaultUnfoldPred : Name → Bool :=
defaultNamesToUnfold.contains | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | defaultUnfoldPred | Default names to unfold |
unfoldNamePred : FunPropM (Name → Bool) := do
let toUnfold := (← read).constToUnfold
return fun n => toUnfold.contains n | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | unfoldNamePred | Get predicate on names indicating whether they should be unfolded. |
increaseSteps : FunPropM Unit := do
let numSteps := (← get).numSteps
let maxSteps := (← read).config.maxSteps
if numSteps > maxSteps then
throwError s!"fun_prop failed, maximum number({maxSteps}) of steps exceeded"
modify (fun s => {s with numSteps := s.numSteps + 1}) | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | increaseSteps | Increase heartbeat, throws error when `maxSteps` was reached |
withIncreasedTransitionDepth {α} (go : FunPropM (Option α)) : FunPropM (Option α) := do
let maxDepth := (← read).config.maxTransitionDepth
let newDepth := (← read).transitionDepth + 1
if newDepth > maxDepth then
trace[Meta.Tactic.fun_prop]
"maximum transition depth ({maxDepth}) reached
if you want `fun_prop` to continue then increase the maximum depth with \
`fun_prop (maxTransitionDepth := {newDepth})`"
return none
else
withReader (fun s => {s with transitionDepth := newDepth}) go | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | withIncreasedTransitionDepth | Increase transition depth. Return `none` if maximum transition depth has been reached. |
logError (msg : String) : FunPropM Unit := do
if (← read).transitionDepth = 0 then
modify fun s =>
{s with msgLog :=
if s.msgLog.contains msg then
s.msgLog
else
msg::s.msgLog} | def | Tactic | [
"Mathlib.Tactic.FunProp.FunctionData",
"Mathlib.Lean.Meta.RefinedDiscrTree.Basic"
] | Mathlib/Tactic/FunProp/Types.lean | logError | Log error message that will displayed to the user at the end.
Messages are logged only when `transitionDepth = 0` i.e. when `fun_prop` is **not** trying to infer
function property like continuity from another property like differentiability.
The main reason is that if the user forgets to add a continuity theorem for function `foo` then
`fun_prop` should report that there is a continuity theorem for `foo` missing. If we would log
messages `transitionDepth > 0` then user will see messages saying that there is a missing theorem
for differentiability, smoothness, ... for `foo`. |
mul_le_mul_of_nonneg_left [Mul α] [Zero α] [Preorder α] [PosMulMono α]
{a b c : α} (h : b ≤ c) (a0 : 0 ≤ a) :
a * b ≤ a * c | theorem | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | mul_le_mul_of_nonneg_left | null |
mul_le_mul_of_nonneg_right [Mul α] [Zero α] [Preorder α] [MulPosMono α]
{a b c : α} (h : b ≤ c) (a0 : 0 ≤ a) :
b * a ≤ c * a | theorem | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | mul_le_mul_of_nonneg_right | null |
mul_le_mul [MulZeroClass α] [Preorder α] [PosMulMono α] [MulPosMono α]
{a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) (c0 : 0 ≤ c) (b0 : 0 ≤ b) :
a * c ≤ b * d
```
The advantage of this approach is that the lemmas with fewer "varying" input pairs typically require
fewer side conditions, so the tactic becomes more useful by special-casing them.
There can also be more than one generalized congruence lemma dealing with the same relation
and head function, for example with purely notational head
functions which have different theories when different typeclass assumptions apply. For example,
the following lemma is stored with the same `@[gcongr]` data as `mul_le_mul` above, and the two
lemmas are simply tried in succession to determine which has the typeclasses relevant to the goal:
``` | theorem | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | mul_le_mul | null |
mul_le_mul' [Mul α] [Preorder α] [MulLeftMono α]
[MulRightMono α] {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) :
a * c ≤ b * d
``` | theorem | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | mul_le_mul' | null |
GCongr.Finset.sum_le_sum [OrderedAddCommMonoid N] {f g : ι → N} {s : Finset ι}
(h : ∀ (i : ι), i ∈ s → f i ≤ g i) :
s.sum f ≤ s.sum g
```
The tactic automatically introduces the variable `i✝ : ι` and hypothesis `hi✝ : i✝ ∈ s` in the
subgoal `∀ (i : ι), i ∈ s → f i ≤ g i` generated by applying this lemma. By default this is done
anonymously, so they are inaccessible in the goal state which results. The user can name them if
needed using the syntax `gcongr with i hi`. | theorem | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | GCongr.Finset.sum_le_sum | null |
GCongrKey where
/-- The name of the relation. For example, `a + b ≤ a + c` has ``relName := `LE.le``. -/
relName : Name
/-- The name of the head function. For example, `a + b ≤ a + c` has ``head := `HAdd.hAdd``. -/
head : Name
/-- The number of arguments that `head` is applied to.
For example, `a + b ≤ a + c` has `arity := 6`, because `HAdd.hAdd` has 6 arguments. -/
arity : Nat
deriving Inhabited, BEq, Hashable | structure | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | GCongrKey | `GCongrKey` is the key used in the hashmap for looking up `gcongr` lemmas. |
GCongrLemma where
/-- The key under which the lemma is stored. -/
key : GCongrKey
/-- The name of the lemma. -/
declName : Name
/-- `mainSubgoals` are the subgoals on which `gcongr` will be recursively called. They store
- the index of the hypothesis
- the index of the arguments in the conclusion
- the number of parameters in the hypothesis -/
mainSubgoals : Array (Nat × Nat × Nat)
/-- The number of arguments that `declName` takes when applying it. -/
numHyps : Nat
/-- The given priority of the lemma, for example as `@[gcongr high]`. -/
prio : Nat
/-- The number of arguments in the application of `head` that are different.
This is used for sorting the lemmas.
For example, `a + b ≤ a + c` has `numVarying := 1`. -/
numVarying : Nat
deriving Inhabited | structure | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | GCongrLemma | Structure recording the data for a "generalized congruence" (`gcongr`) lemma. |
GCongrLemmas := Std.HashMap GCongrKey (List GCongrLemma) | abbrev | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | GCongrLemmas | A collection of `GCongrLemma`, to be stored in the environment extension. |
GCongrLemma.prioLE (a b : GCongrLemma) : Bool :=
(compare a.prio b.prio).then (compare b.numVarying a.numVarying) |>.isLE | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | GCongrLemma.prioLE | Return `true` if the priority of `a` is less than or equal to the priority of `b`. |
addGCongrLemmaEntry (m : GCongrLemmas) (l : GCongrLemma) : GCongrLemmas :=
match m[l.key]? with
| none => m.insert l.key [l]
| some es => m.insert l.key <| insert l es
where
/--- Insert a `GCongrLemma` in the correct place in a list of lemmas. -/
insert (l : GCongrLemma) : List GCongrLemma → List GCongrLemma
| [] => [l]
| l'::ls => if l'.prioLE l then l::l'::ls else l' :: insert l ls | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | addGCongrLemmaEntry | Insert a `GCongrLemma` in a collection of lemmas, making sure that the lemmas are sorted. |
getCongrAppFnArgs (e : Expr) : Option (Name × Array Expr) :=
match e.cleanupAnnotations with
| .forallE n d b bi =>
if b.hasLooseBVars then
some (`_Forall, #[.lam n d b bi])
else
some (`_Implies, #[d, b])
| e => e.withApp fun f args => f.constName?.map (·, args) | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | getCongrAppFnArgs | Environment extension for "generalized congruence" (`gcongr`) lemmas. -/
initialize gcongrExt : SimpleScopedEnvExtension GCongrLemma
(Std.HashMap GCongrKey (List GCongrLemma)) ←
registerSimpleScopedEnvExtension {
addEntry := addGCongrLemmaEntry
initial := {}
}
/-- Given an application `f a₁ .. aₙ`, return the name of `f`, and the array of arguments `aᵢ`. |
getRel (e : Expr) : Option (Name × Expr × Expr) :=
match e with
| .app (.app rel lhs) rhs => rel.getAppFn.constName?.map (·, lhs, rhs)
| .forallE _ lhs rhs _ =>
if !rhs.hasLooseBVars then
some (`_Implies, lhs, rhs)
else
none
| _ => none | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | getRel | If `e` is of the form `r a b`, return `(r, a, b)`. |
makeGCongrLemma (declName : Name) (declTy : Expr) (numHyps prio : Nat) : MetaM GCongrLemma := do
withReducible <| forallBoundedTelescope declTy numHyps fun xs targetTy => do
let fail {α} (m : MessageData) : MetaM α := throwError "\
@[gcongr] attribute only applies to lemmas proving f x₁ ... xₙ ∼ f x₁' ... xₙ'.\n \
{m} in the conclusion of {declTy}"
let some (relName, lhs, rhs) := getRel (← whnf targetTy) | fail "No relation found"
let some (head, lhsArgs) := getCongrAppFnArgs lhs | fail "LHS is not suitable for congruence"
let some (head', rhsArgs) := getCongrAppFnArgs rhs | fail "RHS is not suitable for congruence"
unless head == head' && lhsArgs.size == rhsArgs.size do
fail "LHS and RHS do not have the same head function and arity"
let mut numVarying := 0
let mut pairs := #[]
for i in [:lhsArgs.size] do
let e1 := lhsArgs[i]!
let e2 := rhsArgs[i]!
let isEq ← isDefEq e1 e2 <||> (isProof e1 <&&> isProof e2)
if !isEq then
let .fvar e1 := e1.eta | fail "Not all arguments are free variables"
let .fvar e2 := e2.eta | fail "Not all arguments are free variables"
pairs := pairs.push (i, e1, e2)
numVarying := numVarying + 1
if numVarying = 0 then
fail "LHS and RHS are the same"
let mut mainSubgoals := #[]
let mut i := 0
for hyp in xs do
mainSubgoals ← forallTelescopeReducing (← inferType hyp) fun args hypTy => do
let hypTy ← whnf hypTy
if let some (_, lhs₁, rhs₁) := getRel hypTy then
if let .fvar lhs₁ := lhs₁.getAppFn then
if let .fvar rhs₁ := rhs₁.getAppFn then
if let some j := pairs.find? fun (_, e1, e2) =>
lhs₁ == e1 && rhs₁ == e2 || lhs₁ == e2 && rhs₁ == e1
then
return mainSubgoals.push (i, j.1, args.size)
else
if let .fvar rhs₁ := hypTy.getAppFn then
if let some lastFVar := args.back? then
if let .fvar lhs₁ := (← inferType lastFVar).getAppFn then
if let some j := pairs.find? fun (_, e1, e2) =>
lhs₁ == e1 && rhs₁ == e2 || lhs₁ == e2 && rhs₁ == e1
then
return mainSubgoals.push (i, j.1, args.size - 1)
return mainSubgoals
i := i + 1
let key := { relName, head, arity := lhsArgs.size }
return { key, declName, mainSubgoals, numHyps, prio, numVarying } | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | makeGCongrLemma | Construct the `GCongrLemma` data from a given lemma. |
gcongrDischarger (goal : MVarId) : MetaM Unit := Elab.Term.TermElabM.run' do
trace[Meta.gcongr] "Attempting to discharge side goal {goal}"
let [] ← Elab.Tactic.run goal <|
Elab.Tactic.evalTactic (Unhygienic.run `(tactic| gcongr_discharger))
| failure
open Elab Tactic | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | gcongrDischarger | Attribute marking "generalized congruence" (`gcongr`) lemmas. Such lemmas must have a
conclusion of a form such as `f x₁ y z₁ ∼ f x₂ y z₂`; that is, a relation between the application of
a function to two argument lists, in which the "varying argument" pairs (here `x₁`/`x₂` and
`z₁`/`z₂`) are all free variables.
The antecedents of such a lemma are classified as generating "main goals" if they are of the form
`x₁ ≈ x₂` for some "varying argument" pair `x₁`/`x₂` (and a possibly different relation `≈` to `∼`),
or more generally of the form `∀ i h h' j h'', f₁ i j ≈ f₂ i j` (say) for some "varying argument"
pair `f₁`/`f₂`. (Other antecedents are considered to generate "side goals".) The index of the
"varying argument" pair corresponding to each "main" antecedent is recorded.
Lemmas involving `<` or `≤` can also be marked `@[bound]` for use in the related `bound` tactic. -/
initialize registerBuiltinAttribute {
name := `gcongr
descr := "generalized congruence"
add := fun decl stx kind ↦ MetaM.run' do
let prio ← getAttrParamOptPrio stx[1]
let declTy := (← getConstInfo decl).type
let arity := declTy.getForallArity
-- We have to determine how many of the hypotheses should be introduced for
-- processing the `gcongr` lemma. This is because of implication lemmas like `Or.imp`,
-- which we treat as having conclusion `a ∨ b → c ∨ d` instead of just `c ∨ d`.
-- Since there is only one possible arity at which the `gcongr` lemma will be accepted,
-- we simply attempt to process the lemmas at the different possible arities.
try
gcongrExt.add (← makeGCongrLemma decl declTy arity prio) kind
catch e => try
guard (1 ≤ arity)
gcongrExt.add (← makeGCongrLemma decl declTy (arity - 1) prio) kind
catch _ => try
-- We need to use `arity - 2` for lemmas such as `imp_imp_imp` and `forall_imp`.
guard (2 ≤ arity)
gcongrExt.add (← makeGCongrLemma decl declTy (arity - 2) prio) kind
catch _ =>
-- If none of the arities work, we throw the error of the first attempt.
throw e
}
initialize registerTraceClass `Meta.gcongr
syntax "gcongr_discharger" : tactic
/--
This is used as the default side-goal discharger,
it calls the `gcongr_discharger` extensible tactic. |
@[gcongr_forward] exactRefl : ForwardExt where
eval h goal := do
let m ← mkFreshExprMVar none
goal.assignIfDefEq (← mkAppOptM ``Eq.subst #[h, m])
goal.applyRfl | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | exactRefl | See if the term is `a = b` and the goal is `a ∼ b` or `b ∼ a`, with `∼` reflexive. |
@[gcongr_forward] symmExact : ForwardExt where
eval h goal := do (← goal.applySymm).assignIfDefEq h
@[gcongr_forward] def exact : ForwardExt where
eval e m := m.assignIfDefEq e | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | symmExact | See if the term is `a ∼ b` with `∼` symmetric and the goal is `b ∼ a`. |
_root_.Lean.MVarId.gcongrForward (hs : Array Expr) (g : MVarId) : MetaM Unit :=
withReducible do
let s ← saveState
withTraceNode `Meta.gcongr (fun _ => return m!"gcongr_forward: ⊢ {← g.getType}") do
let tacs := (forwardExt.getState (← getEnv)).2
for h in hs do
try
tacs.firstM fun (n, tac) =>
withTraceNode `Meta.gcongr (return m!"{·.emoji} trying {n} on {h} : {← inferType h}") do
tac.eval h g
return
catch _ => s.restore
throwError "gcongr_forward failed" | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | _root_.Lean.MVarId.gcongrForward | Attempt to resolve an (implicitly) relational goal by one of a provided list of hypotheses,
either with such a hypothesis directly or by a limited palette of relational forward-reasoning from
these hypotheses. |
gcongrForwardDischarger (goal : MVarId) : MetaM Unit := Elab.Term.TermElabM.run' do
let mut hs := #[]
for h in ← getLCtx do
if !h.isImplementationDetail then
hs := hs.push (.fvar h.fvarId)
goal.gcongrForward hs | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | gcongrForwardDischarger | This is used as the default main-goal discharger,
consisting of running `Lean.MVarId.gcongrForward` (trying a term together with limited
forward-reasoning on that term) on each nontrivial hypothesis. |
containsHole (template : Expr) : MetaM Bool := do
let mctx ← getMCtx
let hasMVar := template.findMVar? fun mvarId =>
if let some mdecl := mctx.findDecl? mvarId then
mdecl.kind matches .syntheticOpaque
else
false
return hasMVar.isSome | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | containsHole | Determine whether `template` contains a `?_`.
This guides the `gcongr` tactic when it is given a template. |
rel_imp_rel (h₁ : r c a) (h₂ : r b d) : r a b → r c d :=
fun h => IsTrans.trans c b d (IsTrans.trans c a b h₁ h) h₂ | lemma | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | rel_imp_rel | null |
relImpRelLemma (arity : Nat) : List GCongrLemma :=
if arity < 2 then [] else [{
declName := ``rel_imp_rel
mainSubgoals := #[(7, arity - 2, 0), (8, arity - 1, 0)]
numHyps := 9
key := default, prio := default, numVarying := default
}] | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | relImpRelLemma | Construct a `GCongrLemma` for `gcongr` goals of the form `a ≺ b → c ≺ d`.
This will be tried if there is no other available `@[gcongr]` lemma.
For example, the relation `a ≡ b [ZMOD n]` has an instance of `IsTrans`, so a congruence of the form
`a ≡ b [ZMOD n] → c ≡ d [ZMOD n]` can be solved with `rel_imp_rel`, `rel_trans` or `rel_trans'`. |
_root_.Lean.MVarId.applyWithArity (mvarId : MVarId) (e : Expr) (arity : Nat)
(cfg : ApplyConfig := {}) (term? : Option MessageData := none) : MetaM (List MVarId) :=
mvarId.withContext do
mvarId.checkNotAssigned `apply
let targetType ← mvarId.getType
let eType ← inferType e
let (newMVars, binderInfos) ← do
let (newMVars, binderInfos, eType) ← forallMetaTelescopeReducing eType arity
if (← isDefEqApply cfg.approx eType targetType) then
pure (newMVars, binderInfos)
else
let conclusionType? ← if arity = 0 then
pure none
else
let (_, _, r) ← forallMetaTelescopeReducing eType arity
pure (some r)
throwApplyError mvarId eType conclusionType? targetType term?
postprocessAppMVars `apply mvarId newMVars binderInfos
cfg.synthAssignedInstances cfg.allowSynthFailures
let e ← instantiateMVars e
mvarId.assign (mkAppN e newMVars)
let newMVars ← newMVars.filterM fun mvar => not <$> mvar.mvarId!.isAssigned
let otherMVarIds ← getMVarsNoDelayed e
let newMVarIds ← reorderGoals newMVars cfg.newGoals
let otherMVarIds := otherMVarIds.filter fun mvarId => !newMVarIds.contains mvarId
let result := newMVarIds ++ otherMVarIds.toList
result.forM (·.headBetaType)
return result | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | _root_.Lean.MVarId.applyWithArity | `Lean.MVarId.applyWithArity` is a copy of `Lean.MVarId.apply`, where the arity of the
applied function is given explicitly instead of being inferred.
TODO: make `Lean.MVarId.apply` take a configuration argument to do this itself |
partial _root_.Lean.MVarId.gcongr
(g : MVarId) (template : Option Expr) (names : List (TSyntax ``binderIdent))
(depth : Nat := 1000000)
(grewriteHole : Option MVarId := none)
(mainGoalDischarger : MVarId → MetaM Unit := gcongrForwardDischarger)
(sideGoalDischarger : MVarId → MetaM Unit := gcongrDischarger) :
MetaM (Bool × List (TSyntax ``binderIdent) × Array MVarId) := g.withContext do
withTraceNode `Meta.gcongr (fun _ => return m!"gcongr: ⊢ {← g.getType}") do
match template with
| none =>
try
(withReducible g.applyRfl) <|> mainGoalDischarger g
return (true, names, #[])
catch _ => pure ()
| some tpl =>
if let .mvar mvarId := tpl.getAppFn then
if let some hole := grewriteHole then
if hole == mvarId then mainGoalDischarger g; return (true, names, #[])
else
if let .syntheticOpaque ← mvarId.getKind then
try mainGoalDischarger g; return (true, names, #[])
catch _ => return (false, names, #[g])
let hasHole ← match grewriteHole with
| none => containsHole tpl
| some hole => pure (tpl.findMVar? (· == hole)).isSome
unless hasHole do
try withDefault g.applyRfl; return (true, names, #[])
catch _ => throwTacticEx `gcongr g m!"\
subgoal {← withReducible g.getType'} is not allowed by the provided pattern \
and is not closed by `rfl`"
match depth with
| 0 => try mainGoalDischarger g; return (true, names, #[]) catch _ => return (false, names, #[g])
| depth + 1 =>
let rel ← withReducible g.getType'
let some (relName, lhs, rhs) := getRel rel | throwTacticEx `gcongr g m!"{rel} is not a relation"
let some (lhsHead, lhsArgs) := getCongrAppFnArgs lhs
| if template.isNone then return (false, names, #[g])
throwTacticEx `gcongr g m!"the head of {lhs} is not a constant"
let some (rhsHead, rhsArgs) := getCongrAppFnArgs rhs
| if template.isNone then return (false, names, #[g])
throwTacticEx `gcongr g m!"the head of {rhs} is not a constant"
let tplArgs ← if let some tpl := template then
let some (tplHead, tplArgs) := getCongrAppFnArgs tpl
| throwTacticEx `gcongr g m!"the head of {tpl} is not a constant"
if grewriteHole.isNone then
unless tplHead == lhsHead && tplArgs.size == lhsArgs.size do
throwError "expected {tplHead}, got {lhsHead}\n{lhs}"
unless tplHead == rhsHead && tplArgs.size == rhsArgs.size do
throwError "expected {tplHead}, got {rhsHead}\n{rhs}"
pure <| tplArgs.map some
else
... | def | Tactic | [
"Lean",
"Batteries.Lean.Except",
"Batteries.Tactic.Exact",
"Mathlib.Lean.Elab.Term",
"Mathlib.Tactic.GCongr.ForwardAttr",
"Mathlib.Order.Defs.Unbundled"
] | Mathlib/Tactic/GCongr/Core.lean | _root_.Lean.MVarId.gcongr | The core of the `gcongr` tactic. Parse a goal into the form `(f _ ... _) ∼ (f _ ... _)`,
look up any relevant `@[gcongr]` lemmas, try to apply them, recursively run the tactic itself on
"main" goals which are generated, and run the discharger on side goals which are generated. If there
is a user-provided template, first check that the template asks us to descend this far into the
match. |
imp_trans (h : a → b) : (b → c) → a → c := fun g ha => g (h ha) | lemma | Tactic | [
"Mathlib.Tactic.GCongr.Core"
] | Mathlib/Tactic/GCongr/CoreAttrs.lean | imp_trans | null |
imp_right_mono (h : a → b → c) : (a → b) → a → c :=
fun h' ha => h ha (h' ha) | lemma | Tactic | [
"Mathlib.Tactic.GCongr.Core"
] | Mathlib/Tactic/GCongr/CoreAttrs.lean | imp_right_mono | null |
and_right_mono (h : a → b → c) : (a ∧ b) → a ∧ c :=
fun ⟨ha, hb⟩ => ⟨ha, h ha hb⟩
attribute [gcongr] mt
Or.imp Or.imp_left Or.imp_right
And.imp And.imp_left GCongr.and_right_mono
imp_imp_imp GCongr.imp_trans GCongr.imp_right_mono
forall_imp Exists.imp
List.Sublist.append List.Sublist.append_left List.Sublist.append_right
List.Sublist.reverse List.drop_sublist_drop_left List.Sublist.drop
List.Perm.append_left List.Perm.append_right List.Perm.append List.Perm.map
Nat.sub_le_sub_left Nat.sub_le_sub_right Nat.sub_lt_sub_left Nat.sub_lt_sub_right | lemma | Tactic | [
"Mathlib.Tactic.GCongr.Core"
] | Mathlib/Tactic/GCongr/CoreAttrs.lean | and_right_mono | null |
ForwardExt where
eval (h : Expr) (goal : MVarId) : MetaM Unit | structure | Tactic | [
"Mathlib.Init",
"Batteries.Tactic.Basic"
] | Mathlib/Tactic/GCongr/ForwardAttr.lean | ForwardExt | An extension for `gcongr_forward`. |
mkForwardExt (n : Name) : ImportM ForwardExt := do
let { env, opts, .. } ← read
IO.ofExcept <| unsafe env.evalConstCheck ForwardExt opts ``ForwardExt n | def | Tactic | [
"Mathlib.Init",
"Batteries.Tactic.Basic"
] | Mathlib/Tactic/GCongr/ForwardAttr.lean | mkForwardExt | Read a `gcongr_forward` extension from a declaration of the right type. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.