Source/Packages

Std.List

packages/foundation/standard/src/Std/List.alpha

317 lines21 declarations13.5 KiBSHA-256 0d04bb5f90d2

def · lines 248–272

stdListIndex

Full file
The element at a position, as an option: an index past the end is absent, never an error and never a wrong element.
248def stdListIndex =
249  (lambda erased element : Type 0 .
250    unrestricted values : (family StdList element) .
251    unrestricted position : Nat .
252    app
253      (eliminate
254        StdList
255        (lambda unrestricted current : (family StdList element) .
256          pi unrestricted remaining : Nat . family StdOption element)
257        values
258        (branch StdListEmpty .
259          lambda unrestricted remaining : Nat . constructor StdOption StdNone element)
260        (branch StdListCons head tail induction .
261          lambda unrestricted remaining : Nat .
262          nat-eliminate
263            (lambda unrestricted current : Nat . family StdOption element)
264            (constructor StdOption StdSome element head)
265            (lambda unrestricted predecessor : Nat .
266              unrestricted inner : (family StdOption element) .
267              induction (nat-subtract remaining 1))
268            -- a branch on zero, not a recursion on `remaining`: a
269            -- strict evaluator would evaluate every smaller position's
270            -- lookup, doubling the cost with each position
271            (nat-less-than 0 remaining)))
272      position)

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.