unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* [goops] adding a number class with generic methods
@ 2006-05-02 20:16 Marco Maggi
  0 siblings, 0 replies; 10+ messages in thread
From: Marco Maggi @ 2006-05-02 20:16 UTC (permalink / raw)


Hi,

  I  am trying  to add  a new  number class,  export generic
methods from a module and import them into another one. If I
ignore the GOOPS tutorial and read only the reference, it is
my understanting that I should do:

;; ----------------------------------------
;; exporting.scm

(define-module (gee gmp ompfr)
  #:use-module (oop goops)
  #:use-module (gee gmp mpfr))

(define-class <mpfr> (<number>)
  (n #:init-value (real->mpfr 0) #:init-keyword #:value))

;; ... other stuff ...

(define-method (= (a <mpfr>) (b <mpfr>))
  (mpfr-equal? (slot-ref a 'n) (slot-ref b 'n)))

(define-method (= (a <mpfr>) (b <real>))
  (mpfr-equal? (slot-ref a 'n) (usable->mpfr b)))

(define-method (= (b <real>) (a <mpfr>))
  (mpfr-equal? (slot-ref a 'n) (usable->mpfr b)))

(export =)

;; ----------------------------------------
;; importing.scm

(define-module (my-test)
  #:use-module (mmg test dotest-1)
  #:use-module (oop goops)
  #:duplicates merge-generics
  #:use-module (gee gmp ompfr))

(= #,(MPFR 2) #,(MPFR 2))

;; ----------------------------------------

but I get this error:

...
   ?: 6  (= #<<mpfr> 40351520> #<<mpfr> 403505e0>)

<unnamed port>: In expression (= #<<mpfr> 40351520> #<<mpfr>
403505e0>):
<unnamed port>: Unbound variable: =


  If I read the tutorial it seems that I have to replace the
'(define-method (= ...' definitions with:

;; ----------------------------------------

(define-generic my=)

(let ((original= =))

  (define-method (my= (a <number>) (b <number>))
    (original= a b)))

(define-method (my= (a <mpfr>) (b <mpfr>))
  (mpfr-equal? (slot-ref a 'n) (slot-ref b 'n)))

(define-method (my= (a <mpfr>) (b <real>))
  (mpfr-equal? (slot-ref a 'n) (usable->mpfr b)))

(define-method (my= (b <real>) (a <mpfr>))
  (mpfr-equal? (slot-ref a 'n) (usable->mpfr b)))

(set! = my=)

;; ----------------------------------------

but I get the same error.

  I do not understand why  the first solution does not work;
I've tried different combinations  of functions but there is
something I am missing.



--
Marco Maggi

"They say jump!, you say how high?"
Rage Against the Machine - "Bullet in the Head"



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
@ 2006-05-03  5:36 Marco Maggi
  2006-05-04  7:47 ` Neil Jerram
  0 siblings, 1 reply; 10+ messages in thread
From: Marco Maggi @ 2006-05-03  5:36 UTC (permalink / raw)


"marco.maggi-ipsu@poste.it" wrote:
>  I do not understand why  the first solution does not work;
>I've tried different combinations  of functions but there is
>something I am missing.

Damn... it seems to work if I do not EXPORT the generic
methods.

--
Marco Maggi




_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
  2006-05-03  5:36 [goops] adding a number class with generic methods Marco Maggi
@ 2006-05-04  7:47 ` Neil Jerram
  0 siblings, 0 replies; 10+ messages in thread
From: Neil Jerram @ 2006-05-04  7:47 UTC (permalink / raw)
  Cc: guile-user

"Marco Maggi" <marco.maggi-ipsu@poste.it> writes:

> "marco.maggi-ipsu@poste.it" wrote:
>>  I do not understand why  the first solution does not work;
>>I've tried different combinations  of functions but there is
>>something I am missing.
>
> Damn... it seems to work if I do not EXPORT the generic
> methods.

You mean if you remove the "(export =)" line?

    Neil



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
@ 2006-05-04 19:13 Marco Maggi
  2006-05-04 19:26 ` Mikael Djurfeldt
  0 siblings, 1 reply; 10+ messages in thread
From: Marco Maggi @ 2006-05-04 19:13 UTC (permalink / raw)


"Neil Jerram" wrote:
>"Marco Maggi" <marco.maggi-ipsu@poste.it> writes:
> "marco.maggi-ipsu@poste.it" wrote:
>>  I do not understand why  the first solution does not work;
>>I've tried different combinations  of functions but there is
>>something I am missing.
>
> Damn... it seems to work if I do not EXPORT the generic
> methods.
>
>You mean if you remove the "(export =)" line?

Yes, with the following and no EXPORT invocation
it works.

(define-macro (make-comparison-operator class name func)
  `(begin
     (define-method (,name (a ,class) (b ,class))
       (,func (slot-ref a 'n) (slot-ref b 'n)))

     (define-method (,name (a ,class) (b <real>))
       (,func (slot-ref a 'n) (usable->mpfr b)))

     (define-method (,name (a <real>) (b ,class))
       (,func (usable->mpfr a) (slot-ref b 'n)))))

(make-comparison-operator <mpfr> = mpfr-equal?)


--
Marco Maggi




_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
  2006-05-04 19:13 Marco Maggi
@ 2006-05-04 19:26 ` Mikael Djurfeldt
  2006-05-06 23:07   ` Neil Jerram
  0 siblings, 1 reply; 10+ messages in thread
From: Mikael Djurfeldt @ 2006-05-04 19:26 UTC (permalink / raw)
  Cc: guile-user

On 5/4/06, Marco Maggi <marco.maggi-ipsu@poste.it> wrote:
> "Neil Jerram" wrote:
> >"Marco Maggi" <marco.maggi-ipsu@poste.it> writes:
> > "marco.maggi-ipsu@poste.it" wrote:
> >>  I do not understand why  the first solution does not work;
> >>I've tried different combinations  of functions but there is
> >>something I am missing.
> >
> > Damn... it seems to work if I do not EXPORT the generic
> > methods.
> >
> >You mean if you remove the "(export =)" line?
>
> Yes, with the following and no EXPORT invocation
> it works.

Actually, logically this *is* how the code should be structured.  If
you had created a novel generic function within your module, that
function should have been exported.  But what you really want is just
to add methods to the pre-existing generic function = which you
(implicitly) *import* into your module.


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
  2006-05-04 19:26 ` Mikael Djurfeldt
@ 2006-05-06 23:07   ` Neil Jerram
  2006-05-07  9:37     ` Mikael Djurfeldt
  0 siblings, 1 reply; 10+ messages in thread
From: Neil Jerram @ 2006-05-06 23:07 UTC (permalink / raw)
  Cc: guile-user

"Mikael Djurfeldt" <mdjurfeldt@gmail.com> writes:

> On 5/4/06, Marco Maggi <marco.maggi-ipsu@poste.it> wrote:
>>
>> Yes, with the following and no EXPORT invocation
>> it works.
>
> Actually, logically this *is* how the code should be structured.  If
> you had created a novel generic function within your module, that
> function should have been exported.  But what you really want is just
> to add methods to the pre-existing generic function = which you
> (implicitly) *import* into your module.

I agree that this explains why the export is not necessary (and hence
why it works without it).  But I don't yet understand why adding the
export messes things up.

Regards,
        Neil



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
  2006-05-06 23:07   ` Neil Jerram
@ 2006-05-07  9:37     ` Mikael Djurfeldt
  2006-05-07 11:45       ` Neil Jerram
  2006-05-09  7:56       ` Ludovic Courtès
  0 siblings, 2 replies; 10+ messages in thread
From: Mikael Djurfeldt @ 2006-05-07  9:37 UTC (permalink / raw)
  Cc: guile-user

On 5/7/06, Neil Jerram <neil@ossau.uklinux.net> wrote:
> I agree that this explains why the export is not necessary (and hence
> why it works without it).  But I don't yet understand why adding the
> export messes things up.

In the current module system, the effect of mentioning a variable in
an export statement is to set up a new, undefined, variable with that
name.  This variable is added to the module and to the export list of
the model.  The former has the effect of shadowing the old variable =.


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
  2006-05-07  9:37     ` Mikael Djurfeldt
@ 2006-05-07 11:45       ` Neil Jerram
  2006-05-09  7:56       ` Ludovic Courtès
  1 sibling, 0 replies; 10+ messages in thread
From: Neil Jerram @ 2006-05-07 11:45 UTC (permalink / raw)
  Cc: guile-user

"Mikael Djurfeldt" <mdjurfeldt@gmail.com> writes:

> On 5/7/06, Neil Jerram <neil@ossau.uklinux.net> wrote:
>> I agree that this explains why the export is not necessary (and hence
>> why it works without it).  But I don't yet understand why adding the
>> export messes things up.
>
> In the current module system, the effect of mentioning a variable in
> an export statement is to set up a new, undefined, variable with that
> name.  This variable is added to the module and to the export list of
> the model.  The former has the effect of shadowing the old variable =.

I see.  Thanks for explaining this.

  Neil



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
  2006-05-07  9:37     ` Mikael Djurfeldt
  2006-05-07 11:45       ` Neil Jerram
@ 2006-05-09  7:56       ` Ludovic Courtès
  2006-05-09  8:30         ` Mikael Djurfeldt
  1 sibling, 1 reply; 10+ messages in thread
From: Ludovic Courtès @ 2006-05-09  7:56 UTC (permalink / raw)
  Cc: guile-user, Neil Jerram

Hi,

"Mikael Djurfeldt" <mdjurfeldt@gmail.com> writes:

> In the current module system, the effect of mentioning a variable in
> an export statement is to set up a new, undefined, variable with that
> name.

And thus the first `define-method' creates a new, empty, generic
function.  That makes it sometimes quite tricky to spread method
definitions all over the place (e.g., see G-Wrap...).

Thanks,
Ludovic.


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [goops] adding a number class with generic methods
  2006-05-09  7:56       ` Ludovic Courtès
@ 2006-05-09  8:30         ` Mikael Djurfeldt
  0 siblings, 0 replies; 10+ messages in thread
From: Mikael Djurfeldt @ 2006-05-09  8:30 UTC (permalink / raw)
  Cc: Mikael Djurfeldt, Neil Jerram

On 5/9/06, Ludovic Courtès <ludovic.courtes@laas.fr> wrote:
> "Mikael Djurfeldt" <mdjurfeldt@gmail.com> writes:
>
> > In the current module system, the effect of mentioning a variable in
> > an export statement is to set up a new, undefined, variable with that
> > name.
>
> And thus the first `define-method' creates a new, empty, generic
> function.  That makes it sometimes quite tricky to spread method
> definitions all over the place (e.g., see G-Wrap...).

Could you elaborate on that?  In what way does GOOPS/module system
make this tricky, and how do you envision a better design?

It is my opinion that the current design with regards to generic
functions, methods and module system is in no way too restrictive.  If
you think it is, you should read the following reference:

  http://www2.parc.com/csl/groups/sda/publications/papers/Kiczales-OOPSLA92/for-web.pdf

Rather, I think the points where it can be criticized is that some
things are a bit "too automatic" and there should be better and more
error messages in some cases.

If you want a certain generic function to be available everywhere,
then you have to pose exactly the same question as you would do for an
ordinary function: Which module should export this function? 
Everybody else needs to import from that module, even those who add
new methods.

Of course, one may feel that the fact that methods can "spread"
through the generic function in a rather arbitrary manner across
module borders is a problem.  For those who want a more restricted
access to methods, there is the possibility to use extended generics. 
This is explained in NEWS, I think.

Best regards,
M


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2006-05-09  8:30 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-03  5:36 [goops] adding a number class with generic methods Marco Maggi
2006-05-04  7:47 ` Neil Jerram
  -- strict thread matches above, loose matches on Subject: below --
2006-05-04 19:13 Marco Maggi
2006-05-04 19:26 ` Mikael Djurfeldt
2006-05-06 23:07   ` Neil Jerram
2006-05-07  9:37     ` Mikael Djurfeldt
2006-05-07 11:45       ` Neil Jerram
2006-05-09  7:56       ` Ludovic Courtès
2006-05-09  8:30         ` Mikael Djurfeldt
2006-05-02 20:16 Marco Maggi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).