Skip to content

morphismtech/distributors

Repository files navigation

Distributors

Unifying Parsers, Printers & Grammars

GithubWorkflowCI

This library provides mathematically inspired abstractions for coders to write parsers that can also be inverted to printers.

introduction

The term "distributor" is a synonym for "profunctor". Jean Bénabou who invented the term and originally used “profunctor,” then preferred “distributor”, which is supposed to carry the intuition that a distributor generalizes a functor in a similar way to how a distribution generalizes a function.

Bénabou in his time introduced the notions of enriched categories, bicategories as well as distributors and invented the term monad. He was lost to us on 11, February 2022 and this library is dedicated to his memory.

Since "profunctor" became the standard nomenclature, we reappropriate "distributor" to describe a profunctor on a distributive category.

This library provides a study of Monoidal profunctors, Distributors, Alternators and Filtrators. These profunctor constraints are analogous to Applicative, Alternative and Filterable functors. Examples of Distributors will include printers and parsers, and it is demonstrated how to write a single term for both. Profunctors naturally give rise to optics and this library also studies some previously discovered optics, PartialIsos, Monocles, Grates and Withers and also defines new optics, Diopters and Bifocals. Finally, an application of distributors is demonstrated by unifying Backus-Naur form grammars with invertible parsers, giving users a powerful playground for front-end language design.

previous work

The results concerning invertible parsers are a profunctorial interpretation of Invertible Syntax Descriptions by Tillman Rendel & Klaus Ostermann.

While Distributors in the library are lax distributive endoprofunctors, a mathematical treatment of strong (i.e. with invertible structure morphisms) distributors is given by Travis Squires in Profunctors and Distributive Categories.

The idea for unifying Backus-Naur grammars with parsers comes from Joachim Breitner in a post Showcasing Applicative.

The person deserving the most credit for bringing the power of optics to programming is Ed Kmett, to whom I am very grateful for teaching me a lot, with his lens library.

None of the ideas in this library are particularly original and a lot of related ideas have been explored, in Tom Ellis' product-profunctors as well as Sjoerd Visscher's one-liner and more. Such explorations are not limited to Haskell. Brandon Williams and Stephen Celis' excellent swift-parsing was also influenced by invertible parser theory.

Some optics in this library are grates, a new kind of optic, discovered by Russel O'Connor and James Deikun; monocles which are studied by Alexandre Garcia de Oliveira, Mauro Jaskelioff, and Ana Cristina Vieira de Melo in On Structuring Functional Programs with Monoidal Profunctors; and withers, discovered by Chris Penner in Composable filters using Witherable optics.

contributing

Contributors are welcome. The Issues page is a good place to communicate.

Releases

No releases published

Packages

No packages published