Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: docstrings for Alternative #3860

Merged
merged 9 commits into from
Apr 15, 2024
19 changes: 19 additions & 0 deletions src/Init/Control/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@ def Functor.discard {f : Type u → Type v} {α : Type u} [Functor f] (x : f α)

export Functor (discard)

/--
An `Alternative` functor is an `Applicative` functor that has a notion of “emptyness” or “failure”
nomeata marked this conversation as resolved.
Show resolved Hide resolved
and a binary operation `<|>` with a notion of “collecting values” or “left-most success”.
nomeata marked this conversation as resolved.
Show resolved Hide resolved

Important instances include
* `Option`, where `failure := .none` and `<|>` returns the left-most `.some`.
nomeata marked this conversation as resolved.
Show resolved Hide resolved
* Parser combinators typically provide an `Applicative` instance for error-handling and
backtracking.
nomeata marked this conversation as resolved.
Show resolved Hide resolved
-/
-- NB: List instance is in mathlib. Once upstreamed, add
-- * `List`, where `failure` is the empty list and `<|>` concatenates.
class Alternative (f : Type u → Type v) extends Applicative f : Type (max (u+1) v) where
/-- See `Alternative`. -/
nomeata marked this conversation as resolved.
Show resolved Hide resolved
failure : {α : Type u} → f α
/-- See `Alternative`. Can be writen using `<|>`. -/
nomeata marked this conversation as resolved.
Show resolved Hide resolved
orElse : {α : Type u} → f α → (Unit → f α) → f α

instance (f : Type u → Type v) (α : Type u) [Alternative f] : OrElse (f α) := ⟨Alternative.orElse⟩
Expand All @@ -30,9 +43,15 @@ variable {f : Type u → Type v} [Alternative f] {α : Type u}

export Alternative (failure)

/--
If the proposition `p` holds, does nothing, else fails (using `failure`).
nomeata marked this conversation as resolved.
Show resolved Hide resolved
-/
@[always_inline, inline] def guard {f : Type → Type v} [Alternative f] (p : Prop) [Decidable p] : f Unit :=
if p then pure () else failure

/--
Returns `some x` if `f` succeeds with value `x`, else returns `none`.
-/
@[always_inline, inline] def optional (x : f α) : f (Option α) :=
some <$> x <|> pure none

Expand Down
Loading