Skip to content

nielstron/langlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1,340 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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

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 πŸ”— 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.

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.

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!

About

Library for formal language theory in Lean 4

Resources

License

Stars

4 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors