A categorical view of computational effects

Similar documents
Programming Languages in String Diagrams. [ four ] Local stores. Paul-André Melliès. Oregon Summer School in Programming Languages June 2011

Category Theory. Categories. Definition.

Towards a Flowchart Diagrammatic Language for Monad-based Semantics

Semantics for algebraic operations

Logic for Computational Effects: work in progress

How to combine diagrammatic logics

Elements of Category Theory

Structural Foundations for Abstract Mathematics

A categorical model for a quantum circuit description language

Categories and Functors (Lecture Notes for Midlands Graduate School, 2013) Uday S. Reddy The University of Birmingham

Notions of Computation Determine Monads

Quantum groupoids and logical dualities

COMMUTATIVE ALGEBRA LECTURE 1: SOME CATEGORY THEORY

Second-Order Algebraic Theories

Categories and Functors (Lecture Notes for Midlands Graduate School, 2012) Uday S. Reddy The University of Birmingham

Exercises on chapter 0

Mathematical Synthesis of Equational Deduction Systems. Marcelo Fiore. Computer Laboratory University of Cambridge

Equational reasoning for probabilistic programming Part II: Quantitative equational logic

Monads Need Not Be Endofunctors

1 The Hyland-Schalke functor G Rel. 2 Weakenings

The equivalence axiom and univalent models of type theory.

What s category theory, anyway? Dedicated to the memory of Dietmar Schumacher ( )

Order Sorted Algebra. Japan Advanced Institute of Science and Technology. March 8, 2008

The category of open simply-typed lambda terms

Cellularity, composition, and morphisms of algebraic weak factorization systems

Axiomatics for Data Refinement in Call by Value Programming Languages

On a Monadic Encoding of Continuous Behaviour

A fully abstract semantics for a nondeterministic functional language with monadic types

Monads and More: Part 4

On the Semantics of Parsing Actions

Homological Algebra and Differential Linear Logic

Complete Partial Orders, PCF, and Control

Higher Order Containers

A Monad For Randomized Algorithms

ACLT: Algebra, Categories, Logic in Topology - Grothendieck's generalized topological spaces (toposes)

DEFINITIONS: OPERADS, ALGEBRAS AND MODULES. Let S be a symmetric monoidal category with product and unit object κ.

FUNCTORS AND ADJUNCTIONS. 1. Functors

Logical connections in the many-sorted setting

Construction of Physical Models from Category Theory. Master Thesis in Theoretical Physics. Marko Marjanovic

Recognisable Languages over Monads

PDF hosted at the Radboud Repository of the Radboud University Nijmegen

Algebraic Geometry

Categorical Probability and Stochastic Dominance in Metric Spaces

Duality in Probabilistic Automata

Limit Preservation from Naturality

3. Categories and Functors We recall the definition of a category: Definition 3.1. A category C is the data of two collections. The first collection

Isomorphism of Finitary Inductive Types

Local states in string diagrams

Algebras. Larry Moss Indiana University, Bloomington. TACL 13 Summer School, Vanderbilt University

Relative Hilbert-Post completeness for exceptions

Partially ordered monads and powerset Kleene algebras

From parametric polymorphism to models of polymorphic FPC

FOUNDATIONS OF ALGEBRAIC GEOMETRY CLASS 2

A Differential Model Theory for Resource Lambda Calculi - Part I

Monad Transformers as Monoid Transformers

Complex Systems from the Perspective of Category. Theory: II. Covering Systems and Sheaves

What are Iteration Theories?

Interpreting the Second-Order Lambda Calculus using Qualitative Domains

Uniform Schemata for Proof Rules

Symmetric monoidal sketches

Categories and functors

Elementary (ha-ha) Aspects of Topos Theory

The algebraicity of the lambda-calculus

PART I. Abstract algebraic categories

Introduction to Kleene Algebras

five years later Olivia Caramello TACL 2015, 25 June 2015 Université Paris 7 The theory of topos-theoretic bridges, five years later Olivia Caramello

Bicubical Directed Type Theory

Towards a quantum calculus

NOTES ON ATIYAH S TQFT S

NOTES ON ADJUNCTIONS, MONADS AND LAWVERE THEORIES. 1. Adjunctions

Conservation of Information

UNIVERSAL DERIVED EQUIVALENCES OF POSETS

arxiv: v1 [cs.lo] 4 Jul 2018

FROM COHERENT TO FINITENESS SPACES

An introduction to Yoneda structures

Homotopy Type Theory

Monads and More: Part 4

Second-Order Equational Logic

About categorical semantics

University of Oxford, Michaelis November 16, Categorical Semantics and Topos Theory Homotopy type theor

Universal Properties

What are Iteration Theories?

Algebraic model structures

An Intuitive Introduction to Motivic Homotopy Theory Vladimir Voevodsky

Reconsidering MacLane. Peter M. Hines

C2.7: CATEGORY THEORY

NON-SYMMETRIC -AUTONOMOUS CATEGORIES

CATEGORY THEORY. Cats have been around for 70 years. Eilenberg + Mac Lane =. Cats are about building bridges between different parts of maths.

A brief Introduction to Category Theory

