On this page:
examples
make-base-eval
make-base-eval-factory
make-eval-factory
make-log-based-eval
close-eval
scribble-eval-handler
scribble-exn->string
4.4.1 Legacy Evaluation
interaction
interaction0
interaction/  no-prompt
interaction-eval
interaction-eval-show
racketblock+  eval
racketblock0+  eval
racketmod+  eval
def+  int
defs+  int
examples
examples*
defexamples
defexamples*
as-examples
with-eval-preserve-source-locations

4.4 Evaluation and Examples

The scribble/example library provides utilities for evaluating code at document-build time and incorporating the results in the document, especially to show example uses of defined procedures and syntax.

Added in version 1.16 of package scribble-lib.

syntax

(examples option ... datum ...)

 
option = #:eval eval-expr
  | #:once
  | #:escape escape-id
  | #:label label-expr
  | #:hidden
  | #:result-only
  | #:no-inset
  | #:no-prompt
  | #:preserve-source-locations
  | #:no-result
  | #:lang language-name
Similar to racketinput, except that the result for each input datum is shown on the next line. The result is determined by evaluating the quoted form of the datum using the evaluator produced by eval-expr.

Each keyword option can be provided at most once:

Certain patterns in datum are treated specially:

A datum cannot be a keyword. To specify a datum that is a keyword, wrap it with code:line.

When evaluating a datum produces an error (and datum does not have an eval:error wrapper), an exception is raised by examples.

If the value of current-print in the sandbox is changed from its default value, or if print-as-expression in the sandbox is set to #f, then each evaluation result is formatted to a port by applying (current-print) to the value; the output port is set to a pipe that supports specials in the sense of write-special, and non-character values written to the port are used as content. Otherwise, when the default current-print is in place, result values are typeset using to-element/no-color.

As an example,

#lang scribble/manual
@(require racket/sandbox
          scribble/example)
