Skip to content

Instantly share code, notes, and snippets.

@sparverius
Created September 25, 2019 22:27
Show Gist options
  • Save sparverius/8635697960966cbcc083e8505c0501d2 to your computer and use it in GitHub Desktop.
Save sparverius/8635697960966cbcc083e8505c0501d2 to your computer and use it in GitHub Desktop.
(* ****** ****** *)
#include
"share/atspre_staload.hats"
#include
"share/atspre_staload_libats_ML.hats"
(* ****** ****** *)
extern
fun
{fa,a0:t0p}
{fb,b0:t0p}
monad_bind
(fa, cfun(a0, fb)): fb
extern
fun
{fa,a0:t0p}
monad_return(x0: a0): fa
(* ****** ****** *)
implement
(a:t@ype)
monad_return<list0(a),a>(x0) =
list0_sing(x0)
implement
(a,b:t@ype)
monad_bind<list0(a),a><list0(b),b>
(fx, f0) =
(
list0_concat(list0_map(fx, f0))
)
(* ****** ****** *)
implement
(a,b:t@ype)
monad_bind<option0(a),a><option0(b),b>
(fx, f0) =
(
case+ fx of
| None0() => None0() | Some0(x0) => f0(x0)
)
implement
(a:t@ype)
monad_return<option0(a),a>(x0) = option0_some(x0)
(* ****** ****** *)
extern
fun
{fa,a0:t0p}
{fb,b0:t0p}
monad_fmap(fa, cfun(a0, b0)): fb
(* ****** ****** *)
implement
{fa,a0:t@ype}
{fb,b0:t@ype}
monad_fmap(fx, f0) =
(
monad_bind<fa,a0><fb,b0>
(fx, lam x0 => monad_return<fb,b0>(f0(x0)))
// monad_bind
) // end of [monad_fmap]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment