Bird–Meertens formalism explained
The Bird–Meertens formalism (BMF) is a calculus for deriving programs from program specifications (in a functional programming setting) by a process of equational reasoning. It was devised by Richard Bird and Lambert Meertens as part of their work within IFIP Working Group 2.1.
It is sometimes referred to in publications as BMF, as a nod to Backus–Naur form. Facetiously it is also referred to as Squiggol, as a nod to ALGOL, which was also in the remit of WG 2.1, and because of the "squiggly" symbols it uses. A less-used variant name, but actually the first one suggested, is SQUIGOL.Martin and Nipkow provided automated support for Squiggol development proofs, using the Larch Prover.[1]
Basic examples and notations
Map is a well-known second-order function that applies a given function to every element of a list; in BMF, it is written
:
f*[e1,...,en]=[f e1,...,f en].
Likewise, reduce is a function that collapses a list into a single value by repeated application of a binary operator. It is written / in BMF.Taking
as a suitable binary operator with neutral element
e, we have
⊕ /[e1,...,en]=e ⊕ e1 ⊕ ... ⊕ en.
Using those two operators and the primitives
(as the usual addition), and
(for list concatenation), we can easily express the sum of all elements of a list, and the flatten function, as
and
, in
point-free style. We have:
{\rmsum} [e1,...,en]=+/[e1,...,en]=0+e1+...+en=\sumkek.
{\rmflatten} [l1,...,ln]=++/[l1,...,ln]=[]++ l1++...++ ln=theconcatenationofalllistslk.
Similarly, writing
for
functional composition and
for
conjunction, it is easy to write a function testing that all elements of a list satisfy a predicate
p, simply as
{\rmall} p=(\land/) ⋅ (p*)
:
\begin{align}
{\rmall} p [e1,...,en]&=(\land/) ⋅ (p*) [e1,...,en]\\&=\land/(p*[e1,...,en])
\\&=\land/[p e1,...,p en]
\\&=p e1\land...\landp en
\\&=\forallk . p ek.
\end{align}
Bird (1989) transforms inefficient easy-to-understand expressions ("specifications") into efficient involved expressions ("programs") by algebraic manipulation. For example, the specification "
" is an almost literal translation of the maximum segment sum problem,
[2] but running that functional program on a list of size
will take time
in general. From this, Bird computes an equivalent functional program that runs in time
, and is in fact a functional version of
Kadane's algorithm.
The derivation is shown in the picture, with computational complexities[3] given in blue, and law applications indicated in red.Example instances of the laws can be opened by clicking on [show]; they use lists of integer numbers, addition, minus, and multiplication. The notation in Bird's paper differs from that used above:
,
, and
correspond to
,
, and a generalized version of
above, respectively, while
and
compute a list of all prefixes and suffixes of its arguments, respectively. As above, function composition is denoted by "
", which has lowest
binding precedence. In the example instances, lists are colored by nesting depth; in some cases, new operations are defined ad hoc (grey boxes).
The homomorphism lemma and its applications to parallel implementations
A function h on lists is called a list homomorphism if there exists an associative binary operator
and neutral element
such that the following holds:
\begin{align}
&h []&&= e\\
&h (l++ m)&&= h l ⊕ h m.\end{align}
The homomorphism lemma states that h is a homomorphism if and only if there exists an operator
and a function
f such that
.
A point of great interest for this lemma is its application to the derivation of highly parallel implementations of computations. Indeed, it is trivial to see that
has a highly parallel implementation, and so does
— most obviously as a binary tree. Thus for any list homomorphism
h, there exists a parallel implementation. That implementation cuts the list into chunks, which are assigned to different computers; each computes the result on its own chunk. It is those results that transit on the network and are finally combined into one. In any application where the list is enormous and the result is a very simple type – say an integer – the benefits of parallelisation are considerable. This is the basis of the
map-reduce approach.
See also
References
- algorithmics --> . 1986 . Lambert . Meertens . Lambert Meertens . Algorithmics: Towards programming as a mathematical activity . https://ir.cwi.nl/pub/2686 . de Bakker . J.W. . Hazewinkel . M. . Lenstra . J.K. . Mathematics and Computer Science . CWI Monographs . 1 . 289–334 . North-Holland.
- exercises --> . 1987 . Lambert . Meertens . Lambert Meertens . Richard . Bird . Richard Bird (computer scientist) . Two Exercises Found in a Book on Algorithmics . North-Holland .
- exploration --> . 1988 . Roland . Backhouse . Roland Carl Backhouse . An Exploration of the Bird-Meertens Formalism .
- identities --> . 1989 . Richard S. . Bird . Richard Bird (computer scientist) . Algebraic Identities for Program Calculation . The Computer Journal . 32 . 2 . 122–126 . 10.1093/comjnl/32.2.122 . free .
- maximum-segment-sum --> . 1993 . Murray . Cole . Parallel Programming, List Homomorphisms and the Maximum Segment Sum Problem . Parallel Computing: Trends and Applications, PARCO 1993, Grenoble, France . 489–492 .
- relational-theory-datatypes --> . 1993 . Roland . Backhouse . Roland Carl Backhouse . Paul . Hoogendijk . 7-42 . Elements of a Relational Theory of Datatypes . 978-3-540-57499-6 . 10.1007/3-540-57499-9_15 .
- bunkenburg1994 --> . 1994 . Alexander . Bunkenburg . O'Donnell . John T. . Hammond . Kevin . The Boom Hierarchy . Functional Programming, Glasgow 1993: Proceedings of the 1993 Glasgow Workshop on Functional Programming, Ayr, Scotland, 5–7 July 1993 . Springer . London . 1–8 . 978-1-4471-3236-3 . 10.1007/978-1-4471-3236-3_1 . PDF.
- AOP --> . 1997 . Bird . Richard . Richard Bird (computer scientist) . Oege . de Moor . Algebra of Programming . International Series in Computing Science . 100 . Prentice Hall . 0-13-507245-X.
- squiggol-history --> . 2020 . Gibbons . Jeremy . Jeremy Gibbons . The School of Squiggol: A History of the Bird-Meertens Formalism . Formal Methods (Workshop on History of Formal Methods) . Troy Astarte . Springer . LNCS . 12233 . 10.1007/978-3-030-54997-8_2 .
Notes and References
- https://www21.in.tum.de/~nipkow/pubs/squiggol.html . Ursula Martin . Tobias Nipkow . Ursula Martin . Tobias Nipkow. Automating Squiggol . Manfred Broy . Cliff B. Jones . Manfred Broy . Cliff Jones (computer scientist) . Proc. IFIP WG 2.2/2.3 Working Conference on Programming Concepts and Methods . North-Holland . 233 - 247 . Apr 1990 .
- Where
,
, and
returns the largest value, the sum, and the list of all segments (i.e. sublists) of a given list, respectively.
- Each expression in a line denotes an executable functional program to compute the maximum segment sum.