Compositional semantics for new paradigms: probabilistic, hybrid and beyond

04/11/2018
by   Fredrik Dahlqvist, et al.
uminho
UCL
0

Emerging computational paradigms, such as probabilistic and hybrid programming, introduce new primitive operations that often need to be combined with classic programming constructs. However, it still remains a challenge to provide a semantics to these features and their combination in a systematic manner. For this reason, we introduce a generic, monadic framework that allows us to investigate not only which programming features a given paradigm supports, but also on how it can be extended with new constructs. By applying our method to the probabilistic and hybrid case, we list for example all binary program operations they possess, and show precisely when and if important axioms such as commutativity and idempotency hold. Using this framework, we also study the possibility of incorporating notions of failure and non-determinism, and obtain new results on this topic for hybrid and probabilistic programming.

READ FULL TEXT VIEW PDF

Authors

page 1

page 2

page 3

page 4

12/07/2020

Coalgebraic Semantics for Probabilistic Logic Programming

Probabilistic logic programming is increasingly important in artificial ...
01/17/2020

Generative Datalog with Continuous Distributions

Arguing for the need to combine declarative and probabilistic programmin...
12/03/2012

Compositional Stochastic Modeling and Probabilistic Programming

Probabilistic programming is related to a compositional approach to stoc...
05/19/2017

RankPL: A Qualitative Probabilistic Programming Language

In this paper we introduce RankPL, a modeling language that can be thoug...
06/09/2021

Expectation Programming

Building on ideas from probabilistic programming, we introduce the conce...
09/20/2017

Context-Updates Analysis and Refinement in Chisel

This paper presents the context-updates synthesis component of Chisel--a...
05/07/2019

A Visual Programming Paradigm for Abstract Deep Learning Model Development

Deep learning is one of the fastest growing technologies in computer sci...
This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1. Introduction

Probabilistic programming languages such as Church (Goodman et al., 2012), Anglican (Wood et al., 2014) or Probabilistic C (P. and F., 2014) have become increasingly popular in the last years, and although progress has been made in developing semantics for them many questions remain. In particular, how does one interpret combinations of probabilistic features with ‘classical’ features like error handling or non-deterministic choice? Consider for example the program below, written in Probabilistic C-style.

int main() {
  int a;
  int c_1=bernoulli(0.3);
  int c_2=bernoulli(0.6);
  printf("Please input an integer: ");
  scanf("%d", &a);
  if(a % 3 == 0){
    return c_1;
  } else if (a % 3 == 1){
  return c_2;
  } else {
   exit(EXIT_FAILURE);
  }
}

This program non-deterministically combines two probabilistic instructions – producing Bernoulli trials which return 1 with probability 0.3 (resp. 0.6) and 0 with probability 0.7 (resp. 0.4) – with an execution failure. By abstracting away from the C-style grammar and moving to an algebraic syntax, we want to understand how to interpret the expression

where is the binary probabilistic choice operator with parameter . It is easy enough to provide a semantics to the purely probabilistic instructions in terms of Markov kernels , where is the finitely supported distribution monad. But can we interpret and in this semantics? And if not, how can we modify to support these constructs? We aim to provide firm answers to this kind of question.

The challenge that we described above is not unique to probabilistic programming. Parallel to the latter, recent years have witnessed a flurry of research activity aiming to formalise the programmable features of hybrid systems (Höfner, 2009; Platzer, 2010; Suenaga and Hasuo, 2011), which require an orchestrated use of both classic program constructs and systems of differential equations. Consider, for example, the ‘C-style’ hybrid program below.

int cool_or_heat() {
  int a;
  printf("Please input an integer: ");
  scanf("%d", &a);
  if(a == 0){
    // Heating up
    (dtemp = 1 & 3);
    return 0; // success
  } else if (a == 1){
    // Cooling down
    (dtemp = -1 & 3);
    return 0; // success
  } else {
     exit(EXIT_FAILURE);
  }
}

