--  Verada predefined library: bounded maps of at most Capacity entries from keys to elements,
--  specified through Length, Key and Element at positions 1 .. Length (M): each key at most
--  once. Lookup is by position (a linear search), which keeps the model the prover sees the
--  same as what runs; `for K of M` gives the keys.
with Verada.Containers.Functional.Sequences;

generic
   type Key_Type is private;
   type Element_Type is private;
   Capacity : Positive;
package Verada.Containers.Formal.Maps is
   subtype Extended_Index is Natural range 0 .. Capacity;
   subtype Index_Type is Positive range 1 .. Capacity;

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

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

   function Key (Container : Map; Index : Index_Type) return Key_Type
     with Global => null,
          Pre => Index <= Length (Container);

   --  the functional model: the keys in insertion order, as a sequence
   package Key_Models is new Verada.Containers.Functional.Sequences (Key_Type);

   function Keys_Model (Container : Map) return Key_Models.Sequence
     with Ghost,
          Global => null,
          Post => Key_Models.Length (Keys_Model'Result) = Length (Container)
                  and then (for all I in 1 .. Length (Container) => Key_Models.Get (Keys_Model'Result, I) = Key (Container, I));

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

   --  the position of K, or 0
   function Find (Container : Map; K : Key_Type) return Extended_Index
     with Global => null,
          Post => (if Find'Result = 0 then (for all I in 1 .. Length (Container) => Key (Container, I) /= K)
                   else Find'Result <= Length (Container) and then Key (Container, Find'Result) = K
                        and then (for all I in 1 .. Find'Result - 1 => Key (Container, I) /= K));

   function Contains (Container : Map; K : Key_Type) return Boolean is (Find (Container, K) /= 0);

   function Value (Container : Map; K : Key_Type) return Element_Type is
     (Element (Container, Find (Container, K)))
     with Pre => Contains (Container, K);

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

   --  a new entry for K, at the end
   procedure Insert (Container : in out Map; K : Key_Type; New_Item : Element_Type)
     with Global => null,
          Pre => not Contains (Container, K) and then Length (Container) < Capacity,
          Post => Length (Container) = Length (Container)'Old + 1
                  and then Key (Container, Length (Container)) = K
                  and then Element (Container, Length (Container)) = New_Item
                  and then (for all I in 1 .. Length (Container'Old) => Key (Container, I) = Key (Container'Old, I) and then Element (Container, I) = Element (Container'Old, I));

   --  K now maps to New_Item
   procedure Replace (Container : in out Map; K : Key_Type; New_Item : Element_Type)
     with Global => null,
          Pre => Contains (Container, K),
          Post => Length (Container) = Length (Container)'Old
                  and then (for all I in 1 .. Length (Container) => Key (Container, I) = Key (Container'Old, I))
                  and then Value (Container, K) = New_Item
                  and then (for all I in 1 .. Length (Container) => (if Key (Container, I) /= K then Element (Container, I) = Element (Container'Old, I)));

   function Iter_First (Container : Map) return Natural is (1);

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

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

   function Iter_Key (Container : Map; Position : Natural) return Key_Type is
     (Key (Container, Position))
     with Pre => Iter_Has_Element (Container, Position);
private
   type Key_Array is array (Index_Type) of Key_Type;
   type Element_Array is array (Index_Type) of Element_Type;

   type Map is record
      Last : Extended_Index := 0;
      Keys : Key_Array;
      Items : Element_Array;
   end record;
   --  the entries past Last start unset: the preconditions keep every read below Last
   pragma Annotate (Verada, Intentional, "not fully initialized", "entries past Last are never read");

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

   function Key (Container : Map; Index : Index_Type) return Key_Type is (Container.Keys (Index));

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