src/HOL/HOLCF/IOA/NTP/Impl.thy
author wenzelm
Wed Nov 23 22:59:39 2011 +0100 (2011-11-23)
changeset 45620 f2a587696afb
parent 42151 4da4fc77664b
child 51717 9e7d1c139569
permissions -rw-r--r--
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm@42151
     1
(*  Title:      HOL/HOLCF/IOA/NTP/Impl.thy
mueller@3073
     2
    Author:     Tobias Nipkow & Konrad Slind
mueller@3073
     3
*)
mueller@3073
     4
wenzelm@17244
     5
header {* The implementation *}
mueller@3073
     6
wenzelm@17244
     7
theory Impl
wenzelm@17244
     8
imports Sender Receiver Abschannel
wenzelm@17244
     9
begin
mueller@3073
    10
huffman@41476
    11
type_synonym 'm impl_state
wenzelm@19739
    12
  = "'m sender_state * 'm receiver_state * 'm packet multiset * bool multiset"
wenzelm@19739
    13
  (*  sender_state   *  receiver_state   *    srch_state      * rsch_state *)
mueller@3073
    14
mueller@3073
    15
wenzelm@27361
    16
definition
wenzelm@27361
    17
  impl_ioa :: "('m action, 'm impl_state)ioa" where
wenzelm@27361
    18
  impl_def: "impl_ioa == (sender_ioa || receiver_ioa || srch_ioa || rsch_ioa)"
mueller@3073
    19
wenzelm@27361
    20
definition sen :: "'m impl_state => 'm sender_state" where "sen = fst"
wenzelm@27361
    21
definition rec :: "'m impl_state => 'm receiver_state" where "rec = fst o snd"
wenzelm@27361
    22
definition srch :: "'m impl_state => 'm packet multiset" where "srch = fst o snd o snd"
wenzelm@27361
    23
definition rsch :: "'m impl_state => bool multiset" where "rsch = snd o snd o snd"
mueller@3073
    24
wenzelm@27361
    25
definition
wenzelm@27361
    26
  hdr_sum :: "'m packet multiset => bool => nat" where
wenzelm@27361
    27
  "hdr_sum M b == countm M (%pkt. hdr(pkt) = b)"
mueller@3073
    28
mueller@3073
    29
(* Lemma 5.1 *)
wenzelm@27361
    30
definition
wenzelm@17244
    31
  "inv1(s) ==
wenzelm@17244
    32
     (!b. count (rsent(rec s)) b = count (srcvd(sen s)) b + count (rsch s) b)
wenzelm@17244
    33
   & (!b. count (ssent(sen s)) b
mueller@3073
    34
          = hdr_sum (rrcvd(rec s)) b + hdr_sum (srch s) b)"
mueller@3073
    35
mueller@3073
    36
(* Lemma 5.2 *)
wenzelm@27361
    37
definition
wenzelm@27361
    38
  "inv2(s) ==
wenzelm@17244
    39
  (rbit(rec(s)) = sbit(sen(s)) &
wenzelm@17244
    40
   ssending(sen(s)) &
mueller@3073
    41
   count (rsent(rec s)) (~sbit(sen s)) <= count (ssent(sen s)) (~sbit(sen s)) &
wenzelm@17244
    42
   count (ssent(sen s)) (~sbit(sen s)) <= count (rsent(rec s)) (sbit(sen s)))
wenzelm@17244
    43
   |
wenzelm@17244
    44
  (rbit(rec(s)) = (~sbit(sen(s))) &
wenzelm@17244
    45
   rsending(rec(s)) &
mueller@3073
    46
   count (ssent(sen s)) (~sbit(sen s)) <= count (rsent(rec s)) (sbit(sen s)) &
mueller@3073
    47
   count (rsent(rec s)) (sbit(sen s)) <= count (ssent(sen s)) (sbit(sen s)))"
mueller@3073
    48
mueller@3073
    49
(* Lemma 5.3 *)
wenzelm@27361
    50
definition
wenzelm@27361
    51
  "inv3(s) ==
wenzelm@17244
    52
   rbit(rec(s)) = sbit(sen(s))
wenzelm@17244
    53
   --> (!m. sq(sen(s))=[] | m ~= hd(sq(sen(s)))
wenzelm@17244
    54
        -->  count (rrcvd(rec s)) (sbit(sen(s)),m)
wenzelm@17244
    55
             + count (srch s) (sbit(sen(s)),m)
mueller@3073
    56
            <= count (rsent(rec s)) (~sbit(sen s)))"
mueller@3073
    57
mueller@3073
    58
(* Lemma 5.4 *)
wenzelm@27361
    59
definition "inv4(s) == rbit(rec(s)) = (~sbit(sen(s))) --> sq(sen(s)) ~= []"
wenzelm@17244
    60
wenzelm@19739
    61
wenzelm@19739
    62
subsection {* Invariants *}
wenzelm@19739
    63
huffman@35215
    64
declare le_SucI [simp]
wenzelm@19739
    65
wenzelm@19739
    66
lemmas impl_ioas =
wenzelm@19739
    67
  impl_def sender_ioa_def receiver_ioa_def srch_ioa_thm [THEN eq_reflection]
wenzelm@19739
    68
  rsch_ioa_thm [THEN eq_reflection]
wenzelm@19739
    69
wenzelm@19739
    70
lemmas "transitions" =
wenzelm@19739
    71
  sender_trans_def receiver_trans_def srch_trans_def rsch_trans_def
wenzelm@19739
    72
wenzelm@19739
    73
wenzelm@19739
    74
lemmas [simp] =
wenzelm@19739
    75
  ioa_triple_proj starts_of_par trans_of_par4 in_sender_asig
wenzelm@19739
    76
  in_receiver_asig in_srch_asig in_rsch_asig
wenzelm@19739
    77
wenzelm@19739
    78
declare let_weak_cong [cong]
wenzelm@19739
    79
wenzelm@19739
    80
lemma [simp]:
wenzelm@19739
    81
  "fst(x) = sen(x)"
wenzelm@19739
    82
  "fst(snd(x)) = rec(x)"
wenzelm@19739
    83
  "fst(snd(snd(x))) = srch(x)"
wenzelm@19739
    84
  "snd(snd(snd(x))) = rsch(x)"
wenzelm@19739
    85
  by (simp_all add: sen_def rec_def srch_def rsch_def)
wenzelm@19739
    86
wenzelm@19739
    87
lemma [simp]:
wenzelm@19739
    88
  "a:actions(sender_asig)
wenzelm@19739
    89
  | a:actions(receiver_asig)
wenzelm@19739
    90
  | a:actions(srch_asig)
wenzelm@19739
    91
  | a:actions(rsch_asig)"
wenzelm@19739
    92
  by (induct a) simp_all
wenzelm@19739
    93
wenzelm@19739
    94
declare split_paired_All [simp del]
wenzelm@19739
    95
wenzelm@19739
    96
wenzelm@19739
    97
(* Three Simp_sets in different sizes
wenzelm@19739
    98
----------------------------------------------
wenzelm@19739
    99
wenzelm@19739
   100
1) simpset() does not unfold the transition relations
wenzelm@19739
   101
2) ss unfolds transition relations
wenzelm@19739
   102
3) renname_ss unfolds transitions and the abstract channel *)
wenzelm@19739
   103
wenzelm@19739
   104
ML {*
wenzelm@27355
   105
val ss = @{simpset} addsimps @{thms "transitions"};
wenzelm@26305
   106
val rename_ss = ss addsimps @{thms unfold_renaming};
wenzelm@19739
   107
wenzelm@45620
   108
val tac =
wenzelm@45620
   109
  asm_simp_tac (ss |> Simplifier.add_cong @{thm conj_cong} |> Splitter.add_split @{thm split_if})
wenzelm@45620
   110
val tac_ren =
wenzelm@45620
   111
  asm_simp_tac (rename_ss |> Simplifier.add_cong @{thm conj_cong} |> Splitter.add_split @{thm split_if})
wenzelm@19739
   112
*}
wenzelm@19739
   113
