digitalmars.D - Pure and Structs
- dsimcha (26/26) Dec 26 2008 I'm trying to annotate some code with pure and nothrow (mostly just to t...
- dsimcha (4/30) Dec 26 2008 Oh, and I just realize my silly mistake and I apologize. Of course nobo...
- Walter Bright (2/5) Dec 26 2008 If it can be proven (and it can in your example) it can be pure.
- Chad J (6/14) Dec 27 2008 I'm curious, how difficult is it to mechanically prove/disprove purity
- Walter Bright (2/16) Dec 27 2008
I'm trying to annotate some code with pure and nothrow (mostly just to test it out and give feedback at this point), and I've noticed one very irritating thing. If I use a struct as follows: struct LameStruct { uint foo; uint bar; void doStuff() { foo++; bar++; } } void foo() { LameStruct lamestruct; s.doStuff(); } foo() cannot be annotated as pure in this case because technically, it calls doStuff(), which modifies lamestruct. However, I think in this fairly simple case, DMD could reasonably figure out that this function is still referentially transparent and safe for multithreading, at least if LameStruct is defined in the same module as foo(). Of course, no other function can have access to lamestruct before foo() is called because it is created within foo(). Furthermore, doStuff() only modifies value types that it owns, and the return type is void. Will pure eventually be expanded to deal with more cases where the referential transparency and inherent thread-safety of a function is obvious to humans, like these, or is this simply asking too much?
Dec 26 2008
== Quote from dsimcha (dsimcha yahoo.com)'s articleI'm trying to annotate some code with pure and nothrow (mostly just to test it out and give feedback at this point), and I've noticed one very irritating thing. If I use a struct as follows: struct LameStruct { uint foo; uint bar; void doStuff() { foo++; bar++; } } void foo() { LameStruct lamestruct; s.doStuff(); } foo() cannot be annotated as pure in this case because technically, it calls doStuff(), which modifies lamestruct. However, I think in this fairly simple case, DMD could reasonably figure out that this function is still referentially transparent and safe for multithreading, at least if LameStruct is defined in the same module as foo(). Of course, no other function can have access to lamestruct before foo() is called because it is created within foo(). Furthermore, doStuff() only modifies value types that it owns, and the return type is void. Will pure eventually be expanded to deal with more cases where the referential transparency and inherent thread-safety of a function is obvious to humans, like these, or is this simply asking too much?Oh, and I just realize my silly mistake and I apologize. Of course nobody writes referentially transparent functions that return void. Please pretend foo() returns something that would be consistent with it being pure.
Dec 26 2008
dsimcha wrote:Will pure eventually be expanded to deal with more cases where the referential transparency and inherent thread-safety of a function is obvious to humans, like these, or is this simply asking too much?If it can be proven (and it can in your example) it can be pure.
Dec 26 2008
Walter Bright wrote:dsimcha wrote:I'm curious, how difficult is it to mechanically prove/disprove purity in the general case? I thought this was going to be one of those things like escape analysis, where we'd have to settle for a subset of the provable cases for the sake of easy of implementation.Will pure eventually be expanded to deal with more cases where the referential transparency and inherent thread-safety of a function is obvious to humans, like these, or is this simply asking too much?If it can be proven (and it can in your example) it can be pure.
Dec 27 2008
Chad J wrote:Walter Bright wrote:I don't know.dsimcha wrote:I'm curious, how difficult is it to mechanically prove/disprove purity in the general case?Will pure eventually be expanded to deal with more cases where the referential transparency and inherent thread-safety of a function is obvious to humans, like these, or is this simply asking too much?If it can be proven (and it can in your example) it can be pure.I thought this was going to be one of those things like escape analysis, where we'd have to settle for a subset of the provable cases for the sake of easy of implementation.
Dec 27 2008