interplane.Get Omega
Read in your language

spark-connector

aien-dev/spark-connector

Brought by drakestapleton

branch main Branches Tags History

spark-connector / trust / Counting.lean

267 lines · 11766 bytes Raw Blame History
1/-
2 Counting.lean — a Lean 4 model of the Interplane hub's community "rerun" counting rule.
3
4 What this models:
5 * spark-connector docs/PROTOCOL.md, section "Counting rule (the hub page)";
6 * interplane-forge community/20api.om `cm_count1` (the per-result counting decision);
7 * interplane-forge community/30page.om `cm_count` (N, K, D shown on the hub page).
8
9 This is a model of the RULE, not of the Omega implementation. The correspondence between
10 the definitions below and the Omega code in 20api.om / 30page.om is NOT proved.
11
12 Abstractions:
13 * A result is (key, revision, outcome). The key is the machine's key id, the revision is
14 the part id, and the outcome stands for the triple (binary-hash, stdout-hash, exit);
15 both key and outcome are abstract types with decidable equality.
16 * The input list holds results in arrival order and only results that already passed
17 every protocol check (signature, mismatch, rate limits, ...). Results whose outcome is
18 `cell<N>` (stored but never counted) are assumed filtered out before this rule applies.
19 Rule: scanning in arrival order, a result is kept when its revision is `rev` and no earlier
20 kept result has the same key (first result per key wins; later ones are stored, never
21 counted). N = length of the counted list; agree o = number of counted results with outcome o;
22 K = the largest agree o; D = N - K.
23
24 Core Lean 4 only (no Mathlib).
25-/
26
27namespace Counting
28
29/-- One stored result: who ran it (`key`), for which part revision (`rev`), and what came out
30(`outcome`, standing for the triple binary-hash, stdout-hash, exit). -/
31structure Result (Key Out : Type) where
32 key : Key
33 rev : Nat
34 outcome : Out
35
36variable {Key Out : Type} [DecidableEq Key]
37
38/-- Keys already present in a list of kept results. -/
39def keys (l : List (Result Key Out)) : List Key := l.map (·.key)
40
41/-- One step of the scan: keep `r` iff it is for revision `rev` and its key is not yet kept. -/
42def step (rev : Nat) (acc : List (Result Key Out)) (r : Result Key Out) :
43 List (Result Key Out) :=
44 if r.rev = rev ∧ r.key ∉ keys acc then acc ++ [r] else acc
45
46/-- The counted list for revision `rev`: scan the results in arrival order. -/
47def counted (rev : Nat) (rs : List (Result Key Out)) : List (Result Key Out) :=
48 rs.foldl (step rev) []
49
50/-- N: how many keys are counted for the revision. -/
51def N (rev : Nat) (rs : List (Result Key Out)) : Nat := (counted rev rs).length
52
53/-! ### Helper lemmas about one scan step and the fold -/
54
55theorem step_of_not_rev {rev : Nat} {acc : List (Result Key Out)} {r : Result Key Out}
56 (h : r.rev ≠ rev) : step rev acc r = acc := by
57 simp [step, h]
58
59theorem step_of_key_mem {rev : Nat} {acc : List (Result Key Out)} {r : Result Key Out}
60 (h : r.key ∈ keys acc) : step rev acc r = acc := by
61 simp [step, h]
62
63/-- The scan only ever appends: the accumulator is a prefix of the result. -/
64theorem foldl_prefix (rev : Nat) :
65 ∀ (rs acc : List (Result Key Out)), acc <+: rs.foldl (step rev) acc
66 | [], acc => List.prefix_refl acc
67 | r :: rs, acc => by
68 have ih := foldl_prefix rev rs (step rev acc r)
69 have hstep : acc <+: step rev acc r := by
70 unfold step
71 split
72 · exact List.prefix_append acc [r]
73 · exact List.prefix_refl acc
74 exact List.IsPrefix.trans hstep ih
75
76/-- One step keeps the keys of the accumulator pairwise distinct. -/
77theorem step_nodup (rev : Nat) (acc : List (Result Key Out)) (r : Result Key Out)
78 (h : (keys acc).Nodup) : (keys (step rev acc r)).Nodup := by
79 unfold step
80 split
81 · rename_i hc
82 simp only [keys, List.map_append, List.map_cons, List.map_nil]
83 rw [List.nodup_append]
84 refine ⟨h, List.nodup_cons.mpr ⟨List.not_mem_nil, List.nodup_nil⟩, ?_⟩
85 intro a ha b hb hab
86 simp only [List.mem_singleton] at hb
87 subst hb
88 subst hab
89 exact hc.2 ha
90 · exact h
91
92theorem foldl_nodup (rev : Nat) :
93 ∀ (rs acc : List (Result Key Out)), (keys acc).Nodup →
94 (keys (rs.foldl (step rev) acc)).Nodup
95 | [], _, h => h
96 | r :: rs, acc, h => foldl_nodup rev rs (step rev acc r) (step_nodup rev acc r h)
97
98theorem counted_append_one (rev : Nat) (rs : List (Result Key Out)) (r : Result Key Out) :
99 counted rev (rs ++ [r]) = step rev (counted rev rs) r := by
100 simp [counted, List.foldl_append]
101
102/-! ### 1. keys_nodup: a key counts once per revision -/
103
104theorem keys_nodup (rev : Nat) (rs : List (Result Key Out)) :
105 ((counted rev rs).map (·.key)).Nodup :=
106 foldl_nodup rev rs [] List.nodup_nil
107
108/-! ### 2. replay_no_change: a replay or second result is not double counted -/
109
110/-- If some counted result already has key `k`, appending any further result with key `k`
111(any revision, any outcome) leaves the counted list unchanged. -/
112theorem replay_no_change (rev : Nat) (rs : List (Result Key Out)) (k : Key)
113 (hk : k ∈ (counted rev rs).map (·.key)) (r : Result Key Out) (hr : r.key = k) :
114 counted rev (rs ++ [r]) = counted rev rs := by
115 rw [counted_append_one]
116 exact step_of_key_mem (by rw [hr]; exact hk)
117
118/-- The same for any number of appended results whose keys are all already counted. -/
119theorem replay_list_no_change (rev : Nat) (rs more : List (Result Key Out))
120 (hmore : ∀ r ∈ more, r.key ∈ (counted rev rs).map (·.key)) :
121 counted rev (rs ++ more) = counted rev rs := by
122 induction more generalizing rs with
123 | nil => simp
124 | cons r more ih =>
125 have hr : counted rev (rs ++ [r]) = counted rev rs := by
126 rw [counted_append_one]
127 exact step_of_key_mem (hmore r (List.mem_cons_self ..))
128 have hrest : ∀ x ∈ more, x.key ∈ (counted rev (rs ++ [r])).map (·.key) := by
129 rw [hr]; exact fun x hx => hmore x (List.mem_cons_of_mem _ hx)
130 rw [show rs ++ r :: more = (rs ++ [r]) ++ more by simp, ih (rs ++ [r]) hrest, hr]
131
132/-! ### 3. other_rev_no_change: results for another revision are ignored -/
133
134theorem other_rev_no_change (rev : Nat) (rs : List (Result Key Out)) (r : Result Key Out)
135 (hr : r.rev ≠ rev) : counted rev (rs ++ [r]) = counted rev rs := by
136 rw [counted_append_one]
137 exact step_of_not_rev hr
138
139/-! ### Agreement: outcomes are compared from here on -/
140
141variable [DecidableEq Out]
142
143/-- agree o: how many counted results have outcome `o`. -/
144def agree (rev : Nat) (rs : List (Result Key Out)) (o : Out) : Nat :=
145 (counted rev rs).countP (fun r => r.outcome == o)
146
147/-- K: the size of the largest agreeing group (0 when nothing is counted). -/
148def K (rev : Nat) (rs : List (Result Key Out)) : Nat :=
149 ((counted rev rs).map (fun r => agree rev rs r.outcome)).foldr max 0
150
151/-- D: how many counted results are outside the largest agreeing group. -/
152def D (rev : Nat) (rs : List (Result Key Out)) : Nat := N rev rs - K rev rs
153
154/-! ### 4. agree_le_count: no outcome has more agreeing results than N -/
155
156theorem agree_le_count (rev : Nat) (rs : List (Result Key Out)) (o : Out) :
157 agree rev rs o ≤ N rev rs :=
158 List.countP_le_length
159
160/-! ### K is exactly the largest agreeing group -/
161
162theorem foldr_max_ge {α : Type} (f : α → Nat) :
163 ∀ (l : List α) (x : α), x ∈ l → f x ≤ (l.map f).foldr max 0
164 | [], _, hx => absurd hx List.not_mem_nil
165 | y :: l, x, hx => by
166 simp only [List.map_cons, List.foldr_cons]
167 cases List.mem_cons.mp hx with
168 | inl h => subst h; exact Nat.le_max_left _ _
169 | inr h => exact Nat.le_trans (foldr_max_ge f l x h) (Nat.le_max_right _ _)
170
171theorem foldr_max_lt {α : Type} (f : α → Nat) (n : Nat) (hn : 0 < n) :
172 ∀ (l : List α), (∀ x ∈ l, f x < n) → (l.map f).foldr max 0 < n
173 | [], _ => hn
174 | y :: l, h => by
175 simp only [List.map_cons, List.foldr_cons]
176 exact Nat.max_lt.mpr ⟨h y (List.mem_cons_self ..),
177 foldr_max_lt f n hn l (fun x hx => h x (List.mem_cons_of_mem _ hx))⟩
178
179theorem agree_eq_zero_of_absent (rev : Nat) (rs : List (Result Key Out)) (o : Out)
180 (h : ∀ r ∈ counted rev rs, r.outcome ≠ o) : agree rev rs o = 0 := by
181 unfold agree
182 rw [List.countP_eq_zero]
183 intro r hr
184 simpa using h r hr
185
186/-- Every agreeing group has size at most K (so K is the largest agree o). -/
187theorem agree_le_K (rev : Nat) (rs : List (Result Key Out)) (o : Out) :
188 agree rev rs o ≤ K rev rs := by
189 by_cases h : ∃ r ∈ counted rev rs, r.outcome = o
190 · obtain ⟨r, hr, ho⟩ := h
191 have := foldr_max_ge (fun r => agree rev rs r.outcome) (counted rev rs) r hr
192 rw [ho] at this
193 exact this
194 · rw [agree_eq_zero_of_absent rev rs o (fun r hr ho => h ⟨r, hr, ho⟩)]
195 exact Nat.zero_le _
196
197/-- K is attained: when anything is counted, some outcome has exactly K agreeing results. -/
198theorem K_attained (rev : Nat) (rs : List (Result Key Out)) (h : counted rev rs ≠ []) :
199 ∃ r ∈ counted rev rs, agree rev rs r.outcome = K rev rs := by
200 unfold K
201 generalize counted rev rs = l at h ⊢
202 induction l with
203 | nil => exact absurd rfl h
204 | cons y l ih =>
205 simp only [List.map_cons, List.foldr_cons]
206 cases l with
207 | nil => exact ⟨y, List.mem_cons_self .., by simp⟩
208 | cons z l' =>
209 obtain ⟨w, hw, hwe⟩ := ih (List.cons_ne_nil z l')
210 by_cases hle : agree rev rs y.outcome ≤ (List.map (fun r => agree rev rs r.outcome) (z :: l')).foldr max 0
211 · exact ⟨w, List.mem_cons_of_mem _ hw, by rw [Nat.max_eq_right hle, hwe]⟩
212 · exact ⟨y, List.mem_cons_self .., (Nat.max_eq_left (Nat.le_of_lt (Nat.lt_of_not_le hle))).symm⟩
213
214theorem K_le_N (rev : Nat) (rs : List (Result Key Out)) : K rev rs ≤ N rev rs := by
215 by_cases h : counted rev rs = []
216 · simp [K, N, h]
217 · obtain ⟨r, _, hr⟩ := K_attained rev rs h
218 rw [← hr]
219 exact agree_le_count rev rs r.outcome
220
221/-! ### 5. disagreement_visible: a disagreement cannot be presented as all-agreeing -/
222
223/-- If the counted list contains two results with different outcomes, every outcome has
224strictly fewer than N agreeing results. -/
225theorem disagreement_visible (rev : Nat) (rs : List (Result Key Out))
226 (a b : Result Key Out) (ha : a ∈ counted rev rs) (hb : b ∈ counted rev rs)
227 (hab : a.outcome ≠ b.outcome) : ∀ o, agree rev rs o < N rev rs := by
228 intro o
229 apply Nat.lt_of_le_of_ne (agree_le_count rev rs o)
230 intro heq
231 have hall := List.countP_eq_length.mp heq
232 have h1 : a.outcome = o := by simpa using hall a ha
233 have h2 : b.outcome = o := by simpa using hall b hb
234 exact hab (h1.trans h2.symm)
235
236/-- Consequently K < N, whatever group is called largest. -/
237theorem disagreement_K_lt_N (rev : Nat) (rs : List (Result Key Out))
238 (a b : Result Key Out) (ha : a ∈ counted rev rs) (hb : b ∈ counted rev rs)
239 (hab : a.outcome ≠ b.outcome) : K rev rs < N rev rs := by
240 have hN : 0 < N rev rs := List.length_pos_of_mem ha
241 exact foldr_max_lt (fun r => agree rev rs r.outcome) (N rev rs) hN (counted rev rs)
242 (fun r _ => disagreement_visible rev rs a b ha hb hab r.outcome)
243
244/-- ... and so D ≥ 1: the page shows "D disagree" and not "reproduced". -/
245theorem disagreement_D_pos (rev : Nat) (rs : List (Result Key Out))
246 (a b : Result Key Out) (ha : a ∈ counted rev rs) (hb : b ∈ counted rev rs)
247 (hab : a.outcome ≠ b.outcome) : 1 ≤ D rev rs := by
248 have := disagreement_K_lt_N rev rs a b ha hb hab
249 unfold D
250 omega
251
252/-! ### 6. mono: appending results never decreases N -/
253
254omit [DecidableEq Out] in
255/-- The counted list of a prefix of the arrivals is a prefix of the counted list of the whole. -/
256theorem counted_prefix (rev : Nat) (rs more : List (Result Key Out)) :
257 counted rev rs <+: counted rev (rs ++ more) := by
258 unfold counted
259 rw [List.foldl_append]
260 exact foldl_prefix rev more _
261
262omit [DecidableEq Out] in
263theorem mono (rev : Nat) (rs more : List (Result Key Out)) :
264 N rev rs ≤ N rev (rs ++ more) :=
265 (counted_prefix rev rs more).length_le
266
267end Counting