Sharing is Linear

Andrea Condoluci Beniamino AccaŠoli Claudio Sacerdoti Coen Department of Computer Science and LIX Department of Computer Science and Engineering Inria & Ecole´ Polytechnique Engineering University of Bologna France University of Bologna Italy beniamino.accaŠ[email protected] Italy [email protected] [email protected]

ABSTRACT Reasonable evaluation and sharing. Fix a dialect λX of the λ- Œe λ-calculus is a handy formalism to specify the evaluation of calculus with a deterministic evaluation strategy →X , and note higher-order programs. It is not very handy, however, when one nfX (t) the normal form of t with respect to →X . If the λ-calculus interprets the speci€cation as an execution mechanism, because were a reasonable execution model then one would at least expect →n nf ( ) terms can grow exponentially with the number of β-steps. Œis is that mechanizing an evaluation sequence t X X t on random why implementations of functional languages and proof assistants access machines (RAM) would have a cost polynomial in the size of always rely on some form of sharing of subterms. t and in the number n of β-steps. In this way a program of λX eval- Œese frameworks however do not only evaluate λ-terms, they uating in a polynomial number of steps can indeed be considered also have to compare them for equality. In presence of sharing, one as having polynomial cost. is actually interested in equality of the underlying unshared λ-terms. Unfortunately, this is not the case, at least not literally. Œe prob- Œe literature contains algorithms for such a sharing equality, that lem is called size explosion: there are families of terms whose size are polynomial in the sizes of the shared terms. grows exponentially with the number of evaluation steps, obtained Œis paper improves the bounds in the literature by presenting by nesting duplications one inside the other—simply writing down the €rst linear time algorithm. As others before us, we are inspired the result nfX (t) may then require cost exponential in n. by Paterson and Wegman’s algorithm for €rst-order uni€cation, it- In many cases sharing is the cure because size explosion is caused self based on representing terms with sharing as DAGs, and sharing by an unnecessary duplications of subterms, that can be avoided equality as bisimulation of DAGs. Beyond the improved complexity, if such subterms are instead shared, and evaluation is modi€ed a distinguishing point of our work is a dissection of the involved accordingly. concepts. In particular, we show that the algorithm computes the Œe idea is to introduce an intermediate seŠing λshX where λX smallest bisimulation between the given DAGs, if any. is re€ned with sharing (we are vague about sharing on purpose) and evaluation in λX is simulated by some re€nement →shX of  KEYWORDS →X . A term with sharing t represents the ordinary term t ob- tained by unfolding the sharing in t—the key point is that t can lambda-calculus, sharing, alpha-equivalence, bisimulation  be exponentially smaller than t . Evaluation in λshX produces a shared normal form nfshX (t) that is a compact representation of

ACM Reference format:   Andrea Condoluci, Beniamino AccaŠoli, and Claudio Sacerdoti Coen. 2016. the ordinary result, that is, such that nfshX (t) = nfX (t ). Œe Sharing Equality is Linear. In Proceedings of ACM Conference, Washington, situation can then be re€ned as in the following diagram: DC, USA, July 2017 (Conference’17), 25 pages. polynomial DOI: 10.1145/nnnnnnn.nnnnnnn λX RAM polynomial polynomial λshX

1 INTRODUCTION Let us explain it. One says that λX is reasonably implementable if both the simulation of λX in λshX up to sharing and the mecha- arXiv:1907.06101v1 [cs.LO] 13 Jul 2019 Origin and Downfall of the Problem nization of λshX can be done in time polynomial in the size of the For as strange as it may sound, the λ-calculus is not a good seŠing initial term t and of the number n of β-steps. If λX is reasonably for evaluating and representing higher-order programs. It is an implementable then it is possible to reason about it as if it were not excellent speci€cation framework, but—it is simply a maŠer of su‚ering of size explosion. Œe main consequence of such a schema fact—no tool based on the λ-calculus implements it as it is. is that the number of β-steps in λX then becomes a reasonable complexity measure—essentially the complexity P de€ned in Permission to make digital or hard copies of all or part of this work for personal or λX coincides with the one de€ned by RAM or Turing machines. classroom use is granted without fee provided that copies are not made or distributed Œe €rst result in this area appeared only in the nineties and for pro€t or commercial advantage and that copies bear this notice and the full citation for a special case—Blelloch and Greiner showed that weak (that on the €rst page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permiŠed. To copy otherwise, or republish, is, not under abstraction) call-by-value evaluation is reasonably to post on servers or to redistribute to lists, requires prior speci€c permission and/or a implementable [10]. Œe strong case, where reduction is allowed fee. Request permissions from [email protected]. everywhere, has received a positive answer only in 2014, when Ac- Conference’17, Washington, DC, USA © 2016 ACM. 978-x-xxxx-xxxx-x/YY/MM...$15.00 caŠoli and Dal Lago have shown that le‰most-outermost evaluation DOI: 10.1145/nnnnnnn.nnnnnnn is reasonably implementable [6]. Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

