unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#26106: Defining a method named '-' with one parameter
@ 2017-03-15 13:35 Alejandro Sanchez
  2017-04-19 15:00 ` Andy Wingo
  0 siblings, 1 reply; 2+ messages in thread
From: Alejandro Sanchez @ 2017-03-15 13:35 UTC (permalink / raw)
  To: 26106

If I define a method named ‘-‘ which only takes in one parameter, the expression ‘(- v)’ gets rewritten to ‘(- 0 v)’. Here is a minimal example:

	(use-modules (oop goops))
	
	(define-class <vector2> ()
	  (x #:init-value 0 #:getter get-x #:init-keyword #:x)
	  (y #:init-value 0 #:getter get-y #:init-keyword #:y))
	
	(define-method (* (n <number>) (v <vector2>))
	  (make <vector2> #:x (* n (get-x v)) #:y (* n (get-y v))))
	
	(define-method (- (v <vector2>))
	  (* -1 v))
	
	(define v (make <vector2> #:x 1 #:y 2))
	(* -1 v)  ; Works fine
	(- v)  ; Throws error

Here is the error message:

	scheme@(guile-user)> (- v)
	ERROR: In procedure scm-error:
	ERROR: No applicable method for #<<generic> - (2)> in call (- 0 #<<vector2> 10a8e4020>)
	
	Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
	scheme@(guile-user) [1]> ,bt
	In current input:
	     23:0  2 (_)
	In oop/goops.scm:
	   1438:4  1 (cache-miss 0 #<<vector2> 10a8e4020>)
	In unknown file:
	           0 (scm-error goops-error #f "No applicable method for ~S in call ~S" (#<<generic> - (2)> (- 0 #<<vec…>)) #)






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

* bug#26106: Defining a method named '-' with one parameter
  2017-03-15 13:35 bug#26106: Defining a method named '-' with one parameter Alejandro Sanchez
@ 2017-04-19 15:00 ` Andy Wingo
  0 siblings, 0 replies; 2+ messages in thread
From: Andy Wingo @ 2017-04-19 15:00 UTC (permalink / raw)
  To: Alejandro Sanchez; +Cc: 26106

On Wed 15 Mar 2017 14:35, Alejandro Sanchez <hiphish@openmailbox.org> writes:

> If I define a method named ‘-‘ which only takes in one parameter, the expression ‘(- v)’ gets rewritten to ‘(- 0 v)’. Here is a minimal example:
>
> 	(use-modules (oop goops))
> 	
> 	(define-class <vector2> ()
> 	  (x #:init-value 0 #:getter get-x #:init-keyword #:x)
> 	  (y #:init-value 0 #:getter get-y #:init-keyword #:y))
> 	
> 	(define-method (* (n <number>) (v <vector2>))
> 	  (make <vector2> #:x (* n (get-x v)) #:y (* n (get-y v))))
> 	
> 	(define-method (- (v <vector2>))
> 	  (* -1 v))
> 	
> 	(define v (make <vector2> #:x 1 #:y 2))
> 	(* -1 v)  ; Works fine
> 	(- v)  ; Throws error
>
> Here is the error message:
>
> 	scheme@(guile-user)> (- v)
> 	ERROR: In procedure scm-error:
> 	ERROR: No applicable method for #<<generic> - (2)> in call (- 0 #<<vector2> 10a8e4020>)
> 	
> 	Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
> 	scheme@(guile-user) [1]> ,bt
> 	In current input:
> 	     23:0  2 (_)
> 	In oop/goops.scm:
> 	   1438:4  1 (cache-miss 0 #<<vector2> 10a8e4020>)
> 	In unknown file:
> 	           0 (scm-error goops-error #f "No applicable method for ~S in call ~S" (#<<generic> - (2)> (- 0 #<<vec…>)) #)

Is (- x) equivalent to (* x -1) ?

Right now there are a few things happening.  The "primitive expansion"
phase in an early part of the compiler turns (- x) to (- 0 x), where
obviously it should not be doing that.  But can it turn it into (* x -1)
?  Note that somewhat confusingly, a later part of the compiler that can
detect when X is a real number will undo that transformation, turning it
to (- 0 x) when X is real.  So that sounds OK from an optimization point
of view but is the (* x -1) tranformation correct from the math point of
view?

Andy





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

end of thread, other threads:[~2017-04-19 15:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-15 13:35 bug#26106: Defining a method named '-' with one parameter Alejandro Sanchez
2017-04-19 15:00 ` Andy Wingo

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