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.
Turn a sequence into a weak sequence
Equations
Turn a list into a weak sequence
Equations
Turn a stream into a weak sequence
Equations
Equations
- wseq.coe_seq = {coe := wseq.of_seq α}
Equations
- wseq.coe_list = {coe := wseq.of_list α}
Equations
- wseq.coe_stream = {coe := wseq.of_stream α}
Equations
- wseq.inhabited = {default := wseq.nil α}
Prepend an element to a weak sequence
Equations
- wseq.cons a = seq.cons (option.some a)
Compute for one tick, without producing any elements
Equations
Destruct a weak sequence, to (eventually possibly) produce either
none
for nil
or some (a, s)
if an element is produced.
Equations
- wseq.destruct = computation.corec (λ (s : wseq α), wseq.destruct._match_1 (seq.destruct s))
- wseq.destruct._match_1 (option.some (option.some a, s')) = sum.inl (option.some (a, s'))
- wseq.destruct._match_1 (option.some (option.none α, s')) = sum.inr s'
- wseq.destruct._match_1 option.none = sum.inl option.none
Equations
- wseq.has_mem = {mem := wseq.mem α}
Get the head of a weak sequence. This involves a possibly infinite computation.
Equations
Encode a computation yielding a weak sequence into additional
think
constructors in a weak sequence
Equations
- wseq.flatten = seq.corec (λ (c : computation (wseq α)), wseq.flatten._match_1 c.destruct)
- wseq.flatten._match_1 (sum.inr c') = option.some (option.none α, c')
- wseq.flatten._match_1 (sum.inl s) = seq.omap return (seq.destruct s)
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.
Convert s
to a list (if it is finite and completes in finite time).
Equations
- s.to_list = computation.corec (λ (_x : list α × wseq α), wseq.to_list._match_2 _x) (list.nil α, s)
- wseq.to_list._match_2 (l, s) = wseq.to_list._match_1 l (seq.destruct s)
- wseq.to_list._match_1 l (option.some (option.some a, s')) = sum.inr (a :: l, s')
- wseq.to_list._match_1 l (option.some (option.none α, s')) = sum.inr (l, s')
- wseq.to_list._match_1 l option.none = sum.inl l.reverse
Get the length of s
(if it is finite and completes in finite time).
Equations
- s.length = computation.corec (λ (_x : ℕ × wseq α), wseq.length._match_2 _x) (0, s)
- wseq.length._match_2 (n, s) = wseq.length._match_1 n (seq.destruct s)
- wseq.length._match_1 n (option.some (option.some a, s')) = sum.inr (n + 1, s')
- wseq.length._match_1 n (option.some (option.none α, s')) = sum.inr (n, s')
- wseq.length._match_1 n option.none = sum.inl n
A weak sequence is finite if to_list s
terminates. Equivalently,
it is a finite number of think
and cons
applied to nil
.
Equations
- s.is_finite = s.to_list.terminates
Equations
- _ = h
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.
Equations
- s.productive = ∀ (n : ℕ), (s.nth n).terminates
Equations
- _ = h
Equations
- _ = _
Replace the n
th element of s
with a
.
Equations
- s.update_nth n a = seq.corec (λ (_x : ℕ × wseq α), wseq.update_nth._match_2 a _x) (n + 1, s)
- wseq.update_nth._match_2 a (n, s) = wseq.update_nth._match_1 a (seq.destruct s) n
- wseq.update_nth._match_1 a (option.some (option.some a', s')) (n + 2) = option.some (option.some a', n + 1, s')
- wseq.update_nth._match_1 a (option.some (option.some a', s')) 1 = option.some (option.some a, 0, s')
- wseq.update_nth._match_1 a (option.some (option.some a', s')) 0 = option.some (option.some a', 0, s')
- wseq.update_nth._match_1 a (option.some (option.none α, s')) n = option.some (option.none α, n, s')
- wseq.update_nth._match_1 a option.none n = option.none
Remove the n
th element of s
.
Equations
- s.remove_nth n = seq.corec (λ (_x : ℕ × wseq α), wseq.remove_nth._match_2 _x) (n + 1, s)
- wseq.remove_nth._match_2 (n, s) = wseq.remove_nth._match_1 (seq.destruct s) n
- wseq.remove_nth._match_1 (option.some (option.some a', s')) (n + 2) = option.some (option.some a', n + 1, s')
- wseq.remove_nth._match_1 (option.some (option.some a', s')) 1 = option.some (option.none α, 0, s')
- wseq.remove_nth._match_1 (option.some (option.some a', s')) 0 = option.some (option.some a', 0, s')
- wseq.remove_nth._match_1 (option.some (option.none α, s')) n = option.some (option.none α, n, s')
- wseq.remove_nth._match_1 option.none n = option.none
Map the elements of s
over f
, removing any values that yield none
.
Equations
- wseq.filter_map f = seq.corec (λ (s : wseq α), wseq.filter_map._match_1 f (seq.destruct s))
- wseq.filter_map._match_1 f (option.some (option.some a, s')) = option.some (f a, s')
- wseq.filter_map._match_1 f (option.some (option.none α, s')) = option.some (option.none β, s')
- wseq.filter_map._match_1 f option.none = option.none
Select the elements of s
that satisfy p
.
Equations
- wseq.filter p = wseq.filter_map (λ (a : α), ite (p a) (option.some a) option.none)
Get the first element of s
satisfying p
.
Equations
- wseq.find p s = (wseq.filter p s).head
Zip a function over two weak sequences
Equations
- wseq.zip_with f s1 s2 = seq.corec (λ (_x : wseq α × wseq β), wseq.zip_with._match_2 f _x) (s1, s2)
- wseq.zip_with._match_2 f (s1, s2) = wseq.zip_with._match_1 f s1 s2 (seq.destruct s1) (seq.destruct s2)
- wseq.zip_with._match_1 f s1 s2 (option.some (option.some a1, s1')) (option.some (option.some a2, s2')) = option.some (option.some (f a1 a2), s1', s2')
- wseq.zip_with._match_1 f s1 s2 (option.some (option.some a1, s1')) (option.some (option.none β, s2')) = option.some (option.none γ, s1, s2')
- wseq.zip_with._match_1 f s1 s2 (option.some (option.some val, snd)) option.none = option.none
- wseq.zip_with._match_1 f s1 s2 (option.some (option.none α, s1')) (option.some (option.some a2, s2')) = option.some (option.none γ, s1', s2)
- wseq.zip_with._match_1 f s1 s2 (option.some (option.none α, s1')) (option.some (option.none β, s2')) = option.some (option.none γ, s1', s2')
- wseq.zip_with._match_1 f s1 s2 (option.some (option.none α, snd)) option.none = option.none
- wseq.zip_with._match_1 f s1 s2 option.none _x = option.none
Get the list of indexes of elements of s
satisfying p
Equations
- wseq.find_indexes p s = wseq.filter_map (λ (_x : α × ℕ), wseq.find_indexes._match_1 p _x) (s.zip ↑stream.nats)
- wseq.find_indexes._match_1 p (a, n) = ite (p a) (option.some n) option.none
Get the index of the first element of s
satisfying p
Equations
- wseq.find_index p s = (λ (o : option ℕ), o.get_or_else 0) <$> (wseq.find_indexes p s).head
Get the index of the first occurrence of a
in s
Equations
- wseq.index_of a = wseq.find_index (eq a)
Get the indexes of occurrences of a
in s
Equations
- wseq.indexes_of a = wseq.find_indexes (eq a)
union s1 s2
is a weak sequence which interleaves s1
and s2
in
some order (nondeterministically).
Equations
- s1.union s2 = seq.corec (λ (_x : wseq α × wseq α), wseq.union._match_2 _x) (s1, s2)
- wseq.union._match_2 (s1, s2) = wseq.union._match_1 (seq.destruct s1) (seq.destruct s2)
- wseq.union._match_1 (option.some (option.some a1, s1')) (option.some (option.some a2, s2')) = option.some (option.some a1, wseq.cons a2 s1', s2')
- wseq.union._match_1 (option.some (option.some a1, s1')) (option.some (option.none α, s2')) = option.some (option.some a1, s1', s2')
- wseq.union._match_1 (option.some (option.some val, s1')) option.none = option.some (option.some val, s1', wseq.nil α)
- wseq.union._match_1 (option.some (option.none α, s1')) (option.some (option.some a2, s2')) = option.some (option.some a2, s1', s2')
- wseq.union._match_1 (option.some (option.none α, s1')) (option.some (option.none α, s2')) = option.some (option.none α, s1', s2')
- wseq.union._match_1 (option.some (option.none α, s1')) option.none = option.some (option.none α, s1', wseq.nil α)
- wseq.union._match_1 option.none (option.some (a2, s2')) = option.some (a2, wseq.nil α, s2')
- wseq.union._match_1 option.none option.none = option.none
Returns tt
if s
is nil
and ff
if s
has an element
Equations
Calculate one step of computation
Equations
- s.compute = wseq.compute._match_1 s (seq.destruct s)
- wseq.compute._match_1 s (option.some (option.some val, snd)) = s
- wseq.compute._match_1 s (option.some (option.none α, s')) = s'
- wseq.compute._match_1 s option.none = s
Get the first n
elements of a weak sequence
Equations
- s.take n = seq.corec (λ (_x : ℕ × wseq α), wseq.take._match_2 _x) (n, s)
- wseq.take._match_2 (n, s) = wseq.take._match_1 n (seq.destruct s)
- wseq.take._match_1 (m + 1) (option.some (option.some a, s')) = option.some (option.some a, m, s')
- wseq.take._match_1 (m + 1) (option.some (option.none α, s')) = option.some (option.none α, m + 1, s')
- wseq.take._match_1 (m + 1) option.none = option.none
- wseq.take._match_1 0 _x = option.none
Split the sequence at position n
into a finite initial segment
and the weak sequence tail
Equations
- s.split_at n = computation.corec (λ (_x : ℕ × list α × wseq α), wseq.split_at._match_2 _x) (n, list.nil α, s)
- wseq.split_at._match_2 (n, l, s) = wseq.split_at._match_1 n l s n (seq.destruct s)
- wseq.split_at._match_1 n l s (m + 1) (option.some (option.some a, s')) = sum.inr (m, a :: l, s')
- wseq.split_at._match_1 n l s (m + 1) (option.some (option.none α, s')) = sum.inr (n, l, s')
- wseq.split_at._match_1 n l s (m + 1) option.none = sum.inl (l.reverse, s)
- wseq.split_at._match_1 n l s 0 _x = sum.inl (l.reverse, s)
Returns tt
if any element of s
satisfies p
Equations
- s.any p = computation.corec (λ (s : wseq α), wseq.any._match_1 p (seq.destruct s)) s
- wseq.any._match_1 p (option.some (option.some a, s')) = ite ↥(p a) (sum.inl bool.tt) (sum.inr s')
- wseq.any._match_1 p (option.some (option.none α, s')) = sum.inr s'
- wseq.any._match_1 p option.none = sum.inl bool.ff
Returns tt
if every element of s
satisfies p
Equations
- s.all p = computation.corec (λ (s : wseq α), wseq.all._match_1 p (seq.destruct s)) s
- wseq.all._match_1 p (option.some (option.some a, s')) = ite ↥(p a) (sum.inr s') (sum.inl bool.ff)
- wseq.all._match_1 p (option.some (option.none α, s')) = sum.inr s'
- wseq.all._match_1 p option.none = sum.inl bool.tt
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.)
Equations
- wseq.scanl f a s = wseq.cons a (seq.corec (λ (_x : α × wseq β), wseq.scanl._match_2 f _x) (a, s))
- wseq.scanl._match_2 f (a, s) = wseq.scanl._match_1 f a (seq.destruct s)
- wseq.scanl._match_1 f a (option.some (option.some b, s')) = let a' : α := f a b in option.some (option.some a', a', s')
- wseq.scanl._match_1 f a (option.some (option.none β, s')) = option.some (option.none α, a, s')
- wseq.scanl._match_1 f a option.none = option.none
Get the weak sequence of initial segments of the input sequence
Equations
- s.inits = wseq.cons list.nil (seq.corec (λ (_x : dlist α × wseq α), wseq.inits._match_2 _x) (dlist.empty α, s))
- wseq.inits._match_2 (l, s) = wseq.inits._match_1 l (seq.destruct s)
- wseq.inits._match_1 l (option.some (option.some a, s')) = let l' : dlist α := dlist.concat a l in option.some (option.some l'.to_list, l', s')
- wseq.inits._match_1 l (option.some (option.none α, s')) = option.some (option.none (list α), l, s')
- wseq.inits._match_1 l option.none = option.none
Like take, but does not wait for a result. Calculates n
steps of
computation and returns the sequence computed so far
Equations
- s.collect n = list.filter_map id (seq.take n s)
Append two weak sequences. As with seq.append
, this may not use
the second sequence if the first one takes forever to compute
Equations
Flatten a sequence of weak sequences. (Note that this allows
empty sequences, unlike seq.join
.)
Equations
- S.join = ((λ (o : option (wseq α)), wseq.join._match_1 o) <$> S).join
- wseq.join._match_1 (option.some s) = (option.none α, s)
- wseq.join._match_1 option.none = seq1.ret option.none
Equations
- wseq.lift_rel_o R C (option.some (a, s)) (option.some (b, t)) = (R a b ∧ C s t)
- wseq.lift_rel_o R C (option.some (fst, snd)) option.none = false
- wseq.lift_rel_o R C option.none (option.some val) = false
- wseq.lift_rel_o R C option.none option.none = true
Two weak sequences are lift_rel R
related if they are either both empty,
or they are both nonempty and the heads are R
related and the tails are
lift_rel R
related. (This is a coinductive definition.)
Equations
- wseq.lift_rel R s t = ∃ (C : wseq α → wseq β → Prop), C s t ∧ ∀ {s : wseq α} {t : wseq β}, C s t → computation.lift_rel (wseq.lift_rel_o R C) s.destruct t.destruct
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.
Equations
Equations
Equations
- wseq.drop.aux (n + 1) = λ (a : option (α × wseq α)), wseq.tail.aux a >>= wseq.drop.aux n
- wseq.drop.aux 0 = computation.return
Equations
- _ = _
Equations
- _ = _
Equations
- _ = _
Equations
- wseq.destruct_append.aux t (option.some (a, s)) = computation.return (option.some (a, s.append t))
- wseq.destruct_append.aux t option.none = t.destruct