guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Extending primitive to generic functions problem


From: Cor Legemaat
Subject: Extending primitive to generic functions problem
Date: Fri, 09 Oct 2020 20:30:29 +0200
User-agent: Evolution 3.36.1

Hi All:

I am trying to extend some of the primitive functions with generic
methods in a module to use them with custom classes. From 8.6.5 Generic
Function and Method Examples in the manual I came up with the following
module:

(define-module (test ep)
  #:use-module (oop goops)
  #:use-module ((rnrs base)
                #:select (+ - * / map max min sqrt expt abs log)
                #:prefix rnrs:))


(define-macro (make-primitive-generic source destination arguments)
  (let ((tmp-symbol (string->symbol (string-append (symbol->string
destination)
                                                   "-new"))))
    `(begin (define-generic ,tmp-symbol)
            ,(if (not (defined? destination))
                 `(define ,destination ,source))
            (let ((,destination ,source))
              ,(cond
                ((eq? arguments 1)
                 `(define-method (,tmp-symbol (n <real>)) (,source n)))
                ((eq? arguments 2)
                 `(define-method (,tmp-symbol (a <real>) (b <real>))
                                 (,source a b)))
                (else
                 `(begin
                    (define-method (,tmp-symbol (a <real>) (b <real>))
                                   (,source a b))
                    (define-method (,tmp-symbol (a <number>)) a)
                    (define-method (,tmp-symbol) 0)
                    (define-method (,tmp-symbol . args)
                      (,tmp-symbol (car args)
                                   (apply ,tmp-symbol (cdr args))))))))
            (set! ,destination ,tmp-symbol)
            (export ,destination))))
(export make-primitive-generic)

(make-primitive-generic rnrs:+ sum 0)
(make-primitive-generic rnrs:max max 0)
(make-primitive-generic rnrs:min min 0)
(make-primitive-generic rnrs:* product 0)
(make-primitive-generic rnrs:/ divide 0)
(make-primitive-generic rnrs:- difference 0)
(make-primitive-generic rnrs:sqrt sqrt 1)
(make-primitive-generic rnrs:expt expt 2)
(make-primitive-generic rnrs:abs abs 1)
(make-primitive-generic rnrs:log log 1)

Now 2 problems:
If I don't hack the primitive with the prefix there is an 'Unbound
variable:' error on those where the source and destination is the same.
This works but only if compiled, the second time guile uses 100% cpu
and never finish loading. Did play with eval-when but it's either not
defining the methods or not loading after it's compiled.

Where is it going wrong, is this possible or do I mis understand
something about how guile works?

Thanks in advance

Regards:
Cor

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]