src/HOL/UNITY/Alloc.ML
author paulson
Mon Sep 20 10:40:40 1999 +0200 (1999-09-20)
changeset 7540 8af61a565a4a
parent 7538 357873391561
child 7689 affe0c2fdfbf
permissions -rw-r--r--
working Safety proof for the system at last
paulson@6828
     1
(*  Title:      HOL/UNITY/Alloc
paulson@6828
     2
    ID:         $Id$
paulson@6828
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@6828
     4
    Copyright   1998  University of Cambridge
paulson@6828
     5
paulson@6828
     6
Specification of Chandy and Charpentier's Allocator
paulson@7540
     7
paulson@7540
     8
STOP USING o (COMPOSITION), since function application is naturally associative
paulson@6828
     9
*)
paulson@6828
    10
paulson@7399
    11
paulson@7399
    12
Goal "~ f #2 ==> ~ (!t::nat. (#0 <= t & t <= #10) --> f t)";
paulson@7399
    13
paulson@7399
    14
(*Useful examples:  singletonI RS subst_elem,  subst_elem RSN (2,IntI) *)
paulson@7399
    15
Goal "[| b:A;  a=b |] ==> a:A";
paulson@7399
    16
by (etac ssubst 1);
paulson@7399
    17
by (assume_tac 1);
paulson@7399
    18
qed "subst_elem";
paulson@7399
    19
paulson@7399
    20
paulson@7399
    21
paulson@7538
    22
Goal "(ALL i: lessThan n. f i <= g i) --> sum f n <= sum g n";
paulson@7538
    23
by (induct_tac "n" 1);
paulson@7538
    24
by Auto_tac;
paulson@7538
    25
by (dres_inst_tac [("x","n")] bspec 1);
paulson@7538
    26
by Auto_tac;
paulson@7538
    27
by (arith_tac 1);
paulson@7538
    28
qed_spec_mp "sum_mono";
paulson@7538
    29
paulson@7540
    30
Goal "ALL xs. xs <= ys --> tokens xs <= tokens ys";
paulson@7540
    31
by (induct_tac "ys" 1);
paulson@7540
    32
by (auto_tac (claset(), simpset() addsimps [prefix_Cons]));
paulson@7540
    33
qed_spec_mp "tokens_mono_prefix";
paulson@7540
    34
paulson@7540
    35
Goalw [mono_def] "mono tokens";
paulson@7540
    36
by (blast_tac (claset() addIs [tokens_mono_prefix]) 1);
paulson@7540
    37
qed "mono_tokens";
paulson@7399
    38
paulson@6840
    39
(*Generalized version allowing different clients*)
paulson@6840
    40
Goal "[| Alloc : alloc_spec;  \
paulson@6840
    41
\        Client : (lessThan Nclients) funcset client_spec;  \
paulson@6840
    42
\        Network : network_spec |] \
paulson@6840
    43
\     ==> (extend sysOfAlloc Alloc) \
paulson@6840
    44
\         Join (extend sysOfClient (PLam (lessThan Nclients) Client)) \
paulson@6840
    45
\         Join Network : system_spec";
paulson@6840
    46
paulson@6840
    47
Goal "System : system_spec";
paulson@6840
    48
paulson@6840
    49
paulson@6828
    50
Goalw [sysOfAlloc_def] "inj sysOfAlloc";
paulson@6828
    51
by (rtac injI 1);
paulson@7347
    52
by (auto_tac (claset() addSWrapper record_split_wrapper, simpset()));
paulson@6828
    53
qed "inj_sysOfAlloc";
paulson@6828
    54
paulson@6828
    55
Goalw [sysOfAlloc_def] "surj sysOfAlloc";
paulson@6828
    56
