src/HOL/Lazy_Sequence.thy
author wenzelm
Fri Apr 16 21:28:09 2010 +0200 (2010-04-16)
changeset 36176 3fe7e97ccca8
parent 36049 0ce5b7a5c2fd
child 36513 70096cbdd4e0
permissions -rw-r--r--
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
     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 ML {*
   127 signature LAZY_SEQUENCE =
   128 sig
   129   datatype 'a lazy_sequence = Lazy_Sequence of unit -> ('a * 'a lazy_sequence) option
   130   val yield : 'a lazy_sequence -> ('a * 'a lazy_sequence) option
   131   val yieldn : int -> 'a lazy_sequence -> ('a list * 'a lazy_sequence)
   132   val map : ('a -> 'b) -> 'a lazy_sequence -> 'b lazy_sequence
   133 end;
   134 
   135 structure Lazy_Sequence : LAZY_SEQUENCE =
   136 struct
   137 
   138 @{code_datatype lazy_sequence = Lazy_Sequence}
   139 
   140 val yield = @{code yield}
   141 
   142 fun anamorph f k x = (if k = 0 then ([], x)
   143   else case f x
   144    of NONE => ([], x)
   145     | SOME (v, y) => let
   146         val (vs, z) = anamorph f (k - 1) y
   147       in (v :: vs, z) end);
   148 
   149 fun yieldn S = anamorph yield S;
   150 
   151 val map = @{code map}
   152 
   153 end;
   154 *}
   155 
   156 code_reserved Eval Lazy_Sequence
   157 
   158 code_type lazy_sequence (Eval "_/ Lazy'_Sequence.lazy'_sequence")
   159 
   160 code_const Lazy_Sequence (Eval "Lazy'_Sequence.Lazy'_Sequence")
   161 
   162 section {* With Hit Bound Value *}
   163 text {* assuming in negative context *}
   164 
   165 types 'a hit_bound_lazy_sequence = "'a option lazy_sequence"
   166 
   167 definition hit_bound :: "'a hit_bound_lazy_sequence"
   168 where
   169   [code]: "hit_bound = Lazy_Sequence (%u. Some (None, empty))"
   170 
   171 
   172 definition hb_single :: "'a => 'a hit_bound_lazy_sequence"
   173 where
   174   [code]: "hb_single x = Lazy_Sequence (%u. Some (Some x, empty))"
   175 
   176 primrec hb_flat :: "'a hit_bound_lazy_sequence hit_bound_lazy_sequence => 'a hit_bound_lazy_sequence"
   177 where
   178   "hb_flat Empty = Empty"
   179 | "hb_flat (Insert xq xqq) = append (case xq of None => hit_bound | Some xq => xq) (hb_flat xqq)"
   180 
   181 lemma [code]:
   182   "hb_flat xqq = Lazy_Sequence (%u. case yield xqq of
   183     None => None
   184   | Some (xq, xqq') => yield (append (case xq of None => hit_bound | Some xq => xq) (hb_flat xqq')))"
   185 apply (cases "xqq")
   186 apply (auto simp add: Seq_yield)
   187 unfolding Lazy_Sequence_def
   188 by auto
   189 
   190 primrec hb_map :: "('a => 'b) => 'a hit_bound_lazy_sequence => 'b hit_bound_lazy_sequence"
   191 where
   192   "hb_map f Empty = Empty"
   193 | "hb_map f (Insert x xq) = Insert (Option.map f x) (hb_map f xq)"
   194 
   195 lemma [code]:
   196   "hb_map f xq = Lazy_Sequence (%u. Option.map (%(x, xq'). (Option.map f x, hb_map f xq')) (yield xq))"
   197 apply (cases xq)
   198 apply (auto simp add: Seq_yield)
   199 unfolding Lazy_Sequence_def
   200 apply auto
   201 done
   202 
   203 definition hb_bind :: "'a hit_bound_lazy_sequence => ('a => 'b hit_bound_lazy_sequence) => 'b hit_bound_lazy_sequence"
   204 where
   205   [code]: "hb_bind xq f = hb_flat (hb_map f xq)"
   206 
   207 definition hb_if_seq :: "bool => unit hit_bound_lazy_sequence"
   208 where
   209   "hb_if_seq b = (if b then hb_single () else empty)"
   210 
   211 definition hb_not_seq :: "unit hit_bound_lazy_sequence => unit lazy_sequence"
   212 where
   213   "hb_not_seq xq = (case yield xq of None => single () | Some (x, xq) => empty)"
   214 
   215 hide_type (open) lazy_sequence
   216 hide_const (open) Empty Insert Lazy_Sequence yield empty single append flat map bind if_seq iterate_upto not_seq
   217 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
   218 
   219 end