src/HOL/Lazy_Sequence.thy
author bulwahn
Mon Mar 29 17:30:49 2010 +0200 (2010-03-29)
changeset 36030 1cd962a0b1a6
parent 36024 c1ce2f60b0f2
child 36049 0ce5b7a5c2fd
permissions -rw-r--r--
adding Lazy_Sequences with explicit depth-bound
bulwahn@34948
     1
bulwahn@34948
     2
(* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@34948
     3
bulwahn@34948
     4
header {* Lazy sequences *}
bulwahn@34948
     5
bulwahn@34948
     6
theory Lazy_Sequence
bulwahn@34948
     7
imports List Code_Numeral
bulwahn@34948
     8
begin
bulwahn@34948
     9
bulwahn@34948
    10
datatype 'a lazy_sequence = Empty | Insert 'a "'a lazy_sequence"
bulwahn@34948
    11
bulwahn@34948
    12
definition Lazy_Sequence :: "(unit => ('a * 'a lazy_sequence) option) => 'a lazy_sequence"
bulwahn@34948
    13
where
bulwahn@34948
    14
  "Lazy_Sequence f = (case f () of None => Empty | Some (x, xq) => Insert x xq)"
bulwahn@34948
    15
bulwahn@34948
    16
code_datatype Lazy_Sequence 
bulwahn@34948
    17
bulwahn@34948
    18
primrec yield :: "'a lazy_sequence => ('a * 'a lazy_sequence) option"
bulwahn@34948
    19
where
bulwahn@34948
    20
  "yield Empty = None"
bulwahn@34948
    21
| "yield (Insert x xq) = Some (x, xq)"
bulwahn@34948
    22
bulwahn@34948
    23
lemma [simp]: "yield xq = Some (x, xq') ==> size xq' < size xq"
bulwahn@34948
    24
by (cases xq) auto
bulwahn@34948
    25
bulwahn@34948
    26
lemma yield_Seq [code]:
bulwahn@34948
    27
  "yield (Lazy_Sequence f) = f ()"
bulwahn@34948
    28
unfolding Lazy_Sequence_def by (cases "f ()") auto
bulwahn@34948
    29
bulwahn@34948
    30
lemma Seq_yield:
bulwahn@34948
    31
  "Lazy_Sequence (%u. yield f) = f"
bulwahn@34948
    32
unfolding Lazy_Sequence_def by (cases f) auto
bulwahn@34948
    33
bulwahn@34948
    34
lemma lazy_sequence_size_code [code]:
bulwahn@34948
    35
  "lazy_sequence_size s xq = (case yield xq of None => 0 | Some (x, xq') => s x + lazy_sequence_size s xq' + 1)"
bulwahn@34948
    36
by (cases xq) auto
bulwahn@34948
    37
bulwahn@34948
    38
lemma size_code [code]:
bulwahn@34948
    39
  "size xq = (case yield xq of None => 0 | Some (x, xq') => size xq' + 1)"
bulwahn@34948
    40
by (cases xq) auto
bulwahn@34948
    41
bulwahn@34948
    42
lemma [code]: "eq_class.eq xq yq = (case (yield xq, yield yq) of
bulwahn@34948
    43
  (None, None) => True | (Some (x, xq'), Some (y, yq')) => (HOL.eq x y) \<and> (eq_class.eq xq yq) | _ => False)"
bulwahn@34948
    44
apply (cases xq) apply (cases yq) apply (auto simp add: eq_class.eq_equals) 
bulwahn@34948
    45
apply (cases yq) apply (auto simp add: eq_class.eq_equals) done
bulwahn@34948
    46
bulwahn@34948
    47
lemma seq_case [code]:
bulwahn@34948
    48
  "lazy_sequence_case f g xq = (case (yield xq) of None => f | Some (x, xq') => g x xq')"
bulwahn@34948
    49
by (cases xq) auto
bulwahn@34948
    50
bulwahn@34948
    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'))"
bulwahn@34948
    52
by (cases xq) auto
bulwahn@34948
    53
bulwahn@34948
    54
definition empty :: "'a lazy_sequence"
bulwahn@34948
    55
where
bulwahn@34948
    56
  [code]: "empty = Lazy_Sequence (%u. None)"
bulwahn@34948
    57
bulwahn@34948
    58
definition single :: "'a => 'a lazy_sequence"
bulwahn@34948
    59
where
bulwahn@34948
    60
  [code]: "single x = Lazy_Sequence (%u. Some (x, empty))"
bulwahn@34948
    61
bulwahn@34948
    62
primrec append :: "'a lazy_sequence => 'a lazy_sequence => 'a lazy_sequence"
bulwahn@34948
    63
where
bulwahn@34948
    64
  "append Empty yq = yq"
bulwahn@34948
    65
| "append (Insert x xq) yq = Insert x (append xq yq)"
bulwahn@34948
    66
bulwahn@34948
    67
