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 _ O R D E R E D _ S E T S    --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2013, 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_Ordered_Sets in
33--  the 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: Key, Element, Next, Query_Element, Previous,
42--    Has_Element, Iterate, Reverse_Iterate. This change is motivated by the
43--    need to have cursors which are valid on different containers (typically
44--    a 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. The operators "<" and ">" that could not be modified that way
47--    have been removed.
48
49--    There are three new functions:
50
51--      function Strict_Equal (Left, Right : Set) return Boolean;
52--      function First_To_Previous (Container : Set; Current : Cursor)
53--         return Set;
54--      function Current_To_Last (Container : Set; Current : Cursor)
55--         return Set;
56
57--    See detailed specifications for these subprograms
58
59private with Ada.Containers.Red_Black_Trees;
60
61generic
62   type Element_Type is private;
63
64   with function "<" (Left, Right : Element_Type) return Boolean is <>;
65   with function "=" (Left, Right : Element_Type) return Boolean is <>;
66
67package Ada.Containers.Formal_Ordered_Sets is
68   pragma Annotate (GNATprove, External_Axiomatization);
69   pragma Pure;
70
71   function Equivalent_Elements (Left, Right : Element_Type) return Boolean
72   with
73     Global => null;
74
75   type Set (Capacity : Count_Type) is private with
76     Iterable => (First       => First,
77                  Next        => Next,
78                  Has_Element => Has_Element,
79                  Element     => Element);
80   pragma Preelaborable_Initialization (Set);
81
82   type Cursor is private;
83   pragma Preelaborable_Initialization (Cursor);
84
85   Empty_Set : constant Set;
86
87   No_Element : constant Cursor;
88
89   function "=" (Left, Right : Set) return Boolean with
90     Global => null;
91
92   function Equivalent_Sets (Left, Right : Set) return Boolean with
93     Global => null;
94
95   function To_Set (New_Item : Element_Type) return Set with
96     Global => null;
97
98   function Length (Container : Set) return Count_Type with
99     Global => null;
100
101   function Is_Empty (Container : Set) return Boolean with
102     Global => null;
103
104   procedure Clear (Container : in out Set) with
105     Global => null;
106
107   procedure Assign (Target : in out Set; Source : Set) with
108     Pre => Target.Capacity >= Length (Source);
109
110   function Copy (Source : Set; Capacity : Count_Type := 0) return Set with
111     Global => null,
112     Pre    => Capacity = 0 or else Capacity >= Source.Capacity;
113
114   function Element
115     (Container : Set;
116      Position  : Cursor) return Element_Type
117   with
118     Global => null,
119     Pre    => Has_Element (Container, Position);
120
121   procedure Replace_Element
122     (Container : in out Set;
123      Position  : Cursor;
124      New_Item  : Element_Type)
125   with
126     Global => null,
127     Pre    => Has_Element (Container, Position);
128
129   procedure Move (Target : in out Set; Source : in out Set) with
130     Global => null,
131     Pre    => Target.Capacity >= Length (Source);
132
133   procedure Insert
134     (Container : in out Set;
135      New_Item  : Element_Type;
136      Position  : out Cursor;
137      Inserted  : out Boolean)
138   with
139     Global => null,
140     Pre    => Length (Container) < Container.Capacity;
141
142   procedure Insert
143     (Container : in out Set;
144      New_Item  : Element_Type)
145   with
146     Global => null,
147     Pre    => Length (Container) < Container.Capacity
148                 and then (not Contains (Container, New_Item));
149
150   procedure Include
151     (Container : in out Set;
152      New_Item  : Element_Type)
153   with
154     Global => null,
155     Pre    => Length (Container) < Container.Capacity;
156
157   procedure Replace
158     (Container : in out Set;
159      New_Item  : Element_Type)
160   with
161     Global => null,
162     Pre    => Contains (Container, New_Item);
163
164   procedure Exclude
165     (Container : in out Set;
166      Item      : Element_Type)
167   with
168     Global => null;
169
170   procedure Delete
171     (Container : in out Set;
172      Item      : Element_Type)
173   with
174     Global => null,
175     Pre    => Contains (Container, Item);
176
177   procedure Delete
178     (Container : in out Set;
179      Position  : in out Cursor)
180   with
181     Global => null,
182     Pre    => Has_Element (Container, Position);
183
184   procedure Delete_First (Container : in out Set) with
185     Global => null;
186
187   procedure Delete_Last (Container : in out Set) with
188     Global => null;
189
190   procedure Union (Target : in out Set; Source : Set) with
191     Global => null,
192     Pre    => Length (Target) + Length (Source) -
193                 Length (Intersection (Target, Source)) <= Target.Capacity;
194
195   function Union (Left, Right : Set) return Set with
196     Global => null,
197     Pre    => Length (Left) + Length (Right) -
198                 Length (Intersection (Left, Right)) <= Count_Type'Last;
199
200   function "or" (Left, Right : Set) return Set renames Union;
201
202   procedure Intersection (Target : in out Set; Source : Set) with
203     Global => null;
204
205   function Intersection (Left, Right : Set) return Set with
206     Global => null;
207
208   function "and" (Left, Right : Set) return Set renames Intersection;
209
210   procedure Difference (Target : in out Set; Source : Set) with
211     Global => null;
212
213   function Difference (Left, Right : Set) return Set with
214     Global => null;
215
216   function "-" (Left, Right : Set) return Set renames Difference;
217
218   procedure Symmetric_Difference (Target : in out Set; Source : Set) with
219     Global => null,
220     Pre    => Length (Target) + Length (Source) -
221                 2 * Length (Intersection (Target, Source)) <= Target.Capacity;
222
223   function Symmetric_Difference (Left, Right : Set) return Set with
224     Global => null,
225     Pre    => Length (Left) + Length (Right) -
226                 2 * Length (Intersection (Left, Right)) <= Count_Type'Last;
227
228   function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
229
230   function Overlap (Left, Right : Set) return Boolean with
231     Global => null;
232
233   function Is_Subset (Subset : Set; Of_Set : Set) return Boolean with
234     Global => null;
235
236   function First (Container : Set) return Cursor with
237     Global => null;
238
239   function First_Element (Container : Set) return Element_Type with
240     Global => null,
241     Pre    => not Is_Empty (Container);
242
243   function Last (Container : Set) return Cursor;
244
245   function Last_Element (Container : Set) return Element_Type with
246     Global => null,
247     Pre    => not Is_Empty (Container);
248
249   function Next (Container : Set; Position : Cursor) return Cursor with
250     Global => null,
251     Pre    => Has_Element (Container, Position) or else Position = No_Element;
252
253   procedure Next (Container : Set; Position : in out Cursor) with
254     Global => null,
255     Pre    => Has_Element (Container, Position) or else Position = No_Element;
256
257   function Previous (Container : Set; Position : Cursor) return Cursor with
258     Global => null,
259     Pre    => Has_Element (Container, Position) or else Position = No_Element;
260
261   procedure Previous (Container : Set; Position : in out Cursor) with
262     Global => null,
263     Pre    => Has_Element (Container, Position) or else Position = No_Element;
264
265   function Find (Container : Set; Item : Element_Type) return Cursor with
266     Global => null;
267
268   function Floor (Container : Set; Item : Element_Type) return Cursor with
269     Global => null;
270
271   function Ceiling (Container : Set; Item : Element_Type) return Cursor with
272     Global => null;
273
274   function Contains (Container : Set; Item : Element_Type) return Boolean with
275     Global => null;
276
277   function Has_Element (Container : Set; Position : Cursor) return Boolean
278   with
279     Global => null;
280
281   generic
282      type Key_Type (<>) is private;
283
284      with function Key (Element : Element_Type) return Key_Type;
285
286      with function "<" (Left, Right : Key_Type) return Boolean is <>;
287
288   package Generic_Keys is
289
290      function Equivalent_Keys (Left, Right : Key_Type) return Boolean with
291        Global => null;
292
293      function Key (Container : Set; Position : Cursor) return Key_Type with
294        Global => null;
295
296      function Element (Container : Set; Key : Key_Type) return Element_Type
297      with
298        Global => null;
299
300      procedure Replace
301        (Container : in out Set;
302         Key       : Key_Type;
303         New_Item  : Element_Type)
304      with
305        Global => null;
306
307      procedure Exclude (Container : in out Set; Key : Key_Type) with
308        Global => null;
309
310      procedure Delete (Container : in out Set; Key : Key_Type) with
311        Global => null;
312
313      function Find (Container : Set; Key : Key_Type) return Cursor with
314        Global => null;
315
316      function Floor (Container : Set; Key : Key_Type) return Cursor with
317        Global => null;
318
319      function Ceiling (Container : Set; Key : Key_Type) return Cursor with
320        Global => null;
321
322      function Contains (Container : Set; Key : Key_Type) return Boolean with
323        Global => null;
324
325   end Generic_Keys;
326
327   function Strict_Equal (Left, Right : Set) return Boolean with
328        Global => null;
329   --  Strict_Equal returns True if the containers are physically equal, i.e.
330   --  they are structurally equal (function "=" returns True) and that they
331   --  have the same set of cursors.
332
333   function First_To_Previous (Container : Set; Current : Cursor) return Set
334   with
335     Global => null,
336     Pre    => Has_Element (Container, Current) or else Current = No_Element;
337   function Current_To_Last (Container : Set; Current : Cursor) return Set
338   with
339     Global => null,
340     Pre    => Has_Element (Container, Current) or else Current = No_Element;
341   --  First_To_Previous returns a container containing all elements preceding
342   --  Current (excluded) in Container. Current_To_Last returns a container
343   --  containing all elements following Current (included) in Container.
344   --  These two new functions can be used to express invariant properties in
345   --  loops which iterate over containers. First_To_Previous returns the part
346   --  of the container already scanned and Current_To_Last the part not
347   --  scanned yet.
348
349private
350
351   pragma Inline (Next);
352   pragma Inline (Previous);
353
354   type Node_Type is record
355      Has_Element : Boolean := False;
356      Parent  : Count_Type := 0;
357      Left    : Count_Type := 0;
358      Right   : Count_Type := 0;
359      Color   : Red_Black_Trees.Color_Type;
360      Element : Element_Type;
361   end record;
362
363   package Tree_Types is
364     new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
365
366   type Set (Capacity : Count_Type) is
367     new Tree_Types.Tree_Type (Capacity) with null record;
368
369   use Red_Black_Trees;
370
371   type Cursor is record
372      Node : Count_Type;
373   end record;
374
375   No_Element : constant Cursor := (Node => 0);
376
377   Empty_Set : constant Set := (Capacity => 0, others => <>);
378
379end Ada.Containers.Formal_Ordered_Sets;
380