src/HOLCF/Library/Stream.thy
author huffman
Tue Oct 12 06:20:05 2010 -0700 (2010-10-12)
changeset 40006 116e94f9543b
parent 40002 c5b5f7a3a3b1
child 40025 876689e6bbdf
permissions -rw-r--r--
remove unneeded lemmas from Fun_Cpo.thy
     1 (*  Title:      HOLCF/ex/Stream.thy
     2     Author:     Franz Regensburger, David von Oheimb, Borislav Gajanovic
     3 *)
     4 
     5 header {* General Stream domain *}
     6 
     7 theory Stream
     8 imports HOLCF Nat_Infinity
     9 begin
    10 
    11 domain 'a stream = scons (ft::'a) (lazy rt::"'a stream") (infixr "&&" 65)
    12 
    13 definition
    14   smap :: "('a \<rightarrow> 'b) \<rightarrow> 'a stream \<rightarrow> 'b stream" where
    15   "smap = fix\<cdot>(\<Lambda> h f s. case s of x && xs \<Rightarrow> f\<cdot>x && h\<cdot>f\<cdot>xs)"
    16 
    17 definition
    18   sfilter :: "('a \<rightarrow> tr) \<rightarrow> 'a stream \<rightarrow> 'a stream" where
    19   "sfilter = fix\<cdot>(\<Lambda> h p s. case s of x && xs \<Rightarrow>
    20                                      If p\<cdot>x then x && h\<cdot>p\<cdot>xs else h\<cdot>p\<cdot>xs fi)"
    21 
    22 definition
    23   slen :: "'a stream \<Rightarrow> inat"  ("#_" [1000] 1000) where
    24   "#s = (if stream_finite s then Fin (LEAST n. stream_take n\<cdot>s = s) else \<infinity>)"
    25 
    26 
    27 (* concatenation *)
    28 
    29 definition
    30   i_rt :: "nat => 'a stream => 'a stream" where (* chops the first i elements *)
    31   "i_rt = (%i s. iterate i$rt$s)"
    32 
    33 definition
    34   i_th :: "nat => 'a stream => 'a" where (* the i-th element *)
    35   "i_th = (%i s. ft$(i_rt i s))"
    36 
    37 definition
    38   sconc :: "'a stream => 'a stream => 'a stream"  (infixr "ooo" 65) where
    39   "s1 ooo s2 = (case #s1 of
    40                   Fin n \<Rightarrow> (SOME s. (stream_take n$s=s1) & (i_rt n s = s2))
    41                | \<infinity>     \<Rightarrow> s1)"
    42 
    43 primrec constr_sconc' :: "nat => 'a stream => 'a stream => 'a stream"
    44 where
    45   constr_sconc'_0:   "constr_sconc' 0 s1 s2 = s2"
    46 | constr_sconc'_Suc: "constr_sconc' (Suc n) s1 s2 = ft$s1 &&
    47                                                     constr_sconc' n (rt$s1) s2"
    48 
    49 definition
    50   constr_sconc  :: "'a stream => 'a stream => 'a stream" where (* constructive *)
    51   "constr_sconc s1 s2 = (case #s1 of
    52                           Fin n \<Rightarrow> constr_sconc' n s1 s2
    53                         | \<infinity>    \<Rightarrow> s1)"
    54 
    55 
    56 (* ----------------------------------------------------------------------- *)
    57 (* theorems about scons                                                    *)
    58 (* ----------------------------------------------------------------------- *)
    59 
    60 
    61 section "scons"
    62 
    63 lemma scons_eq_UU: "(a && s = UU) = (a = UU)"
    64 by simp
    65 
    66 lemma scons_not_empty: "[| a && x = UU; a ~= UU |] ==> R"
    67 by simp
    68 
    69 lemma stream_exhaust_eq: "(x ~= UU) = (EX a y. a ~= UU &  x = a && y)"
    70 by (cases x, auto)
    71 
    72 lemma stream_neq_UU: "x~=UU ==> EX a a_s. x=a&&a_s & a~=UU"
    73 by (simp add: stream_exhaust_eq,auto)
    74 
    75 lemma stream_prefix:
    76   "[| a && s << t; a ~= UU  |] ==> EX b tt. t = b && tt &  b ~= UU &  s << tt"
    77 by (cases t, auto)
    78 
    79 lemma stream_prefix':
    80   "b ~= UU ==> x << b && z =
    81    (x = UU |  (EX a y. x = a && y &  a ~= UU &  a << b &  y << z))"
    82 by (cases x, auto)
    83 
    84 
    85 (*
    86 lemma stream_prefix1: "[| x<<y; xs<<ys |] ==> x&&xs << y&&ys"
    87 by (insert stream_prefix' [of y "x&&xs" ys],force)
    88 *)
    89 
    90 lemma stream_flat_prefix:
    91   "[| x && xs << y && ys; (x::'a::flat) ~= UU|] ==> x = y & xs << ys"
    92 apply (case_tac "y=UU",auto)
    93 by (drule ax_flat,simp)
    94 
    95 
    96 
    97 
    98 (* ----------------------------------------------------------------------- *)
    99 (* theorems about stream_when                                              *)
   100 (* ----------------------------------------------------------------------- *)
   101 
   102 section "stream_when"
   103 
   104 
   105 lemma stream_when_strictf: "stream_when$UU$s=UU"
   106 by (cases s, auto)
   107 
   108 
   109 
   110 (* ----------------------------------------------------------------------- *)
   111 (* theorems about ft and rt                                                *)
   112 (* ----------------------------------------------------------------------- *)
   113 
   114 
   115 section "ft & rt"
   116 
   117 
   118 lemma ft_defin: "s~=UU ==> ft$s~=UU"
   119 by simp
   120 
   121 lemma rt_strict_rev: "rt$s~=UU ==> s~=UU"
   122 by auto
   123 
   124 lemma surjectiv_scons: "(ft$s)&&(rt$s)=s"
   125 by (cases s, auto)
   126 
   127 lemma monofun_rt_mult: "x << s ==> iterate i$rt$x << iterate i$rt$s"
   128 by (rule monofun_cfun_arg)
   129 
   130 
   131 
   132 (* ----------------------------------------------------------------------- *)
   133 (* theorems about stream_take                                              *)
   134 (* ----------------------------------------------------------------------- *)
   135 
   136 
   137 section "stream_take"
   138 
   139 
   140 lemma stream_reach2: "(LUB i. stream_take i$s) = s"
   141 by (rule stream.reach)
   142 
   143 lemma chain_stream_take: "chain (%i. stream_take i$s)"
   144 by simp
   145 
   146 lemma stream_take_prefix [simp]: "stream_take n$s << s"
   147 apply (insert stream_reach2 [of s])
   148 apply (erule subst) back
   149 apply (rule is_ub_thelub)
   150 by (simp only: chain_stream_take)
   151 
   152 lemma stream_take_more [rule_format]:
   153   "ALL x. stream_take n$x = x --> stream_take (Suc n)$x = x"
   154 apply (induct_tac n,auto)
   155 apply (case_tac "x=UU",auto)
   156 by (drule stream_exhaust_eq [THEN iffD1],auto)
   157 
   158 lemma stream_take_lemma3 [rule_format]:
   159   "ALL x xs. x~=UU --> stream_take n$(x && xs) = x && xs --> stream_take n$xs=xs"
   160 apply (induct_tac n,clarsimp)
   161 (*apply (drule sym, erule scons_not_empty, simp)*)
   162 apply (clarify, rule stream_take_more)
   163 apply (erule_tac x="x" in allE)
   164 by (erule_tac x="xs" in allE,simp)
   165 
   166 lemma stream_take_lemma4:
   167   "ALL x xs. stream_take n$xs=xs --> stream_take (Suc n)$(x && xs) = x && xs"
   168 by auto
   169 
   170 lemma stream_take_idempotent [rule_format, simp]:
   171  "ALL s. stream_take n$(stream_take n$s) = stream_take n$s"
   172 apply (induct_tac n, auto)
   173 apply (case_tac "s=UU", auto)
   174 by (drule stream_exhaust_eq [THEN iffD1], auto)
   175 
   176 lemma stream_take_take_Suc [rule_format, simp]:
   177   "ALL s. stream_take n$(stream_take (Suc n)$s) =
   178                                     stream_take n$s"
   179 apply (induct_tac n, auto)
   180 apply (case_tac "s=UU", auto)
   181 by (drule stream_exhaust_eq [THEN iffD1], auto)
   182 
   183 lemma mono_stream_take_pred:
   184   "stream_take (Suc n)$s1 << stream_take (Suc n)$s2 ==>
   185                        stream_take n$s1 << stream_take n$s2"
   186 by (insert monofun_cfun_arg [of "stream_take (Suc n)$s1"
   187   "stream_take (Suc n)$s2" "stream_take n"], auto)
   188 (*
   189 lemma mono_stream_take_pred:
   190   "stream_take (Suc n)$s1 << stream_take (Suc n)$s2 ==>
   191                        stream_take n$s1 << stream_take n$s2"
   192 by (drule mono_stream_take [of _ _ n],simp)
   193 *)
   194 
   195 lemma stream_take_lemma10 [rule_format]:
   196   "ALL k<=n. stream_take n$s1 << stream_take n$s2
   197                              --> stream_take k$s1 << stream_take k$s2"
   198 apply (induct_tac n,simp,clarsimp)
   199 apply (case_tac "k=Suc n",blast)
   200 apply (erule_tac x="k" in allE)
   201 by (drule mono_stream_take_pred,simp)
   202 
   203 lemma stream_take_le_mono : "k<=n ==> stream_take k$s1 << stream_take n$s1"
   204 apply (insert chain_stream_take [of s1])
   205 by (drule chain_mono,auto)
   206 
   207 lemma mono_stream_take: "s1 << s2 ==> stream_take n$s1 << stream_take n$s2"
   208 by (simp add: monofun_cfun_arg)
   209 
   210 (*
   211 lemma stream_take_prefix [simp]: "stream_take n$s << s"
   212 apply (subgoal_tac "s=(LUB n. stream_take n$s)")
   213  apply (erule ssubst, rule is_ub_thelub)
   214  apply (simp only: chain_stream_take)
   215 by (simp only: stream_reach2)
   216 *)
   217 
   218 lemma stream_take_take_less:"stream_take k$(stream_take n$s) << stream_take k$s"
   219 by (rule monofun_cfun_arg,auto)
   220 
   221 
   222 (* ------------------------------------------------------------------------- *)
   223 (* special induction rules                                                   *)
   224 (* ------------------------------------------------------------------------- *)
   225 
   226 
   227 section "induction"
   228 
   229 lemma stream_finite_ind:
   230  "[| stream_finite x; P UU; !!a s. [| a ~= UU; P s |] ==> P (a && s) |] ==> P x"
   231 apply (simp add: stream.finite_def,auto)
   232 apply (erule subst)
   233 by (drule stream.finite_induct [of P _ x], auto)
   234 
   235 lemma stream_finite_ind2:
   236 "[| P UU; !! x. x ~= UU ==> P (x && UU); !! y z s. [| y ~= UU; z ~= UU; P s |] ==> P (y && z && s )|] ==>
   237                                  !s. P (stream_take n$s)"
   238 apply (rule nat_less_induct [of _ n],auto)
   239 apply (case_tac n, auto) 
   240 apply (case_tac nat, auto) 
   241 apply (case_tac "s=UU",clarsimp)
   242 apply (drule stream_exhaust_eq [THEN iffD1],clarsimp)
   243 apply (case_tac "s=UU",clarsimp)
   244 apply (drule stream_exhaust_eq [THEN iffD1],clarsimp)
   245 apply (case_tac "y=UU",clarsimp)
   246 by (drule stream_exhaust_eq [THEN iffD1],clarsimp)
   247 
   248 lemma stream_ind2:
   249 "[| adm P; P UU; !!a. a ~= UU ==> P (a && UU); !!a b s. [| a ~= UU; b ~= UU; P s |] ==> P (a && b && s) |] ==> P x"
   250 apply (insert stream.reach [of x],erule subst)
   251 apply (erule admD, rule chain_stream_take)
   252 apply (insert stream_finite_ind2 [of P])
   253 by simp
   254 
   255 
   256 
   257 (* ----------------------------------------------------------------------- *)
   258 (* simplify use of coinduction                                             *)
   259 (* ----------------------------------------------------------------------- *)
   260 
   261 
   262 section "coinduction"
   263 
   264 lemma stream_coind_lemma2: "!s1 s2. R s1 s2 --> ft$s1 = ft$s2 &  R (rt$s1) (rt$s2) ==> stream_bisim R"
   265  apply (simp add: stream.bisim_def,clarsimp)
   266  apply (drule spec, drule spec, drule (1) mp)
   267  apply (case_tac "x", simp)
   268  apply (case_tac "x'", simp)
   269 by auto
   270 
   271 
   272 
   273 (* ----------------------------------------------------------------------- *)
   274 (* theorems about stream_finite                                            *)
   275 (* ----------------------------------------------------------------------- *)
   276 
   277 
   278 section "stream_finite"
   279 
   280 lemma stream_finite_UU [simp]: "stream_finite UU"
   281 by (simp add: stream.finite_def)
   282 
   283 lemma stream_finite_UU_rev: "~  stream_finite s ==> s ~= UU"
   284 by (auto simp add: stream.finite_def)
   285 
   286 lemma stream_finite_lemma1: "stream_finite xs ==> stream_finite (x && xs)"
   287 apply (simp add: stream.finite_def,auto)
   288 apply (rule_tac x="Suc n" in exI)
   289 by (simp add: stream_take_lemma4)
   290 
   291 lemma stream_finite_lemma2: "[| x ~= UU; stream_finite (x && xs) |] ==> stream_finite xs"
   292 apply (simp add: stream.finite_def, auto)
   293 apply (rule_tac x="n" in exI)
   294 by (erule stream_take_lemma3,simp)
   295 
   296 lemma stream_finite_rt_eq: "stream_finite (rt$s) = stream_finite s"
   297 apply (cases s, auto)
   298 apply (rule stream_finite_lemma1, simp)
   299 by (rule stream_finite_lemma2,simp)
   300 
   301 lemma stream_finite_less: "stream_finite s ==> !t. t<<s --> stream_finite t"
   302 apply (erule stream_finite_ind [of s], auto)
   303 apply (case_tac "t=UU", auto)
   304 apply (drule stream_exhaust_eq [THEN iffD1],auto)
   305 apply (erule_tac x="y" in allE, simp)
   306 by (rule stream_finite_lemma1, simp)
   307 
   308 lemma stream_take_finite [simp]: "stream_finite (stream_take n$s)"
   309 apply (simp add: stream.finite_def)
   310 by (rule_tac x="n" in exI,simp)
   311 
   312 lemma adm_not_stream_finite: "adm (%x. ~ stream_finite x)"
   313 apply (rule adm_upward)
   314 apply (erule contrapos_nn)
   315 apply (erule (1) stream_finite_less [rule_format])
   316 done
   317 
   318 
   319 
   320 (* ----------------------------------------------------------------------- *)
   321 (* theorems about stream length                                            *)
   322 (* ----------------------------------------------------------------------- *)
   323 
   324 
   325 section "slen"
   326 
   327 lemma slen_empty [simp]: "#\<bottom> = 0"
   328 by (simp add: slen_def stream.finite_def zero_inat_def Least_equality)
   329 
   330 lemma slen_scons [simp]: "x ~= \<bottom> ==> #(x&&xs) = iSuc (#xs)"
   331 apply (case_tac "stream_finite (x && xs)")
   332 apply (simp add: slen_def, auto)
   333 apply (simp add: stream.finite_def, auto simp add: iSuc_Fin)
   334 apply (rule Least_Suc2, auto)
   335 (*apply (drule sym)*)
   336 (*apply (drule sym scons_eq_UU [THEN iffD1],simp)*)
   337 apply (erule stream_finite_lemma2, simp)
   338 apply (simp add: slen_def, auto)
   339 by (drule stream_finite_lemma1,auto)
   340 
   341 lemma slen_less_1_eq: "(#x < Fin (Suc 0)) = (x = \<bottom>)"
   342 by (cases x, auto simp add: Fin_0 iSuc_Fin[THEN sym])
   343 
   344 lemma slen_empty_eq: "(#x = 0) = (x = \<bottom>)"
   345 by (cases x, auto)
   346 
   347 lemma slen_scons_eq: "(Fin (Suc n) < #x) = (? a y. x = a && y &  a ~= \<bottom> &  Fin n < #y)"
   348 apply (auto, case_tac "x=UU",auto)
   349 apply (drule stream_exhaust_eq [THEN iffD1], auto)
   350 apply (case_tac "#y") apply simp_all
   351 apply (case_tac "#y") apply simp_all
   352 done
   353 
   354 lemma slen_iSuc: "#x = iSuc n --> (? a y. x = a&&y &  a ~= \<bottom> &  #y = n)"
   355 by (cases x, auto)
   356 
   357 lemma slen_stream_take_finite [simp]: "#(stream_take n$s) ~= \<infinity>"
   358 by (simp add: slen_def)
   359 
   360 lemma slen_scons_eq_rev: "(#x < Fin (Suc (Suc n))) = (!a y. x ~= a && y |  a = \<bottom> |  #y < Fin (Suc n))"
   361  apply (cases x, auto)
   362    apply (simp add: zero_inat_def)
   363   apply (case_tac "#stream") apply (simp_all add: iSuc_Fin)
   364  apply (case_tac "#stream") apply (simp_all add: iSuc_Fin)
   365 done
   366 
   367 lemma slen_take_lemma4 [rule_format]:
   368   "!s. stream_take n$s ~= s --> #(stream_take n$s) = Fin n"
   369 apply (induct n, auto simp add: Fin_0)
   370 apply (case_tac "s=UU", simp)
   371 by (drule stream_exhaust_eq [THEN iffD1], auto simp add: iSuc_Fin)
   372 
   373 (*
   374 lemma stream_take_idempotent [simp]:
   375  "stream_take n$(stream_take n$s) = stream_take n$s"
   376 apply (case_tac "stream_take n$s = s")
   377 apply (auto,insert slen_take_lemma4 [of n s]);
   378 by (auto,insert slen_take_lemma1 [of "stream_take n$s" n],simp)
   379 
   380 lemma stream_take_take_Suc [simp]: "stream_take n$(stream_take (Suc n)$s) =
   381                                     stream_take n$s"
   382 apply (simp add: po_eq_conv,auto)
   383  apply (simp add: stream_take_take_less)
   384 apply (subgoal_tac "stream_take n$s = stream_take n$(stream_take n$s)")
   385  apply (erule ssubst)
   386  apply (rule_tac monofun_cfun_arg)
   387  apply (insert chain_stream_take [of s])
   388 by (simp add: chain_def,simp)
   389 *)
   390 
   391 lemma slen_take_eq: "ALL x. (Fin n < #x) = (stream_take n\<cdot>x ~= x)"
   392 apply (induct_tac n, auto)
   393 apply (simp add: Fin_0, clarsimp)
   394 apply (drule not_sym)
   395 apply (drule slen_empty_eq [THEN iffD1], simp)
   396 apply (case_tac "x=UU", simp)
   397 apply (drule stream_exhaust_eq [THEN iffD1], clarsimp)
   398 apply (erule_tac x="y" in allE, auto)
   399 apply (simp_all add: not_less iSuc_Fin)
   400 apply (case_tac "#y") apply simp_all
   401 apply (case_tac "x=UU", simp)
   402 apply (drule stream_exhaust_eq [THEN iffD1], clarsimp)
   403 apply (erule_tac x="y" in allE, simp)
   404 apply (case_tac "#y") by simp_all
   405 
   406 lemma slen_take_eq_rev: "(#x <= Fin n) = (stream_take n\<cdot>x = x)"
   407 by (simp add: linorder_not_less [symmetric] slen_take_eq)
   408 
   409 lemma slen_take_lemma1: "#x = Fin n ==> stream_take n\<cdot>x = x"
   410 by (rule slen_take_eq_rev [THEN iffD1], auto)
   411 
   412 lemma slen_rt_mono: "#s2 <= #s1 ==> #(rt$s2) <= #(rt$s1)"
   413 apply (cases s1)
   414  by (cases s2, simp+)+
   415 
   416 lemma slen_take_lemma5: "#(stream_take n$s) <= Fin n"
   417 apply (case_tac "stream_take n$s = s")
   418  apply (simp add: slen_take_eq_rev)
   419 by (simp add: slen_take_lemma4)
   420 
   421 lemma slen_take_lemma2: "!x. ~stream_finite x --> #(stream_take i\<cdot>x) = Fin i"
   422 apply (simp add: stream.finite_def, auto)
   423 by (simp add: slen_take_lemma4)
   424 
   425 lemma slen_infinite: "stream_finite x = (#x ~= Infty)"
   426 by (simp add: slen_def)
   427 
   428 lemma slen_mono_lemma: "stream_finite s ==> ALL t. s << t --> #s <= #t"
   429 apply (erule stream_finite_ind [of s], auto)
   430 apply (case_tac "t=UU", auto)
   431 apply (drule stream_exhaust_eq [THEN iffD1], auto)
   432 done
   433 
   434 lemma slen_mono: "s << t ==> #s <= #t"
   435 apply (case_tac "stream_finite t")
   436 apply (frule stream_finite_less)
   437 apply (erule_tac x="s" in allE, simp)
   438 apply (drule slen_mono_lemma, auto)
   439 by (simp add: slen_def)
   440 
   441 lemma iterate_lemma: "F$(iterate n$F$x) = iterate n$F$(F$x)"
   442 by (insert iterate_Suc2 [of n F x], auto)
   443 
   444 lemma slen_rt_mult [rule_format]: "!x. Fin (i + j) <= #x --> Fin j <= #(iterate i$rt$x)"
   445 apply (induct i, auto)
   446 apply (case_tac "x=UU", auto simp add: zero_inat_def)
   447 apply (drule stream_exhaust_eq [THEN iffD1], auto)
   448 apply (erule_tac x="y" in allE, auto)
   449 apply (simp add: not_le) apply (case_tac "#y") apply (simp_all add: iSuc_Fin)
   450 by (simp add: iterate_lemma)
   451 
   452 lemma slen_take_lemma3 [rule_format]:
   453   "!(x::'a::flat stream) y. Fin n <= #x --> x << y --> stream_take n\<cdot>x = stream_take n\<cdot>y"
   454 apply (induct_tac n, auto)
   455 apply (case_tac "x=UU", auto)
   456 apply (simp add: zero_inat_def)
   457 apply (simp add: Suc_ile_eq)
   458 apply (case_tac "y=UU", clarsimp)
   459 apply (drule stream_exhaust_eq [THEN iffD1], clarsimp)+
   460 apply (erule_tac x="ya" in allE, simp)
   461 by (drule ax_flat, simp)
   462 
   463 lemma slen_strict_mono_lemma:
   464   "stream_finite t ==> !s. #(s::'a::flat stream) = #t &  s << t --> s = t"
   465 apply (erule stream_finite_ind, auto)
   466 apply (case_tac "sa=UU", auto)
   467 apply (drule stream_exhaust_eq [THEN iffD1], clarsimp)
   468 by (drule ax_flat, simp)
   469 
   470 lemma slen_strict_mono: "[|stream_finite t; s ~= t; s << (t::'a::flat stream) |] ==> #s < #t"
   471 by (auto simp add: slen_mono less_le dest: slen_strict_mono_lemma)
   472 
   473 lemma stream_take_Suc_neq: "stream_take (Suc n)$s ~=s ==>
   474                      stream_take n$s ~= stream_take (Suc n)$s"
   475 apply auto
   476 apply (subgoal_tac "stream_take n$s ~=s")
   477  apply (insert slen_take_lemma4 [of n s],auto)
   478 apply (cases s, simp)
   479 by (simp add: slen_take_lemma4 iSuc_Fin)
   480 
   481 (* ----------------------------------------------------------------------- *)
   482 (* theorems about smap                                                     *)
   483 (* ----------------------------------------------------------------------- *)
   484 
   485 
   486 section "smap"
   487 
   488 lemma smap_unfold: "smap = (\<Lambda> f t. case t of x&&xs \<Rightarrow> f$x && smap$f$xs)"
   489 by (insert smap_def [where 'a='a and 'b='b, THEN eq_reflection, THEN fix_eq2], auto)
   490 
   491 lemma smap_empty [simp]: "smap\<cdot>f\<cdot>\<bottom> = \<bottom>"
   492 by (subst smap_unfold, simp)
   493 
   494 lemma smap_scons [simp]: "x~=\<bottom> ==> smap\<cdot>f\<cdot>(x&&xs) = (f\<cdot>x)&&(smap\<cdot>f\<cdot>xs)"
   495 by (subst smap_unfold, force)
   496 
   497 
   498 
   499 (* ----------------------------------------------------------------------- *)
   500 (* theorems about sfilter                                                  *)
   501 (* ----------------------------------------------------------------------- *)
   502 
   503 section "sfilter"
   504 
   505 lemma sfilter_unfold:
   506  "sfilter = (\<Lambda> p s. case s of x && xs \<Rightarrow>
   507   If p\<cdot>x then x && sfilter\<cdot>p\<cdot>xs else sfilter\<cdot>p\<cdot>xs fi)"
   508 by (insert sfilter_def [where 'a='a, THEN eq_reflection, THEN fix_eq2], auto)
   509 
   510 lemma strict_sfilter: "sfilter\<cdot>\<bottom> = \<bottom>"
   511 apply (rule cfun_eqI)
   512 apply (subst sfilter_unfold, auto)
   513 apply (case_tac "x=UU", auto)
   514 by (drule stream_exhaust_eq [THEN iffD1], auto)
   515 
   516 lemma sfilter_empty [simp]: "sfilter\<cdot>f\<cdot>\<bottom> = \<bottom>"
   517 by (subst sfilter_unfold, force)
   518 
   519 lemma sfilter_scons [simp]:
   520   "x ~= \<bottom> ==> sfilter\<cdot>f\<cdot>(x && xs) =
   521                            If f\<cdot>x then x && sfilter\<cdot>f\<cdot>xs else sfilter\<cdot>f\<cdot>xs fi"
   522 by (subst sfilter_unfold, force)
   523 
   524 
   525 (* ----------------------------------------------------------------------- *)
   526    section "i_rt"
   527 (* ----------------------------------------------------------------------- *)
   528 
   529 lemma i_rt_UU [simp]: "i_rt n UU = UU"
   530   by (induct n) (simp_all add: i_rt_def)
   531 
   532 lemma i_rt_0 [simp]: "i_rt 0 s = s"
   533 by (simp add: i_rt_def)
   534 
   535 lemma i_rt_Suc [simp]: "a ~= UU ==> i_rt (Suc n) (a&&s) = i_rt n s"
   536 by (simp add: i_rt_def iterate_Suc2 del: iterate_Suc)
   537 
   538 lemma i_rt_Suc_forw: "i_rt (Suc n) s = i_rt n (rt$s)"
   539 by (simp only: i_rt_def iterate_Suc2)
   540 
   541 lemma i_rt_Suc_back:"i_rt (Suc n) s = rt$(i_rt n s)"
   542 by (simp only: i_rt_def,auto)
   543 
   544 lemma i_rt_mono: "x << s ==> i_rt n x  << i_rt n s"
   545 by (simp add: i_rt_def monofun_rt_mult)
   546 
   547 lemma i_rt_ij_lemma: "Fin (i + j) <= #x ==> Fin j <= #(i_rt i x)"
   548 by (simp add: i_rt_def slen_rt_mult)
   549 
   550 lemma slen_i_rt_mono: "#s2 <= #s1 ==> #(i_rt n s2) <= #(i_rt n s1)"
   551 apply (induct_tac n,auto)
   552 apply (simp add: i_rt_Suc_back)
   553 by (drule slen_rt_mono,simp)
   554 
   555 lemma i_rt_take_lemma1 [rule_format]: "ALL s. i_rt n (stream_take n$s) = UU"
   556 apply (induct_tac n)
   557  apply (simp add: i_rt_Suc_back,auto)
   558 apply (case_tac "s=UU",auto)
   559 by (drule stream_exhaust_eq [THEN iffD1],auto)
   560 
   561 lemma i_rt_slen: "(i_rt n s = UU) = (stream_take n$s = s)"
   562 apply auto
   563  apply (insert i_rt_ij_lemma [of n "Suc 0" s])
   564  apply (subgoal_tac "#(i_rt n s)=0")
   565   apply (case_tac "stream_take n$s = s",simp+)
   566   apply (insert slen_take_eq [rule_format,of n s],simp)
   567   apply (cases "#s") apply (simp_all add: zero_inat_def)
   568   apply (simp add: slen_take_eq)
   569   apply (cases "#s")
   570   using i_rt_take_lemma1 [of n s]
   571   apply (simp_all add: zero_inat_def)
   572   done
   573 
   574 lemma i_rt_lemma_slen: "#s=Fin n ==> i_rt n s = UU"
   575 by (simp add: i_rt_slen slen_take_lemma1)
   576 
   577 lemma stream_finite_i_rt [simp]: "stream_finite (i_rt n s) = stream_finite s"
   578 apply (induct_tac n, auto)
   579  apply (cases s, auto simp del: i_rt_Suc)
   580 by (simp add: i_rt_Suc_back stream_finite_rt_eq)+
   581 
   582 lemma take_i_rt_len_lemma: "ALL sl x j t. Fin sl = #x & n <= sl &
   583                             #(stream_take n$x) = Fin t & #(i_rt n x)= Fin j
   584                                               --> Fin (j + t) = #x"
   585 apply (induct n, auto)
   586  apply (simp add: zero_inat_def)
   587 apply (case_tac "x=UU",auto)
   588  apply (simp add: zero_inat_def)
   589 apply (drule stream_exhaust_eq [THEN iffD1],clarsimp)
   590 apply (subgoal_tac "EX k. Fin k = #y",clarify)
   591  apply (erule_tac x="k" in allE)
   592  apply (erule_tac x="y" in allE,auto)
   593  apply (erule_tac x="THE p. Suc p = t" in allE,auto)
   594    apply (simp add: iSuc_def split: inat.splits)
   595   apply (simp add: iSuc_def split: inat.splits)
   596   apply (simp only: the_equality)
   597  apply (simp add: iSuc_def split: inat.splits)
   598  apply force
   599 apply (simp add: iSuc_def split: inat.splits)
   600 done
   601 
   602 lemma take_i_rt_len:
   603 "[| Fin sl = #x; n <= sl; #(stream_take n$x) = Fin t; #(i_rt n x) = Fin j |] ==>
   604     Fin (j + t) = #x"
   605 by (blast intro: take_i_rt_len_lemma [rule_format])
   606 
   607 
   608 (* ----------------------------------------------------------------------- *)
   609    section "i_th"
   610 (* ----------------------------------------------------------------------- *)
   611 
   612 lemma i_th_i_rt_step:
   613 "[| i_th n s1 << i_th n s2; i_rt (Suc n) s1 << i_rt (Suc n) s2 |] ==>
   614    i_rt n s1 << i_rt n s2"
   615 apply (simp add: i_th_def i_rt_Suc_back)
   616 apply (cases "i_rt n s1", simp)
   617 apply (cases "i_rt n s2", auto)
   618 done
   619 
   620 lemma i_th_stream_take_Suc [rule_format]:
   621  "ALL s. i_th n (stream_take (Suc n)$s) = i_th n s"
   622 apply (induct_tac n,auto)
   623  apply (simp add: i_th_def)
   624  apply (case_tac "s=UU",auto)
   625  apply (drule stream_exhaust_eq [THEN iffD1],auto)
   626 apply (case_tac "s=UU",simp add: i_th_def)
   627 apply (drule stream_exhaust_eq [THEN iffD1],auto)
   628 by (simp add: i_th_def i_rt_Suc_forw)
   629 
   630 lemma i_th_last: "i_th n s && UU = i_rt n (stream_take (Suc n)$s)"
   631 apply (insert surjectiv_scons [of "i_rt n (stream_take (Suc n)$s)"])
   632 apply (rule i_th_stream_take_Suc [THEN subst])
   633 apply (simp add: i_th_def  i_rt_Suc_back [symmetric])
   634 by (simp add: i_rt_take_lemma1)
   635 
   636 lemma i_th_last_eq:
   637 "i_th n s1 = i_th n s2 ==> i_rt n (stream_take (Suc n)$s1) = i_rt n (stream_take (Suc n)$s2)"
   638 apply (insert i_th_last [of n s1])
   639 apply (insert i_th_last [of n s2])
   640 by auto
   641 
   642 lemma i_th_prefix_lemma:
   643 "[| k <= n; stream_take (Suc n)$s1 << stream_take (Suc n)$s2 |] ==>
   644     i_th k s1 << i_th k s2"
   645 apply (insert i_th_stream_take_Suc [of k s1, THEN sym])
   646 apply (insert i_th_stream_take_Suc [of k s2, THEN sym],auto)
   647 apply (simp add: i_th_def)
   648 apply (rule monofun_cfun, auto)
   649 apply (rule i_rt_mono)
   650 by (blast intro: stream_take_lemma10)
   651 
   652 lemma take_i_rt_prefix_lemma1:
   653   "stream_take (Suc n)$s1 << stream_take (Suc n)$s2 ==>
   654    i_rt (Suc n) s1 << i_rt (Suc n) s2 ==>
   655    i_rt n s1 << i_rt n s2 & stream_take n$s1 << stream_take n$s2"
   656 apply auto
   657  apply (insert i_th_prefix_lemma [of n n s1 s2])
   658  apply (rule i_th_i_rt_step,auto)
   659 by (drule mono_stream_take_pred,simp)
   660 
   661 lemma take_i_rt_prefix_lemma:
   662 "[| stream_take n$s1 << stream_take n$s2; i_rt n s1 << i_rt n s2 |] ==> s1 << s2"
   663 apply (case_tac "n=0",simp)
   664 apply (auto)
   665 apply (subgoal_tac "stream_take 0$s1 << stream_take 0$s2 &
   666                     i_rt 0 s1 << i_rt 0 s2")
   667  defer 1
   668  apply (rule zero_induct,blast)
   669  apply (blast dest: take_i_rt_prefix_lemma1)
   670 by simp
   671 
   672 lemma streams_prefix_lemma: "(s1 << s2) =
   673   (stream_take n$s1 << stream_take n$s2 & i_rt n s1 << i_rt n s2)"
   674 apply auto
   675   apply (simp add: monofun_cfun_arg)
   676  apply (simp add: i_rt_mono)
   677 by (erule take_i_rt_prefix_lemma,simp)
   678 
   679 lemma streams_prefix_lemma1:
   680  "[| stream_take n$s1 = stream_take n$s2; i_rt n s1 = i_rt n s2 |] ==> s1 = s2"
   681 apply (simp add: po_eq_conv,auto)
   682  apply (insert streams_prefix_lemma)
   683  by blast+
   684 
   685 
   686 (* ----------------------------------------------------------------------- *)
   687    section "sconc"
   688 (* ----------------------------------------------------------------------- *)
   689 
   690 lemma UU_sconc [simp]: " UU ooo s = s "
   691 by (simp add: sconc_def zero_inat_def)
   692 
   693 lemma scons_neq_UU: "a~=UU ==> a && s ~=UU"
   694 by auto
   695 
   696 lemma singleton_sconc [rule_format, simp]: "x~=UU --> (x && UU) ooo y = x && y"
   697 apply (simp add: sconc_def zero_inat_def iSuc_def split: inat.splits, auto)
   698 apply (rule someI2_ex,auto)
   699  apply (rule_tac x="x && y" in exI,auto)
   700 apply (simp add: i_rt_Suc_forw)
   701 apply (case_tac "xa=UU",simp)
   702 by (drule stream_exhaust_eq [THEN iffD1],auto)
   703 
   704 lemma ex_sconc [rule_format]:
   705   "ALL k y. #x = Fin k --> (EX w. stream_take k$w = x & i_rt k w = y)"
   706 apply (case_tac "#x")
   707  apply (rule stream_finite_ind [of x],auto)
   708   apply (simp add: stream.finite_def)
   709   apply (drule slen_take_lemma1,blast)
   710  apply (simp_all add: zero_inat_def iSuc_def split: inat.splits)
   711 apply (erule_tac x="y" in allE,auto)
   712 by (rule_tac x="a && w" in exI,auto)
   713 
   714 lemma rt_sconc1: "Fin n = #x ==> i_rt n (x ooo y) = y"
   715 apply (simp add: sconc_def split: inat.splits, arith?,auto)
   716 apply (rule someI2_ex,auto)
   717 by (drule ex_sconc,simp)
   718 
   719 lemma sconc_inj2: "\<lbrakk>Fin n = #x; x ooo y = x ooo z\<rbrakk> \<Longrightarrow> y = z"
   720 apply (frule_tac y=y in rt_sconc1)
   721 by (auto elim: rt_sconc1)
   722 
   723 lemma sconc_UU [simp]:"s ooo UU = s"
   724 apply (case_tac "#s")
   725  apply (simp add: sconc_def)
   726  apply (rule someI2_ex)
   727   apply (rule_tac x="s" in exI)
   728   apply auto
   729    apply (drule slen_take_lemma1,auto)
   730   apply (simp add: i_rt_lemma_slen)
   731  apply (drule slen_take_lemma1,auto)
   732  apply (simp add: i_rt_slen)
   733 by (simp add: sconc_def)
   734 
   735 lemma stream_take_sconc [simp]: "Fin n = #x ==> stream_take n$(x ooo y) = x"
   736 apply (simp add: sconc_def)
   737 apply (cases "#x")
   738 apply auto
   739 apply (rule someI2_ex, auto)
   740 by (drule ex_sconc,simp)
   741 
   742 lemma scons_sconc [rule_format,simp]: "a~=UU --> (a && x) ooo y = a && x ooo y"
   743 apply (cases "#x",auto)
   744  apply (simp add: sconc_def iSuc_Fin)
   745  apply (rule someI2_ex)
   746   apply (drule ex_sconc, simp)
   747  apply (rule someI2_ex, auto)
   748   apply (simp add: i_rt_Suc_forw)
   749   apply (rule_tac x="a && x" in exI, auto)
   750  apply (case_tac "xa=UU",auto)
   751  apply (drule stream_exhaust_eq [THEN iffD1],auto)
   752  apply (drule streams_prefix_lemma1,simp+)
   753 by (simp add: sconc_def)
   754 
   755 lemma ft_sconc: "x ~= UU ==> ft$(x ooo y) = ft$x"
   756 by (cases x, auto)
   757 
   758 lemma sconc_assoc: "(x ooo y) ooo z = x ooo y ooo z"
   759 apply (case_tac "#x")
   760  apply (rule stream_finite_ind [of x],auto simp del: scons_sconc)
   761   apply (simp add: stream.finite_def del: scons_sconc)
   762   apply (drule slen_take_lemma1,auto simp del: scons_sconc)
   763  apply (case_tac "a = UU", auto)
   764 by (simp add: sconc_def)
   765 
   766 
   767 (* ----------------------------------------------------------------------- *)
   768 
   769 lemma cont_sconc_lemma1: "stream_finite x \<Longrightarrow> cont (\<lambda>y. x ooo y)"
   770 by (erule stream_finite_ind, simp_all)
   771 
   772 lemma cont_sconc_lemma2: "\<not> stream_finite x \<Longrightarrow> cont (\<lambda>y. x ooo y)"
   773 by (simp add: sconc_def slen_def)
   774 
   775 lemma cont_sconc: "cont (\<lambda>y. x ooo y)"
   776 apply (cases "stream_finite x")
   777 apply (erule cont_sconc_lemma1)
   778 apply (erule cont_sconc_lemma2)
   779 done
   780 
   781 lemma sconc_mono: "y << y' ==> x ooo y << x ooo y'"
   782 by (rule cont_sconc [THEN cont2mono, THEN monofunE])
   783 
   784 lemma sconc_mono1 [simp]: "x << x ooo y"
   785 by (rule sconc_mono [of UU, simplified])
   786 
   787 (* ----------------------------------------------------------------------- *)
   788 
   789 lemma empty_sconc [simp]: "(x ooo y = UU) = (x = UU & y = UU)"
   790 apply (case_tac "#x",auto)
   791    apply (insert sconc_mono1 [of x y])
   792    by auto
   793 
   794 (* ----------------------------------------------------------------------- *)
   795 
   796 lemma rt_sconc [rule_format, simp]: "s~=UU --> rt$(s ooo x) = rt$s ooo x"
   797 by (cases s, auto)
   798 
   799 lemma i_th_sconc_lemma [rule_format]:
   800   "ALL x y. Fin n < #x --> i_th n (x ooo y) = i_th n x"
   801 apply (induct_tac n, auto)
   802 apply (simp add: Fin_0 i_th_def)
   803 apply (simp add: slen_empty_eq ft_sconc)
   804 apply (simp add: i_th_def)
   805 apply (case_tac "x=UU",auto)
   806 apply (drule stream_exhaust_eq [THEN iffD1], auto)
   807 apply (erule_tac x="ya" in allE)
   808 apply (case_tac "#ya") by simp_all
   809 
   810 
   811 
   812 (* ----------------------------------------------------------------------- *)
   813 
   814 lemma sconc_lemma [rule_format, simp]: "ALL s. stream_take n$s ooo i_rt n s = s"
   815 apply (induct_tac n,auto)
   816 apply (case_tac "s=UU",auto)
   817 by (drule stream_exhaust_eq [THEN iffD1],auto)
   818 
   819 (* ----------------------------------------------------------------------- *)
   820    subsection "pointwise equality"
   821 (* ----------------------------------------------------------------------- *)
   822 
   823 lemma ex_last_stream_take_scons: "stream_take (Suc n)$s =
   824                      stream_take n$s ooo i_rt n (stream_take (Suc n)$s)"
   825 by (insert sconc_lemma [of n "stream_take (Suc n)$s"],simp)
   826 
   827 lemma i_th_stream_take_eq:
   828 "!!n. ALL n. i_th n s1 = i_th n s2 ==> stream_take n$s1 = stream_take n$s2"
   829 apply (induct_tac n,auto)
   830 apply (subgoal_tac "stream_take (Suc na)$s1 =
   831                     stream_take na$s1 ooo i_rt na (stream_take (Suc na)$s1)")
   832  apply (subgoal_tac "i_rt na (stream_take (Suc na)$s1) =
   833                     i_rt na (stream_take (Suc na)$s2)")
   834   apply (subgoal_tac "stream_take (Suc na)$s2 =
   835                     stream_take na$s2 ooo i_rt na (stream_take (Suc na)$s2)")
   836    apply (insert ex_last_stream_take_scons,simp)
   837   apply blast
   838  apply (erule_tac x="na" in allE)
   839  apply (insert i_th_last_eq [of _ s1 s2])
   840 by blast+
   841 
   842 lemma pointwise_eq_lemma[rule_format]: "ALL n. i_th n s1 = i_th n s2 ==> s1 = s2"
   843 by (insert i_th_stream_take_eq [THEN stream.take_lemma],blast)
   844 
   845 (* ----------------------------------------------------------------------- *)
   846    subsection "finiteness"
   847 (* ----------------------------------------------------------------------- *)
   848 
   849 lemma slen_sconc_finite1:
   850   "[| #(x ooo y) = Infty; Fin n = #x |] ==> #y = Infty"
   851 apply (case_tac "#y ~= Infty",auto)
   852 apply (drule_tac y=y in rt_sconc1)
   853 apply (insert stream_finite_i_rt [of n "x ooo y"])
   854 by (simp add: slen_infinite)
   855 
   856 lemma slen_sconc_infinite1: "#x=Infty ==> #(x ooo y) = Infty"
   857 by (simp add: sconc_def)
   858 
   859 lemma slen_sconc_infinite2: "#y=Infty ==> #(x ooo y) = Infty"
   860 apply (case_tac "#x")
   861  apply (simp add: sconc_def)
   862  apply (rule someI2_ex)
   863   apply (drule ex_sconc,auto)
   864  apply (erule contrapos_pp)
   865  apply (insert stream_finite_i_rt)
   866  apply (fastsimp simp add: slen_infinite,auto)
   867 by (simp add: sconc_def)
   868 
   869 lemma sconc_finite: "(#x~=Infty & #y~=Infty) = (#(x ooo y)~=Infty)"
   870 apply auto
   871   apply (metis not_Infty_eq slen_sconc_finite1)
   872  apply (metis not_Infty_eq slen_sconc_infinite1)
   873 apply (metis not_Infty_eq slen_sconc_infinite2)
   874 done
   875 
   876 (* ----------------------------------------------------------------------- *)
   877 
   878 lemma slen_sconc_mono3: "[| Fin n = #x; Fin k = #(x ooo y) |] ==> n <= k"
   879 apply (insert slen_mono [of "x" "x ooo y"])
   880 apply (cases "#x") apply simp_all
   881 apply (cases "#(x ooo y)") apply simp_all
   882 done
   883 
   884 (* ----------------------------------------------------------------------- *)
   885    subsection "finite slen"
   886 (* ----------------------------------------------------------------------- *)
   887 
   888 lemma slen_sconc: "[| Fin n = #x; Fin m = #y |] ==> #(x ooo y) = Fin (n + m)"
   889 apply (case_tac "#(x ooo y)")
   890  apply (frule_tac y=y in rt_sconc1)
   891  apply (insert take_i_rt_len [of "THE j. Fin j = #(x ooo y)" "x ooo y" n n m],simp)
   892  apply (insert slen_sconc_mono3 [of n x _ y],simp)
   893 by (insert sconc_finite [of x y],auto)
   894 
   895 (* ----------------------------------------------------------------------- *)
   896    subsection "flat prefix"
   897 (* ----------------------------------------------------------------------- *)
   898 
   899 lemma sconc_prefix: "(s1::'a::flat stream) << s2 ==> EX t. s1 ooo t = s2"
   900 apply (case_tac "#s1")
   901  apply (subgoal_tac "stream_take nat$s1 = stream_take nat$s2")
   902   apply (rule_tac x="i_rt nat s2" in exI)
   903   apply (simp add: sconc_def)
   904   apply (rule someI2_ex)
   905    apply (drule ex_sconc)
   906    apply (simp,clarsimp,drule streams_prefix_lemma1)
   907    apply (simp+,rule slen_take_lemma3 [of _ s1 s2])
   908   apply (simp+,rule_tac x="UU" in exI)
   909 apply (insert slen_take_lemma3 [of _ s1 s2])
   910 by (rule stream.take_lemma,simp)
   911 
   912 (* ----------------------------------------------------------------------- *)
   913    subsection "continuity"
   914 (* ----------------------------------------------------------------------- *)
   915 
   916 lemma chain_sconc: "chain S ==> chain (%i. (x ooo S i))"
   917 by (simp add: chain_def,auto simp add: sconc_mono)
   918 
   919 lemma chain_scons: "chain S ==> chain (%i. a && S i)"
   920 apply (simp add: chain_def,auto)
   921 by (rule monofun_cfun_arg,simp)
   922 
   923 lemma contlub_scons_lemma: "chain S ==> (LUB i. a && S i) = a && (LUB i. S i)"
   924 by (rule cont2contlubE [OF cont_Rep_CFun2, symmetric])
   925 
   926 lemma finite_lub_sconc: "chain Y ==> (stream_finite x) ==>
   927                         (LUB i. x ooo Y i) = (x ooo (LUB i. Y i))"
   928 apply (rule stream_finite_ind [of x])
   929  apply (auto)
   930 apply (subgoal_tac "(LUB i. a && (s ooo Y i)) = a && (LUB i. s ooo Y i)")
   931  by (force,blast dest: contlub_scons_lemma chain_sconc)
   932 
   933 lemma contlub_sconc_lemma:
   934   "chain Y ==> (LUB i. x ooo Y i) = (x ooo (LUB i. Y i))"
   935 apply (case_tac "#x=Infty")
   936  apply (simp add: sconc_def)
   937 apply (drule finite_lub_sconc,auto simp add: slen_infinite)
   938 done
   939 
   940 lemma monofun_sconc: "monofun (%y. x ooo y)"
   941 by (simp add: monofun_def sconc_mono)
   942 
   943 
   944 (* ----------------------------------------------------------------------- *)
   945    section "constr_sconc"
   946 (* ----------------------------------------------------------------------- *)
   947 
   948 lemma constr_sconc_UUs [simp]: "constr_sconc UU s = s"
   949 by (simp add: constr_sconc_def zero_inat_def)
   950 
   951 lemma "x ooo y = constr_sconc x y"
   952 apply (case_tac "#x")
   953  apply (rule stream_finite_ind [of x],auto simp del: scons_sconc)
   954   defer 1
   955   apply (simp add: constr_sconc_def del: scons_sconc)
   956   apply (case_tac "#s")
   957    apply (simp add: iSuc_Fin)
   958    apply (case_tac "a=UU",auto simp del: scons_sconc)
   959    apply (simp)
   960   apply (simp add: sconc_def)
   961  apply (simp add: constr_sconc_def)
   962 apply (simp add: stream.finite_def)
   963 by (drule slen_take_lemma1,auto)
   964 
   965 end