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