src/HOL/ex/Predicate_Compile.thy
author haftmann
Tue, 31 Mar 2009 11:11:36 +0200
changeset 30812 7d02340f095d
parent 30810 83642621425a
child 30953 d5f5ab29d769
child 31106 9a1178204dc0
permissions -rw-r--r--
generalized pull to anamorph

theory Predicate_Compile
imports Complex_Main Code_Index Lattice_Syntax
uses "predicate_compile.ML"
begin

setup {* Predicate_Compile.setup *}

primrec "next" :: "('a Predicate.pred \<Rightarrow> ('a \<times> 'a Predicate.pred) option)
  \<Rightarrow> 'a Predicate.seq \<Rightarrow> ('a \<times> 'a Predicate.pred) option" where
    "next yield Predicate.Empty = None"
  | "next yield (Predicate.Insert x P) = Some (x, P)"
  | "next yield (Predicate.Join P xq) = (case yield P
   of None \<Rightarrow> next yield xq | Some (x, Q) \<Rightarrow> Some (x, Predicate.Seq (\<lambda>_. Predicate.Join Q xq)))"

ML {*
let
  fun yield (@{code Predicate.Seq} f) = @{code next} yield (f ())
in
  yield @{code "\<bottom> :: 'a Predicate.pred"} (*replace bottom with sequence to evaluate*)
end
*}

fun anamorph :: "('b \<Rightarrow> ('a \<times> 'b) option) \<Rightarrow> index \<Rightarrow> 'b \<Rightarrow> 'a list \<times> 'b" where
  "anamorph f k x = (if k = 0 then ([], x)
    else case f x of None \<Rightarrow> ([], x) | Some (v, y) \<Rightarrow> let (vs, z) = anamorph f (k - 1) y in (v # vs, z))"

ML {*
let
  fun yield (@{code Predicate.Seq} f) = @{code next} yield (f ())
  fun yieldn k = @{code anamorph} yield k
in
  yieldn 0 (*replace with number of elements to retrieve*)
    @{code "\<bottom> :: 'a Predicate.pred"} (*replace bottom with sequence to evaluate*)
end
*}

end