src/HOL/HOLCF/FOCUS/Fstreams.thy
author wenzelm
Wed Dec 29 17:34:41 2010 +0100 (2010-12-29)
changeset 41413 64cd30d6b0b8
parent 40774 0437dbc127b3
child 41431 138f414f14cb
permissions -rw-r--r--
explicit file specifications -- avoid secondary load path;
wenzelm@32960
     1
(*  Title:      HOLCF/FOCUS/Fstreams.thy
wenzelm@32960
     2
    Author:     Borislav Gajanovic
oheimb@15188
     3
wenzelm@32960
     4
FOCUS flat streams (with lifted elements).
wenzelm@32960
     5
wenzelm@32960
     6
TODO: integrate this with Fstream.
oheimb@15188
     7
*)
oheimb@15188
     8
huffman@37110
     9
theory Fstreams
wenzelm@41413
    10
imports "~~/src/HOL/HOLCF/Library/Stream"
huffman@37110
    11
begin
oheimb@15188
    12
wenzelm@36452
    13
default_sort type
oheimb@15188
    14
oheimb@15188
    15
types 'a fstream = "('a lift) stream"
oheimb@15188
    16
wenzelm@19763
    17
definition
wenzelm@21404
    18
  fsingleton    :: "'a => 'a fstream"  ("<_>" [1000] 999) where
wenzelm@19763
    19
  fsingleton_def2: "fsingleton = (%a. Def a && UU)"
wenzelm@19763
    20
wenzelm@21404
    21
definition
wenzelm@21404
    22
  fsfilter      :: "'a set \<Rightarrow> 'a fstream \<rightarrow> 'a fstream" where
wenzelm@19763
    23
  "fsfilter A = sfilter\<cdot>(flift2 (\<lambda>x. x\<in>A))"
wenzelm@19763
    24
wenzelm@21404
    25
definition
wenzelm@32960
    26
  fsmap         :: "('a => 'b) => 'a fstream -> 'b fstream" where
wenzelm@19763
    27
  "fsmap f = smap$(flift2 f)"
oheimb@15188
    28
wenzelm@21404
    29
definition
wenzelm@21404
    30
  jth           :: "nat => 'a fstream => 'a" where
haftmann@28524
    31
  "jth = (%n s. if Fin n < #s then THE a. i_th n s = Def a else undefined)"
oheimb@15188
    32
wenzelm@21404
    33
definition
wenzelm@21404
    34
  first         :: "'a fstream => 'a" where
wenzelm@19763
    35
  "first = (%s. jth 0 s)"
wenzelm@19763
    36
wenzelm@21404
    37
definition
wenzelm@21404
    38
  last          :: "'a fstream => 'a" where
haftmann@28524
    39
  "last = (%s. case #s of Fin n => (if n~=0 then jth (THE k. Suc k = n) s else undefined))"
oheimb@15188
    40
oheimb@15188
    41
wenzelm@19763
    42
abbreviation
wenzelm@21404
    43
  emptystream :: "'a fstream"  ("<>") where
wenzelm@19763
    44
  "<> == \<bottom>"
wenzelm@21404
    45
wenzelm@21404
    46
abbreviation
wenzelm@32960
    47
  fsfilter' :: "'a set \<Rightarrow> 'a fstream \<Rightarrow> 'a fstream"       ("(_'(C')_)" [64,63] 63) where
wenzelm@19763
    48
  "A(C)s == fsfilter A\<cdot>s"
oheimb@15188
    49
wenzelm@21210
    50
notation (xsymbols)
wenzelm@19763
    51
  fsfilter'  ("(_\<copyright>_)" [64,63] 63)
oheimb@15188
    52
oheimb@15188
    53
oheimb@15188
    54
lemma ft_fsingleton[simp]: "ft$(<a>) = Def a"
oheimb@15188
    55
by (simp add: fsingleton_def2)
oheimb@15188
    56
oheimb@15188
    57
lemma slen_fsingleton[simp]: "#(<a>) = Fin 1"
oheimb@15188
    58
by (simp add: fsingleton_def2 inat_defs)
oheimb@15188
    59
oheimb@15188
    60
