www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Issue 9148

reply "bearophile" <bearophileHUGS lycos.com> writes:
This is one of the largest problems left in the implementation of 
D purity:

https://issues.dlang.org/show_bug.cgi?id=9148

One example of the refused code:


void foo(const int[] a) {
     int bar() pure {
         return a[0];
     }
}
void main() {}


Bye,
bearophile
Apr 25 2014
next sibling parent "Xinok" <xinok live.com> writes:
On Saturday, 26 April 2014 at 01:57:06 UTC, bearophile wrote:
 This is one of the largest problems left in the implementation 
 of D purity:

 https://issues.dlang.org/show_bug.cgi?id=9148

 One example of the refused code:


 void foo(const int[] a) {
     int bar() pure {
         return a[0];
     }
 }
 void main() {}


 Bye,
 bearophile

I think this would break the D conventions of purity. 'a' is not immutable, only const, meaning one or more elements could change, making 'bar' impure.
Apr 25 2014
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 25 Apr 2014 23:26:29 -0400, Xinok <xinok live.com> wrote:

 On Saturday, 26 April 2014 at 01:57:06 UTC, bearophile wrote:
 This is one of the largest problems left in the implementation of D  
 purity:

 https://issues.dlang.org/show_bug.cgi?id=9148

 One example of the refused code:


 void foo(const int[] a) {
     int bar() pure {
         return a[0];
     }
 }
 void main() {}


 Bye,
 bearophile

I think this would break the D conventions of purity. 'a' is not immutable, only const, meaning one or more elements could change, making 'bar' impure.

It should compile. Purity in D is not the same as the more traditional definition of purity. For example, this compiles: int foo(int[] a) pure {return a[0];} I see no difference from the above. -Steve
Apr 25 2014
prev sibling next sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Sat, 26 Apr 2014 00:44:13 -0400
Steven Schveighoffer via Digitalmars-d <digitalmars-d puremagic.com>
wrote:

 On Fri, 25 Apr 2014 23:26:29 -0400, Xinok <xinok live.com> wrote:
 
 On Saturday, 26 April 2014 at 01:57:06 UTC, bearophile wrote:
 This is one of the largest problems left in the implementation of
 D purity:

 https://issues.dlang.org/show_bug.cgi?id=9148

 One example of the refused code:


 void foo(const int[] a) {
     int bar() pure {
         return a[0];
     }
 }
 void main() {}


 Bye,
 bearophile

I think this would break the D conventions of purity. 'a' is not immutable, only const, meaning one or more elements could change, making 'bar' impure.

It should compile. Purity in D is not the same as the more traditional definition of purity. For example, this compiles: int foo(int[] a) pure {return a[0];} I see no difference from the above.

It's the same, and it isn't. Your example has no access to anything other than a, whereas in bearophile's example, bar has access to its outer scope. So, you can have fun like auto baz = new int[](5); void foo(const int[] a) { int bar() pure { return a[0]; } auto i = bar(); baz[0] = 2; auto j = bar(); } void main() { foo(baz); } Now, if we treat the outer scope like a mutable this pointer (which it probably essentially is underneath the hood), then bar can probably be weakly pure, but we have to be very careful not to consider it strongly pure in spite of the fact that it has no explicit, mutable arguments. Contrast that with int foo(const int[] a) pure { return a[0]; } which is the same as your example save for the const, and that function _can_ be strongly pure if it's passed an immutable array (dmd doesn't currently take strong purity that far - it only treats immutable parameters as strongly pure, not immutable arguments to const parameters -but there's no reason why it couldn't). Your example can't be strongly pure either way though, since its parameter is mutable. In any case, I think that bearophile's example is subtly different from yours, though I think that we can make it weakly pure if we're very careful about how we handle the outer scope. However, I'm not sure that treating it as weakly pure buys us anything except in the case where we're trying to make the outer function pure as well. - Jonathan M Davis
Apr 26 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 However, I'm not sure that treating it as weakly pure buys
 us anything except in the case where we're trying to make
 the outer function pure as well.

Here is a bit more realistic example of the problem, currently this (rather common) code doesn't compile: import std.algorithm, std.array, std.range; int[] foo(in int[] data) pure { auto indexes = iota(int(data.length)); return indexes.map!(i => i * data[i]).array; } void main() {} Bye, bearophile
Apr 26 2014
prev sibling parent Kenji Hara via Digitalmars-d <digitalmars-d puremagic.com> writes:
--047d7b66f59fd6671804fb226792
Content-Type: text/plain; charset=UTF-8

2014-04-26 19:27 GMT+09:00 bearophile via Digitalmars-d <
digitalmars-d puremagic.com>:

 Jonathan M Davis:


  However, I'm not sure that treating it as weakly pure buys
 us anything except in the case where we're trying to make
 the outer function pure as well.

Here is a bit more realistic example of the problem, currently this (rather common) code doesn't compile: import std.algorithm, std.array, std.range; int[] foo(in int[] data) pure { auto indexes = iota(int(data.length)); return indexes.map!(i => i * data[i]).array; } void main() {}

https://github.com/D-Programming-Language/dmd/pull/3626 Kenji Hara --047d7b66f59fd6671804fb226792 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">2014= -04-26 19:27 GMT+09:00 bearophile via Digitalmars-d <span dir=3D"ltr">&lt;<= a href=3D"mailto:digitalmars-d puremagic.com" target=3D"_blank">digitalmars= -d puremagic.com</a>&gt;</span>:<br> <blockquote class=3D"gmail_quote" style=3D"margin-top:0px;margin-right:0px;= margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color= :rgb(204,204,204);border-left-style:solid;padding-left:1ex">Jonathan M Davi= s:<div class=3D""> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin-top:0px;margin-right:0px;= margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color= :rgb(204,204,204);border-left-style:solid;padding-left:1ex"> However, I&#39;m not sure that treating it as weakly pure buys<br> us anything except in the case where we&#39;re trying to make<br> the outer function pure as well.<br> </blockquote> <br></div> Here is a bit more realistic example of the problem, currently this (rather= common) code doesn&#39;t compile:<br> <br> <br> import std.algorithm, std.array, std.range;<br> <br> int[] foo(in int[] data) pure {<br> =C2=A0 =C2=A0 auto indexes =3D iota(int(data.length));<br> =C2=A0 =C2=A0 return indexes.map!(i =3D&gt; i * data[i]).array;<br> }<br> void main() {}<br></blockquote><div><br></div><div><a href=3D"https://githu= b.com/D-Programming-Language/dmd/pull/3626">https://github.com/D-Programmin= g-Language/dmd/pull/3626</a>=C2=A0</div><div><br></div><div>Kenji Hara</div=
</div>

--047d7b66f59fd6671804fb226792--
Jun 05 2014