### 8.5 Structure Type Property Contracts
> ```
(struct-type-property/c value-contract)  →  contract?
  value-contract : contract?
```
Produces a contract for a structure type property. When the contract is applied to a struct type property, it produces a wrapped struct type property that applies value-contract to the value associated with the property when it used to create a new struct type (via struct, make-struct-type, etc).

The struct type property’s accessor function is not affected; if it is exported, it must be protected separately.

As an example, consider the following module. It creates a structure type property, prop, whose value should be a function mapping a structure instance to a numeric predicate. The module also exports app-prop, which extracts the predicate from a structure instance and applies it to a given value.

> ```racket
> > ( module propmod racket ( require racket/contract ) ( define-values ( prop prop? prop-ref ) ( make-struct-type-property ' prop ) ) ( define ( app-prop x v ) ( ( ( prop-ref x ) x ) v ) ) ( provide/contract [ prop? (  →  any/c boolean? ) ] [ prop ( struct-type-property/c (  →  prop? (  →  integer? boolean? ) ) ) ] [ app-prop (  →  prop? integer? boolean? ) ] ) ( provide prop-ref ) )
> ```

The structmod module creates a structure type named s with a single field; the value of prop is a function that extracts the field value from an instance. Thus the field ought to be an integer predicate, but notice that structmod places no contract on s enforcing that constraint.

> ```racket
> > ( module structmod racket ( require ' propmod ) ( struct s ( f ) #:property prop ( lambda ( s ) ( s-f s ) ) ) ( provide ( struct-out s ) ) )
> > ( require ' propmod ' structmod )
> ```

First we create an s instance with an integer predicate, so the constraint on prop is in fact satisfied. The first call to app-prop is correct; the second simply violates the contract of app-prop.

> ```racket
> > ( define s1 ( s even? ) )
> > ( app-prop s1 5 )
> #f
> > ( app-prop s1 ' apple )
> app-prop: contract violation
> expected: integer?
> given: 'apple
> in: the 2nd argument of
> (  →  prop? integer? boolean?)
> contract from: propmod
> blaming: top-level
> (assuming the contract is correct)
> at: eval:2:0
> ```

We are able to create s instances with values other than integer predicates, but applying app-prop on them blames structmod, because the function associated with prop—that is, (lambda (s) (s-f s))—does not always produce a value satisfying (  →  integer? boolean?).

> ```racket
> > ( define s2 ( s "not a fun" ) )
> > ( app-prop s2 5 )
> prop: contract violation
> expected: a procedure
> given: "not a fun"
> in: the range of
> the struct property value of
> (struct-type-property/c
> (  →  prop? (  →  integer? boolean?)))
> contract from: propmod
> blaming: structmod
> (assuming the contract is correct)
> at: eval:2:0
> > ( define s3 ( s list ) )
> > ( app-prop s3 5 )
> prop: contract violation
> expected: boolean?
> given: '(5)
> in: the range of
> the range of
> the struct property value of
> (struct-type-property/c
> (  →  prop? (  →  integer? boolean?)))
> contract from: propmod
> blaming: structmod
> (assuming the contract is correct)
> at: eval:2:0
> ```

The fix would be to propagate the obligation inherited from prop to s:

> ```racket
> ( provide ( contract-out
> [ struct s ( [ f (  →  integer? boolean? ) ] ) ] ) )
> ```

Finally, if we directly apply the property accessor, prop-ref, and then misuse the resulting function, the propmod module is blamed:

> ```racket
> > ( ( prop-ref s3 ) ' apple )
> prop: broke its own contract
> promised: prop?
> produced: 'apple
> in: the 1st argument of
> the struct property value of
> (struct-type-property/c
> (  →  prop? (  →  integer? boolean?)))
> contract from: propmod
> blaming: propmod
> (assuming the contract is correct)
> at: eval:2:0
> ```

The propmod module has an obligation to ensure a function associated with prop is applied only to values satisfying prop?. By directly providing prop-ref, it enables that constraint to be violated (and thus it is blamed), even though the bad application actually occurs elsewhere.

Generally there is no need to provide a structure type property accessor at all; it is typically only used by other functions within the module. But if it must be provided, it should be protected thus:

> ```racket
> ( provide ( contract-out
> [ prop-ref (  →  prop? (  →  prop? (  →  integer? boolean? ) ) ) ] ) )
> ```

------------------------------------------------------------------------

# 8.6 Attaching Contracts to Values

### 8.6 Attaching Contracts to Values

>
<colgroup><col style="width: 100%" /></colgroup>

<colgroup><col style="width: 100%" /></colgroup>

syntax

(contract-in module-path in-out-item ...)

<colgroup><col style="width: 100%" /></colgroup>

syntax

(contract-out unprotected-submodule in-out-item ...)

<colgroup><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /></colgroup>

in-out-item	 	=	 	[id contract-expr]

 	 	|	 	(rename internal-id external-id contract-expr)

 	 	|
( struct id/ignored ( [ id contract-expr ] ... )
> struct-option )

 	 	|	 	#:∃ poly-variables

 	 	|	 	#:exists poly-variables

 	 	|	 	#:∀ poly-variables

 	 	|	 	#:forall poly-variables

unprotected-submodule	 	=

 	 	|	 	#:unprotected-submodule submodule-name

poly-variables	 	=	 	id

 	 	|	 	(id ...)

id/ignored	 	=	 	id

 	 	|	 	(id ignored-id)

struct-option	 	=

 	 	|	 	#:omit-constructor

Use contract-in in require and contract-out in provide (currently only for the same phase level as the provide form; for example, contract-out cannot be nested within for-syntax). Each identifier in contract-out is provided from the enclosing module and each one in contract-in is required from the named module. In addition, uses of the identifies must live up to the contract specified by contract-expr for each export.

The contract-out and contract-in forms treat modules as units of blame. The module that provides each identifier is expected to meet the positive (co-variant) positions of the contract. Each module that imports the provided variable must obey the negative (contra-variant) positions of the contract. Only uses of the contracted variable outside the module that provides them are checked. Inside the providing module, no contract checking occurs.

In a contract-out form, each contract-expr in a contract-out form is effectively moved to the end of the enclosing module, so a contract-expr can refer to variables that are defined later in the same module.

The rename form exports the first variable (the internal name) with the name specified by the second variable (the external name).

The struct form gives contracts to a structure-type definition id, and each field has a contract that dictates the contents of the fields. Unlike a struct definition, however, all of the fields (and their contracts) must be listed. The contract on the fields that the sub-struct shares with its parent are only used in the contract for the sub-struct’s constructor, and the selector or mutators for the super-struct are not provided. The exported structure-type name always doubles as a constructor, even if the original structure-type name does not act as a constructor. If the #:omit-constructor option is present, the constructor is not provided. The second form of id/ignored, which has both id and ignored-id, is deprecated and allowed in the grammar only for backward compatibility, where ignored-id is ignored. The first form should be used instead.

Note that if the struct is created with serializable-struct or define-serializable-struct, contract-out does not protect struct instances that are created via deserialize. Consider using struct-guard/c instead.

The #:∃, #:exists, #:∀, and #:forall clauses define new abstract contracts. The variables are bound in the remainder of the contract-out form to new contracts that hide the values they accept and ensure that the exported functions are treated parametrically. See new-∃/c and new-∀/c for details on how the clauses hide the values.

If #:unprotected-submodule appears, the identifier that follows it is used as the name of a submodule that contract-out generates. The submodule exports all of the names in the contract-out, but without contracts. In particular, the original structure-type name is exported for each struct form, which means #:omit-constructor only omits the extra constructor, if any.

The implementation of contract-out uses syntax-property to attach properties to the code it generates that records the syntax of the contracts in the fully expanded program. Specifically, the symbol 'provide/contract-original-contract is bound to vectors of two elements, the exported identifier and a syntax object for the expression that produces the contract controlling the export.

Examples:

> ```racket
> > ( module math-example racket/base ( require racket/contract ) ; Compute the reciprocal of a real number ( define ( recip x ) ( / 1 x ) ) ( provide ( contract-out [ recip (  →  ( and/c real? ( not/c zero? ) ) real? ) ] ) ) )
> > ( require ' math-example )
> > ( recip 3 )
> 1/3
> > ( recip 1+2i )
> recip: contract violation
> expected: real?
> given: 1+2i
> in: an and/c case of
> the 1st argument of
> (  →  (and/c real? (not/c zero?)) real?)
> contract from: math-example
> blaming: top-level
> (assuming the contract is correct)
> at: eval:2:0
> ```

Changed in version 7.3.0.3 of package base: Added #:unprotected-submodule.
Changed in version 7.7.0.9: Started ignoring ignored-id.
Changed in version 8.12.0.13: Added contract-in
Changed in version 8.13.0.1: Added rename and struct to contract-in

>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(recontract-out id ...)

A provide-spec for use in provide (currently, just like contract-out, only for the same phase level as the provide form).

It re-exports id, but with positive blame associated to the module containing recontract-out instead of the location of the original site of id.

This can be useful when a public module wants to export an identifier from a private module but where any contract violations should be reported in terms of the public module instead of the private one.

Examples:

> ```racket
> > ( module private-implementation racket/base ( require racket/contract ) ( define ( recip x ) ( / 1 x ) ) ( define ( non-zero? x ) ( not ( = x 0 ) ) ) ( provide/contract [ recip (  →  ( and/c real? non-zero? ) ( between/c -1 1 ) ) ] ) )
> > ( module public racket/base ( require racket/contract ' private-implementation ) ( provide ( recontract-out recip ) ) )
> > ( require ' public )
> > ( recip +nan.0 )
> recip: broke its own contract
> promised: (between/c -1 1)
> produced: +nan.0
> in: the range of
> (  →
> (and/c real? non-zero?)
> (between/c -1 1))
> contract from: public
> blaming: public
> (assuming the contract is correct)
> at: eval:3:0
> ```

