lemma case_lazy_sequence [simp]: "case_lazy_sequence f xq = f (list_of_lazy_sequence xq)" by (cases xq) auto
lemma rec_lazy_sequence [simp]: "rec_lazy_sequence f xq = f (list_of_lazy_sequence xq)" by (cases xq) auto
definition Lazy_Sequence :: "(unit \ ('a \ 'a lazy_sequence) option) \ 'a lazy_sequence" where "Lazy_Sequence f = lazy_sequence_of_list (case f () of
None ==> []
| Some (x, xq) ==> x # list_of_lazy_sequence xq)"
lemma list_of_Lazy_Sequence [simp]: "list_of_lazy_sequence (Lazy_Sequence f) = (case f () of
None ==> []
| Some (x, xq) ==> x # list_of_lazy_sequence xq)" by (simp add: Lazy_Sequence_def)
definition yield :: "'a lazy_sequence \ ('a \ 'a lazy_sequence) option" where "yield xq = (case list_of_lazy_sequence xq of
[] ==> None
| x # xs ==> Some (x, lazy_sequence_of_list xs))"
lemma yield_Seq [simp, code]: "yield (Lazy_Sequence f) = f ()" by (cases "f ()") (simp_all add: yield_def split_def)
lemma case_yield_eq [simp]: "case_option g h (yield xq) =
case_list g (λx. curry h x ∘ lazy_sequence_of_list) (list_of_lazy_sequence xq)" by (cases "list_of_lazy_sequence xq") (simp_all add: yield_def)
lemma equal_lazy_sequence_code [code]: "HOL.equal xq yq = (case (yield xq, yield yq) of
(None, None) ==> True
| (Some (x, xq'), Some (y, yq')) ==> HOL.equal x y ∧ HOL.equal xq yq
| _ ==> False)" by (simp_all add: lazy_sequence_eq_iff equal_eq split: list.splits)
lemma [code nbe]: "HOL.equal (x :: 'a lazy_sequence) x \ True" by (fact equal_refl)
definition empty :: "'a lazy_sequence" where "empty = lazy_sequence_of_list []"
lemma flat_code [code]: "flat xqq = Lazy_Sequence (\_. case yield xqq of
None ==> None
| Some (xq, xqq') \ yield (append xq (flat xqq')))" by (simp add: lazy_sequence_eq_iff split: list.splits)
definition bind :: "'a lazy_sequence \ ('a \ 'b lazy_sequence) \ 'b lazy_sequence" where "bind xq f = flat (map f xq)"
definition if_seq :: "bool \ unit lazy_sequence" where "if_seq b = (if b then single () else empty)"
definition those :: "'a option lazy_sequence \ 'a lazy_sequence option" where "those xq = map_option lazy_sequence_of_list (List.those (list_of_lazy_sequence xq))"
function iterate_upto :: "(natural \ 'a) \ natural \ natural \ 'a lazy_sequence" where "iterate_upto f n m =
Lazy_Sequence (λ_. if n > m then None else Some (f n, iterate_upto f (n + 1) m))" by pat_completeness auto
terminationby (relation "measure (\(f, n, m). nat_of_natural (m + 1 - n))")
(auto simp add: less_natural_def)
definition not_seq :: "unit lazy_sequence \ unit lazy_sequence" where "not_seq xq = (case yield xq of
None ==> single ()
| Some ((), xq) ==> empty)"
ML ‹ signature LAZY_SEQUENCE =
sig datatype'a lazy_sequence = Lazy_Sequence of (unit -> ('a * 'a Lazy_Sequence.lazy_sequence) option)
val map: ('a -> 'b) -> 'a lazy_sequence -> 'b lazy_sequence
val yield: 'a lazy_sequence -> ('a * 'a lazy_sequence) option
val yieldn: int -> 'a lazy_sequence -> 'a list * 'a lazy_sequence end;
class small_lazy = fixes small_lazy :: "natural \ 'a lazy_sequence"
instantiation unit :: small_lazy begin
definition"small_lazy d = single ()"
instance ..
end
instantiation int :: small_lazy begin
text‹maybe optimise this expression -> append (single x) xs == cons x xs
Performance difference?›
function small_lazy' :: "int \ int \ int lazy_sequence" where "small_lazy' d i = (if d < i then empty
else append (single i) (small_lazy' d (i + 1)))" by pat_completeness auto
termination by (relation "measure (%(d, i). nat (d + 1 - i))") auto
instantiation prod :: (small_lazy, small_lazy) small_lazy begin
definition "small_lazy d = product (small_lazy d) (small_lazy d)"
instance ..
end
instantiation list :: (small_lazy) small_lazy begin
fun small_lazy_list :: "natural \ 'a list lazy_sequence" where "small_lazy_list d = append (single [])
(if d > 0 then bind (product (small_lazy (d - 1))
(small_lazy (d - 1))) (λ(x, xs). single (x # xs)) else empty)"
instance ..
end
subsection‹With Hit Bound Value› text‹assuming in negative context›
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.