src/HOL/BNF/Examples/Stream.thy
author traytel
Wed Oct 02 13:29:04 2013 +0200 (2013-10-02)
changeset 54027 e5853a648b59
parent 53808 b3e2022530e3
child 54469 0ccec59194af
permissions -rw-r--r--
use new coinduction method and primcorec in examples
traytel@50518
     1
(*  Title:      HOL/BNF/Examples/Stream.thy
traytel@50518
     2
    Author:     Dmitriy Traytel, TU Muenchen
traytel@50518
     3
    Author:     Andrei Popescu, TU Muenchen
blanchet@51778
     4
    Copyright   2012, 2013
traytel@50518
     5
traytel@50518
     6
Infinite streams.
traytel@50518
     7
*)
traytel@50518
     8
traytel@50518
     9
header {* Infinite Streams *}
traytel@50518
    10
traytel@50518
    11
theory Stream
traytel@50518
    12
imports "../BNF"
traytel@50518
    13
begin
traytel@50518
    14
blanchet@51804
    15
codatatype (sset: 'a) stream (map: smap rel: stream_all2) =
blanchet@51804
    16
  Stream (shd: 'a) (stl: "'a stream") (infixr "##" 65)
traytel@50518
    17
traytel@51409
    18
code_datatype Stream
traytel@51409
    19
traytel@51409
    20
lemma stream_case_cert:
traytel@51409
    21
  assumes "CASE \<equiv> stream_case c"
traytel@51409
    22
  shows "CASE (a ## s) \<equiv> c a s"
traytel@51409
    23
  using assms by simp_all
traytel@51409
    24
traytel@51409
    25
setup {*
traytel@51409
    26
  Code.add_case @{thm stream_case_cert}
traytel@51409
    27
*}
traytel@51409
    28
traytel@51462
    29
(*for code generation only*)
traytel@51462
    30
definition smember :: "'a \<Rightarrow> 'a stream \<Rightarrow> bool" where
traytel@51772
    31
  [code_abbrev]: "smember x s \<longleftrightarrow> x \<in> sset s"
traytel@51462
    32
traytel@51462
    33
lemma smember_code[code, simp]: "smember x (Stream y s) = (if x = y then True else smember x s)"
traytel@51462
    34
  unfolding smember_def by auto
traytel@51462
    35
traytel@51462
    36
hide_const (open) smember
traytel@51462
    37
traytel@50518
    38
(* TODO: Provide by the package*)
traytel@51772
    39
theorem sset_induct:
traytel@51772
    40
  "\<lbrakk>\<And>s. P (shd s) s; \<And>s y. \<lbrakk>y \<in> sset (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s\<rbrakk> \<Longrightarrow>
traytel@51772
    41
    \<forall>y \<in> sset s. P y s"
blanchet@52991
    42
  apply (rule stream.dtor_set_induct)
blanchet@52991
    43
  apply (auto simp add: shd_def stl_def fsts_def snds_def split_beta)
blanchet@52991
    44
  apply (metis Stream_def fst_conv stream.case stream.dtor_ctor stream.exhaust)
blanchet@52991
    45
  by (metis Stream_def sndI stl_def stream.collapse stream.dtor_ctor)
traytel@51141
    46
traytel@51772
    47
lemma smap_simps[simp]:
traytel@51772
    48
  "shd (smap f s) = f (shd s)" "stl (smap f s) = smap f (stl s)"
blanchet@52991
    49
  by (case_tac [!] s) auto
traytel@51141
    50
traytel@51772
    51
theorem shd_sset: "shd s \<in> sset s"
blanchet@52991
    52
  by (case_tac s) auto
traytel@50518
    53
traytel@51772
    54
theorem stl_sset: "y \<in> sset (stl s) \<Longrightarrow> y \<in> sset s"
blanchet@52991
    55
  by (case_tac s) auto
traytel@50518
    56
traytel@50518
    57
(* only for the non-mutual case: *)
traytel@51772
    58
theorem sset_induct1[consumes 1, case_names shd stl, induct set: "sset"]:
traytel@51772
    59
  assumes "y \<in> sset s" and "\<And>s. P (shd s) s"
traytel@51772
    60
  and "\<And>s y. \<lbrakk>y \<in> sset (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s"
traytel@50518
    61
  shows "P y s"
traytel@51772
    62
  using assms sset_induct by blast
traytel@50518
    63
(* end TODO *)
traytel@50518
    64
traytel@50518
    65
traytel@50518
    66
subsection {* prepend list to stream *}
traytel@50518
    67
traytel@50518
    68
primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr "@-" 65) where
traytel@50518
    69
  "shift [] s = s"
traytel@51023
    70
| "shift (x # xs) s = x ## shift xs s"
traytel@50518
    71
traytel@51772
    72
lemma smap_shift[simp]: "smap f (xs @- s) = map f xs @- smap f s"
traytel@51353
    73
  by (induct xs) auto
traytel@51353
    74
traytel@50518
    75
lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s"
traytel@51141
    76
  by (induct xs) auto
traytel@50518
    77
traytel@50518
    78
lemma shift_simps[simp]:
traytel@50518
    79
   "shd (xs @- s) = (if xs = [] then shd s else hd xs)"
traytel@50518
    80
   "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)"
traytel@51141
    81
  by (induct xs) auto
traytel@50518
    82
traytel@51772
    83
lemma sset_shift[simp]: "sset (xs @- s) = set xs \<union> sset s"
traytel@51141
    84
  by (induct xs) auto
traytel@50518
    85
traytel@51352
    86
lemma shift_left_inj[simp]: "xs @- s1 = xs @- s2 \<longleftrightarrow> s1 = s2"
traytel@51352
    87
  by (induct xs) auto
traytel@51352
    88
traytel@50518
    89
traytel@51141
    90
subsection {* set of streams with elements in some fixes set *}
traytel@50518
    91
traytel@50518
    92
coinductive_set
traytel@50518
    93
  streams :: "'a set => 'a stream set"
traytel@50518
    94
  for A :: "'a set"
traytel@50518
    95
where
traytel@51023
    96
  Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> a ## s \<in> streams A"
traytel@50518
    97
traytel@50518
    98
lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A"
traytel@51141
    99
  by (induct w) auto
traytel@50518
   100
traytel@51772
   101
lemma sset_streams:
traytel@51772
   102
  assumes "sset s \<subseteq> A"
traytel@50518
   103
  shows "s \<in> streams A"
traytel@54027
   104
using assms proof (coinduction arbitrary: s)
traytel@54027
   105
  case streams then show ?case by (cases s) simp
traytel@50518
   106
qed
traytel@50518
   107
traytel@50518
   108
traytel@51141
   109
subsection {* nth, take, drop for streams *}
traytel@51141
   110
traytel@51141
   111
primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where
traytel@51141
   112
  "s !! 0 = shd s"
traytel@51141
   113
| "s !! Suc n = stl s !! n"
traytel@51141
   114
traytel@51772
   115
lemma snth_smap[simp]: "smap f s !! n = f (s !! n)"
traytel@51141
   116
  by (induct n arbitrary: s) auto
traytel@51141
   117
traytel@51141
   118
lemma shift_snth_less[simp]: "p < length xs \<Longrightarrow> (xs @- s) !! p = xs ! p"
traytel@51141
   119
  by (induct p arbitrary: xs) (auto simp: hd_conv_nth nth_tl)
traytel@51141
   120
traytel@51141
   121
lemma shift_snth_ge[simp]: "p \<ge> length xs \<Longrightarrow> (xs @- s) !! p = s !! (p - length xs)"
traytel@51141
   122
  by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred)
traytel@51141
   123
traytel@51772
   124
lemma snth_sset[simp]: "s !! n \<in> sset s"
traytel@51772
   125
  by (induct n arbitrary: s) (auto intro: shd_sset stl_sset)
traytel@51141
   126
traytel@51772
   127
lemma sset_range: "sset s = range (snth s)"
traytel@51141
   128
proof (intro equalityI subsetI)
traytel@51772
   129
  fix x assume "x \<in> sset s"
traytel@51141
   130
  thus "x \<in> range (snth s)"
traytel@51141
   131
  proof (induct s)
traytel@51141
   132
    case (stl s x)
traytel@51141
   133
    then obtain n where "x = stl s !! n" by auto
traytel@51141
   134
    thus ?case by (auto intro: range_eqI[of _ _ "Suc n"])
traytel@51141
   135
  qed (auto intro: range_eqI[of _ _ 0])
traytel@51141
   136
qed auto
traytel@50518
   137
traytel@50518
   138
primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
traytel@50518
   139
  "stake 0 s = []"
traytel@50518
   140
| "stake (Suc n) s = shd s # stake n (stl s)"
traytel@50518
   141
traytel@51141
   142
lemma length_stake[simp]: "length (stake n s) = n"
traytel@51141
   143
  by (induct n arbitrary: s) auto
