urm


Nameurm JSON
Version 1.1.0 PyPI version JSON
download
home_pagehttps://github.com/tunmx/URMSimulator
SummaryUnlimited Register Machine (URM) Simulator
upload_time2024-09-29 03:02:36
maintainerNone
docs_urlNone
authorJingyu Yan
requires_python>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            URM Simulator is a Python library for simulating the operations of an Unlimited Register Machine (URM), a simplified model in theoretical computer science that simulates computation processes through a series of basic operations. This library provides a way to define URM programs, execute these programs, and observe changes in register values.

## Main Features

- **Support for the Four Basic URM Operations**: Zero, Successor, Copy, and Jump.
- **Program Execution and Tracking**: Offers a simulator for executing URM programs and tracking their execution process.
- **Custom URM Programs**: Allows users to define and execute custom URM programs and check the results of the execution.
- **Support Visual**: Add the GUI functionality to support visual representation of each step in URM instruction execution. This feature will facilitate users' learning and analysis of the instruction execution process.

## Advanced Features

- **size Function**: Calculates the number of instructions in a URM program. This is helpful for analyzing program complexity and debugging.
- **haddr Function**: Finds the highest register address used in a URM program. This is crucial for determining how many registers a program needs.
- **normalize Method**: Normalizes a URM program to ensure all jump operations point to valid instruction lines. This helps optimize program structure and prevent errors.
- **concat Method**: Concatenates two URM programs into a single program. This is useful for building complex logic or reusing existing code.
- **reloc Method**: Relocates register addresses in a URM program according to a specified mapping. This is useful when adjusting a program to fit a specific register configuration.

## Installation

Install URM Simulator using pip:

```bash
pip install urm
```

Make sure your Python environment is version 3.7 or higher.

## Usage Examples for GUI

You can directly use the GUI functionality for a more convenient and intuitive way to build URM programs and visualize their execution process. You only need to execute the following command in the command line:

```python
# Run the GUI program
urm gui
```

The program will use port 8975. If there's a conflict, you can modify the port number yourself.

```python
# Modify the port
urm gui --port 8975
```

After successfully launching the GUI program, you can open the URM program panel by accessing http://localhost:8975/index through your web browser.

![creator page](images/page-1.png)

![steps page](images/page-2.png)


## Usage Examples for Python Code

Below is an example of how to define and execute a simple URM program using the URM Simulator library:

### add(x, y) Function

Defines a URM program to calculate the sum of two numbers using the URM model.

```python
import urm
from urm import C, J, Z, S

# Define the sequence of instructions for the addition operation.
add_instruct = urm.Instructions(
    C(2, 0),
    Z(2),
    J(1, 2, 0),
    S(0),
    S(2),
    J(3, 3, 3),
)

# Define the function to perform addition using the URM simulator.
def add(x, y, safety_count=1000):
    num_of_registers = urm.haddr(add_instruct) + 1
    registers = urm.allocate(num_of_registers)
    input_nodes = {1: x, 2: y}
    result = urm.forward(input_nodes, registers, add_instruct, safety_count=safety_count)
    return result.last_registers[0]

if __name__ == '__main__':
    x = 5
    y = 7
    z = add(x, y)
    print(f'add({x}, {y}) = {z}')
```

**Output:**

```bash
add(5, 7) = 12
```


### predecessor(x) function
Define a URM program to calculate the predecessor of a given number.
```bash
import urm
from urm import C, J, Z, S

# Define a URM program to calculate the predecessor of a given number.
# The 'pred_instruct' represents a series of URM instructions for this purpose.
pred_instruct = urm.Instructions(
    J(0, 1, 0),
    S(2, ),
    J(1, 2, 0),
    S(0),
    S(2),
    J(0, 0, 3)
)


# Define the 'pred' function to calculate the predecessor of a number using the URM model.
def pred(x, safety_count=10000):
    # Determine the number of registers needed based on the highest address used in the instructions.
    num_of_registers = urm.haddr(pred_instruct) + 1
    # Allocate the registers, initializing them with zeros.
    registers = urm.allocate(num_of_registers)
    # Setup the initial values for the registers based on the input.
    input_nodes = {1: x}
    # Execute the URM program with the given instructions and input, then obtain the result.
    result = urm.forward(input_nodes, registers, pred_instruct, safety_count=safety_count)
    # Return the value in the result register (R0).
    return result.last_registers[0]


if __name__ == '__main__':
    for _ in range(5):
        x = random.randint(0, 10)
        y = pred(x)
        print(f'predecessor({x}) = {y}')
```
**Output:**
```bash
predecessor(9) = 8
predecessor(8) = 7
predecessor(4) = 3
predecessor(0) = 0
predecessor(3) = 2
```


