# Singular

 Page 1 of 1 [ 9 posts ]
 Print view | E-mail friend Previous topic | Next topic
Author Message
 Post subject: detect variable shadowing issuesPosted: Mon Jan 05, 2015 11:52 am
Is it possible to detect in Singular the following issue and warn the user?

Code:
///////////////////////////
ring rng = 0, p ,dp;
poly p = p^2 - p - 1;
p; // **boom**  (   p = -1,  wrong !  )
///////////////////////////

bughunter

Top

 Post subject: Re: detect variable shadowing issuesPosted: Wed Jan 07, 2015 8:47 pm

Joined: Wed May 25, 2005 4:16 pm
Posts: 205
Cannot be done in a sensible/reasonable way because of dynamic scopes of names
- ideas from static scopes (like in C) do not apply.
Example where we do not want a warning:
Code:
ring r1=0,(x,y,z),dp;
ring r2=0,(a,b,x),dp; // but x has already a (different) meaning
// but a warning would be confusing/misleading

Example where a decision about a warning is impossible
Code:
ring r1=0,(x,y),dp;
ring r2=0,(a,b).dp;
int x;// nobody knows that a 'setring r1' follows
setring r1;

and so on...
The precedence rules are not fool-proof but allow to solve these issues.

Top

 Post subject: Re: detect variable shadowing issuesPosted: Fri Jan 09, 2015 2:10 am
Quote:
Example where we do not want a warning:
ring r1=0,(x,y,z),dp;
ring r2=0,(a,b,x),dp; // but x has already a (different) meaning

a naive question (I do not understand yet how the Singular interpreter works):
is it not possible to remove all variables tied to r1 from current scope before the ring change
from r1 to r2 ? Then there would be no warning?

Quote:
ring r1=0,(x,y),dp;
ring r2=0,(a,b).dp;
int x;// nobody knows that a 'setring r1' follows
setring r1;

I would not want to warn at line 'int x',
but at 'setring r1' line.

My intention would be not to help the user to write safe code (which seems impossible without changes to interpreter)
but just to warn that there might be a mistake.

And the only safe way seems (?) not to import ring variables to current scope but access them only like
Code:
r1.x

or
Code:
var(1)
or similar.

So a proposal would be to update all library code to usage of var(), par(), ringlist or
r1.x and so on , warn during the transition time the user if possible and when finished,
refrain from exporting ring variables to current scope?

Top

 Post subject: Re: detect variable shadowing issuesPosted: Tue Jan 13, 2015 5:17 pm

Joined: Wed May 25, 2005 4:16 pm
Posts: 205
Assuming that this reply was not a bad joke:
- "is it not possible to remove all variables tied to r1 from current scope before the ring change from r1 to r2 ?"

This is the way it works: otherwise

int x,y;
ring r=integer,(x,y),dp;

would not have 2 generators.

- " would not want to warn at line 'int x',
but at 'setring r1' line."
but the conflict/mistake is at "int x": setring introduces no new variables.

- Addressing variables as "var(i)" is already possible.
(The form r1.x would introduce other problems: what is r1.x+r2.y?)
Or do you propose to use ONLY that form?
Remember that the user (FYI: a human being) had type that in: and
tab-expansion does not help here. And it would break compatibility with
older versions, with other programs, and with common sense.

- Or do you propose to use a different language for libraries than for the user
input? That is already possible, but we use C/C++ as that language.
Quite useful for special purpose but generally discouraged.
Would require to rewrite ~300 000 lines of code (the existing libraries) and
would double the amount of tests (test 2 interpreters instead of one).

Top

 Post subject: Re: detect variable shadowing issuesPosted: Thu Jan 15, 2015 12:40 pm
Quote:
- " would not want to warn at line 'int x',
but at 'setring r1' line."
but the conflict/mistake is at "int x": setring introduces no new variables.
True, but I still propose (to be discussed) to optionally warn here,
because a simple-minded developer or user would spuriously expect that x refers to a ring variable:

> int x;
...
... tons of other code
...
> ring rng = 0,(x,y),dp;
> x+y;
y // => a baffled user or developer

Quote:
The form r1.x would introduce other problems:
what is r1.x+r2.y?
Initially, an error.

Quote:
Or do you propose to use ONLY that form (r1.x )?
Currently I do not propose to use _only_ that form.

Quote:
Or do you propose
I will be happy if we _find_ a proposal at all on which we agree and which solves the problems of silent bugs
related to variable shadowing

Quote:
- Or do you propose to use a different language for libraries than for the user
input?
No

Quote:
- "is it not possible to remove all variables tied to r1 from current scope before the ring change from r1 to r2 ?"

This is the way it works
ok, good to know. So here is another probably naive or dumb question:

Is it possible to check for variable conflicts between the full variable scope and variables tied to r2
just _after_ all variables tied to r1 were removed from current scope?

> int x;
> ring r1 = 0, y, dp;
> ring r2 = 0,(x,y),dp; // expect a warning for conflict of 'x'

Top

 Post subject: Re: detect variable shadowing issuesPosted: Mon Jan 19, 2015 7:33 pm

Joined: Tue Jun 23, 2009 10:33 pm
Posts: 51
Location: Kaiserslautern
bughunter wrote:
Is it possible to detect in Singular the following issue and warn the user?
Code:
///////////////////////////
ring rng = 0, p ,dp;
poly p = p^2 - p - 1;
p; // **boom**  (   p = -1,  wrong !  )
///////////////////////////

i suppose one would never write "poly p = p;" unless it happens due to execute. therefore never use execute!

the problem happens since the Interprer virtually rewrites
Code:
TYPE NAME = EXPRESSION;

into
Code:
TYPE NAME; // declaration of new variable NAME and its initialization with some default value (0)
NAME = EXPRESSION; // evaluation of EXPRESSION and assigment

Due to performance reasons the assigment has no idea whether NAME was declared just now or not.

Therefore it is thechnically possible to catch such issues but that will result in performance loss.
Moreover such a change is rather complicated to implement (i think at least a change of grammer will be necessary for that).

Regards,
O.

Top

 Post subject: Re: detect variable shadowing issuesPosted: Wed Jan 21, 2015 3:40 pm

Joined: Wed Jan 21, 2015 12:33 pm
Posts: 2
I agree with malex. There are times in which I wish Singular would have a list of names that I am not allowed to use (for objects and for variables) and give an explicit error if I try to use one of the forbidden names (most of the times I'm trying to introduce a variable called "test" while debugging my code), but the work to realize that far outweigh the gain.

There are various promising projects involving alternative interpreters for Singular, perhaps one of them will solve this issue in the long run...? In the meantime, let's trust the users that they know what they are doing when using "execute". Sometimes, it is sadly just not possible to get around that.

Top

 Post subject: Re: detect variable shadowing issuesPosted: Fri Jan 23, 2015 11:43 pm
ren wrote:
I agree with malex. There are times in which I wish Singular would have a list of names that I am not allowed to use (for objects and for variables) and give an explicit error if I try to use one of the forbidden names (most of the times I'm trying to introduce a variable called "test" while debugging my code), but the work to realize that far outweigh the gain.
What do you suggest? Do nothing is not an option (for me).

ren wrote:
There are various promising projects involving alternative interpreters for Singular, perhaps one of them will solve this issue in the long run...? In the meantime, let's trust the users that they know what they are doing when using "execute". Sometimes, it is sadly just not possible to get around that.
Sadly, we can't trust the users or developers, and meanwhile many unsafe code statements are part of the library code.
And, as shown in the initial example, it is not necessary to use 'execute' to hit this bug.
So if we continue without a change, things will became much worse over time (and they did).
And of course a change may feel strange, that is normal.

malex wrote:
Therefore it is thechnically possible to catch such issues but that will result in performance loss. Moreover such a change is rather complicated to implement (i think at least a change of grammer will be necessary for that).
So should we ignore the problem because it is easier? I won't.

@hannes
hannes wrote:
ring r1=0,(x,y,z),dp;
ring r2=0,(a,b,x),dp; // but x has already a (different) meaning
// but a warning would be confusing/misleading
I agree on this with you.

Top

 Post subject: Re: detect variable shadowing issuesPosted: Sat Jan 24, 2015 2:10 pm

Joined: Wed May 25, 2005 4:16 pm
Posts: 205
ren wrote:
There are times in which I wish Singular would have a list of names that I am not allowed to use (for objects and for variables) ....

That is already there:
Code:
reservedName();
or
reservedName("bla");

helps with reserved names, for the currently used names, use:
Code:
defined(bla);
names();
listvar();

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 9 posts ]

 You can post new topics in this forumYou can reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 It is currently Mon Oct 22, 2018 1:42 pm
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group