traytel@51141
   144
traytel@51772
   145
lemma stake_smap[simp]: "stake n (smap f s) = map f (stake n s)"
traytel@51141
   146
  by (induct n arbitrary: s) auto
traytel@51141
   147
traytel@50518
   148
primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
traytel@50518
   149
  "sdrop 0 s = s"
traytel@50518
   150
| "sdrop (Suc n) s = sdrop n (stl s)"
traytel@50518
   151
traytel@51141
   152
lemma sdrop_simps[simp]:
traytel@51141
   153
  "shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s"
traytel@51141
   154
  by (induct n arbitrary: s)  auto
traytel@51141
   155
traytel@51772
   156
lemma sdrop_smap[simp]: "sdrop n (smap f s) = smap f (sdrop n s)"
traytel@51141
   157
  by (induct n arbitrary: s) auto
traytel@50518
   158
traytel@51352
   159
lemma sdrop_stl: "sdrop n (stl s) = stl (sdrop n s)"
traytel@51352
   160
  by (induct n) auto
traytel@51352
   161
traytel@50518
   162
lemma stake_sdrop: "stake n s @- sdrop n s = s"
traytel@51141
   163
  by (induct n arbitrary: s) auto
traytel@51141
   164
traytel@51141
   165
lemma id_stake_snth_sdrop:
traytel@51141
   166
  "s = stake i s @- s !! i ## sdrop (Suc i) s"
traytel@51141
   167
  by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse)
traytel@50518
   168
traytel@51772
   169
lemma smap_alt: "smap f s = s' \<longleftrightarrow> (\<forall>n. f (s !! n) = s' !! n)" (is "?L = ?R")
traytel@51141
   170
proof
traytel@51141
   171
  assume ?R
traytel@54027
   172
  then have "\<And>n. smap f (sdrop n s) = sdrop n s'"
traytel@54027
   173
    by coinduction (auto intro: exI[of _ 0] simp del: sdrop.simps(2))
traytel@54027
   174
  then show ?L using sdrop.simps(1) by metis
traytel@51141
   175
qed auto
traytel@51141
   176
traytel@51141
   177
lemma stake_invert_Nil[iff]: "stake n s = [] \<longleftrightarrow> n = 0"
traytel@51141
   178
  by (induct n) auto
traytel@50518
   179
traytel@50518
   180
lemma sdrop_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> sdrop n s = s'"
traytel@51141
   181
  by (induct n arbitrary: w s) auto
traytel@50518
   182
traytel@50518
   183
lemma stake_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> stake n s = w"
traytel@51141
   184
  by (induct n arbitrary: w s) auto
traytel@50518
   185
traytel@50518
   186
lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s"
traytel@51141
   187
  by (induct m arbitrary: s) auto
traytel@50518
   188
traytel@50518
   189
lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"
traytel@51141
   190
  by (induct m arbitrary: s) auto
traytel@51141
   191
traytel@51430
   192
partial_function (tailrec) sdrop_while :: "('a \<Rightarrow> bool) \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where 
traytel@51430
   193
  "sdrop_while P s = (if P (shd s) then sdrop_while P (stl s) else s)"
traytel@51430
   194
traytel@51430
   195
lemma sdrop_while_Stream[code]:
traytel@51430
   196
  "sdrop_while P (Stream a s) = (if P a then sdrop_while P s else Stream a s)"
traytel@51430
   197
  by (subst sdrop_while.simps) simp
traytel@51430
   198
traytel@51430
   199
lemma sdrop_while_sdrop_LEAST:
traytel@51430
   200
  assumes "\<exists>n. P (s !! n)"
traytel@51430
   201
  shows "sdrop_while (Not o P) s = sdrop (LEAST n. P (s !! n)) s"
traytel@51430
   202
proof -
traytel@51430
   203
  from assms obtain m where "P (s !! m)" "\<And>n. P (s !! n) \<Longrightarrow> m \<le> n"
traytel@51430
   204
    and *: "(LEAST n. P (s !! n)) = m" by atomize_elim (auto intro: LeastI Least_le)
