src/HOL/Library/Stream.thy
author wenzelm
Sat, 05 Jan 2019 17:24:33 +0100
changeset 69597 ff784d5a5bfb
parent 69313 b021008c5397
child 81332 f94b30fa2b6c
permissions -rw-r--r--
isabelle update -u control_cartouches;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58607
1f90ea1b4010 move Stream theory from Datatype_Examples to Library
hoelzl
parents: 58309
diff changeset
     1
(*  Title:      HOL/Library/Stream.thy
50518
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
51778
190f89980f7b adjusted stream library to coinduct attributes
blanchet
parents: 51772
diff changeset
     4
    Copyright   2012, 2013
50518
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
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
     9
section \<open>Infinite Streams\<close>
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    10
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    11
theory Stream
65366
10ca63a18e56 proper imports;
wenzelm
parents: 64320
diff changeset
    12
  imports Nat_Bijection
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    13
begin
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    14
57206
d9be905d6283 changed syntax of map: and rel: arguments to BNF-based datatypes
blanchet
parents: 57175
diff changeset
    15
codatatype (sset: 'a) stream =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69313
diff changeset
    16
  SCons (shd: 'a) (stl: "'a stream") (infixr \<open>##\<close> 65)
57206
d9be905d6283 changed syntax of map: and rel: arguments to BNF-based datatypes
blanchet
parents: 57175
diff changeset
    17
for
d9be905d6283 changed syntax of map: and rel: arguments to BNF-based datatypes
blanchet
parents: 57175
diff changeset
    18
  map: smap
d9be905d6283 changed syntax of map: and rel: arguments to BNF-based datatypes
blanchet
parents: 57175
diff changeset
    19
  rel: stream_all2
51409
6e01fa224ad5 nitpick setup and code generation for streams
traytel
parents: 51353
diff changeset
    20
60011
wenzelm
parents: 59016
diff changeset
    21
context
wenzelm
parents: 59016
diff changeset
    22
begin
wenzelm
parents: 59016
diff changeset
    23
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
    24
\<comment> \<open>for code generation only\<close>
60011
wenzelm
parents: 59016
diff changeset
    25
qualified 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
    26
  [code_abbrev]: "smember x s \<longleftrightarrow> x \<in> sset s"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
    27
54720
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
    28
lemma smember_code[code, simp]: "smember x (y ## s) = (if x = y then True else smember x s)"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
    29
  unfolding smember_def by auto
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
    30
60011
wenzelm
parents: 59016
diff changeset
    31
end
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
    32
57983
6edc3529bb4e reordered some (co)datatype property names for more consistency
blanchet
parents: 57206
diff changeset
    33
lemmas smap_simps[simp] = stream.map_sel
6edc3529bb4e reordered some (co)datatype property names for more consistency
blanchet
parents: 57206
diff changeset
    34
lemmas shd_sset = stream.set_sel(1)
6edc3529bb4e reordered some (co)datatype property names for more consistency
blanchet
parents: 57206
diff changeset
    35
lemmas stl_sset = stream.set_sel(2)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    36
57986
0d60b9e58487 cleaned up derivation of 'sset_induct'
blanchet
parents: 57983
diff changeset
    37
theorem sset_induct[consumes 1, case_names shd stl, induct set: sset]:
0d60b9e58487 cleaned up derivation of 'sset_induct'
blanchet
parents: 57983
diff changeset
    38
  assumes "y \<in> sset s" and "\<And>s. P (shd s) s" 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
    39
  shows "P y s"
57986
0d60b9e58487 cleaned up derivation of 'sset_induct'
blanchet
parents: 57983
diff changeset
    40
using assms by induct (metis stream.sel(1), auto)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    41
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    42
lemma smap_ctr: "smap f s = x ## s' \<longleftrightarrow> f (shd s) = x \<and> smap f (stl s) = s'"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    43
  by (cases s) simp
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    44
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
    45
subsection \<open>prepend list to stream\<close>
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    46
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69313
diff changeset
    47
primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr \<open>@-\<close> 65) where
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    48
  "shift [] s = s"
51023
550f265864e3 infix syntax for streams (reflecting the one for lists)
traytel
parents: 50518
diff changeset
    49
| "shift (x # xs) s = x ## shift xs s"
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    50
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
    51
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
    52
  by (induct xs) auto
ae707530c359 extended stream library a little more
traytel
parents: 51352
diff changeset
    53
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    54
lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
    55
  by (induct xs) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    56
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    57
lemma shift_simps[simp]:
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    58
   "shd (xs @- s) = (if xs = [] then shd s else hd xs)"
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    59
   "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
    60
  by (induct xs) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    61
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
    62
lemma sset_shift[simp]: "sset (xs @- s) = set xs \<union> sset s"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
    63
  by (induct xs) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    64
51352
fdecc2cd5649 extended stream library
traytel
parents: 51141
diff changeset
    65
lemma shift_left_inj[simp]: "xs @- s1 = xs @- s2 \<longleftrightarrow> s1 = s2"
fdecc2cd5649 extended stream library
traytel
parents: 51141
diff changeset
    66
  by (induct xs) auto
fdecc2cd5649 extended stream library
traytel
parents: 51141
diff changeset
    67
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    68
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
    69
subsection \<open>set of streams with elements in some fixed set\<close>
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    70
61681
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61424
diff changeset
    71
context
62093
bd73a2279fcd more uniform treatment of package internals;
wenzelm
parents: 61681
diff changeset
    72
  notes [[inductive_internals]]
61681
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61424
diff changeset
    73
begin
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61424
diff changeset
    74
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    75
coinductive_set
54469
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
    76
  streams :: "'a set \<Rightarrow> 'a stream set"
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    77
  for A :: "'a set"
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    78
where
51023
550f265864e3 infix syntax for streams (reflecting the one for lists)
traytel
parents: 50518
diff changeset
    79
  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
    80
61681
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61424
diff changeset
    81
end
ca53150406c9 option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents: 61424
diff changeset
    82
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    83
lemma in_streams: "stl s \<in> streams S \<Longrightarrow> shd s \<in> S \<Longrightarrow> s \<in> streams S"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    84
  by (cases s) auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    85
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    86
lemma streamsE: "s \<in> streams A \<Longrightarrow> (shd s \<in> A \<Longrightarrow> stl s \<in> streams A \<Longrightarrow> P) \<Longrightarrow> P"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    87
  by (erule streams.cases) simp_all
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    88
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67091
diff changeset
    89
lemma Stream_image: "x ## y \<in> ((##) x') ` Y \<longleftrightarrow> x = x' \<and> y \<in> Y"
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    90
  by auto
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
    91
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    92
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
    93
  by (induct w) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
    94
54469
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
    95
lemma streams_Stream: "x ## s \<in> streams A \<longleftrightarrow> x \<in> A \<and> s \<in> streams A"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
    96
  by (auto elim: streams.cases)
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
    97
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
    98
lemma streams_stl: "s \<in> streams A \<Longrightarrow> stl s \<in> streams A"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
    99
  by (cases s) (auto simp: streams_Stream)
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   100
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   101
lemma streams_shd: "s \<in> streams A \<Longrightarrow> shd s \<in> A"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   102
  by (cases s) (auto simp: streams_Stream)
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
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_streams:
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   105
  assumes "sset s \<subseteq> A"
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   106
  shows "s \<in> streams A"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   107
using assms proof (coinduction arbitrary: s)
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   108
  case streams then show ?case by (cases s) simp
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   109
qed
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   110
54469
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   111
lemma streams_sset:
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   112
  assumes "s \<in> streams A"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   113
  shows "sset s \<subseteq> A"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   114
proof
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   115
  fix x assume "x \<in> sset s" from this \<open>s \<in> streams A\<close> show "x \<in> A"
54469
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   116
    by (induct s) (auto intro: streams_shd streams_stl)
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   117
qed
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   118
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   119
lemma streams_iff_sset: "s \<in> streams A \<longleftrightarrow> sset s \<subseteq> A"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   120
  by (metis sset_streams streams_sset)
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   121
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   122
lemma streams_mono:  "s \<in> streams A \<Longrightarrow> A \<subseteq> B \<Longrightarrow> s \<in> streams B"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   123
  unfolding streams_iff_sset by auto
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   124
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   125
lemma streams_mono2: "S \<subseteq> T \<Longrightarrow> streams S \<subseteq> streams T"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   126
  by (auto intro: streams_mono)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   127
54469
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   128
lemma smap_streams: "s \<in> streams A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> f x \<in> B) \<Longrightarrow> smap f s \<in> streams B"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   129
  unfolding streams_iff_sset stream.set_map by auto
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   130
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   131
lemma streams_empty: "streams {} = {}"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   132
  by (auto elim: streams.cases)
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   133
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   134
lemma streams_UNIV[simp]: "streams UNIV = UNIV"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   135
  by (auto simp: streams_iff_sset)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   136
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   137
subsection \<open>nth, take, drop for streams\<close>
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   138
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69313
diff changeset
   139
primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl \<open>!!\<close> 100) where
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   140
  "s !! 0 = shd s"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   141
| "s !! Suc n = stl s !! n"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   142
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   143
lemma snth_Stream: "(x ## s) !! Suc i = s !! i"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   144
  by simp
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   145
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   146
lemma snth_smap[simp]: "smap f s !! n = f (s !! n)"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   147
  by (induct n arbitrary: s) auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   148
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   149
lemma shift_snth_less[simp]: "p < length xs \<Longrightarrow> (xs @- s) !! p = xs ! p"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   150
  by (induct p arbitrary: xs) (auto simp: hd_conv_nth nth_tl)
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   151
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   152
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
   153
  by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred)
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   154
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   155
lemma shift_snth: "(xs @- s) !! n = (if n < length xs then xs ! n else s !! (n - length xs))"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   156
  by auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   157
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   158
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
   159
  by (induct n arbitrary: s) (auto intro: shd_sset stl_sset)
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   160
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   161
lemma sset_range: "sset s = range (snth s)"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   162
proof (intro equalityI subsetI)
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   163
  fix x assume "x \<in> sset s"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   164
  thus "x \<in> range (snth s)"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   165
  proof (induct s)
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   166
    case (stl s x)
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   167
    then obtain n where "x = stl s !! n" by auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   168
    thus ?case by (auto intro: range_eqI[of _ _ "Suc n"])
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   169
  qed (auto intro: range_eqI[of _ _ 0])
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   170
qed auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   171
59000
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   172
lemma streams_iff_snth: "s \<in> streams X \<longleftrightarrow> (\<forall>n. s !! n \<in> X)"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   173
  by (force simp: streams_iff_sset sset_range)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   174
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   175
lemma snth_in: "s \<in> streams X \<Longrightarrow> s !! n \<in> X"
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   176
  by (simp add: streams_iff_snth)
