Langlib

CI

Langlib is a Lean 4 library of formalized results about grammars, language classes, and automata across the Chomsky hierarchy.

Regular Languages

Linear and DCF Languages

Context-Free Languages

Indexed Languages

Context-Sensitive Languages

Recursive Languages

Recursively Enumerable Languages

Automata

Examples

Proof overview

🔗 indicates that this repository contains a corresponding proof file.

Hierarchy And Equivalences

Grammar sideRelationAutomaton side
Regular languages (Left-regular ⇔🔗 Right-regular)🔗DFA languages (Mathlib)
🔗🔗
Deterministic context-free languages🔗DPDA final-state languages
⊊ (⊆ 🔗)⊊ (⊆ 🔗)
Context-free languages🔗PDA languages (Final State ⇔ Empty Stack 🔗)
🔗 (⊆ CS 🔗)
Indexed languagesNested Stack Automata
Context-sensitive languages (Non-erasing ⇔ Non-contracting (⇒ 🔗))LBA languages (LBA ⇔? DLBA)
⊊ (⊆ RE 🔗)⊊ (⊆ 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.

Closure

Abstract closure predicates (ClosedUnderUnion, ClosedUnderHomomorphism, etc.) are defined in 🔗.

OperationRegularDCFLCFLINDCSLRecursiveRE
UnionYes 🔗NoYes 🔗Yes 🔗Yes 🔗YesYes 🔗
IntersectionYes 🔗NoNo 🔗NoYesYesYes
ComplementYes 🔗Yes (🔗)No 🔗NoYesYes 🔗No
ConcatenationYes 🔗NoYes 🔗YesYes 🔗YesYes 🔗
Kleene starYes 🔗NoYes 🔗YesYesYes 🔗Yes
(String) homomorphismYes (letter-to-letter 🔗)NoYes 🔗YesNoNoYes
ε-free (string) homomorphismYes 🔗NoYes 🔗YesYesYesYes 🔗
SubstitutionYes 🔗NoYes 🔗YesYesNoYes
Inverse homomorphismYes 🔗YesYes 🔗YesYesYesYes
ReverseYes 🔗NoYes 🔗YesYes 🔗YesYes 🔗
Intersection with a regular languageYes 🔗Yes 🔗Yes 🔗YesYesYesYes
Right quotientYes 🔗NoNoNoNoNoYes
Right quotient with a regular languageYes 🔗NoYes 🔗YesYesYesYes

Notes on the table above:

Additional context-free closure results formalized here:

Decidability

Results marked ✓ᶜ use ComputablePred (the strongest form). Results marked ✓ use only Mathlib's Decidable instance (weaker — does not establish computability).

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 large 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!