Adjunctions! Everywhere!

Teooriaseminar. TTÜ Küberneetika Instituut. May 10, Categorical Models. for Two Intuitionistic Modal Logics. Wolfgang Jeltsch.

Applications of 2-categorical algebra to the theory of operads. Mark Weber

Dependent Type Theories. Lecture 4. Computing the B-sets for C-systems CC(RR)[LM]. The term C-systems of type theories.

EXAMPLES AND EXERCISES IN BASIC CATEGORY THEORY

The basic theory of monads and their connection to universal algebra

Second-Order Equational Logic

Algebraic model structures

Modèles des langages de programmation Domaines, catégories, jeux. Programme de cette seconde séance:

Lecture 15: Duality. Next we spell out the answer to Exercise It is part of the definition of a TQFT.

Calculating monad transformers with category theory

Transcription:

Emily Riehl Johns Hopkins University A categorical view of computational effects C mp se::conference

1. Functions, composition, and categories 2. Categories for computational effects (monads) 3. Categories of operations and equations (Lawvere theories) 4. Lawvere theories vs monads

Preview Let T denote a notion of computation. A T-program is a function A f T(B) from the set of values of type A to the set of T-computations of type B. T is a monad just when it has the structure needed to turn T-programs into a category. The T-programs between finite types define a Lawvere theory. The Lawvere theory presents the operations and equations for the computational effect T.* *If T is not finitary, these operations and equations define a different monad.

1 Functions, composition, and categories

The mathematician s view of functions A function, e.g.: f (x) = x 2 x always comes with specified sets of possible input values and potential output values. One writes I f O to indicate that f is a function with source I and target O. Why bother with sources and targets? This data indicates when two functions are composable: f g A B and B C are composable just when the target of f equals the source of g.

Composable and non-composable functions Are the functions composable? It depends. If f (x) = x 2 x and g(y) = ( ) 1 y 2 f g N Z and Z Q where N is the set of natural numbers, Z is the set of integers, and Q is the set of rational numbers then yes: (g f )(x) = ( 1 2) x 2 x. But if f g Mat 2 2 (Z) Mat 2x2 (Z) and Z Q where Mat 2 2 (Z) is the set of 2 2-matrices with integer coefficients then no: what is the meaning of ( 1 2) y if y is a matrix?

What is a category? A category is a two-sorted structure that encodes the algebra of composition. It has objects: A, B, C... and arrows: A f B, B g C, each with a specified source and target so that for any pair of composable arrows: f B g there exists a composite arrow A g f C and each object has an identity arrow A id A A for which the composition operation is associative and unital.

What is the point of identities? An isomorphism consists of: A f g B so that g f = id A and f g = id B Isomorphism invariance principle: If A and B are isomorphic then every category theoretic property of A is also true of B.

Examples of categories In the category Set the objects are (finite) sets X, Y,... arrows are functions X f Y,... In the syntactic category for some programming language the objects are types X, Y,... arrows are programs X f Y,... Note that the same notation describes the data in any category. The precise ontology of the objects and arrows won t matter much.

2 Categories for computational effects (monads)

Notions of computation Let us introduce some large functions Set T Set each encoding some notion of computation: list(x) := finite lists of elements of X partial(x) := X + { } side-effects S (X) := [S, S X], the set of functions from S to S X continuations R (X) := [[X, R], R], the set of functions from the set [X, R] of functions from X to R to R non-det(x) := P + (X), the set of non-empty subsets of X prob-dist(x) := the set of probability functions X p [0, 1] so that x X p(x) = 1

T-programs For any notion of computation T list(x) := finite lists of elements of X partial(x) := X + { } side-effects S (X) := [S, S X], the set of functions from S to S X continuations R (X) := [[X, R], R], the set of functions from the set [X, R] of functions from X to R to R non-det(x) := P(X), the set of all subsets of X prob-dist(x) := the set of probability functions X p [0, 1] so that x X p(x) = 1 A T-program from A to B is a function A f T(B), from the set of values of type A to the set of T-computations of type B. Write A f B to mean A f T(B).

Programs should form a category A T-program from A to B is a function A f T(B), from the set of values of type A to the set of T-computations of type B. The notion of monad arises from the following categorical imperative: programs should form a category Theorem: A notion of computation T defines a monad if* and only if the T-programs A f B define the arrows in a category. *If the category of T-programs is constructed using a Kleisli triple, as depicted on the next slide, then T defines a monad.

The category of T-programs To define the category of T-programs we need: identity arrows A id A a composition rule for T-computations: η A; a monad has unit functions A A T(A) A f B g f g C Problem: A f T(B) and B g T(C) are not composable! With a monad, any function B g T(C) can be extended to a function T(B) g T(C). Then f T(B) g A g f T(C) defines the Kleisli composite of A f B and B g C in the category Kl T.

The category of partial-computations For partial(x) := X + { } A program A f from A to B. The unit A id A B is a function A f B + { }, i.e., a partial function A is the function A incl A + { }. Any function B g C + { } extends to a function by the rule g ( ) =. The Kleisli composite f B g B + { } g C + { } B + { } A C A C + { } g f is the largest partial function from A to C. f g f g

