Krivine Machine
Get Krivine Machine essential facts below. View Videos or join the Krivine Machine discussion. Add Krivine Machine to your PopFlock.com topic list for future reference or share this resource on social media.
Krivine Machine
A picture view of a Krivine machine

In theoretical computer science, the Krivine machine is an abstract machine (sometimes called virtual machine). As an abstract machine, it shares features with Turing machines and the SECD machine. The Krivine machine explains how to compute a recursive function. More specifically it aims to define rigorously head normal form reduction of a lambda term using call-by-name reduction. Thanks to its formalism, it tells in details how a kind of reduction works and sets the theoretical foundation of the operational semantics of functional programming languages. On the other hand, Krivine machine implements call-by-name because it evaluates the body of a ?-redex before it applies the body to its parameter. In other words, in an expression (? x. t) u it evaluates first ? x. t before applying it to u. In functional programming, this would mean that in order to evaluate a function applied to a parameter, it evaluates first the function before applying it to the parameter.

The Krivine machine has been designed by the French logician Jean-Louis Krivine at the beginning of the 1980s.

## Call by name and head normal form reduction

The Krivine machine is based on two concepts related to lambda calculus, namely head reduction and call by name.

#### An example

The term ((? 0) (? 0)) (? 0) (which corresponds, if one uses explicit variables, to the term (?x.x) (?y.y) (?z.z)) is not in head normal form because (? 0) (? 0) contracts in (? 0) yielding the head redex (? 0) (? 0) which contracts in (? 0) and which is therefore the head normal form of ((? 0) (? 0)) (? 0). Said otherwise the head normal form contraction is:

((? 0) (? 0)) (? 0) ? (? 0) (? 0) ? ? 0,

which corresponds to :

(?x.x) (?y.y) (?z.z) ? (?y.y) (?z.z) ? ?z.z.

We will see further how the Krivine machine reduces the term ((? 0) (? 0)) (? 0).

### Call by name

To implement the head reduction of a term u v which is an application, but which is not a redex, one must reduce the body u to exhibit an abstraction and therefore create a redex with v. When a redex appears, one reduces it. To reduce always the body of an application first is called call by name.[b] The Krivine machine implements call by name.

## Description

The presentation of the Krivine machine given here is based on notations of lambda terms that use de Bruijn indices and assumes that the terms of which it computes the head normal forms are closed.[2] It modifies the current state until it cannot do it anymore, in which case it obtains a head normal form. This head normal form represents the result of the computation or yields an error, meaning that the term it started from is not correct. However, it can enter an infinite sequence of transitions, which means that the term it attempts reducing has no head normal form and corresponds to a non terminating computation.

It has been proved that the Krivine machine implements correctly the call by name head normal form reduction in the lambda-calculus. Moreover, the Krivine machine is deterministic, since each pattern of the state corresponds to at most one machine transition.

### The state

The state has three components[2]

1. a term,
2. a stack,
3. an environment.

The term is a ?-term with de Bruijn indices. The stack and the environment belong to the same recursive data structure. More precisely, the environment and the stack are lists of pairs <term, environment>, that are called closures. In what follows, the insertion as the head of a list l (stack or environment) of an element a is written a:l, whereas the empty list is written ?. The stack is the location where the machine stores the closures that must be evaluated furthermore, whereas the environment is the association between the indices and the closures at a given time during the evaluation. The first element of the environment is the closure associated with the index 0, the second element corresponds to the closure associated with index 1 etc. If the machine has to evaluate an index, it fetches there the pair <term, environment> the closure that yields the term to be evaluated and the environment in which this term must be evaluated.[c] This intuitive explanations allow understanding the operating rules of the machine. If one writes t for term, p for stack,[d] and e for environment, the states associated with these three entities will be written t, p, e. The rules explain how the machine transforms a state into another state, after identifying the patterns among the states.

The initial state aims to evaluate a term t, it is the state t,?,?, in which the term is t and the stack and the environment are empty. The final state (in absence of error) is of the form ? t, ?, e, in other words, the resulting terms is an abstraction together with its environment and an empty stack.

### The transitions

The Krivine machine[2] has four transitions : App, Abs, Zero, Succ.

Transitions of the Krivine machine
Name Before After

App

t u, p, e

t, <u,e>:p, e

Abs

? t, <u,e'>:p, e

t, p, <u,e'>:e

Zero

0, p, <t, e'>:e

t, p, e'

Succ

n+1, p, <t,e'>:e

n, p, e

The transition App removes the parameter of an application and put it on the stack for further evaluation. The transition Abs removes the ? of the term and pop up the closure from the top of the stack and put it on the top of the environment. This closure corresponds to the de Bruijn index 0 in the new environment. The transition Zero takes the first closure of the environment. The term of this closure becomes the current term and the environment of this closure becomes the current environment. The transition Succ removes the first closure of the environment list and decreases the value of the index.

### Two examples

Let us evaluate the term (? 0 0) (? 0) which corresponds to the term (? x. x x) (? x. x). Let us start with the state (? 0 0) (? 0), ?, ?.

 (? 0 0) (? 0), ?, ? ? 0 0, [], ? 0 0, ?, [] 0, [<0, >], [] ? 0, [<0, >], ? 0, ?, [<0, >] 0, ?, [] ? 0, ?, ?

The conclusion is that the head normal form of the term (? 0 0) (? 0) is ? 0. This translates with variables in: the head normal form of the term (? x. x x) (? x. x) is ? x. x.

Let us evaluate the term ((? 0) (? 0)) (? 0) as shown belown:

 ((? 0) (? 0)) (? 0), ?, ? (? 0) (? 0), [<(? 0), ?>], ? (? 0), [<(? 0), ?>,<(? 0), ?>], ? 0, [<(? 0), ?>], [<(? 0), ?>] ? 0, [<(? 0), ?>], ? 0, ?, [<(? 0), ?>] (? 0), ?, ?

This confirms the above fact that the normal form of the term ((? 0) (? 0)) (? 0) is (? 0).

## Notes

1. ^ If one only deals with closed terms, these terms take the form ? x. t.
2. ^ This ancient terminology refers to concepts of the 60's and hardly justified in 2017.
3. ^ Using the concept of closure, one may replace the triple <term,stack, environment>, which defines the state, by a couple <closure,stack>, but this change is cosmetic.
4. ^ p is for pile, the French word for stack, which we do not want to mix up with s, for state.

## References

1. ^ Barendregt, Hendrik Pieter (1984), The Lambda Calculus: Its Syntax and Semantics, Studies in Logic and the Foundations of Mathematics, 103 (Revised ed.), North Holland, Amsterdam, ISBN 0-444-87508-5, archived from the original on 2004-08-23Corrections.
2. ^ a b c Curien, Pierre-Louis (1993). Categorical Combinators, Sequential Algorithms and Functional (2nd ed.). Birkhaüser.

Content in this edit is translated from the existing French popflock.com resource article at fr:Machine de Krivine; see its history for attribution.

## Bibliography

• Jean-Louis Krivine: A call-by-name lambda-calculus machine. Higher-Order and Symbolic Computation 20(3): 199-207 (2007) archive.
• Curien, Pierre-Louis (1993). Categorical Combinators, Sequential Algorithms and Functional (2nd ed.). Birkhaüser.
• Frédéric Lang: Explaining the lazy Krivine machine using explicit substitution and addresses. Higher-Order and Symbolic Computation 20(3): 257-270 (2007) archive.
• Olivier Danvy (Ed.): Editorial of special issue of Higher-Order and Symbolic Computation on the Krivine machine, vol. 20(3) (2007)