fact
stringlengths
10
4.79k
type
stringclasses
9 values
library
stringclasses
44 values
imports
listlengths
0
13
filename
stringclasses
718 values
symbolic_name
stringlengths
1
76
docstring
stringlengths
10
64.6k
@[inline] withPtrEqDecEq {α : Type u} (a b : α) (k : Unit → Decidable (a = b)) : Decidable (a = b) := let b := withPtrEq a b (fun _ => toBoolUsing (k ())) (toBoolUsing_eq_true (k ())); match h:b with | true => isTrue (of_toBoolUsing_eq_true h) | false => isFalse (of_toBoolUsing_eq_false h) @[implemented_by wi...
def
Init.Util
[ "Init.Data.ToString.Basic" ]
Init/Util.lean
withPtrEqDecEq
`withPtrEq` for `DecidableEq`
withPtrAddr {α : Type u} {β : Type v} (a : α) (k : USize → β) (h : ∀ u₁ u₂, k u₁ = k u₂) : β := k 0
def
Init.Util
[ "Init.Data.ToString.Basic" ]
Init/Util.lean
withPtrAddr
null
Acc {α : Sort u} (r : α → α → Prop) : α → Prop where /-- A value is accessible if for all `y` such that `r y x`, `y` is also accessible. Note that if there exists no `y` such that `r y x`, then `x` is accessible. Such an `x` is called a _base case_. -/ | intro (x : α) (h : (y : α) → r y x → Acc r y) : Acc r...
inductive
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Acc
`Acc` is the accessibility predicate. Given some relation `r` (e.g. `<`) and a value `x`, `Acc r x` means that `x` is accessible through `r`: `x` is accessible if there exists no infinite sequence `... < y₂ < y₁ < y₀ < x`.
noncomputable Acc.ndrec.{u1, u2} {α : Sort u2} {r : α → α → Prop} {C : α → Sort u1} (m : (x : α) → ((y : α) → r y x → Acc r y) → ((y : α) → (a : r y x) → C y) → C x) {a : α} (n : Acc r a) : C a := n.rec m
abbrev
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Acc.ndrec.
null
noncomputable Acc.ndrecOn.{u1, u2} {α : Sort u2} {r : α → α → Prop} {C : α → Sort u1} {a : α} (n : Acc r a) (m : (x : α) → ((y : α) → r y x → Acc r y) → ((y : α) → (a : r y x) → C y) → C x) : C a := n.rec m
abbrev
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Acc.ndrecOn.
null
inv {x y : α} (h₁ : Acc r x) (h₂ : r y x) : Acc r y := h₁.recOn (fun _ ac₁ _ h₂ => ac₁ y h₂) h₂
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
inv
null
WellFounded {α : Sort u} (r : α → α → Prop) : Prop where /-- If all elements are accessible via `r`, then `r` is well-founded. -/ | intro (h : ∀ a, Acc r a) : WellFounded r
inductive
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
WellFounded
A relation `r` is `WellFounded` if all elements of `α` are accessible within `r`. If a relation is `WellFounded`, it does not allow for an infinite descent along the relation. If the arguments of the recursive calls in a function definition decrease according to a well founded relation, then the function terminates. W...
WellFoundedRelation (α : Sort u) where /-- A well-founded relation on `α`. -/ rel : α → α → Prop /-- A proof that `rel` is, in fact, well-founded. -/ wf : WellFounded rel
class
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
WellFoundedRelation
A type that has a standard well-founded relation. Instances are used to prove that functions terminate using well-founded recursion by showing that recursive calls reduce some measure according to a well-founded relation. This relation can combine well-founded relations on the recursive function's parameters.
apply {α : Sort u} {r : α → α → Prop} (wf : WellFounded r) (a : α) : Acc r a := wf.rec (fun p => p) a
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
apply
null
noncomputable recursion {C : α → Sort v} (a : α) (h : ∀ x, (∀ y, r y x → C y) → C x) : C a := by induction (apply hwf a) with | intro x₁ _ ih => exact h x₁ ih include hwf in
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
recursion
null
induction {C : α → Prop} (a : α) (h : ∀ x, (∀ y, r y x → C y) → C x) : C a := recursion hwf a h variable {C : α → Sort v} variable (F : ∀ x, (∀ y, r y x → C y) → C x)
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
induction
null
noncomputable fixF (x : α) (a : Acc r x) : C x := by induction a with | intro x₁ _ ih => exact F x₁ ih
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
fixF
null
fixF_eq (x : α) (acx : Acc r x) : fixF F x acx = F x (fun (y : α) (p : r y x) => fixF F y (Acc.inv acx p)) := by induction acx with | intro x r _ => exact rfl
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
fixF_eq
null
wrap {α : Sort u} {r : α → α → Prop} (h : WellFounded r) (x : α) : {x : α // Acc r x} := ⟨_, h.apply x⟩ @[simp]
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
wrap
Attaches to `x` the proof that `x` is accessible in the given well-founded relation. This can be used in recursive function definitions to explicitly use a different relation than the one inferred by default: ``` def otherWF : WellFounded Nat := … def foo (n : Nat) := … termination_by otherWF.wrap n ```
val_wrap {α : Sort u} {r : α → α → Prop} (h : WellFounded r) (x : α) : (h.wrap x).val = x := (rfl)
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
val_wrap
null
@[reducible] invImage (f : α → β) (h : WellFoundedRelation β) : WellFoundedRelation α where rel := InvImage h.rel f wf := InvImage.wf f h.wf -- The transitive closure of a well-founded relation is well-founded open Relation
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
invImage
A well-founded fixpoint. If satisfying the motive `C` for all values that are smaller according to a well-founded relation allows it to be satisfied for the current value, then it is satisfied for all values. This function is used as part of the elaboration of well-founded recursion. -/ -- Well-founded fixpoint noncom...
Acc.transGen (h : Acc r a) : Acc (TransGen r) a := by induction h with | intro x _ H => refine Acc.intro x fun y hy ↦ ?_ cases hy with | single hyx => exact H y hyx | tail hyz hzx => exact (H _ hzx).inv hyz
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Acc.transGen
null
acc_transGen_iff : Acc (TransGen r) a ↔ Acc r a := ⟨Subrelation.accessible TransGen.single, Acc.transGen⟩
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
acc_transGen_iff
null
WellFounded.transGen (h : WellFounded r) : WellFounded (TransGen r) := ⟨fun a ↦ (h.apply a).transGen⟩
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
WellFounded.transGen
null
lt_wfRel : WellFoundedRelation Nat where rel := (· < ·) wf := by apply WellFounded.intro intro n induction n with | zero => apply Acc.intro 0 intro _ h apply absurd h (Nat.not_lt_zero _) | succ n ih => apply Acc.intro (Nat.succ n) intro m h have : m = n ...
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
lt_wfRel
null
@[elab_as_elim] protected noncomputable strongRecOn {motive : Nat → Sort u} (n : Nat) (ind : ∀ n, (∀ m, m < n → motive m) → motive n) : motive n := Nat.lt_wfRel.wf.fix ind n
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
strongRecOn
Strong induction on the natural numbers. The induction hypothesis is that all numbers less than a given number satisfy the motive, which should be demonstrated for the given number.
@[elab_as_elim] protected noncomputable caseStrongRecOn {motive : Nat → Sort u} (a : Nat) (zero : motive 0) (ind : ∀ n, (∀ m, m ≤ n → motive m) → motive (succ n)) : motive a := Nat.strongRecOn a fun n => match n with | 0 => fun _ => zero | n+1 => fun h₁ => ind n (λ _ h₂ => h₁ _ (lt_succ...
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
caseStrongRecOn
Case analysis based on strong induction for the natural numbers.
measure {α : Sort u} (f : α → Nat) : WellFoundedRelation α := invImage f Nat.lt_wfRel
abbrev
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
measure
null
sizeOfWFRel {α : Sort u} [SizeOf α] : WellFoundedRelation α := measure sizeOf attribute [instance low] sizeOfWFRel
abbrev
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
sizeOfWFRel
null
protected Lex : α × β → α × β → Prop where /-- If the first projections of two pairs are ordered, then they are lexicographically ordered. -/ | left {a₁} (b₁) {a₂} (b₂) (h : ra a₁ a₂) : Prod.Lex (a₁, b₁) (a₂, b₂) /-- If the first projections of two pairs are equal, then they are lexicographically ordered i...
inductive
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Lex
A lexicographical order based on the orders `ra` and `rb` for the elements of pairs.
lex_def {r : α → α → Prop} {s : β → β → Prop} {p q : α × β} : Prod.Lex r s p q ↔ r p.1 q.1 ∨ p.1 = q.1 ∧ s p.2 q.2 := ⟨fun h => by cases h <;> simp [*], fun h => match p, q, h with | _, _, Or.inl h => Lex.left _ _ h | (_, _), (_, _), Or.inr ⟨e, h⟩ => by subst e; exact Lex.right _ h⟩
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
lex_def
null
right' {a₁ : Nat} {b₁ : β} (h₁ : a₁ ≤ a₂) (h₂ : rb b₁ b₂) : Prod.Lex Nat.lt rb (a₁, b₁) (a₂, b₂) := match Nat.eq_or_lt_of_le h₁ with | Or.inl h => h ▸ Prod.Lex.right a₁ h₂ | Or.inr h => Prod.Lex.left b₁ _ h
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
right'
null
RProd : α × β → α × β → Prop where | intro {a₁ b₁ a₂ b₂} (h₁ : ra a₁ a₂) (h₂ : rb b₁ b₂) : RProd (a₁, b₁) (a₂, b₂)
inductive
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
RProd
null
lexAccessible {a : α} (aca : Acc ra a) (acb : (b : β) → Acc rb b) (b : β) : Acc (Prod.Lex ra rb) (a, b) := by induction aca generalizing b with | intro xa _ iha => induction (acb b) with | intro xb _ ihb => apply Acc.intro (xa, xb) intro p lt cases lt with | left _ _ h => apply iha ...
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
lexAccessible
null
RProdSubLex (a : α × β) (b : α × β) (h : RProd ra rb a b) : Prod.Lex ra rb a b := by cases h with | intro h₁ h₂ => exact Prod.Lex.left _ _ h₁ -- The relational product of well founded relations is well-founded
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
RProdSubLex
null
rprod (ha : WellFoundedRelation α) (hb : WellFoundedRelation β) : WellFoundedRelation (α × β) where rel := RProd ha.rel hb.rel wf := by apply Subrelation.wf (r := Prod.Lex ha.rel hb.rel) (h₂ := (lex ha hb).wf) intro a b h exact RProdSubLex a b h
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
rprod
null
Lex : PSigma β → PSigma β → Prop where | left : ∀ {a₁ : α} (b₁ : β a₁) {a₂ : α} (b₂ : β a₂), r a₁ a₂ → Lex ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ | right : ∀ (a : α) {b₁ b₂ : β a}, s a b₁ b₂ → Lex ⟨a, b₁⟩ ⟨a, b₂⟩
inductive
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Lex
null
lexAccessible {a} (aca : Acc r a) (acb : (a : α) → WellFounded (s a)) (b : β a) : Acc (Lex r s) ⟨a, b⟩ := by induction aca with | intro xa _ iha => induction (WellFounded.apply (acb xa) b) with | intro xb _ ihb => apply Acc.intro intro p lt cases lt with | left => apply iha; assumpt...
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
lexAccessible
null
lexNdep (r : α → α → Prop) (s : β → β → Prop) := Lex r (fun _ => s)
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
lexNdep
null
lexNdepWf {r : α → α → Prop} {s : β → β → Prop} (ha : WellFounded r) (hb : WellFounded s) : WellFounded (lexNdep r s) := WellFounded.intro fun ⟨a, b⟩ => lexAccessible (WellFounded.apply ha a) (fun _ => hb) b
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
lexNdepWf
null
RevLex (r : α → α → Prop) (s : β → β → Prop) : @PSigma α (fun _ => β) → @PSigma α (fun _ => β) → Prop where | left : {a₁ a₂ : α} → (b : β) → r a₁ a₂ → RevLex r s ⟨a₁, b⟩ ⟨a₂, b⟩ | right : (a₁ : α) → {b₁ : β} → (a₂ : α) → {b₂ : β} → s b₁ b₂ → RevLex r s ⟨a₁, b₁⟩ ⟨a₂, b₂⟩
inductive
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
RevLex
null
revLexAccessible {b} (acb : Acc s b) (aca : (a : α) → Acc r a): (a : α) → Acc (RevLex r s) ⟨a, b⟩ := by induction acb with | intro xb _ ihb => intro a induction (aca a) with | intro xa _ iha => apply Acc.intro intro p lt cases lt with | left => apply iha; assumption | righ...
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
revLexAccessible
null
revLex (ha : WellFounded r) (hb : WellFounded s) : WellFounded (RevLex r s) := WellFounded.intro fun ⟨a, b⟩ => revLexAccessible (apply hb b) (WellFounded.apply ha) a
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
revLex
null
SkipLeft (α : Type u) {β : Type v} (s : β → β → Prop) : @PSigma α (fun _ => β) → @PSigma α (fun _ => β) → Prop := RevLex emptyRelation s
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
SkipLeft
null
skipLeft (α : Type u) {β : Type v} (hb : WellFoundedRelation β) : WellFoundedRelation (PSigma fun _ : α => β) where rel := SkipLeft α hb.rel wf := revLex emptyWf.wf hb.wf
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
skipLeft
null
mkSkipLeft {α : Type u} {β : Type v} {b₁ b₂ : β} {s : β → β → Prop} (a₁ a₂ : α) (h : s b₁ b₂) : SkipLeft α s ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ := RevLex.right _ _ h
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
mkSkipLeft
null
Nat.eager (n : Nat) : Nat := if Nat.beq n n = true then n else n
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Nat.eager
Helper gadget that prevents reduction of `Nat.eager n` unless `n` evaluates to a ground term.
Nat.eager_eq (n : Nat) : Nat.eager n = n := ite_self n
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Nat.eager_eq
null
Nat.fix : (x : α) → motive x := let rec go : ∀ (fuel : Nat) (x : α), (h x < fuel) → motive x := Nat.rec (fun _ hfuel => (Nat.not_succ_le_zero _ hfuel).elim) (fun _ ih x hfuel => F x (fun y hy => ih y (Nat.lt_of_lt_of_le hy (Nat.le_of_lt_add_one hfuel)))) fun x => go (Nat.eager (h x + 1)) x (Nat.eage...
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Nat.fix
A well-founded fixpoint operator specialized for `Nat`-valued measures. Given a measure `h`, it expects its higher order function argument `F` to invoke its argument only on values `y` that are smaller than `x` with regard to `h`. In contrast to `WellFounded.fix`, this fixpoint operator reduces on closed terms. (More ...
protected Nat.fix.go_congr (x : α) (fuel₁ fuel₂ : Nat) (h₁ : h x < fuel₁) (h₂ : h x < fuel₂) : Nat.fix.go h F fuel₁ x h₁ = Nat.fix.go h F fuel₂ x h₂ := by induction fuel₁ generalizing x fuel₂ with | zero => contradiction | succ fuel₁ ih => cases fuel₂ with | zero => contradiction | succ fuel₂ => ...
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Nat.fix.go_congr
null
Nat.fix_eq (x : α) : Nat.fix h F x = F x (fun y _ => Nat.fix h F y) := by unfold Nat.fix simp [Nat.eager_eq] exact congrArg (F x) (funext fun _ => funext fun _ => Nat.fix.go_congr ..)
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
Nat.fix_eq
null
wfParam {α : Sort u} (a : α) : α := a
def
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
wfParam
The `wfParam` gadget is used internally during the construction of recursive functions by wellfounded recursion, to keep track of the parameter for which the automatic introduction of `List.attach` (or similar) is plausible.
@[wf_preprocess] ite_eq_dite [Decidable P] : ite P a b = (dite P (fun h => binderNameHint h () a) (fun h => binderNameHint h () b)) := rfl
theorem
Init.WF
[ "Init.BinderNameHint", "Init.Data.Nat.Basic" ]
Init/WF.lean
ite_eq_dite
Reverse direction of `dite_eq_ite`. Used by the well-founded definition preprocessor to extend the context of a termination proof inside `if-then-else` with the condition.
log2p1 : Nat → Nat := WellFounded.fix Nat.lt_wfRel.2 fun n IH => let m := n / 2 if h : m < n then IH m h + 1 else 0 ``` -/
def
Init.WFComputable
[ "Init.WF", "Init.NotationExtra" ]
Init/WFComputable.lean
log2p1
null
public wfRel {r : α → α → Prop} : WellFoundedRelation { val // Acc r val } where rel := InvImage r (·.1) wf := ⟨fun ac => InvImage.accessible _ ac.2⟩
instance
Init.WFComputable
[ "Init.WF", "Init.NotationExtra" ]
Init/WFComputable.lean
wfRel
null
@[specialize, elab_as_elim] public recC {motive : (a : α) → Acc r a → Sort v} (intro : (x : α) → (h : ∀ (y : α), r y x → Acc r y) → ((y : α) → (hr : r y x) → motive y (h y hr)) → motive x (intro x h)) {a : α} (t : Acc r a) : motive a t := intro a (fun _ h => t.inv h) (fun _ hr => recC intro (t.inv hr)) t...
def
Init.WFComputable
[ "Init.WF", "Init.NotationExtra" ]
Init/WFComputable.lean
recC
A computable version of `Acc.rec`.
@[inline] public ndrecC {C : α → Sort v} (m : (x : α) → ((y : α) → r y x → Acc r y) → ((y : α) → (a : r y x) → C y) → C x) {a : α} (n : Acc r a) : C a := n.recC m @[csimp] public theorem ndrec_eq_ndrecC : @Acc.ndrec = @Acc.ndrecC := by funext α r motive intro a t rw [Acc.ndrec, rec_eq_recC, Acc.ndrecC]
abbrev
Init.WFComputable
[ "Init.WF", "Init.NotationExtra" ]
Init/WFComputable.lean
ndrecC
A computable version of `Acc.ndrec`.
@[inline] public ndrecOnC {C : α → Sort v} {a : α} (n : Acc r a) (m : (x : α) → ((y : α) → r y x → Acc r y) → ((y : α) → r y x → C y) → C x) : C a := n.recC m @[csimp] public theorem ndrecOn_eq_ndrecOnC : @Acc.ndrecOn = @Acc.ndrecOnC := by funext α r motive intro a t rw [Acc.ndrecOn, rec_eq_recC, Acc.ndrecOn...
abbrev
Init.WFComputable
[ "Init.WF", "Init.NotationExtra" ]
Init/WFComputable.lean
ndrecOnC
A computable version of `Acc.ndrecOn`.
@[inline] public fixFC {α : Sort u} {r : α → α → Prop} {C : α → Sort v} (F : ∀ x, (∀ y, r y x → C y) → C x) (x : α) (a : Acc r x) : C x := by induction a using Acc.recC with | intro x₁ _ ih => exact F x₁ ih @[csimp] public theorem fixF_eq_fixFC : @fixF = @fixFC := by funext α r C F x a rw [fixF, Acc.rec_eq...
def
Init.WFComputable
[ "Init.WF", "Init.NotationExtra" ]
Init/WFComputable.lean
fixFC
A computable version of `WellFounded.fixF`.
@[specialize] public fixC {α : Sort u} {C : α → Sort v} {r : α → α → Prop} (hwf : WellFounded r) (F : ∀ x, (∀ y, r y x → C y) → C x) (x : α) : C x := F x (fun y _ => fixC hwf F y) termination_by hwf.wrap x unseal fixC @[csimp] public theorem fix_eq_fixC : @fix = @fixC := by rfl
def
Init.WFComputable
[ "Init.WF", "Init.NotationExtra" ]
Init/WFComputable.lean
fixC
A computable version of `fix`.
guaranteeing that it is equal to {lean}`WellFounded.fix`. -/ variable {α : Sort _} {β : α → Sort _} {γ : (a : α) → β a → Sort _} {C : α → Sort _} {C₂ : (a : α) → β a → Sort _} {C₃ : (a : α) → (b : β a) → γ a b → Sort _} set_option doc.verso true
theorem
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
guaranteeing
null
@[specialize] partial opaqueFix [∀ a, Nonempty (C a)] (R : α → α → Prop) (F : ∀ a, (∀ a', R a' a → C a') → C a) (a : α) : C a := F a (fun a _ => opaqueFix R F a) /- SAFE assuming that the code generated by iteration over `F` is equivalent to the well-founded fixpoint of `F` if `R` is well-founded. -/
def
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
opaqueFix
The function implemented as the loop {lean}`opaqueFix R F a = F a (fun a _ => opaqueFix R F a)`. {lean}`opaqueFix R F` is the fixpoint of the functional {name}`F`, as long as the loop is well-founded. The loop might run forever depending on {name}`F`. It is opaque, i.e., it is impossible to prove nontrivial properties...
@[implemented_by opaqueFix] public extrinsicFix [∀ a, Nonempty (C a)] (R : α → α → Prop) (F : ∀ a, (∀ a', R a' a → C a') → C a) (a : α) : C a := open scoped Classical in if h : WellFounded R then h.fix F a else -- Return `opaqueFix R F a` so that `implemented_by opaqueFix` is sound. -- In effect, ...
def
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix
A fixpoint combinator that can be used to construct recursive functions with an *extrinsic* proof of termination. Given a relation {name}`R` and a fixpoint functional {name}`F` which must be decreasing with respect to {name}`R`, {lean}`extrinsicFix R F` is the recursive function obtained by having {name}`F` call itsel...
public extrinsicFix_eq_fix [∀ a, Nonempty (C a)] {R : α → α → Prop} {F : ∀ a, (∀ a', R a' a → C a') → C a} (wf : WellFounded R) {a : α} : extrinsicFix R F a = wf.fix F a := by simp only [extrinsicFix, dif_pos wf]
theorem
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix_eq_fix
null
public extrinsicFix_eq_apply [∀ a, Nonempty (C a)] {R : α → α → Prop} {F : ∀ a, (∀ a', R a' a → C a') → C a} (h : WellFounded R) {a : α} : extrinsicFix R F a = F a (fun a _ => extrinsicFix R F a) := by simp only [extrinsicFix, dif_pos h] rw [WellFounded.fix_eq]
theorem
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix_eq_apply
null
@[specialize] partial opaqueFix₂ [∀ a b, Nonempty (C₂ a b)] (R : (a : α) ×' β a → (a : α) ×' β a → Prop) (F : (a : α) → (b : β a) → ((a' : α) → (b' : β a') → R ⟨a', b'⟩ ⟨a, b⟩ → C₂ a' b') → C₂ a b) (a : α) (b : β a) : C₂ a b := F a b (fun a' b' _ => opaqueFix₂ R F a' b') /- SAFE assuming that the cod...
def
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
opaqueFix₂
A fixpoint combinator that allows for deferred proofs of termination. {lean}`extrinsicFix R F` is function implemented as the loop {lean}`extrinsicFix R F a = F a (fun a _ => extrinsicFix R F a)`. If the loop can be shown to be well-founded, {name}`extrinsicFix_eq_apply` proves that it satisfies the fixpoint equation...
@[implemented_by opaqueFix₂] public extrinsicFix₂ [∀ a b, Nonempty (C₂ a b)] (R : (a : α) ×' β a → (a : α) ×' β a → Prop) (F : (a : α) → (b : β a) → ((a' : α) → (b' : β a') → R ⟨a', b'⟩ ⟨a, b⟩ → C₂ a' b') → C₂ a b) (a : α) (b : β a) : C₂ a b := let F' (x : PSigma β) (G : (y : PSigma β) → R y x → C₂ y....
def
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix₂
A fixpoint combinator that can be used to construct recursive functions of arity two with an *extrinsic* proof of termination. Given a relation {name}`R` and a fixpoint functional {name}`F` which must be decreasing with respect to {name}`R`, {lean}`extrinsicFix₂ R F` is the recursive function obtained by having {name}...
public extrinsicFix₂_eq_fix [∀ a b, Nonempty (C₂ a b)] {R : (a : α) ×' β a → (a : α) ×' β a → Prop} {F : ∀ a b, (∀ a' b', R ⟨a', b'⟩ ⟨a, b⟩ → C₂ a' b') → C₂ a b} (wf : WellFounded R) {a b} : extrinsicFix₂ R F a b = wf.fix (fun x G => F x.1 x.2 (fun a b h => G ⟨a, b⟩ h)) ⟨a, b⟩ := by rw [extrinsicFix₂,...
theorem
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix₂_eq_fix
null
public extrinsicFix₂_eq_apply [∀ a b, Nonempty (C₂ a b)] {R : (a : α) ×' β a → (a : α) ×' β a → Prop} {F : (a : α) → (b : β a) → ((a' : α) → (b' : β a') → R ⟨a', b'⟩ ⟨a, b⟩ → C₂ a' b') → C₂ a b} (wf : WellFounded R) {a : α} {b : β a} : extrinsicFix₂ R F a b = F a b (fun a' b' _ => extrinsicFix₂ R F a' b...
theorem
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix₂_eq_apply
null
@[specialize] public partial opaqueFix₃ [∀ a b c, Nonempty (C₃ a b c)] (R : (a : α) ×' (b : β a) ×' γ a b → (a : α) ×' (b : β a) ×' γ a b → Prop) (F : ∀ (a b c), (∀ (a' b' c'), R ⟨a', b', c'⟩ ⟨a, b, c⟩ → C₃ a' b' c') → C₃ a b c) (a : α) (b : β a) (c : γ a b) : C₃ a b c := F a b c (fun a b c _ => opaqu...
def
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
opaqueFix₃
A fixpoint combinator that allows for deferred proofs of termination. {lean}`extrinsicFix₂ R F` is function implemented as the loop {lean}`extrinsicFix₂ R F a b = F a b (fun a' b' _ => extrinsicFix₂ R F a' b')`. If the loop can be shown to be well-founded, {name}`extrinsicFix₂_eq_apply` proves that it satisfies the f...
@[implemented_by opaqueFix₃] public extrinsicFix₃ [∀ a b c, Nonempty (C₃ a b c)] (R : (a : α) ×' (b : β a) ×' γ a b → (a : α) ×' (b : β a) ×' γ a b → Prop) (F : ∀ (a b c), (∀ (a' b' c'), R ⟨a', b', c'⟩ ⟨a, b, c⟩ → C₃ a' b' c') → C₃ a b c) (a : α) (b : β a) (c : γ a b) : C₃ a b c := let F' (x : (a : α)...
def
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix₃
A fixpoint combinator that can be used to construct recursive functions of arity three with an *extrinsic* proof of termination. Given a relation {name}`R` and a fixpoint functional {name}`F` which must be decreasing with respect to {name}`R`, {lean}`extrinsicFix₃ R F` is the recursive function obtained by having {nam...
public extrinsicFix₃_eq_fix [∀ a b c, Nonempty (C₃ a b c)] {R : (a : α) ×' (b : β a) ×' γ a b → (a : α) ×' (b : β a) ×' γ a b → Prop} {F : ∀ a b c, (∀ a' b' c', R ⟨a', b', c'⟩ ⟨a, b, c⟩ → C₃ a' b' c') → C₃ a b c} (wf : WellFounded R) {a b c} : extrinsicFix₃ R F a b c = wf.fix (fun x G => F x.1 x.2.1 x.2...
theorem
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix₃_eq_fix
null
public extrinsicFix₃_eq_apply [∀ a b c, Nonempty (C₃ a b c)] {R : (a : α) ×' (b : β a) ×' γ a b → (a : α) ×' (b : β a) ×' γ a b → Prop} {F : ∀ (a b c), (∀ (a' b' c'), R ⟨a', b', c'⟩ ⟨a, b, c⟩ → C₃ a' b' c') → C₃ a b c} (wf : WellFounded R) {a : α} {b : β a} {c : γ a b} : extrinsicFix₃ R F a b c = F a b ...
theorem
Init.WFExtrinsicFix
[ "Init.WF", "Init.Classical", "Init.Ext", "Init.NotationExtra" ]
Init/WFExtrinsicFix.lean
extrinsicFix₃_eq_apply
null
to the user via `decreasing_by`. It is not necessary to use this tactic manually. -/ macro "clean_wf" : tactic => `(tactic| simp +unfoldPartialApp +zetaDelta -failIfUnchanged only [invImage, InvImage, Prod.lex, sizeOfWFRel, measure, Nat.lt_wfRel, WellFoundedRelation.rel, sizeOf_nat, reduceCtorEq])
def
Init.WFTactics
[ "Init.MetaTypes", "Init.WF" ]
Init/WFTactics.lean
to
null
Loop where | mk @[inline]
inductive
Init.While
[ "Init.Core" ]
Init/While.lean
Loop
null
partial Loop.forIn {β : Type u} {m : Type u → Type v} [Monad m] (_ : Loop) (init : β) (f : Unit → β → m (ForInStep β)) : m β := let rec @[specialize] loop (b : β) : m β := do match ← f () b with | ForInStep.done b => pure b | ForInStep.yield b => loop b loop init
def
Init.While
[ "Init.Core" ]
Init/While.lean
Loop.forIn
null
@[expose] ForInStep.value (x : ForInStep α) : α := match x with | ForInStep.done b => b | ForInStep.yield b => b @[simp] theorem ForInStep.value_done (b : β) : (ForInStep.done b).value = b := rfl @[simp] theorem ForInStep.value_yield (b : β) : (ForInStep.yield b).value = b := rfl
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
ForInStep.value
A `ForIn'` instance, which handles `for h : x in c do`, can also handle `for x in x do` by ignoring `h`, and so provides a `ForIn` instance. Note that this instance will cause a potentially non-defeq duplication if both `ForIn` and `ForIn'` instances are provided for the same type. -/ -- We set the priority to 500 so ...
@[reducible] Functor.mapRev {f : Type u → Type v} [Functor f] {α β : Type u} : f α → (α → β) → f β := fun a f => f <$> a @[inherit_doc Functor.mapRev] infixr:100 " <&> " => Functor.mapRev recommended_spelling "mapRev" for "<&>" in [Functor.mapRev, «term_<&>_»]
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
Functor.mapRev
Maps a function over a functor, with parameters swapped so that the function comes last. This function is `Functor.map` with the parameters reversed, typically used via the `<&>` operator.
@[always_inline, inline] Functor.discard {f : Type u → Type v} {α : Type u} [Functor f] (x : f α) : f PUnit := Functor.mapConst PUnit.unit x export Functor (discard)
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
Functor.discard
Discards the value in a functor, retaining the functor's structure. Discarding values is especially useful when using `Applicative` functors or `Monad`s to implement effects, and some operation should be carried out only for its effects. In `do`-notation, statements whose values are discarded must return `Unit`, and `...
@[always_inline, inline] guard {f : Type → Type v} [Alternative f] (p : Prop) [Decidable p] : f Unit := if p then pure () else failure
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
guard
An `Alternative` functor is an `Applicative` functor that can "fail" or be "empty" and a binary operation `<|>` that “collects values” or finds the “left-most success”. Important instances include * `Option`, where `failure := none` and `<|>` returns the left-most `some`. * Parser combinators typically provide an `App...
@[always_inline, inline] optional (x : f α) : f (Option α) := some <$> x <|> pure none
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
optional
Returns `some x` if `f` succeeds with value `x`, else returns `none`.
ToBool (α : Type u) where toBool : α → Bool export ToBool (toBool)
class
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
ToBool
null
@[macro_inline] bool {β : Type u} {α : Type v} [ToBool β] (f t : α) (b : β) : α := match toBool b with | true => t | false => f
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
bool
null
@[macro_inline] orM {m : Type u → Type v} {β : Type u} [Monad m] [ToBool β] (x y : m β) : m β := do let b ← x match toBool b with | true => pure b | false => y infixr:30 " <||> " => orM recommended_spelling "orM" for "<||>" in [orM, «term_<||>_»]
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
orM
Converts the result of the monadic action `x` to a `Bool`. If it is `true`, returns it and ignores `y`; otherwise, runs `y` and returns its result. This is a monadic counterpart to the short-circuiting `||` operator, usually accessed via the `<||>` operator.
@[macro_inline] andM {m : Type u → Type v} {β : Type u} [Monad m] [ToBool β] (x y : m β) : m β := do let b ← x match toBool b with | true => y | false => pure b infixr:35 " <&&> " => andM recommended_spelling "andM" for "<&&>" in [andM, «term_<&&>_»]
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
andM
Converts the result of the monadic action `x` to a `Bool`. If it is `true`, returns `y`; otherwise, returns the original result of `x`. This is a monadic counterpart to the short-circuiting `&&` operator, usually accessed via the `<&&>` operator.
@[macro_inline] notM {m : Type → Type v} [Functor m] (x : m Bool) : m Bool := not <$> x /-!
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
notM
Runs a monadic action and returns the negation of its result.
foo : ∀ {α}, IO α → IO α
opaque
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
foo
null
bar : StateT σ IO β
opaque
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
bar
null
mapped_foo : StateT σ IO β := do let s ← get let (b, s') ← liftM <| foo <| StateT.run bar s set s' return b ``` This is fine but it's not going to generalise, what if we replace `StateT Nat IO` with a large tower of monad transformers? We would have to rewrite the above to handle each of the `run` functions fo...
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
mapped_foo
null
stM (α : Type) := α × σ
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
stM
null
restoreM (x : IO (stM α)) : StateT σ IO α := do let (a,s) ← liftM x set s return a ``` To get: ```lean
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
restoreM
null
mapped_foo' : StateT σ IO β := do let s ← get let mapInBase := fun z => StateT.run z s restoreM <| foo <| mapInBase bar ``` and finally define ```lean
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
mapped_foo'
null
control {α : Type} (f : ({β : Type} → StateT σ IO β → IO (stM β)) → IO (stM α)) : StateT σ IO α := do let s ← get let mapInBase := fun {β} (z : StateT σ IO β) => StateT.run z s let r : IO (stM α) := f mapInBase restoreM r ``` Now we can write `mapped_foo` as: ```lean
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
control
null
mapped_foo'' : StateT σ IO β := control (fun mapInBase => foo (mapInBase bar)) ``` The core idea of `mapInBase` is that given any `β`, it runs an instance of `StateT σ IO β` and 'packages' the result and state as `IO (stM β)` so that it can be piped through `foo`. Once it's been through `foo` we can then unpack the...
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
mapped_foo''
null
MonadControlT (m : Type u → Type v) (n : Type u → Type w) where /-- A type that can be used to reconstruct both a returned value and any state used by the outer monad. -/ stM : Type u → Type u /-- Lifts an action from the inner monad `m` to the outer monad `n`. The inner monad has access to a rever...
class
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
MonadControlT
A way to lift a computation from one monad to another while providing the lifted computation with a means of interpreting computations from the outer monad. This provides a means of lifting higher-order operations automatically. Clients should typically use `control` or `controlAt`, which request an instance of `Monad...
@[always_inline, inline] controlAt (m : Type u → Type v) {n : Type u → Type w} [MonadControlT m n] [Bind n] {α : Type u} (f : ({β : Type u} → n β → m (stM m n β)) → m (stM m n α)) : n α := liftWith f >>= restoreM
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
controlAt
Lifts an operation from an inner monad to an outer monad, providing it with a reverse lifting operator that allows outer monad computations to be run in the inner monad. The lifted operation is required to return extra information that is required in order to reconstruct the reverse lift's effects in the outer monad; t...
@[always_inline, inline] control {m : Type u → Type v} {n : Type u → Type w} [MonadControlT m n] [Bind n] {α : Type u} (f : ({β : Type u} → n β → m (stM m n β)) → m (stM m n α)) : n α := controlAt m f
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
control
Lifts an operation from an inner monad to an outer monad, providing it with a reverse lifting operator that allows outer monad computations to be run in the inner monad. The lifted operation is required to return extra information that is required in order to reconstruct the reverse lift's effects in the outer monad; t...
ForM (m : Type u → Type v) (γ : Type w₁) (α : outParam (Type w₂)) where /-- Runs the monadic action `f` on each element of the collection `coll`. -/ forM (coll : γ) (f : α → m PUnit) : m PUnit export ForM (forM)
class
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
ForM
Overloaded monadic iteration over some container type. An instance of `ForM m γ α` describes how to iterate a monadic operator over a container of type `γ` with elements of type `α` in the monad `m`. The element type should be uniquely determined by the monad and the container. Use `ForM.forIn` to construct a `ForIn`...
@[always_inline] Bind.kleisliRight [Bind m] (f₁ : α → m β) (f₂ : β → m γ) (a : α) : m γ := f₁ a >>= f₂
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
Bind.kleisliRight
Left-to-right composition of Kleisli arrows.
@[always_inline] Bind.kleisliLeft [Bind m] (f₂ : β → m γ) (f₁ : α → m β) (a : α) : m γ := f₁ a >>= f₂
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
Bind.kleisliLeft
Right-to-left composition of Kleisli arrows.
@[always_inline] Bind.bindLeft [Bind m] (f : α → m β) (ma : m α) : m β := ma >>= f -- Precedence choice taken to be the same as in haskell: -- https://hackage.haskell.org/package/base-4.17.0.0/docs/Control-Monad.html#v:-61--60--60- @[inherit_doc] infixr:55 " >=> " => Bind.kleisliRight @[inherit_doc] infixr:55 " <=< ...
def
Init.Control
[ "Init.Core", "Init.BinderNameHint" ]
Init/Control/Basic.lean
Bind.bindLeft
Same as `Bind.bind` but with arguments swapped.
@[always_inline, inline] protected orElse' {δ} [Backtrackable δ σ] (x₁ x₂ : EStateM ε σ α) (useFirstEx := true) : EStateM ε σ α := fun s => let d := Backtrackable.save s; match x₁ s with | Result.error e₁ s₁ => match x₂ (Backtrackable.restore s₁ d) with | Result.error e₂ s₂ => Result.error (if useFirstEx ...
def
Init.Control
[ "Init.Data.ToString.Basic" ]
Init/Control/EState.lean
orElse'
Alternative orElse operator that allows callers to select which exception should be used when both operations fail. The default is to use the first exception since the standard `orElse` uses the second.
@[always_inline, inline] fromStateM {ε σ α : Type} (x : StateM σ α) : EStateM ε σ α := fun s => match x.run s with | (a, s') => EStateM.Result.ok a s'
def
Init.Control
[ "Init.Data.ToString.Basic" ]
Init/Control/EState.lean
fromStateM
Converts a state monad action into a state monad action with exceptions. The resulting action does not throw an exception.
@[always_inline, inline] protected pure (a : α) : Except ε α := Except.ok a
def
Init.Control
[ "Init.Control.Basic", "Init.Control.Id" ]
Init/Control/Except.lean
pure
A successful computation in the `Except ε` monad: `a` is returned, and no exception is thrown.
@[always_inline, inline] protected map (f : α → β) : Except ε α → Except ε β | Except.error err => Except.error err | Except.ok v => Except.ok <| f v @[simp] theorem map_id : Except.map (ε := ε) (α := α) (β := α) id = id := by apply funext intro e simp [Except.map]; cases e <;> rfl
def
Init.Control
[ "Init.Control.Basic", "Init.Control.Id" ]
Init/Control/Except.lean
map
Transforms a successful result with a function, doing nothing when an exception is thrown. Examples: * `(pure 2 : Except String Nat).map toString = pure 2` * `(throw "Error" : Except String Nat).map toString = throw "Error"`