In call-by-value languages, some mutually-recursive value 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 (right-to-left) directed into an algorithmic 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/grammatical 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]
Jean-Philippe Bernardy,et al.
A unified view of modalities in type systems
,
2020,
Proc. ACM Program. Lang..
[2]
Didier Rémy,et al.
Ambivalent Types for Principal Type Inference with GADTs
,
2013,
APLAS.
[3]
Pascal Zimmer,et al.
Recursion in the call-by-value lambda-calculus
,
2002,
FICS.
[4]
Derek Dreyer,et al.
A type system for well-founded recursion
,
2004,
POPL.
[5]
Gérard Boudol.
The Recursive Record Semantics of Objects Revisited
,
2001,
ESOP.
[6]
Tom Hirschowitz,et al.
Compilation of extended recursion in call-by-value functional languages
,
2003,
PPDP '03.
[7]
Don Syme.
Initializing Mutually Referential Abstract Objects: The Value Recursion Challenge
,
2006,
Electron. Notes Theor. Comput. Sci..