arXiv:1703.01204v1 [cs.LO] 3 Mar 2017 hr ti rudta h ennflcnet hudbe should concepts meaningful the that argued cognition of is setting models it mathematical closed where in compact important a is into Convexity incorporated be can convexity manner. domain theoretic problem new process a a model uncon- in to an of trying clea is when theory highlights faced This therefore challenges the [9]. and [8], area, in application in examples ventional developed two as cognition, with human categories closed pact categories. closed examples compact the in of live many above and calculus, cited t graphical cat- as elegant importance suitable Dagg an particular have a interest. of are of find categories phenomena closed to compact the how capturing of setting arises egorical question the approach, line [4], even theory and [6] control circuits [3], [7]. electrical algebra graphs [5], flow processes languag Markov signal natural encompasses including [2], perspective domains, processing theoretic application process other the many foundations. of quantum scope and compositiona The ab computation reasoning quantum This in in domain. successful questions incredibly application been the has perspective seman to categorical a tailored language and tics graphical systems, composite a about of reasoning consist typically processes. theories abstract process of theories compositional describe to oesta pnu rudfrfrhrdevelopment. further for ground and up literature, open the pa- techniqu that from our the models models existing illustrate in many We functorial constructing domains. b are application connections constructions mathematical different establishing Our proof choices, maps. rameter t of and pre of converse choice relational are graphs of the categories analogues provide resulting and and The enriched provability. universe or st mathematical algebraic relevance f of Ou choice along ambient a theories. values, the truth process binary the for of variation, of notion models axes the of suitab generalizes construction a framework the identify for to work how as of such model. ne arises theoretic a fields question process investigating When to the theory. linear network applied application, processing, and successfully systems language namical natural been computation, se have quantum categorical They underlying an tics. with reasoning compositional sorfis xml,w osdrhwtento of notion the how consider we example, first our As com- dagger new constructing of process the illustrate We theoretic process the of application new a considering When [1] introduced been recently has theory” “process term The epeetacnetal oiae aaeeie frame- parameterized motivated conceptually a present We Abstract utmHprrp aeoisvaGeneralized via Categories Hypergraph Custom Poestere obn rpia agaefor language graphical a combine theories —Process .I I. NTRODUCTION eateto optrSine nvriyo Oxford of University Science, Computer of Department a ase n arzoGenovese Fabrizio and Marsden Dan Relations ructure, l the rly etween order- These aking sby es man- new hey our out dy- for ar er le w e r - . l , httectgr frltosoe eua aeoyi a is convex a relation regular Concretely, binary ordinary [10]. a an is category over relation closed relations compact of the that eapidt h niiulelements. individual the that to operations imp scaling applied to and be intended addition not independent is are notation there This algebra. the of elements h aeoyo ovxagba srglradw a omits form can we denoted and relations, regular of is category algebras The convex category. of a itself the is category, regular any fact hr the where elbhvdoeainfrfrigcne itrsof as mixtures mixture convex convex a forming with such equipped denote for we sets operation Informally, are elements. behaved algebras, alge- well convex These as a . to distribution referred finite bras, the of algebras Moore dog. models a we be being also dogs, of should concept describe between” the “in points of points two any expect if spac would a then have animals, we if representing Informally, mixtures. forming under closed omn ovxmxue,i h es htipiain ft of implications hold that form sense following the in mixtures, convex forming hc ileti ml eor ebgnb nrdcn the introducing approach, by quantale. new begin We a a detour. of require small notion a will spac entail we metric will Therefore, of which regular. categories no not natural is example various are previous the the closed as in compact applicable We used a elephant. a into construction We metrics an The model. nearly introduce setting. than our to is rat within how metrics a wolf consider capture nearness now being to a to of like reasoning, closer therefore notions would is of about squirrel models a think dog, for to distance natural and is It for basis cognition. mathematical [11]. the cognition as of model used compositional was a model This hoped. type of A in h iebr-or aeoyo n oa on monad any of category Eilenberg-Moore The nagbacmdlo ovxt sgvnb h Eilenberg- the by given is convexity of model algebraic An sorscn xml,w eunt h ahmtc of mathematics the to return we example, second our As state ConvexRel R ( a 1 fa object an of I b , p 1 → i ) r oiierassmigt n,adthe and one, to summing reals positive are ∧ A ... r h ovxsbes sw a have may we as subsets, convex the are where ∧ R A ( a namnia aeoyi a is category monoidal a in n I b , X stemnia nt h states The unit. monoidal the is i n ) ConvexRel p ⇒ i x i R ( X R i hc scoe under closed is which p i a ti elknown well is It . i , X i p i b i ) rin or , x refore i can are (1) he es ly e t Definition 1 (Quantale). A quantale is a join complete monads, in the sense of formal [13]. These partial order Q with a monoid structure (⊗, k) satisfying the identify important “structured objects” within our categories following distributivity axioms, for all a,b ∈ Q and A, B ⊆ Q as follows. • The internal monads of Rel(B) are endo-relations such a ⊗ B = {a ⊗ b | b ∈ B} that h_ i _ A ⊗ b = {a ⊗ b | a ∈ A} R(a,a) and R(a,b) ∧ R(b,c) ⇒ R(a,c) A quantale is saidh_ toi be commutative_ if its monoid structure That is, they are preorders. is commutative. • The internal monads of Rel(I) are endo-relations where Example 2. Every locale [12] is a commutative quantale, and R(a,a)=1 and R(a,b) ∧ R(b,c) ≤ R(a,c) in particular any complete chain is a commutative quantale with We can see these as a fuzzy generalization of the notion of a preorder. A = sup A • The key example is the internal monads of Rel(C). These are endo-relations satisfying a1 _⊗ a2 = min(a,b) k = ⊤ R(a,a)=0 and R(a,b)+ R(b,c) ≥ R(a,c)

• The Boolean quantale B is given by the chain {0, 1} If we read the relation R as a distance function, we see with its usual ordering that they are generalized metric spaces [14]. • The interval quantale I is given by the chain [0, 1] ⊆ R • The internal monads of Rel(F) are endo-relations satis- with its usual ordering fying • The quantale F is given by the chain of extended [0, ∞] R(a,a)=0 and max(R(a,b), R(b,c)) ≥ R(a,c) positive reals with the reverse ordering An important example of a commutative quantale that does Again, if we regard R as a distance function, these can not correspond to a locale is the Lawvere quantale C with be seen to be generalized ultrametric spaces. underlying set the extended positive reals with reverse order So in particular, Rel(C) gives us a partial order enriched and algebraic structure dagger compact in which the internal monads are generalized metric spaces. Such categories of relations A = inf A have been proposed as a unifying categorical setting for

a1 _⊗ a2 = a1 + a2 investigating various topological notions, see [15], [16]. Multi- k =0 valued relations have also been investigated for compositional models of natural language [17]. A between two sets A and B can be To recap, we have constructed two compact closed cate- described by its characteristic function gories using differing techniques that can be found in the literature: A × B → 2 • By exploiting relations respecting algebraic structure, where 2 is the two element set of Boolean truth values. We standard monad and regular category theory provided can generalize the notion of binary relation by allowing the us with a category where the states are exactly convex truth values to be taken in a suitable choice of quantale Q, as subsets. a function of the form • Generalizing the notion of relations in a different direc- tion, we produced a category where the internal monads A × B → Q are generalized metric spaces. We can see this as a potentially infinite matrix of truth values. So, using rather ad-hoc methods, we have solved two mod- These binary relations form a category Rel(Q), with identities elling problems using generalizations of binary relations. This and composition given by suitable generalizations of their prompts several questions: matrix theoretic analogues. If the quantale of truth values is • How do these constructions relate to each other? In commutative, Rel(Q) is in fact dagger compact closed. So particular, can we simultaneously work with convexity we have found another dagger compact closed category, but and metrics in an appropriate setting? what has this got to do with metrics? In order to establish • Can they be seen as instances of a general construction? the required connection, we note that we can order relations • Does the notion of binary relation permit further axes pointwise in the quantale order, as follows: of variation, producing additional examples of compact closed categories? R ⊆ R′ iff ∀a, b.R(a,b) ≤ R′(a,b) • As the parameters of our constructions vary, can the re- This order structure makes Rel(Q) into a poset-enriched sym- sulting categories be related? Formally, this is a question metric . This means we can consider internal of functoriality in a suitable sense. These questions provide the starting point for our investiga- category. This style is most effective when the application un- tions. We also observe that the categories we identified in our der consideration has well understood calculational properties. examples are both in fact instances of Fong and Kissinger’s Our approach instead emphasizes the direct construction of hypergraph categories [18]. These are a particularly well models which can then be investigated for their suitability to behaved class of dagger compact closed categories, and this a given application. will be our technical setting for the remainder of the paper. The beautiful work on decorated cospans and correlations We summarize our contributions as follows of [25], [18], motivated by the program of network theory • We provide parameterized constructions of hypergraph initiated in [26], is of most direct relevance to our approach. categories of generalized relations and spans in theo- In a precise sense, the decorated corelation construction is rems III.7, IV.3 and IV.8. completely generic, every hypergraph category is produced by • We introduce analogues of the notion of converse of that construction. Our emphasis is different, we do not aim a generalized relation, and taking the graph of an un- for maximum generality. Instead, our aim is conceptually mo- derlying morphism. Many further aspects are shown to tivated parameterization. By providing four clearly motivated commute with this important structure. features that can be adjusted to application needs, we aim for • In section V the resulting categories are shown to be a practical construction with which investigators using process appropriately order enriched. theories can construct new models with desirable features. • In section VI we show that generalized spans can be II. MATHEMATICAL BACKGROUND functorially mapped to generalized relations in a manner We will be interested in particular types of symmetric respecting all the important structure. monoidal categories, and will make use of their corresponding • In section VII we show that homomorphisms of truth graphical languages [27]. Technical background on monoidal values functorially induce between models, pre- categories and general categorical notions can be found in [28]. serving all the important structure. We will also refer to and their internal languages • In section VIII we show that our constructions are functo- in places, standard references are [29], [30], [31], [32]. The rial in the choices of algebraic structure. We also describe paper has been written with the intention that it should be how the algebraic and truth value structures interact, readable without any detailed knowledge of theory. For providing connections with notions resource sensitivity such readers, definitions should be read as if they pertain to in the sense of linear logic. ordinary sets, functions and predicate logic. In this section we • In section IX we show that our constructions are also briefly describe some standard mathematical background and functorial in the choice of ambient topos, with the quan- conventions. tale structure being transferred along a logical . • In theorem X.1 we show that the functors induced by Definition 1 (Compact Closed Category). An object A in a changes of parameters commute with each other. symmetric monoidal category is said to have dual A∗ if there ∗ ∗ • Our methods give explicit concrete descriptions of the exist unit η : I → A ⊗ A and counit ǫ : A ⊗ A → I mathematical objects of interest, suitable for use in ap- . These morphisms are depicted in the graphical plications. calculus using special notation as • We provide many examples illustrating the flexibility of A∗ A our techniques, particularly to the construction of new ∗ and existing models of natural language processing and A A cognition applications. They are required to satisfy the following snake equations. ∗ ∗ Related Work A A A A Categories of relations have been studied in the form of allegories [19]. This work is somewhat removed from our = = approach, the heavy use of the modular law does not directly yield the graphical phenomena of interest. Of more direct rele- A A A∗ A∗ vance is the concept of cartesian of [20]. Although A compact closed category is a symmetric monoidal cat- graphical notation is not used directly in this work, these egory in which every object has a dual. A compact closed categories can be seen as close relatives of the hypergraph category A, equipped with an identity on objects involu- categories resulting from our constructions. The emphasis in tion (−)† : Aop → A coherently with the symmetric monoidal the study of cartesian was characterization rather compact closed structure, is referred to as a dagger compact than construction of models. closed category [33]. The older term strongly compact closed A somewhat syntactic approach to constructing categories category is also occasionally used. with graphical calculi is the use of PROPs [21], [22]. They have recently been used to construct various categorical mod- Example 2. The canonical example of a dagger compact els relating to control theory [3], [23], [24]. These methods closed category of relevance to the current work is the cat- begin with syntax and equations, and freely derive a resulting egory Rel of sets and binary relations between them. The symmetric monoidal structure is given by cartesian products Example 4. The category Rel is also an example of a of sets, and the dagger by the usual converse of relations. hypergraph category. The cocommutative comonoid is given Objects are self-dual, with the unit on a set A given by the by the relations relation ǫ = {(a, ∗) | a ∈ A} δ = {(a, (a,a)) | a ∈ A} η = {(∗, (a,a)) | a ∈ A} The monoid is the relational converse of the comonoid struc- and the counit is its converse. ture. The induced dagger compact closed structure is exactly Definition 3 (Hypergraph Category). A hypergraph category that of example 2. is a symmetric monoidal category such that every object A Our interest in hypergraph categories is that they are a carries a commutative monoid structure particularly pleasant form of dagger compact closed category, as established by the following well known observation. (η : I → A, µ : A ⊗ A → A) Proposition 5. Every hypergraph category is a dagger com- and a cocommutative comonoid structure pact closed category, with the cup and cap given by (ǫ : A → I,δ : A → A ⊗ A) A A A A ǫ δ We depict these morphisms graphically as follows: = = µ A A A A η A A A A δ ǫ The dagger of a morphism f : A → B is given by its µ η transpose A A A A A The choice of monoid structure on each object is required to f satisfy the following coherence condition with respect to the monoidal structure. B AB A B A ⊗ B A ⊗ B As a final technical point, we will be working with various δ categories with finite products. Throughout, we will implicitly = δ δ assume a choice of terminal object and binary products has been given. To reduce clutter, we therefore resist repeating this A B A ⊗ B assumption in the statements of our subsequent theorems. Here, we overload the use of the symbol δ to avoid cluttering III. RELATIONS our diagrams with indices or subscripts. We will exploit similar overloading of names in many places in what follows. The The aim in this section is to broadly generalize the notion of monoid structure is also required to satisfy the dual coherence binary relation between sets, in order to support our motivating condition. The multiplication and comultiplication must also examples, and to provide scope for many other variations. We satisfy the Frobenius axiom observed, for sets A and B, and quantale Q, that we can consider a function A×B → Q as a relation, with truth values A A A A A A taken in the quantale. For such generalized relations, we define δ the R : A → B and S : B → C by µ δ = = δ µ analogy with the usual composition of relations µ (S ◦ R)(a,c)= R(a,b) ⊗ S(b,c) A A A A A A _b With this notion of composition, the following relation, with and the special axiom truth values in Q, serves as an identity on set A: A A 1A(a1,a2)= {k | a1 = a2} µ _ = We then observe that all of these definitions actually make δ sense in the internal language of an arbitrary elementary topos. This leads us to the following definition. A A Definition 1 (Q-relation). Let E be a topos, and (Q, ⊗, k, ) More briefly, a hypergraph category is a symmetric monoidal an internal quantale. A Q-relation between E objects A and B category with a chosen special commutative Frobenius algebra is a E-morphism of type W structure on every object, coherent with the tensor product. A × B → Q E-objects and Q-relations between them form a cate- we can form a binary relation using the graph of f gory Rel(Q), with identities and composition as described above. {(a,b) | f(a)= b} Definition 1 is a first step in the right direction, but in The next proposition establishes that we can take graphs of order to capture convexity, as discussed in the introduction, morphisms in our underlying category of algebras, in a manner we must find a way of incorporating algebraic structure. If we respecting all the relevant categorical structure. consider an algebraic signature (Σ, E) with set of operations Σ Proposition 5. [Graph] Let E be a topos, (Σ, E) a variety and equations E, the general form of equation (1), for n-ary in E, and (Q, ⊗, k, ) an internal commutative quantale. operation σ ∈ Σ, is There is an identity on objects strict symmetric monoidal functor W R(a1,b1) ∧ ... ∧ R(an,bn) ⇒ R(σ(a1, ..., an), σ(b1, ..., bn)) (−)◦ : Alg(Σ, E) → Rel(Σ,E)(Q) We will require throughout that all operation symbols have defined on morphism by finite arity, as is conventional in universal algebra. f : A → B

