com.jnape.palatable.lambda.monad

## Class SafeT<M extends MonadRec<?,M>,A>

- java.lang.Object
- - com.jnape.palatable.lambda.monad.SafeT<M,A>

- Type Parameters:
    `M` - the `MonadRec` instance
    `A` - the carrier type
  All Implemented Interfaces:
    Applicative<A,SafeT<M,?>>, Functor<A,SafeT<M,?>>, Monad<A,SafeT<M,?>>, MonadBase<M,A,SafeT<?,?>>, MonadRec<A,SafeT<M,?>>, MonadT<M,A,SafeT<M,?>,SafeT<?,?>>

      public final class SafeT<M extends MonadRec<?,M>,A>
      extends Object
      implements MonadT<M,A,SafeT<M,?>,SafeT<?,?>>

  A stack-safe `monad transformer` that can safely interpret deeply nested left- or right-associated binds for any `MonadRec`.

  Example:

       Times.<Fn1<Integer, Integer>>times(100_000, f -> f.fmap(x -> x + 1), id()).apply(0); // stack-overflow
       Times.<SafeT<Fn1<Integer, ?>, Integer>>times(100_000, f -> f.fmap(x -> x + 1), safeT(id()))
               .<Fn1<Integer, Integer>>runSafeT()
               .apply(0); // 100_000

  Inspired by Phil Freeman's paper Stack Safety for Free.

- - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `<B> SafeT<M,B>` | `discardL(Applicative<B,SafeT<M,?>> appB)` Sequence both this `Applicative` and `appB`, discarding this `Applicative's` result and returning `appB`.
    `<B> SafeT<M,A>` | `discardR(Applicative<B,SafeT<M,?>> appB)` Sequence both this `Applicative` and `appB`, discarding `appB's` result and returning this `Applicative`.
    `<B> SafeT<M,B>` | `flatMap(Fn1<? super A,? extends Monad<B,SafeT<M,?>>> f)` Chain dependent computations that may continue or short-circuit based on previous results.
    `<B> SafeT<M,B>` | `fmap(Fn1<? super A,? extends B> fn)` Covariantly transmute this functor's parameter using the given mapping function.
    `<B> Lazy<SafeT<M,B>>` | `lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,SafeT<M,?>>> lazyAppFn)` Given a `lazy` instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.
    `<B,N extends MonadRec<?,N>> SafeT<N,B>` | `lift(MonadRec<B,N> nb)` Lift a new argument `MonadRec` into this `MonadBase`.
    `static Lift<SafeT<?,?>>` | `liftSafeT()` `Lift` for `SafeT`.
    `<B> SafeT<M,B>` | `pure(B b)` Lift the value `b` into this applicative functor.
    `static <M extends MonadRec<?,M>> Pure<SafeT<M,?>>` | `pureSafeT(Pure<M> pureM)` The canonical `Pure` instance for `SafeT`.
    `<MA extends MonadRec<A,M>> MA` | `runSafeT()` Recover the full structure of the embedded `Monad` in a stack-safe way.
    `static <M extends MonadRec<?,M>,A> SafeT<M,A>` | `safeT(MonadRec<A,M> ma)` Lift any ``MonadRec`<A, M>` into a ``SafeT`<M, A>`.
    `<B> SafeT<M,B>` | `trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,SafeT<M,?>>> bounce)` Given some operation yielding a `RecursiveResult` inside this `MonadRec`, internally trampoline the operation until it yields a `termination` instruction.
    `<B> SafeT<M,B>` | `zip(Applicative<Fn1<? super A,? extends B>,SafeT<M,?>> appFn)` Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface com.jnape.palatable.lambda.monad.Monad

      `join`

    - ### Methods inherited from interface com.jnape.palatable.lambda.functor.Functor

      `coerce`

- - ### Method Detail

    - #### runSafeT

          public <MA extends MonadRec<A,M>> MA runSafeT()

      Recover the full structure of the embedded `Monad` in a stack-safe way.
      Type Parameters:
        `MA` - the witnessed target type
      Returns:
        the embedded `Monad`

    - #### fmap

          public <B> SafeT<M,B> fmap(Fn1<? super A,? extends B> fn)

      Covariantly transmute this functor's parameter using the given mapping function. Generally this method is specialized to return an instance of the class implementing Functor.
      Specified by:
        `fmap` in interface `Applicative<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `fmap` in interface `Functor<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `fmap` in interface `Monad<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `fmap` in interface `MonadRec<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `fmap` in interface `MonadT<M extends MonadRec<?,M>,A,SafeT<M extends MonadRec<?,M>,?>,SafeT<?,?>>`
      Type Parameters:
        `B` - the new parameter type
      Parameters:
        `fn` - the mapping function
      Returns:
        a functor over B (the new parameter type)

    - #### lift

          public <B,N extends MonadRec<?,N>> SafeT<N,B> lift(MonadRec<B,N> nb)

      Lift a new argument `MonadRec` into this `MonadBase`.
      Specified by:
        `lift` in interface `MonadBase<M extends MonadRec<?,M>,A,SafeT<?,?>>`
      Specified by:
        `lift` in interface `MonadT<M extends MonadRec<?,M>,A,SafeT<M extends MonadRec<?,M>,?>,SafeT<?,?>>`
      Type Parameters:
        `B` - the `MonadRec` carrier type
        `N` - the argument `MonadRec` witness
      Parameters:
        `nb` - the argument `MonadRec`
      Returns:
        the new `MonadBase`

    - #### flatMap

          public <B> SafeT<M,B> flatMap(Fn1<? super A,? extends Monad<B,SafeT<M,?>>> f)

      Chain dependent computations that may continue or short-circuit based on previous results.
      Specified by:
        `flatMap` in interface `Monad<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `flatMap` in interface `MonadRec<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `flatMap` in interface `MonadT<M extends MonadRec<?,M>,A,SafeT<M extends MonadRec<?,M>,?>,SafeT<?,?>>`
      Type Parameters:
        `B` - the resulting monad parameter type
      Parameters:
        `f` - the dependent computation over A
      Returns:
        the new monad instance

    - #### zip

          public <B> SafeT<M,B> zip(Applicative<Fn1<? super A,? extends B>,SafeT<M,?>> appFn)

      Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.
      Specified by:
        `zip` in interface `Applicative<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `zip` in interface `Monad<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `zip` in interface `MonadRec<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `zip` in interface `MonadT<M extends MonadRec<?,M>,A,SafeT<M extends MonadRec<?,M>,?>,SafeT<?,?>>`
      Type Parameters:
        `B` - the resulting applicative parameter type
      Parameters:
        `appFn` - the other applicative instance
      Returns:
        the mapped applicative

    - #### lazyZip

          public <B> Lazy<SafeT<M,B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,SafeT<M,?>>> lazyAppFn)

      Given a `lazy` instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports. This is useful for applicatives that support lazy evaluation and early termination.
      Specified by:
        `lazyZip` in interface `Applicative<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `lazyZip` in interface `Monad<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `lazyZip` in interface `MonadRec<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `lazyZip` in interface `MonadT<M extends MonadRec<?,M>,A,SafeT<M extends MonadRec<?,M>,?>,SafeT<?,?>>`
      Type Parameters:
        `B` - the resulting applicative parameter type
      Parameters:
        `lazyAppFn` - the lazy other applicative instance
      Returns:
        the mapped applicative
      See Also:
        `Maybe`, `Either`

    - #### discardL

          public <B> SafeT<M,B> discardL(Applicative<B,SafeT<M,?>> appB)

      Sequence both this `Applicative` and `appB`, discarding this `Applicative's` result and returning `appB`. This is generally useful for sequentially performing side-effects.
      Specified by:
        `discardL` in interface `Applicative<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `discardL` in interface `Monad<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `discardL` in interface `MonadRec<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `discardL` in interface `MonadT<M extends MonadRec<?,M>,A,SafeT<M extends MonadRec<?,M>,?>,SafeT<?,?>>`
      Type Parameters:
        `B` - the type of the returned Applicative's parameter
      Parameters:
        `appB` - the other Applicative
      Returns:
        appB

    - #### discardR

          public <B> SafeT<M,A> discardR(Applicative<B,SafeT<M,?>> appB)

      Sequence both this `Applicative` and `appB`, discarding `appB's` result and returning this `Applicative`. This is generally useful for sequentially performing side-effects.
      Specified by:
        `discardR` in interface `Applicative<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `discardR` in interface `Monad<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `discardR` in interface `MonadRec<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `discardR` in interface `MonadT<M extends MonadRec<?,M>,A,SafeT<M extends MonadRec<?,M>,?>,SafeT<?,?>>`
      Type Parameters:
        `B` - the type of appB's parameter
      Parameters:
        `appB` - the other Applicative
      Returns:
        this Applicative

    - #### pure

          public <B> SafeT<M,B> pure(B b)

      Lift the value `b` into this applicative functor.
      Specified by:
        `pure` in interface `Applicative<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `pure` in interface `Monad<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `pure` in interface `MonadRec<A,SafeT<M extends MonadRec<?,M>,?>>`
      Specified by:
        `pure` in interface `MonadT<M extends MonadRec<?,M>,A,SafeT<M extends MonadRec<?,M>,?>,SafeT<?,?>>`
      Type Parameters:
        `B` - the type of the returned applicative's parameter
      Parameters:
        `b` - the value
      Returns:
        an instance of this applicative over b

    - #### trampolineM

          public <B> SafeT<M,B> trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,SafeT<M,?>>> bounce)

      Given some operation yielding a `RecursiveResult` inside this `MonadRec`, internally trampoline the operation until it yields a `termination` instruction.

      Stack-safety depends on implementations guaranteeing that the growth of the call stack is a constant factor independent of the number of invocations of the operation. For various examples of how this can be achieved in stereotypical circumstances, see the referenced types.

      Specified by:
        `trampolineM` in interface `MonadRec<A,SafeT<M extends MonadRec<?,M>,?>>`
      Type Parameters:
        `B` - the ultimate resulting carrier type
      Parameters:
        `bounce` - the function to internally trampoline
      Returns:
        the trampolined `MonadRec`
      See Also:
        `for a basic implementation`, `for a {@link CoProduct2 coproduct} implementation`, `for an implementation leveraging an already stack-safe {@link Monad#flatMap(Fn1)}`, `for a {@link MonadT monad transformer} implementation`

    - #### safeT

          public static <M extends MonadRec<?,M>,A> SafeT<M,A> safeT(MonadRec<A,M> ma)

      Lift any ``MonadRec`<A, M>` into a ``SafeT`<M, A>`.
      Type Parameters:
        `M` - the `MonadRec` witness
        `A` - the carrier type
      Parameters:
        `ma` - the `MonadRec`<A, M>
      Returns:
        the new `SafeT`

    - #### pureSafeT

          public static <M extends MonadRec<?,M>> Pure<SafeT<M,?>> pureSafeT(Pure<M> pureM)

      The canonical `Pure` instance for `SafeT`.
      Type Parameters:
        `M` - the argument `Monad` witness
      Parameters:
        `pureM` - the argument `Monad` `Pure`
      Returns:
        the `Pure` instance

    - #### liftSafeT

          public static Lift<SafeT<?,?>> liftSafeT()

      `Lift` for `SafeT`.
      Returns:
        the `Monad` lifted into `SafeT`