### subtraction(x,y) function
Define the sequence of instructions for the subtraction operation.
```bash
import random

import urm
from urm import C, J, Z, S

# Define the sequence of instructions for the subtraction operation.
sub_instruct = urm.Instructions(
    C(1, 4),
    J(3, 2, 14),
    J(5, 4, 10),
    S(5),
    J(5, 4, 9),
    S(5),
    S(0),
    J(0, 0, 5),
    Z(5),
    S(3),
    C(0, 4),
    Z(0),
    J(0, 0, 2),
    C(4, 0),
)


# Define the 'sub' function to perform subtraction using the URM simulator.
def sub(x, y, safety_count=100000):
    # Determine the number of registers needed based on the highest address used in the instructions.
    num_of_registers = urm.haddr(sub_instruct) + 1
    # Allocate the registers, initializing them with zeros.
    registers = urm.allocate(num_of_registers)
    # Setup the initial values for the registers based on the inputs.
    input_nodes = {1: x, 2: y}
    # Execute the URM program with the given instructions and input, then obtain the result.
    result = urm.forward(input_nodes, registers, sub_instruct, safety_count=safety_count)
    # Return the value in the result register (R0).
    return result.last_registers[0]


# Main execution block to test the subtraction function.
if __name__ == '__main__':
    for _ in range(5):
        x, y = random.randint(0, 20), random.randint(0, 20)
        z = sub(x, y)
        print(f'subtraction({x}, {y}) = {z}')

```
**Output:**
```bash
subtraction(5, 14) = 0
subtraction(6, 9) = 0
subtraction(19, 13) = 6
subtraction(14, 1) = 13
subtraction(10, 3) = 7
```


### greater_than(x,y) function
Defines a URM program to determine if x is greater than y.
```bash
import random

import urm
from urm import C, J, Z, S

# Defines a URM program to determine if x is greater than y.
gt_instruct = urm.Instructions(
    Z(0),

    # Check if either x (R1) or y (R2) is 0 first. If so, proceed to the "end game" conditions.
    J(1, 0, 25),
    J(2, 0, 24),

    # Compute the predecessor of R1 (x), simulating x - 1.
    Z(3),
    J(3, 1, 25),
    S(4),
    J(1, 4, 11),
    S(3),
    S(4),
    J(0, 0, 7),
    C(3, 1),
    Z(4),

    # Compute the predecessor of R2 (y), simulating y - 1.
    Z(3),
    J(3, 2, 25),
    S(4),
    J(2, 4, 20),
    S(3),
    S(4),
    J(0, 0, 16),
    C(3, 2),
    Z(4),

    Z(3),  # Reset R3 for safety.
    J(0, 0, 2),  # Jump back to the start to check conditions again.

    S(0),  # Set R0 to 1 indicating x > y.
)


# The gt function uses the URM model to determine if x > y.
def gt(x, y, safety_count=10000):
    # Determine the number of registers needed.
    num_of_registers = urm.haddr(gt_instruct) + 1
    # Allocate registers with initial values set to 0.
    registers = urm.allocate(num_of_registers)
    # Set initial values for x and y.
    input_nodes = {1: x, 2: y}
    # Execute the URM instructions and return the result stored in R0.
    result = urm.forward(input_nodes, registers, gt_instruct, safety_count=safety_count)
    return result.last_registers[0]


if __name__ == '__main__':
    for _ in range(5):
        x, y = random.randint(0, 20), random.randint(0, 20)
        z = gt(x, y)
        print(f'{x} > {y} is {z}')

```
**Output:**
```bash
10 > 4 is 1
20 > 19 is 1
20 > 1 is 1
14 > 20 is 0
18 > 19 is 0
```


