# Theory Reach

theory Reach
imports UNITY_Main
```(*  Title:      HOL/UNITY/Simple/Reach.thy
Author:     Lawrence C Paulson, Cambridge University Computer Laboratory

Reachability in Directed Graphs.  From Chandy and Misra, section 6.4.
[this example took only four days!]
*)

theory Reach imports "../UNITY_Main" begin

typedecl vertex

type_synonym state = "vertex=>bool"

consts
init ::  "vertex"

edges :: "(vertex*vertex) set"

definition asgt :: "[vertex,vertex] => (state*state) set"
where "asgt u v = {(s,s'). s' = s(v:= s u | s v)}"

definition Rprg :: "state program"
where "Rprg = mk_total_program ({%v. v=init}, ⋃(u,v)∈edges. {asgt u v}, UNIV)"

definition reach_invariant :: "state set"
where "reach_invariant = {s. (∀v. s v --> (init, v) ∈ edges⇧*) & s init}"

definition fixedpoint :: "state set"
where "fixedpoint = {s. ∀(u,v)∈edges. s u --> s v}"

definition metric :: "state => nat"
where "metric s = card {v. ~ s v}"

text‹*We assume that the set of vertices is finite›
axiomatization where
finite_graph:  "finite (UNIV :: vertex set)"

(*TO SIMPDATA.ML??  FOR CLASET??  *)
lemma ifE [elim!]:
"[| if P then Q else R;
[| P;   Q |] ==> S;
[| ~ P; R |] ==> S |] ==> S"
by (simp split: if_split_asm)

declare Rprg_def [THEN def_prg_Init, simp]

declare asgt_def [THEN def_act_simp,simp]

text‹All vertex sets are finite›
declare finite_subset [OF subset_UNIV finite_graph, iff]

declare reach_invariant_def [THEN def_set_simp, simp]

lemma reach_invariant: "Rprg ∈ Always reach_invariant"
apply (rule AlwaysI, force)
apply (unfold Rprg_def, safety)
apply (blast intro: rtrancl_trans)
done

(*** Fixedpoint ***)

(*If it reaches a fixedpoint, it has found a solution*)
lemma fixedpoint_invariant_correct:
"fixedpoint ∩ reach_invariant = { %v. (init, v) ∈ edges⇧* }"
apply (unfold fixedpoint_def)
apply (rule equalityI)
apply (auto intro!: ext)
apply (erule rtrancl_induct, auto)
done

lemma lemma1:
"FP Rprg ⊆ fixedpoint"
apply (simp add: FP_def fixedpoint_def Rprg_def mk_total_program_def)
apply (auto simp add: stable_def constrains_def)
apply (drule bspec, assumption)
apply (drule fun_cong, auto)
done

lemma lemma2:
"fixedpoint ⊆ FP Rprg"
apply (simp add: FP_def fixedpoint_def Rprg_def mk_total_program_def)
apply (auto intro!: ext simp add: stable_def constrains_def)
done

lemma FP_fixedpoint: "FP Rprg = fixedpoint"
by (rule equalityI [OF lemma1 lemma2])

(*If we haven't reached a fixedpoint then there is some edge for which u but
not v holds.  Progress will be proved via an ENSURES assertion that the
metric will decrease for each suitable edge.  A union over all edges proves
a LEADSTO assertion that the metric decreases if we are not at a fixedpoint.
*)

lemma Compl_fixedpoint: "- fixedpoint = (⋃(u,v)∈edges. {s. s u & ~ s v})"
apply (simp add: FP_fixedpoint [symmetric] Rprg_def mk_total_program_def)
apply (rule subset_antisym)
apply (auto simp add: Compl_FP UN_UN_flatten)
apply (rule fun_upd_idem, force)
apply (force intro!: rev_bexI simp add: fun_upd_idem_iff)
done

lemma Diff_fixedpoint:
"A - fixedpoint = (⋃(u,v)∈edges. A ∩ {s. s u & ~ s v})"
by (simp add: Diff_eq Compl_fixedpoint, blast)

(*** Progress ***)

lemma Suc_metric: "~ s x ==> Suc (metric (s(x:=True))) = metric s"
apply (unfold metric_def)
apply (subgoal_tac "{v. ~ (s (x:=True)) v} = {v. ~ s v} - {x}")
prefer 2 apply force
done

lemma metric_less [intro!]: "~ s x ==> metric (s(x:=True)) < metric s"
by (erule Suc_metric [THEN subst], blast)

lemma metric_le: "metric (s(y:=s x | s y)) ≤ metric s"
by (cases "s x --> s y") (auto intro: less_imp_le simp add: fun_upd_idem)

"Rprg ∈ ((metric-`{m}) - fixedpoint) LeadsTo (metric-`(lessThan m))"
apply (simp (no_asm) add: Diff_fixedpoint Rprg_def)
apply (ensures_tac "asgt a b")
prefer 2 apply blast
apply (drule metric_le [THEN order_antisym])
apply (auto elim: less_not_refl3 [THEN [2] rev_notE])
done

"Rprg ∈ (metric-`{m}) LeadsTo (metric-`(lessThan m) ∪ fixedpoint)"
done

(*Execution in any state leads to a fixedpoint (i.e. can terminate)*)