DeepAI

# From Equations to Distinctions: Two Interpretations of Effectful Computations

There are several ways to define program equivalence for functional programs with algebraic effects. We consider two complementing ways to specify behavioural equivalence. One way is to specify a set of axiomatic equations, and allow proof methods to show that two programs are equivalent. Another way is to specify an Eilenberg-Moore algebra, which generate tests that could distinguish programs. These two methods are said to complement each other if any two programs can be shown to be equivalent if and only if there is no test to distinguish them. In this paper, we study a generic method to formulate from a set of axiomatic equations an Eilenberg-Moore algebra which complements it. We will look at an additional condition which must be satisfied for this to work. We then apply this method to a handful of examples of effects, including probability and global store, and show they coincide with the usual algebras from the literature. We will moreover study whether or not it is possible to specify a set of unary Boolean modalities which could function as distinction-tests complementing the equational theory.

04/18/2019

### Behavioural Equivalence via Modalities for Algebraic Effects

The paper investigates behavioural equivalence between programs in a cal...
02/12/2019

### Program Equivalence for Algebraic Effects via Modalities

This dissertation is concerned with the study of program equivalence and...
11/04/2019

### Generalising KAT to verify weighted computations

Kleene algebra with tests (KAT) was introduced as an algebraic structure...
04/26/2019

### Quantitative Logics for Equivalence of Effectful Programs

In order to reason about effects, we can define quantitative formulas to...
10/13/2021

### Algebraic Reasoning of Quantum Programs via Non-Idempotent Kleene Algebra

We investigate the algebraic reasoning of quantum programs inspired by t...
02/17/2020

### Equivalence of Dataflow Graphs via Rewrite Rules Using a Graph-to-Sequence Neural Model

In this work we target the problem of provably computing the equivalence...
06/01/2021

### Proving Equivalence Between Complex Expressions Using Graph-to-Sequence Neural Models

We target the problem of provably computing the equivalence between two ...

## 1 Introduction

Program equivalence is an active field of study, allowing us to formulate when two different programs can be used interchangeably. This can be done in two ways. One can axiomatise which programs should be considered equivalent, and derive a notion of program equivalence from those axioms. Alternatively, one can formulate theoretical tests on programs, which check whether the program satisfies a certain behavioural property. Two programs are then considered equivalent if they satisfy the same properties.

We consider such methods in the presence of functional languages with algebraic effects in the sense of [effect, alg_eff, Plotkin:2004]. Effects describe interactions a program has with the outside world. Because of possibly varying inputs from the outside world, the same program may produce different results at different executions. This happens, for instance, if the program requests a random number, or reads off information from a global store location. Different possible continuations of a program can be combined using algebraic operators. E.g. we get program terms like , which probabilistically chooses fairly between executing program term and program term .

Traditionally, behaviour of algebraic effects has been formulated using algebraic equations [PlotkinPower02, Domains]. One could for instance state that and . In recent research based on [op_meta], modalities are used to formulate properties on computations that exhibit effectful behaviour [modal, modal_journal, Sound]. Using these modalities, Boolean predicates on sets of return values could be lifted to Boolean predicates on algebraic expressions over such return values. For instance, one might test whether the probability that a program returns an even number exceeds some threshold. These modalities are then used as the foundation upon which a logic of program properties is constructed, specifying a notion of behavioural equivalence for functional languages.

In [Quantitative], such logics were generalised to quantitative logics built using quantitative modalities. In most examples of effects, it is more natural to use a singular quantitative modality, given by Eilenberg-Moore algebras (e.g. used in [HasuoGeneric]), to describe effectful behaviour. This generalisation also enables us to describe combinations of effects more easily.

We say that an Eilenberg-Moore algebra complements a set of equational axioms when they induce the same relation on algebraic expressions over the natural numbers. This then extends to them specifying the same notion of program equivalence on functional languages. For most examples of effects, an Eilenberg-Moore algebra exists which complements the traditionally chosen set of axiomatic equations. In this paper, we show that these algebras can be directly constructed from the algebraic relations on algebraic expressions induced by the axiomatic equations, motivating their formulation in the literature.

In general, we construct from any set of equations an Eilenberg-Moore algebra. We show that this algebra complements the set of equations if an additional, relatively weak, property is satisfied. If a stronger property is satisfied, which does not hold for all examples, we can also generically generate a set of Boolean modalities complementing the axiomatic equations.

In Section 2, we study algebraic expressions and algebraic relations given by effects in general, and several examples in particular. In Section 3 we look at the complementing view on effects, Eilenberg-Moore algebras, and how to construct them using algebraic relations. Section 4 discusses some extra topics surrounding effect descriptions, after which we look at Boolean modalities in Section 5.

## 2 Operations and Equations

For each effect, we specify an effect signature of algebraic operations , containing effect operators with an associated arity . See Subsection 2.2 for examples.

###### Definition 2.1.

An effect tree (henceforth tree), over a set , determined by a signature of effect operators, is a labelled tree of possibly infinite depth, whose nodes have the following possible forms:

1. A leaf node labelled (representing divergence).

2. A leaf node labelled (representing success or termination).

3. A leaf node labelled where .

4. A node labelled with children , when the operator has arity . In this case, we write the subtree at that node as .

5. A node labelled with an infinite sequence of children, when the operator has arity . We write the subtree at that node as (we may use this notation for the nodes described in point 4 too).

This definition varies slightly from effect trees used in [modal_journal, Quantitative], with the addition of a top element .

We define a functor on the category of sets, sending each set to the set of trees over determined by , and sending each function to the function replacing each leaf of its input by . We will henceforth write instead of , leaving the underlying signature implicit. The functor determines a monad , where , and flattens a double-tree into a tree by replacing each leaf of by as a subtree. Both and are natural transformations, satisfying the monad laws. For , define .

Given a preorder we define an order on coinductively according to the following rules:

1. .

2. .

3. .

An order is -complete if it contains the supremum (limit) of any ascending sequence of elements. If is an -complete preorder, then is an -complete preorder. Note that if has the discrete order, it is -complete, and hence is -complete, and its order is specified by rules 1 and 3 only.

### 2.1 Equations and Inequations

We use the natural numbers to describe a countable set of variables, and trees as the set of possibly infinite algebraic expressions. An algebraic equation is simply the assertion that two expressions are equal ‘’, and an algebraic inequation is the assertion that two expressions are ordered ‘’. Both such statements can be seen as elements of .

We study algebraic relations containing such assertions, in particular inequations, and write for . We study properties of , given e.g. in [op_meta, LopezSimpson].

1. is reflexive if for any , then .

2. is transitive if for any , .

If satisfies R and T, then it is a preorder. The next two properties discuss substituting trees for variables in the algebraic expressions, with compositionality from [op_meta].

1. is substitutional if and ,  .

2. is compositional if and , s.t. ,  .

Note that reflexivity and compositionality together imply substitutionality. We consider two more properties, concerning the order on .

1. is ordered if .

2. is admissible if for any two increasing sequences and , if then  (here is the limit/sumpremum of the sequence ).

Note that if is ordered, then for any . Moreover, if is ordered, then it is reflexive. We call an algebraic relation complete if it satisfies all of the six properties given above, though as noted it is enough to satisfy T, C, O and A. Note that is a complete algebraic relation.

Given a set of axioms , we define the resulting algebraic relation as the transitive, compositional and admissible closure of the relation given by . As such, is the smallest complete relation containing . See [LopezSimpson] for some more details on axiomatically defined preorders.

We consider the empty set as a subset of given by . As such, we see as the subset of containing algebraic expressions without variables, only having and as leaves. In this paper, takes the place of the set unit type trees from [modal_journal] as the basis for studying effects111There is a bijection between the two sets, with the leaf corresponding to the unit leaf. However, this bijection does not preserve the order..

We consider one more property for algebraic relations.

###### Definition 2.2.

is base-valued if for any :

 (∀f:N→T0. f∗(a)⊑If∗(b)) ⟹ a⊑Ib.

The property asserts that the algebraic relation is completely specified by its subset , which we call the base relation .

### 2.2 Effect examples

We look at some examples of effects and their algebraic operations. Moreover, we will specify the usual axiomatic equations given in the literature (e.g. in [PlotkinPower02, Domains]). For clarity, we will use variables instead of numbers when writing elements of , and we will often leave out the leaf-notation, writing instead of . For each example, turns out to be base-valued, but we omit the proofs.

###### Example 2.3 (Nondeterminism).

We first consider the example of nondeterminism, where the effect signature contains a single algebraic effect operator ‘’ of arity . This operator chooses between two possible continuations in a completely unpredictable manner, under control of a scheduler which makes choices according to some unknown decision process. Because of its unpredictable nature, no probability can be associated to the choices. As such, its equational axioms are given by idempotency, symmetry and associativity:

###### Example 2.4 (Probability).

We consider the example of probability, with one algebraic effect operator ‘’ with arity , which chooses between two continuations randomly, by fair choice. In this case, the equational axioms are given by idempotency, symmetry together with two more axioms:

 prob(x,x)=x,prob(x,y)=prob(y,x) prob(prob(x,y),prob(z,w))=prob(prob(x,z),prob(y,w)),μx.prob(y,x)=y.

Here, stands for the infinite tree such that .

###### Example 2.5 (Global Store).

We consider a global memory location which contains some natural number. Our effect signature contains a lookup operator ‘’ with arity , which looks up the stored natural number and continues the computation accordingly, and for each we have an update operator ‘’ which updates the stored number to an (this can be generalised to multiple store locations). We have the following equations as axioms, ranging over natural numbers :

 updaten(updatem(x))=updatem(x),lookup(m↦x)=x,updaten(lookup(m↦xm))=xn, lookup(m↦updatem(xm))=lookup(m↦xm).
###### Example 2.6 (Exception catching).

This example is similar to the algebraic description of the jump effect from [Fiore]. We consider a set of exceptions , and for each we have an operator ‘’ of arity raising the exception, and an operator ‘’ of arity catching that exception. The computation will execute the computation , and if the exception is raised by , it continues by executing the computation given by . We consider the following axiomatic equations:

###### Example 2.7 (Input).

We consider the situation in which a computation may ask for a binary input from the user of the computer. This is modelled using a single operation ‘’ of arity two, where is the computation which asks a binary input, and continues with if the input is , and if the input is . In this example, the entity giving the inputs can keep track of what choices are made. As a result, we will not assume any axioms, since any two different trees of can be distinguished by testing their evaluation with a particular sequence of inputs.

###### Example 2.8 (Cost).

We consider the situation in which we associate a cost to computation, for instance energy, time, or monetary cost necessary to evaluate a program. We consider a single tick operation ‘’ with arity one, where evaluates after a unit of cost has been paid. Elements of are given by a sequence of ticks, which is either infinite, or results in , , or a natural number. We consider one axiom, asserting that cheaper is better:

 tick(x) ≤ x .

As a consequence, we can show that , and hence . Using admissibility, we can prove that the algebraic expression given by an infinite sequence of ticks is equal to .

###### Example 2.9 (Nondeterminism + Probability).

As a last example, we look at a combination of effects. As signature we take with two binary operators, and we assume the axiomatic equations of the two effects from Examples 3.11 and 3.12 hold. Moreover, we state the following interaction law:

 prob(x,nond(y,z)) = nond(prob(x,y),prob(x,z)) .

## 3 Eilenberg-Moore Algebras

On the opposite side of equations, we have distinctions. We will use Eilenberg-Moore algebras to specify tests on algebraic expressions as done in [Quantitative]. If two expressions give us a different result for a test, we consider them to be distinct.

###### Definition 3.1.

Given a monad , an Eilenberg-Moore algebra (henceforth EM-algebra) is a morphism on some carrier object , such that the following two diagrams commute:

Given a preorder , and an algebra on the tree functor , we define a relation as follows:

 a⊑αb⟺∀h:N→A.α(T(h)(a))⊴α(T(h)(b))

