Skip to content

Comments

Add implementation of Kleene's algorithm to show each regular language has a regex matching it #35600

Draft
yisiox wants to merge 4 commits intoleanprover-community:masterfrom
focs-lab:kleenes-algorithm
Draft

Add implementation of Kleene's algorithm to show each regular language has a regex matching it #35600
yisiox wants to merge 4 commits intoleanprover-community:masterfrom
focs-lab:kleenes-algorithm

Conversation

@yisiox
Copy link

@yisiox yisiox commented Feb 21, 2026

This PR adds a proof that every regular language has some regular expression matching it.

This was achieved by the following constructions and proofs of their correctness

  • Define toSingleεNFA to transform any epsilon-NFA to an equivalent one with only a single start and accept state (and the type is ExtendedState)
  • Define a bijection between ExtendedState and Fin (FinEnum.card (ExtendedState σ)) to index the states
  • Define pathRegex which implements Kleene's algorithm
  • Define toRegex which takes an epsilon-NFA, applies toSingleεNFA on it, and computes the corresponding regex using pathRegex

The proofs of correctness chain together to yield (toRegex M).matches' = M.accepts which asserts the direction of Kleene's theorem required.


Open in Gitpod

@github-actions github-actions bot added the new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! label Feb 21, 2026
@github-actions
Copy link

Welcome new contributor!

Thank you for contributing to Mathlib! If you haven't done so already, please review our contribution guidelines, as well as the style guide and naming conventions.

We use a review queue to manage reviews. If your PR does not appear there, it is probably because it is not successfully building (i.e., it doesn't have a green checkmark), has the awaiting-author tag, or another reason described in the Lifecycle of a PR.

If you haven't already done so, please come to https://leanprover.zulipchat.com/, introduce yourself, and mention your new PR.

Thank you again for joining our community.

@github-actions github-actions bot added the t-computability Computability theory (TMs, DFAs, languages, grammars, etc) label Feb 21, 2026
@github-actions
Copy link

PR summary dffa0f2129

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.Computability.EpsilonNFA 706 716 +10 (+1.42%)
Import changes for all files
Files Import difference
Mathlib.Computability.EpsilonNFA 10

Declarations diff

+ ExtendedState
+ ExtendedState.equivSum
+ IsPath.from_accept
+ IsPath.state_accept
+ IsPath.toSingleεNFA_lift_extendedState
+ IsRestrictedMatch
+ IsRestrictedMatch.mono
+ IsRestrictedPath
+ accepts_toRegex
+ accepts_toSingleεNFA
+ directRegex
+ e
+ instance [FinEnum σ] : FinEnum (ExtendedState σ)
+ instance {M : εNFA α σ}
+ isRestrictedMatch_iff_exists_isPath
+ isRestrictedMatch_iff_exists_isRestrictedPath
+ isRestrictedMatch_nil
+ isRestrictedMatch_of_mem_pathRegex
+ isRestrictedMatch_star
+ isRestrictedPath_iff_isPath
+ matches'_foldl_acc
+ matches'_foldl_sum
+ mem_matches'_directRegex
+ mem_matches_mul_star_mul
+ mem_matches_star_concat
+ mem_matches_star_singleton
+ mem_pathRegex_iff_isRestrictedMatch
+ mem_pathRegex_of_isRestrictedMatch
+ pathRegex
+ pathRegex_mono
+ pathRegex_trans
+ toRegex
+ toSingleεNFA

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for scripts/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! t-computability Computability theory (TMs, DFAs, languages, grammars, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant