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 \<Rightarrow> []
| Some (x, xq) \<Rightarrow> x # list_of_lazy_sequence xq)"
lemma list_of_Lazy_Sequence [simp]: "list_of_lazy_sequence (Lazy_Sequence f) = (case f () of
None \<Rightarrow> []
| Some (x, xq) \<Rightarrow> 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
[] \<Rightarrow> None
| x # xs \<Rightarrow> 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 (\<lambda>x. curry h x \<circ> 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) \<Rightarrow> True
| (Some (x, xq'), Some (y, yq')) \<Rightarrow> HOL.equal x y \<and> HOL.equal xq yq
| _ \<Rightarrow> 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 \<Rightarrow> 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 (\<lambda>_. 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 \<Rightarrow> single ()
| Some ((), xq) \<Rightarrow> empty)"
ML \<open> 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\<open>maybe optimise this expression -> append (single x) xs == cons x xs
Performance difference?\<close>
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))) (\<lambda>(x, xs). single (x # xs)) else empty)"
instance ..
end
subsection \<open>With Hit Bound Value\<close> text\<open>assuming in negative context\<close>
¤ 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.0.15Bemerkung:
(vorverarbeitet)
¤
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 ist noch experimentell.