G-Machine State Transition Rules¶
Core Transition Rules¶
Lookup a global by name and push its value onto the stack
Allocate an int node on the heap, and push the address of the newly created node onto the stack
Allocate an application node on the heap, applying the top of the stack to the address directly below it. The address of the application node is pushed onto the stack.
Push a function’s argument onto the stack
Tidy up the stack after instantiating a supercombinator
If the top of the stack is in WHNF (currently this just means a number) is on top of the stack,
Unwindconsiders evaluation complete. In the case where the dump is not empty, the instruction queue and stack is restored from the top.Bulding on the previous rule, in the case where the dump is empty, leave the machine in a halt state (i.e. with an empty instruction queue).
Again, building on the previous rules, this rule makes the machine consider unapplied supercombinators to be in WHNF
If an application is on top of the stack,
Unwindcontinues unwindingWhen a supercombinator is on top of the stack (and the correct number of arguments have been provided),
Unwindsets up the stack and jumps to the supercombinator’s code (-reduction)
Pop the stack, and update the nth node to point to the popped address
Pop the stack.
Follow indirections while unwinding
Allocate uninitialised heap space
Evaluate the top of the stack to WHNF
Reduce a primitive binary operator
.
Reduce a primitive unary operator
.
Pack a constructor if there are sufficient arguments
Evaluate a case
Extension Rules¶
A sneaky trick to enable sharing of
NNumnodes. We note that the global environment is a mapping of plain old strings to heap addresses. Strings of digits are not considered valid identifiers, so putting them on the global environment will never conflict with a supercombinator! We abuse this by modifying Core Rule 2 to update the global environment with the new node’s address. Consider how this rule might impact garbage collection (remember that the environment is intended for globals).In order for the previous rule to be effective, we are also required to take action when a number already exists in the environment: