generalized pull to anamorph
authorhaftmann
Tue, 31 Mar 2009 11:11:36 +0200
changeset 308127d02340f095d
parent 30811 461da7178275
child 30813 a0863fcd9bbf
generalized pull to anamorph
src/HOL/ex/Predicate_Compile.thy
     1.1 --- a/src/HOL/ex/Predicate_Compile.thy	Tue Mar 31 11:04:34 2009 +0200
     1.2 +++ b/src/HOL/ex/Predicate_Compile.thy	Tue Mar 31 11:11:36 2009 +0200
     1.3 @@ -20,15 +20,14 @@
     1.4  end
     1.5  *}
     1.6  
     1.7 -fun pull :: "('a Predicate.pred \<Rightarrow> ('a \<times> 'a Predicate.pred) option)
     1.8 -  \<Rightarrow> index \<Rightarrow> 'a Predicate.pred \<Rightarrow> 'a list \<times> 'a Predicate.pred" where
     1.9 -  "pull yield k P = (if k = 0 then ([], \<bottom>)
    1.10 -    else case yield P of None \<Rightarrow> ([], \<bottom>) | Some (x, Q) \<Rightarrow> let (xs, R) = pull yield (k - 1) Q in (x # xs, R))"
    1.11 +fun anamorph :: "('b \<Rightarrow> ('a \<times> 'b) option) \<Rightarrow> index \<Rightarrow> 'b \<Rightarrow> 'a list \<times> 'b" where
    1.12 +  "anamorph f k x = (if k = 0 then ([], x)
    1.13 +    else case f x of None \<Rightarrow> ([], x) | Some (v, y) \<Rightarrow> let (vs, z) = anamorph f (k - 1) y in (v # vs, z))"
    1.14  
    1.15  ML {*
    1.16  let
    1.17    fun yield (@{code Predicate.Seq} f) = @{code next} yield (f ())
    1.18 -  fun yieldn k = @{code pull} yield k
    1.19 +  fun yieldn k = @{code anamorph} yield k
    1.20  in
    1.21    yieldn 0 (*replace with number of elements to retrieve*)
    1.22      @{code "\<bottom> :: 'a Predicate.pred"} (*replace bottom with sequence to evaluate*)