Programming Techniques for Reversible Comparison Sorts

  • Conference paper
  • First Online:
Programming Languages and Systems (APLAS 2015)

Part of the book series: Lecture Notes in Computer Science ((LNPSE,volume 9458))

Included in the following conference series:

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
EUR 32.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or Ebook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free ship** worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Similar content being viewed by others

Notes

  1. 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. 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. 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. 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. 5.

    The size of the auxiliary arrays in merge is not optimized, in order to make the indexing clearer in the presentation.

References

  1. 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)

    Chapter  Google Scholar 

  2. 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)

    Chapter  Google Scholar 

  3. Bennett, C.H.: Time/space trade-offs for reversible computation. SIAM J. Comput. 18(4), 766–776 (1989)

    Article  MathSciNet  MATH  Google Scholar 

  4. Bonet, B.: Efficient algorithms to rank and unrank permutations in lexicographic order. In: Workshop on Search in Artificial Intelligence and Robotics. AAAI (2008)

    Google Scholar 

  5. 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)

    Chapter  Google Scholar 

  6. 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)

    Chapter  Google Scholar 

  7. Hall, J.S.: A reversible instruction set architecture and algorithms. In: Proceedings of Physics and Computation, pp. 128–134. IEEE Press, New York (1994)

    Google Scholar 

  8. Knuth, D.E.: The Art of Computer Programming, Volume 3: Sorting and Searching, 2nd edn. Addison Wesley Longman Publishing Co. Inc., Boston (1998)

    Google Scholar 

  9. Lutz, C.: Janus: A time-reversible language. Letter to R. Landauer (1986)

    Google Scholar 

  10. Myrvold, W., Ruskey, F.: Ranking and unranking permutations in linear time. Inf. Proc. Let. 79(6), 281–284 (2001)

    Article  MathSciNet  MATH  Google Scholar 

  11. 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)

    Google Scholar 

  12. Perumalla, K.S.: Introduction to Reversible Computing. CRC Press, Boca Raton (2013)

    Google Scholar 

  13. Wille, R., Drechsler, R.: Towards a Design Flow for Reversible Logic. Springer, Heidelberg (2010)

    Book  MATH  Google Scholar 

  14. 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)

    Google Scholar 

  15. 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)

    Google Scholar 

Download references

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

Authors

Corresponding author

Correspondence to Holger Bock Axelsen .

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

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

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]:

figure r

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

Reprints 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)

Publish with us

Policies and ethics

Navigation