Skip to content

Commit 7d7a6dd

Browse files
authored
chore: move usize theorem to Batteries.Data.UInt (#1673)
1 parent 8fda52e commit 7d7a6dd

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

Batteries/Data/Array/Basic.lean

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Authors: Arthur Paulino, Floris van Doorn, Jannis Limperg
55
-/
66
module
77
import Batteries.Tactic.Alias
8+
import Batteries.Data.UInt
89

910
@[expose] public section
1011

@@ -130,12 +131,6 @@ May be asserted to be true in an unsafe context via `Array.unsafe_sizeFitsUsize`
130131
-/
131132
private abbrev SizeFitsUSize (a : Array α) : Prop := a.size < USize.size
132133

133-
@[grind .]
134-
private theorem SizeFitsUSize.toNat_ofNat_eq {n : Nat} {a : Array α}
135-
(h : a.SizeFitsUSize) (hn : n ≤ a.size) :
136-
(USize.ofNat n).toNat = n :=
137-
USize.toNat_ofNat_of_lt' (Nat.lt_of_le_of_lt ‹_› ‹_›)
138-
139134
/-
140135
This is guaranteed by the Array docs but it is unprovable.
141136
Can be used in unsafe functions to write more efficient implementations
@@ -214,13 +209,13 @@ private theorem scanlM_loop_eq_scanlMFast_loop [Monad m]
214209
{h_stop : stop ≤ as.size} {acc : Array β} :
215210
scanlM.loop f init as start stop h_stop acc
216211
= scanlMFast.loop f init as (USize.ofNat start) (USize.ofNat stop)
217-
(by rw [USize.toNat_ofNat_of_lt' (Nat.lt_of_le_of_lt h_stop h_size)]; exact h_stop) acc := by
212+
(by rw [USize.toNat_ofNat_of_le_of_lt h_size h_stop]; exact h_stop) acc := by
218213
generalize h_n : stop - start = n
219214
induction n using Nat.strongRecOn generalizing start acc init
220215
rename_i n ih
221216
rw [scanlM.loop, scanlMFast.loop]
222-
have h_stop_usize := h_size.toNat_ofNat_eq h_stop
223-
have h_start_usize := h_size.toNat_ofNat_eq h_start
217+
have h_stop_usize := USize.toNat_ofNat_of_le_of_lt h_size h_stop
218+
have h_start_usize := USize.toNat_ofNat_of_le_of_lt h_size h_start
224219
split
225220
case isTrue h_lt =>
226221
simp_all only [USize.toNat_ofNat', ↓reduceDIte, uget,
@@ -229,7 +224,7 @@ private theorem scanlM_loop_eq_scanlMFast_loop [Monad m]
229224
intro next
230225
have h_start_succ : USize.ofNat start + 1 = USize.ofNat (start + 1) := by
231226
simp_all only [← USize.toNat_inj, USize.toNat_add]
232-
grind only [USize.size_eq, SizeFitsUSize.toNat_ofNat_eq]
227+
grind only [USize.size_eq, USize.toNat_ofNat_of_le_of_lt]
233228
rw [h_start_succ]
234229
apply ih (stop - (start + 1)) <;> omega
235230
case isFalse h_nlt => grind [USize.lt_iff_toNat_lt]
@@ -255,7 +250,7 @@ private def scanrMFast [Monad m] (f : α → β → m β) (init : β) (as : Arra
255250
(start := USize.ofNat start) (stop := USize.ofNat stop)
256251
(h_start := by grind only [USize.size_eq, USize.ofNat_eq_iff_mod_eq_toNat, = Nat.min_def])
257252
(acc := Array.replicate (start - stop + 1) init)
258-
(by grind only [!Array.size_replicate, = Nat.min_def, SizeFitsUSize.toNat_ofNat_eq])
253+
(by grind only [!Array.size_replicate, = Nat.min_def, USize.toNat_ofNat_of_le_of_lt])
259254
where
260255
@[specialize]
261256
loop (f : α → β → m β) (init : β) (as : Array α)

Batteries/Data/UInt.lean

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,16 @@ theorem UInt32.toUSize_toNat (x : UInt32) : x.toUSize.toNat = x.toNat := by simp
209209
instance : Std.LawfulOrd USize :=
210210
Std.LawfulCmp.compareOfLessAndEq_of_irrefl_of_trans_of_not_lt_of_antisymm
211211
(fun _ => Nat.lt_irrefl _) Nat.lt_trans Nat.not_lt USize.le_antisymm
212+
213+
theorem USize.toNat_ofNat_of_le_of_lt (h : n < USize.size) (hn : i ≤ n) :
214+
(USize.ofNat i).toNat = i :=
215+
USize.toNat_ofNat_of_lt' (Nat.lt_of_le_of_lt ‹_› ‹_›)
216+
217+
theorem USize.pred_toNat {i : USize} (h_gt : 0 < i) :
218+
(i - 1).toNat = i.toNat - 1 := by
219+
have h_gt_nat := USize.lt_iff_toNat_lt.mp h_gt
220+
have h_bound := i.toNat_lt_size
221+
cases System.Platform.numBits_eq
222+
all_goals
223+
simp_all only [USize.size, USize.toNat_sub, USize.reduceToNat]
224+
omega

0 commit comments

Comments
 (0)