src/HOL/Lazy_Sequence.thy
author blanchet
Wed Sep 03 00:06:24 2014 +0200 (2014-09-03)
changeset 58152 6fe60a9a5bad
parent 56846 9df717fef2bb
child 58310 91ea607a34d8
permissions -rw-r--r--
use 'datatype_new' in 'Main'
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
haftmann@50055
     7
imports Predicate
bulwahn@34948
     8
begin
bulwahn@34948
     9
haftmann@51126
    10
subsection {* Type of lazy sequences *}
bulwahn@34948
    11
blanchet@58152
    12
datatype_new (dead 'a) lazy_sequence = lazy_sequence_of_list "'a list"
bulwahn@34948
    13
haftmann@51126
    14
primrec list_of_lazy_sequence :: "'a lazy_sequence \<Rightarrow> 'a list"
bulwahn@34948
    15
where
haftmann@51126
    16
  "list_of_lazy_sequence (lazy_sequence_of_list xs) = xs"
haftmann@51126
    17
haftmann@51126
    18
lemma lazy_sequence_of_list_of_lazy_sequence [simp]:
haftmann@51126
    19
  "lazy_sequence_of_list (list_of_lazy_sequence xq) = xq"
haftmann@51126
    20
  by (cases xq) simp_all
haftmann@51126
    21
haftmann@51126
    22
lemma lazy_sequence_eqI:
haftmann@51126
    23
  "list_of_lazy_sequence xq = list_of_lazy_sequence yq \<Longrightarrow> xq = yq"
haftmann@51126
    24
  by (cases xq, cases yq) simp
haftmann@51126
    25
haftmann@51126
    26
lemma lazy_sequence_eq_iff:
haftmann@51126
    27
  "xq = yq \<longleftrightarrow> list_of_lazy_sequence xq = list_of_lazy_sequence yq"
haftmann@51126
    28
  by (auto intro: lazy_sequence_eqI)
bulwahn@34948
    29
haftmann@51126
    30
lemma size_lazy_sequence_eq [code]:
blanchet@56846
    31
  "size_lazy_sequence f xq = Suc (size_list f (list_of_lazy_sequence xq))"
blanchet@58152
    32
  "size xq = Suc (length (list_of_lazy_sequence xq))"
blanchet@56846
    33
  by (cases xq, simp)+
haftmann@51126
    34
blanchet@55416
    35
lemma case_lazy_sequence [simp]:
blanchet@55416
    36
  "case_lazy_sequence f xq = f (list_of_lazy_sequence xq)"
haftmann@51126
    37
  by (cases xq) auto
haftmann@51126
    38
blanchet@55416
    39
lemma rec_lazy_sequence [simp]:
blanchet@55416
    40
  "rec_lazy_sequence f xq = f (list_of_lazy_sequence xq)"
haftmann@51126
    41
  by (cases xq) auto
bulwahn@34948
    42
haftmann@51126
    43
definition Lazy_Sequence :: "(unit \<Rightarrow> ('a \<times> 'a lazy_sequence) option) \<Rightarrow> 'a lazy_sequence"
haftmann@51126
    44
where
haftmann@51126
    45
  "Lazy_Sequence f = lazy_sequence_of_list (case f () of
haftmann@51126
    46
    None \<Rightarrow> []
haftmann@51126
    47
  | Some (x, xq) \<Rightarrow> x # list_of_lazy_sequence xq)"
haftmann@51126
    48
haftmann@51126
    49
code_datatype Lazy_Sequence
haftmann@51126
    50
haftmann@51126
    51
declare list_of_lazy_sequence.simps [code del]
blanchet@55642
    52
declare lazy_sequence.case [code del]
blanchet@55642
    53
declare lazy_sequence.rec [code del]
bulwahn@34948
    54
haftmann@51126
    55
lemma list_of_Lazy_Sequence [simp]:
haftmann@51126
    56
  "list_of_lazy_sequence (Lazy_Sequence f) = (case f () of
haftmann@51126
    57
    None \<Rightarrow> []
haftmann@51126
    58
  | Some (x, xq) \<Rightarrow> x # list_of_lazy_sequence xq)"
haftmann@51126
    59
  by (simp add: Lazy_Sequence_def)
haftmann@51126
    60
haftmann@51126
    61
definition yield :: "'a lazy_sequence \<Rightarrow> ('a \<times> 'a lazy_sequence) option"
haftmann@51126
    62
where
haftmann@51126
    63
  "yield xq = (case list_of_lazy_sequence xq of
haftmann@51126
    64
    [] \<Rightarrow> None
haftmann@51126
    65
  | x # xs \<Rightarrow> Some (x, lazy_sequence_of_list xs))" 
haftmann@51126
    66
haftmann@51126
    67
lemma yield_Seq [simp, code]:
haftmann@51126
    68
  "yield (Lazy_Sequence f) = f ()"
haftmann@51126
    69
  by (cases "f ()") (simp_all add: yield_def split_def)
haftmann@51126
    70
blanchet@55413
    71
lemma case_yield_eq [simp]: "case_option g h (yield xq) =
blanchet@55413
    72
  case_list g (\<lambda>x. curry h x \<circ> lazy_sequence_of_list) (list_of_lazy_sequence xq)"
haftmann@51126
    73
  by (cases "list_of_lazy_sequence xq") (simp_all add: yield_def)
bulwahn@34948
    74
blanchet@56846
    75
lemma size_lazy_sequence_code [code]:
blanchet@56846
    76
  "size_lazy_sequence s xq = (case yield xq of
haftmann@51126
    77
    None \<Rightarrow> 1
blanchet@56846
    78
  | Some (x, xq') \<Rightarrow> Suc (s x + size_lazy_sequence s xq'))"
