package alg_structs

  1. Overview
  2. Docs

Endo is a semigroup where the operator is the composition of functions with input and output of the same type.

Or, to paraphrase the Haskell docs, Endo implements "the semigroup of endomorphisms under composition". "Endomorphism" just meaning a morphism with the same object for its source and target, i.e., (here) a function with input and output of same type.

E.g. using the first-order module generator Endo.make, we can make the Endo semigroup over functions of type string -> string thus:

# module E = (val Semigroup.Endo.make "");;
module E :
sig
  type t = string -> string
  val op : t -> t -> t
  val ( * ) : t -> t -> t
  val concat : t NonEmptyList.t -> t
end;;

# let comp = E.( (fun y -> "Hello, " ^ y) * (fun x -> x ^ "!") );;
val comp : E.t = <fun>;;

# comp "OCaml";;
- : string = "Hello, OCaml!"
module Make (T : Triv.S) : S with type t = T.t -> T.t

Make (T) is a module implementing the Endo semigroup for functions over type T.t

val make : 'a Alg_structs__.Util.proxy -> (module S with type t = 'a -> 'a)

make (Proxy : t Util.proxy) is a first-class module implementing the Endo semigroup for functions (t -> t).

Note that Proxy is used only to convey the type. See Util.proxy.

You can lift the result back into the module like so:

# module E = (val Semigroup.Endo.make (Util.Proxy : int proxy));;
module E :
sig
  type t = int -> int
  val op : t -> t -> t
  val ( * ) : t -> t -> t
  val concat : t NonEmptyList.t -> t
end
OCaml

Innovation. Community. Security.