lemma slen_fstreams[simp]: "#(<a> ooo s) = iSuc (#s)"
oheimb@15188
    61
by (simp add: fsingleton_def2)
oheimb@15188
    62
oheimb@15188
    63
lemma slen_fstreams2[simp]: "#(s ooo <a>) = iSuc (#s)"
haftmann@27111
    64
apply (cases "#s")
haftmann@27111
    65
apply (auto simp add: iSuc_Fin)
oheimb@15188
    66
apply (insert slen_sconc [of _ s "Suc 0" "<a>"], auto)
oheimb@15188
    67
by (simp add: sconc_def)
oheimb@15188
    68
oheimb@15188
    69
lemma j_th_0_fsingleton[simp]:"jth 0 (<a>) = a"
oheimb@15188
    70
apply (simp add: fsingleton_def2 jth_def)
oheimb@15188
    71
by (simp add: i_th_def Fin_0)
oheimb@15188
    72
oheimb@15188
    73
lemma jth_0[simp]: "jth 0 (<a> ooo s) = a"  
oheimb@15188
    74
apply (simp add: fsingleton_def2 jth_def)
oheimb@15188
    75
by (simp add: i_th_def Fin_0)
oheimb@15188
    76
oheimb@15188
    77
lemma first_sconc[simp]: "first (<a> ooo s) = a"
oheimb@15188
    78
by (simp add: first_def)
oheimb@15188
    79
oheimb@15188
    80
lemma first_fsingleton[simp]: "first (<a>) = a"
oheimb@15188
    81
by (simp add: first_def)
oheimb@15188
    82
oheimb@15188
    83
lemma jth_n[simp]: "Fin n = #s ==> jth n (s ooo <a>) = a"
oheimb@15188
    84
apply (simp add: jth_def, auto)
oheimb@15188
    85
apply (simp add: i_th_def rt_sconc1)
oheimb@15188
    86
by (simp add: inat_defs split: inat_splits)
oheimb@15188
    87
oheimb@15188
    88
lemma last_sconc[simp]: "Fin n = #s ==> last (s ooo <a>) = a"
oheimb@15188
    89
apply (simp add: last_def)
oheimb@15188
    90
apply (simp add: inat_defs split:inat_splits)
oheimb@15188
    91
by (drule sym, auto)
oheimb@15188
    92
oheimb@15188
    93
lemma last_fsingleton[simp]: "last (<a>) = a"
oheimb@15188
    94
by (simp add: last_def)
oheimb@15188
    95
haftmann@28524
    96
lemma first_UU[simp]: "first UU = undefined"
oheimb@15188
    97
by (simp add: first_def jth_def)
oheimb@15188
    98
haftmann@28524
    99
lemma last_UU[simp]:"last UU = undefined"
oheimb@15188
   100
by (simp add: last_def jth_def inat_defs)
oheimb@15188
   101
haftmann@28524
   102
lemma last_infinite[simp]:"#s = Infty ==> last s = undefined"
oheimb@15188
   103
by (simp add: last_def)
oheimb@15188
   104
haftmann@28524
   105
lemma jth_slen_lemma1:"n <= k & Fin n = #s ==> jth k s = undefined"
oheimb@15188
   106
by (simp add: jth_def inat_defs split:inat_splits, auto)
oheimb@15188
   107
haftmann@28524
   108
lemma jth_UU[simp]:"jth n UU = undefined" 
oheimb@15188
   109
by (simp add: jth_def)
oheimb@15188
   110
oheimb@15188
   111
lemma ext_last:"[|s ~= UU; Fin (Suc n) = #s|] ==> (stream_take n$s) ooo <(last s)> = s" 
oheimb@15188
   112
apply (simp add: last_def)
oheimb@15188
   113
apply (case_tac "#s", auto)
oheimb@15188
   114
apply (simp add: fsingleton_def2)
oheimb@15188
   115
apply (subgoal_tac "Def (jth n s) = i_th n s")
oheimb@15188
   116
apply (auto simp add: i_th_last)
oheimb@15188
   117
apply (drule slen_take_lemma1, auto)
oheimb@15188
   118
apply (simp add: jth_def)
oheimb@15188
   119
