From natural semantics to C: A formal derivation of two STG machines (original) (raw)

Formally deriving an STG machine

Proceedings of the 5th ACM SIGPLAN international conference on Principles and practice of declaritive programming - PPDP '03, 2003

Starting from P. Sestoft semantics for lazy evaluation, we define a new semantics in which normal forms consist of variables pointing to lambdas or constructions. This is in accordance with the more recent changes in the Spineless Tagless G-machine (STG) machine, where constructions only appear in closures (lambdas only appeared in closures already in previous versions). We prove the equivalence between the new semantics and Sestoft's. Then, a sequence of STG machines are derived, formally proving the correctness of each derivation. The last machine consists of a few imperative instructions and its distance to a conventional language is minimal. The paper also discusses the differences between the final machine and the actual STG machine implemented in the Glasgow Haskell Compiler.

An Action Semantics for STG

Action Semantics, 2000

STG (Shared Term Graph) is a very simple higher order non-strict pure functional language, primarily designed to be the "abstract machine code" of STG-machine (Spineless Tagless G-Machine), an abstract machine designed to support implementation of lazy functional languages, like Haskell, on stock hardware. GHC and Haskell/GammaCMC are compilers for Haskell that use STG as intermediate code. This paper gives an action semantics for STG, trying to capture its essential characteristics, like lazy evaluation, higher order functions, pattern matching by case expressions and support for algebraic values.

From Interpretation to Compilation

Lecture Notes in Computer Science, 2008

In this paper we sketch some experiments with the construction of a simple compiler for a high level intermediate lazy functional language, with C++ as a target language. Because the compiler is intended for educational and experimental use, simplicity and clearness of construction are considered to be more important than efficiency. Starting point for the construction is a simple interpreter. In a first step this interpreter is turned into a simple compiler in a straightforward manner. The performance of a number of compiled benchmarks is analysed in a comparison with the interpreter and the Clean and GHC compilers. This analysis leads to some suggestions for optimisations. Of these optimisations tail recursion optimisation and optimisation of numerical functions and numerical (sub)expressions in functions are implemented. It turns out that in many cases these optimisations suffice to obtain a competitive performance.

The semantics of lazy functional languages

Theoretical Computer Science, 1989

A denotationaf semantics for the A-calculus is described. The semantics is cotinuationbased, and so reflects the order in which expressions are evaluated. It provides a means by which lazy functional languages can be better understood.

A Lazy to Strict Language Compiler

2017

The evaluation strategies of programming languages can be broadly categorised as strict or lazy. A common approach to strict evaluation is to implement a call-by-value semantics that always evaluates expressions when they are bound to variables, while lazy evaluation is often implemented as call-by-need semantics that evaluates expressions when they are needed for some computation. Lazy semantics makes use of a data structure called thunk that contains an expression, whose evaluation has become suspended, together with its environment. This thesis presents (1) a Haskell definition of the existing semantics of CakeML, a strict programming language, (2) a Haskell definition of a lazy semantics for the pure part of CakeML, and (3) a Haskell implementation of a compiler that compiles lazy CakeML to strict CakeML as defined in (1) and (2). The compiler makes use of stateful features in strict CakeML to optimise evaluation so that each thunk is evaluated at most once, simulating a call-by...

A Dynamic Semantics for Haskell

1992

This paper defines a dynamic semantics for the functional programming language Haskell. The semantics defines the meaning of expressions, modules and programs, including all nonoptionalI/O requests. All constructs in the Haskell language are considered. We use thesame natural semantics style as the definition of Standard ML. For brevity, we assume that the translations specified by the Wadler/Peyton Jones staticsemantics have been performed, and base our source language on the target language of thestatic semantics.

From interpreter to compiler and virtual machine: a functional derivation

2003

We show how to derive a compiler and a virtual machine from a compositional interpreter. We first illustrate the derivation with two evaluation functions and two normalization functions. We obtain Krivine's machine, Felleisen et al.'s CEK machine, and a generalization of these machines performing strong normalization, which is new. We observe that several existing compilers and virtual machines-e.g., the Categorical Abstract Machine (CAM), Schmidt's VEC machine, and Leroy's Zinc abstract machine-are already in derived form and we present the corresponding interpreter for the CAM and the VEC machine. We also consider Hannan and Miller's CLS machine and Landin's SECD machine.

Functional low-level interpreters

2002

We show how to construct in a pure, lazy functional language Haskell a low-level, FORTH-like (stack based, postfix style) virtual machine, used as a target platform in our compilation course for computer science students. The implementation is clear, reasonably efficient, and easily extensible. Two models are presented: more “classical”, with a separate return stack, and an interpreter based on the continuation-passing style. The idea, although traditional, was a bit experimental, since this was the first contact of our students with Haskell and with advanced functional techniques. We exploited thus this course to teach also those techniques, on a comprehensive and conceptually coherent set of examples.

A new programming technique for lazy functional languages

Science of Computer Programming, 1995

In this paper we present a new programming technique for lazy functional programming languages. The technique is embedded in a programming methodology which is based on divide and conquer: the division of problems into subproblems. Such a division will be represented by a call graph.

Proving the Correctness of the STG Machine

Lecture Notes in Computer Science, 2002

J. Launchbury gave an operational semantics for lazy evaluation and showed that it is sound and complete w.r.t. a denotational semantics of the language. P. Sestoft then introduced several abstract machines for lazy evaluation and showed that they were sound and complete w.r.t. Launchbury's operational semantics. We go a step forward and show that the Spineless Tagless G-machine is complete and (almost) sound w.r.t. one of Sestoft's machines. In the way to this goal we also prove some interesting properties about the operational semantics and about Sestoft's machines which clarify some minor points on garbage collection and on closures' local environments. Unboxed values and primitive operators are excluded from the study.