[plt-scheme] mutually required modules

From: David A. Herman (dherman at ccs.neu.edu)
Date: Tue May 6 14:47:28 EDT 2003

In separating modules between interfaces and implementations, I sometimes
find that I'd like to offer a particular implementation as the default for
convenience, but the mutual reference between modules causes a cycle in
the module resolver. Would it be best to define the one default
implementation in the interface module to avoid the cycle, or to leave out
the default altogether? Or is there some way to allow mutually required
modules?

Here's the simplest example I could come up with: a sort function that
requires a comparison function but chooses one by default from the set of
default implementations:

;; sort.ss ------
(module sort mzscheme
  (require "cmp.ss" (lib "etc.ss"))

  (define-struct employee (name id))

  (define (insert elt l elt<?)
    (cond
      [(null? l) (cons elt null)]
      [(elt<? (car l) elt)
       (cons (car l) (insert elt (cdr l) elt<?))]
      [else (cons elt l)]))

  (define sort
    (opt-lambda (l [elt<? by-id])  ; sort by ID by default
      (if (null? l) null
          (insert (car l) (sort (cdr l) elt<?) elt<?))))

  (provide sort (struct employee (name id))
)

;; cmp.ss -------
(module cmp mzscheme
  (require "sort.ss")

  (define (by-name e1 e2)
    (string<? (employee-name e1)
              (employee-name e2)))

  (define (by-id e1 e2)
    (< (employee-id e1)
       (employee-id e2)))

  (provide by-name by-id)
)
;; --------------

Thanks,

Dave Herman
dherman at ccs.neu.edu


Posted on the users mailing list.