cat/instances/monad

Monad instances: Writer, Reader.

Values

pub fn function_monad(
  ,
) -> monad.Monad(types.FunctionF(r), a, b, fn(r) -> a, fn(r) -> b)

Monad instance for (->).

Examples

let h = {
  use f <- function_monad().bind(fn(x) { fn(y) { x * y } })
  use x <- function_monad().map(fn(x) { x + 5 })
  f(x)
}
h(2)
// -> 14
pub fn identity_monad(
  ,
) -> monad.Monad(
  types.IdentityF,
  a,
  b,
  cat.Identity(a),
  cat.Identity(b),
)

Monad instance for Identity.

Examples

{
  use x <- identity_monad().bind(Identity("res: "))
  use y <- identity_monad().bind(Identity(7))
  identity_monad().return(x <> int.to_string(y))
}
// -> Identity("res: 7")
// Or without use expressions:
identity_monad().bind(Identity("res: "), fn(x) {
  identity_monad().bind(Identity(7), fn(y) {
    identity_monad().return(x <> int.to_string(y))
  })
})
// -> Identity("res: 7")
pub fn list_monad(
  ,
) -> monad.Monad(types.ListF, a, b, List(a), List(b))

Monad instance for List.

Examples

let lm = list_monad()
{
  use x <- lm.bind([1, 2, 3])
  use y <- lm.bind([4, 5])
  lm.return(x * y)
}
// -> [4, 5, 8, 10, 12, 15]
pub fn option_monad(
  ,
) -> monad.Monad(
  types.OptionF,
  a,
  b,
  option.Option(a),
  option.Option(b),
)

Monad instance for Option.

Examples

let op = option_monad()
{
  use x <- op.bind(Some(2))
  use y <- op.map(Some(3))
  x + y
}
// -> Some(5)
{
  use x <- op.bind(None)
  use y <- op.map(Some(3))
  x + y
}
// -> None
{
  use x <- op.bind(Some(2))
  use y <- op.map(None)
  x + y
}
// -> None
pub fn reader_monad(
  ,
) -> monad.Monad(
  types.ReaderF(r),
  a,
  b,
  cat.Reader(r, a),
  cat.Reader(r, b),
)

Monad instance for Reader.

instance Monad ((->) r) where
  f >>= k = \ r -> k (f r) r

Examples

let r = {
  use t1 <- reader_monad().bind(Reader(fn(x) { x % 2 == 0 }))
  use t2 <- reader_monad().map(Reader(fn(x) { x % 3 == 0 }))
  t1 || t2
}
r.apply(5)
// -> False
r.apply(6)
// -> True
pub fn result_monad(
  ,
) -> monad.Monad(
  types.ResultF(e),
  a,
  b,
  Result(a, e),
  Result(b, e),
)

Monad instance for Result.

Examples

let rm = result_monad()
{
  use x <- rm.bind(Ok(2))
  use y <- rm.map(Ok(3))
  x + y
}
// -> Ok(5)
{
  use x <- rm.bind(Error("Nan"))
  use y <- rm.map(Ok(3))
  x + y
}
// -> Error("Nan")
{
  use x <- rm.bind(Ok(2))
  use y <- rm.map(Error("Nan"))
  x + y
}
// -> Error("Nan")
pub fn state_monad(
  ,
) -> monad.Monad(
  types.StateM(s),
  a,
  b,
  cat.State(s, a),
  cat.State(s, b),
)

State monad.

Examples

let count = State(fn(x) { #(x, x + 1) })
{
  use x <- state_monad().bind(count) // 1
  use y <- state_monad().bind(count) // 2
  use z <- state_monad().map(count)  // 3
  x + y + z                          // 1 + 2 + 3
}.run(1)
// -> #(6, 4)
pub fn writer_monad(
  ,
) -> monad.Monad(
  types.WriterF,
  a,
  b,
  cat.Writer(a),
  cat.Writer(b),
)

Monad instance for Writer.

instance Monad Writer where
 ma >>= k = 
  let (va, log1) = runWriter ma
      (vb, log2) = runWriter (k va)
  in  Writer (vb, log1 ++ log2)

Examples

{
  use x <- writer_monad().bind(Writer(2, "two + "))
  use y <- writer_monad().map(Writer(3, "three"))
  x + y
}
// -> Writer(5, "two + three")
Search Document