Reasonable conversion and sharing. Some higher-order seŠings topmost constructor and whose leaves are the (free) variables. A need more than evaluation of a single term. Œey o‰en also have to λ-term with sharing is more generally a Directed Acyclic Graph check whether two terms t and s are X-convertible—for instance to (DAG). Sharing of a subterm t is then the fact that the root node n implement the equality predicate, as in OCaml, or for type checking of t has more than one parent. in seŠings using dependent types, typically in Coq. Œese frame- Œis type of sharing is usually called horizontal or subterm shar- works usually rely on a set of folklore and ad-hoc heuristics for ing, and it is essentially the same sharing as in calculi with explicit conversion, that quickly solve many frequent special cases. In the substitution, environment-based abstract machines, or linear logic— general case, however, the only known algorithm is to €rst evaluate the details are di‚erent but all these approaches provide di‚erent t and s to their normal forms nfX (t) and nfX (s) and then check incarnations of the same notion of sharing. Other types of sharing nfX (t) and nfX (s) for equality—actually, for α-equivalence because include so-called vertical sharing (µ, letrec), twisted sharing [11], terms in the λ-calculus are identi€ed up to α. One can then say and sharing graphs [23]. Œe laŠer provide a much deeper form of that conversion in λX is reasonable if checking nfX (t) =α nfX (s) sharing than our DAGs, and are required by Lamping’s algorithm can be done in time polynomial in the sizes of t and s and in the for optimal reduction. To our knowledge, sharing equality for shar- number of β steps to evaluate them. ing graphs has never been studied—it is not even known whether Sharing is the cure for size explosion during evaluation, but what it is reasonable. about conversion? Size explosion forces reasonable evaluations to produce shared results. Equality in λX unfortunately does not Sharing equality as bisimilarity. When λ-terms with sharing are trivially reduce to equality in λshX , because a single term admits represented as DAGs, a natural way of checking sharing equal- many di‚erent shared representations in general. Œerefore, one ity is to test DAGs for bisimilarity. Careful here: the transition needs to be able to test sharing equality, that is to decide whether system under study is the one given by the directed edges of the   t =α s given two shared terms t and s. DAG, and not the one given by β-reduction steps, as in applicative For conversion to be reasonable, sharing equality has to be bisimilarity—our DAGs may have β-redexes but we do not reduce testable in time polynomial in the sizes of t and s. Œe obvious them in this paper, that is an orthogonal issue, namely evaluation.  algorithm that extracts the unfoldings t and s  and then checks Essentially, two DAGs represent the same unfolded λ-term if they α-equivalence is of course too na¨ıve, because computing the un- have the same structural paths, just collapsed di‚erently. folding is exponential. Œe tricky point therefore is that sharing To be precise, sharing equality is based on what we call shar- equality has to be checked without unfolding the sharing. ing equivalences, that are bisimulations plus some additional re- In these terms, the question has €rst been addressed by AccaŠoli quirements about free variables and the requirement that they are and Dal Lago in [5], where they provide a quadratic algorithm for equivalence relations. sharing equality. Consequently, conversion is reasonable. Binders, cycles, and domination. A key point of our problem is A closer look to the costs. Once established that strong evaluation the presence of binders, i.e. abstractions, and the fact that equality and conversion are both reasonable it is natural to wonder how on usual λ-terms is α-equivalence. Graphically, it is standard to eciently can they be implemented. AccaŠoli and Sacerdoti Coen see abstractions as geŠing a backward edge from the variable they in [3] essentially show that strong evaluation can be implemented bind—a way of representing scopes that dates back to Bourbaki in within a bilinear overhead, i.e. with overhead linear in the size Elements´ de Šeorie´ des Ensembles, but also supported by the strong of the initial term and in the number of β-steps. Œeir technique relationship between λ-calculus and linear logic proof nets. has then been simpli€ed by AccaŠoli and Guerrieri in [4]. Both In this approach, binders introduce a form of cycle in the λ- works actually address open evaluation, which is a bit simpler than graph: while two free variables are bisimilar only if they coincide, strong evaluation—the moral however is that evaluation is bilinear. two bound variables are bisimilar only when also their binders Consequently, the size of the computed result is bilinear. are bisimilar, suggesting that λ-terms with sharing are, as directed Œe boŠleneck for conversion then seemed to be AccaŠoli and graphs, structurally closer to deterministic €nite automata (DFA), Dal Lago’s quadratic algorithm for sharing equality. Œe litera- that may have cycles, than to DAGs. Œe problem with cycles is ture actually contains also other algorithms, studied with di‚erent that in general bisimilarity of DAGs cannot be checked in linear motivations or for slightly di‚erent problems, discussed among re- time—Hopcro‰ and Karp’s algorithm [22], the best one, is only lated works in the next section. None of these algorithms however pseudo-linear, that is, with an inverse Ackermann factor. matches the complexity of evaluation. Technically speaking, the cycles induced by binders are not ac- In this paper we provide the €rst algorithm for sharing equality tual cycles: unlike usual downward edges, backwards edges do that is linear in the size of the shared terms, improving over the not point to subterms of a node, but are merely a graphical repre- literature. Œerefore, the complexity of sharing equality matches sentation of scopes. Œey are indeed characterized by a structural the one of evaluation, providing a combined bilinear algorithm for property called domination—exploring the DAG from the root, one conversion, that is the real motivation behind this work. necessarily visits the binder before the bound variable. Domination turns out to be one of the key ingredients for a linear algorithm in Computing Sharing Equality presence of binders. Sharing as DAGs. Sharing can be added to λ-terms in di‚erent forms. In this paper we adopt a graphical approach. Roughly, a Previous work. Sharing equality bears similarities with uni€ca- λ-term can be seen as a (sort of) directed tree whose root is the tion, which are discussed in the next section. For what concerns Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA sharing equality itself, in the literature there are only two algo- reduce sharing equality in presence of binders to the Blind rithms explicitly addressing it. First, the already cited quadratic check, under key assumptions on the context in which one by AccaŠoli and Dal Lago. Second, a O(n logn) algorithm by terms are tested (see queries, Sect. 4). Grabmayer and Rochel [21] (where n is the sum of the sizes of the • Minimality. Œe set of shared representations of an ordi- shared terms to compare, and the input of the algorithm is a graph), nary λ-term t is a laŠice: the boŠom element is t itself, the obtained by a reduction to equivalence of DFAs and treating the top element is the (always existing) maximally sharing of t, more general case of λ-terms with letrec. and for any two terms with sharing there exist inf and sup. Essentially, AccaŠoli & Dal Lago and Grabmayer & Rochel Contributions: a theory and a 2-levels linear algorithm. Œis paper address sharing equality by computing the top elements of is divided in two parts. Œe €rst part develops a re-usable, self- the laŠices of the two λ-terms with sharing, and then com- contained, and clean theory of sharing equality, independent of paring them for α-equivalence. We show that our Blind the algorithm that computes it. Some of its concepts are implicitly check—and morally every PW-based algorithm—computes used by other authors, but never emerged from the collective un- the sup of t and s, that is, the term having all and only the conscious before (propagated queries in particular)—others instead sharing in t or s, that is the smallest sharing equivalence are new. A key point is that we bypass the use of α-equivalence between the two DAGs. Œis insight, €rst pointed out in by relating sharing equalities on DAGs with λ-terms represented PW’s original paper to characterize most general uni€ers, in a locally nameless way [17]. In such an approach, bound names is a prominent concept in our theory of sharing equality are represented using de Bruijn indices, while free variables are as well. represented using names—thus α-equivalence collapses on equality. • Proofs, invariants, and detailed development. We provide de- Œe theory culminates with the sharing equality theorem, which tailed correctness, completeness, and linearity proofs, plus connects equality of λ-terms and sharing equivalences on shared a detailed treatment of the relationship between equality λ-terms, under suitable conditions. on locally nameless λ-terms and sharing equivalences on Œe second part studies a linear algorithm for sharing equality λ-graphs. Our work is therefore self-contained, but for the by adapting Paterson and Wegman’s (shortened to PW) linear algo- fact that most of the theorems and their proofs have been rithm for €rst-order uni€cation [27] to λ-terms with sharing. Our moved to the appendix. algorithm is actually composed by a 2-levels, modular approach, • Concrete implementation. We implemented our algorithm pushing further the modularity suggested—but not implemented— 1 and veri€ed experimentally its linear time complexity. in the nominal uni€cation study by Calves` & Fernandez´ in [15]: However, let us stress that despite providing an imple- • Blind check: a reformulation of PW from which we re- mentation our aim is mainly theoretical. Namely, we are moved the management of meta-variables for uni€cation. interested in showing that sharing equality is linear (to ob- It is used as a €rst-order test on λ-terms with sharing, to tain that conversion is bilinear) and not only pseudo-linear, check that the unfolded terms have the same skeleton, even though other algorithms with super-linear asymptotic ignoring variables. complexity may perform beŠer in practice. • Variables check: a straightforward algorithm executed a‰er the previous one, testing α-equivalence by checking that 2 RELATED PROBLEMS AND TECHNICAL bisimilar bound variables have bisimilar binders and that CHOICES two di‚erent free variables are never equated. We suggest the reader to skip this section at a €rst reading. Œe decomposition plus the correctness and the completeness of the checks crucially rely on the theory developed in the €rst part. Related problems. Œere are various problems that are closely re- lated to sharing equality, and that are also treated with bisimilarity- Še value of the paper. It is delicate to explain the value of our based algorithms. Let us list similarities and di‚erences: work. Œree contributions are clear: 1) the improved complexity of the problem, 2) the consequent downfall on the complexity of • First-order uni€cation. On the one hand the problem is more β-conversion, and 3) the isolation of a theory of sharing equality. At general, because uni€cation roughly allows to substitute the same time, however, our algorithm looks as an easy adaptation variables with terms not present in the original DAGs, of PW, and binders do not seem to play much of a role. Let us then while in sharing equality this is not possible. On the other draw aŠention to the following points: hand, the problem is less general, because it does not allow binders and does not test α-equivalence. Œere are basically • Identi€cation of the problem: the literature presents similar two linear algorithm for €rst-order uni€cation, Paterson studies and techniques, and yet we are the €rst to formulate and Wegman’s (PW) [27] and Martelli and Montanari’s and study the problem per se (uni€cation is di‚erent, and it (MM) [26]. Both rely on sharing to be linear. PW even takes is usually not formulated on terms with sharing), directly terms with sharing as inputs, while MM deals with sharing (i.e. without reducing it to DFAs, like in Grabmayer and in a less direct way, except in its less known variant [25] Rochel), and with a €ne-grained look at the complexity that takes in input terms shared using the Boyer-Moore (AccaŠoli and Dal Lago only tried not to be exponential). technique [12]. • Še role of binders: the fact that binders can be treated straightforwardly is—we believe—an insight and not a weakness of our work. Essentially, domination allows to 1Œe code is available on hŠp://www.cs.unibo.it/∼sacerdot/sharing equivalence.tgz. Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

• Nominal uni€cation is uni€cation up to α-equivalence (but On the other hand, abstract machines with local environments not up to β or η equivalence) of λ-calculi extended with (e.g. Krivine asbtract machine), that typically rely on de Bruijn name swapping, in the nominal tradition. It has €rst stud- indices, produce di‚erent representations where every subterm is ied by Urban, PiŠs, and Gabbay [31] and ecient algo- paired with an environment, representing a delayed substitution. rithms are due to two groups, Calves` & Fernandez´ and Œose outputs cannot be directly compared for sharing equality, Levy & Villaret, adapting PW and MM form €rst-order uni- they €rst have to be translated to a global environment representation— €cation. It is very close to sharing equality, but the known the study of such a translation is future work. best algorithms [15, 24] are only quadratic. See [14] for a unifying presentation. 3 PRELIMINARIES • Paˆern uni€cation. Miller’s paŠern uni€cation can also be In this section we introduce λ-graphs, the representation of λ-terms stripped down to test sharing equality. Qian presents a PW- with sharing that we consider in this paper. First of all we introduce inspired algorithm, claiming linear complexity [28], that the usual λ-calculus without sharing. seems to work only on unshared terms. We say claiming because the algorithm is very involved and the proofs are λ-terms and equality. Œe syntax of λ-terms is usually de€ned as far from being clear. Moreover, according to Levy and follows: Villaret in [24]: it is really dicult to obtain a practical (Named) Terms t,s ::= x | λx.t | t s . We believe that algorithm from the proof described in [28] Œis representation of λ-terms is called named, since variables and is fair to say that Qian’s work is hermetic (please try to abstractions bear names. Equality on named terms is not just syntac- read it!). tic equality, but α-equality: terms are identi€ed up to the renaming • Nominal matching. Calves` & Fernandez´ in [16] present an of bound variables, because for example the named terms λx.λy.x algorithm for nominal matching (a special case of uni€ca- and λy.λx.y should denote the same λ-term. tion) that is linear, but only on unshared input terms. Œis is the standard representation of λ-terms, but reasoning • Equivalence of DFA. Automata do not have binders, and in presence of names is cumbersome, especially names for bound yet they are structurally more general than λ-terms with variables as they force the introduction of α-equivalence classes. sharing, since they allow arbitrary directed cycles, not Moreover nodes naturally bear no names, thus we prefer to avoid necessarily dominated. As already pointed out, the best assigning arbitrary variable names when performing the readback equivalence algorithm is only pseudo-linear [22]. of a node to a λ-term. On closed λ-terms (terms with only bound variables), this sug- Algorithms for α-equivalence extended with further principles (e.g. gests a approach: by using , variables permutations of let expressions), but not up to sharing unfolding, nameless de Bruijn indices simply consist of an , a indicating the number is studied by Schmidt-Schauß, Rau, and Sabel in [30]. index of abstractions that occur between the abstraction to which the variable is bound and that variable occurrence. For example, the Hash-consing [18, 20] is a technique to share Hash-consing. named term λx.λy.x and the nameless term λλ1 denote the same λ- purely functional data, traditionally realised through a hash ta- term. Œe nameless representation can be extended to open λ-terms, ble [7]. It is a eager approach to conversion of λ-terms, in which but it adds complications because for example di‚erent occurrences one keeps trace in a huge table of all the pairs of convertible terms of the same free variable unnaturally have di‚erent indices. previously encountered. Our approach is somewhat dual or lazy, Since we are forced to work with open terms (because proof as- as our technique does not record previous tests, only the current sistants require them) the most comfortable representation and the equality problem is analysed. With hash consing, to check that one we adopt in our proofs is the locally nameless representation two terms are sharing equivalent it suces to perform a lookup (see Chargueraud´ [17] for a thorough discussion). Œis represen- in the hash table, but €rst the terms have to be hash-consed ( i.e. tation combines named and nameless, by using de Bruijn indices maximally shared), which requires quasilinear running times. for bound variables (thus avoiding the need for α-equivalence), and names for free variables. Œis representation of λ-terms is Technical choices. Our algorithm requires λ-terms to be repre- the most faithful to our de€nition of λ-graphs: as we shall see, to sented as graphs. Œis choice is fair, since abstract machines with compare two bound variable nodes one compares their binders, global environments such as those described by AccaŠoli and Bar- but to compare two free variable nodes one uses their identi€er, ras in [2] do manipulate similar representations, and thus produce which in implementations is usually their memory address or a λ-graphs to be compared for sharing equality—moreover, it is es- user-supplied string. sentially the same representation induced by the translation of Œe syntax of locally nameless (l.n.) terms is: λ-calculus into linear logic proof nets [1, 29] up to explicit sharing | | | ( ∈ N ∈ A) nodes—namely ?-links—and explicit boxes. In this paper we do not (l.n.) Terms t,s ::= i a t s λt i , a consider explicit sharing nodes (that in λ-calculus syntax corre- where i denotes a bound variable of de Bruijn index i (N is the set spond to have sharing only on variables), but one can translate in of natural numbers), and a denotes a free variable of name a (A is linear time between that representation to ours (and viceversa) by a set of atoms). collapsing these variables-as-sharing on their child if they are the In the rest of the paper, we switch seamlessly between di‚erent child of some other node. Our results could be adapted to this other representations of λ-terms (without sharing): we use named terms approach, but at the price of more technical de€nitions. in examples, as they are more human-friendly, but locally nameless Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

a) @ b) @ c) @ Õ Õ λ | # λ λ : @ I G  Õ    Ø @ 7 λ @ @ @  Õ Õ  xÕ λ x 8 λ y 7  Ö  Ø x y # w Ð y w

(λx. x (λy.w)) ((λy.w)w) (λx. x (λy.w)) ((λy.w)w) (λx.x x)(x x)

Figure 1: a) λ-term as a λ-tree, without sharing; b) λ-graph with sharing (same term of a); c) λ-graph breaking domination. terms in the technical parts, since much more elegant and short uniquely identi€es it2: this identi€er is going to be used proofs can be obtained by avoiding to reason explicitly on bound later, when de€ning the readback of nodes to λ-terms names and α-equivalence. (cf. Šeorem 3.9). – A bound variable node has exactly one child, called Terms as graphs, informally. Graphically, λ-terms can be seen as its binder ( ). We write Var(l) for a node labelled by syntax trees—please have a look at the example in Fig. 1(a). Note  Var with binder l. Še corresponding binding edge is that in all Fig. 1 we provide names to bound variables (both in the represented with a dashed line. graphs and in the terms below) only to make the examples more understandable; as already mentioned, our upcoming notion of Domination. Not every pre–λ-graph represents a λ-graph. For λ-graph follows the locally nameless convention (as in Fig. 2). instance, the graph in Fig. 1(c) does not, because the bound variable As seen in the €gure, we apply two tweaks relative to variable x is visible outside the scope of its abstraction, since there is a path nodes: to x from the application above the abstraction that does not pass • Binding edges: bound variable nodes have a binding (dashed) through the abstraction itself. One would say that such a graph edge towards the abstraction node that binds them; represents (λx.xx)(xx), but the variable x in xx and the one in • Variables merging: all the nodes corresponding to the oc- λx.xx =α λy.yy cannot be the same. currences of a same variable are merged together, like the It is well-known that scopes corresponding to λ-terms are char- three occurrences of w in the example. In this way, the acterized by a property borrowed from control-ƒow graphs called equality of free variable nodes is just the physical equality domination (also called unique binding by Wadsworth [32], and of nodes. checkable in linear time [8, 13, 19]): given two nodes n and m of a Sharing is realized by simply allowing all nodes to have more graph G, we say that n dominates m when every path from a root than one parent, as for instance the abstraction on y in Fig. 1(b) of G to m crosses n. To de€ne this property formally, we €rst need —note that sharing can happen inside abstractions, e.g. λy.w is to de€ne paths in a pre–λ-graph. shared under the abstraction on x. Notations for paths. Paths are a crucial concept, needed both to Our notion of λ-terms with sharing is given in Œeorem 3.7. It de€ne the readback to λ-terms and to state formally the properties

is built in in two steps: we €rst introduce pre–λ-graphs, and later of λ-graphs of being acyclic and dominated. de€ne the required structural properties that make them λ-graphs. A path in a graph is determined by a start node together with a Definition 3.1 (pre–λ-graphs). A pre–λ-graph is a directed trace, i.e. a sequence of directions. Œe allowed directions are “ ”, 

graph with three kind of nodes: “ ”, and “ ”: we are not going to consider paths that use binding  edges “ ”, because these edges simply denote scoping and do not • Application: an application node is labelled with App and  actually point to subterms of (the readback of) a node. has exactly two children, called le‰ ( ) and right ( ). We write App(n,m) for a node labelled by App whose le‡ child Definition 3.2 (Paths, traces). We de€ne traces as €nite se-

is n and whose right child is m. quences of directions:  • Abs Abstraction: an abstraction node is labelled with and   = | | has exactly one child, called its body ( ). We write Abs(n) Directions d ::  for a node labelled by Abs with body n. We denote by l a Traces τ ::= ϵ | τ · d generic abstraction node. Let n,m be nodes of a pre–λ-graph, and τ be a trace. We de€ne • Variable: a variable node is labelled with Var, and may be inductively the judgement “n τ m”, which reads “path from n to m free or bound: (of trace τ )”: – A free variable node has no children, and is denoted by Var(). We assume a function name(·) that assigns to 2We also assume that the set of atoms A is such that the equality of atoms can be every free variable node n an atom name(n) ∈ A that tested in constant time. Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

a) @ @ b) @ c) λ λ d) λ λ e) λ λ G G G G G G Ò   ×  × @ @ @ @ v× v× v× v× v× v×  Ø  Ø Ó   Ø λ λ λ λ λ F E E E E v Ô v Ô v Ô v Ô v Ô

Figure 2: Examples of sharing equivalences and queries.

• Empty: n ϵ n. to adopt another approach. We de€ne a readback procedure as- 

τ τ · sociating a λ-term r (without sharing) to each root node r of • Abstraction: if n Abs(m), then n m.  the λ-graph, in suchJ aK way that shared sub-graphs simply appear • τ App( , ) τ · Application: if n m1 m2 , then n m1 and multiple times. However, since we use de Bruijn indices for bound τ · n m2. variables, any node by itself does not uniquely identify a λ-term: in fact, its readback depends on the path through which it is reached We just write n τ if n τ m for some node m when the endpoint from a root, also known as its access path [9]. Œat path determines m is not relevant. the abstraction nodes encountered, and thus the indices to assign To state formally the requirements that make a pre–λ-graph a λ- to bound variable nodes. We de€ne formally index(l | n τ ), the graph, we need two additional concepts: the roots of a pre–λ-graph, index of an abstraction node l according to a path n τ crossing l and paths crossing a node. (recall that l,l 0 denote abstraction nodes): Root nodes. pre–λ-graphs (and later λ-graphs) may have various Definition 3.8 (index(· | ·)). Let n,l be nodes of a λ-graph G, root nodes. What is maybe less expected, is that these roots may and n τ a path crossing l. We de€ne index(l | n τ ) by structural share some parts of the graph. Consider Fig. 1(b), and imagine to induction on the derivation of the judgement “n τ crosses l”: remove the root and its edges: the outcome is still a perfectly legal pre–λ-graph. We admit these con€gurations because they actually index(l | n τ l) B 0 · 0 0 arise naturally in implementations, especially of proof assistants. index(l | n τ d l ) B index(l | n τ ) + 1 if l , l Definition 3.3 (Roots). Let r be a node of a pre–λ-graph G. r is index(l | n τ ·d m) B index(l | n τ ) otherwise. a root if and only if the only path with endnode r has empty trace. Definition 3.9 (Readback to λ-terms · ). Let G be a λ-graph. Definition 3.4 (Path Crossing a Node). Let n,m be nodes of a For every root r and path r τ n, we de€ne theJ K readback qr τ ny of n τ pre–λ-graph, and τ a trace such that n . We de€ne inductively the relative to the access path r τ n, by cases on n: τ n m τ τ judgment “ crosses ”: (1) qr Var(l)y B i where i B index(l | r ). τ τ • if n m, then n crosses m (2) qr τ Var()y B a where a B name(n).

τ τ ·d τ ·d  • if n crosses m and n , then n crosses m. τ τ ·

(3) qr Abs(m)y B λ rr mz.  Definition 3.5 (Dominated pre–λ-graph). Let G be a pre–λ- · · τ App( , ) s τ { s τ  { graph, and n,m be nodes of G: we say that m dominates n when every (4) qr n1 n2 y B r n1 r n2 . τ path r n from a root node r crosses m. ϵ When τ = ϵ, we just write r instead of qr y. We say that G is dominated when each bound variable node Var(l) J K is dominatedd by its binder l. Some remarks about Œeorem 3.9: • Œe hypothesis that r is a root node is necessary to ensure Definition 3.6 (Acyclic pre–λ-graph). We say that a pre–λ- that the readback qr τ y is a valid locally-nameless term. graph G is acyclic when for every node n in G and every trace τ , τ τ In fact Point 1 of the de€nition uses index(l | r ), which n n if and only if τ = ϵ. is well-de€ned only if r τ Var(l) crosses l. When r is a Our precise de€nition of λ-terms with sharing follows: root node, this is the case by domination (Œeorem 3.5). • Œe de€nition is recursive, but it is not immediately clear Definition 3.7 (λ-graphs). A pre–λ-graph G is a λ-graph if it what is the measure of termination. In fact, the readback satis€es the following additional structural properties: calls itself recursively on longer paths. Still, the de€nition • Finite: G has a €nite number of nodes. is well-posed because paths do not use binding edges, and • Acyclic: if binding edges are ignored, G is a DAG (Def. 3.6). because λ-graphs are €nite and acyclic (Œeorem 3.6). • Dominated: bound variable nodes are dominated by their binder (Def. 3.5). 4 THE THEORY OF SHARING EQUALITY Readback. Œe sharing in a λ-graph can be unfolded by dupli- Sharing equivalence. To formalize the idea that two di‚erent λ- cating shared sub-graphs, obtaining a λ-tree. We prefer however graphs unfold to the same λ-term, we introduce a general notion

Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA  • n R m n R m m R p n R n

m R n  n R p   App(n1,n2) R App(m1,m2) App(n1,n2) R App(m1,m2)  n1 R m1 n2 R m2

Abs(n) R Abs(m)  Var(n) R Var(m) n R m n R m 

Figure 3: Sharing equivalence rules on a λ-graph

of equivalence between nodes (sharing equivalence, Œeorem 4.2) – Equivalence: ≡ is an equivalence ; whose intended meaning is that two related nodes have the same – Bisimulation : ≡ is homogeneous and closed under the  readback. Œis notion shall rest on various requirements; the €rst rules   of Figure 3. one is that only homogeneous nodes can be related: – Open: v ≡ w implies v = w for every free variable nodes v,w. Definition 4.1 (Homogeneous nodes [27]). Let n,m be nodes ≡ of a λ-graph G. We say that n and m are homogeneous if they are Equivalently, is a sharing equivalence if it is a blind sharing equiv- both application nodes, or they are both abstraction nodes, or they are alence and it also satis€es the following conditions on variable nodes: open both free variable nodes, or they are both bound variable nodes. the requirement for free variable nodes, and the under  for bound variable nodes. In the following, let us denote by R a generic Example. Consider Fig. 2(a). Œe green (horizontal) waves are an over the nodes of a λ-graph G. We call a relation R homogeneous if economical representation of a sharing equivalence—nodes in the it only relates pairs of homogeneous nodes, i.e. n R m implies that same class are connected by a green path, and reƒexive/transitive n and m are homogeneous. waves are omiŠed. Another requirement for sharing equivalences is that they must be closed under certain structural rules. More in general, in the rest Remark 4.3. (Blind) sharing equivalences are closed by intersec- of this paper we are going to characterize various kinds of relations tion, so that if there exists a (blind) sharing equivalence on a λ-graph

as being closed under di‚erent sets of rules, which can all be found then there is a smallest one. in Figure 3:  Œe requirements for a sharing equivalence ≡ on a λ-graph G • Equivalence rules: rules • are the usual rules that essentially ensure that G quotiented by ≡ has itself the structure   characterize equivalence relations, respectively reƒexivity, of a λ-graph. Note that blind sharing equivalences are not enough, symmetry, and transitivity. because without the scoping rule, binders are not unique up to ≡—it

• Bisimulation rules:  is nonetheless possible to prove that paths up to ≡ are acyclic, which  – Downward propagation rules: rules  are is going to be one of the key properties to prove the completeness

downward propagation rules on the λ-graph. Œe of the Blind check. rules and state that if two application nodes  ≡ are related, then also their corresponding le‰ and right Remark 4.4. Let be a blind sharing equivalence on a λ-graph G. children should be related. Œe rule  states that Šen: if two abstraction nodes are related, then also their (1) Acyclicity up to ≡: paths upto ≡ in G are acyclic. bodies should be related. (2) Sharing equivalences as λ-graphs: if ≡ also satis€es the – Scoping rule: the rule  states that if two bound vari- name conditions then G/≡ is a λ-graph. able nodes are related, then also their binders should For instance, Fig. 2(b) shows the λ-graph corresponding to the be related. quotient of the one of Fig. 2(a) by the sharing equivalence induced Œe last requirement for a sharing equivalence is the handling by the green waves. of free variable nodes: a sharing equivalence shall not equate two Sharing equivalences do capture equality of readbacks, as we di‚erent free variable nodes, cf. the requirement Open in the up- shall show, in the following sense (this is a sketch given to guide the coming de€nition. reader towards the proper relationship, formalized by Œeorem 4.9 We are now ready to de€ne sharing equivalences: at the end of this section): • ≡ Definition 4.2 ((Blind) sharing e€ivalences). Let ≡ be a sharing to α: if n m then n = m ; • J K J K binary relation over the nodes of a λ-graph G. α to sharing: if n = m then there exists a sharing equivalence ≡ suchJ K that nJ ≡Km. •≡ is a blind sharing equivalence if:

– Equivalence: ≡ is an ; (Spreaded) queries. According to the sketch we just provided, – Blind bisimulation : ≡ is homogeneous and closed under to check the sharing equality of two terms with sharing, i.e. two  the rules  of Figure 3. λ-graphs with roots n and m, it is enough to compute the smallest •≡ is a sharing equivalence if: sharing equivalence ≡ such that n ≡ m, if it exists, and failing Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen otherwise. Œis is what our algorithm does. At the same time, equivalence because it does not include the Abs-nodes above the however, it is slightly more general: it may test more than two original query—note that spreading only happens downwards. To nodes at the same time, i.e. all the pairs of nodes contained in a obtain a sharing equivalence one has to also include the Abs-nodes query. (Fig. 2(e)). Œe example does not show it, but in general then one has to start over spreading the new relation (eventually having to add Definition 4.5 (ery). A query Q over a λ-graph G is a binary other Abs-nodes found in the process, and so on). Œese iterations relation over the root nodes of G. are obviously problematic in order to keep linear the complexity of Œe simplest case is when there are only two roots n and m the procedure—a key point of Paterson and Wegman’s algorithm is and the query contains only n Q m (depicted as a blue wave in that every node is processed only once. Fig. 2(a))—from now on however we work with a generic query Q, What makes possible to extend their algorithm to binders is that which may relate nodes that are roots of not necessarily disjoint the query is context-free, i.e. it is not in the scope of any abstraction, or even distinct λ-graphs; our focus is on the smallest sharing which is the case since it involves only pairs of roots, that therefore equivalence containing Q. are above all abstractions as in Fig. 2(c). Œen—remarkably—there Let us be more precise. Every query Q induces a number of is no need to iterate the propagation of the query. Said di‚erently, other equality requests obtained by closing Q with respect to the if the relation Q is context-free then Q# is universal. equivalence and propagation clauses that every sharing equivalence Œe structural property of λ-graphs guaranteeing the absence of has to satisfy. In other words, every query induces a spreaded query. iterations for context-free queries is domination. Domination asks that to reach a bound variable from outside its scope one necessarily Definition 4.6 (Spreading R#). Let R be a binary relation over

needs to €rst pass through its binder. Œe intuition is that if one the nodes of a λ-graph G. Še spreading R# induced by R is the binary

  starts with a context-free query then there is no need to iterate relation on the nodes of G inductively de€ned by closing R under the  • because binders are necessarily visited before the variables while rules  of Figure 3.   propagating the query downwards. Example. Œe spreading Q# of the (blue) query Q in Fig. 2(a) is Let us stress, however, that it is not evident that domination the (reƒexive and ) of the green waves. is enough. In fact, domination is about one bound variable and its only binder. For sharing equivalence instead one deals with a Blind universality of Q#. Note that the spreaded query Q# is de- class of equivalent variables and a class of binders—said di‚erently, €ned without knowing if there exists a (blind) sharing equivalence domination is given in a seŠing without queries, and is not obvious containing the query Q—there might very well be none (if the nodes that it gets along well with them. Œe fact that domination on single are not sharing equivalent). It turns out that the spreaded query binders is enough for spreaded queries to be universal requires Q# is itself a blind sharing equivalence, whenever there exists a indeed a non-trivial proof and it is a somewhat surprising fact. blind sharing equivalence containing the query Q. In that case, # unsurprisingly, Q is also the smallest blind sharing equivalence Proposition 4.8 (Universality of Q#). Let Q be a query over a containing the query Q. λ-graph G. If there exists a sharing equivalence ≡ containing Q, then Q# Proposition 4.7 (Blind universality of Q#). Let Q be a query. the spreaded query is the smallest sharing equivalence containing Q If there exists a blind sharing equivalence ≡ containing Q then: . (1) Še spreaded query Q# is contained in ≡, i.e. Q# ⊆ ≡. Œe proof of this proposition is in Appendix A.4, page 17, where (2) Q# is the smallest blind sharing equivalence containing Q. it is obtained as a corollary of other results connecting equality of λ Cycles up to Q#. Let us apply Œeorem 4.4(1) to Q#, and take -terms and sharing equivalences, that also rely crucially on the the contrapositive statement: if paths up to Q# are cyclic then the fact that queries only relate roots. It can also be proved directly, Q# is not a blind sharing equivalence. Œe Blind Check in Sect. 6 but it requires a very similar reasoning, which is why we rather indeed fails as soon as it €nds a cycle up to Q#. Note, now, that prove it indirectly. # Q satis€es the equivalence and bisimulation requirements for a Še sharing equality theorem. We have now introduced all the blind sharing equivalence by de€nition. Œe only way in which it needed concepts to state the precise connection between the equal- might not be such an equivalence then, is if it is not homogeneous. ity of λ-terms, queries, and sharing equivalences, which is the main Said di‚erently, there is in principle no need to check for cycles, it result of our abstract study of sharing equality. is enough to test for homogeneity. We are going to do it anyway, because cycles provide earlier failures—there are also other practical Theorem 4.9 (Sharing e€ality). Let Q be a query over a λ- reasons to do so, to be discussed in Sect. 6. graph G. Q# is a sharing equivalence if and only if n = m for every nodes n,m such that n Q m. J K J K Universality of the spreaded query Q#. Here it lies the key con- ceptual point in extending the linearity of Paterson and Wegman’s Despite the—we hope—quite intuitive nature of the theorem, its algorithm to binders and working up to α-equivalence of bound proof is delicate and requires a number of further concepts and variables. lemmas, developed in Appendices A–B. Œe key point is €nding In general the spreading R# of a binary relation R may not be a an invariant expressing how queries on roots propagate under sharing equivalence, even though a sharing equivalence containing abstractions and interact with domination, until they eventually R exists. Consider for instance the relation R in Fig. 2(d): R coin- satisfy the name conditions for a sharing equivalence, and vice cides with its spreading R# (up to reƒexivity), which is not a sharing versa. Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

# Let us conclude the section by stressing a subtlety of Œeorem 4.9. a linear relation =c generating Q , based on keeping a Consider Fig. 2(c)—with that query the statement is satis€ed. Con- canonical element for every sharing equivalence class. sider Fig. 2(d)—that relation is not a valid query because it does not (2) Merging equivalence classes: merging equivalence classes relate root nodes, and in fact the statement would fail because the is an operation that, for as ecient as it may be, it is not readback of the two queried nodes are not the same and not even a costant time operation. Œe trickiness of the visit of the well-de€ned. Consider the relation R in Fig. 2(e)—now R and R# λ-graph is indeed meant to guarantee that, if the query coincide (up to reƒexivity) and R# is a sharing equivalence, but the is satis€able, one never needs to merge two equivalence theorem (correctly) fails, because not all queried pairs of nodes are classes, but only to add single elements to classes. equivalent, as in Fig. 2(d). Algorithm 1: Œe BlindCheck Algorithm 5 ALGORITHMS FOR SHARING EQUALITY Data: an initial state From now on, we focus on the algorithmic side of sharing equality. Result: Fail or a €nal state By the universality of spreaded queries Q# (Proposition 4.8), 1 BlindCheck() checking the satisfability of a query Q boils down to compute Procedure Q#, and then check that it is a sharing equivalence: the fact that 2 for every node n do 3 canonic( ) the requirements on variables are modular to the blind sharing if n unde€ned then requirement is one of our main contributions. Indeed it is possible BuildEquivalenceClass(n) to check sharing equality in two phases: 4 Procedure BuildEquivalenceClass(c) (1) : building Q# and at the same time checking Blind Check 5 canonic(c) B c that it is a blind sharing equivalence, i.e. that it is homoge- 6 building(c) B true neous; 7 queue(c) B {c} (2) Variables Check: veri€ng that Q# is a sharing equivalence 8 while queue(c) is non-empty do by checking the conditions for free and bound variable 9 n queue(c).pop() nodes. B 10 for every parent m of n do Of course, the diculty is doing it in linear time, and it essentially 11 case canonic(m) of lies in the Blind Check. 12 unde€ned ⇒ BuildEquivalenceClass(m) 0 0 Œe rest of this part presents two algorithms, the Blind Check 13 c ⇒ if building(c ) then fail (Algorithm 1) and the Variables Check (Algorithm 2), with proofs of correctness and completeness, and complexity analyses. Œe 14 for every ∼neighbour m of n do second algorithm is actually straightforward. Be careful, however: 15 case canonic(m) of the algorithm for the Variables Check is trivial just because the 16 unde€ned ⇒ EnqueueAndPropagate(m, c) 0 0 subtleties of this part have been isolated in the previous section. 17 c ⇒ if c , c then fail

6 THE BLIND CHECK 18 building(c) B false

In this section we introduce the basic concepts for the Blind Check, 19 Procedure EnqueueAndPropagate(m, c) plus the algorithm itself. 20 case m , c of 0 0 0 0 Our algorithm is a simple adaptation of Paterson and Wegman’s, 21 Abs(m ) , Abs(c ) ⇒ create edge m ∼ c and it relies on the same key ideas in order to be linear. With respect 22 App(m1,m2) , App(c1,c2) ⇒ to PW original algorithm, our reformulation does not rely on their 23 create edges m1 ∼ c1 and m2 ∼ c2 notions of dead/alive nodes used to keep track of the nodes already 0 24 Var(l) , Var(l ) ⇒ () processed; in addition it is not destructive, i.e. it does not remove 25 Var() , Var() ⇒ () edges and nodes from the graph, hence it is more suitable for use 26 , ⇒ fail in computer tools where the λ-terms to be checked for equality need not be destroyed. Another contribution in this part is a formal 27 canonic(m) B c proof of correctness and completeness, obtained via the isolation 28 queue(c).push(m) of properties of program runs.

Intuitions for the Blind Check. Paterson and Wegman’s algorithm More speci€cally, the ideas behind Algorithm 1 are: is based on a tricky, linear time visit of the λ-graph. It addresses • Top-down recursive exploration: the algorithm can start on two main eciency issues: any node, not necessarily a root. However, when process- (1) Še spreaded query is quadratic: the number of pairs in ing a node n the algorithm €rst makes a recursive call on the spreaded query Q# can be quadratic in the size of the the parents of n that have not been visited yet. Œis is λ-graph. An equivalence class of cardinality n has indeed done to avoid the risk of reprocessing n later because of Ω(n2) pairs for the relation—this is true for every equiva- some new equality requests on n coming from a parent lence relation. Œis point is addressed by rather computing processed a‰er n. Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

• ‰ery edges: the query is represented through additional the algorithm propagates only O(k) query edges—this undirected query edges between nodes, and it is propagated is why the number of query edges is kept linear in on child nodes by adding further query edges. Œe query the number of the nodes (assuming that the original is propagated carefully, on-demand. Œe fully propagated query itself was linear). If instead one would propa- query is never computed, because, as explained, in general gate query edges before canonizing the class, then the its size is quadratic in the number of nodes. number of query edges may grow quadratically. • Canonic edges: when a node is visited, it is assigned a canonic node that is a representative of its sharing equiva- States. As explained, the algorithm needs to enrich λ-graphs lence class. Œis is represented via a directed canonic edge, with a few additional concepts, namely canonic edges, query edges, which is implemented as a pointer. building ƒags, queues, and execution stack, all grouped under the • Building ƒag: each node has a boolean “building” ƒag that notion of program state. is used only by canonic representatives and notes the state A state S of the algorithm is either Fail or a of construction of their equivalence class. When unde- €ned, it means that that node is not currently designated as canonic; when true, it means that the equivalence class (G, undir, canonic, building, queue, active) of that canonic is still being computed; when false, it signals that the equivalence class has been completely computed. where G is a λ-graph, and the remaining data structures have the • Failures and cycles: the algorithm fails in three cases. First, following properties: when it €nds two nodes in the same class that are not homo- geneous (Line 26), because then the approximation of Q# • Undirected query edges (∼): undir is a multiset of undi- that it is computing cannot be a blind sharing equivalence. rected query edges, pairing nodes that are expected to be Œe two other cases (on Line 13 and Line 17) the algorithm placed by the algorithm in the same sharing equivalence uses the fact that the canonic edge is already present to class. Undirected loops are admiŠed and there may be infer that it found a cycle up to Q#, and so, again Q# can- multiple occurrences of an undirected edge between two not be a blind sharing equivalence (please read again the nodes. More precisely, for every undirected edge between paragraph a‰er Proposition 4.7). n and m with multiplicty k in the state, both (n,m) and • Building a class: calling BuildEquivalenceClass(n) boils (m,n) belong with multiplicity k to undir. We denote by down to ∼ the binary relation over G such that n ∼ m i‚ the edge (1) collect without duplicates all the nodes in the intended (n,m) belongs to undir. sharing equivalence class of n, that is, the nodes re- • Canonic edges (c): nodes may have one additional canonic lated to n by a sequence of query edges. Œis is done by directed edge pointing to the computed canonical repre- the while loop at Line 14, that €rst collects the nodes sentative of that node. Œe partial function mapping each queried with n and then iterates on the nodes queried node to its canonical representative (if de€ned) is noted with them. Œese nodes are inserted in a queue; c. We then write c(n) = m if the canonical of n is m, and (2) set n as the canonical element of its class, by seŠing the c(n) = undefined otherwise. By abuse of notation, we canonical edge of every node in the class (including also consider c a binary relation on G, where n c m i‚ n) to n; c(n) = m. (3) propagate the query on the children (in case n is a Lam • Building ƒags (b): nodes may have an additional boolean or a App node), by adding query edges between the ƒag building that signals whether an equivalence class corresponding children of every node in the class and has or has not been constructed yet. Œe partial function their canonic. mapping each node to its building ƒag (if de€ned) is noted (4) Pushing a node in the queue, seŠing its canonic, and b. We then write b(n) = true | false if the building ƒag propagating the query on its children is done by the of n is de€ned, and b(n) = undefined otherwise. procedure EnqueueAndPropagate. • ‰eues (q): nodes have a queue data structure that is used • Linearity: let us now come back to the two eciency issues only on canonic representatives, and contains the nodes of we mentioned before: the class that are going to be processed next. Œe partial – Merging classes: the recursive calls are done in order to function mapping each node to its queue (if de€ned) is guarantee that when a node is processed all the query noted q. edges for its sharing class are already available, so • Active Calls (active): a program state contains informa- that the class shall not be extended nor merged with tion on the execution stack of the algorithm, including the other classes later on during the visit of the λ-graph. active procedures, local variables, and current execution – Propagating the query: the query is propagated only line. We leave the concept of execution stack informal; a‡er having set the canonics of the current sharing we only de€ne more formally active, which records the equivalence class. To explain, consider a class of k order of visit of equivalence classes that are under con- nodes, which in general can be de€ned by Ω(k2) query struction, and that is essential in the proof of complete- edges. Note that a‰er canonization, the class is rep- ness of the algorithm. active is an abstraction of the resented using only k − 1 canonic edges, and thus implicit execution stack of active calls to the procedure Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

BuildEquivalenceClass where only (part of) the acti- • Blind bisimulation upto: in all reachable states, c is a blind vation frames for BuildEquivalenceClass(c) are repre- bisimulation upto (∼ ∪ =). sented. Formally, active is simply a sequence of nodes of • Undirected query edges approximate the spreaded query: in # the λ-graph, and active = [c1,...,cK ] if and only if: all reachable states, Q ⊆ ∼ ⊆ Q . – Active: BuildEquivalenceClass(c1),..., • Še canonic assignment respects the ∼ constraints: in all ∗ BuildEquivalenceClass(cK ) are exactly the reachable states, c ⊆ ∼ calls to BuildEquivalenceClass that are currently • Eventually, all query edges are visited: in all €nal states, all active, i.e. have been called before S, but have not yet query edges are subsumed by the canonic assignment, i.e. returned; ∼ ⊆ c∗. ≤ – Call Order: for every 0 < i < j K, In every reachable state, we de€ne =c as the equivalence relation BuildEquivalenceClass(ci ) was called before over the λ-graph that equates two nodes whenever their canonic BuildEquivalenceClass(cj ). representatives are both de€ned and coincide. Œe lemmas above Moreover we introduce the following concepts: basically state that during the execution of the algorithm, =c is a blind sharing equivalence up to the query edges, and that ∼ can • Program transition: the change of state caused by the execu- indeed be seen as an approximation of the spreaded query Q#. At tion of a piece of code. For the sake of readability, we avoid the end of the algorithm, ∼ and c actually represent the same (up ∗ a technical de€nition of transitions; roughly, a transition is to the (·) closure) relation =c, which is then exactly the spreaded the execution of a line of code, as they appear numbered in query: the algorithm itself. When the line is a while loop, a tran- sition is an iteration of the body, or the exit from the loop; Proposition 6.1 (Correctness). In every €nal state Sf : # when the line is a if-then-else, a transition is entering • Succint representation: (=c) = (Q ), one of the branches according to the condition; and so on. • Blind check: Q# is homogeneous, and therefore a blind shar- • Fail state: Fail is the state reached a‰er executing a fail ing equivalence. instruction; it has no aŠributes and no transitions. • S : a non-F state with the following at- Completeness. Completeness is the fact that whenever Algo- Initial state 0 ail # tributes: rithm 1 fails, Q is not a blind sharing equivalence. Recall that – Initial ∼edges: simply the initial query, i.e. ∼ B Q. the algorithm can fail only while executing the following three – Initial assignments: c(n), b(n), and q(n) are unde€ned lines of code: for every node n of G. • On Line 13 during BuildEquivalenceClass, when a node – Initial transition: the €rst transition is a call to n is being processed and it has a parent whose equivalence BlindCheck(). class is still being built; • Program run: a sequence of program states starting from • On Line 17 during BuildEquivalenceClass, when a node S0 obtained by consecutive transitions. n is being processed and it has a ∼neighbour belonging to • Reachable: a state which is the last state of a program run. a di‚erence equivalence class; • Failing: a reachable state that transitions to Fail. • On Line 26 during EnqueueAndPropagate(m,c), when the • Final: a non-Fail reachable state that has no further transi- algorithm is trying two relate the nodes m and c which are tions. not homogeneous. Details about how the additional structures of enriched λ-graphs While in the laŠer case (Line 26) the failure of the homogeneous are implemented are given in Sect. 6.2, where the complexity of the condition is more evident, in the €rst two cases it is more subtle. In algorithm is analysed. fact, when the homogeneous condition fails on Line 13 and Line 17 it is not because we explicitly found two related nodes that are not homogeneous, but because Q# does not satisfy an indirect property 6.1 Correctness and Completeness that is necessary for Q# to be homogenous (see below). In these Here we prove that Algorithm 1 correctly and completely solves cases the algorithm fails early, even though it has not visited yet the blind sharing equivalence problem, that is, it checks whether the actual pair of nodes that are not homogeneous. the spreaded query is a blind sharing equivalence. To justify the early failures we use active, which basi- Œe proofs rely on a number of general properties of program cally records the equivalence classes that the algorithm is build- runs and reachable states. Œe full statements and proofs are in ing in a given state, sorted according to the order of calls to Appendix D, grouped according to the concepts that they analyze. BuildEquivalenceClass. Nodes in active respect a certain strict order: if active = [c1,...,cK ], then c1 ≺ c2 ≺ ... ≺ cK (Œeo- Correctness. We prove that whenever Algorithm 1 terminates rem D.27 in the Appendix), where n ≺ m implies that the equiv- # successfully with €nal state Sf from an initial query Q, then Q alence class of n is a child of the one of m in the quotient graph. is homogeneous, and therefore a blind sharing equivalence. Addi- Œe algorithm fails on Line 13 and Line 17 because it found a cyclic tionally, we show that the canonic assignment is a succint repre- chain of nodes related by ≺, therefore €nding a cycle in the quo- # # sentation of Q , i.e. that for all nodes n,m: n Q m if and only if tient graph. By Œeorem 4.4, there cannot exist any blind sharing n and m have the same canonic assigned in Sf . Œe following are equivalence containing the initial query in this case. the most important properties to prove correctness: Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

Proposition 6.2 (Completeness). If Algorithm 1 fails, then Q# is of a node n, we prove that the ∼neighbours of n do not change not a blind sharing equivalence, and therefore by the blind universality a‰er the parents of n are visited. Finally, recall that the algorithm of Q# (Šeorem 4.7), there does not exist any blind sharing equivalence parsimoniously propagates query edges only between a node and containing the initial query Q. its canonic: as a consequence, in every reachable state |undir| is linear in the size of Q and the number of nodes in the λ-graph. 6.2 Linearity Let |G| denote the size of a λ-graph G, i.e. the number of nodes In this section we show that Algorithm 1 always terminates, and and edges of G. Œen: it does so in time linear in the size of the λ-graph and the initial Proposition 6.4 (Linear termination). Let S0 be an initial query. state of the algorithm, with a query Q over a λ-graph G. Šen the Low-level assumptions. In order to analyse the complexity of the Blind check terminates in a number of transitions linear in |G| and algorithm we have to spell out some details about an hypothetical |Q|. implementation on a RAM of the data structures used by the Blind check. 7 THE VARIABLES CHECK • Nodes: since Line 2 of the algorithm needs to iterate on all Our second algorithm (Algorithm 2) takes in input the output of nodes of the λ-graph, we assume an array of pointers to the Blind check, that is, a blind sharing equivalence on a λ-graph G all the nodes of the graph. represented via canonic edges, and checks whether the Var-nodes • Directed edges: these edges—despite being directed—have of G satisfy the variables conditions for a sharing equivalence—free to be traversed in both directions, typically to recurse over variable nodes at line 7, and bound variable nodes at line 9. the parents of a node. We then assume that every node has Œe Variables check is based on the fact that to compare a node an array of pointers to its parents. with all those in its class it is enough to compare it with the canon- • Undirected query edges: query edges are undirected and ical representative of the class—note that this fact is used twice, for are dynamically created; in addition, the algorithm needs the Var-nodes and for their binders. Œe check fails in two cases, # to iterate on all ∼neighbours of a node. In order to obtain either when checking a free variable node (in this case Q is not the right complexity, every node simply maintains a linked an open relation) or when checking a bound variable node (in this # list of its ∼neighbours, in such a way that when a new case Q is not closed under  ). undirected edge (n,m) is created, then n is pushed on the list of ∼neighbours of m, and m on the one of n. Algorithm 2: Variables Check • Canonical assignment is obtained by a pointer to a node Data: canonic(·) representation of Q# (possibly undefined) in the data structure for nodes. Result: is Q# a sharing equivalence? • Building ƒags are just implemented via a boolean on each node. 1 Procedure VarCheck() • ‰eues do not need to be recorded in the data structure for 33 foreach Var-node v do nodes, as they can be equivalently coded as local variables. 4 w ← canonic(v) Let us call atomic the following operations performed by the 5 if v , w then check: €nding the €rst node, €nding the next node given the previ- 77 if binder(v) or binder(w) is undefined ous node, €nding the €rst parent of a node, €nding the next parent 8 then fail// free Var-nodes of a node given the previous parent, checking and seŠing canonics, 9 else if checking and seŠing building ƒags, geŠing the next query edge on canonic(binder(v)) , canonic(binder(w)) a given node, traversing a query edge, adding a query edge between then fail// bound Var-nodes two nodes, pushing to a queue, and popping an element o‚ of a 10 end queue. 11 end Lemma 6.3 (Atomic operations are constant). Še atomic operations of the Blind check are all implementable in constant time on a RAM. Theorem 7.1 (Correctness & completeness of the Variables check). Termination and linearity of the algorithm are proved via a Let Q be a query over a λ-graph G passing the Blind check, and let c global estimation of the number of transitions in a program run. be the canonic assignment produced by that check. Œe dicult part is to estimate the number of transitions execut- • (Completeness) If the Variables check fails then there are no BuildEquivalenceClass ing lines of , since it contains multiple sharing equivalences containing Q, nested loops. First of all, we prove that in every program run • (Correctness) otherwise =c is the smallest sharing equiva- BuildEquivalenceClass is called at most once for each node. lence containing Q. Œen, also the body of the while loop on Line 8 is executed in Moreover, the Variables check clearly terminates in time linear in the total at most once for each node, because we prove that in a pro- size of G. gram run each node is enqueued at most once. Since the parents of a node do not change during a program run, the loop on Line 10 Composing Œeorem 6.1, Œeorem 6.2, Œeorem 6.4, and Œeo- is not problematic. As for the loop on Line 14 on the ∼neighbours rem 7.1, we obtain the second main result of the paper. Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

Theorem 7.2 (Sharing e€ality is linear). Let Q be a query [10] Guy E. Blelloch and John Greiner. 1995. Parallelism in Sequential Functional over a λ-graph G. Še sharing equality algorithm obtained by com- Languages. In Proceedings of the seventh international conference on Functional programming languages and computer architecture, FPCA 1995, La Jolla, California, bining Algorithm 1 and Algorithm 2 runs in time linear in the sizes USA, June 25-28, 1995. 226–237. hŠps://doi.org/10.1145/224164.224210 of G and Q, and it succeeds if and only if there exists a sharing equiv- [11] Stefanus Cornelis Christo‚el Blom. 2001. Term Graph . Syntax and Q semantics. Ph.D. Dissertation. Vrije Universiteit Amsterdam. alence containing . Moreover, if it succeeds, it outputs a concrete [12] Robert S Boyer and Jay S Moore. 1972. Œe sharing of structure in theorem- (and linear) representation of the smallest such sharing equivalence. proving programs. Machine intelligence 7 (1972), 101–116. [13] Adam L. Buchsbaum, Haim Kaplan, Anne Rogers, and Je‚ery R. Westbrook. 1998. Finally, composing with the sharing equality theorem (Œeo- A New, Simpler Linear-Time Dominators Algorithm. ACM Trans. Program. Lang. rem 4.9) one obtains that the algorithm indeed tests the equality of Syst. 20, 6 (1998), 1265–1296. hŠps://doi.org/10.1145/295656.295663 [14] Christophe Calves.` 2013. Unifying Nominal Uni€cation. In 24th International the readbacks of the query, as expected. Conference on Rewriting Techniques and Applications, RTA 2013, June 24-26, 2013, Eindhoven, Še Netherlands. 143–157. hŠps://doi.org/10.4230/LIPIcs.RTA.2013. 143 8 CONCLUSIONS [15] Christophe Calves` and Maribel Fernandez.´ 2010. Œe First-Order Nominal Link. We presented the €rst linear-time algorithm to check the sharing In Logic-Based Program Synthesis and Transformation - 20th International Sympo- sium, LOPSTR 2010, Hagenberg, Austria, July 23-25, 2010, Revised Selected Papers. equivalence of λ-graphs. Following our development of the theory 234–248. hŠps://doi.org/10.1007/978-3-642-20551-4 15 of sharing equality, we split the algorithm in two parts: a €rst part [16] Christophe Calves` and Maribel Fernandez.´ 2010. Matching and alpha-equivalence check for nominal terms. J. Comput. Syst. Sci. 76, 5 (2010), 283–301. hŠps: performing a check on the DAG structure of λ-graphs, and the //doi.org/10.1016/j.jcss.2009.10.003 second part checking the additional requirements on variables and [17] Arthur Chargueraud.´ 2012. Œe Locally Nameless Representation. J. Autom. scopes. Œe paper is accompanied by a technical report with formal Reasoning 49, 3 (2012), 363–408. hŠps://doi.org/10.1007/s10817-011-9225-2 [18] Andrei P. Ershov. 1958. On Programming of Arithmetic Operations. Commun. proofs of correctness, completeness, and termination in linear time. ACM 1, 8 (1958), 3–9. hŠps://doi.org/10.1145/368892.368907 Œe motivation for this work stemmed from our previous works [19] Harold N. Gabow. 1990. Data Structures for Weighted Matching and Nearest on the evaluation of λ-terms: abstract machines can reduce a λ- Common Ancestors with Linking. In Proceedings of the First Annual ACM-SIAM Symposium on Discrete Algorithms, 22-24 January 1990, San Francisco, California, term to normal form in time bilinear in the size of the term and the USA. 434–443. hŠp://dl.acm.org/citation.cfm?id=320176.320229 number of evaluation steps, if λ-terms are represented in memory [20] Eiichi Goto. 1974. Monocopy and associative algorithms in extended Lisp. Technical report TR 74–03. University of Tokyo. as λ-graphs. When combining bilinear evaluation through abstract [21] Clemens Grabmayer and Jan Rochel. 2014. Maximal sharing in the Lambda machines and the sharing equality algorithm presented here, one calculus with letrec. In Proceedings of the 19th ACM SIGPLAN international obtains a bilinear algorithm for conversion. As a consequence, conference on Functional programming, Gothenburg, Sweden, September 1-3, 2014. 67–80. hŠps://doi.org/10.1145/2628136.2628148 αβ-conversion can be implemented with only bilinear overhead. [22] J. Hopcro‰ and R. Karp. 1971. A Linear Algorithm for Testing Equivalence of Finite Automata. Technical Report 0. Dept. of Computer Science, Cornell U. ACKNOWLEDGMENTS [23] John Lamping. 1990. An Algorithm for Optimal Lambda Calculus Reduction. In Conference Record of the Seventeenth Annual ACM Symposium on Principles of Œis work has been partially funded by the ANR JCJC grant Programming Languages, San Francisco, California, USA, January 1990. 16–30. hŠps://doi.org/10.1145/96709.96711 COCA HOLA (ANR-16-CE40-004-01) and the COST Action EU- [24] Jordi Levy and Mateu Villaret. 2010. An Ecient Nominal Uni€cation Algorithm. Types CA15123 STSM#43345. In Proceedings of the 21st International Conference on Rewriting Techniques and Applications, RTA 2010, July 11-13, 2010, Edinburgh, Scoˆland, UK. 209–226. hŠps://doi.org/10.4230/LIPIcs.RTA.2010.209 REFERENCES [25] Alberto Martelli and Ugo Montanari. 1977. Œeorem Proving with Structure [1] Beniamino AccaŠoli. 2018. Proof Nets and the Linear Substitution Calculus. In Sharing and Ecient Uni€cation. In Proceedings of the 5th International Joint Šeoretical Aspects of Computing - ICTAC 2018 - 15th International Colloquium, Conference on Arti€cial Intelligence. Cambridge, MA, USA, August 22-25, 1977. 543. Stellenbosch, South Africa, October 16-19, 2018, Proceedings. 37–61. hŠps://doi. hŠp://ijcai.org/Proceedings/77-1/Papers/096.pdf org/10.1007/978-3-030-02508-3 3 [26] Alberto Martelli and Ugo Montanari. 1982. An Ecient Uni€cation Algorithm. [2] Beniamino AccaŠoli and Bruno Barras. 2017. Environments and the complexity ACM Trans. Program. Lang. Syst. 4, 2 (1982), 258–282. hŠps://doi.org/10.1145/ of abstract machines. In Proceedings of the 19th International Symposium on 357162.357169 Principles and Practice of Declarative Programming, Namur, Belgium, October 09 - [27] M.S. Paterson and M.N. Wegman. 1978. Linear uni€cation. J. Comput. System 11, 2017. 4–16. hŠps://doi.org/10.1145/3131851.3131855 Sci. 16, 2 (1978), 158 – 167. hŠps://doi.org/10.1016/0022-0000(78)90043-0 [3] Beniamino AccaŠoli and Claudio Sacerdoti Coen. 2015. On the Relative Use- [28] Zhenyu Qian. 1993. Linear Uni€cation of Higher-Order PaŠerns. In TAP- fulness of Fireballs. In 30th Annual ACM/IEEE Symposium on Logic in Computer SOFT’93: Šeory and Practice of So‡ware Development, International Joint Con- Science, LICS 2015, Kyoto, Japan, July 6-10, 2015. 141–155. hŠps://doi.org/10. ference CAAP/FASE, Orsay, France, April 13-17, 1993, Proceedings. 391–405. 1109/LICS.2015.23 hŠps://doi.org/10.1007/3-540-56610-4 78 [4] Beniamino AccaŠoli and Giulio Guerrieri. 2017. Implementing Open Call-by- [29] Laurent Regnier. 1992. Lambda-calcul et reseaux´ . PhD thesis. Univ. Paris VII. Value. In Fundamentals of So‡ware Engineering - 7th International Conference, [30] Manfred Schmidt-Schauß, Conrad Rau, and David Sabel. 2013. Algorithms for FSEN 2017, Tehran, Iran, April 26-28, 2017, Revised Selected Papers. 1–19. hŠps: Extended Alpha-Equivalence and Complexity. In 24th International Conference //doi.org/10.1007/978-3-319-68972-2 1 on Rewriting Techniques and Applications, RTA 2013, June 24-26, 2013, Eindhoven, [5] Beniamino AccaŠoli and Ugo Dal Lago. 2012. On the Invariance of the Unitary Še Netherlands. 255–270. hŠps://doi.org/10.4230/LIPIcs.RTA.2013.255 Cost Model for Head Reduction. In 23rd International Conference on Rewriting [31] Christian Urban, Andrew M. PiŠs, and Murdoch Gabbay. 2003. Nominal Uni€- Techniques and Applications (RTA’12) , RTA 2012, May 28 - June 2, 2012, Nagoya, caiton. In Computer Science Logic, 17th International Workshop, CSL 2003, 12th Japan. 22–37. hŠps://doi.org/10.4230/LIPIcs.RTA.2012.22 Annual Conference of the EACSL, and 8th Kurt Godel¨ Colloquium, KGC 2003, Vi- [6] Beniamino AccaŠoli and Ugo Dal Lago. 2014. Beta reduction is invariant, indeed. enna, Austria, August 25-30, 2003, Proceedings. 513–527. hŠps://doi.org/10.1007/ In Joint Meeting of the Twenty-Šird EACSL Annual Conference on Computer 978-3-540-45220-1 41 Science Logic (CSL) and the Twenty-Ninth Annual ACM/IEEE Symposium on Logic [32] C.P. Wadsworth. 1971. Semantics and Pragmatics of the Lambda-calculus. Uni- in Computer Science (LICS), CSL-LICS ’14, Vienna, Austria, July 14 - 18, 2014. versity of Oxford. hŠps://books.google.it/books?id=kl1QIQAACAAJ 8:1–8:10. hŠps://doi.org/10.1145/2603088.2603105 [7] John Allen. 1978. Anatomy of LISP. McGraw-Hill, Inc., New York, NY, USA. [8] Stephen Alstrup, Dov Harel, Peter W. Lauridsen, and Mikkel Œorup. 1999. Dominators in Linear Time. SIAM J. Comput. 28, 6 (1999), 2117–2132. hŠps: //doi.org/10.1137/S0097539797317263 [9] Zena M. Ariola and Jan Willem Klop. 1996. Equational Term Graph Rewriting. Fundam. Inform. 26, 3/4 (1996), 207–240. hŠps://doi.org/10.3233/FI-1996-263401 Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

