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