Depending on the input, the program increases or decreases a reactor’s temperature during three miliseconds or aborts: there exists a global variable (temp) that registers the current temperature, the expression (dtemp = 1 & 3) dictates how the temperature is going to evolve for the next three miliseconds, and similarly for (dtemp = -1 & 3). Abstracting from the C-style grammar, we want to interpret the expression

in a suitable manner, and this yields questions completely analogous to the probabilistic case. Purely hybrid programs can be naturally interpreted as functions where is the hybrid monad (Neves et al., 2016). But it is not clear how to extend this semantics in a systematic manner so that it incorporates exceptions and non-determinism.

Approach and contributions.

The paper is divided in two halves: in the first part we develop a framework – Kleisli representations – so that we can analyse program semantics in a systematic way. In the rest of the paper, we show how program semantics can be explicitly built in this framework, and use it to analyse the hybrid and probabilistic paradigms.

The framework is laid out in Section  3. It reinterprets Moggi’s idea (Moggi, 1991) of interpreting a program as a Kleisli arrow and sequential composition as Kleisli composition by saying that the interpretation map is a monoid morphism from the monoid of programs to the monoid of endomorphisms of in the Kleisli category of .

Representing an algebraic structure as a collection of endomorphisms is an idea very familiar to mathematicians and physicists alike, it sits at the heart of Representation Theory

, a vast field of research which has extensively studied the representations of groups and Lie algebras as endomorphisms of vector spaces, with applications ranging from the classification of finite groups to quantum field theory

(Steinberg, 2011; Haywood, 2011). In this work we re-interpret denotations à la Moggi as representations in the usual mathematical understanding of the word. We will refer to a representation in the Kleisli category of as a Kleisli -representation.

Representation theory provides a natural and useful way of thinking about program semantics: it captures both the algebraic aspects of the language and the coalgebraic aspects of its interpretation in a simple, well-known mathematical object, it separates the role of sequential composition from other operations on programs, it connects smoothly with the existing literature on algebraic effects (Power, 1999; Plotkin and Power, 2001a, b, 2003), and leads to new methods for building program semantic.

Whilst the Kleisli representation of sequential composition boils down to a natural transformation , all other binary operations on programs will be interpreted as natural transformations , an idea first proposed by Plotkin and Power (Moggi, 1989, 1991). Thus, in order to determine precisely which binary program operations are supported by a monad, we must completely enumerate all natural transformations . Section 4, introduces a set of techniques to do this for a large list of well-known monads. For example, we list all binary operations for the hybrid, probabilistic, non-deterministic, and partial paradigms.

Our next step is to study program axiomatics, i.e. the (im)possibility for a monad to support program operations satisfying some given axioms. This is done in Section 5 where we focus on commutativity, idempotence, units, and absorption. We give fine grained results on which axioms can be supported by Kleisli -representations for different monads, which clarifies the types of operation specific computational paradigms support. We show for example that the hybrid paradigm does not admit a non-deterministic choice, that the probabilistic paradigm supports precisely one commutative idempotent operation and that it does not support failures.

Section 6 shows that combining monads with the monad or the powerset monad (i) provides a generic interpretation of tests, and (ii) yields richer monads with which to overcome representability issues highlighted in Section 5. We strengthen a well-known result from (Varacca, 2003) by showing that there cannot exist any monad structure on whatsoever. We also prove the existence of a distributive law of the non-empty powerset monad over the hybrid one. This allows to generate powerful, hybrid programming languages that mix non-deterministic assignments with differential predicates.

We assume basic knowledge of category theory and monads. All proofs can be found in the appendix.

2. Building semantics for hybrid and probabilistic programs

Let us illustrate the questions raised in the introduction and some of the solutions that we have developed for them by looking at two emerging programming paradigms.

Hybrid programs

The distinguishing feature of hybrid programming is that it emphasises and makes explicit the interaction between digital devices and physical processes. This is becoming essential for the software engineer, because he/she needs more and more often to develop complex systems that are deeply intertwined with physical systems (Tabuada, 2009; Platzer, 2010), e.g. cruise controllers, thermostats, etc.

Let us build a very simple hybrid programming language. Take a finite set of real-valued variables and denote by the set, given by the grammar

where is a real number and . Then define as the free monoid over given by the grammar

and with the usual monoidal laws. One possible program is the composition , which, intuitively, sets the acceleration of a vehicle to and then makes it move during three milliseconds.

In Section 3, we will see that the semantics for this language comes naturally as a Kleisli representation

where is the hybrid monad (Neves et al., 2016) (whose definition is recalled in the following section). We now want to endow with other programming features, such as operations and non-deterministic choice. In the framework of Klesili representations, this amounts to providing a suitable algebraic structure that supports these constructs. Consider, for example, the language with syntax

Our goal is to build a Kleisli representation

for this language. Corollary 4.1 shows that this is impossible, because there does not exist a natural transformation to interpret . In other words, pure hybrid computations do not support operations.

In order to surpass this obstacle, one may want to consider the partial hybrid monad . We show that this monad has precisely one natural transformation to interpret such that the axiom holds. In contrast to classic paradigms, we prove that partial hybrid programs do not admit the axiom and such is to be expected.

Suppose now that we wish to extend the language with a non-deterministic choice . As already mentioned, in our framework this amounts to finding a suitable natural transformation . Using the set of techniques introduced in the paper, we list all such transformations and quickly discover that none of them is commutative. This means that in pure hybrid computations one cannot expect a non-deterministic choice operation.

To solve this issue, we show that there exists a distributive law of the non-empty powerset monad over . The monad inherits the natural transformation that takes unions and this allows to extend the language with a non-deterministic choice operation in the usual way.

Probabilistic programs

Consider the simple probabilistic programming language described by the syntax

where is the probabilistic choice operation and is a set of atomic programs. satisfies the following axioms:

(1) (5)
(2) (6)
(3) (7)
(4)

Let us call an algebraic structure for this signature and these equations a convex semiring. is the free convex semiring over . The fragment of such a structure is known as a convex algebra (modulo an extension to -ary affine combinations, see e.g. (Sokolova and Woracek, 2017)), and convex algebras are precisely the Eilenberg-Moore algebras for the distribution monad . The set can be equipped with a convex algebra structure inherited from in the obvious way. In fact, is also a convex semiring, i.e. all the axioms listed above also hold when is interpreted as the Kleisli composition .

We now interpret the language above in terms of rational Markov kernels (i.e. maps where is the monad of rationalprobability distributions), by defining Kleisli -representations as follows: for each choose an interpretation for some state space . The semantics is then extended inductively:

This Kleisli -representation is a convex semiring homomorphism. We now want to extend the language with other operations on programs, such as non-deterministic choice (as in ProbNetKAT (Foster et al., 2016; Smolka et al., 2017)), iteration, or parallel composition. Again, non-deterministic choice is enough to illustrate some goals and contributions of this paper. Consider the language whose syntax is

and whose axioms are those of together with those making an idempotent semiring.

The most obvious strategy to provide a semantics for is to try to put a operation satisfying the axioms above directly on and then define Kleisli representations in the same way as we did for . Since will in fine be interpreted by a natural transformation , our first task is to characterise these. We show in Theorem 4.10 that they are precisely the convex sum operations. It follows that by choosing the equally weighted convex sum we can equip with an operation which satisfies all the axioms above apart from (10) and (11) – actually, we will see that this is the only possible choice. The axioms (10) and (11) fail because once again there are no natural transformations to interpret (Corollary 4.1). In particular there cannot exist any Kleisli -representations of . To overcome this obstacle we will, as in the hybrid case, consider a more complex monad in Section 6 – which adds the missing notion of partial computation to , and restrict our attention to a particular class of non-deterministic instruction.

3. Kleisli representations

Kleisli representation of monoids.

Given a monoid , a monad and a -object , define a Kleisli -representation of in , or simply a Kleisli representation of in if there is no ambiguity, as a monoid homomorphism

where is Kleisli composition and is the unit of .

Example 3.1 (Classical linear representations).

Let be the functor building free vector spaces over some chosen field, and let be the corresponding forgetful functor. The composition is a monad, and for any group , a Kleisli -representation of on a finite set is simply the usual notion of linear representation of on the -dimensional vector space.

