-
Notifications
You must be signed in to change notification settings - Fork 44
feat: prove that regular languages are closed under concatenation #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ctchou
wants to merge
3
commits into
leanprover:main
Choose a base branch
from
ctchou:na-total
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+244
−15
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /- | ||
| Copyright (c) 2025 Ching-Tsun Chou. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Ching-Tsun Chou | ||
| -/ | ||
|
|
||
| import Cslib.Computability.Automata.NA.Basic | ||
|
|
||
| /-! # Making a nondeterministic automaton total. | ||
| -/ | ||
|
|
||
| namespace Cslib.Automata.NA | ||
|
|
||
| open Sum ωSequence Acceptor | ||
|
|
||
| variable {Symbol State : Type*} | ||
|
|
||
| /-- `NA.totalize` makes the original NA total by replacing its LTS with `LTS.totalize` | ||
| and its starting states with their lifted non-sink versions. -/ | ||
| def totalize (na : NA State Symbol) : NA (State ⊕ Unit) Symbol where | ||
| toLTS := na.toLTS.totalize | ||
| start := inl '' na.start | ||
|
|
||
| variable {na : NA State Symbol} | ||
|
|
||
| /-- In an infinite execution of `NA.totalize`, as long as the NA stays in a non-sink state, | ||
| the execution so far corresponds to a finite execution of the original NA. -/ | ||
| theorem totalize_run_mtr {xs : ωSequence Symbol} {ss : ωSequence (State ⊕ Unit)} {n : ℕ} | ||
| (h : na.totalize.Run xs ss) (hl : (ss n).isLeft) : | ||
| ∃ s t, na.MTr s (xs.take n) t ∧ s ∈ na.start ∧ ss 0 = inl s ∧ ss n = inl t := by | ||
| obtain ⟨s, _, eq₁⟩ := h.start | ||
| obtain ⟨t, eq₂⟩ := isLeft_iff.mp hl | ||
| use s, t | ||
| refine ⟨?_, by grind⟩ | ||
| -- TODO: `grind` does not use congruence relations with `na.totalize.MTr` | ||
| rw [← LTS.totalize.mtr_left_iff, ← extract_eq_take, eq₁, ← eq₂] | ||
| exact LTS.ωTr_mTr h.trans (by grind) | ||
|
|
||
| /-- Any finite execution of the original NA can be extended to an infinite execution of | ||
| `NA.totalize`, provided that the alphabet is inbabited. -/ | ||
| theorem totalize_mtr_run [Inhabited Symbol] {xl : List Symbol} {s t : State} | ||
| (hs : s ∈ na.start) (hm : na.MTr s xl t) : | ||
| ∃ xs ss, na.totalize.Run (xl ++ω xs) ss ∧ ss 0 = inl s ∧ ss xl.length = inl t := by | ||
| grind [totalize, Run, (LTS.totalize.total na.toLTS).mTr_ωTr, =_ LTS.totalize.mtr_left_iff] | ||
|
|
||
| namespace FinAcc | ||
|
|
||
| /-- `NA.totalize` and the original NA accept the same language of finite words, | ||
| as long as the accepting states are also lifted in the obvious way. -/ | ||
| theorem totalize_language_eq {na : FinAcc State Symbol} : | ||
| language (FinAcc.mk na.totalize (inl '' na.accept)) = language na := by | ||
| ext xl | ||
| simp [totalize] | ||
|
|
||
| end FinAcc | ||
|
|
||
| end Cslib.Automata.NA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -239,7 +239,7 @@ variable {lts : LTS State Label} | |
|
|
||
| open scoped ωSequence in | ||
| /-- Any finite execution extracted from an infinite execution is valid. -/ | ||
| theorem LTS.ωTr_mTr {n m : ℕ} {hnm : n ≤ m} (h : lts.ωTr ss μs) : | ||
| theorem LTS.ωTr_mTr (h : lts.ωTr ss μs) {n m : ℕ} (hnm : n ≤ m) : | ||
| lts.MTr (ss n) (μs.extract n m) (ss m) := by | ||
| by_cases heq : n = m | ||
| case pos => grind | ||
|
|
@@ -250,16 +250,119 @@ theorem LTS.ωTr_mTr {n m : ℕ} {hnm : n ≤ m} (h : lts.ωTr ss μs) : | |
| have : lts.MTr (ss n) (μs.extract n m) (ss m) := ωTr_mTr (hnm := by grind) h | ||
| grind [MTr.comp] | ||
|
|
||
| open scoped ωSequence | ||
| open ωSequence | ||
|
|
||
| /-- Prepends an infinite execution with a transition. -/ | ||
| theorem LTS.ωTr.cons (hmtr : lts.Tr s1 μ s2) (hωtr : lts.ωTr ss μs) (hm : ss 0 = s2) : | ||
| lts.ωTr (s1 ::ω ss) (μ ::ω μs) := by | ||
| theorem LTS.ωTr.cons (htr : lts.Tr s μ t) (hωtr : lts.ωTr ss μs) (hm : ss 0 = t) : | ||
| lts.ωTr (s ::ω ss) (μ ::ω μs) := by | ||
| intro i | ||
| induction i <;> grind | ||
|
|
||
| /-- Prepends an infinite execution with a finite execution. -/ | ||
| theorem LTS.ωTr.append (hmtr : lts.MTr s μl t) (hωtr : lts.ωTr ss μs) | ||
| (hm : ss 0 = t) : ∃ ss', lts.ωTr ss' (μl ++ω μs) ∧ ss' 0 = s ∧ ss' μl.length = t := by | ||
| obtain ⟨sl, _, _, _, _⟩ := LTS.MTr.exists_states hmtr | ||
| refine ⟨sl ++ω ss.drop 1, ?_, by grind [get_append_left], by grind [get_append_left]⟩ | ||
| intro n | ||
| by_cases n < μl.length | ||
| · grind [get_append_left] | ||
| · by_cases n = μl.length | ||
| · grind [get_append_left] | ||
| · grind [get_append_right', hωtr (n - μl.length - 1)] | ||
|
|
||
| end ωMultiStep | ||
|
|
||
| section Total | ||
|
|
||
| /-! ## Total LTS | ||
|
|
||
| A LTS is total iff every state has a transition for every label. | ||
| -/ | ||
|
|
||
| open Sum ωSequence | ||
|
|
||
| variable {State Label : Type*} {lts : LTS State Label} | ||
|
|
||
| /-- `LTS.Total` provides a witness that the LTS is total. -/ | ||
| structure LTS.Total (lts : LTS State Label) where | ||
| /-- `next` rovides a next state for any given starting state and label. -/ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. provides (lacks a p)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| next : State → Label → State | ||
| /-- A proof that the state provided by `next` indeed forms a legal transition. -/ | ||
| total s μ : lts.Tr s μ (next s μ) | ||
|
|
||
| /-- `LTS.makeωTr` builds an infinite execution of a total LTS from any starting state and | ||
| any infinite sequence of labels. -/ | ||
| def LTS.makeωTr (lts : LTS State Label) (h : lts.Total) | ||
| (s : State) (μs : ωSequence Label) : ℕ → State | ||
| | 0 => s | ||
| | n + 1 => h.next (lts.makeωTr h s μs n) (μs n) | ||
|
|
||
| /-- If a LTS is total, then there exists an infinite execution from any starting state and | ||
| over any infinite sequence of labels. -/ | ||
| theorem LTS.Total.ωTr_exists (h : lts.Total) (s : State) (μs : ωSequence Label) : | ||
| ∃ ss, lts.ωTr ss μs ∧ ss 0 = s := by | ||
| use lts.makeωTr h s μs | ||
| grind [LTS.makeωTr, h.total] | ||
|
|
||
| /-- If a LTS is total, then any finite execution can be extended to an infinite execution, | ||
| provided that the label type is inbabited. -/ | ||
| theorem LTS.Total.mTr_ωTr [Inhabited Label] (ht : lts.Total) {μl : List Label} {s t : State} | ||
| (hm : lts.MTr s μl t) : ∃ μs ss, lts.ωTr ss (μl ++ω μs) ∧ ss 0 = s ∧ ss μl.length = t := by | ||
| let μs : ωSequence Label := .const default | ||
| obtain ⟨ss', ho, h0⟩ := LTS.Total.ωTr_exists ht t μs | ||
| refine ⟨μs, LTS.ωTr.append hm ho h0⟩ | ||
|
|
||
| /-- `LTS.totalize` constructs a total LTS from any given LTS by adding a sink state. -/ | ||
| def LTS.totalize (lts : LTS State Label) : LTS (State ⊕ Unit) Label where | ||
| Tr s' μ t' := match s', t' with | ||
| | inl s, inl t => lts.Tr s μ t | ||
| | _, inr () => True | ||
| | inr (), inl _ => False | ||
|
|
||
| /-- The LTS constructed by `LTS.totalize` is indeed total. -/ | ||
| def LTS.totalize.total (lts : LTS State Label) : lts.totalize.Total where | ||
| next _ _ := inr () | ||
| total _ _ := by simp [LTS.totalize] | ||
|
|
||
| /-- In `LTS.totalize`, there is no finite execution from the sink state to any non-sink state. -/ | ||
| theorem LTS.totalize.not_right_left {μs : List Label} {t : State} : | ||
| ¬ lts.totalize.MTr (inr ()) μs (inl t) := by | ||
| intro h | ||
| generalize h_s : (inr () : State ⊕ Unit) = s' | ||
| generalize h_t : (inl t : State ⊕ Unit) = t' | ||
| rw [h_s, h_t] at h | ||
| induction h <;> grind [LTS.totalize] | ||
|
|
||
| /-- In `LTS.totalize`, the transitions between non-sink states correspond exactly to | ||
| the transitions in the original LTS. -/ | ||
| @[simp] | ||
| theorem LTS.totalize.tr_left_iff {μ : Label} {s t : State} : | ||
| lts.totalize.Tr (inl s) μ (inl t) ↔ lts.Tr s μ t := by | ||
| simp [LTS.totalize] | ||
|
|
||
| /-- In `LTS.totalize`, the multistep transitions between non-sink states correspond exactly to | ||
| the multistep transitions in the original LTS. -/ | ||
| @[simp] | ||
| theorem LTS.totalize.mtr_left_iff {μs : List Label} {s t : State} : | ||
| lts.totalize.MTr (inl s) μs (inl t) ↔ lts.MTr s μs t := by | ||
| constructor <;> intro h | ||
| · generalize h_s : (inl s : State ⊕ Unit) = s' | ||
| generalize h_t : (inl t : State ⊕ Unit) = t' | ||
| rw [h_s, h_t] at h | ||
| induction h generalizing s | ||
| case refl _ => grind [LTS.MTr] | ||
| case stepL t1' μ t2' μs t3' h_tr h_mtr h_ind => | ||
| obtain ⟨rfl⟩ := h_s | ||
| cases t2' | ||
| case inl t2 => grind [LTS.MTr, totalize.tr_left_iff.mp h_tr] | ||
| case inr t2 => grind [totalize.not_right_left] | ||
| · induction h | ||
| case refl _ => grind [LTS.MTr] | ||
| case stepL t1 μ t2 μs t3 h_tr h_mtr h_ind => | ||
| grind [LTS.MTr, totalize.tr_left_iff.mpr h_tr] | ||
|
|
||
| end Total | ||
|
|
||
| section Termination | ||
| /-! ## Definitions about termination -/ | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.