diff --git a/URM_en.ipynb b/URM_en.ipynb
index a979f76..e29060c 100644
--- a/URM_en.ipynb
+++ b/URM_en.ipynb
@@ -23,7 +23,7 @@
"colab_type": "text"
},
"source": [
- "
"
+ "
"
]
},
{
@@ -73,7 +73,7 @@
"base_uri": "https://localhost:8080/"
},
"id": "0uybnLWmIUXM",
- "outputId": "3f49375b-bccc-4466-a2cd-f7912e9b4489"
+ "outputId": "2dae9af2-975f-4c38-ac70-c5ae975a6ebe"
},
"execution_count": 1,
"outputs": [
@@ -451,7 +451,7 @@
"base_uri": "https://localhost:8080/"
},
"id": "J2-oNVEWd959",
- "outputId": "73051929-cfff-4441-f57b-8865cfada4ee"
+ "outputId": "1ca851cd-d33c-4ee0-eca2-30d3be12fa94"
},
"execution_count": 6,
"outputs": [
@@ -579,7 +579,7 @@
"base_uri": "https://localhost:8080/"
},
"id": "5wMcWBoqmnEZ",
- "outputId": "569dbfd1-554e-409b-e996-6510e2252283"
+ "outputId": "bc44b039-9b78-4b9d-fa92-db225a5c8062"
},
"execution_count": 8,
"outputs": [
@@ -670,9 +670,9 @@
"base_uri": "https://localhost:8080/"
},
"id": "ca3jyBTE7Nu9",
- "outputId": "318d3dda-98cb-4311-bfb7-78f196e6b156"
+ "outputId": "bc7ba15d-c2f0-464e-ceb9-33d6fbb52596"
},
- "execution_count": 12,
+ "execution_count": 10,
"outputs": [
{
"output_type": "stream",
@@ -703,7 +703,7 @@
{
"cell_type": "code",
"source": [
- "def run(prgm: program, *data: Tuple[Any]) -> nat:\n",
+ "def run(prgm: program, *data: Tuple[Any], verbose=False) -> nat:\n",
" if type(prgm) != program:\n",
" raise ValueError(\"run() error! Bad program\")\n",
" try:\n",
@@ -719,12 +719,14 @@
" if i is None:\n",
" return m.read(0)\n",
" q, m = apply(i, to=m)\n",
- " ic = ic + 1 if q is None else q"
+ " ic = ic + 1 if q is None else q\n",
+ " if verbose:\n",
+ " print(m, i)"
],
"metadata": {
"id": "vNtaMaamB8Hu"
},
- "execution_count": 13,
+ "execution_count": 26,
"outputs": []
},
{
@@ -743,9 +745,9 @@
"base_uri": "https://localhost:8080/"
},
"id": "hVheckP1FJQX",
- "outputId": "6b427e9e-3457-49e6-aa18-9e336dd490da"
+ "outputId": "10ac2934-aba0-494c-832e-c8d6ec82bea4"
},
- "execution_count": 14,
+ "execution_count": 12,
"outputs": [
{
"output_type": "stream",
@@ -828,7 +830,7 @@
"metadata": {
"id": "ICB0f78XXRkY"
},
- "execution_count": 15,
+ "execution_count": 13,
"outputs": []
},
{
@@ -850,7 +852,7 @@
},
"outputId": "6c624feb-59f9-4f16-9bbe-de97d9374c1b"
},
- "execution_count": 20,
+ "execution_count": null,
"outputs": [
{
"output_type": "stream",
@@ -920,7 +922,7 @@
"metadata": {
"id": "d1h7TKbA8AfB"
},
- "execution_count": 22,
+ "execution_count": 14,
"outputs": []
},
{
@@ -946,9 +948,9 @@
"base_uri": "https://localhost:8080/"
},
"id": "b8pVbesRXikf",
- "outputId": "5aa6daf9-52a6-43e6-9bf7-20ab9aa7c206"
+ "outputId": "4128a443-cb89-4984-f75a-8c78ae12eacf"
},
- "execution_count": 23,
+ "execution_count": 15,
"outputs": [
{
"output_type": "stream",
@@ -998,9 +1000,9 @@
"base_uri": "https://localhost:8080/"
},
"id": "FxzKfPMtXk-f",
- "outputId": "44405bc9-b1bd-4e5a-e53b-4611ea9217cb"
+ "outputId": "271d03bd-57c7-41db-a75f-2f5cf57adc92"
},
- "execution_count": 25,
+ "execution_count": 16,
"outputs": [
{
"output_type": "stream",
@@ -1072,7 +1074,7 @@
"metadata": {
"id": "cMTdPqrK9HwV"
},
- "execution_count": 26,
+ "execution_count": 17,
"outputs": []
},
{
@@ -1112,9 +1114,9 @@
"base_uri": "https://localhost:8080/"
},
"id": "zMt9kqJOXpmM",
- "outputId": "457a01c7-2207-4b2a-d983-36cd507c6519"
+ "outputId": "e2bf7682-444a-4496-8310-f662ffb5b146"
},
- "execution_count": 27,
+ "execution_count": 18,
"outputs": [
{
"output_type": "stream",
@@ -1155,9 +1157,9 @@
"base_uri": "https://localhost:8080/"
},
"id": "2QNC084mXrVH",
- "outputId": "b1d4eb64-4f77-4615-c71c-16eb2b38a110"
+ "outputId": "2ff10097-9fac-428f-b195-8fa8b0d4e842"
},
- "execution_count": 28,
+ "execution_count": 19,
"outputs": [
{
"output_type": "stream",
@@ -1203,13 +1205,263 @@
" Returns\n",
" program\n",
" \"\"\"\n",
- " pass"
+ " if not isinstance(P, program) or not isinstance(Q, program):\n",
+ " raise ValueError(\"alter() error! Invalid program type\")\n",
+ "\n",
+ " # Step 1: Move Q to a disjoint memory segment\n",
+ " P_haddr = P.haddr\n",
+ " offset_Q = P_haddr + 1 # Offset for Q registers\n",
+ " P_moved = move(P, 0) # P remains at its original position\n",
+ " Q_moved = move(Q, offset_Q)\n",
+ "\n",
+ " # Define result registers\n",
+ " r_p = 0 # P: result register (after move, remains 0)\n",
+ " r_q = offset_Q # Q: result register (after move)\n",
+ "\n",
+ " # Step 2: Interleave instructions and build line maps\n",
+ " interleaved_instructions = []\n",
+ " line_map_P = {}\n",
+ " line_map_Q = {}\n",
+ " instr_origin = [] # Keep track of the origin (P or Q) of each instruction\n",
+ "\n",
+ " idx = 1\n",
+ " P_idx = 1\n",
+ " Q_idx = 1\n",
+ " P_length = P_moved.length\n",
+ " Q_length = Q_moved.length\n",
+ " while P_idx <= P_length or Q_idx <= Q_length:\n",
+ " if P_idx <= P_length:\n",
+ " line_map_P[P_idx] = idx\n",
+ " interleaved_instructions.append(P_moved[P_idx - 1])\n",
+ " instr_origin.append('P')\n",
+ " idx += 1\n",
+ " P_idx += 1\n",
+ " if Q_idx <= Q_length:\n",
+ " line_map_Q[Q_idx] = idx\n",
+ " interleaved_instructions.append(Q_moved[Q_idx - 1])\n",
+ " instr_origin.append('Q')\n",
+ " idx += 1\n",
+ " Q_idx += 1\n",
+ "\n",
+ " # Step 3: Add termination code for P and Q\n",
+ " t_p = idx\n",
+ " interleaved_instructions.append(ins.C(r_p, 0)) # Copy P result to R0\n",
+ " instr_origin.append('Termination_P')\n",
+ " idx += 1\n",
+ "\n",
+ " t_q = idx\n",
+ " interleaved_instructions.append(ins.C(r_q, 1)) # Copy Q result to R1\n",
+ " instr_origin.append('Termination_Q')\n",
+ " idx += 1\n",
+ "\n",
+ " # Step 4: Adjust jump instructions\n",
+ " adjusted_instructions = []\n",
+ " for i, instr in enumerate(interleaved_instructions):\n",
+ " if instr[0] == 3: # Jumps\n",
+ " m, n, k = instr[1:]\n",
+ " origin = instr_origin[i]\n",
+ " if origin == 'P':\n",
+ " if k == 0:\n",
+ " new_k = t_p # Jump to P termination code\n",
+ " else:\n",
+ " new_k = line_map_P.get(k, idx) # Default to program end if not found\n",
+ " elif origin == 'Q':\n",
+ " if k == 0:\n",
+ " new_k = t_q # Jump to Q termination code\n",
+ " else:\n",
+ " new_k = line_map_Q.get(k, idx)\n",
+ " else:\n",
+ " new_k = k # For termination code, k remains the same\n",
+ " adjusted_instr = ins.J(m, n, new_k)\n",
+ " adjusted_instructions.append(adjusted_instr)\n",
+ " else:\n",
+ " adjusted_instructions.append(instr)\n",
+ "\n",
+ " return program(tuple(adjusted_instructions))"
],
"metadata": {
"id": "GCa3GB5wpQfd"
},
- "execution_count": null,
+ "execution_count": 21,
"outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Prepare 2 URM programs\n",
+ "text_add = \"\"\"\n",
+ " C(2, 0)\n",
+ " Z (2)\n",
+ "\n",
+ " J(1, 2, 0) check loop condition\n",
+ " S(0)\n",
+ " S(2)\n",
+ " J(0, 0, 3) repeat loop\n",
+ "\"\"\"\n",
+ "add = program(compile(text_add))\n",
+ "print(f\"3 + 4 = {run(add, 3, 4)}\")"
+ ],
+ "metadata": {
+ "id": "wTYlHtLaW0BD",
+ "outputId": "1425a50d-03c9-4bc3-c619-39c92dfd9153",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "execution_count": 22,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "3 + 4 = 7\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Predecessor function\n",
+ "text_pred = \"\"\"\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",
+ "pred = program(compile(text_pred))\n",
+ "print(f\"5 - 1 = {run(pred, 5)}\")"
+ ],
+ "metadata": {
+ "id": "JztWeQReW2Eb",
+ "outputId": "718679db-a2d0-46d9-da40-aabeb41d03ac",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "execution_count": 23,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "5 - 1 = 4\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Alternately merge two functions\n",
+ "merge = alter(add, pred)\n",
+ "print(merge)"
+ ],
+ "metadata": {
+ "id": "poXD5cUrW41Y",
+ "outputId": "f9bfb1de-b60e-4fd3-d82d-cee133b6c7ac",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "execution_count": 24,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ " 1: C(2, 0)\n",
+ " 2: J(3, 4, 14)\n",
+ " 3: Z(2)\n",
+ " 4: S(5)\n",
+ " 5: J(1, 2, 13)\n",
+ " 6: J(4, 5, 14)\n",
+ " 7: S(0)\n",
+ " 8: S(3)\n",
+ " 9: S(2)\n",
+ " 10: S(5)\n",
+ " 11: J(0, 0, 5)\n",
+ " 12: J(3, 3, 6)\n",
+ " 13: C(0, 0)\n",
+ " 14: C(3, 1)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Try running this function\n",
+ "# Build parameters for \"3 + 4\" and \"5 - 1\"\n",
+ "input_args = (3, 4, 0, 5)\n",
+ "# Debug mode\n",
+ "run(merge, *(input_args), verbose=True)"
+ ],
+ "metadata": {
+ "id": "FBABcHZaW6-w",
+ "outputId": "356c5d7c-604e-4f24-9e97-982b1b5a990a",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "execution_count": 27,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[4, 3, 4, 0, 5] C(2, 0)\n",
+ "[4, 3, 4, 0, 5] J(3, 4, 14)\n",
+ "[4, 3, 0, 0, 5] Z(2)\n",
+ "[4, 3, 0, 0, 5, 1] S(5)\n",
+ "[4, 3, 0, 0, 5, 1] J(1, 2, 13)\n",
+ "[4, 3, 0, 0, 5, 1] J(4, 5, 14)\n",
+ "[5, 3, 0, 0, 5, 1] S(0)\n",
+ "[5, 3, 0, 1, 5, 1] S(3)\n",
+ "[5, 3, 1, 1, 5, 1] S(2)\n",
+ "[5, 3, 1, 1, 5, 2] S(5)\n",
+ "[5, 3, 1, 1, 5, 2] J(0, 0, 5)\n",
+ "[5, 3, 1, 1, 5, 2] J(1, 2, 13)\n",
+ "[5, 3, 1, 1, 5, 2] J(4, 5, 14)\n",
+ "[6, 3, 1, 1, 5, 2] S(0)\n",
+ "[6, 3, 1, 2, 5, 2] S(3)\n",
+ "[6, 3, 2, 2, 5, 2] S(2)\n",
+ "[6, 3, 2, 2, 5, 3] S(5)\n",
+ "[6, 3, 2, 2, 5, 3] J(0, 0, 5)\n",
+ "[6, 3, 2, 2, 5, 3] J(1, 2, 13)\n",
+ "[6, 3, 2, 2, 5, 3] J(4, 5, 14)\n",
+ "[7, 3, 2, 2, 5, 3] S(0)\n",
+ "[7, 3, 2, 3, 5, 3] S(3)\n",
+ "[7, 3, 3, 3, 5, 3] S(2)\n",
+ "[7, 3, 3, 3, 5, 4] S(5)\n",
+ "[7, 3, 3, 3, 5, 4] J(0, 0, 5)\n",
+ "[7, 3, 3, 3, 5, 4] J(1, 2, 13)\n",
+ "[7, 3, 3, 3, 5, 4] C(0, 0)\n",
+ "[7, 3, 3, 3, 5, 4] C(3, 1)\n"
+ ]
+ },
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "7"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 27
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Error**: The merged program does not meet expectations. From observing the debug information, memory[0] being 7 is as expected, but memory[3] is 3, which does not match the expected value of 4. This seems to be caused by an issue where either program P or Q terminates the entire program once it finishes executing."
+ ],
+ "metadata": {
+ "id": "2m9OpQnFXJ5V"
+ }
}
]
}
\ No newline at end of file