package travesty

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Monadic traversal.

Traversable provides signatures and functors for containers and data structures that can be 'traversed': mapped across with a monadic side-effect. It 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

Traversable_intf contains the signatures for Traversable.

include module type of Traversable_intf

The generic signature

As with Mappable, we define the signature of traversable structures in an arity-generic way, then specialise it for arity-0 and arity-1 types.

module type Generic = sig ... end

Generic describes monadic traversal on either an arity-0 or arity-1 type.

Basic signatures

module type S0 = sig ... end

S0 is the signature of a monadic traversal over arity-0 types.

module type S1 = sig ... end

S1 is the signature of a monadic traversal over arity-1 types.

Building containers from traversable types

Any traversable type can be turned into a Core container, using the monadic fold to implement all container functionality. The unified signature of a Core container with monadic traversals is S0_container (arity 0) or S1_container (arity 1).

To satisfy these signatures for new types, implement Basic0 or Basic1, and use the corresponding Make functor in Traversable.

For types that are _already_ Core containers, or types where custom implementation of the Core signature are desired, implement Basic_container0 or Basic_container1, and use the Extend functors.

Input signatures

module type Basic0 = sig ... end

Basic0 is the minimal signature that traversable containers of arity 0 must implement to be extensible into S0_container.

module type Basic_container0 = sig ... end

Basic_container0 combines Basic0 and the Core container signature, and is used for extending existing containers into S0_containers.

module type Basic1 = sig ... end

Basic1 is the minimal signature that traversable containers of arity 1 must implement to be extensible into.

module type Basic_container1 = sig ... end

Basic_container1 combines Basic1 and the Core container signature, and is used for extending existing containers into S1_containers.

Helper signatures

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.

module type On_monad1 = sig ... end

On_monad1 extends Generic_on_monad with functionality that only works on arity-1 containers.

module type Generic_container = sig ... end

Generic_container is a generic interface for traversable containers, used to build Container0 (arity-0) and Container1 (arity-1).

Signatures for traversable containers

module type S0_container = sig ... end

S0_container is a generic interface for arity-0 traversable containers.

module type S1_container = sig ... end

S1_container is a generic interface for arity-1 traversable containers. It also includes the extensions from Mappable.

Making containers

Monadic traversal is sufficient to define fold, and, therefore, all of the key functionality for a Core-style container. As such, we expose functors for building traversable Core-style containers.

module Make_container0 (I : Basic0) : S0_container with module Elt = I.Elt and type t := I.t

Make_container0 makes a S0_container from a Basic0.

module Make_container1 (I : Basic1) : S1_container with type 'a t := 'a I.t

Make_container1 makes a S1_container from a Basic1.

Extending existing containers

We also expose functors for adding monadic traversals to existing containers.

module Extend_container0 (I : Basic_container0) : S0_container with module Elt = I.Elt and type t := I.t

Extend_container0 makes a S0_container from a Basic_container0.

module Extend_container1 (I : Basic_container1) : S1_container with type 'a t := 'a I.t

Extend_container1 makes a S1_container from a Basic_container1.

Chaining together traversables

module Chain0 (Outer : S0_container) (Inner : S0_container with type t := Outer.Elt.t) : S0_container with module Elt = Inner.Elt and type t := Outer.t

Chain0 chains two S0_container instances together, traversing each element of the outer instance with the inner instance.

Helper functions

module Helpers (M : Base.Monad.S) : sig ... end

Utility functions for building traversals.

OCaml

Innovation. Community. Security.