138 "list_of_lazy_sequence (map f xq) = List.map f (list_of_lazy_sequence xq)" |
138 "list_of_lazy_sequence (map f xq) = List.map f (list_of_lazy_sequence xq)" |
139 by (simp add: map_def) |
139 by (simp add: map_def) |
140 |
140 |
141 lemma map_code [code]: |
141 lemma map_code [code]: |
142 "map f xq = |
142 "map f xq = |
143 Lazy_Sequence (\<lambda>_. Option.map (\<lambda>(x, xq'). (f x, map f xq')) (yield xq))" |
143 Lazy_Sequence (\<lambda>_. map_option (\<lambda>(x, xq'). (f x, map f xq')) (yield xq))" |
144 by (simp_all add: lazy_sequence_eq_iff split: list.splits) |
144 by (simp_all add: lazy_sequence_eq_iff split: list.splits) |
145 |
145 |
146 definition flat :: "'a lazy_sequence lazy_sequence \<Rightarrow> 'a lazy_sequence" |
146 definition flat :: "'a lazy_sequence lazy_sequence \<Rightarrow> 'a lazy_sequence" |
147 where |
147 where |
148 "flat xqq = lazy_sequence_of_list (concat (List.map list_of_lazy_sequence (list_of_lazy_sequence xqq)))" |
148 "flat xqq = lazy_sequence_of_list (concat (List.map list_of_lazy_sequence (list_of_lazy_sequence xqq)))" |
165 where |
165 where |
166 "if_seq b = (if b then single () else empty)" |
166 "if_seq b = (if b then single () else empty)" |
167 |
167 |
168 definition those :: "'a option lazy_sequence \<Rightarrow> 'a lazy_sequence option" |
168 definition those :: "'a option lazy_sequence \<Rightarrow> 'a lazy_sequence option" |
169 where |
169 where |
170 "those xq = Option.map lazy_sequence_of_list (List.those (list_of_lazy_sequence xq))" |
170 "those xq = map_option lazy_sequence_of_list (List.those (list_of_lazy_sequence xq))" |
171 |
171 |
172 function iterate_upto :: "(natural \<Rightarrow> 'a) \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> 'a lazy_sequence" |
172 function iterate_upto :: "(natural \<Rightarrow> 'a) \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> 'a lazy_sequence" |
173 where |
173 where |
174 "iterate_upto f n m = |
174 "iterate_upto f n m = |
175 Lazy_Sequence (\<lambda>_. if n > m then None else Some (f n, iterate_upto f (n + 1) m))" |
175 Lazy_Sequence (\<lambda>_. if n > m then None else Some (f n, iterate_upto f (n + 1) m))" |
299 where |
299 where |
300 "hb_single x = Lazy_Sequence (\<lambda>_. Some (Some x, empty))" |
300 "hb_single x = Lazy_Sequence (\<lambda>_. Some (Some x, empty))" |
301 |
301 |
302 definition hb_map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a hit_bound_lazy_sequence \<Rightarrow> 'b hit_bound_lazy_sequence" |
302 definition hb_map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a hit_bound_lazy_sequence \<Rightarrow> 'b hit_bound_lazy_sequence" |
303 where |
303 where |
304 "hb_map f xq = map (Option.map f) xq" |
304 "hb_map f xq = map (map_option f) xq" |
305 |
305 |
306 lemma hb_map_code [code]: |
306 lemma hb_map_code [code]: |
307 "hb_map f xq = |
307 "hb_map f xq = |
308 Lazy_Sequence (\<lambda>_. Option.map (\<lambda>(x, xq'). (Option.map f x, hb_map f xq')) (yield xq))" |
308 Lazy_Sequence (\<lambda>_. map_option (\<lambda>(x, xq'). (map_option f x, hb_map f xq')) (yield xq))" |
309 using map_code [of "Option.map f" xq] by (simp add: hb_map_def) |
309 using map_code [of "map_option f" xq] by (simp add: hb_map_def) |
310 |
310 |
311 definition hb_flat :: "'a hit_bound_lazy_sequence hit_bound_lazy_sequence \<Rightarrow> 'a hit_bound_lazy_sequence" |
311 definition hb_flat :: "'a hit_bound_lazy_sequence hit_bound_lazy_sequence \<Rightarrow> 'a hit_bound_lazy_sequence" |
312 where |
312 where |
313 "hb_flat xqq = lazy_sequence_of_list (concat |
313 "hb_flat xqq = lazy_sequence_of_list (concat |
314 (List.map ((\<lambda>x. case x of None \<Rightarrow> [None] | Some xs \<Rightarrow> xs) \<circ> Option.map list_of_lazy_sequence) (list_of_lazy_sequence xqq)))" |
314 (List.map ((\<lambda>x. case x of None \<Rightarrow> [None] | Some xs \<Rightarrow> xs) \<circ> map_option list_of_lazy_sequence) (list_of_lazy_sequence xqq)))" |
315 |
315 |
316 lemma list_of_lazy_sequence_hb_flat [simp]: |
316 lemma list_of_lazy_sequence_hb_flat [simp]: |
317 "list_of_lazy_sequence (hb_flat xqq) = |
317 "list_of_lazy_sequence (hb_flat xqq) = |
318 concat (List.map ((\<lambda>x. case x of None \<Rightarrow> [None] | Some xs \<Rightarrow> xs) \<circ> Option.map list_of_lazy_sequence) (list_of_lazy_sequence xqq))" |
318 concat (List.map ((\<lambda>x. case x of None \<Rightarrow> [None] | Some xs \<Rightarrow> xs) \<circ> map_option list_of_lazy_sequence) (list_of_lazy_sequence xqq))" |
319 by (simp add: hb_flat_def) |
319 by (simp add: hb_flat_def) |
320 |
320 |
321 lemma hb_flat_code [code]: |
321 lemma hb_flat_code [code]: |
322 "hb_flat xqq = Lazy_Sequence (\<lambda>_. case yield xqq of |
322 "hb_flat xqq = Lazy_Sequence (\<lambda>_. case yield xqq of |
323 None \<Rightarrow> None |
323 None \<Rightarrow> None |