src/HOL/Lazy_Sequence.thy
author bulwahn
Mon Mar 29 17:30:41 2010 +0200 (2010-03-29)
changeset 36024 c1ce2f60b0f2
parent 36020 3ee4c29ead7f
child 36030 1cd962a0b1a6
permissions -rw-r--r--
removed yieldn in Lazy_Sequence and put in the ML structure; corrects behaviour of values command
     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 definition not_seq :: "unit lazy_sequence => unit lazy_sequence"
   114 where
   115   "not_seq xq = (case yield xq of None => single () | Some ((), xq) => empty)"
   116 
   117 subsection {* Code setup *}
   118 
   119 ML {*
   120 signature LAZY_SEQUENCE =
   121 sig
   122   datatype 'a lazy_sequence = Lazy_Sequence of unit -> ('a * 'a lazy_sequence) option
   123   val yield : 'a lazy_sequence -> ('a * 'a lazy_sequence) option
   124   val yieldn : int -> 'a lazy_sequence -> ('a list * 'a lazy_sequence)
   125   val map : ('a -> 'b) -> 'a lazy_sequence -> 'b lazy_sequence
   126 end;
   127 
   128 structure Lazy_Sequence : LAZY_SEQUENCE =
   129 struct
   130 
   131 @{code_datatype lazy_sequence = Lazy_Sequence}
   132 
   133 val yield = @{code yield}
   134 
   135 fun anamorph f k x = (if k = 0 then ([], x)
   136   else case f x
   137    of NONE => ([], x)
   138     | SOME (v, y) => let
   139         val (vs, z) = anamorph f (k - 1) y
   140       in (v :: vs, z) end);
   141 
   142 fun yieldn S = anamorph yield S;
   143 
   144 val map = @{code map}
   145 
   146 end;
   147 *}
   148 
   149 code_reserved Eval Lazy_Sequence
   150 
   151 code_type lazy_sequence (Eval "_/ Lazy'_Sequence.lazy'_sequence")
   152 
   153 code_const Lazy_Sequence (Eval "Lazy'_Sequence.Lazy'_Sequence")
   154 
   155 hide (open) type lazy_sequence
   156 hide (open) const Empty Insert Lazy_Sequence yield empty single append flat map bind if_seq not_seq
   157 hide fact yield.simps empty_def single_def append.simps flat.simps map.simps bind_def if_seq_def not_seq_def
   158 
   159 end