Motivation
I am working on constrained decoding of large language models (LLMs).
Today's auto-regressive LLMs take a string $s \in \Sigma^*$ as input, and output a probability distribution over an alphabet $\Sigma$. We then sample a symbol $\sigma$ from this distribution, append it to $s$ to construct $s' = s\cdot\sigma$, and repeat the process with $s'$.
Constrained generation restricts the output distribution to a subset $\Sigma' \subseteq \Sigma$. We construct $\Sigma'$ as $\Sigma' = f(s)$, where $f$ is some computable function, and $s$ is the string generated so far (auto-regressive construction).
I am interested in the power of the corresponding formal grammar:
- If we can choose any computable $f$ and generate strings auto-regressively, where would we find ourselves in the Chomsky hierarchy? (My hunch right now is somewhere strictly between recursive and recursively enumerable.)
- Is this type of grammar already studied somewhere?
Formal question
Let's define an auto-regressive grammar (I made up this term) as a pair $(f, \Sigma)$, where
- $\Sigma$ is an alphabet that includes the terminal character $\bot$.
- $f: \Sigma^* \rightarrow \mathcal{P}(\Sigma)$ is some computable function that takes a string and returns a subset of symbols. $f$ may or may not halt on its input.
Let $L$ be the language induced by $(f, \Sigma)$. A word $w = \sigma_1\cdot\sigma_1\cdot\ldots\cdot\sigma_n$ is in $L$ if we have $$ \sigma_1 \in f() \\ \sigma_2 \in f(\sigma_1) \\ \sigma_3 \in f(\sigma_1 \cdot \sigma_2) \\ \ldots \\ \sigma_n \in f(\sigma_1\cdot\sigma_1\cdot\ldots\cdot\sigma_{n-1}) \\ \bot \in f(\sigma_1\cdot\sigma_1\cdot\ldots\cdot\sigma_{n}) $$ and each of the invocations of $f$ halts.
- Is this a known grammar?
- What is the power of this grammar?
My work so far
Is this a known grammar?
I couldn't find it, but I also don't really know how to search for it.
What is the power of this grammar?
I think it is at least powerful enough to model all recursive languages (https://en.wikipedia.org/wiki/Recursive_language). To see this, note that we can just have $f(s)$ internally enumerate all strings in $L$ up until length one greater than $s$, and output the delta.
I think it is more powerful than grammars for recursive languages, because it should be able to model the universal language (https://www.cs.columbia.edu/~aho/cs3261/Lectures/L16-Universal_Language.html). To see this, note that we can model $f$ to accept pairs $(M, s)$ of a Turing machine and a string as input, and then either output $\bot$ if $M$ accepts $s$, or not halt otherwise.
I do not know if it is as powerful as grammars for recursively enumerable languages.
Edit: I think now that it is less powerful than grammars for recursively enumerable languages. To see this, consider again that it should be able to model the universal language. But this time, we make the following modification to the universal language:
- We say that strings ending with a new, special symbol $\dagger$ should always be accepted.
If we had an auto-regressive function $f$ to model this language, then it would have to accept $(M, s)\dagger$. Thus, we need $\dagger \in f(M, s)$, and $f(M, s)$ must halt, for all inputs $(M, s)$. But this means that $f(M, s)$ must decide if $M$ accepts $s$ [since $M$ accepts $s$ $\iff$ $\bot \in f(M, s)$]. Hence, we could use $f$ to not just accept, but decide the universal language, which is a contradiction.
Does this reasoning make sense?