feat: proof of Myhill-Nerode theorem for DFAs#479
feat: proof of Myhill-Nerode theorem for DFAs#479akhilesh-balaji wants to merge 0 commit intoleanprover:mainfrom
Conversation
ctchou
left a comment
There was a problem hiding this comment.
Very nice work! Please take a look my comments and make appropriate changes. I'll probably have more comments after those changes are made.
| @@ -0,0 +1,252 @@ | |||
| /- | |||
There was a problem hiding this comment.
Even though Automata.DA is used in the proofs, I think this file should be put in Cslib/Computability/Languages because it contains results about regular languages in general.
Also, please run lake exe mk_all --module, so that the new file can be added to Cslib.lean.
| * [J. E. Hopcroft, R. Motwani, J. D. Ullman, | ||
| *Introduction to Automata Theory, Languages, and Computation*][Hopcroft2006] | ||
| * [T. Malkin, *COMS W3261: Computer Science Theory, Handout 3: The Myhill-Nerode Theorem | ||
| and Implications*][Malkin2024] |
There was a problem hiding this comment.
Please add this reference to references.bib. Or perhaps the Wikipedia page suffices?
https://en.wikipedia.org/wiki/Myhill–Nerode_theorem
| public import Cslib.Computability.Automata.DA.Basic | ||
| public import Cslib.Computability.Automata.DA.Congr | ||
| public import Cslib.Computability.Languages.RegularLanguage | ||
| public import Cslib.Computability.Languages.Congruences.RightCongruence | ||
| public import Mathlib.Computability.Language | ||
| public import Mathlib.Data.Fintype.Card | ||
| public import Mathlib.CategoryTheory.Iso |
There was a problem hiding this comment.
You only need:
public import Cslib.Computability.Languages.RegularLanguage
You can find this out by running #min_imports at the end of the file.
| namespace Automata.DA | ||
| open Acceptor | ||
|
|
||
| variable {α : Type} {l m : Language α} |
There was a problem hiding this comment.
Replace Type by Type* for more generality.
| equivalence class of the language under the Nerode congruence. Note that this is simply the DFA | ||
| given rise to by the underlying right congruence with only the accept states specified here as | ||
| `{⟦ x ⟧ | x ∈ l}`. -/ | ||
| def NerodeCongruence.toFinAcc (l : Language α) : |
There was a problem hiding this comment.
Remove redundant space at the end of the line. You should be able to configure your editor to do that automatically.
|
|
||
| /-- The DFA constructed from the Nerode congruence on `l` accepts `l`. -/ | ||
| @[simp, scoped grind =] | ||
| theorem nerodecongruence_to_finacc_acc (l : Language α) : |
There was a problem hiding this comment.
The name of this theorem should probably be nerodeCongruence_language_eq, to be consistent with other similar theorems. Note that you should keep the camel case in nerodeCongruence in this all subsequent theorems.
|
|
||
| /-- The Nerode congruence is the most coarse state congruence given a language. -/ | ||
| @[simp] | ||
| theorem statecongruence_refines_nerodecongruence {M : DA.FinAcc States α} : |
There was a problem hiding this comment.
Camel case: stateCongruence, nerodeCongruence.
| theorem nerodecongruence_eqv_cls_eq_union_statecongruence_eqv_clss | ||
| {M : DA.FinAcc States α} (Q : Quotient (NerodeCongruence (language M)).eq) : | ||
| {x : List α | (⟦ x ⟧ : Quotient (NerodeCongruence (language M)).eq) = Q} = | ||
| ⋃ (R : Quotient (StateCongruence M).eq) | ||
| (_ : (⟦ Quotient.out R ⟧ : Quotient (NerodeCongruence (language M)).eq) = Q), | ||
| {x | (⟦ x ⟧ : Quotient (StateCongruence M).eq) = R} := by |
There was a problem hiding this comment.
First, I find the statement of this theorem very hard to read. Can you find a better phrasing?
Second, this is really a general fact about Setoid, isn't it? If one Setoid is a refinement of another in the sense of the previous theorem, then their equivalence classes must be related in this manner. Can you find and prove a suitably general proposition in terms of Setoid? Perhaps there is something in mathlib already? Please pose this question on Zulip if you can't find anything on mathlib.
There was a problem hiding this comment.
I had originally added this theorem thinking I would use it in the proof of unique_minimal_dfa, but I ended up not using it and did not remove it. Yes, this is a general statement about Setoids, and I will prove this generally.
But either way, I should probably remove this theorem from the file as it's not used anywhere and is unrelated to the proofs of Myhill-Nerode presented here.
I can't find anything similar in mathlib, so perhaps the general version of this belongs there.
There was a problem hiding this comment.
You can put general definitions and results about Setoid in a file under the directory Cslib/Foundations/Data/Setoid. We have done similar things for Set.
| -- | ||
|
|
||
| /-- The minimal DFA accepting `l` has `|l/c|` states. -/ | ||
| def IsMinimalAutomaton (M : DA.FinAcc States α) |
There was a problem hiding this comment.
I would add a second argument (l : Language α) so that M.IsMinimalAutomaton l means M is a minimal automaton accepting l. This would make the statement of the theorem unique_minimal_dfa below more natural. Also, instead of the finiteness assumption on the quotient type, you assume l is regular, from which it follows that the Nerode congruence of l has finite index.
| theorem unique_minimal_dfa (M : DA.FinAcc States α) [Fintype States] | ||
| [Fintype (Quotient (NerodeCongruence (language M)).eq)] (hMin : IsMinimalAutomaton M) : | ||
| ∃! φ : States ≃ Quotient (NerodeCongruence (language M)).eq, | ||
| ∀ x, φ (M.mtr M.start x) = ⟦ x ⟧ := by |
There was a problem hiding this comment.
See my comment about IsMinimalAutomaton above. By replacing language M by l, you also make the formal statement closer to your informal comment above. Instead of the finiteness assumption on the quotient type, you assume l is regular.
|
Thank you for the review. I have addressed all of the comments, except the following one:
Also, I have tried to put Please let me know what you think. |
|
@ctchou Did you mean to close this? Maybe an accidental force push? |
|
Sorry, I did something stupid and am trying to fix it now. Please stay tuned. |
|
This is part of why I pretty much never force-push, especially to a public branch involved in a PR. @akhilesh-balaji could maybe also just (force) push again or reopen themselves if need be. |
|
@akhilesh-balaji I'm very sorry for messing up your PR. I did some golfing for you, which you can find in the "myhill-nerode" branch of my cslib fork: In the future, please do not open a PR using the "main" branch. This is very error-prone and contributed to the mistake I made. |
|
@akhilesh-balaji If you have trouble re-opening this PR, just open a new one and we can proceed from there. Please do not use "main" again when you open a new PR. |
Yes, I'd normally be able to fix myself, but I think it is easiest to just reopen from a branch not named |
This PR adds a proof of the Myhill-Nerode theorem. It builds on the definition of right congruence (and associated definitions and theorems) in #278. These are the main results shown (the statement of the theorem has been taken from Wikipedia and amended slightly):
(1)
Lregular iff.∼_L(distinguishability byL, also called the Nerodecongruence) has a finite number of equivalence classes
N.(2)
Nis the number of states in the minimal DFA acceptingL.(3) The minimal DFA is unique up to unique isomorphism. That is, for any
minimal DFA acceptor, there exists exactly one isomorphism from it to the
following one: