Langlib is a Lean 4 library of formalized results from formal language theory, defining and relating various grammars, language classes, and automata across the Chomsky hierarchy and beyond.
π Documentation: overview Β· API reference
The goal of this library is to encapsulate some core results of the (extended) Chomsky hierarchy: inclusions, closures and decidability. The following gives a rough overview over the contents in highly condensed form.
The tables contain standard results. π indicates that this repository contains a corresponding definition or proof file (possibly for a weaker variant of the result, e.g. β vs. β and β vs. β).
More detailed results and developed tooling (e.g., Pumping lemmas, Totalizations) can be found in the documentation.
Each class of the (extended) hierarchy is charaterized as grammar or automaton (or both, and variants thereof). We show (strict) inclusions of the classes and equivalences between different characterizations.
In an inclusion row, a link is attached only when the cited file states a theorem
explicitly for the language or presentation classes displayed in that column. The
proof may use established equivalences. Parenthesized β links record a proved
weaker inclusion when the displayed strict result is not yet formalized for those
classes.
| Class Name | Grammar | Relation | Automaton |
|---|---|---|---|
| Regular | Regular (Left-regular π βπ Right-regular π) | β π | Finite Automata π (NFA β π DFA) |
| β π | β π | ||
| Deterministic context-free | LR(k) π | β π | Deterministic Pushdown Automata π |
| β π | β π | ||
| Context-free | Context-free π | β π | Pushdown Automata π (Final State β π Empty Stack) |
| β π (β CS π) | β | ||
| Indexed | Indexed π | β | Nested Stack Automata |
| β π | β | ||
| Context-sensitive | Context-sensitive π (Non-erasing β π Non-contracting π) | β π | Linear Bounded Automaton π (DLBA π β? NLBA (β π)) |
| β π | |||
| Recursive | β π (β π) | Turing-machines with halting deciders π | |
| β π | |||
| Recursively Enumerable | Unrestricted π | β π | Turing-machines π |
The strict inclusion Indexed β CS is formalized for finite alphabets with at least two symbols. The underlying inclusion Indexed β CS π holds over every terminal type.
Additional results
- Context Free Languages β π Mathlib's
IsContextFree. - Regular β π Linear β π Context-free.
- Regular β π Recursive.
- Context-free β π Recursive.
We define abstract closure predicates (ClosedUnderUnion, ClosedUnderHomomorphism, etc.) for uniform proofs in π.
| Operation | Regular | DCFL | CFL | IND | CSL | Recursive | RE |
|---|---|---|---|---|---|---|---|
| Union | Yes π | No π | Yes π | Yes π | Yes π | Yes π | Yes π |
| Intersection | Yes π | No π | No π | No | Yes π | Yes π | Yes π |
| Complement | Yes π | Yes π | No π | No | Yes π | Yes π | No π |
| Concatenation | Yes π | No π | Yes π | Yes π | Yes π | Yes π | Yes π |
| Kleene star | Yes π | No π | Yes π | Yes π | Yes π | Yes π | Yes π |
| (String) homomorphism | Yes π | No π | Yes π | Yes π | No π | No π | Yes π |
Ξ΅-free (string) homomorphism |
Yes π | No π | Yes π | Yes π | Yes π | Yes π | Yes π |
| Substitution | Yes π | No π | Yes π | Yes π | No π | No π | Yes π |
| Inverse homomorphism | Yes π | Yes π | Yes π | Yes π | Yes π | Yes π | Yes π |
| Reverse | Yes π | No π | Yes π | Yes π | Yes π | Yes π | Yes π |
| Intersection with a regular language | Yes π | Yes π | Yes π | Yes π | Yes π | Yes π | Yes π |
| Right quotient | Yes π | No π | No π | No | No π | No π | Yes π |
| Right quotient with a regular language | Yes π | Yes π | Yes π | Yes π | No π | No π | Yes π |
For a negative closure entry, No means that closure fails over some finite
alphabet; it does not claim failure over every alphabet. The linked files expose
embedding/cardinality variants (for example, _of_embedding and _of_card) giving
the proved sufficient alphabet-size bounds. Positive entries are stated uniformly
over the finite alphabet assumptions required by their definitions.
Additional DCFL results:
Additional CFL results:
Additional CSL results:
Membership is the uniform word problem for a concrete presentation: the input is a
valid encoded automaton or grammar together with a word. ComputableMembership
π takes an optional validity
promise, requires valid codes to present exactly the stated language class, and
requires one partial-recursive evaluator to halt and answer correctly on every
valid code-and-word pair. It separately requires raw encoded membership to be
uniformly recursively enumerable; this prevents the semantic decoding map itself
from hiding a non-r.e. membership oracle.
The remaining columns use the corresponding uniform emptiness, universality, and equivalence problems for the indicated standard presentation.
| Language | Membership | Emptiness | Universality | Equivalence |
|---|---|---|---|---|
| Regular | β π | β π | β π | β π |
| Deterministic context-free | β π | β π | β | β |
| Context-free | β π | β π | β | β |
| Context-sensitive | β π | β | β | β |
| Recursive | β π | β π | β π | β π |
| Recursively enumerable | β π | β π | β π | β π |
For Recursive membership, the input program is promised to be an always-halting
decider. The linked theorem supplies one universal evaluator taking the raw program
code and word jointly, proves that it halts and is correct under that promise, and
shows that valid codes present exactly the recursive languages over every finite
computably encoded alphabet. Emptiness, universality, and equivalence are
undecidable for this presentation over every nonempty computably encoded alphabet;
nonemptiness is optimal because an empty alphabet has only the empty word. The
separate diagonal result
π says that
these semantically valid programs cannot instead be replaced by an adequate
Primcodable type on which membership is total for every raw code; that is a
different, stronger requirement.
For most uses, import the hub:
import LanglibIf you only need one part of the development, import the corresponding module directly, for example:
import Langlib.Classes.ContextFree.Definition
import Langlib.Grammars.ContextFree.Definition
import Langlib.Automata.Pushdown.Equivalence.ContextFree
import Langlib.Classes.Regular.Decidability.Membership
import Langlib.Classes.Recursive.Decidability.MembershipThe files in test/LanglibTest provide small worked examples:
- test/LanglibTest/DemoContextFree.lean
- test/LanglibTest/DemoContextSensitive.lean
- test/LanglibTest/DemoUnrestricted.lean
To build the library and examples, run:
lake buildTo install Lean 4, follow the Lean community manual.
To download and build this project, run:
git clone https://github.com/nielstron/langlib
cd langlib
lake buildThis repository started as a Lean 4 port of madvorak/grammars. It further includes a port of the Pumping Lemma proof from AlexLoitzl/pumping_cfg and the equivalence proof between CFGs and PDAs from shetzl/autth.
A part of this repository was created with the help of Aristotle. It's an amazing tool for ambitious proofs. Special thanks to the developers to provide this tool to the community!