unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Using '-1' in a method named '*'
@ 2017-02-27 10:06 Alejandro Sanchez
  2017-02-27 19:21 ` Andy Wingo
  0 siblings, 1 reply; 13+ messages in thread
From: Alejandro Sanchez @ 2017-02-27 10:06 UTC (permalink / raw)
  To: guile-user

Hello,

I have been trying to write a Guile library for 3D motion and I wrote a ‘vector3’ class and added the method ‘*’ for scaling a vector. This works fine for any scalar, except for ‘-1’. Here is a minimal code example, I am using Guile 2.0.14 on OS X 10.11.6:

(use-modules (oop goops))

(define-class <vector3> ()
  ;; Only one slot, for simlicity
  (x #:init-value 0 #:getter get-x #:init-keyword #:x))

(define-method (*  (n <real>) (v <vector3>))
  ;; Return anything, doesn't matter
  3)


(define v (make <vector3> #:x 1))

(* -1 v)  ; Does not work
(* -2 v)  ; Works fine


The error message is:

<unnamed port>:14:0: In procedure #<procedure 104d7b8e0 at <current input>:14:0 ()>:
<unnamed port>:14:0: In procedure -: Wrong type argument in position 1: #<<vector3> 104ccdce0>

The backtrace is:

     16:0  0 (#<procedure 104db6240 at <current input>:16:0 ()>)

This issue only happens when the argument is -1, it doesn’t happen for other negative integers and it doesn’t happen for ‘-1.0’ either. And it’s not just the literal ‘-1’, if instead I use an expression that evaluates to ‘-1’ like ‘(- 2 3)’ the same happens. However, changing the order of arguments in the definition of the method does work.

The reason for this seems that the expression ‘(* -1 x)’ for any x is re-written by Guile to ‘(- x)’, because by adding the following method it gets working:

(define-method (- (v <vector3>))
  2)

However, since '(* -1 v)’ gets re-written to ‘(- v)’ the result is ‘2' instead of ‘3’. Granted, this is a contrived example and in any scenario I can think of '(* -1 v)’ and ‘(- v)’ would yield the same result, but is this intended behaviour? It was something that got me hung up over the last few days quite a lot (I was already in the process of writing this email when it occurred to me to define the ‘-‘ method).


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

* Re: Using '-1' in a method named '*'
  2017-02-27 10:06 Using '-1' in a method named '*' Alejandro Sanchez
@ 2017-02-27 19:21 ` Andy Wingo
  2017-02-27 22:26   ` Alejandro Sanchez
                     ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Andy Wingo @ 2017-02-27 19:21 UTC (permalink / raw)
  To: Alejandro Sanchez; +Cc: guile-user

Hi,

On Mon 27 Feb 2017 11:06, Alejandro Sanchez <hiphish@openmailbox.org> writes:

> (define v (make <vector3> #:x 1))
>
> (* -1 v)  ; Does not work
> (* -2 v)  ; Works fine

I believe that Guile is doing strength reduction, transforming (* -1 v)
to (- 0 v).

It could be that this is totally the wrong thing.  Is (* x 2) -> (+ x x)
a valid transformation if you don't know the type of x?  I don't know.

I think there's currently an assumption that if you extend *, that you
will do so in a mathy way, and that you implement - + and similar.  But
in this case it's not the clear right thing to do.

WDYT?  We could remove this transformation, or only apply it when type
inference has run.

Andy



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

* Re: Using '-1' in a method named '*'
  2017-02-27 19:21 ` Andy Wingo