Replacing the use of recontract-out with just recip would result in a contract violation blaming the private module.

>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(provide/contract unprotected-submodule in-out-item ...)

A legacy shorthand for (provide (contract-out unprotected-submodule in-out-item ...)), except that a contract-expr within provide/contract is evaluated at the position of the provide/contract form instead of at the end of the enclosing module.

>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(struct-guard/c contract-expr ...)

Returns a procedure suitable to be passed as the #:guard argument to struct, serializable-struct (and related forms). The guard procedure ensures that each contract protects the corresponding field values, as long as the struct is not mutated. Mutations are not protected.

Examples:

> ```racket
> > ( struct snake ( weight hungry? ) #:guard ( struct-guard/c real? boolean? ) )
> > ( snake 1.5 "yep" )
> snake, field 2: contract violation
> expected: boolean?
> given: "yep"
> in: boolean?
> contract from: top-level
> blaming: top-level
> (assuming the contract is correct)
> at: eval:2:0
> ```

#### 8.6.1 Nested Contract Boundaries

|  |  |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  (require racket/contract/region) |  package: base |

>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(with-contract blame-id (wc-export ...) free-var-list ... body ...+)

(with-contract blame-id results-spec free-var-list ... body ...+)

<colgroup><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /></colgroup>

wc-export	 	=	 	(id contract-expr)

result-spec	 	=	 	#:result contract-expr

 	 	|	 	#:results (contract-expr ...)

free-var-list	 	=

 	 	|	 	#:freevar id contract-expr

 	 	|	 	#:freevars ([id contract-expr] ...)

Generates a local contract boundary.

The first with-contract form cannot appear in expression position. All names defined within the first with-contract form are visible externally, but those names listed in the wc-export list are protected with the corresponding contract. The body of the form allows definition/expression interleaving if its context does.

The second with-contract form must appear in expression position. The final body expression should return the same number of values as the number of contracts listed in the result-spec, and each returned value is contracted with its respective contract. The sequence of body forms is treated as for let.

The blame-id is used for the positive positions of contracts paired with exported ids. Contracts broken within the with-contract body will use the blame-id for their negative position.

If a free-var-list is given, then any uses of the free variables inside the body will be protected with contracts that blame the context of the with-contract form for the positive positions and the with-contract form for the negative ones.

>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(define/contract id contract-expr free-var-list init-value-expr)

(define/contract (head args) contract-expr free-var-list body ...+)

Works like define, except that the contract contract-expr is attached to the bound value. For the definition of head and args, see define. For the definition of free-var-list, see with-contract.

Examples:

> ```racket
> > ( define/contract distance ( >=/c 0 ) 43.52 )
> > ( define/contract ( furlongs  →  feet fr ) (  →  real? real? ) ( * 660 fr ) )
> ; a contract violation expected here:
> > ( furlongs  →  feet "not a furlong" )
> furlongs  →  feet: contract violation
> expected: real?
> given: "not a furlong"
> in: the 1st argument of
> (  →  real? real?)
> contract from: (function furlongs  →  feet)
> blaming: top-level
> (assuming the contract is correct)
> at: eval:3:0
> ```

The define/contract form treats the individual definition as a contract region. The definition itself is responsible for positive (co-variant) positions of the contract, and references to id outside of the definition must meet the negative positions of the contract. Since the contract boundary is between the definition and the surrounding context, references to id inside the define/contract form are not checked.

Examples:

> ```racket
> ; an unsual predicate that prints when called
> > ( define ( printing-int? x ) ( displayln "I was called" ) ( exact-integer? x ) )
> > ( define/contract ( fact n ) (  →  printing-int? printing-int? ) ( if ( zero? n ) 1 ( * n ( fact ( sub1 n ) ) ) ) )
> > ( fact 5 ) ; only prints twice, not for each recursive call
> I was called I was called
> 120
> ```

If a free-var-list is given, then any uses of the free variables inside the body will be protected with contracts that blame the context of the define/contract form for the positive positions and the define/contract form for the negative ones.

Examples:

> ```racket
> > ( define ( integer  →  binary-string n ) ( number  →  string n 2 ) )
> > ( define/contract ( numbers  →  strings lst ) (  →  ( listof number? ) ( listof string? ) ) #:freevar integer  →  binary-string (  →  exact-integer? string? ) ; mistake, lst might contain inexact numbers ( map integer  →  binary-string lst ) )
> > ( numbers  →  strings ' ( 4.0 3.3 5.8 ) )
> integer  →  binary-string: contract violation
> expected: exact-integer?
> given: 4.0
> in: the 1st argument of
> (  →  exact-integer? string?)
> contract from: top-level
> blaming: (function numbers  →  strings)
> (assuming the contract is correct)
> at: eval:3:0
> ```

> ```
syntax

(struct/contract struct-id ([field contract-expr] ...)
> struct-option ... )
( struct/contract struct-id super-struct-id
> ( [ field contract-expr ] ... )
> struct-option ... )```

Works like [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29), except that the arguments to the constructor, accessors, and mutators are protected by contracts. For the definitions of field and struct-option, see [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29).

The [struct/contract](#%28form._%28%28lib._racket%2Fcontract%2Fregion..rkt%29._struct%2Fcontract%29%29) form only allows a subset of the struct-option keywords: #:mutable, #:transparent, #:auto-value, #:omit-define-syntaxes, and #:property.

Examples:

> ```racket
> > ( struct/contract fruit ( [ seeds number? ] ) )
> > ( fruit 60 )
> #<fruit>
> > ( fruit #f )
> fruit: contract violation
> expected: number?
> given: #f
> in: the 1st argument of
> (-> number? symbol? any)
> contract from: (struct fruit)
> blaming: top-level
> (assuming the contract is correct)
> > ( struct/contract apple fruit ( [ type string? ] ) )
> > ( apple 14 "golden delicious" )
> #<apple>
> > ( apple 5 30 )
> apple: contract violation
> expected: string?
> given: 30
> in: the 2nd argument of
> (-> any/c string? symbol? any)
> contract from: (struct apple)
> blaming: top-level
> (assuming the contract is correct)
> > ( apple #f "granny smith" )
> fruit: contract violation
> expected: number?
> given: #f
> in: the 1st argument of
> (-> number? symbol? any)
> contract from: (struct fruit)
> blaming: top-level
> (assuming the contract is correct)
> ```

> ```
syntax

(define-struct/contract struct-id ([field contract-expr] ...)
> struct-option ... )
( define-struct/contract ( struct-id super-struct-id )
> ( [ field contract-expr ] ... )
> struct-option ... )
```


# 8.5 Structure Type Property Contracts
(parameter)	→
(-> struct fish)```
Bound by [define-syntax-parameter](stxparam.html#%28form._%28%28lib._racket%2Fstxparam..rkt%29._define-syntax-parameter%29%29), this contains information about the current contract region, used by the above forms to determine the candidates for blame assignment.


#### 8.6.2 Low-level Contract Boundaries
> ```
syntax
(define-module-boundary-contract id
> orig-id
> contract-expr
> d-m-b-c-kwd-arg ... )
d-m-b-c-kwd-arg   =   #:name-for-contract name-for-contract-id
    |   #:name-for-blame blame-id
    |   #:srcloc srcloc-expr
    |   #:pos-source pos-source-expr
    |   #:context-limit limit-expr
    |   #:lift-to-end? boolean
    |   #:start-swapped? boolean
```
Defines id to be orig-id, but with the contract contract-expr.

The identifier id is defined as a macro transformer that consults the context of its use to determine the name for negative blame assignment (using the entire module where a reference appears as the negative party).

The name used in the error messages will be orig-id, unless #:name-for-blame is supplied, in which case the identifier following it is used as the name in the error messages.

The contract expression is wrapped in a let to give it a name which will be passed on to the name of the wrapped value in certain situations (e.g., if the contract is a function contract). If name-for-contract-id is supplied, the identifier that follows it is used to name the contract; otherwise orig-id is used.

The source location used in the blame error messages for the location of the place where the contract was put on the value defaults to the source location of the use of define-module-boundary-contract, but can be specified via the #:srcloc argument, in which case it can be any of the things that the third argument to datum  →  syntax can be.

The positive party defaults to the module containing the use of define-module-boundary-contract, but can be specified explicitly via the #:pos-source keyword.

If #:context-limit is supplied, it behaves the same as it does when supplied to contract.

If lift-to-end? is #t or is not supplied, then the contract expression is placed at the end of the enclosing module (using syntax-local-lift-module-end-declaration). If it is supplied and #f, the contract expression is placed where define-module-boundary-contract is placed.

If start-swapped? is #t, then the initial blame object is created in the “swapped?” state, and the pos-source is used as a negative source. This is helpful to get the “contract from:” line in contract violations correct in certain situations. If #:start-swapped? is not supplied, it is treated as if it was supplied as #f.

Examples:

> ```racket
> > ( module server racket/base ( require racket/contract/base ) ( define ( f x ) #f ) ( define-module-boundary-contract g f (  →  integer? integer? ) ) ( provide g ) )
> > ( module client racket/base ( require ' server ) ( define ( clients-fault ) ( g #f ) ) ( define ( servers-fault ) ( g 1 ) ) ( provide servers-fault clients-fault ) )
> > ( require ' client )
> > ( clients-fault )
> g: contract violation
> expected: integer?
> given: #f
> in: the 1st argument of
> (  →  integer? integer?)
> contract from: 'server
> blaming: client
> (assuming the contract is correct)
> at: eval:2:0
> > ( servers-fault )
> g: broke its own contract
> promised: integer?
> produced: #f
> in: the range of
> (  →  integer? integer?)
> contract from: 'server
> blaming: (quote server)
> (assuming the contract is correct)
> at: eval:2:0
> ```

Changed in version 6.7.0.4 of package base: Added the #:name-for-blame argument.
Changed in version 6.90.0.29: Added the #:context-limit argument.
Changed in version 8.13.0.1: Added the #:name-for-contract and #:start-swapped arguments.

>
<colgroup><col style="width: 100%" /></colgroup>

syntax

( contract contract-expr to-protect-expr
> positive-blame-expr negative-blame-expr )

( contract contract-expr to-protect-expr
> positive-blame-expr negative-blame-expr
> #:context-limit limit-expr )

( contract contract-expr to-protect-expr
> positive-blame-expr negative-blame-expr
> value-name-expr source-location-expr )

The primitive mechanism for attaching a contract to a value. The purpose of contract is as a target for the expansion of some higher-level contract specifying form.

The contract expression adds the contract specified by contract-expr to the value produced by to-protect-expr. The result of a contract expression is the result of the to-protect-expr expression, but with the contract specified by contract-expr enforced on to-protect-expr.

The values of positive-blame-expr and negative-blame-expr indicate how to assign blame for positive and negative positions of the contract specified by contract-expr. They may be any value, and are formatted as by display for purposes of contract violation error messages.

If specified, value-name-expr indicates a name for the protected value to be used in error messages. If not supplied, or if value-name-expr produces #f, no name is printed. Otherwise, it is also formatted as by display. More precisely, the value-name-expr ends up in the blame-value field of the blame record, which is used as the first portion of the error message.

Examples:

> ```racket
> > ( contract integer? #f ' pos ' neg ' timothy #f )
> timothy: broke its own contract
> promised: integer?
> produced: #f
> in: integer?
> contract from: pos
> blaming: pos
> (assuming the contract is correct)
> > ( contract integer? #f ' pos ' neg #f #f )
> broke its own contract
> promised: integer?
> produced: #f
> in: integer?
> contract from: pos
> blaming: pos
> (assuming the contract is correct)
> ```

If specified, source-location-expr indicates the source location reported by contract violations. The expression must produce a srcloc structure, syntax object, #f, or a list or vector in the format accepted by the third argument to datum  →  syntax.

If #:context-limit is supplied, the following expression must evaluate to either #f or a natural number. If the expression evaluates to an natural number, the number of layers of context information is limited to at most that many. For example, if the number is 0, no context information is recorded and the error messages do not contain the section that starts with in:.

------------------------------------------------------------------------

# 8.7 Building New Contract Combinators

### 8.7 Building New Contract Combinators

|  |  |
|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  (require [racket/contract/combinator]()) |  package: base |

> ```
(make-contract
   [ #:name name
    #:first-order first-order
    #:late-neg-projection late-neg-proj
    #:collapsible-late-neg-projection collapsible-late-neg-proj
    #:val-first-projection val-first-proj
    #:projection proj
    #:stronger stronger
    #:equivalent equivalent
    #:list-contract? is-list-contract?)

   →   contract?
  name : any/c = 'anonymous-contract
  first-order : (  →   any/c any/c) = (λ (x) #t)
   late-neg-proj   :   (or/c #f (  →   blame? (  →   any/c any/c any/c)))
      =   #f

   collapsible-late-neg-proj   :   (or/c #f (  →   blame? (values (  →   any/c any/c any/c) collapsible-contract?)))
      =   #f

   val-first-proj   :   (or/c #f (  →   blame? (  →   any/c (  →   any/c any/c))))
      =   #f

   proj   :   (  →   blame? (  →   any/c any/c))
      =   ( λ ( b )
> ( λ ( x )
> ( if ( first-order x )
> x
> ( raise-blame-error
> b x
> ' ( expected: "~a" given: "~e" )
> name x ) ) ) )

  stronger : (or/c #f (  →   contract? contract? boolean?)) = #f
  equivalent : (or/c #f (  →   contract? contract? boolean?)) = #f
  is-list-contract? : boolean? = #f

(make-chaperone-contract
   [ #:name name
    #:first-order first-order
    #:late-neg-projection late-neg-proj
    #:collapsible-late-neg-projection collapsible-late-neg-proj
    #:val-first-projection val-first-proj
    #:projection proj
    #:stronger stronger
    #:equivalent equivalent
    #:list-contract? is-list-contract?)

   →   chaperone-contract?
  name : any/c = 'anonymous-chaperone-contract
  first-order : (  →   any/c any/c) = (λ (x) #t)
   late-neg-proj   :   (or/c #f (  →   blame? (  →   any/c any/c any/c)))
      =   #f

   collapsible-late-neg-proj   :   (or/c #f (  →   blame? (values (  →   any/c any/c any/c) collapsible-contract?)))
      =   #f

   val-first-proj   :   (or/c #f (  →   blame? (  →   any/c (  →   any/c any/c))))
      =   #f

   proj   :   (  →   blame? (  →   any/c any/c))
      =   ( λ ( b )
> ( λ ( x )
> ( if ( first-order x )
> x
> ( raise-blame-error
> b x
> ' ( expected: "~a" given: "~e" )
> name x ) ) ) )

  stronger : (or/c #f (  →   contract? contract? boolean?)) = #f
  equivalent : (or/c #f (  →   contract? contract? boolean?)) = #f
  is-list-contract? : boolean? = #f

(make-flat-contract
   [ #:name name
    #:first-order first-order
    #:late-neg-projection late-neg-proj
    #:collapsible-late-neg-projection collapsible-late-neg-proj
    #:val-first-projection val-first-proj
    #:projection proj
    #:stronger stronger
    #:equivalent equivalent
    #:list-contract? is-list-contract?)

   →   flat-contract?
  name : any/c = 'anonymous-flat-contract
  first-order : (  →   any/c any/c) = (λ (x) #t)
   late-neg-proj   :   (or/c #f (  →   blame? (  →   any/c any/c any/c)))
      =   #f

   collapsible-late-neg-proj   :   (or/c #f (  →   blame? (values (  →   any/c any/c any/c) collapsible-contract?)))
      =   #f

   val-first-proj   :   (or/c #f (  →   blame? (  →   any/c (  →   any/c any/c))))
      =   #f

   proj   :   (  →   blame? (  →   any/c any/c))
      =   ( λ ( b )
> ( λ ( x )
> ( if ( first-order x )
> x
> ( raise-blame-error
> b x
> ' ( expected: "~a" given: "~e" )
> name x ) ) ) )

  stronger : (or/c #f (  →   contract? contract? boolean?)) = #f
  equivalent : (or/c #f (  →   contract? contract? boolean?)) = #f
  is-list-contract? : boolean? = #f```

These functions build simple higher-order contracts, [chaperone contracts](contracts.html#%28tech._chaperone._contract%29), and [flat contracts](contracts.html#%28tech._flat._contract%29), respectively. They all take the same set of three optional arguments: a name, a first-order predicate, and a blame-tracking projection. For [make-flat-contract](#%28def._%28%28lib._racket%2Fcontract%2Fcombinator..rkt%29._make-flat-contract%29%29), see also [flat-contract-with-explanation](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._flat-contract-with-explanation%29%29).

The name argument is any value to be rendered using [display](Writing.html#%28def._%28%28quote._~23~25kernel%29._display%29%29) to describe the contract when a violation occurs. The default name for simple higher-order contracts is anonymous-contract, for [chaperone contracts](contracts.html#%28tech._chaperone._contract%29) is anonymous-chaperone-contract, and for [flat contracts](contracts.html#%28tech._flat._contract%29) is anonymous-flat-contract.

The first-order predicate first-order is used to determine which values the contract applies to. This test is used by [contract-first-order-passes?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-first-order-passes~3f%29%29), and indirectly by [or/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29) and [first-or/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._first-or%2Fc%29%29) to determine which higher-order contract to wrap a value with when there are multiple higher-order contracts to choose from. The default value accepts any value, but it must match the behavior of the projection argument (see below for how). The predicate should be influenced by the value of (contract-first-order-okay-to-give-up?) (see it’s documentation for more explanation).

The late-neg-proj argument defines the behavior of applying the contract via a late neg projection. If it is supplied, this argument accepts a [blame object](#%28tech._blame._object%29) that is missing one party (see also [blame-missing-party?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame-missing-party~3f%29%29)). Then it must return a function that accepts both the value that is getting the contract and the name of the missing blame party, in that order. The result must either be the value (perhaps suitably wrapped with a [chaperone](chaperones.html#%28tech._chaperone%29) or [impersonator](chaperones.html#%28tech._impersonator%29) to enforce the contract), or signal a contract violation using [raise-blame-error](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._raise-blame-error%29%29). The default is #f.

The collapsible-late-neg-proj argument takes the place of the late-neg-proj argument for contracts that support collapsing. If it is supplied, this argument accepts a [blame object](#%28tech._blame._object%29) that is missing one party. It must return two values. The first value must be a function that accepts both the value that is getting the contract and the name of the missing blame party, in that order. The second value should be a [collapsible](collapsible.html#%28tech._collapsible._contract%29) representation of the contract.

The projection proj and val-first-proj are older mechanisms for defining the behavior of applying the contract. The proj argument is a curried function of two arguments: the first application accepts a blame object, and the second accepts a value to protect with the contract. The projection must either produce the value, suitably wrapped to enforce any higher-order aspects of the contract, or signal a contract violation using [raise-blame-error](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._raise-blame-error%29%29). The default projection produces an error when the first-order test fails, and produces the value unchanged otherwise. The val-first-proj is like late-neg-proj, except with an extra layer of currying.

At least one of the late-neg-proj, proj, val-first-proj, or first-order must be non-#f.

The projection arguments (late-neg-proj, proj, and val-first-proj) must be in sync with the first-order argument. In particular, if the first-order argument returns #f for some value, then the projections must raise a blame error for that value and if the first-order argument returns #t for some value, then the projection must not signal any blame for this value, unless there are higher-order interactions later. In other words, for [flat contracts](contracts.html#%28tech._flat._contract%29), the first-order and projection arguments must check the same predicate. For convenience, the the default projection uses the first-order argument, signalling an error when it returns #f and never signalling one otherwise.

Projections for [chaperone contracts](contracts.html#%28tech._chaperone._contract%29) must produce a value that passes [chaperone-of?](chaperones.html#%28def._%28%28quote._~23~25kernel%29._chaperone-of~3f%29%29) when compared with the original, uncontracted value. Projections for [flat contracts](contracts.html#%28tech._flat._contract%29) must fail precisely when first-order does, and must produce the input value unchanged otherwise. Applying a [flat contract](contracts.html#%28tech._flat._contract%29) may result in either an application of the predicate, or the projection, or both; therefore, the two must be consistent. The existence of a separate projection only serves to provide more specific error messages. Most [flat contracts](contracts.html#%28tech._flat._contract%29) do not need to supply an explicit projection.

The stronger argument is used to implement [contract-stronger?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-stronger~3f%29%29). The first argument is always the contract itself and the second argument is whatever was passed as the second argument to [contract-stronger?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-stronger~3f%29%29). If no stronger argument is supplied, then a default that compares its arguments with [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) is used for [flat contracts](contracts.html#%28tech._flat._contract%29) and [chaperone contracts](contracts.html#%28tech._chaperone._contract%29). For [impersonator contracts](contracts.html#%28tech._impersonator._contract%29) constructed with [make-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._make-contract%29%29) that do not supply the stronger argument, [contract-stronger?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-stronger~3f%29%29) returns #f.

Similarly, the equivalent argument is used to implement [contract-equivalent?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-equivalent~3f%29%29). If it isn’t supplied or #false is supplied, then [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) is used for chaperone and flat contracts, and ([λ](lambda.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~ce~bb%29%29) (x y) #f) is used otherwise.

The is-list-contract? argument is used by the [list-contract?](contract-utilities.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._list-contract~3f%29%29) predicate to determine if this is a contract that accepts only [list?](pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29) values.

Examples:

> ```racket
> > ( define int/c ( make-flat-contract #:name ' int/c #:first-order integer? ) )
> > ( contract int/c 1 ' positive ' negative )
> 1
> > ( contract int/c "not one" ' positive ' negative )
> eval:4:0: broke its own contract
> promised: int/c
> produced: "not one"
> in: int/c
> contract from: positive
> blaming: positive
> (assuming the contract is correct)
> > ( int/c 1 )
> #t
> > ( int/c "not one" )
> #f
> > ( define int->int/c ( make-contract #:name ' int->int/c #:first-order ( λ ( x ) ( and ( procedure? x ) ( procedure-arity-includes? x 1 ) ) ) #:projection ( λ ( b ) ( let ( [ domain ( ( contract-projection int/c ) ( blame-swap b ) ) ] [ range ( ( contract-projection int/c ) b ) ] ) ( λ ( f ) ( if ( and ( procedure? f ) ( procedure-arity-includes? f 1 ) ) ( λ ( x ) ( range ( f ( domain x ) ) ) ) ( raise-blame-error b f ' ( expected "a function of one argument" given: "~e" ) f ) ) ) ) ) ) )
> > ( contract int->int/c "not fun" ' positive ' negative )
> eval:8:0: broke its own contract;
> promised a function of one argument
> produced: "not fun"
> in: int->int/c
> contract from: positive
> blaming: positive
> (assuming the contract is correct)
> > ( define halve ( contract int->int/c ( λ ( x ) ( / x 2 ) ) ' positive ' negative ) )
> > ( halve 2 )
> 1
> > ( halve 1/2 )
> halve: contract violation
> expected: int/c
> given: 1/2
> in: int->int/c
> contract from: positive
> blaming: negative
> (assuming the contract is correct)
> > ( halve 1 )
> halve: broke its own contract
> promised: int/c
> produced: 1/2
> in: int->int/c
> contract from: positive
> blaming: positive
> (assuming the contract is correct)
> ```

Changed in version 6.0.1.13 of package base: Added the #:list-contract? argument.
Changed in version 6.90.0.30: Added the #:equivalent argument.
Changed in version 7.1.0.10: Added the #:collapsible-late-neg-projection argument.

> ```
(build-compound-type-name c/s ...) → any
  c/s : any/c
```


# 8.5 Structure Type Property Contracts
(parameter)	→```
Converts a regular Racket value into an instance of a contract struct, converting it according to the description of [contracts](contracts.html#%28tech._contract%29).
If v is not one of the coercible values, [coerce-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._coerce-contract%29%29) signals an error, using the first argument in the error message.
> ```
(parameter)	→
(-> listof contract?)
```
(parameter)	→```

Like [coerce-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._coerce-contract%29%29), but requires the result to be a [chaperone contract](contracts.html#%28tech._chaperone._contract%29), not an arbitrary contract.

> ```
(parameter)	→
(-> listof chaperone-contract?)
```
(parameter)	→```
Like [coerce-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._coerce-contract%29%29), but requires the result to be a [flat contract](contracts.html#%28tech._flat._contract%29), not an arbitrary contract.
> ```
(parameter)	→
(-> listof flat-contract?)
```
Like coerce-contracts, but requires the results to be flat contracts, not arbitrary contracts.

> ```
(coerce-contract/f v)   →   (or/c contract? #f)
  v : any/c```

Like [coerce-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._coerce-contract%29%29), but returns #f if the value cannot be coerced to a contract.

> ```
(parameter)	→
(-> skip-projection-wrapper? wrap?)
```


# 8.5 Structure Type Property Contracts
The functions make-chaperone-contract and build-chaperone-contract-property wrap their arguments to ensure that the result of the projections are chaperones of the input. This layer of wrapping can, in some cases, introduce unwanted overhead into contract checking. If this parameter’s value is #t during the dynamic extent of the call to either of those functions, the wrapping (and thus the checks) are skipped.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(with-contract-continuation-mark blame body ...)

(with-contract-continuation-mark blame+neg-party body ...)

Inserts a continuation mark that informs the contract profiler (see the contract profiling documentation) that contract checking is happening. For the costs from checking your new combinator to be included, you should wrap any deferred, higher-order checks with this form. First-order checks are recognized automatically and do not require this form.
If your combinator’s projections operate on complete blame objects (i.e., no missing blame parties), the blame object should be the first argument to this form. Otherwise (e.g., in the case of late-neg projections), a pair of the blame object and the missing party should be used instead.
Added in version 6.4.0.4 of package base.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax


# 8.5 Structure Type Property Contracts

(contract-pos/neg-doubling e1 e2)

Some contract combinators need to build projections for subcontracts with both regular and blame-swaped versions of the blame that they are given in order to check both access and mutations (e.g., vector/c and vectorof). In the case that such combinators are nested deeply inside each other, there is a potential for an exponential explosion of nested projections being built.
To avoid that explosion, wrap each of the calls to the blame-accepting portion of the combinator in contract-pos/neg-doubling. It returns three values. The first is a boolean, indicating how to interpret the other two results. If the boolean is #t, then the other two results are the values of e1 and e2 and we are not too deep in the nesting. If the boolean is #f, then we have passed a threshold and it is not safe to evaluate e1 and e2 yet, as we are in danger of running into the exponential slowdown. In that case, the last two results are thunks that, when invoked, compute the values of e1 and e2.
As an example, vectorof uses contract-pos/neg-doubling wrapping its two calls to the blame-accepting part of the projection for its subcontract. When it receives a #f as that first boolean, it does not invoke the thunks right away, but waits until the interposition procedure that it attaches to the chaperoned vector is called. Then it invokes them (and caches the result). This delays the construction of the projections until they are actually needed, avoiding the exponential blowup.


# 8.5 Structure Type Property Contracts
Added in version 6.90.0.27 of package base.


#### 8.7.1 Blame Objects
This section describes blame objects and operations on them.
> ```
(blame? v) → boolean?
  v : any/c```
This predicate recognizes [blame objects](#%28tech._blame._object%29).
> ```
(raise-blame-error b
    #:missing-party missing-party
    v
    fmt
    v-fmt ...)    →    none/c
  b : blame?
  missing-party : #f
  v : any/c
   fmt   :   ( or/c string?
> ( listof ( or/c string?
> ' given ' given:
> ' expected ' expected: ) ) )
  v-fmt : any/c
```
Signals a contract violation. The first argument, b, records the current blame information, including positive and negative parties, the name of the contract, the name of the value, and the source location of the contract application. The #:missing-party argument supplies one of the blame parties. It should be non-#f when the b object was created without supplying a negative party. See blame-add-missing-party and the description of the late-neg-proj argument of make-contract.

The second positional argument, v, is the value that failed to satisfy the contract.

The remaining arguments are a format string, fmt, and its arguments, v-fmt ..., specifying an error message specific to the precise violation.

If fmt is a list, then the elements are concatenated together (with spaces added, unless there are already spaces at the ends of the strings), after first replacing symbols with either their string counterparts, or replacing 'given with "produced" and 'expected with "promised", depending on whether or not the b argument has been swapped or not (see blame-swap).

If fmt contains the symbols 'given: or 'expected:, they are replaced like 'given and 'expected are, but the replacements are prefixed with the string "\\n  " to conform to the error message guidelines in Error Message Conventions.

> ```
(blame-add-context blame
    context
   [ #:important important
    #:swap? swap?)    →    blame?

  blame : blame?
  context : (or/c string? #f)
  important : (or/c string? #f) = #f
  swap? : boolean? = #f```

Adds some context information to blame error messages that explicates which portion of the contract failed (and that gets rendered by [raise-blame-error](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._raise-blame-error%29%29)).

The context argument describes one layer of the portion of the contract, typically of the form "the 1st argument of" (in the case of a function contract) or "a conjunct of" (in the case of an [and/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29) contract).

For example, consider this contract violation:

> ```racket
> > ( define/contract f ( list/c ( -> integer? integer? ) ) ( list ( λ ( x ) x ) ) )
> > ( ( car f ) #f )
> f: contract violation
> expected: integer?
> given: #f
> in: the 1st argument of
> the 1st element of
> (list/c (-> integer? integer?))
> contract from: (definition f)
> blaming: top-level
> (assuming the contract is correct)
> at: eval:2:0
> ```

It shows that the portion of the contract being violated is the first occurrence of [integer?](number-types.html#%28def._%28%28quote._~23~25kernel%29._integer~3f%29%29), because the [->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) and the [list/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._list%2Fc%29%29) combinators each internally called [blame-add-context](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame-add-context%29%29) to add the two lines following “in” in the error message.

The important argument is used to build the beginning part of the contract violation. The last important argument that gets added to a [blame object](#%28tech._blame._object%29) is used. The [class/c](Object_and_Class_Contracts.html#%28form._%28%28lib._racket%2Fclass..rkt%29._class%2Fc%29%29) contract adds an important argument, as does the [->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) contract (when [->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) knows the name of the function getting the contract).

The swap? argument has the effect of calling [blame-swap](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame-swap%29%29) while adding the layer of context, but without creating an extra [blame object](#%28tech._blame._object%29).

Passing #f as the context string argument is no longer relevant. For backwards compatibility, [blame-add-context](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame-add-context%29%29) returns b when context is #f.

Changed in version 6.90.0.29 of package base: The context argument being #f is no longer relevant.

> ```
(blame-context blame)  →  (listof string?)
  blame : blame?
```


# 8.5 Structure Type Property Contracts
Returns the context information that would be supplied in an error message, if blame is passed to raise-blame-error.
> ```
(blame-positive b)   →   any/c
  b : blame?
(blame-negative b)   →   any/c
  b : blame?```
These functions produce printable descriptions of the current positive and negative parties of a [blame object](#%28tech._blame._object%29).
> ```
(blame-contract b)  →  any/c
  b : blame?
```
This function produces a description of the contract associated with a blame object (the result of contract-name).

> ```
(blame-value b)   →   any/c
  b : blame?```

This function produces the name of the value to which the contract was applied, or #f if no name was provided.

> ```
(blame-source b)  →  srcloc?
  b : blame?
```
This function produces the source location associated with a contract. If no source location was provided, all fields of the structure will contain #f.
> ```
(blame-swap b)  →  blame?
  b : blame?
```

This function swaps the positive and negative parties of a [blame object](#%28tech._blame._object%29). (See also [blame-add-context](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame-add-context%29%29).)

> ```
(blame-original? b)  →  boolean?
  b : blame?

(blame-swapped? b)  →  boolean?
  b : blame?
```


# 8.5 Structure Type Property Contracts
These functions report whether the current blame of a given blame object is the same as in the original contract invocation (possibly of a compound contract containing the current one), or swapped, respectively. Each is the negation of the other; both are provided for convenience and clarity.
> ```
(blame-replace-negative b neg)   →   blame?
  b : blame?
  neg : any/c```
Produces a [blame?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame~3f%29%29) object just like b except that it uses neg instead of the negative position b has.
> ```
(blame-replaced-negative? b)  →  boolean?
  b : blame?
```
Returns #t if b is the result of calling blame-replace-negative (or the result of some other function whose input was the result of blame-replace-negative).

> ```
(blame-update b pos neg)   →   blame?
  b : blame?
  pos : any/c
  neg : any/c```

Produces a [blame?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame~3f%29%29) object just like b except that it adds pos and neg to the positive and negative parties of b respectively.

> ```
(blame-missing-party? b)  →  boolean?
  b : blame?
```
Returns #t when b does not have both parties.
> ```
(blame-add-missing-party b missing-party)
  →  (and/c blame? (not/c blame-missing-party?))
  b : (and/c blame? blame-missing-party?)
  missing-party : any/c


# 8.5 Structure Type Property Contracts
```

Produces a new [blame object](#%28tech._blame._object%29) like b, except that the missing party is replaced with missing-party.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>struct</p>

<table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td>([struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29)</td><td> </td><td>[exn:fail:contract:blame](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._exn~3afail~3acontract~3ablame%29%29) [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) (object)</td></tr></tbody></table></blockquote></td></tr><tr><td>    #:extra-constructor-name [make-exn:fail:contract:blame](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._make-exn~3afail~3acontract~3ablame%29%29))</td></tr><tr><td>  object : [blame?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame~3f%29%29)</td></tr></tbody></table>

This exception is raised to signal a contract error. The object field contains a [blame object](#%28tech._blame._object%29) associated with a contract violation.

> ```
(current-blame-format)	→
(-> blame? any/c string? string?)
(current-blame-format proc) → void?
    proc	:
(-> blame? any/c string? string?)
```
(c)	→
(-> / x 2)```


# 8.5 Structure Type Property Contracts
These functions build the arguments for [prop:contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._prop~3acontract%29%29), [prop:chaperone-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._prop~3achaperone-contract%29%29), and [prop:flat-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._prop~3aflat-contract%29%29), respectively.
A contract property specifies the behavior of a structure when used as a contract. It is specified in terms of seven properties:
-   get-name which produces a description to [write](Writing.html#%28def._%28%28quote._~23~25kernel%29._write%29%29) as part of a contract violation and defaults to a function that always produces 'anonymous-contract, 'anonymous-chaperone-contract, or 'anonymous-flat-contract;
-   get-first-order, which produces a first-order predicate to be used by [contract-first-order-passes?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-first-order-passes~3f%29%29);
-   late-neg-proj, which produces a blame-tracking projection defining the behavior of the contract (The get-projection and val-first-proj arguments also specify the projection, but using a different signature. They are here for backwards compatibility.);
-   collapsible-late-neg-proj, similar to late-neg-proj which produces a blame-tracking projection defining the behavior of the contract, this function additionally specifies the [collapsible](collapsible.html#%28tech._collapsible._contract%29) behavior of the contract;


# 8.5 Structure Type Property Contracts
-   stronger, a predicate that determines whether this contract (passed in the first argument) is stronger than some other contract (passed in the second argument) and whose default always returns #f;
-   equivalent, a predicate that determines whether this contract (passed in the first argument) is equivalent to some other contract (passed in the second argument); the default for flat and chaperone contracts is [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) and for impersonator contracts returns #f;
-   generate, which returns a thunk that generates random values matching the contract (using [contract-random-generate-fail](Random_generation.html#%28def._%28%28lib._racket%2Fcontract..rkt%29._contract-random-generate-fail%29%29)) to indicate failure) or #f to indicate that random generation for this contract isn’t supported;
-   exercise, which returns a function that exercises values matching the contract (e.g., if it is a function contract, it may call the function) and a list of contracts whose values will be generated by this process;
-   and is-list-contract?, which is used by [flat-contract?](contract-utilities.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._flat-contract~3f%29%29) to determine if this contract accepts only [list?](pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29)s.
At least one of the late-neg-proj, collapsible-late-neg-proj, get-projection, val-first-proj, or get-first-order must be non-#f.


# 8.5 Structure Type Property Contracts
These accessors are passed as (optional) keyword arguments to [build-contract-property](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._build-contract-property%29%29), and are applied to instances of the appropriate structure type by the contract system. Their results are used analogously to the arguments of [make-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._make-contract%29%29).
A chaperone contract property specifies the behavior of a structure when used as a chaperone contract. It is specified using [build-chaperone-contract-property](#%28def._%28%28lib._racket%2Fcontract%2Fcombinator..rkt%29._build-chaperone-contract-property%29%29), and accepts exactly the same set of arguments as [build-contract-property](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._build-contract-property%29%29). The only difference is that the projection accessor must return a value that passes [chaperone-of?](chaperones.html#%28def._%28%28quote._~23~25kernel%29._chaperone-of~3f%29%29) when compared with the original, uncontracted value.
A flat contract property specifies the behavior of a structure when used as a [flat contract](contracts.html#%28tech._flat._contract%29). It is specified using [build-flat-contract-property](#%28def._%28%28lib._racket%2Fcontract%2Fcombinator..rkt%29._build-flat-contract-property%29%29), and accepts similar arguments as [build-contract-property](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._build-contract-property%29%29). The differences are:


# 8.5 Structure Type Property Contracts
-   the projection accessor is expected not to wrap its argument in a higher-order fashion, analogous to the constraint on projections in [make-flat-contract](#%28def._%28%28lib._racket%2Fcontract%2Fcombinator..rkt%29._make-flat-contract%29%29);
-   the #:exercise keyword argument is omitted because it is not relevant for flat contracts.
Changed in version 6.0.1.13 of package base: Added the #:list-contract? argument.
Changed in version 6.1.1.4: Allow generate to return [contract-random-generate-fail](Random_generation.html#%28def._%28%28lib._racket%2Fcontract..rkt%29._contract-random-generate-fail%29%29).
Changed in version 6.90.0.30: Added the #:equivalent argument.
Changed in version 7.1.0.10: Added the #:collapsible-late-neg-projection argument.
> ```
(contract-property? v)  →  boolean?
  v : any/c
(chaperone-contract-property? v)  →  boolean?
  v : any/c
(flat-contract-property? v)  →  boolean?
  v : any/c


# 8.5 Structure Type Property Contracts
```
(module)	→
(->)```

Returns #t if the contract c1 accepts either fewer or the same set of values that c2 does.

[Chaperone contracts](contracts.html#%28tech._chaperone._contract%29) and [flat contracts](contracts.html#%28tech._flat._contract%29) that are the same (i.e., where c1 is [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) to c2) are considered to always be stronger than each other.

This function is conservative, so it may return #f when c1 does, in fact, accept fewer values.

Examples:

> ```racket
> > ( contract-stronger? integer? integer? )
> #t
> > ( contract-stronger? ( between/c 25 75 ) ( between/c 0 100 ) )
> #t
> > ( contract-stronger? ( between/c 0 100 ) ( between/c 25 75 ) )
> #f
> > ( contract-stronger? ( between/c -1 0 0 ) ( between/c 0 10 ) )
> #f
> > ( contract-stronger? ( λ ( x ) ( and ( real? x ) ( <= x 0 ) ) ) ( λ ( x ) ( and ( real? x ) ( <= x 100 ) ) ) )> #f
> ```

> ```
(contract-equivalent? c1 c2)  →  boolean?
  c1 : contract?
  c2 : contract?
```
Returns #t if the contract c1 accepts the same set of values that c2 does.
Chaperone contracts and flat contracts that are the same (i.e., where c1 is equal? to c2) are considered to always be equivalent to each other.
This function is conservative, so it may return #f when c1 does, in fact, accept the same set of values that c2 does.
Examples:


# 8.5 Structure Type Property Contracts
> ```racket
> > ( contract-equivalent? integer? integer? )
> #t
> > ( contract-equivalent? ( non-empty-listof integer? ) ( cons/c integer? ( listof integer? ) ) )
> #t
> > ( contract-equivalent? ( λ ( x ) ( and ( real? x ) ( and ( number? x ) ( >= ( sqr x ) 0 ) ) ) ) ( λ ( x ) ( and ( real? x ) ( real? x ) ) ) )
> #f
> ```
Added in version 6.90.0.30 of package base.
> ```
(contract-first-order-passes? contract v) → boolean?
  contract : contract?
  v : any/c```
Returns a boolean indicating whether the first-order tests of contract pass for v.
If it returns #f, the contract is guaranteed not to hold for that value; if it returns #t, the contract may or may not hold. If the contract is a first-order contract, a result of #t guarantees that the contract holds.
See also contract-first-order-okay-to-give-up? and contract-first-order-try-less-hard.
> ```
(contract-first-order c)  →  (  →   any/c boolean?)
  c : contract?
```
Produces the first-order test used by or/c to match values to higher-order contracts.

------------------------------------------------------------------------

# 8.8 Contract Utilities

### 8.8 Contract Utilities

> ```
(contract? v)   →   boolean?
  v : any/c```

Returns #t if its argument is a [contract](contracts.html#%28tech._contract%29) (i.e., constructed with one of the combinators described in this section or a value that can be used as a contract) and #f otherwise.

> ```
(chaperone-contract? v)  →  boolean?
  v : any/c
```


# 8.5 Structure Type Property Contracts
Returns #t if its argument is a chaperone contract, i.e., one that guarantees that it returns a value which passes chaperone-of? when compared to the original, uncontracted value.
> ```
(impersonator-contract? v)   →   boolean?
  v : any/c```
Returns #t if its argument is an [impersonator contract](contracts.html#%28tech._impersonator._contract%29), i.e., a [contract](contracts.html#%28tech._contract%29) that is neither a [chaperone contract](contracts.html#%28tech._chaperone._contract%29) nor a [flat contract](contracts.html#%28tech._flat._contract%29).
> ```
(flat-contract? v)  →  boolean?
  v : any/c


# 8.5 Structure Type Property Contracts
```
Returns #t when its argument is a contract that can be checked immediately (unlike, say, a function contract).

For example, flat-contract constructs flat contracts from predicates, and symbols, booleans, numbers, and other ordinary Racket values (that are defined as contracts) are also flat contracts.

> ```
(list-contract? v)   →   boolean?
  v : any/c```

Recognizes certain [contract?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract~3f%29%29) values that accept [list?](pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29)s.

A list contract is one that insists that its argument is a [list?](pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29), meaning that the value cannot be cyclic and must either be the empty list or a pair constructed with [cons](pairs.html#%28def._%28%28quote._~23~25kernel%29._cons%29%29) and another list.

Added in version 6.0.1.13 of package base.

> ```
(contract-name c)  →  any/c
  c : contract?
```
Produces the name used to describe the contract in error messages.
> ```
(value-contract v)  →  (or/c contract? #f)
  v : has-contract?


# 8.5 Structure Type Property Contracts
```

Returns the contract attached to v, if recorded. Otherwise it returns #f.

To support [value-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._value-contract%29%29) and [value-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._value-contract%29%29) in your own contract combinators, use [prop:contracted](Building_New_Contract_Combinators.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._prop~3acontracted%29%29) or [impersonator-prop:contracted](Building_New_Contract_Combinators.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._impersonator-prop~3acontracted%29%29).

> ```
(has-contract? v)  →  boolean?
  v : any/c
```
Returns #t if v is a value that has a recorded contract attached to it.
> ```
(value-blame v)  →  (or/c blame? #f)
  v : has-blame?


# 8.5 Structure Type Property Contracts
```

Returns the [blame object](Building_New_Contract_Combinators.html#%28tech._blame._object%29) for the contract attached to v, if recorded. Otherwise it returns #f.

To support [value-contract](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._value-contract%29%29) and [value-blame](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._value-blame%29%29) in your own contract combinators, use [prop:blame](Building_New_Contract_Combinators.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._prop~3ablame%29%29) or [impersonator-prop:blame](Building_New_Contract_Combinators.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._impersonator-prop~3ablame%29%29).

Added in version 6.0.1.12 of package base.

> ```
(has-blame? v)  →  boolean?
  v : any/c
```
Returns #t if v is a value that has a contract with blame information attached to it.
Added in version 6.0.1.12 of package base.
> ```
(contract-late-neg-projection c)
  →  (  →   blame? (  →   any/c (or/c #f any/c) any/c))
  c : contract?


# 8.5 Structure Type Property Contracts
```

Produces the projection defining a contract’s behavior.

The first argument, [blame?](Building_New_Contract_Combinators.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame~3f%29%29) object encapsulates information about the contract checking, mostly used to create a meaningful error message if a contract violation is detected. The resulting function’s first argument is the value that should have the contract and its second argument is a missing party for the [blame object](Building_New_Contract_Combinators.html#%28tech._blame._object%29), to be passed to [raise-contract-error](Legacy_Contracts.html#%28def._%28%28lib._racket%2Fcontract..rkt%29._raise-contract-error%29%29).

If possible, use this function instead of [contract-val-first-projection](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-val-first-projection%29%29) or [contract-projection](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-projection%29%29).

> ```
(contract-projection c)  →  (  →   blame? (  →   any/c any/c))
  c : contract?
```
Produces a projection defining a contract’s behavior. This projection is a curried function of two arguments: the first application accepts a blame object, and the second accepts a value to protect with the contract.
If possible, use contract-late-neg-projection instead.
> ```
(contract-val-first-projection c)
   →   (  →   blame? (  →   any/c (  →   any/c any/c)))
  c : contract?```


# 8.5 Structure Type Property Contracts
Produces a projection defining a contract’s behavior. This projection is similar to the result of [contract-late-neg-projection](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-late-neg-projection%29%29) except with an extra layer of currying.
If possible, use [contract-late-neg-projection](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract-late-neg-projection%29%29) instead.
> ```
(make-none/c sexp-name)  →  contract?
  sexp-name : any/c
```
(not-a-contract)	→
(-> or/c #f #t 0 1)```

Prints c to p using the contract’s name.

Added in version 6.1.1.5 of package base.

> ```
(rename-contract contract name)  →  contract?
  contract : contract?
  name : any/c
```
Produces a contract that acts like contract but with the name name.
The resulting contract is a flat contract if contract is a flat contract.
Added in version 6.3 of package base.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(contract-first-order-okay-to-give-up?)

This form returns a boolean that controls the result of first-order contact checks. More specifically, if it returns #t, then a first-order check may return #t even when the entire first-order checks have not happened. If it returns #f then the first order checks must continue until a definitive answer is returned.
This will only return #t in the dynamic extent of or/c or first-or/c’s checking to determine which branch to use.


# 8.5 Structure Type Property Contracts
Added in version 6.3.0.9 of package base.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(contract-first-order-try-less-hard e)

Encourages first-order checks that happen in the dynamic-extent of e to be more likely to give up. That is, makes it more likely that contract-first-order-okay-to-give-up? might return #t.
If not in the dynamic-extent of or/c’s or first-or/c’s checking to determine the branch, then this form has no effect.
Added in version 6.3.0.9 of package base.
> ```
(if/c predicate then-contract else-contract) → contract?
  predicate : (  →   any/c any/c)
  then-contract : contract?
  else-contract : contract?```
Produces a contract that, when applied to a value, first tests the value with predicate; if predicate returns true, the then-contract is applied; otherwise, the else-contract is applied. The resulting contract is a [flat contract](contracts.html#%28tech._flat._contract%29) if both then-contract and else-contract are [flat contracts](contracts.html#%28tech._flat._contract%29).
For example, the following contract enforces that if a value is a procedure, it is a thunk; otherwise it can be any (non-procedure) value:


# 8.5 Structure Type Property Contracts
> ([if/c](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._if%2Fc%29%29) [procedure?](procedures.html#%28def._%28%28quote._~23~25kernel%29._procedure~3f%29%29) ([->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) [any](data-structure-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%29%29)) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29))
Note that the following contract is not equivalent:
> ([or/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29) ([->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) [any](data-structure-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%29%29)) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29)) ; wrong!
The last contract is the same as [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29) because [or/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29) tries [flat contracts](contracts.html#%28tech._flat._contract%29) before higher-order contracts.
Added in version 6.3 of package base.
>
<colgroup><col style="width: 100%" /></colgroup>

value


# 8.5 Structure Type Property Contracts

[failure-result/c](#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._failure-result%2Fc%29%29) : [contract?](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract~3f%29%29)

A contract that describes the failure result arguments of procedures such as [hash-ref](hashtables.html#%28def._%28%28quote._~23~25kernel%29._hash-ref%29%29).
Equivalent to ([if/c](#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._if%2Fc%29%29) [procedure?](procedures.html#%28def._%28%28quote._~23~25kernel%29._procedure~3f%29%29) ([->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) [any](data-structure-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%29%29)) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29)).
Added in version 6.3 of package base.
> ```
(get/build-val-first-projection c)
  →  (  →   blame? (  →   any/c (  →   any/c any/c)))
  c : contract?


# 8.5 Structure Type Property Contracts
```
Returns the val-first projection for c.

See make-contract for more details.

Added in version 6.1.1.5 of package base.

> ```
(get/build-late-neg-projection c)
   →   (  →   blame? (  →   any/c any/c any/c))
  c : contract?```

Returns the late-neg projection for c.

If c does not have a late-neg contract, then this function uses the original projection for it and logs a warning to the 'racket/contract logger.

See [make-contract](Building_New_Contract_Combinators.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fprop..rkt%29._make-contract%29%29) for more details.

Added in version 6.2.900.11 of package base.

------------------------------------------------------------------------

# 8.9 racket/contract/base

### 8.9 [racket/contract/base]()

|                                                                                |                                                                                |
|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/contract/base]()) |  package: [base](https://pkgs.racket-lang.org/package/base "Install this package using `raco pkg install base`") |

The [racket/contract/base]() module provides a subset of the exports of [racket/contract](contracts.html) module. In particular, it contains everything in the

-   [Data-structure Contracts](data-structure-contracts.html)

-   [Function Contracts](function-contracts.html)

-   [Attaching Contracts to Values](attaching-contracts-to-values.html) and

-   [Contract Utilities](contract-utilities.html) sections.

Unfortunately, using [racket/contract/base]() does not yield a significantly smaller memory footprint than [racket/contract](contracts.html), but it can still be useful to add contracts to libraries that [racket/contract](contracts.html) uses to implement some of the more sophisticated parts of the contract system.

------------------------------------------------------------------------

# 8.10 Collapsible Contracts

### 8.10 Collapsible Contracts

|                                                                                |                                                                                |
|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/contract/collapsible]()) |  package: [base](https://pkgs.racket-lang.org/package/base "Install this package using `raco pkg install base`") |

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](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fvector..rkt%29._vectorof%29%29), [vector/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fvector..rkt%29._vector%2Fc%29%29), and [->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) 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](#%28def._%28%28lib._racket%2Fcontract%2Fcollapsible..rkt%29._collapsible-ho%2Fc%29%29) represent higher-order contracts (e.g., [->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29)) and the [tree leaves](#%28def._%28%28lib._racket%2Fcontract%2Fcollapsible..rkt%29._collapsible-leaf%2Fc%29%29) represent sequences of flat contracts. Two trees can collapse into one tree via the [merge](#%28def._%28%28lib._racket%2Fcontract%2Fcollapsible..rkt%29._merge%29%29) procedure, which removes unnecessary flat contracts from the leaves.

For more information on the motivation and design of collapsible contracts, see \[[Feltey18](doc-bibliography.html#%28cite._.Feltey18%29)\]. For the theoretical foundations, see \[[Greenberg15](doc-bibliography.html#%28cite._.Greenberg15%29)\].

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

> ```
(get/build-collapsible-late-neg-projection c)
  →  (  →   blame? (values (  →   any/c any/c any/c) collapsible-contract?))
  c : contract?
```


# 8.5 Structure Type Property Contracts
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.
>
<colgroup><col style="width: 100%" /></colgroup>

value

collapsible-contract-continuation-mark-key

 : continuation-mark-key?

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.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(with-collapsible-contract-continuation-mark body ...)

Inserts a continuation mark that informs the contract profiler that the current contract is collapsible.
>
<colgroup><col style="width: 100%" /></colgroup>

value

prop:collapsible-contract : struct-type-property?

Structures implementing this property are usable as collapsible contracts. The value associated with this property should be constructed by calling build-collapsible-contract-property.
> ```
(collapsible-contract? v) → boolean?
  v : any/c```


# 8.5 Structure Type Property Contracts
A predicate recognizing structures with the [prop:collapsible-contract](#%28def._%28%28lib._racket%2Fcontract%2Fcollapsible..rkt%29._prop~3acollapsible-contract%29%29) property.
> ```
(merge new-cc new-neg old-cc old-neg)  →  collapsible-contract?
  new-cc : collapsible-contract?
  new-neg : any/c
  old-cc : collapsible-contract?
  old-neg : any/c
```
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.

> ```
(collapsible-guard cc val neg-party)   →   any/c
  cc : collapsible-contract?
  val : any/c
  neg-party : any/c```

Similar to a [late neg projection](Building_New_Contract_Combinators.html#%28tech._late._neg._projection%29), this function guards the value val with the collapsible contract cc.

> ```
(collapsible-contract-property? v)  →  boolean?
  v : any/c
```
This predicate indicates that a value can be used as the property for prop:collapsible-contract.
> ```
(build-collapsible-contract-property
   [ #:try-merge try-merge
    #:collapsible-guard collapsible-guard)
   →   collapsible-contract-property?
   try-merge   :   ( or/c #f
> (  →  collapsible-contract?
> any/c
> collapsible-contract?
> any/c
> ( or/c #f collapsible-contract? ) ) )   =   #f
   collapsible-guard   :   (  →   collapsible-contract? any/c any/c any/c)
      =   ( λ ( cc v neg )
> ( error
> "internal error: contract does not support `collapsible-guard`" cc ) )```


# 8.5 Structure Type Property Contracts
Constructs a collapsible contract property from a merging function and a guard. The try-merge argument is similar to [merge](#%28def._%28%28lib._racket%2Fcontract%2Fcollapsible..rkt%29._merge%29%29), 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.
>
<colgroup><col style="width: 100%" /></colgroup>

struct

([struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29) [collapsible-ho/c](#%28def._%28%28lib._racket%2Fcontract%2Fcollapsible..rkt%29._collapsible-ho%2Fc%29%29) (latest-blame missing-party latest-ctc))

  latest-blame : [blame?](Building_New_Contract_Combinators.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame~3f%29%29)

  missing-party : [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29)

  latest-ctc : [contract?](contract-utilities.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract~3f%29%29)


# 8.5 Structure Type Property Contracts
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.
>
<colgroup><col style="width: 100%" /></colgroup>

struct

([struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29)	 	[collapsible-leaf/c](#%28def._%28%28lib._racket%2Fcontract%2Fcollapsible..rkt%29._collapsible-leaf%2Fc%29%29)	 (	proj-list

 	 	 	 	contract-list

 	 	 	 	blame-list

 	 	 	 	missing-party-list))

  proj-list : ([listof](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._listof%29%29) ([->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29)))

  contract-list : ([listof](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._listof%29%29) [contract?](contract-utilities.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._contract~3f%29%29))

  blame-list : ([listof](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._listof%29%29) [blame?](Building_New_Contract_Combinators.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fblame..rkt%29._blame~3f%29%29))

  missing-party-list : ([listof](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._listof%29%29) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29))


# 8.5 Structure Type Property Contracts
A structure representing the leaf nodes of a collapsible contract. The proj-list field holds a list of partially applied [late neg projections](Building_New_Contract_Combinators.html#%28tech._late._neg._projection%29). The contract-list, blame-list, and missing-party-list fields hold a list of contracts, blame objects, and blame missing parties respectively.
> ```
value
impersonator-prop:collapsible : impersonator-property?
(has-impersonator-prop:collapsible? v)  →  boolean?
  v : any/c
(get-impersonator-prop:collapsible v)  →  collapsible-property?
  v : any/c


# 8.5 Structure Type Property Contracts
```
An impersonator property (and its accessors) that should be attached to chaperoned or impersonated values that are guarded with a collapsible contract.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>struct</p>

<p>(struct collapsible-property (c-c neg-party ref))</p></blockquote></td></tr><tr><td>  c-c : collapsible-contract?</td></tr><tr><td>  neg-party : any/c</td></tr><tr><td>  ref : (or/c #f impersonator?)</td></tr></tbody></table>

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.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>struct</p>

<table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td>(struct</td><td> </td><td>collapsible-count-property collapsible-property</td><td> (</td><td>count</td></tr><tr><td> </td><td> </td><td> </td><td> </td><td>prev))</td></tr></tbody></table></blockquote></td></tr><tr><td>  count : natural-number/c</td></tr><tr><td>  prev : (or/c collapsible-count-property? any/c)</td></tr></tbody></table>

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.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>struct</p>

<table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td>(struct</td><td> </td><td>collapsible-wrapper-property collapsible-property</td></tr><tr><td> </td><td> </td><td>(checking-wrapper)</td></tr></tbody></table></blockquote></td></tr><tr><td>  checking-wrapper : impersonator?</td></tr></tbody></table>

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.

------------------------------------------------------------------------

# 8.11 Legacy Contracts

### 8.11 Legacy Contracts

> ```
(make-proj-contract name proj first-order) → contract?
  name : any/c
   proj   :   ( or/c (  →  any/c
> any/c
> ( list/c any/c any/c )
> contact?
> (  →  any/c any/c ) )
> (  →  any/c
> any/c
> ( list/c any/c any/c )
> contact?
> boolean?
> (  →  any/c any/c ) ) )

  first-order : (  →   any/c boolean?)```

Builds a contract using an old interface.

Modulo errors, it is equivalent to:

> ```racket
> ( make-contract
> #:name name
> #:first-order first-order
> #:projection
> ( cond
> [ ( procedure-arity-includes? proj 5 )
> ( lambda ( blame )
> ( proj ( blame-positive blame )
> ( blame-negative blame )
> ( list ( blame-source blame ) ( blame-value blame ) )
> ( blame-contract blame )
> ( not ( blame-swapped? blame ) ) ) ) ]
> [ ( procedure-arity-includes? proj 4 )
> ( lambda ( blame )
> ( proj ( blame-positive blame )
> ( blame-negative blame )
> ( list ( blame-source blame ) ( blame-value blame ) )
> ( blame-contract blame ) ) ) ] ) )
> ```

> ```
(raise-contract-error val
    src
    pos
    name
    fmt
    arg ...)    →    any/c

  val : any/c
  src : any/c
  pos : any/c
  name : any/c
  fmt : string?
  arg : any/c
```


# 8.5 Structure Type Property Contracts
(parameter)	→
(-> * (symbol? symbol? (or/c syntax? (list/c any/c any/c)))
    (boolean?)
    (→ any/c any))```
Constructs an old-style projection from a contract.
The resulting function accepts the information that is in a blame struct and returns a projection function that checks the contract.
------------------------------------------------------------------------


# 8.12 Random generation


### 8.12 Random generation
> ```
(contract-random-generate ctc [fuel fail) → any/c
  ctc : contract?
  fuel : 5 = exact-nonnegative-integer?
  fail : (or/c #f (  →   any) (  →   boolean? any)) = #f
```
(parameter)	→```

Attempts to get the vals to break their contracts (if any).

Uses [value-contract](contract-utilities.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fguts..rkt%29._value-contract%29%29) to determine if any of the vals have a contract and, for those that do, uses information about the contract’s shape to poke and prod at the value. For example, if the value is function, it will use the contract to tell it what arguments to supply to the value.

The argument fuel determines how hard [contract-exercise](#%28def._%28%28lib._racket%2Fcontract..rkt%29._contract-exercise%29%29) tries to break the values. It controls both the number of exercise iterations and the size of the intermediate values generated during the exercises.

The argument shuffle? controls whether [contract-exercise](#%28def._%28%28lib._racket%2Fcontract..rkt%29._contract-exercise%29%29) randomizes the exercise order or not. If shuffle? is not #f, [contract-exercise](#%28def._%28%28lib._racket%2Fcontract..rkt%29._contract-exercise%29%29) would shuffle the order of the contracts in each exercise iteration.

Examples:

> ```racket
> > ( define/contract ( returns-false x ) ( -> integer? integer? ) ; does not obey its contract #f )
> > ( contract-exercise returns-false )
> returns-false: broke its own contract
> promised: integer?
> produced: #f
> in: the range of
> (-> integer? integer?)
> contract from: (function returns-false)
> blaming: (function returns-false)
> (assuming the contract is correct)
> at: eval:2:0
> > ( define/contract ( calls-its-argument-with-eleven f ) ( -> ( -> integer? integer? ) boolean? ) ; f returns an integer, but ; we're supposed to return a boolean ( f 11 ) )
> > ( contract-exercise calls-its-argument-with-eleven )
> calls-its-argument-with-eleven: broke its own contract
> promised: boolean?
> produced: 11
> in: the range of
> (-> (-> integer? integer?) boolean?)
> contract from:
> (function calls-its-argument-with-eleven)
> blaming: (function calls-its-argument-with-eleven)
> (assuming the contract is correct)
> at: eval:4:0
> ```

Changed in version 7.0.0.18 of package base: Added the shuffle? optional argument.

> ```
(contract-random-generate/choose c fuel)  →  (or/c #f (  →   c))
  c : contract?
  fuel : exact-nonnegative-integer?
```


# 8.12 Random generation
This function is like contract-random-generate, but it is intended to be used with combinators that generate values based on sub-contracts they have. It must be called when contract-random-generate (and contract-exercise) creates the generators. To be more precise, contract-random-generate/choose is available only for the generate and exercise arguments in build-contract-property, build-chaperone-contract-property or build-flat-contract-property and only during the dynamic extent of the call to generate (and exercise). That is, after it receives the c and fuel arguments and before it returns the thunk (or the exerciser).
contract-random-generate/choose will never fail, but it might escape back to an enclosing call or to the original call to contract-random-generate.
It chooses one of several possible generation strategies, and thus it may not actually use the generator associated with c, but might instead use a stashed value that matches c that it knows about via contract-random-generate-stash.
Added in version 6.1.1.5 of package base.
>
<colgroup><col style="width: 100%" /></colgroup>

value

contract-random-generate-fail : contract-random-generate-fail?

An atomic value that is used to indicate that a generator failed to generate a value.
Added in version 6.1.1.5 of package base.
> ```
(contract-random-generate-fail? v) → boolean?
  v : any/c```


# 8.12 Random generation
A predicate to recognize [contract-random-generate-fail](#%28def._%28%28lib._racket%2Fcontract..rkt%29._contract-random-generate-fail%29%29).
Added in version 6.1.1.5 of package base.
> ```
(contract-random-generate-env? v)  →  boolean?
  v : any/c
```
(parameter)	→```

This should be called with values that the program under test supplies during contract generation. For example, when ([->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) ([->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) [integer?](number-types.html#%28def._%28%28quote._~23~25kernel%29._integer~3f%29%29) [integer?](number-types.html#%28def._%28%28quote._~23~25kernel%29._integer~3f%29%29)) [integer?](number-types.html#%28def._%28%28quote._~23~25kernel%29._integer~3f%29%29)) is generated, it may call its argument function. That argument function may return an integer and, if so, that integer should be saved by calling [contract-random-generate-stash](#%28def._%28%28lib._racket%2Fcontract..rkt%29._contract-random-generate-stash%29%29), so it can be used by other integer generators.

Added in version 6.1.1.5 of package base.

> ```
(contract-random-generate-get-current-environment)	→
```
Returns the environment currently being used for generation. This function can be called only during the dynamic extent of contract generation. It is intended to be grabbed during the construction of a contract generator and then used with contract-random-generate-stash while generation is happening.


# 8.12 Random generation
Added in version 6.1.1.5 of package base.
------------------------------------------------------------------------


# 9 Pattern Matching


## 9 Pattern Matching
> > > Pattern Matching in The Racket Guide introduces pattern matching.
The match form and related forms support general pattern matching on Racket values. See also Regular Expressions for information on regular-expression matching on strings, bytes, and streams.
|  |  |
|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  (require [racket/match]()) |  package: base |
The bindings documented in this section are provided by the [racket/match]() and racket libraries, but not racket/base.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax


# 9 Pattern Matching

(match val-expr clause ...)

<colgroup><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /></colgroup>

clause	 	=	 	[pat option=> option ... body ...+]

option=>	 	=

 	 	|	 	(=> id)

option	 	=	 	#:when cond-expr

 	 	|	 	#:do [do-body ...]


# 9 Pattern Matching
Finds the first pat that matches the result of val-expr, and evaluates the corresponding bodys with bindings introduced by pat (if any). Bindings introduced by pat are not available in other parts of pat. The last body in the matching clause is evaluated in tail position with respect to the match expression.
To find a match, the clauses are tried in order. If no clause matches, then the exn:misc:match? exception is raised.
An optional #:when cond-expr specifies that the pattern should only match if cond-expr produces a true value. cond-expr is in the scope of all of the variables bound in pat. cond-expr must not mutate the object being matched before calling the failure procedure, otherwise the behavior of matching is unpredictable. See also failure-cont, which is a lower-level mechanism achieving the same ends.
Examples:
> ```racket
> > ( define ( m x ) ( match x [ ( list a b c ) #:when ( = 6 ( + a b c ) ) ' sum-is-six ] [ ( list a b c ) ' sum-is-not-six ] ) )
> > ( m ' ( 1 2 3 ) )
> 'sum-is-six
> > ( m ' ( 2 3 4 ) )
> 'sum-is-not-six
> ```
An optional #:do \[do-body ...\] executes do-body forms. In particular, the forms may introduce definitions that are visible in the remaining options and the main clause body. Both #:when and #:do options may appear multiple times
Examples:


# 9 Pattern Matching
> ```racket
> > ( define ( m x ) ( match x [ ( list a b c ) #:do [ ( define sum ( + a b c ) ) ] #:when ( > sum 6 ) ( format "the sum, which is ~a, is greater than 6" sum ) ] [ ( list a b c ) ' sum-is-not-greater-than-six ] ) )
> > ( m ' ( 1 2 3 ) )
> 'sum-is-not-greater-than-six
> > ( m ' ( 2 3 4 ) )
> "the sum, which is 9, is greater than 6"
> ```
An optional (=> id), which must appear immediately after pat, is bound to a failure procedure of zero arguments. id is visible in all clause options and the clause body. If this procedure is invoked, it escapes back to the pattern matching expression, and resumes the matching process as if the pattern had failed to match. The bodys must not mutate the object being matched before calling the failure procedure, otherwise the behavior of matching is unpredictable.
Examples:
> ```racket
> > ( define ( m x ) ( match x [ ( list a b c ) ( => exit ) ( f x exit ) ] [ ( list a b c ) ' sum-is-not-six ] ) )
> > ( define ( f x exit ) ( if ( = 6 ( apply + x ) ) ' sum-is-six ( exit ) ) )
> > ( m ' ( 1 2 3 ) )
> 'sum-is-six
> > ( m ' ( 2 3 4 ) )
> 'sum-is-not-six
> ```
The grammar of pat is as follows, where non-italicized identifiers are recognized symbolically (i.e., not by binding).