Documentation

CslibFrontier.Foundations.Control.Except

Sequencing and observing Except #

Core and Batteries state how <$> acts on Except.ok and Except.error, but not >>=, and nothing about Except.toOption, which forgets which error occurred. These equations let simp evaluate do blocks over Except and compare computations up to the error they raise.

For a list of computations run from left to right, List.mapM and List.filterMapM succeed exactly when every step does, and then return the results of the steps. Batteries characterizes the successful results of List.mapM by List.Forall₂ (List.mapM_eq_ok_iff_forall₂); the observations here say the same through Except.toOption, in a form that is stable under permutations and filters of the list.

A specification x.Spec P E says that x succeeds with a result satisfying P or fails with an error satisfying E. Specifications of the steps of a do block compose by Except.Spec.bind, so a proof that a program only fails in a given way needs no case for each step that can fail.

Main results #

@[simp]
theorem Except.ok_bind {ε : Type u} {α β : Type v} (a : α) (f : α → Except ε β) :
ok a >>= f = f a
@[simp]
theorem Except.error_bind {ε : Type u} {α β : Type v} (e : ε) (f : α → Except ε β) :
@[simp]
theorem Except.pure_eq_ok {ε : Type u} {α : Type v} (a : α) :
pure a = ok a

pure is Except.ok. The simp normal form is Except.ok, the form statements use.

@[simp]
theorem Except.bind_ok {ε : Type u} {α : Type v} (x : Except ε α) :
x >>= ok = x

Continuing with Except.ok returns the computation: the right unit law in the normal form of Except.pure_eq_ok.

theorem Except.bind_eq_ok {ε : Type u} {α β : Type v} {x : Except ε α} {f : α → Except ε β} {b : β} :
x >>= f = ok b ↔ ∃ (a : α), x = ok a ∧ f a = ok b
theorem Except.bind_comm_of_eq_error {ε : Type u} {α β γ : Type v} {x : Except ε α} {y : Except ε β} {e₀ : ε} (hx : ∀ (e : ε), x = error e → e = e₀) (hy : ∀ (e : ε), y = error e → e = e₀) (f : α → β → Except ε γ) :
(do let a ← x let b ← y f a b) = do let b ← y let a ← x f a b

Computations that can fail only with the same error commute.

@[simp]
theorem Except.toOption_ok {ε : Type u} {α : Type v} (a : α) :
@[simp]
theorem Except.toOption_error {ε : Type u} {α : Type v} (e : ε) :
theorem Except.toOption_pure {ε : Type u} {α : Type v} (a : α) :
theorem Except.toOption_bind {ε : Type u} {α β : Type v} (x : Except ε α) (f : α → Except ε β) :
(x >>= f).toOption = x.toOption.bind fun (a : α) => (f a).toOption
theorem Except.toOption_map {ε : Type u} {α β : Type v} (f : α → β) (x : Except ε α) :
theorem Except.toOption_eq_some_iff {ε : Type u} {α : Type v} {x : Except ε α} {a : α} :
x.toOption = some a ↔ x = ok a

Lists of computations #

theorem Except.toOption_mapM {ε : Type u} {α β : Type v} (f : α → Except ε β) (l : List α) :
(List.mapM f l).toOption = if ∀ a ∈ l, (f a).toOption.isSome = true then some (List.filterMap (fun (a : α) => (f a).toOption) l) else none

List.mapM succeeds exactly when every step succeeds, and then returns their results.

theorem Except.mapM_eq_ok_map {ε : Type u} {α β : Type v} {f : α → Except ε β} {g : α → β} {l : List α} (h : ∀ a ∈ l, f a = ok (g a)) :

List.mapM of steps that succeed returns their results.

theorem Except.toOption_mapM_congr {ε : Type u} {α β : Type v} {f g : α → Except ε β} {l : List α} (h : ∀ a ∈ l, (f a).toOption = (g a).toOption) :

List.mapM of steps with the same observations on the list has the same observation.

theorem Except.toOption_filterMapM {ε : Type u} {α β : Type v} (f : α → Except ε (Option β)) (l : List α) :
(List.filterMapM f l).toOption = if ∀ a ∈ l, (f a).toOption.isSome = true then some (List.filterMap (fun (a : α) => (f a).toOption.join) l) else none

