8.10Collapsible Contractsπ i
Added in version 7.1.0.10 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,
see [Feltey18].
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
is collapsible.
Structures implementing this property are usable as collapsible contracts. The value
associated with this property should be constructed by calling
build-collapsible-contract-property .
Combine two collapsible contracts into a single collapsible contract.
The
new-neg and
old-neg 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.
[ #:try-mergetry-merge
#:collapsible-guardcollapsible-guard])
=
"internal error: contract does not support `collapsible-guard`"cc))
Constructs a
collapsible contract property from a merging function and a guard.
The
try-merge argument is similar to
merge , but may return
#f instead
of a collapsible contract and may be specialized to a particular collapsible contract.
The
collapsible-guard 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.
contract-list
blame-list
missing-party-list))
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,
blame-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 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 property before
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
count property 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.