Abstract
A common approach to reversible programming is to reversibly simulate an irreversible program with the desired functionality, which in general puts additional pressure on the computational resources (time, space.) If the same running time is required, ensuring a minimal space overhead is a significant programming challenge.
We introduce criteria for the optimality of reversible simulation: A reversible simulation is faithful if it incurs no asymptotic time overhead and bounds the space overhead (the garbage) by some function g(n), and hygienic if g is (asymptotically) optimal for faithful simulation.
We demonstrate the programming techniques used to develop faithful and hygienic reversible simulations of several well-known comparison sorts, e.g. insertion sort and quicksort, using representations of permutations in both the output and intermediate additional space required.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Similar content being viewed by others
Notes
- 1.
This bound can depend significantly on the data structures used. For list sorting, which returns a redundant representation of the sorted array, no garbage is needed.
- 2.
If i=n holds at line 9, the loop terminates without evaluating expression
with the out-of-bounds index n, by using short-circuit evaluation.
- 3.
The non-variable actual argument n-1 is syntactic sugar for a fresh variable allocated with value \(n-1\) before the call, and deallocated with value \(n-1\) afterwards.
- 4.
We remark that Hall’s instruction set is only reversible when programmer discipline is employed, and in fact contains a number of irreversible instructions.
- 5.
The size of the auxiliary arrays in merge is not optimized, in order to make the indexing clearer in the presentation.
References
Axelsen, H.B., Glück, R.: What do reversible programs compute? In: Hofmann, M. (ed.) FOSSACS 2011. LNCS, vol. 6604, pp. 42–56. Springer, Heidelberg (2011)
Axelsen, H.B., Thomsen, M.K.: Garbage-free reversible integer multiplication with constants of the form 2\(^{k}\pm \)2\(^l\pm \)1. In: Glück, R., Yokoyama, T. (eds.) RC 2012. LNCS, vol. 7581, pp. 171–182. Springer, Heidelberg (2013)
Bennett, C.H.: Time/space trade-offs for reversible computation. SIAM J. Comput. 18(4), 766–776 (1989)
Bonet, B.: Efficient algorithms to rank and unrank permutations in lexicographic order. In: Workshop on Search in Artificial Intelligence and Robotics. AAAI (2008)
Dijkstra, E.W.: Program inversion. In: Bauer, F.L., Broy, M. (eds.) Program Construction: International Summer School. LNCS, vol. 69, pp. 54–57. Springer, Heidelberg (1979)
Early, D., Gao, A., Schellekens, M.: Frugal encoding in reversible \({\cal MOQA}\): a case study for quicksort. In: Glück, R., Yokoyama, T. (eds.) RC 2012. LNCS, vol. 7581, pp. 85–96. Springer, Heidelberg (2013)
Hall, J.S.: A reversible instruction set architecture and algorithms. In: Proceedings of Physics and Computation, pp. 128–134. IEEE Press, New York (1994)
Knuth, D.E.: The Art of Computer Programming, Volume 3: Sorting and Searching, 2nd edn. Addison Wesley Longman Publishing Co. Inc., Boston (1998)
Lutz, C.: Janus: A time-reversible language. Letter to R. Landauer (1986)
Myrvold, W., Ruskey, F.: Ranking and unranking permutations in linear time. Inf. Proc. Let. 79(6), 281–284 (2001)
Nishida, N., Vidal, G.: Program inversion for tail recursive functions. In: Schmidt-Schauß, M. (ed.) RTA. LIPIcs, vol. 10, pp. 283–298. Schloss Dagstuhl–Leibniz-Zentrum für Informatik, Dagstuhl (2011)
Perumalla, K.S.: Introduction to Reversible Computing. CRC Press, Boca Raton (2013)
Wille, R., Drechsler, R.: Towards a Design Flow for Reversible Logic. Springer, Heidelberg (2010)
Yokoyama, T., Axelsen, H.B., Glück, R.: Principles of a reversible programming language. In: Proceedings of Computing Frontiers, pp. 43–54. ACM Press, New York (2008)
Yokoyama, T., Axelsen, H.B., Glück, R.: Minimizing garbage size by generating reversible simulations. In: Proceedings of Networking and Computing, pp. 379–387. IEEE Press, New York (2012)
Acknowledgements
H.B. Axelsen was supported by the Danish Council for Independent Research \(\mid \) Natural Sciences under the Foundations of Reversible Computing project. T. Yokoyama was supported by MEXT KAKENHI 25730049.
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
A Converting Between Permutation Representations
A Converting Between Permutation Representations
Factorial Representation to Rank. Procedure decfac2rank takes a decreasing factorial representation d[], its length n, and zero-cleared rank as arguments, and returns the lexicographic rank (in rank) of the decreasing factorial representation, with d[] zero-cleared (n is preserved):
![figure p](http://media.springernature.com/lw685/springer-static/image/chp%3A10.1007%2F978-3-319-26529-2_22/MediaObjects/393248_1_En_22_Figp_HTML.gif)
Direct to Factorial Representation. Procedure perm2decfac is based on an irreversible unranking algorithm by Bonet [4]. The procedure converts from the direct permutation p[] of length n to decreasing factorial representation d[] using an intermediate tree data structure (here in the form of the array t[] which is initially and finally zero cleared). The key idea of the algorithm is to use the tree t[] to perform the count of how many elements in p[i+1..n-1] are smaller than p[i] in a logarithmic number of updates:
![figure q](http://media.springernature.com/lw685/springer-static/image/chp%3A10.1007%2F978-3-319-26529-2_22/MediaObjects/393248_1_En_22_Figq_HTML.gif)
We use syntactic sugar in the expressions: the power operator **, which we assume to run in constant time for powers of two (e.g. as realized by left rotation), and the function log2_ceil(n), which evaluates to \(\lceil \log _2 n \rceil \). Executable versions are available at topps.diku.dk/pirc/sorts.
The tree t[] has root t[1] and the left and right children of node t[ i ] are t[2i ] and t[ \(2i+1\) ] for any \(i~(\ge 1)\). The call to make_tree (code not shown) at line 23 sets each node in the tree to two to the power of its height (how many times it is visited.) make_tree reversibly simulates the irreversible codelet [4]:
Rank to Direct Representation. The implementations decfac2rank and perm2decfac above are reversible and in particular garbage-free, so each procedure can cover conversion in both directions. For instance, the inverse invocation uncall decfac2rank(d,n,r) converts the rank r to the length n factorial representation d[] (initially zero-cleared), clearing r in the process. Further, we can implement the remaining conversions between permutation and rank by subsequent calls to perm2decfac and decfac2rank.
Rights and permissions
Copyright information
© 2015 Springer International Publishing Switzerland
About this paper
Cite this paper
Axelsen, H.B., Yokoyama, T. (2015). Programming Techniques for Reversible Comparison Sorts. In: Feng, X., Park, S. (eds) Programming Languages and Systems. APLAS 2015. Lecture Notes in Computer Science(), vol 9458. Springer, Cham. https://doi.org/10.1007/978-3-319-26529-2_22
Download citation
DOI: https://doi.org/10.1007/978-3-319-26529-2_22
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-26528-5
Online ISBN: 978-3-319-26529-2
eBook Packages: Computer ScienceComputer Science (R0)