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