Skip to content
vic

xvw/preface

Preface is an opinionated library designed to facilitate the handling of recurring functional programming idioms in OCaml.

xvw/preface.json
{
"createdAt": "2019-08-22T15:45:47Z",
"defaultBranch": "master",
"description": "Preface is an opinionated library designed to facilitate the handling of recurring functional programming idioms in OCaml.",
"fullName": "xvw/preface",
"homepage": "https://ocaml-preface.github.io/preface/index.html",
"language": "OCaml",
"name": "preface",
"pushedAt": "2025-04-03T10:01:40Z",
"stargazersCount": 152,
"topics": [
"category-theory",
"functional-programming",
"ocaml",
"stdlib"
],
"updatedAt": "2025-08-16T18:49:08Z",
"url": "https://github.com/xvw/preface"
}

Preface is an opinionated library designed to facilitate the handling of recurring functional programming idioms in OCaml. Many of the design decisions were made in an attempt to calibrate, as best as possible, to the OCaml language. Trying to get the most out of the module language. The name “preface” is a nod to “Prelude”.

When learning functional programming, one is often confronted with constructs derived (or not) from category theory. Languages such as Haskell offer very complete libraries to use them, and thus, facilitate their learning. In OCaml, it often happens that these abstractions are buried in the heart of certain libraries/projects (Lwt, Cmdliner, Bonsai, Dune etc.). This is why one of the objectives of Preface is to propose tools for concretising these abstractions, at least as a pedagogical tool.

Since OCaml allows for efficient imperative programming, Preface is probably not really useful for building software. However, we (the maintainers) think that Preface can be useful for a few things:

  • technical experimentation with abstractions (especially those from the Haskell world) that allow programming in a fun style.
  • As an educational tool. Many teaching aids generally only offer the minimal interfaces to these abstractions. Preface tries to be as complete as possible.
  • It was a lot of fun to make. The last point is obviously the lightest but building Preface was really fun! So even if some people won’t see the point… we had fun making it!

The package is available on OPAM, opam install preface should be enough. (And by describing, of course, OPAM in your project OPAM file and linking it to your project in the standard way proposed by your build-system.)

If you would like to use the latest version of Preface (under development) you can use the pin mechanism.

...
depends: [
...
"preface" {pinned}
]
pin-depends: [
["preface.dev" "git+ssh://git@github.com/xvw/preface.git"]
...
]
...

The library can also be installed with [esy]!(esy.sh) using a resolution in your package.json file :

...
"dependencies": {
...
"@opam/preface":"*"
},
"resolutions": {
"@opam/preface":"xvw/preface#<commit>"
},
...

The pattern of the resolution is xvw/preface#<commit> where <commit> is mandatory and should point to a specific commit.

The library is divided into four parts (in the user area) which serve complementary purposes.

LibraryDescription
preface.specsContains all the interfaces of the available abstractions. The specifications resemble the _intf suffixed signatures found in other libraries in the OCaml ecosystem.
preface.makeContains the set of functors (in the ML sense of the term) for concretising abstractions. Schematically, a module in Preface.Make takes a module (or modules) respecting a signature described in Preface.Specs to produce a complete signature (also described in Preface.Specs).
preface.stdlibContains concrete implementations, constructs that implement abstractions described in Preface.Specs by means of the functors present in Preface.Make. This library is, at least, an example of the use of Specs and Make.
prefacePacks all libraries making Preface.Specs and Preface.Make accessible as soon as Preface is available in the current scope. And includes Preface.Stdlib (so everything in Preface.Stdlib is available from Preface).

Although Stdlib offers common and, in our view, useful implementations, the heart of Preface lies in its ability to build the concretisation of abstractions for all sorts of data structures. Here is a list of abstractions that can be built relatively easily. As you can see, the diagram is heavily inspired by the Haskell community’s Typeclassopedia.

typeclassopedia

Obviously, the set of useful abstractions is still far from being present in Preface. We have decided to privilege those for which we had a short and medium term use. But if you find that an abstraction is missing, the development of Preface is open, don’t hesitate to contribute by adding what was missing.

As for the implemented abstractions, we favoured objects that we often manipulated (that we constantly reproduced in our projects) and also those that allowed us to test certain abstractions (Predicate and Contravariant for example). Don’t hesitate to add some that would be useful for the greatest number of people!

