[CM] Variables in R7RS libraries

Rudolf Adamkovič salutis at me.com
Thu Dec 8 03:38:55 PST 2022


bil at ccrma.Stanford.EDU writes:

> [...] please send me a working example of the problem,
> and I'll look at it.

I apologize for the lack of clarity!

Below, I include a working minimal working example.

FILE 1: ./experiment/count.scm

## (define-library (experiment count)
## 
##   (export count-value
##           count-value-get
##           count-value-increment!)
##           
##   (import (scheme base))
##   
##   (begin
##   
##     (define count-value 1)
##     
##     (define (count-value-get)
##       count-value)
##       
##     (define (count-value-increment!)
##       (set! count-value (+ count-value 1)))))

FILE 2: ./experiment/user.scm

## (define-library (experiment user)
## 
##   (export user-value-get
##           user-value-increment!)
##           
##   (import (scheme base)
##           (experiment count))
##           
##   (begin
##     (define (user-value-get)
##       count-value)
##       
##     (define (user-value-increment!)
##       (count-value-increment!))))

FILE 3: ./experiment.scm

## (import (experiment count)
##         (experiment user))
## 
## ;; Increment the count from 1 to 2 using the exported procedure.
## (count-value-increment!)
## 
## ;; Direct access of the exported variable.
## ;; ERROR: Returns 1 instead of 2.
## (display count-value)
## (newline)
## 
## ;; Indirect access via the exported accessor.
## ;; OK: Returns 2 as expected.
## (display (count-value-get))
## (newline)
## 
## ;; Doubly indirect access via another library's accessor.
## ;; ERROR: Returns 1 instead of 2.
## (display (user-value-get))
## (newline)

In other words, we expect exactly one instance of the `count-value' to
ever exist across all scopes that import the library.

More generally, for any imported library, one and only one instance of
it and its exported bindings exists in the program.

P.S. In Guile, executed with `guile -L . experiment.scm', the above
program prints `2' three times, as expected.

Rudy
-- 
"Chop your own wood and it will warm you twice."
-- Henry Ford; Francis Kinloch, 1819; Henry David Thoreau, 1854

Rudolf Adamkovič <salutis at me.com> [he/him]
Studenohorská 25
84103 Bratislava
Slovakia



More information about the Cmdist mailing list