It is then natural to consider replacing the logical com- f◦(a,b)= {k | f(a)= b} ponents of this definition with the structure of our chosen quantale. This leads to the definition we require. The symmetric monoidal structure_ on Alg(Σ, E) is the finite product structure. Definition 2 (Algebraic Q-relation). Let E be a topos, and (Q, ⊗, k, ) an internal quantale. Let (Σ, E) be an alge- The graph functor allows us to structures from the braic variety in E. An algebraic Q-relation between (Σ, E)- underlying category of algebras. The following canonical algebras A andWB is a Q-relation between their underlying E- comonoids are of particular conceptual importance. objects such that for each σ ∈ Σ the following axiom holds Proposition 6. Let E be a category with finite products. Each object A carries a cocommutative comonoid structure via the R(a1,b1) ⊗ ...⊗R(an,bn) canonical morphisms ≤ R(σ(a1, ..., an), σ(b1, ..., bn)) (2) !: A → 1 and h1 , 1 i : A → A × A (Σ, E)-algebras and algebraic Q-relations form a cate- A A gory Rel(Σ,E)(Q), with identities and composition as for their These morphisms satisfy the coherence condition (3). underlying Q-relations. Finally, we are in a position to establish that our categories There is some subtlety to the interaction between truth of algebraic Q-relations are hypergraph categories. values and algebraic structure, we will return to this topic Theorem III.7. Let be a topos, a variety in , in section VIII. We now continue studying the categorical E (Σ, E) E and (Q, ⊗, k, ) an internal commutative quantale. The cat- structure of algebraic Q-relations. egory Rel(Σ,E)(Q) is a hypergraph category. The cocom- Proposition 3. Let E be a topos, (Σ, E) a variety in E, mutative comonoidW structure is given by the graphs of the and (Q, ⊗, k, ) an internal commutative quantale. The cat- canonical comonoids described in proposition 6, and the egory Rel(Σ,E)(Q) is a symmetric monoidal category. The monoid structure is given by their converses. symmetric monoidalW structure is inherited from the finite We quickly return to one of the examples discussed in the products in E. introduction. We can take the converse of an ordinary binary relation, Example 8. The convex algebras discussed in the introduction simply by reversing its arguments. The notion of converse can be presented by a family of binary operations for forming generalizes smoothly to algebraic Q-relations, in a manner pairwise convex combinations that respects all the relevant categorical structure. p Proposition 4. [Converse] Let E be a topos, (Σ, E) a variety + where p ∈ (0, 1) in E, and (Q, ⊗, k, ) an internal commutative quantale. satisfying suitable equations. Writing Convex for this signa- There is an identity on objects strict symmetric monoidal ture, we can construct ConvexRel as Rel , where W Convex(2) 2 functor is the two element set. ◦ op (−) : Rel(Σ,E)(Q) → Rel(Σ,E)(Q) IV. SPANS given by reversing arguments: Generalizing the truth values, algebraic structure and am- bient category has provided three degrees of freedom for ◦ R (b,a)= R(a,b) describing custom hypergraph categories. Currently we can For ordinary sets and functions, given a function vary the underlying topos, quantale and choice of algebraic structure. We now investigate a fourth, final direction of f : A → B variation. If we consider a span of sets isomorphism classes of Q-spans form a category Span(Q). If we write χk for the constant morphism X ! k f g χk = A −→ 1 −→ Q

then the identity at A is given by the Q-span (A, 1, 1,χk). A B These spans with configurable truth values provide another we can consider an element x ∈ X as a proof witness construction of hypergraph categories. relating f(x) and g(x). Two spans are composed by forming Theorem IV.3. Let E be a finitely complete category, the pullback and (Q, ⊗, k) an internal commutative monoid. The cate- gory Span(Q) is a hypergraph category. X ×B Y p1 p2 We will not dwell on the explicit symmetric monoidal and hypergraph structures claimed in theorem IV.3. Once we have X Y incorporated algebraic structure into our span constructions, f g h k the required details can be found in proposition 5 and theo- rem IV.8. The key step now is incorporate algebraic structure into A B C the picture, paralleling the ideas of definition 2. In this case, Recall that pullbacks in Set are given explicitly by things are slightly more complicated as we have to explicitly administer the proof witnesses in the spans. We also must X ×B Y = {(x, y) | g(x)= h(y)} introduce an ordering on our truth values in order to specify the necessary axiom. Therefore, a pair (x, y) relates a and c exactly if x relates a to some b and this b is related to c by y. So, at least for the Definition 4. Let E be a topos, (Σ, E) a variety in E, category Set, we can think of spans as proof relevant relations. and (Q, ⊗, k, ≤) an internal partially ordered commutative This is the intuition we now pursue, starting by adjusting the monoid. For (Σ, E)-algebras A and B, an algebraic Q-span notion of Q-relation in definition 1 to the setting of spans. is a quadruple (X,f,g,χ) which is a Q-span between the underlying E-objects satisfying the following axiom. Definition 1 ( -span). Let be a finitely complete category, Q E For every σ ∈ Σ if and (Q, ⊗, k) an internal monoid. A Q-span of type A → B is a quadruple (X,f,g,χ) where f(x1)= a1 ∧ g(x1)= b1 ∧ ... ∧ f(xn)= an ∧ g(xn)= bn • is a span in (X,f : X → A, g : X → B) E then there exists x such that • χ : X → Q is a E-morphism, referred to as the charac- teristic morphism. f(x)= σ(a1, ..., an) ∧ g(x)= σ(b1, ..., bn) Two Q-spans (X,f,g,χ), (Y,h,k,ξ) are composed by com- and posing their underlying spans by pullback, and taking the χ(x ) ⊗ ... ⊗ χ(x ) ≤ χ(x) resulting characteristic morphism to be 1 n (Σ, E)-algebras and algebraic Q-spans form a cate- hp1,p2i χ×ξ ⊗ X ×C Y −−−−→ X × Y −−−→ Q × Q −→ Q gory Span(Σ,E)(Q) with identities and composition given as for the underlying Q-spans. where p1 and p2 are the pullback projections. A morphism of Q-spans between two Q-spans of As with the algebraic Q-relations in section III, we obtain type A → B a symmetric monoidal category with analogues of relational converse and taking graphs. α : (X1,f1,g1,χ1) → (X2,f2,g2,χ2) Proposition 5. Let E be a topos, (Σ, E) a variety in E, is a E-morphism α : X1 → X2 such that and (Q, ⊗, k, ≤) an internal partially ordered commutative monoid. The category Span(Σ,E)(Q) is a symmetric monoidal f1 = f2 ◦ α g1 = g2 ◦ α χ1 = χ2 ◦ α category. The symmetric monoidal structure is inherited from the finite product structure in E. Remark 2. When discussing Q-spans in the remainder of this paper, we actually intend isomorphism classes of spans with Proposition 6. [Converse] Let E be a topos, (Σ, E) a variety respect to the homomorphisms of definition 1. This convention in E, and (Q, ⊗, k, ≤) an internal partially ordered commu- is common when considering categories of ordinary spans, tative monoid. There is an identity on objects strict symmetric where composition of spans via pullback is only defined monoidal functor up to isomorphism. All definitions and calculations using ◦ op representatives will respect this isomorphism structure. These (−) : Span(Σ,E)(Q) → Span(Σ,E)(Q) given by reversing the legs of the underlying span: Definition 1. Let E be a topos and (Q, ⊗, k, ) an internal quantale. We define a partial order on Q-relations as follows (X,f,g,χ)◦ = (X,g,f,χ) W R ⊆ R′ iff ∀a, b.R(a,b) ≤ R′(a,b) Proposition 7. [Graph] Let E be a topos, and (Q, ⊗, k, ≤) Algebraic Q-relations are ordered similarly, by comparing an internal partially ordered commutative monoid. There is an their underlying Q-relations. identity on objects strict symmetric monoidal functor

(−)◦ : Alg(Σ, E) → Span(Σ,E)(Q) Theorem V.2. Let E be a topos, (Σ, E) a variety in E, and (Q, ⊗, k, ) an internal commutative quantale. The cate- with the action on morphism f : A → B given by gory Rel(Σ,E)(Q) is a partially ordered symmetric monoidal category. W f◦ = (A, 1,f,χk) As before, we can exploit the graph construction and the Q-spans can also be ordered, in a manner analogous to canonical comonoids of proposition 6 to establish the existence that for relations, but explicitly taking into account the proof of a hypergraph structure. witnesses. Theorem IV.8. Let E be a topos, (Σ, E) a variety in E, Definition 3. For topos E and internal partially ordered and (Q, ⊗, k, ≤) an internal partially ordered commutative monoid Q, we define a preorder on Q-spans as follow. monoid. The category Span (Q) is a hypergraph cate- (Σ,E) (X1,f1,g1,χ1) ⊆ (X2,f2,g2,χ2) gory. The cocommutative comonoid structure is given by the graphs of the canonical comonoids described in proposition 6, if there is a E-monomorphism m : X1 → X2 such that and the monoid structure is given by their converses. f1 = f2 ◦ m and g1 = g2 ◦ m and ∀x.χ1(x) ≤ χ2(m(x)) This construction presents new modelling possibilities, that can be combined with other features, opening fresh directions Algebraic Q-spans are ordered similarly, by comparing their for investigation that may not have been immediately apparent. underlying Q-spans.

Theorem V.4. Let E be a topos, (Σ, E) a variety in E, Example 9. The span construction allows us to construct and (Q, ⊗, k, ≤) an internal partially ordered commutative variations on the models we are already interested in. For monoid. The category Span(Σ,E)(Q) is a preordered sym- example, we can now construct a proof relevant version of metric monoidal category. the model 8. From a practical perspective, this presents the possibility of models in which we can describe the interaction The orders are respected by the important converse opera- of cognitive phenomena, and provide quantitative evidence for tion any relationships that we conclude hold. Proposition 5. Let E be a topos, and (Σ, E) a variety in E. Example 10. Instead of using Set as our base topos in our Converses respect order structure, in that models, we could consider using a presheaf topos [Cop, Set] • If (Q, ⊗, k, ) is an internal quantale, the converse for a small category C. This allows us to construct models functor of proposition 4 is a partially ordered functor W using “sets varying with context”, incorporating all the features • If (Q, ⊗, k, ≤) is an internal partially order monoid, the discussed in the previous examples. In linguistic or cognitive converse functor of proposition 6 is a preordered functor examples, contexts could describe time, the agents involved or The order enrichment of Q-relations and Q-spans is crucial the broader setting in which meaning should be interpreted. for us to be able to consider the internal monads central to the These context sensitive models present a lot of new expressive second example of the introduction. potential, and will be investigated in future work. Example 6. The model incorporating metric spaces as internal V. ORDER ENRICHMENT monads, as discussed in the introduction, can be constructed In order to meaningfully discuss internal monads, we re- with base topos Set, the empty algebraic signature and using quire some 2-categorical structure on our relational construc- the Lawvere quantale C as the choice of truth values. tions. Specifically, we introduce an appropriate ordering on Now we have both algebraic and order structure available to our morphisms. Order enrichment is also important from a us within the same construction, we can consider combining practical perspective when modelling real world applications. the features we are interested in, by making appropriate For example, in natural language applications, we are often choices for the parameters used in the construction. interested in phenomena such as ambiguity [34], [35] and lexical entailment [36], and these are best studied from an Example 7. We now see that we can combine both the order theoretic perspective. convex and metric features in a single model. With underlying Generalizing the situation for ordinary set theoretic binary topos Set, we take our algebraic structure as in example 8 relations, we introduce an ordering on Q-relations. and our quantale C as in example 6. In this case we find the internal monads are distance measures d : A × A → [0, ∞] quantales. There is an identity on objects, strict symmetric satisfying monoidal Pos-functor ∗ d(a,a)=0 h : Rel(Σ,E)(Q1) → Rel(Σ,E)(Q2) d(a,b)+ d(b,c) ≥ d(a,c) The assignment h 7→ h∗ is functorial. d(a ,a )+ d(b ,b ) ≥ d(a +p b ,a +p b ) 1 2 1 2 1 1 2 2 As with the extensional collapse functor of section VI, the These are generalized metric spaces that respect convex struc- induced functor respects graphs and converses, and therefore ture. The usual metric on Rn is an example of such a metric. preserves the hypergraph structure on the nose. ∗ VI. FROM SPANS TO RELATIONS Proposition 2. With the same assumptions, the functor h of theorem VII.1 commutes with graphs and converses. That is, We now begin our study of the relationship between the the following diagrams commute: different parameter choices we can take. We start with the simplest case, the binary choice between proof relevance and h∗ Rel (Q1) Rel (Q2) provability. (Σ,E) (Σ,E) The next theorem shows that the orders on relations and spans are compatible, in the sense that we can collapse spans (−)◦ (−)◦ to relations using the join of a quantale to choose optimal truth Alg(Σ, E) values, and this mapping is functorial and respects the order (h∗)op structure. op op Rel(Σ,E)(Q1) Rel(Σ,E)(Q2)

Theorem VI.1. Let E be a topos, (Σ, E) a variety in E ◦ ◦ and (Q, ⊗, k, ) an internal commutative quantale. There is (−) (−) an identity on objects, strict symmetric monoidal Preord- W Rel(Σ,E)(Q1) Rel(Σ,E)(Q2) functor h∗ Span Rel V : (Σ,E)(Q) → (Σ,E)(Q) In the case of the span constructions, morphisms of partially As we would expect, this extensional collapse of proof wit- ordered monoids are the appropriate notion of homomorphism nesses interacts well with the graph and converse operations, to consider. and therefore preserves our chosen hypergraph structure on Theorem VII.3. Let E be a topos, (Σ, E) a variety in E, the nose. and h : Q1 → Q2 a morphism of internal partially ordered Proposition 2. With the same assumptions, the functor V of commutative monoids. There is an identity on objects, strict theorem VI.1 commutes with graphs and converses. That is, symmetric monoidal Preord-functor the following diagrams commute: ∗ h : Span(Σ,E)(Q1) → Span(Σ,E)(Q2) V The assignment h 7→ h∗ is functorial. Span(Σ,E)(Q) Rel(Σ,E)(Q) Again, the induced functor commutes with graphs and converses. (−)◦ (−)◦ Alg(Σ, E) Proposition 4. With the same assumptions, the functor h∗ of

op theorem VII.3 commutes with graphs and converses. That is, op V op Span(Σ,E)(Q) Rel(Σ,E)(Q) the following diagrams commute: ◦ ◦ h∗ (−) (−) Span(Σ,E)(Q1) Span(Σ,E)(Q2)

Span(Σ,E)(Q) Rel(Σ,E)(Q) V (−)◦ (−)◦ Alg(Σ, E) VII. CHANGING TRUTH VALUES We would expect that homomorphisms between our struc- ∗ op op (h ) op tures of truth values lead to functorial relationships between Span(Σ,E)(Q1) Span(Σ,E)(Q2) models. This all goes through very smoothly, as we now ◦ ◦ elaborate. (−) (−) Firstly, for algebraic Q-relations, it is natural to consider Span(Σ,E)(Q1) Span(Σ,E)(Q2) internal quantale homomorphisms. h∗ Theorem VII.1. Let E be a topos, (Σ, E) a variety in E, Example 5. For any commutative quantale Q there is a and h : Q1 → Q2 a morphism of internal commutative partially ordered monoid morphism 1 → Q, induced by the monoid unit. Here, 1 is the terminal quantale. Therefore there is valid is a strict symmetric monoidal functor • Cartesian if it is both affine and relevant We note the following important special case. Span(Σ,E)(1) → Span(Σ,E)(Q) This example motivates our use of partially ordered monoids, Example 4. A cartesian commutative quantale is a locale. rather than simply restricting to the quantales of interest in So in the case where our truth values have a genuine locale our primary applications, as the required morphism is not a structure, everything becomes very well behaved. quantale morphism. Definition 5. Let E be a topos, and (Q, ⊗, k, ) an internal Example 6. There is a quantale morphism B → C from commutative quantale. We say that a Q-relation is the Boolean to the Lawvere quantale. The induced functor W • Linear to emphasize that no additional axioms are as- identifies the ordinary binary relations as living within the sumed to hold category Rel(C) that we introduced to capture metric spaces • Affine if the axiom as internal monads.

∀a1,a2,b1,b2.R(a1,b1) ⊗ R(a2,b2) ≤ R(a1,b1) VIII. ALGEBRAIC STRUCTURE We now investigate the interaction between truth values is valid and algebraic structure. Again, this will lead to functorial • Relevant if the axiom relationships between models, but the subject is more delicate ∀a, b.R(a,b) ≤ R(a,b) ⊗ R(a,b) than in the previous sections. The essential detail is that inequation (2) is only required to hold for the operations in is valid our signature. It does not directly say anything about derived • Cartesian if it is both affine and relevant terms and operations. We will require several definitions in lin aff rel We write Rel(Σ,E)(Q), Rel(Σ,E)(Q), Rel(Σ,E)(Q) and order to make the situation precise. cart Rel(Σ,E)(Q) for the corresponding of algebraic Definition 1. Let (Σ, E) be an algebraic signature. We say Q-relations. that a term τ over a finite set of variables is Our terminology is derived from that sometimes used for • Linear if it uses each variable is used exactly once variants of linear logic. If we view truth values as resources, • Affine if it uses each variable at most once the question is when can these resources be “copied” or • Relevant if it uses each variable at least once “deleted”. We have adopted a naming convention that is lin • Cartesian to emphasize that its use of variables is unre- slightly redundant, for example Rel(Σ,E)(Q) is the same thing stricted as Rel(Σ,E)(Q). We permit this redundancy in order to allow We use the same terminology for the derived operation asso- uniform statements of the subsequent theorems. We begin ciated to τ. with important closure properties of our various classes of morphisms. Definition 2 (Interpretation). An interpretation of signa- ture (Σ1, E1) in signature (Σ2, E2) is a mapping assigning Proposition 6. The subcategories of linear (affine, relevant, each σ ∈ Σ1 to a derived term of (Σ2, E2) of the same cartesian) algebraic Q-relations are closed under tensors, arity, such that the equations E1 can be proved in equational converses and the functors induced by quantale homomor- logic from E2. We say that an interpretation is linear (affine, phisms. Also, the algebraic Q-relations in the image of the relevant, cartesian) if all the derived terms used in the interpre- graph functor are all cartesian. tation are linear (affine, relevant, cartesian). We write Siglin aff rel cart A straightforward corollary of the closure properties of (Sig , Sig , Sig ) for the corresponding categories with proposition 6 is objects signatures and morphisms linear (affine, relevant, carte- sian) interpretations. Theorem VIII.7. For a topos E, variety (Σ, E) in E and internal commutative quantale (Q, ⊗, k, ), the Definition 3. Let E be a topos, and (Q, ⊗, k, ≤) an internal lin aff rel categories Rel(Σ,E)(Q), Rel(Σ,E)(Q), Rel(Σ,E)(Q) and partially ordered monoid. We say that Q is cart W Rel(Σ,E)(Q) are sub-hypergraph categories of Rel(Σ,E)(Q). • Linear to emphasize that no additional axioms are as- sumed to hold Our restricted classes of relations respect the corresponding • Affine if the axiom classes of terms.

∀p, q.p ⊗ q ≤ p Proposition 8. Let E be a topos, (Σ, E) a variety in E and (Q, ⊗, k, ) an internal commutative quantale. For linear is valid (affine, relevant, cartesian) algebraic Q-relation R : A → B W • Relevant if the axiom the axiom

∀q.q ≤ q ⊗ q R(a1,b1) ⊗ ... ⊗ R(an,bn) ≤ R(τ(a1, ..., an), τ(b1, ..., bn)) holds for every linear (affine, relevant, cartesian) n-ary de- • Affine if the axiom ∀x1, x2.χ(x1) ⊗ χ(x2) ≤ χ(x1) is rived operation τ. valid • Relevant if the axiom ∀x.χ(x) ≤ χ(x) ⊗ χ(x) is valid The next proposition is straightforward, it establishes that • Cartesian if it is both affine and relevant once our truth values are sufficiently nice, all our relations lin aff rel inherit the same property. We write Span(Σ,E)(Q), Span(Σ,E)(Q), Span(Σ,E)(Q) and cart Span(Σ,E)(Q) for the corresponding subcategories of alge- Proposition 9. Let E be a topos, (Σ, E) a variety braic Q-spans. in E and (Q, ⊗, k, ) an internal commutative quantale. If Q is linear (affine, relevant, cartesian), every morphism Spans with sufficient structure respect the corresponding W types of terms. in Rel(Σ,E)(Q) is linear (affine, relevant, cartesian). In particular, if our quantale is in fact a locale, proposition 9 Proposition 13. Let E be a topos, (Σ, E) a variety in E, tells us that everything becomes as straightforward as we might and (Q, ⊗, k, ≤) an internal partially ordered commutative hope. monoid. For (Σ, E)-algebras A and B, and linear (affine, Finally, we can establish a contravariant functorial relation- relevant, cartesian) algebraic Q-span (X,f,g,χ) and n-ary ship between interpretations and functors between models. linear (affine, relevant, cartesian) term τ if Theorem VIII.10. Let E be a topos and (Q, ⊗, k, ) an f(x1)= a1 ∧ g(x1)= b1 ∧ ... ∧ f(xn)= an ∧ g(xn)= bn internal commutative quantale. Let i : (Σ1, E1) → (Σ2, E2) then there exists x such that be a linear interpretation of signatures. There is aW strict monoidal functor f(x)= τ(a1, ..., an) ∧ g(x)= τ(b1, ..., bn) ∗ lin lin and i : Rel(Σ2,E2)(Q) → Rel(Σ1,E1)(Q) χ(x1) ⊗ ... ⊗ χ(xn) ≤ χ(x) The assignment i 7→ i∗ extends to a contravariant functor. Similar results hold for affine, relevant and cartesian inter- Again, we have good closure of our various classes of pretations and relations. morphisms. As usual, the induced functors respect the essential graph Proposition 14. The subcategories of linear (affine, relevant, and converse structure. cartesian) algebraic Q-spans are closed under tensors, con- verses and the functors induced by quantale homomorphisms. Proposition 11. With the same assumptions, the induced Also, the algebraic Q-spans in the image of the graph functor functor i∗ of theorem VIII.10 commutes with graphs and are all cartesian. converses. That is, the following diagrams commute: As with relations, the closure properties of proposition 14 i∗ yield a straightforward corollary about our subcategories of Rellin (Q) Rellin (Q) (Σ2,E2) (Σ1,E1) algebraic Q-spans.

(−)◦ (−)◦ Theorem VIII.15. For a topos E, variety (Σ, E) in E and internal partially ordered monoid (Q, ⊗, k, ≤), the Alg(Σ , E ) Alg(Σ , E ) 2 2 ∗ 1 1 lin aff rel i categories Span(Σ,E)(Q), Span(Σ,E)(Q), Span(Σ,E)(Q) cart and Span(Σ,E)(Q) are sub-hypergraph categories (i∗)op of Span (Q). lin op lin op (Σ,E) Rel(Σ2,E2)(Q) Rel(Σ1,E1)(Q) As with relations, algebraic Q-spans inherit good properties (−)◦ (−)◦ from their underlying quantale. lin lin Proposition 16. Let E be a topos, (Σ, E) a variety in E Rel(Σ2,E2)(Q) Rel(Σ1,E1)(Q) i∗ and (Q, ⊗, k, ≤) an internal partially ordered monoid. The bottom functor in these diagrams is the obvious induced If Q is linear (affine, relevant, cartesian), every morphism functor between categories of algebras. Similar diagrams in Span(Σ,E)(Q) is linear (affine, relevant, cartesian). commute for affine, relevant and cartesian interpretations and Again, we can now establish a contravariant functorial rela- relations. tionship between interpretations and functors between models. We now introduce similar definitions for the setting of spans, in order to proceed with a similar analysis. Theorem VIII.17. Let E be a topos and (Q, ⊗, k, ≤) an inter- Definition 12. Let E be a topos, and (Q, ⊗, k, ≤) an internal nal partially ordered commutative monoid. Let i : (Σ1, E1) → partially ordered monoid. We say that a Q-span (X,f,g,χ) is (Σ2, E2) be a linear interpretation of signatures. There is a strict monoidal functor • Linear to emphasize that no additional axioms are as- ∗ lin lin sumed to hold i : Span(Σ2,E2)(Q) → Span(Σ1,E1)(Q) The assignment i 7→ i∗ extends to a contravariant functor. Example 22. An affine join semilattice is a set with an as- Similar results hold for affine, relevant and cartesian inter- sociative, commutative, idempotent binary operation. From an pretations and spans. information theoretic perspective, we think of convex algebras as describing probabilistic ambiguity. Affine join semilattices The induced functors respect the usual essential structure. can then be thought of as modelling unquantified ambiguity. If Proposition 18. For the same assumptions, the induced func- we denote the signature for affine join semilattices as Affine tor i∗ of theorem VIII.17 commutes with graphs and converses. there is an interpretation in Siglin of type Convex → Affine That is, the following diagrams commute inducing a functor

∗ lin i lin RelAffine(Q) → RelConvex(Q) Span(Σ2,E2)(Q) Span(Σ1,E1)(Q) relating these two different models of epistemic phenomena. (−) (−) ◦ ◦ This exhibits another interesting of ConvexRel.

Alg(Σ2, E2) Alg(Σ1, E1) i∗ IX. CHANGING TOPOS We now explore the last axis of variation, the topos struc- (i∗)op lin op lin op ture. We would expect that, if E and F are elementary toposes, Span(Σ2,E2)(Q) Span(Σ1,E1)(Q) given a suitable functor L : E →F it would be possible to (−)◦ (−)◦ lift it to a functor between their respective relation and span constructions. Since the definitions of these categories make Spanlin (Q) Spanlin (Q) (Σ2,E2) i∗ (Σ1,E1) wide use of the internal language, it should not be surprising that by “suitable” we actually mean that L behaves well with The bottom functor in these diagrams is the obvious induced respect to the logical properties of E, F. functor between categories of algebras. Similar diagrams commute for affine, relevant and cartesian interpretations and Definition 1. Given toposes E, F, a functor L : E →F is relations. called logical if: • L preserves products The extensional collapse functor of section VI also respects • L preserves exponentials our different classes of spans and relations. • L preserves the subobject classifier. Proposition 19. Let E be a topos, (Σ, E) a variety in E Logical functors are the right functors to consider, since and (Q, ⊗, k, ) an internal commutative quantale. The func- they preserve the validity of internal formulas: If |= φ in E, tor of theorem VI.1 maps linear (affine, relevant, cartesian) then |= φ in F for every formula φ written in the language of algebraic Q-spansW to linear (affine, relevant, cartesian) alge- first order intuitionistic logic. braic Q-relations. To make the following results more readable, we will E We briefly discuss some examples. have to slightly refine our notation, writing Rel(Σ,E)(Q) E Example 20. Let (∅, ∅) denote the signature with no opera- and SpanΣ,E (Q) to explicitly indicate that the constructions tions or equations. For any signature (Σ, E) there is a trivial are performed on topos E. If L : E →F is a logical functor linear interpretation (∅, ∅) → (Σ, E). We therefore have, for and Q is an internal quantale in E, then the fact that L every choice of internal quantale Q, strict symmetric monoidal preserves models of first order intuitionistic theories implies that LQ is an internal quantale in F. It makes sense, then, forgetful functors E F to consider how Rel(Σ,E)(Q) and Rel(Σ,E)(LQ) are related. Rel(Σ,E)(Q) → Rel(∅,∅)(Q) The main result of the section is the following: Span Span (Σ,E)(Q) → (∅,∅)(Q) Theorem IX.2. Let E, F be toposes, and L : E →F be a Example 21. We can present real vector spaces by a signature logical functor. Let (Q, ⊗, k, ) be an internal commutative with a constant element representing the origin, and a family of quantale in E and (Σ, E) be a signature. There is a symmetric W binary mixing operations, indexed by the scalars involved, sat- monoidal functor isfying suitable equations. We denote this signature as Linear. L∗ : RelE (Q) → RelF (LQ) There is an interpretation in Siglin of type Convex → Linear. (Σ,E) (Σ,E) For any commutative quantale Q, this interpretation induces a The assignment L 7→ L∗ is functorial. functor As in the previous cases, graph and converse functors are RelLinear(Q) → RelConvex(Q) preserved. So, as we would expect, we can find the vector spaces in the convex algebras, in a manner respecting all the relevant Proposition 3. With the same assumptions, the induced func- ∗ categorical structure. tor L of theorem IX.2 commutes with graphs and converses. That is, the following diagrams commute: of sieves (subfunctors of the homset functor) and these sieves L∗ RelE (Q) RelF (LQ) trivialize when the only arrows at our disposal are isos. This (Σ,E) (Σ,E) in turn trivializes the structure of truth values in the presheaf itself, that ends up to be defined pointwise from Set. (−)◦ (−)◦ Theorems IX.2, IX.4 then ensure that ¯ can be lifted to E F F Alg (Σ, E) Alg (Σ, E) C D L the relational and span structures built on Set and Set , respectively. (L∗)op E op F op Rel(Σ,E)(Q) Rel(Σ,E)(LQ) Example 7. If E is a topos, and f : I → J is a morphism of E, then pulling back along f induces a logical functor F : ◦ ◦ (−) (−) E/J → E/I. Theorem IX.2 guarantees the existence of a functor F ∗ : RelE/J (Q) → RelE/I (FQ). In particular, RelE (Q) RelF (LQ) (Σ,E) (Σ,E) (Σ,E) ∗ (Σ,E) ∗ E L this means that there is always a functor F : Rel(Σ,E)(Q) → E/I As with relations, morphisms between toposes extend func- Rel(Σ,E)(FQ), where E/I is any slice topos of E. torially to morphisms between spans. Theorem IX.4. Let E, F be toposes, and L : E →F be a logical functor. Let (Q, ⊗, k, ≤) be an internal partially X.INDEPENDENCE OF THE AXES OF VARIATION ordered commutative monoid in E and (Σ, E) be a signature. There is a symmetric monoidal functor

∗ E F L : Span(Σ,E)(Q) → Span(Σ,E)(LQ) Finally, we establish that our various induced functors between models are independent, in that they all commute with The assignment L 7→ L∗ is functorial. each other.Unfortunately, the commutativity of the functors in- The essential structure is again respected by the induced duced by interpretations between algebras, order structure and functors. quantale morphisms with L∗ will hold only up to isomorphism. This depends intrinsicly on the definition of logical functor, Proposition 5. With the same assumptions, the induced func- that is, in turn, defined to preserve validity of formulas in the tor L∗ of theorem IX.4 commutes with graphs and converses. internal language only up to natural isomorphism. That is, the following diagrams commute:

∗ Theorem X.1. Let E be a topos, h : Q1 → Q2 a morphism E L F Span(Σ,E)(Q) Span(Σ,E)(LQ) of internal commutative quantales, i : (Σ1, E1) → (Σ2, E2) a linear interpretation and L : E→F a logical functor. For the (−)◦ (−)◦ induced functors of theorems VII.1, VII.3, VIII.10, VIII.17, IX.2 and IX.4, the following diagram commutes (be aware that in AlgE (Σ, E) AlgF (Σ, E) L the hypercube below commutative squares involving L∗ only commute up to isomorphism. Other squares commute up to (L∗)op E op F op equality): Span(Σ,E)(Q) Span(Σ,E)(LQ)

(−)◦ (−)◦ • • SpanE (Q) SpanF (LQ) (Σ,E) L∗ (Σ,E) L∗ Example 6. Given any category C we can form a cor- L∗ responding presheaf category, having representable functors • • from C to Set as objects and natural transformations between them as morphisms. Presheaves constitute one of the most • • ∗ ∗ important examples of toposes, and it makes sense to ask how L L Theorems IX.2, IX.4 behave in these circumstances. • • In general, given arbitrary categories C, D it is difficult • • L∗ to say when a functor F : C → D lifts to a logical • • functor between the corresponding presheaves. Nevertheless, • • the following result holds: If C, D are groupoids (categories in which every arrow is an isomorphism), then any functor ∗ ∗ F : C → D lifts to a logical functor F¯ : C → D. This is L L because truth values in presheaf toposes are defined in terms • • Where the inner cube is analogues of the notions of taking the converse of a relations, and taking the graph of a map to construct a new relation. i∗ Spanlin,E Spanlin,E (Σ2,E2)(Q1) (Σ1,E1)(Q1) Interestingly, our framework points to new models in which features can be combined. This was a key objective of this di- ∗ ∗ h h rection of research. For example the model incorporating both ∗ lin,E i lin,E convexity and metrics of example 7, the proof relevant models Span (Q2) Span (Q2) (Σ2,E2) (Σ1,E1) of cognition of example 9 and the possibility of incorporating contexts as discussed in example 10. The application of these constructions to models of cognition and natural language will i∗ Rellin,E Rellin,E be explored in forthcoming work. (Σ2,E2)(Q1) (Σ1,E1)(Q1) In order to gain a strict composition operation, in section IV ∗ ∗ h h we used isomorphism classes of spans, and then introduced ∗ lin,E i lin,E an analogue of the usual order structure for relations in Rel (Q2) Rel (Q2) (Σ2,E2) (Σ1,E1) section V. If we use spans, rather than their equivalence and the outer cube is classes, they should form a symmetric monoidal bicategory, sacrificing strict composition for a richer 2-cell structure. i∗ Spanlin,F Spanlin,F This is of practical interest as internal monads have been (Σ2,E2)(LQ1) (Σ1,E1)(LQ1) important in our examples. The internal monads in categories (Lh)∗ (Lh)∗ of spans correspond to internal categories [37], which would i∗ open up further interesting possibilities. Some related work Spanlin,F (LQ ) Spanlin,F (LQ ) (Σ2,E2) 2 (Σ1,E1) 2 on bicategorical aspects of the decorated cospan construction appears in [38]. In fact, the resulting categories should be an appropriate bicategorical generalization of a hypergraph i∗ Rellin,F Rellin,F category. Such bicategorical aspects are left to later work. (Σ2,E2)(LQ1) (Σ1,E1)(LQ1) (Lh)∗ (Lh)∗ Acknowledgments i∗ Rellin,F Rellin,F The authors would like to thank Bob Coecke, Ignacio (Σ2,E2)(LQ2) (Σ1,E1)(LQ2) Funke, Kohei Kishida and Martha Lewis for feedback and In both cases the vertical arrows are the functors of theo- discussions. This work was partially funded by the AFSOR rem VI.1. Similar diagrams commute for affine, relevant and grant “Algorithmic and Logical Aspects when Composing cartesian interpretations, relations and spans. Meanings” and the FQXi grant “Categorical Compositional Physics”. XI. CONCLUSION We have developed a parameterized scheme for constructing REFERENCES hypergraph categories, by generalizing the notion of binary [1] B. Coecke and A. Kissinger, Picturing Quantum Processes. A First relation along four axes of variation: Course in Quantum Theory and Diagrammatic Reasoning. Cambridge • The ambient mathematical background via the choice of University Press, 2017, forthcoming. underlying category [2] B. Coecke, M. Sadrzadeh, and S. Clark, “Mathematical foundations for distributed compositional model of meaning. Lambek festschrift,” • The truth values via a choice of internal quantale Linguistic Analysis, vol. 36, pp. 345–384, 2010. • The choice of algebraic structure [3] F. Bonchi, P. Sobocinski, and F. Zanasi, “Full abstraction for signal flow ACM SIGPLAN Notices • The choice between proof relevance and provability graphs,” , vol. 50, no. 1, pp. 515–526, 2015. [4] J. C. Baez and J. Erbele, “Categories in control,” Theory and Applica- This construction provides a conceptually motivated approach tions of Categories, vol. 30, no. 24, pp. 836–881, 2015. for producing models of process theories when investigating [5] J. C. Baez, B. Fong, and B. S. Pollard, “A compositional framework for Markov processes,” Journal of Mathematical Physics, vol. 57, no. 3, p. new applications. Many existing examples in the literature are 033301, 2016. covered by our approach, including examples used for linguis- [6] J. C. Baez and B. Fong, “A compositional framework for passive linear tics, cognition, linear dynamical systems and non-deterministic networks,” arXiv preprint arXiv:1504.05625, 2015. computation. [7] P. Sobocinski, “Graphical linear algebra,” mathematical blog. [Online]. Available: https://graphicallinearalgebra.net/ We showed that the resulting categories are preorder en- [8] P. G¨ardenfors, Conceptual spaces: The geometry of thought. MIT press, riched, providing more flexible modelling possibilities. It 2004. was also established that varying each of the parameters is [9] ——, The geometry of meaning: Semantics based on conceptual spaces. MIT Press, 2014. functorial, preserving all the important hypergraph and order [10] C. Heunen and S. Tull, “Categories of relations as models of quantum structure. In the case of the algebraic structure, this functo- theory,” in Proceedings of the 12th International Workshop on Quantum riality exhibited an interesting relationship between algebra Physics and Logic, Oxford, U.K., July 15-17, 2015, ser. Electronic Proceedings in Theoretical Computer Science, C. Heunen, P. Selinger, and resource sensitivity in the sense of linear logic. Our and J. Vicary, Eds., vol. 195. Open Publishing Association, 2015, pp. constructions were also shown to have well behaved functorial 247–261. [11] J. Bolt, B. Coecke, F. Genovese, M. Lewis, D. Marsden, and R. Piedeleu, Proposition 3. Let E be a topos, (Σ, E) a variety in E, “Interacting conceptual spaces,” in Proceedings of the 2016 Workshop and (Q, ⊗, k, ) an internal commutative quantale. The cat- on Semantic Spaces at the Intersection of NLP, Physics and Cognitive Science, SLPCS@QPL 2016, Glasgow, Scotland, 11th June 2016., ser. egory Rel(Σ,E)(Q) is a symmetric monoidal category. The EPTCS, D. Kartsaklis, M. Lewis, and L. Rimell, Eds., vol. 221, 2016, symmetric monoidalW structure is inherited from the finite pp. 11–19. [Online]. Available: http://dx.doi.org/10.4204/EPTCS.221.2 products in E. [12] P. T. Johnstone, Stone spaces. Cambridge University Press, 1986, vol. 3. [13] R. Street, “The formal theory of monads,” Journal of Pure and Applied Proof. The proof that Rel(Σ,E)(Q) is a category follows from Algebra, vol. 2, no. 2, pp. 149–168, 1972. [14] F. W. Lawvere, “Metric spaces, generalized logic, and closed categories,” the unit and associative properties of quantale multiplication. Rendiconti del seminario mat´ematico e fisico di Milano, vol. 43, no. 1, We must first confirm that identities respect algebraic structure. pp. 135–166, 1973. For σ ∈ Σ [15] M. M. Clementino and W. Tholen, “Metric, topology and multicategory– Journal of Pure and Applied Algebra ′ ′ a common approach,” , vol. 179, 1A(a1,a1) ⊗ ... ⊗ 1A(an,an)= no. 1, pp. 13–47, 2003. ′ ′ [16] D. Hofmann, G. J. Seal, and W. Tholen, Monoidal Topology: A Categor- = {k | a1 = a1} ⊗ ... ⊗ {k | an = an} ical Approach to Order, Metric, and Topology. Cambridge University Press, 2014, vol. 153. h ′ i h ′ i = _{k | (a1 = a ) ∧ ... ∧ (an_= a )} [17] M. Dostal and M. Sadrzadeh, “Many valued generalised quantifiers for 1 n natural language in the DisCoCat model,” Queen Mary University of ≤ _{k | σ(a , ..., a )= σ(a′ , ..., a′ )} London, Tech. Rep., 2016. 1 n 1 n [18] B. Fong, “The algebra of open and interconnected systems,” Ph.D. ′ ′ =1_A(σ(a1, ..., an), σ(a , ..., a )) dissertation, University of Oxford, 2016. 1 n [19] P. J. Freyd and A. Scedrov, Categories, allegories. Elsevier, 1990, We do the same for compositions vol. 39. [20] A. Carboni and R. F. C. Walters, “Cartesian bicategories I,” Journal of (S ◦ R)(a1,c1) ⊗ ... ⊗ (S ◦ R)(an,cn)= pure and applied algebra, vol. 49, no. 1-2, pp. 11–32, 1987. [21] S. MacLane, “Categorical algebra,” Bulletin of the American Mathemat- ical Society, vol. 71, no. 1, pp. 40–106, 1965. = R(a1,b1) ⊗ S(b1,c1) ⊗ ... [22] S. Lack, “Composing PROPs,” Theory and Applications of Categories, " b1 # vol. 13, no. 9, pp. 147–163, 2004. _ [23] F. Zanasi, “Interacting Hopf algebras: The theory of linear systems,” Ph.D. dissertation, l’Ecole´ Normale Sup´erieure de Lyon, 2015. ···⊗ R(an,bn) ⊗ S(bn,cn) " # [24] J. Erbele, “Categories in control: Applied PROPs,” Ph.D. dissertation, _bn University of California Riverside, 2016. [25] B. Fong, “Decorated cospans,” Theory and Applications of Categories, = R(a1,b1) ⊗ S(b1,c1) ⊗···⊗ R(an,bn) ⊗ S(bn,cn) vol. 30, no. 33, pp. 1096–1120, 2015. b1,...,b_ n [26] J. C. Baez, “Network theory (part 1),” mathematical blog entry. [Online]. Available: = [R(a1,b1) ⊗ ... ⊗ R(an,bn)] ⊗ https://johncarlosbaez.wordpress.com/2011/03/04/network-theory-part-1/ b1,...,b_ n [27] P. Selinger, “A survey of graphical languages for monoidal categories,” ⊗ [S(b ,c ) ⊗ ... ⊗ S(b ,c )] in New structures for physics. Springer, 2010, pp. 289–355. 1 1 n n [28] S. MacLane, Categories for the Working Mathematician, ser. Graduate ≤ R(σ(a1, ..., an), σ(b1, ..., bn))⊗ Texts in Mathematics. Springer, 1998, vol. 5. [29] S. MacLane and I. Moerdijk, Sheaves in geometry and logic: A first b1,...,b_ n introduction to topos theory. Springer, 1992. ⊗ S(σ(b1, ..., bn), σ(c1, ..., cn)) [30] P. Johnstone, Sketches of an Elephant: A Topos Theory Compendium. Oxford University Press, 2002, vol. I. ≤ R(σ(a1, ..., an),b) ⊗ S(b, σ(c1, ..., cn)) [31] ——, Sketches of an Elephant: A Topos Theory Compendium. Oxford b University Press, 2002, vol. II. _ [32] F. Borceux, Handbook of Categorical Algebra. Cambridge University = (S ◦ R)(σ(a1, ..., an), σ(c1, ..., cn)) Press, 1994, vol. 3. [33] S. Abramsky and B. Coecke, “A categorical semantics of quantum Now we check explicitly the associativity of composition. For protocols,” in Logic in Computer Science, 2004. Proceedings of the 19th relations R,S,T , we have Annual IEEE Symposium on. IEEE, 2004, pp. 415–425. [34] R. Piedeleu, “Ambiguity in categorical models of meaning,” Master’s (T ◦ (S ◦ R))(a, d)= (S ◦ R)(a,c) ⊗ T (c, d) thesis, University of Oxford, 2014. c [35] R. Piedeleu, D. Kartsaklis, B. Coecke, and M. Sadrzadeh, “Open _ system categorical quantum semantics in natural language processing,” = R(a,b) ⊗ S(b,c) ⊗ T (c, d) arXiv:1502.00831, 2015. c " # [36] D. Bankova, “Comparing meaning in language and cognition - p- _ _d hypononymy, concept combination, asymmetric similarity,” Master’s thesis, University of Oxford, 2015. = R(a,b) ⊗ S(b,c) ⊗ T (c, d) [37] R. Rosebrugh and R. J. Wood, “Distributive laws and factorization,” d " c # Journal of Pure and Applied Algebra, vol. 175, no. 1, pp. 327–353, _ _ 2002. = R(a,b) ⊗ (T ◦ S)(b, d) arXiv preprint [38] K. Courser, “A bicategory of decorated cospans,” d arXiv:1605.08100, 2016. _ = ((T ◦ S) ◦ R)(a, d) APPENDIX We also check the right identity law. ′ ′ We outline proofs of the key results. (R ◦ 1A)(a,b)= 1A(a,a ) ⊗ R(a ,b) ′ _a = {k | a = a′} ⊗ R(a′,b) We consider E as a symmetric monoidal category with respect a′ to our choice of binary products and terminal object. We then _ h_ i = {k ⊗ R(a′,b) | a = a′} take the graphs (see proposition 5 for the definition) of the a′ corresponding left and right unitors, associator and symmetry _ _ as the corresponding structure in Rel . = {R(a,b)} (Q) We must confirm that these coherence morphisms are nat- a′ _ ural in their parameters. The proofs are all similar, we check = R(a,b) the associator explicitly The left identity law proof is similar. Now we prove this category is symmetric monoidal. We R⊗(S ⊗ T ) ◦ αA,B,C = define the monoidal unit to be the terminal algebra. On objects, = αA,B,C(((a,b),c), (x, (y,z)))⊗ the tensor takes products of algebras. We define the action on x,y,z _ morphisms pointwise as ⊗ [R ⊗ (S ⊗ T )] ((x, (y,z)), (a′, (b′,c′))) (R ⊗ R′)(a,a′,b,b′)= R(a,b) ⊗ R′(a′,b′) = {k | (a = x) ∧ (b = y) ∧ (c = z)} ⊗ We first confirm ′ respects the algebraic structure. x,y,z h i R ⊗ R _ _ ′ ′ ′ For σ ∈ Σ with arity n ⊗ R(x, a ) ⊗ S(y,b ) ⊗ T (z,c ) ′ ′ ′ ′ ′ ′ ′ ′ ′ = {R(x, a ) ⊗ S(y,b ) ⊗ T (z,c ) | (R ⊗ R )(a1,a1,b1,b1) ⊗ ... ⊗ (R ⊗ R )(an,an,bn,bn)= x,y,z = R(a ,b ) ⊗ R′(a′ ,b′ ) ⊗ ... ⊗ R(a ,b ) ⊗ R′(a′ ,b′ ) _ _ 1 1 1 1 n n n n | (a = x) ∧ (b = y) ∧ (c = z)} = [R(a ,b ) ⊗ ... ⊗ R(a ,b )] ⊗ [R′(a′ ,b′ ) ⊗ ... ⊗ R′(a′ ,b′ )] 1 1 n n 1 1 n n = R(a,a′) ⊗ S(b,b′) ⊗ T (c,c′) ≤ R(σ(a1, ..., an), σ(b1, ..., bn))⊗ ′ ′ ′ ′ ′ = {R(a, x) ⊗ S(b,y) ⊗ T (c,z) | ⊗ R (σ(a1, ..., an), σ(b1, ..., bn)) x,y,z ′ ′ ′ _ _ ′ ′ ′ = (R ⊗ R )((σ(a1, ..., an), σ(a1, ..., an)), | (x = a ) ∧ (y = b ) ∧ (z = c )} ′ ′ (σ(b1, ..., bn), σ(b1, ..., bn))) = R(a, x) ⊗ S(b,y) ⊗ T (c,z)⊗ ′ ′ ′ ′ ′ = (R ⊗ R )(σ((a ,a ), ..., (a ,a )), σ((b ,b ), ..., (b ,b ))) x,y,z 1 1 n n 1 1 n n _ Then, we show that the tensor is bifunctorial. Identities are ⊗ {k | x = a′ ∧ y = b′ ∧ z = c′} preserved: = [(R ⊗h_S) ⊗ T ] (((a,b),c), ((x, y),z))⊗i ′ ′ ′ ′ (1A1 ⊗ 1A2 )(a1,a2,a1,a2)=1A1 (a1,a1) ⊗ 1A2 (a2,a2) x,y,z _ ′ ′ ′ ′ ′ ⊗ αA′,B′,C′ (((x, y),z), (a , (b ,c ))) = {k | a1 = a1}⊗ {k | a2 = a2} ′ ′ ′ ′ ′ = αA ,B ,C ◦ (R ⊗ S) ⊗ T = _{k | (a1 = a1) ∧_(a2 = a2)} These are isomorphisms by functoriality of graphs (see propo- _ ′ ′ = {k | (a1,a2) = (a1,a2)} sition 5 for the proof). Their inverses are given by their ′ ′ =1_A1⊗A2 (a1,a2,a1,a2) converses, as established in proposition 4. Moreover, taking graphs commutes with our choice of For composition, products in E in the sense that [(S1 ⊗ S2) ◦ (R1 ⊗ R2)] (a1,a2,c1,c2)= (f × g)◦ = f◦ ⊗ g◦ = (R1 ⊗ R2)(a1,a2,b1,b2) ⊗ (S1 ⊗ S2)(b1,b2,c1,c2) To check this we reason as follows: b_1,b2 ′ ′ ′ ′ = R1(a1,b1) ⊗ R2(a2,b2) ⊗ S1(b1,c1) ⊗ S2(b2,c2) (f × g)◦((a,a ), (b,b )) = {k | (b,b ) = (f × g)(a,a )} b1,b2 _ = {k | (b = f(a_)) ∧ (b′ = g(a′))} = R1(a1,b1) ⊗ S1(b1,c1) ⊗ R2(a2,b2) ⊗ S2(b2,c2) = _ {k | b = f(a)} ⊗ {k | b′ = g(a′)} b_1,b2 ′ ′ = hf_◦(a,b) ⊗ g◦(a ,b )i h_ i = R1(a1,b1) ⊗ S1(b1,c1) ⊗ " b1 # This guarantees that the triangle and pentagon equations hold _ as the same equations hold for the cartesian monoidal structure ⊗ R2(a2,b2) ⊗ S2(b2,c2) in E. The coherence conditions for the symmetry follow " # _b2 similarly. = (S ◦ R )(a ,c ) ⊗ (S ◦ R )(a ,c ) 1 1 1 1 2 2 2 2 Proposition 5. [Graph] Let E be a topos, (Σ, E) a variety = [(S1 ◦ R1) ⊗ (S2 ◦ R2)] (a1,a2,c1,c2) in E, and (Q, ⊗, k, ) an internal commutative quantale. W There is an identity on objects strict symmetric monoidal Proof. We first check that taking the converse gives a well functor defined relation,. For σ ∈ Σ of arity n, we reason

