Skip to content

feat: Bitstring encodings - #21

Draft
BoltonBailey wants to merge 6 commits into
SamuelSchlesinger:devfrom
BoltonBailey:feat/bitstring-encoding
Draft

feat: Bitstring encodings#21
BoltonBailey wants to merge 6 commits into
SamuelSchlesinger:devfrom
BoltonBailey:feat/bitstring-encoding

Conversation

@BoltonBailey

@BoltonBailey BoltonBailey commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #25. The file relocation (moving the rose-tree Data type and
DataEncode into Complexitylib/Encoding/) has been split out into #25.
This PR now contains only the encoding work itself. Merge #25 first, after
which this diff shrinks to just the changes below.

This PR adds bitstring encodings, trying to make it a bit more natural to express e.g. polytime computability on arbitrary input types.

As a part of this it refactors the "delimiting" operation out of the current definition of pair, to reuse for other typeclass derivations.

What this adds on top of #25

  • Encoding/Delimit.lean — the self-delimiting block framing extracted out of pair,
    plus its parsers (unpair?, undelimitBlock, takeFirstBlock, hasBlock,
    tagBlock, undelimitBlocks) and their correctness lemmas
  • Encoding/Pairing.leanpair x y becomes delimit x ++ y
  • Encoding/Data.leanData.toBits / Data.fromBits, the round-trip
    Data.fromBits_toBits, injectivity Data.toBits_injective, and Data.size_lt_of_mem
  • Encoding/DataEncode.leanDataEncode.bitstringEncode and its injectivity,
    plus the List Bool instance

The two pair definitions produce the same bitstring but associate differently, so five
consumer proofs are realigned (ThreeSAT/Verifier, PairEmit/Internal, UTM/Internal/Init,
PairBuildTM, PPoly/Advice). No statement is weakened.

Open question

There is a DataEncode class which encodes into arbitrary-arity trees; it may be better to
just encode those trees as lists. Worth settling before this leaves draft, since it changes
this same API surface.

🤖 Generated with Claude Code

@BoltonBailey
BoltonBailey marked this pull request as ready for review August 1, 2026 14:19
@BoltonBailey

Copy link
Copy Markdown
Collaborator Author

Hopefully with these changes it should also be possible to refactor existing files like:

  • SAT/Encoding.lean
  • DescriptiveComplexity/Encoding.lean
  • Circuits/Encoding/Defs.lean

@BoltonBailey
BoltonBailey marked this pull request as draft August 1, 2026 22:57
BoltonBailey and others added 2 commits August 13, 2026 19:02
# Conflicts:
#	Complexitylib/Models/RoseTreeMachine/Data.lean
`dev` de-exposed `Encoding/DataEncode.lean` as part of the module-interface
minimization, but this branch adds `bitstringEncode` together with
`bitstringEncode_def` and `bitstringEncode_injective`, both of which need
the definition's body to typecheck. Expose the single definition rather
than re-exposing the whole module.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
`dev` gained commit 60118c9 ("revert encoding stuff") via the SamuelSchlesinger#23 merge,
which deleted Complexitylib/Encoding.lean and Encoding/Delimit.lean and
inlined the delimiting logic back into Encoding/Pairing.lean. That is the
exact refactor this branch performs, so the merge conflicts on both files.

Resolved in favour of this branch's extraction: keep Encoding.lean and
Encoding/Delimit.lean, and restore this branch's Encoding/Pairing.lean
(`pair x y = delimit x ++ y`). No declaration is lost — every declaration
in dev's Pairing.lean is present in this branch's Pairing.lean plus
Delimit.lean. Also restore the `Complexitylib.Encoding` import in
Complexitylib.lean, which the merge dropped because dev deleted the line.

The two `pair`s produce the same bitstring but associate differently
(`(A ++ sep) ++ y` here vs `A ++ (sep ++ y)` on dev), so five consumer
proofs needed the shape realigned:

- SAT/ThreeSAT/Verifier: unfold `delimit` in the `foldl_append` chain
- TuringMachine/Subroutines/PairEmit/Internal: add `delimit` to `simpa`
- TuringMachine/UTM/Internal/Init: add `delimit` to `simp`
- Classes/NP/Internal/PairBuildTM: drop now-unused `List.append_assoc`
- Classes/PPoly/Advice: drop now-unused `List.append_assoc`

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
BoltonBailey and others added 2 commits August 13, 2026 22:01
…layer

`Data` and the `DataEncode` typeclass are machine-independent: `Data` is the
rose tree the RTM operates on, but it is also the target every `DataEncode`
instance encodes into, so it does not belong under `Models/RoseTreeMachine/`.

Move `Models/RoseTreeMachine/Data.lean` to `Encoding/Data.lean` and
`Models/RoseTreeMachine/DataEncode.lean` to `Encoding/DataEncode.lean`, and
repoint the three import statements that referred to the old paths.

Nothing else changes. `Encoding/Data.lean` is byte-identical to the file it
replaces; `Encoding/DataEncode.lean` differs only in its own import of
`Data`. Namespaces, module docs, comments and declarations are all untouched,
so the `Complexity.RoseTreeMachine` namespace is preserved for now.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ncoding

# Conflicts:
#	Complexitylib/Encoding/Data.lean
#	Complexitylib/Models.lean
@BoltonBailey
BoltonBailey force-pushed the feat/bitstring-encoding branch from 4925b02 to f54611b Compare August 14, 2026 05:05
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.

1 participant