We say that complements if coincides with . The algebra complements a set of axiomatic equations , if complements .

Suppose complements . For any two algebraic expressions , it is either possible to show that using the axioms from and proof rules such as compositionality and admissibility, or it is possible to show that using the EM-algebra together with some test . As such, we have both a method for showing equivalence, and for showing inequivalence.

Note that is reflexive and transitive. We look at some other general results.

###### Lemma 3.2.

If is an EM-algebra, then is substitutional.

###### Proof.

Assume , and take , we need to show that . Let , then , and hence . Note that the following diagram commutes:

 (1)

Hence . So we have the desired result. ∎

We call monotone if it preserves order: .

###### Lemma 3.3.

If is a monotone EM-algebra, then is ordered and compositional.

###### Proof.

Let and , then and hence by monotonicity of we get .

Assume , and take s.t. , we need to show that . Because of substitutivity, , so with transitivity it is sufficient to show that .

Let , so for all , . Hence , and since is monotone:

 α(T(α∘Th∘f)(a)) ⊴ α(T(α∘Th∘g)(a)).

Using diagram (1) from the previous lemma, we conclude that , and hence . We conclude that is compositional. ∎

Last but not least, we establish a sufficient condition for admissibility. Note that is a morphism in the category of -cpos precisely if is -complete and preserves limits of ascending sequences.

###### Lemma 3.4.

If is an EM-algebra in the category of -cpos, then is admissible.

###### Proof.

Let and be ascending sequences of trees from such that . Let , then and hence:

 α(T(h)(⋁i∈Nai))=⋁i∈Nα(T(h)(ai))⊴⋁i∈Nα(T(h)(bi))=α(T(h)(⋁i∈Nai)).

We conclude that , so is admissible. ∎

### 3.1 From Equations to EM-algebras

We specified how we can extract a relation on from an EM-algebra . If this algebra forms a morphism in the category of -cpos, the resulting relation is complete. We will now go in the other direction, extracting an algebra from a relation on in a novel way. In particular, we will formulate an EM-algebra using the relation specified by .

We denote by the largest symmetric subset of a relation . Remember that .

###### Definition 3.5.

The value space of is given by , where .

We have a function defined by . Note that by transitivity, . We define an order on where , which by transitivity of is well-defined.

A choice function for is a function such that for all , . Note by the above properties that for all , . We specify an algebra as the function which makes the following diagram commute:

###### Lemma 3.6.

If is reflexive, transitive and compositional, then for any two choice functions and , is equal to .

###### Proof.

Note that for any , and , hence . Take some . Note that the number of leaves of is countable, hence we can find a tree and a function such that . Since by reflexivity, and for each , , it holds by compositionality that . Hence . ∎

Hence, is invariant under choice of . From now on, we will fix a choice function , and simply write for . We will show that is an EM-algebra. First, we establish a useful lemma.

###### Lemma 3.7.

If is reflexive, transitive and compositional, then the following diagram commutes:

###### Proof.

This has a similar proof to the previous lemma. Take , and define and such that . For all , holds, hence by compositionality, . So . ∎

###### Proposition 3.8.

If is reflexive, transitive and compositional, then is an EM-algebra.

###### Proof.

We use the monad laws together with definition of and Lemma 3.7 to observe that the following diagrams commute:

###### Lemma 3.9.

If is reflexive, transitive and compositional, then for all , .

###### Proof.

Assume that , and let , then . So by compositionality, , hence , so we conclude that: . ∎

To prove that the constructed EM-algebra complements the algebraic relation, we use a relation lifting operation. Given a relation , we define the lifted relation coinductively as follows:

1. ,

2. .

3. .

This can be seen as the functorial lifting of relations, and has the following two properties:

• .

• .

###### Proposition 3.10.

If is reflexive, transitive, compositional and base-valued, then complements .

###### Proof.

Assume that , we prove that using that is base-valued, in order to conclude using the previous lemma that complements .

Let , then , hence . So we get , and hence . Note that for all , .

Hence for any , , so by compositionality, we derive that . We conclude that:

 f∗(a) ¯¯¯¯B μ(T(c∘[−]∘f)(a)) B μ(T(c∘[−]∘f)(b)) ¯¯¯¯B f∗(b).

So by transitivity, . Hence by base-valuedness, . ∎

### 3.2 EM-algebras for the examples

We look at the examples of effects given in Subsection 2.2, and study what value spaces and EM-algebras they generate. For every example, the constructed EM-algebra satisfies the following rules: , , for any , and for any sequence . To complete the definition of the algebras in the following examples, we will specify their behaviour over algebraic effect operators. These local functions together with the above properties uniquely characterise the morphism .

###### Example 3.11 (Nondeterminism).

The first example is nondeterminism with the binary operation . We study as derived from the induced equational theory , where is given by the equations from Example 3.11. We get three elements of , each denoting an equivalence class of .

1. Any tree without a -leaf is equivalent to .

2. Any tree with at least one -leaf, which moreover either has a leaf or is infinite, is equivalent to .

3. Any tree which is finite and only has -leaves, is equivalent to .

If we write for , the ordered set is given by .

We give some informal arguments for the above observations. Note first that:

 ⊥≤T0nond(⊤,⊥)≤T0⊤,

which proves the given ordering. Now consider a finite tree . If only has one type of leaf, consequetive application of the idempotency axiom reduces that tree to just that leaf. If has both and leaves, applying symmetry and transitivity can change the tree to one of the form , where only has leaves, and only -leaves. So can be reduced to with idempotency. Now consider an infinite tree , and let be a sequence of finite trees approximating . If only has leaves, all finite approximations only have leaves, and each is equal to . So by admissibility, . If has at least one leaf, all finite approximations have at least one -leaf (by studying ). Moreover, there must be an such that for all , has a -leaf, so . Hence . We conclude that all trees must be equal to either , or .

The induced EM-algebra corresponds to the algebra operation given in [bat], with and for any such that .

###### Example 3.12 (Probability).

We study resulting from Example 2.4. Consider the real number interval , and the function satisfying the following rules , , , and for any ascending sequence . For any two elements , holds if and only if . Moreover, is surjective222Since rationals with power 2 denominators are dense in the real numbers.. Hence can be expressed as , where is given by . The induced EM-algebra calculates the expected result, where .

###### Example 3.13 (Global Store).

The above two examples are standard in the literature, and do not explicitly use that element is the top element of . In the case of global store however, this fact is important. We study resulting from Example 2.5.

Note that . Hence for any two natural numbers , . So:

 ⊥ ¯¯¯¯I lookup(m↦⊥) ¯¯¯¯I lookup(m↦updatem(⊥)) ¯¯¯¯I lookup(m↦updaten(⊥)) ¯¯¯¯I updaten(⊥).

With similar reasoning, . We derive that for any tree , there is a function such that .

Studying in particular, we see that each is equivalent to for some unique function . For , holds if and only if for any , . Note moreover that for any function , there is an element such that . So, can be expressed as the powerset , where , and the order is given by inclusion. We see this powerset as the set of assertions on the global state.

The induced EM-algebra calculates the weakest precondition: it gives the set of starting sates for which the tree reaches a leaf with a final state satisfying the assertion .

###### Example 3.14 (Exception catching).

We look at resulting from Example 2.6. Note that any element of is, under , equivalent to one of the following three types of trees: or for some . The elements are ordered in the following way:

 ∀e∈Exc.[⊥]⊴[exepte]⊴[⊤].

There is no ordering between and if . The induced EM-algebra is defined by .

###### Example 3.15 (Input).

We look at the input effect as given in Example 2.7. We did not specify any axioms there, which can be motivated from the perspective of testing distinctions. A test of an input program would be checking a series of inputs until either: a) the program terminates successfully (marked by ), or b) the program stops asking for inputs because of divergence (marked by ). Since there are no axioms,  is given by , is given by , and the constructed EM-algebra is given by the function .

###### Example 3.16 (Cost).