Example 3.2 (Stochastic processes).

The category is the category of Polish spaces, i.e. separable, completely metrisable topological spaces, and continuous maps. The Giry monad (Giry, 1982) associates to every Polish space the set of probability distributions on together with the topology of weak convergence, which is Polish. On morphisms, it associates to any continuous map the map taking the pushforward of measures. A Kleisli -representation of the monoid of non-negative reals is a stochastic process.

Example 3.3 (Hybrid systems).

The functorial part of the hybrid monad (Neves et al., 2016) is defined by

Its unit is given by the equation , with the constant function on , and the multiplication is defined by

with the natural transformation that sends an evolution to and the natural transformation that concatenates two evolutions. Intuitively, the multiplication will be used to concatenate the evolutions produced by two hybrid programs.

Recall from Section 2 the grammar of atomic hybrid programs and denote the usual interpretation of a term over a valuation by or simply

if the valuation is clear from the context. Since linear systems of ordinary differential equations always have unique solutions

(Perko, 2013), there exists an interpretation map

that sends to the function defined by,

and that sends to the respective solution but restricted to . The free monoid extension of this interpretation map provides a Kleisli representation

which includes both assignments and differential equations. In the appendix we provide more details about this language, and give examples of other languages generated by the hybrid monad.

Representing general varieties.

Consider a finitary variety defined by a signature with arity map , and a set of equations that contains the monoidal laws for . Consider also a monad on a category with products. As the reader may have guessed, a Kleisli -representation of a -object will be a morphism . But in which category? Since our starting point will always be a signature , we define a Kleisli -representation of a -object as a morphism in the category of -algebras, that is to say a morphism which commutes with all the operations in the signature but whose codomain may not live in . The rationale for this choice of category is the following: a group representation is not a group homomorphism because the whole point of a representation is to map group elements to important mathematical objects which do not form a group, for example real-valued matrices. Similarly, requiring the representation map to be a -morphism would be way too stringent and would drastically limit the choice of possible semantics. By defining a Kleisli representation as a -algebra morphism we do not require that all equations in be valid in , but we do require that every operation in be interpretable in .

In order to endow with a suitable algebraic structure from a signature , we proceed in the footsteps of (Plotkin and Power, 2001a, b, 2003): for every consider the set of natural transformations (as usual we take , the constant functor on the final object 1). For each we choose an element and define the operation by

(1)

We can now define a generic Kleisli representation as follows: a Kleisli -representation of in is an assignment to every of a natural transformation together with a -algebra morphism

Naturality and abstraction

The requirement that operations be interpreted by natural transformations could be seen as either too strong or too weak. Too strong because it is a very restrictive condition whose justification is not immediately obvious. Too weak because it is strictly weaker than the requirement of (Plotkin and Power, 2001a, b) defining algebraic operations where compatibility conditions with the strength and multiplication of the monad are assumed. So why have we chosen to focus on naturality?

First, algebraic operations are in general too restrictive for our purpose. Examples of non-algebraic operations include exception-handling operators (Plotkin and Power, 2003) (which we will address in Section 6), ‘true’ parallel composition axiomatised by the exchange law (Gischer, 1988) and, more recently, examples from Game Logic (Hansen et al., 2014). Actually, our work is to a large degree a systematic investigation of what can be said about the semantics of programs with operations that are not necessarily algebraic in the sense of Plotkin and Power – a research direction already mentioned in (Plotkin and Power, 2001b) – for a wide range of monads.

Second, it is important to be able to consider sub-representations and quotient representations, and, as we will show, naturality plays a key role in allowing these to be defined. Often we need to abstract away details of a representation in a large, fine-grained state space and build a representation in a coarser one, but in such a way that both representations ‘agree’ with each other. Formally, for a quotient map and two Kleisli representations , we need that the equation

holds for all programs . Abstracting and then interpreting should be the same as interpreting and then abstracting. The naturality of program operations allows a compositional construction of quotient representations because it allows to prove that the equation holds just by showing that it holds for atomic programs. For sequential composition, this follows from the naturality of :