(−)◦ : Alg(Σ, E) → Rel(Σ,E)(Q) ◦ ◦ R (a1,b1) ⊗ ...⊗R (an,bn)= R(b1,a1) ⊗ ... ⊗ R(bn,an) defined on morphism by f : A → B ≤ R(σ(b1, ..., bn), σ(a1, ..., an)) ◦ f◦(a,b)= {k | f(a)= b} = R (σ(a1, ..., an), σ(b1, ..., bn)) The symmetric monoidal structure_ on Alg(Σ, E) is the finite Next, we must confirm identities are preserved. product structure. ◦ 1A(a1,a2)=1A(a2,a1) Proof. First of all we have to check that the resulting relation = {k | a2 = a1} respects the algebraic structure. For σ ∈ Σ =1_A(a1,a2) f (a ,b )⊗···⊗ f (a ,b )= ◦ 1 1 ◦ n n We confirm also functoriality with respect to composition = {k | f(a1)= b1} ⊗ ... (R ◦ S)◦(a,c) = (R ◦ S)(c,a) h_ i ···⊗ {k | f(an)= bn} = S(c,b) ⊗ R(b,a) h i b = {k | f(_a1)= b1 ∧···∧ f(an)= bn} _ = S◦(b,c) ⊗ R◦(a,b) ≤ _{k | σ(f(a1),...,f(an)) = σ(b1,...,bn)} _b ◦ ◦ = _{k | f(σ(a1,...,an)) = σ(b1,...,bn) = R (a,b) ⊗ S (b,c) b = f_◦(σ(a1,...,an), σ(b1,...,bn)) _ = (S◦ ◦ R◦)(a,c) The we confirm this is functorial with respect to identities Finally, we must check that the converse distributes over

1A◦(a1,a2)= {k | 1A(a1)= a2} tensors ◦ ◦ ′ ′ ◦ ◦ ′ ′ = _{k | a1 = a2} (R ⊗ S )(b,b ,a,a )= R (b,a) ⊗ S (b ,a ) = R(a,b) ⊗ S(a′,b′) =1_A(a1,a2) = (R ⊗ S)(a,a′,b,b′) For functoriality with respect to composition, = (R ⊗ S)◦(b,b′,a,a′) (g ◦ f )(a,c)= f (a,b) ⊗ g (b,c) ◦ ◦ ◦ ◦ We moreover prove a fact used in the proof of proposition 3, b _ that is, if f is an isomorphism in E then = {k | f(a)= b} ⊗ {k | g(b)= c} −1 ◦ (f )◦ = (f◦) _b h_ i h_ i = {k | g(f(a)) = c} This is a simple check: −1 = (_g ◦ f)◦(a,c) (f )◦(b,a)= Finally we prove the preservation of the monoidal structure: = {k | f −1b = a} ′ ′ ′ ′ (f × g)◦((a,a ), (b,b )) = {k | (b,b ) = (f × g)(a,a )} = _{k | f(a)= b} ′ ′ = {k | b = f(a_) ∧ b = g(a )} = f_◦(a,b) = (f )◦(b,a) = _ {k | b = f(a)} ⊗ {k | b′ = g(a′)} ◦ Theorem III.7. Let E be a topos, (Σ, E) a variety in E, = hf_(a,b) ⊗ g (a′,b′i) h_ i ◦ ◦ and (Q, ⊗, k, ) an internal commutative quantale. The cat- = (f ⊗ g )((a,a′), (b,b′)) ◦ ◦ egory Rel(Σ,E)(Q) is a hypergraph category. The cocom- W Proposition 4. [Converse] Let E be a topos, (Σ, E) a variety mutative comonoid structure is given by the graphs of the in E, and (Q, ⊗, k, ) an internal commutative quantale. canonical comonoids described in proposition 6, and the There is an identity on objects strict symmetric monoidal monoid structure is given by their converses. W functor Proof. For every object A of E, call ǫA, δA the comultiplica- ◦ op tion and counit of proposition 6, and ηA, µA their respective (−) : Rel(Σ,E)(Q) → Rel(Σ,E)(Q) converses. The morphisms ǫA, δA have in the internal logic given by reversing arguments: the explicit form ◦ R (b,a)= R(a,b) ǫA(a, x)= k ′ δA(a1, (a2,a3)) = {k | a1 = a2 = a3} g2 ◦ p2 ◦ ϕ ×B ψ = g2 ◦ ψ ◦ p2 = g1 ◦ p2

Checking that ηA, µA form_ a monoid is straightforward, Also from the definition of converse. With respect to this ′ ′ monoid/comonoid pair, we first confirm the special axiom ⊗◦ (χ2 × ξ2)◦hp1,p2i◦ ϕ ×B ψ = = ⊗◦ (χ2 × ξ2) ◦ (ϕ × ψ) ◦ hp1,p2i (µA ◦ δA)(a1,a2)= = ⊗◦ (χ2 ◦ ϕ, ξ2 ◦ ψ) ◦ hp1,p2i ′ ′ = δA(a1, (a,a )) ⊗ µA((a,a ),a2) = ⊗◦ (χ × ψ ) ◦ hp ,p i ′ 1 1 1 2 (a,a_ ) ′ ′ and as ϕ ×B ψ is also an isomorphism we can use lemma 1 = {k | a1 = a = a } ⊗ {k | a = a = a2} ′ to complete this part of the proof. (a,a_ ) h_ i h_ i Next we confirm the left identity axiom. Firstly we note ′ = {k | a1 = a = a = a2} ′ (B, 1B, 1B,χk) ◦ (X,f,g,χ)= (a,a_ ) _ = (X ×B B,f ◦ p1,p2, ⊗◦ (χ × χk) ◦ hp1,p2i) = {k | a1 = a2} We claim is a -span morphism to the span . =1_A(a1,a2) p1 Q (X,f,g,χ) The conditions for this being a span morphism are Finally, we check the Frobenius axiom, omitting some stages where the expressions get very long f ◦ p1 = f ◦ p1 and g ◦ p1 = p2

((1A ⊗ δA) ◦ (µA ⊗ 1A))(a1,a2,a3,a4)= and the second condition is obvious from the pullback square. Finally, we must confirm this also commutes with the charac- = 1 (a , x) ⊗ δ (a , (y,z)) ⊗ µ ((x, y),a ) ⊗ 1 (z,a ) A 1 A 2 A 3 A 4 teristic functions. x,y,z _ = {k | x = y = z = a1 = a2 = a3 = a4} ⊗◦ (χ × χk) ◦ hp1,p2i = p1 ◦ (χ×!) ◦ hp1,p2i x,y,z = p ◦ hχ ◦ p , ! ◦ p i _ 1 1 2 = {k | a1 = a2 = a3 = a4} = χ ◦ p1

=_ ((δA ⊗ 1A) ◦ (1A ⊗ µA))(a1,a2,a3,a4) We must prove that p1 is an isomorphism, the required inverse Lemma 1. Let E be a finitely complete category, and (Q, ⊗, k) being given by the of pullbacks as an internal monoid. If h1,gi h : (X1,f1,g1,χ1) → (X2,f2,g2,χ2) Checking that this is an isomorphism follows from the uni- is a Q-span morphism with an inverse in E, then it is an versal property of pullbacks. We can then use lemma 1 to isomorphism. complete this part of the proof. The right identity axiom follows similarly. −1 Proof. We aim to show that h is the required inverse as We must then confirm associativity. We consider the com- a Q-span morphism. We calculate posites −1 −1 f1 ◦ h = f2 ◦ h ◦ h = f2 L = ((Z,l,m,ζ) ◦ (Y,h,k,ξ)) ◦ (X,f,g,χ) −1 −1 g1 ◦ h = g2 ◦ h ◦ h = g2 R = (Z,l,m,ζ) ◦ ((Y,h,k,ξ) ◦ (X,f,g,χ)) χ ◦ h−1 = χ ◦ h ◦ h−1 = χ 1 2 2 Via the usual proof for categories of ordinary spans Lemma 2. Let E be a finitely complete category and (Q, ⊗, k) an internal monoid. Span(Q) is a category. ι := hp1◦p1, hp2◦p1,p2ii : (X×B Y )×C Z → X×B (Y ×C Z) Proof. We first confirm that composition is independent of is an isomorphism of spans. It remains to show that this representatives. Consider span isomorphisms commutes with the characteristic morphisms. This is a horrible exercise in tracking various canonical morphisms, and is most ϕ : (X1,f1,g1,χ1) → (X2,f2,g2,χ2) easily handled using the graphical calculus for a cartesian ψ : (Y1,h1, k1, ξ1) → (Y2,h2, k2, ξ2) monoidal category. Details are omitted to avoid a long type- setting exercise for the diagrams. We must show an isomorphism between the Q-spans Lemma 3. Let E be a finitely complete category and (Q, ⊗, k) (X × Y ,f ◦ p , k ◦ p , ⊗◦ (χ × ξ ) ◦ hp ,p i) 1 B 1 1 1 1 2 1 1 1 2 an internal monoid. There is an identity on objects contravari- ′ ′ ′ ′ (X2 ×B y2,f2 ◦ p1, k2 ◦ p2, ⊗◦ (χ2 × ξ2) ◦ hp1,p2i) ant involution (dagger functor) given by We calculate, using properties of pullbacks (−)◦ : Span(Q)op → Span(Q) ′ f2 ◦ p1 ◦ ϕ ×B ψ = f2 ◦ ϕ ◦ p1 = f1 ◦ p1 (X,f,g,χ) 7→ (X,g,f,χ) Proof. This involution clearly preserves identities. We aim to Lemma 5. Let E be a finitely complete category and (Q, ⊗, k) show an internal commutative monoid. There is a bifunctor ◦ ◦ ◦ (X,f,g,χ) ◦ (Y,h,k,ξ) = ((Y,h,k,ξ) ◦ (X,f,g,χ)) ⊗ : Span(Q) × Span(Q) → Span(Q) These two spans are given by A ⊗ B = A × B (X ,f ,g ,χ ) ⊗ (X ,f ,g ,χ )= (X,f,g,χ)◦ ◦ (Y,h,k,ξ)◦ = 1 1 1 1 2 2 2 2 = (X1 × X2,f1 × f2,g1 × g2, ⊗◦ (χ1 × χ2)) = (Y ×B X, k ◦ p1,f ◦ p2, ⊗ ◦ hξ ◦ p1,χ ◦ p2i) ((Y,h,k,ξ)◦(X,f,g,χ))◦ = Furthermore, this bifunctor commutes with graphs in the sense that the following diagram commutes = (X ×B Y, k ◦ p2,f ◦ p1, ⊗ ◦ hχ ◦ p1, ξ ◦ p2i) ⊗ The morphisms Span(Q) × Span(Q) Span(Q)

hp2,p1i : X ×B Y → Y ×B X

hp2,p1i : Y ×B X → X ×B Y (−)◦ × (−)◦ (−)◦ witness an isomorphism in E. We first confirm that this gives E×E E a span isomorphism. This follows trivially from elementary × properties of pullbacks. Finally, we must prove that this commutes with characteristic morphisms. This makes essential Proof. We first show that this respects equivalence classes of use of commutativity of ⊗ spans. Assume we have span isomorphisms ϕ : (X ,f ,g ,χ ) → (X′ ,f ′ ,g′ ,χ′ ) ⊗ ◦ hξ ◦ p1,χ ◦ p2i ◦ hp2,p1i = ⊗ ◦ hξ ◦ p2,χ ◦ p1i 1 1 1 1 1 1 1 1 ψ : (X ,f ,g ,χ ) → (X′ ,f ′ ,g′ ,χ′ ) = ⊗ ◦ hχ ◦ p1, ξ ◦ p2i 2 2 2 2 2 2 2 2 Lemma 4. Let E be a finitely complete category, and (Q, ⊗, k) The product ϕ × ψ gives an isomorphism of ordinary spans an internal monoid. There is an identity on objects covariant ϕ × ψ : (X1,f1,g1,χ1) ⊗ (X2,f2,g2,χ2) → graph functor ′ ′ ′ ′ ′ ′ ′ ′ → (X1,f1,g1,χ1) ⊗ (X2,f2,g2,χ2) (−)◦ : E → Span(Q) It then remains to check this commutes with characteristic f : A → B 7→ (A, 1 ,f,χ ) A k morphisms. We calculate There is also an identity on objects contravariant cograph ⊗◦ (χ′ × χ′ ) ◦ (ϕ × ψ)= ⊗◦ [(χ′ ◦ ϕ) × (χ′ ◦ ψ)] functor 1 2 1 2 = ⊗◦ (χ1 × χ2) op ◦(−): E → Span(Q) That this is bifunctorial as an operation on the underlying f : A → B 7→ (B, f, 1 ,χ ) B k spans is well known. It remains to check the behaviour with If Q is a commutative monoid then respect to characteristic morphisms. For identity Q-spans, the ◦ resulting characteristic function is ◦(−) = (−) ◦ (−)◦ Proof. This construction is well known for ordinary spans. ⊗◦ (χk × χk)= χk For Q-spans, we must confirm the interaction with character- For composition, we note there is an isomorphism of spans istic morphisms behaves appropriately. Firstly we note that hhp1 ◦ p1,p1 ◦ p2i, hp2 ◦ p1,p2 ◦ p2ii : (1A)◦ = (A, 1A, 1A,χk) ′ ′ ′ ′ : (X ×B Y ) × (X ×B′ Y ) → (X × X ) ×B×B′ (Y × Y ) and so identities are preserved. For composition, we have an We must show this commutes with the corresponding char- ordinary span isomorphism acteristic morphisms. The following unpleasant calculation h1A,fi : A → A ×B B establishes the required equality ′ ′ We must confirm this commutes with characteristic morphisms ⊗◦ (⊗×⊗) ◦ [((χ × ξ) ◦ hp1,p2i) × ((χ × ξ )hp1,p2i)] = ′ ′ ⊗◦ (χk × χk) ◦ hp1,p2i ◦ h1A,fi = ⊗ ◦ hχk,χk ◦ fi = ⊗◦ (⊗×⊗) ◦ [(χ × ξ) × (χ × ξ )] ◦

= ⊗ ◦ hχk,χki ◦ hhp1p1,p2p1i, hp1p2,p2p2ii ′ ′ = χk = ⊗◦ (⊗×⊗) ◦ [(χ × χ ) × (ξ × ξ )] ◦ ◦ hhp p ,p p i, hp p ,p p ii◦ The proof for the cograph construction is similar. In the case 1 1 1 2 2 1 2 2 of commutative Q, the relationship to the converse functor is ◦ hhp1p1,p2p1i, hp1p2,p2p2ii immediate from the definitions. = ⊗◦ (⊗×⊗) ◦ [(χ × χ′) × (ξ × ξ′)] ◦ ◦ hhp1p1,p1p2i, hp2p1,p2p2ii Applying proposition 6, it is sufficient to show the following are span isomorphisms Finally, we must confirm that this bifunctor commutes with graphs. On objects this is obvious, as all the functors involved λX : (1 × X, λA ◦ (11 × f), λB ◦ (11 × g), act as the identity on objects. On morphisms we reason , ⊗◦ (χk × χ)) → (X,f,g,χ)

(f1 × f2)◦ = (A1 × A2, 1A1×A2 ,f1 × f2, k) ρX : (X × 1,ρA ◦ (f × 11),ρB ◦ (g × 11),

= (A1, 1A1 ,f,k) ⊗ (A2, 1A2 ,f,k) , ⊗◦ (χ × χk)) → (X,f,g,χ)

= (f1)◦ ⊗ (f2)◦ αX,Y,Z : ((X1 × X2) × X3, αA1,A2,A3 ◦

Lemma 6. Let E be a finitely complete category and (Q, ⊗, k) ◦ ((f1 × f2) × f3), αB1,B2,B3 ◦ ((g1 × g2) × g3), ...) → an internal monoid. The following equation holds in Span(Q) → (X1 × (X2 × X3)) σ : (X × X , σ ◦ f × f , σ ◦ g × g , k◦ ◦ (X,f,g,χ) ◦ ◦h = (X,h ◦ f, k ◦ g,χ) X,Y 1 2 A1,A2 1 2 B1,B2 1 2 , ⊗◦ (χ × χ )) → Proof. We firstly consider the case of post composition with 1 2 the graph of a morphism in the underlying category → (X2 × X1,f2 × f2,g2 × g1, ⊗◦ (χ2 × χ1)) And this is now just a straightforward (but very unpleasant) k◦ ◦ (X,f,g,χ) check. This composite is given by the pullback span Lemma 7. Let E be a topos, (Q, ⊗, k, ≤) an internal partially (X ×B B,p1 ◦ f,p2 ◦ k, ⊗◦ (χ × χk) ◦ hp1,p2i) ordered commutative monoid, and (Σ, E) an algebraic variety.

We note that p1 ◦ h1X ,gi =1X and If (X1,f1,g1,χ1) is an algebraic Q-span, and (X2,f2,g2,χ2) is an isomorphic Q-span, then it is also an algebraic span. h1X ,gi◦ p1 = hp1,g ◦ p1i = hp1,p2i =1X×BB Proof. For the assumptions in the question, with ι denoting and so p1 and h1X ,gi witness an isomorphism in E. We next the assumed isomorphism and σ ∈ Σ, if confirm they give a span isomorphism. One of the conditions for p1 to be a span morphism is trivial, for the other f2(x1)= a1 ∧ g2(x1)= b1 ∧ ... ∧ f2(xn)= an ∧ g2(xn)= bn

k ◦ g ◦ p1 = k ◦ 1 ◦ p2 = k ◦ p2 then using our span isomorphism.

−1 −1 Finally, we must confirm that this commutes with the charac- f1(ι (x1)) = a1 ∧ g1(ι (x1)) = b1 ∧ ... teristic morphisms −1 −1 ···∧ f1(ι (xn)) = an ∧ g1(ι (xn)) = bn ⊗◦ (χ × χ ) ◦ hp ,p i = χ ◦ p ◦ hp ,p i = χ ◦ p k 1 2 1 1 2 1 By assumption that the first span is algebraic, there exists x Now we note that such that ◦ (X,f,g,χ) ◦ ◦h = (X,f,g,χ) ◦ h◦ f1(x)=σ(a1, ..., an) ∧ g1(x)= σ(b1, ..., bn)∧ ◦ ◦ −1 −1 = (h◦ ◦ (X,f,g,χ) ) ∧ χ1(ι (x1)) ⊗ ... ⊗ χ1(ι (xn)) ≤ χ1(x) = (h ◦ (X,g,f,χ))◦ ◦ Therefore, using our span isomorphism again = (X,g,h ◦ f,χ)◦ = (X,h ◦ f,g,χ) f2(ι(x)) = σ(a1, ..., an) ∧ g2(ι(x)) = σ(b1, ..., bn)∧ ∧ χ (x ) ⊗ ... ⊗ χ (x ) ≤ χ (ι(x)) Combining these two observations then completes the proof. 2 1 2 n 2 Lemma 8. Let E be a topos, (Q, ⊗, k, ≤) an internal partially Theorem IV.3. Let E be a finitely complete category, ordered commutative monoid, and (Σ, E) an algebraic variety. and (Q, ⊗, k) an internal commutative monoid. The cate- Span(Σ,E)(Q) is a category. gory Span(Q) is a hypergraph category. Proof. Throughout, we will perform checks for an arbi- Proof. We first confirm that Span(Q) carries a monoidal trary σ ∈ Σ. Firstly, we must confirm that the identity structure. We take as our monoidal unit the terminal object morphisms are algebraic. The required condition is immedi- in E and the tensor to be the bifunctor of proposition 5. ate, as A is closed under the algebraic operations, and the Next, we note that the underlying category is a symmetric characteristic morphism is constant in the internal language. monoidal category with respect to binary products. The graph Secondly, we must confirm that algebraic Q-spans are construction is surjective on objects, and commutes with the closed under composition. Assume tensor, therefore the graphs of the coherence morphisms in E f(p (z )) = a ∧ k(p (z )) = c ∧ ... lift to Span(Q). We must confirm that each of these remains 1 1 1 2 1 1 natural in Span(Q). ···∧ f(p1(zn)) = an ∧ k(p2(zn)) = cn ′ then there exist x1, ..., xn and y1, ..., yn such that As the spans are algebraic, there exist x and x such that ′ ′ ′ ′ ′ ′ f(x1)= a1 ∧ g(x1)= h(y1) ∧ k(y1)= c1 ∧ ... χ(x1)⊗...⊗χ(xn) ≤ χ(x), χ (x1)⊗...⊗χ (xn) ≤ χ (x ) ···∧ f(xn)= an ∧ g(xn)= h(yn) ∧ k(yn)= cn By monotonicity and commutativity of the tensor, we then therefore as the component spans are algebraic, there exist x have and y such that ′ ′ ′ ′ ′ ′ χ(x1) ⊗ χ (x1) ⊗ ... ⊗ χ(xn) ⊗ χ (xn) ≤ χ(x) ⊗ χ (x ) f(x)= σ(a , ..., a ) ∧ g(x)= h(y) ∧ k(y)= σ(c , ..., c ) 1 n 1 n As we said, functoriality, and that the required diagram com- and both mutes then follows from the proof of theorem IV.3 as tensors coincide on the underlying Q-spans. χ(x1) ⊗ ... ⊗ χ(xn) ≤ χ(x), ξ(y1) ⊗ ... ⊗ ξ(yn) ≤ ξ(y) Proposition 6. [Converse] Let E be a topos, (Σ, E) a Therefore we have (x, y) in the apex of the composite span, variety in E, and (Q, ⊗, k, ≤) an internal partially ordered with truth value χ(x) ⊗ ξ(y). By monotonicity of the tensor commutative monoid. There is an identity on objects strict χ(x1) ⊗ ... ⊗ χ(xn) ⊗ ξ(y1) ⊗ ... ⊗ ξ(yn) ≤ χ(x) ⊗ ξ(y) symmetric monoidal functor ◦ op Finally, as the tensor is commutative (−) : Span(Σ,E)(Q) → Span(Σ,E)(Q)

χ(x1) ⊗ ξ(y1) ⊗ ... ⊗ χ(xn) ⊗ ξ(yn) ≤ χ(x) ⊗ ξ(y) given by reversing the legs of the underlying span: That the composition is associative and the identities sat- (X,f,g,χ)◦ = (X,g,f,χ) isfy the required axioms follows immediately from the same properties for the underlying Q-spans as established in Proof. Converse is defined as in lemma 3, and the proof that lemma 2. this is algebraic is in proposition 5. That converse commutes with the tensor is trivial. Proposition 5. Let E be a topos, (Σ, E) a variety in E, and (Q, ⊗, k, ≤) an internal partially ordered commutative Proposition 7. [Graph] Let E be a topos, and (Q, ⊗, k, ≤) monoid. The category Span(Σ,E)(Q) is a symmetric monoidal an internal partially ordered commutative monoid. There is an category. The symmetric monoidal structure is inherited from identity on objects strict symmetric monoidal functor the finite product structure in E. (−)◦ : Alg(Σ, E) → Span(Σ,E)(Q) Proof. Throughout, we will perform checks for an arbi- with the action on morphism given by trary σ ∈ Σ. The proof is exactly as in lemma 5, we just need f : A → B to prove that the functors defined in lemmas 3, 4, 5 respect f◦ = (A, 1,f,χk) the algebraic condition. With regard to lemma 3, we just observe that the condition Proof. Graph is defined as in lemma 4, and the proof that this for a Q-span to be algebraic is symmetrical in its domain and is algebraic is in proposition 5. The proof that graphs commute codomain, and therefore preserved. with the tensor is as in lemma 5. With regard to lemma 4, as the characteristic morphism is Theorem IV.8. Let E be a topos, (Σ, E) a variety in E, constant, we must simply confirm the existence of witnesses and (Q, ⊗, k, ≤) an internal partially ordered commutative relating composite terms. If monoid. The category Span(Σ,E)(Q) is a hypergraph cate- f(a1)= b1 ∧ ... ∧ f(an)= bn gory. The cocommutative comonoid structure is given by the graphs of the canonical comonoids described in proposition 6, then as f is a homomorphism and the monoid structure is given by their converses. f(σ(a1, ..., an)) = σ(f(a1), ..., f(an)) = σ(b1, ..., bn) Proof. As in theorem IV.3, since all the tools used there With regard to lemma 5, for algebraic Q-spans (X,f,g,χ) preserve the algebraic structure. ′ ′ ′ ′ and (X ,f ,g ,χ ), if Theorem V.2. Let E be a topos, (Σ, E) a variety in E, ′ ′ ′ ′ ′ ′ and (Q, ⊗, k, ) an internal commutative quantale. The cate- (f × f )(x1, x1) = (a1,a1) ∧ ... ∧ (f × f )(xn, xn) = (an,an) gory Rel(Σ,E)(Q) is a partially ordered symmetric monoidal and category. W ′ ′ ′ ′ ′ ′ (g × g )(x1, x1) = (b1,b1) ∧ ... ∧ (g × g )(xn, xn) = (bn,bn) Proof. First of all we have to prove that our partial order is Then well defined. It is clearly reflexive. For transitivity, if ′ ′ ′ ′ ′ ′ ′ and ′ ′′ f(x1)= a1 ∧ f (x1)= a1 ∧ ... ∧ f(xn)= an ∧ f (xn)= an R ⊆ R R ⊆ R and then both ′ ′ ′ ′ ′ ′ ′ ′ ′′ g(x1)= b1 ∧ g (xn)= b1 ∧ ... ∧ g(xn)= bn ∧ g (xn)= bn ⊢ R(a,b) ≤ R (a,b) and ⊢ R (a,b) ≤ R (a,b) Therefore internally which is monic in E as monomorphisms are closed under composition. We then have ⊢ R(a,b) ≤ R′(a,b) ∧ R′(a,b) ≤ R′′(a,b) χ (x)= χ (i−1(x)) = ξ (m ◦ i−1(x)) = ξ (j ◦ m ◦ i−1(x)) and so by transitivity of the order on the quantale 2 1 1 2 The relation ⊆ is clearly reflexive via the identity Q-span ⊢ R(a,b) ≤ R′′(a,b) morphism. Finally, if R ⊆ R′ and R′ ⊆ R, and so internally For transitivity, assume ⊢ R(a,b) ≤ R′(a,b) ∧ R′(a,b) ≤ R(a,b) (X,f,g,χ) ⊆ (Y,h,k,ξ) ⊆ (Z,m,n,ζ) by antisymmetry of the order on the internal quantale Denote the required monomorphisms ⊢ R(a,b)= R′(a,b) r : (X,f,g,χ) → (Y,h,k,ξ) and so s : (Y,h,k,ξ) → (Z,m,n,ζ) ′ ⊢ ∀a, b.R(a,b)= R (a,b) There is then an obvious span morphism s ◦ r that is a Meaning externally R = R′. monomorphism in E. We then have Next, we must confirm that composition is monotone in both χ(x) ≤ ξ(r(x)) components. As the proofs are symmetrical, we only consider precomposition explicitly. and so Assume R ⊆ R′. We consider post-composing each of these ξ(r(x)) ≤ ζ(s ◦ r(x)) relations with the relation S. Remembering that the quantale By transitivity of the quantale ordering product preserves order, we calculate χ(x) ≤ ζ(s ◦ r(x)) (S ◦ R)(a,c)= R(a,b) ⊗ S(b,c) _b Then, we must confirm that composition is monotone in both ≤ R′(a,b) ⊗ S(b,c) components. As the proofs are symmetrical, we only consider = R′(a,b) ⊗ S(b,c) precomposition explicitly. Assume (X1,f1,g1,χ1) ⊆ (X2,f2,g2,χ2) as witnessed _b = (S ◦ R′)(a,c) by E monomorphism m : X1 → X2. We consider post- composing each of these spans with the span (Y,h,k,ξ). There Finally, we must confirm that the tensor on Rel(Σ,E)(Q) is is then a Q-span morphism monotone in both arguments. Assume again R ⊆ R′. We calculate m ×B 1 : {(x1,y) | f1(x1)= h(y)} → →{(x2,y) | f2(x2)= h(y)} (R ⊗ S)(a,b,c,d)= R(a,b) ⊗ S(c, d) ≤ R′(a,b) ⊗ S(c, d) and the underlying morphism is a monomorphism in E by ′ standard properties of pullbacks and monomorphisms. By = (R ⊗ S)(a,b,c,d) monotonicity of the tensor Theorem V.4. Let E be a topos, (Σ, E) a variety in E, (⊗◦ (χ × ξ) ◦ hp ,p i)(x, y)= ξ (x) ⊗ ξ(y) and (Q, ⊗, k, ≤) an internal partially ordered commutative 1 1 2 1 ≤ ξ2(m(x)) ⊗ ξ(y) monoid. The category Span(Σ,E)(Q) is a preordered sym- metric monoidal category. = (⊗◦ (χ2 × ξ) ◦ hp1,p2i)(m(x),y) Proof. Firstly, we must confirm that this ordering is indepen- = (⊗◦ (χ2 × ξ) ◦ hp1,p2i◦ (m ×B 1))(x, y) dent of choices of representatives for equivalence classes of Finally, we must confirm that the tensor bifunctor spans. on Span(Σ,E)(Q) is monotone in both arguments. Assume Assume (X1,f1,g1,χ1) ⊆ (Y,h1, k1, ξ1), and span isomor- phisms (X1,f1,g1,χ1) ⊆ (X2,f2,g2,χ2)

i : (X1,f1,g1,χ1) → (X2,f2,g2,χ2) witnessed by E monomorphism m : X1 → X2. There is then a span morphism j : (Y1,h1, k1, ξ1) → (Y2,h2, k2, ξ2) Let m × 1 : (X1,f1,g1,χ1) ⊗ (Y,h,k,ξ) → m : (X1,f1,g1,χ1) → (Y,h1, k1, ξ1) → (X2,f2,g2,χ2) ⊗ (Y,h,k,ξ) be the span morphism that is monic in E required by the and this a E monomorphism by standard theory of products assumed order structure. There is then a span morphism and monomorphisms. We then calculate −1 j ◦ m ◦ i : (X2,f2,g2,χ2) → (Y2,h2, k2, ξ2) ⊗◦ (χ1 × ξ)(x, y)= χ1(x) ⊗ ξ(y) ≤ χ2(m(x)) ⊗ ξ(y) Theorem VII.3. Let E be a topos, (Σ, E) a variety in E, and h : Q → Q a morphism of internal partially ordered = (⊗◦ (χ2 × ξ) ◦ (m × 1))(x, y) 1 2 commutative monoids. There is an identity on objects, strict Theorem VI.1. Let E be a topos, (Σ, E) a variety in E symmetric monoidal Preord-functor and (Q, ⊗, k, ) an internal commutative quantale. There is ∗ an identity on objects, strict symmetric monoidal Preord- h : Span(Σ,E)(Q1) → Span(Σ,E)(Q2) functor W The assignment h 7→ h∗ is functorial. V : Span(Σ,E)(Q) → Rel(Σ,E)(Q) Proof. The h∗ functor is again defined by postcomposition, as Proof. We define the action on morphisms on a chosen rep- in the relational case. Proof of functoriality and preservation resentative span as follows of tensor and preorder is almost identical to the relational case.

V (X,p1,p2,χ)(a,b)= {χ(x) | p1(x)= a ∧ p2(x)= b} x _ Proposition 8. Let E be a topos, (Σ, E) a variety in E It is easy to check that this definition is independent of our and (Q, ⊗, k, ) an internal commutative quantale. For linear choice of representatives. We moreover check preservation of (affine, relevant, cartesian) algebraic Q-relation R : A → B identities: the axiom W

V (A, 1A, 1A,χk)(a1,a2)= {χk(a) | 1A(a)= a1 ∧ 1A(a)= a2} R(a1,b1) ⊗ ... ⊗ R(an,bn) ≤ R(τ(a1, ..., an), τ(b1, ..., bn)) _ = {k | a = a1 ∧ a = a2} holds for every linear (affine, relevant, cartesian) n-ary de- rived operation τ. = _{k | a1 = a2}

=1_A(a1,a2) Proof. Suppose R is a linear relation. we proceed by induc- tion. By definition, if τ is any n-ary operation, then it is That the functor commutes with the tensor is clear from the n definition. That the coherence morphisms for the monoidal R(a ,b ) ≤ R(τ(a ,...,a ), τ(b ,...,b )) structures are preserved on the nose is clear as they were k k 1 n 1 n k=1 constructed using the graph constructions, and V preserves O graphs. To see that V preserves preorders, we assume (in this proof the big tensor symbol is just a shorthand for the quantale product over a finite number of components). Now let (X1,f1,g1,χ1) ⊆ (X2,f2,g2,χ2) τ1,...,τn be operations of arities n, k1,...,kn, respectively. Being τ an operation it is witnessed by a monomorphism m : X1 → X2. We then calculate n i i i i R(τi(a1,...aki ), τi(b1,...bki )) ≤ {χ1(x) | f1(x)= a ∧ g1(x)= b}≤ i=1 O ≤ R(τ(τ (a1,...a1 ),...,τ (an,...ai )), _ ≤ {χ2(m(x)) | f2(m(x)) = a ∧ g2(m(x)) = b} 1 1 k1 n 1 kn 1 1 n n ′ ′ ′ τ(τ1(b1,...bk1 ),...,τn(b1 ,...bkn ))) ≤ _{χ2(x ) | f2(x )= a ∧ g2(x )= b} and combining with the same condition on the τi one obtains Theorem VII.1._ Let E be a topos, (Σ, E) a variety in E, and h : Q → Q a morphism of internal commutative n ki 1 2 i i quantales. There is an identity on objects, strict symmetric R(az,bz) ≤ monoidal Pos-functor i=1 z=1 O O 1 1 n i ≤ R(τ(τ1(a ,...a ),...,τn(a ,...a )), ∗ 1 k1 1 kn h : Rel(Σ,E)(Q1) → Rel(Σ,E)(Q2) 1 1 n n τ(τ1(b1,...bk1 ),...,τn(b1 ,...bkn )) The assignment h 7→ h∗ is functorial. This concludes the proof since every linear term can be written ∗ Proof. h acts by postcomposing a relation R : A × B → Q1 as a concatenation of operations. with h: Affine terms are obtained as compositions of operations and ∗ R h h (R): A × B −→ Q1 −→ Q2 projections. It is thus sufficient to prove that the condition holds for affine relations if is a projection. Then, we can Preservation of identities, compositions and tensors follows τ treat any n-ary projection as a generic operation and proceed from the fact that h preserves quantale identities, products and as in the previous case. But the condition joins. In particular, the preservation of quantale joins makes h also order-preserving, proving that h∗ is a Pos-functor. n The functoriality of assigments is trivial: Postcomposing with R(ak,bk) ≤ R(π(a1,...,an), π(b1,...,bn)) the identity function on a quantale gives back the same Ok=1 category we started from, and the composition of quantale being the right hand side just R(ai,bi), trivially holds when homomorphisms is a quantale homomorphism. R is affine. Relevant terms are obtained as compositions of operations relevant, cartesian) algebraic Q-span (X,f,g,χ) and n-ary and diagonals. Note that a diagonal δ is not a term when taken linear (affine, relevant, cartesian) term τ if alone because it is not a morphism of the form Xn → X for f(x )= a ∧ g(x )= b ∧ ... ∧ f(x )= a ∧ g(x )= b some n. This means that if a term is built using diagonals, 1 1 1 1 n n n n there is always at least one operation that is composed with then there exists x such that the diagonal on the left. The proof is then very similar to the previous ones, with the additional step that if R is f(x)= τ(a1, ..., an) ∧ g(x)= τ(b1, ..., bn) relevant, then the condition has to be proven to hold for every and τ(x1,...,xi,δ(x), xi+1,...,xn), where δ is the n-th diagonal χ(x1) ⊗ ... ⊗ χ(xn) ≤ χ(x) and τ is any (n + m)-ary operation. For cartesian terms it is sufficient to put all these observation Proof. As in the relational case, we proceed by induction. together and proceed in the same way. Let (X,f,g,χ) be a span. By definition, if τ is any n-ary operation, then the axiom (here the big wedge and the big Proposition 9. Let E be a topos, (Σ, E) a variety tensor product are just a shorthand for a logical conjunction in E and (Q, ⊗, k, ) an internal commutative quantale. and a quantale product over a finite number of components, If Q is linear (affine, relevant, cartesian), every morphism respectively) W in Rel(Σ,E)(Q) is linear (affine, relevant, cartesian). n (f(x )= a ∧ g(x )= b ) =⇒ Proof. We only prove the affine case explicitly, all the rest i i i i i=1 being similar. For arbitrary a ,a ,b ,b consider the prod- ^ 1 2 1 2 ∃x : f(x)= τ(a ,...,a ), k(x) ∧ g(x)= τ(b ,...,b )∧ uct . Both and are 1 n 1 n R(a1,b1) ⊗ R(a2,b2) R(a1,b1) R(a2,b2) n elements of Q, hence, being Q affine, we can readily infer ∧ χ(xi) ≤ χ(x) R(a ,b ) ⊗ R(a ,b ) ≤ R(a ,b ). Being the variables arbi- 1 1 2 2 1 1 i=1 trarily chosen, we universally quantify on them obtaining the O is already satisfied. Now let τ, τ ,...,τ be operations of axiom of being affine for R as a valid formula in the ambient 1 n arities n, k ,...,k , respectively. Being τ an operation it is topos E. 1 n n i i i i i i Theorem VIII.10. Let E be a topos and (Q, ⊗, k, ) an (f(x )= τi(a1,...aki ) ∧ g(x )= τi(b1,...bki )) =⇒ internal commutative quantale. Let i : (Σ1, E1) → (Σ2, E2) i=1 W ^ 1 1 n i be a linear interpretation of signatures. There is a strict ∃x : f(x)= τ(τ1(a1,...ak1 ),...,τn(a1 ,...akn ))∧ monoidal functor 1 1 n i ∧g(x)= τ(τ1(b1,...bk1 ),...,τn(b1 ,...bkn ))∧ ∗ lin lin n i : Rel(Σ2,E2)(Q) → Rel(Σ1,E1)(Q) ∧ χ(xi) ≤ χ(x) i=1 The assignment i 7→ i∗ extends to a contravariant functor. O Similar results hold for affine, relevant and cartesian inter- and combining with the same condition on the τi one obtains pretations and relations. n ki i i i i lin (f(xj )= aj ∧ g(xj )= bj) =⇒ Proof. An object of Rel(Σ2,E2)(Q) is written as hhA, σj ii, n i=1 j=1 where A is an object of E and the σj are morphisms A → A ^ ^ ∃x : f(x)= τ(τ (a1,...a1 ),...,τ (an,...ai ))∧ in bijective correspondence with the operations in Σ2, agreeing 1 1 k1 n 1 kn 1 1 n i with them on arities, and such that they satisfy the equations ∧g(x)= τ(τ1(b1,...bk1 ),...,τn(b1 ,...bkn ))∧ in E2 (these equations are just commutative diagams between n ki i the above mentioned morphisms). The linear (affine, relevant, ∧ χ(xj ) ≤ χ(x) ′ cartesian) interpretation i maps every operation in σk ∈ Σ1 i=1 j=1 ′ O O to a linear (affine, relevant cartesian) term i(σk) on Σ2, such This concludes the proof since every linear term can be written that these terms satisfy the equations in E1. This means that ′ as a concatenation of operations. hhA, i(σk)ii is an algebra of type (Σ1, E1). For affine, relevant and cartesian terms the considerations ∗ The functor i then acts as follows: It sends every hhA, σj ii done in the proof of proposition 8 can easily be adapted to ′ to hhA, i(σk)ii, and it is identity on morphisms (the fact lin the span case. that morphisms of Rel(Σ2,E2)(Q) are also morphisms of lin Theorem VIII.17. Let E be a topos and (Q, ⊗, k, ≤) an inter- Rel(Σ1,E1)(Q) is a direct consequence of proposition 8). Functoriality then holds trivially being i∗ identity on mor- nal partially ordered commutative monoid. Let i : (Σ1, E1) → phisms. (Σ2, E2) be a linear interpretation of signatures. There is a strict monoidal functor Proposition 13. Let E be a topos, (Σ, E) a variety in E, i∗ : Spanlin (Q) → Spanlin (Q) and (Q, ⊗, k, ≤) an internal partially ordered commutative (Σ2,E2) (Σ1,E1) monoid. For (Σ, E)-algebras A and B, and linear (affine, The assignment i 7→ i∗ extends to a contravariant functor. Q×Q Similar results hold for affine, relevant and cartesian inter- • ⊗ is a constant of type Q P Q pretations and spans. • ∨ is a constant of type Q ∗ • k is a constant of type Q Proof. i is defined as in the relational case, sending al- B×B • 1B is a constant of type Q gebras of type (Σ2, E2) to their interpretations of type • R , R , R are constants of type (Σ , E ). Being it identity on morphisms by definition (the AB BC CD 1 1 QA×B,QB×C ,QA×C, respectively fact that morphisms of Spanlin (Q) are also morphisms (Σ2,E2) We require this theory to satisfy the set of axioms of Spanlin (Q) is a direct consequence of proposition 13) (Σ1,E1) , functoriality follows trivially. {α1B , {αA}, {αB}, {αC}, {αQ}, {αRAB }, {αRBC }, αcomp} where: Theorem IX.2. Let E, F be toposes, and L : E →F be a A • {αA} is the set of axioms that makes (A, {σi }σi∈Σ) into logical functor. Let (Q, ⊗, k, ) be an internal commutative an algebra of type (Σ, E) quantale in E and (Σ, E) be a signature. There is a symmetric B W • {αB} is the set of axioms that makes (B, {σi }σi∈Σ) into monoidal functor an algebra of type (Σ, E) C ∗ E F • {αC} is the set of axioms that makes (C, {σ }σi∈Σ) into L : Rel(Σ,E)(Q) → Rel(Σ,E)(LQ) i an algebra of type (Σ, E) ∗ The assignment L 7→ L is functorial. • {αQ} is the set of axioms that makes (Q, ⊗, k, ) into Proof. The proof heavily relies on the fact that logical mor- a quantale ′ ′ ′ W phisms preserve models of logical theories: We know that, if T • α1B is the axiom ∀b,b .1B(b,b )= {k|b = b } • {αRAB } is the set of all the axioms, one for every σi ∈ Σ is a logical theory, a logical functor L : E→F preserves every W interpretation (that is, every model), of T in E. This is because of ariety n, of the form an interpretation of T in E assigns to every term and formula its correspondent in the Mitchell-B´ernabou internal language: ∀a1,...,an,b1...,bn Every type is interpreted in a product of objects, and every ( _ constant into a morphism of . The axioms correspond, finally, n E A B to commutative diagrams in E. Since these diagrams involve RAB(σi (a1,...,an), σi (b1,...,bn)), R(aj,bj ) = j=1 ) only limits, exponentials and subobject classifiers, they are O A B preserved by L up to isomorphism. This means that the image = RAB(σi (a1,...,an), σi (b1,...,bn)) through L of objects and morphisms that constitute a model (Note that in this setting to use the quantale order relation of T in E is a model of T in F. The idea is then to state our we have to write explicitly what it is. The axiom above definition of composition and identity of RelE (Q) in terms (Σ,E) is nothing but the algebra preservation axiom for R of logical theories: In this case the composition and the identity AB written explicitly using the algebraic lattice structure) of two algebra-preserving relations will be just a model of this • {α } is the set of all the axioms, one for every σ ∈ Σ theory in E, and will hence be preserved by L. The images RBC i of ariety n,of the form through L of our relations will then still satisfy our definition of composition in the internal language of F, guaranteeing that ∀ L(R◦S)(a,c)= b{LR(a,b)⊗LS(b,c)} = (LR◦LS)(a,c). b1...,bn,c1,...,cn ∗ E F ( From this, we can define L : Rel(Σ,E)(Q) → Rel(Σ,E)(LQ) W _ n as follows: B C ∗ RBC (σi (b1,...,bn), σi (c1,...,cn)), R(bj ,cj ) = • On objects, L (A)= L(A) j=1 ) • On morphisms, denoting with κ the canonical isomor- O = R (σB (b ,...,b ), σC (c ,...,c )) phism from LA × LB to L(A × B), BC i 1 n i 1 n κ LR • Finally, α is the axiom L∗(R)= LA × LB −→ L(A × B) −−→ LQ comp Now we have to state what our composition is in terms of ∀a, c.RAC (a,c)= {RAB(a,b) ⊗ RBC (b,c)|b ∈ B} logical theories. Given a signature , we can define a (Σ, E) An interpretation of this_ theory in the topos E then consists logical theory of three morphisms in E A B C T = (A, B, C, Q, {σi }σi∈Σ, {σi }σi∈Σ, {σi }σi∈Σ, RAB : A×B → Q RBC : B ×C → Q RAC : A×C → Q ⊗, ∨, k, 1B, RAB, RBC , RAC ), Where the sets of axioms {αA}, {αB}, {αC} get inter- where preted into commutative diagrams ensuring that A, B, C • For a given σi ∈ Σ of ariety ni, are internal algebras of signature (Σ, E), respectively, while n A A i – σ is a constant of type A {α AB }, {α BC } guarantee that R and R respect the i n R R AB BC – σB is a constant of type BB i usual algebraic condition. {α } gets interpreted into diagrams i n Q C C i – σi is a constant of type C ensuring that Q is an internal quantale and αcomp guarantees A B Q that RAC is exactly the composition of relations RAB, RBC • f,g,χ are constants of type X ,X ,X ”, respectively E Q×Q in Rel(Σ,E)(Q). • ⊗ is a constant of type Q Q×Q • ≤ is a constant of type Ω Proposition 3. With the same assumptions, the induced func- • k is a constant of type Q tor L∗ of theorem IX.2 commutes with graphs and converses. That is, the following diagrams commute: We require this theory to satisfy the set of axioms {{αA}, {αB}, {αQ}, {αX }}, where: ∗ A E L F • is the set of axioms that makes into Rel (Q) Rel (LQ) {αA} (A, {σi }σi∈Σ) (Σ,E) (Σ,E) an algebra of type (Σ, E) B • {αB} is the set of axioms that makes (B, {σ }σi∈Σ) into (−)◦ (−)◦ i an algebra of type (Σ, E) E F C Alg (Σ, E) Alg (Σ, E) • {αC} is the set of axioms that makes (C, {σi }σi∈Σ) into L an algebra of type (Σ, E) (L∗)op • {αQ} is the set of axioms that makes (Q, ⊗, k, ≤) into E op F op Rel(Σ,E)(Q) Rel(Σ,E)(LQ) a partially ordered monoid • {αX } is the set of axioms, one for every σ ∈ Σ of ariety ◦ ◦ (−) (−) n, of the form E F Rel (Q) Rel (LQ) ∀ ∃x.f(x)= σA(f(x ),...,f(x )∧ (Σ,E) L∗ (Σ,E) x1,...,xn i 1 n B ∧ g(x)= σi (g(x1),...,g(xn) ∧ Proof. Start noting that the graph functor is identity on objects, n so trivially L∗(A) = L∗A = (L∗A) for every object A. For  ◦ ◦ ∧ χ(xj ) ≤ χ(x) a morphism f : A → B, in E, consider the diagram: j=1 O Rel iso L(f × 1B) L(idB ) A model of T in E is just a span that respects the algebraic LA × LB L(A × B) L(B × B) L(Q) structure and we know that L preserves this condition. L∗ then agrees with L on objects and is defined as (LX,Lf,Lg,Lχ) iso iso Rel on the morphism (X,f,g,χ). For composition and identity we idLB LA × LB LB × LB do not need to invoke any logical theory: The identity span Lf × L1B is of the form (X, 1X , 1X ,χk), and the span part is clearly

Rel preserved because functors preserve identities in general. The Where idB is the morphism of E that defines 1B in quantale part χ is the morphism A → 1 → Q where the final E ∗ k RelΣ,E(Q). The top row of the diagram is just L (f)◦, while arrow sends the terminal object to the quantale unit. Again, be- ∗ the bottom one is (L f)◦. The left triangle commutes trivially, ing L logical this is trivially preserved. For composition, note the square commutes because L preserves products, that the span part is composed via pullbacks and L preserves the right triangle commutes because L preserves relational limits. For the quantale part we have, supposing (Z,h,k,ζ) identities (previous proposition). Preservation of the converse to be the composite of (X,f,g,χ) and (Y,f ′,g′,υ), follows trivially from the fact that any logical functor preserves products. L L(χ × υ) L(⊗) LZ L(X × Y ) L(Q × Q) L(Q) Theorem IX.4. Let E, F be toposes, and L : E →F be a logical functor. Let (Q, ⊗, k, ≤) be an internal partially iso iso ordered commutative monoid in E and (Σ, E) be a signature. ⊗ There is a symmetric monoidal functor LX × LY LQ × LQ Lχ × Lυ ∗ E F L : Span(Σ,E)(Q) → Span(Σ,E)(LQ) Where p1,p2 are the pullback projections. The top row is the The assignment L 7→ L∗ is functorial. image of ζ through L. The triangle on the left and the square Proof. Here the same considerations used to prove theo- on the center commute because L preserves limits, while the rem IX.2 hold. Given a signature (Σ, E), the logical theory triangle on the right commutes because every partially ordered we use is monoid is obviously a model of a theory, so the multiplication of Q gets carried in the multiplication of LQ. A B T = (X, A, B, Q, {σi }σi∈Σ, {σi }σi∈Σ, f, g, χ, ⊗, ≤, k), Theorem X.1. Let E be a topos, h : Q1 → Q2 a morphism where of internal commutative quantales, i : (Σ1, E1) → (Σ2, E2) a • For a given σi ∈ Σ of ariety ni, linear interpretation and L : E→F a logical functor. For the n – σA is a constant of type AA i induced functors of theorems VII.1, VII.3, VIII.10, VIII.17, IX.2 i n – σB is a constant of type BB i and IX.4, the following diagram commutes (be aware that in i n C C i ∗ – σi is a constant of type C the hypercube below commutative squares involving L only commute up to isomorphism. Other squares commute up to with h∗ because the former acts by postcomposition with a equality): homomorphism of quantales, that commutes with joins and orders. • • To show that L∗i∗ ≃ i∗L∗, note that for morphisms this is trivial, being i∗ the identity on them. Let then ∗ hhA, σAii be an object of, say, RelE (Q), and consider ∗ L j (Σ2,E2) L ∗ ∗ A ∗ A ′ L i hhA, σj ii. By definition this is equal to L hhA, i(σ )j ii, A ′ A • • where every i(σ )j is a term derived from the σj , so a composition of σA (and eventually diagonals and pro- • • j jections, depending on the interpretation). Being L logical, L∗ L∗ operations of A get carried into operations of LA, hence • • ∗ A A L hhA, i(σ )j′ ii = hhLA,Li(σ )j′ ii is an algebra of type F ∗ A ′ • • L (Σ1, E1) in Rel(Σ1,E1)(LQ). But, being i(σ )j a compo- sition of operations, projections and diagonals, and being L • • A LA • • product preserving, it is Li(σ )j′ ≃ i(σ )j′ . Hence

∗ ∗ A A ′ L i hhA, σj ii = hhLA,Li(σ )j ii L∗ L∗ LA ≃ hhLA,i(σ )j′ ii • • ∗ ∗ A = i L hhA, σj ii Where the inner cube is The proof is the same when L∗ and i∗ act on spans. ∗ ∗ ∗ i∗ To prove that L V ≃ V L , consider the following logical Spanlin,E Spanlin,E (Σ2,E2)(Q1) (Σ1,E1)(Q1) theory:

∗ ∗ A B h h T = (X, A, B, Q, {σi }σi∈Σ, {σi }σi∈Σ, f, g, χ, ⊗, ∨,k,R) i∗ Spanlin,E Spanlin,E Where: (Σ2,E2)(Q2) (Σ1,E1)(Q2) A B • (X, A, B, Q, {σi }σi∈Σ, {σi }σi∈Σ, f, g, χ, ⊗, ∨, k) is the fragment that states that (Q, ⊗, k, ) is a quantale, ∗ A B i that hhA, {σi }σi∈Σii and hhB, {σi }σi∈Σii are algebras Rellin,E (Q ) Rellin,E (Q ) W (Σ2,E2) 1 (Σ1,E1) 1 of the required signature and that (X,f,g,χ) is an algebraic preserving span over Q, with all the obvious h∗ h∗ axioms required to hold (see proof of theorems IX.2 i∗ Rellin,E Rellin,E and IX.4 for details) (Σ2,E2)(Q2) (Σ1,E1)(Q2) A×B • R is a constant of type Q together with the axioms and the outer cube is that say it is an algebraic preserving relation over Q (again refer to the relational case in theorem IX.2) i∗ Spanlin,F Spanlin,F (Σ2,E2)(LQ1) (Σ1,E1)(LQ1) • The additional axiom (Lh)∗ (Lh)∗ R(a,b)= {χ(m)|∃m.(s(m)= a ∧ t(m)= b)} i∗ Spanlin,F Spanlin,F is satisfied. _ (Σ2,E2)(LQ2) (Σ1,E1)(LQ2) This logical theory expresses exactly the fact that R is a relation coming from a span in the sense of the order functor, i∗ so if R = V (X,s,t,χ) then R and (X,s,t,χ) are a model Rellin,F (LQ ) Rellin,F (LQ ) (Σ2,E2) 1 (Σ1,E1) 1 for T . From this we get an isomorphism between LR and V (LX,Lf,Lg,Lχ), and hence (Lh)∗ (Lh)∗ i∗ L∗V (X,f,g,χ)= L∗R ≃ LR ≃ V (LX,Lf,Lg,Lχ) Rellin,F Rellin,F (Σ2,E2)(LQ2) (Σ1,E1)(LQ2) Finally, to verify that L∗h∗ ≃ h∗L∗, just note that it is In both cases the vertical arrows are the functors of theo- possible to state what a quantale homomorphism is in terms rem VI.1. Similar diagrams commute for affine, relevant and of logical theories. This guarantees that if h : Q1 → Q2 cartesian interpretations, relations and spans. is a homomorphism of quantales, so is Lh. Everything then follows from the fact that ∗ acts by postcomposition and Proof. Here the notation A ≃ B will denote that A and B are h L respects it. isomorphic. i∗ trivially commutes with h∗, since the first is identity on morphisms and the second is identity on objects; for the very same reason, i∗ commutes with V . V commutes