STReAM: Suspendable Term Reduction Abstract Machine (formal semantics)
- STReAM is Suspendable Term Reduction Abstract Machine designed for
strict functional programming languages. - Phox VM is an implementation of STReAM designed for
the Phox programming language. - Phox is a strict functional programming language.
Machine State
STReAM decomposes its state into three semantic scopes:
- lexical scope : the current term and its lexical environment
- contextual scope : continuation code and continuation values
- global scope : global code table and heap
Formal State
VM state = (
term,ctx.conts,ctx.env,g.codes,g.heap)
State {
// lexical scope
term : enum Term {
Val(Value), // a value, or
Clo(Closure { // a closure
code: Code, // - code of the closure
env: Env, // - variables bounded to the closure
}),
},
// contextual scope
ctx : Context {
conts: CStack, // continuation closure-stack
env: Env, // continuation value-stack
},
// global scope
g : {
codes: GlobalEnv, // global code table
heap: Heap, // global store
},
}
Note
termandctxare specific to each VM instance, butgis sharable among multiple VM instances.In other words,
- By their very nature, one VM instance can represent one suspendable tasks or jobs, and
- Multiple VM instances (i.e., multiple tasks/jobs) can run in concurrent, if the global allocator
g.heapis multithread-safe,Consequently, the STReAM/Phox VM can naturally support asynchronous and concurrent computation at the abstract machine and runtime system levels.
Notations of VM state
-
term- Term. A Term is Closure or Value.
tmeans an arbitrary Closure or Value.<val>means an arbitrary Value.-
val- Value
- …
-
{code, env}means a Closure. Closure is pair of Codecodeand Envenv.
-
ctx.conts- Continuation closure-stack (CStack)
[]means empty CStackksmeans an arbitrary CStack.k::ksmeans a CStack whose top isk, wherekis a Closure.
-
ctx.env- continuation value-stack (WStack ≡ Env)
[]means empty WStackwsmeans an arbitrary WStack.a::wsmeans a WStack whose top isa, whereais an address of heap
-
g.codes- Random access read-only code table. (GlobalEnv)
gsmeans an arbitrary GloalEnv.gs[s ↦ c]means GloalEnvgswhose element at keysis codec
-
g.heap- Random access heap memory (Heap)
hmeans an arbitrary Heap.h[a ↦ t]means Heaphwhose element at addressais termth[a ↦ {}]means heaphwhose element at addressais nil
(i.e.ais fresh address to be allocated later)
Note
h[a ↦ {}]does not allocate memory.
It only denotes thatais a fresh address.
Actual allocation occurs when a value is written toa.
Dynamics of VM state transition
- WHNF (end of state transition)
- WHNF w/ continuation
- CSEQ (Continuation Sequencer)
- ACCESS (variable lookup)
- APP (function application)
- LET (let binding)
- LETREC (recursive binding)
WHNF (end of state transition)
Evaluation halts when the current term was {Lam E, es} or <val> and there is no continuations.
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| (Done) | {Lam E, es} | [] | [] | gs | h |
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| (Done) | <val> | [] | [] | gs | h |
WHNF w/ continuation
Save the current term to the heap, push its address to ctx.env, and load the next continuation.
- Allocate fresh address
aof heap for the current term, - Push
atoctx.env, - Pop continuation from
ctx.conts.
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| cont | {Lam E, es} | k::ks | ws | gs | h[a ↦ {}] |
| → | k | ks | a::ws | gs | h[a ↦ {Lam E, es}] |
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| cont | <val> | k::ks | ws | gs | h[a ↦ {}] |
| → | k | ks | a::ws | gs | h[a ↦ <val>] |
CSEQ (Continuation Sequencer)
-
CSeq M N- Evaluate
M, and thenN.
SinceNis evaluated afterM,Nis pushed onto the continuation stack as a closure, and
the current code is replaced withM.
-
Push continuation code
N(as closure{N, es}) toctx.conts, -
Replace the current code with
M.
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| cseq | {CSeq M N, es} | ks | ws | gs | h |
| → | {M, es} | {N, es}::ks | ws | gs | h |
ACCESS (variable lookup)
-
Var n- Load term of a variable bounded the current env.
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| access | {Var n, es[n ↦ a]} | ks | ws | gs | h[a ↦ t] |
| → | t | ks | ws | gs | h[a ↦ t] |
If de Bruijn index n was out of bounds, causes run-time error “variable not found”.
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| (Error) | {Var n, es[n ↦ {}]} | ks | ws | gs | h |
APP (function application)
-
App M N- Evaluate
MandNin order, then apply the resulting function to the argument viaKApp.
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| app | {App M N, es} | ks | ws | gs | h |
| → | {N, es} | {M, es}::{KApp, []}::ks | ws | gs | h |
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| kapp | {KApp, []} | ks | f::x::ws | gs | h[f ↦ {Lam E, es}, x ↦ tN] |
| → | {E, x::es} | ks | ws | gs | h[x ↦ tN] |
where:
{Lam E, es}= resulting term (WHNF) of{M, es}viaconttransitiontN= resulting term (WHNF) of{N, es}viaconttransition
LET (let binding)
The code Let X E is synonym of App (Lam E) X.
LETREC (recursive binding)
-
LetRec X E- Allocate a dummy for recursive binding, evaluate
X, then update the dummy with the result and evaluateE.
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| letrec | {LetRec X E, es} | ks | ws | gs | h[f ↦ {}] |
| → | {X, f::es} | {KLetRec E, f::es}::ks | ws | gs | h[f ↦ dummy] |
| (rule) | term | ctx.conts | ctx.env | g.codes | g.heap |
|---|---|---|---|---|---|
| kletrec | {KLetRec E, f::es} | ks | x::ws | gs | h[x ↦ tX, f ↦ dummy] |
| → | {E, f::es} | ks | ws | gs | h[f ↦ tX] |
where:
dummy= an arabitrary allocated dummy term.f= an address that- holds
dummyat first vialetrectransition, and then - be updated with term at
xlater viakletrectransition. - finally
fholdstX(the recursive function body).
- holds
tX= resulting term (WHNF) of{X, f::es}viaconttransition