from dummy import Hyper, Param, Var, Runtime, Input, Output, Inline maxScalar = Hyper() numRegisters = Hyper() programLen = Hyper() numTimesteps = Hyper() inputNum = Hyper() inputStackSize = Hyper() numRegInstrs = 13 numBranchInstrs = 2 numInstrTypes = 3 numInstructions = numBranchInstrs + numRegInstrs + 1 mutableStackSize = maxScalar - inputStackSize - 1 # Inputs inputRegVal = Input(maxScalar)[inputNum] inputStackCarVal = Input(maxScalar)[inputStackSize] inputStackCdrVal = Input(maxScalar)[inputStackSize] # Outputs outputTermState = Output(2) outputRegVal = Output(maxScalar) outputListVal = Output(maxScalar)[maxScalar] # Runtime state stackCarValue = Var(maxScalar)[numTimesteps + 1, mutableStackSize] stackCdrValue = Var(maxScalar)[numTimesteps + 1, mutableStackSize] stackPtr = Var(mutableStackSize)[numTimesteps + 1]
from dummy import Hyper, Param, Var, Runtime, Input, Output, Inline #### Parameters to the model (changes in this block should not require #### any changes in the actual model) maxInt = Hyper() inputNum = Hyper() inputStackSize = Hyper() prefixLength = Hyper() lambdaLength = Hyper() suffixLength = Hyper() #### Inputs: # We first need to work out the size of the stack. # The combinator can run at most for the number of input elements + what # was allocated in the prefix: maxLoopsteps = inputStackSize + prefixLength # The number of stack cells is dependent on the number of instructions # and inputs as follows: # - 1 for Nil # - inputStackSize # - prefixLength (as each instruction can allocate) # - maxLoopsteps (as we create a list element for each map/zipWith iteration) # - maxLoopsteps * lambdaLength (as each lambda instruction can allocate) # - suffixLength (as each instruction can allocate) stackPtrAtPrefixStart = 1 + inputStackSize stackPtrAtCombinatorStart = stackPtrAtPrefixStart + prefixLength if lambdaLength > 0: stackPtrAtSuffixStart = stackPtrAtCombinatorStart + maxLoopsteps * ( 1 + lambdaLength) else: stackPtrAtSuffixStart = stackPtrAtCombinatorStart
from dummy import Hyper, Param, Var, Runtime, Input, Output, Inline numBlocks = Hyper() numRegisters = Hyper() numTimesteps = Hyper() maxInt = Hyper() # Inputs and Outputs boolSize = 2 initial_memory = Input(maxInt)[maxInt] final_is_halted = Output(boolSize) final_memory = Output(maxInt)[maxInt] # Interpreter instruction details / implementations numInstructions = 16 @Runtime([], maxInt) def Zero(): return 0 @Runtime([maxInt], maxInt) def Inc(a): return (a + 1) % maxInt @Runtime([maxInt, maxInt], maxInt) def Add(a, b): return (a + b) % maxInt
from dummy import Hyper, Param, Var, Runtime, Input, Output, Inline #### Parameters to the model (changes in this block should not require #### any changes in the actual model) inputNum = Hyper() inputStackSize = Hyper() prefixLength = Hyper() loopBodyLength = Hyper() suffixLength = Hyper() extraRegisterNum = Hyper() #### Inputs: # We first need to work out the size of the stack. # The combinator can run at most for the number of input elements + what # was allocated in the prefix: maxLoopsteps = inputStackSize + prefixLength # The number of stack cells is dependent on the number of instructions # and inputs as follows: # - 1 for Nil # - inputStackSize # - prefixLength (as each instruction can allocate) # - maxLoopsteps * lambdaLength (as each lambda instruction can allocate) # - suffixLength (as each instruction can allocate) stackPtrAtPrefixStart = 1 + inputStackSize stackPtrAtLoopStart = stackPtrAtPrefixStart + prefixLength numTimesteps = 1 + prefixLength + (loopBodyLength * maxLoopsteps) + suffixLength stackSize = stackPtrAtLoopStart + maxLoopsteps * loopBodyLength + suffixLength maxScalar = stackSize + 0 registerNum = inputNum + extraRegisterNum
from dummy import Hyper, Param, Var, Runtime, Input, Output, Inline #### Parameters to the model (changes in this block should not require #### any changes in the actual model) inputNum = Hyper() inputStackSize = Hyper() prefixLength = Hyper() lambdaLength = Hyper() suffixLength = Hyper() extraRegisterNum = Hyper() #### Inputs: # We first need to work out the size of the stack. # The combinator can run at most for the number of input elements + what # was allocated in the prefix: maxLoopsteps = inputStackSize + prefixLength # The number of stack cells is dependent on the number of instructions # and inputs as follows: # - 1 for Nil # - inputStackSize # - prefixLength (as each instruction can allocate) # - maxLoopsteps (as we create a list element for each map/zipWith iteration) # - maxLoopsteps * lambdaLength (as each lambda instruction can allocate) # - suffixLength (as each instruction can allocate) stackPtrAtPrefixStart = 1 + inputStackSize stackPtrAtCombinatorStart = stackPtrAtPrefixStart + prefixLength if lambdaLength > 0: stackPtrAtSuffixStart = stackPtrAtCombinatorStart + maxLoopsteps * ( 1 + lambdaLength) numTimesteps = prefixLength + ( (lambdaLength + 1) * maxLoopsteps) + suffixLength + 2
from dummy import Hyper, Param, Var, Runtime, Input, Output, Inline numTapeSymbols = Hyper() numHeadStates = Hyper() tapeLength = Hyper() numTimesteps = Hyper() boolSize = 2 numDirections = 3 # Inputs and Output initial_tape = Input(numTapeSymbols)[tapeLength] final_is_halted = Output(2) final_tape = Output(numTapeSymbols)[tapeLength] # Turing Machine parameters write = Param(numTapeSymbols)[numHeadStates, numTapeSymbols] dir = Param(numDirections)[numHeadStates, numTapeSymbols] newState = Param(numHeadStates)[numHeadStates, numTapeSymbols] @Runtime([tapeLength, numDirections], tapeLength) def move(pos, dir): if dir == 0: return pos elif dir == 1: return (pos + 1) % tapeLength elif dir == 2: return (pos - 1) % tapeLength @Runtime([tapeLength, tapeLength], boolSize)
from dummy import Hyper, Param, Var, Runtime, Input, Output, Inline numGates = Hyper() numWires = Hyper() numOutputs = Hyper() numGateTypes = 5 @Runtime([2, 2], 2) def AND(a, b): return int(a and b) @Runtime([2, 2], 2) def OR(a, b): return int(a or b) @Runtime([2, 2], 2) def XOR(a, b): return int(a ^ b) @Runtime([2], 2) def NOT(a): return int(not a) @Runtime([2], 2) def NOP(a): return a