wenzelm@19739
   114
wenzelm@19739
   115
subsubsection {* Invariant 1 *}
wenzelm@19739
   116
wenzelm@26305
   117
lemma raw_inv1: "invariant impl_ioa inv1"
wenzelm@19739
   118
wenzelm@19739
   119
apply (unfold impl_ioas)
wenzelm@19739
   120
apply (rule invariantI)
wenzelm@19739
   121
apply (simp add: inv1_def hdr_sum_def srcvd_def ssent_def rsent_def rrcvd_def)
wenzelm@19739
   122
wenzelm@19739
   123
apply (simp (no_asm) del: trans_of_par4 add: imp_conjR inv1_def)
wenzelm@19739
   124
wenzelm@19739
   125
txt {* Split proof in two *}
wenzelm@19739
   126
apply (rule conjI)
wenzelm@19739
   127
wenzelm@19739
   128
(* First half *)
wenzelm@19739
   129
apply (simp add: Impl.inv1_def split del: split_if)
wenzelm@19739
   130
apply (induct_tac a)
wenzelm@19739
   131
wenzelm@19739
   132
apply (tactic "EVERY1[tac, tac, tac, tac]")
wenzelm@19739
   133
apply (tactic "tac 1")
wenzelm@19739
   134
apply (tactic "tac_ren 1")
wenzelm@19739
   135
wenzelm@19739
   136
txt {* 5 + 1 *}
wenzelm@19739
   137
wenzelm@19739
   138
apply (tactic "tac 1")
wenzelm@19739
   139
apply (tactic "tac_ren 1")
wenzelm@19739
   140
wenzelm@19739
   141
txt {* 4 + 1 *}
wenzelm@19739
   142
apply (tactic {* EVERY1[tac, tac, tac, tac] *})
wenzelm@19739
   143
wenzelm@19739
   144
wenzelm@19739
   145
txt {* Now the other half *}
wenzelm@19739
   146
apply (simp add: Impl.inv1_def split del: split_if)
wenzelm@19739
   147
apply (induct_tac a)
wenzelm@19739
   148
apply (tactic "EVERY1 [tac, tac]")
wenzelm@19739
   149
wenzelm@19739
   150
txt {* detour 1 *}
wenzelm@19739
   151
apply (tactic "tac 1")
wenzelm@19739
   152
apply (tactic "tac_ren 1")
wenzelm@19739
   153
apply (rule impI)
wenzelm@19739
   154
apply (erule conjE)+
wenzelm@19739
   155
apply (simp (no_asm_simp) add: hdr_sum_def Multiset.count_def Multiset.countm_nonempty_def
wenzelm@19739
   156
  split add: split_if)
wenzelm@19739
   157
txt {* detour 2 *}
wenzelm@19739
   158
apply (tactic "tac 1")
wenzelm@19739
   159
apply (tactic "tac_ren 1")
wenzelm@19739
   160
apply (rule impI)
wenzelm@19739
   161
apply (erule conjE)+
wenzelm@19739
   162
apply (simp add: Impl.hdr_sum_def Multiset.count_def Multiset.countm_nonempty_def
nipkow@25161
   163
  Multiset.delm_nonempty_def split add: split_if)
wenzelm@19739
   164
apply (rule allI)
wenzelm@19739
   165
apply (rule conjI)
wenzelm@19739
   166
apply (rule impI)
wenzelm@19739
   167
apply hypsubst
wenzelm@19739
   168
apply (rule pred_suc [THEN iffD1])
wenzelm@19739
   169
apply (drule less_le_trans)
wenzelm@19739
   170
apply (cut_tac eq_packet_imp_eq_hdr [unfolded Packet.hdr_def, THEN countm_props])
wenzelm@19739
   171
apply assumption
wenzelm@19739
   172
apply assumption
wenzelm@19739
   173
wenzelm@19739
   174
apply (rule countm_done_delm [THEN mp, symmetric])
wenzelm@19739
   175
apply (rule refl)
wenzelm@19739
   176
apply (simp (no_asm_simp) add: Multiset.count_def)
wenzelm@19739
   177
wenzelm@19739
   178
apply (rule impI)
wenzelm@19739
   179
apply (simp add: neg_flip)
wenzelm@19739
   180
apply hypsubst
wenzelm@19739
   181
apply (rule countm_spurious_delm)
wenzelm@19739
   182
apply (simp (no_asm))
wenzelm@19739
   183
wenzelm@19739
   184
apply (tactic "EVERY1 [tac, tac, tac, tac, tac, tac]")
wenzelm@19739
   185
wenzelm@19739
   186
done
wenzelm@19739
   187
wenzelm@19739
   188
wenzelm@19739
   189
wenzelm@19739
   190
subsubsection {* INVARIANT 2 *}
wenzelm@19739
   191
wenzelm@26305
   192
lemma raw_inv2: "invariant impl_ioa inv2"
wenzelm@19739
   193
wenzelm@19739
   194
  apply (rule invariantI1)
wenzelm@19739
   195
  txt {* Base case *}
wenzelm@19739
   196
  apply (simp add: inv2_def receiver_projections sender_projections impl_ioas)
wenzelm@19739
   197
wenzelm@19739
   198
  apply (simp (no_asm_simp) add: impl_ioas split del: split_if)
wenzelm@19739
   199
  apply (induct_tac "a")
wenzelm@19739
   200
wenzelm@19739
   201
  txt {* 10 cases. First 4 are simple, since state doesn't change *}
wenzelm@19739
   202
wenzelm@28265
   203
  ML_prf {* val tac2 = asm_full_simp_tac (ss addsimps [@{thm inv2_def}]) *}
wenzelm@19739
   204
wenzelm@19739
   205
  txt {* 10 - 7 *}
wenzelm@19739
   206
  apply (tactic "EVERY1 [tac2,tac2,tac2,tac2]")
wenzelm@19739
   207
  txt {* 6 *}
wenzelm@26305
   208
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv1_def}]
wenzelm@26305
   209
                               (@{thm raw_inv1} RS @{thm invariantE}) RS conjunct1] 1 *})
wenzelm@19739
   210
wenzelm@19739
   211
  txt {* 6 - 5 *}
wenzelm@19739
   212
  apply (tactic "EVERY1 [tac2,tac2]")
wenzelm@19739
   213
wenzelm@19739
   214
  txt {* 4 *}
wenzelm@26305
   215
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv1_def}]
wenzelm@26305
   216
                                (@{thm raw_inv1} RS @{thm invariantE}) RS conjunct1] 1 *})
wenzelm@19739
   217
  apply (tactic "tac2 1")
wenzelm@19739
   218
wenzelm@19739
   219
  txt {* 3 *}
wenzelm@26305
   220
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv1_def}]
wenzelm@26305
   221
    (@{thm raw_inv1} RS @{thm invariantE})] 1 *})
wenzelm@19739
   222
wenzelm@19739
   223
  apply (tactic "tac2 1")
wenzelm@28839
   224
  apply (tactic {* fold_goals_tac [rewrite_rule [@{thm Packet.hdr_def}]
wenzelm@27361
   225
    (@{thm Impl.hdr_sum_def})] *})
wenzelm@19739
   226
  apply arith
wenzelm@19739
   227
wenzelm@19739
   228
  txt {* 2 *}
wenzelm@19739
   229
  apply (tactic "tac2 1")
wenzelm@26305
   230
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv1_def}]
wenzelm@26305
   231
                               (@{thm raw_inv1} RS @{thm invariantE}) RS conjunct1] 1 *})
wenzelm@19739
   232
  apply (intro strip)
wenzelm@19739
   233
  apply (erule conjE)+
wenzelm@19739
   234
  apply simp
wenzelm@19739
   235
wenzelm@19739
   236
  txt {* 1 *}
wenzelm@19739
   237
  apply (tactic "tac2 1")
