src/HOL/Library/Linear_Temporal_Logic_on_Streams.thy
author wenzelm
Tue May 15 13:57:39 2018 +0200 (16 months ago)
changeset 68189 6163c90694ef
parent 67408 4a4c14b24800
child 68406 6beb45f6cf67
permissions -rw-r--r--
tuned headers;
     1 (*  Title:      HOL/Library/Linear_Temporal_Logic_on_Streams.thy
     2     Author:     Andrei Popescu, TU Muenchen
     3     Author:     Dmitriy Traytel, TU Muenchen
     4 *)
     5 
     6 section \<open>Linear Temporal Logic on Streams\<close>
     7 
     8 theory Linear_Temporal_Logic_on_Streams
     9   imports Stream Sublist Extended_Nat Infinite_Set
    10 begin
    11 
    12 section\<open>Preliminaries\<close>
    13 
    14 lemma shift_prefix:
    15 assumes "xl @- xs = yl @- ys" and "length xl \<le> length yl"
    16 shows "prefix xl yl"
    17 using assms proof(induct xl arbitrary: yl xs ys)
    18   case (Cons x xl yl xs ys)
    19   thus ?case by (cases yl) auto
    20 qed auto
    21 
    22 lemma shift_prefix_cases:
    23 assumes "xl @- xs = yl @- ys"
    24 shows "prefix xl yl \<or> prefix yl xl"
    25 using shift_prefix[OF assms]
    26 by (cases "length xl \<le> length yl") (metis, metis assms nat_le_linear shift_prefix)
    27 
    28 
    29 section\<open>Linear temporal logic\<close>
    30 
    31 text \<open>Propositional connectives:\<close>
    32 
    33 abbreviation (input) IMPL (infix "impl" 60)
    34 where "\<phi> impl \<psi> \<equiv> \<lambda> xs. \<phi> xs \<longrightarrow> \<psi> xs"
    35 
    36 abbreviation (input) OR (infix "or" 60)
    37 where "\<phi> or \<psi> \<equiv> \<lambda> xs. \<phi> xs \<or> \<psi> xs"
    38 
    39 abbreviation (input) AND (infix "aand" 60)
    40 where "\<phi> aand \<psi> \<equiv> \<lambda> xs. \<phi> xs \<and> \<psi> xs"
    41 
    42 abbreviation (input) "not \<phi> \<equiv> \<lambda> xs. \<not> \<phi> xs"
    43 
    44 abbreviation (input) "true \<equiv> \<lambda> xs. True"
    45 
    46 abbreviation (input) "false \<equiv> \<lambda> xs. False"
    47 
    48 lemma impl_not_or: "\<phi> impl \<psi> = (not \<phi>) or \<psi>"
    49 by blast
    50 
    51 lemma not_or: "not (\<phi> or \<psi>) = (not \<phi>) aand (not \<psi>)"
    52 by blast
    53 
    54 lemma not_aand: "not (\<phi> aand \<psi>) = (not \<phi>) or (not \<psi>)"
    55 by blast
    56 
    57 lemma non_not[simp]: "not (not \<phi>) = \<phi>" by simp
    58 
    59 text \<open>Temporal (LTL) connectives:\<close>
    60 
    61 fun holds where "holds P xs \<longleftrightarrow> P (shd xs)"
    62 fun nxt where "nxt \<phi> xs = \<phi> (stl xs)"
    63 
    64 definition "HLD s = holds (\<lambda>x. x \<in> s)"
    65 
    66 abbreviation HLD_nxt (infixr "\<cdot>" 65) where
    67   "s \<cdot> P \<equiv> HLD s aand nxt P"
    68 
    69 context
    70   notes [[inductive_internals]]
    71 begin
    72 
    73 inductive ev for \<phi> where
    74 base: "\<phi> xs \<Longrightarrow> ev \<phi> xs"
    75 |
    76 step: "ev \<phi> (stl xs) \<Longrightarrow> ev \<phi> xs"
    77 
    78 coinductive alw for \<phi> where
    79 alw: "\<lbrakk>\<phi> xs; alw \<phi> (stl xs)\<rbrakk> \<Longrightarrow> alw \<phi> xs"
    80 
    81 \<comment> \<open>weak until:\<close>
    82 coinductive UNTIL (infix "until" 60) for \<phi> \<psi> where
    83 base: "\<psi> xs \<Longrightarrow> (\<phi> until \<psi>) xs"
    84 |
    85 step: "\<lbrakk>\<phi> xs; (\<phi> until \<psi>) (stl xs)\<rbrakk> \<Longrightarrow> (\<phi> until \<psi>) xs"
    86 
    87 end
    88 
    89 lemma holds_mono:
    90 assumes holds: "holds P xs" and 0: "\<And> x. P x \<Longrightarrow> Q x"
    91 shows "holds Q xs"
    92 using assms by auto
    93 
    94 lemma holds_aand:
    95 "(holds P aand holds Q) steps \<longleftrightarrow> holds (\<lambda> step. P step \<and> Q step) steps" by auto
    96 
    97 lemma HLD_iff: "HLD s \<omega> \<longleftrightarrow> shd \<omega> \<in> s"
    98   by (simp add: HLD_def)
    99 
   100 lemma HLD_Stream[simp]: "HLD X (x ## \<omega>) \<longleftrightarrow> x \<in> X"
   101   by (simp add: HLD_iff)
   102 
   103 lemma nxt_mono:
   104 assumes nxt: "nxt \<phi> xs" and 0: "\<And> xs. \<phi> xs \<Longrightarrow> \<psi> xs"
   105 shows "nxt \<psi> xs"
   106 using assms by auto
   107 
   108 declare ev.intros[intro]
   109 declare alw.cases[elim]
   110 
   111 lemma ev_induct_strong[consumes 1, case_names base step]:
   112   "ev \<phi> x \<Longrightarrow> (\<And>xs. \<phi> xs \<Longrightarrow> P xs) \<Longrightarrow> (\<And>xs. ev \<phi> (stl xs) \<Longrightarrow> \<not> \<phi> xs \<Longrightarrow> P (stl xs) \<Longrightarrow> P xs) \<Longrightarrow> P x"
   113   by (induct rule: ev.induct) auto
   114 
   115 lemma alw_coinduct[consumes 1, case_names alw stl]:
   116   "X x \<Longrightarrow> (\<And>x. X x \<Longrightarrow> \<phi> x) \<Longrightarrow> (\<And>x. X x \<Longrightarrow> \<not> alw \<phi> (stl x) \<Longrightarrow> X (stl x)) \<Longrightarrow> alw \<phi> x"
   117   using alw.coinduct[of X x \<phi>] by auto
   118 
   119 lemma ev_mono:
   120 assumes ev: "ev \<phi> xs" and 0: "\<And> xs. \<phi> xs \<Longrightarrow> \<psi> xs"
   121 shows "ev \<psi> xs"
   122 using ev by induct (auto simp: 0)
   123 
   124 lemma alw_mono:
   125 assumes alw: "alw \<phi> xs" and 0: "\<And> xs. \<phi> xs \<Longrightarrow> \<psi> xs"
   126 shows "alw \<psi> xs"
   127 using alw by coinduct (auto simp: 0)
   128 
   129 lemma until_monoL:
   130 assumes until: "(\<phi>1 until \<psi>) xs" and 0: "\<And> xs. \<phi>1 xs \<Longrightarrow> \<phi>2 xs"
   131 shows "(\<phi>2 until \<psi>) xs"
   132 using until by coinduct (auto elim: UNTIL.cases simp: 0)
   133 
   134 lemma until_monoR:
   135 assumes until: "(\<phi> until \<psi>1) xs" and 0: "\<And> xs. \<psi>1 xs \<Longrightarrow> \<psi>2 xs"
   136 shows "(\<phi> until \<psi>2) xs"
   137 using until by coinduct (auto elim: UNTIL.cases simp: 0)
   138 
   139 lemma until_mono:
   140 assumes until: "(\<phi>1 until \<psi>1) xs" and
   141 0: "\<And> xs. \<phi>1 xs \<Longrightarrow> \<phi>2 xs" "\<And> xs. \<psi>1 xs \<Longrightarrow> \<psi>2 xs"
   142 shows "(\<phi>2 until \<psi>2) xs"
   143 using until by coinduct (auto elim: UNTIL.cases simp: 0)
   144 
   145 lemma until_false: "\<phi> until false = alw \<phi>"
   146 proof-
   147   {fix xs assume "(\<phi> until false) xs" hence "alw \<phi> xs"
   148    by coinduct (auto elim: UNTIL.cases)
   149   }
   150   moreover
   151   {fix xs assume "alw \<phi> xs" hence "(\<phi> until false) xs"
   152    by coinduct auto
   153   }
   154   ultimately show ?thesis by blast
   155 qed
   156 
   157 lemma ev_nxt: "ev \<phi> = (\<phi> or nxt (ev \<phi>))"
   158 by (rule ext) (metis ev.simps nxt.simps)
   159 
   160 lemma alw_nxt: "alw \<phi> = (\<phi> aand nxt (alw \<phi>))"
   161 by (rule ext) (metis alw.simps nxt.simps)
   162 
   163 lemma ev_ev[simp]: "ev (ev \<phi>) = ev \<phi>"
   164 proof-
   165   {fix xs
   166    assume "ev (ev \<phi>) xs" hence "ev \<phi> xs"
   167    by induct auto
   168   }
   169   thus ?thesis by auto
   170 qed
   171 
   172 lemma alw_alw[simp]: "alw (alw \<phi>) = alw \<phi>"
   173 proof-
   174   {fix xs
   175    assume "alw \<phi> xs" hence "alw (alw \<phi>) xs"
   176    by coinduct auto
   177   }
   178   thus ?thesis by auto
   179 qed
   180 
   181 lemma ev_shift:
   182 assumes "ev \<phi> xs"
   183 shows "ev \<phi> (xl @- xs)"
   184 using assms by (induct xl) auto
   185 
   186 lemma ev_imp_shift:
   187 assumes "ev \<phi> xs"  shows "\<exists> xl xs2. xs = xl @- xs2 \<and> \<phi> xs2"
   188 using assms by induct (metis shift.simps(1), metis shift.simps(2) stream.collapse)+
   189 
   190 lemma alw_ev_shift: "alw \<phi> xs1 \<Longrightarrow> ev (alw \<phi>) (xl @- xs1)"
   191 by (auto intro: ev_shift)
   192 
   193 lemma alw_shift:
   194 assumes "alw \<phi> (xl @- xs)"
   195 shows "alw \<phi> xs"
   196 using assms by (induct xl) auto
   197 
   198 lemma ev_ex_nxt:
   199 assumes "ev \<phi> xs"
   200 shows "\<exists> n. (nxt ^^ n) \<phi> xs"
   201 using assms proof induct
   202   case (base xs) thus ?case by (intro exI[of _ 0]) auto
   203 next
   204   case (step xs)
   205   then obtain n where "(nxt ^^ n) \<phi> (stl xs)" by blast
   206   thus ?case by (intro exI[of _ "Suc n"]) (metis funpow.simps(2) nxt.simps o_def)
   207 qed
   208 
   209 lemma alw_sdrop:
   210 assumes "alw \<phi> xs"  shows "alw \<phi> (sdrop n xs)"
   211 by (metis alw_shift assms stake_sdrop)
   212 
   213 lemma nxt_sdrop: "(nxt ^^ n) \<phi> xs \<longleftrightarrow> \<phi> (sdrop n xs)"
   214 by (induct n arbitrary: xs) auto
   215 
   216 definition "wait \<phi> xs \<equiv> LEAST n. (nxt ^^ n) \<phi> xs"
   217 
   218 lemma nxt_wait:
   219 assumes "ev \<phi> xs"  shows "(nxt ^^ (wait \<phi> xs)) \<phi> xs"
   220 unfolding wait_def using ev_ex_nxt[OF assms] by(rule LeastI_ex)
   221 
   222 lemma nxt_wait_least:
   223 assumes ev: "ev \<phi> xs" and nxt: "(nxt ^^ n) \<phi> xs"  shows "wait \<phi> xs \<le> n"
   224 unfolding wait_def using ev_ex_nxt[OF ev] by (metis Least_le nxt)
   225 
   226 lemma sdrop_wait:
   227 assumes "ev \<phi> xs"  shows "\<phi> (sdrop (wait \<phi> xs) xs)"
   228 using nxt_wait[OF assms] unfolding nxt_sdrop .
   229 
   230 lemma sdrop_wait_least:
   231 assumes ev: "ev \<phi> xs" and nxt: "\<phi> (sdrop n xs)"  shows "wait \<phi> xs \<le> n"
   232 using assms nxt_wait_least unfolding nxt_sdrop by auto
   233 
   234 lemma nxt_ev: "(nxt ^^ n) \<phi> xs \<Longrightarrow> ev \<phi> xs"
   235 by (induct n arbitrary: xs) auto
   236 
   237 lemma not_ev: "not (ev \<phi>) = alw (not \<phi>)"
   238 proof(rule ext, safe)
   239   fix xs assume "not (ev \<phi>) xs" thus "alw (not \<phi>) xs"
   240   by (coinduct) auto
   241 next
   242   fix xs assume "ev \<phi> xs" and "alw (not \<phi>) xs" thus False
   243   by (induct) auto
   244 qed
   245 
   246 lemma not_alw: "not (alw \<phi>) = ev (not \<phi>)"
   247 proof-
   248   have "not (alw \<phi>) = not (alw (not (not \<phi>)))" by simp
   249   also have "... = ev (not \<phi>)" unfolding not_ev[symmetric] by simp
   250   finally show ?thesis .
   251 qed
   252 
   253 lemma not_ev_not[simp]: "not (ev (not \<phi>)) = alw \<phi>"
   254 unfolding not_ev by simp
   255 
   256 lemma not_alw_not[simp]: "not (alw (not \<phi>)) = ev \<phi>"
   257 unfolding not_alw by simp
   258 
   259 lemma alw_ev_sdrop:
   260 assumes "alw (ev \<phi>) (sdrop m xs)"
   261 shows "alw (ev \<phi>) xs"
   262 using assms
   263 by coinduct (metis alw_nxt ev_shift funpow_swap1 nxt.simps nxt_sdrop stake_sdrop)
   264 
   265 lemma ev_alw_imp_alw_ev:
   266 assumes "ev (alw \<phi>) xs"  shows "alw (ev \<phi>) xs"
   267 using assms by induct (metis (full_types) alw_mono ev.base, metis alw alw_nxt ev.step)
   268 
   269 lemma alw_aand: "alw (\<phi> aand \<psi>) = alw \<phi> aand alw \<psi>"
   270 proof-
   271   {fix xs assume "alw (\<phi> aand \<psi>) xs" hence "(alw \<phi> aand alw \<psi>) xs"
   272    by (auto elim: alw_mono)
   273   }
   274   moreover
   275   {fix xs assume "(alw \<phi> aand alw \<psi>) xs" hence "alw (\<phi> aand \<psi>) xs"
   276    by coinduct auto
   277   }
   278   ultimately show ?thesis by blast
   279 qed
   280 
   281 lemma ev_or: "ev (\<phi> or \<psi>) = ev \<phi> or ev \<psi>"
   282 proof-
   283   {fix xs assume "(ev \<phi> or ev \<psi>) xs" hence "ev (\<phi> or \<psi>) xs"
   284    by (auto elim: ev_mono)
   285   }
   286   moreover
   287   {fix xs assume "ev (\<phi> or \<psi>) xs" hence "(ev \<phi> or ev \<psi>) xs"
   288    by induct auto
   289   }
   290   ultimately show ?thesis by blast
   291 qed
   292 
   293 lemma ev_alw_aand:
   294 assumes \<phi>: "ev (alw \<phi>) xs" and \<psi>: "ev (alw \<psi>) xs"
   295 shows "ev (alw (\<phi> aand \<psi>)) xs"
   296 proof-
   297   obtain xl xs1 where xs1: "xs = xl @- xs1" and \<phi>\<phi>: "alw \<phi> xs1"
   298   using \<phi> by (metis ev_imp_shift)
   299   moreover obtain yl ys1 where xs2: "xs = yl @- ys1" and \<psi>\<psi>: "alw \<psi> ys1"
   300   using \<psi> by (metis ev_imp_shift)
   301   ultimately have 0: "xl @- xs1 = yl @- ys1" by auto
   302   hence "prefix xl yl \<or> prefix yl xl" using shift_prefix_cases by auto
   303   thus ?thesis proof
   304     assume "prefix xl yl"
   305     then obtain yl1 where yl: "yl = xl @ yl1" by (elim prefixE)
   306     have xs1': "xs1 = yl1 @- ys1" using 0 unfolding yl by simp
   307     have "alw \<phi> ys1" using \<phi>\<phi> unfolding xs1' by (metis alw_shift)
   308     hence "alw (\<phi> aand \<psi>) ys1" using \<psi>\<psi> unfolding alw_aand by auto
   309     thus ?thesis unfolding xs2 by (auto intro: alw_ev_shift)
   310   next
   311     assume "prefix yl xl"
   312     then obtain xl1 where xl: "xl = yl @ xl1" by (elim prefixE)
   313     have ys1': "ys1 = xl1 @- xs1" using 0 unfolding xl by simp
   314     have "alw \<psi> xs1" using \<psi>\<psi> unfolding ys1' by (metis alw_shift)
   315     hence "alw (\<phi> aand \<psi>) xs1" using \<phi>\<phi> unfolding alw_aand by auto
   316     thus ?thesis unfolding xs1 by (auto intro: alw_ev_shift)
   317   qed
   318 qed
   319 
   320 lemma ev_alw_alw_impl:
   321 assumes "ev (alw \<phi>) xs" and "alw (alw \<phi> impl ev \<psi>) xs"
   322 shows "ev \<psi> xs"
   323 using assms by induct auto
   324 
   325 lemma ev_alw_stl[simp]: "ev (alw \<phi>) (stl x) \<longleftrightarrow> ev (alw \<phi>) x"
   326 by (metis (full_types) alw_nxt ev_nxt nxt.simps)
   327 
   328 lemma alw_alw_impl_ev:
   329 "alw (alw \<phi> impl ev \<psi>) = (ev (alw \<phi>) impl alw (ev \<psi>))" (is "?A = ?B")
   330 proof-
   331   {fix xs assume "?A xs \<and> ev (alw \<phi>) xs" hence "alw (ev \<psi>) xs"
   332     by coinduct (auto elim: ev_alw_alw_impl)
   333   }
   334   moreover
   335   {fix xs assume "?B xs" hence "?A xs"
   336    by coinduct auto
   337   }
   338   ultimately show ?thesis by blast
   339 qed
   340 
   341 lemma ev_alw_impl:
   342 assumes "ev \<phi> xs" and "alw (\<phi> impl \<psi>) xs"  shows "ev \<psi> xs"
   343 using assms by induct auto
   344 
   345 lemma ev_alw_impl_ev:
   346 assumes "ev \<phi> xs" and "alw (\<phi> impl ev \<psi>) xs"  shows "ev \<psi> xs"
   347 using ev_alw_impl[OF assms] by simp
   348 
   349 lemma alw_mp:
   350 assumes "alw \<phi> xs" and "alw (\<phi> impl \<psi>) xs"
   351 shows "alw \<psi> xs"
   352 proof-
   353   {assume "alw \<phi> xs \<and> alw (\<phi> impl \<psi>) xs" hence ?thesis
   354    by coinduct auto
   355   }
   356   thus ?thesis using assms by auto
   357 qed
   358 
   359 lemma all_imp_alw:
   360 assumes "\<And> xs. \<phi> xs"  shows "alw \<phi> xs"
   361 proof-
   362   {assume "\<forall> xs. \<phi> xs"
   363    hence ?thesis by coinduct auto
   364   }
   365   thus ?thesis using assms by auto
   366 qed
   367 
   368 lemma alw_impl_ev_alw:
   369 assumes "alw (\<phi> impl ev \<psi>) xs"
   370 shows "alw (ev \<phi> impl ev \<psi>) xs"
   371 using assms by coinduct (auto dest: ev_alw_impl)
   372 
   373 lemma ev_holds_sset:
   374 "ev (holds P) xs \<longleftrightarrow> (\<exists> x \<in> sset xs. P x)" (is "?L \<longleftrightarrow> ?R")
   375 proof safe
   376   assume ?L thus ?R by induct (metis holds.simps stream.set_sel(1), metis stl_sset)
   377 next
   378   fix x assume "x \<in> sset xs" "P x"
   379   thus ?L by (induct rule: sset_induct) (simp_all add: ev.base ev.step)
   380 qed
   381 
   382 text \<open>LTL as a program logic:\<close>
   383 lemma alw_invar:
   384 assumes "\<phi> xs" and "alw (\<phi> impl nxt \<phi>) xs"
   385 shows "alw \<phi> xs"
   386 proof-
   387   {assume "\<phi> xs \<and> alw (\<phi> impl nxt \<phi>) xs" hence ?thesis
   388    by coinduct auto
   389   }
   390   thus ?thesis using assms by auto
   391 qed
   392 
   393 lemma variance:
   394 assumes 1: "\<phi> xs" and 2: "alw (\<phi> impl (\<psi> or nxt \<phi>)) xs"
   395 shows "(alw \<phi> or ev \<psi>) xs"
   396 proof-
   397   {assume "\<not> ev \<psi> xs" hence "alw (not \<psi>) xs" unfolding not_ev[symmetric] .
   398    moreover have "alw (not \<psi> impl (\<phi> impl nxt \<phi>)) xs"
   399    using 2 by coinduct auto
   400    ultimately have "alw (\<phi> impl nxt \<phi>) xs" by(auto dest: alw_mp)
   401    with 1 have "alw \<phi> xs" by(rule alw_invar)
   402   }
   403   thus ?thesis by blast
   404 qed
   405 
   406 lemma ev_alw_imp_nxt:
   407 assumes e: "ev \<phi> xs" and a: "alw (\<phi> impl (nxt \<phi>)) xs"
   408 shows "ev (alw \<phi>) xs"
   409 proof-
   410   obtain xl xs1 where xs: "xs = xl @- xs1" and \<phi>: "\<phi> xs1"
   411   using e by (metis ev_imp_shift)
   412   have "\<phi> xs1 \<and> alw (\<phi> impl (nxt \<phi>)) xs1" using a \<phi> unfolding xs by (metis alw_shift)
   413   hence "alw \<phi> xs1" by(coinduct xs1 rule: alw.coinduct) auto
   414   thus ?thesis unfolding xs by (auto intro: alw_ev_shift)
   415 qed
   416 
   417 
   418 inductive ev_at :: "('a stream \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> 'a stream \<Rightarrow> bool" for P :: "'a stream \<Rightarrow> bool" where
   419   base: "P \<omega> \<Longrightarrow> ev_at P 0 \<omega>"
   420 | step:"\<not> P \<omega> \<Longrightarrow> ev_at P n (stl \<omega>) \<Longrightarrow> ev_at P (Suc n) \<omega>"
   421 
   422 inductive_simps ev_at_0[simp]: "ev_at P 0 \<omega>"
   423 inductive_simps ev_at_Suc[simp]: "ev_at P (Suc n) \<omega>"
   424 
   425 lemma ev_at_imp_snth: "ev_at P n \<omega> \<Longrightarrow> P (sdrop n \<omega>)"
   426   by (induction n arbitrary: \<omega>) auto
   427 
   428 lemma ev_at_HLD_imp_snth: "ev_at (HLD X) n \<omega> \<Longrightarrow> \<omega> !! n \<in> X"
   429   by (auto dest!: ev_at_imp_snth simp: HLD_iff)
   430 
   431 lemma ev_at_HLD_single_imp_snth: "ev_at (HLD {x}) n \<omega> \<Longrightarrow> \<omega> !! n = x"
   432   by (drule ev_at_HLD_imp_snth) simp
   433 
   434 lemma ev_at_unique: "ev_at P n \<omega> \<Longrightarrow> ev_at P m \<omega> \<Longrightarrow> n = m"
   435 proof (induction arbitrary: m rule: ev_at.induct)
   436   case (base \<omega>) then show ?case
   437     by (simp add: ev_at.simps[of _ _ \<omega>])
   438 next
   439   case (step \<omega> n) from step.prems step.hyps step.IH[of "m - 1"] show ?case
   440     by (auto simp add: ev_at.simps[of _ _ \<omega>])
   441 qed
   442 
   443 lemma ev_iff_ev_at: "ev P \<omega> \<longleftrightarrow> (\<exists>n. ev_at P n \<omega>)"
   444 proof
   445   assume "ev P \<omega>" then show "\<exists>n. ev_at P n \<omega>"
   446     by (induction rule: ev_induct_strong) (auto intro: ev_at.intros)
   447 next
   448   assume "\<exists>n. ev_at P n \<omega>"
   449   then obtain n where "ev_at P n \<omega>"
   450     by auto
   451   then show "ev P \<omega>"
   452     by induction auto
   453 qed
   454 
   455 lemma ev_at_shift: "ev_at (HLD X) i (stake (Suc i) \<omega> @- \<omega>' :: 's stream) \<longleftrightarrow> ev_at (HLD X) i \<omega>"
   456   by (induction i arbitrary: \<omega>) (auto simp: HLD_iff)
   457 
   458 lemma ev_iff_ev_at_unqiue: "ev P \<omega> \<longleftrightarrow> (\<exists>!n. ev_at P n \<omega>)"
   459   by (auto intro: ev_at_unique simp: ev_iff_ev_at)
   460 
   461 lemma alw_HLD_iff_streams: "alw (HLD X) \<omega> \<longleftrightarrow> \<omega> \<in> streams X"
   462 proof
   463   assume "alw (HLD X) \<omega>" then show "\<omega> \<in> streams X"
   464   proof (coinduction arbitrary: \<omega>)
   465     case (streams \<omega>) then show ?case by (cases \<omega>) auto
   466   qed
   467 next
   468   assume "\<omega> \<in> streams X" then show "alw (HLD X) \<omega>"
   469   proof (coinduction arbitrary: \<omega>)
   470     case (alw \<omega>) then show ?case by (cases \<omega>) auto
   471   qed
   472 qed
   473 
   474 lemma not_HLD: "not (HLD X) = HLD (- X)"
   475   by (auto simp: HLD_iff)
   476 
   477 lemma not_alw_iff: "\<not> (alw P \<omega>) \<longleftrightarrow> ev (not P) \<omega>"
   478   using not_alw[of P] by (simp add: fun_eq_iff)
   479 
   480 lemma not_ev_iff: "\<not> (ev P \<omega>) \<longleftrightarrow> alw (not P) \<omega>"
   481   using not_alw_iff[of "not P" \<omega>, symmetric]  by simp
   482 
   483 lemma ev_Stream: "ev P (x ## s) \<longleftrightarrow> P (x ## s) \<or> ev P s"
   484   by (auto elim: ev.cases)
   485 
   486 lemma alw_ev_imp_ev_alw:
   487   assumes "alw (ev P) \<omega>" shows "ev (P aand alw (ev P)) \<omega>"
   488 proof -
   489   have "ev P \<omega>" using assms by auto
   490   from this assms show ?thesis
   491     by induct auto
   492 qed
   493 
   494 lemma ev_False: "ev (\<lambda>x. False) \<omega> \<longleftrightarrow> False"
   495 proof
   496   assume "ev (\<lambda>x. False) \<omega>" then show False
   497     by induct auto
   498 qed auto
   499 
   500 lemma alw_False: "alw (\<lambda>x. False) \<omega> \<longleftrightarrow> False"
   501   by auto
   502 
   503 lemma ev_iff_sdrop: "ev P \<omega> \<longleftrightarrow> (\<exists>m. P (sdrop m \<omega>))"
   504 proof safe
   505   assume "ev P \<omega>" then show "\<exists>m. P (sdrop m \<omega>)"
   506     by (induct rule: ev_induct_strong) (auto intro: exI[of _ 0] exI[of _ "Suc n" for n])
   507 next
   508   fix m assume "P (sdrop m \<omega>)" then show "ev P \<omega>"
   509     by (induct m arbitrary: \<omega>) auto
   510 qed
   511 
   512 lemma alw_iff_sdrop: "alw P \<omega> \<longleftrightarrow> (\<forall>m. P (sdrop m \<omega>))"
   513 proof safe
   514   fix m assume "alw P \<omega>" then show "P (sdrop m \<omega>)"
   515     by (induct m arbitrary: \<omega>) auto
   516 next
   517   assume "\<forall>m. P (sdrop m \<omega>)" then show "alw P \<omega>"
   518     by (coinduction arbitrary: \<omega>) (auto elim: allE[of _ 0] allE[of _ "Suc n" for n])
   519 qed
   520 
   521 lemma infinite_iff_alw_ev: "infinite {m. P (sdrop m \<omega>)} \<longleftrightarrow> alw (ev P) \<omega>"
   522   unfolding infinite_nat_iff_unbounded_le alw_iff_sdrop ev_iff_sdrop
   523   by simp (metis le_Suc_ex le_add1)
   524 
   525 lemma alw_inv:
   526   assumes stl: "\<And>s. f (stl s) = stl (f s)"
   527   shows "alw P (f s) \<longleftrightarrow> alw (\<lambda>x. P (f x)) s"
   528 proof
   529   assume "alw P (f s)" then show "alw (\<lambda>x. P (f x)) s"
   530     by (coinduction arbitrary: s rule: alw_coinduct)
   531        (auto simp: stl)
   532 next
   533   assume "alw (\<lambda>x. P (f x)) s" then show "alw P (f s)"
   534     by (coinduction arbitrary: s rule: alw_coinduct) (auto simp: stl[symmetric])
   535 qed
   536 
   537 lemma ev_inv:
   538   assumes stl: "\<And>s. f (stl s) = stl (f s)"
   539   shows "ev P (f s) \<longleftrightarrow> ev (\<lambda>x. P (f x)) s"
   540 proof
   541   assume "ev P (f s)" then show "ev (\<lambda>x. P (f x)) s"
   542     by (induction "f s" arbitrary: s) (auto simp: stl)
   543 next
   544   assume "ev (\<lambda>x. P (f x)) s" then show "ev P (f s)"
   545     by induction (auto simp: stl[symmetric])
   546 qed
   547 
   548 lemma alw_smap: "alw P (smap f s) \<longleftrightarrow> alw (\<lambda>x. P (smap f x)) s"
   549   by (rule alw_inv) simp
   550 
   551 lemma ev_smap: "ev P (smap f s) \<longleftrightarrow> ev (\<lambda>x. P (smap f x)) s"
   552   by (rule ev_inv) simp
   553 
   554 lemma alw_cong:
   555   assumes P: "alw P \<omega>" and eq: "\<And>\<omega>. P \<omega> \<Longrightarrow> Q1 \<omega> \<longleftrightarrow> Q2 \<omega>"
   556   shows "alw Q1 \<omega> \<longleftrightarrow> alw Q2 \<omega>"
   557 proof -
   558   from eq have "(alw P aand Q1) = (alw P aand Q2)" by auto
   559   then have "alw (alw P aand Q1) \<omega> = alw (alw P aand Q2) \<omega>" by auto
   560   with P show "alw Q1 \<omega> \<longleftrightarrow> alw Q2 \<omega>"
   561     by (simp add: alw_aand)
   562 qed
   563 
   564 lemma ev_cong:
   565   assumes P: "alw P \<omega>" and eq: "\<And>\<omega>. P \<omega> \<Longrightarrow> Q1 \<omega> \<longleftrightarrow> Q2 \<omega>"
   566   shows "ev Q1 \<omega> \<longleftrightarrow> ev Q2 \<omega>"
   567 proof -
   568   from P have "alw (\<lambda>xs. Q1 xs \<longrightarrow> Q2 xs) \<omega>" by (rule alw_mono) (simp add: eq)
   569   moreover from P have "alw (\<lambda>xs. Q2 xs \<longrightarrow> Q1 xs) \<omega>" by (rule alw_mono) (simp add: eq)
   570   moreover note ev_alw_impl[of Q1 \<omega> Q2] ev_alw_impl[of Q2 \<omega> Q1]
   571   ultimately show "ev Q1 \<omega> \<longleftrightarrow> ev Q2 \<omega>"
   572     by auto
   573 qed
   574 
   575 lemma alwD: "alw P x \<Longrightarrow> P x"
   576   by auto
   577 
   578 lemma alw_alwD: "alw P \<omega> \<Longrightarrow> alw (alw P) \<omega>"
   579   by simp
   580 
   581 lemma alw_ev_stl: "alw (ev P) (stl \<omega>) \<longleftrightarrow> alw (ev P) \<omega>"
   582   by (auto intro: alw.intros)
   583 
   584 lemma holds_Stream: "holds P (x ## s) \<longleftrightarrow> P x"
   585   by simp
   586 
   587 lemma holds_eq1[simp]: "holds ((=) x) = HLD {x}"
   588   by rule (auto simp: HLD_iff)
   589 
   590 lemma holds_eq2[simp]: "holds (\<lambda>y. y = x) = HLD {x}"
   591   by rule (auto simp: HLD_iff)
   592 
   593 lemma not_holds_eq[simp]: "holds (- (=) x) = not (HLD {x})"
   594   by rule (auto simp: HLD_iff)
   595 
   596 text \<open>Strong until\<close>
   597 
   598 context
   599   notes [[inductive_internals]]
   600 begin
   601 
   602 inductive suntil (infix "suntil" 60) for \<phi> \<psi> where
   603   base: "\<psi> \<omega> \<Longrightarrow> (\<phi> suntil \<psi>) \<omega>"
   604 | step: "\<phi> \<omega> \<Longrightarrow> (\<phi> suntil \<psi>) (stl \<omega>) \<Longrightarrow> (\<phi> suntil \<psi>) \<omega>"
   605 
   606 inductive_simps suntil_Stream: "(\<phi> suntil \<psi>) (x ## s)"
   607 
   608 end
   609 
   610 lemma suntil_induct_strong[consumes 1, case_names base step]:
   611   "(\<phi> suntil \<psi>) x \<Longrightarrow>
   612     (\<And>\<omega>. \<psi> \<omega> \<Longrightarrow> P \<omega>) \<Longrightarrow>
   613     (\<And>\<omega>. \<phi> \<omega> \<Longrightarrow> \<not> \<psi> \<omega> \<Longrightarrow> (\<phi> suntil \<psi>) (stl \<omega>) \<Longrightarrow> P (stl \<omega>) \<Longrightarrow> P \<omega>) \<Longrightarrow> P x"
   614   using suntil.induct[of \<phi> \<psi> x P] by blast
   615 
   616 lemma ev_suntil: "(\<phi> suntil \<psi>) \<omega> \<Longrightarrow> ev \<psi> \<omega>"
   617   by (induct rule: suntil.induct) auto
   618 
   619 lemma suntil_inv:
   620   assumes stl: "\<And>s. f (stl s) = stl (f s)"
   621   shows "(P suntil Q) (f s) \<longleftrightarrow> ((\<lambda>x. P (f x)) suntil (\<lambda>x. Q (f x))) s"
   622 proof
   623   assume "(P suntil Q) (f s)" then show "((\<lambda>x. P (f x)) suntil (\<lambda>x. Q (f x))) s"
   624     by (induction "f s" arbitrary: s) (auto simp: stl intro: suntil.intros)
   625 next
   626   assume "((\<lambda>x. P (f x)) suntil (\<lambda>x. Q (f x))) s" then show "(P suntil Q) (f s)"
   627     by induction (auto simp: stl[symmetric] intro: suntil.intros)
   628 qed
   629 
   630 lemma suntil_smap: "(P suntil Q) (smap f s) \<longleftrightarrow> ((\<lambda>x. P (smap f x)) suntil (\<lambda>x. Q (smap f x))) s"
   631   by (rule suntil_inv) simp
   632 
   633 lemma hld_smap: "HLD x (smap f s) = holds (\<lambda>y. f y \<in> x) s"
   634   by (simp add: HLD_def)
   635 
   636 lemma suntil_mono:
   637   assumes eq: "\<And>\<omega>. P \<omega> \<Longrightarrow> Q1 \<omega> \<Longrightarrow> Q2 \<omega>" "\<And>\<omega>. P \<omega> \<Longrightarrow> R1 \<omega> \<Longrightarrow> R2 \<omega>"
   638   assumes *: "(Q1 suntil R1) \<omega>" "alw P \<omega>" shows "(Q2 suntil R2) \<omega>"
   639   using * by induct (auto intro: eq suntil.intros)
   640 
   641 lemma suntil_cong:
   642   "alw P \<omega> \<Longrightarrow> (\<And>\<omega>. P \<omega> \<Longrightarrow> Q1 \<omega> \<longleftrightarrow> Q2 \<omega>) \<Longrightarrow> (\<And>\<omega>. P \<omega> \<Longrightarrow> R1 \<omega> \<longleftrightarrow> R2 \<omega>) \<Longrightarrow>
   643     (Q1 suntil R1) \<omega> \<longleftrightarrow> (Q2 suntil R2) \<omega>"
   644   using suntil_mono[of P Q1 Q2 R1 R2 \<omega>] suntil_mono[of P Q2 Q1 R2 R1 \<omega>] by auto
   645 
   646 lemma ev_suntil_iff: "ev (P suntil Q) \<omega> \<longleftrightarrow> ev Q \<omega>"
   647 proof
   648   assume "ev (P suntil Q) \<omega>" then show "ev Q \<omega>"
   649    by induct (auto dest: ev_suntil)
   650 next
   651   assume "ev Q \<omega>" then show "ev (P suntil Q) \<omega>"
   652     by induct (auto intro: suntil.intros)
   653 qed
   654 
   655 lemma true_suntil: "((\<lambda>_. True) suntil P) = ev P"
   656   by (simp add: suntil_def ev_def)
   657 
   658 lemma suntil_lfp: "(\<phi> suntil \<psi>) = lfp (\<lambda>P s. \<psi> s \<or> (\<phi> s \<and> P (stl s)))"
   659   by (simp add: suntil_def)
   660 
   661 lemma sfilter_P[simp]: "P (shd s) \<Longrightarrow> sfilter P s = shd s ## sfilter P (stl s)"
   662   using sfilter_Stream[of P "shd s" "stl s"] by simp
   663 
   664 lemma sfilter_not_P[simp]: "\<not> P (shd s) \<Longrightarrow> sfilter P s = sfilter P (stl s)"
   665   using sfilter_Stream[of P "shd s" "stl s"] by simp
   666 
   667 lemma sfilter_eq:
   668   assumes "ev (holds P) s"
   669   shows "sfilter P s = x ## s' \<longleftrightarrow>
   670     P x \<and> (not (holds P) suntil (HLD {x} aand nxt (\<lambda>s. sfilter P s = s'))) s"
   671   using assms
   672   by (induct rule: ev_induct_strong)
   673      (auto simp add: HLD_iff intro: suntil.intros elim: suntil.cases)
   674 
   675 lemma sfilter_streams:
   676   "alw (ev (holds P)) \<omega> \<Longrightarrow> \<omega> \<in> streams A \<Longrightarrow> sfilter P \<omega> \<in> streams {x\<in>A. P x}"
   677 proof (coinduction arbitrary: \<omega>)
   678   case (streams \<omega>)
   679   then have "ev (holds P) \<omega>" by blast
   680   from this streams show ?case
   681     by (induct rule: ev_induct_strong) (auto elim: streamsE)
   682 qed
   683 
   684 lemma alw_sfilter:
   685   assumes *: "alw (ev (holds P)) s"
   686   shows "alw Q (sfilter P s) \<longleftrightarrow> alw (\<lambda>x. Q (sfilter P x)) s"
   687 proof
   688   assume "alw Q (sfilter P s)" with * show "alw (\<lambda>x. Q (sfilter P x)) s"
   689   proof (coinduction arbitrary: s rule: alw_coinduct)
   690     case (stl s)
   691     then have "ev (holds P) s"
   692       by blast
   693     from this stl show ?case
   694       by (induct rule: ev_induct_strong) auto
   695   qed auto
   696 next
   697   assume "alw (\<lambda>x. Q (sfilter P x)) s" with * show "alw Q (sfilter P s)"
   698   proof (coinduction arbitrary: s rule: alw_coinduct)
   699     case (stl s)
   700     then have "ev (holds P) s"
   701       by blast
   702     from this stl show ?case
   703       by (induct rule: ev_induct_strong) auto
   704   qed auto
   705 qed
   706 
   707 lemma ev_sfilter:
   708   assumes *: "alw (ev (holds P)) s"
   709   shows "ev Q (sfilter P s) \<longleftrightarrow> ev (\<lambda>x. Q (sfilter P x)) s"
   710 proof
   711   assume "ev Q (sfilter P s)" from this * show "ev (\<lambda>x. Q (sfilter P x)) s"
   712   proof (induction "sfilter P s" arbitrary: s rule: ev_induct_strong)
   713     case (step s)
   714     then have "ev (holds P) s"
   715       by blast
   716     from this step show ?case
   717       by (induct rule: ev_induct_strong) auto
   718   qed auto
   719 next
   720   assume "ev (\<lambda>x. Q (sfilter P x)) s" then show "ev Q (sfilter P s)"
   721   proof (induction rule: ev_induct_strong)
   722     case (step s) then show ?case
   723       by (cases "P (shd s)") auto
   724   qed auto
   725 qed
   726 
   727 lemma holds_sfilter:
   728   assumes "ev (holds Q) s" shows "holds P (sfilter Q s) \<longleftrightarrow> (not (holds Q) suntil (holds (Q aand P))) s"
   729 proof
   730   assume "holds P (sfilter Q s)" with assms show "(not (holds Q) suntil (holds (Q aand P))) s"
   731     by (induct rule: ev_induct_strong) (auto intro: suntil.intros)
   732 next
   733   assume "(not (holds Q) suntil (holds (Q aand P))) s" then show "holds P (sfilter Q s)"
   734     by induct auto
   735 qed
   736 
   737 lemma suntil_aand_nxt:
   738   "(\<phi> suntil (\<phi> aand nxt \<psi>)) \<omega> \<longleftrightarrow> (\<phi> aand nxt (\<phi> suntil \<psi>)) \<omega>"
   739 proof
   740   assume "(\<phi> suntil (\<phi> aand nxt \<psi>)) \<omega>" then show "(\<phi> aand nxt (\<phi> suntil \<psi>)) \<omega>"
   741     by induction (auto intro: suntil.intros)
   742 next
   743   assume "(\<phi> aand nxt (\<phi> suntil \<psi>)) \<omega>"
   744   then have "(\<phi> suntil \<psi>) (stl \<omega>)" "\<phi> \<omega>"
   745     by auto
   746   then show "(\<phi> suntil (\<phi> aand nxt \<psi>)) \<omega>"
   747     by (induction "stl \<omega>" arbitrary: \<omega>)
   748        (auto elim: suntil.cases intro: suntil.intros)
   749 qed
   750 
   751 lemma alw_sconst: "alw P (sconst x) \<longleftrightarrow> P (sconst x)"
   752 proof
   753   assume "P (sconst x)" then show "alw P (sconst x)"
   754     by coinduction auto
   755 qed auto
   756 
   757 lemma ev_sconst: "ev P (sconst x) \<longleftrightarrow> P (sconst x)"
   758 proof
   759   assume "ev P (sconst x)" then show "P (sconst x)"
   760     by (induction "sconst x") auto
   761 qed auto
   762 
   763 lemma suntil_sconst: "(\<phi> suntil \<psi>) (sconst x) \<longleftrightarrow> \<psi> (sconst x)"
   764 proof
   765   assume "(\<phi> suntil \<psi>) (sconst x)" then show "\<psi> (sconst x)"
   766     by (induction "sconst x") auto
   767 qed (auto intro: suntil.intros)
   768 
   769 lemma hld_smap': "HLD x (smap f s) = HLD (f -` x) s"
   770   by (simp add: HLD_def)
   771 
   772 lemma pigeonhole_stream:
   773   assumes "alw (HLD s) \<omega>"
   774   assumes "finite s"
   775   shows "\<exists>x\<in>s. alw (ev (HLD {x})) \<omega>"
   776 proof -
   777   have "\<forall>i\<in>UNIV. \<exists>x\<in>s. \<omega> !! i = x"
   778     using \<open>alw (HLD s) \<omega>\<close> by (simp add: alw_iff_sdrop HLD_iff)
   779   from pigeonhole_infinite_rel[OF infinite_UNIV_nat \<open>finite s\<close> this]
   780   show ?thesis
   781     by (simp add: HLD_iff infinite_iff_alw_ev[symmetric])
   782 qed
   783 
   784 lemma ev_eq_suntil: "ev P \<omega> \<longleftrightarrow> (not P suntil P) \<omega>"
   785 proof
   786   assume "ev P \<omega>" then show "((\<lambda>xs. \<not> P xs) suntil P) \<omega>"
   787     by (induction rule: ev_induct_strong) (auto intro: suntil.intros)
   788 qed (auto simp: ev_suntil)
   789 
   790 end