by (res_inst_tac [("f", "%s. ((| allocGiv = allocGiv s,    \
paulson@6828
    57
\                                allocAsk = allocAsk s,    \
paulson@6828
    58
\                                allocRel = allocRel s |), \
paulson@6828
    59
\                             client s)")] surjI 1);
paulson@7347
    60
by (auto_tac (claset() addSWrapper record_split_wrapper, simpset()));
paulson@6828
    61
qed "surj_sysOfAlloc";
paulson@6828
    62
paulson@6828
    63
AddIffs [inj_sysOfAlloc, surj_sysOfAlloc];
paulson@6828
    64
paulson@7482
    65
Goalw [good_map_def] "good_map sysOfAlloc";
paulson@7482
    66
by Auto_tac;
paulson@7482
    67
qed "good_map_sysOfAlloc";
paulson@7399
    68
paulson@7399
    69
(*MUST BE AUTOMATED: even the statement of such results*)
paulson@7399
    70
Goal "!!s. inv sysOfAlloc s = \
paulson@7399
    71
\            ((| allocGiv = allocGiv s,   \
paulson@7399
    72
\                allocAsk = allocAsk s,   \
paulson@7399
    73
\                allocRel = allocRel s|), \
paulson@7399
    74
\             client s)";
paulson@7399
    75
by (rtac (inj_sysOfAlloc RS inv_f_eq) 1);
paulson@7399
    76
by (auto_tac (claset() addSWrapper record_split_wrapper, 
paulson@7399
    77
	      simpset() addsimps [sysOfAlloc_def]));
paulson@7399
    78
qed "inv_sysOfAlloc_eq";
paulson@7399
    79
paulson@7538
    80
Addsimps [inv_sysOfAlloc_eq];
paulson@7399
    81
paulson@7365
    82
(**SHOULD NOT BE NECESSARY: The various injections into the system
paulson@7365
    83
   state need to be treated symmetrically or done automatically*)
paulson@7347
    84
Goalw [sysOfClient_def] "inj sysOfClient";
paulson@7347
    85
by (rtac injI 1);
paulson@7347
    86
by (auto_tac (claset() addSDs [inj_sysOfAlloc RS injD]
paulson@7347
    87
		       addSWrapper record_split_wrapper, simpset()));
paulson@7347
    88
qed "inj_sysOfClient";
paulson@7347
    89
paulson@7347
    90
Goalw [sysOfClient_def] "surj sysOfClient";
paulson@7347
    91
by (cut_facts_tac [surj_sysOfAlloc] 1);
paulson@7347
    92
by (rewtac surj_def);
paulson@7347
    93
by Auto_tac;
paulson@7347
    94
by (Blast_tac 1);
paulson@7347
    95
qed "surj_sysOfClient";
paulson@7347
    96
paulson@7347
    97
AddIffs [inj_sysOfClient, surj_sysOfClient];
paulson@7347
    98
paulson@7482
    99
Goalw [good_map_def] "good_map sysOfClient";
paulson@7482
   100
by Auto_tac;
paulson@7482
   101
qed "good_map_sysOfClient";
paulson@6828
   102
paulson@7347
   103
(*MUST BE AUTOMATED: even the statement of such results*)
paulson@7347
   104
Goal "!!s. inv sysOfClient s = \
paulson@7347
   105
\            (client s, \
paulson@7347
   106
\             (| allocGiv = allocGiv s, \
paulson@7347
   107
\                allocAsk = allocAsk s, \
paulson@7347
   108
\                allocRel = allocRel s|) )";
paulson@7347
   109
by (rtac (inj_sysOfClient RS inv_f_eq) 1);
paulson@7399
   110
by (auto_tac (claset() addSWrapper record_split_wrapper, 
paulson@7399
   111
	      simpset() addsimps [sysOfAlloc_def, sysOfClient_def]));
paulson@7365
   112
qed "inv_sysOfClient_eq";
paulson@6837
   113
paulson@6837
   114
paulson@7365
   115
Open_locale "System";
paulson@7365
   116
paulson@7365
   117
val Alloc = thm "Alloc";
paulson@7365
   118
val Client = thm "Client";
paulson@7365
   119
val Network = thm "Network";
paulson@7365
   120
val System_def = thm "System_def";
paulson@7365
   121
paulson@7365
   122
AddIffs [finite_lessThan];
paulson@7365
   123
paulson@7365
   124
(*Client : <unfolded specification> *)
paulson@7365
   125
val Client_Spec =
paulson@7365
   126
    rewrite_rule [client_spec_def, client_increasing_def,
paulson@7365
   127
		  client_bounded_def, client_progress_def] Client;
paulson@7365
   128
paulson@7365
   129
Goal "Client : UNIV guarantees Increasing ask";
paulson@7365
   130
by (cut_facts_tac [Client_Spec] 1);
paulson@7365
   131
by (asm_full_simp_tac (simpset() addsimps [guarantees_Int_right]) 1);
paulson@7365
   132
qed "Client_Increasing_ask";
paulson@7365
   133
paulson@7365
   134
Goal "Client : UNIV guarantees Increasing rel";
paulson@7365
   135
by (cut_facts_tac [Client_Spec] 1);
paulson@7365
   136
by (asm_full_simp_tac (simpset() addsimps [guarantees_Int_right]) 1);
paulson@7365
   137
qed "Client_Increasing_rel";
paulson@7365
   138
paulson@7365
   139
AddIffs [Client_Increasing_ask, Client_Increasing_rel];
paulson@7365
   140
paulson@7365
   141
Goal "Client : UNIV guarantees Always {s. ALL elt : set (ask s). elt <= NbT}";
paulson@7365
   142
by (cut_facts_tac [Client_Spec] 1);
paulson@7365
   143
by Auto_tac;
paulson@7365
   144
qed "Client_Bounded";
paulson@7365
   145
paulson@7365
   146
(*Client_Progress is cumbersome to state*)
paulson@7365
   147
val Client_Progress = Client_Spec RS IntD2;
paulson@6828
   148
paulson@6828
   149
paulson@7365
   150
(*Network : <unfolded specification> *)
paulson@7365
   151
val Network_Spec =
paulson@7365
   152
    rewrite_rule [network_spec_def, network_ask_def,
paulson@7365
   153
		  network_giv_def, network_rel_def] Network;
paulson@7365
   154
paulson@7365
   155
(*CANNOT use bind_thm: it puts the theorem into standard form, in effect
paulson@7365
   156
  exporting it from the locale*)
paulson@7365
   157
val Network_Ask = Network_Spec RS IntD1 RS IntD1;
paulson@7540
   158
val Network_Giv = Network_Spec RS IntD1 RS IntD2 RS INT_D;
paulson@7365
   159
val Network_Rel = Network_Spec RS IntD2 RS INT_D;
paulson@7365
   160
paulson@7365
   161
paulson@7365
   162
(*Alloc : <unfolded specification> *)
paulson@7365
   163
val Alloc_Spec =
paulson@7365
   164
    rewrite_rule [alloc_spec_def, alloc_increasing_def,
paulson@7365
   165
		  alloc_safety_def, alloc_progress_def] Alloc;
paulson@7365
   166
paulson@7540
   167
Goal "i < Nclients ==> Alloc : UNIV guarantees Increasing (sub i o allocGiv)";
paulson@7365
   168
by (cut_facts_tac [Alloc_Spec] 1);
paulson@7365
   169
by (asm_full_simp_tac (simpset() addsimps [guarantees_INT_right]) 1);
paulson@7365
   170
qed "Alloc_Increasing";
paulson@7365
   171
paulson@7365
   172
val Alloc_Safety = Alloc_Spec RS IntD1 RS IntD2;
paulson@7365
   173
val Alloc_Progress = (Alloc_Spec RS IntD2
paulson@7365
   174
                      |> simplify (simpset() addsimps [guarantees_INT_right]))
paulson@7365
   175
                     RS bspec RS spec;
paulson@7365
   176
		     
paulson@7365
   177
paulson@7347
   178
paulson@7365
   179
(*Not sure what to say about the other components because they involve
paulson@7365
   180
  extend*)
paulson@7399
   181
Goalw [System_def] "Network <= System";
paulson@7365
   182
by (blast_tac (claset() addIs [componentI]) 1);
paulson@7365
   183
qed "Network_component_System";
paulson@7365
   184
paulson@7399
   185
Goalw [System_def] "(extend sysOfAlloc Alloc) <= System";
paulson@7399
   186
by (blast_tac (claset() addIs [componentI]) 1);
paulson@7399
   187
qed "Alloc_component_System";
paulson@7365
   188
paulson@7399
   189
AddIffs [Network_component_System, Alloc_component_System];
paulson@7399
   190
paulson@7365
   191
paulson@7365
   192
(* F : UNIV guarantees Increasing func
paulson@7365
   193
   ==> extend sysOfClient F : UNIV guarantees Increasing (func o client) *)
paulson@7399
   194
val extend_Client_guar_Increasing =
paulson@7482
   195
  good_map_sysOfClient RS export extend_guar_Increasing
paulson@7365
   196
  |> simplify (simpset() addsimps [inv_sysOfClient_eq]);
paulson@7365
   197
paulson@7540
   198
fun alloc_export th = good_map_sysOfAlloc RS export th;
paulson@7540
   199
paulson@7540
   200
(*NEED AUTOMATIC PROPAGATION of Alloc_Increasing*)
paulson@7540
   201
Goal "i < Nclients \
paulson@7540
   202
\ ==> extend sysOfAlloc Alloc : UNIV guarantees Increasing (sub i o allocGiv)";
paulson@7540
   203
by (dtac (Alloc_Increasing RS alloc_export extend_guar_Increasing) 1);
paulson@7540
   204
by (auto_tac (claset(), simpset() addsimps [o_def]));
paulson@7540
   205
qed "extend_Alloc_Increasing_allocGiv";
paulson@7540
   206
paulson@7365
   207
paulson@7365
   208
(** Proof of property (1) **)
paulson@7365
   209
paulson@7365
   210
(*step 1*)
paulson@7365
   211
Goalw [System_def]
paulson@7365
   212
     "i < Nclients ==> System : Increasing (rel o sub i o client)";
paulson@7365
   213
by (rtac ([guaranteesI RS disjI2 RS guarantees_Join_I, UNIV_I] 
paulson@7365
   214
	  MRS guaranteesD) 1);
paulson@7365
   215
by (asm_simp_tac 
paulson@7365
   216
    (simpset() addsimps [guarantees_PLam_I, 
paulson@7399
   217
			 extend_Client_guar_Increasing RS guaranteesD,
paulson@7365
   218
			 lift_prog_guar_Increasing]) 1);
paulson@7347
   219
qed "System_Increasing_rel";
paulson@6828
   220
paulson@7365
   221
(*Note: the first step above performs simple quantifier reasoning.  It could
paulson@7365
   222
  be replaced by a proof mentioning no guarantees primitives*)
paulson@6828
   223
paulson@6828
   224
paulson@7365
   225
(*step 2*)
paulson@7365
   226
Goal "i < Nclients ==> System : Increasing (sub i o allocRel)";
paulson@7365
   227
by (rtac Follows_Increasing1 1);
paulson@7365
   228
by (blast_tac (claset() addIs [Network_Rel RS component_guaranteesD,
paulson@7540
   229
			       System_Increasing_rel]) 1);
paulson@7365
   230
qed "System_Increasing_allocRel";
paulson@6828
   231
paulson@7538
   232
(*step 2*)
paulson@7399
   233
Goal "System : Always {s. sum (%i. (tokens o sub i o allocGiv) s) Nclients \
paulson@7399
   234
\                  <= NbT + sum (%i. (tokens o sub i o allocRel) s) Nclients}";
paulson@7399
   235
