The subword counting game

Lax100001.Counting · concepts/Lax100001/Counting.lean · lax-100001

definition

Natural Language Statement

Definition

A guesser must find a hidden binary word. It may ask how often a chosen word occurs in the hidden word as a subword. The letters of the chosen word are matched, in order, to letters of the hidden word, and letters of the hidden word may be skipped. Occurrences that use different positions are counted separately. For example, 01\texttt{01} occurs five times in 010110\texttt{010110}.

The guesser is a rule. It sees its earlier queries together with the answers, and then either asks one more query or stops with a guess. A history is a run of the rule when every answer is true, every query is the one that the rule asks after the exchanges before it, and the rule finally stops with the hidden word. A rule reconstructs all words, with a bound on the number of queries, when one rule, one constant and one length threshold work for every hidden word. The run uses only allowed queries, and a hidden word of length nn at least the threshold needs at most C⋅rate(n)C \cdot \mathrm{rate}(n) exchanges.

Concept map
100%

Preparing this local graph with the browser’s fonts…

Proven claimDefinitionThis conceptRelated conceptA → B: B builds on A

In the paper

  • page 1 of this submission's paper

Lean source

1/-
2Copyright (c) 2026 Anonymous. All rights reserved.
3Released under Apache 2.0 license as described in the file LICENSE.
4Authors: ChatGPT (OpenAI), Claude (Anthropic)
5-/
6import Mathlib.Data.List.Count
7import Mathlib.Data.List.Lex
8import Mathlib.Data.List.Sublists
9import Mathlib.Data.Real.Basic
10
11/-!
12---
13title: The subword counting game
14type: definition
15---
16A guesser must find a hidden binary word. It may ask how often a chosen word occurs in the
17hidden word as a subword. The letters of the chosen word are matched, in order, to letters of
18the hidden word, and letters of the hidden word may be skipped. Occurrences that use different
19positions are counted separately. For example, 01\texttt{01} occurs five times in 010110\texttt{010110}.
20
21The guesser is a rule. It sees its earlier queries together with the answers, and then either
22asks one more query or stops with a guess. A history is a run of the rule when every answer is
23true, every query is the one that the rule asks after the exchanges before it, and the rule
24finally stops with the hidden word. A rule reconstructs all words, with a bound on the number of
25queries, when one rule, one constant and one length threshold work for every hidden word. The
26run uses only allowed queries, and a hidden word of length nn at least the threshold needs at
27most C⋅rate(n)C \cdot \mathrm{rate}(n) exchanges.
28-/
29
30namespace Lax100001.Counting
31
32/-- A binary word, written as a list of bits. The bit false\texttt{false} is the letter 0\texttt{0}
33and the bit true\texttt{true} is the letter 1\texttt{1}. -/
34abbrev Word := List Bool
35
36/-- The number of occurrences of pattern\mathit{pattern} in word\mathit{word} as a subword.
37
38An occurrence is a choice of positions of word\mathit{word}, from left to right, whose letters spell
39pattern\mathit{pattern}. The list word.sublists\texttt{word.sublists} has one entry for each choice of positions, so
40counting the entries equal to pattern\mathit{pattern} counts the occurrences. For example,
41count [false, true, false, true, true, false] [false, true]=5\texttt{count [false, true, false, true, true, false] [false, true]} = 5. The word is
42010110\texttt{010110}, and the pattern 01\texttt{01} is spelled by the positions (1,2)(1, 2), (1,4)(1, 4),
43(1,5)(1, 5), (3,4)(3, 4) and (3,5)(3, 5). -/
44def count (word pattern : Word) : ℕ := word.sublists.count pattern
45
46/-- One question and the answer that the guesser received. -/
47structure Exchange where
48 /-- The word whose occurrences the guesser asked for. -/
49 query : Word
50 /-- The number of occurrences that the guesser was told. -/
51 answer : ℕ
52
53/-- What the guesser does next: ask for another count, or stop with a guess. -/
54inductive Move where
55 /-- Ask for the number of occurrences of query\mathit{query} in the hidden word. -/
56 | ask (query : Word)
57 /-- Stop and name guess\mathit{guess} as the hidden word. -/
58 | stop (guess : Word)
59
60/-- A guesser. It sees the earlier exchanges, oldest first, and chooses its next move. The
61empty list is what it sees at the start. -/
62abbrev Rule := List Exchange → Move
63
64/-- history\mathit{history} is a complete play of rule\mathit{rule} against the hidden word
65word\mathit{word}.
66
67Every answer in the history is true. Every query in the history is the one that the rule asks
68when it has seen only the exchanges before it. After the last exchange the rule stops with
69the hidden word. For example, let a rule ask 0\texttt{0} first and stop with 000\texttt{000} after
70the answer 33. Then [⟨[false],3⟩][\langle[\texttt{false}], 3\rangle] is a run of this rule against the hidden
71word 000\texttt{000}. -/ structure IsRun (rule : Rule) (word : Word) (history : List Exchange) : Prop
72where
73 /-- Every answer is the number of occurrences of its query in the hidden word. -/
74 answers_true : ∀ e ∈ history, e.answer = count word e.query
75 /-- Each query is the one that the rule asks after the exchanges before it. -/
76 asks_next : ∀ (i : ℕ) (hi : i < history.length),
77 rule (history.take i) = .ask history[i].query
78 /-- After the whole history the rule stops with the hidden word. -/
79 stops : rule history = .stop word
80
81/-- A nonempty word is a Lyndon word when it is smaller than each of its proper rotations.
82
83Words are compared in dictionary order, with 0<10 < 1. For example 0\texttt{0}, 01\texttt{01} and
840011\texttt{0011} are Lyndon words, while 10\texttt{10} and 0101\texttt{0101} are not. -/
85def IsLyndon (pattern : Word) : Prop :=
86 pattern ≠ [] ∧ ∀ k, 0 < k → k < pattern.length →
87 pattern < pattern.drop k ++ pattern.take k
88
89/-- One rule reconstructs every binary word, asks only allowed queries, and uses few queries.
90
91There are a rule, a positive constant and a length threshold such that every hidden word has a
92run of the rule in which every query satisfies allowed\mathit{allowed}. When the hidden word has length
93nn at least the threshold, the run has at most C⋅rate(n)C \cdot \mathrm{rate}(n) exchanges. The rule is
94chosen before the hidden word, so it does not know nn, which occurs only in the bound. -/
95def Reconstructs (allowed : Word → Prop) (rate : ℕ → ℝ) : Prop :=
96 ∃ rule : Rule, ∃ constant : ℝ, 0 < constant ∧ ∃ threshold : ℕ,
97 ∀ word : Word, ∃ history : List Exchange,
98 IsRun rule word history ∧ (∀ e ∈ history, allowed e.query) ∧
99 (threshold ≤ word.length → (history.length : ℝ) ≤ constant * rate word.length)
100
101end Lax100001.Counting
102