Opened 14 years ago

Closed 10 years ago

#95 closed proposed feature (fixed)

Aliases in Singular?

Reported by: Oleksandr Owned by: Oleksandr
Priority: minor Milestone: 3-1-1
Component: singular-kernel Version:
Keywords: alias Cc: seelisch

Description

At the moment we have at least one alias: basering, which is used very often.

I think it would be useful to have generic aliases. For instance alias parameters to procedures will allow one to save memory by making no copies for huge objects and set attribues for complicated reusable objects for later reuse and caching of already computed object properties etc...

Since alias is just a syntax sugar for object name, at first we may simply add a function to query an object by (string) name, and come up with a new syntax for alias later on.

Attachments (1)

aliases.sing (860 bytes) - added by Oleksandr 14 years ago.
My examples

Download all attachments as: .zip

Change History (10)

comment:1 Changed 14 years ago by Oleksandr

Component: ALLsingular-kernel
Keywords: alias added
Owner: changed from somebody to hannes
Priority: majorminor

We already have listvar:

> ring r;
> poly p = var(1);
> listvar();
// r                    [0]  *ring
//      p                    [0]  poly
> listvar(r);
// r                    [0]  *ring
// p                    [0]  poly
> listvar(p);
// p                    [0]  poly x, 1 monomial(s)

comment:2 Changed 14 years ago by hannes

Nice idea, but difficult to do - and difficult to manage: what we really want here is a "call by name" for procedure parameters.

The other aspaect: reference objects by its name - is laready in Singular (since version 1.2, if my memory is correct): see `

comment:3 Changed 14 years ago by Oleksandr

I agree - this is what is actually needed.

Where can one look for "reference objects by its name"?

Besides: I was thinking about C++-like syntax:

ring r; poly p = var(1);
poly& A = p;

proc Test(poly p){}

Test(A); // makes a copy of p 

proc TestA(poly& p){}

TestA(A); // no convertion/copy!
TestA(p); // p is implicitly converted into an alias and thus no copy

It seems to me that aliases should be just plain variables and have the same parents/contexts as original variables.

comment:4 in reply to:  3 Changed 14 years ago by gorzel

Replying to motsak:

Where can one look for "reference objects by its name"?

See my (guest) answer in:

http://www.singular.uni-kl.de/forum/viewtopic.php?t=1680

` ` is mentioned in the Handbook as special character 3.5.2, but seems not to be explained.

comment:5 Changed 14 years ago by Oleksandr

Cc: seelisch added
Owner: changed from hannes to Oleksandr

Thanks for explaing!

It seems that my description above is a bit confusing, so please let me explain: in Singular interpretator Objects (rings/ideals/modules/polys etc) have name, ID, type, attributes... So i imagined myself simply some special syntax for the approach 2 below:

ring r;ideal I = maxideal(1);ideal J = I;

// Approach 1: do evrything by hand inline:

proc computeReg(def I) // COPY! Bad for HUGE objects!
{
  if( typeof(attrib(I, "reg")) == "string" )
  {
    "............. LOOOOONG COMPUTATIONS HERE .................... ";
    attrib(I, "reg", 11);
  }
  return( attrib(I, "reg") );
}

attrib(I); // no attributes
attrib(I, "reg", computeReg(I)); // Main step! This is very limited and may be very complicated!
attrib(I); // 'reg'!!!

computeReg(I); // No long computations any more!!!


// Approach 2: alias == 'name of object'

proc computeReg2(n) // NO OBJECT COPY! Simply use `name` instead of object everywhere...
{
  if( typeof(attrib(`n`, "reg")) == "string" )
  {
    "............. LOOOOONG COMPUTATIONS HERE .................... ";
    attrib(`n`, "reg", 11);
  }
  return( attrib(`n`, "reg") );
}

attrib(J); // no attribues
computeReg2(nameof(J)); // Almost perfect but one needs to write nameof(J) instead of J!!!
attrib(J); // 'reg'!!!!

computeReg2(nameof(J)); // No long computations any more!!!

comment:6 Changed 14 years ago by Oleksandr

Frank, wrote:

The appropriate data structure for what you would like to have is a cache from which pre-computed objects may be retrieved by name and/or id, and/or some other sensible mechanism of querying.
I can see your point, as it would make programming easier sometimes. But I also see a problem with that, as we do not have object locking in our C/C++ Singular code. That means that once an object has been created, our Singular code does NOT ensure that this object will stay unmodified during its lifetime. Especially - as I understand our code - parameters of procedures are quite often side-effected (and thus modified) inside the procedure. But this implies that cached objects may be subject to unexpected changes (assuming that you would want to cache by reference) unless you cache physical copies of these objects.
We should talk about that after the new release; perhaps in our next meeting. 

comment:7 in reply to:  6 Changed 14 years ago by Oleksandr

Replying to seelisch:

The appropriate data structure for what you would like to have is a cache from which pre-computed objects may be retrieved by name and/or id, and/or some other sensible mechanism of querying. I can see your point, as it would make programming easier sometimes. But I also see a problem with that, as we do not have object locking in our C/C++ Singular code. That means that once an object has been created, our Singular code does NOT ensure that this object will stay unmodified during its lifetime. Especially - as I understand our code - parameters of procedures are quite often side-effected (and thus modified) inside the procedure. But this implies that cached objects may be subject to unexpected changes (assuming that you would want to cache by reference) unless you cache physical copies of these objects.

There seems to be some confusion, please see my post above.

We should talk about that after the new release; perhaps in our next meeting.

Clearly this will not be done for the new release.

Changed 14 years ago by Oleksandr

Attachment: aliases.sing added

My examples

comment:8 Changed 14 years ago by seelisch

Milestone: Releases 3-1-1 and higher

comment:9 Changed 10 years ago by Oleksandr

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.