skip to main content
research-article
Open Access
Artifacts Evaluated & Functional / v1.1

Algebras for weighted search

Published:19 August 2021Publication History
Skip Abstract Section

Abstract

Weighted search is an essential component of many fundamental and useful algorithms. Despite this, it is relatively under explored as a computational effect, receiving not nearly as much attention as either depth- or breadth-first search. This paper explores the algebraic underpinning of weighted search, and demonstrates how to implement it as a monad transformer.

The development first explores breadth-first search, which can be expressed as a polynomial over semirings. These polynomials are generalised to the free semimodule monad to capture a wide range of applications, including probability monads, polynomial monads, and monads for weighted search. Finally, a monad transformer based on the free semimodule monad is introduced. Applying optimisations to this type yields an implementation of pairing heaps, which is then used to implement Dijkstra's algorithm and efficient probabilistic sampling. The construction is formalised in Cubical Agda and implemented in Haskell.

Skip Supplemental Material Section

Supplemental Material

3473577.mp4

Presentation Videos

Auxiliary Presentation Video

Weighted search is an essential component of many fundamental and useful algorithms. Despite this, it is relatively under explored as a computational effect, receiving not nearly as much attention as either depth- or breadth-first search. This paper explores the algebraic underpinning of weighted search, and demonstrates how to implement it as a monad transformer. The development first explores breadth-first search, which can be expressed as a polynomial over semirings. These polynomials are generalised to the free semimodule monad to capture a wide range of applications, including probability monads, polynomial monads, and monads for weighted search. Finally, a monad transformer based on the free semimodule monad is introduced. Applying optimisations to this type yields an implementation of pairing heaps, which is then used to implement Dijkstra's algorithm and efficient probabilistic sampling. The construction is formalised in Cubical Agda and implemented in Haskell.

References

  1. Lloyd Allison. 2006. Circular Programs and Self-Referential Structures. Software: Practice and Experience, 19, 2 (2006), Oct., 99–109. issn:1097-024X https://doi.org/10.1002/spe.4380190202 Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Thorsten Altenkirch, Thomas Anberrée, and Nuo Li. 2011. Definable Quotients in Type Theory. http://www.cs.nott.ac.uk/~psztxa/publ/defquotients.pdfGoogle ScholarGoogle Scholar
  3. K. Amer. 1984. Equationally Complete Classes of Commutative Monoids with Monus. Algebra Universalis, 18, 1 (1984), Feb., 129–131. issn:1420-8911 https://doi.org/10.1007/BF01182254 Google ScholarGoogle ScholarCross RefCross Ref
  4. Ulrich Berger, Ralph Matthes, and Anton Setzer. 2019. Martin Hofmann’s Case for Non-Strictly Positive Data Types. In 24th International Conference on Types for Proofs and Programs (TYPES 2018), Peter Dybjer, José Espírito Santo, and Luís Pinto (Eds.) (Leibniz International Proceedings in Informatics (LIPIcs), Vol. 130). Schloss Dagstuhl– Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany. 22. isbn:978-3-95977-106-1 issn:1868-8969 https://doi.org/10.4230/LIPIcs.TYPES.2018.1 Google ScholarGoogle ScholarCross RefCross Ref
  5. Paolo Capriotti and Ambrus Kaposi. 2014. Free Applicative Functors. Electronic Proceedings in Theoretical Computer Science, 153 (2014), June, 2–30. issn:2075-2180 https://doi.org/10.4204/EPTCS.153.2 arxiv:1403.0749. Google ScholarGoogle ScholarCross RefCross Ref
  6. A. Cayley. 1854. On the Theory of Groups as Depending on the Symbolic Equation $þ eta$n= 1 Phil. Mag.Google ScholarGoogle Scholar
  7. Keith L. Clark and Sten-Åke Tärnlund. 1977. A First Order Theory of Data and Programs. In Information Processing, Proceedings of the 7th IFIP Congress 1977, Toronto, Canada, August 8-12, 1977, Bruce Gilchrist (Ed.). North-Holland, 939–944.Google ScholarGoogle Scholar
  8. Noah Easterly. 2019. Functions and Newtype Wrappers for Traversing Trees: Rampion/Tree-Traversals. https://github.com/rampion/tree-traversalsGoogle ScholarGoogle Scholar
  9. Martin Erwig. 2001. Inductive Graphs and Functional Graph Algorithms. J. Funct. Program., 11, 5 (2001), Sept., 467–492. issn:1469-7653, 0956-7968 https://doi.org/10.1017/S0956796801004075 Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Martin Erwig and Steve Kollmansberger. 2006. FUNCTIONAL PEARLS: Probabilistic Functional Programming in Haskell. J. Funct. Prog., 16, 1 (2006), Jan., 21–34. issn:1469-7653, 0956-7968 https://doi.org/10.1017/S0956796805005721 Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Martín Escardó and Paulo Oliva. 2010. Selection Functions, Bar Recursion and Backward Induction. Mathematical Structures in Computer Science, 20, 2 (2010), April, 127–168. issn:1469-8072, 0960-1295 https://doi.org/10.1017/S0960129509990351 Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Sebastian Fischer. 2009. Reinventing Haskell Backtracking. In Informatik 2009, Im Fokus Das Leben (ATPS’09). GI Edition, 15. http://www-ps.informatik.uni-kiel.de/~sebf/data/pub/atps09.pdfGoogle ScholarGoogle Scholar
  13. Michael L. Fredman, Robert Sedgewick, Daniel D. Sleator, and Robert E. Tarjan. 1986. The Pairing Heap: A New Form of Self-Adjusting Heap. Algorithmica, 1, 1-4 (1986), Jan., 111–129. issn:0178-4617, 1432-0541 https://doi.org/10.1007/BF01840439 Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. Jeremy Gibbons and Geraint Jones. 1998. The Under-Appreciated Unfold. In Proceedings of the Third ACM SIGPLAN International Conference on Functional Programming (ICFP ’98). Association for Computing Machinery, Baltimore, Maryland, USA. 273–279. isbn:978-1-58113-024-9 https://doi.org/10.1145/289423.289455 Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Andrew Gill, John Launchbury, and Simon L. Peyton Jones. 1993. A Short Cut to Deforestation. In Proceedings of the Conference on Functional Programming Languages and Computer Architecture (FPCA ’93). Association for Computing Machinery, New York, NY, USA. 223–232. isbn:978-0-89791-595-3 https://doi.org/10.1145/165180.165214 Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Jonathan S. Golan. 2003. Semirings. In Semirings and Affine Equations over Them: Theory and Applications, Jonathan S. Golan (Ed.). Springer Netherlands, Dordrecht. 1–26. isbn:978-94-017-0383-3 https://doi.org/10.1007/978-94-017-0383-3_1 Google ScholarGoogle ScholarCross RefCross Ref
  17. Jules Hedges. 2014. Monad Transformers for Backtracking Search. Electron. Proc. Theor. Comput. Sci., 153 (2014), June, 31–50. issn:2075-2180 https://doi.org/10.4204/EPTCS.153.3 arxiv:1406.2058. Google ScholarGoogle ScholarCross RefCross Ref
  18. Ralf Hinze. 1999. Efficient Monadic-Style Backtracking. Sept., 51.Google ScholarGoogle Scholar
  19. Ralf Hinze. 2000. Deriving Backtracking Monad Transformers. In Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming (ICFP’00), Montréal, Canada, September 18-21, 2000. 35, ACM, 186. https://karczmarczuk.users.greyc.fr/TEACH/Doc/Hinze_back.pdfGoogle ScholarGoogle ScholarDigital LibraryDigital Library
  20. Ralf Hinze and Ross Paterson. 2006. Finger Trees: A Simple General-Purpose Data Structure. Journal of Functional Programming, 16, 2 (2006), 197–217. http://www.staff.city.ac.uk/~ross/papers/FingerTree.htmlGoogle ScholarGoogle ScholarDigital LibraryDigital Library
  21. Peter Höfner and Bernhard Möller. 2012. Dijkstra, Floyd and Warshall Meet Kleene. Formal Aspects of Computing, 24, 4-6 (2012), July, 459–476. issn:0934-5043, 1433-299X https://doi.org/10.1007/s00165-012-0245-4 Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. R. John Muir Hughes. 1986. A Novel Representation of Lists and Its Application to the Function "Reverse". Inform. Process. Lett., 22, 3 (1986), March, 141–144. issn:0020-0190 https://doi.org/10.1016/0020-0190(86)90059-1 Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Graham Hutton, Mauro Jaskelioff, and Andy Gill. 2010. Factorising Folds for Faster Functions. Journal of Functional Programming, 20, 3-4 (2010), July, 353–373. issn:1469-7653, 0956-7968 https://doi.org/10.1017/S0956796810000122 Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Nathan Jacobson. 1985. Basic Algebra I (2nd ed ed.). W.H. Freeman, New York. isbn:978-0-7167-1480-4 978-0-7167-1933-5 lccn:QA154.2 .J32 1985Google ScholarGoogle Scholar
  25. Mauro Jaskelioff and Eugenio Moggi. 2010. Monad Transformers as Monoid Transformers. Theoretical Computer Science, 411, 51 (2010), Dec., 4441–4466. issn:0304-3975 https://doi.org/10.1016/j.tcs.2010.09.011 Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Mauro Jaskelioff and Exequiel Rivas. 2015. Functional Pearl: A Smart View on Datatypes. In Proceedings of the 20th ACM SIGPLAN International Conference on Functional Programming (ICFP 2015). ACM, New York, NY, USA. 355–361. isbn:978-1-4503-3669-7 https://doi.org/10.1145/2784731.2784743 Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Geraint Jones and Jeremy Gibbons. 1993. Linear-Time Breadth-First Tree Algorithms: An Exercise in the Arithmetic of Folds and Zips. Dept of Computer Science, University of Auckland. http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/linear.ps.gzGoogle ScholarGoogle Scholar
  28. Mark P. Jones. 1995. Functional Programming with Overloading and Higher-Order Polymorphism. In Advanced Functional Programming, Gerhard Goos, Juris Hartmanis, Jan Leeuwen, Johan Jeuring, and Erik Meijer (Eds.). 925, Springer Berlin Heidelberg, Berlin, Heidelberg. 97–136. isbn:978-3-540-59451-2 978-3-540-49270-2 https://doi.org/10.1007/3-540-59451-5_4 Google ScholarGoogle ScholarCross RefCross Ref
  29. Donnacha Oisín Kidney and Nicolas Wu. 2021. Supporting Code for Algebras for Weighted Search. Zenodo. https://doi.org/10.5281/zenodo.4774319 Google ScholarGoogle ScholarCross RefCross Ref
  30. David J. King and John Launchbury. 1995. Structuring Depth-First Search Algorithms in Haskell. In Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages - POPL ’95. ACM Press, San Francisco, California, United States. 344–354. isbn:978-0-89791-692-9 https://doi.org/10.1145/199448.199530 Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. Oleg Kiselyov, Chung-chieh Shan, Daniel P. Friedman, and Amr Sabry. 2005. Backtracking, Interleaving, and Terminating Monad Transformers: (Functional Pearl). In Proceedings of the Tenth ACM SIGPLAN International Conference on Functional Programming (ICFP ’05). ACM, New York, NY, USA. 192–203. isbn:978-1-59593-064-4 https://doi.org/10.1145/1086365.1086390 Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Daniel H. Larkin, Siddhartha Sen, and Robert E. Tarjan. 2013. A Back-to-Basics Empirical Study of Priority Queues. In 2014 Proceedings of the Meeting on Algorithm Engineering and Experiments (ALENEX). Society for Industrial and Applied Mathematics, 61–72. https://doi.org/10.1137/1.9781611973198.7 Google ScholarGoogle ScholarCross RefCross Ref
  33. John Launchbury, Sava Krstic, and Timothy E. Sauerwein. 2000. Zip Fusion with Hyperfunctions. http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.36.4961Google ScholarGoogle Scholar
  34. Conor McBride and Ross Paterson. 2008. Applicative Programming with Effects. Journal of Functional Programming, 18, 1 (2008), Jan., 1–13. issn:1469-7653, 0956-7968 https://doi.org/10.1017/S0956796807006326 Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. M. Douglas McIlroy. 1999. Power Series, Power Serious. J. Funct. Program., 9, 3 (1999), May, 325–337. issn:0956-7968 https://doi.org/10.1017/S0956796899003299 Google ScholarGoogle ScholarDigital LibraryDigital Library
  36. Lambert Meertens. 1992. Paramorphisms. Formal Aspects of Computing, 4, 5 (1992), Sept., 413–424. issn:1433-299X https://doi.org/10.1007/BF01211391 Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. Mehryar Mohri. 2002. Semiring Frameworks and Algorithms for Shortest-Distance Problems. J. Autom. Lang. Comb., 7, 3 (2002), Jan., 321–350. issn:1430-189X http://dl.acm.org/citation.cfm?id=639508.639512Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. Chris Okasaki. 2000. Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design. In Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming (ICFP ’00). ACM, New York, NY, USA. 131–136. isbn:978-1-58113-202-1 https://doi.org/10.1145/351240.351253 Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. Ross Paterson. 2012. Constructing Applicative Functors. In Mathematics of Program Construction, Jeremy Gibbons and Pablo Nogueira (Eds.) (Lecture Notes in Computer Science). Springer Berlin Heidelberg, 300–323. isbn:978-3-642-31113-0Google ScholarGoogle Scholar
  40. Maciej Piróg. 2016. Eilenberg– Moore Monoids and Backtracking Monad Transformers. Electron. Proc. Theor. Comput. Sci., 207 (2016), April, 23–56. issn:2075-2180 https://doi.org/10.4204/EPTCS.207.2 arxiv:1604.01184. Google ScholarGoogle ScholarCross RefCross Ref
  41. Exequiel Rivas and Mauro Jaskelioff. 2014. Notions of Computation as Monoids. arXiv:1406.4823 [cs, math], May, arxiv:1406.4823. arxiv:1406.4823Google ScholarGoogle Scholar
  42. Exequiel Rivas, Mauro Jaskelioff, and Tom Schrijvers. 2018. A Unified View of Monadic and Applicative Non-Determinism. Science of Computer Programming, 152 (2018), Jan., 70–98. issn:0167-6423 https://doi.org/10.1016/j.scico.2017.09.007 Google ScholarGoogle ScholarDigital LibraryDigital Library
  43. Daniel Dominic Sleator and Robert Endre Tarjan. 1985. Self-Adjusting Binary Search Trees. J. ACM, 32, 3 (1985), July, 652–686. issn:00045411 https://doi.org/10.1145/3828.3835 Google ScholarGoogle ScholarDigital LibraryDigital Library
  44. Leon P Smith. 2009. Lloyd Allison’s Corecursive Queues: Why Continuations Matter. The Monad.Reader, 14, 14 (2009), July, 28. https://meldingmonads.files.wordpress.com/2009/06/corecqueues.pdfGoogle ScholarGoogle Scholar
  45. J. Michael Spivey. 2009. Algebras for Combinatorial Search. Journal of Functional Programming, 19, 3-4 (2009), July, 469–487. issn:1469-7653, 0956-7968 https://doi.org/10.1017/S0956796809007321 Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. J. Michael Spivey and Silvija Seres. 2003. Combinators for Logic Programming. In The Fun of Programming. Palgrave. https://www.cs.ox.ac.uk/publications/books/fop/dist/fop/chapters/9/Logic.hsGoogle ScholarGoogle Scholar
  47. Michael Spivey. 2000. Combinators for Breadth-First Search. Journal of Functional Programming, 10, 4 (2000), July, 397–408. issn:1469-7653, 0956-7968 https://www.cambridge.org/core/journals/journal-of-functional-programming/article/combinators-for-breadthfirst-search/60383337C85657F3F6549C18F4E345BAGoogle ScholarGoogle ScholarDigital LibraryDigital Library
  48. Alexander Vandenbroucke, Tom Schrijvers, and Frank Piessens. 2015. Fixing Non-Determinism. In Proceedings of the 27th Symposium on the Implementation and Application of Functional Programming Languages - IFL ’15. ACM Press, Koblenz, Germany. 1–12. isbn:978-1-4503-4273-5 https://doi.org/10.1145/2897336.2897342 Google ScholarGoogle ScholarDigital LibraryDigital Library
  49. Andrea Vezzosi, Anders Mörtberg, and Andreas Abel. 2019. Cubical Agda: A Dependently Typed Programming Language with Univalence and Higher Inductive Types. Proc. ACM Program. Lang., 3, ICFP (2019), July, 87:1–87:29. issn:2475-1421 https://doi.org/10.1145/3341691 Google ScholarGoogle ScholarDigital LibraryDigital Library
  50. Jeffrey S. Vitter. 1985. Random Sampling with a Reservoir. ACM Trans. Math. Softw., 11, 1 (1985), March, 37–57. issn:0098-3500, 1557-7295 https://doi.org/10.1145/3147.3165 Google ScholarGoogle ScholarDigital LibraryDigital Library
  51. Janis Voigtländer. 2008. Asymptotic Improvement of Computations over Free Monads. In Mathematics of Program Construction (Lecture Notes in Computer Science, Vol. 5133). Springer, Berlin, Heidelberg, 388–403. isbn:978-3-540-70593-2 978-3-540-70594-9 https://doi.org/10.1007/978-3-540-70594-9_20 Google ScholarGoogle ScholarDigital LibraryDigital Library
  52. Philip Wadler. 1985. How to Replace Failure by a List of Successes: A Method for Exception Handling, Backtracking, and Pattern Matching in Lazy Functional Languages. In Functional Programming Languages and Computer Architecture, FPCA 1985, Nancy, France, September 16-19, 1985, Proceedings. 113–128. https://doi.org/10.1007/3-540-15975-4_33 Google ScholarGoogle ScholarCross RefCross Ref
  53. Philip Wadler. 1990. Comprehending Monads. In Proceedings of the 1990 ACM Conference on LISP and Functional Programming (LFP ’90). Association for Computing Machinery, New York, NY, USA. 61–78. isbn:978-0-89791-368-3 https://doi.org/10.1145/91556.91592 Google ScholarGoogle ScholarDigital LibraryDigital Library
  54. Friedrich Wehrung. 1992. Injective Positively Ordered Monoids I. Journal of Pure and Applied Algebra, 83, 1 (1992), Nov., 43–82. issn:0022-4049 https://doi.org/10.1016/0022-4049(92)90104-N Google ScholarGoogle ScholarCross RefCross Ref

Index Terms

  1. Algebras for weighted search

                    Recommendations

                    Comments

                    Login options

                    Check if you have access through your login credentials or your institution to get full access on this article.

                    Sign in

                    Full Access

                    PDF Format

                    View or Download as a PDF file.

                    PDF

                    eReader

                    View online with eReader.

                    eReader
                    About Cookies On This Site

                    We use cookies to ensure that we give you the best experience on our website.

                    Learn more

                    Got it!