From Monoids to Near-Semirings: the Essence of Monadplus and Alternative

From Monoids to Near-Semirings: the Essence of Monadplus and Alternative

From monoids to near-semirings: the essence of MonadPlus and Alternative Exequiel Rivas Mauro Jaskelioff Tom Schrijvers CIFASIS-CONICET KU Leuven, Belgium Universidad Nacional de Rosario, Argentina [email protected] jaskelioff@cifasis-conicet.gov.ar [email protected] Abstract 2. The codensity transformation CodT m provides a continuation- It is well-known that monads are monoids in the category of endo- based representation for a monad m. functors, and in fact so are applicative functors. Unfortunately, newtype CodT m a = CodT (8x:(a ! m x) ! m x) the benefits of this unified view are lost when the additional non- determinism structure of MonadPlus or Alternative is required. rep :: Monad m ) m a ! CodT m a This article recovers the essence of these two type classes by rep v = CodT (v>>=) extending monoids to near-semirings with both additive and multi- abs :: Monad m ) CodT m a ! m a plicative structure. This unified algebraic view enables us to gener- abs (CodT c) = c return ically define the free construction as well as a novel double Cayley representation that optimises both left-nested sums and left-nested Since CodT m is a representation for m, we can lift m- products. computations to CodT m, compute in that monad, and when we are finished go back to m using abs. This change of repre- Keywords monoid, near-semiring, monad, monadplus, applica- sentation is useful because it turns left-nested binds into right- tive functor, alternative, free construction, Cayley representation nested binds [10, 22]. This is very convenient for monads (like the free monad) where left-nested binds are costly and right- 1. Introduction nested binds are cheap. The monad interface provides a basic structure for computations: instance Monad (CodT m) where class Monad m where return x = CodT (λk ! k x) return :: a ! m a CodT c >>= f = CodT (λk ! c (λa ! (>>=) :: m a ! (a ! m b) ! m b let CodT g = f a in g k)) where the operations return, for injecting values, and >>=, for se- quentially composing computations, are subject to the three monad Both results can be derived by viewing a monad as a monoid in laws. Study of this structure has led to many insights and applica- a monoidal category. The free monad is just the free monoid in tions. Two of these are especially notable: that category and the codensity transformation arises as the Cayley representation of that monoid [17]. Moreover, useful generality is 1. The free instance of the monad interface, the free monad, has gained by this approach, as not only monads are monoids, but also many applications in defining new monads and extending the applicative functors and arrows. capabilities of existing ones, e.g., in the form of algebraic effect While the monad interface is well understood and comes handlers [16]. with many useful results, it is also very limiting. When dealing with specific computations, we always require additional opera- data Free f a = Return x j Op (f (Free f a)) tions. A prominent example is non-determinism that occurs, e.g., instance Functor f ) Monad (Free f ) where in logic programming languages and parser combinators. Non- return x = Return x deterministic computations involve two additional operations: a Return x >>= f = f x failing computation (mzero) and a non-deterministic choice be- Op op >>= f = Op (fmap (>>=f ) op) tween two computations (mplus). These additional operations are captured in Haskell by the MonadPlus type class: class Monad m ) MonadPlus m where Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed mzero :: m a for profit or commercial advantage and that copies bear this notice and the full citation mplus :: m a ! m a ! m a on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or This type class comes with five additional laws that govern the republish, to post on servers or to redistribute to lists, requires prior specific permission interaction between the operations. and/or a fee. Request permissions from [email protected]. It is not difficult to see that the above two constructions for the PPDP ’15, July 14–16, 2015, Siena, Italy. Copyright is held by the owner/author(s). Publication rights licensed to ACM. Monad interface do not work for the MonadPlus interface. Firstly, ACM 978-1-4503-3516-4/15/07. $15.00. the free monad has no provision for the two additional operations http://dx.doi.org/10.1145/2790449.2790514 of MonadPlus and the five additional laws. Secondly, the codensity construction does not optimise left- Using type classes, we can describe monoids in Haskell as nested uses of mplus. Consider for instance, the following program follows: due to Fischer [5]: class Monoid m where anyof :: MonadPlus m ) [a ] ! m a mempty :: m anyof [ ] = mzero mappend :: m ! m ! m anyof (x : xs) = anyof xs `mplus` return x Here, mempty is the unit element and mappend is the multi- If we instantiate m with the list monad, whose MonadPlus instance plication. Instances of this class are required to satisfy the monoid is defined as follows laws. However, these are not enforced by Haskell, and it is left to the programmer to verify them. instance MonadPlus [] where There are two important constructions that are important for this mzero = [ ] paper: free monoids and the Cayley representation for monoids. mplus = (++) Free Monoids The notion of free monoid is defined in terms of we obtain the naive-reverse program, which has a quadratic running monoid homomorphisms. A monoid homomorphism is a function time. from one monoid to another that preserves the monoid structure. Let us now consider what happens if we use CodT [] instead. While there is no established CodT instance for MonadPlus, we Definition 2.1. A monoid homomorphism from a monoid (M; ⊗M ; 1 can easily provide one in terms of the underlying operations: eM ) to a monoid (N; ⊗N ; eN ) is a function f : M ! N such that 0 0 f(eM ) = eN and f(m ⊗M m ) = f(m) ⊗N f(m ). instance MonadPlus m ) MonadPlus (CodT m) where mzero = CodT (λk ! mzero) Now we can define the notion of free monoid. CodT p `mplus` CodT q = CodT (λk ! p k `mplus` q k) Definition 2.2. The free monoid over a set X is a monoid ∗ ∗ However, there is no improvement by running the computation (X ; ⊗∗; e∗) together with a function inj : X ! X such that on CodT []. The problem is that CodT [] just delegates the for every monoid (M; ⊗M ; eM ) and function h : X ! M, there ∗ MonadPlus operations to the underlying instance. This obviously exists a unique monoid homomorphism h : X ! M such that does not improve the running time. h ◦ inj = h. This paper provides a new algebraic understanding of the oper- A concrete representation for the free monoid over a set X are ations of the MonadPlus type class, one that enables us to derive lists with elements of that set; concatenation is its multiplication both the free structure and an optimised Cayley-like representation. and the empty list is its unit. As we have argued, the monoid view is insufficient for this pur- The free monoid construction extends to a functor. That is, for pose; we require a richer algebraic structure that augments monoids every function f : X ! Y , we define the monoid homomorphism with additional operations. This algebraic structure is that of a near- f ∗ : X∗ ! Y ∗ as f ∗ = inj ◦ f. semiring. For every monoid (M; ⊗ ; e ), the monoid morphism Specifically, our contributions are as follows: M M id : M ∗ ! M • We present a generalised form of near-semirings (Section 3), M and provide generic definitions for its free construction and a behaves as an evaluation algebra in the following sense: M ∗ rep- novel double Cayley representation. resents the syntax of programs constructed from the monoid opera- • tions and elements of M. The algebra idM simply gives semantics We establish that both MonadPlus (Section 4) and Alternative ∗ (Section 5) are instances of this generalised notion, and we to these programs by replacing the syntactic operations in M with specialise the constructions for both cases. the corresponding monoid operations in M. • We demonstrate the use of the constructions on two examples: Monoid Representation A representation for a given monoid combinatorial search and interleaving parsers (Section 6). (M; ⊗M ; eM ) is a monoid (R(M); ⊗R(M); eR(M)), together with functions rep : M ! R(M) and abs : R(M) ! M such that the There is quite a bit of related work; this is discussed in Section 7. following diagram commutes. rep∗ 2. Monoids and Near-Semirings M ∗ / R(M)∗ In this section we introduce ordinary monoids and near-semirings. That is, we present monoids and near-semirings over sets. idM idR(M) MR(M) 2.1 Background: Monoids o abs A monoid (M; ⊗; e) is a triple consisting of a set M, together with Intuitively, the diagram states that running a monoid program on an operation ⊗ : M × M ! M and an element e 2 M such that M is the same as first interpreting it as a monoid program on the the following axioms hold for all a, b, and c 2 M: representation R(M), running it there, and then abstracting the a ⊗ e = a (1) result back into M. e ⊗ a = a (2) Cayley Representation The Cayley representation of a monoid a ⊗ (b ⊗ c) = (a ⊗ b) ⊗ c (3) is an efficient representation of that monoid with a constant time multiplication.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us