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.
35 lines
655 B
Text
35 lines
655 B
Text
import Lean.Data.PersistentHashSet
|
||
|
||
open Lean
|
||
|
||
def sum (s : PHashSet Nat) : Nat := Id.run do
|
||
let mut r := 0
|
||
for a in s do
|
||
r := r + a
|
||
return r
|
||
|
||
def sumIf (s : PHashSet Nat) (p : Nat → Bool) : Nat := Id.run do
|
||
let mut r := 0
|
||
for a in s do
|
||
unless p a do
|
||
continue
|
||
r := r + a
|
||
return r
|
||
|
||
def mk [Hashable α] [BEq α] (f : Nat → α) (n : Nat) : PHashSet α := Id.run do
|
||
let mut s := {}
|
||
for i in *...n do
|
||
s := s.insert (f i)
|
||
return s
|
||
|
||
/-- info: 45 -/
|
||
#guard_msgs in
|
||
#eval sum (mk id 10)
|
||
|
||
/-- info: 9900 -/
|
||
#guard_msgs in
|
||
#eval sum (mk (2*·) 100)
|
||
|
||
/-- info: 2450 -/
|
||
#guard_msgs in
|
||
#eval sumIf (mk id 100) (· % 2 == 0)
|