@ 2017-02-27 22:26   ` Alejandro Sanchez
  2017-02-28  8:00   ` tomas
  2017-02-28  8:54   ` Andy Wingo
  2 siblings, 0 replies; 13+ messages in thread
From: Alejandro Sanchez @ 2017-02-27 22:26 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user


> It could be that this is totally the wrong thing.  Is (* x 2) -> (+ x x)
> a valid transformation if you don't know the type of x?  I don't know.
Only if both addition and multiplication are defined for the type. In a group-like structure there is only one operation, while ring-like structures have two (usually called addition and multiplication). All the numeric types included in Guile are ring-like, so that works fine. Outside of that all bets are off. What would transforming (* -1 v) to (- 0 v) mean? You cannot add or subtract scalars and vectors, unless you were to embed both into a field where they are both defined, like the quaternion field. Or you would have to use the zero-vector as the value of 0.

> WDYT?  We could remove this transformation, or only apply it when type
> inference has run.
I don’t know. I guess there must be a good reason why Guile does these transformations, all I know is that they are not guaranteed to make sense outside Guile’s numeric types. Maybe it was my mistake for trying to make methods called * or + instead of making up my own names. The reason why I used those names (aside from simplicity) is that in those case it does make sense to mix multiplication of different types. I can write (* 2 3 q 4 5) where q is a quaternion and it will apply the correct operation to all operands, just as if I had written it on paper. It also allows me to use any type that implements * as the component of a dual number and have it work.

If you can stop Guile from re-writing code when it is not certain that the rewritten code makes sense it would be great. Pretty much every test that I have written over the past few days was broken even though my math was correct.


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

* Re: Using '-1' in a method named '*'
  2017-02-27 19:21 ` Andy Wingo
  2017-02-27 22:26   ` Alejandro Sanchez
@ 2017-02-28  8:00   ` tomas
  2017-02-28  8:54   ` Andy Wingo
  2 siblings, 0 replies; 13+ messages in thread
From: tomas @ 2017-02-28  8:00 UTC (permalink / raw)
  To: guile-user

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, Feb 27, 2017 at 08:21:03PM +0100, Andy Wingo wrote:
> Hi,
> 
> On Mon 27 Feb 2017 11:06, Alejandro Sanchez <hiphish@openmailbox.org> writes:
> 
> > (define v (make <vector3> #:x 1))
> >
> > (* -1 v)  ; Does not work
> > (* -2 v)  ; Works fine
> 
> I believe that Guile is doing strength reduction, transforming (* -1 v)
> to (- 0 v).

Woah. This is somehow... exquisite.

> It could be that this is totally the wrong thing.  Is (* x 2) -> (+ x x)
> a valid transformation if you don't know the type of x?  I don't know.

What we really want to know is the meaning of '*' and '+' (is that akin
to constant folding?), but yes, for that we need some info about the type
of x, I guess.

> I think there's currently an assumption that if you extend *, that you
> will do so in a mathy way, and that you implement - + and similar.  But
> in this case it's not the clear right thing to do.
> 
> WDYT?  We could remove this transformation, or only apply it when type
> inference has run.

If overloading is allowed then yes, it seems type inference has to go
in first?

regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAli1Lh4ACgkQBcgs9XrR2kYpEwCdGS4EaOgUy/tBL46i2Isiz1MZ
sNwAnj+JM+oX7RM6LTjC8+h6r0qnIiBO
=Cc5b
-----END PGP SIGNATURE-----



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

* Re: Using '-1' in a method named '*'
  2017-02-27 19:21 ` Andy Wingo
  2017-02-27 22:26   ` Alejandro Sanchez
  2017-02-28  8:00   ` tomas
@ 2017-02-28  8:54   ` Andy Wingo
  2017-02-28  9:19     ` Andy Wingo
  2 siblings, 1 reply; 13+ messages in thread
From: Andy Wingo @ 2017-02-28  8:54 UTC (permalink / raw)
  To: Alejandro Sanchez; +Cc: guile-user

On Mon 27 Feb 2017 20:21, Andy Wingo <wingo@pobox.com> writes:

