DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Generic package Verada.Containers.Functional.Sets

Verada predefined library: functional sets, the unbounded ghost models that contracts are written against. A Set is a value: Add, Remove, Union and Intersection make a new one. Its elements can be named by position, from 1 to its length, in an order that means nothing (two sets are equal when they have the same elements), so that contracts quantify over them as over a sequence's.

generic
   type Element_Type is private;

Set

type Set is private;

Length

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

Element

function Element (Container : Set; Position : Positive) return Element_Type
  with Global => null,
       Pre => Position <= Length (Container);

an element, by a position that means nothing about it

Contains

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

Is_Empty

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

Empty_Set

function Empty_Set return Set
  with Global => null,
       Post => Length (Empty_Set'Result) = 0;

the empty set

function "<=" (Left, Right : Set) return Boolean is
  (for all I in 1 .. Length (Left) => Contains (Right, Element (Left, I)));

every element of Left is in Right

function "=" (Left, Right : Set) return Boolean is
  (Length (Left) = Length (Right) and then Left <= Right);

the same elements

Included_Except

function Included_Except (Left, Right : Set; Item : Element_Type) return Boolean is
  (for all I in 1 .. Length (Left) => Element (Left, I) = Item or else Contains (Right, Element (Left, I)));

every element of Left is in Right, except perhaps Item

Included_In_Union

function Included_In_Union (Container, Left, Right : Set) return Boolean is
  (for all I in 1 .. Length (Container) => Contains (Left, Element (Container, I)) or else Contains (Right, Element (Container, I)));

every element of Container is in Left or in Right

Includes_Intersection

function Includes_Intersection (Container, Left, Right : Set) return Boolean is
  (for all I in 1 .. Length (Left) => (if Contains (Right, Element (Left, I)) then Contains (Container, Element (Left, I))));

every element of both Left and Right is in Container

No_Overlap

function No_Overlap (Left, Right : Set) return Boolean is
  (for all I in 1 .. Length (Left) => not Contains (Right, Element (Left, I)));

no element of Left is in Right

Add

function Add (Container : Set; Item : Element_Type) return Set
  with Global => null,
       Pre => not Contains (Container, Item) and then Length (Container) < Natural'Last,
       Post => Length (Add'Result) = Length (Container) + 1
               and then Contains (Add'Result, Item)
               and then Container <= Add'Result
               and then Included_Except (Add'Result, Container, Item);

Container with Item

Remove

function Remove (Container : Set; Item : Element_Type) return Set
  with Global => null,
       Pre => Contains (Container, Item),
       Post => Length (Remove'Result) = Length (Container) - 1
               and then not Contains (Remove'Result, Item)
               and then Remove'Result <= Container
               and then Included_Except (Container, Remove'Result, Item);

Container without Item

Union

function Union (Left, Right : Set) return Set
  with Global => null,
       Pre => Length (Left) <= Natural'Last - Length (Right),
       Post => Left <= Union'Result
               and then Right <= Union'Result
               and then Included_In_Union (Union'Result, Left, Right);

the elements of Left or Right

Intersection

function Intersection (Left, Right : Set) return Set
  with Global => null,
       Post => Intersection'Result <= Left
               and then Intersection'Result <= Right
               and then Includes_Intersection (Intersection'Result, Left, Right);

the elements of both Left and Right

Choose

function Choose (Container : Set) return Element_Type
  with Global => null,
       Pre => not Is_Empty (Container),
       Post => Contains (Container, Choose'Result);

an element of a set that has one