src/HOL/Library/Stream.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (21 months ago)
changeset 67003 49850a679c2c
parent 65366 10ca63a18e56
child 67091 1393c2340eec
permissions -rw-r--r--
more robust sorted_entries;
hoelzl@58607
     1
(*  Title:      HOL/Library/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
wenzelm@60500
     9
section \<open>Infinite Streams\<close>
traytel@50518
    10
traytel@50518
    11
theory Stream
wenzelm@65366
    12
  imports Nat_Bijection
traytel@50518
    13
begin
traytel@50518
    14
blanchet@57206
    15
codatatype (sset: 'a) stream =
traytel@54720
    16
  SCons (shd: 'a) (stl: "'a stream") (infixr "##" 65)
blanchet@57206
    17
for
blanchet@57206
    18
  map: smap
blanchet@57206
    19
  rel: stream_all2
traytel@51409
    20
wenzelm@60011
    21
context
wenzelm@60011
    22
begin
wenzelm@60011
    23
traytel@51462
    24
(*for code generation only*)
wenzelm@60011
    25
qualified definition smember :: "'a \<Rightarrow> 'a stream \<Rightarrow> bool" where
traytel@51772
    26
  [code_abbrev]: "smember x s \<longleftrightarrow> x \<in> sset s"
traytel@51462
    27
traytel@54720
    28
lemma smember_code[code, simp]: "smember x (y ## s) = (if x = y then True else smember x s)"
traytel@51462
    29
  unfolding smember_def by auto
traytel@51462
    30
wenzelm@60011
    31
end
traytel@51462
    32
blanchet@57983
    33
lemmas smap_simps[simp] = stream.map_sel
blanchet@57983
    34
lemmas shd_sset = stream.set_sel(1)
blanchet@57983
    35
lemmas stl_sset = stream.set_sel(2)
traytel@50518
    36
blanchet@57986
    37
theorem sset_induct[consumes 1, case_names shd stl, induct set: sset]:
blanchet@57986
    38
  assumes "y \<in> sset s" and "\<And>s. P (shd s) s" and "\<And>s y. \<lbrakk>y \<in> sset (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s"
traytel@50518
    39
  shows "P y s"
blanchet@57986
    40
using assms by induct (metis stream.sel(1), auto)
traytel@50518
    41
hoelzl@59000
    42
lemma smap_ctr: "smap f s = x ## s' \<longleftrightarrow> f (shd s) = x \<and> smap f (stl s) = s'"
hoelzl@59000
    43
  by (cases s) simp
traytel@50518
    44
wenzelm@60500
    45
subsection \<open>prepend list to stream\<close>
traytel@50518
    46
traytel@50518
    47
primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr "@-" 65) where
traytel@50518
    48
  "shift [] s = s"
traytel@51023
    49
| "shift (x # xs) s = x ## shift xs s"
traytel@50518
    50
traytel@51772
    51
lemma smap_shift[simp]: "smap f (xs @- s) = map f xs @- smap f s"
traytel@51353
    52
  by (induct xs) auto
traytel@51353
    53
traytel@50518
    54
lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s"
traytel@51141
    55
  by (induct xs) auto
traytel@50518
    56
traytel@50518
    57
lemma shift_simps[simp]:
traytel@50518
    58
   "shd (xs @- s) = (if xs = [] then shd s else hd xs)"
traytel@50518
    59
   "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)"
traytel@51141
    60
  by (induct xs) auto
traytel@50518
    61
traytel@51772
    62
lemma sset_shift[simp]: "sset (xs @- s) = set xs \<union> sset s"
traytel@51141
    63
  by (induct xs) auto
traytel@50518
    64
traytel@51352
    65
lemma shift_left_inj[simp]: "xs @- s1 = xs @- s2 \<longleftrightarrow> s1 = s2"
traytel@51352
    66
  by (induct xs) auto
traytel@51352
    67
traytel@50518
    68
wenzelm@60500
    69
subsection \<open>set of streams with elements in some fixed set\<close>
traytel@50518
    70
wenzelm@61681
    71
context
wenzelm@62093
    72
  notes [[inductive_internals]]
wenzelm@61681
    73
begin
wenzelm@61681
    74
traytel@50518
    75
coinductive_set
hoelzl@54469
    76
  streams :: "'a set \<Rightarrow> 'a stream set"
traytel@50518
    77
  for A :: "'a set"
traytel@50518
    78
where
traytel@51023
    79
  Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> a ## s \<in> streams A"
traytel@50518
    80
wenzelm@61681
    81
end
wenzelm@61681
    82
hoelzl@59000
    83
lemma in_streams: "stl s \<in> streams S \<Longrightarrow> shd s \<in> S \<Longrightarrow> s \<in> streams S"
hoelzl@59000
    84
  by (cases s) auto
hoelzl@59000
    85
hoelzl@59000
    86
lemma streamsE: "s \<in> streams A \<Longrightarrow> (shd s \<in> A \<Longrightarrow> stl s \<in> streams A \<Longrightarrow> P) \<Longrightarrow> P"
hoelzl@59000
    87
  by (erule streams.cases) simp_all
hoelzl@59000
    88
hoelzl@59000
    89
lemma Stream_image: "x ## y \<in> (op ## x') ` Y \<longleftrightarrow> x = x' \<and> y \<in> Y"
hoelzl@59000
    90
  by auto
hoelzl@59000
    91
traytel@50518
    92
lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A"
traytel@51141
    93
  by (induct w) auto
traytel@50518
    94
hoelzl@54469
    95
lemma streams_Stream: "x ## s \<in> streams A \<longleftrightarrow> x \<in> A \<and> s \<in> streams A"
hoelzl@54469
    96
  by (auto elim: streams.cases)
hoelzl@54469
    97
hoelzl@54469
    98
lemma streams_stl: "s \<in> streams A \<Longrightarrow> stl s \<in> streams A"
hoelzl@54469
    99
  by (cases s) (auto simp: streams_Stream)
hoelzl@54469
   100
hoelzl@54469
   101
lemma streams_shd: "s \<in> streams A \<Longrightarrow> shd s \<in> A"
hoelzl@54469
   102
  by (cases s) (auto simp: streams_Stream)
hoelzl@54469
   103
traytel@51772
   104
lemma sset_streams:
traytel@51772
   105
  assumes "sset s \<subseteq> A"
traytel@50518
   106
  shows "s \<in> streams A"
traytel@54027
   107
using assms proof (coinduction arbitrary: s)
traytel@54027
   108
  case streams then show ?case by (cases s) simp
traytel@50518
   109
qed
traytel@50518
   110
hoelzl@54469
   111
lemma streams_sset:
hoelzl@54469
   112
  assumes "s \<in> streams A"
hoelzl@54469
   113
  shows "sset s \<subseteq> A"
hoelzl@54469
   114
proof
wenzelm@60500
   115
  fix x assume "x \<in> sset s" from this \<open>s \<in> streams A\<close> show "x \<in> A"
hoelzl@54469
   116
    by (induct s) (auto intro: streams_shd streams_stl)
hoelzl@54469
   117
qed
hoelzl@54469
   118
hoelzl@54469
   119
lemma streams_iff_sset: "s \<in> streams A \<longleftrightarrow> sset s \<subseteq> A"
hoelzl@54469
   120
  by (metis sset_streams streams_sset)
hoelzl@54469
   121
hoelzl@54469
   122
lemma streams_mono:  "s \<in> streams A \<Longrightarrow> A \<subseteq> B \<Longrightarrow> s \<in> streams B"
hoelzl@54469
   123
  unfolding streams_iff_sset by auto
hoelzl@54469
   124
hoelzl@59000
   125
lemma streams_mono2: "S \<subseteq> T \<Longrightarrow> streams S \<subseteq> streams T"
hoelzl@59000
   126
  by (auto intro: streams_mono)
hoelzl@59000
   127
hoelzl@54469
   128
lemma smap_streams: "s \<in> streams A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> f x \<in> B) \<Longrightarrow> smap f s \<in> streams B"
hoelzl@54469
   129
  unfolding streams_iff_sset stream.set_map by auto
hoelzl@54469
   130
hoelzl@54469
   131
lemma streams_empty: "streams {} = {}"
hoelzl@54469
   132
  by (auto elim: streams.cases)
hoelzl@54469
   133
hoelzl@54469
   134
lemma streams_UNIV[simp]: "streams UNIV = UNIV"
hoelzl@54469
   135
  by (auto simp: streams_iff_sset)
traytel@50518
   136
wenzelm@60500
   137
subsection \<open>nth, take, drop for streams\<close>
traytel@51141
   138
traytel@51141
   139
primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where
traytel@51141
   140
  "s !! 0 = shd s"
traytel@51141
   141
| "s !! Suc n = stl s !! n"
traytel@51141
   142
hoelzl@59000
   143
lemma snth_Stream: "(x ## s) !! Suc i = s !! i"
hoelzl@59000
   144
  by simp
hoelzl@59000
   145
traytel@51772
   146
lemma snth_smap[simp]: "smap f s !! n = f (s !! n)"
traytel@51141
   147
  by (induct n arbitrary: s) auto
traytel@51141
   148
traytel@51141
   149
lemma shift_snth_less[simp]: "p < length xs \<Longrightarrow> (xs @- s) !! p = xs ! p"
traytel@51141
   150
  by (induct p arbitrary: xs) (auto simp: hd_conv_nth nth_tl)
traytel@51141
   151
traytel@51141
   152
lemma shift_snth_ge[simp]: "p \<ge> length xs \<Longrightarrow> (xs @- s) !! p = s !! (p - length xs)"
traytel@51141
   153
  by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred)
traytel@51141
   154
traytel@57175
   155
lemma shift_snth: "(xs @- s) !! n = (if n < length xs then xs ! n else s !! (n - length xs))"
traytel@57175
   156
  by auto
traytel@57175
   157
traytel@51772
   158
lemma snth_sset[simp]: "s !! n \<in> sset s"
traytel@51772
   159
  by (induct n arbitrary: s) (auto intro: shd_sset stl_sset)
traytel@51141
   160
traytel@51772
   161
lemma sset_range: "sset s = range (snth s)"
traytel@51141
   162
proof (intro equalityI subsetI)
traytel@51772
   163
  fix x assume "x \<in> sset s"
traytel@51141
   164
  thus "x \<in> range (snth s)"
traytel@51141
   165
  proof (induct s)
traytel@51141
   166
    case (stl s x)
traytel@51141
   167
    then obtain n where "x = stl s !! n" by auto
traytel@51141
   168
    thus ?case by (auto intro: range_eqI[of _ _ "Suc n"])
traytel@51141
   169
  qed (auto intro: range_eqI[of _ _ 0])
traytel@51141
   170
qed auto
traytel@50518
   171
hoelzl@59000
   172
lemma streams_iff_snth: "s \<in> streams X \<longleftrightarrow> (\<forall>n. s !! n \<in> X)"
hoelzl@59000
   173
  by (force simp: streams_iff_sset sset_range)
hoelzl@59000
   174
hoelzl@59000
   175
lemma snth_in: "s \<in> streams X \<Longrightarrow> s !! n \<in> X"
hoelzl@59000
   176
  by (simp add: streams_iff_snth)
hoelzl@59000
   177
traytel@50518
   178
primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
traytel@50518
   179
  "stake 0 s = []"
traytel@50518
   180
| "stake (Suc n) s = shd s # stake n (stl s)"
traytel@50518
   181
traytel@51141
   182
lemma length_stake[simp]: "length (stake n s) = n"
traytel@51141
   183
  by (induct n arbitrary: s) auto
traytel@51141
   184
traytel@51772
   185
lemma stake_smap[simp]: "stake n (smap f s) = map f (stake n s)"
traytel@51141
   186
  by (induct n arbitrary: s) auto
traytel@51141
   187
traytel@57175
   188
lemma take_stake: "take n (stake m s) = stake (min n m) s"
traytel@57175
   189
proof (induct m arbitrary: s n)
traytel@57175
   190
  case (Suc m) thus ?case by (cases n) auto
traytel@57175
   191
qed simp
traytel@57175
   192
traytel@50518
   193
primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
traytel@50518
   194
  "sdrop 0 s = s"
traytel@50518
   195
| "sdrop (Suc n) s = sdrop n (stl s)"
traytel@50518
   196
traytel@51141
   197
lemma sdrop_simps[simp]:
traytel@51141
   198
  "shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s"
traytel@51141
   199
  by (induct n arbitrary: s)  auto
traytel@51141
   200
traytel@51772
   201
lemma sdrop_smap[simp]: "sdrop n (smap f s) = smap f (sdrop n s)"
traytel@51141
   202
  by (induct n arbitrary: s) auto
traytel@50518
   203
traytel@51352
   204
lemma sdrop_stl: "sdrop n (stl s) = stl (sdrop n s)"
traytel@51352
   205
  by (induct n) auto
traytel@51352
   206
traytel@57175
   207
lemma drop_stake: "drop n (stake m s) = stake (m - n) (sdrop n s)"
traytel@57175
   208
proof (induct m arbitrary: s n)
traytel@57175
   209
  case (Suc m) thus ?case by (cases n) auto
traytel@57175
   210
qed simp
traytel@57175
   211
traytel@50518
   212
lemma stake_sdrop: "stake n s @- sdrop n s = s"
traytel@51141
   213
  by (induct n arbitrary: s) auto
traytel@51141
   214
traytel@51141
   215
lemma id_stake_snth_sdrop:
traytel@51141
   216
  "s = stake i s @- s !! i ## sdrop (Suc i) s"
traytel@51141
   217
  by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse)
traytel@50518
   218
traytel@51772
   219
lemma smap_alt: "smap f s = s' \<longleftrightarrow> (\<forall>n. f (s !! n) = s' !! n)" (is "?L = ?R")
traytel@51141
   220
proof
traytel@51141
   221
  assume ?R
traytel@54027
   222
  then have "\<And>n. smap f (sdrop n s) = sdrop n s'"
traytel@54027
   223
    by coinduction (auto intro: exI[of _ 0] simp del: sdrop.simps(2))
traytel@54027
   224
  then show ?L using sdrop.simps(1) by metis
traytel@51141
   225
qed auto
traytel@51141
   226
traytel@51141
   227
lemma stake_invert_Nil[iff]: "stake n s = [] \<longleftrightarrow> n = 0"
traytel@51141
   228
  by (induct n) auto
traytel@50518
   229
traytel@57175
   230
lemma sdrop_shift: "sdrop i (w @- s) = drop i w @- sdrop (i - length w) s"
traytel@57175
   231
  by (induct i arbitrary: w s) (auto simp: drop_tl drop_Suc neq_Nil_conv)
traytel@50518
   232
traytel@57175
   233
lemma stake_shift: "stake i (w @- s) = take i w @ stake (i - length w) s"
traytel@57175
   234
  by (induct i arbitrary: w s) (auto simp: neq_Nil_conv)
traytel@50518
   235
traytel@50518
   236
lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s"
traytel@51141
   237
  by (induct m arbitrary: s) auto
traytel@50518
   238
traytel@50518
   239
lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"
traytel@51141
   240
  by (induct m arbitrary: s) auto
traytel@51141
   241
traytel@57175
   242
lemma sdrop_snth: "sdrop n s !! m = s !! (n + m)"
traytel@57175
   243
  by (induct n arbitrary: m s) auto
traytel@57175
   244
hoelzl@64320
   245
partial_function (tailrec) sdrop_while :: "('a \<Rightarrow> bool) \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
traytel@51430
   246
  "sdrop_while P s = (if P (shd s) then sdrop_while P (stl s) else s)"
traytel@51430
   247
traytel@54720
   248
lemma sdrop_while_SCons[code]:
traytel@54720
   249
  "sdrop_while P (a ## s) = (if P a then sdrop_while P s else a ## s)"
traytel@51430
   250
  by (subst sdrop_while.simps) simp
traytel@51430
   251
traytel@51430
   252
lemma sdrop_while_sdrop_LEAST:
traytel@51430
   253
  assumes "\<exists>n. P (s !! n)"
traytel@51430
   254
  shows "sdrop_while (Not o P) s = sdrop (LEAST n. P (s !! n)) s"
traytel@51430
   255
proof -
traytel@51430
   256
  from assms obtain m where "P (s !! m)" "\<And>n. P (s !! n) \<Longrightarrow> m \<le> n"
traytel@51430
   257
    and *: "(LEAST n. P (s !! n)) = m" by atomize_elim (auto intro: LeastI Least_le)
traytel@51430
   258
  thus ?thesis unfolding *
traytel@51430
   259
  proof (induct m arbitrary: s)
traytel@51430
   260
    case (Suc m)
traytel@51430
   261
    hence "sdrop_while (Not \<circ> P) (stl s) = sdrop m (stl s)"
traytel@51430
   262
      by (metis (full_types) not_less_eq_eq snth.simps(2))
traytel@51430
   263
    moreover from Suc(3) have "\<not> (P (s !! 0))" by blast
traytel@51430
   264
    ultimately show ?case by (subst sdrop_while.simps) simp
traytel@51430
   265
  qed (metis comp_apply sdrop.simps(1) sdrop_while.simps snth.simps(1))
traytel@51430
   266
qed
traytel@51430
   267
traytel@54027
   268
primcorec sfilter where
traytel@54027
   269
  "shd (sfilter P s) = shd (sdrop_while (Not o P) s)"
traytel@54027
   270
| "stl (sfilter P s) = sfilter P (stl (sdrop_while (Not o P) s))"
traytel@52905
   271
traytel@52905
   272
lemma sfilter_Stream: "sfilter P (x ## s) = (if P x then x ## sfilter P s else sfilter P s)"
traytel@52905
   273
proof (cases "P x")
traytel@54720
   274
  case True thus ?thesis by (subst sfilter.ctr) (simp add: sdrop_while_SCons)
traytel@52905
   275
next
traytel@54720
   276
  case False thus ?thesis by (subst (1 2) sfilter.ctr) (simp add: sdrop_while_SCons)
traytel@52905
   277
qed
traytel@52905
   278
traytel@51141
   279
wenzelm@60500
   280
subsection \<open>unary predicates lifted to streams\<close>
traytel@51141
   281
traytel@51141
   282
definition "stream_all P s = (\<forall>p. P (s !! p))"
traytel@51141
   283
traytel@51772
   284
lemma stream_all_iff[iff]: "stream_all P s \<longleftrightarrow> Ball (sset s) P"
traytel@51772
   285
  unfolding stream_all_def sset_range by auto
traytel@51141
   286
traytel@51141
   287
lemma stream_all_shift[simp]: "stream_all P (xs @- s) = (list_all P xs \<and> stream_all P s)"
traytel@51141
   288
  unfolding stream_all_iff list_all_iff by auto
traytel@51141
   289
hoelzl@54469
   290
lemma stream_all_Stream: "stream_all P (x ## X) \<longleftrightarrow> P x \<and> stream_all P X"
hoelzl@54469
   291
  by simp
hoelzl@54469
   292
traytel@51141
   293
wenzelm@60500
   294
subsection \<open>recurring stream out of a list\<close>
traytel@51141
   295
traytel@54027
   296
primcorec cycle :: "'a list \<Rightarrow> 'a stream" where
traytel@54027
   297
  "shd (cycle xs) = hd xs"
traytel@54027
   298
| "stl (cycle xs) = cycle (tl xs @ [hd xs])"
traytel@54720
   299
traytel@51141
   300
lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u"
traytel@54027
   301
proof (coinduction arbitrary: u)
traytel@54027
   302
  case Eq_stream then show ?case using stream.collapse[of "cycle u"]
traytel@54027
   303
    by (auto intro!: exI[of _ "tl u @ [hd u]"])
traytel@54027
   304
qed
traytel@51141
   305
traytel@51409
   306
lemma cycle_Cons[code]: "cycle (x # xs) = x ## cycle (xs @ [x])"
traytel@54027
   307
  by (subst cycle.ctr) simp
traytel@50518
   308
traytel@50518
   309
lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @- s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @- s"
traytel@51141
   310
  by (auto dest: arg_cong[of _ _ stl])
traytel@50518
   311
traytel@50518
   312
lemma stake_append: "stake n (u @- s) = take (min (length u) n) u @ stake (n - length u) s"
traytel@50518
   313
proof (induct n arbitrary: u)
traytel@50518
   314
  case (Suc n) thus ?case by (cases u) auto
traytel@50518
   315
qed auto
traytel@50518
   316
traytel@50518
   317
lemma stake_cycle_le[simp]:
traytel@50518
   318
  assumes "u \<noteq> []" "n < length u"
traytel@50518
   319
  shows "stake n (cycle u) = take n u"
traytel@50518
   320
using min_absorb2[OF less_imp_le_nat[OF assms(2)]]
traytel@51141
   321
  by (subst cycle_decomp[OF assms(1)], subst stake_append) auto
traytel@50518
   322
traytel@50518
   323
lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u"
traytel@57175
   324
  by (subst cycle_decomp) (auto simp: stake_shift)
traytel@50518
   325
traytel@50518
   326
lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u"
traytel@57175
   327
  by (subst cycle_decomp) (auto simp: sdrop_shift)
traytel@50518
   328
traytel@50518
   329
lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
traytel@50518
   330
   stake n (cycle u) = concat (replicate (n div length u) u)"
traytel@51141
   331
  by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric])
traytel@50518
   332
traytel@50518
   333
lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
traytel@50518
   334
   sdrop n (cycle u) = cycle u"
traytel@51141
   335
  by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric])
traytel@50518
   336
traytel@50518
   337
lemma stake_cycle: "u \<noteq> [] \<Longrightarrow>
traytel@50518
   338
   stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u"
haftmann@64242
   339
  by (subst div_mult_mod_eq[of n "length u", symmetric], unfold stake_add[symmetric]) auto
traytel@50518
   340
traytel@50518
   341
lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)"
traytel@51141
   342
  by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric])
traytel@51141
   343
traytel@63192
   344
lemma sset_cycle[simp]:
hoelzl@64320
   345
  assumes "xs \<noteq> []"
traytel@63192
   346
  shows "sset (cycle xs) = set xs"
traytel@63192
   347
proof (intro set_eqI iffI)
traytel@63192
   348
  fix x
traytel@63192
   349
  assume "x \<in> sset (cycle xs)"
traytel@63192
   350
  then show "x \<in> set xs" using assms
traytel@63192
   351
    by (induction "cycle xs" arbitrary: xs rule: sset_induct) (fastforce simp: neq_Nil_conv)+
traytel@63192
   352
qed (metis assms UnI1 cycle_decomp sset_shift)
traytel@63192
   353
traytel@51141
   354
wenzelm@60500
   355
subsection \<open>iterated application of a function\<close>
hoelzl@54497
   356
hoelzl@54497
   357
primcorec siterate where
hoelzl@54497
   358
  "shd (siterate f x) = x"
hoelzl@54497
   359
| "stl (siterate f x) = siterate f (f x)"
hoelzl@54497
   360
hoelzl@54497
   361
lemma stake_Suc: "stake (Suc n) s = stake n s @ [s !! n]"
hoelzl@54497
   362
  by (induct n arbitrary: s) auto
hoelzl@54497
   363
hoelzl@54497
   364
lemma snth_siterate[simp]: "siterate f x !! n = (f^^n) x"
hoelzl@54497
   365
  by (induct n arbitrary: x) (auto simp: funpow_swap1)
hoelzl@54497
   366
hoelzl@54497
   367
lemma sdrop_siterate[simp]: "sdrop n (siterate f x) = siterate f ((f^^n) x)"
hoelzl@54497
   368
  by (induct n arbitrary: x) (auto simp: funpow_swap1)
hoelzl@54497
   369
hoelzl@54497
   370
lemma stake_siterate[simp]: "stake n (siterate f x) = map (\<lambda>n. (f^^n) x) [0 ..< n]"
hoelzl@54497
   371
  by (induct n arbitrary: x) (auto simp del: stake.simps(2) simp: stake_Suc)
hoelzl@54497
   372
hoelzl@54497
   373
lemma sset_siterate: "sset (siterate f x) = {(f^^n) x | n. True}"
hoelzl@54497
   374
  by (auto simp: sset_range)
hoelzl@54497
   375
hoelzl@54497
   376
lemma smap_siterate: "smap f (siterate f x) = siterate f (f x)"
hoelzl@54497
   377
  by (coinduction arbitrary: x) auto
hoelzl@54497
   378
hoelzl@54497
   379
wenzelm@60500
   380
subsection \<open>stream repeating a single element\<close>
traytel@51141
   381
hoelzl@54497
   382
abbreviation "sconst \<equiv> siterate id"
traytel@51141
   383
hoelzl@54497
   384
lemma shift_replicate_sconst[simp]: "replicate n x @- sconst x = sconst x"
hoelzl@54497
   385
  by (subst (3) stake_sdrop[symmetric]) (simp add: map_replicate_trivial)
traytel@51141
   386
traytel@57175
   387
lemma sset_sconst[simp]: "sset (sconst x) = {x}"
hoelzl@54497
   388
  by (simp add: sset_siterate)
traytel@51141
   389
traytel@57175
   390
lemma sconst_alt: "s = sconst x \<longleftrightarrow> sset s = {x}"
traytel@57175
   391
proof
traytel@57175
   392
  assume "sset s = {x}"
traytel@57175
   393
  then show "s = sconst x"
traytel@57175
   394
  proof (coinduction arbitrary: s)
traytel@57175
   395
    case Eq_stream
wenzelm@63649
   396
    then have "shd s = x" "sset (stl s) \<subseteq> {x}" by (cases s; auto)+
traytel@57175
   397
    then have "sset (stl s) = {x}" by (cases "stl s") auto
wenzelm@60500
   398
    with \<open>shd s = x\<close> show ?case by auto
traytel@57175
   399
  qed
traytel@57175
   400
qed simp
traytel@57175
   401
traytel@59016
   402
lemma sconst_cycle: "sconst x = cycle [x]"
hoelzl@54497
   403
  by coinduction auto
traytel@51141
   404
hoelzl@54497
   405
lemma smap_sconst: "smap f (sconst x) = sconst (f x)"
hoelzl@54497
   406
  by coinduction auto
traytel@51141
   407
hoelzl@54497
   408
lemma sconst_streams: "x \<in> A \<Longrightarrow> sconst x \<in> streams A"
hoelzl@54497
   409
  by (simp add: streams_iff_sset)
traytel@51141
   410
hoelzl@64320
   411
lemma streams_empty_iff: "streams S = {} \<longleftrightarrow> S = {}"
hoelzl@64320
   412
proof safe
hoelzl@64320
   413
  fix x assume "x \<in> S" "streams S = {}"
hoelzl@64320
   414
  then have "sconst x \<in> streams S"
hoelzl@64320
   415
    by (intro sconst_streams)
hoelzl@64320
   416
  then show "x \<in> {}"
hoelzl@64320
   417
    unfolding \<open>streams S = {}\<close> by simp
hoelzl@64320
   418
qed (auto simp: streams_empty)
traytel@51141
   419
wenzelm@60500
   420
subsection \<open>stream of natural numbers\<close>
traytel@51141
   421
hoelzl@54497
   422
abbreviation "fromN \<equiv> siterate Suc"
hoelzl@54469
   423
traytel@51141
   424
abbreviation "nats \<equiv> fromN 0"
traytel@51141
   425
hoelzl@54497
   426
lemma sset_fromN[simp]: "sset (fromN n) = {n ..}"
traytel@54720
   427
  by (auto simp add: sset_siterate le_iff_add)
hoelzl@54497
   428
traytel@57175
   429
lemma stream_smap_fromN: "s = smap (\<lambda>j. let i = j - n in s !! i) (fromN n)"
traytel@57175
   430
  by (coinduction arbitrary: s n)
traytel@57175
   431
    (force simp: neq_Nil_conv Let_def snth.simps(2)[symmetric] Suc_diff_Suc
traytel@57175
   432
      intro: stream.map_cong split: if_splits simp del: snth.simps(2))
traytel@57175
   433
traytel@57175
   434
lemma stream_smap_nats: "s = smap (snth s) nats"
traytel@57175
   435
  using stream_smap_fromN[where n = 0] by simp
traytel@57175
   436
traytel@51141
   437
wenzelm@60500
   438
subsection \<open>flatten a stream of lists\<close>
traytel@51462
   439
traytel@54027
   440
primcorec flat where
traytel@51462
   441
  "shd (flat ws) = hd (shd ws)"
traytel@54027
   442
| "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)"
traytel@51462
   443
traytel@51462
   444
lemma flat_Cons[simp, code]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)"
traytel@54027
   445
  by (subst flat.ctr) simp
traytel@51462
   446
traytel@51462
   447
lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws"
traytel@51462
   448
  by (induct xs) auto
traytel@51462
   449
traytel@51462
   450
lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)"
traytel@51462
   451
  by (cases ws) auto
traytel@51462
   452
hoelzl@64320
   453
lemma flat_snth: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow> flat s !! n = (if n < length (shd s) then
traytel@51462
   454
  shd s ! n else flat (stl s) !! (n - length (shd s)))"
traytel@51772
   455
  by (metis flat_unfold not_less shd_sset shift_snth_ge shift_snth_less)
traytel@51462
   456
hoelzl@64320
   457
lemma sset_flat[simp]: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow>
traytel@51772
   458
  sset (flat s) = (\<Union>xs \<in> sset s. set xs)" (is "?P \<Longrightarrow> ?L = ?R")
traytel@51462
   459
proof safe
traytel@51462
   460
  fix x assume ?P "x : ?L"
traytel@51772
   461
  then obtain m where "x = flat s !! m" by (metis image_iff sset_range)
wenzelm@60500
   462
  with \<open>?P\<close> obtain n m' where "x = s !! n ! m'" "m' < length (s !! n)"
traytel@51462
   463
  proof (atomize_elim, induct m arbitrary: s rule: less_induct)
traytel@51462
   464
    case (less y)
traytel@51462
   465
    thus ?case
traytel@51462
   466
    proof (cases "y < length (shd s)")
traytel@51462
   467
      case True thus ?thesis by (metis flat_snth less(2,3) snth.simps(1))
traytel@51462
   468
    next
traytel@51462
   469
      case False
traytel@51462
   470
      hence "x = flat (stl s) !! (y - length (shd s))" by (metis less(2,3) flat_snth)
traytel@51462
   471
      moreover
wenzelm@53374
   472
      { from less(2) have *: "length (shd s) > 0" by (cases s) simp_all
wenzelm@53374
   473
        with False have "y > 0" by (cases y) simp_all
wenzelm@53374
   474
        with * have "y - length (shd s) < y" by simp
traytel@51462
   475
      }
traytel@51772
   476
      moreover have "\<forall>xs \<in> sset (stl s). xs \<noteq> []" using less(2) by (cases s) auto
traytel@51462
   477
      ultimately have "\<exists>n m'. x = stl s !! n ! m' \<and> m' < length (stl s !! n)" by (intro less(1)) auto
traytel@51462
   478
      thus ?thesis by (metis snth.simps(2))
traytel@51462
   479
    qed
traytel@51462
   480
  qed
traytel@51772
   481
  thus "x \<in> ?R" by (auto simp: sset_range dest!: nth_mem)
traytel@51462
   482
next
traytel@51772
   483
  fix x xs assume "xs \<in> sset s" ?P "x \<in> set xs" thus "x \<in> ?L"
