www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to use pure in D 2.0 question on Stack Overflow?

reply Jeroen Dirks <jeroen.dirks sympatico.ca> writes:
Does anyone know the answer to this D related question on SO?

http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0/
Jun 17 2009
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Jeroen,

 Does anyone know the answer to this D related question on SO?
 
 http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0/
 
DMD can't tell that the method call is safe even though it is inpure.
Jun 17 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
BCS wrote:
 Reply to Jeroen,
 
 Does anyone know the answer to this D related question on SO?

 http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0/
DMD can't tell that the method call is safe even though it is inpure.
Also, the purity of a function is determined by its signature, not by its implementation.
Jun 17 2009
parent reply div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Walter Bright wrote:
 BCS wrote:
 Reply to Jeroen,

 Does anyone know the answer to this D related question on SO?

 http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0/
DMD can't tell that the method call is safe even though it is inpure.
Also, the purity of a function is determined by its signature, not by its implementation.
Surely the heap itself is global state? So creating a new object should be forbidden. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFKOr3pT9LetA9XoXwRAtnUAKC7QklSOjnu/O57K2NfRMh7UkitKgCeIo9/ 5M2DTIKLPzBW5Oa4UqfrZtw= =cYCg -----END PGP SIGNATURE-----
Jun 18 2009
parent Jesse Phillips <jessekphillips gmail.com> writes:
On Thu, 18 Jun 2009 23:21:29 +0100, div0 wrote:

 
 Surely the heap itself is global state? So creating a new object should
 be forbidden.
 
Isn't the stack just as global as the heap? By global it isn't referring to storage location but the scoping that is enforced by the compiler.
Jun 18 2009
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Jeroen Dirks:
 Does anyone know the answer to this D related question on SO?
 http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0/
I like this answer:
you may have found case where the compiler's semantic analysis is not as good
as your brain's.<
"pure" is something that has being added recently to D, so its semantics isn't fully refined yet. Walter & Don will hopefully keep working on it (adding optimizations, like pulling out pure calls from loops, adopting some strategy to keep purity even regarding changes in FP rounding modes, improving purity analysis for classes, etc) removing some of the current faults/limits. But we have to keep looking for corner cases and possible bugs. This is something I have written recently, Don may be interested: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=16907 Bye, bearophile
Jun 17 2009
prev sibling next sibling parent reply Jason House <jason.james.house gmail.com> writes:
Jeroen Dirks Wrote:

 Does anyone know the answer to this D related question on SO?
 
 http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0/
addMsg is not marked pure and pure functions can't call impure functions... I'm not sure if addMsg can be marked pure or not. It does not touch global state but does have side effects. The key is if side effects to on the containing object or not. (I haven't used pure enough to know. Many functions in Phobos are not marked as pure even though they could be. That limits pure's use for me)
Jun 17 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
Jason House:
Many functions in Phobos are not marked as pure even though they could be. That
limits pure's use for me)<
If you list them then Walter may later fix Phobos. Bye, bearophile
Jun 17 2009
parent reply Jason House <jason.james.house gmail.com> writes:
bearophile Wrote:

 Jason House:
Many functions in Phobos are not marked as pure even though they could be. That
limits pure's use for me)<
If you list them then Walter may later fix Phobos.
I did file an entry I'm bugzilla for std.cpuid. It was marked as a dup of the entry for std.math and then only std.math got fixed. I should probably reopen the bug report
Jun 18 2009
parent Don <nospam nospam.com> writes:
Jason House wrote:
 bearophile Wrote:
 
 Jason House:
 Many functions in Phobos are not marked as pure even though they could be.
That limits pure's use for me)<
If you list them then Walter may later fix Phobos.
I did file an entry I'm bugzilla for std.cpuid. It was marked as a dup of the entry for std.math and then only std.math got fixed. I should probably reopen the bug report
std.cpuid will be removed. The functionality is going into druntime.
Jun 30 2009
prev sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 18 Jun 2009 00:32:20 +0400, Jeroen Dirks  
<jeroen.dirks sympatico.ca> wrote:

 Does anyone know the answer to this D related question on SO?

 http://stackoverflow.com/questions/1008803/how-to-use-pure-in-d-2-0/
There needs to be a way to tell that addMsg doesn't access global variables. One of the solutions is to add one more annotation (I call it impure here, because it is an exact opposite of pure): void foo() // doesn't access globals, fine to call inside pure functions { } void bar() // fails to compile, accesses global state but doesn't have proper annotation { errno = 42; } impure void bar() // fine { errno = 42; } But there might be a problem: calling impure function makes your function impure, too, so it's viral. Another solution is... remove global variables entirely! No globals - no problems :)
Jun 18 2009
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Denis Koroskin:
There needs to be a way to tell that addMsg doesn't access global variables.<
Another solution is... remove global variables entirely! No globals - no 
problems :)<
"global variables" are names in the outer scope for nested functions, so that doesn't solve the problem, and generally kills C compatibility of D. Time ago I have suggested a keyword like uses or outer, to explicitly list what names a function uses from the outer scope: int z; int bar(int k) uses out z { int y = k * 2; int foo(int x) uses in y, out z { z = y * 2; return x * y; } return k + foo(k * 3); } But this example is a mess already, better to not encourage the usage of such coding style. Bye, bearophile
Jun 18 2009
prev sibling parent Jeroen Dirks <jeroen.dirks sympatico.ca> writes:
It seems that we are back to making copied all over the place because
the compiler can not tell if we would modify something shared.

If the compiler would be smarter and maybe with the help of some
annotations on the functions called can figure out what data is owned
by a pure operation then you might have a real killer feature here.

1 Compiler guarantees related to locality of changes (without need for
locks everywhere)
2 Still write imperative style code.
3 Not having to do copy on write all the time so that old object is
not changed.

Maybe something like this could be added:

class TestObj
{
   init( int v )
   {
      value = v;
   }
   int value;
}

class Log
{
   internal string[] msg;
   internal TestObj[] values;
   local addMsg( string s ) // marked local since it only changes
internal data
   {
      msg ~= s;
   }
   local addValue( int v )
   {
      values != new TestObj( v )
   }
   local incValues()
   {
      for ( v; values ) {
         v.value += 1;
      }
   }
   pure string[] getMsgs() {
      return msg.dup();  // have to make a copy since it is internal
   }
   // following would be compiler errors:
   string[] getMsg2()
   {
      return msg;  // error: references to internal can not be
returned or exported somehow
   }
   local addValue2( TestObj t )
   {
      values ~= tl // error: can not assign external object to
internal data
   }
};

pure Log WorkWithObj()
{
   Log l = new Log();
   l.addMsg("Hello");
   l.addMsg("World");
   l.addValue( 41 );
   l.addValue( 9 );
   l.incValues();
   return l;
}
Jun 18 2009