Correctness of monadic state: an imperative call-by-need calculus

The extension of Haskell with a built-in state monad combines mathematical elegance with operational efficiency: ¿Semantically, at the source language level, constructs that act on the state are viewed as functions that pass an explicit store data structure around. ¿Operationally, at the implementation level, constructs that act on the state are viewed as statements whose evaluation has the side-effect of updating the implicit global store in place.There are several unproven conjectures that the two views are consistent.Recently, we have noted that the consistency of the two views is far from obvious: all it takes for the implementation to become unsound is one judiciously-placed beta-step in the optimization phase of the compiler. This discovery motivates the current paper in which we formalize and show the correctness of the implementation of monadic state.For the proof, we first design a typed call-by-need language that models the intermediate language of the compiler, together with a type-preserving compilation map. Second, we show that the compilation is semantics-preserving by proving that the compilation of every source axiom yields an observational equivalence of the target language. Because of the wide semantic gap between the source and target languages, we perform this last step using an additional intermediate language.The imperative call-by-need ¿-Calculus is of independent interest for reasoning about system-level Haskell code providing services such as memo-functions, generation of new names, etc., and is the starting point for reasoning about the space usage of Haskell programs.

[1]  John Field,et al.  A Simple Rewriting Semantics for Realistic Imperative Programs and its Application to Program Analysis , 1992, ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation.

[2]  Robert Hieb,et al.  The Revised Report on the Syntactic Theories of Sequential Control and State , 1992, Theor. Comput. Sci..

[3]  Robert Harper,et al.  TIL: a type-directed optimizing compiler for ML , 1996, PLDI '96.

[4]  Paul Hudak,et al.  Mutable abstract datatypes -or- how to have your state and mange it too , 1992 .

[5]  Simon L. Peyton Jones,et al.  Lazy functional state threads , 1994, PLDI '94.

[6]  Simon L. Peyton Jones,et al.  Bridging the gulf: a common intermediate language for ML and Haskell , 1998, POPL '98.

[7]  Amr Sabry,et al.  What is a purely functional language? , 1998, Journal of Functional Programming.

[8]  Zena M. Ariola,et al.  Lambda Calculus with Explicit Recursion , 1997, Inf. Comput..

[9]  Uday S. Reddy,et al.  Imperative functional programming , 1996, CSUR.

[10]  Matthias Felleisen,et al.  The call-by-need lambda calculus , 1997, Journal of Functional Programming.

[11]  Peter Sestoft,et al.  Replacing function parameters by global variables , 1989, FPCA.

[12]  Matthias Felleisen,et al.  Parameter-passing and the lambda calculus , 1991, POPL '91.

[13]  Philip Wadler,et al.  The essence of functional programming , 1992, POPL '92.

[14]  B. Cook,et al.  Disposable Memo Functions , 1997 .

[15]  Amr Sabry,et al.  Monadic state: axiomatization and type safety , 1997, ICFP '97.

[16]  Simon L. Peyton Jones,et al.  The Implementation of Functional Programming Languages , 1987 .

[17]  John Launchbury,et al.  Disposable memo functions (extended abstract) , 1997, ICFP '97.

[18]  Matthias Felleisen,et al.  A call-by-need lambda calculus , 1995, POPL '95.

[19]  Colin Runciman,et al.  Linearity and Laziness , 1991, FPCA.

[20]  John Hughes,et al.  Lazy Memo-functions , 1985, FPCA.

[21]  Amr Sabry,et al.  Reasoning about Explicit and Implicit Representations of State , 1993 .

[22]  David A. Schmidt Detecting global variables in denotational specifications , 1985, TOPL.

[23]  Paul Hudak,et al.  Single-threaded polymorphic lambda calculus , 1990, [1990] Proceedings. Fifth Annual IEEE Symposium on Logic in Computer Science.

[24]  Stefan Blom,et al.  Cyclic Lambda Calculi , 1997, TACS.

[25]  Paul Hudak,et al.  Rolling your own mutable ADT—a connection between linear types and monads , 1997, POPL '97.

[26]  Philip Wadler,et al.  The essence of functional programming (Invited talk) , 1997 .

[27]  Matthias Felleisen,et al.  The semantics of program dependence , 1989, PLDI '89.

[28]  Philip Wadler,et al.  Comprehending monads , 1990, LISP and Functional Programming.

[29]  Simon L. Peyton Jones,et al.  State in Haskell , 1995, LISP Symb. Comput..

[30]  Philip Wadler Is there a use for linear logic , 1991 .

[31]  Paul Hudak,et al.  Call by name, assignment, and the lambda calculus , 1993, POPL '93.

[32]  Vipin Swarup,et al.  Assignments for Applicative Languages , 1991, FPCA.

[33]  Simon L. Peyton Jones,et al.  Unboxed Values as First Class Citizens in a Non-Strict Functional Language , 1991, FPCA.