### fibb(x) function
Define the instructions for computing the nth Fibonacci number using the URM model.
```bash
import urm
from urm import C, J, Z, S

# Define the instructions for computing the nth Fibonacci number using the URM model.
fibb_instructions = urm.Instructions(
    J(1, 0, 0),  # If R1 (input n) is 0, end the program (fib(0) = 0, already in R0).
    S(0),  # Set R0 to 1 (to handle the case of fib(1)).
    J(1, 0, 0),  # If R1 is 1, end the program (fib(1) = 1).
    S(2),  # Initialize R2 (counter k) to 1.

    J(1, 2, 0),  # Loop condition: if R1 equals R2, end the program.

    S(2),  # Increment R2 (k).
    C(0, 4),  # Copy R0 (current Fibonacci number) to R4.
    Z(0),  # Zero R0 for the next calculation.
    Z(5),  # Zero R5, used as a counter in the loop.

    C(4, 0),  # Copy R4 (fib(k-1)) back to R0.
    J(5, 3, 15),  # Loop: if R5 equals R3, jump to instruction 15.
    S(0),  # Increment R0.
    S(5),  # Increment R5.
    J(1, 1, 11),  # Jump back to instruction 11, continue the loop.
    C(4, 3),  # Copy fib(k-1) to fib(k-2) for the next iteration.

    J(2, 2, 5),  # Jump back to instruction 5, continue until R1 equals R2.
)


# Define the function to compute the nth Fibonacci number using the URM instructions.
def fibb(x, safety_count=10000):
    # Determine the number of registers based on the highest address used.
    num_of_registers = urm.haddr(fibb_instructions) + 1
    # Allocate registers with initial values set to 0.
    registers = urm.allocate(num_of_registers)
    # Set the input value (n) in R1.
    input_nodes = {1: x}
    # Execute the URM program and return the result stored in R0 (the nth Fibonacci number).
    result = urm.forward(input_nodes, registers, fibb_instructions, safety_count=safety_count)
    return result.last_registers[0]


if __name__ == '__main__':
    for n in range(11):
        y = fibb(n)
        print(f'fibb({n}) = {y}')

```
### Tracking Execution Process - predecessor(x)
To track the execution process of your designed URM program, showing each step's instructions and register states, refer to the `predecessor(x)` example for debugging your instructions.
```bash
import random

import urm
from urm import C, J, Z, S

# Define a URM program to calculate the predecessor of a given number.
# The 'pred_instruct' represents a series of URM instructions for this purpose.
pred_instruct = urm.Instructions(
    J(0, 1, 0),
    S(2, ),
    J(1, 2, 0),
    S(0),
    S(2),
    J(0, 0, 3)
)


def pred(x, safety_count=10000):
    num_of_registers = urm.haddr(pred_instruct) + 1
    registers = urm.allocate(num_of_registers)
    input_nodes = {1: x}
    result = urm.forward(input_nodes, registers, pred_instruct, safety_count=safety_count)

    # Extract detailed execution information from the result.
    num_steps = result.num_of_steps  # The total number of steps (operations) performed.
    registers_from_steps = result.registers_from_steps  # The state of the registers after each step.
    ops_from_steps = result.ops_from_steps  # The description of each operation performed.

    # Iterate through each step of the computation.
    for idx in range(num_steps):
        # Print the operation performed in the current step.
        print(f'>{ops_from_steps[idx]}')
        # Print a summary of the register states after the current operation.
        print(registers_from_steps[idx].summary())

    return result.last_registers[0]


if __name__ == '__main__':
    x = 5
    y = pred(x)
    print(f'predecessor({x}) = {y}')

```
**Output:**
```bash
>Initial
R0      R1      R2
-----------------------
0       5       0
>1: J(0, 1, 0)
R0      R1      R2
-----------------------
0       5       0
>2: S(2)
R0      R1      R2
-----------------------
0       5       1
>3: J(1, 2, 0)
R0      R1      R2
-----------------------
0       5       1
>4: S(0)
R0      R1      R2
-----------------------
1       5       1
>5: S(2)
R0      R1      R2
-----------------------
1       5       2
>2: J(0, 0, 3)
R0      R1      R2
-----------------------
1       5       2
>3: J(1, 2, 0)
R0      R1      R2
-----------------------
1       5       2
>4: S(0)
R0      R1      R2
-----------------------
2       5       2
>5: S(2)
R0      R1      R2
-----------------------
2       5       3
>2: J(0, 0, 3)
R0      R1      R2
-----------------------
2       5       3
>3: J(1, 2, 0)
R0      R1      R2
-----------------------
2       5       3
>4: S(0)
R0      R1      R2
-----------------------
3       5       3
>5: S(2)
R0      R1      R2
-----------------------
3       5       4
>2: J(0, 0, 3)
R0      R1      R2
-----------------------
3       5       4
>3: J(1, 2, 0)
R0      R1      R2
-----------------------
3       5       4
>4: S(0)
R0      R1      R2
-----------------------
4       5       4
>5: S(2)
R0      R1      R2
-----------------------
4       5       5
>2: J(0, 0, 3)
R0      R1      R2
-----------------------
4       5       5


predecessor(5) = 4
```
## Author

