monad_cps v0.1.0 Control.Continuation View Source

This module provides the Continuation Monad, in which every other monad can be implemented.

defmodule Maybe do
  @moduledoc false

  @nothing :nothing
  @just :just

  use ContinuationMonad

  defmacro __using__(_options) do
    quote do
      import unquote(__MODULE__)
      use ContinuationMonad
    end
  end

  defp _return(x) do
    {@just, x}
  end

  defp _bind(@nothing, _a2mb) do
    @nothing
  end

  defp _bind({@just, a}, a2mb) do
    a2mb.(a)
  end

  def return(x) do
    ic(_return(x))
  end

  def nothing() do
    ic(@nothing)
  end

  # Wrapping / Unwrapping
  defp ic(ma) do
    make_ic(&_bind/2).(ma)
  end

  def run(m) do
    make_run(&_return/1).(m)
  end
end

defmodule MaybeClient do

  use Maybe

  def some_fun() do
    monad do
      x <- return("eggs")
      y <- return("bacon")
      return({x, y})
    end
  end

  run(some_fun())

end

Link to this section Summary

Functions

applies a function from a -> b that is within the Monad to a Monad of type a and produces a Monad of type b.

Shortcut for ~>>

This is the classic monadic bind function. In Haskell this would be

allows for early exits.

Take a value x and produces a function that ignores its input and produces x.

Applies a function of type a -> b to a Functor of type a. Produces a Functor of type b.

Identity function

This functions produces a "lift" function, that lifts the monad instances into the continuation monad.

Produces a function that "executes" the continuation monad.

Any Monad instance is also an instance of Applicative. This function is equivalent to return

Link to this section Functions

applies a function from a -> b that is within the Monad to a Monad of type a and produces a Monad of type b.

Shortcut for ~>>

This is the classic monadic bind function. In Haskell this would be

Monad a >>=  -> Monad b

allows for early exits.

Take a value x and produces a function that ignores its input and produces x.

Applies a function of type a -> b to a Functor of type a. Produces a Functor of type b.

Identity function

This functions produces a "lift" function, that lifts the monad instances into the continuation monad.

Produces a function that "executes" the continuation monad.

Any Monad instance is also an instance of Applicative. This function is equivalent to return