The category of list-computations For list(x) A program A f A to lists in B. The unit A id A B is a function A f list(b), i.e., a function from list(a) is the function A singleton list(a). Any function B g list(c) extends to a function list(b) g list(c) by applying g to each term in a list of elements of B and concatenating the result. The Kleisli composite f B g list(b) A C A list(c) g f f g f is defined by application of f and g followed by concatenation. g

3 Categories of operations and equations (Lawvere theories)

Kleisli arrows define operations Let n := {x 1,..., x n } denote the set with n elements. An arrow 1 n in the category of list-programs Kl list is a function 1 list(n) (by definition) or equivalently an element of list(n) (the image of the previous function). E.g. 1 x 3x 5 x 2 x 5 6 x3 x 5 x 2 x 5 list(6) Arrows 1 n, i.e., elements of list(n), define n-ary operations.

Kleisli composites define equations between operations In the category of list-programs Kl list, arrows 1 operations. n define n-ary Compositions n 1 m define equations between operations. E.g. x 1 x 2 2 (x 1,x 2 x 3 ) 1 3 x 1 x 2 x 3 corresponds to the equation x 1 (x 2 x 3 ) = x 1 x 2 x 3. Together these operations and equations define the list-theory L list.

Models for the list-theory A model for the list-theory L list is: E.g. a set A together with a function A n A for each n-ary operation 1 satisfying the equations determined by the compositions in the category of programs. n x 1 x 2 2 (x 1,x 2 x 3 ) 1 3 x 1 x 2 x 3 are modeled by A 2 λ.x 1 x 2 λ.(x 1,x 2 x 3 ) λ.x 1 (x 2 x 3 ) A = A 3 λ.x 1 x 2 x 3 A model for the list-theory L list is a contravariant product-preserving functor from the category L list of list-programs between finite sets.

4 Lawvere theories vs monads

models for a Lawvere theory If T is any monadic notion of computation let L T denote the category of T-programs between finite sets. The opposite category L T op, obtained by formally reversing the arrows, defines a Lawvere theory. A model is a functor L op T Set defined on objects by 1 A 2 A 2. n. A n and carrying each arrow n A m A n. m in the category L T to a function

algebras for a monad If T is any monadic notion of computation an algebra for T is a set A together with a function T(A) α A so that the composition relations hold: η A id T(A) A T(A) T(T(A)) TA id A α Tα α A TA A α Theorem: The category of models for the Lawvere theory L T op and the category of algebra for the monad T are equivalent.

monads vs Lawvere theories A monad is a notion of composition Set T Set so that T-programs A f T(B) define the arrows A f category Kl T. B in a The opposite of the category of T-programs between finite sets defines a Lawvere theory L T op. Conversely, any Lawvere theory defines a monad on Set. Theorem: The category of Lawvere theories is equivalent to the category of finitary monads on Set. Finitary monads and Lawvere theories describe equivalent categorical encodings of universal algebra.

Advantages of Lawvere theories Why bother with Lawvere theories if they are equivalent to monads? Each monad acts on just one category, whereas models of Lawvere theories can be defined in any category with finite products and the construction of the category of models is functorial in both arguments. Lawvere theory operations can be added: any two Lawvere theories L and L have a sum L + L indeed the category of Lawvere theories is locally finitely presentable. Lawvere theory operations can be intertwined: any two Lawvere theories L and L have a tensor product L L. In practice, Lawvere theories are generated by computationally natural operations satisfying computationally meaningful equations e.g., exceptions, side-effects, interactive input-output,

Continuations All of computational effects mentioned thusfar fit into this framework for categorical universal algebra with one exception: Even for 2 = {, }, the continuations monad continuations 2 (X) := [[X, 2], 2] = P(P(X)) is not finitary. It does define a large Lawvere theory, but this is specified with a proper class of operations. it appears that the continuations monad transformer should be seen as something sui generis.

A look towards the future In The Category Theoretic Understanding of Universal Algebra: Lawvere Theories and Monads, Martin Hyland and John Power suggest that computational effects might be seen as an instance and development of universal algebra. From this viewpoint: Continuations would not be regarded as a computational effect but rather as a distinct notion. It would still have its own body of theory, and one would still study the relationship between it and computational effects; but perhaps it would not be regarded as a computational effect? Monads appear quite directly in the study of continuations. So perhaps the notion of monad might be seen as a generalised semantics of continuations?

Review Let T denote a notion of computation. A T-program is a function A f T(B) from the set of values of type A to the set of T-computations of type B. T is a monad just when it has the structure needed to turn T-programs into a category. The T-programs between finite types define a Lawvere theory. The Lawvere theory presents the operations and equations for the computational effect T.* *If T is not finitary, these operations and equations define a different monad.

References Eugenio Moggi, Computational lambda-calculus and monads describes monads and the category of programs Gordon Plotkin and John Power, Computational Effects and Operations: An Overview describes the connection between Moggi s monads and Lawvere theories Martin Hyland and John Power, The Category Theoretic Understanding of Universal Algebra: Lawvere Theories and Monads Thank you! inspired this talk