by (res_inst_tac 
paulson@7537
   236
    [("X", "(INT i : lessThan Nclients. Increasing (sub i o allocRel))")] 
paulson@7537
   237
    component_guaranteesD 1);
paulson@7482
   238
by (rtac Alloc_component_System 3);
paulson@7537
   239
by (force_tac (claset(), simpset() addsimps [System_Increasing_allocRel]) 2);
paulson@7538
   240
by (rtac (Alloc_Safety RS project_guarantees) 1);
paulson@7537
   241
by Auto_tac;
paulson@7538
   242
(*1: Increasing precondition*)
paulson@7538
   243
by (stac (alloc_export project_Increasing) 1);
paulson@7538
   244
by (force_tac
paulson@7538
   245
    (claset(),
paulson@7538
   246
     simpset() addsimps [o_def, alloc_export project_Increasing]) 1);
paulson@7538
   247
(*2: Always postcondition*)
paulson@7538
   248
by (dtac (subset_refl RS alloc_export project_Always_D) 1);
paulson@7538
   249
by (asm_full_simp_tac
paulson@7538
   250
     (simpset() addsimps [alloc_export Collect_eq_extend_set RS sym]) 1);
paulson@7538
   251
qed "System_sum_bounded";
paulson@7537
   252
paulson@7538
   253
