www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

contracts-to-types.scrbl (2023B)


      1 #lang scribble/manual
      2 
      3 @require[(for-label racket/contract/base)
      4          scribble/example]
      5 @title{Using contract syntax to specify types}
      6 
      7 @defmodule[type-expander/contracts-to-types]
      8 
      9 @defform*[{(contract→type contract)
     10            (contract->type contract)}]{
     11 
     12  This is a simple type expander which translates common contracts to types.
     13  Note that it only supports a limited number of contract constructors. The
     14  following are supported: @racket[or/c], @racket[and/c] (the translation may
     15  produce a type too complex for Typed/Racket to understand properly, though),
     16  @racket[listof], @racket[list/c], @racket[*list/c], @racket[vectorof],
     17  @racket[vector/c], @racket[cons/c], @racket[number?], @racket[integer?],
     18  @racket[string?], @racket[symbol?], @racket[char?], @racket[boolean?],
     19  @racket[bytes?], @racket[void?], @racket[null?], @racket[empty?],
     20  @racket[list?], @racket[exact-nonnegative-integer?],
     21  @racket[exact-positive-integer?], @racket[syntax/c], @racket[parameter/c],
     22  @racket[promise/c], @racket[suggest/c], @racket[flat-rec-contract], some uses
     23  of @racket[->] and @racket[->*], @racket['quoted-datum],
     24  @racket[`quasiquoted-datum-with-unquoted-types]. Literal data (numbers,
     25  strings, characters, booleans, byte strings, regular expressions and byte
     26  regular expressions) are also interpreted as singleton types.
     27 
     28  Furthermore, using @racket[,_τ] anywhere outside of a quoted datum will leave
     29  the type @racket[_τ] unchaged, allowing the user to manually convert to types
     30  only the parts which cannot be converted automatically.}
     31 
     32 @defform*[{(:contract→type contract)
     33            (:contract->type contract)}]{
     34                                         
     35  Prints a representation of the contract translated as a type. It is then
     36  possible to copy-paste that result into the code.
     37                                         
     38  @examples[
     39  (require type-expander/lang
     40           racket/contract/base
     41           type-expander/contracts-to-types)
     42  (:contract→type (list/c 1 2 "str" (or/c integer? string?)))]
     43 }