and for other operations the naturality requirement on makes the following diagram also commute.

So naturality allows to freely extend an abstraction from atomic programs to all programs in the language.

4. Interpreting constants and operations

4.1. Constants

The following result, despite its simplicity, gives a very general characterisation of natural transformations .

Theorem 4.1 ().

Let be a category with an initial object and an object such that , then we have the sequence of bijections below.

Note that in the result above is a trivial consequence of the Yoneda lemma, since is representable as .

Example 4.2 ().
  1. The monad has exactly one natural transformation .

  2. , and since the category of Polish spaces satisfies the assumptions of Theorem 4.1, it is also the case that . Probabilistic programs do not support partial computations.

  3. and thus hybrid programs also do not support an interpretation of failure.

4.2. Operations

4.2.1. Coproducts of functors

We start with those functors that can be written as coproducts of functors, since they can be treated completely straightforwardly. Actually, using the notions of container and fibration, (Abbott et al., 2003) already provides a powerful representation theorem for natural transformations when is one such functor. In order to keep this paper self-contained, however, we introduce a direct, equivalent result that does not need the notion of fibration nor the notion of container.

Theorem 4.3 ().

If is a functor expressible as a coproduct of functors, i.e. if there exists a non-empty family of sets such that , then

Example 4.4 ().

The maybe monad can be written as the coproduct . It follows from Theorem 4.3 that the possible interpretations of a binary operation are in bijective correspondence with the set , in particular there are exactly 12 natural transformations . The ‘-coordinate’ specifies what a transformation does on pairs with , viz. projecting to the left, to the right or mapping to , the first -coordinate specifies what happens to pairs , viz. projecting to the left or the right, and similarly for the last coordinate and pairs .

Example 4.5 ().

Since , the natural transformations are in bijective correspondence with the set

For an element of this set, each -coordinate dictates what the transformation does to pairs of evolutions with duration and . In particular, it tells how the values in a given pair of evolutions of duration and are distributed in the new evolution: one has the composition

which makes clear that for every element the value arises from one of the two starting functions ( or ) and an element in their respective domain ( or ).

To cover a broader spectrum of monads we need to introduce some more sophisticated mathematics.

4.2.2. The presentation of -valued functors.

Let be a small category and be a functor. We define the category of elements of , denoted , as the category whose objects are pairs where is an object in and . There exists a morphism in whenever there exists a morphism such that . For every object in , we will define the orbit of as all the objects which can be reached from by a zigzag of morphisms in . The decomposition of in orbits is key to understanding natural transformations involving , since naturality is only a constraint on objects in the same orbit. The category allows us to completely reconstruct . Moreover, this reconstruction process provides us with a presentation of as a colimit of covariant functors.

Theorem 4.6 ((MacLane and Moerdijk, 2012) i.5).

Let denote the Yoneda embedding, and be the forgetful functor sending each pair to the object , then

Theorem 4.6 gives us a way of presenting functors from a small category to as a colimit of functors, but what we really need are presentations of functors . To move from to we need a few relatively well-known definitions. Recall that an object in a category is finitely presentable if the functor preserves filtered colimits. In the finitely presentable objects are precisely the finite sets. A category is called locally finitely presentable if it is cocomplete and contains a small subcategory of finitely presentable objects such that every object in is the filtered colimit of the canonical diagram sending each arrow of to its domain. The category is finitely presentable, since every set can written as with and the subcategory of consisting of elements (this simply says that every set is the union of its finite subsets). Finally, a functor is called finitary if it preserves filtered colimits. Finitary functors are entirely determined by their restriction to . In fact if is a finitary functor on a locally finitely presentable category and is the inclusion functor, then can be written as the left Kan extension , where . We refer the interested reader to the classic (Adámek and Rosicky, 1994) for a full account of the theory of locally finitely presentable categories.

Proposition 4.7 ().

Let be finitary and let be the inclusion functor, then

The following result is a simple application of Proposition 4.7 and the Yoneda lemma.

Theorem 4.8 ().

Let be a finitary functor and let denote its restriction to , then the set is in one-to-one correspondence with the limit

(2)

