Hostname: page-component-7479d7b7d-q6k6v Total loading time: 0 Render date: 2024-07-09T15:58:26.191Z Has data issue: false hasContentIssue false

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

Published online by Cambridge University Press:  01 January 2009

ALBERTO DE LA ENCINA
Affiliation:
Departamento de Sistemas Informáticos y Computación, Universidad Complutense de Madrid, Spain (e-mail: albertoe@sip.ucm.es, ricardo@sip.ucm.es)
RICARDO PEÑA
Affiliation:
Departamento de Sistemas Informáticos y Computación, Universidad Complutense de Madrid, Spain (e-mail: albertoe@sip.ucm.es, ricardo@sip.ucm.es)
Rights & Permissions [Opens in a new window]

Abstract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

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.

Type
Articles
Copyright
Copyright © Cambridge University Press 2008

References

Ager, M. S., Biernacki, D., Danvy, O., & Midtgaard, J. (2003). A functional correspondence between evaluators and abstract machines. In Proceedings of Principles and practice of Declarative Programming, PPDP'03, Uppsala, Sweden, ACM Press, pp. 819.Google Scholar
de la Encina, A., & Peña, R. (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.CrossRefGoogle Scholar
de la Encina, A., & Peña, R. (2003): Formally deriving a STG machine. In Principles and Practice of Declarative Programming, PPDP'03. Uppsala, Sweden: ACM Press, pp. 102112.Google Scholar
Hannan, J., & Miller, D. (1992). From operational semantics to abstract machines. Math. Struct. Comput. Sci. 2 (4), 415459.CrossRefGoogle Scholar
Kluge, W. (2005). Abstract Computing Machines: A Lambda Calculus Perspective. Springer Texts in Theoretical Computer Science.Google Scholar
Launchbury, J. (1993). A Natural semantics for lazy evaluation. In Proceedings Conference on Principles of Programming Languages, POPL'93, Charleston, South Carolina, ACM Press, pp. 144154.Google Scholar
Marlow, S., & Peyton Jones, S. L. (2004). Making a fast curry: Push/enter vs. eval/apply for higher-order languages. Sigplan Not. 39 (9), 415.CrossRefGoogle Scholar
Marlow, S., & Peyton Jones, S. L. (2006). Making a fast curry: Push/enter vs. eval/apply for higher-order languages. J. Funct. Program. 16 (4-5), 415449.CrossRefGoogle Scholar
Morrisett, G., Felleisen, M., & Harper, R. (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.Google Scholar
Mountjoy, J. (1998). The spineless tagless G-machine, naturally. In Third International Conference on Functional Programming, ICFP'98. Baltimore: ACM Press, pp. 163173.CrossRefGoogle Scholar
Peyton Jones, S. L. (1992). Implementing lazy functional languages on stock hardware: The spineless tagless G-machine, version 2.5. J. Funct. Program. 2 (2), 127202.Google Scholar
Peyton Jones, S. L., & Hughes, J. (eds). (1999). Report on the Programming Language Haskell 98. URL http://www.haskell.org.Google Scholar
Peyton Jones, S. L., & Marlow, S. (2002). Secrets of the Glasgow Haskell Compiler inliner. J. Funct. Program. 12 (4), 393434.CrossRefGoogle Scholar
Peyton Jones, S. L., & Salkild, J. (1989). The spineless tagless G-machine. In Proceedings of Functional Programming Languages and Computer Arquitecture Conference. FPCA'89, London. ACM, pp. 184201.Google Scholar
Peyton Jones, S. L., & Santos, A. (1998). A transformation-based optimiser for haskell. Sci. Computer Program. 32 (1-3), 347.CrossRefGoogle Scholar
Peyton Jones, S. L., Hall, C. V., Hammond, K., Partain, W. D., & Wadler, P. L. (1993). The Glasgow Haskell Compiler: A technical overview. In Joint Framework for Information Technology. Keele, UK.Google Scholar
Peyton Jones, S. L. (1996). Compiling Haskell by Program Transformation: A report from the trenches. In 6th European Symposium on Programming ESOP'96, Sweden. LNCS 1058. Springer. pp. 1844.Google Scholar
Peyton Jones, S. L., & Launchbury, J. (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.CrossRefGoogle Scholar
Pitts, A. (2005). Alpha-structural recursion and induction. In Theorem Proving in Higher Order Logics, TPHOL'05. Oxford, UK. LNCS 3603. Springer. pp. 1734.CrossRefGoogle Scholar
Sestoft, P. (1997). Deriving a Lazy Abstract Machine. J. Funct. Program. 7 (3), 231264.Google Scholar
Urban, C., Pitts, A. M., & Gabbay, M. J. (2004). Nominal Unification. Theor. Comput. Sci. 323, 473497.CrossRefGoogle Scholar
Submit a response

Discussions

No Discussions have been published for this article.