Quick viewing(Text Mode)

A Linear-Time Self-Interpreter of a Reversible Imperative Language

A Linear-Time Self-Interpreter of a Reversible Imperative Language

Information and Media Technologies 11: 160-180 (2016) reprinted from: 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

推薦論文● PPL 2015

A Linear-Time Self-Interpreter of a Reversible Imperative Language

Robert Gl¨uck Tetsuo Yokoyama

A linear-time reversible self-interpreter in an -Turing complete reversible imperative language is presented. The proposed imperative language has reversible structured control flow operators and symbolic tree- structured (S-expressions). The latter data structures are dynamically allocated and enable reversible simulation of programs of arbitrary size and space consumption. As self-interpreters are used to show a number of fundamental properties in classic computability and complexity theory, the present study of an efficient reversible self-interpreter is intended as a basis for future work on reversible computability and com- plexity theory as well as theory for reversible . Although the proposed reversible interpreter consumes superlinear space, the restriction of the number of variables in the source language leads to linear-time reversible simulation.

Examples of recently shown properties include the 1 Introduction structured program theorem [32] and the universal- Reversible computing is a relatively young area of ity of Turing machines [2]. that studies the theory and prac- This paper deals with the question of the exis- tice of forward and backward computation on all tence of linear-time self-interpretation in reversible levels of the computation stack ranging from logic computing (i.e., efficient interpretation in the sense circuits and computer architectures to program- of Jones [16]). Linear-time interpreters are used ming languages and (e.g. [8][29][34]). in computability and complexity theory. Among Reversible programming languages compute only others, they are used to show the existence of a injective functions [2] owing to the forward and linear-time complexity hierarchy along the lines backward determinism of the underlying compu- of Jones [16][17] and of Levin’s optimal univer- tation model. This means that there is a unique sal search [18][13]. Although linear-time self- relation between the input and the output of a re- interpretation is a good property to have in any versible program. The validity of several funda- computation model, no such self-interpreter is mental programming language theories, whose ir- known in some models (cf. a universal Turing ma- reversible counterparts are often taken for granted, chine as fast as that with a logarithmic-time over- has not yet been examined in reversible computing. head is known [19]), whereas it is for the pure un- typed lambda calculus [21] and for Jones’ imper- ative I language [16]. However, the existence of 可逆命令型言語の線形時間自己解釈系 linear-time self-interpreters in irreversible comput- ロバート・グリュック , コペンハーゲン大学計算機科学 ing does not imply the corresponding result for re- 部, DIKU, Department of Computer Science, Uni- versity of Copenhagen. versible computing. 横山哲郎, 南山大学理工学部ソフトウェア工学科 , Depart- We begin by introducing the language R-WHILE, ment of , Nanzan University. a reversible version of Jones’ WHILE language. The コンピュータソフトウェア ,Vol.33, No.3 (2016),pp.108–128. language has structured control flow operators [研究論文] 2015 年 7 月 30 日受付. and symbolic tree-structured data (S-expressions

160 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology known from Lisp). The latter allows for the mod- and their input and output are written in type- eling of many familiar data structures in a simple writer font. If p is a program written in the lan- way, which is more difficult in existing reversible guage S,then[[p]] S denotes the meaning of p, i.e., imperative languages such as Janus, which only the input–output function that p computes. Thus, provide integers, arrays, and stacks. It was only the output d =[[p]] S(d) results from running p on recently shown how to represent and manipulate bi- the input d. It is undefined if the program does not nary trees in the heap of a reversible random-access terminate. machine [3]. We shall see that a linear-time self- We assume that programs compute with trees interpreter exists in R-WHILE-M,anr-Turingcom- built from a finite set of atoms. The data domain D plete subset of the language R-WHILE in which the of a program is the smallest set that contains the fi- number of variables is bounded. nite set of atoms and all of the pairs (d1.d2),where

The reversible structured control flow operators d1, d2 ∈ D. of the reversible imperative reversible language R-WHILE-M are instances of those of the reversible 2. 1 Standard Interpreter flowchart language [34]. There are other r-Turing An L-program int is an interpreter of a language complete languages with reversible structured con- S in the language L iff for any S-program p and S- †1 trol flow operators such as Janus and R [9].How- data d and d , ever, none of these languages have tree-structured [[ int]] L(p.d)=d ⇐⇒ [[ p]] S(d)=d . (1) data, and they explicitly limit the number of vari- Following the convention [17], whenever a program ables and constants. is an input into another program, we regard it as Moreover, the reversibility of R-WHILE will be the corresponding data representation. For exam- shown from a new viewpoint, namely by using its ple, p on the right-hand side is an S-program text, . This simplifies the proof whereas p on the left-hand side is a representation of this defining property as compared to the pre- of the text as L-data (e.g., as tree-structured data). vious approach used for the reversible language The language S is the source language,andL is Janus [35]. the implementation language of int. If the source The work presented here is part of a larger ef- language and the implementation language are the fort on advancing reversible computing systems on same, S = L; then, int is a self-interpreter. all levels, including the design and physical realiza- tion of reversible logic circuits [8][29][25], reversible 2. 2 Reversible Interpreter computer architectures [4][28], and programming The usual definition of an interpreter in Eq. (1) languages and abstract machines [1][20][22]. does not carry over to interpreters written in a re- To demonstrate our results, a complete online versible language. Let us look at this surprising interpreter for R-WHILE, an implementation of the and important point more closely. Suppose that linear-time reversible self-interpreter in R-WHILE-M, the implementation language L is a reversible lan- and the program inverter are available from http: guage, which means that all L-programs compute //tetsuo.jp/rwhile-playground/. injective functions. Thus, for all L-programs p and

data d1, d2,wehave L L ⇒ . 2 Standard vs. Reversible Interpreters [[ p]] (d1)=[[p]] (d2)= d1 = d2 (2) That is, whenever the output of two applications We define the notion of a reversible interpreter of the same program is identical, so must be their and its efficiency. We find the fundamental differ- input. In particular, consider the L-program int, ences between standard and reversible interpreters which computes an injective function because of the already on this abstract level and even more when reversibility of L. For all S-programs p , p and data we later consider the actual construction of re- 1 2 versible interpreters. We begin by recalling the standard definition of an interpreter and then dis- †1 Following the notational convention [17], we omit cuss the properties of reversible interpreters. the outermost brackets of data if it appears as the argument of a semantic functions, e.g., we write We use the notation presented in [17]: Programs [[ int]] L(p.d) instead of [[int]] L((p.d)).

161 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology d,wehave A reversible interpreter that does not require any L L ⇒ . [[ int]] (p1.d)=[[int]] (p2.d)= (p1.d) = (p2.d) output in addition to the representation of the pro- (3) gram and its output is said to be functionally clean. This implies that the program representation of the Thus, an interpreter that returns an extra trace is two S-programs is textually identical: p1 = p2.Us- not functionally clean. S ing this implication and Eq. (1), we get [[p1]] (d)= S ⇒ [[ p2]] (d)= p1 = p2 from Eq. (3). That is, S- 2. 3 Running Time Function programs that compute the same result for some In analogy to the standard terminology, the effi- d must have the same program representation as ciency of programs written in a reversible language the L-data argument for int. However, source lan- L is defined by a running time function L guages that do satisfy this condition are too re- time : L-programs → (L-data → N⊥), (5) L strictive and not very expressive. For example, where timep(d) is the number of steps to compute p either the injective function f1(x)=(x.nil) or with the input d, provided the computation termi- f2(x)=(nil.x) cannot be computed in such a lan- nates; otherwise, undefined ⊥.WedenoteN ∪{⊥} guage since f1(nil)=f2(nil), and the programs by N⊥ (see Sect. 3. 3). implementing f1 and f2 will not be syntactically Finally, the definition of an efficient (i.e., linear- equivalent. time) reversible interpreter in the sense of Jones co- The issue described above cannot be avoided incides with the standard case [17]. The constant when a universal reversible Turing machine (RTM) in the definition may depend on the reversible in- is considered, i.e., a self-interpreter for a reversible terpreter rint but not on the source program p or language [2]. A universal RTM needs to preserve data d. the machine it simulates in the output [19].These Definition 2. A reversible interpreter rint of the considerations lead to the following definition of a language S in the language L is linear-time if there reversible interpreter. exists a constant c such that for any S-program p Definition 1. Let L be a reversible language. and S-data d, L S Then, an L-program rint is a reversible interpreter timerint(p.d) ≤ c · timep(d) . (6) of the language S in L iff for any S-program p and S-data d and d , 3 A Structured Reversible Language [[ rint]] L(p.d)=(p.d ) ⇐⇒ [[ p]] S(d)=d . (4) This section presents the concise reversible lan- The reversible interpreter pairs p and d in the out- guage R-WHILE, a structured imperative language put, and it becomes symmetric: the input and out- with tree-structured data. We define its syntax put formats of the interpreter are the same. (Fig. 1), denotational semantics (Fig. 2), and a The definition also implies that every S-program syntax-directed program inverter (Fig. 4), which computes an injective function. Otherwise, an S- we will use later for the construction of the self- program p and the data d , d exist s.t. [[p]] S(d )= 1 2 1 interpreter. The language is based on Jones’ ir- [[ p]] S(d )andd = d . By Eq. (4), we have 2 1 2 reversible language WHILE [17]. We adopt denota- [[ rint]] L(p.d )=(p.[[ p]] S(d )) 1 1 tional semantics for ease of comparison, as the se- = (p.[[ p]] S(d )) 2 mantics of WHILE is defined in a denotational man- =[[rint]] L(p.d ) . 2 ner. We assume that the reader is familiar with the However, (p.d ) = (p.d ) because d = d . There- 1 2 1 2 notions of denotational semantics (e.g., [30]). fore, [[rint]] L is not an injective function, and L can- not be reversible, which contradicts the assumption 3. 1 Example Program: List Reversal that L is reversible. Thus, the source language S S S A short example program reverse for list rever- must have a semantic function [[·]] such that [[p]] is †2 sal illustrates several language features . injective for any S-program p. Simulating irrevers- read X; (* reverse list X *) ible languages in reversible languages requires an from (=? Y nil) injectivization of the semantic function of the irre- versible language. For example, this can be done by †2 Here, we omit the do branch of the loop. See adding a trace to the output of the interpreter [24]. Sect. 3. 2.

162 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

