A Divertimento on Monadplus and Nondeterminism1
Total Page:16
File Type:pdf, Size:1020Kb
A Divertimento on MonadPlus and Nondeterminism1 Tarmo Uustalu Institute of Cybernetics at Tallinn University of Technology, Akadeemia tee 21, 12618 Tallinn, Estonia Abstract In the Haskell community, there is a controversy about what the laws of the MonadPlus type constructor class ought to be. We suggest that there is no single universal correct answer, however there is a universal method. Important classes of notions of finitary nondeterminism are captured by what we call monads of semigroups and monads of monoids, but also by monads of different specializations of semigroups and monoids. Some of these specializations of monads are exotic and amusing too. Keywords: monads, algebraic operations, semigroups, nondeterminism 1. Introduction Monads are a widely used in functional programming, especially Haskell, as an ab- straction of computational effects. The mathematics of monads is very well understood, as they are a central concept of category theory. But Haskell's standard library features also a more specific type constructor class MonadPlus to cover a particular variety of effects, namely different notions of finitary nondeterminism. Oddly, the mathematics of MonadPlus has been unclear. The question of what the \right" laws of MonadPlus ought to be has been open from the beginning and there is no commonly agreed answer. Moreover, until the recent work by Rivas et al. [12], there have been no attempts to justify any proposal by mathematical considerations. In this note, we pursue one principled way of addressing this question. We take inspiration from the algebraic operations approach of Plotkin and Power [11]. We sug- gest that there is no single universal correct answer, rather there is a universal method. Important classes of notions of finitary nondeterministic choice are captured by what we call monads of semigroups and monads of monoids, but also by monads of different specializations (varieties) of semigroups and monoids. We argue that such special types of monads, uniformly defined following a certain pattern, are well-motivated, since they enjoy neat mathematical properties. At the same time, one to be specific about what idea of finitary nondeterminism is had in mind, i.e., what exactly is meant to be ob- servable about nondeterministic computations, following a clear method. Some of these 1Dedicated to Jos´eNuno Oliveira on the occasion of his 60th birthday. Email address: [email protected] (Tarmo Uustalu) Preprint submitted to Elsevier September 20, 2015 specializations of monads are quite exotic, but all of them are instructive and fun to study. I have titled this piece a divertimento, since I consider it lighthearted; certainly it was intended so. I have tried to make it entertaining, yet serious, and thus fit for a festive social function like a birthday event is. And above all I hope I found a beautiful theme which is easy to memorize. I have learned a lot from you, Jos´eNuno, and the thing I value above all from our interactions is your overflowing enthusiasm about beautiful mathematics and music. The paper is organized as follows. First, in Section 2, we review the MonadPlus class of Haskell. In Section 3, we introduce monads of semigroups and show that this is a well-motivated concept with neat mathematical properties that also corresponds to a natural class of notions of finitary nondeterminism. In Section 4, we introduce monads of monoids as a similar concept with analogous properties. In Section 5, we discuss monads of some specializations of semigroups to capture narrower classes of notions of nondeterminism (where less and less operational detail is observable). In Section 6, we point to the closest related work, to conclude in Section 7. We assume that the reader is knowledgeable about monads and their application to programming with effects. To keep the presentation simple, especially concerning examples, we use Set as the base category throughout the presentation. But any category with finite products would work for most of our purposes. 2. The MonadPlus class of Haskell The Control.Monad module of Haskell's standard library provides a type class called MonadPlus [3]. Its definition says that a MonadPlus instance is a Monad instance equipped with two polymorphic functions mzero and mplus. class Monad m => MonadPlus m where mzero :: m a mplus :: m a -> m a -> m a MonadPlus is thus a subclass of Monad. It is intended as an abstraction of notions of finitary nondeterminism rather than of notions of just any type of effectful computation. mzero is the operation of nullary choice (choice with no options) and mplus is binary choice. But MonadPlus has also been applied to organize search with backtracking, sometimes regarded as \don't know" nondeterminism, in opposition to normal (demonic) \don't care" nondeterminism. The most important intended MonadPlus instance is the list monad, with the empty list and concatenation of lists as mzero and mplus. Other wanted instances include, e.g., the combination of the list monad with the state monad for some state set. The maybe monad is seen by some as an intended instance, as a monad to use for backtracking, while others want to exclude it, perhaps arguing that it should belong to a different type class with member functions of the same types but subject to a different set of laws. A remarkable controversy about MonadPlus is that there is no agreement about the \right" laws (this question has been discussed in countless mailinglist threads, wiki pages etc., see, e.g., [13, 5, 4]). It seems generally accepted that mzero and mplus should obey the laws of a monoid, i.e., the left and right unital laws and associativity: 2 mzero `mplus` c == c c `mplus` mzero == c (c `mplus` c') `mplus` c'' == c `mplus` (c' `mplus` c'') And also, mzero should be a left zero of bind and bind should right distribute over mplus2: mzero >>= k == mzero (c `mplus` c') >>= k == (c >>= k) `mplus` (c' >>= k) (Right distributivity fails for the maybe monad.) Some require in addition that mzero be a right zero and bind left distribute over mplus: c >>= \ _ -> mzero == mzero c >>= \ a -> (k a `mplus` k' a) == (c >>= k) `mplus` (c >>= k') But left distributivity fails already for the list monad. Among further laws that one may want to impose for some purposes are commuta- tivity and idempotence of mplus. c `mplus` c' == c' `mplus` c c `mplus` c == c Of course these fail for the list monad. But lists may be used as representations for finite sets and under this view commutativity and idempotence hold on the level of denotations. Yet another law sometimes suggested is the following so-called left catch law. return a `mplus` c == return a This is satisfied by the maybe monad but falsified by the list monad. Together with right distributivity, left catch gives k a `mplus` (c >>= k) == k a which is unreasonably strong. In this paper, we aim to give a well-motivated method for answering the question of what the laws of the MonadPlus class should be. We concentrate first on the case of where only mplus is present and justify a relatively weak axiomatization (only associativity together with as little more as one can feasibly get away with|it turns out that there are very good reasons to include right distributivity). Then we add mzero. And then we look at a number of laws that can be included optionally in order to capture narrower classes of notions of nondeterminism. 2Confusingly, this law is called left distributivity in most documents about MonadPlus. 3 3. Monads of semigroups We follow the algebraic operations approach of Plotkin and Power [11]. We take interest in monads that \support" some algebraic operations. We say that a monad of semigroups is a monad T = (T; η; µ) with a family of maps ⊕X : TX ×TX ! TX natural in X such that ⊕ is associative and µ is right distributive over ⊕: ⊕ ×TX µX ×µX (TX × TX) × TX X / TX × TX T (TX) × T (TX) / TX × TX αT X;T X;T X TX × (TX × TX) ⊕X ⊕TX ⊕X TX×⊕X ⊕X µX TX × TX / TX T (TX) / TX (c ⊕ c0) ⊕ c00 = c ⊕ (c0 ⊕ c00) µ (c ⊕ c0) = µ c ⊕ µ c0 The right distributivity law can also be expressed in terms of (−)∗ (the Kleisli extension operation) rather than µ and then ensures also naturality of ⊕: k∗ (c ⊕ c0) = k∗ c ⊕ k∗ c0 We claim that monads of semigroups are canonical in several ways and make a good abstraction of those notions of finitary nondeterminism based on binary choice where the temporal order of binary choices in a computation (the interior of the computation tree) is not observable. It is immediate from the definition that a monad of semigroups (T; ⊕) delivers semi- groups in the sense that, for any X,(T X; ⊕X ) is a semigroup|this is ensured by the associativity law. Moreover, right distributivity says that µX is a semigroup homomor- phism between (T (TX); µTX ) and (T X; µX ). Abstractly, while a monad is nothing but a monoid in the monoidal category ([Set; Set]; Id; ·), a monad of semigroups is the same thing as a right near-semiring in the right near-semiring category ([Set; Set]; ×; Id; ·). This becomes clear when we write the laws of ⊕ on the level of functors: ⊕×T µ×µ (T × T ) × T / T × T T · T × T · T / T × T αT;T;T T × (T × T ) ⊕X (T × T ) · T ⊕ T ×⊕ ⊕·T ⊕ µ T × T / TX T · T / T But the more interesting properties of a monad of semigroups pertain to its Eilenberg- Moore category and Kleisli category. Also, the initial monad of semigroups stands out by additional special properties. 4 The Eilenberg-Moore category of a monad of semigroups First, algebras of a monad of semigroups (T; ⊕) are semigroups in the sense of there being a functor F : EM(T) ! Sgrp preserving carriers: F EM(T) / Sgrp FF z FF zz FF z U FF zz U F# }zz C The semigroup structure : A × A ! A for a T-algebra (A; α) is defined by ηA×ηA ⊕A α = A × A / TA × TA / TA / A The converse is also true.