blanchet@56846
    79
  by (cases "list_of_lazy_sequence xq") (simp_all add: size_lazy_sequence_eq)
bulwahn@34948
    80
haftmann@51126
    81
lemma equal_lazy_sequence_code [code]:
haftmann@51126
    82
  "HOL.equal xq yq = (case (yield xq, yield yq) of
haftmann@51126
    83
    (None, None) \<Rightarrow> True
haftmann@51126
    84
  | (Some (x, xq'), Some (y, yq')) \<Rightarrow> HOL.equal x y \<and> HOL.equal xq yq
haftmann@51126
    85
  | _ \<Rightarrow> False)"
haftmann@51126
    86
  by (simp_all add: lazy_sequence_eq_iff equal_eq split: list.splits)
haftmann@38857
    87
haftmann@38857
    88
lemma [code nbe]:
haftmann@38857
    89
  "HOL.equal (x :: 'a lazy_sequence) x \<longleftrightarrow> True"
haftmann@38857
    90
  by (fact equal_refl)
bulwahn@34948
    91
bulwahn@34948
    92
definition empty :: "'a lazy_sequence"
bulwahn@34948
    93
where
haftmann@51126
    94
  "empty = lazy_sequence_of_list []"
bulwahn@34948
    95
haftmann@51126
    96
lemma list_of_lazy_sequence_empty [simp]:
haftmann@51126
    97
  "list_of_lazy_sequence empty = []"
haftmann@51126
    98
  by (simp add: empty_def)
bulwahn@34948
    99
haftmann@51126
   100
lemma empty_code [code]:
haftmann@51126
   101
  "empty = Lazy_Sequence (\<lambda>_. None)"
haftmann@51126
   102
  by (simp add: lazy_sequence_eq_iff)
bulwahn@34948
   103
haftmann@51126
   104
definition single :: "'a \<Rightarrow> 'a lazy_sequence"
haftmann@51126
   105
where
haftmann@51126
   106
  "single x = lazy_sequence_of_list [x]"
bulwahn@34948
   107
haftmann@51126
   108
lemma list_of_lazy_sequence_single [simp]:
haftmann@51126
   109
  "list_of_lazy_sequence (single x) = [x]"
haftmann@51126
   110
  by (simp add: single_def)
haftmann@51126
   111
haftmann@51126
   112
lemma single_code [code]:
haftmann@51126
   113
  "single x = Lazy_Sequence (\<lambda>_. Some (x, empty))"
