section‹Inductive definition of Hoare logic for partial correctness›
theory Hoare imports Natural begin
text‹
Completeness is taken relative to completeness of the underlying logic.
Two versions of completeness proof: nested single recursion
vs. simultaneous recursion in call rule ›
type_synonym 'a assn = "'a => state => bool" translations
(type) "'a assn" <= (type) "'a => state => bool"
definition
state_not_singleton :: bool where "state_not_singleton = (∃s t::state. s ~= t)"(* at least two elements *)
definition
peek_and :: "'a assn => (state => bool) => 'a assn" (infixr‹&>›35) where "peek_and P p = (%Z s. P Z s & p s)"
datatype 'a triple =
triple "'a assn" com "'a assn" (‹{(1_)}./ (_)/ .{(1_)}› [3,60,3] 58)
definition
triple_valid :: "nat => 'a triple => bool" ( ‹|=_:_› [0 , 58] 57) where "|=n:t = (case t of {P}.c.{Q} => ∀Z s. P Z s ⟶ (∀s'. <c,s> -n-> s' ⟶ Q Z s'))" abbreviation
triples_valid :: "nat => 'a triple set => bool" (‹||=_:_› [0 , 58] 57) where "||=n:G == Ball G (triple_valid n)"
definition
hoare_valids :: "'a triple set => 'a triple set => bool" (‹_||=_› [58, 58] 57) where "G||=ts = (∀n. ||=n:G --> ||=n:ts)" abbreviation
hoare_valid :: "'a triple set => 'a triple => bool" (‹_|=_› [58, 58] 57) where "G |=t == G||={t}"
(* Most General Triples *) definition
MGT :: "com => state triple" (‹{=}._.{->}› [60] 58) where "{=}.c.{->} = {%Z s0. Z = s0}. c .{%Z s1. <c,Z> -c-> s1}"
inductive
hoare_derivs :: "'a triple set => 'a triple set => bool" (‹_||-_› [58, 58] 57) and
hoare_deriv :: "'a triple set => 'a triple => bool" (‹_|-_› [58, 58] 57) where "G |-t == G||-{t}"
| empty: "G||-{}"
| insert: "[| G |-t; G||-ts |] ==> G||-insert t ts"
| asm: "ts <= G ==> G||-ts"(* {P}.BODY pn.{Q} instead of (general) t for SkipD_lemma *)
| cut: "[| G'||-ts; G||-G' |] ==> G||-ts"(* for convenience and efficiency *)
| weaken: "[| G||-ts' ; ts <= ts' |] ==> G||-ts"
| conseq: "∀Z s. P Z s ⟶ (∃P' Q'. G|-{P'}.c.{Q'} ∧ (∀s'. (∀Z'. P' Z' s ⟶ Q' Z' s') ⟶ Q Z s')) ==> G|-{P}.c.{Q}"
| Skip: "G|-{P}. SKIP .{P}"
| Ass: "G|-{%Z s. P Z (s[X::=a s])}. X:==a .{P}"
| Local: "G|-{P}. c .{%Z s. Q Z (s[Loc X::=s'<X>])} ==> G|-{%Z s. s'=s & P Z (s[Loc X::=a s])}. LOCAL X:=a IN c .{Q}"
| If: "[| G|-{P &> b }.c.{Q}; G|-{P &> (Not o b)}.d.{Q} |] ==> G|-{P}. IF b THEN c ELSE d .{Q}"
| Loop: "G|-{P &> b}.c.{P} ==> G|-{P}. WHILE b DO c .{P &> (Not o b)}"
(* BodyN:"(insert({P}.BODYpn.{Q})G) |-{P}.the(bodypn).{Q}==> G|-{P}.BODYpn.{Q}"
*)
| Body: "[| G Un (%p. {P p}. BODY p .{Q p})`Procs ||-(%p. {P p}. the (body p) .{Q p})`Procs |] ==> G||-(%p. {P p}. BODY p .{Q p})`Procs"
| Call: "G|-{P}. BODY pn .{%Z s. Q Z (setlocs s (getlocs s')[X::=s<Res>])} ==> G|-{%Z s. s'=s & P Z (setlocs s newlocs[Loc Arg::=a s])}. X:=CALL pn(a) .{Q}"
section‹Soundness and relative completeness of Hoare rules wrt operational semantics›
lemma conseq12: "[| G|-{P'}.c.{Q'}; ∀Z s. P Z s ⟶ (∀s'. (∀Z'. P' Z' s ⟶ Q' Z' s') --> Q Z s') |] ==> G|-{P}.c.{Q}" apply (rule hoare_derivs.conseq) apply blast done
lemma conseq1: "[| G|-{P'}.c.{Q}; ∀Z s. P Z s ⟶ P' Z s |] ==> G|-{P}.c.{Q}" apply (erule conseq12) apply fast done
lemma conseq2: "[| G|-{P}.c.{Q'}; ∀Z s. Q' Z s ⟶ Q Z s |] ==> G|-{P}.c.{Q}" apply (erule conseq12) apply fast done
lemma Body1: "[| G Un (λp. {P p}. BODY p .{Q p})`Procs ||- (λp. {P p}. the (body p) .{Q p})`Procs; pn ∈ Procs |] ==> G|-{P pn}. BODY pn .{Q pn}" apply (drule hoare_derivs.Body) apply (erule hoare_derivs.weaken) apply fast done
lemma BodyN: "(insert ({P}. BODY pn .{Q}) G) |-{P}. the (body pn) .{Q} ==> G|-{P}. BODY pn .{Q}" apply (rule Body1) apply (rule_tac [2] singletonI) apply clarsimp done
lemma escape: "[| ∀Z s. P Z s ⟶ G|-{λZ s'. s'=s}.c.{λZ'. Q Z} |] ==> G|-{P}.c.{Q}" apply (rule hoare_derivs.conseq) apply fast done
lemma"constant": "[| C ==> G|-{P}.c.{Q} |] ==> G|-{λZ s. P Z s & C}.c.{Q}" apply (rule hoare_derivs.conseq) apply fast done
lemma LoopF: "G|-{λZ s. P Z s ∧¬b s}.WHILE b DO c.{P}" apply (rule hoare_derivs.Loop [THEN conseq2]) apply (simp_all (no_asm)) apply (rule hoare_derivs.conseq) apply fast done
lemma Loop_sound_lemma: "G|={P &> b}. c .{P} ==> G|={P}. WHILE b DO c .{P &> (Not o b)}" apply (unfold hoare_valids_def) apply (simp (no_asm_use) add: triple_valid_def2) apply (rule allI) apply (subgoal_tac "∀d s s'. <d,s> -n-> s' --> d = WHILE b DO c --> ||=n:G --> (∀Z. P Z s --> P Z s' & ~b s') ") apply (erule thin_rl, fast) apply ((rule allI)+, rule impI) apply (erule evaln.induct) apply (simp_all (no_asm)) apply fast apply fast done
lemma disj: "[| G|-{P}.c.{Q}; G|-{P'}.c.{Q'} |] ==> G|-{%Z s. P Z s | P' Z s}.c.{%Z s. Q Z s | Q' Z s}" apply (rule hoare_derivs.conseq) apply (fast elim: conseq12) done(* analogue conj non-derivable *)
lemma hoare_SkipI: "(∀Z s. P Z s ⟶ Q Z s) ==> G|-{P}. SKIP .{Q}" apply (rule conseq12) apply (rule hoare_derivs.Skip) apply fast done
lemma export_s: "[| !!s'. G|-{%Z s. s'=s & P Z s}.c.{Q} |] ==> G|-{P}.c.{Q}" apply (rule hoare_derivs.conseq) apply auto done
lemma weak_Local: "[| G|-{P}. c .{Q}; ∀k Z s. Q Z s --> Q Z (s[Loc Y::=k]) |] ==> G|-{%Z s. P Z (s[Loc Y::=a s])}. LOCAL Y:=a IN c .{Q}" apply (rule export_s) apply (rule hoare_derivs.Local) apply (erule conseq2) apply (erule spec) done
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.