Expressions E, F ::= X | d | cons E F | hd E | tl E | =?EF Patterns Q, R ::= X | d | cons Q R Commands C, D ::= X^=E | Q<=R | C; D | if E then C else D fi F | from E do C loop D until F Programs P ::= read X; C; write Y Fig. 1 Syntax of R-WHILE.

loop (Z.X) <= X; d, e, f,... ∈ D and X, Y, Z,... ∈ Vars.Forthe Y <= (Z.Y) sake of readability, we will use more than one atom until (=? X nil); in the programs. For example, the three atoms write Y ’a, ’b, ’c can be encoded in D as nil, (nil.nil), The input of the program is read into the vari- (nil.(nil.nil)). able X, and the output is written from the vari- An expression is either a variable X,aconstant able Y. All variables are initially set to nil.The d,ortheapplicationofanoperator(theselectors reversible loop from ...until is entered by assert- hd and tl, constructor cons, and equality test =?). ing that Y is nil and exited when X is nil.The Patterns are a subset of expressions: a variable X,a commands in the loop body are repeated as long data element d, or a pair of patterns cons Q R.For as the entry assertion and the exit test are false. the sake of readability, we denote a pair of patterns The first command in the loop, a reversible re- cons Q R by (Q.R). Patterns must be linear, i.e., placement, deconstructs the value of X into its head not contain repeated variables. The language has and tail components; the second reversible replace- no local variables. ment constructs a value by pairing the values of In a reversible assignment X^=E,thevariable Z and Y. A reversible replacement sets the vari- X on the left-hand side must not occur in the ex- ables on the right-hand side to nil before bind- pression E on the right-hand side. A reversible re- ing the original value of the right-hand side to the placement Q<=Rupdates the variables in Q using variables on the left-hand side (e.g., Z is nil af- the values of the variables in R. In contrast to an ter the replacement Y <= (Z.Y)). All variables ex- assignment, a variable can appear on both sides of cept the output variable Y must finally be nil. a replacement in the linear patterns Q and R (e.g., The application of the program to reverse a list is the variable Y in the replacement Y <= (Z.Y)). denoted by [[reverse]] R-WHILE(’a.(’b.(’c.nil)))= The two structured control-flow operators of the (’c.(’b.(’a.nil))), where the list elements ’a, language are conditional if E then C else D fi ’b,and’c are atoms. Because of the entry as- F and loop from E do C loop D until F.Loops sertion and reversible replacements, the program is have an additional entry assertion E, and condi- reversible. tionals have an additional exit assertion F.Tests and assertions have the same syntax, and both are 3. 2 Syntax evaluated at run time. For readability, we often The syntax of the language is simple (Fig. 1). A omit the branches of conditionals and loops that program P has a unique entry and exit point (read, contain only skip commands (e.g., nil <= nil). write) and a command C as its body. The input As usual, we write the list and output of a program is read into and written (d1 d2 ··· dn) from the variables in the entry and exit points. for (d1.(d2.(···.(dn.nil)···))), and the im- The data domain D of a program is the small- proper list est set that contains the atom nil and all pairs (d1 ··· dn-1.dn)

(d1.d2) ∈ D if d1, d2 ∈ D. Vars is an infinite for (d1.(···.(dn-1.dn)···)). set of variable names. We use the conventions Similar to the notation for lists, we write the pat-

163 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

E σ E σ E σ E σ [[ d]] = d [[ cons E F]] = ( [[ E]] . [[ F]] ) E[[ X]] σ = σ(X) true if E[[ E]] σ = E[[ F]] σ E σ E σ E[[ =? E F]] σ = [[ hd E]] = e if [[ E]] = (e.f) false otherwise E[[ tl E]] σ = f if E[[ E]] σ = (e.f)

Q[[ d]] σ =(d,σ) Q[[ X]] ( σ {X → d})=(d,σ {X → nil})

Q[[ cons Q R]] σ =((d1.d2),σ2) where (d1,σ1)=Q[[ Q]] σ ∧ (d2,σ2)=Q[[ R]] σ1

C[[ X^=E]] ( σ {X → d})=σ {X → d E[[ E]] σ} −1 C[[ Q<=R]] σ = Q[[ Q]] (Q[[ R]] σ) C σ C C σ [[ C; D]] = [[ D]] ( [[ C]] )   if E then C σ if E[[ E]] σ = true ∧ σ = C[[ C]] σ ∧E[[ F]] σ = true C σ = elseDfiF σ if E[[ E]] σ = false ∧ σ = C[[ D]] σ ∧E[[ F]] σ = false   fromEdoC C σ = σ if E[[ E]] σ = true ∧ σ = fix(F )(σ) loop D until F where F (ϕ)={ (σ, σ1) | σ1 = C[[ C]] σ ∧E[[ F]] σ1 = true }∪ { (σ, σ3) | σ1 = C[[ C]] σ ∧E[[ F]] σ1 = false ∧ σ2 = C[[ D]] σ1 ∧E[[ E]] σ2 = false ∧ σ3 = ϕ(σ2) }

P P P[[ P]] D = D if P is read X; C; write Y ∧C[[ C]] ( σX (D)) = σY (D ) Fig. 2 Denotational semantics of R-WHILE. tern list The set of all stores for P is denoted by StoresP. ··· (Q1 Q2 Qn) The store σ\X is identical to σ,exceptthatitmaps for (Q1.(Q2.(···.(Qn.nil)···))), and the im- the variable X to ⊥.Thestoreσ σ is the disjoint proper pattern list union of the bindings in the stores σ and σ,which ··· (Q1 Qn-1.Qn) have no variables in common. The undefined store, for (Q1.(···.(Qn-1.Qn)···)). which returns ⊥ for any variable, is also written as

The expression (list E1 ··· En) will be used as ⊥. ··· P shorthand for the expression (cons E1 (cons En The store σX (D) binds the variable X to D and all ··· ··· nil) ), and the expression (cons* E1 En-1 En) other variables in Var (P)={X, Y1,...,Yn} to nil: ··· P is used for the expression (cons E1 (cons En-1 σX (D)=[X → D, Y1 → nil,...,Yn → nil] . (7) En) ···). The semantic functions are defined for ex- pressions, patterns, commands, and programs †3 3. 3 Semantics (Fig. 2) : P We define the denotational semantics for the lan- E : Expressions → (Stores → D⊥) P P guage R-WHILE. We begin with some notation and Q : Patterns → (Stores → D⊥ × Stores⊥) P P then explain the four semantic functions in Fig. 2. C : Commands → (Stores → Stores⊥) The sets of variables occurring in a program P P : Programs → (D → D⊥) P and an expression E are denoted by Var (P)and A program p denotes the partial function [[ p]] . We R-WHILE Var (E), respectively. Boolean values are denoted sometimes write [[p]] instead to explicit by two distinct elements false, true ∈ D for read- that p is a program in the language R-WHILE. ability. The undefined value is denoted by ⊥,and The expression evaluation E evaluates an expres- σ D asetX ∪{⊥} is denoted by X⊥.WeuseD⊥ as the sion E in a store to a value in ⊥. The evaluation value domain. † A B Astoreσ for P is a function from Var (P)toD⊥. 3 An injective mapping from to is denoted by A→ B.

164 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology of a variable X, constant d andtheapplicationofan Applying a reversible assignment X^=Etwice operator such as head and tail selectors hd and tl, in a row restores the original value of X. This holds the pairing cons, and the equality test =? are as because for any d and e,ifd  e is defined, then expected. E can evaluate to ⊥ because the values (d  e)  e = d . (10) of hd E and tl E are undefined if the value of E is A reversible assignment is an instance of a re- nil. versible update [31]. The operator  is injective The pattern evaluation Q evaluates a pattern Q in its first argument; it is not commutative. in a store σ to a value-store pair. Every variable Remark: The reversible assignment operator ^= used in Q is bound to nil in the resulting store. is reminiscent of the bitwise exclusive-or assign- This moves every value used in constructing the ment operator in Janus [35], which zero-clears the resulting value out of the store. We have Q[[ Q]] σ = value of the variable on the left-hand side if it is (E[[ Q]] σ, σ)forsomeσ. For any pattern Q, the deno- equal to the value of the right-hand side expres- tation Q[[ Q]] is an injective function. Thus, the in- sion. Unlike the Janus assignment operator, which −1 verse function, Q[[ Q]] , is unique. The inverse pat- always returns a valid store in constant time given −1 tern evaluation, Q[[ Q]] (d,σ), takes a value-store the value of the right-hand-side expression (the val- pair to a new store in which all of the variables in Q ues are fixed-size integers), the R-WHILE assignment are bound to the corresponding component of the operator can return an undefined store ⊥ and take value d, provided those variables were bound to nil time depending on the unbounded size of the tree- −1 in the store σ.WehaveQ[[ Q]] (Q[[ Q]] σ)=σ if Q structured data owing to the equality test. is defined for Q and σ. Pattern evaluation and its A reversible replacement Q<=R updates the inverse will be used for defining the semantics of variables in the linear patterns Q and R on both sides reversible replacements. of the replacement. C[[ Q<=R]] σ is defined by first We now turn to the command evaluation C and evaluating Q[[ R]] σ,whichresultsin(d,σ), where explain some design choices. Assignments in an d = E[[ R]] σ and σ is σ,exceptthatallvariables irreversible language are destructive. They over- in Var (R)arenil. This is followed by the inverse −1 write the value of the variable on the left-hand side, evaluation Q[[ Q]] (d,σ), which yields σ—the fi- and the original value cannot be reconstructed af- nal store of the reversible replacement—in which ter the assignment. Thus, they cannot be used in all variables in Var (Q), which must be nil in σ, a reversible language (unless one logs the original are bound to parts of d such that d = E[[ Q]] σ. values to injectivize the computation). Instead, we The computational structure defining a re- use a reversible assignment X^=Ethat sets X to versible replacement resembles the original Bennett the value of E if X is nil and sets X to nil if the method [6] without copying where d is the interme- values of X and E are equal. In the former case, the diate garbage. However, the forward computation −1 value of E is duplicated, and in the latter case, the Q[[ R]] and backward uncomputation Q[[ Q]] are not equality of the values of X and E is asserted. inverses of each other. On the other hand, the value We formalize the semantics by defining an update of R before the operation matches the value of Q af- operator :  ter the operation. Specifically, for any σ and σ , e if d = nil C[[ Q<=R]] σ = σ ⇐⇒ E [[ R]] σ = E[[ Q]] σ . (11) d  e = (8) nil if d = e = nil . We note that a reversible replacement can be sim- A reversible assignment can then be defined by ulated in R-WHILE by using several reversible assign- C[[ X^=E]] ( σ [X → d]) = σ [X → d ], (9) ments and auxiliary variables. However, reversible where d = d E[[ E]] σ.Notethatσ,inwhichE replacements are convenient for programming and is evaluated, contains no binding for X on the left- lead to shorter programs. hand side. This is ensured by the disjoint union At a reversible conditional if E then C else D σ [X → d]. Hence, if X occurs in E, the assignment fi F, the control flow branches depending on the is undefined. If this were not the case, an invalid test E.Iftrue, the command C is executed, and the assignment X^=Xwould set X to nil for any value assertion F must be true.Iffalse, the command of X,andC[[ X^=X]] would not be injective. D is executed, and the assertion F must be false. If the value of F at the exit does not correspond to

165 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

X <= (nil.X) (* increment X *)  l (nil.X) <= X (* decrement X *) I l   l A syntax-directed program inverter for R-WHILE   is defined in Fig. 4. Each command is inverted lo-

l  cally by a recursive descent over the program struc- l   ture. Reversible assignments are self-inverse; there- l fore, no change is needed. The patterns in a re- Fig. 3 A flowchart for a reversible while loop. versible replacement switch sides. The inverse of a command sequence is the reversed sequence of its the value of E at the entry, the conditional is unde- inverted commands. Tests and assertions change fined. The assertion F ensures that the of roles in control-flow operators. A program is in- a conditional is backward deterministic. Both ex- verted by swapping the input and output variables pressions, E and F, are evaluated at run time. The and inverting the body. definition of the semantics is straightforward. For example, we obtain the inverse program of I A reversible loop from E do C loop D until F our example program by [[ reverse]] : can be illustrated by a reversible flowchart read Y; (* inverse of list reversal *) (Fig. 3) [32]. The informal semantics is as follows. from (=? X nil) At the entry of the loop, the assertion E must be loop (Z.Y) <= Y; true, indicated by t in the flowchart, and the com- X <= (Z.X) mand C is executed. Then, if the test F is true,the until (=? Y nil); loop terminates. If F is false, the command D is write X executed, and the assertion E must be false, indi- The inverse program performs the same function cated by f in the flowchart. If the value of E does as the original program, which is not surprising be- not match the required value, the loop is undefined. cause list reversal is self-inverse. The inverse pro- Different types of loops can be programmed with gram is textually identical to reverse,exceptthat this general loop construct. For example, when C the variables X and Y switched places. (Clearly, this is a skip command, the loop becomes a while loop; is not always the case for an inverse program.) The when D is a skip command, the loop becomes a inversion of programs, such as reverse, is straight- do-until loop. In the semantics, the stores before forward, and we will use it extensively for building and after loop execution are related by the least the reversible interpreter in the next section. I fixed point of F . The function F (ϕ) is defined by Owing to the syntactic nature of , applying it I I the union of the two sets, which correspond to the twice returns the original program P = [[ [[ P]] ]] . A termination and iteration of the loop execution. program and its inverse take the same number of Labels l1–l4 in Fig. 3 will be used later in Sect. 4 computation steps. For any P and d,wehave time ( )=time ([[ ]] ( )) . (12) to simulate loops in the reversible interpreter. P d I[[ P]] P d We prove the correctness of I by showing the 3. 4 A Tool: A Program Inverter correctness of command inversion and then that of A program inverter is usually not available as a program inversion. The following lemma implies I in a conventional language (cf. that is a program inverter for R-WHILE. [11][12]) but is straightforward to define for R-WHILE Lemma 1. For any R-WHILE command C and stores σ σ σ C σ σ C I σ (cf. [34]). It is useful for the construction of the and ,if = [[ C]] ,then = [[ [[ C]] ]] . programs in this paper. Proof. If σ = ⊥, the statement holds vacuously. We immediately have the inverse program in a Otherwise, we use induction over the number of reversible language. A simple example is a replace- times used to obtain σ by unfolding the seman- ment that pairs nil with the value of X.IfX is a tic function C. We use an induction hypothesis in unary number (consisting of a list of nil), this in- which all occurrences of C on the right-hand side crements X. Then, the inverse of the replacement in Fig. 2 satisfy the statement and show for each decrements X. case that the left-hand side satisfies the statement.

166 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

I[[ X^=E]] = X^=E I[[ Q<=R]] = R<=Q I[[ C; D]] = I[[ D]] ; I[[ C]] I[[ if E then C else D fi F]] = if F then I[[ C]] else I[[ D]] fi E I[[ from E do C loop D until F]] = from F do I[[ C]] loop I[[ D]] until E I[[ read X; C; write Y]] = read Y; I[[ C]] ; write X

Fig. 4 A syntax-directed program inverter for R-WHILE.

(The case for loops uses induction on the natural 3. 5 Reversibility of R-WHILE numbers. This simplifies the correctness proof of For a language to be reversible, it should be injec- a program inverter [35], which uses induction on tive at a level of certain atomic constructs so that proof trees constructed by operational rules.) computational sequences of atomic constructs are In the case of a reversible assignment, as- locally forward and backward deterministic. This sume that σ = C[[ X^=E]] σ holds. Then, atomic level of reversible execution differs for con- C[[ I[[ X^=E]] ]] σ = σ[X → (σ(X) E[[ E]] ( σ\X))  crete languages. We will show that R-WHILE is re- E[[ E]] ( σ\X)] = σ, where the last equality holds by versible at the level of commands, which is the nat- Eq. (10). In the case of a reversible replacement, ural level of execution for an imperative language. by the definition of C,wehave The following theorem shows that the execution of σ C σ ⇐⇒ σ Q −1 Q σ Q = [[ Q<=R]] R Q =( [[ Q]] ) ( [[ R]] R) any command is injective, even though expression ⇐⇒ σ Q −1 Q σ R =( [[ R]] ) ( [[ Q]] Q) evaluation is not injective. The injectivity of com- ⇐⇒ σ C σ . R = [[ R<=Q]] Q mands implies that of programs. σ C σ σ C σ This implies = [[ Q<=R]] iff = [[ R<=Q]] . Theorem 1. For any R-WHILE command C and any In the case of a reversible conditional, a simple stores σ1 and σ2,ifC[[ C]] σ1 = C[[ C]] σ2,thenσ1 = σ2. case analysis proves the correctness. Proof. By Lemma 1, we have the desired equality In the case of a reversible loop, assume that σ = σ1 = C[[ I[[ C]] ]] ( C[[ C]] σ1)=C[[ I[[ C]] ]] ( C[[ C]] σ2)=σ2 . C[[ from E do C loop D until F]] σ holds. Then, E σ by unfolding, we have the entry condition [[ E]] = Corollary 2. For any R-WHILE program P and any E σ true, the exit condition [[ F]] = true,andthe data d and d,ifP[[ P]] ( d)=P[[ P]] ( d), then d = d. store update by the loop body σ = fix(F )σ. By induction on n (and the induction hypothe- Proof. Let P be read X; C; write Y. Assume P P C σP sis on the number of unfoldings of the seman- [[ P]] ( d)= [[ P]] ( d ). By definition, [[ C]] ( X (d)) = n C σP σP σP tic function C), if σ2 = F (∅)(σ1), then σ1 ∈ [[ C]] ( X (d )), and by Theorem 1, X (d)= X (d ). n σP F (∅)(σ2) for all σ1,σ2. Here, F is defined by Since X is injective, the equality d = d holds. F ,inwhichC and D are replaced with I[[ C]] a n d I[[ D]], respectively. Therefore, if σ = fix(F )σ, 3. 6 Rewrite Rules as Syntactic then σ = fix(F )σ. By the definition of I and Programs often become more readable by using the evaluation of E and F above, we have σ = a reversible version of the rewrite rules [17] that si- ,..., C[[ I[[ from E do C loop D until F]] ]] σ . multaneously update several variables X1 Xn as syntactic sugar: The correctness of the program inverter I di- rewrite [X1,...,Xn] by Rule1 ... Rulem. rectly follows from the lemma. Rule Rulej is either Corollary 1. For any R-WHILE program P and data 1. [Q1,...,Qn] ⇒ [E1,...,En],or d and d,ifd = P[[ P]] ( d), then d = P[[ I[[ P]] ]] ( d). 2. [Q1,...,Qn] ⇒ C; . Proof. Let P be read X; C; write Y. We assume that The intended meaning is as follows. If the val- = P[[ ]] ( )holds.WehaveC[[ ]] ( σP( )) = σP( ), ,..., ,..., d P d C X d Y d ues of X1 Xn match the patterns Q1 Qn of C I σP σP and by Lemma 1, [[ [[ C]] ]] ( Y (d )) = X (d). By aruleRulej, which are tried sequentially, the vari- σI[[ P]] σP P I Y = Y ,wehave [[ [[ P]] ]] ( d )=d. ables in Qi are bound. Then, depending on the

of Rulej,eitherX1,...,Xn are bound to the

167 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

Reversible rewriting:

from (=? T nil) loop rewrite [ T, T,N] [ (’(l).T), T,N]⇒ [ T, cons ’(l) T, cons nil N ] [ ((’n L.R).T), T,N]⇒ [ cons* L R ’(m) T, cons ’(m) T,N] [ (’(m).T), (R L ’(m).T),N]⇒ [ T, cons (cons* ’n L R) T,N] until (=? T nil)

R-WHILE: A binary tree and its representation:

from (=? T nil) loop /\ if (=? ’(l) (hd T)) then (l)/\ (’(l).T) <= T; (* leaf *) (l) (l) T <= (’(l).T); N <= (nil.N) t = (’n ’(l).(’n ’(l).’(l))) else (* node *) if (=? ’n (hd (hd T))) then ((’n L.R).T) <= T; Rewriting steps for t (3 leaves): T <= (L R ’(m).T); T <= (’(m).T) [ ((’n ’(l).(’n ’(l).’(l)))), nil, nil ] ⇒ else (* ascent *) [ (’(l) (’n ’(l).’(l)) ’(m)), (’(m)), nil ] ⇒ (’(m).T) <= T; [ ((’n ’(l).’(l)) ’(m)), (’(l) ’(m)), (nil1)]⇒ (R L ’(m).T)<=T; [ (’(l) ’(l) ’(m) ’(m)), (’(m) ’(l) ’(m)), (nil1)]⇒ T <= ((’n L.R).T) [ (’(l) ’(m) ’(m)), (’(l) ’(m) ’(l) ’(m)), (nil2)]⇒ fi (=? ’(m) (hd T)) [ (’(m) ’(m)), (’(l) ’(l) ’(m) ’(l) ’(m)), (nil3)]⇒ fi (=? ’(l) (hd T)) [ (’(m)), ((’n ’(l).’(l)) ’(l) ’(m)), (nil3)]⇒ until (=? T nil) [ nil, ((’n ’(l).(’n ’(l).’(l)))), (nil3)]

Fig. 5 Example: Counting the leaves in a binary tree. The reversible rewriting rules are syntactic sugar that is expanded into R-WHILE. The steps of an example run are shown.

values of E1,...,En, correspondingly, or the com- tree structures and to perform more involved cal- mand C is executed, which may also update Xi.Pat- culations. In the reversible interpreter, we will use terns with repeated variables match if the variables this method to compute, among others, the value of match with the same value. an expression given as an .We Similar to the orthogonality condition of case ex- now describe the reversible rewrite rules and their pressions [10], the range of the output of a rule implementation in R-WHILE. must statically differ from the output of all previ- A rewrite tuple [T,T,N] is a stack of trees T,a ous rules in order for the rewriting to be injective stack of processed trees T,andacounterN.The and thus must be implementable by expansion into initial tuple is [(t),nil,nil], and the final tuple nested reversible conditionals. We do not require a is [nil,(t),n],wheret is the representation of a general expansion mechanism. We only use rules as binary tree, as shown in Fig. 5, and n is the number syntax sugar where atoms make the left and right of leaves in t (a unary number). The iteration of sides statically orthogonal and thus easily expand- the rules is implemented by a reversible loop. able. There are three cases. If a leaf ’(l) is on top Example rewriting: tree traversal of T, the counter N is incremented, and the leaf is The example in Fig. 5 illustrates R-WHILE and the moved to T. When an internal node tagged ’n is use of the reversible rewrite rules for counting the on top of T, its two subtrees L and R and a marker leaves in a binary tree by performing an in-order ’(m) are placed on T. The marker is also placed on tree traversal. The same reversible programming T to make the right side disjoint from the other method can be used to traverse more complicated rules. After the subtrees are traversed, the marker

168 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

’(m) is on top of T,andthenodeisreassembledon BENNETT(C,COPY) ≡ C; COPY; I[[ C]] T. The rewrite rules are orthogonal on both sides: macro COMP-INV(C,D) ≡ C; I[[ D]] on the left side, the tags on top of T are disjoint, macro COMP-CP-INV(C,COPY,D) ≡ C; COPY; I[[ D]] and on the right side, the tags on top of T are dis- joint. The rules define a reversible state transition Fig. 6 Bennett method and two variants. system. Owing to their orthogonality, the rewrite rules so that macro expansion terminates. are easily expanded into nested reversible condi- By allowing different reversible computations, tionals (Fig. 5). The three cases are guarded by C and D, in the first and third part, we can the tests and assertions given by the rules. There cover a wider range of program patterns. In is no error checking; all trees are assumed to be Fig. 6, we define the generalized composition macro well-formed. COMP-CP-INV(C,COPY,D) for this program pattern and its copy-free version COMP-INV(C,D) in the 4 Building a Reversible Interpreter case where no COPY is needed between the com- In this section, we introduce the Bennett mand C and the inverse command I[[ D]]. The Ben- compute–uncompute method and two variants— nett method BENNETT and the copy-free composi- the reversible store management of the interpreter tion COMP-INV are instances of the generalized com- and the data representation of R-WHILE programs. position COMP-CP-INV:

BENNETT(C,COPY) = COMP-CP-INV(C,COPY,C) 4. 1 Bennett Reversible Simulation and a COMP-INV(C,D) = COMP-CP-INV(C,SKIP,D) Generalization In a reversible computation, no information can where the macro SKIP has no computational effect. be deleted, neither data nor control-flow informa- If COPY is self-inverse, the original BENNETT(C,COPY) tion. By preserving the information lost in an ir- is also self-inverse. However, the copy-free and gen- reversible computation step, e.g., by tracing the eralized variants are not. In the next subsections, value of a variable overwritten by an assignment, we will see how the macros are used in reversible a reversible simulation of an irreversible computa- programming. tion can be obtained. When we want the result of the reversibilized computation but wish to undo 4. 2 Store Management all side effects it had on the store, e.g., the trace, We use a list vl bound to Vl to hold the values we use the Bennett method [6], a method for clean of the variables in a source program. The i-th ele- reversible programming. The original method con- ment of vl is the value of the i-th variable Xi (the sists of three parts, where the third part undoes all first element in the list is indexed by one). Initially, changes made to the store by the first part: vl is a list of nil of length n, which is the number 1. the reversibilized computation C, of variables in a program. Without loss of general- 2. saving of the result by COPY,and ity, we assume that all of the variables in a program 3. the computation of the inverse I[[ C]] . are named Xi,wherei ≥ 1. Every program has at To abstract from this frequently recurring compute– least one variable that is read and written. We ac- uncompute program pattern, the macro cess the values in Vl using four reversible macros: BENNETT(C,COPY) (Fig. 6) is used. We use such UPDATE, LOOKUP, GETV,andPUTV. C-like function macros to keep our programs short First, we specify the functionality of UPDATE and and readable. The parameterized macros are ex- LOOKUP. For any value σ(Vl)=(d1 d2 ... dn),we panded before program execution. A macro is - require, for any index I ranging over 1 to n and tually replaced by the macro body, in which formal variable X,that arguments are replaced by actual arguments and C[[ LOOKUP(I,X)]] σ = σ[X → σ(X)  di] (13) inversion of commands is carried out. The actual C[[ UPDATE(I,X)]] σ = σ[Vl → (d1 ... arguments are completely expanded before macro (di  σ(X)) ... dn)] (14) expansion. Self-referential macros are not allowed where σ(I)=i.ThemacroLOOKUP(I,X) reversibly

169 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

D D D D [[ read Xi; C; write Xj]] = ((’var. nat [[ i]] ) cmd [[ C]] (’var. nat [[ j]] ))

Dcmd [[ Xi ^= E]] = (’asn (’var.Dnat [[ i]] ).Dexp [[ E]] ) Dcmd [[ Q<=R]] = (’rep Dexp [[ Q]] .Dexp [[ R]] ) Dcmd [[ C; D]] = (’seq Dcmd [[ C]] .Dcmd [[ D]] ) Dcmd [[ ifEthenCelseDfiF]] = (’cond Dexp [[ E]] Dcmd [[ C]] Dcmd [[ D]] Dexp [[ F]] ) Dcmd [[ from E do C loop D until F]] = (’loop Dexp [[ E]] Dcmd [[ C]] Dcmd [[ D]] Dexp [[ F]] )

Dexp [[ d]] = (’val.d) Dexp [[ cons E F]] = (’cons Dexp [[ E]] .Dexp [[ F]] )

Dexp [[ Xi]] = (’var.Dnat [[ i]] ) Dexp [[ =? E F]] = (’eq Dexp [[ E]] .Dexp [[ F]] ) Dexp [[ hd E]] = (’hd.Dexp [[ E]] )

Dexp [[ tl E]] = (’tl.Dexp [[ E]] ) Dnat [[ i]] = (nil1 ··· nili)

Fig. 7 Mapping R-WHILE programs to their data representation.

We require that all temporary variables, i.e., all macro LOOKUP(J,X) ≡ BENNETT(POS(J,Y),X ^= Y) variables that are neither macro parameters nor macro UPDATE(J,X) ≡ BENNETT(POS(J,Y),Y ^= X) the store variable , are initially and finally nil- macro GETV(J,X) ≡ BENNETT(POS(J,Y),X <= Y) Vl cleared. Under this requirement, Eq. 13 and Eq. 14 macro PUTV(J,X) ≡ BENNETT(POS(J,Y),Y <= X) hold. Because the store management must be re- versible, both lookup and update first move forward macro POS(J,Y) ≡ and then backward on the value list. Since these from (=? JJ nil) loop two movements are inverses of each other, one im- (U.Vl) <= Vl; (* Move head *) plementation by combined with the Bennett Vr <= (U.Vr); (* of Vl to Vr *) POS method suffices. Between the forward and back- JJ <= (nil.JJ) (* JJ + 1 *) ward movements, the value in is used as desired. until (=? JJ J); Y The macro places the -th value of (Y.Vr) <= Vr GETV(J,X) J the value list vl in the variable X,whichmustbe Fig. 8 Store management macros. nil-cleared. Afterwards, the J-th value of the value list bound to Vl is nil. For any value σ(Vl)= updates the value of X in σ by the i-th value of vl. (d1 d1 ... dn),werequire The macro UPDATE(I,X) reversibly updates the i- C[[ GETV(J,X)]] ( σ[X → nil]) = th value of the list bound to Vl by the value of σ[X → d ][Vl → (d ... d nil d ... d )],  j 1 j-1 j+1 n the variable X. The operator is the reversible up- (15) date defined in Eq. 10. LOOKUP and UPDATE are where σ(J)=j.ThemacroGETV(J,X) has the self-inverse, a property which we will exploit as a same functionality as a sequence of lookup and up- reversible programming technique to reset values. date, LOOKUP(J,X); UPDATE(J,X) (a copy-free com- (Other data structures could be used for the value position COMP-INV(LOOKUP(J,X),UPDATE(J,X))). list vl, but the list representation does not affect However, the original method BENNETT(POS(J,Y),X the asymptotic time complexity of the interpreter <= Y) with a reversible replacement is a more direct if the number of variables in R-WHILE is fixed.) and efficient implementation in this case (Fig. 8). Fig. 8 defines the two parameterized macros The macro PUTV(J,X) is the inverse of the macro LOOKUP(J,X) and UPDATE(J,X) using the original GETV(J,X): Bennett method BENNETT parameterized by the PUTV(J,X) ≡I[[ GETV(J,X)]] . auxiliary macro POS(J,Y) in Fig. 8, where the tem- Although reversible programming does not al- porary variable JJ traverses the index of vl,and low discarding of the values left in an “unorderly the temporary variable U is used to move the head state” but requires their restoration to a well- elements bound to Vl to the head bound to Vr. defined state, as in the case of the value list vl

170 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology after a variable lookup, this does not necessarily re- is omitted (cf. Sect. 3. 2), the data representation quire writing the program text twice. The Bennett of the skip command nil <= nil is used. method and the invertibility of the programming language reduce this to a minimum, and somewhat 5 Main Program: Program Execution surprisingly, we gain a new form of modularity, as We now build the reversible self-interpreter for illustrated by these macros for store management. R-WHILE. The main challenge is to organize the in- terpreter to be fully reversible. No values may be 4. 3 Program Representation discarded, including any part of the source pro- The mapping of R-WHILE programs to their data gram. To this end, we utilize several program- representation is straightforward (Fig. 7): each ex- ming techniques: the Bennett method [6] and the pression and command is mapped to a tagged list variants we introduced above, the reversible tree so that they can be distinguished easily. Let {’var, traversal and the rewriting rules as syntactic sugar ’seq, ’asn, ’rep, ’cond, ’loop, ’val, ’cons, ’hd, (Sect 3. 6), the reversible flowchart simulation [32], ’tl, ’eq} be the set of tags. The natural numbers and the reversible programming techniques of the i and j that index variables are transformed into Janus interpreter [35]. Together, they play a cru- unary numbers represented by lists of nil.They cial role in minimizing the reversible programming are used to access the value list representing the effort. We will now describe the reversible inter- store. The mapping is injective. In the rest of the preter in more detail. paper, we assume that the largest index is used for the variable for the input and output in the pro- 5. 1 Disassembly–reassembly of programs gram representation. It is easy to transform any A conventional interpreter switches between de- program representation to this form without chang- composing and simulating parts of the source pro- ing its asymptotic time complexity. gram. Typically, while interpreting a part (e.g., Example data representation: List reversal an expression), that part is consumed to compute We show the data representation of the list re- its effect (e.g., the value of the expression). If the versal program same part is needed again later, it is taken from (x (’seq a “backup copy” of the source program. However, (’loop this classic use–delete interpretation strategy can- (’eq y . (’val . nil)) not be used in a reversible interpreter. (Deletion as (’rep (’val . nil) . (’val . nil)) in an irreversible language is not possible.) (’seq (’rep (’consz.x).x). Recall from Def. 1 that a reversible interpreter (’rep y . (’cons z . y))) has a symmetric input and output format: it reads (’eq x . (’val . nil))) . and writes program–data pairs. The data d in the (’rep x . y)) input pair (p.d) are transformed into the data d x) by simulating p, which is returned unchanged in where the data representation of the variables are the output pair (p.d). Thus, even though p needs as follows: to be decomposed, one cannot just dump its used x = (’var . (nil nil nil)) parts somewhere but has to handle them so that y = (’var . (nil nil)) p can be reassembled. (Again, a naive solution is z = (’var . (nil)) not possible: make a copy of p; after interpretation, The program is based on the list reversal program delete all dumped parts and return the copy.) in Sect. 3. 1, but the reversible replacement is in- The way the disassembly and reassembly of a serted at the last line of the body in order for the source program is handled depends on the im- variable X used for the input and output to have plementation language. The method employed in the largest index in its data representation x.We R-WHILE is the one illustrated in Fig. 5, i.e., move assume the sequence of the list reversal program is all parts onto a stack during the descent over an ab- left associative, and the structure in the data repre- stract syntax tree (expression, pattern, command) sentation is maintained as the left associative ’seq. and reassemble the tree from that stack during the Because the do branch of the list reversal program

171 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

macro -EXP(E,V,H) ≡ read PD; (* input (p.d) *) BENNETT(INIT(Cd, PD),EVAL-CMD(Cd,Cd)); LOOP(E,E ,EVAL-EXP-STEP(E,E ,St,H)); write PD (* output (p.d’) *) (V.nil) <= St

≡ macro EVAL-PAT(Q,V) ≡ macro INIT(Cd,PD) (Pgm.D) <= PD; LOOP(Q,Q ,EVAL-PAT-STEP(Q,Q ,St)); ((’var.I) Cd (’var.J)) <= Pgm; (V.nil) <= St Vl ^= I ; ≡ PUTV(I ,D) macro EVAL-CMD(Cd,Cd ) LOOP(Cd,Cd,EVAL-CMD-STEP(Cd,Cd)) Fig. 9 Reversible self-interpreter ri for R-WHILE. macro LOOP(L,L,Body) ≡ ascent. In a reversible language with RAM, such L <= (L.nil); as arrays in Janus, one can instead reversibly posi- from (=? L nil) loop tion and reposition pointers in the syntactic struc- Body ture [35]. until (=? L nil); (L.nil) <= L

5. 2 Main program of the interpreter Fig. 10 Macros for expression, pattern, and The main program of the reversible interpreter ri command evaluation. is shown in Fig. 9. It consists of a macro BENNETT that invokes the command simulation EVAL-CMD (i.e., J-th) position, the non-nil value is paired with and administers the initial and final stores of the the program representation and set to PD,andVl program. It is a reversible implementation of the is nil-cleared. semantic function P in Fig. 2: [[ri]] R-WHILE(p.d)= P[[ p]] ( d). 5. 3 Three mini-interpreters The macro EVAL-CMD(Cd,Cd ), which we explain The main components of ri are the reversible in- later, simulates a list of commands. Initially, the terpreters for the three sublanguages of R-WHILE: variable Cd holds a singleton list of the command expressions, patterns, and commands. They imple- in the body of the source program representation. ment the semantic functions E, Q,andC in Fig. 2. The command is disassembled on Cd and reassem- Just like any reversible interpreter (cf. Def. 1), they bled on Cd so that, in the end, the original com- preserve the interpreted syntactic object in their mand can be taken from the singleton list Cd and result. Their implementation follows the same pro- returned to Cd. gramming principles and uses rewrite rules to define The initialization macro INIT(Cd,PD) decom- the traversal of the abstract syntax trees. They as- poses the program–data pair (p.d) read into PD. sume that the variable Vl holds the values of the The program in Pgm is further decomposed into the variables. The macros that iterate the rewrite rules input and output variables and the command in its are shown in Fig. 10. We begin with the mini- body. The value list bound to Vl is initialized to interpreter for pattern evaluation, the smallest of be a list of nil of length n, the number of vari- the three interpreters. ables in the source program (recall that the index Pattern evaluation I as well as the index J obtained from the in- The evaluation of a pattern on the right side of a put program p is the largest index occurring in p reversible replacement builds a new value and never and that all indices are represented by unary num- discards a value (there are no selectors hd and tl bers). The input data d are moved to the I -th in a pattern). Because Q[[ q]] is injective, it suffices position in the value list bound to Vl by the store- to preserve the pattern q in the result to obtain a management macro PUTV (Fig. 8). The inverse of reversible implementation. INIT(Cd,PD) performs post-processing. If all ele- The rewrite rules of the macro EVAL-PAT-STEP in ments in the value list are nil except for the I-th Fig. 11 define a reversible state transition system

172 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology that simulates Q without the use of recursion. A that simulates E. A rewrite tuple [Ex,Ex,St,H] rewrite tuple [Q,Q ,St] consists of a stack of pat- consists of a stack of expressions Ex, a stack of pro- terns Q, a stack of processed patterns Q , and a value cessed expressions Ex, a stack of intermediate val- stack St. The rewriting rules test the next pattern ues St, and a history stack H. The rewriting rules on Q and update the state accordingly. test the next expression on Ex and update the state The traversal of a pattern works in essentially the accordingly. The value of a variable in an expres- same way as the in-order traversal of a binary tree sion is copied from Vl to V by the macro LOOKUP in Fig. 5. A pattern on Q is disassembled during the (Sect. 4. 2) and pushed on St (unlike a pattern, descent over its syntactic structure and reassem- where the value is moved). The initial rewrite tuple bled on Q during the ascent that builds the final is [(e),nil,nil,nil],wheree is the expression to value from the intermediate values on St. The ini- evaluate. The rewrite rules are applied until the tial tuple is [(q),nil,nil], and the final tuple is tuple has the form [nil,(e),(v),h],wherev is [nil,(q),(v)],whereq is the pattern to evaluate, the value of e,andh is a history stack. Except and v is its value. The store in which q is evalu- for maintaining a history stack, the rules work in ated is the value list vl. The value of a variable essentially the same way as the rules for pattern (’var.J) in a pattern is moved from Vl to V by evaluation in Fig. 11. the macro GETV (Sect. 4. 2) and pushed on St.The The iteration of the rewrite rules is implemented macro GETV swaps values. Thus, Vl is updated by by the macro EVAL-EXP in Fig. 10. The his- the evaluation, as required by the semantic func- tory stack is uncomputed by the original Bennett tion. method: BENNETT(EVAL-EXP(E,V,H),...).The Pattern evaluation by iterating the rewrite rules reader can see this in all five places in Fig. 13, where in Fig. 11 is based on the following invariant: the expressions are evaluated (assignments, condi- ∗ [(q.qs),qs ,st] ⇒ [qs,(q.qs ),(v.st)] (16) tionals, loops). iff Q[[ Q]] σ =(v,σ), where σ  vl and σ  vl Expression evaluation by iterating the rewrite hold for the value lists before and after applying rules in Fig. 12 is based on the following invariant: ∗ the rewrite rules to evaluate the representation q [(e.ex),ex ,st,h] ⇒ [ex,(e.ex ),(v.st),h ] of Q. Suppose that q is on top of qs,andqs and st (17) are some values. After a number of rule iterations, iff E[[ E]] σ = v, where the value list in which the q will be on top of qs and v on top of st,andvl is representation e of E is evaluated is σ  vl. Sup- updated. The store equivalence σ  (d1 d2 ...dn) pose that e is on top of ex,andex , st,andh are is such that σ(Xi)=di for any i. some values. After a number of rule iterations, e The iteration of the rewrite rules is implemented will be on top of ex and v on top of st. The list by the macro EVAL-PAT in Fig. 10. The rewriting vl is unchanged; the history h is updated. terminates because the pattern stack eventually be- The rules are orthogonal on both sides, as re- comes nil. The rewriting is reversible because the quired for reversible rewriting (Sect. 3. 6). On the tags on Q and Q differ on both sides. left side, all tags on top of Ex are disjoint (’var, Expression evaluation ’hd, etc.), and on the right side, all tags on top The semantic function E for expression evalua- of Ex are disjoint (’var, ’hdm, etc.). The markers tion is non-injective. For example, the evaluation that trigger the evaluation of an operation (’hdm, of an expression hd E removes the tail of the value ’tlm, ’consm, ’eqm) are placed in singleton lists; of E. To obtain a reversible implementation of E thus, all rules have a uniform format. The evalua- in R-WHILE, we preserve all values, which would be tion halts after a finite number of rewrite steps. lost if we use an irreversible evaluator, on a sepa- Command execution rate history stack H. That is, we push the tail value What remains is to define is the semantic func- to H. Because this value is irrelevant for the final tion C for command execution. As before, the re- value of hd E, it is so-called garbage data, which we versible simulation will preserve the command in uncompute afterwards using the Bennett method. the result. The rewrite rules in Fig. 13 define a re- The rewrite rules of the macro EVAL-EXP-STEP in versible state transition system that simulates C.A Fig. 12 define a reversible state transition system rewrite tuple consists of a command stack Cd and

173 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology macro EVAL-PAT-STEP(Q,Q,St) ≡ rewrite [ Q, Q,St]by [ ((’val.V).Q), Q,St]⇒ [ Q, cons (cons ’val V) Q, consVSt] [ ((’var.J).Q), Q,St]⇒{GETV(J,V); Q <= ((’var.J).Q); St <= (V.St)} [ ((’cons E.F).Q), Q,St]⇒ [ cons* E F ’(consm) Q, cons ’(consm) Q,St] [ (’(consm).Q), (F E ’(consm).Q), (W V.St) ] ⇒ [ Q, cons (cons* ’cons E F) Q, cons (cons V W) St ]

Fig. 11 Evaluation step of a pattern on the right side of a reversible replacement. macro EVAL-EXP-STEP(Ex,Ex,St,H) ≡ rewrite [ Ex, Ex,St,H]by [ ((’val.V). Ex), Ex, St, H ] ⇒ [ Ex, cons (cons ’val V) Ex, cons V St, H ] [ ((’var.J). Ex), Ex, St, H ] ⇒{LOOKUP(J,V); Ex <= ((’var.J).Ex); St <= (V.St) } [ ((’hd.E). Ex), Ex, St, H ] ⇒ [ cons* E ’(hdm) Ex, cons ’(hdm) Ex, St, H ] [ (’(hdm). Ex), (E ’(hdm).Ex), ((V.W).St), H ] ⇒ [ Ex, cons (cons ’hd E) Ex, cons V St, cons W H ] [ ((’tl.E). Ex), Ex, St, H ] ⇒ [ cons* E ’(tlm) Ex, cons ’(tlm) Ex, St, H ] [ (’(tlm). Ex), (E ’(tlm).Ex), ((V.W).St), H ] ⇒ [ Ex, cons (cons ’tl E) Ex, cons W St, cons V H ] [ ((’cons E.F).Ex), Ex, St, H ] ⇒ [ cons* E F ’(consm) Ex, cons ’(consm) Ex, St, H ] [ (’(consm). Ex), (F E ’(consm).Ex), (W V.St), H ] ⇒ [ Ex, cons (cons* ’cons E F) Ex, cons (cons V W) St, H ] [ ((’eq E.F). Ex), Ex, St, H ] ⇒ [ cons* E F ’(eqm) Ex, cons ’(eqm) Ex, St, H ] [ (’(eqm). Ex), (F E ’(eqm). Ex), (W V.St), H ] ⇒{Ex <= ((’eq E.F).Ex); U ^= (=? V W); St <= (U.St); H <= (V W.H)}

Fig. 12 Expression evaluation step using the history H. a stack of processed commands Cd. The effect of We now explain the rewrite rules in more detail. command execution is to update the value list vl. A reversible assignment Xk ^= E is represented by The initial tuple is [(c),nil], and the final tuple (’asn (’var. ).e),wherek is the unary number is [nil,(c)],wherec is the command to execute of k,ande is the representation of E. The evaluation with the value list vl. The rewriting rules work of E is simulated by the original Bennett reversible in essentially the same way as the other rewriting simulation using the macro EVAL-EXP. In the Ben- rules, except that the rules for simulating loops are nett simulation, the macro EVAL-EXP computes the more involved. The rules make use of the macros value of E,themacroUPDATE reversibly updates the for expression and pattern evaluation. k-th position of Vl by the value, and the inverse of The iteration of the rewrite rules is implemented the macro EVAL-EXP clears the value of E set on V. by the macro EVAL-CMD in Fig. 10. The macro A reversible replacement Q<=Ris represented is used in the main program of the interpreter in by (’rep q.r),whereq and r are the representa- Fig. 9 to evaluate the body of the source program. tions of Q and R. The execution of reversible replace- All rewriting rules are pairwise orthogonal on both ments is performed by a copy-free Bennett simula- sides. The simulation of a loop by the rewrite rules tion. First, the macro EVAL-PAT simulates the eval- may not terminate. uation of R, clearing all variables in Vl that occur Command execution by iterating the rewrite in R and setting V to the value of R. Second, the in- rules in Fig. 13 is based on the following invariant: verse of the macro EVAL-PAT updates all variables ∗ [(c.cd),cd ] ⇒ [cd,(c.cd )] (18) in Vl that occur in Q so that the value of Q in the iff C[[ C]] σ = σ,whereσ  vl and σ  vl hold for updated value list is equal to V and clears V. the value list before and after applying the rewrite A command sequence C; D is represented by rules to execute the representation c of C. (’seq c.d),wherec and d are the representations

174 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology macro EVAL-CMD-STEP(Cd,Cd) ≡ rewrite [ Cd, Cd ]by (* Assignment, replacement, sequence *) [ ((’asn (’var.K).E).Cd), Cd ] ⇒{BENNETT(EVAL-EXP(E,V,H),UPDATE(K,V)); Cd <= ((’asn (’var.K).E).Cd)} [ ((’rep Q.R).Cd), Cd ] ⇒{COMP-INV(EVAL-PAT(R,V),EVAL-PAT(Q,V)); Cd <= ((’rep Q.R).Cd)} [ ((’seq C.D).Cd), Cd ] ⇒ [ cons* C D ’(seqm) Cd, cons ’(seqm) Cd ] [ (’(seqm).Cd), (D C ’(seqm).Cd)]⇒ [ Cd, cons (cons* ’seq C D) Cd ]

(* Conditional *) [ ((’cond E C D F).Cd), Cd ] ⇒{BENNETT(EVAL-EXP(E,V,H),W ^= V); if W then C ^= C else C ^=DfiW; Cd <= (C (’condmECDF).Cd); Cd <= ((’condm W).Cd)} [ ((’condm E C D F).Cd), (C (’condm W).Cd)] ⇒{if W then C ^= C else C ^=DfiW; BENNETT(EVAL-EXP(F,V,H),W ^= V) Cd <= ((’condECDF).Cd)} (* Loop *) [ ((’loop E C D F).Cd), Cd ] ⇒ [ cons (list ’l1ECDF)Cd, cons ’(l1 true) Cd ] [ ((’l1 E C D F).Cd), ((’l1 W).Cd)]⇒{BENNETT(EVAL-EXP(E,V,H),W ^= V); Cd <= (C ’(l2) .Cd ); Cd <= ((’l2 E C D F).Cd)} [ (’(l2).Cd), (C (’l2 E C D F).Cd)]⇒{BENNETT(EVAL-EXP(F,V,H),W ^= V); Cd <= ((’l3 W) .Cd ); Cd <= ((’l3 E C D F).Cd)} [ (’(l3 false).Cd), ((’l3 E C D F).Cd)]⇒ [ cons* D ’(l4) Cd, cons (list ’l4 E C D F) Cd ] [ (’(l3 true) .Cd), ((’l3 E C D F).Cd)]⇒ [ Cd, cons (list ’loop ECDF)Cd ] [ (’(l4).Cd), (D (’l4 E C D F).Cd)]⇒ [ cons (list ’l1ECDF)Cd,cons ’(l1 false) Cd ]

Fig. 13 Command execution step using expression and pattern evaluation. of the commands C and D. Two rules are used for either the then-branch c or the else-branch d—is the execution. The first rule decomposes the se- pushed onto Cd, and the value of W is kept on Cd. quence and pushes c and d onto Cd together with In the second rule, the code of the processed branch the marker ’(seqm) to mark the end of the se- Cd1 is cleared depending on the value of W,andthe quence. The marker is also pushed on Cd.Thetwo value of W is cleared by the value of the assertion commands c and d arethenprocessedbyrepeated F (if it is correct), which is also computed by the application of the macro EVAL-CMD-STEP. After the original Bennett simulation of expression evalua- execution of c and d,themarker’(seqm) appears tion. If this value is different from the value of on top of Cd, and the second rule reconstructs the the assertion F, the computation abnormally halts original sequence (’seq c.d) on Cd. The following at the reversible assignment W^=V. The codes of rules use markers in a similar fashion. the conditionals are reconstructed and moved onto A conditional if E then C else D fi F is rep- the processed codes Cd. The palindromic structure resented by (’condecdf),wheree, c, d,and appears on the right-hand sides of the two rules, al- f are the representations of E, C, D,andF,respec- though the expressions to be evaluated (E and F) tively. First, the value of the test expression E is are different in the first and last Bennett simula- computed by the original Bennett method: setting tions. the value of E on V by EVAL-EXP with the garbage A loop from E do C loop D until F is repre- set on H,thevalueV is copied to W, and the value sented by (’loopecdf),wheree, c, d,andf V and the garbage H are cleared by the inverse of are the representations of E, C, D,andF,respec- EVAL-EXP. Depending on W, the result of the test— tively. For the interpretation of a loop, we follow

175 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

[ ((’seq (’loop _ _ _ _) (’rep _ . _))), nil ] ⇒ [ ((’loop _ _ _ _) (’rep _ . _) ’(seqm)), (’(seqm)) ] ⇒ [ ((’l1 _ _ _ _) (’rep _ . _) ’(seqm)), (’(l1 true) . ’(seqm)) ] ⇒ [ (’(l2) (’rep _ . _) ’(seqm)), ((’l2 _ _ _ _) . ’(seqm)) ] ⇒ [ (’(l3.false) (’rep _ . _) ’(seqm)), ((’l3 _ _ _ _) . ’(seqm)) ] ⇒ [ ((’asn y . (’val . nil)) ’(l4) (’rep_._)’(seqm)), ((’l4 _ _ _ _) . ’(seqm)) ] ⇒ [ (’(l4) (’rep _ . _) ’(seqm)), ((’asn y . (’val . nil)) (’l4 _ _ _ _) . ’(seqm)) ] ⇒ [ ((’l1 _ _ _ _) (’rep _ . _) ’(seqm)), (’(l1 false) . ’(seqm)) ] ⇒

... the above five lines repeat ...

[ (’(l2) (’rep _ . _) ’(seqm)), ((’l2 _ _ _ _) . ’(seqm)) ] ⇒ [ (’(l3.true) (’rep _ . _) ’(seqm)), ((’l3 _ _ _ _) . ’(seqm)) ] ⇒ [ ((’rep _ . _) ’(seqm)), ((’loop _ _ _ _) . ’(seqm)) ] ⇒ [ (’(seqm)), ((’rep _ . _) (’loop _ _ _ _) . ’(seqm)) ] ⇒ [ nil, ((’seq (’loop _ _ _ _) (’rep _ . _))) ]

Fig. 14 Rewriting the trace of a command sequence of the list reversal program (underscores are “don’t care” elements). the flowchart in Fig. 3. The tags ’l1, ’l2, ’l3,and test of the loop with the expected assertion false ’l4 are associated with the labels l1 before the as- paired with ’l1. sertion E (two places), l2 before the test F, l3 after The simulation of an unstructured flowchart by a the test F (two places), and l4 after the command structured flowchart was used to give an alternative D. Expression evaluations of tests and assertions are global proof for the structured program theorem performed by the original Bennett simulation. An by Cooper [7]. In his construction, the position of assertion E is placed in the circle that merges the the control in the source program is stored in a se- two control flows labeled l1. If a control flow comes quence of Boolean variables. Similarly, we use tags from the edge labeled t (f, resp.), the evaluation of to simulate the control in the source programs. E must be true (false, resp.). This is simulated by Example: Trace of list reversal the rule ’l1, in which the expected value of the as- To illustrate the macro EVAL-CMD-STEP,weshow sertion E clears W by the value of V. The rule pushes a tracing of rewriting a part of the list reversal pro- the representation of the do-branch C and tag ’l2 gram in Fig. 14, whose program representation was onto Cd and the tag ’l2 onto the processed code shown in Sect. 4. 3. Which rewrite rule to apply Cd.AtestF is placed in the lozenge that divides depends on the tags in the head of the commands the control flow into two branches. If control moves to be processed. to the edge labeled t (f, resp.), the evaluation of F must be true (false, resp.). (Otherwise, the 6 Efficiency of the Interpreter computation abnormally halts at the reversible as- In this study, we use a simple cost model for signment W^=V.) This is simulated by one rule for the language R-WHILE. The unfolding of a semantic ’l2 and two rules for ’l3.Therule’l2 pushes a function is counted as a step (E, Q, C,andP in pair of tags ’l3 and the value of F onto the code Fig. 2), and an equality test for data, d1 = d2, to process Cd.Attherule’l3,if’l3 is paired †4 is counted as min(|d1|, |d2|)steps . Unfolding with true, the representation of the loop-branch D −1 (Q[[ Q]] ) takesthesamenumberofstepsasun- is pushed onto the code to process Cd.When’l3 is folding Q[[ Q]] . paired with false, it is the end of the loop. Then, The reversible interpreter ri for R-WHILE is not D is moved to the processed code Cd. It should be linear-time according to Def. 2 because the variable noted that the rules ’l2 and ’l4 have nonlinear in- access by the store management macros LOOKUP, put patterns, and the first ’l3 rule has a nonlinear UPDATE, PUTV,andGETV takes time proportional to output pattern. The rule ’l4 is used to clear the code of the loop branch and moves control to the †4 We denote the size of data d by |d| and the size of the data in the variable X by |X|.

176 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology the number of variables in the source program. To sions. It should be noted that the time for inter- show the existence of a linear-time reversible self- preting an equality test =? is bounded by the time interpreter, consider an r-Turing complete subset of the equality test in direct evaluation. of R-WHILE, in which the number of variables is For the case of commands, an analogous analysis bounded. holds in addition to an induction on the number of Let m be the maximum number of variables that steps. We assume as an induction hypothesis that occurs in the reversible interpreter ri and in an the time for interpreting the branches of condition- RTM interpreter implemented in R-WHILE. Define a als and loops is bounded by that of the correspond- subset R-WHILE-M of R-WHILE with at most m vari- ing direct execution. With the results for expres- ables. Since R-WHILE is an instance of the reversible sions and patterns above, we obtain that the time flowchart language framework [34] and has dynamic for interpreting commands by the macro EVAL-CMD allocation of data, we can construct an RTM inter- is bounded by the time for directly evaluating com- preter in R-WHILE. By definition, this program is mands. also an R-WHILE-M program. Hence, R-WHILE-M is Finally, we show that the total time for running r-Turing complete. As a consequence of limiting ri with the input (p.d) is O(|d|). The time of the number of variables, variable access in ri is the macro BENNETT is bounded by the maximum constant time, i.e., O(m). Therefore, ri is a linear- of the times to run its two arguments. The ini- time reversible interpreter for R-WHILE-M. tialization macro INIT and the reversible replace- Theorem 2. The program ri is a linear-time re- ment are performed in constant time. Hence, the versible self-interpreter for an r-Turing complete total time of running ri is bounded by the time language R-WHILE-M: there exists a constant c such required for interpreting the command in the body that for any R-WHILE-M program p and data d,we of p with d. have The result that a linear-time reversible self- timeR-WHILE-M(p.d) ≤ c · timeR-WHILE-M(d) . (19) ri p interpreter exists in the language R-WHILE-M is ro- Proof. A simple induction on the structure of pat- bust in that it is not affected by the time to ac- terns shows that the direct evaluation of patterns, cess variables in the cost model of the language. Q[[ Q]], takes time proportional to |Q|.Thisalso Any reasonable access timing that we choose is con- −1 holds for Q[[ Q]] . Therefore, the command execu- stant time owing to the bounded number of vari- tion C[[ Q<=R]] takes time proportional to |Q| + |R|. ables. The existence of a linear-time reversible self- The macros LOOKUP, UPDATE, GETV,andPUTV take interpreter in the language R-WHILE depends on the time proportional to the size of the store, m,be- assumption of the time to access variables. For cause of the loop of their common macro POS. example, if we assume a unit cost for each vari- It is easy to see that the time for interpreting able access, simulating the access to the unbounded a pattern by the macro EVAL-PAT is bounded by number of variables in the source program by the the time for direct pattern evaluation. The macro bounded number in the interpreter is impossible in EVAL-PAT-STEP is performed in constant time be- constant time. Therefore, under this assumption, cause of branching and merging of the control flow, ri for R-WHILE is not linear-time. The cause of the and all other operations on the right side of the nonlinearity of this interpreter is the variable ac- rewrite rules are constant time. The size of the cess time, and this question is independent of the data representation of a pattern is proportional reversibility of the language. to the size of the pattern, and each application There has been a discussion, independent of the of EVAL-PAT-STEP decrements the size of the pat- reversibility, of what is a “fair charge” for store tern stack in the rewrite tuple. Hence, the macro access in a language with an unbounded number EVAL-PAT(Q,V) takes time proportional to the size of variables or constants because some cost mod- of the pattern represented by Q. els have the counterintuitive consequence (Blum’s An analogous analysis shows that the time for speedup theorem) that there are functions, which interpreting expressions by the macro EVAL-EXP is for any program computing such functions, an- bounded by the time for directly evaluating expres- other program that is significantly faster exists.

177 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

Limiting the number of variables and constants is known to avoid the question about the fairness of 8 Conclusion and Future Work a model. For example, to make his complexity We extended the definition of standard inter- results independent of this discussion, Jones used preters to reversible interpreters and showed for a language with a single variable and single con- the first time that a linear-time reversible self- stant for constructing a linear-time (irreversible) interpreter exists in an r-Turing complete reversible self-interpreter [16][17]. imperative language. The language R-WHILE has reversible structured control-flow operators and 7 Related Work symbolic tree-structured data (S-expressions) and R-WHILE-M is a simple imperative reversible lan- is well-suited for symbolic program manipulation. guage that only has tree-structured data and a lim- This new reversible language is as computationally ited number of variables and atoms, yet it is ex- powerful as any reversible language can get. pressive enough to be r-Turing complete. These When building the reversible interpreter in a properties were all indispensable for construct- reversible imperative language, we identified and ing a linear-time reversible self-interpreter in this confirmed the importance of several unconven- study. To the best of the authors’ knowledge, there tional programming techniques such as a family is no report on the existence of linear-time self- of Bennett-type reversible simulations, reversible interpretation in r-Turing complete reversible im- control loops, the usage of biorthogonal rewrite perative languages. Linear-time self-interpretation rules, and the program inversion of reversibilized was neither reported for the high-level impera- programs. We have seen that these techniques tive reversible languages R [9] and Gries’ invert- are effective in practice to minimize the reversible ible language [12] nor for the low-level assembler programming effort. They may also inspire new languages PISA [9] and BoB [27].Noneofthese approaches in conventional programming language languages have tree-structured data. Reversible theory. It is future work to properly abstract languages with tree-structured data include the re- reusable programming patterns and components versible linear Ψ-Lisp [5], the reversible functional and point out the applications of these program- languages INV [23] and RFUN [33], and a family ming techniques in reversible programming lan- of combinator languages [14]. Self-interpretation guage design. with a superlinear overhead was reported for other As we saw in Section 2, the extension of the famil- reversible languages and reversible computational iar (but irreversible) interpreter definition to a new models such as a reversible metacircular evalu- reversible definition is a necessity and not an op- ator [15] for the reversible combinator language tion in reversible computing. A direction for future o Π [14], an RFUN self-interpreter [26],andan work will be to investigate its consequences and RTM [2]. None of these languages explicitly limit which properties hold in a reversible setting, which the number of variables and constants. we take for granted in a irreversible setting. For ex- It was shown that for any finite set of Janus pro- ample, the constant hierarchy theorem [16][17] de- grams, there is a nontrivial linear-time reversible nies the existence of programs that are arbitrarily interpreter in Janus that can simulate the programs faster than a given program by constant time, and without garbage data [35]. Because the storage Levin’s optimal universal search programs [18][13] available to an interpreter is fixed by the static are optimal to within a constant factor for any storage allocation of Janus, no interpreter can sim- (semi-)decidable relations. Although the existence ulate all Janus programs because they can have of linear-time self-interpreters plays important roles any size. Janus was extended with dynamically in the proofs of these theorems, whether reversible allocated stacks to be r-Turing complete [31], but self-interpreters play important roles in the coun- whether this data structure allows linear-time self- terparts of these theorems is an open question. interpretation is not known. There is room to reduce the number of variables required to construct a linear-time reversible self- interpreter in a reversible language with a single

178 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology atom. One could optimize the interpreter to be [8] DeVos, A.: Reversible Computing: Funda- more economical with the use of variables. Of mentals, , and Applications, course, one could also add more complex constructs Wiley-VCH, 2010. [ 9 ] Frank, M. P.: Reversibility for Efficient Com- to the language, which may even reduce the num- puting, PhD Thesis, Massachusetts Institute of ber of variables to one, as in the irreversible set- Technology, 1999. ting [16][17]. [10] Gl¨uck, R. and Kawabe, M.: A Program In- verter for a Functional Language with Equality and Constructors, Programming Languages and Sys- Acknowledgments tems. Proceedings, Ohori, A.(ed.), Lecture Notes in A preliminary version of this paper was presented Computer Science, Vol. 2895, Springer-Verlag, 2003, at the 17th JSSST Workshop on Programming and pp. 246–264. Programming Languages (PPL2015). The authors [11] Gl¨uck, R. and Kawabe, M.: Derivation of De- would like to thank the anonymous reviewers for terministic Inverse Programs Based on LR , Functional and . Proceedings, their detailed and constructive comments. The first Kameyama, Y. and Stuckey, P. J.(eds.), Lecture author has the great pleasure to thank Masami Notes in Computer Science, Vol. 2998, Springer- Hagiya, Akihiko Takano, and Zhenjiang Hu for Verlag, 2004, pp. 291–306. their invaluable support in Japan. [12] Gries, D.: The Science of Programming, Texts This work was partly supported by JSPS No. RC and Monographs in Computer Science, Springer- Verlag, 1981, chapter 21 Inverting Programs, 21413101, JSPS KAKENHI Grant No. 25730049, pp. 265–274. and Danish DFF Grant No. FI 12-126689. [13] Hutter, M.: The Fastest and Shortest for All Well-Defined Problems, International Jour- References nal of Foundations of Computer Science, Vol. 13, No. 3(2002), pp. 431–443. [ 1 ] Abramsky, S.: A Structural Approach to Re- [14] James, R. P. and Sabry, A.: Information Effects, versible Computation, Theoretical Computer Sci- Principles of Programming Languages. Proceedings, ence, Vol. 347, No. 3(2005), pp. 441–464. ACM Press, 2012, pp. 73–84. [ 2 ] Axelsen, H. B. and Gl¨uck, R.: What Do Re- [15] James, R. P. and Sabry, A.: Isomorphic versible Programs Compute?, Foundations of Soft- Interpreters from Logically Reversible Abstract ware Science and Computation Structures. Proceed- Machines, Reversible Computation. Proceedings, ings, Hofmann, M.(ed.), Lecture Notes in Computer Gl¨uck, R. and Yokoyama, T.(eds.), Lecture Notes in Science, Vol. 6604, Springer-Verlag, 2011, pp. 42–56. Computer Science, Vol. 7581, Springer-Verlag, 2013, [ 3 ] Axelsen, H. B. and Gl¨uck, R.: Reversible Rep- pp. 57–71. resentation and Manipulation of Constructor Terms [16] Jones, N. D.: Constant Time Factors Do Matter, in the Heap, Reversible Computation. Proceedings, ACM Symposium on Theory of Computing,ACM Dueck, G. W. and Miller, D. M.(eds.), Lecture Press, 1993, pp. 602–611. Notes in Computer Science, Vol. 7948, Springer- [17] Jones, N. D.: Computability and Complexity: Verlag, 2013, pp. 96–109. From a Programming Perspective, MIT Press, 1997. [ 4 ] Axelsen, H. B., Gl¨uck, R. and Yokoyama, T.: Revised version, available from http://www.diku. Reversible and Its Abstract Pro- dk/∼neil/Comp2book.html. cessor Architecture, Computer Science – Theory [18] Levin, L. A.: Universal Sequential Search and Applications. Proceedings, Diekert, V., Volkov, Problems, Problems of Information Transmission, M. V. and Voronkov, A.(eds.), Lecture Notes in Vol. 9, No. 3(1975), pp. 265–266. Translated from Computer Science, Vol. 4649, Springer-Verlag, 2007, Probl. Peredachi Inf., Vol.9, No.3(1973), pp. 115– pp. 56–69. 116. [ 5 ] Baker, H. G.: NREVERSAL of Fortune — The [19] Li, M. and Vit´anyi,P.M.B.: An Introduc- Thermodynamics of Garbage Collection, Interna- tion to and Its Applica- tional Workshop on Memory Management. Pro- tions, Springer-Verlag, 3rd edition, 2008. ceedings, Bekkers, Y. and Cohen, J.(eds.), Lec- [20] Matos, A. B.: Linear Programs in a Simple Re- ture Notes in Computer Science, Vol. 637, Springer- versible Language, Theoretical Computer Science, Verlag, 1992, pp. 507–524. Vol. 290, No. 3(2003), pp. 2063–2074. [ 6 ] Bennett, C. H.: Logical Reversibility of Compu- [21] Mogensen, T. Æ.: Linear-Time Self-Interpretation tation, IBM Journal of Research and Development, of the Pure Lambda Calculus, Higher-Order Vol. 17, No. 6(1973), pp. 525–532. and Symbolic Computation, Vol. 13, No. 3(2000), [ 7 ] Cooper, D. C.: B¨ohm and Jacopini’s Reduc- pp. 217–237. tion of Flow Charts, Communications of the ACM, [22] Morita, K.: Reversible Computing and Cellular Vol. 10, No. 8(1967), p. 463, p. 473.

179 Information and Media Technologies 11: 160-180 (2016) reprinted from: Computer Software 33(3): 108-128 (2016) © Japan Society for Software Science and Technology

Automata — A Survey, Theoretical Computer Sci- Programming Language and its Invertible Self- ence, Vol. 395, No. 1(2008), pp. 101–131. Interpreter, Partial Evaluation and Semantics- [23] Mu, S.-C., Hu, Z. and Takeichi, M.: An Based Program Manipulation. Proceedings,ACM Injective Language for Reversible Computation, Press, 2007, pp. 144–153. Mathematics of Program Construction. Proceed- ings, Kozen, D.(ed.), Lecture Notes in Computer Science, Vol. 3125, Springer-Verlag, 2004, pp. 289– 313. [24] Perumalla, K. S.: Introduction to Reversible Computing, CRC Press, 2013. Robert Gl¨uck [25] Saeedi, M. and Markov, I. L.: Synthesis and Op- Robert Gl¨uck received his PhD de- timization of Reversible Circuits — A Survey, ACM gree and post-PhD (Habilitation) Computing Survey, Vol. 45, No. 2(2013), pp. 21:1– in Austria, and is an Associate 21:34. [26] Thomsen, M. K. and Axelsen, H. B.: Interpreta- Professor in Computer Science at tion and Programming of the Reversible Functional the University of Copenhagen, Den- Language RFUN, IFL Pre-Proceedings, 2015. mark. He received twice the Erwin-Schr¨odinger- [27] Thomsen, M. K., Axelsen, H. B. and Gl¨uck, Fellowship of the Austrian Science Foundation, was R.: A Reversible Processor Architecture and Its Reversible Logic Design, Reversible Computation. an Invited Fellow of JSPS, and funded by the Proceedings, De Vos, A. and Wille, R.(eds.), Lecture PRESTO21 basic research program of JST. He co- Notes in Computer Science, Vol. 7165, Springer- chaired meetings in North America, Europe and Verlag, 2012, pp. 30–42. Asia, and is a member of several international com- [28] Thomsen, M. K., Gl¨uck, R. and Axelsen, puter societies. His main research interests are the H. B.: Reversible Arithmetic Logic Unit for Quan- tum Arithmetic, Journal of Physics A: Math- theory and practice of programming languages and ematical and Theoretical, Vol. 43, No. 38(2010), software systems. His last works relate to reversible 382002 (10pp.). computing, partial evaluation, and metacomputa- [29] Wille, R. and Drechsler, R.: Towards a Design tion. Flow for Reversible Logic, Springer-Verlag, 2010. [30] Winskel, G.: The Formal Semantics of Pro- gramming Languages: An Introduction, MIT Press, Tetsuo Yokoyama 1993. [31] Yokoyama, T., Axelsen, H. B. and Gl¨uck, R.: Tetsuo Yokoyama is an Associate Principles of a Reversible Programming Language, Professor at the Department of Computing Frontiers. Proceedings, ACM Press, Software Engineering, the Faculty 2008, pp. 43–54. of Science and Engineering, Nanzan [32] Yokoyama, T., Axelsen, H. B. and Gl¨uck, R.: University. He received his Ph.D. Reversible Flowchart Languages and the Structured Reversible Program Theorem, International Collo- degree in Information Science and Technology from quium on Automata, Languages and Programming. the University of Tokyo in 2006. He visited the Uni- Proceedings, Aceto, L., et al. (eds.), Lecture Notes in versity of Copenhagen, Department of Computer Computer Science, Vol. 5126, Springer-Verlag, 2008, Science, supported by JSPS Research Fellowships pp. 258–270. [33] Yokoyama, T., Axelsen, H. B. and Gl¨uck, R.: for Young Scientists, from 2006 to 2007. He was a Towards a Reversible Functional Language, Re- Researcher at the Center for Embedded Comput- versible Computation. Proceedings, De Vos, A. and ing Systems, the Graduate School of Information Wille, R.(eds.), Lecture Notes in Computer Science, Science, Nagoya University from 2007 to 2009, and Vol. 7165, Springer-Verlag, 2012, pp. 14–29. was an Assistant Professor from 2009 to 2011 at [34] Yokoyama, T., Axelsen, H. B. and Gl¨uck, R.: Fundamentals of Reversible Flowchart Languages, the Department of Software Engineering, the Fac- Theoretical Computer Science, Vol. 611(2016), ulty of Information Sciences and Engineering ; He pp. 87–115. is a member of ACM, IPSJ, IEICE and JSSST. [35] Yokoyama, T. and Gl¨uck, R.: A Reversible

180