diff --git a/lib/ExtraCorres.thy b/lib/ExtraCorres.thy index 04ab5bc3b6..d8476f0125 100644 --- a/lib/ExtraCorres.thy +++ b/lib/ExtraCorres.thy @@ -241,17 +241,19 @@ lemma hoare_from_abs_inv: lemma in_whileLoop_corres: assumes body_corres: "\r r'. rrel r r' \ - corres_underlying srel False nf' rrel (P and C r) (P' and C' r') (B r) (B' r')" - and body_inv: "\r. \P and C r\ B r \\_. P\" - "\r'. \P' and C' r'\ B' r' \\_. P'\" - and cond: "\r r' s s'. \rrel r r'; (s, s') \ srel; P s; P' s'\ \ C r s = C' r' s'" + corres_underlying srel nf nf' rrel (P r and C r) (P' r' and C' r') (B r) (B' r')" + and body_inv: "\r. \P r and C r\ B r \\r. P r\" + "\r'. \P' r' and C' r'\ B' r' \\r'. P' r'\" + and cond: "\r r' s s'. \rrel r r'; (s, s') \ srel; P r s; P' r' s'\ \ C r s = C' r' s'" and result: "(rv', t') \ fst (whileLoop C' B' r' s')" - shows "\s r. (s, s') \ srel \ rrel r r' \ P s \ P' s' + and nf: "nf \ (\r. no_fail (P r and C r) (B r))" + shows "\s r. (s, s') \ srel \ rrel r r' \ P r s \ P' r' s' \ (\rv t. (rv, t) \ fst (whileLoop C B r s) \ (t, t') \ srel \ rrel rv rv')" apply (rule in_whileLoop_induct[OF result]) apply (force simp: cond whileLoop_def) apply clarsimp - apply (frule (1) corres_underlyingD2[OF body_corres]; (fastforce simp: cond)?) + apply (frule (1) corres_underlyingD2[OF body_corres]; + (fastforce dest: nf simp: cond no_fail_def)?) apply clarsimp apply (frule use_valid[OF _ body_inv(1)]) apply (fastforce dest: cond) @@ -260,21 +262,21 @@ lemma in_whileLoop_corres: apply (fastforce simp: whileLoop_def intro: whileLoop_results.intros(3) dest: cond) done -lemma corres_whileLoop: - assumes cond: "\r r' s s'. \rrel r r'; (s, s') \ srel; P s; P' s'\ \ C r s = C' r' s'" +lemma corres_whileLoop_ret: + assumes cond: "\r r' s s'. \rrel r r'; (s, s') \ srel; P r s; P' r' s'\ \ C r s = C' r' s'" and body_corres: "\r r'. rrel r r' \ - corres_underlying srel False nf' rrel (P and C r) (P' and C' r') (B r) (B' r')" - and body_inv: "\r. \P and C r\ B r \\_. P\" - "\r'. \P' and C' r'\ B' r' \\_. P'\" + corres_underlying srel False nf' rrel (P r and C r) (P' r' and C' r') (B r) (B' r')" + and body_inv: "\r. \P r and C r\ B r \\r. P r\" + "\r'. \P' r' and C' r'\ B' r' \\r'. P' r'\" and rel: "rrel r r'" - and nf': "\r'. no_fail (P' and C' r') (B' r')" - and termin: "\r' s'. \P' s'; C' r' s'\ \ whileLoop_terminates C' B' r' s'" - shows "corres_underlying srel False nf' rrel P P' (whileLoop C B r) (whileLoop C' B' r')" + and nf': "\r'. no_fail (P' r' and C' r') (B' r')" + and termin: "\r' s'. \P' r' s'; C' r' s'\ \ whileLoop_terminates C' B' r' s'" + shows "corres_underlying srel False nf' rrel (P r) (P' r') (whileLoop C B r) (whileLoop C' B' r')" apply (rule corres_no_failI) apply (simp add: no_fail_def) apply (intro impI allI) - apply (erule_tac I="\_ s. P' s" + apply (erule_tac I="\r' s'. P' r' s'" and R="{((r', s'), r, s). C' r s \ (r', s') \ fst (B' r s) \ whileLoop_terminates C' B' r s}" in not_snd_whileLoop) @@ -293,82 +295,98 @@ lemma corres_whileLoop: apply (fastforce intro: assms) done +lemmas corres_whileLoop = + corres_whileLoop_ret[where P="\_. P" for P, where P'="\_. P'" for P', simplified] + lemma whileLoop_terminates_cross: assumes body_corres: "\r r'. rrel r r' \ - corres_underlying srel False nf' rrel (P and C r) (P' and C' r') (B r) (B' r')" - and cond: "\r r' s s'. \rrel r r'; (s, s') \ srel; P s; P' s'\ \ C r s = C' r' s'" - and body_inv: "\r. \P and C r\ B r \\_. P\" - "\r'. \P' and C' r'\ B' r' \\_. P'\" - and abs_termination: "\r s. P s \ whileLoop_terminates C B r s" - and ex_abs: "ex_abs_underlying srel P s'" + corres_underlying srel nf nf' rrel (P r and C r) (P' r' and C' r') (B r) (B' r')" + and cond: "\r r' s s'. \rrel r r'; (s, s') \ srel; P r s; P' r' s'\ \ C r s = C' r' s'" + and body_inv: "\r. \P r and C r\ B r \\r. P r\" + "\r'. \P' r' and C' r'\ B' r' \\r'. P' r'\" + and abs_termination: "\r s. (P r s \ C r s) \ whileLoop_terminates C B r s" + and ex_abs: "ex_abs_underlying srel (P r) s'" and rrel: "rrel r r'" - and P': "P' s'" + and P': "P' r' s'" + and nf: "nf \ (\r. no_fail (P r and C r) (B r))" shows "whileLoop_terminates C' B' r' s'" proof - - have helper: "\s. P s \ \r' s'. rrel r r' \ (s, s') \ srel \ P s \ P' s' - \ whileLoop_terminates C' B' r' s'" + have helper: "\s. P r s \ C r s \ \r' s'. rrel r r' \ (s, s') \ srel \ P r s \ P' r' s' + \ whileLoop_terminates C' B' r' s'" (is "\s. _ \ ?I r s") apply (rule_tac P="?I" in whileLoop_terminates.induct) apply (fastforce intro: abs_termination) apply (fastforce simp: whileLoop_terminates.intros dest: cond) apply (subst whileLoop_terminates.simps) apply clarsimp - apply (frule (1) corres_underlyingD2[OF body_corres], fastforce+) + apply (frule (1) corres_underlyingD2[OF body_corres], (fastforce dest: nf simp: no_fail_def)+) apply (fastforce dest: use_valid intro: body_inv) done show ?thesis apply (insert assms helper) - apply (clarsimp simp: ex_abs_underlying_def) + apply (cases "C' r' s'") + apply (fastforce simp: ex_abs_underlying_def) + apply (simp add: whileLoop_terminates.intros(1)) done qed -lemma corres_whileLoop_abs: - assumes cond: "\r r' s s'. \rrel r r'; (s, s') \ srel; P s; P' s'\ \ C r s = C' r' s'" +lemma corres_whileLoop_abs_ret: + assumes cond: "\r r' s s'. \rrel r r'; (s, s') \ srel; P r s; P' r' s'\ \ C r s = C' r' s'" and body_corres: "\r r'. rrel r r' \ - corres_underlying srel False nf' rrel (P and C r) (P' and C' r') (B r) (B' r')" - and nf: "\r. no_fail (P and C r) (B r)" + corres_underlying srel nf nf' rrel (P r and C r) (P' r' and C' r') (B r) (B' r')" and rrel: "rrel r r'" - and rrel2: "\r'. \r. rrel r r'" - and body_inv: "\r. \P and C r\ B r \\_. P\" - "\r'. \P' and C' r'\ B' r' \\_. P'\" - and abs_termination: "\r s. P s \ whileLoop_terminates C B r s" - shows "corres_underlying srel False nf' rrel P P' (whileLoop C B r) (whileLoop C' B' r')" + and body_inv: "\r. \P r and C r\ B r \\r. P r\" + "\r'. \P' r' and C' r'\ B' r' \\r'. P' r'\" + and abs_termination: "\r s. (P r s \ C r s) \ whileLoop_terminates C B r s" + and nf: "nf \ (\r. no_fail (P r and C r) (B r))" + shows "corres_underlying srel nf nf' rrel (P r) (P' r') (whileLoop C B r) (whileLoop C' B' r')" apply (rule corres_underlyingI) apply (frule in_whileLoop_corres[OF body_corres body_inv]; - (fastforce intro: body_corres body_inv rrel dest: cond)) - apply (rule_tac I="\rv' s'. \rv s. (s, s') \ srel \ rrel rv rv' \ P s \ P' s'" - and R="{((r', s'), r, s). C' r s \ (r', s') \ fst (B' r s) - \ whileLoop_terminates C' B' r s}" - in not_snd_whileLoop) + (fastforce intro: body_corres body_inv rrel dest: nf cond)) + apply (rule_tac I="\rv' s'. \rv s. (s, s') \ srel \ rrel rv rv' \ P rv s \ P' rv' s'" + and R="{((r', s'), r, s). C' r s \ (r', s') \ fst (B' r s) + \ whileLoop_terminates C' B' r s}" + in not_snd_whileLoop) apply (fastforce intro: rrel) - apply (rename_tac conc_r s) + apply (rename_tac s s' conc_r new_s) apply (clarsimp simp: validNF_def) apply (rule conjI) apply (intro hoare_vcg_conj_lift_pre_fix; (solves wpsimp)?) - apply (prop_tac "\abs_r. rrel abs_r conc_r") - apply (fastforce simp: rrel2) - apply clarsimp + apply (rule_tac Q="\s'. \rv s. (s, s') \ srel \ rrel rv conc_r + \ P rv s \ (P' conc_r s' \ C' conc_r s') \ s' = new_s" + in hoare_weaken_pre[rotated]) + apply clarsimp + apply (rule hoare_ex_pre) + apply (rename_tac abs_r) apply (rule hoare_weaken_pre) - apply (fastforce intro!: wp_from_corres_u body_inv body_corres) + apply (rule_tac G="rrel abs_r conc_r" in hoare_grab_asm) + apply (wpsimp wp: wp_from_corres_u[OF body_corres] body_inv) + apply (fastforce dest: nf) apply (fastforce dest: cond) apply (fastforce simp: valid_def) apply wpsimp apply (rule whileLoop_terminates_cross[OF body_corres]; - (fastforce dest: cond intro: body_inv abs_termination)) - apply (prop_tac "\abs_r. rrel abs_r conc_r") - apply (fastforce simp: rrel2) - apply clarsimp - apply (rule_tac P="\s'. \s. (s, s') \ srel \ (P and C abs_r) s \ P' s' \ C' conc_r s'" - in no_fail_pre) - apply (insert cond body_corres) - apply (fastforce intro: corres_u_nofail simp: pred_conj_def) - apply fastforce + (fastforce dest: nf cond intro: body_inv abs_termination)) + apply (rule_tac P="\s'. \rv s. (s, s') \ srel \ rrel rv conc_r + \ P rv s \ (P' conc_r s' \ C' conc_r s') \ s' = new_s" + in no_fail_pre[rotated]) + apply fastforce + apply (rule no_fail_ex_pre) + apply (rename_tac abs_r) + apply (rule no_fail_pre) + apply (rule_tac G="rrel abs_r conc_r" in no_fail_grab_asm) + apply (rule corres_u_nofail) + apply (fastforce dest: body_corres) + apply (fastforce dest: nf) + apply (fastforce simp: cond) apply (fastforce intro: wf_subset[OF whileLoop_terminates_wf[where C=C']]) done +lemmas corres_whileLoop_abs = + corres_whileLoop_abs_ret[where P="\_. P" for P, where P'="\_. P'" for P', simplified] text \Some corres_underlying rules for monadic combinators\