Lazy Contract Checking for Immutable Data Structures

Existing contract checkers for data structures force programmers to choose between poor alternatives. Contracts are either built into the functions that construct the data structure, meaning that each object can only be used with a single contract and that a data structure with an invariant cannot be viewed as a subtype of the data structure without the invariant (thus inhibiting abstraction) or contracts are checked eagerly when an operation on the data structure is invoked, meaning that many redundant checks are performed, potentially even changing the program's asymptotic complexity. We explore the idea of adding a small, controlled amount of laziness to contract checkers so that the contracts on a data structure are only checked as the program inspects the data structure. Unlike contracts on the constructors, our lazy contracts allow subtyping and thus preserve the potential for abstraction. Unlike eagerly-checked contracts, our contracts do not affect the asymptotic behavior of the program. This paper presents our implementation of these ideas, an optimization in our implementation, performance measurements, and a discussion of an extension to our implementation that admits more expressive contracts by loosening the strict asymptotic guarantees and only preserving the amortized asymptotic complexity.

[1]  Matthias Felleisen,et al.  Contracts for higher-order functions , 2002, ICFP '02.

[2]  Ralf Hinze,et al.  Typed Contracts for Functional Programming , 2006, FLOPS.

[3]  Chris Okasaki,et al.  Purely functional data structures , 1998 .

[4]  Colin Runciman,et al.  Lazy Assertions , 2003, IFL.

[5]  David S. Rosenblum A Practical Approach to Programming With Assertions , 1995, IEEE Trans. Software Eng..

[6]  Robert Bruce Findler,et al.  Contracts as Pairs of Projections , 2006, FLOPS.

[7]  Reinhold Plösch,et al.  Contracts: from analysis to C++ implementation , 1999, Proceedings of Technology of Object-Oriented Languages and Systems - TOOLS 30 (Cat. No.PR00278).

[8]  John L. Bruno,et al.  jContractor: A Reflective Java Library to Support Design by Contract , 1999, Reflection.

[9]  Reinhold Plösch Design by Contract for Python , 1997, APSEC.

[10]  Matthew Flatt,et al.  PLT MzScheme: Language Manual , 2000 .

[11]  Ernesto Pimentel,et al.  Design by Contract in Smalltalk , 1996, J. Object Oriented Program..

[12]  Desmond D'Souza,et al.  Working with OMT: Model Integration , 1995, J. Object Oriented Program..

[13]  Matthias Felleisen,et al.  How to Design Programs , 2001 .

[14]  Werner Kluge,et al.  Implementation of Functional Languages , 1996, Lecture Notes in Computer Science.

[15]  Amr Sabry,et al.  The essence of compiling with continuations , 1993, PLDI '93.

[16]  David Lorge Parnas,et al.  A technique for software module specification with examples , 1972, CACM.

[17]  David A. McAllester,et al.  A Min-Cover Approach for Finding Salient Curves , 2006, 2006 Conference on Computer Vision and Pattern Recognition Workshop (CVPRW'06).

[18]  Olaf Chitil,et al.  Monadic, Prompt Lazy Assertions in Haskell , 2007, APLAS.

[19]  Amr Sabry,et al.  The essence of compiling with continuations (with retrospective) , 1993, PLDI 1993.

[20]  Bertrand Meyer,et al.  Eiffel: The Language , 1991 .

[21]  Pattie Maes,et al.  Meta-Level Architectures and Reflection , 1988 .

[22]  Olaf Chitil,et al.  A Pattern Logic for Prompt Lazy Assertions in Haskell , 2006, IFL.