6eb0725503fc import general theorems from AFP/Markov_Models
hoelzl
parents: 58881
diff changeset
   177
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   178
primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   179
  "stake 0 s = []"
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   180
| "stake (Suc n) s = shd s # stake n (stl s)"
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   181
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   182
lemma length_stake[simp]: "length (stake n s) = n"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   183
  by (induct n arbitrary: s) auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   184
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   185
lemma stake_smap[simp]: "stake n (smap f s) = map f (stake n s)"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   186
  by (induct n arbitrary: s) auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   187
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   188
lemma take_stake: "take n (stake m s) = stake (min n m) s"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   189
proof (induct m arbitrary: s n)
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   190
  case (Suc m) thus ?case by (cases n) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   191
qed simp
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   192
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   193
primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   194
  "sdrop 0 s = s"
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   195
| "sdrop (Suc n) s = sdrop n (stl s)"
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   196
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   197
lemma sdrop_simps[simp]:
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   198
  "shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   199
  by (induct n arbitrary: s)  auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   200
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   201
lemma sdrop_smap[simp]: "sdrop n (smap f s) = smap f (sdrop n s)"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   202
  by (induct n arbitrary: s) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   203
51352
fdecc2cd5649 extended stream library
traytel
parents: 51141
diff changeset
   204