traytel@51430
   205
  thus ?thesis unfolding *
traytel@51430
   206
  proof (induct m arbitrary: s)
traytel@51430
   207
    case (Suc m)
traytel@51430
   208
    hence "sdrop_while (Not \<circ> P) (stl s) = sdrop m (stl s)"
traytel@51430
   209
      by (metis (full_types) not_less_eq_eq snth.simps(2))
traytel@51430
   210
    moreover from Suc(3) have "\<not> (P (s !! 0))" by blast
traytel@51430
   211
    ultimately show ?case by (subst sdrop_while.simps) simp
traytel@51430
   212
  qed (metis comp_apply sdrop.simps(1) sdrop_while.simps snth.simps(1))
traytel@51430
   213
qed
traytel@51430
   214
traytel@54027
   215
primcorec sfilter where
traytel@54027
   216
  "shd (sfilter P s) = shd (sdrop_while (Not o P) s)"
traytel@54027
   217
| "stl (sfilter P s) = sfilter P (stl (sdrop_while (Not o P) s))"
traytel@52905
   218
traytel@52905
   219
lemma sfilter_Stream: "sfilter P (x ## s) = (if P x then x ## sfilter P s else sfilter P s)"
traytel@52905
   220
proof (cases "P x")
traytel@54027
   221
  case True thus ?thesis by (subst sfilter.ctr) (simp add: sdrop_while_Stream)
traytel@52905
   222
next
traytel@54027
   223
  case False thus ?thesis by (subst (1 2) sfilter.ctr) (simp add: sdrop_while_Stream)
traytel@52905
   224
qed
traytel@52905
   225
traytel@51141
   226
traytel@51141
   227
subsection {* unary predicates lifted to streams *}
traytel@51141
   228
traytel@51141
   229
definition "stream_all P s = (\<forall>p. P (s !! p))"
traytel@51141
   230
traytel@51772
   231
lemma stream_all_iff[iff]: "stream_all P s \<longleftrightarrow> Ball (sset s) P"
traytel@51772
   232
  unfolding stream_all_def sset_range by auto
traytel@51141
   233
traytel@51141
   234
lemma stream_all_shift[simp]: "stream_all P (xs @- s) = (list_all P xs \<and> stream_all P s)"
traytel@51141
   235
  unfolding stream_all_iff list_all_iff by auto
traytel@51141
   236
traytel@51141
   237
traytel@51141
   238
subsection {* recurring stream out of a list *}
traytel@51141
   239
traytel@54027
   240
primcorec cycle :: "'a list \<Rightarrow> 'a stream" where
traytel@54027
   241
  "shd (cycle xs) = hd xs"
traytel@54027
   242
| "stl (cycle xs) = cycle (tl xs @ [hd xs])"
traytel@51141
   243
lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u"
traytel@54027
   244
proof (coinduction arbitrary: u)
traytel@54027
   245
  case Eq_stream then show ?case using stream.collapse[of "cycle u"]
traytel@54027
   246
    by (auto intro!: exI[of _ "tl u @ [hd u]"])
traytel@54027
   247
qed
traytel@51141
   248
traytel@51409
   249
lemma cycle_Cons[code]: "cycle (x # xs) = x ## cycle (xs @ [x])"
traytel@54027
   250
  by (subst cycle.ctr) simp
traytel@50518
   251
traytel@50518
   252
lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @- s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @- s"
traytel@51141
   253
  by (auto dest: arg_cong[of _ _ stl])
traytel@50518
   254
traytel@50518
   255
lemma stake_append: "stake n (u @- s) = take (min (length u) n) u @ stake (n - length u) s"
traytel@50518
   256
proof (induct n arbitrary: u)
traytel@50518
   257
  case (Suc n) thus ?case by (cases u) auto
traytel@50518
   258
qed auto
traytel@50518
   259
traytel@50518
   260
lemma stake_cycle_le[simp]:
traytel@50518
   261
  assumes "u \<noteq> []" "n < length u"
traytel@50518
   262
  shows "stake n (cycle u) = take n u"
traytel@50518
   263
using min_absorb2[OF less_imp_le_nat[OF assms(2)]]
traytel@51141
   264
  by (subst cycle_decomp[OF assms(1)], subst stake_append) auto
traytel@50518
   265
traytel@50518
   266
lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u"
traytel@51141
   267
  by (metis cycle_decomp stake_shift)
traytel@50518
   268
traytel@50518
   269
lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u"
traytel@51141
   270
  by (metis cycle_decomp sdrop_shift)
traytel@50518
   271
traytel@50518
   272
lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
traytel@50518
   273
   stake n (cycle u) = concat (replicate (n div length u) u)"
traytel@51141
   274
  by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric])
traytel@50518
   275
traytel@50518
   276
lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
traytel@50518
   277
   sdrop n (cycle u) = cycle u"
traytel@51141
   278
  by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric])
traytel@50518
   279
traytel@50518
   280
lemma stake_cycle: "u \<noteq> [] \<Longrightarrow>
traytel@50518
   281
   stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u"
traytel@51141
   282
  by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto
traytel@50518
   283
traytel@50518
   284
lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)"
traytel@51141
   285
  by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric])
traytel@51141
   286
traytel@51141
   287
traytel@51141
   288
subsection {* stream repeating a single element *}
traytel@51141
   289
traytel@54027
   290
primcorec same where
traytel@54027
   291
  "shd (same x) = x"
traytel@54027
   292
| "stl (same x) = same x"
traytel@51141
   293
traytel@51141
   294
lemma snth_same[simp]: "same x !! n = x"
traytel@51141
   295
  unfolding same_def by (induct n) auto
traytel@51141
   296
traytel@51141
   297
lemma stake_same[simp]: "stake n (same x) = replicate n x"
traytel@51141
   298
  unfolding same_def by (induct n) (auto simp: upt_rec)
traytel@51141
   299
traytel@51141
   300
lemma sdrop_same[simp]: "sdrop n (same x) = same x"
traytel@51141
   301
  unfolding same_def by (induct n) auto
traytel@51141
   302
traytel@51141
   303
lemma shift_replicate_same[simp]: "replicate n x @- same x = same x"
traytel@51141
   304
  by (metis sdrop_same stake_same stake_sdrop)
traytel@51141
   305
traytel@51141
   306
lemma stream_all_same[simp]: "stream_all P (same x) \<longleftrightarrow> P x"
traytel@51141
   307
  unfolding stream_all_def by auto
traytel@51141
   308
traytel@51141
   309
lemma same_cycle: "same x = cycle [x]"
traytel@54027
   310
  by coinduction auto
traytel@51141
   311
traytel@51141
   312
traytel@51141
   313
subsection {* stream of natural numbers *}
traytel@51141
   314
traytel@54027
   315
primcorec fromN :: "nat \<Rightarrow> nat stream" where
traytel@54027
   316
  "fromN n = n ## fromN (n + 1)"
traytel@51409
   317
traytel@51141
   318
lemma snth_fromN[simp]: "fromN n !! m = n + m"
traytel@51141
   319
  unfolding fromN_def by (induct m arbitrary: n) auto
traytel@51141
   320
traytel@51141
   321
lemma stake_fromN[simp]: "stake m (fromN n) = [n ..< m + n]"
traytel@51141
   322
  unfolding fromN_def by (induct m arbitrary: n) (auto simp: upt_rec)
traytel@51141
   323
traytel@51141
   324
lemma sdrop_fromN[simp]: "sdrop m (fromN n) = fromN (n + m)"
traytel@51141
   325
  unfolding fromN_def by (induct m arbitrary: n) auto
traytel@51141
   326
traytel@51772
   327
lemma sset_fromN[simp]: "sset (fromN n) = {n ..}" (is "?L = ?R")
traytel@51352
   328
proof safe
traytel@54027
   329
  fix m assume "m \<in> ?L"
traytel@51352
   330
  moreover
traytel@51772
   331
  { fix s assume "m \<in> sset s" "\<exists>n'\<ge>n. s = fromN n'"
traytel@54027
   332
    hence "n \<le> m"  by (induct arbitrary: n rule: sset_induct1) fastforce+
traytel@51352
   333
  }
traytel@54027
   334
  ultimately show "n \<le> m" by auto
traytel@51352
   335
next
traytel@51772
   336
  fix m assume "n \<le> m" thus "m \<in> ?L" by (metis le_iff_add snth_fromN snth_sset)
traytel@51352
   337
qed
traytel@51352
   338
traytel@51141
   339
