 no such
variable, it is added first."
  (let ((variable (module-local-variable module name)))
    (if variable
        (begin
          (variable-set! variable value)
          (module-modified module))
        (let ((variable (make-variable value)))
          (module-add! module name variable)))))

;; MODULE-DEFINED? -- exported
;;
(define (module-defined? module name)
  "Return #t iff NAME is defined in MODULE (or in a module that MODULE
uses)."
  (let ((variable (module-variable module name)))
    (and variable (variable-bound? variable))))

(define (module-use! module interface)
  "Add INTERFACE to the list of interfaces used by MODULE."
  (if (not (or (eq? module interface)
               (memq interface (module-uses module))))
      (begin
        ;; Newly used modules must be appended rather than consed, so that
        ;; `module-variable' traverses the use list starting from the first
        ;; used module.
        (set-module-uses! module (append (module-uses module)
                                         (list interface)))
        (hash-clear! (module-import-obarray module))
        (module-modified module))))

(define (module-use-interfaces! module interfaces)
  "Same as MODULE-USE!, but only notifies module observers after all
interfaces are added to the inports list."
  (let* ((cur (module-uses module))
         (new (let lp ((in interfaces) (out '()))
                (if (null? in)
                    (reverse out)
                    (lp (cdr in)
                        (let ((iface (car in)))
                          (if (or (memq iface cur) (memq iface out))
                              out
                              (cons iface out))))))))
    (set-module-uses! module (append cur new))
    (hash-clear! (module-import-obarray module))
    (module-modified module)))

