One way of seeing program semantics is as the science of program equivalence. Each way of giving semantics to programs implicitly identifies which programs are equivalent. Similarly, a notion of program equivalence can be seen as a way of attributing meaning to programs (namely, the equivalence class to which the program belongs). This point of view makes semantics a powerful source of ideas and techniques for program transformation and program verification, with the remarkable advantage that such techniques can be defined in a compositional and modular way.
However, there are circumstances in which equivalences between programs, being purely dychotomous, are just not informative enough: two programs are either equivalent or not, period. No further quantitative or causal information can be extracted from two programs which are slightly different, although not equivalent. Furthermore, as program equivalences are usually congruences, and therefore preserved by any context, programs that only differ in peculiar circumstances are also just non-equivalent ones. For these reasons, methods alternative to program equivalence have to be looked for in all (very common) situations involving transformations that replace a program by one which is only approximately equivalent , or when the specifications are either not precise or not to be met precisely (e.g. in modern cryptography 
, in which most security properties hold in an approximate sense, namely modulo a negligible probability).
The considerations above led the scientific community to question the possibility of broadening the scope of program semantics from a science of equivalences to a science of distances between programs. By the way, the possibility of interpreting programs in domains having a metric structure has been known since the 1990s [17, 18]. Recently, Mardare, Panangaden, and Plotkin have introduced a notion of quantitative algebra  that generalizes usual equational reasoning to a setting in which the compared entities can be at a certain distance. In this way, various notions of quantitative algebra have been shown to be captured through a formal system, à la Birkhoff .
Still, when the programs at hand are higher-order functional programs, the construction of a metric semantics faces several obstacles. First, it is well-known that the category of metric spaces and non-expansive maps, providing the standard setting of the approaches just recalled, is not a model of the simply typed -calculus (more precisely, it is not cartesian closed). Furthermore, finding relevant sub-categories of enjoying enough structure to model higher-order programs can lead to trivial (i.e. discrete) models, and several (mostly negative) results have remained so far in the folklore (with a few notable exceptions, e.g. ).
In this paper we bite the tail of the dragon: we apply the framework of quantitative equational theories and algebras from Mardare et al to the cases of combinatory logic and the -calculus, and we try to highlight features and obstacles in the construction of higher-order quantitative algebras, at the same time showing the existence of several interesting models.
There are various reasons for exploring combinatory algebras, i.e. applicative structures where the -rule fails. The first is that these structures naturally arise in various contexts, most notably in Game Semantics and in particular in the “Geometry of Interaction”-construction as introduced by Girard and Abramsky, see . The -rule can then be enforced only by introducing a rather complex notion of equivalence relation, whose fine structure is usually rather awkward to grasp. The second reason is that combinatory algebras, being indeed algebras, might appear at first sight to be amenable straightforwardly in the first order framework of quantitative algebras of Mardare et al. We show that this is illusory, because the impact of the basic assumption that constructors are non-expansive, i.e. Axiom NExp (see Section 4) is very strong, even in a context which could appear to be algebraically well-behaved. Finally, even if it is convenient to assume the -rule, in reasoning on higher-order programming languages, showing that it holds in implementations is not at all immediate and, when side-effects are present, it needs to be carefully phrased.
The contributions of this paper are threefold:
(i) We introduce, following the framework defined by Mardare et al, quantitative generalizations of the standard notions of weak -theories and -theories , as well as their corresponding notions of algebra. This is in Section 3, Section 4, and Section 5, respectively.
(ii) We study properties and examples of algebras for such theories, as suitable sub-categories of . In particular, we highlight the relevance of ultra-metric and injective metric spaces in the construction of non-trivial (i.e. non discrete) algebras. Some examples are discussed through Section 2 and Section 5, further properties and examples are in Section 6.
(iii) Finally, we discuss algebras obtained by relaxing the conditions from Mardare et al: either by replacing metrics by partial metrics [9, 32], i.e. generalized metrics in which self-distances need not be zero, or by relaxing the non-expansiveness condition and introducing a class of approximate quantitative algebras. This is in Section 7 and Section 8.
2 Preliminaries on Metric Spaces
In this section we discuss a few properties of metric spaces and their associated categories, which provide the general setting for quantitative algebras in the sense of Mardare et al In particular, we recall the definition of ultra-metric spaces, as well as partial ultra-metric spaces [9, 32]. The latter is a class of generalized metric spaces in which self-distances are not required to be but only smaller than any distance of the form .
A pair formed by a set and a function
(i) a pre-metric space if it satisfies, for all , and ; (ii) a (pseudo-)metric space if it satisfies , , and, for all , ; (iii) an ultra-metric space if it satisfies , and, for all , ; (iv) a partial ultra-metric space if it satisfies , and, for all , . Since all metrics we consider are “pseudo”, from now on we will omit this prefix. Observe that an ultra-metric space is also a metric space. Moreover, a partial ultra-metric space also yields an ultra-metric space , with if and otherwise. Usually, partial metric spaces are defined using a stronger version of the triangular law, given by . However, for partial ultra-metrics this condition is equivalent to (see e.g. ).
The standard morphisms between metric (ultra-metric, partial ultra-metric) spaces and are the non-expansive functions, i.e. those functions such that for all , . We let (resp. , ) indicate the category of metric spaces (resp. ultra-metric spaces, partial ultra-metric spaces) and non-expansive maps. All categories and are cartesian, the product of and being given by . In and the cartesian functors have right-adjoints given, respectively, by and , where for all metric space , . For this reason, both categories are cartesian closed.
By contrast, is not cartesian closed. Indeed, the functor is right-adjoint in (and thus also in ) to the functor , but for all metric spaces , is isomorphic to the cartesian product only when and are ultra-metrics. On the other hand, the exponential of and in , if it exists, is necessarily of the form , where
We use the Greek letter , since, as we’ll see, this metric is tightly related to the interpretation of the “-rule” of the -calculus. Notice that in general is only a pre-metric. Indeed, the category of pre-metric spaces and non-expansive functions is cartesian closed, while the exponential of and exists in precisely when further satisfies . Let and be metric spaces. The exponential of and in , if it exists, is isomorphic to .
By definition is the exponential of of iff there exists a family of isomorphisms , natural in . From this it follows that we can identify, up to bijection, with , and we can let and .
It remains to show that , for all . Let . Since is non-expansive we have, for all and , , which shows that .
For the converse direction, let and be the two points metric space given by . Let be the function given by and . Then for all and , , which shows that . Using the non-expansivity of we deduce then , that is, . ∎
The distances computed with are larger than those computed with . For example, given the functions with and , one has , while , since for all , there exist such that . Moreover, the distance function should not be confused with the similar function , which is also not in general a metric. Indeed, consider the metric space consisting of the subset as a subspace of , and the non-expansive functions and . While for we have , we have as above that for all , there exist such that .
We will exploit the following useful characterization of exponentiable objects in 111An object in a cartesian category is exponentiable when for all object the exponential of and exists in . In particular is cartesian closed iff all its objects are exponentiable.: [] A metric space is exponentiable in iff for all and such that , the condition below holds:
Condition ( ‣ 2) intuitively requires to have “enough points”. For example, the set , as a subspace of , is not exponentiable in (take and : a point between and is “missing”). Instead, condition ( ‣ 2) always holds when is injective (see [21, 13]): for any collection of points in and positive reals such that , there is a point lying in the intersection of all balls . This implies that the sub-category of formed by injective metric spaces is cartesian closed. Since the Euclidean metric is injective, there is a cartesian closed sub-category of formed by “simple types” over closed real intervals, that we’ll use as working example.
Let be the set of simple types over the intervals, defined by , for all intervals (with and ) and . For any , the metric spaces are defined by , , , , and .
3 Many-Sorted Quantitative Theories and Algebras
In this section we introduce quantitative theories and algebras in the sense of . In order to cover both the typed and the untyped case, we consider many-sorted theories and algebras, hence combining the quantitative (but one-sorted) approach from  with the qualitative (but many-sorted) approach from .
Notation. For any set , an -sorted set is an -indexed family of sets (i.e. an object of ), and an -sorted function between -sorted sets is an -indexed family of functions (i.e. a morphism in ). For a set , we denote by the set of all finite (possibly empty) lists of elements of , we let range over and use for concatenation. For an -sorted set and , we let . We let denote a distinguished -sorted set such that for all , is a countably infinite set of variables. For any -sorted set , -sorted function , pairwise disjoint variables , with and with , we let indicate the -sorted function mapping to and being as on all other variables.
[Many-Sorted Signature] An -sorted signature is an -sorted set (i.e. an object of ).
The objects will be called symbols of the signature.
[-Algebra] A -algebra is a pair where is a -sorted family and associates each symbol with a function , where , for . For any object of , the free -algebra over , noted , is the -sorted set defined by the following conditions: (i) for all , ; (ii) for all and , then .
Intuitively, is the set of “terms of sort with parameters in ”. Free algebras enjoy the following universal property:
For any -algebra and map there exists a unique -homomorphism extending , that is, such that , where is the inclusion map.
Given a function , if is some term of sort with parameters in , is the result of “substituting” each parameter in with .
Let us now introduce the equational language of quantitative theories.
Let be an -sorted signature.
(i) A quantitative -equation over is an expression of the form , where , and .
(ii) For all , let be the set of indexed -equations of the form , for some and , and be the set of indexed -equations of the form , where and .
A consequence relation on the free -algebra is a relation closed under all instances of the following rules (where vary over all ):
if for all and , then ;
if , then ;
, for all ;
if , then implies .
Notice that rule (Arch) has infinitely many assumptions.
We let indicate the set of quantitative inferences on and indicate the set of basic quantitative inferences. Axioms for theories will be basic quantitative inferences.
[Many-Sorted Quantitative Theory] Let be a set of basic quantitative inferences. Let be the smallest consequence relation including . The quantitative equational theory over generated by is the set . The elements of are the axioms of .
To the syntactic notion of quantitative theory there corresponds a semantic notion of quantitative algebra, given by a -algebra endowed with suitable metrics.
[Many-Sorted Quantitative Algebra] Let be an -sorted signature. A quantitative -algebra is a tuple where is a -algebra and is an -sorted family of metrics such that for all , .
Given a quantitative -algebra, we can define a multicategory whose objects are the metric spaces , and where for all , contains all functions such that for some term , . For brevity, we will often abbreviate as .
Let be a quantitative -algebra. satisfies a quantitative inference (denoted ), if for all , the following holds:
satisfies a quantitative equational theory (noted ) if it satisfies all inferences in .
Notice that the interpretation of rule (Nexp) implies that functional terms need to be interpreted as non-expansive morphisms.
4 Quantitative Weak -Theories and Algebras
As is well-known (see e.g. ), a purely algebraic approach to the -calculus is provided by combinatory logic . Hence, it is natural to start from this calculus. The equational theory of captures so-called weak -theories , namely -theories where the -rule (discussed in more detail in Section 5) may fail. In this section we introduce quantitative weak -theories and we discuss their algebras, of which itself is a notable example.
[Applicative Signature] Let be a set of sorts (called types) endowed with a binary function . An applicative signature is a -sorted signature which includes symbols , for all .
We will often note infix, i.e. , or simply as , when clear from the context. For all and , we let . A notable example of applicative signature is the following: [-Signature] Let be the applicative signature which includes symbols , , , for all . The terms of combinatory logic are the elements of the free -algebra, .
Definition 4 above comprises both the typed and untyped case. In typed Combinatory Logic the set of types includes at least a base type , i.e. a type which is not in the image of and is injective, while in the untyped case is a singleton set and hence . In the traditional language of “syntax and semantics”, used for instance in , when , the function of Proposition 3, amounts to the notion of intepretation of a term in the environment , namely .
We now introduce the natural notion of theory for a -signature:
[-Theory] The quantitative equational theory over , is generated by the axioms , , and . We call (quantitative) weak -theory any theory including .
The set (cf. Example 2) is a particular instance of the set . Let be the signature obtained by enriching with 0-ary symbols for all , and -ary symbols for all . Let be the theory obtained by extending with all axioms whenever as well as all axioms for all rational .
A well-known property of Combinatory Logic is functional completeness: for any term and variable , one can construct a term so that “simulates” -abstraction in the sense that one can prove . This leads to the following definition:
[Quantitative Weak -Algebra] An applicative quantitative -algebra is said a quantitative weak -algebra if for all , , and , the set is non-empty.
Any quantitative -algebra satisfying is a quantitative weak -algebra. Vice versa, any quantitative weak -algebra satisfies .
(sketch) For any term of combinatory logic, one can define a term , only depending on the variable , i.e. such that , where maps to and is the identity on the remaining variables. We simply let , , where does not occur in , and . The reverse implication is obtained by choosing elements , and . ∎
We obtain a quantitative weak -algebra by letting , where , , and . It is clear that (cf. Example 4).
Following , the condition from Def. 4 can be specified in categorial terms: a cartesian multicategory is a model of precisely when for all objects of there is an object (called a very weak exponential of and ) together with a surjective natural transformation . When is the multicategory , the conditions of Def. 4 imply that is a very weak exponential of and in : a family of multiarrows , natural in , is given by , and the non-emptyness of the sets corresponds to the surjectivity of this transformation.
Notice that itself admits very weak exponentials for all of its objects, i.e. it is a very weak CCC in the sense of , provided we endow with the metric for metric spaces and , where for is if , and otherwise is . Intuitively, when , measures the diameter of the interval spanned by the image of both and .
and do not coincide: for example, consider where and for while for ; then one can show that while .
is in general rather odd since the identity is an isolated point ifis infinite and not trivial. Assume that the sequence converges to the identity . Then we have for all the following inequality: . While can be made arbitrarily small, a can always be picked, provided the metric is not trivial and the space is infinite, such that . Thus for all . The constructions just sketched yields a different weak -algebra over the reals , where is defined like but for . Notice that we still have , since and agree on distances of types .
[Soundness and Completeness of Quantitative Weak -Theories] For any quantitative weak -theory over , iff holds for any quantitative weak -algebra such that .
Following Remark 3, in the case of partial ultra-metric spaces we will talk of partial weak -theories and partial weak -algebras.
5 Quantitative -Theories and Algebras
As we recalled, weak -theories do not fully capture the equational theory of the -calculus, as they fail to capture the so-called -rule . In our quantitative setting, this rule can be expressed as the inference provided the equation on the left of is locally universally quantified: the righthand equation holds under the condition that, for all possible value of , the lefthand equation holds. This kind of quantitative inferences differ from those seen so far. The reason for this proviso is that it involves the higher-order operator , which “binds” the variable . The example below shows that quantitative weak -algebras fail to capture this rule.
The -rule fails in the weak -algebra : let (where and ) be, respectively, the identity function and the function ; for any , we then have , which shows . However, since , we deduce .
In order to define quantitative -theories we could follow Curry  and “strengthen” the set of axioms, in fact mere equalities, satisfied by a -algebra and essentially do away with the -rule and all higher order features. The alternative, that we develop in this section, is to take abstraction and the -rule as first class elements of our theories and algebras. This will require a number of generalizations of the original approach of .
At the level of syntax, the first step is to enrich the class of symbols with higher-order operators of the form . The occurrence of the variable is part of the symbol itself.
[-Signature] Given an applicative -sorted signature , let be the applicative -sorted signature further including the symbols , for all and . The -terms are the elements of the free -algebra, .
Terms will be denoted by or simply . Free and bound variables, open and closed -terms are defined as usual. For a -term , we denote by , , the sets of free, bound, and all variables in , respectively. In order to simplify the notation we deal with bound variables by implementing directly Barendregt’s “hygiene condition”. For any function there exists a function such that corresponds to the substitution of for in , for any variable occurring free in . Given pairwise disjoint variables , with and terms , with , we indicate the “substitution” simply as .
In order to be able to express correctly the -rule we generalize quantitative equations to expressions of the form , where indicates a finite set of variables which are intended to be “locally quantified” on the left of .
[-equation] A quantitative -equation is an expression of the form , where , , , . The set is the set of locally quantified variables in the equation.
We let indicate the set of quantitative -equations
(i) A consequence relation on is a relation closed under the rules (Cut)-(Nexp) from Def. 3 (with everywhere replaced by ), together with the following rules: