fact stringlengths 5 7.52k | type stringclasses 9 values | library stringclasses 2 values | imports listlengths 0 15 | filename stringclasses 121 values | symbolic_name stringlengths 1 53 | docstring stringlengths 6 446 ⌀ |
|---|---|---|---|---|---|---|
rewrite_constraint_4_last (hrw : ∀ x, c1 x → c2 x → c3 x → (c4 x ↔ c4' x)) : ⟨f, [[c1, c2, c3, c4]]⟩ ≼ ⟨f, [[c1, c2, c3, c4']]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_4_last hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_4_last | null |
rewrite_constraint_5 (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → cs x → (c5 x ↔ c5' x)) : ⟨f, [[c1, c2, c3, c4, c5, cs]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5', cs]]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_5 hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_5 | null |
rewrite_constraint_5_last (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → (c5 x ↔ c5' x)) : ⟨f, [[c1, c2, c3, c4, c5]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5']]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_5_last hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_5_last | null |
rewrite_constraint_6 (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → cs x → (c6 x ↔ c6' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, cs]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6', cs]]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_6 hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_6 | null |
rewrite_constraint_6_last (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → (c6 x ↔ c6' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6']]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_6_last hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_6_last | null |
rewrite_constraint_7 (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → c6 x → cs x → (c7 x ↔ c7' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, c7, cs]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6, c7', cs]]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_7 hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_7 | null |
rewrite_constraint_7_last (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → c6 x → (c7 x ↔ c7' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, c7]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6, c7']]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_7_last hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_7_last | null |
rewrite_constraint_8 (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → c6 x → c7 x → cs x → (c8 x ↔ c8' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, cs]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8', cs]]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_8 hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_8 | null |
rewrite_constraint_8_last (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → c6 x → c7 x → (c8 x ↔ c8' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8']]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_8_last hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_8_last | null |
rewrite_constraint_9 (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → c6 x → c7 x → c8 x → cs x → (c9 x ↔ c9' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, c9, cs]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, c9', cs]]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_9 hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_9 | null |
rewrite_constraint_9_last (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → c6 x → c7 x → c8 x → (c9 x ↔ c9' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, c9]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, c9']]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_9_last hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_9_last | null |
rewrite_constraint_10 (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → c6 x → c7 x → c8 x → c9 x → cs x → (c10 x ↔ c10' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, cs]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, c9, c10', cs]]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_10 hrw @[red] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_10 | null |
rewrite_constraint_10_last (hrw : ∀ x, c1 x → c2 x → c3 x → c4 x → c5 x → c6 x → c7 x → c8 x → c9 x → (c10 x ↔ c10' x)) : ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, c9, c10]]⟩ ≼ ⟨f, [[c1, c2, c3, c4, c5, c6, c7, c8, c9, c10']]⟩ := ofEquivalence <| Equivalence.rewrite_constraint_10_last hrw | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | rewrite_constraint_10_last | null |
ofReductions (R₁ : p ≼ q) (R₂ : q ≼ p) : p ≡ q := { phi := R₂.psi, psi := R₁.psi, phi_optimality := R₂.psi_optimality, psi_optimality := R₁.psi_optimality } | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Reduction.lean | ofReductions | null |
Relaxation where phi : D → E phi_feasibility : ∀ x, p.feasible x → q.feasible (phi x) phi_optimality : ∀ x, p.feasible x → q.objFun (phi x) ≤ p.objFun x namespace Relaxation variable {p q r} notation p " ≽' " q => Relaxation p q @[rel] | structure | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | Relaxation | /-- We read `Relaxation p q` as `p` relaxes to `q` or `q` is a relaxation of `p`. -/ |
refl : p ≽' p := { phi := id, phi_feasibility := fun _ h => h, phi_optimality := fun _ _ => le_refl _ } @[rel] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | refl | /-- We read `Relaxation p q` as `p` relaxes to `q` or `q` is a relaxation of `p`. -/ |
trans (Rx₁ : p ≽' q) (Rx₂ : q ≽' r) : p ≽' r := { phi := Rx₂.phi ∘ Rx₁.phi, phi_feasibility := fun x h => Rx₂.phi_feasibility (Rx₁.phi x) (Rx₁.phi_feasibility x h), phi_optimality := fun x hx => -- `h(φ₂(φ₁(x))) ≤ g(φ₁(x))` have h₁ := Rx₂.phi_optimality (Rx₁.phi x) (Rx₁.phi_feasibility x hx) -- `g(φ₁(x)) ≤ f(x)` have h₂ := Rx₁.phi_optimality x hx le_trans h₁ h₂ } | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | trans | /-- We read `Relaxation p q` as `p` relaxes to `q` or `q` is a relaxation of `p`. -/ |
feasible_and_bounded_of_feasible (Rx : p ≽' q) {x : D} (h_feas_x : p.feasible x) : q.feasible (Rx.phi x) ∧ q.objFun (Rx.phi x) ≤ p.objFun x := ⟨Rx.phi_feasibility x h_feas_x, Rx.phi_optimality x h_feas_x⟩ /-- Second property in [RelaxationWiki]. NOTE: This does not use `Rx.phi_optimality`. -/ | lemma | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | feasible_and_bounded_of_feasible | /-- First property in [RelaxationWiki]. -/ |
induces_original_problem_optimality (Rx : p ≽' q) (phi_inv : E → D) (phi_left_inv : Function.LeftInverse Rx.phi phi_inv) (h_objFun : ∀ x, p.feasible x → p.objFun x = q.objFun (Rx.phi x)) {y : E} (h_opt_y : q.optimal y) (h_feas_y : p.feasible (phi_inv y)) : p.optimal (phi_inv y) := by refine ⟨h_feas_y, ?_⟩ intros x h_feas_x rw [h_objFun _ h_feas_y, phi_left_inv] have h_bound := h_opt_y.2 (Rx.phi x) (Rx.phi_feasibility x h_feas_x) rwa [← h_objFun _ h_feas_x] at h_bound | lemma | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | induces_original_problem_optimality | /-- Second property in [RelaxationWiki]. NOTE: This does not use `Rx.phi_optimality`. -/ |
ofStrongEquivalence (E : p ≡' q) : p ≽' q := { phi := E.phi, phi_feasibility := E.phi_feasibility, phi_optimality := E.phi_optimality } | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | ofStrongEquivalence | null |
ofRelaxations (Rx₁ : p ≽' q) (Rx₂ : q ≽' p) : p ≡' q := { phi := Rx₁.phi, psi := Rx₂.phi, phi_feasibility := Rx₁.phi_feasibility, psi_feasibility := Rx₂.phi_feasibility, phi_optimality := Rx₁.phi_optimality, psi_optimality := Rx₂.phi_optimality } | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | ofRelaxations | null |
ofRelaxations (Rx₁ : p ≽' q) (Rx₂ : q ≽' p) : p ≡ q := Equivalence.ofStrongEquivalence (StrongEquivalence.ofRelaxations Rx₁ Rx₂) | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | ofRelaxations | null |
remove_constraint {c cs' : D → Prop} (hcs : ∀ x, cs x ↔ c x ∧ cs' x) : ⟨f, cs⟩ ≽' ⟨f, cs'⟩ := { phi := id, phi_feasibility := fun x h_feas_x => ((hcs x).mp h_feas_x).2, phi_optimality := fun _ _ => le_refl _ } @[rel] | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | remove_constraint | null |
weaken_constraints (cs' : D → Prop) (hcs : ∀ x, cs x → cs' x) : ⟨f, cs⟩ ≽' ⟨f, cs'⟩ := { phi := id, phi_feasibility := fun x h_feas_x => hcs x h_feas_x, phi_optimality := fun _ _ => le_refl _ } | def | CvxLean | [
"import CvxLean.Lib.Equivalence"
] | CvxLean/Lib/Relaxation.lean | weaken_constraints | null |
EquivalenceExpr where domainLHS : Expr domainRHS : Expr codomain : Expr codomainPreorder : Expr lhs : Expr rhs : Expr namespace EquivalenceExpr | structure | CvxLean | [
"import CvxLean.Meta.Minimization",
"import CvxLean.Lib.Equivalence"
] | CvxLean/Meta/Equivalence.lean | EquivalenceExpr | /-- `Equivalence` type components as expressions. -/ |
toMinimizationExprLHS (eqvExpr : EquivalenceExpr) : MetaM MinimizationExpr := do MinimizationExpr.fromExpr (← whnf eqvExpr.lhs) | def | CvxLean | [
"import CvxLean.Meta.Minimization",
"import CvxLean.Lib.Equivalence"
] | CvxLean/Meta/Equivalence.lean | toMinimizationExprLHS | /-- `Equivalence` type components as expressions. -/ |
toMinimizationExprRHS (eqvExpr : EquivalenceExpr) : MetaM MinimizationExpr := do MinimizationExpr.fromExpr (← whnf eqvExpr.rhs) | def | CvxLean | [
"import CvxLean.Meta.Minimization",
"import CvxLean.Lib.Equivalence"
] | CvxLean/Meta/Equivalence.lean | toMinimizationExprRHS | /-- `Equivalence` type components as expressions. -/ |
toExpr (eqvExpr : EquivalenceExpr) : Expr := mkApp6 (mkConst ``Minimization.Equivalence) eqvExpr.domainLHS eqvExpr.domainRHS eqvExpr.codomain eqvExpr.codomainPreorder eqvExpr.lhs eqvExpr.rhs | def | CvxLean | [
"import CvxLean.Meta.Minimization",
"import CvxLean.Lib.Equivalence"
] | CvxLean/Meta/Equivalence.lean | toExpr | /-- `Equivalence` type components as expressions. -/ |
fromExpr (e : Expr) : MetaM EquivalenceExpr := do match e with | .app (.app (.app (.app (.app (.app (.const ``Minimization.Equivalence _) domainLHS) domainRHS) codomain) codomainPreorder) lhs) rhs => do return EquivalenceExpr.mk domainLHS domainRHS codomain codomainPreorder lhs rhs | _ => throwError "Expression not of the form `Minimization.Equivalence ...`." | def | CvxLean | [
"import CvxLean.Meta.Minimization",
"import CvxLean.Lib.Equivalence"
] | CvxLean/Meta/Equivalence.lean | fromExpr | null |
fromGoal (goal : MVarId) : MetaM EquivalenceExpr := do let goalType ← whnf (← MVarId.getDecl goal).type fromExpr goalType | def | CvxLean | [
"import CvxLean.Meta.Minimization",
"import CvxLean.Lib.Equivalence"
] | CvxLean/Meta/Equivalence.lean | fromGoal | null |
MinimizationExpr where domain : Expr codomain : Expr objFun : Expr constraints : Expr namespace MinimizationExpr /-- Build a `Minimization` type from `MinimizationExpr`. -/ | structure | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | MinimizationExpr | /-- Structure holding all the components in the `Minimization` type. -/ |
toExpr (minExpr : MinimizationExpr) : Expr := mkApp4 (mkConst ``Minimization.mk) minExpr.domain minExpr.codomain minExpr.objFun minExpr.constraints /-- Decompose `Minimization` type into its components. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | toExpr | /-- Build a `Minimization` type from `MinimizationExpr`. -/ |
fromExpr (prob : Expr) : MetaM MinimizationExpr := match prob with | .app (.app (.app (.app (.const ``Minimization.mk _) domain) codomain) objFun) constraints => do return MinimizationExpr.mk domain codomain objFun constraints | _ => throwError "Expr not of the form `Minimization.mk ...` {prob}." | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | fromExpr | /-- Decompose `Minimization` type into its components. -/ |
SolutionExpr where domain : Expr codomain : Expr codomainPreorder : Expr p : Expr namespace SolutionExpr /-- Build `MinimizationExpr` from `SolutionExpr`. -/ | structure | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | SolutionExpr | /-- Structure holding all the components in the `Solution` type. -/ |
toMinimizationExpr (solExpr : SolutionExpr) : MetaM MinimizationExpr := MinimizationExpr.fromExpr solExpr.p /-- Build a `Solution` type from `SolutionExpr`. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | toMinimizationExpr | /-- Build `MinimizationExpr` from `SolutionExpr`. -/ |
toExpr (solExpr : SolutionExpr) : Expr := mkApp4 (mkConst ``Minimization.Solution) solExpr.domain solExpr.codomain solExpr.codomainPreorder solExpr.p /-- Decompose `Solution` type into its components. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | toExpr | /-- Build a `Solution` type from `SolutionExpr`. -/ |
fromExpr (goalType : Expr) : MetaM SolutionExpr := do match goalType with | .app (.app (.app (.app (.const ``Minimization.Solution _) domain) codomain) preorder) p => do return SolutionExpr.mk domain codomain preorder p | _ => throwError "Goal not of the form `Minimization.Solution ...`." /-- Applies `SolutionExpr.fromExpr` to goal. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | fromExpr | /-- Decompose `Solution` type into its components. -/ |
fromGoal (goal : MVarId) : MetaM SolutionExpr := do let goalType ← whnf (← MVarId.getDecl goal).type fromExpr goalType | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | fromGoal | /-- Applies `SolutionExpr.fromExpr` to goal. -/ |
replaceProjections (e : Expr) (p : FVarId) (rs : Array Expr) : MetaM Expr := do let pre (e : Expr) : MetaM TransformStep := do match ← decomposeProj e p rs.data with | some r => return TransformStep.done r | none => return TransformStep.continue transform e (pre := pre) where decomposeProj (e : Expr) (p : FVarId) (rs : List Expr) (first : Option Bool := none) : MetaM (Option Expr) := do -- `first` tells us whether the outermost projection was `.1` (`some true`) or -- `.2` (`some false`). If this is not a recursive call, `first` is `none`. -/ -- TODO: This won't recognize "p.2 i" which means that we cannot handle vector or matrix -- applications or functions that depend on these such as sums. match first, e, rs with | _, .fvar fVarId, [r] => if fVarId == p then return r else return none | some true, .fvar fVarId, r :: _ :: _ => if fVarId == p then return r else return none | none, .app (.app (.app (.const ``Prod.fst _) _) _) e, r :: rs => do decomposeProj e p (r :: rs) (first := true) | _, .app (.app (.app (.const ``Prod.snd _) _) _) e, _ :: rs => do decomposeProj e p rs (first := first == some true) | _, _, [] => return none | _, _, _ :: _ => return none /-- Determine a list of variables described by a `domain`. Returns a list of variables, consisting of their name and type. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | replaceProjections | /-- Replaces projections of an FVar `p` in an expression `e` by the expressions `rs`. For example,
`p.2.2.1` will be replaced by `rs[2]`. If `p` is not fully projected, e.g., `p.2` when `rs` has more
than 2 elements, it is not replaced at all. -/ |
decomposeDomain (domain : Expr) : m (List (Name × Expr)) := do match domain with | .app (.app (.const `Prod _) ty1) ty2 => do return (← decomposeLabel ty1) :: (← decomposeDomain ty2) | _ => do return [← decomposeLabel domain] /-- Same as `decomposeDomain` but also try to instantiate meta-variables. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | decomposeDomain | /-- Determine a list of variables described by a `domain`. Returns a list of variables, consisting
of their name and type. -/ |
decomposeDomainInstantiating (minExpr : MinimizationExpr) : MetaM (List (Name × Expr)) := do decomposeDomain (← instantiateMVars minExpr.domain) /-- Determine domain type from a list of variables and their types. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | decomposeDomainInstantiating | /-- Same as `decomposeDomain` but also try to instantiate meta-variables. -/ |
composeDomain (vars : List (Name × Expr)) : Expr := match vars with | [] => mkConst `Unit | [(name, ty)] => mkLabel name ty | (name, ty) :: rest => mkApp2 (mkConst `Prod [levelZero, levelZero]) (mkLabel name ty) (composeDomain rest) /-- Determine a list of variables described by a `domain`. The expression `p` must be a variable of type `domain`. Returns a list of variables, with name, type, and definition in terms of `p`. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | composeDomain | /-- Determine domain type from a list of variables and their types. -/ |
mkProjections (domain : Expr) (p : Expr) : m (List (Name × Expr × Expr)) := do match domain with | .app (.app (.const ``Prod lvls) (ty1 : Expr)) (ty2 : Expr) => do let v ← getLabelName ty1 let d := mkApp3 (mkConst `Prod.fst lvls) ty1 ty2 p let r ← mkProjections ty2 (mkApp3 (mkConst `Prod.snd lvls) ty1 ty2 p) return (v, ty1, d) :: r | _ => do return [(← getLabelName domain, domain, p)] /-- Introduce let declarations into the context, corresponding to the projections of `p`. The argument `domain` specifies the type of `p`. CvxLeanLabels in the `domain` are used to determine the names of the new variables. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | mkProjections | /-- Determine a list of variables described by a `domain`. The expression `p` must be a variable of
type `domain`. Returns a list of variables, with name, type, and definition in terms of `p`. -/ |
withDomainLocalDecls {α} [Inhabited α] (domain : Expr) (p : Expr) (x : Array Expr → Array Expr → m α) : m α := do let pr := (← mkProjections domain p).toArray withLetDecls (pr.map fun (n, ty, val) => (n, fun _ => return ty, fun _ => return val)) fun xs => do let mut xs := xs -- Use projections instead of variables named "_". for i in [:pr.size] do if pr[i]!.1 == `_ then xs := xs.set! i pr[i]!.2.2 x xs (pr.map (fun a => a.2.2)) /-- Decompose an expression into its `And`-connected components. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | withDomainLocalDecls | /-- Introduce let declarations into the context, corresponding to the projections of `p`. The
argument `domain` specifies the type of `p`. CvxLeanLabels in the `domain` are used to determine the
names of the new variables. -/ |
decomposeAnd (e : Expr) : MetaM (List (Expr)) := do match e with | .app (.app (.const ``And _) p) q => do return p :: (← decomposeAnd q) | _ => return [e] /-- Decompose an expression of `And`-connected constraints into a list of names and expressions. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | decomposeAnd | /-- Decompose an expression into its `And`-connected components. -/ |
decomposeConstraints (e : Expr) : MetaM (List (Name × Expr)) := do (← decomposeAnd e).mapM fun e => do return (← getLabelName e, e) /-- Compose a list of expressions with `And`. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | decomposeConstraints | /-- Decompose an expression of `And`-connected constraints into a list of names and expressions. -/ |
composeAnd : List Expr → Expr | [] => mkConst ``True | [c] => c | c :: cs => mkApp2 (mkConst ``And) c (composeAnd cs) /-- Compose a list of expressions with `And`. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | composeAnd | /-- Compose a list of expressions with `And`. -/ |
composeAndWithProj : List Expr → (Expr × (Expr → List Expr)) | [] => (mkConst ``True, fun _ => []) | [c] => (c, fun e => [e]) | c :: cs => let (cs, prs) := composeAndWithProj cs let res := mkApp2 (mkConst ``And) c cs let prs := fun e => mkApp3 (mkConst ``And.left) c cs e :: prs (mkApp3 (mkConst ``And.right) c cs e) (res, prs) /-- Get a HashSet of variable names in a given domain. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | composeAndWithProj | /-- Compose a list of expressions with `And`. -/ |
getVariableNameSet (domain : Expr) : m (HashSet Name) := do let mut res : HashSet Name := {} for (name, _) in ← decomposeDomain domain do res := res.insert name return res /-- Get a `HashSet` of constraint names in a given domain. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | getVariableNameSet | /-- Get a HashSet of variable names in a given domain. -/ |
getConstraintNameSet (e : Expr) : MetaM (HashSet Name) := do let mut res : HashSet Name := {} for (name, _) in ← decomposeConstraints e do res := res.insert name return res /-- Generates a name that is not yet contained in `set`. -/ | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | getConstraintNameSet | /-- Get a `HashSet` of constraint names in a given domain. -/ |
generateNewName (base : String) (set : HashSet Name) : MetaM Name := do tryNumber 1 set where tryNumber (i : Nat) vars : MetaM Name := let name := Name.mkSimple s!"{base}{i}" if vars.contains name then tryNumber (i + 1) vars else return name | def | CvxLean | [
"import CvxLean.Syntax.Label",
"import CvxLean.Meta.Util.Meta",
"import CvxLean.Lib.Minimization"
] | CvxLean/Meta/Minimization.lean | generateNewName | /-- Generates a name that is not yet contained in `set`. -/ |
ReductionExpr where domainLHS : Expr domainRHS : Expr codomain : Expr codomainLHSreorder : Expr lhs : Expr rhs : Expr namespace ReductionExpr | structure | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Reduction"
] | CvxLean/Meta/Reduction.lean | ReductionExpr | /-- `Reduction` type components as expressions. -/ |
toMinimizationExprLHS (redExpr : ReductionExpr) : MetaM MinimizationExpr := MinimizationExpr.fromExpr redExpr.lhs | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Reduction"
] | CvxLean/Meta/Reduction.lean | toMinimizationExprLHS | /-- `Reduction` type components as expressions. -/ |
toMinimizationExprRHS (redExpr : ReductionExpr) : MetaM MinimizationExpr := MinimizationExpr.fromExpr redExpr.rhs | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Reduction"
] | CvxLean/Meta/Reduction.lean | toMinimizationExprRHS | /-- `Reduction` type components as expressions. -/ |
toExpr (redExpr : ReductionExpr) : Expr := mkApp6 (mkConst ``Minimization.Reduction) redExpr.domainLHS redExpr.domainRHS redExpr.codomain redExpr.codomainLHSreorder redExpr.lhs redExpr.rhs | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Reduction"
] | CvxLean/Meta/Reduction.lean | toExpr | /-- `Reduction` type components as expressions. -/ |
fromExpr (e : Expr) : MetaM ReductionExpr := do match e with | .app (.app (.app (.app (.app (.app (.const ``Minimization.Reduction _) domainLHS) domainRHS) codomain) codomainLHSreorder) p) q => do return ReductionExpr.mk domainLHS domainRHS codomain codomainLHSreorder p q | _ => throwError "Expression not of the form `Minimization.Reduction ...`." | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Reduction"
] | CvxLean/Meta/Reduction.lean | fromExpr | null |
fromGoal (goal : MVarId) : MetaM ReductionExpr := do let goalType ← whnf (← MVarId.getDecl goal).type fromExpr goalType | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Reduction"
] | CvxLean/Meta/Reduction.lean | fromGoal | null |
RelaxationExpr where domainP : Expr domainQ : Expr codomain : Expr codomainPreorder : Expr p : Expr q : Expr namespace RelaxationExpr | structure | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Relaxation"
] | CvxLean/Meta/Relaxation.lean | RelaxationExpr | /-- `Relaxation` type components as expressions. -/ |
toMinimizationExprLHS (relExpr : RelaxationExpr) : MetaM MinimizationExpr := MinimizationExpr.fromExpr relExpr.p | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Relaxation"
] | CvxLean/Meta/Relaxation.lean | toMinimizationExprLHS | /-- `Relaxation` type components as expressions. -/ |
toMinimizationExprRHS (relExpr : RelaxationExpr) : MetaM MinimizationExpr := MinimizationExpr.fromExpr relExpr.q | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Relaxation"
] | CvxLean/Meta/Relaxation.lean | toMinimizationExprRHS | /-- `Relaxation` type components as expressions. -/ |
toExpr (relExpr : RelaxationExpr) : Expr := mkApp6 (mkConst ``Minimization.Relaxation) relExpr.domainP relExpr.domainQ relExpr.codomain relExpr.codomainPreorder relExpr.p relExpr.q | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Relaxation"
] | CvxLean/Meta/Relaxation.lean | toExpr | /-- `Relaxation` type components as expressions. -/ |
fromExpr (e : Expr) : MetaM RelaxationExpr := do match e with | .app (.app (.app (.app (.app (.app (.const ``Minimization.Relaxation _) domainP) domainQ) codomain) codomainPreorder) p) q => do return RelaxationExpr.mk domainP domainQ codomain codomainPreorder p q | _ => throwError "Expression not of the form `Minimization.Relaxation ...`." | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Relaxation"
] | CvxLean/Meta/Relaxation.lean | fromExpr | null |
fromGoal (goal : MVarId) : MetaM RelaxationExpr := do let goalType ← whnf (← MVarId.getDecl goal).type fromExpr goalType | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Lib.Relaxation"
] | CvxLean/Meta/Relaxation.lean | fromGoal | null |
TransformationGoal | Solution | Equivalence | Reduction | Relaxation namespace TransformationGoal | inductive | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | TransformationGoal | null |
isTransitive : TransformationGoal → Bool | TransformationGoal.Solution => false | _ => true | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | isTransitive | null |
fromExpr (e : Expr) : MetaM TransformationGoal := do let e ← whnf e if e.isAppOf ``Minimization.Solution then return TransformationGoal.Solution else if e.isAppOf ``Minimization.Equivalence then return TransformationGoal.Equivalence else if e.isAppOf ``Minimization.Reduction then return TransformationGoal.Reduction else if e.isAppOf ``Minimization.Relaxation then return TransformationGoal.Relaxation else trace[Meta.TacticBuilder] "{e.ctorName}" throwTacticBuilderError "expected a `Solution`, `Equivalence`, `Reduction` or `Relaxation` goal, got {e}." /-- Applies appropriate transitivity tactic to the goal. -/ | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | fromExpr | null |
applyTransitivity (transf : TransformationGoal) (g : MVarId) : TacticM (MVarId × MVarId) := g.withContext do if transf.isTransitive then let gsTrans ← match transf with | TransformationGoal.Relaxation => evalTacticAt (← `(tactic| relaxation_trans)) g | TransformationGoal.Reduction => evalTacticAt (← `(tactic| reduction_trans)) g | TransformationGoal.Equivalence => evalTacticAt (← `(tactic| equivalence_trans)) g | _ => pure [] if gsTrans.length != 4 then throwTacticBuilderError "transitivity failed." let mut gToChange := gsTrans[0]! gToChange.setTag Name.anonymous let gNext := gsTrans[1]! gNext.setTag Name.anonymous return (gToChange, gNext) else return (g, g) | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | applyTransitivity | /-- Applies appropriate transitivity tactic to the goal. -/ |
RelaxationBuilder (α) := RelaxationExpr → MVarId → TacticM α namespace RelaxationBuilder | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | RelaxationBuilder | /-- Given a relaxation goal in the form of a `RelaxationExpr` and the `MVarId` of the current goal,
provide a tactic to close it. -/ |
toTactic {α} (builder : RelaxationBuilder α) : TacticM α := withMainContext do let transf ← TransformationGoal.fromExpr (← getMainTarget) -- Apply transitivity. let (gToChange, gNext) ← transf.applyTransitivity <| ← getMainGoal let mut gToChange := gToChange let mut gNext := gNext match transf with | TransformationGoal.Solution => throwTacticBuilderError "relaxation tactic does not apply to `Solution`." | TransformationGoal.Equivalence => do throwTacticBuilderError "relaxation tactic does not apply to `Equivalence`." | TransformationGoal.Reduction => do throwTacticBuilderError "relaxation tactic does not apply to `Reduction`." | TransformationGoal.Relaxation => do pure () -- Run builder. let relExpr ← RelaxationExpr.fromExpr (← gToChange.getType) let res ← builder relExpr gToChange -- Set next goal. gNext.setTag Name.anonymous setGoals [gNext] return res | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | toTactic | /-- Given a relaxation goal in the form of a `RelaxationExpr` and the `MVarId` of the current goal,
provide a tactic to close it. -/ |
ReductionBuilder (α) := ReductionExpr → MVarId → TacticM α namespace ReductionBuilder | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | ReductionBuilder | /-- Given a reduction goal in the form of a `ReductionExpr` and the `MVarId` of the current goal,
provide a tactic to close it. -/ |
toTactic {α} (builder : ReductionBuilder α) : TacticM α := do let transf ← TransformationGoal.fromExpr (← getMainTarget) -- Apply transitivity. let (gToChange, gNext) ← transf.applyTransitivity <| ← getMainGoal let mut gToChange := gToChange let mut gNext := gNext -- Convert `Solution` goal to `Reduction` goal if needed. match transf with | TransformationGoal.Solution => if let [red, sol, _, _] ← gToChange.apply (mkConst ``Minimization.Reduction.toBwd) then -- Set the reduction as the goal to change and set the solution as the next goal. gToChange := red gNext := sol else throwTacticBuilderError "could not apply reduction tactic to `Solution`." | TransformationGoal.Equivalence => do throwTacticBuilderError "expected `Reduction`, found `Equivalence`." | TransformationGoal.Relaxation => do throwTacticBuilderError "expected `Reduction`, found `Relaxation`." | TransformationGoal.Reduction => do pure () -- Run builder. let redExpr ← ReductionExpr.fromExpr (← gToChange.getType) let res ← builder redExpr gToChange -- Set next goal. gNext.instantiateMVars setGoals [gNext] return res | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | toTactic | /-- Given a reduction goal in the form of a `ReductionExpr` and the `MVarId` of the current goal,
provide a tactic to close it. -/ |
EquivalenceBuilder (α) := EquivalenceExpr → MVarId → TacticM α namespace EquivalenceBuilder | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | EquivalenceBuilder | /-- Given an equivalence goal in the form of a `EquivalenceExpr` and the `MVarId` of the current
goal, provide a tactic to close it. -/ |
toTactic {α} (builder : EquivalenceBuilder α) : TacticM α := withMainContext do let transf ← TransformationGoal.fromExpr (← getMainTarget) -- Apply transitivity. let (gToChange, gNext) ← transf.applyTransitivity <| ← getMainGoal let mut gToChange := gToChange let mut gNext := gNext -- Convert reduction to equivalence if needed. match transf with | TransformationGoal.Solution => if let [eqv, sol, _, _] ← gToChange.apply (mkConst ``Minimization.Equivalence.toBwd) then -- Set the equivalence as the goal to change and set the solution as the next goal. gToChange := eqv gNext := sol else throwTacticBuilderError "could not apply equivalence tactic to `Solution`." | TransformationGoal.Equivalence => do pure () | TransformationGoal.Reduction => do if let [eqv] ← gToChange.apply (mkConst ``Minimization.Reduction.ofEquivalence) then gToChange := eqv else throwTacticBuilderError "could not apply equivalence tactic to `Reduction`." | TransformationGoal.Relaxation => do throwTacticBuilderError "equivalence tactic does not apply to `Relaxation`." -- Run builder. let eqvExpr ← EquivalenceExpr.fromExpr (← gToChange.getType) let res ← builder eqvExpr gToChange -- Set next goal. gNext.setTag Name.anonymous setGoals [gNext] return res | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | toTactic | /-- Given an equivalence goal in the form of a `EquivalenceExpr` and the `MVarId` of the current
goal, provide a tactic to close it. -/ |
runTransformationTactic (transf : TransformationGoal) (mvarId : MVarId) (stx : Syntax) : TermElabM Unit := do let tacticStx := ⟨stx[1]⟩ let rflTacticStx ← match transf with | TransformationGoal.Solution => `(tactic| skip) | TransformationGoal.Equivalence => `(tactic| equivalence_rfl) | TransformationGoal.Reduction => `(tactic| reduction_rfl) | TransformationGoal.Relaxation => `(tactic| relaxation_rfl) let code ← `(tactic| $tacticStx <;> $rflTacticStx) instantiateMVarDeclMVars mvarId withInfoHole mvarId do let remainingGoals ← Tactic.run mvarId do withTacticInfoContext stx do evalTactic code match remainingGoals with | [] => pure () | _ => reportUnsolvedGoals remainingGoals synthesizeSyntheticMVars (mayPostpone := false) /-- Wraper that works for all defined transformations, elaborating syntax into the RHS expression and a proof of the relation with the LHS. The RHS can be named so that the metavariable displayed in the infoview corresponds to a user-defined name. -/ | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | runTransformationTactic | null |
elabTransformationProof (transf : TransformationGoal) (lhs : Expr) (rhsName : Name) (stx : Syntax) : TermElabM (Expr × Expr) := do withRef stx do let syntheticMVarsSaved := (← get).syntheticMVars modify fun s => { s with syntheticMVars := {} } try -- Unfold LHS if needed. let lhs := ← do let (n, _) := lhs.getAppFnArgs if n != ``Minimization.mk then let r ← unfold lhs n return r.expr else return lhs -- Build type. let lhsMinExpr ← MinimizationExpr.fromExpr lhs let D := lhsMinExpr.domain let E ← Meta.mkFreshTypeMVar let R := lhsMinExpr.codomain let RPreorder ← Meta.mkFreshExprMVar (mkAppN (Lean.mkConst ``Preorder [levelZero]) #[R]) let rhs ← Meta.mkFreshExprMVar (userName := rhsName) (mkAppN (Lean.mkConst ``Minimization) #[E, R]) let transfTy := match transf with | TransformationGoal.Solution => mkAppN (mkConst ``Minimization.Solution) #[D, E, R, RPreorder, lhs] | TransformationGoal.Equivalence => mkAppN (mkConst ``Minimization.Equivalence) #[D, E, R, RPreorder, lhs, rhs] | TransformationGoal.Reduction => mkAppN (mkConst ``Minimization.Reduction) #[D, E, R, RPreorder, lhs, rhs] | TransformationGoal.Relaxation => mkAppN (mkConst ``Minimization.Relaxation) #[D, E, R, RPreorder, lhs, rhs] -- Proof from `stx`. let proof ← elabTerm stx (some transfTy) let some mvarDecl ← getSyntheticMVarDecl? proof.mvarId! | throwTacticBuilderError "`SyntheticMVarDecl` not found." modify fun s => { s with syntheticMVars := {} } match mvarDecl.kind with | SyntheticMVarKind.tactic tacticCode savedContext => withSavedContext savedContext do runTransformationTactic transf proof.mvarId! tacticCode | _ => throwTacticBuilderError "expected `SyntheticMVarDecl` of kind `tactic`, got {mvarDecl.kind}" return (rhs, ← instantiateMVars proof) finally modify fun s => { s with syntheticMVars := s.syntheticMVars.mergeBy (fun _ _ a => a) syntheticMVarsSaved } | def | CvxLean | [
"import CvxLean.Meta.Equivalence",
"import CvxLean.Meta.Reduction",
"import CvxLean.Meta.Relaxation",
"import CvxLean.Meta.Util.Error"
] | CvxLean/Meta/TacticBuilder.lean | elabTransformationProof | /-- Wraper that works for all defined transformations, elaborating syntax into the RHS expression
and a proof of the relation with the LHS. The RHS can be named so that the metavariable displayed
in the infoview corresponds to a user-defined name. -/ |
mkLabel (name : Name) (e : Expr) : Expr := mkMData (MData.empty.setName `CvxLeanLabel name) e variable {m} [MonadControlT MetaM m] [Monad m] /-- Get the name and expression from metadata labelled with `CvxLeanLabel`. -/ | def | CvxLean | [
"import Lean",
"import CvxLean.Syntax.Options"
] | CvxLean/Syntax/Label.lean | mkLabel | /-- Attach a CvxLean label to an expression. They are used to indicate variable names in a domain
type and names of constraints. -/ |
decomposeLabel (e : Expr) : m (Name × Expr) := do match e with | Expr.mdata m e => match m.get? `CvxLeanLabel with | some (name : Name) => return (name, e) | none => decomposeLabel e | _ => return (`_, e) /-- Like `CvxLean.Meta.decomposeLabel` but only returns the label name. -/ | def | CvxLean | [
"import Lean",
"import CvxLean.Syntax.Options"
] | CvxLean/Syntax/Label.lean | decomposeLabel | /-- Get the name and expression from metadata labelled with `CvxLeanLabel`. -/ |
getLabelName (e : Expr) : m Name := do return (← decomposeLabel e).1 | def | CvxLean | [
"import Lean",
"import CvxLean.Syntax.Options"
] | CvxLean/Syntax/Label.lean | getLabelName | /-- Like `CvxLean.Meta.decomposeLabel` but only returns the label name. -/ |
elabNamedConstraint : Term.TermElab := fun stx expectedType? => do match stx with | `({** $t ** $id **}) => match id.raw with | Syntax.ident _ _ val _ => let e ← Term.elabTerm t expectedType? return Meta.mkLabel val e | _ => throwUnsupportedSyntax | _ => throwUnsupportedSyntax | def | CvxLean | [
"import Lean",
"import CvxLean.Syntax.Options"
] | CvxLean/Syntax/Label.lean | elabNamedConstraint | /-- Notation for attaching a name label to a term. -/ |
delabNamedConstraint : Delab := do failure -- -- Omit delaboration if pretty printing option is disabled. -- if !(pp.labels.get (← getOptions)) then failure -- -- Check if `CvxLeanLabel` metadata is attached to current expression. -- let Expr.mdata m e ← getExpr | unreachable! -- match m.get? `CvxLeanLabel with -- | some (name : Name) => -- let stx ← descend e 0 (do delab) -- let id := mkIdent name -- `({** $stx ** $id**}) -- | none => failure | def | CvxLean | [
"import Lean",
"import CvxLean.Syntax.Options"
] | CvxLean/Syntax/Label.lean | delabNamedConstraint | /-- Display labelled terms using the `{** term ** name **}` syntax. -/ |
maximizeNeg := @Neg.neg namespace Elab open Lean.Elab Lean.Elab.Term Lean.Meta Lean.Parser.Term /-- Helper function for `elabVars`. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | maximizeNeg | /-- This alias for negation is used to mark a minimization problem to be pretty printed as
maximization. -/ |
decomposeBracketedBinder : Syntax → TermElabM (Array (Syntax × Syntax)) := fun stx => match stx[0] with | `(bracketedBinderF|($ids* : $ty)) => return ids.map (·.raw, ty.raw) | `(ident|$id) => return #[(id.raw, (← `(_)).raw)] /-- Get the names and types of the variables after the `optimization` keyword. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | decomposeBracketedBinder | /-- Helper function for `elabVars`. -/ |
elabVars (idents : Array (TSyntax `CvxLean.Parser.minimizationVar)) : TermElabM (Array (Lean.Name × Expr)) := do let idents ← idents.concatMapM decomposeBracketedBinder let idents ← idents.mapM fun (id, ty) => do match id with | Syntax.ident _ _ val _ => return (val, ← Term.elabTerm ty none) | _ => throwParserError "expected identifier got {id}." return idents /-- Extract names of "let" expressions after `with`. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | elabVars | /-- Get the names and types of the variables after the `optimization` keyword. -/ |
preElabLetVars (letVars : Array (TSyntax `CvxLean.Parser.letVar)) : TermElabM (Array (Lean.Name × TSyntax `Lean.Parser.Term.letDecl)) := do letVars.mapM fun stx => match stx with | `(CvxLean.Parser.letVar| with $letD:letDecl) => match letD with | `(letDecl| $id:ident := $_) => return (id.getId, letD) | _ => throwParserError "expected identified let declaration got {letD}." | _ => throwParserError "expected let declaration got {stx}." /-- Extract names and terms of constraints. We do it in two steps so that we can insert lets if needed. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | preElabLetVars | /-- Extract names of "let" expressions after `with`. -/ |
preElabConstraints (constraints : TSyntax `CvxLean.Parser.constraints) : TermElabM (Array (Lean.Name × TSyntax `term)) := do match constraints with | `(CvxLean.Parser.constraints| $constrs*) => do constrs.mapM fun cDecl => match cDecl with | `(CvxLean.Parser.constraint| $id:ident : $c) => return (id.getId, c) | `(CvxLean.Parser.constraint| _ : $c) => return (Name.anonymous, c) | _ => throwParserError "expected constraint got {cDecl}." | _ => throwParserError "expected constraints got {constraints}." macro_rules | `(optimization $idents* $minOrMax:minOrMax $obj) => `(optimization $idents* $minOrMax:minOrMax $obj subject to _ : True) -- TODO: allow dependently typed variables? /-- Collect all identifiers that appear in some syntax. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | preElabConstraints | /-- Extract names and terms of constraints. We do it in two steps so that we can insert lets if
needed. -/ |
_root_.Lean.Syntax.gatherIdents : Syntax → Array Lean.Name | .missing => #[] | .ident _ _ n _ => #[n] | .atom _ _ => #[] | .node _ _ stxs => stxs.foldl (init := #[]) fun acc stx => acc ++ stx.gatherIdents /-- Elaborate `optimization` problem syntax, building a term of type `Minimization D R`. -/ @[term_elab «optimization»] def elabOptmiziation : Term.TermElab := fun stx _expectedType? => do match stx with | `(optimization $idents* $lets:letVar* $minOrMax:minOrMax $obj subject to $constraints) => -- Determine names and types of the variables. let vars ← elabVars idents -- Construct domain type. let domain := Meta.composeDomain vars.data -- Construct let vars syntax. let letsStx ← preElabLetVars lets -- Introduce FVar for the domain. withLocalDeclD `p domain fun p => do -- Introduce FVars for the variables Meta.withDomainLocalDecls domain p fun xs prs => do -- Elaborate objFun. let mut objStx := obj let objIdents := Syntax.gatherIdents objStx if letsStx.size > 0 then for (letVar, letD) in letsStx do if letVar ∈ objIdents then objStx := ← `(let $letD:letDecl; $objStx) let mut obj := Expr.replaceFVars (← Term.elabTerm objStx.raw none) xs prs -- Add `maximizeNeg` constant to mark maximization problems and to negate the objective. let minOrMaxStx := minOrMax.raw[0]! if minOrMaxStx.isOfKind `maximize then obj ← mkAppM ``maximizeNeg #[obj] else if !(minOrMaxStx.isOfKind `minimize) then throwParserError "expected minimize or maximize, got: {minOrMaxStx.getKind}" obj ← mkLambdaFVars #[p] obj -- Elaborate constraints. let constraints ← preElabConstraints constraints let constraints ← constraints.mapM fun (n, cStx) => do let mut cStx := cStx let cIdents := Syntax.gatherIdents cStx if letsStx.size > 0 then for (letVar, letD) in letsStx do if letVar ∈ cIdents then cStx := ← `(let $letD:letDecl; $cStx) return Meta.mkLabel n (← Term.elabTerm cStx none) let constraints ← mkLambdaFVars #[p] $ Expr.replaceFVars (Meta.composeAnd constraints.data) xs prs -- Put it all together. let res ← mkAppM ``Minimization.mk #[obj, constraints] check res return ← instantiateMVars res | _ => throwUnsupportedSyntax | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | _root_.Lean.Syntax.gatherIdents | /-- Collect all identifiers that appear in some syntax. -/ |
delabVar (e : Expr) : DelabM (Lean.Name × Term) := do match e with | Expr.mdata m e => match m.get? `CvxLeanLabel with | some (name : Lean.Name) => return (name, ← descend e 0 do delab) | none => Alternative.failure | _ => return (`_, ← delab) /-- Show labels (variable names) and terms (types) in a domain type. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | delabVar | /-- Show label in an expression. -/ |
delabDomain : DelabM (List (Lean.Name × Term)) := do let e ← getExpr match e with | Expr.app (Expr.app (Expr.const `Prod _) _ty1) _ty2 => do let stx1 ← withNaryArg 0 (do delabVar <| e.getArg! 0) let stx2 ← withNaryArg 1 (do delabDomain) return stx1 :: stx2 | _ => return [← delabVar e] /-- Show constraint with label if it has one. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | delabDomain | /-- Show labels (variable names) and terms (types) in a domain type. -/ |
delabConstraint : DelabM (TSyntax ``Parser.constraint) := do match ← getExpr with | Expr.mdata m e => match m.get? `CvxLeanLabel with | some (name : Lean.Name) => return mkNode ``Parser.constraint #[(mkIdent name).raw, mkAtom ":", (← descend e 0 do delab).raw] | none => Alternative.failure | _ => return (← `(Parser.constraint|_ : $(← delab))) /-- Show all constraints with their labels. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | delabConstraint | /-- Show constraint with label if it has one. -/ |
delabConstraints : DelabM (List (TSyntax ``Parser.constraint)) := do let e ← getExpr match e with | Expr.app (Expr.app (Expr.const `And _) _l) _r => let l : TSyntax _ ← withNaryArg 0 delabConstraint let r : List (TSyntax _) ← withNaryArg 1 delabConstraints return l :: r | _ => return [← delabConstraint] /-- Delaborate with variable names replaced. -/ | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | delabConstraints | /-- Show all constraints with their labels. -/ |
withDomainBinding {α} [Inhabited α] (domain : Expr) (x : DelabM α) : DelabM α := do guard (← getExpr).isLambda withBindingBody'' `p fun p => do withDomainLocalDecls domain p fun xs _prs => do let e ← getExpr let e ← replaceProjections e p.fvarId! xs withExpr e do x /-- Show minimization problem using the custom syntax, with variable names and constraitn tags. -/ @[delab app] | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | withDomainBinding | /-- Delaborate with variable names replaced. -/ |
delabMinimization : Delab := do --if !(pp.opt.get (← getOptions)) then Alternative.failure match ← getExpr with | .app (.app (.app (.app (.const `Minimization.mk _) domain) _codomain) _objFun) constraints => let idents ← withType <| withNaryArg 0 do let tys ← delabDomain let tys ← tys.mapM fun (name, stx) => do `(Parser.minimizationVar| ($(mkIdent name) : $stx)) return tys.toArray let (objFun, isMax) ← withNaryArg 2 do withDomainBinding domain do match ← getExpr with | .app (.app (.app (.const ``maximizeNeg _) _) _) e => withExpr e do return (← delab, true) | _ => return (← delab, false) let noConstrs ← withLambdaBody constraints fun _ constrsBody => do isDefEq constrsBody (mkConst ``True) let constraints := ← withNaryArg 3 do let cs ← withDomainBinding domain delabConstraints return mkNode ``Parser.constraints #[mkNullNode <| cs.toArray.map (·.raw)] if noConstrs then if isMax then `(optimization $idents* maximize $objFun) else `(optimization $idents* minimize $objFun) else if isMax then `(optimization $idents* maximize $objFun subject to $constraints) else `(optimization $idents* minimize $objFun subject to $constraints) | _ => Alternative.failure | def | CvxLean | [
"import CvxLean.Lib.Minimization",
"import CvxLean.Meta.Util.SubExpr",
"import CvxLean.Meta.Util.Error",
"import CvxLean.Meta.Minimization",
"import CvxLean.Syntax.Parser"
] | CvxLean/Syntax/Minimization.lean | delabMinimization | /-- Show minimization problem using the custom syntax, with variable names and constraitn tags. -/ |
isOptimizationParam (n : Name) : MetaM Bool := do return optimizationParamAttr.hasTag (← getEnv) n | def | CvxLean | [
"import Lean",
"import Lean.Environment"
] | CvxLean/Syntax/OptimizationParam.lean | isOptimizationParam | null |
getAllOptimizationParams : MetaM (List Name) := do return (optimizationParamAttr.ext.getState (← getEnv)).toList | def | CvxLean | [
"import Lean",
"import Lean.Environment"
] | CvxLean/Syntax/OptimizationParam.lean | getAllOptimizationParams | null |
getOptimizationParamExpr (n : Name) (e : Expr) : MetaM Expr := do match (← getEnv).constants.find! n with | ConstantInfo.defnInfo defn => return defn.value | _ => return e | def | CvxLean | [
"import Lean",
"import Lean.Environment"
] | CvxLean/Syntax/OptimizationParam.lean | getOptimizationParamExpr | null |
minimizationVar : Parser := leading_parser ((ident <|> Term.bracketedBinder) >> ppSpace) | def | CvxLean | [
"import Lean"
] | CvxLean/Syntax/Parser.lean | minimizationVar | null |
letVar : Parser := leading_parser "with " >> Term.letDecl >> ppSpace | def | CvxLean | [
"import Lean"
] | CvxLean/Syntax/Parser.lean | letVar | null |
constraint : Parser := leading_parser (ppLine >> checkColGe >> ppGroup (Term.binderIdent >> " : " >> termParser)) | def | CvxLean | [
"import Lean"
] | CvxLean/Syntax/Parser.lean | constraint | null |
constraints : Parser := leading_parser manyIndent constraint | def | CvxLean | [
"import Lean"
] | CvxLean/Syntax/Parser.lean | constraints | null |
minOrMax := leading_parser "minimize " <|> "maximize " | def | CvxLean | [
"import Lean"
] | CvxLean/Syntax/Parser.lean | minOrMax | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.