lean4-htt/tests/lean/run/sarray.lean
Paul Reichert 98e4b2882f
refactor: migrate to new ranges (#8841)
This PR migrates usages of `Std.Range` to the new polymorphic ranges.

This PR unfortunately increases the transitive imports for
frequently-used parts of `Init` because the ranges now rely on iterators
in order to provide their functionality for types other than `Nat`.
However, iteration over ranges in compiled code is as efficient as
before in the examples I checked. This is because of a special
`IteratorLoop` implementation provided in the PR for this purpose.

There were two issues that were uncovered during migration:

* In `IndPredBelow.lean`, migrating the last remaining range causes
`compilerTest1.lean` to break. I have minimized the issue and came to
the conclusion it's a compiler bug. Therefore, I have not replaced said
old range usage yet (see #9186).
* In `BRecOn.lean`, we are publicly importing the ranges. Making this
import private should theoretically work, but there seems to be a
problem with the module system, causing the build to panic later in
`Init.Data.Grind.Poly` (see #9185).
* In `FuzzyMatching.lean`, inlining fails with the new ranges, which
would have led to significant slowdown. Therefore, I have not migrated
this file either.
2025-07-07 12:41:53 +00:00

43 lines
1,022 B
Text

def mkByteArray (n : Nat) : ByteArray := Id.run <| do
let mut r := {}
for i in *...n do
r := r.push (UInt8.ofNat i)
return r
def tst1 (n : Nat) (expected : UInt32) : IO Unit := do
let bs := mkByteArray n
let sum := bs.foldl (init := 0) fun s b => s + b.toUInt32
assert! sum == expected
IO.println sum
/-- info: 4950 -/
#guard_msgs in
#eval tst1 100 4950
def tst2 (n : Nat) (expected : UInt32) : IO Unit := do
let bs := mkByteArray n
let mut sum := 0
for b in bs do
sum := sum + b.toUInt32
assert! sum == expected
IO.println sum
/-- info: 4950 -/
#guard_msgs in
#eval tst2 100 4950
def tst3 (n : Nat) (expected : UInt32) : IO Unit := do
let bs := mkByteArray n
let mut sum := 0
for i in *...bs.size do
sum := sum + bs[i]!.toUInt32
assert! sum == expected
IO.println sum
/-- info: 4950 -/
#guard_msgs in
#eval tst3 100 4950
set_option trace.compiler.ir.result true in
def computeByteHash (bytes : ByteArray) :=
bytes.foldl (init := 1723) fun h b => mixHash h (hash b)