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