On this page:

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).


(id : type)

Declares that id has type type. This type declaration and the definition of id must be within the same definition sequence, either at the top of a module or together in a set of local definitions.

Added in version 1.1 of package plait.


(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


> (define b : Number (+ 1 2))
> b

- Number


> (define (c x)
    (+ x b))
> (c 3)

- Number


> (define (d [y : Number]) : Number
    (c y))
> (d 4)

- Number



(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


> (define-values ([x : Number] [b : Symbol] [c : String]) t)
> c

- String



(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:

> (define-type Shape
    (circle [radius : Number])
    (rectangle [width : Number]
               [height : Number]))
> (define cr (circle 10))
> cr

- Shape

(circle 10)

> (circle? cr)

- Boolean


> (circle-radius cr)

- Number


> (define rc (rectangle 2 3))
> (+ (rectangle-width rc) (rectangle-height rc))

- Number



(define-type-alias tyid/abs type)

tyid/abs = id
  | (id 'arg-id ...)
Defines a type alias id. When tyid/abs is id, then using id is the same as using type. When tyid/abs is (id 'arg-id ...), then using (id arg-type ...) is the same as using type with each 'arg-id replaced by the corresponding arg-type.

> (define-type-alias Size Number)
> (define (square-area [side : Size])
    (* side side))
> (square-area 10)

- Number


Except for arg-ids, the type form must not reference any type variables that do not yet have a scope.


(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.

> (require (typed-in racket/base [gensym : (-> Symbol)]))
> (gensym)

- Symbol



(trace id ...)

For an introduction, see the tutorial section Testing and Debugging.

Traces subsequent calls—showing arguments and results—for functions bound to the ids. This form can be used only in a module top level, and only for tracing functions defined within the module.


(module id module-path form ...)

Declares a submodule named id, which can be required in the enclosing module using 'id or (submod "." id):

(module sub plait
  (define n 8))
(require 'sub)
(+ n 1)


(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))


(include path-spec)

Copy the content of path-spec in place of the include form, which can only be used in a top-level position.


(define-syntax-rule (id pattern ...) template)


(define-syntax id macro-expr)

(define-syntax (id arg-id) macro-body ...)
macro = (syntax-rules ....)
  | (lambda ....)
Defines a macro. In a macro-expr or macro-body, the bindings of racket/base are available.

A macro of the form

(define-syntax-rule (id pattern ...) template)

is equivalent to

(define-syntax id
  (syntax-rules ()
   [(id pattern ...) template]))


(splice form ...)

Equivalent to the forms sequence in a module or top-level context, which is useful for producing multiple definitions from a macro.