Zhangir Azerbayev commited on
Commit
4365a98
1 Parent(s): a581ece
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +1 -0
  2. fetch_books_and_formal.py +15 -1
  3. fetch_mathoverflow.py +23 -5
  4. formal/afp/ADS_Functor/ADS_Construction.thy +1281 -0
  5. formal/afp/ADS_Functor/Canton_Transaction_Tree.thy +518 -0
  6. formal/afp/ADS_Functor/Generic_ADS_Construction.thy +469 -0
  7. formal/afp/ADS_Functor/Inclusion_Proof_Construction.thy +430 -0
  8. formal/afp/ADS_Functor/Merkle_Interface.thy +299 -0
  9. formal/afp/ADS_Functor/document/root.tex +78 -0
  10. formal/afp/AI_Planning_Languages_Semantics/Error_Monad_Add.thy +52 -0
  11. formal/afp/AI_Planning_Languages_Semantics/Lifschitz_Consistency.thy +416 -0
  12. formal/afp/AI_Planning_Languages_Semantics/Option_Monad_Add.thy +101 -0
  13. formal/afp/AI_Planning_Languages_Semantics/PDDL_STRIPS_Checker.thy +406 -0
  14. formal/afp/AI_Planning_Languages_Semantics/PDDL_STRIPS_Semantics.thy +969 -0
  15. formal/afp/AI_Planning_Languages_Semantics/SASP_Checker.thy +348 -0
  16. formal/afp/AI_Planning_Languages_Semantics/SASP_Semantics.thy +228 -0
  17. formal/afp/AI_Planning_Languages_Semantics/document/root.tex +72 -0
  18. formal/afp/AODV/All.thy +16 -0
  19. formal/afp/AODV/Aodv.thy +535 -0
  20. formal/afp/AODV/Aodv_Basic.thy +44 -0
  21. formal/afp/AODV/Aodv_Data.thy +990 -0
  22. formal/afp/AODV/Aodv_Loop_Freedom.thy +369 -0
  23. formal/afp/AODV/Aodv_Message.thy +74 -0
  24. formal/afp/AODV/Aodv_Predicates.thy +136 -0
  25. formal/afp/AODV/Fresher.thy +798 -0
  26. formal/afp/AODV/Global_Invariants.thy +1151 -0
  27. formal/afp/AODV/Loop_Freedom.thy +123 -0
  28. formal/afp/AODV/OAodv.thy +47 -0
  29. formal/afp/AODV/Quality_Increases.thy +456 -0
  30. formal/afp/AODV/Seq_Invariants.thy +643 -0
  31. formal/afp/AODV/document/root.tex +70 -0
  32. formal/afp/AODV/variants/a_norreqid/A_Aodv.thy +532 -0
  33. formal/afp/AODV/variants/a_norreqid/A_Aodv_Data.thy +986 -0
  34. formal/afp/AODV/variants/a_norreqid/A_Aodv_Loop_Freedom.thy +369 -0
  35. formal/afp/AODV/variants/a_norreqid/A_Aodv_Message.thy +75 -0
  36. formal/afp/AODV/variants/a_norreqid/A_Aodv_Predicates.thy +137 -0
  37. formal/afp/AODV/variants/a_norreqid/A_Fresher.thy +799 -0
  38. formal/afp/AODV/variants/a_norreqid/A_Global_Invariants.thy +1159 -0
  39. formal/afp/AODV/variants/a_norreqid/A_Loop_Freedom.thy +123 -0
  40. formal/afp/AODV/variants/a_norreqid/A_Norreqid.thy +25 -0
  41. formal/afp/AODV/variants/a_norreqid/A_OAodv.thy +47 -0
  42. formal/afp/AODV/variants/a_norreqid/A_Quality_Increases.thy +457 -0
  43. formal/afp/AODV/variants/a_norreqid/A_Seq_Invariants.thy +643 -0
  44. formal/afp/AODV/variants/b_fwdrreps/B_Aodv.thy +532 -0
  45. formal/afp/AODV/variants/b_fwdrreps/B_Aodv_Data.thy +990 -0
  46. formal/afp/AODV/variants/b_fwdrreps/B_Aodv_Loop_Freedom.thy +369 -0
  47. formal/afp/AODV/variants/b_fwdrreps/B_Aodv_Message.thy +74 -0
  48. formal/afp/AODV/variants/b_fwdrreps/B_Aodv_Predicates.thy +136 -0
  49. formal/afp/AODV/variants/b_fwdrreps/B_Fresher.thy +799 -0
  50. formal/afp/AODV/variants/b_fwdrreps/B_Fwdrreps.thy +33 -0
.gitattributes CHANGED
@@ -49,3 +49,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
49
  *.jpg filter=lfs diff=lfs merge=lfs -text
50
  *.jpeg filter=lfs diff=lfs merge=lfs -text
51
  *.webp filter=lfs diff=lfs merge=lfs -text
 
 
49
  *.jpg filter=lfs diff=lfs merge=lfs -text
50
  *.jpeg filter=lfs diff=lfs merge=lfs -text
51
  *.webp filter=lfs diff=lfs merge=lfs -text
52
+ formal/setmm/set.mm filter=lfs diff=lfs merge=lfs -text
fetch_books_and_formal.py CHANGED
@@ -17,6 +17,18 @@ PROOFWIKI_URL = (
17
  "https://zenodo.org/record/4902289/files/naturalproofs_proofwiki.json?download=1"
18
  )
19
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  def _get_dir_from_repo(author, repo, sha, repo_dir, save_path, creds):
22
  """
@@ -313,6 +325,8 @@ def hol(testing=False):
313
  if os.path.isfile(f_path):
314
  os.remove(f_path)
315
 
 
 
316
  _delete_files_except_pattern(save_dir, r".*\.ml|.*\.doc")
317
 
318
 
@@ -560,7 +574,7 @@ def main():
560
  coq(creds)
561
  lean(creds)
562
  hol()
563
- cam()
564
 
565
 
566
  if __name__ == "__main__":
 
17
  "https://zenodo.org/record/4902289/files/naturalproofs_proofwiki.json?download=1"
18
  )
19
 
20
+ def check_encoding(path):
21
+ for f in os.listdir(path):
22
+ f_path = os.path.join(path, f)
23
+ if os.path.isfile(f_path):
24
+ with open(f_path, encoding="utf-8") as fle:
25
+ try:
26
+ fle.read()
27
+ except UnicodeDecodeError:
28
+ print(f"{f_path} is not unicode")
29
+ elif os.path.isdir(f_path):
30
+ check_encoding(f_path)
31
+
32
 
33
  def _get_dir_from_repo(author, repo, sha, repo_dir, save_path, creds):
34
  """
 
325
  if os.path.isfile(f_path):
326
  os.remove(f_path)
327
 
328
+ os.system("rm -r formal/hol/Proofrecording")
329
+
330
  _delete_files_except_pattern(save_dir, r".*\.ml|.*\.doc")
331
 
332
 
 
574
  coq(creds)
575
  lean(creds)
576
  hol()
577
+ #cam()
578
 
579
 
580
  if __name__ == "__main__":
fetch_mathoverflow.py CHANGED
@@ -24,6 +24,14 @@ a structured set of questions with answers.
24
  3. Run `questions()` and run it to get a dictionary of mathoverflow questions.
25
  Each question has an `Answers` field that contains a list of answers for the given q.
26
  """
 
 
 
 
 
 
 
 
27
 
28
  # source: https://meta.stackexchange.com/questions/2677/database-schema-documentation-for-the-public-data-dump-and-sede
29
  class PostType(Enum):
@@ -180,16 +188,26 @@ def get_and_format(url, save_dir):
180
  print("parsing xml...")
181
  qs = questions()
182
 
 
183
  qs_texts = [text_of_post(qs[key]) for key in tqdm(qs.keys())]
184
 
185
- for post, score, eyed, answered in tqdm(qs_texts):
186
- if score >= 5 and answered:
187
- with open(os.path.join(save_dir, str(eyed) + ".txt"), "w") as f:
188
- f.write(post)
 
 
 
 
 
 
 
189
 
190
  os.system(f"rm -r {DATA_DIR}")
191
  os.remove(archive_path)
192
 
193
  if __name__ == '__main__':
194
  get_and_format("https://archive.org/download/stackexchange/mathoverflow.net.7z",
195
- "stack_exchange/math_overflow")
 
 
 
24
  3. Run `questions()` and run it to get a dictionary of mathoverflow questions.
25
  Each question has an `Answers` field that contains a list of answers for the given q.
26
  """
27
+ def batch_loader(seq, size):
28
+ """
29
+ Iterator that takes in a list `seq` and returns
30
+ chunks of size `size`
31
+ """
32
+ return [seq[pos:pos + size] for pos in range(0, len(seq), size)]
33
+
34
+ DOC_SEP = "<|endoftext|>"
35
 
36
  # source: https://meta.stackexchange.com/questions/2677/database-schema-documentation-for-the-public-data-dump-and-sede
37
  class PostType(Enum):
 
188
  print("parsing xml...")
189
  qs = questions()
190
 
191
+ print("converting xml to text...")
192
  qs_texts = [text_of_post(qs[key]) for key in tqdm(qs.keys())]
193
 
194
+ batches = batch_loader(qs_texts, 5000)
195
+
196
+ for i, batch in tqdm(enumerate(batches)):
197
+ shard_path = os.path.join(save_dir, f"shard_{i}.txt")
198
+
199
+ to_cat = [post for post, score, _, answered in batch
200
+ if score >=5 and answered]
201
+ shard = f"{DOC_SEP}\n".join(to_cat)
202
+
203
+ with open(shard_path, "w") as f:
204
+ f.write(shard)
205
 
206
  os.system(f"rm -r {DATA_DIR}")
207
  os.remove(archive_path)
208
 
209
  if __name__ == '__main__':
210
  get_and_format("https://archive.org/download/stackexchange/mathoverflow.net.7z",
211
+ "stack-exchange/math_overflow")
212
+ get_and_format("https://archive.org/download/stackexchange/math.stackexchange.com.7z",
213
+ "stack-exchange/math_stack_exchange")
formal/afp/ADS_Functor/ADS_Construction.thy ADDED
@@ -0,0 +1,1281 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Author: Andreas Lochbihler, Digital Asset
2
+ Author: Ognjen Maric, Digital Asset *)
3
+
4
+ theory ADS_Construction imports
5
+ Merkle_Interface
6
+ "HOL-Library.Simps_Case_Conv"
7
+ begin
8
+
9
+ (************************************************************)
10
+ section \<open> Building blocks for authenticated data structures on datatypes \<close>
11
+ (************************************************************)
12
+
13
+ (************************************************************)
14
+ subsection \<open> Building Block: Identity Functor \<close>
15
+ (************************************************************)
16
+
17
+ text \<open>If nothing is blindable in a type, then the type itself is the hash and the ADS of itself.\<close>
18
+
19
+ abbreviation (input) hash_discrete :: "('a, 'a) hash" where "hash_discrete \<equiv> id"
20
+
21
+ abbreviation (input) blinding_of_discrete :: "'a blinding_of" where
22
+ "blinding_of_discrete \<equiv> (=)"
23
+
24
+ definition merge_discrete :: "'a merge" where
25
+ "merge_discrete x y = (if x = y then Some y else None)"
26
+
27
+ lemma blinding_of_discrete_hash:
28
+ "blinding_of_discrete \<le> vimage2p hash_discrete hash_discrete (=)"
29
+ by(auto simp add: vimage2p_def)
30
+
31
+ lemma blinding_of_on_discrete [locale_witness]:
32
+ "blinding_of_on UNIV hash_discrete blinding_of_discrete"
33
+ by(unfold_locales)(simp_all add: OO_eq eq_onp_def blinding_of_discrete_hash)
34
+
35
+ lemma merge_on_discrete [locale_witness]:
36
+ "merge_on UNIV hash_discrete blinding_of_discrete merge_discrete"
37
+ by unfold_locales(auto simp add: merge_discrete_def)
38
+
39
+ lemma merkle_discrete [locale_witness]:
40
+ "merkle_interface hash_discrete blinding_of_discrete merge_discrete"
41
+ ..
42
+
43
+ parametric_constant merge_discrete_parametric [transfer_rule]: merge_discrete_def
44
+
45
+ (************************************************************)
46
+ subsubsection \<open>Example: instantiation for @{typ unit}\<close>
47
+ (************************************************************)
48
+
49
+ abbreviation (input) hash_unit :: "(unit, unit) hash" where "hash_unit \<equiv> hash_discrete"
50
+
51
+ abbreviation blinding_of_unit :: "unit blinding_of" where
52
+ "blinding_of_unit \<equiv> blinding_of_discrete"
53
+
54
+ abbreviation merge_unit :: "unit merge" where "merge_unit \<equiv> merge_discrete"
55
+
56
+ lemma blinding_of_unit_hash:
57
+ "blinding_of_unit \<le> vimage2p hash_unit hash_unit (=)"
58
+ by(fact blinding_of_discrete_hash)
59
+
60
+ lemma blinding_of_on_unit:
61
+ "blinding_of_on UNIV hash_unit blinding_of_unit"
62
+ by(fact blinding_of_on_discrete)
63
+
64
+ lemma merge_on_unit:
65
+ "merge_on UNIV hash_unit blinding_of_unit merge_unit"
66
+ by(fact merge_on_discrete)
67
+
68
+ lemma merkle_interface_unit:
69
+ "merkle_interface hash_unit blinding_of_unit merge_unit"
70
+ by(intro merkle_interfaceI merge_on_unit)
71
+
72
+ (************************************************************)
73
+ subsection \<open> Building Block: Blindable Position \<close>
74
+ (************************************************************)
75
+
76
+ type_synonym 'a blindable = 'a
77
+
78
+ text \<open> The following type represents the hashes of a datatype. We model hashes as being injective,
79
+ but not surjective; some hashes do not correspond to any values of the original datatypes. We
80
+ model such values as "garbage" coming from a countable set (here, naturals). \<close>
81
+
82
+ type_synonym garbage = nat
83
+
84
+ datatype 'a\<^sub>h blindable\<^sub>h = Content 'a\<^sub>h | Garbage garbage
85
+
86
+ datatype ('a\<^sub>m, 'a\<^sub>h) blindable\<^sub>m = Unblinded 'a\<^sub>m | Blinded "'a\<^sub>h blindable\<^sub>h"
87
+
88
+ (************************************************************)
89
+ subsubsection \<open> Hashes \<close>
90
+ (************************************************************)
91
+
92
+ primrec hash_blindable' :: "(('a\<^sub>h, 'a\<^sub>h) blindable\<^sub>m, 'a\<^sub>h blindable\<^sub>h) hash" where
93
+ "hash_blindable' (Unblinded x) = Content x"
94
+ | "hash_blindable' (Blinded x) = x"
95
+
96
+ definition hash_blindable :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> (('a\<^sub>m, 'a\<^sub>h) blindable\<^sub>m, 'a\<^sub>h blindable\<^sub>h) hash" where
97
+ "hash_blindable h = hash_blindable' \<circ> map_blindable\<^sub>m h id"
98
+
99
+ lemma hash_blindable_simps [simp]:
100
+ "hash_blindable h (Unblinded x) = Content (h x)"
101
+ "hash_blindable h (Blinded y) = y"
102
+ by(simp_all add: hash_blindable_def blindable\<^sub>h.map_id)
103
+
104
+ lemma hash_map_blindable_simp:
105
+ "hash_blindable f (map_blindable\<^sub>m f' id x) = hash_blindable (f o f') x"
106
+ by(cases x) (simp_all add: hash_blindable_def blindable\<^sub>h.map_comp)
107
+
108
+ parametric_constant hash_blindable'_parametric [transfer_rule]: hash_blindable'_def
109
+
110
+ parametric_constant hash_blindable_parametric [transfer_rule]: hash_blindable_def
111
+
112
+ (************************************************************)
113
+ subsubsection \<open> Blinding \<close>
114
+ (************************************************************)
115
+
116
+ context
117
+ fixes h :: "('a\<^sub>m, 'a\<^sub>h) hash"
118
+ and bo :: "'a\<^sub>m blinding_of"
119
+ begin
120
+
121
+ inductive blinding_of_blindable :: "('a\<^sub>m, 'a\<^sub>h) blindable\<^sub>m blinding_of" where
122
+ "blinding_of_blindable (Unblinded x) (Unblinded y)" if "bo x y"
123
+ | "blinding_of_blindable (Blinded x) t" if "hash_blindable h t = x"
124
+
125
+ inductive_simps blinding_of_blindable_simps [simp]:
126
+ "blinding_of_blindable (Unblinded x) y"
127
+ "blinding_of_blindable (Blinded x) y"
128
+ "blinding_of_blindable z (Unblinded x)"
129
+ "blinding_of_blindable z (Blinded x)"
130
+
131
+ inductive_simps blinding_of_blindable_simps2:
132
+ "blinding_of_blindable (Unblinded x) (Unblinded y)"
133
+ "blinding_of_blindable (Unblinded x) (Blinded y')"
134
+ "blinding_of_blindable (Blinded x') (Unblinded y)"
135
+ "blinding_of_blindable (Blinded x') (Blinded y')"
136
+
137
+ end
138
+
139
+ lemma blinding_of_blindable_mono:
140
+ assumes "bo \<le> bo'"
141
+ shows "blinding_of_blindable h bo \<le> blinding_of_blindable h bo'"
142
+ apply(rule predicate2I)
143
+ apply(erule blinding_of_blindable.cases; hypsubst)
144
+ subgoal by(rule blinding_of_blindable.intros)(rule assms[THEN predicate2D])
145
+ subgoal by(rule blinding_of_blindable.intros) simp
146
+ done
147
+
148
+ lemma blinding_of_blindable_hash:
149
+ assumes "bo \<le> vimage2p h h (=)"
150
+ shows "blinding_of_blindable h bo \<le> vimage2p (hash_blindable h) (hash_blindable h) (=)"
151
+ apply(rule predicate2I vimage2pI)+
152
+ apply(erule blinding_of_blindable.cases; hypsubst)
153
+ subgoal using assms[THEN predicate2D] by(simp add: vimage2p_def)
154
+ subgoal by simp
155
+ done
156
+
157
+ lemma blinding_of_on_blindable [locale_witness]:
158
+ assumes "blinding_of_on A h bo"
159
+ shows "blinding_of_on {x. set1_blindable\<^sub>m x \<subseteq> A} (hash_blindable h) (blinding_of_blindable h bo)"
160
+ (is "blinding_of_on ?A ?h ?bo")
161
+ proof -
162
+ interpret blinding_of_on A h bo by fact
163
+ show ?thesis
164
+ proof
165
+ show "?bo \<le> vimage2p ?h ?h (=)"
166
+ by(rule blinding_of_blindable_hash)(rule hash)
167
+ show "?bo x x" if "x \<in> ?A" for x using that by(cases x)(auto simp add: refl)
168
+ show "?bo x z" if "?bo x y" "?bo y z" "x \<in> ?A" for x y z using that
169
+ by(auto elim!: blinding_of_blindable.cases dest: trans blinding_hash_eq)
170
+ show "x = y" if "?bo x y" "?bo y x" "x \<in> ?A" for x y using that
171
+ by(auto elim!: blinding_of_blindable.cases dest: antisym)
172
+ qed
173
+ qed
174
+
175
+ lemmas blinding_of_blindable [locale_witness] = blinding_of_on_blindable[of UNIV, simplified]
176
+
177
+ case_of_simps blinding_of_blindable_alt_def: blinding_of_blindable_simps2
178
+ parametric_constant blinding_of_blindable_parametric [transfer_rule]: blinding_of_blindable_alt_def
179
+
180
+ (************************************************************)
181
+ subsubsection \<open> Merging \<close>
182
+ (************************************************************)
183
+
184
+ context
185
+ fixes h :: "('a\<^sub>m, 'a\<^sub>h) hash"
186
+ fixes m :: "'a\<^sub>m merge"
187
+ begin
188
+
189
+ fun merge_blindable :: "('a\<^sub>m, 'a\<^sub>h) blindable\<^sub>m merge" where
190
+ "merge_blindable (Unblinded x) (Unblinded y) = map_option Unblinded (m x y)"
191
+ | "merge_blindable (Blinded x) (Unblinded y) = (if x = Content (h y) then Some (Unblinded y) else None)"
192
+ | "merge_blindable (Unblinded y) (Blinded x) = (if x = Content (h y) then Some (Unblinded y) else None)"
193
+ | "merge_blindable (Blinded t) (Blinded u) = (if t = u then Some (Blinded u) else None)"
194
+
195
+ lemma merge_on_blindable [locale_witness]:
196
+ assumes "merge_on A h bo m"
197
+ shows "merge_on {x. set1_blindable\<^sub>m x \<subseteq> A} (hash_blindable h) (blinding_of_blindable h bo) merge_blindable"
198
+ (is "merge_on ?A ?h ?bo ?m")
199
+ proof -
200
+ interpret merge_on A h bo m by fact
201
+ show ?thesis
202
+ proof
203
+ show "\<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)" if "?h a = ?h b" "a \<in> ?A" for a b
204
+ using that by(cases "(a, b)" rule: merge_blindable.cases)(auto simp add: refl dest!: join)
205
+ show "?m a b = None" if "?h a \<noteq> ?h b" "a \<in> ?A" for a b
206
+ using that by(cases "(a, b)" rule: merge_blindable.cases)(auto simp add: dest!: undefined)
207
+ qed
208
+ qed
209
+
210
+ lemmas merge_blindable [locale_witness] =
211
+ merge_on_blindable[of UNIV, simplified]
212
+
213
+ end
214
+
215
+ lemma merge_blindable_alt_def:
216
+ "merge_blindable h m x y = (case (x, y) of
217
+ (Unblinded x, Unblinded y) \<Rightarrow> map_option Unblinded (m x y)
218
+ | (Blinded x, Unblinded y) \<Rightarrow> (if Content (h y) = x then Some (Unblinded y) else None)
219
+ | (Unblinded y, Blinded x) \<Rightarrow> (if Content (h y) = x then Some (Unblinded y) else None)
220
+ | (Blinded t, Blinded u) \<Rightarrow> (if t = u then Some (Blinded u) else None))"
221
+ by(simp split: blindable\<^sub>m.split blindable\<^sub>h.split)
222
+
223
+ parametric_constant merge_blindable_parametric [transfer_rule]: merge_blindable_alt_def
224
+
225
+ lemma merge_blindable_cong [fundef_cong]:
226
+ assumes "\<And>a b. \<lbrakk> a \<in> set1_blindable\<^sub>m x; b \<in> set1_blindable\<^sub>m y \<rbrakk> \<Longrightarrow> m a b = m' a b"
227
+ shows "merge_blindable h m x y = merge_blindable h m' x y"
228
+ by(auto simp add: merge_blindable_alt_def split: blindable\<^sub>m.split intro: assms intro!: arg_cong[where f="map_option _"])
229
+
230
+ (************************************************************)
231
+ subsubsection \<open> Merkle interface \<close>
232
+ (************************************************************)
233
+
234
+ lemma merkle_blindable [locale_witness]:
235
+ assumes "merkle_interface h bo m"
236
+ shows "merkle_interface (hash_blindable h) (blinding_of_blindable h bo) (merge_blindable h m)"
237
+ proof -
238
+ interpret merge_on UNIV h bo m using assms by(simp add: merkle_interface_aux)
239
+ show ?thesis unfolding merkle_interface_aux ..
240
+ qed
241
+
242
+
243
+ (************************************************************)
244
+ subsubsection \<open> Non-recursive blindable positions \<close>
245
+ (************************************************************)
246
+
247
+ text \<open> For a non-recursive data type @{typ 'a}, the type of hashes in @{type blindable\<^sub>m} is fixed
248
+ to be simply @{typ "'a blindable\<^sub>h"}. We obtain this by instantiating the type variable with the
249
+ identity building block. \<close>
250
+
251
+ type_synonym 'a nr_blindable = "('a, 'a) blindable\<^sub>m"
252
+
253
+ abbreviation hash_nr_blindable :: "('a nr_blindable, 'a blindable\<^sub>h) hash" where
254
+ "hash_nr_blindable \<equiv> hash_blindable hash_discrete"
255
+
256
+ abbreviation blinding_of_nr_blindable :: "'a nr_blindable blinding_of" where
257
+ "blinding_of_nr_blindable \<equiv> blinding_of_blindable hash_discrete blinding_of_discrete"
258
+
259
+ abbreviation merge_nr_blindable :: "'a nr_blindable merge" where
260
+ "merge_nr_blindable \<equiv> merge_blindable hash_discrete merge_discrete"
261
+
262
+ lemma merge_on_nr_blindable:
263
+ "merge_on UNIV hash_nr_blindable blinding_of_nr_blindable merge_nr_blindable"
264
+ ..
265
+
266
+ lemma merkle_nr_blindable:
267
+ "merkle_interface hash_nr_blindable blinding_of_nr_blindable merge_nr_blindable"
268
+ ..
269
+
270
+ (************************************************************)
271
+ subsection \<open> Building block: Sums \<close>
272
+ (************************************************************)
273
+
274
+ text \<open> We prove that we can lift the ADS construction through sums.\<close>
275
+
276
+ type_synonym ('a\<^sub>h, 'b\<^sub>h) sum\<^sub>h = "'a\<^sub>h + 'b\<^sub>h"
277
+ type_notation sum\<^sub>h (infixr "+\<^sub>h" 10)
278
+
279
+ type_synonym ('a\<^sub>m, 'b\<^sub>m) sum\<^sub>m = "'a\<^sub>m + 'b\<^sub>m"
280
+ \<comment> \<open>If a functor does not introduce blindable positions, then we don't need the type variable copies.\<close>
281
+ type_notation sum\<^sub>m (infixr "+\<^sub>m" 10)
282
+
283
+ (************************************************************)
284
+ subsubsection \<open> Hashes \<close>
285
+ (************************************************************)
286
+
287
+ abbreviation (input) hash_sum' :: "('a\<^sub>h +\<^sub>h 'b\<^sub>h, 'a\<^sub>h +\<^sub>h 'b\<^sub>h) hash" where
288
+ "hash_sum' \<equiv> id"
289
+
290
+ abbreviation (input) hash_sum :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> ('a\<^sub>m +\<^sub>m 'b\<^sub>m, 'a\<^sub>h +\<^sub>h 'b\<^sub>h) hash"
291
+ where "hash_sum \<equiv> map_sum"
292
+
293
+ (************************************************************)
294
+ subsubsection \<open> Blinding \<close>
295
+ (************************************************************)
296
+
297
+ abbreviation (input) blinding_of_sum :: "'a\<^sub>m blinding_of \<Rightarrow> 'b\<^sub>m blinding_of \<Rightarrow> ('a\<^sub>m +\<^sub>m 'b\<^sub>m) blinding_of" where
298
+ "blinding_of_sum \<equiv> rel_sum"
299
+
300
+ lemmas blinding_of_sum_mono = sum.rel_mono
301
+
302
+ lemma blinding_of_sum_hash:
303
+ assumes "boa \<le> vimage2p rha rha (=)" "bob \<le> vimage2p rhb rhb (=)"
304
+ shows "blinding_of_sum boa bob \<le> vimage2p (hash_sum rha rhb) (hash_sum rha rhb) (=)"
305
+ using assms by(auto simp add: vimage2p_def elim!: rel_sum.cases)
306
+
307
+ lemma blinding_of_on_sum [locale_witness]:
308
+ assumes "blinding_of_on A rha boa" "blinding_of_on B rhb bob"
309
+ shows "blinding_of_on {x. setl x \<subseteq> A \<and> setr x \<subseteq> B} (hash_sum rha rhb) (blinding_of_sum boa bob)"
310
+ (is "blinding_of_on ?A ?h ?bo")
311
+ proof -
312
+ interpret a: blinding_of_on A rha boa by fact
313
+ interpret b: blinding_of_on B rhb bob by fact
314
+ show ?thesis
315
+ proof
316
+ show "?bo x x" if "x \<in> ?A" for x using that by(intro sum.rel_refl_strong)(auto intro: a.refl b.refl)
317
+ show "?bo x z" if "?bo x y" "?bo y z" "x \<in> ?A" for x y z
318
+ using that by(auto elim!: rel_sum.cases dest: a.trans b.trans)
319
+ show "x = y" if "?bo x y" "?bo y x" "x \<in> ?A" for x y
320
+ using that by(auto elim!: rel_sum.cases dest: a.antisym b.antisym)
321
+ qed(rule blinding_of_sum_hash a.hash b.hash)+
322
+ qed
323
+
324
+ lemmas blinding_of_sum [locale_witness] = blinding_of_on_sum[of UNIV _ _ UNIV, simplified]
325
+
326
+ (************************************************************)
327
+ subsubsection \<open> Merging \<close>
328
+ (************************************************************)
329
+
330
+ context
331
+ fixes ma :: "'a\<^sub>m merge"
332
+ fixes mb :: "'b\<^sub>m merge"
333
+ begin
334
+
335
+ fun merge_sum :: "('a\<^sub>m +\<^sub>m 'b\<^sub>m) merge" where
336
+ "merge_sum (Inl x) (Inl y) = map_option Inl (ma x y)"
337
+ | "merge_sum (Inr x) (Inr y) = map_option Inr (mb x y)"
338
+ | "merge_sum _ _ = None"
339
+
340
+ lemma merge_on_sum [locale_witness]:
341
+ assumes "merge_on A rha boa ma" "merge_on B rhb bob mb"
342
+ shows "merge_on {x. setl x \<subseteq> A \<and> setr x \<subseteq> B} (hash_sum rha rhb) (blinding_of_sum boa bob) merge_sum"
343
+ (is "merge_on ?A ?h ?bo ?m")
344
+ proof -
345
+ interpret a: merge_on A rha boa ma by fact
346
+ interpret b: merge_on B rhb bob mb by fact
347
+ show ?thesis
348
+ proof
349
+ show "\<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)"
350
+ if "?h a = ?h b" "a \<in> ?A" for a b using that
351
+ by(cases "(a, b)" rule: merge_sum.cases)(auto dest!: a.join b.join elim!: rel_sum.cases)
352
+ show "?m a b = None" if "?h a \<noteq> ?h b" "a \<in> ?A" for a b using that
353
+ by(cases "(a, b)" rule: merge_sum.cases)(auto dest!: a.undefined b.undefined)
354
+ qed
355
+ qed
356
+
357
+ lemmas merge_sum [locale_witness] = merge_on_sum[where A=UNIV and B=UNIV, simplified]
358
+
359
+ lemma merge_sum_alt_def:
360
+ "merge_sum x y = (case (x, y) of
361
+ (Inl x, Inl y) \<Rightarrow> map_option Inl (ma x y)
362
+ | (Inr x, Inr y) \<Rightarrow> map_option Inr (mb x y)
363
+ | _ \<Rightarrow> None)"
364
+ by(simp add: split: sum.split)
365
+
366
+ end
367
+
368
+ lemma merge_sum_cong[fundef_cong]:
369
+ "\<lbrakk> x = x'; y = y';
370
+ \<And>xl yl. \<lbrakk> x = Inl xl; y = Inl yl \<rbrakk> \<Longrightarrow> ma xl yl = ma' xl yl;
371
+ \<And>xr yr. \<lbrakk> x = Inr xr; y = Inr yr \<rbrakk> \<Longrightarrow> mb xr yr = mb' xr yr \<rbrakk> \<Longrightarrow>
372
+ merge_sum ma mb x y = merge_sum ma' mb' x' y'"
373
+ by(cases x; simp_all; cases y; auto)
374
+
375
+ parametric_constant merge_sum_parametric [transfer_rule]: merge_sum_alt_def
376
+
377
+ subsubsection \<open> Merkle interface \<close>
378
+
379
+ lemma merkle_sum [locale_witness]:
380
+ assumes "merkle_interface rha boa ma" "merkle_interface rhb bob mb"
381
+ shows "merkle_interface (hash_sum rha rhb) (blinding_of_sum boa bob) (merge_sum ma mb)"
382
+ proof -
383
+ interpret a: merge_on UNIV rha boa ma unfolding merkle_interface_aux[symmetric] by fact
384
+ interpret b: merge_on UNIV rhb bob mb unfolding merkle_interface_aux[symmetric] by fact
385
+ show ?thesis unfolding merkle_interface_aux[symmetric] ..
386
+ qed
387
+
388
+ (************************************************************)
389
+ subsection \<open> Building Block: Products\<close>
390
+ (************************************************************)
391
+
392
+ text \<open> We prove that we can lift the ADS construction through products.\<close>
393
+
394
+ type_synonym ('a\<^sub>h, 'b\<^sub>h) prod\<^sub>h = "'a\<^sub>h \<times> 'b\<^sub>h"
395
+ type_notation prod\<^sub>h ("(_ \<times>\<^sub>h/ _)" [21, 20] 20)
396
+
397
+ type_synonym ('a\<^sub>m, 'b\<^sub>m) prod\<^sub>m = "'a\<^sub>m \<times> 'b\<^sub>m"
398
+ \<comment> \<open>If a functor does not introduce blindable positions, then we don't need the type variable copies.\<close>
399
+ type_notation prod\<^sub>m ("(_ \<times>\<^sub>m/ _)" [21, 20] 20)
400
+
401
+ (************************************************************)
402
+ subsubsection \<open> Hashes \<close>
403
+ (************************************************************)
404
+
405
+ abbreviation (input) hash_prod' :: "('a\<^sub>h \<times>\<^sub>h 'b\<^sub>h, 'a\<^sub>h \<times>\<^sub>h 'b\<^sub>h) hash" where
406
+ "hash_prod' \<equiv> id"
407
+
408
+ abbreviation (input) hash_prod :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> ('a\<^sub>m \<times>\<^sub>m 'b\<^sub>m, 'a\<^sub>h \<times>\<^sub>h 'b\<^sub>h) hash"
409
+ where "hash_prod \<equiv> map_prod"
410
+
411
+ (************************************************************)
412
+ subsubsection \<open> Blinding \<close>
413
+ (************************************************************)
414
+
415
+ abbreviation (input) blinding_of_prod :: "'a\<^sub>m blinding_of \<Rightarrow> 'b\<^sub>m blinding_of \<Rightarrow> ('a\<^sub>m \<times>\<^sub>m 'b\<^sub>m) blinding_of" where
416
+ "blinding_of_prod \<equiv> rel_prod"
417
+
418
+ lemmas blinding_of_prod_mono = prod.rel_mono
419
+
420
+ lemma blinding_of_prod_hash:
421
+ assumes "boa \<le> vimage2p rha rha (=)" "bob \<le> vimage2p rhb rhb (=)"
422
+ shows "blinding_of_prod boa bob \<le> vimage2p (hash_prod rha rhb) (hash_prod rha rhb) (=)"
423
+ using assms by(auto simp add: vimage2p_def)
424
+
425
+ lemma blinding_of_on_prod [locale_witness]:
426
+ assumes "blinding_of_on A rha boa" "blinding_of_on B rhb bob"
427
+ shows "blinding_of_on {x. fsts x \<subseteq> A \<and> snds x \<subseteq> B} (hash_prod rha rhb) (blinding_of_prod boa bob)"
428
+ (is "blinding_of_on ?A ?h ?bo")
429
+ proof -
430
+ interpret a: blinding_of_on A rha boa by fact
431
+ interpret b: blinding_of_on B rhb bob by fact
432
+ show ?thesis
433
+ proof
434
+ show "?bo x x" if "x \<in> ?A" for x using that by(cases x)(auto intro: a.refl b.refl)
435
+ show "?bo x z" if "?bo x y" "?bo y z" "x \<in> ?A" for x y z using that
436
+ by(auto elim!: rel_prod.cases dest: a.trans b.trans)
437
+ show "x = y" if "?bo x y" "?bo y x" "x \<in> ?A" for x y using that
438
+ by(auto elim!: rel_prod.cases dest: a.antisym b.antisym)
439
+ qed(rule blinding_of_prod_hash a.hash b.hash)+
440
+ qed
441
+
442
+ lemmas blinding_of_prod [locale_witness] = blinding_of_on_prod[where A=UNIV and B=UNIV, simplified]
443
+
444
+ (************************************************************)
445
+ subsubsection \<open> Merging \<close>
446
+ (************************************************************)
447
+
448
+ context
449
+ fixes ma :: "'a\<^sub>m merge"
450
+ fixes mb :: "'b\<^sub>m merge"
451
+ begin
452
+
453
+ fun merge_prod :: "('a\<^sub>m \<times>\<^sub>m 'b\<^sub>m) merge" where
454
+ "merge_prod (x, y) (x', y') = Option.bind (ma x x') (\<lambda>x''. map_option (Pair x'') (mb y y'))"
455
+
456
+ lemma merge_on_prod [locale_witness]:
457
+ assumes "merge_on A rha boa ma" "merge_on B rhb bob mb"
458
+ shows "merge_on {x. fsts x \<subseteq> A \<and> snds x \<subseteq> B} (hash_prod rha rhb) (blinding_of_prod boa bob) merge_prod"
459
+ (is "merge_on ?A ?h ?bo ?m")
460
+ proof -
461
+ interpret a: merge_on A rha boa ma by fact
462
+ interpret b: merge_on B rhb bob mb by fact
463
+ show ?thesis
464
+ proof
465
+ show "\<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)"
466
+ if "?h a = ?h b" "a \<in> ?A" for a b using that
467
+ by(cases "(a, b)" rule: merge_prod.cases)(auto dest!: a.join b.join)
468
+ show "?m a b = None" if "?h a \<noteq> ?h b" "a \<in> ?A" for a b using that
469
+ by(cases "(a, b)" rule: merge_prod.cases)(auto dest!: a.undefined b.undefined)
470
+ qed
471
+ qed
472
+
473
+ lemmas merge_prod [locale_witness] = merge_on_prod[where A=UNIV and B=UNIV, simplified]
474
+
475
+ lemma merge_prod_alt_def:
476
+ "merge_prod = (\<lambda>(x, y) (x', y'). Option.bind (ma x x') (\<lambda>x''. map_option (Pair x'') (mb y y')))"
477
+ by(simp add: fun_eq_iff)
478
+
479
+ end
480
+
481
+ lemma merge_prod_cong[fundef_cong]:
482
+ assumes "\<And>a b. \<lbrakk> a \<in> fsts p1; b \<in> fsts p2 \<rbrakk> \<Longrightarrow> ma a b = ma' a b"
483
+ and "\<And>a b. \<lbrakk> a \<in> snds p1; b \<in> snds p2 \<rbrakk> \<Longrightarrow> mb a b = mb' a b"
484
+ shows "merge_prod ma mb p1 p2 = merge_prod ma' mb' p1 p2"
485
+ using assms by(cases p1; cases p2) auto
486
+
487
+ parametric_constant merge_prod_parametric [transfer_rule]: merge_prod_alt_def
488
+
489
+ (************************************************************)
490
+ subsubsection \<open> Merkle Interface \<close>
491
+ (************************************************************)
492
+
493
+ lemma merkle_product [locale_witness]:
494
+ assumes "merkle_interface rha boa ma" "merkle_interface rhb bob mb"
495
+ shows "merkle_interface (hash_prod rha rhb) (blinding_of_prod boa bob) (merge_prod ma mb)"
496
+ proof -
497
+ interpret a: merge_on UNIV rha boa ma unfolding merkle_interface_aux[symmetric] by fact
498
+ interpret b: merge_on UNIV rhb bob mb unfolding merkle_interface_aux[symmetric] by fact
499
+ show ?thesis unfolding merkle_interface_aux[symmetric] ..
500
+ qed
501
+
502
+
503
+ (************************************************************)
504
+ subsection \<open>Building Block: Lists\<close>
505
+ (************************************************************)
506
+
507
+ text \<open>The ADS construction on lists is done the easiest through a separate isomorphic datatype
508
+ that has only a single constructor. We hide this construction in a locale. \<close>
509
+
510
+ locale list_R1 begin
511
+
512
+ type_synonym ('a, 'b) list_F = "unit + 'a \<times> 'b"
513
+
514
+ abbreviation (input) "set_base_F\<^sub>m \<equiv> \<lambda>x. setr x \<bind> fsts"
515
+ abbreviation (input) "set_rec_F\<^sub>m \<equiv> \<lambda>A. setr A \<bind> snds"
516
+ abbreviation (input) "map_F \<equiv> \<lambda>fb fr. map_sum id (map_prod fb fr)"
517
+
518
+ datatype 'a list_R1 = list_R1 (unR: "('a, 'a list_R1) list_F")
519
+
520
+ lemma list_R1_const_into_dest: "list_R1 F = l \<longleftrightarrow> F = unR l"
521
+ by auto
522
+
523
+ declare list_R1.split[split]
524
+
525
+ lemma list_R1_induct[case_names list_R1]:
526
+ assumes "\<And>F. \<lbrakk> \<And>l'. l' \<in> set_rec_F\<^sub>m F \<Longrightarrow> P l' \<rbrakk> \<Longrightarrow> P (list_R1 F)"
527
+ shows "P l"
528
+ apply(rule list_R1.induct)
529
+ apply(auto intro!: assms)
530
+ done
531
+
532
+ lemma set_list_R1_eq:
533
+ "{x. set_base_F\<^sub>m x \<subseteq> A \<and> set_rec_F\<^sub>m x \<subseteq> B} =
534
+ {x. setl x \<subseteq> UNIV \<and> setr x \<subseteq> {x. fsts x \<subseteq> A \<and> snds x \<subseteq> B}}"
535
+ by(auto simp add: bind_UNION)
536
+
537
+ (************************************************************)
538
+ subsubsection \<open> The Isomorphism \<close>
539
+ (************************************************************)
540
+
541
+ primrec (transfer) list_R1_to_list :: "'a list_R1 \<Rightarrow> 'a list" where
542
+ "list_R1_to_list (list_R1 l) = (case map_sum id (map_prod id list_R1_to_list) l of Inl () \<Rightarrow> [] | Inr (x, xs) \<Rightarrow> x # xs)"
543
+
544
+ lemma list_R1_to_list_simps [simp]:
545
+ "list_R1_to_list (list_R1 (Inl ())) = []"
546
+ "list_R1_to_list (list_R1 (Inr (x, xs))) = x # list_R1_to_list xs"
547
+ by(simp_all split: unit.split)
548
+
549
+ declare list_R1_to_list.simps [simp del]
550
+
551
+ primrec (transfer) list_to_list_R1 :: "'a list \<Rightarrow> 'a list_R1" where
552
+ "list_to_list_R1 [] = list_R1 (Inl ())"
553
+ | "list_to_list_R1 (x#xs) = list_R1 (Inr (x, list_to_list_R1 xs))"
554
+
555
+ lemma R1_of_list: "list_R1_to_list (list_to_list_R1 x) = x"
556
+ by(induct x) (auto)
557
+
558
+ lemma list_of_R1: "list_to_list_R1 (list_R1_to_list x) = x"
559
+ apply(induct x)
560
+ subgoal for x
561
+ by(cases x) (auto)
562
+ done
563
+
564
+ lemma list_R1_def: "type_definition list_to_list_R1 list_R1_to_list UNIV"
565
+ by(unfold_locales)(auto intro: R1_of_list list_of_R1)
566
+
567
+ setup_lifting list_R1_def
568
+
569
+ lemma map_list_R1_list_to_list_R1: "map_list_R1 f (list_to_list_R1 xs) = list_to_list_R1 (map f xs)"
570
+ by(induction xs) auto
571
+
572
+ lemma list_R1_map_trans [transfer_rule]: includes lifting_syntax shows
573
+ "(((=) ===> (=)) ===> pcr_list (=) ===> pcr_list (=)) map_list_R1 map"
574
+ by(auto 4 3 simp add: list.pcr_cr_eq rel_fun_eq cr_list_def map_list_R1_list_to_list_R1)
575
+
576
+ lemma set_list_R1_list_to_list_R1: "set_list_R1 (list_to_list_R1 xs) = set xs"
577
+ by(induction xs) auto
578
+
579
+ lemma list_R1_set_trans [transfer_rule]: includes lifting_syntax shows
580
+ "(pcr_list (=) ===> (=)) set_list_R1 set"
581
+ by(auto simp add: list.pcr_cr_eq cr_list_def set_list_R1_list_to_list_R1)
582
+
583
+ lemma rel_list_R1_list_to_list_R1:
584
+ "rel_list_R1 R (list_to_list_R1 xs) (list_to_list_R1 ys) \<longleftrightarrow> list_all2 R xs ys"
585
+ (is "?lhs \<longleftrightarrow> ?rhs")
586
+ proof
587
+ define xs' and ys' where "xs' = list_to_list_R1 xs" and "ys' = list_to_list_R1 ys"
588
+ assume "rel_list_R1 R xs' ys'"
589
+ then have "list_all2 R (list_R1_to_list xs') (list_R1_to_list ys')"
590
+ by induction(auto elim!: rel_sum.cases)
591
+ thus ?rhs by(simp add: xs'_def ys'_def R1_of_list)
592
+ next
593
+ show ?lhs if ?rhs using that by induction auto
594
+ qed
595
+
596
+ lemma list_R1_rel_trans[transfer_rule]: includes lifting_syntax shows
597
+ "(((=) ===> (=) ===> (=)) ===> pcr_list (=) ===> pcr_list (=) ===> (=)) rel_list_R1 list_all2"
598
+ by(auto 4 4 simp add: list.pcr_cr_eq rel_fun_eq cr_list_def rel_list_R1_list_to_list_R1)
599
+
600
+ (************************************************************)
601
+ subsubsection \<open> Hashes \<close>
602
+ (************************************************************)
603
+
604
+ type_synonym ('a\<^sub>h, 'b\<^sub>h) list_F\<^sub>h = "unit +\<^sub>h 'a\<^sub>h \<times>\<^sub>h 'b\<^sub>h"
605
+
606
+ type_synonym ('a\<^sub>m, 'b\<^sub>m) list_F\<^sub>m = "unit +\<^sub>m 'a\<^sub>m \<times>\<^sub>m 'b\<^sub>m"
607
+
608
+ type_synonym 'a\<^sub>h list_R1\<^sub>h = "'a\<^sub>h list_R1"
609
+ \<comment> \<open>In theory, we should define a separate datatype here of the functor @{typ "('a\<^sub>h, _) list_F\<^sub>h"}.
610
+ We take a shortcut because they're isomorphic.\<close>
611
+
612
+ type_synonym 'a\<^sub>m list_R1\<^sub>m = "'a\<^sub>m list_R1"
613
+ \<comment> \<open>In theory, we should define a separate datatype here of the functor @{typ "('a\<^sub>m, _) list_F\<^sub>m"}.
614
+ We take a shortcut because they're isomorphic.\<close>
615
+
616
+ definition hash_F :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> (('a\<^sub>m, 'b\<^sub>m) list_F\<^sub>m, ('a\<^sub>h, 'b\<^sub>h) list_F\<^sub>h) hash" where
617
+ "hash_F h rhL = hash_sum hash_unit (hash_prod h rhL)"
618
+
619
+ abbreviation (input) hash_R1 :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> ('a\<^sub>m list_R1\<^sub>m, 'a\<^sub>h list_R1\<^sub>h) hash" where
620
+ "hash_R1 \<equiv> map_list_R1"
621
+
622
+ parametric_constant hash_F_parametric[transfer_rule]: hash_F_def
623
+
624
+ (************************************************************)
625
+ subsubsection \<open> Blinding \<close>
626
+ (************************************************************)
627
+
628
+ definition blinding_of_F :: "'a\<^sub>m blinding_of \<Rightarrow> 'b\<^sub>m blinding_of \<Rightarrow> ('a\<^sub>m, 'b\<^sub>m) list_F\<^sub>m blinding_of" where
629
+ "blinding_of_F bo bL = blinding_of_sum blinding_of_unit (blinding_of_prod bo bL)"
630
+
631
+ abbreviation (input) blinding_of_R1 :: "'a blinding_of \<Rightarrow> 'a list_R1 blinding_of" where
632
+ "blinding_of_R1 \<equiv> rel_list_R1"
633
+
634
+ lemma blinding_of_hash_R1:
635
+ assumes "bo \<le> vimage2p h h (=)"
636
+ shows "blinding_of_R1 bo \<le> vimage2p (hash_R1 h) (hash_R1 h) (=)"
637
+ apply(rule predicate2I vimage2pI)+
638
+ apply(auto simp add: predicate2D_vimage2p[OF assms] elim!: list_R1.rel_induct rel_sum.cases rel_prod.cases)
639
+ done
640
+
641
+ lemma blinding_of_on_R1 [locale_witness]:
642
+ assumes "blinding_of_on A h bo"
643
+ shows "blinding_of_on {x. set_list_R1 x \<subseteq> A} (hash_R1 h) (blinding_of_R1 bo)"
644
+ (is "blinding_of_on ?A ?h ?bo")
645
+ proof -
646
+ interpret a: blinding_of_on A h bo by fact
647
+ show ?thesis
648
+ proof
649
+ show hash: "?bo \<le> vimage2p ?h ?h (=)" using a.hash by(rule blinding_of_hash_R1)
650
+
651
+ have "?bo x x \<and> (?bo x y \<longrightarrow> ?bo y z \<longrightarrow> ?bo x z) \<and> (?bo x y \<longrightarrow> ?bo y x \<longrightarrow> x = y)" if "x \<in> ?A" for x y z using that
652
+ proof(induction x arbitrary: y z)
653
+ case (list_R1 x y' z')
654
+ from list_R1.prems have s1: "set_base_F\<^sub>m x \<subseteq> A" by(fastforce)
655
+ from list_R1.prems have s3: "set_rec_F\<^sub>m x \<bind> set_list_R1 \<subseteq> A" by(fastforce intro: rev_bexI)
656
+
657
+ interpret F: blinding_of_on "{y. set_base_F\<^sub>m y \<subseteq> A \<and> set_rec_F\<^sub>m y \<subseteq> set_rec_F\<^sub>m x}"
658
+ "hash_F h (hash_R1 h)" "blinding_of_F bo (blinding_of_R1 bo)"
659
+ unfolding hash_F_def blinding_of_F_def set_list_R1_eq
660
+ proof
661
+ let ?A' = "setr x \<bind> snds" and ?bo' = "rel_list_R1 bo"
662
+ show "?bo' x x" if "x \<in> ?A'" for x using that list_R1 by(force simp add: eq_onp_def)
663
+ show "?bo' x z" if "?bo' x y" "?bo' y z" "x \<in> ?A'" for x y z
664
+ using that list_R1.IH[of _ x y z] list_R1.prems
665
+ by(force simp add: bind_UNION prod_set_defs)
666
+ show "x = y" if "?bo' x y" "?bo' y x" "x \<in> ?A'" for x y
667
+ using that list_R1.IH[of _ x y] list_R1.prems
668
+ by(force simp add: prod_set_defs)
669
+ qed(rule hash)
670
+ show ?case using list_R1.prems
671
+ apply(intro conjI)
672
+ subgoal using F.refl[of x] s1 unfolding blinding_of_F_def by(auto intro: list_R1.rel_intros)
673
+ subgoal using s1 by(auto elim!: list_R1.rel_cases F.trans[unfolded blinding_of_F_def] intro: list_R1.rel_intros)
674
+ subgoal using s1 by(auto elim!: list_R1.rel_cases dest: F.antisym[unfolded blinding_of_F_def])
675
+ done
676
+ qed
677
+ then show "x \<in> ?A \<Longrightarrow> ?bo x x"
678
+ and "\<lbrakk> ?bo x y; ?bo y z; x \<in> ?A \<rbrakk> \<Longrightarrow> ?bo x z"
679
+ and "\<lbrakk> ?bo x y; ?bo y x; x \<in> ?A \<rbrakk> \<Longrightarrow> x = y"
680
+ for x y z by blast+
681
+ qed
682
+ qed
683
+
684
+ lemmas blinding_of_R1 [locale_witness] = blinding_of_on_R1[where A=UNIV, simplified]
685
+
686
+ parametric_constant blinding_of_F_parametric[transfer_rule]: blinding_of_F_def
687
+
688
+ (************************************************************)
689
+ subsubsection \<open> Merging \<close>
690
+ (************************************************************)
691
+
692
+ definition merge_F :: "'a\<^sub>m merge \<Rightarrow> 'b\<^sub>m merge \<Rightarrow> ('a\<^sub>m, 'b\<^sub>m) list_F\<^sub>m merge" where
693
+ "merge_F m mL = merge_sum merge_unit (merge_prod m mL)"
694
+
695
+ lemma merge_F_cong[fundef_cong]:
696
+ assumes "\<And>a b. \<lbrakk> a \<in> set_base_F\<^sub>m x; b \<in> set_base_F\<^sub>m y \<rbrakk> \<Longrightarrow> m a b = m' a b"
697
+ and "\<And>a b. \<lbrakk> a \<in> set_rec_F\<^sub>m x; b \<in> set_rec_F\<^sub>m y \<rbrakk> \<Longrightarrow> mL a b = mL' a b"
698
+ shows "merge_F m mL x y = merge_F m' mL' x y"
699
+ using assms
700
+ apply(cases x; cases y)
701
+ apply(simp_all add: merge_F_def)
702
+ apply(rule arg_cong[where f="map_option _"])
703
+ apply(blast intro: merge_prod_cong)
704
+ done
705
+
706
+ context
707
+ fixes m :: "'a\<^sub>m merge"
708
+ notes setr.simps[simp]
709
+ begin
710
+ fun merge_R1 :: "'a\<^sub>m list_R1\<^sub>m merge" where
711
+ "merge_R1 (list_R1 l1) (list_R1 l2) = map_option list_R1 (merge_F m merge_R1 l1 l2)"
712
+ end
713
+
714
+ case_of_simps merge_cases [simp]: merge_R1.simps
715
+
716
+ lemma merge_on_R1:
717
+ assumes "merge_on A h bo m"
718
+ shows "merge_on {x. set_list_R1 x \<subseteq> A } (hash_R1 h) (blinding_of_R1 bo) (merge_R1 m)"
719
+ (is "merge_on ?A ?h ?bo ?m")
720
+ proof -
721
+ interpret a: merge_on A h bo m by fact
722
+ show ?thesis
723
+ proof
724
+ have "(?h a = ?h b \<longrightarrow> (\<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u))) \<and>
725
+ (?h a \<noteq> ?h b \<longrightarrow> ?m a b = None)"
726
+ if "a \<in> ?A" for a b using that unfolding mem_Collect_eq
727
+ proof(induction a arbitrary: b rule: list_R1_induct)
728
+ case wfInd: (list_R1 l)
729
+ interpret merge_on "{y. set_base_F\<^sub>m y \<subseteq> A \<and> set_rec_F\<^sub>m y \<subseteq> set_rec_F\<^sub>m l}"
730
+ "hash_F h ?h" "blinding_of_F bo ?bo" "merge_F m ?m"
731
+ unfolding set_list_R1_eq hash_F_def merge_F_def blinding_of_F_def
732
+ proof
733
+ fix a
734
+ assume a: "a \<in> set_rec_F\<^sub>m l"
735
+ with wfInd.prems have a': "set_list_R1 a \<subseteq> A"
736
+ by fastforce
737
+
738
+ show "hash_R1 h a = hash_R1 h b
739
+ \<Longrightarrow> \<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and>
740
+ (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)"
741
+ and "?h a \<noteq> ?h b \<Longrightarrow> ?m a b = None" for b
742
+ using wfInd.IH[OF a a', rule_format, of b]
743
+ by(auto dest: sym)
744
+ qed
745
+ show ?case using wfInd.prems
746
+ apply(intro conjI strip)
747
+ subgoal
748
+ by(auto 4 4 dest!: join[unfolded hash_F_def]
749
+ simp add: blinding_of_F_def UN_subset_iff list_R1.rel_sel)
750
+ subgoal by(auto 4 3 intro!: undefined[simplified hash_F_def])
751
+ done
752
+ qed
753
+ then show
754
+ "?h a = ?h b \<Longrightarrow> \<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)"
755
+ "?h a \<noteq> ?h b \<Longrightarrow> ?m a b = None"
756
+ if "a \<in> ?A" for a b using that by blast+
757
+ qed
758
+ qed
759
+
760
+ lemmas merge_R1 [locale_witness] = merge_on_R1[where A=UNIV, simplified]
761
+
762
+ lemma merkle_list_R1 [locale_witness]:
763
+ assumes "merkle_interface h bo m"
764
+ shows "merkle_interface (hash_R1 h) (blinding_of_R1 bo) (merge_R1 m)"
765
+ proof -
766
+ interpret merge_on UNIV h bo m using assms by(unfold merkle_interface_aux)
767
+ show ?thesis unfolding merkle_interface_aux[symmetric] ..
768
+ qed
769
+
770
+ lemma merge_R1_cong [fundef_cong]:
771
+ assumes "\<And>a b. \<lbrakk> a \<in> set_list_R1 x; b \<in> set_list_R1 y \<rbrakk> \<Longrightarrow> m a b = m' a b"
772
+ shows "merge_R1 m x y = merge_R1 m' x y"
773
+ using assms
774
+ apply(induction x y rule: merge_R1.induct)
775
+ apply(simp del: merge_cases)
776
+ apply(rule arg_cong[where f="map_option _"])
777
+ apply(blast intro: merge_F_cong[unfolded bind_UNION])
778
+ done
779
+
780
+ parametric_constant merge_F_parametric[transfer_rule]: merge_F_def
781
+
782
+ lemma merge_R1_parametric [transfer_rule]:
783
+ includes lifting_syntax
784
+ notes [simp del] = merge_cases
785
+ assumes [transfer_rule]: "bi_unique A"
786
+ shows "((A ===> A ===> rel_option A) ===> rel_list_R1 A ===> rel_list_R1 A ===> rel_option (rel_list_R1 A))
787
+ merge_R1 merge_R1"
788
+ apply(intro rel_funI)
789
+ subgoal premises prems [transfer_rule] for m1 m2 xs1 xs2 ys1 ys2 using prems(2, 3)
790
+ apply(induction xs1 ys1 arbitrary: xs2 ys2 rule: merge_R1.induct)
791
+ apply(elim list_R1.rel_cases rel_sum.cases; clarsimp simp add: option.rel_map merge_F_def merge_discrete_def)
792
+ apply(elim meta_allE; (erule meta_impE, simp)+)
793
+ subgoal premises [transfer_rule] by transfer_prover
794
+ done
795
+ done
796
+
797
+ end
798
+
799
+ subsubsection \<open> Transferring the Constructions to Lists \<close>
800
+ type_synonym 'a\<^sub>h list\<^sub>h = "'a\<^sub>h list"
801
+ type_synonym 'a\<^sub>m list\<^sub>m = "'a\<^sub>m list"
802
+
803
+ context begin
804
+ interpretation list_R1 .
805
+
806
+ abbreviation (input) hash_list :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> ('a\<^sub>m list\<^sub>m, 'a\<^sub>h list\<^sub>h) hash"
807
+ where "hash_list \<equiv> map"
808
+ abbreviation (input) blinding_of_list :: "'a\<^sub>m blinding_of \<Rightarrow> 'a\<^sub>m list\<^sub>m blinding_of"
809
+ where "blinding_of_list \<equiv> list_all2"
810
+ lift_definition merge_list :: "'a\<^sub>m merge \<Rightarrow> 'a\<^sub>m list\<^sub>m merge" is merge_R1 .
811
+
812
+ lemma blinding_of_list_mono:
813
+ "\<lbrakk> \<And>x y. bo x y \<longrightarrow> bo' x y \<rbrakk> \<Longrightarrow>
814
+ blinding_of_list bo x y \<longrightarrow> blinding_of_list bo' x y"
815
+ by (transfer) (blast intro: list_R1.rel_mono_strong)
816
+
817
+ lemmas blinding_of_list_hash = blinding_of_hash_R1[Transfer.transferred]
818
+ and blinding_of_on_list [locale_witness] = blinding_of_on_R1[Transfer.transferred]
819
+ and blinding_of_list [locale_witness] = blinding_of_R1[Transfer.transferred]
820
+ and merge_on_list [locale_witness] = merge_on_R1[Transfer.transferred]
821
+ and merge_list [locale_witness] = merge_R1[Transfer.transferred]
822
+ and merge_list_cong = merge_R1_cong[Transfer.transferred]
823
+
824
+ lemma blinding_of_list_mono_pred:
825
+ "R \<le> R' \<Longrightarrow> blinding_of_list R \<le> blinding_of_list R'"
826
+ by(transfer) (rule list_R1.rel_mono)
827
+
828
+ lemma blinding_of_list_simp: "blinding_of_list = list_all2"
829
+ by(transfer) (rule refl)
830
+
831
+ lemma merkle_list [locale_witness]:
832
+ assumes [locale_witness]: "merkle_interface h bo m"
833
+ shows "merkle_interface (hash_list h) (blinding_of_list bo) (merge_list m)"
834
+ by(transfer fixing: h bo m) unfold_locales
835
+
836
+ parametric_constant merge_list_parametric [transfer_rule]: merge_list_def
837
+
838
+ lifting_update list.lifting
839
+ lifting_forget list.lifting
840
+
841
+ end
842
+
843
+
844
+ (************************************************************)
845
+ subsection \<open>Building block: function space\<close>
846
+ (************************************************************)
847
+
848
+ text \<open> We prove that we can lift the ADS construction through functions.\<close>
849
+
850
+ type_synonym ('a, 'b\<^sub>h) fun\<^sub>h = "'a \<Rightarrow> 'b\<^sub>h"
851
+ type_notation fun\<^sub>h (infixr "\<Rightarrow>\<^sub>h" 0)
852
+
853
+ type_synonym ('a, 'b\<^sub>m) fun\<^sub>m = "'a \<Rightarrow> 'b\<^sub>m"
854
+ type_notation fun\<^sub>m (infixr "\<Rightarrow>\<^sub>m" 0)
855
+
856
+ (************************************************************)
857
+ subsubsection \<open> Hashes \<close>
858
+ (************************************************************)
859
+
860
+ text \<open> Only the range is live, the domain is dead like for BNFs. \<close>
861
+
862
+ abbreviation (input) hash_fun' :: "('a \<Rightarrow>\<^sub>m 'b\<^sub>h, 'a \<Rightarrow>\<^sub>h 'b\<^sub>h) hash" where
863
+ "hash_fun' \<equiv> id"
864
+
865
+ abbreviation (input) hash_fun :: "('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> ('a \<Rightarrow>\<^sub>m 'b\<^sub>m, 'a \<Rightarrow>\<^sub>h 'b\<^sub>h) hash"
866
+ where "hash_fun \<equiv> comp"
867
+
868
+ (************************************************************)
869
+ subsubsection \<open> Blinding \<close>
870
+ (************************************************************)
871
+
872
+ abbreviation (input) blinding_of_fun :: "'b\<^sub>m blinding_of \<Rightarrow> ('a \<Rightarrow>\<^sub>m 'b\<^sub>m) blinding_of" where
873
+ "blinding_of_fun \<equiv> rel_fun (=)"
874
+
875
+ lemmas blinding_of_fun_mono = fun.rel_mono
876
+
877
+ lemma blinding_of_fun_hash:
878
+ assumes "bo \<le> vimage2p rh rh (=)"
879
+ shows "blinding_of_fun bo \<le> vimage2p (hash_fun rh) (hash_fun rh) (=)"
880
+ using assms by(auto simp add: vimage2p_def rel_fun_def le_fun_def)
881
+
882
+ lemma blinding_of_on_fun [locale_witness]:
883
+ assumes "blinding_of_on A rh bo"
884
+ shows "blinding_of_on {x. range x \<subseteq> A} (hash_fun rh) (blinding_of_fun bo)"
885
+ (is "blinding_of_on ?A ?h ?bo")
886
+ proof -
887
+ interpret a: blinding_of_on A rh bo by fact
888
+ show ?thesis
889
+ proof
890
+ show "?bo x x" if "x \<in> ?A" for x using that by(auto simp add: rel_fun_def intro: a.refl)
891
+ show "?bo x z" if "?bo x y" "?bo y z" "x \<in> ?A" for x y z using that
892
+ by(auto 4 3 simp add: rel_fun_def intro: a.trans)
893
+ show "x = y" if "?bo x y" "?bo y x" "x \<in> ?A" for x y using that
894
+ by(fastforce simp add: fun_eq_iff rel_fun_def intro: a.antisym)
895
+ qed(rule blinding_of_fun_hash a.hash)+
896
+ qed
897
+
898
+ lemmas blinding_of_fun [locale_witness] = blinding_of_on_fun[where A=UNIV, simplified]
899
+
900
+ (************************************************************)
901
+ subsubsection \<open> Merging \<close>
902
+ (************************************************************)
903
+
904
+ context
905
+ fixes m :: "'b\<^sub>m merge"
906
+ begin
907
+
908
+ definition merge_fun :: "('a \<Rightarrow>\<^sub>m 'b\<^sub>m) merge" where
909
+ "merge_fun f g = (if \<forall>x. m (f x) (g x) \<noteq> None then Some (\<lambda>x. the (m (f x) (g x))) else None)"
910
+
911
+ lemma merge_on_fun [locale_witness]:
912
+ assumes "merge_on A rh bo m"
913
+ shows "merge_on {x. range x \<subseteq> A} (hash_fun rh) (blinding_of_fun bo) merge_fun"
914
+ (is "merge_on ?A ?h ?bo ?m")
915
+ proof -
916
+ interpret a: merge_on A rh bo m by fact
917
+ show ?thesis
918
+ proof
919
+ show "\<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)"
920
+ if "?h a = ?h b" "a \<in> ?A" for a b
921
+ using that(1)[THEN fun_cong, unfolded o_apply, THEN a.join, OF that(2)[unfolded mem_Collect_eq, THEN subsetD, OF rangeI]]
922
+ by atomize(subst (asm) choice_iff; auto simp add: merge_fun_def rel_fun_def)
923
+ show "?m a b = None" if "?h a \<noteq> ?h b" "a \<in> ?A" for a b using that
924
+ by(auto simp add: merge_fun_def fun_eq_iff dest: a.undefined)
925
+ qed
926
+ qed
927
+
928
+ lemmas merge_fun [locale_witness] = merge_on_fun[where A=UNIV, simplified]
929
+
930
+ end
931
+
932
+ lemma merge_fun_cong[fundef_cong]:
933
+ assumes "\<And>a b. \<lbrakk> a \<in> range f; b \<in> range g \<rbrakk> \<Longrightarrow> m a b = m' a b"
934
+ shows "merge_fun m f g = merge_fun m' f g"
935
+ using assms[OF rangeI rangeI] by(clarsimp simp add: merge_fun_def)
936
+
937
+ lemma is_none_alt_def: "Option.is_none x \<longleftrightarrow> (case x of None \<Rightarrow> True | Some _ \<Rightarrow> False)"
938
+ by(auto simp add: Option.is_none_def split: option.splits)
939
+
940
+ parametric_constant is_none_parametric [transfer_rule]: is_none_alt_def
941
+
942
+ lemma merge_fun_parametric [transfer_rule]: includes lifting_syntax shows
943
+ "((A ===> B ===> rel_option C) ===> ((=) ===> A) ===> ((=) ===> B) ===> rel_option ((=) ===> C))
944
+ merge_fun merge_fun"
945
+ proof(intro rel_funI)
946
+ fix m :: "'a merge" and m' :: "'b merge" and f :: "'c \<Rightarrow> 'a" and f' :: "'c \<Rightarrow> 'b" and g :: "'c \<Rightarrow> 'a" and g' :: "'c \<Rightarrow> 'b"
947
+ assume m: "(A ===> B ===> rel_option C) m m'"
948
+ and f: "((=) ===> A) f f'" and g: "((=) ===> B) g g'"
949
+ note [transfer_rule] = this
950
+ have cond [unfolded Option.is_none_def]: "(\<forall>x. \<not> Option.is_none (m (f x) (g x))) \<longleftrightarrow> (\<forall>x. \<not> Option.is_none (m' (f' x) (g' x)))"
951
+ by transfer_prover
952
+ moreover
953
+ have "((=) ===> C) (\<lambda>x. the (m (f x) (g x))) (\<lambda>x. the (m' (f' x) (g' x)))" if *: "\<forall>x. \<not> m (f x) (g x) = None"
954
+ proof -
955
+ obtain fg fg' where m: "m (f x) (g x) = Some (fg x)" and m': "m' (f' x) (g' x) = Some (fg' x)" for x
956
+ using * *[simplified cond]
957
+ by(simp)(subst (asm) (1 2) choice_iff; clarsimp)
958
+ have "rel_option C (Some (fg x)) (Some (fg' x))" for x unfolding m[symmetric] m'[symmetric] by transfer_prover
959
+ then show ?thesis by(simp add: rel_fun_def m m')
960
+ qed
961
+ ultimately show "rel_option ((=) ===> C) (merge_fun m f g) (merge_fun m' f' g')"
962
+ unfolding merge_fun_def by(simp)
963
+ qed
964
+
965
+ (************************************************************)
966
+ subsubsection \<open> Merkle Interface \<close>
967
+ (************************************************************)
968
+
969
+ lemma merkle_fun [locale_witness]:
970
+ assumes "merkle_interface rh bo m"
971
+ shows "merkle_interface (hash_fun rh) (blinding_of_fun bo) (merge_fun m)"
972
+ proof -
973
+ interpret a: merge_on UNIV rh bo m unfolding merkle_interface_aux[symmetric] by fact
974
+ show ?thesis unfolding merkle_interface_aux[symmetric] ..
975
+ qed
976
+
977
+ (************************************************************)
978
+ subsection \<open>Rose trees\<close>
979
+ (************************************************************)
980
+
981
+ text \<open>
982
+ We now define an ADS over rose trees, which is like a arbitrarily branching Merkle tree where each
983
+ node in the tree can be blinded, including the root. The number of children and the position of a
984
+ child among its siblings cannot be hidden. The construction allows to plug in further blindable
985
+ positions in the labels of the nodes.
986
+ \<close>
987
+
988
+ type_synonym ('a, 'b) rose_tree_F = "'a \<times> 'b list"
989
+
990
+ abbreviation (input) map_rose_tree_F where
991
+ "map_rose_tree_F f1 f2 \<equiv> map_prod f1 (map f2)"
992
+ definition map_rose_tree_F_const where
993
+ "map_rose_tree_F_const f1 f2 \<equiv> map_rose_tree_F f1 f2"
994
+
995
+ datatype 'a rose_tree = Tree "('a, 'a rose_tree) rose_tree_F"
996
+
997
+ type_synonym ('a\<^sub>h, 'b\<^sub>h) rose_tree_F\<^sub>h = "('a\<^sub>h \<times>\<^sub>h 'b\<^sub>h list\<^sub>h) blindable\<^sub>h"
998
+
999
+ datatype 'a\<^sub>h rose_tree\<^sub>h = Tree\<^sub>h "('a\<^sub>h, 'a\<^sub>h rose_tree\<^sub>h) rose_tree_F\<^sub>h"
1000
+
1001
+ type_synonym ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) rose_tree_F\<^sub>m = "('a\<^sub>m \<times>\<^sub>m 'b\<^sub>m list\<^sub>m, 'a\<^sub>h \<times>\<^sub>h 'b\<^sub>h list\<^sub>h) blindable\<^sub>m"
1002
+
1003
+ datatype ('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m = Tree\<^sub>m "('a\<^sub>m, 'a\<^sub>h, ('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m, 'a\<^sub>h rose_tree\<^sub>h) rose_tree_F\<^sub>m"
1004
+
1005
+ abbreviation (input) map_rose_tree_F\<^sub>m
1006
+ :: "('ma \<Rightarrow> 'a) \<Rightarrow> ('mr \<Rightarrow> 'r) \<Rightarrow> ('ma, 'ha, 'mr, 'hr) rose_tree_F\<^sub>m \<Rightarrow> ('a, 'ha, 'r, 'hr) rose_tree_F\<^sub>m"
1007
+ where
1008
+ "map_rose_tree_F\<^sub>m f g \<equiv> map_blindable\<^sub>m (map_prod f (map g)) id"
1009
+
1010
+ (************************************************************)
1011
+ subsubsection \<open> Hashes \<close>
1012
+ (************************************************************)
1013
+
1014
+ abbreviation (input) hash_rt_F'
1015
+ :: "(('a\<^sub>h, 'a\<^sub>h, 'b\<^sub>h, 'b\<^sub>h) rose_tree_F\<^sub>m, ('a\<^sub>h, 'b\<^sub>h) rose_tree_F\<^sub>h) hash"
1016
+ where
1017
+ "hash_rt_F' \<equiv> hash_blindable id"
1018
+
1019
+ definition hash_rt_F\<^sub>m
1020
+ :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow>
1021
+ (('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) rose_tree_F\<^sub>m, ('a\<^sub>h, 'b\<^sub>h) rose_tree_F\<^sub>h) hash" where
1022
+ "hash_rt_F\<^sub>m h rhm \<equiv> hash_rt_F' o map_rose_tree_F\<^sub>m h rhm"
1023
+
1024
+ lemma hash_rt_F\<^sub>m_alt_def: "hash_rt_F\<^sub>m h rhm = hash_blindable (map_prod h (map rhm))"
1025
+ by(simp add: hash_rt_F\<^sub>m_def fun_eq_iff hash_map_blindable_simp)
1026
+
1027
+ primrec (transfer) hash_rt_tree'
1028
+ :: "(('a\<^sub>h, 'a\<^sub>h) rose_tree\<^sub>m, 'a\<^sub>h rose_tree\<^sub>h) hash" where
1029
+ "hash_rt_tree' (Tree\<^sub>m x) = Tree\<^sub>h (hash_rt_F' (map_rose_tree_F\<^sub>m id hash_rt_tree' x))"
1030
+
1031
+ definition hash_tree
1032
+ :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> (('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m, 'a\<^sub>h rose_tree\<^sub>h) hash" where
1033
+ "hash_tree h = hash_rt_tree' o map_rose_tree\<^sub>m h id"
1034
+
1035
+ lemma blindable\<^sub>m_map_compositionality:
1036
+ "map_blindable\<^sub>m f g o map_blindable\<^sub>m f' g' = map_blindable\<^sub>m (f o f') (g o g')"
1037
+ by(rule ext) (simp add: blindable\<^sub>m.map_comp)
1038
+
1039
+ lemma hash_tree_simps [simp]:
1040
+ "hash_tree h (Tree\<^sub>m x) = Tree\<^sub>h (hash_rt_F\<^sub>m h (hash_tree h) x)"
1041
+ by(simp add: hash_tree_def hash_rt_F\<^sub>m_def
1042
+ map_prod.comp map_sum.comp rose_tree\<^sub>h.map_comp blindable\<^sub>m.map_comp
1043
+ prod.map_id0 rose_tree\<^sub>h.map_id0)
1044
+
1045
+ parametric_constant hash_rt_F\<^sub>m_parametric [transfer_rule]: hash_rt_F\<^sub>m_alt_def
1046
+
1047
+ parametric_constant hash_tree_parametric [transfer_rule]: hash_tree_def
1048
+
1049
+ (************************************************************)
1050
+ subsubsection \<open> Blinding \<close>
1051
+ (************************************************************)
1052
+
1053
+ abbreviation (input) blinding_of_rt_F\<^sub>m
1054
+ :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> 'a\<^sub>m blinding_of \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> 'b\<^sub>m blinding_of
1055
+ \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) rose_tree_F\<^sub>m blinding_of" where
1056
+ "blinding_of_rt_F\<^sub>m ha boa hb bob \<equiv> blinding_of_blindable (hash_prod ha (map hb))
1057
+ (blinding_of_prod boa (blinding_of_list bob))"
1058
+
1059
+ lemma blinding_of_rt_F\<^sub>m_mono:
1060
+ "\<lbrakk> boa \<le> boa'; bob \<le> bob' \<rbrakk> \<Longrightarrow> blinding_of_rt_F\<^sub>m ha boa hb bob \<le> blinding_of_rt_F\<^sub>m ha boa' hb bob'"
1061
+ by(intro blinding_of_blindable_mono prod.rel_mono list.rel_mono)
1062
+
1063
+ lemma blinding_of_rt_F\<^sub>m_mono_inductive:
1064
+ assumes "\<And>x y. boa x y \<longrightarrow> boa' x y" "\<And>x y. bob x y \<longrightarrow> bob' x y"
1065
+ shows "blinding_of_rt_F\<^sub>m ha boa hb bob x y \<longrightarrow> blinding_of_rt_F\<^sub>m ha boa' hb bob' x y"
1066
+ apply(rule impI)
1067
+ apply(erule blinding_of_rt_F\<^sub>m_mono[THEN predicate2D, rotated -1])
1068
+ using assms by blast+
1069
+
1070
+ context
1071
+ fixes h :: "('a\<^sub>m, 'a\<^sub>h) hash"
1072
+ and bo :: "'a\<^sub>m blinding_of"
1073
+ begin
1074
+
1075
+ inductive blinding_of_tree :: "('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m blinding_of" where
1076
+ "blinding_of_tree (Tree\<^sub>m t1) (Tree\<^sub>m t2)"
1077
+ if "blinding_of_rt_F\<^sub>m h bo (hash_tree h) blinding_of_tree t1 t2"
1078
+ monos blinding_of_rt_F\<^sub>m_mono_inductive
1079
+
1080
+ end
1081
+
1082
+ inductive_simps blinding_of_tree_simps [simp]:
1083
+ "blinding_of_tree h bo (Tree\<^sub>m t1) (Tree\<^sub>m t2)"
1084
+
1085
+ lemma blinding_of_rt_F\<^sub>m_hash:
1086
+ assumes "boa \<le> vimage2p ha ha (=)" "bob \<le> vimage2p hb hb (=)"
1087
+ shows "blinding_of_rt_F\<^sub>m ha boa hb bob \<le> vimage2p (hash_rt_F\<^sub>m ha hb) (hash_rt_F\<^sub>m ha hb) (=)"
1088
+ apply(rule order_trans)
1089
+ apply(rule blinding_of_blindable_hash)
1090
+ apply(fold relator_eq)
1091
+ apply(unfold vimage2p_map_rel_prod vimage2p_map_list_all2)
1092
+ apply(rule prod.rel_mono assms list.rel_mono)+
1093
+ apply(simp only: hash_rt_F\<^sub>m_def vimage2p_comp o_apply hash_blindable_def blindable\<^sub>m.map_id0 id_def[symmetric] vimage2p_id id_apply)
1094
+ done
1095
+
1096
+ lemma blinding_of_tree_hash:
1097
+ assumes "bo \<le> vimage2p h h (=)"
1098
+ shows "blinding_of_tree h bo \<le> vimage2p (hash_tree h) (hash_tree h) (=)"
1099
+ apply(rule predicate2I vimage2pI)+
1100
+ apply(erule blinding_of_tree.induct)
1101
+ apply(simp)
1102
+ apply(erule blinding_of_rt_F\<^sub>m_hash[OF assms, THEN predicate2D_vimage2p, rotated 1])
1103
+ apply(blast intro: vimage2pI)
1104
+ done
1105
+
1106
+ abbreviation (input) set1_rt_F\<^sub>m :: "('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>h, 'b\<^sub>m) rose_tree_F\<^sub>m \<Rightarrow> 'a\<^sub>m set" where
1107
+ "set1_rt_F\<^sub>m x \<equiv> set1_blindable\<^sub>m x \<bind> fsts"
1108
+
1109
+ abbreviation (input) set3_rt_F\<^sub>m :: "('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) rose_tree_F\<^sub>m \<Rightarrow> 'b\<^sub>m set" where
1110
+ "set3_rt_F\<^sub>m x \<equiv> (set1_blindable\<^sub>m x \<bind> snds) \<bind> set"
1111
+
1112
+ lemma set_rt_F\<^sub>m_eq:
1113
+ "{x. set1_rt_F\<^sub>m x \<subseteq> A \<and> set3_rt_F\<^sub>m x \<subseteq> B} =
1114
+ {x. set1_blindable\<^sub>m x \<subseteq> {x. fsts x \<subseteq> A \<and> snds x \<subseteq> {x. set x \<subseteq> B}}}"
1115
+ by force
1116
+
1117
+ lemma hash_blindable_map: "hash_blindable f \<circ> map_blindable\<^sub>m g id = hash_blindable (f \<circ> g)"
1118
+ by(rule ext) (simp add: hash_blindable_def blindable\<^sub>m.map_comp)
1119
+
1120
+ lemma blinding_of_on_tree [locale_witness]:
1121
+ assumes "blinding_of_on A h bo"
1122
+ shows "blinding_of_on {x. set1_rose_tree\<^sub>m x \<subseteq> A} (hash_tree h) (blinding_of_tree h bo)"
1123
+ (is "blinding_of_on ?A ?h ?bo")
1124
+ proof -
1125
+ interpret a: blinding_of_on A h bo by fact
1126
+ show ?thesis
1127
+ proof
1128
+ show "?bo \<le> vimage2p ?h ?h (=)" using a.hash by(rule blinding_of_tree_hash)
1129
+ have "?bo x x \<and> (?bo x y \<longrightarrow> ?bo y z \<longrightarrow> ?bo x z) \<and> (?bo x y \<longrightarrow> ?bo y x \<longrightarrow> x = y)" if "x \<in> ?A" for x y z using that
1130
+ proof(induction x arbitrary: y z)
1131
+ case (Tree\<^sub>m x)
1132
+ have [locale_witness]: "blinding_of_on (set3_rt_F\<^sub>m x) (hash_tree h) (blinding_of_tree h bo)"
1133
+ apply unfold_locales
1134
+ subgoal by(rule blinding_of_tree_hash)(rule a.hash)
1135
+ subgoal using Tree\<^sub>m.IH Tree\<^sub>m.prems by(fastforce simp add: eq_onp_def)
1136
+ subgoal for x y z using Tree\<^sub>m.IH[of _ _ x y z] Tree\<^sub>m.prems by fastforce
1137
+ subgoal for x y using Tree\<^sub>m.IH[of _ _ x y] Tree\<^sub>m.prems by fastforce
1138
+ done
1139
+ interpret blinding_of_on
1140
+ "{a. set1_rt_F\<^sub>m a \<subseteq> A \<and> set3_rt_F\<^sub>m a \<subseteq> set3_rt_F\<^sub>m x}"
1141
+ "hash_rt_F\<^sub>m h ?h" "blinding_of_rt_F\<^sub>m h bo ?h ?bo"
1142
+ unfolding set_rt_F\<^sub>m_eq hash_rt_F\<^sub>m_alt_def ..
1143
+ from Tree\<^sub>m.prems show ?case
1144
+ apply(intro conjI)
1145
+ subgoal by(fastforce intro!: blinding_of_tree.intros refl[unfolded hash_rt_F\<^sub>m_alt_def])
1146
+ subgoal by(fastforce elim!: blinding_of_tree.cases trans[unfolded hash_rt_F\<^sub>m_alt_def]
1147
+ intro!: blinding_of_tree.intros)
1148
+ subgoal by(fastforce elim!: blinding_of_tree.cases antisym[unfolded hash_rt_F\<^sub>m_alt_def])
1149
+ done
1150
+ qed
1151
+ then show "x \<in> ?A \<Longrightarrow> ?bo x x"
1152
+ and "\<lbrakk> ?bo x y; ?bo y z; x \<in> ?A \<rbrakk> \<Longrightarrow> ?bo x z"
1153
+ and "\<lbrakk> ?bo x y; ?bo y x; x \<in> ?A \<rbrakk> \<Longrightarrow> x = y"
1154
+ for x y z by blast+
1155
+ qed
1156
+ qed
1157
+
1158
+ lemmas blinding_of_tree [locale_witness] = blinding_of_on_tree[where A=UNIV, simplified]
1159
+
1160
+ lemma blinding_of_tree_mono:
1161
+ "bo \<le> bo' \<Longrightarrow> blinding_of_tree h bo \<le> blinding_of_tree h bo'"
1162
+ apply(rule predicate2I)
1163
+ apply(erule blinding_of_tree.induct)
1164
+ apply(rule blinding_of_tree.intros)
1165
+ apply(erule blinding_of_rt_F\<^sub>m_mono[THEN predicate2D, rotated -1])
1166
+ apply(blast)+
1167
+ done
1168
+
1169
+ (************************************************************)
1170
+ subsubsection \<open> Merging \<close>
1171
+ (************************************************************)
1172
+
1173
+ definition merge_rt_F\<^sub>m
1174
+ :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> 'a\<^sub>m merge \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> 'b\<^sub>m merge \<Rightarrow>
1175
+ ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) rose_tree_F\<^sub>m merge"
1176
+ where
1177
+ "merge_rt_F\<^sub>m ha ma hr mr \<equiv> merge_blindable (hash_prod ha (hash_list hr)) (merge_prod ma (merge_list mr))"
1178
+
1179
+ lemma merge_rt_F\<^sub>m_cong [fundef_cong]:
1180
+ assumes "\<And>a b. \<lbrakk> a \<in> set1_rt_F\<^sub>m x; b \<in> set1_rt_F\<^sub>m y \<rbrakk> \<Longrightarrow> ma a b = ma' a b"
1181
+ and "\<And>a b. \<lbrakk> a \<in> set3_rt_F\<^sub>m x; b \<in> set3_rt_F\<^sub>m y \<rbrakk> \<Longrightarrow> mm a b = mm' a b"
1182
+ shows "merge_rt_F\<^sub>m ha ma hm mm x y = merge_rt_F\<^sub>m ha ma' hm mm' x y"
1183
+ using assms
1184
+ apply(cases x; cases y; simp add: merge_rt_F\<^sub>m_def bind_UNION)
1185
+ apply(rule arg_cong[where f="map_option _"])
1186
+ apply(blast intro: merge_prod_cong merge_list_cong)
1187
+ done
1188
+
1189
+ lemma in_set1_blindable\<^sub>m_iff: "x \<in> set1_blindable\<^sub>m y \<longleftrightarrow> y = Unblinded x"
1190
+ by(cases y) auto
1191
+
1192
+ context
1193
+ fixes h :: "('a\<^sub>m, 'a\<^sub>h) hash"
1194
+ and ma :: "'a\<^sub>m merge"
1195
+ notes in_set1_blindable\<^sub>m_iff[simp]
1196
+ begin
1197
+ fun merge_tree :: "('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m merge" where
1198
+ "merge_tree (Tree\<^sub>m x) (Tree\<^sub>m y) = map_option Tree\<^sub>m (
1199
+ merge_rt_F\<^sub>m h ma (hash_tree h) merge_tree x y)"
1200
+ end
1201
+
1202
+ lemma merge_on_tree [locale_witness]:
1203
+ assumes "merge_on A h bo m"
1204
+ shows "merge_on {x. set1_rose_tree\<^sub>m x \<subseteq> A} (hash_tree h) (blinding_of_tree h bo) (merge_tree h m)"
1205
+ (is "merge_on ?A ?h ?bo ?m")
1206
+ proof -
1207
+ interpret a: merge_on A h bo m by fact
1208
+ show ?thesis
1209
+ proof
1210
+ have "(?h a = ?h b \<longrightarrow> (\<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u))) \<and>
1211
+ (?h a \<noteq> ?h b \<longrightarrow> ?m a b = None)"
1212
+ if "a \<in> ?A" for a b using that unfolding mem_Collect_eq
1213
+ proof(induction a arbitrary: b rule: rose_tree\<^sub>m.induct)
1214
+ case (Tree\<^sub>m x y)
1215
+ interpret merge_on
1216
+ "{y. set1_rt_F\<^sub>m y \<subseteq> A \<and> set3_rt_F\<^sub>m y \<subseteq> set3_rt_F\<^sub>m x}"
1217
+ "hash_rt_F\<^sub>m h ?h"
1218
+ "blinding_of_rt_F\<^sub>m h bo ?h ?bo"
1219
+ "merge_rt_F\<^sub>m h m ?h ?m"
1220
+ unfolding set_rt_F\<^sub>m_eq hash_rt_F\<^sub>m_alt_def merge_rt_F\<^sub>m_def
1221
+ proof
1222
+ fix a
1223
+ assume a: "a \<in> set3_rt_F\<^sub>m x"
1224
+ with Tree\<^sub>m.prems have a': "set1_rose_tree\<^sub>m a \<subseteq> A"
1225
+ by(force simp add: bind_UNION)
1226
+
1227
+ from a obtain l and ab where a'': "ab \<in> set1_blindable\<^sub>m x" "l \<in> snds ab" "a \<in> set l"
1228
+ by(clarsimp simp add: bind_UNION)
1229
+
1230
+ fix b
1231
+ from Tree\<^sub>m.IH[OF a'' a', rule_format, of b]
1232
+ show "hash_tree h a = hash_tree h b
1233
+ \<Longrightarrow> \<exists>ab. merge_tree h m a b = Some ab \<and> blinding_of_tree h bo a ab \<and> blinding_of_tree h bo b ab \<and>
1234
+ (\<forall>u. blinding_of_tree h bo a u \<longrightarrow> blinding_of_tree h bo b u \<longrightarrow> blinding_of_tree h bo ab u)"
1235
+ and "hash_tree h a \<noteq> hash_tree h b \<Longrightarrow> merge_tree h m a b = None"
1236
+ by(auto dest: sym)
1237
+ qed
1238
+ show ?case using Tree\<^sub>m.prems
1239
+ apply(intro conjI strip)
1240
+ subgoal by(cases y)(fastforce dest!: join simp add: blinding_of_tree.simps)
1241
+ subgoal by (cases y) (fastforce dest!: undefined)
1242
+ done
1243
+ qed
1244
+ then show
1245
+ "?h a = ?h b \<Longrightarrow> \<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)"
1246
+ "?h a \<noteq> ?h b \<Longrightarrow> ?m a b = None"
1247
+ if "a \<in> ?A" for a b using that by blast+
1248
+ qed
1249
+ qed
1250
+
1251
+ lemmas merge_tree [locale_witness] = merge_on_tree[where A=UNIV, simplified]
1252
+
1253
+ lemma option_bind_comm:
1254
+ "((x :: 'a option) \<bind> (\<lambda>y. c \<bind> (\<lambda>z. f y z))) = (c \<bind> (\<lambda>y. x \<bind> (\<lambda>z. f z y)))"
1255
+ by(cases x; cases c; auto)
1256
+
1257
+ parametric_constant merge_rt_F\<^sub>m_parametric [transfer_rule]: merge_rt_F\<^sub>m_def
1258
+
1259
+ (************************************************************)
1260
+ subsubsection \<open>Merkle interface\<close>
1261
+ (************************************************************)
1262
+
1263
+ lemma merkle_tree [locale_witness]:
1264
+ assumes "merkle_interface h bo m"
1265
+ shows "merkle_interface (hash_tree h) (blinding_of_tree h bo) (merge_tree h m)"
1266
+ proof -
1267
+ interpret merge_on UNIV h bo m using assms unfolding merkle_interface_aux .
1268
+ show ?thesis unfolding merkle_interface_aux[symmetric] ..
1269
+ qed
1270
+
1271
+ lemma merge_tree_cong [fundef_cong]:
1272
+ assumes "\<And>a b. \<lbrakk> a \<in> set1_rose_tree\<^sub>m x; b \<in> set1_rose_tree\<^sub>m y \<rbrakk> \<Longrightarrow> m a b = m' a b"
1273
+ shows "merge_tree h m x y = merge_tree h m' x y"
1274
+ using assms
1275
+ apply(induction x y rule: merge_tree.induct)
1276
+ apply(simp add: bind_UNION)
1277
+ apply(rule arg_cong[where f="map_option _"])
1278
+ apply(rule merge_rt_F\<^sub>m_cong; simp add: bind_UNION; blast)
1279
+ done
1280
+
1281
+ end
formal/afp/ADS_Functor/Canton_Transaction_Tree.thy ADDED
@@ -0,0 +1,518 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ theory Canton_Transaction_Tree imports
2
+ Inclusion_Proof_Construction
3
+ begin
4
+
5
+ section \<open>Canton's hierarchical transaction trees\<close>
6
+
7
+ typedecl view_data
8
+ typedecl view_metadata
9
+ typedecl common_metadata
10
+ typedecl participant_metadata
11
+
12
+ datatype view = View view_metadata view_data (subviews: "view list")
13
+
14
+ datatype transaction = Transaction common_metadata participant_metadata (views: "view list")
15
+
16
+ subsection \<open>Views as authenticated data structures\<close>
17
+
18
+ type_synonym view_metadata\<^sub>h = "view_metadata blindable\<^sub>h"
19
+ type_synonym view_data\<^sub>h = "view_data blindable\<^sub>h"
20
+
21
+ datatype view\<^sub>h = View\<^sub>h "((view_metadata\<^sub>h \<times>\<^sub>h view_data\<^sub>h) \<times>\<^sub>h view\<^sub>h list\<^sub>h) blindable\<^sub>h"
22
+
23
+ type_synonym view_metadata\<^sub>m = "(view_metadata, view_metadata) blindable\<^sub>m"
24
+ type_synonym view_data\<^sub>m = "(view_data, view_data) blindable\<^sub>m"
25
+
26
+ datatype view\<^sub>m = View\<^sub>m
27
+ "((view_metadata\<^sub>m \<times>\<^sub>m view_data\<^sub>m) \<times>\<^sub>m view\<^sub>m list\<^sub>m,
28
+ (view_metadata\<^sub>h \<times>\<^sub>h view_data\<^sub>h) \<times>\<^sub>h view\<^sub>h list\<^sub>h) blindable\<^sub>m"
29
+
30
+ abbreviation (input) hash_view_data :: "(view_data\<^sub>m, view_data\<^sub>h) hash" where
31
+ "hash_view_data \<equiv> hash_blindable id"
32
+ abbreviation (input) blinding_of_view_data :: "view_data\<^sub>m blinding_of" where
33
+ "blinding_of_view_data \<equiv> blinding_of_blindable id (=)"
34
+ abbreviation (input) merge_view_data :: "view_data\<^sub>m merge" where
35
+ "merge_view_data \<equiv> merge_blindable id merge_discrete"
36
+
37
+ lemma merkle_view_data:
38
+ "merkle_interface hash_view_data blinding_of_view_data merge_view_data"
39
+ by unfold_locales
40
+
41
+ abbreviation (input) hash_view_metadata :: "(view_metadata\<^sub>m, view_metadata\<^sub>h) hash" where
42
+ "hash_view_metadata \<equiv> hash_blindable id"
43
+ abbreviation (input) blinding_of_view_metadata :: "view_metadata\<^sub>m blinding_of" where
44
+ "blinding_of_view_metadata \<equiv> blinding_of_blindable id (=)"
45
+ abbreviation (input) merge_view_metadata :: "view_metadata\<^sub>m merge" where
46
+ "merge_view_metadata \<equiv> merge_blindable id merge_discrete"
47
+
48
+ lemma merkle_view_metadata:
49
+ "merkle_interface hash_view_metadata blinding_of_view_metadata merge_view_metadata"
50
+ by unfold_locales
51
+
52
+ type_synonym view_content = "view_metadata \<times> view_data"
53
+ type_synonym view_content\<^sub>h = "view_metadata\<^sub>h \<times>\<^sub>h view_data\<^sub>h"
54
+ type_synonym view_content\<^sub>m = "view_metadata\<^sub>m \<times>\<^sub>m view_data\<^sub>m"
55
+
56
+ locale view_merkle begin
57
+
58
+ type_synonym view\<^sub>h' = "view_content\<^sub>h rose_tree\<^sub>h"
59
+
60
+ primrec from_view\<^sub>h :: "view\<^sub>h \<Rightarrow> view\<^sub>h'" where
61
+ "from_view\<^sub>h (View\<^sub>h x) = Tree\<^sub>h (map_blindable\<^sub>h (map_prod id (map from_view\<^sub>h)) x)"
62
+
63
+ primrec to_view\<^sub>h :: "view\<^sub>h' \<Rightarrow> view\<^sub>h" where
64
+ "to_view\<^sub>h (Tree\<^sub>h x) = View\<^sub>h (map_blindable\<^sub>h (map_prod id (map to_view\<^sub>h)) x)"
65
+
66
+ lemma from_to_view\<^sub>h [simp]: "from_view\<^sub>h (to_view\<^sub>h x) = x"
67
+ apply(induction x)
68
+ apply(simp add: blindable\<^sub>h.map_comp o_def prod.map_comp)
69
+ apply(simp cong: blindable\<^sub>h.map_cong prod.map_cong list.map_cong add: blindable\<^sub>h.map_id[unfolded id_def])
70
+ done
71
+
72
+ lemma to_from_view\<^sub>h [simp]: "to_view\<^sub>h (from_view\<^sub>h x) = x"
73
+ apply(induction x)
74
+ apply(simp add: blindable\<^sub>h.map_comp o_def prod.map_comp)
75
+ apply(simp cong: blindable\<^sub>h.map_cong prod.map_cong list.map_cong add: blindable\<^sub>h.map_id[unfolded id_def])
76
+ done
77
+
78
+ lemma iso_view\<^sub>h: "type_definition from_view\<^sub>h to_view\<^sub>h UNIV"
79
+ by unfold_locales simp_all
80
+
81
+ setup_lifting iso_view\<^sub>h
82
+
83
+ lemma cr_view\<^sub>h_Grp: "cr_view\<^sub>h = Grp UNIV to_view\<^sub>h"
84
+ by(simp add: cr_view\<^sub>h_def Grp_def fun_eq_iff)(transfer, auto)
85
+
86
+ lemma View\<^sub>h_transfer [transfer_rule]: includes lifting_syntax shows
87
+ "(rel_blindable\<^sub>h (rel_prod (=) (list_all2 pcr_view\<^sub>h)) ===> pcr_view\<^sub>h) Tree\<^sub>h View\<^sub>h"
88
+ by(simp add: rel_fun_def view\<^sub>h.pcr_cr_eq cr_view\<^sub>h_Grp list.rel_Grp eq_alt prod.rel_Grp blindable\<^sub>h.rel_Grp)
89
+ (simp add: Grp_def)
90
+
91
+ type_synonym view\<^sub>m' = "(view_content\<^sub>m, view_content\<^sub>h) rose_tree\<^sub>m"
92
+
93
+ primrec from_view\<^sub>m :: "view\<^sub>m \<Rightarrow> view\<^sub>m'" where
94
+ "from_view\<^sub>m (View\<^sub>m x) = Tree\<^sub>m (map_blindable\<^sub>m (map_prod id (map from_view\<^sub>m)) (map_prod id (map from_view\<^sub>h)) x)"
95
+
96
+ primrec to_view\<^sub>m :: "view\<^sub>m' \<Rightarrow> view\<^sub>m" where
97
+ "to_view\<^sub>m (Tree\<^sub>m x) = View\<^sub>m (map_blindable\<^sub>m (map_prod id (map to_view\<^sub>m)) (map_prod id (map to_view\<^sub>h)) x)"
98
+
99
+ lemma from_to_view\<^sub>m [simp]: "from_view\<^sub>m (to_view\<^sub>m x) = x"
100
+ apply(induction x)
101
+ apply(simp add: blindable\<^sub>m.map_comp o_def prod.map_comp)
102
+ apply(simp cong: blindable\<^sub>m.map_cong prod.map_cong list.map_cong add: blindable\<^sub>m.map_id[unfolded id_def])
103
+ done
104
+
105
+ lemma to_from_view\<^sub>m [simp]: "to_view\<^sub>m (from_view\<^sub>m x) = x"
106
+ apply(induction x)
107
+ apply(simp add: blindable\<^sub>m.map_comp o_def prod.map_comp)
108
+ apply(simp cong: blindable\<^sub>m.map_cong prod.map_cong list.map_cong add: blindable\<^sub>m.map_id[unfolded id_def])
109
+ done
110
+
111
+ lemma iso_view\<^sub>m: "type_definition from_view\<^sub>m to_view\<^sub>m UNIV"
112
+ by unfold_locales simp_all
113
+
114
+ setup_lifting iso_view\<^sub>m
115
+
116
+ lemma cr_view\<^sub>m_Grp: "cr_view\<^sub>m = Grp UNIV to_view\<^sub>m"
117
+ by(simp add: cr_view\<^sub>m_def Grp_def fun_eq_iff)(transfer, auto)
118
+
119
+ lemma View\<^sub>m_transfer [transfer_rule]: includes lifting_syntax shows
120
+ "(rel_blindable\<^sub>m (rel_prod (=) (list_all2 pcr_view\<^sub>m)) (rel_prod (=) (list_all2 pcr_view\<^sub>h)) ===> pcr_view\<^sub>m) Tree\<^sub>m View\<^sub>m"
121
+ by(simp add: rel_fun_def view\<^sub>h.pcr_cr_eq view\<^sub>m.pcr_cr_eq cr_view\<^sub>h_Grp cr_view\<^sub>m_Grp list.rel_Grp eq_alt prod.rel_Grp blindable\<^sub>m.rel_Grp)
122
+ (simp add: Grp_def)
123
+
124
+ end
125
+
126
+ code_datatype View\<^sub>h
127
+ code_datatype View\<^sub>m
128
+
129
+ context begin
130
+ interpretation view_merkle .
131
+
132
+ abbreviation (input) hash_view_content :: "(view_content\<^sub>m, view_content\<^sub>h) hash" where
133
+ "hash_view_content \<equiv> hash_prod hash_view_metadata hash_view_data"
134
+
135
+ abbreviation (input) blinding_of_view_content :: "view_content\<^sub>m blinding_of" where
136
+ "blinding_of_view_content \<equiv> blinding_of_prod blinding_of_view_metadata blinding_of_view_data"
137
+
138
+ abbreviation (input) merge_view_content :: "view_content\<^sub>m merge" where
139
+ "merge_view_content \<equiv> merge_prod merge_view_metadata merge_view_data"
140
+
141
+ lift_definition hash_view :: "(view\<^sub>m, view\<^sub>h) hash" is
142
+ "hash_tree hash_view_content" .
143
+
144
+ lift_definition blinding_of_view :: "view\<^sub>m blinding_of" is
145
+ "blinding_of_tree hash_view_content blinding_of_view_content" .
146
+
147
+ lift_definition merge_view :: "view\<^sub>m merge" is
148
+ "merge_tree hash_view_content merge_view_content" .
149
+
150
+ lemma merkle_view [locale_witness]: "merkle_interface hash_view blinding_of_view merge_view"
151
+ by transfer unfold_locales
152
+
153
+ lemma hash_view_simps [simp]:
154
+ "hash_view (View\<^sub>m x) =
155
+ View\<^sub>h (hash_blindable (hash_prod hash_view_content (hash_list hash_view)) x)"
156
+ by transfer(simp add: hash_rt_F\<^sub>m_def prod.map_comp hash_blindable_def blindable\<^sub>m.map_id)
157
+
158
+ lemma blinding_of_view_iff [simp]:
159
+ "blinding_of_view (View\<^sub>m x) (View\<^sub>m y) \<longleftrightarrow>
160
+ blinding_of_blindable (hash_prod hash_view_content (hash_list hash_view))
161
+ (blinding_of_prod blinding_of_view_content (blinding_of_list blinding_of_view)) x y"
162
+ by transfer simp
163
+
164
+ lemma blinding_of_view_induct [consumes 1, induct pred: blinding_of_view]:
165
+ assumes "blinding_of_view x y"
166
+ and "\<And>x y. blinding_of_blindable (hash_prod hash_view_content (hash_list hash_view))
167
+ (blinding_of_prod blinding_of_view_content (blinding_of_list (\<lambda>x y. blinding_of_view x y \<and> P x y))) x y
168
+ \<Longrightarrow> P (View\<^sub>m x) (View\<^sub>m y)"
169
+ shows "P x y"
170
+ using assms by transfer(rule blinding_of_tree.induct)
171
+
172
+ lemma merge_view_simps [simp]:
173
+ "merge_view (View\<^sub>m x) (View\<^sub>m y) =
174
+ map_option View\<^sub>m (merge_rt_F\<^sub>m hash_view_content merge_view_content hash_view merge_view x y)"
175
+ by transfer simp
176
+
177
+ end
178
+
179
+ subsection \<open>Transaction trees as authenticated data structures\<close>
180
+
181
+ type_synonym common_metadata\<^sub>h = "common_metadata blindable\<^sub>h"
182
+ type_synonym common_metadata\<^sub>m = "(common_metadata, common_metadata) blindable\<^sub>m"
183
+
184
+ type_synonym participant_metadata\<^sub>h = "participant_metadata blindable\<^sub>h"
185
+ type_synonym participant_metadata\<^sub>m = "(participant_metadata, participant_metadata) blindable\<^sub>m"
186
+
187
+ datatype transaction\<^sub>h = Transaction\<^sub>h
188
+ (the_Transaction\<^sub>h: "((common_metadata\<^sub>h \<times>\<^sub>h participant_metadata\<^sub>h) \<times>\<^sub>h view\<^sub>h list\<^sub>h) blindable\<^sub>h")
189
+
190
+ datatype transaction\<^sub>m = Transaction\<^sub>m
191
+ (the_Transaction\<^sub>m: "((common_metadata\<^sub>m \<times>\<^sub>m participant_metadata\<^sub>m) \<times>\<^sub>m view\<^sub>m list\<^sub>m,
192
+ (common_metadata\<^sub>h \<times>\<^sub>h participant_metadata\<^sub>h) \<times>\<^sub>h view\<^sub>h list\<^sub>h) blindable\<^sub>m")
193
+
194
+ abbreviation (input) hash_common_metadata :: "(common_metadata\<^sub>m, common_metadata\<^sub>h) hash" where
195
+ "hash_common_metadata \<equiv> hash_blindable id"
196
+ abbreviation (input) blinding_of_common_metadata :: "common_metadata\<^sub>m blinding_of" where
197
+ "blinding_of_common_metadata \<equiv> blinding_of_blindable id (=)"
198
+ abbreviation (input) merge_common_metadata :: "common_metadata\<^sub>m merge" where
199
+ "merge_common_metadata \<equiv> merge_blindable id merge_discrete"
200
+
201
+ abbreviation (input) hash_participant_metadata :: "(participant_metadata\<^sub>m, participant_metadata\<^sub>h) hash" where
202
+ "hash_participant_metadata \<equiv> hash_blindable id"
203
+ abbreviation (input) blinding_of_participant_metadata :: "participant_metadata\<^sub>m blinding_of" where
204
+ "blinding_of_participant_metadata \<equiv> blinding_of_blindable id (=)"
205
+ abbreviation (input) merge_participant_metadata :: "participant_metadata\<^sub>m merge" where
206
+ "merge_participant_metadata \<equiv> merge_blindable id merge_discrete"
207
+
208
+ locale transaction_merkle begin
209
+
210
+ lemma iso_transaction\<^sub>h: "type_definition the_Transaction\<^sub>h Transaction\<^sub>h UNIV"
211
+ by unfold_locales simp_all
212
+
213
+ setup_lifting iso_transaction\<^sub>h
214
+
215
+ lemma Transaction\<^sub>h_transfer [transfer_rule]: includes lifting_syntax shows
216
+ "((=) ===> pcr_transaction\<^sub>h) id Transaction\<^sub>h"
217
+ by(simp add: transaction\<^sub>h.pcr_cr_eq cr_transaction\<^sub>h_def rel_fun_def)
218
+
219
+ lemma iso_transaction\<^sub>m: "type_definition the_Transaction\<^sub>m Transaction\<^sub>m UNIV"
220
+ by unfold_locales simp_all
221
+
222
+ setup_lifting iso_transaction\<^sub>m
223
+
224
+ lemma Transaction\<^sub>m_transfer [transfer_rule]: includes lifting_syntax shows
225
+ "((=) ===> pcr_transaction\<^sub>m) id Transaction\<^sub>m"
226
+ by(simp add: transaction\<^sub>m.pcr_cr_eq cr_transaction\<^sub>m_def rel_fun_def)
227
+
228
+ end
229
+
230
+ code_datatype Transaction\<^sub>h
231
+ code_datatype Transaction\<^sub>m
232
+
233
+ context begin
234
+ interpretation transaction_merkle .
235
+
236
+ lift_definition hash_transaction :: "(transaction\<^sub>m, transaction\<^sub>h) hash" is
237
+ "hash_blindable (hash_prod (hash_prod hash_common_metadata hash_participant_metadata) (hash_list hash_view))" .
238
+
239
+ lift_definition blinding_of_transaction :: "transaction\<^sub>m blinding_of" is
240
+ "blinding_of_blindable
241
+ (hash_prod (hash_prod hash_common_metadata hash_participant_metadata) (hash_list hash_view))
242
+ (blinding_of_prod (blinding_of_prod blinding_of_common_metadata blinding_of_participant_metadata) (blinding_of_list blinding_of_view))" .
243
+
244
+ lift_definition merge_transaction :: "transaction\<^sub>m merge" is
245
+ "merge_blindable
246
+ (hash_prod (hash_prod hash_common_metadata hash_participant_metadata) (hash_list hash_view))
247
+ (merge_prod (merge_prod merge_common_metadata merge_participant_metadata) (merge_list merge_view))" .
248
+
249
+ lemma merkle_transaction [locale_witness]:
250
+ "merkle_interface hash_transaction blinding_of_transaction merge_transaction"
251
+ by transfer unfold_locales
252
+
253
+ lemmas hash_transaction_simps [simp] = hash_transaction.abs_eq
254
+ lemmas blinding_of_transaction_iff [simp] = blinding_of_transaction.abs_eq
255
+ lemmas merge_transaction_simps [simp] = merge_transaction.abs_eq
256
+
257
+ end
258
+
259
+ interpretation transaction:
260
+ merkle_interface hash_transaction blinding_of_transaction merge_transaction
261
+ by(rule merkle_transaction)
262
+
263
+ subsection \<open>
264
+ Constructing authenticated data structures for views
265
+ \<close>
266
+
267
+ context view_merkle begin
268
+
269
+ type_synonym view' = "(view_metadata \<times> view_data) rose_tree"
270
+
271
+ primrec from_view :: "view \<Rightarrow> view'" where
272
+ "from_view (View vm vd vs) = Tree ((vm, vd), map from_view vs)"
273
+
274
+ primrec to_view :: "view' \<Rightarrow> view" where
275
+ "to_view (Tree x) = View (fst (fst x)) (snd (fst x)) (snd (map_prod id (map to_view) x))"
276
+
277
+ lemma from_to_view [simp]: "from_view (to_view x) = x"
278
+ by(induction x)(clarsimp cong: map_cong)
279
+
280
+ lemma to_from_view [simp]: "to_view (from_view x) = x"
281
+ by(induction x)(clarsimp cong: map_cong)
282
+
283
+ lemma iso_view: "type_definition from_view to_view UNIV"
284
+ by unfold_locales simp_all
285
+
286
+ setup_lifting iso_view
287
+
288
+ definition View' :: "(view_metadata \<times> view_data) \<times> view list \<Rightarrow> view" where
289
+ "View' = (\<lambda>((vm, vd), vs). View vm vd vs)"
290
+
291
+ lemma View_View': "View = (\<lambda>vm vd vs. View' ((vm, vd), vs))"
292
+ by(simp add: View'_def)
293
+
294
+ lemma cr_view_Grp: "cr_view = Grp UNIV to_view"
295
+ by(simp add: cr_view_def Grp_def fun_eq_iff)(transfer, auto)
296
+
297
+ lemma View'_transfer [transfer_rule]: includes lifting_syntax shows
298
+ "(rel_prod (=) (list_all2 pcr_view) ===> pcr_view) Tree View'"
299
+ by(simp add: view.pcr_cr_eq cr_view_Grp eq_alt prod.rel_Grp rose_tree.rel_Grp list.rel_Grp)
300
+ (auto simp add: Grp_def View'_def)
301
+
302
+ end
303
+
304
+ code_datatype View
305
+
306
+ context begin
307
+ interpretation view_merkle .
308
+
309
+ abbreviation embed_view_content :: "view_metadata \<times> view_data \<Rightarrow> view_metadata\<^sub>m \<times> view_data\<^sub>m" where
310
+ "embed_view_content \<equiv> map_prod Unblinded Unblinded"
311
+
312
+ lift_definition embed_view :: "view \<Rightarrow> view\<^sub>m" is "embed_source_tree embed_view_content" .
313
+
314
+ lemma embed_view_simps [simp]:
315
+ "embed_view (View vm vd vs) = View\<^sub>m (Unblinded ((Unblinded vm, Unblinded vd), map embed_view vs))"
316
+ unfolding View_View' by transfer simp
317
+
318
+ end
319
+
320
+ context transaction_merkle begin
321
+
322
+ primrec the_Transaction :: "transaction \<Rightarrow> (common_metadata \<times> participant_metadata) \<times> view list" where
323
+ "the_Transaction (Transaction cm pm views) = ((cm, pm), views)" for views
324
+
325
+ definition Transaction' :: "(common_metadata \<times> participant_metadata) \<times> view list \<Rightarrow> transaction" where
326
+ "Transaction' = (\<lambda>((cm, pm), views). Transaction cm pm views)"
327
+
328
+ lemma Transaction_Transaction': "Transaction = (\<lambda>cm pm views. Transaction' ((cm, pm), views))"
329
+ by(simp add: Transaction'_def)
330
+
331
+ lemma the_Transaction_inverse [simp]: "Transaction' (the_Transaction x) = x"
332
+ by(cases x)(simp add: Transaction'_def)
333
+
334
+ lemma Transaction'_inverse [simp]: "the_Transaction (Transaction' x) = x"
335
+ by(simp add: Transaction'_def split_def)
336
+
337
+ lemma iso_transaction: "type_definition the_Transaction Transaction' UNIV"
338
+ by unfold_locales simp_all
339
+
340
+ setup_lifting iso_transaction
341
+
342
+ lemma Transaction'_transfer [transfer_rule]: includes lifting_syntax shows
343
+ "((=) ===> pcr_transaction) id Transaction'"
344
+ by(simp add: transaction.pcr_cr_eq cr_transaction_def rel_fun_def)
345
+
346
+ end
347
+
348
+ code_datatype Transaction
349
+
350
+ context begin
351
+ interpretation transaction_merkle .
352
+
353
+ lift_definition embed_transaction :: "transaction \<Rightarrow> transaction\<^sub>m" is
354
+ "Unblinded \<circ> map_prod (map_prod Unblinded Unblinded) (map embed_view)" .
355
+
356
+ lemma embed_transaction_simps [simp]:
357
+ "embed_transaction (Transaction cm pm views) =
358
+ Transaction\<^sub>m (Unblinded ((Unblinded cm, Unblinded pm), map embed_view views))"
359
+ for views unfolding Transaction_Transaction' by transfer simp
360
+
361
+ end
362
+
363
+ subsubsection \<open>Inclusion proof for the mediator\<close>
364
+
365
+ primrec mediator_view :: "view \<Rightarrow> view\<^sub>m" where
366
+ "mediator_view (View vm vd vs) =
367
+ View\<^sub>m (Unblinded ((Unblinded vm, Blinded (Content vd)), map mediator_view vs))"
368
+
369
+ primrec mediator_transaction_tree :: "transaction \<Rightarrow> transaction\<^sub>m" where
370
+ "mediator_transaction_tree (Transaction cm pm views) =
371
+ Transaction\<^sub>m (Unblinded ((Unblinded cm, Blinded (Content pm)), map mediator_view views))"
372
+ for views
373
+
374
+ lemma blinding_of_mediator_view [simp]: "blinding_of_view (mediator_view view) (embed_view view)"
375
+ by(induction view)(auto simp add: list.rel_map intro!: list.rel_refl_strong)
376
+
377
+ lemma blinding_of_mediator_transaction_tree:
378
+ "blinding_of_transaction (mediator_transaction_tree tt) (embed_transaction tt)"
379
+ by(cases tt)(auto simp add: list.rel_map intro: list.rel_refl_strong)
380
+
381
+ subsubsection \<open>Inclusion proofs for participants\<close>
382
+
383
+ text \<open>Next, we define a function for producing all transaction views from a given view,
384
+ and prove its properties.\<close>
385
+
386
+ type_synonym view_path_elem = "(view_metadata \<times> view_data) blindable \<times> view list \<times> view list"
387
+ type_synonym view_path = "view_path_elem list"
388
+ type_synonym view_zipper = "view_path \<times> view"
389
+
390
+ type_synonym view_path_elem\<^sub>m = "(view_metadata\<^sub>m \<times>\<^sub>m view_data\<^sub>m) \<times> view\<^sub>m list\<^sub>m \<times> view\<^sub>m list\<^sub>m"
391
+ type_synonym view_path\<^sub>m = "view_path_elem\<^sub>m list"
392
+ type_synonym view_zipper\<^sub>m = "view_path\<^sub>m \<times> view\<^sub>m"
393
+
394
+ context begin
395
+ interpretation view_merkle .
396
+
397
+ lift_definition zipper_of_view :: "view \<Rightarrow> view_zipper" is zipper_of_tree .
398
+ lift_definition view_of_zipper :: "view_zipper \<Rightarrow> view" is tree_of_zipper .
399
+
400
+ lift_definition zipper_of_view\<^sub>m :: "view\<^sub>m \<Rightarrow> view_zipper\<^sub>m" is zipper_of_tree\<^sub>m .
401
+ lift_definition view_of_zipper\<^sub>m :: "view_zipper\<^sub>m \<Rightarrow> view\<^sub>m" is tree_of_zipper\<^sub>m .
402
+
403
+ lemma view_of_zipper\<^sub>m_Nil [simp]: "view_of_zipper\<^sub>m ([], t) = t"
404
+ by transfer simp
405
+
406
+ lift_definition blind_view_path_elem :: "view_path_elem \<Rightarrow> view_path_elem\<^sub>m" is
407
+ "blind_path_elem embed_view_content hash_view_content" .
408
+
409
+ lift_definition blind_view_path :: "view_path \<Rightarrow> view_path\<^sub>m" is
410
+ "blind_path embed_view_content hash_view_content" .
411
+
412
+ lift_definition embed_view_path_elem :: "view_path_elem \<Rightarrow> view_path_elem\<^sub>m" is
413
+ "embed_path_elem embed_view_content" .
414
+
415
+ lift_definition embed_view_path :: "view_path \<Rightarrow> view_path\<^sub>m" is
416
+ "embed_path embed_view_content" .
417
+
418
+ lift_definition hash_view_path_elem :: "view_path_elem\<^sub>m \<Rightarrow> (view_content\<^sub>h \<times> view\<^sub>h list \<times> view\<^sub>h list)" is
419
+ "hash_path_elem hash_view_content" .
420
+
421
+ lift_definition zippers_view :: "view_zipper \<Rightarrow> view_zipper\<^sub>m list" is
422
+ "zippers_rose_tree embed_view_content hash_view_content" .
423
+
424
+ lemma embed_view_path_Nil [simp]: "embed_view_path [] = []"
425
+ by transfer(simp add: embed_path_def)
426
+
427
+ lemma zippers_view_same_hash:
428
+ assumes "z \<in> set (zippers_view (p, t))"
429
+ shows "hash_view (view_of_zipper\<^sub>m z) = hash_view (view_of_zipper\<^sub>m (embed_view_path p, embed_view t))"
430
+ using assms by transfer(rule zippers_rose_tree_same_hash')
431
+
432
+ lemma zippers_view_blinding_of:
433
+ assumes "z \<in> set (zippers_view (p, t))"
434
+ shows "blinding_of_view (view_of_zipper\<^sub>m z) (view_of_zipper\<^sub>m (blind_view_path p, embed_view t))"
435
+ using assms by transfer(rule zippers_rose_tree_blinding_of, unfold_locales)
436
+
437
+ end
438
+
439
+ primrec blind_view :: "view \<Rightarrow> view\<^sub>m" where
440
+ "blind_view (View vm vd subviews) =
441
+ View\<^sub>m (Blinded (Content ((Content vm, Content vd), map (hash_view \<circ> embed_view) subviews)))"
442
+ for subviews
443
+
444
+ lemma hash_blind_view: "hash_view (blind_view view) = hash_view (embed_view view)"
445
+ by(cases view) simp
446
+
447
+ primrec blind_transaction :: "transaction \<Rightarrow> transaction\<^sub>m" where
448
+ "blind_transaction (Transaction cm pm views) =
449
+ Transaction\<^sub>m (Blinded (Content ((Content cm, Content pm), map (hash_view \<circ> blind_view) views)))"
450
+ for views
451
+
452
+ lemma hash_blind_transaction:
453
+ "hash_transaction (blind_transaction transaction) = hash_transaction (embed_transaction transaction)"
454
+ by(cases transaction)(simp add: hash_blind_view)
455
+
456
+
457
+ typedecl participant
458
+ consts recipients :: "view_metadata \<Rightarrow> participant list"
459
+
460
+ fun view_recipients :: "view\<^sub>m \<Rightarrow> participant set" where
461
+ "view_recipients (View\<^sub>m (Unblinded ((Unblinded vm, vd), subviews))) = set (recipients vm)" for subviews
462
+ | "view_recipients _ = {}" \<comment> \<open>Sane default case\<close>
463
+
464
+ context fixes participant :: participant begin
465
+
466
+ definition view_trees_for :: "view \<Rightarrow> view\<^sub>m list" where
467
+ "view_trees_for view =
468
+ map view_of_zipper\<^sub>m
469
+ (filter (\<lambda>(_, t). participant \<in> view_recipients t)
470
+ (zippers_view ([], view)))"
471
+
472
+ primrec transaction_views_for :: "transaction \<Rightarrow> transaction\<^sub>m list" where
473
+ "transaction_views_for (Transaction cm pm views) =
474
+ map (\<lambda>view\<^sub>m. Transaction\<^sub>m (Unblinded ((Unblinded cm, Unblinded pm), view\<^sub>m)))
475
+ (concat (map (\<lambda>(l, v, r). map (\<lambda>v\<^sub>m. map blind_view l @ [v\<^sub>m] @ map blind_view r) (view_trees_for v)) (splits views)))"
476
+ for views
477
+
478
+ lemma view_trees_for_same_hash:
479
+ "vt \<in> set (view_trees_for view) \<Longrightarrow> hash_view vt = hash_view (embed_view view)"
480
+ by(auto simp add: view_trees_for_def dest: zippers_view_same_hash)
481
+
482
+ lemma transaction_views_for_same_hash:
483
+ "t\<^sub>m \<in> set (transaction_views_for t) \<Longrightarrow> hash_transaction t\<^sub>m = hash_transaction (embed_transaction t)"
484
+ by(cases t)(clarsimp simp add: splits_iff hash_blind_view view_trees_for_same_hash)
485
+
486
+ definition transaction_projection_for :: "transaction \<Rightarrow> transaction\<^sub>m" where
487
+ "transaction_projection_for t =
488
+ (let tvs = transaction_views_for t
489
+ in if tvs = [] then blind_transaction t else the (transaction.Merge (set tvs)))"
490
+
491
+ lemma transaction_projection_for_same_hash:
492
+ "hash_transaction (transaction_projection_for t) = hash_transaction (embed_transaction t)"
493
+ proof(cases "transaction_views_for t = []")
494
+ case True thus ?thesis by(simp add: transaction_projection_for_def Let_def hash_blind_transaction)
495
+ next
496
+ case False
497
+ then have "transaction.Merge (set (transaction_views_for t)) \<noteq> None"
498
+ by(intro transaction.Merge_defined)(auto simp add: transaction_views_for_same_hash)
499
+ with False show ?thesis
500
+ apply(clarsimp simp add: transaction_projection_for_def neq_Nil_conv simp del: transaction.Merge_insert)
501
+ apply(drule transaction.Merge_hash[symmetric], blast)
502
+ apply(auto intro: transaction_views_for_same_hash)
503
+ done
504
+ qed
505
+
506
+ lemma transaction_projection_for_upper:
507
+ assumes "t\<^sub>m \<in> set (transaction_views_for t)"
508
+ shows "blinding_of_transaction t\<^sub>m (transaction_projection_for t)"
509
+ proof -
510
+ from assms have "transaction.Merge (set (transaction_views_for t)) \<noteq> None"
511
+ by(intro transaction.Merge_defined)(auto simp add: transaction_views_for_same_hash)
512
+ with assms show ?thesis
513
+ by(auto simp add: transaction_projection_for_def Let_def dest: transaction.Merge_upper)
514
+ qed
515
+
516
+ end
517
+
518
+ end
formal/afp/ADS_Functor/Generic_ADS_Construction.thy ADDED
@@ -0,0 +1,469 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Author: Andreas Lochbihler, Digital Asset
2
+ Author: Ognjen Maric, Digital Asset *)
3
+
4
+ theory Generic_ADS_Construction imports
5
+ "Merkle_Interface"
6
+ "HOL-Library.BNF_Axiomatization"
7
+ begin
8
+
9
+ section \<open>Generic construction of authenticated data structures\<close>
10
+
11
+ subsection \<open>Functors\<close>
12
+
13
+ subsubsection \<open>Source functor\<close>
14
+
15
+ text \<open>
16
+
17
+ We want to allow ADSs of arbitrary ADTs, which we call "source trees". The ADTs we are interested in can
18
+ in general be represented as the least fixpoints of some bounded natural (bi-)functor (BNF) \<open>('a, 'b) F\<close>, where
19
+ @{typ 'a} is the type of "source" data, and @{typ 'b} is a recursion "handle".
20
+ However, Isabelle's type system does not support higher kinds, necessary to parameterize our definitions
21
+ over functors.
22
+ Instead, we first develop a general theory of ADSs over an arbitrary, but fixed functor,
23
+ and its least fixpoint. We show that the theory is compositional, in that the functor's least fixed point
24
+ can then be reused as the "source" data of another functor.
25
+
26
+ We start by defining the arbitrary fixed functor, its fixpoints, and showing how composition can be
27
+ done. A higher-level explanation is found in the paper.
28
+ \<close>
29
+
30
+
31
+ bnf_axiomatization ('a, 'b) F [wits: "'a \<Rightarrow> ('a, 'b) F"]
32
+
33
+ context notes [[typedef_overloaded]] begin
34
+ datatype 'a T = T "('a, 'a T) F"
35
+ end
36
+
37
+ subsubsection \<open>Base Merkle functor\<close>
38
+
39
+ text \<open>
40
+ This type captures the ADS hashes.
41
+ \<close>
42
+
43
+ bnf_axiomatization ('a, 'b) F\<^sub>h [wits: "'a \<Rightarrow> ('a, 'b) F\<^sub>h"]
44
+
45
+ text \<open>
46
+ It intuitively contains mixed garbage and source values.
47
+ The functor's recursive handle @{typ 'b} might contain partial garbage.
48
+ \<close>
49
+
50
+
51
+ text \<open>
52
+ This type captures the ADS inclusion proofs.
53
+ The functor \<open>('a, 'a', 'b, 'b') F\<^sub>m\<close> has all type variables doubled.
54
+ This type represents all values including the information which parts are blinded.
55
+ The original type variable @{typ 'a} now represents the source data, which for compositionality can contain blindable positions.
56
+ The type @{typ 'b} is a recursive handle to inclusion sub-proofs (which can be partialy blinded).
57
+ The type @{typ 'a'} represent "hashes" of the source data in @{typ 'a}, i.e., a mix of source values and garbage.
58
+ The type @{typ 'b'} is a recursive handle to ADS hashes of subtrees.
59
+
60
+ The corresponding type of recursive authenticated trees is then a fixpoint of this functor.
61
+ \<close>
62
+
63
+ bnf_axiomatization ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) F\<^sub>m [wits: "'a\<^sub>m \<Rightarrow> 'a\<^sub>h \<Rightarrow> 'b\<^sub>h \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) F\<^sub>m"]
64
+
65
+ subsubsection \<open>Least fixpoint\<close>
66
+
67
+ context notes [[typedef_overloaded]] begin
68
+ datatype 'a\<^sub>h T\<^sub>h = T\<^sub>h "('a\<^sub>h, 'a\<^sub>h T\<^sub>h) F\<^sub>h"
69
+ end
70
+
71
+ context notes [[typedef_overloaded]] begin
72
+ datatype ('a\<^sub>m, 'a\<^sub>h) T\<^sub>m = T\<^sub>m (the_T\<^sub>m: "('a\<^sub>m, 'a\<^sub>h, ('a\<^sub>m, 'a\<^sub>h) T\<^sub>m, 'a\<^sub>h T\<^sub>h) F\<^sub>m")
73
+ end
74
+
75
+
76
+ subsubsection \<open> Composition \<close>
77
+
78
+ text \<open>
79
+ Finally, we show how to compose two Merkle functors.
80
+ For simplicity, we reuse @{typ \<open>('a, 'b) F\<close>} and @{typ \<open>'a T\<close>}.
81
+ \<close>
82
+
83
+ context notes [[typedef_overloaded]] begin
84
+
85
+ datatype ('a, 'b) G = G "('a T, 'b) F"
86
+
87
+ datatype ('a\<^sub>h, 'b\<^sub>h) G\<^sub>h = G\<^sub>h (the_G\<^sub>h: "('a\<^sub>h T\<^sub>h, 'b\<^sub>h) F\<^sub>h")
88
+
89
+ datatype ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) G\<^sub>m = G\<^sub>m (the_G\<^sub>m: "(('a\<^sub>m, 'a\<^sub>h) T\<^sub>m, 'a\<^sub>h T\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) F\<^sub>m")
90
+
91
+ end
92
+
93
+
94
+ subsection \<open>Root hash\<close>
95
+
96
+ subsubsection \<open>Base functor\<close>
97
+
98
+ text \<open>
99
+ The root hash of an authenticated value is modelled as a blindable value of type @{typ "('a', 'b') F\<^sub>h"}.
100
+ (Actually, we want to use an abstract datatype for root hashes, but we omit this distinction here for simplicity.)
101
+ \<close>
102
+
103
+ consts root_hash_F' :: "(('a\<^sub>h, 'a\<^sub>h, 'b\<^sub>h, 'b\<^sub>h) F\<^sub>m, ('a\<^sub>h, 'b\<^sub>h) F\<^sub>h) hash"
104
+ \<comment> \<open>Root hash operation where we assume that all atoms have already been replaced by root hashes.
105
+ This assumption is reflected in the equality of the type parameters of @{type F\<^sub>m} \<close>
106
+
107
+ type_synonym ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) hash_F =
108
+ "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> (('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) F\<^sub>m, ('a\<^sub>h, 'b\<^sub>h) F\<^sub>h) hash"
109
+ definition root_hash_F :: "('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) hash_F" where
110
+ "root_hash_F rha rhb = root_hash_F' \<circ> map_F\<^sub>m rha id rhb id"
111
+
112
+ subsubsection \<open>Least fixpoint\<close>
113
+
114
+ primrec root_hash_T' :: "(('a\<^sub>h, 'a\<^sub>h) T\<^sub>m, 'a\<^sub>h T\<^sub>h) hash" where
115
+ "root_hash_T' (T\<^sub>m x) = T\<^sub>h (root_hash_F' (map_F\<^sub>m id id root_hash_T' id x))"
116
+
117
+ definition root_hash_T :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> (('a\<^sub>m, 'a\<^sub>h) T\<^sub>m, 'a\<^sub>h T\<^sub>h) hash" where
118
+ "root_hash_T rha = root_hash_T' \<circ> map_T\<^sub>m rha id"
119
+
120
+ lemma root_hash_T_simps [simp]:
121
+ "root_hash_T rha (T\<^sub>m x) = T\<^sub>h (root_hash_F rha (root_hash_T rha) x)"
122
+ by(simp add: root_hash_T_def F\<^sub>m.map_comp root_hash_F_def T\<^sub>h.map_id0)
123
+
124
+ subsubsection \<open>Composition\<close>
125
+
126
+ primrec root_hash_G' :: "(('a\<^sub>h, 'a\<^sub>h, 'b\<^sub>h, 'b\<^sub>h) G\<^sub>m, ('a\<^sub>h, 'b\<^sub>h) G\<^sub>h) hash" where
127
+ "root_hash_G' (G\<^sub>m x) = G\<^sub>h (root_hash_F' (map_F\<^sub>m root_hash_T' id id id x))"
128
+
129
+ definition root_hash_G :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> (('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) G\<^sub>m, ('a\<^sub>h, 'b\<^sub>h) G\<^sub>h) hash" where
130
+ "root_hash_G rha rhb = root_hash_G' \<circ> map_G\<^sub>m rha id rhb id"
131
+
132
+ lemma root_hash_G_unfold:
133
+ "root_hash_G rha rhb = G\<^sub>h \<circ> root_hash_F (root_hash_T rha) rhb \<circ> the_G\<^sub>m"
134
+ apply(rule ext)
135
+ subgoal for x
136
+ by(cases x)(simp add: root_hash_G_def fun_eq_iff root_hash_F_def root_hash_T_def F\<^sub>m.map_comp T\<^sub>m.map_comp o_def T\<^sub>h.map_id id_def[symmetric])
137
+ done
138
+
139
+ lemma root_hash_G_simps [simp]:
140
+ "root_hash_G rha rhb (G\<^sub>m x) = G\<^sub>h (root_hash_F (root_hash_T rha) rhb x)"
141
+ by(simp add: root_hash_G_def root_hash_T_def F\<^sub>m.map_comp root_hash_F_def T\<^sub>h.map_id0)
142
+
143
+
144
+ subsection \<open>Blinding relation\<close>
145
+
146
+ text \<open>
147
+ The blinding relation determines whether one ADS value is a blinding of another.
148
+ \<close>
149
+
150
+ subsubsection \<open> Blinding on the base functor (@{type F\<^sub>m}) \<close>
151
+
152
+ type_synonym ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) blinding_of_F =
153
+ "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> 'a\<^sub>m blinding_of \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> 'b\<^sub>m blinding_of \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) F\<^sub>m blinding_of"
154
+
155
+ \<comment> \<open> Computes whether a partially blinded ADS is a blinding of another one \<close>
156
+ axiomatization blinding_of_F :: "('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) blinding_of_F" where
157
+ blinding_of_F_mono: "\<lbrakk> boa \<le> boa'; bob \<le> bob' \<rbrakk>
158
+ \<Longrightarrow> blinding_of_F rha boa rhb bob \<le> blinding_of_F rha boa' rhb bob'"
159
+ \<comment> \<open> Monotonicity must be unconditional (without the assumption @{text "blinding_of_on"})
160
+ such that we can justify the recursive definition for the least fixpoint. \<close>
161
+ and blinding_respects_hashes_F [locale_witness]:
162
+ "\<lbrakk> blinding_respects_hashes rha boa; blinding_respects_hashes rhb bob \<rbrakk>
163
+ \<Longrightarrow> blinding_respects_hashes (root_hash_F rha rhb) (blinding_of_F rha boa rhb bob)"
164
+ and blinding_of_on_F [locale_witness]:
165
+ "\<lbrakk> blinding_of_on A rha boa; blinding_of_on B rhb bob \<rbrakk>
166
+ \<Longrightarrow> blinding_of_on {x. set1_F\<^sub>m x \<subseteq> A \<and> set3_F\<^sub>m x \<subseteq> B} (root_hash_F rha rhb) (blinding_of_F rha boa rhb bob)"
167
+
168
+ lemma blinding_of_F_mono_inductive:
169
+ assumes a: "\<And>x y. boa x y \<longrightarrow> boa' x y"
170
+ and b: "\<And>x y. bob x y \<longrightarrow> bob' x y"
171
+ shows "blinding_of_F rha boa rhb bob x y \<longrightarrow> blinding_of_F rha boa' rhb bob' x y"
172
+ using assms by(blast intro: blinding_of_F_mono[THEN predicate2D, OF predicate2I predicate2I])
173
+
174
+ subsubsection \<open> Blinding on least fixpoints \<close>
175
+
176
+ context
177
+ fixes rh :: "('a\<^sub>m, 'a\<^sub>h) hash"
178
+ and bo :: "'a\<^sub>m blinding_of"
179
+ begin
180
+
181
+ inductive blinding_of_T :: "('a\<^sub>m, 'a\<^sub>h) T\<^sub>m blinding_of" where
182
+ "blinding_of_T (T\<^sub>m x) (T\<^sub>m y)" if
183
+ "blinding_of_F rh bo (root_hash_T rh) blinding_of_T x y"
184
+ monos blinding_of_F_mono_inductive
185
+
186
+ end
187
+
188
+ lemma blinding_of_T_mono:
189
+ assumes "bo \<le> bo'"
190
+ shows "blinding_of_T rh bo \<le> blinding_of_T rh bo'"
191
+ by(rule predicate2I; erule blinding_of_T.induct)
192
+ (blast intro: blinding_of_T.intros blinding_of_F_mono[THEN predicate2D, OF assms, rotated -1])
193
+
194
+ lemma blinding_of_T_root_hash:
195
+ assumes "bo \<le> vimage2p rh rh (=)"
196
+ shows "blinding_of_T rh bo \<le> vimage2p (root_hash_T rh) (root_hash_T rh) (=)"
197
+ apply(rule predicate2I vimage2pI)+
198
+ apply(erule blinding_of_T.induct)
199
+ apply simp
200
+ apply(drule blinding_respects_hashes_F[unfolded blinding_respects_hashes_def, THEN predicate2D, rotated -1])
201
+ apply(rule assms)
202
+ apply(blast intro: vimage2pI)
203
+ apply(simp add: vimage2p_def)
204
+ done
205
+
206
+ lemma blinding_respects_hashes_T [locale_witness]:
207
+ "blinding_respects_hashes rh bo \<Longrightarrow> blinding_respects_hashes (root_hash_T rh) (blinding_of_T rh bo)"
208
+ unfolding blinding_respects_hashes_def by(rule blinding_of_T_root_hash)
209
+
210
+ lemma blinding_of_on_T [locale_witness]:
211
+ assumes "blinding_of_on A rh bo"
212
+ shows "blinding_of_on {x. set1_T\<^sub>m x \<subseteq> A} (root_hash_T rh) (blinding_of_T rh bo)"
213
+ (is "blinding_of_on ?A ?h ?bo")
214
+ proof -
215
+ interpret a: blinding_of_on A rh bo by fact
216
+ show ?thesis
217
+ proof
218
+ have "?bo x x \<and> (?bo x y \<longrightarrow> ?bo y z \<longrightarrow> ?bo x z) \<and> (?bo x y \<longrightarrow> ?bo y x \<longrightarrow> x = y)"
219
+ if "x \<in> ?A" for x y z using that
220
+ proof(induction x arbitrary: y z)
221
+ case (T\<^sub>m x)
222
+ interpret blinding_of_on
223
+ "{a. set1_F\<^sub>m a \<subseteq> A \<and> set3_F\<^sub>m a \<subseteq> set3_F\<^sub>m x}"
224
+ "root_hash_F rh ?h"
225
+ "blinding_of_F rh bo ?h ?bo"
226
+ apply(rule blinding_of_on_F[OF assms])
227
+ apply unfold_locales
228
+ subgoal using T\<^sub>m.IH T\<^sub>m.prems by(force simp add: eq_onp_def)
229
+ subgoal for a b c using T\<^sub>m.IH[of a b c] T\<^sub>m.prems by auto
230
+ subgoal for a b using T\<^sub>m.IH[of a b] T\<^sub>m.prems by auto
231
+ done
232
+ show ?case using T\<^sub>m.prems
233
+ apply(intro conjI)
234
+ subgoal by(auto intro: blinding_of_T.intros refl)
235
+ subgoal by(auto elim!: blinding_of_T.cases trans intro!: blinding_of_T.intros)
236
+ subgoal by(auto elim!: blinding_of_T.cases dest: antisym)
237
+ done
238
+ qed
239
+ then show "x \<in> ?A \<Longrightarrow> ?bo x x"
240
+ and "\<lbrakk> ?bo x y; ?bo y z; x \<in> ?A \<rbrakk> \<Longrightarrow> ?bo x z"
241
+ and "\<lbrakk> ?bo x y; ?bo y x; x \<in> ?A \<rbrakk> \<Longrightarrow> x = y"
242
+ for x y z by blast+
243
+ qed
244
+ qed
245
+
246
+ lemmas blinding_of_T [locale_witness] = blinding_of_on_T[where A=UNIV, simplified]
247
+
248
+ subsubsection \<open> Blinding on composition \<close>
249
+
250
+ context
251
+ fixes rha :: "('a\<^sub>m, 'a\<^sub>h) hash"
252
+ and boa :: "'a\<^sub>m blinding_of"
253
+ and rhb :: "('b\<^sub>m, 'b\<^sub>h) hash"
254
+ and bob :: "'b\<^sub>m blinding_of"
255
+ begin
256
+
257
+ inductive blinding_of_G :: "('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) G\<^sub>m blinding_of" where
258
+ "blinding_of_G (G\<^sub>m x) (G\<^sub>m y)" if
259
+ "blinding_of_F (root_hash_T rha) (blinding_of_T rha boa) rhb bob x y"
260
+
261
+ lemma blinding_of_G_unfold:
262
+ "blinding_of_G = vimage2p the_G\<^sub>m the_G\<^sub>m (blinding_of_F (root_hash_T rha) (blinding_of_T rha boa) rhb bob)"
263
+ apply(rule ext)+
264
+ subgoal for x y by(cases x; cases y)(simp_all add: blinding_of_G.simps fun_eq_iff vimage2p_def)
265
+ done
266
+
267
+ end
268
+
269
+ lemma blinding_of_G_mono:
270
+ assumes "boa \<le> boa'" "bob \<le> bob'"
271
+ shows "blinding_of_G rha boa rhb bob \<le> blinding_of_G rha boa' rhb bob'"
272
+ unfolding blinding_of_G_unfold
273
+ by(rule vimage2p_mono' blinding_of_F_mono blinding_of_T_mono assms)+
274
+
275
+ lemma blinding_of_G_root_hash:
276
+ assumes "boa \<le> vimage2p rha rha (=)" and "bob \<le> vimage2p rhb rhb (=)"
277
+ shows "blinding_of_G rha boa rhb bob \<le> vimage2p (root_hash_G rha rhb) (root_hash_G rha rhb) (=)"
278
+ unfolding blinding_of_G_unfold root_hash_G_unfold vimage2p_comp o_apply
279
+ apply(rule vimage2p_mono')
280
+ apply(rule order_trans)
281
+ apply(rule blinding_respects_hashes_F[unfolded blinding_respects_hashes_def])
282
+ apply(rule blinding_of_T_root_hash)
283
+ apply(rule assms)+
284
+ apply(rule vimage2p_mono')
285
+ apply(simp add: vimage2p_def)
286
+ done
287
+
288
+ lemma blinding_of_on_G [locale_witness]:
289
+ assumes "blinding_of_on A rha boa" "blinding_of_on B rhb bob"
290
+ shows "blinding_of_on {x. set1_G\<^sub>m x \<subseteq> A \<and> set3_G\<^sub>m x \<subseteq> B} (root_hash_G rha rhb) (blinding_of_G rha boa rhb bob)"
291
+ (is "blinding_of_on ?A ?h ?bo")
292
+ proof -
293
+ interpret a: blinding_of_on A rha boa by fact
294
+ interpret b: blinding_of_on B rhb bob by fact
295
+ interpret FT: blinding_of_on
296
+ "{x. set1_F\<^sub>m x \<subseteq> {x. set1_T\<^sub>m x \<subseteq> A} \<and> set3_F\<^sub>m x \<subseteq> B}"
297
+ "root_hash_F (root_hash_T rha) rhb"
298
+ "blinding_of_F (root_hash_T rha) (blinding_of_T rha boa) rhb bob"
299
+ ..
300
+ show ?thesis
301
+ proof
302
+ show "?bo \<le> vimage2p ?h ?h (=)"
303
+ using a.hash b.hash
304
+ by(rule blinding_of_G_root_hash)
305
+ show "?bo x x" if "x \<in> ?A" for x using that
306
+ by(cases x; hypsubst)(rule blinding_of_G.intros; rule FT.refl; auto)
307
+ show "?bo x z" if "?bo x y" "?bo y z" "x \<in> ?A" for x y z using that
308
+ by(fastforce elim!: blinding_of_G.cases intro!: blinding_of_G.intros elim!: FT.trans)
309
+ show "x = y" if "?bo x y" "?bo y x" "x \<in> ?A" for x y using that
310
+ by(clarsimp elim!: blinding_of_G.cases)(erule (1) FT.antisym; auto)
311
+ qed
312
+ qed
313
+
314
+ lemmas blinding_of_G [locale_witness] = blinding_of_on_G[where A=UNIV and B=UNIV, simplified]
315
+
316
+ subsection \<open>Merging\<close>
317
+
318
+ text \<open>Two Merkle values with the same root hash can be merged into a less blinded Merkle value.
319
+ The operation is unspecified for trees with different root hashes.
320
+ \<close>
321
+
322
+ subsubsection \<open> Merging on the base functor \<close>
323
+
324
+ axiomatization merge_F :: "('a\<^sub>m, 'a\<^sub>h) hash \<Rightarrow> 'a\<^sub>m merge \<Rightarrow> ('b\<^sub>m, 'b\<^sub>h) hash \<Rightarrow> 'b\<^sub>m merge
325
+ \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) F\<^sub>m merge" where
326
+ merge_F_cong [fundef_cong]:
327
+ "\<lbrakk> \<And>a b. a \<in> set1_F\<^sub>m x \<Longrightarrow> ma a b = ma' a b; \<And>a b. a \<in> set3_F\<^sub>m x \<Longrightarrow> mb a b = mb' a b \<rbrakk>
328
+ \<Longrightarrow> merge_F rha ma rhb mb x y = merge_F rha ma' rhb mb' x y"
329
+ and
330
+ merge_on_F [locale_witness]:
331
+ "\<lbrakk> merge_on A rha boa ma; merge_on B rhb bob mb \<rbrakk>
332
+ \<Longrightarrow> merge_on {x. set1_F\<^sub>m x \<subseteq> A \<and> set3_F\<^sub>m x \<subseteq> B} (root_hash_F rha rhb) (blinding_of_F rha boa rhb bob) (merge_F rha ma rhb mb)"
333
+
334
+ lemmas merge_F [locale_witness] = merge_on_F[where A=UNIV and B=UNIV, simplified]
335
+
336
+ subsubsection \<open> Merging on the least fixpoint \<close>
337
+
338
+ lemma wfP_subterm_T: "wfP (\<lambda>x y. x \<in> set3_F\<^sub>m (the_T\<^sub>m y))"
339
+ apply(rule wfPUNIVI)
340
+ subgoal premises IH[rule_format] for P x
341
+ by(induct x)(auto intro: IH)
342
+ done
343
+
344
+ lemma irrefl_subterm_T: "x \<in> set3_F\<^sub>m y \<Longrightarrow> y \<noteq> the_T\<^sub>m x"
345
+ using wfP_subterm_T by (auto simp: wfP_def elim!: wf_irrefl)
346
+
347
+ context
348
+ fixes rh :: "('a\<^sub>m, 'a\<^sub>h) hash"
349
+ fixes m :: "'a\<^sub>m merge"
350
+ begin
351
+
352
+ function merge_T :: "('a\<^sub>m, 'a\<^sub>h) T\<^sub>m merge" where
353
+ "merge_T (T\<^sub>m x) (T\<^sub>m y) = map_option T\<^sub>m (merge_F rh m (root_hash_T rh) merge_T x y)"
354
+ by pat_completeness auto
355
+ termination
356
+ apply(relation "{(x, y). x \<in> set3_F\<^sub>m (the_T\<^sub>m y)} <*lex*> {(x, y). x \<in> set3_F\<^sub>m (the_T\<^sub>m y)}")
357
+ apply(auto simp add: wfP_def[symmetric] wfP_subterm_T)
358
+ done
359
+
360
+ lemma merge_on_T [locale_witness]:
361
+ assumes "merge_on A rh bo m"
362
+ shows "merge_on {x. set1_T\<^sub>m x \<subseteq> A} (root_hash_T rh) (blinding_of_T rh bo) merge_T"
363
+ (is "merge_on ?A ?h ?bo ?m")
364
+ proof -
365
+ interpret a: merge_on A rh bo m by fact
366
+ show ?thesis
367
+ proof
368
+ have "(?h a = ?h b \<longrightarrow> (\<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u))) \<and>
369
+ (?h a \<noteq> ?h b \<longrightarrow> ?m a b = None)"
370
+ if "a \<in> ?A" for a b using that unfolding mem_Collect_eq
371
+ proof(induction a arbitrary: b)
372
+ case (T\<^sub>m x y)
373
+ interpret merge_on "{y. set1_F\<^sub>m y \<subseteq> A \<and> set3_F\<^sub>m y \<subseteq> set3_F\<^sub>m x}"
374
+ "root_hash_F rh ?h" "blinding_of_F rh bo ?h ?bo" "merge_F rh m ?h ?m"
375
+ proof
376
+ fix a
377
+ assume a: "a \<in> set3_F\<^sub>m x"
378
+ with T\<^sub>m.prems have a': "set1_T\<^sub>m a \<subseteq> A" by auto
379
+
380
+ fix b
381
+ from T\<^sub>m.IH[OF a a', rule_format, of b]
382
+ show "root_hash_T rh a = root_hash_T rh b
383
+ \<Longrightarrow> \<exists>ab. merge_T a b = Some ab \<and> blinding_of_T rh bo a ab \<and> blinding_of_T rh bo b ab \<and>
384
+ (\<forall>u. blinding_of_T rh bo a u \<longrightarrow> blinding_of_T rh bo b u \<longrightarrow> blinding_of_T rh bo ab u)"
385
+ and "root_hash_T rh a \<noteq> root_hash_T rh b \<Longrightarrow> merge_T a b = None"
386
+ by(auto dest: sym)
387
+ qed
388
+ show ?case using T\<^sub>m.prems
389
+ apply(intro conjI strip)
390
+ subgoal by(cases y)(auto dest!: join simp add: blinding_of_T.simps)
391
+ subgoal by(cases y)(auto dest!: undefined)
392
+ done
393
+ qed
394
+ then show
395
+ "?h a = ?h b \<Longrightarrow> \<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)"
396
+ "?h a \<noteq> ?h b \<Longrightarrow> ?m a b = None"
397
+ if "a \<in> ?A" for a b using that by blast+
398
+ qed
399
+ qed
400
+
401
+ lemmas merge_T [locale_witness] = merge_on_T[where A=UNIV, simplified]
402
+
403
+ end
404
+
405
+ lemma merge_T_cong [fundef_cong]:
406
+ assumes "\<And>a b. a \<in> set1_T\<^sub>m x \<Longrightarrow> m a b = m' a b"
407
+ shows "merge_T rh m x y = merge_T rh m' x y"
408
+ using assms
409
+ apply(induction x y rule: merge_T.induct)
410
+ apply simp
411
+ apply(rule arg_cong[where f="map_option _"])
412
+ apply(blast intro: merge_F_cong)
413
+ done
414
+
415
+ subsubsection \<open> Merging and composition \<close>
416
+
417
+ context
418
+ fixes rha :: "('a\<^sub>m, 'a\<^sub>h) hash"
419
+ fixes ma :: "'a\<^sub>m merge"
420
+ fixes rhb :: "('b\<^sub>m, 'b\<^sub>h) hash"
421
+ fixes mb :: "'b\<^sub>m merge"
422
+ begin
423
+
424
+ primrec merge_G :: "('a\<^sub>m, 'a\<^sub>h, 'b\<^sub>m, 'b\<^sub>h) G\<^sub>m merge" where
425
+ "merge_G (G\<^sub>m x) y' = (case y' of G\<^sub>m y \<Rightarrow>
426
+ map_option G\<^sub>m (merge_F (root_hash_T rha) (merge_T rha ma) rhb mb x y))"
427
+
428
+ lemma merge_G_simps [simp]:
429
+ "merge_G (G\<^sub>m x) (G\<^sub>m y) = map_option G\<^sub>m (merge_F (root_hash_T rha) (merge_T rha ma) rhb mb x y)"
430
+ by(simp)
431
+
432
+ declare merge_G.simps [simp del]
433
+
434
+ lemma merge_on_G:
435
+ assumes a: "merge_on A rha boa ma" and b: "merge_on B rhb bob mb"
436
+ shows "merge_on {x. set1_G\<^sub>m x \<subseteq> A \<and> set3_G\<^sub>m x \<subseteq> B} (root_hash_G rha rhb) (blinding_of_G rha boa rhb bob) merge_G"
437
+ (is "merge_on ?A ?h ?bo ?m")
438
+ proof -
439
+ interpret a: merge_on A rha boa ma by fact
440
+ interpret b: merge_on B rhb bob mb by fact
441
+ interpret F: merge_on
442
+ "{x. set1_F\<^sub>m x \<subseteq> {x. set1_T\<^sub>m x \<subseteq> A} \<and> set3_F\<^sub>m x \<subseteq> B}"
443
+ "root_hash_F (root_hash_T rha) rhb"
444
+ "blinding_of_F (root_hash_T rha) (blinding_of_T rha boa) rhb bob"
445
+ "merge_F (root_hash_T rha) (merge_T rha ma) rhb mb"
446
+ ..
447
+ show ?thesis
448
+ proof
449
+ show "\<exists>ab. ?m a b = Some ab \<and> ?bo a ab \<and> ?bo b ab \<and> (\<forall>u. ?bo a u \<longrightarrow> ?bo b u \<longrightarrow> ?bo ab u)"
450
+ if "?h a = ?h b" "a \<in> ?A" for a b using that
451
+ by(cases a; cases b)(auto dest!: F.join simp add: blinding_of_G.simps)
452
+ show "?m a b = None" if "?h a \<noteq> ?h b" "a \<in> ?A" for a b using that
453
+ by(cases a; cases b)(auto dest!: F.undefined)
454
+ qed
455
+ qed
456
+
457
+ lemmas merge_G [locale_witness] = merge_on_G[where A=UNIV and B=UNIV, simplified]
458
+
459
+ end
460
+
461
+ lemma merge_G_cong [fundef_cong]:
462
+ "\<lbrakk> \<And>a b. a \<in> set1_G\<^sub>m x \<Longrightarrow> ma a b = ma' a b; \<And>a b. a \<in> set3_G\<^sub>m x \<Longrightarrow> mb a b = mb' a b \<rbrakk>
463
+ \<Longrightarrow> merge_G rha ma rhb mb x y = merge_G rha ma' rhb mb' x y"
464
+ apply(cases x; cases y; simp)
465
+ apply(rule arg_cong[where f="map_option _"])
466
+ apply(blast intro: merge_F_cong merge_T_cong)
467
+ done
468
+
469
+ end
formal/afp/ADS_Functor/Inclusion_Proof_Construction.thy ADDED
@@ -0,0 +1,430 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Author: Andreas Lochbihler, Digital Asset
2
+ Author: Ognjen Maric, Digital Asset *)
3
+
4
+ theory Inclusion_Proof_Construction imports
5
+ ADS_Construction
6
+ begin
7
+
8
+ primrec blind_blindable :: "('a\<^sub>m \<Rightarrow> 'a\<^sub>h) \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) blindable\<^sub>m \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) blindable\<^sub>m" where
9
+ "blind_blindable h (Blinded x) = Blinded x"
10
+ | "blind_blindable h (Unblinded x) = Blinded (Content (h x))"
11
+
12
+ lemma hash_blind_blindable [simp]: "hash_blindable h (blind_blindable h x) = hash_blindable h x"
13
+ by(cases x) simp_all
14
+
15
+ subsection \<open>Inclusion proof construction for rose trees\<close>
16
+
17
+ (************************************************************)
18
+ subsubsection \<open> Hashing, embedding and blinding source trees \<close>
19
+ (************************************************************)
20
+
21
+ context fixes h :: "'a \<Rightarrow> 'a\<^sub>h" begin
22
+ fun hash_source_tree :: "'a rose_tree \<Rightarrow> 'a\<^sub>h rose_tree\<^sub>h" where
23
+ "hash_source_tree (Tree (data, subtrees)) = Tree\<^sub>h (Content (h data, map hash_source_tree subtrees))"
24
+ end
25
+
26
+ context fixes e :: "'a \<Rightarrow> 'a\<^sub>m" begin
27
+ fun embed_source_tree :: "'a rose_tree \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m" where
28
+ "embed_source_tree (Tree (data, subtrees)) =
29
+ Tree\<^sub>m (Unblinded (e data, map embed_source_tree subtrees))"
30
+ end
31
+
32
+ context fixes h :: "'a \<Rightarrow> 'a\<^sub>h" begin
33
+ fun blind_source_tree :: "'a rose_tree \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m" where
34
+ "blind_source_tree (Tree (data, subtrees)) = Tree\<^sub>m (Blinded (Content (h data, map (hash_source_tree h) subtrees)))"
35
+ end
36
+
37
+ case_of_simps blind_source_tree_cases: blind_source_tree.simps
38
+
39
+ fun is_blinded :: "('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m \<Rightarrow> bool" where
40
+ "is_blinded (Tree\<^sub>m (Blinded _)) = True"
41
+ | "is_blinded _ = False"
42
+
43
+ lemma hash_blinded_simp: "hash_tree h' (blind_source_tree h st) = hash_source_tree h st"
44
+ by(cases st rule: blind_source_tree.cases)(simp_all add: hash_rt_F\<^sub>m_def)
45
+
46
+ lemma hash_embedded_simp:
47
+ "hash_tree h (embed_source_tree e st) = hash_source_tree (h \<circ> e) st"
48
+ by(induction st rule: embed_source_tree.induct)(simp add: hash_rt_F\<^sub>m_def)
49
+
50
+ lemma blinded_embedded_same_hash:
51
+ "hash_tree h'' (blind_source_tree (h o e) st) = hash_tree h (embed_source_tree e st)"
52
+ by(simp add: hash_blinded_simp hash_embedded_simp)
53
+
54
+ lemma blinding_blinds [simp]:
55
+ "is_blinded (blind_source_tree h t)"
56
+ by(simp add: blind_source_tree_cases split: rose_tree.split)
57
+
58
+ lemma blinded_blinds_embedded:
59
+ "blinding_of_tree h bo (blind_source_tree (h o e) st) (embed_source_tree e st)"
60
+ by(cases st rule: blind_source_tree.cases)(simp_all add: hash_embedded_simp)
61
+
62
+ fun embed_hash_tree :: "'ha rose_tree\<^sub>h \<Rightarrow> ('a, 'ha) rose_tree\<^sub>m" where
63
+ "embed_hash_tree (Tree\<^sub>h h) = Tree\<^sub>m (Blinded h)"
64
+
65
+
66
+ (************************************************************)
67
+ subsubsection \<open>Auxiliary definitions: selectors and list splits\<close>
68
+ (************************************************************)
69
+
70
+ fun children :: "'a rose_tree \<Rightarrow> 'a rose_tree list" where
71
+ "children (Tree (data, subtrees)) = subtrees"
72
+
73
+ fun children\<^sub>m :: "('a, 'a\<^sub>h) rose_tree\<^sub>m \<Rightarrow> ('a, 'a\<^sub>h) rose_tree\<^sub>m list" where
74
+ "children\<^sub>m (Tree\<^sub>m (Unblinded (data, subtrees))) = subtrees"
75
+ | "children\<^sub>m _ = undefined"
76
+
77
+ fun splits :: "'a list \<Rightarrow> ('a list \<times> 'a \<times> 'a list) list" where
78
+ "splits [] = []"
79
+ | "splits (x#xs) = ([], x, xs) # map (\<lambda>(l, y, r). (x # l, y, r)) (splits xs)"
80
+
81
+ lemma splits_iff: "(l, a, r) \<in> set (splits ll) = (ll = l @ a # r)"
82
+ by(induction ll arbitrary: l a r)(auto simp add: Cons_eq_append_conv)
83
+
84
+ (************************************************************)
85
+ subsubsection \<open> Zippers \<close>
86
+ (************************************************************)
87
+
88
+ text \<open> Zippers provide a neat representation of tree-like ADSs when they have only a single
89
+ unblinded subtree. The zipper path provides the "inclusion proof" that the unblinded subtree is
90
+ included in a larger structure. \<close>
91
+
92
+ type_synonym 'a path_elem = "'a \<times> 'a rose_tree list \<times> 'a rose_tree list"
93
+ type_synonym 'a path = "'a path_elem list"
94
+ type_synonym 'a zipper = "'a path \<times> 'a rose_tree"
95
+
96
+ definition zipper_of_tree :: "'a rose_tree \<Rightarrow> 'a zipper" where
97
+ "zipper_of_tree t \<equiv> ([], t)"
98
+
99
+ fun tree_of_zipper :: "'a zipper \<Rightarrow> 'a rose_tree" where
100
+ "tree_of_zipper ([], t) = t"
101
+ | "tree_of_zipper ((a, l, r) # z, t) = tree_of_zipper (z, (Tree (a, (l @ t # r))))"
102
+
103
+ case_of_simps tree_of_zipper_cases: tree_of_zipper.simps
104
+
105
+ lemma tree_of_zipper_id[iff]: "tree_of_zipper (zipper_of_tree t) = t"
106
+ by(simp add: zipper_of_tree_def)
107
+
108
+ fun zipper_children :: "'a zipper \<Rightarrow> 'a zipper list" where
109
+ "zipper_children (p, Tree (a, ts)) = map (\<lambda>(l, t, r). ((a, l, r) # p, t)) (splits ts)"
110
+
111
+ lemma zipper_children_same_tree:
112
+ assumes "z' \<in> set (zipper_children z)"
113
+ shows "tree_of_zipper z' = tree_of_zipper z"
114
+ proof-
115
+ obtain p a ts where z: "z = (p, Tree (a, ts))"
116
+ using assms
117
+ by(cases z rule: zipper_children.cases) (simp_all)
118
+
119
+ then obtain l t r where ltr: "z' = ((a, l, r) # p, t)" and "(l, t, r) \<in> set (splits ts)"
120
+ using assms
121
+ by(auto)
122
+
123
+ with z show ?thesis
124
+ by(simp add: splits_iff)
125
+ qed
126
+
127
+ type_synonym ('a\<^sub>m, 'a\<^sub>h) path_elem\<^sub>m = "'a\<^sub>m \<times> ('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m list \<times> ('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m list"
128
+ type_synonym ('a\<^sub>m, 'a\<^sub>h) path\<^sub>m = "('a\<^sub>m, 'a\<^sub>h) path_elem\<^sub>m list"
129
+ type_synonym ('a\<^sub>m, 'a\<^sub>h) zipper\<^sub>m = "('a\<^sub>m, 'a\<^sub>h) path\<^sub>m \<times> ('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m"
130
+
131
+ definition zipper_of_tree\<^sub>m :: "('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) zipper\<^sub>m" where
132
+ "zipper_of_tree\<^sub>m t \<equiv> ([], t)"
133
+
134
+ fun tree_of_zipper\<^sub>m :: "('a\<^sub>m, 'a\<^sub>h) zipper\<^sub>m \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) rose_tree\<^sub>m" where
135
+ "tree_of_zipper\<^sub>m ([], t) = t"
136
+ | "tree_of_zipper\<^sub>m ((m, l, r) # z, t) = tree_of_zipper\<^sub>m (z, Tree\<^sub>m (Unblinded (m, l @ t # r)))"
137
+
138
+ lemma tree_of_zipper\<^sub>m_append:
139
+ "tree_of_zipper\<^sub>m (p @ p', t) = tree_of_zipper\<^sub>m (p', tree_of_zipper\<^sub>m (p, t))"
140
+ by(induction p arbitrary: p' t) auto
141
+
142
+ fun zipper_children\<^sub>m :: "('a\<^sub>m, 'a\<^sub>h) zipper\<^sub>m \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) zipper\<^sub>m list" where
143
+ "zipper_children\<^sub>m (p, Tree\<^sub>m (Unblinded (a, ts))) = map (\<lambda>(l, t, r). ((a, l, r) # p, t)) (splits ts) "
144
+ | "zipper_children\<^sub>m _ = []"
145
+
146
+ lemma zipper_children_same_tree\<^sub>m:
147
+ assumes "z' \<in> set (zipper_children\<^sub>m z)"
148
+ shows "tree_of_zipper\<^sub>m z' = tree_of_zipper\<^sub>m z"
149
+ proof-
150
+ obtain p a ts where z: "z = (p, Tree\<^sub>m (Unblinded (a, ts)))"
151
+ using assms
152
+ by(cases z rule: zipper_children\<^sub>m.cases) (simp_all)
153
+
154
+ then obtain l t r where ltr: "z' = ((a, l, r) # p, t)" and "(l, t, r) \<in> set (splits ts)"
155
+ using assms
156
+ by(auto)
157
+
158
+ with z show ?thesis
159
+ by(simp add: splits_iff)
160
+ qed
161
+
162
+ fun blind_path_elem :: "('a \<Rightarrow> 'a\<^sub>m) \<Rightarrow> ('a\<^sub>m \<Rightarrow> 'a\<^sub>h) \<Rightarrow> 'a path_elem \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) path_elem\<^sub>m" where
163
+ "blind_path_elem e h (x, l, r) = (e x, map (blind_source_tree (h \<circ> e)) l, map (blind_source_tree (h \<circ> e)) r)"
164
+
165
+ case_of_simps blind_path_elem_cases: blind_path_elem.simps
166
+
167
+ definition blind_path :: "('a \<Rightarrow> 'a\<^sub>m) \<Rightarrow> ('a\<^sub>m \<Rightarrow> 'a\<^sub>h) \<Rightarrow> 'a path \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) path\<^sub>m" where
168
+ "blind_path e h \<equiv> map (blind_path_elem e h)"
169
+
170
+ fun embed_path_elem :: "('a \<Rightarrow> 'a\<^sub>m) \<Rightarrow> 'a path_elem \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) path_elem\<^sub>m" where
171
+ "embed_path_elem e (d, l, r) = (e d, map (embed_source_tree e) l, map (embed_source_tree e) r)"
172
+
173
+ definition embed_path :: "('a \<Rightarrow> 'a\<^sub>m) \<Rightarrow> 'a path \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) path\<^sub>m" where
174
+ "embed_path embed_elem \<equiv> map (embed_path_elem embed_elem)"
175
+
176
+ lemma hash_tree_of_zipper_same_path:
177
+ "hash_tree h (tree_of_zipper\<^sub>m (p, v)) = hash_tree h (tree_of_zipper\<^sub>m (p, v'))
178
+ \<longleftrightarrow> hash_tree h v = hash_tree h v'"
179
+ by(induction p arbitrary: v v')(auto simp add: hash_rt_F\<^sub>m_def)
180
+
181
+ fun hash_path_elem :: "('a\<^sub>m \<Rightarrow> 'a\<^sub>h) \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) path_elem\<^sub>m \<Rightarrow> ('a\<^sub>h \<times> 'a\<^sub>h rose_tree\<^sub>h list \<times> 'a\<^sub>h rose_tree\<^sub>h list)" where
182
+ "hash_path_elem h (e, l, r) = (h e, map (hash_tree h) l, map (hash_tree h) r)"
183
+
184
+ lemma hash_view_zipper_eqI:
185
+ "\<lbrakk> hash_list (hash_path_elem h) p = hash_list (hash_path_elem h') p';
186
+ hash_tree h v = hash_tree h' v' \<rbrakk> \<Longrightarrow>
187
+ hash_tree h (tree_of_zipper\<^sub>m (p, v)) = hash_tree h' (tree_of_zipper\<^sub>m (p', v'))"
188
+ by(induction p arbitrary: p' v v')(auto simp add: hash_rt_F\<^sub>m_def)
189
+
190
+ lemma blind_embed_path_same_hash:
191
+ "hash_tree h (tree_of_zipper\<^sub>m (blind_path e h p, t)) = hash_tree h (tree_of_zipper\<^sub>m (embed_path e p, t))"
192
+ proof -
193
+ have "hash_path_elem h \<circ> blind_path_elem e h = hash_path_elem h \<circ> embed_path_elem e"
194
+ by(clarsimp simp add: hash_blinded_simp hash_embedded_simp fun_eq_iff intro!: arg_cong2[where f=hash_source_tree, OF _ refl])
195
+ then show ?thesis
196
+ by(intro hash_view_zipper_eqI)(simp_all add: embed_path_def blind_path_def list.map_comp)
197
+ qed
198
+
199
+ lemma tree_of_embed_commute:
200
+ "tree_of_zipper\<^sub>m (embed_path e p, embed_source_tree e t) = embed_source_tree e (tree_of_zipper (p, t))"
201
+ by(induction "(p, t)" arbitrary: p t rule: tree_of_zipper.induct)(simp_all add: embed_path_def)
202
+
203
+ lemma childz_same_tree:
204
+ "(l, t, r) \<in> set (splits ts) \<Longrightarrow>
205
+ tree_of_zipper\<^sub>m (embed_path e p, embed_source_tree e (Tree (d, ts)))
206
+ = tree_of_zipper\<^sub>m (embed_path e ((d, l, r) # p), embed_source_tree e t)"
207
+ by(simp add: tree_of_embed_commute splits_iff del: embed_source_tree.simps)
208
+
209
+ lemma blinding_of_same_path:
210
+ assumes bo: "blinding_of_on UNIV h bo"
211
+ shows
212
+ "blinding_of_tree h bo (tree_of_zipper\<^sub>m (p, t)) (tree_of_zipper\<^sub>m (p, t'))
213
+ \<longleftrightarrow> blinding_of_tree h bo t t'"
214
+ proof -
215
+ interpret a: blinding_of_on UNIV h bo by fact
216
+ interpret tree: blinding_of_on UNIV "hash_tree h" "blinding_of_tree h bo" ..
217
+ show ?thesis
218
+ by(induction p arbitrary: t t')(auto simp add: list_all2_append list.rel_refl a.refl tree.refl)
219
+ qed
220
+
221
+ lemma zipper_children_size_change [termination_simp]: "(a, b) \<in> set (zipper_children (p, v)) \<Longrightarrow> size b < size v"
222
+ by(cases v)(clarsimp simp add: splits_iff Set.image_iff)
223
+
224
+
225
+ subsection \<open>All zippers of a rose tree\<close>
226
+
227
+ context fixes e :: "'a \<Rightarrow> 'a\<^sub>m" and h :: "'a\<^sub>m \<Rightarrow> 'a\<^sub>h" begin
228
+
229
+ fun zippers_rose_tree :: "'a zipper \<Rightarrow> ('a\<^sub>m, 'a\<^sub>h) zipper\<^sub>m list" where
230
+ "zippers_rose_tree (p, t) = (blind_path e h p, embed_source_tree e t) #
231
+ concat (map zippers_rose_tree (zipper_children (p, t)))"
232
+
233
+ end
234
+
235
+ lemmas [simp del] = zippers_rose_tree.simps zipper_children.simps
236
+
237
+ lemma zippers_rose_tree_same_hash':
238
+ assumes "z \<in> set (zippers_rose_tree e h (p, t))"
239
+ shows "hash_tree h (tree_of_zipper\<^sub>m z) =
240
+ hash_tree h (tree_of_zipper\<^sub>m (embed_path e p, embed_source_tree e t))"
241
+ using assms(1)
242
+ proof(induction "(p, t)" arbitrary: p t rule: zippers_rose_tree.induct)
243
+ case (1 p t)
244
+ from "1.prems"[unfolded zippers_rose_tree.simps]
245
+ consider (find) "z = (blind_path e h p, embed_source_tree e t)"
246
+ | (rec) x ts l t' r where "t = Tree (x, ts)" "(l, t', r) \<in> set (splits ts)" "z \<in> set (zippers_rose_tree e h ((x, l, r) # p, t'))"
247
+ by(cases t)(auto simp add: zipper_children.simps)
248
+ then show ?case
249
+ proof cases
250
+ case rec
251
+ then show ?thesis
252
+ apply(subst "1.hyps"[of "(x, l, r) # p" "t'"])
253
+ apply(simp_all add: rev_image_eqI zipper_children.simps)
254
+ by (metis (no_types) childz_same_tree comp_apply embed_source_tree.simps rec(2))
255
+ qed(simp add: blind_embed_path_same_hash)
256
+ qed
257
+
258
+ lemma zippers_rose_tree_blinding_of:
259
+ assumes "blinding_of_on UNIV h bo"
260
+ and z: "z \<in> set (zippers_rose_tree e h (p, t))"
261
+ shows "blinding_of_tree h bo (tree_of_zipper\<^sub>m z) (tree_of_zipper\<^sub>m (blind_path e h p, embed_source_tree e t))"
262
+ using z
263
+ proof(induction "(p, t)" arbitrary: p t rule: zippers_rose_tree.induct)
264
+ case (1 p t)
265
+
266
+ interpret a: blinding_of_on UNIV h bo by fact
267
+ interpret rt: blinding_of_on UNIV "hash_tree h" "blinding_of_tree h bo" ..
268
+
269
+ from "1.prems"[unfolded zippers_rose_tree.simps]
270
+ consider (find) "z = (blind_path e h p, embed_source_tree e t)"
271
+ | (rec) x ts l t' r where "t = Tree (x, ts)" "(l, t', r) \<in> set (splits ts)" "z \<in> set (zippers_rose_tree e h ((x, l, r) # p, t'))"
272
+ by(cases t)(auto simp add: zipper_children.simps)
273
+ then show ?case
274
+ proof cases
275
+ case find
276
+ then show ?thesis by(simp add: rt.refl)
277
+ next
278
+ case rec
279
+ then have "blinding_of_tree h bo
280
+ (tree_of_zipper\<^sub>m z)
281
+ (tree_of_zipper\<^sub>m (blind_path e h ((x, l, r) # p), embed_source_tree e t'))"
282
+ by(intro 1)(simp add: rev_image_eqI zipper_children.simps)
283
+ also have "blinding_of_tree h bo
284
+ (tree_of_zipper\<^sub>m (blind_path e h ((x, l, r) # p), embed_source_tree e t'))
285
+ (tree_of_zipper\<^sub>m (blind_path e h p, embed_source_tree e (Tree (x, ts))))"
286
+ using rec
287
+ by(simp add: blind_path_def splits_iff blinding_of_same_path[OF assms(1)] a.refl list_all2_append list_all2_same list.rel_map blinded_blinds_embedded rt.refl)
288
+ finally (rt.trans) show ?thesis using rec by simp
289
+ qed
290
+ qed
291
+
292
+ lemma zippers_rose_tree_neq_Nil: "zippers_rose_tree e h (p, t) \<noteq> []"
293
+ by(simp add: zippers_rose_tree.simps)
294
+
295
+ lemma (in comp_fun_idem) fold_set_union:
296
+ assumes "finite A" "finite B"
297
+ shows "Finite_Set.fold f z (A \<union> B) = Finite_Set.fold f (Finite_Set.fold f z A) B"
298
+ using assms(2,1) by induct simp_all
299
+
300
+ context merkle_interface begin
301
+
302
+ lemma comp_fun_idem_merge: "comp_fun_idem (\<lambda>x yo. yo \<bind> m x)"
303
+ apply(unfold_locales; clarsimp simp add: fun_eq_iff split: bind_split)
304
+ subgoal by (metis assoc bind.bind_lunit bind.bind_lzero idem option.distinct(1))
305
+ subgoal by (simp add: join)
306
+ done
307
+
308
+ interpretation merge: comp_fun_idem "\<lambda>x yo. yo \<bind> m x" by(rule comp_fun_idem_merge)
309
+
310
+ definition Merge :: "'a\<^sub>m set \<Rightarrow> 'a\<^sub>m option" where
311
+ "Merge A = (if A = {} \<or> infinite A then None else Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some (SOME x. x \<in> A)) A)"
312
+
313
+ lemma Merge_empty [simp]: "Merge {} = None"
314
+ by(simp add: Merge_def)
315
+
316
+ lemma Merge_infinite [simp]: "infinite A \<Longrightarrow> Merge A = None"
317
+ by(simp add: Merge_def)
318
+
319
+ lemma Merge_cong_start:
320
+ "Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some x) A = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some y) A" (is "?lhs = ?rhs")
321
+ if "x \<in> A" "y \<in> A" "finite A"
322
+ proof -
323
+ have "?lhs = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some x) (insert y A)" using that by(simp add: insert_absorb)
324
+ also have "\<dots> = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (m x y) A" using that
325
+ by(simp only: merge.fold_insert_idem2)(simp add: commute)
326
+ also have "\<dots> = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some y) (insert x A)" using that
327
+ by(simp only: merge.fold_insert_idem2)(simp)
328
+ also have "\<dots> = ?rhs" using that by(simp add: insert_absorb)
329
+ finally show ?thesis .
330
+ qed
331
+
332
+ lemma Merge_insert [simp]: "Merge (insert x A) = (if A = {} then Some x else Merge A \<bind> m x)" (is "?lhs = ?rhs")
333
+ proof(cases "finite A \<and> A \<noteq> {}")
334
+ case True
335
+ then have "?lhs = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some (SOME x. x \<in> A)) (insert x A)"
336
+ unfolding Merge_def by(subst Merge_cong_start[where y="SOME x. x \<in> A", OF someI])(auto intro: someI)
337
+ also have "\<dots> = ?rhs" using True by(simp add: Merge_def)
338
+ finally show ?thesis .
339
+ qed(auto simp add: Merge_def idem)
340
+
341
+ lemma Merge_insert_alt:
342
+ "Merge (insert x A) = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some x) A" (is "?lhs = ?rhs") if "finite A"
343
+ proof -
344
+ have "?lhs = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some x) (insert x A)" using that
345
+ unfolding Merge_def by(subst Merge_cong_start[where y=x, OF someI]) auto
346
+ also have "\<dots> = ?rhs" using that by(simp only: merge.fold_insert_idem2)(simp add: idem)
347
+ finally show ?thesis .
348
+ qed
349
+
350
+ lemma Merge_None [simp]: "Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) None A = None"
351
+ proof(cases "finite A")
352
+ case True
353
+ then show ?thesis by(induction) auto
354
+ qed simp
355
+
356
+ lemma Merge_union:
357
+ "Merge (A \<union> B) = (if A = {} then Merge B else if B = {} then Merge A else (Merge A \<bind> (\<lambda>a. Merge B \<bind> m a)))"
358
+ (is "?lhs = ?rhs")
359
+ proof(cases "finite (A \<union> B) \<and> A \<noteq> {} \<and> B \<noteq> {}")
360
+ case True
361
+ then have "?lhs = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some (SOME x. x \<in> B)) (B \<union> A)"
362
+ unfolding Merge_def by(subst Merge_cong_start[where y="SOME x. x \<in> B", OF someI])(auto intro: someI simp add: Un_commute)
363
+ also have "\<dots> = Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Merge B) A" using True
364
+ by(simp add: Merge_def merge.fold_set_union)
365
+ also have "\<dots> = Merge A \<bind> (\<lambda>a. Merge B \<bind> m a)"
366
+ proof(cases "Merge B")
367
+ case (Some b)
368
+ thus ?thesis using True
369
+ by simp(subst Merge_insert_alt[symmetric]; simp add: commute; metis commute)
370
+ qed simp
371
+ finally show ?thesis using True by simp
372
+ qed auto
373
+
374
+ lemma Merge_upper:
375
+ assumes m: "Merge A = Some x" and y: "y \<in> A"
376
+ shows "bo y x"
377
+ proof -
378
+ have "Merge A = Merge (insert y A)" using y by(simp add: insert_absorb)
379
+ also have "\<dots> = Merge A \<bind> m y" using y by auto
380
+ finally have "m y x = Some x" using m by simp
381
+ thus ?thesis by(simp add: bo_def)
382
+ qed
383
+
384
+ lemma Merge_least:
385
+ assumes m: "Merge A = Some x" and u[rule_format]: "\<forall>a\<in>A. bo a u"
386
+ shows "bo x u"
387
+ proof -
388
+ define a where "a \<equiv> SOME x. x \<in> A"
389
+ from m have A: "finite A" "A \<noteq> {}"
390
+ and *: "Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some a) A = Some x"
391
+ by(auto simp add: Merge_def a_def split: if_splits)
392
+ from A have "bo a u" by(auto intro: someI u simp add: a_def)
393
+ with A * u show ?thesis
394
+ proof(induction A arbitrary: a)
395
+ case (insert x A)
396
+ then show ?case
397
+ by(cases "m x a"; cases "A = {}"; simp only: merge.fold_insert_idem2; simp)(auto simp add: join)
398
+ qed simp
399
+ qed
400
+
401
+ lemma Merge_defined:
402
+ assumes "finite A" "A \<noteq> {}" "\<forall>a\<in>A. \<forall>b \<in> A. h a = h b"
403
+ shows "Merge A \<noteq> None"
404
+ proof
405
+ define a where "a \<equiv> SOME a. a \<in> A"
406
+ have a: "a \<in> A" unfolding a_def using assms by(auto intro: someI)
407
+ hence ha: "\<forall>b \<in> A. h b = h a" using assms by blast
408
+
409
+ assume m: "Merge A = None"
410
+ hence "Finite_Set.fold (\<lambda>x yo. yo \<bind> m x) (Some a) A = None"
411
+ using assms by(simp add: Merge_def a_def)
412
+ with assms(1) show False using ha
413
+ proof(induction arbitrary: a)
414
+ case (insert x A)
415
+ thus ?case
416
+ apply(cases "m x a"; use nothing in \<open>simp only: merge.fold_insert_idem2\<close>)
417
+ apply(simp add: merge_respects_hashes)
418
+ apply(fastforce simp add: join vimage2p_def dest: hash[THEN predicate2D])
419
+ done
420
+ qed simp
421
+ qed
422
+
423
+ lemma Merge_hash:
424
+ assumes "Merge A = Some x" "a \<in> A"
425
+ shows "h a = h x"
426
+ using Merge_upper[OF assms] hash by(auto simp add: vimage2p_def)
427
+
428
+ end
429
+
430
+ end
formal/afp/ADS_Functor/Merkle_Interface.thy ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Author: Andreas Lochbihler, Digital Asset
2
+ Author: Ognjen Maric, Digital Asset *)
3
+
4
+ theory Merkle_Interface
5
+ imports
6
+ Main
7
+ "HOL-Library.Conditional_Parametricity"
8
+ "HOL-Library.Monad_Syntax"
9
+ begin
10
+
11
+ alias vimage2p = BNF_Def.vimage2p
12
+ alias Grp = BNF_Def.Grp
13
+ alias setl = Basic_BNFs.setl
14
+ alias setr = Basic_BNFs.setr
15
+ alias fsts = Basic_BNFs.fsts
16
+ alias snds = Basic_BNFs.snds
17
+
18
+ attribute_setup locale_witness = \<open>Scan.succeed Locale.witness_add\<close>
19
+
20
+ lemma vimage2p_mono': "R \<le> S \<Longrightarrow> vimage2p f g R \<le> vimage2p f g S"
21
+ by(auto simp add: vimage2p_def le_fun_def)
22
+
23
+ lemma vimage2p_map_rel_prod:
24
+ "vimage2p (map_prod f g) (map_prod f' g') (rel_prod A B) = rel_prod (vimage2p f f' A) (vimage2p g g' B)"
25
+ by(simp add: vimage2p_def prod.rel_map)
26
+
27
+ lemma vimage2p_map_list_all2:
28
+ "vimage2p (map f) (map g) (list_all2 A) = list_all2 (vimage2p f g A)"
29
+ by(simp add: vimage2p_def list.rel_map)
30
+
31
+ lemma equivclp_least:
32
+ assumes le: "r \<le> s" and s: "equivp s"
33
+ shows "equivclp r \<le> s"
34
+ apply(rule predicate2I)
35
+ subgoal by(induction rule: equivclp_induct)(auto 4 3 intro: equivp_reflp[OF s] equivp_transp[OF s] equivp_symp[OF s] le[THEN predicate2D])
36
+ done
37
+
38
+ lemma reflp_eq_onp: "reflp R \<longleftrightarrow> eq_onp (\<lambda>x. True) \<le> R"
39
+ by(auto simp add: reflp_def eq_onp_def)
40
+
41
+ lemma eq_onpE:
42
+ assumes "eq_onp P x y"
43
+ obtains "x = y" "P y"
44
+ using assms by(auto simp add: eq_onp_def)
45
+
46
+ lemma case_unit_parametric [transfer_rule]: "rel_fun A (rel_fun (=) A) case_unit case_unit"
47
+ by(simp add: rel_fun_def split: unit.split)
48
+
49
+
50
+ (************************************************************)
51
+ section \<open>Authenticated Data Structures\<close>
52
+ (************************************************************)
53
+
54
+ (************************************************************)
55
+ subsection \<open>Interface\<close>
56
+ (************************************************************)
57
+
58
+ (************************************************************)
59
+ subsubsection \<open> Types \<close>
60
+ (************************************************************)
61
+
62
+ type_synonym ('a\<^sub>m, 'a\<^sub>h) hash = "'a\<^sub>m \<Rightarrow> 'a\<^sub>h" \<comment> \<open>Type of hash operation\<close>
63
+ type_synonym 'a\<^sub>m blinding_of = "'a\<^sub>m \<Rightarrow> 'a\<^sub>m \<Rightarrow> bool"
64
+ type_synonym 'a\<^sub>m merge = "'a\<^sub>m \<Rightarrow> 'a\<^sub>m \<Rightarrow> 'a\<^sub>m option" \<comment> \<open> merging that can fail for values with different hashes\<close>
65
+
66
+ (************************************************************)
67
+ subsubsection \<open> Properties \<close>
68
+ (************************************************************)
69
+
70
+ locale merkle_interface =
71
+ fixes h :: "('a\<^sub>m, 'a\<^sub>h) hash"
72
+ and bo :: "'a\<^sub>m blinding_of"
73
+ and m :: "'a\<^sub>m merge"
74
+ assumes merge_respects_hashes: "h a = h b \<longleftrightarrow> (\<exists>ab. m a b = Some ab)"
75
+ and idem: "m a a = Some a"
76
+ and commute: "m a b = m b a"
77
+ and assoc: "m a b \<bind> m c = m b c \<bind> m a"
78
+ and bo_def: "bo a b \<longleftrightarrow> m a b = Some b"
79
+ begin
80
+
81
+ lemma reflp: "reflp bo"
82
+ unfolding bo_def by(rule reflpI)(simp add: idem)
83
+
84
+ lemma antisymp: "antisymp bo"
85
+ unfolding bo_def by(rule antisympI)(simp add: commute)
86
+
87
+ lemma transp: "transp bo"
88
+ apply(rule transpI)
89
+ subgoal for x y z using assoc[of x y z] by(simp add: commute bo_def)
90
+ done
91
+
92
+ lemma hash: "bo \<le> vimage2p h h (=)"
93
+ unfolding bo_def by(auto simp add: vimage2p_def merge_respects_hashes)
94
+
95
+ lemma join: "m a b = Some ab \<longleftrightarrow> bo a ab \<and> bo b ab \<and> (\<forall>u. bo a u \<longrightarrow> bo b u \<longrightarrow> bo ab u)"
96
+ unfolding bo_def
97
+ by (smt Option.bind_cong bind.bind_lunit commute idem merkle_interface.assoc merkle_interface_axioms)
98
+
99
+ text \<open>The equivalence closure of the blinding relation are the equivalence classes of the hash function (the kernel).\<close>
100
+
101
+ lemma equivclp_blinding_of: "equivclp bo = vimage2p h h (=)" (is "?lhs = ?rhs")
102
+ proof(rule antisym)
103
+ show "?lhs \<le> ?rhs" by(rule equivclp_least[OF hash])(rule equivp_vimage2p[OF identity_equivp])
104
+ show "?rhs \<le> ?lhs" unfolding vimage2p_def
105
+ proof(rule predicate2I)
106
+ fix x y
107
+ assume "h x = h y"
108
+ then obtain xy where "m x y = Some xy" unfolding merge_respects_hashes ..
109
+ hence "bo x xy" "bo y xy" unfolding join by blast+
110
+ hence "equivclp bo x xy" "equivclp bo xy y" by(blast)+
111
+ thus "equivclp bo x y" by(rule equivclp_trans)
112
+ qed
113
+ qed
114
+
115
+ end
116
+
117
+ (************************************************************)
118
+ subsection \<open> Auxiliary definitions \<close>
119
+ (************************************************************)
120
+
121
+ text \<open> Directly proving that an interface satisfies the specification of a Merkle interface as given
122
+ above is difficult. Instead, we provide several layers of auxiliary definitions that can easily be
123
+ proved layer-by-layer.
124
+
125
+ In particular, proving that an interface on recursive datatypes is a Merkle interface requires
126
+ induction. As the induction hypothesis only applies to a subset of values of a type, we add
127
+ auxiliary definitions equipped with an explicit set @{term A} of values to which the definition
128
+ applies. Once the induction proof is complete, we can typically instantiate @{term A} with @{term
129
+ UNIV}. In particular, in the induction proof for a layer, we can assume that properties for the
130
+ earlier layers hold for \<^emph>\<open>all\<close> values, not just those in the induction hypothesis.
131
+ \<close>
132
+
133
+ (************************************************************)
134
+ subsubsection \<open> Blinding \<close>
135
+ (************************************************************)
136
+ locale blinding_respects_hashes =
137
+ fixes h :: "('a\<^sub>m, 'a\<^sub>h) hash"
138
+ and bo :: "'a\<^sub>m blinding_of"
139
+ assumes hash: "bo \<le> vimage2p h h (=)"
140
+ begin
141
+
142
+ lemma blinding_hash_eq: "bo x y \<Longrightarrow> h x = h y"
143
+ by(drule hash[THEN predicate2D])(simp add: vimage2p_def)
144
+
145
+ end
146
+
147
+ locale blinding_of_on =
148
+ blinding_respects_hashes h bo
149
+ for A :: "'a\<^sub>m set"
150
+ and h :: "('a\<^sub>m, 'a\<^sub>h) hash"
151
+ and bo :: "'a\<^sub>m blinding_of"
152
+ + assumes refl: "x \<in> A \<Longrightarrow> bo x x"
153
+ and trans: "\<lbrakk> bo x y; bo y z; x \<in> A \<rbrakk> \<Longrightarrow> bo x z"
154
+ and antisym: "\<lbrakk> bo x y; bo y x; x \<in> A \<rbrakk> \<Longrightarrow> x = y"
155
+ begin
156
+
157
+ lemma refl_pointfree: "eq_onp (\<lambda>x. x \<in> A) \<le> bo"
158
+ by(auto elim!: eq_onpE intro: refl)
159
+
160
+ lemma blinding_respects_hashes: "blinding_respects_hashes h bo" ..
161
+ lemmas hash = hash
162
+
163
+ lemma trans_pointfree: "eq_onp (\<lambda>x. x \<in> A) OO bo OO bo \<le> bo"
164
+ by(auto elim!: eq_onpE intro: trans)
165
+
166
+ lemma antisym_pointfree: "inf (eq_onp (\<lambda>x. x \<in> A) OO bo) bo\<inverse>\<inverse> \<le> (=)"
167
+ by(auto elim!: eq_onpE dest: antisym)
168
+
169
+ end
170
+
171
+ (************************************************************)
172
+ subsubsection \<open> Merging \<close>
173
+ (************************************************************)
174
+
175
+ text \<open> In general, we prove the properties of blinding before the properties of merging. Thus,
176
+ in the following definitions we assume that the blinding properties already hold on @{term UNIV}.
177
+ The @{term Ball} restricts the argument of the merge operation on which induction will be done. \<close>
178
+
179
+ locale merge_on =
180
+ blinding_of_on UNIV h bo
181
+ for A :: "'a\<^sub>m set"
182
+ and h :: "('a\<^sub>m, 'a\<^sub>h) hash"
183
+ and bo :: "'a\<^sub>m blinding_of"
184
+ and m :: "'a\<^sub>m merge" +
185
+ assumes join: "\<lbrakk> h a = h b; a \<in> A \<rbrakk>
186
+ \<Longrightarrow> \<exists>ab. m a b = Some ab \<and> bo a ab \<and> bo b ab \<and> (\<forall>u. bo a u \<longrightarrow> bo b u \<longrightarrow> bo ab u)"
187
+ and undefined: "\<lbrakk> h a \<noteq> h b; a \<in> A \<rbrakk> \<Longrightarrow> m a b = None"
188
+ begin
189
+
190
+ lemma same: "a \<in> A \<Longrightarrow> m a a = Some a"
191
+ using join[of a a] refl[of a] by(auto 4 3 intro: antisym)
192
+
193
+ lemma blinding_of_antisym_on: "blinding_of_on UNIV h bo" ..
194
+
195
+ lemma transp: "transp bo"
196
+ by(auto intro: transpI trans)
197
+
198
+ lemmas hash = hash
199
+ and refl = refl
200
+ and antisym = antisym[OF _ _ UNIV_I]
201
+
202
+ lemma respects_hashes:
203
+ "a \<in> A \<Longrightarrow> h a = h b \<longleftrightarrow> (\<exists>ab. m a b = Some ab)"
204
+ using join undefined
205
+ by fastforce
206
+
207
+ lemma join':
208
+ "a \<in> A \<Longrightarrow> \<forall>ab. m a b = Some ab \<longleftrightarrow> bo a ab \<and> bo b ab \<and> (\<forall>u. bo a u \<longrightarrow> bo b u \<longrightarrow> bo ab u)"
209
+ using join undefined
210
+ by (metis (full_types) hash local.antisym option.distinct(1) option.sel predicate2D vimage2p_def)
211
+
212
+ lemma merge_on_subset:
213
+ "B \<subseteq> A \<Longrightarrow> merge_on B h bo m"
214
+ by unfold_locales (auto dest: same join undefined)
215
+
216
+ end
217
+
218
+ subsection \<open> Interface equality \<close>
219
+
220
+ text \<open> Here, we prove that the auxiliary definitions specify the same interface as the original ones.\<close>
221
+
222
+ lemma merkle_interface_aux: "merkle_interface h bo m = merge_on UNIV h bo m"
223
+ (is "?lhs = ?rhs")
224
+ proof
225
+ show ?rhs if ?lhs
226
+ proof
227
+ interpret merkle_interface h bo m by(fact that)
228
+ show "bo \<le> vimage2p h h (=)" by(fact hash)
229
+ show "bo x x" for x using reflp by(simp add: reflp_def)
230
+ show "bo x z" if "bo x y" "bo y z" for x y z using transp that by(rule transpD)
231
+ show "x = y" if "bo x y" "bo y x" for x y using antisymp that by(rule antisympD)
232
+ show "\<exists>ab. m a b = Some ab \<and> bo a ab \<and> bo b ab \<and> (\<forall>u. bo a u \<longrightarrow> bo b u \<longrightarrow> bo ab u)" if "h a = h b" for a b
233
+ using that by(simp add: merge_respects_hashes join)
234
+ show "m a b = None" if "h a \<noteq> h b" for a b using that by(simp add: merge_respects_hashes)
235
+ qed
236
+
237
+ show ?lhs if ?rhs
238
+ proof
239
+ interpret merge_on UNIV h bo m by(fact that)
240
+ show eq: "h a = h b \<longleftrightarrow> (\<exists>ab. m a b = Some ab)" for a b by(simp add: respects_hashes)
241
+ show idem: "m a a = Some a" for a by(simp add: same)
242
+ show commute: "m a b = m b a" for a b
243
+ using join[of a b] join[of b a] undefined antisym by(cases "m a b") force+
244
+ have undefined_partitioned: "m a c = None" if "m a b = None" "m b c = Some bc" for a b c bc
245
+ using that eq by (metis option.distinct(1) option.exhaust)
246
+ have merge_twice: "m a b = Some c \<Longrightarrow> m a c = Some c" for a b c by (simp add: join')
247
+ show "m a b \<bind> m c = m b c \<bind> m a" for a b c
248
+ proof(simp split: Option.bind_split; safe)
249
+ show "None = m a d" if "m a b = None" "m b c = Some d" for d using that
250
+ by(metis undefined_partitioned merge_twice)
251
+ show "m c d = None" if "m a b = Some d" "m b c = None" for d using that
252
+ by(metis commute merge_twice undefined_partitioned)
253
+ next
254
+ fix ab bc
255
+ assume assms: "m a b = Some ab" "m b c = Some bc"
256
+ then obtain cab and abc where cab: "m c ab = Some cab" and abc: "m a bc = Some abc"
257
+ using eq[THEN iffD2, OF exI] eq[THEN iffD1] by (metis merge_twice)
258
+ thus "m c ab = m a bc" using assms
259
+ by(clarsimp simp add: join')(metis UNIV_I abc cab local.antisym local.trans)
260
+ qed
261
+ show "bo a b \<longleftrightarrow> m a b = Some b" for a b using idem join' by auto
262
+ qed
263
+ qed
264
+
265
+ lemma merkle_interfaceI [locale_witness]:
266
+ assumes "merge_on UNIV h bo m"
267
+ shows "merkle_interface h bo m"
268
+ using assms unfolding merkle_interface_aux by auto
269
+
270
+ lemma (in merkle_interface) merkle_interfaceD: "merge_on UNIV h bo m"
271
+ using merkle_interface_aux[of h bo m, symmetric]
272
+ by simp unfold_locales
273
+
274
+ subsection \<open> Parametricity rules \<close>
275
+
276
+ context includes lifting_syntax begin
277
+ parametric_constant le_fun_parametric[transfer_rule]: le_fun_def
278
+ parametric_constant vimage2p_parametric[transfer_rule]: vimage2p_def
279
+ parametric_constant blinding_respects_hashes_parametric_aux: blinding_respects_hashes_def
280
+
281
+ lemma blinding_respects_hashes_parametric [transfer_rule]:
282
+ "((A1 ===> A2) ===> (A1 ===> A1 ===> (\<longleftrightarrow>)) ===> (\<longleftrightarrow>))
283
+ blinding_respects_hashes blinding_respects_hashes"
284
+ if [transfer_rule]: "bi_unique A2" "bi_total A1"
285
+ by(rule blinding_respects_hashes_parametric_aux that le_fun_parametric | simp add: rel_fun_eq)+
286
+
287
+ parametric_constant blinding_of_on_axioms_parametric [transfer_rule]:
288
+ blinding_of_on_axioms_def[folded Ball_def, unfolded le_fun_def le_bool_def eq_onp_def relcompp.simps, simplified]
289
+ parametric_constant blinding_of_on_parametric [transfer_rule]: blinding_of_on_def
290
+ parametric_constant antisymp_parametric[transfer_rule]: antisymp_def
291
+ parametric_constant transp_parametric[transfer_rule]: transp_def
292
+
293
+ parametric_constant merge_on_axioms_parametric [transfer_rule]: merge_on_axioms_def
294
+ parametric_constant merge_on_parametric[transfer_rule]: merge_on_def
295
+
296
+ parametric_constant merkle_interface_parametric[transfer_rule]: merkle_interface_def
297
+ end
298
+
299
+ end
formal/afp/ADS_Functor/document/root.tex ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ \documentclass[11pt,a4paper]{article}
2
+ \usepackage[T1]{fontenc}
3
+ \usepackage{isabelle,isabellesym}
4
+
5
+ % further packages required for unusual symbols (see also
6
+ % isabellesym.sty), use only when needed
7
+
8
+ %\usepackage{amssymb}
9
+ %for \<leadsto>, \<box>, \<diamond>, \<sqsupset>, \<mho>, \<Join>,
10
+ %\<lhd>, \<lesssim>, \<greatersim>, \<lessapprox>, \<greaterapprox>,
11
+ %\<triangleq>, \<yen>, \<lozenge>
12
+
13
+ %\usepackage{eurosym}
14
+ %for \<euro>
15
+
16
+ %\usepackage[only,bigsqcap]{stmaryrd}
17
+ %for \<Sqinter>
18
+
19
+ %\usepackage{eufrak}
20
+ %for \<AA> ... \<ZZ>, \<aa> ... \<zz> (also included in amssymb)
21
+
22
+ %\usepackage{textcomp}
23
+ %for \<onequarter>, \<onehalf>, \<threequarters>, \<degree>, \<cent>,
24
+ %\<currency>
25
+
26
+ % this should be the last package used
27
+ \usepackage{pdfsetup}
28
+
29
+ % urls in roman style, theory text in math-similar italics
30
+ \urlstyle{rm}
31
+ \isabellestyle{it}
32
+
33
+ % for uniform font size
34
+ %\renewcommand{\isastyle}{\isastyleminor}
35
+
36
+
37
+ \begin{document}
38
+
39
+ \title{Authenticated Data Structures as Functors}
40
+ \author{Andreas Lochbihler \qquad Ognjen Maric \\[1em] Digital Asset}
41
+
42
+ \maketitle
43
+
44
+ \begin{abstract}
45
+ Authenticated data structures allow several systems to convince each other that they are referring to the same data structure,
46
+ even if each of them knows only a part of the data structure.
47
+ Using inclusion proofs, knowledgable systems can selectively share their knowledge with other systems
48
+ and the latter can verify the authenticity of what is being shared.
49
+
50
+ In this paper, we show how to modularly define authenticated data structures, their inclusion proofs, and operations thereon
51
+ as datatypes in Isabelle/HOL, using a shallow embedding.
52
+ Modularity allows us to construct complicated trees from reusable building blocks, which we call Merkle functors.
53
+ Merkle functors include sums, products, and function spaces and are closed under composition and least fixpoints.
54
+
55
+ As a practical application, we model the hierarchical transactions of Canton,
56
+ a practical interoperability protocol for distributed ledgers, as authenticated data structures.
57
+ This is a first step towards formalizing the Canton protocol and verifying its integrity and security guarantees.
58
+ \end{abstract}
59
+
60
+
61
+ \tableofcontents
62
+
63
+ % sane default for proof documents
64
+ \parindent 0pt\parskip 0.5ex
65
+
66
+ % generated text of all theories
67
+ \input{session}
68
+
69
+ % optional bibliography
70
+ %\bibliographystyle{abbrv}
71
+ %\bibliography{root}
72
+
73
+ \end{document}
74
+
75
+ %%% Local Variables:
76
+ %%% mode: latex
77
+ %%% TeX-master: t
78
+ %%% End:
formal/afp/AI_Planning_Languages_Semantics/Error_Monad_Add.thy ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ theory Error_Monad_Add
2
+ imports
3
+ "Certification_Monads.Check_Monad"
4
+ "Show.Show_Instances"
5
+ begin
6
+ (* TODO: Move *)
7
+ abbreviation "assert_opt \<Phi> \<equiv> if \<Phi> then Some () else None"
8
+
9
+ definition "lift_opt m e \<equiv> case m of Some x \<Rightarrow> Error_Monad.return x | None \<Rightarrow> Error_Monad.error e"
10
+
11
+ lemma lift_opt_simps[simp]:
12
+ "lift_opt None e = error e"
13
+ "lift_opt (Some v) e = return v"
14
+ by (auto simp: lift_opt_def)
15
+
16
+ (* TODO: Move *)
17
+ lemma reflcl_image_iff[simp]: "R\<^sup>=``S = S\<union>R``S" by blast
18
+
19
+ named_theorems return_iff
20
+
21
+ lemma bind_return_iff[return_iff]: "Error_Monad.bind m f = Inr y \<longleftrightarrow> (\<exists>x. m = Inr x \<and> f x = Inr y)"
22
+ by auto
23
+
24
+ lemma lift_opt_return_iff[return_iff]: "lift_opt m e = Inr x \<longleftrightarrow> m=Some x"
25
+ unfolding lift_opt_def by (auto split: option.split)
26
+
27
+ lemma check_return_iff[return_iff]: "check \<Phi> e = Inr uu \<longleftrightarrow> \<Phi>"
28
+ by (auto simp: check_def)
29
+
30
+
31
+ lemma check_simps[simp]:
32
+ "check True e = succeed"
33
+ "check False e = error e"
34
+ by (auto simp: check_def)
35
+
36
+ lemma Let_return_iff[return_iff]: "(let x=v in f x) = Inr w \<longleftrightarrow> f v = Inr w" by simp
37
+
38
+
39
+ abbreviation ERR :: "shows \<Rightarrow> (unit \<Rightarrow> shows)" where "ERR s \<equiv> \<lambda>_. s"
40
+ abbreviation ERRS :: "String.literal \<Rightarrow> (unit \<Rightarrow> shows)" where "ERRS s \<equiv> ERR (shows s)"
41
+
42
+
43
+ lemma error_monad_bind_split: "P (bind m f) \<longleftrightarrow> (\<forall>v. m = Inl v \<longrightarrow> P (Inl v)) \<and> (\<forall>v. m = Inr v \<longrightarrow> P (f v))"
44
+ by (cases m) auto
45
+
46
+ lemma error_monad_bind_split_asm: "P (bind m f) \<longleftrightarrow> \<not> (\<exists>x. m = Inl x \<and> \<not> P (Inl x) \<or> (\<exists>x. m = Inr x \<and> \<not> P (f x)))"
47
+ by (cases m) auto
48
+
49
+ lemmas error_monad_bind_splits =error_monad_bind_split error_monad_bind_split_asm
50
+
51
+
52
+ end
formal/afp/AI_Planning_Languages_Semantics/Lifschitz_Consistency.thy ADDED
@@ -0,0 +1,416 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ section \<open>Soundness theorem for the STRIPS semantics\<close>
2
+ text \<open>We prove the soundness theorem according to ~\cite{lifschitz1987semantics}.\<close>
3
+
4
+ theory Lifschitz_Consistency
5
+ imports PDDL_STRIPS_Semantics
6
+ begin
7
+
8
+
9
+ text \<open>States are modeled as valuations of our underlying predicate logic.\<close>
10
+ type_synonym state = "(predicate\<times>object list) valuation"
11
+
12
+ context ast_domain begin
13
+ text \<open>An action is a partial function from states to states. \<close>
14
+ type_synonym action = "state \<rightharpoonup> state"
15
+
16
+ text \<open>The Isabelle/HOL formula @{prop \<open>f s = Some s'\<close>} means
17
+ that \<open>f\<close> is applicable in state \<open>s\<close>, and the result is \<open>s'\<close>. \<close>
18
+
19
+ text \<open>Definition B (i)--(iv) in Lifschitz's paper~\cite{lifschitz1987semantics}\<close>
20
+
21
+ fun is_NegPredAtom where
22
+ "is_NegPredAtom (Not x) = is_predAtom x" | "is_NegPredAtom _ = False"
23
+
24
+ definition "close_eq s = (\<lambda>predAtm p xs \<Rightarrow> s (p,xs) | Eq a b \<Rightarrow> a=b)"
25
+
26
+ lemma close_eq_predAtm[simp]: "close_eq s (predAtm p xs) \<longleftrightarrow> s (p,xs)"
27
+ by (auto simp: close_eq_def)
28
+
29
+ lemma close_eq_Eq[simp]: "close_eq s (Eq a b) \<longleftrightarrow> a=b"
30
+ by (auto simp: close_eq_def)
31
+
32
+
33
+ abbreviation entail_eq :: "state \<Rightarrow> object atom formula \<Rightarrow> bool" (infix "\<Turnstile>\<^sub>=" 55)
34
+ where "entail_eq s f \<equiv> close_eq s \<Turnstile> f"
35
+
36
+
37
+ fun sound_opr :: "ground_action \<Rightarrow> action \<Rightarrow> bool" where
38
+ "sound_opr (Ground_Action pre (Effect add del)) f \<longleftrightarrow>
39
+ (\<forall>s. s \<Turnstile>\<^sub>= pre \<longrightarrow>
40
+ (\<exists>s'. f s = Some s' \<and> (\<forall>atm. is_predAtom atm \<and> atm \<notin> set del \<and> s \<Turnstile>\<^sub>= atm \<longrightarrow> s' \<Turnstile>\<^sub>= atm)
41
+ \<and> (\<forall>atm. is_predAtom atm \<and> atm \<notin> set add \<and> s \<Turnstile>\<^sub>= Not atm \<longrightarrow> s' \<Turnstile>\<^sub>= Not atm)
42
+ \<and> (\<forall>fmla. fmla \<in> set add \<longrightarrow> s' \<Turnstile>\<^sub>= fmla)
43
+ \<and> (\<forall>fmla. fmla \<in> set del \<and> fmla \<notin> set add \<longrightarrow> s' \<Turnstile>\<^sub>= (Not fmla))
44
+ ))
45
+ \<and> (\<forall>fmla\<in>set add. is_predAtom fmla)"
46
+
47
+ lemma sound_opr_alt:
48
+ "sound_opr opr f =
49
+ ((\<forall>s. s \<Turnstile>\<^sub>= (precondition opr) \<longrightarrow>
50
+ (\<exists>s'. f s = (Some s')
51
+ \<and> (\<forall>atm. is_predAtom atm \<and> atm \<notin> set(dels (effect opr)) \<and> s \<Turnstile>\<^sub>= atm \<longrightarrow> s' \<Turnstile>\<^sub>= atm)
52
+ \<and> (\<forall>atm. is_predAtom atm \<and> atm \<notin> set (adds (effect opr)) \<and> s \<Turnstile>\<^sub>= Not atm \<longrightarrow> s' \<Turnstile>\<^sub>= Not atm)
53
+ \<and> (\<forall>atm. atm \<in> set(adds (effect opr)) \<longrightarrow> s' \<Turnstile>\<^sub>= atm)
54
+ \<and> (\<forall>fmla. fmla \<in> set (dels (effect opr)) \<and> fmla \<notin> set(adds (effect opr)) \<longrightarrow> s' \<Turnstile>\<^sub>= (Not fmla))
55
+ \<and> (\<forall>a b. s \<Turnstile>\<^sub>= Atom (Eq a b) \<longrightarrow> s' \<Turnstile>\<^sub>= Atom (Eq a b))
56
+ \<and> (\<forall>a b. s \<Turnstile>\<^sub>= Not (Atom (Eq a b)) \<longrightarrow> s' \<Turnstile>\<^sub>= Not (Atom (Eq a b)))
57
+ ))
58
+ \<and> (\<forall>fmla\<in>set(adds (effect opr)). is_predAtom fmla))"
59
+ by (cases "(opr,f)" rule: sound_opr.cases) auto
60
+
61
+ text \<open>Definition B (v)--(vii) in Lifschitz's paper~\cite{lifschitz1987semantics}\<close>
62
+ definition sound_system
63
+ :: "ground_action set
64
+ \<Rightarrow> world_model
65
+ \<Rightarrow> state
66
+ \<Rightarrow> (ground_action \<Rightarrow> action)
67
+ \<Rightarrow> bool"
68
+ where
69
+ "sound_system \<Sigma> M\<^sub>0 s\<^sub>0 f \<longleftrightarrow>
70
+ ((\<forall>fmla\<in>close_world M\<^sub>0. s\<^sub>0 \<Turnstile>\<^sub>= fmla)
71
+ \<and> wm_basic M\<^sub>0
72
+ \<and> (\<forall>\<alpha>\<in>\<Sigma>. sound_opr \<alpha> (f \<alpha>)))"
73
+
74
+ text \<open>Composing two actions\<close>
75
+ definition compose_action :: "action \<Rightarrow> action \<Rightarrow> action" where
76
+ "compose_action f1 f2 x = (case f2 x of Some y \<Rightarrow> f1 y | None \<Rightarrow> None)"
77
+
78
+ text \<open>Composing a list of actions\<close>
79
+ definition compose_actions :: "action list \<Rightarrow> action" where
80
+ "compose_actions fs \<equiv> fold compose_action fs Some"
81
+
82
+ text \<open>Composing a list of actions satisfies some natural lemmas: \<close>
83
+ lemma compose_actions_Nil[simp]:
84
+ "compose_actions [] = Some" unfolding compose_actions_def by auto
85
+
86
+ lemma compose_actions_Cons[simp]:
87
+ "f s = Some s' \<Longrightarrow> compose_actions (f#fs) s = compose_actions fs s'"
88
+ proof -
89
+ interpret monoid_add compose_action Some
90
+ apply unfold_locales
91
+ unfolding compose_action_def
92
+ by (auto split: option.split)
93
+ assume "f s = Some s'"
94
+ then show ?thesis
95
+ unfolding compose_actions_def
96
+ by (simp add: compose_action_def fold_plus_sum_list_rev)
97
+ qed
98
+
99
+ text \<open>Soundness Theorem in Lifschitz's paper~\cite{lifschitz1987semantics}.\<close>
100
+ theorem STRIPS_sema_sound:
101
+ assumes "sound_system \<Sigma> M\<^sub>0 s\<^sub>0 f"
102
+ \<comment> \<open>For a sound system \<open>\<Sigma>\<close>\<close>
103
+ assumes "set \<alpha>s \<subseteq> \<Sigma>"
104
+ \<comment> \<open>And a plan \<open>\<alpha>s\<close>\<close>
105
+ assumes "ground_action_path M\<^sub>0 \<alpha>s M'"
106
+ \<comment> \<open>Which is accepted by the system, yielding result \<open>M'\<close>
107
+ (called \<open>R(\<alpha>s)\<close> in Lifschitz's paper~\cite{lifschitz1987semantics}.)\<close>
108
+ obtains s'
109
+ \<comment> \<open>We have that \<open>f(\<alpha>s)\<close> is applicable
110
+ in initial state, yielding state \<open>s'\<close> (called \<open>f\<^sub>\<alpha>\<^sub>s(s\<^sub>0)\<close> in Lifschitz's paper~\cite{lifschitz1987semantics}.)\<close>
111
+ where "compose_actions (map f \<alpha>s) s\<^sub>0 = Some s'"
112
+ \<comment> \<open>The result world model \<open>M'\<close> is satisfied in state \<open>s'\<close>\<close>
113
+ and "\<forall>fmla\<in>close_world M'. s' \<Turnstile>\<^sub>= fmla"
114
+ proof -
115
+ have "(valuation M' \<Turnstile> fmla)" if "wm_basic M'" "fmla\<in>M'" for fmla
116
+ using that apply (induction fmla)
117
+ by (auto simp: valuation_def wm_basic_def split: atom.split)
118
+ have "\<exists>s'. compose_actions (map f \<alpha>s) s\<^sub>0 = Some s' \<and> (\<forall>fmla\<in>close_world M'. s' \<Turnstile>\<^sub>= fmla)"
119
+ using assms
120
+ proof(induction \<alpha>s arbitrary: s\<^sub>0 M\<^sub>0 )
121
+ case Nil
122
+ then show ?case by (auto simp add: close_world_def compose_action_def sound_system_def)
123
+ next
124
+ case ass: (Cons \<alpha> \<alpha>s)
125
+ then obtain pre add del where a: "\<alpha> = Ground_Action pre (Effect add del)"
126
+ using ground_action.exhaust ast_effect.exhaust by metis
127
+ let ?M\<^sub>1 = "execute_ground_action \<alpha> M\<^sub>0"
128
+ have "close_world M\<^sub>0 \<TTurnstile> precondition \<alpha>"
129
+ using ass(4)
130
+ by auto
131
+ moreover have s0_ent_cwM0: "\<forall>fmla\<in>(close_world M\<^sub>0). close_eq s\<^sub>0 \<Turnstile> fmla"
132
+ using ass(2)
133
+ unfolding sound_system_def
134
+ by auto
135
+ ultimately have s0_ent_alpha_precond: "close_eq s\<^sub>0 \<Turnstile> precondition \<alpha>"
136
+ unfolding entailment_def
137
+ by auto
138
+ then obtain s\<^sub>1 where s1: "(f \<alpha>) s\<^sub>0 = Some s\<^sub>1"
139
+ "(\<forall>atm. is_predAtom atm \<longrightarrow> atm \<notin> set(dels (effect \<alpha>))
140
+ \<longrightarrow> close_eq s\<^sub>0 \<Turnstile> atm
141
+ \<longrightarrow> close_eq s\<^sub>1 \<Turnstile> atm)"
142
+ "(\<forall>fmla. fmla \<in> set(adds (effect \<alpha>))
143
+ \<longrightarrow> close_eq s\<^sub>1 \<Turnstile> fmla)"
144
+ "(\<forall>atm. is_predAtom atm \<and> atm \<notin> set (adds (effect \<alpha>)) \<and> close_eq s\<^sub>0 \<Turnstile> Not atm \<longrightarrow> close_eq s\<^sub>1 \<Turnstile> Not atm)"
145
+ "(\<forall>fmla. fmla \<in> set (dels (effect \<alpha>)) \<and> fmla \<notin> set(adds (effect \<alpha>)) \<longrightarrow> close_eq s\<^sub>1 \<Turnstile> (Not fmla))"
146
+ "(\<forall>a b. close_eq s\<^sub>0 \<Turnstile> Atom (Eq a b) \<longrightarrow> close_eq s\<^sub>1 \<Turnstile> Atom (Eq a b))"
147
+ "(\<forall>a b. close_eq s\<^sub>0 \<Turnstile> Not (Atom (Eq a b)) \<longrightarrow> close_eq s\<^sub>1 \<Turnstile> Not (Atom (Eq a b)))"
148
+ using ass(2-4)
149
+ unfolding sound_system_def sound_opr_alt by force
150
+ have "close_eq s\<^sub>1 \<Turnstile> fmla" if "fmla\<in>close_world ?M\<^sub>1" for fmla
151
+ using ass(2)
152
+ using that s1 s0_ent_cwM0
153
+ unfolding sound_system_def execute_ground_action_def wm_basic_def
154
+ apply (auto simp: in_close_world_conv)
155
+ subgoal
156
+ by (metis (no_types, lifting) DiffE UnE a apply_effect.simps ground_action.sel(2) ast_effect.sel(1) ast_effect.sel(2) close_world_extensive subsetCE)
157
+ subgoal
158
+ by (metis Diff_iff Un_iff a ground_action.sel(2) ast_domain.apply_effect.simps ast_domain.close_eq_predAtm ast_effect.sel(1) ast_effect.sel(2) formula_semantics.simps(1) formula_semantics.simps(3) in_close_world_conv is_predAtom.simps(1))
159
+ done
160
+ moreover have "(\<forall>atm. fmla \<noteq> formula.Atom atm) \<longrightarrow> s \<Turnstile> fmla" if "fmla\<in>?M\<^sub>1" for fmla s
161
+ proof-
162
+ have alpha: "(\<forall>s.\<forall>fmla\<in>set(adds (effect \<alpha>)). \<not> is_predAtom fmla \<longrightarrow> s \<Turnstile> fmla)"
163
+ using ass(2,3)
164
+ unfolding sound_system_def ast_domain.sound_opr_alt
165
+ by auto
166
+ then show ?thesis
167
+ using that
168
+ unfolding a execute_ground_action_def
169
+ using ass.prems(1)[unfolded sound_system_def]
170
+ by(cases fmla; fastforce simp: wm_basic_def)
171
+
172
+ qed
173
+ moreover have "(\<forall>opr\<in>\<Sigma>. sound_opr opr (f opr))"
174
+ using ass(2) unfolding sound_system_def
175
+ by (auto simp add:)
176
+ moreover have "wm_basic ?M\<^sub>1"
177
+ using ass(2,3)
178
+ unfolding sound_system_def execute_ground_action_def
179
+ thm sound_opr.cases
180
+ apply (cases "(\<alpha>,f \<alpha>)" rule: sound_opr.cases)
181
+ apply (auto simp: wm_basic_def)
182
+ done
183
+ ultimately have "sound_system \<Sigma> ?M\<^sub>1 s\<^sub>1 f"
184
+ unfolding sound_system_def
185
+ by (auto simp: wm_basic_def)
186
+ from ass.IH[OF this] ass.prems obtain s' where
187
+ "compose_actions (map f \<alpha>s) s\<^sub>1 = Some s' \<and> (\<forall>a\<in>close_world M'. s' \<Turnstile>\<^sub>= a)"
188
+ by auto
189
+ thus ?case by (auto simp: s1(1))
190
+ qed
191
+ with that show ?thesis by blast
192
+ qed
193
+
194
+ text \<open>More compact notation of the soundness theorem.\<close>
195
+ theorem STRIPS_sema_sound_compact_version:
196
+ "sound_system \<Sigma> M\<^sub>0 s\<^sub>0 f \<Longrightarrow> set \<alpha>s \<subseteq> \<Sigma>
197
+ \<Longrightarrow> ground_action_path M\<^sub>0 \<alpha>s M'
198
+ \<Longrightarrow> \<exists>s'. compose_actions (map f \<alpha>s) s\<^sub>0 = Some s'
199
+ \<and> (\<forall>fmla\<in>close_world M'. s' \<Turnstile>\<^sub>= fmla)"
200
+ using STRIPS_sema_sound by metis
201
+
202
+ end \<comment> \<open>Context of \<open>ast_domain\<close>\<close>
203
+
204
+ subsection \<open>Soundness Theorem for PDDL\<close>
205
+
206
+ context wf_ast_problem begin
207
+
208
+ text \<open>Mapping world models to states\<close>
209
+ definition state_to_wm :: "state \<Rightarrow> world_model"
210
+ where "state_to_wm s = ({formula.Atom (predAtm p xs) | p xs. s (p,xs)})"
211
+ definition wm_to_state :: "world_model \<Rightarrow> state"
212
+ where "wm_to_state M = (\<lambda>(p,xs). (formula.Atom (predAtm p xs)) \<in> M)"
213
+
214
+
215
+ lemma wm_to_state_eq[simp]: "wm_to_state M (p, as) \<longleftrightarrow> Atom (predAtm p as) \<in> M"
216
+ by (auto simp: wm_to_state_def)
217
+
218
+
219
+
220
+
221
+ lemma wm_to_state_inv[simp]: "wm_to_state (state_to_wm s) = s"
222
+ by (auto simp: wm_to_state_def
223
+ state_to_wm_def image_def)
224
+
225
+ text \<open>Mapping AST action instances to actions\<close>
226
+ definition "pddl_opr_to_act g_opr s = (
227
+ let M = state_to_wm s in
228
+ if (wm_to_state (close_world M)) \<Turnstile>\<^sub>= (precondition g_opr) then
229
+ Some (wm_to_state (apply_effect (effect g_opr) M))
230
+ else
231
+ None)"
232
+
233
+ definition "close_eq_M M = (M \<inter> {Atom (predAtm p xs) | p xs. True }) \<union> {Atom (Eq a a) | a. True} \<union> {\<^bold>\<not>(Atom (Eq a b)) | a b. a\<noteq>b}"
234
+
235
+ lemma atom_in_wm_eq:
236
+ "s \<Turnstile>\<^sub>= (formula.Atom atm)
237
+ \<longleftrightarrow> ((formula.Atom atm) \<in> close_eq_M (state_to_wm s))"
238
+ by (auto simp: wm_to_state_def
239
+ state_to_wm_def image_def close_eq_M_def close_eq_def split: atom.splits)
240
+
241
+ lemma atom_in_wm_2_eq:
242
+ "close_eq (wm_to_state M) \<Turnstile> (formula.Atom atm)
243
+ \<longleftrightarrow> ((formula.Atom atm) \<in> close_eq_M M)"
244
+ by (auto simp: wm_to_state_def
245
+ state_to_wm_def image_def close_eq_def close_eq_M_def split:atom.splits)
246
+
247
+ lemma not_dels_preserved:
248
+ assumes "f \<notin> (set d)" " f \<in> M"
249
+ shows "f \<in> apply_effect (Effect a d) M"
250
+ using assms
251
+ by auto
252
+
253
+ lemma adds_satisfied:
254
+ assumes "f \<in> (set a)"
255
+ shows "f \<in> apply_effect (Effect a d) M"
256
+ using assms
257
+ by auto
258
+
259
+ lemma dels_unsatisfied:
260
+ assumes "f \<in> (set d)" "f \<notin> set a"
261
+ shows "f \<notin> apply_effect (Effect a d) M"
262
+ using assms
263
+ by auto
264
+
265
+ lemma dels_unsatisfied_2:
266
+ assumes "f \<in> set (dels eff)" "f \<notin> set (adds eff)"
267
+ shows "f \<notin> apply_effect eff M"
268
+ using assms
269
+ by (cases eff; auto)
270
+
271
+ lemma wf_fmla_atm_is_atom: "wf_fmla_atom objT f \<Longrightarrow> is_predAtom f"
272
+ by (cases f rule: wf_fmla_atom.cases) auto
273
+
274
+ lemma wf_act_adds_are_atoms:
275
+ assumes "wf_effect_inst effs" "ae \<in> set (adds effs)"
276
+ shows "is_predAtom ae"
277
+ using assms
278
+ by (cases effs) (auto simp: wf_fmla_atom_alt)
279
+
280
+ lemma wf_act_adds_dels_atoms:
281
+ assumes "wf_effect_inst effs" "ae \<in> set (dels effs)"
282
+ shows "is_predAtom ae"
283
+ using assms
284
+ by (cases effs) (auto simp: wf_fmla_atom_alt)
285
+
286
+ lemma to_state_close_from_state_eq[simp]: "wm_to_state (close_world (state_to_wm s)) = s"
287
+ by (auto simp: wm_to_state_def close_world_def
288
+ state_to_wm_def image_def)
289
+
290
+
291
+
292
+ lemma wf_eff_pddl_ground_act_is_sound_opr:
293
+ assumes "wf_effect_inst (effect g_opr)"
294
+ shows "sound_opr g_opr ((pddl_opr_to_act g_opr))"
295
+ unfolding sound_opr_alt
296
+ apply(cases g_opr; safe)
297
+ subgoal for pre eff s
298
+ apply (rule exI[where x="wm_to_state(apply_effect eff (state_to_wm s))"])
299
+ apply (auto simp: pddl_opr_to_act_def Let_def split:if_splits)
300
+ subgoal for atm
301
+ by (cases eff; cases atm; auto simp: close_eq_def wm_to_state_def state_to_wm_def split: atom.splits)
302
+ subgoal for atm
303
+ by (cases eff; cases atm; auto simp: close_eq_def wm_to_state_def state_to_wm_def split: atom.splits)
304
+ subgoal for atm
305
+ using assms
306
+ by (cases eff; cases atm; force simp: close_eq_def wm_to_state_def state_to_wm_def split: atom.splits)
307
+ subgoal for fmla
308
+ using assms
309
+ by (cases eff; cases fmla rule: wf_fmla_atom.cases; force simp: close_eq_def wm_to_state_def state_to_wm_def split: atom.splits)
310
+ done
311
+ subgoal for pre eff fmla
312
+ using assms
313
+ by (cases eff; cases fmla rule: wf_fmla_atom.cases; force)
314
+ done
315
+
316
+
317
+
318
+ lemma wf_eff_impt_wf_eff_inst: "wf_effect objT eff \<Longrightarrow> wf_effect_inst eff"
319
+ by (cases eff; auto simp add: wf_fmla_atom_alt)
320
+
321
+ lemma wf_pddl_ground_act_is_sound_opr:
322
+ assumes "wf_ground_action g_opr"
323
+ shows "sound_opr g_opr (pddl_opr_to_act g_opr)"
324
+ using wf_eff_impt_wf_eff_inst wf_eff_pddl_ground_act_is_sound_opr assms
325
+ by (cases g_opr; auto)
326
+
327
+ lemma wf_action_schema_sound_inst:
328
+ assumes "action_params_match act args" "wf_action_schema act"
329
+ shows "sound_opr
330
+ (instantiate_action_schema act args)
331
+ ((pddl_opr_to_act (instantiate_action_schema act args)))"
332
+ using
333
+ wf_pddl_ground_act_is_sound_opr[
334
+ OF wf_instantiate_action_schema[OF assms]]
335
+ by blast
336
+
337
+ lemma wf_plan_act_is_sound:
338
+ assumes "wf_plan_action (PAction n args)"
339
+ shows "sound_opr
340
+ (instantiate_action_schema (the (resolve_action_schema n)) args)
341
+ ((pddl_opr_to_act
342
+ (instantiate_action_schema (the (resolve_action_schema n)) args)))"
343
+ using assms
344
+ using wf_action_schema_sound_inst wf_eff_pddl_ground_act_is_sound_opr
345
+ by (auto split: option.splits)
346
+
347
+ lemma wf_plan_act_is_sound':
348
+ assumes "wf_plan_action \<pi>"
349
+ shows "sound_opr
350
+ (resolve_instantiate \<pi>)
351
+ ((pddl_opr_to_act (resolve_instantiate \<pi>)))"
352
+ using assms wf_plan_act_is_sound
353
+ by (cases \<pi>; auto )
354
+
355
+ lemma wf_world_model_has_atoms: "f\<in>M \<Longrightarrow> wf_world_model M \<Longrightarrow> is_predAtom f"
356
+ using wf_fmla_atm_is_atom
357
+ unfolding wf_world_model_def
358
+ by auto
359
+
360
+ lemma wm_to_state_works_for_wf_wm_closed:
361
+ "wf_world_model M \<Longrightarrow> fmla\<in>close_world M \<Longrightarrow> close_eq (wm_to_state M) \<Turnstile> fmla"
362
+ apply (cases fmla rule: wf_fmla_atom.cases)
363
+ by (auto simp: wf_world_model_def close_eq_def wm_to_state_def close_world_def)
364
+
365
+ lemma wm_to_state_works_for_wf_wm: "wf_world_model M \<Longrightarrow> fmla\<in>M \<Longrightarrow> close_eq (wm_to_state M) \<Turnstile> fmla"
366
+ apply (cases fmla rule: wf_fmla_atom.cases)
367
+ by (auto simp: wf_world_model_def close_eq_def wm_to_state_def)
368
+
369
+
370
+
371
+ lemma wm_to_state_works_for_I_closed:
372
+ assumes "x \<in> close_world I"
373
+ shows "close_eq (wm_to_state I) \<Turnstile> x"
374
+ apply (rule wm_to_state_works_for_wf_wm_closed)
375
+ using assms wf_I by auto
376
+
377
+
378
+ lemma wf_wm_imp_basic: "wf_world_model M \<Longrightarrow> wm_basic M"
379
+ by (auto simp: wf_world_model_def wm_basic_def wf_fmla_atm_is_atom)
380
+
381
+ theorem wf_plan_sound_system:
382
+ assumes "\<forall>\<pi>\<in> set \<pi>s. wf_plan_action \<pi>"
383
+ shows "sound_system
384
+ (set (map resolve_instantiate \<pi>s))
385
+ I
386
+ (wm_to_state I)
387
+ ((\<lambda>\<alpha>. pddl_opr_to_act \<alpha>))"
388
+ unfolding sound_system_def
389
+ proof(intro conjI ballI)
390
+ show "close_eq(wm_to_state I) \<Turnstile> x" if "x \<in> close_world I" for x
391
+ using that[unfolded in_close_world_conv]
392
+ wm_to_state_works_for_I_closed wm_to_state_works_for_wf_wm
393
+ by (auto simp: wf_I)
394
+
395
+ show "wm_basic I" using wf_wm_imp_basic[OF wf_I] .
396
+
397
+ show "sound_opr \<alpha> (pddl_opr_to_act \<alpha>)" if "\<alpha> \<in> set (map resolve_instantiate \<pi>s)" for \<alpha>
398
+ using that
399
+ using wf_plan_act_is_sound' assms
400
+ by auto
401
+ qed
402
+
403
+ theorem wf_plan_soundness_theorem:
404
+ assumes "plan_action_path I \<pi>s M"
405
+ defines "\<alpha>s \<equiv> map (pddl_opr_to_act \<circ> resolve_instantiate) \<pi>s"
406
+ defines "s\<^sub>0 \<equiv> wm_to_state I"
407
+ shows "\<exists>s'. compose_actions \<alpha>s s\<^sub>0 = Some s' \<and> (\<forall>\<phi>\<in>close_world M. s' \<Turnstile>\<^sub>= \<phi>)"
408
+ apply (rule STRIPS_sema_sound)
409
+ apply (rule wf_plan_sound_system)
410
+ using assms
411
+ unfolding plan_action_path_def
412
+ by (auto simp add: image_def)
413
+
414
+ end \<comment> \<open>Context of \<open>wf_ast_problem\<close>\<close>
415
+
416
+ end
formal/afp/AI_Planning_Languages_Semantics/Option_Monad_Add.thy ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ theory Option_Monad_Add
2
+ imports "HOL-Library.Monad_Syntax"
3
+ begin
4
+ definition "oassert \<Phi> \<equiv> if \<Phi> then Some () else None"
5
+
6
+ fun omap :: "('a\<rightharpoonup>'b) \<Rightarrow> 'a list \<rightharpoonup> 'b list" where
7
+ "omap f [] = Some []"
8
+ | "omap f (x#xs) = do { y \<leftarrow> f x; ys \<leftarrow> omap f xs; Some (y#ys) }"
9
+
10
+ lemma omap_cong[fundef_cong]:
11
+ assumes "\<And>x. x\<in>set l' \<Longrightarrow> f x = f' x"
12
+ assumes "l=l'"
13
+ shows "omap f l = omap f' l'"
14
+ unfolding assms(2) using assms(1) by (induction l') (auto)
15
+
16
+ lemma assert_eq_iff[simp]:
17
+ "oassert \<Phi> = None \<longleftrightarrow> \<not>\<Phi>"
18
+ "oassert \<Phi> = Some u \<longleftrightarrow> \<Phi>"
19
+ unfolding oassert_def by auto
20
+
21
+ lemma omap_length[simp]: "omap f l = Some l' \<Longrightarrow> length l' = length l"
22
+ apply (induction l arbitrary: l')
23
+ apply (auto split: Option.bind_splits)
24
+ done
25
+
26
+ lemma omap_append[simp]: "omap f (xs@ys) = do {xs \<leftarrow> omap f xs; ys \<leftarrow> omap f ys; Some (xs@ys)}"
27
+ by (induction xs) (auto)
28
+
29
+
30
+ lemma omap_alt: "omap f l = Some l' \<longleftrightarrow> (l' = map (the o f) l \<and> (\<forall>x\<in>set l. f x \<noteq> None))"
31
+ apply (induction l arbitrary: l')
32
+ apply (auto split: Option.bind_splits)
33
+ done
34
+
35
+ lemma omap_alt_None: "omap f l = None \<longleftrightarrow> (\<exists>x\<in>set l. f x = None)"
36
+ apply (induction l)
37
+ apply (auto split: Option.bind_splits)
38
+ done
39
+
40
+ lemma omap_nth: "\<lbrakk>omap f l = Some l'; i<length l\<rbrakk> \<Longrightarrow> f (l!i) = Some (l'!i)"
41
+ apply (induction l arbitrary: l' i)
42
+ apply (auto split: Option.bind_splits simp: nth_Cons split: nat.splits)
43
+ done
44
+
45
+ lemma omap_eq_Nil_conv[simp]: "omap f xs = Some [] \<longleftrightarrow> xs=[]"
46
+ apply (cases xs)
47
+ apply (auto split: Option.bind_splits)
48
+ done
49
+
50
+ lemma omap_eq_Cons_conv[simp]: "omap f xs = Some (y#ys') \<longleftrightarrow> (\<exists>x xs'. xs=x#xs' \<and> f x = Some y \<and> omap f xs' = Some ys')"
51
+ apply (cases xs)
52
+ apply (auto split: Option.bind_splits)
53
+ done
54
+
55
+ lemma omap_eq_append_conv[simp]: "omap f xs = Some (ys\<^sub>1@ys\<^sub>2) \<longleftrightarrow> (\<exists>xs\<^sub>1 xs\<^sub>2. xs=xs\<^sub>1@xs\<^sub>2 \<and> omap f xs\<^sub>1 = Some ys\<^sub>1 \<and> omap f xs\<^sub>2 = Some ys\<^sub>2)"
56
+ apply (induction ys\<^sub>1 arbitrary: xs)
57
+ apply (auto 0 3 split: Option.bind_splits)
58
+ apply (metis append_Cons)
59
+ done
60
+
61
+ lemma omap_list_all2_conv: "omap f xs = Some ys \<longleftrightarrow> (list_all2 (\<lambda>x y. f x = Some y)) xs ys"
62
+ apply (induction xs arbitrary: ys)
63
+ apply (auto split: Option.bind_splits simp: )
64
+ apply (simp add: list_all2_Cons1)
65
+ apply (simp add: list_all2_Cons1)
66
+ apply (simp add: list_all2_Cons1)
67
+ apply clarsimp
68
+ by (metis option.inject)
69
+
70
+
71
+
72
+
73
+ fun omap_option where
74
+ "omap_option f None = Some None"
75
+ | "omap_option f (Some x) = do { x \<leftarrow> f x; Some (Some x) }"
76
+
77
+ lemma omap_option_conv:
78
+ "omap_option f xx = None \<longleftrightarrow> (\<exists>x. xx=Some x \<and> f x = None)"
79
+ "omap_option f xx = (Some (Some x')) \<longleftrightarrow> (\<exists>x. xx=Some x \<and> f x = Some x')"
80
+ "omap_option f xx = (Some None) \<longleftrightarrow> xx=None"
81
+ by (cases xx;auto split: Option.bind_splits)+
82
+
83
+ lemma omap_option_eq: "omap_option f x = (case x of None \<Rightarrow> Some None | Some x \<Rightarrow> do { x \<leftarrow> f x; Some (Some x) })"
84
+ by (auto split: option.split)
85
+
86
+ fun omap_prod where
87
+ "omap_prod f\<^sub>1 f\<^sub>2 (a,b) = do { a\<leftarrow>f\<^sub>1 a; b\<leftarrow>f\<^sub>2 b; Some (a,b) }"
88
+
89
+
90
+ (* Extend map function for datatype to option monad.
91
+ TODO: Show reasonable lemmas, like parametricity, etc.
92
+ Hopefully only depending on BNF-property of datatype
93
+ *)
94
+ definition "omap_dt setf mapf f obj \<equiv> do {
95
+ oassert (\<forall>x\<in>setf obj. f x \<noteq> None);
96
+ Some (mapf (the o f) obj)
97
+ }"
98
+
99
+
100
+
101
+ end
formal/afp/AI_Planning_Languages_Semantics/PDDL_STRIPS_Checker.thy ADDED
@@ -0,0 +1,406 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ section \<open>Executable PDDL Checker\<close>
2
+ theory PDDL_STRIPS_Checker
3
+ imports
4
+ PDDL_STRIPS_Semantics
5
+
6
+ Error_Monad_Add
7
+ "HOL.String"
8
+
9
+ (*"HOL-Library.Code_Char" TODO: This might lead to performance loss! CHECK! *)
10
+ "HOL-Library.Code_Target_Nat"
11
+
12
+ "HOL-Library.While_Combinator"
13
+
14
+ "Containers.Containers"
15
+ begin
16
+
17
+ subsection \<open>Generic DFS Reachability Checker\<close>
18
+ text \<open>Used for subtype checks\<close>
19
+
20
+ definition "E_of_succ succ \<equiv> { (u,v). v\<in>set (succ u) }"
21
+ lemma succ_as_E: "set (succ x) = E_of_succ succ `` {x}"
22
+ unfolding E_of_succ_def by auto
23
+
24
+ context
25
+ fixes succ :: "'a \<Rightarrow> 'a list"
26
+ begin
27
+
28
+ private abbreviation (input) "E \<equiv> E_of_succ succ"
29
+
30
+
31
+ definition "dfs_reachable D w \<equiv>
32
+ let (V,w,brk) = while (\<lambda>(V,w,brk). \<not>brk \<and> w\<noteq>[]) (\<lambda>(V,w,_).
33
+ case w of v#w \<Rightarrow>
34
+ if D v then (V,v#w,True)
35
+ else if v\<in>V then (V,w,False)
36
+ else
37
+ let V = insert v V in
38
+ let w = succ v @ w in
39
+ (V,w,False)
40
+ ) ({},w,False)
41
+ in brk"
42
+
43
+
44
+ context
45
+ fixes w\<^sub>0 :: "'a list"
46
+ assumes finite_dfs_reachable[simp, intro!]: "finite (E\<^sup>* `` set w\<^sub>0)"
47
+ begin
48
+
49
+ private abbreviation (input) "W\<^sub>0 \<equiv> set w\<^sub>0"
50
+
51
+ definition "dfs_reachable_invar D V W brk \<longleftrightarrow>
52
+ W\<^sub>0 \<subseteq> W \<union> V
53
+ \<and> W \<union> V \<subseteq> E\<^sup>* `` W\<^sub>0
54
+ \<and> E``V \<subseteq> W \<union> V
55
+ \<and> Collect D \<inter> V = {}
56
+ \<and> (brk \<longrightarrow> Collect D \<inter> E\<^sup>* `` W\<^sub>0 \<noteq> {})"
57
+
58
+ lemma card_decreases: "
59
+ \<lbrakk>finite V; y \<notin> V; dfs_reachable_invar D V (Set.insert y W) brk \<rbrakk>
60
+ \<Longrightarrow> card (E\<^sup>* `` W\<^sub>0 - Set.insert y V) < card (E\<^sup>* `` W\<^sub>0 - V)"
61
+ apply (rule psubset_card_mono)
62
+ apply (auto simp: dfs_reachable_invar_def)
63
+ done
64
+
65
+ lemma all_neq_Cons_is_Nil[simp]: (* Odd term remaining in goal \<dots> *)
66
+ "(\<forall>y ys. x2 \<noteq> y # ys) \<longleftrightarrow> x2 = []" by (cases x2) auto
67
+
68
+ lemma dfs_reachable_correct: "dfs_reachable D w\<^sub>0 \<longleftrightarrow> Collect D \<inter> E\<^sup>* `` set w\<^sub>0 \<noteq> {}"
69
+ unfolding dfs_reachable_def
70
+ apply (rule while_rule[where
71
+ P="\<lambda>(V,w,brk). dfs_reachable_invar D V (set w) brk \<and> finite V"
72
+ and r="measure (\<lambda>V. card (E\<^sup>* `` (set w\<^sub>0) - V)) <*lex*> measure length <*lex*> measure (\<lambda>True\<Rightarrow>0 | False\<Rightarrow>1)"
73
+ ])
74
+ subgoal by (auto simp: dfs_reachable_invar_def)
75
+ subgoal
76
+ apply (auto simp: neq_Nil_conv succ_as_E[of succ] split: if_splits)
77
+ by (auto simp: dfs_reachable_invar_def Image_iff intro: rtrancl.rtrancl_into_rtrancl)
78
+ subgoal by (fastforce simp: dfs_reachable_invar_def dest: Image_closed_trancl)
79
+ subgoal by blast
80
+ subgoal by (auto simp: neq_Nil_conv card_decreases)
81
+ done
82
+
83
+ end
84
+
85
+ definition "tab_succ l \<equiv> Mapping.lookup_default [] (fold (\<lambda>(u,v). Mapping.map_default u [] (Cons v)) l Mapping.empty)"
86
+
87
+
88
+ lemma Some_eq_map_option [iff]: "(Some y = map_option f xo) = (\<exists>z. xo = Some z \<and> f z = y)"
89
+ by (auto simp add: map_option_case split: option.split)
90
+
91
+
92
+ lemma tab_succ_correct: "E_of_succ (tab_succ l) = set l"
93
+ proof -
94
+ have "set (Mapping.lookup_default [] (fold (\<lambda>(u,v). Mapping.map_default u [] (Cons v)) l m) u) = set l `` {u} \<union> set (Mapping.lookup_default [] m u)"
95
+ for m u
96
+ apply (induction l arbitrary: m)
97
+ by (auto
98
+ simp: Mapping.lookup_default_def Mapping.map_default_def Mapping.default_def
99
+ simp: lookup_map_entry' lookup_update' keys_is_none_rep Option.is_none_def
100
+ split: if_splits
101
+ )
102
+ from this[where m=Mapping.empty] show ?thesis
103
+ by (auto simp: E_of_succ_def tab_succ_def lookup_default_empty)
104
+ qed
105
+
106
+ end
107
+
108
+ lemma finite_imp_finite_dfs_reachable:
109
+ "\<lbrakk>finite E; finite S\<rbrakk> \<Longrightarrow> finite (E\<^sup>*``S)"
110
+ apply (rule finite_subset[where B="S \<union> (Relation.Domain E \<union> Relation.Range E)"])
111
+ apply (auto simp: intro: finite_Domain finite_Range elim: rtranclE)
112
+ done
113
+
114
+ lemma dfs_reachable_tab_succ_correct: "dfs_reachable (tab_succ l) D vs\<^sub>0 \<longleftrightarrow> Collect D \<inter> (set l)\<^sup>*``set vs\<^sub>0 \<noteq> {}"
115
+ apply (subst dfs_reachable_correct)
116
+ by (simp_all add: tab_succ_correct finite_imp_finite_dfs_reachable)
117
+
118
+
119
+
120
+ subsection \<open>Implementation Refinements\<close>
121
+
122
+ subsubsection \<open>Of-Type\<close>
123
+
124
+ definition "of_type_impl G oT T \<equiv> (\<forall>pt\<in>set (primitives oT). dfs_reachable G ((=) pt) (primitives T))"
125
+
126
+
127
+ fun ty_term' where
128
+ "ty_term' varT objT (term.VAR v) = varT v"
129
+ | "ty_term' varT objT (term.CONST c) = Mapping.lookup objT c"
130
+
131
+ lemma ty_term'_correct_aux: "ty_term' varT objT t = ty_term varT (Mapping.lookup objT) t"
132
+ by (cases t) auto
133
+
134
+ lemma ty_term'_correct[simp]: "ty_term' varT objT = ty_term varT (Mapping.lookup objT)"
135
+ using ty_term'_correct_aux by auto
136
+
137
+ context ast_domain begin
138
+
139
+ definition "of_type1 pt T \<longleftrightarrow> pt \<in> subtype_rel\<^sup>* `` set (primitives T)"
140
+
141
+ lemma of_type_refine1: "of_type oT T \<longleftrightarrow> (\<forall>pt\<in>set (primitives oT). of_type1 pt T)"
142
+ unfolding of_type_def of_type1_def by auto
143
+
144
+ definition "STG \<equiv> (tab_succ (map subtype_edge (types D)))"
145
+
146
+ lemma subtype_rel_impl: "subtype_rel = E_of_succ (tab_succ (map subtype_edge (types D)))"
147
+ by (simp add: tab_succ_correct subtype_rel_def)
148
+
149
+ lemma of_type1_impl: "of_type1 pt T \<longleftrightarrow> dfs_reachable (tab_succ (map subtype_edge (types D))) ((=)pt) (primitives T)"
150
+ by (simp add: subtype_rel_impl of_type1_def dfs_reachable_tab_succ_correct tab_succ_correct)
151
+
152
+ lemma of_type_impl_correct: "of_type_impl STG oT T \<longleftrightarrow> of_type oT T"
153
+ unfolding of_type1_impl STG_def of_type_impl_def of_type_refine1 ..
154
+
155
+ definition mp_constT :: "(object, type) mapping" where
156
+ "mp_constT = Mapping.of_alist (consts D)"
157
+
158
+ lemma mp_objT_correct[simp]: "Mapping.lookup mp_constT = constT"
159
+ unfolding mp_constT_def constT_def
160
+ by transfer (simp add: Map_To_Mapping.map_apply_def)
161
+
162
+
163
+
164
+
165
+
166
+
167
+ text \<open>Lifting the subtype-graph through wf-checker\<close>
168
+ context
169
+ fixes ty_ent :: "'ent \<rightharpoonup> type" \<comment> \<open>Entity's type, None if invalid\<close>
170
+ begin
171
+
172
+ definition "is_of_type' stg v T \<longleftrightarrow> (
173
+ case ty_ent v of
174
+ Some vT \<Rightarrow> of_type_impl stg vT T
175
+ | None \<Rightarrow> False)"
176
+
177
+ lemma is_of_type'_correct: "is_of_type' STG v T = is_of_type ty_ent v T"
178
+ unfolding is_of_type'_def is_of_type_def of_type_impl_correct ..
179
+
180
+ fun wf_pred_atom' where "wf_pred_atom' stg (p,vs) \<longleftrightarrow> (case sig p of
181
+ None \<Rightarrow> False
182
+ | Some Ts \<Rightarrow> list_all2 (is_of_type' stg) vs Ts)"
183
+
184
+ lemma wf_pred_atom'_correct: "wf_pred_atom' STG pvs = wf_pred_atom ty_ent pvs"
185
+ by (cases pvs) (auto simp: is_of_type'_correct[abs_def] split:option.split)
186
+
187
+ fun wf_atom' :: "_ \<Rightarrow> 'ent atom \<Rightarrow> bool" where
188
+ "wf_atom' stg (atom.predAtm p vs) \<longleftrightarrow> wf_pred_atom' stg (p,vs)"
189
+ | "wf_atom' stg (atom.Eq a b) = (ty_ent a \<noteq> None \<and> ty_ent b \<noteq> None)"
190
+
191
+ lemma wf_atom'_correct: "wf_atom' STG a = wf_atom ty_ent a"
192
+ by (cases a) (auto simp: wf_pred_atom'_correct is_of_type'_correct[abs_def] split: option.splits)
193
+
194
+ fun wf_fmla' :: "_ \<Rightarrow> ('ent atom) formula \<Rightarrow> bool" where
195
+ "wf_fmla' stg (Atom a) \<longleftrightarrow> wf_atom' stg a"
196
+ | "wf_fmla' stg \<bottom> \<longleftrightarrow> True"
197
+ | "wf_fmla' stg (\<phi>1 \<^bold>\<and> \<phi>2) \<longleftrightarrow> (wf_fmla' stg \<phi>1 \<and> wf_fmla' stg \<phi>2)"
198
+ | "wf_fmla' stg (\<phi>1 \<^bold>\<or> \<phi>2) \<longleftrightarrow> (wf_fmla' stg \<phi>1 \<and> wf_fmla' stg \<phi>2)"
199
+ | "wf_fmla' stg (\<phi>1 \<^bold>\<rightarrow> \<phi>2) \<longleftrightarrow> (wf_fmla' stg \<phi>1 \<and> wf_fmla' stg \<phi>2)"
200
+ | "wf_fmla' stg (\<^bold>\<not>\<phi>) \<longleftrightarrow> wf_fmla' stg \<phi>"
201
+
202
+ lemma wf_fmla'_correct: "wf_fmla' STG \<phi> \<longleftrightarrow> wf_fmla ty_ent \<phi>"
203
+ by (induction \<phi> rule: wf_fmla.induct) (auto simp: wf_atom'_correct)
204
+
205
+ fun wf_fmla_atom1' where
206
+ "wf_fmla_atom1' stg (Atom (predAtm p vs)) \<longleftrightarrow> wf_pred_atom' stg (p,vs)"
207
+ | "wf_fmla_atom1' stg _ \<longleftrightarrow> False"
208
+
209
+ lemma wf_fmla_atom1'_correct: "wf_fmla_atom1' STG \<phi> = wf_fmla_atom ty_ent \<phi>"
210
+ by (cases \<phi> rule: wf_fmla_atom.cases) (auto
211
+ simp: wf_atom'_correct is_of_type'_correct[abs_def] split: option.splits)
212
+
213
+ fun wf_effect' where
214
+ "wf_effect' stg (Effect a d) \<longleftrightarrow>
215
+ (\<forall>ae\<in>set a. wf_fmla_atom1' stg ae)
216
+ \<and> (\<forall>de\<in>set d. wf_fmla_atom1' stg de)"
217
+
218
+ lemma wf_effect'_correct: "wf_effect' STG e = wf_effect ty_ent e"
219
+ by (cases e) (auto simp: wf_fmla_atom1'_correct)
220
+
221
+ end \<comment> \<open>Context fixing \<open>ty_ent\<close>\<close>
222
+
223
+ fun wf_action_schema' :: "_ \<Rightarrow> _ \<Rightarrow> ast_action_schema \<Rightarrow> bool" where
224
+ "wf_action_schema' stg conT (Action_Schema n params pre eff) \<longleftrightarrow> (
225
+ let
226
+ tyv = ty_term' (map_of params) conT
227
+ in
228
+ distinct (map fst params)
229
+ \<and> wf_fmla' tyv stg pre
230
+ \<and> wf_effect' tyv stg eff)"
231
+
232
+ lemma wf_action_schema'_correct: "wf_action_schema' STG mp_constT s = wf_action_schema s"
233
+ by (cases s) (auto simp: wf_fmla'_correct wf_effect'_correct)
234
+
235
+ definition wf_domain' :: "_ \<Rightarrow> _ \<Rightarrow> bool" where
236
+ "wf_domain' stg conT \<equiv>
237
+ wf_types
238
+ \<and> distinct (map (predicate_decl.pred) (predicates D))
239
+ \<and> (\<forall>p\<in>set (predicates D). wf_predicate_decl p)
240
+ \<and> distinct (map fst (consts D))
241
+ \<and> (\<forall>(n,T)\<in>set (consts D). wf_type T)
242
+ \<and> distinct (map ast_action_schema.name (actions D))
243
+ \<and> (\<forall>a\<in>set (actions D). wf_action_schema' stg conT a)
244
+ "
245
+
246
+ lemma wf_domain'_correct: "wf_domain' STG mp_constT = wf_domain"
247
+ unfolding wf_domain_def wf_domain'_def
248
+ by (auto simp: wf_action_schema'_correct)
249
+
250
+
251
+ end \<comment> \<open>Context of \<open>ast_domain\<close>\<close>
252
+
253
+ subsubsection \<open>Application of Effects\<close>
254
+
255
+ context ast_domain begin
256
+ text \<open>We implement the application of an effect by explicit iteration over
257
+ the additions and deletions\<close>
258
+ fun apply_effect_exec
259
+ :: "object ast_effect \<Rightarrow> world_model \<Rightarrow> world_model"
260
+ where
261
+ "apply_effect_exec (Effect a d) s
262
+ = fold (\<lambda>add s. Set.insert add s) a
263
+ (fold (\<lambda>del s. Set.remove del s) d s)"
264
+
265
+ lemma apply_effect_exec_refine[simp]:
266
+ "apply_effect_exec (Effect (a) (d)) s
267
+ = apply_effect (Effect (a) (d)) s"
268
+ proof(induction a arbitrary: s)
269
+ case Nil
270
+ then show ?case
271
+ proof(induction d arbitrary: s)
272
+ case Nil
273
+ then show ?case by auto
274
+ next
275
+ case (Cons a d)
276
+ then show ?case
277
+ by (auto simp add: image_def)
278
+ qed
279
+ next
280
+ case (Cons a a)
281
+ then show ?case
282
+ proof(induction d arbitrary: s)
283
+ case Nil
284
+ then show ?case by (auto; metis Set.insert_def sup_assoc insert_iff)
285
+ next
286
+ case (Cons a d)
287
+ then show ?case
288
+ by (auto simp: Un_commute minus_set_fold union_set_fold)
289
+ qed
290
+ qed
291
+
292
+ lemmas apply_effect_eq_impl_eq
293
+ = apply_effect_exec_refine[symmetric, unfolded apply_effect_exec.simps]
294
+
295
+ end \<comment> \<open>Context of \<open>ast_domain\<close>\<close>
296
+
297
+ subsubsection \<open>Well-Formedness\<close>
298
+
299
+ context ast_problem begin
300
+
301
+ text \<open> We start by defining a mapping from objects to types. The container
302
+ framework will generate efficient, red-black tree based code for that
303
+ later. \<close>
304
+
305
+ type_synonym objT = "(object, type) mapping"
306
+
307
+ definition mp_objT :: "(object, type) mapping" where
308
+ "mp_objT = Mapping.of_alist (consts D @ objects P)"
309
+
310
+ lemma mp_objT_correct[simp]: "Mapping.lookup mp_objT = objT"
311
+ unfolding mp_objT_def objT_alt
312
+ by transfer (simp add: Map_To_Mapping.map_apply_def)
313
+
314
+ text \<open>We refine the typecheck to use the mapping\<close>
315
+
316
+ definition "is_obj_of_type_impl stg mp n T = (
317
+ case Mapping.lookup mp n of None \<Rightarrow> False | Some oT \<Rightarrow> of_type_impl stg oT T
318
+ )"
319
+
320
+ lemma is_obj_of_type_impl_correct[simp]:
321
+ "is_obj_of_type_impl STG mp_objT = is_obj_of_type"
322
+ apply (intro ext)
323
+ apply (auto simp: is_obj_of_type_impl_def is_obj_of_type_def of_type_impl_correct split: option.split)
324
+ done
325
+
326
+ text \<open>We refine the well-formedness checks to use the mapping\<close>
327
+
328
+ definition wf_fact' :: "objT \<Rightarrow> _ \<Rightarrow> fact \<Rightarrow> bool"
329
+ where
330
+ "wf_fact' ot stg \<equiv> wf_pred_atom' (Mapping.lookup ot) stg"
331
+
332
+ lemma wf_fact'_correct[simp]: "wf_fact' mp_objT STG = wf_fact"
333
+ by (auto simp: wf_fact'_def wf_fact_def wf_pred_atom'_correct[abs_def])
334
+
335
+
336
+ definition "wf_fmla_atom2' mp stg f
337
+ = (case f of formula.Atom (predAtm p vs) \<Rightarrow> (wf_fact' mp stg (p,vs)) | _ \<Rightarrow> False)"
338
+
339
+ lemma wf_fmla_atom2'_correct[simp]:
340
+ "wf_fmla_atom2' mp_objT STG \<phi> = wf_fmla_atom objT \<phi>"
341
+ apply (cases \<phi> rule: wf_fmla_atom.cases)
342
+ by (auto simp: wf_fmla_atom2'_def wf_fact_def split: option.splits)
343
+
344
+ definition "wf_problem' stg conT mp \<equiv>
345
+ wf_domain' stg conT
346
+ \<and> distinct (map fst (objects P) @ map fst (consts D))
347
+ \<and> (\<forall>(n,T)\<in>set (objects P). wf_type T)
348
+ \<and> distinct (init P)
349
+ \<and> (\<forall>f\<in>set (init P). wf_fmla_atom2' mp stg f)
350
+ \<and> wf_fmla' (Mapping.lookup mp) stg (goal P)"
351
+
352
+ lemma wf_problem'_correct:
353
+ "wf_problem' STG mp_constT mp_objT = wf_problem"
354
+ unfolding wf_problem_def wf_problem'_def wf_world_model_def
355
+ by (auto simp: wf_domain'_correct wf_fmla'_correct)
356
+
357
+
358
+ text \<open>Instantiating actions will yield well-founded effects.
359
+ Corollary of @{thm wf_instantiate_action_schema}.\<close>
360
+ lemma wf_effect_inst_weak:
361
+ fixes a args
362
+ defines "ai \<equiv> instantiate_action_schema a args"
363
+ assumes A: "action_params_match a args"
364
+ "wf_action_schema a"
365
+ shows "wf_effect_inst (effect ai)"
366
+ using wf_instantiate_action_schema[OF A] unfolding ai_def[symmetric]
367
+ by (cases ai) (auto simp: wf_effect_inst_alt)
368
+
369
+
370
+ end \<comment> \<open>Context of \<open>ast_problem\<close>\<close>
371
+
372
+
373
+ context wf_ast_domain begin
374
+ text \<open>Resolving an action yields a well-founded action schema.\<close>
375
+ (* TODO: This must be implicitly proved when showing that plan execution
376
+ preserves wf. Try to remove this redundancy!*)
377
+ lemma resolve_action_wf:
378
+ assumes "resolve_action_schema n = Some a"
379
+ shows "wf_action_schema a"
380
+ proof -
381
+ from wf_domain have
382
+ X1: "distinct (map ast_action_schema.name (actions D))"
383
+ and X2: "\<forall>a\<in>set (actions D). wf_action_schema a"
384
+ unfolding wf_domain_def by auto
385
+
386
+ show ?thesis
387
+ using assms unfolding resolve_action_schema_def
388
+ by (auto simp add: index_by_eq_Some_eq[OF X1] X2)
389
+ qed
390
+
391
+ end \<comment> \<open>Context of \<open>ast_domain\<close>\<close>
392
+
393
+
394
+ subsubsection \<open>Execution of Plan Actions\<close>
395
+
396
+ text \<open>We will perform two refinement steps, to summarize redundant operations\<close>
397
+
398
+ text \<open>We first lift action schema lookup into the error monad. \<close>
399
+ context ast_domain begin
400
+ definition "resolve_action_schemaE n \<equiv>
401
+ lift_opt
402
+ (resolve_action_schema n)
403
+ (ERR (shows ''No such action schema '' o shows n))"
404
+ end \<comment> \<open>Context of \<open>ast_domain\<close>\<close>
405
+
406
+ end \<comment> \<open>Theory\<close>
formal/afp/AI_Planning_Languages_Semantics/PDDL_STRIPS_Semantics.thy ADDED
@@ -0,0 +1,969 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ section \<open>PDDL and STRIPS Semantics\<close>
2
+ theory PDDL_STRIPS_Semantics
3
+ imports
4
+ "Propositional_Proof_Systems.Formulas"
5
+ "Propositional_Proof_Systems.Sema"
6
+ "Propositional_Proof_Systems.Consistency"
7
+ "Automatic_Refinement.Misc"
8
+ "Automatic_Refinement.Refine_Util"
9
+ begin
10
+ no_notation insert ("_ \<triangleright> _" [56,55] 55)
11
+
12
+ subsection \<open>Utility Functions\<close>
13
+ definition "index_by f l \<equiv> map_of (map (\<lambda>x. (f x,x)) l)"
14
+
15
+ lemma index_by_eq_Some_eq[simp]:
16
+ assumes "distinct (map f l)"
17
+ shows "index_by f l n = Some x \<longleftrightarrow> (x\<in>set l \<and> f x = n)"
18
+ unfolding index_by_def
19
+ using assms
20
+ by (auto simp: o_def)
21
+
22
+ lemma index_by_eq_SomeD:
23
+ shows "index_by f l n = Some x \<Longrightarrow> (x\<in>set l \<and> f x = n)"
24
+ unfolding index_by_def
25
+ by (auto dest: map_of_SomeD)
26
+
27
+
28
+ lemma lookup_zip_idx_eq:
29
+ assumes "length params = length args"
30
+ assumes "i<length args"
31
+ assumes "distinct params"
32
+ assumes "k = params ! i"
33
+ shows "map_of (zip params args) k = Some (args ! i)"
34
+ using assms
35
+ by (auto simp: in_set_conv_nth)
36
+
37
+ lemma rtrancl_image_idem[simp]: "R\<^sup>* `` R\<^sup>* `` s = R\<^sup>* `` s"
38
+ by (metis relcomp_Image rtrancl_idemp_self_comp)
39
+
40
+
41
+ subsection \<open>Abstract Syntax\<close>
42
+
43
+ subsubsection \<open>Generic Entities\<close>
44
+ type_synonym name = string
45
+
46
+ datatype predicate = Pred (name: name)
47
+
48
+ text \<open>Some of the AST entities are defined over a polymorphic \<open>'val\<close> type,
49
+ which gets either instantiated by variables (for domains)
50
+ or objects (for problems).
51
+ \<close>
52
+
53
+ text \<open>An atom is either a predicate with arguments, or an equality statement.\<close>
54
+ datatype 'ent atom = predAtm (predicate: predicate) (arguments: "'ent list")
55
+ | Eq (lhs: 'ent) (rhs: 'ent)
56
+
57
+ text \<open>A type is a list of primitive type names.
58
+ To model a primitive type, we use a singleton list.\<close>
59
+ datatype type = Either (primitives: "name list")
60
+
61
+ text \<open>An effect contains a list of values to be added, and a list of values
62
+ to be removed.\<close>
63
+ datatype 'ent ast_effect = Effect (adds: "('ent atom formula) list") (dels: "('ent atom formula) list")
64
+
65
+ text \<open>Variables are identified by their names.\<close>
66
+ datatype variable = varname: Var name
67
+ text \<open>Objects and constants are identified by their names\<close>
68
+ datatype object = name: Obj name
69
+
70
+ datatype "term" = VAR variable | CONST object
71
+ hide_const (open) VAR CONST \<comment> \<open>Refer to constructors by qualified names only\<close>
72
+
73
+
74
+
75
+
76
+ subsubsection \<open>Domains\<close>
77
+
78
+ text \<open>An action schema has a name, a typed parameter list, a precondition,
79
+ and an effect.\<close>
80
+ datatype ast_action_schema = Action_Schema
81
+ (name: name)
82
+ (parameters: "(variable \<times> type) list")
83
+ (precondition: "term atom formula")
84
+ (effect: "term ast_effect")
85
+
86
+ text \<open>A predicate declaration contains the predicate's name and its
87
+ argument types.\<close>
88
+ datatype predicate_decl = PredDecl
89
+ (pred: predicate)
90
+ (argTs: "type list")
91
+
92
+ text \<open>A domain contains the declarations of primitive types, predicates,
93
+ and action schemas.\<close>
94
+ datatype ast_domain = Domain
95
+ (types: "(name \<times> name) list") \<comment> \<open> \<open>(type, supertype)\<close> declarations. \<close>
96
+ (predicates: "predicate_decl list")
97
+ ("consts": "(object \<times> type) list")
98
+ (actions: "ast_action_schema list")
99
+
100
+ subsubsection \<open>Problems\<close>
101
+
102
+
103
+ text \<open>A fact is a predicate applied to objects.\<close>
104
+ type_synonym fact = "predicate \<times> object list"
105
+
106
+ text \<open>A problem consists of a domain, a list of objects,
107
+ a description of the initial state, and a description of the goal state. \<close>
108
+ datatype ast_problem = Problem
109
+ (domain: ast_domain)
110
+ (objects: "(object \<times> type) list")
111
+ (init: "object atom formula list")
112
+ (goal: "object atom formula")
113
+
114
+
115
+ subsubsection \<open>Plans\<close>
116
+ datatype plan_action = PAction
117
+ (name: name)
118
+ (arguments: "object list")
119
+
120
+ type_synonym plan = "plan_action list"
121
+
122
+ subsubsection \<open>Ground Actions\<close>
123
+ text \<open>The following datatype represents an action scheme that has been
124
+ instantiated by replacing the arguments with concrete objects,
125
+ also called ground action.
126
+ \<close>
127
+ datatype ground_action = Ground_Action
128
+ (precondition: "(object atom) formula")
129
+ (effect: "object ast_effect")
130
+
131
+
132
+
133
+ subsection \<open>Closed-World Assumption, Equality, and Negation\<close>
134
+ text \<open>Discriminator for atomic predicate formulas.\<close>
135
+ fun is_predAtom where
136
+ "is_predAtom (Atom (predAtm _ _)) = True" | "is_predAtom _ = False"
137
+
138
+
139
+ text \<open>The world model is a set of (atomic) formulas\<close>
140
+ type_synonym world_model = "object atom formula set"
141
+
142
+ text \<open>It is basic, if it only contains atoms\<close>
143
+ definition "wm_basic M \<equiv> \<forall>a\<in>M. is_predAtom a"
144
+
145
+ text \<open>A valuation extracted from the atoms of the world model\<close>
146
+ definition valuation :: "world_model \<Rightarrow> object atom valuation"
147
+ where "valuation M \<equiv> \<lambda>predAtm p xs \<Rightarrow> Atom (predAtm p xs) \<in> M | Eq a b \<Rightarrow> a=b"
148
+
149
+ text \<open>Augment a world model by adding negated versions of all atoms
150
+ not contained in it, as well as interpretations of equality.\<close>
151
+ definition close_world :: "world_model \<Rightarrow> world_model" where "close_world M =
152
+ M \<union> {\<^bold>\<not>(Atom (predAtm p as)) | p as. Atom (predAtm p as) \<notin> M}
153
+ \<union> {Atom (Eq a a) | a. True} \<union> {\<^bold>\<not>(Atom (Eq a b)) | a b. a\<noteq>b}"
154
+
155
+ definition "close_neg M \<equiv> M \<union> {\<^bold>\<not>(Atom a) | a. Atom a \<notin> M}"
156
+ lemma "wm_basic M \<Longrightarrow> close_world M = close_neg (M \<union> {Atom (Eq a a) | a. True})"
157
+ unfolding close_world_def close_neg_def wm_basic_def
158
+ apply clarsimp
159
+ apply (auto 0 3)
160
+ by (metis atom.exhaust)
161
+
162
+
163
+ abbreviation cw_entailment (infix "\<^sup>c\<TTurnstile>\<^sub>=" 53) where
164
+ "M \<^sup>c\<TTurnstile>\<^sub>= \<phi> \<equiv> close_world M \<TTurnstile> \<phi>"
165
+
166
+
167
+ lemma
168
+ close_world_extensive: "M \<subseteq> close_world M" and
169
+ close_world_idem[simp]: "close_world (close_world M) = close_world M"
170
+ by (auto simp: close_world_def)
171
+
172
+ lemma in_close_world_conv:
173
+ "\<phi> \<in> close_world M \<longleftrightarrow> (
174
+ \<phi>\<in>M
175
+ \<or> (\<exists>p as. \<phi>=\<^bold>\<not>(Atom (predAtm p as)) \<and> Atom (predAtm p as)\<notin>M)
176
+ \<or> (\<exists>a. \<phi>=Atom (Eq a a))
177
+ \<or> (\<exists>a b. \<phi>=\<^bold>\<not>(Atom (Eq a b)) \<and> a\<noteq>b)
178
+ )"
179
+ by (auto simp: close_world_def)
180
+
181
+ lemma valuation_aux_1:
182
+ fixes M :: world_model and \<phi> :: "object atom formula"
183
+ defines "C \<equiv> close_world M"
184
+ assumes A: "\<forall>\<phi>\<in>C. \<A> \<Turnstile> \<phi>"
185
+ shows "\<A> = valuation M"
186
+ using A unfolding C_def
187
+ apply -
188
+ apply (auto simp: in_close_world_conv valuation_def Ball_def intro!: ext split: atom.split)
189
+ apply (metis formula_semantics.simps(1) formula_semantics.simps(3))
190
+ apply (metis formula_semantics.simps(1) formula_semantics.simps(3))
191
+ by (metis atom.collapse(2) formula_semantics.simps(1) is_predAtm_def)
192
+
193
+
194
+
195
+ lemma valuation_aux_2:
196
+ assumes "wm_basic M"
197
+ shows "(\<forall>G\<in>close_world M. valuation M \<Turnstile> G)"
198
+ using assms unfolding wm_basic_def
199
+ by (force simp: in_close_world_conv valuation_def elim: is_predAtom.elims)
200
+
201
+ lemma val_imp_close_world: "valuation M \<Turnstile> \<phi> \<Longrightarrow> M \<^sup>c\<TTurnstile>\<^sub>= \<phi>"
202
+ unfolding entailment_def
203
+ using valuation_aux_1
204
+ by blast
205
+
206
+ lemma close_world_imp_val:
207
+ "wm_basic M \<Longrightarrow> M \<^sup>c\<TTurnstile>\<^sub>= \<phi> \<Longrightarrow> valuation M \<Turnstile> \<phi>"
208
+ unfolding entailment_def using valuation_aux_2 by blast
209
+
210
+ text \<open>Main theorem of this section:
211
+ If a world model \<open>M\<close> contains only atoms, its induced valuation
212
+ satisfies a formula \<open>\<phi>\<close> if and only if the closure of \<open>M\<close> entails \<open>\<phi>\<close>.
213
+
214
+ Note that there are no syntactic restrictions on \<open>\<phi>\<close>,
215
+ in particular, \<open>\<phi>\<close> may contain negation.
216
+ \<close>
217
+ theorem valuation_iff_close_world:
218
+ assumes "wm_basic M"
219
+ shows "valuation M \<Turnstile> \<phi> \<longleftrightarrow> M \<^sup>c\<TTurnstile>\<^sub>= \<phi>"
220
+ using assms val_imp_close_world close_world_imp_val by blast
221
+
222
+
223
+ subsubsection \<open>Proper Generalization\<close>
224
+ text \<open>Adding negation and equality is a proper generalization of the
225
+ case without negation and equality\<close>
226
+
227
+ fun is_STRIPS_fmla :: "'ent atom formula \<Rightarrow> bool" where
228
+ "is_STRIPS_fmla (Atom (predAtm _ _)) \<longleftrightarrow> True"
229
+ | "is_STRIPS_fmla (\<bottom>) \<longleftrightarrow> True"
230
+ | "is_STRIPS_fmla (\<phi>\<^sub>1 \<^bold>\<and> \<phi>\<^sub>2) \<longleftrightarrow> is_STRIPS_fmla \<phi>\<^sub>1 \<and> is_STRIPS_fmla \<phi>\<^sub>2"
231
+ | "is_STRIPS_fmla (\<phi>\<^sub>1 \<^bold>\<or> \<phi>\<^sub>2) \<longleftrightarrow> is_STRIPS_fmla \<phi>\<^sub>1 \<and> is_STRIPS_fmla \<phi>\<^sub>2"
232
+ | "is_STRIPS_fmla (\<^bold>\<not>\<bottom>) \<longleftrightarrow> True"
233
+ | "is_STRIPS_fmla _ \<longleftrightarrow> False"
234
+
235
+ lemma aux1: "\<lbrakk>wm_basic M; is_STRIPS_fmla \<phi>; valuation M \<Turnstile> \<phi>; \<forall>G\<in>M. \<A> \<Turnstile> G\<rbrakk> \<Longrightarrow> \<A> \<Turnstile> \<phi>"
236
+ apply(induction \<phi> rule: is_STRIPS_fmla.induct)
237
+ by (auto simp: valuation_def)
238
+
239
+ lemma aux2: "\<lbrakk>wm_basic M; is_STRIPS_fmla \<phi>; \<forall>\<A>. (\<forall>G\<in>M. \<A> \<Turnstile> G) \<longrightarrow> \<A> \<Turnstile> \<phi>\<rbrakk> \<Longrightarrow> valuation M \<Turnstile> \<phi>"
240
+ apply(induction \<phi> rule: is_STRIPS_fmla.induct)
241
+ apply simp_all
242
+ apply (metis in_close_world_conv valuation_aux_2)
243
+ using in_close_world_conv valuation_aux_2 apply blast
244
+ using in_close_world_conv valuation_aux_2 by auto
245
+
246
+
247
+ lemma valuation_iff_STRIPS:
248
+ assumes "wm_basic M"
249
+ assumes "is_STRIPS_fmla \<phi>"
250
+ shows "valuation M \<Turnstile> \<phi> \<longleftrightarrow> M \<TTurnstile> \<phi>"
251
+ proof -
252
+ have aux1: "\<And>\<A>. \<lbrakk>valuation M \<Turnstile> \<phi>; \<forall>G\<in>M. \<A> \<Turnstile> G\<rbrakk> \<Longrightarrow> \<A> \<Turnstile> \<phi>"
253
+ using assms apply(induction \<phi> rule: is_STRIPS_fmla.induct)
254
+ by (auto simp: valuation_def)
255
+ have aux2: "\<lbrakk>\<forall>\<A>. (\<forall>G\<in>M. \<A> \<Turnstile> G) \<longrightarrow> \<A> \<Turnstile> \<phi>\<rbrakk> \<Longrightarrow> valuation M \<Turnstile> \<phi>"
256
+ using assms
257
+ apply(induction \<phi> rule: is_STRIPS_fmla.induct)
258
+ apply simp_all
259
+ apply (metis in_close_world_conv valuation_aux_2)
260
+ using in_close_world_conv valuation_aux_2 apply blast
261
+ using in_close_world_conv valuation_aux_2 by auto
262
+ show ?thesis
263
+ by (auto simp: entailment_def intro: aux1 aux2)
264
+ qed
265
+
266
+ text \<open>Our extension to negation and equality is a proper generalization of the
267
+ standard STRIPS semantics for formula without negation and equality\<close>
268
+ theorem proper_STRIPS_generalization:
269
+ "\<lbrakk>wm_basic M; is_STRIPS_fmla \<phi>\<rbrakk> \<Longrightarrow> M \<^sup>c\<TTurnstile>\<^sub>= \<phi> \<longleftrightarrow> M \<TTurnstile> \<phi>"
270
+ by (simp add: valuation_iff_close_world[symmetric] valuation_iff_STRIPS)
271
+
272
+ subsection \<open>STRIPS Semantics\<close>
273
+
274
+ text \<open>For this section, we fix a domain \<open>D\<close>, using Isabelle's
275
+ locale mechanism.\<close>
276
+ locale ast_domain =
277
+ fixes D :: ast_domain
278
+ begin
279
+ text \<open>It seems to be agreed upon that, in case of a contradictory effect,
280
+ addition overrides deletion. We model this behaviour by first executing
281
+ the deletions, and then the additions.\<close>
282
+ fun apply_effect :: "object ast_effect \<Rightarrow> world_model \<Rightarrow> world_model"
283
+ where
284
+ "apply_effect (Effect a d) s = (s - set d) \<union> (set a)"
285
+
286
+ text \<open>Execute a ground action\<close>
287
+ definition execute_ground_action :: "ground_action \<Rightarrow> world_model \<Rightarrow> world_model"
288
+ where
289
+ "execute_ground_action a M = apply_effect (effect a) M"
290
+
291
+ text \<open>Predicate to model that the given list of action instances is
292
+ executable, and transforms an initial world model \<open>M\<close> into a final
293
+ model \<open>M'\<close>.
294
+
295
+ Note that this definition over the list structure is more convenient in HOL
296
+ than to explicitly define an indexed sequence \<open>M\<^sub>0\<dots>M\<^sub>N\<close> of intermediate world
297
+ models, as done in [Lif87].
298
+ \<close>
299
+ fun ground_action_path
300
+ :: "world_model \<Rightarrow> ground_action list \<Rightarrow> world_model \<Rightarrow> bool"
301
+ where
302
+ "ground_action_path M [] M' \<longleftrightarrow> (M = M')"
303
+ | "ground_action_path M (\<alpha>#\<alpha>s) M' \<longleftrightarrow> M \<^sup>c\<TTurnstile>\<^sub>= precondition \<alpha>
304
+ \<and> ground_action_path (execute_ground_action \<alpha> M) \<alpha>s M'"
305
+
306
+ text \<open>Function equations as presented in paper,
307
+ with inlined @{const execute_ground_action}.\<close>
308
+ lemma ground_action_path_in_paper:
309
+ "ground_action_path M [] M' \<longleftrightarrow> (M = M')"
310
+ "ground_action_path M (\<alpha>#\<alpha>s) M' \<longleftrightarrow> M \<^sup>c\<TTurnstile>\<^sub>= precondition \<alpha>
311
+ \<and> (ground_action_path (apply_effect (effect \<alpha>) M) \<alpha>s M')"
312
+ by (auto simp: execute_ground_action_def)
313
+
314
+ end \<comment> \<open>Context of \<open>ast_domain\<close>\<close>
315
+
316
+
317
+
318
+ subsection \<open>Well-Formedness of PDDL\<close>
319
+
320
+ (* Well-formedness *)
321
+
322
+ (*
323
+ Compute signature: predicate/arity
324
+ Check that all atoms (schemas and facts) satisfy signature
325
+
326
+ for action:
327
+ Check that used parameters \<subseteq> declared parameters
328
+
329
+ for init/goal: Check that facts only use declared objects
330
+ *)
331
+
332
+
333
+ fun ty_term where
334
+ "ty_term varT objT (term.VAR v) = varT v"
335
+ | "ty_term varT objT (term.CONST c) = objT c"
336
+
337
+
338
+ lemma ty_term_mono: "varT \<subseteq>\<^sub>m varT' \<Longrightarrow> objT \<subseteq>\<^sub>m objT' \<Longrightarrow>
339
+ ty_term varT objT \<subseteq>\<^sub>m ty_term varT' objT'"
340
+ apply (rule map_leI)
341
+ subgoal for x v
342
+ apply (cases x)
343
+ apply (auto dest: map_leD)
344
+ done
345
+ done
346
+
347
+
348
+ context ast_domain begin
349
+
350
+ text \<open>The signature is a partial function that maps the predicates
351
+ of the domain to lists of argument types.\<close>
352
+ definition sig :: "predicate \<rightharpoonup> type list" where
353
+ "sig \<equiv> map_of (map (\<lambda>PredDecl p n \<Rightarrow> (p,n)) (predicates D))"
354
+
355
+ text \<open>We use a flat subtype hierarchy, where every type is a subtype
356
+ of object, and there are no other subtype relations.
357
+
358
+ Note that we do not need to restrict this relation to declared types,
359
+ as we will explicitly ensure that all types used in the problem are
360
+ declared.
361
+ \<close>
362
+
363
+ fun subtype_edge where
364
+ "subtype_edge (ty,superty) = (superty,ty)"
365
+
366
+ definition "subtype_rel \<equiv> set (map subtype_edge (types D))"
367
+
368
+ (*
369
+ definition "subtype_rel \<equiv> {''object''}\<times>UNIV"
370
+ *)
371
+
372
+ definition of_type :: "type \<Rightarrow> type \<Rightarrow> bool" where
373
+ "of_type oT T \<equiv> set (primitives oT) \<subseteq> subtype_rel\<^sup>* `` set (primitives T)"
374
+ text \<open>This checks that every primitive on the LHS is contained in or a
375
+ subtype of a primitive on the RHS\<close>
376
+
377
+
378
+ text \<open>For the next few definitions, we fix a partial function that maps
379
+ a polymorphic entity type @{typ "'e"} to types. An entity can be
380
+ instantiated by variables or objects later.\<close>
381
+ context
382
+ fixes ty_ent :: "'ent \<rightharpoonup> type" \<comment> \<open>Entity's type, None if invalid\<close>
383
+ begin
384
+
385
+ text \<open>Checks whether an entity has a given type\<close>
386
+ definition is_of_type :: "'ent \<Rightarrow> type \<Rightarrow> bool" where
387
+ "is_of_type v T \<longleftrightarrow> (
388
+ case ty_ent v of
389
+ Some vT \<Rightarrow> of_type vT T
390
+ | None \<Rightarrow> False)"
391
+
392
+ fun wf_pred_atom :: "predicate \<times> 'ent list \<Rightarrow> bool" where
393
+ "wf_pred_atom (p,vs) \<longleftrightarrow> (
394
+ case sig p of
395
+ None \<Rightarrow> False
396
+ | Some Ts \<Rightarrow> list_all2 is_of_type vs Ts)"
397
+
398
+ text \<open>Predicate-atoms are well-formed if their arguments match the
399
+ signature, equalities are well-formed if the arguments are valid
400
+ objects (have a type).
401
+
402
+ TODO: We could check that types may actually overlap
403
+ \<close>
404
+ fun wf_atom :: "'ent atom \<Rightarrow> bool" where
405
+ "wf_atom (predAtm p vs) \<longleftrightarrow> wf_pred_atom (p,vs)"
406
+ | "wf_atom (Eq a b) \<longleftrightarrow> ty_ent a \<noteq> None \<and> ty_ent b \<noteq> None"
407
+
408
+ text \<open>A formula is well-formed if it consists of valid atoms,
409
+ and does not contain negations, except for the encoding \<open>\<^bold>\<not>\<bottom>\<close> of true.
410
+ \<close>
411
+ fun wf_fmla :: "('ent atom) formula \<Rightarrow> bool" where
412
+ "wf_fmla (Atom a) \<longleftrightarrow> wf_atom a"
413
+ | "wf_fmla (\<bottom>) \<longleftrightarrow> True"
414
+ | "wf_fmla (\<phi>1 \<^bold>\<and> \<phi>2) \<longleftrightarrow> (wf_fmla \<phi>1 \<and> wf_fmla \<phi>2)"
415
+ | "wf_fmla (\<phi>1 \<^bold>\<or> \<phi>2) \<longleftrightarrow> (wf_fmla \<phi>1 \<and> wf_fmla \<phi>2)"
416
+ | "wf_fmla (\<^bold>\<not>\<phi>) \<longleftrightarrow> wf_fmla \<phi>"
417
+ | "wf_fmla (\<phi>1 \<^bold>\<rightarrow> \<phi>2) \<longleftrightarrow> (wf_fmla \<phi>1 \<and> wf_fmla \<phi>2)"
418
+
419
+ lemma "wf_fmla \<phi> = (\<forall>a\<in>atoms \<phi>. wf_atom a)"
420
+ by (induction \<phi>) auto
421
+
422
+ (*lemma wf_fmla_add_simps[simp]: "wf_fmla (\<^bold>\<not>\<phi>) \<longleftrightarrow> \<phi>=\<bottom>"
423
+ by (cases \<phi>) auto*)
424
+
425
+ text \<open>Special case for a well-formed atomic predicate formula\<close>
426
+ fun wf_fmla_atom where
427
+ "wf_fmla_atom (Atom (predAtm a vs)) \<longleftrightarrow> wf_pred_atom (a,vs)"
428
+ | "wf_fmla_atom _ \<longleftrightarrow> False"
429
+
430
+ lemma wf_fmla_atom_alt: "wf_fmla_atom \<phi> \<longleftrightarrow> is_predAtom \<phi> \<and> wf_fmla \<phi>"
431
+ by (cases \<phi> rule: wf_fmla_atom.cases) auto
432
+
433
+ text \<open>An effect is well-formed if the added and removed formulas
434
+ are atomic\<close>
435
+ fun wf_effect where
436
+ "wf_effect (Effect a d) \<longleftrightarrow>
437
+ (\<forall>ae\<in>set a. wf_fmla_atom ae)
438
+ \<and> (\<forall>de\<in>set d. wf_fmla_atom de)"
439
+
440
+ end \<comment> \<open>Context fixing \<open>ty_ent\<close>\<close>
441
+
442
+
443
+ definition constT :: "object \<rightharpoonup> type" where
444
+ "constT \<equiv> map_of (consts D)"
445
+
446
+ text \<open>An action schema is well-formed if the parameter names are distinct,
447
+ and the precondition and effect is well-formed wrt.\ the parameters.
448
+ \<close>
449
+ fun wf_action_schema :: "ast_action_schema \<Rightarrow> bool" where
450
+ "wf_action_schema (Action_Schema n params pre eff) \<longleftrightarrow> (
451
+ let
452
+ tyt = ty_term (map_of params) constT
453
+ in
454
+ distinct (map fst params)
455
+ \<and> wf_fmla tyt pre
456
+ \<and> wf_effect tyt eff)"
457
+
458
+ text \<open>A type is well-formed if it consists only of declared primitive types,
459
+ and the type object.\<close>
460
+ fun wf_type where
461
+ "wf_type (Either Ts) \<longleftrightarrow> set Ts \<subseteq> insert ''object'' (fst`set (types D))"
462
+
463
+ text \<open>A predicate is well-formed if its argument types are well-formed.\<close>
464
+ fun wf_predicate_decl where
465
+ "wf_predicate_decl (PredDecl p Ts) \<longleftrightarrow> (\<forall>T\<in>set Ts. wf_type T)"
466
+
467
+ text \<open>The types declaration is well-formed, if all supertypes are declared types (or object)\<close>
468
+ definition "wf_types \<equiv> snd`set (types D) \<subseteq> insert ''object'' (fst`set (types D))"
469
+
470
+ text \<open>A domain is well-formed if
471
+ \<^item> there are no duplicate declared predicate names,
472
+ \<^item> all declared predicates are well-formed,
473
+ \<^item> there are no duplicate action names,
474
+ \<^item> and all declared actions are well-formed
475
+ \<close>
476
+ definition wf_domain :: "bool" where
477
+ "wf_domain \<equiv>
478
+ wf_types
479
+ \<and> distinct (map (predicate_decl.pred) (predicates D))
480
+ \<and> (\<forall>p\<in>set (predicates D). wf_predicate_decl p)
481
+ \<and> distinct (map fst (consts D))
482
+ \<and> (\<forall>(n,T)\<in>set (consts D). wf_type T)
483
+ \<and> distinct (map ast_action_schema.name (actions D))
484
+ \<and> (\<forall>a\<in>set (actions D). wf_action_schema a)
485
+ "
486
+
487
+ end \<comment> \<open>locale \<open>ast_domain\<close>\<close>
488
+
489
+ text \<open>We fix a problem, and also include the definitions for the domain
490
+ of this problem.\<close>
491
+ locale ast_problem = ast_domain "domain P"
492
+ for P :: ast_problem
493
+ begin
494
+ text \<open>We refer to the problem domain as \<open>D\<close>\<close>
495
+ abbreviation "D \<equiv> ast_problem.domain P"
496
+
497
+ definition objT :: "object \<rightharpoonup> type" where
498
+ "objT \<equiv> map_of (objects P) ++ constT"
499
+
500
+ lemma objT_alt: "objT = map_of (consts D @ objects P)"
501
+ unfolding objT_def constT_def
502
+ apply (clarsimp)
503
+ done
504
+
505
+ definition wf_fact :: "fact \<Rightarrow> bool" where
506
+ "wf_fact = wf_pred_atom objT"
507
+
508
+ text \<open>This definition is needed for well-formedness of the initial model,
509
+ and forward-references to the concept of world model.
510
+ \<close>
511
+ definition wf_world_model where
512
+ "wf_world_model M = (\<forall>f\<in>M. wf_fmla_atom objT f)"
513
+
514
+ (*Note: current semantics assigns each object a unique type *)
515
+ definition wf_problem where
516
+ "wf_problem \<equiv>
517
+ wf_domain
518
+ \<and> distinct (map fst (objects P) @ map fst (consts D))
519
+ \<and> (\<forall>(n,T)\<in>set (objects P). wf_type T)
520
+ \<and> distinct (init P)
521
+ \<and> wf_world_model (set (init P))
522
+ \<and> wf_fmla objT (goal P)
523
+ "
524
+
525
+ fun wf_effect_inst :: "object ast_effect \<Rightarrow> bool" where
526
+ "wf_effect_inst (Effect (a) (d))
527
+ \<longleftrightarrow> (\<forall>a\<in>set a \<union> set d. wf_fmla_atom objT a)"
528
+
529
+ lemma wf_effect_inst_alt: "wf_effect_inst eff = wf_effect objT eff"
530
+ by (cases eff) auto
531
+
532
+ end \<comment> \<open>locale \<open>ast_problem\<close>\<close>
533
+
534
+ text \<open>Locale to express a well-formed domain\<close>
535
+ locale wf_ast_domain = ast_domain +
536
+ assumes wf_domain: wf_domain
537
+
538
+ text \<open>Locale to express a well-formed problem\<close>
539
+ locale wf_ast_problem = ast_problem P for P +
540
+ assumes wf_problem: wf_problem
541
+ begin
542
+ sublocale wf_ast_domain "domain P"
543
+ apply unfold_locales
544
+ using wf_problem
545
+ unfolding wf_problem_def by simp
546
+
547
+ end \<comment> \<open>locale \<open>wf_ast_problem\<close>\<close>
548
+
549
+ subsection \<open>PDDL Semantics\<close>
550
+
551
+ (* Semantics *)
552
+
553
+ (* To apply plan_action:
554
+ find action schema, instantiate, check precond, apply effect
555
+ *)
556
+
557
+
558
+
559
+ context ast_domain begin
560
+
561
+ definition resolve_action_schema :: "name \<rightharpoonup> ast_action_schema" where
562
+ "resolve_action_schema n = index_by ast_action_schema.name (actions D) n"
563
+
564
+ fun subst_term where
565
+ "subst_term psubst (term.VAR x) = psubst x"
566
+ | "subst_term psubst (term.CONST c) = c"
567
+
568
+ text \<open>To instantiate an action schema, we first compute a substitution from
569
+ parameters to objects, and then apply this substitution to the
570
+ precondition and effect. The substitution is applied via the \<open>map_xxx\<close>
571
+ functions generated by the datatype package.
572
+ \<close>
573
+ fun instantiate_action_schema
574
+ :: "ast_action_schema \<Rightarrow> object list \<Rightarrow> ground_action"
575
+ where
576
+ "instantiate_action_schema (Action_Schema n params pre eff) args = (let
577
+ tsubst = subst_term (the o (map_of (zip (map fst params) args)));
578
+ pre_inst = (map_formula o map_atom) tsubst pre;
579
+ eff_inst = (map_ast_effect) tsubst eff
580
+ in
581
+ Ground_Action pre_inst eff_inst
582
+ )"
583
+
584
+ end \<comment> \<open>Context of \<open>ast_domain\<close>\<close>
585
+
586
+
587
+ context ast_problem begin
588
+
589
+ text \<open>Initial model\<close>
590
+ definition I :: "world_model" where
591
+ "I \<equiv> set (init P)"
592
+
593
+
594
+ text \<open>Resolve a plan action and instantiate the referenced action schema.\<close>
595
+ fun resolve_instantiate :: "plan_action \<Rightarrow> ground_action" where
596
+ "resolve_instantiate (PAction n args) =
597
+ instantiate_action_schema
598
+ (the (resolve_action_schema n))
599
+ args"
600
+
601
+ text \<open>Check whether object has specified type\<close>
602
+ definition "is_obj_of_type n T \<equiv> case objT n of
603
+ None \<Rightarrow> False
604
+ | Some oT \<Rightarrow> of_type oT T"
605
+
606
+ text \<open>We can also use the generic \<open>is_of_type\<close> function.\<close>
607
+ lemma is_obj_of_type_alt: "is_obj_of_type = is_of_type objT"
608
+ apply (intro ext)
609
+ unfolding is_obj_of_type_def is_of_type_def by auto
610
+
611
+
612
+ text \<open>HOL encoding of matching an action's formal parameters against an
613
+ argument list.
614
+ The parameters of the action are encoded as a list of \<open>name\<times>type\<close> pairs,
615
+ such that we map it to a list of types first. Then, the list
616
+ relator @{const list_all2} checks that arguments and types have the same
617
+ length, and each matching pair of argument and type
618
+ satisfies the predicate @{const is_obj_of_type}.
619
+ \<close>
620
+ definition "action_params_match a args
621
+ \<equiv> list_all2 is_obj_of_type args (map snd (parameters a))"
622
+
623
+ text \<open>At this point, we can define well-formedness of a plan action:
624
+ The action must refer to a declared action schema, the arguments must
625
+ be compatible with the formal parameters' types.
626
+ \<close>
627
+ (* Objects are valid and match parameter types *)
628
+ fun wf_plan_action :: "plan_action \<Rightarrow> bool" where
629
+ "wf_plan_action (PAction n args) = (
630
+ case resolve_action_schema n of
631
+ None \<Rightarrow> False
632
+ | Some a \<Rightarrow>
633
+ action_params_match a args
634
+ \<and> wf_effect_inst (effect (instantiate_action_schema a args))
635
+ )"
636
+ text \<open>
637
+ TODO: The second conjunct is redundant, as instantiating a well formed
638
+ action with valid objects yield a valid effect.
639
+ \<close>
640
+
641
+
642
+
643
+ text \<open>A sequence of plan actions form a path, if they are well-formed and
644
+ their instantiations form a path.\<close>
645
+ definition plan_action_path
646
+ :: "world_model \<Rightarrow> plan_action list \<Rightarrow> world_model \<Rightarrow> bool"
647
+ where
648
+ "plan_action_path M \<pi>s M' =
649
+ ((\<forall>\<pi> \<in> set \<pi>s. wf_plan_action \<pi>)
650
+ \<and> ground_action_path M (map resolve_instantiate \<pi>s) M')"
651
+
652
+ text \<open>A plan is valid wrt.\ a given initial model, if it forms a path to a
653
+ goal model \<close>
654
+ definition valid_plan_from :: "world_model \<Rightarrow> plan \<Rightarrow> bool" where
655
+ "valid_plan_from M \<pi>s = (\<exists>M'. plan_action_path M \<pi>s M' \<and> M' \<^sup>c\<TTurnstile>\<^sub>= (goal P))"
656
+
657
+ (* Implementation note: resolve and instantiate already done inside
658
+ enabledness check, redundancy! *)
659
+
660
+ text \<open>Finally, a plan is valid if it is valid wrt.\ the initial world
661
+ model @{const I}\<close>
662
+ definition valid_plan :: "plan \<Rightarrow> bool"
663
+ where "valid_plan \<equiv> valid_plan_from I"
664
+
665
+ text \<open>Concise definition used in paper:\<close>
666
+ lemma "valid_plan \<pi>s \<equiv> \<exists>M'. plan_action_path I \<pi>s M' \<and> M' \<^sup>c\<TTurnstile>\<^sub>= (goal P)"
667
+ unfolding valid_plan_def valid_plan_from_def by auto
668
+
669
+
670
+ end \<comment> \<open>Context of \<open>ast_problem\<close>\<close>
671
+
672
+
673
+
674
+ subsection \<open>Preservation of Well-Formedness\<close>
675
+
676
+ subsubsection \<open>Well-Formed Action Instances\<close>
677
+ text \<open>The goal of this section is to establish that well-formedness of
678
+ world models is preserved by execution of well-formed plan actions.
679
+ \<close>
680
+
681
+ context ast_problem begin
682
+
683
+ text \<open>As plan actions are executed by first instantiating them, and then
684
+ executing the action instance, it is natural to define a well-formedness
685
+ concept for action instances.\<close>
686
+
687
+ fun wf_ground_action :: "ground_action \<Rightarrow> bool" where
688
+ "wf_ground_action (Ground_Action pre eff) \<longleftrightarrow> (
689
+ wf_fmla objT pre
690
+ \<and> wf_effect objT eff
691
+ )
692
+ "
693
+
694
+ text \<open>We first prove that instantiating a well-formed action schema will yield
695
+ a well-formed action instance.
696
+
697
+ We begin with some auxiliary lemmas before the actual theorem.
698
+ \<close>
699
+
700
+ lemma (in ast_domain) of_type_refl[simp, intro!]: "of_type T T"
701
+ unfolding of_type_def by auto
702
+
703
+ lemma (in ast_domain) of_type_trans[trans]:
704
+ "of_type T1 T2 \<Longrightarrow> of_type T2 T3 \<Longrightarrow> of_type T1 T3"
705
+ unfolding of_type_def
706
+ by clarsimp (metis (no_types, opaque_lifting)
707
+ Image_mono contra_subsetD order_refl rtrancl_image_idem)
708
+
709
+ lemma is_of_type_map_ofE:
710
+ assumes "is_of_type (map_of params) x T"
711
+ obtains i xT where "i<length params" "params!i = (x,xT)" "of_type xT T"
712
+ using assms
713
+ unfolding is_of_type_def
714
+ by (auto split: option.splits dest!: map_of_SomeD simp: in_set_conv_nth)
715
+
716
+ lemma wf_atom_mono:
717
+ assumes SS: "tys \<subseteq>\<^sub>m tys'"
718
+ assumes WF: "wf_atom tys a"
719
+ shows "wf_atom tys' a"
720
+ proof -
721
+ have "list_all2 (is_of_type tys') xs Ts" if "list_all2 (is_of_type tys) xs Ts" for xs Ts
722
+ using that
723
+ apply induction
724
+ by (auto simp: is_of_type_def split: option.splits dest: map_leD[OF SS])
725
+ with WF show ?thesis
726
+ by (cases a) (auto split: option.splits dest: map_leD[OF SS])
727
+ qed
728
+
729
+ lemma wf_fmla_atom_mono:
730
+ assumes SS: "tys \<subseteq>\<^sub>m tys'"
731
+ assumes WF: "wf_fmla_atom tys a"
732
+ shows "wf_fmla_atom tys' a"
733
+ proof -
734
+ have "list_all2 (is_of_type tys') xs Ts" if "list_all2 (is_of_type tys) xs Ts" for xs Ts
735
+ using that
736
+ apply induction
737
+ by (auto simp: is_of_type_def split: option.splits dest: map_leD[OF SS])
738
+ with WF show ?thesis
739
+ by (cases a rule: wf_fmla_atom.cases) (auto split: option.splits dest: map_leD[OF SS])
740
+ qed
741
+
742
+
743
+ lemma constT_ss_objT: "constT \<subseteq>\<^sub>m objT"
744
+ unfolding constT_def objT_def
745
+ apply rule
746
+ by (auto simp: map_add_def split: option.split)
747
+
748
+ lemma wf_atom_constT_imp_objT: "wf_atom (ty_term Q constT) a \<Longrightarrow> wf_atom (ty_term Q objT) a"
749
+ apply (erule wf_atom_mono[rotated])
750
+ apply (rule ty_term_mono)
751
+ by (simp_all add: constT_ss_objT)
752
+
753
+ lemma wf_fmla_atom_constT_imp_objT: "wf_fmla_atom (ty_term Q constT) a \<Longrightarrow> wf_fmla_atom (ty_term Q objT) a"
754
+ apply (erule wf_fmla_atom_mono[rotated])
755
+ apply (rule ty_term_mono)
756
+ by (simp_all add: constT_ss_objT)
757
+
758
+ context
759
+ fixes Q and f :: "variable \<Rightarrow> object"
760
+ assumes INST: "is_of_type Q x T \<Longrightarrow> is_of_type objT (f x) T"
761
+ begin
762
+
763
+ lemma is_of_type_var_conv: "is_of_type (ty_term Q objT) (term.VAR x) T \<longleftrightarrow> is_of_type Q x T"
764
+ unfolding is_of_type_def by (auto)
765
+
766
+ lemma is_of_type_const_conv: "is_of_type (ty_term Q objT) (term.CONST x) T \<longleftrightarrow> is_of_type objT x T"
767
+ unfolding is_of_type_def
768
+ by (auto split: option.split)
769
+
770
+ lemma INST': "is_of_type (ty_term Q objT) x T \<Longrightarrow> is_of_type objT (subst_term f x) T"
771
+ apply (cases x) using INST apply (auto simp: is_of_type_var_conv is_of_type_const_conv)
772
+ done
773
+
774
+
775
+ lemma wf_inst_eq_aux: "Q x = Some T \<Longrightarrow> objT (f x) \<noteq> None"
776
+ using INST[of x T] unfolding is_of_type_def
777
+ by (auto split: option.splits)
778
+
779
+ lemma wf_inst_eq_aux': "ty_term Q objT x = Some T \<Longrightarrow> objT (subst_term f x) \<noteq> None"
780
+ by (cases x) (auto simp: wf_inst_eq_aux)
781
+
782
+
783
+ lemma wf_inst_atom:
784
+ assumes "wf_atom (ty_term Q constT) a"
785
+ shows "wf_atom objT (map_atom (subst_term f) a)"
786
+ proof -
787
+ have X1: "list_all2 (is_of_type objT) (map (subst_term f) xs) Ts" if
788
+ "list_all2 (is_of_type (ty_term Q objT)) xs Ts" for xs Ts
789
+ using that
790
+ apply induction
791
+ using INST'
792
+ by auto
793
+ then show ?thesis
794
+ using assms[THEN wf_atom_constT_imp_objT] wf_inst_eq_aux'
795
+ by (cases a; auto split: option.splits)
796
+
797
+ qed
798
+
799
+ lemma wf_inst_formula_atom:
800
+ assumes "wf_fmla_atom (ty_term Q constT) a"
801
+ shows "wf_fmla_atom objT ((map_formula o map_atom o subst_term) f a)"
802
+ using assms[THEN wf_fmla_atom_constT_imp_objT] wf_inst_atom
803
+ apply (cases a rule: wf_fmla_atom.cases; auto split: option.splits)
804
+ by (simp add: INST' list.rel_map(1) list_all2_mono)
805
+
806
+ lemma wf_inst_effect:
807
+ assumes "wf_effect (ty_term Q constT) \<phi>"
808
+ shows "wf_effect objT ((map_ast_effect o subst_term) f \<phi>)"
809
+ using assms
810
+ proof (induction \<phi>)
811
+ case (Effect x1a x2a)
812
+ then show ?case using wf_inst_formula_atom by auto
813
+ qed
814
+
815
+ lemma wf_inst_formula:
816
+ assumes "wf_fmla (ty_term Q constT) \<phi>"
817
+ shows "wf_fmla objT ((map_formula o map_atom o subst_term) f \<phi>)"
818
+ using assms
819
+ by (induction \<phi>) (auto simp: wf_inst_atom dest: wf_inst_eq_aux)
820
+
821
+ end
822
+
823
+
824
+
825
+ text \<open>Instantiating a well-formed action schema with compatible arguments
826
+ will yield a well-formed action instance.
827
+ \<close>
828
+ theorem wf_instantiate_action_schema:
829
+ assumes "action_params_match a args"
830
+ assumes "wf_action_schema a"
831
+ shows "wf_ground_action (instantiate_action_schema a args)"
832
+ proof (cases a)
833
+ case [simp]: (Action_Schema name params pre eff)
834
+ have INST:
835
+ "is_of_type objT ((the \<circ> map_of (zip (map fst params) args)) x) T"
836
+ if "is_of_type (map_of params) x T" for x T
837
+ using that
838
+ apply (rule is_of_type_map_ofE)
839
+ using assms
840
+ apply (clarsimp simp: Let_def)
841
+ subgoal for i xT
842
+ unfolding action_params_match_def
843
+ apply (subst lookup_zip_idx_eq[where i=i];
844
+ (clarsimp simp: list_all2_lengthD)?)
845
+ apply (frule list_all2_nthD2[where p=i]; simp?)
846
+ apply (auto
847
+ simp: is_obj_of_type_alt is_of_type_def
848
+ intro: of_type_trans
849
+ split: option.splits)
850
+ done
851
+ done
852
+ then show ?thesis
853
+ using assms(2) wf_inst_formula wf_inst_effect
854
+ by (fastforce split: term.splits simp: Let_def comp_apply[abs_def])
855
+ qed
856
+ end \<comment> \<open>Context of \<open>ast_problem\<close>\<close>
857
+
858
+
859
+
860
+ subsubsection \<open>Preservation\<close>
861
+
862
+ context ast_problem begin
863
+
864
+ text \<open>We start by defining two shorthands for enabledness and execution of
865
+ a plan action.\<close>
866
+
867
+ text \<open>Shorthand for enabled plan action: It is well-formed, and the
868
+ precondition holds for its instance.\<close>
869
+ definition plan_action_enabled :: "plan_action \<Rightarrow> world_model \<Rightarrow> bool" where
870
+ "plan_action_enabled \<pi> M
871
+ \<longleftrightarrow> wf_plan_action \<pi> \<and> M \<^sup>c\<TTurnstile>\<^sub>= precondition (resolve_instantiate \<pi>)"
872
+
873
+ text \<open>Shorthand for executing a plan action: Resolve, instantiate, and
874
+ apply effect\<close>
875
+ definition execute_plan_action :: "plan_action \<Rightarrow> world_model \<Rightarrow> world_model"
876
+ where "execute_plan_action \<pi> M
877
+ = (apply_effect (effect (resolve_instantiate \<pi>)) M)"
878
+
879
+ text \<open>The @{const plan_action_path} predicate can be decomposed naturally
880
+ using these shorthands: \<close>
881
+ lemma plan_action_path_Nil[simp]: "plan_action_path M [] M' \<longleftrightarrow> M'=M"
882
+ by (auto simp: plan_action_path_def)
883
+
884
+ lemma plan_action_path_Cons[simp]:
885
+ "plan_action_path M (\<pi>#\<pi>s) M' \<longleftrightarrow>
886
+ plan_action_enabled \<pi> M
887
+ \<and> plan_action_path (execute_plan_action \<pi> M) \<pi>s M'"
888
+ by (auto
889
+ simp: plan_action_path_def execute_plan_action_def
890
+ execute_ground_action_def plan_action_enabled_def)
891
+
892
+
893
+
894
+ end \<comment> \<open>Context of \<open>ast_problem\<close>\<close>
895
+
896
+ context wf_ast_problem begin
897
+ text \<open>The initial world model is well-formed\<close>
898
+ lemma wf_I: "wf_world_model I"
899
+ using wf_problem
900
+ unfolding I_def wf_world_model_def wf_problem_def
901
+ apply(safe) subgoal for f by (induction f) auto
902
+ done
903
+
904
+ text \<open>Application of a well-formed effect preserves well-formedness
905
+ of the model\<close>
906
+ lemma wf_apply_effect:
907
+ assumes "wf_effect objT e"
908
+ assumes "wf_world_model s"
909
+ shows "wf_world_model (apply_effect e s)"
910
+ using assms wf_problem
911
+ unfolding wf_world_model_def wf_problem_def wf_domain_def
912
+ by (cases e) (auto split: formula.splits prod.splits)
913
+
914
+ text \<open>Execution of plan actions preserves well-formedness\<close>
915
+ theorem wf_execute:
916
+ assumes "plan_action_enabled \<pi> s"
917
+ assumes "wf_world_model s"
918
+ shows "wf_world_model (execute_plan_action \<pi> s)"
919
+ using assms
920
+ proof (cases \<pi>)
921
+ case [simp]: (PAction name args)
922
+
923
+ from \<open>plan_action_enabled \<pi> s\<close> have "wf_plan_action \<pi>"
924
+ unfolding plan_action_enabled_def by auto
925
+ then obtain a where
926
+ "resolve_action_schema name = Some a" and
927
+ T: "action_params_match a args"
928
+ by (auto split: option.splits)
929
+
930
+ from wf_domain have
931
+ [simp]: "distinct (map ast_action_schema.name (actions D))"
932
+ unfolding wf_domain_def by auto
933
+
934
+ from \<open>resolve_action_schema name = Some a\<close> have
935
+ "a \<in> set (actions D)"
936
+ unfolding resolve_action_schema_def by auto
937
+ with wf_domain have "wf_action_schema a"
938
+ unfolding wf_domain_def by auto
939
+ hence "wf_ground_action (resolve_instantiate \<pi>)"
940
+ using \<open>resolve_action_schema name = Some a\<close> T
941
+ wf_instantiate_action_schema
942
+ by auto
943
+ thus ?thesis
944
+ apply (simp add: execute_plan_action_def execute_ground_action_def)
945
+ apply (rule wf_apply_effect)
946
+ apply (cases "resolve_instantiate \<pi>"; simp)
947
+ by (rule \<open>wf_world_model s\<close>)
948
+ qed
949
+
950
+ theorem wf_execute_compact_notation:
951
+ "plan_action_enabled \<pi> s \<Longrightarrow> wf_world_model s
952
+ \<Longrightarrow> wf_world_model (execute_plan_action \<pi> s)"
953
+ by (rule wf_execute)
954
+
955
+
956
+ text \<open>Execution of a plan preserves well-formedness\<close>
957
+ corollary wf_plan_action_path:
958
+ assumes "wf_world_model M" and " plan_action_path M \<pi>s M'"
959
+ shows "wf_world_model M'"
960
+ using assms
961
+ by (induction \<pi>s arbitrary: M) (auto intro: wf_execute)
962
+
963
+
964
+ end \<comment> \<open>Context of \<open>wf_ast_problem\<close>\<close>
965
+
966
+
967
+
968
+
969
+ end \<comment> \<open>Theory\<close>
formal/afp/AI_Planning_Languages_Semantics/SASP_Checker.thy ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ theory SASP_Checker
2
+ imports SASP_Semantics
3
+ "HOL-Library.Code_Target_Nat"
4
+ begin
5
+
6
+ section \<open>An Executable Checker for Multi-Valued Planning Problem Solutions\<close>
7
+
8
+
9
+ subsection \<open>Auxiliary Lemmas\<close>
10
+ lemma map_of_leI:
11
+ assumes "distinct (map fst l)"
12
+ assumes "\<And>k v. (k,v)\<in>set l \<Longrightarrow> m k = Some v"
13
+ shows "map_of l \<subseteq>\<^sub>m m"
14
+ using assms
15
+ by (metis (no_types, opaque_lifting) domIff map_le_def map_of_SomeD not_Some_eq)
16
+
17
+ lemma [simp]: "fst \<circ> (\<lambda>(a, b, c, d). (f a b c d, g a b c d)) = (\<lambda>(a,b,c,d). f a b c d)"
18
+ by auto
19
+
20
+ lemma map_mp: "m\<subseteq>\<^sub>mm' \<Longrightarrow> m k = Some v \<Longrightarrow> m' k = Some v"
21
+ by (auto simp: map_le_def dom_def)
22
+
23
+
24
+ lemma map_add_map_of_fold:
25
+ fixes ps and m :: "'a \<rightharpoonup> 'b"
26
+ assumes "distinct (map fst ps)"
27
+ shows "m ++ map_of ps = fold (\<lambda>(k, v) m. m(k \<mapsto> v)) ps m"
28
+ proof -
29
+ have X1: "fold (\<lambda>(k, v) m. m(k \<mapsto> v)) ps m(a \<mapsto> b)
30
+ = fold (\<lambda>(k, v) m. m(k \<mapsto> v)) ps (m(a \<mapsto> b))"
31
+ if "a \<notin> fst ` set ps"
32
+ for a b ps and m :: "'a \<rightharpoonup> 'b"
33
+ using that
34
+ by (induction ps arbitrary: m) (auto simp: fun_upd_twist)
35
+
36
+ show ?thesis
37
+ using assms
38
+ by (induction ps arbitrary: m) (auto simp: X1)
39
+ qed
40
+
41
+
42
+
43
+ subsection \<open>Well-formedness Check\<close>
44
+ lemmas wf_code_thms =
45
+ ast_problem.astDom_def ast_problem.astI_def ast_problem.astG_def ast_problem.ast\<delta>_def
46
+ ast_problem.numVars_def ast_problem.numVals_def
47
+ ast_problem.wf_partial_state_def ast_problem.wf_operator_def ast_problem.well_formed_def
48
+
49
+
50
+ declare wf_code_thms[code]
51
+
52
+ export_code ast_problem.well_formed in SML
53
+
54
+
55
+ subsection \<open>Execution\<close>
56
+
57
+ definition match_pre :: "ast_precond \<Rightarrow> state \<Rightarrow> bool" where
58
+ "match_pre \<equiv> \<lambda>(x,v) s. s x = Some v"
59
+
60
+ definition match_pres :: "ast_precond list \<Rightarrow> state \<Rightarrow> bool" where
61
+ "match_pres pres s \<equiv> \<forall>pre\<in>set pres. match_pre pre s"
62
+
63
+ definition match_implicit_pres :: "ast_effect list \<Rightarrow> state \<Rightarrow> bool" where
64
+ "match_implicit_pres effs s \<equiv> \<forall>(_,x,vp,_)\<in>set effs.
65
+ (case vp of None \<Rightarrow> True | Some v \<Rightarrow> s x = Some v)"
66
+
67
+ definition enabled_opr' :: "ast_operator \<Rightarrow> state \<Rightarrow> bool" where
68
+ "enabled_opr' \<equiv> \<lambda>(name,pres,effs,cost) s. match_pres pres s \<and> match_implicit_pres effs s"
69
+
70
+ definition eff_enabled' :: "state \<Rightarrow> ast_effect \<Rightarrow> bool" where
71
+ "eff_enabled' s \<equiv> \<lambda>(pres,_,_,_). match_pres pres s"
72
+
73
+ definition "execute_opr' \<equiv> \<lambda>(name,_,effs,_) s.
74
+ let effs = filter (eff_enabled' s) effs
75
+ in fold (\<lambda>(_,x,_,v) s. s(x\<mapsto>v)) effs s
76
+ "
77
+
78
+ definition lookup_operator' :: "ast_problem \<Rightarrow> name \<rightharpoonup> ast_operator"
79
+ where "lookup_operator' \<equiv> \<lambda>(D,I,G,\<delta>) name. find (\<lambda>(n,_,_,_). n=name) \<delta>"
80
+
81
+ definition enabled' :: "ast_problem \<Rightarrow> name \<Rightarrow> state \<Rightarrow> bool" where
82
+ "enabled' problem name s \<equiv>
83
+ case lookup_operator' problem name of
84
+ Some \<pi> \<Rightarrow> enabled_opr' \<pi> s
85
+ | None \<Rightarrow> False"
86
+
87
+ definition execute' :: "ast_problem \<Rightarrow> name \<Rightarrow> state \<Rightarrow> state" where
88
+ "execute' problem name s \<equiv>
89
+ case lookup_operator' problem name of
90
+ Some \<pi> \<Rightarrow> execute_opr' \<pi> s
91
+ | None \<Rightarrow> undefined"
92
+
93
+
94
+ context wf_ast_problem begin
95
+
96
+ lemma match_pres_correct:
97
+ assumes D: "distinct (map fst pres)"
98
+ assumes "s\<in>valid_states"
99
+ shows "match_pres pres s \<longleftrightarrow> s\<in>subsuming_states (map_of pres)"
100
+ proof -
101
+ have "match_pres pres s \<longleftrightarrow> map_of pres \<subseteq>\<^sub>m s"
102
+ unfolding match_pres_def match_pre_def
103
+ apply (auto split: prod.splits)
104
+ using map_le_def map_of_SomeD apply fastforce
105
+ by (metis (full_types) D domIff map_le_def map_of_eq_Some_iff option.simps(3))
106
+
107
+ with assms show ?thesis
108
+ unfolding subsuming_states_def
109
+ by auto
110
+ qed
111
+
112
+ lemma match_implicit_pres_correct:
113
+ assumes D: "distinct (map (\<lambda>(_, v, _, _). v) effs)"
114
+ assumes "s\<in>valid_states"
115
+ shows "match_implicit_pres effs s \<longleftrightarrow> s\<in>subsuming_states (map_of (implicit_pres effs))"
116
+ proof -
117
+ from assms show ?thesis
118
+ unfolding subsuming_states_def
119
+ unfolding match_implicit_pres_def implicit_pres_def
120
+ apply (auto
121
+ split: prod.splits option.splits
122
+ simp: distinct_map_filter
123
+ intro!: map_of_leI)
124
+ apply (force simp: distinct_map_filter split: prod.split elim: map_mp)
125
+ done
126
+ qed
127
+
128
+ lemma enabled_opr'_correct:
129
+ assumes V: "s\<in>valid_states"
130
+ assumes "lookup_operator name = Some \<pi>"
131
+ shows "enabled_opr' \<pi> s \<longleftrightarrow> enabled name s"
132
+ using lookup_operator_wf[OF assms(2)] assms
133
+ unfolding enabled_opr'_def enabled_def wf_operator_def
134
+ by (auto
135
+ simp: match_pres_correct[OF _ V] match_implicit_pres_correct[OF _ V]
136
+ simp: wf_partial_state_def
137
+ split: option.split
138
+ )
139
+
140
+ lemma eff_enabled'_correct:
141
+ assumes V: "s\<in>valid_states"
142
+ assumes "case eff of (pres,_,_,_) \<Rightarrow> wf_partial_state pres"
143
+ shows "eff_enabled' s eff \<longleftrightarrow> eff_enabled s eff"
144
+ using assms
145
+ unfolding eff_enabled'_def eff_enabled_def wf_partial_state_def
146
+ by (auto simp: match_pres_correct)
147
+
148
+
149
+ lemma execute_opr'_correct:
150
+ assumes V: "s\<in>valid_states"
151
+ assumes LO: "lookup_operator name = Some \<pi>"
152
+ shows "execute_opr' \<pi> s = execute name s"
153
+ proof (cases \<pi>)
154
+ case [simp]: (fields name pres effs)
155
+
156
+ have [simp]: "filter (eff_enabled' s) effs = filter (eff_enabled s) effs"
157
+ apply (rule filter_cong[OF refl])
158
+ apply (rule eff_enabled'_correct[OF V])
159
+ using lookup_operator_wf[OF LO]
160
+ unfolding wf_operator_def by auto
161
+
162
+ have X1: "distinct (map fst (map (\<lambda>(_, x, _, y). (x, y)) (filter (eff_enabled s) effs)))"
163
+ using lookup_operator_wf[OF LO]
164
+ unfolding wf_operator_def
165
+ by (auto simp: distinct_map_filter)
166
+
167
+ term "filter (eff_enabled s) effs"
168
+
169
+ have [simp]:
170
+ "fold (\<lambda>(_, x, _, v) s. s(x \<mapsto> v)) l s =
171
+ fold (\<lambda>(k, v) m. m(k \<mapsto> v)) (map (\<lambda>(_, x, _, y). (x, y)) l) s"
172
+ for l :: "ast_effect list"
173
+ by (induction l arbitrary: s) auto
174
+
175
+ show ?thesis
176
+ unfolding execute_opr'_def execute_def using LO
177
+ by (auto simp: map_add_map_of_fold[OF X1])
178
+ qed
179
+
180
+
181
+ lemma lookup_operator'_correct:
182
+ "lookup_operator' problem name = lookup_operator name"
183
+ unfolding lookup_operator'_def lookup_operator_def
184
+ unfolding ast\<delta>_def
185
+ by (auto split: prod.split)
186
+
187
+ lemma enabled'_correct:
188
+ assumes V: "s\<in>valid_states"
189
+ shows "enabled' problem name s = enabled name s"
190
+ unfolding enabled'_def
191
+ using enabled_opr'_correct[OF V]
192
+ by (auto split: option.splits simp: enabled_def lookup_operator'_correct)
193
+
194
+ lemma execute'_correct:
195
+ assumes V: "s\<in>valid_states"
196
+ assumes "enabled name s" (* Intentionally put this here, also we could resolve non-enabled case by reflexivity (undefined=undefined) *)
197
+ shows "execute' problem name s = execute name s"
198
+ unfolding execute'_def
199
+ using execute_opr'_correct[OF V] \<open>enabled name s\<close>
200
+ by (auto split: option.splits simp: enabled_def lookup_operator'_correct)
201
+
202
+
203
+
204
+ end
205
+
206
+ context ast_problem
207
+ begin
208
+
209
+ fun simulate_plan :: "plan \<Rightarrow> state \<rightharpoonup> state" where
210
+ "simulate_plan [] s = Some s"
211
+ | "simulate_plan (\<pi>#\<pi>s) s = (
212
+ if enabled \<pi> s then
213
+ let s' = execute \<pi> s in
214
+ simulate_plan \<pi>s s'
215
+ else
216
+ None
217
+ )"
218
+
219
+ lemma simulate_plan_correct: "simulate_plan \<pi>s s = Some s' \<longleftrightarrow> path_to s \<pi>s s'"
220
+ by (induction s \<pi>s s' rule: path_to.induct) auto
221
+
222
+ definition check_plan :: "plan \<Rightarrow> bool" where
223
+ "check_plan \<pi>s = (
224
+ case simulate_plan \<pi>s I of
225
+ None \<Rightarrow> False
226
+ | Some s' \<Rightarrow> s' \<in> G)"
227
+
228
+ lemma check_plan_correct: "check_plan \<pi>s \<longleftrightarrow> valid_plan \<pi>s"
229
+ unfolding check_plan_def valid_plan_def
230
+ by (auto split: option.split simp: simulate_plan_correct[symmetric])
231
+
232
+ end
233
+
234
+ fun simulate_plan' :: "ast_problem \<Rightarrow> plan \<Rightarrow> state \<rightharpoonup> state" where
235
+ "simulate_plan' problem [] s = Some s"
236
+ | "simulate_plan' problem (\<pi>#\<pi>s) s = (
237
+ if enabled' problem \<pi> s then
238
+ let s = execute' problem \<pi> s in
239
+ simulate_plan' problem \<pi>s s
240
+ else
241
+ None
242
+ )"
243
+
244
+ text \<open>Avoiding duplicate lookup.\<close>
245
+ (*[code] *)
246
+ lemma simulate_plan'_code[code]:
247
+ "simulate_plan' problem [] s = Some s"
248
+ "simulate_plan' problem (\<pi>#\<pi>s) s = (
249
+ case lookup_operator' problem \<pi> of
250
+ None \<Rightarrow> None
251
+ | Some \<pi> \<Rightarrow>
252
+ if enabled_opr' \<pi> s then
253
+ simulate_plan' problem \<pi>s (execute_opr' \<pi> s)
254
+ else None
255
+ )"
256
+ by (auto simp: enabled'_def execute'_def split: option.split)
257
+
258
+
259
+ definition initial_state' :: "ast_problem \<Rightarrow> state" where
260
+ "initial_state' problem \<equiv> let astI = ast_problem.astI problem in (
261
+ \<lambda>v. if v<length astI then Some (astI!v) else None
262
+ )"
263
+
264
+ definition check_plan' :: "ast_problem \<Rightarrow> plan \<Rightarrow> bool" where
265
+ "check_plan' problem \<pi>s = (
266
+ case simulate_plan' problem \<pi>s (initial_state' problem) of
267
+ None \<Rightarrow> False
268
+ | Some s' \<Rightarrow> match_pres (ast_problem.astG problem) s')"
269
+
270
+
271
+ context wf_ast_problem
272
+ begin
273
+
274
+ lemma simulate_plan'_correct:
275
+ assumes "s\<in>valid_states"
276
+ shows "simulate_plan' problem \<pi>s s = simulate_plan \<pi>s s"
277
+ using assms
278
+ apply (induction \<pi>s s rule: simulate_plan.induct)
279
+ apply (auto simp: enabled'_correct execute'_correct execute_preserves_valid)
280
+ done
281
+
282
+ lemma simulate_plan'_correct_paper: (* For presentation in paper.
283
+ Summarizing intermediate refinement step. *)
284
+ assumes "s\<in>valid_states"
285
+ shows "simulate_plan' problem \<pi>s s = Some s'
286
+ \<longleftrightarrow> path_to s \<pi>s s'"
287
+ using simulate_plan'_correct[OF assms] simulate_plan_correct by simp
288
+
289
+
290
+ lemma initial_state'_correct:
291
+ "initial_state' problem = I"
292
+ unfolding initial_state'_def I_def by (auto simp: Let_def)
293
+
294
+ lemma check_plan'_correct:
295
+ "check_plan' problem \<pi>s = check_plan \<pi>s"
296
+ proof -
297
+ have D: "distinct (map fst astG)" using wf_goal unfolding wf_partial_state_def by auto
298
+
299
+ have S'V: "s'\<in>valid_states" if "simulate_plan \<pi>s I = Some s'" for s'
300
+ using that by (auto simp: simulate_plan_correct path_to_pres_valid[OF I_valid])
301
+
302
+ show ?thesis
303
+ unfolding check_plan'_def check_plan_def
304
+ by (auto
305
+ split: option.splits
306
+ simp: initial_state'_correct simulate_plan'_correct[OF I_valid]
307
+ simp: match_pres_correct[OF D S'V] G_def
308
+ )
309
+ qed
310
+
311
+ end
312
+
313
+
314
+ (* Overall checker *)
315
+
316
+ definition verify_plan :: "ast_problem \<Rightarrow> plan \<Rightarrow> String.literal + unit" where
317
+ "verify_plan problem \<pi>s = (
318
+ if ast_problem.well_formed problem then
319
+ if check_plan' problem \<pi>s then Inr () else Inl (STR ''Invalid plan'')
320
+ else Inl (STR ''Problem not well formed'')
321
+ )"
322
+
323
+ lemma verify_plan_correct:
324
+ "verify_plan problem \<pi>s = Inr ()
325
+ \<longleftrightarrow> ast_problem.well_formed problem \<and> ast_problem.valid_plan problem \<pi>s"
326
+ proof -
327
+ {
328
+ assume "ast_problem.well_formed problem"
329
+ then interpret wf_ast_problem by unfold_locales
330
+
331
+ from check_plan'_correct check_plan_correct
332
+ have "check_plan' problem \<pi>s = valid_plan \<pi>s" by simp
333
+ }
334
+ then show ?thesis
335
+ unfolding verify_plan_def
336
+ by auto
337
+ qed
338
+
339
+ definition nat_opt_of_integer :: "integer \<Rightarrow> nat option" where
340
+ "nat_opt_of_integer i = (if (i \<ge> 0) then Some (nat_of_integer i) else None)"
341
+
342
+ (*Export functions, which includes constructors*)
343
+ export_code verify_plan nat_of_integer integer_of_nat nat_opt_of_integer Inl Inr String.explode String.implode
344
+ in SML
345
+ module_name SASP_Checker_Exported
346
+ file "code/SASP_Checker_Exported.sml"
347
+
348
+ end
formal/afp/AI_Planning_Languages_Semantics/SASP_Semantics.thy ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ theory SASP_Semantics
2
+ imports Main
3
+ begin
4
+
5
+ section \<open>Semantics of Fast-Downward's Multi-Valued Planning Tasks Language\<close>
6
+
7
+ subsection \<open>Syntax\<close>
8
+ type_synonym name = string
9
+ type_synonym ast_variable = "name \<times> nat option \<times> name list" (* var name, axiom layer, atom names *)
10
+ type_synonym ast_variable_section = "ast_variable list"
11
+ type_synonym ast_initial_state = "nat list"
12
+ type_synonym ast_goal = "(nat \<times> nat) list"
13
+ type_synonym ast_precond = "(nat \<times> nat)"
14
+ type_synonym ast_effect = "ast_precond list \<times> nat \<times> nat option \<times> nat"
15
+ type_synonym ast_operator = "name \<times> ast_precond list \<times> ast_effect list \<times> nat"
16
+ type_synonym ast_operator_section = "ast_operator list"
17
+
18
+ type_synonym ast_problem =
19
+ "ast_variable_section \<times> ast_initial_state \<times> ast_goal \<times> ast_operator_section"
20
+
21
+ type_synonym plan = "name list"
22
+
23
+ subsubsection \<open>Well-Formedness\<close>
24
+
25
+ locale ast_problem =
26
+ fixes problem :: ast_problem
27
+ begin
28
+ definition astDom :: ast_variable_section (* TODO: Dom \<rightarrow> Vars, D \<rightarrow> X*)
29
+ where "astDom \<equiv> case problem of (D,I,G,\<delta>) \<Rightarrow> D"
30
+ definition astI :: ast_initial_state
31
+ where "astI \<equiv> case problem of (D,I,G,\<delta>) \<Rightarrow> I"
32
+ definition astG :: ast_goal
33
+ where "astG \<equiv> case problem of (D,I,G,\<delta>) \<Rightarrow> G"
34
+ definition ast\<delta> :: ast_operator_section
35
+ where "ast\<delta> \<equiv> case problem of (D,I,G,\<delta>) \<Rightarrow> \<delta>"
36
+
37
+ definition "numVars \<equiv> length astDom"
38
+ definition "numVals x \<equiv> length (snd (snd (astDom!x)))"
39
+
40
+ definition "wf_partial_state ps \<equiv>
41
+ distinct (map fst ps)
42
+ \<and> (\<forall>(x,v) \<in> set ps. x < numVars \<and> v < numVals x)"
43
+
44
+ definition wf_operator :: "ast_operator \<Rightarrow> bool"
45
+ where "wf_operator \<equiv> \<lambda>(name, pres, effs, cost).
46
+ wf_partial_state pres
47
+ \<and> distinct (map (\<lambda>(_, v, _, _). v) effs) \<comment> \<open>This may be too restrictive\<close>
48
+ \<and> (\<forall>(epres,x,vp,v)\<in>set effs.
49
+ wf_partial_state epres
50
+ \<and> x < numVars \<and> v < numVals x
51
+ \<and> (case vp of None \<Rightarrow> True | Some v \<Rightarrow> v<numVals x)
52
+ )
53
+ "
54
+
55
+ definition "well_formed \<equiv>
56
+ \<comment> \<open>Initial state\<close>
57
+ length astI = numVars
58
+ \<and> (\<forall>x<numVars. astI!x < numVals x)
59
+
60
+ \<comment> \<open>Goal\<close>
61
+ \<and> wf_partial_state astG
62
+
63
+ \<comment> \<open>Operators\<close>
64
+ \<and> (distinct (map fst ast\<delta>))
65
+ \<and> (\<forall>\<pi>\<in>set ast\<delta>. wf_operator \<pi>)
66
+ "
67
+
68
+ end
69
+
70
+ locale wf_ast_problem = ast_problem +
71
+ assumes wf: well_formed
72
+ begin
73
+ lemma wf_initial:
74
+ "length astI = numVars"
75
+ "\<forall>x<numVars. astI!x < numVals x"
76
+ using wf unfolding well_formed_def by auto
77
+
78
+ lemma wf_goal: "wf_partial_state astG"
79
+ using wf unfolding well_formed_def by auto
80
+
81
+ lemma wf_operators:
82
+ "distinct (map fst ast\<delta>)"
83
+ "\<forall>\<pi>\<in>set ast\<delta>. wf_operator \<pi>"
84
+ using wf unfolding well_formed_def by auto
85
+ end
86
+
87
+
88
+ subsection \<open>Semantics as Transition System\<close>
89
+
90
+ type_synonym state = "nat \<rightharpoonup> nat"
91
+ type_synonym pstate = "nat \<rightharpoonup> nat"
92
+
93
+
94
+ context ast_problem
95
+ begin
96
+
97
+ definition Dom :: "nat set" where "Dom = {0..<numVars}"
98
+
99
+ definition range_of_var where "range_of_var x \<equiv> {0..<numVals x}"
100
+
101
+ definition valid_states :: "state set" where "valid_states \<equiv> {
102
+ s. dom s = Dom \<and> (\<forall>x\<in>Dom. the (s x) \<in> range_of_var x)
103
+ }"
104
+
105
+ definition I :: state
106
+ where "I v \<equiv> if v<length astI then Some (astI!v) else None"
107
+
108
+ definition subsuming_states :: "pstate \<Rightarrow> state set"
109
+ where "subsuming_states partial \<equiv> { s\<in>valid_states. partial \<subseteq>\<^sub>m s }"
110
+
111
+ definition G :: "state set"
112
+ where "G \<equiv> subsuming_states (map_of astG)"
113
+ end
114
+
115
+ definition implicit_pres :: "ast_effect list \<Rightarrow> ast_precond list" where
116
+ "implicit_pres effs \<equiv>
117
+ map (\<lambda>(_,v,vpre,_). (v,the vpre))
118
+ (filter (\<lambda>(_,_,vpre,_). vpre\<noteq>None) effs)"
119
+
120
+ context ast_problem
121
+ begin
122
+
123
+ definition lookup_operator :: "name \<Rightarrow> ast_operator option" where
124
+ "lookup_operator name \<equiv> find (\<lambda>(n,_,_,_). n=name) ast\<delta>"
125
+
126
+ definition enabled :: "name \<Rightarrow> state \<Rightarrow> bool"
127
+ where "enabled name s \<equiv>
128
+ case lookup_operator name of
129
+ Some (_,pres,effs,_) \<Rightarrow>
130
+ s\<in>subsuming_states (map_of pres)
131
+ \<and> s\<in>subsuming_states (map_of (implicit_pres effs))
132
+ | None \<Rightarrow> False"
133
+
134
+ definition eff_enabled :: "state \<Rightarrow> ast_effect \<Rightarrow> bool" where
135
+ "eff_enabled s \<equiv> \<lambda>(pres,_,_,_). s\<in>subsuming_states (map_of pres)"
136
+
137
+ definition execute :: "name \<Rightarrow> state \<Rightarrow> state" where
138
+ "execute name s \<equiv>
139
+ case lookup_operator name of
140
+ Some (_,_,effs,_) \<Rightarrow>
141
+ s ++ map_of (map (\<lambda>(_,x,_,v). (x,v)) (filter (eff_enabled s) effs))
142
+ | None \<Rightarrow> undefined
143
+ "
144
+
145
+ fun path_to where
146
+ "path_to s [] s' \<longleftrightarrow> s'=s"
147
+ | "path_to s (\<pi>#\<pi>s) s' \<longleftrightarrow> enabled \<pi> s \<and> path_to (execute \<pi> s) \<pi>s s'"
148
+
149
+ definition valid_plan :: "plan \<Rightarrow> bool"
150
+ where "valid_plan \<pi>s \<equiv> \<exists>s'\<in>G. path_to I \<pi>s s'"
151
+
152
+
153
+ end
154
+
155
+ (*
156
+ Next steps:
157
+ * well-formed stuff
158
+ * Executable SAS+ validator (well_formed and execute function)
159
+
160
+ *)
161
+
162
+ subsubsection \<open>Preservation of well-formedness\<close>
163
+ context wf_ast_problem
164
+ begin
165
+ lemma I_valid: "I \<in> valid_states"
166
+ using wf_initial
167
+ unfolding valid_states_def Dom_def I_def range_of_var_def
168
+ by (auto split:if_splits)
169
+
170
+ lemma lookup_operator_wf:
171
+ assumes "lookup_operator name = Some \<pi>"
172
+ shows "wf_operator \<pi>" "fst \<pi> = name"
173
+ proof -
174
+ obtain name' pres effs cost where [simp]: "\<pi>=(name',pres,effs,cost)" by (cases \<pi>)
175
+ hence [simp]: "name'=name" and IN_AST: "(name,pres,effs,cost) \<in> set ast\<delta>"
176
+ using assms
177
+ unfolding lookup_operator_def
178
+ apply -
179
+ apply (metis (mono_tags, lifting) case_prodD find_Some_iff)
180
+ by (metis (mono_tags, lifting) case_prodD find_Some_iff nth_mem)
181
+
182
+ from IN_AST show WF: "wf_operator \<pi>" "fst \<pi> = name"
183
+ unfolding enabled_def using wf_operators by auto
184
+ qed
185
+
186
+
187
+ lemma execute_preserves_valid:
188
+ assumes "s\<in>valid_states"
189
+ assumes "enabled name s"
190
+ shows "execute name s \<in> valid_states"
191
+ proof -
192
+ from \<open>enabled name s\<close> obtain name' pres effs cost where
193
+ [simp]: "lookup_operator name = Some (name',pres,effs,cost)"
194
+ unfolding enabled_def by (auto split: option.splits)
195
+ from lookup_operator_wf[OF this] have WF: "wf_operator (name,pres,effs,cost)" by simp
196
+
197
+ have X1: "s ++ m \<in> valid_states" if "\<forall>x v. m x = Some v \<longrightarrow> x<numVars \<and> v<numVals x" for m
198
+ using that \<open>s\<in>valid_states\<close>
199
+ by (auto
200
+ simp: valid_states_def Dom_def range_of_var_def map_add_def dom_def
201
+ split: option.splits)
202
+
203
+ have X2: "x<numVars" "v<numVals x"
204
+ if "map_of (map (\<lambda>(_, x, _, y). (x, y)) (filter (eff_enabled s) effs)) x = Some v"
205
+ for x v
206
+ proof -
207
+ from that obtain epres vp where "(epres,x,vp,v) \<in> set effs"
208
+ by (auto dest!: map_of_SomeD)
209
+ with WF show "x<numVars" "v<numVals x"
210
+ unfolding wf_operator_def by auto
211
+ qed
212
+
213
+ show ?thesis
214
+ using assms
215
+ unfolding enabled_def execute_def
216
+ by (auto intro!: X1 X2)
217
+ qed
218
+
219
+ lemma path_to_pres_valid:
220
+ assumes "s\<in>valid_states"
221
+ assumes "path_to s \<pi>s s'"
222
+ shows "s'\<in>valid_states"
223
+ using assms
224
+ by (induction s \<pi>s s' rule: path_to.induct) (auto simp: execute_preserves_valid)
225
+
226
+ end
227
+
228
+ end
formal/afp/AI_Planning_Languages_Semantics/document/root.tex ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ \documentclass[11pt,a4paper]{article}
2
+ \usepackage[T1]{fontenc}
3
+ \usepackage{isabelle,isabellesym}
4
+
5
+ % further packages required for unusual symbols (see also
6
+ % isabellesym.sty), use only when needed
7
+
8
+ %\usepackage{amssymb}
9
+ %for \<leadsto>, \<box>, \<diamond>, \<sqsupset>, \<mho>, \<Join>,
10
+ %\<lhd>, \<lesssim>, \<greatersim>, \<lessapprox>, \<greaterapprox>,
11
+ %\<triangleq>, \<yen>, \<lozenge>
12
+
13
+ %\usepackage{eurosym}
14
+ %for \<euro>
15
+
16
+ %\usepackage[only,bigsqcap]{stmaryrd}
17
+ %for \<Sqinter>
18
+
19
+ %\usepackage{eufrak}
20
+ %for \<AA> ... \<ZZ>, \<aa> ... \<zz> (also included in amssymb)
21
+
22
+ %\usepackage{textcomp}
23
+ %for \<onequarter>, \<onehalf>, \<threequarters>, \<degree>, \<cent>,
24
+ %\<currency>
25
+
26
+ \usepackage{wasysym}
27
+
28
+ % this should be the last package used
29
+ \usepackage{pdfsetup}
30
+
31
+ % urls in roman style, theory text in math-similar italics
32
+ \urlstyle{rm}
33
+ \isabellestyle{it}
34
+
35
+ % for uniform font size
36
+ %\renewcommand{\isastyle}{\isastyleminor}
37
+
38
+
39
+ \begin{document}
40
+
41
+ \title{Semantics of AI Planning Languages}
42
+ \author{Mohammad Abdulaziz and Peter Lammich\footnote{Author names are alphabetically ordered.}}
43
+
44
+ % \subtitle{Proof Document}
45
+ % \author{M. Abdulaziz \and P. Lammich}
46
+ \date{}
47
+
48
+ \maketitle
49
+
50
+ This is an Isabelle/HOL formalisation of the semantics of the multi-valued planning tasks language that is used by the planning system Fast-Downward~\cite{helmert2006fast}, the STRIPS~\cite{fikes1971strips} fragment of the Planning Domain Definition Language~\cite{PDDLref} (PDDL), and the STRIPS soundness meta-theory developed by Lifschitz~\cite{lifschitz1987semantics}.
51
+ It also contains formally verified checkers for checking the well-formedness of problems specified in either language as well the correctness of potential solutions.
52
+ The formalisation in this entry was described in an earlier publication~\cite{ictai2018}.
53
+
54
+ \tableofcontents
55
+
56
+ \clearpage
57
+
58
+ % sane default for proof documents
59
+ \parindent 0pt\parskip 0.5ex
60
+
61
+ % generated text of all theories
62
+ \input{session}
63
+
64
+ \bibliographystyle{abbrv}
65
+ \bibliography{root}
66
+
67
+ \end{document}
68
+
69
+ %%% Local Variables:
70
+ %%% mode: latex
71
+ %%% TeX-master: t
72
+ %%% End:
formal/afp/AODV/All.thy ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: All.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ theory %invisible All
8
+ imports Aodv_Loop_Freedom
9
+ "variants/a_norreqid/A_Aodv_Loop_Freedom"
10
+ "variants/b_fwdrreps/B_Aodv_Loop_Freedom"
11
+ "variants/c_gtobcast/C_Aodv_Loop_Freedom"
12
+ "variants/d_fwdrreqs/D_Aodv_Loop_Freedom"
13
+ "variants/e_all_abcd/E_Aodv_Loop_Freedom"
14
+ begin
15
+
16
+ end %invisible
formal/afp/AODV/Aodv.thy ADDED
@@ -0,0 +1,535 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Aodv.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "The AODV protocol"
7
+
8
+ theory Aodv
9
+ imports Aodv_Data Aodv_Message
10
+ AWN.AWN_SOS_Labels AWN.AWN_Invariants
11
+ begin
12
+
13
+ subsection "Data state"
14
+
15
+ record state =
16
+ ip :: "ip"
17
+ sn :: "sqn"
18
+ rt :: "rt"
19
+ rreqs :: "(ip \<times> rreqid) set"
20
+ store :: "store"
21
+ (* all locals *)
22
+ msg :: "msg"
23
+ data :: "data"
24
+ dests :: "ip \<rightharpoonup> sqn"
25
+ pre :: "ip set"
26
+ rreqid :: "rreqid"
27
+ dip :: "ip"
28
+ oip :: "ip"
29
+ hops :: "nat"
30
+ dsn :: "sqn"
31
+ dsk :: "k"
32
+ osn :: "sqn"
33
+ sip :: "ip"
34
+
35
+ abbreviation aodv_init :: "ip \<Rightarrow> state"
36
+ where "aodv_init i \<equiv> \<lparr>
37
+ ip = i,
38
+ sn = 1,
39
+ rt = Map.empty,
40
+ rreqs = {},
41
+ store = Map.empty,
42
+
43
+ msg = (SOME x. True),
44
+ data = (SOME x. True),
45
+ dests = (SOME x. True),
46
+ pre = (SOME x. True),
47
+ rreqid = (SOME x. True),
48
+ dip = (SOME x. True),
49
+ oip = (SOME x. True),
50
+ hops = (SOME x. True),
51
+ dsn = (SOME x. True),
52
+ dsk = (SOME x. True),
53
+ osn = (SOME x. True),
54
+ sip = (SOME x. x \<noteq> i)
55
+ \<rparr>"
56
+
57
+ lemma some_neq_not_eq [simp]: "\<not>((SOME x :: nat. x \<noteq> i) = i)"
58
+ by (subst some_eq_ex) (metis zero_neq_numeral)
59
+
60
+ definition clear_locals :: "state \<Rightarrow> state"
61
+ where "clear_locals \<xi> = \<xi> \<lparr>
62
+ msg := (SOME x. True),
63
+ data := (SOME x. True),
64
+ dests := (SOME x. True),
65
+ pre := (SOME x. True),
66
+ rreqid := (SOME x. True),
67
+ dip := (SOME x. True),
68
+ oip := (SOME x. True),
69
+ hops := (SOME x. True),
70
+ dsn := (SOME x. True),
71
+ dsk := (SOME x. True),
72
+ osn := (SOME x. True),
73
+ sip := (SOME x. x \<noteq> ip \<xi>)
74
+ \<rparr>"
75
+
76
+ lemma clear_locals_sip_not_ip [simp]: "\<not>(sip (clear_locals \<xi>) = ip \<xi>)"
77
+ unfolding clear_locals_def by simp
78
+
79
+ lemma clear_locals_but_not_globals [simp]:
80
+ "ip (clear_locals \<xi>) = ip \<xi>"
81
+ "sn (clear_locals \<xi>) = sn \<xi>"
82
+ "rt (clear_locals \<xi>) = rt \<xi>"
83
+ "rreqs (clear_locals \<xi>) = rreqs \<xi>"
84
+ "store (clear_locals \<xi>) = store \<xi>"
85
+ unfolding clear_locals_def by auto
86
+
87
+ subsection "Auxilliary message handling definitions"
88
+
89
+ definition is_newpkt
90
+ where "is_newpkt \<xi> \<equiv> case msg \<xi> of
91
+ Newpkt data' dip' \<Rightarrow> { \<xi>\<lparr>data := data', dip := dip'\<rparr> }
92
+ | _ \<Rightarrow> {}"
93
+
94
+ definition is_pkt
95
+ where "is_pkt \<xi> \<equiv> case msg \<xi> of
96
+ Pkt data' dip' oip' \<Rightarrow> { \<xi>\<lparr> data := data', dip := dip', oip := oip' \<rparr> }
97
+ | _ \<Rightarrow> {}"
98
+
99
+ definition is_rreq
100
+ where "is_rreq \<xi> \<equiv> case msg \<xi> of
101
+ Rreq hops' rreqid' dip' dsn' dsk' oip' osn' sip' \<Rightarrow>
102
+ { \<xi>\<lparr> hops := hops', rreqid := rreqid', dip := dip', dsn := dsn',
103
+ dsk := dsk', oip := oip', osn := osn', sip := sip' \<rparr> }
104
+ | _ \<Rightarrow> {}"
105
+
106
+ lemma is_rreq_asm [dest!]:
107
+ assumes "\<xi>' \<in> is_rreq \<xi>"
108
+ shows "(\<exists>hops' rreqid' dip' dsn' dsk' oip' osn' sip'.
109
+ msg \<xi> = Rreq hops' rreqid' dip' dsn' dsk' oip' osn' sip' \<and>
110
+ \<xi>' = \<xi>\<lparr> hops := hops', rreqid := rreqid', dip := dip', dsn := dsn',
111
+ dsk := dsk', oip := oip', osn := osn', sip := sip' \<rparr>)"
112
+ using assms unfolding is_rreq_def
113
+ by (cases "msg \<xi>") simp_all
114
+
115
+ definition is_rrep
116
+ where "is_rrep \<xi> \<equiv> case msg \<xi> of
117
+ Rrep hops' dip' dsn' oip' sip' \<Rightarrow>
118
+ { \<xi>\<lparr> hops := hops', dip := dip', dsn := dsn', oip := oip', sip := sip' \<rparr> }
119
+ | _ \<Rightarrow> {}"
120
+
121
+ lemma is_rrep_asm [dest!]:
122
+ assumes "\<xi>' \<in> is_rrep \<xi>"
123
+ shows "(\<exists>hops' dip' dsn' oip' sip'.
124
+ msg \<xi> = Rrep hops' dip' dsn' oip' sip' \<and>
125
+ \<xi>' = \<xi>\<lparr> hops := hops', dip := dip', dsn := dsn', oip := oip', sip := sip' \<rparr>)"
126
+ using assms unfolding is_rrep_def
127
+ by (cases "msg \<xi>") simp_all
128
+
129
+ definition is_rerr
130
+ where "is_rerr \<xi> \<equiv> case msg \<xi> of
131
+ Rerr dests' sip' \<Rightarrow> { \<xi>\<lparr> dests := dests', sip := sip' \<rparr> }
132
+ | _ \<Rightarrow> {}"
133
+
134
+ lemma is_rerr_asm [dest!]:
135
+ assumes "\<xi>' \<in> is_rerr \<xi>"
136
+ shows "(\<exists>dests' sip'.
137
+ msg \<xi> = Rerr dests' sip' \<and>
138
+ \<xi>' = \<xi>\<lparr> dests := dests', sip := sip' \<rparr>)"
139
+ using assms unfolding is_rerr_def
140
+ by (cases "msg \<xi>") simp_all
141
+
142
+ lemmas is_msg_defs =
143
+ is_rerr_def is_rrep_def is_rreq_def is_pkt_def is_newpkt_def
144
+
145
+ lemma is_msg_inv_ip [simp]:
146
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
147
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
148
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
149
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
150
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
151
+ unfolding is_msg_defs
152
+ by (cases "msg \<xi>", clarsimp+)+
153
+
154
+ lemma is_msg_inv_sn [simp]:
155
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
156
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
157
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
158
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
159
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
160
+ unfolding is_msg_defs
161
+ by (cases "msg \<xi>", clarsimp+)+
162
+
163
+ lemma is_msg_inv_rt [simp]:
164
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
165
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
166
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
167
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
168
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
169
+ unfolding is_msg_defs
170
+ by (cases "msg \<xi>", clarsimp+)+
171
+
172
+ lemma is_msg_inv_rreqs [simp]:
173
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
174
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
175
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
176
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
177
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
178
+ unfolding is_msg_defs
179
+ by (cases "msg \<xi>", clarsimp+)+
180
+
181
+ lemma is_msg_inv_store [simp]:
182
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
183
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
184
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
185
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
186
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
187
+ unfolding is_msg_defs
188
+ by (cases "msg \<xi>", clarsimp+)+
189
+
190
+ lemma is_msg_inv_sip [simp]:
191
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> sip \<xi>' = sip \<xi>"
192
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> sip \<xi>' = sip \<xi>"
193
+ unfolding is_msg_defs
194
+ by (cases "msg \<xi>", clarsimp+)+
195
+
196
+ subsection "The protocol process"
197
+
198
+ datatype pseqp =
199
+ PAodv
200
+ | PNewPkt
201
+ | PPkt
202
+ | PRreq
203
+ | PRrep
204
+ | PRerr
205
+
206
+ fun nat_of_seqp :: "pseqp \<Rightarrow> nat"
207
+ where
208
+ "nat_of_seqp PAodv = 1"
209
+ | "nat_of_seqp PPkt = 2"
210
+ | "nat_of_seqp PNewPkt = 3"
211
+ | "nat_of_seqp PRreq = 4"
212
+ | "nat_of_seqp PRrep = 5"
213
+ | "nat_of_seqp PRerr = 6"
214
+
215
+ instantiation "pseqp" :: ord
216
+ begin
217
+ definition less_eq_seqp [iff]: "l1 \<le> l2 = (nat_of_seqp l1 \<le> nat_of_seqp l2)"
218
+ definition less_seqp [iff]: "l1 < l2 = (nat_of_seqp l1 < nat_of_seqp l2)"
219
+ instance ..
220
+ end
221
+
222
+ abbreviation AODV
223
+ where
224
+ "AODV \<equiv> \<lambda>_. \<lbrakk>clear_locals\<rbrakk> call(PAodv)"
225
+
226
+ abbreviation PKT
227
+ where
228
+ "PKT args \<equiv>
229
+
230
+ \<lbrakk>\<xi>. let (data, dip, oip) = args \<xi> in
231
+ (clear_locals \<xi>) \<lparr> data := data, dip := dip, oip := oip \<rparr>\<rbrakk>
232
+ call(PPkt)"
233
+ abbreviation NEWPKT
234
+ where
235
+ "NEWPKT args \<equiv>
236
+ \<lbrakk>\<xi>. let (data, dip) = args \<xi> in
237
+ (clear_locals \<xi>) \<lparr> data := data, dip := dip \<rparr>\<rbrakk>
238
+ call(PNewPkt)"
239
+
240
+ abbreviation RREQ
241
+ where
242
+ "RREQ args \<equiv>
243
+ \<lbrakk>\<xi>. let (hops, rreqid, dip, dsn, dsk, oip, osn, sip) = args \<xi> in
244
+ (clear_locals \<xi>) \<lparr> hops := hops, rreqid := rreqid, dip := dip,
245
+ dsn := dsn, dsk := dsk, oip := oip,
246
+ osn := osn, sip := sip \<rparr>\<rbrakk>
247
+ call(PRreq)"
248
+
249
+ abbreviation RREP
250
+ where
251
+ "RREP args \<equiv>
252
+ \<lbrakk>\<xi>. let (hops, dip, dsn, oip, sip) = args \<xi> in
253
+ (clear_locals \<xi>) \<lparr> hops := hops, dip := dip, dsn := dsn,
254
+ oip := oip, sip := sip \<rparr>\<rbrakk>
255
+ call(PRrep)"
256
+
257
+ abbreviation RERR
258
+ where
259
+ "RERR args \<equiv>
260
+ \<lbrakk>\<xi>. let (dests, sip) = args \<xi> in
261
+ (clear_locals \<xi>) \<lparr> dests := dests, sip := sip \<rparr>\<rbrakk>
262
+ call(PRerr)"
263
+
264
+ fun \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V :: "(state, msg, pseqp, pseqp label) seqp_env"
265
+ where
266
+ "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv = labelled PAodv (
267
+ receive(\<lambda>msg' \<xi>. \<xi> \<lparr> msg := msg' \<rparr>).
268
+ ( \<langle>is_newpkt\<rangle> NEWPKT(\<lambda>\<xi>. (data \<xi>, ip \<xi>))
269
+ \<oplus> \<langle>is_pkt\<rangle> PKT(\<lambda>\<xi>. (data \<xi>, dip \<xi>, oip \<xi>))
270
+ \<oplus> \<langle>is_rreq\<rangle>
271
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
272
+ RREQ(\<lambda>\<xi>. (hops \<xi>, rreqid \<xi>, dip \<xi>, dsn \<xi>, dsk \<xi>, oip \<xi>, osn \<xi>, sip \<xi>))
273
+ \<oplus> \<langle>is_rrep\<rangle>
274
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
275
+ RREP(\<lambda>\<xi>. (hops \<xi>, dip \<xi>, dsn \<xi>, oip \<xi>, sip \<xi>))
276
+ \<oplus> \<langle>is_rerr\<rangle>
277
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
278
+ RERR(\<lambda>\<xi>. (dests \<xi>, sip \<xi>))
279
+ )
280
+ \<oplus> \<langle>\<lambda>\<xi>. { \<xi>\<lparr> dip := dip \<rparr> | dip. dip \<in> qD(store \<xi>) \<inter> vD(rt \<xi>) }\<rangle>
281
+ \<lbrakk>\<xi>. \<xi> \<lparr> data := hd(\<sigma>\<^bsub>queue\<^esub>(store \<xi>, dip \<xi>)) \<rparr>\<rbrakk>
282
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (dip \<xi>)), \<lambda>\<xi>. pkt(data \<xi>, dip \<xi>, ip \<xi>)).
283
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := the (drop (dip \<xi>) (store \<xi>)) \<rparr>\<rbrakk>
284
+ AODV()
285
+ \<triangleright> \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (dip \<xi>))
286
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
287
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
288
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
289
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
290
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
291
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
292
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)). AODV()
293
+ \<oplus> \<langle>\<lambda>\<xi>. { \<xi>\<lparr> dip := dip \<rparr>
294
+ | dip. dip \<in> qD(store \<xi>) - vD(rt \<xi>) \<and> the (\<sigma>\<^bsub>p-flag\<^esub>(store \<xi>, dip)) = req }\<rangle>
295
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := unsetRRF (store \<xi>) (dip \<xi>) \<rparr>\<rbrakk>
296
+ \<lbrakk>\<xi>. \<xi> \<lparr> sn := inc (sn \<xi>) \<rparr>\<rbrakk>
297
+ \<lbrakk>\<xi>. \<xi> \<lparr> rreqid := nrreqid (rreqs \<xi>) (ip \<xi>) \<rparr>\<rbrakk>
298
+ \<lbrakk>\<xi>. \<xi> \<lparr> rreqs := rreqs \<xi> \<union> {(ip \<xi>, rreqid \<xi>)} \<rparr>\<rbrakk>
299
+ broadcast(\<lambda>\<xi>. rreq(0, rreqid \<xi>, dip \<xi>, sqn (rt \<xi>) (dip \<xi>), sqnf (rt \<xi>) (dip \<xi>),
300
+ ip \<xi>, sn \<xi>, ip \<xi>)). AODV())"
301
+
302
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt = labelled PNewPkt (
303
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
304
+ deliver(\<lambda>\<xi>. data \<xi>).AODV()
305
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
306
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := add (data \<xi>) (dip \<xi>) (store \<xi>) \<rparr>\<rbrakk>
307
+ AODV())"
308
+
309
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt = labelled PPkt (
310
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
311
+ deliver(\<lambda>\<xi>. data \<xi>).AODV()
312
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
313
+ (
314
+ \<langle>\<xi>. dip \<xi> \<in> vD (rt \<xi>)\<rangle>
315
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (dip \<xi>)), \<lambda>\<xi>. pkt(data \<xi>, dip \<xi>, oip \<xi>)).AODV()
316
+ \<triangleright>
317
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (dip \<xi>))
318
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
319
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
320
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
321
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
322
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
323
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
324
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
325
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> vD (rt \<xi>)\<rangle>
326
+ (
327
+ \<langle>\<xi>. dip \<xi> \<in> iD (rt \<xi>)\<rangle>
328
+ groupcast(\<lambda>\<xi>. the (precs (rt \<xi>) (dip \<xi>)),
329
+ \<lambda>\<xi>. rerr([dip \<xi> \<mapsto> sqn (rt \<xi>) (dip \<xi>)], ip \<xi>)). AODV()
330
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> iD (rt \<xi>)\<rangle>
331
+ AODV()
332
+ )
333
+ ))"
334
+
335
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq = labelled PRreq (
336
+ \<langle>\<xi>. (oip \<xi>, rreqid \<xi>) \<in> rreqs \<xi>\<rangle>
337
+ AODV()
338
+ \<oplus> \<langle>\<xi>. (oip \<xi>, rreqid \<xi>) \<notin> rreqs \<xi>\<rangle>
339
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rparr>\<rbrakk>
340
+ \<lbrakk>\<xi>. \<xi> \<lparr> rreqs := rreqs \<xi> \<union> {(oip \<xi>, rreqid \<xi>)} \<rparr>\<rbrakk>
341
+ (
342
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
343
+ \<lbrakk>\<xi>. \<xi> \<lparr> sn := max (sn \<xi>) (dsn \<xi>) \<rparr>\<rbrakk>
344
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(0, dip \<xi>, sn \<xi>, oip \<xi>, ip \<xi>)).AODV()
345
+ \<triangleright>
346
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
347
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
348
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
349
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
350
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
351
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
352
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
353
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
354
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
355
+ (
356
+ \<langle>\<xi>. dip \<xi> \<in> vD (rt \<xi>) \<and> dsn \<xi> \<le> sqn (rt \<xi>) (dip \<xi>) \<and> sqnf (rt \<xi>) (dip \<xi>) = kno\<rangle>
357
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (dip \<xi>) {sip \<xi>}) \<rparr>\<rbrakk>
358
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (oip \<xi>) {the (nhop (rt \<xi>) (dip \<xi>))}) \<rparr>\<rbrakk>
359
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(the (dhops (rt \<xi>) (dip \<xi>)), dip \<xi>,
360
+ sqn (rt \<xi>) (dip \<xi>), oip \<xi>, ip \<xi>)).
361
+ AODV()
362
+ \<triangleright>
363
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
364
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
365
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
366
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
367
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
368
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
369
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
370
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
371
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> vD (rt \<xi>) \<or> sqn (rt \<xi>) (dip \<xi>) < dsn \<xi> \<or> sqnf (rt \<xi>) (dip \<xi>) = unk\<rangle>
372
+ broadcast(\<lambda>\<xi>. rreq(hops \<xi> + 1, rreqid \<xi>, dip \<xi>, max (sqn (rt \<xi>) (dip \<xi>)) (dsn \<xi>),
373
+ dsk \<xi>, oip \<xi>, osn \<xi>, ip \<xi>)).
374
+ AODV()
375
+ )
376
+ ))"
377
+
378
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep = labelled PRrep (
379
+ \<langle>\<xi>. rt \<xi> \<noteq> update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rangle>
380
+ (
381
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rparr> \<rbrakk>
382
+ (
383
+ \<langle>\<xi>. oip \<xi> = ip \<xi> \<rangle>
384
+ AODV()
385
+ \<oplus> \<langle>\<xi>. oip \<xi> \<noteq> ip \<xi> \<rangle>
386
+ (
387
+ \<langle>\<xi>. oip \<xi> \<in> vD (rt \<xi>)\<rangle>
388
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (dip \<xi>) {the (nhop (rt \<xi>) (oip \<xi>))}) \<rparr>\<rbrakk>
389
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (the (nhop (rt \<xi>) (dip \<xi>)))
390
+ {the (nhop (rt \<xi>) (oip \<xi>))}) \<rparr>\<rbrakk>
391
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(hops \<xi> + 1, dip \<xi>, dsn \<xi>, oip \<xi>, ip \<xi>)).
392
+ AODV()
393
+ \<triangleright>
394
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
395
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
396
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
397
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
398
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
399
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
400
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
401
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
402
+ \<oplus> \<langle>\<xi>. oip \<xi> \<notin> vD (rt \<xi>)\<rangle>
403
+ AODV()
404
+ )
405
+ )
406
+ )
407
+ \<oplus> \<langle>\<xi>. rt \<xi> = update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rangle>
408
+ AODV()
409
+ )"
410
+
411
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr = labelled PRerr (
412
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. case (dests \<xi>) rip of None \<Rightarrow> None
413
+ | Some rsn \<Rightarrow> if rip \<in> vD (rt \<xi>) \<and> the (nhop (rt \<xi>) rip) = sip \<xi>
414
+ \<and> sqn (rt \<xi>) rip < rsn then Some rsn else None) \<rparr>\<rbrakk>
415
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
416
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
417
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
418
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
419
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
420
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)). AODV())"
421
+
422
+ declare \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V.simps [simp del, code del]
423
+ lemmas \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps [simp, code] = \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V.simps [simplified]
424
+
425
+ fun \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton
426
+ where
427
+ "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PAodv = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv)"
428
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PNewPkt = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt)"
429
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PPkt = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt)"
430
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRreq = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq)"
431
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRrep = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep)"
432
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRerr = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr)"
433
+
434
+ lemma \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton_wf [simp]:
435
+ "wellformed \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton"
436
+ proof (rule, intro allI)
437
+ fix pn pn'
438
+ show "call(pn') \<notin> stermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton pn)"
439
+ by (cases pn) simp_all
440
+ qed
441
+
442
+ declare \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton.simps [simp del, code del]
443
+ lemmas \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton_simps [simp, code]
444
+ = \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton.simps [simplified \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps seqp_skeleton.simps]
445
+
446
+ lemma aodv_proc_cases [dest]:
447
+ fixes p pn
448
+ shows "p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn) \<Longrightarrow>
449
+ (p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv) \<or>
450
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt) \<or>
451
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt) \<or>
452
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq) \<or>
453
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep) \<or>
454
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr))"
455
+ by (cases pn) simp_all
456
+
457
+ definition \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V :: "ip \<Rightarrow> (state \<times> (state, msg, pseqp, pseqp label) seqp) set"
458
+ where "\<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<equiv> {(aodv_init i, \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv)}"
459
+
460
+ abbreviation paodv
461
+ :: "ip \<Rightarrow> (state \<times> (state, msg, pseqp, pseqp label) seqp, msg seq_action) automaton"
462
+ where
463
+ "paodv i \<equiv> \<lparr> init = \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i, trans = seqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V \<rparr>"
464
+
465
+ lemma aodv_trans: "trans (paodv i) = seqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
466
+ by simp
467
+
468
+ lemma aodv_control_within [simp]: "control_within \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (init (paodv i))"
469
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by (rule control_withinI) (auto simp del: \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps)
470
+
471
+ lemma aodv_wf [simp]:
472
+ "wellformed \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
473
+ proof (rule, intro allI)
474
+ fix pn pn'
475
+ show "call(pn') \<notin> stermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn)"
476
+ by (cases pn) simp_all
477
+ qed
478
+
479
+ lemmas aodv_labels_not_empty [simp] = labels_not_empty [OF aodv_wf]
480
+
481
+ lemma aodv_ex_label [intro]: "\<exists>l. l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
482
+ by (metis aodv_labels_not_empty all_not_in_conv)
483
+
484
+ lemma aodv_ex_labelE [elim]:
485
+ assumes "\<forall>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p. P l p"
486
+ and "\<exists>p l. P l p \<Longrightarrow> Q"
487
+ shows "Q"
488
+ using assms by (metis aodv_ex_label)
489
+
490
+ lemma aodv_simple_labels [simp]: "simple_labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
491
+ proof
492
+ fix pn p
493
+ assume "p\<in>subterms(\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn)"
494
+ thus "\<exists>!l. labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p = {l}"
495
+ by (cases pn) (simp_all cong: seqp_congs | elim disjE)+
496
+ qed
497
+
498
+ lemma \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_labels [simp]: "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<Longrightarrow> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p = {PAodv-:0}"
499
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
500
+
501
+ lemma aodv_init_kD_empty [simp]:
502
+ "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<Longrightarrow> kD (rt \<xi>) = {}"
503
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def kD_def by simp
504
+
505
+ lemma aodv_init_sip_not_ip [simp]: "\<not>(sip (aodv_init i) = i)" by simp
506
+
507
+ lemma aodv_init_sip_not_ip' [simp]:
508
+ assumes "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
509
+ shows "sip \<xi> \<noteq> ip \<xi>"
510
+ using assms unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
511
+
512
+ lemma aodv_init_sip_not_i [simp]:
513
+ assumes "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
514
+ shows "sip \<xi> \<noteq> i"
515
+ using assms unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
516
+
517
+ lemma clear_locals_sip_not_ip':
518
+ assumes "ip \<xi> = i"
519
+ shows "\<not>(sip (clear_locals \<xi>) = i)"
520
+ using assms by auto
521
+
522
+ text \<open>Stop the simplifier from descending into process terms.\<close>
523
+ declare seqp_congs [cong]
524
+
525
+ text \<open>Configure the main invariant tactic for AODV.\<close>
526
+
527
+ declare
528
+ \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps [cterms_env]
529
+ aodv_proc_cases [ctermsl_cases]
530
+ seq_invariant_ctermsI [OF aodv_wf aodv_control_within aodv_simple_labels aodv_trans,
531
+ cterms_intros]
532
+ seq_step_invariant_ctermsI [OF aodv_wf aodv_control_within aodv_simple_labels aodv_trans,
533
+ cterms_intros]
534
+
535
+ end
formal/afp/AODV/Aodv_Basic.thy ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Aodv_Basic.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Basic data types and constants"
7
+
8
+ theory Aodv_Basic
9
+ imports Main AWN.AWN_SOS
10
+ begin
11
+
12
+ text \<open>These definitions are shared with all variants.\<close>
13
+
14
+ type_synonym rreqid = nat
15
+ type_synonym sqn = nat
16
+
17
+ datatype k = Known | Unknown
18
+ abbreviation kno where "kno \<equiv> Known"
19
+ abbreviation unk where "unk \<equiv> Unknown"
20
+
21
+ datatype p = NoRequestRequired | RequestRequired
22
+ abbreviation noreq where "noreq \<equiv> NoRequestRequired"
23
+ abbreviation req where "req \<equiv> RequestRequired"
24
+
25
+ datatype f = Valid | Invalid
26
+ abbreviation val where "val \<equiv> Valid"
27
+ abbreviation inv where "inv \<equiv> Invalid"
28
+
29
+ lemma not_ks [simp]:
30
+ "(x \<noteq> kno) = (x = unk)"
31
+ "(x \<noteq> unk) = (x = kno)"
32
+ by (cases x, clarsimp+)+
33
+
34
+ lemma not_ps [simp]:
35
+ "(x \<noteq> noreq) = (x = req)"
36
+ "(x \<noteq> req) = (x = noreq)"
37
+ by (cases x, clarsimp+)+
38
+
39
+ lemma not_ffs [simp]:
40
+ "(x \<noteq> val) = (x = inv)"
41
+ "(x \<noteq> inv) = (x = val)"
42
+ by (cases x, clarsimp+)+
43
+
44
+ end
formal/afp/AODV/Aodv_Data.thy ADDED
@@ -0,0 +1,990 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Aodv_Data.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Predicates and functions used in the AODV model"
7
+
8
+ theory Aodv_Data
9
+ imports Aodv_Basic
10
+ begin
11
+
12
+ subsection "Sequence Numbers"
13
+
14
+ text \<open>Sequence numbers approximate the relative freshness of routing information.\<close>
15
+
16
+ definition inc :: "sqn \<Rightarrow> sqn"
17
+ where "inc sn \<equiv> if sn = 0 then sn else sn + 1"
18
+
19
+ lemma less_than_inc [simp]: "x \<le> inc x"
20
+ unfolding inc_def by simp
21
+
22
+ lemma inc_minus_suc_0 [simp]:
23
+ "inc x - Suc 0 = x"
24
+ unfolding inc_def by simp
25
+
26
+ lemma inc_never_one' [simp, intro]: "inc x \<noteq> Suc 0"
27
+ unfolding inc_def by simp
28
+
29
+ lemma inc_never_one [simp, intro]: "inc x \<noteq> 1"
30
+ by simp
31
+
32
+ subsection "Modelling Routes"
33
+
34
+ text \<open>
35
+ A route is a 6-tuple, @{term "(dsn, dsk, flag, hops, nhip, pre)"} where
36
+ @{term dsn} is the `destination sequence number', @{term dsk} is the
37
+ `destination-sequence-number status', @{term flag} is the route status,
38
+ @{term hops} is the number of hops to the destination, @{term nhip} is the
39
+ next hop toward the destination, and @{term pre} is the set of `precursor nodes'--those
40
+ interested in hearing about changes to the route.
41
+ \<close>
42
+
43
+ type_synonym r = "sqn \<times> k \<times> f \<times> nat \<times> ip \<times> ip set"
44
+
45
+ definition proj2 :: "r \<Rightarrow> sqn" ("\<pi>\<^sub>2")
46
+ where "\<pi>\<^sub>2 \<equiv> \<lambda>(dsn, _, _, _, _, _). dsn"
47
+
48
+ definition proj3 :: "r \<Rightarrow> k" ("\<pi>\<^sub>3")
49
+ where "\<pi>\<^sub>3 \<equiv> \<lambda>(_, dsk, _, _, _, _). dsk"
50
+
51
+ definition proj4 :: "r \<Rightarrow> f" ("\<pi>\<^sub>4")
52
+ where "\<pi>\<^sub>4 \<equiv> \<lambda>(_, _, flag, _, _, _). flag"
53
+
54
+ definition proj5 :: "r \<Rightarrow> nat" ("\<pi>\<^sub>5")
55
+ where "\<pi>\<^sub>5 \<equiv> \<lambda>(_, _, _, hops, _, _). hops"
56
+
57
+ definition proj6 :: "r \<Rightarrow> ip" ("\<pi>\<^sub>6")
58
+ where "\<pi>\<^sub>6 \<equiv> \<lambda>(_, _, _, _, nhip, _). nhip"
59
+
60
+ definition proj7 :: "r \<Rightarrow> ip set" ("\<pi>\<^sub>7")
61
+ where "\<pi>\<^sub>7 \<equiv> \<lambda>(_, _, _, _, _, pre). pre"
62
+
63
+ lemma projs [simp]:
64
+ "\<pi>\<^sub>2(dsn, dsk, flag, hops, nhip, pre) = dsn"
65
+ "\<pi>\<^sub>3(dsn, dsk, flag, hops, nhip, pre) = dsk"
66
+ "\<pi>\<^sub>4(dsn, dsk, flag, hops, nhip, pre) = flag"
67
+ "\<pi>\<^sub>5(dsn, dsk, flag, hops, nhip, pre) = hops"
68
+ "\<pi>\<^sub>6(dsn, dsk, flag, hops, nhip, pre) = nhip"
69
+ "\<pi>\<^sub>7(dsn, dsk, flag, hops, nhip, pre) = pre"
70
+ by (clarsimp simp: proj2_def proj3_def proj4_def
71
+ proj5_def proj6_def proj7_def)+
72
+
73
+ lemma proj3_pred [intro]: "\<lbrakk> P kno; P unk \<rbrakk> \<Longrightarrow> P (\<pi>\<^sub>3 x)"
74
+ by (rule k.induct)
75
+
76
+ lemma proj4_pred [intro]: "\<lbrakk> P val; P inv \<rbrakk> \<Longrightarrow> P (\<pi>\<^sub>4 x)"
77
+ by (rule f.induct)
78
+
79
+ lemma proj6_pair_snd [simp]:
80
+ fixes dsn' r
81
+ shows "\<pi>\<^sub>6 (dsn', snd (r)) = \<pi>\<^sub>6(r)"
82
+ by (cases r) simp
83
+
84
+ subsection "Routing Tables"
85
+
86
+ text \<open>Routing tables map ip addresses to route entries.\<close>
87
+
88
+ type_synonym rt = "ip \<rightharpoonup> r"
89
+
90
+ syntax
91
+ "_Sigma_route" :: "rt \<Rightarrow> ip \<rightharpoonup> r" ("\<sigma>\<^bsub>route\<^esub>'(_, _')")
92
+
93
+ translations
94
+ "\<sigma>\<^bsub>route\<^esub>(rt, dip)" => "rt dip"
95
+
96
+ definition sqn :: "rt \<Rightarrow> ip \<Rightarrow> sqn"
97
+ where "sqn rt dip \<equiv> case \<sigma>\<^bsub>route\<^esub>(rt, dip) of Some r \<Rightarrow> \<pi>\<^sub>2(r) | None \<Rightarrow> 0"
98
+
99
+ definition sqnf :: "rt \<Rightarrow> ip \<Rightarrow> k"
100
+ where "sqnf rt dip \<equiv> case \<sigma>\<^bsub>route\<^esub>(rt, dip) of Some r \<Rightarrow> \<pi>\<^sub>3(r) | None \<Rightarrow> unk"
101
+
102
+ abbreviation flag :: "rt \<Rightarrow> ip \<rightharpoonup> f"
103
+ where "flag rt dip \<equiv> map_option \<pi>\<^sub>4 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
104
+
105
+ abbreviation dhops :: "rt \<Rightarrow> ip \<rightharpoonup> nat"
106
+ where "dhops rt dip \<equiv> map_option \<pi>\<^sub>5 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
107
+
108
+ abbreviation nhop :: "rt \<Rightarrow> ip \<rightharpoonup> ip"
109
+ where "nhop rt dip \<equiv> map_option \<pi>\<^sub>6 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
110
+
111
+ abbreviation precs :: "rt \<Rightarrow> ip \<rightharpoonup> ip set"
112
+ where "precs rt dip \<equiv> map_option \<pi>\<^sub>7 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
113
+
114
+ definition vD :: "rt \<Rightarrow> ip set"
115
+ where "vD rt \<equiv> {dip. flag rt dip = Some val}"
116
+
117
+ definition iD :: "rt \<Rightarrow> ip set"
118
+ where "iD rt \<equiv> {dip. flag rt dip = Some inv}"
119
+
120
+ definition kD :: "rt \<Rightarrow> ip set"
121
+ where "kD rt \<equiv> {dip. rt dip \<noteq> None}"
122
+
123
+ lemma kD_is_vD_and_iD: "kD rt = vD rt \<union> iD rt"
124
+ unfolding kD_def vD_def iD_def by auto
125
+
126
+ lemma vD_iD_gives_kD [simp]:
127
+ "\<And>ip rt. ip \<in> vD rt \<Longrightarrow> ip \<in> kD rt"
128
+ "\<And>ip rt. ip \<in> iD rt \<Longrightarrow> ip \<in> kD rt"
129
+ unfolding kD_is_vD_and_iD by simp_all
130
+
131
+ lemma kD_Some [dest]:
132
+ fixes dip rt
133
+ assumes "dip \<in> kD rt"
134
+ shows "\<exists>dsn dsk flag hops nhip pre.
135
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, flag, hops, nhip, pre)"
136
+ using assms unfolding kD_def by simp
137
+
138
+ lemma kD_None [dest]:
139
+ fixes dip rt
140
+ assumes "dip \<notin> kD rt"
141
+ shows "\<sigma>\<^bsub>route\<^esub>(rt, dip) = None"
142
+ using assms unfolding kD_def
143
+ by (metis (mono_tags) mem_Collect_eq)
144
+
145
+ lemma vD_Some [dest]:
146
+ fixes dip rt
147
+ assumes "dip \<in> vD rt"
148
+ shows "\<exists>dsn dsk hops nhip pre.
149
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, val, hops, nhip, pre)"
150
+ using assms unfolding vD_def by simp
151
+
152
+ lemma vD_empty [simp]: "vD Map.empty = {}"
153
+ unfolding vD_def by simp
154
+
155
+ lemma iD_Some [dest]:
156
+ fixes dip rt
157
+ assumes "dip \<in> iD rt"
158
+ shows "\<exists>dsn dsk hops nhip pre.
159
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, inv, hops, nhip, pre)"
160
+ using assms unfolding iD_def by simp
161
+
162
+ lemma val_is_vD [elim]:
163
+ fixes ip rt
164
+ assumes "ip\<in>kD(rt)"
165
+ and "the (flag rt ip) = val"
166
+ shows "ip\<in>vD(rt)"
167
+ using assms unfolding vD_def by auto
168
+
169
+ lemma inv_is_iD [elim]:
170
+ fixes ip rt
171
+ assumes "ip\<in>kD(rt)"
172
+ and "the (flag rt ip) = inv"
173
+ shows "ip\<in>iD(rt)"
174
+ using assms unfolding iD_def by auto
175
+
176
+ lemma iD_flag_is_inv [elim, simp]:
177
+ fixes ip rt
178
+ assumes "ip\<in>iD(rt)"
179
+ shows "the (flag rt ip) = inv"
180
+ proof -
181
+ from \<open>ip\<in>iD(rt)\<close> have "ip\<in>kD(rt)" by auto
182
+ with assms show ?thesis unfolding iD_def by auto
183
+ qed
184
+
185
+ lemma kD_but_not_vD_is_iD [elim]:
186
+ fixes ip rt
187
+ assumes "ip\<in>kD(rt)"
188
+ and "ip\<notin>vD(rt)"
189
+ shows "ip\<in>iD(rt)"
190
+ proof -
191
+ from \<open>ip\<in>kD(rt)\<close> obtain dsn dsk f hops nhop pre
192
+ where rtip: "rt ip = Some (dsn, dsk, f, hops, nhop, pre)"
193
+ by (metis kD_Some)
194
+ from \<open>ip\<notin>vD(rt)\<close> have "f \<noteq> val"
195
+ proof (rule contrapos_nn)
196
+ assume "f = val"
197
+ with rtip have "the (flag rt ip) = val" by simp
198
+ with \<open>ip\<in>kD(rt)\<close> show "ip\<in>vD(rt)" ..
199
+ qed
200
+ with rtip have "the (flag rt ip)= inv" by simp
201
+ with \<open>ip\<in>kD(rt)\<close> show "ip\<in>iD(rt)" ..
202
+ qed
203
+
204
+ lemma vD_or_iD [elim]:
205
+ fixes ip rt
206
+ assumes "ip\<in>kD(rt)"
207
+ and "ip\<in>vD(rt) \<Longrightarrow> P rt ip"
208
+ and "ip\<in>iD(rt) \<Longrightarrow> P rt ip"
209
+ shows "P rt ip"
210
+ proof -
211
+ from \<open>ip\<in>kD(rt)\<close> have "ip\<in>vD(rt) \<union> iD(rt)"
212
+ by (simp add: kD_is_vD_and_iD)
213
+ thus ?thesis by (auto elim: assms(2-3))
214
+ qed
215
+
216
+ lemma proj5_eq_dhops: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>5(the (rt dip)) = the (dhops rt dip)"
217
+ unfolding sqn_def by (drule kD_Some) clarsimp
218
+
219
+ lemma proj4_eq_flag: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>4(the (rt dip)) = the (flag rt dip)"
220
+ unfolding sqn_def by (drule kD_Some) clarsimp
221
+
222
+ lemma proj2_eq_sqn: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>2(the (rt dip)) = sqn rt dip"
223
+ unfolding sqn_def by (drule kD_Some) clarsimp
224
+
225
+ lemma kD_sqnf_is_proj3 [simp]:
226
+ "\<And>ip rt. ip\<in>kD(rt) \<Longrightarrow> sqnf rt ip = \<pi>\<^sub>3(the (rt ip))"
227
+ unfolding sqnf_def by auto
228
+
229
+ lemma vD_flag_val [simp]:
230
+ "\<And>dip rt. dip \<in> vD (rt) \<Longrightarrow> the (flag rt dip) = val"
231
+ unfolding vD_def by clarsimp
232
+
233
+ lemma kD_update [simp]:
234
+ "\<And>rt nip v. kD (rt(nip \<mapsto> v)) = insert nip (kD rt)"
235
+ unfolding kD_def by auto
236
+
237
+ lemma kD_empty [simp]: "kD Map.empty = {}"
238
+ unfolding kD_def by simp
239
+
240
+ lemma ip_equal_or_known [elim]:
241
+ fixes rt ip ip'
242
+ assumes "ip = ip' \<or> ip\<in>kD(rt)"
243
+ and "ip = ip' \<Longrightarrow> P rt ip ip'"
244
+ and "\<lbrakk> ip \<noteq> ip'; ip\<in>kD(rt)\<rbrakk> \<Longrightarrow> P rt ip ip'"
245
+ shows "P rt ip ip'"
246
+ using assms by auto
247
+
248
+ subsection "Updating Routing Tables"
249
+
250
+ text \<open>Routing table entries are modified through explicit functions.
251
+ The properties of these functions are important in invariant proofs.\<close>
252
+
253
+ subsubsection "Updating Precursor Lists"
254
+
255
+ definition addpre :: "r \<Rightarrow> ip set \<Rightarrow> r"
256
+ where "addpre r npre \<equiv> let (dsn, dsk, flag, hops, nhip, pre) = r in
257
+ (dsn, dsk, flag, hops, nhip, pre \<union> npre)"
258
+
259
+ lemma proj2_addpre:
260
+ fixes v pre
261
+ shows "\<pi>\<^sub>2(addpre v pre) = \<pi>\<^sub>2(v)"
262
+ unfolding addpre_def by (cases v) simp
263
+
264
+ lemma proj3_addpre:
265
+ fixes v pre
266
+ shows "\<pi>\<^sub>3(addpre v pre) = \<pi>\<^sub>3(v)"
267
+ unfolding addpre_def by (cases v) simp
268
+
269
+ lemma proj4_addpre:
270
+ fixes v pre
271
+ shows "\<pi>\<^sub>4(addpre v pre) = \<pi>\<^sub>4(v)"
272
+ unfolding addpre_def by (cases v) simp
273
+
274
+ lemma proj5_addpre:
275
+ fixes v pre
276
+ shows "\<pi>\<^sub>5(addpre v pre) = \<pi>\<^sub>5(v)"
277
+ unfolding addpre_def by (cases v) simp
278
+
279
+ lemma proj6_addpre:
280
+ fixes dsn dsk flag hops nhip pre npre
281
+ shows "\<pi>\<^sub>6(addpre v npre) = \<pi>\<^sub>6(v)"
282
+ unfolding addpre_def by (cases v) simp
283
+
284
+ lemma proj7_addpre:
285
+ fixes dsn dsk flag hops nhip pre npre
286
+ shows "\<pi>\<^sub>7(addpre v npre) = \<pi>\<^sub>7(v) \<union> npre"
287
+ unfolding addpre_def by (cases v) simp
288
+
289
+ lemma addpre_empty: "addpre r {} = r"
290
+ unfolding addpre_def by simp
291
+
292
+ lemma addpre_r:
293
+ "addpre (dsn, dsk, fl, hops, nhip, pre) npre = (dsn, dsk, fl, hops, nhip, pre \<union> npre)"
294
+ unfolding addpre_def by simp
295
+
296
+ lemmas addpre_simps [simp] = proj2_addpre proj3_addpre proj4_addpre proj5_addpre
297
+ proj6_addpre proj7_addpre addpre_empty addpre_r
298
+
299
+ definition addpreRT :: "rt \<Rightarrow> ip \<Rightarrow> ip set \<rightharpoonup> rt"
300
+ where "addpreRT rt dip npre \<equiv>
301
+ map_option (\<lambda>s. rt (dip \<mapsto> addpre s npre)) (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
302
+
303
+ lemma snd_addpre [simp]:
304
+ "\<And>dsn dsn' v pre. (dsn, snd(addpre (dsn', v) pre)) = addpre (dsn, v) pre"
305
+ unfolding addpre_def by clarsimp
306
+
307
+ lemma proj2_addpreRT [simp]:
308
+ fixes ip rt ip' npre
309
+ assumes "ip\<in>kD rt"
310
+ and "ip'\<in>kD rt"
311
+ shows "\<pi>\<^sub>2(the (the (addpreRT rt ip' npre) ip)) = \<pi>\<^sub>2(the (rt ip))"
312
+ using assms [THEN kD_Some] unfolding addpreRT_def by clarsimp
313
+
314
+ lemma proj3_addpreRT [simp]:
315
+ fixes ip rt ip' npre
316
+ assumes "ip\<in>kD rt"
317
+ and "ip'\<in>kD rt"
318
+ shows "\<pi>\<^sub>3(the (the (addpreRT rt ip' npre) ip)) = \<pi>\<^sub>3(the (rt ip))"
319
+ using assms [THEN kD_Some] unfolding addpreRT_def by clarsimp
320
+
321
+ lemma proj5_addpreRT [simp]:
322
+ "\<And>rt dip ip npre. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>5(the (the (addpreRT rt dip npre) ip)) = \<pi>\<^sub>5(the (rt ip))"
323
+ unfolding addpreRT_def by auto
324
+
325
+ lemma flag_addpreRT [simp]:
326
+ fixes rt pre ip dip
327
+ assumes "dip \<in> kD rt"
328
+ shows "flag (the (addpreRT rt dip pre)) ip = flag rt ip"
329
+ unfolding addpreRT_def
330
+ using assms [THEN kD_Some] by (clarsimp)
331
+
332
+ lemma kD_addpreRT [simp]:
333
+ fixes rt dip npre
334
+ assumes "dip \<in> kD rt"
335
+ shows "kD (the (addpreRT rt dip npre)) = kD rt"
336
+ unfolding kD_def addpreRT_def
337
+ using assms [THEN kD_Some]
338
+ by clarsimp blast
339
+
340
+ lemma vD_addpreRT [simp]:
341
+ fixes rt dip npre
342
+ assumes "dip \<in> kD rt"
343
+ shows "vD (the (addpreRT rt dip npre)) = vD rt"
344
+ unfolding vD_def addpreRT_def
345
+ using assms [THEN kD_Some] by clarsimp auto
346
+
347
+ lemma iD_addpreRT [simp]:
348
+ fixes rt dip npre
349
+ assumes "dip \<in> kD rt"
350
+ shows "iD (the (addpreRT rt dip npre)) = iD rt"
351
+ unfolding iD_def addpreRT_def
352
+ using assms [THEN kD_Some] by clarsimp auto
353
+
354
+ lemma nhop_addpreRT [simp]:
355
+ fixes rt pre ip dip
356
+ assumes "dip \<in> kD rt"
357
+ shows "nhop (the (addpreRT rt dip pre)) ip = nhop rt ip"
358
+ unfolding sqn_def addpreRT_def
359
+ using assms [THEN kD_Some] by (clarsimp)
360
+
361
+ lemma sqn_addpreRT [simp]:
362
+ fixes rt pre ip dip
363
+ assumes "dip \<in> kD rt"
364
+ shows "sqn (the (addpreRT rt dip pre)) ip = sqn rt ip"
365
+ unfolding sqn_def addpreRT_def
366
+ using assms [THEN kD_Some] by (clarsimp)
367
+
368
+ lemma dhops_addpreRT [simp]:
369
+ fixes rt pre ip dip
370
+ assumes "dip \<in> kD rt"
371
+ shows "dhops (the (addpreRT rt dip pre)) ip = dhops rt ip"
372
+ unfolding addpreRT_def
373
+ using assms [THEN kD_Some] by (clarsimp)
374
+
375
+ lemma sqnf_addpreRT [simp]:
376
+ "\<And>ip dip. ip\<in>kD(rt \<xi>) \<Longrightarrow> sqnf (the (addpreRT (rt \<xi>) ip npre)) dip = sqnf (rt \<xi>) dip"
377
+ unfolding sqnf_def addpreRT_def by auto
378
+
379
+ subsubsection "Updating route entries"
380
+
381
+ lemma in_kD_case [simp]:
382
+ fixes dip rt
383
+ assumes "dip \<in> kD(rt)"
384
+ shows "(case rt dip of None \<Rightarrow> en | Some r \<Rightarrow> es r) = es (the (rt dip))"
385
+ using assms [THEN kD_Some] by auto
386
+
387
+ lemma not_in_kD_case [simp]:
388
+ fixes dip rt
389
+ assumes "dip \<notin> kD(rt)"
390
+ shows "(case rt dip of None \<Rightarrow> en | Some r \<Rightarrow> es r) = en"
391
+ using assms [THEN kD_None] by auto
392
+
393
+ lemma rt_Some_sqn [dest]:
394
+ fixes rt and ip dsn dsk flag hops nhip pre
395
+ assumes "rt ip = Some (dsn, dsk, flag, hops, nhip, pre)"
396
+ shows "sqn rt ip = dsn"
397
+ unfolding sqn_def using assms by simp
398
+
399
+ lemma not_kD_sqn [simp]:
400
+ fixes dip rt
401
+ assumes "dip \<notin> kD(rt)"
402
+ shows "sqn rt dip = 0"
403
+ using assms unfolding sqn_def
404
+ by simp
405
+
406
+ definition update_arg_wf :: "r \<Rightarrow> bool"
407
+ where "update_arg_wf r \<equiv> \<pi>\<^sub>4(r) = val \<and>
408
+ (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk) \<and>
409
+ (\<pi>\<^sub>3(r) = unk \<longrightarrow> \<pi>\<^sub>5(r) = 1)"
410
+
411
+ lemma update_arg_wf_gives_cases:
412
+ "\<And>r. update_arg_wf r \<Longrightarrow> (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
413
+ unfolding update_arg_wf_def by simp
414
+
415
+ lemma update_arg_wf_tuples [simp]:
416
+ "\<And>nhip pre. update_arg_wf (0, unk, val, Suc 0, nhip, pre)"
417
+ "\<And>n hops nhip pre. update_arg_wf (Suc n, kno, val, hops, nhip, pre)"
418
+ unfolding update_arg_wf_def by auto
419
+
420
+ lemma update_arg_wf_tuples' [elim]:
421
+ "\<And>n hops nhip pre. Suc 0 \<le> n \<Longrightarrow> update_arg_wf (n, kno, val, hops, nhip, pre)"
422
+ unfolding update_arg_wf_def by auto
423
+
424
+ lemma wf_r_cases [intro]:
425
+ fixes P r
426
+ assumes "update_arg_wf r"
427
+ and c1: "\<And>nhip pre. P (0, unk, val, Suc 0, nhip, pre)"
428
+ and c2: "\<And>dsn hops nhip pre. dsn > 0 \<Longrightarrow> P (dsn, kno, val, hops, nhip, pre)"
429
+ shows "P r"
430
+ proof -
431
+ obtain dsn dsk flag hops nhip pre
432
+ where *: "r = (dsn, dsk, flag, hops, nhip, pre)" by (cases r)
433
+ with \<open>update_arg_wf r\<close> have wf1: "flag = val"
434
+ and wf2: "(dsn = 0) = (dsk = unk)"
435
+ and wf3: "dsk = unk \<longrightarrow> (hops = 1)"
436
+ unfolding update_arg_wf_def by auto
437
+ have "P (dsn, dsk, flag, hops, nhip, pre)"
438
+ proof (cases dsk)
439
+ assume "dsk = unk"
440
+ moreover with wf2 wf3 have "dsn = 0" and "hops = Suc 0" by auto
441
+ ultimately show ?thesis using \<open>flag = val\<close> by simp (rule c1)
442
+ next
443
+ assume "dsk = kno"
444
+ moreover with wf2 have "dsn > 0" by simp
445
+ ultimately show ?thesis using \<open>flag = val\<close> by simp (rule c2)
446
+ qed
447
+ with * show "P r" by simp
448
+ qed
449
+
450
+ definition update :: "rt \<Rightarrow> ip \<Rightarrow> r \<Rightarrow> rt"
451
+ where
452
+ "update rt ip r \<equiv>
453
+ case \<sigma>\<^bsub>route\<^esub>(rt, ip) of
454
+ None \<Rightarrow> rt (ip \<mapsto> r)
455
+ | Some s \<Rightarrow>
456
+ if \<pi>\<^sub>2(s) < \<pi>\<^sub>2(r) then rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(s)))
457
+ else if \<pi>\<^sub>2(s) = \<pi>\<^sub>2(r) \<and> (\<pi>\<^sub>5(s) > \<pi>\<^sub>5(r) \<or> \<pi>\<^sub>4(s) = inv)
458
+ then rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(s)))
459
+ else if \<pi>\<^sub>3(r) = unk
460
+ then rt (ip \<mapsto> (\<pi>\<^sub>2(s), snd (addpre r (\<pi>\<^sub>7(s)))))
461
+ else rt (ip \<mapsto> addpre s (\<pi>\<^sub>7(r)))"
462
+
463
+ lemma update_simps [simp]:
464
+ fixes r s nrt nr nr' ns rt ip
465
+ defines "s \<equiv> the \<sigma>\<^bsub>route\<^esub>(rt, ip)"
466
+ and "nr \<equiv> addpre r (\<pi>\<^sub>7(s))"
467
+ and "nr' \<equiv> (\<pi>\<^sub>2(s), \<pi>\<^sub>3(nr), \<pi>\<^sub>4(nr), \<pi>\<^sub>5(nr), \<pi>\<^sub>6(nr), \<pi>\<^sub>7(nr))"
468
+ and "ns \<equiv> addpre s (\<pi>\<^sub>7(r))"
469
+ shows
470
+ "\<lbrakk>ip \<notin> kD(rt)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> r)"
471
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip < \<pi>\<^sub>2(r)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
472
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r);
473
+ the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
474
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r);
475
+ flag rt ip = Some inv\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
476
+ "\<lbrakk>ip \<in> kD(rt); \<pi>\<^sub>3(r) = unk; (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr')"
477
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
478
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val \<rbrakk>
479
+ \<Longrightarrow> update rt ip r = rt (ip \<mapsto> ns)"
480
+ proof -
481
+ assume "ip\<notin>kD(rt)"
482
+ hence "\<sigma>\<^bsub>route\<^esub>(rt, ip) = None" ..
483
+ thus "update rt ip r = rt (ip \<mapsto> r)"
484
+ unfolding update_def by simp
485
+ next
486
+ assume "ip \<in> kD(rt)"
487
+ and "sqn rt ip < \<pi>\<^sub>2(r)"
488
+ from this(1) obtain dsn dsk fl hops nhip pre
489
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
490
+ by (metis kD_Some)
491
+ with \<open>sqn rt ip < \<pi>\<^sub>2(r)\<close> show "update rt ip r = rt (ip \<mapsto> nr)"
492
+ unfolding update_def nr_def s_def by auto
493
+ next
494
+ assume "ip \<in> kD(rt)"
495
+ and "sqn rt ip = \<pi>\<^sub>2(r)"
496
+ and "the (dhops rt ip) > \<pi>\<^sub>5(r)"
497
+ from this(1) obtain dsn dsk fl hops nhip pre
498
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
499
+ by (metis kD_Some)
500
+ with \<open>sqn rt ip = \<pi>\<^sub>2(r)\<close> and \<open>the (dhops rt ip) > \<pi>\<^sub>5(r)\<close>
501
+ show "update rt ip r = rt (ip \<mapsto> nr)"
502
+ unfolding update_def nr_def s_def by auto
503
+ next
504
+ assume "ip \<in> kD(rt)"
505
+ and "sqn rt ip = \<pi>\<^sub>2(r)"
506
+ and "flag rt ip = Some inv"
507
+ from this(1) obtain dsn dsk fl hops nhip pre
508
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
509
+ by (metis kD_Some)
510
+ with \<open>sqn rt ip = \<pi>\<^sub>2(r)\<close> and \<open>flag rt ip = Some inv\<close>
511
+ show "update rt ip r = rt (ip \<mapsto> nr)"
512
+ unfolding update_def nr_def s_def by auto
513
+ next
514
+ assume "ip \<in> kD(rt)"
515
+ and "\<pi>\<^sub>3(r) = unk"
516
+ and "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
517
+ from this(1) obtain dsn dsk fl hops nhip pre
518
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
519
+ by (metis kD_Some)
520
+ with \<open>(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<close> and \<open>\<pi>\<^sub>3(r) = unk\<close>
521
+ show "update rt ip r = rt (ip \<mapsto> nr')"
522
+ unfolding update_def nr'_def nr_def s_def
523
+ by (cases r) simp
524
+ next
525
+ assume "ip \<in> kD(rt)"
526
+ and otherassms: "sqn rt ip \<ge> \<pi>\<^sub>2(r)"
527
+ "\<pi>\<^sub>3(r) = kno"
528
+ "sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val"
529
+ from this(1) obtain dsn dsk fl hops nhip pre
530
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
531
+ by (metis kD_Some)
532
+ with otherassms show "update rt ip r = rt (ip \<mapsto> ns)"
533
+ unfolding update_def ns_def s_def by auto
534
+ qed
535
+
536
+ lemma update_cases [elim]:
537
+ assumes "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
538
+ and c1: "\<lbrakk>ip \<notin> kD(rt)\<rbrakk> \<Longrightarrow> P (rt (ip \<mapsto> r))"
539
+
540
+ and c2: "\<lbrakk>ip \<in> kD(rt); sqn rt ip < \<pi>\<^sub>2(r)\<rbrakk>
541
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
542
+ and c3: "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r); the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk>
543
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
544
+ and c4: "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r); the (flag rt ip) = inv\<rbrakk>
545
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
546
+ and c5: "\<lbrakk>ip \<in> kD(rt); \<pi>\<^sub>3(r) = unk\<rbrakk>
547
+ \<Longrightarrow> P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r),
548
+ \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r), \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))))"
549
+ and c6: "\<lbrakk>ip \<in> kD(rt); sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
550
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val\<rbrakk>
551
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre (the \<sigma>\<^bsub>route\<^esub>(rt, ip)) (\<pi>\<^sub>7(r))))"
552
+ shows "(P (update rt ip r))"
553
+ proof (cases "ip \<in> kD(rt)")
554
+ assume "ip \<notin> kD(rt)"
555
+ with c1 show ?thesis
556
+ by simp
557
+ next
558
+ assume "ip \<in> kD(rt)"
559
+ moreover then obtain dsn dsk fl hops nhip pre
560
+ where rteq: "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
561
+ by (metis kD_Some)
562
+ moreover obtain dsn' dsk' fl' hops' nhip' pre'
563
+ where req: "r = (dsn', dsk', fl', hops', nhip', pre')"
564
+ by (cases r) metis
565
+ ultimately show ?thesis
566
+ using \<open>(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<close>
567
+ c2 [OF \<open>ip\<in>kD(rt)\<close>]
568
+ c3 [OF \<open>ip\<in>kD(rt)\<close>]
569
+ c4 [OF \<open>ip\<in>kD(rt)\<close>]
570
+ c5 [OF \<open>ip\<in>kD(rt)\<close>]
571
+ c6 [OF \<open>ip\<in>kD(rt)\<close>]
572
+ unfolding update_def sqn_def by auto
573
+ qed
574
+
575
+ lemma update_cases_kD:
576
+ assumes "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
577
+ and "ip \<in> kD(rt)"
578
+ and c2: "sqn rt ip < \<pi>\<^sub>2(r) \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
579
+ and c3: "\<lbrakk>sqn rt ip = \<pi>\<^sub>2(r); the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk>
580
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
581
+ and c4: "\<lbrakk>sqn rt ip = \<pi>\<^sub>2(r); the (flag rt ip) = inv\<rbrakk>
582
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
583
+ and c5: "\<pi>\<^sub>3(r) = unk \<Longrightarrow> P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r),
584
+ \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r),
585
+ \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))))"
586
+ and c6: "\<lbrakk>sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
587
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val\<rbrakk>
588
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre (the \<sigma>\<^bsub>route\<^esub>(rt, ip)) (\<pi>\<^sub>7(r))))"
589
+ shows "(P (update rt ip r))"
590
+ using assms(1) proof (rule update_cases)
591
+ assume "sqn rt ip < \<pi>\<^sub>2(r)"
592
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7(the (rt ip)))))" by (rule c2)
593
+ next
594
+ assume "sqn rt ip = \<pi>\<^sub>2(r)"
595
+ and "the (dhops rt ip) > \<pi>\<^sub>5(r)"
596
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7 (the (rt ip)))))"
597
+ by (rule c3)
598
+ next
599
+ assume "sqn rt ip = \<pi>\<^sub>2(r)"
600
+ and "the (flag rt ip) = inv"
601
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7 (the (rt ip)))))"
602
+ by (rule c4)
603
+ next
604
+ assume "\<pi>\<^sub>3(r) = unk"
605
+ thus "P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r), \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r),
606
+ \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the (rt ip)))))))"
607
+ by (rule c5)
608
+ next
609
+ assume "sqn rt ip \<ge> \<pi>\<^sub>2(r)"
610
+ and "\<pi>\<^sub>3(r) = kno"
611
+ and "sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val"
612
+ thus "P (rt (ip \<mapsto> addpre (the (rt ip)) (\<pi>\<^sub>7(r))))"
613
+ by (rule c6)
614
+ qed (simp add: \<open>ip \<in> kD(rt)\<close>)
615
+
616
+ lemma in_kD_after_update [simp]:
617
+ fixes rt nip dsn dsk flag hops nhip pre
618
+ shows "kD (update rt nip (dsn, dsk, flag, hops, nhip, pre)) = insert nip (kD rt)"
619
+ unfolding update_def
620
+ by (cases "rt nip") auto
621
+
622
+ lemma nhop_of_update [simp]:
623
+ fixes rt dip dsn dsk flag hops nhip
624
+ assumes "rt \<noteq> update rt dip (dsn, dsk, flag, hops, nhip, {})"
625
+ shows "the (nhop (update rt dip (dsn, dsk, flag, hops, nhip, {})) dip) = nhip"
626
+ proof -
627
+ from assms
628
+ have update_neq: "\<And>v. rt dip = Some v \<Longrightarrow>
629
+ update rt dip (dsn, dsk, flag, hops, nhip, {})
630
+ \<noteq> rt(dip \<mapsto> addpre (the (rt dip)) (\<pi>\<^sub>7 (dsn, dsk, flag, hops, nhip, {})))"
631
+ by auto
632
+ show ?thesis
633
+ proof (cases "rt dip = None")
634
+ assume "rt dip = None"
635
+ thus "?thesis" unfolding update_def by clarsimp
636
+ next
637
+ assume "rt dip \<noteq> None"
638
+ then obtain v where "rt dip = Some v" by (metis not_None_eq)
639
+ with update_neq [OF this] show ?thesis
640
+ unfolding update_def by auto
641
+ qed
642
+ qed
643
+
644
+ lemma sqn_if_updated:
645
+ fixes rip v rt ip
646
+ shows "sqn (\<lambda>x. if x = rip then Some v else rt x) ip
647
+ = (if ip = rip then \<pi>\<^sub>2(v) else sqn rt ip)"
648
+ unfolding sqn_def by simp
649
+
650
+ lemma update_sqn [simp]:
651
+ fixes rt dip rip dsn dsk hops nhip pre
652
+ assumes "(dsn = 0) = (dsk = unk)"
653
+ shows "sqn rt dip \<le> sqn (update rt rip (dsn, dsk, val, hops, nhip, pre)) dip"
654
+ proof (rule update_cases)
655
+ show "(\<pi>\<^sub>2 (dsn, dsk, val, hops, nhip, pre) = 0) = (\<pi>\<^sub>3 (dsn, dsk, val, hops, nhip, pre) = unk)"
656
+ by simp (rule assms)
657
+ qed (clarsimp simp: sqn_if_updated sqn_def)+
658
+
659
+ lemma sqn_update_bigger [simp]:
660
+ fixes rt ip ip' dsn dsk flag hops nhip pre
661
+ assumes "1 \<le> hops"
662
+ shows "sqn rt ip \<le> sqn (update rt ip' (dsn, dsk, flag, hops, nhip, pre)) ip"
663
+ using assms unfolding update_def sqn_def
664
+ by (clarsimp split: option.split) auto
665
+
666
+ lemma dhops_update [intro]:
667
+ fixes rt dsn dsk flag hops ip rip nhip pre
668
+ assumes ex: "\<forall>ip\<in>kD rt. the (dhops rt ip) \<ge> 1"
669
+ and ip: "(ip = rip \<and> Suc 0 \<le> hops) \<or> (ip \<noteq> rip \<and> ip\<in>kD rt)"
670
+ shows "Suc 0 \<le> the (dhops (update rt rip (dsn, dsk, flag, hops, nhip, pre)) ip)"
671
+ using ip proof
672
+ assume "ip = rip \<and> Suc 0 \<le> hops" thus ?thesis
673
+ unfolding update_def using ex
674
+ by (cases "rip \<in> kD rt") (drule(1) bspec, auto)
675
+ next
676
+ assume "ip \<noteq> rip \<and> ip\<in>kD rt" thus ?thesis
677
+ using ex unfolding update_def
678
+ by (cases "rip\<in>kD rt") auto
679
+ qed
680
+
681
+ lemma update_another [simp]:
682
+ fixes dip ip rt dsn dsk flag hops nhip pre
683
+ assumes "ip \<noteq> dip"
684
+ shows "(update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = rt ip"
685
+ using assms unfolding update_def
686
+ by (clarsimp split: option.split)
687
+
688
+ lemma nhop_update_another [simp]:
689
+ fixes dip ip rt dsn dsk flag hops nhip pre
690
+ assumes "ip \<noteq> dip"
691
+ shows "nhop (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = nhop rt ip"
692
+ using assms unfolding update_def
693
+ by (clarsimp split: option.split)
694
+
695
+ lemma dhops_update_another [simp]:
696
+ fixes dip ip rt dsn dsk flag hops nhip pre
697
+ assumes "ip \<noteq> dip"
698
+ shows "dhops (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = dhops rt ip"
699
+ using assms unfolding update_def
700
+ by (clarsimp split: option.split)
701
+
702
+ lemma sqn_update_same [simp]:
703
+ "\<And>rt ip dsn dsk flag hops nhip pre. sqn (rt(ip \<mapsto> v)) ip = \<pi>\<^sub>2(v)"
704
+ unfolding sqn_def by simp
705
+
706
+ lemma dhops_update_changed [simp]:
707
+ fixes rt dip osn hops nhip
708
+ assumes "rt \<noteq> update rt dip (osn, kno, val, hops, nhip, {})"
709
+ shows "the (dhops (update rt dip (osn, kno, val, hops, nhip, {})) dip) = hops"
710
+ using assms unfolding update_def
711
+ by (clarsimp split: option.split_asm option.split if_split_asm) auto
712
+
713
+ lemma nhop_update_unk_val [simp]:
714
+ "\<And>rt dip ip dsn hops npre.
715
+ the (nhop (update rt dip (dsn, unk, val, hops, ip, npre)) dip) = ip"
716
+ unfolding update_def by (clarsimp split: option.split)
717
+
718
+ lemma nhop_update_changed [simp]:
719
+ fixes rt dip dsn dsk flg hops sip
720
+ assumes "update rt dip (dsn, dsk, flg, hops, sip, {}) \<noteq> rt"
721
+ shows "the (nhop (update rt dip (dsn, dsk, flg, hops, sip, {})) dip) = sip"
722
+ using assms unfolding update_def
723
+ by (clarsimp split: option.splits if_split_asm) auto
724
+
725
+ lemma update_rt_split_asm:
726
+ "\<And>rt ip dsn dsk flag hops sip.
727
+ P (update rt ip (dsn, dsk, flag, hops, sip, {}))
728
+ =
729
+ (\<not>(rt = update rt ip (dsn, dsk, flag, hops, sip, {}) \<and> \<not>P rt
730
+ \<or> rt \<noteq> update rt ip (dsn, dsk, flag, hops, sip, {})
731
+ \<and> \<not>P (update rt ip (dsn, dsk, flag, hops, sip, {}))))"
732
+ by auto
733
+
734
+ lemma sqn_update [simp]: "\<And>rt dip dsn flg hops sip.
735
+ rt \<noteq> update rt dip (dsn, kno, flg, hops, sip, {})
736
+ \<Longrightarrow> sqn (update rt dip (dsn, kno, flg, hops, sip, {})) dip = dsn"
737
+ unfolding update_def by (clarsimp split: option.split if_split_asm) auto
738
+
739
+ lemma sqnf_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
740
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
741
+ \<Longrightarrow> sqnf (update rt dip (dsn, dsk, flg, hops, sip, {})) dip = dsk"
742
+ unfolding update_def sqnf_def
743
+ by (clarsimp split: option.splits if_split_asm) auto
744
+
745
+ lemma update_kno_dsn_greater_zero:
746
+ "\<And>rt dip ip dsn hops npre. 1 \<le> dsn \<Longrightarrow> 1 \<le> (sqn (update rt dip (dsn, kno, val, hops, ip, npre)) dip)"
747
+ unfolding update_def
748
+ by (clarsimp split: option.splits)
749
+
750
+ lemma proj3_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
751
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
752
+ \<Longrightarrow> \<pi>\<^sub>3(the (update rt dip (dsn, dsk, flg, hops, sip, {}) dip)) = dsk"
753
+ unfolding update_def sqnf_def
754
+ by (clarsimp split: option.splits if_split_asm) auto
755
+
756
+ lemma nhop_update_changed_kno_val [simp]: "\<And>rt ip dsn dsk hops nhip.
757
+ rt \<noteq> update rt ip (dsn, kno, val, hops, nhip, {})
758
+ \<Longrightarrow> the (nhop (update rt ip (dsn, kno, val, hops, nhip, {})) ip) = nhip"
759
+ unfolding update_def
760
+ by (clarsimp split: option.split_asm option.split if_split_asm) auto
761
+
762
+ lemma flag_update [simp]: "\<And>rt dip dsn flg hops sip.
763
+ rt \<noteq> update rt dip (dsn, kno, flg, hops, sip, {})
764
+ \<Longrightarrow> the (flag (update rt dip (dsn, kno, flg, hops, sip, {})) dip) = flg"
765
+ unfolding update_def
766
+ by (clarsimp split: option.split if_split_asm) auto
767
+
768
+ lemma the_flag_Some [dest!]:
769
+ fixes ip rt
770
+ assumes "the (flag rt ip) = x"
771
+ and "ip \<in> kD rt"
772
+ shows "flag rt ip = Some x"
773
+ using assms by auto
774
+
775
+ lemma kD_update_unchanged [dest]:
776
+ fixes rt dip dsn dsk flag hops nhip pre
777
+ assumes "rt = update rt dip (dsn, dsk, flag, hops, nhip, pre)"
778
+ shows "dip\<in>kD(rt)"
779
+ proof -
780
+ have "dip\<in>kD(update rt dip (dsn, dsk, flag, hops, nhip, pre))" by simp
781
+ with assms show ?thesis by simp
782
+ qed
783
+
784
+ lemma nhop_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
785
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
786
+ \<Longrightarrow> the (nhop (update rt dip (dsn, dsk, flg, hops, sip, {})) dip) = sip"
787
+ unfolding update_def sqnf_def
788
+ by (clarsimp split: option.splits if_split_asm) auto
789
+
790
+ lemma sqn_update_another [simp]:
791
+ fixes dip ip rt dsn dsk flag hops nhip pre
792
+ assumes "ip \<noteq> dip"
793
+ shows "sqn (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = sqn rt ip"
794
+ using assms unfolding update_def sqn_def
795
+ by (clarsimp split: option.splits) auto
796
+
797
+ lemma sqnf_update_another [simp]:
798
+ fixes dip ip rt dsn dsk flag hops nhip pre
799
+ assumes "ip \<noteq> dip"
800
+ shows "sqnf (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = sqnf rt ip"
801
+ using assms unfolding update_def sqnf_def
802
+ by (clarsimp split: option.splits) auto
803
+
804
+ lemma vD_update_val [dest]:
805
+ "\<And>dip rt dip' dsn dsk hops nhip pre.
806
+ dip \<in> vD(update rt dip' (dsn, dsk, val, hops, nhip, pre)) \<Longrightarrow> (dip\<in>vD(rt) \<or> dip=dip')"
807
+ unfolding update_def vD_def by (clarsimp split: option.split_asm if_split_asm)
808
+
809
+ subsubsection "Invalidating route entries"
810
+
811
+ definition invalidate :: "rt \<Rightarrow> (ip \<rightharpoonup> sqn) \<Rightarrow> rt"
812
+ where "invalidate rt dests \<equiv>
813
+ \<lambda>ip. case (rt ip, dests ip) of
814
+ (None, _) \<Rightarrow> None
815
+ | (Some s, None) \<Rightarrow> Some s
816
+ | (Some (_, dsk, _, hops, nhip, pre), Some rsn) \<Rightarrow>
817
+ Some (rsn, dsk, inv, hops, nhip, pre)"
818
+
819
+ lemma proj3_invalidate [simp]:
820
+ "\<And>dip. \<pi>\<^sub>3(the ((invalidate rt dests) dip)) = \<pi>\<^sub>3(the (rt dip))"
821
+ unfolding invalidate_def by (clarsimp split: option.split)
822
+
823
+ lemma proj5_invalidate [simp]:
824
+ "\<And>dip. \<pi>\<^sub>5(the ((invalidate rt dests) dip)) = \<pi>\<^sub>5(the (rt dip))"
825
+ unfolding invalidate_def by (clarsimp split: option.split)
826
+
827
+ lemma proj6_invalidate [simp]:
828
+ "\<And>dip. \<pi>\<^sub>6(the ((invalidate rt dests) dip)) = \<pi>\<^sub>6(the (rt dip))"
829
+ unfolding invalidate_def by (clarsimp split: option.split)
830
+
831
+ lemma proj7_invalidate [simp]:
832
+ "\<And>dip. \<pi>\<^sub>7(the ((invalidate rt dests) dip)) = \<pi>\<^sub>7(the (rt dip))"
833
+ unfolding invalidate_def by (clarsimp split: option.split)
834
+
835
+ lemma invalidate_kD_inv [simp]:
836
+ "\<And>rt dests. kD (invalidate rt dests) = kD rt"
837
+ unfolding invalidate_def kD_def
838
+ by (simp split: option.split)
839
+
840
+ lemma invalidate_sqn:
841
+ fixes rt dip dests
842
+ assumes "\<forall>rsn. dests dip = Some rsn \<longrightarrow> sqn rt dip \<le> rsn"
843
+ shows "sqn rt dip \<le> sqn (invalidate rt dests) dip"
844
+ proof (cases "dip \<notin> kD(rt)")
845
+ assume "\<not> dip \<notin> kD(rt)"
846
+ hence "dip\<in>kD(rt)" by simp
847
+ then obtain dsn dsk flag hops nhip pre where "rt dip = Some (dsn, dsk, flag, hops, nhip, pre)"
848
+ by (metis kD_Some)
849
+ with assms show "sqn rt dip \<le> sqn (invalidate rt dests) dip"
850
+ by (cases "dests dip") (auto simp add: invalidate_def sqn_def)
851
+ qed simp
852
+
853
+ lemma sqn_invalidate_in_dests [simp]:
854
+ fixes dests ipa rsn rt
855
+ assumes "dests ipa = Some rsn"
856
+ and "ipa\<in>kD(rt)"
857
+ shows "sqn (invalidate rt dests) ipa = rsn"
858
+ unfolding invalidate_def sqn_def
859
+ using assms(1) assms(2) [THEN kD_Some]
860
+ by clarsimp
861
+
862
+ lemma dhops_invalidate [simp]:
863
+ "\<And>dip. the (dhops (invalidate rt dests) dip) = the (dhops rt dip)"
864
+ unfolding invalidate_def by (clarsimp split: option.split)
865
+
866
+ lemma sqnf_invalidate [simp]:
867
+ "\<And>dip. sqnf (invalidate (rt \<xi>) (dests \<xi>)) dip = sqnf (rt \<xi>) dip"
868
+ unfolding sqnf_def invalidate_def by (clarsimp split: option.split)
869
+
870
+ lemma nhop_invalidate [simp]:
871
+ "\<And>dip. the (nhop (invalidate (rt \<xi>) (dests \<xi>)) dip) = the (nhop (rt \<xi>) dip)"
872
+ unfolding invalidate_def by (clarsimp split: option.split)
873
+
874
+ lemma invalidate_other [simp]:
875
+ fixes rt dests dip
876
+ assumes "dip\<notin>dom(dests)"
877
+ shows "invalidate rt dests dip = rt dip"
878
+ using assms unfolding invalidate_def
879
+ by (clarsimp split: option.split_asm)
880
+
881
+ lemma invalidate_none [simp]:
882
+ fixes rt dests dip
883
+ assumes "dip\<notin>kD(rt)"
884
+ shows "invalidate rt dests dip = None"
885
+ using assms unfolding invalidate_def by clarsimp
886
+
887
+ lemma vD_invalidate_vD_not_dests:
888
+ "\<And>dip rt dests. dip\<in>vD(invalidate rt dests) \<Longrightarrow> dip\<in>vD(rt) \<and> dests dip = None"
889
+ unfolding invalidate_def vD_def
890
+ by (clarsimp split: option.split_asm)
891
+
892
+ lemma sqn_invalidate_not_in_dests [simp]:
893
+ fixes dests dip rt
894
+ assumes "dip\<notin>dom(dests)"
895
+ shows "sqn (invalidate rt dests) dip = sqn rt dip"
896
+ using assms unfolding sqn_def by simp
897
+
898
+ lemma invalidate_changes:
899
+ fixes rt dests dip dsn dsk flag hops nhip pre
900
+ assumes "invalidate rt dests dip = Some (dsn, dsk, flag, hops, nhip, pre)"
901
+ shows " dsn = (case dests dip of None \<Rightarrow> \<pi>\<^sub>2(the (rt dip)) | Some rsn \<Rightarrow> rsn)
902
+ \<and> dsk = \<pi>\<^sub>3(the (rt dip))
903
+ \<and> flag = (if dests dip = None then \<pi>\<^sub>4(the (rt dip)) else inv)
904
+ \<and> hops = \<pi>\<^sub>5(the (rt dip))
905
+ \<and> nhip = \<pi>\<^sub>6(the (rt dip))
906
+ \<and> pre = \<pi>\<^sub>7(the (rt dip))"
907
+ using assms unfolding invalidate_def
908
+ by (cases "rt dip", clarsimp, cases "dests dip") auto
909
+
910
+
911
+ lemma proj3_inv: "\<And>dip rt dests. dip\<in>kD (rt)
912
+ \<Longrightarrow> \<pi>\<^sub>3(the (invalidate rt dests dip)) = \<pi>\<^sub>3(the (rt dip))"
913
+ by (clarsimp simp: invalidate_def kD_def split: option.split)
914
+
915
+ lemma dests_iD_invalidate [simp]:
916
+ assumes "dests ip = Some rsn"
917
+ and "ip\<in>kD(rt)"
918
+ shows "ip\<in>iD(invalidate rt dests)"
919
+ using assms(1) assms(2) [THEN kD_Some] unfolding invalidate_def iD_def
920
+ by (clarsimp split: option.split)
921
+
922
+ subsection "Route Requests"
923
+
924
+ text \<open>Generate a fresh route request identifier.\<close>
925
+
926
+ definition nrreqid :: "(ip \<times> rreqid) set \<Rightarrow> ip \<Rightarrow> rreqid"
927
+ where "nrreqid rreqs ip \<equiv> Max ({n. (ip, n) \<in> rreqs} \<union> {0}) + 1"
928
+
929
+ subsection "Queued Packets"
930
+
931
+ text \<open>Functions for sending data packets.\<close>
932
+
933
+ type_synonym store = "ip \<rightharpoonup> (p \<times> data list)"
934
+
935
+ definition sigma_queue :: "store \<Rightarrow> ip \<Rightarrow> data list" ("\<sigma>\<^bsub>queue\<^esub>'(_, _')")
936
+ where "\<sigma>\<^bsub>queue\<^esub>(store, dip) \<equiv> case store dip of None \<Rightarrow> [] | Some (p, q) \<Rightarrow> q"
937
+
938
+ definition qD :: "store \<Rightarrow> ip set"
939
+ where "qD \<equiv> dom"
940
+
941
+ definition add :: "data \<Rightarrow> ip \<Rightarrow> store \<Rightarrow> store"
942
+ where "add d dip store \<equiv> case store dip of
943
+ None \<Rightarrow> store (dip \<mapsto> (req, [d]))
944
+ | Some (p, q) \<Rightarrow> store (dip \<mapsto> (p, q @ [d]))"
945
+
946
+ lemma qD_add [simp]:
947
+ fixes d dip store
948
+ shows "qD(add d dip store) = insert dip (qD store)"
949
+ unfolding add_def Let_def qD_def
950
+ by (clarsimp split: option.split)
951
+
952
+ definition drop :: "ip \<Rightarrow> store \<rightharpoonup> store"
953
+ where "drop dip store \<equiv>
954
+ map_option (\<lambda>(p, q). if tl q = [] then store (dip := None)
955
+ else store (dip \<mapsto> (p, tl q))) (store dip)"
956
+
957
+ definition sigma_p_flag :: "store \<Rightarrow> ip \<rightharpoonup> p" ("\<sigma>\<^bsub>p-flag\<^esub>'(_, _')")
958
+ where "\<sigma>\<^bsub>p-flag\<^esub>(store, dip) \<equiv> map_option fst (store dip)"
959
+
960
+ definition unsetRRF :: "store \<Rightarrow> ip \<Rightarrow> store"
961
+ where "unsetRRF store dip \<equiv> case store dip of
962
+ None \<Rightarrow> store
963
+ | Some (p, q) \<Rightarrow> store (dip \<mapsto> (noreq, q))"
964
+
965
+ definition setRRF :: "store \<Rightarrow> (ip \<rightharpoonup> sqn) \<Rightarrow> store"
966
+ where "setRRF store dests \<equiv> \<lambda>dip. if dests dip = None then store dip
967
+ else map_option (\<lambda>(_, q). (req, q)) (store dip)"
968
+
969
+ subsection "Comparison with the original technical report"
970
+
971
+ text \<open>
972
+ The major differences with the AODV technical report of Fehnker et al are:
973
+ \begin{enumerate}
974
+ \item @{term nhop} is partial, thus a `@{term the}' is needed, similarly for @{term dhops}
975
+ and @{term addpreRT}.
976
+ \item @{term precs} is partial.
977
+ \item @{term "\<sigma>\<^bsub>p-flag\<^esub>(store, dip)"} is partial.
978
+ \item The routing table (@{typ rt}) is modelled as a map (@{typ "ip \<Rightarrow> r option"})
979
+ rather than a set of 7-tuples, likewise, the @{typ r} is a 6-tuple rather than
980
+ a 7-tuple, i.e., the destination ip-address (@{term "dip"}) is taken from the
981
+ argument to the function, rather than a part of the result. Well-definedness then
982
+ follows from the structure of the type and more related facts are available
983
+ automatically, rather than having to be acquired through tedious proofs.
984
+ \item Similar remarks hold for the dests mapping passed to @{term "invalidate"},
985
+ and @{term "store"}.
986
+ \end{enumerate}
987
+ \<close>
988
+
989
+ end
990
+
formal/afp/AODV/Aodv_Loop_Freedom.thy ADDED
@@ -0,0 +1,369 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Aodv_Loop_Freedom.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Lift and transfer invariants to show loop freedom"
7
+
8
+ theory Aodv_Loop_Freedom
9
+ imports AWN.OClosed_Transfer AWN.Qmsg_Lifting Global_Invariants Loop_Freedom
10
+ begin
11
+
12
+ subsection \<open>Lift to parallel processes with queues\<close>
13
+
14
+ lemma par_step_no_change_on_send_or_receive:
15
+ fixes \<sigma> s a \<sigma>' s'
16
+ assumes "((\<sigma>, s), a, (\<sigma>', s')) \<in> oparp_sos i (oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i) (seqp_sos \<Gamma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G)"
17
+ and "a \<noteq> \<tau>"
18
+ shows "\<sigma>' i = \<sigma> i"
19
+ using assms by (rule qmsg_no_change_on_send_or_receive)
20
+
21
+ lemma par_nhop_quality_increases:
22
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile> (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m.
23
+ msg_fresh \<sigma> m \<and> msg_zhops m)),
24
+ other quality_increases {i} \<rightarrow>)
25
+ global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
26
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
27
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
28
+ proof (rule lift_into_qmsg [OF seq_nhop_quality_increases])
29
+ show "opaodv i \<Turnstile>\<^sub>A (otherwith ((=)) {i}
30
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
31
+ other quality_increases {i} \<rightarrow>)
32
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
33
+ proof (rule ostep_invariant_weakenE [OF oquality_increases], simp_all)
34
+ fix t :: "(((nat \<Rightarrow> state) \<times> (state, msg, pseqp, pseqp label) seqp), msg seq_action) transition"
35
+ assume "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), _, (\<sigma>', _)). \<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)) t"
36
+ thus "quality_increases (fst (fst t) i) (fst (snd (snd t)) i)"
37
+ by (cases t) (clarsimp dest!: onllD, metis aodv_ex_label)
38
+ next
39
+ fix \<sigma> \<sigma>' a
40
+ assume "otherwith ((=)) {i}
41
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)) \<sigma> \<sigma>' a"
42
+ thus "otherwith quality_increases {i} (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma> \<sigma>' a"
43
+ by - (erule weaken_otherwith, auto)
44
+ qed
45
+ qed auto
46
+
47
+ lemma par_rreq_rrep_sn_quality_increases:
48
+ "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
49
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
50
+ proof -
51
+ have "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
52
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
53
+ by (rule ostep_invariant_weakenE [OF olocal_quality_increases])
54
+ (auto dest!: onllD seqllD elim!: aodv_ex_labelE)
55
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
56
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
57
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
58
+ thus ?thesis by rule auto
59
+ qed
60
+
61
+ lemma par_rreq_rrep_nsqn_fresh_any_step:
62
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
63
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
64
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
65
+ proof -
66
+ have "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
67
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
68
+ proof (rule ostep_invariant_weakenE [OF rreq_rrep_nsqn_fresh_any_step_invariant])
69
+ fix t
70
+ assume "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), a, _). anycast (msg_fresh \<sigma>) a) t"
71
+ thus "globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a) t"
72
+ by (cases t) (clarsimp dest!: onllD, metis aodv_ex_label)
73
+ qed auto
74
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
75
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
76
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
77
+ thus ?thesis by rule auto
78
+ qed
79
+
80
+ lemma par_anycast_msg_zhops:
81
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
82
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
83
+ proof -
84
+ from anycast_msg_zhops initiali_aodv oaodv_trans aodv_trans
85
+ have "opaodv i \<Turnstile>\<^sub>A (act TT, other (\<lambda>_ _. True) {i} \<rightarrow>)
86
+ seqll i (onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast msg_zhops a))"
87
+ by (rule open_seq_step_invariant)
88
+ hence "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
89
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
90
+ proof (rule ostep_invariant_weakenE)
91
+ fix t :: "(((nat \<Rightarrow> state) \<times> (state, msg, pseqp, pseqp label) seqp), msg seq_action) transition"
92
+ assume "seqll i (onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast msg_zhops a)) t"
93
+ thus "globala (\<lambda>(_, a, _). anycast msg_zhops a) t"
94
+ by (cases t) (clarsimp dest!: seqllD onllD, metis aodv_ex_label)
95
+ qed simp_all
96
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
97
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
98
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
99
+ thus ?thesis by rule auto
100
+ qed
101
+
102
+ subsection \<open>Lift to nodes\<close>
103
+
104
+ lemma node_step_no_change_on_send_or_receive:
105
+ assumes "((\<sigma>, NodeS i P R), a, (\<sigma>', NodeS i' P' R')) \<in> onode_sos
106
+ (oparp_sos i (oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i) (seqp_sos \<Gamma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G))"
107
+ and "a \<noteq> \<tau>"
108
+ shows "\<sigma>' i = \<sigma> i"
109
+ using assms
110
+ by (cases a) (auto elim!: par_step_no_change_on_send_or_receive)
111
+
112
+ lemma node_nhop_quality_increases:
113
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>
114
+ (otherwith ((=)) {i}
115
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
116
+ other quality_increases {i}
117
+ \<rightarrow>) global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
118
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
119
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
120
+ by (rule node_lift [OF par_nhop_quality_increases]) auto
121
+
122
+ lemma node_quality_increases:
123
+ "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
124
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
125
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
126
+ by (rule node_lift_step_statelessassm [OF par_rreq_rrep_sn_quality_increases]) simp
127
+
128
+ lemma node_rreq_rrep_nsqn_fresh_any_step:
129
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A
130
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
131
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). castmsg (msg_fresh \<sigma>) a)"
132
+ by (rule node_lift_anycast_statelessassm [OF par_rreq_rrep_nsqn_fresh_any_step])
133
+
134
+ lemma node_anycast_msg_zhops:
135
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A
136
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
137
+ globala (\<lambda>(_, a, _). castmsg msg_zhops a)"
138
+ by (rule node_lift_anycast_statelessassm [OF par_anycast_msg_zhops])
139
+
140
+ lemma node_silent_change_only:
141
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i \<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_ _. True) \<sigma>,
142
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
143
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). a \<noteq> \<tau> \<longrightarrow> \<sigma>' i = \<sigma> i)"
144
+ proof (rule ostep_invariantI, simp (no_asm), rule impI)
145
+ fix \<sigma> \<zeta> a \<sigma>' \<zeta>'
146
+ assume or: "(\<sigma>, \<zeta>) \<in> oreachable (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i\<rangle>\<^sub>o)
147
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_ _. True) \<sigma>)
148
+ (other (\<lambda>_ _. True) {i})"
149
+ and tr: "((\<sigma>, \<zeta>), a, (\<sigma>', \<zeta>')) \<in> trans (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i\<rangle>\<^sub>o)"
150
+ and "a \<noteq> \<tau>\<^sub>n"
151
+ from or obtain p R where "\<zeta> = NodeS i p R"
152
+ by - (drule node_net_state, metis)
153
+ with tr have "((\<sigma>, NodeS i p R), a, (\<sigma>', \<zeta>'))
154
+ \<in> onode_sos (oparp_sos i (trans (opaodv i)) (trans qmsg))"
155
+ by simp
156
+ thus "\<sigma>' i = \<sigma> i" using \<open>a \<noteq> \<tau>\<^sub>n\<close>
157
+ by (cases rule: onode_sos.cases)
158
+ (auto elim: qmsg_no_change_on_send_or_receive)
159
+ qed
160
+
161
+ subsection \<open>Lift to partial networks\<close>
162
+
163
+ lemma arrive_rreq_rrep_nsqn_fresh_inc_sn [simp]:
164
+ assumes "oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> P \<sigma> m) \<sigma> m"
165
+ shows "oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma> m"
166
+ using assms by (cases m) auto
167
+
168
+ lemma opnet_nhop_quality_increases:
169
+ shows "opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p \<Turnstile>
170
+ (otherwith ((=)) (net_tree_ips p)
171
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
172
+ other quality_increases (net_tree_ips p) \<rightarrow>)
173
+ global (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips p. \<forall>dip.
174
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
175
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
176
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
177
+ proof (rule pnet_lift [OF node_nhop_quality_increases])
178
+ fix i R
179
+ have "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
180
+ other (\<lambda>_ _. True) {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
181
+ castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a)"
182
+ proof (rule ostep_invariantI, simp (no_asm))
183
+ fix \<sigma> s a \<sigma>' s'
184
+ assume or: "(\<sigma>, s) \<in> oreachable (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o)
185
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>)
186
+ (other (\<lambda>_ _. True) {i})"
187
+ and tr: "((\<sigma>, s), a, (\<sigma>', s')) \<in> trans (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o)"
188
+ and am: "oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma> a"
189
+ from or tr am have "castmsg (msg_fresh \<sigma>) a"
190
+ by (auto dest!: ostep_invariantD [OF node_rreq_rrep_nsqn_fresh_any_step])
191
+ moreover from or tr am have "castmsg (msg_zhops) a"
192
+ by (auto dest!: ostep_invariantD [OF node_anycast_msg_zhops])
193
+ ultimately show "castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a"
194
+ by (case_tac a) auto
195
+ qed
196
+ thus "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
197
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
198
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, _).
199
+ castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a)"
200
+ by rule auto
201
+ next
202
+ fix i R
203
+ show "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
204
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
205
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
206
+ a \<noteq> \<tau> \<and> (\<forall>d. a \<noteq> i:deliver(d)) \<longrightarrow> \<sigma> i = \<sigma>' i)"
207
+ by (rule ostep_invariant_weakenE [OF node_silent_change_only]) auto
208
+ next
209
+ fix i R
210
+ show "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
211
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
212
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
213
+ a = \<tau> \<or> (\<exists>d. a = i:deliver(d)) \<longrightarrow> quality_increases (\<sigma> i) (\<sigma>' i))"
214
+ by (rule ostep_invariant_weakenE [OF node_quality_increases]) auto
215
+ qed simp_all
216
+
217
+ subsection \<open>Lift to closed networks\<close>
218
+
219
+ lemma onet_nhop_quality_increases:
220
+ shows "oclosed (opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p)
221
+ \<Turnstile> (\<lambda>_ _ _. True, other quality_increases (net_tree_ips p) \<rightarrow>)
222
+ global (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips p. \<forall>dip.
223
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
224
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
225
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
226
+ (is "_ \<Turnstile> (_, ?U \<rightarrow>) ?inv")
227
+ proof (rule inclosed_closed)
228
+ from opnet_nhop_quality_increases
229
+ show "opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p
230
+ \<Turnstile> (otherwith ((=)) (net_tree_ips p) inoclosed, ?U \<rightarrow>) ?inv"
231
+ proof (rule oinvariant_weakenE)
232
+ fix \<sigma> \<sigma>' :: "ip \<Rightarrow> state" and a :: "msg node_action"
233
+ assume "otherwith ((=)) (net_tree_ips p) inoclosed \<sigma> \<sigma>' a"
234
+ thus "otherwith ((=)) (net_tree_ips p)
235
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)) \<sigma> \<sigma>' a"
236
+ proof (rule otherwithEI)
237
+ fix \<sigma> :: "ip \<Rightarrow> state" and a :: "msg node_action"
238
+ assume "inoclosed \<sigma> a"
239
+ thus "oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma> a"
240
+ proof (cases a)
241
+ fix ii ni ms
242
+ assume "a = ii\<not>ni:arrive(ms)"
243
+ moreover with \<open>inoclosed \<sigma> a\<close> obtain d di where "ms = newpkt(d, di)"
244
+ by (cases ms) auto
245
+ ultimately show ?thesis by simp
246
+ qed simp_all
247
+ qed
248
+ qed
249
+ qed
250
+
251
+ subsection \<open>Transfer into the standard model\<close>
252
+
253
+ interpretation aodv_openproc: openproc paodv opaodv id
254
+ rewrites "aodv_openproc.initmissing = initmissing"
255
+ proof -
256
+ show "openproc paodv opaodv id"
257
+ proof unfold_locales
258
+ fix i :: ip
259
+ have "{(\<sigma>, \<zeta>). (\<sigma> i, \<zeta>) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<and> (\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j \<in> fst ` \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V j)} \<subseteq> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'"
260
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def
261
+ proof (rule equalityD1)
262
+ show "\<And>f p. {(\<sigma>, \<zeta>). (\<sigma> i, \<zeta>) \<in> {(f i, p)} \<and> (\<forall>j. j \<noteq> i
263
+ \<longrightarrow> \<sigma> j \<in> fst ` {(f j, p)})} = {(f, p)}"
264
+ by (rule set_eqI) auto
265
+ qed
266
+ thus "{ (\<sigma>, \<zeta>) |\<sigma> \<zeta> s. s \<in> init (paodv i)
267
+ \<and> (\<sigma> i, \<zeta>) = id s
268
+ \<and> (\<forall>j. j\<noteq>i \<longrightarrow> \<sigma> j \<in> (fst o id) ` init (paodv j)) } \<subseteq> init (opaodv i)"
269
+ by simp
270
+ next
271
+ show "\<forall>j. init (paodv j) \<noteq> {}"
272
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
273
+ next
274
+ fix i s a s' \<sigma> \<sigma>'
275
+ assume "\<sigma> i = fst (id s)"
276
+ and "\<sigma>' i = fst (id s')"
277
+ and "(s, a, s') \<in> trans (paodv i)"
278
+ then obtain q q' where "s = (\<sigma> i, q)"
279
+ and "s' = (\<sigma>' i, q')"
280
+ and "((\<sigma> i, q), a, (\<sigma>' i, q')) \<in> trans (paodv i)"
281
+ by (cases s, cases s') auto
282
+ from this(3) have "((\<sigma>, q), a, (\<sigma>', q')) \<in> trans (opaodv i)"
283
+ by simp (rule open_seqp_action [OF aodv_wf])
284
+
285
+ with \<open>s = (\<sigma> i, q)\<close> and \<open>s' = (\<sigma>' i, q')\<close>
286
+ show "((\<sigma>, snd (id s)), a, (\<sigma>', snd (id s'))) \<in> trans (opaodv i)"
287
+ by simp
288
+ qed
289
+ then interpret opn: openproc paodv opaodv id .
290
+ have [simp]: "\<And>i. (SOME x. x \<in> (fst o id) ` init (paodv i)) = aodv_init i"
291
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
292
+ hence "\<And>i. openproc.initmissing paodv id i = initmissing i"
293
+ unfolding opn.initmissing_def opn.someinit_def initmissing_def
294
+ by (auto split: option.split)
295
+ thus "openproc.initmissing paodv id = initmissing" ..
296
+ qed
297
+
298
+ interpretation aodv_openproc_par_qmsg: openproc_parq paodv opaodv id qmsg
299
+ rewrites "aodv_openproc_par_qmsg.netglobal = netglobal"
300
+ and "aodv_openproc_par_qmsg.initmissing = initmissing"
301
+ proof -
302
+ show "openproc_parq paodv opaodv id qmsg"
303
+ by (unfold_locales) simp
304
+ then interpret opq: openproc_parq paodv opaodv id qmsg .
305
+
306
+ have im: "\<And>\<sigma>. openproc.initmissing (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) \<sigma>
307
+ = initmissing \<sigma>"
308
+ unfolding opq.initmissing_def opq.someinit_def initmissing_def
309
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def by (clarsimp cong: option.case_cong)
310
+ thus "openproc.initmissing (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) = initmissing"
311
+ by (rule ext)
312
+ have "\<And>P \<sigma>. openproc.netglobal (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) P \<sigma>
313
+ = netglobal P \<sigma>"
314
+ unfolding opq.netglobal_def netglobal_def opq.initmissing_def initmissing_def opq.someinit_def
315
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def
316
+ by (clarsimp cong: option.case_cong
317
+ simp del: One_nat_def
318
+ simp add: fst_initmissing_netgmap_default_aodv_init_netlift
319
+ [symmetric, unfolded initmissing_def])
320
+ thus "openproc.netglobal (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) = netglobal"
321
+ by auto
322
+ qed
323
+
324
+ lemma net_nhop_quality_increases:
325
+ assumes "wf_net_tree n"
326
+ shows "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal
327
+ (\<lambda>\<sigma>. \<forall>i dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
328
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
329
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
330
+ (is "_ \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>i. ?inv \<sigma> i)")
331
+ proof -
332
+ from \<open>wf_net_tree n\<close>
333
+ have proto: "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips n. \<forall>dip.
334
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
335
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
336
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
337
+ by (rule aodv_openproc_par_qmsg.close_opnet [OF _ onet_nhop_quality_increases])
338
+ show ?thesis
339
+ unfolding invariant_def opnet_sos.opnet_tau1
340
+ proof (rule, simp only: aodv_openproc_par_qmsg.netglobalsimp
341
+ fst_initmissing_netgmap_pair_fst, rule allI)
342
+ fix \<sigma> i
343
+ assume sr: "\<sigma> \<in> reachable (closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n)) TT"
344
+ hence "\<forall>i\<in>net_tree_ips n. ?inv (fst (initmissing (netgmap fst \<sigma>))) i"
345
+ by - (drule invariantD [OF proto],
346
+ simp only: aodv_openproc_par_qmsg.netglobalsimp
347
+ fst_initmissing_netgmap_pair_fst)
348
+ thus "?inv (fst (initmissing (netgmap fst \<sigma>))) i"
349
+ proof (cases "i\<in>net_tree_ips n")
350
+ assume "i\<notin>net_tree_ips n"
351
+ from sr have "\<sigma> \<in> reachable (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) TT" ..
352
+ hence "net_ips \<sigma> = net_tree_ips n" ..
353
+ with \<open>i\<notin>net_tree_ips n\<close> have "i\<notin>net_ips \<sigma>" by simp
354
+ hence "(fst (initmissing (netgmap fst \<sigma>))) i = aodv_init i"
355
+ by simp
356
+ thus ?thesis by simp
357
+ qed metis
358
+ qed
359
+ qed
360
+
361
+ subsection \<open>Loop freedom of AODV\<close>
362
+
363
+ theorem aodv_loop_freedom:
364
+ assumes "wf_net_tree n"
365
+ shows "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>dip. irrefl ((rt_graph \<sigma> dip)\<^sup>+))"
366
+ using assms by (rule aodv_openproc_par_qmsg.netglobal_weakenE
367
+ [OF net_nhop_quality_increases inv_to_loop_freedom])
368
+
369
+ end
formal/afp/AODV/Aodv_Message.thy ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Aodv_Message.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "AODV protocol messages"
7
+
8
+ theory Aodv_Message
9
+ imports Aodv_Basic
10
+ begin
11
+
12
+ datatype msg =
13
+ Rreq nat rreqid ip sqn k ip sqn ip
14
+ | Rrep nat ip sqn ip ip
15
+ | Rerr "ip \<rightharpoonup> sqn" ip
16
+ | Newpkt data ip
17
+ | Pkt data ip ip
18
+
19
+ instantiation msg :: msg
20
+ begin
21
+ definition newpkt_def [simp]: "newpkt \<equiv> \<lambda>(d, dip). Newpkt d dip"
22
+ definition eq_newpkt_def: "eq_newpkt m \<equiv> case m of Newpkt d dip \<Rightarrow> True | _ \<Rightarrow> False"
23
+
24
+ instance by intro_classes (simp add: eq_newpkt_def)
25
+ end
26
+
27
+ text \<open>The @{type msg} type models the different messages used within AODV.
28
+ The instantiation as a @{class msg} is a technicality due to the special
29
+ treatment of @{term newpkt} messages in the AWN SOS rules.
30
+ This use of classes allows a clean separation of the AWN-specific definitions
31
+ and these AODV-specific definitions.\<close>
32
+
33
+ definition rreq :: "nat \<times> rreqid \<times> ip \<times> sqn \<times> k \<times> ip \<times> sqn \<times> ip \<Rightarrow> msg"
34
+ where "rreq \<equiv> \<lambda>(hops, rreqid, dip, dsn, dsk, oip, osn, sip).
35
+ Rreq hops rreqid dip dsn dsk oip osn sip"
36
+
37
+ lemma rreq_simp [simp]:
38
+ "rreq(hops, rreqid, dip, dsn, dsk, oip, osn, sip) = Rreq hops rreqid dip dsn dsk oip osn sip"
39
+ unfolding rreq_def by simp
40
+
41
+ definition rrep :: "nat \<times> ip \<times> sqn \<times> ip \<times> ip \<Rightarrow> msg"
42
+ where "rrep \<equiv> \<lambda>(hops, dip, dsn, oip, sip). Rrep hops dip dsn oip sip"
43
+
44
+ lemma rrep_simp [simp]:
45
+ "rrep(hops, dip, dsn, oip, sip) = Rrep hops dip dsn oip sip"
46
+ unfolding rrep_def by simp
47
+
48
+ definition rerr :: "(ip \<rightharpoonup> sqn) \<times> ip \<Rightarrow> msg"
49
+ where "rerr \<equiv> \<lambda>(dests, sip). Rerr dests sip"
50
+
51
+ lemma rerr_simp [simp]:
52
+ "rerr(dests, sip) = Rerr dests sip"
53
+ unfolding rerr_def by simp
54
+
55
+ lemma not_eq_newpkt_rreq [simp]: "\<not>eq_newpkt (Rreq hops rreqid dip dsn dsk oip osn sip)"
56
+ unfolding eq_newpkt_def by simp
57
+
58
+ lemma not_eq_newpkt_rrep [simp]: "\<not>eq_newpkt (Rrep hops dip dsn oip sip)"
59
+ unfolding eq_newpkt_def by simp
60
+
61
+ lemma not_eq_newpkt_rerr [simp]: "\<not>eq_newpkt (Rerr dests sip)"
62
+ unfolding eq_newpkt_def by simp
63
+
64
+ lemma not_eq_newpkt_pkt [simp]: "\<not>eq_newpkt (Pkt d dip sip)"
65
+ unfolding eq_newpkt_def by simp
66
+
67
+ definition pkt :: "data \<times> ip \<times> ip \<Rightarrow> msg"
68
+ where "pkt \<equiv> \<lambda>(d, dip, sip). Pkt d dip sip"
69
+
70
+ lemma pkt_simp [simp]:
71
+ "pkt(d, dip, sip) = Pkt d dip sip"
72
+ unfolding pkt_def by simp
73
+
74
+ end
formal/afp/AODV/Aodv_Predicates.thy ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Aodv_Predicates.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Invariant assumptions and properties"
7
+
8
+ theory Aodv_Predicates
9
+ imports Aodv
10
+ begin
11
+
12
+ text \<open>Definitions for expression assumptions on incoming messages and properties of
13
+ outgoing messages.\<close>
14
+
15
+ abbreviation not_Pkt :: "msg \<Rightarrow> bool"
16
+ where "not_Pkt m \<equiv> case m of Pkt _ _ _ \<Rightarrow> False | _ \<Rightarrow> True"
17
+
18
+ definition msg_sender :: "msg \<Rightarrow> ip"
19
+ where "msg_sender m \<equiv> case m of Rreq _ _ _ _ _ _ _ ipc \<Rightarrow> ipc
20
+ | Rrep _ _ _ _ ipc \<Rightarrow> ipc
21
+ | Rerr _ ipc \<Rightarrow> ipc
22
+ | Pkt _ _ ipc \<Rightarrow> ipc"
23
+
24
+ lemma msg_sender_simps [simp]:
25
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
26
+ msg_sender (Rreq hops rreqid dip dsn dsk oip osn sip) = sip"
27
+ "\<And>hops dip dsn oip sip. msg_sender (Rrep hops dip dsn oip sip) = sip"
28
+ "\<And>dests sip. msg_sender (Rerr dests sip) = sip"
29
+ "\<And>d dip sip. msg_sender (Pkt d dip sip) = sip"
30
+ unfolding msg_sender_def by simp_all
31
+
32
+ definition msg_zhops :: "msg \<Rightarrow> bool"
33
+ where "msg_zhops m \<equiv> case m of
34
+ Rreq hopsc _ dipc _ _ oipc _ sipc \<Rightarrow> hopsc = 0 \<longrightarrow> oipc = sipc
35
+ | Rrep hopsc dipc _ _ sipc \<Rightarrow> hopsc = 0 \<longrightarrow> dipc = sipc
36
+ | _ \<Rightarrow> True"
37
+
38
+ lemma msg_zhops_simps [simp]:
39
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
40
+ msg_zhops (Rreq hops rreqid dip dsn dsk oip osn sip) = (hops = 0 \<longrightarrow> oip = sip)"
41
+ "\<And>hops dip dsn oip sip. msg_zhops (Rrep hops dip dsn oip sip) = (hops = 0 \<longrightarrow> dip = sip)"
42
+ "\<And>dests sip. msg_zhops (Rerr dests sip) = True"
43
+ "\<And>d dip. msg_zhops (Newpkt d dip) = True"
44
+ "\<And>d dip sip. msg_zhops (Pkt d dip sip) = True"
45
+ unfolding msg_zhops_def by simp_all
46
+
47
+ definition rreq_rrep_sn :: "msg \<Rightarrow> bool"
48
+ where "rreq_rrep_sn m \<equiv> case m of Rreq _ _ _ _ _ _ osnc _ \<Rightarrow> osnc \<ge> 1
49
+ | Rrep _ _ dsnc _ _ \<Rightarrow> dsnc \<ge> 1
50
+ | _ \<Rightarrow> True"
51
+
52
+ lemma rreq_rrep_sn_simps [simp]:
53
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
54
+ rreq_rrep_sn (Rreq hops rreqid dip dsn dsk oip osn sip) = (osn \<ge> 1)"
55
+ "\<And>hops dip dsn oip sip. rreq_rrep_sn (Rrep hops dip dsn oip sip) = (dsn \<ge> 1)"
56
+ "\<And>dests sip. rreq_rrep_sn (Rerr dests sip) = True"
57
+ "\<And>d dip. rreq_rrep_sn (Newpkt d dip) = True"
58
+ "\<And>d dip sip. rreq_rrep_sn (Pkt d dip sip) = True"
59
+ unfolding rreq_rrep_sn_def by simp_all
60
+
61
+ definition rreq_rrep_fresh :: "rt \<Rightarrow> msg \<Rightarrow> bool"
62
+ where "rreq_rrep_fresh crt m \<equiv> case m of Rreq hopsc _ _ _ _ oipc osnc ipcc \<Rightarrow> (ipcc \<noteq> oipc \<longrightarrow>
63
+ oipc\<in>kD(crt) \<and> (sqn crt oipc > osnc
64
+ \<or> (sqn crt oipc = osnc
65
+ \<and> the (dhops crt oipc) \<le> hopsc
66
+ \<and> the (flag crt oipc) = val)))
67
+ | Rrep hopsc dipc dsnc _ ipcc \<Rightarrow> (ipcc \<noteq> dipc \<longrightarrow>
68
+ dipc\<in>kD(crt)
69
+ \<and> sqn crt dipc = dsnc
70
+ \<and> the (dhops crt dipc) = hopsc
71
+ \<and> the (flag crt dipc) = val)
72
+ | _ \<Rightarrow> True"
73
+
74
+ lemma rreq_rrep_fresh [simp]:
75
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
76
+ rreq_rrep_fresh crt (Rreq hops rreqid dip dsn dsk oip osn sip) =
77
+ (sip \<noteq> oip \<longrightarrow> oip\<in>kD(crt)
78
+ \<and> (sqn crt oip > osn
79
+ \<or> (sqn crt oip = osn
80
+ \<and> the (dhops crt oip) \<le> hops
81
+ \<and> the (flag crt oip) = val)))"
82
+ "\<And>hops dip dsn oip sip. rreq_rrep_fresh crt (Rrep hops dip dsn oip sip) =
83
+ (sip \<noteq> dip \<longrightarrow> dip\<in>kD(crt)
84
+ \<and> sqn crt dip = dsn
85
+ \<and> the (dhops crt dip) = hops
86
+ \<and> the (flag crt dip) = val)"
87
+ "\<And>dests sip. rreq_rrep_fresh crt (Rerr dests sip) = True"
88
+ "\<And>d dip. rreq_rrep_fresh crt (Newpkt d dip) = True"
89
+ "\<And>d dip sip. rreq_rrep_fresh crt (Pkt d dip sip) = True"
90
+ unfolding rreq_rrep_fresh_def by simp_all
91
+
92
+ definition rerr_invalid :: "rt \<Rightarrow> msg \<Rightarrow> bool"
93
+ where "rerr_invalid crt m \<equiv> case m of Rerr destsc _ \<Rightarrow> (\<forall>ripc\<in>dom(destsc).
94
+ (ripc\<in>iD(crt) \<and> the (destsc ripc) = sqn crt ripc))
95
+ | _ \<Rightarrow> True"
96
+
97
+ lemma rerr_invalid [simp]:
98
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
99
+ rerr_invalid crt (Rreq hops rreqid dip dsn dsk oip osn sip) = True"
100
+ "\<And>hops dip dsn oip sip. rerr_invalid crt (Rrep hops dip dsn oip sip) = True"
101
+ "\<And>dests sip. rerr_invalid crt (Rerr dests sip) = (\<forall>rip\<in>dom(dests).
102
+ rip\<in>iD(crt) \<and> the (dests rip) = sqn crt rip)"
103
+ "\<And>d dip. rerr_invalid crt (Newpkt d dip) = True"
104
+ "\<And>d dip sip. rerr_invalid crt (Pkt d dip sip) = True"
105
+ unfolding rerr_invalid_def by simp_all
106
+
107
+ definition
108
+ initmissing :: "(nat \<Rightarrow> state option) \<times> 'a \<Rightarrow> (nat \<Rightarrow> state) \<times> 'a"
109
+ where
110
+ "initmissing \<sigma> = (\<lambda>i. case (fst \<sigma>) i of None \<Rightarrow> aodv_init i | Some s \<Rightarrow> s, snd \<sigma>)"
111
+
112
+ lemma not_in_net_ips_fst_init_missing [simp]:
113
+ assumes "i \<notin> net_ips \<sigma>"
114
+ shows "fst (initmissing (netgmap fst \<sigma>)) i = aodv_init i"
115
+ using assms unfolding initmissing_def by simp
116
+
117
+ lemma fst_initmissing_netgmap_pair_fst [simp]:
118
+ "fst (initmissing (netgmap (\<lambda>(p, q). (fst (id p), snd (id p), q)) s))
119
+ = fst (initmissing (netgmap fst s))"
120
+ unfolding initmissing_def by auto
121
+
122
+ text \<open>We introduce a streamlined alternative to @{term initmissing} with @{term netgmap}
123
+ to simplify invariant statements and thus facilitate their comprehension and
124
+ presentation.\<close>
125
+
126
+ lemma fst_initmissing_netgmap_default_aodv_init_netlift:
127
+ "fst (initmissing (netgmap fst s)) = default aodv_init (netlift fst s)"
128
+ unfolding initmissing_def default_def
129
+ by (simp add: fst_netgmap_netlift del: One_nat_def)
130
+
131
+ definition
132
+ netglobal :: "((nat \<Rightarrow> state) \<Rightarrow> bool) \<Rightarrow> ((state \<times> 'b) \<times> 'c) net_state \<Rightarrow> bool"
133
+ where
134
+ "netglobal P \<equiv> (\<lambda>s. P (default aodv_init (netlift fst s)))"
135
+
136
+ end
formal/afp/AODV/Fresher.thy ADDED
@@ -0,0 +1,798 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Fresher.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Quality relations between routes"
7
+
8
+ theory Fresher
9
+ imports Aodv_Data
10
+ begin
11
+
12
+ subsection "Net sequence numbers"
13
+
14
+ subsubsection "On individual routes"
15
+
16
+ definition
17
+ nsqn\<^sub>r :: "r \<Rightarrow> sqn"
18
+ where
19
+ "nsqn\<^sub>r r \<equiv> if \<pi>\<^sub>4(r) = val \<or> \<pi>\<^sub>2(r) = 0 then \<pi>\<^sub>2(r) else (\<pi>\<^sub>2(r) - 1)"
20
+
21
+ lemma nsqnr_def':
22
+ "nsqn\<^sub>r r = (if \<pi>\<^sub>4(r) = inv then \<pi>\<^sub>2(r) - 1 else \<pi>\<^sub>2(r))"
23
+ unfolding nsqn\<^sub>r_def by simp
24
+
25
+ lemma nsqn\<^sub>r_zero [simp]:
26
+ "\<And>dsn dsk flag hops nhip pre. nsqn\<^sub>r (0, dsk, flag, hops, nhip, pre) = 0"
27
+ unfolding nsqn\<^sub>r_def by clarsimp
28
+
29
+ lemma nsqn\<^sub>r_val [simp]:
30
+ "\<And>dsn dsk hops nhip pre. nsqn\<^sub>r (dsn, dsk, val, hops, nhip, pre) = dsn"
31
+ unfolding nsqn\<^sub>r_def by clarsimp
32
+
33
+ lemma nsqn\<^sub>r_inv [simp]:
34
+ "\<And>dsn dsk hops nhip pre. nsqn\<^sub>r (dsn, dsk, inv, hops, nhip, pre) = dsn - 1"
35
+ unfolding nsqn\<^sub>r_def by clarsimp
36
+
37
+ lemma nsqn\<^sub>r_lte_dsn [simp]:
38
+ "\<And>dsn dsk flag hops nhip pre. nsqn\<^sub>r (dsn, dsk, flag, hops, nhip, pre) \<le> dsn"
39
+ unfolding nsqn\<^sub>r_def by clarsimp
40
+
41
+ subsubsection "On routes in routing tables"
42
+
43
+ definition
44
+ nsqn :: "rt \<Rightarrow> ip \<Rightarrow> sqn"
45
+ where
46
+ "nsqn \<equiv> \<lambda>rt dip. case \<sigma>\<^bsub>route\<^esub>(rt, dip) of None \<Rightarrow> 0 | Some r \<Rightarrow> nsqn\<^sub>r(r)"
47
+
48
+ lemma nsqn_sqn_def:
49
+ "\<And>rt dip. nsqn rt dip = (if flag rt dip = Some val \<or> sqn rt dip = 0
50
+ then sqn rt dip else sqn rt dip - 1)"
51
+ unfolding nsqn_def sqn_def by (clarsimp split: option.split)
52
+
53
+ lemma not_in_kD_nsqn [simp]:
54
+ assumes "dip \<notin> kD(rt)"
55
+ shows "nsqn rt dip = 0"
56
+ using assms unfolding nsqn_def by simp
57
+
58
+ lemma kD_nsqn:
59
+ assumes "dip \<in> kD(rt)"
60
+ shows "nsqn rt dip = nsqn\<^sub>r(the (\<sigma>\<^bsub>route\<^esub>(rt, dip)))"
61
+ using assms [THEN kD_Some] unfolding nsqn_def by clarsimp
62
+
63
+ lemma nsqnr_r_flag_pred [simp, intro]:
64
+ fixes dsn dsk flag hops nhip pre
65
+ assumes "P (nsqn\<^sub>r (dsn, dsk, val, hops, nhip, pre))"
66
+ and "P (nsqn\<^sub>r (dsn, dsk, inv, hops, nhip, pre))"
67
+ shows "P (nsqn\<^sub>r (dsn, dsk, flag, hops, nhip, pre))"
68
+ using assms by (cases flag) auto
69
+
70
+ lemma nsqn\<^sub>r_addpreRT_inv [simp]:
71
+ "\<And>rt dip npre dip'. dip \<in> kD(rt) \<Longrightarrow>
72
+ nsqn\<^sub>r (the (the (addpreRT rt dip npre) dip')) = nsqn\<^sub>r (the (rt dip'))"
73
+ unfolding addpreRT_def nsqn\<^sub>r_def
74
+ by (frule kD_Some) (clarsimp split: option.split)
75
+
76
+ lemma sqn_nsqn:
77
+ "\<And>rt dip. sqn rt dip - 1 \<le> nsqn rt dip"
78
+ unfolding sqn_def nsqn_def by (clarsimp split: option.split)
79
+
80
+ lemma nsqn_sqn: "nsqn rt dip \<le> sqn rt dip"
81
+ unfolding sqn_def nsqn_def by (cases "rt dip") auto
82
+
83
+ lemma val_nsqn_sqn [elim, simp]:
84
+ assumes "ip\<in>kD(rt)"
85
+ and "the (flag rt ip) = val"
86
+ shows "nsqn rt ip = sqn rt ip"
87
+ using assms unfolding nsqn_sqn_def by auto
88
+
89
+ lemma vD_nsqn_sqn [elim, simp]:
90
+ assumes "ip\<in>vD(rt)"
91
+ shows "nsqn rt ip = sqn rt ip"
92
+ proof -
93
+ from \<open>ip\<in>vD(rt)\<close> have "ip\<in>kD(rt)"
94
+ and "the (flag rt ip) = val" by auto
95
+ thus ?thesis ..
96
+ qed
97
+
98
+ lemma inv_nsqn_sqn [elim, simp]:
99
+ assumes "ip\<in>kD(rt)"
100
+ and "the (flag rt ip) = inv"
101
+ shows "nsqn rt ip = sqn rt ip - 1"
102
+ using assms unfolding nsqn_sqn_def by auto
103
+
104
+ lemma iD_nsqn_sqn [elim, simp]:
105
+ assumes "ip\<in>iD(rt)"
106
+ shows "nsqn rt ip = sqn rt ip - 1"
107
+ proof -
108
+ from \<open>ip\<in>iD(rt)\<close> have "ip\<in>kD(rt)"
109
+ and "the (flag rt ip) = inv" by auto
110
+ thus ?thesis ..
111
+ qed
112
+
113
+ lemma nsqn_update_changed_kno_val [simp]: "\<And>rt ip dsn dsk hops nhip.
114
+ rt \<noteq> update rt ip (dsn, kno, val, hops, nhip, {})
115
+ \<Longrightarrow> nsqn (update rt ip (dsn, kno, val, hops, nhip, {})) ip = dsn"
116
+ unfolding nsqn\<^sub>r_def update_def
117
+ by (clarsimp simp: kD_nsqn split: option.split_asm option.split if_split_asm)
118
+ (metis fun_upd_triv)
119
+
120
+ lemma nsqn_addpreRT_inv [simp]:
121
+ "\<And>rt dip npre dip'. dip \<in> kD(rt) \<Longrightarrow>
122
+ nsqn (the (addpreRT rt dip npre)) dip' = nsqn rt dip'"
123
+ unfolding addpreRT_def nsqn_def nsqn\<^sub>r_def
124
+ by (frule kD_Some) (clarsimp split: option.split)
125
+
126
+ lemma nsqn_update_other [simp]:
127
+ fixes dsn dsk flag hops dip nhip pre rt ip
128
+ assumes "dip \<noteq> ip"
129
+ shows "nsqn (update rt ip (dsn, dsk, flag, hops, nhip, pre)) dip = nsqn rt dip"
130
+ using assms unfolding nsqn_def
131
+ by (clarsimp split: option.split)
132
+
133
+ lemma nsqn_invalidate_eq:
134
+ assumes "dip \<in> kD(rt)"
135
+ and "dests dip = Some rsn"
136
+ shows "nsqn (invalidate rt dests) dip = rsn - 1"
137
+ using assms
138
+ proof -
139
+ from assms obtain dsk hops nhip pre
140
+ where "invalidate rt dests dip = Some (rsn, dsk, inv, hops, nhip, pre)"
141
+ unfolding invalidate_def by auto
142
+ moreover from \<open>dip \<in> kD(rt)\<close> have "dip \<in> kD(invalidate rt dests)" by simp
143
+ ultimately show ?thesis
144
+ using \<open>dests dip = Some rsn\<close> by simp
145
+ qed
146
+
147
+ lemma nsqn_invalidate_other [simp]:
148
+ assumes "dip\<in>kD(rt)"
149
+ and "dip\<notin>dom dests"
150
+ shows "nsqn (invalidate rt dests) dip = nsqn rt dip"
151
+ using assms by (clarsimp simp add: kD_nsqn)
152
+
153
+ subsection "Comparing routes "
154
+
155
+ definition
156
+ fresher :: "r \<Rightarrow> r \<Rightarrow> bool" ("(_/ \<sqsubseteq> _)" [51, 51] 50)
157
+ where
158
+ "fresher r r' \<equiv> ((nsqn\<^sub>r r < nsqn\<^sub>r r') \<or> (nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r')))"
159
+
160
+ lemma fresherI1 [intro]:
161
+ assumes "nsqn\<^sub>r r < nsqn\<^sub>r r'"
162
+ shows "r \<sqsubseteq> r'"
163
+ unfolding fresher_def using assms by simp
164
+
165
+ lemma fresherI2 [intro]:
166
+ assumes "nsqn\<^sub>r r = nsqn\<^sub>r r'"
167
+ and "\<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r')"
168
+ shows "r \<sqsubseteq> r'"
169
+ unfolding fresher_def using assms by simp
170
+
171
+ lemma fresherI [intro]:
172
+ assumes "(nsqn\<^sub>r r < nsqn\<^sub>r r') \<or> (nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r'))"
173
+ shows "r \<sqsubseteq> r'"
174
+ unfolding fresher_def using assms .
175
+
176
+ lemma fresherE [elim]:
177
+ assumes "r \<sqsubseteq> r'"
178
+ and "nsqn\<^sub>r r < nsqn\<^sub>r r' \<Longrightarrow> P r r'"
179
+ and "nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r') \<Longrightarrow> P r r'"
180
+ shows "P r r'"
181
+ using assms unfolding fresher_def by auto
182
+
183
+ lemma fresher_refl [simp]: "r \<sqsubseteq> r"
184
+ unfolding fresher_def by simp
185
+
186
+ lemma fresher_trans [elim, trans]:
187
+ "\<lbrakk> x \<sqsubseteq> y; y \<sqsubseteq> z \<rbrakk> \<Longrightarrow> x \<sqsubseteq> z"
188
+ unfolding fresher_def by auto
189
+
190
+ lemma not_fresher_trans [elim, trans]:
191
+ "\<lbrakk> \<not>(x \<sqsubseteq> y); \<not>(z \<sqsubseteq> x) \<rbrakk> \<Longrightarrow> \<not>(z \<sqsubseteq> y)"
192
+ unfolding fresher_def by auto
193
+
194
+ lemma fresher_dsn_flag_hops_const [simp]:
195
+ fixes dsn dsk dsk' flag hops nhip nhip' pre pre'
196
+ shows "(dsn, dsk, flag, hops, nhip, pre) \<sqsubseteq> (dsn, dsk', flag, hops, nhip', pre')"
197
+ unfolding fresher_def by (cases flag) simp_all
198
+
199
+ lemma addpre_fresher [simp]: "\<And>r npre. r \<sqsubseteq> (addpre r npre)"
200
+ by clarsimp
201
+
202
+ subsection "Comparing routing tables "
203
+
204
+ definition
205
+ rt_fresher :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
206
+ where
207
+ "rt_fresher \<equiv> \<lambda>dip rt rt'. (the (\<sigma>\<^bsub>route\<^esub>(rt, dip))) \<sqsubseteq> (the (\<sigma>\<^bsub>route\<^esub>(rt', dip)))"
208
+
209
+ abbreviation
210
+ rt_fresher_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<sqsubseteq>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
211
+ where
212
+ "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2 \<equiv> rt_fresher i rt1 rt2"
213
+
214
+ lemma rt_fresher_def':
215
+ "(rt\<^sub>1 \<sqsubseteq>\<^bsub>i\<^esub> rt\<^sub>2) = (nsqn\<^sub>r (the (rt\<^sub>1 i)) < nsqn\<^sub>r (the (rt\<^sub>2 i)) \<or>
216
+ nsqn\<^sub>r (the (rt\<^sub>1 i)) = nsqn\<^sub>r (the (rt\<^sub>2 i)) \<and> \<pi>\<^sub>5 (the (rt\<^sub>2 i)) \<le> \<pi>\<^sub>5 (the (rt\<^sub>1 i)))"
217
+ unfolding rt_fresher_def fresher_def by (rule refl)
218
+
219
+ lemma single_rt_fresher [intro]:
220
+ assumes "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
221
+ shows "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
222
+ using assms unfolding rt_fresher_def .
223
+
224
+ lemma rt_fresher_single [intro]:
225
+ assumes "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
226
+ shows "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
227
+ using assms unfolding rt_fresher_def .
228
+
229
+ lemma rt_fresher_def2:
230
+ assumes "dip \<in> kD(rt1)"
231
+ and "dip \<in> kD(rt2)"
232
+ shows "(rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) = (nsqn rt1 dip < nsqn rt2 dip
233
+ \<or> (nsqn rt1 dip = nsqn rt2 dip
234
+ \<and> the (dhops rt1 dip) \<ge> the (dhops rt2 dip)))"
235
+ using assms unfolding rt_fresher_def fresher_def by (simp add: kD_nsqn proj5_eq_dhops)
236
+
237
+ lemma rt_fresherI1 [intro]:
238
+ assumes "dip \<in> kD(rt1)"
239
+ and "dip \<in> kD(rt2)"
240
+ and "nsqn rt1 dip < nsqn rt2 dip"
241
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
242
+ unfolding rt_fresher_def2 [OF assms(1-2)] using assms(3) by simp
243
+
244
+ lemma rt_fresherI2 [intro]:
245
+ assumes "dip \<in> kD(rt1)"
246
+ and "dip \<in> kD(rt2)"
247
+ and "nsqn rt1 dip = nsqn rt2 dip"
248
+ and "the (dhops rt1 dip) \<ge> the (dhops rt2 dip)"
249
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
250
+ unfolding rt_fresher_def2 [OF assms(1-2)] using assms(3-4) by simp
251
+
252
+ lemma rt_fresherE [elim]:
253
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
254
+ and "dip \<in> kD(rt1)"
255
+ and "dip \<in> kD(rt2)"
256
+ and "\<lbrakk> nsqn rt1 dip < nsqn rt2 dip \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
257
+ and "\<lbrakk> nsqn rt1 dip = nsqn rt2 dip;
258
+ the (dhops rt1 dip) \<ge> the (dhops rt2 dip) \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
259
+ shows "P rt1 rt2 dip"
260
+ using assms(1) unfolding rt_fresher_def2 [OF assms(2-3)]
261
+ using assms(4-5) by auto
262
+
263
+ lemma rt_fresher_refl [simp]: "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt"
264
+ unfolding rt_fresher_def by simp
265
+
266
+ lemma rt_fresher_trans [elim, trans]:
267
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
268
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
269
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
270
+ using assms unfolding rt_fresher_def by auto
271
+
272
+ lemma rt_fresher_if_Some [intro!]:
273
+ assumes "the (rt dip) \<sqsubseteq> r"
274
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> (\<lambda>ip. if ip = dip then Some r else rt ip)"
275
+ using assms unfolding rt_fresher_def by simp
276
+
277
+ definition rt_fresh_as :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
278
+ where
279
+ "rt_fresh_as \<equiv> \<lambda>dip rt1 rt2. (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) \<and> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
280
+
281
+ abbreviation
282
+ rt_fresh_as_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<approx>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
283
+ where
284
+ "rt1 \<approx>\<^bsub>i\<^esub> rt2 \<equiv> rt_fresh_as i rt1 rt2"
285
+
286
+ lemma rt_fresh_as_refl [simp]: "\<And>rt dip. rt \<approx>\<^bsub>dip\<^esub> rt"
287
+ unfolding rt_fresh_as_def by simp
288
+
289
+ lemma rt_fresh_as_trans [simp, intro, trans]:
290
+ "\<And>rt1 rt2 rt3 dip. \<lbrakk> rt1 \<approx>\<^bsub>dip\<^esub> rt2; rt2 \<approx>\<^bsub>dip\<^esub> rt3 \<rbrakk> \<Longrightarrow> rt1 \<approx>\<^bsub>dip\<^esub> rt3"
291
+ unfolding rt_fresh_as_def rt_fresher_def
292
+ by (metis (mono_tags) fresher_trans)
293
+
294
+ lemma rt_fresh_asI [intro!]:
295
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
296
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
297
+ shows "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
298
+ using assms unfolding rt_fresh_as_def by simp
299
+
300
+ lemma rt_fresh_as_fresherI [intro]:
301
+ assumes "dip\<in>kD(rt1)"
302
+ and "dip\<in>kD(rt2)"
303
+ and "the (rt1 dip) \<sqsubseteq> the (rt2 dip)"
304
+ and "the (rt2 dip) \<sqsubseteq> the (rt1 dip)"
305
+ shows "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
306
+ using assms unfolding rt_fresh_as_def
307
+ by (clarsimp dest!: single_rt_fresher)
308
+
309
+ lemma nsqn_rt_fresh_asI:
310
+ assumes "dip \<in> kD(rt)"
311
+ and "dip \<in> kD(rt')"
312
+ and "nsqn rt dip = nsqn rt' dip"
313
+ and "\<pi>\<^sub>5(the (rt dip)) = \<pi>\<^sub>5(the (rt' dip))"
314
+ shows "rt \<approx>\<^bsub>dip\<^esub> rt'"
315
+ proof
316
+ from assms(1-2,4) have dhops': "the (dhops rt' dip) \<le> the (dhops rt dip)"
317
+ by (simp add: proj5_eq_dhops)
318
+ with assms(1-3) show "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt'"
319
+ by (rule rt_fresherI2)
320
+ next
321
+ from assms(1-2,4) have dhops: "the (dhops rt dip) \<le> the (dhops rt' dip)"
322
+ by (simp add: proj5_eq_dhops)
323
+ with assms(2,1) assms(3) [symmetric] show "rt' \<sqsubseteq>\<^bsub>dip\<^esub> rt"
324
+ by (rule rt_fresherI2)
325
+ qed
326
+
327
+ lemma rt_fresh_asE [elim]:
328
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
329
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2; rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1 \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
330
+ shows "P rt1 rt2 dip"
331
+ using assms unfolding rt_fresh_as_def by simp
332
+
333
+ lemma rt_fresh_asD1 [dest]:
334
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
335
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
336
+ using assms unfolding rt_fresh_as_def by simp
337
+
338
+ lemma rt_fresh_asD2 [dest]:
339
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
340
+ shows "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
341
+ using assms unfolding rt_fresh_as_def by simp
342
+
343
+ lemma rt_fresh_as_sym:
344
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
345
+ shows "rt2 \<approx>\<^bsub>dip\<^esub> rt1"
346
+ using assms unfolding rt_fresh_as_def by simp
347
+
348
+ lemma not_rt_fresh_asI1 [intro]:
349
+ assumes "\<not> (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)"
350
+ shows "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
351
+ proof
352
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
353
+ hence "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" ..
354
+ with \<open>\<not> (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)\<close> show False ..
355
+ qed
356
+
357
+ lemma not_rt_fresh_asI2 [intro]:
358
+ assumes "\<not> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
359
+ shows "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
360
+ proof
361
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
362
+ hence "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
363
+ with \<open>\<not> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)\<close> show False ..
364
+ qed
365
+
366
+ lemma not_single_rt_fresher [elim]:
367
+ assumes "\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))"
368
+ shows "\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)"
369
+ proof
370
+ assume "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
371
+ hence "the (rt1 ip) \<sqsubseteq> the (rt2 ip)" ..
372
+ with \<open>\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))\<close> show False ..
373
+ qed
374
+
375
+ lemmas not_single_rt_fresh_asI1 [intro] = not_rt_fresh_asI1 [OF not_single_rt_fresher]
376
+ lemmas not_single_rt_fresh_asI2 [intro] = not_rt_fresh_asI2 [OF not_single_rt_fresher]
377
+
378
+ lemma not_rt_fresher_single [elim]:
379
+ assumes "\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)"
380
+ shows "\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))"
381
+ proof
382
+ assume "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
383
+ hence "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2" ..
384
+ with \<open>\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)\<close> show False ..
385
+ qed
386
+
387
+ lemma rt_fresh_as_nsqnr:
388
+ assumes "dip \<in> kD(rt1)"
389
+ and "dip \<in> kD(rt2)"
390
+ and "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
391
+ shows "nsqn\<^sub>r (the (rt2 dip)) = nsqn\<^sub>r (the (rt1 dip))"
392
+ using assms(3) unfolding rt_fresh_as_def
393
+ by (auto simp: rt_fresher_def2 [OF \<open>dip \<in> kD(rt1)\<close> \<open>dip \<in> kD(rt2)\<close>]
394
+ rt_fresher_def2 [OF \<open>dip \<in> kD(rt2)\<close> \<open>dip \<in> kD(rt1)\<close>]
395
+ kD_nsqn [OF \<open>dip \<in> kD(rt1)\<close>]
396
+ kD_nsqn [OF \<open>dip \<in> kD(rt2)\<close>])
397
+
398
+ lemma rt_fresher_mapupd [intro!]:
399
+ assumes "dip\<in>kD(rt)"
400
+ and "the (rt dip) \<sqsubseteq> r"
401
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt(dip \<mapsto> r)"
402
+ using assms unfolding rt_fresher_def by simp
403
+
404
+ lemma rt_fresher_map_update_other [intro!]:
405
+ assumes "dip\<in>kD(rt)"
406
+ and "dip \<noteq> ip"
407
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt(ip \<mapsto> r)"
408
+ using assms unfolding rt_fresher_def by simp
409
+
410
+ lemma rt_fresher_update_other [simp]:
411
+ assumes inkD: "dip\<in>kD(rt)"
412
+ and "dip \<noteq> ip"
413
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt ip r"
414
+ using assms unfolding update_def
415
+ by (clarsimp split: option.split) (fastforce)
416
+
417
+ theorem rt_fresher_update [simp]:
418
+ assumes "dip\<in>kD(rt)"
419
+ and "the (dhops rt dip) \<ge> 1"
420
+ and "update_arg_wf r"
421
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt ip r"
422
+ proof (cases "dip = ip")
423
+ assume "dip \<noteq> ip" with \<open>dip\<in>kD(rt)\<close> show ?thesis
424
+ by (rule rt_fresher_update_other)
425
+ next
426
+ assume "dip = ip"
427
+
428
+ from \<open>dip\<in>kD(rt)\<close> obtain dsn\<^sub>n dsk\<^sub>n f\<^sub>n hops\<^sub>n nhip\<^sub>n pre\<^sub>n
429
+ where rtn [simp]: "the (rt dip) = (dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)"
430
+ by (metis prod_cases6)
431
+ with \<open>the (dhops rt dip) \<ge> 1\<close> and \<open>dip\<in>kD(rt)\<close> have "hops\<^sub>n \<ge> 1"
432
+ by (metis proj5_eq_dhops projs(4))
433
+ from \<open>dip\<in>kD(rt)\<close> rtn have [simp]: "sqn rt dip = dsn\<^sub>n"
434
+ and [simp]: "the (dhops rt dip) = hops\<^sub>n"
435
+ and [simp]: "the (flag rt dip) = f\<^sub>n"
436
+ by (simp add: sqn_def proj5_eq_dhops [symmetric]
437
+ proj4_eq_flag [symmetric])+
438
+
439
+ from \<open>update_arg_wf r\<close> have "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
440
+ \<sqsubseteq> the ((update rt dip r) dip)"
441
+ proof (rule wf_r_cases)
442
+ fix nhip pre
443
+ from \<open>hops\<^sub>n \<ge> 1\<close> have "\<And>pre'. (dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
444
+ \<sqsubseteq> (dsn\<^sub>n, unk, val, Suc 0, nhip, pre')"
445
+ unfolding fresher_def sqn_def by (cases f\<^sub>n) auto
446
+ thus "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
447
+ \<sqsubseteq> the (update rt dip (0, unk, val, Suc 0, nhip, pre) dip)"
448
+ using \<open>dip\<in>kD(rt)\<close> by - (rule update_cases_kD, simp_all)
449
+ next
450
+ fix dsn :: sqn and hops nhip pre
451
+ assume "0 < dsn"
452
+ show "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
453
+ \<sqsubseteq> the (update rt dip (dsn, kno, val, hops, nhip, pre) dip)"
454
+ proof (rule update_cases_kD [OF _ \<open>dip\<in>kD(rt)\<close>], simp_all add: \<open>0 < dsn\<close>)
455
+ assume "dsn\<^sub>n < dsn"
456
+ thus "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
457
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
458
+ unfolding fresher_def by auto
459
+ next
460
+ assume "dsn\<^sub>n = dsn"
461
+ and "hops < hops\<^sub>n"
462
+ thus "(dsn, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
463
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
464
+ unfolding fresher_def nsqn\<^sub>r_def by simp
465
+ next
466
+ assume "dsn\<^sub>n = dsn"
467
+ with \<open>0 < dsn\<close>
468
+ show "(dsn, dsk\<^sub>n, inv, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
469
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
470
+ unfolding fresher_def by simp
471
+ qed
472
+ qed
473
+ hence "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt dip r"
474
+ by - (rule single_rt_fresher, simp)
475
+ with \<open>dip = ip\<close> show ?thesis by simp
476
+ qed
477
+
478
+ theorem rt_fresher_invalidate [simp]:
479
+ assumes "dip\<in>kD(rt)"
480
+ and indests: "\<forall>rip\<in>dom(dests). rip\<in>vD(rt) \<and> sqn rt rip < the (dests rip)"
481
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> invalidate rt dests"
482
+ proof (cases "dip\<in>dom(dests)")
483
+ assume "dip\<notin>dom(dests)"
484
+ thus ?thesis using \<open>dip\<in>kD(rt)\<close>
485
+ by - (rule single_rt_fresher, simp)
486
+ next
487
+ assume "dip\<in>dom(dests)"
488
+ moreover with indests have "dip\<in>vD(rt)"
489
+ and "sqn rt dip < the (dests dip)"
490
+ by auto
491
+ ultimately show ?thesis
492
+ unfolding invalidate_def sqn_def
493
+ by - (rule single_rt_fresher, auto simp: fresher_def)
494
+ qed
495
+
496
+ lemma nsqn\<^sub>r_invalidate [simp]:
497
+ assumes "dip\<in>kD(rt)"
498
+ and "dip\<in>dom(dests)"
499
+ shows "nsqn\<^sub>r (the (invalidate rt dests dip)) = the (dests dip) - 1"
500
+ using assms unfolding invalidate_def by auto
501
+
502
+ lemma rt_fresh_as_inc_invalidate [simp]:
503
+ assumes "dip\<in>kD(rt)"
504
+ and "\<forall>rip\<in>dom(dests). rip\<in>vD(rt) \<and> the (dests rip) = inc (sqn rt rip)"
505
+ shows "rt \<approx>\<^bsub>dip\<^esub> invalidate rt dests"
506
+ proof (cases "dip\<in>dom(dests)")
507
+ assume "dip\<notin>dom(dests)"
508
+ with \<open>dip\<in>kD(rt)\<close> have "dip\<in>kD(invalidate rt dests)"
509
+ by simp
510
+ with \<open>dip\<in>kD(rt)\<close> show ?thesis
511
+ by rule (simp_all add: \<open>dip\<notin>dom(dests)\<close>)
512
+ next
513
+ assume "dip\<in>dom(dests)"
514
+ with assms(2) have "dip\<in>vD(rt)"
515
+ and "the (dests dip) = inc (sqn rt dip)" by auto
516
+ from \<open>dip\<in>vD(rt)\<close> have "dip\<in>kD(rt)" by simp
517
+ moreover then have "dip\<in>kD(invalidate rt dests)" by simp
518
+ ultimately show ?thesis
519
+ proof (rule nsqn_rt_fresh_asI)
520
+ from \<open>dip\<in>vD(rt)\<close> have "nsqn rt dip = sqn rt dip" by simp
521
+ also have "sqn rt dip = nsqn\<^sub>r (the (invalidate rt dests dip))"
522
+ proof -
523
+ from \<open>dip\<in>kD(rt)\<close> have "nsqn\<^sub>r (the (invalidate rt dests dip)) = the (dests dip) - 1"
524
+ using \<open>dip\<in>dom(dests)\<close> by (rule nsqn\<^sub>r_invalidate)
525
+ with \<open>the (dests dip) = inc (sqn rt dip)\<close>
526
+ show "sqn rt dip = nsqn\<^sub>r (the (invalidate rt dests dip))" by simp
527
+ qed
528
+ also from \<open>dip\<in>kD(invalidate rt dests)\<close>
529
+ have "nsqn\<^sub>r (the (invalidate rt dests dip)) = nsqn (invalidate rt dests) dip"
530
+ by (simp add: kD_nsqn)
531
+ finally show "nsqn rt dip = nsqn (invalidate rt dests) dip" .
532
+ qed simp
533
+ qed
534
+
535
+ lemmas rt_fresher_inc_invalidate [simp] = rt_fresh_as_inc_invalidate [THEN rt_fresh_asD1]
536
+
537
+ lemma rt_fresh_as_addpreRT [simp]:
538
+ assumes "ip\<in>kD(rt)"
539
+ shows "rt \<approx>\<^bsub>dip\<^esub> the (addpreRT rt ip npre)"
540
+ using assms [THEN kD_Some] by (auto simp: addpreRT_def)
541
+
542
+ lemmas rt_fresher_addpreRT [simp] = rt_fresh_as_addpreRT [THEN rt_fresh_asD1]
543
+
544
+ subsection "Strictly comparing routing tables "
545
+
546
+ definition rt_strictly_fresher :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
547
+ where
548
+ "rt_strictly_fresher \<equiv> \<lambda>dip rt1 rt2. (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) \<and> \<not>(rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
549
+
550
+ abbreviation
551
+ rt_strictly_fresher_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<sqsubset>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
552
+ where
553
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 \<equiv> rt_strictly_fresher i rt1 rt2"
554
+
555
+ lemma rt_strictly_fresher_def'':
556
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 = ((rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2) \<and> \<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1))"
557
+ unfolding rt_strictly_fresher_def rt_fresh_as_def by auto
558
+
559
+ lemma rt_strictly_fresherI' [intro]:
560
+ assumes "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2"
561
+ and "\<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1)"
562
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
563
+ using assms unfolding rt_strictly_fresher_def'' by simp
564
+
565
+ lemma rt_strictly_fresherE' [elim]:
566
+ assumes "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
567
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2; \<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1) \<rbrakk> \<Longrightarrow> P rt1 rt2 i"
568
+ shows "P rt1 rt2 i"
569
+ using assms unfolding rt_strictly_fresher_def'' by simp
570
+
571
+ lemma rt_strictly_fresherI [intro]:
572
+ assumes "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2"
573
+ and "\<not>(rt1 \<approx>\<^bsub>i\<^esub> rt2)"
574
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
575
+ unfolding rt_strictly_fresher_def using assms ..
576
+
577
+ lemmas rt_strictly_fresher_singleI [elim] = rt_strictly_fresherI [OF single_rt_fresher]
578
+
579
+ lemma rt_strictly_fresherE [elim]:
580
+ assumes "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
581
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2; \<not>(rt1 \<approx>\<^bsub>i\<^esub> rt2) \<rbrakk> \<Longrightarrow> P rt1 rt2 i"
582
+ shows "P rt1 rt2 i"
583
+ using assms(1) unfolding rt_strictly_fresher_def
584
+ by rule (erule(1) assms(2))
585
+
586
+ lemma rt_strictly_fresher_def':
587
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 =
588
+ (nsqn\<^sub>r (the (rt1 i)) < nsqn\<^sub>r (the (rt2 i))
589
+ \<or> (nsqn\<^sub>r (the (rt1 i)) = nsqn\<^sub>r (the (rt2 i)) \<and> \<pi>\<^sub>5(the (rt1 i)) > \<pi>\<^sub>5(the (rt2 i))))"
590
+ unfolding rt_strictly_fresher_def'' rt_fresher_def fresher_def by auto
591
+
592
+ lemma rt_strictly_fresher_fresherD [dest]:
593
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
594
+ shows "the (rt1 dip) \<sqsubseteq> the (rt2 dip)"
595
+ using assms unfolding rt_strictly_fresher_def rt_fresher_def by auto
596
+
597
+ lemma rt_strictly_fresher_not_fresh_asD [dest]:
598
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
599
+ shows "\<not> rt1 \<approx>\<^bsub>dip\<^esub> rt2"
600
+ using assms unfolding rt_strictly_fresher_def by auto
601
+
602
+ lemma rt_strictly_fresher_trans [elim, trans]:
603
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
604
+ and "rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3"
605
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
606
+ using assms proof -
607
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> obtain "the (rt1 dip) \<sqsubseteq> the (rt2 dip)" by auto
608
+ also from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> obtain "the (rt2 dip) \<sqsubseteq> the (rt3 dip)" by auto
609
+ finally have "the (rt1 dip) \<sqsubseteq> the (rt3 dip)" .
610
+
611
+ moreover have "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt3)"
612
+ proof -
613
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> obtain "\<not>(the (rt2 dip) \<sqsubseteq> the (rt1 dip))" by auto
614
+ also from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> obtain "\<not>(the (rt3 dip) \<sqsubseteq> the (rt2 dip))" by auto
615
+ finally have "\<not>(the (rt3 dip) \<sqsubseteq> the (rt1 dip))" .
616
+ thus ?thesis ..
617
+ qed
618
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3" ..
619
+ qed
620
+
621
+ lemma rt_strictly_fresher_irefl [simp]: "\<not> (rt \<sqsubset>\<^bsub>dip\<^esub> rt)"
622
+ unfolding rt_strictly_fresher_def
623
+ by clarsimp
624
+
625
+ lemma rt_fresher_trans_rt_strictly_fresher [elim, trans]:
626
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
627
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
628
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
629
+ proof -
630
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
631
+ and "\<not>(rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
632
+ unfolding rt_strictly_fresher_def'' by auto
633
+ from this(1) and \<open>rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3\<close> have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3" ..
634
+
635
+ moreover from \<open>\<not>(rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)\<close> have "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
636
+ proof (rule contrapos_nn)
637
+ assume "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
638
+ with \<open>rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3\<close> show "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
639
+ qed
640
+
641
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
642
+ unfolding rt_strictly_fresher_def'' by auto
643
+ qed
644
+
645
+ lemma rt_fresher_trans_rt_strictly_fresher' [elim, trans]:
646
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
647
+ and "rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3"
648
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
649
+ proof -
650
+ from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> have "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
651
+ and "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)"
652
+ unfolding rt_strictly_fresher_def'' by auto
653
+ from \<open>rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2\<close> and this(1) have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3" ..
654
+
655
+ moreover from \<open>\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)\<close> have "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
656
+ proof (rule contrapos_nn)
657
+ assume "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
658
+ thus "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" using \<open>rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2\<close> ..
659
+ qed
660
+
661
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
662
+ unfolding rt_strictly_fresher_def'' by auto
663
+ qed
664
+
665
+ lemma rt_fresher_imp_nsqn_le:
666
+ assumes "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
667
+ and "ip \<in> kD rt1"
668
+ and "ip \<in> kD rt2"
669
+ shows "nsqn rt1 ip \<le> nsqn rt2 ip"
670
+ using assms(1)
671
+ by (auto simp add: rt_fresher_def2 [OF assms(2-3)])
672
+
673
+ lemma rt_strictly_fresher_ltI [intro]:
674
+ assumes "dip \<in> kD(rt1)"
675
+ and "dip \<in> kD(rt2)"
676
+ and "nsqn rt1 dip < nsqn rt2 dip"
677
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
678
+ proof
679
+ from assms show "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" ..
680
+ next
681
+ show "\<not>(rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
682
+ proof
683
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
684
+ hence "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
685
+ hence "nsqn rt2 dip \<le> nsqn rt1 dip"
686
+ using \<open>dip \<in> kD(rt2)\<close> \<open>dip \<in> kD(rt1)\<close>
687
+ by (rule rt_fresher_imp_nsqn_le)
688
+ with \<open>nsqn rt1 dip < nsqn rt2 dip\<close> show "False"
689
+ by simp
690
+ qed
691
+ qed
692
+
693
+ lemma rt_strictly_fresher_eqI [intro]:
694
+ assumes "i\<in>kD(rt1)"
695
+ and "i\<in>kD(rt2)"
696
+ and "nsqn rt1 i = nsqn rt2 i"
697
+ and "\<pi>\<^sub>5(the (rt2 i)) < \<pi>\<^sub>5(the (rt1 i))"
698
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
699
+ using assms unfolding rt_strictly_fresher_def' by (auto simp add: kD_nsqn)
700
+
701
+ lemma invalidate_rtsf_left [simp]:
702
+ "\<And>dests dip rt rt'. dests dip = None \<Longrightarrow> (invalidate rt dests \<sqsubset>\<^bsub>dip\<^esub> rt') = (rt \<sqsubset>\<^bsub>dip\<^esub> rt')"
703
+ unfolding invalidate_def rt_strictly_fresher_def'
704
+ by (rule iffI) (auto split: option.split_asm)
705
+
706
+ lemma vD_invalidate_rt_strictly_fresher [simp]:
707
+ assumes "dip \<in> vD(invalidate rt1 dests)"
708
+ shows "(invalidate rt1 dests \<sqsubset>\<^bsub>dip\<^esub> rt2) = (rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2)"
709
+ proof (cases "dip \<in> dom(dests)")
710
+ assume "dip \<in> dom(dests)"
711
+ hence "dip \<notin> vD(invalidate rt1 dests)"
712
+ unfolding invalidate_def vD_def
713
+ by clarsimp (metis assms option.simps(3) vD_invalidate_vD_not_dests)
714
+ with \<open>dip \<in> vD(invalidate rt1 dests)\<close> show ?thesis by simp
715
+ next
716
+ assume "dip \<notin> dom(dests)"
717
+ hence "dests dip = None" by auto
718
+ moreover with \<open>dip \<in> vD(invalidate rt1 dests)\<close> have "dip \<in> vD(rt1)"
719
+ unfolding invalidate_def vD_def
720
+ by clarsimp (metis (opaque_lifting, no_types) assms vD_Some vD_invalidate_vD_not_dests)
721
+ ultimately show ?thesis
722
+ unfolding invalidate_def rt_strictly_fresher_def' by auto
723
+ qed
724
+
725
+ lemma rt_strictly_fresher_update_other [elim!]:
726
+ "\<And>dip ip rt r rt'. \<lbrakk> dip \<noteq> ip; rt \<sqsubset>\<^bsub>dip\<^esub> rt' \<rbrakk> \<Longrightarrow> update rt ip r \<sqsubset>\<^bsub>dip\<^esub> rt'"
727
+ unfolding rt_strictly_fresher_def' by clarsimp
728
+
729
+ lemma addpreRT_strictly_fresher [simp]:
730
+ assumes "dip \<in> kD(rt)"
731
+ shows "(the (addpreRT rt dip npre) \<sqsubset>\<^bsub>ip\<^esub> rt2) = (rt \<sqsubset>\<^bsub>ip\<^esub> rt2)"
732
+ using assms unfolding rt_strictly_fresher_def' by clarsimp
733
+
734
+ lemma lt_sqn_imp_update_strictly_fresher:
735
+ assumes "dip \<in> vD (rt2 nhip)"
736
+ and *: "osn < sqn (rt2 nhip) dip"
737
+ and **: "rt \<noteq> update rt dip (osn, kno, val, hops, nhip, {})"
738
+ shows "update rt dip (osn, kno, val, hops, nhip, {}) \<sqsubset>\<^bsub>dip\<^esub> rt2 nhip"
739
+ unfolding rt_strictly_fresher_def'
740
+ proof (rule disjI1)
741
+ from ** have "nsqn (update rt dip (osn, kno, val, hops, nhip, {})) dip = osn"
742
+ by (rule nsqn_update_changed_kno_val)
743
+ with \<open>dip\<in>vD(rt2 nhip)\<close>
744
+ have "nsqn\<^sub>r (the (update rt dip (osn, kno, val, hops, nhip, {}) dip)) = osn"
745
+ by (simp add: kD_nsqn)
746
+ also have "osn < sqn (rt2 nhip) dip" by (rule *)
747
+ also have "sqn (rt2 nhip) dip = nsqn\<^sub>r (the (rt2 nhip dip))"
748
+ unfolding nsqn\<^sub>r_def using \<open>dip \<in> vD (rt2 nhip)\<close>
749
+ by - (metis vD_flag_val proj2_eq_sqn proj4_eq_flag vD_iD_gives_kD(1))
750
+ finally show "nsqn\<^sub>r (the (update rt dip (osn, kno, val, hops, nhip, {}) dip))
751
+ < nsqn\<^sub>r (the (rt2 nhip dip))" .
752
+ qed
753
+
754
+ lemma dhops_le_hops_imp_update_strictly_fresher:
755
+ assumes "dip \<in> vD(rt2 nhip)"
756
+ and sqn: "sqn (rt2 nhip) dip = osn"
757
+ and hop: "the (dhops (rt2 nhip) dip) \<le> hops"
758
+ and **: "rt \<noteq> update rt dip (osn, kno, val, Suc hops, nhip, {})"
759
+ shows "update rt dip (osn, kno, val, Suc hops, nhip, {}) \<sqsubset>\<^bsub>dip\<^esub> rt2 nhip"
760
+ unfolding rt_strictly_fresher_def'
761
+ proof (rule disjI2, rule conjI)
762
+ from ** have "nsqn (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip = osn"
763
+ by (rule nsqn_update_changed_kno_val)
764
+ with \<open>dip\<in>vD(rt2 nhip)\<close>
765
+ have "nsqn\<^sub>r (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip)) = osn"
766
+ by (simp add: kD_nsqn)
767
+ also have "osn = sqn (rt2 nhip) dip" by (rule sqn [symmetric])
768
+ also have "sqn (rt2 nhip) dip = nsqn\<^sub>r (the (rt2 nhip dip))"
769
+ unfolding nsqn\<^sub>r_def using \<open>dip \<in> vD(rt2 nhip)\<close>
770
+ by - (metis vD_flag_val proj2_eq_sqn proj4_eq_flag vD_iD_gives_kD(1))
771
+ finally show "nsqn\<^sub>r (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip))
772
+ = nsqn\<^sub>r (the (rt2 nhip dip))" .
773
+ next
774
+ have "the (dhops (rt2 nhip) dip) \<le> hops" by (rule hop)
775
+ also have "hops < hops + 1" by simp
776
+ also have "hops + 1 = the (dhops (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip)"
777
+ using ** by simp
778
+ finally have "the (dhops (rt2 nhip) dip)
779
+ < the (dhops (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip)" .
780
+ thus "\<pi>\<^sub>5 (the (rt2 nhip dip)) < \<pi>\<^sub>5 (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip))"
781
+ using \<open>dip \<in> vD(rt2 nhip)\<close> by (simp add: proj5_eq_dhops)
782
+ qed
783
+
784
+ lemma nsqn_invalidate:
785
+ assumes "dip \<in> kD(rt)"
786
+ and "\<forall>ip\<in>dom(dests). ip \<in> vD(rt) \<and> the (dests ip) = inc (sqn rt ip)"
787
+ shows "nsqn (invalidate rt dests) dip = nsqn rt dip"
788
+ proof -
789
+ from \<open>dip \<in> kD(rt)\<close> have "dip \<in> kD(invalidate rt dests)" by simp
790
+
791
+ from assms have "rt \<approx>\<^bsub>dip\<^esub> invalidate rt dests"
792
+ by (rule rt_fresh_as_inc_invalidate)
793
+ with \<open>dip \<in> kD(rt)\<close> \<open>dip \<in> kD(invalidate rt dests)\<close> show ?thesis
794
+ by (simp add: kD_nsqn del: invalidate_kD_inv)
795
+ (erule(2) rt_fresh_as_nsqnr)
796
+ qed
797
+
798
+ end
formal/afp/AODV/Global_Invariants.thy ADDED
@@ -0,0 +1,1151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Global_Invariants.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Global invariant proofs over sequential processes"
7
+
8
+ theory Global_Invariants
9
+ imports Seq_Invariants
10
+ Aodv_Predicates
11
+ Fresher
12
+ Quality_Increases
13
+ AWN.OAWN_Convert
14
+ OAodv
15
+ begin
16
+
17
+ lemma other_quality_increases [elim]:
18
+ assumes "other quality_increases I \<sigma> \<sigma>'"
19
+ shows "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
20
+ using assms by (rule, clarsimp) (metis quality_increases_refl)
21
+
22
+ lemma weaken_otherwith [elim]:
23
+ fixes m
24
+ assumes *: "otherwith P I (orecvmsg Q) \<sigma> \<sigma>' a"
25
+ and weakenP: "\<And>\<sigma> m. P \<sigma> m \<Longrightarrow> P' \<sigma> m"
26
+ and weakenQ: "\<And>\<sigma> m. Q \<sigma> m \<Longrightarrow> Q' \<sigma> m"
27
+ shows "otherwith P' I (orecvmsg Q') \<sigma> \<sigma>' a"
28
+ proof
29
+ fix j
30
+ assume "j\<notin>I"
31
+ with * have "P (\<sigma> j) (\<sigma>' j)" by auto
32
+ thus "P' (\<sigma> j) (\<sigma>' j)" by (rule weakenP)
33
+ next
34
+ from * have "orecvmsg Q \<sigma> a" by auto
35
+ thus "orecvmsg Q' \<sigma> a"
36
+ by rule (erule weakenQ)
37
+ qed
38
+
39
+ lemma oreceived_msg_inv:
40
+ assumes other: "\<And>\<sigma> \<sigma>' m. \<lbrakk> P \<sigma> m; other Q {i} \<sigma> \<sigma>' \<rbrakk> \<Longrightarrow> P \<sigma>' m"
41
+ and local: "\<And>\<sigma> m. P \<sigma> m \<Longrightarrow> P (\<sigma>(i := \<sigma> i\<lparr>msg := m\<rparr>)) m"
42
+ shows "opaodv i \<Turnstile> (otherwith Q {i} (orecvmsg P), other Q {i} \<rightarrow>)
43
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l). l \<in> {PAodv-:1} \<longrightarrow> P \<sigma> (msg (\<sigma> i)))"
44
+ proof (inv_cterms, intro impI)
45
+ fix \<sigma> \<sigma>' l
46
+ assume "l = PAodv-:1 \<longrightarrow> P \<sigma> (msg (\<sigma> i))"
47
+ and "l = PAodv-:1"
48
+ and "other Q {i} \<sigma> \<sigma>'"
49
+ from this(1-2) have "P \<sigma> (msg (\<sigma> i))" ..
50
+ hence "P \<sigma>' (msg (\<sigma> i))" using \<open>other Q {i} \<sigma> \<sigma>'\<close>
51
+ by (rule other)
52
+ moreover from \<open>other Q {i} \<sigma> \<sigma>'\<close> have "\<sigma>' i = \<sigma> i" ..
53
+ ultimately show "P \<sigma>' (msg (\<sigma>' i))" by simp
54
+ next
55
+ fix \<sigma> \<sigma>' msg
56
+ assume "otherwith Q {i} (orecvmsg P) \<sigma> \<sigma>' (receive msg)"
57
+ and "\<sigma>' i = \<sigma> i\<lparr>msg := msg\<rparr>"
58
+ from this(1) have "P \<sigma> msg"
59
+ and "\<forall>j. j\<noteq>i \<longrightarrow> Q (\<sigma> j) (\<sigma>' j)" by auto
60
+ from this(1) have "P (\<sigma>(i := \<sigma> i\<lparr>msg := msg\<rparr>)) msg" by (rule local)
61
+ thus "P \<sigma>' msg"
62
+ proof (rule other)
63
+ from \<open>\<sigma>' i = \<sigma> i\<lparr>msg := msg\<rparr>\<close> and \<open>\<forall>j. j\<noteq>i \<longrightarrow> Q (\<sigma> j) (\<sigma>' j)\<close>
64
+ show "other Q {i} (\<sigma>(i := \<sigma> i\<lparr>msg := msg\<rparr>)) \<sigma>'"
65
+ by - (rule otherI, auto)
66
+ qed
67
+ qed
68
+
69
+
70
+ text \<open>(Equivalent to) Proposition 7.27\<close>
71
+
72
+ lemma local_quality_increases:
73
+ "paodv i \<TTurnstile>\<^sub>A (recvmsg rreq_rrep_sn \<rightarrow>) onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). quality_increases \<xi> \<xi>')"
74
+ proof (rule step_invariantI)
75
+ fix s a s'
76
+ assume sr: "s \<in> reachable (paodv i) (recvmsg rreq_rrep_sn)"
77
+ and tr: "(s, a, s') \<in> trans (paodv i)"
78
+ and rm: "recvmsg rreq_rrep_sn a"
79
+ from sr have srTT: "s \<in> reachable (paodv i) TT" ..
80
+
81
+ from route_tables_fresher sr tr rm
82
+ have "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). \<forall>dip\<in>kD (rt \<xi>). rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>') (s, a, s')"
83
+ by (rule step_invariantD)
84
+
85
+ moreover from known_destinations_increase srTT tr TT_True
86
+ have "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). kD (rt \<xi>) \<subseteq> kD (rt \<xi>')) (s, a, s')"
87
+ by (rule step_invariantD)
88
+
89
+ moreover from sqns_increase srTT tr TT_True
90
+ have "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). \<forall>ip. sqn (rt \<xi>) ip \<le> sqn (rt \<xi>') ip) (s, a, s')"
91
+ by (rule step_invariantD)
92
+
93
+ ultimately show "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). quality_increases \<xi> \<xi>') (s, a, s')"
94
+ unfolding onll_def by auto
95
+ qed
96
+
97
+ lemmas olocal_quality_increases =
98
+ open_seq_step_invariant [OF local_quality_increases initiali_aodv oaodv_trans aodv_trans,
99
+ simplified seqll_onll_swap]
100
+
101
+ lemma oquality_increases:
102
+ "opaodv i \<Turnstile>\<^sub>A (otherwith quality_increases {i} (orecvmsg (\<lambda>_. rreq_rrep_sn)),
103
+ other quality_increases {i} \<rightarrow>)
104
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), _, (\<sigma>', _)). \<forall>j. quality_increases (\<sigma> j) (\<sigma>' j))"
105
+ (is "_ \<Turnstile>\<^sub>A (?S, _ \<rightarrow>) _")
106
+ proof (rule onll_ostep_invariantI, simp)
107
+ fix \<sigma> p l a \<sigma>' p' l'
108
+ assume or: "(\<sigma>, p) \<in> oreachable (opaodv i) ?S (other quality_increases {i})"
109
+ and ll: "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
110
+ and "?S \<sigma> \<sigma>' a"
111
+ and tr: "((\<sigma>, p), a, (\<sigma>', p')) \<in> oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
112
+ and ll': "l' \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'"
113
+ from this(1-3) have "orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma> a"
114
+ by (auto dest!: oreachable_weakenE [where QS="act (recvmsg rreq_rrep_sn)"
115
+ and QU="other quality_increases {i}"]
116
+ otherwith_actionD)
117
+ with or have orw: "(\<sigma>, p) \<in> oreachable (opaodv i) (act (recvmsg rreq_rrep_sn))
118
+ (other quality_increases {i})"
119
+ by - (erule oreachable_weakenE, auto)
120
+ with tr ll ll' and \<open>orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma> a\<close> have "quality_increases (\<sigma> i) (\<sigma>' i)"
121
+ by - (drule onll_ostep_invariantD [OF olocal_quality_increases], auto simp: seqll_def)
122
+ with \<open>?S \<sigma> \<sigma>' a\<close> show "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
123
+ by (auto dest!: otherwith_syncD)
124
+ qed
125
+
126
+ lemma rreq_rrep_nsqn_fresh_any_step_invariant:
127
+ "opaodv i \<Turnstile>\<^sub>A (act (recvmsg rreq_rrep_sn), other A {i} \<rightarrow>)
128
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), a, _). anycast (msg_fresh \<sigma>) a)"
129
+ proof (rule ostep_invariantI, simp del: act_simp)
130
+ fix \<sigma> p a \<sigma>' p'
131
+ assume or: "(\<sigma>, p) \<in> oreachable (opaodv i) (act (recvmsg rreq_rrep_sn)) (other A {i})"
132
+ and "((\<sigma>, p), a, (\<sigma>', p')) \<in> oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
133
+ and recv: "act (recvmsg rreq_rrep_sn) \<sigma> \<sigma>' a"
134
+ obtain l l' where "l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p" and "l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'"
135
+ by (metis aodv_ex_label)
136
+ from \<open>((\<sigma>, p), a, (\<sigma>', p')) \<in> oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i\<close>
137
+ have tr: "((\<sigma>, p), a, (\<sigma>', p')) \<in> trans (opaodv i)" by simp
138
+
139
+ have "anycast (rreq_rrep_fresh (rt (\<sigma> i))) a"
140
+ proof -
141
+ have "opaodv i \<Turnstile>\<^sub>A (act (recvmsg rreq_rrep_sn), other A {i} \<rightarrow>)
142
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (rreq_rrep_fresh (rt \<xi>)) a))"
143
+ by (rule ostep_invariant_weakenE [OF
144
+ open_seq_step_invariant [OF rreq_rrep_fresh_any_step_invariant initiali_aodv,
145
+ simplified seqll_onll_swap]]) auto
146
+ hence "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (rreq_rrep_fresh (rt \<xi>)) a))
147
+ ((\<sigma>, p), a, (\<sigma>', p'))"
148
+ using or tr recv by - (erule(4) ostep_invariantE)
149
+ thus ?thesis
150
+ using \<open>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and \<open>l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'\<close> by auto
151
+ qed
152
+
153
+ moreover have "anycast (rerr_invalid (rt (\<sigma> i))) a"
154
+ proof -
155
+ have "opaodv i \<Turnstile>\<^sub>A (act (recvmsg rreq_rrep_sn), other A {i} \<rightarrow>)
156
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (rerr_invalid (rt \<xi>)) a))"
157
+ by (rule ostep_invariant_weakenE [OF
158
+ open_seq_step_invariant [OF rerr_invalid_any_step_invariant initiali_aodv,
159
+ simplified seqll_onll_swap]]) auto
160
+ hence "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (rerr_invalid (rt \<xi>)) a))
161
+ ((\<sigma>, p), a, (\<sigma>', p'))"
162
+ using or tr recv by - (erule(4) ostep_invariantE)
163
+ thus ?thesis
164
+ using \<open>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and \<open>l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'\<close> by auto
165
+ qed
166
+
167
+ moreover have "anycast rreq_rrep_sn a"
168
+ proof -
169
+ from or tr recv
170
+ have "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>(_, a, _). anycast rreq_rrep_sn a)) ((\<sigma>, p), a, (\<sigma>', p'))"
171
+ by (rule ostep_invariantE [OF
172
+ open_seq_step_invariant [OF rreq_rrep_sn_any_step_invariant initiali_aodv
173
+ oaodv_trans aodv_trans,
174
+ simplified seqll_onll_swap]])
175
+ thus ?thesis
176
+ using \<open>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and \<open>l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'\<close> by auto
177
+ qed
178
+
179
+ moreover have "anycast (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m = i) a"
180
+ proof -
181
+ have "opaodv i \<Turnstile>\<^sub>A (act (recvmsg rreq_rrep_sn), other A {i} \<rightarrow>)
182
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m = i) a))"
183
+ by (rule ostep_invariant_weakenE [OF
184
+ open_seq_step_invariant [OF sender_ip_valid initiali_aodv,
185
+ simplified seqll_onll_swap]]) auto
186
+ thus ?thesis using or tr recv \<open>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and \<open>l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'\<close>
187
+ by - (drule(3) onll_ostep_invariantD, auto)
188
+ qed
189
+
190
+ ultimately have "anycast (msg_fresh \<sigma>) a"
191
+ by (simp_all add: anycast_def
192
+ del: msg_fresh
193
+ split: seq_action.split_asm msg.split_asm) simp_all
194
+ thus "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), a, _). anycast (msg_fresh \<sigma>) a) ((\<sigma>, p), a, (\<sigma>', p'))"
195
+ by auto
196
+ qed
197
+
198
+ lemma oreceived_rreq_rrep_nsqn_fresh_inv:
199
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
200
+ other quality_increases {i} \<rightarrow>)
201
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l). l \<in> {PAodv-:1} \<longrightarrow> msg_fresh \<sigma> (msg (\<sigma> i)))"
202
+ proof (rule oreceived_msg_inv)
203
+ fix \<sigma> \<sigma>' m
204
+ assume *: "msg_fresh \<sigma> m"
205
+ and "other quality_increases {i} \<sigma> \<sigma>'"
206
+ from this(2) have "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)" ..
207
+ thus "msg_fresh \<sigma>' m" using * ..
208
+ next
209
+ fix \<sigma> m
210
+ assume "msg_fresh \<sigma> m"
211
+ thus "msg_fresh (\<sigma>(i := \<sigma> i\<lparr>msg := m\<rparr>)) m"
212
+ proof (cases m)
213
+ fix dests sip
214
+ assume "m = Rerr dests sip"
215
+ with \<open>msg_fresh \<sigma> m\<close> show ?thesis
216
+ by auto
217
+ qed auto
218
+ qed
219
+
220
+ lemma oquality_increases_nsqn_fresh:
221
+ "opaodv i \<Turnstile>\<^sub>A (otherwith quality_increases {i} (orecvmsg msg_fresh),
222
+ other quality_increases {i} \<rightarrow>)
223
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), _, (\<sigma>', _)). \<forall>j. quality_increases (\<sigma> j) (\<sigma>' j))"
224
+ by (rule ostep_invariant_weakenE [OF oquality_increases]) auto
225
+
226
+ lemma oosn_rreq:
227
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
228
+ other quality_increases {i} \<rightarrow>)
229
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seql i (\<lambda>(\<xi>, l). l \<in> {PAodv-:4, PAodv-:5} \<union> {PRreq-:n |n. True} \<longrightarrow> 1 \<le> osn \<xi>))"
230
+ by (rule oinvariant_weakenE [OF open_seq_invariant [OF osn_rreq initiali_aodv]])
231
+ (auto simp: seql_onl_swap)
232
+
233
+ lemma rreq_sip:
234
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
235
+ other quality_increases {i} \<rightarrow>)
236
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l).
237
+ (l \<in> {PAodv-:4, PAodv-:5, PRreq-:0, PRreq-:2} \<and> sip (\<sigma> i) \<noteq> oip (\<sigma> i))
238
+ \<longrightarrow> oip (\<sigma> i) \<in> kD(rt (\<sigma> (sip (\<sigma> i))))
239
+ \<and> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i)) \<ge> osn (\<sigma> i)
240
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i)) = osn (\<sigma> i)
241
+ \<longrightarrow> (hops (\<sigma> i) \<ge> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i)))
242
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i))) = inv)))"
243
+ (is "_ \<Turnstile> (?S, ?U \<rightarrow>) _")
244
+ proof (inv_cterms inv add: oseq_step_invariant_sterms [OF oquality_increases_nsqn_fresh
245
+ aodv_wf oaodv_trans]
246
+ onl_oinvariant_sterms [OF aodv_wf oreceived_rreq_rrep_nsqn_fresh_inv]
247
+ onl_oinvariant_sterms [OF aodv_wf oosn_rreq]
248
+ simp add: seqlsimp
249
+ simp del: One_nat_def, rule impI)
250
+ fix \<sigma> \<sigma>' p l
251
+ assume "(\<sigma>, p) \<in> oreachable (opaodv i) ?S ?U"
252
+ and "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
253
+ and pre:
254
+ "(l = PAodv-:4 \<or> l = PAodv-:5 \<or> l = PRreq-:0 \<or> l = PRreq-:2) \<and> sip (\<sigma> i) \<noteq> oip (\<sigma> i)
255
+ \<longrightarrow> oip (\<sigma> i) \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
256
+ \<and> osn (\<sigma> i) \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i))
257
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i)) = osn (\<sigma> i)
258
+ \<longrightarrow> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i))) \<le> hops (\<sigma> i)
259
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i))) = inv)"
260
+ and "other quality_increases {i} \<sigma> \<sigma>'"
261
+ and hyp: "(l=PAodv-:4 \<or> l=PAodv-:5 \<or> l=PRreq-:0 \<or> l=PRreq-:2) \<and> sip (\<sigma>' i) \<noteq> oip (\<sigma>' i)"
262
+ (is "?labels \<and> sip (\<sigma>' i) \<noteq> oip (\<sigma>' i)")
263
+ from this(4) have "\<sigma>' i = \<sigma> i" ..
264
+ with hyp have hyp': "?labels \<and> sip (\<sigma> i) \<noteq> oip (\<sigma> i)" by simp
265
+ show "oip (\<sigma>' i) \<in> kD (rt (\<sigma>' (sip (\<sigma>' i))))
266
+ \<and> osn (\<sigma>' i) \<le> nsqn (rt (\<sigma>' (sip (\<sigma>' i)))) (oip (\<sigma>' i))
267
+ \<and> (nsqn (rt (\<sigma>' (sip (\<sigma>' i)))) (oip (\<sigma>' i)) = osn (\<sigma>' i)
268
+ \<longrightarrow> the (dhops (rt (\<sigma>' (sip (\<sigma>' i)))) (oip (\<sigma>' i))) \<le> hops (\<sigma>' i)
269
+ \<or> the (flag (rt (\<sigma>' (sip (\<sigma>' i)))) (oip (\<sigma>' i))) = inv)"
270
+ proof (cases "sip (\<sigma> i) = i")
271
+ assume "sip (\<sigma> i) \<noteq> i"
272
+ from \<open>other quality_increases {i} \<sigma> \<sigma>'\<close>
273
+ have "quality_increases (\<sigma> (sip (\<sigma> i))) (\<sigma>' (sip (\<sigma>' i)))"
274
+ by (rule otherE) (clarsimp simp: \<open>sip (\<sigma> i) \<noteq> i\<close>)
275
+ moreover from \<open>(\<sigma>, p) \<in> oreachable (opaodv i) ?S ?U\<close> \<open>l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and hyp
276
+ have "1 \<le> osn (\<sigma>' i)"
277
+ by (auto dest!: onl_oinvariant_weakenD [OF oosn_rreq]
278
+ simp add: seqlsimp \<open>\<sigma>' i = \<sigma> i\<close>)
279
+ moreover from \<open>sip (\<sigma> i) \<noteq> i\<close> hyp' and pre
280
+ have "oip (\<sigma>' i) \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
281
+ \<and> osn (\<sigma>' i) \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma>' i))
282
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma>' i)) = osn (\<sigma>' i)
283
+ \<longrightarrow> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma>' i))) \<le> hops (\<sigma>' i)
284
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma>' i))) = inv)"
285
+ by (auto simp: \<open>\<sigma>' i = \<sigma> i\<close>)
286
+ ultimately show ?thesis
287
+ by (rule quality_increases_rreq_rrep_props)
288
+ next
289
+ assume "sip (\<sigma> i) = i" thus ?thesis
290
+ using \<open>\<sigma>' i = \<sigma> i\<close> hyp and pre by auto
291
+ qed
292
+ qed (auto elim!: quality_increases_rreq_rrep_props')
293
+
294
+ lemma odsn_rrep:
295
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
296
+ other quality_increases {i} \<rightarrow>)
297
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seql i (\<lambda>(\<xi>, l). l \<in> {PAodv-:6, PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow> 1 \<le> dsn \<xi>))"
298
+ by (rule oinvariant_weakenE [OF open_seq_invariant [OF dsn_rrep initiali_aodv]])
299
+ (auto simp: seql_onl_swap)
300
+
301
+ lemma rrep_sip:
302
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
303
+ other quality_increases {i} \<rightarrow>)
304
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l).
305
+ (l \<in> {PAodv-:6, PAodv-:7, PRrep-:0, PRrep-:1} \<and> sip (\<sigma> i) \<noteq> dip (\<sigma> i))
306
+ \<longrightarrow> dip (\<sigma> i) \<in> kD(rt (\<sigma> (sip (\<sigma> i))))
307
+ \<and> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i)) \<ge> dsn (\<sigma> i)
308
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i)) = dsn (\<sigma> i)
309
+ \<longrightarrow> (hops (\<sigma> i) \<ge> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i)))
310
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i))) = inv)))"
311
+ (is "_ \<Turnstile> (?S, ?U \<rightarrow>) _")
312
+ proof (inv_cterms inv add: oseq_step_invariant_sterms [OF oquality_increases_nsqn_fresh aodv_wf
313
+ oaodv_trans]
314
+ onl_oinvariant_sterms [OF aodv_wf oreceived_rreq_rrep_nsqn_fresh_inv]
315
+ onl_oinvariant_sterms [OF aodv_wf odsn_rrep]
316
+ simp del: One_nat_def, rule impI)
317
+ fix \<sigma> \<sigma>' p l
318
+ assume "(\<sigma>, p) \<in> oreachable (opaodv i) ?S ?U"
319
+ and "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
320
+ and pre:
321
+ "(l = PAodv-:6 \<or> l = PAodv-:7 \<or> l = PRrep-:0 \<or> l = PRrep-:1) \<and> sip (\<sigma> i) \<noteq> dip (\<sigma> i)
322
+ \<longrightarrow> dip (\<sigma> i) \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
323
+ \<and> dsn (\<sigma> i) \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i))
324
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i)) = dsn (\<sigma> i)
325
+ \<longrightarrow> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i))) \<le> hops (\<sigma> i)
326
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i))) = inv)"
327
+ and "other quality_increases {i} \<sigma> \<sigma>'"
328
+ and hyp: "(l=PAodv-:6 \<or> l=PAodv-:7 \<or> l=PRrep-:0 \<or> l=PRrep-:1) \<and> sip (\<sigma>' i) \<noteq> dip (\<sigma>' i)"
329
+ (is "?labels \<and> sip (\<sigma>' i) \<noteq> dip (\<sigma>' i)")
330
+ from this(4) have "\<sigma>' i = \<sigma> i" ..
331
+ with hyp have hyp': "?labels \<and> sip (\<sigma> i) \<noteq> dip (\<sigma> i)" by simp
332
+ show "dip (\<sigma>' i) \<in> kD (rt (\<sigma>' (sip (\<sigma>' i))))
333
+ \<and> dsn (\<sigma>' i) \<le> nsqn (rt (\<sigma>' (sip (\<sigma>' i)))) (dip (\<sigma>' i))
334
+ \<and> (nsqn (rt (\<sigma>' (sip (\<sigma>' i)))) (dip (\<sigma>' i)) = dsn (\<sigma>' i)
335
+ \<longrightarrow> the (dhops (rt (\<sigma>' (sip (\<sigma>' i)))) (dip (\<sigma>' i))) \<le> hops (\<sigma>' i)
336
+ \<or> the (flag (rt (\<sigma>' (sip (\<sigma>' i)))) (dip (\<sigma>' i))) = inv)"
337
+ proof (cases "sip (\<sigma> i) = i")
338
+ assume "sip (\<sigma> i) \<noteq> i"
339
+ from \<open>other quality_increases {i} \<sigma> \<sigma>'\<close>
340
+ have "quality_increases (\<sigma> (sip (\<sigma> i))) (\<sigma>' (sip (\<sigma>' i)))"
341
+ by (rule otherE) (clarsimp simp: \<open>sip (\<sigma> i) \<noteq> i\<close>)
342
+ moreover from \<open>(\<sigma>, p) \<in> oreachable (opaodv i) ?S ?U\<close> \<open>l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and hyp
343
+ have "1 \<le> dsn (\<sigma>' i)"
344
+ by (auto dest!: onl_oinvariant_weakenD [OF odsn_rrep]
345
+ simp add: seqlsimp \<open>\<sigma>' i = \<sigma> i\<close>)
346
+ moreover from \<open>sip (\<sigma> i) \<noteq> i\<close> hyp' and pre
347
+ have "dip (\<sigma>' i) \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
348
+ \<and> dsn (\<sigma>' i) \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma>' i))
349
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma>' i)) = dsn (\<sigma>' i)
350
+ \<longrightarrow> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma>' i))) \<le> hops (\<sigma>' i)
351
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma>' i))) = inv)"
352
+ by (auto simp: \<open>\<sigma>' i = \<sigma> i\<close>)
353
+ ultimately show ?thesis
354
+ by (rule quality_increases_rreq_rrep_props)
355
+ next
356
+ assume "sip (\<sigma> i) = i" thus ?thesis
357
+ using \<open>\<sigma>' i = \<sigma> i\<close> hyp and pre by auto
358
+ qed
359
+ qed (auto simp add: seqlsimp elim!: quality_increases_rreq_rrep_props')
360
+
361
+ lemma rerr_sip:
362
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
363
+ other quality_increases {i} \<rightarrow>)
364
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l).
365
+ l \<in> {PAodv-:8, PAodv-:9, PRerr-:0, PRerr-:1}
366
+ \<longrightarrow> (\<forall>ripc\<in>dom(dests (\<sigma> i)). ripc\<in>kD(rt (\<sigma> (sip (\<sigma> i)))) \<and>
367
+ the (dests (\<sigma> i) ripc) - 1 \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) ripc))"
368
+ (is "_ \<Turnstile> (?S, ?U \<rightarrow>) _")
369
+ proof -
370
+ { fix dests rip sip rsn and \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
371
+ assume qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
372
+ and *: "\<forall>rip\<in>dom dests. rip \<in> kD (rt (\<sigma> sip))
373
+ \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
374
+ and "dests rip = Some rsn"
375
+ from this(3) have "rip\<in>dom dests" by auto
376
+ with * and \<open>dests rip = Some rsn\<close> have "rip\<in>kD(rt (\<sigma> sip))"
377
+ and "rsn - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
378
+ by (auto dest!: bspec)
379
+ from qinc have "quality_increases (\<sigma> sip) (\<sigma>' sip)" ..
380
+ have "rip \<in> kD(rt (\<sigma>' sip)) \<and> rsn - 1 \<le> nsqn (rt (\<sigma>' sip)) rip"
381
+ proof
382
+ from \<open>rip\<in>kD(rt (\<sigma> sip))\<close> and \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
383
+ show "rip \<in> kD(rt (\<sigma>' sip))" ..
384
+ next
385
+ from \<open>rip\<in>kD(rt (\<sigma> sip))\<close> and \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
386
+ have "nsqn (rt (\<sigma> sip)) rip \<le> nsqn (rt (\<sigma>' sip)) rip" ..
387
+ with \<open>rsn - 1 \<le> nsqn (rt (\<sigma> sip)) rip\<close> show "rsn - 1 \<le> nsqn (rt (\<sigma>' sip)) rip"
388
+ by (rule le_trans)
389
+ qed
390
+ } note partial = this
391
+
392
+ show ?thesis
393
+ by (inv_cterms inv add: oseq_step_invariant_sterms [OF oquality_increases_nsqn_fresh aodv_wf
394
+ oaodv_trans]
395
+ onl_oinvariant_sterms [OF aodv_wf oreceived_rreq_rrep_nsqn_fresh_inv]
396
+ other_quality_increases other_localD
397
+ simp del: One_nat_def, intro conjI)
398
+ (clarsimp simp del: One_nat_def split: if_split_asm option.split_asm, erule(2) partial)+
399
+ qed
400
+
401
+ lemma prerr_guard: "paodv i \<TTurnstile>
402
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l = PRerr-:1
403
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>vD(rt \<xi>)
404
+ \<and> the (nhop (rt \<xi>) ip) = sip \<xi>
405
+ \<and> sqn (rt \<xi>) ip < the (dests \<xi> ip))))"
406
+ by (inv_cterms) (clarsimp split: option.split_asm if_split_asm)
407
+
408
+ lemmas oaddpreRT_welldefined =
409
+ open_seq_invariant [OF addpreRT_welldefined initiali_aodv oaodv_trans aodv_trans,
410
+ simplified seql_onl_swap,
411
+ THEN oinvariant_anyact]
412
+
413
+ lemmas odests_vD_inc_sqn =
414
+ open_seq_invariant [OF dests_vD_inc_sqn initiali_aodv oaodv_trans aodv_trans,
415
+ simplified seql_onl_swap,
416
+ THEN oinvariant_anyact]
417
+
418
+ lemmas oprerr_guard =
419
+ open_seq_invariant [OF prerr_guard initiali_aodv oaodv_trans aodv_trans,
420
+ simplified seql_onl_swap,
421
+ THEN oinvariant_anyact]
422
+
423
+ text \<open>Proposition 7.28\<close>
424
+
425
+ lemma seq_compare_next_hop':
426
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
427
+ other quality_increases {i} \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, _).
428
+ \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
429
+ in dip \<in> kD(rt (\<sigma> i)) \<and> nhip \<noteq> dip \<longrightarrow>
430
+ dip \<in> kD(rt (\<sigma> nhip)) \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> nhip)) dip)"
431
+ (is "_ \<Turnstile> (?S, ?U \<rightarrow>) _")
432
+ proof -
433
+
434
+ { fix nhop and \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
435
+ assume pre: "\<forall>dip\<in>kD(rt (\<sigma> i)). nhop dip \<noteq> dip \<longrightarrow>
436
+ dip\<in>kD(rt (\<sigma> (nhop dip))) \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (nhop dip))) dip"
437
+ and qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
438
+ have "\<forall>dip\<in>kD(rt (\<sigma> i)). nhop dip \<noteq> dip \<longrightarrow>
439
+ dip\<in>kD(rt (\<sigma>' (nhop dip))) \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
440
+ proof (intro ballI impI)
441
+ fix dip
442
+ assume "dip\<in>kD(rt (\<sigma> i))"
443
+ and "nhop dip \<noteq> dip"
444
+ with pre have "dip\<in>kD(rt (\<sigma> (nhop dip)))"
445
+ and "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (nhop dip))) dip"
446
+ by auto
447
+ from qinc have qinc_nhop: "quality_increases (\<sigma> (nhop dip)) (\<sigma>' (nhop dip))" ..
448
+ with \<open>dip\<in>kD(rt (\<sigma> (nhop dip)))\<close> have "dip\<in>kD (rt (\<sigma>' (nhop dip)))" ..
449
+
450
+ moreover have "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
451
+ proof -
452
+ from \<open>dip\<in>kD(rt (\<sigma> (nhop dip)))\<close> qinc_nhop
453
+ have "nsqn (rt (\<sigma> (nhop dip))) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip" ..
454
+ with \<open>nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (nhop dip))) dip\<close> show ?thesis
455
+ by simp
456
+ qed
457
+
458
+ ultimately show "dip\<in>kD(rt (\<sigma>' (nhop dip)))
459
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip" ..
460
+ qed
461
+ } note basic = this
462
+
463
+ { fix nhop and \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
464
+ assume pre: "\<forall>dip\<in>kD(rt (\<sigma> i)). nhop dip \<noteq> dip \<longrightarrow> dip\<in>kD(rt (\<sigma> (nhop dip)))
465
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (nhop dip))) dip"
466
+ and ndest: "\<forall>ripc\<in>dom (dests (\<sigma> i)). ripc \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
467
+ \<and> the (dests (\<sigma> i) ripc) - 1 \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) ripc"
468
+ and issip: "\<forall>ip\<in>dom (dests (\<sigma> i)). nhop ip = sip (\<sigma> i)"
469
+ and qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
470
+ have "\<forall>dip\<in>kD(rt (\<sigma> i)). nhop dip \<noteq> dip \<longrightarrow> dip \<in> kD (rt (\<sigma>' (nhop dip)))
471
+ \<and> nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
472
+ proof (intro ballI impI)
473
+ fix dip
474
+ assume "dip\<in>kD(rt (\<sigma> i))"
475
+ and "nhop dip \<noteq> dip"
476
+ with pre and qinc have "dip\<in>kD(rt (\<sigma>' (nhop dip)))"
477
+ and "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
478
+ by (auto dest!: basic)
479
+
480
+ have "nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
481
+ proof (cases "dip\<in>dom (dests (\<sigma> i))")
482
+ assume "dip\<in>dom (dests (\<sigma> i))"
483
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> obtain dsn where "dests (\<sigma> i) dip = Some dsn"
484
+ by auto
485
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> have "nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip = dsn - 1"
486
+ by (rule nsqn_invalidate_eq)
487
+ moreover have "dsn - 1 \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
488
+ proof -
489
+ from \<open>dests (\<sigma> i) dip = Some dsn\<close> have "the (dests (\<sigma> i) dip) = dsn" by simp
490
+ with ndest and \<open>dip\<in>dom (dests (\<sigma> i))\<close> have "dip \<in> kD (rt (\<sigma> (sip (\<sigma> i))))"
491
+ "dsn - 1 \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) dip"
492
+ by auto
493
+ moreover from issip and \<open>dip\<in>dom (dests (\<sigma> i))\<close> have "nhop dip = sip (\<sigma> i)" ..
494
+ ultimately have "dip \<in> kD (rt (\<sigma> (nhop dip)))"
495
+ and "dsn - 1 \<le> nsqn (rt (\<sigma> (nhop dip))) dip" by auto
496
+ with qinc show "dsn - 1 \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
497
+ by simp (metis kD_nsqn_quality_increases_trans)
498
+ qed
499
+ ultimately show ?thesis by simp
500
+ next
501
+ assume "dip \<notin> dom (dests (\<sigma> i))"
502
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close>
503
+ have "nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip = nsqn (rt (\<sigma> i)) dip"
504
+ by (rule nsqn_invalidate_other)
505
+ with \<open>nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip\<close> show ?thesis by simp
506
+ qed
507
+ with \<open>dip\<in>kD(rt (\<sigma>' (nhop dip)))\<close>
508
+ show "dip \<in> kD (rt (\<sigma>' (nhop dip)))
509
+ \<and> nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip" ..
510
+ qed
511
+ } note basic_prerr = this
512
+
513
+ { fix \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
514
+ assume a1: "\<forall>dip\<in>kD(rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
515
+ \<longrightarrow> dip\<in>kD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
516
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))) dip"
517
+ and a2: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
518
+ have "\<forall>dip\<in>kD(rt (\<sigma> i)).
519
+ the (nhop (update (rt (\<sigma> i)) (sip (\<sigma> i)) (0, unk, val, Suc 0, sip (\<sigma> i), {})) dip) \<noteq> dip \<longrightarrow>
520
+ dip\<in>kD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) (sip (\<sigma> i))
521
+ (0, unk, val, Suc 0, sip (\<sigma> i), {}))
522
+ dip)))) \<and>
523
+ nsqn (update (rt (\<sigma> i)) (sip (\<sigma> i)) (0, unk, val, Suc 0, sip (\<sigma> i), {})) dip
524
+ \<le> nsqn (rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) (sip (\<sigma> i))
525
+ (0, unk, val, Suc 0, sip (\<sigma> i), {}))
526
+ dip))))
527
+ dip" (is "\<forall>dip\<in>kD(rt (\<sigma> i)). ?P dip")
528
+ proof
529
+ fix dip
530
+ assume "dip\<in>kD(rt (\<sigma> i))"
531
+ with a1 and a2
532
+ have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip \<longrightarrow> dip\<in>kD(rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))
533
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))) dip"
534
+ by - (drule(1) basic, auto)
535
+ thus "?P dip" by (cases "dip = sip (\<sigma> i)") auto
536
+ qed
537
+ } note nhop_update_sip = this
538
+
539
+ { fix \<sigma> \<sigma>' oip sip osn hops
540
+ assume pre: "\<forall>dip\<in>kD (rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
541
+ \<longrightarrow> dip\<in>kD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
542
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))) dip"
543
+ and qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
544
+ and *: "sip \<noteq> oip \<longrightarrow> oip\<in>kD(rt (\<sigma> sip))
545
+ \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
546
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
547
+ \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops
548
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv)"
549
+ from pre and qinc
550
+ have pre': "\<forall>dip\<in>kD (rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
551
+ \<longrightarrow> dip\<in>kD(rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))
552
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))) dip"
553
+ by (rule basic)
554
+ have "(the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) oip) \<noteq> oip
555
+ \<longrightarrow> oip\<in>kD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
556
+ (osn, kno, val, Suc hops, sip, {})) oip))))
557
+ \<and> nsqn (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) oip
558
+ \<le> nsqn (rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
559
+ (osn, kno, val, Suc hops, sip, {})) oip)))) oip)"
560
+ (is "?nhop_not_oip \<longrightarrow> ?oip_in_kD \<and> ?nsqn_le_nsqn")
561
+ proof (rule, split update_rt_split_asm)
562
+ assume "rt (\<sigma> i) = update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})"
563
+ and "the (nhop (rt (\<sigma> i)) oip) \<noteq> oip"
564
+ with pre' show "?oip_in_kD \<and> ?nsqn_le_nsqn" by auto
565
+ next
566
+ assume rtnot: "rt (\<sigma> i) \<noteq> update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})"
567
+ and notoip: ?nhop_not_oip
568
+ with * qinc have ?oip_in_kD
569
+ by auto
570
+ moreover with * pre qinc rtnot notoip have ?nsqn_le_nsqn
571
+ by simp (metis kD_nsqn_quality_increases_trans)
572
+ ultimately show "?oip_in_kD \<and> ?nsqn_le_nsqn" ..
573
+ qed
574
+ } note update1 = this
575
+
576
+ { fix \<sigma> \<sigma>' oip sip osn hops
577
+ assume pre: "\<forall>dip\<in>kD (rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
578
+ \<longrightarrow> dip\<in>kD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
579
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))) dip"
580
+ and qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
581
+ and *: "sip \<noteq> oip \<longrightarrow> oip\<in>kD(rt (\<sigma> sip))
582
+ \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
583
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
584
+ \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops
585
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv)"
586
+ from pre and qinc
587
+ have pre': "\<forall>dip\<in>kD (rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
588
+ \<longrightarrow> dip\<in>kD(rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))
589
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))) dip"
590
+ by (rule basic)
591
+ have "\<forall>dip\<in>kD(rt (\<sigma> i)).
592
+ the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip) \<noteq> dip
593
+ \<longrightarrow> dip\<in>kD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
594
+ (osn, kno, val, Suc hops, sip, {})) dip))))
595
+ \<and> nsqn (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip
596
+ \<le> nsqn (rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
597
+ (osn, kno, val, Suc hops, sip, {})) dip)))) dip"
598
+ (is "\<forall>dip\<in>kD(rt (\<sigma> i)). _ \<longrightarrow> ?dip_in_kD dip \<and> ?nsqn_le_nsqn dip")
599
+ proof (intro ballI impI, split update_rt_split_asm)
600
+ fix dip
601
+ assume "dip\<in>kD(rt (\<sigma> i))"
602
+ and "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip"
603
+ and "rt (\<sigma> i) = update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})"
604
+ with pre' show "?dip_in_kD dip \<and> ?nsqn_le_nsqn dip" by simp
605
+ next
606
+ fix dip
607
+ assume "dip\<in>kD(rt (\<sigma> i))"
608
+ and notdip: "the (nhop (update (rt (\<sigma> i)) oip
609
+ (osn, kno, val, Suc hops, sip, {})) dip) \<noteq> dip"
610
+ and rtnot: "rt (\<sigma> i) \<noteq> update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})"
611
+ show "?dip_in_kD dip \<and> ?nsqn_le_nsqn dip"
612
+ proof (cases "dip = oip")
613
+ assume "dip \<noteq> oip"
614
+ with pre' \<open>dip\<in>kD(rt (\<sigma> i))\<close> notdip
615
+ show ?thesis by clarsimp
616
+ next
617
+ assume "dip = oip"
618
+ with rtnot qinc \<open>dip\<in>kD(rt (\<sigma> i))\<close> notdip *
619
+ have "?dip_in_kD dip"
620
+ by simp (metis kD_quality_increases)
621
+ moreover from \<open>dip = oip\<close> rtnot qinc \<open>dip\<in>kD(rt (\<sigma> i))\<close> notdip *
622
+ have "?nsqn_le_nsqn dip" by simp (metis kD_nsqn_quality_increases_trans)
623
+ ultimately show ?thesis ..
624
+ qed
625
+ qed
626
+ } note update2 = this
627
+
628
+ have "opaodv i \<Turnstile> (?S, ?U \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, _).
629
+ \<forall>dip \<in> kD(rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
630
+ \<longrightarrow> dip \<in> kD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
631
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))) dip)"
632
+ by (inv_cterms inv add: oseq_step_invariant_sterms [OF oquality_increases_nsqn_fresh aodv_wf
633
+ oaodv_trans]
634
+ onl_oinvariant_sterms [OF aodv_wf oaddpreRT_welldefined]
635
+ onl_oinvariant_sterms [OF aodv_wf odests_vD_inc_sqn]
636
+ onl_oinvariant_sterms [OF aodv_wf oprerr_guard]
637
+ onl_oinvariant_sterms [OF aodv_wf rreq_sip]
638
+ onl_oinvariant_sterms [OF aodv_wf rrep_sip]
639
+ onl_oinvariant_sterms [OF aodv_wf rerr_sip]
640
+ other_quality_increases
641
+ other_localD
642
+ solve: basic basic_prerr
643
+ simp add: seqlsimp nsqn_invalidate nhop_update_sip
644
+ simp del: One_nat_def)
645
+ (rule conjI, erule(2) update1, erule(2) update2)+
646
+
647
+ thus ?thesis unfolding Let_def by auto
648
+ qed
649
+
650
+ text \<open>Proposition 7.30\<close>
651
+
652
+ lemmas okD_unk_or_atleast_one =
653
+ open_seq_invariant [OF kD_unk_or_atleast_one initiali_aodv,
654
+ simplified seql_onl_swap]
655
+
656
+ lemmas ozero_seq_unk_hops_one =
657
+ open_seq_invariant [OF zero_seq_unk_hops_one initiali_aodv,
658
+ simplified seql_onl_swap]
659
+
660
+ lemma oreachable_fresh_okD_unk_or_atleast_one:
661
+ fixes dip
662
+ assumes "(\<sigma>, p) \<in> oreachable (opaodv i)
663
+ (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m
664
+ \<and> msg_zhops m)))
665
+ (other quality_increases {i})"
666
+ and "dip\<in>kD(rt (\<sigma> i))"
667
+ shows "\<pi>\<^sub>3(the (rt (\<sigma> i) dip)) = unk \<or> 1 \<le> \<pi>\<^sub>2(the (rt (\<sigma> i) dip))"
668
+ (is "?P dip")
669
+ proof -
670
+ have "\<exists>l. l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p" by (metis aodv_ex_label)
671
+ with assms(1) have "\<forall>dip\<in>kD (rt (\<sigma> i)). ?P dip"
672
+ by - (drule oinvariant_weakenD [OF okD_unk_or_atleast_one [OF oaodv_trans aodv_trans]],
673
+ auto dest!: otherwith_actionD onlD simp: seqlsimp)
674
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> show ?thesis by simp
675
+ qed
676
+
677
+ lemma oreachable_fresh_ozero_seq_unk_hops_one:
678
+ fixes dip
679
+ assumes "(\<sigma>, p) \<in> oreachable (opaodv i)
680
+ (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m
681
+ \<and> msg_zhops m)))
682
+ (other quality_increases {i})"
683
+ and "dip\<in>kD(rt (\<sigma> i))"
684
+ shows "sqn (rt (\<sigma> i)) dip = 0 \<longrightarrow>
685
+ sqnf (rt (\<sigma> i)) dip = unk
686
+ \<and> the (dhops (rt (\<sigma> i)) dip) = 1
687
+ \<and> the (nhop (rt (\<sigma> i)) dip) = dip"
688
+ (is "?P dip")
689
+ proof -
690
+ have "\<exists>l. l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p" by (metis aodv_ex_label)
691
+ with assms(1) have "\<forall>dip\<in>kD (rt (\<sigma> i)). ?P dip"
692
+ by - (drule oinvariant_weakenD [OF ozero_seq_unk_hops_one [OF oaodv_trans aodv_trans]],
693
+ auto dest!: onlD otherwith_actionD simp: seqlsimp)
694
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> show ?thesis by simp
695
+ qed
696
+
697
+ lemma seq_nhop_quality_increases':
698
+ shows "opaodv i \<Turnstile> (otherwith ((=)) {i}
699
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
700
+ other quality_increases {i} \<rightarrow>)
701
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, _). \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
702
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip))
703
+ \<and> nhip \<noteq> dip
704
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
705
+ (is "_ \<Turnstile> (?S i, _ \<rightarrow>) _")
706
+ proof -
707
+ have weaken:
708
+ "\<And>p I Q R P. p \<Turnstile> (otherwith quality_increases I (orecvmsg Q), other quality_increases I \<rightarrow>) P
709
+ \<Longrightarrow> p \<Turnstile> (otherwith ((=)) I (orecvmsg (\<lambda>\<sigma> m. Q \<sigma> m \<and> R \<sigma> m)), other quality_increases I \<rightarrow>) P"
710
+ by auto
711
+ {
712
+ fix i a and \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
713
+ assume a1: "\<forall>dip. dip\<in>vD(rt (\<sigma> i))
714
+ \<and> dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
715
+ \<and> (the (nhop (rt (\<sigma> i)) dip)) \<noteq> dip
716
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
717
+ and ow: "?S i \<sigma> \<sigma>' a"
718
+ have "\<forall>dip. dip\<in>vD(rt (\<sigma> i))
719
+ \<and> dip\<in>vD (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))
720
+ \<and> (the (nhop (rt (\<sigma> i)) dip)) \<noteq> dip
721
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))"
722
+ proof clarify
723
+ fix dip
724
+ assume a2: "dip\<in>vD(rt (\<sigma> i))"
725
+ and a3: "dip\<in>vD (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))"
726
+ and a4: "(the (nhop (rt (\<sigma> i)) dip)) \<noteq> dip"
727
+ from ow have "\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j" by auto
728
+ show "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))"
729
+ proof (cases "(the (nhop (rt (\<sigma> i)) dip)) = i")
730
+ assume "(the (nhop (rt (\<sigma> i)) dip)) = i"
731
+ with \<open>dip \<in> vD(rt (\<sigma> i))\<close> have "dip \<in> vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))" by simp
732
+ with a1 a2 a4 have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))" by simp
733
+ with \<open>(the (nhop (rt (\<sigma> i)) dip)) = i\<close> have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> i)" by simp
734
+ hence False by simp
735
+ thus ?thesis ..
736
+ next
737
+ assume "(the (nhop (rt (\<sigma> i)) dip)) \<noteq> i"
738
+ with \<open>\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j\<close>
739
+ have *: "\<sigma> (the (nhop (rt (\<sigma> i)) dip)) = \<sigma>' (the (nhop (rt (\<sigma> i)) dip))" by simp
740
+ with \<open>dip\<in>vD (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))\<close>
741
+ have "dip\<in>vD (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))" by simp
742
+ with a1 a2 a4 have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))" by simp
743
+ with * show ?thesis by simp
744
+ qed
745
+ qed
746
+ } note basic = this
747
+
748
+ { fix \<sigma> \<sigma>' a dip sip i
749
+ assume a1: "\<forall>dip. dip\<in>vD(rt (\<sigma> i))
750
+ \<and> dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
751
+ \<and> the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
752
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
753
+ and ow: "?S i \<sigma> \<sigma>' a"
754
+ have "\<forall>dip. dip\<in>vD(update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {}))
755
+ \<and> dip\<in>vD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip))))
756
+ \<and> the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip) \<noteq> dip
757
+ \<longrightarrow> update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})
758
+ \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip)))"
759
+ proof clarify
760
+ fix dip
761
+ assume a2: "dip\<in>vD (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {}))"
762
+ and a3: "dip\<in>vD(rt (\<sigma>' (the (nhop
763
+ (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip))))"
764
+ and a4: "the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip) \<noteq> dip"
765
+ show "update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})
766
+ \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip)))"
767
+ proof (cases "dip = sip")
768
+ assume "dip = sip"
769
+ with \<open>the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip) \<noteq> dip\<close>
770
+ have False by simp
771
+ thus ?thesis ..
772
+ next
773
+ assume [simp]: "dip \<noteq> sip"
774
+ from a2 have "dip\<in>vD(rt (\<sigma> i)) \<or> dip = sip"
775
+ by (rule vD_update_val)
776
+ with \<open>dip \<noteq> sip\<close> have "dip\<in>vD(rt (\<sigma> i))" by simp
777
+ moreover from a3 have "dip\<in>vD(rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))" by simp
778
+ moreover from a4 have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip" by simp
779
+ ultimately have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))"
780
+ using a1 ow by - (drule(1) basic, simp)
781
+ with \<open>dip \<noteq> sip\<close> show ?thesis
782
+ by - (erule rt_strictly_fresher_update_other, simp)
783
+ qed
784
+ qed
785
+ } note update_0_unk = this
786
+
787
+ { fix \<sigma> a \<sigma>' nhop
788
+ assume pre: "\<forall>dip. dip\<in>vD(rt (\<sigma> i)) \<and> dip\<in>vD(rt (\<sigma> (nhop dip))) \<and> nhop dip \<noteq> dip
789
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (nhop dip))"
790
+ and ow: "?S i \<sigma> \<sigma>' a"
791
+ have "\<forall>dip. dip \<in> vD (invalidate (rt (\<sigma> i)) (dests (\<sigma> i)))
792
+ \<and> dip \<in> vD (rt (\<sigma>' (nhop dip))) \<and> nhop dip \<noteq> dip
793
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (nhop dip))"
794
+ proof clarify
795
+ fix dip
796
+ assume "dip\<in>vD(invalidate (rt (\<sigma> i)) (dests (\<sigma> i)))"
797
+ and "dip\<in>vD(rt (\<sigma>' (nhop dip)))"
798
+ and "nhop dip \<noteq> dip"
799
+ from this(1) have "dip\<in>vD (rt (\<sigma> i))"
800
+ by (clarsimp dest!: vD_invalidate_vD_not_dests)
801
+ moreover from ow have "\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j" by auto
802
+ ultimately have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (nhop dip))"
803
+ using pre \<open>dip \<in> vD (rt (\<sigma>' (nhop dip)))\<close> \<open>nhop dip \<noteq> dip\<close>
804
+ by metis
805
+ with \<open>\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j\<close> show "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (nhop dip))"
806
+ by (metis rt_strictly_fresher_irefl)
807
+ qed
808
+ } note invalidate = this
809
+
810
+ { fix \<sigma> a \<sigma>' dip oip osn sip hops i
811
+ assume pre: "\<forall>dip. dip \<in> vD (rt (\<sigma> i))
812
+ \<and> dip \<in> vD (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
813
+ \<and> the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
814
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
815
+ and ow: "?S i \<sigma> \<sigma>' a"
816
+ and "Suc 0 \<le> osn"
817
+ and a6: "sip \<noteq> oip \<longrightarrow> oip \<in> kD (rt (\<sigma> sip))
818
+ \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
819
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
820
+ \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops
821
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv)"
822
+ and after: "\<sigma>' i = \<sigma> i\<lparr>rt := update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})\<rparr>"
823
+ have "\<forall>dip. dip \<in> vD (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {}))
824
+ \<and> dip \<in> vD (rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
825
+ (osn, kno, val, Suc hops, sip, {})) dip))))
826
+ \<and> the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip) \<noteq> dip
827
+ \<longrightarrow> update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})
828
+ \<sqsubset>\<^bsub>dip\<^esub>
829
+ rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip)))"
830
+ proof clarify
831
+ fix dip
832
+ assume a2: "dip\<in>vD(update (rt (\<sigma> i)) oip (osn, kno, val, Suc (hops), sip, {}))"
833
+ and a3: "dip\<in>vD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
834
+ (osn, kno, val, Suc hops, sip, {})) dip))))"
835
+ and a4: "the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip) \<noteq> dip"
836
+ from ow have a5: "\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j" by auto
837
+ show "update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})
838
+ \<sqsubset>\<^bsub>dip\<^esub>
839
+ rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip)))"
840
+ (is "?rt1 \<sqsubset>\<^bsub>dip\<^esub> ?rt2 dip")
841
+ proof (cases "?rt1 = rt (\<sigma> i)")
842
+ assume nochange [simp]:
843
+ "update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {}) = rt (\<sigma> i)"
844
+
845
+ from after have "\<sigma>' i = \<sigma> i" by simp
846
+ with a5 have "\<forall>j. \<sigma> j = \<sigma>' j" by metis
847
+
848
+ from a2 have "dip\<in>vD (rt (\<sigma> i))" by simp
849
+ moreover from a3 have "dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))"
850
+ using nochange and \<open>\<forall>j. \<sigma> j = \<sigma>' j\<close> by clarsimp
851
+ moreover from a4 have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip" by simp
852
+ ultimately have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
853
+ using pre by simp
854
+
855
+ hence "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))"
856
+ using \<open>\<forall>j. \<sigma> j = \<sigma>' j\<close> by simp
857
+ thus "?thesis" by simp
858
+ next
859
+ assume change: "?rt1 \<noteq> rt (\<sigma> i)"
860
+ from after a2 have "dip\<in>kD(rt (\<sigma>' i))" by auto
861
+ show ?thesis
862
+ proof (cases "dip = oip")
863
+ assume "dip \<noteq> oip"
864
+
865
+ with a2 have "dip\<in>vD (rt (\<sigma> i))" by auto
866
+ moreover with a3 a5 after and \<open>dip \<noteq> oip\<close>
867
+ have "dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))"
868
+ by simp metis
869
+ moreover from a4 and \<open>dip \<noteq> oip\<close> have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip" by simp
870
+ ultimately have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
871
+ using pre by simp
872
+
873
+ with after and a5 and \<open>dip \<noteq> oip\<close> show ?thesis
874
+ by simp (metis rt_strictly_fresher_update_other
875
+ rt_strictly_fresher_irefl)
876
+ next
877
+ assume "dip = oip"
878
+
879
+ with a4 and change have "sip \<noteq> oip" by simp
880
+ with a6 have "oip\<in>kD(rt (\<sigma> sip))"
881
+ and "osn \<le> nsqn (rt (\<sigma> sip)) oip" by auto
882
+
883
+ from a3 change \<open>dip = oip\<close> have "oip\<in>vD(rt (\<sigma>' sip))" by simp
884
+ hence "the (flag (rt (\<sigma>' sip)) oip) = val" by simp
885
+
886
+ from \<open>oip\<in>kD(rt (\<sigma> sip))\<close>
887
+ have "osn < nsqn (rt (\<sigma>' sip)) oip \<or> (osn = nsqn (rt (\<sigma>' sip)) oip
888
+ \<and> the (dhops (rt (\<sigma>' sip)) oip) \<le> hops)"
889
+ proof
890
+ assume "oip\<in>vD(rt (\<sigma> sip))"
891
+ hence "the (flag (rt (\<sigma> sip)) oip) = val" by simp
892
+ with a6 \<open>sip \<noteq> oip\<close> have "nsqn (rt (\<sigma> sip)) oip = osn \<longrightarrow>
893
+ the (dhops (rt (\<sigma> sip)) oip) \<le> hops"
894
+ by simp
895
+ show ?thesis
896
+ proof (cases "sip = i")
897
+ assume "sip \<noteq> i"
898
+ with a5 have "\<sigma> sip = \<sigma>' sip" by simp
899
+ with \<open>osn \<le> nsqn (rt (\<sigma> sip)) oip\<close>
900
+ and \<open>nsqn (rt (\<sigma> sip)) oip = osn \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops\<close>
901
+ show ?thesis by auto
902
+ next
903
+ \<comment> \<open>alternative to using @{text sip_not_ip}\<close>
904
+ assume [simp]: "sip = i"
905
+ have "?rt1 = rt (\<sigma> i)"
906
+ proof (rule update_cases_kD, simp_all)
907
+ from \<open>Suc 0 \<le> osn\<close> show "0 < osn" by simp
908
+ next
909
+ from \<open>oip\<in>kD(rt (\<sigma> sip))\<close> and \<open>sip = i\<close> show "oip\<in>kD(rt (\<sigma> i))"
910
+ by simp
911
+ next
912
+ assume "sqn (rt (\<sigma> i)) oip < osn"
913
+ also from \<open>osn \<le> nsqn (rt (\<sigma> sip)) oip\<close>
914
+ have "... \<le> nsqn (rt (\<sigma> i)) oip" by simp
915
+ also have "... \<le> sqn (rt (\<sigma> i)) oip"
916
+ by (rule nsqn_sqn)
917
+ finally have "sqn (rt (\<sigma> i)) oip < sqn (rt (\<sigma> i)) oip" .
918
+ hence False by simp
919
+ thus "(\<lambda>a. if a = oip
920
+ then Some (osn, kno, val, Suc hops, i, \<pi>\<^sub>7 (the (rt (\<sigma> i) oip)))
921
+ else rt (\<sigma> i) a) = rt (\<sigma> i)" ..
922
+ next
923
+ assume "sqn (rt (\<sigma> i)) oip = osn"
924
+ and "Suc hops < the (dhops (rt (\<sigma> i)) oip)"
925
+ from this(1) and \<open>oip \<in> vD (rt (\<sigma> sip))\<close> have "nsqn (rt (\<sigma> i)) oip = osn"
926
+ by simp
927
+ with \<open>nsqn (rt (\<sigma> sip)) oip = osn \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops\<close>
928
+ have "the (dhops (rt (\<sigma> i)) oip) \<le> hops" by simp
929
+ with \<open>Suc hops < the (dhops (rt (\<sigma> i)) oip)\<close> have False by simp
930
+ thus "(\<lambda>a. if a = oip
931
+ then Some (osn, kno, val, Suc hops, i, \<pi>\<^sub>7 (the (rt (\<sigma> i) oip)))
932
+ else rt (\<sigma> i) a) = rt (\<sigma> i)" ..
933
+ next
934
+ assume "the (flag (rt (\<sigma> i)) oip) = inv"
935
+ with \<open>the (flag (rt (\<sigma> sip)) oip) = val\<close> have False by simp
936
+ thus "(\<lambda>a. if a = oip
937
+ then Some (osn, kno, val, Suc hops, i, \<pi>\<^sub>7 (the (rt (\<sigma> i) oip)))
938
+ else rt (\<sigma> i) a) = rt (\<sigma> i)" ..
939
+ next
940
+ from \<open>oip\<in>kD(rt (\<sigma> sip))\<close>
941
+ show "(\<lambda>a. if a = oip then Some (the (rt (\<sigma> i) oip)) else rt (\<sigma> i) a) = rt (\<sigma> i)"
942
+ by (auto dest!: kD_Some)
943
+ qed
944
+ with change have False ..
945
+ thus ?thesis ..
946
+ qed
947
+ next
948
+ assume "oip\<in>iD(rt (\<sigma> sip))"
949
+ with \<open>the (flag (rt (\<sigma>' sip)) oip) = val\<close> and a5 have "sip = i"
950
+ by (metis f.distinct(1) iD_flag_is_inv)
951
+ from \<open>oip\<in>iD(rt (\<sigma> sip))\<close> have "the (flag (rt (\<sigma> sip)) oip) = inv" by auto
952
+ with \<open>sip = i\<close> \<open>Suc 0 \<le> osn\<close> change after \<open>oip\<in>kD(rt (\<sigma> sip))\<close>
953
+ have "nsqn (rt (\<sigma> sip)) oip < nsqn (rt (\<sigma>' sip)) oip"
954
+ unfolding update_def
955
+ by (clarsimp split: option.split_asm if_split_asm)
956
+ (auto simp: sqn_def)
957
+ with \<open>osn \<le> nsqn (rt (\<sigma> sip)) oip\<close> have "osn < nsqn (rt (\<sigma>' sip)) oip"
958
+ by simp
959
+ thus ?thesis ..
960
+ qed
961
+ thus ?thesis
962
+ proof
963
+ assume osnlt: "osn < nsqn (rt (\<sigma>' sip)) oip"
964
+ from \<open>dip\<in>kD(rt (\<sigma>' i))\<close> and \<open>dip = oip\<close> have "dip \<in> kD (?rt1)" by simp
965
+ moreover from a3 have "dip \<in> kD(?rt2 dip)" by simp
966
+ moreover have "nsqn ?rt1 dip < nsqn (?rt2 dip) dip"
967
+ proof -
968
+ have "nsqn ?rt1 oip = osn"
969
+ by (simp add: \<open>dip = oip\<close> nsqn_update_changed_kno_val [OF change [THEN not_sym]])
970
+ also have "... < nsqn (rt (\<sigma>' sip)) oip" using osnlt .
971
+ also have "... = nsqn (?rt2 oip) oip" by (simp add: change)
972
+ finally show ?thesis
973
+ using \<open>dip = oip\<close> by simp
974
+ qed
975
+ ultimately show ?thesis
976
+ by (rule rt_strictly_fresher_ltI)
977
+ next
978
+ assume osneq: "osn = nsqn (rt (\<sigma>' sip)) oip \<and> the (dhops (rt (\<sigma>' sip)) oip) \<le> hops"
979
+
980
+ have "oip\<in>kD(?rt1)" by simp
981
+ moreover from a3 \<open>dip = oip\<close> have "oip\<in>kD(?rt2 oip)" by simp
982
+
983
+ moreover have "nsqn ?rt1 oip = nsqn (?rt2 oip) oip"
984
+ proof -
985
+ from osneq have "osn = nsqn (rt (\<sigma>' sip)) oip" ..
986
+ also have "osn = nsqn ?rt1 oip"
987
+ by (simp add: \<open>dip = oip\<close> nsqn_update_changed_kno_val [OF change [THEN not_sym]])
988
+ also have "nsqn (rt (\<sigma>' sip)) oip = nsqn (?rt2 oip) oip"
989
+ by (simp add: change)
990
+ finally show ?thesis .
991
+ qed
992
+
993
+ moreover have "\<pi>\<^sub>5(the (?rt2 oip oip)) < \<pi>\<^sub>5(the (?rt1 oip))"
994
+ proof -
995
+ from osneq have "the (dhops (rt (\<sigma>' sip)) oip) \<le> hops" ..
996
+ moreover from \<open>oip \<in> vD (rt (\<sigma>' sip))\<close> have "oip\<in>kD(rt (\<sigma>' sip))" by auto
997
+ ultimately have "\<pi>\<^sub>5(the (rt (\<sigma>' sip) oip)) \<le> hops"
998
+ by (auto simp add: proj5_eq_dhops)
999
+ also from change after have "hops < \<pi>\<^sub>5(the (rt (\<sigma>' i) oip))"
1000
+ by (simp add: proj5_eq_dhops) (metis dhops_update_changed lessI)
1001
+ finally have "\<pi>\<^sub>5(the (rt (\<sigma>' sip) oip)) < \<pi>\<^sub>5(the (rt (\<sigma>' i) oip))" .
1002
+ with change after show ?thesis by simp
1003
+ qed
1004
+
1005
+ ultimately have "?rt1 \<sqsubset>\<^bsub>oip\<^esub> ?rt2 oip"
1006
+ by (rule rt_strictly_fresher_eqI)
1007
+ with \<open>dip = oip\<close> show ?thesis by simp
1008
+ qed
1009
+ qed
1010
+ qed
1011
+ qed
1012
+ } note rreq_rrep_update = this
1013
+
1014
+ have "opaodv i \<Turnstile> (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m
1015
+ \<and> msg_zhops m)),
1016
+ other quality_increases {i} \<rightarrow>)
1017
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V
1018
+ (\<lambda>(\<sigma>, _). \<forall>dip. dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
1019
+ \<and> the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
1020
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))"
1021
+ proof (inv_cterms inv add: onl_oinvariant_sterms [OF aodv_wf rreq_sip [THEN weaken]]
1022
+ onl_oinvariant_sterms [OF aodv_wf rrep_sip [THEN weaken]]
1023
+ onl_oinvariant_sterms [OF aodv_wf rerr_sip [THEN weaken]]
1024
+ onl_oinvariant_sterms [OF aodv_wf oosn_rreq [THEN weaken]]
1025
+ onl_oinvariant_sterms [OF aodv_wf odsn_rrep [THEN weaken]]
1026
+ onl_oinvariant_sterms [OF aodv_wf oaddpreRT_welldefined]
1027
+ solve: basic update_0_unk invalidate rreq_rrep_update
1028
+ simp add: seqlsimp)
1029
+ fix \<sigma> \<sigma>' p l
1030
+ assume or: "(\<sigma>, p) \<in> oreachable (opaodv i) (?S i) (other quality_increases {i})"
1031
+ and "other quality_increases {i} \<sigma> \<sigma>'"
1032
+ and ll: "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
1033
+ and pre: "\<forall>dip. dip\<in>vD (rt (\<sigma> i))
1034
+ \<and> dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
1035
+ \<and> the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
1036
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
1037
+ from this(1-2)
1038
+ have or': "(\<sigma>', p) \<in> oreachable (opaodv i) (?S i) (other quality_increases {i})"
1039
+ by - (rule oreachable_other')
1040
+
1041
+ from or and ll have next_hop: "\<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
1042
+ in dip \<in> kD(rt (\<sigma> i)) \<and> nhip \<noteq> dip
1043
+ \<longrightarrow> dip \<in> kD(rt (\<sigma> nhip))
1044
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> nhip)) dip"
1045
+ by (auto dest!: onl_oinvariant_weakenD [OF seq_compare_next_hop'])
1046
+
1047
+ from or and ll have unk_hops_one: "\<forall>dip\<in>kD (rt (\<sigma> i)). sqn (rt (\<sigma> i)) dip = 0
1048
+ \<longrightarrow> sqnf (rt (\<sigma> i)) dip = unk
1049
+ \<and> the (dhops (rt (\<sigma> i)) dip) = 1
1050
+ \<and> the (nhop (rt (\<sigma> i)) dip) = dip"
1051
+ by (auto dest!: onl_oinvariant_weakenD [OF ozero_seq_unk_hops_one
1052
+ [OF oaodv_trans aodv_trans]]
1053
+ otherwith_actionD
1054
+ simp: seqlsimp)
1055
+
1056
+ from \<open>other quality_increases {i} \<sigma> \<sigma>'\<close> have "\<sigma>' i = \<sigma> i" by auto
1057
+ hence "quality_increases (\<sigma> i) (\<sigma>' i)" by auto
1058
+ with \<open>other quality_increases {i} \<sigma> \<sigma>'\<close> have "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
1059
+ by - (erule otherE, metis singleton_iff)
1060
+
1061
+ show "\<forall>dip. dip \<in> vD (rt (\<sigma>' i))
1062
+ \<and> dip \<in> vD (rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip))))
1063
+ \<and> the (nhop (rt (\<sigma>' i)) dip) \<noteq> dip
1064
+ \<longrightarrow> rt (\<sigma>' i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip)))"
1065
+ proof clarify
1066
+ fix dip
1067
+ assume "dip\<in>vD(rt (\<sigma>' i))"
1068
+ and "dip\<in>vD(rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip))))"
1069
+ and "the (nhop (rt (\<sigma>' i)) dip) \<noteq> dip"
1070
+ from this(1) and \<open>\<sigma>' i = \<sigma> i\<close> have "dip\<in>vD(rt (\<sigma> i))"
1071
+ and "dip\<in>kD(rt (\<sigma> i))"
1072
+ by auto
1073
+
1074
+ from \<open>the (nhop (rt (\<sigma>' i)) dip) \<noteq> dip\<close> and \<open>\<sigma>' i = \<sigma> i\<close>
1075
+ have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip" (is "?nhip \<noteq> _") by simp
1076
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> and next_hop
1077
+ have "dip\<in>kD(rt (\<sigma> (?nhip)))"
1078
+ and nsqns: "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> ?nhip)) dip"
1079
+ by (auto simp: Let_def)
1080
+
1081
+ have "0 < sqn (rt (\<sigma> i)) dip"
1082
+ proof (rule neq0_conv [THEN iffD1, OF notI])
1083
+ assume "sqn (rt (\<sigma> i)) dip = 0"
1084
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> and unk_hops_one
1085
+ have "?nhip = dip" by simp
1086
+ with \<open>?nhip \<noteq> dip\<close> show False ..
1087
+ qed
1088
+ also have "... = nsqn (rt (\<sigma> i)) dip"
1089
+ by (rule vD_nsqn_sqn [OF \<open>dip\<in>vD(rt (\<sigma> i))\<close>, THEN sym])
1090
+ also have "... \<le> nsqn (rt (\<sigma> ?nhip)) dip"
1091
+ by (rule nsqns)
1092
+ also have "... \<le> sqn (rt (\<sigma> ?nhip)) dip"
1093
+ by (rule nsqn_sqn)
1094
+ finally have "0 < sqn (rt (\<sigma> ?nhip)) dip" .
1095
+
1096
+ have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' ?nhip)"
1097
+ proof (cases "dip\<in>vD(rt (\<sigma> ?nhip))")
1098
+ assume "dip\<in>vD(rt (\<sigma> ?nhip))"
1099
+ with pre \<open>dip\<in>vD(rt (\<sigma> i))\<close> and \<open>?nhip \<noteq> dip\<close>
1100
+ have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> ?nhip)" by auto
1101
+ moreover from \<open>\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)\<close>
1102
+ have "quality_increases (\<sigma> ?nhip) (\<sigma>' ?nhip)" ..
1103
+ ultimately show ?thesis
1104
+ using \<open>dip\<in>kD(rt (\<sigma> ?nhip))\<close>
1105
+ by (rule strictly_fresher_quality_increases_right)
1106
+ next
1107
+ assume "dip\<notin>vD(rt (\<sigma> ?nhip))"
1108
+ with \<open>dip\<in>kD(rt (\<sigma> ?nhip))\<close> have "dip\<in>iD(rt (\<sigma> ?nhip))" ..
1109
+ hence "the (flag (rt (\<sigma> ?nhip)) dip) = inv"
1110
+ by auto
1111
+ have "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> ?nhip)) dip"
1112
+ by (rule nsqns)
1113
+ also from \<open>dip\<in>iD(rt (\<sigma> ?nhip))\<close>
1114
+ have "... = sqn (rt (\<sigma> ?nhip)) dip - 1" ..
1115
+ also have "... < sqn (rt (\<sigma>' ?nhip)) dip"
1116
+ proof -
1117
+ from \<open>\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)\<close>
1118
+ have "quality_increases (\<sigma> ?nhip) (\<sigma>' ?nhip)" ..
1119
+ hence "\<forall>ip. sqn (rt (\<sigma> ?nhip)) ip \<le> sqn (rt (\<sigma>' ?nhip)) ip" by auto
1120
+ hence "sqn (rt (\<sigma> ?nhip)) dip \<le> sqn (rt (\<sigma>' ?nhip)) dip" ..
1121
+ with \<open>0 < sqn (rt (\<sigma> ?nhip)) dip\<close> show ?thesis by auto
1122
+ qed
1123
+ also have "... = nsqn (rt (\<sigma>' ?nhip)) dip"
1124
+ proof (rule vD_nsqn_sqn [THEN sym])
1125
+ from \<open>dip\<in>vD(rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip))))\<close> and \<open>\<sigma>' i = \<sigma> i\<close>
1126
+ show "dip\<in>vD(rt (\<sigma>' ?nhip))" by simp
1127
+ qed
1128
+ finally have "nsqn (rt (\<sigma> i)) dip < nsqn (rt (\<sigma>' ?nhip)) dip" .
1129
+
1130
+ moreover from \<open>dip\<in>vD(rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip))))\<close> and \<open>\<sigma>' i = \<sigma> i\<close>
1131
+ have "dip\<in>kD(rt (\<sigma>' ?nhip))" by auto
1132
+ ultimately show "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' ?nhip)"
1133
+ using \<open>dip\<in>kD(rt (\<sigma> i))\<close> by - (rule rt_strictly_fresher_ltI)
1134
+ qed
1135
+ with \<open>\<sigma>' i = \<sigma> i\<close> show "rt (\<sigma>' i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip)))"
1136
+ by simp
1137
+ qed
1138
+ qed
1139
+ thus ?thesis unfolding Let_def .
1140
+ qed
1141
+
1142
+ lemma seq_nhop_quality_increases:
1143
+ shows "opaodv i \<Turnstile> (otherwith ((=)) {i}
1144
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
1145
+ other quality_increases {i} \<rightarrow>)
1146
+ global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
1147
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
1148
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
1149
+ by (rule oinvariant_weakenE [OF seq_nhop_quality_increases']) (auto dest!: onlD)
1150
+
1151
+ end
formal/afp/AODV/Loop_Freedom.thy ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Loop_Freedom.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Routing graphs and loop freedom"
7
+
8
+ theory Loop_Freedom
9
+ imports Aodv_Predicates Fresher
10
+ begin
11
+
12
+ text \<open>Define the central theorem that relates an invariant over network states to the absence
13
+ of loops in the associate routing graph.\<close>
14
+
15
+ definition
16
+ rt_graph :: "(ip \<Rightarrow> state) \<Rightarrow> ip \<Rightarrow> ip rel"
17
+ where
18
+ "rt_graph \<sigma> = (\<lambda>dip.
19
+ {(ip, ip') | ip ip' dsn dsk hops pre.
20
+ ip \<noteq> dip \<and> rt (\<sigma> ip) dip = Some (dsn, dsk, val, hops, ip', pre)})"
21
+
22
+ text \<open>Given the state of a network @{term \<sigma>}, a routing graph for a given destination
23
+ ip address @{term dip} abstracts the details of routing tables into nodes
24
+ (ip addresses) and vertices (valid routes between ip addresses).\<close>
25
+
26
+ lemma rt_graphE [elim]:
27
+ fixes n dip ip ip'
28
+ assumes "(ip, ip') \<in> rt_graph \<sigma> dip"
29
+ shows "ip \<noteq> dip \<and> (\<exists>r. rt (\<sigma> ip) = r
30
+ \<and> (\<exists>dsn dsk hops pre. r dip = Some (dsn, dsk, val, hops, ip', pre)))"
31
+ using assms unfolding rt_graph_def by auto
32
+
33
+ lemma rt_graph_vD [dest]:
34
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> rt_graph \<sigma> dip \<Longrightarrow> dip \<in> vD(rt (\<sigma> ip))"
35
+ unfolding rt_graph_def vD_def by auto
36
+
37
+ lemma rt_graph_vD_trans [dest]:
38
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> (rt_graph \<sigma> dip)\<^sup>+ \<Longrightarrow> dip \<in> vD(rt (\<sigma> ip))"
39
+ by (erule converse_tranclE) auto
40
+
41
+ lemma rt_graph_not_dip [dest]:
42
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> rt_graph \<sigma> dip \<Longrightarrow> ip \<noteq> dip"
43
+ unfolding rt_graph_def by auto
44
+
45
+ lemma rt_graph_not_dip_trans [dest]:
46
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> (rt_graph \<sigma> dip)\<^sup>+ \<Longrightarrow> ip \<noteq> dip"
47
+ by (erule converse_tranclE) auto
48
+
49
+ text "NB: the property below cannot be lifted to the transitive closure"
50
+
51
+ lemma rt_graph_nhip_is_nhop [dest]:
52
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> rt_graph \<sigma> dip \<Longrightarrow> ip' = the (nhop (rt (\<sigma> ip)) dip)"
53
+ unfolding rt_graph_def by auto
54
+
55
+ theorem inv_to_loop_freedom:
56
+ assumes "\<forall>i dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
57
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
58
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip))"
59
+ shows "\<forall>dip. irrefl ((rt_graph \<sigma> dip)\<^sup>+)"
60
+ using assms proof (intro allI)
61
+ fix \<sigma> :: "ip \<Rightarrow> state" and dip
62
+ assume inv: "\<forall>ip dip.
63
+ let nhip = the (nhop (rt (\<sigma> ip)) dip)
64
+ in dip \<in> vD(rt (\<sigma> ip)) \<inter> vD(rt (\<sigma> nhip)) \<and>
65
+ nhip \<noteq> dip \<longrightarrow> rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)"
66
+ { fix ip ip'
67
+ assume "(ip, ip') \<in> (rt_graph \<sigma> dip)\<^sup>+"
68
+ and "dip \<in> vD(rt (\<sigma> ip'))"
69
+ and "ip' \<noteq> dip"
70
+ hence "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> ip')"
71
+ proof induction
72
+ fix nhip
73
+ assume "(ip, nhip) \<in> rt_graph \<sigma> dip"
74
+ and "dip \<in> vD(rt (\<sigma> nhip))"
75
+ and "nhip \<noteq> dip"
76
+ from \<open>(ip, nhip) \<in> rt_graph \<sigma> dip\<close> have "dip \<in> vD(rt (\<sigma> ip))"
77
+ and "nhip = the (nhop (rt (\<sigma> ip)) dip)"
78
+ by auto
79
+ from \<open>dip \<in> vD(rt (\<sigma> ip))\<close> and \<open>dip \<in> vD(rt (\<sigma> nhip))\<close>
80
+ have "dip \<in> vD(rt (\<sigma> ip)) \<inter> vD(rt (\<sigma> nhip))" ..
81
+ with \<open>nhip = the (nhop (rt (\<sigma> ip)) dip)\<close>
82
+ and \<open>nhip \<noteq> dip\<close>
83
+ and inv
84
+ show "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)"
85
+ by (clarsimp simp: Let_def)
86
+ next
87
+ fix nhip nhip'
88
+ assume "(ip, nhip) \<in> (rt_graph \<sigma> dip)\<^sup>+"
89
+ and "(nhip, nhip') \<in> rt_graph \<sigma> dip"
90
+ and IH: "\<lbrakk> dip \<in> vD(rt (\<sigma> nhip)); nhip \<noteq> dip \<rbrakk> \<Longrightarrow> rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)"
91
+ and "dip \<in> vD(rt (\<sigma> nhip'))"
92
+ and "nhip' \<noteq> dip"
93
+ from \<open>(nhip, nhip') \<in> rt_graph \<sigma> dip\<close> have 1: "dip \<in> vD(rt (\<sigma> nhip))"
94
+ and 2: "nhip \<noteq> dip"
95
+ and "nhip' = the (nhop (rt (\<sigma> nhip)) dip)"
96
+ by auto
97
+ from 1 2 have "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)" by (rule IH)
98
+ also have "rt (\<sigma> nhip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip')"
99
+ proof -
100
+ from \<open>dip \<in> vD(rt (\<sigma> nhip))\<close> and \<open>dip \<in> vD(rt (\<sigma> nhip'))\<close>
101
+ have "dip \<in> vD(rt (\<sigma> nhip)) \<inter> vD(rt (\<sigma> nhip'))" ..
102
+ with \<open>nhip' \<noteq> dip\<close>
103
+ and \<open>nhip' = the (nhop (rt (\<sigma> nhip)) dip)\<close>
104
+ and inv
105
+ show "rt (\<sigma> nhip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip')"
106
+ by (clarsimp simp: Let_def)
107
+ qed
108
+ finally show "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip')" .
109
+ qed } note fresher = this
110
+
111
+ show "irrefl ((rt_graph \<sigma> dip)\<^sup>+)"
112
+ unfolding irrefl_def proof (intro allI notI)
113
+ fix ip
114
+ assume "(ip, ip) \<in> (rt_graph \<sigma> dip)\<^sup>+"
115
+ moreover then have "dip \<in> vD(rt (\<sigma> ip))"
116
+ and "ip \<noteq> dip"
117
+ by auto
118
+ ultimately have "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> ip)" by (rule fresher)
119
+ thus False by simp
120
+ qed
121
+ qed
122
+
123
+ end
formal/afp/AODV/OAodv.thy ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: OAodv.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "The `open' AODV model"
7
+
8
+ theory OAodv
9
+ imports Aodv AWN.OAWN_SOS_Labels AWN.OAWN_Convert
10
+ begin
11
+
12
+ text \<open>Definitions for stating and proving global network properties over individual processes.\<close>
13
+
14
+ definition \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' :: "((ip \<Rightarrow> state) \<times> ((state, msg, pseqp, pseqp label) seqp)) set"
15
+ where "\<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' \<equiv> {(\<lambda>i. aodv_init i, \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv)}"
16
+
17
+ abbreviation opaodv
18
+ :: "ip \<Rightarrow> ((ip \<Rightarrow> state) \<times> (state, msg, pseqp, pseqp label) seqp, msg seq_action) automaton"
19
+ where
20
+ "opaodv i \<equiv> \<lparr> init = \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V', trans = oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<rparr>"
21
+
22
+ lemma initiali_aodv [intro!, simp]: "initiali i (init (opaodv i)) (init (paodv i))"
23
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def by rule simp_all
24
+
25
+ lemma oaodv_control_within [simp]: "control_within \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (init (opaodv i))"
26
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def by (rule control_withinI) (auto simp del: \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps)
27
+
28
+ lemma \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_labels [simp]: "(\<sigma>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' \<Longrightarrow> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p = {PAodv-:0}"
29
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def by simp
30
+
31
+ lemma oaodv_init_kD_empty [simp]:
32
+ "(\<sigma>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' \<Longrightarrow> kD (rt (\<sigma> i)) = {}"
33
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def kD_def by simp
34
+
35
+ lemma oaodv_init_vD_empty [simp]:
36
+ "(\<sigma>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' \<Longrightarrow> vD (rt (\<sigma> i)) = {}"
37
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def vD_def by simp
38
+
39
+ lemma oaodv_trans: "trans (opaodv i) = oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
40
+ by simp
41
+
42
+ declare
43
+ oseq_invariant_ctermsI [OF aodv_wf oaodv_control_within aodv_simple_labels oaodv_trans, cterms_intros]
44
+ oseq_step_invariant_ctermsI [OF aodv_wf oaodv_control_within aodv_simple_labels oaodv_trans, cterms_intros]
45
+
46
+ end
47
+
formal/afp/AODV/Quality_Increases.thy ADDED
@@ -0,0 +1,456 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Quality_Increases.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "The quality increases predicate"
7
+
8
+ theory Quality_Increases
9
+ imports Aodv_Predicates Fresher
10
+ begin
11
+
12
+ definition quality_increases :: "state \<Rightarrow> state \<Rightarrow> bool"
13
+ where "quality_increases \<xi> \<xi>' \<equiv> (\<forall>dip\<in>kD(rt \<xi>). dip \<in> kD(rt \<xi>') \<and> rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>')
14
+ \<and> (\<forall>dip. sqn (rt \<xi>) dip \<le> sqn (rt \<xi>') dip)"
15
+
16
+ lemma quality_increasesI [intro!]:
17
+ assumes "\<And>dip. dip \<in> kD(rt \<xi>) \<Longrightarrow> dip \<in> kD(rt \<xi>')"
18
+ and "\<And>dip. \<lbrakk> dip \<in> kD(rt \<xi>); dip \<in> kD(rt \<xi>') \<rbrakk> \<Longrightarrow> rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>'"
19
+ and "\<And>dip. sqn (rt \<xi>) dip \<le> sqn (rt \<xi>') dip"
20
+ shows "quality_increases \<xi> \<xi>'"
21
+ unfolding quality_increases_def using assms by clarsimp
22
+
23
+ lemma quality_increasesE [elim]:
24
+ fixes dip
25
+ assumes "quality_increases \<xi> \<xi>'"
26
+ and "dip\<in>kD(rt \<xi>)"
27
+ and "\<lbrakk> dip \<in> kD(rt \<xi>'); rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>'; sqn (rt \<xi>) dip \<le> sqn (rt \<xi>') dip \<rbrakk> \<Longrightarrow> R dip \<xi> \<xi>'"
28
+ shows "R dip \<xi> \<xi>'"
29
+ using assms unfolding quality_increases_def by clarsimp
30
+
31
+ lemma quality_increases_rt_fresherD [dest]:
32
+ fixes ip
33
+ assumes "quality_increases \<xi> \<xi>'"
34
+ and "ip\<in>kD(rt \<xi>)"
35
+ shows "rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> rt \<xi>'"
36
+ using assms by auto
37
+
38
+ lemma quality_increases_sqnE [elim]:
39
+ fixes dip
40
+ assumes "quality_increases \<xi> \<xi>'"
41
+ and "sqn (rt \<xi>) dip \<le> sqn (rt \<xi>') dip \<Longrightarrow> R dip \<xi> \<xi>'"
42
+ shows "R dip \<xi> \<xi>'"
43
+ using assms unfolding quality_increases_def by clarsimp
44
+
45
+ lemma quality_increases_refl [intro, simp]: "quality_increases \<xi> \<xi>"
46
+ by rule simp_all
47
+
48
+ lemma strictly_fresher_quality_increases_right [elim]:
49
+ fixes \<sigma> \<sigma>' dip
50
+ assumes "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)"
51
+ and qinc: "quality_increases (\<sigma> nhip) (\<sigma>' nhip)"
52
+ and "dip\<in>kD(rt (\<sigma> nhip))"
53
+ shows "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' nhip)"
54
+ proof -
55
+ from qinc have "rt (\<sigma> nhip) \<sqsubseteq>\<^bsub>dip\<^esub> rt (\<sigma>' nhip)" using \<open>dip\<in>kD(rt (\<sigma> nhip))\<close>
56
+ by auto
57
+ with \<open>rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)\<close> show ?thesis ..
58
+ qed
59
+
60
+ lemma kD_quality_increases [elim]:
61
+ assumes "i\<in>kD(rt \<xi>)"
62
+ and "quality_increases \<xi> \<xi>'"
63
+ shows "i\<in>kD(rt \<xi>')"
64
+ using assms by auto
65
+
66
+ lemma kD_nsqn_quality_increases [elim]:
67
+ assumes "i\<in>kD(rt \<xi>)"
68
+ and "quality_increases \<xi> \<xi>'"
69
+ shows "i\<in>kD(rt \<xi>') \<and> nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i"
70
+ proof -
71
+ from assms have "i\<in>kD(rt \<xi>')" ..
72
+ moreover with assms have "rt \<xi> \<sqsubseteq>\<^bsub>i\<^esub> rt \<xi>'" by auto
73
+ ultimately have "nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i"
74
+ using \<open>i\<in>kD(rt \<xi>)\<close> by - (erule(2) rt_fresher_imp_nsqn_le)
75
+ with \<open>i\<in>kD(rt \<xi>')\<close> show ?thesis ..
76
+ qed
77
+
78
+ lemma nsqn_quality_increases [elim]:
79
+ assumes "i\<in>kD(rt \<xi>)"
80
+ and "quality_increases \<xi> \<xi>'"
81
+ shows "nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i"
82
+ using assms by (rule kD_nsqn_quality_increases [THEN conjunct2])
83
+
84
+ lemma kD_nsqn_quality_increases_trans [elim]:
85
+ assumes "i\<in>kD(rt \<xi>)"
86
+ and "s \<le> nsqn (rt \<xi>) i"
87
+ and "quality_increases \<xi> \<xi>'"
88
+ shows "i\<in>kD(rt \<xi>') \<and> s \<le> nsqn (rt \<xi>') i"
89
+ proof
90
+ from \<open>i\<in>kD(rt \<xi>)\<close> and \<open>quality_increases \<xi> \<xi>'\<close> show "i\<in>kD(rt \<xi>')" ..
91
+ next
92
+ from \<open>i\<in>kD(rt \<xi>)\<close> and \<open>quality_increases \<xi> \<xi>'\<close> have "nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i" ..
93
+ with \<open>s \<le> nsqn (rt \<xi>) i\<close> show "s \<le> nsqn (rt \<xi>') i" by (rule le_trans)
94
+ qed
95
+
96
+ lemma nsqn_quality_increases_nsqn_lt_lt [elim]:
97
+ assumes "i\<in>kD(rt \<xi>)"
98
+ and "quality_increases \<xi> \<xi>'"
99
+ and "s < nsqn (rt \<xi>) i"
100
+ shows "s < nsqn (rt \<xi>') i"
101
+ proof -
102
+ from assms(1-2) have "nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i" ..
103
+ with \<open>s < nsqn (rt \<xi>) i\<close> show "s < nsqn (rt \<xi>') i" by simp
104
+ qed
105
+
106
+ lemma nsqn_quality_increases_dhops [elim]:
107
+ assumes "i\<in>kD(rt \<xi>)"
108
+ and "quality_increases \<xi> \<xi>'"
109
+ and "nsqn (rt \<xi>) i = nsqn (rt \<xi>') i"
110
+ shows "the (dhops (rt \<xi>) i) \<ge> the (dhops (rt \<xi>') i)"
111
+ using assms unfolding quality_increases_def
112
+ by (clarsimp) (drule(1) bspec, clarsimp simp: rt_fresher_def2)
113
+
114
+ lemma nsqn_quality_increases_nsqn_eq_le [elim]:
115
+ assumes "i\<in>kD(rt \<xi>)"
116
+ and "quality_increases \<xi> \<xi>'"
117
+ and "s = nsqn (rt \<xi>) i"
118
+ shows "s < nsqn (rt \<xi>') i \<or> (s = nsqn (rt \<xi>') i \<and> the (dhops (rt \<xi>) i) \<ge> the (dhops (rt \<xi>') i))"
119
+ using assms by (metis nat_less_le nsqn_quality_increases nsqn_quality_increases_dhops)
120
+
121
+ lemma quality_increases_rreq_rrep_props [elim]:
122
+ fixes sn ip hops sip
123
+ assumes qinc: "quality_increases (\<sigma> sip) (\<sigma>' sip)"
124
+ and "1 \<le> sn"
125
+ and *: "ip\<in>kD(rt (\<sigma> sip)) \<and> sn \<le> nsqn (rt (\<sigma> sip)) ip
126
+ \<and> (nsqn (rt (\<sigma> sip)) ip = sn
127
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) ip) \<le> hops
128
+ \<or> the (flag (rt (\<sigma> sip)) ip) = inv))"
129
+ shows "ip\<in>kD(rt (\<sigma>' sip)) \<and> sn \<le> nsqn (rt (\<sigma>' sip)) ip
130
+ \<and> (nsqn (rt (\<sigma>' sip)) ip = sn
131
+ \<longrightarrow> (the (dhops (rt (\<sigma>' sip)) ip) \<le> hops
132
+ \<or> the (flag (rt (\<sigma>' sip)) ip) = inv))"
133
+ (is "_ \<and> ?nsqnafter")
134
+ proof -
135
+ from * obtain "ip\<in>kD(rt (\<sigma> sip))" and "sn \<le> nsqn (rt (\<sigma> sip)) ip" by auto
136
+
137
+ from \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
138
+ have "sqn (rt (\<sigma> sip)) ip \<le> sqn (rt (\<sigma>' sip)) ip" ..
139
+ from \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close> and \<open>ip\<in>kD (rt (\<sigma> sip))\<close>
140
+ have "ip\<in>kD (rt (\<sigma>' sip))" ..
141
+
142
+ from \<open>sn \<le> nsqn (rt (\<sigma> sip)) ip\<close> have ?nsqnafter
143
+ proof
144
+ assume "sn < nsqn (rt (\<sigma> sip)) ip"
145
+ also from \<open>ip\<in>kD(rt (\<sigma> sip))\<close> and \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
146
+ have "... \<le> nsqn (rt (\<sigma>' sip)) ip" ..
147
+ finally have "sn < nsqn (rt (\<sigma>' sip)) ip" .
148
+ thus ?thesis by simp
149
+ next
150
+ assume "sn = nsqn (rt (\<sigma> sip)) ip"
151
+ with \<open>ip\<in>kD(rt (\<sigma> sip))\<close> and \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
152
+ have "sn < nsqn (rt (\<sigma>' sip)) ip
153
+ \<or> (sn = nsqn (rt (\<sigma>' sip)) ip
154
+ \<and> the (dhops (rt (\<sigma>' sip)) ip) \<le> the (dhops (rt (\<sigma> sip)) ip))" ..
155
+ hence "sn < nsqn (rt (\<sigma>' sip)) ip
156
+ \<or> (nsqn (rt (\<sigma>' sip)) ip = sn \<and> (the (dhops (rt (\<sigma>' sip)) ip) \<le> hops
157
+ \<or> the (flag (rt (\<sigma>' sip)) ip) = inv))"
158
+ proof
159
+ assume "sn < nsqn (rt (\<sigma>' sip)) ip" thus ?thesis ..
160
+ next
161
+ assume "sn = nsqn (rt (\<sigma>' sip)) ip
162
+ \<and> the (dhops (rt (\<sigma> sip)) ip) \<ge> the (dhops (rt (\<sigma>' sip)) ip)"
163
+ hence "sn = nsqn (rt (\<sigma>' sip)) ip"
164
+ and "the (dhops (rt (\<sigma>' sip)) ip) \<le> the (dhops (rt (\<sigma> sip)) ip)" by auto
165
+
166
+ from * and \<open>sn = nsqn (rt (\<sigma> sip)) ip\<close> have "the (dhops (rt (\<sigma> sip)) ip) \<le> hops
167
+ \<or> the (flag (rt (\<sigma> sip)) ip) = inv"
168
+ by simp
169
+ thus ?thesis
170
+ proof
171
+ assume "the (dhops (rt (\<sigma> sip)) ip) \<le> hops"
172
+ with \<open>the (dhops (rt (\<sigma>' sip)) ip) \<le> the (dhops (rt (\<sigma> sip)) ip)\<close>
173
+ have "the (dhops (rt (\<sigma>' sip)) ip) \<le> hops" by simp
174
+ with \<open>sn = nsqn (rt (\<sigma>' sip)) ip\<close> show ?thesis by simp
175
+ next
176
+ assume "the (flag (rt (\<sigma> sip)) ip) = inv"
177
+ with \<open>ip\<in>kD(rt (\<sigma> sip))\<close> have "nsqn (rt (\<sigma> sip)) ip = sqn (rt (\<sigma> sip)) ip - 1" ..
178
+
179
+ with \<open>sn \<ge> 1\<close> and \<open>sn = nsqn (rt (\<sigma> sip)) ip\<close>
180
+ have "sqn (rt (\<sigma> sip)) ip > 1" by simp
181
+
182
+ from \<open>ip\<in>kD(rt (\<sigma>' sip))\<close> show ?thesis
183
+ proof (rule vD_or_iD)
184
+ assume "ip\<in>iD(rt (\<sigma>' sip))"
185
+ hence "the (flag (rt (\<sigma>' sip)) ip) = inv" ..
186
+ with \<open>sn = nsqn (rt (\<sigma>' sip)) ip\<close> show ?thesis
187
+ by simp
188
+ next
189
+ (* the tricky case: sn = nsqn (rt (\<sigma>' sip)) ip
190
+ \<and> ip\<in>iD(rt (\<sigma> sip))
191
+ \<and> ip\<in>vD(rt (\<sigma>' sip)) *)
192
+ assume "ip\<in>vD(rt (\<sigma>' sip))"
193
+ hence "nsqn (rt (\<sigma>' sip)) ip = sqn (rt (\<sigma>' sip)) ip" ..
194
+ with \<open>sqn (rt (\<sigma> sip)) ip \<le> sqn (rt (\<sigma>' sip)) ip\<close>
195
+ have "nsqn (rt (\<sigma>' sip)) ip \<ge> sqn (rt (\<sigma> sip)) ip" by simp
196
+
197
+ with \<open>sqn (rt (\<sigma> sip)) ip > 1\<close>
198
+ have "nsqn (rt (\<sigma>' sip)) ip > sqn (rt (\<sigma> sip)) ip - 1" by simp
199
+ with \<open>nsqn (rt (\<sigma> sip)) ip = sqn (rt (\<sigma> sip)) ip - 1\<close>
200
+ have "nsqn (rt (\<sigma>' sip)) ip > nsqn (rt (\<sigma> sip)) ip" by simp
201
+ with \<open>sn = nsqn (rt (\<sigma> sip)) ip\<close> have "nsqn (rt (\<sigma>' sip)) ip > sn"
202
+ by simp
203
+ thus ?thesis ..
204
+ qed
205
+ qed
206
+ qed
207
+ thus ?thesis by (metis (mono_tags) le_cases not_le)
208
+ qed
209
+ with \<open>ip\<in>kD (rt (\<sigma>' sip))\<close> show "ip\<in>kD (rt (\<sigma>' sip)) \<and> ?nsqnafter" ..
210
+ qed
211
+
212
+ lemma quality_increases_rreq_rrep_props':
213
+ fixes sn ip hops sip
214
+ assumes "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
215
+ and "1 \<le> sn"
216
+ and *: "ip\<in>kD(rt (\<sigma> sip)) \<and> sn \<le> nsqn (rt (\<sigma> sip)) ip
217
+ \<and> (nsqn (rt (\<sigma> sip)) ip = sn
218
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) ip) \<le> hops
219
+ \<or> the (flag (rt (\<sigma> sip)) ip) = inv))"
220
+ shows "ip\<in>kD(rt (\<sigma>' sip)) \<and> sn \<le> nsqn (rt (\<sigma>' sip)) ip
221
+ \<and> (nsqn (rt (\<sigma>' sip)) ip = sn
222
+ \<longrightarrow> (the (dhops (rt (\<sigma>' sip)) ip) \<le> hops
223
+ \<or> the (flag (rt (\<sigma>' sip)) ip) = inv))"
224
+ proof -
225
+ from assms(1) have "quality_increases (\<sigma> sip) (\<sigma>' sip)" ..
226
+ thus ?thesis using assms(2-3) by (rule quality_increases_rreq_rrep_props)
227
+ qed
228
+
229
+ lemma rteq_quality_increases:
230
+ assumes "\<forall>j. j \<noteq> i \<longrightarrow> quality_increases (\<sigma> j) (\<sigma>' j)"
231
+ and "rt (\<sigma>' i) = rt (\<sigma> i)"
232
+ shows "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
233
+ using assms by clarsimp (metis order_refl quality_increasesI rt_fresher_refl)
234
+
235
+ definition msg_fresh :: "(ip \<Rightarrow> state) \<Rightarrow> msg \<Rightarrow> bool"
236
+ where "msg_fresh \<sigma> m \<equiv>
237
+ case m of Rreq hopsc _ _ _ _ oipc osnc sipc \<Rightarrow> osnc \<ge> 1 \<and> (sipc \<noteq> oipc \<longrightarrow>
238
+ oipc\<in>kD(rt (\<sigma> sipc)) \<and> nsqn (rt (\<sigma> sipc)) oipc \<ge> osnc
239
+ \<and> (nsqn (rt (\<sigma> sipc)) oipc = osnc
240
+ \<longrightarrow> (hopsc \<ge> the (dhops (rt (\<sigma> sipc)) oipc)
241
+ \<or> the (flag (rt (\<sigma> sipc)) oipc) = inv)))
242
+ | Rrep hopsc dipc dsnc _ sipc \<Rightarrow> dsnc \<ge> 1 \<and> (sipc \<noteq> dipc \<longrightarrow>
243
+ dipc\<in>kD(rt (\<sigma> sipc)) \<and> nsqn (rt (\<sigma> sipc)) dipc \<ge> dsnc
244
+ \<and> (nsqn (rt (\<sigma> sipc)) dipc = dsnc
245
+ \<longrightarrow> (hopsc \<ge> the (dhops (rt (\<sigma> sipc)) dipc)
246
+ \<or> the (flag (rt (\<sigma> sipc)) dipc) = inv)))
247
+ | Rerr destsc sipc \<Rightarrow> (\<forall>ripc\<in>dom(destsc). (ripc\<in>kD(rt (\<sigma> sipc))
248
+ \<and> the (destsc ripc) - 1 \<le> nsqn (rt (\<sigma> sipc)) ripc))
249
+ | _ \<Rightarrow> True"
250
+
251
+ lemma msg_fresh [simp]:
252
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
253
+ msg_fresh \<sigma> (Rreq hops rreqid dip dsn dsk oip osn sip) =
254
+ (osn \<ge> 1 \<and> (sip \<noteq> oip \<longrightarrow> oip\<in>kD(rt (\<sigma> sip))
255
+ \<and> nsqn (rt (\<sigma> sip)) oip \<ge> osn
256
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
257
+ \<longrightarrow> (hops \<ge> the (dhops (rt (\<sigma> sip)) oip)
258
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv))))"
259
+ "\<And>hops dip dsn oip sip. msg_fresh \<sigma> (Rrep hops dip dsn oip sip) =
260
+ (dsn \<ge> 1 \<and> (sip \<noteq> dip \<longrightarrow> dip\<in>kD(rt (\<sigma> sip))
261
+ \<and> nsqn (rt (\<sigma> sip)) dip \<ge> dsn
262
+ \<and> (nsqn (rt (\<sigma> sip)) dip = dsn
263
+ \<longrightarrow> (hops \<ge> the (dhops (rt (\<sigma> sip)) dip))
264
+ \<or> the (flag (rt (\<sigma> sip)) dip) = inv)))"
265
+ "\<And>dests sip. msg_fresh \<sigma> (Rerr dests sip) =
266
+ (\<forall>ripc\<in>dom(dests). (ripc\<in>kD(rt (\<sigma> sip))
267
+ \<and> the (dests ripc) - 1 \<le> nsqn (rt (\<sigma> sip)) ripc))"
268
+ "\<And>d dip. msg_fresh \<sigma> (Newpkt d dip) = True"
269
+ "\<And>d dip sip. msg_fresh \<sigma> (Pkt d dip sip) = True"
270
+ unfolding msg_fresh_def by simp_all
271
+
272
+ lemma msg_fresh_inc_sn [simp, elim]:
273
+ "msg_fresh \<sigma> m \<Longrightarrow> rreq_rrep_sn m"
274
+ by (cases m) simp_all
275
+
276
+ lemma recv_msg_fresh_inc_sn [simp, elim]:
277
+ "orecvmsg (msg_fresh) \<sigma> m \<Longrightarrow> recvmsg rreq_rrep_sn m"
278
+ by (cases m) simp_all
279
+
280
+ lemma rreq_nsqn_is_fresh [simp]:
281
+ fixes \<sigma> msg hops rreqid dip dsn dsk oip osn sip
282
+ assumes "rreq_rrep_fresh (rt (\<sigma> sip)) (Rreq hops rreqid dip dsn dsk oip osn sip)"
283
+ and "rreq_rrep_sn (Rreq hops rreqid dip dsn dsk oip osn sip)"
284
+ shows "msg_fresh \<sigma> (Rreq hops rreqid dip dsn dsk oip osn sip)"
285
+ (is "msg_fresh \<sigma> ?msg")
286
+ proof -
287
+ let ?rt = "rt (\<sigma> sip)"
288
+ from assms(2) have "1 \<le> osn" by simp
289
+ thus ?thesis
290
+ unfolding msg_fresh_def
291
+ proof (simp only: msg.case, intro conjI impI)
292
+ assume "sip \<noteq> oip"
293
+ with assms(1) show "oip \<in> kD(?rt)" by simp
294
+ next
295
+ assume "sip \<noteq> oip"
296
+ and "nsqn ?rt oip = osn"
297
+ show "the (dhops ?rt oip) \<le> hops \<or> the (flag ?rt oip) = inv"
298
+ proof (cases "oip\<in>vD(?rt)")
299
+ assume "oip\<in>vD(?rt)"
300
+ hence "nsqn ?rt oip = sqn ?rt oip" ..
301
+ with \<open>nsqn ?rt oip = osn\<close> have "sqn ?rt oip = osn" by simp
302
+ with assms(1) and \<open>sip \<noteq> oip\<close> have "the (dhops ?rt oip) \<le> hops"
303
+ by simp
304
+ thus ?thesis ..
305
+ next
306
+ assume "oip\<notin>vD(?rt)"
307
+ moreover from assms(1) and \<open>sip \<noteq> oip\<close> have "oip\<in>kD(?rt)" by simp
308
+ ultimately have "oip\<in>iD(?rt)" by auto
309
+ hence "the (flag ?rt oip) = inv" ..
310
+ thus ?thesis ..
311
+ qed
312
+ next
313
+ assume "sip \<noteq> oip"
314
+ with assms(1) have "osn \<le> sqn ?rt oip" by auto
315
+ thus "osn \<le> nsqn (rt (\<sigma> sip)) oip"
316
+ proof (rule nat_le_eq_or_lt)
317
+ assume "osn < sqn ?rt oip"
318
+ hence "osn \<le> sqn ?rt oip - 1" by simp
319
+ also have "... \<le> nsqn ?rt oip" by (rule sqn_nsqn)
320
+ finally show "osn \<le> nsqn ?rt oip" .
321
+ next
322
+ assume "osn = sqn ?rt oip"
323
+ with assms(1) and \<open>sip \<noteq> oip\<close> have "oip\<in>kD(?rt)"
324
+ and "the (flag ?rt oip) = val"
325
+ by auto
326
+ hence "nsqn ?rt oip = sqn ?rt oip" ..
327
+ with \<open>osn = sqn ?rt oip\<close> have "nsqn ?rt oip = osn" by simp
328
+ thus "osn \<le> nsqn ?rt oip" by simp
329
+ qed
330
+ qed simp
331
+ qed
332
+
333
+ lemma rrep_nsqn_is_fresh [simp]:
334
+ fixes \<sigma> msg hops dip dsn oip sip
335
+ assumes "rreq_rrep_fresh (rt (\<sigma> sip)) (Rrep hops dip dsn oip sip)"
336
+ and "rreq_rrep_sn (Rrep hops dip dsn oip sip)"
337
+ shows "msg_fresh \<sigma> (Rrep hops dip dsn oip sip)"
338
+ (is "msg_fresh \<sigma> ?msg")
339
+ proof -
340
+ let ?rt = "rt (\<sigma> sip)"
341
+ from assms have "sip \<noteq> dip \<longrightarrow> dip\<in>kD(?rt) \<and> sqn ?rt dip = dsn \<and> the (flag ?rt dip) = val"
342
+ by simp
343
+ hence "sip \<noteq> dip \<longrightarrow> dip\<in>kD(?rt) \<and> nsqn ?rt dip \<ge> dsn"
344
+ by clarsimp
345
+ with assms show "msg_fresh \<sigma> ?msg"
346
+ by clarsimp
347
+ qed
348
+
349
+ lemma rerr_nsqn_is_fresh [simp]:
350
+ fixes \<sigma> msg dests sip
351
+ assumes "rerr_invalid (rt (\<sigma> sip)) (Rerr dests sip)"
352
+ shows "msg_fresh \<sigma> (Rerr dests sip)"
353
+ (is "msg_fresh \<sigma> ?msg")
354
+ proof -
355
+ let ?rt = "rt (\<sigma> sip)"
356
+ from assms have *: "(\<forall>rip\<in>dom(dests). (rip\<in>iD(rt (\<sigma> sip))
357
+ \<and> the (dests rip) = sqn (rt (\<sigma> sip)) rip))"
358
+ by clarsimp
359
+ have "(\<forall>rip\<in>dom(dests). (rip\<in>kD(rt (\<sigma> sip))
360
+ \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip))"
361
+ proof
362
+ fix rip
363
+ assume "rip \<in> dom dests"
364
+ with * have "rip\<in>iD(rt (\<sigma> sip))" and "the (dests rip) = sqn (rt (\<sigma> sip)) rip"
365
+ by auto
366
+
367
+ from this(2) have "the (dests rip) - 1 = sqn (rt (\<sigma> sip)) rip - 1" by simp
368
+ also have "... \<le> nsqn (rt (\<sigma> sip)) rip" by (rule sqn_nsqn)
369
+ finally have "the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip" .
370
+
371
+ with \<open>rip\<in>iD(rt (\<sigma> sip))\<close>
372
+ show "rip\<in>kD(rt (\<sigma> sip)) \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
373
+ by clarsimp
374
+ qed
375
+ thus "msg_fresh \<sigma> ?msg"
376
+ by simp
377
+ qed
378
+
379
+ lemma quality_increases_msg_fresh [elim]:
380
+ assumes qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
381
+ and "msg_fresh \<sigma> m"
382
+ shows "msg_fresh \<sigma>' m"
383
+ using assms(2)
384
+ proof (cases m)
385
+ fix hops rreqid dip dsn dsk oip osn sip
386
+ assume [simp]: "m = Rreq hops rreqid dip dsn dsk oip osn sip"
387
+ and "msg_fresh \<sigma> m"
388
+ then have "osn \<ge> 1" and "sip = oip \<or> (oip\<in>kD(rt (\<sigma> sip)) \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
389
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
390
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) oip) \<le> hops
391
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv)))"
392
+ by auto
393
+ from this(2) show ?thesis
394
+ proof
395
+ assume "sip = oip" with \<open>osn \<ge> 1\<close> show ?thesis by simp
396
+ next
397
+ assume "oip\<in>kD(rt (\<sigma> sip)) \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
398
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
399
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) oip) \<le> hops
400
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv))"
401
+ moreover from qinc have "quality_increases (\<sigma> sip) (\<sigma>' sip)" ..
402
+ ultimately have "oip\<in>kD(rt (\<sigma>' sip)) \<and> osn \<le> nsqn (rt (\<sigma>' sip)) oip
403
+ \<and> (nsqn (rt (\<sigma>' sip)) oip = osn
404
+ \<longrightarrow> (the (dhops (rt (\<sigma>' sip)) oip) \<le> hops
405
+ \<or> the (flag (rt (\<sigma>' sip)) oip) = inv))"
406
+ using \<open>osn \<ge> 1\<close> by (rule quality_increases_rreq_rrep_props [rotated 2])
407
+ with \<open>osn \<ge> 1\<close> show "msg_fresh \<sigma>' m"
408
+ by (clarsimp)
409
+ qed
410
+ next
411
+ fix hops dip dsn oip sip
412
+ assume [simp]: "m = Rrep hops dip dsn oip sip"
413
+ and "msg_fresh \<sigma> m"
414
+ then have "dsn \<ge> 1" and "sip = dip \<or> (dip\<in>kD(rt (\<sigma> sip)) \<and> dsn \<le> nsqn (rt (\<sigma> sip)) dip
415
+ \<and> (nsqn (rt (\<sigma> sip)) dip = dsn
416
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) dip) \<le> hops
417
+ \<or> the (flag (rt (\<sigma> sip)) dip) = inv)))"
418
+ by auto
419
+ from this(2) show "?thesis"
420
+ proof
421
+ assume "sip = dip" with \<open>dsn \<ge> 1\<close> show ?thesis by simp
422
+ next
423
+ assume "dip\<in>kD(rt (\<sigma> sip)) \<and> dsn \<le> nsqn (rt (\<sigma> sip)) dip
424
+ \<and> (nsqn (rt (\<sigma> sip)) dip = dsn
425
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) dip) \<le> hops
426
+ \<or> the (flag (rt (\<sigma> sip)) dip) = inv))"
427
+ moreover from qinc have "quality_increases (\<sigma> sip) (\<sigma>' sip)" ..
428
+ ultimately have "dip\<in>kD(rt (\<sigma>' sip)) \<and> dsn \<le> nsqn (rt (\<sigma>' sip)) dip
429
+ \<and> (nsqn (rt (\<sigma>' sip)) dip = dsn
430
+ \<longrightarrow> (the (dhops (rt (\<sigma>' sip)) dip) \<le> hops
431
+ \<or> the (flag (rt (\<sigma>' sip)) dip) = inv))"
432
+ using \<open>dsn \<ge> 1\<close> by (rule quality_increases_rreq_rrep_props [rotated 2])
433
+ with \<open>dsn \<ge> 1\<close> show "msg_fresh \<sigma>' m"
434
+ by clarsimp
435
+ qed
436
+ next
437
+ fix dests sip
438
+ assume [simp]: "m = Rerr dests sip"
439
+ and "msg_fresh \<sigma> m"
440
+ then have *: "\<forall>rip\<in>dom(dests). rip\<in>kD(rt (\<sigma> sip))
441
+ \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
442
+ by simp
443
+ have "\<forall>rip\<in>dom(dests). rip\<in>kD(rt (\<sigma>' sip))
444
+ \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma>' sip)) rip"
445
+ proof
446
+ fix rip
447
+ assume "rip\<in>dom(dests)"
448
+ with * have "rip\<in>kD(rt (\<sigma> sip))" and "the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
449
+ by - (drule(1) bspec, clarsimp)+
450
+ moreover from qinc have "quality_increases (\<sigma> sip) (\<sigma>' sip)" by simp
451
+ ultimately show "rip\<in>kD(rt (\<sigma>' sip)) \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma>' sip)) rip" ..
452
+ qed
453
+ thus ?thesis by simp
454
+ qed simp_all
455
+
456
+ end
formal/afp/AODV/Seq_Invariants.thy ADDED
@@ -0,0 +1,643 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: Seq_Invariants.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Invariant proofs on individual processes"
7
+
8
+ theory Seq_Invariants
9
+ imports AWN.Invariants Aodv Aodv_Data Aodv_Predicates Fresher
10
+
11
+ begin
12
+
13
+ text \<open>
14
+ The proposition numbers are taken from the December 2013 version of
15
+ the Fehnker et al technical report.
16
+ \<close>
17
+
18
+ text \<open>Proposition 7.2\<close>
19
+
20
+ lemma sequence_number_increases:
21
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). sn \<xi> \<le> sn \<xi>')"
22
+ by inv_cterms
23
+
24
+ lemma sequence_number_one_or_bigger:
25
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). 1 \<le> sn \<xi>)"
26
+ by (rule onll_step_to_invariantI [OF sequence_number_increases])
27
+ (auto simp: \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def)
28
+
29
+ text \<open>We can get rid of the onl/onll if desired...\<close>
30
+
31
+ lemma sequence_number_increases':
32
+ "paodv i \<TTurnstile>\<^sub>A (\<lambda>((\<xi>, _), _, (\<xi>', _)). sn \<xi> \<le> sn \<xi>')"
33
+ by (rule step_invariant_weakenE [OF sequence_number_increases]) (auto dest!: onllD)
34
+
35
+ lemma sequence_number_one_or_bigger':
36
+ "paodv i \<TTurnstile> (\<lambda>(\<xi>, _). 1 \<le> sn \<xi>)"
37
+ by (rule invariant_weakenE [OF sequence_number_one_or_bigger]) auto
38
+
39
+ lemma sip_in_kD:
40
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). l \<in> ({PAodv-:7} \<union> {PAodv-:5} \<union> {PRrep-:0..PRrep-:1}
41
+ \<union> {PRreq-:0..PRreq-:3}) \<longrightarrow> sip \<xi> \<in> kD (rt \<xi>))"
42
+ by inv_cterms
43
+
44
+ lemma rrep_1_update_changes:
45
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l = PRrep-:1 \<longrightarrow>
46
+ rt \<xi> \<noteq> update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {})))"
47
+ by inv_cterms
48
+
49
+ lemma addpreRT_partly_welldefined:
50
+ "paodv i \<TTurnstile>
51
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PRreq-:16..PRreq-:18} \<union> {PRrep-:2..PRrep-:6} \<longrightarrow> dip \<xi> \<in> kD (rt \<xi>))
52
+ \<and> (l \<in> {PRreq-:3..PRreq-:17} \<longrightarrow> oip \<xi> \<in> kD (rt \<xi>)))"
53
+ by inv_cterms
54
+
55
+ text \<open>Proposition 7.38\<close>
56
+
57
+ lemma includes_nhip:
58
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). \<forall>dip\<in>kD(rt \<xi>). the (nhop (rt \<xi>) dip)\<in>kD(rt \<xi>))"
59
+ proof -
60
+ { fix ip and \<xi> \<xi>' :: state
61
+ assume "\<forall>dip\<in>kD (rt \<xi>). the (nhop (rt \<xi>) dip) \<in> kD (rt \<xi>)"
62
+ and "\<xi>' = \<xi>\<lparr>rt := update (rt \<xi>) ip (0, unk, val, Suc 0, ip, {})\<rparr>"
63
+ hence "\<forall>dip\<in>kD (rt \<xi>).
64
+ the (nhop (update (rt \<xi>) ip (0, unk, val, Suc 0, ip, {})) dip) = ip
65
+ \<or> the (nhop (update (rt \<xi>) ip (0, unk, val, Suc 0, ip, {})) dip) \<in> kD (rt \<xi>)"
66
+ by clarsimp (metis nhop_update_unk_val update_another)
67
+ } note one_hop = this
68
+ { fix ip sip sn hops and \<xi> \<xi>' :: state
69
+ assume "\<forall>dip\<in>kD (rt \<xi>). the (nhop (rt \<xi>) dip) \<in> kD (rt \<xi>)"
70
+ and "\<xi>' = \<xi>\<lparr>rt := update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})\<rparr>"
71
+ and "sip \<in> kD (rt \<xi>)"
72
+ hence "(the (nhop (update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})) ip) = ip
73
+ \<or> the (nhop (update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})) ip) \<in> kD (rt \<xi>))
74
+ \<and> (\<forall>dip\<in>kD (rt \<xi>).
75
+ the (nhop (update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})) dip) = ip
76
+ \<or> the (nhop (update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})) dip) \<in> kD (rt \<xi>))"
77
+ by (metis kD_update_unchanged nhop_update_changed update_another)
78
+ } note nhip_is_sip = this
79
+ show ?thesis
80
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf sip_in_kD]
81
+ onl_invariant_sterms [OF aodv_wf addpreRT_partly_welldefined]
82
+ solve: one_hop nhip_is_sip)
83
+ qed
84
+
85
+ text \<open>Proposition 7.22: needed in Proposition 7.4\<close>
86
+
87
+ lemma addpreRT_welldefined:
88
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PRreq-:16..PRreq-:18} \<longrightarrow> dip \<xi> \<in> kD (rt \<xi>)) \<and>
89
+ (l = PRreq-:17 \<longrightarrow> oip \<xi> \<in> kD (rt \<xi>)) \<and>
90
+ (l = PRrep-:5 \<longrightarrow> dip \<xi> \<in> kD (rt \<xi>)) \<and>
91
+ (l = PRrep-:6 \<longrightarrow> (the (nhop (rt \<xi>) (dip \<xi>))) \<in> kD (rt \<xi>)))"
92
+ (is "_ \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V ?P")
93
+ unfolding invariant_def
94
+ proof
95
+ fix s
96
+ assume "s \<in> reachable (paodv i) TT"
97
+ then obtain \<xi> p where "s = (\<xi>, p)"
98
+ and "(\<xi>, p) \<in> reachable (paodv i) TT"
99
+ by (metis prod.exhaust)
100
+ have "onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V ?P (\<xi>, p)"
101
+ proof (rule onlI)
102
+ fix l
103
+ assume "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
104
+ with \<open>(\<xi>, p) \<in> reachable (paodv i) TT\<close>
105
+ have I1: "l \<in> {PRreq-:16..PRreq-:18} \<longrightarrow> dip \<xi> \<in> kD(rt \<xi>)"
106
+ and I2: "l = PRreq-:17 \<longrightarrow> oip \<xi> \<in> kD(rt \<xi>)"
107
+ and I3: "l \<in> {PRrep-:2..PRrep-:6} \<longrightarrow> dip \<xi> \<in> kD(rt \<xi>)"
108
+ by (auto dest!: invariantD [OF addpreRT_partly_welldefined])
109
+ moreover from \<open>(\<xi>, p) \<in> reachable (paodv i) TT\<close> \<open>l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and I3
110
+ have "l = PRrep-:6 \<longrightarrow> (the (nhop (rt \<xi>) (dip \<xi>))) \<in> kD(rt \<xi>)"
111
+ by (auto dest!: invariantD [OF includes_nhip])
112
+ ultimately show "?P (\<xi>, l)"
113
+ by simp
114
+ qed
115
+ with \<open>s = (\<xi>, p)\<close> show "onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V ?P s"
116
+ by simp
117
+ qed
118
+
119
+ text \<open>Proposition 7.4\<close>
120
+
121
+ lemma known_destinations_increase:
122
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). kD (rt \<xi>) \<subseteq> kD (rt \<xi>'))"
123
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf addpreRT_welldefined]
124
+ simp add: subset_insertI)
125
+
126
+ text \<open>Proposition 7.5\<close>
127
+
128
+ lemma rreqs_increase:
129
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). rreqs \<xi> \<subseteq> rreqs \<xi>')"
130
+ by (inv_cterms simp add: subset_insertI)
131
+
132
+ lemma dests_bigger_than_sqn:
133
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). l \<in> {PAodv-:15..PAodv-:19}
134
+ \<union> {PPkt-:7..PPkt-:11}
135
+ \<union> {PRreq-:9..PRreq-:13}
136
+ \<union> {PRreq-:21..PRreq-:25}
137
+ \<union> {PRrep-:10..PRrep-:14}
138
+ \<union> {PRerr-:1..PRerr-:5}
139
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>kD(rt \<xi>) \<and> sqn (rt \<xi>) ip \<le> the (dests \<xi> ip)))"
140
+ proof -
141
+ have sqninv:
142
+ "\<And>dests rt rsn ip.
143
+ \<lbrakk> \<forall>ip\<in>dom(dests). ip\<in>kD(rt) \<and> sqn rt ip \<le> the (dests ip); dests ip = Some rsn \<rbrakk>
144
+ \<Longrightarrow> sqn (invalidate rt dests) ip \<le> rsn"
145
+ by (rule sqn_invalidate_in_dests [THEN eq_imp_le], assumption) auto
146
+ have indests:
147
+ "\<And>dests rt rsn ip.
148
+ \<lbrakk> \<forall>ip\<in>dom(dests). ip\<in>kD(rt) \<and> sqn rt ip \<le> the (dests ip); dests ip = Some rsn \<rbrakk>
149
+ \<Longrightarrow> ip\<in>kD(rt) \<and> sqn rt ip \<le> rsn"
150
+ by (metis domI option.sel)
151
+ show ?thesis
152
+ by inv_cterms
153
+ (clarsimp split: if_split_asm option.split_asm
154
+ elim!: sqninv indests)+
155
+ qed
156
+
157
+ text \<open>Proposition 7.6\<close>
158
+
159
+ lemma sqns_increase:
160
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). \<forall>ip. sqn (rt \<xi>) ip \<le> sqn (rt \<xi>') ip)"
161
+ proof -
162
+ { fix \<xi> :: state
163
+ assume *: "\<forall>ip\<in>dom(dests \<xi>). ip \<in> kD (rt \<xi>) \<and> sqn (rt \<xi>) ip \<le> the (dests \<xi> ip)"
164
+ have "\<forall>ip. sqn (rt \<xi>) ip \<le> sqn (invalidate (rt \<xi>) (dests \<xi>)) ip"
165
+ proof
166
+ fix ip
167
+ from * have "ip\<notin>dom(dests \<xi>) \<or> sqn (rt \<xi>) ip \<le> the (dests \<xi> ip)" by simp
168
+ thus "sqn (rt \<xi>) ip \<le> sqn (invalidate (rt \<xi>) (dests \<xi>)) ip"
169
+ by (metis domI invalidate_sqn option.sel)
170
+ qed
171
+ } note solve_invalidate = this
172
+ show ?thesis
173
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf addpreRT_welldefined]
174
+ onl_invariant_sterms [OF aodv_wf dests_bigger_than_sqn]
175
+ simp add: solve_invalidate)
176
+ qed
177
+
178
+ text \<open>Proposition 7.7\<close>
179
+
180
+ lemma ip_constant:
181
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). ip \<xi> = i)"
182
+ by (inv_cterms simp add: \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def)
183
+
184
+ text \<open>Proposition 7.8\<close>
185
+
186
+ lemma sender_ip_valid':
187
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), a, _). anycast (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m = ip \<xi>) a)"
188
+ by inv_cterms
189
+
190
+ lemma sender_ip_valid:
191
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), a, _). anycast (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m = i) a)"
192
+ by (rule step_invariant_weaken_with_invariantE [OF ip_constant sender_ip_valid'])
193
+ (auto dest!: onlD onllD)
194
+
195
+ lemma received_msg_inv:
196
+ "paodv i \<TTurnstile> (recvmsg P \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). l \<in> {PAodv-:1} \<longrightarrow> P (msg \<xi>))"
197
+ by inv_cterms
198
+
199
+ text \<open>Proposition 7.9\<close>
200
+
201
+ lemma sip_not_ip':
202
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m \<noteq> i) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). sip \<xi> \<noteq> ip \<xi>)"
203
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]
204
+ onl_invariant_sterms [OF aodv_wf ip_constant [THEN invariant_restrict_inD]]
205
+ simp add: clear_locals_sip_not_ip') clarsimp+
206
+
207
+ lemma sip_not_ip:
208
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m \<noteq> i) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). sip \<xi> \<noteq> i)"
209
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]
210
+ onl_invariant_sterms [OF aodv_wf ip_constant [THEN invariant_restrict_inD]]
211
+ simp add: clear_locals_sip_not_ip') clarsimp+
212
+
213
+ text \<open>Neither \<open>sip_not_ip'\<close> nor \<open>sip_not_ip\<close> is needed to show loop freedom.\<close>
214
+
215
+ text \<open>Proposition 7.10\<close>
216
+
217
+ lemma hop_count_positive:
218
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). \<forall>ip\<in>kD (rt \<xi>). the (dhops (rt \<xi>) ip) \<ge> 1)"
219
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf addpreRT_welldefined]) auto
220
+
221
+ lemma rreq_dip_in_vD_dip_eq_ip:
222
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PRreq-:16..PRreq-:18} \<longrightarrow> dip \<xi> \<in> vD(rt \<xi>))
223
+ \<and> (l \<in> {PRreq-:5, PRreq-:6} \<longrightarrow> dip \<xi> = ip \<xi>)
224
+ \<and> (l \<in> {PRreq-:15..PRreq-:18} \<longrightarrow> dip \<xi> \<noteq> ip \<xi>))"
225
+ proof (inv_cterms, elim conjE)
226
+ fix l \<xi> pp p'
227
+ assume "(\<xi>, pp) \<in> reachable (paodv i) TT"
228
+ and "{PRreq-:17}\<lbrakk>\<lambda>\<xi>. \<xi>\<lparr>rt := the (addpreRT (rt \<xi>) (oip \<xi>) {the (nhop (rt \<xi>) (dip \<xi>))})\<rparr>\<rbrakk> p'
229
+ \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
230
+ and "l = PRreq-:17"
231
+ and "dip \<xi> \<in> vD (rt \<xi>)"
232
+ from this(1-3) have "oip \<xi> \<in> kD (rt \<xi>)"
233
+ by (auto dest: onl_invariant_sterms [OF aodv_wf addpreRT_welldefined, where l="PRreq-:17"])
234
+ with \<open>dip \<xi> \<in> vD (rt \<xi>)\<close>
235
+ show "dip \<xi> \<in> vD (the (addpreRT (rt \<xi>) (oip \<xi>) {the (nhop (rt \<xi>) (dip \<xi>))}))" by simp
236
+ qed
237
+
238
+ text \<open>Proposition 7.11\<close>
239
+
240
+ lemma anycast_msg_zhops:
241
+ "\<And>rreqid dip dsn dsk oip osn sip.
242
+ paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast msg_zhops a)"
243
+ proof (inv_cterms inv add:
244
+ onl_invariant_sterms [OF aodv_wf rreq_dip_in_vD_dip_eq_ip [THEN invariant_restrict_inD]]
245
+ onl_invariant_sterms [OF aodv_wf hop_count_positive [THEN invariant_restrict_inD]],
246
+ elim conjE)
247
+ fix l \<xi> a pp p' pp'
248
+ assume "(\<xi>, pp) \<in> reachable (paodv i) TT"
249
+ and "{PRreq-:18}unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)),
250
+ \<lambda>\<xi>. Rrep (the (dhops (rt \<xi>) (dip \<xi>))) (dip \<xi>) (sqn (rt \<xi>) (dip \<xi>)) (oip \<xi>) (ip \<xi>)).
251
+ p' \<triangleright> pp' \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
252
+ and "l = PRreq-:18"
253
+ and "a = unicast (the (nhop (rt \<xi>) (oip \<xi>)))
254
+ (Rrep (the (dhops (rt \<xi>) (dip \<xi>))) (dip \<xi>) (sqn (rt \<xi>) (dip \<xi>)) (oip \<xi>) (ip \<xi>))"
255
+ and *: "\<forall>ip\<in>kD (rt \<xi>). Suc 0 \<le> the (dhops (rt \<xi>) ip)"
256
+ and "dip \<xi> \<in> vD (rt \<xi>)"
257
+ from \<open>dip \<xi> \<in> vD (rt \<xi>)\<close> have "dip \<xi> \<in> kD (rt \<xi>)"
258
+ by (rule vD_iD_gives_kD(1))
259
+ with * have "Suc 0 \<le> the (dhops (rt \<xi>) (dip \<xi>))" ..
260
+ thus "0 < the (dhops (rt \<xi>) (dip \<xi>))" by simp
261
+ qed
262
+
263
+ lemma hop_count_zero_oip_dip_sip:
264
+ "paodv i \<TTurnstile> (recvmsg msg_zhops \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
265
+ (l\<in>{PAodv-:4..PAodv-:5} \<union> {PRreq-:n|n. True} \<longrightarrow>
266
+ (hops \<xi> = 0 \<longrightarrow> oip \<xi> = sip \<xi>))
267
+ \<and>
268
+ ((l\<in>{PAodv-:6..PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow>
269
+ (hops \<xi> = 0 \<longrightarrow> dip \<xi> = sip \<xi>))))"
270
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]) auto
271
+
272
+ lemma osn_rreq:
273
+ "paodv i \<TTurnstile> (recvmsg rreq_rrep_sn \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
274
+ l \<in> {PAodv-:4, PAodv-:5} \<union> {PRreq-:n|n. True} \<longrightarrow> 1 \<le> osn \<xi>)"
275
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]) clarsimp
276
+
277
+ lemma osn_rreq':
278
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
279
+ l \<in> {PAodv-:4, PAodv-:5} \<union> {PRreq-:n|n. True} \<longrightarrow> 1 \<le> osn \<xi>)"
280
+ proof (rule invariant_weakenE [OF osn_rreq])
281
+ fix a
282
+ assume "recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) a"
283
+ thus "recvmsg rreq_rrep_sn a"
284
+ by (cases a) simp_all
285
+ qed
286
+
287
+ lemma dsn_rrep:
288
+ "paodv i \<TTurnstile> (recvmsg rreq_rrep_sn \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
289
+ l \<in> {PAodv-:6, PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow> 1 \<le> dsn \<xi>)"
290
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]) clarsimp
291
+
292
+ lemma dsn_rrep':
293
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
294
+ l \<in> {PAodv-:6, PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow> 1 \<le> dsn \<xi>)"
295
+ proof (rule invariant_weakenE [OF dsn_rrep])
296
+ fix a
297
+ assume "recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) a"
298
+ thus "recvmsg rreq_rrep_sn a"
299
+ by (cases a) simp_all
300
+ qed
301
+
302
+ lemma hop_count_zero_oip_dip_sip':
303
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
304
+ (l\<in>{PAodv-:4..PAodv-:5} \<union> {PRreq-:n|n. True} \<longrightarrow>
305
+ (hops \<xi> = 0 \<longrightarrow> oip \<xi> = sip \<xi>))
306
+ \<and>
307
+ ((l\<in>{PAodv-:6..PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow>
308
+ (hops \<xi> = 0 \<longrightarrow> dip \<xi> = sip \<xi>))))"
309
+ proof (rule invariant_weakenE [OF hop_count_zero_oip_dip_sip])
310
+ fix a
311
+ assume "recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) a"
312
+ thus "recvmsg msg_zhops a"
313
+ by (cases a) simp_all
314
+ qed
315
+
316
+ text \<open>Proposition 7.12\<close>
317
+
318
+ lemma zero_seq_unk_hops_one':
319
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _).
320
+ \<forall>dip\<in>kD(rt \<xi>). (sqn (rt \<xi>) dip = 0 \<longrightarrow> sqnf (rt \<xi>) dip = unk)
321
+ \<and> (sqnf (rt \<xi>) dip = unk \<longrightarrow> the (dhops (rt \<xi>) dip) = 1)
322
+ \<and> (the (dhops (rt \<xi>) dip) = 1 \<longrightarrow> the (nhop (rt \<xi>) dip) = dip))"
323
+ proof -
324
+ { fix dip and \<xi> :: state and P
325
+ assume "sqn (invalidate (rt \<xi>) (dests \<xi>)) dip = 0"
326
+ and all: "\<forall>ip. sqn (rt \<xi>) ip \<le> sqn (invalidate (rt \<xi>) (dests \<xi>)) ip"
327
+ and *: "sqn (rt \<xi>) dip = 0 \<Longrightarrow> P \<xi> dip"
328
+ have "P \<xi> dip"
329
+ proof -
330
+ from all have "sqn (rt \<xi>) dip \<le> sqn (invalidate (rt \<xi>) (dests \<xi>)) dip" ..
331
+ with \<open>sqn (invalidate (rt \<xi>) (dests \<xi>)) dip = 0\<close> have "sqn (rt \<xi>) dip = 0" by simp
332
+ thus "P \<xi> dip" by (rule *)
333
+ qed
334
+ } note sqn_invalidate_zero [elim!] = this
335
+
336
+ { fix dsn hops :: nat and sip oip rt and ip dip :: ip
337
+ assume "\<forall>dip\<in>kD(rt).
338
+ (sqn rt dip = 0 \<longrightarrow> \<pi>\<^sub>3(the (rt dip)) = unk) \<and>
339
+ (\<pi>\<^sub>3(the (rt dip)) = unk \<longrightarrow> the (dhops rt dip) = Suc 0) \<and>
340
+ (the (dhops rt dip) = Suc 0 \<longrightarrow> the (nhop rt dip) = dip)"
341
+ and "hops = 0 \<longrightarrow> sip = dip"
342
+ and "Suc 0 \<le> dsn"
343
+ and "ip \<noteq> dip \<longrightarrow> ip\<in>kD(rt)"
344
+ hence "the (dhops (update rt dip (dsn, kno, val, Suc hops, sip, {})) ip) = Suc 0 \<longrightarrow>
345
+ the (nhop (update rt dip (dsn, kno, val, Suc hops, sip, {})) ip) = ip"
346
+ by - (rule update_cases, auto simp add: sqn_def dest!: bspec)
347
+ } note prreq_ok1 [simp] = this
348
+
349
+ { fix ip dsn hops sip oip rt dip
350
+ assume "\<forall>dip\<in>kD(rt).
351
+ (sqn rt dip = 0 \<longrightarrow> \<pi>\<^sub>3(the (rt dip)) = unk) \<and>
352
+ (\<pi>\<^sub>3(the (rt dip)) = unk \<longrightarrow> the (dhops rt dip) = Suc 0) \<and>
353
+ (the (dhops rt dip) = Suc 0 \<longrightarrow> the (nhop rt dip) = dip)"
354
+ and "Suc 0 \<le> dsn"
355
+ and "ip \<noteq> dip \<longrightarrow> ip\<in>kD(rt)"
356
+ hence "\<pi>\<^sub>3(the (update rt dip (dsn, kno, val, Suc hops, sip, {}) ip)) = unk \<longrightarrow>
357
+ the (dhops (update rt dip (dsn, kno, val, Suc hops, sip, {})) ip) = Suc 0"
358
+ by - (rule update_cases, auto simp add: sqn_def sqnf_def dest!: bspec)
359
+ } note prreq_ok2 [simp] = this
360
+
361
+ { fix ip dsn hops sip oip rt dip
362
+ assume "\<forall>dip\<in>kD(rt).
363
+ (sqn rt dip = 0 \<longrightarrow> \<pi>\<^sub>3(the (rt dip)) = unk) \<and>
364
+ (\<pi>\<^sub>3(the (rt dip)) = unk \<longrightarrow> the (dhops rt dip) = Suc 0) \<and>
365
+ (the (dhops rt dip) = Suc 0 \<longrightarrow> the (nhop rt dip) = dip)"
366
+ and "Suc 0 \<le> dsn"
367
+ and "ip \<noteq> dip \<longrightarrow> ip\<in>kD(rt)"
368
+ hence "sqn (update rt dip (dsn, kno, val, Suc hops, sip, {})) ip = 0 \<longrightarrow>
369
+ \<pi>\<^sub>3 (the (update rt dip (dsn, kno, val, Suc hops, sip, {}) ip)) = unk"
370
+ by - (rule update_cases, auto simp add: sqn_def dest!: bspec)
371
+ } note prreq_ok3 [simp] = this
372
+
373
+ { fix rt sip
374
+ assume "\<forall>dip\<in>kD rt.
375
+ (sqn rt dip = 0 \<longrightarrow> \<pi>\<^sub>3(the (rt dip)) = unk) \<and>
376
+ (\<pi>\<^sub>3(the (rt dip)) = unk \<longrightarrow> the (dhops rt dip) = Suc 0) \<and>
377
+ (the (dhops rt dip) = Suc 0 \<longrightarrow> the (nhop rt dip) = dip)"
378
+ hence "\<forall>dip\<in>kD rt.
379
+ (sqn (update rt sip (0, unk, val, Suc 0, sip, {})) dip = 0 \<longrightarrow>
380
+ \<pi>\<^sub>3(the (update rt sip (0, unk, val, Suc 0, sip, {}) dip)) = unk)
381
+ \<and> (\<pi>\<^sub>3(the (update rt sip (0, unk, val, Suc 0, sip, {}) dip)) = unk \<longrightarrow>
382
+ the (dhops (update rt sip (0, unk, val, Suc 0, sip, {})) dip) = Suc 0)
383
+ \<and> (the (dhops (update rt sip (0, unk, val, Suc 0, sip, {})) dip) = Suc 0 \<longrightarrow>
384
+ the (nhop (update rt sip (0, unk, val, Suc 0, sip, {})) dip) = dip)"
385
+ by - (rule update_cases, simp_all add: sqnf_def sqn_def)
386
+ } note prreq_ok4 [simp] = this
387
+
388
+ have prreq_ok5 [simp]: "\<And>sip rt.
389
+ \<pi>\<^sub>3(the (update rt sip (0, unk, val, Suc 0, sip, {}) sip)) = unk \<longrightarrow>
390
+ the (dhops (update rt sip (0, unk, val, Suc 0, sip, {})) sip) = Suc 0"
391
+ by (rule update_cases) simp_all
392
+
393
+ have prreq_ok6 [simp]: "\<And>sip rt.
394
+ sqn (update rt sip (0, unk, val, Suc 0, sip, {})) sip = 0 \<longrightarrow>
395
+ \<pi>\<^sub>3 (the (update rt sip (0, unk, val, Suc 0, sip, {}) sip)) = unk"
396
+ by (rule update_cases) simp_all
397
+
398
+ show ?thesis
399
+ by (inv_cterms inv add: onl_invariant_sterms_TT [OF aodv_wf addpreRT_welldefined]
400
+ onl_invariant_sterms [OF aodv_wf hop_count_zero_oip_dip_sip']
401
+ seq_step_invariant_sterms_TT [OF sqns_increase aodv_wf aodv_trans]
402
+ onl_invariant_sterms [OF aodv_wf osn_rreq']
403
+ onl_invariant_sterms [OF aodv_wf dsn_rrep']) clarsimp+
404
+ qed
405
+
406
+ lemma zero_seq_unk_hops_one:
407
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _).
408
+ \<forall>dip\<in>kD(rt \<xi>). (sqn (rt \<xi>) dip = 0 \<longrightarrow> (sqnf (rt \<xi>) dip = unk
409
+ \<and> the (dhops (rt \<xi>) dip) = 1
410
+ \<and> the (nhop (rt \<xi>) dip) = dip)))"
411
+ by (rule invariant_weakenE [OF zero_seq_unk_hops_one']) auto
412
+
413
+ lemma kD_unk_or_atleast_one:
414
+ "paodv i \<TTurnstile> (recvmsg rreq_rrep_sn \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
415
+ \<forall>dip\<in>kD(rt \<xi>). \<pi>\<^sub>3(the (rt \<xi> dip)) = unk \<or> 1 \<le> \<pi>\<^sub>2(the (rt \<xi> dip)))"
416
+ proof -
417
+ { fix sip rt dsn1 dsn2 dsk1 dsk2 flag1 flag2 hops1 hops2 nhip1 nhip2 pre1 pre2
418
+ assume "dsk1 = unk \<or> Suc 0 \<le> dsn2"
419
+ hence "\<pi>\<^sub>3(the (update rt sip (dsn1, dsk1, flag1, hops1, nhip1, pre1) sip)) = unk
420
+ \<or> Suc 0 \<le> sqn (update rt sip (dsn2, dsk2, flag2, hops2, nhip2, pre2)) sip"
421
+ unfolding update_def by (cases "dsk1 =unk") (clarsimp split: option.split)+
422
+ } note fromsip [simp] = this
423
+
424
+ { fix dip sip rt dsn1 dsn2 dsk1 dsk2 flag1 flag2 hops1 hops2 nhip1 nhip2 pre1 pre2
425
+ assume allkd: "\<forall>dip\<in>kD(rt). \<pi>\<^sub>3(the (rt dip)) = unk \<or> Suc 0 \<le> sqn rt dip"
426
+ and **: "dsk1 = unk \<or> Suc 0 \<le> dsn2"
427
+ have "\<forall>dip\<in>kD(rt). \<pi>\<^sub>3(the (update rt sip (dsn1, dsk1, flag1, hops1, nhip1, pre1) dip)) = unk
428
+ \<or> Suc 0 \<le> sqn (update rt sip (dsn2, dsk2, flag2, hops2, nhip2, pre2)) dip"
429
+ (is "\<forall>dip\<in>kD(rt). ?prop dip")
430
+ proof
431
+ fix dip
432
+ assume "dip\<in>kD(rt)"
433
+ thus "?prop dip"
434
+ proof (cases "dip = sip")
435
+ assume "dip = sip"
436
+ with ** show ?thesis
437
+ by simp
438
+ next
439
+ assume "dip \<noteq> sip"
440
+ with \<open>dip\<in>kD(rt)\<close> allkd show ?thesis
441
+ by simp
442
+ qed
443
+ qed
444
+ } note solve_update [simp] = this
445
+
446
+ { fix dip rt dests
447
+ assume *: "\<forall>ip\<in>dom(dests). ip\<in>kD(rt) \<and> sqn rt ip \<le> the (dests ip)"
448
+ and **: "\<forall>ip\<in>kD(rt). \<pi>\<^sub>3(the (rt ip)) = unk \<or> Suc 0 \<le> sqn rt ip"
449
+ have "\<forall>dip\<in>kD(rt). \<pi>\<^sub>3(the (rt dip)) = unk \<or> Suc 0 \<le> sqn (invalidate rt dests) dip"
450
+ proof
451
+ fix dip
452
+ assume "dip\<in>kD(rt)"
453
+ with ** have "\<pi>\<^sub>3(the (rt dip)) = unk \<or> Suc 0 \<le> sqn rt dip" ..
454
+ thus "\<pi>\<^sub>3 (the (rt dip)) = unk \<or> Suc 0 \<le> sqn (invalidate rt dests) dip"
455
+ proof
456
+ assume "\<pi>\<^sub>3(the (rt dip)) = unk" thus ?thesis ..
457
+ next
458
+ assume "Suc 0 \<le> sqn rt dip"
459
+ have "Suc 0 \<le> sqn (invalidate rt dests) dip"
460
+ proof (cases "dip\<in>dom(dests)")
461
+ assume "dip\<in>dom(dests)"
462
+ with * have "sqn rt dip \<le> the (dests dip)" by simp
463
+ with \<open>Suc 0 \<le> sqn rt dip\<close> have "Suc 0 \<le> the (dests dip)" by simp
464
+ with \<open>dip\<in>dom(dests)\<close> \<open>dip\<in>kD(rt)\<close> [THEN kD_Some] show ?thesis
465
+ unfolding invalidate_def sqn_def by auto
466
+ next
467
+ assume "dip\<notin>dom(dests)"
468
+ with \<open>Suc 0 \<le> sqn rt dip\<close> \<open>dip\<in>kD(rt)\<close> [THEN kD_Some] show ?thesis
469
+ unfolding invalidate_def sqn_def by auto
470
+ qed
471
+ thus ?thesis by (rule disjI2)
472
+ qed
473
+ qed
474
+ } note solve_invalidate [simp] = this
475
+
476
+ show ?thesis
477
+ by (inv_cterms inv add: onl_invariant_sterms_TT [OF aodv_wf addpreRT_welldefined]
478
+ onl_invariant_sterms [OF aodv_wf dests_bigger_than_sqn
479
+ [THEN invariant_restrict_inD]]
480
+ onl_invariant_sterms [OF aodv_wf osn_rreq]
481
+ onl_invariant_sterms [OF aodv_wf dsn_rrep]
482
+ simp add: proj3_inv proj2_eq_sqn)
483
+ qed
484
+
485
+ text \<open>Proposition 7.13\<close>
486
+
487
+ lemma rreq_rrep_sn_any_step_invariant:
488
+ "paodv i \<TTurnstile>\<^sub>A (recvmsg rreq_rrep_sn \<rightarrow>) onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast rreq_rrep_sn a)"
489
+ proof -
490
+ have sqnf_kno: "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
491
+ (l \<in> {PRreq-:16..PRreq-:18} \<longrightarrow> sqnf (rt \<xi>) (dip \<xi>) = kno))"
492
+ by (inv_cterms inv add: onl_invariant_sterms_TT [OF aodv_wf addpreRT_welldefined])
493
+ show ?thesis
494
+ by (inv_cterms inv add: onl_invariant_sterms_TT [OF aodv_wf addpreRT_welldefined]
495
+ onl_invariant_sterms [OF aodv_wf sequence_number_one_or_bigger
496
+ [THEN invariant_restrict_inD]]
497
+ onl_invariant_sterms [OF aodv_wf kD_unk_or_atleast_one]
498
+ onl_invariant_sterms_TT [OF aodv_wf sqnf_kno]
499
+ onl_invariant_sterms [OF aodv_wf osn_rreq]
500
+ onl_invariant_sterms [OF aodv_wf dsn_rrep])
501
+ (auto simp: proj2_eq_sqn)
502
+ qed
503
+
504
+ text \<open>Proposition 7.14\<close>
505
+
506
+ lemma rreq_rrep_fresh_any_step_invariant:
507
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), a, _). anycast (rreq_rrep_fresh (rt \<xi>)) a)"
508
+ proof -
509
+ have rreq_oip: "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
510
+ (l \<in> {PRreq-:3, PRreq-:4, PRreq-:15, PRreq-:27}
511
+ \<longrightarrow> oip \<xi> \<in> kD(rt \<xi>)
512
+ \<and> (sqn (rt \<xi>) (oip \<xi>) > (osn \<xi>)
513
+ \<or> (sqn (rt \<xi>) (oip \<xi>) = (osn \<xi>)
514
+ \<and> the (dhops (rt \<xi>) (oip \<xi>)) \<le> Suc (hops \<xi>)
515
+ \<and> the (flag (rt \<xi>) (oip \<xi>)) = val))))"
516
+ proof inv_cterms
517
+ fix l \<xi> l' pp p'
518
+ assume "(\<xi>, pp) \<in> reachable (paodv i) TT"
519
+ and "{PRreq-:2}\<lbrakk>\<lambda>\<xi>. \<xi>\<lparr>rt :=
520
+ update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})\<rparr>\<rbrakk> p' \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
521
+ and "l' = PRreq-:3"
522
+ show "osn \<xi> < sqn (update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})) (oip \<xi>)
523
+ \<or> (sqn (update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})) (oip \<xi>) = osn \<xi>
524
+ \<and> the (dhops (update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})) (oip \<xi>))
525
+ \<le> Suc (hops \<xi>)
526
+ \<and> the (flag (update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})) (oip \<xi>))
527
+ = val)"
528
+ unfolding update_def by (clarsimp split: option.split)
529
+ (metis linorder_neqE_nat not_less)
530
+ qed
531
+
532
+ have rrep_prrep: "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
533
+ (l \<in> {PRrep-:2..PRrep-:7} \<longrightarrow> (dip \<xi> \<in> kD(rt \<xi>)
534
+ \<and> sqn (rt \<xi>) (dip \<xi>) = dsn \<xi>
535
+ \<and> the (dhops (rt \<xi>) (dip \<xi>)) = Suc (hops \<xi>)
536
+ \<and> the (flag (rt \<xi>) (dip \<xi>)) = val
537
+ \<and> the (nhop (rt \<xi>) (dip \<xi>)) \<in> kD (rt \<xi>))))"
538
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf rrep_1_update_changes]
539
+ onl_invariant_sterms [OF aodv_wf sip_in_kD])
540
+
541
+ show ?thesis
542
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf rreq_oip]
543
+ onl_invariant_sterms [OF aodv_wf rreq_dip_in_vD_dip_eq_ip]
544
+ onl_invariant_sterms [OF aodv_wf rrep_prrep])
545
+ qed
546
+
547
+ text \<open>Proposition 7.15\<close>
548
+
549
+ lemma rerr_invalid_any_step_invariant:
550
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), a, _). anycast (rerr_invalid (rt \<xi>)) a)"
551
+ proof -
552
+ have dests_inv: "paodv i \<TTurnstile>
553
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PAodv-:15, PPkt-:7, PRreq-:9,
554
+ PRreq-:21, PRrep-:10, PRerr-:1}
555
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>vD(rt \<xi>)))
556
+ \<and> (l \<in> {PAodv-:16..PAodv-:19}
557
+ \<union> {PPkt-:8..PPkt-:11}
558
+ \<union> {PRreq-:10..PRreq-:13}
559
+ \<union> {PRreq-:22..PRreq-:25}
560
+ \<union> {PRrep-:11..PRrep-:14}
561
+ \<union> {PRerr-:2..PRerr-:5} \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>iD(rt \<xi>)
562
+ \<and> the (dests \<xi> ip) = sqn (rt \<xi>) ip))
563
+ \<and> (l = PPkt-:14 \<longrightarrow> dip \<xi>\<in>iD(rt \<xi>)))"
564
+ by inv_cterms (clarsimp split: if_split_asm option.split_asm simp: domIff)+
565
+ show ?thesis
566
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf dests_inv])
567
+ qed
568
+
569
+ text \<open>Proposition 7.16\<close>
570
+
571
+ text \<open>
572
+ Some well-definedness obligations are irrelevant for the Isabelle development:
573
+
574
+ \begin{enumerate}
575
+ \item In each routing table there is at most one entry for each destination: guaranteed by type.
576
+
577
+ \item In each store of queued data packets there is at most one data queue for
578
+ each destination: guaranteed by structure.
579
+
580
+ \item Whenever a set of pairs @{term "(rip, rsn)"} is assigned to the variable
581
+ @{term "dests"} of type @{typ "ip \<rightharpoonup> sqn"}, or to the first argument of
582
+ the function @{term "rerr"}, this set is a partial function, i.e., there
583
+ is at most one entry @{term "(rip, rsn)"} for each destination
584
+ @{term "rip"}: guaranteed by type.
585
+ \end{enumerate}
586
+ \<close>
587
+
588
+ lemma dests_vD_inc_sqn:
589
+ "paodv i \<TTurnstile>
590
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PAodv-:15, PPkt-:7, PRreq-:9, PRreq-:21, PRrep-:10}
591
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>vD(rt \<xi>) \<and> the (dests \<xi> ip) = inc (sqn (rt \<xi>) ip)))
592
+ \<and> (l = PRerr-:1
593
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>vD(rt \<xi>) \<and> the (dests \<xi> ip) > sqn (rt \<xi>) ip)))"
594
+ by inv_cterms (clarsimp split: if_split_asm option.split_asm)+
595
+
596
+ text \<open>Proposition 7.27\<close>
597
+
598
+ lemma route_tables_fresher:
599
+ "paodv i \<TTurnstile>\<^sub>A (recvmsg rreq_rrep_sn \<rightarrow>) onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)).
600
+ \<forall>dip\<in>kD(rt \<xi>). rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>')"
601
+ proof (inv_cterms inv add:
602
+ onl_invariant_sterms [OF aodv_wf dests_vD_inc_sqn [THEN invariant_restrict_inD]]
603
+ onl_invariant_sterms [OF aodv_wf hop_count_positive [THEN invariant_restrict_inD]]
604
+ onl_invariant_sterms [OF aodv_wf osn_rreq]
605
+ onl_invariant_sterms [OF aodv_wf dsn_rrep]
606
+ onl_invariant_sterms [OF aodv_wf addpreRT_welldefined [THEN invariant_restrict_inD]])
607
+ fix \<xi> pp p'
608
+ assume "(\<xi>, pp) \<in> reachable (paodv i) (recvmsg rreq_rrep_sn)"
609
+ and "{PRreq-:2}\<lbrakk>\<lambda>\<xi>. \<xi>\<lparr>rt := update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})\<rparr>\<rbrakk>
610
+ p' \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
611
+ and "Suc 0 \<le> osn \<xi>"
612
+ and *: "\<forall>ip\<in>kD (rt \<xi>). Suc 0 \<le> the (dhops (rt \<xi>) ip)"
613
+ show "\<forall>ip\<in>kD (rt \<xi>). rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})"
614
+ proof
615
+ fix ip
616
+ assume "ip\<in>kD (rt \<xi>)"
617
+ moreover with * have "1 \<le> the (dhops (rt \<xi>) ip)" by simp
618
+ moreover from \<open>Suc 0 \<le> osn \<xi>\<close>
619
+ have "update_arg_wf (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})" ..
620
+ ultimately show "rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})"
621
+ by (rule rt_fresher_update)
622
+ qed
623
+ next
624
+ fix \<xi> pp p'
625
+ assume "(\<xi>, pp) \<in> reachable (paodv i) (recvmsg rreq_rrep_sn)"
626
+ and "{PRrep-:1}\<lbrakk>\<lambda>\<xi>. \<xi>\<lparr>rt := update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})\<rparr>\<rbrakk>
627
+ p' \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
628
+ and "Suc 0 \<le> dsn \<xi>"
629
+ and *: "\<forall>ip\<in>kD (rt \<xi>). Suc 0 \<le> the (dhops (rt \<xi>) ip)"
630
+ show "\<forall>ip\<in>kD (rt \<xi>). rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})"
631
+ proof
632
+ fix ip
633
+ assume "ip\<in>kD (rt \<xi>)"
634
+ moreover with * have "1 \<le> the (dhops (rt \<xi>) ip)" by simp
635
+ moreover from \<open>Suc 0 \<le> dsn \<xi>\<close>
636
+ have "update_arg_wf (dsn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})" ..
637
+ ultimately show "rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})"
638
+ by (rule rt_fresher_update)
639
+ qed
640
+ qed
641
+
642
+ end
643
+
formal/afp/AODV/document/root.tex ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % vim:nojs:spelllang=en_au tw=76 sw=4 sts=4 fo+=awn fmr={-{,}-} et ts=8
2
+ \documentclass[11pt,a4paper]{report}
3
+ \usepackage[T1]{fontenc}
4
+ \usepackage{isabelle,isabellesym}
5
+
6
+ \usepackage{amssymb}
7
+ \usepackage[only,bigsqcap]{stmaryrd}
8
+
9
+ \usepackage{mathpartir}
10
+ \usepackage[margin=10mm,bottom=15mm]{geometry}
11
+ \usepackage[final]{graphicx}
12
+
13
+ % this should be the last package used
14
+ \usepackage{pdfsetup}
15
+
16
+ % urls in roman style, theory text in math-similar italics
17
+ \urlstyle{rm}
18
+ \isabellestyle{rm}
19
+
20
+ \begin{document}
21
+
22
+ \title{Loop freedom of the (untimed) AODV routing protocol}
23
+ \author{Timothy Bourke\thanks{Inria,
24
+ \'Ecole normale sup\'erieure,
25
+ and NICTA}
26
+ \and
27
+ Peter H\"ofner\thanks{NICTA
28
+ and Computer Science and Engineering, UNSW}}
29
+ \maketitle
30
+
31
+ \begin{abstract}
32
+ The Ad hoc On-demand Distance Vector (AODV) routing protocol~\cite{RFC3561}
33
+ allows the nodes in a Mobile Ad hoc Network (MANET) or a Wireless Mesh
34
+ Network (WMN) to know where to forward data packets. Such a protocol is
35
+ `loop free' if it never leads to routing decisions that forward packets in
36
+ circles.
37
+
38
+ This development mechanises an existing pen-and-paper proof of loop freedom
39
+ of AODV~\cite{FehnkerEtAl:AWN:2013}.
40
+ The protocol is modelled in the Algebra of Wireless Networks (AWN),
41
+ which is the subject of an earlier paper~\cite{BourkeEtAl:MechAWN:2014} and
42
+ mechanization~\cite{Bourke14}.
43
+ The proof relies on a novel compositional approach for lifting invariants to
44
+ networks of nodes.
45
+
46
+ We exploit the mechanization to analyse several variants of AODV and show
47
+ that Isabelle/HOL can re-establish most proof obligations automatically and
48
+ identify exactly the steps that are no longer valid.
49
+ Each of the variants is essentially a modified copy of the main development.
50
+
51
+ Further documentation is available in~\cite{BourkevGlHof:ATVA:2014}.
52
+
53
+ \centering{\includegraphics[width=\textwidth]{session_graph}}
54
+ \end{abstract}
55
+
56
+ \newpage
57
+ \tableofcontents
58
+
59
+ % sane default for proof documents
60
+ \parindent 0pt\parskip 0.5ex
61
+
62
+ % generated text of all theories
63
+ \newpage
64
+ \input{session}
65
+
66
+ % optional bibliography
67
+ \bibliographystyle{abbrv}
68
+ \bibliography{root}
69
+
70
+ \end{document}
formal/afp/AODV/variants/a_norreqid/A_Aodv.thy ADDED
@@ -0,0 +1,532 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Aodv.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ section "The AODV protocol"
8
+
9
+ theory A_Aodv
10
+ imports A_Aodv_Data A_Aodv_Message
11
+ AWN.AWN_SOS_Labels AWN.AWN_Invariants
12
+ begin
13
+
14
+ subsection "Data state"
15
+
16
+ record state =
17
+ ip :: "ip"
18
+ sn :: "sqn"
19
+ rt :: "rt"
20
+ rreqs :: "(ip \<times> sqn) set"
21
+ store :: "store"
22
+ (* all locals *)
23
+ msg :: "msg"
24
+ data :: "data"
25
+ dests :: "ip \<rightharpoonup> sqn"
26
+ pre :: "ip set"
27
+ dip :: "ip"
28
+ oip :: "ip"
29
+ hops :: "nat"
30
+ dsn :: "sqn"
31
+ dsk :: "k"
32
+ osn :: "sqn"
33
+ sip :: "ip"
34
+
35
+ abbreviation aodv_init :: "ip \<Rightarrow> state"
36
+ where "aodv_init i \<equiv> \<lparr>
37
+ ip = i,
38
+ sn = 1,
39
+ rt = Map.empty,
40
+ rreqs = {},
41
+ store = Map.empty,
42
+
43
+ msg = (SOME x. True),
44
+ data = (SOME x. True),
45
+ dests = (SOME x. True),
46
+ pre = (SOME x. True),
47
+ dip = (SOME x. True),
48
+ oip = (SOME x. True),
49
+ hops = (SOME x. True),
50
+ dsn = (SOME x. True),
51
+ dsk = (SOME x. True),
52
+ osn = (SOME x. True),
53
+ sip = (SOME x. x \<noteq> i)
54
+ \<rparr>"
55
+
56
+ lemma some_neq_not_eq [simp]: "\<not>((SOME x :: nat. x \<noteq> i) = i)"
57
+ by (subst some_eq_ex) (metis zero_neq_numeral)
58
+
59
+ definition clear_locals :: "state \<Rightarrow> state"
60
+ where "clear_locals \<xi> = \<xi> \<lparr>
61
+ msg := (SOME x. True),
62
+ data := (SOME x. True),
63
+ dests := (SOME x. True),
64
+ pre := (SOME x. True),
65
+ dip := (SOME x. True),
66
+ oip := (SOME x. True),
67
+ hops := (SOME x. True),
68
+ dsn := (SOME x. True),
69
+ dsk := (SOME x. True),
70
+ osn := (SOME x. True),
71
+ sip := (SOME x. x \<noteq> ip \<xi>)
72
+ \<rparr>"
73
+
74
+ lemma clear_locals_sip_not_ip [simp]: "\<not>(sip (clear_locals \<xi>) = ip \<xi>)"
75
+ unfolding clear_locals_def by simp
76
+
77
+ lemma clear_locals_but_not_globals [simp]:
78
+ "ip (clear_locals \<xi>) = ip \<xi>"
79
+ "sn (clear_locals \<xi>) = sn \<xi>"
80
+ "rt (clear_locals \<xi>) = rt \<xi>"
81
+ "rreqs (clear_locals \<xi>) = rreqs \<xi>"
82
+ "store (clear_locals \<xi>) = store \<xi>"
83
+ unfolding clear_locals_def by auto
84
+
85
+ subsection "Auxilliary message handling definitions"
86
+
87
+ definition is_newpkt
88
+ where "is_newpkt \<xi> \<equiv> case msg \<xi> of
89
+ Newpkt data' dip' \<Rightarrow> { \<xi>\<lparr>data := data', dip := dip'\<rparr> }
90
+ | _ \<Rightarrow> {}"
91
+
92
+ definition is_pkt
93
+ where "is_pkt \<xi> \<equiv> case msg \<xi> of
94
+ Pkt data' dip' oip' \<Rightarrow> { \<xi>\<lparr> data := data', dip := dip', oip := oip' \<rparr> }
95
+ | _ \<Rightarrow> {}"
96
+
97
+ definition is_rreq
98
+ where "is_rreq \<xi> \<equiv> case msg \<xi> of
99
+ Rreq hops' dip' dsn' dsk' oip' osn' sip' \<Rightarrow>
100
+ { \<xi>\<lparr> hops := hops', dip := dip', dsn := dsn',
101
+ dsk := dsk', oip := oip', osn := osn', sip := sip' \<rparr> }
102
+ | _ \<Rightarrow> {}"
103
+
104
+ lemma is_rreq_asm [dest!]:
105
+ assumes "\<xi>' \<in> is_rreq \<xi>"
106
+ shows "(\<exists>hops' rreqid' dip' dsn' dsk' oip' osn' sip'.
107
+ msg \<xi> = Rreq hops' dip' dsn' dsk' oip' osn' sip' \<and>
108
+ \<xi>' = \<xi>\<lparr> hops := hops', dip := dip', dsn := dsn',
109
+ dsk := dsk', oip := oip', osn := osn', sip := sip' \<rparr>)"
110
+ using assms unfolding is_rreq_def
111
+ by (cases "msg \<xi>") simp_all
112
+
113
+ definition is_rrep
114
+ where "is_rrep \<xi> \<equiv> case msg \<xi> of
115
+ Rrep hops' dip' dsn' oip' sip' \<Rightarrow>
116
+ { \<xi>\<lparr> hops := hops', dip := dip', dsn := dsn', oip := oip', sip := sip' \<rparr> }
117
+ | _ \<Rightarrow> {}"
118
+
119
+ lemma is_rrep_asm [dest!]:
120
+ assumes "\<xi>' \<in> is_rrep \<xi>"
121
+ shows "(\<exists>hops' dip' dsn' oip' sip'.
122
+ msg \<xi> = Rrep hops' dip' dsn' oip' sip' \<and>
123
+ \<xi>' = \<xi>\<lparr> hops := hops', dip := dip', dsn := dsn', oip := oip', sip := sip' \<rparr>)"
124
+ using assms unfolding is_rrep_def
125
+ by (cases "msg \<xi>") simp_all
126
+
127
+ definition is_rerr
128
+ where "is_rerr \<xi> \<equiv> case msg \<xi> of
129
+ Rerr dests' sip' \<Rightarrow> { \<xi>\<lparr> dests := dests', sip := sip' \<rparr> }
130
+ | _ \<Rightarrow> {}"
131
+
132
+ lemma is_rerr_asm [dest!]:
133
+ assumes "\<xi>' \<in> is_rerr \<xi>"
134
+ shows "(\<exists>dests' sip'.
135
+ msg \<xi> = Rerr dests' sip' \<and>
136
+ \<xi>' = \<xi>\<lparr> dests := dests', sip := sip' \<rparr>)"
137
+ using assms unfolding is_rerr_def
138
+ by (cases "msg \<xi>") simp_all
139
+
140
+ lemmas is_msg_defs =
141
+ is_rerr_def is_rrep_def is_rreq_def is_pkt_def is_newpkt_def
142
+
143
+ lemma is_msg_inv_ip [simp]:
144
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
145
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
146
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
147
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
148
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
149
+ unfolding is_msg_defs
150
+ by (cases "msg \<xi>", clarsimp+)+
151
+
152
+ lemma is_msg_inv_sn [simp]:
153
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
154
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
155
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
156
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
157
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
158
+ unfolding is_msg_defs
159
+ by (cases "msg \<xi>", clarsimp+)+
160
+
161
+ lemma is_msg_inv_rt [simp]:
162
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
163
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
164
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
165
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
166
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
167
+ unfolding is_msg_defs
168
+ by (cases "msg \<xi>", clarsimp+)+
169
+
170
+ lemma is_msg_inv_rreqs [simp]:
171
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
172
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
173
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
174
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
175
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
176
+ unfolding is_msg_defs
177
+ by (cases "msg \<xi>", clarsimp+)+
178
+
179
+ lemma is_msg_inv_store [simp]:
180
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
181
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
182
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
183
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
184
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
185
+ unfolding is_msg_defs
186
+ by (cases "msg \<xi>", clarsimp+)+
187
+
188
+ lemma is_msg_inv_sip [simp]:
189
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> sip \<xi>' = sip \<xi>"
190
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> sip \<xi>' = sip \<xi>"
191
+ unfolding is_msg_defs
192
+ by (cases "msg \<xi>", clarsimp+)+
193
+
194
+ subsection "The protocol process"
195
+
196
+ datatype pseqp =
197
+ PAodv
198
+ | PNewPkt
199
+ | PPkt
200
+ | PRreq
201
+ | PRrep
202
+ | PRerr
203
+
204
+ fun nat_of_seqp :: "pseqp \<Rightarrow> nat"
205
+ where
206
+ "nat_of_seqp PAodv = 1"
207
+ | "nat_of_seqp PPkt = 2"
208
+ | "nat_of_seqp PNewPkt = 3"
209
+ | "nat_of_seqp PRreq = 4"
210
+ | "nat_of_seqp PRrep = 5"
211
+ | "nat_of_seqp PRerr = 6"
212
+
213
+ instantiation "pseqp" :: ord
214
+ begin
215
+ definition less_eq_seqp [iff]: "l1 \<le> l2 = (nat_of_seqp l1 \<le> nat_of_seqp l2)"
216
+ definition less_seqp [iff]: "l1 < l2 = (nat_of_seqp l1 < nat_of_seqp l2)"
217
+ instance ..
218
+ end
219
+
220
+ abbreviation AODV
221
+ where
222
+ "AODV \<equiv> \<lambda>_. \<lbrakk>clear_locals\<rbrakk> call(PAodv)"
223
+
224
+ abbreviation PKT
225
+ where
226
+ "PKT args \<equiv>
227
+
228
+ \<lbrakk>\<xi>. let (data, dip, oip) = args \<xi> in
229
+ (clear_locals \<xi>) \<lparr> data := data, dip := dip, oip := oip \<rparr>\<rbrakk>
230
+ call(PPkt)"
231
+ abbreviation NEWPKT
232
+ where
233
+ "NEWPKT args \<equiv>
234
+ \<lbrakk>\<xi>. let (data, dip) = args \<xi> in
235
+ (clear_locals \<xi>) \<lparr> data := data, dip := dip \<rparr>\<rbrakk>
236
+ call(PNewPkt)"
237
+
238
+ abbreviation RREQ
239
+ where
240
+ "RREQ args \<equiv>
241
+ \<lbrakk>\<xi>. let (hops, dip, dsn, dsk, oip, osn, sip) = args \<xi> in
242
+ (clear_locals \<xi>) \<lparr> hops := hops, dip := dip,
243
+ dsn := dsn, dsk := dsk, oip := oip,
244
+ osn := osn, sip := sip \<rparr>\<rbrakk>
245
+ call(PRreq)"
246
+
247
+ abbreviation RREP
248
+ where
249
+ "RREP args \<equiv>
250
+ \<lbrakk>\<xi>. let (hops, dip, dsn, oip, sip) = args \<xi> in
251
+ (clear_locals \<xi>) \<lparr> hops := hops, dip := dip, dsn := dsn,
252
+ oip := oip, sip := sip \<rparr>\<rbrakk>
253
+ call(PRrep)"
254
+
255
+ abbreviation RERR
256
+ where
257
+ "RERR args \<equiv>
258
+ \<lbrakk>\<xi>. let (dests, sip) = args \<xi> in
259
+ (clear_locals \<xi>) \<lparr> dests := dests, sip := sip \<rparr>\<rbrakk>
260
+ call(PRerr)"
261
+
262
+ fun \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V :: "(state, msg, pseqp, pseqp label) seqp_env"
263
+ where
264
+ "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv = labelled PAodv (
265
+ receive(\<lambda>msg' \<xi>. \<xi> \<lparr> msg := msg' \<rparr>).
266
+ ( \<langle>is_newpkt\<rangle> NEWPKT(\<lambda>\<xi>. (data \<xi>, ip \<xi>))
267
+ \<oplus> \<langle>is_pkt\<rangle> PKT(\<lambda>\<xi>. (data \<xi>, dip \<xi>, oip \<xi>))
268
+ \<oplus> \<langle>is_rreq\<rangle>
269
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
270
+ RREQ(\<lambda>\<xi>. (hops \<xi>, dip \<xi>, dsn \<xi>, dsk \<xi>, oip \<xi>, osn \<xi>, sip \<xi>))
271
+ \<oplus> \<langle>is_rrep\<rangle>
272
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
273
+ RREP(\<lambda>\<xi>. (hops \<xi>, dip \<xi>, dsn \<xi>, oip \<xi>, sip \<xi>))
274
+ \<oplus> \<langle>is_rerr\<rangle>
275
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
276
+ RERR(\<lambda>\<xi>. (dests \<xi>, sip \<xi>))
277
+ )
278
+ \<oplus> \<langle>\<lambda>\<xi>. { \<xi>\<lparr> dip := dip \<rparr> | dip. dip \<in> qD(store \<xi>) \<inter> vD(rt \<xi>) }\<rangle>
279
+ \<lbrakk>\<xi>. \<xi> \<lparr> data := hd(\<sigma>\<^bsub>queue\<^esub>(store \<xi>, dip \<xi>)) \<rparr>\<rbrakk>
280
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (dip \<xi>)), \<lambda>\<xi>. pkt(data \<xi>, dip \<xi>, ip \<xi>)).
281
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := the (drop (dip \<xi>) (store \<xi>)) \<rparr>\<rbrakk>
282
+ AODV()
283
+ \<triangleright> \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (dip \<xi>))
284
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
285
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
286
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
287
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
288
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
289
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
290
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)). AODV()
291
+ \<oplus> \<langle>\<lambda>\<xi>. { \<xi>\<lparr> dip := dip \<rparr>
292
+ | dip. dip \<in> qD(store \<xi>) - vD(rt \<xi>) \<and> the (\<sigma>\<^bsub>p-flag\<^esub>(store \<xi>, dip)) = req }\<rangle>
293
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := unsetRRF (store \<xi>) (dip \<xi>) \<rparr>\<rbrakk>
294
+ \<lbrakk>\<xi>. \<xi> \<lparr> sn := inc (sn \<xi>) \<rparr>\<rbrakk>
295
+ \<lbrakk>\<xi>. \<xi> \<lparr> rreqs := rreqs \<xi> \<union> {(ip \<xi>, sn \<xi>)} \<rparr>\<rbrakk>
296
+ broadcast(\<lambda>\<xi>. rreq(0, dip \<xi>, sqn (rt \<xi>) (dip \<xi>), sqnf (rt \<xi>) (dip \<xi>),
297
+ ip \<xi>, sn \<xi>, ip \<xi>)). AODV())"
298
+
299
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt = labelled PNewPkt (
300
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
301
+ deliver(\<lambda>\<xi>. data \<xi>).AODV()
302
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
303
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := add (data \<xi>) (dip \<xi>) (store \<xi>) \<rparr>\<rbrakk>
304
+ AODV())"
305
+
306
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt = labelled PPkt (
307
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
308
+ deliver(\<lambda>\<xi>. data \<xi>).AODV()
309
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
310
+ (
311
+ \<langle>\<xi>. dip \<xi> \<in> vD (rt \<xi>)\<rangle>
312
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (dip \<xi>)), \<lambda>\<xi>. pkt(data \<xi>, dip \<xi>, oip \<xi>)).AODV()
313
+ \<triangleright>
314
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (dip \<xi>))
315
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
316
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
317
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
318
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
319
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
320
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
321
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
322
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> vD (rt \<xi>)\<rangle>
323
+ (
324
+ \<langle>\<xi>. dip \<xi> \<in> iD (rt \<xi>)\<rangle>
325
+ groupcast(\<lambda>\<xi>. the (precs (rt \<xi>) (dip \<xi>)),
326
+ \<lambda>\<xi>. rerr([dip \<xi> \<mapsto> sqn (rt \<xi>) (dip \<xi>)], ip \<xi>)). AODV()
327
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> iD (rt \<xi>)\<rangle>
328
+ AODV()
329
+ )
330
+ ))"
331
+
332
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq = labelled PRreq (
333
+ \<langle>\<xi>. (oip \<xi>, osn \<xi>) \<in> rreqs \<xi>\<rangle>
334
+ AODV()
335
+ \<oplus> \<langle>\<xi>. (oip \<xi>, osn \<xi>) \<notin> rreqs \<xi>\<rangle>
336
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rparr>\<rbrakk>
337
+ \<lbrakk>\<xi>. \<xi> \<lparr> rreqs := rreqs \<xi> \<union> {(oip \<xi>, osn \<xi>)} \<rparr>\<rbrakk>
338
+ (
339
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
340
+ \<lbrakk>\<xi>. \<xi> \<lparr> sn := max (sn \<xi>) (dsn \<xi>) \<rparr>\<rbrakk>
341
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(0, dip \<xi>, sn \<xi>, oip \<xi>, ip \<xi>)).AODV()
342
+ \<triangleright>
343
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
344
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
345
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
346
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
347
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
348
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
349
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
350
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
351
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
352
+ (
353
+ \<langle>\<xi>. dip \<xi> \<in> vD (rt \<xi>) \<and> dsn \<xi> \<le> sqn (rt \<xi>) (dip \<xi>) \<and> sqnf (rt \<xi>) (dip \<xi>) = kno\<rangle>
354
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (dip \<xi>) {sip \<xi>}) \<rparr>\<rbrakk>
355
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (oip \<xi>) {the (nhop (rt \<xi>) (dip \<xi>))}) \<rparr>\<rbrakk>
356
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(the (dhops (rt \<xi>) (dip \<xi>)), dip \<xi>,
357
+ sqn (rt \<xi>) (dip \<xi>), oip \<xi>, ip \<xi>)).
358
+ AODV()
359
+ \<triangleright>
360
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
361
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
362
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
363
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
364
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
365
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
366
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
367
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
368
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> vD (rt \<xi>) \<or> sqn (rt \<xi>) (dip \<xi>) < dsn \<xi> \<or> sqnf (rt \<xi>) (dip \<xi>) = unk\<rangle>
369
+ broadcast(\<lambda>\<xi>. rreq(hops \<xi> + 1, dip \<xi>, max (sqn (rt \<xi>) (dip \<xi>)) (dsn \<xi>),
370
+ dsk \<xi>, oip \<xi>, osn \<xi>, ip \<xi>)).
371
+ AODV()
372
+ )
373
+ ))"
374
+
375
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep = labelled PRrep (
376
+ \<langle>\<xi>. rt \<xi> \<noteq> update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rangle>
377
+ (
378
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rparr> \<rbrakk>
379
+ (
380
+ \<langle>\<xi>. oip \<xi> = ip \<xi> \<rangle>
381
+ AODV()
382
+ \<oplus> \<langle>\<xi>. oip \<xi> \<noteq> ip \<xi> \<rangle>
383
+ (
384
+ \<langle>\<xi>. oip \<xi> \<in> vD (rt \<xi>)\<rangle>
385
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (dip \<xi>) {the (nhop (rt \<xi>) (oip \<xi>))}) \<rparr>\<rbrakk>
386
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (the (nhop (rt \<xi>) (dip \<xi>)))
387
+ {the (nhop (rt \<xi>) (oip \<xi>))}) \<rparr>\<rbrakk>
388
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(hops \<xi> + 1, dip \<xi>, dsn \<xi>, oip \<xi>, ip \<xi>)).
389
+ AODV()
390
+ \<triangleright>
391
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
392
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
393
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
394
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
395
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
396
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
397
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
398
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
399
+ \<oplus> \<langle>\<xi>. oip \<xi> \<notin> vD (rt \<xi>)\<rangle>
400
+ AODV()
401
+ )
402
+ )
403
+ )
404
+ \<oplus> \<langle>\<xi>. rt \<xi> = update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rangle>
405
+ AODV()
406
+ )"
407
+
408
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr = labelled PRerr (
409
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. case (dests \<xi>) rip of None \<Rightarrow> None
410
+ | Some rsn \<Rightarrow> if rip \<in> vD (rt \<xi>) \<and> the (nhop (rt \<xi>) rip) = sip \<xi>
411
+ \<and> sqn (rt \<xi>) rip < rsn then Some rsn else None) \<rparr>\<rbrakk>
412
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
413
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
414
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
415
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
416
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
417
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)). AODV())"
418
+
419
+ declare \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V.simps [simp del, code del]
420
+ lemmas \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps [simp, code] = \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V.simps [simplified]
421
+
422
+ fun \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton
423
+ where
424
+ "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PAodv = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv)"
425
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PNewPkt = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt)"
426
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PPkt = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt)"
427
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRreq = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq)"
428
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRrep = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep)"
429
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRerr = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr)"
430
+
431
+ lemma \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton_wf [simp]:
432
+ "wellformed \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton"
433
+ proof (rule, intro allI)
434
+ fix pn pn'
435
+ show "call(pn') \<notin> stermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton pn)"
436
+ by (cases pn) simp_all
437
+ qed
438
+
439
+ declare \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton.simps [simp del, code del]
440
+ lemmas \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton_simps [simp, code]
441
+ = \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton.simps [simplified \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps seqp_skeleton.simps]
442
+
443
+ lemma aodv_proc_cases [dest]:
444
+ fixes p pn
445
+ shows "p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn) \<Longrightarrow>
446
+ (p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv) \<or>
447
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt) \<or>
448
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt) \<or>
449
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq) \<or>
450
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep) \<or>
451
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr))"
452
+ by (cases pn) simp_all
453
+
454
+ definition \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V :: "ip \<Rightarrow> (state \<times> (state, msg, pseqp, pseqp label) seqp) set"
455
+ where "\<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<equiv> {(aodv_init i, \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv)}"
456
+
457
+ abbreviation paodv
458
+ :: "ip \<Rightarrow> (state \<times> (state, msg, pseqp, pseqp label) seqp, msg seq_action) automaton"
459
+ where
460
+ "paodv i \<equiv> \<lparr> init = \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i, trans = seqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V \<rparr>"
461
+
462
+ lemma aodv_trans: "trans (paodv i) = seqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
463
+ by simp
464
+
465
+ lemma aodv_control_within [simp]: "control_within \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (init (paodv i))"
466
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by (rule control_withinI) (auto simp del: \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps)
467
+
468
+ lemma aodv_wf [simp]:
469
+ "wellformed \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
470
+ proof (rule, intro allI)
471
+ fix pn pn'
472
+ show "call(pn') \<notin> stermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn)"
473
+ by (cases pn) simp_all
474
+ qed
475
+
476
+ lemmas aodv_labels_not_empty [simp] = labels_not_empty [OF aodv_wf]
477
+
478
+ lemma aodv_ex_label [intro]: "\<exists>l. l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
479
+ by (metis aodv_labels_not_empty all_not_in_conv)
480
+
481
+ lemma aodv_ex_labelE [elim]:
482
+ assumes "\<forall>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p. P l p"
483
+ and "\<exists>p l. P l p \<Longrightarrow> Q"
484
+ shows "Q"
485
+ using assms by (metis aodv_ex_label)
486
+
487
+ lemma aodv_simple_labels [simp]: "simple_labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
488
+ proof
489
+ fix pn p
490
+ assume "p\<in>subterms(\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn)"
491
+ thus "\<exists>!l. labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p = {l}"
492
+ by (cases pn) (simp_all cong: seqp_congs | elim disjE)+
493
+ qed
494
+
495
+ lemma \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_labels [simp]: "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<Longrightarrow> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p = {PAodv-:0}"
496
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
497
+
498
+ lemma aodv_init_kD_empty [simp]:
499
+ "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<Longrightarrow> kD (rt \<xi>) = {}"
500
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def kD_def by simp
501
+
502
+ lemma aodv_init_sip_not_ip [simp]: "\<not>(sip (aodv_init i) = i)" by simp
503
+
504
+ lemma aodv_init_sip_not_ip' [simp]:
505
+ assumes "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
506
+ shows "sip \<xi> \<noteq> ip \<xi>"
507
+ using assms unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
508
+
509
+ lemma aodv_init_sip_not_i [simp]:
510
+ assumes "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
511
+ shows "sip \<xi> \<noteq> i"
512
+ using assms unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
513
+
514
+ lemma clear_locals_sip_not_ip':
515
+ assumes "ip \<xi> = i"
516
+ shows "\<not>(sip (clear_locals \<xi>) = i)"
517
+ using assms by auto
518
+
519
+ text \<open>Stop the simplifier from descending into process terms.\<close>
520
+ declare seqp_congs [cong]
521
+
522
+ text \<open>Configure the main invariant tactic for AODV.\<close>
523
+
524
+ declare
525
+ \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps [cterms_env]
526
+ aodv_proc_cases [ctermsl_cases]
527
+ seq_invariant_ctermsI [OF aodv_wf aodv_control_within aodv_simple_labels aodv_trans,
528
+ cterms_intros]
529
+ seq_step_invariant_ctermsI [OF aodv_wf aodv_control_within aodv_simple_labels aodv_trans,
530
+ cterms_intros]
531
+
532
+ end
formal/afp/AODV/variants/a_norreqid/A_Aodv_Data.thy ADDED
@@ -0,0 +1,986 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Aodv_Data.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ section "Predicates and functions used in the AODV model"
8
+
9
+ theory A_Aodv_Data
10
+ imports A_Norreqid
11
+ begin
12
+
13
+ subsection "Sequence Numbers"
14
+
15
+ text \<open>Sequence numbers approximate the relative freshness of routing information.\<close>
16
+
17
+ definition inc :: "sqn \<Rightarrow> sqn"
18
+ where "inc sn \<equiv> if sn = 0 then sn else sn + 1"
19
+
20
+ lemma less_than_inc [simp]: "x \<le> inc x"
21
+ unfolding inc_def by simp
22
+
23
+ lemma inc_minus_suc_0 [simp]:
24
+ "inc x - Suc 0 = x"
25
+ unfolding inc_def by simp
26
+
27
+ lemma inc_never_one' [simp, intro]: "inc x \<noteq> Suc 0"
28
+ unfolding inc_def by simp
29
+
30
+ lemma inc_never_one [simp, intro]: "inc x \<noteq> 1"
31
+ by simp
32
+
33
+ subsection "Modelling Routes"
34
+
35
+ text \<open>
36
+ A route is a 6-tuple, @{term "(dsn, dsk, flag, hops, nhip, pre)"} where
37
+ @{term dsn} is the `destination sequence number', @{term dsk} is the
38
+ `destination-sequence-number status', @{term flag} is the route status,
39
+ @{term hops} is the number of hops to the destination, @{term nhip} is the
40
+ next hop toward the destination, and @{term pre} is the set of `precursor nodes'--those
41
+ interested in hearing about changes to the route.
42
+ \<close>
43
+
44
+ type_synonym r = "sqn \<times> k \<times> f \<times> nat \<times> ip \<times> ip set"
45
+
46
+ definition proj2 :: "r \<Rightarrow> sqn" ("\<pi>\<^sub>2")
47
+ where "\<pi>\<^sub>2 \<equiv> \<lambda>(dsn, _, _, _, _, _). dsn"
48
+
49
+ definition proj3 :: "r \<Rightarrow> k" ("\<pi>\<^sub>3")
50
+ where "\<pi>\<^sub>3 \<equiv> \<lambda>(_, dsk, _, _, _, _). dsk"
51
+
52
+ definition proj4 :: "r \<Rightarrow> f" ("\<pi>\<^sub>4")
53
+ where "\<pi>\<^sub>4 \<equiv> \<lambda>(_, _, flag, _, _, _). flag"
54
+
55
+ definition proj5 :: "r \<Rightarrow> nat" ("\<pi>\<^sub>5")
56
+ where "\<pi>\<^sub>5 \<equiv> \<lambda>(_, _, _, hops, _, _). hops"
57
+
58
+ definition proj6 :: "r \<Rightarrow> ip" ("\<pi>\<^sub>6")
59
+ where "\<pi>\<^sub>6 \<equiv> \<lambda>(_, _, _, _, nhip, _). nhip"
60
+
61
+ definition proj7 :: "r \<Rightarrow> ip set" ("\<pi>\<^sub>7")
62
+ where "\<pi>\<^sub>7 \<equiv> \<lambda>(_, _, _, _, _, pre). pre"
63
+
64
+ lemma projs [simp]:
65
+ "\<pi>\<^sub>2(dsn, dsk, flag, hops, nhip, pre) = dsn"
66
+ "\<pi>\<^sub>3(dsn, dsk, flag, hops, nhip, pre) = dsk"
67
+ "\<pi>\<^sub>4(dsn, dsk, flag, hops, nhip, pre) = flag"
68
+ "\<pi>\<^sub>5(dsn, dsk, flag, hops, nhip, pre) = hops"
69
+ "\<pi>\<^sub>6(dsn, dsk, flag, hops, nhip, pre) = nhip"
70
+ "\<pi>\<^sub>7(dsn, dsk, flag, hops, nhip, pre) = pre"
71
+ by (clarsimp simp: proj2_def proj3_def proj4_def
72
+ proj5_def proj6_def proj7_def)+
73
+
74
+ lemma proj3_pred [intro]: "\<lbrakk> P kno; P unk \<rbrakk> \<Longrightarrow> P (\<pi>\<^sub>3 x)"
75
+ by (rule k.induct)
76
+
77
+ lemma proj4_pred [intro]: "\<lbrakk> P val; P inv \<rbrakk> \<Longrightarrow> P (\<pi>\<^sub>4 x)"
78
+ by (rule f.induct)
79
+
80
+ lemma proj6_pair_snd [simp]:
81
+ fixes dsn' r
82
+ shows "\<pi>\<^sub>6 (dsn', snd (r)) = \<pi>\<^sub>6(r)"
83
+ by (cases r) simp
84
+
85
+ subsection "Routing Tables"
86
+
87
+ text \<open>Routing tables map ip addresses to route entries.\<close>
88
+
89
+ type_synonym rt = "ip \<rightharpoonup> r"
90
+
91
+ syntax
92
+ "_Sigma_route" :: "rt \<Rightarrow> ip \<rightharpoonup> r" ("\<sigma>\<^bsub>route\<^esub>'(_, _')")
93
+
94
+ translations
95
+ "\<sigma>\<^bsub>route\<^esub>(rt, dip)" => "rt dip"
96
+
97
+ definition sqn :: "rt \<Rightarrow> ip \<Rightarrow> sqn"
98
+ where "sqn rt dip \<equiv> case \<sigma>\<^bsub>route\<^esub>(rt, dip) of Some r \<Rightarrow> \<pi>\<^sub>2(r) | None \<Rightarrow> 0"
99
+
100
+ definition sqnf :: "rt \<Rightarrow> ip \<Rightarrow> k"
101
+ where "sqnf rt dip \<equiv> case \<sigma>\<^bsub>route\<^esub>(rt, dip) of Some r \<Rightarrow> \<pi>\<^sub>3(r) | None \<Rightarrow> unk"
102
+
103
+ abbreviation flag :: "rt \<Rightarrow> ip \<rightharpoonup> f"
104
+ where "flag rt dip \<equiv> map_option \<pi>\<^sub>4 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
105
+
106
+ abbreviation dhops :: "rt \<Rightarrow> ip \<rightharpoonup> nat"
107
+ where "dhops rt dip \<equiv> map_option \<pi>\<^sub>5 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
108
+
109
+ abbreviation nhop :: "rt \<Rightarrow> ip \<rightharpoonup> ip"
110
+ where "nhop rt dip \<equiv> map_option \<pi>\<^sub>6 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
111
+
112
+ abbreviation precs :: "rt \<Rightarrow> ip \<rightharpoonup> ip set"
113
+ where "precs rt dip \<equiv> map_option \<pi>\<^sub>7 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
114
+
115
+ definition vD :: "rt \<Rightarrow> ip set"
116
+ where "vD rt \<equiv> {dip. flag rt dip = Some val}"
117
+
118
+ definition iD :: "rt \<Rightarrow> ip set"
119
+ where "iD rt \<equiv> {dip. flag rt dip = Some inv}"
120
+
121
+ definition kD :: "rt \<Rightarrow> ip set"
122
+ where "kD rt \<equiv> {dip. rt dip \<noteq> None}"
123
+
124
+ lemma kD_is_vD_and_iD: "kD rt = vD rt \<union> iD rt"
125
+ unfolding kD_def vD_def iD_def by auto
126
+
127
+ lemma vD_iD_gives_kD [simp]:
128
+ "\<And>ip rt. ip \<in> vD rt \<Longrightarrow> ip \<in> kD rt"
129
+ "\<And>ip rt. ip \<in> iD rt \<Longrightarrow> ip \<in> kD rt"
130
+ unfolding kD_is_vD_and_iD by simp_all
131
+
132
+ lemma kD_Some [dest]:
133
+ fixes dip rt
134
+ assumes "dip \<in> kD rt"
135
+ shows "\<exists>dsn dsk flag hops nhip pre.
136
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, flag, hops, nhip, pre)"
137
+ using assms unfolding kD_def by simp
138
+
139
+ lemma kD_None [dest]:
140
+ fixes dip rt
141
+ assumes "dip \<notin> kD rt"
142
+ shows "\<sigma>\<^bsub>route\<^esub>(rt, dip) = None"
143
+ using assms unfolding kD_def
144
+ by (metis (mono_tags) mem_Collect_eq)
145
+
146
+ lemma vD_Some [dest]:
147
+ fixes dip rt
148
+ assumes "dip \<in> vD rt"
149
+ shows "\<exists>dsn dsk hops nhip pre.
150
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, val, hops, nhip, pre)"
151
+ using assms unfolding vD_def by simp
152
+
153
+ lemma vD_empty [simp]: "vD Map.empty = {}"
154
+ unfolding vD_def by simp
155
+
156
+ lemma iD_Some [dest]:
157
+ fixes dip rt
158
+ assumes "dip \<in> iD rt"
159
+ shows "\<exists>dsn dsk hops nhip pre.
160
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, inv, hops, nhip, pre)"
161
+ using assms unfolding iD_def by simp
162
+
163
+ lemma val_is_vD [elim]:
164
+ fixes ip rt
165
+ assumes "ip\<in>kD(rt)"
166
+ and "the (flag rt ip) = val"
167
+ shows "ip\<in>vD(rt)"
168
+ using assms unfolding vD_def by auto
169
+
170
+ lemma inv_is_iD [elim]:
171
+ fixes ip rt
172
+ assumes "ip\<in>kD(rt)"
173
+ and "the (flag rt ip) = inv"
174
+ shows "ip\<in>iD(rt)"
175
+ using assms unfolding iD_def by auto
176
+
177
+ lemma iD_flag_is_inv [elim, simp]:
178
+ fixes ip rt
179
+ assumes "ip\<in>iD(rt)"
180
+ shows "the (flag rt ip) = inv"
181
+ proof -
182
+ from \<open>ip\<in>iD(rt)\<close> have "ip\<in>kD(rt)" by auto
183
+ with assms show ?thesis unfolding iD_def by auto
184
+ qed
185
+
186
+ lemma kD_but_not_vD_is_iD [elim]:
187
+ fixes ip rt
188
+ assumes "ip\<in>kD(rt)"
189
+ and "ip\<notin>vD(rt)"
190
+ shows "ip\<in>iD(rt)"
191
+ proof -
192
+ from \<open>ip\<in>kD(rt)\<close> obtain dsn dsk f hops nhop pre
193
+ where rtip: "rt ip = Some (dsn, dsk, f, hops, nhop, pre)"
194
+ by (metis kD_Some)
195
+ from \<open>ip\<notin>vD(rt)\<close> have "f \<noteq> val"
196
+ proof (rule contrapos_nn)
197
+ assume "f = val"
198
+ with rtip have "the (flag rt ip) = val" by simp
199
+ with \<open>ip\<in>kD(rt)\<close> show "ip\<in>vD(rt)" ..
200
+ qed
201
+ with rtip have "the (flag rt ip)= inv" by simp
202
+ with \<open>ip\<in>kD(rt)\<close> show "ip\<in>iD(rt)" ..
203
+ qed
204
+
205
+ lemma vD_or_iD [elim]:
206
+ fixes ip rt
207
+ assumes "ip\<in>kD(rt)"
208
+ and "ip\<in>vD(rt) \<Longrightarrow> P rt ip"
209
+ and "ip\<in>iD(rt) \<Longrightarrow> P rt ip"
210
+ shows "P rt ip"
211
+ proof -
212
+ from \<open>ip\<in>kD(rt)\<close> have "ip\<in>vD(rt) \<union> iD(rt)"
213
+ by (simp add: kD_is_vD_and_iD)
214
+ thus ?thesis by (auto elim: assms(2-3))
215
+ qed
216
+
217
+ lemma proj5_eq_dhops: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>5(the (rt dip)) = the (dhops rt dip)"
218
+ unfolding sqn_def by (drule kD_Some) clarsimp
219
+
220
+ lemma proj4_eq_flag: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>4(the (rt dip)) = the (flag rt dip)"
221
+ unfolding sqn_def by (drule kD_Some) clarsimp
222
+
223
+ lemma proj2_eq_sqn: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>2(the (rt dip)) = sqn rt dip"
224
+ unfolding sqn_def by (drule kD_Some) clarsimp
225
+
226
+ lemma kD_sqnf_is_proj3 [simp]:
227
+ "\<And>ip rt. ip\<in>kD(rt) \<Longrightarrow> sqnf rt ip = \<pi>\<^sub>3(the (rt ip))"
228
+ unfolding sqnf_def by auto
229
+
230
+ lemma vD_flag_val [simp]:
231
+ "\<And>dip rt. dip \<in> vD (rt) \<Longrightarrow> the (flag rt dip) = val"
232
+ unfolding vD_def by clarsimp
233
+
234
+ lemma kD_update [simp]:
235
+ "\<And>rt nip v. kD (rt(nip \<mapsto> v)) = insert nip (kD rt)"
236
+ unfolding kD_def by auto
237
+
238
+ lemma kD_empty [simp]: "kD Map.empty = {}"
239
+ unfolding kD_def by simp
240
+
241
+ lemma ip_equal_or_known [elim]:
242
+ fixes rt ip ip'
243
+ assumes "ip = ip' \<or> ip\<in>kD(rt)"
244
+ and "ip = ip' \<Longrightarrow> P rt ip ip'"
245
+ and "\<lbrakk> ip \<noteq> ip'; ip\<in>kD(rt)\<rbrakk> \<Longrightarrow> P rt ip ip'"
246
+ shows "P rt ip ip'"
247
+ using assms by auto
248
+
249
+ subsection "Updating Routing Tables"
250
+
251
+ text \<open>Routing table entries are modified through explicit functions.
252
+ The properties of these functions are important in invariant proofs.\<close>
253
+
254
+ subsubsection "Updating Precursor Lists"
255
+
256
+ definition addpre :: "r \<Rightarrow> ip set \<Rightarrow> r"
257
+ where "addpre r npre \<equiv> let (dsn, dsk, flag, hops, nhip, pre) = r in
258
+ (dsn, dsk, flag, hops, nhip, pre \<union> npre)"
259
+
260
+ lemma proj2_addpre:
261
+ fixes v pre
262
+ shows "\<pi>\<^sub>2(addpre v pre) = \<pi>\<^sub>2(v)"
263
+ unfolding addpre_def by (cases v) simp
264
+
265
+ lemma proj3_addpre:
266
+ fixes v pre
267
+ shows "\<pi>\<^sub>3(addpre v pre) = \<pi>\<^sub>3(v)"
268
+ unfolding addpre_def by (cases v) simp
269
+
270
+ lemma proj4_addpre:
271
+ fixes v pre
272
+ shows "\<pi>\<^sub>4(addpre v pre) = \<pi>\<^sub>4(v)"
273
+ unfolding addpre_def by (cases v) simp
274
+
275
+ lemma proj5_addpre:
276
+ fixes v pre
277
+ shows "\<pi>\<^sub>5(addpre v pre) = \<pi>\<^sub>5(v)"
278
+ unfolding addpre_def by (cases v) simp
279
+
280
+ lemma proj6_addpre:
281
+ fixes dsn dsk flag hops nhip pre npre
282
+ shows "\<pi>\<^sub>6(addpre v npre) = \<pi>\<^sub>6(v)"
283
+ unfolding addpre_def by (cases v) simp
284
+
285
+ lemma proj7_addpre:
286
+ fixes dsn dsk flag hops nhip pre npre
287
+ shows "\<pi>\<^sub>7(addpre v npre) = \<pi>\<^sub>7(v) \<union> npre"
288
+ unfolding addpre_def by (cases v) simp
289
+
290
+ lemma addpre_empty: "addpre r {} = r"
291
+ unfolding addpre_def by simp
292
+
293
+ lemma addpre_r:
294
+ "addpre (dsn, dsk, fl, hops, nhip, pre) npre = (dsn, dsk, fl, hops, nhip, pre \<union> npre)"
295
+ unfolding addpre_def by simp
296
+
297
+ lemmas addpre_simps [simp] = proj2_addpre proj3_addpre proj4_addpre proj5_addpre
298
+ proj6_addpre proj7_addpre addpre_empty addpre_r
299
+
300
+ definition addpreRT :: "rt \<Rightarrow> ip \<Rightarrow> ip set \<rightharpoonup> rt"
301
+ where "addpreRT rt dip npre \<equiv>
302
+ map_option (\<lambda>s. rt (dip \<mapsto> addpre s npre)) (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
303
+
304
+ lemma snd_addpre [simp]:
305
+ "\<And>dsn dsn' v pre. (dsn, snd(addpre (dsn', v) pre)) = addpre (dsn, v) pre"
306
+ unfolding addpre_def by clarsimp
307
+
308
+ lemma proj2_addpreRT [simp]:
309
+ fixes ip rt ip' npre
310
+ assumes "ip\<in>kD rt"
311
+ and "ip'\<in>kD rt"
312
+ shows "\<pi>\<^sub>2(the (the (addpreRT rt ip' npre) ip)) = \<pi>\<^sub>2(the (rt ip))"
313
+ using assms [THEN kD_Some] unfolding addpreRT_def by clarsimp
314
+
315
+ lemma proj3_addpreRT [simp]:
316
+ fixes ip rt ip' npre
317
+ assumes "ip\<in>kD rt"
318
+ and "ip'\<in>kD rt"
319
+ shows "\<pi>\<^sub>3(the (the (addpreRT rt ip' npre) ip)) = \<pi>\<^sub>3(the (rt ip))"
320
+ using assms [THEN kD_Some] unfolding addpreRT_def by clarsimp
321
+
322
+ lemma proj5_addpreRT [simp]:
323
+ "\<And>rt dip ip npre. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>5(the (the (addpreRT rt dip npre) ip)) = \<pi>\<^sub>5(the (rt ip))"
324
+ unfolding addpreRT_def by auto
325
+
326
+ lemma flag_addpreRT [simp]:
327
+ fixes rt pre ip dip
328
+ assumes "dip \<in> kD rt"
329
+ shows "flag (the (addpreRT rt dip pre)) ip = flag rt ip"
330
+ unfolding addpreRT_def
331
+ using assms [THEN kD_Some] by (clarsimp)
332
+
333
+ lemma kD_addpreRT [simp]:
334
+ fixes rt dip npre
335
+ assumes "dip \<in> kD rt"
336
+ shows "kD (the (addpreRT rt dip npre)) = kD rt"
337
+ unfolding kD_def addpreRT_def
338
+ using assms [THEN kD_Some]
339
+ by clarsimp blast
340
+
341
+ lemma vD_addpreRT [simp]:
342
+ fixes rt dip npre
343
+ assumes "dip \<in> kD rt"
344
+ shows "vD (the (addpreRT rt dip npre)) = vD rt"
345
+ unfolding vD_def addpreRT_def
346
+ using assms [THEN kD_Some] by clarsimp auto
347
+
348
+ lemma iD_addpreRT [simp]:
349
+ fixes rt dip npre
350
+ assumes "dip \<in> kD rt"
351
+ shows "iD (the (addpreRT rt dip npre)) = iD rt"
352
+ unfolding iD_def addpreRT_def
353
+ using assms [THEN kD_Some] by clarsimp auto
354
+
355
+ lemma nhop_addpreRT [simp]:
356
+ fixes rt pre ip dip
357
+ assumes "dip \<in> kD rt"
358
+ shows "nhop (the (addpreRT rt dip pre)) ip = nhop rt ip"
359
+ unfolding sqn_def addpreRT_def
360
+ using assms [THEN kD_Some] by (clarsimp)
361
+
362
+ lemma sqn_addpreRT [simp]:
363
+ fixes rt pre ip dip
364
+ assumes "dip \<in> kD rt"
365
+ shows "sqn (the (addpreRT rt dip pre)) ip = sqn rt ip"
366
+ unfolding sqn_def addpreRT_def
367
+ using assms [THEN kD_Some] by (clarsimp)
368
+
369
+ lemma dhops_addpreRT [simp]:
370
+ fixes rt pre ip dip
371
+ assumes "dip \<in> kD rt"
372
+ shows "dhops (the (addpreRT rt dip pre)) ip = dhops rt ip"
373
+ unfolding addpreRT_def
374
+ using assms [THEN kD_Some] by (clarsimp)
375
+
376
+ lemma sqnf_addpreRT [simp]:
377
+ "\<And>ip dip. ip\<in>kD(rt \<xi>) \<Longrightarrow> sqnf (the (addpreRT (rt \<xi>) ip npre)) dip = sqnf (rt \<xi>) dip"
378
+ unfolding sqnf_def addpreRT_def by auto
379
+
380
+ subsubsection "Updating route entries"
381
+
382
+ lemma in_kD_case [simp]:
383
+ fixes dip rt
384
+ assumes "dip \<in> kD(rt)"
385
+ shows "(case rt dip of None \<Rightarrow> en | Some r \<Rightarrow> es r) = es (the (rt dip))"
386
+ using assms [THEN kD_Some] by auto
387
+
388
+ lemma not_in_kD_case [simp]:
389
+ fixes dip rt
390
+ assumes "dip \<notin> kD(rt)"
391
+ shows "(case rt dip of None \<Rightarrow> en | Some r \<Rightarrow> es r) = en"
392
+ using assms [THEN kD_None] by auto
393
+
394
+ lemma rt_Some_sqn [dest]:
395
+ fixes rt and ip dsn dsk flag hops nhip pre
396
+ assumes "rt ip = Some (dsn, dsk, flag, hops, nhip, pre)"
397
+ shows "sqn rt ip = dsn"
398
+ unfolding sqn_def using assms by simp
399
+
400
+ lemma not_kD_sqn [simp]:
401
+ fixes dip rt
402
+ assumes "dip \<notin> kD(rt)"
403
+ shows "sqn rt dip = 0"
404
+ using assms unfolding sqn_def
405
+ by simp
406
+
407
+ definition update_arg_wf :: "r \<Rightarrow> bool"
408
+ where "update_arg_wf r \<equiv> \<pi>\<^sub>4(r) = val \<and>
409
+ (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk) \<and>
410
+ (\<pi>\<^sub>3(r) = unk \<longrightarrow> \<pi>\<^sub>5(r) = 1)"
411
+
412
+ lemma update_arg_wf_gives_cases:
413
+ "\<And>r. update_arg_wf r \<Longrightarrow> (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
414
+ unfolding update_arg_wf_def by simp
415
+
416
+ lemma update_arg_wf_tuples [simp]:
417
+ "\<And>nhip pre. update_arg_wf (0, unk, val, Suc 0, nhip, pre)"
418
+ "\<And>n hops nhip pre. update_arg_wf (Suc n, kno, val, hops, nhip, pre)"
419
+ unfolding update_arg_wf_def by auto
420
+
421
+ lemma update_arg_wf_tuples' [elim]:
422
+ "\<And>n hops nhip pre. Suc 0 \<le> n \<Longrightarrow> update_arg_wf (n, kno, val, hops, nhip, pre)"
423
+ unfolding update_arg_wf_def by auto
424
+
425
+ lemma wf_r_cases [intro]:
426
+ fixes P r
427
+ assumes "update_arg_wf r"
428
+ and c1: "\<And>nhip pre. P (0, unk, val, Suc 0, nhip, pre)"
429
+ and c2: "\<And>dsn hops nhip pre. dsn > 0 \<Longrightarrow> P (dsn, kno, val, hops, nhip, pre)"
430
+ shows "P r"
431
+ proof -
432
+ obtain dsn dsk flag hops nhip pre
433
+ where *: "r = (dsn, dsk, flag, hops, nhip, pre)" by (cases r)
434
+ with \<open>update_arg_wf r\<close> have wf1: "flag = val"
435
+ and wf2: "(dsn = 0) = (dsk = unk)"
436
+ and wf3: "dsk = unk \<longrightarrow> (hops = 1)"
437
+ unfolding update_arg_wf_def by auto
438
+ have "P (dsn, dsk, flag, hops, nhip, pre)"
439
+ proof (cases dsk)
440
+ assume "dsk = unk"
441
+ moreover with wf2 wf3 have "dsn = 0" and "hops = Suc 0" by auto
442
+ ultimately show ?thesis using \<open>flag = val\<close> by simp (rule c1)
443
+ next
444
+ assume "dsk = kno"
445
+ moreover with wf2 have "dsn > 0" by simp
446
+ ultimately show ?thesis using \<open>flag = val\<close> by simp (rule c2)
447
+ qed
448
+ with * show "P r" by simp
449
+ qed
450
+
451
+ definition update :: "rt \<Rightarrow> ip \<Rightarrow> r \<Rightarrow> rt"
452
+ where
453
+ "update rt ip r \<equiv>
454
+ case \<sigma>\<^bsub>route\<^esub>(rt, ip) of
455
+ None \<Rightarrow> rt (ip \<mapsto> r)
456
+ | Some s \<Rightarrow>
457
+ if \<pi>\<^sub>2(s) < \<pi>\<^sub>2(r) then rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(s)))
458
+ else if \<pi>\<^sub>2(s) = \<pi>\<^sub>2(r) \<and> (\<pi>\<^sub>5(s) > \<pi>\<^sub>5(r) \<or> \<pi>\<^sub>4(s) = inv)
459
+ then rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(s)))
460
+ else if \<pi>\<^sub>3(r) = unk
461
+ then rt (ip \<mapsto> (\<pi>\<^sub>2(s), snd (addpre r (\<pi>\<^sub>7(s)))))
462
+ else rt (ip \<mapsto> addpre s (\<pi>\<^sub>7(r)))"
463
+
464
+ lemma update_simps [simp]:
465
+ fixes r s nrt nr nr' ns rt ip
466
+ defines "s \<equiv> the \<sigma>\<^bsub>route\<^esub>(rt, ip)"
467
+ and "nr \<equiv> addpre r (\<pi>\<^sub>7(s))"
468
+ and "nr' \<equiv> (\<pi>\<^sub>2(s), \<pi>\<^sub>3(nr), \<pi>\<^sub>4(nr), \<pi>\<^sub>5(nr), \<pi>\<^sub>6(nr), \<pi>\<^sub>7(nr))"
469
+ and "ns \<equiv> addpre s (\<pi>\<^sub>7(r))"
470
+ shows
471
+ "\<lbrakk>ip \<notin> kD(rt)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> r)"
472
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip < \<pi>\<^sub>2(r)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
473
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r);
474
+ the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
475
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r);
476
+ flag rt ip = Some inv\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
477
+ "\<lbrakk>ip \<in> kD(rt); \<pi>\<^sub>3(r) = unk; (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr')"
478
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
479
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val \<rbrakk>
480
+ \<Longrightarrow> update rt ip r = rt (ip \<mapsto> ns)"
481
+ proof -
482
+ assume "ip\<notin>kD(rt)"
483
+ hence "\<sigma>\<^bsub>route\<^esub>(rt, ip) = None" ..
484
+ thus "update rt ip r = rt (ip \<mapsto> r)"
485
+ unfolding update_def by simp
486
+ next
487
+ assume "ip \<in> kD(rt)"
488
+ and "sqn rt ip < \<pi>\<^sub>2(r)"
489
+ from this(1) obtain dsn dsk fl hops nhip pre
490
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
491
+ by (metis kD_Some)
492
+ with \<open>sqn rt ip < \<pi>\<^sub>2(r)\<close> show "update rt ip r = rt (ip \<mapsto> nr)"
493
+ unfolding update_def nr_def s_def by auto
494
+ next
495
+ assume "ip \<in> kD(rt)"
496
+ and "sqn rt ip = \<pi>\<^sub>2(r)"
497
+ and "the (dhops rt ip) > \<pi>\<^sub>5(r)"
498
+ from this(1) obtain dsn dsk fl hops nhip pre
499
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
500
+ by (metis kD_Some)
501
+ with \<open>sqn rt ip = \<pi>\<^sub>2(r)\<close> and \<open>the (dhops rt ip) > \<pi>\<^sub>5(r)\<close>
502
+ show "update rt ip r = rt (ip \<mapsto> nr)"
503
+ unfolding update_def nr_def s_def by auto
504
+ next
505
+ assume "ip \<in> kD(rt)"
506
+ and "sqn rt ip = \<pi>\<^sub>2(r)"
507
+ and "flag rt ip = Some inv"
508
+ from this(1) obtain dsn dsk fl hops nhip pre
509
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
510
+ by (metis kD_Some)
511
+ with \<open>sqn rt ip = \<pi>\<^sub>2(r)\<close> and \<open>flag rt ip = Some inv\<close>
512
+ show "update rt ip r = rt (ip \<mapsto> nr)"
513
+ unfolding update_def nr_def s_def by auto
514
+ next
515
+ assume "ip \<in> kD(rt)"
516
+ and "\<pi>\<^sub>3(r) = unk"
517
+ and "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
518
+ from this(1) obtain dsn dsk fl hops nhip pre
519
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
520
+ by (metis kD_Some)
521
+ with \<open>(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<close> and \<open>\<pi>\<^sub>3(r) = unk\<close>
522
+ show "update rt ip r = rt (ip \<mapsto> nr')"
523
+ unfolding update_def nr'_def nr_def s_def
524
+ by (cases r) simp
525
+ next
526
+ assume "ip \<in> kD(rt)"
527
+ and otherassms: "sqn rt ip \<ge> \<pi>\<^sub>2(r)"
528
+ "\<pi>\<^sub>3(r) = kno"
529
+ "sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val"
530
+ from this(1) obtain dsn dsk fl hops nhip pre
531
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
532
+ by (metis kD_Some)
533
+ with otherassms show "update rt ip r = rt (ip \<mapsto> ns)"
534
+ unfolding update_def ns_def s_def by auto
535
+ qed
536
+
537
+ lemma update_cases [elim]:
538
+ assumes "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
539
+ and c1: "\<lbrakk>ip \<notin> kD(rt)\<rbrakk> \<Longrightarrow> P (rt (ip \<mapsto> r))"
540
+
541
+ and c2: "\<lbrakk>ip \<in> kD(rt); sqn rt ip < \<pi>\<^sub>2(r)\<rbrakk>
542
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
543
+ and c3: "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r); the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk>
544
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
545
+ and c4: "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r); the (flag rt ip) = inv\<rbrakk>
546
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
547
+ and c5: "\<lbrakk>ip \<in> kD(rt); \<pi>\<^sub>3(r) = unk\<rbrakk>
548
+ \<Longrightarrow> P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r),
549
+ \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r), \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))))"
550
+ and c6: "\<lbrakk>ip \<in> kD(rt); sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
551
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val\<rbrakk>
552
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre (the \<sigma>\<^bsub>route\<^esub>(rt, ip)) (\<pi>\<^sub>7(r))))"
553
+ shows "(P (update rt ip r))"
554
+ proof (cases "ip \<in> kD(rt)")
555
+ assume "ip \<notin> kD(rt)"
556
+ with c1 show ?thesis
557
+ by simp
558
+ next
559
+ assume "ip \<in> kD(rt)"
560
+ moreover then obtain dsn dsk fl hops nhip pre
561
+ where rteq: "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
562
+ by (metis kD_Some)
563
+ moreover obtain dsn' dsk' fl' hops' nhip' pre'
564
+ where req: "r = (dsn', dsk', fl', hops', nhip', pre')"
565
+ by (cases r) metis
566
+ ultimately show ?thesis
567
+ using \<open>(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<close>
568
+ c2 [OF \<open>ip\<in>kD(rt)\<close>]
569
+ c3 [OF \<open>ip\<in>kD(rt)\<close>]
570
+ c4 [OF \<open>ip\<in>kD(rt)\<close>]
571
+ c5 [OF \<open>ip\<in>kD(rt)\<close>]
572
+ c6 [OF \<open>ip\<in>kD(rt)\<close>]
573
+ unfolding update_def sqn_def by auto
574
+ qed
575
+
576
+ lemma update_cases_kD:
577
+ assumes "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
578
+ and "ip \<in> kD(rt)"
579
+ and c2: "sqn rt ip < \<pi>\<^sub>2(r) \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
580
+ and c3: "\<lbrakk>sqn rt ip = \<pi>\<^sub>2(r); the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk>
581
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
582
+ and c4: "\<lbrakk>sqn rt ip = \<pi>\<^sub>2(r); the (flag rt ip) = inv\<rbrakk>
583
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
584
+ and c5: "\<pi>\<^sub>3(r) = unk \<Longrightarrow> P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r),
585
+ \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r),
586
+ \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))))"
587
+ and c6: "\<lbrakk>sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
588
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val\<rbrakk>
589
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre (the \<sigma>\<^bsub>route\<^esub>(rt, ip)) (\<pi>\<^sub>7(r))))"
590
+ shows "(P (update rt ip r))"
591
+ using assms(1) proof (rule update_cases)
592
+ assume "sqn rt ip < \<pi>\<^sub>2(r)"
593
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7(the (rt ip)))))" by (rule c2)
594
+ next
595
+ assume "sqn rt ip = \<pi>\<^sub>2(r)"
596
+ and "the (dhops rt ip) > \<pi>\<^sub>5(r)"
597
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7 (the (rt ip)))))"
598
+ by (rule c3)
599
+ next
600
+ assume "sqn rt ip = \<pi>\<^sub>2(r)"
601
+ and "the (flag rt ip) = inv"
602
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7 (the (rt ip)))))"
603
+ by (rule c4)
604
+ next
605
+ assume "\<pi>\<^sub>3(r) = unk"
606
+ thus "P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r), \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r),
607
+ \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the (rt ip)))))))"
608
+ by (rule c5)
609
+ next
610
+ assume "sqn rt ip \<ge> \<pi>\<^sub>2(r)"
611
+ and "\<pi>\<^sub>3(r) = kno"
612
+ and "sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val"
613
+ thus "P (rt (ip \<mapsto> addpre (the (rt ip)) (\<pi>\<^sub>7(r))))"
614
+ by (rule c6)
615
+ qed (simp add: \<open>ip \<in> kD(rt)\<close>)
616
+
617
+ lemma in_kD_after_update [simp]:
618
+ fixes rt nip dsn dsk flag hops nhip pre
619
+ shows "kD (update rt nip (dsn, dsk, flag, hops, nhip, pre)) = insert nip (kD rt)"
620
+ unfolding update_def
621
+ by (cases "rt nip") auto
622
+
623
+ lemma nhop_of_update [simp]:
624
+ fixes rt dip dsn dsk flag hops nhip
625
+ assumes "rt \<noteq> update rt dip (dsn, dsk, flag, hops, nhip, {})"
626
+ shows "the (nhop (update rt dip (dsn, dsk, flag, hops, nhip, {})) dip) = nhip"
627
+ proof -
628
+ from assms
629
+ have update_neq: "\<And>v. rt dip = Some v \<Longrightarrow>
630
+ update rt dip (dsn, dsk, flag, hops, nhip, {})
631
+ \<noteq> rt(dip \<mapsto> addpre (the (rt dip)) (\<pi>\<^sub>7 (dsn, dsk, flag, hops, nhip, {})))"
632
+ by auto
633
+ show ?thesis
634
+ proof (cases "rt dip = None")
635
+ assume "rt dip = None"
636
+ thus "?thesis" unfolding update_def by clarsimp
637
+ next
638
+ assume "rt dip \<noteq> None"
639
+ then obtain v where "rt dip = Some v" by (metis not_None_eq)
640
+ with update_neq [OF this] show ?thesis
641
+ unfolding update_def by auto
642
+ qed
643
+ qed
644
+
645
+ lemma sqn_if_updated:
646
+ fixes rip v rt ip
647
+ shows "sqn (\<lambda>x. if x = rip then Some v else rt x) ip
648
+ = (if ip = rip then \<pi>\<^sub>2(v) else sqn rt ip)"
649
+ unfolding sqn_def by simp
650
+
651
+ lemma update_sqn [simp]:
652
+ fixes rt dip rip dsn dsk hops nhip pre
653
+ assumes "(dsn = 0) = (dsk = unk)"
654
+ shows "sqn rt dip \<le> sqn (update rt rip (dsn, dsk, val, hops, nhip, pre)) dip"
655
+ proof (rule update_cases)
656
+ show "(\<pi>\<^sub>2 (dsn, dsk, val, hops, nhip, pre) = 0) = (\<pi>\<^sub>3 (dsn, dsk, val, hops, nhip, pre) = unk)"
657
+ by simp (rule assms)
658
+ qed (clarsimp simp: sqn_if_updated sqn_def)+
659
+
660
+ lemma sqn_update_bigger [simp]:
661
+ fixes rt ip ip' dsn dsk flag hops nhip pre
662
+ assumes "1 \<le> hops"
663
+ shows "sqn rt ip \<le> sqn (update rt ip' (dsn, dsk, flag, hops, nhip, pre)) ip"
664
+ using assms unfolding update_def sqn_def
665
+ by (clarsimp split: option.split) auto
666
+
667
+ lemma dhops_update [intro]:
668
+ fixes rt dsn dsk flag hops ip rip nhip pre
669
+ assumes ex: "\<forall>ip\<in>kD rt. the (dhops rt ip) \<ge> 1"
670
+ and ip: "(ip = rip \<and> Suc 0 \<le> hops) \<or> (ip \<noteq> rip \<and> ip\<in>kD rt)"
671
+ shows "Suc 0 \<le> the (dhops (update rt rip (dsn, dsk, flag, hops, nhip, pre)) ip)"
672
+ using ip proof
673
+ assume "ip = rip \<and> Suc 0 \<le> hops" thus ?thesis
674
+ unfolding update_def using ex
675
+ by (cases "rip \<in> kD rt") (drule(1) bspec, auto)
676
+ next
677
+ assume "ip \<noteq> rip \<and> ip\<in>kD rt" thus ?thesis
678
+ using ex unfolding update_def
679
+ by (cases "rip\<in>kD rt") auto
680
+ qed
681
+
682
+ lemma update_another [simp]:
683
+ fixes dip ip rt dsn dsk flag hops nhip pre
684
+ assumes "ip \<noteq> dip"
685
+ shows "(update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = rt ip"
686
+ using assms unfolding update_def
687
+ by (clarsimp split: option.split)
688
+
689
+ lemma nhop_update_another [simp]:
690
+ fixes dip ip rt dsn dsk flag hops nhip pre
691
+ assumes "ip \<noteq> dip"
692
+ shows "nhop (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = nhop rt ip"
693
+ using assms unfolding update_def
694
+ by (clarsimp split: option.split)
695
+
696
+ lemma dhops_update_another [simp]:
697
+ fixes dip ip rt dsn dsk flag hops nhip pre
698
+ assumes "ip \<noteq> dip"
699
+ shows "dhops (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = dhops rt ip"
700
+ using assms unfolding update_def
701
+ by (clarsimp split: option.split)
702
+
703
+ lemma sqn_update_same [simp]:
704
+ "\<And>rt ip dsn dsk flag hops nhip pre. sqn (rt(ip \<mapsto> v)) ip = \<pi>\<^sub>2(v)"
705
+ unfolding sqn_def by simp
706
+
707
+ lemma dhops_update_changed [simp]:
708
+ fixes rt dip osn hops nhip
709
+ assumes "rt \<noteq> update rt dip (osn, kno, val, hops, nhip, {})"
710
+ shows "the (dhops (update rt dip (osn, kno, val, hops, nhip, {})) dip) = hops"
711
+ using assms unfolding update_def
712
+ by (clarsimp split: option.split_asm option.split if_split_asm) auto
713
+
714
+ lemma nhop_update_unk_val [simp]:
715
+ "\<And>rt dip ip dsn hops npre.
716
+ the (nhop (update rt dip (dsn, unk, val, hops, ip, npre)) dip) = ip"
717
+ unfolding update_def by (clarsimp split: option.split)
718
+
719
+ lemma nhop_update_changed [simp]:
720
+ fixes rt dip dsn dsk flg hops sip
721
+ assumes "update rt dip (dsn, dsk, flg, hops, sip, {}) \<noteq> rt"
722
+ shows "the (nhop (update rt dip (dsn, dsk, flg, hops, sip, {})) dip) = sip"
723
+ using assms unfolding update_def
724
+ by (clarsimp split: option.splits if_split_asm) auto
725
+
726
+ lemma update_rt_split_asm:
727
+ "\<And>rt ip dsn dsk flag hops sip.
728
+ P (update rt ip (dsn, dsk, flag, hops, sip, {}))
729
+ =
730
+ (\<not>(rt = update rt ip (dsn, dsk, flag, hops, sip, {}) \<and> \<not>P rt
731
+ \<or> rt \<noteq> update rt ip (dsn, dsk, flag, hops, sip, {})
732
+ \<and> \<not>P (update rt ip (dsn, dsk, flag, hops, sip, {}))))"
733
+ by auto
734
+
735
+ lemma sqn_update [simp]: "\<And>rt dip dsn flg hops sip.
736
+ rt \<noteq> update rt dip (dsn, kno, flg, hops, sip, {})
737
+ \<Longrightarrow> sqn (update rt dip (dsn, kno, flg, hops, sip, {})) dip = dsn"
738
+ unfolding update_def by (clarsimp split: option.split if_split_asm) auto
739
+
740
+ lemma sqnf_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
741
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
742
+ \<Longrightarrow> sqnf (update rt dip (dsn, dsk, flg, hops, sip, {})) dip = dsk"
743
+ unfolding update_def sqnf_def
744
+ by (clarsimp split: option.splits if_split_asm) auto
745
+
746
+ lemma update_kno_dsn_greater_zero:
747
+ "\<And>rt dip ip dsn hops npre. 1 \<le> dsn \<Longrightarrow> 1 \<le> (sqn (update rt dip (dsn, kno, val, hops, ip, npre)) dip)"
748
+ unfolding update_def
749
+ by (clarsimp split: option.splits)
750
+
751
+ lemma proj3_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
752
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
753
+ \<Longrightarrow> \<pi>\<^sub>3(the (update rt dip (dsn, dsk, flg, hops, sip, {}) dip)) = dsk"
754
+ unfolding update_def sqnf_def
755
+ by (clarsimp split: option.splits if_split_asm) auto
756
+
757
+ lemma nhop_update_changed_kno_val [simp]: "\<And>rt ip dsn dsk hops nhip.
758
+ rt \<noteq> update rt ip (dsn, kno, val, hops, nhip, {})
759
+ \<Longrightarrow> the (nhop (update rt ip (dsn, kno, val, hops, nhip, {})) ip) = nhip"
760
+ unfolding update_def
761
+ by (clarsimp split: option.split_asm option.split if_split_asm) auto
762
+
763
+ lemma flag_update [simp]: "\<And>rt dip dsn flg hops sip.
764
+ rt \<noteq> update rt dip (dsn, kno, flg, hops, sip, {})
765
+ \<Longrightarrow> the (flag (update rt dip (dsn, kno, flg, hops, sip, {})) dip) = flg"
766
+ unfolding update_def
767
+ by (clarsimp split: option.split if_split_asm) auto
768
+
769
+ lemma the_flag_Some [dest!]:
770
+ fixes ip rt
771
+ assumes "the (flag rt ip) = x"
772
+ and "ip \<in> kD rt"
773
+ shows "flag rt ip = Some x"
774
+ using assms by auto
775
+
776
+ lemma kD_update_unchanged [dest]:
777
+ fixes rt dip dsn dsk flag hops nhip pre
778
+ assumes "rt = update rt dip (dsn, dsk, flag, hops, nhip, pre)"
779
+ shows "dip\<in>kD(rt)"
780
+ proof -
781
+ have "dip\<in>kD(update rt dip (dsn, dsk, flag, hops, nhip, pre))" by simp
782
+ with assms show ?thesis by simp
783
+ qed
784
+
785
+ lemma nhop_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
786
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
787
+ \<Longrightarrow> the (nhop (update rt dip (dsn, dsk, flg, hops, sip, {})) dip) = sip"
788
+ unfolding update_def sqnf_def
789
+ by (clarsimp split: option.splits if_split_asm) auto
790
+
791
+ lemma sqn_update_another [simp]:
792
+ fixes dip ip rt dsn dsk flag hops nhip pre
793
+ assumes "ip \<noteq> dip"
794
+ shows "sqn (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = sqn rt ip"
795
+ using assms unfolding update_def sqn_def
796
+ by (clarsimp split: option.splits) auto
797
+
798
+ lemma sqnf_update_another [simp]:
799
+ fixes dip ip rt dsn dsk flag hops nhip pre
800
+ assumes "ip \<noteq> dip"
801
+ shows "sqnf (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = sqnf rt ip"
802
+ using assms unfolding update_def sqnf_def
803
+ by (clarsimp split: option.splits) auto
804
+
805
+ lemma vD_update_val [dest]:
806
+ "\<And>dip rt dip' dsn dsk hops nhip pre.
807
+ dip \<in> vD(update rt dip' (dsn, dsk, val, hops, nhip, pre)) \<Longrightarrow> (dip\<in>vD(rt) \<or> dip=dip')"
808
+ unfolding update_def vD_def by (clarsimp split: option.split_asm if_split_asm)
809
+
810
+ subsubsection "Invalidating route entries"
811
+
812
+ definition invalidate :: "rt \<Rightarrow> (ip \<rightharpoonup> sqn) \<Rightarrow> rt"
813
+ where "invalidate rt dests \<equiv>
814
+ \<lambda>ip. case (rt ip, dests ip) of
815
+ (None, _) \<Rightarrow> None
816
+ | (Some s, None) \<Rightarrow> Some s
817
+ | (Some (_, dsk, _, hops, nhip, pre), Some rsn) \<Rightarrow>
818
+ Some (rsn, dsk, inv, hops, nhip, pre)"
819
+
820
+ lemma proj3_invalidate [simp]:
821
+ "\<And>dip. \<pi>\<^sub>3(the ((invalidate rt dests) dip)) = \<pi>\<^sub>3(the (rt dip))"
822
+ unfolding invalidate_def by (clarsimp split: option.split)
823
+
824
+ lemma proj5_invalidate [simp]:
825
+ "\<And>dip. \<pi>\<^sub>5(the ((invalidate rt dests) dip)) = \<pi>\<^sub>5(the (rt dip))"
826
+ unfolding invalidate_def by (clarsimp split: option.split)
827
+
828
+ lemma proj6_invalidate [simp]:
829
+ "\<And>dip. \<pi>\<^sub>6(the ((invalidate rt dests) dip)) = \<pi>\<^sub>6(the (rt dip))"
830
+ unfolding invalidate_def by (clarsimp split: option.split)
831
+
832
+ lemma proj7_invalidate [simp]:
833
+ "\<And>dip. \<pi>\<^sub>7(the ((invalidate rt dests) dip)) = \<pi>\<^sub>7(the (rt dip))"
834
+ unfolding invalidate_def by (clarsimp split: option.split)
835
+
836
+ subsection "Route Requests"
837
+
838
+ lemma invalidate_kD_inv [simp]:
839
+ "\<And>rt dests. kD (invalidate rt dests) = kD rt"
840
+ unfolding invalidate_def kD_def
841
+ by (simp split: option.split)
842
+
843
+ lemma invalidate_sqn:
844
+ fixes rt dip dests
845
+ assumes "\<forall>rsn. dests dip = Some rsn \<longrightarrow> sqn rt dip \<le> rsn"
846
+ shows "sqn rt dip \<le> sqn (invalidate rt dests) dip"
847
+ proof (cases "dip \<notin> kD(rt)")
848
+ assume "\<not> dip \<notin> kD(rt)"
849
+ hence "dip\<in>kD(rt)" by simp
850
+ then obtain dsn dsk flag hops nhip pre where "rt dip = Some (dsn, dsk, flag, hops, nhip, pre)"
851
+ by (metis kD_Some)
852
+ with assms show "sqn rt dip \<le> sqn (invalidate rt dests) dip"
853
+ by (cases "dests dip") (auto simp add: invalidate_def sqn_def)
854
+ qed simp
855
+
856
+ lemma sqn_invalidate_in_dests [simp]:
857
+ fixes dests ipa rsn rt
858
+ assumes "dests ipa = Some rsn"
859
+ and "ipa\<in>kD(rt)"
860
+ shows "sqn (invalidate rt dests) ipa = rsn"
861
+ unfolding invalidate_def sqn_def
862
+ using assms(1) assms(2) [THEN kD_Some]
863
+ by clarsimp
864
+
865
+ lemma dhops_invalidate [simp]:
866
+ "\<And>dip. the (dhops (invalidate rt dests) dip) = the (dhops rt dip)"
867
+ unfolding invalidate_def by (clarsimp split: option.split)
868
+
869
+ lemma sqnf_invalidate [simp]:
870
+ "\<And>dip. sqnf (invalidate (rt \<xi>) (dests \<xi>)) dip = sqnf (rt \<xi>) dip"
871
+ unfolding sqnf_def invalidate_def by (clarsimp split: option.split)
872
+
873
+ lemma nhop_invalidate [simp]:
874
+ "\<And>dip. the (nhop (invalidate (rt \<xi>) (dests \<xi>)) dip) = the (nhop (rt \<xi>) dip)"
875
+ unfolding invalidate_def by (clarsimp split: option.split)
876
+
877
+ lemma invalidate_other [simp]:
878
+ fixes rt dests dip
879
+ assumes "dip\<notin>dom(dests)"
880
+ shows "invalidate rt dests dip = rt dip"
881
+ using assms unfolding invalidate_def
882
+ by (clarsimp split: option.split_asm)
883
+
884
+ lemma invalidate_none [simp]:
885
+ fixes rt dests dip
886
+ assumes "dip\<notin>kD(rt)"
887
+ shows "invalidate rt dests dip = None"
888
+ using assms unfolding invalidate_def by clarsimp
889
+
890
+ lemma vD_invalidate_vD_not_dests:
891
+ "\<And>dip rt dests. dip\<in>vD(invalidate rt dests) \<Longrightarrow> dip\<in>vD(rt) \<and> dests dip = None"
892
+ unfolding invalidate_def vD_def
893
+ by (clarsimp split: option.split_asm)
894
+
895
+ lemma sqn_invalidate_not_in_dests [simp]:
896
+ fixes dests dip rt
897
+ assumes "dip\<notin>dom(dests)"
898
+ shows "sqn (invalidate rt dests) dip = sqn rt dip"
899
+ using assms unfolding sqn_def by simp
900
+
901
+ lemma invalidate_changes:
902
+ fixes rt dests dip dsn dsk flag hops nhip pre
903
+ assumes "invalidate rt dests dip = Some (dsn, dsk, flag, hops, nhip, pre)"
904
+ shows " dsn = (case dests dip of None \<Rightarrow> \<pi>\<^sub>2(the (rt dip)) | Some rsn \<Rightarrow> rsn)
905
+ \<and> dsk = \<pi>\<^sub>3(the (rt dip))
906
+ \<and> flag = (if dests dip = None then \<pi>\<^sub>4(the (rt dip)) else inv)
907
+ \<and> hops = \<pi>\<^sub>5(the (rt dip))
908
+ \<and> nhip = \<pi>\<^sub>6(the (rt dip))
909
+ \<and> pre = \<pi>\<^sub>7(the (rt dip))"
910
+ using assms unfolding invalidate_def
911
+ by (cases "rt dip", clarsimp, cases "dests dip") auto
912
+
913
+
914
+ lemma proj3_inv: "\<And>dip rt dests. dip\<in>kD (rt)
915
+ \<Longrightarrow> \<pi>\<^sub>3(the (invalidate rt dests dip)) = \<pi>\<^sub>3(the (rt dip))"
916
+ by (clarsimp simp: invalidate_def kD_def split: option.split)
917
+
918
+ lemma dests_iD_invalidate [simp]:
919
+ assumes "dests ip = Some rsn"
920
+ and "ip\<in>kD(rt)"
921
+ shows "ip\<in>iD(invalidate rt dests)"
922
+ using assms(1) assms(2) [THEN kD_Some] unfolding invalidate_def iD_def
923
+ by (clarsimp split: option.split)
924
+
925
+ subsection "Queued Packets"
926
+
927
+ text \<open>Functions for sending data packets.\<close>
928
+
929
+ type_synonym store = "ip \<rightharpoonup> (p \<times> data list)"
930
+
931
+ definition sigma_queue :: "store \<Rightarrow> ip \<Rightarrow> data list" ("\<sigma>\<^bsub>queue\<^esub>'(_, _')")
932
+ where "\<sigma>\<^bsub>queue\<^esub>(store, dip) \<equiv> case store dip of None \<Rightarrow> [] | Some (p, q) \<Rightarrow> q"
933
+
934
+ definition qD :: "store \<Rightarrow> ip set"
935
+ where "qD \<equiv> dom"
936
+
937
+ definition add :: "data \<Rightarrow> ip \<Rightarrow> store \<Rightarrow> store"
938
+ where "add d dip store \<equiv> case store dip of
939
+ None \<Rightarrow> store (dip \<mapsto> (req, [d]))
940
+ | Some (p, q) \<Rightarrow> store (dip \<mapsto> (p, q @ [d]))"
941
+
942
+ lemma qD_add [simp]:
943
+ fixes d dip store
944
+ shows "qD(add d dip store) = insert dip (qD store)"
945
+ unfolding add_def Let_def qD_def
946
+ by (clarsimp split: option.split)
947
+
948
+ definition drop :: "ip \<Rightarrow> store \<rightharpoonup> store"
949
+ where "drop dip store \<equiv>
950
+ map_option (\<lambda>(p, q). if tl q = [] then store (dip := None)
951
+ else store (dip \<mapsto> (p, tl q))) (store dip)"
952
+
953
+ definition sigma_p_flag :: "store \<Rightarrow> ip \<rightharpoonup> p" ("\<sigma>\<^bsub>p-flag\<^esub>'(_, _')")
954
+ where "\<sigma>\<^bsub>p-flag\<^esub>(store, dip) \<equiv> map_option fst (store dip)"
955
+
956
+ definition unsetRRF :: "store \<Rightarrow> ip \<Rightarrow> store"
957
+ where "unsetRRF store dip \<equiv> case store dip of
958
+ None \<Rightarrow> store
959
+ | Some (p, q) \<Rightarrow> store (dip \<mapsto> (noreq, q))"
960
+
961
+ definition setRRF :: "store \<Rightarrow> (ip \<rightharpoonup> sqn) \<Rightarrow> store"
962
+ where "setRRF store dests \<equiv> \<lambda>dip. if dests dip = None then store dip
963
+ else map_option (\<lambda>(_, q). (req, q)) (store dip)"
964
+
965
+ subsection "Comparison with the original technical report"
966
+
967
+ text \<open>
968
+ The major differences with the AODV technical report of Fehnker et al are:
969
+ \begin{enumerate}
970
+ \item @{term nhop} is partial, thus a `@{term the}' is needed, similarly for @{term dhops}
971
+ and @{term addpreRT}.
972
+ \item @{term precs} is partial.
973
+ \item @{term "\<sigma>\<^bsub>p-flag\<^esub>(store, dip)"} is partial.
974
+ \item The routing table (@{typ rt}) is modelled as a map (@{typ "ip \<Rightarrow> r option"})
975
+ rather than a set of 7-tuples, likewise, the @{typ r} is a 6-tuple rather than
976
+ a 7-tuple, i.e., the destination ip-address (@{term "dip"}) is taken from the
977
+ argument to the function, rather than a part of the result. Well-definedness then
978
+ follows from the structure of the type and more related facts are available
979
+ automatically, rather than having to be acquired through tedious proofs.
980
+ \item Similar remarks hold for the dests mapping passed to @{term "invalidate"},
981
+ and @{term "store"}.
982
+ \end{enumerate}
983
+ \<close>
984
+
985
+ end
986
+
formal/afp/AODV/variants/a_norreqid/A_Aodv_Loop_Freedom.thy ADDED
@@ -0,0 +1,369 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Aodv_Loop_Freedom.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Lift and transfer invariants to show loop freedom"
7
+
8
+ theory A_Aodv_Loop_Freedom
9
+ imports AWN.OClosed_Transfer AWN.Qmsg_Lifting A_Global_Invariants A_Loop_Freedom
10
+ begin
11
+
12
+ text \<open>lift to parallel processes with queues\<close>
13
+
14
+ lemma par_step_no_change_on_send_or_receive:
15
+ fixes \<sigma> s a \<sigma>' s'
16
+ assumes "((\<sigma>, s), a, (\<sigma>', s')) \<in> oparp_sos i (oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i) (seqp_sos \<Gamma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G)"
17
+ and "a \<noteq> \<tau>"
18
+ shows "\<sigma>' i = \<sigma> i"
19
+ using assms by (rule qmsg_no_change_on_send_or_receive)
20
+
21
+ lemma par_nhop_quality_increases:
22
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile> (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m.
23
+ msg_fresh \<sigma> m \<and> msg_zhops m)),
24
+ other quality_increases {i} \<rightarrow>)
25
+ global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
26
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
27
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
28
+ proof (rule lift_into_qmsg [OF seq_nhop_quality_increases])
29
+ show "opaodv i \<Turnstile>\<^sub>A (otherwith ((=)) {i}
30
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
31
+ other quality_increases {i} \<rightarrow>)
32
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
33
+ proof (rule ostep_invariant_weakenE [OF oquality_increases], simp_all)
34
+ fix t :: "(((nat \<Rightarrow> state) \<times> (state, msg, pseqp, pseqp label) seqp), msg seq_action) transition"
35
+ assume "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), _, (\<sigma>', _)). \<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)) t"
36
+ thus "quality_increases (fst (fst t) i) (fst (snd (snd t)) i)"
37
+ by (cases t) (clarsimp dest!: onllD, metis aodv_ex_label)
38
+ next
39
+ fix \<sigma> \<sigma>' a
40
+ assume "otherwith ((=)) {i}
41
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)) \<sigma> \<sigma>' a"
42
+ thus "otherwith quality_increases {i} (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma> \<sigma>' a"
43
+ by - (erule weaken_otherwith, auto)
44
+ qed
45
+ qed auto
46
+
47
+ lemma par_rreq_rrep_sn_quality_increases:
48
+ "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
49
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
50
+ proof -
51
+ have "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
52
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
53
+ by (rule ostep_invariant_weakenE [OF olocal_quality_increases])
54
+ (auto dest!: onllD seqllD elim!: aodv_ex_labelE)
55
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
56
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
57
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
58
+ thus ?thesis by rule auto
59
+ qed
60
+
61
+ lemma par_rreq_rrep_nsqn_fresh_any_step:
62
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
63
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
64
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
65
+ proof -
66
+ have "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
67
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
68
+ proof (rule ostep_invariant_weakenE [OF rreq_rrep_nsqn_fresh_any_step_invariant])
69
+ fix t
70
+ assume "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), a, _). anycast (msg_fresh \<sigma>) a) t"
71
+ thus "globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a) t"
72
+ by (cases t) (clarsimp dest!: onllD, metis aodv_ex_label)
73
+ qed auto
74
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
75
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
76
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
77
+ thus ?thesis by rule auto
78
+ qed
79
+
80
+ lemma par_anycast_msg_zhops:
81
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
82
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
83
+ proof -
84
+ from anycast_msg_zhops initiali_aodv oaodv_trans aodv_trans
85
+ have "opaodv i \<Turnstile>\<^sub>A (act TT, other (\<lambda>_ _. True) {i} \<rightarrow>)
86
+ seqll i (onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast msg_zhops a))"
87
+ by (rule open_seq_step_invariant)
88
+ hence "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
89
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
90
+ proof (rule ostep_invariant_weakenE)
91
+ fix t :: "(((nat \<Rightarrow> state) \<times> (state, msg, pseqp, pseqp label) seqp), msg seq_action) transition"
92
+ assume "seqll i (onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast msg_zhops a)) t"
93
+ thus "globala (\<lambda>(_, a, _). anycast msg_zhops a) t"
94
+ by (cases t) (clarsimp dest!: seqllD onllD, metis aodv_ex_label)
95
+ qed simp_all
96
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
97
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
98
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
99
+ thus ?thesis by rule auto
100
+ qed
101
+
102
+ subsection \<open>Lift to nodes\<close>
103
+
104
+ lemma node_step_no_change_on_send_or_receive:
105
+ assumes "((\<sigma>, NodeS i P R), a, (\<sigma>', NodeS i' P' R')) \<in> onode_sos
106
+ (oparp_sos i (oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i) (seqp_sos \<Gamma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G))"
107
+ and "a \<noteq> \<tau>"
108
+ shows "\<sigma>' i = \<sigma> i"
109
+ using assms
110
+ by (cases a) (auto elim!: par_step_no_change_on_send_or_receive)
111
+
112
+ lemma node_nhop_quality_increases:
113
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>
114
+ (otherwith ((=)) {i}
115
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
116
+ other quality_increases {i}
117
+ \<rightarrow>) global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
118
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
119
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
120
+ by (rule node_lift [OF par_nhop_quality_increases]) auto
121
+
122
+ lemma node_quality_increases:
123
+ "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
124
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
125
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
126
+ by (rule node_lift_step_statelessassm [OF par_rreq_rrep_sn_quality_increases]) simp
127
+
128
+ lemma node_rreq_rrep_nsqn_fresh_any_step:
129
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A
130
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
131
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). castmsg (msg_fresh \<sigma>) a)"
132
+ by (rule node_lift_anycast_statelessassm [OF par_rreq_rrep_nsqn_fresh_any_step])
133
+
134
+ lemma node_anycast_msg_zhops:
135
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A
136
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
137
+ globala (\<lambda>(_, a, _). castmsg msg_zhops a)"
138
+ by (rule node_lift_anycast_statelessassm [OF par_anycast_msg_zhops])
139
+
140
+ lemma node_silent_change_only:
141
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i \<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_ _. True) \<sigma>,
142
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
143
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). a \<noteq> \<tau> \<longrightarrow> \<sigma>' i = \<sigma> i)"
144
+ proof (rule ostep_invariantI, simp (no_asm), rule impI)
145
+ fix \<sigma> \<zeta> a \<sigma>' \<zeta>'
146
+ assume or: "(\<sigma>, \<zeta>) \<in> oreachable (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i\<rangle>\<^sub>o)
147
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_ _. True) \<sigma>)
148
+ (other (\<lambda>_ _. True) {i})"
149
+ and tr: "((\<sigma>, \<zeta>), a, (\<sigma>', \<zeta>')) \<in> trans (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i\<rangle>\<^sub>o)"
150
+ and "a \<noteq> \<tau>\<^sub>n"
151
+ from or obtain p R where "\<zeta> = NodeS i p R"
152
+ by - (drule node_net_state, metis)
153
+ with tr have "((\<sigma>, NodeS i p R), a, (\<sigma>', \<zeta>'))
154
+ \<in> onode_sos (oparp_sos i (trans (opaodv i)) (trans qmsg))"
155
+ by simp
156
+ thus "\<sigma>' i = \<sigma> i" using \<open>a \<noteq> \<tau>\<^sub>n\<close>
157
+ by (cases rule: onode_sos.cases)
158
+ (auto elim: qmsg_no_change_on_send_or_receive)
159
+ qed
160
+
161
+ subsection \<open>Lift to partial networks\<close>
162
+
163
+ lemma arrive_rreq_rrep_nsqn_fresh_inc_sn [simp]:
164
+ assumes "oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> P \<sigma> m) \<sigma> m"
165
+ shows "oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma> m"
166
+ using assms by (cases m) auto
167
+
168
+ lemma opnet_nhop_quality_increases:
169
+ shows "opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p \<Turnstile>
170
+ (otherwith ((=)) (net_tree_ips p)
171
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
172
+ other quality_increases (net_tree_ips p) \<rightarrow>)
173
+ global (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips p. \<forall>dip.
174
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
175
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
176
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
177
+ proof (rule pnet_lift [OF node_nhop_quality_increases])
178
+ fix i R
179
+ have "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
180
+ other (\<lambda>_ _. True) {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
181
+ castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a)"
182
+ proof (rule ostep_invariantI, simp (no_asm))
183
+ fix \<sigma> s a \<sigma>' s'
184
+ assume or: "(\<sigma>, s) \<in> oreachable (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o)
185
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>)
186
+ (other (\<lambda>_ _. True) {i})"
187
+ and tr: "((\<sigma>, s), a, (\<sigma>', s')) \<in> trans (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o)"
188
+ and am: "oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma> a"
189
+ from or tr am have "castmsg (msg_fresh \<sigma>) a"
190
+ by (auto dest!: ostep_invariantD [OF node_rreq_rrep_nsqn_fresh_any_step])
191
+ moreover from or tr am have "castmsg (msg_zhops) a"
192
+ by (auto dest!: ostep_invariantD [OF node_anycast_msg_zhops])
193
+ ultimately show "castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a"
194
+ by (case_tac a) auto
195
+ qed
196
+ thus "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
197
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
198
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, _).
199
+ castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a)"
200
+ by rule auto
201
+ next
202
+ fix i R
203
+ show "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
204
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
205
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
206
+ a \<noteq> \<tau> \<and> (\<forall>d. a \<noteq> i:deliver(d)) \<longrightarrow> \<sigma> i = \<sigma>' i)"
207
+ by (rule ostep_invariant_weakenE [OF node_silent_change_only]) auto
208
+ next
209
+ fix i R
210
+ show "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
211
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
212
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
213
+ a = \<tau> \<or> (\<exists>d. a = i:deliver(d)) \<longrightarrow> quality_increases (\<sigma> i) (\<sigma>' i))"
214
+ by (rule ostep_invariant_weakenE [OF node_quality_increases]) auto
215
+ qed simp_all
216
+
217
+ subsection \<open>Lift to closed networks\<close>
218
+
219
+ lemma onet_nhop_quality_increases:
220
+ shows "oclosed (opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p)
221
+ \<Turnstile> (\<lambda>_ _ _. True, other quality_increases (net_tree_ips p) \<rightarrow>)
222
+ global (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips p. \<forall>dip.
223
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
224
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
225
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
226
+ (is "_ \<Turnstile> (_, ?U \<rightarrow>) ?inv")
227
+ proof (rule inclosed_closed)
228
+ from opnet_nhop_quality_increases
229
+ show "opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p
230
+ \<Turnstile> (otherwith ((=)) (net_tree_ips p) inoclosed, ?U \<rightarrow>) ?inv"
231
+ proof (rule oinvariant_weakenE)
232
+ fix \<sigma> \<sigma>' :: "ip \<Rightarrow> state" and a :: "msg node_action"
233
+ assume "otherwith ((=)) (net_tree_ips p) inoclosed \<sigma> \<sigma>' a"
234
+ thus "otherwith ((=)) (net_tree_ips p)
235
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)) \<sigma> \<sigma>' a"
236
+ proof (rule otherwithEI)
237
+ fix \<sigma> :: "ip \<Rightarrow> state" and a :: "msg node_action"
238
+ assume "inoclosed \<sigma> a"
239
+ thus "oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma> a"
240
+ proof (cases a)
241
+ fix ii ni ms
242
+ assume "a = ii\<not>ni:arrive(ms)"
243
+ moreover with \<open>inoclosed \<sigma> a\<close> obtain d di where "ms = newpkt(d, di)"
244
+ by (cases ms) auto
245
+ ultimately show ?thesis by simp
246
+ qed simp_all
247
+ qed
248
+ qed
249
+ qed
250
+
251
+ subsection \<open>Transfer into the standard model\<close>
252
+
253
+ interpretation aodv_openproc: openproc paodv opaodv id
254
+ rewrites "aodv_openproc.initmissing = initmissing"
255
+ proof -
256
+ show "openproc paodv opaodv id"
257
+ proof unfold_locales
258
+ fix i :: ip
259
+ have "{(\<sigma>, \<zeta>). (\<sigma> i, \<zeta>) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<and> (\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j \<in> fst ` \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V j)} \<subseteq> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'"
260
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def
261
+ proof (rule equalityD1)
262
+ show "\<And>f p. {(\<sigma>, \<zeta>). (\<sigma> i, \<zeta>) \<in> {(f i, p)} \<and> (\<forall>j. j \<noteq> i
263
+ \<longrightarrow> \<sigma> j \<in> fst ` {(f j, p)})} = {(f, p)}"
264
+ by (rule set_eqI) auto
265
+ qed
266
+ thus "{ (\<sigma>, \<zeta>) |\<sigma> \<zeta> s. s \<in> init (paodv i)
267
+ \<and> (\<sigma> i, \<zeta>) = id s
268
+ \<and> (\<forall>j. j\<noteq>i \<longrightarrow> \<sigma> j \<in> (fst o id) ` init (paodv j)) } \<subseteq> init (opaodv i)"
269
+ by simp
270
+ next
271
+ show "\<forall>j. init (paodv j) \<noteq> {}"
272
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
273
+ next
274
+ fix i s a s' \<sigma> \<sigma>'
275
+ assume "\<sigma> i = fst (id s)"
276
+ and "\<sigma>' i = fst (id s')"
277
+ and "(s, a, s') \<in> trans (paodv i)"
278
+ then obtain q q' where "s = (\<sigma> i, q)"
279
+ and "s' = (\<sigma>' i, q')"
280
+ and "((\<sigma> i, q), a, (\<sigma>' i, q')) \<in> trans (paodv i)"
281
+ by (cases s, cases s') auto
282
+ from this(3) have "((\<sigma>, q), a, (\<sigma>', q')) \<in> trans (opaodv i)"
283
+ by simp (rule open_seqp_action [OF aodv_wf])
284
+
285
+ with \<open>s = (\<sigma> i, q)\<close> and \<open>s' = (\<sigma>' i, q')\<close>
286
+ show "((\<sigma>, snd (id s)), a, (\<sigma>', snd (id s'))) \<in> trans (opaodv i)"
287
+ by simp
288
+ qed
289
+ then interpret opn: openproc paodv opaodv id .
290
+ have [simp]: "\<And>i. (SOME x. x \<in> (fst o id) ` init (paodv i)) = aodv_init i"
291
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
292
+ hence "\<And>i. openproc.initmissing paodv id i = initmissing i"
293
+ unfolding opn.initmissing_def opn.someinit_def initmissing_def
294
+ by (auto split: option.split)
295
+ thus "openproc.initmissing paodv id = initmissing" ..
296
+ qed
297
+
298
+ interpretation aodv_openproc_par_qmsg: openproc_parq paodv opaodv id qmsg
299
+ rewrites "aodv_openproc_par_qmsg.netglobal = netglobal"
300
+ and "aodv_openproc_par_qmsg.initmissing = initmissing"
301
+ proof -
302
+ show "openproc_parq paodv opaodv id qmsg"
303
+ by (unfold_locales) simp
304
+ then interpret opq: openproc_parq paodv opaodv id qmsg .
305
+
306
+ have im: "\<And>\<sigma>. openproc.initmissing (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) \<sigma>
307
+ = initmissing \<sigma>"
308
+ unfolding opq.initmissing_def opq.someinit_def initmissing_def
309
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def by (clarsimp cong: option.case_cong)
310
+ thus "openproc.initmissing (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) = initmissing"
311
+ by (rule ext)
312
+ have "\<And>P \<sigma>. openproc.netglobal (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) P \<sigma>
313
+ = netglobal P \<sigma>"
314
+ unfolding opq.netglobal_def netglobal_def opq.initmissing_def initmissing_def opq.someinit_def
315
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def
316
+ by (clarsimp cong: option.case_cong
317
+ simp del: One_nat_def
318
+ simp add: fst_initmissing_netgmap_default_aodv_init_netlift
319
+ [symmetric, unfolded initmissing_def])
320
+ thus "openproc.netglobal (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) = netglobal"
321
+ by auto
322
+ qed
323
+
324
+ lemma net_nhop_quality_increases:
325
+ assumes "wf_net_tree n"
326
+ shows "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal
327
+ (\<lambda>\<sigma>. \<forall>i dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
328
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
329
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
330
+ (is "_ \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>i. ?inv \<sigma> i)")
331
+ proof -
332
+ from \<open>wf_net_tree n\<close>
333
+ have proto: "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips n. \<forall>dip.
334
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
335
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
336
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
337
+ by (rule aodv_openproc_par_qmsg.close_opnet [OF _ onet_nhop_quality_increases])
338
+ show ?thesis
339
+ unfolding invariant_def opnet_sos.opnet_tau1
340
+ proof (rule, simp only: aodv_openproc_par_qmsg.netglobalsimp
341
+ fst_initmissing_netgmap_pair_fst, rule allI)
342
+ fix \<sigma> i
343
+ assume sr: "\<sigma> \<in> reachable (closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n)) TT"
344
+ hence "\<forall>i\<in>net_tree_ips n. ?inv (fst (initmissing (netgmap fst \<sigma>))) i"
345
+ by - (drule invariantD [OF proto],
346
+ simp only: aodv_openproc_par_qmsg.netglobalsimp
347
+ fst_initmissing_netgmap_pair_fst)
348
+ thus "?inv (fst (initmissing (netgmap fst \<sigma>))) i"
349
+ proof (cases "i\<in>net_tree_ips n")
350
+ assume "i\<notin>net_tree_ips n"
351
+ from sr have "\<sigma> \<in> reachable (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) TT" ..
352
+ hence "net_ips \<sigma> = net_tree_ips n" ..
353
+ with \<open>i\<notin>net_tree_ips n\<close> have "i\<notin>net_ips \<sigma>" by simp
354
+ hence "(fst (initmissing (netgmap fst \<sigma>))) i = aodv_init i"
355
+ by simp
356
+ thus ?thesis by simp
357
+ qed metis
358
+ qed
359
+ qed
360
+
361
+ subsection \<open>Loop freedom of AODV\<close>
362
+
363
+ theorem aodv_loop_freedom:
364
+ assumes "wf_net_tree n"
365
+ shows "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>dip. irrefl ((rt_graph \<sigma> dip)\<^sup>+))"
366
+ using assms by (rule aodv_openproc_par_qmsg.netglobal_weakenE
367
+ [OF net_nhop_quality_increases inv_to_loop_freedom])
368
+
369
+ end
formal/afp/AODV/variants/a_norreqid/A_Aodv_Message.thy ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Aodv_Message.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ section "AODV protocol messages"
8
+
9
+ theory A_Aodv_Message
10
+ imports A_Norreqid
11
+ begin
12
+
13
+ datatype msg =
14
+ Rreq nat ip sqn k ip sqn ip
15
+ | Rrep nat ip sqn ip ip
16
+ | Rerr "ip \<rightharpoonup> sqn" ip
17
+ | Newpkt data ip
18
+ | Pkt data ip ip
19
+
20
+ instantiation msg :: msg
21
+ begin
22
+ definition newpkt_def [simp]: "newpkt \<equiv> \<lambda>(d, dip). Newpkt d dip"
23
+ definition eq_newpkt_def: "eq_newpkt m \<equiv> case m of Newpkt d dip \<Rightarrow> True | _ \<Rightarrow> False"
24
+
25
+ instance by intro_classes (simp add: eq_newpkt_def)
26
+ end
27
+
28
+ text \<open>The @{type msg} type models the different messages used within AODV.
29
+ The instantiation as a @{class msg} is a technicality due to the special
30
+ treatment of @{term newpkt} messages in the AWN SOS rules.
31
+ This use of classes allows a clean separation of the AWN-specific definitions
32
+ and these AODV-specific definitions.\<close>
33
+
34
+ definition rreq :: "nat \<times> ip \<times> sqn \<times> k \<times> ip \<times> sqn \<times> ip \<Rightarrow> msg"
35
+ where "rreq \<equiv> \<lambda>(hops, dip, dsn, dsk, oip, osn, sip).
36
+ Rreq hops dip dsn dsk oip osn sip"
37
+
38
+ lemma rreq_simp [simp]:
39
+ "rreq(hops, dip, dsn, dsk, oip, osn, sip) = Rreq hops dip dsn dsk oip osn sip"
40
+ unfolding rreq_def by simp
41
+
42
+ definition rrep :: "nat \<times> ip \<times> sqn \<times> ip \<times> ip \<Rightarrow> msg"
43
+ where "rrep \<equiv> \<lambda>(hops, dip, dsn, oip, sip). Rrep hops dip dsn oip sip"
44
+
45
+ lemma rrep_simp [simp]:
46
+ "rrep(hops, dip, dsn, oip, sip) = Rrep hops dip dsn oip sip"
47
+ unfolding rrep_def by simp
48
+
49
+ definition rerr :: "(ip \<rightharpoonup> sqn) \<times> ip \<Rightarrow> msg"
50
+ where "rerr \<equiv> \<lambda>(dests, sip). Rerr dests sip"
51
+
52
+ lemma rerr_simp [simp]:
53
+ "rerr(dests, sip) = Rerr dests sip"
54
+ unfolding rerr_def by simp
55
+
56
+ lemma not_eq_newpkt_rreq [simp]: "\<not>eq_newpkt (Rreq hops dip dsn dsk oip osn sip)"
57
+ unfolding eq_newpkt_def by simp
58
+
59
+ lemma not_eq_newpkt_rrep [simp]: "\<not>eq_newpkt (Rrep hops dip dsn oip sip)"
60
+ unfolding eq_newpkt_def by simp
61
+
62
+ lemma not_eq_newpkt_rerr [simp]: "\<not>eq_newpkt (Rerr dests sip)"
63
+ unfolding eq_newpkt_def by simp
64
+
65
+ lemma not_eq_newpkt_pkt [simp]: "\<not>eq_newpkt (Pkt d dip sip)"
66
+ unfolding eq_newpkt_def by simp
67
+
68
+ definition pkt :: "data \<times> ip \<times> ip \<Rightarrow> msg"
69
+ where "pkt \<equiv> \<lambda>(d, dip, sip). Pkt d dip sip"
70
+
71
+ lemma pkt_simp [simp]:
72
+ "pkt(d, dip, sip) = Pkt d dip sip"
73
+ unfolding pkt_def by simp
74
+
75
+ end
formal/afp/AODV/variants/a_norreqid/A_Aodv_Predicates.thy ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Aodv_Predicates.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ section "Invariant assumptions and properties"
8
+
9
+ theory A_Aodv_Predicates
10
+ imports A_Aodv
11
+ begin
12
+
13
+ text \<open>Definitions for expression assumptions on incoming messages and properties of
14
+ outgoing messages.\<close>
15
+
16
+ abbreviation not_Pkt :: "msg \<Rightarrow> bool"
17
+ where "not_Pkt m \<equiv> case m of Pkt _ _ _ \<Rightarrow> False | _ \<Rightarrow> True"
18
+
19
+ definition msg_sender :: "msg \<Rightarrow> ip"
20
+ where "msg_sender m \<equiv> case m of Rreq _ _ _ _ _ _ ipc \<Rightarrow> ipc
21
+ | Rrep _ _ _ _ ipc \<Rightarrow> ipc
22
+ | Rerr _ ipc \<Rightarrow> ipc
23
+ | Pkt _ _ ipc \<Rightarrow> ipc"
24
+
25
+ lemma msg_sender_simps [simp]:
26
+ "\<And>hops dip dsn dsk oip osn sip.
27
+ msg_sender (Rreq hops dip dsn dsk oip osn sip) = sip"
28
+ "\<And>hops dip dsn oip sip. msg_sender (Rrep hops dip dsn oip sip) = sip"
29
+ "\<And>dests sip. msg_sender (Rerr dests sip) = sip"
30
+ "\<And>d dip sip. msg_sender (Pkt d dip sip) = sip"
31
+ unfolding msg_sender_def by simp_all
32
+
33
+ definition msg_zhops :: "msg \<Rightarrow> bool"
34
+ where "msg_zhops m \<equiv> case m of
35
+ Rreq hopsc dipc _ _ oipc _ sipc \<Rightarrow> hopsc = 0 \<longrightarrow> oipc = sipc
36
+ | Rrep hopsc dipc _ _ sipc \<Rightarrow> hopsc = 0 \<longrightarrow> dipc = sipc
37
+ | _ \<Rightarrow> True"
38
+
39
+ lemma msg_zhops_simps [simp]:
40
+ "\<And>hops dip dsn dsk oip osn sip.
41
+ msg_zhops (Rreq hops dip dsn dsk oip osn sip) = (hops = 0 \<longrightarrow> oip = sip)"
42
+ "\<And>hops dip dsn oip sip. msg_zhops (Rrep hops dip dsn oip sip) = (hops = 0 \<longrightarrow> dip = sip)"
43
+ "\<And>dests sip. msg_zhops (Rerr dests sip) = True"
44
+ "\<And>d dip. msg_zhops (Newpkt d dip) = True"
45
+ "\<And>d dip sip. msg_zhops (Pkt d dip sip) = True"
46
+ unfolding msg_zhops_def by simp_all
47
+
48
+ definition rreq_rrep_sn :: "msg \<Rightarrow> bool"
49
+ where "rreq_rrep_sn m \<equiv> case m of Rreq _ _ _ _ _ osnc _ \<Rightarrow> osnc \<ge> 1
50
+ | Rrep _ _ dsnc _ _ \<Rightarrow> dsnc \<ge> 1
51
+ | _ \<Rightarrow> True"
52
+
53
+ lemma rreq_rrep_sn_simps [simp]:
54
+ "\<And>hops dip dsn dsk oip osn sip.
55
+ rreq_rrep_sn (Rreq hops dip dsn dsk oip osn sip) = (osn \<ge> 1)"
56
+ "\<And>hops dip dsn oip sip. rreq_rrep_sn (Rrep hops dip dsn oip sip) = (dsn \<ge> 1)"
57
+ "\<And>dests sip. rreq_rrep_sn (Rerr dests sip) = True"
58
+ "\<And>d dip. rreq_rrep_sn (Newpkt d dip) = True"
59
+ "\<And>d dip sip. rreq_rrep_sn (Pkt d dip sip) = True"
60
+ unfolding rreq_rrep_sn_def by simp_all
61
+
62
+ definition rreq_rrep_fresh :: "rt \<Rightarrow> msg \<Rightarrow> bool"
63
+ where "rreq_rrep_fresh crt m \<equiv> case m of Rreq hopsc _ _ _ oipc osnc ipcc \<Rightarrow> (ipcc \<noteq> oipc \<longrightarrow>
64
+ oipc\<in>kD(crt) \<and> (sqn crt oipc > osnc
65
+ \<or> (sqn crt oipc = osnc
66
+ \<and> the (dhops crt oipc) \<le> hopsc
67
+ \<and> the (flag crt oipc) = val)))
68
+ | Rrep hopsc dipc dsnc _ ipcc \<Rightarrow> (ipcc \<noteq> dipc \<longrightarrow>
69
+ dipc\<in>kD(crt)
70
+ \<and> sqn crt dipc = dsnc
71
+ \<and> the (dhops crt dipc) = hopsc
72
+ \<and> the (flag crt dipc) = val)
73
+ | _ \<Rightarrow> True"
74
+
75
+ lemma rreq_rrep_fresh [simp]:
76
+ "\<And>hops dip dsn dsk oip osn sip.
77
+ rreq_rrep_fresh crt (Rreq hops dip dsn dsk oip osn sip) =
78
+ (sip \<noteq> oip \<longrightarrow> oip\<in>kD(crt)
79
+ \<and> (sqn crt oip > osn
80
+ \<or> (sqn crt oip = osn
81
+ \<and> the (dhops crt oip) \<le> hops
82
+ \<and> the (flag crt oip) = val)))"
83
+ "\<And>hops dip dsn oip sip. rreq_rrep_fresh crt (Rrep hops dip dsn oip sip) =
84
+ (sip \<noteq> dip \<longrightarrow> dip\<in>kD(crt)
85
+ \<and> sqn crt dip = dsn
86
+ \<and> the (dhops crt dip) = hops
87
+ \<and> the (flag crt dip) = val)"
88
+ "\<And>dests sip. rreq_rrep_fresh crt (Rerr dests sip) = True"
89
+ "\<And>d dip. rreq_rrep_fresh crt (Newpkt d dip) = True"
90
+ "\<And>d dip sip. rreq_rrep_fresh crt (Pkt d dip sip) = True"
91
+ unfolding rreq_rrep_fresh_def by simp_all
92
+
93
+ definition rerr_invalid :: "rt \<Rightarrow> msg \<Rightarrow> bool"
94
+ where "rerr_invalid crt m \<equiv> case m of Rerr destsc _ \<Rightarrow> (\<forall>ripc\<in>dom(destsc).
95
+ (ripc\<in>iD(crt) \<and> the (destsc ripc) = sqn crt ripc))
96
+ | _ \<Rightarrow> True"
97
+
98
+ lemma rerr_invalid [simp]:
99
+ "\<And>hops dip dsn dsk oip osn sip.
100
+ rerr_invalid crt (Rreq hops dip dsn dsk oip osn sip) = True"
101
+ "\<And>hops dip dsn oip sip. rerr_invalid crt (Rrep hops dip dsn oip sip) = True"
102
+ "\<And>dests sip. rerr_invalid crt (Rerr dests sip) = (\<forall>rip\<in>dom(dests).
103
+ rip\<in>iD(crt) \<and> the (dests rip) = sqn crt rip)"
104
+ "\<And>d dip. rerr_invalid crt (Newpkt d dip) = True"
105
+ "\<And>d dip sip. rerr_invalid crt (Pkt d dip sip) = True"
106
+ unfolding rerr_invalid_def by simp_all
107
+
108
+ definition
109
+ initmissing :: "(nat \<Rightarrow> state option) \<times> 'a \<Rightarrow> (nat \<Rightarrow> state) \<times> 'a"
110
+ where
111
+ "initmissing \<sigma> = (\<lambda>i. case (fst \<sigma>) i of None \<Rightarrow> aodv_init i | Some s \<Rightarrow> s, snd \<sigma>)"
112
+
113
+ lemma not_in_net_ips_fst_init_missing [simp]:
114
+ assumes "i \<notin> net_ips \<sigma>"
115
+ shows "fst (initmissing (netgmap fst \<sigma>)) i = aodv_init i"
116
+ using assms unfolding initmissing_def by simp
117
+
118
+ lemma fst_initmissing_netgmap_pair_fst [simp]:
119
+ "fst (initmissing (netgmap (\<lambda>(p, q). (fst (id p), snd (id p), q)) s))
120
+ = fst (initmissing (netgmap fst s))"
121
+ unfolding initmissing_def by auto
122
+
123
+ text \<open>We introduce a streamlined alternative to @{term initmissing} with @{term netgmap}
124
+ to simplify invariant statements and thus facilitate their comprehension and
125
+ presentation.\<close>
126
+
127
+ lemma fst_initmissing_netgmap_default_aodv_init_netlift:
128
+ "fst (initmissing (netgmap fst s)) = default aodv_init (netlift fst s)"
129
+ unfolding initmissing_def default_def
130
+ by (simp add: fst_netgmap_netlift del: One_nat_def)
131
+
132
+ definition
133
+ netglobal :: "((nat \<Rightarrow> state) \<Rightarrow> bool) \<Rightarrow> ((state \<times> 'b) \<times> 'c) net_state \<Rightarrow> bool"
134
+ where
135
+ "netglobal P \<equiv> (\<lambda>s. P (default aodv_init (netlift fst s)))"
136
+
137
+ end
formal/afp/AODV/variants/a_norreqid/A_Fresher.thy ADDED
@@ -0,0 +1,799 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Fresher.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Quality relations between routes"
7
+
8
+ theory A_Fresher
9
+ imports A_Aodv_Data
10
+ begin
11
+
12
+ subsection "Net sequence numbers"
13
+
14
+ subsubsection "On individual routes"
15
+
16
+ definition
17
+ nsqn\<^sub>r :: "r \<Rightarrow> sqn"
18
+ where
19
+ "nsqn\<^sub>r r \<equiv> if \<pi>\<^sub>4(r) = val \<or> \<pi>\<^sub>2(r) = 0 then \<pi>\<^sub>2(r) else (\<pi>\<^sub>2(r) - 1)"
20
+
21
+ lemma nsqnr_def':
22
+ "nsqn\<^sub>r r = (if \<pi>\<^sub>4(r) = inv then \<pi>\<^sub>2(r) - 1 else \<pi>\<^sub>2(r))"
23
+ unfolding nsqn\<^sub>r_def by simp
24
+
25
+ lemma nsqn\<^sub>r_zero [simp]:
26
+ "\<And>dsn dsk flag hops nhip pre. nsqn\<^sub>r (0, dsk, flag, hops, nhip, pre) = 0"
27
+ unfolding nsqn\<^sub>r_def by clarsimp
28
+
29
+ lemma nsqn\<^sub>r_val [simp]:
30
+ "\<And>dsn dsk hops nhip pre. nsqn\<^sub>r (dsn, dsk, val, hops, nhip, pre) = dsn"
31
+ unfolding nsqn\<^sub>r_def by clarsimp
32
+
33
+ lemma nsqn\<^sub>r_inv [simp]:
34
+ "\<And>dsn dsk hops nhip pre. nsqn\<^sub>r (dsn, dsk, inv, hops, nhip, pre) = dsn - 1"
35
+ unfolding nsqn\<^sub>r_def by clarsimp
36
+
37
+ lemma nsqn\<^sub>r_lte_dsn [simp]:
38
+ "\<And>dsn dsk flag hops nhip pre. nsqn\<^sub>r (dsn, dsk, flag, hops, nhip, pre) \<le> dsn"
39
+ unfolding nsqn\<^sub>r_def by clarsimp
40
+
41
+ subsubsection "On routes in routing tables"
42
+
43
+ definition
44
+ nsqn :: "rt \<Rightarrow> ip \<Rightarrow> sqn"
45
+ where
46
+ "nsqn \<equiv> \<lambda>rt dip. case \<sigma>\<^bsub>route\<^esub>(rt, dip) of None \<Rightarrow> 0 | Some r \<Rightarrow> nsqn\<^sub>r(r)"
47
+
48
+ lemma nsqn_sqn_def:
49
+ "\<And>rt dip. nsqn rt dip = (if flag rt dip = Some val \<or> sqn rt dip = 0
50
+ then sqn rt dip else sqn rt dip - 1)"
51
+ unfolding nsqn_def sqn_def by (clarsimp split: option.split)
52
+
53
+ lemma not_in_kD_nsqn [simp]:
54
+ assumes "dip \<notin> kD(rt)"
55
+ shows "nsqn rt dip = 0"
56
+ using assms unfolding nsqn_def by simp
57
+
58
+ lemma kD_nsqn:
59
+ assumes "dip \<in> kD(rt)"
60
+ shows "nsqn rt dip = nsqn\<^sub>r(the (\<sigma>\<^bsub>route\<^esub>(rt, dip)))"
61
+ using assms [THEN kD_Some] unfolding nsqn_def by clarsimp
62
+
63
+ lemma nsqnr_r_flag_pred [simp, intro]:
64
+ fixes dsn dsk flag hops nhip pre
65
+ assumes "P (nsqn\<^sub>r (dsn, dsk, val, hops, nhip, pre))"
66
+ and "P (nsqn\<^sub>r (dsn, dsk, inv, hops, nhip, pre))"
67
+ shows "P (nsqn\<^sub>r (dsn, dsk, flag, hops, nhip, pre))"
68
+ using assms by (cases flag) auto
69
+
70
+ lemma nsqn\<^sub>r_addpreRT_inv [simp]:
71
+ "\<And>rt dip npre dip'. dip \<in> kD(rt) \<Longrightarrow>
72
+ nsqn\<^sub>r (the (the (addpreRT rt dip npre) dip')) = nsqn\<^sub>r (the (rt dip'))"
73
+ unfolding addpreRT_def nsqn\<^sub>r_def
74
+ by (frule kD_Some) (clarsimp split: option.split)
75
+
76
+ lemma sqn_nsqn:
77
+ "\<And>rt dip. sqn rt dip - 1 \<le> nsqn rt dip"
78
+ unfolding sqn_def nsqn_def by (clarsimp split: option.split)
79
+
80
+ lemma nsqn_sqn: "nsqn rt dip \<le> sqn rt dip"
81
+ unfolding sqn_def nsqn_def by (cases "rt dip") auto
82
+
83
+ lemma val_nsqn_sqn [elim, simp]:
84
+ assumes "ip\<in>kD(rt)"
85
+ and "the (flag rt ip) = val"
86
+ shows "nsqn rt ip = sqn rt ip"
87
+ using assms unfolding nsqn_sqn_def by auto
88
+
89
+ lemma vD_nsqn_sqn [elim, simp]:
90
+ assumes "ip\<in>vD(rt)"
91
+ shows "nsqn rt ip = sqn rt ip"
92
+ proof -
93
+ from \<open>ip\<in>vD(rt)\<close> have "ip\<in>kD(rt)"
94
+ and "the (flag rt ip) = val" by auto
95
+ thus ?thesis ..
96
+ qed
97
+
98
+ lemma inv_nsqn_sqn [elim, simp]:
99
+ assumes "ip\<in>kD(rt)"
100
+ and "the (flag rt ip) = inv"
101
+ shows "nsqn rt ip = sqn rt ip - 1"
102
+ using assms unfolding nsqn_sqn_def by auto
103
+
104
+ lemma iD_nsqn_sqn [elim, simp]:
105
+ assumes "ip\<in>iD(rt)"
106
+ shows "nsqn rt ip = sqn rt ip - 1"
107
+ proof -
108
+ from \<open>ip\<in>iD(rt)\<close> have "ip\<in>kD(rt)"
109
+ and "the (flag rt ip) = inv" by auto
110
+ thus ?thesis ..
111
+ qed
112
+
113
+ lemma nsqn_update_changed_kno_val [simp]: "\<And>rt ip dsn dsk hops nhip.
114
+ rt \<noteq> update rt ip (dsn, kno, val, hops, nhip, {})
115
+ \<Longrightarrow> nsqn (update rt ip (dsn, kno, val, hops, nhip, {})) ip = dsn"
116
+ unfolding nsqn\<^sub>r_def update_def
117
+ by (clarsimp simp: kD_nsqn split: option.split_asm option.split if_split_asm)
118
+ (metis fun_upd_triv)
119
+
120
+ lemma nsqn_addpreRT_inv [simp]:
121
+ "\<And>rt dip npre dip'. dip \<in> kD(rt) \<Longrightarrow>
122
+ nsqn (the (addpreRT rt dip npre)) dip' = nsqn rt dip'"
123
+ unfolding addpreRT_def nsqn_def nsqn\<^sub>r_def
124
+ by (frule kD_Some) (clarsimp split: option.split)
125
+
126
+ lemma nsqn_update_other [simp]:
127
+ fixes dsn dsk flag hops dip nhip pre rt ip
128
+ assumes "dip \<noteq> ip"
129
+ shows "nsqn (update rt ip (dsn, dsk, flag, hops, nhip, pre)) dip = nsqn rt dip"
130
+ using assms unfolding nsqn_def
131
+ by (clarsimp split: option.split)
132
+
133
+ lemma nsqn_invalidate_eq:
134
+ assumes "dip \<in> kD(rt)"
135
+ and "dests dip = Some rsn"
136
+ shows "nsqn (invalidate rt dests) dip = rsn - 1"
137
+ using assms
138
+ proof -
139
+ from assms obtain dsk hops nhip pre
140
+ where "invalidate rt dests dip = Some (rsn, dsk, inv, hops, nhip, pre)"
141
+ unfolding invalidate_def
142
+ by auto
143
+ moreover from \<open>dip \<in> kD(rt)\<close> have "dip \<in> kD(invalidate rt dests)" by simp
144
+ ultimately show ?thesis
145
+ using \<open>dests dip = Some rsn\<close> by simp
146
+ qed
147
+
148
+ lemma nsqn_invalidate_other [simp]:
149
+ assumes "dip\<in>kD(rt)"
150
+ and "dip\<notin>dom dests"
151
+ shows "nsqn (invalidate rt dests) dip = nsqn rt dip"
152
+ using assms by (clarsimp simp add: kD_nsqn)
153
+
154
+ subsection "Comparing routes "
155
+
156
+ definition
157
+ fresher :: "r \<Rightarrow> r \<Rightarrow> bool" ("(_/ \<sqsubseteq> _)" [51, 51] 50)
158
+ where
159
+ "fresher r r' \<equiv> ((nsqn\<^sub>r r < nsqn\<^sub>r r') \<or> (nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r')))"
160
+
161
+ lemma fresherI1 [intro]:
162
+ assumes "nsqn\<^sub>r r < nsqn\<^sub>r r'"
163
+ shows "r \<sqsubseteq> r'"
164
+ unfolding fresher_def using assms by simp
165
+
166
+ lemma fresherI2 [intro]:
167
+ assumes "nsqn\<^sub>r r = nsqn\<^sub>r r'"
168
+ and "\<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r')"
169
+ shows "r \<sqsubseteq> r'"
170
+ unfolding fresher_def using assms by simp
171
+
172
+ lemma fresherI [intro]:
173
+ assumes "(nsqn\<^sub>r r < nsqn\<^sub>r r') \<or> (nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r'))"
174
+ shows "r \<sqsubseteq> r'"
175
+ unfolding fresher_def using assms .
176
+
177
+ lemma fresherE [elim]:
178
+ assumes "r \<sqsubseteq> r'"
179
+ and "nsqn\<^sub>r r < nsqn\<^sub>r r' \<Longrightarrow> P r r'"
180
+ and "nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r') \<Longrightarrow> P r r'"
181
+ shows "P r r'"
182
+ using assms unfolding fresher_def by auto
183
+
184
+ lemma fresher_refl [simp]: "r \<sqsubseteq> r"
185
+ unfolding fresher_def by simp
186
+
187
+ lemma fresher_trans [elim, trans]:
188
+ "\<lbrakk> x \<sqsubseteq> y; y \<sqsubseteq> z \<rbrakk> \<Longrightarrow> x \<sqsubseteq> z"
189
+ unfolding fresher_def by auto
190
+
191
+ lemma not_fresher_trans [elim, trans]:
192
+ "\<lbrakk> \<not>(x \<sqsubseteq> y); \<not>(z \<sqsubseteq> x) \<rbrakk> \<Longrightarrow> \<not>(z \<sqsubseteq> y)"
193
+ unfolding fresher_def by auto
194
+
195
+ lemma fresher_dsn_flag_hops_const [simp]:
196
+ fixes dsn dsk dsk' flag hops nhip nhip' pre pre'
197
+ shows "(dsn, dsk, flag, hops, nhip, pre) \<sqsubseteq> (dsn, dsk', flag, hops, nhip', pre')"
198
+ unfolding fresher_def by (cases flag) simp_all
199
+
200
+ lemma addpre_fresher [simp]: "\<And>r npre. r \<sqsubseteq> (addpre r npre)"
201
+ by clarsimp
202
+
203
+ subsection "Comparing routing tables "
204
+
205
+ definition
206
+ rt_fresher :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
207
+ where
208
+ "rt_fresher \<equiv> \<lambda>dip rt rt'. (the (\<sigma>\<^bsub>route\<^esub>(rt, dip))) \<sqsubseteq> (the (\<sigma>\<^bsub>route\<^esub>(rt', dip)))"
209
+
210
+ abbreviation
211
+ rt_fresher_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<sqsubseteq>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
212
+ where
213
+ "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2 \<equiv> rt_fresher i rt1 rt2"
214
+
215
+ lemma rt_fresher_def':
216
+ "(rt\<^sub>1 \<sqsubseteq>\<^bsub>i\<^esub> rt\<^sub>2) = (nsqn\<^sub>r (the (rt\<^sub>1 i)) < nsqn\<^sub>r (the (rt\<^sub>2 i)) \<or>
217
+ nsqn\<^sub>r (the (rt\<^sub>1 i)) = nsqn\<^sub>r (the (rt\<^sub>2 i)) \<and> \<pi>\<^sub>5 (the (rt\<^sub>2 i)) \<le> \<pi>\<^sub>5 (the (rt\<^sub>1 i)))"
218
+ unfolding rt_fresher_def fresher_def by (rule refl)
219
+
220
+ lemma single_rt_fresher [intro]:
221
+ assumes "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
222
+ shows "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
223
+ using assms unfolding rt_fresher_def .
224
+
225
+ lemma rt_fresher_single [intro]:
226
+ assumes "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
227
+ shows "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
228
+ using assms unfolding rt_fresher_def .
229
+
230
+ lemma rt_fresher_def2:
231
+ assumes "dip \<in> kD(rt1)"
232
+ and "dip \<in> kD(rt2)"
233
+ shows "(rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) = (nsqn rt1 dip < nsqn rt2 dip
234
+ \<or> (nsqn rt1 dip = nsqn rt2 dip
235
+ \<and> the (dhops rt1 dip) \<ge> the (dhops rt2 dip)))"
236
+ using assms unfolding rt_fresher_def fresher_def by (simp add: kD_nsqn proj5_eq_dhops)
237
+
238
+ lemma rt_fresherI1 [intro]:
239
+ assumes "dip \<in> kD(rt1)"
240
+ and "dip \<in> kD(rt2)"
241
+ and "nsqn rt1 dip < nsqn rt2 dip"
242
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
243
+ unfolding rt_fresher_def2 [OF assms(1-2)] using assms(3) by simp
244
+
245
+ lemma rt_fresherI2 [intro]:
246
+ assumes "dip \<in> kD(rt1)"
247
+ and "dip \<in> kD(rt2)"
248
+ and "nsqn rt1 dip = nsqn rt2 dip"
249
+ and "the (dhops rt1 dip) \<ge> the (dhops rt2 dip)"
250
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
251
+ unfolding rt_fresher_def2 [OF assms(1-2)] using assms(3-4) by simp
252
+
253
+ lemma rt_fresherE [elim]:
254
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
255
+ and "dip \<in> kD(rt1)"
256
+ and "dip \<in> kD(rt2)"
257
+ and "\<lbrakk> nsqn rt1 dip < nsqn rt2 dip \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
258
+ and "\<lbrakk> nsqn rt1 dip = nsqn rt2 dip;
259
+ the (dhops rt1 dip) \<ge> the (dhops rt2 dip) \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
260
+ shows "P rt1 rt2 dip"
261
+ using assms(1) unfolding rt_fresher_def2 [OF assms(2-3)]
262
+ using assms(4-5) by auto
263
+
264
+ lemma rt_fresher_refl [simp]: "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt"
265
+ unfolding rt_fresher_def by simp
266
+
267
+ lemma rt_fresher_trans [elim, trans]:
268
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
269
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
270
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
271
+ using assms unfolding rt_fresher_def by auto
272
+
273
+ lemma rt_fresher_if_Some [intro!]:
274
+ assumes "the (rt dip) \<sqsubseteq> r"
275
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> (\<lambda>ip. if ip = dip then Some r else rt ip)"
276
+ using assms unfolding rt_fresher_def by simp
277
+
278
+ definition rt_fresh_as :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
279
+ where
280
+ "rt_fresh_as \<equiv> \<lambda>dip rt1 rt2. (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) \<and> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
281
+
282
+ abbreviation
283
+ rt_fresh_as_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<approx>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
284
+ where
285
+ "rt1 \<approx>\<^bsub>i\<^esub> rt2 \<equiv> rt_fresh_as i rt1 rt2"
286
+
287
+ lemma rt_fresh_as_refl [simp]: "\<And>rt dip. rt \<approx>\<^bsub>dip\<^esub> rt"
288
+ unfolding rt_fresh_as_def by simp
289
+
290
+ lemma rt_fresh_as_trans [simp, intro, trans]:
291
+ "\<And>rt1 rt2 rt3 dip. \<lbrakk> rt1 \<approx>\<^bsub>dip\<^esub> rt2; rt2 \<approx>\<^bsub>dip\<^esub> rt3 \<rbrakk> \<Longrightarrow> rt1 \<approx>\<^bsub>dip\<^esub> rt3"
292
+ unfolding rt_fresh_as_def rt_fresher_def
293
+ by (metis (mono_tags) fresher_trans)
294
+
295
+ lemma rt_fresh_asI [intro!]:
296
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
297
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
298
+ shows "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
299
+ using assms unfolding rt_fresh_as_def by simp
300
+
301
+ lemma rt_fresh_as_fresherI [intro]:
302
+ assumes "dip\<in>kD(rt1)"
303
+ and "dip\<in>kD(rt2)"
304
+ and "the (rt1 dip) \<sqsubseteq> the (rt2 dip)"
305
+ and "the (rt2 dip) \<sqsubseteq> the (rt1 dip)"
306
+ shows "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
307
+ using assms unfolding rt_fresh_as_def
308
+ by (clarsimp dest!: single_rt_fresher)
309
+
310
+ lemma nsqn_rt_fresh_asI:
311
+ assumes "dip \<in> kD(rt)"
312
+ and "dip \<in> kD(rt')"
313
+ and "nsqn rt dip = nsqn rt' dip"
314
+ and "\<pi>\<^sub>5(the (rt dip)) = \<pi>\<^sub>5(the (rt' dip))"
315
+ shows "rt \<approx>\<^bsub>dip\<^esub> rt'"
316
+ proof
317
+ from assms(1-2,4) have dhops': "the (dhops rt' dip) \<le> the (dhops rt dip)"
318
+ by (simp add: proj5_eq_dhops)
319
+ with assms(1-3) show "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt'"
320
+ by (rule rt_fresherI2)
321
+ next
322
+ from assms(1-2,4) have dhops: "the (dhops rt dip) \<le> the (dhops rt' dip)"
323
+ by (simp add: proj5_eq_dhops)
324
+ with assms(2,1) assms(3) [symmetric] show "rt' \<sqsubseteq>\<^bsub>dip\<^esub> rt"
325
+ by (rule rt_fresherI2)
326
+ qed
327
+
328
+ lemma rt_fresh_asE [elim]:
329
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
330
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2; rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1 \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
331
+ shows "P rt1 rt2 dip"
332
+ using assms unfolding rt_fresh_as_def by simp
333
+
334
+ lemma rt_fresh_asD1 [dest]:
335
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
336
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
337
+ using assms unfolding rt_fresh_as_def by simp
338
+
339
+ lemma rt_fresh_asD2 [dest]:
340
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
341
+ shows "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
342
+ using assms unfolding rt_fresh_as_def by simp
343
+
344
+ lemma rt_fresh_as_sym:
345
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
346
+ shows "rt2 \<approx>\<^bsub>dip\<^esub> rt1"
347
+ using assms unfolding rt_fresh_as_def by simp
348
+
349
+ lemma not_rt_fresh_asI1 [intro]:
350
+ assumes "\<not> (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)"
351
+ shows "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
352
+ proof
353
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
354
+ hence "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" ..
355
+ with \<open>\<not> (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)\<close> show False ..
356
+ qed
357
+
358
+ lemma not_rt_fresh_asI2 [intro]:
359
+ assumes "\<not> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
360
+ shows "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
361
+ proof
362
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
363
+ hence "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
364
+ with \<open>\<not> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)\<close> show False ..
365
+ qed
366
+
367
+ lemma not_single_rt_fresher [elim]:
368
+ assumes "\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))"
369
+ shows "\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)"
370
+ proof
371
+ assume "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
372
+ hence "the (rt1 ip) \<sqsubseteq> the (rt2 ip)" ..
373
+ with \<open>\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))\<close> show False ..
374
+ qed
375
+
376
+ lemmas not_single_rt_fresh_asI1 [intro] = not_rt_fresh_asI1 [OF not_single_rt_fresher]
377
+ lemmas not_single_rt_fresh_asI2 [intro] = not_rt_fresh_asI2 [OF not_single_rt_fresher]
378
+
379
+ lemma not_rt_fresher_single [elim]:
380
+ assumes "\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)"
381
+ shows "\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))"
382
+ proof
383
+ assume "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
384
+ hence "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2" ..
385
+ with \<open>\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)\<close> show False ..
386
+ qed
387
+
388
+ lemma rt_fresh_as_nsqnr:
389
+ assumes "dip \<in> kD(rt1)"
390
+ and "dip \<in> kD(rt2)"
391
+ and "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
392
+ shows "nsqn\<^sub>r (the (rt2 dip)) = nsqn\<^sub>r (the (rt1 dip))"
393
+ using assms(3) unfolding rt_fresh_as_def
394
+ by (auto simp: rt_fresher_def2 [OF \<open>dip \<in> kD(rt1)\<close> \<open>dip \<in> kD(rt2)\<close>]
395
+ rt_fresher_def2 [OF \<open>dip \<in> kD(rt2)\<close> \<open>dip \<in> kD(rt1)\<close>]
396
+ kD_nsqn [OF \<open>dip \<in> kD(rt1)\<close>]
397
+ kD_nsqn [OF \<open>dip \<in> kD(rt2)\<close>])
398
+
399
+ lemma rt_fresher_mapupd [intro!]:
400
+ assumes "dip\<in>kD(rt)"
401
+ and "the (rt dip) \<sqsubseteq> r"
402
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt(dip \<mapsto> r)"
403
+ using assms unfolding rt_fresher_def by simp
404
+
405
+ lemma rt_fresher_map_update_other [intro!]:
406
+ assumes "dip\<in>kD(rt)"
407
+ and "dip \<noteq> ip"
408
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt(ip \<mapsto> r)"
409
+ using assms unfolding rt_fresher_def by simp
410
+
411
+ lemma rt_fresher_update_other [simp]:
412
+ assumes inkD: "dip\<in>kD(rt)"
413
+ and "dip \<noteq> ip"
414
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt ip r"
415
+ using assms unfolding update_def
416
+ by (clarsimp split: option.split) (fastforce)
417
+
418
+ theorem rt_fresher_update [simp]:
419
+ assumes "dip\<in>kD(rt)"
420
+ and "the (dhops rt dip) \<ge> 1"
421
+ and "update_arg_wf r"
422
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt ip r"
423
+ proof (cases "dip = ip")
424
+ assume "dip \<noteq> ip" with \<open>dip\<in>kD(rt)\<close> show ?thesis
425
+ by (rule rt_fresher_update_other)
426
+ next
427
+ assume "dip = ip"
428
+
429
+ from \<open>dip\<in>kD(rt)\<close> obtain dsn\<^sub>n dsk\<^sub>n f\<^sub>n hops\<^sub>n nhip\<^sub>n pre\<^sub>n
430
+ where rtn [simp]: "the (rt dip) = (dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)"
431
+ by (metis prod_cases6)
432
+ with \<open>the (dhops rt dip) \<ge> 1\<close> and \<open>dip\<in>kD(rt)\<close> have "hops\<^sub>n \<ge> 1"
433
+ by (metis proj5_eq_dhops projs(4))
434
+ from \<open>dip\<in>kD(rt)\<close> rtn have [simp]: "sqn rt dip = dsn\<^sub>n"
435
+ and [simp]: "the (dhops rt dip) = hops\<^sub>n"
436
+ and [simp]: "the (flag rt dip) = f\<^sub>n"
437
+ by (simp add: sqn_def proj5_eq_dhops [symmetric]
438
+ proj4_eq_flag [symmetric])+
439
+
440
+ from \<open>update_arg_wf r\<close> have "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
441
+ \<sqsubseteq> the ((update rt dip r) dip)"
442
+ proof (rule wf_r_cases)
443
+ fix nhip pre
444
+ from \<open>hops\<^sub>n \<ge> 1\<close> have "\<And>pre'. (dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
445
+ \<sqsubseteq> (dsn\<^sub>n, unk, val, Suc 0, nhip, pre')"
446
+ unfolding fresher_def sqn_def by (cases f\<^sub>n) auto
447
+ thus "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
448
+ \<sqsubseteq> the (update rt dip (0, unk, val, Suc 0, nhip, pre) dip)"
449
+ using \<open>dip\<in>kD(rt)\<close> by - (rule update_cases_kD, simp_all)
450
+ next
451
+ fix dsn :: sqn and hops nhip pre
452
+ assume "0 < dsn"
453
+ show "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
454
+ \<sqsubseteq> the (update rt dip (dsn, kno, val, hops, nhip, pre) dip)"
455
+ proof (rule update_cases_kD [OF _ \<open>dip\<in>kD(rt)\<close>], simp_all add: \<open>0 < dsn\<close>)
456
+ assume "dsn\<^sub>n < dsn"
457
+ thus "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
458
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
459
+ unfolding fresher_def by auto
460
+ next
461
+ assume "dsn\<^sub>n = dsn"
462
+ and "hops < hops\<^sub>n"
463
+ thus "(dsn, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
464
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
465
+ unfolding fresher_def nsqn\<^sub>r_def by simp
466
+ next
467
+ assume "dsn\<^sub>n = dsn"
468
+ with \<open>0 < dsn\<close>
469
+ show "(dsn, dsk\<^sub>n, inv, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
470
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
471
+ unfolding fresher_def by simp
472
+ qed
473
+ qed
474
+ hence "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt dip r"
475
+ by - (rule single_rt_fresher, simp)
476
+ with \<open>dip = ip\<close> show ?thesis by simp
477
+ qed
478
+
479
+ theorem rt_fresher_invalidate [simp]:
480
+ assumes "dip\<in>kD(rt)"
481
+ and indests: "\<forall>rip\<in>dom(dests). rip\<in>vD(rt) \<and> sqn rt rip < the (dests rip)"
482
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> invalidate rt dests"
483
+ proof (cases "dip\<in>dom(dests)")
484
+ assume "dip\<notin>dom(dests)"
485
+ thus ?thesis using \<open>dip\<in>kD(rt)\<close>
486
+ by - (rule single_rt_fresher, simp)
487
+ next
488
+ assume "dip\<in>dom(dests)"
489
+ moreover with indests have "dip\<in>vD(rt)"
490
+ and "sqn rt dip < the (dests dip)"
491
+ by auto
492
+ ultimately show ?thesis
493
+ unfolding invalidate_def sqn_def
494
+ by - (rule single_rt_fresher, auto simp: fresher_def)
495
+ qed
496
+
497
+ lemma nsqn\<^sub>r_invalidate [simp]:
498
+ assumes "dip\<in>kD(rt)"
499
+ and "dip\<in>dom(dests)"
500
+ shows "nsqn\<^sub>r (the (invalidate rt dests dip)) = the (dests dip) - 1"
501
+ using assms unfolding invalidate_def by auto
502
+
503
+ lemma rt_fresh_as_inc_invalidate [simp]:
504
+ assumes "dip\<in>kD(rt)"
505
+ and "\<forall>rip\<in>dom(dests). rip\<in>vD(rt) \<and> the (dests rip) = inc (sqn rt rip)"
506
+ shows "rt \<approx>\<^bsub>dip\<^esub> invalidate rt dests"
507
+ proof (cases "dip\<in>dom(dests)")
508
+ assume "dip\<notin>dom(dests)"
509
+ with \<open>dip\<in>kD(rt)\<close> have "dip\<in>kD(invalidate rt dests)"
510
+ by simp
511
+ with \<open>dip\<in>kD(rt)\<close> show ?thesis
512
+ by rule (simp_all add: \<open>dip\<notin>dom(dests)\<close>)
513
+ next
514
+ assume "dip\<in>dom(dests)"
515
+ with assms(2) have "dip\<in>vD(rt)"
516
+ and "the (dests dip) = inc (sqn rt dip)" by auto
517
+ from \<open>dip\<in>vD(rt)\<close> have "dip\<in>kD(rt)" by simp
518
+ moreover then have "dip\<in>kD(invalidate rt dests)" by simp
519
+ ultimately show ?thesis
520
+ proof (rule nsqn_rt_fresh_asI)
521
+ from \<open>dip\<in>vD(rt)\<close> have "nsqn rt dip = sqn rt dip" by simp
522
+ also have "sqn rt dip = nsqn\<^sub>r (the (invalidate rt dests dip))"
523
+ proof -
524
+ from \<open>dip\<in>kD(rt)\<close> have "nsqn\<^sub>r (the (invalidate rt dests dip)) = the (dests dip) - 1"
525
+ using \<open>dip\<in>dom(dests)\<close> by (rule nsqn\<^sub>r_invalidate)
526
+ with \<open>the (dests dip) = inc (sqn rt dip)\<close>
527
+ show "sqn rt dip = nsqn\<^sub>r (the (invalidate rt dests dip))" by simp
528
+ qed
529
+ also from \<open>dip\<in>kD(invalidate rt dests)\<close>
530
+ have "nsqn\<^sub>r (the (invalidate rt dests dip)) = nsqn (invalidate rt dests) dip"
531
+ by (simp add: kD_nsqn)
532
+ finally show "nsqn rt dip = nsqn (invalidate rt dests) dip" .
533
+ qed simp
534
+ qed
535
+
536
+ lemmas rt_fresher_inc_invalidate [simp] = rt_fresh_as_inc_invalidate [THEN rt_fresh_asD1]
537
+
538
+ lemma rt_fresh_as_addpreRT [simp]:
539
+ assumes "ip\<in>kD(rt)"
540
+ shows "rt \<approx>\<^bsub>dip\<^esub> the (addpreRT rt ip npre)"
541
+ using assms [THEN kD_Some] by (auto simp: addpreRT_def)
542
+
543
+ lemmas rt_fresher_addpreRT [simp] = rt_fresh_as_addpreRT [THEN rt_fresh_asD1]
544
+
545
+ subsection "Strictly comparing routing tables "
546
+
547
+ definition rt_strictly_fresher :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
548
+ where
549
+ "rt_strictly_fresher \<equiv> \<lambda>dip rt1 rt2. (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) \<and> \<not>(rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
550
+
551
+ abbreviation
552
+ rt_strictly_fresher_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<sqsubset>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
553
+ where
554
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 \<equiv> rt_strictly_fresher i rt1 rt2"
555
+
556
+ lemma rt_strictly_fresher_def'':
557
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 = ((rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2) \<and> \<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1))"
558
+ unfolding rt_strictly_fresher_def rt_fresh_as_def by auto
559
+
560
+ lemma rt_strictly_fresherI' [intro]:
561
+ assumes "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2"
562
+ and "\<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1)"
563
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
564
+ using assms unfolding rt_strictly_fresher_def'' by simp
565
+
566
+ lemma rt_strictly_fresherE' [elim]:
567
+ assumes "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
568
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2; \<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1) \<rbrakk> \<Longrightarrow> P rt1 rt2 i"
569
+ shows "P rt1 rt2 i"
570
+ using assms unfolding rt_strictly_fresher_def'' by simp
571
+
572
+ lemma rt_strictly_fresherI [intro]:
573
+ assumes "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2"
574
+ and "\<not>(rt1 \<approx>\<^bsub>i\<^esub> rt2)"
575
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
576
+ unfolding rt_strictly_fresher_def using assms ..
577
+
578
+ lemmas rt_strictly_fresher_singleI [elim] = rt_strictly_fresherI [OF single_rt_fresher]
579
+
580
+ lemma rt_strictly_fresherE [elim]:
581
+ assumes "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
582
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2; \<not>(rt1 \<approx>\<^bsub>i\<^esub> rt2) \<rbrakk> \<Longrightarrow> P rt1 rt2 i"
583
+ shows "P rt1 rt2 i"
584
+ using assms(1) unfolding rt_strictly_fresher_def
585
+ by rule (erule(1) assms(2))
586
+
587
+ lemma rt_strictly_fresher_def':
588
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 =
589
+ (nsqn\<^sub>r (the (rt1 i)) < nsqn\<^sub>r (the (rt2 i))
590
+ \<or> (nsqn\<^sub>r (the (rt1 i)) = nsqn\<^sub>r (the (rt2 i)) \<and> \<pi>\<^sub>5(the (rt1 i)) > \<pi>\<^sub>5(the (rt2 i))))"
591
+ unfolding rt_strictly_fresher_def'' rt_fresher_def fresher_def by auto
592
+
593
+ lemma rt_strictly_fresher_fresherD [dest]:
594
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
595
+ shows "the (rt1 dip) \<sqsubseteq> the (rt2 dip)"
596
+ using assms unfolding rt_strictly_fresher_def rt_fresher_def by auto
597
+
598
+ lemma rt_strictly_fresher_not_fresh_asD [dest]:
599
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
600
+ shows "\<not> rt1 \<approx>\<^bsub>dip\<^esub> rt2"
601
+ using assms unfolding rt_strictly_fresher_def by auto
602
+
603
+ lemma rt_strictly_fresher_trans [elim, trans]:
604
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
605
+ and "rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3"
606
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
607
+ using assms proof -
608
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> obtain "the (rt1 dip) \<sqsubseteq> the (rt2 dip)" by auto
609
+ also from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> obtain "the (rt2 dip) \<sqsubseteq> the (rt3 dip)" by auto
610
+ finally have "the (rt1 dip) \<sqsubseteq> the (rt3 dip)" .
611
+
612
+ moreover have "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt3)"
613
+ proof -
614
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> obtain "\<not>(the (rt2 dip) \<sqsubseteq> the (rt1 dip))" by auto
615
+ also from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> obtain "\<not>(the (rt3 dip) \<sqsubseteq> the (rt2 dip))" by auto
616
+ finally have "\<not>(the (rt3 dip) \<sqsubseteq> the (rt1 dip))" .
617
+ thus ?thesis ..
618
+ qed
619
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3" ..
620
+ qed
621
+
622
+ lemma rt_strictly_fresher_irefl [simp]: "\<not> (rt \<sqsubset>\<^bsub>dip\<^esub> rt)"
623
+ unfolding rt_strictly_fresher_def
624
+ by clarsimp
625
+
626
+ lemma rt_fresher_trans_rt_strictly_fresher [elim, trans]:
627
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
628
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
629
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
630
+ proof -
631
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
632
+ and "\<not>(rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
633
+ unfolding rt_strictly_fresher_def'' by auto
634
+ from this(1) and \<open>rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3\<close> have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3" ..
635
+
636
+ moreover from \<open>\<not>(rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)\<close> have "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
637
+ proof (rule contrapos_nn)
638
+ assume "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
639
+ with \<open>rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3\<close> show "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
640
+ qed
641
+
642
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
643
+ unfolding rt_strictly_fresher_def'' by auto
644
+ qed
645
+
646
+ lemma rt_fresher_trans_rt_strictly_fresher' [elim, trans]:
647
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
648
+ and "rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3"
649
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
650
+ proof -
651
+ from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> have "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
652
+ and "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)"
653
+ unfolding rt_strictly_fresher_def'' by auto
654
+ from \<open>rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2\<close> and this(1) have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3" ..
655
+
656
+ moreover from \<open>\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)\<close> have "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
657
+ proof (rule contrapos_nn)
658
+ assume "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
659
+ thus "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" using \<open>rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2\<close> ..
660
+ qed
661
+
662
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
663
+ unfolding rt_strictly_fresher_def'' by auto
664
+ qed
665
+
666
+ lemma rt_fresher_imp_nsqn_le:
667
+ assumes "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
668
+ and "ip \<in> kD rt1"
669
+ and "ip \<in> kD rt2"
670
+ shows "nsqn rt1 ip \<le> nsqn rt2 ip"
671
+ using assms(1)
672
+ by (auto simp add: rt_fresher_def2 [OF assms(2-3)])
673
+
674
+ lemma rt_strictly_fresher_ltI [intro]:
675
+ assumes "dip \<in> kD(rt1)"
676
+ and "dip \<in> kD(rt2)"
677
+ and "nsqn rt1 dip < nsqn rt2 dip"
678
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
679
+ proof
680
+ from assms show "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" ..
681
+ next
682
+ show "\<not>(rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
683
+ proof
684
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
685
+ hence "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
686
+ hence "nsqn rt2 dip \<le> nsqn rt1 dip"
687
+ using \<open>dip \<in> kD(rt2)\<close> \<open>dip \<in> kD(rt1)\<close>
688
+ by (rule rt_fresher_imp_nsqn_le)
689
+ with \<open>nsqn rt1 dip < nsqn rt2 dip\<close> show "False"
690
+ by simp
691
+ qed
692
+ qed
693
+
694
+ lemma rt_strictly_fresher_eqI [intro]:
695
+ assumes "i\<in>kD(rt1)"
696
+ and "i\<in>kD(rt2)"
697
+ and "nsqn rt1 i = nsqn rt2 i"
698
+ and "\<pi>\<^sub>5(the (rt2 i)) < \<pi>\<^sub>5(the (rt1 i))"
699
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
700
+ using assms unfolding rt_strictly_fresher_def' by (auto simp add: kD_nsqn)
701
+
702
+ lemma invalidate_rtsf_left [simp]:
703
+ "\<And>dests dip rt rt'. dests dip = None \<Longrightarrow> (invalidate rt dests \<sqsubset>\<^bsub>dip\<^esub> rt') = (rt \<sqsubset>\<^bsub>dip\<^esub> rt')"
704
+ unfolding invalidate_def rt_strictly_fresher_def'
705
+ by (rule iffI) (auto split: option.split_asm)
706
+
707
+ lemma vD_invalidate_rt_strictly_fresher [simp]:
708
+ assumes "dip \<in> vD(invalidate rt1 dests)"
709
+ shows "(invalidate rt1 dests \<sqsubset>\<^bsub>dip\<^esub> rt2) = (rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2)"
710
+ proof (cases "dip \<in> dom(dests)")
711
+ assume "dip \<in> dom(dests)"
712
+ hence "dip \<notin> vD(invalidate rt1 dests)"
713
+ unfolding invalidate_def vD_def
714
+ by clarsimp (metis assms option.simps(3) vD_invalidate_vD_not_dests)
715
+ with \<open>dip \<in> vD(invalidate rt1 dests)\<close> show ?thesis by simp
716
+ next
717
+ assume "dip \<notin> dom(dests)"
718
+ hence "dests dip = None" by auto
719
+ moreover with \<open>dip \<in> vD(invalidate rt1 dests)\<close> have "dip \<in> vD(rt1)"
720
+ unfolding invalidate_def vD_def
721
+ by clarsimp (metis (opaque_lifting, no_types) assms vD_Some vD_invalidate_vD_not_dests)
722
+ ultimately show ?thesis
723
+ unfolding invalidate_def rt_strictly_fresher_def' by auto
724
+ qed
725
+
726
+ lemma rt_strictly_fresher_update_other [elim!]:
727
+ "\<And>dip ip rt r rt'. \<lbrakk> dip \<noteq> ip; rt \<sqsubset>\<^bsub>dip\<^esub> rt' \<rbrakk> \<Longrightarrow> update rt ip r \<sqsubset>\<^bsub>dip\<^esub> rt'"
728
+ unfolding rt_strictly_fresher_def' by clarsimp
729
+
730
+ lemma addpreRT_strictly_fresher [simp]:
731
+ assumes "dip \<in> kD(rt)"
732
+ shows "(the (addpreRT rt dip npre) \<sqsubset>\<^bsub>ip\<^esub> rt2) = (rt \<sqsubset>\<^bsub>ip\<^esub> rt2)"
733
+ using assms unfolding rt_strictly_fresher_def' by clarsimp
734
+
735
+ lemma lt_sqn_imp_update_strictly_fresher:
736
+ assumes "dip \<in> vD (rt2 nhip)"
737
+ and *: "osn < sqn (rt2 nhip) dip"
738
+ and **: "rt \<noteq> update rt dip (osn, kno, val, hops, nhip, {})"
739
+ shows "update rt dip (osn, kno, val, hops, nhip, {}) \<sqsubset>\<^bsub>dip\<^esub> rt2 nhip"
740
+ unfolding rt_strictly_fresher_def'
741
+ proof (rule disjI1)
742
+ from ** have "nsqn (update rt dip (osn, kno, val, hops, nhip, {})) dip = osn"
743
+ by (rule nsqn_update_changed_kno_val)
744
+ with \<open>dip\<in>vD(rt2 nhip)\<close>
745
+ have "nsqn\<^sub>r (the (update rt dip (osn, kno, val, hops, nhip, {}) dip)) = osn"
746
+ by (simp add: kD_nsqn)
747
+ also have "osn < sqn (rt2 nhip) dip" by (rule *)
748
+ also have "sqn (rt2 nhip) dip = nsqn\<^sub>r (the (rt2 nhip dip))"
749
+ unfolding nsqn\<^sub>r_def using \<open>dip \<in> vD (rt2 nhip)\<close>
750
+ by - (metis vD_flag_val proj2_eq_sqn proj4_eq_flag vD_iD_gives_kD(1))
751
+ finally show "nsqn\<^sub>r (the (update rt dip (osn, kno, val, hops, nhip, {}) dip))
752
+ < nsqn\<^sub>r (the (rt2 nhip dip))" .
753
+ qed
754
+
755
+ lemma dhops_le_hops_imp_update_strictly_fresher:
756
+ assumes "dip \<in> vD(rt2 nhip)"
757
+ and sqn: "sqn (rt2 nhip) dip = osn"
758
+ and hop: "the (dhops (rt2 nhip) dip) \<le> hops"
759
+ and **: "rt \<noteq> update rt dip (osn, kno, val, Suc hops, nhip, {})"
760
+ shows "update rt dip (osn, kno, val, Suc hops, nhip, {}) \<sqsubset>\<^bsub>dip\<^esub> rt2 nhip"
761
+ unfolding rt_strictly_fresher_def'
762
+ proof (rule disjI2, rule conjI)
763
+ from ** have "nsqn (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip = osn"
764
+ by (rule nsqn_update_changed_kno_val)
765
+ with \<open>dip\<in>vD(rt2 nhip)\<close>
766
+ have "nsqn\<^sub>r (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip)) = osn"
767
+ by (simp add: kD_nsqn)
768
+ also have "osn = sqn (rt2 nhip) dip" by (rule sqn [symmetric])
769
+ also have "sqn (rt2 nhip) dip = nsqn\<^sub>r (the (rt2 nhip dip))"
770
+ unfolding nsqn\<^sub>r_def using \<open>dip \<in> vD(rt2 nhip)\<close>
771
+ by - (metis vD_flag_val proj2_eq_sqn proj4_eq_flag vD_iD_gives_kD(1))
772
+ finally show "nsqn\<^sub>r (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip))
773
+ = nsqn\<^sub>r (the (rt2 nhip dip))" .
774
+ next
775
+ have "the (dhops (rt2 nhip) dip) \<le> hops" by (rule hop)
776
+ also have "hops < hops + 1" by simp
777
+ also have "hops + 1 = the (dhops (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip)"
778
+ using ** by simp
779
+ finally have "the (dhops (rt2 nhip) dip)
780
+ < the (dhops (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip)" .
781
+ thus "\<pi>\<^sub>5 (the (rt2 nhip dip)) < \<pi>\<^sub>5 (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip))"
782
+ using \<open>dip \<in> vD(rt2 nhip)\<close> by (simp add: proj5_eq_dhops)
783
+ qed
784
+
785
+ lemma nsqn_invalidate:
786
+ assumes "dip \<in> kD(rt)"
787
+ and "\<forall>ip\<in>dom(dests). ip \<in> vD(rt) \<and> the (dests ip) = inc (sqn rt ip)"
788
+ shows "nsqn (invalidate rt dests) dip = nsqn rt dip"
789
+ proof -
790
+ from \<open>dip \<in> kD(rt)\<close> have "dip \<in> kD(invalidate rt dests)" by simp
791
+
792
+ from assms have "rt \<approx>\<^bsub>dip\<^esub> invalidate rt dests"
793
+ by (rule rt_fresh_as_inc_invalidate)
794
+ with \<open>dip \<in> kD(rt)\<close> \<open>dip \<in> kD(invalidate rt dests)\<close> show ?thesis
795
+ by (simp add: kD_nsqn del: invalidate_kD_inv)
796
+ (erule(2) rt_fresh_as_nsqnr)
797
+ qed
798
+
799
+ end
formal/afp/AODV/variants/a_norreqid/A_Global_Invariants.thy ADDED
@@ -0,0 +1,1159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Global_Invariants.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Global invariant proofs over sequential processes"
7
+
8
+ theory A_Global_Invariants
9
+ imports A_Seq_Invariants
10
+ A_Aodv_Predicates
11
+ A_Fresher
12
+ A_Quality_Increases
13
+ AWN.OAWN_Convert
14
+ A_OAodv
15
+ begin
16
+
17
+ lemma other_quality_increases [elim]:
18
+ assumes "other quality_increases I \<sigma> \<sigma>'"
19
+ shows "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
20
+ using assms by (rule, clarsimp) (metis quality_increases_refl)
21
+
22
+ lemma weaken_otherwith [elim]:
23
+ fixes m
24
+ assumes *: "otherwith P I (orecvmsg Q) \<sigma> \<sigma>' a"
25
+ and weakenP: "\<And>\<sigma> m. P \<sigma> m \<Longrightarrow> P' \<sigma> m"
26
+ and weakenQ: "\<And>\<sigma> m. Q \<sigma> m \<Longrightarrow> Q' \<sigma> m"
27
+ shows "otherwith P' I (orecvmsg Q') \<sigma> \<sigma>' a"
28
+ proof
29
+ fix j
30
+ assume "j\<notin>I"
31
+ with * have "P (\<sigma> j) (\<sigma>' j)" by auto
32
+ thus "P' (\<sigma> j) (\<sigma>' j)" by (rule weakenP)
33
+ next
34
+ from * have "orecvmsg Q \<sigma> a" by auto
35
+ thus "orecvmsg Q' \<sigma> a"
36
+ by rule (erule weakenQ)
37
+ qed
38
+
39
+ lemma oreceived_msg_inv:
40
+ assumes other: "\<And>\<sigma> \<sigma>' m. \<lbrakk> P \<sigma> m; other Q {i} \<sigma> \<sigma>' \<rbrakk> \<Longrightarrow> P \<sigma>' m"
41
+ and local: "\<And>\<sigma> m. P \<sigma> m \<Longrightarrow> P (\<sigma>(i := \<sigma> i\<lparr>msg := m\<rparr>)) m"
42
+ shows "opaodv i \<Turnstile> (otherwith Q {i} (orecvmsg P), other Q {i} \<rightarrow>)
43
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l). l \<in> {PAodv-:1} \<longrightarrow> P \<sigma> (msg (\<sigma> i)))"
44
+ proof (inv_cterms, intro impI)
45
+ fix \<sigma> \<sigma>' l
46
+ assume "l = PAodv-:1 \<longrightarrow> P \<sigma> (msg (\<sigma> i))"
47
+ and "l = PAodv-:1"
48
+ and "other Q {i} \<sigma> \<sigma>'"
49
+ from this(1-2) have "P \<sigma> (msg (\<sigma> i))" ..
50
+ hence "P \<sigma>' (msg (\<sigma> i))" using \<open>other Q {i} \<sigma> \<sigma>'\<close>
51
+ by (rule other)
52
+ moreover from \<open>other Q {i} \<sigma> \<sigma>'\<close> have "\<sigma>' i = \<sigma> i" ..
53
+ ultimately show "P \<sigma>' (msg (\<sigma>' i))" by simp
54
+ next
55
+ fix \<sigma> \<sigma>' msg
56
+ assume "otherwith Q {i} (orecvmsg P) \<sigma> \<sigma>' (receive msg)"
57
+ and "\<sigma>' i = \<sigma> i\<lparr>msg := msg\<rparr>"
58
+ from this(1) have "P \<sigma> msg"
59
+ and "\<forall>j. j\<noteq>i \<longrightarrow> Q (\<sigma> j) (\<sigma>' j)" by auto
60
+ from this(1) have "P (\<sigma>(i := \<sigma> i\<lparr>msg := msg\<rparr>)) msg" by (rule local)
61
+ thus "P \<sigma>' msg"
62
+ proof (rule other)
63
+ from \<open>\<sigma>' i = \<sigma> i\<lparr>msg := msg\<rparr>\<close> and \<open>\<forall>j. j\<noteq>i \<longrightarrow> Q (\<sigma> j) (\<sigma>' j)\<close>
64
+ show "other Q {i} (\<sigma>(i := \<sigma> i\<lparr>msg := msg\<rparr>)) \<sigma>'"
65
+ by - (rule otherI, auto)
66
+ qed
67
+ qed
68
+
69
+ text \<open>(Equivalent to) Proposition 7.27\<close>
70
+
71
+ lemma local_quality_increases:
72
+ "paodv i \<TTurnstile>\<^sub>A (recvmsg rreq_rrep_sn \<rightarrow>) onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). quality_increases \<xi> \<xi>')"
73
+ proof (rule step_invariantI)
74
+ fix s a s'
75
+ assume sr: "s \<in> reachable (paodv i) (recvmsg rreq_rrep_sn)"
76
+ and tr: "(s, a, s') \<in> trans (paodv i)"
77
+ and rm: "recvmsg rreq_rrep_sn a"
78
+ from sr have srTT: "s \<in> reachable (paodv i) TT" ..
79
+
80
+ from route_tables_fresher sr tr rm
81
+ have "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). \<forall>dip\<in>kD (rt \<xi>). rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>') (s, a, s')"
82
+ by (rule step_invariantD)
83
+
84
+ moreover from known_destinations_increase srTT tr TT_True
85
+ have "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). kD (rt \<xi>) \<subseteq> kD (rt \<xi>')) (s, a, s')"
86
+ by (rule step_invariantD)
87
+
88
+ moreover from sqns_increase srTT tr TT_True
89
+ have "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). \<forall>ip. sqn (rt \<xi>) ip \<le> sqn (rt \<xi>') ip) (s, a, s')"
90
+ by (rule step_invariantD)
91
+
92
+ ultimately show "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). quality_increases \<xi> \<xi>') (s, a, s')"
93
+ unfolding onll_def by auto
94
+ qed
95
+
96
+ lemmas olocal_quality_increases =
97
+ open_seq_step_invariant [OF local_quality_increases initiali_aodv oaodv_trans aodv_trans,
98
+ simplified seqll_onll_swap]
99
+
100
+ lemma oquality_increases:
101
+ "opaodv i \<Turnstile>\<^sub>A (otherwith quality_increases {i} (orecvmsg (\<lambda>_. rreq_rrep_sn)),
102
+ other quality_increases {i} \<rightarrow>)
103
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), _, (\<sigma>', _)). \<forall>j. quality_increases (\<sigma> j) (\<sigma>' j))"
104
+ (is "_ \<Turnstile>\<^sub>A (?S, _ \<rightarrow>) _")
105
+ proof (rule onll_ostep_invariantI, simp)
106
+ fix \<sigma> p l a \<sigma>' p' l'
107
+ assume or: "(\<sigma>, p) \<in> oreachable (opaodv i) ?S (other quality_increases {i})"
108
+ and ll: "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
109
+ and "?S \<sigma> \<sigma>' a"
110
+ and tr: "((\<sigma>, p), a, (\<sigma>', p')) \<in> oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
111
+ and ll': "l' \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'"
112
+ from this(1-3) have "orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma> a"
113
+ by (auto dest!: oreachable_weakenE [where QS="act (recvmsg rreq_rrep_sn)"
114
+ and QU="other quality_increases {i}"]
115
+ otherwith_actionD)
116
+ with or have orw: "(\<sigma>, p) \<in> oreachable (opaodv i) (act (recvmsg rreq_rrep_sn))
117
+ (other quality_increases {i})"
118
+ by - (erule oreachable_weakenE, auto)
119
+ with tr ll ll' and \<open>orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma> a\<close> have "quality_increases (\<sigma> i) (\<sigma>' i)"
120
+ by - (drule onll_ostep_invariantD [OF olocal_quality_increases], auto simp: seqll_def)
121
+ with \<open>?S \<sigma> \<sigma>' a\<close> show "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
122
+ by (auto dest!: otherwith_syncD)
123
+ qed
124
+
125
+ lemma rreq_rrep_nsqn_fresh_any_step_invariant:
126
+ "opaodv i \<Turnstile>\<^sub>A (act (recvmsg rreq_rrep_sn), other A {i} \<rightarrow>)
127
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), a, _). anycast (msg_fresh \<sigma>) a)"
128
+ proof (rule ostep_invariantI, simp del: act_simp)
129
+ fix \<sigma> p a \<sigma>' p'
130
+ assume or: "(\<sigma>, p) \<in> oreachable (opaodv i) (act (recvmsg rreq_rrep_sn)) (other A {i})"
131
+ and "((\<sigma>, p), a, (\<sigma>', p')) \<in> oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
132
+ and recv: "act (recvmsg rreq_rrep_sn) \<sigma> \<sigma>' a"
133
+ obtain l l' where "l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p" and "l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'"
134
+ by (metis aodv_ex_label)
135
+ from \<open>((\<sigma>, p), a, (\<sigma>', p')) \<in> oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i\<close>
136
+ have tr: "((\<sigma>, p), a, (\<sigma>', p')) \<in> trans (opaodv i)" by simp
137
+
138
+ have "anycast (rreq_rrep_fresh (rt (\<sigma> i))) a"
139
+ proof -
140
+ have "opaodv i \<Turnstile>\<^sub>A (act (recvmsg rreq_rrep_sn), other A {i} \<rightarrow>)
141
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (rreq_rrep_fresh (rt \<xi>)) a))"
142
+ by (rule ostep_invariant_weakenE [OF
143
+ open_seq_step_invariant [OF rreq_rrep_fresh_any_step_invariant initiali_aodv,
144
+ simplified seqll_onll_swap]]) auto
145
+ hence "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (rreq_rrep_fresh (rt \<xi>)) a))
146
+ ((\<sigma>, p), a, (\<sigma>', p'))"
147
+ using or tr recv by - (erule(4) ostep_invariantE)
148
+ thus ?thesis
149
+ using \<open>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and \<open>l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'\<close> by auto
150
+ qed
151
+
152
+ moreover have "anycast (rerr_invalid (rt (\<sigma> i))) a"
153
+ proof -
154
+ have "opaodv i \<Turnstile>\<^sub>A (act (recvmsg rreq_rrep_sn), other A {i} \<rightarrow>)
155
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (rerr_invalid (rt \<xi>)) a))"
156
+ by (rule ostep_invariant_weakenE [OF
157
+ open_seq_step_invariant [OF rerr_invalid_any_step_invariant initiali_aodv,
158
+ simplified seqll_onll_swap]]) auto
159
+ hence "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (rerr_invalid (rt \<xi>)) a))
160
+ ((\<sigma>, p), a, (\<sigma>', p'))"
161
+ using or tr recv by - (erule(4) ostep_invariantE)
162
+ thus ?thesis
163
+ using \<open>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and \<open>l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'\<close> by auto
164
+ qed
165
+
166
+ moreover have "anycast rreq_rrep_sn a"
167
+ proof -
168
+ from or tr recv
169
+ have "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>(_, a, _). anycast rreq_rrep_sn a)) ((\<sigma>, p), a, (\<sigma>', p'))"
170
+ by (rule ostep_invariantE [OF
171
+ open_seq_step_invariant [OF rreq_rrep_sn_any_step_invariant initiali_aodv
172
+ oaodv_trans aodv_trans,
173
+ simplified seqll_onll_swap]])
174
+ thus ?thesis
175
+ using \<open>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and \<open>l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'\<close> by auto
176
+ qed
177
+
178
+ moreover have "anycast (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m = i) a"
179
+ proof -
180
+ have "opaodv i \<Turnstile>\<^sub>A (act (recvmsg rreq_rrep_sn), other A {i} \<rightarrow>)
181
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seqll i (\<lambda>((\<xi>, _), a, _). anycast (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m = i) a))"
182
+ by (rule ostep_invariant_weakenE [OF
183
+ open_seq_step_invariant [OF sender_ip_valid initiali_aodv,
184
+ simplified seqll_onll_swap]]) auto
185
+ thus ?thesis using or tr recv \<open>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and \<open>l'\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p'\<close>
186
+ by - (drule(3) onll_ostep_invariantD, auto)
187
+ qed
188
+
189
+ ultimately have "anycast (msg_fresh \<sigma>) a"
190
+ by (simp_all add: anycast_def
191
+ del: msg_fresh
192
+ split: seq_action.split_asm msg.split_asm) simp_all
193
+ thus "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), a, _). anycast (msg_fresh \<sigma>) a) ((\<sigma>, p), a, (\<sigma>', p'))"
194
+ by auto
195
+ qed
196
+
197
+ lemma oreceived_rreq_rrep_nsqn_fresh_inv:
198
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
199
+ other quality_increases {i} \<rightarrow>)
200
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l). l \<in> {PAodv-:1} \<longrightarrow> msg_fresh \<sigma> (msg (\<sigma> i)))"
201
+ proof (rule oreceived_msg_inv)
202
+ fix \<sigma> \<sigma>' m
203
+ assume *: "msg_fresh \<sigma> m"
204
+ and "other quality_increases {i} \<sigma> \<sigma>'"
205
+ from this(2) have "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)" ..
206
+ thus "msg_fresh \<sigma>' m" using * ..
207
+ next
208
+ fix \<sigma> m
209
+ assume "msg_fresh \<sigma> m"
210
+ thus "msg_fresh (\<sigma>(i := \<sigma> i\<lparr>msg := m\<rparr>)) m"
211
+ proof (cases m)
212
+ fix dests sip
213
+ assume "m = Rerr dests sip"
214
+ with \<open>msg_fresh \<sigma> m\<close> show ?thesis by auto
215
+ qed auto
216
+ qed
217
+
218
+ lemma oquality_increases_nsqn_fresh:
219
+ "opaodv i \<Turnstile>\<^sub>A (otherwith quality_increases {i} (orecvmsg msg_fresh),
220
+ other quality_increases {i} \<rightarrow>)
221
+ onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), _, (\<sigma>', _)). \<forall>j. quality_increases (\<sigma> j) (\<sigma>' j))"
222
+ by (rule ostep_invariant_weakenE [OF oquality_increases]) auto
223
+
224
+ lemma oosn_rreq:
225
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
226
+ other quality_increases {i} \<rightarrow>)
227
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seql i (\<lambda>(\<xi>, l). l \<in> {PAodv-:4, PAodv-:5} \<union> {PRreq-:n |n. True} \<longrightarrow> 1 \<le> osn \<xi>))"
228
+ by (rule oinvariant_weakenE [OF open_seq_invariant [OF osn_rreq initiali_aodv]])
229
+ (auto simp: seql_onl_swap)
230
+
231
+ lemma rreq_sip:
232
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
233
+ other quality_increases {i} \<rightarrow>)
234
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l).
235
+ (l \<in> {PAodv-:4, PAodv-:5, PRreq-:0, PRreq-:2} \<and> sip (\<sigma> i) \<noteq> oip (\<sigma> i))
236
+ \<longrightarrow> oip (\<sigma> i) \<in> kD(rt (\<sigma> (sip (\<sigma> i))))
237
+ \<and> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i)) \<ge> osn (\<sigma> i)
238
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i)) = osn (\<sigma> i)
239
+ \<longrightarrow> (hops (\<sigma> i) \<ge> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i)))
240
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i))) = inv)))"
241
+ (is "_ \<Turnstile> (?S, ?U \<rightarrow>) _")
242
+ proof (inv_cterms inv add: oseq_step_invariant_sterms [OF oquality_increases_nsqn_fresh
243
+ aodv_wf oaodv_trans]
244
+ onl_oinvariant_sterms [OF aodv_wf oreceived_rreq_rrep_nsqn_fresh_inv]
245
+ onl_oinvariant_sterms [OF aodv_wf oosn_rreq]
246
+ simp add: seqlsimp
247
+ simp del: One_nat_def, rule impI)
248
+ fix \<sigma> \<sigma>' p l
249
+ assume "(\<sigma>, p) \<in> oreachable (opaodv i) ?S ?U"
250
+ and "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
251
+ and pre:
252
+ "(l = PAodv-:4 \<or> l = PAodv-:5 \<or> l = PRreq-:0 \<or> l = PRreq-:2) \<and> sip (\<sigma> i) \<noteq> oip (\<sigma> i)
253
+ \<longrightarrow> oip (\<sigma> i) \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
254
+ \<and> osn (\<sigma> i) \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i))
255
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i)) = osn (\<sigma> i)
256
+ \<longrightarrow> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i))) \<le> hops (\<sigma> i)
257
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma> i))) = inv)"
258
+ and "other quality_increases {i} \<sigma> \<sigma>'"
259
+ and hyp: "(l=PAodv-:4 \<or> l=PAodv-:5 \<or> l=PRreq-:0 \<or> l=PRreq-:2) \<and> sip (\<sigma>' i) \<noteq> oip (\<sigma>' i)"
260
+ (is "?labels \<and> sip (\<sigma>' i) \<noteq> oip (\<sigma>' i)")
261
+ from this(4) have "\<sigma>' i = \<sigma> i" ..
262
+ with hyp have hyp': "?labels \<and> sip (\<sigma> i) \<noteq> oip (\<sigma> i)" by simp
263
+ show "oip (\<sigma>' i) \<in> kD (rt (\<sigma>' (sip (\<sigma>' i))))
264
+ \<and> osn (\<sigma>' i) \<le> nsqn (rt (\<sigma>' (sip (\<sigma>' i)))) (oip (\<sigma>' i))
265
+ \<and> (nsqn (rt (\<sigma>' (sip (\<sigma>' i)))) (oip (\<sigma>' i)) = osn (\<sigma>' i)
266
+ \<longrightarrow> the (dhops (rt (\<sigma>' (sip (\<sigma>' i)))) (oip (\<sigma>' i))) \<le> hops (\<sigma>' i)
267
+ \<or> the (flag (rt (\<sigma>' (sip (\<sigma>' i)))) (oip (\<sigma>' i))) = inv)"
268
+ proof (cases "sip (\<sigma> i) = i")
269
+ assume "sip (\<sigma> i) \<noteq> i"
270
+ from \<open>other quality_increases {i} \<sigma> \<sigma>'\<close>
271
+ have "quality_increases (\<sigma> (sip (\<sigma> i))) (\<sigma>' (sip (\<sigma>' i)))"
272
+ by (rule otherE) (clarsimp simp: \<open>sip (\<sigma> i) \<noteq> i\<close>)
273
+ moreover from \<open>(\<sigma>, p) \<in> oreachable (opaodv i) ?S ?U\<close> \<open>l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and hyp
274
+ have "1 \<le> osn (\<sigma>' i)"
275
+ by (auto dest!: onl_oinvariant_weakenD [OF oosn_rreq]
276
+ simp add: seqlsimp \<open>\<sigma>' i = \<sigma> i\<close>)
277
+ moreover from \<open>sip (\<sigma> i) \<noteq> i\<close> hyp' and pre
278
+ have "oip (\<sigma>' i) \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
279
+ \<and> osn (\<sigma>' i) \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma>' i))
280
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma>' i)) = osn (\<sigma>' i)
281
+ \<longrightarrow> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma>' i))) \<le> hops (\<sigma>' i)
282
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (oip (\<sigma>' i))) = inv)"
283
+ by (auto simp: \<open>\<sigma>' i = \<sigma> i\<close>)
284
+ ultimately show ?thesis
285
+ by (rule quality_increases_rreq_rrep_props)
286
+ next
287
+ assume "sip (\<sigma> i) = i" thus ?thesis
288
+ using \<open>\<sigma>' i = \<sigma> i\<close> hyp and pre by auto
289
+ qed
290
+ qed (auto elim!: quality_increases_rreq_rrep_props')
291
+
292
+ lemma odsn_rrep:
293
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
294
+ other quality_increases {i} \<rightarrow>)
295
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (seql i (\<lambda>(\<xi>, l). l \<in> {PAodv-:6, PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow> 1 \<le> dsn \<xi>))"
296
+ by (rule oinvariant_weakenE [OF open_seq_invariant [OF dsn_rrep initiali_aodv]])
297
+ (auto simp: seql_onl_swap)
298
+
299
+ lemma rrep_sip:
300
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
301
+ other quality_increases {i} \<rightarrow>)
302
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l).
303
+ (l \<in> {PAodv-:6, PAodv-:7, PRrep-:0, PRrep-:1} \<and> sip (\<sigma> i) \<noteq> dip (\<sigma> i))
304
+ \<longrightarrow> dip (\<sigma> i) \<in> kD(rt (\<sigma> (sip (\<sigma> i))))
305
+ \<and> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i)) \<ge> dsn (\<sigma> i)
306
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i)) = dsn (\<sigma> i)
307
+ \<longrightarrow> (hops (\<sigma> i) \<ge> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i)))
308
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i))) = inv)))"
309
+ (is "_ \<Turnstile> (?S, ?U \<rightarrow>) _")
310
+ proof (inv_cterms inv add: oseq_step_invariant_sterms [OF oquality_increases_nsqn_fresh aodv_wf
311
+ oaodv_trans]
312
+ onl_oinvariant_sterms [OF aodv_wf oreceived_rreq_rrep_nsqn_fresh_inv]
313
+ onl_oinvariant_sterms [OF aodv_wf odsn_rrep]
314
+ simp del: One_nat_def, rule impI)
315
+ fix \<sigma> \<sigma>' p l
316
+ assume "(\<sigma>, p) \<in> oreachable (opaodv i) ?S ?U"
317
+ and "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
318
+ and pre:
319
+ "(l = PAodv-:6 \<or> l = PAodv-:7 \<or> l = PRrep-:0 \<or> l = PRrep-:1) \<and> sip (\<sigma> i) \<noteq> dip (\<sigma> i)
320
+ \<longrightarrow> dip (\<sigma> i) \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
321
+ \<and> dsn (\<sigma> i) \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i))
322
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i)) = dsn (\<sigma> i)
323
+ \<longrightarrow> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i))) \<le> hops (\<sigma> i)
324
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma> i))) = inv)"
325
+ and "other quality_increases {i} \<sigma> \<sigma>'"
326
+ and hyp: "(l=PAodv-:6 \<or> l=PAodv-:7 \<or> l=PRrep-:0 \<or> l=PRrep-:1) \<and> sip (\<sigma>' i) \<noteq> dip (\<sigma>' i)"
327
+ (is "?labels \<and> sip (\<sigma>' i) \<noteq> dip (\<sigma>' i)")
328
+ from this(4) have "\<sigma>' i = \<sigma> i" ..
329
+ with hyp have hyp': "?labels \<and> sip (\<sigma> i) \<noteq> dip (\<sigma> i)" by simp
330
+ show "dip (\<sigma>' i) \<in> kD (rt (\<sigma>' (sip (\<sigma>' i))))
331
+ \<and> dsn (\<sigma>' i) \<le> nsqn (rt (\<sigma>' (sip (\<sigma>' i)))) (dip (\<sigma>' i))
332
+ \<and> (nsqn (rt (\<sigma>' (sip (\<sigma>' i)))) (dip (\<sigma>' i)) = dsn (\<sigma>' i)
333
+ \<longrightarrow> the (dhops (rt (\<sigma>' (sip (\<sigma>' i)))) (dip (\<sigma>' i))) \<le> hops (\<sigma>' i)
334
+ \<or> the (flag (rt (\<sigma>' (sip (\<sigma>' i)))) (dip (\<sigma>' i))) = inv)"
335
+ proof (cases "sip (\<sigma> i) = i")
336
+ assume "sip (\<sigma> i) \<noteq> i"
337
+ from \<open>other quality_increases {i} \<sigma> \<sigma>'\<close>
338
+ have "quality_increases (\<sigma> (sip (\<sigma> i))) (\<sigma>' (sip (\<sigma>' i)))"
339
+ by (rule otherE) (clarsimp simp: \<open>sip (\<sigma> i) \<noteq> i\<close>)
340
+ moreover from \<open>(\<sigma>, p) \<in> oreachable (opaodv i) ?S ?U\<close> \<open>l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and hyp
341
+ have "1 \<le> dsn (\<sigma>' i)"
342
+ by (auto dest!: onl_oinvariant_weakenD [OF odsn_rrep]
343
+ simp add: seqlsimp \<open>\<sigma>' i = \<sigma> i\<close>)
344
+ moreover from \<open>sip (\<sigma> i) \<noteq> i\<close> hyp' and pre
345
+ have "dip (\<sigma>' i) \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
346
+ \<and> dsn (\<sigma>' i) \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma>' i))
347
+ \<and> (nsqn (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma>' i)) = dsn (\<sigma>' i)
348
+ \<longrightarrow> the (dhops (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma>' i))) \<le> hops (\<sigma>' i)
349
+ \<or> the (flag (rt (\<sigma> (sip (\<sigma> i)))) (dip (\<sigma>' i))) = inv)"
350
+ by (auto simp: \<open>\<sigma>' i = \<sigma> i\<close>)
351
+ ultimately show ?thesis
352
+ by (rule quality_increases_rreq_rrep_props)
353
+ next
354
+ assume "sip (\<sigma> i) = i" thus ?thesis
355
+ using \<open>\<sigma>' i = \<sigma> i\<close> hyp and pre by auto
356
+ qed
357
+ qed (auto simp add: seqlsimp elim!: quality_increases_rreq_rrep_props')
358
+
359
+ lemma rerr_sip:
360
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
361
+ other quality_increases {i} \<rightarrow>)
362
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, l).
363
+ l \<in> {PAodv-:8, PAodv-:9, PRerr-:0, PRerr-:1}
364
+ \<longrightarrow> (\<forall>ripc\<in>dom(dests (\<sigma> i)). ripc\<in>kD(rt (\<sigma> (sip (\<sigma> i)))) \<and>
365
+ the (dests (\<sigma> i) ripc) - 1 \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) ripc))"
366
+ (is "_ \<Turnstile> (?S, ?U \<rightarrow>) _")
367
+ proof -
368
+ { fix dests rip sip rsn and \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
369
+ assume qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
370
+ and *: "\<forall>rip\<in>dom dests. rip \<in> kD (rt (\<sigma> sip))
371
+ \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
372
+ and "dests rip = Some rsn"
373
+ from this(3) have "rip\<in>dom dests" by auto
374
+ with * and \<open>dests rip = Some rsn\<close> have "rip\<in>kD(rt (\<sigma> sip))"
375
+ and "rsn - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
376
+ by (auto dest!: bspec)
377
+ from qinc have "quality_increases (\<sigma> sip) (\<sigma>' sip)" ..
378
+ have "rip \<in> kD(rt (\<sigma>' sip)) \<and> rsn - 1 \<le> nsqn (rt (\<sigma>' sip)) rip"
379
+ proof
380
+ from \<open>rip\<in>kD(rt (\<sigma> sip))\<close> and \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
381
+ show "rip \<in> kD(rt (\<sigma>' sip))" ..
382
+ next
383
+ from \<open>rip\<in>kD(rt (\<sigma> sip))\<close> and \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
384
+ have "nsqn (rt (\<sigma> sip)) rip \<le> nsqn (rt (\<sigma>' sip)) rip" ..
385
+ with \<open>rsn - 1 \<le> nsqn (rt (\<sigma> sip)) rip\<close> show "rsn - 1 \<le> nsqn (rt (\<sigma>' sip)) rip"
386
+ by (rule le_trans)
387
+ qed
388
+ } note partial = this
389
+
390
+ show ?thesis
391
+ by (inv_cterms inv add: oseq_step_invariant_sterms [OF oquality_increases_nsqn_fresh aodv_wf
392
+ oaodv_trans]
393
+ onl_oinvariant_sterms [OF aodv_wf oreceived_rreq_rrep_nsqn_fresh_inv]
394
+ other_quality_increases other_localD
395
+ simp del: One_nat_def, intro conjI)
396
+ (clarsimp simp del: One_nat_def split: if_split_asm option.split_asm, erule(2) partial)+
397
+ qed
398
+
399
+ lemma prerr_guard: "paodv i \<TTurnstile>
400
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l = PRerr-:1
401
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>vD(rt \<xi>)
402
+ \<and> the (nhop (rt \<xi>) ip) = sip \<xi>
403
+ \<and> sqn (rt \<xi>) ip < the (dests \<xi> ip))))"
404
+ by (inv_cterms) (clarsimp split: option.split_asm if_split_asm)
405
+
406
+ lemmas oaddpreRT_welldefined =
407
+ open_seq_invariant [OF addpreRT_welldefined initiali_aodv oaodv_trans aodv_trans,
408
+ simplified seql_onl_swap,
409
+ THEN oinvariant_anyact]
410
+
411
+ lemmas odests_vD_inc_sqn =
412
+ open_seq_invariant [OF dests_vD_inc_sqn initiali_aodv oaodv_trans aodv_trans,
413
+ simplified seql_onl_swap,
414
+ THEN oinvariant_anyact]
415
+
416
+ lemmas oprerr_guard =
417
+ open_seq_invariant [OF prerr_guard initiali_aodv oaodv_trans aodv_trans,
418
+ simplified seql_onl_swap,
419
+ THEN oinvariant_anyact]
420
+
421
+ text \<open>Proposition 7.28\<close>
422
+
423
+ lemma seq_compare_next_hop':
424
+ "opaodv i \<Turnstile> (otherwith quality_increases {i} (orecvmsg msg_fresh),
425
+ other quality_increases {i} \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, _).
426
+ \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
427
+ in dip \<in> kD(rt (\<sigma> i)) \<and> nhip \<noteq> dip \<longrightarrow>
428
+ dip \<in> kD(rt (\<sigma> nhip)) \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> nhip)) dip)"
429
+ (is "_ \<Turnstile> (?S, ?U \<rightarrow>) _")
430
+ proof -
431
+
432
+ { fix nhop and \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
433
+ assume pre: "\<forall>dip\<in>kD(rt (\<sigma> i)). nhop dip \<noteq> dip \<longrightarrow>
434
+ dip\<in>kD(rt (\<sigma> (nhop dip))) \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (nhop dip))) dip"
435
+ and qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
436
+ have "\<forall>dip\<in>kD(rt (\<sigma> i)). nhop dip \<noteq> dip \<longrightarrow>
437
+ dip\<in>kD(rt (\<sigma>' (nhop dip))) \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
438
+ proof (intro ballI impI)
439
+ fix dip
440
+ assume "dip\<in>kD(rt (\<sigma> i))"
441
+ and "nhop dip \<noteq> dip"
442
+ with pre have "dip\<in>kD(rt (\<sigma> (nhop dip)))"
443
+ and "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (nhop dip))) dip"
444
+ by auto
445
+ from qinc have qinc_nhop: "quality_increases (\<sigma> (nhop dip)) (\<sigma>' (nhop dip))" ..
446
+ with \<open>dip\<in>kD(rt (\<sigma> (nhop dip)))\<close> have "dip\<in>kD (rt (\<sigma>' (nhop dip)))" ..
447
+
448
+ moreover have "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
449
+ proof -
450
+ from \<open>dip\<in>kD(rt (\<sigma> (nhop dip)))\<close> qinc_nhop
451
+ have "nsqn (rt (\<sigma> (nhop dip))) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip" ..
452
+ with \<open>nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (nhop dip))) dip\<close> show ?thesis
453
+ by simp
454
+ qed
455
+
456
+ ultimately show "dip\<in>kD(rt (\<sigma>' (nhop dip)))
457
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip" ..
458
+ qed
459
+ } note basic = this
460
+
461
+ { fix nhop and \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
462
+ assume pre: "\<forall>dip\<in>kD(rt (\<sigma> i)). nhop dip \<noteq> dip \<longrightarrow> dip\<in>kD(rt (\<sigma> (nhop dip)))
463
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (nhop dip))) dip"
464
+ and ndest: "\<forall>ripc\<in>dom (dests (\<sigma> i)). ripc \<in> kD (rt (\<sigma> (sip (\<sigma> i))))
465
+ \<and> the (dests (\<sigma> i) ripc) - 1 \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) ripc"
466
+ and issip: "\<forall>ip\<in>dom (dests (\<sigma> i)). nhop ip = sip (\<sigma> i)"
467
+ and qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
468
+ have "\<forall>dip\<in>kD(rt (\<sigma> i)). nhop dip \<noteq> dip \<longrightarrow> dip \<in> kD (rt (\<sigma>' (nhop dip)))
469
+ \<and> nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
470
+ proof (intro ballI impI)
471
+ fix dip
472
+ assume "dip\<in>kD(rt (\<sigma> i))"
473
+ and "nhop dip \<noteq> dip"
474
+ with pre and qinc have "dip\<in>kD(rt (\<sigma>' (nhop dip)))"
475
+ and "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
476
+ by (auto dest!: basic)
477
+
478
+ have "nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
479
+ proof (cases "dip\<in>dom (dests (\<sigma> i))")
480
+ assume "dip\<in>dom (dests (\<sigma> i))"
481
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> obtain dsn where "dests (\<sigma> i) dip = Some dsn"
482
+ by auto
483
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> have "nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip = dsn - 1"
484
+ by (rule nsqn_invalidate_eq)
485
+ moreover have "dsn - 1 \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
486
+ proof -
487
+ from \<open>dests (\<sigma> i) dip = Some dsn\<close> have "the (dests (\<sigma> i) dip) = dsn" by simp
488
+ with ndest and \<open>dip\<in>dom (dests (\<sigma> i))\<close> have "dip \<in> kD (rt (\<sigma> (sip (\<sigma> i))))"
489
+ "dsn - 1 \<le> nsqn (rt (\<sigma> (sip (\<sigma> i)))) dip"
490
+ by auto
491
+ moreover from issip and \<open>dip\<in>dom (dests (\<sigma> i))\<close> have "nhop dip = sip (\<sigma> i)" ..
492
+ ultimately have "dip \<in> kD (rt (\<sigma> (nhop dip)))"
493
+ and "dsn - 1 \<le> nsqn (rt (\<sigma> (nhop dip))) dip" by auto
494
+ with qinc show "dsn - 1 \<le> nsqn (rt (\<sigma>' (nhop dip))) dip"
495
+ by simp (metis kD_nsqn_quality_increases_trans)
496
+ qed
497
+ ultimately show ?thesis by simp
498
+ next
499
+ assume "dip \<notin> dom (dests (\<sigma> i))"
500
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close>
501
+ have "nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip = nsqn (rt (\<sigma> i)) dip"
502
+ by (rule nsqn_invalidate_other)
503
+ with \<open>nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip\<close> show ?thesis by simp
504
+ qed
505
+ with \<open>dip\<in>kD(rt (\<sigma>' (nhop dip)))\<close>
506
+ show "dip \<in> kD (rt (\<sigma>' (nhop dip)))
507
+ \<and> nsqn (invalidate (rt (\<sigma> i)) (dests (\<sigma> i))) dip \<le> nsqn (rt (\<sigma>' (nhop dip))) dip" ..
508
+ qed
509
+ } note basic_prerr = this
510
+
511
+ { fix \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
512
+ assume a1: "\<forall>dip\<in>kD(rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
513
+ \<longrightarrow> dip\<in>kD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
514
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))) dip"
515
+ and a2: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
516
+ have "\<forall>dip\<in>kD(rt (\<sigma> i)).
517
+ the (nhop (update (rt (\<sigma> i)) (sip (\<sigma> i)) (0, unk, val, Suc 0, sip (\<sigma> i), {})) dip) \<noteq> dip \<longrightarrow>
518
+ dip\<in>kD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) (sip (\<sigma> i))
519
+ (0, unk, val, Suc 0, sip (\<sigma> i), {}))
520
+ dip)))) \<and>
521
+ nsqn (update (rt (\<sigma> i)) (sip (\<sigma> i)) (0, unk, val, Suc 0, sip (\<sigma> i), {})) dip
522
+ \<le> nsqn (rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) (sip (\<sigma> i))
523
+ (0, unk, val, Suc 0, sip (\<sigma> i), {}))
524
+ dip))))
525
+ dip" (is "\<forall>dip\<in>kD(rt (\<sigma> i)). ?P dip")
526
+ proof
527
+ fix dip
528
+ assume "dip\<in>kD(rt (\<sigma> i))"
529
+ with a1 and a2
530
+ have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip \<longrightarrow> dip\<in>kD(rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))
531
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))) dip"
532
+ by - (drule(1) basic, auto)
533
+ thus "?P dip" by (cases "dip = sip (\<sigma> i)") auto
534
+ qed
535
+ } note nhop_update_sip = this
536
+
537
+ { fix \<sigma> \<sigma>' oip sip osn hops
538
+ assume pre: "\<forall>dip\<in>kD (rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
539
+ \<longrightarrow> dip\<in>kD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
540
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))) dip"
541
+ and qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
542
+ and *: "sip \<noteq> oip \<longrightarrow> oip\<in>kD(rt (\<sigma> sip))
543
+ \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
544
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
545
+ \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops
546
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv)"
547
+ from pre and qinc
548
+ have pre': "\<forall>dip\<in>kD (rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
549
+ \<longrightarrow> dip\<in>kD(rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))
550
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))) dip"
551
+ by (rule basic)
552
+ have "(the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) oip) \<noteq> oip
553
+ \<longrightarrow> oip\<in>kD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
554
+ (osn, kno, val, Suc hops, sip, {})) oip))))
555
+ \<and> nsqn (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) oip
556
+ \<le> nsqn (rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
557
+ (osn, kno, val, Suc hops, sip, {})) oip)))) oip)"
558
+ (is "?nhop_not_oip \<longrightarrow> ?oip_in_kD \<and> ?nsqn_le_nsqn")
559
+ proof (rule, split update_rt_split_asm)
560
+ assume "rt (\<sigma> i) = update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})"
561
+ and "the (nhop (rt (\<sigma> i)) oip) \<noteq> oip"
562
+ with pre' show "?oip_in_kD \<and> ?nsqn_le_nsqn" by auto
563
+ next
564
+ assume rtnot: "rt (\<sigma> i) \<noteq> update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})"
565
+ and notoip: ?nhop_not_oip
566
+ with * qinc have ?oip_in_kD
567
+ by (clarsimp elim!: kD_quality_increases)
568
+ moreover with * pre qinc rtnot notoip have ?nsqn_le_nsqn
569
+ by simp (metis kD_nsqn_quality_increases_trans)
570
+ ultimately show "?oip_in_kD \<and> ?nsqn_le_nsqn" ..
571
+ qed
572
+ } note update1 = this
573
+
574
+ { fix \<sigma> \<sigma>' oip sip osn hops
575
+ assume pre: "\<forall>dip\<in>kD (rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
576
+ \<longrightarrow> dip\<in>kD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
577
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))) dip"
578
+ and qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
579
+ and *: "sip \<noteq> oip \<longrightarrow> oip\<in>kD(rt (\<sigma> sip))
580
+ \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
581
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
582
+ \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops
583
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv)"
584
+ from pre and qinc
585
+ have pre': "\<forall>dip\<in>kD (rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
586
+ \<longrightarrow> dip\<in>kD(rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))
587
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))) dip"
588
+ by (rule basic)
589
+ have "\<forall>dip\<in>kD(rt (\<sigma> i)).
590
+ the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip) \<noteq> dip
591
+ \<longrightarrow> dip\<in>kD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
592
+ (osn, kno, val, Suc hops, sip, {})) dip))))
593
+ \<and> nsqn (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip
594
+ \<le> nsqn (rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
595
+ (osn, kno, val, Suc hops, sip, {})) dip)))) dip"
596
+ (is "\<forall>dip\<in>kD(rt (\<sigma> i)). _ \<longrightarrow> ?dip_in_kD dip \<and> ?nsqn_le_nsqn dip")
597
+ proof (intro ballI impI, split update_rt_split_asm)
598
+ fix dip
599
+ assume "dip\<in>kD(rt (\<sigma> i))"
600
+ and "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip"
601
+ and "rt (\<sigma> i) = update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})"
602
+ with pre' show "?dip_in_kD dip \<and> ?nsqn_le_nsqn dip" by simp
603
+ next
604
+ fix dip
605
+ assume "dip\<in>kD(rt (\<sigma> i))"
606
+ and notdip: "the (nhop (update (rt (\<sigma> i)) oip
607
+ (osn, kno, val, Suc hops, sip, {})) dip) \<noteq> dip"
608
+ and rtnot: "rt (\<sigma> i) \<noteq> update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})"
609
+ show "?dip_in_kD dip \<and> ?nsqn_le_nsqn dip"
610
+ proof (cases "dip = oip")
611
+ assume "dip \<noteq> oip"
612
+ with pre' \<open>dip\<in>kD(rt (\<sigma> i))\<close> notdip
613
+ show ?thesis by clarsimp
614
+ next
615
+ assume "dip = oip"
616
+ with rtnot qinc \<open>dip\<in>kD(rt (\<sigma> i))\<close> notdip *
617
+ have "?dip_in_kD dip"
618
+ by simp (metis kD_quality_increases)
619
+ moreover from \<open>dip = oip\<close> rtnot qinc \<open>dip\<in>kD(rt (\<sigma> i))\<close> notdip *
620
+ have "?nsqn_le_nsqn dip" by simp (metis kD_nsqn_quality_increases_trans)
621
+ ultimately show ?thesis ..
622
+ qed
623
+ qed
624
+ } note update2 = this
625
+
626
+ have "opaodv i \<Turnstile> (?S, ?U \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, _).
627
+ \<forall>dip \<in> kD(rt (\<sigma> i)). the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
628
+ \<longrightarrow> dip \<in> kD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
629
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))) dip)"
630
+ by (inv_cterms inv add: oseq_step_invariant_sterms [OF oquality_increases_nsqn_fresh aodv_wf
631
+ oaodv_trans]
632
+ onl_oinvariant_sterms [OF aodv_wf oaddpreRT_welldefined]
633
+ onl_oinvariant_sterms [OF aodv_wf odests_vD_inc_sqn]
634
+ onl_oinvariant_sterms [OF aodv_wf oprerr_guard]
635
+ onl_oinvariant_sterms [OF aodv_wf rreq_sip]
636
+ onl_oinvariant_sterms [OF aodv_wf rrep_sip]
637
+ onl_oinvariant_sterms [OF aodv_wf rerr_sip]
638
+ other_quality_increases
639
+ other_localD
640
+ solve: basic basic_prerr
641
+ simp add: seqlsimp nsqn_invalidate nhop_update_sip
642
+ simp del: One_nat_def)
643
+ (rule conjI, erule(2) update1, erule(2) update2)+
644
+
645
+ thus ?thesis unfolding Let_def by auto
646
+ qed
647
+
648
+ text \<open>Proposition 7.30\<close>
649
+
650
+ lemmas okD_unk_or_atleast_one =
651
+ open_seq_invariant [OF kD_unk_or_atleast_one initiali_aodv,
652
+ simplified seql_onl_swap]
653
+
654
+ lemmas ozero_seq_unk_hops_one =
655
+ open_seq_invariant [OF zero_seq_unk_hops_one initiali_aodv,
656
+ simplified seql_onl_swap]
657
+
658
+ lemma oreachable_fresh_okD_unk_or_atleast_one:
659
+ fixes dip
660
+ assumes "(\<sigma>, p) \<in> oreachable (opaodv i)
661
+ (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m
662
+ \<and> msg_zhops m)))
663
+ (other quality_increases {i})"
664
+ and "dip\<in>kD(rt (\<sigma> i))"
665
+ shows "\<pi>\<^sub>3(the (rt (\<sigma> i) dip)) = unk \<or> 1 \<le> \<pi>\<^sub>2(the (rt (\<sigma> i) dip))"
666
+ (is "?P dip")
667
+ proof -
668
+ have "\<exists>l. l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p" by (metis aodv_ex_label)
669
+ with assms(1) have "\<forall>dip\<in>kD (rt (\<sigma> i)). ?P dip"
670
+ by - (drule oinvariant_weakenD [OF okD_unk_or_atleast_one [OF oaodv_trans aodv_trans]],
671
+ auto dest!: otherwith_actionD onlD simp: seqlsimp)
672
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> show ?thesis by simp
673
+ qed
674
+
675
+ lemma oreachable_fresh_ozero_seq_unk_hops_one:
676
+ fixes dip
677
+ assumes "(\<sigma>, p) \<in> oreachable (opaodv i)
678
+ (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m
679
+ \<and> msg_zhops m)))
680
+ (other quality_increases {i})"
681
+ and "dip\<in>kD(rt (\<sigma> i))"
682
+ shows "sqn (rt (\<sigma> i)) dip = 0 \<longrightarrow>
683
+ sqnf (rt (\<sigma> i)) dip = unk
684
+ \<and> the (dhops (rt (\<sigma> i)) dip) = 1
685
+ \<and> the (nhop (rt (\<sigma> i)) dip) = dip"
686
+ (is "?P dip")
687
+ proof -
688
+ have "\<exists>l. l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p" by (metis aodv_ex_label)
689
+ with assms(1) have "\<forall>dip\<in>kD (rt (\<sigma> i)). ?P dip"
690
+ by - (drule oinvariant_weakenD [OF ozero_seq_unk_hops_one [OF oaodv_trans aodv_trans]],
691
+ auto dest!: onlD otherwith_actionD simp: seqlsimp)
692
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> show ?thesis by simp
693
+ qed
694
+
695
+ lemma seq_nhop_quality_increases':
696
+ shows "opaodv i \<Turnstile> (otherwith ((=)) {i}
697
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
698
+ other quality_increases {i} \<rightarrow>)
699
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<sigma>, _). \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
700
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip))
701
+ \<and> nhip \<noteq> dip
702
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
703
+ (is "_ \<Turnstile> (?S i, _ \<rightarrow>) _")
704
+ proof -
705
+ have weaken:
706
+ "\<And>p I Q R P. p \<Turnstile> (otherwith quality_increases I (orecvmsg Q), other quality_increases I \<rightarrow>) P
707
+ \<Longrightarrow> p \<Turnstile> (otherwith ((=)) I (orecvmsg (\<lambda>\<sigma> m. Q \<sigma> m \<and> R \<sigma> m)), other quality_increases I \<rightarrow>) P"
708
+ by auto
709
+ {
710
+ fix i a and \<sigma> \<sigma>' :: "ip \<Rightarrow> state"
711
+ assume a1: "\<forall>dip. dip\<in>vD(rt (\<sigma> i))
712
+ \<and> dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
713
+ \<and> (the (nhop (rt (\<sigma> i)) dip)) \<noteq> dip
714
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
715
+ and ow: "?S i \<sigma> \<sigma>' a"
716
+ have "\<forall>dip. dip\<in>vD(rt (\<sigma> i))
717
+ \<and> dip\<in>vD (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))
718
+ \<and> (the (nhop (rt (\<sigma> i)) dip)) \<noteq> dip
719
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))"
720
+ proof clarify
721
+ fix dip
722
+ assume a2: "dip\<in>vD(rt (\<sigma> i))"
723
+ and a3: "dip\<in>vD (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))"
724
+ and a4: "(the (nhop (rt (\<sigma> i)) dip)) \<noteq> dip"
725
+ from ow have "\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j" by auto
726
+ show "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))"
727
+ proof (cases "(the (nhop (rt (\<sigma> i)) dip)) = i")
728
+ assume "(the (nhop (rt (\<sigma> i)) dip)) = i"
729
+ with \<open>dip \<in> vD(rt (\<sigma> i))\<close> have "dip \<in> vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))" by simp
730
+ with a1 a2 a4 have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))" by simp
731
+ with \<open>(the (nhop (rt (\<sigma> i)) dip)) = i\<close> have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> i)" by simp
732
+ hence False by simp
733
+ thus ?thesis ..
734
+ next
735
+ assume "(the (nhop (rt (\<sigma> i)) dip)) \<noteq> i"
736
+ with \<open>\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j\<close>
737
+ have *: "\<sigma> (the (nhop (rt (\<sigma> i)) dip)) = \<sigma>' (the (nhop (rt (\<sigma> i)) dip))" by simp
738
+ with \<open>dip\<in>vD (rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))\<close>
739
+ have "dip\<in>vD (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))" by simp
740
+ with a1 a2 a4 have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))" by simp
741
+ with * show ?thesis by simp
742
+ qed
743
+ qed
744
+ } note basic = this
745
+
746
+ { fix \<sigma> \<sigma>' a dip sip i
747
+ assume a1: "\<forall>dip. dip\<in>vD(rt (\<sigma> i))
748
+ \<and> dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
749
+ \<and> the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
750
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
751
+ and ow: "?S i \<sigma> \<sigma>' a"
752
+ have "\<forall>dip. dip\<in>vD(update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {}))
753
+ \<and> dip\<in>vD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip))))
754
+ \<and> the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip) \<noteq> dip
755
+ \<longrightarrow> update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})
756
+ \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip)))"
757
+ proof clarify
758
+ fix dip
759
+ assume a2: "dip\<in>vD (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {}))"
760
+ and a3: "dip\<in>vD(rt (\<sigma>' (the (nhop
761
+ (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip))))"
762
+ and a4: "the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip) \<noteq> dip"
763
+ show "update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})
764
+ \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip)))"
765
+ proof (cases "dip = sip")
766
+ assume "dip = sip"
767
+ with \<open>the (nhop (update (rt (\<sigma> i)) sip (0, unk, val, Suc 0, sip, {})) dip) \<noteq> dip\<close>
768
+ have False by simp
769
+ thus ?thesis ..
770
+ next
771
+ assume [simp]: "dip \<noteq> sip"
772
+ from a2 have "dip\<in>vD(rt (\<sigma> i)) \<or> dip = sip"
773
+ by (rule vD_update_val)
774
+ with \<open>dip \<noteq> sip\<close> have "dip\<in>vD(rt (\<sigma> i))" by simp
775
+ moreover from a3 have "dip\<in>vD(rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip))))" by simp
776
+ moreover from a4 have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip" by simp
777
+ ultimately have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))"
778
+ using a1 ow by - (drule(1) basic, simp)
779
+ with \<open>dip \<noteq> sip\<close> show ?thesis
780
+ by - (erule rt_strictly_fresher_update_other, simp)
781
+ qed
782
+ qed
783
+ } note update_0_unk = this
784
+
785
+ { fix \<sigma> a \<sigma>' nhop
786
+ assume pre: "\<forall>dip. dip\<in>vD(rt (\<sigma> i)) \<and> dip\<in>vD(rt (\<sigma> (nhop dip))) \<and> nhop dip \<noteq> dip
787
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (nhop dip))"
788
+ and ow: "?S i \<sigma> \<sigma>' a"
789
+ have "\<forall>dip. dip \<in> vD (invalidate (rt (\<sigma> i)) (dests (\<sigma> i)))
790
+ \<and> dip \<in> vD (rt (\<sigma>' (nhop dip))) \<and> nhop dip \<noteq> dip
791
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (nhop dip))"
792
+ proof clarify
793
+ fix dip
794
+ assume "dip\<in>vD(invalidate (rt (\<sigma> i)) (dests (\<sigma> i)))"
795
+ and "dip\<in>vD(rt (\<sigma>' (nhop dip)))"
796
+ and "nhop dip \<noteq> dip"
797
+ from this(1) have "dip\<in>vD (rt (\<sigma> i))"
798
+ by (clarsimp dest!: vD_invalidate_vD_not_dests)
799
+ moreover from ow have "\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j" by auto
800
+ ultimately have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (nhop dip))"
801
+ using pre \<open>dip \<in> vD (rt (\<sigma>' (nhop dip)))\<close> \<open>nhop dip \<noteq> dip\<close>
802
+ by metis
803
+ with \<open>\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j\<close> show "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (nhop dip))"
804
+ by (metis rt_strictly_fresher_irefl)
805
+ qed
806
+ } note invalidate = this
807
+
808
+ { fix \<sigma> a \<sigma>' dip oip osn sip hops i
809
+ assume pre: "\<forall>dip. dip \<in> vD (rt (\<sigma> i))
810
+ \<and> dip \<in> vD (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
811
+ \<and> the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
812
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
813
+ and ow: "?S i \<sigma> \<sigma>' a"
814
+ and "Suc 0 \<le> osn"
815
+ and a6: "sip \<noteq> oip \<longrightarrow> oip \<in> kD (rt (\<sigma> sip))
816
+ \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
817
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
818
+ \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops
819
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv)"
820
+ and after: "\<sigma>' i = \<sigma> i\<lparr>rt := update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})\<rparr>"
821
+ have "\<forall>dip. dip \<in> vD (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {}))
822
+ \<and> dip \<in> vD (rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
823
+ (osn, kno, val, Suc hops, sip, {})) dip))))
824
+ \<and> the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip) \<noteq> dip
825
+ \<longrightarrow> update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})
826
+ \<sqsubset>\<^bsub>dip\<^esub>
827
+ rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip)))"
828
+ proof clarify
829
+ fix dip
830
+ assume a2: "dip\<in>vD(update (rt (\<sigma> i)) oip (osn, kno, val, Suc (hops), sip, {}))"
831
+ and a3: "dip\<in>vD(rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip
832
+ (osn, kno, val, Suc hops, sip, {})) dip))))"
833
+ and a4: "the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip) \<noteq> dip"
834
+ from ow have a5: "\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j = \<sigma>' j" by auto
835
+ show "update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})
836
+ \<sqsubset>\<^bsub>dip\<^esub>
837
+ rt (\<sigma>' (the (nhop (update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {})) dip)))"
838
+ (is "?rt1 \<sqsubset>\<^bsub>dip\<^esub> ?rt2 dip")
839
+ proof (cases "?rt1 = rt (\<sigma> i)")
840
+ assume nochange [simp]:
841
+ "update (rt (\<sigma> i)) oip (osn, kno, val, Suc hops, sip, {}) = rt (\<sigma> i)"
842
+
843
+ from after have "\<sigma>' i = \<sigma> i" by simp
844
+ with a5 have "\<forall>j. \<sigma> j = \<sigma>' j" by metis
845
+
846
+ from a2 have "dip\<in>vD (rt (\<sigma> i))" by simp
847
+ moreover from a3 have "dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))"
848
+ using nochange and \<open>\<forall>j. \<sigma> j = \<sigma>' j\<close> by clarsimp
849
+ moreover from a4 have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip" by simp
850
+ ultimately have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
851
+ using pre by simp
852
+
853
+ hence "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma> i)) dip)))"
854
+ using \<open>\<forall>j. \<sigma> j = \<sigma>' j\<close> by simp
855
+ thus "?thesis" by simp
856
+ next
857
+ assume change: "?rt1 \<noteq> rt (\<sigma> i)"
858
+ from after a2 have "dip\<in>kD(rt (\<sigma>' i))" by auto
859
+ show ?thesis
860
+ proof (cases "dip = oip")
861
+ assume "dip \<noteq> oip"
862
+
863
+ with a2 have "dip\<in>vD (rt (\<sigma> i))" by auto
864
+ moreover with a3 a5 after and \<open>dip \<noteq> oip\<close>
865
+ have "dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))"
866
+ by simp metis
867
+ moreover from a4 and \<open>dip \<noteq> oip\<close> have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip" by simp
868
+ ultimately have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
869
+ using pre by simp
870
+
871
+ with after and a5 and \<open>dip \<noteq> oip\<close> show ?thesis
872
+ by simp (metis rt_strictly_fresher_update_other
873
+ rt_strictly_fresher_irefl)
874
+ next
875
+ assume "dip = oip"
876
+
877
+ with a4 and change have "sip \<noteq> oip" by simp
878
+ with a6 have "oip\<in>kD(rt (\<sigma> sip))"
879
+ and "osn \<le> nsqn (rt (\<sigma> sip)) oip" by auto
880
+
881
+ from a3 change \<open>dip = oip\<close> have "oip\<in>vD(rt (\<sigma>' sip))" by simp
882
+ hence "the (flag (rt (\<sigma>' sip)) oip) = val" by simp
883
+
884
+ from \<open>oip\<in>kD(rt (\<sigma> sip))\<close>
885
+ have "osn < nsqn (rt (\<sigma>' sip)) oip \<or> (osn = nsqn (rt (\<sigma>' sip)) oip
886
+ \<and> the (dhops (rt (\<sigma>' sip)) oip) \<le> hops)"
887
+ proof
888
+ assume "oip\<in>vD(rt (\<sigma> sip))"
889
+ hence "the (flag (rt (\<sigma> sip)) oip) = val" by simp
890
+ with a6 \<open>sip \<noteq> oip\<close> have "nsqn (rt (\<sigma> sip)) oip = osn \<longrightarrow>
891
+ the (dhops (rt (\<sigma> sip)) oip) \<le> hops"
892
+ by simp
893
+ show ?thesis
894
+ proof (cases "sip = i")
895
+ assume "sip \<noteq> i"
896
+ with a5 have "\<sigma> sip = \<sigma>' sip" by simp
897
+ with \<open>osn \<le> nsqn (rt (\<sigma> sip)) oip\<close>
898
+ and \<open>nsqn (rt (\<sigma> sip)) oip = osn \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops\<close>
899
+ show ?thesis by auto
900
+ next
901
+ \<comment> \<open>alternative to using @{text sip_not_ip}\<close>
902
+ assume [simp]: "sip = i"
903
+ have "?rt1 = rt (\<sigma> i)"
904
+ proof (rule update_cases_kD, simp_all)
905
+ from \<open>Suc 0 \<le> osn\<close> show "0 < osn" by simp
906
+ next
907
+ from \<open>oip\<in>kD(rt (\<sigma> sip))\<close> and \<open>sip = i\<close> show "oip\<in>kD(rt (\<sigma> i))"
908
+ by simp
909
+ next
910
+ assume "sqn (rt (\<sigma> i)) oip < osn"
911
+ also from \<open>osn \<le> nsqn (rt (\<sigma> sip)) oip\<close>
912
+ have "... \<le> nsqn (rt (\<sigma> i)) oip" by simp
913
+ also have "... \<le> sqn (rt (\<sigma> i)) oip"
914
+ by (rule nsqn_sqn)
915
+ finally have "sqn (rt (\<sigma> i)) oip < sqn (rt (\<sigma> i)) oip" .
916
+ hence False by simp
917
+ thus "(\<lambda>a. if a = oip
918
+ then Some (osn, kno, val, Suc hops, i, \<pi>\<^sub>7 (the (rt (\<sigma> i) oip)))
919
+ else rt (\<sigma> i) a) = rt (\<sigma> i)" ..
920
+ next
921
+ assume "sqn (rt (\<sigma> i)) oip = osn"
922
+ and "Suc hops < the (dhops (rt (\<sigma> i)) oip)"
923
+ from this(1) and \<open>oip \<in> vD (rt (\<sigma> sip))\<close> have "nsqn (rt (\<sigma> i)) oip = osn"
924
+ by simp
925
+ with \<open>nsqn (rt (\<sigma> sip)) oip = osn \<longrightarrow> the (dhops (rt (\<sigma> sip)) oip) \<le> hops\<close>
926
+ have "the (dhops (rt (\<sigma> i)) oip) \<le> hops" by simp
927
+ with \<open>Suc hops < the (dhops (rt (\<sigma> i)) oip)\<close> have False by simp
928
+ thus "(\<lambda>a. if a = oip
929
+ then Some (osn, kno, val, Suc hops, i, \<pi>\<^sub>7 (the (rt (\<sigma> i) oip)))
930
+ else rt (\<sigma> i) a) = rt (\<sigma> i)" ..
931
+ next
932
+ assume "the (flag (rt (\<sigma> i)) oip) = inv"
933
+ with \<open>the (flag (rt (\<sigma> sip)) oip) = val\<close> have False by simp
934
+ thus "(\<lambda>a. if a = oip
935
+ then Some (osn, kno, val, Suc hops, i, \<pi>\<^sub>7 (the (rt (\<sigma> i) oip)))
936
+ else rt (\<sigma> i) a) = rt (\<sigma> i)" ..
937
+ next
938
+ from \<open>oip\<in>kD(rt (\<sigma> sip))\<close>
939
+ show "(\<lambda>a. if a = oip then Some (the (rt (\<sigma> i) oip)) else rt (\<sigma> i) a) = rt (\<sigma> i)"
940
+ by (auto dest!: kD_Some)
941
+ qed
942
+ with change have False ..
943
+ thus ?thesis ..
944
+ qed
945
+ next
946
+ assume "oip\<in>iD(rt (\<sigma> sip))"
947
+ with \<open>the (flag (rt (\<sigma>' sip)) oip) = val\<close> and a5 have "sip = i"
948
+ by (metis f.distinct(1) iD_flag_is_inv)
949
+ from \<open>oip\<in>iD(rt (\<sigma> sip))\<close> have "the (flag (rt (\<sigma> sip)) oip) = inv" by auto
950
+ with \<open>sip = i\<close> \<open>Suc 0 \<le> osn\<close> change after \<open>oip\<in>kD(rt (\<sigma> sip))\<close>
951
+ have "nsqn (rt (\<sigma> sip)) oip < nsqn (rt (\<sigma>' sip)) oip"
952
+ unfolding update_def
953
+ by (clarsimp split: option.split_asm if_split_asm)
954
+ (auto simp: sqn_def)
955
+ with \<open>osn \<le> nsqn (rt (\<sigma> sip)) oip\<close> have "osn < nsqn (rt (\<sigma>' sip)) oip"
956
+ by simp
957
+ thus ?thesis ..
958
+ qed
959
+ thus ?thesis
960
+ proof
961
+ assume osnlt: "osn < nsqn (rt (\<sigma>' sip)) oip"
962
+ from \<open>dip\<in>kD(rt (\<sigma>' i))\<close> and \<open>dip = oip\<close> have "dip \<in> kD (?rt1)" by simp
963
+ moreover from a3 have "dip \<in> kD(?rt2 dip)" by simp
964
+ moreover have "nsqn ?rt1 dip < nsqn (?rt2 dip) dip"
965
+ proof -
966
+ have "nsqn ?rt1 oip = osn"
967
+ by (simp add: \<open>dip = oip\<close> nsqn_update_changed_kno_val [OF change [THEN not_sym]])
968
+ also have "... < nsqn (rt (\<sigma>' sip)) oip" using osnlt .
969
+ also have "... = nsqn (?rt2 oip) oip" by (simp add: change)
970
+ finally show ?thesis
971
+ using \<open>dip = oip\<close> by simp
972
+ qed
973
+ ultimately show ?thesis
974
+ by (rule rt_strictly_fresher_ltI)
975
+ next
976
+ assume osneq: "osn = nsqn (rt (\<sigma>' sip)) oip \<and> the (dhops (rt (\<sigma>' sip)) oip) \<le> hops"
977
+
978
+ have "oip\<in>kD(?rt1)" by simp
979
+ moreover from a3 \<open>dip = oip\<close> have "oip\<in>kD(?rt2 oip)" by simp
980
+
981
+ moreover have "nsqn ?rt1 oip = nsqn (?rt2 oip) oip"
982
+ proof -
983
+ from osneq have "osn = nsqn (rt (\<sigma>' sip)) oip" ..
984
+ also have "osn = nsqn ?rt1 oip"
985
+ by (simp add: \<open>dip = oip\<close> nsqn_update_changed_kno_val [OF change [THEN not_sym]])
986
+ also have "nsqn (rt (\<sigma>' sip)) oip = nsqn (?rt2 oip) oip"
987
+ by (simp add: change)
988
+ finally show ?thesis .
989
+ qed
990
+
991
+ moreover have "\<pi>\<^sub>5(the (?rt2 oip oip)) < \<pi>\<^sub>5(the (?rt1 oip))"
992
+ proof -
993
+ from osneq have "the (dhops (rt (\<sigma>' sip)) oip) \<le> hops" ..
994
+ moreover from \<open>oip \<in> vD (rt (\<sigma>' sip))\<close> have "oip\<in>kD(rt (\<sigma>' sip))" by auto
995
+ ultimately have "\<pi>\<^sub>5(the (rt (\<sigma>' sip) oip)) \<le> hops"
996
+ by (auto simp add: proj5_eq_dhops)
997
+ also from change after have "hops < \<pi>\<^sub>5(the (rt (\<sigma>' i) oip))"
998
+ by (simp add: proj5_eq_dhops) (metis dhops_update_changed lessI)
999
+ finally have "\<pi>\<^sub>5(the (rt (\<sigma>' sip) oip)) < \<pi>\<^sub>5(the (rt (\<sigma>' i) oip))" .
1000
+ with change after show ?thesis by simp
1001
+ qed
1002
+
1003
+ ultimately have "?rt1 \<sqsubset>\<^bsub>oip\<^esub> ?rt2 oip"
1004
+ by (rule rt_strictly_fresher_eqI)
1005
+ with \<open>dip = oip\<close> show ?thesis by simp
1006
+ qed
1007
+ qed
1008
+ qed
1009
+ qed
1010
+ } note rreq_rrep_update = this
1011
+
1012
+ have "opaodv i \<Turnstile> (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m
1013
+ \<and> msg_zhops m)),
1014
+ other quality_increases {i} \<rightarrow>)
1015
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V
1016
+ (\<lambda>(\<sigma>, _). \<forall>dip. dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
1017
+ \<and> the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
1018
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))"
1019
+ proof (inv_cterms inv add: onl_oinvariant_sterms [OF aodv_wf rreq_sip [THEN weaken]]
1020
+ onl_oinvariant_sterms [OF aodv_wf rrep_sip [THEN weaken]]
1021
+ onl_oinvariant_sterms [OF aodv_wf rerr_sip [THEN weaken]]
1022
+ onl_oinvariant_sterms [OF aodv_wf oosn_rreq [THEN weaken]]
1023
+ onl_oinvariant_sterms [OF aodv_wf odsn_rrep [THEN weaken]]
1024
+ onl_oinvariant_sterms [OF aodv_wf oaddpreRT_welldefined]
1025
+ solve: basic update_0_unk invalidate rreq_rrep_update
1026
+ simp add: seqlsimp)
1027
+ fix \<sigma> \<sigma>' p l
1028
+ assume or: "(\<sigma>, p) \<in> oreachable (opaodv i) (?S i) (other quality_increases {i})"
1029
+ and "other quality_increases {i} \<sigma> \<sigma>'"
1030
+ and ll: "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
1031
+ and pre: "\<forall>dip. dip\<in>vD (rt (\<sigma> i))
1032
+ \<and> dip\<in>vD(rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip))))
1033
+ \<and> the (nhop (rt (\<sigma> i)) dip) \<noteq> dip
1034
+ \<longrightarrow> rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> (the (nhop (rt (\<sigma> i)) dip)))"
1035
+ from this(1-2)
1036
+ have or': "(\<sigma>', p) \<in> oreachable (opaodv i) (?S i) (other quality_increases {i})"
1037
+ by - (rule oreachable_other')
1038
+
1039
+ from or and ll have next_hop: "\<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
1040
+ in dip \<in> kD(rt (\<sigma> i)) \<and> nhip \<noteq> dip
1041
+ \<longrightarrow> dip \<in> kD(rt (\<sigma> nhip))
1042
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> nhip)) dip"
1043
+ by (auto dest!: onl_oinvariant_weakenD [OF seq_compare_next_hop'])
1044
+
1045
+ from or and ll have unk_hops_one: "\<forall>dip\<in>kD (rt (\<sigma> i)). sqn (rt (\<sigma> i)) dip = 0
1046
+ \<longrightarrow> sqnf (rt (\<sigma> i)) dip = unk
1047
+ \<and> the (dhops (rt (\<sigma> i)) dip) = 1
1048
+ \<and> the (nhop (rt (\<sigma> i)) dip) = dip"
1049
+ by (auto dest!: onl_oinvariant_weakenD [OF ozero_seq_unk_hops_one
1050
+ [OF oaodv_trans aodv_trans]]
1051
+ otherwith_actionD
1052
+ simp: seqlsimp)
1053
+
1054
+ from \<open>other quality_increases {i} \<sigma> \<sigma>'\<close> have "\<sigma>' i = \<sigma> i" by auto
1055
+ hence "quality_increases (\<sigma> i) (\<sigma>' i)" by auto
1056
+ with \<open>other quality_increases {i} \<sigma> \<sigma>'\<close> have "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
1057
+ by - (erule otherE, metis singleton_iff)
1058
+
1059
+ show "\<forall>dip. dip \<in> vD (rt (\<sigma>' i))
1060
+ \<and> dip \<in> vD (rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip))))
1061
+ \<and> the (nhop (rt (\<sigma>' i)) dip) \<noteq> dip
1062
+ \<longrightarrow> rt (\<sigma>' i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip)))"
1063
+ proof clarify
1064
+ fix dip
1065
+ assume "dip\<in>vD(rt (\<sigma>' i))"
1066
+ and "dip\<in>vD(rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip))))"
1067
+ and "the (nhop (rt (\<sigma>' i)) dip) \<noteq> dip"
1068
+ from this(1) and \<open>\<sigma>' i = \<sigma> i\<close> have "dip\<in>vD(rt (\<sigma> i))"
1069
+ and "dip\<in>kD(rt (\<sigma> i))"
1070
+ by auto
1071
+
1072
+ from \<open>the (nhop (rt (\<sigma>' i)) dip) \<noteq> dip\<close> and \<open>\<sigma>' i = \<sigma> i\<close>
1073
+ have "the (nhop (rt (\<sigma> i)) dip) \<noteq> dip" (is "?nhip \<noteq> _") by simp
1074
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> and next_hop
1075
+ have "dip\<in>kD(rt (\<sigma> (?nhip)))"
1076
+ and nsqns: "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> ?nhip)) dip"
1077
+ by (auto simp: Let_def)
1078
+
1079
+ have "0 < sqn (rt (\<sigma> i)) dip"
1080
+ proof (rule neq0_conv [THEN iffD1, OF notI])
1081
+ assume "sqn (rt (\<sigma> i)) dip = 0"
1082
+ with \<open>dip\<in>kD(rt (\<sigma> i))\<close> and unk_hops_one
1083
+ have "?nhip = dip" by simp
1084
+ with \<open>?nhip \<noteq> dip\<close> show False ..
1085
+ qed
1086
+ also have "... = nsqn (rt (\<sigma> i)) dip"
1087
+ by (rule vD_nsqn_sqn [OF \<open>dip\<in>vD(rt (\<sigma> i))\<close>, THEN sym])
1088
+ also have "... \<le> nsqn (rt (\<sigma> ?nhip)) dip"
1089
+ by (rule nsqns)
1090
+ also have "... \<le> sqn (rt (\<sigma> ?nhip)) dip"
1091
+ by (rule nsqn_sqn)
1092
+ finally have "0 < sqn (rt (\<sigma> ?nhip)) dip" .
1093
+
1094
+ have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' ?nhip)"
1095
+ proof (cases "dip\<in>vD(rt (\<sigma> ?nhip))")
1096
+ assume "dip\<in>vD(rt (\<sigma> ?nhip))"
1097
+ with pre \<open>dip\<in>vD(rt (\<sigma> i))\<close> and \<open>?nhip \<noteq> dip\<close>
1098
+ have "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> ?nhip)" by auto
1099
+ moreover from \<open>\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)\<close>
1100
+ have "quality_increases (\<sigma> ?nhip) (\<sigma>' ?nhip)" ..
1101
+ ultimately show ?thesis
1102
+ using \<open>dip\<in>kD(rt (\<sigma> ?nhip))\<close>
1103
+ by (rule strictly_fresher_quality_increases_right)
1104
+ next
1105
+ assume "dip\<notin>vD(rt (\<sigma> ?nhip))"
1106
+ with \<open>dip\<in>kD(rt (\<sigma> ?nhip))\<close> have "dip\<in>iD(rt (\<sigma> ?nhip))" ..
1107
+ hence "the (flag (rt (\<sigma> ?nhip)) dip) = inv"
1108
+ by auto
1109
+ have "nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> ?nhip)) dip"
1110
+ by (rule nsqns)
1111
+ also from \<open>dip\<in>iD(rt (\<sigma> ?nhip))\<close>
1112
+ have "... = sqn (rt (\<sigma> ?nhip)) dip - 1" ..
1113
+ also have "... < sqn (rt (\<sigma>' ?nhip)) dip"
1114
+ proof -
1115
+ from \<open>\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)\<close>
1116
+ have "quality_increases (\<sigma> ?nhip) (\<sigma>' ?nhip)" ..
1117
+ hence "\<forall>ip. sqn (rt (\<sigma> ?nhip)) ip \<le> sqn (rt (\<sigma>' ?nhip)) ip" by auto
1118
+ hence "sqn (rt (\<sigma> ?nhip)) dip \<le> sqn (rt (\<sigma>' ?nhip)) dip" ..
1119
+ with \<open>0 < sqn (rt (\<sigma> ?nhip)) dip\<close> show ?thesis by auto
1120
+ qed
1121
+ also have "... = nsqn (rt (\<sigma>' ?nhip)) dip"
1122
+ proof (rule vD_nsqn_sqn [THEN sym])
1123
+ from \<open>dip\<in>vD(rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip))))\<close> and \<open>\<sigma>' i = \<sigma> i\<close>
1124
+ show "dip\<in>vD(rt (\<sigma>' ?nhip))" by simp
1125
+ qed
1126
+ finally have "nsqn (rt (\<sigma> i)) dip < nsqn (rt (\<sigma>' ?nhip)) dip" .
1127
+
1128
+ moreover from \<open>dip\<in>vD(rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip))))\<close> and \<open>\<sigma>' i = \<sigma> i\<close>
1129
+ have "dip\<in>kD(rt (\<sigma>' ?nhip))" by auto
1130
+ ultimately show "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' ?nhip)"
1131
+ using \<open>dip\<in>kD(rt (\<sigma> i))\<close> by - (rule rt_strictly_fresher_ltI)
1132
+ qed
1133
+ with \<open>\<sigma>' i = \<sigma> i\<close> show "rt (\<sigma>' i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' (the (nhop (rt (\<sigma>' i)) dip)))"
1134
+ by simp
1135
+ qed
1136
+ qed
1137
+ thus ?thesis unfolding Let_def .
1138
+ qed
1139
+
1140
+ lemma seq_compare_next_hop:
1141
+ fixes w
1142
+ shows "opaodv i \<Turnstile> (otherwith ((=)) {i} (orecvmsg msg_fresh),
1143
+ other quality_increases {i} \<rightarrow>)
1144
+ global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
1145
+ in dip \<in> kD(rt (\<sigma> i)) \<and> nhip \<noteq> dip \<longrightarrow>
1146
+ dip \<in> kD(rt (\<sigma> nhip))
1147
+ \<and> nsqn (rt (\<sigma> i)) dip \<le> nsqn (rt (\<sigma> nhip)) dip)"
1148
+ by (rule oinvariant_weakenE [OF seq_compare_next_hop']) (auto dest!: onlD)
1149
+
1150
+ lemma seq_nhop_quality_increases:
1151
+ shows "opaodv i \<Turnstile> (otherwith ((=)) {i}
1152
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
1153
+ other quality_increases {i} \<rightarrow>)
1154
+ global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
1155
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
1156
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
1157
+ by (rule oinvariant_weakenE [OF seq_nhop_quality_increases']) (auto dest!: onlD)
1158
+
1159
+ end
formal/afp/AODV/variants/a_norreqid/A_Loop_Freedom.thy ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Loop_Freedom.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Routing graphs and loop freedom"
7
+
8
+ theory A_Loop_Freedom
9
+ imports A_Aodv_Predicates A_Fresher
10
+ begin
11
+
12
+ text \<open>Define the central theorem that relates an invariant over network states to the absence
13
+ of loops in the associate routing graph.\<close>
14
+
15
+ definition
16
+ rt_graph :: "(ip \<Rightarrow> state) \<Rightarrow> ip \<Rightarrow> ip rel"
17
+ where
18
+ "rt_graph \<sigma> = (\<lambda>dip.
19
+ {(ip, ip') | ip ip' dsn dsk hops pre.
20
+ ip \<noteq> dip \<and> rt (\<sigma> ip) dip = Some (dsn, dsk, val, hops, ip', pre)})"
21
+
22
+ text \<open>Given the state of a network @{term \<sigma>}, a routing graph for a given destination
23
+ ip address @{term dip} abstracts the details of routing tables into nodes
24
+ (ip addresses) and vertices (valid routes between ip addresses).\<close>
25
+
26
+ lemma rt_graphE [elim]:
27
+ fixes n dip ip ip'
28
+ assumes "(ip, ip') \<in> rt_graph \<sigma> dip"
29
+ shows "ip \<noteq> dip \<and> (\<exists>r. rt (\<sigma> ip) = r
30
+ \<and> (\<exists>dsn dsk hops pre. r dip = Some (dsn, dsk, val, hops, ip', pre)))"
31
+ using assms unfolding rt_graph_def by auto
32
+
33
+ lemma rt_graph_vD [dest]:
34
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> rt_graph \<sigma> dip \<Longrightarrow> dip \<in> vD(rt (\<sigma> ip))"
35
+ unfolding rt_graph_def vD_def by auto
36
+
37
+ lemma rt_graph_vD_trans [dest]:
38
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> (rt_graph \<sigma> dip)\<^sup>+ \<Longrightarrow> dip \<in> vD(rt (\<sigma> ip))"
39
+ by (erule converse_tranclE) auto
40
+
41
+ lemma rt_graph_not_dip [dest]:
42
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> rt_graph \<sigma> dip \<Longrightarrow> ip \<noteq> dip"
43
+ unfolding rt_graph_def by auto
44
+
45
+ lemma rt_graph_not_dip_trans [dest]:
46
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> (rt_graph \<sigma> dip)\<^sup>+ \<Longrightarrow> ip \<noteq> dip"
47
+ by (erule converse_tranclE) auto
48
+
49
+ text "NB: the property below cannot be lifted to the transitive closure"
50
+
51
+ lemma rt_graph_nhip_is_nhop [dest]:
52
+ "\<And>ip ip' \<sigma> dip. (ip, ip') \<in> rt_graph \<sigma> dip \<Longrightarrow> ip' = the (nhop (rt (\<sigma> ip)) dip)"
53
+ unfolding rt_graph_def by auto
54
+
55
+ theorem inv_to_loop_freedom:
56
+ assumes "\<forall>i dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
57
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
58
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip))"
59
+ shows "\<forall>dip. irrefl ((rt_graph \<sigma> dip)\<^sup>+)"
60
+ using assms proof (intro allI)
61
+ fix \<sigma> :: "ip \<Rightarrow> state" and dip
62
+ assume inv: "\<forall>ip dip.
63
+ let nhip = the (nhop (rt (\<sigma> ip)) dip)
64
+ in dip \<in> vD(rt (\<sigma> ip)) \<inter> vD(rt (\<sigma> nhip)) \<and>
65
+ nhip \<noteq> dip \<longrightarrow> rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)"
66
+ { fix ip ip'
67
+ assume "(ip, ip') \<in> (rt_graph \<sigma> dip)\<^sup>+"
68
+ and "dip \<in> vD(rt (\<sigma> ip'))"
69
+ and "ip' \<noteq> dip"
70
+ hence "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> ip')"
71
+ proof induction
72
+ fix nhip
73
+ assume "(ip, nhip) \<in> rt_graph \<sigma> dip"
74
+ and "dip \<in> vD(rt (\<sigma> nhip))"
75
+ and "nhip \<noteq> dip"
76
+ from \<open>(ip, nhip) \<in> rt_graph \<sigma> dip\<close> have "dip \<in> vD(rt (\<sigma> ip))"
77
+ and "nhip = the (nhop (rt (\<sigma> ip)) dip)"
78
+ by auto
79
+ from \<open>dip \<in> vD(rt (\<sigma> ip))\<close> and \<open>dip \<in> vD(rt (\<sigma> nhip))\<close>
80
+ have "dip \<in> vD(rt (\<sigma> ip)) \<inter> vD(rt (\<sigma> nhip))" ..
81
+ with \<open>nhip = the (nhop (rt (\<sigma> ip)) dip)\<close>
82
+ and \<open>nhip \<noteq> dip\<close>
83
+ and inv
84
+ show "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)"
85
+ by (clarsimp simp: Let_def)
86
+ next
87
+ fix nhip nhip'
88
+ assume "(ip, nhip) \<in> (rt_graph \<sigma> dip)\<^sup>+"
89
+ and "(nhip, nhip') \<in> rt_graph \<sigma> dip"
90
+ and IH: "\<lbrakk> dip \<in> vD(rt (\<sigma> nhip)); nhip \<noteq> dip \<rbrakk> \<Longrightarrow> rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)"
91
+ and "dip \<in> vD(rt (\<sigma> nhip'))"
92
+ and "nhip' \<noteq> dip"
93
+ from \<open>(nhip, nhip') \<in> rt_graph \<sigma> dip\<close> have 1: "dip \<in> vD(rt (\<sigma> nhip))"
94
+ and 2: "nhip \<noteq> dip"
95
+ and "nhip' = the (nhop (rt (\<sigma> nhip)) dip)"
96
+ by auto
97
+ from 1 2 have "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)" by (rule IH)
98
+ also have "rt (\<sigma> nhip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip')"
99
+ proof -
100
+ from \<open>dip \<in> vD(rt (\<sigma> nhip))\<close> and \<open>dip \<in> vD(rt (\<sigma> nhip'))\<close>
101
+ have "dip \<in> vD(rt (\<sigma> nhip)) \<inter> vD(rt (\<sigma> nhip'))" ..
102
+ with \<open>nhip' \<noteq> dip\<close>
103
+ and \<open>nhip' = the (nhop (rt (\<sigma> nhip)) dip)\<close>
104
+ and inv
105
+ show "rt (\<sigma> nhip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip')"
106
+ by (clarsimp simp: Let_def)
107
+ qed
108
+ finally show "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip')" .
109
+ qed } note fresher = this
110
+
111
+ show "irrefl ((rt_graph \<sigma> dip)\<^sup>+)"
112
+ unfolding irrefl_def proof (intro allI notI)
113
+ fix ip
114
+ assume "(ip, ip) \<in> (rt_graph \<sigma> dip)\<^sup>+"
115
+ moreover then have "dip \<in> vD(rt (\<sigma> ip))"
116
+ and "ip \<noteq> dip"
117
+ by auto
118
+ ultimately have "rt (\<sigma> ip) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> ip)" by (rule fresher)
119
+ thus False by simp
120
+ qed
121
+ qed
122
+
123
+ end
formal/afp/AODV/variants/a_norreqid/A_Norreqid.thy ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/A_Norreqid.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ theory %invisible A_Norreqid
8
+ imports "../../Aodv_Basic"
9
+ begin
10
+
11
+ chapter "Variant A: Skipping the RREQ ID"
12
+
13
+ text \<open>
14
+ Explanation~\cite[\textsection 10.1]{FehnkerEtAl:AWN:2013}:
15
+ AODV does not need the route request identifier. This number, in
16
+ combination with the IP address of the originator, is used to identify
17
+ every RREQ message in a unique way. This variant shows that the
18
+ combination of the originator's IP address and its sequence number is just
19
+ as suited to uniquely determine the route request to which the message
20
+ belongs. Hence, the route request identifier field is not required. This
21
+ can then reduce the size of the RREQ message.
22
+ \<close>
23
+
24
+ end %invisible
25
+
formal/afp/AODV/variants/a_norreqid/A_OAodv.thy ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/OAodv.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "The `open' AODV model"
7
+
8
+ theory A_OAodv
9
+ imports A_Aodv AWN.OAWN_SOS_Labels AWN.OAWN_Convert
10
+ begin
11
+
12
+ text \<open>Definitions for stating and proving global network properties over individual processes.\<close>
13
+
14
+ definition \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' :: "((ip \<Rightarrow> state) \<times> ((state, msg, pseqp, pseqp label) seqp)) set"
15
+ where "\<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' \<equiv> {(\<lambda>i. aodv_init i, \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv)}"
16
+
17
+ abbreviation opaodv
18
+ :: "ip \<Rightarrow> ((ip \<Rightarrow> state) \<times> (state, msg, pseqp, pseqp label) seqp, msg seq_action) automaton"
19
+ where
20
+ "opaodv i \<equiv> \<lparr> init = \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V', trans = oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<rparr>"
21
+
22
+ lemma initiali_aodv [intro!, simp]: "initiali i (init (opaodv i)) (init (paodv i))"
23
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def by rule simp_all
24
+
25
+ lemma oaodv_control_within [simp]: "control_within \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (init (opaodv i))"
26
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def by (rule control_withinI) (auto simp del: \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps)
27
+
28
+ lemma \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_labels [simp]: "(\<sigma>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' \<Longrightarrow> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p = {PAodv-:0}"
29
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def by simp
30
+
31
+ lemma oaodv_init_kD_empty [simp]:
32
+ "(\<sigma>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' \<Longrightarrow> kD (rt (\<sigma> i)) = {}"
33
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def kD_def by simp
34
+
35
+ lemma oaodv_init_vD_empty [simp]:
36
+ "(\<sigma>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V' \<Longrightarrow> vD (rt (\<sigma> i)) = {}"
37
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def vD_def by simp
38
+
39
+ lemma oaodv_trans: "trans (opaodv i) = oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
40
+ by simp
41
+
42
+ declare
43
+ oseq_invariant_ctermsI [OF aodv_wf oaodv_control_within aodv_simple_labels oaodv_trans, cterms_intros]
44
+ oseq_step_invariant_ctermsI [OF aodv_wf oaodv_control_within aodv_simple_labels oaodv_trans, cterms_intros]
45
+
46
+ end
47
+
formal/afp/AODV/variants/a_norreqid/A_Quality_Increases.thy ADDED
@@ -0,0 +1,457 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Quality_Increases.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ section "The quality increases predicate"
8
+
9
+ theory A_Quality_Increases
10
+ imports A_Aodv_Predicates A_Fresher
11
+ begin
12
+
13
+ definition quality_increases :: "state \<Rightarrow> state \<Rightarrow> bool"
14
+ where "quality_increases \<xi> \<xi>' \<equiv> (\<forall>dip\<in>kD(rt \<xi>). dip \<in> kD(rt \<xi>') \<and> rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>')
15
+ \<and> (\<forall>dip. sqn (rt \<xi>) dip \<le> sqn (rt \<xi>') dip)"
16
+
17
+ lemma quality_increasesI [intro!]:
18
+ assumes "\<And>dip. dip \<in> kD(rt \<xi>) \<Longrightarrow> dip \<in> kD(rt \<xi>')"
19
+ and "\<And>dip. \<lbrakk> dip \<in> kD(rt \<xi>); dip \<in> kD(rt \<xi>') \<rbrakk> \<Longrightarrow> rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>'"
20
+ and "\<And>dip. sqn (rt \<xi>) dip \<le> sqn (rt \<xi>') dip"
21
+ shows "quality_increases \<xi> \<xi>'"
22
+ unfolding quality_increases_def using assms by clarsimp
23
+
24
+ lemma quality_increasesE [elim]:
25
+ fixes dip
26
+ assumes "quality_increases \<xi> \<xi>'"
27
+ and "dip\<in>kD(rt \<xi>)"
28
+ and "\<lbrakk> dip \<in> kD(rt \<xi>'); rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>'; sqn (rt \<xi>) dip \<le> sqn (rt \<xi>') dip \<rbrakk> \<Longrightarrow> R dip \<xi> \<xi>'"
29
+ shows "R dip \<xi> \<xi>'"
30
+ using assms unfolding quality_increases_def by clarsimp
31
+
32
+ lemma quality_increases_rt_fresherD [dest]:
33
+ fixes ip
34
+ assumes "quality_increases \<xi> \<xi>'"
35
+ and "ip\<in>kD(rt \<xi>)"
36
+ shows "rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> rt \<xi>'"
37
+ using assms by auto
38
+
39
+ lemma quality_increases_sqnE [elim]:
40
+ fixes dip
41
+ assumes "quality_increases \<xi> \<xi>'"
42
+ and "sqn (rt \<xi>) dip \<le> sqn (rt \<xi>') dip \<Longrightarrow> R dip \<xi> \<xi>'"
43
+ shows "R dip \<xi> \<xi>'"
44
+ using assms unfolding quality_increases_def by clarsimp
45
+
46
+ lemma quality_increases_refl [intro, simp]: "quality_increases \<xi> \<xi>"
47
+ by rule simp_all
48
+
49
+ lemma strictly_fresher_quality_increases_right [elim]:
50
+ fixes \<sigma> \<sigma>' dip
51
+ assumes "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)"
52
+ and qinc: "quality_increases (\<sigma> nhip) (\<sigma>' nhip)"
53
+ and "dip\<in>kD(rt (\<sigma> nhip))"
54
+ shows "rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma>' nhip)"
55
+ proof -
56
+ from qinc have "rt (\<sigma> nhip) \<sqsubseteq>\<^bsub>dip\<^esub> rt (\<sigma>' nhip)" using \<open>dip\<in>kD(rt (\<sigma> nhip))\<close>
57
+ by auto
58
+ with \<open>rt (\<sigma> i) \<sqsubset>\<^bsub>dip\<^esub> rt (\<sigma> nhip)\<close> show ?thesis ..
59
+ qed
60
+
61
+ lemma kD_quality_increases [elim]:
62
+ assumes "i\<in>kD(rt \<xi>)"
63
+ and "quality_increases \<xi> \<xi>'"
64
+ shows "i\<in>kD(rt \<xi>')"
65
+ using assms by auto
66
+
67
+ lemma kD_nsqn_quality_increases [elim]:
68
+ assumes "i\<in>kD(rt \<xi>)"
69
+ and "quality_increases \<xi> \<xi>'"
70
+ shows "i\<in>kD(rt \<xi>') \<and> nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i"
71
+ proof -
72
+ from assms have "i\<in>kD(rt \<xi>')" ..
73
+ moreover with assms have "rt \<xi> \<sqsubseteq>\<^bsub>i\<^esub> rt \<xi>'" by auto
74
+ ultimately have "nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i"
75
+ using \<open>i\<in>kD(rt \<xi>)\<close> by - (erule(2) rt_fresher_imp_nsqn_le)
76
+ with \<open>i\<in>kD(rt \<xi>')\<close> show ?thesis ..
77
+ qed
78
+
79
+ lemma nsqn_quality_increases [elim]:
80
+ assumes "i\<in>kD(rt \<xi>)"
81
+ and "quality_increases \<xi> \<xi>'"
82
+ shows "nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i"
83
+ using assms by (rule kD_nsqn_quality_increases [THEN conjunct2])
84
+
85
+ lemma kD_nsqn_quality_increases_trans [elim]:
86
+ assumes "i\<in>kD(rt \<xi>)"
87
+ and "s \<le> nsqn (rt \<xi>) i"
88
+ and "quality_increases \<xi> \<xi>'"
89
+ shows "i\<in>kD(rt \<xi>') \<and> s \<le> nsqn (rt \<xi>') i"
90
+ proof
91
+ from \<open>i\<in>kD(rt \<xi>)\<close> and \<open>quality_increases \<xi> \<xi>'\<close> show "i\<in>kD(rt \<xi>')" ..
92
+ next
93
+ from \<open>i\<in>kD(rt \<xi>)\<close> and \<open>quality_increases \<xi> \<xi>'\<close> have "nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i" ..
94
+ with \<open>s \<le> nsqn (rt \<xi>) i\<close> show "s \<le> nsqn (rt \<xi>') i" by (rule le_trans)
95
+ qed
96
+
97
+ lemma nsqn_quality_increases_nsqn_lt_lt [elim]:
98
+ assumes "i\<in>kD(rt \<xi>)"
99
+ and "quality_increases \<xi> \<xi>'"
100
+ and "s < nsqn (rt \<xi>) i"
101
+ shows "s < nsqn (rt \<xi>') i"
102
+ proof -
103
+ from assms(1-2) have "nsqn (rt \<xi>) i \<le> nsqn (rt \<xi>') i" ..
104
+ with \<open>s < nsqn (rt \<xi>) i\<close> show "s < nsqn (rt \<xi>') i" by simp
105
+ qed
106
+
107
+ lemma nsqn_quality_increases_dhops [elim]:
108
+ assumes "i\<in>kD(rt \<xi>)"
109
+ and "quality_increases \<xi> \<xi>'"
110
+ and "nsqn (rt \<xi>) i = nsqn (rt \<xi>') i"
111
+ shows "the (dhops (rt \<xi>) i) \<ge> the (dhops (rt \<xi>') i)"
112
+ using assms unfolding quality_increases_def
113
+ by (clarsimp) (drule(1) bspec, clarsimp simp: rt_fresher_def2)
114
+
115
+ lemma nsqn_quality_increases_nsqn_eq_le [elim]:
116
+ assumes "i\<in>kD(rt \<xi>)"
117
+ and "quality_increases \<xi> \<xi>'"
118
+ and "s = nsqn (rt \<xi>) i"
119
+ shows "s < nsqn (rt \<xi>') i \<or> (s = nsqn (rt \<xi>') i \<and> the (dhops (rt \<xi>) i) \<ge> the (dhops (rt \<xi>') i))"
120
+ using assms by (metis nat_less_le nsqn_quality_increases nsqn_quality_increases_dhops)
121
+
122
+ lemma quality_increases_rreq_rrep_props [elim]:
123
+ fixes sn ip hops sip
124
+ assumes qinc: "quality_increases (\<sigma> sip) (\<sigma>' sip)"
125
+ and "1 \<le> sn"
126
+ and *: "ip\<in>kD(rt (\<sigma> sip)) \<and> sn \<le> nsqn (rt (\<sigma> sip)) ip
127
+ \<and> (nsqn (rt (\<sigma> sip)) ip = sn
128
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) ip) \<le> hops
129
+ \<or> the (flag (rt (\<sigma> sip)) ip) = inv))"
130
+ shows "ip\<in>kD(rt (\<sigma>' sip)) \<and> sn \<le> nsqn (rt (\<sigma>' sip)) ip
131
+ \<and> (nsqn (rt (\<sigma>' sip)) ip = sn
132
+ \<longrightarrow> (the (dhops (rt (\<sigma>' sip)) ip) \<le> hops
133
+ \<or> the (flag (rt (\<sigma>' sip)) ip) = inv))"
134
+ (is "_ \<and> ?nsqnafter")
135
+ proof -
136
+ from * obtain "ip\<in>kD(rt (\<sigma> sip))" and "sn \<le> nsqn (rt (\<sigma> sip)) ip" by auto
137
+
138
+ from \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
139
+ have "sqn (rt (\<sigma> sip)) ip \<le> sqn (rt (\<sigma>' sip)) ip" ..
140
+ from \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close> and \<open>ip\<in>kD (rt (\<sigma> sip))\<close>
141
+ have "ip\<in>kD (rt (\<sigma>' sip))" ..
142
+
143
+ from \<open>sn \<le> nsqn (rt (\<sigma> sip)) ip\<close> have ?nsqnafter
144
+ proof
145
+ assume "sn < nsqn (rt (\<sigma> sip)) ip"
146
+ also from \<open>ip\<in>kD(rt (\<sigma> sip))\<close> and \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
147
+ have "... \<le> nsqn (rt (\<sigma>' sip)) ip" ..
148
+ finally have "sn < nsqn (rt (\<sigma>' sip)) ip" .
149
+ thus ?thesis by simp
150
+ next
151
+ assume "sn = nsqn (rt (\<sigma> sip)) ip"
152
+ with \<open>ip\<in>kD(rt (\<sigma> sip))\<close> and \<open>quality_increases (\<sigma> sip) (\<sigma>' sip)\<close>
153
+ have "sn < nsqn (rt (\<sigma>' sip)) ip
154
+ \<or> (sn = nsqn (rt (\<sigma>' sip)) ip
155
+ \<and> the (dhops (rt (\<sigma>' sip)) ip) \<le> the (dhops (rt (\<sigma> sip)) ip))" ..
156
+ hence "sn < nsqn (rt (\<sigma>' sip)) ip
157
+ \<or> (nsqn (rt (\<sigma>' sip)) ip = sn \<and> (the (dhops (rt (\<sigma>' sip)) ip) \<le> hops
158
+ \<or> the (flag (rt (\<sigma>' sip)) ip) = inv))"
159
+ proof
160
+ assume "sn < nsqn (rt (\<sigma>' sip)) ip" thus ?thesis ..
161
+ next
162
+ assume "sn = nsqn (rt (\<sigma>' sip)) ip
163
+ \<and> the (dhops (rt (\<sigma> sip)) ip) \<ge> the (dhops (rt (\<sigma>' sip)) ip)"
164
+ hence "sn = nsqn (rt (\<sigma>' sip)) ip"
165
+ and "the (dhops (rt (\<sigma>' sip)) ip) \<le> the (dhops (rt (\<sigma> sip)) ip)" by auto
166
+
167
+ from * and \<open>sn = nsqn (rt (\<sigma> sip)) ip\<close> have "the (dhops (rt (\<sigma> sip)) ip) \<le> hops
168
+ \<or> the (flag (rt (\<sigma> sip)) ip) = inv"
169
+ by simp
170
+ thus ?thesis
171
+ proof
172
+ assume "the (dhops (rt (\<sigma> sip)) ip) \<le> hops"
173
+ with \<open>the (dhops (rt (\<sigma>' sip)) ip) \<le> the (dhops (rt (\<sigma> sip)) ip)\<close>
174
+ have "the (dhops (rt (\<sigma>' sip)) ip) \<le> hops" by simp
175
+ with \<open>sn = nsqn (rt (\<sigma>' sip)) ip\<close> show ?thesis by simp
176
+ next
177
+ assume "the (flag (rt (\<sigma> sip)) ip) = inv"
178
+ with \<open>ip\<in>kD(rt (\<sigma> sip))\<close> have "nsqn (rt (\<sigma> sip)) ip = sqn (rt (\<sigma> sip)) ip - 1" ..
179
+
180
+ with \<open>sn \<ge> 1\<close> and \<open>sn = nsqn (rt (\<sigma> sip)) ip\<close>
181
+ have "sqn (rt (\<sigma> sip)) ip > 1" by simp
182
+
183
+ from \<open>ip\<in>kD(rt (\<sigma>' sip))\<close> show ?thesis
184
+ proof (rule vD_or_iD)
185
+ assume "ip\<in>iD(rt (\<sigma>' sip))"
186
+ hence "the (flag (rt (\<sigma>' sip)) ip) = inv" ..
187
+ with \<open>sn = nsqn (rt (\<sigma>' sip)) ip\<close> show ?thesis
188
+ by simp
189
+ next
190
+ (* the tricky case: sn = nsqn (rt (\<sigma>' sip)) ip
191
+ \<and> ip\<in>iD(rt (\<sigma> sip))
192
+ \<and> ip\<in>vD(rt (\<sigma>' sip)) *)
193
+ assume "ip\<in>vD(rt (\<sigma>' sip))"
194
+ hence "nsqn (rt (\<sigma>' sip)) ip = sqn (rt (\<sigma>' sip)) ip" ..
195
+ with \<open>sqn (rt (\<sigma> sip)) ip \<le> sqn (rt (\<sigma>' sip)) ip\<close>
196
+ have "nsqn (rt (\<sigma>' sip)) ip \<ge> sqn (rt (\<sigma> sip)) ip" by simp
197
+
198
+ with \<open>sqn (rt (\<sigma> sip)) ip > 1\<close>
199
+ have "nsqn (rt (\<sigma>' sip)) ip > sqn (rt (\<sigma> sip)) ip - 1" by simp
200
+ with \<open>nsqn (rt (\<sigma> sip)) ip = sqn (rt (\<sigma> sip)) ip - 1\<close>
201
+ have "nsqn (rt (\<sigma>' sip)) ip > nsqn (rt (\<sigma> sip)) ip" by simp
202
+ with \<open>sn = nsqn (rt (\<sigma> sip)) ip\<close> have "nsqn (rt (\<sigma>' sip)) ip > sn"
203
+ by simp
204
+ thus ?thesis ..
205
+ qed
206
+ qed
207
+ qed
208
+ thus ?thesis by (metis (mono_tags) le_cases not_le)
209
+ qed
210
+ with \<open>ip\<in>kD (rt (\<sigma>' sip))\<close> show "ip\<in>kD (rt (\<sigma>' sip)) \<and> ?nsqnafter" ..
211
+ qed
212
+
213
+ lemma quality_increases_rreq_rrep_props':
214
+ fixes sn ip hops sip
215
+ assumes "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
216
+ and "1 \<le> sn"
217
+ and *: "ip\<in>kD(rt (\<sigma> sip)) \<and> sn \<le> nsqn (rt (\<sigma> sip)) ip
218
+ \<and> (nsqn (rt (\<sigma> sip)) ip = sn
219
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) ip) \<le> hops
220
+ \<or> the (flag (rt (\<sigma> sip)) ip) = inv))"
221
+ shows "ip\<in>kD(rt (\<sigma>' sip)) \<and> sn \<le> nsqn (rt (\<sigma>' sip)) ip
222
+ \<and> (nsqn (rt (\<sigma>' sip)) ip = sn
223
+ \<longrightarrow> (the (dhops (rt (\<sigma>' sip)) ip) \<le> hops
224
+ \<or> the (flag (rt (\<sigma>' sip)) ip) = inv))"
225
+ proof -
226
+ from assms(1) have "quality_increases (\<sigma> sip) (\<sigma>' sip)" ..
227
+ thus ?thesis using assms(2-3) by (rule quality_increases_rreq_rrep_props)
228
+ qed
229
+
230
+ lemma rteq_quality_increases:
231
+ assumes "\<forall>j. j \<noteq> i \<longrightarrow> quality_increases (\<sigma> j) (\<sigma>' j)"
232
+ and "rt (\<sigma>' i) = rt (\<sigma> i)"
233
+ shows "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
234
+ using assms by clarsimp (metis order_refl quality_increasesI rt_fresher_refl)
235
+
236
+ definition msg_fresh :: "(ip \<Rightarrow> state) \<Rightarrow> msg \<Rightarrow> bool"
237
+ where "msg_fresh \<sigma> m \<equiv>
238
+ case m of Rreq hopsc _ _ _ oipc osnc sipc \<Rightarrow> osnc \<ge> 1 \<and> (sipc \<noteq> oipc \<longrightarrow>
239
+ oipc\<in>kD(rt (\<sigma> sipc)) \<and> nsqn (rt (\<sigma> sipc)) oipc \<ge> osnc
240
+ \<and> (nsqn (rt (\<sigma> sipc)) oipc = osnc
241
+ \<longrightarrow> (hopsc \<ge> the (dhops (rt (\<sigma> sipc)) oipc)
242
+ \<or> the (flag (rt (\<sigma> sipc)) oipc) = inv)))
243
+ | Rrep hopsc dipc dsnc _ sipc \<Rightarrow> dsnc \<ge> 1 \<and> (sipc \<noteq> dipc \<longrightarrow>
244
+ dipc\<in>kD(rt (\<sigma> sipc)) \<and> nsqn (rt (\<sigma> sipc)) dipc \<ge> dsnc
245
+ \<and> (nsqn (rt (\<sigma> sipc)) dipc = dsnc
246
+ \<longrightarrow> (hopsc \<ge> the (dhops (rt (\<sigma> sipc)) dipc)
247
+ \<or> the (flag (rt (\<sigma> sipc)) dipc) = inv)))
248
+ | Rerr destsc sipc \<Rightarrow> (\<forall>ripc\<in>dom(destsc). (ripc\<in>kD(rt (\<sigma> sipc))
249
+ \<and> the (destsc ripc) - 1 \<le> nsqn (rt (\<sigma> sipc)) ripc))
250
+ | _ \<Rightarrow> True"
251
+
252
+ lemma msg_fresh [simp]:
253
+ "\<And>hops dip dsn dsk oip osn sip.
254
+ msg_fresh \<sigma> (Rreq hops dip dsn dsk oip osn sip) =
255
+ (osn \<ge> 1 \<and> (sip \<noteq> oip \<longrightarrow> oip\<in>kD(rt (\<sigma> sip))
256
+ \<and> nsqn (rt (\<sigma> sip)) oip \<ge> osn
257
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
258
+ \<longrightarrow> (hops \<ge> the (dhops (rt (\<sigma> sip)) oip)
259
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv))))"
260
+ "\<And>hops dip dsn oip sip. msg_fresh \<sigma> (Rrep hops dip dsn oip sip) =
261
+ (dsn \<ge> 1 \<and> (sip \<noteq> dip \<longrightarrow> dip\<in>kD(rt (\<sigma> sip))
262
+ \<and> nsqn (rt (\<sigma> sip)) dip \<ge> dsn
263
+ \<and> (nsqn (rt (\<sigma> sip)) dip = dsn
264
+ \<longrightarrow> (hops \<ge> the (dhops (rt (\<sigma> sip)) dip))
265
+ \<or> the (flag (rt (\<sigma> sip)) dip) = inv)))"
266
+ "\<And>dests sip. msg_fresh \<sigma> (Rerr dests sip) =
267
+ (\<forall>ripc\<in>dom(dests). (ripc\<in>kD(rt (\<sigma> sip))
268
+ \<and> the (dests ripc) - 1 \<le> nsqn (rt (\<sigma> sip)) ripc))"
269
+ "\<And>d dip. msg_fresh \<sigma> (Newpkt d dip) = True"
270
+ "\<And>d dip sip. msg_fresh \<sigma> (Pkt d dip sip) = True"
271
+ unfolding msg_fresh_def by simp_all
272
+
273
+ lemma msg_fresh_inc_sn [simp, elim]:
274
+ "msg_fresh \<sigma> m \<Longrightarrow> rreq_rrep_sn m"
275
+ by (cases m) simp_all
276
+
277
+ lemma recv_msg_fresh_inc_sn [simp, elim]:
278
+ "orecvmsg (msg_fresh) \<sigma> m \<Longrightarrow> recvmsg rreq_rrep_sn m"
279
+ by (cases m) simp_all
280
+
281
+ lemma rreq_nsqn_is_fresh [simp]:
282
+ fixes \<sigma> msg hops dip dsn dsk oip osn sip
283
+ assumes "rreq_rrep_fresh (rt (\<sigma> sip)) (Rreq hops dip dsn dsk oip osn sip)"
284
+ and "rreq_rrep_sn (Rreq hops dip dsn dsk oip osn sip)"
285
+ shows "msg_fresh \<sigma> (Rreq hops dip dsn dsk oip osn sip)"
286
+ (is "msg_fresh \<sigma> ?msg")
287
+ proof -
288
+ let ?rt = "rt (\<sigma> sip)"
289
+ from assms(2) have "1 \<le> osn" by simp
290
+ thus ?thesis
291
+ unfolding msg_fresh_def
292
+ proof (simp only: msg.case, intro conjI impI)
293
+ assume "sip \<noteq> oip"
294
+ with assms(1) show "oip \<in> kD(?rt)" by simp
295
+ next
296
+ assume "sip \<noteq> oip"
297
+ and "nsqn ?rt oip = osn"
298
+ show "the (dhops ?rt oip) \<le> hops \<or> the (flag ?rt oip) = inv"
299
+ proof (cases "oip\<in>vD(?rt)")
300
+ assume "oip\<in>vD(?rt)"
301
+ hence "nsqn ?rt oip = sqn ?rt oip" ..
302
+ with \<open>nsqn ?rt oip = osn\<close> have "sqn ?rt oip = osn" by simp
303
+ with assms(1) and \<open>sip \<noteq> oip\<close> have "the (dhops ?rt oip) \<le> hops"
304
+ by simp
305
+ thus ?thesis ..
306
+ next
307
+ assume "oip\<notin>vD(?rt)"
308
+ moreover from assms(1) and \<open>sip \<noteq> oip\<close> have "oip\<in>kD(?rt)" by simp
309
+ ultimately have "oip\<in>iD(?rt)" by auto
310
+ hence "the (flag ?rt oip) = inv" ..
311
+ thus ?thesis ..
312
+ qed
313
+ next
314
+ assume "sip \<noteq> oip"
315
+ with assms(1) have "osn \<le> sqn ?rt oip" by auto
316
+ thus "osn \<le> nsqn (rt (\<sigma> sip)) oip"
317
+ proof (rule nat_le_eq_or_lt)
318
+ assume "osn < sqn ?rt oip"
319
+ hence "osn \<le> sqn ?rt oip - 1" by simp
320
+ also have "... \<le> nsqn ?rt oip" by (rule sqn_nsqn)
321
+ finally show "osn \<le> nsqn ?rt oip" .
322
+ next
323
+ assume "osn = sqn ?rt oip"
324
+ with assms(1) and \<open>sip \<noteq> oip\<close> have "oip\<in>kD(?rt)"
325
+ and "the (flag ?rt oip) = val"
326
+ by auto
327
+ hence "nsqn ?rt oip = sqn ?rt oip" ..
328
+ with \<open>osn = sqn ?rt oip\<close> have "nsqn ?rt oip = osn" by simp
329
+ thus "osn \<le> nsqn ?rt oip" by simp
330
+ qed
331
+ qed simp
332
+ qed
333
+
334
+ lemma rrep_nsqn_is_fresh [simp]:
335
+ fixes \<sigma> msg hops dip dsn oip sip
336
+ assumes "rreq_rrep_fresh (rt (\<sigma> sip)) (Rrep hops dip dsn oip sip)"
337
+ and "rreq_rrep_sn (Rrep hops dip dsn oip sip)"
338
+ shows "msg_fresh \<sigma> (Rrep hops dip dsn oip sip)"
339
+ (is "msg_fresh \<sigma> ?msg")
340
+ proof -
341
+ let ?rt = "rt (\<sigma> sip)"
342
+ from assms have "sip \<noteq> dip \<longrightarrow> dip\<in>kD(?rt) \<and> sqn ?rt dip = dsn \<and> the (flag ?rt dip) = val"
343
+ by simp
344
+ hence "sip \<noteq> dip \<longrightarrow> dip\<in>kD(?rt) \<and> nsqn ?rt dip \<ge> dsn"
345
+ by clarsimp
346
+ with assms show "msg_fresh \<sigma> ?msg"
347
+ by clarsimp
348
+ qed
349
+
350
+ lemma rerr_nsqn_is_fresh [simp]:
351
+ fixes \<sigma> msg dests sip
352
+ assumes "rerr_invalid (rt (\<sigma> sip)) (Rerr dests sip)"
353
+ shows "msg_fresh \<sigma> (Rerr dests sip)"
354
+ (is "msg_fresh \<sigma> ?msg")
355
+ proof -
356
+ let ?rt = "rt (\<sigma> sip)"
357
+ from assms have *: "(\<forall>rip\<in>dom(dests). (rip\<in>iD(rt (\<sigma> sip))
358
+ \<and> the (dests rip) = sqn (rt (\<sigma> sip)) rip))"
359
+ by clarsimp
360
+ have "(\<forall>rip\<in>dom(dests). (rip\<in>kD(rt (\<sigma> sip))
361
+ \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip))"
362
+ proof
363
+ fix rip
364
+ assume "rip \<in> dom dests"
365
+ with * have "rip\<in>iD(rt (\<sigma> sip))" and "the (dests rip) = sqn (rt (\<sigma> sip)) rip"
366
+ by auto
367
+
368
+ from this(2) have "the (dests rip) - 1 = sqn (rt (\<sigma> sip)) rip - 1" by simp
369
+ also have "... \<le> nsqn (rt (\<sigma> sip)) rip" by (rule sqn_nsqn)
370
+ finally have "the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip" .
371
+
372
+ with \<open>rip\<in>iD(rt (\<sigma> sip))\<close>
373
+ show "rip\<in>kD(rt (\<sigma> sip)) \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
374
+ by clarsimp
375
+ qed
376
+ thus "msg_fresh \<sigma> ?msg"
377
+ by simp
378
+ qed
379
+
380
+ lemma quality_increases_msg_fresh [elim]:
381
+ assumes qinc: "\<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)"
382
+ and "msg_fresh \<sigma> m"
383
+ shows "msg_fresh \<sigma>' m"
384
+ using assms(2)
385
+ proof (cases m)
386
+ fix hops rreqid dip dsn dsk oip osn sip
387
+ assume [simp]: "m = Rreq hops dip dsn dsk oip osn sip"
388
+ and "msg_fresh \<sigma> m"
389
+ then have "osn \<ge> 1" and "sip = oip \<or> (oip\<in>kD(rt (\<sigma> sip)) \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
390
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
391
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) oip) \<le> hops
392
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv)))"
393
+ by auto
394
+ from this(2) show ?thesis
395
+ proof
396
+ assume "sip = oip" with \<open>osn \<ge> 1\<close> show ?thesis by simp
397
+ next
398
+ assume "oip\<in>kD(rt (\<sigma> sip)) \<and> osn \<le> nsqn (rt (\<sigma> sip)) oip
399
+ \<and> (nsqn (rt (\<sigma> sip)) oip = osn
400
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) oip) \<le> hops
401
+ \<or> the (flag (rt (\<sigma> sip)) oip) = inv))"
402
+ moreover from qinc have "quality_increases (\<sigma> sip) (\<sigma>' sip)" ..
403
+ ultimately have "oip\<in>kD(rt (\<sigma>' sip)) \<and> osn \<le> nsqn (rt (\<sigma>' sip)) oip
404
+ \<and> (nsqn (rt (\<sigma>' sip)) oip = osn
405
+ \<longrightarrow> (the (dhops (rt (\<sigma>' sip)) oip) \<le> hops
406
+ \<or> the (flag (rt (\<sigma>' sip)) oip) = inv))"
407
+ using \<open>osn \<ge> 1\<close> by (rule quality_increases_rreq_rrep_props [rotated 2])
408
+ with \<open>osn \<ge> 1\<close> show "msg_fresh \<sigma>' m"
409
+ by (clarsimp)
410
+ qed
411
+ next
412
+ fix hops dip dsn oip sip
413
+ assume [simp]: "m = Rrep hops dip dsn oip sip"
414
+ and "msg_fresh \<sigma> m"
415
+ then have "dsn \<ge> 1" and "sip = dip \<or> (dip\<in>kD(rt (\<sigma> sip)) \<and> dsn \<le> nsqn (rt (\<sigma> sip)) dip
416
+ \<and> (nsqn (rt (\<sigma> sip)) dip = dsn
417
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) dip) \<le> hops
418
+ \<or> the (flag (rt (\<sigma> sip)) dip) = inv)))"
419
+ by auto
420
+ from this(2) show "?thesis"
421
+ proof
422
+ assume "sip = dip" with \<open>dsn \<ge> 1\<close> show ?thesis by simp
423
+ next
424
+ assume "dip\<in>kD(rt (\<sigma> sip)) \<and> dsn \<le> nsqn (rt (\<sigma> sip)) dip
425
+ \<and> (nsqn (rt (\<sigma> sip)) dip = dsn
426
+ \<longrightarrow> (the (dhops (rt (\<sigma> sip)) dip) \<le> hops
427
+ \<or> the (flag (rt (\<sigma> sip)) dip) = inv))"
428
+ moreover from qinc have "quality_increases (\<sigma> sip) (\<sigma>' sip)" ..
429
+ ultimately have "dip\<in>kD(rt (\<sigma>' sip)) \<and> dsn \<le> nsqn (rt (\<sigma>' sip)) dip
430
+ \<and> (nsqn (rt (\<sigma>' sip)) dip = dsn
431
+ \<longrightarrow> (the (dhops (rt (\<sigma>' sip)) dip) \<le> hops
432
+ \<or> the (flag (rt (\<sigma>' sip)) dip) = inv))"
433
+ using \<open>dsn \<ge> 1\<close> by (rule quality_increases_rreq_rrep_props [rotated 2])
434
+ with \<open>dsn \<ge> 1\<close> show "msg_fresh \<sigma>' m"
435
+ by clarsimp
436
+ qed
437
+ next
438
+ fix dests sip
439
+ assume [simp]: "m = Rerr dests sip"
440
+ and "msg_fresh \<sigma> m"
441
+ then have *: "\<forall>rip\<in>dom(dests). rip\<in>kD(rt (\<sigma> sip))
442
+ \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
443
+ by simp
444
+ have "\<forall>rip\<in>dom(dests). rip\<in>kD(rt (\<sigma>' sip))
445
+ \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma>' sip)) rip"
446
+ proof
447
+ fix rip
448
+ assume "rip\<in>dom(dests)"
449
+ with * have "rip\<in>kD(rt (\<sigma> sip))" and "the (dests rip) - 1 \<le> nsqn (rt (\<sigma> sip)) rip"
450
+ by - (drule(1) bspec, clarsimp)+
451
+ moreover from qinc have "quality_increases (\<sigma> sip) (\<sigma>' sip)" by simp
452
+ ultimately show "rip\<in>kD(rt (\<sigma>' sip)) \<and> the (dests rip) - 1 \<le> nsqn (rt (\<sigma>' sip)) rip" ..
453
+ qed
454
+ thus ?thesis by simp
455
+ qed simp_all
456
+
457
+ end
formal/afp/AODV/variants/a_norreqid/A_Seq_Invariants.thy ADDED
@@ -0,0 +1,643 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/a_norreqid/Seq_Invariants.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Invariant proofs on individual processes"
7
+
8
+ theory A_Seq_Invariants
9
+ imports AWN.Invariants A_Aodv A_Aodv_Data A_Aodv_Predicates A_Fresher
10
+
11
+ begin
12
+
13
+ text \<open>
14
+ The proposition numbers are taken from the December 2013 version of
15
+ the Fehnker et al technical report.
16
+ \<close>
17
+
18
+ text \<open>Proposition 7.2\<close>
19
+
20
+ lemma sequence_number_increases:
21
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). sn \<xi> \<le> sn \<xi>')"
22
+ by inv_cterms
23
+
24
+ lemma sequence_number_one_or_bigger:
25
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). 1 \<le> sn \<xi>)"
26
+ by (rule onll_step_to_invariantI [OF sequence_number_increases])
27
+ (auto simp: \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def)
28
+
29
+ text \<open>We can get rid of the onl/onll if desired...\<close>
30
+
31
+ lemma sequence_number_increases':
32
+ "paodv i \<TTurnstile>\<^sub>A (\<lambda>((\<xi>, _), _, (\<xi>', _)). sn \<xi> \<le> sn \<xi>')"
33
+ by (rule step_invariant_weakenE [OF sequence_number_increases]) (auto dest!: onllD)
34
+
35
+ lemma sequence_number_one_or_bigger':
36
+ "paodv i \<TTurnstile> (\<lambda>(\<xi>, _). 1 \<le> sn \<xi>)"
37
+ by (rule invariant_weakenE [OF sequence_number_one_or_bigger]) auto
38
+
39
+ lemma sip_in_kD:
40
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). l \<in> ({PAodv-:7} \<union> {PAodv-:5} \<union> {PRrep-:0..PRrep-:1}
41
+ \<union> {PRreq-:0..PRreq-:3}) \<longrightarrow> sip \<xi> \<in> kD (rt \<xi>))"
42
+ by inv_cterms
43
+
44
+ lemma rrep_1_update_changes:
45
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l = PRrep-:1 \<longrightarrow>
46
+ rt \<xi> \<noteq> update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {})))"
47
+ by inv_cterms
48
+
49
+ lemma addpreRT_partly_welldefined:
50
+ "paodv i \<TTurnstile>
51
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PRreq-:16..PRreq-:18} \<union> {PRrep-:2..PRrep-:6} \<longrightarrow> dip \<xi> \<in> kD (rt \<xi>))
52
+ \<and> (l \<in> {PRreq-:3..PRreq-:17} \<longrightarrow> oip \<xi> \<in> kD (rt \<xi>)))"
53
+ by inv_cterms
54
+
55
+ text \<open>Proposition 7.38\<close>
56
+
57
+ lemma includes_nhip:
58
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). \<forall>dip\<in>kD(rt \<xi>). the (nhop (rt \<xi>) dip)\<in>kD(rt \<xi>))"
59
+ proof -
60
+ { fix ip and \<xi> \<xi>' :: state
61
+ assume "\<forall>dip\<in>kD (rt \<xi>). the (nhop (rt \<xi>) dip) \<in> kD (rt \<xi>)"
62
+ and "\<xi>' = \<xi>\<lparr>rt := update (rt \<xi>) ip (0, unk, val, Suc 0, ip, {})\<rparr>"
63
+ hence "\<forall>dip\<in>kD (rt \<xi>).
64
+ the (nhop (update (rt \<xi>) ip (0, unk, val, Suc 0, ip, {})) dip) = ip
65
+ \<or> the (nhop (update (rt \<xi>) ip (0, unk, val, Suc 0, ip, {})) dip) \<in> kD (rt \<xi>)"
66
+ by clarsimp (metis nhop_update_unk_val update_another)
67
+ } note one_hop = this
68
+ { fix ip sip sn hops and \<xi> \<xi>' :: state
69
+ assume "\<forall>dip\<in>kD (rt \<xi>). the (nhop (rt \<xi>) dip) \<in> kD (rt \<xi>)"
70
+ and "\<xi>' = \<xi>\<lparr>rt := update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})\<rparr>"
71
+ and "sip \<in> kD (rt \<xi>)"
72
+ hence "(the (nhop (update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})) ip) = ip
73
+ \<or> the (nhop (update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})) ip) \<in> kD (rt \<xi>))
74
+ \<and> (\<forall>dip\<in>kD (rt \<xi>).
75
+ the (nhop (update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})) dip) = ip
76
+ \<or> the (nhop (update (rt \<xi>) ip (sn, kno, val, Suc hops, sip, {})) dip) \<in> kD (rt \<xi>))"
77
+ by (metis kD_update_unchanged nhop_update_changed update_another)
78
+ } note nhip_is_sip = this
79
+ show ?thesis
80
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf sip_in_kD]
81
+ onl_invariant_sterms [OF aodv_wf addpreRT_partly_welldefined]
82
+ solve: one_hop nhip_is_sip)
83
+ qed
84
+
85
+ text \<open>Proposition 7.22: needed in Proposition 7.4\<close>
86
+
87
+ lemma addpreRT_welldefined:
88
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PRreq-:16..PRreq-:18} \<longrightarrow> dip \<xi> \<in> kD (rt \<xi>)) \<and>
89
+ (l = PRreq-:17 \<longrightarrow> oip \<xi> \<in> kD (rt \<xi>)) \<and>
90
+ (l = PRrep-:5 \<longrightarrow> dip \<xi> \<in> kD (rt \<xi>)) \<and>
91
+ (l = PRrep-:6 \<longrightarrow> (the (nhop (rt \<xi>) (dip \<xi>))) \<in> kD (rt \<xi>)))"
92
+ (is "_ \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V ?P")
93
+ unfolding invariant_def
94
+ proof
95
+ fix s
96
+ assume "s \<in> reachable (paodv i) TT"
97
+ then obtain \<xi> p where "s = (\<xi>, p)"
98
+ and "(\<xi>, p) \<in> reachable (paodv i) TT"
99
+ by (metis prod.exhaust)
100
+ have "onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V ?P (\<xi>, p)"
101
+ proof (rule onlI)
102
+ fix l
103
+ assume "l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
104
+ with \<open>(\<xi>, p) \<in> reachable (paodv i) TT\<close>
105
+ have I1: "l \<in> {PRreq-:16..PRreq-:18} \<longrightarrow> dip \<xi> \<in> kD(rt \<xi>)"
106
+ and I2: "l = PRreq-:17 \<longrightarrow> oip \<xi> \<in> kD(rt \<xi>)"
107
+ and I3: "l \<in> {PRrep-:2..PRrep-:6} \<longrightarrow> dip \<xi> \<in> kD(rt \<xi>)"
108
+ by (auto dest!: invariantD [OF addpreRT_partly_welldefined])
109
+ moreover from \<open>(\<xi>, p) \<in> reachable (paodv i) TT\<close> \<open>l \<in> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p\<close> and I3
110
+ have "l = PRrep-:6 \<longrightarrow> (the (nhop (rt \<xi>) (dip \<xi>))) \<in> kD(rt \<xi>)"
111
+ by (auto dest!: invariantD [OF includes_nhip])
112
+ ultimately show "?P (\<xi>, l)"
113
+ by simp
114
+ qed
115
+ with \<open>s = (\<xi>, p)\<close> show "onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V ?P s"
116
+ by simp
117
+ qed
118
+
119
+ text \<open>Proposition 7.4\<close>
120
+
121
+ lemma known_destinations_increase:
122
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). kD (rt \<xi>) \<subseteq> kD (rt \<xi>'))"
123
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf addpreRT_welldefined]
124
+ simp add: subset_insertI)
125
+
126
+ text \<open>Proposition 7.5\<close>
127
+
128
+ lemma rreqs_increase:
129
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). rreqs \<xi> \<subseteq> rreqs \<xi>')"
130
+ by (inv_cterms simp add: subset_insertI)
131
+
132
+ lemma dests_bigger_than_sqn:
133
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). l \<in> {PAodv-:15..PAodv-:19}
134
+ \<union> {PPkt-:7..PPkt-:11}
135
+ \<union> {PRreq-:9..PRreq-:13}
136
+ \<union> {PRreq-:21..PRreq-:25}
137
+ \<union> {PRrep-:10..PRrep-:14}
138
+ \<union> {PRerr-:1..PRerr-:5}
139
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>kD(rt \<xi>) \<and> sqn (rt \<xi>) ip \<le> the (dests \<xi> ip)))"
140
+ proof -
141
+ have sqninv:
142
+ "\<And>dests rt rsn ip.
143
+ \<lbrakk> \<forall>ip\<in>dom(dests). ip\<in>kD(rt) \<and> sqn rt ip \<le> the (dests ip); dests ip = Some rsn \<rbrakk>
144
+ \<Longrightarrow> sqn (invalidate rt dests) ip \<le> rsn"
145
+ by (rule sqn_invalidate_in_dests [THEN eq_imp_le], assumption) auto
146
+ have indests:
147
+ "\<And>dests rt rsn ip.
148
+ \<lbrakk> \<forall>ip\<in>dom(dests). ip\<in>kD(rt) \<and> sqn rt ip \<le> the (dests ip); dests ip = Some rsn \<rbrakk>
149
+ \<Longrightarrow> ip\<in>kD(rt) \<and> sqn rt ip \<le> rsn"
150
+ by (metis domI option.sel)
151
+ show ?thesis
152
+ by inv_cterms
153
+ (clarsimp split: if_split_asm option.split_asm
154
+ elim!: sqninv indests)+
155
+ qed
156
+
157
+ text \<open>Proposition 7.6\<close>
158
+
159
+ lemma sqns_increase:
160
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)). \<forall>ip. sqn (rt \<xi>) ip \<le> sqn (rt \<xi>') ip)"
161
+ proof -
162
+ { fix \<xi> :: state
163
+ assume *: "\<forall>ip\<in>dom(dests \<xi>). ip \<in> kD (rt \<xi>) \<and> sqn (rt \<xi>) ip \<le> the (dests \<xi> ip)"
164
+ have "\<forall>ip. sqn (rt \<xi>) ip \<le> sqn (invalidate (rt \<xi>) (dests \<xi>)) ip"
165
+ proof
166
+ fix ip
167
+ from * have "ip\<notin>dom(dests \<xi>) \<or> sqn (rt \<xi>) ip \<le> the (dests \<xi> ip)" by simp
168
+ thus "sqn (rt \<xi>) ip \<le> sqn (invalidate (rt \<xi>) (dests \<xi>)) ip"
169
+ by (metis domI invalidate_sqn option.sel)
170
+ qed
171
+ } note solve_invalidate = this
172
+ show ?thesis
173
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf addpreRT_welldefined]
174
+ onl_invariant_sterms [OF aodv_wf dests_bigger_than_sqn]
175
+ simp add: solve_invalidate)
176
+ qed
177
+
178
+ text \<open>Proposition 7.7\<close>
179
+
180
+ lemma ip_constant:
181
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). ip \<xi> = i)"
182
+ by (inv_cterms simp add: \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def)
183
+
184
+ text \<open>Proposition 7.8\<close>
185
+
186
+ lemma sender_ip_valid':
187
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), a, _). anycast (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m = ip \<xi>) a)"
188
+ by inv_cterms
189
+
190
+ lemma sender_ip_valid:
191
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), a, _). anycast (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m = i) a)"
192
+ by (rule step_invariant_weaken_with_invariantE [OF ip_constant sender_ip_valid'])
193
+ (auto dest!: onlD onllD)
194
+
195
+ lemma received_msg_inv:
196
+ "paodv i \<TTurnstile> (recvmsg P \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). l \<in> {PAodv-:1} \<longrightarrow> P (msg \<xi>))"
197
+ by inv_cterms
198
+
199
+ text \<open>Proposition 7.9\<close>
200
+
201
+ lemma sip_not_ip':
202
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m \<noteq> i) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). sip \<xi> \<noteq> ip \<xi>)"
203
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]
204
+ onl_invariant_sterms [OF aodv_wf ip_constant [THEN invariant_restrict_inD]]
205
+ simp add: clear_locals_sip_not_ip') clarsimp+
206
+
207
+ lemma sip_not_ip:
208
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. not_Pkt m \<longrightarrow> msg_sender m \<noteq> i) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). sip \<xi> \<noteq> i)"
209
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]
210
+ onl_invariant_sterms [OF aodv_wf ip_constant [THEN invariant_restrict_inD]]
211
+ simp add: clear_locals_sip_not_ip') clarsimp+
212
+
213
+ text \<open>Neither \<open>sip_not_ip'\<close> nor \<open>sip_not_ip\<close> is needed to show loop freedom.\<close>
214
+
215
+ text \<open>Proposition 7.10\<close>
216
+
217
+ lemma hop_count_positive:
218
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _). \<forall>ip\<in>kD (rt \<xi>). the (dhops (rt \<xi>) ip) \<ge> 1)"
219
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf addpreRT_welldefined]) auto
220
+
221
+ lemma rreq_dip_in_vD_dip_eq_ip:
222
+ "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PRreq-:16..PRreq-:18} \<longrightarrow> dip \<xi> \<in> vD(rt \<xi>))
223
+ \<and> (l \<in> {PRreq-:5, PRreq-:6} \<longrightarrow> dip \<xi> = ip \<xi>)
224
+ \<and> (l \<in> {PRreq-:15..PRreq-:18} \<longrightarrow> dip \<xi> \<noteq> ip \<xi>))"
225
+ proof (inv_cterms, elim conjE)
226
+ fix l \<xi> pp p'
227
+ assume "(\<xi>, pp) \<in> reachable (paodv i) TT"
228
+ and "{PRreq-:17}\<lbrakk>\<lambda>\<xi>. \<xi>\<lparr>rt := the (addpreRT (rt \<xi>) (oip \<xi>) {the (nhop (rt \<xi>) (dip \<xi>))})\<rparr>\<rbrakk> p'
229
+ \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
230
+ and "l = PRreq-:17"
231
+ and "dip \<xi> \<in> vD (rt \<xi>)"
232
+ from this(1-3) have "oip \<xi> \<in> kD (rt \<xi>)"
233
+ by (auto dest: onl_invariant_sterms [OF aodv_wf addpreRT_welldefined, where l="PRreq-:17"])
234
+ with \<open>dip \<xi> \<in> vD (rt \<xi>)\<close>
235
+ show "dip \<xi> \<in> vD (the (addpreRT (rt \<xi>) (oip \<xi>) {the (nhop (rt \<xi>) (dip \<xi>))}))" by simp
236
+ qed
237
+
238
+ text \<open>Proposition 7.11\<close>
239
+
240
+ lemma anycast_msg_zhops:
241
+ "\<And>rreqid dip dsn dsk oip osn sip.
242
+ paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast msg_zhops a)"
243
+ proof (inv_cterms inv add:
244
+ onl_invariant_sterms [OF aodv_wf rreq_dip_in_vD_dip_eq_ip [THEN invariant_restrict_inD]]
245
+ onl_invariant_sterms [OF aodv_wf hop_count_positive [THEN invariant_restrict_inD]],
246
+ elim conjE)
247
+ fix l \<xi> a pp p' pp'
248
+ assume "(\<xi>, pp) \<in> reachable (paodv i) TT"
249
+ and "{PRreq-:18}unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)),
250
+ \<lambda>\<xi>. Rrep (the (dhops (rt \<xi>) (dip \<xi>))) (dip \<xi>) (sqn (rt \<xi>) (dip \<xi>)) (oip \<xi>) (ip \<xi>)).
251
+ p' \<triangleright> pp' \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
252
+ and "l = PRreq-:18"
253
+ and "a = unicast (the (nhop (rt \<xi>) (oip \<xi>)))
254
+ (Rrep (the (dhops (rt \<xi>) (dip \<xi>))) (dip \<xi>) (sqn (rt \<xi>) (dip \<xi>)) (oip \<xi>) (ip \<xi>))"
255
+ and *: "\<forall>ip\<in>kD (rt \<xi>). Suc 0 \<le> the (dhops (rt \<xi>) ip)"
256
+ and "dip \<xi> \<in> vD (rt \<xi>)"
257
+ from \<open>dip \<xi> \<in> vD (rt \<xi>)\<close> have "dip \<xi> \<in> kD (rt \<xi>)"
258
+ by (rule vD_iD_gives_kD(1))
259
+ with * have "Suc 0 \<le> the (dhops (rt \<xi>) (dip \<xi>))" ..
260
+ thus "0 < the (dhops (rt \<xi>) (dip \<xi>))" by simp
261
+ qed
262
+
263
+ lemma hop_count_zero_oip_dip_sip:
264
+ "paodv i \<TTurnstile> (recvmsg msg_zhops \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
265
+ (l\<in>{PAodv-:4..PAodv-:5} \<union> {PRreq-:n|n. True} \<longrightarrow>
266
+ (hops \<xi> = 0 \<longrightarrow> oip \<xi> = sip \<xi>))
267
+ \<and>
268
+ ((l\<in>{PAodv-:6..PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow>
269
+ (hops \<xi> = 0 \<longrightarrow> dip \<xi> = sip \<xi>))))"
270
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]) auto
271
+
272
+ lemma osn_rreq:
273
+ "paodv i \<TTurnstile> (recvmsg rreq_rrep_sn \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
274
+ l \<in> {PAodv-:4, PAodv-:5} \<union> {PRreq-:n|n. True} \<longrightarrow> 1 \<le> osn \<xi>)"
275
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]) clarsimp
276
+
277
+ lemma osn_rreq':
278
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
279
+ l \<in> {PAodv-:4, PAodv-:5} \<union> {PRreq-:n|n. True} \<longrightarrow> 1 \<le> osn \<xi>)"
280
+ proof (rule invariant_weakenE [OF osn_rreq])
281
+ fix a
282
+ assume "recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) a"
283
+ thus "recvmsg rreq_rrep_sn a"
284
+ by (cases a) simp_all
285
+ qed
286
+
287
+ lemma dsn_rrep:
288
+ "paodv i \<TTurnstile> (recvmsg rreq_rrep_sn \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
289
+ l \<in> {PAodv-:6, PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow> 1 \<le> dsn \<xi>)"
290
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf received_msg_inv]) clarsimp
291
+
292
+ lemma dsn_rrep':
293
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
294
+ l \<in> {PAodv-:6, PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow> 1 \<le> dsn \<xi>)"
295
+ proof (rule invariant_weakenE [OF dsn_rrep])
296
+ fix a
297
+ assume "recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) a"
298
+ thus "recvmsg rreq_rrep_sn a"
299
+ by (cases a) simp_all
300
+ qed
301
+
302
+ lemma hop_count_zero_oip_dip_sip':
303
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
304
+ (l\<in>{PAodv-:4..PAodv-:5} \<union> {PRreq-:n|n. True} \<longrightarrow>
305
+ (hops \<xi> = 0 \<longrightarrow> oip \<xi> = sip \<xi>))
306
+ \<and>
307
+ ((l\<in>{PAodv-:6..PAodv-:7} \<union> {PRrep-:n|n. True} \<longrightarrow>
308
+ (hops \<xi> = 0 \<longrightarrow> dip \<xi> = sip \<xi>))))"
309
+ proof (rule invariant_weakenE [OF hop_count_zero_oip_dip_sip])
310
+ fix a
311
+ assume "recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) a"
312
+ thus "recvmsg msg_zhops a"
313
+ by (cases a) simp_all
314
+ qed
315
+
316
+ text \<open>Proposition 7.12\<close>
317
+
318
+ lemma zero_seq_unk_hops_one':
319
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _).
320
+ \<forall>dip\<in>kD(rt \<xi>). (sqn (rt \<xi>) dip = 0 \<longrightarrow> sqnf (rt \<xi>) dip = unk)
321
+ \<and> (sqnf (rt \<xi>) dip = unk \<longrightarrow> the (dhops (rt \<xi>) dip) = 1)
322
+ \<and> (the (dhops (rt \<xi>) dip) = 1 \<longrightarrow> the (nhop (rt \<xi>) dip) = dip))"
323
+ proof -
324
+ { fix dip and \<xi> :: state and P
325
+ assume "sqn (invalidate (rt \<xi>) (dests \<xi>)) dip = 0"
326
+ and all: "\<forall>ip. sqn (rt \<xi>) ip \<le> sqn (invalidate (rt \<xi>) (dests \<xi>)) ip"
327
+ and *: "sqn (rt \<xi>) dip = 0 \<Longrightarrow> P \<xi> dip"
328
+ have "P \<xi> dip"
329
+ proof -
330
+ from all have "sqn (rt \<xi>) dip \<le> sqn (invalidate (rt \<xi>) (dests \<xi>)) dip" ..
331
+ with \<open>sqn (invalidate (rt \<xi>) (dests \<xi>)) dip = 0\<close> have "sqn (rt \<xi>) dip = 0" by simp
332
+ thus "P \<xi> dip" by (rule *)
333
+ qed
334
+ } note sqn_invalidate_zero [elim!] = this
335
+
336
+ { fix dsn hops :: nat and sip oip rt and ip dip :: ip
337
+ assume "\<forall>dip\<in>kD(rt).
338
+ (sqn rt dip = 0 \<longrightarrow> \<pi>\<^sub>3(the (rt dip)) = unk) \<and>
339
+ (\<pi>\<^sub>3(the (rt dip)) = unk \<longrightarrow> the (dhops rt dip) = Suc 0) \<and>
340
+ (the (dhops rt dip) = Suc 0 \<longrightarrow> the (nhop rt dip) = dip)"
341
+ and "hops = 0 \<longrightarrow> sip = dip"
342
+ and "Suc 0 \<le> dsn"
343
+ and "ip \<noteq> dip \<longrightarrow> ip\<in>kD(rt)"
344
+ hence "the (dhops (update rt dip (dsn, kno, val, Suc hops, sip, {})) ip) = Suc 0 \<longrightarrow>
345
+ the (nhop (update rt dip (dsn, kno, val, Suc hops, sip, {})) ip) = ip"
346
+ by - (rule update_cases, auto simp add: sqn_def dest!: bspec)
347
+ } note prreq_ok1 [simp] = this
348
+
349
+ { fix ip dsn hops sip oip rt dip
350
+ assume "\<forall>dip\<in>kD(rt).
351
+ (sqn rt dip = 0 \<longrightarrow> \<pi>\<^sub>3(the (rt dip)) = unk) \<and>
352
+ (\<pi>\<^sub>3(the (rt dip)) = unk \<longrightarrow> the (dhops rt dip) = Suc 0) \<and>
353
+ (the (dhops rt dip) = Suc 0 \<longrightarrow> the (nhop rt dip) = dip)"
354
+ and "Suc 0 \<le> dsn"
355
+ and "ip \<noteq> dip \<longrightarrow> ip\<in>kD(rt)"
356
+ hence "\<pi>\<^sub>3(the (update rt dip (dsn, kno, val, Suc hops, sip, {}) ip)) = unk \<longrightarrow>
357
+ the (dhops (update rt dip (dsn, kno, val, Suc hops, sip, {})) ip) = Suc 0"
358
+ by - (rule update_cases, auto simp add: sqn_def sqnf_def dest!: bspec)
359
+ } note prreq_ok2 [simp] = this
360
+
361
+ { fix ip dsn hops sip oip rt dip
362
+ assume "\<forall>dip\<in>kD(rt).
363
+ (sqn rt dip = 0 \<longrightarrow> \<pi>\<^sub>3(the (rt dip)) = unk) \<and>
364
+ (\<pi>\<^sub>3(the (rt dip)) = unk \<longrightarrow> the (dhops rt dip) = Suc 0) \<and>
365
+ (the (dhops rt dip) = Suc 0 \<longrightarrow> the (nhop rt dip) = dip)"
366
+ and "Suc 0 \<le> dsn"
367
+ and "ip \<noteq> dip \<longrightarrow> ip\<in>kD(rt)"
368
+ hence "sqn (update rt dip (dsn, kno, val, Suc hops, sip, {})) ip = 0 \<longrightarrow>
369
+ \<pi>\<^sub>3 (the (update rt dip (dsn, kno, val, Suc hops, sip, {}) ip)) = unk"
370
+ by - (rule update_cases, auto simp add: sqn_def dest!: bspec)
371
+ } note prreq_ok3 [simp] = this
372
+
373
+ { fix rt sip
374
+ assume "\<forall>dip\<in>kD rt.
375
+ (sqn rt dip = 0 \<longrightarrow> \<pi>\<^sub>3(the (rt dip)) = unk) \<and>
376
+ (\<pi>\<^sub>3(the (rt dip)) = unk \<longrightarrow> the (dhops rt dip) = Suc 0) \<and>
377
+ (the (dhops rt dip) = Suc 0 \<longrightarrow> the (nhop rt dip) = dip)"
378
+ hence "\<forall>dip\<in>kD rt.
379
+ (sqn (update rt sip (0, unk, val, Suc 0, sip, {})) dip = 0 \<longrightarrow>
380
+ \<pi>\<^sub>3(the (update rt sip (0, unk, val, Suc 0, sip, {}) dip)) = unk)
381
+ \<and> (\<pi>\<^sub>3(the (update rt sip (0, unk, val, Suc 0, sip, {}) dip)) = unk \<longrightarrow>
382
+ the (dhops (update rt sip (0, unk, val, Suc 0, sip, {})) dip) = Suc 0)
383
+ \<and> (the (dhops (update rt sip (0, unk, val, Suc 0, sip, {})) dip) = Suc 0 \<longrightarrow>
384
+ the (nhop (update rt sip (0, unk, val, Suc 0, sip, {})) dip) = dip)"
385
+ by - (rule update_cases, simp_all add: sqnf_def sqn_def)
386
+ } note prreq_ok4 [simp] = this
387
+
388
+ have prreq_ok5 [simp]: "\<And>sip rt.
389
+ \<pi>\<^sub>3(the (update rt sip (0, unk, val, Suc 0, sip, {}) sip)) = unk \<longrightarrow>
390
+ the (dhops (update rt sip (0, unk, val, Suc 0, sip, {})) sip) = Suc 0"
391
+ by (rule update_cases) simp_all
392
+
393
+ have prreq_ok6 [simp]: "\<And>sip rt.
394
+ sqn (update rt sip (0, unk, val, Suc 0, sip, {})) sip = 0 \<longrightarrow>
395
+ \<pi>\<^sub>3 (the (update rt sip (0, unk, val, Suc 0, sip, {}) sip)) = unk"
396
+ by (rule update_cases) simp_all
397
+
398
+ show ?thesis
399
+ by (inv_cterms inv add: onl_invariant_sterms_TT [OF aodv_wf addpreRT_welldefined]
400
+ onl_invariant_sterms [OF aodv_wf hop_count_zero_oip_dip_sip']
401
+ seq_step_invariant_sterms_TT [OF sqns_increase aodv_wf aodv_trans]
402
+ onl_invariant_sterms [OF aodv_wf osn_rreq']
403
+ onl_invariant_sterms [OF aodv_wf dsn_rrep']) clarsimp+
404
+ qed
405
+
406
+ lemma zero_seq_unk_hops_one:
407
+ "paodv i \<TTurnstile> (recvmsg (\<lambda>m. rreq_rrep_sn m \<and> msg_zhops m) \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, _).
408
+ \<forall>dip\<in>kD(rt \<xi>). (sqn (rt \<xi>) dip = 0 \<longrightarrow> (sqnf (rt \<xi>) dip = unk
409
+ \<and> the (dhops (rt \<xi>) dip) = 1
410
+ \<and> the (nhop (rt \<xi>) dip) = dip)))"
411
+ by (rule invariant_weakenE [OF zero_seq_unk_hops_one']) auto
412
+
413
+ lemma kD_unk_or_atleast_one:
414
+ "paodv i \<TTurnstile> (recvmsg rreq_rrep_sn \<rightarrow>) onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
415
+ \<forall>dip\<in>kD(rt \<xi>). \<pi>\<^sub>3(the (rt \<xi> dip)) = unk \<or> 1 \<le> \<pi>\<^sub>2(the (rt \<xi> dip)))"
416
+ proof -
417
+ { fix sip rt dsn1 dsn2 dsk1 dsk2 flag1 flag2 hops1 hops2 nhip1 nhip2 pre1 pre2
418
+ assume "dsk1 = unk \<or> Suc 0 \<le> dsn2"
419
+ hence "\<pi>\<^sub>3(the (update rt sip (dsn1, dsk1, flag1, hops1, nhip1, pre1) sip)) = unk
420
+ \<or> Suc 0 \<le> sqn (update rt sip (dsn2, dsk2, flag2, hops2, nhip2, pre2)) sip"
421
+ unfolding update_def by (cases "dsk1 =unk") (clarsimp split: option.split)+
422
+ } note fromsip [simp] = this
423
+
424
+ { fix dip sip rt dsn1 dsn2 dsk1 dsk2 flag1 flag2 hops1 hops2 nhip1 nhip2 pre1 pre2
425
+ assume allkd: "\<forall>dip\<in>kD(rt). \<pi>\<^sub>3(the (rt dip)) = unk \<or> Suc 0 \<le> sqn rt dip"
426
+ and **: "dsk1 = unk \<or> Suc 0 \<le> dsn2"
427
+ have "\<forall>dip\<in>kD(rt). \<pi>\<^sub>3(the (update rt sip (dsn1, dsk1, flag1, hops1, nhip1, pre1) dip)) = unk
428
+ \<or> Suc 0 \<le> sqn (update rt sip (dsn2, dsk2, flag2, hops2, nhip2, pre2)) dip"
429
+ (is "\<forall>dip\<in>kD(rt). ?prop dip")
430
+ proof
431
+ fix dip
432
+ assume "dip\<in>kD(rt)"
433
+ thus "?prop dip"
434
+ proof (cases "dip = sip")
435
+ assume "dip = sip"
436
+ with ** show ?thesis
437
+ by simp
438
+ next
439
+ assume "dip \<noteq> sip"
440
+ with \<open>dip\<in>kD(rt)\<close> allkd show ?thesis
441
+ by simp
442
+ qed
443
+ qed
444
+ } note solve_update [simp] = this
445
+
446
+ { fix dip rt dests
447
+ assume *: "\<forall>ip\<in>dom(dests). ip\<in>kD(rt) \<and> sqn rt ip \<le> the (dests ip)"
448
+ and **: "\<forall>ip\<in>kD(rt). \<pi>\<^sub>3(the (rt ip)) = unk \<or> Suc 0 \<le> sqn rt ip"
449
+ have "\<forall>dip\<in>kD(rt). \<pi>\<^sub>3(the (rt dip)) = unk \<or> Suc 0 \<le> sqn (invalidate rt dests) dip"
450
+ proof
451
+ fix dip
452
+ assume "dip\<in>kD(rt)"
453
+ with ** have "\<pi>\<^sub>3(the (rt dip)) = unk \<or> Suc 0 \<le> sqn rt dip" ..
454
+ thus "\<pi>\<^sub>3 (the (rt dip)) = unk \<or> Suc 0 \<le> sqn (invalidate rt dests) dip"
455
+ proof
456
+ assume "\<pi>\<^sub>3(the (rt dip)) = unk" thus ?thesis ..
457
+ next
458
+ assume "Suc 0 \<le> sqn rt dip"
459
+ have "Suc 0 \<le> sqn (invalidate rt dests) dip"
460
+ proof (cases "dip\<in>dom(dests)")
461
+ assume "dip\<in>dom(dests)"
462
+ with * have "sqn rt dip \<le> the (dests dip)" by simp
463
+ with \<open>Suc 0 \<le> sqn rt dip\<close> have "Suc 0 \<le> the (dests dip)" by simp
464
+ with \<open>dip\<in>dom(dests)\<close> \<open>dip\<in>kD(rt)\<close> [THEN kD_Some] show ?thesis
465
+ unfolding invalidate_def sqn_def by auto
466
+ next
467
+ assume "dip\<notin>dom(dests)"
468
+ with \<open>Suc 0 \<le> sqn rt dip\<close> \<open>dip\<in>kD(rt)\<close> [THEN kD_Some] show ?thesis
469
+ unfolding invalidate_def sqn_def by auto
470
+ qed
471
+ thus ?thesis by (rule disjI2)
472
+ qed
473
+ qed
474
+ } note solve_invalidate [simp] = this
475
+
476
+ show ?thesis
477
+ by (inv_cterms inv add: onl_invariant_sterms_TT [OF aodv_wf addpreRT_welldefined]
478
+ onl_invariant_sterms [OF aodv_wf dests_bigger_than_sqn
479
+ [THEN invariant_restrict_inD]]
480
+ onl_invariant_sterms [OF aodv_wf osn_rreq]
481
+ onl_invariant_sterms [OF aodv_wf dsn_rrep]
482
+ simp add: proj3_inv proj2_eq_sqn)
483
+ qed
484
+
485
+ text \<open>Proposition 7.13\<close>
486
+
487
+ lemma rreq_rrep_sn_any_step_invariant:
488
+ "paodv i \<TTurnstile>\<^sub>A (recvmsg rreq_rrep_sn \<rightarrow>) onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast rreq_rrep_sn a)"
489
+ proof -
490
+ have sqnf_kno: "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
491
+ (l \<in> {PRreq-:16..PRreq-:18} \<longrightarrow> sqnf (rt \<xi>) (dip \<xi>) = kno))"
492
+ by (inv_cterms inv add: onl_invariant_sterms_TT [OF aodv_wf addpreRT_welldefined])
493
+ show ?thesis
494
+ by (inv_cterms inv add: onl_invariant_sterms_TT [OF aodv_wf addpreRT_welldefined]
495
+ onl_invariant_sterms [OF aodv_wf sequence_number_one_or_bigger
496
+ [THEN invariant_restrict_inD]]
497
+ onl_invariant_sterms [OF aodv_wf kD_unk_or_atleast_one]
498
+ onl_invariant_sterms_TT [OF aodv_wf sqnf_kno]
499
+ onl_invariant_sterms [OF aodv_wf osn_rreq]
500
+ onl_invariant_sterms [OF aodv_wf dsn_rrep])
501
+ (auto simp: proj2_eq_sqn)
502
+ qed
503
+
504
+ text \<open>Proposition 7.14\<close>
505
+
506
+ lemma rreq_rrep_fresh_any_step_invariant:
507
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), a, _). anycast (rreq_rrep_fresh (rt \<xi>)) a)"
508
+ proof -
509
+ have rreq_oip: "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
510
+ (l \<in> {PRreq-:3, PRreq-:4, PRreq-:15, PRreq-:27}
511
+ \<longrightarrow> oip \<xi> \<in> kD(rt \<xi>)
512
+ \<and> (sqn (rt \<xi>) (oip \<xi>) > (osn \<xi>)
513
+ \<or> (sqn (rt \<xi>) (oip \<xi>) = (osn \<xi>)
514
+ \<and> the (dhops (rt \<xi>) (oip \<xi>)) \<le> Suc (hops \<xi>)
515
+ \<and> the (flag (rt \<xi>) (oip \<xi>)) = val))))"
516
+ proof inv_cterms
517
+ fix l \<xi> l' pp p'
518
+ assume "(\<xi>, pp) \<in> reachable (paodv i) TT"
519
+ and "{PRreq-:2}\<lbrakk>\<lambda>\<xi>. \<xi>\<lparr>rt :=
520
+ update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})\<rparr>\<rbrakk> p' \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
521
+ and "l' = PRreq-:3"
522
+ show "osn \<xi> < sqn (update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})) (oip \<xi>)
523
+ \<or> (sqn (update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})) (oip \<xi>) = osn \<xi>
524
+ \<and> the (dhops (update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})) (oip \<xi>))
525
+ \<le> Suc (hops \<xi>)
526
+ \<and> the (flag (update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})) (oip \<xi>))
527
+ = val)"
528
+ unfolding update_def by (clarsimp split: option.split)
529
+ (metis linorder_neqE_nat not_less)
530
+ qed
531
+
532
+ have rrep_prrep: "paodv i \<TTurnstile> onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l).
533
+ (l \<in> {PRrep-:2..PRrep-:7} \<longrightarrow> (dip \<xi> \<in> kD(rt \<xi>)
534
+ \<and> sqn (rt \<xi>) (dip \<xi>) = dsn \<xi>
535
+ \<and> the (dhops (rt \<xi>) (dip \<xi>)) = Suc (hops \<xi>)
536
+ \<and> the (flag (rt \<xi>) (dip \<xi>)) = val
537
+ \<and> the (nhop (rt \<xi>) (dip \<xi>)) \<in> kD (rt \<xi>))))"
538
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf rrep_1_update_changes]
539
+ onl_invariant_sterms [OF aodv_wf sip_in_kD])
540
+
541
+ show ?thesis
542
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf rreq_oip]
543
+ onl_invariant_sterms [OF aodv_wf rreq_dip_in_vD_dip_eq_ip]
544
+ onl_invariant_sterms [OF aodv_wf rrep_prrep])
545
+ qed
546
+
547
+ text \<open>Proposition 7.15\<close>
548
+
549
+ lemma rerr_invalid_any_step_invariant:
550
+ "paodv i \<TTurnstile>\<^sub>A onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), a, _). anycast (rerr_invalid (rt \<xi>)) a)"
551
+ proof -
552
+ have dests_inv: "paodv i \<TTurnstile>
553
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PAodv-:15, PPkt-:7, PRreq-:9,
554
+ PRreq-:21, PRrep-:10, PRerr-:1}
555
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>vD(rt \<xi>)))
556
+ \<and> (l \<in> {PAodv-:16..PAodv-:19}
557
+ \<union> {PPkt-:8..PPkt-:11}
558
+ \<union> {PRreq-:10..PRreq-:13}
559
+ \<union> {PRreq-:22..PRreq-:25}
560
+ \<union> {PRrep-:11..PRrep-:14}
561
+ \<union> {PRerr-:2..PRerr-:5} \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>iD(rt \<xi>)
562
+ \<and> the (dests \<xi> ip) = sqn (rt \<xi>) ip))
563
+ \<and> (l = PPkt-:14 \<longrightarrow> dip \<xi>\<in>iD(rt \<xi>)))"
564
+ by inv_cterms (clarsimp split: if_split_asm option.split_asm simp: domIff)+
565
+ show ?thesis
566
+ by (inv_cterms inv add: onl_invariant_sterms [OF aodv_wf dests_inv])
567
+ qed
568
+
569
+ text \<open>Proposition 7.16\<close>
570
+
571
+ text \<open>
572
+ Some well-definedness obligations are irrelevant for the Isabelle development:
573
+
574
+ \begin{enumerate}
575
+ \item In each routing table there is at most one entry for each destination: guaranteed by type.
576
+
577
+ \item In each store of queued data packets there is at most one data queue for
578
+ each destination: guaranteed by structure.
579
+
580
+ \item Whenever a set of pairs @{term "(rip, rsn)"} is assigned to the variable
581
+ @{term "dests"} of type @{typ "ip \<rightharpoonup> sqn"}, or to the first argument of
582
+ the function @{term "rerr"}, this set is a partial function, i.e., there
583
+ is at most one entry @{term "(rip, rsn)"} for each destination
584
+ @{term "rip"}: guaranteed by type.
585
+ \end{enumerate}
586
+ \<close>
587
+
588
+ lemma dests_vD_inc_sqn:
589
+ "paodv i \<TTurnstile>
590
+ onl \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(\<xi>, l). (l \<in> {PAodv-:15, PPkt-:7, PRreq-:9, PRreq-:21, PRrep-:10}
591
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>vD(rt \<xi>) \<and> the (dests \<xi> ip) = inc (sqn (rt \<xi>) ip)))
592
+ \<and> (l = PRerr-:1
593
+ \<longrightarrow> (\<forall>ip\<in>dom(dests \<xi>). ip\<in>vD(rt \<xi>) \<and> the (dests \<xi> ip) > sqn (rt \<xi>) ip)))"
594
+ by inv_cterms (clarsimp split: if_split_asm option.split_asm)+
595
+
596
+ text \<open>Proposition 7.27\<close>
597
+
598
+ lemma route_tables_fresher:
599
+ "paodv i \<TTurnstile>\<^sub>A (recvmsg rreq_rrep_sn \<rightarrow>) onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<xi>, _), _, (\<xi>', _)).
600
+ \<forall>dip\<in>kD(rt \<xi>). rt \<xi> \<sqsubseteq>\<^bsub>dip\<^esub> rt \<xi>')"
601
+ proof (inv_cterms inv add:
602
+ onl_invariant_sterms [OF aodv_wf dests_vD_inc_sqn [THEN invariant_restrict_inD]]
603
+ onl_invariant_sterms [OF aodv_wf hop_count_positive [THEN invariant_restrict_inD]]
604
+ onl_invariant_sterms [OF aodv_wf osn_rreq]
605
+ onl_invariant_sterms [OF aodv_wf dsn_rrep]
606
+ onl_invariant_sterms [OF aodv_wf addpreRT_welldefined [THEN invariant_restrict_inD]])
607
+ fix \<xi> pp p'
608
+ assume "(\<xi>, pp) \<in> reachable (paodv i) (recvmsg rreq_rrep_sn)"
609
+ and "{PRreq-:2}\<lbrakk>\<lambda>\<xi>. \<xi>\<lparr>rt := update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})\<rparr>\<rbrakk>
610
+ p' \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
611
+ and "Suc 0 \<le> osn \<xi>"
612
+ and *: "\<forall>ip\<in>kD (rt \<xi>). Suc 0 \<le> the (dhops (rt \<xi>) ip)"
613
+ show "\<forall>ip\<in>kD (rt \<xi>). rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})"
614
+ proof
615
+ fix ip
616
+ assume "ip\<in>kD (rt \<xi>)"
617
+ moreover with * have "1 \<le> the (dhops (rt \<xi>) ip)" by simp
618
+ moreover from \<open>Suc 0 \<le> osn \<xi>\<close>
619
+ have "update_arg_wf (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})" ..
620
+ ultimately show "rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})"
621
+ by (rule rt_fresher_update)
622
+ qed
623
+ next
624
+ fix \<xi> pp p'
625
+ assume "(\<xi>, pp) \<in> reachable (paodv i) (recvmsg rreq_rrep_sn)"
626
+ and "{PRrep-:1}\<lbrakk>\<lambda>\<xi>. \<xi>\<lparr>rt := update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})\<rparr>\<rbrakk>
627
+ p' \<in> sterms \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pp"
628
+ and "Suc 0 \<le> dsn \<xi>"
629
+ and *: "\<forall>ip\<in>kD (rt \<xi>). Suc 0 \<le> the (dhops (rt \<xi>) ip)"
630
+ show "\<forall>ip\<in>kD (rt \<xi>). rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})"
631
+ proof
632
+ fix ip
633
+ assume "ip\<in>kD (rt \<xi>)"
634
+ moreover with * have "1 \<le> the (dhops (rt \<xi>) ip)" by simp
635
+ moreover from \<open>Suc 0 \<le> dsn \<xi>\<close>
636
+ have "update_arg_wf (dsn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})" ..
637
+ ultimately show "rt \<xi> \<sqsubseteq>\<^bsub>ip\<^esub> update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, Suc (hops \<xi>), sip \<xi>, {})"
638
+ by (rule rt_fresher_update)
639
+ qed
640
+ qed
641
+
642
+ end
643
+
formal/afp/AODV/variants/b_fwdrreps/B_Aodv.thy ADDED
@@ -0,0 +1,532 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/b_fwdrreps/Aodv.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ section "The AODV protocol"
8
+
9
+ theory B_Aodv
10
+ imports B_Aodv_Data B_Aodv_Message
11
+ AWN.AWN_SOS_Labels AWN.AWN_Invariants
12
+ begin
13
+
14
+ subsection "Data state"
15
+
16
+ record state =
17
+ ip :: "ip"
18
+ sn :: "sqn"
19
+ rt :: "rt"
20
+ rreqs :: "(ip \<times> rreqid) set"
21
+ store :: "store"
22
+ (* all locals *)
23
+ msg :: "msg"
24
+ data :: "data"
25
+ dests :: "ip \<rightharpoonup> sqn"
26
+ pre :: "ip set"
27
+ rreqid :: "rreqid"
28
+ dip :: "ip"
29
+ oip :: "ip"
30
+ hops :: "nat"
31
+ dsn :: "sqn"
32
+ dsk :: "k"
33
+ osn :: "sqn"
34
+ sip :: "ip"
35
+
36
+ abbreviation aodv_init :: "ip \<Rightarrow> state"
37
+ where "aodv_init i \<equiv> \<lparr>
38
+ ip = i,
39
+ sn = 1,
40
+ rt = Map.empty,
41
+ rreqs = {},
42
+ store = Map.empty,
43
+
44
+ msg = (SOME x. True),
45
+ data = (SOME x. True),
46
+ dests = (SOME x. True),
47
+ pre = (SOME x. True),
48
+ rreqid = (SOME x. True),
49
+ dip = (SOME x. True),
50
+ oip = (SOME x. True),
51
+ hops = (SOME x. True),
52
+ dsn = (SOME x. True),
53
+ dsk = (SOME x. True),
54
+ osn = (SOME x. True),
55
+ sip = (SOME x. x \<noteq> i)
56
+ \<rparr>"
57
+
58
+ lemma some_neq_not_eq [simp]: "\<not>((SOME x :: nat. x \<noteq> i) = i)"
59
+ by (subst some_eq_ex) (metis zero_neq_numeral)
60
+
61
+ definition clear_locals :: "state \<Rightarrow> state"
62
+ where "clear_locals \<xi> = \<xi> \<lparr>
63
+ msg := (SOME x. True),
64
+ data := (SOME x. True),
65
+ dests := (SOME x. True),
66
+ pre := (SOME x. True),
67
+ rreqid := (SOME x. True),
68
+ dip := (SOME x. True),
69
+ oip := (SOME x. True),
70
+ hops := (SOME x. True),
71
+ dsn := (SOME x. True),
72
+ dsk := (SOME x. True),
73
+ osn := (SOME x. True),
74
+ sip := (SOME x. x \<noteq> ip \<xi>)
75
+ \<rparr>"
76
+
77
+ lemma clear_locals_sip_not_ip [simp]: "\<not>(sip (clear_locals \<xi>) = ip \<xi>)"
78
+ unfolding clear_locals_def by simp
79
+
80
+ lemma clear_locals_but_not_globals [simp]:
81
+ "ip (clear_locals \<xi>) = ip \<xi>"
82
+ "sn (clear_locals \<xi>) = sn \<xi>"
83
+ "rt (clear_locals \<xi>) = rt \<xi>"
84
+ "rreqs (clear_locals \<xi>) = rreqs \<xi>"
85
+ "store (clear_locals \<xi>) = store \<xi>"
86
+ unfolding clear_locals_def by auto
87
+
88
+ subsection "Auxilliary message handling definitions"
89
+
90
+ definition is_newpkt
91
+ where "is_newpkt \<xi> \<equiv> case msg \<xi> of
92
+ Newpkt data' dip' \<Rightarrow> { \<xi>\<lparr>data := data', dip := dip'\<rparr> }
93
+ | _ \<Rightarrow> {}"
94
+
95
+ definition is_pkt
96
+ where "is_pkt \<xi> \<equiv> case msg \<xi> of
97
+ Pkt data' dip' oip' \<Rightarrow> { \<xi>\<lparr> data := data', dip := dip', oip := oip' \<rparr> }
98
+ | _ \<Rightarrow> {}"
99
+
100
+ definition is_rreq
101
+ where "is_rreq \<xi> \<equiv> case msg \<xi> of
102
+ Rreq hops' rreqid' dip' dsn' dsk' oip' osn' sip' \<Rightarrow>
103
+ { \<xi>\<lparr> hops := hops', rreqid := rreqid', dip := dip', dsn := dsn',
104
+ dsk := dsk', oip := oip', osn := osn', sip := sip' \<rparr> }
105
+ | _ \<Rightarrow> {}"
106
+
107
+ lemma is_rreq_asm [dest!]:
108
+ assumes "\<xi>' \<in> is_rreq \<xi>"
109
+ shows "(\<exists>hops' rreqid' dip' dsn' dsk' oip' osn' sip'.
110
+ msg \<xi> = Rreq hops' rreqid' dip' dsn' dsk' oip' osn' sip' \<and>
111
+ \<xi>' = \<xi>\<lparr> hops := hops', rreqid := rreqid', dip := dip', dsn := dsn',
112
+ dsk := dsk', oip := oip', osn := osn', sip := sip' \<rparr>)"
113
+ using assms unfolding is_rreq_def
114
+ by (cases "msg \<xi>") simp_all
115
+
116
+ definition is_rrep
117
+ where "is_rrep \<xi> \<equiv> case msg \<xi> of
118
+ Rrep hops' dip' dsn' oip' sip' \<Rightarrow>
119
+ { \<xi>\<lparr> hops := hops', dip := dip', dsn := dsn', oip := oip', sip := sip' \<rparr> }
120
+ | _ \<Rightarrow> {}"
121
+
122
+ lemma is_rrep_asm [dest!]:
123
+ assumes "\<xi>' \<in> is_rrep \<xi>"
124
+ shows "(\<exists>hops' dip' dsn' oip' sip'.
125
+ msg \<xi> = Rrep hops' dip' dsn' oip' sip' \<and>
126
+ \<xi>' = \<xi>\<lparr> hops := hops', dip := dip', dsn := dsn', oip := oip', sip := sip' \<rparr>)"
127
+ using assms unfolding is_rrep_def
128
+ by (cases "msg \<xi>") simp_all
129
+
130
+ definition is_rerr
131
+ where "is_rerr \<xi> \<equiv> case msg \<xi> of
132
+ Rerr dests' sip' \<Rightarrow> { \<xi>\<lparr> dests := dests', sip := sip' \<rparr> }
133
+ | _ \<Rightarrow> {}"
134
+
135
+ lemma is_rerr_asm [dest!]:
136
+ assumes "\<xi>' \<in> is_rerr \<xi>"
137
+ shows "(\<exists>dests' sip'.
138
+ msg \<xi> = Rerr dests' sip' \<and>
139
+ \<xi>' = \<xi>\<lparr> dests := dests', sip := sip' \<rparr>)"
140
+ using assms unfolding is_rerr_def
141
+ by (cases "msg \<xi>") simp_all
142
+
143
+ lemmas is_msg_defs =
144
+ is_rerr_def is_rrep_def is_rreq_def is_pkt_def is_newpkt_def
145
+
146
+ lemma is_msg_inv_ip [simp]:
147
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
148
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
149
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
150
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
151
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> ip \<xi>' = ip \<xi>"
152
+ unfolding is_msg_defs
153
+ by (cases "msg \<xi>", clarsimp+)+
154
+
155
+ lemma is_msg_inv_sn [simp]:
156
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
157
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
158
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
159
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
160
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> sn \<xi>' = sn \<xi>"
161
+ unfolding is_msg_defs
162
+ by (cases "msg \<xi>", clarsimp+)+
163
+
164
+ lemma is_msg_inv_rt [simp]:
165
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
166
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
167
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
168
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
169
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> rt \<xi>' = rt \<xi>"
170
+ unfolding is_msg_defs
171
+ by (cases "msg \<xi>", clarsimp+)+
172
+
173
+ lemma is_msg_inv_rreqs [simp]:
174
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
175
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
176
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
177
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
178
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> rreqs \<xi>' = rreqs \<xi>"
179
+ unfolding is_msg_defs
180
+ by (cases "msg \<xi>", clarsimp+)+
181
+
182
+ lemma is_msg_inv_store [simp]:
183
+ "\<xi>' \<in> is_rerr \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
184
+ "\<xi>' \<in> is_rrep \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
185
+ "\<xi>' \<in> is_rreq \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
186
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
187
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> store \<xi>' = store \<xi>"
188
+ unfolding is_msg_defs
189
+ by (cases "msg \<xi>", clarsimp+)+
190
+
191
+ lemma is_msg_inv_sip [simp]:
192
+ "\<xi>' \<in> is_pkt \<xi> \<Longrightarrow> sip \<xi>' = sip \<xi>"
193
+ "\<xi>' \<in> is_newpkt \<xi> \<Longrightarrow> sip \<xi>' = sip \<xi>"
194
+ unfolding is_msg_defs
195
+ by (cases "msg \<xi>", clarsimp+)+
196
+
197
+ subsection "The protocol process"
198
+
199
+ datatype pseqp =
200
+ PAodv
201
+ | PNewPkt
202
+ | PPkt
203
+ | PRreq
204
+ | PRrep
205
+ | PRerr
206
+
207
+ fun nat_of_seqp :: "pseqp \<Rightarrow> nat"
208
+ where
209
+ "nat_of_seqp PAodv = 1"
210
+ | "nat_of_seqp PPkt = 2"
211
+ | "nat_of_seqp PNewPkt = 3"
212
+ | "nat_of_seqp PRreq = 4"
213
+ | "nat_of_seqp PRrep = 5"
214
+ | "nat_of_seqp PRerr = 6"
215
+
216
+ instantiation "pseqp" :: ord
217
+ begin
218
+ definition less_eq_seqp [iff]: "l1 \<le> l2 = (nat_of_seqp l1 \<le> nat_of_seqp l2)"
219
+ definition less_seqp [iff]: "l1 < l2 = (nat_of_seqp l1 < nat_of_seqp l2)"
220
+ instance ..
221
+ end
222
+
223
+ abbreviation AODV
224
+ where
225
+ "AODV \<equiv> \<lambda>_. \<lbrakk>clear_locals\<rbrakk> call(PAodv)"
226
+
227
+ abbreviation PKT
228
+ where
229
+ "PKT args \<equiv>
230
+
231
+ \<lbrakk>\<xi>. let (data, dip, oip) = args \<xi> in
232
+ (clear_locals \<xi>) \<lparr> data := data, dip := dip, oip := oip \<rparr>\<rbrakk>
233
+ call(PPkt)"
234
+ abbreviation NEWPKT
235
+ where
236
+ "NEWPKT args \<equiv>
237
+ \<lbrakk>\<xi>. let (data, dip) = args \<xi> in
238
+ (clear_locals \<xi>) \<lparr> data := data, dip := dip \<rparr>\<rbrakk>
239
+ call(PNewPkt)"
240
+
241
+ abbreviation RREQ
242
+ where
243
+ "RREQ args \<equiv>
244
+ \<lbrakk>\<xi>. let (hops, rreqid, dip, dsn, dsk, oip, osn, sip) = args \<xi> in
245
+ (clear_locals \<xi>) \<lparr> hops := hops, rreqid := rreqid, dip := dip,
246
+ dsn := dsn, dsk := dsk, oip := oip,
247
+ osn := osn, sip := sip \<rparr>\<rbrakk>
248
+ call(PRreq)"
249
+
250
+ abbreviation RREP
251
+ where
252
+ "RREP args \<equiv>
253
+ \<lbrakk>\<xi>. let (hops, dip, dsn, oip, sip) = args \<xi> in
254
+ (clear_locals \<xi>) \<lparr> hops := hops, dip := dip, dsn := dsn,
255
+ oip := oip, sip := sip \<rparr>\<rbrakk>
256
+ call(PRrep)"
257
+
258
+ abbreviation RERR
259
+ where
260
+ "RERR args \<equiv>
261
+ \<lbrakk>\<xi>. let (dests, sip) = args \<xi> in
262
+ (clear_locals \<xi>) \<lparr> dests := dests, sip := sip \<rparr>\<rbrakk>
263
+ call(PRerr)"
264
+
265
+ fun \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V :: "(state, msg, pseqp, pseqp label) seqp_env"
266
+ where
267
+ "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv = labelled PAodv (
268
+ receive(\<lambda>msg' \<xi>. \<xi> \<lparr> msg := msg' \<rparr>).
269
+ ( \<langle>is_newpkt\<rangle> NEWPKT(\<lambda>\<xi>. (data \<xi>, ip \<xi>))
270
+ \<oplus> \<langle>is_pkt\<rangle> PKT(\<lambda>\<xi>. (data \<xi>, dip \<xi>, oip \<xi>))
271
+ \<oplus> \<langle>is_rreq\<rangle>
272
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
273
+ RREQ(\<lambda>\<xi>. (hops \<xi>, rreqid \<xi>, dip \<xi>, dsn \<xi>, dsk \<xi>, oip \<xi>, osn \<xi>, sip \<xi>))
274
+ \<oplus> \<langle>is_rrep\<rangle>
275
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
276
+ RREP(\<lambda>\<xi>. (hops \<xi>, dip \<xi>, dsn \<xi>, oip \<xi>, sip \<xi>))
277
+ \<oplus> \<langle>is_rerr\<rangle>
278
+ \<lbrakk>\<xi>. \<xi> \<lparr>rt := update (rt \<xi>) (sip \<xi>) (0, unk, val, 1, sip \<xi>, {}) \<rparr>\<rbrakk>
279
+ RERR(\<lambda>\<xi>. (dests \<xi>, sip \<xi>))
280
+ )
281
+ \<oplus> \<langle>\<lambda>\<xi>. { \<xi>\<lparr> dip := dip \<rparr> | dip. dip \<in> qD(store \<xi>) \<inter> vD(rt \<xi>) }\<rangle>
282
+ \<lbrakk>\<xi>. \<xi> \<lparr> data := hd(\<sigma>\<^bsub>queue\<^esub>(store \<xi>, dip \<xi>)) \<rparr>\<rbrakk>
283
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (dip \<xi>)), \<lambda>\<xi>. pkt(data \<xi>, dip \<xi>, ip \<xi>)).
284
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := the (drop (dip \<xi>) (store \<xi>)) \<rparr>\<rbrakk>
285
+ AODV()
286
+ \<triangleright> \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (dip \<xi>))
287
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
288
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
289
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
290
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
291
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
292
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
293
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)). AODV()
294
+ \<oplus> \<langle>\<lambda>\<xi>. { \<xi>\<lparr> dip := dip \<rparr>
295
+ | dip. dip \<in> qD(store \<xi>) - vD(rt \<xi>) \<and> the (\<sigma>\<^bsub>p-flag\<^esub>(store \<xi>, dip)) = req }\<rangle>
296
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := unsetRRF (store \<xi>) (dip \<xi>) \<rparr>\<rbrakk>
297
+ \<lbrakk>\<xi>. \<xi> \<lparr> sn := inc (sn \<xi>) \<rparr>\<rbrakk>
298
+ \<lbrakk>\<xi>. \<xi> \<lparr> rreqid := nrreqid (rreqs \<xi>) (ip \<xi>) \<rparr>\<rbrakk>
299
+ \<lbrakk>\<xi>. \<xi> \<lparr> rreqs := rreqs \<xi> \<union> {(ip \<xi>, rreqid \<xi>)} \<rparr>\<rbrakk>
300
+ broadcast(\<lambda>\<xi>. rreq(0, rreqid \<xi>, dip \<xi>, sqn (rt \<xi>) (dip \<xi>), sqnf (rt \<xi>) (dip \<xi>),
301
+ ip \<xi>, sn \<xi>, ip \<xi>)). AODV())"
302
+
303
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt = labelled PNewPkt (
304
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
305
+ deliver(\<lambda>\<xi>. data \<xi>).AODV()
306
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
307
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := add (data \<xi>) (dip \<xi>) (store \<xi>) \<rparr>\<rbrakk>
308
+ AODV())"
309
+
310
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt = labelled PPkt (
311
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
312
+ deliver(\<lambda>\<xi>. data \<xi>).AODV()
313
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
314
+ (
315
+ \<langle>\<xi>. dip \<xi> \<in> vD (rt \<xi>)\<rangle>
316
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (dip \<xi>)), \<lambda>\<xi>. pkt(data \<xi>, dip \<xi>, oip \<xi>)).AODV()
317
+ \<triangleright>
318
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (dip \<xi>))
319
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
320
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
321
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
322
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
323
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
324
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
325
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
326
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> vD (rt \<xi>)\<rangle>
327
+ (
328
+ \<langle>\<xi>. dip \<xi> \<in> iD (rt \<xi>)\<rangle>
329
+ groupcast(\<lambda>\<xi>. the (precs (rt \<xi>) (dip \<xi>)),
330
+ \<lambda>\<xi>. rerr([dip \<xi> \<mapsto> sqn (rt \<xi>) (dip \<xi>)], ip \<xi>)). AODV()
331
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> iD (rt \<xi>)\<rangle>
332
+ AODV()
333
+ )
334
+ ))"
335
+
336
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq = labelled PRreq (
337
+ \<langle>\<xi>. (oip \<xi>, rreqid \<xi>) \<in> rreqs \<xi>\<rangle>
338
+ AODV()
339
+ \<oplus> \<langle>\<xi>. (oip \<xi>, rreqid \<xi>) \<notin> rreqs \<xi>\<rangle>
340
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := update (rt \<xi>) (oip \<xi>) (osn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rparr>\<rbrakk>
341
+ \<lbrakk>\<xi>. \<xi> \<lparr> rreqs := rreqs \<xi> \<union> {(oip \<xi>, rreqid \<xi>)} \<rparr>\<rbrakk>
342
+ (
343
+ \<langle>\<xi>. dip \<xi> = ip \<xi>\<rangle>
344
+ \<lbrakk>\<xi>. \<xi> \<lparr> sn := max (sn \<xi>) (dsn \<xi>) \<rparr>\<rbrakk>
345
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(0, dip \<xi>, sn \<xi>, oip \<xi>, ip \<xi>)).AODV()
346
+ \<triangleright>
347
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
348
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
349
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
350
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
351
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
352
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
353
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
354
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
355
+ \<oplus> \<langle>\<xi>. dip \<xi> \<noteq> ip \<xi>\<rangle>
356
+ (
357
+ \<langle>\<xi>. dip \<xi> \<in> vD (rt \<xi>) \<and> dsn \<xi> \<le> sqn (rt \<xi>) (dip \<xi>) \<and> sqnf (rt \<xi>) (dip \<xi>) = kno\<rangle>
358
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (dip \<xi>) {sip \<xi>}) \<rparr>\<rbrakk>
359
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (oip \<xi>) {the (nhop (rt \<xi>) (dip \<xi>))}) \<rparr>\<rbrakk>
360
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(the (dhops (rt \<xi>) (dip \<xi>)), dip \<xi>,
361
+ sqn (rt \<xi>) (dip \<xi>), oip \<xi>, ip \<xi>)).
362
+ AODV()
363
+ \<triangleright>
364
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
365
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
366
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
367
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
368
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
369
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
370
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
371
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
372
+ \<oplus> \<langle>\<xi>. dip \<xi> \<notin> vD (rt \<xi>) \<or> sqn (rt \<xi>) (dip \<xi>) < dsn \<xi> \<or> sqnf (rt \<xi>) (dip \<xi>) = unk\<rangle>
373
+ broadcast(\<lambda>\<xi>. rreq(hops \<xi> + 1, rreqid \<xi>, dip \<xi>, max (sqn (rt \<xi>) (dip \<xi>)) (dsn \<xi>),
374
+ dsk \<xi>, oip \<xi>, osn \<xi>, ip \<xi>)).
375
+ AODV()
376
+ )
377
+ ))"
378
+
379
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep = labelled PRrep (
380
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := update (rt \<xi>) (dip \<xi>) (dsn \<xi>, kno, val, hops \<xi> + 1, sip \<xi>, {}) \<rparr> \<rbrakk>
381
+ (
382
+ \<langle>\<xi>. oip \<xi> = ip \<xi> \<rangle>
383
+ AODV()
384
+ \<oplus> \<langle>\<xi>. oip \<xi> \<noteq> ip \<xi> \<rangle>
385
+ (
386
+ \<langle>\<xi>. oip \<xi> \<in> vD (rt \<xi>) \<and> dip \<xi> \<in> vD (rt \<xi>)\<rangle>
387
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (dip \<xi>)
388
+ {the (nhop (rt \<xi>) (oip \<xi>))}) \<rparr>\<rbrakk>
389
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := the (addpreRT (rt \<xi>) (the (nhop (rt \<xi>) (dip \<xi>))) {the (nhop (rt \<xi>) (oip \<xi>))}) \<rparr>\<rbrakk>
390
+ unicast(\<lambda>\<xi>. the (nhop (rt \<xi>) (oip \<xi>)), \<lambda>\<xi>. rrep(the (dhops (rt \<xi>) (dip \<xi>)), dip \<xi>,
391
+ sqn (rt \<xi>) (dip \<xi>), oip \<xi>, ip \<xi>)).
392
+ AODV()
393
+ \<triangleright>
394
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if (rip \<in> vD (rt \<xi>) \<and> nhop (rt \<xi>) rip = nhop (rt \<xi>) (oip \<xi>))
395
+ then Some (inc (sqn (rt \<xi>) rip)) else None) \<rparr>\<rbrakk>
396
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
397
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
398
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
399
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
400
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
401
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)).AODV()
402
+ \<oplus> \<langle>\<xi>. oip \<xi> \<notin> vD (rt \<xi>) \<or> dip \<xi> \<notin> vD (rt \<xi>)\<rangle>
403
+ AODV()
404
+ )
405
+ )
406
+ )"
407
+
408
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr = labelled PRerr (
409
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. case (dests \<xi>) rip of None \<Rightarrow> None
410
+ | Some rsn \<Rightarrow> if rip \<in> vD (rt \<xi>) \<and> the (nhop (rt \<xi>) rip) = sip \<xi>
411
+ \<and> sqn (rt \<xi>) rip < rsn then Some rsn else None) \<rparr>\<rbrakk>
412
+ \<lbrakk>\<xi>. \<xi> \<lparr> rt := invalidate (rt \<xi>) (dests \<xi>) \<rparr>\<rbrakk>
413
+ \<lbrakk>\<xi>. \<xi> \<lparr> store := setRRF (store \<xi>) (dests \<xi>)\<rparr>\<rbrakk>
414
+ \<lbrakk>\<xi>. \<xi> \<lparr> pre := \<Union>{ the (precs (rt \<xi>) rip) | rip. rip \<in> dom (dests \<xi>) } \<rparr>\<rbrakk>
415
+ \<lbrakk>\<xi>. \<xi> \<lparr> dests := (\<lambda>rip. if ((dests \<xi>) rip \<noteq> None \<and> the (precs (rt \<xi>) rip) \<noteq> {})
416
+ then (dests \<xi>) rip else None) \<rparr>\<rbrakk>
417
+ groupcast(\<lambda>\<xi>. pre \<xi>, \<lambda>\<xi>. rerr(dests \<xi>, ip \<xi>)). AODV())"
418
+
419
+ declare \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V.simps [simp del, code del]
420
+ lemmas \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps [simp, code] = \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V.simps [simplified]
421
+
422
+ fun \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton
423
+ where
424
+ "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PAodv = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv)"
425
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PNewPkt = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt)"
426
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PPkt = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt)"
427
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRreq = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq)"
428
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRrep = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep)"
429
+ | "\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton PRerr = seqp_skeleton (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr)"
430
+
431
+ lemma \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton_wf [simp]:
432
+ "wellformed \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton"
433
+ proof (rule, intro allI)
434
+ fix pn pn'
435
+ show "call(pn') \<notin> stermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton pn)"
436
+ by (cases pn) simp_all
437
+ qed
438
+
439
+ declare \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton.simps [simp del, code del]
440
+ lemmas \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton_simps [simp, code]
441
+ = \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_skeleton.simps [simplified \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps seqp_skeleton.simps]
442
+
443
+ lemma aodv_proc_cases [dest]:
444
+ fixes p pn
445
+ shows "p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn) \<Longrightarrow>
446
+ (p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv) \<or>
447
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PNewPkt) \<or>
448
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PPkt) \<or>
449
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRreq) \<or>
450
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRrep) \<or>
451
+ p \<in> ctermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PRerr))"
452
+ by (cases pn) simp_all
453
+
454
+ definition \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V :: "ip \<Rightarrow> (state \<times> (state, msg, pseqp, pseqp label) seqp) set"
455
+ where "\<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<equiv> {(aodv_init i, \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V PAodv)}"
456
+
457
+ abbreviation paodv
458
+ :: "ip \<Rightarrow> (state \<times> (state, msg, pseqp, pseqp label) seqp, msg seq_action) automaton"
459
+ where
460
+ "paodv i \<equiv> \<lparr> init = \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i, trans = seqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V \<rparr>"
461
+
462
+ lemma aodv_trans: "trans (paodv i) = seqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
463
+ by simp
464
+
465
+ lemma aodv_control_within [simp]: "control_within \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (init (paodv i))"
466
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by (rule control_withinI) (auto simp del: \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps)
467
+
468
+ lemma aodv_wf [simp]:
469
+ "wellformed \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
470
+ proof (rule, intro allI)
471
+ fix pn pn'
472
+ show "call(pn') \<notin> stermsl (\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn)"
473
+ by (cases pn) simp_all
474
+ qed
475
+
476
+ lemmas aodv_labels_not_empty [simp] = labels_not_empty [OF aodv_wf]
477
+
478
+ lemma aodv_ex_label [intro]: "\<exists>l. l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p"
479
+ by (metis aodv_labels_not_empty all_not_in_conv)
480
+
481
+ lemma aodv_ex_labelE [elim]:
482
+ assumes "\<forall>l\<in>labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p. P l p"
483
+ and "\<exists>p l. P l p \<Longrightarrow> Q"
484
+ shows "Q"
485
+ using assms by (metis aodv_ex_label)
486
+
487
+ lemma aodv_simple_labels [simp]: "simple_labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V"
488
+ proof
489
+ fix pn p
490
+ assume "p\<in>subterms(\<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V pn)"
491
+ thus "\<exists>!l. labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p = {l}"
492
+ by (cases pn) (simp_all cong: seqp_congs | elim disjE)+
493
+ qed
494
+
495
+ lemma \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_labels [simp]: "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<Longrightarrow> labels \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V p = {PAodv-:0}"
496
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
497
+
498
+ lemma aodv_init_kD_empty [simp]:
499
+ "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<Longrightarrow> kD (rt \<xi>) = {}"
500
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def kD_def by simp
501
+
502
+ lemma aodv_init_sip_not_ip [simp]: "\<not>(sip (aodv_init i) = i)" by simp
503
+
504
+ lemma aodv_init_sip_not_ip' [simp]:
505
+ assumes "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
506
+ shows "sip \<xi> \<noteq> ip \<xi>"
507
+ using assms unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
508
+
509
+ lemma aodv_init_sip_not_i [simp]:
510
+ assumes "(\<xi>, p) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i"
511
+ shows "sip \<xi> \<noteq> i"
512
+ using assms unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
513
+
514
+ lemma clear_locals_sip_not_ip':
515
+ assumes "ip \<xi> = i"
516
+ shows "\<not>(sip (clear_locals \<xi>) = i)"
517
+ using assms by auto
518
+
519
+ text \<open>Stop the simplifier from descending into process terms.\<close>
520
+ declare seqp_congs [cong]
521
+
522
+ text \<open>Configure the main invariant tactic for AODV.\<close>
523
+
524
+ declare
525
+ \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_simps [cterms_env]
526
+ aodv_proc_cases [ctermsl_cases]
527
+ seq_invariant_ctermsI [OF aodv_wf aodv_control_within aodv_simple_labels aodv_trans,
528
+ cterms_intros]
529
+ seq_step_invariant_ctermsI [OF aodv_wf aodv_control_within aodv_simple_labels aodv_trans,
530
+ cterms_intros]
531
+
532
+ end
formal/afp/AODV/variants/b_fwdrreps/B_Aodv_Data.thy ADDED
@@ -0,0 +1,990 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/b_fwdrreps/Aodv_Data.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Predicates and functions used in the AODV model"
7
+
8
+ theory B_Aodv_Data
9
+ imports B_Fwdrreps
10
+ begin
11
+
12
+ subsection "Sequence Numbers"
13
+
14
+ text \<open>Sequence numbers approximate the relative freshness of routing information.\<close>
15
+
16
+ definition inc :: "sqn \<Rightarrow> sqn"
17
+ where "inc sn \<equiv> if sn = 0 then sn else sn + 1"
18
+
19
+ lemma less_than_inc [simp]: "x \<le> inc x"
20
+ unfolding inc_def by simp
21
+
22
+ lemma inc_minus_suc_0 [simp]:
23
+ "inc x - Suc 0 = x"
24
+ unfolding inc_def by simp
25
+
26
+ lemma inc_never_one' [simp, intro]: "inc x \<noteq> Suc 0"
27
+ unfolding inc_def by simp
28
+
29
+ lemma inc_never_one [simp, intro]: "inc x \<noteq> 1"
30
+ by simp
31
+
32
+ subsection "Modelling Routes"
33
+
34
+ text \<open>
35
+ A route is a 6-tuple, @{term "(dsn, dsk, flag, hops, nhip, pre)"} where
36
+ @{term dsn} is the `destination sequence number', @{term dsk} is the
37
+ `destination-sequence-number status', @{term flag} is the route status,
38
+ @{term hops} is the number of hops to the destination, @{term nhip} is the
39
+ next hop toward the destination, and @{term pre} is the set of `precursor nodes'--those
40
+ interested in hearing about changes to the route.
41
+ \<close>
42
+
43
+ type_synonym r = "sqn \<times> k \<times> f \<times> nat \<times> ip \<times> ip set"
44
+
45
+ definition proj2 :: "r \<Rightarrow> sqn" ("\<pi>\<^sub>2")
46
+ where "\<pi>\<^sub>2 \<equiv> \<lambda>(dsn, _, _, _, _, _). dsn"
47
+
48
+ definition proj3 :: "r \<Rightarrow> k" ("\<pi>\<^sub>3")
49
+ where "\<pi>\<^sub>3 \<equiv> \<lambda>(_, dsk, _, _, _, _). dsk"
50
+
51
+ definition proj4 :: "r \<Rightarrow> f" ("\<pi>\<^sub>4")
52
+ where "\<pi>\<^sub>4 \<equiv> \<lambda>(_, _, flag, _, _, _). flag"
53
+
54
+ definition proj5 :: "r \<Rightarrow> nat" ("\<pi>\<^sub>5")
55
+ where "\<pi>\<^sub>5 \<equiv> \<lambda>(_, _, _, hops, _, _). hops"
56
+
57
+ definition proj6 :: "r \<Rightarrow> ip" ("\<pi>\<^sub>6")
58
+ where "\<pi>\<^sub>6 \<equiv> \<lambda>(_, _, _, _, nhip, _). nhip"
59
+
60
+ definition proj7 :: "r \<Rightarrow> ip set" ("\<pi>\<^sub>7")
61
+ where "\<pi>\<^sub>7 \<equiv> \<lambda>(_, _, _, _, _, pre). pre"
62
+
63
+ lemma projs [simp]:
64
+ "\<pi>\<^sub>2(dsn, dsk, flag, hops, nhip, pre) = dsn"
65
+ "\<pi>\<^sub>3(dsn, dsk, flag, hops, nhip, pre) = dsk"
66
+ "\<pi>\<^sub>4(dsn, dsk, flag, hops, nhip, pre) = flag"
67
+ "\<pi>\<^sub>5(dsn, dsk, flag, hops, nhip, pre) = hops"
68
+ "\<pi>\<^sub>6(dsn, dsk, flag, hops, nhip, pre) = nhip"
69
+ "\<pi>\<^sub>7(dsn, dsk, flag, hops, nhip, pre) = pre"
70
+ by (clarsimp simp: proj2_def proj3_def proj4_def
71
+ proj5_def proj6_def proj7_def)+
72
+
73
+ lemma proj3_pred [intro]: "\<lbrakk> P kno; P unk \<rbrakk> \<Longrightarrow> P (\<pi>\<^sub>3 x)"
74
+ by (rule k.induct)
75
+
76
+ lemma proj4_pred [intro]: "\<lbrakk> P val; P inv \<rbrakk> \<Longrightarrow> P (\<pi>\<^sub>4 x)"
77
+ by (rule f.induct)
78
+
79
+ lemma proj6_pair_snd [simp]:
80
+ fixes dsn' r
81
+ shows "\<pi>\<^sub>6 (dsn', snd (r)) = \<pi>\<^sub>6(r)"
82
+ by (cases r) simp
83
+
84
+ subsection "Routing Tables"
85
+
86
+ text \<open>Routing tables map ip addresses to route entries.\<close>
87
+
88
+ type_synonym rt = "ip \<rightharpoonup> r"
89
+
90
+ syntax
91
+ "_Sigma_route" :: "rt \<Rightarrow> ip \<rightharpoonup> r" ("\<sigma>\<^bsub>route\<^esub>'(_, _')")
92
+
93
+ translations
94
+ "\<sigma>\<^bsub>route\<^esub>(rt, dip)" => "rt dip"
95
+
96
+ definition sqn :: "rt \<Rightarrow> ip \<Rightarrow> sqn"
97
+ where "sqn rt dip \<equiv> case \<sigma>\<^bsub>route\<^esub>(rt, dip) of Some r \<Rightarrow> \<pi>\<^sub>2(r) | None \<Rightarrow> 0"
98
+
99
+ definition sqnf :: "rt \<Rightarrow> ip \<Rightarrow> k"
100
+ where "sqnf rt dip \<equiv> case \<sigma>\<^bsub>route\<^esub>(rt, dip) of Some r \<Rightarrow> \<pi>\<^sub>3(r) | None \<Rightarrow> unk"
101
+
102
+ abbreviation flag :: "rt \<Rightarrow> ip \<rightharpoonup> f"
103
+ where "flag rt dip \<equiv> map_option \<pi>\<^sub>4 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
104
+
105
+ abbreviation dhops :: "rt \<Rightarrow> ip \<rightharpoonup> nat"
106
+ where "dhops rt dip \<equiv> map_option \<pi>\<^sub>5 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
107
+
108
+ abbreviation nhop :: "rt \<Rightarrow> ip \<rightharpoonup> ip"
109
+ where "nhop rt dip \<equiv> map_option \<pi>\<^sub>6 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
110
+
111
+ abbreviation precs :: "rt \<Rightarrow> ip \<rightharpoonup> ip set"
112
+ where "precs rt dip \<equiv> map_option \<pi>\<^sub>7 (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
113
+
114
+ definition vD :: "rt \<Rightarrow> ip set"
115
+ where "vD rt \<equiv> {dip. flag rt dip = Some val}"
116
+
117
+ definition iD :: "rt \<Rightarrow> ip set"
118
+ where "iD rt \<equiv> {dip. flag rt dip = Some inv}"
119
+
120
+ definition kD :: "rt \<Rightarrow> ip set"
121
+ where "kD rt \<equiv> {dip. rt dip \<noteq> None}"
122
+
123
+ lemma kD_is_vD_and_iD: "kD rt = vD rt \<union> iD rt"
124
+ unfolding kD_def vD_def iD_def by auto
125
+
126
+ lemma vD_iD_gives_kD [simp]:
127
+ "\<And>ip rt. ip \<in> vD rt \<Longrightarrow> ip \<in> kD rt"
128
+ "\<And>ip rt. ip \<in> iD rt \<Longrightarrow> ip \<in> kD rt"
129
+ unfolding kD_is_vD_and_iD by simp_all
130
+
131
+ lemma kD_Some [dest]:
132
+ fixes dip rt
133
+ assumes "dip \<in> kD rt"
134
+ shows "\<exists>dsn dsk flag hops nhip pre.
135
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, flag, hops, nhip, pre)"
136
+ using assms unfolding kD_def by simp
137
+
138
+ lemma kD_None [dest]:
139
+ fixes dip rt
140
+ assumes "dip \<notin> kD rt"
141
+ shows "\<sigma>\<^bsub>route\<^esub>(rt, dip) = None"
142
+ using assms unfolding kD_def
143
+ by (metis (mono_tags) mem_Collect_eq)
144
+
145
+ lemma vD_Some [dest]:
146
+ fixes dip rt
147
+ assumes "dip \<in> vD rt"
148
+ shows "\<exists>dsn dsk hops nhip pre.
149
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, val, hops, nhip, pre)"
150
+ using assms unfolding vD_def by simp
151
+
152
+ lemma vD_empty [simp]: "vD Map.empty = {}"
153
+ unfolding vD_def by simp
154
+
155
+ lemma iD_Some [dest]:
156
+ fixes dip rt
157
+ assumes "dip \<in> iD rt"
158
+ shows "\<exists>dsn dsk hops nhip pre.
159
+ \<sigma>\<^bsub>route\<^esub>(rt, dip) = Some (dsn, dsk, inv, hops, nhip, pre)"
160
+ using assms unfolding iD_def by simp
161
+
162
+ lemma val_is_vD [elim]:
163
+ fixes ip rt
164
+ assumes "ip\<in>kD(rt)"
165
+ and "the (flag rt ip) = val"
166
+ shows "ip\<in>vD(rt)"
167
+ using assms unfolding vD_def by auto
168
+
169
+ lemma inv_is_iD [elim]:
170
+ fixes ip rt
171
+ assumes "ip\<in>kD(rt)"
172
+ and "the (flag rt ip) = inv"
173
+ shows "ip\<in>iD(rt)"
174
+ using assms unfolding iD_def by auto
175
+
176
+ lemma iD_flag_is_inv [elim, simp]:
177
+ fixes ip rt
178
+ assumes "ip\<in>iD(rt)"
179
+ shows "the (flag rt ip) = inv"
180
+ proof -
181
+ from \<open>ip\<in>iD(rt)\<close> have "ip\<in>kD(rt)" by auto
182
+ with assms show ?thesis unfolding iD_def by auto
183
+ qed
184
+
185
+ lemma kD_but_not_vD_is_iD [elim]:
186
+ fixes ip rt
187
+ assumes "ip\<in>kD(rt)"
188
+ and "ip\<notin>vD(rt)"
189
+ shows "ip\<in>iD(rt)"
190
+ proof -
191
+ from \<open>ip\<in>kD(rt)\<close> obtain dsn dsk f hops nhop pre
192
+ where rtip: "rt ip = Some (dsn, dsk, f, hops, nhop, pre)"
193
+ by (metis kD_Some)
194
+ from \<open>ip\<notin>vD(rt)\<close> have "f \<noteq> val"
195
+ proof (rule contrapos_nn)
196
+ assume "f = val"
197
+ with rtip have "the (flag rt ip) = val" by simp
198
+ with \<open>ip\<in>kD(rt)\<close> show "ip\<in>vD(rt)" ..
199
+ qed
200
+ with rtip have "the (flag rt ip)= inv" by simp
201
+ with \<open>ip\<in>kD(rt)\<close> show "ip\<in>iD(rt)" ..
202
+ qed
203
+
204
+ lemma vD_or_iD [elim]:
205
+ fixes ip rt
206
+ assumes "ip\<in>kD(rt)"
207
+ and "ip\<in>vD(rt) \<Longrightarrow> P rt ip"
208
+ and "ip\<in>iD(rt) \<Longrightarrow> P rt ip"
209
+ shows "P rt ip"
210
+ proof -
211
+ from \<open>ip\<in>kD(rt)\<close> have "ip\<in>vD(rt) \<union> iD(rt)"
212
+ by (simp add: kD_is_vD_and_iD)
213
+ thus ?thesis by (auto elim: assms(2-3))
214
+ qed
215
+
216
+ lemma proj5_eq_dhops: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>5(the (rt dip)) = the (dhops rt dip)"
217
+ unfolding sqn_def by (drule kD_Some) clarsimp
218
+
219
+ lemma proj4_eq_flag: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>4(the (rt dip)) = the (flag rt dip)"
220
+ unfolding sqn_def by (drule kD_Some) clarsimp
221
+
222
+ lemma proj2_eq_sqn: "\<And>dip rt. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>2(the (rt dip)) = sqn rt dip"
223
+ unfolding sqn_def by (drule kD_Some) clarsimp
224
+
225
+ lemma kD_sqnf_is_proj3 [simp]:
226
+ "\<And>ip rt. ip\<in>kD(rt) \<Longrightarrow> sqnf rt ip = \<pi>\<^sub>3(the (rt ip))"
227
+ unfolding sqnf_def by auto
228
+
229
+ lemma vD_flag_val [simp]:
230
+ "\<And>dip rt. dip \<in> vD (rt) \<Longrightarrow> the (flag rt dip) = val"
231
+ unfolding vD_def by clarsimp
232
+
233
+ lemma kD_update [simp]:
234
+ "\<And>rt nip v. kD (rt(nip \<mapsto> v)) = insert nip (kD rt)"
235
+ unfolding kD_def by auto
236
+
237
+ lemma kD_empty [simp]: "kD Map.empty = {}"
238
+ unfolding kD_def by simp
239
+
240
+ lemma ip_equal_or_known [elim]:
241
+ fixes rt ip ip'
242
+ assumes "ip = ip' \<or> ip\<in>kD(rt)"
243
+ and "ip = ip' \<Longrightarrow> P rt ip ip'"
244
+ and "\<lbrakk> ip \<noteq> ip'; ip\<in>kD(rt)\<rbrakk> \<Longrightarrow> P rt ip ip'"
245
+ shows "P rt ip ip'"
246
+ using assms by auto
247
+
248
+ subsection "Updating Routing Tables"
249
+
250
+ text \<open>Routing table entries are modified through explicit functions.
251
+ The properties of these functions are important in invariant proofs.\<close>
252
+
253
+ subsubsection "Updating Precursor Lists"
254
+
255
+ definition addpre :: "r \<Rightarrow> ip set \<Rightarrow> r"
256
+ where "addpre r npre \<equiv> let (dsn, dsk, flag, hops, nhip, pre) = r in
257
+ (dsn, dsk, flag, hops, nhip, pre \<union> npre)"
258
+
259
+ lemma proj2_addpre:
260
+ fixes v pre
261
+ shows "\<pi>\<^sub>2(addpre v pre) = \<pi>\<^sub>2(v)"
262
+ unfolding addpre_def by (cases v) simp
263
+
264
+ lemma proj3_addpre:
265
+ fixes v pre
266
+ shows "\<pi>\<^sub>3(addpre v pre) = \<pi>\<^sub>3(v)"
267
+ unfolding addpre_def by (cases v) simp
268
+
269
+ lemma proj4_addpre:
270
+ fixes v pre
271
+ shows "\<pi>\<^sub>4(addpre v pre) = \<pi>\<^sub>4(v)"
272
+ unfolding addpre_def by (cases v) simp
273
+
274
+ lemma proj5_addpre:
275
+ fixes v pre
276
+ shows "\<pi>\<^sub>5(addpre v pre) = \<pi>\<^sub>5(v)"
277
+ unfolding addpre_def by (cases v) simp
278
+
279
+ lemma proj6_addpre:
280
+ fixes dsn dsk flag hops nhip pre npre
281
+ shows "\<pi>\<^sub>6(addpre v npre) = \<pi>\<^sub>6(v)"
282
+ unfolding addpre_def by (cases v) simp
283
+
284
+ lemma proj7_addpre:
285
+ fixes dsn dsk flag hops nhip pre npre
286
+ shows "\<pi>\<^sub>7(addpre v npre) = \<pi>\<^sub>7(v) \<union> npre"
287
+ unfolding addpre_def by (cases v) simp
288
+
289
+ lemma addpre_empty: "addpre r {} = r"
290
+ unfolding addpre_def by simp
291
+
292
+ lemma addpre_r:
293
+ "addpre (dsn, dsk, fl, hops, nhip, pre) npre = (dsn, dsk, fl, hops, nhip, pre \<union> npre)"
294
+ unfolding addpre_def by simp
295
+
296
+ lemmas addpre_simps [simp] = proj2_addpre proj3_addpre proj4_addpre proj5_addpre
297
+ proj6_addpre proj7_addpre addpre_empty addpre_r
298
+
299
+ definition addpreRT :: "rt \<Rightarrow> ip \<Rightarrow> ip set \<rightharpoonup> rt"
300
+ where "addpreRT rt dip npre \<equiv>
301
+ map_option (\<lambda>s. rt (dip \<mapsto> addpre s npre)) (\<sigma>\<^bsub>route\<^esub>(rt, dip))"
302
+
303
+ lemma snd_addpre [simp]:
304
+ "\<And>dsn dsn' v pre. (dsn, snd(addpre (dsn', v) pre)) = addpre (dsn, v) pre"
305
+ unfolding addpre_def by clarsimp
306
+
307
+ lemma proj2_addpreRT [simp]:
308
+ fixes ip rt ip' npre
309
+ assumes "ip\<in>kD rt"
310
+ and "ip'\<in>kD rt"
311
+ shows "\<pi>\<^sub>2(the (the (addpreRT rt ip' npre) ip)) = \<pi>\<^sub>2(the (rt ip))"
312
+ using assms [THEN kD_Some] unfolding addpreRT_def by clarsimp
313
+
314
+ lemma proj3_addpreRT [simp]:
315
+ fixes ip rt ip' npre
316
+ assumes "ip\<in>kD rt"
317
+ and "ip'\<in>kD rt"
318
+ shows "\<pi>\<^sub>3(the (the (addpreRT rt ip' npre) ip)) = \<pi>\<^sub>3(the (rt ip))"
319
+ using assms [THEN kD_Some] unfolding addpreRT_def by clarsimp
320
+
321
+ lemma proj5_addpreRT [simp]:
322
+ "\<And>rt dip ip npre. dip\<in>kD(rt) \<Longrightarrow> \<pi>\<^sub>5(the (the (addpreRT rt dip npre) ip)) = \<pi>\<^sub>5(the (rt ip))"
323
+ unfolding addpreRT_def by auto
324
+
325
+ lemma flag_addpreRT [simp]:
326
+ fixes rt pre ip dip
327
+ assumes "dip \<in> kD rt"
328
+ shows "flag (the (addpreRT rt dip pre)) ip = flag rt ip"
329
+ unfolding addpreRT_def
330
+ using assms [THEN kD_Some] by (clarsimp)
331
+
332
+ lemma kD_addpreRT [simp]:
333
+ fixes rt dip npre
334
+ assumes "dip \<in> kD rt"
335
+ shows "kD (the (addpreRT rt dip npre)) = kD rt"
336
+ unfolding kD_def addpreRT_def
337
+ using assms [THEN kD_Some]
338
+ by clarsimp blast
339
+
340
+ lemma vD_addpreRT [simp]:
341
+ fixes rt dip npre
342
+ assumes "dip \<in> kD rt"
343
+ shows "vD (the (addpreRT rt dip npre)) = vD rt"
344
+ unfolding vD_def addpreRT_def
345
+ using assms [THEN kD_Some] by clarsimp auto
346
+
347
+ lemma iD_addpreRT [simp]:
348
+ fixes rt dip npre
349
+ assumes "dip \<in> kD rt"
350
+ shows "iD (the (addpreRT rt dip npre)) = iD rt"
351
+ unfolding iD_def addpreRT_def
352
+ using assms [THEN kD_Some] by clarsimp auto
353
+
354
+ lemma nhop_addpreRT [simp]:
355
+ fixes rt pre ip dip
356
+ assumes "dip \<in> kD rt"
357
+ shows "nhop (the (addpreRT rt dip pre)) ip = nhop rt ip"
358
+ unfolding sqn_def addpreRT_def
359
+ using assms [THEN kD_Some] by (clarsimp)
360
+
361
+ lemma sqn_addpreRT [simp]:
362
+ fixes rt pre ip dip
363
+ assumes "dip \<in> kD rt"
364
+ shows "sqn (the (addpreRT rt dip pre)) ip = sqn rt ip"
365
+ unfolding sqn_def addpreRT_def
366
+ using assms [THEN kD_Some] by (clarsimp)
367
+
368
+ lemma dhops_addpreRT [simp]:
369
+ fixes rt pre ip dip
370
+ assumes "dip \<in> kD rt"
371
+ shows "dhops (the (addpreRT rt dip pre)) ip = dhops rt ip"
372
+ unfolding addpreRT_def
373
+ using assms [THEN kD_Some] by (clarsimp)
374
+
375
+ lemma sqnf_addpreRT [simp]:
376
+ "\<And>ip dip. ip\<in>kD(rt \<xi>) \<Longrightarrow> sqnf (the (addpreRT (rt \<xi>) ip npre)) dip = sqnf (rt \<xi>) dip"
377
+ unfolding sqnf_def addpreRT_def by auto
378
+
379
+ subsubsection "Updating route entries"
380
+
381
+ lemma in_kD_case [simp]:
382
+ fixes dip rt
383
+ assumes "dip \<in> kD(rt)"
384
+ shows "(case rt dip of None \<Rightarrow> en | Some r \<Rightarrow> es r) = es (the (rt dip))"
385
+ using assms [THEN kD_Some] by auto
386
+
387
+ lemma not_in_kD_case [simp]:
388
+ fixes dip rt
389
+ assumes "dip \<notin> kD(rt)"
390
+ shows "(case rt dip of None \<Rightarrow> en | Some r \<Rightarrow> es r) = en"
391
+ using assms [THEN kD_None] by auto
392
+
393
+ lemma rt_Some_sqn [dest]:
394
+ fixes rt and ip dsn dsk flag hops nhip pre
395
+ assumes "rt ip = Some (dsn, dsk, flag, hops, nhip, pre)"
396
+ shows "sqn rt ip = dsn"
397
+ unfolding sqn_def using assms by simp
398
+
399
+ lemma not_kD_sqn [simp]:
400
+ fixes dip rt
401
+ assumes "dip \<notin> kD(rt)"
402
+ shows "sqn rt dip = 0"
403
+ using assms unfolding sqn_def
404
+ by simp
405
+
406
+ definition update_arg_wf :: "r \<Rightarrow> bool"
407
+ where "update_arg_wf r \<equiv> \<pi>\<^sub>4(r) = val \<and>
408
+ (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk) \<and>
409
+ (\<pi>\<^sub>3(r) = unk \<longrightarrow> \<pi>\<^sub>5(r) = 1)"
410
+
411
+ lemma update_arg_wf_gives_cases:
412
+ "\<And>r. update_arg_wf r \<Longrightarrow> (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
413
+ unfolding update_arg_wf_def by simp
414
+
415
+ lemma update_arg_wf_tuples [simp]:
416
+ "\<And>nhip pre. update_arg_wf (0, unk, val, Suc 0, nhip, pre)"
417
+ "\<And>n hops nhip pre. update_arg_wf (Suc n, kno, val, hops, nhip, pre)"
418
+ unfolding update_arg_wf_def by auto
419
+
420
+ lemma update_arg_wf_tuples' [elim]:
421
+ "\<And>n hops nhip pre. Suc 0 \<le> n \<Longrightarrow> update_arg_wf (n, kno, val, hops, nhip, pre)"
422
+ unfolding update_arg_wf_def by auto
423
+
424
+ lemma wf_r_cases [intro]:
425
+ fixes P r
426
+ assumes "update_arg_wf r"
427
+ and c1: "\<And>nhip pre. P (0, unk, val, Suc 0, nhip, pre)"
428
+ and c2: "\<And>dsn hops nhip pre. dsn > 0 \<Longrightarrow> P (dsn, kno, val, hops, nhip, pre)"
429
+ shows "P r"
430
+ proof -
431
+ obtain dsn dsk flag hops nhip pre
432
+ where *: "r = (dsn, dsk, flag, hops, nhip, pre)" by (cases r)
433
+ with \<open>update_arg_wf r\<close> have wf1: "flag = val"
434
+ and wf2: "(dsn = 0) = (dsk = unk)"
435
+ and wf3: "dsk = unk \<longrightarrow> (hops = 1)"
436
+ unfolding update_arg_wf_def by auto
437
+ have "P (dsn, dsk, flag, hops, nhip, pre)"
438
+ proof (cases dsk)
439
+ assume "dsk = unk"
440
+ moreover with wf2 wf3 have "dsn = 0" and "hops = Suc 0" by auto
441
+ ultimately show ?thesis using \<open>flag = val\<close> by simp (rule c1)
442
+ next
443
+ assume "dsk = kno"
444
+ moreover with wf2 have "dsn > 0" by simp
445
+ ultimately show ?thesis using \<open>flag = val\<close> by simp (rule c2)
446
+ qed
447
+ with * show "P r" by simp
448
+ qed
449
+
450
+ definition update :: "rt \<Rightarrow> ip \<Rightarrow> r \<Rightarrow> rt"
451
+ where
452
+ "update rt ip r \<equiv>
453
+ case \<sigma>\<^bsub>route\<^esub>(rt, ip) of
454
+ None \<Rightarrow> rt (ip \<mapsto> r)
455
+ | Some s \<Rightarrow>
456
+ if \<pi>\<^sub>2(s) < \<pi>\<^sub>2(r) then rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(s)))
457
+ else if \<pi>\<^sub>2(s) = \<pi>\<^sub>2(r) \<and> (\<pi>\<^sub>5(s) > \<pi>\<^sub>5(r) \<or> \<pi>\<^sub>4(s) = inv)
458
+ then rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(s)))
459
+ else if \<pi>\<^sub>3(r) = unk
460
+ then rt (ip \<mapsto> (\<pi>\<^sub>2(s), snd (addpre r (\<pi>\<^sub>7(s)))))
461
+ else rt (ip \<mapsto> addpre s (\<pi>\<^sub>7(r)))"
462
+
463
+ lemma update_simps [simp]:
464
+ fixes r s nrt nr nr' ns rt ip
465
+ defines "s \<equiv> the \<sigma>\<^bsub>route\<^esub>(rt, ip)"
466
+ and "nr \<equiv> addpre r (\<pi>\<^sub>7(s))"
467
+ and "nr' \<equiv> (\<pi>\<^sub>2(s), \<pi>\<^sub>3(nr), \<pi>\<^sub>4(nr), \<pi>\<^sub>5(nr), \<pi>\<^sub>6(nr), \<pi>\<^sub>7(nr))"
468
+ and "ns \<equiv> addpre s (\<pi>\<^sub>7(r))"
469
+ shows
470
+ "\<lbrakk>ip \<notin> kD(rt)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> r)"
471
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip < \<pi>\<^sub>2(r)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
472
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r);
473
+ the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
474
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r);
475
+ flag rt ip = Some inv\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr)"
476
+ "\<lbrakk>ip \<in> kD(rt); \<pi>\<^sub>3(r) = unk; (\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<rbrakk> \<Longrightarrow> update rt ip r = rt (ip \<mapsto> nr')"
477
+ "\<lbrakk>ip \<in> kD(rt); sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
478
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val \<rbrakk>
479
+ \<Longrightarrow> update rt ip r = rt (ip \<mapsto> ns)"
480
+ proof -
481
+ assume "ip\<notin>kD(rt)"
482
+ hence "\<sigma>\<^bsub>route\<^esub>(rt, ip) = None" ..
483
+ thus "update rt ip r = rt (ip \<mapsto> r)"
484
+ unfolding update_def by simp
485
+ next
486
+ assume "ip \<in> kD(rt)"
487
+ and "sqn rt ip < \<pi>\<^sub>2(r)"
488
+ from this(1) obtain dsn dsk fl hops nhip pre
489
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
490
+ by (metis kD_Some)
491
+ with \<open>sqn rt ip < \<pi>\<^sub>2(r)\<close> show "update rt ip r = rt (ip \<mapsto> nr)"
492
+ unfolding update_def nr_def s_def by auto
493
+ next
494
+ assume "ip \<in> kD(rt)"
495
+ and "sqn rt ip = \<pi>\<^sub>2(r)"
496
+ and "the (dhops rt ip) > \<pi>\<^sub>5(r)"
497
+ from this(1) obtain dsn dsk fl hops nhip pre
498
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
499
+ by (metis kD_Some)
500
+ with \<open>sqn rt ip = \<pi>\<^sub>2(r)\<close> and \<open>the (dhops rt ip) > \<pi>\<^sub>5(r)\<close>
501
+ show "update rt ip r = rt (ip \<mapsto> nr)"
502
+ unfolding update_def nr_def s_def by auto
503
+ next
504
+ assume "ip \<in> kD(rt)"
505
+ and "sqn rt ip = \<pi>\<^sub>2(r)"
506
+ and "flag rt ip = Some inv"
507
+ from this(1) obtain dsn dsk fl hops nhip pre
508
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
509
+ by (metis kD_Some)
510
+ with \<open>sqn rt ip = \<pi>\<^sub>2(r)\<close> and \<open>flag rt ip = Some inv\<close>
511
+ show "update rt ip r = rt (ip \<mapsto> nr)"
512
+ unfolding update_def nr_def s_def by auto
513
+ next
514
+ assume "ip \<in> kD(rt)"
515
+ and "\<pi>\<^sub>3(r) = unk"
516
+ and "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
517
+ from this(1) obtain dsn dsk fl hops nhip pre
518
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
519
+ by (metis kD_Some)
520
+ with \<open>(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<close> and \<open>\<pi>\<^sub>3(r) = unk\<close>
521
+ show "update rt ip r = rt (ip \<mapsto> nr')"
522
+ unfolding update_def nr'_def nr_def s_def
523
+ by (cases r) simp
524
+ next
525
+ assume "ip \<in> kD(rt)"
526
+ and otherassms: "sqn rt ip \<ge> \<pi>\<^sub>2(r)"
527
+ "\<pi>\<^sub>3(r) = kno"
528
+ "sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val"
529
+ from this(1) obtain dsn dsk fl hops nhip pre
530
+ where "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
531
+ by (metis kD_Some)
532
+ with otherassms show "update rt ip r = rt (ip \<mapsto> ns)"
533
+ unfolding update_def ns_def s_def by auto
534
+ qed
535
+
536
+ lemma update_cases [elim]:
537
+ assumes "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
538
+ and c1: "\<lbrakk>ip \<notin> kD(rt)\<rbrakk> \<Longrightarrow> P (rt (ip \<mapsto> r))"
539
+
540
+ and c2: "\<lbrakk>ip \<in> kD(rt); sqn rt ip < \<pi>\<^sub>2(r)\<rbrakk>
541
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
542
+ and c3: "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r); the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk>
543
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
544
+ and c4: "\<lbrakk>ip \<in> kD(rt); sqn rt ip = \<pi>\<^sub>2(r); the (flag rt ip) = inv\<rbrakk>
545
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
546
+ and c5: "\<lbrakk>ip \<in> kD(rt); \<pi>\<^sub>3(r) = unk\<rbrakk>
547
+ \<Longrightarrow> P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r),
548
+ \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r), \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))))"
549
+ and c6: "\<lbrakk>ip \<in> kD(rt); sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
550
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val\<rbrakk>
551
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre (the \<sigma>\<^bsub>route\<^esub>(rt, ip)) (\<pi>\<^sub>7(r))))"
552
+ shows "(P (update rt ip r))"
553
+ proof (cases "ip \<in> kD(rt)")
554
+ assume "ip \<notin> kD(rt)"
555
+ with c1 show ?thesis
556
+ by simp
557
+ next
558
+ assume "ip \<in> kD(rt)"
559
+ moreover then obtain dsn dsk fl hops nhip pre
560
+ where rteq: "rt ip = Some (dsn, dsk, fl, hops, nhip, pre)"
561
+ by (metis kD_Some)
562
+ moreover obtain dsn' dsk' fl' hops' nhip' pre'
563
+ where req: "r = (dsn', dsk', fl', hops', nhip', pre')"
564
+ by (cases r) metis
565
+ ultimately show ?thesis
566
+ using \<open>(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)\<close>
567
+ c2 [OF \<open>ip\<in>kD(rt)\<close>]
568
+ c3 [OF \<open>ip\<in>kD(rt)\<close>]
569
+ c4 [OF \<open>ip\<in>kD(rt)\<close>]
570
+ c5 [OF \<open>ip\<in>kD(rt)\<close>]
571
+ c6 [OF \<open>ip\<in>kD(rt)\<close>]
572
+ unfolding update_def sqn_def by auto
573
+ qed
574
+
575
+ lemma update_cases_kD:
576
+ assumes "(\<pi>\<^sub>2(r) = 0) = (\<pi>\<^sub>3(r) = unk)"
577
+ and "ip \<in> kD(rt)"
578
+ and c2: "sqn rt ip < \<pi>\<^sub>2(r) \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
579
+ and c3: "\<lbrakk>sqn rt ip = \<pi>\<^sub>2(r); the (dhops rt ip) > \<pi>\<^sub>5(r)\<rbrakk>
580
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
581
+ and c4: "\<lbrakk>sqn rt ip = \<pi>\<^sub>2(r); the (flag rt ip) = inv\<rbrakk>
582
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))"
583
+ and c5: "\<pi>\<^sub>3(r) = unk \<Longrightarrow> P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r),
584
+ \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r),
585
+ \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the \<sigma>\<^bsub>route\<^esub>(rt, ip)))))))"
586
+ and c6: "\<lbrakk>sqn rt ip \<ge> \<pi>\<^sub>2(r); \<pi>\<^sub>3(r) = kno;
587
+ sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val\<rbrakk>
588
+ \<Longrightarrow> P (rt (ip \<mapsto> addpre (the \<sigma>\<^bsub>route\<^esub>(rt, ip)) (\<pi>\<^sub>7(r))))"
589
+ shows "(P (update rt ip r))"
590
+ using assms(1) proof (rule update_cases)
591
+ assume "sqn rt ip < \<pi>\<^sub>2(r)"
592
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7(the (rt ip)))))" by (rule c2)
593
+ next
594
+ assume "sqn rt ip = \<pi>\<^sub>2(r)"
595
+ and "the (dhops rt ip) > \<pi>\<^sub>5(r)"
596
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7 (the (rt ip)))))"
597
+ by (rule c3)
598
+ next
599
+ assume "sqn rt ip = \<pi>\<^sub>2(r)"
600
+ and "the (flag rt ip) = inv"
601
+ thus "P (rt(ip \<mapsto> addpre r (\<pi>\<^sub>7 (the (rt ip)))))"
602
+ by (rule c4)
603
+ next
604
+ assume "\<pi>\<^sub>3(r) = unk"
605
+ thus "P (rt (ip \<mapsto> (\<pi>\<^sub>2(the \<sigma>\<^bsub>route\<^esub>(rt, ip)), \<pi>\<^sub>3(r), \<pi>\<^sub>4(r), \<pi>\<^sub>5(r), \<pi>\<^sub>6(r),
606
+ \<pi>\<^sub>7(addpre r (\<pi>\<^sub>7(the (rt ip)))))))"
607
+ by (rule c5)
608
+ next
609
+ assume "sqn rt ip \<ge> \<pi>\<^sub>2(r)"
610
+ and "\<pi>\<^sub>3(r) = kno"
611
+ and "sqn rt ip = \<pi>\<^sub>2(r) \<Longrightarrow> the (dhops rt ip) \<le> \<pi>\<^sub>5(r) \<and> the (flag rt ip) = val"
612
+ thus "P (rt (ip \<mapsto> addpre (the (rt ip)) (\<pi>\<^sub>7(r))))"
613
+ by (rule c6)
614
+ qed (simp add: \<open>ip \<in> kD(rt)\<close>)
615
+
616
+ lemma in_kD_after_update [simp]:
617
+ fixes rt nip dsn dsk flag hops nhip pre
618
+ shows "kD (update rt nip (dsn, dsk, flag, hops, nhip, pre)) = insert nip (kD rt)"
619
+ unfolding update_def
620
+ by (cases "rt nip") auto
621
+
622
+ lemma nhop_of_update [simp]:
623
+ fixes rt dip dsn dsk flag hops nhip
624
+ assumes "rt \<noteq> update rt dip (dsn, dsk, flag, hops, nhip, {})"
625
+ shows "the (nhop (update rt dip (dsn, dsk, flag, hops, nhip, {})) dip) = nhip"
626
+ proof -
627
+ from assms
628
+ have update_neq: "\<And>v. rt dip = Some v \<Longrightarrow>
629
+ update rt dip (dsn, dsk, flag, hops, nhip, {})
630
+ \<noteq> rt(dip \<mapsto> addpre (the (rt dip)) (\<pi>\<^sub>7 (dsn, dsk, flag, hops, nhip, {})))"
631
+ by auto
632
+ show ?thesis
633
+ proof (cases "rt dip = None")
634
+ assume "rt dip = None"
635
+ thus "?thesis" unfolding update_def by clarsimp
636
+ next
637
+ assume "rt dip \<noteq> None"
638
+ then obtain v where "rt dip = Some v" by (metis not_None_eq)
639
+ with update_neq [OF this] show ?thesis
640
+ unfolding update_def by auto
641
+ qed
642
+ qed
643
+
644
+ lemma sqn_if_updated:
645
+ fixes rip v rt ip
646
+ shows "sqn (\<lambda>x. if x = rip then Some v else rt x) ip
647
+ = (if ip = rip then \<pi>\<^sub>2(v) else sqn rt ip)"
648
+ unfolding sqn_def by simp
649
+
650
+ lemma update_sqn [simp]:
651
+ fixes rt dip rip dsn dsk hops nhip pre
652
+ assumes "(dsn = 0) = (dsk = unk)"
653
+ shows "sqn rt dip \<le> sqn (update rt rip (dsn, dsk, val, hops, nhip, pre)) dip"
654
+ proof (rule update_cases)
655
+ show "(\<pi>\<^sub>2 (dsn, dsk, val, hops, nhip, pre) = 0) = (\<pi>\<^sub>3 (dsn, dsk, val, hops, nhip, pre) = unk)"
656
+ by simp (rule assms)
657
+ qed (clarsimp simp: sqn_if_updated sqn_def)+
658
+
659
+ lemma sqn_update_bigger [simp]:
660
+ fixes rt ip ip' dsn dsk flag hops nhip pre
661
+ assumes "1 \<le> hops"
662
+ shows "sqn rt ip \<le> sqn (update rt ip' (dsn, dsk, flag, hops, nhip, pre)) ip"
663
+ using assms unfolding update_def sqn_def
664
+ by (clarsimp split: option.split) auto
665
+
666
+ lemma dhops_update [intro]:
667
+ fixes rt dsn dsk flag hops ip rip nhip pre
668
+ assumes ex: "\<forall>ip\<in>kD rt. the (dhops rt ip) \<ge> 1"
669
+ and ip: "(ip = rip \<and> Suc 0 \<le> hops) \<or> (ip \<noteq> rip \<and> ip\<in>kD rt)"
670
+ shows "Suc 0 \<le> the (dhops (update rt rip (dsn, dsk, flag, hops, nhip, pre)) ip)"
671
+ using ip proof
672
+ assume "ip = rip \<and> Suc 0 \<le> hops" thus ?thesis
673
+ unfolding update_def using ex
674
+ by (cases "rip \<in> kD rt") (drule(1) bspec, auto)
675
+ next
676
+ assume "ip \<noteq> rip \<and> ip\<in>kD rt" thus ?thesis
677
+ using ex unfolding update_def
678
+ by (cases "rip\<in>kD rt") auto
679
+ qed
680
+
681
+ lemma update_another [simp]:
682
+ fixes dip ip rt dsn dsk flag hops nhip pre
683
+ assumes "ip \<noteq> dip"
684
+ shows "(update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = rt ip"
685
+ using assms unfolding update_def
686
+ by (clarsimp split: option.split)
687
+
688
+ lemma nhop_update_another [simp]:
689
+ fixes dip ip rt dsn dsk flag hops nhip pre
690
+ assumes "ip \<noteq> dip"
691
+ shows "nhop (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = nhop rt ip"
692
+ using assms unfolding update_def
693
+ by (clarsimp split: option.split)
694
+
695
+ lemma dhops_update_another [simp]:
696
+ fixes dip ip rt dsn dsk flag hops nhip pre
697
+ assumes "ip \<noteq> dip"
698
+ shows "dhops (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = dhops rt ip"
699
+ using assms unfolding update_def
700
+ by (clarsimp split: option.split)
701
+
702
+ lemma sqn_update_same [simp]:
703
+ "\<And>rt ip dsn dsk flag hops nhip pre. sqn (rt(ip \<mapsto> v)) ip = \<pi>\<^sub>2(v)"
704
+ unfolding sqn_def by simp
705
+
706
+ lemma dhops_update_changed [simp]:
707
+ fixes rt dip osn hops nhip
708
+ assumes "rt \<noteq> update rt dip (osn, kno, val, hops, nhip, {})"
709
+ shows "the (dhops (update rt dip (osn, kno, val, hops, nhip, {})) dip) = hops"
710
+ using assms unfolding update_def
711
+ by (clarsimp split: option.split_asm option.split if_split_asm) auto
712
+
713
+ lemma nhop_update_unk_val [simp]:
714
+ "\<And>rt dip ip dsn hops npre.
715
+ the (nhop (update rt dip (dsn, unk, val, hops, ip, npre)) dip) = ip"
716
+ unfolding update_def by (clarsimp split: option.split)
717
+
718
+ lemma nhop_update_changed [simp]:
719
+ fixes rt dip dsn dsk flg hops sip
720
+ assumes "update rt dip (dsn, dsk, flg, hops, sip, {}) \<noteq> rt"
721
+ shows "the (nhop (update rt dip (dsn, dsk, flg, hops, sip, {})) dip) = sip"
722
+ using assms unfolding update_def
723
+ by (clarsimp split: option.splits if_split_asm) auto
724
+
725
+ lemma update_rt_split_asm:
726
+ "\<And>rt ip dsn dsk flag hops sip.
727
+ P (update rt ip (dsn, dsk, flag, hops, sip, {}))
728
+ =
729
+ (\<not>(rt = update rt ip (dsn, dsk, flag, hops, sip, {}) \<and> \<not>P rt
730
+ \<or> rt \<noteq> update rt ip (dsn, dsk, flag, hops, sip, {})
731
+ \<and> \<not>P (update rt ip (dsn, dsk, flag, hops, sip, {}))))"
732
+ by auto
733
+
734
+ lemma sqn_update [simp]: "\<And>rt dip dsn flg hops sip.
735
+ rt \<noteq> update rt dip (dsn, kno, flg, hops, sip, {})
736
+ \<Longrightarrow> sqn (update rt dip (dsn, kno, flg, hops, sip, {})) dip = dsn"
737
+ unfolding update_def by (clarsimp split: option.split if_split_asm) auto
738
+
739
+ lemma sqnf_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
740
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
741
+ \<Longrightarrow> sqnf (update rt dip (dsn, dsk, flg, hops, sip, {})) dip = dsk"
742
+ unfolding update_def sqnf_def
743
+ by (clarsimp split: option.splits if_split_asm) auto
744
+
745
+ lemma update_kno_dsn_greater_zero:
746
+ "\<And>rt dip ip dsn hops npre. 1 \<le> dsn \<Longrightarrow> 1 \<le> (sqn (update rt dip (dsn, kno, val, hops, ip, npre)) dip)"
747
+ unfolding update_def
748
+ by (clarsimp split: option.splits)
749
+
750
+ lemma proj3_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
751
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
752
+ \<Longrightarrow> \<pi>\<^sub>3(the (update rt dip (dsn, dsk, flg, hops, sip, {}) dip)) = dsk"
753
+ unfolding update_def sqnf_def
754
+ by (clarsimp split: option.splits if_split_asm) auto
755
+
756
+ lemma nhop_update_changed_kno_val [simp]: "\<And>rt ip dsn dsk hops nhip.
757
+ rt \<noteq> update rt ip (dsn, kno, val, hops, nhip, {})
758
+ \<Longrightarrow> the (nhop (update rt ip (dsn, kno, val, hops, nhip, {})) ip) = nhip"
759
+ unfolding update_def
760
+ by (clarsimp split: option.split_asm option.split if_split_asm) auto
761
+
762
+ lemma flag_update [simp]: "\<And>rt dip dsn flg hops sip.
763
+ rt \<noteq> update rt dip (dsn, kno, flg, hops, sip, {})
764
+ \<Longrightarrow> the (flag (update rt dip (dsn, kno, flg, hops, sip, {})) dip) = flg"
765
+ unfolding update_def
766
+ by (clarsimp split: option.split if_split_asm) auto
767
+
768
+ lemma the_flag_Some [dest!]:
769
+ fixes ip rt
770
+ assumes "the (flag rt ip) = x"
771
+ and "ip \<in> kD rt"
772
+ shows "flag rt ip = Some x"
773
+ using assms by auto
774
+
775
+ lemma kD_update_unchanged [dest]:
776
+ fixes rt dip dsn dsk flag hops nhip pre
777
+ assumes "rt = update rt dip (dsn, dsk, flag, hops, nhip, pre)"
778
+ shows "dip\<in>kD(rt)"
779
+ proof -
780
+ have "dip\<in>kD(update rt dip (dsn, dsk, flag, hops, nhip, pre))" by simp
781
+ with assms show ?thesis by simp
782
+ qed
783
+
784
+ lemma nhop_update [simp]: "\<And>rt dip dsn dsk flg hops sip.
785
+ rt \<noteq> update rt dip (dsn, dsk, flg, hops, sip, {})
786
+ \<Longrightarrow> the (nhop (update rt dip (dsn, dsk, flg, hops, sip, {})) dip) = sip"
787
+ unfolding update_def sqnf_def
788
+ by (clarsimp split: option.splits if_split_asm) auto
789
+
790
+ lemma sqn_update_another [simp]:
791
+ fixes dip ip rt dsn dsk flag hops nhip pre
792
+ assumes "ip \<noteq> dip"
793
+ shows "sqn (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = sqn rt ip"
794
+ using assms unfolding update_def sqn_def
795
+ by (clarsimp split: option.splits) auto
796
+
797
+ lemma sqnf_update_another [simp]:
798
+ fixes dip ip rt dsn dsk flag hops nhip pre
799
+ assumes "ip \<noteq> dip"
800
+ shows "sqnf (update rt dip (dsn, dsk, flag, hops, nhip, pre)) ip = sqnf rt ip"
801
+ using assms unfolding update_def sqnf_def
802
+ by (clarsimp split: option.splits) auto
803
+
804
+ lemma vD_update_val [dest]:
805
+ "\<And>dip rt dip' dsn dsk hops nhip pre.
806
+ dip \<in> vD(update rt dip' (dsn, dsk, val, hops, nhip, pre)) \<Longrightarrow> (dip\<in>vD(rt) \<or> dip=dip')"
807
+ unfolding update_def vD_def by (clarsimp split: option.split_asm if_split_asm)
808
+
809
+ subsubsection "Invalidating route entries"
810
+
811
+ definition invalidate :: "rt \<Rightarrow> (ip \<rightharpoonup> sqn) \<Rightarrow> rt"
812
+ where "invalidate rt dests \<equiv>
813
+ \<lambda>ip. case (rt ip, dests ip) of
814
+ (None, _) \<Rightarrow> None
815
+ | (Some s, None) \<Rightarrow> Some s
816
+ | (Some (_, dsk, _, hops, nhip, pre), Some rsn) \<Rightarrow>
817
+ Some (rsn, dsk, inv, hops, nhip, pre)"
818
+
819
+ lemma proj3_invalidate [simp]:
820
+ "\<And>dip. \<pi>\<^sub>3(the ((invalidate rt dests) dip)) = \<pi>\<^sub>3(the (rt dip))"
821
+ unfolding invalidate_def by (clarsimp split: option.split)
822
+
823
+ lemma proj5_invalidate [simp]:
824
+ "\<And>dip. \<pi>\<^sub>5(the ((invalidate rt dests) dip)) = \<pi>\<^sub>5(the (rt dip))"
825
+ unfolding invalidate_def by (clarsimp split: option.split)
826
+
827
+ lemma proj6_invalidate [simp]:
828
+ "\<And>dip. \<pi>\<^sub>6(the ((invalidate rt dests) dip)) = \<pi>\<^sub>6(the (rt dip))"
829
+ unfolding invalidate_def by (clarsimp split: option.split)
830
+
831
+ lemma proj7_invalidate [simp]:
832
+ "\<And>dip. \<pi>\<^sub>7(the ((invalidate rt dests) dip)) = \<pi>\<^sub>7(the (rt dip))"
833
+ unfolding invalidate_def by (clarsimp split: option.split)
834
+
835
+ lemma invalidate_kD_inv [simp]:
836
+ "\<And>rt dests. kD (invalidate rt dests) = kD rt"
837
+ unfolding invalidate_def kD_def
838
+ by (simp split: option.split)
839
+
840
+ lemma invalidate_sqn:
841
+ fixes rt dip dests
842
+ assumes "\<forall>rsn. dests dip = Some rsn \<longrightarrow> sqn rt dip \<le> rsn"
843
+ shows "sqn rt dip \<le> sqn (invalidate rt dests) dip"
844
+ proof (cases "dip \<notin> kD(rt)")
845
+ assume "\<not> dip \<notin> kD(rt)"
846
+ hence "dip\<in>kD(rt)" by simp
847
+ then obtain dsn dsk flag hops nhip pre where "rt dip = Some (dsn, dsk, flag, hops, nhip, pre)"
848
+ by (metis kD_Some)
849
+ with assms show "sqn rt dip \<le> sqn (invalidate rt dests) dip"
850
+ by (cases "dests dip") (auto simp add: invalidate_def sqn_def)
851
+ qed simp
852
+
853
+ lemma sqn_invalidate_in_dests [simp]:
854
+ fixes dests ipa rsn rt
855
+ assumes "dests ipa = Some rsn"
856
+ and "ipa\<in>kD(rt)"
857
+ shows "sqn (invalidate rt dests) ipa = rsn"
858
+ unfolding invalidate_def sqn_def
859
+ using assms(1) assms(2) [THEN kD_Some]
860
+ by clarsimp
861
+
862
+ lemma dhops_invalidate [simp]:
863
+ "\<And>dip. the (dhops (invalidate rt dests) dip) = the (dhops rt dip)"
864
+ unfolding invalidate_def by (clarsimp split: option.split)
865
+
866
+ lemma sqnf_invalidate [simp]:
867
+ "\<And>dip. sqnf (invalidate (rt \<xi>) (dests \<xi>)) dip = sqnf (rt \<xi>) dip"
868
+ unfolding sqnf_def invalidate_def by (clarsimp split: option.split)
869
+
870
+ lemma nhop_invalidate [simp]:
871
+ "\<And>dip. the (nhop (invalidate (rt \<xi>) (dests \<xi>)) dip) = the (nhop (rt \<xi>) dip)"
872
+ unfolding invalidate_def by (clarsimp split: option.split)
873
+
874
+ lemma invalidate_other [simp]:
875
+ fixes rt dests dip
876
+ assumes "dip\<notin>dom(dests)"
877
+ shows "invalidate rt dests dip = rt dip"
878
+ using assms unfolding invalidate_def
879
+ by (clarsimp split: option.split_asm)
880
+
881
+ lemma invalidate_none [simp]:
882
+ fixes rt dests dip
883
+ assumes "dip\<notin>kD(rt)"
884
+ shows "invalidate rt dests dip = None"
885
+ using assms unfolding invalidate_def by clarsimp
886
+
887
+ lemma vD_invalidate_vD_not_dests:
888
+ "\<And>dip rt dests. dip\<in>vD(invalidate rt dests) \<Longrightarrow> dip\<in>vD(rt) \<and> dests dip = None"
889
+ unfolding invalidate_def vD_def
890
+ by (clarsimp split: option.split_asm)
891
+
892
+ lemma sqn_invalidate_not_in_dests [simp]:
893
+ fixes dests dip rt
894
+ assumes "dip\<notin>dom(dests)"
895
+ shows "sqn (invalidate rt dests) dip = sqn rt dip"
896
+ using assms unfolding sqn_def by simp
897
+
898
+ lemma invalidate_changes:
899
+ fixes rt dests dip dsn dsk flag hops nhip pre
900
+ assumes "invalidate rt dests dip = Some (dsn, dsk, flag, hops, nhip, pre)"
901
+ shows " dsn = (case dests dip of None \<Rightarrow> \<pi>\<^sub>2(the (rt dip)) | Some rsn \<Rightarrow> rsn)
902
+ \<and> dsk = \<pi>\<^sub>3(the (rt dip))
903
+ \<and> flag = (if dests dip = None then \<pi>\<^sub>4(the (rt dip)) else inv)
904
+ \<and> hops = \<pi>\<^sub>5(the (rt dip))
905
+ \<and> nhip = \<pi>\<^sub>6(the (rt dip))
906
+ \<and> pre = \<pi>\<^sub>7(the (rt dip))"
907
+ using assms unfolding invalidate_def
908
+ by (cases "rt dip", clarsimp, cases "dests dip") auto
909
+
910
+
911
+ lemma proj3_inv: "\<And>dip rt dests. dip\<in>kD (rt)
912
+ \<Longrightarrow> \<pi>\<^sub>3(the (invalidate rt dests dip)) = \<pi>\<^sub>3(the (rt dip))"
913
+ by (clarsimp simp: invalidate_def kD_def split: option.split)
914
+
915
+ lemma dests_iD_invalidate [simp]:
916
+ assumes "dests ip = Some rsn"
917
+ and "ip\<in>kD(rt)"
918
+ shows "ip\<in>iD(invalidate rt dests)"
919
+ using assms(1) assms(2) [THEN kD_Some] unfolding invalidate_def iD_def
920
+ by (clarsimp split: option.split)
921
+
922
+ subsection "Route Requests"
923
+
924
+ text \<open>Generate a fresh route request identifier.\<close>
925
+
926
+ definition nrreqid :: "(ip \<times> rreqid) set \<Rightarrow> ip \<Rightarrow> rreqid"
927
+ where "nrreqid rreqs ip \<equiv> Max ({n. (ip, n) \<in> rreqs} \<union> {0}) + 1"
928
+
929
+ subsection "Queued Packets"
930
+
931
+ text \<open>Functions for sending data packets.\<close>
932
+
933
+ type_synonym store = "ip \<rightharpoonup> (p \<times> data list)"
934
+
935
+ definition sigma_queue :: "store \<Rightarrow> ip \<Rightarrow> data list" ("\<sigma>\<^bsub>queue\<^esub>'(_, _')")
936
+ where "\<sigma>\<^bsub>queue\<^esub>(store, dip) \<equiv> case store dip of None \<Rightarrow> [] | Some (p, q) \<Rightarrow> q"
937
+
938
+ definition qD :: "store \<Rightarrow> ip set"
939
+ where "qD \<equiv> dom"
940
+
941
+ definition add :: "data \<Rightarrow> ip \<Rightarrow> store \<Rightarrow> store"
942
+ where "add d dip store \<equiv> case store dip of
943
+ None \<Rightarrow> store (dip \<mapsto> (req, [d]))
944
+ | Some (p, q) \<Rightarrow> store (dip \<mapsto> (p, q @ [d]))"
945
+
946
+ lemma qD_add [simp]:
947
+ fixes d dip store
948
+ shows "qD(add d dip store) = insert dip (qD store)"
949
+ unfolding add_def Let_def qD_def
950
+ by (clarsimp split: option.split)
951
+
952
+ definition drop :: "ip \<Rightarrow> store \<rightharpoonup> store"
953
+ where "drop dip store \<equiv>
954
+ map_option (\<lambda>(p, q). if tl q = [] then store (dip := None)
955
+ else store (dip \<mapsto> (p, tl q))) (store dip)"
956
+
957
+ definition sigma_p_flag :: "store \<Rightarrow> ip \<rightharpoonup> p" ("\<sigma>\<^bsub>p-flag\<^esub>'(_, _')")
958
+ where "\<sigma>\<^bsub>p-flag\<^esub>(store, dip) \<equiv> map_option fst (store dip)"
959
+
960
+ definition unsetRRF :: "store \<Rightarrow> ip \<Rightarrow> store"
961
+ where "unsetRRF store dip \<equiv> case store dip of
962
+ None \<Rightarrow> store
963
+ | Some (p, q) \<Rightarrow> store (dip \<mapsto> (noreq, q))"
964
+
965
+ definition setRRF :: "store \<Rightarrow> (ip \<rightharpoonup> sqn) \<Rightarrow> store"
966
+ where "setRRF store dests \<equiv> \<lambda>dip. if dests dip = None then store dip
967
+ else map_option (\<lambda>(_, q). (req, q)) (store dip)"
968
+
969
+ subsection "Comparison with the original technical report"
970
+
971
+ text \<open>
972
+ The major differences with the AODV technical report of Fehnker et al are:
973
+ \begin{enumerate}
974
+ \item @{term nhop} is partial, thus a `@{term the}' is needed, similarly for @{term dhops}
975
+ and @{term addpreRT}.
976
+ \item @{term precs} is partial.
977
+ \item @{term "\<sigma>\<^bsub>p-flag\<^esub>(store, dip)"} is partial.
978
+ \item The routing table (@{typ rt}) is modelled as a map (@{typ "ip \<Rightarrow> r option"})
979
+ rather than a set of 7-tuples, likewise, the @{typ r} is a 6-tuple rather than
980
+ a 7-tuple, i.e., the destination ip-address (@{term "dip"}) is taken from the
981
+ argument to the function, rather than a part of the result. Well-definedness then
982
+ follows from the structure of the type and more related facts are available
983
+ automatically, rather than having to be acquired through tedious proofs.
984
+ \item Similar remarks hold for the dests mapping passed to @{term "invalidate"},
985
+ and @{term "store"}.
986
+ \end{enumerate}
987
+ \<close>
988
+
989
+ end
990
+
formal/afp/AODV/variants/b_fwdrreps/B_Aodv_Loop_Freedom.thy ADDED
@@ -0,0 +1,369 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/b_fwdrreps/Aodv_Loop_Freedom.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Lift and transfer invariants to show loop freedom"
7
+
8
+ theory B_Aodv_Loop_Freedom
9
+ imports AWN.OClosed_Transfer AWN.Qmsg_Lifting B_Global_Invariants B_Loop_Freedom
10
+ begin
11
+
12
+ subsection \<open>Lift to parallel processes with queues\<close>
13
+
14
+ lemma par_step_no_change_on_send_or_receive:
15
+ fixes \<sigma> s a \<sigma>' s'
16
+ assumes "((\<sigma>, s), a, (\<sigma>', s')) \<in> oparp_sos i (oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i) (seqp_sos \<Gamma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G)"
17
+ and "a \<noteq> \<tau>"
18
+ shows "\<sigma>' i = \<sigma> i"
19
+ using assms by (rule qmsg_no_change_on_send_or_receive)
20
+
21
+ lemma par_nhop_quality_increases:
22
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile> (otherwith ((=)) {i} (orecvmsg (\<lambda>\<sigma> m.
23
+ msg_fresh \<sigma> m \<and> msg_zhops m)),
24
+ other quality_increases {i} \<rightarrow>)
25
+ global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
26
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
27
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
28
+ proof (rule lift_into_qmsg [OF seq_nhop_quality_increases])
29
+ show "opaodv i \<Turnstile>\<^sub>A (otherwith ((=)) {i}
30
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
31
+ other quality_increases {i} \<rightarrow>)
32
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
33
+ proof (rule ostep_invariant_weakenE [OF oquality_increases], simp_all)
34
+ fix t :: "(((nat \<Rightarrow> state) \<times> (state, msg, pseqp, pseqp label) seqp), msg seq_action) transition"
35
+ assume "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), _, (\<sigma>', _)). \<forall>j. quality_increases (\<sigma> j) (\<sigma>' j)) t"
36
+ thus "quality_increases (fst (fst t) i) (fst (snd (snd t)) i)"
37
+ by (cases t) (clarsimp dest!: onllD, metis aodv_ex_label)
38
+ next
39
+ fix \<sigma> \<sigma>' a
40
+ assume "otherwith ((=)) {i}
41
+ (orecvmsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)) \<sigma> \<sigma>' a"
42
+ thus "otherwith quality_increases {i} (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma> \<sigma>' a"
43
+ by - (erule weaken_otherwith, auto)
44
+ qed
45
+ qed auto
46
+
47
+ lemma par_rreq_rrep_sn_quality_increases:
48
+ "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
49
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
50
+ proof -
51
+ have "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
52
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
53
+ by (rule ostep_invariant_weakenE [OF olocal_quality_increases])
54
+ (auto dest!: onllD seqllD elim!: aodv_ex_labelE)
55
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
56
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
57
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
58
+ thus ?thesis by rule auto
59
+ qed
60
+
61
+ lemma par_rreq_rrep_nsqn_fresh_any_step:
62
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
63
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
64
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
65
+ proof -
66
+ have "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
67
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
68
+ proof (rule ostep_invariant_weakenE [OF rreq_rrep_nsqn_fresh_any_step_invariant])
69
+ fix t
70
+ assume "onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>((\<sigma>, _), a, _). anycast (msg_fresh \<sigma>) a) t"
71
+ thus "globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a) t"
72
+ by (cases t) (clarsimp dest!: onllD, metis aodv_ex_label)
73
+ qed auto
74
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. (orecvmsg (\<lambda>_. rreq_rrep_sn)) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
75
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). anycast (msg_fresh \<sigma>) a)"
76
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
77
+ thus ?thesis by rule auto
78
+ qed
79
+
80
+ lemma par_anycast_msg_zhops:
81
+ shows "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
82
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
83
+ proof -
84
+ from anycast_msg_zhops initiali_aodv oaodv_trans aodv_trans
85
+ have "opaodv i \<Turnstile>\<^sub>A (act TT, other (\<lambda>_ _. True) {i} \<rightarrow>)
86
+ seqll i (onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast msg_zhops a))"
87
+ by (rule open_seq_step_invariant)
88
+ hence "opaodv i \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
89
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
90
+ proof (rule ostep_invariant_weakenE)
91
+ fix t :: "(((nat \<Rightarrow> state) \<times> (state, msg, pseqp, pseqp label) seqp), msg seq_action) transition"
92
+ assume "seqll i (onll \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V (\<lambda>(_, a, _). anycast msg_zhops a)) t"
93
+ thus "globala (\<lambda>(_, a, _). anycast msg_zhops a) t"
94
+ by (cases t) (clarsimp dest!: seqllD onllD, metis aodv_ex_label)
95
+ qed simp_all
96
+ hence "opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. orecvmsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
97
+ globala (\<lambda>(_, a, _). anycast msg_zhops a)"
98
+ by (rule lift_step_into_qmsg_statelessassm) simp_all
99
+ thus ?thesis by rule auto
100
+ qed
101
+
102
+ subsection \<open>Lift to nodes\<close>
103
+
104
+ lemma node_step_no_change_on_send_or_receive:
105
+ assumes "((\<sigma>, NodeS i P R), a, (\<sigma>', NodeS i' P' R')) \<in> onode_sos
106
+ (oparp_sos i (oseqp_sos \<Gamma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i) (seqp_sos \<Gamma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G))"
107
+ and "a \<noteq> \<tau>"
108
+ shows "\<sigma>' i = \<sigma> i"
109
+ using assms
110
+ by (cases a) (auto elim!: par_step_no_change_on_send_or_receive)
111
+
112
+ lemma node_nhop_quality_increases:
113
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>
114
+ (otherwith ((=)) {i}
115
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
116
+ other quality_increases {i}
117
+ \<rightarrow>) global (\<lambda>\<sigma>. \<forall>dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
118
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
119
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
120
+ by (rule node_lift [OF par_nhop_quality_increases]) auto
121
+
122
+ lemma node_quality_increases:
123
+ "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
124
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
125
+ globala (\<lambda>(\<sigma>, _, \<sigma>'). quality_increases (\<sigma> i) (\<sigma>' i))"
126
+ by (rule node_lift_step_statelessassm [OF par_rreq_rrep_sn_quality_increases]) simp
127
+
128
+ lemma node_rreq_rrep_nsqn_fresh_any_step:
129
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A
130
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
131
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). castmsg (msg_fresh \<sigma>) a)"
132
+ by (rule node_lift_anycast_statelessassm [OF par_rreq_rrep_nsqn_fresh_any_step])
133
+
134
+ lemma node_anycast_msg_zhops:
135
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R \<rangle>\<^sub>o \<Turnstile>\<^sub>A
136
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>, other (\<lambda>_ _. True) {i} \<rightarrow>)
137
+ globala (\<lambda>(_, a, _). castmsg msg_zhops a)"
138
+ by (rule node_lift_anycast_statelessassm [OF par_anycast_msg_zhops])
139
+
140
+ lemma node_silent_change_only:
141
+ shows "\<langle> i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i \<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_ _. True) \<sigma>,
142
+ other (\<lambda>_ _. True) {i} \<rightarrow>)
143
+ globala (\<lambda>(\<sigma>, a, \<sigma>'). a \<noteq> \<tau> \<longrightarrow> \<sigma>' i = \<sigma> i)"
144
+ proof (rule ostep_invariantI, simp (no_asm), rule impI)
145
+ fix \<sigma> \<zeta> a \<sigma>' \<zeta>'
146
+ assume or: "(\<sigma>, \<zeta>) \<in> oreachable (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i\<rangle>\<^sub>o)
147
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_ _. True) \<sigma>)
148
+ (other (\<lambda>_ _. True) {i})"
149
+ and tr: "((\<sigma>, \<zeta>), a, (\<sigma>', \<zeta>')) \<in> trans (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<^sub>i\<rangle>\<^sub>o)"
150
+ and "a \<noteq> \<tau>\<^sub>n"
151
+ from or obtain p R where "\<zeta> = NodeS i p R"
152
+ by - (drule node_net_state, metis)
153
+ with tr have "((\<sigma>, NodeS i p R), a, (\<sigma>', \<zeta>'))
154
+ \<in> onode_sos (oparp_sos i (trans (opaodv i)) (trans qmsg))"
155
+ by simp
156
+ thus "\<sigma>' i = \<sigma> i" using \<open>a \<noteq> \<tau>\<^sub>n\<close>
157
+ by (cases rule: onode_sos.cases)
158
+ (auto elim: qmsg_no_change_on_send_or_receive)
159
+ qed
160
+
161
+ subsection \<open>Lift to partial networks\<close>
162
+
163
+ lemma arrive_rreq_rrep_nsqn_fresh_inc_sn [simp]:
164
+ assumes "oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> P \<sigma> m) \<sigma> m"
165
+ shows "oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma> m"
166
+ using assms by (cases m) auto
167
+
168
+ lemma opnet_nhop_quality_increases:
169
+ shows "opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p \<Turnstile>
170
+ (otherwith ((=)) (net_tree_ips p)
171
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)),
172
+ other quality_increases (net_tree_ips p) \<rightarrow>)
173
+ global (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips p. \<forall>dip.
174
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
175
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
176
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
177
+ proof (rule pnet_lift [OF node_nhop_quality_increases])
178
+ fix i R
179
+ have "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>,
180
+ other (\<lambda>_ _. True) {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
181
+ castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a)"
182
+ proof (rule ostep_invariantI, simp (no_asm))
183
+ fix \<sigma> s a \<sigma>' s'
184
+ assume or: "(\<sigma>, s) \<in> oreachable (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o)
185
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma>)
186
+ (other (\<lambda>_ _. True) {i})"
187
+ and tr: "((\<sigma>, s), a, (\<sigma>', s')) \<in> trans (\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o)"
188
+ and am: "oarrivemsg (\<lambda>_. rreq_rrep_sn) \<sigma> a"
189
+ from or tr am have "castmsg (msg_fresh \<sigma>) a"
190
+ by (auto dest!: ostep_invariantD [OF node_rreq_rrep_nsqn_fresh_any_step])
191
+ moreover from or tr am have "castmsg (msg_zhops) a"
192
+ by (auto dest!: ostep_invariantD [OF node_anycast_msg_zhops])
193
+ ultimately show "castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a"
194
+ by (case_tac a) auto
195
+ qed
196
+ thus "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
197
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
198
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, _).
199
+ castmsg (\<lambda>m. msg_fresh \<sigma> m \<and> msg_zhops m) a)"
200
+ by rule auto
201
+ next
202
+ fix i R
203
+ show "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
204
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
205
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
206
+ a \<noteq> \<tau> \<and> (\<forall>d. a \<noteq> i:deliver(d)) \<longrightarrow> \<sigma> i = \<sigma>' i)"
207
+ by (rule ostep_invariant_weakenE [OF node_silent_change_only]) auto
208
+ next
209
+ fix i R
210
+ show "\<langle>i : opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg : R\<rangle>\<^sub>o \<Turnstile>\<^sub>A
211
+ (\<lambda>\<sigma> _. oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma>,
212
+ other quality_increases {i} \<rightarrow>) globala (\<lambda>(\<sigma>, a, \<sigma>').
213
+ a = \<tau> \<or> (\<exists>d. a = i:deliver(d)) \<longrightarrow> quality_increases (\<sigma> i) (\<sigma>' i))"
214
+ by (rule ostep_invariant_weakenE [OF node_quality_increases]) auto
215
+ qed simp_all
216
+
217
+ subsection \<open>Lift to closed networks\<close>
218
+
219
+ lemma onet_nhop_quality_increases:
220
+ shows "oclosed (opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p)
221
+ \<Turnstile> (\<lambda>_ _ _. True, other quality_increases (net_tree_ips p) \<rightarrow>)
222
+ global (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips p. \<forall>dip.
223
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
224
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
225
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
226
+ (is "_ \<Turnstile> (_, ?U \<rightarrow>) ?inv")
227
+ proof (rule inclosed_closed)
228
+ from opnet_nhop_quality_increases
229
+ show "opnet (\<lambda>i. opaodv i \<langle>\<langle>\<^bsub>i\<^esub> qmsg) p
230
+ \<Turnstile> (otherwith ((=)) (net_tree_ips p) inoclosed, ?U \<rightarrow>) ?inv"
231
+ proof (rule oinvariant_weakenE)
232
+ fix \<sigma> \<sigma>' :: "ip \<Rightarrow> state" and a :: "msg node_action"
233
+ assume "otherwith ((=)) (net_tree_ips p) inoclosed \<sigma> \<sigma>' a"
234
+ thus "otherwith ((=)) (net_tree_ips p)
235
+ (oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m)) \<sigma> \<sigma>' a"
236
+ proof (rule otherwithEI)
237
+ fix \<sigma> :: "ip \<Rightarrow> state" and a :: "msg node_action"
238
+ assume "inoclosed \<sigma> a"
239
+ thus "oarrivemsg (\<lambda>\<sigma> m. msg_fresh \<sigma> m \<and> msg_zhops m) \<sigma> a"
240
+ proof (cases a)
241
+ fix ii ni ms
242
+ assume "a = ii\<not>ni:arrive(ms)"
243
+ moreover with \<open>inoclosed \<sigma> a\<close> obtain d di where "ms = newpkt(d, di)"
244
+ by (cases ms) auto
245
+ ultimately show ?thesis by simp
246
+ qed simp_all
247
+ qed
248
+ qed
249
+ qed
250
+
251
+ subsection \<open>Transfer into the standard model\<close>
252
+
253
+ interpretation aodv_openproc: openproc paodv opaodv id
254
+ rewrites "aodv_openproc.initmissing = initmissing"
255
+ proof -
256
+ show "openproc paodv opaodv id"
257
+ proof unfold_locales
258
+ fix i :: ip
259
+ have "{(\<sigma>, \<zeta>). (\<sigma> i, \<zeta>) \<in> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V i \<and> (\<forall>j. j \<noteq> i \<longrightarrow> \<sigma> j \<in> fst ` \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V j)} \<subseteq> \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'"
260
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V'_def
261
+ proof (rule equalityD1)
262
+ show "\<And>f p. {(\<sigma>, \<zeta>). (\<sigma> i, \<zeta>) \<in> {(f i, p)} \<and> (\<forall>j. j \<noteq> i
263
+ \<longrightarrow> \<sigma> j \<in> fst ` {(f j, p)})} = {(f, p)}"
264
+ by (rule set_eqI) auto
265
+ qed
266
+ thus "{ (\<sigma>, \<zeta>) |\<sigma> \<zeta> s. s \<in> init (paodv i)
267
+ \<and> (\<sigma> i, \<zeta>) = id s
268
+ \<and> (\<forall>j. j\<noteq>i \<longrightarrow> \<sigma> j \<in> (fst o id) ` init (paodv j)) } \<subseteq> init (opaodv i)"
269
+ by simp
270
+ next
271
+ show "\<forall>j. init (paodv j) \<noteq> {}"
272
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
273
+ next
274
+ fix i s a s' \<sigma> \<sigma>'
275
+ assume "\<sigma> i = fst (id s)"
276
+ and "\<sigma>' i = fst (id s')"
277
+ and "(s, a, s') \<in> trans (paodv i)"
278
+ then obtain q q' where "s = (\<sigma> i, q)"
279
+ and "s' = (\<sigma>' i, q')"
280
+ and "((\<sigma> i, q), a, (\<sigma>' i, q')) \<in> trans (paodv i)"
281
+ by (cases s, cases s') auto
282
+ from this(3) have "((\<sigma>, q), a, (\<sigma>', q')) \<in> trans (opaodv i)"
283
+ by simp (rule open_seqp_action [OF aodv_wf])
284
+
285
+ with \<open>s = (\<sigma> i, q)\<close> and \<open>s' = (\<sigma>' i, q')\<close>
286
+ show "((\<sigma>, snd (id s)), a, (\<sigma>', snd (id s'))) \<in> trans (opaodv i)"
287
+ by simp
288
+ qed
289
+ then interpret opn: openproc paodv opaodv id .
290
+ have [simp]: "\<And>i. (SOME x. x \<in> (fst o id) ` init (paodv i)) = aodv_init i"
291
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def by simp
292
+ hence "\<And>i. openproc.initmissing paodv id i = initmissing i"
293
+ unfolding opn.initmissing_def opn.someinit_def initmissing_def
294
+ by (auto split: option.split)
295
+ thus "openproc.initmissing paodv id = initmissing" ..
296
+ qed
297
+
298
+ interpretation aodv_openproc_par_qmsg: openproc_parq paodv opaodv id qmsg
299
+ rewrites "aodv_openproc_par_qmsg.netglobal = netglobal"
300
+ and "aodv_openproc_par_qmsg.initmissing = initmissing"
301
+ proof -
302
+ show "openproc_parq paodv opaodv id qmsg"
303
+ by (unfold_locales) simp
304
+ then interpret opq: openproc_parq paodv opaodv id qmsg .
305
+
306
+ have im: "\<And>\<sigma>. openproc.initmissing (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) \<sigma>
307
+ = initmissing \<sigma>"
308
+ unfolding opq.initmissing_def opq.someinit_def initmissing_def
309
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def by (clarsimp cong: option.case_cong)
310
+ thus "openproc.initmissing (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) = initmissing"
311
+ by (rule ext)
312
+ have "\<And>P \<sigma>. openproc.netglobal (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) P \<sigma>
313
+ = netglobal P \<sigma>"
314
+ unfolding opq.netglobal_def netglobal_def opq.initmissing_def initmissing_def opq.someinit_def
315
+ unfolding \<sigma>\<^sub>A\<^sub>O\<^sub>D\<^sub>V_def \<sigma>\<^sub>Q\<^sub>M\<^sub>S\<^sub>G_def
316
+ by (clarsimp cong: option.case_cong
317
+ simp del: One_nat_def
318
+ simp add: fst_initmissing_netgmap_default_aodv_init_netlift
319
+ [symmetric, unfolded initmissing_def])
320
+ thus "openproc.netglobal (\<lambda>i. paodv i \<langle>\<langle> qmsg) (\<lambda>(p, q). (fst (id p), snd (id p), q)) = netglobal"
321
+ by auto
322
+ qed
323
+
324
+ lemma net_nhop_quality_increases:
325
+ assumes "wf_net_tree n"
326
+ shows "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal
327
+ (\<lambda>\<sigma>. \<forall>i dip. let nhip = the (nhop (rt (\<sigma> i)) dip)
328
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
329
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
330
+ (is "_ \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>i. ?inv \<sigma> i)")
331
+ proof -
332
+ from \<open>wf_net_tree n\<close>
333
+ have proto: "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>i\<in>net_tree_ips n. \<forall>dip.
334
+ let nhip = the (nhop (rt (\<sigma> i)) dip)
335
+ in dip \<in> vD (rt (\<sigma> i)) \<inter> vD (rt (\<sigma> nhip)) \<and> nhip \<noteq> dip
336
+ \<longrightarrow> (rt (\<sigma> i)) \<sqsubset>\<^bsub>dip\<^esub> (rt (\<sigma> nhip)))"
337
+ by (rule aodv_openproc_par_qmsg.close_opnet [OF _ onet_nhop_quality_increases])
338
+ show ?thesis
339
+ unfolding invariant_def opnet_sos.opnet_tau1
340
+ proof (rule, simp only: aodv_openproc_par_qmsg.netglobalsimp
341
+ fst_initmissing_netgmap_pair_fst, rule allI)
342
+ fix \<sigma> i
343
+ assume sr: "\<sigma> \<in> reachable (closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n)) TT"
344
+ hence "\<forall>i\<in>net_tree_ips n. ?inv (fst (initmissing (netgmap fst \<sigma>))) i"
345
+ by - (drule invariantD [OF proto],
346
+ simp only: aodv_openproc_par_qmsg.netglobalsimp
347
+ fst_initmissing_netgmap_pair_fst)
348
+ thus "?inv (fst (initmissing (netgmap fst \<sigma>))) i"
349
+ proof (cases "i\<in>net_tree_ips n")
350
+ assume "i\<notin>net_tree_ips n"
351
+ from sr have "\<sigma> \<in> reachable (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) TT" ..
352
+ hence "net_ips \<sigma> = net_tree_ips n" ..
353
+ with \<open>i\<notin>net_tree_ips n\<close> have "i\<notin>net_ips \<sigma>" by simp
354
+ hence "(fst (initmissing (netgmap fst \<sigma>))) i = aodv_init i"
355
+ by simp
356
+ thus ?thesis by simp
357
+ qed metis
358
+ qed
359
+ qed
360
+
361
+ subsection \<open>Loop freedom of AODV\<close>
362
+
363
+ theorem aodv_loop_freedom:
364
+ assumes "wf_net_tree n"
365
+ shows "closed (pnet (\<lambda>i. paodv i \<langle>\<langle> qmsg) n) \<TTurnstile> netglobal (\<lambda>\<sigma>. \<forall>dip. irrefl ((rt_graph \<sigma> dip)\<^sup>+))"
366
+ using assms by (rule aodv_openproc_par_qmsg.netglobal_weakenE
367
+ [OF net_nhop_quality_increases inv_to_loop_freedom])
368
+
369
+ end
formal/afp/AODV/variants/b_fwdrreps/B_Aodv_Message.thy ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/b_fwdrreps/Aodv_Message.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "AODV protocol messages"
7
+
8
+ theory B_Aodv_Message
9
+ imports B_Fwdrreps
10
+ begin
11
+
12
+ datatype msg =
13
+ Rreq nat rreqid ip sqn k ip sqn ip
14
+ | Rrep nat ip sqn ip ip
15
+ | Rerr "ip \<rightharpoonup> sqn" ip
16
+ | Newpkt data ip
17
+ | Pkt data ip ip
18
+
19
+ instantiation msg :: msg
20
+ begin
21
+ definition newpkt_def [simp]: "newpkt \<equiv> \<lambda>(d, dip). Newpkt d dip"
22
+ definition eq_newpkt_def: "eq_newpkt m \<equiv> case m of Newpkt d dip \<Rightarrow> True | _ \<Rightarrow> False"
23
+
24
+ instance by intro_classes (simp add: eq_newpkt_def)
25
+ end
26
+
27
+ text \<open>The @{type msg} type models the different messages used within AODV.
28
+ The instantiation as a @{class msg} is a technicality due to the special
29
+ treatment of @{term newpkt} messages in the AWN SOS rules.
30
+ This use of classes allows a clean separation of the AWN-specific definitions
31
+ and these AODV-specific definitions.\<close>
32
+
33
+ definition rreq :: "nat \<times> rreqid \<times> ip \<times> sqn \<times> k \<times> ip \<times> sqn \<times> ip \<Rightarrow> msg"
34
+ where "rreq \<equiv> \<lambda>(hops, rreqid, dip, dsn, dsk, oip, osn, sip).
35
+ Rreq hops rreqid dip dsn dsk oip osn sip"
36
+
37
+ lemma rreq_simp [simp]:
38
+ "rreq(hops, rreqid, dip, dsn, dsk, oip, osn, sip) = Rreq hops rreqid dip dsn dsk oip osn sip"
39
+ unfolding rreq_def by simp
40
+
41
+ definition rrep :: "nat \<times> ip \<times> sqn \<times> ip \<times> ip \<Rightarrow> msg"
42
+ where "rrep \<equiv> \<lambda>(hops, dip, dsn, oip, sip). Rrep hops dip dsn oip sip"
43
+
44
+ lemma rrep_simp [simp]:
45
+ "rrep(hops, dip, dsn, oip, sip) = Rrep hops dip dsn oip sip"
46
+ unfolding rrep_def by simp
47
+
48
+ definition rerr :: "(ip \<rightharpoonup> sqn) \<times> ip \<Rightarrow> msg"
49
+ where "rerr \<equiv> \<lambda>(dests, sip). Rerr dests sip"
50
+
51
+ lemma rerr_simp [simp]:
52
+ "rerr(dests, sip) = Rerr dests sip"
53
+ unfolding rerr_def by simp
54
+
55
+ lemma not_eq_newpkt_rreq [simp]: "\<not>eq_newpkt (Rreq hops rreqid dip dsn dsk oip osn sip)"
56
+ unfolding eq_newpkt_def by simp
57
+
58
+ lemma not_eq_newpkt_rrep [simp]: "\<not>eq_newpkt (Rrep hops dip dsn oip sip)"
59
+ unfolding eq_newpkt_def by simp
60
+
61
+ lemma not_eq_newpkt_rerr [simp]: "\<not>eq_newpkt (Rerr dests sip)"
62
+ unfolding eq_newpkt_def by simp
63
+
64
+ lemma not_eq_newpkt_pkt [simp]: "\<not>eq_newpkt (Pkt d dip sip)"
65
+ unfolding eq_newpkt_def by simp
66
+
67
+ definition pkt :: "data \<times> ip \<times> ip \<Rightarrow> msg"
68
+ where "pkt \<equiv> \<lambda>(d, dip, sip). Pkt d dip sip"
69
+
70
+ lemma pkt_simp [simp]:
71
+ "pkt(d, dip, sip) = Pkt d dip sip"
72
+ unfolding pkt_def by simp
73
+
74
+ end
formal/afp/AODV/variants/b_fwdrreps/B_Aodv_Predicates.thy ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/b_fwdrreps/Aodv_Predicates.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Invariant assumptions and properties"
7
+
8
+ theory B_Aodv_Predicates
9
+ imports B_Aodv
10
+ begin
11
+
12
+ text \<open>Definitions for expression assumptions on incoming messages and properties of
13
+ outgoing messages.\<close>
14
+
15
+ abbreviation not_Pkt :: "msg \<Rightarrow> bool"
16
+ where "not_Pkt m \<equiv> case m of Pkt _ _ _ \<Rightarrow> False | _ \<Rightarrow> True"
17
+
18
+ definition msg_sender :: "msg \<Rightarrow> ip"
19
+ where "msg_sender m \<equiv> case m of Rreq _ _ _ _ _ _ _ ipc \<Rightarrow> ipc
20
+ | Rrep _ _ _ _ ipc \<Rightarrow> ipc
21
+ | Rerr _ ipc \<Rightarrow> ipc
22
+ | Pkt _ _ ipc \<Rightarrow> ipc"
23
+
24
+ lemma msg_sender_simps [simp]:
25
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
26
+ msg_sender (Rreq hops rreqid dip dsn dsk oip osn sip) = sip"
27
+ "\<And>hops dip dsn oip sip. msg_sender (Rrep hops dip dsn oip sip) = sip"
28
+ "\<And>dests sip. msg_sender (Rerr dests sip) = sip"
29
+ "\<And>d dip sip. msg_sender (Pkt d dip sip) = sip"
30
+ unfolding msg_sender_def by simp_all
31
+
32
+ definition msg_zhops :: "msg \<Rightarrow> bool"
33
+ where "msg_zhops m \<equiv> case m of
34
+ Rreq hopsc _ dipc _ _ oipc _ sipc \<Rightarrow> hopsc = 0 \<longrightarrow> oipc = sipc
35
+ | Rrep hopsc dipc _ _ sipc \<Rightarrow> hopsc = 0 \<longrightarrow> dipc = sipc
36
+ | _ \<Rightarrow> True"
37
+
38
+ lemma msg_zhops_simps [simp]:
39
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
40
+ msg_zhops (Rreq hops rreqid dip dsn dsk oip osn sip) = (hops = 0 \<longrightarrow> oip = sip)"
41
+ "\<And>hops dip dsn oip sip. msg_zhops (Rrep hops dip dsn oip sip) = (hops = 0 \<longrightarrow> dip = sip)"
42
+ "\<And>dests sip. msg_zhops (Rerr dests sip) = True"
43
+ "\<And>d dip. msg_zhops (Newpkt d dip) = True"
44
+ "\<And>d dip sip. msg_zhops (Pkt d dip sip) = True"
45
+ unfolding msg_zhops_def by simp_all
46
+
47
+ definition rreq_rrep_sn :: "msg \<Rightarrow> bool"
48
+ where "rreq_rrep_sn m \<equiv> case m of Rreq _ _ _ _ _ _ osnc _ \<Rightarrow> osnc \<ge> 1
49
+ | Rrep _ _ dsnc _ _ \<Rightarrow> dsnc \<ge> 1
50
+ | _ \<Rightarrow> True"
51
+
52
+ lemma rreq_rrep_sn_simps [simp]:
53
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
54
+ rreq_rrep_sn (Rreq hops rreqid dip dsn dsk oip osn sip) = (osn \<ge> 1)"
55
+ "\<And>hops dip dsn oip sip. rreq_rrep_sn (Rrep hops dip dsn oip sip) = (dsn \<ge> 1)"
56
+ "\<And>dests sip. rreq_rrep_sn (Rerr dests sip) = True"
57
+ "\<And>d dip. rreq_rrep_sn (Newpkt d dip) = True"
58
+ "\<And>d dip sip. rreq_rrep_sn (Pkt d dip sip) = True"
59
+ unfolding rreq_rrep_sn_def by simp_all
60
+
61
+ definition rreq_rrep_fresh :: "rt \<Rightarrow> msg \<Rightarrow> bool"
62
+ where "rreq_rrep_fresh crt m \<equiv> case m of Rreq hopsc _ _ _ _ oipc osnc ipcc \<Rightarrow> (ipcc \<noteq> oipc \<longrightarrow>
63
+ oipc\<in>kD(crt) \<and> (sqn crt oipc > osnc
64
+ \<or> (sqn crt oipc = osnc
65
+ \<and> the (dhops crt oipc) \<le> hopsc
66
+ \<and> the (flag crt oipc) = val)))
67
+ | Rrep hopsc dipc dsnc _ ipcc \<Rightarrow> (ipcc \<noteq> dipc \<longrightarrow>
68
+ dipc\<in>kD(crt)
69
+ \<and> sqn crt dipc = dsnc
70
+ \<and> the (dhops crt dipc) = hopsc
71
+ \<and> the (flag crt dipc) = val)
72
+ | _ \<Rightarrow> True"
73
+
74
+ lemma rreq_rrep_fresh [simp]:
75
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
76
+ rreq_rrep_fresh crt (Rreq hops rreqid dip dsn dsk oip osn sip) =
77
+ (sip \<noteq> oip \<longrightarrow> oip\<in>kD(crt)
78
+ \<and> (sqn crt oip > osn
79
+ \<or> (sqn crt oip = osn
80
+ \<and> the (dhops crt oip) \<le> hops
81
+ \<and> the (flag crt oip) = val)))"
82
+ "\<And>hops dip dsn oip sip. rreq_rrep_fresh crt (Rrep hops dip dsn oip sip) =
83
+ (sip \<noteq> dip \<longrightarrow> dip\<in>kD(crt)
84
+ \<and> sqn crt dip = dsn
85
+ \<and> the (dhops crt dip) = hops
86
+ \<and> the (flag crt dip) = val)"
87
+ "\<And>dests sip. rreq_rrep_fresh crt (Rerr dests sip) = True"
88
+ "\<And>d dip. rreq_rrep_fresh crt (Newpkt d dip) = True"
89
+ "\<And>d dip sip. rreq_rrep_fresh crt (Pkt d dip sip) = True"
90
+ unfolding rreq_rrep_fresh_def by simp_all
91
+
92
+ definition rerr_invalid :: "rt \<Rightarrow> msg \<Rightarrow> bool"
93
+ where "rerr_invalid crt m \<equiv> case m of Rerr destsc _ \<Rightarrow> (\<forall>ripc\<in>dom(destsc).
94
+ (ripc\<in>iD(crt) \<and> the (destsc ripc) = sqn crt ripc))
95
+ | _ \<Rightarrow> True"
96
+
97
+ lemma rerr_invalid [simp]:
98
+ "\<And>hops rreqid dip dsn dsk oip osn sip.
99
+ rerr_invalid crt (Rreq hops rreqid dip dsn dsk oip osn sip) = True"
100
+ "\<And>hops dip dsn oip sip. rerr_invalid crt (Rrep hops dip dsn oip sip) = True"
101
+ "\<And>dests sip. rerr_invalid crt (Rerr dests sip) = (\<forall>rip\<in>dom(dests).
102
+ rip\<in>iD(crt) \<and> the (dests rip) = sqn crt rip)"
103
+ "\<And>d dip. rerr_invalid crt (Newpkt d dip) = True"
104
+ "\<And>d dip sip. rerr_invalid crt (Pkt d dip sip) = True"
105
+ unfolding rerr_invalid_def by simp_all
106
+
107
+ definition
108
+ initmissing :: "(nat \<Rightarrow> state option) \<times> 'a \<Rightarrow> (nat \<Rightarrow> state) \<times> 'a"
109
+ where
110
+ "initmissing \<sigma> = (\<lambda>i. case (fst \<sigma>) i of None \<Rightarrow> aodv_init i | Some s \<Rightarrow> s, snd \<sigma>)"
111
+
112
+ lemma not_in_net_ips_fst_init_missing [simp]:
113
+ assumes "i \<notin> net_ips \<sigma>"
114
+ shows "fst (initmissing (netgmap fst \<sigma>)) i = aodv_init i"
115
+ using assms unfolding initmissing_def by simp
116
+
117
+ lemma fst_initmissing_netgmap_pair_fst [simp]:
118
+ "fst (initmissing (netgmap (\<lambda>(p, q). (fst (id p), snd (id p), q)) s))
119
+ = fst (initmissing (netgmap fst s))"
120
+ unfolding initmissing_def by auto
121
+
122
+ text \<open>We introduce a streamlined alternative to @{term initmissing} with @{term netgmap}
123
+ to simplify invariant statements and thus facilitate their comprehension and
124
+ presentation.\<close>
125
+
126
+ lemma fst_initmissing_netgmap_default_aodv_init_netlift:
127
+ "fst (initmissing (netgmap fst s)) = default aodv_init (netlift fst s)"
128
+ unfolding initmissing_def default_def
129
+ by (simp add: fst_netgmap_netlift del: One_nat_def)
130
+
131
+ definition
132
+ netglobal :: "((nat \<Rightarrow> state) \<Rightarrow> bool) \<Rightarrow> ((state \<times> 'b) \<times> 'c) net_state \<Rightarrow> bool"
133
+ where
134
+ "netglobal P \<equiv> (\<lambda>s. P (default aodv_init (netlift fst s)))"
135
+
136
+ end
formal/afp/AODV/variants/b_fwdrreps/B_Fresher.thy ADDED
@@ -0,0 +1,799 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/b_fwdrreps/Fresher.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ *)
5
+
6
+ section "Quality relations between routes"
7
+
8
+ theory B_Fresher
9
+ imports B_Aodv_Data
10
+ begin
11
+
12
+ subsection "Net sequence numbers"
13
+
14
+ subsubsection "On individual routes"
15
+
16
+ definition
17
+ nsqn\<^sub>r :: "r \<Rightarrow> sqn"
18
+ where
19
+ "nsqn\<^sub>r r \<equiv> if \<pi>\<^sub>4(r) = val \<or> \<pi>\<^sub>2(r) = 0 then \<pi>\<^sub>2(r) else (\<pi>\<^sub>2(r) - 1)"
20
+
21
+ lemma nsqnr_def':
22
+ "nsqn\<^sub>r r = (if \<pi>\<^sub>4(r) = inv then \<pi>\<^sub>2(r) - 1 else \<pi>\<^sub>2(r))"
23
+ unfolding nsqn\<^sub>r_def by simp
24
+
25
+ lemma nsqn\<^sub>r_zero [simp]:
26
+ "\<And>dsn dsk flag hops nhip pre. nsqn\<^sub>r (0, dsk, flag, hops, nhip, pre) = 0"
27
+ unfolding nsqn\<^sub>r_def by clarsimp
28
+
29
+ lemma nsqn\<^sub>r_val [simp]:
30
+ "\<And>dsn dsk hops nhip pre. nsqn\<^sub>r (dsn, dsk, val, hops, nhip, pre) = dsn"
31
+ unfolding nsqn\<^sub>r_def by clarsimp
32
+
33
+ lemma nsqn\<^sub>r_inv [simp]:
34
+ "\<And>dsn dsk hops nhip pre. nsqn\<^sub>r (dsn, dsk, inv, hops, nhip, pre) = dsn - 1"
35
+ unfolding nsqn\<^sub>r_def by clarsimp
36
+
37
+ lemma nsqn\<^sub>r_lte_dsn [simp]:
38
+ "\<And>dsn dsk flag hops nhip pre. nsqn\<^sub>r (dsn, dsk, flag, hops, nhip, pre) \<le> dsn"
39
+ unfolding nsqn\<^sub>r_def by clarsimp
40
+
41
+ subsubsection "On routes in routing tables"
42
+
43
+ definition
44
+ nsqn :: "rt \<Rightarrow> ip \<Rightarrow> sqn"
45
+ where
46
+ "nsqn \<equiv> \<lambda>rt dip. case \<sigma>\<^bsub>route\<^esub>(rt, dip) of None \<Rightarrow> 0 | Some r \<Rightarrow> nsqn\<^sub>r(r)"
47
+
48
+ lemma nsqn_sqn_def:
49
+ "\<And>rt dip. nsqn rt dip = (if flag rt dip = Some val \<or> sqn rt dip = 0
50
+ then sqn rt dip else sqn rt dip - 1)"
51
+ unfolding nsqn_def sqn_def by (clarsimp split: option.split)
52
+
53
+ lemma not_in_kD_nsqn [simp]:
54
+ assumes "dip \<notin> kD(rt)"
55
+ shows "nsqn rt dip = 0"
56
+ using assms unfolding nsqn_def by simp
57
+
58
+ lemma kD_nsqn:
59
+ assumes "dip \<in> kD(rt)"
60
+ shows "nsqn rt dip = nsqn\<^sub>r(the (\<sigma>\<^bsub>route\<^esub>(rt, dip)))"
61
+ using assms [THEN kD_Some] unfolding nsqn_def by clarsimp
62
+
63
+ lemma nsqnr_r_flag_pred [simp, intro]:
64
+ fixes dsn dsk flag hops nhip pre
65
+ assumes "P (nsqn\<^sub>r (dsn, dsk, val, hops, nhip, pre))"
66
+ and "P (nsqn\<^sub>r (dsn, dsk, inv, hops, nhip, pre))"
67
+ shows "P (nsqn\<^sub>r (dsn, dsk, flag, hops, nhip, pre))"
68
+ using assms by (cases flag) auto
69
+
70
+ lemma nsqn\<^sub>r_addpreRT_inv [simp]:
71
+ "\<And>rt dip npre dip'. dip \<in> kD(rt) \<Longrightarrow>
72
+ nsqn\<^sub>r (the (the (addpreRT rt dip npre) dip')) = nsqn\<^sub>r (the (rt dip'))"
73
+ unfolding addpreRT_def nsqn\<^sub>r_def
74
+ by (frule kD_Some) (clarsimp split: option.split)
75
+
76
+ lemma sqn_nsqn:
77
+ "\<And>rt dip. sqn rt dip - 1 \<le> nsqn rt dip"
78
+ unfolding sqn_def nsqn_def by (clarsimp split: option.split)
79
+
80
+ lemma nsqn_sqn: "nsqn rt dip \<le> sqn rt dip"
81
+ unfolding sqn_def nsqn_def by (cases "rt dip") auto
82
+
83
+ lemma val_nsqn_sqn [elim, simp]:
84
+ assumes "ip\<in>kD(rt)"
85
+ and "the (flag rt ip) = val"
86
+ shows "nsqn rt ip = sqn rt ip"
87
+ using assms unfolding nsqn_sqn_def by auto
88
+
89
+ lemma vD_nsqn_sqn [elim, simp]:
90
+ assumes "ip\<in>vD(rt)"
91
+ shows "nsqn rt ip = sqn rt ip"
92
+ proof -
93
+ from \<open>ip\<in>vD(rt)\<close> have "ip\<in>kD(rt)"
94
+ and "the (flag rt ip) = val" by auto
95
+ thus ?thesis ..
96
+ qed
97
+
98
+ lemma inv_nsqn_sqn [elim, simp]:
99
+ assumes "ip\<in>kD(rt)"
100
+ and "the (flag rt ip) = inv"
101
+ shows "nsqn rt ip = sqn rt ip - 1"
102
+ using assms unfolding nsqn_sqn_def by auto
103
+
104
+ lemma iD_nsqn_sqn [elim, simp]:
105
+ assumes "ip\<in>iD(rt)"
106
+ shows "nsqn rt ip = sqn rt ip - 1"
107
+ proof -
108
+ from \<open>ip\<in>iD(rt)\<close> have "ip\<in>kD(rt)"
109
+ and "the (flag rt ip) = inv" by auto
110
+ thus ?thesis ..
111
+ qed
112
+
113
+ lemma nsqn_update_changed_kno_val [simp]: "\<And>rt ip dsn dsk hops nhip.
114
+ rt \<noteq> update rt ip (dsn, kno, val, hops, nhip, {})
115
+ \<Longrightarrow> nsqn (update rt ip (dsn, kno, val, hops, nhip, {})) ip = dsn"
116
+ unfolding nsqn\<^sub>r_def update_def
117
+ by (clarsimp simp: kD_nsqn split: option.split_asm option.split if_split_asm)
118
+ (metis fun_upd_triv)
119
+
120
+ lemma nsqn_addpreRT_inv [simp]:
121
+ "\<And>rt dip npre dip'. dip \<in> kD(rt) \<Longrightarrow>
122
+ nsqn (the (addpreRT rt dip npre)) dip' = nsqn rt dip'"
123
+ unfolding addpreRT_def nsqn_def nsqn\<^sub>r_def
124
+ by (frule kD_Some) (clarsimp split: option.split)
125
+
126
+ lemma nsqn_update_other [simp]:
127
+ fixes dsn dsk flag hops dip nhip pre rt ip
128
+ assumes "dip \<noteq> ip"
129
+ shows "nsqn (update rt ip (dsn, dsk, flag, hops, nhip, pre)) dip = nsqn rt dip"
130
+ using assms unfolding nsqn_def
131
+ by (clarsimp split: option.split)
132
+
133
+ lemma nsqn_invalidate_eq:
134
+ assumes "dip \<in> kD(rt)"
135
+ and "dests dip = Some rsn"
136
+ shows "nsqn (invalidate rt dests) dip = rsn - 1"
137
+ using assms
138
+ proof -
139
+ from assms obtain dsk hops nhip pre
140
+ where "invalidate rt dests dip = Some (rsn, dsk, inv, hops, nhip, pre)"
141
+ unfolding invalidate_def
142
+ by auto
143
+ moreover from \<open>dip \<in> kD(rt)\<close> have "dip \<in> kD(invalidate rt dests)" by simp
144
+ ultimately show ?thesis
145
+ using \<open>dests dip = Some rsn\<close> by simp
146
+ qed
147
+
148
+ lemma nsqn_invalidate_other [simp]:
149
+ assumes "dip\<in>kD(rt)"
150
+ and "dip\<notin>dom dests"
151
+ shows "nsqn (invalidate rt dests) dip = nsqn rt dip"
152
+ using assms by (clarsimp simp add: kD_nsqn)
153
+
154
+ subsection "Comparing routes "
155
+
156
+ definition
157
+ fresher :: "r \<Rightarrow> r \<Rightarrow> bool" ("(_/ \<sqsubseteq> _)" [51, 51] 50)
158
+ where
159
+ "fresher r r' \<equiv> ((nsqn\<^sub>r r < nsqn\<^sub>r r') \<or> (nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r')))"
160
+
161
+ lemma fresherI1 [intro]:
162
+ assumes "nsqn\<^sub>r r < nsqn\<^sub>r r'"
163
+ shows "r \<sqsubseteq> r'"
164
+ unfolding fresher_def using assms by simp
165
+
166
+ lemma fresherI2 [intro]:
167
+ assumes "nsqn\<^sub>r r = nsqn\<^sub>r r'"
168
+ and "\<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r')"
169
+ shows "r \<sqsubseteq> r'"
170
+ unfolding fresher_def using assms by simp
171
+
172
+ lemma fresherI [intro]:
173
+ assumes "(nsqn\<^sub>r r < nsqn\<^sub>r r') \<or> (nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r'))"
174
+ shows "r \<sqsubseteq> r'"
175
+ unfolding fresher_def using assms .
176
+
177
+ lemma fresherE [elim]:
178
+ assumes "r \<sqsubseteq> r'"
179
+ and "nsqn\<^sub>r r < nsqn\<^sub>r r' \<Longrightarrow> P r r'"
180
+ and "nsqn\<^sub>r r = nsqn\<^sub>r r' \<and> \<pi>\<^sub>5(r) \<ge> \<pi>\<^sub>5(r') \<Longrightarrow> P r r'"
181
+ shows "P r r'"
182
+ using assms unfolding fresher_def by auto
183
+
184
+ lemma fresher_refl [simp]: "r \<sqsubseteq> r"
185
+ unfolding fresher_def by simp
186
+
187
+ lemma fresher_trans [elim, trans]:
188
+ "\<lbrakk> x \<sqsubseteq> y; y \<sqsubseteq> z \<rbrakk> \<Longrightarrow> x \<sqsubseteq> z"
189
+ unfolding fresher_def by auto
190
+
191
+ lemma not_fresher_trans [elim, trans]:
192
+ "\<lbrakk> \<not>(x \<sqsubseteq> y); \<not>(z \<sqsubseteq> x) \<rbrakk> \<Longrightarrow> \<not>(z \<sqsubseteq> y)"
193
+ unfolding fresher_def by auto
194
+
195
+ lemma fresher_dsn_flag_hops_const [simp]:
196
+ fixes dsn dsk dsk' flag hops nhip nhip' pre pre'
197
+ shows "(dsn, dsk, flag, hops, nhip, pre) \<sqsubseteq> (dsn, dsk', flag, hops, nhip', pre')"
198
+ unfolding fresher_def by (cases flag) simp_all
199
+
200
+ lemma addpre_fresher [simp]: "\<And>r npre. r \<sqsubseteq> (addpre r npre)"
201
+ by clarsimp
202
+
203
+ subsection "Comparing routing tables "
204
+
205
+ definition
206
+ rt_fresher :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
207
+ where
208
+ "rt_fresher \<equiv> \<lambda>dip rt rt'. (the (\<sigma>\<^bsub>route\<^esub>(rt, dip))) \<sqsubseteq> (the (\<sigma>\<^bsub>route\<^esub>(rt', dip)))"
209
+
210
+ abbreviation
211
+ rt_fresher_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<sqsubseteq>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
212
+ where
213
+ "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2 \<equiv> rt_fresher i rt1 rt2"
214
+
215
+ lemma rt_fresher_def':
216
+ "(rt\<^sub>1 \<sqsubseteq>\<^bsub>i\<^esub> rt\<^sub>2) = (nsqn\<^sub>r (the (rt\<^sub>1 i)) < nsqn\<^sub>r (the (rt\<^sub>2 i)) \<or>
217
+ nsqn\<^sub>r (the (rt\<^sub>1 i)) = nsqn\<^sub>r (the (rt\<^sub>2 i)) \<and> \<pi>\<^sub>5 (the (rt\<^sub>2 i)) \<le> \<pi>\<^sub>5 (the (rt\<^sub>1 i)))"
218
+ unfolding rt_fresher_def fresher_def by (rule refl)
219
+
220
+ lemma single_rt_fresher [intro]:
221
+ assumes "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
222
+ shows "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
223
+ using assms unfolding rt_fresher_def .
224
+
225
+ lemma rt_fresher_single [intro]:
226
+ assumes "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
227
+ shows "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
228
+ using assms unfolding rt_fresher_def .
229
+
230
+ lemma rt_fresher_def2:
231
+ assumes "dip \<in> kD(rt1)"
232
+ and "dip \<in> kD(rt2)"
233
+ shows "(rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) = (nsqn rt1 dip < nsqn rt2 dip
234
+ \<or> (nsqn rt1 dip = nsqn rt2 dip
235
+ \<and> the (dhops rt1 dip) \<ge> the (dhops rt2 dip)))"
236
+ using assms unfolding rt_fresher_def fresher_def by (simp add: kD_nsqn proj5_eq_dhops)
237
+
238
+ lemma rt_fresherI1 [intro]:
239
+ assumes "dip \<in> kD(rt1)"
240
+ and "dip \<in> kD(rt2)"
241
+ and "nsqn rt1 dip < nsqn rt2 dip"
242
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
243
+ unfolding rt_fresher_def2 [OF assms(1-2)] using assms(3) by simp
244
+
245
+ lemma rt_fresherI2 [intro]:
246
+ assumes "dip \<in> kD(rt1)"
247
+ and "dip \<in> kD(rt2)"
248
+ and "nsqn rt1 dip = nsqn rt2 dip"
249
+ and "the (dhops rt1 dip) \<ge> the (dhops rt2 dip)"
250
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
251
+ unfolding rt_fresher_def2 [OF assms(1-2)] using assms(3-4) by simp
252
+
253
+ lemma rt_fresherE [elim]:
254
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
255
+ and "dip \<in> kD(rt1)"
256
+ and "dip \<in> kD(rt2)"
257
+ and "\<lbrakk> nsqn rt1 dip < nsqn rt2 dip \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
258
+ and "\<lbrakk> nsqn rt1 dip = nsqn rt2 dip;
259
+ the (dhops rt1 dip) \<ge> the (dhops rt2 dip) \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
260
+ shows "P rt1 rt2 dip"
261
+ using assms(1) unfolding rt_fresher_def2 [OF assms(2-3)]
262
+ using assms(4-5) by auto
263
+
264
+ lemma rt_fresher_refl [simp]: "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt"
265
+ unfolding rt_fresher_def by simp
266
+
267
+ lemma rt_fresher_trans [elim, trans]:
268
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
269
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
270
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
271
+ using assms unfolding rt_fresher_def by auto
272
+
273
+ lemma rt_fresher_if_Some [intro!]:
274
+ assumes "the (rt dip) \<sqsubseteq> r"
275
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> (\<lambda>ip. if ip = dip then Some r else rt ip)"
276
+ using assms unfolding rt_fresher_def by simp
277
+
278
+ definition rt_fresh_as :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
279
+ where
280
+ "rt_fresh_as \<equiv> \<lambda>dip rt1 rt2. (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) \<and> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
281
+
282
+ abbreviation
283
+ rt_fresh_as_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<approx>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
284
+ where
285
+ "rt1 \<approx>\<^bsub>i\<^esub> rt2 \<equiv> rt_fresh_as i rt1 rt2"
286
+
287
+ lemma rt_fresh_as_refl [simp]: "\<And>rt dip. rt \<approx>\<^bsub>dip\<^esub> rt"
288
+ unfolding rt_fresh_as_def by simp
289
+
290
+ lemma rt_fresh_as_trans [simp, intro, trans]:
291
+ "\<And>rt1 rt2 rt3 dip. \<lbrakk> rt1 \<approx>\<^bsub>dip\<^esub> rt2; rt2 \<approx>\<^bsub>dip\<^esub> rt3 \<rbrakk> \<Longrightarrow> rt1 \<approx>\<^bsub>dip\<^esub> rt3"
292
+ unfolding rt_fresh_as_def rt_fresher_def
293
+ by (metis (mono_tags) fresher_trans)
294
+
295
+ lemma rt_fresh_asI [intro!]:
296
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
297
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
298
+ shows "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
299
+ using assms unfolding rt_fresh_as_def by simp
300
+
301
+ lemma rt_fresh_as_fresherI [intro]:
302
+ assumes "dip\<in>kD(rt1)"
303
+ and "dip\<in>kD(rt2)"
304
+ and "the (rt1 dip) \<sqsubseteq> the (rt2 dip)"
305
+ and "the (rt2 dip) \<sqsubseteq> the (rt1 dip)"
306
+ shows "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
307
+ using assms unfolding rt_fresh_as_def
308
+ by (clarsimp dest!: single_rt_fresher)
309
+
310
+ lemma nsqn_rt_fresh_asI:
311
+ assumes "dip \<in> kD(rt)"
312
+ and "dip \<in> kD(rt')"
313
+ and "nsqn rt dip = nsqn rt' dip"
314
+ and "\<pi>\<^sub>5(the (rt dip)) = \<pi>\<^sub>5(the (rt' dip))"
315
+ shows "rt \<approx>\<^bsub>dip\<^esub> rt'"
316
+ proof
317
+ from assms(1-2,4) have dhops': "the (dhops rt' dip) \<le> the (dhops rt dip)"
318
+ by (simp add: proj5_eq_dhops)
319
+ with assms(1-3) show "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt'"
320
+ by (rule rt_fresherI2)
321
+ next
322
+ from assms(1-2,4) have dhops: "the (dhops rt dip) \<le> the (dhops rt' dip)"
323
+ by (simp add: proj5_eq_dhops)
324
+ with assms(2,1) assms(3) [symmetric] show "rt' \<sqsubseteq>\<^bsub>dip\<^esub> rt"
325
+ by (rule rt_fresherI2)
326
+ qed
327
+
328
+ lemma rt_fresh_asE [elim]:
329
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
330
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2; rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1 \<rbrakk> \<Longrightarrow> P rt1 rt2 dip"
331
+ shows "P rt1 rt2 dip"
332
+ using assms unfolding rt_fresh_as_def by simp
333
+
334
+ lemma rt_fresh_asD1 [dest]:
335
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
336
+ shows "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
337
+ using assms unfolding rt_fresh_as_def by simp
338
+
339
+ lemma rt_fresh_asD2 [dest]:
340
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
341
+ shows "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
342
+ using assms unfolding rt_fresh_as_def by simp
343
+
344
+ lemma rt_fresh_as_sym:
345
+ assumes "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
346
+ shows "rt2 \<approx>\<^bsub>dip\<^esub> rt1"
347
+ using assms unfolding rt_fresh_as_def by simp
348
+
349
+ lemma not_rt_fresh_asI1 [intro]:
350
+ assumes "\<not> (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)"
351
+ shows "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
352
+ proof
353
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
354
+ hence "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" ..
355
+ with \<open>\<not> (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)\<close> show False ..
356
+ qed
357
+
358
+ lemma not_rt_fresh_asI2 [intro]:
359
+ assumes "\<not> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
360
+ shows "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
361
+ proof
362
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
363
+ hence "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
364
+ with \<open>\<not> (rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)\<close> show False ..
365
+ qed
366
+
367
+ lemma not_single_rt_fresher [elim]:
368
+ assumes "\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))"
369
+ shows "\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)"
370
+ proof
371
+ assume "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
372
+ hence "the (rt1 ip) \<sqsubseteq> the (rt2 ip)" ..
373
+ with \<open>\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))\<close> show False ..
374
+ qed
375
+
376
+ lemmas not_single_rt_fresh_asI1 [intro] = not_rt_fresh_asI1 [OF not_single_rt_fresher]
377
+ lemmas not_single_rt_fresh_asI2 [intro] = not_rt_fresh_asI2 [OF not_single_rt_fresher]
378
+
379
+ lemma not_rt_fresher_single [elim]:
380
+ assumes "\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)"
381
+ shows "\<not>(the (rt1 ip) \<sqsubseteq> the (rt2 ip))"
382
+ proof
383
+ assume "the (rt1 ip) \<sqsubseteq> the (rt2 ip)"
384
+ hence "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2" ..
385
+ with \<open>\<not>(rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2)\<close> show False ..
386
+ qed
387
+
388
+ lemma rt_fresh_as_nsqnr:
389
+ assumes "dip \<in> kD(rt1)"
390
+ and "dip \<in> kD(rt2)"
391
+ and "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
392
+ shows "nsqn\<^sub>r (the (rt2 dip)) = nsqn\<^sub>r (the (rt1 dip))"
393
+ using assms(3) unfolding rt_fresh_as_def
394
+ by (auto simp: rt_fresher_def2 [OF \<open>dip \<in> kD(rt1)\<close> \<open>dip \<in> kD(rt2)\<close>]
395
+ rt_fresher_def2 [OF \<open>dip \<in> kD(rt2)\<close> \<open>dip \<in> kD(rt1)\<close>]
396
+ kD_nsqn [OF \<open>dip \<in> kD(rt1)\<close>]
397
+ kD_nsqn [OF \<open>dip \<in> kD(rt2)\<close>])
398
+
399
+ lemma rt_fresher_mapupd [intro!]:
400
+ assumes "dip\<in>kD(rt)"
401
+ and "the (rt dip) \<sqsubseteq> r"
402
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt(dip \<mapsto> r)"
403
+ using assms unfolding rt_fresher_def by simp
404
+
405
+ lemma rt_fresher_map_update_other [intro!]:
406
+ assumes "dip\<in>kD(rt)"
407
+ and "dip \<noteq> ip"
408
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> rt(ip \<mapsto> r)"
409
+ using assms unfolding rt_fresher_def by simp
410
+
411
+ lemma rt_fresher_update_other [simp]:
412
+ assumes inkD: "dip\<in>kD(rt)"
413
+ and "dip \<noteq> ip"
414
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt ip r"
415
+ using assms unfolding update_def
416
+ by (clarsimp split: option.split) (fastforce)
417
+
418
+ theorem rt_fresher_update [simp]:
419
+ assumes "dip\<in>kD(rt)"
420
+ and "the (dhops rt dip) \<ge> 1"
421
+ and "update_arg_wf r"
422
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt ip r"
423
+ proof (cases "dip = ip")
424
+ assume "dip \<noteq> ip" with \<open>dip\<in>kD(rt)\<close> show ?thesis
425
+ by (rule rt_fresher_update_other)
426
+ next
427
+ assume "dip = ip"
428
+
429
+ from \<open>dip\<in>kD(rt)\<close> obtain dsn\<^sub>n dsk\<^sub>n f\<^sub>n hops\<^sub>n nhip\<^sub>n pre\<^sub>n
430
+ where rtn [simp]: "the (rt dip) = (dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)"
431
+ by (metis prod_cases6)
432
+ with \<open>the (dhops rt dip) \<ge> 1\<close> and \<open>dip\<in>kD(rt)\<close> have "hops\<^sub>n \<ge> 1"
433
+ by (metis proj5_eq_dhops projs(4))
434
+ from \<open>dip\<in>kD(rt)\<close> rtn have [simp]: "sqn rt dip = dsn\<^sub>n"
435
+ and [simp]: "the (dhops rt dip) = hops\<^sub>n"
436
+ and [simp]: "the (flag rt dip) = f\<^sub>n"
437
+ by (simp add: sqn_def proj5_eq_dhops [symmetric]
438
+ proj4_eq_flag [symmetric])+
439
+
440
+ from \<open>update_arg_wf r\<close> have "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
441
+ \<sqsubseteq> the ((update rt dip r) dip)"
442
+ proof (rule wf_r_cases)
443
+ fix nhip pre
444
+ from \<open>hops\<^sub>n \<ge> 1\<close> have "\<And>pre'. (dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
445
+ \<sqsubseteq> (dsn\<^sub>n, unk, val, Suc 0, nhip, pre')"
446
+ unfolding fresher_def sqn_def by (cases f\<^sub>n) auto
447
+ thus "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
448
+ \<sqsubseteq> the (update rt dip (0, unk, val, Suc 0, nhip, pre) dip)"
449
+ using \<open>dip\<in>kD(rt)\<close> by - (rule update_cases_kD, simp_all)
450
+ next
451
+ fix dsn :: sqn and hops nhip pre
452
+ assume "0 < dsn"
453
+ show "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
454
+ \<sqsubseteq> the (update rt dip (dsn, kno, val, hops, nhip, pre) dip)"
455
+ proof (rule update_cases_kD [OF _ \<open>dip\<in>kD(rt)\<close>], simp_all add: \<open>0 < dsn\<close>)
456
+ assume "dsn\<^sub>n < dsn"
457
+ thus "(dsn\<^sub>n, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
458
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
459
+ unfolding fresher_def by auto
460
+ next
461
+ assume "dsn\<^sub>n = dsn"
462
+ and "hops < hops\<^sub>n"
463
+ thus "(dsn, dsk\<^sub>n, f\<^sub>n, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
464
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
465
+ unfolding fresher_def nsqn\<^sub>r_def by simp
466
+ next
467
+ assume "dsn\<^sub>n = dsn"
468
+ with \<open>0 < dsn\<close>
469
+ show "(dsn, dsk\<^sub>n, inv, hops\<^sub>n, nhip\<^sub>n, pre\<^sub>n)
470
+ \<sqsubseteq> (dsn, kno, val, hops, nhip, pre \<union> pre\<^sub>n)"
471
+ unfolding fresher_def by simp
472
+ qed
473
+ qed
474
+ hence "rt \<sqsubseteq>\<^bsub>dip\<^esub> update rt dip r"
475
+ by - (rule single_rt_fresher, simp)
476
+ with \<open>dip = ip\<close> show ?thesis by simp
477
+ qed
478
+
479
+ theorem rt_fresher_invalidate [simp]:
480
+ assumes "dip\<in>kD(rt)"
481
+ and indests: "\<forall>rip\<in>dom(dests). rip\<in>vD(rt) \<and> sqn rt rip < the (dests rip)"
482
+ shows "rt \<sqsubseteq>\<^bsub>dip\<^esub> invalidate rt dests"
483
+ proof (cases "dip\<in>dom(dests)")
484
+ assume "dip\<notin>dom(dests)"
485
+ thus ?thesis using \<open>dip\<in>kD(rt)\<close>
486
+ by - (rule single_rt_fresher, simp)
487
+ next
488
+ assume "dip\<in>dom(dests)"
489
+ moreover with indests have "dip\<in>vD(rt)"
490
+ and "sqn rt dip < the (dests dip)"
491
+ by auto
492
+ ultimately show ?thesis
493
+ unfolding invalidate_def sqn_def
494
+ by - (rule single_rt_fresher, auto simp: fresher_def)
495
+ qed
496
+
497
+ lemma nsqn\<^sub>r_invalidate [simp]:
498
+ assumes "dip\<in>kD(rt)"
499
+ and "dip\<in>dom(dests)"
500
+ shows "nsqn\<^sub>r (the (invalidate rt dests dip)) = the (dests dip) - 1"
501
+ using assms unfolding invalidate_def by auto
502
+
503
+ lemma rt_fresh_as_inc_invalidate [simp]:
504
+ assumes "dip\<in>kD(rt)"
505
+ and "\<forall>rip\<in>dom(dests). rip\<in>vD(rt) \<and> the (dests rip) = inc (sqn rt rip)"
506
+ shows "rt \<approx>\<^bsub>dip\<^esub> invalidate rt dests"
507
+ proof (cases "dip\<in>dom(dests)")
508
+ assume "dip\<notin>dom(dests)"
509
+ with \<open>dip\<in>kD(rt)\<close> have "dip\<in>kD(invalidate rt dests)"
510
+ by simp
511
+ with \<open>dip\<in>kD(rt)\<close> show ?thesis
512
+ by rule (simp_all add: \<open>dip\<notin>dom(dests)\<close>)
513
+ next
514
+ assume "dip\<in>dom(dests)"
515
+ with assms(2) have "dip\<in>vD(rt)"
516
+ and "the (dests dip) = inc (sqn rt dip)" by auto
517
+ from \<open>dip\<in>vD(rt)\<close> have "dip\<in>kD(rt)" by simp
518
+ moreover then have "dip\<in>kD(invalidate rt dests)" by simp
519
+ ultimately show ?thesis
520
+ proof (rule nsqn_rt_fresh_asI)
521
+ from \<open>dip\<in>vD(rt)\<close> have "nsqn rt dip = sqn rt dip" by simp
522
+ also have "sqn rt dip = nsqn\<^sub>r (the (invalidate rt dests dip))"
523
+ proof -
524
+ from \<open>dip\<in>kD(rt)\<close> have "nsqn\<^sub>r (the (invalidate rt dests dip)) = the (dests dip) - 1"
525
+ using \<open>dip\<in>dom(dests)\<close> by (rule nsqn\<^sub>r_invalidate)
526
+ with \<open>the (dests dip) = inc (sqn rt dip)\<close>
527
+ show "sqn rt dip = nsqn\<^sub>r (the (invalidate rt dests dip))" by simp
528
+ qed
529
+ also from \<open>dip\<in>kD(invalidate rt dests)\<close>
530
+ have "nsqn\<^sub>r (the (invalidate rt dests dip)) = nsqn (invalidate rt dests) dip"
531
+ by (simp add: kD_nsqn)
532
+ finally show "nsqn rt dip = nsqn (invalidate rt dests) dip" .
533
+ qed simp
534
+ qed
535
+
536
+ lemmas rt_fresher_inc_invalidate [simp] = rt_fresh_as_inc_invalidate [THEN rt_fresh_asD1]
537
+
538
+ lemma rt_fresh_as_addpreRT [simp]:
539
+ assumes "ip\<in>kD(rt)"
540
+ shows "rt \<approx>\<^bsub>dip\<^esub> the (addpreRT rt ip npre)"
541
+ using assms [THEN kD_Some] by (auto simp: addpreRT_def)
542
+
543
+ lemmas rt_fresher_addpreRT [simp] = rt_fresh_as_addpreRT [THEN rt_fresh_asD1]
544
+
545
+ subsection "Strictly comparing routing tables "
546
+
547
+ definition rt_strictly_fresher :: "ip \<Rightarrow> rt \<Rightarrow> rt \<Rightarrow> bool"
548
+ where
549
+ "rt_strictly_fresher \<equiv> \<lambda>dip rt1 rt2. (rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2) \<and> \<not>(rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
550
+
551
+ abbreviation
552
+ rt_strictly_fresher_syn :: "rt \<Rightarrow> ip \<Rightarrow> rt \<Rightarrow> bool" ("(_/ \<sqsubset>\<^bsub>_\<^esub> _)" [51, 999, 51] 50)
553
+ where
554
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 \<equiv> rt_strictly_fresher i rt1 rt2"
555
+
556
+ lemma rt_strictly_fresher_def'':
557
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 = ((rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2) \<and> \<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1))"
558
+ unfolding rt_strictly_fresher_def rt_fresh_as_def by auto
559
+
560
+ lemma rt_strictly_fresherI' [intro]:
561
+ assumes "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2"
562
+ and "\<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1)"
563
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
564
+ using assms unfolding rt_strictly_fresher_def'' by simp
565
+
566
+ lemma rt_strictly_fresherE' [elim]:
567
+ assumes "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
568
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2; \<not>(rt2 \<sqsubseteq>\<^bsub>i\<^esub> rt1) \<rbrakk> \<Longrightarrow> P rt1 rt2 i"
569
+ shows "P rt1 rt2 i"
570
+ using assms unfolding rt_strictly_fresher_def'' by simp
571
+
572
+ lemma rt_strictly_fresherI [intro]:
573
+ assumes "rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2"
574
+ and "\<not>(rt1 \<approx>\<^bsub>i\<^esub> rt2)"
575
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
576
+ unfolding rt_strictly_fresher_def using assms ..
577
+
578
+ lemmas rt_strictly_fresher_singleI [elim] = rt_strictly_fresherI [OF single_rt_fresher]
579
+
580
+ lemma rt_strictly_fresherE [elim]:
581
+ assumes "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
582
+ and "\<lbrakk> rt1 \<sqsubseteq>\<^bsub>i\<^esub> rt2; \<not>(rt1 \<approx>\<^bsub>i\<^esub> rt2) \<rbrakk> \<Longrightarrow> P rt1 rt2 i"
583
+ shows "P rt1 rt2 i"
584
+ using assms(1) unfolding rt_strictly_fresher_def
585
+ by rule (erule(1) assms(2))
586
+
587
+ lemma rt_strictly_fresher_def':
588
+ "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2 =
589
+ (nsqn\<^sub>r (the (rt1 i)) < nsqn\<^sub>r (the (rt2 i))
590
+ \<or> (nsqn\<^sub>r (the (rt1 i)) = nsqn\<^sub>r (the (rt2 i)) \<and> \<pi>\<^sub>5(the (rt1 i)) > \<pi>\<^sub>5(the (rt2 i))))"
591
+ unfolding rt_strictly_fresher_def'' rt_fresher_def fresher_def by auto
592
+
593
+ lemma rt_strictly_fresher_fresherD [dest]:
594
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
595
+ shows "the (rt1 dip) \<sqsubseteq> the (rt2 dip)"
596
+ using assms unfolding rt_strictly_fresher_def rt_fresher_def by auto
597
+
598
+ lemma rt_strictly_fresher_not_fresh_asD [dest]:
599
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
600
+ shows "\<not> rt1 \<approx>\<^bsub>dip\<^esub> rt2"
601
+ using assms unfolding rt_strictly_fresher_def by auto
602
+
603
+ lemma rt_strictly_fresher_trans [elim, trans]:
604
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
605
+ and "rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3"
606
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
607
+ using assms proof -
608
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> obtain "the (rt1 dip) \<sqsubseteq> the (rt2 dip)" by auto
609
+ also from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> obtain "the (rt2 dip) \<sqsubseteq> the (rt3 dip)" by auto
610
+ finally have "the (rt1 dip) \<sqsubseteq> the (rt3 dip)" .
611
+
612
+ moreover have "\<not> (rt1 \<approx>\<^bsub>dip\<^esub> rt3)"
613
+ proof -
614
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> obtain "\<not>(the (rt2 dip) \<sqsubseteq> the (rt1 dip))" by auto
615
+ also from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> obtain "\<not>(the (rt3 dip) \<sqsubseteq> the (rt2 dip))" by auto
616
+ finally have "\<not>(the (rt3 dip) \<sqsubseteq> the (rt1 dip))" .
617
+ thus ?thesis ..
618
+ qed
619
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3" ..
620
+ qed
621
+
622
+ lemma rt_strictly_fresher_irefl [simp]: "\<not> (rt \<sqsubset>\<^bsub>dip\<^esub> rt)"
623
+ unfolding rt_strictly_fresher_def
624
+ by clarsimp
625
+
626
+ lemma rt_fresher_trans_rt_strictly_fresher [elim, trans]:
627
+ assumes "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
628
+ and "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
629
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
630
+ proof -
631
+ from \<open>rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2\<close> have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
632
+ and "\<not>(rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
633
+ unfolding rt_strictly_fresher_def'' by auto
634
+ from this(1) and \<open>rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3\<close> have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3" ..
635
+
636
+ moreover from \<open>\<not>(rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)\<close> have "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
637
+ proof (rule contrapos_nn)
638
+ assume "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
639
+ with \<open>rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3\<close> show "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
640
+ qed
641
+
642
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
643
+ unfolding rt_strictly_fresher_def'' by auto
644
+ qed
645
+
646
+ lemma rt_fresher_trans_rt_strictly_fresher' [elim, trans]:
647
+ assumes "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2"
648
+ and "rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3"
649
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
650
+ proof -
651
+ from \<open>rt2 \<sqsubset>\<^bsub>dip\<^esub> rt3\<close> have "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt3"
652
+ and "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)"
653
+ unfolding rt_strictly_fresher_def'' by auto
654
+ from \<open>rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2\<close> and this(1) have "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt3" ..
655
+
656
+ moreover from \<open>\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2)\<close> have "\<not>(rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1)"
657
+ proof (rule contrapos_nn)
658
+ assume "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt1"
659
+ thus "rt3 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" using \<open>rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2\<close> ..
660
+ qed
661
+
662
+ ultimately show "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt3"
663
+ unfolding rt_strictly_fresher_def'' by auto
664
+ qed
665
+
666
+ lemma rt_fresher_imp_nsqn_le:
667
+ assumes "rt1 \<sqsubseteq>\<^bsub>ip\<^esub> rt2"
668
+ and "ip \<in> kD rt1"
669
+ and "ip \<in> kD rt2"
670
+ shows "nsqn rt1 ip \<le> nsqn rt2 ip"
671
+ using assms(1)
672
+ by (auto simp add: rt_fresher_def2 [OF assms(2-3)])
673
+
674
+ lemma rt_strictly_fresher_ltI [intro]:
675
+ assumes "dip \<in> kD(rt1)"
676
+ and "dip \<in> kD(rt2)"
677
+ and "nsqn rt1 dip < nsqn rt2 dip"
678
+ shows "rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2"
679
+ proof
680
+ from assms show "rt1 \<sqsubseteq>\<^bsub>dip\<^esub> rt2" ..
681
+ next
682
+ show "\<not>(rt1 \<approx>\<^bsub>dip\<^esub> rt2)"
683
+ proof
684
+ assume "rt1 \<approx>\<^bsub>dip\<^esub> rt2"
685
+ hence "rt2 \<sqsubseteq>\<^bsub>dip\<^esub> rt1" ..
686
+ hence "nsqn rt2 dip \<le> nsqn rt1 dip"
687
+ using \<open>dip \<in> kD(rt2)\<close> \<open>dip \<in> kD(rt1)\<close>
688
+ by (rule rt_fresher_imp_nsqn_le)
689
+ with \<open>nsqn rt1 dip < nsqn rt2 dip\<close> show "False"
690
+ by simp
691
+ qed
692
+ qed
693
+
694
+ lemma rt_strictly_fresher_eqI [intro]:
695
+ assumes "i\<in>kD(rt1)"
696
+ and "i\<in>kD(rt2)"
697
+ and "nsqn rt1 i = nsqn rt2 i"
698
+ and "\<pi>\<^sub>5(the (rt2 i)) < \<pi>\<^sub>5(the (rt1 i))"
699
+ shows "rt1 \<sqsubset>\<^bsub>i\<^esub> rt2"
700
+ using assms unfolding rt_strictly_fresher_def' by (auto simp add: kD_nsqn)
701
+
702
+ lemma invalidate_rtsf_left [simp]:
703
+ "\<And>dests dip rt rt'. dests dip = None \<Longrightarrow> (invalidate rt dests \<sqsubset>\<^bsub>dip\<^esub> rt') = (rt \<sqsubset>\<^bsub>dip\<^esub> rt')"
704
+ unfolding invalidate_def rt_strictly_fresher_def'
705
+ by (rule iffI) (auto split: option.split_asm)
706
+
707
+ lemma vD_invalidate_rt_strictly_fresher [simp]:
708
+ assumes "dip \<in> vD(invalidate rt1 dests)"
709
+ shows "(invalidate rt1 dests \<sqsubset>\<^bsub>dip\<^esub> rt2) = (rt1 \<sqsubset>\<^bsub>dip\<^esub> rt2)"
710
+ proof (cases "dip \<in> dom(dests)")
711
+ assume "dip \<in> dom(dests)"
712
+ hence "dip \<notin> vD(invalidate rt1 dests)"
713
+ unfolding invalidate_def vD_def
714
+ by clarsimp (metis assms option.simps(3) vD_invalidate_vD_not_dests)
715
+ with \<open>dip \<in> vD(invalidate rt1 dests)\<close> show ?thesis by simp
716
+ next
717
+ assume "dip \<notin> dom(dests)"
718
+ hence "dests dip = None" by auto
719
+ moreover with \<open>dip \<in> vD(invalidate rt1 dests)\<close> have "dip \<in> vD(rt1)"
720
+ unfolding invalidate_def vD_def
721
+ by clarsimp (metis (opaque_lifting, no_types) assms vD_Some vD_invalidate_vD_not_dests)
722
+ ultimately show ?thesis
723
+ unfolding invalidate_def rt_strictly_fresher_def' by auto
724
+ qed
725
+
726
+ lemma rt_strictly_fresher_update_other [elim!]:
727
+ "\<And>dip ip rt r rt'. \<lbrakk> dip \<noteq> ip; rt \<sqsubset>\<^bsub>dip\<^esub> rt' \<rbrakk> \<Longrightarrow> update rt ip r \<sqsubset>\<^bsub>dip\<^esub> rt'"
728
+ unfolding rt_strictly_fresher_def' by clarsimp
729
+
730
+ lemma addpreRT_strictly_fresher [simp]:
731
+ assumes "dip \<in> kD(rt)"
732
+ shows "(the (addpreRT rt dip npre) \<sqsubset>\<^bsub>ip\<^esub> rt2) = (rt \<sqsubset>\<^bsub>ip\<^esub> rt2)"
733
+ using assms unfolding rt_strictly_fresher_def' by clarsimp
734
+
735
+ lemma lt_sqn_imp_update_strictly_fresher:
736
+ assumes "dip \<in> vD (rt2 nhip)"
737
+ and *: "osn < sqn (rt2 nhip) dip"
738
+ and **: "rt \<noteq> update rt dip (osn, kno, val, hops, nhip, {})"
739
+ shows "update rt dip (osn, kno, val, hops, nhip, {}) \<sqsubset>\<^bsub>dip\<^esub> rt2 nhip"
740
+ unfolding rt_strictly_fresher_def'
741
+ proof (rule disjI1)
742
+ from ** have "nsqn (update rt dip (osn, kno, val, hops, nhip, {})) dip = osn"
743
+ by (rule nsqn_update_changed_kno_val)
744
+ with \<open>dip\<in>vD(rt2 nhip)\<close>
745
+ have "nsqn\<^sub>r (the (update rt dip (osn, kno, val, hops, nhip, {}) dip)) = osn"
746
+ by (simp add: kD_nsqn)
747
+ also have "osn < sqn (rt2 nhip) dip" by (rule *)
748
+ also have "sqn (rt2 nhip) dip = nsqn\<^sub>r (the (rt2 nhip dip))"
749
+ unfolding nsqn\<^sub>r_def using \<open>dip \<in> vD (rt2 nhip)\<close>
750
+ by - (metis vD_flag_val proj2_eq_sqn proj4_eq_flag vD_iD_gives_kD(1))
751
+ finally show "nsqn\<^sub>r (the (update rt dip (osn, kno, val, hops, nhip, {}) dip))
752
+ < nsqn\<^sub>r (the (rt2 nhip dip))" .
753
+ qed
754
+
755
+ lemma dhops_le_hops_imp_update_strictly_fresher:
756
+ assumes "dip \<in> vD(rt2 nhip)"
757
+ and sqn: "sqn (rt2 nhip) dip = osn"
758
+ and hop: "the (dhops (rt2 nhip) dip) \<le> hops"
759
+ and **: "rt \<noteq> update rt dip (osn, kno, val, Suc hops, nhip, {})"
760
+ shows "update rt dip (osn, kno, val, Suc hops, nhip, {}) \<sqsubset>\<^bsub>dip\<^esub> rt2 nhip"
761
+ unfolding rt_strictly_fresher_def'
762
+ proof (rule disjI2, rule conjI)
763
+ from ** have "nsqn (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip = osn"
764
+ by (rule nsqn_update_changed_kno_val)
765
+ with \<open>dip\<in>vD(rt2 nhip)\<close>
766
+ have "nsqn\<^sub>r (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip)) = osn"
767
+ by (simp add: kD_nsqn)
768
+ also have "osn = sqn (rt2 nhip) dip" by (rule sqn [symmetric])
769
+ also have "sqn (rt2 nhip) dip = nsqn\<^sub>r (the (rt2 nhip dip))"
770
+ unfolding nsqn\<^sub>r_def using \<open>dip \<in> vD(rt2 nhip)\<close>
771
+ by - (metis vD_flag_val proj2_eq_sqn proj4_eq_flag vD_iD_gives_kD(1))
772
+ finally show "nsqn\<^sub>r (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip))
773
+ = nsqn\<^sub>r (the (rt2 nhip dip))" .
774
+ next
775
+ have "the (dhops (rt2 nhip) dip) \<le> hops" by (rule hop)
776
+ also have "hops < hops + 1" by simp
777
+ also have "hops + 1 = the (dhops (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip)"
778
+ using ** by simp
779
+ finally have "the (dhops (rt2 nhip) dip)
780
+ < the (dhops (update rt dip (osn, kno, val, Suc hops, nhip, {})) dip)" .
781
+ thus "\<pi>\<^sub>5 (the (rt2 nhip dip)) < \<pi>\<^sub>5 (the (update rt dip (osn, kno, val, Suc hops, nhip, {}) dip))"
782
+ using \<open>dip \<in> vD(rt2 nhip)\<close> by (simp add: proj5_eq_dhops)
783
+ qed
784
+
785
+ lemma nsqn_invalidate:
786
+ assumes "dip \<in> kD(rt)"
787
+ and "\<forall>ip\<in>dom(dests). ip \<in> vD(rt) \<and> the (dests ip) = inc (sqn rt ip)"
788
+ shows "nsqn (invalidate rt dests) dip = nsqn rt dip"
789
+ proof -
790
+ from \<open>dip \<in> kD(rt)\<close> have "dip \<in> kD(invalidate rt dests)" by simp
791
+
792
+ from assms have "rt \<approx>\<^bsub>dip\<^esub> invalidate rt dests"
793
+ by (rule rt_fresh_as_inc_invalidate)
794
+ with \<open>dip \<in> kD(rt)\<close> \<open>dip \<in> kD(invalidate rt dests)\<close> show ?thesis
795
+ by (simp add: kD_nsqn del: invalidate_kD_inv)
796
+ (erule(2) rt_fresh_as_nsqnr)
797
+ qed
798
+
799
+ end
formal/afp/AODV/variants/b_fwdrreps/B_Fwdrreps.thy ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (* Title: variants/b_fwdrreps/B_Fwdrreps.thy
2
+ License: BSD 2-Clause. See LICENSE.
3
+ Author: Timothy Bourke, Inria
4
+ Author: Peter Höfner, NICTA
5
+ *)
6
+
7
+ theory %invisible B_Fwdrreps
8
+ imports "../../Aodv_Basic"
9
+ begin
10
+
11
+ chapter "Variant B: Forwarding the Route Reply"
12
+
13
+ text \<open>
14
+ Explanation~\cite[\textsection 10.2]{FehnkerEtAl:AWN:2013}:
15
+ In AODV's route discovery process, a RREP message from the destination
16
+ node is unicast back along a route towards the originator of the RREQ
17
+ message. Every intermediate node on the selected route will process the
18
+ RREP message and, in most cases, forward it towards the originator node.
19
+ However, there is a possibility that the RREP message is discarded at an
20
+ intermediate node, which results in the originator node not receiving a
21
+ reply. The discarding of the RREP message is due to the RFC specification
22
+ of AODV~\cite{RFC3561} stating that an intermediate node only forwards the
23
+ RREP message if it is not the originator node and it has created or
24
+ updated a routing table entry to the destination node described in the
25
+ RREP message. The latter requirement means that if a valid routing table
26
+ entry to the destination node already exists, and is not updated when
27
+ processing the RREP message, then the intermediate node will not forward
28
+ the message. A solution to this problem is to require intermediate nodes
29
+ to forward all RREP messages that they receive.
30
+ \<close>
31
+
32
+ end %invisible
33
+