--  Verada predefined library: bounded doubly linked lists of at most Capacity elements, with
--  cursors that stay valid while other elements come and go, and no storage allocated after
--  the list is made. Contracts say what each operation does to the
--  list's model (its elements in order, a functional sequence) and to the position of every
--  cursor in it.
with Verada.Containers.Functional.Sequences;

generic
   type Element_Type is private;
   Capacity : Positive;
package Verada.Containers.Formal.Doubly_Linked_Lists is
   subtype Count_Type is Natural range 0 .. Capacity;

   type List is private
     with Default_Initial_Condition => Length (List) = 0;

   --  a node of a list; No_Element designates none
   type Cursor is record
      Node : Count_Type := 0;
   end record;

   No_Element : constant Cursor := (Node => 0);

   function Length (Container : List) return Count_Type
     with Global => null;

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

   function Is_Full (Container : List) return Boolean is (Length (Container) = Capacity);

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

   function Model (Container : List) return Models.Sequence
     with Ghost,
          Global => null,
          Post => Models.Length (Model'Result) = Length (Container);

   --  the node of the list's element at a place, from 1 (proof only)
   function Node_At (Container : List; Index : Positive) return Count_Type
     with Ghost,
          Global => null,
          Pre => Index <= Length (Container);

   --  the place of a node's element in the list, from 1, or 0 when the list does not use it
   function Node_Index (Container : List; Node : Count_Type) return Count_Type
     with Global => null,
          Post => Node_Index'Result <= Length (Container)
                  and then (if Node = 0 then Node_Index'Result = 0)
                  and then (if Node_Index'Result /= 0 then Node_At (Container, Node_Index'Result) = Node)
                  and then (for all I in 1 .. Length (Container) => (Node_At (Container, I) = Node) = (I = Node_Index'Result));

   --  the place of a cursor's element in the list, from 1, or 0 when it designates none
   function Index_Of (Container : List; Position : Cursor) return Count_Type is
     (Node_Index (Container, Position.Node));

   function Has_Element (Container : List; Position : Cursor) return Boolean is
     (Index_Of (Container, Position) /= 0);

   function Element (Container : List; Position : Cursor) return Element_Type
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => Element'Result = Models.Get (Model (Container), Index_Of (Container, Position));

   --  every cursor keeps its place
   function Same_Places (Left, Right : List) return Boolean is
     (for all N in 1 .. Capacity => Node_Index (Left, N) = Node_Index (Right, N))
     with Ghost;

   --  the cursors of Old keep their place in New, those from At on one further
   function Places_Shifted_Up (Old, New_List : List; At_Index : Positive) return Boolean is
     (for all N in 1 .. Capacity =>
        (if Node_Index (Old, N) /= 0 then
           Node_Index (New_List, N)
           = (if Node_Index (Old, N) < At_Index then Node_Index (Old, N) else Node_Index (Old, N) + 1)))
     with Ghost;

   --  the cursors of Old other than the one at At_Index keep their place in New, those after
   --  it one nearer
   function Places_Shifted_Down (Old, New_List : List; At_Index : Positive) return Boolean is
     (for all N in 1 .. Capacity =>
        (if Node_Index (Old, N) /= 0 and then Node_Index (Old, N) /= At_Index then
           Node_Index (New_List, N)
           = (if Node_Index (Old, N) < At_Index then Node_Index (Old, N) else Node_Index (Old, N) - 1)))
     with Ghost;

   --  New is Old with Item at At_Index
   function Inserted (Old, New_Model : Models.Sequence; At_Index : Positive; Item : Element_Type) return Boolean is
     (Models.Length (Old) < Natural'Last
      and then At_Index <= Models.Length (Old) + 1
      and then Models.Length (New_Model) = Models.Length (Old) + 1
      and then Models.Get (New_Model, At_Index) = Item
      and then (for all I in 1 .. At_Index - 1 => Models.Get (New_Model, I) = Models.Get (Old, I))
      and then (for all I in At_Index .. Models.Length (Old) => Models.Get (New_Model, I + 1) = Models.Get (Old, I)))
     with Ghost;

   --  New is Old without the element at At_Index
   function Removed (Old, New_Model : Models.Sequence; At_Index : Positive) return Boolean is
     (At_Index <= Models.Length (Old)
      and then Models.Length (Old) < Natural'Last
      and then Models.Length (New_Model) = Models.Length (Old) - 1
      and then (for all I in 1 .. At_Index - 1 => Models.Get (New_Model, I) = Models.Get (Old, I))
      and then (for all I in At_Index + 1 .. Models.Length (Old) => Models.Get (New_Model, I - 1) = Models.Get (Old, I)))
     with Ghost;

   function First (Container : List) return Cursor
     with Global => null,
          Post => (if Is_Empty (Container) then First'Result = No_Element else Index_Of (Container, First'Result) = 1);

   function Last (Container : List) return Cursor
     with Global => null,
          Post => (if Is_Empty (Container) then Last'Result = No_Element else Index_Of (Container, Last'Result) = Length (Container));

   function Next (Container : List; Position : Cursor) return Cursor
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => (if Index_Of (Container, Position) = Length (Container) then Next'Result = No_Element
                   else Index_Of (Container, Next'Result) = Index_Of (Container, Position) + 1);

   function Previous (Container : List; Position : Cursor) return Cursor
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => (if Index_Of (Container, Position) = 1 then Previous'Result = No_Element
                   else Index_Of (Container, Previous'Result) = Index_Of (Container, Position) - 1);

   --  the first cursor of Item, or No_Element
   function Find (Container : List; Item : Element_Type) return Cursor
     with Global => null,
          Post => (if Find'Result = No_Element then
                     (for all I in 1 .. Length (Container) => Models.Get (Model (Container), I) /= Item)
                   else Has_Element (Container, Find'Result) and then Element (Container, Find'Result) = Item);

   function Contains (Container : List; Item : Element_Type) return Boolean is
     (Find (Container, Item) /= No_Element);

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

   --  New_Item before Before (at the end when Before is No_Element); Position designates it
   procedure Insert (Container : in out List; Before : Cursor; New_Item : Element_Type; Position : out Cursor)
     with Global => null,
          Pre => not Is_Full (Container) and then (Before = No_Element or else Has_Element (Container, Before)),
          Post => Inserted (Model (Container'Old), Model (Container),
                            (if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before)), New_Item)
                  and then Index_Of (Container, Position) = (if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before))
                  and then Places_Shifted_Up (Container'Old, Container, Index_Of (Container, Position));

   procedure Append (Container : in out List; New_Item : Element_Type)
     with Global => null,
          Pre => not Is_Full (Container),
          Post => Inserted (Model (Container'Old), Model (Container), Length (Container), New_Item)
                  and then Places_Shifted_Up (Container'Old, Container, Length (Container));

   procedure Prepend (Container : in out List; New_Item : Element_Type)
     with Global => null,
          Pre => not Is_Full (Container),
          Post => Inserted (Model (Container'Old), Model (Container), 1, New_Item)
                  and then Places_Shifted_Up (Container'Old, Container, 1);

   procedure Replace_Element (Container : in out List; Position : Cursor; New_Item : Element_Type)
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => Length (Container) = Length (Container'Old)
                  and then Element (Container, Position) = New_Item
                  and then Same_Places (Container'Old, Container)
                  and then (for all I in 1 .. Length (Container) =>
                              (if I /= Index_Of (Container, Position) then Models.Get (Model (Container), I) = Models.Get (Model (Container'Old), I)));

   --  the element Position designates is gone, and so is Position
   procedure Delete (Container : in out List; Position : in out Cursor)
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => Removed (Model (Container'Old), Model (Container), Index_Of (Container'Old, Position'Old))
                  and then Position = No_Element
                  and then not Has_Element (Container, Position'Old)
                  and then Places_Shifted_Down (Container'Old, Container, Index_Of (Container'Old, Position'Old));

   procedure Delete_First (Container : in out List)
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => Removed (Model (Container'Old), Model (Container), 1)
                  and then Places_Shifted_Down (Container'Old, Container, 1);

   procedure Delete_Last (Container : in out List)
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => Removed (Model (Container'Old), Model (Container), Length (Container'Old))
                  and then Places_Shifted_Down (Container'Old, Container, Length (Container'Old));
private
   type Node_Type is record
      Element : Element_Type;
      Next, Previous : Count_Type := 0;
      In_Use : Boolean := False;
   end record;

   type Node_Array is array (1 .. Capacity) of Node_Type;

   --  sequences of nodes (proof only)
   package Node_Sequences is new Verada.Containers.Functional.Sequences (Count_Type);

   --  every node, 1 to Capacity
   function All_Nodes return Node_Sequences.Sequence
     with Ghost,
          Global => null,
          Post => Node_Sequences.Length (All_Nodes'Result) = Capacity
                  and then (for all I in 1 .. Capacity => Node_Sequences.Get (All_Nodes'Result, I) = I);

   --  the elements in order from Head by Next (back from Tail by Previous); Order lists their
   --  nodes and Values their elements (the model), Spare the nodes not in use; a new element
   --  takes the first node not in use
   type List is record
      Head, Tail : Count_Type := 0;
      Count : Count_Type := 0;
      Nodes : Node_Array;
      Order : Node_Sequences.Sequence := Node_Sequences.Empty_Sequence;
      Spare : Node_Sequences.Sequence := All_Nodes;
      Values : Models.Sequence := Models.Empty_Sequence;
   end record
     with Type_Invariant => Well_Formed (List);
   --  an unused node's element is unset: Element's precondition keeps every read to used ones
   pragma Annotate (Verada, Intentional, "not fully initialized", "unused nodes are never read");

   --  Order's nodes in use, linked each way, all distinct; Spare's not in use, all distinct;
   --  every node in one of them
   function Well_Formed (Container : List) return Boolean is
     (Node_Sequences.Length (Container.Order) = Container.Count
      and then Models.Length (Container.Values) = Container.Count
      and then Node_Sequences.Length (Container.Spare) = Capacity - Container.Count
      and then (if Container.Count = 0 then Container.Head = 0 and then Container.Tail = 0
                else Container.Head = Node_Sequences.Get (Container.Order, 1)
                     and then Container.Tail = Node_Sequences.Get (Container.Order, Container.Count))
      and then (for all I in 1 .. Container.Count =>
                  Node_Sequences.Get (Container.Order, I) in 1 .. Capacity
                  and then Container.Nodes (Node_Sequences.Get (Container.Order, I)).In_Use
                  and then Container.Nodes (Node_Sequences.Get (Container.Order, I)).Element = Models.Get (Container.Values, I)
                  and then Container.Nodes (Node_Sequences.Get (Container.Order, I)).Next
                           = (if I = Container.Count then 0 else Node_Sequences.Get (Container.Order, I + 1))
                  and then Container.Nodes (Node_Sequences.Get (Container.Order, I)).Previous
                           = (if I = 1 then 0 else Node_Sequences.Get (Container.Order, I - 1)))
      and then (for all I in 1 .. Container.Count =>
                  (for all J in 1 .. I - 1 => Node_Sequences.Get (Container.Order, J) /= Node_Sequences.Get (Container.Order, I)))
      and then (for all I in 1 .. Node_Sequences.Length (Container.Spare) =>
                  Node_Sequences.Get (Container.Spare, I) in 1 .. Capacity
                  and then not Container.Nodes (Node_Sequences.Get (Container.Spare, I)).In_Use)
      and then (for all I in 1 .. Node_Sequences.Length (Container.Spare) =>
                  (for all J in 1 .. I - 1 => Node_Sequences.Get (Container.Spare, J) /= Node_Sequences.Get (Container.Spare, I)))
      and then (for all N in 1 .. Capacity =>
                  (if Container.Nodes (N).In_Use then (for some I in 1 .. Container.Count => Node_Sequences.Get (Container.Order, I) = N)
                   else (for some I in 1 .. Node_Sequences.Length (Container.Spare) => Node_Sequences.Get (Container.Spare, I) = N))));

   function Length (Container : List) return Count_Type is (Container.Count);
   function Model (Container : List) return Models.Sequence is (Container.Values);
   function Node_At (Container : List; Index : Positive) return Count_Type is (Node_Sequences.Get (Container.Order, Index));
end Verada.Containers.Formal.Doubly_Linked_Lists;
