# Theory Follows

theory Follows
imports SubstAx ListOrder Multiset
```(*  Title:      HOL/UNITY/Follows.thy
Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
*)

section‹The Follows Relation of Charpentier and Sivilotte›

theory Follows
imports SubstAx ListOrder "HOL-Library.Multiset"
begin

definition Follows :: "['a => 'b::{order}, 'a => 'b::{order}] => 'a program set" (infixl "Fols" 65) where
"f Fols g == Increasing g ∩ Increasing f Int
Always {s. f s ≤ g s} Int
(⋂k. {s. k ≤ g s} LeadsTo {s. k ≤ f s})"

(*Does this hold for "invariant"?*)
lemma mono_Always_o:
"mono h ==> Always {s. f s ≤ g s} ⊆ Always {s. h (f s) ≤ h (g s)}"
apply (blast intro: monoD)
done

"mono (h::'a::order => 'b::order)
==> (⋂j. {s. j ≤ g s} LeadsTo {s. j ≤ f s}) ⊆
(⋂k. {s. k ≤ h (g s)} LeadsTo {s. k ≤ h (f s)})"
apply auto
apply (drule_tac x = "g s" in spec)
apply (blast intro: monoD order_trans)+
done

lemma Follows_constant [iff]: "F ∈ (%s. c) Fols (%s. c)"

lemma mono_Follows_o:
assumes "mono h"
shows "f Fols g ⊆ (h o f) Fols (h o g)"
proof
fix x
assume "x ∈ f Fols g"
with assms show "x ∈ (h ∘ f) Fols (h ∘ g)"
by (auto simp add: Follows_def mono_Increasing_o [THEN [2] rev_subsetD]
mono_Always_o [THEN [2] rev_subsetD]
mono_LeadsTo_o [THEN [2] rev_subsetD, THEN INT_D])
qed

lemma mono_Follows_apply:
"mono h ==> f Fols g ⊆ (%x. h (f x)) Fols (%x. h (g x))"
apply (drule mono_Follows_o)
done

lemma Follows_trans:
"[| F ∈ f Fols g;  F ∈ g Fols h |] ==> F ∈ f Fols h"
done

subsection‹Destruction rules›

lemma Follows_Increasing1: "F ∈ f Fols g ==> F ∈ Increasing f"

lemma Follows_Increasing2: "F ∈ f Fols g ==> F ∈ Increasing g"

lemma Follows_Bounded: "F ∈ f Fols g ==> F ∈ Always {s. f s ≤ g s}"

"F ∈ f Fols g ==> F ∈ {s. k ≤ g s} LeadsTo {s. k ≤ f s}"

"F ∈ f Fols g ==> F ∈ {s. k pfixLe g s} LeadsTo {s. k pfixLe f s}"
apply (drule_tac k="g s" in Follows_LeadsTo)
apply blast
apply (blast intro: pfixLe_trans prefix_imp_pfixLe)
done

"F ∈ f Fols g ==> F ∈ {s. k pfixGe g s} LeadsTo {s. k pfixGe f s}"
apply (drule_tac k="g s" in Follows_LeadsTo)
apply blast
apply (blast intro: pfixGe_trans prefix_imp_pfixGe)
done

lemma Always_Follows1:
"[| F ∈ Always {s. f s = f' s}; F ∈ f Fols g |] ==> F ∈ f' Fols g"

apply (simp add: Follows_def Increasing_def Stable_def, auto)
apply (erule_tac A = "{s. x ≤ f s}" and A' = "{s. x ≤ f s}"
in Always_Constrains_weaken, auto)
apply (drule Always_Int_I, assumption)
apply (force intro: Always_weaken)
done

lemma Always_Follows2:
"[| F ∈ Always {s. g s = g' s}; F ∈ f Fols g |] ==> F ∈ f Fols g'"
apply (simp add: Follows_def Increasing_def Stable_def, auto)
apply (erule_tac A = "{s. x ≤ g s}" and A' = "{s. x ≤ g s}"
in Always_Constrains_weaken, auto)
apply (drule Always_Int_I, assumption)
apply (force intro: Always_weaken)
done

subsection‹Union properties (with the subset ordering)›

(*Can replace "Un" by any sup.  But existing max only works for linorders.*)

lemma increasing_Un:
"[| F ∈ increasing f;  F ∈ increasing g |]
==> F ∈ increasing (%s. (f s) ∪ (g s))"
apply (simp add: increasing_def stable_def constrains_def, auto)
apply (drule_tac x = "f xb" in spec)
apply (drule_tac x = "g xb" in spec)
apply (blast dest!: bspec)
done

lemma Increasing_Un:
"[| F ∈ Increasing f;  F ∈ Increasing g |]
==> F ∈ Increasing (%s. (f s) ∪ (g s))"
apply (auto simp add: Increasing_def Stable_def Constrains_def
stable_def constrains_def)
apply (drule_tac x = "f xb" in spec)
apply (drule_tac x = "g xb" in spec)
apply (blast dest!: bspec)
done

lemma Always_Un:
"[| F ∈ Always {s. f' s ≤ f s}; F ∈ Always {s. g' s ≤ g s} |]
==> F ∈ Always {s. f' s ∪ g' s ≤ f s ∪ g s}"

(*Lemma to re-use the argument that one variable increases (progress)
while the other variable doesn't decrease (safety)*)
lemma Follows_Un_lemma:
"[| F ∈ Increasing f; F ∈ Increasing g;
F ∈ Increasing g'; F ∈ Always {s. f' s ≤ f s};
∀k. F ∈ {s. k ≤ f s} LeadsTo {s. k ≤ f' s} |]
==> F ∈ {s. k ≤ f s ∪ g s} LeadsTo {s. k ≤ f' s ∪ g s}"
apply (drule_tac x = "f s" in IncreasingD)
apply (drule_tac x = "g s" in IncreasingD)
apply (rule PSP_Stable)
apply (erule_tac x = "f s" in spec)
apply (erule Stable_Int, assumption, blast+)
done

lemma Follows_Un:
"[| F ∈ f' Fols f;  F ∈ g' Fols g |]
==> F ∈ (%s. (f' s) ∪ (g' s)) Fols (%s. (f s) ∪ (g s))"
apply (simp add: Follows_def Increasing_Un Always_Un del: Un_subset_iff sup.bounded_iff, auto)
apply (blast intro: Follows_Un_lemma)
(*Weakening is used to exchange Un's arguments*)
apply (blast intro: Follows_Un_lemma [THEN LeadsTo_weaken])
done

subsection‹Multiset union properties (with the multiset ordering)›

lemma increasing_union:
"[| F ∈ increasing f;  F ∈ increasing g |]
==> F ∈ increasing (%s. (f s) + (g s :: ('a::order) multiset))"
apply (simp add: increasing_def stable_def constrains_def, auto)
apply (drule_tac x = "f xb" in spec)
apply (drule_tac x = "g xb" in spec)
apply (drule bspec, assumption)
done

lemma Increasing_union:
"[| F ∈ Increasing f;  F ∈ Increasing g |]
==> F ∈ Increasing (%s. (f s) + (g s :: ('a::order) multiset))"
apply (auto simp add: Increasing_def Stable_def Constrains_def
stable_def constrains_def)
apply (drule_tac x = "f xb" in spec)
apply (drule_tac x = "g xb" in spec)
apply (drule bspec, assumption)
done

lemma Always_union:
"[| F ∈ Always {s. f' s ≤ f s}; F ∈ Always {s. g' s ≤ g s} |]
==> F ∈ Always {s. f' s + g' s ≤ f s + (g s :: ('a::order) multiset)}"
done

(*Except the last line, IDENTICAL to the proof script for Follows_Un_lemma*)
lemma Follows_union_lemma:
"[| F ∈ Increasing f; F ∈ Increasing g;
F ∈ Increasing g'; F ∈ Always {s. f' s ≤ f s};
∀k::('a::order) multiset.
F ∈ {s. k ≤ f s} LeadsTo {s. k ≤ f' s} |]
==> F ∈ {s. k ≤ f s + g s} LeadsTo {s. k ≤ f' s + g s}"
apply (drule_tac x = "f s" in IncreasingD)
apply (drule_tac x = "g s" in IncreasingD)
apply (rule PSP_Stable)
apply (erule_tac x = "f s" in spec)
apply (erule Stable_Int, assumption, blast)
done

(*The !! is there to influence to effect of permutative rewriting at the end*)
lemma Follows_union:
"!!g g' ::'b => ('a::order) multiset.
[| F ∈ f' Fols f;  F ∈ g' Fols g |]
==> F ∈ (%s. (f' s) + (g' s)) Fols (%s. (f s) + (g s))"
apply (simp add: Increasing_union Always_union, auto)
apply (blast intro: Follows_union_lemma)
(*now exchange union's arguments*)
apply (blast intro: Follows_union_lemma)
done

lemma Follows_sum:
"!!f ::['c,'b] => ('a::order) multiset.
[| ∀i ∈ I. F ∈ f' i Fols f i;  finite I |]
==> F ∈ (%s. ∑i ∈ I. f' i s) Fols (%s. ∑i ∈ I. f i s)"
apply (erule rev_mp)
apply (erule finite_induct, simp)
done

(*Currently UNUSED, but possibly of interest*)
lemma Increasing_imp_Stable_pfixGe:
"F ∈ Increasing func ==> F ∈ Stable {s. h pfixGe (func s)}"
apply (simp add: Increasing_def Stable_def Constrains_def constrains_def)
apply (blast intro: trans_Ge [THEN trans_genPrefix, THEN transD]
prefix_imp_pfixGe)
done

(*Currently UNUSED, but possibly of interest*)