Langlib logo

Langlib

CI

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 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.

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.

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 NameGrammarRelationAutomaton
RegularRegular (Left-regular ๐Ÿ”— โ‡”๐Ÿ”— Right-regular ๐Ÿ”—)โ‡” ๐Ÿ”—Finite Automata ๐Ÿ”— (NFA โ‡” ๐Ÿ”— DFA)
โŠŠ ๐Ÿ”—โŠŠ ๐Ÿ”—
Deterministic context-freeLR(k) ๐Ÿ”—โ‡” ๐Ÿ”—Deterministic Pushdown Automata ๐Ÿ”—
โŠŠ ๐Ÿ”—โŠŠ ๐Ÿ”—
Context-freeContext-free ๐Ÿ”—โ‡” ๐Ÿ”—Pushdown Automata ๐Ÿ”— (Final State โ‡” ๐Ÿ”— Empty Stack)
โŠŠ ๐Ÿ”— (โŠŠ CS ๐Ÿ”—)โŠŠ
IndexedIndexed ๐Ÿ”—โ‡”Nested Stack Automata
โŠŠ ๐Ÿ”—โŠŠ
Context-sensitiveContext-sensitive ๐Ÿ”— (Non-erasing โ‡” ๐Ÿ”— Non-contracting ๐Ÿ”—)โ‡” ๐Ÿ”—Linear Bounded Automaton ๐Ÿ”— (DLBA ๐Ÿ”— โ‡”? NLBA (โІ ๐Ÿ”—))
โŠŠ ๐Ÿ”—
RecursiveโŠŠ ๐Ÿ”— (โІ ๐Ÿ”—)Turing-machines with halting deciders ๐Ÿ”—
โŠŠ ๐Ÿ”—
Recursively EnumerableUnrestricted ๐Ÿ”—โ‡” ๐Ÿ”—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

Closure

We define abstract closure predicates (ClosedUnderUnion, ClosedUnderHomomorphism, etc.) for uniform proofs in ๐Ÿ”—.

OperationRegularDCFLCFLINDCSLRecursiveRE
UnionYes ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—
IntersectionYes ๐Ÿ”—No ๐Ÿ”—No ๐Ÿ”—NoYes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—
ComplementYes ๐Ÿ”—Yes ๐Ÿ”—No ๐Ÿ”—NoYes ๐Ÿ”—Yes ๐Ÿ”—No ๐Ÿ”—
ConcatenationYes ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—
Kleene starYes ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—
(String) homomorphismYes ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—No ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—
ฮต-free (string) homomorphismYes ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—
SubstitutionYes ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—No ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—
Inverse homomorphismYes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—
ReverseYes ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—
Intersection with a regular languageYes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—Yes ๐Ÿ”—
Right quotientYes ๐Ÿ”—No ๐Ÿ”—No ๐Ÿ”—NoNo ๐Ÿ”—No ๐Ÿ”—Yes ๐Ÿ”—
Right quotient with a regular languageYes ๐Ÿ”—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:

Decidability

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.

LanguageMembershipEmptinessUniversalityEquivalence
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.

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:

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!