DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Generic package Verada.Containers.Functional.Maps

Verada predefined library: functional maps, the unbounded ghost models that contracts are written against. A Map is a value: Add, Set and Remove make a new one. Its keys can be named by position, from 1 to its length, in an order that means nothing (two maps are equal when they have the same keys, each with the same element), so that contracts quantify over them.

generic
   type Key_Type is private;
   type Element_Type is private;

Map

type Map is private;

Length

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

Key_At

function Key_At (Container : Map; Position : Positive) return Key_Type
  with Global => null,
       Pre => Position <= Length (Container);

a key, by a position that means nothing about it

Element_At

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

the element of the key at a position

Has_Key

function Has_Key (Container : Map; Key : Key_Type) return Boolean is
  (for some I in 1 .. Length (Container) => Key_At (Container, I) = Key);

Is_Empty

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

Get

function Get (Container : Map; Key : Key_Type) return Element_Type
  with Global => null,
       Pre => Has_Key (Container, Key),
       Post => (for all I in 1 .. Length (Container) => (if Key_At (Container, I) = Key then Get'Result = Element_At (Container, I)));

the element of a key

Empty_Map

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

the empty map

Keys_Included

function Keys_Included (Left, Right : Map) return Boolean is
  (for all I in 1 .. Length (Left) => Has_Key (Right, Key_At (Left, I)));

every key of Left is a key of Right

Same_Keys

function Same_Keys (Left, Right : Map) return Boolean is
  (Length (Left) = Length (Right) and then Keys_Included (Left, Right));

the same keys

Has_Pair

function Has_Pair (Right, Left : Map; Position : Positive) return Boolean is
  (Position <= Length (Left)
   and then (for some J in 1 .. Length (Right) =>
               Key_At (Right, J) = Key_At (Left, Position) and then Element_At (Right, J) = Element_At (Left, Position)));

Right has Left's key at a position, with the same element

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

every key of Left is a key of Right, with the same element

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

the same keys, each with the same element

Keys_Included_Except

function Keys_Included_Except (Left, Right : Map; New_Key : Key_Type) return Boolean is
  (for all I in 1 .. Length (Left) => Key_At (Left, I) = New_Key or else Has_Key (Right, Key_At (Left, I)));

every key of Left is a key of Right, except perhaps New_Key

Add

function Add (Container : Map; New_Key : Key_Type; New_Item : Element_Type) return Map
  with Global => null,
       Pre => not Has_Key (Container, New_Key) and then Length (Container) < Natural'Last,
       Post => Length (Add'Result) = Length (Container) + 1
               and then Key_At (Add'Result, Length (Add'Result)) = New_Key
               and then Element_At (Add'Result, Length (Add'Result)) = New_Item
               and then (for all I in 1 .. Length (Container) =>
                           Key_At (Add'Result, I) = Key_At (Container, I) and then Element_At (Add'Result, I) = Element_At (Container, I));

Container with New_Key, whose element is New_Item (at the end)

Set

function Set (Container : Map; Key : Key_Type; New_Item : Element_Type) return Map
  with Global => null,
       Pre => Has_Key (Container, Key),
       Post => Length (Set'Result) = Length (Container)
               and then (for all I in 1 .. Length (Container) =>
                           Key_At (Set'Result, I) = Key_At (Container, I)
                           and then Element_At (Set'Result, I) = (if Key_At (Container, I) = Key then New_Item else Element_At (Container, I)));

Container with New_Item for Key (the keys stay where they are)

Remove

function Remove (Container : Map; Key : Key_Type) return Map
  with Global => null,
       Pre => Has_Key (Container, Key),
       Post => Length (Remove'Result) = Length (Container) - 1
               and then not Has_Key (Remove'Result, Key)
               and then Remove'Result <= Container
               and then Keys_Included_Except (Container, Remove'Result, Key)
               and then (for all I in 1 .. Length (Container) => Key_At (Container, I) = Key or else Has_Pair (Remove'Result, Container, I));

Container without Key

Choose

function Choose (Container : Map) return Key_Type
  with Global => null,
       Pre => not Is_Empty (Container),
       Post => Has_Key (Container, Choose'Result);

a key of a map that has one