Sharing Equality Is Linear
Total Page:16
File Type:pdf, Size:1020Kb
Sharing Equality is Linear Andrea Condoluci Beniamino Accaoli 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 [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 specication 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 unication, 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 modied 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 seing λshX where λX smallest bisimulation between the given DAGs, if any. is rened with sharing (we are vague about sharing on purpose) and evaluation in λX is simulated by some renement !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 Accaoli, 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 rened 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 seing 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 specication framework, but—it is simply a maer of suering 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 class P dened in Permission to make digital or hard copies of all or part of this work for personal or λX coincides with the one dened 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 prot 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 permied. 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 specic 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 caoli and Dal Lago have shown that lemost-outermost evaluation DOI: 10.1145/nnnnnnn.nnnnnnn is reasonably implementable [6]. Conference’17, July 2017, Washington, DC, USA Condoluci, Accaoli and Sacerdoti Coen Reasonable conversion and sharing. Some higher-order seings topmost constructor and whose leaves are the (free) variables. A need more than evaluation of a single term. ey oen 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 seings 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 dierent but all these approaches provide dierent 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 identied up to α. One can then say and sharing graphs [23]. e laer 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 dierent 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 dierently. 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 Accaoli 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.