- Jingyu Yan ([tunmxy@163.com](mailto:tunmxy@163.com))


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tunmx/URMSimulator",
    "name": "urm",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": "Jingyu Yan",
    "author_email": "tunmxy@163.com",
    "download_url": "https://files.pythonhosted.org/packages/c3/46/e3d7ae9f6a3f194e94f7f6d9e9ee6b830b694fc793a5e4685b610f71f71d/urm-1.1.0.tar.gz",
    "platform": null,
    "description": "URM Simulator is a Python library for simulating the operations of an Unlimited Register Machine (URM), a simplified model in theoretical computer science that simulates computation processes through a series of basic operations. This library provides a way to define URM programs, execute these programs, and observe changes in register values.\n\n## Main Features\n\n- **Support for the Four Basic URM Operations**: Zero, Successor, Copy, and Jump.\n- **Program Execution and Tracking**: Offers a simulator for executing URM programs and tracking their execution process.\n- **Custom URM Programs**: Allows users to define and execute custom URM programs and check the results of the execution.\n- **Support Visual**: Add the GUI functionality to support visual representation of each step in URM instruction execution. This feature will facilitate users' learning and analysis of the instruction execution process.\n\n## Advanced Features\n\n- **size Function**: Calculates the number of instructions in a URM program. This is helpful for analyzing program complexity and debugging.\n- **haddr Function**: Finds the highest register address used in a URM program. This is crucial for determining how many registers a program needs.\n- **normalize Method**: Normalizes a URM program to ensure all jump operations point to valid instruction lines. This helps optimize program structure and prevent errors.\n- **concat Method**: Concatenates two URM programs into a single program. This is useful for building complex logic or reusing existing code.\n- **reloc Method**: Relocates register addresses in a URM program according to a specified mapping. This is useful when adjusting a program to fit a specific register configuration.\n\n## Installation\n\nInstall URM Simulator using pip:\n\n```bash\npip install urm\n```\n\nMake sure your Python environment is version 3.7 or higher.\n\n## Usage Examples for GUI\n\nYou can directly use the GUI functionality for a more convenient and intuitive way to build URM programs and visualize their execution process. You only need to execute the following command in the command line:\n\n```python\n# Run the GUI program\nurm gui\n```\n\nThe program will use port 8975. If there's a conflict, you can modify the port number yourself.\n\n```python\n# Modify the port\nurm gui --port 8975\n```\n\nAfter successfully launching the GUI program, you can open the URM program panel by accessing http://localhost:8975/index through your web browser.\n\n![creator page](images/page-1.png)\n\n![steps page](images/page-2.png)\n\n\n## Usage Examples for Python Code\n\nBelow is an example of how to define and execute a simple URM program using the URM Simulator library:\n\n### add(x, y) Function\n\nDefines a URM program to calculate the sum of two numbers using the URM model.\n\n```python\nimport urm\nfrom urm import C, J, Z, S\n\n# Define the sequence of instructions for the addition operation.\nadd_instruct = urm.Instructions(\n    C(2, 0),\n    Z(2),\n    J(1, 2, 0),\n    S(0),\n    S(2),\n    J(3, 3, 3),\n)\n\n# Define the function to perform addition using the URM simulator.\ndef add(x, y, safety_count=1000):\n    num_of_registers = urm.haddr(add_instruct) + 1\n    registers = urm.allocate(num_of_registers)\n    input_nodes = {1: x, 2: y}\n    result = urm.forward(input_nodes, registers, add_instruct, safety_count=safety_count)\n    return result.last_registers[0]\n\nif __name__ == '__main__':\n    x = 5\n    y = 7\n    z = add(x, y)\n    print(f'add({x}, {y}) = {z}')\n```\n\n**Output:**\n\n```bash\nadd(5, 7) = 12\n```\n\n\n### predecessor(x) function\nDefine a URM program to calculate the predecessor of a given number.\n```bash\nimport urm\nfrom urm import C, J, Z, S\n\n# Define a URM program to calculate the predecessor of a given number.\n# The 'pred_instruct' represents a series of URM instructions for this purpose.\npred_instruct = urm.Instructions(\n    J(0, 1, 0),\n    S(2, ),\n    J(1, 2, 0),\n    S(0),\n    S(2),\n    J(0, 0, 3)\n)\n\n\n# Define the 'pred' function to calculate the predecessor of a number using the URM model.\ndef pred(x, safety_count=10000):\n    # Determine the number of registers needed based on the highest address used in the instructions.\n    num_of_registers = urm.haddr(pred_instruct) + 1\n    # Allocate the registers, initializing them with zeros.\n    registers = urm.allocate(num_of_registers)\n    # Setup the initial values for the registers based on the input.\n    input_nodes = {1: x}\n    # Execute the URM program with the given instructions and input, then obtain the result.\n    result = urm.forward(input_nodes, registers, pred_instruct, safety_count=safety_count)\n    # Return the value in the result register (R0).\n    return result.last_registers[0]\n\n\nif __name__ == '__main__':\n    for _ in range(5):\n        x = random.randint(0, 10)\n        y = pred(x)\n        print(f'predecessor({x}) = {y}')\n```\n**Output:**\n```bash\npredecessor(9) = 8\npredecessor(8) = 7\npredecessor(4) = 3\npredecessor(0) = 0\npredecessor(3) = 2\n```\n\n\n### subtraction(x,y) function\nDefine the sequence of instructions for the subtraction operation.\n```bash\nimport random\n\nimport urm\nfrom urm import C, J, Z, S\n\n# Define the sequence of instructions for the subtraction operation.\nsub_instruct = urm.Instructions(\n    C(1, 4),\n    J(3, 2, 14),\n    J(5, 4, 10),\n    S(5),\n    J(5, 4, 9),\n    S(5),\n    S(0),\n    J(0, 0, 5),\n    Z(5),\n    S(3),\n    C(0, 4),\n    Z(0),\n    J(0, 0, 2),\n    C(4, 0),\n)\n\n\n# Define the 'sub' function to perform subtraction using the URM simulator.\ndef sub(x, y, safety_count=100000):\n    # Determine the number of registers needed based on the highest address used in the instructions.\n    num_of_registers = urm.haddr(sub_instruct) + 1\n    # Allocate the registers, initializing them with zeros.\n    registers = urm.allocate(num_of_registers)\n    # Setup the initial values for the registers based on the inputs.\n    input_nodes = {1: x, 2: y}\n    # Execute the URM program with the given instructions and input, then obtain the result.\n    result = urm.forward(input_nodes, registers, sub_instruct, safety_count=safety_count)\n    # Return the value in the result register (R0).\n    return result.last_registers[0]\n\n\n# Main execution block to test the subtraction function.\nif __name__ == '__main__':\n    for _ in range(5):\n        x, y = random.randint(0, 20), random.randint(0, 20)\n        z = sub(x, y)\n        print(f'subtraction({x}, {y}) = {z}')\n\n```\n**Output:**\n```bash\nsubtraction(5, 14) = 0\nsubtraction(6, 9) = 0\nsubtraction(19, 13) = 6\nsubtraction(14, 1) = 13\nsubtraction(10, 3) = 7\n```\n\n\n### greater_than(x,y) function\nDefines a URM program to determine if x is greater than y.\n```bash\nimport random\n\nimport urm\nfrom urm import C, J, Z, S\n\n# Defines a URM program to determine if x is greater than y.\ngt_instruct = urm.Instructions(\n    Z(0),\n\n    # Check if either x (R1) or y (R2) is 0 first. If so, proceed to the \"end game\" conditions.\n    J(1, 0, 25),\n    J(2, 0, 24),\n\n    # Compute the predecessor of R1 (x), simulating x - 1.\n    Z(3),\n    J(3, 1, 25),\n    S(4),\n    J(1, 4, 11),\n    S(3),\n    S(4),\n    J(0, 0, 7),\n    C(3, 1),\n    Z(4),\n\n    # Compute the predecessor of R2 (y), simulating y - 1.\n    Z(3),\n    J(3, 2, 25),\n    S(4),\n    J(2, 4, 20),\n    S(3),\n    S(4),\n    J(0, 0, 16),\n    C(3, 2),\n    Z(4),\n\n    Z(3),  # Reset R3 for safety.\n    J(0, 0, 2),  # Jump back to the start to check conditions again.\n\n    S(0),  # Set R0 to 1 indicating x > y.\n)\n\n\n# The gt function uses the URM model to determine if x > y.\ndef gt(x, y, safety_count=10000):\n    # Determine the number of registers needed.\n    num_of_registers = urm.haddr(gt_instruct) + 1\n    # Allocate registers with initial values set to 0.\n    registers = urm.allocate(num_of_registers)\n    # Set initial values for x and y.\n    input_nodes = {1: x, 2: y}\n    # Execute the URM instructions and return the result stored in R0.\n    result = urm.forward(input_nodes, registers, gt_instruct, safety_count=safety_count)\n    return result.last_registers[0]\n\n\nif __name__ == '__main__':\n    for _ in range(5):\n        x, y = random.randint(0, 20), random.randint(0, 20)\n        z = gt(x, y)\n        print(f'{x} > {y} is {z}')\n\n```\n**Output:**\n```bash\n10 > 4 is 1\n20 > 19 is 1\n20 > 1 is 1\n14 > 20 is 0\n18 > 19 is 0\n```\n\n\n### fibb(x) function\nDefine the instructions for computing the nth Fibonacci number using the URM model.\n```bash\nimport urm\nfrom urm import C, J, Z, S\n\n# Define the instructions for computing the nth Fibonacci number using the URM model.\nfibb_instructions = urm.Instructions(\n    J(1, 0, 0),  # If R1 (input n) is 0, end the program (fib(0) = 0, already in R0).\n    S(0),  # Set R0 to 1 (to handle the case of fib(1)).\n    J(1, 0, 0),  # If R1 is 1, end the program (fib(1) = 1).\n    S(2),  # Initialize R2 (counter k) to 1.\n\n    J(1, 2, 0),  # Loop condition: if R1 equals R2, end the program.\n\n    S(2),  # Increment R2 (k).\n    C(0, 4),  # Copy R0 (current Fibonacci number) to R4.\n    Z(0),  # Zero R0 for the next calculation.\n    Z(5),  # Zero R5, used as a counter in the loop.\n\n    C(4, 0),  # Copy R4 (fib(k-1)) back to R0.\n    J(5, 3, 15),  # Loop: if R5 equals R3, jump to instruction 15.\n    S(0),  # Increment R0.\n    S(5),  # Increment R5.\n    J(1, 1, 11),  # Jump back to instruction 11, continue the loop.\n    C(4, 3),  # Copy fib(k-1) to fib(k-2) for the next iteration.\n\n    J(2, 2, 5),  # Jump back to instruction 5, continue until R1 equals R2.\n)\n\n\n# Define the function to compute the nth Fibonacci number using the URM instructions.\ndef fibb(x, safety_count=10000):\n    # Determine the number of registers based on the highest address used.\n    num_of_registers = urm.haddr(fibb_instructions) + 1\n    # Allocate registers with initial values set to 0.\n    registers = urm.allocate(num_of_registers)\n    # Set the input value (n) in R1.\n    input_nodes = {1: x}\n    # Execute the URM program and return the result stored in R0 (the nth Fibonacci number).\n    result = urm.forward(input_nodes, registers, fibb_instructions, safety_count=safety_count)\n    return result.last_registers[0]\n\n\nif __name__ == '__main__':\n    for n in range(11):\n        y = fibb(n)\n        print(f'fibb({n}) = {y}')\n\n```\n### Tracking Execution Process - predecessor(x)\nTo track the execution process of your designed URM program, showing each step's instructions and register states, refer to the `predecessor(x)` example for debugging your instructions.\n```bash\nimport random\n\nimport urm\nfrom urm import C, J, Z, S\n\n# Define a URM program to calculate the predecessor of a given number.\n# The 'pred_instruct' represents a series of URM instructions for this purpose.\npred_instruct = urm.Instructions(\n    J(0, 1, 0),\n    S(2, ),\n    J(1, 2, 0),\n    S(0),\n    S(2),\n    J(0, 0, 3)\n)\n\n\ndef pred(x, safety_count=10000):\n    num_of_registers = urm.haddr(pred_instruct) + 1\n    registers = urm.allocate(num_of_registers)\n    input_nodes = {1: x}\n    result = urm.forward(input_nodes, registers, pred_instruct, safety_count=safety_count)\n\n    # Extract detailed execution information from the result.\n    num_steps = result.num_of_steps  # The total number of steps (operations) performed.\n    registers_from_steps = result.registers_from_steps  # The state of the registers after each step.\n    ops_from_steps = result.ops_from_steps  # The description of each operation performed.\n\n    # Iterate through each step of the computation.\n    for idx in range(num_steps):\n        # Print the operation performed in the current step.\n        print(f'>{ops_from_steps[idx]}')\n        # Print a summary of the register states after the current operation.\n        print(registers_from_steps[idx].summary())\n\n    return result.last_registers[0]\n\n\nif __name__ == '__main__':\n    x = 5\n    y = pred(x)\n    print(f'predecessor({x}) = {y}')\n\n```\n**Output:**\n```bash\n>Initial\nR0      R1      R2\n-----------------------\n0       5       0\n>1: J(0, 1, 0)\nR0      R1      R2\n-----------------------\n0       5       0\n>2: S(2)\nR0      R1      R2\n-----------------------\n0       5       1\n>3: J(1, 2, 0)\nR0      R1      R2\n-----------------------\n0       5       1\n>4: S(0)\nR0      R1      R2\n-----------------------\n1       5       1\n>5: S(2)\nR0      R1      R2\n-----------------------\n1       5       2\n>2: J(0, 0, 3)\nR0      R1      R2\n-----------------------\n1       5       2\n>3: J(1, 2, 0)\nR0      R1      R2\n-----------------------\n1       5       2\n>4: S(0)\nR0      R1      R2\n-----------------------\n2       5       2\n>5: S(2)\nR0      R1      R2\n-----------------------\n2       5       3\n>2: J(0, 0, 3)\nR0      R1      R2\n-----------------------\n2       5       3\n>3: J(1, 2, 0)\nR0      R1      R2\n-----------------------\n2       5       3\n>4: S(0)\nR0      R1      R2\n-----------------------\n3       5       3\n>5: S(2)\nR0      R1      R2\n-----------------------\n3       5       4\n>2: J(0, 0, 3)\nR0      R1      R2\n-----------------------\n3       5       4\n>3: J(1, 2, 0)\nR0      R1      R2\n-----------------------\n3       5       4\n>4: S(0)\nR0      R1      R2\n-----------------------\n4       5       4\n>5: S(2)\nR0      R1      R2\n-----------------------\n4       5       5\n>2: J(0, 0, 3)\nR0      R1      R2\n-----------------------\n4       5       5\n\n\npredecessor(5) = 4\n```\n## Author\n\n- Jingyu Yan ([tunmxy@163.com](mailto:tunmxy@163.com))\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Unlimited Register Machine (URM) Simulator",
    "version": "1.1.0",
    "project_urls": {
        "Homepage": "https://github.com/tunmx/URMSimulator"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "88f0f806726f282b4c09d83c812c2ac579496efe0cb926767bd970e4c1cf3231",
                "md5": "52a6e5789b6d4dd1b2760af7835d5f1f",
                "sha256": "98f02bf244dfb143dc4f101e7e8304cdafab7827bbea5cd61431b9a15f133d2b"
            },
            "downloads": -1,
            "filename": "urm-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "52a6e5789b6d4dd1b2760af7835d5f1f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 226657,
            "upload_time": "2024-09-29T03:02:34",
            "upload_time_iso_8601": "2024-09-29T03:02:34.480355Z",
            "url": "https://files.pythonhosted.org/packages/88/f0/f806726f282b4c09d83c812c2ac579496efe0cb926767bd970e4c1cf3231/urm-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c346e3d7ae9f6a3f194e94f7f6d9e9ee6b830b694fc793a5e4685b610f71f71d",
                "md5": "3943b3163566fb877a72feedee56cca8",
                "sha256": "42b2b301ac5e2abe35372f6118bce11fb3d31695ef919783c8a36b4f4af6614a"
            },
            "downloads": -1,
            "filename": "urm-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3943b3163566fb877a72feedee56cca8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 224595,
            "upload_time": "2024-09-29T03:02:36",
            "upload_time_iso_8601": "2024-09-29T03:02:36.638659Z",
            "url": "https://files.pythonhosted.org/packages/c3/46/e3d7ae9f6a3f194e94f7f6d9e9ee6b830b694fc793a5e4685b610f71f71d/urm-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-29 03:02:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tunmx",
    "github_project": "URMSimulator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "urm"
}
        
Elapsed time: 0.74638s