lemma sdrop_stl: "sdrop n (stl s) = stl (sdrop n s)"
fdecc2cd5649 extended stream library
traytel
parents: 51141
diff changeset
   205
  by (induct n) auto
fdecc2cd5649 extended stream library
traytel
parents: 51141
diff changeset
   206
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   207
lemma drop_stake: "drop n (stake m s) = stake (m - n) (sdrop n s)"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   208
proof (induct m arbitrary: s n)
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   209
  case (Suc m) thus ?case by (cases n) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   210
qed simp
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   211
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   212
lemma stake_sdrop: "stake n s @- sdrop n s = s"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   213
  by (induct n arbitrary: s) auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   214
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   215
lemma id_stake_snth_sdrop:
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   216
  "s = stake i s @- s !! i ## sdrop (Suc i) s"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   217
  by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   218
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   219
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
   220
proof
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   221
  assume ?R
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   222
  then have "\<And>n. smap f (sdrop n s) = sdrop n s'"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   223
    by coinduction (auto intro: exI[of _ 0] simp del: sdrop.simps(2))
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   224
  then show ?L using sdrop.simps(1) by metis
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   225
qed auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   226
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   227
lemma stake_invert_Nil[iff]: "stake n s = [] \<longleftrightarrow> n = 0"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   228
  by (induct n) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   229
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   230
lemma sdrop_shift: "sdrop i (w @- s) = drop i w @- sdrop (i - length w) s"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   231
  by (induct i arbitrary: w s) (auto simp: drop_tl drop_Suc neq_Nil_conv)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   232
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   233
lemma stake_shift: "stake i (w @- s) = take i w @ stake (i - length w) s"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   234
  by (induct i arbitrary: w s) (auto simp: neq_Nil_conv)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   235
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   236
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
   237
  by (induct m arbitrary: s) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   238
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   239
lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   240
  by (induct m arbitrary: s) auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   241
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   242
lemma sdrop_snth: "sdrop n s !! m = s !! (n + m)"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   243
  by (induct n arbitrary: m s) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   244
64320
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   245
partial_function (tailrec) sdrop_while :: "('a \<Rightarrow> bool) \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
51430
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   246
  "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
   247
54720
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
   248
lemma sdrop_while_SCons[code]:
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
   249
  "sdrop_while P (a ## s) = (if P a then sdrop_while P s else a ## s)"
51430
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   250
  by (subst sdrop_while.simps) simp
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   251
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   252
lemma sdrop_while_sdrop_LEAST:
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   253
  assumes "\<exists>n. P (s !! n)"
67091
1393c2340eec more symbols;
wenzelm
parents: 65366
diff changeset
   254
  shows "sdrop_while (Not \<circ> P) s = sdrop (LEAST n. P (s !! n)) s"
51430
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   255
proof -
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   256
  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
   257
    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
   258
  thus ?thesis unfolding *
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   259
  proof (induct m arbitrary: s)
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   260
    case (Suc m)
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   261
    hence "sdrop_while (Not \<circ> P) (stl s) = sdrop m (stl s)"
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   262
      by (metis (full_types) not_less_eq_eq snth.simps(2))
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   263
    moreover from Suc(3) have "\<not> (P (s !! 0))" by blast
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   264
    ultimately show ?case by (subst sdrop_while.simps) simp
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   265
  qed (metis comp_apply sdrop.simps(1) sdrop_while.simps snth.simps(1))
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   266
qed
e96447ea13c9 extended stream library (sdrop_while)
traytel
parents: 51409
diff changeset
   267
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   268
primcorec sfilter where
67091
1393c2340eec more symbols;
wenzelm
parents: 65366
diff changeset
   269
  "shd (sfilter P s) = shd (sdrop_while (Not \<circ> P) s)"
1393c2340eec more symbols;
wenzelm
parents: 65366
diff changeset
   270
| "stl (sfilter P s) = sfilter P (stl (sdrop_while (Not \<circ> P) s))"
52905
41ebc19276ea filter function on streams
traytel
parents: 51804
diff changeset
   271
41ebc19276ea filter function on streams
traytel
parents: 51804
diff changeset
   272
lemma sfilter_Stream: "sfilter P (x ## s) = (if P x then x ## sfilter P s else sfilter P s)"
41ebc19276ea filter function on streams
traytel
parents: 51804
diff changeset
   273
proof (cases "P x")
54720
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
   274
  case True thus ?thesis by (subst sfilter.ctr) (simp add: sdrop_while_SCons)
52905
41ebc19276ea filter function on streams
traytel
parents: 51804
diff changeset
   275
next
54720
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
   276
  case False thus ?thesis by (subst (1 2) sfilter.ctr) (simp add: sdrop_while_SCons)
52905
41ebc19276ea filter function on streams
traytel
parents: 51804
diff changeset
   277
qed
41ebc19276ea filter function on streams
traytel
parents: 51804
diff changeset
   278
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   279
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   280
subsection \<open>unary predicates lifted to streams\<close>
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   281
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   282
definition "stream_all P s = (\<forall>p. P (s !! p))"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   283
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   284
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
   285
  unfolding stream_all_def sset_range by auto
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   286
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   287
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
   288
  unfolding stream_all_iff list_all_iff by auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   289
54469
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   290
lemma stream_all_Stream: "stream_all P (x ## X) \<longleftrightarrow> P x \<and> stream_all P X"
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   291
  by simp
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   292
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   293
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   294
subsection \<open>recurring stream out of a list\<close>
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   295
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   296
primcorec cycle :: "'a list \<Rightarrow> 'a stream" where
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   297
  "shd (cycle xs) = hd xs"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   298
| "stl (cycle xs) = cycle (tl xs @ [hd xs])"
54720
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
   299
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   300
lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   301
proof (coinduction arbitrary: u)
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   302
  case Eq_stream then show ?case using stream.collapse[of "cycle u"]
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   303
    by (auto intro!: exI[of _ "tl u @ [hd u]"])
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   304
qed
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   305
51409
6e01fa224ad5 nitpick setup and code generation for streams
traytel
parents: 51353
diff changeset
   306
lemma cycle_Cons[code]: "cycle (x # xs) = x ## cycle (xs @ [x])"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   307
  by (subst cycle.ctr) simp
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   308
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   309
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
   310
  by (auto dest: arg_cong[of _ _ stl])
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   311
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   312
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
   313
proof (induct n arbitrary: u)
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   314
  case (Suc n) thus ?case by (cases u) auto
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   315
qed auto
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   316
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   317
lemma stake_cycle_le[simp]:
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   318
  assumes "u \<noteq> []" "n < length u"
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   319
  shows "stake n (cycle u) = take n u"
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   320
using min_absorb2[OF less_imp_le_nat[OF assms(2)]]
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   321
  by (subst cycle_decomp[OF assms(1)], subst stake_append) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   322
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   323
lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u"
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   324
  by (subst cycle_decomp) (auto simp: stake_shift)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   325
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   326
lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u"
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   327
  by (subst cycle_decomp) (auto simp: sdrop_shift)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   328
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   329
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
   330
   stake n (cycle u) = concat (replicate (n div length u) u)"
68260
61188c781cdd avoid overaggressive classical rule
haftmann
parents: 67613
diff changeset
   331
  by (induct "n div length u" arbitrary: n u)
61188c781cdd avoid overaggressive classical rule
haftmann
parents: 67613
diff changeset
   332
    (auto simp: stake_add [symmetric] mod_eq_0_iff_dvd elim!: dvdE)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   333
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   334
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
   335
   sdrop n (cycle u) = cycle u"
68260
61188c781cdd avoid overaggressive classical rule
haftmann
parents: 67613
diff changeset
   336
  by (induct "n div length u" arbitrary: n u)
61188c781cdd avoid overaggressive classical rule
haftmann
parents: 67613
diff changeset
   337
    (auto simp: sdrop_add [symmetric] mod_eq_0_iff_dvd elim!: dvdE)
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   338
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   339
lemma stake_cycle: "u \<noteq> [] \<Longrightarrow>
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   340
   stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u"
64242
93c6f0da5c70 more standardized theorem names for facts involving the div and mod identity
haftmann
parents: 63649
diff changeset
   341
  by (subst div_mult_mod_eq[of n "length u", symmetric], unfold stake_add[symmetric]) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   342
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   343
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
   344
  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
   345
63192
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   346
lemma sset_cycle[simp]:
64320
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   347
  assumes "xs \<noteq> []"
63192
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   348
  shows "sset (cycle xs) = set xs"
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   349
proof (intro set_eqI iffI)
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   350
  fix x
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   351
  assume "x \<in> sset (cycle xs)"
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   352
  then show "x \<in> set xs" using assms
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   353
    by (induction "cycle xs" arbitrary: xs rule: sset_induct) (fastforce simp: neq_Nil_conv)+
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   354
qed (metis assms UnI1 cycle_decomp sset_shift)
a742d309afa2 moved lemma from afp
traytel
parents: 62093
diff changeset
   355
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   356
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   357
subsection \<open>iterated application of a function\<close>
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   358
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   359
primcorec siterate where
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   360
  "shd (siterate f x) = x"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   361
| "stl (siterate f x) = siterate f (f x)"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   362
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   363
lemma stake_Suc: "stake (Suc n) s = stake n s @ [s !! n]"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   364
  by (induct n arbitrary: s) auto
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   365
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   366
lemma snth_siterate[simp]: "siterate f x !! n = (f^^n) x"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   367
  by (induct n arbitrary: x) (auto simp: funpow_swap1)
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   368
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   369
lemma sdrop_siterate[simp]: "sdrop n (siterate f x) = siterate f ((f^^n) x)"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   370
  by (induct n arbitrary: x) (auto simp: funpow_swap1)
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   371
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   372
lemma stake_siterate[simp]: "stake n (siterate f x) = map (\<lambda>n. (f^^n) x) [0 ..< n]"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   373
  by (induct n arbitrary: x) (auto simp del: stake.simps(2) simp: stake_Suc)
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   374
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   375
lemma sset_siterate: "sset (siterate f x) = {(f^^n) x | n. True}"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   376
  by (auto simp: sset_range)
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   377
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   378
lemma smap_siterate: "smap f (siterate f x) = siterate f (f x)"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   379
  by (coinduction arbitrary: x) auto
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   380
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   381
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   382
subsection \<open>stream repeating a single element\<close>
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   383
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   384
abbreviation "sconst \<equiv> siterate id"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   385
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   386
lemma shift_replicate_sconst[simp]: "replicate n x @- sconst x = sconst x"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   387
  by (subst (3) stake_sdrop[symmetric]) (simp add: map_replicate_trivial)
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   388
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   389
lemma sset_sconst[simp]: "sset (sconst x) = {x}"
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   390
  by (simp add: sset_siterate)
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   391
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   392
lemma sconst_alt: "s = sconst x \<longleftrightarrow> sset s = {x}"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   393
proof
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   394
  assume "sset s = {x}"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   395
  then show "s = sconst x"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   396
  proof (coinduction arbitrary: s)
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   397
    case Eq_stream
