src/HOL/BNF/Examples/Stream.thy
author traytel
Thu Dec 13 15:39:07 2012 +0100 (2012-12-13)
changeset 50518 d4fdda801e19
child 51023 550f265864e3
permissions -rw-r--r--
short library for streams
traytel@50518
     1
(*  Title:      HOL/BNF/Examples/Stream.thy
traytel@50518
     2
    Author:     Dmitriy Traytel, TU Muenchen
traytel@50518
     3
    Author:     Andrei Popescu, TU Muenchen
traytel@50518
     4
    Copyright   2012
traytel@50518
     5
traytel@50518
     6
Infinite streams.
traytel@50518
     7
*)
traytel@50518
     8
traytel@50518
     9
header {* Infinite Streams *}
traytel@50518
    10
traytel@50518
    11
theory Stream
traytel@50518
    12
imports "../BNF"
traytel@50518
    13
begin
traytel@50518
    14
traytel@50518
    15
codata 'a stream = Stream (shd: 'a) (stl: "'a stream")
traytel@50518
    16
traytel@50518
    17
(* TODO: Provide by the package*)
traytel@50518
    18
theorem stream_set_induct:
traytel@50518
    19
   "\<lbrakk>\<And>s. P (shd s) s; \<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s\<rbrakk> \<Longrightarrow>
traytel@50518
    20
   \<forall>y \<in> stream_set s. P y s"
traytel@50518
    21
by (rule stream.dtor_set_induct)
traytel@50518
    22
   (auto simp add:  shd_def stl_def stream_case_def fsts_def snds_def split_beta)
traytel@50518
    23
traytel@50518
    24
theorem shd_stream_set: "shd s \<in> stream_set s"
traytel@50518
    25
by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)
traytel@50518
    26
   (metis UnCI fsts_def insertI1 stream.dtor_set)
traytel@50518
    27
traytel@50518
    28
theorem stl_stream_set: "y \<in> stream_set (stl s) \<Longrightarrow> y \<in> stream_set s"
traytel@50518
    29
by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)
traytel@50518
    30
   (metis insertI1 set_mp snds_def stream.dtor_set_set_incl)
traytel@50518
    31
traytel@50518
    32
(* only for the non-mutual case: *)
traytel@50518
    33
theorem stream_set_induct1[consumes 1, case_names shd stl, induct set: "stream_set"]:
traytel@50518
    34
  assumes "y \<in> stream_set s" and "\<And>s. P (shd s) s"
traytel@50518
    35
  and "\<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s"
traytel@50518
    36
  shows "P y s"
traytel@50518
    37
using assms stream_set_induct by blast
traytel@50518
    38
(* end TODO *)
traytel@50518
    39
traytel@50518
    40
traytel@50518
    41
subsection {* prepend list to stream *}
traytel@50518
    42
traytel@50518
    43
primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr "@-" 65) where
traytel@50518
    44
  "shift [] s = s"
traytel@50518
    45
| "shift (x # xs) s = Stream x (shift xs s)"
traytel@50518
    46
traytel@50518
    47
lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s"
traytel@50518
    48
by (induct xs) auto
traytel@50518
    49
traytel@50518
    50
lemma shift_simps[simp]:
traytel@50518
    51
   "shd (xs @- s) = (if xs = [] then shd s else hd xs)"
traytel@50518
    52
   "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)"
traytel@50518
    53
by (induct xs) auto
traytel@50518
    54
traytel@50518
    55
traytel@50518
    56
subsection {* recurring stream out of a list *}
traytel@50518
    57
traytel@50518
    58
definition cycle :: "'a list \<Rightarrow> 'a stream" where
traytel@50518
    59
  "cycle = stream_unfold hd (\<lambda>xs. tl xs @ [hd xs])"
traytel@50518
    60
traytel@50518
    61
lemma cycle_simps[simp]:
traytel@50518
    62
  "shd (cycle u) = hd u"
traytel@50518
    63
  "stl (cycle u) = cycle (tl u @ [hd u])"
traytel@50518
    64
by (auto simp: cycle_def)
traytel@50518
    65
traytel@50518
    66
traytel@50518
    67
lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u"
traytel@50518
    68
proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>u. s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []"])
traytel@50518
    69
  case (2 s1 s2)
traytel@50518
    70
  then obtain u where "s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []" by blast
traytel@50518
    71
  thus ?case using stream.unfold[of hd "\<lambda>xs. tl xs @ [hd xs]" u] by (auto simp: cycle_def)
traytel@50518
    72
qed auto
traytel@50518
    73
traytel@50518
    74
lemma cycle_Cons: "cycle (x # xs) = Stream x (cycle (xs @ [x]))"
traytel@50518
    75
proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>x xs. s1 = cycle (x # xs) \<and> s2 = Stream x (cycle (xs @ [x]))"])
traytel@50518
    76
  case (2 s1 s2)
traytel@50518
    77
  then obtain x xs where "s1 = cycle (x # xs) \<and> s2 = Stream x (cycle (xs @ [x]))" by blast
traytel@50518
    78
  thus ?case
traytel@50518
    79
    by (auto simp: cycle_def intro!: exI[of _ "hd (xs @ [x])"] exI[of _ "tl (xs @ [x])"] stream.unfold)
traytel@50518
    80
qed auto
traytel@50518
    81
traytel@50518
    82
coinductive_set
traytel@50518
    83
  streams :: "'a set => 'a stream set"
traytel@50518
    84
  for A :: "'a set"
traytel@50518
    85
where
traytel@50518
    86
  Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> Stream a s \<in> streams A"
traytel@50518
    87
traytel@50518
    88
lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A"
traytel@50518
    89
by (induct w) auto
traytel@50518
    90
traytel@50518
    91
lemma stream_set_streams:
traytel@50518
    92
  assumes "stream_set s \<subseteq> A"
traytel@50518
    93
  shows "s \<in> streams A"
traytel@50518
    94
proof (coinduct rule: streams.coinduct[of "\<lambda>s'. \<exists>a s. s' = Stream a s \<and> a \<in> A \<and> stream_set s \<subseteq> A"])
traytel@50518
    95
  case streams from assms show ?case by (cases s) auto
traytel@50518
    96
next
traytel@50518
    97
  fix s' assume "\<exists>a s. s' = Stream a s \<and> a \<in> A \<and> stream_set s \<subseteq> A"
traytel@50518
    98
  then guess a s by (elim exE)
traytel@50518
    99
  with assms show "\<exists>a l. s' = Stream a l \<and>
traytel@50518
   100
    a \<in> A \<and> ((\<exists>a s. l = Stream a s \<and> a \<in> A \<and> stream_set s \<subseteq> A) \<or> l \<in> streams A)"
traytel@50518
   101
    by (cases s) auto
traytel@50518
   102
qed
traytel@50518
   103
traytel@50518
   104
traytel@50518
   105
subsection {* flatten a stream of lists *}
traytel@50518
   106
traytel@50518
   107
definition flat where
traytel@50518
   108
  "flat \<equiv> stream_unfold (hd o shd) (\<lambda>s. if tl (shd s) = [] then stl s else Stream (tl (shd s)) (stl s))"
traytel@50518
   109
traytel@50518
   110
lemma flat_simps[simp]:
traytel@50518
   111
  "shd (flat ws) = hd (shd ws)"
traytel@50518
   112
  "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else Stream (tl (shd ws)) (stl ws))"
traytel@50518
   113
unfolding flat_def by auto
traytel@50518
   114
traytel@50518
   115
lemma flat_Cons[simp]: "flat (Stream (x#xs) w) = Stream x (flat (if xs = [] then w else Stream xs w))"
traytel@50518
   116
unfolding flat_def using stream.unfold[of "hd o shd" _ "Stream (x#xs) w"] by auto
traytel@50518
   117
traytel@50518
   118
lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (Stream xs ws) = xs @- flat ws"
traytel@50518
   119
by (induct xs) auto
traytel@50518
   120
traytel@50518
   121
lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)"
traytel@50518
   122
by (cases ws) auto
traytel@50518
   123
traytel@50518
   124
traytel@50518
   125
subsection {* take, drop, nth for streams *}
traytel@50518
   126
traytel@50518
   127
primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
traytel@50518
   128
  "stake 0 s = []"
traytel@50518
   129
| "stake (Suc n) s = shd s # stake n (stl s)"
traytel@50518
   130
traytel@50518
   131
primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
traytel@50518
   132
  "sdrop 0 s = s"
traytel@50518
   133
| "sdrop (Suc n) s = sdrop n (stl s)"
traytel@50518
   134
traytel@50518
   135
primrec snth :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a" where
traytel@50518
   136
  "snth 0 s = shd s"
traytel@50518
   137
| "snth (Suc n) s = snth n (stl s)"
traytel@50518
   138
traytel@50518
   139
lemma stake_sdrop: "stake n s @- sdrop n s = s"
traytel@50518
   140
by (induct n arbitrary: s) auto
traytel@50518
   141
traytel@50518
   142
lemma stake_empty: "stake n s = [] \<longleftrightarrow> n = 0"
traytel@50518
   143
by (cases n) auto
traytel@50518
   144
traytel@50518
   145
lemma sdrop_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> sdrop n s = s'"
traytel@50518
   146
by (induct n arbitrary: w s) auto
traytel@50518
   147
traytel@50518
   148
lemma stake_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> stake n s = w"
traytel@50518
   149
by (induct n arbitrary: w s) auto
traytel@50518
   150
traytel@50518
   151
lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s"
traytel@50518
   152
by (induct m arbitrary: s) auto
traytel@50518
   153
traytel@50518
   154
lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"
traytel@50518
   155
by (induct m arbitrary: s) auto
traytel@50518
   156
traytel@50518
   157
lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @- s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @- s"
traytel@50518
   158
by (auto dest: arg_cong[of _ _ stl])
traytel@50518
   159
traytel@50518
   160
lemma stake_append: "stake n (u @- s) = take (min (length u) n) u @ stake (n - length u) s"
traytel@50518
   161
proof (induct n arbitrary: u)
traytel@50518
   162
  case (Suc n) thus ?case by (cases u) auto
traytel@50518
   163
qed auto
traytel@50518
   164
traytel@50518
   165
lemma stake_cycle_le[simp]:
traytel@50518
   166
  assumes "u \<noteq> []" "n < length u"
traytel@50518
   167
  shows "stake n (cycle u) = take n u"
traytel@50518
   168
using min_absorb2[OF less_imp_le_nat[OF assms(2)]]
traytel@50518
   169
by (subst cycle_decomp[OF assms(1)], subst stake_append) auto
traytel@50518
   170
traytel@50518
   171
lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u"
traytel@50518
   172
by (metis cycle_decomp stake_shift)
traytel@50518
   173
traytel@50518
   174
lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u"
traytel@50518
   175
by (metis cycle_decomp sdrop_shift)
traytel@50518
   176
traytel@50518
   177
lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
traytel@50518
   178
   stake n (cycle u) = concat (replicate (n div length u) u)"
traytel@50518
   179
by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric])
traytel@50518
   180
traytel@50518
   181
lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
traytel@50518
   182
   sdrop n (cycle u) = cycle u"
traytel@50518
   183
by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric])
traytel@50518
   184
traytel@50518
   185
lemma stake_cycle: "u \<noteq> [] \<Longrightarrow>
traytel@50518
   186
   stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u"
traytel@50518
   187
by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto
traytel@50518
   188
traytel@50518
   189
lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)"
traytel@50518
   190
by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric])
traytel@50518
   191
traytel@50518
   192
end