haftmann@51126
   114
  by (simp add: lazy_sequence_eq_iff)
haftmann@51126
   115
haftmann@51126
   116
definition append :: "'a lazy_sequence \<Rightarrow> 'a lazy_sequence \<Rightarrow> 'a lazy_sequence"
bulwahn@34948
   117
where
haftmann@51126
   118
  "append xq yq = lazy_sequence_of_list (list_of_lazy_sequence xq @ list_of_lazy_sequence yq)"
haftmann@51126
   119
haftmann@51126
   120
lemma list_of_lazy_sequence_append [simp]:
haftmann@51126
   121
  "list_of_lazy_sequence (append xq yq) = list_of_lazy_sequence xq @ list_of_lazy_sequence yq"
haftmann@51126
   122
  by (simp add: append_def)
bulwahn@34948
   123
haftmann@51126
   124
lemma append_code [code]:
haftmann@51126
   125
  "append xq yq = Lazy_Sequence (\<lambda>_. case yield xq of
haftmann@51126
   126
    None \<Rightarrow> yield yq
haftmann@51126
   127
  | Some (x, xq') \<Rightarrow> Some (x, append xq' yq))"
haftmann@51126
   128
  by (simp_all add: lazy_sequence_eq_iff split: list.splits)
haftmann@51126
   129
haftmann@51126
   130
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a lazy_sequence \<Rightarrow> 'b lazy_sequence"
bulwahn@34948
   131
where
haftmann@51126
   132
  "map f xq = lazy_sequence_of_list (List.map f (list_of_lazy_sequence xq))"
haftmann@51126
   133
haftmann@51126
   134
lemma list_of_lazy_sequence_map [simp]:
haftmann@51126
   135
  "list_of_lazy_sequence (map f xq) = List.map f (list_of_lazy_sequence xq)"
haftmann@51126
   136
  by (simp add: map_def)
haftmann@51126
   137
haftmann@51126
   138
lemma map_code [code]:
haftmann@51126
   139
  "map f xq =
blanchet@55466
   140
    Lazy_Sequence (\<lambda>_. map_option (\<lambda>(x, xq'). (f x, map f xq')) (yield xq))"
haftmann@51126
   141
  by (simp_all add: lazy_sequence_eq_iff split: list.splits)
haftmann@51126
   142
haftmann@51126
   143
definition flat :: "'a lazy_sequence lazy_sequence \<Rightarrow> 'a lazy_sequence"
haftmann@51126
   144
where
haftmann@51126
   145
  "flat xqq = lazy_sequence_of_list (concat (List.map list_of_lazy_sequence (list_of_lazy_sequence xqq)))"
bulwahn@34948
   146
haftmann@51126
   147
lemma list_of_lazy_sequence_flat [simp]:
haftmann@51126
   148
  "list_of_lazy_sequence (flat xqq) = concat (List.map list_of_lazy_sequence (list_of_lazy_sequence xqq))"
haftmann@51126
   149
  by (simp add: flat_def)
bulwahn@34948
   150
haftmann@51126
   151
lemma flat_code [code]:
haftmann@51126
   152
  "flat xqq = Lazy_Sequence (\<lambda>_. case yield xqq of
haftmann@51126
   153
    None \<Rightarrow> None
haftmann@51126
   154
  | Some (xq, xqq') \<Rightarrow> yield (append xq (flat xqq')))"
haftmann@51126
   155
  by (simp add: lazy_sequence_eq_iff split: list.splits)
haftmann@51126
   156
haftmann@51126
   157
definition bind :: "'a lazy_sequence \<Rightarrow> ('a \<Rightarrow> 'b lazy_sequence) \<Rightarrow> 'b lazy_sequence"
bulwahn@34948
   158
where
haftmann@51126
   159
  "bind xq f = flat (map f xq)"
bulwahn@34948
   160
haftmann@51126
   161
definition if_seq :: "bool \<Rightarrow> unit lazy_sequence"
bulwahn@34948
   162
where
bulwahn@34948
   163
  "if_seq b = (if b then single () else empty)"
bulwahn@34948
   164
