(* Title: Aodv_Loop_Freedom.thy License: BSD 2-Clause. See LICENSE. Author: Timothy Bourke, Inria *) section "Lift and transfer invariants to show loop freedom" theory Aodv_Loop_Freedom imports AWN.OClosed_Transfer AWN.Qmsg_Lifting Global_Invariants Loop_Freedom begin subsection \Lift to parallel processes with queues\ lemma par_step_no_change_on_send_or_receive: fixes \ s a \' s' assumes "((\, s), a, (\', s')) \ oparp_sos i (oseqp_sos \\<^sub>A\<^sub>O\<^sub>D\<^sub>V i) (seqp_sos \\<^sub>Q\<^sub>M\<^sub>S\<^sub>G)" and "a \ \" shows "\' i = \ i" using assms by (rule qmsg_no_change_on_send_or_receive) lemma par_nhop_quality_increases: shows "opaodv i \\\<^bsub>i\<^esub> qmsg \ (otherwith ((=)) {i} (orecvmsg (\\ m. msg_fresh \ m \ msg_zhops m)), other quality_increases {i} \) global (\\. \dip. let nhip = the (nhop (rt (\ i)) dip) in dip \ vD (rt (\ i)) \ vD (rt (\ nhip)) \ nhip \ dip \ (rt (\ i)) \\<^bsub>dip\<^esub> (rt (\ nhip)))" proof (rule lift_into_qmsg [OF seq_nhop_quality_increases]) show "opaodv i \\<^sub>A (otherwith ((=)) {i} (orecvmsg (\\ m. msg_fresh \ m \ msg_zhops m)), other quality_increases {i} \) globala (\(\, _, \'). quality_increases (\ i) (\' i))" proof (rule ostep_invariant_weakenE [OF oquality_increases], simp_all) fix t :: "(((nat \ state) \ (state, msg, pseqp, pseqp label) seqp), msg seq_action) transition" assume "onll \\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\((\, _), _, (\', _)). \j. quality_increases (\ j) (\' j)) t" thus "quality_increases (fst (fst t) i) (fst (snd (snd t)) i)" by (cases t) (clarsimp dest!: onllD, metis aodv_ex_label) next fix \ \' a assume "otherwith ((=)) {i} (orecvmsg (\\ m. msg_fresh \ m \ msg_zhops m)) \ \' a" thus "otherwith quality_increases {i} (orecvmsg (\_. rreq_rrep_sn)) \ \' a" by - (erule weaken_otherwith, auto) qed qed auto lemma par_rreq_rrep_sn_quality_increases: "opaodv i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. orecvmsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(\, _, \'). quality_increases (\ i) (\' i))" proof - have "opaodv i \\<^sub>A (\\ _. orecvmsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(\, _, \'). quality_increases (\ i) (\' i))" by (rule ostep_invariant_weakenE [OF olocal_quality_increases]) (auto dest!: onllD seqllD elim!: aodv_ex_labelE) hence "opaodv i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. orecvmsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(\, _, \'). quality_increases (\ i) (\' i))" by (rule lift_step_into_qmsg_statelessassm) simp_all thus ?thesis by rule auto qed lemma par_rreq_rrep_nsqn_fresh_any_step: shows "opaodv i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. orecvmsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(\, a, \'). anycast (msg_fresh \) a)" proof - have "opaodv i \\<^sub>A (\\ _. (orecvmsg (\_. rreq_rrep_sn)) \, other (\_ _. True) {i} \) globala (\(\, a, \'). anycast (msg_fresh \) a)" proof (rule ostep_invariant_weakenE [OF rreq_rrep_nsqn_fresh_any_step_invariant]) fix t assume "onll \\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\((\, _), a, _). anycast (msg_fresh \) a) t" thus "globala (\(\, a, \'). anycast (msg_fresh \) a) t" by (cases t) (clarsimp dest!: onllD, metis aodv_ex_label) qed auto hence "opaodv i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. (orecvmsg (\_. rreq_rrep_sn)) \, other (\_ _. True) {i} \) globala (\(\, a, \'). anycast (msg_fresh \) a)" by (rule lift_step_into_qmsg_statelessassm) simp_all thus ?thesis by rule auto qed lemma par_anycast_msg_zhops: shows "opaodv i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. orecvmsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(_, a, _). anycast msg_zhops a)" proof - from anycast_msg_zhops initiali_aodv oaodv_trans aodv_trans have "opaodv i \\<^sub>A (act TT, other (\_ _. True) {i} \) seqll i (onll \\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\(_, a, _). anycast msg_zhops a))" by (rule open_seq_step_invariant) hence "opaodv i \\<^sub>A (\\ _. orecvmsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(_, a, _). anycast msg_zhops a)" proof (rule ostep_invariant_weakenE) fix t :: "(((nat \ state) \ (state, msg, pseqp, pseqp label) seqp), msg seq_action) transition" assume "seqll i (onll \\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\(_, a, _). anycast msg_zhops a)) t" thus "globala (\(_, a, _). anycast msg_zhops a) t" by (cases t) (clarsimp dest!: seqllD onllD, metis aodv_ex_label) qed simp_all hence "opaodv i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. orecvmsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(_, a, _). anycast msg_zhops a)" by (rule lift_step_into_qmsg_statelessassm) simp_all thus ?thesis by rule auto qed subsection \Lift to nodes\ lemma node_step_no_change_on_send_or_receive: assumes "((\, NodeS i P R), a, (\', NodeS i' P' R')) \ onode_sos (oparp_sos i (oseqp_sos \\<^sub>A\<^sub>O\<^sub>D\<^sub>V i) (seqp_sos \\<^sub>Q\<^sub>M\<^sub>S\<^sub>G))" and "a \ \" shows "\' i = \ i" using assms by (cases a) (auto elim!: par_step_no_change_on_send_or_receive) lemma node_nhop_quality_increases: shows "\ i : opaodv i \\\<^bsub>i\<^esub> qmsg : R \\<^sub>o \ (otherwith ((=)) {i} (oarrivemsg (\\ m. msg_fresh \ m \ msg_zhops m)), other quality_increases {i} \) global (\\. \dip. let nhip = the (nhop (rt (\ i)) dip) in dip \ vD (rt (\ i)) \ vD (rt (\ nhip)) \ nhip \ dip \ (rt (\ i)) \\<^bsub>dip\<^esub> (rt (\ nhip)))" by (rule node_lift [OF par_nhop_quality_increases]) auto lemma node_quality_increases: "\ i : opaodv i \\\<^bsub>i\<^esub> qmsg : R \\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(\, _, \'). quality_increases (\ i) (\' i))" by (rule node_lift_step_statelessassm [OF par_rreq_rrep_sn_quality_increases]) simp lemma node_rreq_rrep_nsqn_fresh_any_step: shows "\ i : opaodv i \\\<^bsub>i\<^esub> qmsg : R \\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(\, a, \'). castmsg (msg_fresh \) a)" by (rule node_lift_anycast_statelessassm [OF par_rreq_rrep_nsqn_fresh_any_step]) lemma node_anycast_msg_zhops: shows "\ i : opaodv i \\\<^bsub>i\<^esub> qmsg : R \\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(_, a, _). castmsg msg_zhops a)" by (rule node_lift_anycast_statelessassm [OF par_anycast_msg_zhops]) lemma node_silent_change_only: shows "\ i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\<^sub>i \\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\_ _. True) \, other (\_ _. True) {i} \) globala (\(\, a, \'). a \ \ \ \' i = \ i)" proof (rule ostep_invariantI, simp (no_asm), rule impI) fix \ \ a \' \' assume or: "(\, \) \ oreachable (\i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\<^sub>i\\<^sub>o) (\\ _. oarrivemsg (\_ _. True) \) (other (\_ _. True) {i})" and tr: "((\, \), a, (\', \')) \ trans (\i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\<^sub>i\\<^sub>o)" and "a \ \\<^sub>n" from or obtain p R where "\ = NodeS i p R" by - (drule node_net_state, metis) with tr have "((\, NodeS i p R), a, (\', \')) \ onode_sos (oparp_sos i (trans (opaodv i)) (trans qmsg))" by simp thus "\' i = \ i" using \a \ \\<^sub>n\ by (cases rule: onode_sos.cases) (auto elim: qmsg_no_change_on_send_or_receive) qed subsection \Lift to partial networks\ lemma arrive_rreq_rrep_nsqn_fresh_inc_sn [simp]: assumes "oarrivemsg (\\ m. msg_fresh \ m \ P \ m) \ m" shows "oarrivemsg (\_. rreq_rrep_sn) \ m" using assms by (cases m) auto lemma opnet_nhop_quality_increases: shows "opnet (\i. opaodv i \\\<^bsub>i\<^esub> qmsg) p \ (otherwith ((=)) (net_tree_ips p) (oarrivemsg (\\ m. msg_fresh \ m \ msg_zhops m)), other quality_increases (net_tree_ips p) \) global (\\. \i\net_tree_ips p. \dip. let nhip = the (nhop (rt (\ i)) dip) in dip \ vD (rt (\ i)) \ vD (rt (\ nhip)) \ nhip \ dip \ (rt (\ i)) \\<^bsub>dip\<^esub> (rt (\ nhip)))" proof (rule pnet_lift [OF node_nhop_quality_increases]) fix i R have "\i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\_. rreq_rrep_sn) \, other (\_ _. True) {i} \) globala (\(\, a, \'). castmsg (\m. msg_fresh \ m \ msg_zhops m) a)" proof (rule ostep_invariantI, simp (no_asm)) fix \ s a \' s' assume or: "(\, s) \ oreachable (\i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\\<^sub>o) (\\ _. oarrivemsg (\_. rreq_rrep_sn) \) (other (\_ _. True) {i})" and tr: "((\, s), a, (\', s')) \ trans (\i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\\<^sub>o)" and am: "oarrivemsg (\_. rreq_rrep_sn) \ a" from or tr am have "castmsg (msg_fresh \) a" by (auto dest!: ostep_invariantD [OF node_rreq_rrep_nsqn_fresh_any_step]) moreover from or tr am have "castmsg (msg_zhops) a" by (auto dest!: ostep_invariantD [OF node_anycast_msg_zhops]) ultimately show "castmsg (\m. msg_fresh \ m \ msg_zhops m) a" by (case_tac a) auto qed thus "\i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\\ m. msg_fresh \ m \ msg_zhops m) \, other quality_increases {i} \) globala (\(\, a, _). castmsg (\m. msg_fresh \ m \ msg_zhops m) a)" by rule auto next fix i R show "\i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\\ m. msg_fresh \ m \ msg_zhops m) \, other quality_increases {i} \) globala (\(\, a, \'). a \ \ \ (\d. a \ i:deliver(d)) \ \ i = \' i)" by (rule ostep_invariant_weakenE [OF node_silent_change_only]) auto next fix i R show "\i : opaodv i \\\<^bsub>i\<^esub> qmsg : R\\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\\ m. msg_fresh \ m \ msg_zhops m) \, other quality_increases {i} \) globala (\(\, a, \'). a = \ \ (\d. a = i:deliver(d)) \ quality_increases (\ i) (\' i))" by (rule ostep_invariant_weakenE [OF node_quality_increases]) auto qed simp_all subsection \Lift to closed networks\ lemma onet_nhop_quality_increases: shows "oclosed (opnet (\i. opaodv i \\\<^bsub>i\<^esub> qmsg) p) \ (\_ _ _. True, other quality_increases (net_tree_ips p) \) global (\\. \i\net_tree_ips p. \dip. let nhip = the (nhop (rt (\ i)) dip) in dip \ vD (rt (\ i)) \ vD (rt (\ nhip)) \ nhip \ dip \ (rt (\ i)) \\<^bsub>dip\<^esub> (rt (\ nhip)))" (is "_ \ (_, ?U \) ?inv") proof (rule inclosed_closed) from opnet_nhop_quality_increases show "opnet (\i. opaodv i \\\<^bsub>i\<^esub> qmsg) p \ (otherwith ((=)) (net_tree_ips p) inoclosed, ?U \) ?inv" proof (rule oinvariant_weakenE) fix \ \' :: "ip \ state" and a :: "msg node_action" assume "otherwith ((=)) (net_tree_ips p) inoclosed \ \' a" thus "otherwith ((=)) (net_tree_ips p) (oarrivemsg (\\ m. msg_fresh \ m \ msg_zhops m)) \ \' a" proof (rule otherwithEI) fix \ :: "ip \ state" and a :: "msg node_action" assume "inoclosed \ a" thus "oarrivemsg (\\ m. msg_fresh \ m \ msg_zhops m) \ a" proof (cases a) fix ii ni ms assume "a = ii\ni:arrive(ms)" moreover with \inoclosed \ a\ obtain d di where "ms = newpkt(d, di)" by (cases ms) auto ultimately show ?thesis by simp qed simp_all qed qed qed subsection \Transfer into the standard model\ interpretation aodv_openproc: openproc paodv opaodv id rewrites "aodv_openproc.initmissing = initmissing" proof - show "openproc paodv opaodv id" proof unfold_locales fix i :: ip have "{(\, \). (\ i, \) \ \\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \ (\j. j \ i \ \ j \ fst ` \\<^sub>A\<^sub>O\<^sub>D\<^sub>V j)} \ \\<^sub>A\<^sub>O\<^sub>D\<^sub>V'" unfolding \\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def proof (rule equalityD1) show "\f p. {(\, \). (\ i, \) \ {(f i, p)} \ (\j. j \ i \ \ j \ fst ` {(f j, p)})} = {(f, p)}" by (rule set_eqI) auto qed thus "{ (\, \) |\ \ s. s \ init (paodv i) \ (\ i, \) = id s \ (\j. j\i \ \ j \ (fst o id) ` init (paodv j)) } \ init (opaodv i)" by simp next show "\j. init (paodv j) \ {}" unfolding \\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp next fix i s a s' \ \' assume "\ i = fst (id s)" and "\' i = fst (id s')" and "(s, a, s') \ trans (paodv i)" then obtain q q' where "s = (\ i, q)" and "s' = (\' i, q')" and "((\ i, q), a, (\' i, q')) \ trans (paodv i)" by (cases s, cases s') auto from this(3) have "((\, q), a, (\', q')) \ trans (opaodv i)" by simp (rule open_seqp_action [OF aodv_wf]) with \s = (\ i, q)\ and \s' = (\' i, q')\ show "((\, snd (id s)), a, (\', snd (id s'))) \ trans (opaodv i)" by simp qed then interpret opn: openproc paodv opaodv id . have [simp]: "\i. (SOME x. x \ (fst o id) ` init (paodv i)) = aodv_init i" unfolding \\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp hence "\i. openproc.initmissing paodv id i = initmissing i" unfolding opn.initmissing_def opn.someinit_def initmissing_def by (auto split: option.split) thus "openproc.initmissing paodv id = initmissing" .. qed interpretation aodv_openproc_par_qmsg: openproc_parq paodv opaodv id qmsg rewrites "aodv_openproc_par_qmsg.netglobal = netglobal" and "aodv_openproc_par_qmsg.initmissing = initmissing" proof - show "openproc_parq paodv opaodv id qmsg" by (unfold_locales) simp then interpret opq: openproc_parq paodv opaodv id qmsg . have im: "\\. openproc.initmissing (\i. paodv i \\ qmsg) (\(p, q). (fst (id p), snd (id p), q)) \ = initmissing \" unfolding opq.initmissing_def opq.someinit_def initmissing_def unfolding \\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def by (clarsimp cong: option.case_cong) thus "openproc.initmissing (\i. paodv i \\ qmsg) (\(p, q). (fst (id p), snd (id p), q)) = initmissing" by (rule ext) have "\P \. openproc.netglobal (\i. paodv i \\ qmsg) (\(p, q). (fst (id p), snd (id p), q)) P \ = netglobal P \" unfolding opq.netglobal_def netglobal_def opq.initmissing_def initmissing_def opq.someinit_def unfolding \\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def by (clarsimp cong: option.case_cong simp del: One_nat_def simp add: fst_initmissing_netgmap_default_aodv_init_netlift [symmetric, unfolded initmissing_def]) thus "openproc.netglobal (\i. paodv i \\ qmsg) (\(p, q). (fst (id p), snd (id p), q)) = netglobal" by auto qed lemma net_nhop_quality_increases: assumes "wf_net_tree n" shows "closed (pnet (\i. paodv i \\ qmsg) n) \ netglobal (\\. \i dip. let nhip = the (nhop (rt (\ i)) dip) in dip \ vD (rt (\ i)) \ vD (rt (\ nhip)) \ nhip \ dip \ (rt (\ i)) \\<^bsub>dip\<^esub> (rt (\ nhip)))" (is "_ \ netglobal (\\. \i. ?inv \ i)") proof - from \wf_net_tree n\ have proto: "closed (pnet (\i. paodv i \\ qmsg) n) \ netglobal (\\. \i\net_tree_ips n. \dip. let nhip = the (nhop (rt (\ i)) dip) in dip \ vD (rt (\ i)) \ vD (rt (\ nhip)) \ nhip \ dip \ (rt (\ i)) \\<^bsub>dip\<^esub> (rt (\ nhip)))" by (rule aodv_openproc_par_qmsg.close_opnet [OF _ onet_nhop_quality_increases]) show ?thesis unfolding invariant_def opnet_sos.opnet_tau1 proof (rule, simp only: aodv_openproc_par_qmsg.netglobalsimp fst_initmissing_netgmap_pair_fst, rule allI) fix \ i assume sr: "\ \ reachable (closed (pnet (\i. paodv i \\ qmsg) n)) TT" hence "\i\net_tree_ips n. ?inv (fst (initmissing (netgmap fst \))) i" by - (drule invariantD [OF proto], simp only: aodv_openproc_par_qmsg.netglobalsimp fst_initmissing_netgmap_pair_fst) thus "?inv (fst (initmissing (netgmap fst \))) i" proof (cases "i\net_tree_ips n") assume "i\net_tree_ips n" from sr have "\ \ reachable (pnet (\i. paodv i \\ qmsg) n) TT" .. hence "net_ips \ = net_tree_ips n" .. with \i\net_tree_ips n\ have "i\net_ips \" by simp hence "(fst (initmissing (netgmap fst \))) i = aodv_init i" by simp thus ?thesis by simp qed metis qed qed subsection \Loop freedom of AODV\ theorem aodv_loop_freedom: assumes "wf_net_tree n" shows "closed (pnet (\i. paodv i \\ qmsg) n) \ netglobal (\\. \dip. irrefl ((rt_graph \ dip)\<^sup>+))" using assms by (rule aodv_openproc_par_qmsg.netglobal_weakenE [OF net_nhop_quality_increases inv_to_loop_freedom]) end