We look at for the cost effect given in Example 2.8. Considering the observations made there, we see that is given by the set containing:

 [⊤]⊵[tick(⊤)]⊵[tick(tick(⊤))]⊵[tick(tick(tick(⊤)))]⊵…⊵[⊥].

We can represent as with reverse order. The constructed EM-algebra is , where and applied to the infinite sequence of ticks gives .

###### Example 3.17 (Nondeterminism + Probability).

We look at the induced EM-algebra for the combination of effects given in Example 2.9, which coincides with a description from [LopezSimpson]. The value space is given by , and the EM-algebra by and . E.g. .

## 4 Notes on logic and equivalence

In this section we will look at some more connections with the quantitative logic from [Quantitative] used to specify behavioural equivalence. In particular, we will look at two topics discussed in that paper.

### 4.1 Relators

In proving that the behavioural equivalence given by an EM-algebra is a congruence, the paper [Quantitative] made essential use of a connection with applicative bisimilarity [Abramsky90]. Applicative bisimilarity gives us a technique for proving equivalence between higher-order functional programs. In [Relational], applicative bisimilarity is defined for effectful programs using relators. We will briefly look at how we can derive such a relator from algebraic relations and EM-algebras.

###### Definition 4.1 ([Levy11, Relational]).

A relator for a monad is a family of operations, giving for each pair of sets , , a function sending relations to relations , such that:

 (1) =MX⊆Γ(=X),(2) Γ(R)Γ(S)⊆Γ(RS),(3) R⊆S⇒Γ(R)⊆Γ(S), (4) ∀f:X→Z,g:Y→W,R⊆Z×W. Γ({(x,y)∣f(x) R g(y)})={(a,b)∣M(f)(a)Γ(R)M(g)(b)}

For instance, defined at the end of Subsection 3.1 is a relator. We give two ways of constructing a relator.

Firstly, given a complete algebraic relation , we define the operation for the monad as follows: For any two sets and , relation , and elements and , holds if and only if for any two functions :

 (∀x∈X,y∈Y,x R y⇒f(x)⊑Ig(y))⟹f∗(a)⊑Ig∗(b).

Secondly, given an Eilenberg-Moore algebra , we define the operation for the monad as follows: For any two sets and , relation , and elements and , holds if and only if for any two functions :

 (∀x∈X,y∈Y,x R y⇒f(x)⊴g(y))⟹α(Tf(a))⊴α(Tg(b)).

if is complete. Moreover, is base-valued precisely when .

###### Lemma 4.2.

If is a complete lattice, and is a monotone EM-algebra, then is a relator.

This lemma holds for all of the given examples. If moreover is a morphism in the category of -cpos, then satisfies the additional properties required in [Relational] in order to use Howe’s method and prove that applicative bisimilarity is compatible. As such, is required to be a complete lattice by the theory developed in [Quantitative]. This is the case in all our examples.

### 4.2 Involutions

In [Quantitative], a quantitative logic is defined with the intention to specify a behavioural equivalence. One optional ingredient in that definition is the notion of negation, an involution on the carrier set of the Eilenberg-Moore algebra. Given how trees are formulated in this paper, with the addition of a top element , there is a natural candidate for an involution function on .

A function on a preorder is an involution if, a) for all , , and b) for all , holds if and only if . Note in particular that with the discrete ordering has the identity function as an involution, and has a trivial involution. Given a preorder with involution , we let be the function that takes a tree , and produces a tree of the same shape by doing the following two alterations:

• Replace each leaf of labelled with a leaf labelled , and vice versa.

• Replace each leaf of labelled , with a leaf labelled .

Note that gives an involution on with respect to the tree ordering . In particular, and have an involution.

In order for to induce an involution on , we need to preserve involutions:

###### Definition 4.3.

preserves involutions if .

We call the inequation the involution-complement of . If preserves involutions, then the function given by is well-defined and hence gives an involution on . Unfortunately though, for all but the input example, does not give an involution on .

For the cost example, the does not give an involution because simply does not have an involution. Remember the asserted axiom , and note that its complement is not contained in . Hence, in order to get a proper involution, we need the