haftmann@51126
   165
definition those :: "'a option lazy_sequence \<Rightarrow> 'a lazy_sequence option"
bulwahn@36049
   166
where
blanchet@55466
   167
  "those xq = map_option lazy_sequence_of_list (List.those (list_of_lazy_sequence xq))"
haftmann@51126
   168
  
haftmann@51143
   169
function iterate_upto :: "(natural \<Rightarrow> 'a) \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> 'a lazy_sequence"
haftmann@51126
   170
where
haftmann@51126
   171
  "iterate_upto f n m =
haftmann@51126
   172
    Lazy_Sequence (\<lambda>_. if n > m then None else Some (f n, iterate_upto f (n + 1) m))"
haftmann@51126
   173
  by pat_completeness auto
bulwahn@36049
   174
haftmann@51143
   175
termination by (relation "measure (\<lambda>(f, n, m). nat_of_natural (m + 1 - n))")
haftmann@51143
   176
  (auto simp add: less_natural_def)
bulwahn@36049
   177
haftmann@51126
   178
definition not_seq :: "unit lazy_sequence \<Rightarrow> unit lazy_sequence"
bulwahn@34948
   179
where
haftmann@51126
   180
  "not_seq xq = (case yield xq of
haftmann@51126
   181
    None \<Rightarrow> single ()
haftmann@51126
   182
  | Some ((), xq) \<Rightarrow> empty)"
bulwahn@34948
   183
haftmann@51126
   184
  
haftmann@51126
   185
subsection {* Code setup *}
bulwahn@34948
   186
haftmann@36533
   187
code_reflect Lazy_Sequence
haftmann@36533
   188
  datatypes lazy_sequence = Lazy_Sequence
haftmann@51126
   189
haftmann@51126
   190
ML {*
haftmann@51126
   191
signature LAZY_SEQUENCE =
haftmann@51126
   192
sig
haftmann@51126
   193
  datatype 'a lazy_sequence = Lazy_Sequence of (unit -> ('a * 'a Lazy_Sequence.lazy_sequence) option)
haftmann@51126
   194
  val map: ('a -> 'b) -> 'a lazy_sequence -> 'b lazy_sequence
haftmann@51126
   195
  val yield: 'a lazy_sequence -> ('a * 'a lazy_sequence) option
haftmann@51126
   196
  val yieldn: int -> 'a lazy_sequence -> 'a list * 'a lazy_sequence
haftmann@51126
   197
end;
haftmann@51126
   198
haftmann@51126
   199
structure Lazy_Sequence : LAZY_SEQUENCE =
haftmann@51126
   200
struct
haftmann@51126
   201
haftmann@51126
   202
datatype lazy_sequence = datatype Lazy_Sequence.lazy_sequence;
haftmann@51126
   203
haftmann@51126
   204
fun map f = @{code Lazy_Sequence.map} f;
haftmann@51126
   205
haftmann@51126
   206
fun yield P = @{code Lazy_Sequence.yield} P;
haftmann@51126
   207
haftmann@51126
   208
fun yieldn k = Predicate.anamorph yield k;
haftmann@51126
   209
haftmann@51126
   210
end;
haftmann@51126
   211
*}
haftmann@51126
   212
bulwahn@34948
   213
bulwahn@40051
   214
subsection {* Generator Sequences *}
bulwahn@40051
   215
bulwahn@40051
   216
subsubsection {* General lazy sequence operation *}
bulwahn@40051
   217
haftmann@51126
   218
definition product :: "'a lazy_sequence \<Rightarrow> 'b lazy_sequence \<Rightarrow> ('a \<times> 'b) lazy_sequence"
bulwahn@40051
   219
where
haftmann@51126
   220
  "product s1 s2 = bind s1 (\<lambda>a. bind s2 (\<lambda>b. single (a, b)))"
bulwahn@40051
   221
bulwahn@40051
   222
hoelzl@40056
   223
subsubsection {* Small lazy typeclasses *}
bulwahn@40051
   224
bulwahn@40051
   225
class small_lazy =
haftmann@51143
   226
  fixes small_lazy :: "natural \<Rightarrow> 'a lazy_sequence"
