src/HOL/Lazy_Sequence.thy
author haftmann
Fri Jun 11 17:14:02 2010 +0200 (2010-06-11)
changeset 37407 61dd8c145da7
parent 36902 c6bae4456741
child 38857 97775f3e8722
permissions -rw-r--r--
declare lex_prod_def [code del]
     1 
     2 (* Author: Lukas Bulwahn, TU Muenchen *)
     3 
     4 header {* Lazy sequences *}
     5 
     6 theory Lazy_Sequence
     7 imports List Code_Numeral
     8 begin
     9 
    10 datatype 'a lazy_sequence = Empty | Insert 'a "'a lazy_sequence"
    11 
    12 definition Lazy_Sequence :: "(unit => ('a * 'a lazy_sequence) option) => 'a lazy_sequence"
    13 where
    14   "Lazy_Sequence f = (case f () of None => Empty | Some (x, xq) => Insert x xq)"
    15 
    16 code_datatype Lazy_Sequence 
    17 
    18 primrec yield :: "'a lazy_sequence => ('a * 'a lazy_sequence) option"
    19 where
    20   "yield Empty = None"
    21 | "yield (Insert x xq) = Some (x, xq)"
    22 
    23 lemma [simp]: "yield xq = Some (x, xq') ==> size xq' < size xq"
    24 by (cases xq) auto
    25 
    26 lemma yield_Seq [code]:
    27   "yield (Lazy_Sequence f) = f ()"
    28 unfolding Lazy_Sequence_def by (cases "f ()") auto
    29 
    30 lemma Seq_yield:
    31   "Lazy_Sequence (%u. yield f) = f"
    32 unfolding Lazy_Sequence_def by (cases f) auto
    33 
    34 lemma lazy_sequence_size_code [code]:
    35   "lazy_sequence_size s xq = (case yield xq of None => 0 | Some (x, xq') => s x + lazy_sequence_size s xq' + 1)"
    36 by (cases xq) auto
    37 
    38 lemma size_code [code]:
    39   "size xq = (case yield xq of None => 0 | Some (x, xq') => size xq' + 1)"
    40 by (cases xq) auto
    41 
    42 lemma [code]: "eq_class.eq xq yq = (case (yield xq, yield yq) of
    43   (None, None) => True | (Some (x, xq'), Some (y, yq')) => (HOL.eq x y) \<and> (eq_class.eq xq yq) | _ => False)"
    44 apply (cases xq) apply (cases yq) apply (auto simp add: eq_class.eq_equals) 
    45 apply (cases yq) apply (auto simp add: eq_class.eq_equals) done
    46 
    47 lemma seq_case [code]:
    48   "lazy_sequence_case f g xq = (case (yield xq) of None => f | Some (x, xq') => g x xq')"
    49 by (cases xq) auto
    50 
    51 lemma [code]: "lazy_sequence_rec f g xq = (case (yield xq) of None => f | Some (x, xq') => g x xq' (lazy_sequence_rec f g xq'))"
    52 by (cases xq) auto
    53 
    54 definition empty :: "'a lazy_sequence"
    55 where
    56   [code]: "empty = Lazy_Sequence (%u. None)"
    57 
    58 definition single :: "'a => 'a lazy_sequence"
    59 where
    60   [code]: "single x = Lazy_Sequence (%u. Some (x, empty))"
    61 
    62 primrec append :: "'a lazy_sequence => 'a lazy_sequence => 'a lazy_sequence"
    63 where
    64   "append Empty yq = yq"
    65 | "append (Insert x xq) yq = Insert x (append xq yq)"
    66 
    67 lemma [code]:
    68   "append xq yq = Lazy_Sequence (%u. case yield xq of
    69      None => yield yq
    70   | Some (x, xq') => Some (x, append xq' yq))"
    71 unfolding Lazy_Sequence_def
    72 apply (cases "xq")
    73 apply auto
    74 apply (cases "yq")
    75 apply auto
    76 done
    77 
    78 primrec flat :: "'a lazy_sequence lazy_sequence => 'a lazy_sequence"
    79 where
    80   "flat Empty = Empty"
    81 | "flat (Insert xq xqq) = append xq (flat xqq)"
    82  
    83 lemma [code]:
    84   "flat xqq = Lazy_Sequence (%u. case yield xqq of
    85     None => None
    86   | Some (xq, xqq') => yield (append xq (flat xqq')))"
    87 apply (cases "xqq")
    88 apply (auto simp add: Seq_yield)
    89 unfolding Lazy_Sequence_def
    90 by auto
    91 
    92 primrec map :: "('a => 'b) => 'a lazy_sequence => 'b lazy_sequence"
    93 where
    94   "map f Empty = Empty"
    95 | "map f (Insert x xq) = Insert (f x) (map f xq)"
    96 
    97 lemma [code]:
    98   "map f xq = Lazy_Sequence (%u. Option.map (%(x, xq'). (f x, map f xq')) (yield xq))"
    99 apply (cases xq)
   100 apply (auto simp add: Seq_yield)
   101 unfolding Lazy_Sequence_def
   102 apply auto
   103 done
   104 
   105 definition bind :: "'a lazy_sequence => ('a => 'b lazy_sequence) => 'b lazy_sequence"
   106 where
   107   [code]: "bind xq f = flat (map f xq)"
   108 
   109 definition if_seq :: "bool => unit lazy_sequence"
   110 where
   111   "if_seq b = (if b then single () else empty)"
   112 
   113 function iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a Lazy_Sequence.lazy_sequence"
   114 where
   115   "iterate_upto f n m = Lazy_Sequence.Lazy_Sequence (%u. if n > m then None else Some (f n, iterate_upto f (n + 1) m))"
   116 by pat_completeness auto
   117 
   118 termination by (relation "measure (%(f, n, m). Code_Numeral.nat_of (m + 1 - n))") auto
   119 
   120 definition not_seq :: "unit lazy_sequence => unit lazy_sequence"
   121 where
   122   "not_seq xq = (case yield xq of None => single () | Some ((), xq) => empty)"
   123 
   124 subsection {* Code setup *}
   125 
   126 fun anamorph :: "('a \<Rightarrow> ('b \<times> 'a) option) \<Rightarrow> code_numeral \<Rightarrow> 'a \<Rightarrow> 'b list \<times> 'a" where
   127   "anamorph f k x = (if k = 0 then ([], x)
   128     else case f x of None \<Rightarrow> ([], x) | Some (v, y) \<Rightarrow>
   129       let (vs, z) = anamorph f (k - 1) y
   130     in (v # vs, z))"
   131 
   132 definition yieldn :: "code_numeral \<Rightarrow> 'a lazy_sequence \<Rightarrow> 'a list \<times> 'a lazy_sequence" where
   133   "yieldn = anamorph yield"
   134 
   135 code_reflect Lazy_Sequence
   136   datatypes lazy_sequence = Lazy_Sequence
   137   functions map yield yieldn
   138 
   139 subsection {* With Hit Bound Value *}
   140 text {* assuming in negative context *}
   141 
   142 types 'a hit_bound_lazy_sequence = "'a option lazy_sequence"
   143 
   144 definition hit_bound :: "'a hit_bound_lazy_sequence"
   145 where
   146   [code]: "hit_bound = Lazy_Sequence (%u. Some (None, empty))"
   147 
   148 
   149 definition hb_single :: "'a => 'a hit_bound_lazy_sequence"
   150 where
   151   [code]: "hb_single x = Lazy_Sequence (%u. Some (Some x, empty))"
   152 
   153 primrec hb_flat :: "'a hit_bound_lazy_sequence hit_bound_lazy_sequence => 'a hit_bound_lazy_sequence"
   154 where
   155   "hb_flat Empty = Empty"
   156 | "hb_flat (Insert xq xqq) = append (case xq of None => hit_bound | Some xq => xq) (hb_flat xqq)"
   157 
   158 lemma [code]:
   159   "hb_flat xqq = Lazy_Sequence (%u. case yield xqq of
   160     None => None
   161   | Some (xq, xqq') => yield (append (case xq of None => hit_bound | Some xq => xq) (hb_flat xqq')))"
   162 apply (cases "xqq")
   163 apply (auto simp add: Seq_yield)
   164 unfolding Lazy_Sequence_def
   165 by auto
   166 
   167 primrec hb_map :: "('a => 'b) => 'a hit_bound_lazy_sequence => 'b hit_bound_lazy_sequence"
   168 where
   169   "hb_map f Empty = Empty"
   170 | "hb_map f (Insert x xq) = Insert (Option.map f x) (hb_map f xq)"
   171 
   172 lemma [code]:
   173   "hb_map f xq = Lazy_Sequence (%u. Option.map (%(x, xq'). (Option.map f x, hb_map f xq')) (yield xq))"
   174 apply (cases xq)
   175 apply (auto simp add: Seq_yield)
   176 unfolding Lazy_Sequence_def
   177 apply auto
   178 done
   179 
   180 definition hb_bind :: "'a hit_bound_lazy_sequence => ('a => 'b hit_bound_lazy_sequence) => 'b hit_bound_lazy_sequence"
   181 where
   182   [code]: "hb_bind xq f = hb_flat (hb_map f xq)"
   183 
   184 definition hb_if_seq :: "bool => unit hit_bound_lazy_sequence"
   185 where
   186   "hb_if_seq b = (if b then hb_single () else empty)"
   187 
   188 definition hb_not_seq :: "unit hit_bound_lazy_sequence => unit lazy_sequence"
   189 where
   190   "hb_not_seq xq = (case yield xq of None => single () | Some (x, xq) => empty)"
   191 
   192 hide_type (open) lazy_sequence
   193 hide_const (open) Empty Insert Lazy_Sequence yield empty single append flat map bind if_seq iterate_upto not_seq
   194 hide_fact yield.simps empty_def single_def append.simps flat.simps map.simps bind_def iterate_upto.simps if_seq_def not_seq_def
   195 
   196 end