The hard work consists in computing the limit (2) above.

4.2.3. A classification result for some multiset functors.

Computing the limit (2) of Theorem 4.8 for general multiset-type monads (see their definition in the appendix) depends heavily on the choice of semiring and may prove extremely difficult. However, we do have an explicit characterisation in the following useful case. We say that a semiring has the common integer divisor property if for any there exist an invertible element and such that

We will refer to as a common integer divisor of and . Clearly the semiring has this property since we can always pick (which is trivially invertible) and . Similarly the semiring has this property: given two rationals , we can choose (which is invertible) and . The semiring of real does not have this property: if is irrational then there doesn’t exist an with the desired property.

Theorem 4.9 ().

Let be a semiring with the common integer divisor property and let be the multiset monad for , then is in one-to-one correspondence with the set of functions .

4.2.4. Some results for the Giry monad

Due to its importance, we provide some detailed results for the Giry monad , which clarifies what can be expected of purely probabilistic program semantics. Before we turn to the full Giry monad, let us consider the rational distribution monad . The following can be shown using the same ideas as in the proof of Theorem 4.9.

Theorem 4.10 ().

The only natural transformations are the convex combinations .

Theorem 4.10 can be used to provide a full classification result for the Giry monad on . For this we use a set of criteria for functors developed in (Dahlqvist et al., 2016a) and (Dahlqvist et al., 2016b) under which it can be shown that

where is the restriction of to the category , the category of finite Polish spaces (and similarly for ). These criteria restrict both the domain and the codomain functors. We refer the reader to (Dahlqvist et al., 2016a) for more details; for our purpose it will be enough to say that the Giry monad always satisfies the domain and codomain criteria (see (Dahlqvist et al., 2016a, Prop. 5.1)) and that finite products of satisfy the domain criteria (see (Dahlqvist et al., 2016b, Prop. 17)). It follows that

(3)

The isomorphism (3) allows the following result to be established at the level of finite Polish sets, and then lifted to the entire category .

Theorem 4.11 ().

The only natural transformations are the convex combinations defined by the maps such that

for any Borel subset .

The combination (with topologised in the obvious way) is a monad – by a straightforward generalisation of Theorem 6.1 – which is called the subdistribution monad. It behaves very similarly to , but allows failure since , and thus by Corollary 4.1 there exists a natural transformation . Moreover, whilst has a single orbit, has a collection of orbits labelled by . This makes the set of natural transformations rather large, but using the same technique as in the proofs of Theorems 4.9 and 4.11 we can describe them concisely as follows.

Theorem 4.12 ().

The set is in one-to-one correspondence with continuous maps

4.2.5. The case of the powerset monad.

A functor is called -accessible, for a regular cardinal , if it preserves -filtered colimits. In particular -accessible is synonymous with finitary. Theorem 4.8 generalizes completely straightforwardly to -accessible functors. Using this, we can give a complete classification result for the set of natural transformations for the full powerset monad and any cardinal .

Theorem 4.13 ().

For a cardinal the set of natural transformations , where denotes the -fold product of with itself, is in one-to-one correspondence with the set of non-increasing maps (for the product order on ).

5. Interpreting program axiomatics

We explore whether it is possible for a given monad to define binary operations via natural transformations is such a way that important axioms hold.

Let us first fix some terminology: consider a set of variables, a signature , and the corresponding set of terms . Now take two terms . We say that is satisfiable in Kleisli -representations if there exists an assignment for each operation such that for every set and every map , holds for the interpretation defined by Eq. (1) in . We will permit ourselves the slight abuse of language consisting in saying that the choice of natural transformation itself satisfies , for example we will say that a natural transformation is commutative if it makes satisfiable in Kleisli -representations. Finally, we will say that is valid in Kleisli -representations if it satisfied for any assignment .

5.1. Commutativity.

We start with the case of coproducts of functors.

Proposition 5.1 ().

If , then a natural transformation given by an element via Thm 4.3 is commutative iff for all , the equation below holds.

In particular, for all , must be the map with empty domain.

