[R6RS] Condition types as record types

Michael Sperber sperber
Thu Nov 24 05:42:31 EST 2005

This is taking up an old corner of the discussion on condition types.
Marc and Manuel asked that condition types be implemented as record
types of the same shapes, i.e. that the record type corresponding to a
condition type have the same fields, and that the inheritance
hierarchies correspond.

I've attached an implementation of SRFI 35 in terms of the current
version of SRFI 76.  As it stands, it's based on the procedural and
reflection layers.  The use of the reflection layer could be elided by
putting more stuff into the condition-type object.  (Note that the
definition of :CONDITION-TYPE could be elided---its only field
contains the record type.)  This is offered mostly as a
proof-of-concept, to show that it can be done quite straightforwardly
(I wrote it while waiting for a connecting flight at an airport),
rather than a production-quality implementation.

Cheers =8-} Mike
Friede, V?lkerverst?ndigung und ?berhaupt blabla

-------------- next part --------------
;; Condition types as in SRFI 35, implemented as record types as in SRFI 76

;; makes use of SRFIs 1 (lists), and 23 (error-reporting)

(define-type (:condition-type really-make-condition-type condition-type?) (rtd)
   (rtd (condition-type-rtd) rtd)))

(define-type (:condition really-make-condition condition?) (reasons)
   (reasons (condition-reasons) reasons)))

(define (make-condition-type id parent field-names)
  ;; #### should check for duplicate field names
   (make-record-type-descriptor id
				(condition-type-rtd parent)
				(map (lambda (field-name)
				       (list 'immutable field-name))

(define (condition-type-all-fields condition-type)
  (rtd-all-fields (condition-type-rtd condition-type)))

(define (rtd-all-fields rtd)
  (if (not rtd)
      (append (rtd-all-fields (record-type-parent rtd))
	      (record-type-field-names rtd))))

(define (make-condition type . field-plist)
  (let ((alist (let label ((plist field-plist))
                 (if (null? plist)
                     (cons (cons (car plist)
                                 (cadr plist))
                           (label (cddr plist)))))))
    (if (not (lset= eq?
                    (condition-type-all-fields type)
                    (map car alist)))
        (error "condition fields don't match condition type"
	       (condition-type-all-fields type)
	       (map car alist)))
       (record-constructor (condition-type-rtd type))
       (map (lambda (field-name)
	      (cdr (assq field-name alist)))
	    (condition-type-all-fields type)))))))

(define (condition-has-type? condition condition-type)
  (let ((reason-has-type?
	 (record-predicate (condition-type-rtd condition-type)))) 
     (lambda (reason)
       (reason-has-type? reason))
     (condition-reasons condition))))

(define (record-ref record field-name succeed fail)
  (let loop ((rtd (record-type-descriptor record)))
     ((not rtd)
     ((memq field-name (record-type-field-names rtd))
      (succeed ((record-accessor rtd field-name) record)))
      (loop (record-type-parent rtd))))))

(define (condition-ref condition field-name)
  (let loop ((reasons (condition-reasons condition)))
    (if (null? reasons)
	(error "condition-ref: field name doesn't occur in condition"
	       condition field-name)
	(record-ref (car reasons) field-name
		    (lambda ()
		      (loop (cdr reasons)))))))

(define (make-compound-condition . conditions)
   (concatenate (map condition-reasons conditions))))

(define (extract-condition condition condition-type)
  (let loop ((reasons (condition-reasons condition)))
    (if (null? reasons)
	(error "extract-condition: condition didn't match type"
	       condition condition-type)
	(extract-condition-from-reason (car reasons)
				       (lambda ()
					 (loop (cdr reasons)))))))

(define (extract-condition-from-reason reason condition-type
				       succeed fail)
  (let ((rtd (condition-type-rtd condition-type)))
    ;; could cache the predicate in the CONDITION-TYPE record
    (if ((record-predicate rtd)
	    ;; could avoid this by stuffing more into condition-type
	    (record-constructor rtd)
	     (lambda (field-name)
	       (record-ref reason field-name
			   (lambda ()
			     (error "this can't happen"))))
	     (condition-type-all-fields condition-type))))))

(define-syntax define-condition-type
  (syntax-rules ()
    ((define-condition-type ?name ?supertype ?predicate
       (?field1 ?accessor1) ...)
       (define ?name
         (make-condition-type '?name
                              '(?field1 ...)))
       (define (?predicate thing)
         (and (condition? thing)
              (condition-has-type? thing ?name)))
       (define (?accessor1 condition)
         (condition-ref (extract-condition condition ?name)

(define (rtd-extends? rtd-1 rtd-2)
  (let loop ((rtd-1 rtd-1))
     ((not rtd-1) #f)
     ((eq? rtd-1 rtd-2) #t)
      (loop (record-type-parent rtd-1))))))

(define (common-supertype-rtd type-1 type-2)
  (let ((rtd-1 (condition-type-rtd type-1))
	(rtd-2 (condition-type-rtd type-2)))
    (let loop ((rtd-2 rtd-2))
       ((not rtd-2) #f)
       ((rtd-extends? rtd-1 rtd-2) rtd-2)
       (else (loop (record-type-parent rtd-2)))))))

(define (complete-condition type field-alist
     (record-constructor (condition-type-rtd type))
     (map (lambda (field-name)
	     ((assq field-name field-alist) => cdr)
	      (any (lambda (entry)
		      ((common-supertype-rtd (car entry) type)
		       => (lambda (supertype-rtd)
			       (memq field-name
				     (rtd-all-fields supertype-rtd))
			       (assq field-name (cdr entry)))
			      => cdr)
			     (else #f))))
		      (else #f)))
	  (condition-type-all-fields type))))))

(define-syntax condition
  (syntax-rules ()
    ((condition (?type1 (?field1 ?value1) ...) ...)
      (list (cons ?type1 (list (cons '?field1 ?value1) ...)) ...)))))

(define (condition* type-field-alist)
   (map (lambda (entry)
	  (complete-condition (car entry) (cdr entry)

(define &condition
   (make-record-type-descriptor '&condition

(define-condition-type &message &condition
  (message condition-message))

(define-condition-type &serious &condition

(define-condition-type &error &serious

More information about the R6RS mailing list