> Hi,
>
> On Mon 27 Feb 2017 11:06, Alejandro Sanchez <hiphish@openmailbox.org> writes:
>
>> (define v (make <vector3> #:x 1))
>>
>> (* -1 v)  ; Does not work
>> (* -2 v)  ; Works fine
>
> I believe that Guile is doing strength reduction, transforming (* -1 v)
> to (- 0 v).

I spoke too soon.  Guile 2.1.x does indeed do this at compile-time, but
only if "v" is an instance of a built-in number type.  Guile 2.0.x
doesn't do this strength reduction at all at compile-time.

It turns out there is an easier way to reproduce this bug without GOOPS:

   (* -1 "")

scm_product was handling this case specially because of weird
implementation reasons (making sure that negating the most negative
fixnum was a bignum; see b5c40589).

In summary I think Guile's strength reduction is fine as it relies on
type inference to prove when it is safe.  This is "just" a bug, albeit
an annoying one that we need to fix!

Andy



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

* Re: Using '-1' in a method named '*'
  2017-02-28  8:54   ` Andy Wingo
@ 2017-02-28  9:19     ` Andy Wingo
  2017-02-28 14:24       ` Alejandro Sanchez
  0 siblings, 1 reply; 13+ messages in thread
From: Andy Wingo @ 2017-02-28  9:19 UTC (permalink / raw)
  To: Alejandro Sanchez; +Cc: guile-user

On Tue 28 Feb 2017 09:54, Andy Wingo <wingo@pobox.com> writes:

> In summary I think Guile's strength reduction is fine as it relies on
> type inference to prove when it is safe.  This is "just" a bug, albeit
> an annoying one that we need to fix!

Fixed in 2.0 and master.  Thanks for the report :)

Andy



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

* Re: Using '-1' in a method named '*'
  2017-02-28  9:19     ` Andy Wingo
@ 2017-02-28 14:24       ` Alejandro Sanchez
  2017-02-28 17:53         ` Amirouche
  2017-03-01 17:25         ` Andy Wingo
  0 siblings, 2 replies; 13+ messages in thread
From: Alejandro Sanchez @ 2017-02-28 14:24 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user


> On 28 Feb 2017, at 10:19, Andy Wingo <wingo@pobox.com> wrote:
> 
> On Tue 28 Feb 2017 09:54, Andy Wingo <wingo@pobox.com> writes:
> 
>> In summary I think Guile's strength reduction is fine as it relies on
>> type inference to prove when it is safe.  This is "just" a bug, albeit
>> an annoying one that we need to fix!
> 
> Fixed in 2.0 and master.  Thanks for the report :)
> 
> Andy

Great, than you. Do you have any approximation on when a release with that fix will happen, or am I better off getting the source and building myself? I would rather not mess around with building, but I’ll bit the bullet and build myself if that’s the way to go. I am currently on 2.0.14 because my package manager only offers the latest stable release.


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

* Re: Using '-1' in a method named '*'
  2017-02-28 14:24       ` Alejandro Sanchez
@ 2017-02-28 17:53         ` Amirouche
  2017-02-28 18:32           ` Ralf Mattes
  2017-03-01 17:25         ` Andy Wingo
  1 sibling, 1 reply; 13+ messages in thread
From: Amirouche @ 2017-02-28 17:53 UTC (permalink / raw)
  To: guile-user



Le 28/02/2017 à 15:24, Alejandro Sanchez a écrit :
>> On 28 Feb 2017, at 10:19, Andy Wingo <wingo@pobox.com> wrote:
>>
>> On Tue 28 Feb 2017 09:54, Andy Wingo <wingo@pobox.com> writes:
>>
>>> In summary I think Guile's strength reduction is fine as it relies on
>>> type inference to prove when it is safe.  This is "just" a bug, albeit
>>> an annoying one that we need to fix!
>> Fixed in 2.0 and master.  Thanks for the report :)
>>
>> Andy
> Great, than you. Do you have any approximation on when a release with that fix will happen,

>   or am I better off getting the source and building myself? I would rather not mess around with building, but I’ll bit the bullet and build myself if that’s the way to go. I am currently on 2.0.14 because my package manager only offers the latest stable release.
I think git checkout and compiling is worth it.

git clone http://git.sv.gnu.org/r/guile.git

Don't forget to ./configure --prefix=/usr.

Regards,

Amirouche ~ amz3



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

* Re: Using '-1' in a method named '*'
  2017-02-28 17:53         ` Amirouche
@ 2017-02-28 18:32           ` Ralf Mattes
  0 siblings, 0 replies; 13+ messages in thread
From: Ralf Mattes @ 2017-02-28 18:32 UTC (permalink / raw)
  To: Amirouche; +Cc: guile-user

On Tue, Feb 28, 2017 at 06:53:33PM +0100, Amirouche wrote:
> 
> 
> Don't forget to ./configure --prefix=/usr.

Unless you run this on a self-compiled syytem that seems like a
very bad idea. You will happily overwrite a distribution provided guile
and during the next system update your package manager will happily
erase your custom guile and install the distribution version.

Binaries installed into /usr/local (the default chosen by autoconf) will
be the first binary found in the path in all distibutions I have seen
so far.

Cheers, Ralf Mattes

> Regards,
> 
> Amirouche ~ amz3
> 



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

* Re: Using '-1' in a method named '*'
  2017-02-28 14:24       ` Alejandro Sanchez
  2017-02-28 17:53         ` Amirouche
@ 2017-03-01 17:25         ` Andy Wingo
  2017-03-01 23:46           ` Alejandro Sanchez
  1 sibling, 1 reply; 13+ messages in thread
From: Andy Wingo @ 2017-03-01 17:25 UTC (permalink / raw)
  To: Alejandro Sanchez; +Cc: guile-user

On Tue 28 Feb 2017 15:24, Alejandro Sanchez <hiphish@openmailbox.org> writes:

>> On 28 Feb 2017, at 10:19, Andy Wingo <wingo@pobox.com> wrote:
>> 
>> On Tue 28 Feb 2017 09:54, Andy Wingo <wingo@pobox.com> writes:
>> 
>>> In summary I think Guile's strength reduction is fine as it relies on
>>> type inference to prove when it is safe.  This is "just" a bug, albeit
>>> an annoying one that we need to fix!
>> 
>> Fixed in 2.0 and master.  Thanks for the report :)
>> 
>> Andy
>
> Great, than you. Do you have any approximation on when a release with
> that fix will happen, or am I better off getting the source and
> building myself? I would rather not mess around with building, but
> I’ll bit the bullet and build myself if that’s the way to go. I am
> currently on 2.0.14 because my package manager only offers the latest
> stable release.

2.2.0 should come soon (a couple weeks hopefully), but I don't know
about 2.0.14.  In the meantime if defining an implementation for "-"
works for you to handle negation, that's an OK workaround.

Andy



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

* Re: Using '-1' in a method named '*'
  2017-03-01 17:25         ` Andy Wingo
@ 2017-03-01 23:46           ` Alejandro Sanchez
  2017-03-02  0:18             ` Thomas Morley
  0 siblings, 1 reply; 13+ messages in thread
From: Alejandro Sanchez @ 2017-03-01 23:46 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user


> On 01 Mar 2017, at 18:25, Andy Wingo <wingo@pobox.com> wrote:
> 
> 2.2.0 should come soon (a couple weeks hopefully), but I don't know
> about 2.0.14.  In the meantime if defining an implementation for "-"
> works for you to handle negation, that's an OK workaround.
> 
> Andy
This may be a stupid question, but how do I build guile? I ran

git clone http://git.sv.gnu.org/r/guile.git

but there is no “configure” file. There is an “automake.sh”, but when I run it it fails with an error

configure.ac:943: error: possibly undefined macro: AM_GNU_GETTEXT
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
autoreconf: /usr/local/Cellar/autoconf/2.69/bin/autoconf failed with exit status: 1

I guess it’s something about my setup. What are those tarballs I can download that have a configure file? Are those like half-built where someone has already run automake for me?


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

* Re: Using '-1' in a method named '*'
  2017-03-01 23:46           ` Alejandro Sanchez
@ 2017-03-02  0:18             ` Thomas Morley
  0 siblings, 0 replies; 13+ messages in thread
From: Thomas Morley @ 2017-03-02  0:18 UTC (permalink / raw)
  To: Alejandro Sanchez; +Cc: Andy Wingo, guile-user

2017-03-02 0:46 GMT+01:00 Alejandro Sanchez <hiphish@openmailbox.org>:
>
>> On 01 Mar 2017, at 18:25, Andy Wingo <wingo@pobox.com> wrote:
>>
>> 2.2.0 should come soon (a couple weeks hopefully), but I don't know
>> about 2.0.14.  In the meantime if defining an implementation for "-"
>> works for you to handle negation, that's an OK workaround.
>>
>> Andy
> This may be a stupid question, but how do I build guile? I ran
>
> git clone http://git.sv.gnu.org/r/guile.git
>
> but there is no “configure” file. There is an “automake.sh”, but when I run it it fails with an error

I did after git clone:

sh autogen.sh
./configure
make -j5
sudo make install

More info in HACKING

HTH,
  Harm

>
> configure.ac:943: error: possibly undefined macro: AM_GNU_GETTEXT
>       If this token and others are legitimate, please use m4_pattern_allow.
>       See the Autoconf documentation.
> autoreconf: /usr/local/Cellar/autoconf/2.69/bin/autoconf failed with exit status: 1
>
> I guess it’s something about my setup. What are those tarballs I can download that have a configure file? Are those like half-built where someone has already run automake for me?



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

* Re: Using '-1' in a method named '*'
@ 2017-03-03  0:07 Sandic, Aleksandar
  0 siblings, 0 replies; 13+ messages in thread
From: Sandic, Aleksandar @ 2017-03-03  0:07 UTC (permalink / raw)
  To: guile-user@gnu.org

Thank you, everyone. It is working now, I built from HEAD and negating a vector works as expected. This will bridge me over until 2.2 is released.


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

end of thread, other threads:[~2017-03-03  0:07 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-27 10:06 Using '-1' in a method named '*' Alejandro Sanchez
2017-02-27 19:21 ` Andy Wingo
2017-02-27 22:26   ` Alejandro Sanchez
2017-02-28  8:00   ` tomas
2017-02-28  8:54   ` Andy Wingo
2017-02-28  9:19     ` Andy Wingo
2017-02-28 14:24       ` Alejandro Sanchez
2017-02-28 17:53         ` Amirouche
2017-02-28 18:32           ` Ralf Mattes
2017-03-01 17:25         ` Andy Wingo
2017-03-01 23:46           ` Alejandro Sanchez
2017-03-02  0:18             ` Thomas Morley
  -- strict thread matches above, loose matches on Subject: below --
2017-03-03  0:07 Sandic, Aleksandar

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