(* * Copyright 2020, Data61, CSIRO (ABN 41 687 119 230) * * SPDX-License-Identifier: BSD-2-Clause *) theory MonadSep imports Sep_Algebra_L4v "Lib.LemmaBucket" begin locale sep_lifted = fixes lft :: "'a \ 's :: sep_algebra" begin abbreviation lift :: "('s \ 'b) \ 'a \ 'b" ("<_>") where "

s \ P (lft s)" lemma hoare_gen_lifted_asm: "(P \ \\s. P' (lft s)\ f \Q\) \ \\s. (P' and K P) (lft s)\ f \Q\" by (auto intro: hoare_assume_pre) lemma mapM_x_sep_inv': includes no_pre assumes f: "\R x. x \ S \ \\s.

* I \* R> s \ I' s\ f x \\_ s.* I \* R> s \ I' s\" shows "set xs \ S \ \\s.<\* map P xs \* I \* R> s \ I' s\ mapM_x f xs \\_ s.<\* map Q xs \* I \* R> s \ I' s\" proof (induct xs arbitrary: R) case Nil thus ?case by (simp add: mapM_x_Nil) next case (Cons x xs) thus ?case apply (simp add: sep_conj_assoc mapM_x_Cons) apply (wp) apply (insert Cons.hyps [where R1="Q x ** R"])[1] apply (simp add: sep_conj_ac) apply (insert f [where R1="R ** \* map P xs" and x1=x ])[1] apply (simp add: sep_conj_ac) done qed lemmas mapM_x_sep_inv = mapM_x_sep_inv' [OF _ subset_refl] lemmas mapM_x_sep = mapM_x_sep_inv [where I' = \, simplified] lemmas mapM_x_sep' = mapM_x_sep [where I=\, simplified] lemma mapM_x_set_sep_inv: "\distinct xs; set xs = X; (\R x. x \ X \ \

* I \* R> and I'\ f x \\_. * I \* R> and I'\)\ \ \<(\* x \ X. P x) \* I \* R> and I'\ mapM_x f xs \\_. <(\* x \ X. Q x) \* I \* R> and I'\" apply (clarsimp simp: pred_conj_def) apply (drule mapM_x_sep_inv [where R=R]) apply (subst (asm) sep_list_conj_sep_map_set_conj, assumption)+ apply assumption done lemmas mapM_x_set_sep' = mapM_x_set_sep_inv [where I' = \, simplified] lemma mapM_x_set_sep: "\distinct xs; \R x. x \ set xs \ \

