
;;; Simple renaming examples for B&L

What rename-inputs does is to change how a module sees itself.  It
looks up the new names in self (looking in from the outside) and
renames those (using record-extract-as) to the old names which the
module expected to see.  These get layered onto the "exterior view".
So if we rename A to be B, it will look up B in the outside and
present it to the expressions which have subject to renaming as A.  So
when they look up A, they actually find B.

Understanding rename is also good for understanding freeze.

;------------------------

;;; Rename outputs example

(define m1
  (make-module
   'a (lambda (x) (+ x b))
   'b 19))
;Value: m1

(define m2
  (make-module
   'b 50))
;Value: m2

(define m3
  (module-layer
   m2
   (module-rename-outputs '((c . b)) m1)))
;Value: m3

(define mi3 (module-instantiate m3))
;Value: mi3

(define a3 (record-select 'a mi3))
;Value: a3

(a3 5)
;Value: 55

mi3
;Value: ((b . 50) (c . 19) (a . #[compound-procedure 2]))

(pp (unhash 2))
(lambda (x) (+ x b))
;No value

(where (procedure-environment (unhash 2)))

Environment created by a make-environment special form
Depth (relative to initial environment): 0
 has bindings:

b = 50
c = 19
a = #[compound-procedure 2]

;Quit!

;------------------------

;;; Rename inputs example

(define m1
  (make-module
   'a (lambda (x) (+ x b))
   'b 19))
;Value: m1

(define m4
  (module-layer
   (make-module 'c 50)
   (module-rename-inputs '((c . b)) m1)))
;Value: m4

(define mi4 (module-instantiate m4))
;Value: mi4

(define a4 (record-select 'a mi4))
;Value: a4

(a4 5)
;Value: 55

mi4
;Value: ((c . 50) (b . 19) (a . #[compound-procedure 4]))

(pp (unhash 4))
(lambda (x) (+ x b))
;No value

(where (procedure-environment (unhash 4)))

Environment created by a make-environment special form
Depth (relative to initial environment): 0
 has bindings:

b = 50
c = 50
a = #[compound-procedure 4]

;Quit!

;------------------------

;;; Rename (both) example

(define m1
  (make-module
   'a (lambda (x) (+ x b))
   'b 19))
;Value: m1

(define m5
  (module-union
   (make-module 'b 50)
   (module-rename '((c . b)) m1)))
;Value: m5

(define mi5 (module-instantiate m5))
;Value: mi5

(define a5 (record-select 'a mi5))
;Value: a5

(a5 5)
;Value: 24

mi5
;Value: ((b . 50) (c . 19) (a . #[compound-procedure 6]))

(pp (unhash 6))
(lambda (x) (+ x b))
;No value

(where (procedure-environment (unhash 6)))

Environment created by a make-environment special form
Depth (relative to initial environment): 0
 has bindings:

b = 19
c = 19
a = #[compound-procedure 6]

;Quit!