abbreviation "nats \<equiv> fromN 0"
traytel@51141
   340
traytel@51141
   341
traytel@51462
   342
subsection {* flatten a stream of lists *}
traytel@51462
   343
traytel@54027
   344
primcorec flat where
traytel@51462
   345
  "shd (flat ws) = hd (shd ws)"
traytel@54027
   346
| "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)"
traytel@51462
   347
traytel@51462
   348
lemma flat_Cons[simp, code]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)"
traytel@54027
   349
  by (subst flat.ctr) simp
traytel@51462
   350
traytel@51462
   351
lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws"
traytel@51462
   352
  by (induct xs) auto
traytel@51462
   353
traytel@51462
   354
lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)"
traytel@51462
   355
  by (cases ws) auto
traytel@51462
   356
traytel@51772
   357
lemma flat_snth: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow> flat s !! n = (if n < length (shd s) then 
traytel@51462
   358
  shd s ! n else flat (stl s) !! (n - length (shd s)))"
traytel@51772
   359
  by (metis flat_unfold not_less shd_sset shift_snth_ge shift_snth_less)
traytel@51462
   360
traytel@51772
   361
lemma sset_flat[simp]: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow> 
traytel@51772
   362
  sset (flat s) = (\<Union>xs \<in> sset s. set xs)" (is "?P \<Longrightarrow> ?L = ?R")
traytel@51462
   363
proof safe
traytel@51462
   364
  fix x assume ?P "x : ?L"
traytel@51772
   365
  then obtain m where "x = flat s !! m" by (metis image_iff sset_range)
traytel@51462
   366
  with `?P` obtain n m' where "x = s !! n ! m'" "m' < length (s !! n)"
traytel@51462
   367
  proof (atomize_elim, induct m arbitrary: s rule: less_induct)
traytel@51462
   368
    case (less y)
traytel@51462
   369
    thus ?case
traytel@51462
   370
    proof (cases "y < length (shd s)")
traytel@51462
   371
      case True thus ?thesis by (metis flat_snth less(2,3) snth.simps(1))
traytel@51462
   372
    next
traytel@51462
   373
      case False
traytel@51462
   374
      hence "x = flat (stl s) !! (y - length (shd s))" by (metis less(2,3) flat_snth)
traytel@51462
   375
      moreover
wenzelm@53374
   376
      { from less(2) have *: "length (shd s) > 0" by (cases s) simp_all
wenzelm@53374
   377
        with False have "y > 0" by (cases y) simp_all
wenzelm@53374
   378
        with * have "y - length (shd s) < y" by simp
traytel@51462
   379
      }
traytel@51772
   380
      moreover have "\<forall>xs \<in> sset (stl s). xs \<noteq> []" using less(2) by (cases s) auto
traytel@51462
   381
      ultimately have "\<exists>n m'. x = stl s !! n ! m' \<and> m' < length (stl s !! n)" by (intro less(1)) auto
traytel@51462
   382
      thus ?thesis by (metis snth.simps(2))
traytel@51462
   383
    qed
traytel@51462
   384
  qed
traytel@51772
   385
  thus "x \<in> ?R" by (auto simp: sset_range dest!: nth_mem)
traytel@51462
   386
next
traytel@51772
   387
  fix x xs assume "xs \<in> sset s" ?P "x \<in> set xs" thus "x \<in> ?L"
traytel@51772
   388
    by (induct rule: sset_induct1)
traytel@51772
   389
      (metis UnI1 flat_unfold shift.simps(1) sset_shift,
traytel@51772
   390
       metis UnI2 flat_unfold shd_sset stl_sset sset_shift)
traytel@51462
   391
qed
traytel@51462
   392
traytel@51462
   393
traytel@51462
   394
subsection {* merge a stream of streams *}
traytel@51462
   395
traytel@51462
   396
definition smerge :: "'a stream stream \<Rightarrow> 'a stream" where
traytel@51772
   397
  "smerge ss = flat (smap (\<lambda>n. map (\<lambda>s. s !! n) (stake (Suc n) ss) @ stake n (ss !! n)) nats)"
traytel@51462
   398
traytel@51462
   399
