Re: class precedence list

From: Anthony Beurive' <beurive_at_labri.u-bordeaux.fr>
Date: Thu, 14 Oct 1999 12:43:03 +0200

Erick Gallesio writes:
...
> The problem HERE is that my function is buggy. I had no time to fix it
> right now (but if you have a corrected version I would be happy to
> know it ;-)

Well, I wrote a new procedure ;-) It solves the problem I had with
class precedence. I did not take a look at CLOS or Tiny CLOS, so
maybe my solution is different. Here is it:

(define (compute-cpl class)
  (define (aux inh classes)
    (if (pair? classes)
        (let* ((class (car classes))
               (supers (reverse (slot-ref class 'direct-supers))))
          (aux (let ((syn (aux inh supers)))
                 (if (not (memq class syn)) ; may be done with a hash table
                     (cons class syn)
                     syn))
               (cdr classes)))
        inh))
  (aux '() (list class)))

The principle is a depth-first traversal of the class hierarchy. The
class precedence list is built during this traversal, in the spirit of
inherited and synthesized attributes of attribute grammars. (Don't
read "inherited attribute" like "class inheritance"!)

One thing you could say is that it doesn't behave "by level". It
rather keeps "branches" of class inheritance together. For the class
definitions I had, the result is the same as the one mentioned by Ken
Anderson for Common LISP.

Note the comment about the memq test. This leads to another version
that doesn't use memq:

(define (compute-cpl class)
  (define ht (make-hash-table))
  (define (aux inh classes)
    (if (pair? classes)
        (let* ((class (car classes))
               (supers (reverse (slot-ref class 'direct-supers))))
          (aux (let ((syn (aux inh supers)))
                 (if (not (hash-table-get ht class #f))
                     (begin
                       (hash-table-put! ht class #t)
                       (cons class syn))
                     syn))
               (cdr classes)))
        inh))
  (aux '() (list class)))

You're welcome to do what you want with these procedures.

              ,
Anthony BEURIVE
Received on Thu Oct 14 1999 - 12:43:37 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST