1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.FUNCTIONAL_VECTORS --
9 -- Copyright (C) 2016-2017, Free Software Foundation, Inc. --
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. --
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. --
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. --
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 ------------------------------------------------------------------------------
33 private with Ada.Containers.Functional_Base;
36 type Index_Type is (<>);
37 -- To avoid Constraint_Error being raised at run time, Index_Type'Base
38 -- should have at least one more element at the low end than Index_Type.
40 type Element_Type (<>) is private;
42 package Ada.Containers.Functional_Vectors with SPARK_Mode is
44 subtype Extended_Index is Index_Type'Base range
45 Index_Type'Pred (Index_Type'First) .. Index_Type'Last;
46 -- Index_Type with one more element at the low end of the range.
47 -- This type is never used but it forces GNATprove to check that there is
48 -- room for one more element at the low end of Index_Type.
50 type Sequence is private
51 with Default_Initial_Condition => Length (Sequence) = 0,
52 Iterable => (First => Iter_First,
53 Has_Element => Iter_Has_Element,
56 -- Sequences are empty when default initialized.
57 -- Quantification over sequences can be done using the regular
58 -- quantification over its range or directly on its elements with "for of".
60 -----------------------
61 -- Basic operations --
62 -----------------------
64 -- Sequences are axiomatized using Length and Get, providing respectively
65 -- the length of a sequence and an accessor to its Nth element:
67 function Length (Container : Sequence) return Count_Type with
68 -- Length of a sequence
72 (Index_Type'Pos (Index_Type'First) - 1) + Length'Result <=
73 Index_Type'Pos (Index_Type'Last);
76 (Container : Sequence;
77 Position : Extended_Index) return Element_Type
78 -- Access the Element at position Position in Container
82 Pre => Position in Index_Type'First .. Last (Container);
84 function Last (Container : Sequence) return Extended_Index with
85 -- Last index of a sequence
90 Index_Type'Val ((Index_Type'Pos (Index_Type'First) - 1) +
92 pragma Annotate (GNATprove, Inline_For_Proof, Last);
94 function First return Extended_Index is (Index_Type'First);
95 -- First index of a sequence
97 ------------------------
98 -- Property Functions --
99 ------------------------
101 function "=" (Left : Sequence; Right : Sequence) return Boolean with
102 -- Extensional equality over sequences
107 (Length (Left) = Length (Right)
108 and then (for all N in Index_Type'First .. Last (Left) =>
109 Get (Left, N) = Get (Right, N)));
110 pragma Annotate (GNATprove, Inline_For_Proof, "=");
112 function "<" (Left : Sequence; Right : Sequence) return Boolean with
113 -- Left is a strict subsequence of Right
118 (Length (Left) < Length (Right)
119 and then (for all N in Index_Type'First .. Last (Left) =>
120 Get (Left, N) = Get (Right, N)));
121 pragma Annotate (GNATprove, Inline_For_Proof, "<");
123 function "<=" (Left : Sequence; Right : Sequence) return Boolean with
124 -- Left is a subsequence of Right
129 (Length (Left) <= Length (Right)
130 and then (for all N in Index_Type'First .. Last (Left) =>
131 Get (Left, N) = Get (Right, N)));
132 pragma Annotate (GNATprove, Inline_For_Proof, "<=");
135 (Container : Sequence;
137 Lst : Extended_Index;
138 Item : Element_Type) return Boolean
139 -- Returns True if Item occurs in the range from Fst to Lst of Container
143 Pre => Lst <= Last (Container),
146 (for some I in Fst .. Lst => Get (Container, I) = Item);
147 pragma Annotate (GNATprove, Inline_For_Proof, Contains);
149 function Constant_Range
150 (Container : Sequence;
152 Lst : Extended_Index;
153 Item : Element_Type) return Boolean
154 -- Returns True if every element of the range from Fst to Lst of Container
159 Pre => Lst <= Last (Container),
161 Constant_Range'Result =
162 (for all I in Fst .. Lst => Get (Container, I) = Item);
163 pragma Annotate (GNATprove, Inline_For_Proof, Constant_Range);
165 function Equal_Except
168 Position : Index_Type) return Boolean
169 -- Returns True is Left and Right are the same except at position Position
173 Pre => Position <= Last (Left),
175 Equal_Except'Result =
176 (Length (Left) = Length (Right)
177 and then (for all I in Index_Type'First .. Last (Left) =>
178 (if I /= Position then Get (Left, I) = Get (Right, I))));
179 pragma Annotate (GNATprove, Inline_For_Proof, Equal_Except);
181 function Equal_Except
185 Y : Index_Type) return Boolean
186 -- Returns True is Left and Right are the same except at positions X and Y
190 Pre => X <= Last (Left) and Y <= Last (Left),
192 Equal_Except'Result =
193 (Length (Left) = Length (Right)
194 and then (for all I in Index_Type'First .. Last (Left) =>
195 (if I /= X and I /= Y then
196 Get (Left, I) = Get (Right, I))));
197 pragma Annotate (GNATprove, Inline_For_Proof, Equal_Except);
203 Lst : Extended_Index) return Boolean
204 -- Returns True if the ranges from Fst to Lst contain the same elements in
209 Pre => Lst <= Last (Left) and Lst <= Last (Right),
212 (for all I in Fst .. Lst => Get (Left, I) = Get (Right, I));
213 pragma Annotate (GNATprove, Inline_For_Proof, Range_Equal);
215 function Range_Shifted
219 Lst : Extended_Index;
220 Offset : Count_Type'Base) return Boolean
221 -- Returns True if the range from Fst to Lst in Left contains the same
222 -- elements as the range from Fst + Offset to Lst + Offset in Right.
229 Index_Type'Pos (Index_Type'First) - Index_Type'Pos (Fst) ..
230 (Index_Type'Pos (Index_Type'First) - 1) + Length (Right) -
231 Index_Type'Pos (Lst),
233 Range_Shifted'Result =
234 ((for all I in Fst .. Lst =>
236 Get (Right, Index_Type'Val (Index_Type'Pos (I) + Offset)))
238 (for all I in Index_Type'Val (Index_Type'Pos (Fst) + Offset) ..
239 Index_Type'Val (Index_Type'Pos (Lst) + Offset)
241 Get (Left, Index_Type'Val (Index_Type'Pos (I) - Offset)) =
243 pragma Annotate (GNATprove, Inline_For_Proof, Range_Shifted);
245 ----------------------------
246 -- Construction Functions --
247 ----------------------------
249 -- For better efficiency of both proofs and execution, avoid using
250 -- construction functions in annotations and rather use property functions.
253 (Container : Sequence;
254 Position : Index_Type;
255 New_Item : Element_Type) return Sequence
256 -- Returns a new sequence which contains the same elements as Container
257 -- except for the one at position Position which is replaced by New_Item.
261 Pre => Position in Index_Type'First .. Last (Container),
263 Get (Set'Result, Position) = New_Item
264 and then Equal_Except (Container, Set'Result, Position);
266 function Add (Container : Sequence; New_Item : Element_Type) return Sequence
267 -- Returns a new sequence which contains the same elements as Container
268 -- plus New_Item at the end.
273 Length (Container) < Count_Type'Last
274 and then Last (Container) < Index_Type'Last,
276 Length (Add'Result) = Length (Container) + 1
277 and then Get (Add'Result, Last (Add'Result)) = New_Item
278 and then Container <= Add'Result;
281 (Container : Sequence;
282 Position : Index_Type;
283 New_Item : Element_Type) return Sequence
285 -- Returns a new sequence which contains the same elements as Container
286 -- except that New_Item has been inserted at position Position.
290 Length (Container) < Count_Type'Last
291 and then Last (Container) < Index_Type'Last
292 and then Position <= Extended_Index'Succ (Last (Container)),
294 Length (Add'Result) = Length (Container) + 1
295 and then Get (Add'Result, Position) = New_Item
299 Fst => Index_Type'First,
300 Lst => Index_Type'Pred (Position))
301 and then Range_Shifted
305 Lst => Last (Container),
309 (Container : Sequence;
310 Position : Index_Type) return Sequence
311 -- Returns a new sequence which contains the same elements as Container
312 -- except that the element at position Position has been removed.
317 Length (Container) < Count_Type'Last
318 and Last (Container) < Index_Type'Last
319 and Position in Index_Type'First .. Last (Container),
321 Length (Remove'Result) = Length (Container) - 1
324 Right => Remove'Result,
325 Fst => Index_Type'First,
326 Lst => Index_Type'Pred (Position))
327 and then Range_Shifted
328 (Left => Remove'Result,
331 Lst => Last (Remove'Result),
334 ---------------------------
335 -- Iteration Primitives --
336 ---------------------------
338 function Iter_First (Container : Sequence) return Extended_Index with
341 function Iter_Has_Element
342 (Container : Sequence;
343 Position : Extended_Index) return Boolean
347 Iter_Has_Element'Result =
348 (Position in Index_Type'First .. Last (Container));
349 pragma Annotate (GNATprove, Inline_For_Proof, Iter_Has_Element);
352 (Container : Sequence;
353 Position : Extended_Index) return Extended_Index
356 Pre => Iter_Has_Element (Container, Position);
360 pragma SPARK_Mode (Off);
362 package Containers is new Ada.Containers.Functional_Base
363 (Index_Type => Index_Type,
364 Element_Type => Element_Type);
366 type Sequence is record
367 Content : Containers.Container;
370 function Iter_First (Container : Sequence) return Extended_Index is
374 (Container : Sequence;
375 Position : Extended_Index) return Extended_Index
377 (if Position = Extended_Index'Last then
380 Extended_Index'Succ (Position));
382 function Iter_Has_Element
383 (Container : Sequence;
384 Position : Extended_Index) return Boolean
386 (Position in Index_Type'First ..
388 ((Index_Type'Pos (Index_Type'First) - 1) + Length (Container))));
390 end Ada.Containers.Functional_Vectors;