--  Verada predefined library: bounded sets of at most Capacity elements, kept in increasing
--  order of "<" (which must be a strict order), specified through Length and Element like the
--  bounded vectors: the elements are Element (S, 1) .. Element (S, Length (S)).
with Verada.Containers.Functional.Sequences;

generic
   type Element_Type is private;
   Capacity : Positive;
   with function "<" (Left, Right : Element_Type) return Boolean is <>;
package Verada.Containers.Formal.Ordered_Sets is
   subtype Extended_Index is Natural range 0 .. Capacity;
   subtype Index_Type is Positive range 1 .. Capacity;

   type Set is private
     with Default_Initial_Condition => Length (Set) = 0,
          Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Element);

   function Length (Container : Set) return Extended_Index
     with Global => null;

   function Is_Empty (Container : Set) return Boolean is (Length (Container) = 0);

   function Element (Container : Set; Index : Index_Type) return Element_Type
     with Global => null,
          Pre => Index <= Length (Container);

   --  the functional model: the elements in increasing order, as a sequence
   package Models is new Verada.Containers.Functional.Sequences (Element_Type);

   function Model (Container : Set) return Models.Sequence
     with Ghost,
          Global => null,
          Post => Models.Length (Model'Result) = Length (Container)
                  and then (for all I in 1 .. Length (Container) => Models.Get (Model'Result, I) = Element (Container, I));

   function Contains (Container : Set; Item : Element_Type) return Boolean is
     (for some I in 1 .. Length (Container) => Element (Container, I) = Item);

   procedure Clear (Container : in out Set)
     with Global => null,
          Post => Length (Container) = 0;

   --  Item joins the set, in its place in the order
   procedure Insert (Container : in out Set; Item : Element_Type)
     with Global => null,
          Pre => not Contains (Container, Item) and then Length (Container) < Capacity,
          Post => Length (Container) = Length (Container)'Old + 1
                  and then Contains (Container, Item)
                  and then (for all I in 1 .. Length (Container'Old) => Contains (Container, Element (Container'Old, I)))
                  and then (for all I in 1 .. Length (Container) => Element (Container, I) = Item or else Contains (Container'Old, Element (Container, I)));

   --  Item joins the set unless it is there already
   procedure Include (Container : in out Set; Item : Element_Type)
     with Global => null,
          Pre => Contains (Container, Item) or else Length (Container) < Capacity,
          Post => Contains (Container, Item)
                  --  (the same in the model, so that callers need not carry the witness there)
                  and then Models.Contains (Model (Container), Item)
                  and then (for all I in 1 .. Length (Container'Old) => Contains (Container, Element (Container'Old, I)))
                  and then (for all I in 1 .. Length (Container) => Element (Container, I) = Item or else Contains (Container'Old, Element (Container, I)));

   --  `for E of S` and quantifiers over S: in increasing order
   function Iter_First (Container : Set) return Natural is (1);

   function Iter_Next (Container : Set; Position : Natural) return Natural is
     (if Position < Natural'Last then Position + 1 else Position);

   function Iter_Has_Element (Container : Set; Position : Natural) return Boolean is
     (Position in 1 .. Length (Container));

   function Iter_Element (Container : Set; Position : Natural) return Element_Type is
     (Element (Container, Position))
     with Pre => Iter_Has_Element (Container, Position);
private
   type Element_Array is array (Index_Type) of Element_Type;

   type Set is record
      Last : Extended_Index := 0;
      Items : Element_Array;
   end record;
   --  the items past Last start unset: Element's precondition keeps every read below Last
   pragma Annotate (Verada, Intentional, "not fully initialized", "items past Last are never read");

   function Length (Container : Set) return Extended_Index is (Container.Last);

   function Element (Container : Set; Index : Index_Type) return Element_Type is (Container.Items (Index));
end Verada.Containers.Formal.Ordered_Sets;
