Okay, let's break down why you're seeing that error.
You've correctly identified the environment procedure from the R7RS specification (Section 6.12). However, there's a crucial detail about where standard Scheme procedures reside:
- Standard Libraries: The R7RS organizes many standard procedures into libraries. The
environment procedure, along with eval, belongs to the (scheme eval) library.
- Default REPL Environment: When you start a Scheme interpreter's Read-Eval-Print Loop (REPL), it typically loads a base set of procedures, often corresponding to the
(scheme base) library or an implementation-specific REPL library (like (scheme repl)). The (scheme eval) library is not usually loaded by default.
- Unbound Variable: Because the
(scheme eval) library hasn't been loaded into your current REPL environment, the name environment is not defined. Hence, the interpreter reports it as an "Unbound variable".
How to Fix It:
You need to explicitly import the (scheme eval) library into your current environment before you can use environment.
; First, import the library containing 'environment'
> (import (scheme eval))
; Now you can use the procedure
> (define my-env (environment (gambit))) ; Assuming (gambit) is a valid import set in Gambit
; You can inspect the environment specifier (its representation is implementation-dependent)
> my-env
#<environment> ; Or something similar
; You could potentially use this environment with eval (though you need eval too, which is also in (scheme eval))
> (eval '(+ 1 2) my-env)
3
Explanation of (gambit):
The argument (gambit) you provided to environment is intended as an import set specifier. According to R7RS section 5.6.1, implementations can define their own names for import sets that represent implementation-specific bindings. (gambit) is likely Gambit's way of specifying the core set of bindings available in the Gambit Scheme system. The environment procedure then creates a new, immutable environment containing only those bindings imported from the (gambit) set.
In Summary:
The error occurs because environment is part of the (scheme eval) library, which needs to be explicitly imported using (import (scheme eval)) before you can use the environment procedure in the REPL.