module Monad_intf:sig
..end
return x >>= f = f x
t >>= fun x -> return x = t
(t >>= f) >>= g = t >>= fun x -> (f x >>= g)
>>=
is the infix notation for bind
)module type Basic =sig
..end
module type Infix =sig
..end
module type S =sig
..end
module type Basic2 =sig
..end
module type Infix2 =sig
..end
module type S2 =sig
..end
module S_to_S2:
module type Monad =sig
..end
return x >>= f = f x
t >>= fun x -> return x = t
(t >>= f) >>= g = t >>= fun x -> (f x >>= g)
>>=
is the infix notation for bind
)map
argument to Monad.Make
says how to implement the monad's map
function.
`Define_using_bind
means to define map t ~f = bind t (fun a -> return (f a))
.
`Custom
overrides the default implementation, presumably with something more
efficient.
Some other functions returned by Monad.Make
are defined in terms of map
, so
passing in a more efficient map
will improve their efficiency as well.
t >>= f
returns a computation that sequences the computations represented by two
monad elements. The resulting computation first does t
to yield a value v
, and
then runs the computation returned by f v
.
t >>| f
is t >>= (fun a -> return (f a))
.
A monad is an abstraction of the concept of sequencing of computations. A value of
type 'a monad represents a computation that returns a value of type 'a.
bind t f
= t >>= f
return v
returns the (trivial) computation that returns v.
map t ~f
is t >>| f.
join t
is t >>= (fun t' -> t')
.
ignore_m t
is map t ~f:(fun _ -> ())
. ignore_m
used to be called ignore
,
but we decided that was a bad name, because it shadowed the widely used
Pervasives.ignore
. Some monads still do let ignore = ignore_m
for historical
reasons.
Multi parameter monad.
The second parameter get unified across all the computation. This is used
to encode monads working on a multi parameter data structure like
(('a,'b result)
).
Same as Infix, except the monad type has two arguments. The second is always just
passed through.
The same as S except the monad type has two arguments. The second is always just
passed through.