Typed Contracts for Functional Programming

  • Ralf Hinze
  • Johan Jeuring
  • Andres Löh
Part of the Lecture Notes in Computer Science book series (LNCS, volume 3945)


A robust software component fulfills a contract: it expects data satisfying a certain property and promises to return data satisfying another property. The object-oriented community uses the design-by-contract approach extensively. Proposals for language extensions that add contracts to higher-order functional programming have appeared recently. In this paper we propose an embedded domain-specific language for typed, higher-order and first-class contracts, which is both more expressive than previous proposals, and allows for a more informative blame assignment. We take some first steps towards an algebra of contracts, and we show how to define a generic contract combinator for arbitrary algebraic data types. The contract language is implemented as a library in Haskell using the concept of generalised algebraic data types.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Meyer, B.: Applying ‘design by contract’. IEEE Computer 25(10), 40–51 (1992)CrossRefGoogle Scholar
  2. 2.
    Meyer, B.: Eiffel: The Language. Prentice Hall, Englewood Cliffs (1992)MATHGoogle Scholar
  3. 3.
    Findler, R.B., Felleisen, M.: Contracts for higher-order functions. ACM SIGPLAN Notices 37(9), 48–59 (2002)CrossRefMATHGoogle Scholar
  4. 4.
    Peyton Jones, S.: Haskell 98 Language and Libraries. Cambridge University Press, Cambridge (2003)MATHGoogle Scholar
  5. 5.
    The GHC Team: The Glorious Glasgow Haskell Compilation System User’s Guide, Version 6.4.1 (2005), http://www.haskell.org/ghc/
  6. 6.
    Blume, M., McAllester, D.: A sound (and complete) model of contracts. ACM SIGPLAN Notices 39(9), 189–200 (2004)CrossRefMATHGoogle Scholar
  7. 7.
    Xi, H., Chen, C., Chen, G.: Guarded recursive datatype constructors. In: POPL 2003, pp. 224–235. ACM Press, New York (2003)Google Scholar
  8. 8.
    Hinze, R.: Fun with phantom types. In: Gibbons, J., de Moor, O. (eds.) The Fun of Programming, pp. 245–262. Palgrave Macmillan, Basingstoke (2003); ISBN 1-4039-0772-2 hardback, ISBN 0-333-99285-7 paperbackCrossRefGoogle Scholar
  9. 9.
    Peyton Jones, S., Washburn, G., Weirich, S.: Wobbly types: Type inference for generalised algebraic data types. Technical Report MS-CIS-05-26, University of Pennsylvania (2005)Google Scholar
  10. 10.
    Hinze, R.: Polytypic values possess polykinded types. Science of Computer Programming 43, 129–159 (2002)MathSciNetCrossRefMATHGoogle Scholar
  11. 11.
    Hutton, G.: A tutorial on the universality and expressiveness of fold. Journal of Functional Programming 9(4), 355–372 (1999)CrossRefMATHGoogle Scholar
  12. 12.
    Wadler, P.: Theorems for free! In: The Fourth International Conference on Functional Programming Languages and Computer Architecture (FPCA 1989), London, UK, pp. 347–359. Addison-Wesley Publishing Company, Reading (1989)CrossRefGoogle Scholar
  13. 13.
    Findler, R.B.: Behavioral software contracts (dissertation). Technical Report TR02-402, Department of Computer Science, Rice University (2002)Google Scholar
  14. 14.
    Blume, M., McAllester, D.: Sound and complete models of contracts. Journal of Functional Programming (2006) (to appear)Google Scholar
  15. 15.
    Findler, R.B., Blume, M., Felleisen, M.: An investigation of contracts as projections. Technical Report TR-2004-02, The University of Chicago (2004)Google Scholar
  16. 16.
    Nordström, B., Petersson, K., Smith, J.: Programming in Martin-Löf’s Type Theory. Oxford University Press, Oxford (1990)MATHGoogle Scholar
  17. 17.
    Claessen, K., Runciman, C., Chitil, O., Hughes, J., Wallace, M.: Testing and tracing lazy functional programs using Quickcheck and Hat. In: Jeuring, J., Peyton Jones, S. (eds.) AFP 2002. LNCS, vol. 2638. Springer, Heidelberg (2003)CrossRefGoogle Scholar
  18. 18.
    Chitil, O., McNeill, D., Runciman, C.: Lazy assertions. In: Trinder, P., Michaelson, G.J., Peña, R. (eds.) IFL 2003. LNCS, vol. 3145, pp. 1–19. Springer, Heidelberg (2004)CrossRefGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2006

Authors and Affiliations

  • Ralf Hinze
    • 1
  • Johan Jeuring
    • 2
  • Andres Löh
    • 1
  1. 1.Institut für Informatik IIIUniversität BonnBonnGermany
  2. 2.Institute of Information and Computing SciencesUtrecht UniversityUtrechtThe Netherlands

Personalised recommendations