Skip to content

Commit e46e252

Browse files
author
Atticus Kuhn
committed
finish nat r
1 parent b2e3401 commit e46e252

File tree

8 files changed

+461
-209
lines changed

8 files changed

+461
-209
lines changed

Autoomaton.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
import Autoomaton.Buchi
44
import Autoomaton.NatRF
55
import Autoomaton.OrdinalRF
6+
import Autoomaton.Ordinal2
67
import Autoomaton.Path
8+
import Autoomaton.succeeds

Autoomaton/Buchi.lean

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set_option pp.universes true in
2323
structure Automaton (S : Type) where
2424
/-- The transition relation $R \subseteq S \times S$. -/
2525
R : S → S → Prop
26+
-- Rdec : ∀ s1: S, DecidablePred (R s1)
2627
/-- The set of initial states $I \subseteq S$. -/
2728
I : S → Bool
2829
/-- The set of fair (accepting) states $F \subseteq S$. -/
@@ -65,6 +66,17 @@ def State.IsReachable (s : S) : Prop :=
6566
-- ∃ (r : Run a), ∃ (i : Nat), r.f i = s
6667
∃ (i : S), Relation.ReflTransGen a.R i s ∧ a.I i
6768

69+
theorem init_reachable (s : S) (p : a.I s) : State.IsReachable (a := a) s := by
70+
use s
71+
72+
theorem next_reachable (s1 s2 : S) (rel : a.R s1 s2) (r_s1 : State.IsReachable (a := a) s1 ): State.IsReachable (a := a) s2 := by
73+
rcases r_s1 with ⟨ i, i_rfm, i_init⟩
74+
use i
75+
simp only [i_init, and_true]
76+
rw [Relation.ReflTransGen.cases_tail_iff]
77+
right
78+
use s1
79+
6880
theorem run_reachable (n : Nat) : State.IsReachable (a := a) (r.f n) := by
6981
-- use r
7082
-- use n

Autoomaton/NatRF.lean

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import Autoomaton.Buchi
2-
2+
import Autoomaton.Path
3+
import Autoomaton.succeeds
34
variable {S : Type} {a : Automaton S} (r : Run a)
45

56
/-- A ranking function for a Büchi automaton maps states to natural numbers.
67
The rank must not increase along transitions. If the source state of a transition is fair, the rank must strictly decrease.
78
This is a witness for the emptiness of fair runs for an automaton.
89
-/
9-
structure RankingFunction {S : Type} (a : Automaton S) where
10+
structure RankingFunction {S : Type} (a : Automaton S) : Type where
1011
/-- The ranking function $V: S \to \mathbb{N}$. -/
11-
(rank : S → Nat)
12+
rank : S → Nat
13+
reach : S → Prop
14+
init_reach : (s : S) → a.I s → reach s
15+
next_reach (s1 s2 : S) : a.R s1 s2 → reach s1 → reach s2
1216
/-- The condition on the ranking function: for all transitions $(s_1, s_2) \in R$,
1317
$V(s_2) + \mathbb{I}(s_1 \in F) \le V(s_1)$, where $\mathbb{I}$ is the indicator function. -/
14-
(rank_le_of_rel : ∀ s1 s2, a.R s1 s2 → rank s2 + (if a.F s1 then 1 else 0) ≤ rank s1)
18+
(rank_le_of_rel : ∀ s1 s2, a.R s1 s2 → reach s1 → rank s2 + (if a.F s1 then 1 else 0) ≤ rank s1)
1519

1620
variable (V : RankingFunction a)
1721

22+
theorem nat_run_reachable (n : Nat) : V.reach (r.f n) := by
23+
induction' n with m ih
24+
· exact V.init_reach (r.f 0) (r.is_init)
25+
· exact V.next_reach (r.f m) (r.f (m + 1)) (r.is_valid m) ih
26+
1827
/-- The set of natural numbers `n` for which the state `r.f n` is a fair state.
1928
This represents the time indices at which a run `r` visits a fair state.
2029
Formally, $\{n \in \mathbb{N} \mid r.f(n) \in F\}$. -/
@@ -37,7 +46,7 @@ lemma rank_plus_fairCount_le_rank_zero (y : Nat) : V.rank (r.f y) + fairCount r
3746
induction' y with i ih
3847
· simp only [fairCount, Nat.count_zero, add_zero, le_refl]
3948
· simp only [Nat.count_succ, fairCount] at *
40-
have : V.rank (r.f (i + 1)) + (if a.F (r.f i) then 1 else 0) ≤ V.rank (r.f i) := V.rank_le_of_rel (r.f i) (r.f (i + 1)) (r.is_valid i)
49+
have : V.rank (r.f (i + 1)) + (if a.F (r.f i) then 1 else 0) ≤ V.rank (r.f i) := V.rank_le_of_rel (r.f i) (r.f (i + 1)) (r.is_valid i) (nat_run_reachable r V i)
4150
omega
4251