A RELATIONS ON GRAPHS – Case d =  . Œen m0 = Abs(m00) for some m00.

τ 0·  In this section we are going to introduce several kinds of binary Clearly m  m00.

relations on nodes of the λ-graph. Some are standard (like – Cases d = or d = . Œen m0 = App(m0 ,m0 ,) for bisimula-   1 2 0 0 tions, Œeorem A.2); others (like propagated relations, Œeorem A.4) τ · τ · some m0 ,m0 . Clearly m m0 and m m0 .  are auxiliary concepts that we will later connect to the readback 1 2 1 2 from λ-graphs to λ-terms (Œeorem 3.9). Given a relation R, we de€ne its propagation R⇓ as the smallest A.1 Bisimulations propagated relation containing R:

Definition A.1 (Blind Bisimulation). Let G be a λ-graph, and Definition A.7 (Propagation R⇓). Let R be a binary relation

 R be a binary relation over the nodes of G. R is a blind bisimulation λ G  R⇓ R  over the nodes of a -graph . Še propagation of is obtained if and only if it is homogeneous and closed under the rules  by closing R under the rules  of Figure 3.  of Figure 3. Proposition A.8 (Inversion). Let R be a relation over the nodes G λ R ⇓

Definition A.2 (Bisimulation). Let be a -graph, and be a of a λ-graph G, and n,m be nodes of G. n R m if and only there binary relation over the nodes of G. R is a bisimulation if and only 0 0 0 0 0 τ

 exists a trace τ and nodes n , m such that n R m , n n, and if it is homogeneous and closed under the rules   of m0 τ m. Figure 3. Proof. Notation A.3. We denote by B a generic bisimulation. (⇒) Assume n R⇓ m. We proceed by induction on the inductive de€nition of R⇓:

A.2 Propagation ⇓ – Base case: n R⇓ m because n R m. Conclude by

 Definition A.4 (Propagated Relation). We call propagated a considering n0 = n, m = m0, and τ 0 = ϵ.  ⇓ relation that is closed under the rules  of Figure 3. – Cases : assume that n R m because 00 ⇓ 00 00 00 Œe following auxiliary propositions (Œeorem A.5 and Œeo- App(n,n ) R App(m,m ) for some n ,m . By

0 τ 00  rem A.6) connect propagated relations and paths on graphs. i.h. there exists τ such that n  App(n,n ) and τ · τ · m0 τ App(m,m00). Clearly n0 n and m0 m. Proposition A.5 (Trace Propagation). Let R be a propagated – Case  . Symmetric to the case above. relation over the nodes of a λ-graph G. Let n,m be nodes such that  R⇓ Abs( ) R⇓ n R m τ n τ n0 m τ m0 – Case : assume that n m because n , and let be a trace such that and . Šen 0 τ 0 R 0 Abs(m). By i.h. there exists τ such that n Abs(n) n m .   · · and m0 τ Abs(m). Clearly n0 τ n, and m0 τ m. Proof. We prove n0 R m0 by structural induction on the trace (⇐) Note that n R m implies n R⇓ m, and conclude by Œeo- τ : rem A.5. • Case τ = ϵ. Clearly n0 = n and m0 = m, and we use the



  hypothesis n R m.  τ · 0 τ · 0 • Case (τ · ). Assume n n and m m . By inver- A.2.1 Propagation vs. Bisimulation. Œe main theorem of this 00 00 τ 0 00

sion, there exist n ,m such that n App(n ,n ) and section is the universality of the propagation R⇓ (Œeorem A.13), τ 0 00  0 00 0 00 m App(m ,m ). By i.h. App(n ,n ) R App(m ,m ), which states that in order to look for a bisimulation containing a 0 0 which implies n R m by . given relation R over a λ-graph, it suces to check its propagation • Case (τ ·  ). Symmetric to the case above. R⇓ R R⇓   : whenever a bisimulation containing exists at all, then is

 τ · τ · • Case (τ · ). Assume n n0 and m m0. By inver- also a bisimulation (as we will see, one actually needs an additional sion, n τ Abs(n0) and m τ Abs(m0). By i.h. Abs(n0) R condition on R). Abs(m0), which implies n0 R m0 by  .  Before proving Œeorem A.13 we need some additional proposi- tions and de€nitions. Many of the following results depends on the R Proposition A.6 (Trace E€ivalence). Let be a blind bisim- following proposition, which shows that well-behaved propagated , ulation over the nodes of a λ-graph G. Let n m be nodes such that relations cannot relate both a node and their children: n R m. Šen for every trace τ , n τ if and only if m τ . Proposition A.9 (No Triangles). Let R be a blind bisimulation Proof. Assume that n R m holds. We proceed by structural 0 over the nodes of a λ-graph G. Let n,m,m nodes of G and τ a non- induction on τ : τ empty trace. If n R m and m m0, then n R m0 can not hold. • Empty Trace: Clearly n ϵ and m ϵ . 0 • Trace Cons. Let τ B τ · d, and assume without loss of Proof. Assume that n R m0, and obtain a contradiction. In- τ τ generality that n . We need to prove that m . tuitively, we are going to show that the trace τ can be iterated 0 0 By inversion, n τ n0 for some n0, and by i.h. m τ m0 arbitrarily many times starting from m, contradicting the hypothe- for some m0. From Œeorem A.5 we obtain n0 R m0, where sis that the λ-graph is €nite and acyclic. n0 and m0 are homogenous because by hypothesis R is Œe contradiction will follow by iterating the following construc- homogeneous. We proceed by cases on d: tion: Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

• From the hypotheses that n R m and m τ m0 it follows Proposition A.12. Let B be a bisimulation over the nodes of a from Œeorem A.6 that n τ n0 for some n0. From the hy- λ-graph G, and let Q be a query such that Q ⊆ B. Šen Q⇓ is a pothesis that n R m together with n τ n0 and m τ m0, it bisimulation. follows that n0 R m0 by Œeorem A.5. B Q Q ⊆ B • From the hypothesis that n R m0 and n τ n0 it follows Proof. Let be a bisimulation, and let such that . In Q⇓ Q⇓ from Œeorem A.6 that m0 τ m00 for some m00. From the order to prove that is a bisimulation, it suces to show that hypothesis that n R m0 together with n τ n0 and m0 τ m00, is homogeneous and closed under  . it follows that n0 R m00 by Œeorem A.5. • Homogeneous. Q⇓ is homogeneous because Q⇓ is con- Œe construction above can be repeated by using the new hy- tained in B (since Q ⊆ B and B is propagated), and B is 0 R 0 0 τ 00 0 R 00 homogeneous. potheses n m , m m , and n m . Iterating the construc- ⇓ 0 tion arbitrarily many times yields a sequence of nodes m, m0, m00, • Closed under  . Let Var(l) Q Var(l ): we need to ⇓ 0 ⇓ ⇓ ...such that m τ m0, m0 τ m00,... show that l Q l . Since Q is contained in B, Var(l) Q 0 0 Œis contradicts the property of λ-graphs to be €nite and acyclic. Var(l ) implies Var(l) B Var(l ), and since B is closed 0  under  we obtain l B l . By Var(l) Q⇓ Var(l 0) and Œeorem A.8, there exists ⇓ Œe universality of R (Œeorem A.13) does not hold for every a trace τ and nodes n Q m such that n τ Var(l) and relation R, but an additional requirement for R is necessary: we m τ Var(l 0). Since Q is a query, n τ Var(l) crosses l and will require R to be a query (Œeorem 4.5), i.e. relating only root m τ Var(l 0) crosses l 0. We conclude with l Q⇓ l 0 by Œe- nodes. orem A.11.  Œe following is a simple property of every path crossing a node: We can €nally prove: Proposition A.10. Let n,m,p be nodes of a λ-graph G, and τ a ⇓ trace such that n τ m crosses p. Šen there exists a trace τ 0 such that Theorem A.13 (Universality of Q ). Let Q be a query over the ⇓ τ 0 λ G Q p m. nodes of a -graph . is a bisimulation if and only if there exists a bisimulation containing Q. Proof. By induction on the de€nition of “n τ m crosses p”: Proof. Clearly if Q⇓ is a bisimulation, then Q⇓ is the required 0 • If m = p, conclude with τ B ϵ. bisimulation containing Q. To prove the other implication, just use 00 • If τ = (τ 00 · d) such that n τ crosses p, use the i.h. to Œeorem A.12.  obtain τ 0, and conclude with (τ 0 · d).  A.3 Equivalence Relations Œe following technical proposition is the inductive variant nec- In this section, we are going to de€ne the spreading Q# of Q (Œe- essary to prove Œeorem A.12. orem A.17), which is similar to the propagation Q⇓ but also an Proposition A.11. Let R be a blind bisimulation over the nodes equivalence relation. Œe main result will be Œeorem A.20, in # ⇓ of a λ-graph G, and let Q a relation such that Q ⊆ R. Let n,m be which we prove that Q is a bisimulation if and only if Q is. ∗ nodes such that n Q m, and τ a trace such that n τ crosses l and We €rst introduce the reƒexive symmetric transitive closure (·) : τ 0 R 0 Q⇓ 0 m crosses l . Šen l l implies l l . Definition A.14 (Reflexive Symmetric Transitive Closure).

R∗

R 0 We denote with the reƒexitive symmetric transitive (rst) closure Proof. Assume l l . We proceed by structural induction on  of a relation R, i.e. the relation obtained by closing R under the rules the predicates “n τ crosses l” and “m τ crosses l 0”: • of Figure 3. • Case n τ l and m τ l 0. By Œeorem A.8 l Q⇓ l 0, and we   conclude. We now li‰ some previously de€ned notions to the rst closure. • Case n τ l and m τ m0 , l 0: this case is not possible, i.e. Œe rst closure preserves homogeneity: τ 0 0 we derive a contradiction. Since m m crosses l , we Proposition A.15. Let R be a relation. R is homogeneous if and 0 0 τ 0 0 obtain by Œeorem A.10 a trace τ such that l m , that only if R∗ is homogeneous. is non-empty because l 0 , m0. Note that by Œeorem A.8 R∗ R

it holds that l Q⇓ m0, that implies l R m0 (because R Proof. Clearly if is homogeneous, then also is homoge- R ⊆ R∗  contains Q, and R is propagated). Obtain a contradiction neous because . For the other direction, it suces to note • 0 0 τ 0 0 0 that the equivalence rules preserve homogeneity.  by Œeorem A.9, using l R l , l m , and l R m .   • Case n τ n0 , l and m τ l 0: symmetric to the case above. For homogeneous relations, closure under structural rules is 0 0 • Case τ = (τ 0 · d), n τ ·d n0 , l and m τ ·d m0 , l 0. It holds preserved by equivalence rules:

τ 0 τ 0 0 that n crosses l and m crosses l , and use the i.h. to Proposition A.16. Let R be an homogeneous relation, and r ∈ conclude.   ∗ { , ,  ,  }. If R is closed under r, then R is closed under r.

Œe previous result implies that whenever a query Q is contained Proof. We only consider the case when r is  ; the proofs for in any bisimulation, then Q⇓ is itself a bisimulation: the other rules are similar. Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

Assume that R is closed under r, and let Var(l) R∗ Var(l 0): A.4 Sharing Equivalences ∗ 0 we need to show that l R l . We proceed by induction on the In this section we introduce (Œeorem A.22), ∗ sharing equivalences inductive de€nition of R : which are binary relations on the λ-graph that capture the equiv- • Base case: Var(l) R∗ Var(l 0) because Var(l) R Var(l 0). alence of di‚erent sharings of subgraphs. Œey require the new By the hypothesis that R is closed under  it follows that concept of open relations, relations that do not identify distict free l R l 0, which implies l R∗ l 0. variable nodes: ∗ 0 0 • Case • : Var(l) R Var(l ) because Var(l) = Var(l ).

Definition A.21 (Open). We call a relation R open when n R m  0 R∗ 0 •

Œen l = l , and we conclude with l l by the rule . implies n = m for every free variable nodes n and m. • Case : Var(l) R∗ Var(l 0) because Var(l 0) R∗ Var( l). By i.h.l 0 R∗ l, and we obtain l R∗ l 0 by the rule . Definition A.22 (Sharing E€ivalence). A sharing equiva- • Case : Var(l) R∗ Var(l 0) because Var(l) R∗ n and lence is an open bisimulation that is also an equivalence relation. ∗ 0 n R Var( l ) for some node n. Since R is homogeneous, Œe main result of the section is the universality of Q# (Œe- ∗ by Œeorem A.15 R is homogeneous too, and therefore orem A.26), stating that Q# is a sharing equivalence whenever 00 00 ∗ 00 n = Var(l ) for some l . By i.h. we obtain l R l and there exists a sharing equivalence containing Q. Some auxiliary 00 ∗ 0 ∗ 0 l R l , and we conclude with l R l by the rule .  propositions follow.  We €rst li‰ the open property to the rst closure: We introduce the spreading R# of a relation R: Proposition A.23. Let R be an homogeneous relation over the

