From bd7a6113aeef5c64aeb9e916eafd17c7f5af34eb Mon Sep 17 00:00:00 2001 From: Gerwin Klein Date: Sat, 17 Nov 2018 08:43:56 +1100 Subject: [PATCH] lib: more library lemmas for OptionMonad --- lib/Monad_WP/OptionMonad.thy | 41 ++++++++++++++++++++++++++++++++++ lib/Monad_WP/OptionMonadND.thy | 1 + 2 files changed, 42 insertions(+) diff --git a/lib/Monad_WP/OptionMonad.thy b/lib/Monad_WP/OptionMonad.thy index ed2fe2022..3802a2a34 100644 --- a/lib/Monad_WP/OptionMonad.thy +++ b/lib/Monad_WP/OptionMonad.thy @@ -28,6 +28,10 @@ definition where "f |> g \ \s. case f s of None \ None | Some x \ g x" +abbreviation opt_map_Some :: "('s \ 'a) \ ('a \ 'b) \ 's \ 'b" (infixl "||>" 54) where + "f ||> g \ f |> (Some \ g)" +lemmas opt_map_Some_def = opt_map_def + lemma opt_map_cong [fundef_cong]: "\ f = f'; \v s. f s = Some v \ g v = g' v\ \ f |> g = f' |> g'" by (rule ext) (simp add: opt_map_def split: option.splits) @@ -216,6 +220,43 @@ lemma oreturnOkE: lemmas omonadE [elim!] = opt_mapE obindE oreturnE ofailE othrowE oreturnOkE oassertE +lemma in_opt_map_Some_eq: + "((f ||> g) x = Some y) = (\v. f x = Some v \ g v = y)" + by (simp add: in_opt_map_eq) + +lemma in_opt_map_None_eq[simp]: + "((f ||> g) x = None) = (f x = None)" + by (simp add: opt_map_def split: option.splits) + +lemma oreturn_comp[simp]: + "oreturn x \ f = oreturn x" + by (simp add: oreturn_def K_def o_def) + +lemma ofail_comp[simp]: + "ofail \ f = ofail" + by (auto simp: ofail_def K_def) + +lemma oassert_comp[simp]: + "oassert P \ f = oassert P" + by (simp add: oassert_def) + +lemma fail_apply[simp]: + "ofail s = None" + by (simp add: ofail_def K_def) + +lemma oassert_apply[simp]: + "oassert P s = (if P then Some () else None)" + by (simp add: oassert_def) + +lemma oreturn_apply[simp]: + "oreturn x s = Some x" + by simp + +lemma oapply_apply[simp]: + "oapply x s = s x" + by (simp add: oapply_def) + + section \"While" loops over option monad.\ text \ diff --git a/lib/Monad_WP/OptionMonadND.thy b/lib/Monad_WP/OptionMonadND.thy index 531c9a32a..2dc4b9a02 100644 --- a/lib/Monad_WP/OptionMonadND.thy +++ b/lib/Monad_WP/OptionMonadND.thy @@ -112,6 +112,7 @@ lemmas omonad_simps [simp] = gets_the_return gets_the_fail gets_the_returnOk gets_the_throwError gets_the_assert +lemmas in_omonad = bind_eq_Some_conv in_obind_eq in_opt_map_eq Let_def section "Relation between option monad loops and non-deterministic monad loops."