8.10 Collapsible Contracts
Added in version 184.108.40.206 of package base.
Collapsible contracts are an optimization in the contract system designed
to avoid a particular pathological build up of contract wrappers on higher-order
values. The vectorof, vector/c, and -> contract
combinators support collapsing for vector contracts and function contracts for
functions returning a single value.
Intuitively, a collapsible contract is a tree structure.
The tree nodes represent higher-order contracts
(e.g., ->) and the tree leaves
represent sequences of flat contracts.
Two trees can collapse into one tree via the merge procedure,
which removes unnecessary flat contracts from the leaves.
For more information on the motivation and design of collapsible contracts,
For the theoretical foundations, see [Greenberg15].
Warning: the features described in this section are experimental
and may not be sufficient to implement new collapsible contracts. Implementing
new collapsible contracts requires the use of unsafe chaperones and impersonators
which are only supported for vector and procedure values. This documentation exists
primarily to allow future maintenance of the racket/contract/collapsible
library. End Warning
Returns the collapsible-late-neg projection for c.
If c does not have a collapsible-late-neg projection,
then this function uses the original projection for it and constructs a leaf
as its collapsible representation.
Key used by continuation marks that are present during collapsible contract checking.
The value of these marks are #t if the current contract is collapsible.
Inserts a continuation mark that informs the contract profiler that the current contract
Structures implementing this property are usable as collapsible contracts. The value
associated with this property should be constructed by calling
Combine two collapsible contracts into a single collapsible contract.
arguments are expected to be
blame parties similar to those passed to a late neg projection
Similar to a late neg projection
, this function guards the value val
with the collapsible contract cc
Constructs a collapsible contract property
from a merging function and a guard.
argument is similar to merge
, but may return #f
of a collapsible contract and may be specialized to a particular collapsible contract.
argument should be specialized to the particular collapsible
contract being implemented.
A common parent structure for collapsible contracts for higher-order values.
The latest-blame field holds the blame object for the most recent
contract attached. Similarly, the missing-party field holds the latest
missing party passed to the contract. The latest-contract field stores
the most recent contract attached to the value.
A structure representing the leaf nodes of a collapsible contract. The proj-list
field holds a list of partially applied late neg projections
. The contract-list
, and missing-party-list
fields hold a list of contracts,
blame objects, and blame missing parties respectively.
An impersonator property (and its accessors) that should be attached to chaperoned or impersonated
values that are guarded with a collapsible contract.
The parent struct of properties that should be attached to chaperones or impersonators
of values protected with a collapsible contract. The c-c field stores the collapsible
contract that is or will in the future be attached to the the value. The neg-party field
stores the latest missing blame party passed to the contract on the value. The ref field
is mutable and stores a reference to the chaperone or impersonator to which this property is
attached. This is necessary to determine whether an unknown chaperone has been attached to a value
after it has been protected by a collapsible contract.
This property is associated with the impersonator-prop:collapsible
the value completely enters the collapsible mode. These properties keep track of the number of
contracts on a value in the count
field, and hold a reference to the previous
in the prev
field or the original value without a contract. This
allows the contract system to traverse the chain of attached contracts and merge them into a single
collapsible contract to protect the original value.
This property is used when a value is guarded by a collapsible contract. The
checking-wrapper field holds a chaperone or impersonator that dispatches to the
collapsible contract stored in this property to perform any necessary contract checks. When
the value receives another contract and merging happens, the checking wrapper will remain the
same even though the specific collapsible contract attached to the value may change.