--  Verada predefined library: functional sets, the unbounded ghost
--  models that contracts are written against. A Set is a
--  value: Add, Remove, Union and Intersection make a new one. Its elements can be named by
--  position, from 1 to its length, in an order that means nothing (two sets are equal when
--  they have the same elements), so that contracts quantify over them as over a sequence's.
with Verada.Containers.Functional.Sequences;

generic
   type Element_Type is private;
package Verada.Containers.Functional.Sets
  with Ghost
is
   type Set is private;

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

   --  an element, by a position that means nothing about it
   function Element (Container : Set; Position : Positive) return Element_Type
     with Global => null,
          Pre => Position <= Length (Container);

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

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

   --  the empty set
   function Empty_Set return Set
     with Global => null,
          Post => Length (Empty_Set'Result) = 0;

   --  every element of Left is in Right
   function "<=" (Left, Right : Set) return Boolean is
     (for all I in 1 .. Length (Left) => Contains (Right, Element (Left, I)));

   --  the same elements
   function "=" (Left, Right : Set) return Boolean is
     (Length (Left) = Length (Right) and then Left <= Right);

   --  every element of Left is in Right, except perhaps Item
   function Included_Except (Left, Right : Set; Item : Element_Type) return Boolean is
     (for all I in 1 .. Length (Left) => Element (Left, I) = Item or else Contains (Right, Element (Left, I)));

   --  every element of Container is in Left or in Right
   function Included_In_Union (Container, Left, Right : Set) return Boolean is
     (for all I in 1 .. Length (Container) => Contains (Left, Element (Container, I)) or else Contains (Right, Element (Container, I)));

   --  every element of both Left and Right is in Container
   function Includes_Intersection (Container, Left, Right : Set) return Boolean is
     (for all I in 1 .. Length (Left) => (if Contains (Right, Element (Left, I)) then Contains (Container, Element (Left, I))));

   --  no element of Left is in Right
   function No_Overlap (Left, Right : Set) return Boolean is
     (for all I in 1 .. Length (Left) => not Contains (Right, Element (Left, I)));

   --  Container with Item
   function Add (Container : Set; Item : Element_Type) return Set
     with Global => null,
          Pre => not Contains (Container, Item) and then Length (Container) < Natural'Last,
          Post => Length (Add'Result) = Length (Container) + 1
                  and then Contains (Add'Result, Item)
                  and then Container <= Add'Result
                  and then Included_Except (Add'Result, Container, Item);

   --  Container without Item
   function Remove (Container : Set; Item : Element_Type) return Set
     with Global => null,
          Pre => Contains (Container, Item),
          Post => Length (Remove'Result) = Length (Container) - 1
                  and then not Contains (Remove'Result, Item)
                  and then Remove'Result <= Container
                  and then Included_Except (Container, Remove'Result, Item);

   --  the elements of Left or Right
   function Union (Left, Right : Set) return Set
     with Global => null,
          Pre => Length (Left) <= Natural'Last - Length (Right),
          Post => Left <= Union'Result
                  and then Right <= Union'Result
                  and then Included_In_Union (Union'Result, Left, Right);

   --  the elements of both Left and Right
   function Intersection (Left, Right : Set) return Set
     with Global => null,
          Post => Intersection'Result <= Left
                  and then Intersection'Result <= Right
                  and then Includes_Intersection (Intersection'Result, Left, Right);

   --  an element of a set that has one
   function Choose (Container : Set) return Element_Type
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => Contains (Container, Choose'Result);
private
   package Sequences is new Verada.Containers.Functional.Sequences (Element_Type);

   --  the elements, each once
   type Set is record
      Items : Sequences.Sequence;
   end record
     with Type_Invariant => Unique (Set);

   function Unique (Container : Set) return Boolean is
     (for all I in 1 .. Sequences.Length (Container.Items) =>
        (for all J in 1 .. I - 1 => Sequences.Get (Container.Items, J) /= Sequences.Get (Container.Items, I)));

   function Length (Container : Set) return Natural is (Sequences.Length (Container.Items));

   function Element (Container : Set; Position : Positive) return Element_Type is
     (Sequences.Get (Container.Items, Position));
end Verada.Containers.Functional.Sets;