NameDescriptionAbstractions
Approximation.OverA generalization of Const (the phantom monoid) for over approximationApplicative, Selective
Approximation.UnderSame of Over but for under approximationApplicative, Selective
ContinuationA continuation that can’t be delimitedFunctor (and Invariant), Applicative, Monad
EnvThe env comonad using Identityas inner monadFunctor (and Invariant), Comonad
EitherRepresent a disjunction between left and rightBifunctor and can be specialised for the left part; Functor (and Invariant), Alt, Applicative, Monad, Traversable through Applicative and Monad, Foldable
EquivalenceA generalization of function 'a -> 'a -> boolContravariant (and Invariant), Divisible, Decidable
FunFunction 'a -> 'bProfunctor, Strong, Choice, Closed, Semigroupoid, Category, Arrow, Arrow_choice, Arrow_apply
IdentityA trivial type constructor, type 'a t = 'aFunctor (and Invariant), Applicative, Selective, Monad, Comonad
ListThe standard list of OCamlFoldable, Functor (and Invariant), Applicative, Alternative, Selective, Monad, Monad_plus, Traversable through Applicative or Monad, Monoid (where the inner type must be fixed)
Nonempty_listA list with, at least, one elementFoldable, Functor (and Invariant), Alt, Applicative, Selective, Monad, Comonad, Traversable through Applicative or Monad, Semigroup (where the inner type must be fixed)
OptionDeal with absence of valuesFoldable, Functor (and Invariant), Applicative, Alternative, Monad, Monad_plus, Traversable through Applicative of Monad, Monoid (where the inner type must be fixed)
PredicateA generalization of function 'a -> boolContravariant (and Invariant), Divisible, Decidable
ReaderThe reader monad using Identity as inner monadFunctor (and Invariant), Applicative, Monad
ResultDeal with Ok or Error valuesBifunctor and can be specialised for the error part; Functor (and Invariant), Alt, Applicative, Monad, Traversable through Applicative and Monad, Foldable
SeqThe standard sequence of OCamlFoldable, Functor (and Invariant), Applicative, Alternative, Selective, Monad, Monad_plus, Traversable through Applicative or Monad, Monoid (where the inner type must be fixed)
StateThe state monad using Identity as inner monadFunctor (and Invariant), Applicative, Monad
StoreThe store comonad using Identityas inner monadFunctor (and Invariant), Comonad
StreamInfinite listFunctor (and Invariant), Applicative, Monad, Comonad
TracedThe traced comonad using Identityas inner monadFunctor (and Invariant), Comonad
TryA biased version of Result with exception as the error partFunctor (and Invariant), Alt, Applicative, Monad, Traversable through Applicative and Monad, Foldable
PairA pair 'a * 'bBifunctor
ValidateA biased version of Validation with exception Nonempty_list as invalid partFunctor (and Invariant), Alt, Applicative, Selective, Monad, Traversable through Applicative and Monad, Foldable
ValidationLike Result but the invalid part is a Semigroup for accumulating errorsBifunctor and can be specialized on the invalid part: Functor (and Invariant), Alt, Applicative, Selective, Monad, Traversable through Applicative and Monad, Foldable
WriterThe writer monad using Identity as inner monadFunctor (and Invariant), Applicative, Monad

As it is possible to take several paths to realise an abstraction, we decided to describe each abstraction in a dedicated sub-module. For example Option.Functor or Option.Monad to let the user choose which combinators to use.

Although it was tempting to extend the standard OCaml library with this technique:

module Preface : sig
module List : sig
include module type of List
include module type of Preface_stdlib.List
end
end

We have decided not to do this to ensure consistent documentation (not varying according to the version of OCaml one is using).

Abstractions must respect a minimum interface, however, sometimes there are several paths to describe the abstraction. For example, building a monad on a type requires a return (or pure depending on the convention in practice) and:

  • bind (>>=)
  • map and join
  • or possibly >=>

In addition, on the basis of these minimum combinators, it is possible to derive other combinators. However, it happens that these combinators are not implemented in an optimal way (this is the cost of abstraction). In the OCaml ecosystem, the use of polymorphic variants is sometimes used to give the user the freedom to implement, or not, a function by wrapping the function definition in a value of this type:

val f : [< `Derived | `Custom of ('a -> 'b)]

Instead of relying on this kind of (rather clever!) trick, we decided to rely mainly on the module language.

To make it easy to describe the embodiment of an abstraction, but still allow for the possibility of providing more efficient implementations (that propagate new implementations on aliases, such as infix operators, or functions that use these functions), Preface proposes a rather particular cut.

Each abstraction is broken down into several sub-modules:

SubmoduleRole
CoreThis module describes all the fundamental operations. For example, for a monad, we would find return, map, bind, join and compose_left_to_right
OperationThe module contains the set of operations that can be described using the Core functions.
InfixThe module contains infix operators built on top of the Core and Operation.
SyntaxThe module contains the let operators (such as let* and let+ for example), built with the Core and Operation functions.

Sometimes it happens that some modules are not present (e.g. when there are no infix operators) or sometimes some additional modules are added, but in general the documentation is clear enough.

The functors exposed in Make allow you to build each component one by one (Core, Operation, using Core, and Infix and Syntax using Core and Operation) and then group all these modules together to form the abstraction. Or use the Happy Path, which generally offers a similar approach to functors which builds Core but builds the whole abstraction.

Here is an example of the canonical flow of concretisation of an abstraction:

module hierarchy

Although it is likely that the use of the Happy Path covers a very large part of the use cases and that it is not necessary to concretise every abstraction by hand, it is still possible to do so.

In addition, it is sometimes possible to describe one abstraction by specialising another. In general, these specialisations follow this naming convention: From_name (More_general_module) or To_name (Less_general_module) and sometimes you can build a module on top of another, for example Selective on top of Applicative and the naming follows this convention: Over_name (Req), ie Selective.Over_applicative

Project nameDescriptionLinks
YOCamlYOCaml is a static blog generator that essentially takes advantage of Preface’s Freer, Result, Validation and Arrow.Github repository
MuhokamaA simple forum built on top of Dream, Caqti, Omd, Preface, Cmdliner and other useful OCaml librariesGithub repository
JaylangA semantically typed language that uses Preface for abstractions including Arrow, Writer, Nonempty_list, and more.Github repository

You use Preface for one of your projects and you want to be in this list? Don’t hesitate to open a PR or fill an issue, we’d love to hear from you.

Preface is a fun project to develop and we have learned a lot from it. We hope you find it useful and/or enjoyable to use. We are open to any improvements and open to external contributions!

We received a lot of help during the development of Preface. Feel free to go to the [CREDITS]!(CREDITS.md) page to learn more.