Lazy lists #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
The type lazy_list α
is a lazy list with elements of type α
.
In the VM, these are potentially infinite lists
where all elements after the first are computed on-demand.
(This is only useful for execution in the VM,
logically we can prove that lazy_list α
is isomorphic to list α
.)
Lazy list. All elements (except the first) are computed lazily.
Instances for lazy_list
Equations
The singleton lazy list.
Equations
- lazy_list.singleton a = lazy_list.cons a (λ («_» : unit), lazy_list.nil)
Constructs a lazy list from a list.
Equations
- lazy_list.of_list (h :: t) = lazy_list.cons h (λ («_» : unit), lazy_list.of_list t)
- lazy_list.of_list list.nil = lazy_list.nil
Converts a lazy list to a list. If the lazy list is infinite, then this function does not terminate.
Equations
- (lazy_list.cons h t).to_list = h :: (t unit.star()).to_list
- lazy_list.nil.to_list = list.nil
Returns the first element of the lazy list,
or default
if the lazy list is empty.
Equations
- (lazy_list.cons h t).head = h
- lazy_list.nil.head = inhabited.default
Removes the first element of the lazy list.
Equations
- (lazy_list.cons h t).tail = t unit.star()
- lazy_list.nil.tail = lazy_list.nil
Appends two lazy lists.
Equations
- (lazy_list.cons h t).append l = lazy_list.cons h (λ («_» : unit), (t unit.star()).append l)
- lazy_list.nil.append l = l unit.star()
Maps a function over a lazy list.
Equations
- lazy_list.map f (lazy_list.cons h t) = lazy_list.cons (f h) (λ («_» : unit), lazy_list.map f (t unit.star()))
- lazy_list.map f lazy_list.nil = lazy_list.nil
Maps a binary function over two lazy list.
Like lazy_list.zip
, the result is only as long as the smaller input.
Equations
- lazy_list.map₂ f (lazy_list.cons h₁ t₁) (lazy_list.cons h₂ t₂) = lazy_list.cons (f h₁ h₂) (λ («_» : unit), lazy_list.map₂ f (t₁ unit.star()) (t₂ unit.star()))
- lazy_list.map₂ f (lazy_list.cons hd tl) lazy_list.nil = lazy_list.nil
- lazy_list.map₂ f lazy_list.nil (lazy_list.cons hd tl) = lazy_list.nil
- lazy_list.map₂ f lazy_list.nil lazy_list.nil = lazy_list.nil
Zips two lazy lists.
Equations
The monadic join operation for lazy lists.
Equations
- (lazy_list.cons h t).join = h.append (λ («_» : unit), (t unit.star()).join)
- lazy_list.nil.join = lazy_list.nil
Maps a function over a lazy list.
Same as lazy_list.map
, but with swapped arguments.
Equations
- l.for f = lazy_list.map f l
The list containing the first n
elements of a lazy list.
Equations
- lazy_list.approx (a + 1) (lazy_list.cons h t) = h :: lazy_list.approx a (t unit.star())
- lazy_list.approx n.succ lazy_list.nil = list.nil
- lazy_list.approx 0 (lazy_list.cons hd tl) = list.nil
- lazy_list.approx 0 lazy_list.nil = list.nil
The lazy list of all elements satisfying the predicate. If the lazy list is infinite and none of the elements satisfy the predicate, then this function will not terminate.
Equations
- lazy_list.filter p (lazy_list.cons h t) = ite (p h) (lazy_list.cons h (λ («_» : unit), lazy_list.filter p (t unit.star()))) (lazy_list.filter p (t unit.star()))
- lazy_list.filter p lazy_list.nil = lazy_list.nil
The nth element of a lazy list as an option (like list.nth
).
Equations
- (lazy_list.cons a l).nth (n + 1) = (l unit.star()).nth n
- (lazy_list.cons a l).nth 0 = option.some a
- lazy_list.nil.nth n = option.none