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