DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Verada.Containers.Functional.Multisets

Functional multisets, the unbounded ghost models for proofs that one collection is a permutation of another. A multiset is a value: Add and Remove make a new one. Each element is there some number of times, its Count (0 for one that is not there); the elements that are there can be named by position, in an order that means nothing, so that contracts quantify over them.

Read the family guide for how functional models are used in contracts. The complete declaration page retains exact signatures/contracts; the source specification is the implementation baseline.

Availability and ownership

The package is ghost: its values exist only in contracts and ghost code.

Import and instantiate

with Verada.Containers.Functional.Multisets;
...
package Bags is new Verada.Containers.Functional.Multisets (Integer);

API 001

Element_Type (declaration) — The generic formal part: the type of the elements, any type with equality.

Declared interface:

generic
   type Element_Type is private;

API 002

Multiset (declaration) — The multiset value.

Declared interface:

type Multiset is private;

API 003

Distinct_Length (declaration) — How many different elements are there.

Declared interface:

function Distinct_Length (Container : Multiset) return Natural
  with Global => null;

API 004

Distinct_At (declaration) — One of them, by a position that means nothing about it.

Declared interface:

function Distinct_At (Container : Multiset; Position : Positive) return Element_Type
  with Global => null,
       Pre => Position <= Distinct_Length (Container);

API 005

Count (declaration) — How many times an element is there; more than none exactly when it is one of those named by position.

Declared interface:

function Count (Container : Multiset; Item : Element_Type) return Natural
  with Global => null,
       Post => (Count'Result > 0) = (for some I in 1 .. Distinct_Length (Container) => Distinct_At (Container, I) = Item);

API 006

Contains (declaration) — Whether an element is there at least once.

Declared interface:

function Contains (Container : Multiset; Item : Element_Type) return Boolean is (Count (Container, Item) > 0);

API 007

Is_Empty (declaration) — Whether no element is there.

Declared interface:

function Is_Empty (Container : Multiset) return Boolean is (Distinct_Length (Container) = 0);

API 008

Same_Except (declaration) — Every element is there as many times in both, except perhaps one.

Declared interface:

function Same_Except (Left, Right : Multiset; Item : Element_Type) return Boolean is
  ((for all I in 1 .. Distinct_Length (Left) =>
      Distinct_At (Left, I) = Item or else Count (Right, Distinct_At (Left, I)) = Count (Left, Distinct_At (Left, I)))
   and then (for all I in 1 .. Distinct_Length (Right) =>
               Distinct_At (Right, I) = Item or else Count (Left, Distinct_At (Right, I)) = Count (Right, Distinct_At (Right, I))));

API 009

"=" (declaration) — Every element is there as many times in each.

Declared interface:

function "=" (Left, Right : Multiset) return Boolean is
  ((for all I in 1 .. Distinct_Length (Left) => Count (Right, Distinct_At (Left, I)) = Count (Left, Distinct_At (Left, I)))
   and then (for all I in 1 .. Distinct_Length (Right) => Count (Left, Distinct_At (Right, I)) = Count (Right, Distinct_At (Right, I))));

API 010

Empty_Multiset (declaration) — The multiset with no element.

Declared interface:

function Empty_Multiset return Multiset
  with Global => null,
       Post => Distinct_Length (Empty_Multiset'Result) = 0;

API 011

Add (declaration) — The multiset with an element there once more.

Declared interface:

function Add (Container : Multiset; Item : Element_Type) return Multiset
  with Global => null,
       Pre => Count (Container, Item) < Natural'Last and then Distinct_Length (Container) < Natural'Last,
       Post => Count (Add'Result, Item) = Count (Container, Item) + 1
               and then Distinct_Length (Add'Result) <= Distinct_Length (Container) + 1
               and then Same_Except (Container, Add'Result, Item);

API 012

Remove (declaration) — The multiset with an element there once less.

Declared interface:

function Remove (Container : Multiset; Item : Element_Type) return Multiset
  with Global => null,
       Pre => Count (Container, Item) > 0,
       Post => Count (Remove'Result, Item) = Count (Container, Item) - 1
               and then Distinct_Length (Remove'Result) <= Distinct_Length (Container)
               and then Same_Except (Container, Remove'Result, Item);