Skip to content

Commit 6337973

Browse files
sgraf812arthur-adjedj
authored andcommitted
feat: introduce List.Cursor.pos as an abbreviation for prefix.length (leanprover#10642)
This PR introduces `List.Cursor.pos` as an abbreviation for `prefix.length`.
1 parent 8002ec4 commit 6337973

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/Std/Do/Triple/SpecLemmas.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ def Cursor.tail (s : Cursor l) (h : 0 < s.suffix.length := by get_elem_tactic) :
5959
(Cursor.at l n).tail (by simpa using Nat.sub_lt_sub_right (Nat.le_refl n) h) = Cursor.at l (n + 1) := by
6060
simp [Cursor.tail, Cursor.at, Cursor.current]
6161

62+
/--
63+
The position of the cursor in the list.
64+
It's a shortcut for the number of elements in the prefix.
65+
-/
66+
abbrev Cursor.pos (c : Cursor l) : Nat := c.prefix.length
67+
68+
@[simp, grind =]
69+
theorem Cursor.pos_at {l : List α} {n : Nat} (h : n < l.length) :
70+
(Cursor.at l n).pos = n := by simp only [pos, «at», length_take]; omega
71+
72+
@[simp]
73+
theorem Cursor.pos_mk {l pre suff : List α} (h : pre ++ suff = l) :
74+
(Cursor.mk pre suff h).pos = pre.length := rfl
75+
6276
@[grind →]
6377
theorem eq_of_range'_eq_append_cons (h : range' s n step = xs ++ cur :: ys) :
6478
cur = s + step * xs.length := by

tests/lean/run/bhaviksSampler.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ theorem sampler_correct {m : Type → Type u} {k h} [Monad m] [WPMonad m ps] :
152152
sampler (m:=m) n k h
153153
⦃⇓ xs => ⌜xs.toList.Nodup⌝⦄ := by
154154
mvcgen -leave [sampler]
155-
case inv1 => exact (⇓ (xs, midway) => ⌜Midway.valid midway xs.prefix.length⌝)
155+
case inv1 => exact (⇓ (xs, midway) => ⌜Midway.valid midway xs.pos⌝)
156156
case vc1 pref cur _ _ _ _ _ _ r _ _ _ =>
157157
dsimp
158158
mframe

tests/lean/run/doLogicTests.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ theorem fib_triple : ⦃⌜True⌝⦄ fib_impl n ⦃⇓ r => ⌜r = fib_spec n
235235
if h : n = 0 then simp [h] else
236236
simp only [h, reduceIte]
237237
mspec -- Spec.pure
238-
mspec Spec.forIn_range (⇓ ⟨xs, a, b⟩ => ⌜a = fib_spec xs.prefix.length ∧ b = fib_spec (xs.prefix.length + 1)⌝) ?step
238+
mspec Spec.forIn_range (⇓ ⟨xs, a, b⟩ => ⌜a = fib_spec xs.pos ∧ b = fib_spec (xs.pos + 1)⌝) ?step
239239
case step => intros; mintro _; simp_all
240240
simp_all [Nat.sub_one_add_one]
241241

@@ -246,7 +246,7 @@ theorem fib_triple_cases : ⦃⌜True⌝⦄ fib_impl n ⦃⇓ r => ⌜r = fib_sp
246246
mintro -
247247
simp only [fib_impl, h, reduceIte]
248248
mspec
249-
mspec Spec.forIn_range (⇓ ⟨xs, a, b⟩ => ⌜a = fib_spec xs.prefix.length ∧ b = fib_spec (xs.prefix.length + 1)⌝) ?step
249+
mspec Spec.forIn_range (⇓ ⟨xs, a, b⟩ => ⌜a = fib_spec xs.pos ∧ b = fib_spec (xs.pos + 1)⌝) ?step
250250
case step => intros; mintro _; mspec; mspec; simp_all
251251
simp_all [Nat.sub_one_add_one]
252252

@@ -279,7 +279,7 @@ theorem fib_impl_vcs
279279