63649
e690d6f2185b tuned proofs;
wenzelm
parents: 63192
diff changeset
   398
    then have "shd s = x" "sset (stl s) \<subseteq> {x}" by (cases s; auto)+
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   399
    then have "sset (stl s) = {x}" by (cases "stl s") auto
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   400
    with \<open>shd s = x\<close> show ?case by auto
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   401
  qed
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   402
qed simp
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   403
59016
be4a911aca71 more accurate lemma name
traytel
parents: 59000
diff changeset
   404
lemma sconst_cycle: "sconst x = cycle [x]"
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   405
  by coinduction auto
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   406
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   407
lemma smap_sconst: "smap f (sconst x) = sconst (f x)"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   408
  by coinduction auto
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   409
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   410
lemma sconst_streams: "x \<in> A \<Longrightarrow> sconst x \<in> streams A"
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   411
  by (simp add: streams_iff_sset)
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   412
64320
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   413
lemma streams_empty_iff: "streams S = {} \<longleftrightarrow> S = {}"
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   414
proof safe
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   415
  fix x assume "x \<in> S" "streams S = {}"
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   416
  then have "sconst x \<in> streams S"
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   417
    by (intro sconst_streams)
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   418
  then show "x \<in> {}"
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   419
    unfolding \<open>streams S = {}\<close> by simp
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   420
qed (auto simp: streams_empty)
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   421
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   422
subsection \<open>stream of natural numbers\<close>
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   423
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   424
abbreviation "fromN \<equiv> siterate Suc"
54469
0ccec59194af additional lemmas in BNF/Examples/Stream
hoelzl
parents: 54027
diff changeset
   425
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   426
abbreviation "nats \<equiv> fromN 0"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   427
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   428
lemma sset_fromN[simp]: "sset (fromN n) = {n ..}"
54720
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
   429
  by (auto simp add: sset_siterate le_iff_add)
54497
c76dec4df4d7 BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
hoelzl
parents: 54469
diff changeset
   430
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   431
lemma stream_smap_fromN: "s = smap (\<lambda>j. let i = j - n in s !! i) (fromN n)"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   432
  by (coinduction arbitrary: s n)
68406
6beb45f6cf67 utilize 'flip'
nipkow
parents: 68260
diff changeset
   433
    (force simp: neq_Nil_conv Let_def Suc_diff_Suc simp flip: snth.simps(2)
6beb45f6cf67 utilize 'flip'
nipkow
parents: 68260
diff changeset
   434
      intro: stream.map_cong split: if_splits)
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   435
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   436
lemma stream_smap_nats: "s = smap (snth s) nats"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   437
  using stream_smap_fromN[where n = 0] by simp
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   438
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   439
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   440
subsection \<open>flatten a stream of lists\<close>
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   441
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   442
primcorec flat where
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   443
  "shd (flat ws) = hd (shd ws)"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   444
| "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   445
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   446
lemma flat_Cons[simp, code]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   447
  by (subst flat.ctr) simp
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   448
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   449
lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws"
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   450
  by (induct xs) auto
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   451
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   452
lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)"
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   453
  by (cases ws) auto
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   454
64320
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   455
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
   456
  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
   457
  by (metis flat_unfold not_less shd_sset shift_snth_ge shift_snth_less)
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   458
64320
ba194424b895 HOL-Probability: move stopping time from AFP/Markov_Models
hoelzl
parents: 64242
diff changeset
   459
lemma sset_flat[simp]: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow>
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   460
  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
   461
proof safe
67613
ce654b0e6d69 more symbols;
wenzelm
parents: 67408
diff changeset
   462
  fix x assume ?P "x \<in> ?L"
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   463
  then obtain m where "x = flat s !! m" by (metis image_iff sset_range)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   464
  with \<open>?P\<close> obtain n m' where "x = s !! n ! m'" "m' < length (s !! n)"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   465
  proof (atomize_elim, induct m arbitrary: s rule: less_induct)
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   466
    case (less y)
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   467
    thus ?case
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   468
    proof (cases "y < length (shd s)")
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   469
      case True thus ?thesis by (metis flat_snth less(2,3) snth.simps(1))
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   470
    next
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   471
      case False
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   472
      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
   473
      moreover
