The dynamic environment is the structure which allows the system to find the value returned by the standard procedures current-input-port and current-output-port. The standard procedures with-input-from-file and with-output-to-file extend the dynamic environment to produce a new dynamic environment which is in effect for the dynamic extent of the call to the thunk passed as their last argument. These procedures are essentially special purpose dynamic binding operations on hidden dynamic variables (one for current-input-port and one for current-output-port). Gambit generalizes this dynamic binding mechanism to allow the user to introduce new dynamic variables, called parameter objects, and dynamically bind them. The parameter objects implemented by Gambit are compatible with the specification of the “Parameter objects SRFI” (SRFI 39).
One important issue is the relationship between the dynamic environments of the parent and child threads when a thread is created. Each thread has its own dynamic environment that is accessed when looking up the value bound to a parameter object by that thread. When a thread’s dynamic environment is extended it does not affect the dynamic environment of other threads. When a thread is created it is given a dynamic environment whose bindings are inherited from the parent thread. In this inherited dynamic environment the parameter objects are bound to the same cells as the parent’s dynamic environment (in other words an assignment of a new value to a parameter object is visible in the other thread).
Another important issue is the interaction between the dynamic-wind procedure and dynamic environments. When a thread creates a continuation, the thread’s dynamic environment and the ‘dynamic-wind’ stack are saved within the continuation (an alternate but equivalent point of view is that the ‘dynamic-wind’ stack is part of the dynamic environment). When this continuation is invoked the required ‘dynamic-wind’ before and after thunks are called and the saved dynamic environment is reinstated as the dynamic environment of the current thread. During the call to each required ‘dynamic-wind’ before and after thunk, the dynamic environment and the ‘dynamic-wind’ stack in effect when the corresponding ‘dynamic-wind’ was executed are reinstated. Note that this specification precisely defines the semantics of calling ‘call-with-current-continuation’ or invoking a continuation within a before or after thunk. The semantics are well defined even when a continuation created by another thread is invoked. Below is an example exercising the subtleties of this semantics.
The following actions will occur when this code is executed: (b1)(a1) is written to “bar”, (b2) is then written to “foo”, (t2) is then written to “baz”, (a2) is then written to “foo”, and finally (b1)(t1)(a1) is written to “bar”.
(make-parameter obj [set-filter [get-filter]]) procedure
The dynamic environment is composed of two parts: the local dynamic environment and the global dynamic environment. There is a single global dynamic environment, and it is used to lookup parameter objects that can’t be found in the local dynamic environment.
The make-parameter procedure returns a new parameter object. The set-filter argument is a one argument “set” conversion procedure. The get-filter argument is a one argument “get” conversion procedure. If they are not specified the conversion procedures default to the identity function.
The global dynamic environment is updated to associate the parameter object to a new cell. The initial content of the cell is the result of applying the “set” conversion procedure to obj.
A parameter object is a procedure which accepts zero or one argument. The cell bound to a particular parameter object in the dynamic environment is accessed by calling the parameter object. When no argument is passed, the value returned is the result of applying the “get” conversion procedure to the content of the cell. When one argument is passed the content of the cell is updated with the result of applying the parameter object’s “set” conversion procedure to the argument. Note that the conversion procedures can be used for guaranteeing the type of the parameter object’s binding and/or to perform some conversion of the value.
(parameterize ((procedure value)…) body) special form
The parameterize form, evaluates all procedure and value expressions in an unspecified order. All the procedure expressions must evaluate to procedures, either parameter objects or procedures accepting zero and one argument. Then, for each procedure p and in an unspecified order:
code
If p is a parameter object a new cell is created, initialized, and bound to the parameter object in the local dynamic environment. The value contained in the cell is the result of applying the parameter object’s “set” conversion procedure to value. The resulting dynamic environment is then used for processing the remaining bindings (or the evaluation of body if there are no other bindings).
Otherwise p will be used according to the following protocol: we say that the call (p) “gets p’s value” and that the call (p x) “sets p’s value to x”. First, the parameterize form gets p’s value and saves it in a local variable. It then sets p’s value to value. It then processes the remaining bindings (or evaluates body if there are no other bindings). Then it sets p’s value to the saved value. These steps are performed in a dynamic-wind so that it is possible to use continuations to jump into and out of the body (i.e. the dynamic-wind’s before thunk sets p’s value to value and the after thunk sets p’s value to the saved value).
The result(s) of the parameterize form are the result(s) of the body.
Note that using procedures instead of parameter objects may lead to unexpected results in multithreaded programs because the before and after thunks of the dynamic-wind are not called when control switches between threads.
This is a detailed excerpt from the Gambit Scheme documentation, specifically about dynamic environments and parameter objects (SRFI 39), as well as the parameterize special form. Here’s a summary and explanation of the key concepts, with clarifications and examples:
Dynamic Environment in Gambit
Dynamic environment: The context that determines the values of dynamic variables (like the current input/output ports) during program execution.
Standard procedures like with-input-from-file and with-output-to-file temporarily change these dynamic variables for the duration of a thunk (a no-argument procedure).
Gambit generalizes this mechanism with parameter objects (SRFI 39), allowing users to create their own dynamically scoped variables.
Parameter Objects
Created with (make-parameter initial-value [set-filter [get-filter]]).
Parameter object is a procedure:
No argument: returns the current value (possibly filtered by get-filter).
One argument: sets the value (possibly filtered by set-filter).
When a thread is created, it inherits the parent’s dynamic environment, but the parameter objects are bound to the same cells (so changes are visible across threads).
Extending a thread’s dynamic environment does not affect others.
Continuations and
User
i dont get it
Assistant
Absolutely! Let’s break it down step by step, with simple language and examples.
1. What is a Dynamic Environment?
Think of the dynamic environment as a set of temporary settings that your program uses while it runs. For example, when you use with-output-to-file, you temporarily change where your program writes output.
Can you explain in real terms in a paragraph when i would actually use this in a real program
Assistant
Absolutely! Here’s a real-world example:
Suppose you’re writing a program that prints numbers in different formats—sometimes in decimal, sometimes in hexadecimal, sometimes in binary. You want most of your program to use decimal, but for a specific function (like printing debug info), you want to temporarily switch to hexadecimal, and then automatically go back to decimal when you’re done. Instead of passing a “format” argument everywhere, you can use a parameter object (like radix). You set it to 10 (decimal) for the whole program, but inside your debug function, you use parameterize to temporarily set it to 16 (hexadecimal). This way, all the code that prints numbers will “see” the right format, and you don’t have to worry about accidentally leaving the program in the wrong state or passing extra arguments everywhere. It’s especially useful in large programs, libraries, or when dealing with things like logging, configuration, or user preferences that might need to change temporarily and safely.
In short:
Parameter objects and parameterize let you temporarily and safely change settings (like number format, file output, or user preferences) for a part of your program, without affecting the rest or having to pass extra arguments everywhere.
User
Provide a simple code sample of your example
Assistant
Certainly! Here’s a simple Scheme code sample that demonstrates using a parameter object to control number formatting, just like in the real-world example above:
scheme
;; Define a parameter object for the number base (radix)
(define radix (make-parameter 10))
;; Function to print a number using the current radix
(define (print-number n)
(display (number->string n (radix)))
(newline))
;; Main program prints in decimal by default
(print-number 42) ; prints "42"
;; Debug function prints in hexadecimal temporarily
(define (debug-print n)
(parameterize ((radix 16))
(print-number n))) ; prints "2a"
;; Call debug-print
(debug-print 42)
;; After debug-print, radix is still 10 (decimal)
(print-number 42) ; prints "42"
Output:
code
42
2a
42
Explanation:
The first print-number prints 42 in decimal.
debug-print temporarily sets the radix to 16, so 42 is printed as "2a" (hexadecimal).
After debug-print, the radix goes back to 10, so 42 is printed as "42" again.
This shows how you can use parameter objects and parameterize to temporarily change settings in a safe and convenient way!
dynamic-wind
When a continuation is captured, the current dynamic environment and the dynamic-wind stack are saved.
When the continuation is invoked, the saved environment and stack are restored.
This ensures correct behavior even with complex control flow (e.g., jumping in/out of dynamic-wind thunks).
parameterize Special Form
Temporarily changes the values of parameter objects (or compatible procedures) for the dynamic extent of body.
(define p
(make-parameter
100
(lambda (val) (pp (list val: val)) (list 0 val))
(lambda (state) (pp (list state: state))
(set-car! state (+ 1 (car state)))
(+ (car state) (cadr state)))))
(p) ; prints (state: (0 100)), returns 101
(p) ; prints (state: (1 100)), returns 102
(p 555) ; prints (val: 555)
(p) ; prints (state: (0 555)), returns 556
Key Takeaways
Parameter objects provide a safe, thread-aware, dynamically scoped variable mechanism.
parameterize is the idiomatic way to temporarily change parameter values.
Filters allow for type enforcement and value transformation.
Dynamic environment is crucial for correct behavior with threads and continuations.
If you have a specific question about this mechanism, or want to see more examples or use-cases, let me know!
value1
param2
value2
just for
body
After body, the old values come back.
5. What About Threads?
If you have multiple threads (like running two things at once), each thread gets its own copy of the dynamic environment. But when a thread is created, it shares the same parameter objects as the parent, so changes can be seen by both.
6. What About Filters?
When you make a parameter, you can add a filter to check or change values.
Example:
scheme
(define write-shared
(make-parameter
#f
(lambda (x)
(if (boolean? x)
x
(error "only booleans are accepted by write-shared")))))
(write-shared #t) ; OK
(write-shared 123) ; ERROR!
This makes sure only booleans can be set.
7. Summary Table
Concept
What it does
Example usage
Dynamic env
Temporary settings for part of your program
with-output-to-file
Parameter obj
Special variable with dynamic scope
(define radix (make-parameter 10))
parameterize
Temporarily change parameter objects
(parameterize ((radix 16)) ...)
Filter
Checks/changes values set to parameter objects
See write-shared example
8. Simple Example
scheme
(define prompt (make-parameter ">" (lambda (x) (if (string? x) x (object->string x)))))
(display (prompt)) ; prints ">"
(parameterize ((prompt "$")) (display (prompt))) ; prints "$"
(display (prompt)) ; prints ">" again
Still confused?
Tell me which part is unclear, or ask for a specific example!