-
travesty
-
-
travesty.containers
-
-
travesty.core_kernel_exts
Library
Module
Module type
Parameter
Class
Class type
Signatures and functors for containers and data structures that can be mapped across with a monadic side-effect.
Modules based on this pattern resembles the Haskell Traversable typeclass, but with two differences:
- We currently define traversability in terms of monads, not applicative functors (this might change in the future, but monads are generally more well-understood and well-supported in the OCaml/Core ecosystem);
- as a result, the main 'traverse' function is called
map_m
.
Signatures
Inner-traversal signatures
These signatures form the inner body of the On_monad
functor in the main signatures. They all have names ending with _on_monad
, and assume the existence of a monad M
.
While they aren't interesting on their own, they do contain (in slightly abstract form) the specific functions needed to build, and provided on building, traversable containers.
The generic signatures
As with Mappable, we define some signatures for traversable structures in an arity-generic way, then specialise them for arity-0 and arity-1 types.
module type Basic_generic_on_monad = sig ... end
Basic_generic_on_monad
describes monadic traversal on either an arity-0 or arity-1 type.
module type Generic_on_monad = sig ... end
Generic_on_monad
extends Generic
to contain various derived operators; we use it to derive the signatures of the various On_monad
modules.
Basic signatures
module type Basic0_on_monad = sig ... end
Basic0_on_monad
is the inner signature of a monadic traversal over arity-0 types.
module type Basic1_on_monad = sig ... end
Basic1_on_monad
is the inner signature of a monadic traversal over arity-1 types.
Expanded signatures
module type S1_on_monad = sig ... end
S1_on_monad
extends Generic_on_monad
with functionality that only works on arity-1 containers.
Basic signatures
Any traversable type can be turned into a Core container, using the monadic fold to implement all container functionality. The unified signature of a container with monadic traversals is S0 (arity 0) or S1 (arity 1).
To satisfy these signatures for new types, implement Basic0 or Basic1, and use the corresponding MakeN
functor.
For types that are _already_ Core containers, or types where custom implementation of the Core signature are desired, implement Basic0_container or Basic1_container, and use the MakeN_container
functors.
For modules without a Container
implementation
module type Basic0 = sig ... end
Basic0
is the minimal signature that traversable containers of arity 0 must implement to be extensible into S0.
module type Basic1 = sig ... end
Basic1
is the minimal signature that traversable containers of arity 1 must implement to be extensible into.
For modules with a Container
implementation
module type Basic0_container = sig ... end
Basic0_container
combines Basic0 and the Base container signature, and is used for extending existing containers into S0_container s.
module type Basic1_container = sig ... end
Basic1_container
combines Basic1 and the Base container signature, and is used for extending existing containers into S1_container s.
Signatures for traversable containers
module type Generic = sig ... end
Generic
is a generic interface for traversable containers, used to build S0
(arity-0) and S1
(arity-1).
module type S0 = sig ... end
S0
is a generic interface for arity-0 traversable containers.
module type S1 = sig ... end
S1
is a generic interface for arity-1 traversable containers. It also includes the extensions from Mappable.
Making traversable containers
Monadic traversal is sufficient to define fold
, and, therefore, all of the key functionality for a Base-style container. As such, our signatures and functors subsume those for building containers.
New containers
Extending existing containers with monadic traversals
module Make0_container
(I : Basic0_container) :
S0 with module Elt = I.Elt and type t = I.t
Make0_container
makes an S0 from a Basic0_container.
module Make1_container (I : Basic1_container) : S1 with type 'a t = 'a I.t
Make1_container
makes an S1 from a Basic1_container.
Combining and modifying traversable containers
Chaining
module Chain0
(Outer : S0)
(Inner : S0 with type t := Outer.Elt.t) :
S0 with module Elt = Inner.Elt and type t = Outer.t
Chain0
chains two S0 instances together, traversing each element of the outer instance with the inner instance.
Fixing the element type
Helper functions
module Helpers (M : Base.Monad.S) : sig ... end
Utility functions for building traversals.