53374
a14d2a854c02 tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents: 53290
diff changeset
   474
      { from less(2) have *: "length (shd s) > 0" by (cases s) simp_all
a14d2a854c02 tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents: 53290
diff changeset
   475
        with False have "y > 0" by (cases y) simp_all
a14d2a854c02 tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents: 53290
diff changeset
   476
        with * have "y - length (shd s) < y" by simp
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   477
      }
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   478
      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
   479
      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
   480
      thus ?thesis by (metis snth.simps(2))
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   481
    qed
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   482
  qed
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   483
  thus "x \<in> ?R" by (auto simp: sset_range dest!: nth_mem)
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   484
next
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   485
  fix x xs assume "xs \<in> sset s" ?P "x \<in> set xs" thus "x \<in> ?L"
57986
0d60b9e58487 cleaned up derivation of 'sset_induct'
blanchet
parents: 57983
diff changeset
   486
    by (induct rule: sset_induct)
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   487
      (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
   488
       metis UnI2 flat_unfold shd_sset stl_sset sset_shift)
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   489
qed
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   490
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   491
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   492
subsection \<open>merge a stream of streams\<close>
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   493
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   494
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
   495
  "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
   496
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   497
lemma stake_nth[simp]: "m < n \<Longrightarrow> stake n s ! m = s !! m"
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   498
  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
   499
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   500
lemma snth_sset_smerge: "ss !! n !! m \<in> sset (smerge ss)"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   501
proof (cases "n \<le> m")
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   502
  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
   503
    by (subst sset_flat)
53290
b6c3be868217 renamed BNF fact
blanchet
parents: 52991
diff changeset
   504
      (auto simp: stream.set_map in_set_conv_nth simp del: stake.simps
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   505
        intro!: exI[of _ n, OF disjI2] exI[of _ m, OF mp])
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   506
next
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   507
  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
   508
    by (subst sset_flat)
53290
b6c3be868217 renamed BNF fact
blanchet
parents: 52991
diff changeset
   509
      (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
   510
        intro!: exI[of _ m, OF disjI1] bexI[of _ "ss !! n"] exI[of _ n, OF mp])
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   511
qed
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   512
69313
b021008c5397 removed legacy input syntax
haftmann
parents: 68406
diff changeset
   513
lemma sset_smerge: "sset (smerge ss) = \<Union>(sset ` (sset ss))"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   514
proof safe
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   515
  fix x assume "x \<in> sset (smerge ss)"
69313
b021008c5397 removed legacy input syntax
haftmann
parents: 68406
diff changeset
   516
  thus "x \<in> \<Union>(sset ` (sset ss))"
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   517
    unfolding smerge_def by (subst (asm) sset_flat)
53290
b6c3be868217 renamed BNF fact
blanchet
parents: 52991
diff changeset
   518
      (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
   519
next
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   520
  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
   521
  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
   522
qed
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   523
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   524
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   525
subsection \<open>product of two streams\<close>
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   526
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   527
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
   528
  "sproduct s1 s2 = smerge (smap (\<lambda>x. smap (Pair x) s2) s1)"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   529
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   530
lemma sset_sproduct: "sset (sproduct s1 s2) = sset s1 \<times> sset s2"
53290
b6c3be868217 renamed BNF fact
blanchet
parents: 52991
diff changeset
   531
  unfolding sproduct_def sset_smerge by (auto simp: stream.set_map)
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   532
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   533
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   534
subsection \<open>interleave two streams\<close>
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   535
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   536
primcorec sinterleave where
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   537
  "shd (sinterleave s1 s2) = shd s1"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   538
| "stl (sinterleave s1 s2) = sinterleave s2 (stl s1)"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   539
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   540
lemma sinterleave_code[code]:
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   541
  "sinterleave (x ## s1) s2 = x ## sinterleave s2 s1"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   542
  by (subst sinterleave.ctr) simp
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   543
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   544
lemma sinterleave_snth[simp]:
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   545
  "even n \<Longrightarrow> sinterleave s1 s2 !! n = s1 !! (n div 2)"
58710
7216a10d69ba augmented and tuned facts on even/odd and division
haftmann
parents: 58607
diff changeset
   546
  "odd n \<Longrightarrow> sinterleave s1 s2 !! n = s2 !! (n div 2)"
7216a10d69ba augmented and tuned facts on even/odd and division
haftmann
parents: 58607
diff changeset
   547
  by (induct n arbitrary: s1 s2) simp_all
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   548
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   549
lemma sset_sinterleave: "sset (sinterleave s1 s2) = sset s1 \<union> sset s2"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   550
proof (intro equalityI subsetI)
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   551
  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
   552
  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
   553
  thus "x \<in> sset s1 \<union> sset s2" by (cases "even n") auto
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   554
next
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   555
  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
   556
  thus "x \<in> sset (sinterleave s1 s2)"
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   557
  proof
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   558
    assume "x \<in> sset s1"
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   559
    then obtain n where "x = s1 !! n" unfolding sset_range by blast
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   560
    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
   561
    thus ?thesis unfolding sset_range by blast
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   562
  next
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   563
    assume "x \<in> sset s2"
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   564
    then obtain n where "x = s2 !! n" unfolding sset_range by blast
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   565
    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
   566
    thus ?thesis unfolding sset_range by blast
51462
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   567
  qed
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   568
qed
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   569
e239856ca8a2 extended stream library
traytel
parents: 51430
diff changeset
   570
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   571
subsection \<open>zip\<close>
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   572
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   573
primcorec szip where
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   574
  "shd (szip s1 s2) = (shd s1, shd s2)"
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   575
| "stl (szip s1 s2) = szip (stl s1) (stl s2)"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   576
54720
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
   577
lemma szip_unfold[code]: "szip (a ## s1) (b ## s2) = (a, b) ## (szip s1 s2)"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   578
  by (subst szip.ctr) simp
51409
6e01fa224ad5 nitpick setup and code generation for streams
traytel
parents: 51353
diff changeset
   579
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   580
lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)"
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   581
  by (induct n arbitrary: s1 s2) auto
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   582
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   583
lemma stake_szip[simp]:
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   584
  "stake n (szip s1 s2) = zip (stake n s1) (stake n s2)"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   585
  by (induct n arbitrary: s1 s2) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   586
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   587
lemma sdrop_szip[simp]: "sdrop n (szip s1 s2) = szip (sdrop n s1) (sdrop n s2)"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   588
  by (induct n arbitrary: s1 s2) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   589
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   590
lemma smap_szip_fst:
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   591
  "smap (\<lambda>x. f (fst x)) (szip s1 s2) = smap f s1"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   592
  by (coinduction arbitrary: s1 s2) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   593
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   594
lemma smap_szip_snd:
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   595
  "smap (\<lambda>x. g (snd x)) (szip s1 s2) = smap g s2"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   596
  by (coinduction arbitrary: s1 s2) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   597
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   598
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60011
diff changeset
   599