bulwahn@40051
   227
bulwahn@40051
   228
instantiation unit :: small_lazy
bulwahn@40051
   229
begin
bulwahn@40051
   230
haftmann@51126
   231
definition "small_lazy d = single ()"
bulwahn@40051
   232
bulwahn@40051
   233
instance ..
bulwahn@40051
   234
bulwahn@40051
   235
end
bulwahn@40051
   236
bulwahn@40051
   237
instantiation int :: small_lazy
bulwahn@40051
   238
begin
bulwahn@40051
   239
bulwahn@40051
   240
text {* maybe optimise this expression -> append (single x) xs == cons x xs 
bulwahn@40051
   241
Performance difference? *}
bulwahn@40051
   242
haftmann@51126
   243
function small_lazy' :: "int \<Rightarrow> int \<Rightarrow> int lazy_sequence"
haftmann@51126
   244
where
haftmann@51126
   245
  "small_lazy' d i = (if d < i then empty
haftmann@51126
   246
    else append (single i) (small_lazy' d (i + 1)))"
haftmann@51126
   247
    by pat_completeness auto
bulwahn@40051
   248
bulwahn@40051
   249
termination 
bulwahn@40051
   250
  by (relation "measure (%(d, i). nat (d + 1 - i))") auto
bulwahn@40051
   251
haftmann@51126
   252
definition
haftmann@51143
   253
  "small_lazy d = small_lazy' (int (nat_of_natural d)) (- (int (nat_of_natural d)))"
bulwahn@40051
   254
bulwahn@40051
   255
instance ..
bulwahn@40051
   256
bulwahn@40051
   257
end
bulwahn@40051
   258
bulwahn@40051
   259
instantiation prod :: (small_lazy, small_lazy) small_lazy
bulwahn@40051
   260
begin
bulwahn@40051
   261
bulwahn@40051
   262
definition
bulwahn@40051
   263
  "small_lazy d = product (small_lazy d) (small_lazy d)"
bulwahn@40051
   264
bulwahn@40051
   265
instance ..
bulwahn@40051
   266
bulwahn@40051
   267
end
bulwahn@40051
   268
bulwahn@40051
   269
instantiation list :: (small_lazy) small_lazy
bulwahn@40051
   270
begin
bulwahn@40051
   271
haftmann@51143
   272
fun small_lazy_list :: "natural \<Rightarrow> 'a list lazy_sequence"
bulwahn@40051
   273
where
haftmann@51126
   274
  "small_lazy_list d = append (single [])
haftmann@51126
   275
    (if d > 0 then bind (product (small_lazy (d - 1))
haftmann@51126
   276
      (small_lazy (d - 1))) (\<lambda>(x, xs). single (x # xs)) else empty)"
bulwahn@40051
   277
bulwahn@40051
   278
instance ..
bulwahn@40051
   279
bulwahn@40051
   280
end
bulwahn@40051
   281
huffman@36902
   282
subsection {* With Hit Bound Value *}
bulwahn@36030
   283
text {* assuming in negative context *}
bulwahn@36030
   284
bulwahn@42163
   285
type_synonym 'a hit_bound_lazy_sequence = "'a option lazy_sequence"
bulwahn@36030
   286
bulwahn@36030
   287
definition hit_bound :: "'a hit_bound_lazy_sequence"
bulwahn@36030
   288
where
haftmann@51126
   289
  "hit_bound = Lazy_Sequence (\<lambda>_. Some (None, empty))"
bulwahn@36030
   290
haftmann@51126
   291
lemma list_of_lazy_sequence_hit_bound [simp]:
haftmann@51126
   292
  "list_of_lazy_sequence hit_bound = [None]"
haftmann@51126
   293
  by (simp add: hit_bound_def)
haftmann@51126
   294
  
haftmann@51126
   295
definition hb_single :: "'a \<Rightarrow> 'a hit_bound_lazy_sequence"
bulwahn@36030
   296
where
haftmann@51126
   297
  "hb_single x = Lazy_Sequence (\<lambda>_. Some (Some x, empty))"
bulwahn@36030
   298
haftmann@51126
   299
definition hb_map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a hit_bound_lazy_sequence \<Rightarrow> 'b hit_bound_lazy_sequence"
bulwahn@36030
   300
where
blanchet@55466
   301
  "hb_map f xq = map (map_option f) xq"
haftmann@51126
   302
haftmann@51126
   303
lemma hb_map_code [code]:
haftmann@51126
   304
  "hb_map f xq =
blanchet@55466
   305
    Lazy_Sequence (\<lambda>_. map_option (\<lambda>(x, xq'). (map_option f x, hb_map f xq')) (yield xq))"
blanchet@55466
   306
  using map_code [of "map_option f" xq] by (simp add: hb_map_def)
bulwahn@36030
   307
haftmann@51126
   308
definition hb_flat :: "'a hit_bound_lazy_sequence hit_bound_lazy_sequence \<Rightarrow> 'a hit_bound_lazy_sequence"
haftmann@51126
   309
where
haftmann@51126
   310
  "hb_flat xqq = lazy_sequence_of_list (concat
blanchet@55466
   311
    (List.map ((\<lambda>x. case x of None \<Rightarrow> [None] | Some xs \<Rightarrow> xs) \<circ> map_option list_of_lazy_sequence) (list_of_lazy_sequence xqq)))"
bulwahn@36030
   312
haftmann@51126
   313
lemma list_of_lazy_sequence_hb_flat [simp]:
haftmann@51126
   314
  "list_of_lazy_sequence (hb_flat xqq) =
blanchet@55466
   315
    concat (List.map ((\<lambda>x. case x of None \<Rightarrow> [None] | Some xs \<Rightarrow> xs) \<circ> map_option list_of_lazy_sequence) (list_of_lazy_sequence xqq))"
haftmann@51126
   316
  by (simp add: hb_flat_def)
bulwahn@36030
   317
haftmann@51126
   318
lemma hb_flat_code [code]:
haftmann@51126
   319
  "hb_flat xqq = Lazy_Sequence (\<lambda>_. case yield xqq of
haftmann@51126
   320
    None \<Rightarrow> None
haftmann@51126
   321
  | Some (xq, xqq') \<Rightarrow> yield
haftmann@51126
   322
     (append (case xq of None \<Rightarrow> hit_bound | Some xq \<Rightarrow> xq) (hb_flat xqq')))"
haftmann@51126
   323
  by (simp add: lazy_sequence_eq_iff split: list.splits option.splits)
bulwahn@36030
   324
haftmann@51126
   325
definition hb_bind :: "'a hit_bound_lazy_sequence \<Rightarrow> ('a \<Rightarrow> 'b hit_bound_lazy_sequence) \<Rightarrow> 'b hit_bound_lazy_sequence"
bulwahn@36030
   326
where
haftmann@51126
   327
  "hb_bind xq f = hb_flat (hb_map f xq)"
bulwahn@36030
   328
haftmann@51126
   329
definition hb_if_seq :: "bool \<Rightarrow> unit hit_bound_lazy_sequence"
bulwahn@36030
   330
where
bulwahn@36030
   331
  "hb_if_seq b = (if b then hb_single () else empty)"
bulwahn@36030
   332
haftmann@51126
   333
definition hb_not_seq :: "unit hit_bound_lazy_sequence \<Rightarrow> unit lazy_sequence"
bulwahn@36030
   334
where
haftmann@51126
   335
  "hb_not_seq xq = (case yield xq of
haftmann@51126
   336
    None \<Rightarrow> single ()
haftmann@51126
   337
  | Some (x, xq) \<Rightarrow> empty)"
bulwahn@36030
   338
haftmann@51126
   339
hide_const (open) yield empty single append flat map bind
haftmann@51126
   340
  if_seq those iterate_upto not_seq product
haftmann@51126
   341
haftmann@51126
   342
hide_fact (open) yield_def empty_def single_def append_def flat_def map_def bind_def
haftmann@51126
   343
  if_seq_def those_def not_seq_def product_def 
bulwahn@34948
   344
bulwahn@34948
   345
end