(* Goal verification framework - Hoare Logic *) \ \This theory sets up the reasoning about programs using Hoare triples. We give the syntax and semantics of Hoare triples, given an agent. We prove a relation between Hoare triples for basic and conditional actions.\ theory Gvf_Hoare_Logic imports Gvf_Actions begin section \Syntax\ \ \Hoare triples for basic and conditional actions, respectively.\ datatype hoare_triple = htb (pre: \\<^sub>M) cap (post: \\<^sub>M) | htc \\<^sub>M cond_act \\<^sub>M \ \Is basic Hoare triple?\ fun is_htb_basic :: \hoare_triple \ bool\ where \is_htb_basic (htb _ (basic _) _) = True\ | \is_htb_basic _ = False\ \ \Unpack basic Hoare triple fields into an optional triple.\ fun htb_basic_unpack :: \hoare_triple \ (\\<^sub>M \ Bcap \ \\<^sub>M) option\ where \htb_basic_unpack (htb \ (basic n) \) = Some (\, n, \)\ | \htb_basic_unpack _ = None\ \ \Unpacking gives some value if the Hoare triple is for basic actions.\ lemma htb_unpack_some: \is_htb_basic ht = (\t. htb_basic_unpack ht = Some t)\ proof (induct) case (htb \ n \) then show ?case by (cases n) simp_all qed simp \ \Link first and third field of the unpacking triple to the selectors defined for the datatype.\ lemma unpack_sel: assumes \is_htb_basic ht\ shows \fst (the (htb_basic_unpack ht)) = pre ht\ and \snd (snd (the (htb_basic_unpack ht))) = post ht\ using assms htb_basic_unpack.simps(1) is_htb_basic.elims(2) option.sel by (metis fst_conv hoare_triple.sel(1), metis snd_conv hoare_triple.sel(3)) \ \Notation for Hoare triples.\ syntax "_hoare_triple_cond" :: \\\<^sub>M \ \\<^sub>M \ cap \ \\<^sub>M \ hoare_triple\ ("\<^bold>{_\<^bold>} _ \ do _ \<^bold>{_\<^bold>}") "_hoare_triple_basic" :: \\\<^sub>M \ cap \ \\<^sub>M \ hoare_triple\ ("\<^bold>{_\<^bold>} _ \<^bold>{_\<^bold>}") translations "\<^bold>{ \ \<^bold>} (\ \ do b) \<^bold>{ \ \<^bold>}" \ "hoare_triple.htc \ (\, b) \" "\<^bold>{ \ \<^bold>} a \<^bold>{ \ \<^bold>}" \ "hoare_triple.htb \ a \" \ \Start single_agent context block. Proofs, definitions etc. only apply for this context.\ context single_agent begin \ \Notation for mental state formula evaluated in state.\ \ \Mental state formulas with enabled.\ abbreviation mst_in_state\<^sub>E :: \\\<^sub>E \ trace \ nat \ bool\ (\_\<^bold>[_ _\<^bold>]\<^sub>E\) where \\\<^bold>[s i\<^bold>]\<^sub>E \ st_nth s i \\<^sub>E \\ \ \Mental state formulas without enabled.\ abbreviation mst_in_state\<^sub>M :: \\\<^sub>M \ trace \ nat \ bool\ (\_\<^bold>[_ _\<^bold>]\<^sub>M\) where \\\<^bold>[s i\<^bold>]\<^sub>M \ st_nth s i \\<^sub>M \\ section \Semantics\ \ \Semantics of Hoare triples is not evaluated in a specific state but for the agent.\ fun semantics\<^sub>H :: \hoare_triple \ bool\ (\\\<^sub>H\) where \ \Basic action. Precondition holds and enabled => evaluate in successor state; else same state.\ \\\<^sub>H \<^bold>{ \ \<^bold>} a \<^bold>{ \ \<^bold>} = (\M. mst_reachable_basic M \ (M \\<^sub>E (\\<^sup>E) \<^bold>\ (enabledb a) \ the (\ a M) \\<^sub>M \) \ (M \\<^sub>E (\\<^sup>E) \<^bold>\ \<^bold>\(enabledb a) \ M \\<^sub>M \))\ | \ \Conditional action. If \ \ do b is conditional action i in the trace and the precondition holds in state i, then the postcondition must hold in state i+1.\ \\\<^sub>H \<^bold>{ \ \<^bold>} (\ \ do b) \<^bold>{ \ \<^bold>} = (\ s \ Agent. \i. ((\\<^bold>[s i\<^bold>]\<^sub>M) \ (\ \ do b) = (act_nth s i) \ (\\<^bold>[s (i+1)\<^bold>]\<^sub>M)))\ \ \Example.\ lemma \\\<^sub>H \<^bold>{ p \<^bold>\ \<^bold>\ p \<^bold>} a \<^bold>{ p \<^bold>\ \<^bold>\ p \<^bold>}\ by simp lemma \\\<^sub>H \<^bold>{ p \<^bold>\ \<^bold>\ p \<^bold>} (\ \ do b) \<^bold>{ p \<^bold>\ \<^bold>\ p \<^bold>}\ by simp \ \Lemma 4.3: Hoare triple for conditional action from basic action.\ lemma hoare_triple_cond_from_basic: assumes \\\<^sub>H \<^bold>{ \ \<^bold>\ \ \<^bold>} a \<^bold>{ \' \<^bold>}\ and \\ s \ Agent. \i. st_nth s i \\<^sub>M (\ \<^bold>\ \<^bold>\\) \<^bold>\ \'\ shows \\\<^sub>H \<^bold>{ \ \<^bold>} (\ \ do a) \<^bold>{ \' \<^bold>}\ proof - have \\ s \ Agent. \i. (\\<^bold>[s i\<^bold>]\<^sub>M \ (\ \ do a) = (act_nth s i) \ (\'\<^bold>[s (i+1)\<^bold>]\<^sub>M))\ (is \\ s \ Agent. \i. (\\<^bold>[s i\<^bold>]\<^sub>M \ ?b = (act_nth s i) \ (\'\<^bold>[s (i+1)\<^bold>]\<^sub>M))\) proof fix s assume \s \ Agent\ then have \is_trace s\ unfolding Agent_def by simp show \\i. (\\<^bold>[s i\<^bold>]\<^sub>M \ ?b = act_nth s i \ (\'\<^bold>[s (i+1)\<^bold>]\<^sub>M))\ proof fix i let ?M = \st_nth s i\ and ?M' = \st_nth s (i+1)\ show \\\<^bold>[s i\<^bold>]\<^sub>M \ ?b = act_nth s i \ (\'\<^bold>[s (i+1)\<^bold>]\<^sub>M)\ proof assume \\\<^bold>[s i\<^bold>]\<^sub>M \ ?b = act_nth s i\ with conjunct2 have \?b = act_nth s i\ . show \\'\<^bold>[s (i+1)\<^bold>]\<^sub>M\ proof (cases \\\<^bold>[s i\<^bold>]\<^sub>M\) case \: True from \\\<^bold>[s i\<^bold>]\<^sub>M \ ?b = act_nth s i\ have \?M \\<^sub>M \\ by simp moreover from \ have \?M \\<^sub>M \\ by simp ultimately have \?M \\<^sub>E ((\ \<^bold>\ \)\<^sup>E)\ using transfer_semantics\<^sub>M by simp moreover from mst_reachable_basic_trace have \mst_reachable_basic ?M\ using \s \ Agent\ by auto then have \(?M \\<^sub>E ((\ \<^bold>\ \)\<^sup>E) \<^bold>\ (enabledb a) \ the (\ a ?M) \\<^sub>M \') \ (?M \\<^sub>E ((\ \<^bold>\ \)\<^sup>E) \<^bold>\ \<^bold>\(enabledb a) \ ?M \\<^sub>M \')\ using assms(1) semantics\<^sub>H.simps(1) by blast ultimately have *: \(?M \\<^sub>E (enabledb a) \ the (\ a ?M) \\<^sub>M \') \ (?M \\<^sub>E \<^bold>\(enabledb a) \ ?M \\<^sub>M \')\ by simp show ?thesis proof (cases \?M \\<^sub>E enabledb a\) case enabled: True with * have \the (\ a ?M) \\<^sub>M \'\ by simp moreover from enabled have \\ a ?M \ None\ by (cases a) simp_all ultimately show \?M' \\<^sub>M \'\ using \_suc_state \s \ Agent\ \?b = act_nth s i\ using snd_act_nth using \ fst_act_nth by auto next case not_enabled: False with * have \?M \\<^sub>M \'\ by simp moreover have \\ a ?M = None\ proof (cases a) case (basic n) with not_enabled have \\ ((enabledb (basic n))\<^bold>[s i\<^bold>]\<^sub>E)\ by simp with semantics\<^sub>E'.simps(3) have \\ \ (basic n) ?M \ None\ using \_some_Cap by fastforce then have \\ (basic n) ?M = None\ by blast with basic show ?thesis by simp next case (adopt \) with not_enabled have \\ \ (adopt \) ?M \ None\ using \?b = act_nth s i\ adopt_Cap by simp with adopt show ?thesis by blast next case (drop \) with not_enabled have \\ \ (drop \) ?M \ None\ using \?b = act_nth s i\ drop_Cap by simp with drop show ?thesis by blast qed then have \\(?M \?b ?M')\ unfolding transition_def by simp with \is_trace s\ \?b = act_nth s i\ have \?M' = ?M\ using not_transition_eq by metis ultimately show ?thesis by simp qed next case not_\: False then have \\ (?M \?b ?M')\ unfolding transition_def by simp with \is_trace s\ \?b = act_nth s i\ have \?M' = ?M\ using not_transition_eq by metis moreover from assms(2) have \?M \\<^sub>M (\ \<^bold>\ \<^bold>\\) \<^bold>\ \'\ using \s \ Agent\ by simp with not_\ \\\<^bold>[s i\<^bold>]\<^sub>M \ ?b = act_nth s i\ have \?M \\<^sub>M \'\ by simp ultimately show ?thesis by simp qed qed qed qed then show ?thesis by simp qed lemma mst_basic_trace_ht_invariant: assumes \\a\Cap. \\<^sub>H \<^bold>{ \ \<^bold>} a \<^bold>{ \ \<^bold>}\ and \M\<^sub>0 \\<^sub>M \\ shows \\<^bold>\\<^sub>M \\ proof fix M show \mst_reachable_basic M \ M \\<^sub>M \\ proof assume \mst_reachable_basic M\ then obtain S where *: \mst_basic_trace S M\<^sub>0 = M\ unfolding mst_reachable_basic_def by auto moreover have \mst_basic_trace S M\<^sub>0 \\<^sub>M \\ proof (induct S) case Nil with assms(2) show ?case by simp next case (Cons a S') let ?M = \mst_basic_trace S' M\<^sub>0\ show ?case proof (cases \\ a ?M\) case None with Cons show ?thesis by simp next case Some with \_some_Cap have \a \ Cap\ by blast with assms(1) have \?M \\<^sub>E (\\<^sup>E) \<^bold>\ (enabledb a) \ the (\ a ?M) \\<^sub>M \\ using mst_reachable_basic_def semantics\<^sub>H.simps(1) by blast moreover from semantics\<^sub>E'.simps(3) have \?M \\<^sub>E enabledb a\ using \a \ Cap\ Some * by simp ultimately have \?M \\<^sub>M \ \ the (\ a ?M) \\<^sub>M \\ using transfer_semantics\<^sub>M by simp with Cons have \the (\ a ?M) \\<^sub>M \\ by simp with Some show ?thesis by simp qed qed ultimately show \M \\<^sub>M \\ by simp qed qed end end