A practical mode system for recursive definitions

In call-by-value languages, some mutually-recursive definitions can be safely evaluated to build recursive functions or cyclic data structures, but some definitions (let rec x = x + 1) contain vicious circles and their evaluation fails at runtime. We propose a new static analysis to check the absence of such runtime failures. We present a set of declarative inference rules, prove its soundness with respect to the reference source-level semantics of Nordlander, Carlsson, and Gill [2008], and show that it can be directed into an algorithmic backwards analysis check in a surprisingly simple way. Our implementation of this new check replaced the existing check used by the OCaml programming language, a fragile syntactic criterion which let several subtle bugs slip through as the language kept evolving. We document some issues that arise when advanced features of a real-world functional language (exceptions in first-class modules, GADTs, etc.) interact with safety checking for recursive definitions.

[1]  Robin Milner,et al.  Definition of standard ML , 1990 .

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

[3]  John Launchbury,et al.  A natural semantics for lazy evaluation , 1993, POPL '93.

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

[5]  Gérard Boudol The Recursive Record Semantics of Objects Revisited , 2001, ESOP.

[6]  Samir Genaim,et al.  Inferring termination conditions for logic programs using backwards analysis , 2001, Theory and Practice of Logic Programming.

[7]  Pascal Zimmer,et al.  Recursion in the call-by-value lambda-calculus , 2002, FICS.

[8]  Tom Hirschowitz,et al.  Compilation of extended recursion in call-by-value functional languages , 2003, PPDP '03.

[9]  Derek Dreyer,et al.  A type system for well-founded recursion , 2004, POPL.

[10]  R. Kent Dybvig,et al.  Fixing Letrec: A Faithful Yet Efficient Implementation of Scheme's Recursive Binding Construct , 2005, High. Order Symb. Comput..

[11]  Don Syme An Alternative Approach to Initializing Mutually Referential Objects , 2005 .

[12]  Don Syme Initializing Mutually Referential Abstract Objects: The Value Recursion Challenge , 2006, Electron. Notes Theor. Comput. Sci..

[13]  Magnus Carlsson,et al.  Unrestricted pure call-by-value recursion , 2008, ML '08.

[14]  R. Kent Dybvig,et al.  Fixing Letrec ( reloaded ) , 2009 .

[15]  R. Kent Dybvig,et al.  Revised6 Report on the Algorithmic Language Scheme , 2009 .

[16]  Delia Kesner,et al.  The Structural lambda-Calculus , 2010, CSL.

[17]  Matthias Felleisen,et al.  The Call-by-need Lambda Calculus, Revisited , 2012, ESOP.

[18]  Beniamino Accattoli Evaluating functions as processes , 2013, TERMGRAPH.

[19]  Didier Rémy,et al.  Ambivalent Types for Principal Type Inference with GADTs , 2013, APLAS.

[20]  Simon L. Peyton Jones,et al.  Modular, higher-order cardinality analysis in theory and practice , 2014, POPL 2014.

[21]  Oleg Kiselyov The Design and Implementation of BER MetaOCaml - System Description , 2014, FLOPS.

[22]  Alexandra Silva,et al.  CoCaml: Functional Programming with Regular Coinductive Types , 2017, Fundam. Informaticae.

[23]  Simon Peyton Jones,et al.  Theory and practice of demand analysis in Haskell , 2017 .

[24]  Gabriel Scherer,et al.  Merlin: a language server for OCaml (experience report) , 2018, Proc. ACM Program. Lang..

[25]  Tom Hirschowitz,et al.  A practical type system for generalized recursion , 2019 .

[26]  Jean-Philippe Bernardy,et al.  A unified view of modalities in type systems , 2020, Proc. ACM Program. Lang..