@(define my-evaluator
   (parameterize ([sandbox-output 'string]
                  [sandbox-error-output 'string]
                  [sandbox-memory-limit 50])
     (make-evaluator 'typed/racket/base)))
 
@examples[#:eval my-evaluator
          (: my-sqr (Real -> Real))
          (define (my-sqr x)
            (* x x))
          (my-sqr 42)]

uses an evaluator whose language is typed/racket/base.

procedure

(make-base-eval [#:pretty-print? pretty-print? 
  #:lang lang] 
  input-program ...) 
  (any/c . -> . any)
  pretty-print? : any/c = #t
  lang : 
(or/c module-path?
      (list/c 'special symbol?)
      (cons/c 'begin list?))
 = '(begin)
  input-program : any/c
Creates an evaluator using (make-evaluator 'racket/base #:lang lang input-program ...), setting sandbox parameters to disable limits, setting the outputs to 'string, and not adding extra security guards.

If pretty-print? is true, the sandbox’s printer is set to pretty-print-handler. In that case, values that are convertible in the sense of convertible? are printed using write-special, except that values that are serializable in the sense of serializable? are serialized for tranfers from inside the sandbox to outside (which can avoid pulling code and support from the sandboxed environment into the document-rendering environment).

Changed in version 1.6 of package scribble-lib: Changed treatment of convertible values that are serializable.

procedure

(make-base-eval-factory mod-paths 
  [#:pretty-print? pretty-print? 
  #:lang lang]) 
  (-> (any/c . -> . any))
  mod-paths : (listof module-path?)
  pretty-print? : any/c = #t
  lang : 
(or/c module-path?
      (list/c 'special symbol?)
      (cons/c 'begin list?))
 = '(begin)
Produces a function that is like make-base-eval, except that each module in mod-paths is attached to the evaluator’s namespace. The modules are loaded and instantiated once (when the returned make-base-eval-like function is called the first time) and then attached to each evaluator that is created.

procedure

(make-eval-factory mod-paths 
  [#:pretty-print? pretty-print? 
  #:lang lang]) 
  (-> (any/c . -> . any))
  mod-paths : (listof module-path?)
  pretty-print? : any/c = #t
  lang : 
(or/c module-path?
      (list/c 'special symbol?)
      (cons/c 'begin list?))
 = '(begin)
Like make-base-eval-factory, but each module in mod-paths is also required into the top-level environment for each generated evaluator.

procedure

(make-log-based-eval log-file mode)  (-> any/c any)

  log-file : path-string?
  mode : (or/c 'record 'replay)
Creates an evaluator (like make-base-eval) that uses a log file to either record or replay evaluations.

If mode is 'record, the evaluator records every interaction to log-file, replacing log-file if it already exists. The result of each interaction must be serializable.

If mode is 'replay, the evaluator uses the contents of log-file instead of actually performing evaluatings. For each interaction, it compares the term to evaluate against the next interaction recorded in log-file. If the term matches, the stored result is returned; if not, the evaluator raises an error indicating that it is out of sync with log-file.

Use make-log-based-eval to document libraries when the embedded examples rely on external features that may not be present or appropriately configured on all machines.

Added in version 1.12 of package scribble-lib.

procedure

(close-eval eval)  (one-of/c "")

  eval : (any/c . -> . any)
Shuts down an evaluator produced by make-base-eval. Use close-eval when garbage collection cannot otherwise reclaim an evaluator (e.g., because it is defined in a module body).

parameter

(scribble-eval-handler)

  ((any/c . -> . any) boolean? any/c . -> . any)
(scribble-eval-handler handler)  void?
  handler : ((any/c . -> . any) boolean? any/c . -> . any)
A parameter that serves as a hook for evaluation. The evaluator to use is supplied as the first argument to the parameter’s value. The second argument is #t if exceptions are being captured (to display exception results), #f otherwise. The third argument is the form to evaluate.

parameter

(scribble-exn->string)  (-> (or/c exn? any/c) string?)

(scribble-exn->string handler)  void?
  handler : (-> (or/c exn? any/c) string?)
A parameter that controls how exceptions are rendered by interaction. Defaults to
(λ (e)
  (if (exn? e)
      (exn-message e)
      (format "uncaught exception: ~s" e)))

4.4.1 Legacy Evaluation

The scribble/eval library provides an older interface to the functionality of scribble/example. The scribble/example library should be used, instead.

In addition to the forms listed below, scribble/eval re-exports several functions from scribble/example: make-base-eval make-base-eval-factory, make-eval-factory, make-log-based-eval, close-eval, and scribble-eval-handler.

syntax

(interaction maybe-options datum ...)

 
maybe-options = maybe-eval
  | maybe-escape
  | maybe-no-errors
     
maybe-eval = 
  | #:eval eval-expr
     
maybe-escape = 
  | #:escape escape-id
     
maybe-no-errors = 
  | #:no-errors? no-errors?-expr
Like examples from scribble/example, except that

Changed in version 1.14 of package scribble-lib: Added #:no-errors?, eval:no-prompt, and eval:error, and changed code:line to support multiple datums.

syntax

(interaction0 maybe-options datum ...)

Like interaction, but without insetting the code via nested.

Use examples with #:no-indent, instead.

syntax

(interaction/no-prompt maybe-eval maybe-escape datum)

Like interaction, but does not render each datum with a prompt.

Use examples with #:no-prompt, instead.

syntax

(interaction-eval maybe-eval datum)

Like interaction, evaluates the quoted form of datum, but returns the empty string and does not catch exceptions (so eval:error has no effect).

Use examples with #:hidden, instead.

syntax

(interaction-eval-show maybe-eval datum)

Like interaction-eval, but produces an element representing the printed form of the evaluation result.

Use examples with #:result-only, instead.

syntax

(racketblock+eval maybe-eval maybe-escape datum ...)

Use examples with #:no-result, instead.

syntax

(racketblock0+eval maybe-eval maybe-escape datum ...)

Use examples with #:no-result and #:no-indent, instead.

syntax

(racketmod+eval maybe-eval maybe-escape name datum ...)

Use examples with #:lang, instead.

syntax

(def+int maybe-options defn-datum expr-datum ...)

Like interaction, except the defn-datum is typeset as for racketblock (i.e., no prompt) and a line of space is inserted before the expr-datums.

syntax

(defs+int maybe-options (defn-datum ...) expr-datum ...)

Like def+int, but for multiple leading definitions.

Use examples with eval:no-prompt wrappers on definitions, instead.

syntax

(examples maybe-options datum ...)

Like interaction, but with an “Examples:” label prefixed.

Use examples from scribble/example, instead.

syntax

(examples* label-expr maybe-options datum ...)

Like examples, but using the result of label-expr in place of the default “Examples:” label.

Use examples from scribble/example with the #:label option, instead.

syntax

(defexamples maybe-options datum ...)

Like examples, but each definition using define or define-struct among the datums is typeset without a prompt, and with line of space after it.

Use examples with eval:no-prompt wrappers on definitions, instead.

syntax

(defexamples* label-expr maybe-options datum ...)

Like defexamples, but using the result of label-expr in place of the default “Examples:” label.

Use examples with the #:label option and eval:no-prompt wrappers on definitions, instead.

procedure

(as-examples b)  block?

  b : block?
(as-examples label b)  block?
  label : (or/c block? content?)
  b : block?
Adds an “examples” label to b, using either a default label or the given label.

By default, the evaluation forms provided by this module, such as interaction and examples, discard the source locations from the expressions they evaluate. Within a with-eval-preserve-source-locations form, the source locations are preserved. This can be useful for documenting forms that depend on source locations, such as Redex’s typesetting macros.

Use examples with the #:preserve-source-locations option, instead.