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

generic
   type Key_Type is private;
   type Element_Type is private;
package Verada.Containers.Functional.Maps
  with Ghost
is
   type Map is private;

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

   --  a key, by a position that means nothing about it
   function Key_At (Container : Map; Position : Positive) return Key_Type
     with Global => null,
          Pre => Position <= Length (Container);

   --  the element of the key at a position
   function Element_At (Container : Map; Position : Positive) return Element_Type
     with Global => null,
          Pre => Position <= Length (Container);

   function Has_Key (Container : Map; Key : Key_Type) return Boolean is
     (for some I in 1 .. Length (Container) => Key_At (Container, I) = Key);

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

   --  the element of a key
   function Get (Container : Map; Key : Key_Type) return Element_Type
     with Global => null,
          Pre => Has_Key (Container, Key),
          Post => (for all I in 1 .. Length (Container) => (if Key_At (Container, I) = Key then Get'Result = Element_At (Container, I)));

   --  the empty map
   function Empty_Map return Map
     with Global => null,
          Post => Length (Empty_Map'Result) = 0;

   --  every key of Left is a key of Right
   function Keys_Included (Left, Right : Map) return Boolean is
     (for all I in 1 .. Length (Left) => Has_Key (Right, Key_At (Left, I)));

   --  the same keys
   function Same_Keys (Left, Right : Map) return Boolean is
     (Length (Left) = Length (Right) and then Keys_Included (Left, Right));

   --  Right has Left's key at a position, with the same element
   function Has_Pair (Right, Left : Map; Position : Positive) return Boolean is
     (Position <= Length (Left)
      and then (for some J in 1 .. Length (Right) =>
                  Key_At (Right, J) = Key_At (Left, Position) and then Element_At (Right, J) = Element_At (Left, Position)));

   --  every key of Left is a key of Right, with the same element
   function "<=" (Left, Right : Map) return Boolean is
     (for all I in 1 .. Length (Left) => Has_Pair (Right, Left, I));

   --  the same keys, each with the same element
   function "=" (Left, Right : Map) return Boolean is
     (Length (Left) = Length (Right) and then Left <= Right);

   --  every key of Left is a key of Right, except perhaps New_Key
   function Keys_Included_Except (Left, Right : Map; New_Key : Key_Type) return Boolean is
     (for all I in 1 .. Length (Left) => Key_At (Left, I) = New_Key or else Has_Key (Right, Key_At (Left, I)));

   --  Container with New_Key, whose element is New_Item (at the end)
   function Add (Container : Map; New_Key : Key_Type; New_Item : Element_Type) return Map
     with Global => null,
          Pre => not Has_Key (Container, New_Key) and then Length (Container) < Natural'Last,
          Post => Length (Add'Result) = Length (Container) + 1
                  and then Key_At (Add'Result, Length (Add'Result)) = New_Key
                  and then Element_At (Add'Result, Length (Add'Result)) = New_Item
                  and then (for all I in 1 .. Length (Container) =>
                              Key_At (Add'Result, I) = Key_At (Container, I) and then Element_At (Add'Result, I) = Element_At (Container, I));

   --  Container with New_Item for Key (the keys stay where they are)
   function Set (Container : Map; Key : Key_Type; New_Item : Element_Type) return Map
     with Global => null,
          Pre => Has_Key (Container, Key),
          Post => Length (Set'Result) = Length (Container)
                  and then (for all I in 1 .. Length (Container) =>
                              Key_At (Set'Result, I) = Key_At (Container, I)
                              and then Element_At (Set'Result, I) = (if Key_At (Container, I) = Key then New_Item else Element_At (Container, I)));

   --  Container without Key
   function Remove (Container : Map; Key : Key_Type) return Map
     with Global => null,
          Pre => Has_Key (Container, Key),
          Post => Length (Remove'Result) = Length (Container) - 1
                  and then not Has_Key (Remove'Result, Key)
                  and then Remove'Result <= Container
                  and then Keys_Included_Except (Container, Remove'Result, Key)
                  and then (for all I in 1 .. Length (Container) => Key_At (Container, I) = Key or else Has_Pair (Remove'Result, Container, I));

   --  a key of a map that has one
   function Choose (Container : Map) return Key_Type
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => Has_Key (Container, Choose'Result);
private
   package Key_Sequences is new Verada.Containers.Functional.Sequences (Key_Type);
   package Element_Sequences is new Verada.Containers.Functional.Sequences (Element_Type);

   --  the keys, each once, and their elements at the same positions
   type Map is record
      Keys : Key_Sequences.Sequence;
      Elements : Element_Sequences.Sequence;
   end record
     with Type_Invariant => Well_Formed (Map);

   function Well_Formed (Container : Map) return Boolean is
     (Key_Sequences.Length (Container.Keys) = Element_Sequences.Length (Container.Elements)
      and then (for all I in 1 .. Key_Sequences.Length (Container.Keys) =>
                  (for all J in 1 .. I - 1 => Key_Sequences.Get (Container.Keys, J) /= Key_Sequences.Get (Container.Keys, I))));

   function Length (Container : Map) return Natural is (Key_Sequences.Length (Container.Keys));

   function Key_At (Container : Map; Position : Positive) return Key_Type is
     (Key_Sequences.Get (Container.Keys, Position));

   function Element_At (Container : Map; Position : Positive) return Element_Type is
     (Element_Sequences.Get (Container.Elements, Position));
end Verada.Containers.Functional.Maps;
