--  Verada predefined library: functional sequences, the unbounded ghost models
--  that container contracts are written against. A
--  Sequence is a value: Add and Set make a new one. The package is ghost; Verada's prover sees
--  a Sequence as its own sequences (the arrays with run-time bounds), the interpreter as such
--  an array, and no code is generated for it.
generic
   type Element_Type is private;
package Verada.Containers.Functional.Sequences
  with Ghost
is
   type Sequence is private;

   --  the empty sequence
   function Empty_Sequence return Sequence
     with Global => null,
          Post => Length (Empty_Sequence'Result) = 0;

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

   --  the element at a position, from 1
   function Get (Container : Sequence; Position : Positive) return Element_Type
     with Global => null,
          Pre => Position <= Length (Container);

   function Last (Container : Sequence) return Natural is (Length (Container));

   --  Container with New_Item at its end
   function Add (Container : Sequence; New_Item : Element_Type) return Sequence
     with Global => null,
          Pre => Length (Container) < Natural'Last,
          Post => Length (Add'Result) = Length (Container) + 1
                  and then Get (Add'Result, Length (Add'Result)) = New_Item
                  and then (for all I in 1 .. Length (Container) => Get (Add'Result, I) = Get (Container, I));

   --  Container with New_Item at Position
   function Set (Container : Sequence; Position : Positive; New_Item : Element_Type) return Sequence
     with Global => null,
          Pre => Position <= Length (Container),
          Post => Length (Set'Result) = Length (Container)
                  and then Get (Set'Result, Position) = New_Item
                  and then (for all I in 1 .. Length (Container) => (if I /= Position then Get (Set'Result, I) = Get (Container, I)));

   --  Container with New_Item at its start
   function Prepend (Container : Sequence; New_Item : Element_Type) return Sequence
     with Global => null,
          Pre => Length (Container) < Natural'Last,
          Post => Length (Prepend'Result) = Length (Container) + 1
                  and then Get (Prepend'Result, 1) = New_Item
                  and then (for all I in 1 .. Length (Container) => Get (Prepend'Result, I + 1) = Get (Container, I));

   --  Container without its first element
   function Remove_First (Container : Sequence) return Sequence
     with Global => null,
          Pre => Length (Container) > 0,
          Post => Length (Remove_First'Result) = Length (Container) - 1
                  and then (for all I in 1 .. Length (Remove_First'Result) => Get (Remove_First'Result, I) = Get (Container, I + 1));

   --  Container without its last element
   function Remove_Last (Container : Sequence) return Sequence
     with Global => null,
          Pre => Length (Container) > 0,
          Post => Length (Remove_Last'Result) = Length (Container) - 1
                  and then (for all I in 1 .. Length (Remove_Last'Result) => Get (Remove_Last'Result, I) = Get (Container, I));

   --  the same elements in the same order
   function "=" (Left, Right : Sequence) return Boolean
     with Global => null,
          Post => "="'Result = (Length (Left) = Length (Right)
                                and then (for all I in 1 .. Length (Left) => Get (Left, I) = Get (Right, I)));

   --  Left is a prefix of Right
   function "<=" (Left, Right : Sequence) return Boolean
     with Global => null,
          Post => "<="'Result = (Length (Left) <= Length (Right)
                                 and then (for all I in 1 .. Length (Left) => Get (Left, I) = Get (Right, I)));

   function Contains (Container : Sequence; Item : Element_Type) return Boolean is
     (for some I in 1 .. Length (Container) => Get (Container, I) = Item);

   --  Left and Right are the same value: the element type's own equality, which a container
   --  whose "=" for elements is a generic formal (Ada.Containers') states its contracts with
   function Equal_Elements (Left, Right : Element_Type) return Boolean is (Left = Right);
private
   --  the model is a sequence of Element_Type (the record only names the element type for
   --  Verada's lowering); Verada's prover and interpreter know these operations
   type Sequence is record
      Element_Of : Element_Type;
   end record;
end Verada.Containers.Functional.Sequences;