lemma [code]:
bulwahn@34948
    68
  "append xq yq = Lazy_Sequence (%u. case yield xq of
bulwahn@34948
    69
     None => yield yq
bulwahn@34948
    70
  | Some (x, xq') => Some (x, append xq' yq))"
bulwahn@34948
    71
unfolding Lazy_Sequence_def
bulwahn@34948
    72
apply (cases "xq")
bulwahn@34948
    73
apply auto
bulwahn@34948
    74
apply (cases "yq")
bulwahn@34948
    75
apply auto
bulwahn@34948
    76
done
bulwahn@34948
    77
bulwahn@34948
    78
primrec flat :: "'a lazy_sequence lazy_sequence => 'a lazy_sequence"
bulwahn@34948
    79
where
bulwahn@34948
    80
  "flat Empty = Empty"
bulwahn@34948
    81
| "flat (Insert xq xqq) = append xq (flat xqq)"
bulwahn@34948
    82
 
bulwahn@34948
    83
lemma [code]:
bulwahn@34948
    84
  "flat xqq = Lazy_Sequence (%u. case yield xqq of
bulwahn@34948
    85
    None => None
bulwahn@34948
    86
  | Some (xq, xqq') => yield (append xq (flat xqq')))"
bulwahn@34948
    87
apply (cases "xqq")
bulwahn@34948
    88
apply (auto simp add: Seq_yield)
bulwahn@34948
    89
unfolding Lazy_Sequence_def
bulwahn@34948
    90
by auto
bulwahn@34948
    91
bulwahn@34948
    92
primrec map :: "('a => 'b) => 'a lazy_sequence => 'b lazy_sequence"
bulwahn@34948
    93
where
bulwahn@34948
    94
  "map f Empty = Empty"
bulwahn@34948
    95
| "map f (Insert x xq) = Insert (f x) (map f xq)"
bulwahn@34948
    96
bulwahn@34948
    97
lemma [code]:
bulwahn@34948
    98
  "map f xq = Lazy_Sequence (%u. Option.map (%(x, xq'). (f x, map f xq')) (yield xq))"
bulwahn@34948
    99
apply (cases xq)
bulwahn@34948
   100
apply (auto simp add: Seq_yield)
bulwahn@34948
   101
unfolding Lazy_Sequence_def
bulwahn@34948
   102
apply auto
bulwahn@34948
   103
done
bulwahn@34948
   104
bulwahn@34948
   105
definition bind :: "'a lazy_sequence => ('a => 'b lazy_sequence) => 'b lazy_sequence"
bulwahn@34948
   106
where
bulwahn@34948
   107
  [code]: "bind xq f = flat (map f xq)"
bulwahn@34948
   108
bulwahn@34948
   109
definition if_seq :: "bool => unit lazy_sequence"
bulwahn@34948
   110
where
bulwahn@34948
   111
  "if_seq b = (if b then single () else empty)"
bulwahn@34948
   112
bulwahn@34948
   113
definition not_seq :: "unit lazy_sequence => unit lazy_sequence"
bulwahn@34948
   114
where
bulwahn@34948
   115
  "not_seq xq = (case yield xq of None => single () | Some ((), xq) => empty)"
bulwahn@34948
   116
bulwahn@34948
   117
subsection {* Code setup *}
bulwahn@34948
   118
bulwahn@34948
   119
ML {*
bulwahn@34948
   120
signature LAZY_SEQUENCE =
bulwahn@34948
   121
sig
bulwahn@34948
   122
  datatype 'a lazy_sequence = Lazy_Sequence of unit -> ('a * 'a lazy_sequence) option
bulwahn@34948
   123
  val yield : 'a lazy_sequence -> ('a * 'a lazy_sequence) option
bulwahn@34948
   124
  val yieldn : int -> 'a lazy_sequence -> ('a list * 'a lazy_sequence)
bulwahn@36020
   125
  val map : ('a -> 'b) -> 'a lazy_sequence -> 'b lazy_sequence
bulwahn@34948
   126
end;
bulwahn@34948
   127
bulwahn@34948
   128
structure Lazy_Sequence : LAZY_SEQUENCE =
bulwahn@34948
   129
struct
bulwahn@34948
   130
bulwahn@34948
   131
@{code_datatype lazy_sequence = Lazy_Sequence}
bulwahn@34948
   132
bulwahn@34948
   133
val yield = @{code yield}
bulwahn@34948
   134
bulwahn@36024
   135
fun anamorph f k x = (if k = 0 then ([], x)
bulwahn@36024
   136
  else case f x
bulwahn@36024
   137
   of NONE => ([], x)
bulwahn@36024
   138
    | SOME (v, y) => let
bulwahn@36024
   139
        val (vs, z) = anamorph f (k - 1) y
bulwahn@36024
   140
      in (v :: vs, z) end);
bulwahn@36024
   141
bulwahn@36024
   142
fun yieldn S = anamorph yield S;
bulwahn@34948
   143
bulwahn@36020
   144
val map = @{code map}
bulwahn@36020
   145
bulwahn@34948
   146
end;
bulwahn@34948
   147
*}
bulwahn@34948
   148
