1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT LIBRARY COMPONENTS                          --
4--                                                                          --
5--    A D A . C O N T A I N E R S . F O R M A L _ H A S H E D _ S E T S     --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2015, Free Software Foundation, Inc.         --
10--                                                                          --
11-- This specification is derived from the Ada Reference Manual for use with --
12-- GNAT. The copyright notice above, and the license provisions that follow --
13-- apply solely to the  contents of the part following the private keyword. --
14--                                                                          --
15-- GNAT is free software;  you can  redistribute it  and/or modify it under --
16-- terms of the  GNU General Public License as published  by the Free Soft- --
17-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
18-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
21--                                                                          --
22-- As a special exception under Section 7 of GPL version 3, you are granted --
23-- additional permissions described in the GCC Runtime Library Exception,   --
24-- version 3.1, as published by the Free Software Foundation.               --
25--                                                                          --
26-- You should have received a copy of the GNU General Public License and    --
27-- a copy of the GCC Runtime Library Exception along with this program;     --
28-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
29-- <http://www.gnu.org/licenses/>.                                          --
30------------------------------------------------------------------------------
31
32--  This spec is derived from package Ada.Containers.Bounded_Hashed_Sets in the
33--  Ada 2012 RM. The modifications are meant to facilitate formal proofs by
34--  making it easier to express properties, and by making the specification of
35--  this unit compatible with SPARK 2014. Note that the API of this unit may be
36--  subject to incompatible changes as SPARK 2014 evolves.
37
38--  The modifications are:
39
40--    A parameter for the container is added to every function reading the
41--    content of a container: Element, Next, Query_Element, Has_Element, Key,
42--    Iterate, Equivalent_Elements. This change is motivated by the need to
43--    have cursors which are valid on different containers (typically a
44--    container C and its previous version C'Old) for expressing properties,
45--    which is not possible if cursors encapsulate an access to the underlying
46--    container.
47
48--    There are three new functions:
49
50--      function Strict_Equal (Left, Right : Set) return Boolean;
51--      function First_To_Previous  (Container : Set; Current : Cursor)
52--         return Set;
53--      function Current_To_Last (Container : Set; Current : Cursor)
54--         return Set;
55
56--    See detailed specifications for these subprograms
57
58private with Ada.Containers.Hash_Tables;
59
60generic
61   type Element_Type is private;
62
63   with function Hash (Element : Element_Type) return Hash_Type;
64
65   with function Equivalent_Elements (Left, Right : Element_Type)
66                                      return Boolean;
67
68   with function "=" (Left, Right : Element_Type) return Boolean is <>;
69
70package Ada.Containers.Formal_Hashed_Sets with
71  Pure,
72  SPARK_Mode
73is
74   pragma Annotate (GNATprove, External_Axiomatization);
75
76   type Set (Capacity : Count_Type; Modulus : Hash_Type) is private with
77     Iterable => (First       => First,
78                  Next        => Next,
79                  Has_Element => Has_Element,
80                  Element     => Element),
81     Default_Initial_Condition => Is_Empty (Set);
82   pragma Preelaborable_Initialization (Set);
83
84   type Cursor is private;
85   pragma Preelaborable_Initialization (Cursor);
86
87   Empty_Set : constant Set;
88
89   No_Element : constant Cursor;
90
91   function "=" (Left, Right : Set) return Boolean with
92     Global => null;
93
94   function Equivalent_Sets (Left, Right : Set) return Boolean with
95     Global => null;
96
97   function To_Set (New_Item : Element_Type) return Set with
98     Global => null;
99
100   function Capacity (Container : Set) return Count_Type with
101     Global => null;
102
103   procedure Reserve_Capacity
104     (Container : in out Set;
105      Capacity  : Count_Type)
106   with
107     Global => null,
108     Pre    => Capacity <= Container.Capacity;
109
110   function Length (Container : Set) return Count_Type with
111     Global => null;
112
113   function Is_Empty (Container : Set) return Boolean with
114     Global => null;
115
116   procedure Clear (Container : in out Set) with
117     Global => null;
118
119   procedure Assign (Target : in out Set; Source : Set) with
120     Global => null,
121     Pre    => Target.Capacity >= Length (Source);
122
123   function Copy
124     (Source   : Set;
125      Capacity : Count_Type := 0) return Set
126   with
127     Global => null,
128     Pre    => Capacity = 0 or else Capacity >= Source.Capacity;
129
130   function Element
131     (Container : Set;
132      Position  : Cursor) return Element_Type
133   with
134     Global => null,
135     Pre    => Has_Element (Container, Position);
136
137   procedure Replace_Element
138     (Container : in out Set;
139      Position  : Cursor;
140      New_Item  : Element_Type)
141   with
142     Global => null,
143     Pre    => Has_Element (Container, Position);
144
145   procedure Move (Target : in out Set; Source : in out Set) with
146     Global => null,
147     Pre    => Target.Capacity >= Length (Source);
148
149   procedure Insert
150     (Container : in out Set;
151      New_Item  : Element_Type;
152      Position  : out Cursor;
153      Inserted  : out Boolean)
154   with
155     Global => null,
156     Pre    => Length (Container) < Container.Capacity;
157
158   procedure Insert  (Container : in out Set; New_Item : Element_Type) with
159     Global => null,
160     Pre    => Length (Container) < Container.Capacity
161                 and then (not Contains (Container, New_Item));
162
163   procedure Include (Container : in out Set; New_Item : Element_Type) with
164     Global => null,
165     Pre    => Length (Container) < Container.Capacity;
166
167   procedure Replace (Container : in out Set; New_Item : Element_Type) with
168     Global => null,
169     Pre    => Contains (Container, New_Item);
170
171   procedure Exclude (Container : in out Set; Item     : Element_Type) with
172     Global => null;
173
174   procedure Delete  (Container : in out Set; Item     : Element_Type) with
175     Global => null,
176     Pre    => Contains (Container, Item);
177
178   procedure Delete (Container : in out Set; Position  : in out Cursor) with
179     Global => null,
180     Pre    => Has_Element (Container, Position);
181
182   procedure Union (Target : in out Set; Source : Set) with
183     Global => null,
184     Pre    => Length (Target) + Length (Source) -
185                 Length (Intersection (Target, Source)) <= Target.Capacity;
186
187   function Union (Left, Right : Set) return Set with
188     Global => null,
189     Pre    => Length (Left) + Length (Right) -
190                 Length (Intersection (Left, Right)) <= Count_Type'Last;
191
192   function "or" (Left, Right : Set) return Set renames Union;
193
194   procedure Intersection (Target : in out Set; Source : Set) with
195     Global => null;
196
197   function Intersection (Left, Right : Set) return Set with
198     Global => null;
199
200   function "and" (Left, Right : Set) return Set renames Intersection;
201
202   procedure Difference (Target : in out Set; Source : Set) with
203     Global => null;
204
205   function Difference (Left, Right : Set) return Set with
206     Global => null;
207
208   function "-" (Left, Right : Set) return Set renames Difference;
209
210   procedure Symmetric_Difference (Target : in out Set; Source : Set) with
211     Global => null,
212     Pre    => Length (Target) + Length (Source) -
213                 2 * Length (Intersection (Target, Source)) <= Target.Capacity;
214
215   function Symmetric_Difference (Left, Right : Set) return Set with
216     Global => null,
217     Pre    => Length (Left) + Length (Right) -
218                 2 * Length (Intersection (Left, Right)) <= Count_Type'Last;
219
220   function "xor" (Left, Right : Set) return Set
221     renames Symmetric_Difference;
222
223   function Overlap (Left, Right : Set) return Boolean with
224     Global => null;
225
226   function Is_Subset (Subset : Set; Of_Set : Set) return Boolean with
227     Global => null;
228
229   function First (Container : Set) return Cursor with
230     Global => null;
231
232   function Next (Container : Set; Position : Cursor) return Cursor with
233     Global => null,
234     Pre    => Has_Element (Container, Position) or else Position = No_Element;
235
236   procedure Next (Container : Set; Position : in out Cursor) with
237     Global => null,
238     Pre    => Has_Element (Container, Position) or else Position = No_Element;
239
240   function Find
241     (Container : Set;
242      Item      : Element_Type) return Cursor
243   with
244     Global => null;
245
246   function Contains (Container : Set; Item : Element_Type) return Boolean with
247     Global => null;
248
249   function Has_Element (Container : Set; Position : Cursor) return Boolean
250   with
251     Global => null;
252
253   function Equivalent_Elements (Left  : Set; CLeft : Cursor;
254                                 Right : Set; CRight : Cursor) return Boolean
255   with
256     Global => null;
257
258   function Equivalent_Elements
259     (Left  : Set; CLeft : Cursor;
260      Right : Element_Type) return Boolean
261   with
262     Global => null;
263
264   function Equivalent_Elements
265     (Left  : Element_Type;
266      Right : Set; CRight : Cursor) return Boolean
267   with
268     Global => null;
269
270   function Default_Modulus (Capacity : Count_Type) return Hash_Type with
271     Global => null;
272
273   generic
274      type Key_Type (<>) is private;
275
276      with function Key (Element : Element_Type) return Key_Type;
277
278      with function Hash (Key : Key_Type) return Hash_Type;
279
280      with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
281
282   package Generic_Keys is
283
284      function Key (Container : Set; Position : Cursor) return Key_Type with
285        Global => null;
286
287      function Element (Container : Set; Key : Key_Type) return Element_Type
288      with
289          Global => null;
290
291      procedure Replace
292        (Container : in out Set;
293         Key       : Key_Type;
294         New_Item  : Element_Type)
295      with
296          Global => null;
297
298      procedure Exclude (Container : in out Set; Key : Key_Type) with
299        Global => null;
300
301      procedure Delete (Container : in out Set; Key : Key_Type) with
302        Global => null;
303
304      function Find (Container : Set; Key : Key_Type) return Cursor with
305        Global => null;
306
307      function Contains (Container : Set; Key : Key_Type) return Boolean with
308        Global => null;
309
310   end Generic_Keys;
311
312   function Strict_Equal (Left, Right : Set) return Boolean with
313     Ghost,
314     Global => null;
315   --  Strict_Equal returns True if the containers are physically equal, i.e.
316   --  they are structurally equal (function "=" returns True) and that they
317   --  have the same set of cursors.
318
319   function First_To_Previous  (Container : Set; Current : Cursor) return Set
320   with
321     Ghost,
322     Global => null,
323     Pre    => Has_Element (Container, Current) or else Current = No_Element;
324
325   function Current_To_Last (Container : Set; Current : Cursor) return Set
326   with
327     Ghost,
328     Global => null,
329     Pre    => Has_Element (Container, Current) or else Current = No_Element;
330   --  First_To_Previous returns a container containing all elements preceding
331   --  Current (excluded) in Container. Current_To_Last returns a container
332   --  containing all elements following Current (included) in Container.
333   --  These two new functions can be used to express invariant properties in
334   --  loops which iterate over containers. First_To_Previous returns the part
335   --  of the container already scanned and Current_To_Last the part not
336   --  scanned yet.
337
338private
339   pragma SPARK_Mode (Off);
340
341   pragma Inline (Next);
342
343   type Node_Type is
344      record
345         Element     : Element_Type;
346         Next        : Count_Type;
347         Has_Element : Boolean := False;
348      end record;
349
350   package HT_Types is new
351     Ada.Containers.Hash_Tables.Generic_Bounded_Hash_Table_Types (Node_Type);
352
353   type Set (Capacity : Count_Type; Modulus : Hash_Type) is
354     new HT_Types.Hash_Table_Type (Capacity, Modulus) with null record;
355
356   use HT_Types;
357
358   type Cursor is record
359      Node : Count_Type;
360   end record;
361
362   No_Element : constant Cursor := (Node => 0);
363
364   Empty_Set : constant Set := (Capacity => 0, Modulus => 0, others => <>);
365
366end Ada.Containers.Formal_Hashed_Sets;
367