src/HOL/ex/Predicate_Compile.thy
author haftmann
Tue, 31 Mar 2009 11:04:05 +0200
changeset 30810 83642621425a
parent 30374 7311a1546d85
child 30812 7d02340f095d
permissions -rw-r--r--
ML snippets for experimental evaluation
haftmann@30374
     1
theory Predicate_Compile
haftmann@30810
     2
imports Complex_Main Code_Index Lattice_Syntax
haftmann@30374
     3
uses "predicate_compile.ML"
haftmann@30374
     4
begin
haftmann@30374
     5
haftmann@30374
     6
setup {* Predicate_Compile.setup *}
haftmann@30374
     7
haftmann@30374
     8
primrec "next" :: "('a Predicate.pred \<Rightarrow> ('a \<times> 'a Predicate.pred) option)
haftmann@30374
     9
  \<Rightarrow> 'a Predicate.seq \<Rightarrow> ('a \<times> 'a Predicate.pred) option" where
haftmann@30374
    10
    "next yield Predicate.Empty = None"
haftmann@30374
    11
  | "next yield (Predicate.Insert x P) = Some (x, P)"
haftmann@30374
    12
  | "next yield (Predicate.Join P xq) = (case yield P
haftmann@30374
    13
   of None \<Rightarrow> next yield xq | Some (x, Q) \<Rightarrow> Some (x, Predicate.Seq (\<lambda>_. Predicate.Join Q xq)))"
haftmann@30374
    14
haftmann@30810
    15
ML {*
haftmann@30810
    16
let
haftmann@30810
    17
  fun yield (@{code Predicate.Seq} f) = @{code next} yield (f ())
haftmann@30810
    18
in
haftmann@30810
    19
  yield @{code "\<bottom> :: 'a Predicate.pred"} (*replace bottom with sequence to evaluate*)
haftmann@30810
    20
end
haftmann@30810
    21
*}
haftmann@30810
    22
haftmann@30810
    23
fun pull :: "('a Predicate.pred \<Rightarrow> ('a \<times> 'a Predicate.pred) option)
haftmann@30810
    24
  \<Rightarrow> index \<Rightarrow> 'a Predicate.pred \<Rightarrow> 'a list \<times> 'a Predicate.pred" where
haftmann@30810
    25
  "pull yield k P = (if k = 0 then ([], \<bottom>)
haftmann@30810
    26
    else case yield P of None \<Rightarrow> ([], \<bottom>) | Some (x, Q) \<Rightarrow> let (xs, R) = pull yield (k - 1) Q in (x # xs, R))"
haftmann@30810
    27
haftmann@30810
    28
ML {*
haftmann@30810
    29
let
haftmann@30810
    30
  fun yield (@{code Predicate.Seq} f) = @{code next} yield (f ())
haftmann@30810
    31
  fun yieldn k = @{code pull} yield k
haftmann@30810
    32
in
haftmann@30810
    33
  yieldn 0 (*replace with number of elements to retrieve*)
haftmann@30810
    34
    @{code "\<bottom> :: 'a Predicate.pred"} (*replace bottom with sequence to evaluate*)
haftmann@30810
    35
end
haftmann@30810
    36
*}
haftmann@30374
    37
haftmann@30374
    38
end