Lazy lists #
The type LazyList α
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 LazyList α
is isomorphic to List α
.)
The singleton lazy list.
Equations
- LazyList.singleton x = let a := x; LazyList.cons a (Thunk.pure LazyList.nil)
Instances For
Constructs a lazy list from a list.
Equations
- LazyList.ofList [] = LazyList.nil
- LazyList.ofList (h :: t) = LazyList.cons h { fn := fun (x : Unit) => LazyList.ofList t }
Instances For
Converts a lazy list to a list. If the lazy list is infinite, then this function does not terminate.
Equations
- LazyList.nil.toList = []
- (LazyList.cons h t).toList = h :: t.get.toList
Instances For
Returns the first element of the lazy list,
or default
if the lazy list is empty.
Equations
- x.headI = match x with | LazyList.nil => default | LazyList.cons h tl => h
Instances For
Removes the first element of the lazy list.
Equations
- x.tail = match x with | LazyList.nil => LazyList.nil | LazyList.cons hd t => t.get
Instances For
Appends two lazy lists.
Equations
- LazyList.nil.append x = x.get
- (LazyList.cons h t).append x = LazyList.cons h { fn := fun (x_1 : Unit) => t.get.append x }
Instances For
Maps a function over a lazy list.
Equations
- LazyList.map f LazyList.nil = LazyList.nil
- LazyList.map f (LazyList.cons h t) = LazyList.cons (f h) { fn := fun (x : Unit) => LazyList.map f t.get }
Instances For
Maps a binary function over two lazy list.
Like LazyList.zip
, the result is only as long as the smaller input.
Equations
- LazyList.map₂ f LazyList.nil x = LazyList.nil
- LazyList.map₂ f x LazyList.nil = LazyList.nil
- LazyList.map₂ f (LazyList.cons h₁ t₁) (LazyList.cons h₂ t₂) = LazyList.cons (f h₁ h₂) { fn := fun (x : Unit) => LazyList.map₂ f t₁.get t₂.get }
Instances For
The monadic join operation for lazy lists.
Equations
- LazyList.nil.join = LazyList.nil
- (LazyList.cons h t).join = h.append { fn := fun (x : Unit) => t.get.join }
Instances For
The list containing the first n
elements of a lazy list.
Equations
- LazyList.take 0 x = []
- LazyList.take x LazyList.nil = []
- LazyList.take a.succ (LazyList.cons h t) = h :: LazyList.take a t.get
Instances For
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
- LazyList.filter p LazyList.nil = LazyList.nil
- LazyList.filter p (LazyList.cons h t) = if p h then LazyList.cons h { fn := fun (x : Unit) => LazyList.filter p t.get } else LazyList.filter p t.get
Instances For
The nth element of a lazy list as an option (like List.get?
).
Equations
- LazyList.nil.get? x = none
- (LazyList.cons a tl).get? 0 = some a
- (LazyList.cons hd l).get? n.succ = l.get.get? n
Instances For
The infinite lazy list [x, f x, f (f x), ...]
of iterates of a function.
This definition is partial because it creates an infinite list.
The infinite lazy list [i, i+1, i+2, ...]