Documentation

Batteries.Control.AlternativeMonad

Laws for Monads with Failure #

Definitions for monads that also have an Alternative instance while sharing the underlying Applicative instance, and a class LawfulAlternative for types where the failure and orElse operations behave in a natural way. More specifically they satisfy:

Option/OptionT are the most basic examples, but transformers like StateT also preserve the lawfulness of this on the underlying monad.

The law x *> failure = failure is true for monads like Option and List that don't have any "side effects" to execution, but not for something like OptionT on some monads, so we don't include this condition.

We also define a class LawfulAlternativeLift similar to LawfulMonadLift that states that a lifting between monads preserves failure and orElse.

Tags #

monad, alternative, failure

class AlternativeMonad (m : Type u_1 → Type u_2) extends Alternative m, Monad m :
Type (max (u_1 + 1) u_2)

AlternativeMonad m means that m has both a Monad and Alternative instance, which both share the same underlying Applicative instance. The main example is Option, but many monad transformers also preserve or add this structure.

Instances
    class LawfulAlternative (m : Type u_1 → Type u_2) [Alternative m] extends LawfulApplicative m :

    LawfulAlternative m means that the failure operation on m behaves naturally with respect to map, seq, and orElse operators.

    Instances
      @[simp]
      theorem mapConst_failure {m : Type u_1 → Type u_2} {β α : Type u_1} [Alternative m] [LawfulAlternative m] (y : β) :
      @[simp]
      theorem mapConst_orElse {m : Type u_1 → Type u_2} {α β : Type u_1} [Alternative m] [LawfulAlternative m] (x x' : m α) (y : β) :
      @[simp]
      theorem failure_seqLeft {m : Type u_1 → Type u_2} {α β : Type u_1} [Alternative m] [LawfulAlternative m] (x : m α) :
      @[simp]
      theorem failure_seqRight {m : Type u_1 → Type u_2} {α β : Type u_1} [Alternative m] [LawfulAlternative m] (x : m α) :
      @[simp]
      theorem failure_bind {m : Type u_1 → Type u_2} {α β : Type u_1} [AlternativeMonad m] [LawfulAlternative m] [LawfulMonad m] (x : αm β) :
      @[simp]
      theorem seq_failure {m : Type u_1 → Type u_2} {α β : Type u_1} [AlternativeMonad m] [LawfulAlternative m] [LawfulMonad m] (x : m (αβ)) :
      x <*> failure = x *> failure
      class LawfulAlternativeLift (m : semiOutParam (Type u → Type v)) (n : Type u → Type w) [Alternative m] [Alternative n] [MonadLift m n] :

      Type-class for monad lifts that preserve the Alternative operations.

      Instances