Abstract
We present a short implementation of the well-known Tortoise and Hare cycle finding algorithm in the constructive setting of Coq. This algorithm is interesting from a constructive perspective because it is both very simple and potentially non-terminating (depending on the input). To overcome potential non-termination, we encode the given termination argument (there exists a cycle) into a bar inductive predicate that we use as termination certificate. From this development, we extract the standard OCaml implementation of this algorithm. We generalize the method to the full Floyd’s algorithm that computes the entry point and the period of the cycle in the iterated sequence, and to the more efficient Brent’s algorithm for computing the period only, again with accurate extractions of their respective standard OCaml implementations.
Work partially supported by the TICAMORE project ANR grant 16-CE91-0002.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
OCaml equality decider is partially correct, e.g. it throws exceptions on functions.
- 2.
Conventional bar induction often requires Brouwer’s thesis which precisely postulates that bar predicates are inductive.
- 3.
Descending induction is implemented by in file utils.v .
- 4.
For \(=_X\), this is precisely the assumption of the \(\mathrel {=^?_X}\) equality decider.
- 5.
See th_rel.v where an arbitrary decidable relation replaces \(=_X\).
- 6.
Least for the natural order \(\mathrel {\leqslant }\) over .
References
Cycle detection – Wikipedia, The Free Encyclopedia. https://en.wikipedia.org/wiki/Cycle_detection
Aumasson, J.-P., Meier, W., Phan, R.C.-W., Henzen, L.: The Hash Function BLAKE. ISC. Springer, Heidelberg (2014). https://doi.org/10.1007/978-3-662-44757-4
Bertot, Y., Castéran, P.: Interactive Theorem Proving and Program Development - Coq’Art: The Calculus of Inductive Constructions. Texts in Theoretical Computer Science. An EATCS Series. Springer, Heidelberg (2004). https://doi.org/10.1007/978-3-662-07964-5
Bove, A., Capretta, V.: Modelling general recursion in type theory. Math. Structures Comput. Sci. 15(4), 671–708 (2005). https://doi.org/10.1017/S0960129505004822
Bove, A., Krauss, A., Sozeau, M.: Partiality and recursion in interactive theorem provers – an overview. Math. Structures Comput. Sci. 26(1), 38–88 (2016). https://doi.org/10.1017/S0960129514000115
Brent, R.P.: An improved Monte Carlo factorization algorithm. BIT Numer. Math. 20(2), 176–184 (1980). https://doi.org/10.1007/BF01933190
Castéran, P.: Utilisation en Coq de l’opérateur de description (2007). http://jfla.inria.fr/2007/actes/PDF/03_casteran.pdf
Coen, C.S., Valentini, S.: General Recursion and Formal Topology. In: PAR-2010, Partiality and Recursion in Interactive Theorem Provers. EPiC Series in Computing, vol. 5, pp. 72–83. EasyChair (2012). https://doi.org/10.29007/hl75
Dufourd, J.F.: Formal study of functional orbits in finite domains. Theoret. Comput. Sci. 564, 63–88 (2015). https://doi.org/10.1016/j.tcs.2014.10.041
Dybjer, P.: A general formulation of simultaneous inductive-recursive definitions in type theory. J. Symb. Log. 65(2), 525–549 (2000). https://doi.org/10.2307/2586554
Filliâtre, J.C.: Tortoise and the hare algorithm (2007). https://github.com/coq-contribs/tortoise-hare-algorithm
Fridlender, D.: An interpretation of the fan theorem in type theory. In: Altenkirch, T., Reus, B., Naraschewski, W. (eds.) TYPES 1998. LNCS, vol. 1657, pp. 93–105. Springer, Heidelberg (1999). https://doi.org/10.1007/3-540-48167-2_7
Gammie, P.: The Tortoise and Hare Algorithm (2015). https://www.isa-afp.org/entries/TortoiseHare.html
Joux, A.: Algorithmic Cryptanalysis. Cryptography and Network Security. Chapman & Hall/CRC (2009)
Knuth, D.E.: The Art of Computer Programming, Volume 2: Seminumerical Algorithms. Addison-Wesley Longman Publishing Co., Inc., Boston (1997)
Lagarias, J.: The Ultimate Challenge: The \(3x+1\) Problem. American Mathematical Society (2010). http://bookstore.ams.org/mbk-78
Larchey-Wendling, D., Monin, J.F.: Simulating Induction-Recursion for Partial Algorithms. In: TYPES 2018, Braga, Portugal (2018). https://members.loria.fr/DLarchey/files/papers/TYPES_2018_paper_19.pdf
Pollard, J.M.: A monte carlo method for factorization. BIT Numer. Math. 15(3), 331–334 (1975). https://doi.org/10.1007/BF01933667
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2018 Springer International Publishing AG, part of Springer Nature
About this paper
Cite this paper
Larchey-Wendling, D. (2018). Proof Pearl: Constructive Extraction of Cycle Finding Algorithms. In: Avigad, J., Mahboubi, A. (eds) Interactive Theorem Proving. ITP 2018. Lecture Notes in Computer Science(), vol 10895. Springer, Cham. https://doi.org/10.1007/978-3-319-94821-8_22
Download citation
DOI: https://doi.org/10.1007/978-3-319-94821-8_22
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-94820-1
Online ISBN: 978-3-319-94821-8
eBook Packages: Computer ScienceComputer Science (R0)