Proposition 5.1 tells us that commutative natural transformations for coproducts of hom functors must adhere to rather harsh conditions. This is illustrated by the following examples.

Example 5.2 ().
  1. A natural transformation can only be commutative if its ‘ coordinate’ (defined by Thm 4.3) lies in the second summand of , i.e. if two elements different than failure are mapped to failure.

  2. There is no commutative natural transformation for hybrid programs because there exists no real number such that .

For the other monads presented thus far we can look directly at the classification results provided in Section 4. We have for example:

  1. The commutative natural transformations are precisely given by the maps , i.e. the transformations choosing an equally weighted sum of multisets for each orbit.

  2. The unique commutative natural transformation is the average transformation , and by the same argument as in Section 4.2 this is also the case for the Giry monad .

5.2. Idempotence.

For coproducts of functors we have:

Proposition 5.3 ().

If , then a natural transformation given by an element via Thm. 4.3 is idempotent iff for each , is a map such that where is the codiagonal map.

Example 5.4 ().
  1. A natural transformation is idempotent iff its ‘ coordinate’ is in the first summand of . In other words, if two elements different than failure are projected either to the left or to the right.

  2. A natural transformation is idempotent iff for every two evolutions with the same duration , has domain , and for every element , or .

It follows from Propositions 5.1 and 5.3 that the combination of commutativity and idempotence is never satisfiable for Kleisli -representations when is a non-constant coproduct of functors since there will then exist an in the coproduct presentation of for which Proposition 5.1 requires that be of type but for which Proposition 5.3 requires that be of type . Thus no monad whose functor is a coproduct of functors can support a programming language with a commutative and idempotent binary operation on programs.

We summarise the characterisation of idempotent natural transformations for the other monads as follows:

  1. The idempotent natural transformations are those which are defined as projections on pairs of multisets of equal weights. Note that as in the case of coproducts of functors, the combination of commutativity and idempotence is never satisfiable in Kleisli -representations, since the commutative operations must take equally weighted sums, whereas the idempotent ones must put one weight to zero and the other to one on pairs of multisets of equal weights.

  2. Theorem 4.10 guarantees that all natural transformation are idempotent, i.e. idempotence is valid over -representations, and similarly for . Note that maps mapping each pair to a pair with also define the idempotent operations of .

5.3. Units

For coproducts of functors we have:

Proposition 5.5 ().

Consider a functor , a natural transformation , given by an element via Thm 4.3, and a natural transformation . The transformation is a unit for iff with , the isomorphisms that define as the unit of coproducts, and .

Example 5.6 ().
  1. A natural transformation has the unique natural transformation as unit iff its ‘ coordinates’ are in the first summand of .

  2. The unique natural transformation which pick the constant multiset with weight 0 defines a unit for any binary natural transformation since is the unit of . The unit axiom is thus valid over -representations.

  3. Since there are no natural transformations , there can be no interpretation of constants in Kleisli -representations, and similarly for , and .

  4. The sub-distribution monad offers both probabilistic choice and partial computation but the unit axiom will be problematic for any binary operation: consider the sub-distributions on the singleton set , we need

    and

    This means that the function defining (Thm. 4.12) must map to and to for each , which clearly cannot be continuous.

5.5. Absorption.

The absorption law states that for every program the equations below hold.

Even though they appear to be simple, to check their satisfiability in general Kleisli representations is a surprisingly complex issue. On the one hand, we have:

Proposition 5.7 ().

The axiom holds in every Kleisli representation.

On the other hand, the axiom need not hold, and this is exemplified by the partial hybrid paradigm which is briefly introduced in the following section.

Theorem 5.8 ().

Consider a monad with a natural transformation . If the condition holds then the axiom holds as well.

6. Adding features by combining monads

6.1. Failure and Tests

There is a natural procedure for turning Kleisli representations which cannot interpret failure – such as those for the distribution or hybrid monads – into Kleisli representations which do support failure. More interestingly though, this procedure also allows to interpret tests, and in the case of probabilistic programs one recovers the interpretation of if-then-else commands first given by Kozen (Kozen, 1981).

Adding failure.

