--  Verada predefined library: functional multisets, the unbounded ghost models for
--  proofs that something is a permutation of something else. A Multiset is a value, like
--  the other functional containers' (Verada.Containers.Functional): Add and Remove make a new
--  one. Each element is there some number of times, its Count (0 for one that is not there);
--  the elements that are there can be named by position, from 1 to Distinct_Length, in an
--  order that means nothing, so that contracts quantify over them. Two multisets are equal
--  when every element is there as many times in each.
with Verada.Containers.Functional.Maps;

generic
   type Element_Type is private;
package Verada.Containers.Functional.Multisets
  with Ghost
is
   type Multiset is private;

   --  how many different elements are there
   function Distinct_Length (Container : Multiset) return Natural
     with Global => null;

   --  one of them, by a position that means nothing about it
   function Distinct_At (Container : Multiset; Position : Positive) return Element_Type
     with Global => null,
          Pre => Position <= Distinct_Length (Container);

   --  how many times Item is there: more than none exactly when it is one of the elements
   --  named by position
   function Count (Container : Multiset; Item : Element_Type) return Natural
     with Global => null,
          Post => (Count'Result > 0) = (for some I in 1 .. Distinct_Length (Container) => Distinct_At (Container, I) = Item);

   function Contains (Container : Multiset; Item : Element_Type) return Boolean is (Count (Container, Item) > 0);

   function Is_Empty (Container : Multiset) return Boolean is (Distinct_Length (Container) = 0);

   --  every element is there as many times in Left as in Right, except perhaps Item
   function Same_Except (Left, Right : Multiset; Item : Element_Type) return Boolean is
     ((for all I in 1 .. Distinct_Length (Left) =>
         Distinct_At (Left, I) = Item or else Count (Right, Distinct_At (Left, I)) = Count (Left, Distinct_At (Left, I)))
      and then (for all I in 1 .. Distinct_Length (Right) =>
                  Distinct_At (Right, I) = Item or else Count (Left, Distinct_At (Right, I)) = Count (Right, Distinct_At (Right, I))));

   --  every element there as many times in each
   function "=" (Left, Right : Multiset) return Boolean is
     ((for all I in 1 .. Distinct_Length (Left) => Count (Right, Distinct_At (Left, I)) = Count (Left, Distinct_At (Left, I)))
      and then (for all I in 1 .. Distinct_Length (Right) => Count (Left, Distinct_At (Right, I)) = Count (Right, Distinct_At (Right, I))));

   --  no element
   function Empty_Multiset return Multiset
     with Global => null,
          Post => Distinct_Length (Empty_Multiset'Result) = 0;

   --  Container with Item once more
   function Add (Container : Multiset; Item : Element_Type) return Multiset
     with Global => null,
          Pre => Count (Container, Item) < Natural'Last and then Distinct_Length (Container) < Natural'Last,
          Post => Count (Add'Result, Item) = Count (Container, Item) + 1
                  and then Distinct_Length (Add'Result) <= Distinct_Length (Container) + 1
                  and then Same_Except (Container, Add'Result, Item);

   --  Container with Item once less
   function Remove (Container : Multiset; Item : Element_Type) return Multiset
     with Global => null,
          Pre => Count (Container, Item) > 0,
          Post => Count (Remove'Result, Item) = Count (Container, Item) - 1
                  and then Distinct_Length (Remove'Result) <= Distinct_Length (Container)
                  and then Same_Except (Container, Remove'Result, Item);
private
   package Count_Maps is new Verada.Containers.Functional.Maps (Element_Type, Positive);

   --  each element there, with how many times
   type Multiset is record
      Counts : Count_Maps.Map;
   end record;

   function Distinct_Length (Container : Multiset) return Natural is (Count_Maps.Length (Container.Counts));

   function Distinct_At (Container : Multiset; Position : Positive) return Element_Type is
     (Count_Maps.Key_At (Container.Counts, Position));

   function Count (Container : Multiset; Item : Element_Type) return Natural is
     (if Count_Maps.Has_Key (Container.Counts, Item) then Count_Maps.Get (Container.Counts, Item) else 0);
end Verada.Containers.Functional.Multisets;
