src/HOL/BNF/Examples/Stream.thy
author traytel
Tue Mar 05 17:18:02 2013 +0100 (2013-03-05)
changeset 51353 ae707530c359
parent 51352 fdecc2cd5649
child 51409 6e01fa224ad5
permissions -rw-r--r--
extended stream library a little more
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
traytel@50518
     4
    Copyright   2012
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
traytel@51023
    15
codata 'a stream = Stream (shd: 'a) (stl: "'a stream") (infixr "##" 65)
traytel@50518
    16
traytel@50518
    17
(* TODO: Provide by the package*)
traytel@50518
    18
theorem stream_set_induct:
traytel@51141
    19
  "\<lbrakk>\<And>s. P (shd s) s; \<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s\<rbrakk> \<Longrightarrow>
traytel@51141
    20
    \<forall>y \<in> stream_set s. P y s"
traytel@51141
    21
  by (rule stream.dtor_set_induct)
traytel@51141
    22
    (auto simp add:  shd_def stl_def stream_case_def fsts_def snds_def split_beta)
traytel@51141
    23
traytel@51141
    24
lemma stream_map_simps[simp]:
traytel@51141
    25
  "shd (stream_map f s) = f (shd s)" "stl (stream_map f s) = stream_map f (stl s)"
traytel@51141
    26
  unfolding shd_def stl_def stream_case_def stream_map_def stream.dtor_unfold
traytel@51141
    27
  by (case_tac [!] s) (auto simp: Stream_def stream.dtor_ctor)
traytel@51141
    28
traytel@51141
    29
lemma stream_map_Stream[simp]: "stream_map f (x ## s) = f x ## stream_map f s"
traytel@51141
    30
  by (metis stream.exhaust stream.sels stream_map_simps)
traytel@50518
    31
traytel@50518
    32
theorem shd_stream_set: "shd s \<in> stream_set s"
traytel@51141
    33
  by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)
traytel@51141
    34
    (metis UnCI fsts_def insertI1 stream.dtor_set)
traytel@50518
    35
traytel@50518
    36
theorem stl_stream_set: "y \<in> stream_set (stl s) \<Longrightarrow> y \<in> stream_set s"
traytel@51141
    37
  by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)
traytel@51141
    38
    (metis insertI1 set_mp snds_def stream.dtor_set_set_incl)
traytel@50518
    39
traytel@50518
    40
(* only for the non-mutual case: *)
traytel@50518
    41
theorem stream_set_induct1[consumes 1, case_names shd stl, induct set: "stream_set"]:
traytel@50518
    42
  assumes "y \<in> stream_set s" and "\<And>s. P (shd s) s"
traytel@50518
    43
  and "\<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s"
traytel@50518
    44
  shows "P y s"
traytel@51141
    45
  using assms stream_set_induct by blast
traytel@50518
    46
(* end TODO *)
traytel@50518
    47
traytel@50518
    48
traytel@50518
    49
subsection {* prepend list to stream *}
traytel@50518
    50
traytel@50518
    51
primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr "@-" 65) where
traytel@50518
    52
  "shift [] s = s"
traytel@51023
    53
| "shift (x # xs) s = x ## shift xs s"
traytel@50518
    54
traytel@51353
    55
lemma stream_map_shift[simp]: "stream_map f (xs @- s) = map f xs @- stream_map f s"
traytel@51353
    56
  by (induct xs) auto
traytel@51353
    57
traytel@50518
    58
lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s"
traytel@51141
    59
  by (induct xs) auto
traytel@50518
    60
traytel@50518
    61
lemma shift_simps[simp]:
traytel@50518
    62
   "shd (xs @- s) = (if xs = [] then shd s else hd xs)"
traytel@50518
    63
   "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)"
traytel@51141
    64
  by (induct xs) auto
traytel@50518
    65
traytel@51141
    66
lemma stream_set_shift[simp]: "stream_set (xs @- s) = set xs \<union> stream_set s"
traytel@51141
    67
  by (induct xs) auto
traytel@50518
    68
traytel@51352
    69
lemma shift_left_inj[simp]: "xs @- s1 = xs @- s2 \<longleftrightarrow> s1 = s2"
traytel@51352
    70
  by (induct xs) auto
traytel@51352
    71
traytel@50518
    72
traytel@51141
    73
subsection {* set of streams with elements in some fixes set *}
traytel@50518
    74
traytel@50518
    75
coinductive_set
traytel@50518
    76
  streams :: "'a set => '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
traytel@50518
    81
lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A"
traytel@51141
    82
  by (induct w) auto
traytel@50518
    83
traytel@50518
    84
lemma stream_set_streams:
traytel@50518
    85
  assumes "stream_set s \<subseteq> A"
traytel@50518
    86
  shows "s \<in> streams A"
traytel@51023
    87
proof (coinduct rule: streams.coinduct[of "\<lambda>s'. \<exists>a s. s' = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A"])
traytel@50518
    88
  case streams from assms show ?case by (cases s) auto
traytel@50518
    89
next
traytel@51023
    90
  fix s' assume "\<exists>a s. s' = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A"
traytel@50518
    91
  then guess a s by (elim exE)
traytel@51023
    92
  with assms show "\<exists>a l. s' = a ## l \<and>
traytel@51023
    93
    a \<in> A \<and> ((\<exists>a s. l = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A) \<or> l \<in> streams A)"
traytel@50518
    94
    by (cases s) auto
traytel@50518
    95
qed
traytel@50518
    96
traytel@50518
    97
traytel@51141
    98
subsection {* nth, take, drop for streams *}
traytel@51141
    99
traytel@51141
   100
primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where
traytel@51141
   101
  "s !! 0 = shd s"
traytel@51141
   102
| "s !! Suc n = stl s !! n"
traytel@51141
   103
traytel@51141
   104
lemma snth_stream_map[simp]: "stream_map f s !! n = f (s !! n)"
traytel@51141
   105
  by (induct n arbitrary: s) auto
traytel@51141
   106
traytel@51141
   107
lemma shift_snth_less[simp]: "p < length xs \<Longrightarrow> (xs @- s) !! p = xs ! p"
traytel@51141
   108
  by (induct p arbitrary: xs) (auto simp: hd_conv_nth nth_tl)
traytel@51141
   109
traytel@51141
   110
lemma shift_snth_ge[simp]: "p \<ge> length xs \<Longrightarrow> (xs @- s) !! p = s !! (p - length xs)"
traytel@51141
   111
  by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred)
traytel@51141
   112
traytel@51141
   113
lemma snth_stream_set[simp]: "s !! n \<in> stream_set s"
traytel@51141
   114
  by (induct n arbitrary: s) (auto intro: shd_stream_set stl_stream_set)
traytel@51141
   115
traytel@51141
   116
lemma stream_set_range: "stream_set s = range (snth s)"
traytel@51141
   117
proof (intro equalityI subsetI)
traytel@51141
   118
  fix x assume "x \<in> stream_set s"
traytel@51141
   119
  thus "x \<in> range (snth s)"
traytel@51141
   120
  proof (induct s)
traytel@51141
   121
    case (stl s x)
traytel@51141
   122
    then obtain n where "x = stl s !! n" by auto
traytel@51141
   123
    thus ?case by (auto intro: range_eqI[of _ _ "Suc n"])
traytel@51141
   124
  qed (auto intro: range_eqI[of _ _ 0])
traytel@51141
   125
qed auto
traytel@50518
   126
traytel@50518
   127
primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
traytel@50518
   128
  "stake 0 s = []"
traytel@50518
   129
| "stake (Suc n) s = shd s # stake n (stl s)"
traytel@50518
   130
traytel@51141
   131
lemma length_stake[simp]: "length (stake n s) = n"
traytel@51141
   132
  by (induct n arbitrary: s) auto
traytel@51141
   133
traytel@51141
   134
lemma stake_stream_map[simp]: "stake n (stream_map f s) = map f (stake n s)"
traytel@51141
   135
  by (induct n arbitrary: s) auto
traytel@51141
   136
traytel@50518
   137
primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
traytel@50518
   138
  "sdrop 0 s = s"
traytel@50518
   139
| "sdrop (Suc n) s = sdrop n (stl s)"
traytel@50518
   140
traytel@51141
   141
lemma sdrop_simps[simp]:
traytel@51141
   142
  "shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s"
traytel@51141
   143
  by (induct n arbitrary: s)  auto
traytel@51141
   144
traytel@51141
   145
lemma sdrop_stream_map[simp]: "sdrop n (stream_map f s) = stream_map f (sdrop n s)"
traytel@51141
   146
  by (induct n arbitrary: s) auto
traytel@50518
   147
traytel@51352
   148
lemma sdrop_stl: "sdrop n (stl s) = stl (sdrop n s)"
traytel@51352
   149
  by (induct n) auto
traytel@51352
   150
traytel@50518
   151
lemma stake_sdrop: "stake n s @- sdrop n s = s"
traytel@51141
   152
  by (induct n arbitrary: s) auto
traytel@51141
   153
traytel@51141
   154
lemma id_stake_snth_sdrop:
traytel@51141
   155
  "s = stake i s @- s !! i ## sdrop (Suc i) s"
traytel@51141
   156
  by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse)
traytel@50518
   157
traytel@51141
   158
lemma stream_map_alt: "stream_map f s = s' \<longleftrightarrow> (\<forall>n. f (s !! n) = s' !! n)" (is "?L = ?R")
traytel@51141
   159
proof
traytel@51141
   160
  assume ?R
traytel@51141
   161
  thus ?L 
traytel@51141
   162
    by (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>n. s1 = stream_map f (sdrop n s) \<and> s2 = sdrop n s'"])
traytel@51141
   163
      (auto intro: exI[of _ 0] simp del: sdrop.simps(2))
traytel@51141
   164
qed auto
traytel@51141
   165
traytel@51141
   166
lemma stake_invert_Nil[iff]: "stake n s = [] \<longleftrightarrow> n = 0"
traytel@51141
   167
  by (induct n) auto
traytel@50518
   168
traytel@50518
   169
lemma sdrop_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> sdrop n s = s'"
traytel@51141
   170
  by (induct n arbitrary: w s) auto
traytel@50518
   171
traytel@50518
   172
lemma stake_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> stake n s = w"
traytel@51141
   173
  by (induct n arbitrary: w s) auto
traytel@50518
   174
traytel@50518
   175
lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s"
traytel@51141
   176
  by (induct m arbitrary: s) auto
traytel@50518
   177
traytel@50518
   178
lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"
traytel@51141
   179
  by (induct m arbitrary: s) auto
traytel@51141
   180
traytel@51141
   181
traytel@51141
   182
subsection {* unary predicates lifted to streams *}
traytel@51141
   183
traytel@51141
   184
definition "stream_all P s = (\<forall>p. P (s !! p))"
traytel@51141
   185
traytel@51141
   186
lemma stream_all_iff[iff]: "stream_all P s \<longleftrightarrow> Ball (stream_set s) P"
traytel@51141
   187
  unfolding stream_all_def stream_set_range by auto
traytel@51141
   188
traytel@51141
   189
lemma stream_all_shift[simp]: "stream_all P (xs @- s) = (list_all P xs \<and> stream_all P s)"
traytel@51141
   190
  unfolding stream_all_iff list_all_iff by auto
traytel@51141
   191
traytel@51141
   192
traytel@51352
   193
subsection {* flatten a stream of lists *}
traytel@51352
   194
traytel@51352
   195
definition flat where
traytel@51352
   196
  "flat \<equiv> stream_unfold (hd o shd) (\<lambda>s. if tl (shd s) = [] then stl s else tl (shd s) ## stl s)"
traytel@51352
   197
traytel@51352
   198
lemma flat_simps[simp]:
traytel@51352
   199
  "shd (flat ws) = hd (shd ws)"
traytel@51352
   200
  "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)"
traytel@51352
   201
  unfolding flat_def by auto
traytel@51352
   202
traytel@51352
   203
lemma flat_Cons[simp]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)"
traytel@51352
   204
  unfolding flat_def using stream.unfold[of "hd o shd" _ "(x # xs) ## ws"] by auto
traytel@51352
   205
traytel@51352
   206
lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws"
traytel@51352
   207
  by (induct xs) auto
traytel@51352
   208
traytel@51352
   209
lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)"
traytel@51352
   210
  by (cases ws) auto
traytel@51352
   211
traytel@51352
   212
lemma flat_snth: "\<forall>xs \<in> stream_set s. xs \<noteq> [] \<Longrightarrow> flat s !! n = (if n < length (shd s) then 
traytel@51352
   213
  shd s ! n else flat (stl s) !! (n - length (shd s)))"
traytel@51352
   214
  by (metis flat_unfold not_less shd_stream_set shift_snth_ge shift_snth_less)
traytel@51352
   215
traytel@51352
   216
lemma stream_set_flat[simp]: "\<forall>xs \<in> stream_set s. xs \<noteq> [] \<Longrightarrow> 
traytel@51352
   217
  stream_set (flat s) = (\<Union>xs \<in> stream_set s. set xs)" (is "?P \<Longrightarrow> ?L = ?R")
traytel@51352
   218
proof safe
traytel@51352
   219
  fix x assume ?P "x : ?L"
traytel@51352
   220
  then obtain m where "x = flat s !! m" by (metis image_iff stream_set_range)
traytel@51352
   221
  with `?P` obtain n m' where "x = s !! n ! m'" "m' < length (s !! n)"
traytel@51352
   222
  proof (atomize_elim, induct m arbitrary: s rule: less_induct)
traytel@51352
   223
    case (less y)
traytel@51352
   224
    thus ?case
traytel@51352
   225
    proof (cases "y < length (shd s)")
traytel@51352
   226
      case True thus ?thesis by (metis flat_snth less(2,3) snth.simps(1))
traytel@51352
   227
    next
traytel@51352
   228
      case False
traytel@51352
   229
      hence "x = flat (stl s) !! (y - length (shd s))" by (metis less(2,3) flat_snth)
traytel@51352
   230
      moreover
traytel@51352
   231
      { from less(2) have "length (shd s) > 0" by (cases s) simp_all
traytel@51352
   232
        moreover with False have "y > 0" by (cases y) simp_all
traytel@51352
   233
        ultimately have "y - length (shd s) < y" by simp
traytel@51352
   234
      }
traytel@51352
   235
      moreover have "\<forall>xs \<in> stream_set (stl s). xs \<noteq> []" using less(2) by (cases s) auto
traytel@51352
   236
      ultimately have "\<exists>n m'. x = stl s !! n ! m' \<and> m' < length (stl s !! n)" by (intro less(1)) auto
traytel@51352
   237
      thus ?thesis by (metis snth.simps(2))
traytel@51352
   238
    qed
traytel@51352
   239
  qed
traytel@51352
   240
  thus "x \<in> ?R" by (auto simp: stream_set_range dest!: nth_mem)
traytel@51352
   241
next
traytel@51352
   242
  fix x xs assume "xs \<in> stream_set s" ?P "x \<in> set xs" thus "x \<in> ?L"
traytel@51352
   243
    by (induct rule: stream_set_induct1)
traytel@51352
   244
      (metis UnI1 flat_unfold shift.simps(1) stream_set_shift,
traytel@51352
   245
       metis UnI2 flat_unfold shd_stream_set stl_stream_set stream_set_shift)
traytel@51352
   246
qed
traytel@51352
   247
traytel@51352
   248
traytel@51141
   249
subsection {* recurring stream out of a list *}
traytel@51141
   250
traytel@51141
   251
definition cycle :: "'a list \<Rightarrow> 'a stream" where
traytel@51141
   252
  "cycle = stream_unfold hd (\<lambda>xs. tl xs @ [hd xs])"
traytel@51141
   253
traytel@51141
   254
lemma cycle_simps[simp]:
traytel@51141
   255
  "shd (cycle u) = hd u"
traytel@51141
   256
  "stl (cycle u) = cycle (tl u @ [hd u])"
traytel@51141
   257
  by (auto simp: cycle_def)
traytel@51141
   258
traytel@51141
   259
lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u"
traytel@51141
   260
proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>u. s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []"])
traytel@51141
   261
  case (2 s1 s2)
traytel@51141
   262
  then obtain u where "s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []" by blast
traytel@51141
   263
  thus ?case using stream.unfold[of hd "\<lambda>xs. tl xs @ [hd xs]" u] by (auto simp: cycle_def)
traytel@51141
   264
qed auto
traytel@51141
   265
traytel@51141
   266
lemma cycle_Cons: "cycle (x # xs) = x ## cycle (xs @ [x])"
traytel@51141
   267
proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>x xs. s1 = cycle (x # xs) \<and> s2 = x ## cycle (xs @ [x])"])
traytel@51141
   268
  case (2 s1 s2)
traytel@51141
   269
  then obtain x xs where "s1 = cycle (x # xs) \<and> s2 = x ## cycle (xs @ [x])" by blast
traytel@51141
   270
  thus ?case
traytel@51141
   271
    by (auto simp: cycle_def intro!: exI[of _ "hd (xs @ [x])"] exI[of _ "tl (xs @ [x])"] stream.unfold)
traytel@51141
   272
qed auto
traytel@50518
   273
traytel@50518
   274
lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @- s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @- s"
traytel@51141
   275
  by (auto dest: arg_cong[of _ _ stl])
traytel@50518
   276
traytel@50518
   277
lemma stake_append: "stake n (u @- s) = take (min (length u) n) u @ stake (n - length u) s"
traytel@50518
   278
proof (induct n arbitrary: u)
traytel@50518
   279
  case (Suc n) thus ?case by (cases u) auto
traytel@50518
   280
qed auto
traytel@50518
   281
traytel@50518
   282
lemma stake_cycle_le[simp]:
traytel@50518
   283
  assumes "u \<noteq> []" "n < length u"
traytel@50518
   284
  shows "stake n (cycle u) = take n u"
traytel@50518
   285
using min_absorb2[OF less_imp_le_nat[OF assms(2)]]
traytel@51141
   286
  by (subst cycle_decomp[OF assms(1)], subst stake_append) auto
traytel@50518
   287
traytel@50518
   288
lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u"
traytel@51141
   289
  by (metis cycle_decomp stake_shift)
traytel@50518
   290
traytel@50518
   291
lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u"
traytel@51141
   292
  by (metis cycle_decomp sdrop_shift)
traytel@50518
   293
traytel@50518
   294
lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
traytel@50518
   295
   stake n (cycle u) = concat (replicate (n div length u) u)"
traytel@51141
   296
  by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric])
traytel@50518
   297
traytel@50518
   298
lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
traytel@50518
   299
   sdrop n (cycle u) = cycle u"
traytel@51141
   300
  by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric])
traytel@50518
   301
traytel@50518
   302
lemma stake_cycle: "u \<noteq> [] \<Longrightarrow>
traytel@50518
   303
   stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u"
traytel@51141
   304
  by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto
traytel@50518
   305
traytel@50518
   306
lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)"
traytel@51141
   307
  by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric])
traytel@51141
   308
traytel@51141
   309
traytel@51141
   310
subsection {* stream repeating a single element *}
traytel@51141
   311
traytel@51141
   312
definition "same x = stream_unfold (\<lambda>_. x) id ()"
traytel@51141
   313
traytel@51141
   314
lemma same_simps[simp]: "shd (same x) = x" "stl (same x) = same x"
traytel@51141
   315
  unfolding same_def by auto
traytel@51141
   316
traytel@51141
   317
lemma same_unfold: "same x = Stream x (same x)"
traytel@51141
   318
  by (metis same_simps stream.collapse)
traytel@51141
   319
traytel@51141
   320
lemma snth_same[simp]: "same x !! n = x"
traytel@51141
   321
  unfolding same_def by (induct n) auto
traytel@51141
   322
traytel@51141
   323
lemma stake_same[simp]: "stake n (same x) = replicate n x"
traytel@51141
   324
  unfolding same_def by (induct n) (auto simp: upt_rec)
traytel@51141
   325
traytel@51141
   326
lemma sdrop_same[simp]: "sdrop n (same x) = same x"
traytel@51141
   327
  unfolding same_def by (induct n) auto
traytel@51141
   328
traytel@51141
   329
lemma shift_replicate_same[simp]: "replicate n x @- same x = same x"
traytel@51141
   330
  by (metis sdrop_same stake_same stake_sdrop)
traytel@51141
   331
traytel@51141
   332
lemma stream_all_same[simp]: "stream_all P (same x) \<longleftrightarrow> P x"
traytel@51141
   333
  unfolding stream_all_def by auto
traytel@51141
   334
traytel@51141
   335
lemma same_cycle: "same x = cycle [x]"
traytel@51141
   336
  by (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. s1 = same x \<and> s2 = cycle [x]"]) auto
traytel@51141
   337
traytel@51141
   338
traytel@51141
   339
subsection {* stream of natural numbers *}
traytel@51141
   340
traytel@51141
   341
definition "fromN n = stream_unfold id Suc n"
traytel@51141
   342
traytel@51141
   343
lemma fromN_simps[simp]: "shd (fromN n) = n" "stl (fromN n) = fromN (Suc n)"
traytel@51141
   344
  unfolding fromN_def by auto
traytel@51141
   345
traytel@51141
   346
lemma snth_fromN[simp]: "fromN n !! m = n + m"
traytel@51141
   347
  unfolding fromN_def by (induct m arbitrary: n) auto
traytel@51141
   348
traytel@51141
   349
lemma stake_fromN[simp]: "stake m (fromN n) = [n ..< m + n]"
traytel@51141
   350
  unfolding fromN_def by (induct m arbitrary: n) (auto simp: upt_rec)
traytel@51141
   351
traytel@51141
   352
lemma sdrop_fromN[simp]: "sdrop m (fromN n) = fromN (n + m)"
traytel@51141
   353
  unfolding fromN_def by (induct m arbitrary: n) auto
traytel@51141
   354
traytel@51352
   355
lemma stream_set_fromN[simp]: "stream_set (fromN n) = {n ..}" (is "?L = ?R")
traytel@51352
   356
proof safe
traytel@51352
   357
  fix m assume "m : ?L"
traytel@51352
   358
  moreover
traytel@51352
   359
  { fix s assume "m \<in> stream_set s" "\<exists>n'\<ge>n. s = fromN n'"
traytel@51352
   360
    hence "n \<le> m" by (induct arbitrary: n rule: stream_set_induct1) fastforce+
traytel@51352
   361
  }
traytel@51352
   362
  ultimately show "n \<le> m" by blast
traytel@51352
   363
next
traytel@51352
   364
  fix m assume "n \<le> m" thus "m \<in> ?L" by (metis le_iff_add snth_fromN snth_stream_set)
traytel@51352
   365
qed
traytel@51352
   366
traytel@51141
   367
abbreviation "nats \<equiv> fromN 0"
traytel@51141
   368
traytel@51141
   369
traytel@51141
   370
subsection {* zip *}
traytel@51141
   371
traytel@51141
   372
definition "szip s1 s2 =
traytel@51141
   373
  stream_unfold (map_pair shd shd) (map_pair stl stl) (s1, s2)"
traytel@51141
   374
traytel@51141
   375
lemma szip_simps[simp]:
traytel@51141
   376
  "shd (szip s1 s2) = (shd s1, shd s2)" "stl (szip s1 s2) = szip (stl s1) (stl s2)"
traytel@51141
   377
  unfolding szip_def by auto
traytel@51141
   378
traytel@51141
   379
lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)"
traytel@51141
   380
  by (induct n arbitrary: s1 s2) auto
traytel@51141
   381
traytel@51141
   382
traytel@51141
   383
subsection {* zip via function *}
traytel@51141
   384
traytel@51141
   385
definition "stream_map2 f s1 s2 =
traytel@51141
   386
  stream_unfold (\<lambda>(s1,s2). f (shd s1) (shd s2)) (map_pair stl stl) (s1, s2)"
traytel@51141
   387
traytel@51141
   388
lemma stream_map2_simps[simp]:
traytel@51141
   389
 "shd (stream_map2 f s1 s2) = f (shd s1) (shd s2)"
traytel@51141
   390
 "stl (stream_map2 f s1 s2) = stream_map2 f (stl s1) (stl s2)"
traytel@51141
   391
  unfolding stream_map2_def by auto
traytel@51141
   392
traytel@51141
   393
lemma stream_map2_szip:
traytel@51141
   394
  "stream_map2 f s1 s2 = stream_map (split f) (szip s1 s2)"
traytel@51141
   395
  by (coinduct rule: stream.coinduct[of
traytel@51141
   396
    "\<lambda>s1 s2. \<exists>s1' s2'. s1 = stream_map2 f s1' s2' \<and> s2 = stream_map (split f) (szip s1' s2')"])
traytel@51141
   397
    fastforce+
traytel@50518
   398
traytel@50518
   399
end