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

Closure

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

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

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

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!