280280
theorem fib_triple_vcs : ⦃⌜True⌝⦄ fib_impl n ⦃⇓ r => ⌜r = fib_spec n⌝⦄ := by
281281
apply fib_impl_vcs
282-
case I => intro n hn; exact (⇓ ⟨xs, a, b⟩ => ⌜a = fib_spec xs.prefix.length ∧ b = fib_spec (xs.prefix.length + 1)⌝)
282+
case I => intro n hn; exact (⇓ ⟨xs, a, b⟩ => ⌜a = fib_spec xs.pos ∧ b = fib_spec (xs.pos + 1)⌝)
283283
case ret => mpure_intro; rfl
284284
case loop_pre => intros; mpure_intro; trivial
285285
case loop_post => simp_all [Nat.sub_one_add_one]
@@ -365,14 +365,14 @@ theorem fib_triple : ⦃⌜True⌝⦄ fib_impl n ⦃⇓ r => ⌜r = fib_spec n
365365
unfold fib_impl
366366
mvcgen
367367
case inv1 => exact ⇓ (xs, ⟨a, b⟩) =>
368-
⌜a = fib_spec xs.prefix.length ∧ b = fib_spec (xs.prefix.length + 1)⌝
368+
⌜a = fib_spec xs.pos ∧ b = fib_spec (xs.pos + 1)⌝
369369
all_goals simp_all +zetaDelta [Nat.sub_one_add_one]
370370

371371
theorem fib_triple_step : ⦃⌜True⌝⦄ fib_impl n ⦃⇓ r => ⌜r = fib_spec n⌝⦄ := by
372372
unfold fib_impl
373373
mvcgen (stepLimit := some 14) -- 13 still has a wp⟦·⟧
374374
case inv1 => exact ⇓ ⟨xs, a, b⟩ =>
375-
⌜a = fib_spec xs.prefix.length ∧ b = fib_spec (xs.prefix.length + 1)⌝
375+
⌜a = fib_spec xs.pos ∧ b = fib_spec (xs.pos + 1)⌝
376376
all_goals simp_all +zetaDelta [Nat.sub_one_add_one]
377377

378378
attribute [local spec] fib_triple in
@@ -674,7 +674,7 @@ def max_and_sum (xs : Array Nat) : Id (Nat × Nat) := do
674674
theorem max_and_sum_spec (xs : Array Nat) :
675675
⦃⌜∀ i, (h : i < xs.size) → xs[i] ≥ 0⌝⦄ max_and_sum xs ⦃⇓ (m, s) => ⌜s ≤ m * xs.size⌝⦄ := by
676676
mvcgen [max_and_sum]
677-
case inv1 => exact (⇓ ⟨xs, m, s⟩ => ⌜s ≤ m * xs.prefix.length⌝)
677+
case inv1 => exact (⇓ ⟨xs, m, s⟩ => ⌜s ≤ m * xs.pos⌝)
678678
all_goals simp_all
679679
· rw [Nat.left_distrib]
680680
simp +zetaDelta only [Nat.mul_one, Nat.add_le_add_iff_right]
@@ -821,14 +821,14 @@ theorem naive_expo_correct (x n : Nat) : naive_expo x n = x^n := by
821821
generalize h : naive_expo x n = r
822822
apply Id.of_wp_run_eq h
823823
mvcgen
824-
case inv1 => exact ⇓⟨xs, r⟩ => ⌜r = x^xs.prefix.length
824+
case inv1 => exact ⇓⟨xs, r⟩ => ⌜r = x^xs.pos
825825
all_goals simp_all [Nat.pow_add_one]
826826

827827
theorem fast_expo_correct (x n : Nat) : fast_expo x n = x^n := by
828828
generalize h : fast_expo x n = r
829829
apply Id.of_wp_run_eq h
830830
mvcgen
831-
case inv1 => exact ⇓⟨xs, e, x', y⟩ => ⌜x' ^ e * y = x ^ n ∧ e ≤ n - xs.prefix.length
831+
case inv1 => exact ⇓⟨xs, e, x', y⟩ => ⌜x' ^ e * y = x ^ n ∧ e ≤ n - xs.pos
832832
all_goals simp_all
833833
case vc1 b _ _ _ _ _ _ ih =>
834834
obtain ⟨e, y, x'⟩ := b

0 commit comments

Comments
 (0)