subsection \<open>zip via function\<close>
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   600
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   601
primcorec smap2 where
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   602
  "shd (smap2 f s1 s2) = f (shd s1) (shd s2)"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   603
| "stl (smap2 f s1 s2) = smap2 f (stl s1) (stl s2)"
51141
cc7423ce6774 extended stream library
traytel
parents: 51023
diff changeset
   604
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   605
lemma smap2_unfold[code]:
54720
0a9920e46b3a removed obsolete codegen setup; Stream -> SCons; tuned
traytel
parents: 54498
diff changeset
   606
  "smap2 f (a ## s1) (b ## s2) = f a b ## (smap2 f s1 s2)"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   607
  by (subst smap2.ctr) simp
51409
6e01fa224ad5 nitpick setup and code generation for streams
traytel
parents: 51353
diff changeset
   608
51772
d2b265ebc1fa specify nicer names for map, set and rel in the stream library
traytel
parents: 51766
diff changeset
   609
lemma smap2_szip:
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 60500
diff changeset
   610
  "smap2 f s1 s2 = smap (case_prod f) (szip s1 s2)"
54027
e5853a648b59 use new coinduction method and primcorec in examples
traytel
parents: 53808
diff changeset
   611
  by (coinduction arbitrary: s1 s2) auto
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   612
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   613
lemma smap_smap2[simp]:
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   614
  "smap f (smap2 g s1 s2) = smap2 (\<lambda>x y. f (g x y)) s1 s2"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   615
  unfolding smap2_szip stream.map_comp o_def split_def ..
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   616
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   617
lemma smap2_alt:
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   618
  "(smap2 f s1 s2 = s) = (\<forall>n. f (s1 !! n) (s2 !! n) = s !! n)"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   619
  unfolding smap2_szip smap_alt by auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   620
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   621
lemma snth_smap2[simp]:
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   622
  "smap2 f s1 s2 !! n = f (s1 !! n) (s2 !! n)"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   623
  by (induct n arbitrary: s1 s2) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   624
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   625
lemma stake_smap2[simp]:
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 60500
diff changeset
   626
  "stake n (smap2 f s1 s2) = map (case_prod f) (zip (stake n s1) (stake n s2))"
57175
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   627
  by (induct n arbitrary: s1 s2) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   628
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   629
lemma sdrop_smap2[simp]:
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   630
  "sdrop n (smap2 f s1 s2) = smap2 f (sdrop n s1) (sdrop n s2)"
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   631
  by (induct n arbitrary: s1 s2) auto
ca3475504557 extended stream library
traytel
parents: 55873
diff changeset
   632
50518
d4fdda801e19 short library for streams
traytel
parents:
diff changeset
   633
end