| 
6815
 | 
     1  | 
(*  Title:      HOL/UNITY/Alloc
  | 
| 
 | 
     2  | 
    ID:         $Id$
  | 
| 
 | 
     3  | 
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
  | 
| 
 | 
     4  | 
    Copyright   1998  University of Cambridge
  | 
| 
 | 
     5  | 
  | 
| 
 | 
     6  | 
Specification of Chandy and Charpentier's Allocator
  | 
| 
 | 
     7  | 
*)
  | 
| 
 | 
     8  | 
  | 
| 
6827
 | 
     9  | 
Alloc = Follows + Extend + PPROD +
  | 
| 
6815
 | 
    10  | 
  | 
| 
 | 
    11  | 
(*Duplicated from HOL/ex/NatSum.thy.
  | 
| 
 | 
    12  | 
  Maybe type should be  [nat=>int, nat] => int**)
  | 
| 
 | 
    13  | 
consts sum     :: [nat=>nat, nat] => nat
  | 
| 
 | 
    14  | 
primrec 
  | 
| 
 | 
    15  | 
  "sum f 0 = 0"
  | 
| 
 | 
    16  | 
  "sum f (Suc n) = f(n) + sum f n"
  | 
| 
 | 
    17  | 
  | 
| 
 | 
    18  | 
  | 
| 
 | 
    19  | 
(*This function merely sums the elements of a list*)
  | 
| 
 | 
    20  | 
consts tokens     :: nat list => nat
  | 
| 
 | 
    21  | 
primrec 
  | 
| 
 | 
    22  | 
  "tokens [] = 0"
  | 
| 
 | 
    23  | 
  "tokens (x#xs) = x + tokens xs"
  | 
| 
 | 
    24  | 
  | 
| 
 | 
    25  | 
  | 
| 
 | 
    26  | 
consts
  | 
| 
 | 
    27  | 
  NbT      :: nat       (*Number of tokens in system*)
  | 
| 
 | 
    28  | 
  Nclients :: nat       (*Number of clients*)
  | 
| 
 | 
    29  | 
  | 
| 
 | 
    30  | 
  | 
| 
 | 
    31  | 
record clientState =
  | 
| 
 | 
    32  | 
  giv :: nat list   (*client's INPUT history:  tokens GRANTED*)
  | 
| 
 | 
    33  | 
  ask :: nat list   (*client's OUTPUT history: tokens REQUESTED*)
  | 
| 
 | 
    34  | 
  rel :: nat list   (*client's OUTPUT history: tokens RELEASED*)
  | 
| 
 | 
    35  | 
  | 
| 
 | 
    36  | 
record allocState =
  | 
| 
 | 
    37  | 
  allocGiv :: nat => nat list   (*allocator's local copy of "giv" for i*)
  | 
| 
 | 
    38  | 
  allocAsk :: nat => nat list   (*allocator's local copy of "ask" for i*)
  | 
| 
 | 
    39  | 
  allocRel :: nat => nat list   (*allocator's local copy of "rel" for i*)
  | 
| 
 | 
    40  | 
  | 
| 
 | 
    41  | 
record systemState = allocState +
  | 
| 
 | 
    42  | 
  client :: nat => clientState  (*states of all clients*)
  | 
| 
 | 
    43  | 
  | 
| 
 | 
    44  | 
  | 
| 
 | 
    45  | 
constdefs
  | 
| 
 | 
    46  | 
  | 
| 
 | 
    47  | 
(** Resource allocation system specification **)
  | 
| 
 | 
    48  | 
  | 
| 
 | 
    49  | 
  (*spec (1)*)
  | 
| 
 | 
    50  | 
  system_safety :: systemState program set
  | 
| 
 | 
    51  | 
    "system_safety ==
  | 
| 
 | 
    52  | 
     Always {s. sum (%i. (tokens o giv o sub i o client) s) Nclients
 | 
| 
 | 
    53  | 
	        <= NbT + sum (%i. (tokens o rel o sub i o client) s) Nclients}"
  | 
| 
 | 
    54  | 
  | 
| 
 | 
    55  | 
  (*spec (2)*)
  | 
| 
 | 
    56  | 
  system_progress :: systemState program set
  | 
| 
 | 
    57  | 
    "system_progress == INT i : lessThan Nclients.
  | 
| 
 | 
    58  | 
			INT h. 
  | 
| 
 | 
    59  | 
			  {s. h <= (ask o sub i o client)s} LeadsTo
 | 
| 
6827
 | 
    60  | 
			  {s. h pfixLe (giv o sub i o client) s}"
 | 
| 
 | 
    61  | 
  | 
| 
 | 
    62  | 
  system_spec :: systemState program set
  | 
| 
 | 
    63  | 
    "system_spec == system_safety Int system_progress"
  | 
| 
6815
 | 
    64  | 
  | 
| 
 | 
    65  | 
(** Client specification (required) ***)
  | 
| 
 | 
    66  | 
  | 
| 
7347
 | 
    67  | 
  (*spec (3) PROBABLY REQUIRES A LOCALTO PRECONDITION*)
  | 
| 
6815
 | 
    68  | 
  client_increasing :: clientState program set
  | 
| 
 | 
    69  | 
    "client_increasing ==
  | 
| 
7361
 | 
    70  | 
         UNIV guarantees Increasing ask Int Increasing rel"
  | 
| 
6815
 | 
    71  | 
  | 
| 
 | 
    72  | 
  (*spec (4)*)
  | 
| 
 | 
    73  | 
  client_bounded :: clientState program set
  | 
| 
 | 
    74  | 
    "client_bounded ==
  | 
| 
7361
 | 
    75  | 
         UNIV guarantees Always {s. ALL elt : set (ask s). elt <= NbT}"
 | 
| 
6815
 | 
    76  | 
  | 
| 
 | 
    77  | 
  (*spec (5)*)
  | 
| 
 | 
    78  | 
  client_progress :: clientState program set
  | 
| 
 | 
    79  | 
    "client_progress ==
  | 
| 
 | 
    80  | 
	 Increasing giv
  | 
| 
7361
 | 
    81  | 
	 guarantees
  | 
| 
6827
 | 
    82  | 
	 (INT h. {s. h <= giv s & h pfixGe ask s}
 | 
| 
6815
 | 
    83  | 
		 LeadsTo
  | 
| 
 | 
    84  | 
		 {s. tokens h <= (tokens o rel) s})"
 | 
| 
 | 
    85  | 
  | 
| 
6827
 | 
    86  | 
  client_spec :: clientState program set
  | 
| 
 | 
    87  | 
    "client_spec == client_increasing Int client_bounded Int client_progress"
  | 
| 
 | 
    88  | 
  | 
| 
6815
 | 
    89  | 
(** Allocator specification (required) ***)
  | 
| 
 | 
    90  | 
  | 
| 
7347
 | 
    91  | 
  (*spec (6)  PROBABLY REQUIRES A LOCALTO PRECONDITION*)
  | 
| 
6815
 | 
    92  | 
  alloc_increasing :: allocState program set
  | 
| 
 | 
    93  | 
    "alloc_increasing ==
  | 
| 
 | 
    94  | 
	 UNIV
  | 
| 
7361
 | 
    95  | 
         guarantees
  | 
| 
6815
 | 
    96  | 
	 (INT i : lessThan Nclients. Increasing (sub i o allocAsk))"
  | 
| 
 | 
    97  | 
  | 
| 
 | 
    98  | 
  (*spec (7)*)
  | 
| 
 | 
    99  | 
  alloc_safety :: allocState program set
  | 
| 
 | 
   100  | 
    "alloc_safety ==
  | 
| 
 | 
   101  | 
	 (INT i : lessThan Nclients. Increasing (sub i o allocRel))
  | 
| 
7361
 | 
   102  | 
         guarantees
  | 
| 
6815
 | 
   103  | 
	 Always {s. sum (%i. (tokens o sub i o allocGiv) s) Nclients
 | 
| 
 | 
   104  | 
	         <= NbT + sum (%i. (tokens o sub i o allocRel) s) Nclients}"
  | 
| 
 | 
   105  | 
  | 
| 
 | 
   106  | 
  (*spec (8)*)
  | 
| 
 | 
   107  | 
  alloc_progress :: allocState program set
  | 
| 
 | 
   108  | 
    "alloc_progress ==
  | 
| 
 | 
   109  | 
	 (INT i : lessThan Nclients. Increasing (sub i o allocAsk) Int
  | 
| 
 | 
   110  | 
	                             Increasing (sub i o allocRel))
  | 
| 
 | 
   111  | 
         Int
  | 
| 
 | 
   112  | 
         Always {s. ALL i : lessThan Nclients.
 | 
| 
 | 
   113  | 
		    ALL k : lessThan (length (allocAsk s i)).
  | 
| 
 | 
   114  | 
		    allocAsk s i ! k <= NbT}
  | 
| 
 | 
   115  | 
         Int
  | 
| 
 | 
   116  | 
         (INT i : lessThan Nclients. 
  | 
| 
6827
 | 
   117  | 
	  INT h. {s. h <= (sub i o allocGiv)s & h pfixGe (sub i o allocAsk)s}
 | 
| 
6815
 | 
   118  | 
		  LeadsTo {s. tokens h <= (tokens o sub i o allocRel)s})
 | 
| 
7361
 | 
   119  | 
         guarantees
  | 
| 
6815
 | 
   120  | 
	     (INT i : lessThan Nclients.
  | 
| 
 | 
   121  | 
	      INT h. {s. h <= (sub i o allocAsk) s} LeadsTo
 | 
| 
6827
 | 
   122  | 
	             {s. h pfixLe (sub i o allocGiv) s})"
 | 
| 
 | 
   123  | 
  | 
| 
 | 
   124  | 
  alloc_spec :: allocState program set
  | 
| 
 | 
   125  | 
    "alloc_spec == alloc_increasing Int alloc_safety Int alloc_progress"
  | 
| 
6815
 | 
   126  | 
  | 
| 
 | 
   127  | 
(** Network specification ***)
  | 
| 
 | 
   128  | 
  | 
| 
 | 
   129  | 
  (*spec (9.1)*)
  | 
| 
 | 
   130  | 
  network_ask :: systemState program set
  | 
| 
 | 
   131  | 
    "network_ask == INT i : lessThan Nclients.
  | 
| 
6827
 | 
   132  | 
                    Increasing (ask o sub i o client)
  | 
| 
7361
 | 
   133  | 
                    guarantees
  | 
| 
6815
 | 
   134  | 
                    ((sub i o allocAsk) Fols (ask o sub i o client))"
  | 
| 
 | 
   135  | 
  | 
| 
 | 
   136  | 
  (*spec (9.2)*)
  | 
| 
 | 
   137  | 
  network_giv :: systemState program set
  | 
| 
 | 
   138  | 
    "network_giv == INT i : lessThan Nclients.
  | 
| 
6827
 | 
   139  | 
                    Increasing (sub i o allocGiv)
  | 
| 
7361
 | 
   140  | 
                    guarantees
  | 
| 
6815
 | 
   141  | 
                    ((giv o sub i o client) Fols (sub i o allocGiv))"
  | 
| 
 | 
   142  | 
  | 
| 
 | 
   143  | 
  (*spec (9.3)*)
  | 
| 
 | 
   144  | 
  network_rel :: systemState program set
  | 
| 
 | 
   145  | 
    "network_rel == INT i : lessThan Nclients.
  | 
| 
6827
 | 
   146  | 
                    Increasing (rel o sub i o client)
  | 
| 
7361
 | 
   147  | 
                    guarantees
  | 
| 
6815
 | 
   148  | 
                    ((sub i o allocRel) Fols (rel o sub i o client))"
  | 
| 
 | 
   149  | 
  | 
| 
6827
 | 
   150  | 
  network_spec :: systemState program set
  | 
| 
 | 
   151  | 
    "network_spec == network_ask Int network_giv Int network_rel"
  | 
| 
 | 
   152  | 
  | 
| 
 | 
   153  | 
  | 
| 
 | 
   154  | 
(** State mappings **)
  | 
| 
 | 
   155  | 
  sysOfAlloc :: "(allocState * (nat => clientState)) => systemState"
  | 
| 
 | 
   156  | 
    "sysOfAlloc == %(s,y). (| allocGiv = allocGiv s,
  | 
| 
 | 
   157  | 
			      allocAsk = allocAsk s,
  | 
| 
 | 
   158  | 
			      allocRel = allocRel s,
  | 
| 
 | 
   159  | 
			      client   = y |)"
  | 
| 
6841
 | 
   160  | 
(***    "sysOfAlloc == %(s,y). s(|client := y|)"  TYPE DOESN'T CHANGE***)
  | 
| 
 | 
   161  | 
  | 
| 
6827
 | 
   162  | 
  | 
| 
 | 
   163  | 
  sysOfClient :: "((nat => clientState) * allocState ) => systemState"
  | 
| 
 | 
   164  | 
    "sysOfClient == %(x,y). sysOfAlloc(y,x)"
  | 
| 
 | 
   165  | 
  | 
| 
 | 
   166  | 
  | 
| 
 | 
   167  | 
locale System =
  | 
| 
 | 
   168  | 
  fixes 
  | 
| 
 | 
   169  | 
    Alloc   :: allocState program
  | 
| 
 | 
   170  | 
    Client  :: clientState program
  | 
| 
 | 
   171  | 
    Network :: systemState program
  | 
| 
 | 
   172  | 
    System  :: systemState program
  | 
| 
 | 
   173  | 
  
  | 
| 
 | 
   174  | 
  assumes
  | 
| 
 | 
   175  | 
    Alloc   "Alloc   : alloc_spec"
  | 
| 
 | 
   176  | 
    Client  "Client  : client_spec"
  | 
| 
 | 
   177  | 
    Network "Network : network_spec"
  | 
| 
 | 
   178  | 
  | 
| 
 | 
   179  | 
  defines
  | 
| 
 | 
   180  | 
    System_def
  | 
| 
 | 
   181  | 
      "System == (extend sysOfAlloc Alloc)
  | 
| 
 | 
   182  | 
                 Join
  | 
| 
6841
 | 
   183  | 
                 (extend sysOfClient (plam x: lessThan Nclients. Client))
  | 
| 
6827
 | 
   184  | 
                 Join
  | 
| 
 | 
   185  | 
                 Network"
  | 
| 
6815
 | 
   186  | 
end
  |