2 Definitions
The body of a plait module is a sequence of definitions, expressions and type declarations. The module implicitly exports all top-level definitions. When a plait module is imported into a module that does not use plait, the imports have contracts (matching reflecting the exported bindings’ types).
syntax
(id : type)
Added in version 1.1 of package plait.
syntax
(define id expr)
(define id : type expr) (define (id id/type ...) expr) (define (id id/type ...) : type expr)
id/type = id | [id : type]
For an introduction, see the tutorial section Definitions.
Defines id.
The expr in each of the first two forms is evaluated to get the value of id. In the first form, the type of id is inferred at the type of expr, while the second form declares a specific type for id.
The third and fourth forms define id as a function, where each id/type is a function argument (with an optional declare type) and expr is the body of the function, which is evaluated when the function is called. In the fourth form, a type before the body expr declares the function’s result type (which must match the type of expr).
Note that the first and second forms of define also define functions in the case that expr produces a function, such as when expr is a lambda form. The third and fourth forms are simplify shorthands for defining a function.
Evaluating a reference to id before its definition is evaluated triggers an “undefined identifier” error.
> (define a 1) > a - Number
1
> (define b : Number (+ 1 2)) > b - Number
3
> (define (c x) (+ x b)) > (c 3) - Number
6
> (define (d [y : Number]) : Number (c y)) > (d 4) - Number
7
syntax
(define-values (id/type ...) expr)
id/type = id | [id : type]
For an introduction, see the tutorial section Tuples and Options.
Defines each id/type (with an optional type declaration) to be the values within the tuple produced by expr, which must have as many values as declared id/types.
> (define t (values 1 'one "One")) > (define-values (a b c) t) > a - Number
1
> (define-values ([x : Number] [b : Symbol] [c : String]) t) > c - String
"One"
syntax
(define-type tyid/abs (variant-id [field-id : type]) ...)
tyid/abs = id | (id 'arg-id ...)
For an introduction, see the tutorial section Datatypes.
Defines a type (when tyid/abs is id) or type constructor (when tyid/abs has the form (id 'id ...)).
A constructor variant-id is defined for each variant. Each constructor takes an argument for each field of its variant, where the type of each field is declared by the type after each field-id. The result type of each constructor is id.
Instances of a type declared with define-type are normally used through type-case.
In addition to the type and constructors, a define-type expression also defines:
for each variant, a predicate variant-id? that returns #t when applied to an instance of variant-id and #f for any other value; and
for each field of each variant, an accessor variant-id-field-id that takes a instance of variant-id and returns the value of the field corresponding to field-id.
> (define-type Shape (circle [radius : Number]) (rectangle [width : Number] [height : Number])) > (define cr (circle 10)) > cr - Shape
(circle 10)
> (circle? cr) - Boolean
#t
> (circle-radius cr) - Number
10
> (define rc (rectangle 2 3)) > (+ (rectangle-width rc) (rectangle-height rc)) - Number
5
syntax
(define-type-alias tyid/abs type)
tyid/abs = id | (id 'arg-id ...)
> (define-type-alias Size Number)
> (define (square-area [side : Size]) (* side side)) > (square-area 10) - Number
100
Except for arg-ids, the type form must not reference any type variables that do not yet have a scope.
syntax
(require spec ...)
spec = module-path | (typed-in module-path [id : type] ...) | (opaque-type-in module-path [type-id predicate-id] ...) | (rename-in spec [orig-id new-id] ...)
For an introduction, see the tutorial section Programs and Modules.
Imports from each module-path.
When a module-path is not wrapped with typed-in or opaque-type-in, then module-path must refer to a module that is implemented with plait.
When module-path is wrapped with typed-in, then only the specified ids are imported from module-path, and the type system assumes (without static or additional dynamic checks) the given type for each id.
When module-path is wrapped with opaque-type-in, then the corresponding type-ids are bound as opaque datatypes, where predicate-id from module-path is a run-time predicate (used for contracts as needed for cooperation with untyped code) for instances of the datatype.
syntax
(trace id ...)
For an introduction, see the tutorial section Testing and Debugging.
Traces subsequent calls—
syntax
(module id module-path form ...)
(module sub plait (define n 8)) (require 'sub) (+ n 1)
syntax
(module+ id form ...)
For an introduction, see the tutorial section Programs and Modules.
Declares/extends a submodule named id, which is particularly useful for defining a test submodule to hold tests that precede relevant definitions (since the submodule implicitly imports the bindings of its enclosing module, and DrRacket or raco test runs the test submodule):
(module+ test (test 11 (add-one 10))) (define (add-one n) (+ 1 n))
syntax
(include path-spec)
syntax
(define-syntax-rule (id pattern ...) template)
syntax
(define-syntax id macro-expr)
(define-syntax (id arg-id) macro-body ...)
macro = (syntax-rules ....) | (lambda ....)
A macro of the form
(define-syntax-rule (id pattern ...) template)
is equivalent to
(define-syntax id (syntax-rules () [(id pattern ...) template]))
syntax
(splice form ...)