An Abstract Machine for Strong Call by Value 3 of Contexts, and It Subsumes As a Substrategy the Weak Right-To-Left Strategy of Accattoli Et Al.’S fireball Calculus
Total Page:16
File Type:pdf, Size:1020Kb
An Abstract Machine for Strong Call by Value⋆ Ma lgorzata Biernacka1 , Dariusz Biernacki1 , Witold Charatonik1 , and Tomasz Drab1 Institute of Computer Science, University of Wroc law, Poland {mabi,dabi,wch,tdr}@cs.uni.wroc.pl Abstract. We present an abstract machine that implements a full- reducing (a.k.a. strong) call-by-value strategy for pure λ-calculus. It is derived using Danvy et al.’s functional correspondence from Cr´egut’s KN by: (1) deconstructing KN to a call-by-name normalization-by-evaluation function akin to Filinski and Rohde’s, (2) modifying the resulting nor- malizer so that it implements the right-to-left call-by-value function ap- plication, and (3) constructing the functionally corresponding abstract machine. This new machine implements a reduction strategy that subsumes the fireball-calculus variant of call by value studied by Accattoli et al. We describe the strong strategy of the machine in terms of a reduction se- mantics and prove the correctness of the machine using a method based on Biernacka et al.’s generalized refocusing. As a byproduct, we present an example application of the machine to checking term convertibility by discriminating on the basis of their partially normalized forms. Keywords: λ-calculus · Abstract machines · Reduction strategies · Nor- malization by evaluation · Reduction semantics. 1 Introduction Full-reducing (also known as strong) normalization strategies in the lambda cal- culus have so far received relatively little attention compared to weak strategies that provide foundations for functional programming languages, such as OCaml (implementing call by value) or Haskell (implementing call by need). However, arXiv:2009.06984v1 [cs.PL] 15 Sep 2020 recent advances in proof technology and the use of proof assistants based on dependently typed lambda calculus for complex verification efforts propel the design and study of strong reduction strategies, and of their corresponding effi- cient realizations on a machine [2,7,17]. Abstract machines provide a convenient computation model that mediates between the specific reduction strategy in the calculus and its practical im- plementations. The first machine for strong normalization of lambda terms is due to Cr´egut[10]. This machine implements normal-order strategy [16], i.e., a ⋆ This research is supported by the National Science Centre of Poland, under grant number 2019/33/B/ST6/00289. 2 M. Biernacka et al. hybrid strategy that iterates call by name (CbN), and necessarily extends re- duction to open terms and reduces under lambda abstractions—unlike machines for weak strategies that operate on closed terms and stop at lambdas. Similarly to strong CbN, one can define strong call by value (CbV) as an iteration of weak CbV, carefully generalizing the notion of value to open terms [1]. A nor- malization function realizing strong CbV was proposed by Gr´egoire&Leroy and implemented in their virtual machine extending the ZAM machine [17]. Another virtual machine for strong CbV was derived by Ager et al. [6] from Aehlig and Joachimski’s normalization function [4]. Recently, a strong call-by-need strategy has been proposed by Kesner et al. [7], and the corresponding abstract machine has been derived by Biernacka et al. [9]. On the other hand, there is a line of work done by Accattoli et al. who study computational complexity of abstract machines, in particular in the context of a weak CbV strategy that operates on open terms, as an intermediate step towards an efficient machine for strong CbV [2]. Many abstract machines are devised or tailored by hand, and their correctness is far from obvious. Alternatively, Danvy et al. initiated a derivational approach that allows to obtain abstract machines from preexisting semantic artefacts for specific strategies by applying well-defined transformations in a systematic way. Danvy et al.’s functional correspondence [5] is a two-way semantics-preserving derivation method that relates higher-order evaluators and abstract machines. More precisely, following Reynolds’ recipe of applying a CPS translation and defunctionalization to a higher-order evaluator expressed in a functional meta- language, it leads to an implementation, in the same meta-language, of the cor- responding abstract machine [22]. However, the two program transformations can be inverted and, as first observed by Danvy [11], starting with an imple- mentation of an abstract machine, one can obtain a higher-order compositional evaluator, in the style of a valuation function of denotational semantics [23], that abstractly and concisely embodies the low-level intricacies of the machine, typi- cally scattered all over the transition rules. Such an evaluator can then be locally modified according to one’s needs and a new abstract machine can be derived from it. This approach has proven extremely successful at numerous occasions and it appears to be considerably more systematic and effective than groping for the right changes directly at the level of the abstract machine. The goal of this work is to derive an abstract machine that can be seen as a strong CbV counterpart of Cr´egut’s machine for normal order which avoids needless reevaluation of function arguments. Rather than directly tweaking the KN machine we propose to take a systematic approach following Danvy’s recipe and (1) we first deconstruct KN into a compositional evaluator, (2) we then modify this evaluator accordingly to account for CbV, and (3) from the new evaluator we derive a new abstract machine. Our meta-language is a small subset of OCaml [19]. In the process, we identify the reduction semantics of the rrCbV variant of a strong CbV strategy in the pure lambda calculus which we also present. In the terminology of Biernacka et al.’s, this is a hybrid strategy that uses three kinds An Abstract Machine for Strong Call by Value 3 of contexts, and it subsumes as a substrategy the weak right-to-left strategy of Accattoli et al.’s fireball calculus. As an application of the machine we also show how to check convertibility of terms by their partial normalization where we can stop the machine and compare computed prefixes of normal forms. Thus, the contributions of this paper include: 1. a full systematic derivation of the machine from a CbV evaluator, 2. a presentation of an abstract machine for strong CbV that is a counterpart of Cr´egut’s KN machine, 3. a reduction semantics for a strong CbV strategy, 4. an application of the machine to convertibility checking. Outline. In Section 2 we recall the KN machine and present the NbE function obtained by its deconstruction. In Section 3 we present the machine derived from the evaluator, and in Section 4 the corresponding reduction semantics. In Section 5 we prove the correctness of the machine with respect to the semantics, and in Section 6 we conclude. Supplementary materials. The full derivations can be found at https://bitbucket.org/pl-uwr/scbv-machine 2 Deconstruction of the KN Machine In this section we highlight the endpoints of the derivation: the KN machine, and the resulting evaluator obtained from an OCaml encoding of the machine. The main steps in the derivation are: disentangling the abstract machine into a defunctionalized form, refunctionalizing the stacks of the machine into continua- tions, mapping the continuation-passing evaluator to direct style, and refunction- alizing the closures the direct-style evaluator operates on into their functional representation [11]. All these transformations are described in detail in the sup- plementary materials. 2.1 Specification of the KN Machine Cr´egut’s KN machine is shown in Figure 1. Due to the lack of space we do not discuss its architecture here; we refer the reader to the original paper [10] (which also includes a nice introduction to de Bruijn indices and levels) or to a more modern presentation in [16]. We also discuss all transitions of the machine in our supplementary materials. The presentation here is slightly optimized compared to the original, and it coincides (on closed terms) with later presentation introduced by Munk [20]. The machine is in strong bisimulation with the original one, but the latter threads more redundant information: the parameter m in configurations is exactly the number of lambda frames in the current stack and need not be saved in stack frames. 4 M. Biernacka et al. Syntax: Terms ∋ T ::= n | T1 T2 | λT TN ::= T | V (n) Closures ∋ C ::= [TN ,E] Envs = Closures ∗ ∋ E ::= • | C :: E Frames ∋ F ::= [T,E] | λ | T Stacks = Frames ∗ ∋ S ::= • | F :: S Confs ∋ K ::= hTN ,E,S,mi| hS,T,mi Initial state (for closed terms): IKN : T 7→ hT, •, •, 0i Transition rules: hT1 T2,E,S1,mi→hT1, E, [T2,E] :: S1,mi (1) ′ ′ ′ ′ hλT,E, [T ,E ] :: S1,mi→hT, [T ,E ] :: E,S1,mi (2) hλT,E,S2,mi→hT, [V (m + 1), •] :: E,λ :: S2,m + 1i (3) ′ h0, [T,E] :: E ,S1,mi→hT,E,S1,mi (4) hn + 1,C :: E,S1,mi→hn,E,S1,mi (5) hV (n),E,S1,mi→hS1,m − n,mi (6) h•,Tnf, 0i→ Tnf (7) ′ ′ ′ ′ h [T ,E ] :: S1,Tneu,mi→hT ,E ,Tneu :: S1,mi (8) hλ :: S2,Tnf,mi→hS2,λTnf,m − 1i (9) hTneu :: S1,Tnf,mi→hS1,Tneu Tnf,mi (10) Fig. 1. Rules for the KN machine The machine operates on lambda terms with de Bruijn indices used to rep- resent bound variables in the standard way. Things get more complicated when we want to reduce open terms or reduce under lambdas, where we need to care for free variables. In the KN machine this is done using de Bruijn levels which represent the number of enclosing lambda abstractions from the root of the term to the current variable occurrence, and such abstract variables are formed with a different constructor V (n).