236: Evaluating expressions in an unspecified order

by Marc Nieper-Wißkirchen

Status

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

Abstract

This SRFI defines the independently syntax, which can be used to combine side effects into one expression without specifying their relative order.

Rationale

The purpose of the begin expression type in the existing Scheme standards is two-fold. Firstly, it can be used to combine several expressions into one expression. Secondly, it is used to sequence the side effects of these expressions by specifying an evaluation order.

In many use cases, only the first property is needed while any specific sequence order would be arbitrary. Thus, many Scheme programs written with begin overspecify the underlying algorithm. As Scheme is a language to write down algorithms as much as it is a practical programming language, this SRFI introduces the independently syntax, which is similar to begin but without any specific evaluation order (and thus no return value(s)).

The practical side of the independently syntax is that it allows a compiler to reorder expressions freely and that it helps documenting code. If expressions appear inside an independently form, the programmer expresses the fact that the relative order of these expressions does not matter. This can make imperatively-written programs easier to understand.

As the result of an independently expression is unspecified, an independently expression, contrary to a begin expression, need not contain any expression. This can make an independently expression useful for macro writers.

Specification

Example

The following procedure could have been written with begin instead of independently but it would have been an overspecification of the underlying algorithmic content.

(define set-car+cdr!
  (lambda (p x y)
    (assert (pair? p))
    (independently
      (set-car! p x)
      (set-cdr! p y))))

Syntax

(independently ⟨expression⟩ …)

Evaluates the ⟨expressions⟩s in an unspecified order and discards their return values. The result of the independently expression is unspecified.

Note: Although the order of evaluation is otherwise unspecified, the effect of any concurrent evaluation of the ⟨expressions⟩ is constrained to be consistent with some sequential order of evaluation. The order of evaluation may be chosen differently for each evaluation of the independently form.

Library name

The SRFI 97-conformant library name of this SRFI is (srfi :236 independently).

Implementation

The following is an R6RS implementation.

(library (srfi :236 independently)
  (export independently)
  (import (rnrs))
  (define-syntax independently
    (lambda (stx)
      (syntax-case stx ()
        [(_ expr ...)
         (with-syntax ([(tmp ...) (generate-temporaries #'(expr ...))])
           #'(let ([tmp (begin expr #f)] ...) (values)))]
        [_ (syntax-violation 'independently "invalid syntax" stx)]))))

And here is an R7RS (small) implementation.

(define-library (srfi 236)
  (export independently)
  (import (scheme base))
  (begin
    (define-syntax independently
      (syntax-rules ()
	((independently expr ...)
	 (independently-aux (expr ...)))))
    (define-syntax independently-aux
      (syntax-rules ()
	((independently-aux () (expr tmp) ...)
	 (let ((tmp (begin expr #f)) ...) (values)))
	((independently-aux (expr . exprs) . binds)
	 (independently-aux exprs (expr tmp) . binds))))))

Acknowledgements

Thanks go to the members of the Scheme Working Group 2 for discussing the idea of the independently expression with me. The name independently was suggested by Lassi Kortela. Special thanks to Göran Weinholt, who tested the previous sample implementation on his Loko compiler and on Chez Scheme.

© 2022 Marc Nieper-Wißkirchen.

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