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