diff --git a/language_theory/__init__.py b/language_theory/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/language_theory/witnesses/__init__.py b/language_theory/witnesses/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/language_theory/witnesses/collatz/README.md b/language_theory/witnesses/collatz/README.md new file mode 100644 index 00000000..38e15302 --- /dev/null +++ b/language_theory/witnesses/collatz/README.md @@ -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. diff --git a/language_theory/witnesses/collatz/__init__.py b/language_theory/witnesses/collatz/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/language_theory/witnesses/collatz/collatz_total.py b/language_theory/witnesses/collatz/collatz_total.py new file mode 100644 index 00000000..264c0f97 --- /dev/null +++ b/language_theory/witnesses/collatz/collatz_total.py @@ -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 ") + sys.exit(1) + n = int(sys.argv[1]) + fuel = int(sys.argv[2]) + result = collatz_total(n, fuel) + print(f"Result: {result}") diff --git a/language_theory/witnesses/collatz/control_program.py b/language_theory/witnesses/collatz/control_program.py new file mode 100644 index 00000000..62e911db --- /dev/null +++ b/language_theory/witnesses/collatz/control_program.py @@ -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 ") + sys.exit(1) + n = int(sys.argv[1]) + result = control_program(n) + print(f"Result: {result}")