JFP 15 (6): 837–891, 2005. c 2005 Cambridge University Press doi:10.1017/S0956796805005666 Printed in the United Kingdom 837 The logic of demand in Haskell WILLIAM L. HARRISON Department of Computer Science, University of Missouri, Columbia, MO, USA (e-mail: HarrisonWL@missouri.edu) RICHARD B. KIEBURTZ Pacific Software Research Center, OGI School of Science & Engineering, Oregon Health & Science University Abstract Haskell is a functional programming language whose evaluation is lazy by default. However, Haskell also provides pattern matching facilities which add a modicum of eagerness to its otherwise lazy default evaluation. This mixed or “non-strict” semantics can be quite difficult to reason with. This paper introduces a programming logic, P-logic, which neatly formalizes the mixed evaluation in Haskell pattern-matching as a logic, thereby simplifying the task of specifying and verifying Haskell programs. In P-logic, aspects of demand are reflected or represented within both the predicate language and its model theory, allowing for expressive and comprehensible program verification. 1 Introduction Although Haskell is known as a lazy functional language because of its default evaluation strategy, it contains a number of language constructs that force exceptions to that strategy. Among these features are pattern-matching, data type strictness annotations and the seq primitive. The semantics of pattern-matching are further enriched by irrefutable pattern annotations, which may be embedded within patterns. The interaction between Haskell’s default lazy evaluation and its pattern-matching is surprisingly complicated. Although it offers the programmer a facility for fine control of demand (Harrison et al., 2002), it is perhaps the aspect of the Haskell language least well understood by its community of users. In this paper, we characterize the control of demand first in a denotational semantics and then in a verification logic called “P-logic”. P-logic 1 is a modal logic based upon the familiar Gentzen-style sequent calculus (Girard, 1989). P-logic is expressive directly over Haskell expressions – the term language of the logic is Haskell 98. The two modalities of the logic, called weak and strong, determine whether a predicate is interpreted by a set of normalized values of its type (the strong interpretation) or by a set of computations of its type, which 1 The name P-logic is taken from the Programatica project (www.cse.ogi.edu/PacSoft/projects/ programatica) at OGI. https://doi.org/10.1017/S0956796805005666 Published online by Cambridge University Press