lemma stake_nth[simp]: "m < n \<Longrightarrow> stake n s ! m = s !! m"
traytel@51462
   400
  by (induct n arbitrary: s m) (auto simp: nth_Cons', metis Suc_pred snth.simps(2))
traytel@51462
   401
traytel@51772
   402
lemma snth_sset_smerge: "ss !! n !! m \<in> sset (smerge ss)"
traytel@51462
   403
proof (cases "n \<le> m")
traytel@51462
   404
  case False thus ?thesis unfolding smerge_def
traytel@51772
   405
    by (subst sset_flat)
blanchet@53290
   406
      (auto simp: stream.set_map in_set_conv_nth simp del: stake.simps
traytel@51462
   407
        intro!: exI[of _ n, OF disjI2] exI[of _ m, OF mp])
traytel@51462
   408
next
traytel@51462
   409
  case True thus ?thesis unfolding smerge_def
traytel@51772
   410
    by (subst sset_flat)
blanchet@53290
   411
      (auto simp: stream.set_map in_set_conv_nth image_iff simp del: stake.simps snth.simps
traytel@51462
   412
        intro!: exI[of _ m, OF disjI1] bexI[of _ "ss !! n"] exI[of _ n, OF mp])
traytel@51462
   413
qed
traytel@51462
   414
traytel@51772
   415
lemma sset_smerge: "sset (smerge ss) = UNION (sset ss) sset"
traytel@51462
   416
proof safe
traytel@51772
   417
  fix x assume "x \<in> sset (smerge ss)"
traytel@51772
   418
  thus "x \<in> UNION (sset ss) sset"
traytel@51772
   419
    unfolding smerge_def by (subst (asm) sset_flat)
blanchet@53290
   420
      (auto simp: stream.set_map in_set_conv_nth sset_range simp del: stake.simps, fast+)
traytel@51462
   421
next
traytel@51772
   422
  fix s x assume "s \<in> sset ss" "x \<in> sset s"
traytel@51772
   423
  thus "x \<in> sset (smerge ss)" using snth_sset_smerge by (auto simp: sset_range)
traytel@51462
   424
qed
traytel@51462
   425
traytel@51462
   426
traytel@51462
   427
subsection {* product of two streams *}
traytel@51462
   428
traytel@51462
   429
definition sproduct :: "'a stream \<Rightarrow> 'b stream \<Rightarrow> ('a \<times> 'b) stream" where
traytel@51772
   430
  "sproduct s1 s2 = smerge (smap (\<lambda>x. smap (Pair x) s2) s1)"
traytel@51462
   431
traytel@51772
   432
lemma sset_sproduct: "sset (sproduct s1 s2) = sset s1 \<times> sset s2"
blanchet@53290
   433
  unfolding sproduct_def sset_smerge by (auto simp: stream.set_map)
traytel@51462
   434
traytel@51462
   435
traytel@51462
   436
subsection {* interleave two streams *}
traytel@51462
   437
traytel@54027
   438
primcorec sinterleave where
traytel@54027
   439
  "shd (sinterleave s1 s2) = shd s1"
traytel@54027
   440
| "stl (sinterleave s1 s2) = sinterleave s2 (stl s1)"
traytel@51462
   441
traytel@51462
   442
lemma sinterleave_code[code]:
traytel@51462
   443
  "sinterleave (x ## s1) s2 = x ## sinterleave s2 s1"
traytel@54027
   444
  by (subst sinterleave.ctr) simp
traytel@51462
   445
traytel@51462
   446
lemma sinterleave_snth[simp]:
traytel@51462
   447
  "even n \<Longrightarrow> sinterleave s1 s2 !! n = s1 !! (n div 2)"
traytel@51462
   448
   "odd n \<Longrightarrow> sinterleave s1 s2 !! n = s2 !! (n div 2)"
traytel@51462
   449
  by (induct n arbitrary: s1 s2)
traytel@51462
   450
    (auto dest: even_nat_Suc_div_2 odd_nat_plus_one_div_two[folded nat_2])
traytel@51462
   451
traytel@51772
   452
lemma sset_sinterleave: "sset (sinterleave s1 s2) = sset s1 \<union> sset s2"
traytel@51462
   453
proof (intro equalityI subsetI)
traytel@51772
   454
  fix x assume "x \<in> sset (sinterleave s1 s2)"
traytel@51772
   455
  then obtain n where "x = sinterleave s1 s2 !! n" unfolding sset_range by blast
traytel@51772
   456
  thus "x \<in> sset s1 \<union> sset s2" by (cases "even n") auto
traytel@51462
   457
next
traytel@51772
   458
  fix x assume "x \<in> sset s1 \<union> sset s2"
traytel@51772
   459
  thus "x \<in> sset (sinterleave s1 s2)"
traytel@51462
   460
  proof
traytel@51772
   461
    assume "x \<in> sset s1"
traytel@51772
   462
    then obtain n where "x = s1 !! n" unfolding sset_range by blast
traytel@51462
   463
    hence "sinterleave s1 s2 !! (2 * n) = x" by simp
traytel@51772
   464
    thus ?thesis unfolding sset_range by blast
traytel@51462
   465
  next
traytel@51772
   466
    assume "x \<in> sset s2"
traytel@51772
   467
    then obtain n where "x = s2 !! n" unfolding sset_range by blast
traytel@51462
   468
    hence "sinterleave s1 s2 !! (2 * n + 1) = x" by simp
traytel@51772
   469
    thus ?thesis unfolding sset_range by blast
traytel@51462
   470
  qed
traytel@51462
   471
qed
traytel@51462
   472
traytel@51462
   473
traytel@51141
   474
subsection {* zip *}
traytel@51141
   475
traytel@54027
   476
primcorec szip where
traytel@54027
   477
  "shd (szip s1 s2) = (shd s1, shd s2)"
traytel@54027
   478
| "stl (szip s1 s2) = szip (stl s1) (stl s2)"
traytel@51141
   479
traytel@51409
   480
lemma szip_unfold[code]: "szip (Stream a s1) (Stream b s2) = Stream (a, b) (szip s1 s2)"
traytel@54027
   481
  by (subst szip.ctr) simp
traytel@51409
   482
traytel@51141
   483
lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)"
traytel@51141
   484
  by (induct n arbitrary: s1 s2) auto
