245: Mixing definitions and expressions within bodies

by Daphne Preston-Kendal

Status

This SRFI is currently in withdrawn status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-245@nospamsrfi.schemers.org. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

Abstract

Scheme has traditionally required procedure bodies and the bodies of derived constructs such as let to contain definitions followed by expressions. This SRFI proposes to allow expressions to appear before and intermingled with definitions in such bodies, as was allowed in program bodies by the R6RS and in library bodies by R7RS small.

Rationale

It often makes sense to run type and other basic error checks on input forms before any other code runs (including the right-hand sides of definitions):

(define (double-square x)
  (unless (number? x)
    (error "foo: not a number" x))
  (define y (square x))
  (* 2 y))

It is likewise sometimes useful to insert logging code before the beginning of a procedure before any other code:

(define (dangerous-operation x)
  (log-warn "Beginning dangerous operation on value" x)
  (define prepared-x (prepare-for-dangerous-operation x))
  ...)

When writing test suites it is often beneficial to build up values to be tested and run the tests on them incrementally:

(test-group "basic arithmetic"
  (define one-plus-one (+ 1 1))
  (test 2 one-plus-one)
  (define two-plus-two (+ one-plus-one one-plus-one))
  (test 4 two-plus-two))

The change would also simplify the semantics of R7RS Large by using a single order of macro expansion for all three kinds of bodies (program, library, and procedure bodies).

Specification

All instances of body in the syntax of Scheme forms may contain any number of definitions or expressions followed by at least one expression.

definition or expression ... expression

The final expression becomes the result of evaluating the body; it is in tail position if the body as a whole is in tail position.

It is an error for the evaluation of any expression or of the right-hand side of any definition in the sequence of definition or expressions to use or assign the value of a variable whose definition is to the right of it.

It is an error to invoke the continuation of any variable definition more than once, either directly, or by returning again to the continuation of an expression which appears before a definition and then allowing control flow to continue back to the definition. Implementations are strongly encouraged to detect this situation and signal an error when it occurs.

R6RS-compatible semantics

R6RS specifies that program (top-level) bodies are expanded into the equivalent of a letrec*, turning expressions before the final variable definition in the program body into bindings of dummy variables which bind the equivalent of (begin expression unspecified), where unspecified evaluates without side effects to some unspecified value. This SRFI extends a variant of this process to all bodies.

Namely, expressions appearing before a variable definition are combined in order before the right-hand side expression of the definition of the following variable into the equivalent of a begin form as the initializer expression of variable within the hypothetical letrec*. The following two examples are equivalent under this transformation.

expr_1
(define var_1 (x))
expr_2
expr_3
(define var_2 (y))
(define var_3 (z))
expr_4
expr_5
(letrec* ((var_1 (begin expr_1 (x)))
          (var_2 (begin expr_2 expr_3 (y)))
          (var_3 (z)))
  expr_4
  expr_5)

Implementation

Scheme implementations which already support this SRFI include MIT Scheme, Chicken, Guile, Kawa, and S7.

Acknowledgements

Marc Nieper-Wi├čkirchen suggested this change.

William Gardner Schottstaedt let me know that S7 supported this SRFI, and Per Bothner that Kawa already supports it.

© 2023 Daphne Preston-Kendal.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Editor: Arthur A. Gleckler