blanchet@57986
   484
    by (induct rule: sset_induct)
traytel@51772
   485
      (metis UnI1 flat_unfold shift.simps(1) sset_shift,
traytel@51772
   486
       metis UnI2 flat_unfold shd_sset stl_sset sset_shift)
traytel@51462
   487
qed
traytel@51462
   488
traytel@51462
   489
wenzelm@60500
   490
subsection \<open>merge a stream of streams\<close>
traytel@51462
   491
traytel@51462
   492
definition smerge :: "'a stream stream \<Rightarrow> 'a stream" where
traytel@51772
   493
  "smerge ss = flat (smap (\<lambda>n. map (\<lambda>s. s !! n) (stake (Suc n) ss) @ stake n (ss !! n)) nats)"
traytel@51462
   494
traytel@51462
   495
lemma stake_nth[simp]: "m < n \<Longrightarrow> stake n s ! m = s !! m"
traytel@51462
   496
  by (induct n arbitrary: s m) (auto simp: nth_Cons', metis Suc_pred snth.simps(2))
traytel@51462
   497
traytel@51772
   498
lemma snth_sset_smerge: "ss !! n !! m \<in> sset (smerge ss)"
traytel@51462
   499
proof (cases "n \<le> m")
traytel@51462
   500
  case False thus ?thesis unfolding smerge_def
traytel@51772
   501
    by (subst sset_flat)
blanchet@53290
   502
      (auto simp: stream.set_map in_set_conv_nth simp del: stake.simps
traytel@51462
   503
        intro!: exI[of _ n, OF disjI2] exI[of _ m, OF mp])
traytel@51462
   504
next
traytel@51462
   505
  case True thus ?thesis unfolding smerge_def
traytel@51772
   506
    by (subst sset_flat)
blanchet@53290
   507
      (auto simp: stream.set_map in_set_conv_nth image_iff simp del: stake.simps snth.simps
traytel@51462
   508
        intro!: exI[of _ m, OF disjI1] bexI[of _ "ss !! n"] exI[of _ n, OF mp])
traytel@51462
   509
qed
traytel@51462
   510
traytel@51772
   511
lemma sset_smerge: "sset (smerge ss) = UNION (sset ss) sset"
traytel@51462
   512
proof safe
traytel@51772
   513
  fix x assume "x \<in> sset (smerge ss)"
traytel@51772
   514
  thus "x \<in> UNION (sset ss) sset"
traytel@51772
   515
    unfolding smerge_def by (subst (asm) sset_flat)
blanchet@53290
   516
      (auto simp: stream.set_map in_set_conv_nth sset_range simp del: stake.simps, fast+)
traytel@51462
   517
next
traytel@51772
   518
  fix s x assume "s \<in> sset ss" "x \<in> sset s"
traytel@51772
   519
  thus "x \<in> sset (smerge ss)" using snth_sset_smerge by (auto simp: sset_range)
traytel@51462
   520
qed
traytel@51462
   521
traytel@51462
   522
wenzelm@60500
   523
subsection \<open>product of two streams\<close>
traytel@51462
   524
traytel@51462
   525
definition sproduct :: "'a stream \<Rightarrow> 'b stream \<Rightarrow> ('a \<times> 'b) stream" where
traytel@51772
   526
  "sproduct s1 s2 = smerge (smap (\<lambda>x. smap (Pair x) s2) s1)"
traytel@51462
   527
traytel@51772
   528
lemma sset_sproduct: "sset (sproduct s1 s2) = sset s1 \<times> sset s2"
blanchet@53290
   529
  unfolding sproduct_def sset_smerge by (auto simp: stream.set_map)
traytel@51462
   530
traytel@51462
   531
wenzelm@60500
   532
subsection \<open>interleave two streams\<close>
traytel@51462
   533
traytel@54027
   534
primcorec sinterleave where
traytel@54027
   535
  "shd (sinterleave s1 s2) = shd s1"
traytel@54027
   536
| "stl (sinterleave s1 s2) = sinterleave s2 (stl s1)"
traytel@51462
   537
traytel@51462
   538
lemma sinterleave_code[code]:
traytel@51462
   539
  "sinterleave (x ## s1) s2 = x ## sinterleave s2 s1"
traytel@54027
   540
  by (subst sinterleave.ctr) simp
traytel@51462
   541
traytel@51462
   542
lemma sinterleave_snth[simp]:
traytel@51462
   543
  "even n \<Longrightarrow> sinterleave s1 s2 !! n = s1 !! (n div 2)"
haftmann@58710
   544
  "odd n \<Longrightarrow> sinterleave s1 s2 !! n = s2 !! (n div 2)"
haftmann@58710
   545
  by (induct n arbitrary: s1 s2) simp_all
traytel@51462
   546
traytel@51772
   547
lemma sset_sinterleave: "sset (sinterleave s1 s2) = sset s1 \<union> sset s2"
traytel@51462
   548
proof (intro equalityI subsetI)
traytel@51772
   549
  fix x assume "x \<in> sset (sinterleave s1 s2)"
traytel@51772
   550
  then obtain n where "x = sinterleave s1 s2 !! n" unfolding sset_range by blast
traytel@51772
   551
  thus "x \<in> sset s1 \<union> sset s2" by (cases "even n") auto
traytel@51462
   552
next
traytel@51772
   553
  fix x assume "x \<in> sset s1 \<union> sset s2"
traytel@51772
   554
  thus "x \<in> sset (sinterleave s1 s2)"
traytel@51462
   555
  proof
traytel@51772
   556
    assume "x \<in> sset s1"
traytel@51772
   557
    then obtain n where "x = s1 !! n" unfolding sset_range by blast
traytel@51462
   558
    hence "sinterleave s1 s2 !! (2 * n) = x" by simp
traytel@51772
   559
    thus ?thesis unfolding sset_range by blast
traytel@51462
   560
  next
traytel@51772
   561
    assume "x \<in> sset s2"
traytel@51772
   562
    then obtain n where "x = s2 !! n" unfolding sset_range by blast
traytel@51462
   563
    hence "sinterleave s1 s2 !! (2 * n + 1) = x" by simp
traytel@51772
   564
    thus ?thesis unfolding sset_range by blast
traytel@51462
   565
  qed
traytel@51462
   566
qed
traytel@51462
   567
traytel@51462
   568
wenzelm@60500
   569
subsection \<open>zip\<close>
traytel@51141
   570
traytel@54027
   571
primcorec szip where
traytel@54027
   572
  "shd (szip s1 s2) = (shd s1, shd s2)"
traytel@54027
   573
| "stl (szip s1 s2) = szip (stl s1) (stl s2)"
traytel@51141
   574
traytel@54720
   575
lemma szip_unfold[code]: "szip (a ## s1) (b ## s2) = (a, b) ## (szip s1 s2)"
traytel@54027
   576
  by (subst szip.ctr) simp
traytel@51409
   577
traytel@51141
   578
lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)"
traytel@51141
   579
  by (induct n arbitrary: s1 s2) auto
traytel@51141
   580
traytel@57175
   581
lemma stake_szip[simp]:
traytel@57175
   582
  "stake n (szip s1 s2) = zip (stake n s1) (stake n s2)"
traytel@57175
   583
  by (induct n arbitrary: s1 s2) auto
traytel@57175
   584
traytel@57175
   585
lemma sdrop_szip[simp]: "sdrop n (szip s1 s2) = szip (sdrop n s1) (sdrop n s2)"
traytel@57175
   586
  by (induct n arbitrary: s1 s2) auto
traytel@57175
   587
traytel@57175
   588
lemma smap_szip_fst:
traytel@57175
   589
  "smap (\<lambda>x. f (fst x)) (szip s1 s2) = smap f s1"
traytel@57175
   590
  by (coinduction arbitrary: s1 s2) auto
traytel@57175
   591
traytel@57175
   592
lemma smap_szip_snd:
traytel@57175
   593
  "smap (\<lambda>x. g (snd x)) (szip s1 s2) = smap g s2"
traytel@57175
   594
  by (coinduction arbitrary: s1 s2) auto
traytel@57175
   595
traytel@51141
   596
wenzelm@60500
   597
subsection \<open>zip via function\<close>
traytel@51141
   598
traytel@54027
   599
primcorec smap2 where
traytel@51772
   600
  "shd (smap2 f s1 s2) = f (shd s1) (shd s2)"
traytel@54027
   601
| "stl (smap2 f s1 s2) = smap2 f (stl s1) (stl s2)"
traytel@51141
   602
traytel@51772
   603
lemma smap2_unfold[code]:
traytel@54720
   604
  "smap2 f (a ## s1) (b ## s2) = f a b ## (smap2 f s1 s2)"
traytel@54027
   605
  by (subst smap2.ctr) simp
traytel@51409
   606
traytel@51772
   607
lemma smap2_szip:
haftmann@61424
   608
  "smap2 f s1 s2 = smap (case_prod f) (szip s1 s2)"
traytel@54027
   609
  by (coinduction arbitrary: s1 s2) auto
traytel@50518
   610
traytel@57175
   611
lemma smap_smap2[simp]:
traytel@57175
   612
  "smap f (smap2 g s1 s2) = smap2 (\<lambda>x y. f (g x y)) s1 s2"
traytel@57175
   613
  unfolding smap2_szip stream.map_comp o_def split_def ..
traytel@57175
   614
traytel@57175
   615
lemma smap2_alt:
traytel@57175
   616
  "(smap2 f s1 s2 = s) = (\<forall>n. f (s1 !! n) (s2 !! n) = s !! n)"
traytel@57175
   617
  unfolding smap2_szip smap_alt by auto
traytel@57175
   618
traytel@57175
   619
lemma snth_smap2[simp]:
traytel@57175
   620
  "smap2 f s1 s2 !! n = f (s1 !! n) (s2 !! n)"
traytel@57175
   621
  by (induct n arbitrary: s1 s2) auto
traytel@57175
   622
traytel@57175
   623
lemma stake_smap2[simp]:
haftmann@61424
   624
  "stake n (smap2 f s1 s2) = map (case_prod f) (zip (stake n s1) (stake n s2))"
traytel@57175
   625
  by (induct n arbitrary: s1 s2) auto
traytel@57175
   626
traytel@57175
   627
lemma sdrop_smap2[simp]:
traytel@57175
   628
  "sdrop n (smap2 f s1 s2) = smap2 f (sdrop n s1) (sdrop n s2)"
traytel@57175
   629
  by (induct n arbitrary: s1 s2) auto
traytel@57175
   630
traytel@50518
   631
end