Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added language_theory/__init__.py
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions language_theory/witnesses/collatz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Witness: The Collatz Conjecture

This directory contains a concrete witness for the concept of "decidable refactoring," as proposed in the main `language_theory/THEORY.md` document. It separates the undecidable Collatz conjecture into two components:

1. **`collatz_total.py`**: A **total function** that computes a single step of the Collatz sequence. It is guaranteed to terminate because it takes a "fuel" parameter that limits the number of steps.

2. **`control_program.py`**: The **unbounded control logic** that orchestrates the Collatz sequence. This program is not guaranteed to terminate and represents the undecidable part of the problem.

This separation provides a practical example of how to isolate and manage undecidability in a formal system, which is a core concept of the theoretical model.
Empty file.
33 changes: 33 additions & 0 deletions language_theory/witnesses/collatz/collatz_total.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# A total function that computes one step of the Collatz sequence.
# This function is guaranteed to terminate.

def collatz_step(n):
if n % 2 == 0:
return n // 2
else:
return 3 * n + 1

def collatz_total(n, fuel):
"""
Computes the Collatz sequence for a given n, with a 'fuel' limit.
This function is a total function, guaranteed to terminate.
"""
if fuel <= 0:
return n

current_n = n
for _ in range(fuel):
if current_n == 1:
break
current_n = collatz_step(current_n)
return current_n

if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print("Usage: python -m language_theory.witnesses.collatz.collatz_total <n> <fuel>")
sys.exit(1)
n = int(sys.argv[1])
fuel = int(sys.argv[2])
result = collatz_total(n, fuel)
print(f"Result: {result}")
30 changes: 30 additions & 0 deletions language_theory/witnesses/collatz/control_program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# The control program for the Collatz sequence.
# This program is not guaranteed to terminate.

from .collatz_total import collatz_total

def control_program(n, initial_fuel=10):
"""
This program orchestrates the Collatz sequence computation.
It is not guaranteed to terminate for all inputs.
"""
current_n = n
fuel = initial_fuel

while current_n != 1:
current_n = collatz_total(current_n, fuel)
if current_n != 1:
# Decide whether to allocate more fuel or give up.
# For this example, we'll just double the fuel.
fuel *= 2

return current_n

if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print("Usage: python -m language_theory.witnesses.collatz.control_program <n>")
sys.exit(1)
n = int(sys.argv[1])
result = control_program(n)
print(f"Result: {result}")