4352
/-- The number of fair visits up to any time `y` is bounded by the rank of the initial state.
@@ -91,3 +100,63 @@ theorem isFairEmpty_of_rankingFunction (V : RankingFunction a) : a.IsFairEmpty :
91100
Set.mem_setOf_eq] at x_gt_max
92101
have x_lt_x : x < x := x_gt_max x x_fair
93102
omega
103+
104+
-- def decidable_path (s1 : S) [Finite S] [f : (s1 s2 : S) → Decidable (a.R s1 s2)] : DecidablePred (fun s2 ↦ Relation.ReflTransGen a.R s1 s2) := sorry
105+
106+
noncomputable def fair_successors (s1 : S) [fin : Finite S] : Finset S :=
107+
let _ : Fintype S := Fintype.ofFinite S
108+
let _ : DecidablePred (fun s2 ↦ Relation.ReflTransGen a.R s1 s2 ∧ a.F s2) := Classical.decPred (fun s2 ↦ Relation.ReflTransGen a.R s1 s2 ∧ a.F s2)
109+
{s2 | Relation.ReflTransGen a.R s1 s2 ∧ a.F s2}
110+
111+
noncomputable def fair_count (s1 : S) [fin : Finite S] : Nat :=
112+
Finset.card (fair_successors (a := a) s1)
113+
114+
theorem lt_le (a b : Nat) : (a + 1 ≤ b) ↔ (a < b) := by omega
115+
116+
noncomputable def completeness [fin : Finite S] (a : Automaton S) (fe : a.IsFairEmpty) : RankingFunction a := {
117+
rank := fun s => fair_count (a := a) s,
118+
reach := State.IsReachable (a := a),
119+
init_reach := init_reachable (a := a),
120+
next_reach := next_reachable (a := a) ,
121+
rank_le_of_rel := fun s1 s2 s1_r_s2 s1_reach => by
122+
have f_subset : fair_successors (a := a) s2 ⊆ fair_successors (a := a) s1 := by
123+
intro e e_mem
124+
simp only [fair_successors, Finset.mem_filter, Finset.mem_univ, true_and] at e_mem
125+
rcases e_mem with ⟨e_r_s2, e_f⟩
126+
simp only [fair_successors, Finset.mem_filter, Finset.mem_univ, e_f, and_true, true_and]
127+
trans s2
128+
· exact Relation.ReflTransGen.single s1_r_s2
129+
· exact e_r_s2
130+
by_cases s1_fair : a.F s1
131+
<;> simp only [fair_count, fair_successors, s1_fair, ↓reduceIte, lt_le, gt_iff_lt, s1_fair, Bool.false_eq_true, ↓reduceIte, add_zero, ge_iff_le]
132+
· apply Finset.card_lt_card
133+
simp only [fair_successors] at f_subset
134+
simp only [Finset.ssubset_def, f_subset, true_and]
135+
intro sub
136+
have s1_in : s1 ∈ fair_successors (a := a) s2 :=
137+
sub (by
138+
simp only [Finset.mem_filter, Finset.mem_univ, s1_fair, and_true, true_and]
139+
exact Relation.ReflTransGen.refl)
140+
simp only [fair_successors, Finset.mem_filter, Finset.mem_univ, true_and] at s1_in
141+
rcases s1_in with ⟨s2_r_s1, _⟩
142+
apply fe
143+
apply vardiRun_fair (if Even · then s2 else s1)
144+
· intro n
145+
rcases s1_reach with ⟨ i, i_rfm, i_init⟩
146+
by_cases p : Even n
147+
<;> simp only [Nat.even_add_one, p, not_false_eq_true, not_true_eq_false, ↓reduceIte]
148+
<;> use s1, i
149+
<;> simp only [i_init, i_rfm, Relation.ReflTransGen.refl, s1_fair,
150+
Relation.TransGen.single s1_r_s2, s2_r_s1, and_self, true_and]
151+
exact ⟨by
152+
trans s1
153+
· exact i_rfm
154+
· exact Relation.ReflTransGen.single s1_r_s2
155+
, by
156+
rw [Relation.TransGen.head'_iff]
157+
use s2
158+
159+
· exact Finset.card_le_card f_subset
160+
}
161+
162+
#print axioms completeness

Autoomaton/Ordinal2.lean

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import Autoomaton.Buchi
2+
import Autoomaton.NatRF
3+
import Autoomaton.Path
4+
import Autoomaton.succeeds
5+
6+
7+
namespace ordinal2
8+
variable {S A: Type} [Preorder A] [WellFoundedLT A] {a : Automaton S} (r : Run a)
9+
10+
universe u
11+
12+
/-- An ordinal-valued ranking function for a Büchi automaton maps states to ordinals.
13+
The rank must not increase along transitions. If the source state of a transition is fair, the rank must strictly decrease.
14+
This is a more general form of a ranking function.
15+
-/
16+
structure OrdinalRankingFunction {S: Type} (A : Type u) [Preorder A] [WellFoundedLT A] (a : Automaton S) : Type u where
17+
/-- The ranking function $W: S \to \text{Ordinal}$. -/
18+
rank : S → A
19+
reach : S → Prop
20+
init_reach : (s : S) → a.I s → reach s
21+
next_reach (s1 s2 : S) : a.R s1 s2 → reach s1 → reach s2
22+
/-- The condition on the ranking function: for all transitions $(s_1, s_2) \in R$,
23+
$W(s_2) + \mathbb{I}(s_1 \in F) \le W(s_1)$, where $\mathbb{I}$ is the indicator function. -/
24+
rank_le_of_rel_fair : ∀ s1 s2, a.R s1 s2 → reach s1 → a.F s1 → rank s2 < rank s1
25+
rank_le_of_rel_unfair : ∀ s1 s2, a.R s1 s2 → reach s1 → rank s2 ≤ rank s1
26+
27+
28+
29+
30+
variable (W : OrdinalRankingFunction A a)
31+
theorem run_reachable2 (n : Nat) : W.reach (r.f n) := by
32+
induction' n with m ih
33+
· exact W.init_reach (r.f 0) (r.is_init)
34+
· exact W.next_reach (r.f m) (r.f (m + 1)) (r.is_valid m) ih
35+
36+
/-- The sequence of ranks at the times of fair visits.
37+
`ordSeq r W n` is the rank of the state at the `n`-th fair visit, i.e., $W(r.f(\text{nth_visit}(r, n)))$. -/
38+
noncomputable def ordSeq (n : Nat) : A := W.rank (r.f (nth_visit r n))
39+
40+
/-- If a run `r` is fair, then the set of its fair visits is infinite. This is a direct consequence of the definition of a fair run. -/
41+
theorem fair_infinite (r_fair : r.IsFair) : Set.Infinite (fairVisits r) := by
42+
simp only [Set.infinite_iff_exists_gt, Set.mem_setOf_eq]
43+
intro a
44+
rcases (r_fair a) with ⟨k, k_gt_a, k_fair⟩
45+
exact ⟨k, ⟨k_fair, by omega⟩⟩
46+
47+
/-- If a run `r` is fair, then the state at the `n`-th fair visit is indeed a fair state.
48+
$r.f(\text{nth_visit}(r, n)) \in F$. -/
49+
theorem is_fair_at_nth_visit (n : Nat) (r_fair : r.IsFair) : a.F (r.f (nth_visit r n)) := Nat.nth_mem_of_infinite (fair_infinite r r_fair) n
50+
51+
/-- The sequence of ranks along a run is antitone (non-increasing).
52+
The function $n \mapsto W(r.f(n))$ is an antitone function from $\mathbb{N}$ to the ordinals. -/
53+
theorem rank_antitone : Antitone (fun n => W.rank (r.f n)) := antitone_nat_of_succ_le (fun n => by
54+
have cf := W.rank_le_of_rel_fair (r.f n) (r.f (n + 1)) (r.is_valid n)
55+
have cu := W.rank_le_of_rel_unfair (r.f n) (r.f (n + 1)) (r.is_valid n)
56+
by_cases is_fair : a.F (r.f n)
57+
<;> simp only [is_fair, ↓reduceIte, Ordinal.add_one_eq_succ, Order.succ_le_iff, Bool.false_eq_true, add_zero] at cf
58+
· exact LT.lt.le (W.rank_le_of_rel_fair (r.f n) (r.f (n + 1)) (r.is_valid n) (run_reachable2 r W n) is_fair)
59+
· exact cu (run_reachable2 r W n))
60+
61+
/-- For a fair run, the sequence of ranks at fair visits is strictly decreasing.
62+
$W(r.f(\text{nth_visit}(r, n+1))) < W(r.f(\text{nth_visit}(r, n)))$. -/
63+
theorem ordSeq_strict_anti (r_fair : r.IsFair) : StrictAnti (ordSeq r W) := strictAnti_nat_of_succ_lt (fun n => by
64+
have : nth_visit r n < nth_visit r (n + 1) := @Nat.nth_strictMono (fun (x : Nat) => a.F (r.f x)) (fair_infinite r r_fair) n (n + 1) (by omega)
65+
have yf := W.rank_le_of_rel_fair (r.f (nth_visit r n)) (r.f (nth_visit r (n) + 1)) (r.is_valid (nth_visit r n)) (run_reachable2 r W (nth_visit r n)) (is_fair_at_nth_visit r n r_fair)
66+
have yu := W.rank_le_of_rel_unfair (r.f (nth_visit r n)) (r.f (nth_visit r (n) + 1)) (r.is_valid (nth_visit r n))
67+
68+
simp only [is_fair_at_nth_visit r n r_fair, ↓reduceIte, Ordinal.add_one_eq_succ,
69+
Order.succ_le_iff] at yf
70+
exact LE.le.trans_lt (by
71+
apply rank_antitone
72+
omega) yf)
73+
74+
/-- The comparison of ranks in the `ordSeq` is equivalent to the reversed comparison of their indices.
75+
This is a property of strictly antitone sequences.
76+
For $m, n \in \mathbb{N}$, $W(r.f(\text{nth_visit}(r, m))) < W(r.f(\text{nth_visit}(r, n))) \iff n < m$. -/
77+
theorem ordSeq_lt_iff_lt (r_fair : r.IsFair) {m n : ℕ} : ordSeq r W m < ordSeq r W n ↔ n < m := StrictAnti.lt_iff_lt (ordSeq_strict_anti r W r_fair)
78+
79+
80+
/-
81+
If `a` has no fair runs, then `stateSucceeds a` is well-founded.
82+
-/
83+
theorem succeeds_wf (a : Automaton S) (fe : a.IsFairEmpty) : WellFounded (stateSucceeds a) := by
84+
simp only [WellFounded.wellFounded_iff_no_descending_seq]
85+
by_contra c
86+
simp only [not_isEmpty_iff, nonempty_subtype] at c
87+
rcases c with ⟨s, y⟩
88+
exact fe (vardiRun s y) (vardiRun_fair s y)
89+
90+
91+
/-
92+
Provide the `IsWellFounded` instance for `stateSucceeds` from `succeeds_wf`.
93+
-/
94+
instance n {S : Type} (a : Automaton S) (fe : a.IsFairEmpty) : IsWellFounded S (stateSucceeds a) where
95+
wf := succeeds_wf a fe
96+
97+
/-- The main theorem for ordinal-valued ranking functions.
98+
If an ordinal-valued ranking function `W` exists for an automaton `a`, then `a` has no fair runs.
99+
This is proven by showing that a fair run would imply an infinite decreasing sequence of ordinals, which is impossible. -/
100+
theorem isFairEmpty_of_ordinalRankingFunction (W : OrdinalRankingFunction A a) : a.IsFairEmpty := fun r r_fair =>
101+
(RelEmbedding.not_wellFounded_of_decreasing_seq ⟨⟨ordSeq r W,
102+
StrictAnti.injective (ordSeq_strict_anti r W r_fair)
103+
⟩, by
104+
intros m n
105+
simp only [Function.Embedding.coeFn_mk, gt_iff_lt]
106+
exact ordSeq_lt_iff_lt r W r_fair⟩) (wellFounded_lt)
107+
108+
#print axioms isFairEmpty_of_ordinalRankingFunction
109+
110+
111+
112+
/-
113+
Completeness: construct an ordinal ranking from emptiness of fair runs.
114+
115+
Given `fe : a.IsFairEmpty`, the relation `stateSucceeds a` is well-founded, and we
116+
define `W` to be the `WellFounded.rank` on this relation. This yields an
117+
`OrdinalRankingFunction a` whose rank strictly decreases on fair steps and never
118+
increases otherwise. In particular, for every transition `s1 → s2` we have
119+
$$
120+
W(s_2) + \mathbb{I}(s_1 \in F) \le W(s_1),
121+
$$
122+
where `\mathbb{I}` is the indicator of fairness. This is the converse direction
123+
to `isFairEmpty_of_ordinalRankingFunction`.
124+
-/
125+
noncomputable def ordinalRankingFunction_of_isFairEmpty (fe : a.IsFairEmpty) : (OrdinalRankingFunction (Ordinal.{0}) a) := {
126+
reach := fun s => State.IsReachable (a := a) s,
127+
init_reach := init_reachable,
128+
next_reach := next_reachable,
129+
rank := @IsWellFounded.rank S (stateSucceeds a) (n a fe),
130+
rank_le_of_rel_fair := fun s1 s2 rel => by
131+
intro s1_reachable s1_fair
132+
apply IsWellFounded.rank_lt_of_rel (hwf := n a fe)
133+
rcases s1_reachable with ⟨i, i_to_s1, i_initial⟩
134+
use s1, i
135+
simp only [i_initial, i_to_s1, s1_fair, true_and]
136+
rw [Relation.ReflTransGen.cases_head_iff, Relation.transGen_iff]
137+
simp only [true_or, rel, and_self]
138+
,
139+
rank_le_of_rel_unfair := by
140+
intros s1 s2 s1_r_s2 s1_reachable
141+
by_contra c
142+
simp only [not_le] at c
143+
repeat rw [IsWellFounded.rank_eq] at c
144+
have : sSup (Set.range fun (b : { b // stateSucceeds a b s2 }) ↦ Order.succ (IsWellFounded.rank (hwf := n a fe) (stateSucceeds a) ↑b)) ≤ sSup (Set.range fun (b : { b // stateSucceeds a b s1 }) ↦ Order.succ (IsWellFounded.rank (hwf := n a fe) (stateSucceeds a) ↑b)) := csSup_le_csSup (by
145+
simp only [BddAbove, Set.Nonempty, upperBounds, Set.mem_range, Subtype.exists, exists_prop,
146+
forall_exists_index, and_imp, forall_apply_eq_imp_iff₂, Order.succ_le_iff, Set.mem_setOf_eq]
147+
use IsWellFounded.rank (hwf := n a fe) (stateSucceeds a) s1
148+
intros s3
149+
exact IsWellFounded.rank_lt_of_rel (hwf := n a fe))
150+
(by
151+
by_cases n : (∃ (f : S), stateSucceeds a f s2 )
152+
· rcases n with ⟨f, f_suc_s2 ⟩
153+
apply Set.range_nonempty (ι := { b // stateSucceeds a b s2 }) (h := by use f)
154+
· simp only [not_exists] at n
155+
have : IsEmpty { b // stateSucceeds a b s2 } := ⟨ fun ⟨ a, a_succ ⟩ => n a a_succ⟩
156+
simp only [ciSup_of_empty, Ordinal.bot_eq_zero, Ordinal.not_lt_zero] at c
157+
)
158+
(by
159+
intro s s_in_b
160+
simp only [Set.mem_range, Subtype.exists, exists_prop] at s_in_b
161+
rcases s_in_b with ⟨ n, n_suc_s2, ord_suc⟩
162+
simp only [Set.mem_range, Subtype.exists, exists_prop]
163+
use n
164+
simp only [ord_suc, and_true]
165+
exact succeeds_concat4 s1 s2 n a s1_r_s2 n_suc_s2 s1_reachable
166+
)
167+
have := LT.lt.not_ge c
168+
contradiction
169+
}
170+
171+
--info: 'ordinalRankingFunction_of_isFairEmpty' depends on axioms: [propext, Classical.choice, Quot.sound]
172+
#print axioms ordinalRankingFunction_of_isFairEmpty

0 commit comments

Comments
 (0)