(*step 3*)
paulson@7540
   254
Goal "System : Always (INT i: lessThan Nclients. \
paulson@7540
   255
\                         {s. (tokens o giv o sub i o client) s \
paulson@7540
   256
\                          <= (tokens o sub i o allocGiv) s})";
paulson@7540
   257
by (auto_tac (claset(), 
paulson@7540
   258
	      simpset() delsimps [o_apply]
paulson@7540
   259
			addsimps [Always_INT_distrib]));
paulson@7540
   260
by (rtac Follows_Bounded 1);
paulson@7540
   261
by (simp_tac (HOL_ss addsimps [o_assoc RS sym]) 1);
paulson@7540
   262
by (rtac (mono_tokens RS mono_Follows_o RS subsetD) 1);
paulson@7540
   263
by (simp_tac (HOL_ss addsimps [o_assoc]) 1);
paulson@7540
   264
by (blast_tac (claset() addIs [Network_Giv RS component_guaranteesD,
paulson@7540
   265
	  extend_Alloc_Increasing_allocGiv RS component_guaranteesD]) 1);
paulson@7540
   266
qed "System_Always_giv_le_allocGiv";
paulson@7540
   267
paulson@7540
   268
paulson@7540
   269
Goal "System : Always (INT i: lessThan Nclients. \
paulson@7540
   270
