[r6rs-discuss] [Formal] SRFI-39 should be made an R6RS library

From: R. Kent Dybvig <dyb>
Date: Tue Feb 20 20:50:26 2007

> However, parameters are neither simple nor general.

Here is an implementation of make-parameter and parameterize in about 20
lines of r5.92rs code. Adding thread parameters requires an additional
few lines of nonstandard code. I suppose whether you'd call the code or
the mechanism it implements "simple" is a matter of taste, but it is
simple to me.

  (define make-parameter
    (case-lambda
      [(init guard)
       (let ([v (guard init)])
         (case-lambda
           [() v]
           [(u) (set! v (guard u))]))]
      [(init) (make-parameter init values)]))
  
  (define-syntax parameterize
    (lambda (x)
      (syntax-case x ()
        [(_ ((x v) ...) e1 e2 ...)
         (with-syntax ([(p ...) (generate-temporaries #'(x ...))]
                       [(y ...) (generate-temporaries #'(x ...))])
           #'(let ([p x] ... [y v] ...)
               (let ([swap (lambda () (let ([t (p)]) (p y) (set! y t)) ...)])
                 (dynamic-wind #t swap (lambda () e1 e2 ...) swap))))])))

The generality derives from the fact that a parameter is a procedure and,
when the parameter is referenced or set, directly or through parameterize,
it can do anything one can imagine a procedure doing.

Anyway, didn't you just complain that parameters are too general?

> Moreover, the particular design you chose is problematic because it
> destroys modularity.

You've offered no evidence to this effect here or in your Scheme workshop
paper, which does not anticipate the design. From what I can make out of
the rather vague concept in your paper, I don't think the design does
destroy modularity.

While reasoning about programs that both parameterize and directly modify
parameters might be difficult, especially when escape procedures and
threads are involved, this is no more so than for other effects.

If the effects are the problem, you can build an abstraction layer that
prohibits direct mutation and thus preserves whatever modularity you think
has been destroyed. Including the general mechanism and allowing people
to create restricted abstractions seems more in the spirit of Scheme than
including one or more of the restricted abstractions.

> In contrast, dynamic binding as a separate
> mechanism is simple and modular. Combined with thread-local storage
> (less simple and not modular, but still simpler than parameters), you
> can build parameters.

I don't see how you could build the full generality of parameters and
parameterize---please enlighten me. It would be enough to demonstrate it
for the single-threaded world using mutable variables or objects in place
of thread-local storage, i.e., any storage that's local to the single
thread of a single-threaded system. If you actually can implement
parameterize and mutable parameters using your dynamic value binding and
some form of state, I wonder in what sense your dyanmic value binding
actually preserves the modularity you want.

Kent
Received on Tue Feb 20 2007 - 20:50:12 UTC

This archive was generated by hypermail 2.3.0 : Wed Oct 23 2024 - 09:15:01 UTC