# G-Machine State Transition Rules

## Core Transition Rules

1. Lookup a global by name and push its value onto the stack

2. Allocate an int node on the heap, and push the address of the newly created node onto the stack

3. 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.

4. Push a function’s argument onto the stack

5. Tidy up the stack after instantiating a supercombinator

6. If a number is on top of the stack, `Unwind` leaves the machine in a halt state

7. If an application is on top of the stack, `Unwind` continues unwinding

8. When a supercombinator is on top of the stack (and the correct number of arguments have been provided), `Unwind` sets up the stack and jumps to the supercombinator’s code (-reduction)

9. Pop the stack, and update the nth node to point to the popped address

10. Pop the stack.

11. Follow indirections while unwinding

12. Allocate uninitialised heap space

13. When unwinding, if the top of the stack is in WHNF, pop the dump

14. Evaluate the top of the stack to WHNF

## Extension Rules

1. A sneaky trick to enable sharing of `NNum` nodes. We note that the global environment is a mapping of `Name` objects (i.e. identifiers) to heap addresses. Strings of digits are not considered valid identifiers! 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).

2. In order for Extension Rule 1. to be effective, we are also required to take action when a number already exists in the environment: