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;
41 package Ada.Containers.Functional_Vectors with SPARK_Mode is
43 subtype Extended_Index is Index_Type'Base range
44 Index_Type'Pred (Index_Type'First) .. Index_Type'Last;
45 -- Index_Type with one more element at the low end of the range.
46 -- This type is never used but it forces GNATprove to check that there is
47 -- room for one more element at the low end of Index_Type.
49 type Sequence is private
50 with Default_Initial_Condition => Length (Sequence) = 0,
51 Iterable => (First => Iter_First,
52 Has_Element => Iter_Has_Element,
55 -- Sequences are empty when default initialized.
56 -- Quantification over sequences can be done using the regular
57 -- quantification over its range or directly on its elements with "for of".
59 -----------------------
60 -- Basic operations --
61 -----------------------
63 -- Sequences are axiomatized using Length and Get, providing respectively
64 -- the length of a sequence and an accessor to its Nth element:
66 function Length (Container : Sequence) return Count_Type with
67 -- Length of a sequence
71 (Index_Type'Pos (Index_Type'First) - 1) + Length'Result <=
72 Index_Type'Pos (Index_Type'Last);
75 (Container : Sequence;
76 Position : Extended_Index) return Element_Type
77 -- Access the Element at position Position in Container
81 Pre => Position in Index_Type'First .. Last (Container);
83 function Last (Container : Sequence) return Extended_Index with
84 -- Last index of a sequence
89 Index_Type'Val ((Index_Type'Pos (Index_Type'First) - 1)
90 + Length (Container));
91 pragma Annotate (GNATprove, Inline_For_Proof, Last);
93 function First return Extended_Index is (Index_Type'First);
94 -- First index of a sequence
96 ------------------------
97 -- Property Functions --
98 ------------------------
100 function "=" (Left : Sequence; Right : Sequence) return Boolean with
101 -- Extensional equality over sequences
106 (Length (Left) = Length (Right)
107 and then (for all N in Index_Type'First .. Last (Left) =>
108 Get (Left, N) = Get (Right, N)));
109 pragma Annotate (GNATprove, Inline_For_Proof, "=");
111 function "<" (Left : Sequence; Right : Sequence) return Boolean with
112 -- Left is a strict subsequence of Right
117 (Length (Left) < Length (Right)
118 and then (for all N in Index_Type'First .. Last (Left) =>
119 Get (Left, N) = Get (Right, N)));
120 pragma Annotate (GNATprove, Inline_For_Proof, "<");
122 function "<=" (Left : Sequence; Right : Sequence) return Boolean with
123 -- Left is a subsequence of Right
128 (Length (Left) <= Length (Right)
129 and then (for all N in Index_Type'First .. Last (Left) =>
130 Get (Left, N) = Get (Right, N)));
131 pragma Annotate (GNATprove, Inline_For_Proof, "<=");
134 (Container : Sequence;
136 Lst : Extended_Index;
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;
155 -- Returns True if every element of the range from Fst to Lst of Container
160 Pre => Lst <= Last (Container),
162 Constant_Range'Result =
163 (for all I in Fst .. Lst => Get (Container, I) = Item);
164 pragma Annotate (GNATprove, Inline_For_Proof, Constant_Range);
166 function Equal_Except
169 Position : Index_Type) return Boolean
170 -- Returns True is Left and Right are the same except at position Position
174 Pre => Position <= Last (Left),
176 Equal_Except'Result =
177 (Length (Left) = Length (Right)
178 and then (for all I in Index_Type'First .. Last (Left) =>
179 (if I /= Position then Get (Left, I) = Get (Right, I))));
180 pragma Annotate (GNATprove, Inline_For_Proof, Equal_Except);
182 function Equal_Except
185 X, 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 Get (Left, I) = Get (Right, I))));
196 pragma Annotate (GNATprove, Inline_For_Proof, Equal_Except);
202 Lst : Extended_Index) return Boolean
203 -- Returns True if the ranges from Fst to Lst contain the same elements in
208 Pre => Lst <= Last (Left) and Lst <= Last (Right),
211 (for all I in Fst .. Lst => Get (Left, I) = Get (Right, I));
212 pragma Annotate (GNATprove, Inline_For_Proof, Range_Equal);
214 function Range_Shifted
218 Lst : Extended_Index;
219 Offset : Count_Type'Base) return Boolean
220 -- Returns True if the range from Fst to Lst in Left contains the same
221 -- elements as the range from Fst + Offset to Lst + Offset in Right.
225 Pre => Lst <= Last (Left)
227 Index_Type'Pos (Index_Type'First) - Index_Type'Pos (Fst) ..
228 (Index_Type'Pos (Index_Type'First) - 1)
229 + Length (Right) - Index_Type'Pos (Lst),
231 Range_Shifted'Result =
232 ((for all I in Fst .. Lst =>
234 = Get (Right, Index_Type'Val (Index_Type'Pos (I) + Offset)))
236 (for all I in Index_Type'Val (Index_Type'Pos (Fst) + Offset) ..
237 Index_Type'Val (Index_Type'Pos (Lst) + Offset) =>
238 Get (Left, Index_Type'Val (Index_Type'Pos (I) - Offset))
240 pragma Annotate (GNATprove, Inline_For_Proof, Range_Shifted);
242 ----------------------------
243 -- Construction Functions --
244 ----------------------------
246 -- For better efficiency of both proofs and execution, avoid using
247 -- construction functions in annotations and rather use property functions.
250 (Container : Sequence;
251 Position : Index_Type;
252 New_Item : Element_Type) return Sequence
253 -- Returns a new sequence which contains the same elements as Container
254 -- except for the one at position Position which is replaced by New_Item.
258 Pre => Position in Index_Type'First .. Last (Container),
259 Post => Get (Set'Result, Position) = New_Item
260 and then Equal_Except (Container, Set'Result, Position);
262 function Add (Container : Sequence; New_Item : Element_Type) return Sequence
263 -- Returns a new sequence which contains the same elements as Container
264 -- plus New_Item at the end.
269 Length (Container) < Count_Type'Last
270 and then Last (Container) < Index_Type'Last,
272 Length (Add'Result) = Length (Container) + 1
273 and then Get (Add'Result, Last (Add'Result)) = New_Item
274 and then Container <= Add'Result;
277 (Container : Sequence;
278 Position : Index_Type;
279 New_Item : Element_Type) return Sequence
281 -- Returns a new sequence which contains the same elements as Container
282 -- except that New_Item has been inserted at position Position.
286 Length (Container) < Count_Type'Last
287 and then Last (Container) < Index_Type'Last
288 and then Position <= Extended_Index'Succ (Last (Container)),
290 Length (Add'Result) = Length (Container) + 1
291 and then Get (Add'Result, Position) = New_Item
293 Range_Equal (Left => Container,
295 Fst => Index_Type'First,
296 Lst => Index_Type'Pred (Position))
298 Range_Shifted (Left => Container,
301 Lst => Last (Container),
305 (Container : Sequence;
306 Position : Index_Type) return Sequence
307 -- Returns a new sequence which contains the same elements as Container
308 -- except that the element at position Position has been removed.
313 Length (Container) < Count_Type'Last
314 and Last (Container) < Index_Type'Last
315 and Position in Index_Type'First .. Last (Container),
317 Length (Remove'Result) = Length (Container) - 1
319 Range_Equal (Left => Container,
320 Right => Remove'Result,
321 Fst => Index_Type'First,
322 Lst => Index_Type'Pred (Position))
324 Range_Shifted (Left => Remove'Result,
327 Lst => Last (Remove'Result),
330 ---------------------------
331 -- Iteration Primitives --
332 ---------------------------
334 function Iter_First (Container : Sequence) return Extended_Index with
337 function Iter_Has_Element
338 (Container : Sequence;
339 Position : Extended_Index) return Boolean
342 Post => Iter_Has_Element'Result =
343 (Position in Index_Type'First .. Last (Container));
344 pragma Annotate (GNATprove, Inline_For_Proof, Iter_Has_Element);
347 (Container : Sequence;
348 Position : Extended_Index) return Extended_Index
351 Pre => Iter_Has_Element (Container, Position);
355 pragma SPARK_Mode (Off);
357 package Containers is new Ada.Containers.Functional_Base
358 (Index_Type => Index_Type,
359 Element_Type => Element_Type);
361 type Sequence is record
362 Content : Containers.Container;
365 function Iter_First (Container : Sequence) return Extended_Index is
368 (Container : Sequence;
369 Position : Extended_Index) return Extended_Index
371 (if Position = Extended_Index'Last then Extended_Index'First
372 else Extended_Index'Succ (Position));
374 function Iter_Has_Element
375 (Container : Sequence;
376 Position : Extended_Index) return Boolean
378 (Position in Index_Type'First ..
380 ((Index_Type'Pos (Index_Type'First) - 1) + Length (Container))));
382 end Ada.Containers.Functional_Vectors;