traytel@51141
   485
traytel@51141
   486
traytel@51141
   487
subsection {* zip via function *}
traytel@51141
   488
traytel@54027
   489
primcorec smap2 where
traytel@51772
   490
  "shd (smap2 f s1 s2) = f (shd s1) (shd s2)"
traytel@54027
   491
| "stl (smap2 f s1 s2) = smap2 f (stl s1) (stl s2)"
traytel@51141
   492
traytel@51772
   493
lemma smap2_unfold[code]:
traytel@51772
   494
  "smap2 f (Stream a s1) (Stream b s2) = Stream (f a b) (smap2 f s1 s2)"
traytel@54027
   495
  by (subst smap2.ctr) simp
traytel@51409
   496
traytel@51772
   497
lemma smap2_szip:
traytel@51772
   498
  "smap2 f s1 s2 = smap (split f) (szip s1 s2)"
traytel@54027
   499
  by (coinduction arbitrary: s1 s2) auto
traytel@50518
   500
traytel@51462
   501
traytel@51462
   502
subsection {* iterated application of a function *}
traytel@51462
   503
traytel@54027
   504
primcorec siterate where
traytel@54027
   505
  "shd (siterate f x) = x"
traytel@54027
   506
| "stl (siterate f x) = siterate f (f x)"
traytel@51462
   507
traytel@51462
   508
lemma stake_Suc: "stake (Suc n) s = stake n s @ [s !! n]"
traytel@51462
   509
  by (induct n arbitrary: s) auto
traytel@51462
   510
traytel@51462
   511
lemma snth_siterate[simp]: "siterate f x !! n = (f^^n) x"
traytel@51462
   512
  by (induct n arbitrary: x) (auto simp: funpow_swap1)
traytel@51462
   513
traytel@51462
   514
lemma sdrop_siterate[simp]: "sdrop n (siterate f x) = siterate f ((f^^n) x)"
traytel@51462
   515
  by (induct n arbitrary: x) (auto simp: funpow_swap1)
traytel@51462
   516
traytel@51462
   517
lemma stake_siterate[simp]: "stake n (siterate f x) = map (\<lambda>n. (f^^n) x) [0 ..< n]"
traytel@51462
   518
  by (induct n arbitrary: x) (auto simp del: stake.simps(2) simp: stake_Suc)
traytel@51462
   519
traytel@51772
   520
lemma sset_siterate: "sset (siterate f x) = {(f^^n) x | n. True}"
traytel@51772
   521
  by (auto simp: sset_range)
traytel@51462
   522
traytel@50518
   523
end