From 527cdd329a638ead01f4948758ce763646aa7e01 Mon Sep 17 00:00:00 2001 From: Michael McInerney Date: Mon, 30 Jan 2023 11:50:57 +1030 Subject: [PATCH] clib: add ccorres_While rule Signed-off-by: Michael McInerney --- lib/clib/CCorresLemmas.thy | 118 +++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/lib/clib/CCorresLemmas.thy b/lib/clib/CCorresLemmas.thy index 559c5e55c..3618371ae 100644 --- a/lib/clib/CCorresLemmas.thy +++ b/lib/clib/CCorresLemmas.thy @@ -1033,4 +1033,122 @@ lemma ccorres_Guard_True_Seq: \ ccorres_underlying sr \ r xf arrel axf A C hs a (Guard F \True\ c ;; d)" by (simp, ccorres_rewrite, assumption) +lemma exec_While_final_inv'': + "\ \ \ \b, x\ \ s'; b = While C B; x = Normal s; + \s. s \ C \ I s (Normal s); + \t t' t''. \ t \ C; \\ \B, Normal t\ \ Normal t'; \\ \While C B, Normal t'\ \ t''; + I t' t'' \ \ I t t''; + \t t'. \ t \ C; \\ \B, Normal t\ \ Abrupt t' \ \ I t (Abrupt t'); + \t. \ t \ C; \ \ \B, Normal t\ \ Stuck \ \ I t Stuck; + \t f. \ t \ C; \\ \B, Normal t\ \ Fault f \ \ I t (Fault f) \ + \ I s s'" + apply (induct arbitrary: s rule: exec.induct; simp) + apply (erule exec_elim_cases; fastforce simp: exec.WhileTrue exec.WhileFalse) + done + +lemma intermediate_Normal_state: + "\\ \ \Seq c\<^sub>1 c\<^sub>2, Normal t\ \ t''; t \ P; \ \ P c\<^sub>1 Q\ + \ \t'. \ \ \c\<^sub>1, Normal t\ \ Normal t' \ \ \ \c\<^sub>2, Normal t'\ \ t''" + apply (erule exec_Normal_elim_cases(8)) + apply (insert hoarep_exec) + apply fastforce + done + +lemma While_inv_from_body: + "\ \ (G \ C) B G \ \ \ G While C B G" + apply (drule hoare_sound)+ + apply (rule hoare_complete) + apply (clarsimp simp: cvalid_def HoarePartialDef.valid_def) + by (erule exec_While_final_inv''[where I="\s s'. s \ G \ s' \ Normal ` G", THEN impE], + fastforce+) + +lemma While_inv_from_body_setter: + "\\ \ G setter (G \ {s. s \ C \ s \ Cnd}); \ \ (G \ Cnd) B G\ + \ \ \ (G \ {s. s \ C \ s \ Cnd}) While C (Seq B setter) G" + apply (drule hoare_sound)+ + apply (rule hoare_complete) + apply (clarsimp simp: cvalid_def HoarePartialDef.valid_def) + by (rule exec_While_final_inv'' + [where I="\s s'. s \ G \ (s \ C \ s \ Cnd) \ s' \ Normal ` G", THEN impE], + (fastforce dest!: intermediate_Normal_state[where c\<^sub>1=B and P="G \ Cnd" and Q=G] + intro: hoare_complete + simp: cvalid_def HoarePartialDef.valid_def)+) + +lemmas hoarep_Int_pre_fix = hoarep_Int[where P=P and P'=P for P, simplified] + +lemma ccorres_While: + assumes body_ccorres: + "\r. ccorresG srel \ (=) xf (G and (\s. the (C r s))) (G' \ C') hs (B r) B'" + and cond_ccorres: + "\r. ccorresG srel \ (\rv rv'. rv = to_bool rv') cond_xf G G' hs (gets_the (C r)) setter" + and nf: "\r. no_fail (G and (\s. the (C r s))) (B r)" + and no_ofail: "\r. no_ofail G (C r)" + and body_inv: "\r. \G and (\s. the (C r s))\ B r \\_. G\" + "\ \ (G' \ C') B' G'" + and setter_inv_cond: "\ \ G' setter (G' \ {s'. cond_xf s' \ 0 \ s' \ C'})" + and setter_xf_inv: "\val. \ \ {s'. xf s' = val} setter {s'. xf s' = val}" + shows "ccorresG srel \ (=) xf G (G' \ {s'. xf s' = r}) hs + (whileLoop (\r s. the (C r s)) B r) + (Seq setter (While {s'. cond_xf s' \ 0} (Seq B' setter)))" +proof - + note unif_rrel_def[simp add] to_bool_def[simp add] + have helper: + "\state xstate'. + \ \ \While {s'. cond_xf s' \ 0} (Seq B' setter), Normal state\ \ xstate' \ + \st r s. Normal st = xstate' \ (s, state) \ srel + \ (cond_xf state \ 0) = the (C r s) \ xf state = r \ G s + \ state \ G' \ (cond_xf state \ 0 \ state \ C') + \ (\rv s'. (rv, s') \ fst (whileLoop (\r s. the (C r s)) B r s) + \ (s', st) \ srel \ rv = xf st)" + apply (erule exec_While_final_inv''; simp) + apply (fastforce simp: whileLoop_cond_fail return_def) + apply clarsimp + apply (rename_tac t t' t'' s) + apply (frule intermediate_Normal_state[where P="G' \ C'"]) + apply fastforce + apply (fastforce intro: body_inv) + apply clarsimp + apply (rename_tac inter_t) + apply (prop_tac "\s'. (xf inter_t, s') \ fst (B (xf t) s) \ (s', inter_t) \ srel") + subgoal by (erule ccorresE[OF body_ccorres]) + (fastforce simp: no_fail_def nf[simplified no_fail_def] dest: EHOther)+ + apply clarsimp + apply (prop_tac "G s'") + apply (fastforce dest: use_valid intro: body_inv) + apply (prop_tac "inter_t \ G'") + apply (fastforce dest: hoarep_exec[rotated] intro: body_inv) + apply (drule_tac x=s' in spec) + apply (elim impE) + apply (drule_tac s'=inter_t and r1="xf t'" in ccorresE_gets_the[OF cond_ccorres]; assumption?) + apply (fastforce intro: no_ofail) + apply (fastforce dest: EHOther) + subgoal by (fastforce dest: hoarep_exec intro: setter_inv_cond) + apply (prop_tac "xf inter_t = xf t'") + apply (fastforce dest: hoarep_exec[rotated] intro: setter_xf_inv) + apply (fastforce simp: whileLoop_def intro: whileLoop_results.intros(3)) + done + + show ?thesis + apply (clarsimp simp: ccorres_underlying_def) + apply (rename_tac s s' n xstate) + apply (frule (1) exec_handlers_use_hoare_nothrow_hoarep) + apply (rule_tac R="G' \ {s'. s' \ {t. cond_xf t \ 0} \ s' \ C'}" in HoarePartial.Seq) + apply (fastforce intro: setter_inv_cond) + apply (fastforce intro: While_inv_from_body_setter setter_inv_cond body_inv) + apply clarsimp + apply (frule (1) intermediate_Normal_state) + apply (fastforce intro: setter_inv_cond) + apply clarsimp + apply (rename_tac inter_t) + apply (drule (2) ccorresE_gets_the[OF cond_ccorres _ _ _ no_ofail]) + apply (fastforce dest: EHOther) + apply (prop_tac "xf inter_t = xf s'") + apply (fastforce dest: hoarep_exec[rotated] intro: setter_xf_inv) + apply (clarsimp simp: isNormal_def) + apply (auto dest: hoarep_exec dest!: helper spec intro: setter_inv_cond) + done +qed + +lemmas ccorres_While' = ccorres_While[where C'=UNIV, simplified] + end