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