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