Skip to content

feat(MultiTapeTM): Any function constant with finitely many exceptions is computable in constant time and space - #854

Open
crei wants to merge 9 commits into
leanprover:mainfrom
crei:tm_finite
Open

feat(MultiTapeTM): Any function constant with finitely many exceptions is computable in constant time and space#854
crei wants to merge 9 commits into
leanprover:mainfrom
crei:tm_finite

Conversation

@crei

@crei crei commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

This is a starting point of a Turing machine combinator library, it adds one of the leaves:

Any function that is constant with finitely many exceptions is computable in constant time and zero space, relative to any encoding. The same holds for any function with a finite domain.

This result captures many functions we want to compose later or with a combinator library: Any function on tuples of Bool, for example and "equality comparison with a constant". Together with a "fold" and "composition" combinators, this already allows us to evaluate CNFs or compute Nat.succ on binary encoded numbers.

The empty list has to be added explicitly for the case where `α` is empty: `finiteFunTM` uses
the elements of this set as its states while reading the input, so the set has to contain the
starting state `[]` even if there is no input to read. -/
noncomputable def encPrefixes (encIn : α ↪ List Bool) : Finset (List Bool) :=

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one seems a bit of a shame to make noncomputable;

@crei

crei commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator Author

It turns out, almost the same machine can compute any function that is constant with a finite number of exceptions. There are two very important functions in this class: Equality and inequality comparison with a constant. This function is a building block in defining a "while" combinator, so I think it's worth it expanding the file.

@crei crei changed the title feat(MultiTapeTM): Any function between finite types is computable in constant time and space feat(MultiTapeTM): Any function constant with finitely many exceptions is computable in constant time and space Sep 4, 2026
exact Cfg.ext_zero_tapes rfl rfl (by simp)

/-- A constant time bound for the machine `almostConstTM`. -/
public def almostConstTime (encIn : α ↪ List Bool) (encOut : β ↪ List Bool) (f : α → β)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an @[expose] here? Same with finiteFunTime.

(Function.uncurry Bool.and)
(fun _ => c) (fun _ => 0) := by
intro encIn encOut
apply encodedComputableInTimeAndSpace_of_finite

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function.uncurry fullAdder leaves the output as a function of carry. Could we use fun (a, b, carry) => fullAdder a b carry so the example computes the sum/carry pair from all three input bits?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, of course!

@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator

You may have mispushed?

@crei

crei commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator Author

Indeed! sorry ;)

crei added 2 commits September 8, 2026 13:52
The merge of `origin/main` split the configuration out into `Configuration.lean`
and reworked how a step is taken: `TransitionOut` became `Action`, and the
configuration update moved out of `step` into a separate `Action.apply`. The
field rename went unnoticed because `almostConstTM` builds its transitions with
positional anonymous constructors and the field order did not change, but
`step` now stops at `(tm.tr …).apply cfg`, so a `simp only [step, …]` no longer
reduces the record projections.
output : List Symbol
deriving Inhabited

/-- Two configurations of a machine without work tapes are equal if their states, input head

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another pull request moved this part of Deterministic.lean into its own file, so this lemma is also just moved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants