;;;; coops-introspection.scm -*- Scheme -*- ;;;; Kon Lovett, Jul '18 ;;;; Kon Lovett, Jul '17 ;;;; Kon Lovett, Aug '10 ;; Issues ;; ;; - all slot options are "lost" after definition. ;; ;; - generic-procedure introspection is dicey since the generic ;; has no "entity" representation & methods are not 1st-class. ;; ;; - should use more "schemey" names for operations? (module coops-introspection (;export instance-of? class? check-class error-class instance? check-instance error-instance generic? check-generic error-generic method? check-method error-method primitive-instance? class-precedence-list class-direct-supers class-slots class-direct-slots generic-anonymous? generic-name generic-specialized-arguments generic-primary-methods generic-before-methods generic-after-methods generic-around-methods method-specializers method-procedure ) (import scheme) (import (chicken base)) (import (chicken type)) (import (only (srfi 1) every lset-difference lset-union)) (import (only type-checks define-check+error-type)) (import coops) ;;; (define-type coops-class *) (define-type coops-instance *) (define-type coops-generic *) (define-type coops-method *) ;;; Helpers (define +class-primitive+ #t) (: union-class-property-values (procedure (list-of coops-class) -> list)) ; (define (union-class-property-values getter classes) (foldr (lambda (class ls) (lset-union eq? ls (getter class))) '() classes) ) (: *class-slots (coops-class -> list)) ; (define (*class-slots class) (slot-value class 'slots) ) (: *class-supers (coops-class -> list)) ; (define (*class-supers class) (slot-value class 'class-precedence-list) ) (: union-class-slots ((list-of coops-class) -> list)) ; (define (union-class-slots classes) (union-class-property-values *class-slots classes) ) (: union-class-supers ((list-of coops-class) -> list)) ; (define (union-class-supers classes) (union-class-property-values *class-supers classes) ) (: class-supers-slots (coops-class -> list)) ; (define (class-supers-slots class) (union-class-slots (*class-supers class)) ) (: class-supers-supers (coops-class -> list)) ; (define (class-supers-supers class) (union-class-supers (*class-supers class)) ) (: class< (coops-class coops-class -> boolean)) ; ;c1 < c2 (define (class< c1 c2) (subclass? c1 c2) ) (: class<= (coops-class coops-class -> boolean)) ; (define (class<= c1 c2) (or (eq? c1 c2) (class< c1 c2)) ) (: *method-specializers (pair -> *)) ; (define *method-specializers car) (: *method-procedure (pair -> *)) ; (define *method-procedure cdr) (: error-generic-form (symbol * fixnum -> void)) ; (define (error-generic-form loc obj idx) (error loc "generic closure form violation" obj idx) ) (: check-generic-form (symbol * fixnum procedure -> *)) ; (define (check-generic-form loc obj0 idx type-pred?) (let ((obj (##sys#slot (check-generic loc obj0) idx))) (unless (type-pred? obj) (error-generic-form loc obj0 idx) ) obj ) ) (: checked-generic-methods (symbol * fixnum -> *)) ; (define (checked-generic-methods loc obj idx) (vector-ref (check-generic-form loc obj idx vector-boxed-list?) 0) ) (: top-instance-of? (* coops-class -> boolean)) ; (define (top-instance-of? x class) (let ((class-x (class-of x))) (or (eq? +class-primitive+ class-x) (class<= class class-x) ) ) ) ;; call-by-ref (: generic-name? (* -> boolean)) ; (define (generic-name? obj) (or (not obj) (symbol? obj)) ) (: vector-boxed-list? (* -> boolean)) ; (define (vector-boxed-list? obj) (and (vector? obj) (= 1 (vector-length obj)) (list? (vector-ref obj 0))) ) ;;; Predicates (: instance-of? (* coops-class -> boolean)) ; (define (instance-of? x class) (class<= (class-of x) class) ) (: class? (* -> boolean : coops-class)) ; (define (class? x) (instance-of? x ) ) (: instance? (* -> boolean : coops-instance)) ; (define (instance? x) (not (top-instance-of? x )) ) (: primitive-instance? (* -> boolean : coops-instance)) ; (define (primitive-instance? x) (top-instance-of? x ) ) (: method? (* -> boolean : coops-method)) ; (define (method? obj) (and (pair? obj) (procedure? (*method-procedure obj)) (and-let* ( (specializers (*method-specializers obj)) ((list? specializers)) ) (every class? specializers) ) ) ) ;;; Errors & Checks (define generic? generic-procedure?) (define-check+error-type class class? "coops-class") (define-check+error-type instance instance? "coops-instance") (define-check+error-type generic generic? "coops-generic") (define-check+error-type method method? "coops-method") ;;; Introspection ;; Class Properties (: class-precedence-list (coops-class -> list)) ; (define (class-precedence-list class) (*class-supers (check-class 'class-precedence-list class)) ) (: class-slots (coops-class -> list)) ; (define (class-slots class) (*class-slots (check-class 'class-slots class)) ) ;; Class Direct Properties ;those supers declared in the direct class & not inherited (: class-direct-supers (coops-class -> list)) ; (define (class-direct-supers class) (check-class 'class-direct-supers class) (lset-difference eq? (*class-supers class) (class-supers-supers class))) ;those slots declared in the direct class & not inherited (: class-direct-slots (coops-class -> list)) ; (define (class-direct-slots class) (check-class 'class-direct-slots class) (lset-difference eq? (*class-slots class) (class-supers-slots class)) ) ;; Generic Properties (: generic-anonymous? (* -> boolean : coops-generic)) ; (define (generic-anonymous? generic) (and (generic? generic) (not (##sys#slot generic 1)) ) ) (: generic-name (coops-generic -> symbol)) ; (define (generic-name generic) (check-generic-form 'generic-name generic 1 generic-name?) ) (: generic-specialized-arguments (coops-generic -> list)) ; (define (generic-specialized-arguments generic) (check-generic-form 'generic-specialized-args generic 7 pair?) ) (: generic-primary-methods (coops-generic -> list)) ; (define (generic-primary-methods generic) (checked-generic-methods 'generic-primary-methods generic 2) ) (: generic-before-methods (coops-generic -> list)) ; (define (generic-before-methods generic) (checked-generic-methods 'generic-before-methods generic 3) ) (: generic-after-methods (coops-generic -> list)) ; (define (generic-after-methods generic) (checked-generic-methods 'generic-after-methods generic 4) ) (: generic-around-methods (coops-generic -> list)) ; (define (generic-around-methods generic) (checked-generic-methods 'generic-around-methods generic 5) ) ;; Method Properties (: method-specializers (coops-method -> list)) ; (define (method-specializers method) (*method-specializers (check-method 'method-specializers method)) ) (: method-procedure (coops-method -> list)) ; (define (method-procedure method) (*method-procedure (check-method 'method-procedure method)) ) ) ;coops-introspection