wenzelm@26305
   238
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv1_def}]
wenzelm@26305
   239
                               (@{thm raw_inv1} RS @{thm invariantE}) RS conjunct2] 1 *})
wenzelm@19739
   240
  apply (intro strip)
wenzelm@19739
   241
  apply (erule conjE)+
wenzelm@28839
   242
  apply (tactic {* fold_goals_tac [rewrite_rule [@{thm Packet.hdr_def}] (@{thm Impl.hdr_sum_def})] *})
wenzelm@19739
   243
  apply simp
wenzelm@19739
   244
wenzelm@19739
   245
  done
wenzelm@19739
   246
wenzelm@19739
   247
wenzelm@19739
   248
subsubsection {* INVARIANT 3 *}
wenzelm@19739
   249
wenzelm@26305
   250
lemma raw_inv3: "invariant impl_ioa inv3"
wenzelm@19739
   251
wenzelm@19739
   252
  apply (rule invariantI)
wenzelm@19739
   253
  txt {* Base case *}
wenzelm@19739
   254
  apply (simp add: Impl.inv3_def receiver_projections sender_projections impl_ioas)
wenzelm@19739
   255
wenzelm@19739
   256
  apply (simp (no_asm_simp) add: impl_ioas split del: split_if)
wenzelm@19739
   257
  apply (induct_tac "a")
wenzelm@19739
   258
wenzelm@28265
   259
  ML_prf {* val tac3 = asm_full_simp_tac (ss addsimps [@{thm inv3_def}]) *}
wenzelm@19739
   260
wenzelm@19739
   261
  txt {* 10 - 8 *}
wenzelm@19739
   262
wenzelm@19739
   263
  apply (tactic "EVERY1[tac3,tac3,tac3]")
wenzelm@19739
   264
wenzelm@19739
   265
  apply (tactic "tac_ren 1")
wenzelm@19739
   266
  apply (intro strip, (erule conjE)+)
wenzelm@19739
   267
  apply hypsubst
wenzelm@19739
   268
  apply (erule exE)
wenzelm@19739
   269
  apply simp
wenzelm@19739
   270
wenzelm@19739
   271
  txt {* 7 *}
wenzelm@19739
   272
  apply (tactic "tac3 1")
wenzelm@19739
   273
  apply (tactic "tac_ren 1")
wenzelm@19739
   274
  apply force
wenzelm@19739
   275
wenzelm@19739
   276
  txt {* 6 - 3 *}
wenzelm@19739
   277
wenzelm@19739
   278
  apply (tactic "EVERY1[tac3,tac3,tac3,tac3]")
wenzelm@19739
   279
wenzelm@19739
   280
  txt {* 2 *}
wenzelm@19739
   281
  apply (tactic "asm_full_simp_tac ss 1")
wenzelm@19739
   282
  apply (simp (no_asm) add: inv3_def)
wenzelm@19739
   283
  apply (intro strip, (erule conjE)+)
wenzelm@19739
   284
  apply (rule imp_disjL [THEN iffD1])
wenzelm@19739
   285
  apply (rule impI)
wenzelm@26305
   286
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv2_def}]
wenzelm@26305
   287
    (@{thm raw_inv2} RS @{thm invariantE})] 1 *})
wenzelm@19739
   288
  apply simp
wenzelm@19739
   289
  apply (erule conjE)+
wenzelm@19739
   290
  apply (rule_tac j = "count (ssent (sen s)) (~sbit (sen s))" and
wenzelm@19739
   291
    k = "count (rsent (rec s)) (sbit (sen s))" in le_trans)
wenzelm@26305
   292
  apply (tactic {* forward_tac [rewrite_rule [@{thm inv1_def}]
wenzelm@26305
   293
                                (@{thm raw_inv1} RS @{thm invariantE}) RS conjunct2] 1 *})
wenzelm@19739
   294
  apply (simp add: hdr_sum_def Multiset.count_def)
wenzelm@19739
   295
  apply (rule add_le_mono)
wenzelm@19739
   296
  apply (rule countm_props)
