Warren Abstract Machine


In 1983, David H. D. Warren designed an abstract machine for the execution of Prolog consisting of a memory architecture and an instruction set. This design became known as the Warren Abstract Machine and has become the de facto standard target for Prolog compilers.

Purpose

The purpose of compiling Prolog code to the more low-level WAM code is to make subsequent interpretation of the Prolog program more efficient. Prolog code is reasonably easy to translate to WAM instructions, which can be more efficiently interpreted. Also, subsequent code improvements and compilations to native code are often easier to perform on the more low-level representation.
In order to write efficient Prolog programs, a basic understanding of how the WAM works can be advantageous. Some of the most important WAM concepts are first argument indexing and its relation to choice-points, tail call optimization, and memory reclamation on failure.

Memory areas

The WAM has the following memory areas:
Here is a piece of Prolog code:

girl.
girl.
boy :- \+ girl.

A WAM-based Prolog compiler will compile this into WAM instructions similar to the following:

predicate:
switch_on_term,
label: switch_on_atom,
label: try_me_else
label: get_atom
proceed
label: trust_me_else_fail
label: get_atom
proceed
predicate:
get_variable
put_structure
unify_local_value
execute])

An important characteristic of this code is its ability to cope with the various modes in which the predicates can be evoked: any argument might be a variable, a ground term, or a partly instantiated term. The "switch" instructions handle the different cases.