bulwahn@34948
   149
code_reserved Eval Lazy_Sequence
bulwahn@34948
   150
bulwahn@34948
   151
code_type lazy_sequence (Eval "_/ Lazy'_Sequence.lazy'_sequence")
bulwahn@34948
   152
bulwahn@34948
   153
code_const Lazy_Sequence (Eval "Lazy'_Sequence.Lazy'_Sequence")
bulwahn@34948
   154
bulwahn@36030
   155
section {* With Hit Bound Value *}
bulwahn@36030
   156
text {* assuming in negative context *}
bulwahn@36030
   157
bulwahn@36030
   158
types 'a hit_bound_lazy_sequence = "'a option lazy_sequence"
bulwahn@36030
   159
bulwahn@36030
   160
definition hit_bound :: "'a hit_bound_lazy_sequence"
bulwahn@36030
   161
where
bulwahn@36030
   162
  [code]: "hit_bound = Lazy_Sequence (%u. Some (None, empty))"
bulwahn@36030
   163
bulwahn@36030
   164
bulwahn@36030
   165
definition hb_single :: "'a => 'a hit_bound_lazy_sequence"
bulwahn@36030
   166
where
bulwahn@36030
   167
  [code]: "hb_single x = Lazy_Sequence (%u. Some (Some x, empty))"
bulwahn@36030
   168
bulwahn@36030
   169
primrec hb_flat :: "'a hit_bound_lazy_sequence hit_bound_lazy_sequence => 'a hit_bound_lazy_sequence"
bulwahn@36030
   170
where
bulwahn@36030
   171
  "hb_flat Empty = Empty"
bulwahn@36030
   172
| "hb_flat (Insert xq xqq) = append (case xq of None => hit_bound | Some xq => xq) (hb_flat xqq)"
bulwahn@36030
   173
bulwahn@36030
   174
lemma [code]:
bulwahn@36030
   175
  "hb_flat xqq = Lazy_Sequence (%u. case yield xqq of
bulwahn@36030
   176
    None => None
bulwahn@36030
   177
  | Some (xq, xqq') => yield (append (case xq of None => hit_bound | Some xq => xq) (hb_flat xqq')))"
bulwahn@36030
   178
apply (cases "xqq")
bulwahn@36030
   179
apply (auto simp add: Seq_yield)
bulwahn@36030
   180
unfolding Lazy_Sequence_def
bulwahn@36030
   181
by auto
bulwahn@36030
   182
bulwahn@36030
   183
primrec hb_map :: "('a => 'b) => 'a hit_bound_lazy_sequence => 'b hit_bound_lazy_sequence"
bulwahn@36030
   184
where
bulwahn@36030
   185
  "hb_map f Empty = Empty"
bulwahn@36030
   186
| "hb_map f (Insert x xq) = Insert (Option.map f x) (hb_map f xq)"
bulwahn@36030
   187
bulwahn@36030
   188
lemma [code]:
bulwahn@36030
   189
  "hb_map f xq = Lazy_Sequence (%u. Option.map (%(x, xq'). (Option.map f x, hb_map f xq')) (yield xq))"
bulwahn@36030
   190
apply (cases xq)
bulwahn@36030
   191
apply (auto simp add: Seq_yield)
bulwahn@36030
   192
unfolding Lazy_Sequence_def
bulwahn@36030
   193
apply auto
bulwahn@36030
   194
done
bulwahn@36030
   195
bulwahn@36030
   196
definition hb_bind :: "'a hit_bound_lazy_sequence => ('a => 'b hit_bound_lazy_sequence) => 'b hit_bound_lazy_sequence"
bulwahn@36030
   197
where
bulwahn@36030
   198
  [code]: "hb_bind xq f = hb_flat (hb_map f xq)"
bulwahn@36030
   199
bulwahn@36030
   200
definition hb_if_seq :: "bool => unit hit_bound_lazy_sequence"
bulwahn@36030
   201
where
bulwahn@36030
   202
  "hb_if_seq b = (if b then hb_single () else empty)"
bulwahn@36030
   203
bulwahn@36030
   204
definition hb_not_seq :: "unit hit_bound_lazy_sequence => unit lazy_sequence"
bulwahn@36030
   205
where
bulwahn@36030
   206
  "hb_not_seq xq = (case yield xq of None => single () | Some (x, xq) => empty)"
bulwahn@36030
   207
bulwahn@34948
   208
hide (open) type lazy_sequence
bulwahn@36024
   209
hide (open) const Empty Insert Lazy_Sequence yield empty single append flat map bind if_seq not_seq
bulwahn@36024
   210
hide fact yield.simps empty_def single_def append.simps flat.simps map.simps bind_def if_seq_def not_seq_def
bulwahn@34948
   211
bulwahn@34948
   212
end