unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* goops and equal?
@ 2014-11-10 12:57 Jan Nieuwenhuizen
  2014-11-10 13:03 ` Nala Ginrut
  2014-11-11 16:17 ` Mark H Weaver
  0 siblings, 2 replies; 5+ messages in thread
From: Jan Nieuwenhuizen @ 2014-11-10 12:57 UTC (permalink / raw
  To: guile-user

Hi,

In the example below, my implementation of equal? is only being 
used if both types are the same.  That surprised me; the manual
says equal? becomes a generic and for normal generics this works
as I expect.

How do I get the magic equal? to do what show the same result as the
simple generic type-equal?

And also, do we want such surprises?

Greetings,
Jan

(read-set! keywords 'prefix)
(use-modules (oop goops))

(define-class <type> ()
  (name :accessor .name :init-value #f :init-keyword :name))

(define-method (equal? (a <type>) (b <type>))
  (display "<type>equal?\n")
  (eq? (.name a) (.name b)))

(define-method (equal? (a <type>) (b <symbol>))
  (display "<type0>equal?\n")
  (eq? (.name a) b))

(define-method (type-equal? (a <type>) (b <symbol>))
  (display "<type1>equal?\n")
  (eq? (.name a) b))

(format #t "equal?: ~a\n" (equal? (make <type> :name 'a) (make <type> :name 'a)))
(newline)
(format #t "equal?: ~a\n" (equal? (make <type> :name 'a) 'a))
(newline)
(format #t "type-equal?: ~a\n" (type-equal? (make <type> :name 'a) 'a))
(newline)


-- 
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar®  http://AvatarAcademy.nl  



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

* Re: goops and equal?
  2014-11-10 12:57 goops and equal? Jan Nieuwenhuizen
@ 2014-11-10 13:03 ` Nala Ginrut
  2014-11-11 16:17 ` Mark H Weaver
  1 sibling, 0 replies; 5+ messages in thread
From: Nala Ginrut @ 2014-11-10 13:03 UTC (permalink / raw
  To: Jan Nieuwenhuizen; +Cc: Guile User

[-- Attachment #1: Type: text/plain, Size: 1506 bytes --]

I think you have to define your specific certain-class-equal?
So does record-type.
 2014年11月10日 下午8:58于 "Jan Nieuwenhuizen" <janneke@gnu.org>写道:

> Hi,
>
> In the example below, my implementation of equal? is only being
> used if both types are the same.  That surprised me; the manual
> says equal? becomes a generic and for normal generics this works
> as I expect.
>
> How do I get the magic equal? to do what show the same result as the
> simple generic type-equal?
>
> And also, do we want such surprises?
>
> Greetings,
> Jan
>
> (read-set! keywords 'prefix)
> (use-modules (oop goops))
>
> (define-class <type> ()
>   (name :accessor .name :init-value #f :init-keyword :name))
>
> (define-method (equal? (a <type>) (b <type>))
>   (display "<type>equal?\n")
>   (eq? (.name a) (.name b)))
>
> (define-method (equal? (a <type>) (b <symbol>))
>   (display "<type0>equal?\n")
>   (eq? (.name a) b))
>
> (define-method (type-equal? (a <type>) (b <symbol>))
>   (display "<type1>equal?\n")
>   (eq? (.name a) b))
>
> (format #t "equal?: ~a\n" (equal? (make <type> :name 'a) (make <type>
> :name 'a)))
> (newline)
> (format #t "equal?: ~a\n" (equal? (make <type> :name 'a) 'a))
> (newline)
> (format #t "type-equal?: ~a\n" (type-equal? (make <type> :name 'a) 'a))
> (newline)
>
>
> --
> Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
> Freelance IT http://JoyofSource.com | Avatar®  http://AvatarAcademy.nl
>
>

[-- Attachment #2: Type: text/html, Size: 2231 bytes --]

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

* Re: goops and equal?
  2014-11-10 12:57 goops and equal? Jan Nieuwenhuizen
  2014-11-10 13:03 ` Nala Ginrut
@ 2014-11-11 16:17 ` Mark H Weaver
  2014-11-12 13:55   ` Jan Nieuwenhuizen
  1 sibling, 1 reply; 5+ messages in thread
From: Mark H Weaver @ 2014-11-11 16:17 UTC (permalink / raw
  To: Jan Nieuwenhuizen; +Cc: guile-user

Jan Nieuwenhuizen <janneke@gnu.org> writes:

> In the example below, my implementation of equal? is only being 
> used if both types are the same.  That surprised me; the manual
> says equal? becomes a generic and for normal generics this works
> as I expect.

'equal?' is actually a "primitive generic", which means that it's a core
C function that handles the cases it knows about, and only calls out to
user-defined GOOPS methods for cases that aren't handled internally.

One of the facts known by core 'equal?' is that objects of different
types are not equal.

It may be that we should relax this somewhat, but IMO it's probably a
bad idea to allow instances of your user-defined type to be 'equal?' to
symbols.  Out of curiosity, why do you want this?

BTW, in order to allow user-defined types to be 'equal?' to built-in
types, we'd also need to change our hashing function for 'equal?'-based
hash tables.

      Regards,
        Mark



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

* Re: goops and equal?
  2014-11-11 16:17 ` Mark H Weaver
@ 2014-11-12 13:55   ` Jan Nieuwenhuizen
  2014-11-13 16:56     ` Mark H Weaver
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Nieuwenhuizen @ 2014-11-12 13:55 UTC (permalink / raw
  To: Mark H Weaver; +Cc: guile-user

Mark H Weaver writes:

> 'equal?' is actually a "primitive generic", which means that it's a core
> C function that handles the cases it knows about, and only calls out to
> user-defined GOOPS methods for cases that aren't handled internally.
>
> One of the facts known by core 'equal?' is that objects of different
> types are not equal.

That makes sense; once you know it.

> It may be that we should relax this somewhat, but IMO it's probably a
> bad idea to allow instances of your user-defined type to be 'equal?' to
> symbols.  Out of curiosity, why do you want this?

Mainly because of surprise/symmetry.  The manual says equal? becomes
a generic once Goops is loaded.

I agree that it's unwise or at least bad taste to implement equal?
for different objects to be the same.

I have a situation where the compared objects could be the same,
or one could be a symbol which has the same semantics.

   (define-class <type> ()
    (name :accessor .name :init-value 'void :init-keyword :name))

   (define-method (type-equal? (a <type>) (b <symbol>))
     (eq? (.name a) b))

Now I must provide a type-equal? wrapper for the equal? method
I already have, ie

   (define-method (type-equal? (a <type>) (b <type>))
     (equal? a b))

that I could omit if the first type-symbol one could be called equal?

Thanks!
Greetings, Jan

-- 
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar®  http://AvatarAcademy.nl  



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

* Re: goops and equal?
  2014-11-12 13:55   ` Jan Nieuwenhuizen
@ 2014-11-13 16:56     ` Mark H Weaver
  0 siblings, 0 replies; 5+ messages in thread
From: Mark H Weaver @ 2014-11-13 16:56 UTC (permalink / raw
  To: Jan Nieuwenhuizen; +Cc: guile-user

Jan Nieuwenhuizen <janneke@gnu.org> writes:

> Mark H Weaver writes:
>
>> 'equal?' is actually a "primitive generic", which means that it's a core
>> C function that handles the cases it knows about, and only calls out to
>> user-defined GOOPS methods for cases that aren't handled internally.
>>
>> One of the facts known by core 'equal?' is that objects of different
>> types are not equal.
>
> That makes sense; once you know it.
>
>> It may be that we should relax this somewhat, but IMO it's probably a
>> bad idea to allow instances of your user-defined type to be 'equal?' to
>> symbols.  Out of curiosity, why do you want this?
>
> Mainly because of surprise/symmetry.  The manual says equal? becomes
> a generic once Goops is loaded.

I agree that we should improve that bit in the manual.

> I agree that it's unwise or at least bad taste to implement equal?
> for different objects to be the same.
>
> I have a situation where the compared objects could be the same,
> or one could be a symbol which has the same semantics.

They may have the same semantics in some of your code, but they have
different semantics for most other code in your Guile session.  The
problem with modifying 'equal?' in this way is that it would be a global
change, which could cause existing code to start misbehaving.

    Regards,
      Mark



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

end of thread, other threads:[~2014-11-13 16:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-10 12:57 goops and equal? Jan Nieuwenhuizen
2014-11-10 13:03 ` Nala Ginrut
2014-11-11 16:17 ` Mark H Weaver
2014-11-12 13:55   ` Jan Nieuwenhuizen
2014-11-13 16:56     ` Mark H Weaver

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).