--  Verada predefined library: a doubly linked list, a proof enclave. Its cells are shared
--  between the links each way, which ownership forbids; the heap engine proves its
--  operations against the list's model, a sequence, and proves that each changes only its
--  own list's cells, so that clients treat a list as the value its model is. Only memory
--  limits the length: Append and Prepend raise Storage_Error when memory cannot hold one more
--  element (Verada.Storage).
with Verada.Containers.Functional.Sequences;
with Verada.Footprints;
with Verada.Storage;
generic
   type Element_Type is private;
package Verada.Containers.Linked_Lists
  with Annotate => (Verada, Enclave)
is
   package Element_Sequences is new Verada.Containers.Functional.Sequences (Element_Type);
   use type Element_Sequences.Sequence;

   type List is limited private
     with Default_Initial_Condition => Length (List) = 0,
          Finalizable => (Finalize => Clear, Relaxed_Finalization => True);

   --  the elements, first to last
   function Model (L : List) return Element_Sequences.Sequence with Ghost;

   function Length (L : List) return Natural
     with Post => Length'Result = Element_Sequences.Length (Model (L));

   function First_Element (L : List) return Element_Type
     with Pre => Length (L) > 0, Post => First_Element'Result = Element_Sequences.Get (Model (L), 1);

   function Last_Element (L : List) return Element_Type
     with Pre => Length (L) > 0, Post => Last_Element'Result = Element_Sequences.Get (Model (L), Length (L));

   --  every element removed (and its cell freed: a list that ends is cleared)
   procedure Clear (L : in out List)
     with Post => Length (L) = 0;

   procedure Append (L : in out List; X : Element_Type)
     with Post => Model (L) = Element_Sequences.Add (Model (L)'Old, X);

   procedure Prepend (L : in out List; X : Element_Type)
     with Post => Model (L) = Element_Sequences.Prepend (Model (L)'Old, X);

   procedure Remove_Last (L : in out List; X : out Element_Type)
     with Always_Terminates,
          Pre => Length (L) > 0,
          Post => Model (L)'Old = Element_Sequences.Add (Model (L), X);

   procedure Remove_First (L : in out List; X : out Element_Type)
     with Pre => Length (L) > 0,
          Post => Model (L)'Old = Element_Sequences.Prepend (Model (L), X);

   --  the I'th element changed in place by Process, which Updated describes: the
   --  element is not copied out and back, and no pointer into the list outlives the call
   generic
      with function Updated (E : Element_Type) return Element_Type with Ghost;
      with procedure Process (E : in out Element_Type)
        with Global => null, Post => E = Updated (E'Old);
   procedure Update_Element (L : in out List; I : Positive)
     with Pre => I <= Length (L),
          Post => Model (L) = Element_Sequences.Set (Model (L)'Old, I, Updated (Element_Sequences.Get (Model (L)'Old, I)));
private
   type Node;
   type Node_Ptr is access Node;
   type Node is record
      Value : Element_Type;
      Prev, Next : Node_Ptr;
   end record;

   package Node_Cells is new Verada.Footprints (Node_Ptr);
   package Node_Sequences is new Verada.Containers.Functional.Sequences (Node_Ptr);

   --  the nodes first to last (a ghost sequence) and their values (the model), each node in the
   --  footprint, linked to its neighbours each way, all distinct; the footprint holds exactly
   --  them
   type List is limited record
      Head, Tail : Node_Ptr;
      Count : Natural := 0;
      Repr : Node_Cells.Cells := Node_Cells.Empty;
      Nodes : Node_Sequences.Sequence;
      Values : Element_Sequences.Sequence;
   end record
     with Type_Invariant => Well_Formed (List);

   function Well_Formed (L : List) return Boolean is
     (L.Count < Verada.Storage.Length_Bound
      and then Node_Sequences.Length (L.Nodes) = L.Count
      and then Element_Sequences.Length (L.Values) = L.Count
      and then (if L.Count = 0 then L.Head = null and then L.Tail = null
                else L.Head = Node_Sequences.Get (L.Nodes, 1) and then L.Tail = Node_Sequences.Get (L.Nodes, L.Count))
      and then (for all I in 1 .. L.Count =>
                  Node_Sequences.Get (L.Nodes, I) /= null
                  and then Node_Cells.Contains (L.Repr, Node_Sequences.Get (L.Nodes, I))
                  and then Node_Sequences.Get (L.Nodes, I).Value = Element_Sequences.Get (L.Values, I)
                  and then Node_Sequences.Get (L.Nodes, I).Next = (if I = L.Count then null else Node_Sequences.Get (L.Nodes, I + 1))
                  and then Node_Sequences.Get (L.Nodes, I).Prev = (if I = 1 then null else Node_Sequences.Get (L.Nodes, I - 1)))
      and then (for all I in 1 .. L.Count => (for all J in 1 .. I - 1 => Node_Sequences.Get (L.Nodes, I) /= Node_Sequences.Get (L.Nodes, J)))
      and then (for all P of L.Repr => (for some I in 1 .. L.Count => Node_Sequences.Get (L.Nodes, I) = P)));

   function Model (L : List) return Element_Sequences.Sequence is (L.Values);
   function Length (L : List) return Natural is (L.Count);
   function First_Element (L : List) return Element_Type is (L.Head.Value);
   function Last_Element (L : List) return Element_Type is (L.Tail.Value);
end Verada.Containers.Linked_Lists;
