The SECD machine is a highly influential (see: ''
Landin's contribution'')
virtual machine
In computing, a virtual machine (VM) is the virtualization or emulator, emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve ...
and
abstract machine
In computer science, an abstract machine is a theoretical model that allows for a detailed and precise analysis of how a computer system functions. It is similar to a mathematical function in that it receives inputs and produces outputs based on p ...
intended as a target for
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
s of
functional programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s. The letters stand for
stack
,
environment
,
control
,
dump
, respectively, which are the
internal registers of the machine. The registers
stack
,
control
, and
dump
point to (some realizations of)
stacks, and
environment
points to (some realization of) an
associative array
In computer science, an associative array, key-value store, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In math ...
.
The machine was the first to be specifically designed to evaluate
lambda calculus
In mathematical logic, the lambda calculus (also written as ''λ''-calculus) is a formal system for expressing computability, computation based on function Abstraction (computer science), abstraction and function application, application using var ...
expressions. It was originally described by
Peter Landin in "The Mechanical Evaluation of Expressions" in 1964. The description published by Landin was fairly abstract, and left many implementation choices open (like an
operational semantics
Operational semantics is a category of formal programming language semantics in which certain desired properties of a program, such as correctness, safety or security, are verified by constructing proofs from logical statements about its exec ...
).
Lispkit Lisp was an influential compiler based on the SECD machine, and the SECD machine has been used as the target for other systems such as
Lisp
Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation.
Originally specified in the late 1950s, ...
/
370
__NOTOC__
Year 370 ( CCCLXX) was a common year starting on Friday of the Julian calendar. At the time, it was known as the Year of the Consulship of Augustus and Valens (or, less frequently, year 1123 ''Ab urbe condita''). The denomination 370 ...
. In 1989, researchers at the
University of Calgary
{{Infobox university
, name = University of Calgary
, image = University of Calgary coat of arms without motto scroll.svg
, image_size = 150px
, caption = Coat of arms
, former ...
worked on a hardware implementation of the machine, with the same rationale as a
high-level language computer architecture A high-level language computer architecture (HLLCA) is a computer architecture designed to be targeted by a specific high-level programming language (HLL), rather than the architecture being dictated by hardware considerations. It is accordingly al ...
related to a
Lisp machine.
Landin's contribution
D. A. Turner (2012)
points out that the
ALGOL 60
ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a ...
programming language could not return functions from other functions (rendering functions no longer first-class). A function nested inside another function could refer to a variable living on the outer function's stack. If the nested function were returned from the outer function, then it would be referring to a variable in a stack frame that is no longer present. Turner notes that Landin's SECD machine solves this problem (thus allowing functions to return functions), as a function value is now represented with a
closure on the heap that can store the environment of variables it should use irrespective of what happens on the stack.
Informal description
When evaluation of an expression begins, the expression is loaded as the only element of control
C
. The environment
E
, stack
S
and dump
D
begin empty.
During evaluation of
C
it is converted to
reverse Polish notation
Reverse Polish notation (RPN), also known as reverse Łukasiewicz notation, Polish postfix notation or simply postfix notation, is a mathematical notation in which operators ''follow'' their operands, in contrast to prefix or Polish notation ...
(RPN) with
ap
(for
apply
In mathematics and computer science, apply is a function that applies a function to arguments. It is central to programming languages derived from lambda calculus, such as LISP and Scheme, and also in functional languages. It has a role in the ...
) being the only operator. For example, the expression
F (G X)
(a single list element) is changed to the list
X:G:ap:F:ap
.
Evaluation of
C
proceeds similarly to other RPN expressions. If the first item in
C
is a value, it is pushed onto the stack
S
. More exactly, if the item is an identifier, the value pushed onto the stack will be the binding for that identifier in the current environment
E
. If the item is an abstraction, a
closure is constructed to preserve the bindings of its free variables (which are in
E
), and it is this closure which is pushed onto the stack.
If the item is
ap
, two values are popped off the stack and the application done (first applied to second). If the result of the application is a value, it is pushed onto the stack.
If the application is of an abstraction to a value, however, it will result in a lambda calculus expression that may itself be an application (rather than a value), and so cannot be pushed onto the stack. In this case, the current contents of
S
,
E
, and
C
are pushed onto the dump
D
(which is a stack of these triples),
S
is reinitialized to empty, and
C
is reinitialized to the application result with
E
containing the environment for the free variables of this expression, augmented with the binding that resulted from the application. Evaluation then proceeds as above.
Completed evaluation is indicated by
C
being empty, in which case the result will be on the stack
S
. The last saved evaluation state on
D
is then popped, and the result of the completed evaluation is pushed onto the stack contents restored from
D
. Evaluation of the restored state then continues as above.
If
C
and
D
are both empty, overall evaluation has completed with the result on the stack
S
.
Registers and memory
The SECD machine is
stack-based. Functions take their arguments from the stack. The arguments to built-in instructions are encoded immediately after them in the instruction stream.
Like all internal data-structures, the stack is a list, with the
S
register pointing at the list's ''head'' or beginning. Due to the list structure, the stack need not be a continuous block of memory, so stack space is available as long as there is a single free memory cell. Even when all cells have been used,
garbage collection
Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of treatment or landfill. Waste collection also includes the curbside collection of recyclable ...
may yield additional free memory. Obviously, specific implementations of the SECD structure can implement the stack as a canonical stack structure, thus improving the overall efficiency of the virtual machine, provided that a strict bound be put on the dimension of the stack.
The
C
register points at the head of the code or instruction list that will be evaluated. Once the instruction there has been executed, the
C
is pointed at the next instruction in the list—it is similar to an ''instruction pointer'' (or
program counter
The program counter (PC), commonly called the instruction pointer (IP) in Intel x86 and Itanium microprocessors, and sometimes called the instruction address register (IAR), the instruction counter, or just part of the instruction sequencer, ...
) in conventional machines, except that subsequent instructions are always specified during execution and are not by default contained in subsequent memory locations, as is the case with the conventional machines.
The current variable environment is managed by the
E
register, which points at a list of lists. Each individual list represents one environment level: the parameters of the current function are in the head of the list, variables that are free in the current function, but bound by a surrounding function, are in other elements of
E
.
The dump, at whose head the
D
register points, is used as temporary storage for values of the other registers, for example during function calls. It can be likened to the return stack of other machines.
The memory organization of the SECD machine is similar to the model used by most functional language
interpreters
Interpreting is translation from a spoken or signed language into another language, usually in real time to facilitate live communication. It is distinguished from the translation of a written text, which can be more deliberative and make use o ...
: a number of memory cells, each of which can hold either an ''atom'' (a simple value, for example ''13''), or represent an empty or non-empty list. In the latter case, the cell holds two pointers to other cells, one representing the first element, the other representing the list except for the first element. The two pointers are traditionally named
''car'' and ''cdr'' respectively—but the more modern terms ''head'' and ''tail'' are often used instead. The different types of values that a cell can hold are distinguished by a ''
tag''. Often different types of atoms (integers, strings, etc.) are distinguished as well.
So, a list holding the numbers ''1'', ''2'', and ''3'', usually written as
(1 2 3)
, might be represented as follows:
Address Tag Content (value for integers, car & cdr for lists)
9
2 8
3 7
8 , 0 6
9 , 7 ...
2
1 , 6 1
1 0
nil
The memory cells 3 to 5 do not belong to our list, the cells of which can be distributed randomly over the memory. Cell 2 is the head of the list, it points to cell 1, which holds the first element's value, and the list containing only ''2'' and ''3'' (beginning at cell 6). Cell 6 points at a cell holding 2 and at cell 7, which represents the list containing only ''3''. It does so by pointing at cell 8 containing the value ''3'', and pointing at an empty list (''nil'') as cdr. In the SECD machine, cell 0 always implicitly represents the empty list, so no special tag value is needed to signal an empty list (everything needing that can simply point to cell 0).
The principle that the cdr in a list cell must point at another list is just a convention. If both car and cdr point at atoms, that will yield a pair, usually written like
(1 . 2)
Instructions
*
nil
pushes a nil pointer onto the stack
*
ldc
pushes a constant argument onto the stack
*
ld
pushes the value of a variable onto the stack. The variable is indicated by the argument, a pair. The pair's car specifies the level, the cdr the position. So
(1 . 3)
gives the current function's (level 1) third parameter.
*
sel
expects two list arguments, and pops a value from the stack. The first list is executed if the popped value was non-nil, the second list otherwise. Before one of these list pointers is made the new
C
, a pointer to the instruction following
is saved on the dump.
*
join
pops a list reference from the dump and makes this the new value of
C
. This instruction occurs at the end of both alternatives of a
sel
.
*
ldf
takes one list argument representing a function. It constructs a closure (a pair containing the function and the current environment) and pushes that onto the stack.
*
ap
pops a closure and a list of parameter values from the stack. The closure is applied to the parameters by installing its environment as the current one, pushing the parameter list in front of that, clearing the stack, and setting
C
to the closure's function pointer. The previous values of
S
,
E
, and the next value of
C
are saved on the dump.
*
ret
pops one return value from the stack, restores
S
,
E
, and
C
from the dump, and pushes the return value onto the now-current stack.
*
dum
pushes a "dummy", an empty list, in front of the environment list.
*
rap
works like
, only that it replaces an occurrence of a dummy environment with the current one, thus making recursive functions possible
A number of additional instructions for basic functions like car, cdr, list construction, integer addition, I/O, etc. exist. They all take any necessary parameters from the stack.
See also
*
CEK Machine
*
Krivine machine
References
Further reading
*
Danvy, Olivier''A Rational Deconstruction of Landin's SECD Machine'' BRICS research report RS-04-30, 2004. ISSN 0909-0878
* Field, Anthony J. Field and Peter G. Harrison. 1988 ''Functional Programming''. Addison-Wesley.
* Graham, Brian T. 1992 "The SECD Microprocessor: A Verification Case Study". Springer.
* Kogge, Peter M. ''The Architecture of Symbolic Computers''.
*
External links
SECD Mania full collection
{{DEFAULTSORT:Secd Machine
1964 in computing
Implementation of functional programming languages
Models of computation
Abstract machines