DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Functional models for contracts

The Functional.Sequences, Sets and Maps packages are ghost mathematical values. They let contracts describe contents independently of a mutable container's representation. Operations return new values and leave arguments logically unchanged. They exist only in contracts and ghost code: a build leaves them out of the program.

Sequences

Functional.Sequences takes Element_Type. Empty_Sequence has zero length. Length and Last expose the sequence extent; positions start at

  1. Get needs Position <= Length. Add appends one item, requiring Length < Natural'Last, and preserves old positions. Set replaces one present position and preserves length and all other positions. Prepend adds an item before the first, moving the others one position later; Remove_First and Remove_Last need a nonempty sequence and drop its first or last element. Equality means equal lengths and pairwise equal elements. <= means prefix, not lexicographic ordering. Contains existentially quantifies positions.

The prover knows these operations directly; the private record only marks the type for it. See verification modes for what each mode does with ghost code.

Sets

Functional.Sets gives extensional finite sets. Length is cardinality; Element names some member by an otherwise meaningless positive position. A position is a quantification device, not sorted/insertion order or a stable cursor. Contains, Is_Empty, Empty_Set, subset <= and equality describe membership.

Add requires absence and room below Natural'Last, then preserves all old members and adds only the chosen item. Remove requires presence, removes that item and preserves the others. Union requires a conservative sum-of-lengths bound; Intersection has no analogous growth precondition. Their contracts combine inclusion and membership predicates, not a promised iteration order. Choose requires nonempty and returns a member; it does not promise which one.

Included_Except permits one named exception to a subset relation. Included_In_Union constrains all output members to either source. Includes_Intersection requires all common source members to be present in the destination. No_Overlap means disjointness. These are ghost Boolean helpers; read argument direction carefully when writing preservation lemmas.

Multisets and permutations

Functional.Multisets counts how many times each element is there. Count is 0 for an element that is not; Distinct_At names the elements that are, by a position that means nothing. Add and Remove change one element's count by one and keep every other's (Same_Except); = means the same count for every element. A proof that one collection is a permutation of another states that its counts are the other's.

Generic_Stable_Sort sorts an array by an integer key and returns Moved, where each element came from: Is_Permutation (Moved) and A (I) = A'Old (Moved (I)) are the permutation, and elements of equal key keep their order.

Maps

Functional.Maps models unique keys associated with values. Key_At and Element_At use the same arbitrary positive position and require it present. Has_Key, Is_Empty and Get supply membership/lookup; Get requires presence and agrees with the value paired with that key. Empty_Map is empty. Keys_Included compares only key domains, Same_Keys also compares cardinality, and Has_Pair compares the pair at a source position. Map <= means all source pairs occur in the destination; equality means the same pairs.

Keys_Included_Except permits one named key to be absent from the destination. Add requires a new key and room, adds a new pair while preserving old paired positions. Set requires an existing key, preserves all keys/positions and changes its value only. Remove requires presence, removes that pair and preserves remaining pairs. Choose returns a present key, not its value, and does not promise a specific key. Although an Add postcondition preserves one representation ordering, general extensional map equality does not make a stable public iteration order.

Footprints

Footprints gives the ghost sets of cells that proof enclaves state their frames with. An enclave type's full view keeps its footprint, a Cells value, in a component its operations maintain; Empty, Contains, Add, Remove, Union, <= and Disjoint describe it, and for all P of S => ... states something of every cell. A footprint takes no storage in the program.

Bounded vectors/lists and ordered sets expose ghost functional models; bounded maps expose a key-sequence model; arenas expose a functional map from valid handles to values. Unbounded containers mostly expose actual array-valued models, a different public type. Select the matching model package and its equality/quantification rules.

Vada: package Model is new Verada.Containers.Functional.Sequences (Integer);. Varc: package Model = new Verada.Containers.Functional.Sequences<Integer>;. Vibe does not instantiate library generics; its contracts state properties of its collections directly.

A client proof uses the model laws the packages declare and the prover's built-in treatment of them; it trusts that each container's body keeps its model, which the library's own proof establishes (see library assurance).