skip to main content
research-article

How to keep your neighbours in order

Published: 19 August 2014 Publication History

Abstract

I present a datatype-generic treatment of recursive container types whose elements are guaranteed to be stored in increasing order, with the ordering invariant rolled out systematically. Intervals, lists and binary search trees are instances of the generic treatment. On the journey to this treatment, I report a variety of failed experiments and the transferable learning experiences they triggered. I demonstrate that a total element ordering is enough to deliver insertion and flattening algorithms, and show that (with care about the formulation of the types) the implementations remain as usual. Agda's instance arguments and pattern synonyms maximize the proof search done by the typechecker and minimize the appearance of proofs in program text, often eradicating them entirely. Generalizing to indexed recursive container types, invariants such as size and balance can be expressed in addition to ordering. By way of example, I implement insertion and deletion for 2-3 trees, ensuring both order and balance by the discipline of type checking.

References

[1]
William Aitken and John Reppy. Abstract value constructors. Technical Report TR 92-1290, Cornell University, 1992.
[2]
Robert Atkey, Patricia Johann, and Neil Ghani. Refining inductive types. Logical Methods in Computer Science, 8 (2), 2012.
[3]
Jean-Philippe Bernardy and Guilhem Moulin. Type-theory in color. In Greg Morrisett and Tarmo Uustalu, editors, ICFP, pages 61--72. ACM, 2013. ISBN 978-1-4503-2326-0.
[4]
Rod Burstall. Proving properties of programs by structural induction. Computer Journal, 12 (1): 41--48, 1969.
[5]
Pierre-Évariste Dagand and Conor McBride. Transporting functions across ornaments. In Peter Thiemann and Robby Bruce Findler, editors, ICFP, pages 103--114. ACM, 2012. ISBN 978-1-4503-1054-3.
[6]
Dominique Devriese and Frank Piessens. On the bright side of type classes: instance arguments in Agda. In Manuel M. T. Chakravarty, Zhenjiang Hu, and Olivier Danvy, editors, ICFP, pages 143--155. ACM, 2011. ISBN 978-1-4503-0865-6.
[7]
Gérard P. Huet. The zipper. J. Funct. Program., 7 (5): 549--554, 1997.
[8]
Patrik Jansson and Johan Jeuring. PolyP - a polytypic programming language. In Peter Lee, Fritz Henglein, and Neil D. Jones, editors, POPL, pages 470--482. ACM Press, 1997. ISBN 0-89791-853-3.
[9]
Stefan Kahrs. Red-black trees with types. J. Funct. Program., 11 (4): 425--432, 2001.
[10]
Fredrik Lindblad and Marcin Benke. A Tool for Automated Theorem Proving in Agda. In Jean-Christophe Filliâtre, Christine Paulin-Mohring, and Benjamin Werner, editors, TYPES, volume 3839 of Lecture Notes in Computer Science, pages 154--169. Springer, 2004. ISBN 3-540-31428-8.
[11]
Sam Lindley and Conor McBride. Hasochism: the pleasure and pain of dependently typed haskell programming. In Chung-chieh Shan, editor, Haskell, pages 81--92. ACM, 2013. ISBN 978-1-4503-2383-3.
[12]
Conor McBride. A Case For Dependent Families. LFCS Theory Seminar, Edinburgh, 2000. URL http://strictlypositive.org/a-case/.
[13]
Conor McBride and James McKinna. The view from the left. J. Funct. Program., 14 (1): 69--111, 2004.
[14]
Conor McBride and Ross Paterson. Applicative programming with effects. J. Funct. Program., 18 (1): 1--13, 2008.
[15]
Matthew Might. The missing method: Deleting from Okasaki's red-black trees. Blog post, 2010. http://matt.might.net/articles/red-black-delete/.
[16]
Robin Milner. A theory of type polymorphism in programming. J. Comput. Syst. Sci., 17 (3): 348--375, 1978.
[17]
David Turner. Elementary strong functional programming. 1987. URL http://sblp2004.ic.uff.br/papers/turner.pdf.
[18]
Philip Wadler. The concatenate vanishes. Technical report, 1987.
[19]
Hongwei Xi. Dependently Typed Data Structures. In Proceedings of Workshop of Algorithmic Aspects of Advanced Programming Languages (WAAAPL '99), pages 17--32, Paris, September 1999.

Cited By

View all

Recommendations

Comments

Information & Contributors

Information

Published In

cover image ACM SIGPLAN Notices
ACM SIGPLAN Notices  Volume 49, Issue 9
ICFP '14
September 2014
361 pages
ISSN:0362-1340
EISSN:1558-1160
DOI:10.1145/2692915
Issue’s Table of Contents
  • cover image ACM Conferences
    ICFP '14: Proceedings of the 19th ACM SIGPLAN international conference on Functional programming
    August 2014
    390 pages
    ISBN:9781450328739
    DOI:10.1145/2628136
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 19 August 2014
Published in SIGPLAN Volume 49, Issue 9

Check for updates

Author Tags

  1. agda
  2. balancing
  3. dependent types
  4. ordering
  5. sorting

Qualifiers

  • Research-article

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)39
  • Downloads (Last 6 weeks)0
Reflects downloads up to 13 Dec 2024

Other Metrics

Citations

Cited By

View all

View Options

Login options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media