ArticlePDF Available

Algebraic Operations and Generic Effects

Authors:

Abstract

Given a complete and cocomplete symmetric monoidal closed category V and a symmetric monoidal V-category C with cotensors and a strong V-monad T on C, we investigate axioms under which an Ob C-indexed family of operations of the form αx :(Tx)v →(Tx)w provides semantics for algebraic operations on the computational λ-calculus. We recall a definition for which we have elsewhere given adequacy results, and we show that an enrichment of it is equivalent to a range of other possible natural definitions of algebraic operation. In particular, we define the notion of generic effect and show that to give a generic effect is equivalent to giving an algebraic operation. We further show how the usual monadic semantics of the computational λ-calculus extends uniformly to incorporate generic effects. We outline examples and non-examples and we show that our definition also enriches one for call-by-name languages with effects.
A preview of the PDF is not available
... This separation breaks algebraicity, which states that operations commute with sequencing. Modeling scoped effects as handlers [PP03] has been proposed as a way of encoding scoped effects in an algebraic framework. However, this comes at the cost of modularity [YPW + 22] (and thus expressiveness). ...
... While monads [Mog89, Mog91,Wad95] have long been the go-to approach for modelling effects, algebraic effects & handlers [PP03,PP09] are steadily gaining more traction. They offer a more structured and modular approach to composing effects, based on an algebraic model. ...
... • local for creating local variables (local state); • call for creating a scope in a nondeterministic program, where branches can be cut using the algebraic cut operation; • depth for bounding the depth in the depth-bounded search strategy; Following Wu et al. [WSH14], we call them scoped operations. Plotkin and Power [PP03] have already realised that algebraic effects are unable to represent so-called effect deconstructors (e.g. scoped effects) and propose to model them as handlers. ...
Article
Algebraic effects & handlers have become a standard approach for side-effects in functional programming. Their modular composition with other effects and clean separation of syntax and semantics make them attractive to a wide audience. However, not all effects can be classified as algebraic; some need a more sophisticated handling. In particular, effects that have or create a delimited scope need special care, as their continuation consists of two parts-in and out of the scope-and their modular composition introduces additional complexity. These effects are called scoped and have gained attention by their growing applicability and adoption in popular libraries. While calculi have been designed with algebraic effects & handlers built in to facilitate their use, a calculus that supports scoped effects & handlers in a similar manner does not yet exist. This work fills this gap: we present λsc\lambda_{\mathit{sc}}, a calculus with native support for both algebraic and scoped effects & handlers. It addresses the need for polymorphic handlers and explicit clauses for forwarding unknown scoped operations to other handlers. Our calculus is based on Eff, an existing calculus for algebraic effects, extended with Koka-style row polymorphism, and consists of a formal grammar, operational semantics, a (type-safe) type-and-effect system and type inference. We demonstrate λsc\lambda_{\mathit{sc}} on a range of examples.
... This complexity led us to introduce the concept of pre-operations of a strong functor. Preoperations represent a natural extension of Manes' notion of operation of a monad [Man66] and Plotkin and Power's notion of algebraic operation [PP01b,PP03]. We chose to consider pre-operations over functors, not just on monads, to establish a connection between the operations of an algebraic monad and those of its signature functors. ...
... Remark 6.9 (Discussion and related work). Pre-operations of a strong functor F are related to Plotkin and Power's algebraic operations [PP01b,PP03] in the sense that their assignment to F -algebras are the appropriate version of algebraic operations for functors. Moreover, when considered over a strong monad T they correspond to generic effects of type I → T v (i.e., Kleisli maps of type I → v, where I is the unit object of the monoidal structure). ...
Article
Inspired by the seminal work of Hyland, Plotkin, and Power on the combination of algebraic computational effects via sum and tensor, we develop an analogous theory for the combination of quantitative algebraic effects. Quantitative algebraic effects are monadic computational effects on categories of metric spaces, which, moreover, have an algebraic presentation in the form of quantitative equational theories, a logical framework introduced by Mardare, Panangaden, and Plotkin that generalises equational logic to account for a concept of approximate equality. As our main result, we show that the sum and tensor of two quantitative equational theories correspond to the categorical sum (i.e., coproduct) and tensor, respectively, of their effects qua monads. We further give a theory of quantitative effect transformers based on these two operations, essentially providing quantitative analogues to the following monad transformers due to Moggi: exception, resumption, reader, and writer transformers. Finally, as an application, we provide the first quantitative algebraic axiomatizations to the following coalgebraic structures: Markov processes, labelled Markov processes, Mealy machines, and Markov decision processes, each endowed with their respective bisimilarity metrics. Apart from the intrinsic interest in these axiomatizations, it is pleasing they have been obtained as the composition, via sum and tensor, of simpler quantitative equational theories.
... UniChorn is a port of HasChor into the Unison programming language. To implement EPP, it uses the Unison feature of abilities, better known in the literature as algebraic effects [Plotkin and Power 2003;Plotkin and Pretnar 2013]. This implementation approach, which was also recently proposed by Shen and Kuper [2024], can be thought of as a generalization of the free-monad approach. ...
Preprint
Choreographic programming (CP) is a paradigm for implementing distributed systems that uses a single global program to define the actions and interactions of all participants. Library-level CP implementations, like HasChor, integrate well with mainstream programming languages but have several limitations: Their conditionals require extra communication; they require specific host-language features (e.g., monads); and they lack support for programming patterns that are essential for implementing realistic distributed applications. We make three contributions to library-level CP to specifically address these challenges. First, we propose and formalize enclaves and multiply-located values, which enable efficient conditionals in library-level CP without redundant communication. Second, we propose end-point projection as dependency injection, a design pattern that enables library-level CP in host languages without support for monads. Third, we propose census polymorphism, a technique for abstracting over the number of participants in a choreography. We demonstrate these contributions via implementations in Haskell, Rust, and TypeScript.
... Equational reasoning and algebraic methods are widespread in all areas of computer science, and in particular in program semantics. Indeed, initial algebra semantics and monads are cornerstones of the modern theory of functional programming and are used to reason about inductive definitions, computational effects and specifications in a formal way (see, e.g., [PP03,RT93,Mog91,HP07]). ...
Article
We present a generalisation of the theory of quantitative algebras of Mardare, Panangaden and Plotkin where (i) the carriers of quantitative algebras are not restricted to be metric spaces and can be arbitrary fuzzy relations or generalised metric spaces, and (ii) the interpretations of the algebraic operations are not required to be nonexpansive. Our main results include: a novel sound and complete proof system, the proof that free quantitative algebras always exist, the proof of strict monadicity of the induced Free-Forgetful adjunction, the result that all monads (on fuzzy relations) that lift finitary monads (on sets) admit a quantitative equational presentation.
... Note that the continuation k in N is discarded, and replaced with skip, and correspondingly the continuation K in op V K is composed sequentially, rather than passed as an argument. This interprets handlers directly in their generic form [70,67]. The interpretation is correct for handlers satisfying the requisite law. ...
Preprint
Full-text available
The Functional Machine Calculus (Heijltjes 2022) is an extension of the lambda-calculus that preserves confluent reduction and typed termination, while enabling both call-by-name and call-by-value reduction behaviour and encoding the computational effects of mutable higher-order store, input/output, and probabilistic computation. In this note the calculus is extended to capture exception handling and loop constructs.
Article
This work-in-progress paper presents the introduction of algebraic effects to the Fuzion language and how algebraic effects can be used in the context of safety-critical systems. Fuzion is a modern, general purpose programming language that unifies functional and object-oriented paradigms into a pure functional language. Algebraic effects are used to represent and manage non-functional aspects like I/O operations or mutable state. Static analysis is used extensively at several stages in the Fuzion toolchain to verify different correctness aspects of the application. We start with a condensed overview of the Fuzion language to then describe how algebraic effects are used to represent non-functional aspects. The Fuzion toolchain will be explained and how static analysis is used to build and validate applications. Finally, it will be shown how algebraic effects can be used to model aspects relevant to safety-critical systems.
Article
Lexically scoping effect handlers is a language-design idea that equips algebraic effects with a modular semantics: it enables local-reasoning principles without giving up on the control-flow expressiveness that makes effect handlers powerful. However, we observe that existing implementations risk incurring costs akin to the run-time search for dynamically scoped handlers. This paper presents a compilation strategy for lexical effect handlers, adhering to the lexical scoping principle and targeting a language with low-level control over stack layout. Key aspects of this approach are formalized and proven correct. We embody the ideas in a language called Lexa: the Lexa compiler translates high-level effect handling to low-level stack switching. We evaluate the Lexa compiler on a set of benchmarks; the results suggest that it generates efficient code, reducing running-time complexity from quadratic to linear in some cases.
Article
Model checking is one of the successful program verification methodologies. Since the seminal work by Ong, the model checking of higher-order programs―called higher-order model checking, or HOMC for short―has gained attention. It is also crucial for making HOMC applicable to real-world software to address programs involving computational effects. Recently, Dal Lago and Ghyselen considered an extension of HOMC to algebraic effect handlers, which enable programming the semantics of effects. They showed a negative result for HOMC with algebraic effect handlers―it is undecidable. In this work, we explore a restriction on programs with algebraic effect handlers which ensures the decidability of HOMC while allowing implementations of various effects. We identify the crux of the undecidability as the use of an unbounded number of algebraic effect handlers being active at the same time. To prevent it, we introduce answer-type modification (ATM), which can bound the number of algebraic effect handlers that can be active at the same time. We prove that ATM can ensure the decidability of HOMC and show that it accommodates a wide range of effects. To evaluate our approach, we implemented an automated verifier EffCaml based on the presented techniques and confirmed that the program examples discussed in this paper can be automatically verified.
Preprint
Full-text available
We present an adequacy theorem for a concurrent extension of probabilistic GCL. The underlying denotational semantics is based on the so-called mixed powerdomains which combine non-determinism with stochasticity. The theorem itself is formulated via M. Smyth's idea of treating observable properties as open sets of a topological space. One application of our theorem is that it entails semi-decidability w.r.t. whether a concurrent program satisfies an observable property (written in a certain form). This is intimately connected to M. Escard\'o's conjecture about semi-decidability w.r.t. may and must probabilistic testing.
Article
Full-text available
We begin to develop a uniÞed account of modularity for com- putational eects. We use the notion of enriched Lawvere theory, to- gether with its relationship with strong monads, to reformulate Moggi's paradigm for modelling computational eects; we emphasise the impor- tance here of the operations that induce computational eects. Eects qua theories are then combined by appropriate bifunctors (on the cate- gory of theories). We give a theory of the commutative combination of eects, which in particular yields Moggi's side-eects monad transformer (an application is the combination of side-eects with nondeterminism). And we give a theory for the sum of computational eects, which in par- ticular yields Moggi's exceptions monad transformer (an application is the combination of exceptions with other eects).
Book
To construct a compiler for a modern higher-level programming languagel one needs to structure the translation to a machine-like intermediate language in a way that reflects the semantics of the language. little is said about such struc­ turing in compiler texts that are intended to cover a wide variety of program­ ming languages. More is said in the Iiterature on semantics-directed compiler construction [1] but here too the viewpoint is very general (though limited to 1 languages with a finite number of syntactic types). On the other handl there is a considerable body of work using the continuation-passing transformation to structure compilers for the specific case of call-by-value languages such as SCHEME and ML [21 3]. ln this paperl we will describe a method of structuring the translation of ALGOL-like languages that is based on the functor-category semantics devel­ oped by Reynolds [4] and Oles [51 6]. An alternative approach using category theory to structure compilers is the early work of F. L. Morris [7]1 which anticipates our treatment of boolean expressionsl but does not deal with procedures. 2 Types and Syntax An ALGOL-like language is a typed lambda calculus with an unusual repertoire of primitive types. Throughout most of this paper we assume that the primi­ tive types are comm(and) int(eger)exp(ression) int(eger)acc(eptor) int(eger)var(iable) I and that the set 8 of types is the least set containing these primitive types and closed under the binary operation -.
Article
Much of theoretical computer science is based on use of inductive complete partially ordered sets (or ipos). The aim of this thesis is to extend this successful theory to make it applicable to probabilistic computations. The method is to construct a "probabilistic powerdomain" on any ipo to represent the outcome of a probabilistic program which has outputs in the original ipo. In this thesis it is shown that evaluations (functions which assign a probability to open sets with various conditions) form such a powerdomain. Further, the powerdomain is a monadic functor on the categoy Ipo. For restricted classes of ipos a powerdomain of probability distributions, or measures which only take values less than one, has been constructed (by Saheb-Djahromi). In the thesis we show that this powerdomain may be constructed for continuous ipos where it is isomorphic to that of evaluations. The powerdomain of evaluations is shown to have a simple Stone type duality between it and sets of upper continuous functions. This is then used to give a Hoare style logic for an imperative probabilistic language, which is the dual of the probabilistic semantics. Finally the powerdomain is used to give a denotational semantics of a probabilistic metalanguage which is an extension of Moggi's lambda-c-calculus for the powerdomain monad. This semantics is then shown to be equivalent to an operational semantics.
Book
I. Categories, Functors and Natural Transformations.- 1. Axioms for Categories.- 2. Categories.- 3. Functors.- 4. Natural Transformations.- 5. Monics, Epis, and Zeros.- 6. Foundations.- 7. Large Categories.- 8. Hom-sets.- II. Constructions on Categories.- 1. Duality.- 2. Contravariance and Opposites.- 3. Products of Categories.- 4. Functor Categories.- 5. The Category of All Categories.- 6. Comma Categories.- 7. Graphs and Free Categories.- 8. Quotient Categories.- III. Universals and Limits.- 1. Universal Arrows.- 2. The Yoneda Lemma.- 3. Coproducts and Colimits.- 4. Products and Limits.- 5. Categories with Finite Products.- 6. Groups in Categories.- IV. Adjoints.- 1. Adjunctions.- 2. Examples of Adjoints.- 3. Reflective Subcategories.- 4. Equivalence of Categories.- 5. Adjoints for Preorders.- 6. Cartesian Closed Categories.- 7. Transformations of Adjoints.- 8. Composition of Adjoints.- V. Limits.- 1. Creation of Limits.- 2. Limits by Products and Equalizers.- 3. Limits with Parameters.- 4. Preservation of Limits.- 5. Adjoints on Limits.- 6. Freyd's Adjoint Functor Theorem.- 7. Subobjects and Generators.- 8. The Special Adjoint Functor Theorem.- 9. Adjoints in Topology.- VI. Monads and Algebras.- 1. Monads in a Category.- 2. Algebras for a Monad.- 3. The Comparison with Algebras.- 4. Words and Free Semigroups.- 5. Free Algebras for a Monad.- 6. Split Coequalizers.- 7. Beck's Theorem.- 8. Algebras are T-algebras.- 9. Compact Hausdorff Spaces.- VII. Monoids.- 1. Monoidal Categories.- 2. Coherence.- 3. Monoids.- 4. Actions.- 5. The Simplicial Category.- 6. Monads and Homology.- 7. Closed Categories.- 8. Compactly Generated Spaces.- 9. Loops and Suspensions.- VIII. Abelian Categories.- 1. Kernels and Cokernels.- 2. Additive Categories.- 3. Abelian Categories.- 4. Diagram Lemmas.- IX. Special Limits.- 1. Filtered Limits.- 2. Interchange of Limits.- 3. Final Functors.- 4. Diagonal Naturality.- 5. Ends.- 6. Coends.- 7. Ends with Parameters.- 8. Iterated Ends and Limits.- X. Kan Extensions.- 1. Adjoints and Limits.- 2. Weak Universality.- 3. The Kan Extension.- 4. Kan Extensions as Coends.- 5. Pointwise Kan Extensions.- 6. Density.- 7. All Concepts are Kan Extensions.- Table of Terminology.
Chapter
Without Abstract