Definition A.17 (R#). Let R be a binary relation over the nodes nodes of a λ-graph. R is open if and only if R is open.  # of a λ-graph. R is obtained by closing R under the rules • R∗ R R ⊆ R∗  Proof. If is open, then also is open because .

 ∗

 of Figure 3. Assume now that R is open, and that n R m for n,m free   ⇓ ∗ # variable nodes. We proceed by induction on the inductive de€nition Clearly (R ) ⊆ (R ) for every relation R. Œe converse may ∗ of R . Œe base case and the cases • are easy; let us discuss in principle not hold, as the equivalence rules may not commute the case . Assume that n R∗ m because n R∗ p and p R∗ m with the rules of the spreading: for example, the rst closure of a for some node p. Since R is homogeneous, by Œeorem A.15 R∗ is spreaded relation may not be spreaded. But surprisingly this is not homogeneous too, and therefore p is a free variable node as well. By the case, under the hypothesis that R⇓ is homogeneous: i.h. we obtain n = p and p = m, and we conclude with n = m.  R Proposition A.18. Let be a binary relation over the nodes of a Q Q⇓ ⇓ # ⇓ ∗ Corollary A.24. Let be a query such that is homogeneous. λ-graph. If R is homogeneous, then R = (R ) . ⇓

Q is open if and only if Q# is open. Proof. Note that (R⇓)∗ ⊆ (R#), and that (R⇓)∗ is closed under

∗ ⇓ the rules • by the de€nition of (·) . It remains to show Theorem A.25. Let Q be a query over a λ-graph G. Q is an open ⇓ ∗    bisimulation if and only if Q# is a sharing equivalence. that (R ) is also closed under the rules  which follows from Œeorem A.16.  Proof. By Œeorem A.20 and Œeorem A.24.  # We li‰ Œeorem A.15 to R#: Theorem A.26. Let Q be a query over a λ-graph. Q is a sharing equivalence if and only if there exists a sharing equivalence containing Proposition A.19. Let R be a binary relation over the nodes of a Q. λ-graph. R# is homogeneous if and only if R⇓ is homogeneous. Proof. Œe implication from le‰ to right is trivial. Œe other implication follows from Œeorem A.25 and Œeorem A.13. Proof. Clearly if R# is homogeneous, then also R⇓ is homo-  ⇓ geneous because (R ) ⊆ (R#). For the other direction, assume Proof of (Theorem 4.7). Let Q be a query. If there exists a blind that R⇓ is homogeneous: by Œeorem A.15 it follows that (R⇓)∗ is sharing equivalence ≡ containing Q then: homogeneous, and we can conclude by Œeorem A.18.  (1) Še spreaded query Q# is contained in ≡, i.e. Q# ⊆ ≡. (2) Q# is the smallest blind sharing equivalence containing Q. We €nally prove: Proof. ⇓ (1) By the de€nition of spreading and blind sharing equiva- Theorem A.20. Let Q be a query over a λ-graph G. Šen Q is a lence. bisimulation if and only if Q# is a bisimulation. Q# Proof. (2) is a blind sharing equivalence because it is contained ⇓ by hypothesis in a blind sharing equivalence, and there- (⇒) Assume that Q is a bisimulation; in order to prove that fore it is homogeneous. Q# is the smallest blind sharing # # Q is a bisimulation, it suces to show that Q is homo- equivalence by the de€nition of spreading. geneous and closed under . Œe former follows from   Œeorem A.18 and Œeorem A.19. Œe laŠer follows from Œeorem A.18 and Œeorem A.16. Proof of (Theorem 4.8). Let Q be a query over a λ-graph G. If (⇐) Assume that Q# is a bisimulation. Œen Q⇓ is a bisimula- there exists a sharing equivalence ≡ containing Q, then the spreaded tion by the universality of Q⇓ (Œeorem A.13).  query Q# is the smallest sharing equivalence containing Q. Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

Proof. By Œeorem A.26 and the de€nition of the propagation. on the trace τ , and following the cases of the de€nition of index(l |  n τ ) and index(l 0 | m τ ). (1) Case index(l | n τ l) = 0 = index(l 0 | m τ l 0). From B λ-CALCULUS Œeorem A.5 we obtaine l R⇓ l 0, and we can conclude. In this section, we are going to the de€ne the relationship between (2) Case index(l | n τ ·d l) = 0 , 1 + index(l 0 | m τ ) = the equality of λ-terms and bisimulations on a λ-graph. First, we index(l 0 | m τ ·d ) with l 0 , m0. It holds that l R⇓ m, and decompose the equality of readbacks into the equality of their sub- from the fact that m τ m0 crosses l 0 we obtain by Œeo- terms (result similar to Œeorem A.5 and Œeorem A.6 for relations): 0 rem A.10 a trace τ 0 such that l 0 τ ·d m0. From Œeorem A.9 0 Proposition B.1. Let r,r 0 be roots of a λ-graph G. r = r 0 (using l R m0 and l 0 τ ·d m0) we obtain l 6R l 0 and con- holds if and only if, for every trace τ : J K J K clude. τ ·d τ τ 0 τ ( | ) ( | ) (1) Trace Equivalence: r if and only if r ; (3) Case index l n = 1 + index l n , 0 = τ 0 0 τ 0 τ τ index( | ) (2) Trace Propagation: if r and r , then qr y = l m l with l , n . Symmetric to the case τ above. qr 0 y. (4) Case index(l | n τ ·d ) = 1+index(l 0 | n τ ) and index(l 0 | Proof. m τ ·d ) = 1 + index(l 0 | m τ ) with l , n0 and l 0 , m0. (⇒) We assume that r = r 0 , and proceed by structural J K J K Use the i.h. to conclude. induction on τ : τ ·d τ 0 ϵ 0 ϵ (5) Case index(l | n ) = index(l | n ) and index(l | – Empty Trace. Clearly r and r . Œe fact that τ ·d 0 τ ϵ 0 ϵ m ) = index(l | m ): use the i.h. to conclude. qr y = qr y follows from the hypothesis, since qr ϵ y = r and qr 0 ϵ y = r 0 .  – Trace Cons.J LetK τ τ 0 · d, andJ assumeK without loss of B We can now prove: generality that r τ . We need to prove that r 0 τ and τ 0 τ that qr y = qr y. Theorem B.3. Let Q a query over a λ-graph G. Q⇓ is an open 0 0 0 By inversion r τ , and by i.h. r 0 τ and rr τ z = bisimulation if and only if n = m for every nodes n,m such that n Q m. J K J K 0 τ 0 rr z. We proceed by cases on d: Proof. First note that Q⇓ is an open bisimulation if and only

 0 ⇓ ∗ Case d = . Necessarily r τ Abs(n) and if Q is open, homogeneous, and closed under  . We now prove 0 separately the two implications of the statement of the theorem: r 0 τ Abs(m) for some n,m. By the de€nition of ⇓ r τ 0 z τ (⇐) Assume n = m for every n Q m; we prove that Q is readback to λ-terms, r = λqr ny and J K J K open, homogeneous, and closed under  : 0 τ 0 0 τ τ rr z = λqr my, which imply qr ny = – Open. Let n Q⇓ m where n and m are free variable 0 τ 0 τ

0 τ nodes. By Œeorem A.8, n n and m m for some qr my by the de€nition of equality. 0 0 0 τ 0 τ τ 0 τ and n Q m . By Œeorem B.1 qn y = qm y. ∗ Case d = . Necessarily r App(n1,n2) 0 By the de€nition of readback to λ-terms name(n) = and r τ App(m ,m ) for some n ,n ,m ,m . 1 2 1 2 1 2 name(m), which implies name(n) = name(m) and there- By the de€nition of readback to λ-terms, fore n = m. 0 0· τ τ s τ  { ⇓ 0 τ rr z = qr n1y r n2 and – Homogeneous. Let n Q m. By Œeorem A.8, n n and m0 τ m for some τ and n0 Q m0. By Œeorem B.1 0 0· 0 τ 0 τ s 0 τ  { 0 τ 0 τ rr z = qr m1y r m2 , which qn y = qm y. Conclude by the de€nition of readback to λ-terms. imply qr τ n y = qr 0 τ m y by the de€nition 1 1 – Closed under . Let Var(l) Q⇓ Var(l 0): we need of equality.  Q⇓ 0 τ Var( ) ∗ Case d = . Similar to the case above. to prove that l l . By Œeorem A.8, n l  m τ Var(l 0) τ n Q m (⇐) Œe statement follows from the hypothesis Trace Propaga- and for some and , and by Œeo- τ Var( ) = τ Var( 0) tion by taking τ B ϵ.  rem B.1 qn l y qm l y. By the de€- nition of the readback to λ-terms, index(l | n τ ) = In order to prove Œeorem B.3, we need the following auxiliary index(l 0 | m τ ). Conclude by Œeorem B.2. proposition, similar to Œeorem A.11 for relations. (⇒) Assume that Q⇓ is open, homogeneous, and closed under  , and let n Q m. In order to prove that n = m , by Proposition B.2. Let R be a blind bisimulation. Let n,m nodes J K J K τ τ Œeorem B.1 it suces to prove that for every trace τ the such that n R m, and τ a trace such that n crosses l and m 0 0 τ 0 conditions Trace Equivalence and Trace Propagation hold: crosses l . Šen l R l if and only if index(l | n ) = index(l | ⇓ τ – Note that n Q m implies n Q m, m ). Trace Equivalence. and conclude by Œeorem A.6. Proof. Let n τ n0 and m τ m0. Note that by Œeorem A.5 it – Trace Propagation. Assume n τ n0 and m τ m0. By follows that n0 R m0, which implies that n0 and m0 have the same Œeorem A.8, n0 Q⇓ m0. Note that n0 and m0 have the label since R is homogeneous. We proceed by structural induction same label because Q⇓ is homogeneous by hypothesis. Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

Abs( ) R Abs( ) By the property that the λ-graph is €nite and acyclic, n m (3) 0 there exists a bound on the length of traces in the n R m λ-graph, say B. Œe proof that n0 = m0 proceeds Note that when the relations R and R0 are the same, the concept by (course of value) induction onJ BK− |τJ|, andK by cases of propagated up to coincides with the usual concept of propagated: on the label of the nodes: Fact C.2. If a relation R is propagated upto R, then R is simply ∗ Œe cases Var() Q⇓ Var(l) and Var(l) Q⇓ propagated. Var() are impossible because Q⇓ is homoge- neous. Two properties of up to relations follow. Œe €rst proposition ∗ Case Var() Q⇓ Var(). From the hypothesis shows that the property of being propagated upto R0 is monotonous that Q⇓ is open it follows that n0 = m0, and we on R0. conclude. 0 00 ⇓ 0 Proposition C.3 (Monotonicity). Let R, R , R be binary re- ∗ Case Var(l) Q Var(l ). From the hypothe- 0 ⇓ lations over the nodes of a λ-graph G. If R is propagated upto R , and sis that Q is closed under it follows that 0 00 00  R ⊆ R , then R is propagated upto R . l Q⇓ l 0. We need to show that qn τ Var(l)y = 0 00 τ 0 τ R R R qm Var(l )y, i.e. index(l | n Var(l)) = Proof. Let be propagated upto , and let be a relation R0 ⊆ R00 R R00 index(l 0 | m τ Var(l 0)). Since Q is a query, such that . In order to show that is propagated upto , τ τ it suces to check that it is closed under the rules of Œeorem C.1. n crosses l and m crosses l 0. Œe the- τ We show that it is closed under the €rst rule, the proof for the sis index(l | n Var(l)) = index(l 0 | τ 0 other rules is similar. Let App(n1,n2) R App(m1,m2); since R is m Var(l )) follows from Œeorem B.2. 0 0 0 00  τ · τ ·  propagated upto R , it follows that n1 R m1. Since R ⊆ R , we ∗ Case Abs. By i.h. rn z = rm z. 00 conclude with n1 R m1.  We conclude by the de€nition of readback,   Œe second proposition shows that the property of being propa- τ r τ · z r τ · z since qn y = λ n and m = gated upto commutes with the rst-closure.  r τ · z

λ m . Proposition C.4. Let R be a binary relation over the nodes of   s τ · { s τ · { a λ-graph G, and assume R to be homogeneous. If R is propagated ∗ Case App. By i.h. n = m ∗ ∗ upto R , then R is propagated.

s τ · { s τ · { ∗ and n = m . We con- Proof. Let R be propagated upto R . In order to prove that R∗ is propagated, by Œeorem C.2 it suces to prove that R∗ is clude by the de€nition of readback, since

 propagated upto R∗. Œe rest of the proof is similar to the proof of τ s τ · { s τ · { τ qn y = n n and qm y = Œeorem A.16. 

 0 s τ · { s τ · { Definition C.5 (Blind Bisimulation Upto). Let R, R be bi- m m .  nary relations over the nodes of a λ-graph G. We say that R is a blind bisimulation upto R0 when R is homogeneous and propagated upto Proof of (Theorem 4.9). Let Q be a query over a λ-graph G. Q# R0. is a sharing equivalence if and only if n = m for every nodes n,m such that n Q m. J K J K D THE BLIND CHECK D.1 General Properties Proof. By Œeorem A.25 and Œeorem B.3.  In this section, we are going to prove general properties C “UP TO” RELATIONS of program runs. Œe properties are grouped according to the concepts that they analyse, i.e. canonic assignment, We introduce the notion of relations that are propagated up to BuildEquivalenceClass, EnqueueAndPropagate, enqueuing, de- another relation. Œis new concept will be necessary when for- queuing, parents, ∼neighbours. mulating the invariants of the algorithm, which need to capture properties of incomplete relations since the algorithm progressively Canonic Assignment. Œe algorithm assigns a canonic to each constructs the required relations. node n: intuitively, c(n) is the canonic representative of the equiva- lence class to which n belongs. 0 Definition C.1 (Propagated Up To). Let R, R be binary rela- Œe following lemma is a key property required by many of

tions over the nodes of a λ-graph G. We say that R is propagated up the next results: it shows that nodes cannot change class during a 0  to R when R is closed under the following rules (which are variants program run, i.e. once a node is assigned to an equivalence class, it  of the rules  in Figure 3): is never re-assigned. App(n ,n ) R App(m ,m ) (1) 1 2 1 2 Proposition D.1 (Canonic Assignment is Definitive). Let n R0 n1 m1 be a node. In every program run: App(n1,n2) R App(m1,m2) • c(n) is never assigned to unde€ned; (2) 0 • c(n) n2 R m2 is assigned at most once. Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

Proof. No line of the algorithm assigns a canonic to unde€ned, not de€ned in S, then BuildEquivalenceClass(n) is called; when i.e. a‰er c(n) is de€ned, it remains de€ned throughout the program BuildEquivalenceClass(n) is executed, n is assigned a canonic run. (Line 5), and again the canonic is still assigned in Sf since the In order to show that c(n) is never assigned twice, it suces canonic assignment is de€nitive (Œeorem D.1).  to show that, whenever a canonic is assigned, it was previously unde€ned. A canonic is assigned only during the execution of two BuildEquivalenceClass. Œe procedure lines of the algorithm: BuildEquivalenceClass(c) has the e‚ect of constructing • Line 5, during the execution of the equivalence class of a node c, where c is chosen as canonic BuildEquivalenceClass(c) that assigns c(c) B c. representative of that class. Note that only Line 3 and Line 12 can call BuildEquivalenceClass, and both lines check be- Proposition D.4 (No Multiple Calls to BuildEquivalenceClass( ) forehand whether the canonic of c is unde€ned. n ). In every program run, BuildEquivalenceClass( ) • Line 27, during the execution of n is called at most once for every n EnqueueAndPropagate(m, c) that assigns c(m) to c. node . Only Line 16 can call EnqueueAndPropagate, and that line checks beforehand whether the canonic of m is Proof. Only Line 3 and Line 12 can call unde€ned.  BuildEquivalenceClass(n), and only if the canonic of n is unde€ned. Right a‰er BuildEquivalenceClass(n) is called, Œe following lemma shows that the mechanism of canonic Line 5 is executed, and a canonic is assigned to n. Œerefore representatives is correct, i.e. if c is assigned as canonic of an another call to BuildEquivalenceClass(n) is not possible in the equivalence class, then c is itself a representative of that class: future, because c(n) cannot be ever assigned again to unde€ned (Œeorem D.1).  Proposition D.2 (c(·) is Idempotent). Let c,n be nodes, and S be a reachable state such that c(n) = c. Šen c(c) is de€ned and A node c can be assigned as a canonic representative only if c(c) = c. BuildEquivalenceClass(c) has been called: Proof. Let n be a node. It suces to show that whenever c(n) is assigned to c, it holds that c(c) = c; once true, this assertion Proposition D.5 (Only BuildEquivalenceClass(c) Desig- cannot be later falsi€ed because the canonic assignment is de€nitive nates Canonics). Let c,n be nodes, and S be a reachable state (Œeorem D.1). such that c(n) = c. Šen BuildEquivalenceClass(c) has been A canonic is assigned only during the execution of two lines of called before S. the algorithm: c • Line 5, during the execution of Proof. is assigned as a canonic only during the execution of two lines of the algorithm: BuildEquivalenceClass(c). In this case n = c and we conclude. • Line 5, during the execution of • Line 27, during the execution of BuildEquivalenceClass(c). EnqueueAndPropagate(m,c) that assigns c(m) to c. • Line 27, during the execution of Only Line 16 of BuildEquivalenceClass(c) can call EnqueueAndPropagate(m,c) that sets c(m) to c. EnqueueAndPropagate(m, c), and that line comes a‰er Only Line 16 of BuildEquivalenceClass(c) can Line 5 which sets c(c) to c. Œerefore c(c) = c in S call EnqueueAndPropagate(m,c).  as well, because the canonic assignment is de€nitive (Œeorem D.1).  EnqueueAndPropagate. Œe function EnqueueAndPropagate(m,c) adds the node m to the equiva- During a program run nodes are assigned a canonic, i.e. tem- lence class represented by c, and delays the processing of m by porary equivalence classes are extended with new nodes. If the pushing it into q(c). algorithm terminates successfully, the equivalence classes cover the whole set of nodes, as the following lemma shows: Proposition D.6 (No Multiple Calls to EnqueueAndPropagate(n,–)). In every program run, Proposition D.3 (Canonic Assignment Completed). In a €nal EnqueueAndPropagate(n,–) is called at most once for every state Sf , every node has a canonic assigned. node n. Proof. Let n be a node, and let us show that c(n) is de€ned in Sf . Since the algorithm terminated, execution exited the main Proof. Only Line 16 can call EnqueueAndPropagate(m,c), loop on Line 2. Œat loop iterates on every node n of the λ-graph, and only if the canonic of m is unde€ned. A‰er therefore for every n there must exist a state S prior to Sf such that EnqueueAndPropagate(m,c) is called, Line 27 is executed, the next transition is the execution of Line 3 with local variable n. and a canonic is assigned to m. Œerefore another call to Line 3 €rst checks whether c(n) is de€ned. If it is, it does nothing, BuildEquivalenceClass(m,–) is not possible in the future, and we can conclude because c(n) must be de€ned in Sf too since because c(m) cannot be ever assigned again to unde€ned the canonic assignment is de€nitive (Œeorem D.1). If instead c(n) is (Œeorem D.1).  Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

Enqueuing. Two lines of the algorithm push a node to a queue: Proof. c(n) is assigned to c only during the execution of Line 7, when q(c) is created and c pushed to it; Line 28, when m is BuildEquivalenceClass(c) (Œeorem D.5). First, note that pushed to q(c). Intuitively, pushing a node to q(c) results in delaying shortly a‰er c(n) is assigned to c (Line 5 or Line 27), n is enqueued its processing by the algorithm; the node will be fully processed to q(c) (resp. Line 7 and Line 28). only later, a‰er being popped from the queue (Lines 9–17). In both cases, n is enqueued to q(c) when the execution of the while loop on Line 8 has not terminated yet: on Line 7 before the Proposition D.7 (En€eue Once). In a program run, every node beginning of the loop, on Line 28 during the execution of the loop, n is enqueued at most once. since EnqueueAndPropagate is called from Line 16 which is inside Proof. Œe algorithm enqueues a node only shortly a‰er seŠing the while loop. ( ) its canonic: Note also that a node n is dequeued from q c only on Line 9 during the execution of BuildEquivalenceClass(c), and that • on Line 7, a‰er assigning a canonic on Line 5; BuildEquivalenceClass(c) can be called at most once (Œeo- • on Line 28, right a‰er assigning a canonic on Line 27. rem D.4). Since the canonic of a node can be assigned at most once (Œeo- In conclusion, n was enqueued to q(c) before the com- rem D.1), it follows that a node can be enqueued at most once.  pletion of the while loop on q(c) on Line 8, and since BuildEquivalenceClass(c) has already returned and the while Œe following lemma shows that q(c) is a of the equiva- loop terminated, at some point n was dequeued from q(c) on Line 9, lence class with canonic representative c: and the body of the loop executed with locals c,n.  Proposition D.8 (eue ⊆ E€ivalence Class). Let S be a Proposition D.12 (De€eue Once). In each program run, Line 9 reachable state, and n a node. If n ∈ q(c), then c(n) is de€ned and is executed with local n at most once for every node n. c(n) = c. Proof. Executing Line 9 with local n dequeues n, which cannot Proof. Let n be a node. Since the canonic assignment is de€ni- be enqueued twice by Œeorem D.7.  tive (Œeorem D.1), it suces to check that c(n) = c whenever n is enqueued to q(c). A node is enqueued only during the execution of Parents. Œe loop on Line 10 iterates on all the parents of a node, two lines of the algorithm: building their equivalence classes: • Line 7 during the execution of Proposition D.13 (Parent Classes Built). Let c,n be nodes, BuildEquivalenceClass(c). In this case n = c, S and the canonic of c was just assigned to c itself on Line 5. and let be a state reachable a‡er the execution of the loop on Line 10 BuildEquivalenceClass( ) c,n • Line 28 during the execution of of c with local variables . Šen for m n EnqueueAndPropagate( ). Line 28 is executed every parent of : m, c 0 right a‰er Line 27, which sets c(m) B c.  • c(m) is de€ned, say c(m) = c ; • BuildEquivalenceClass(c0) has been called and has al- Dequeuing. Nodes are popped from a queue only on Line 9. Once ready returned. a node is dequeued, the algorithm proceeds by €rst visiting its parents (Line 10) and then its ∼neighbours (Line 14). Proof. Œe loop on Line 10 iterates on all parents of n. For every parent m: Proposition D.9 (De€eued Nodes Have Correct Canonic). • If m has no canonic assigned, S c,n Let be a state reached a‡er the execution of Line 9 with locals . BuildEquivalenceClass(m) is called (Line 12). Since c( ) c( ) = Šen n is de€ned and n c. S is reached a‰er the execution of the loop, the call BuildEquivalenceClass( ) Proof. Let n be a node. n ∈ q(c) in the state S0 right before the to m has already returned. execution of Line 9 with locals c,n. Œerefore c(n) = c in S0 by Note that m was assigned itself as a canonic on Line 5 BuildEquivalenceClass( ) c( ) S Œeorem D.8. Conclude with c(n) = c in S because the canonic of m , and m = m still in assignment is de€nitive (Œeorem D.1).  (Œeorem D.1). • If m has some c0 assigned as a canonic node, then Line 13 0 Definition D.10 (Processed Node). In a reachable state S, we enforces that building(c ) = false. Œis means that 0 say that a node n has already been processed if Lines 9–17 with locals Line 18 of BuildEquivalenceClass(c ) has already been 0 c,n have already been executed (for some c). executed, and therefore BuildEquivalenceClass(c ) has In other words, a node n is processed a‡er it is dequeued and its already returned.  parents and ∼neighbours visited. ∼neighbours. Œe loop on Line 14 iterates on all the ∼neighbours A‰er BuildEquivalenceClass(c) returns, all the nodes in the of a node. Note that the loop does not remove ∼edges a‰er iterating equivalence class of c are processed: on them: in fact, the algorithm simply ignores ∼edges that have already been encountered, as it calls EnqueueAndPropagate only Proposition D.11 (E€ivalence Class Processed). on ∼neighbours that have not been enqueued yet (Line 16). Let c,n be nodes, and S a reachable state such that BuildEquivalenceClass(c) has already returned. If c(n) = c, Proposition D.14 (∼ Grows). During a program run, ∼ mono- then n is processed. tonically grows. Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

Proof. Just note that no line of the algorithm removes ∼edges. D.2 Correctness  Definition D.17 (Same Canonic, =c). In every reachable state S we de€ne =c, a binary relation on the nodes of the λ-graph such A‰er the parent classes of a node are built, the set of its that, for all nodes n,m: n =c m i‚ c(n) and c(m) are both de€ned and ∼neighbours is not going to change during the program run: c(n) = c(m). Proposition D.15 (Finalization of ∼neighbours). Let c,n be ∗ First of all, let us show that =c is identical to c in all €nal states: ∼ S nodes. No edge with endnode n can be created in any state reached this allows to simplify the following proofs, using the more familiar a‡er the execution of the loop on Line 10 with local variables c,n. relation c instead of the the new =c.

Proof. Assume by contradiction that S is a state reached a‰er Proposition D.18. Let Sf be a €nal state. Šen for all nodes n,m: ∗ the execution of the loop on Line 10 with locals c,n, and that the n =c m if and only if n c m. next transition creates a new ∼edge with endnode n. New ∼edges may be created only on Line 21 and Line 23 during the execution Proof. First note that in Sf all nodes have a canonic assigned of EnqueueAndPropagate(m,c0) for some nodes m,c0. Note that by Œeorem D.3. ∗ a ∼edge with endpoint n may be created by these lines only if n has (⇒) Let n =c m, and let us prove that n c m. By the de€nition either m or c0 as a parent. We show that these cases are both not of =c, there exists c such that c(n) = c(m) = c. By the possible: de€nition of c, it holds that n c c and m c c, and therefore c∗ • m cannot be a parent of n since c(m) is unde€ned be- clearly n m. (⇐) c∗ cause of the check on Line 16 (the only line that may Let n m, and let us prove that n =c m. We proceed by c∗ call EnqueueAndPropagate), while by Œeorem D.13 all induction on the de€nition of : c∗ c parents of n have a canonic assigned. – Base case. Assume that n m because n m, i.e. c( ) c( ) • In order to show that c0 cannot be a parent of n, note because n = m. By Œeorem D.2, m = m, and therefore n =c m. €rst that BuildEquivalenceClass(c’) has not returned ∗ – Rule • . Assume that n c m and n = m. From the yet, since EnqueueAndPropagate(m,c’) is called only on hypothesis it follows that n has a canonic assigned,

Line 16 of BuildEquivalenceClass(c’). Note also that 0 0 and therefore clearly n = n. c(c ) = c because Line 5 of BuildEquivalenceClass(c’) c – Rule . Assume that n c∗ m because m c∗ n. By i.h. has already been executed.  Œerefore c0 cannot be a parent of n by Œeorem D.13. m =c n, and we conclude because =c is symmetric. – Rule . Assume that n c∗ m because n c∗ p and  ∗ p c mfor some node p. By i.h. n =c p and p =c m, Œe following lemma proves that, a‰er the ∼neighbours of a and we conclude because =c is transitive.  node n are handled by the loop on Line 14, the canonic assignment # In order to prove that =c equals Q in S (Œeorem D.25), we subsumes the relation ∼ on n: f are going to prove that c∗ is homogeneous and propagated (Œeo- Proposition D.16 (All ∼neighbours Visited). Let S be a state rem D.24). Œe following lemma is a relaxed variant of that state- reachable a‡er the execution of the loop on Line 14 with local variables ment which holds for all reachable states: it collapses to the desired ∗ c,n. Šen for every ∼edge with endnodes n and m, n c∗ m. statement in the €nal state because (∼ ∪ =) = (c ). Proposition D.19 (c is Upto). Let S be a reachable state. Šen Proof. Let S0 be the state prior to S in which execution is just c is a blind bisimulation upto (∼ ∪ =). entering the loop on Line 14. Note that both S0 and S are reached only a‰er the execution of the loop on Line 10 (with locals c,n), and Proof. We prove the statement by induction on the length of 0 therefore in S and S are present the same ∼edges with endnode n the program run leading to S: (by Œeorem D.14 and Œeorem D.15). Œe loop on Line 14 iterates • In the initial state c = ∅, and therefore the statement holds ∼ on all such edges with endnodes n and m, and for each m it either: trivially. • Line 16: calls EnqueueAndPropagate(m,c), which sets • As for the inductive step, we only need to discuss the c(m) B c before returning (Line 27); program transitions that alter ∼ and c. But €rst of all, note • Line 17: explicitly enforces that c(m) = c. that ∼ can only grow (Œeorem D.14), and that creating In both cases, we obtain that c(m) = c holds also in S (because S is new ∼edges (while keeping c unchanged) does not falsify reached a‰er the execution of the loop on Line 14, and the canonic the statement if it was true before the addition. Œerefore assignment is immutable by Œeorem D.1). Note also that c(n) = c we actually need to discuss only the transitions that alter in S by Œeorem D.9, since S is reached a‰er the execution of the canonical assignment, i.e. Line 5 and Line 27: Line 9 which dequeues n from q(c). – Line 5: by i.h., c was homogeneous and propagated We thus obtain c(n) = c(m) = c in S, i.e. n c∗ m.  upto (∼ ∪ =) before the execution of the assignment c(c) = c. A‰er the execution of that assignment, In the following sections, we are going to prove that Algorithm 1 c di‚ers only for the new entry (c,c). Clearly the is sound (Appendix D.2), complete (Appendix D.3), and that it runs new entry satis€es the homogeneous condition, and in linear time (Appendix D.4). it satis€es the property of being propagated upto =. Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

– Line 27, during the execution of • Base case. In the initial state (∼) = Q and the statement EnqueueAndPropagate(m, c): by i.h., c was clearly holds. homogeneous and propagated upto (∼ ∪ =) before • Inductive step. First note that during the execution of the execution of the assignment c(m) B c. A‰er the algorithm ∼ can only grow (Œeorem D.14), therefore the execution of that assignment, c di‚ers only for the requirement Q ⊆ ∼ follows from the i.h. In order to the new entry (m,c). Œe new entry satis€es the prove that ∼ ⊆ Q#, we only need to discuss the program homogeneous condition and the property of being transitions that alter ∼, i.e. those occurring on Line 21 and propagated upto ∼ because of the code executed in Line 23: Lines 20–26, which checked the labels of the nodes – Line 21 during the execution of and created ∼edges on the corresponding children of EnqueueAndPropagate(m,c). Because of the check m,c if any.  on the same line, m = Abs(m0) and c = Abs(c0) for some m0,c0. Executing Line 21 creates the new edge Œe following Œeorem D.20 and Œeorem D.21 state properties 0 ∼ 0 Q# ∗ m c . Since is propagated, in order to show that connect the relations c, Q, and ∼. In a €nal state c is exactly 0 Q# 0 ∗ the new requirement m c it suces to show ∼ , but in intermediate states the following weaker property holds: that m Q# c. We are going to show that m ∼∗ c in Proposition D.20. In every reachable state S: c ⊆ ∼∗. the state prior to S: then by using the i.h. we obtain m Q# c, and we can conclude. Proof. We prove the statement by induction on the length of Note that EnqueueAndPropagate(m, c) is the program run leading to S: called from Line 16 during the execution of • In the initial state, c = ∅ and therefore the statement holds BuildEquivalenceClass(c). Because of the loop on trivially. Line 14, m was a ∼neighbour of n for some node n, • As for the inductive step, we only need to discuss the and it still is because ∼ can only grow (Œeorem D.14). ∗ program transitions that alter ∼ and c. But €rst of all, In order to prove m ∼ c, it thus suces to prove ∗ note that during the execution of the algorithm ∼ can n ∼ c. Œe laŠer follows from Œeorem D.20, by only grow (Œeorem D.14), and that creating new ∼edges using the fact that Line 16 is executed a‰er Line 9, (while keeping c unchanged) does not falsify the statement therefore c(n) = c if it was true before the addition. Œerefore we actually – Line 23 is similar to the previous case.  need to discuss only the transitions that alter the canonical Corollary D.22. In every reachable state, c∗ ⊆ Q#. assignment, i.e. Line 5 and Line 27: ∗ – Line 5: by i.h., c ⊆ ∼ before the execution of the as- Proposition D.23 (All ∼edges Visited). In every €nal state, signment c(c) = c. A‰er the execution of that assign- (∼) ⊆ (c∗). ment, c di‚ers only for the new entry (c,c). Clearly c ∼∗ c because ∼∗ is reƒexive by de€nition. Proof. Let Sf be a €nal state. By Œeorem D.3, in Sf every – Line 27, during the execution of node has a canonic assigned. Let n be a node, and c B c(n); we are ∗ EnqueueAndPropagate(m, c): by i.h., c ⊆ ∼∗ going to prove that n c m for every ∼neighbour m of n. before the execution of the assignment c(m) B c. By Œeorem D.5, BuildEquivalenceClass(c) was called before A‰er the execution of that assignment, c dif- Sf . Since Sf is €nal, BuildEquivalenceClass(c) must have re- fers only for the new entry (m,c). Note that turned, and therefore n has already been processed (Œeorem D.11). EnqueueAndPropagate(m, c) is called from Line 16 A‰er the loop on Line 10, the ∼neighbours of n are €nalized (Œeo- during the execution of BuildEquivalenceClass(c). rem D.15), and a‰er the loop on Line 14, the ∼neighbours of n are Because of the loop on Line 14, m was a ∼neighbour all visited (Œeorem D.16).  of n for some node n, and it still is because ∼ can only Proposition D.24. Let S a €nal state. Šen c∗ is a blind bisim- grow (Œeorem D.14). We are now going to prove f ulation. that n ∼∗ c, which together with the fact that m is a ∼neighbour of n, will allow us to conclude with Proof. By Œeorem D.19, c is homogeneous and propagated up ∗ m ∼ c. to (∼∪=). We need to show that c∗ is homogeneous and propagated. Line 16 is executed a‰er Line 9, therefore c(n) = c (i.e. • Homogeneous. It follows from Œeorem A.15. n c c) by Œeorem D.9. By i.h. n c c implies n ∼∗ c, • Propagated. By Œeorem D.23 and Œeorem D.18, (∼) ⊆ and we are done.  (c∗), which implies that (∼ ∪ =) ⊆ (c∗) because c∗ is re- ∗ ∼ ƒexive. Œerefore c is propagated up to c by Œeorem C.3, During a program run, the relation progressively approximates ∗ the relation Q#: and c is propagated by Œeorem C.4.  # ∗ # Proposition D.21 (∼ Approximates Q ). In every reachable Proposition D.25. Let Sf a €nal state. Šen (c ) = (Q ). state S: Q ⊆ ∼ ⊆ Q#. Proof. By Œeorem D.22, (c∗) ⊆ (Q#). In order to prove (Q# Proof. We prove the statement by induction on the length of ) ⊆ (c∗), it suces to note that Q ⊆ (c∗) (because Q ⊆ (∼) the execution trace leading to S: by Œeorem D.21, and (∼) ⊆ (c∗) by Œeorem D.23), that c∗ is Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA an equivalence relation (by de€nition), and that c∗ is propagated We proceed by induction on j − i − 1: (Œeorem D.24).  • Case i + 1 = j. Obtain from Œeorem D.27 a node n and d Proof of (Theorem 6.1). In every €nal state Sf : a direction d such that ci+1 = cj n and n c ci , and # conclude because c Q# m by Œeorem D.22. • Succint representation: (=c) = (Q ), i • • Blind check: Q# is homogeneous, and therefore a blind shar- Case i + 1 < j. By i.h. there exist a non-empty trace τ and τ # ing equivalence. a node n such that cj n and ci+1 Q n. By Œeorem D.27, d there exist a direction d and a node m such that ci+1 m Proof. By Œeorem D.24 and Œeorem D.25.  # and m c ci (and therefore ci Q m by Œeorem D.22). # d D.3 Completeness From ci+1 Q n and ci+1 m, by Œeorem A.6 there exists a nodem0 such that n d m0 andm Q# m0. We can conclude, Definition D.26 (Staircase Order ≺). Let S be a reachable since c τ ·d m0 and c Q# m0.  state, and n,m be nodes of the λ-graph G. We say that n ≺ m i‚ there j i exist a direction d and a node p such that m d p (riser) and p c n (tread). We are now ready to prove that the algorithm is complete: Proposition D.27 (Order of Active Classes). Let S be a reach- Theorem D.29 (Completeness). If the algorithm fails, then Q# able state such that active = [c1,...,cK ]. Šen ci ≺ ci+1 for every is not homogeneous. 0 < i < K. S Proof. We proceed by induction on the length of the program Proof. Let be a failing state, i.e. a reachable state that tran- sitions to F . Let active = [c ,...,c ] in S. Œere are three run. In the base case, i.e. in the initial state, the callstack is empty ail 1 K and therefore the property holds trivially. lines of the algorithm in which failure may occur: Line 13, Line 17, As for the inductive step, we only need to discuss the pro- and Line 26. We discuss these cases separately; in all cases, in order to prove that Q# is not homogeneous, we assume Q# to be gram transitions that actually alter the callstack, i.e. when BuildEquivalenceClass is called or when it returns: homogeneous, and derive a contradiction. 0 • BuildEquivalenceClass returns. In this case, the last • Line 13. Let m be a parent of n such that c(m) = c and 0 0 entry of the callstack is removed, and the statement follows building(c ) = true. Since building(c ) = true, the from the i.h. call to BuildEquivalenceClass(c’) has not returned be- 0 • BuildEquivalenceClass(n) is called (Line 3). Right be- fore S, and therefore c = ci for some 0 < i ≤ K. By 0 fore the call to BuildEquivalenceClass(n) the callstack Œeorem D.27 c1 ≺ ... ≺ cK , and therefore c was al- is empty, and right a‰er the call active = [n], i.e. there is ready encountered in the stair of active classes. Note that nothing to prove. c(n) = cK = c (Œeorem D.9), and therefore cK ≺ m. Also, # • BuildEquivalenceClass(m) is called (Line 12). In this from c(m) = ci it follows thatm Q ci (Œeorem D.22). Œe case m is a parent of n (i.e. there exists a direction contradiction is obtained by noting that by Œeorem D.28 it # d cannot be the case that m Q ci , since c1 ≺ ... ≺ cK ≺ m. d such that m n), and c = cK before the call to • m ∼ n c(m) = BuildEquivalenceClass(m). Note that Line 12 is exe- Line 17. Let be a neighbour of such that 0 c( ) c( ) 0 cuted a‰er Line 9 with locals c,n, and therefore c(n) = c c , c. By Œeorem D.9, n = c. Since m = c , BuildEquivalenceClass( ) (Œeorem D.9). c’ must have been called (Œe- A‰er the call to BuildEquivalenceClass(m), orem D.5), and there are two options: – BuildEquivalenceClass(c’) has already returned. active = [c1,...,cK ,m]. Œe statement for i < K follows from the i.h., and for the case i = K we just proved that We show that this is not possible. In fact, if d BuildEquivalenceClass(c’) has returned before m n and n c cK .  S, then m has already been processed before S Œe intuition is that recursive calls to BuildEquivalenceClass (Œeorem D.11). Œerefore n =c m (Œeorem D.16), climb a stair of nodes in the λ-graph. Œe algorithm fails when contradicting the hypothesis that c , c0. it encounters a step that was already climbed: this is because the – BuildEquivalenceClass(c’) has not yet returned. # 0 staircase order is strict when Q is homogeneous and propagated. Œerefore c = ci for some 0 < i < K. From ∗ # c(n) = c = cK and c(m) = ci obtain n ∼ cK and Proposition D.28 (Q Respects Staircase Order). Let S be a ∗ ≺ ≺ m ∼ ci (Œeorem D.20). Since m is a ∼neighbour of reachable state, and let c1,...,cK be nodes such that c1 ... cK . ∗ # # # n, ci ∼ cK , and therefore ci Q cK by Œeorem D.21. If Q is homogeneous, then ci Q cj if and only if i = j. Œis yields a contradiction by Œeorem D.27 and # # Proof. If i = j, then clearly ci Q cj because Q is reƒexive. Œeorem D.28. For the other direction, assume by contradiction and without • Line 26. Assume that c and m do not respect the ho- loss of generality that i < j (since Q# is symmetric). We are going mogenity condition. We are going to prove that c Q# m, to prove that there exist a non-empty trace τ and a node m such which contradicts the hypothesis that Q# is homogeneous. τ # # that cj m and ci Q m: this, together with ci Q cj , will yield a EnqueueAndPropagate(m,c) is called from Line 16, and contradiction by Œeorem A.9. therefore m is a ∼neighbour of n. Line 16 is executed a‰er Conference’17, July 2017, Washington, DC, USA Condoluci, Acca‚oli and Sacerdoti Coen

Line 9 with locals c,n, and therefore c(n) = c by Œeo- (1) Transitions executing lines of BlindCheck. Œe proce- ∗ ∗ rem D.9. By Œeorem D.20, c ∼ n, and therefore c ∼ m. dure BlindCheck() is called exactly once, in the initial By Œeorem D.21, we conclude with c Q# m.  state. Œe loop on Line 2 simply iterates on all the nodes of the λ-graph: hence the loop bound is just |N |. Line 3 may # Proof of (Theorem 6.2). If Algorithm 1 fails, then Q is not a call the procedure BuildEquivalenceClass, but here we blind sharing equivalence, and therefore by the blind universality of do not consider the transitions caused by that function, # Q (Šeorem 4.7), there does not exist any blind sharing equivalence that will be discussed separately below. Œerefore, the total containing the initial query Q. number of transitions that execute lines of BlindCheck is simply O(|N |). Proof. By Œeorem D.29 and Œeorem 4.7.  (2) Transitions executing lines of EnqueueAndPropagate. Œe procedure EnqueueAndPropagate(n,–) is called D.4 Termination & Linearity at most once for every node n (Œeorem D.6). Since In this section, we are going to show that Algorithm 1 always EnqueueAndPropagate contains no loops and no function terminates within a number of transitions that is linear on the size calls, there is a constant bound on the number of transi- of the λ-graph and the initial query Q. tions that are executed by EnqueueAndPropagate each First some notations: time it is called. Œerefore, the total number of transitions

Definition D.30. that execute lines of EnqueueAndPropagate is O(|N |). • N B nodes(G) is the set of nodes in the λ-graph G (3) Transitions executing lines of BuildEquivalenceClass.  • E is the set of directed ( , ,  ) edges in the λ-graph Œe discussion of the complexity of • |·| is the size of a (multi)set. BuildEquivalenceClass is more involved, because • #edges(∼,n) is the number of ∼edges with endnode n. (Recall it contains multiple nested loops. that ∼ is a multirelation) As a €rst approximation, the total number of transi- • #parents(n) is the number of directed edges in E with target tions executing lines of BuildEquivalenceClass is big- n. O of the number of calls to BuildEquivalenceClass plus the numer of transitions executing lines of the loop Œe following lemma proves that in every reachable state the on Line 8 (i.e. Lines 8–17). Œe number of calls to Q c number of query edges is linear in the size of and . Œe state- BuildEquivalenceClass is ≤ |N | (Œeorem D.4). As for ment of the lemma is actually more complicated, because the proof the loop on Line 8, in every program run the body of the |undir| requires a more precise bound on . In particular, the bound loop is executed at most once for every node n (Œeo- |undir| ≤ × |Q| × |c| 2 + 4 holds in almost every state, but breaks rem D.12). EnqueueAndPropagate temporarily during the execution of , just Œe body of the while loop contains two additional before the execution of Line 27. Œe problem is that Lines 20–26 loops: one on Line 10, and the other on Line 14: ∼ may add new edges, while the new canonic that restores the • Œe loop on Line 10 (with locals c,n) simply iterates disequality is only assigned slightly later, on Line 27. on all the parents of a node, which is a €xed number: Proposition D.31 (Bound on undir). In every reachable state therefore each time it is executed with local variable S, |undir| ≤ 2 × |Q| + 4 × |N |. n, it is responsible for O(#parents(n)) transitions. • Œe discussion about the loop on Line 14 (with locals Proof. Proved by induction on the length of the program run c,n) is more involved: it iterates on all ∼neighbours leading to S. In the initial state S0, |undir| = 2 × |Q| because of a node n, but ∼ changes during the execution undir contains the same edges as Q (but undir is also symmetric of the loop. In fact, the body of the loop may and a multirelation, hence each ∼edge counts twice). call EnqueueAndPropagate(m,c) on Line 16, which During the program run, new ∼edges are created only by the may create new ∼edges. However, note that the EnqueueAndPropagate procedure, which may create at most two loop on Line 14 is executed only a‰er the loop on new ∼edges per call (that count as four new entries of the symmetric Line 10, which €nalizes the ∼neighbours of n (Œe- multirelation undir). By Œeorem D.6, EnqueueAndPropagate is orem D.15). Œerefore the loop on Line 14 iterates called at most |N | times in each program run, hence we conclude on a set of ∼edges that does not change a‰erwards with the bound |undir| ≤ 2 × |Q| + 4 × |N |.  during the program run, and is therefore responsible for O(#edges(∼,n)) transitions (where the relation ∼ Theorem D.32 (Termination & Linearity). In every program is the one in S). run, the number of transitions is O(|N | + |E| + |Q|).

Proof. Let us assume a program run with end state S (not necessarily a €nal state). We are going to discuss the number of transitions in the program run in a global way. We group the transitions in the program run according to which procedure they are executing a line of, and we discuss the procedures BlindCheck, BuildEquivalenceClass, and EnqueueAndPropagate separately. Sharing Equality is Linear Conference’17, July 2017, Washington, DC, USA

Œerefore the total number of transitions executing Lines 8–17 is big-O of: Õ {#parents(n) + #edges(∼,n) + 1 | Line 9 is executed with local n} Õ ≤ (#parents(n) + #edges(∼,n) + 1) n ∈N Õ Õ Õ = #parents(n) + #edges(∼,n) + 1 n ∈N n ∈N n ∈N = |E| + |undir| + |N |. Note that by Œeorem D.31, |undir| ∈ O(|Q| + ||N ||). Œerefore the total number of transitions executing lines of BuildEquivalenceClass in a program run is O(|N | + |E| + |Q|). In conclusion, we obtain the following asymptotic bound on the number of transitions in a program run: O(|N |) + O(|N |) + O(|N | + |E| + |Q|) = O(|N | + |E| + |Q|). 

Proof of (Theorem 6.4). Let S0 be an initial state of the al- gorithm, with a query Q over a λ-graph G. Šen the Blind check terminates in a number of transitions linear in |G| and |Q|. Proof. By Œeorem D.32.  E THE VARIABLES CHECK Proof of. Šeorem 7.1 Let Q be a query over a λ-graph G passing the Blind check, and let c be the canonic assignment produced by that check. • (Completeness) If the Variables check fails then there are no sharing equivalences containing Q, • (Correctness) otherwise =c is the smallest sharing equiva- lence containing Q. Moreover, the Variables check clearly terminates in time linear in the size of G. Proof. First note that since the Blind Check succeeded, both c and =c are homogeneous relations. We also use the fact that ∗ (=c) = (c ) (Œeorem D.18). • Completeness: If the Variables Check fails on line 8, then c is not an open relation, and therefore also =c is not open by Œeorem A.23. If the Variables Check fails on line 9, then c is not a bisimulation upto =c, and therefore =c is not a bisimulation, and therefore it is not a sharing equivalence. • Correctness: Since also the Variables Check succeeded, c is an open relation, and closed under  upto =c. In order to show that =c is a sharing equivalence, it suces to show that it is open, and closed under  . =c is closed under  by Œeorem A.16. =c is open by Œeorem A.23. • Termination in linear time: obvious, since the algorithm simply iterates on all variable nodes of the λ-graph.