Recall from Section 4 that neither probabilistic nor hybrid systems can handle failure. As recently described in (A. and H., 2017) for probabilistic systems, adding a distinguished state is an obvious strategy to overcome this shortcoming. We thus consider composing effect monads of interest with the monad . This can always be done as the following result (Lüth and Ghani, 2002) shows.

Theorem 6.1 ().

For a monad there exists a distributive law defined by

In particular always forms a monad of which is a submonad. Combining monads in this way potentially addresses more than the problem of non-representability of failure: since every -monad is strong we can always find a natural transformation (we use the convention and ). Hence, if a -monad has a natural transformation , then also has a natural transformation of this type given by

(4)

In other words, we can increase the list of available operations by composing monads. Note that the algebraic properties of discussed in Section 5 are usually lost by the lifting described above. However if is commutative, then the lifting above will preserve associativity, commutativity and the existence of units. For , (4) shows that always has a natural transformation

and we can thus always interpret failure in . In particular, the monads and support an abort operation , and, as shown in Section 5, the axiom is valid in both paradigms. We also have the bijection and by an application of Theorem 5.8 this entails that the axiom is valid in probabilistic programming. On the other hand, in the hybrid case we have the bijections which say that one can always interpret an abort operation that produces failures for a given duration . For each of these operations the axiom cannot hold: on the left side, the duration of an evolution produced by is added to that of . Hence, if the evolution of is greater than the sum is greater than the duration of the evolution produced on the right side.

Adding Tests.

The combination also provides Kleisli -representations of useful fragments of Kleene algebras with tests (KAT) (Kozen, 1997). Here we will only consider the -free fragment of KATs, i.e. idempotent semirings with tests, or ISTs, which are defined as follows. An IST is a two-sorted structure such that , is an idempotent semiring, and is a boolean algebra.

Let us first deal with tests: each test should be interpreted as a predicate on the state space, which is equivalent to a map

(5)

We will therefore interpret tests in as the maps that can be built as , where is of the shape (5). In particular, we automatically get a two-sorted structure on . Note also that we can straightforwardly extend the single-sorted definition of Kleisli representation in Section 3 to a two-sorted definition so that KAT-like structures can be accomodated.

We now need to define a boolean algebra structure on tests in . The constants are as expected: is the -lifting of the unit of , i.e. the unit of , and is the -lifting of the unique natural transformation . The negation of a test is defined in the obvious way and it follows that . Conjunction is simply Kleisli composition. It is not hard to check by unravelling the definitions and using the properties of the distributive law that for any two tests : .

For disjunction, things are more complicated. We have seen in Theorem 4.3 and Section 5, that there is no idempotent commutative binary natural transformation with as unit. In particular we cannot expect the disjunction to be interpreted using a natural transformation of the shape (4). In other words, simply composing with the monad does not give a non-deterministic choice in general.

However, there exists a natural and useful sub-class of programs in an IST for which composing with the monad does give non-deterministic choice. We call this sub-class the if-then-else fragment. It is given by:

where reads ‘if then else ’. This restricted non-determinism has Kleisli -representations for any monad . To see why, choose any of the 3 natural transformations (Prop.5.5) for which is a unit, i.e. any of the transformations mapping pairs to . With this choice made to resolve -non-determinism, we define the Kleisli -representation of in as:

(6)

When is derived from the strength of we can show that this Kleisli -representation of if-then-else statements is sound in the sense that expected algebraic properties of the operators are preserved by the representation. It is routine by unravelling the definitions and using the explicit construction of strength in to show:

Theorem 6.2 ().

Let be tests and consider a Kleisli -representation in interpreting tests according to (5) and if-then-else according to (6). Then111Note the similarity with the axioms of convex algebras.,

In the case of probabilistic programs, the Kleisli -represen-tation (6) of if-then-else is precisely the one given by Kozen in (Kozen, 1981).

6.2. Non-determinism

In order to extend a given programming paradigm with non-deterministic features, one may also wish to combine the underlying monad with the powerset since the following two natural transformations always exist.

These respect the unit, idempotence, commutativity, and associative laws for . So, in principle, less well-behaved monads, like and , together with