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