Skip to main content
×
Home
    • Aa
    • Aa

From natural semantics to C: A formal derivation of two STG machines

  • ALBERTO DE LA ENCINA (a1) and RICARDO PEÑA (a1)
Abstract
Abstract

The Spineless Tag-less G-machine (STG machine) was defined as the target abstract machine for compiling the lazy functional language Haskell. It is at the heart of the Glasgow Haskell Compiler (GHC) which is claimed to be the Haskell compiler that generates the most efficient code. A high-level description of the STG machine can be found in Peyton Jones (In Journal of Functional programming, 2(2), 127–202, 1992), Marlow & Peyton Jones (In Sigplan Not., 39(9), 4–5, 2004), and Marlow & Peyton Jones (In Journal of Functional Programming, 16(4–5), 415–449, 2006). Should the reader be interested in a more detailed view, then the only additional information available is the Haskell code of GHC and the C code of its runtime system.

It is hard to prove that this machine correctly implements the lazy semantics of Haskell. Part of the problem lies in the fact that the STG machine executes a bare-bones functional language, called STGL, much lower level than Haskell. Therefore, part of the correctness should be—and it is—established by showing that the translation from Haskell to STGL preserves Haskell's semantics.

The other part involves showing that the STG machine correctly implements the lazy semantics of STGL. In this paper we provide a step-by-step formal derivation of the STG machine and of its compilation to C, starting from a natural semantics of STGL. Thus, our starting point is higher level than the descriptions found Peyton Jones (In Journal of Functional programming, 2(2), 127–202, 1992) and Marlow & Peyton Jones (In Sigplan Not., 39(9), 4–5, 2004), and our arrival point is lower level than those works. Additionally, there has been substantial changes between the so-called push/enter model of the STG machine described in Peyton Jones (In Journal of Functional programming, 2(2), 127–202, 1992), and the eval/apply model of the STG machine described in Marlow & Peyton Jones (In Sigplan Not., 39(9), 4–5, 2004). So, in fact, we derive two machines instead of one, starting from the same initial semantics.

At each step we provide enough intuitions and explanations in order to understand the refinement, and then the formal definitions and statements proving that the derivation step is sound and complete. The main contribution of the paper is to show that an efficient machine such as the STG can be presented, understood, and formally reasoned about at different levels of abstraction.

Copyright
References
Hide All
A. de la Encina , & R. Peña (2002). Proving the correctness of the STG machine. In Selected Papers of Implementation of Functional Languages, IFL'01, Stockholm, Sweden. LNCS 2312. Springer. pp. 88104.

S. Marlow , & S. L. Peyton Jones (2004). Making a fast curry: Push/enter vs. eval/apply for higher-order languages. Sigplan Not. 39 (9), 415.

G. Morrisett , M. Felleisen , & R. Harper (1995). Abstract models of memory management. In International Conference on Functional Programming Languages and Computer Architecture, FPCA'95. La Jolla, California: ACM Press, pp. 6677.

J. Mountjoy (1998). The spineless tagless G-machine, naturally. In Third International Conference on Functional Programming, ICFP'98. Baltimore: ACM Press, pp. 163173.

S. L. Peyton Jones , & J. Salkild (1989). The spineless tagless G-machine. In Proceedings of Functional Programming Languages and Computer Arquitecture Conference. FPCA'89, London. ACM, pp. 184201.

S. L. Peyton Jones , & A. Santos (1998). A transformation-based optimiser for haskell. Sci. Computer Program. 32 (1-3), 347.

S. L. Peyton Jones , & J. Launchbury (1991). Unboxed values as first class citizens in a non-strict functional language. Conference on Functional Programming Languages and Computer Architecture FPCA'91, Cambridge, MA. LNCS 523. Springer. pp. 636666.

P. Sestoft (1997). Deriving a Lazy Abstract Machine. J. Funct. Program. 7 (3), 231264.

C. Urban , A. M. Pitts , & M. J. Gabbay (2004). Nominal Unification. Theor. Comput. Sci. 323, 473497.

Recommend this journal

Email your librarian or administrator to recommend adding this journal to your organisation's collection.

Journal of Functional Programming
  • ISSN: 0956-7968
  • EISSN: 1469-7653
  • URL: /core/journals/journal-of-functional-programming
Please enter your name
Please enter a valid email address
Who would you like to send this to? *
×

Metrics

Full text views

Total number of HTML views: 0
Total number of PDF views: 6 *
Loading metrics...

Abstract views

Total abstract views: 79 *
Loading metrics...

* Views captured on Cambridge Core between September 2016 - 17th October 2017. This data will be updated every 24 hours.