wenzelm@19739
   297
  apply (simp (no_asm))
wenzelm@19739
   298
  apply (rule countm_props)
wenzelm@19739
   299
  apply (simp (no_asm))
wenzelm@19739
   300
  apply assumption
wenzelm@19739
   301
wenzelm@19739
   302
  txt {* 1 *}
wenzelm@19739
   303
  apply (tactic "tac3 1")
wenzelm@19739
   304
  apply (intro strip, (erule conjE)+)
wenzelm@19739
   305
  apply (rule imp_disjL [THEN iffD1])
wenzelm@19739
   306
  apply (rule impI)
wenzelm@26305
   307
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv2_def}]
wenzelm@26305
   308
    (@{thm raw_inv2} RS @{thm invariantE})] 1 *})
wenzelm@19739
   309
  apply simp
wenzelm@19739
   310
  done
wenzelm@19739
   311
wenzelm@19739
   312
wenzelm@19739
   313
subsubsection {* INVARIANT 4 *}
wenzelm@19739
   314
wenzelm@26305
   315
lemma raw_inv4: "invariant impl_ioa inv4"
wenzelm@19739
   316
wenzelm@19739
   317
  apply (rule invariantI)
wenzelm@19739
   318
  txt {* Base case *}
wenzelm@19739
   319
  apply (simp add: Impl.inv4_def receiver_projections sender_projections impl_ioas)
wenzelm@19739
   320
wenzelm@19739
   321
  apply (simp (no_asm_simp) add: impl_ioas split del: split_if)
wenzelm@19739
   322
  apply (induct_tac "a")
wenzelm@19739
   323
wenzelm@28265
   324
  ML_prf {* val tac4 =  asm_full_simp_tac (ss addsimps [@{thm inv4_def}]) *}
wenzelm@19739
   325
wenzelm@19739
   326
  txt {* 10 - 2 *}
wenzelm@19739
   327
wenzelm@19739
   328
  apply (tactic "EVERY1[tac4,tac4,tac4,tac4,tac4,tac4,tac4,tac4,tac4]")
wenzelm@19739
   329
wenzelm@19739
   330
  txt {* 2 b *}
wenzelm@19739
   331
wenzelm@19739
   332
  apply (intro strip, (erule conjE)+)
wenzelm@26305
   333
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv2_def}]
wenzelm@26305
   334
                               (@{thm raw_inv2} RS @{thm invariantE})] 1 *})
wenzelm@19739
   335
  apply simp
wenzelm@19739
   336
wenzelm@19739
   337
  txt {* 1 *}
wenzelm@19739
   338
  apply (tactic "tac4 1")
wenzelm@19739
   339
  apply (intro strip, (erule conjE)+)
wenzelm@19739
   340
  apply (rule ccontr)
wenzelm@26305
   341
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv2_def}]
wenzelm@26305
   342
                               (@{thm raw_inv2} RS @{thm invariantE})] 1 *})
wenzelm@26305
   343
  apply (tactic {* forward_tac [rewrite_rule [@{thm Impl.inv3_def}]
wenzelm@26305
   344
                               (@{thm raw_inv3} RS @{thm invariantE})] 1 *})
wenzelm@19739
   345
  apply simp
wenzelm@19739
   346
  apply (erule_tac x = "m" in allE)
wenzelm@19739
   347
  apply simp
wenzelm@19739
   348
  done
wenzelm@19739
   349
wenzelm@19739
   350
wenzelm@19739
   351
text {* rebind them *}
wenzelm@19739
   352
wenzelm@26305
   353
lemmas inv1 = raw_inv1 [THEN invariantE, unfolded inv1_def]
wenzelm@26305
   354
  and inv2 = raw_inv2 [THEN invariantE, unfolded inv2_def]
wenzelm@26305
   355
  and inv3 = raw_inv3 [THEN invariantE, unfolded inv3_def]
wenzelm@26305
   356
  and inv4 = raw_inv4 [THEN invariantE, unfolded inv4_def]
mueller@3073
   357
mueller@3073
   358
end