equal
deleted
inserted
replaced
1 (* Author: Lukas Bulwahn, TU Muenchen *) |
1 (* Author: Lukas Bulwahn, TU Muenchen *) |
2 |
2 |
3 section {* Lazy sequences *} |
3 section \<open>Lazy sequences\<close> |
4 |
4 |
5 theory Lazy_Sequence |
5 theory Lazy_Sequence |
6 imports Predicate |
6 imports Predicate |
7 begin |
7 begin |
8 |
8 |
9 subsection {* Type of lazy sequences *} |
9 subsection \<open>Type of lazy sequences\<close> |
10 |
10 |
11 datatype (plugins only: code extraction) (dead 'a) lazy_sequence = |
11 datatype (plugins only: code extraction) (dead 'a) lazy_sequence = |
12 lazy_sequence_of_list "'a list" |
12 lazy_sequence_of_list "'a list" |
13 |
13 |
14 primrec list_of_lazy_sequence :: "'a lazy_sequence \<Rightarrow> 'a list" |
14 primrec list_of_lazy_sequence :: "'a lazy_sequence \<Rightarrow> 'a list" |
169 "not_seq xq = (case yield xq of |
169 "not_seq xq = (case yield xq of |
170 None \<Rightarrow> single () |
170 None \<Rightarrow> single () |
171 | Some ((), xq) \<Rightarrow> empty)" |
171 | Some ((), xq) \<Rightarrow> empty)" |
172 |
172 |
173 |
173 |
174 subsection {* Code setup *} |
174 subsection \<open>Code setup\<close> |
175 |
175 |
176 code_reflect Lazy_Sequence |
176 code_reflect Lazy_Sequence |
177 datatypes lazy_sequence = Lazy_Sequence |
177 datatypes lazy_sequence = Lazy_Sequence |
178 |
178 |
179 ML {* |
179 ML \<open> |
180 signature LAZY_SEQUENCE = |
180 signature LAZY_SEQUENCE = |
181 sig |
181 sig |
182 datatype 'a lazy_sequence = Lazy_Sequence of (unit -> ('a * 'a Lazy_Sequence.lazy_sequence) option) |
182 datatype 'a lazy_sequence = Lazy_Sequence of (unit -> ('a * 'a Lazy_Sequence.lazy_sequence) option) |
183 val map: ('a -> 'b) -> 'a lazy_sequence -> 'b lazy_sequence |
183 val map: ('a -> 'b) -> 'a lazy_sequence -> 'b lazy_sequence |
184 val yield: 'a lazy_sequence -> ('a * 'a lazy_sequence) option |
184 val yield: 'a lazy_sequence -> ('a * 'a lazy_sequence) option |
195 fun yield P = @{code Lazy_Sequence.yield} P; |
195 fun yield P = @{code Lazy_Sequence.yield} P; |
196 |
196 |
197 fun yieldn k = Predicate.anamorph yield k; |
197 fun yieldn k = Predicate.anamorph yield k; |
198 |
198 |
199 end; |
199 end; |
200 *} |
200 \<close> |
201 |
201 |
202 |
202 |
203 subsection {* Generator Sequences *} |
203 subsection \<open>Generator Sequences\<close> |
204 |
204 |
205 subsubsection {* General lazy sequence operation *} |
205 subsubsection \<open>General lazy sequence operation\<close> |
206 |
206 |
207 definition product :: "'a lazy_sequence \<Rightarrow> 'b lazy_sequence \<Rightarrow> ('a \<times> 'b) lazy_sequence" |
207 definition product :: "'a lazy_sequence \<Rightarrow> 'b lazy_sequence \<Rightarrow> ('a \<times> 'b) lazy_sequence" |
208 where |
208 where |
209 "product s1 s2 = bind s1 (\<lambda>a. bind s2 (\<lambda>b. single (a, b)))" |
209 "product s1 s2 = bind s1 (\<lambda>a. bind s2 (\<lambda>b. single (a, b)))" |
210 |
210 |
211 |
211 |
212 subsubsection {* Small lazy typeclasses *} |
212 subsubsection \<open>Small lazy typeclasses\<close> |
213 |
213 |
214 class small_lazy = |
214 class small_lazy = |
215 fixes small_lazy :: "natural \<Rightarrow> 'a lazy_sequence" |
215 fixes small_lazy :: "natural \<Rightarrow> 'a lazy_sequence" |
216 |
216 |
217 instantiation unit :: small_lazy |
217 instantiation unit :: small_lazy |
224 end |
224 end |
225 |
225 |
226 instantiation int :: small_lazy |
226 instantiation int :: small_lazy |
227 begin |
227 begin |
228 |
228 |
229 text {* maybe optimise this expression -> append (single x) xs == cons x xs |
229 text \<open>maybe optimise this expression -> append (single x) xs == cons x xs |
230 Performance difference? *} |
230 Performance difference?\<close> |
231 |
231 |
232 function small_lazy' :: "int \<Rightarrow> int \<Rightarrow> int lazy_sequence" |
232 function small_lazy' :: "int \<Rightarrow> int \<Rightarrow> int lazy_sequence" |
233 where |
233 where |
234 "small_lazy' d i = (if d < i then empty |
234 "small_lazy' d i = (if d < i then empty |
235 else append (single i) (small_lazy' d (i + 1)))" |
235 else append (single i) (small_lazy' d (i + 1)))" |
266 |
266 |
267 instance .. |
267 instance .. |
268 |
268 |
269 end |
269 end |
270 |
270 |
271 subsection {* With Hit Bound Value *} |
271 subsection \<open>With Hit Bound Value\<close> |
272 text {* assuming in negative context *} |
272 text \<open>assuming in negative context\<close> |
273 |
273 |
274 type_synonym 'a hit_bound_lazy_sequence = "'a option lazy_sequence" |
274 type_synonym 'a hit_bound_lazy_sequence = "'a option lazy_sequence" |
275 |
275 |
276 definition hit_bound :: "'a hit_bound_lazy_sequence" |
276 definition hit_bound :: "'a hit_bound_lazy_sequence" |
277 where |
277 where |