Partially defined possibly infinite lists #
This file provides a WSeq α
type representing partially defined possibly infinite lists
(referred here as weak sequences).
Weak sequences.
While the Seq
structure allows for lists which may not be finite,
a weak sequence also allows the computation of each element to
involve an indeterminate amount of computation, including possibly
an infinite loop. This is represented as a regular Seq
interspersed
with none
elements to indicate that computation is ongoing.
This model is appropriate for Haskell style lazy lists, and is closed under most interesting computation patterns on infinite lists, but conversely it is difficult to extract elements from it.
Instances For
Turn a sequence into a weak sequence
Instances For
Turn a list into a weak sequence
Instances For
Turn a stream into a weak sequence
Instances For
Prepend an element to a weak sequence
Instances For
Compute for one tick, without producing any elements
Instances For
Destruct a weak sequence, to (eventually possibly) produce either
none
for nil
or some (a, s)
if an element is produced.
Instances For
Recursion principle for weak sequences, compare with List.recOn
.
Instances For
membership for weak sequences
Instances For
Get the head of a weak sequence. This involves a possibly infinite computation.
Instances For
Encode a computation yielding a weak sequence into additional
think
constructors in a weak sequence
Instances For
Get the tail of a weak sequence. This doesn't need a Computation
wrapper, unlike head
, because flatten
allows us to hide this
in the construction of the weak sequence itself.
Instances For
drop the first n
elements from s
.
Equations
- Stream'.WSeq.drop s 0 = s
- Stream'.WSeq.drop s (Nat.succ n) = Stream'.WSeq.tail (Stream'.WSeq.drop s n)
Instances For
Get the nth element of s
.
Instances For
Convert s
to a list (if it is finite and completes in finite time).
Instances For
Get the length of s
(if it is finite and completes in finite time).
Instances For
- out : Computation.Terminates (Stream'.WSeq.toList s✝)
A weak sequence is finite if toList s
terminates. Equivalently,
it is a finite number of think
and cons
applied to nil
.
Instances
Get the list corresponding to a finite weak sequence.
Instances For
- get?_terminates : ∀ (n : ℕ), Computation.Terminates (Stream'.WSeq.get? s✝ n)
A weak sequence is productive if it never stalls forever - there are
always a finite number of think
s between cons
constructors.
The sequence itself is allowed to be infinite though.
Instances
Replace the n
th element of s
with a
.
Instances For
Remove the n
th element of s
.
Instances For
Map the elements of s
over f
, removing any values that yield none
.
Instances For
Select the elements of s
that satisfy p
.
Instances For
Get the first element of s
satisfying p
.
Instances For
Zip a function over two weak sequences
Instances For
Zip two weak sequences into a single sequence of pairs
Instances For
Get the list of indexes of elements of s
satisfying p
Instances For
Get the index of the first element of s
satisfying p
Instances For
Get the index of the first occurrence of a
in s
Instances For
Get the indexes of occurrences of a
in s
Instances For
union s1 s2
is a weak sequence which interleaves s1
and s2
in
some order (nondeterministically).
Instances For
Returns true
if s
is nil
and false
if s
has an element
Instances For
Calculate one step of computation
Instances For
Get the first n
elements of a weak sequence
Instances For
Split the sequence at position n
into a finite initial segment
and the weak sequence tail
Instances For
Returns true
if any element of s
satisfies p
Instances For
Returns true
if every element of s
satisfies p
Instances For
Apply a function to the elements of the sequence to produce a sequence
of partial results. (There is no scanr
because this would require
working from the end of the sequence, which may not exist.)
Instances For
Get the weak sequence of initial segments of the input sequence
Instances For
Like take, but does not wait for a result. Calculates n
steps of
computation and returns the sequence computed so far
Instances For
Append two weak sequences. As with Seq.append
, this may not use
the second sequence if the first one takes forever to compute
Instances For
Map a function over a weak sequence
Instances For
Flatten a sequence of weak sequences. (Note that this allows
empty sequences, unlike Seq.join
.)
Instances For
Monadic bind operator for weak sequences
Instances For
lift a relation to a relation over weak sequences
Instances For
Definition of bisimilarity for weak sequences
Instances For
Two weak sequences are LiftRel R
related if they are either both empty,
or they are both nonempty and the heads are R
related and the tails are
LiftRel R
related. (This is a coinductive definition.)
Instances For
If two sequences are equivalent, then they have the same values and
the same computational behavior (i.e. if one loops forever then so does
the other), although they may differ in the number of think
s needed to
arrive at the answer.
Instances For
auxiliary definition of tail over weak sequences
Instances For
auxiliary definition of drop over weak sequences
Equations
- Stream'.WSeq.drop.aux 0 = Computation.pure
- Stream'.WSeq.drop.aux (Nat.succ n) = fun a => Stream'.WSeq.tail.aux a >>= Stream'.WSeq.drop.aux n
Instances For
Given a productive weak sequence, we can collapse all the think
s to
produce a sequence.
Instances For
The monadic return a
is a singleton list containing a
.
Instances For
auxiliary definition of destruct_append
over weak sequences
Instances For
auxiliary definition of destruct_join
over weak sequences