package alg_structs

  1. Overview
  2. Docs

Make (S) is a semigroup where op a b is...

  • None if both a and b are None
  • Some v if only one of a or b are Some v
  • Some (S.op a' b') if b is Some b' and a is Some a'

This enables chains of associations over optional values that preserves any values that may be present. E.g.,

# module O = Semigroup.Option.Make ((val Semigroup.make ( * )));;
module O :
sig
  type t = int option
  val op : t -> t -> t
  val ( * ) : t -> t -> t
  val concat : t NonEmptyList.t -> t
end

# O.(Some 2 * None * None * Some 2);;
- : O.t = Option.Some 4

Parameters

module S : S

Signature

include Seed with type t = S.t Option.t
type t = S.t Option.t

The principle (and sole) type.

We can think of this set-theoretically as the carrier set of the algebraic structure or category-theoretically as the single object in the category, with each element being a morphism t -> t.

val op : t -> t -> t

op x y is an associative operation over all elements x and y of type t. Category-theoretically, this is the composition of morphisms.

val (*) : t -> t -> t

The infix version of op.

val concat : t NonEmptyList.t -> t

concat xs is the concatenation of all elements of xs into a single value using op.

This is equivalent to List.fold_right op (NonEmptyList.tl xs) (NonEmptyList.hd xs).

OCaml

Innovation. Community. Security.