--  Verada predefined library: a directed graph, a proof enclave. Its vertices are cells, and
--  each vertex's edges point at the cells of other vertices (or its own), so a vertex is
--  shared by every edge into it and the edges may make cycles, which ownership forbids; the
--  heap engine proves the operations against the graph's model, its vertices' degrees and
--  its edges, so that clients treat a graph as that value. Only memory limits the vertices
--  and a vertex's edges: the table of vertices and each vertex's links double when full, and
--  Add_Vertex and Add_Edge raise Storage_Error when memory cannot hold one more
--  (Verada.Storage).
with Verada.Containers.Functional.Sequences;
with Verada.Footprints;
with Verada.Storage;
package Verada.Containers.Graphs
  with Annotate => (Verada, Enclave)
is
   subtype Vertex_Count_Type is Natural;
   subtype Vertex is Positive;
   subtype Degree_Type is Natural;

   --  an edge, from a vertex to a vertex
   type Edge is record
      From, To : Vertex;
   end record;

   package Edge_Sequences is new Verada.Containers.Functional.Sequences (Edge);
   package Degree_Sequences is new Verada.Containers.Functional.Sequences (Degree_Type);
   use type Edge_Sequences.Sequence;
   use type Degree_Sequences.Sequence;

   --  a graph's value: the out-degree of each vertex, the vertices being 1 .. the count of
   --  degrees; and the edges, each once, in the order they were added
   type Graph_Model is record
      Degrees : Degree_Sequences.Sequence;
      Edges : Edge_Sequences.Sequence;
   end record;

   type Graph is limited private
     with Default_Initial_Condition => Degree_Sequences.Length (Model (Graph).Degrees) = 0
                                       and then Edge_Sequences.Length (Model (Graph).Edges) = 0,
          Finalizable => (Finalize => Clear, Relaxed_Finalization => True);

   function Model (G : Graph) return Graph_Model with Ghost;

   function Vertex_Count (G : Graph) return Vertex_Count_Type
     with Post => Vertex_Count'Result = Degree_Sequences.Length (Model (G).Degrees)
                  and then Vertex_Count'Result < Verada.Storage.Length_Bound;

   --  whether a graph's model has an edge from From to To (of the model, not the graph, so
   --  that clients see what it says: a function of an enclave object is opaque to them)
   function Has_Edge (M : Graph_Model; From, To : Vertex) return Boolean is
     (for some K in 1 .. Edge_Sequences.Length (M.Edges) => Edge_Sequences.Get (M.Edges, K) = Edge'(From, To))
     with Ghost;

   --  how many edges leave V
   function Out_Degree (G : Graph; V : Vertex) return Degree_Type
     with Pre => V <= Vertex_Count (G),
          Post => Out_Degree'Result = Degree_Sequences.Get (Model (G).Degrees, V)
                  and then Out_Degree'Result < Verada.Storage.Length_Bound;

   --  whether an edge leads from From to To, found by following From's edges
   function Adjacent (G : Graph; From, To : Vertex) return Boolean
     with Pre => From <= Vertex_Count (G) and then To <= Vertex_Count (G),
          Post => Adjacent'Result = Has_Edge (Model (G), From, To);

   --  a new vertex, with no edges, numbered after the others
   procedure Add_Vertex (G : in out Graph; V : out Vertex)
     with Post => Model (G).Degrees = Degree_Sequences.Add (Model (G)'Old.Degrees, 0)
                  and Model (G).Edges = Model (G)'Old.Edges
                  and V = Vertex_Count (G);

   --  an edge from From to To (to itself when they are the same), not there before
   procedure Add_Edge (G : in out Graph; From, To : Vertex)
     with Pre => From <= Vertex_Count (G) and then To <= Vertex_Count (G)
                 and then not Has_Edge (Model (G), From, To),
          Post => Model (G).Degrees = Degree_Sequences.Set (Model (G)'Old.Degrees, From, Degree_Sequences.Get (Model (G)'Old.Degrees, From) + 1)
                  and Model (G).Edges = Edge_Sequences.Add (Model (G)'Old.Edges, Edge'(From, To));

   --  every vertex and edge removed (and the cells freed: a graph that ends is cleared)
   procedure Clear (G : in out Graph)
     with Post => Vertex_Count (G) = 0 and then Edge_Sequences.Length (Model (G).Edges) = 0;
private
   type Vertex_Cell;
   type Vertex_Ptr is access Vertex_Cell;

   --  a vertex's links, the cells its edges lead to (the first Degree of them), and the table
   --  of vertices: arrays that double when full
   type Targets is array (Positive range <>) of Vertex_Ptr;
   type Targets_Ptr is access Targets;
   type Vertex_Table is array (Positive range <>) of Vertex_Ptr;
   type Table_Ptr is access Vertex_Table;

   --  a vertex: its number, and the cells its edges lead to, the first Degree of Links
   type Vertex_Cell is record
      Id : Vertex;
      Degree : Degree_Type := 0;
      Links : Targets_Ptr;
   end record;

   package Vertex_Cells is new Verada.Footprints (Vertex_Ptr);
   package Link_Cells is new Verada.Footprints (Targets_Ptr);
   package Table_Cells is new Verada.Footprints (Table_Ptr);

   --  vertex I is the cell Table (I), whose number is I, in the footprint, its degree the
   --  model's; each of its edges leads to a vertex's cell and is an edge of the model; each
   --  edge of the model is one of its From's edges; the footprints hold exactly the cells,
   --  the vertices' links, and the table (none before the first vertex)
   type Graph is limited record
      Count : Vertex_Count_Type := 0;
      Table : Table_Ptr := null;
      Repr : Vertex_Cells.Cells := Vertex_Cells.Empty;
      Link_Repr : Link_Cells.Cells := Link_Cells.Empty;
      Table_Repr : Table_Cells.Cells := Table_Cells.Empty;
      Degree_List : Degree_Sequences.Sequence := Degree_Sequences.Empty_Sequence;
      Edge_List : Edge_Sequences.Sequence := Edge_Sequences.Empty_Sequence;
      --  each edge's place among its From's links (so that the invariant names the link an
      --  edge is, rather than stating that one exists)
      Slot_List : Degree_Sequences.Sequence := Degree_Sequences.Empty_Sequence;
   end record
     with Type_Invariant => Well_Formed (Graph);

   --  the table: none before the first vertex, then one from 1 with room for every vertex,
   --  the only cell of its footprint
   function Table_Kept (G : Graph) return Boolean is
     (G.Count < Verada.Storage.Length_Bound
      and then (if G.Table = null then G.Count = 0 and then (for all T of G.Table_Repr => False)
                else Table_Cells.Contains (G.Table_Repr, G.Table)
                     and then (for all T of G.Table_Repr => T = G.Table)
                     and then G.Table'First = 1 and then G.Table'Last >= 1 and then G.Count <= G.Table'Last));

   --  the cells: vertex I's is Table (I), whose number is I, its links an array of its own
   --  with room for its edges; the footprints hold exactly them (all that clearing needs)
   function Cells_Kept (G : Graph) return Boolean is
     (Table_Kept (G)
      and then (for all I in 1 .. G.Count =>
                  G.Table (I) /= null
                  and then Vertex_Cells.Contains (G.Repr, G.Table (I))
                  and then G.Table (I).Id = I
                  and then G.Table (I).Links /= null
                  and then Link_Cells.Contains (G.Link_Repr, G.Table (I).Links)
                  and then G.Table (I).Links'First = 1
                  and then G.Table (I).Links'Last >= 1
                  and then G.Table (I).Degree <= G.Table (I).Links'Last
                  and then G.Table (I).Degree < Verada.Storage.Length_Bound)
      --  (each vertex's links its own)
      and then (for all I in 1 .. G.Count => (for all J in 1 .. I - 1 => G.Table (J).Links /= G.Table (I).Links))
      and then (for all P of G.Repr => (for some I in 1 .. G.Count => G.Table (I) = P))
      and then (for all L of G.Link_Repr => (for some I in 1 .. G.Count => G.Table (I).Links = L)));

   function Well_Formed (G : Graph) return Boolean is
     (Cells_Kept (G)
      and then Degree_Sequences.Length (G.Degree_List) = G.Count
      and then (for all I in 1 .. G.Count =>
                  G.Table (I).Degree = Degree_Sequences.Get (G.Degree_List, I)
                  and then (for all K in 1 .. G.Table (I).Degree =>
                              G.Table (I).Links (K) /= null
                              and then Vertex_Cells.Contains (G.Repr, G.Table (I).Links (K))
                              and then G.Table (I).Links (K).Id <= G.Count
                              and then G.Table (G.Table (I).Links (K).Id) = G.Table (I).Links (K)
                              and then (for some E in 1 .. Edge_Sequences.Length (G.Edge_List) =>
                                          Edge_Sequences.Get (G.Edge_List, E) = Edge'(I, G.Table (I).Links (K).Id))))
      and then Degree_Sequences.Length (G.Slot_List) = Edge_Sequences.Length (G.Edge_List)
      and then (for all E in 1 .. Edge_Sequences.Length (G.Edge_List) =>
                  Edge_Sequences.Get (G.Edge_List, E).From <= G.Count
                  and then Edge_Sequences.Get (G.Edge_List, E).To <= G.Count
                  and then Degree_Sequences.Get (G.Slot_List, E) in 1 .. G.Table (Edge_Sequences.Get (G.Edge_List, E).From).Degree
                  and then G.Table (Edge_Sequences.Get (G.Edge_List, E).From).Links (Degree_Sequences.Get (G.Slot_List, E))
                           = G.Table (Edge_Sequences.Get (G.Edge_List, E).To)));

   function Model (G : Graph) return Graph_Model is ((Degrees => G.Degree_List, Edges => G.Edge_List));
   function Vertex_Count (G : Graph) return Vertex_Count_Type is (G.Count);
   function Out_Degree (G : Graph; V : Vertex) return Degree_Type is (G.Table (V).Degree);
end Verada.Containers.Graphs;