List.filterMapM succeeds exactly when every step succeeds, and then keeps the present results.

Postconditions #

def Except.Spec {ε : Type u} {α : Type v} (P : α → Prop) (E : ε → Prop) :
Except ε α → Prop

x.Spec P E: x succeeds with a result satisfying P, or fails with an error satisfying E. Specifications of the steps of a do block compose by Except.Spec.bind.

Instances For
    @[simp]
    theorem Except.spec_ok {ε : Type u} {α : Type v} {P : α → Prop} {E : ε → Prop} {a : α} :
    Spec P E (ok a) ↔ P a
    @[simp]
    theorem Except.spec_error {ε : Type u} {α : Type v} {P : α → Prop} {E : ε → Prop} {e : ε} :
    Spec P E (error e) ↔ E e
    theorem Except.Spec.bind {ε : Type u} {α β : Type v} {P : α → Prop} {Q : β → Prop} {E : ε → Prop} {x : Except ε α} {f : α → Except ε β} (hx : Spec P E x) (hf : ∀ (a : α), x = ok a → P a → Spec Q E (f a)) :
    Spec Q E (x >>= f)

    A sequenced computation meets Q and E when its first step meets P and E, and the rest meets Q and E from every result of the first step satisfying P.

    theorem Except.Spec.mono {ε : Type u} {α : Type v} {P P' : α → Prop} {E E' : ε → Prop} {x : Except ε α} (hx : Spec P E x) (hP : ∀ (a : α), P a → P' a) (hE : ∀ (e : ε), E e → E' e) :
    Spec P' E' x
    theorem Except.Spec.imp_error {ε : Type u} {α : Type v} {P : α → Prop} {x : Except ε α} {e₀ : ε} {B B' : Prop} (hx : Spec P (fun (e : ε) => e = e₀ ∧ B) x) (h : B → B') :
    Spec P (fun (e : ε) => e = e₀ ∧ B') x

    Weaken the condition under which a computation fails with e₀.

    theorem Except.Spec.of_eq_ok {ε : Type u} {α : Type v} {P : α → Prop} {E : ε → Prop} {x : Except ε α} (hx : Spec P E x) {a : α} (h : x = ok a) :
    P a
    theorem Except.Spec.of_eq_error {ε : Type u} {α : Type v} {P : α → Prop} {E : ε → Prop} {x : Except ε α} (hx : Spec P E x) {e : ε} (h : x = error e) :
    E e
    theorem Except.Spec.ne_error {ε : Type u} {α : Type v} {P : α → Prop} {E : ε → Prop} {x : Except ε α} (hx : Spec P E x) {e : ε} (he : ¬E e) :
    theorem Except.Spec.exists_eq_ok {ε : Type u} {α : Type v} {P : α → Prop} {E : ε → Prop} {x : Except ε α} (hx : Spec P E x) (hE : ∀ (e : ε), ¬E e) :
    ∃ (a : α), x = ok a ∧ P a
    theorem Except.Spec.eq_ok_or_eq_error {ε : Type u} {α : Type v} {P : α → Prop} {x : Except ε α} {e₀ : ε} {B : Prop} (hx : Spec P (fun (e : ε) => e = e₀ ∧ B) x) :
    (∃ (a : α), x = ok a ∧ P a) ∨ x = error e₀ ∧ B

    A computation that fails only with e₀, under the condition B.

    theorem Except.Spec.mapM {ε : Type u} {α β : Type v} {E : ε → Prop} {l : List α} {f : α → Except ε β} {R : α → β → Prop} (h : ∀ a ∈ l, Spec (R a) E (f a)) :
    Spec (fun (bs : List β) => ∀ b ∈ bs, ∃ a ∈ l, R a b) E (List.mapM f l)

    Every result of a successful List.mapM comes from a step whose specification it meets.

    theorem Except.Spec.filterMapM {ε : Type u} {α β : Type v} {E : ε → Prop} {l : List α} {f : α → Except ε (Option β)} {R : α → β → Prop} (h : ∀ a ∈ l, Spec (fun (o : Option β) => ∀ b ∈ o, R a b) E (f a)) :
    Spec (fun (bs : List β) => ∀ b ∈ bs, ∃ a ∈ l, R a b) E (List.filterMapM f l)

    Every result of a successful List.filterMapM comes from a step whose specification it meets.