apply (case_tac "i_th n s = UU")
oheimb@15188
   120
apply auto
oheimb@15188
   121
apply (simp add: i_th_def)
oheimb@15188
   122
apply (case_tac "i_rt n s = UU", auto)
oheimb@15188
   123
apply (drule i_rt_slen [THEN iffD1])
oheimb@15188
   124
apply (drule slen_take_eq_rev [rule_format, THEN iffD2],auto)
oheimb@15188
   125
by (drule not_Undef_is_Def [THEN iffD1], auto)
oheimb@15188
   126
oheimb@15188
   127
oheimb@15188
   128
lemma fsingleton_lemma1[simp]: "(<a> = <b>) = (a=b)"
oheimb@15188
   129
by (simp add: fsingleton_def2)
oheimb@15188
   130
oheimb@15188
   131
lemma fsingleton_lemma2[simp]: "<a> ~= <>"
oheimb@15188
   132
by (simp add: fsingleton_def2)
oheimb@15188
   133
oheimb@15188
   134
lemma fsingleton_sconc:"<a> ooo s = Def a && s"
oheimb@15188
   135
by (simp add: fsingleton_def2)
oheimb@15188
   136
oheimb@15188
   137
lemma fstreams_ind: 
oheimb@15188
   138
  "[| adm P; P <>; !!a s. P s ==> P (<a> ooo s) |] ==> P x"
oheimb@15188
   139
apply (simp add: fsingleton_def2)
huffman@35781
   140
apply (rule stream.induct, auto)
oheimb@15188
   141
by (drule not_Undef_is_Def [THEN iffD1], auto)
oheimb@15188
   142
oheimb@15188
   143
lemma fstreams_ind2:
oheimb@15188
   144
  "[| adm P; P <>; !!a. P (<a>); !!a b s. P s ==> P (<a> ooo <b> ooo s) |] ==> P x"
oheimb@15188
   145
apply (simp add: fsingleton_def2)
oheimb@15188
   146
apply (rule stream_ind2, auto)
oheimb@15188
   147
by (drule not_Undef_is_Def [THEN iffD1], auto)+
oheimb@15188
   148
oheimb@15188
   149
lemma fstreams_take_Suc[simp]: "stream_take (Suc n)$(<a> ooo s) = <a> ooo stream_take n$s"
oheimb@15188
   150
by (simp add: fsingleton_def2)
oheimb@15188
   151
oheimb@15188
   152
lemma fstreams_not_empty[simp]: "<a> ooo s ~= <>"
oheimb@15188
   153
by (simp add: fsingleton_def2)
oheimb@15188
   154
oheimb@15188
   155
lemma fstreams_not_empty2[simp]: "s ooo <a> ~= <>"
oheimb@15188
   156
by (case_tac "s=UU", auto)
oheimb@15188
   157
oheimb@15188
   158
lemma fstreams_exhaust: "x = UU | (EX a s. x = <a> ooo s)"
oheimb@15188
   159
apply (simp add: fsingleton_def2, auto)
oheimb@15188
   160
apply (erule contrapos_pp, auto)
oheimb@15188
   161
apply (drule stream_exhaust_eq [THEN iffD1], auto)
oheimb@15188
   162
by (drule not_Undef_is_Def [THEN iffD1], auto)
oheimb@15188
   163
oheimb@15188
   164
lemma fstreams_cases: "[| x = UU ==> P; !!a y. x = <a> ooo y ==> P |] ==> P"
oheimb@15188
   165
by (insert fstreams_exhaust [of x], auto)
oheimb@15188
   166
oheimb@15188
   167
lemma fstreams_exhaust_eq: "(x ~= UU) = (? a y. x = <a> ooo y)"
oheimb@15188
   168
apply (simp add: fsingleton_def2, auto)
oheimb@15188
   169
apply (drule stream_exhaust_eq [THEN iffD1], auto)
oheimb@15188
   170
by (drule not_Undef_is_Def [THEN iffD1], auto)
oheimb@15188
   171
oheimb@15188
   172
lemma fstreams_inject: "(<a> ooo s = <b> ooo t) = (a=b & s=t)"
oheimb@15188
   173
