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.
56 lines
1.5 KiB
Text
56 lines
1.5 KiB
Text
import Std.Data.Iterators.Producers.Range
|
|
import Std.Data.Iterators.Combinators.StepSize
|
|
|
|
inductive Tree
|
|
| nil
|
|
| node (l r : Tree)
|
|
instance : Inhabited Tree := ⟨.nil⟩
|
|
|
|
-- This function has an extra argument to suppress the
|
|
-- common sub-expression elimination optimization
|
|
partial def make' (n d : UInt32) : Tree :=
|
|
if d = 0 then .node .nil .nil
|
|
else .node (make' n (d - 1)) (make' (n + 1) (d - 1))
|
|
|
|
-- build a tree
|
|
def make (d : UInt32) := make' d d
|
|
|
|
def check : Tree → UInt32
|
|
| .nil => 0
|
|
| .node l r => 1 + check l + check r
|
|
|
|
def minN := 4
|
|
|
|
def out (s : String) (n : Nat) (t : UInt32) : IO Unit :=
|
|
IO.println s!"{s} of depth {n}\t check: {t}"
|
|
|
|
-- allocate and check lots of trees
|
|
partial def sumT (d i t : UInt32) : UInt32 :=
|
|
if i = 0 then t
|
|
else
|
|
let a := check (make d)
|
|
sumT d (i-1) (t + a)
|
|
|
|
def main : List String → IO UInt32
|
|
| [s] => do
|
|
let n := s.toNat!
|
|
let maxN := Nat.max (minN + 2) n
|
|
let stretchN := maxN + 1
|
|
|
|
-- stretch memory tree
|
|
let c := check (make $ UInt32.ofNat stretchN)
|
|
out "stretch tree" stretchN c
|
|
|
|
-- allocate a long lived tree
|
|
let long := make $ UInt32.ofNat maxN
|
|
|
|
-- allocate, walk, and deallocate many bottom-up binary trees
|
|
for d in (minN...=maxN).iter.stepSize 2 do
|
|
let n := 2 ^ (maxN - d + minN)
|
|
let i := sumT (.ofNat d) (.ofNat n) 0
|
|
out s!"{n}\t trees" d i
|
|
|
|
-- confirm the long-lived binary tree still exists
|
|
out "long lived tree" maxN (check long)
|
|
return 0
|
|
| _ => return 1
|