\                         {s. (tokens o sub i o allocRel) s \
paulson@7540
   271
\                          <= (tokens o rel o sub i o client) s})";
paulson@7540
   272
by (auto_tac (claset(), 
paulson@7540
   273
	      simpset() delsimps [o_apply]
paulson@7540
   274
			addsimps [Always_INT_distrib]));
paulson@7540
   275
by (rtac Follows_Bounded 1);
paulson@7540
   276
by (simp_tac (HOL_ss addsimps [o_assoc RS sym]) 1);
paulson@7540
   277
by (rtac (mono_tokens RS mono_Follows_o RS subsetD) 1);
paulson@7540
   278
by (simp_tac (HOL_ss addsimps [o_assoc]) 1);
paulson@7540
   279
by (blast_tac (claset() addIs [Network_Rel RS component_guaranteesD,
paulson@7540
   280
			       System_Increasing_rel]) 1);
paulson@7540
   281
qed "System_Always_allocRel_le_rel";
paulson@7540
   282
paulson@7540
   283
paulson@7540
   284
Goal "System : Always {s. sum (%i. (tokens o giv o sub i o client)s) Nclients \
paulson@7540
   285
\              <= NbT + sum (%i. (tokens o rel o sub i o client)s) Nclients}";
paulson@7540
   286
by (rtac (Always_Int_rule [System_sum_bounded, System_Always_giv_le_allocGiv, 
paulson@7540
   287
			   System_Always_allocRel_le_rel] RS Always_weaken) 1);
paulson@7538
   288
by Auto_tac;
paulson@7540
   289
by (rtac (sum_mono RS order_trans) 1);
paulson@7540
   290
by (dtac order_trans 2);
paulson@7540
   291
by (rtac ([order_refl, sum_mono] MRS add_le_mono) 2);
paulson@7540
   292
by (assume_tac 3);
paulson@7482
   293
by Auto_tac;
paulson@7540
   294
qed "System_safety";
paulson@7399
   295