by (simp add: fsingleton_def2)
oheimb@15188
   174
oheimb@15188
   175
lemma fstreams_prefix: "<a> ooo s << t ==> EX tt. t = <a> ooo tt &  s << tt"
oheimb@15188
   176
apply (simp add: fsingleton_def2)
oheimb@15188
   177
apply (insert stream_prefix [of "Def a" s t], auto)
huffman@30807
   178
done
oheimb@15188
   179
oheimb@15188
   180
lemma fstreams_prefix': "x << <a> ooo z = (x = <> |  (EX y. x = <a> ooo y &  y << z))"
oheimb@15188
   181
apply (auto, case_tac "x=UU", auto)
oheimb@15188
   182
apply (drule stream_exhaust_eq [THEN iffD1], auto)
oheimb@15188
   183
apply (simp add: fsingleton_def2, auto)
huffman@25920
   184
apply (drule ax_flat, simp)
oheimb@15188
   185
by (erule sconc_mono)
oheimb@15188
   186
oheimb@15188
   187
lemma ft_fstreams[simp]: "ft$(<a> ooo s) = Def a"
oheimb@15188
   188
by (simp add: fsingleton_def2)
oheimb@15188
   189
oheimb@15188
   190
lemma rt_fstreams[simp]: "rt$(<a> ooo s) = s"
oheimb@15188
   191
by (simp add: fsingleton_def2)
oheimb@15188
   192
oheimb@15188
   193
lemma ft_eq[simp]: "(ft$s = Def a) = (EX t. s = <a> ooo t)"
huffman@35781
   194
apply (cases s, auto)
huffman@16746
   195
by ((*drule sym,*) auto simp add: fsingleton_def2)
oheimb@15188
   196
oheimb@15188
   197
lemma surjective_fstreams: "(<d> ooo y = x) = (ft$x = Def d & rt$x = y)"
oheimb@15188
   198
by auto
oheimb@15188
   199
oheimb@15188
   200
lemma fstreams_mono: "<a> ooo b << <a> ooo c ==> b << c"
huffman@30807
   201
by (simp add: fsingleton_def2)
oheimb@15188
   202
oheimb@15188
   203
lemma fsmap_UU[simp]: "fsmap f$UU = UU"
oheimb@15188
   204
by (simp add: fsmap_def)
oheimb@15188
   205
oheimb@15188
   206
lemma fsmap_fsingleton_sconc: "fsmap f$(<x> ooo xs) = <(f x)> ooo (fsmap f$xs)"
oheimb@15188
   207
by (simp add: fsmap_def fsingleton_def2 flift2_def)
oheimb@15188
   208
oheimb@15188
   209
lemma fsmap_fsingleton[simp]: "fsmap f$(<x>) = <(f x)>"
oheimb@15188
   210
by (simp add: fsmap_def fsingleton_def2 flift2_def)
oheimb@15188
   211
oheimb@15188
   212
oheimb@15188
   213
lemma fstreams_chain_lemma[rule_format]:
oheimb@15188
   214
  "ALL s x y. stream_take n$(s::'a fstream) << x & x << y & y << s & x ~= y --> stream_take (Suc n)$s << y"
oheimb@15188
   215
apply (induct_tac n, auto)
oheimb@15188
   216
apply (case_tac "s=UU", auto)
oheimb@15188
   217
apply (drule stream_exhaust_eq [THEN iffD1], auto)
oheimb@15188
   218
apply (case_tac "y=UU", auto)
oheimb@15188
   219
apply (drule stream_exhaust_eq [THEN iffD1], auto)
huffman@40431
   220
apply (simp add: flat_below_iff)
oheimb@15188
   221
apply (case_tac "s=UU", auto)
oheimb@15188
   222
apply (drule stream_exhaust_eq [THEN iffD1], auto)
oheimb@15188
   223
apply (erule_tac x="ya" in allE)
oheimb@15188
   224
apply (drule stream_prefix, auto)
oheimb@15188
   225
apply (case_tac "y=UU",auto)
oheimb@15188
   226
apply (drule stream_exhaust_eq [THEN iffD1], clarsimp)
huffman@35215
   227
apply auto
huffman@40431
   228
apply (simp add: flat_below_iff)
oheimb@15188
   229
apply (erule_tac x="tt" in allE)
oheimb@15188
   230
apply (erule_tac x="yb" in allE, auto)
huffman@40431
   231
apply (simp add: flat_below_iff)
huffman@40431
   232
by (simp add: flat_below_iff)
oheimb@15188
   233
huffman@27413
   234
lemma fstreams_lub_lemma1: "[| chain Y; (LUB i. Y i) = <a> ooo s |] ==> EX j t. Y j = <a> ooo t"
huffman@27413
   235
apply (subgoal_tac "(LUB i. Y i) ~= UU")
oheimb@15188
   236
apply (drule chain_UU_I_inverse2, auto)
oheimb@15188
   237
apply (drule_tac x="i" in is_ub_thelub, auto)
oheimb@15188
   238
by (drule fstreams_prefix' [THEN iffD1], auto)
oheimb@15188
   239
oheimb@15188
   240
lemma fstreams_lub1: 
huffman@27413
   241
 "[| chain Y; (LUB i. Y i) = <a> ooo s |]
huffman@27413
   242
     ==> (EX j t. Y j = <a> ooo t) & (EX X. chain X & (ALL i. EX j. <a> ooo X i << Y j) & (LUB i. X i) = s)"
oheimb@15188
   243
apply (auto simp add: fstreams_lub_lemma1)
oheimb@15188
   244
apply (rule_tac x="%n. stream_take n$s" in exI, auto)
oheimb@15188
   245
apply (induct_tac i, auto)
oheimb@15188
   246
apply (drule fstreams_lub_lemma1, auto)
oheimb@15188
   247
apply (rule_tac x="j" in exI, auto)
oheimb@15188
   248
apply (case_tac "max_in_chain j Y")
huffman@40771
   249
apply (frule lub_finch1 [THEN lub_eqI], auto)
oheimb@15188
   250
apply (rule_tac x="j" in exI)
oheimb@15188
   251
apply (erule subst) back back
huffman@40431
   252
apply (simp add: below_prod_def sconc_mono)
oheimb@15188
   253
apply (simp add: max_in_chain_def, auto)
oheimb@15188
   254
apply (rule_tac x="ja" in exI)
oheimb@15188
   255
apply (subgoal_tac "Y j << Y ja")
oheimb@15188
   256
apply (drule fstreams_prefix, auto)+
oheimb@15188
   257
apply (rule sconc_mono)
oheimb@15188
   258
apply (rule fstreams_chain_lemma, auto)
oheimb@15188
   259
apply (subgoal_tac "Y ja << (LUB i. (Y i))", clarsimp)
oheimb@15188
   260
apply (drule fstreams_mono, simp)
oheimb@15188
   261
apply (rule is_ub_thelub, simp)
huffman@25922
   262
apply (blast intro: chain_mono)
oheimb@15188
   263
by (rule stream_reach2)
oheimb@15188
   264
oheimb@15188
   265
oheimb@15188
   266
lemma lub_Pair_not_UU_lemma: 
huffman@27413
   267
  "[| chain Y; (LUB i. Y i) = ((a::'a::flat), b); a ~= UU; b ~= UU |] 
oheimb@15188
   268
      ==> EX j c d. Y j = (c, d) & c ~= UU & d ~= UU"
huffman@40771
   269
apply (frule lub_prod, clarsimp)
oheimb@15188
   270
apply (drule chain_UU_I_inverse2, clarsimp)
oheimb@15188
   271
apply (case_tac "Y i", clarsimp)
oheimb@15188
   272
apply (case_tac "max_in_chain i Y")
oheimb@15188
   273
apply (drule maxinch_is_thelub, auto)
oheimb@15188
   274
apply (rule_tac x="i" in exI, auto)
oheimb@15188
   275
apply (simp add: max_in_chain_def, auto)
oheimb@15188
   276
apply (subgoal_tac "Y i << Y j",auto)
huffman@40431
   277
apply (simp add: below_prod_def, clarsimp)
huffman@25920
   278
apply (drule ax_flat, auto)
oheimb@15188
   279
apply (case_tac "snd (Y j) = UU",auto)
oheimb@15188
   280
apply (case_tac "Y j", auto)
oheimb@15188
   281
apply (rule_tac x="j" in exI)
oheimb@15188
   282
apply (case_tac "Y j",auto)
huffman@25922
   283
by (drule chain_mono, auto)
oheimb@15188
   284
oheimb@15188
   285
lemma fstreams_lub_lemma2: 
huffman@27413
   286
  "[| chain Y; (LUB i. Y i) = (a, <m> ooo ms); (a::'a::flat) ~= UU |] ==> EX j t. Y j = (a, <m> ooo t)"
oheimb@15188
   287
apply (frule lub_Pair_not_UU_lemma, auto)
oheimb@15188
   288
apply (drule_tac x="j" in is_ub_thelub, auto)
huffman@25920
   289
apply (drule ax_flat, clarsimp)
oheimb@15188
   290
by (drule fstreams_prefix' [THEN iffD1], auto)
oheimb@15188
   291
oheimb@15188
   292
lemma fstreams_lub2:
huffman@27413
   293
  "[| chain Y; (LUB i. Y i) = (a, <m> ooo ms); (a::'a::flat) ~= UU |] 
huffman@27413
   294
      ==> (EX j t. Y j = (a, <m> ooo t)) & (EX X. chain X & (ALL i. EX j. (a, <m> ooo X i) << Y j) & (LUB i. X i) = ms)"
oheimb@15188
   295
apply (auto simp add: fstreams_lub_lemma2)
oheimb@15188
   296
apply (rule_tac x="%n. stream_take n$ms" in exI, auto)
oheimb@15188
   297
apply (induct_tac i, auto)
oheimb@15188
   298
apply (drule fstreams_lub_lemma2, auto)
oheimb@15188
   299
apply (rule_tac x="j" in exI, auto)
oheimb@15188
   300
apply (case_tac "max_in_chain j Y")
huffman@40771
   301
apply (frule lub_finch1 [THEN lub_eqI], auto)
oheimb@15188
   302
apply (rule_tac x="j" in exI)
oheimb@15188
   303
apply (erule subst) back back
huffman@26029
   304
apply (simp add: sconc_mono)
oheimb@15188
   305
apply (simp add: max_in_chain_def, auto)
oheimb@15188
   306
apply (rule_tac x="ja" in exI)
oheimb@15188
   307
apply (subgoal_tac "Y j << Y ja")
huffman@40431
   308
apply (simp add: below_prod_def, auto)
huffman@40431
   309
apply (drule below_trans)
oheimb@15188
   310
apply (simp add: ax_flat, auto)
oheimb@15188
   311
apply (drule fstreams_prefix, auto)+
oheimb@15188
   312
apply (rule sconc_mono)
oheimb@15188
   313
apply (subgoal_tac "tt ~= tta" "tta << ms")
oheimb@15188
   314
apply (blast intro: fstreams_chain_lemma)
huffman@40771
   315
apply (frule lub_prod, auto)
oheimb@15188
   316
apply (subgoal_tac "snd (Y ja) << (LUB i. snd (Y i))", clarsimp)
oheimb@15188
   317
apply (drule fstreams_mono, simp)
oheimb@15188
   318
apply (rule is_ub_thelub chainI)
huffman@40431
   319
apply (simp add: chain_def below_prod_def)
oheimb@15188
   320
apply (subgoal_tac "fst (Y j) ~= fst (Y ja) | snd (Y j) ~= snd (Y ja)", simp)
huffman@25920
   321
apply (drule ax_flat, simp)+
oheimb@15188
   322
apply (drule prod_eqI, auto)
huffman@25922
   323
apply (simp add: chain_mono)
oheimb@15188
   324
by (rule stream_reach2)
oheimb@15188
   325
oheimb@15188
   326
oheimb@15188
   327
lemma cpo_cont_lemma:
oheimb@15188
   328
  "[| monofun (f::'a::cpo => 'b::cpo); (!Y. chain Y --> f (lub(range Y)) << (LUB i. f (Y i))) |] ==> cont f"
huffman@35914
   329
by (erule contI2, simp)
oheimb@15188
   330
oheimb@15188
   331
end