(* Title: Toy.thy License: BSD 2-Clause. See LICENSE. Author: Timothy Bourke Author: Peter Höfner *) section "Simple toy example" theory Toy imports Main AWN_Main Qmsg_Lifting begin subsection "Messages used in the protocol" datatype msg = Pkt data ip | Newpkt data ip instantiation msg :: msg begin definition newpkt_def [simp]: "newpkt \ \(d,did). Newpkt d did" definition eq_newpkt_def: "eq_newpkt m \ case m of Newpkt d did \ True | _ \ False" instance by intro_classes (simp add: eq_newpkt_def) end definition pkt :: "nat \ nat \ msg" where "pkt \ \(no, sid). Pkt no sid" lemma pkt_simp [simp]: "pkt(no, sid) = Pkt no sid" unfolding pkt_def by simp lemma not_eq_newpkt_pkt [simp]: "\eq_newpkt (Pkt no sid)" unfolding eq_newpkt_def by simp subsection "Protocol model" record state = id :: "nat" no :: "nat" nhid :: "nat" (* all locals *) msg :: "msg" num :: "nat" sid :: "nat" abbreviation toy_init :: "ip \ state" where "toy_init i \ \ id = i, no = 0, nhid = i, msg = (SOME x. True), num = (SOME x. True), sid = (SOME x. True) \" lemma some_neq_not_eq [simp]: "\((SOME x :: nat. x \ i) = i)" by (subst some_eq_ex) (metis zero_neq_numeral) definition clear_locals :: "state \ state" where "clear_locals \ = \ \ msg := (SOME x. True), num := (SOME x. True), sid := (SOME x. True) \" lemma clear_locals_but_not_globals [simp]: "id (clear_locals \) = id \" "no (clear_locals \) = no \" "nhid (clear_locals \) = nhid \" unfolding clear_locals_def by auto definition is_newpkt where "is_newpkt \ \ case msg \ of Newpkt data did \ { \\num := data\ } | _ \ {}" definition is_pkt where "is_pkt \ \ case msg \ of Pkt num' sid' \ { \\ num := num', sid := sid' \ } | _ \ {}" lemmas is_msg_defs = is_pkt_def is_newpkt_def lemma is_msg_inv_id [simp]: "\' \ is_pkt \ \ id \' = id \" "\' \ is_newpkt \ \ id \' = id \" unfolding is_msg_defs by (cases "msg \", clarsimp+)+ lemma is_msg_inv_sid [simp]: "\' \ is_newpkt \ \ sid \' = sid \" unfolding is_msg_defs by (cases "msg \", clarsimp+)+ lemma is_msg_inv_no [simp]: "\' \ is_pkt \ \ no \' = no \" "\' \ is_newpkt \ \ no \' = no \" unfolding is_msg_defs by (cases "msg \", clarsimp+)+ lemma is_msg_inv_nhid [simp]: "\' \ is_pkt \ \ nhid \' = nhid \" "\' \ is_newpkt \ \ nhid \' = nhid \" unfolding is_msg_defs by (cases "msg \", clarsimp+)+ lemma is_msg_inv_msg [simp]: "\' \ is_pkt \ \ msg \' = msg \" "\' \ is_newpkt \ \ msg \' = msg \" unfolding is_msg_defs by (cases "msg \", clarsimp+)+ datatype pseqp = PToy fun nat_of_seqp :: "pseqp \ nat" where "nat_of_seqp PToy = 1" instantiation "pseqp" :: ord begin definition less_eq_seqp [iff]: "l1 \ l2 = (nat_of_seqp l1 \ nat_of_seqp l2)" definition less_seqp [iff]: "l1 < l2 = (nat_of_seqp l1 < nat_of_seqp l2)" instance .. end abbreviation Toy where "Toy \ \_. \clear_locals\ call(PToy)" fun \\<^sub>T\<^sub>O\<^sub>Y :: "(state, msg, pseqp, pseqp label) seqp_env" where "\\<^sub>T\<^sub>O\<^sub>Y PToy = labelled PToy ( receive(\msg' \. \ \ msg := msg' \). \\. \ \nhid := id \\\ ( \is_newpkt\ ( \\. \ \no := max (no \) (num \)\\ broadcast(\\. pkt(no \, id \)). Toy() ) \ \is_pkt\ ( \\. num \ > no \\ \\. \ \no := num \\\ \\. \ \nhid := sid \\\ broadcast(\\. pkt(no \, id \)). Toy() \ \\. num \ \ no \\ Toy() ) ))" declare \\<^sub>T\<^sub>O\<^sub>Y.simps [simp del, code del] lemmas \\<^sub>T\<^sub>O\<^sub>Y_simps [simp, code] = \\<^sub>T\<^sub>O\<^sub>Y.simps [simplified] fun \\<^sub>T\<^sub>O\<^sub>Y_skeleton where "\\<^sub>T\<^sub>O\<^sub>Y_skeleton PToy = seqp_skeleton (\\<^sub>T\<^sub>O\<^sub>Y PToy)" lemma \\<^sub>T\<^sub>O\<^sub>Y_skeleton_wf [simp]: "wellformed \\<^sub>T\<^sub>O\<^sub>Y_skeleton" proof (rule, intro allI) fix pn pn' show "call(pn') \ stermsl (\\<^sub>T\<^sub>O\<^sub>Y_skeleton pn)" by (cases pn) simp_all qed declare \\<^sub>T\<^sub>O\<^sub>Y_skeleton.simps [simp del, code del] lemmas \\<^sub>T\<^sub>O\<^sub>Y_skeleton_simps [simp, code] = \\<^sub>T\<^sub>O\<^sub>Y_skeleton.simps [simplified \\<^sub>T\<^sub>O\<^sub>Y_simps seqp_skeleton.simps] lemma toy_proc_cases [dest]: fixes p pn assumes "p \ ctermsl (\\<^sub>T\<^sub>O\<^sub>Y pn)" shows "p \ ctermsl (\\<^sub>T\<^sub>O\<^sub>Y PToy)" using assms by (cases pn) simp_all definition \\<^sub>T\<^sub>O\<^sub>Y :: "ip \ (state \ (state, msg, pseqp, pseqp label) seqp) set" where "\\<^sub>T\<^sub>O\<^sub>Y i \ {(toy_init i, \\<^sub>T\<^sub>O\<^sub>Y PToy)}" abbreviation ptoy :: "ip \ (state \ (state, msg, pseqp, pseqp label) seqp, msg seq_action) automaton" where "ptoy i \ \ init = \\<^sub>T\<^sub>O\<^sub>Y i, trans = seqp_sos \\<^sub>T\<^sub>O\<^sub>Y \" lemma toy_trans: "trans (ptoy i) = seqp_sos \\<^sub>T\<^sub>O\<^sub>Y" by simp lemma toy_control_within [simp]: "control_within \\<^sub>T\<^sub>O\<^sub>Y (init (ptoy i))" unfolding \\<^sub>T\<^sub>O\<^sub>Y_def by (rule control_withinI) (auto simp del: \\<^sub>T\<^sub>O\<^sub>Y_simps) lemma toy_wf [simp]: "wellformed \\<^sub>T\<^sub>O\<^sub>Y" proof (rule, intro allI) fix pn pn' show "call(pn') \ stermsl (\\<^sub>T\<^sub>O\<^sub>Y pn)" by (cases pn) simp_all qed lemmas toy_labels_not_empty [simp] = labels_not_empty [OF toy_wf] lemma toy_ex_label [intro]: "\l. l\labels \\<^sub>T\<^sub>O\<^sub>Y p" by (metis toy_labels_not_empty all_not_in_conv) lemma toy_ex_labelE [elim]: assumes "\l\labels \\<^sub>T\<^sub>O\<^sub>Y p. P l p" and "\p l. P l p \ Q" shows "Q" using assms by (metis toy_ex_label) lemma toy_simple_labels [simp]: "simple_labels \\<^sub>T\<^sub>O\<^sub>Y" proof fix pn p assume "p\subterms(\\<^sub>T\<^sub>O\<^sub>Y pn)" thus "\!l. labels \\<^sub>T\<^sub>O\<^sub>Y p = {l}" by (cases pn) (simp_all cong: seqp_congs | elim disjE)+ qed lemma \\<^sub>T\<^sub>O\<^sub>Y_labels [simp]: "(\, p) \ \\<^sub>T\<^sub>O\<^sub>Y i \ labels \\<^sub>T\<^sub>O\<^sub>Y p = {PToy-:0}" unfolding \\<^sub>T\<^sub>O\<^sub>Y_def by simp text \By default, we no longer let the simplifier descend into process terms.\ declare seqp_congs [cong] (* configure the inv_cterms tactic *) declare \\<^sub>T\<^sub>O\<^sub>Y_simps [cterms_env] toy_proc_cases [ctermsl_cases] seq_invariant_ctermsI [OF toy_wf toy_control_within toy_simple_labels toy_trans, cterms_intros] seq_step_invariant_ctermsI [OF toy_wf toy_control_within toy_simple_labels toy_trans, cterms_intros] subsection "Define an open version of the protocol" definition \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y :: "((ip \ state) \ ((state, msg, pseqp, pseqp label) seqp)) set" where "\\<^sub>O\<^sub>T\<^sub>O\<^sub>Y \ {(toy_init, \\<^sub>T\<^sub>O\<^sub>Y PToy)}" abbreviation optoy :: "ip \ ((ip \ state) \ (state, msg, pseqp, pseqp label) seqp, msg seq_action) automaton" where "optoy i \ \ init = \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y, trans = oseqp_sos \\<^sub>T\<^sub>O\<^sub>Y i \" lemma initiali_toy [intro!, simp]: "initiali i (init (optoy i)) (init (ptoy i))" unfolding \\<^sub>T\<^sub>O\<^sub>Y_def \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y_def by rule simp_all lemma oaodv_control_within [simp]: "control_within \\<^sub>T\<^sub>O\<^sub>Y (init (optoy i))" unfolding \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y_def by (rule control_withinI) (auto simp del: \\<^sub>T\<^sub>O\<^sub>Y_simps) lemma \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y_labels [simp]: "(\, p) \ \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y \ labels \\<^sub>T\<^sub>O\<^sub>Y p = {PToy-:0}" unfolding \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y_def by simp lemma otoy_trans: "trans (optoy i) = oseqp_sos \\<^sub>T\<^sub>O\<^sub>Y i" by simp (* configure the inv_cterms tactic *) declare oseq_invariant_ctermsI [OF toy_wf oaodv_control_within toy_simple_labels otoy_trans, cterms_intros] oseq_step_invariant_ctermsI [OF toy_wf oaodv_control_within toy_simple_labels otoy_trans, cterms_intros] subsection "Predicates" definition msg_sender :: "msg \ ip" where "msg_sender m \ case m of Pkt _ ipc \ ipc" lemma msg_sender_simps [simp]: "\d did sid. msg_sender (Pkt d sid) = sid" unfolding msg_sender_def by simp_all abbreviation not_Pkt :: "msg \ bool" where "not_Pkt m \ case m of Pkt _ _ \ False | _ \ True" definition nos_inc :: "state \ state \ bool" where "nos_inc \ \' \ (no \ \ no \')" definition msg_ok :: "(ip \ state) \ msg \ bool" where "msg_ok \ m \ case m of Pkt num' sid' \ num' \ no (\ sid') | _ \ True" lemma msg_okI [intro]: assumes "\num' sid'. m = Pkt num' sid' \ num' \ no (\ sid')" shows "msg_ok \ m" using assms unfolding msg_ok_def by (auto split: msg.split) lemma msg_ok_Pkt [simp]: "msg_ok \ (Pkt data src) = (data \ no (\ src))" unfolding msg_ok_def by simp lemma msg_ok_pkt [simp]: "msg_ok \ (pkt(data, src)) = (data \ no (\ src))" unfolding msg_ok_def by simp lemma msg_ok_Newpkt [simp]: "msg_ok \ (Newpkt data dst)" unfolding msg_ok_def by simp lemma msg_ok_newpkt [simp]: "msg_ok \ (newpkt(data, dst))" unfolding msg_ok_def by simp subsection "Sequential Invariants" lemma seq_no_leq_num: "ptoy i \ onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). l\{PToy-:7..PToy-:8} \ no \ \ num \)" by inv_cterms lemma seq_nos_incs: "ptoy i \\<^sub>A onll \\<^sub>T\<^sub>O\<^sub>Y (\((\, _), _, (\', _)). nos_inc \ \')" unfolding nos_inc_def by (inv_cterms inv add: onl_invariant_sterms [OF toy_wf seq_no_leq_num]) lemma seq_nos_incs': "ptoy i \\<^sub>A (\((\, _), _, (\', _)). nos_inc \ \')" by (rule step_invariant_weakenE [OF seq_nos_incs]) (auto dest!: onllD) lemma sender_ip_valid: "ptoy i \\<^sub>A onll \\<^sub>T\<^sub>O\<^sub>Y (\((\, _), a, _). anycast (\m. msg_sender m = id \) a)" by inv_cterms lemma id_constant: "ptoy i \ onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, _). id \ = i)" by inv_cterms (simp add: \\<^sub>T\<^sub>O\<^sub>Y_def) lemma nhid_eq_id: "ptoy i \ onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). l\{PToy-:2..PToy-:8} \ nhid \ = id \)" by inv_cterms lemma seq_msg_ok: "ptoy i \\<^sub>A onll \\<^sub>T\<^sub>O\<^sub>Y (\((\, _), a, _). anycast (\m. case m of Pkt num' sid' \ num' = no \ \ sid' = i | _ \ True) a)" by (inv_cterms inv add: onl_invariant_sterms [OF toy_wf id_constant]) lemma nhid_eq_i: "ptoy i \ onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). l\{PToy-:2..PToy-:8} \ nhid \ = i)" proof (rule invariant_arbitraryI, clarify intro!: onlI impI) fix \ p l n assume "(\, p) \ reachable (ptoy i) TT" and "l \ labels \\<^sub>T\<^sub>O\<^sub>Y p" and "l \ {PToy-:2..PToy-:8}" from this(1-3) have "nhid \ = id \" by - (drule invariantD [OF nhid_eq_id], auto) moreover with \(\, p) \ reachable (ptoy i) TT\ and \l \ labels \\<^sub>T\<^sub>O\<^sub>Y p\ have "id \ = i" by (auto dest: invariantD [OF id_constant]) ultimately show "nhid \ = i" by simp qed subsection "Global Invariants" lemma nos_incD [dest]: assumes "nos_inc \ \'" shows "no \ \ no \'" using assms unfolding nos_inc_def . lemma nos_inc_simp [simp]: "nos_inc \ \' = (no \ \ no \')" unfolding nos_inc_def .. lemmas oseq_nos_incs = open_seq_step_invariant [OF seq_nos_incs initiali_toy otoy_trans toy_trans, simplified seqll_onll_swap] lemmas oseq_no_leq_num = open_seq_invariant [OF seq_no_leq_num initiali_toy otoy_trans toy_trans, simplified seql_onl_swap] lemma all_nos_inc: shows "optoy i \\<^sub>A (otherwith nos_inc {i} S, other nos_inc {i} \) onll \\<^sub>T\<^sub>O\<^sub>Y (\((\, _), a, (\', _)). (\j. nos_inc (\ j) (\' j)))" proof - have *: "\\ \' a. \ otherwith nos_inc {i} S \ \' a; no (\ i) \ no (\' i) \ \ \j. no (\ j) \ no (\' j)" by (auto dest!: otherwith_syncD) show ?thesis by (inv_cterms inv add: oseq_step_invariant_sterms [OF oseq_nos_incs [THEN oinvariant_step_anyact] toy_wf otoy_trans] simp add: seqllsimp) (auto elim!: *) qed lemma oreceived_msg_inv: assumes other: "\\ \' m. \ P \ m; other Q {i} \ \' \ \ P \' m" and local: "\\ m. P \ m \ P (\(i := \ i\msg := m\)) m" shows "optoy i \ (otherwith Q {i} (orecvmsg P), other Q {i} \) onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). l \ {PToy-:1} \ P \ (msg (\ i)))" proof (inv_cterms, intro impI) fix \ \' l assume "l = PToy-:1 \ P \ (msg (\ i))" and "l = PToy-:1" and "other Q {i} \ \'" from this(1-2) have "P \ (msg (\ i))" .. hence "P \' (msg (\ i))" using \other Q {i} \ \'\ by (rule other) moreover from \other Q {i} \ \'\ have "\' i = \ i" .. ultimately show "P \' (msg (\' i))" by simp next fix \ \' msg assume "otherwith Q {i} (orecvmsg P) \ \' (receive msg)" and "\' i = \ i\msg := msg\" from this(1) have "P \ msg" and "\j. j\i \ Q (\ j) (\' j)" by auto from this(1) have "P (\(i := \ i\msg := msg\)) msg" by (rule local) thus "P \' msg" proof (rule other) from \\' i = \ i\msg := msg\\ and \\j. j\i \ Q (\ j) (\' j)\ show "other Q {i} (\(i := \ i\msg := msg\)) \'" by - (rule otherI, auto) qed qed lemma msg_ok_other_nos_inc [elim]: assumes "msg_ok \ m" and "other nos_inc {i} \ \'" shows "msg_ok \' m" proof (cases m) fix num sid assume "m = Pkt num sid" with \msg_ok \ m\ have "num \ no (\ sid)" by simp also from \other nos_inc {i} \ \'\ have "no (\ sid) \ no (\' sid)" by (rule otherE) (metis eq_iff nos_incD) finally have "num \ no (\' sid)" . with \m = Pkt num sid\ show ?thesis by simp qed simp lemma msg_ok_no_leq_no [simp, elim]: assumes "msg_ok \ m" and "\j. no (\ j) \ no (\' j)" shows "msg_ok \' m" using assms(1) proof (cases m) fix num sid assume "m = Pkt num sid" with \msg_ok \ m\ have "num \ no (\ sid)" by simp also from \\j. no (\ j) \ no (\' j)\ have "no (\ sid) \ no (\' sid)" by simp finally have "num \ no (\' sid)" . with \m = Pkt num sid\ show ?thesis by simp qed (simp add: assms(1)) lemma oreceived_msg_ok: "optoy i \ (otherwith nos_inc {i} (orecvmsg msg_ok), other nos_inc {i} \) onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). l\{PToy-:1..} \ msg_ok \ (msg (\ i)))" (is "_ \ (?S, ?U \) _") proof (inv_cterms inv add: oseq_step_invariant_sterms [OF all_nos_inc toy_wf otoy_trans], intro impI, elim impE) fix \ \' assume "msg_ok \ (msg (\ i))" and other: "other nos_inc {i} \ \'" moreover from other have "msg (\' i) = msg (\ i)" by (clarsimp elim!: otherE) ultimately show "msg_ok \' (msg (\' i))" by (auto) next fix p l \ a q l' \' pp p' m assume a1: "(\', p') \ oreachable (optoy i) ?S ?U" and a2: "PToy-:1 \ labels \\<^sub>T\<^sub>O\<^sub>Y p'" and a3: "\' i = \ i\msg := m\" have inv: "optoy i \ (?S, ?U \) onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). l \ {PToy-:1} \ msg_ok \ (msg (\ i)))" proof (rule oreceived_msg_inv) fix \ \' m assume "msg_ok \ m" and "other nos_inc {i} \ \'" thus "msg_ok \' m" .. next fix \ m assume "msg_ok \ m" thus "msg_ok (\(i := \ i\msg := m\)) m" by (cases m) auto qed from a1 a2 a3 show "msg_ok \' m" by (clarsimp dest!: oinvariantD [OF inv] onlD) qed simp lemma is_pkt_handler_num_leq_no: shows "optoy i \ (otherwith nos_inc {i} (orecvmsg msg_ok), other nos_inc {i} \) onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). l\{PToy-:6..PToy-:10} \ num (\ i) \ no (\ (sid (\ i))))" proof - { fix \ \' assume "\j. no (\ j) \ no (\' j)" and "num (\ i) \ no (\ (sid (\ i)))" have "num (\ i) \ no (\' (sid (\ i)))" proof - note \num (\ i) \ no (\ (sid (\ i)))\ also from \\j. no (\ j) \ no (\' j)\ have "no (\ (sid (\ i))) \ no (\' (sid (\ i)))" by auto finally show ?thesis . qed } note solve_step = this show ?thesis proof (inv_cterms inv add: oseq_step_invariant_sterms [OF all_nos_inc toy_wf otoy_trans] onl_oinvariant_sterms [OF toy_wf oreceived_msg_ok] solve: solve_step, intro impI, elim impE) fix \ \' assume *: "num (\ i) \ no (\ (sid (\ i)))" and "other nos_inc {i} \ \'" from this(2) obtain "\i\{i}. \' i = \ i" and "\j. j \ {i} \ nos_inc (\ j) (\' j)" .. show "num (\' i) \ no (\' (sid (\' i)))" proof (cases "sid (\ i) = i") assume "sid (\ i) = i" with * \\i\{i}. \' i = \ i\ show ?thesis by simp next assume "sid (\ i) \ i" with \\j. j \ {i} \ nos_inc (\ j) (\' j)\ have "no (\ (sid (\ i))) \ no (\' (sid (\ i)))" by simp with * \\i\{i}. \' i = \ i\ show ?thesis by simp qed next fix p l \ a q l' \' pp p' assume "msg_ok \ (msg (\ i))" and "\j. no (\ j) \ no (\' j)" and "\' i \ is_pkt (\ i)" show "num (\' i) \ no (\' (sid (\' i)))" proof (cases "msg (\ i)") fix num' sid' assume "msg (\ i) = Pkt num' sid'" with \\' i \ is_pkt (\ i)\ obtain "num (\' i) = num'" and "sid (\' i) = sid'" unfolding is_pkt_def by auto with \msg (\ i) = Pkt num' sid'\ and \msg_ok \ (msg (\ i))\ have "num (\' i) \ no (\ (sid (\' i)))" by simp also from \\j. no (\ j) \ no (\' j)\ have "no (\ (sid (\' i))) \ no (\' (sid (\' i)))" .. finally show ?thesis . next fix num' sid' assume "msg (\ i) = Newpkt num' sid'" with \\' i \ is_pkt (\ i)\ have False unfolding is_pkt_def by simp thus ?thesis .. qed qed qed lemmas oseq_id_constant = open_seq_invariant [OF id_constant initiali_toy otoy_trans toy_trans, simplified seql_onl_swap] lemmas oseq_nhid_eq_i = open_seq_invariant [OF nhid_eq_i initiali_toy otoy_trans toy_trans, simplified seql_onl_swap] lemmas oseq_nhid_eq_id = open_seq_invariant [OF nhid_eq_id initiali_toy otoy_trans toy_trans, simplified seql_onl_swap] lemma oseq_bigger_than_next: shows "optoy i \ (otherwith nos_inc {i} (orecvmsg msg_ok), other nos_inc {i} \) global (\\. no (\ i) \ no (\ (nhid (\ i))))" (is "_ \ (?S, ?U \) ?P") proof - have nhidinv: "optoy i \ (?S, ?U \) onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). l\{PToy-:2..PToy-:8} \ nhid (\ i) = id (\ i))" by (rule oinvariant_weakenE [OF oseq_nhid_eq_id]) (auto simp: seqlsimp) have idinv: "optoy i \ (?S, ?U \) onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). id (\ i) = i)" by (rule oinvariant_weakenE [OF oseq_id_constant]) (auto simp: seqlsimp) { fix \ \' a assume "no (\ i) \ no (\ (nhid (\ i)))" and "\j. nos_inc (\ j) (\' j)" note this(1) also from \\j. nos_inc (\ j) (\' j)\ have "no (\ (nhid (\ i))) \ no (\' (nhid (\ i)))" by auto finally have "no (\ i) \ no (\' (nhid (\ i)))" .. } note * = this have "optoy i \ (otherwith nos_inc {i} (orecvmsg msg_ok), other nos_inc {i} \) onl \\<^sub>T\<^sub>O\<^sub>Y (\(\, l). no (\ i) \ no (\ (nhid (\ i))))" proof (inv_cterms inv add: onl_oinvariant_sterms [OF toy_wf oseq_no_leq_num [THEN oinvariant_anyact]] oseq_step_invariant_sterms [OF all_nos_inc toy_wf otoy_trans] onl_oinvariant_sterms [OF toy_wf is_pkt_handler_num_leq_no] onl_oinvariant_sterms [OF toy_wf nhidinv] onl_oinvariant_sterms [OF toy_wf idinv] simp add: seqlsimp seqllsimp simp del: nos_inc_simp solve: *) fix \ p l assume "(\, p) \ \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y" thus "no (\ i) \ no (\ (nhid (\ i)))" by (simp add: \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y_def) next fix \ \' p l assume or: "(\, p) \ oreachable (optoy i) ?S ?U" and "l \ labels \\<^sub>T\<^sub>O\<^sub>Y p" and "no (\ i) \ no (\ (nhid (\ i)))" and "other nos_inc {i} \ \'" show "no (\' i) \ no (\' (nhid (\' i)))" proof (cases "nhid (\' i) = i") assume "nhid (\' i) = i" with \no (\ i) \ no (\ (nhid (\ i)))\ show ?thesis by simp next assume "nhid (\' i) \ i" moreover from \other nos_inc {i} \ \'\ [THEN other_localD] have "\' i = \ i" by simp ultimately have "no (\ (nhid (\ i))) \ no (\' (nhid (\' i)))" using \other nos_inc {i} \ \'\ and \\' i = \ i\ by (auto) with \no (\ i) \ no (\ (nhid (\ i)))\ and \\' i = \ i\ show ?thesis by simp qed next fix p l \ a q l' \' pp p' assume "no (\ i) \ num (\ i)" and "num (\ i) \ no (\ (sid (\ i)))" and "\j. nos_inc (\ j) (\' j)" from this(1-2) have "no (\ i) \ no (\ (sid (\ i)))" by (rule le_trans) also from \\j. nos_inc (\ j) (\' j)\ have "no (\ (sid (\ i))) \ no (\' (sid (\ i)))" by auto finally show "no (\ i) \ no (\' (sid (\ i)))" .. qed thus ?thesis by (rule oinvariant_weakenE) (auto simp: onl_def) qed lemma anycast_weakenE [elim]: assumes "anycast P a" and "\m. P m \ Q m" shows "anycast Q a" using assms unfolding anycast_def by (auto split: seq_action.split) lemma oseq_msg_ok: "optoy i \\<^sub>A (act TT, other U {i} \) globala (\(\, a, _). anycast (msg_ok \) a)" by (rule ostep_invariant_weakenE [OF open_seq_step_invariant [OF seq_msg_ok initiali_toy otoy_trans toy_trans, simplified seql_onl_swap]]) (auto simp: seqllsimp dest!: onllD elim!: anycast_weakenE intro!: msg_okI) subsection "Lifting" lemma opar_bigger_than_next: shows "optoy i \\\<^bsub>i\<^esub> qmsg \ (otherwith nos_inc {i} (orecvmsg msg_ok), other nos_inc {i} \) global (\\. no (\ i) \ no (\ (nhid (\ i))))" proof (rule lift_into_qmsg [OF oseq_bigger_than_next]) fix \ \' m assume "\j. nos_inc (\ j) (\' j)" and "msg_ok \ m" from this(2) show "msg_ok \' m" proof (cases m, simp only: msg_ok_Pkt) fix num' sid' assume "num' \ no (\ sid')" also from \\j. nos_inc (\ j) (\' j)\ have "no (\ sid') \ no (\' sid')" by simp finally show "num' \ no (\' sid')" . qed simp next show "optoy i \\<^sub>A (otherwith nos_inc {i} (orecvmsg msg_ok), other nos_inc {i} \) globala (\(\, _, \'). nos_inc (\ i) (\' i))" by (rule ostep_invariant_weakenE [OF open_seq_step_invariant [OF seq_nos_incs initiali_toy otoy_trans toy_trans]]) (auto simp: seqllsimp dest!: onllD) qed simp lemma onode_bigger_than_next: "\i : optoy i \\\<^bsub>i\<^esub> qmsg : R\<^sub>i\\<^sub>o \ (otherwith nos_inc {i} (oarrivemsg msg_ok), other nos_inc {i} \) global (\\. no (\ i) \ no (\ (nhid (\ i))))" by (rule node_lift [OF opar_bigger_than_next]) lemma node_local_nos_inc: "\i : optoy i \\\<^bsub>i\<^esub> qmsg : R\<^sub>i\\<^sub>o \\<^sub>A (\\ _. oarrivemsg (\_ _. True) \, other (\_ _. True) {i} \) globala (\(\, _, \'). nos_inc (\ i) (\' i))" proof (rule node_lift_step_statelessassm) have "optoy i \\<^sub>A (\\ _. orecvmsg (\_ _. True) \, other (\_ _. True) {i} \) globala (\(\, _, \'). nos_inc (\ i) (\' i))" by (rule ostep_invariant_weakenE [OF oseq_nos_incs]) (auto simp: seqllsimp dest!: onllD) thus "optoy i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. orecvmsg (\_ _. True) \, other (\_ _. True) {i} \) globala (\(\, _, \'). nos_inc (\ i) (\' i))" by (rule lift_step_into_qmsg_statelessassm) auto qed simp lemma opnet_bigger_than_next: "opnet (\i. optoy i \\\<^bsub>i\<^esub> qmsg) n \ (otherwith nos_inc (net_tree_ips n) (oarrivemsg msg_ok), other nos_inc (net_tree_ips n) \) global (\\. \i\net_tree_ips n. no (\ i) \ no (\ (nhid (\ i))))" proof (rule pnet_lift [OF onode_bigger_than_next]) fix i R\<^sub>i have "\i : optoy i \\\<^bsub>i\<^esub> qmsg : R\<^sub>i\\<^sub>o \\<^sub>A (\\ _. oarrivemsg msg_ok \, other (\_ _. True) {i} \) globala (\(\, a, _). castmsg (msg_ok \) a)" proof (rule node_lift_anycast_statelessassm) have "optoy i \\<^sub>A (\\ _. orecvmsg (\_ _. True) \, other (\_ _. True) {i} \) globala (\(\, a, _). anycast (msg_ok \) a)" by (rule ostep_invariant_weakenE [OF oseq_msg_ok]) auto hence "optoy i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. orecvmsg (\_ _. True) \, other (\_ _. True) {i} \) globala (\(\, a, _). anycast (msg_ok \) a)" by (rule lift_step_into_qmsg_statelessassm) auto thus "optoy i \\\<^bsub>i\<^esub> qmsg \\<^sub>A (\\ _. orecvmsg msg_ok \, other (\_ _. True) {i} \) globala (\(\, a, _). anycast (msg_ok \) a)" by (rule ostep_invariant_weakenE) auto qed thus "\i : optoy i \\\<^bsub>i\<^esub> qmsg : R\<^sub>i\\<^sub>o \\<^sub>A (\\ _. oarrivemsg msg_ok \, other nos_inc {i} \) globala (\(\, a, _). castmsg (msg_ok \) a)" by (rule ostep_invariant_weakenE) auto next fix i R\<^sub>i show "\i : optoy i \\\<^bsub>i\<^esub> qmsg : R\<^sub>i\\<^sub>o \\<^sub>A (\\ _. oarrivemsg msg_ok \, other nos_inc {i} \) globala (\(\, a, \'). a \ \ \ (\d. a \ i:deliver(d)) \ nos_inc (\ i) (\' i))" by (rule ostep_invariant_weakenE [OF node_local_nos_inc]) auto next fix i R show "\i : optoy i \\\<^bsub>i\<^esub> qmsg : R\\<^sub>o \\<^sub>A (\\ _. oarrivemsg msg_ok \, other nos_inc {i} \) globala (\(\, a, \'). a = \ \ (\d. a = i:deliver(d)) \ nos_inc (\ i) (\' i))" by (rule ostep_invariant_weakenE [OF node_local_nos_inc]) auto qed simp_all lemma ocnet_bigger_than_next: "oclosed (opnet (\i. optoy i \\\<^bsub>i\<^esub> qmsg) n) \ (\_ _ _. True, other nos_inc (net_tree_ips n) \) global (\\. \i\net_tree_ips n. no (\ i) \ no (\ (nhid (\ i))))" proof (rule inclosed_closed) show "opnet (\i. optoy i \\\<^bsub>i\<^esub> qmsg) n \ (otherwith (=) (net_tree_ips n) inoclosed, other nos_inc (net_tree_ips n) \) global (\\. \i\net_tree_ips n. no (\ i) \ no (\ (nhid (\ i))))" proof (rule oinvariant_weakenE [OF opnet_bigger_than_next]) fix s s':: "nat \ state" and a :: "msg node_action" assume "otherwith (=) (net_tree_ips n) inoclosed s s' a" thus "otherwith nos_inc (net_tree_ips n) (oarrivemsg msg_ok) s s' a" proof (rule otherwithE, intro otherwithI) assume "inoclosed s a" and "\j. j \ net_tree_ips n \ s j = s' j" and "otherwith ((=)) (net_tree_ips n) inoclosed s s' a" thus "oarrivemsg msg_ok s a" by (cases a) auto qed auto qed simp qed subsection "Transfer" definition initmissing :: "(nat \ state option) \ 'a \ (nat \ state) \ 'a" where "initmissing \ = (\i. case (fst \) i of None \ toy_init i | Some s \ s, snd \)" lemma not_in_net_ips_fst_init_missing [simp]: assumes "i \ net_ips \" shows "fst (initmissing (netgmap fst \)) i = toy_init i" using assms unfolding initmissing_def by simp lemma fst_initmissing_netgmap_pair_fst [simp]: "fst (initmissing (netgmap (\(p, q). (fst (Fun.id p), snd (Fun.id p), q)) s)) = fst (initmissing (netgmap fst s))" unfolding initmissing_def by auto interpretation toy_openproc: openproc ptoy optoy Fun.id rewrites "toy_openproc.initmissing = initmissing" proof - show "openproc ptoy optoy Fun.id" proof unfold_locales fix i :: ip have "{(\, \). (\ i, \) \ \\<^sub>T\<^sub>O\<^sub>Y i \ (\j. j \ i \ \ j \ fst ` \\<^sub>T\<^sub>O\<^sub>Y j)} \ \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y" unfolding \\<^sub>T\<^sub>O\<^sub>Y_def \\<^sub>O\<^sub>T\<^sub>O\<^sub>Y_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 (ptoy i) \ (\ i, \) = Fun.id s \ (\j. j\i \ \ j \ (fst o Fun.id) ` init (ptoy j)) } \ init (optoy i)" by simp next show "\j. init (ptoy j) \ {}" unfolding \\<^sub>T\<^sub>O\<^sub>Y_def by simp next fix i s a s' \ \' assume "\ i = fst (Fun.id s)" and "\' i = fst (Fun.id s')" and "(s, a, s') \ trans (ptoy i)" then obtain q q' where "s = (\ i, q)" and "s' = (\' i, q')" and "((\ i, q), a, (\' i, q')) \ trans (ptoy i)" by (cases s, cases s') auto from this(3) have "((\, q), a, (\', q')) \ trans (optoy i)" by simp (rule open_seqp_action [OF toy_wf]) with \s = (\ i, q)\ and \s' = (\' i, q')\ show "((\, snd (Fun.id s)), a, (\', snd (Fun.id s'))) \ trans (optoy i)" by simp qed then interpret op0: openproc ptoy optoy Fun.id . have [simp]: "\i. (SOME x. x \ (fst o Fun.id) ` init (ptoy i)) = toy_init i" unfolding \\<^sub>T\<^sub>O\<^sub>Y_def by simp hence "\i. openproc.initmissing ptoy Fun.id i = initmissing i" unfolding op0.initmissing_def op0.someinit_def initmissing_def by (auto split: option.split) thus "openproc.initmissing ptoy Fun.id = initmissing" .. qed lemma fst_initmissing_netgmap_default_toy_init_netlift: "fst (initmissing (netgmap sr s)) = default toy_init (netlift sr s)" unfolding initmissing_def default_def by (simp add: fst_netgmap_netlift del: One_nat_def) definition netglobal :: "((nat \ state) \ bool) \ ((state \ 'b) \ 'c) net_state \ bool" where "netglobal P \ (\s. P (default toy_init (netlift fst s)))" interpretation toy_openproc_par_qmsg: openproc_parq ptoy optoy Fun.id qmsg rewrites "toy_openproc_par_qmsg.netglobal = netglobal" and "toy_openproc_par_qmsg.initmissing = initmissing" proof - show "openproc_parq ptoy optoy Fun.id qmsg" by (unfold_locales) simp then interpret opq: openproc_parq ptoy optoy Fun.id qmsg . have im: "\\. openproc.initmissing (\i. ptoy i \\ qmsg) (\(p, q). (fst (Fun.id p), snd (Fun.id p), q)) \ = initmissing \" unfolding opq.initmissing_def opq.someinit_def initmissing_def unfolding \\<^sub>T\<^sub>O\<^sub>Y_def \\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def by (clarsimp cong: option.case_cong) thus "openproc.initmissing (\i. ptoy i \\ qmsg) (\(p, q). (fst (Fun.id p), snd (Fun.id p), q)) = initmissing" by (rule ext) have "\P \. openproc.netglobal (\i. ptoy i \\ qmsg) (\(p, q). (fst (Fun.id p), snd (Fun.id p), q)) P \ = netglobal P \" unfolding opq.netglobal_def netglobal_def opq.initmissing_def initmissing_def opq.someinit_def unfolding \\<^sub>T\<^sub>O\<^sub>Y_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_toy_init_netlift [symmetric, unfolded initmissing_def]) thus "openproc.netglobal (\i. ptoy i \\ qmsg) (\(p, q). (fst (Fun.id p), snd (Fun.id p), q)) = netglobal" by auto qed subsection "Final result" lemma bigger_than_next: assumes "wf_net_tree n" shows "closed (pnet (\i. ptoy i \\ qmsg) n) \ netglobal (\\. \i. no (\ i) \ no (\ (nhid (\ i))))" (is "_ \ netglobal (\\. \i. ?inv \ i)") proof - from \wf_net_tree n\ have proto: "closed (pnet (\i. ptoy i \\ qmsg) n) \ netglobal (\\. \i\net_tree_ips n. no (\ i) \ no (\ (nhid (\ i))))" by (rule toy_openproc_par_qmsg.close_opnet [OF _ ocnet_bigger_than_next]) show ?thesis unfolding invariant_def opnet_sos.opnet_tau1 proof (rule, simp only: toy_openproc_par_qmsg.netglobalsimp fst_initmissing_netgmap_pair_fst, rule allI) fix \ i assume sr: "\ \ reachable (closed (pnet (\i. ptoy i \\ qmsg) n)) TT" hence "\i\net_tree_ips n. ?inv (fst (initmissing (netgmap fst \))) i" by - (drule invariantD [OF proto], simp only: toy_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. ptoy 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 = toy_init i" by simp thus ?thesis by simp qed metis qed qed end