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