digitalmars.D - __has_side_effects
- Andrei Alexandrescu (9/9) Mar 30 2018 https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/
- Stefan Koch (8/18) Mar 30 2018 It's actually quite easy.
- Shachar Shemesh (10/31) Mar 31 2018 That would work in C++. It does not work with D. D is willing to call
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (9/17) Mar 31 2018 It's the exact equivalent of this code:
- Andrei Alexandrescu (6/24) Mar 31 2018 Yah, only strongly pure functions would qualify. Indeed that's easy for
- Nicholas Wilson (3/8) Mar 31 2018 Surely you mean __traits(isStronglyPure, expression)?
- Andrei Alexandrescu (2/13) Mar 31 2018 Affirmative, thx for the correction.
- Mark (12/17) Apr 01 2018 It can be used for some rudimentary compiler checks. Namely, in
- Uknown (4/14) Mar 31 2018 Just for the fun of it, I implemented this as a template:
- Nicholas Wilson (3/23) Mar 31 2018 You're missing classes, AAs, and pointers (possibly function
- Uknown (3/13) Mar 31 2018 I knew I was missing something. Fixed it, thanks
- Uknown (5/8) Mar 31 2018 Sorry for the spam, but I also managed to miss `immutable`,
- Andrei Alexandrescu (18/31) Apr 01 2018 That's a great initiative, and a worthy trait for the stdlib. I think
- Uknown (7/14) Apr 01 2018 I got a working implementation that satisfies your requirements
- Andrei Alexandrescu (2/17) Apr 01 2018 Terrific, thanks!!
- Uknown (3/9) Apr 01 2018 Created the PR: https://github.com/dlang/phobos/pull/6403
- Jonathan M Davis (16/41) Apr 01 2018 In principle, a function which has const parameters could be treated as
- Andrei Alexandrescu (3/5) Apr 01 2018 I want to give coders leeway to cheat on that. I'll explain later
https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/ Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects. In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier. I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use. Andrei
Mar 30 2018
On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/ Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects. In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier. I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use. AndreiIt's actually quite easy. make a function literal annotated with pure and call your function, if that returns the function had no side effects. if something is constant you can determine by using at in ctfe context, if that function call does not compile then it is not a constant.
Mar 30 2018
On 30/03/18 23:35, Stefan Koch wrote:On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:That would work in C++. It does not work with D. D is willing to call functions that have side effects "pure". struct S { int a; void func(int b) pure { // For some strange reason, this is not considered a pure violation. a+=b; } }https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/ Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects. In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier. I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use. AndreiIt's actually quite easy. make a function literal annotated with pure and call your function, if that returns the function had no side effects. if something is constant you can determine by using at in ctfe context, if that function call does not compile then it is not a constant.
Mar 31 2018
On Saturday, 31 March 2018 at 19:18:24 UTC, Shachar Shemesh wrote:struct S { int a; void func(int b) pure { // For some strange reason, this is not considered a pure violation. a+=b; } }It's the exact equivalent of this code: void func(ref S s, int b) pure { S.a += b; } And that code is perfectly pure accordion to D rules - it does not modify any data not reachable through its arguments. -- Simen
Mar 31 2018
On 3/31/18 4:01 PM, Simen Kjærås wrote:On Saturday, 31 March 2018 at 19:18:24 UTC, Shachar Shemesh wrote:Yah, only strongly pure functions would qualify. Indeed that's easy for the compiler to figure - so I'm thinking pragma(isStronglyPure, expression) would be easy to define. What would be some good uses of this? Andreistruct S { int a; void func(int b) pure { // For some strange reason, this is not considered a pure violation. a+=b; } }It's the exact equivalent of this code: void func(ref S s, int b) pure { S.a += b; } And that code is perfectly pure accordion to D rules - it does not modify any data not reachable through its arguments.
Mar 31 2018
On Saturday, 31 March 2018 at 20:44:13 UTC, Andrei Alexandrescu wrote:Yah, only strongly pure functions would qualify. Indeed that's easy for the compiler to figure - so I'm thinking pragma(isStronglyPure, expression) would be easy to define. What would be some good uses of this? AndreiSurely you mean __traits(isStronglyPure, expression)?
Mar 31 2018
On 3/31/18 8:35 PM, Nicholas Wilson wrote:On Saturday, 31 March 2018 at 20:44:13 UTC, Andrei Alexandrescu wrote:Affirmative, thx for the correction.Yah, only strongly pure functions would qualify. Indeed that's easy for the compiler to figure - so I'm thinking pragma(isStronglyPure, expression) would be easy to define. What would be some good uses of this? AndreiSurely you mean __traits(isStronglyPure, expression)?
Mar 31 2018
On Saturday, 31 March 2018 at 20:44:13 UTC, Andrei Alexandrescu wrote:Yah, only strongly pure functions would qualify. Indeed that's easy for the compiler to figure - so I'm thinking pragma(isStronglyPure, expression) would be easy to define. What would be some good uses of this? AndreiIt can be used for some rudimentary compiler checks. Namely, in some contexts it doesn't make sense for an expression to have (or rather not have) any side effects: - The increment section in a for loop must have a side effect, unless it's empty. - An assert expression shouldn't have any side effects (because assertions disappear in release mode). Maybe the compiler already does such checks, I don't know. In general this seems far less useful than the concept of a pure function.
Apr 01 2018
On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/ Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects. In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier. I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use. AndreiJust for the fun of it, I implemented this as a template: https://run.dlang.io/is/pXLndG
Mar 31 2018
On Sunday, 1 April 2018 at 04:11:37 UTC, Uknown wrote:On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:You're missing classes, AAs, and pointers (possibly function pointers and delegates as well).https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/ Discussion aside, I notice with pleasant surprise gcc has an introspection primitive we didn't think of: __is_constant that (I assume) yields true if the given expression has no side effects. In D the primitive would be called e.g. __has_side_effects to avoid confusion with the "const" qualifier. I wonder how difficult it would be to define __has_side_effects (it would apply to an alias) and how it can be put to good use. AndreiJust for the fun of it, I implemented this as a template: https://run.dlang.io/is/pXLndG
Mar 31 2018
On Sunday, 1 April 2018 at 04:30:03 UTC, Nicholas Wilson wrote:On Sunday, 1 April 2018 at 04:11:37 UTC, Uknown wrote:I knew I was missing something. Fixed it, thanks https://run.dlang.io/is/tZeZrPOn Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:You're missing classes, AAs, and pointers (possibly function pointers and delegates as well).[...]Just for the fun of it, I implemented this as a template: https://run.dlang.io/is/pXLndG
Mar 31 2018
On Sunday, 1 April 2018 at 05:27:38 UTC, Uknown wrote:[...] I knew I was missing something. Fixed it, thanks https://run.dlang.io/is/tZeZrPSorry for the spam, but I also managed to miss `immutable`, `const` and when T has mutable indirections Final version that I'm sure covers all the cases: https://run.dlang.io/is/kGoU4X
Mar 31 2018
On 4/1/18 2:22 AM, Uknown wrote:On Sunday, 1 April 2018 at 05:27:38 UTC, Uknown wrote:That's a great initiative, and a worthy trait for the stdlib. I think you'd have an easier time if you reasoned from the other end. A function is strongly pure if all of the following are true: * Each parameter: - is immutable, OR - can be converted automatically to immutable (i.e. has no mutable indirections) AND is passed by value * The return type: - is immutable, OR - can be converted automatically to immutable (We don't want to give const this much power yet for other reasons.) The template should support taking the function name as a string, too, and the parameter types so as to easily distinguish across overloads. This would be a great addition to std.traits. Once we have it, we'll have a precise unified definition of strongly pure across the language spec and the stdlib definition. Please follow up, thanks! Andrei[...] I knew I was missing something. Fixed it, thanks https://run.dlang.io/is/tZeZrPSorry for the spam, but I also managed to miss `immutable`, `const` and when T has mutable indirections Final version that I'm sure covers all the cases: https://run.dlang.io/is/kGoU4X
Apr 01 2018
On Sunday, 1 April 2018 at 10:23:40 UTC, Andrei Alexandrescu wrote:On 4/1/18 2:22 AM, Uknown wrote:I got a working implementation that satisfies your requirements in about 60 lines. I will make a Pull Request as soon as I write the Docs and unittests. Here's the implementation: https://run.dlang.io/is/kVpv36[...]That's a great initiative, and a worthy trait for the stdlib. I think you'd have an easier time if you reasoned from the other end. A function is strongly pure if all of the following are true: [...]
Apr 01 2018
On 4/1/18 9:39 AM, Uknown wrote:On Sunday, 1 April 2018 at 10:23:40 UTC, Andrei Alexandrescu wrote:Terrific, thanks!!On 4/1/18 2:22 AM, Uknown wrote:I got a working implementation that satisfies your requirements in about 60 lines. I will make a Pull Request as soon as I write the Docs and unittests. Here's the implementation: https://run.dlang.io/is/kVpv36[...]That's a great initiative, and a worthy trait for the stdlib. I think you'd have an easier time if you reasoned from the other end. A function is strongly pure if all of the following are true: [...]
Apr 01 2018
On Sunday, 1 April 2018 at 14:33:14 UTC, Andrei Alexandrescu wrote:On 4/1/18 9:39 AM, Uknown wrote:Created the PR: https://github.com/dlang/phobos/pull/6403On Sunday, 1 April 2018 at 10:23:40 UTC, Andrei Alexandrescu wrote:Terrific, thanks!!On 4/1/18 2:22 AM, Uknown wrote:[...]
Apr 01 2018
On Sunday, April 01, 2018 06:23:40 Andrei Alexandrescu via Digitalmars-d wrote:On 4/1/18 2:22 AM, Uknown wrote:In principle, a function which has const parameters could be treated as strongly pure if it's given immutable arguments, but I don't believe that the language does that, and in that case, you couldn't just test a function to see if it was strongly pure, since in some cases, it would depend on the arguments. So, to test for strong purity, you'd need both the function and the arguments, meaning that it wouldn't so much be the case that a function was strongly pure as a function call was strongly pure. Such a change to strong purity would increase the number of optimizations that could be based on pure, but I don't know that it would be worth it, especially since it would then be much harder to have a trait like this, and functions are so rarely called with the same arguments in the same expression or statement that I'm not sure that such a change would really add much in the way of optimization opportunities. - Jonathan M DavisOn Sunday, 1 April 2018 at 05:27:38 UTC, Uknown wrote:That's a great initiative, and a worthy trait for the stdlib. I think you'd have an easier time if you reasoned from the other end. A function is strongly pure if all of the following are true: * Each parameter: - is immutable, OR - can be converted automatically to immutable (i.e. has no mutable indirections) AND is passed by value * The return type: - is immutable, OR - can be converted automatically to immutable (We don't want to give const this much power yet for other reasons.)[...] I knew I was missing something. Fixed it, thanks https://run.dlang.io/is/tZeZrPSorry for the spam, but I also managed to miss `immutable`, `const` and when T has mutable indirections Final version that I'm sure covers all the cases: https://run.dlang.io/is/kGoU4X
Apr 01 2018
On 4/1/18 9:46 AM, Jonathan M Davis wrote:In principle, a function which has const parameters could be treated as strongly pure if it's given immutable argumentsI want to give coders leeway to cheat on that. I'll explain later (allocators).
Apr 01 2018