* I \* R>\ f x \\_. * I \* R>\\ \ \<(\* x \ set xs. P x) \* I \* R>\ mapM_x f xs \\_. <(\* x \ set xs. Q x) \* I \* R>\" by (erule mapM_x_set_sep', simp+) (* NOTE: unused *) lemma foldM_Cons: "foldM f (x # xs) acc = do acc' \ foldM f xs acc; f x acc' od" by (clarsimp simp: foldM_def) lemma foldM_sep_inv': includes no_pre assumes f: "\R x acc. x \ S \ \\s.

* I \* R> s \ I' s\ f x acc \\acc' s. * I \* R> s \ I' s\" shows "set xs \ S \ \\s. <\* map P xs \* I \* R> s \ I' s\ foldM f xs acc \\acc' s. <\* map Q xs \* I \* R> s \ I' s\" proof (induct xs arbitrary: R acc) case Nil thus ?case by (simp add: foldM_def) next case (Cons x xs) thus ?case apply (simp add: sep_conj_assoc foldM_Cons) apply wp apply (insert f[where R1="R ** \* map Q xs" and x1=x])[1] apply (fastforce simp: sep_conj_ac) apply (insert Cons.hyps [where R1="P x ** R"])[1] apply (clarsimp simp: sep_conj_ac) done qed lemmas foldM_sep_inv = foldM_sep_inv' [OF _ subset_refl] lemmas foldM_sep = foldM_sep_inv [where I' = \, simplified] lemmas foldM_sep' = foldM_sep [where I=\, simplified] lemma foldM_set_sep_inv: "\distinct xs; set xs = X; \R x acc. x \ X \ \

* I \* R> and I'\ f x acc \\_. * I \* R> and I'\\ \ \<(\* x \ X. P x) \* I \* R> and I'\ foldM f xs acc \\_. <(\* x \ X. Q x) \* I \* R> and I'\" apply (clarsimp simp: pred_conj_def) apply (drule foldM_sep_inv [where R=R]) apply (subst (asm) sep_list_conj_sep_map_set_conj, assumption)+ apply assumption done lemmas foldM_set_sep' = foldM_set_sep_inv [where I' = \, simplified] lemma foldM_set_sep: "\distinct xs; \R x acc. x \ set xs \ \

* I \* R>\ f x acc \\_. * I \* R>\\ \ \<(\* x \ set xs. P x) \* I \* R>\ foldM f xs acc \\_. <(\* x \ set xs. Q x) \* I \* R>\" by (erule foldM_set_sep', simp+) lemma sep_list_conj_map_singleton_wp: "\x \ set xs; \R. \

* I x \* R>\ f \\_. * I x \* R>\\ \ \

* \* map I xs \* R>\ f \\_. * \* map I xs \* R>\" apply (rule hoare_chain [where P="

* I x \* \* map I (remove1 x xs) \* R>" and Q="\_. * I x \* \* map I (remove1 x xs) \* R>"]) apply fastforce apply (subst (asm) sep_list_conj_map_remove1, assumption) apply (sep_select_asm 3) apply (sep_solve) apply (subst sep_list_conj_map_remove1, sep_solve+) done lemma sep_set_conj_map_singleton_wp: "\finite xs; x \ xs; \R. \

* I x \* R>\ f \\_. * I x \* R>\\ \ \

* (\* x\xs. I x) \* R>\ f \\_. * (\* x\xs. I x) \* R>\" apply (rule hoare_chain [where P="

* I x \* (\* x\xs - {x}. I x) \* R>" and Q="\_. * I x \* (\* x\xs - {x}. I x) \* R>"], assumption) apply (subst (asm) sep.prod.remove, assumption+) apply sep_solve apply (subst sep.prod.remove, assumption+) apply sep_solve done lemma sep_list_conj_submap_wp: "\set xs \ set ys; distinct xs; distinct ys; \R. \

* \* map I xs \* R>\ f \\_. * \* map I xs \* R>\\ \ \

* \* map I ys \* R>\ f \\_. * \* map I ys \* R>\" apply (subst sep_list_con_map_filter [where t="\x. x \ set xs" and xs=ys, THEN sym]) apply (subst sep_list_con_map_filter [where t="\x. x \ set xs" and xs=ys, THEN sym]) apply (subgoal_tac "set [x\ys . x \ set xs] = set xs") apply (subst sep_list_conj_eq [where xs="[x\ys . x \ set xs]" and ys=xs], clarsimp+) apply (subst sep_list_conj_eq [where xs="[x\ys . x \ set xs]" and ys=xs], clarsimp+) apply (clarsimp simp: sep_conj_assoc) apply auto done (* This just saves some rearranging later. *) lemma sep_list_conj_submap_wp': "\set xs \ set ys; distinct xs; distinct ys; \R. \

* \* map I xs \* S \* R>\ f \\_. * \* map I xs \* S \* R>\\ \ \

* \* map I ys \* S \* R>\ f \\_. * \* map I ys \* S \* R>\" apply (cut_tac sep_list_conj_submap_wp [where P="P \* S" and Q="Q \* S" and I=I and R=R and ys=ys and xs=xs and f=f]) apply (fastforce simp: sep_conj_ac)+ done lemma sep_set_conj_subset_wp: "\xs \ ys; finite xs; finite ys; \R. \

* (\* x \ xs. I x) \* R>\ f \\_. * (\* x \ xs. I x) \* R>\\ \ \

* (\* x \ ys. I x) \* R>\ f \\_. * (\* x \ ys. I x) \* R>\" apply (subst sep_map_set_conj_restrict [where t="\x. x \ xs" and xs=ys], assumption) apply (subst sep_map_set_conj_restrict [where t="\x. x \ xs" and xs=ys], assumption) apply (subgoal_tac "{x \ ys. x \ xs} = xs") apply (clarsimp simp: sep_conj_assoc) apply auto done (* This just saves some rearranging later. *) lemma sep_set_conj_subset_wp': "\xs \ ys; finite xs; finite ys; \R. \

* (\* x \ xs. I x) \* S \* R>\ f \\_. * (\* x \ xs. I x) \* S \* R>\\ \ \

* (\* x \ ys. I x) \* S \* R>\ f \\_. * (\* x \ ys. I x) \* S \* R>\" apply (cut_tac sep_set_conj_subset_wp [where P="P \* S" and Q="Q \* S" and I=I and R=R and ys=ys and xs=xs and f=f]) apply (fastforce simp: sep_conj_ac)+ done end end