DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Generic package Verada.Containers.Functional.Multisets

Verada predefined library: functional multisets, the unbounded ghost models for proofs that something is a permutation of something else. A Multiset is a value, like the other functional containers' (Verada.Containers.Functional): 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, from 1 to Distinct_Length, in an order that means nothing, so that contracts quantify over them. Two multisets are equal when every element is there as many times in each.

generic
   type Element_Type is private;

Multiset

type Multiset is private;

Distinct_Length

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

how many different elements are there

Distinct_At

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

one of them, by a position that means nothing about it

Count

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);

how many times Item is there: more than none exactly when it is one of the elements named by position

Contains

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

Is_Empty

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

Same_Except

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))));

every element is there as many times in Left as in Right, except perhaps Item

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))));

every element there as many times in each

Empty_Multiset

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

no element

Add

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);

Container with Item once more

Remove

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);

Container with Item once less