Langlib
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
Proof overview
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 proof file (possibly to 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.
Hierarchy And Equivalences
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.
| Grammar side | Relation | Automaton side |
|---|---|---|
| Regular languages (Left-regular โ๐ Right-regular) | โ ๐ | DFA languages (Mathlib) |
| โ ๐ | โ ๐ | |
| LR(k) grammar languages ๐ | โ | DPDA final-state languages ๐ |
| โ (โ ๐) | โ ๐ | |
| Context-free languages | โ ๐ | PDA languages (Final State โ Empty Stack ๐) |
| โ ๐ (โ CS ๐) | โ | |
| Indexed languages | โ | Nested Stack Automata |
| โ | โ | |
| Context-sensitive languages (Non-erasing โ Non-contracting (โ ๐)) | โ ๐ | LBA languages (DLBA โ ๐ LBA, LBA โ? DLBA) |
| โ ๐ | โ ๐ (โ RE ๐) | |
| Recursive languages | โ ๐ | Turing-machine languages (Mathlib), with halting deciders |
| โ ๐ | โ ๐ | |
| Recursively enumerable languages | โ ๐ | Turing-machine languages (Mathlib) |
Additional results
- Context Free Languages โ ๐ Mathlib's
IsContextFree. - Regular โ ๐ Linear โ ๐ Context-free.
- Regular โ ๐ Recursive.
- Context-free โ ๐ Recursive.
Closure
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 | Yes | No ๐ | Yes ๐ |
Additional DCFL results:
Additional CFL results:
Additional CSL results:
Decidability
Each column refers to the corresponding uniform computability predicate
(ComputableMembership, ComputableEmptiness, ComputableUniversality,
ComputableEquivalence) defined in
๐.
| Language | Membership | Emptiness | Universality | Equivalence |
|---|---|---|---|---|
| Regular | โ ๐ | โ ๐ | โ ๐ | โ ๐ |
| Deterministic context-free | โ | โ | โ | โ |
| Context-free | โ ๐ | โ ๐ | โ | โ |
| Context-sensitive | โ ๐ | โ | โ | โ |
| Recursive | โ ๐ | โ | โ | โ |
| Recursively enumerable | โ ๐ | โ ๐ | โ ๐ | โ ๐ |
How To Use The Library
For most uses, import the hub:
import Langlib
If 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.Membership
The 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 build
Installation Instructions
To 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 build
Acknowledgements
This 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!