digitalmars.D - Wikipedia purity example and discussion
- Vladimir Panteleev (12/17) Nov 06 2010 A while ago, someone added an example with pure functions to Wikipedia's...
A while ago, someone added an example with pure functions to Wikipedia's D article: http://en.wikipedia.org/wiki/D_(programming_language)#Functional Someone on the talk page asked why does the program compile despite that mySum accesses a variable in its enclosing function: http://en.wikipedia.org/wiki/Talk:D_(programming_language)#Purity_of_mySum_function_in_the_.22Functional.22_section_.281.1.4.29 I replied with:The code indeed compiles. I think that the idea is that nested functions have a hidden argument - a pointer to their enclosing scope (main's local variables). However, that doesn't explain why the code continues to compile when pivot is moved outside main(), or if you add a call to a non-pure function in mySum - these sound like compiler bugs.I don't know much about purity, so I thought someone could shed some light on this? -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Nov 06 2010
Sat, 06 Nov 2010 13:49:20 +0200, Vladimir Panteleev wrote:A while ago, someone added an example with pure functions to Wikipedia's D article: http://en.wikipedia.org/wiki/D_(programming_language)#Functional Someone on the talk page asked why does the program compile despite that mySum accesses a variable in its enclosing function: http://en.wikipedia.org/wiki/Talk:D_(programming_language)#Purity_of_mySum_function_in_the_.22Functional.22_section_.281.1.4.29I replied with:The function isn't referentially transparent, at least. Well, it is in that particular example if the compiler can "infer" the constness of 'pivot'. Otherwise the value requires a constness attribute. Depends on whether we are splitting hair again, right? auto x = my_sum(a,b); pivot = 42; auto y = my_sum(a,b); assert(x == y); // failsThe code indeed compiles. I think that the idea is that nested functions have a hidden argument - a pointer to their enclosing scope (main's local variables). However, that doesn't explain why the code continues to compile when pivot is moved outside main(), or if you add a call to a non-pure function in mySum - these sound like compiler bugs.I don't know much about purity, so I thought someone could shed some light on this?
Nov 06 2010
Vladimir Panteleev wrote:A while ago, someone added an example with pure functions to Wikipedia's D article: http://en.wikipedia.org/wiki/D_(programming_language)#Functional Someone on the talk page asked why does the program compile despite that mySum accesses a variable in its enclosing function:http://en.wikipedia.org/wiki/Talk:D_(programming_language)#Purity_of_mySum_function_in_the_.22Functional.22_section_.281.1.4.29I replied with:I think the wikipedia example is wrong and that it compiles is related to these bugs: http://d.puremagic.com/issues/show_bug.cgi?id=5006 http://d.puremagic.com/issues/show_bug.cgi?id=4640 if mysum is instead declared in this way as suggested by bug 4640 dmd does give an error: int mysum(int a, int b) pure ... Error: pure nested function 'mysum' cannot access mutable data 'pivot'The code indeed compiles. I think that the idea is that nested functions have a hidden argument - a pointer to their enclosing scope (main's local variables). However, that doesn't explain why the code continues to compile when pivot is moved outside main(), or if you add a call to a non-pure function in mySum - these sound like compiler bugs.I don't know much about purity, so I thought someone could shed some light on this?
Nov 06 2010
On Sat, 06 Nov 2010 13:09:07 +0100 Lutger <lutger.blijdestijn gmail.com> wrote:Vladimir Panteleev wrote: =20m_function_in_the_.22Functional.22_section_.281.1.4.29A while ago, someone added an example with pure functions to Wikipedia's D article: =20 http://en.wikipedia.org/wiki/D_(programming_language)#Functional =20 Someone on the talk page asked why does the program compile despite that mySum accesses a variable in its enclosing function: =20 =20http://en.wikipedia.org/wiki/Talk:D_(programming_language)#Purity_of_mySu=ns=20 I replied with: =20The code indeed compiles. I think that the idea is that nested functio=ahave a hidden argument - a pointer to their enclosing scope (main's local variables). However, that doesn't explain why the code continues to compile when pivot is moved outside main(), or if you add a call to==20=20 I think the wikipedia example is wrong and that it compiles is related to=non-pure function in mySum - these sound like compiler bugs.=20 I don't know much about purity, so I thought someone could shed some light on this? =20these bugs: =20 http://d.puremagic.com/issues/show_bug.cgi?id=3D5006 http://d.puremagic.com/issues/show_bug.cgi?id=3D4640 =20 if mysum is instead declared in this way as suggested by bug 4640 dmd doe=s=20give an error: =20 int mysum(int a, int b) pure ... =20 Error: pure nested function 'mysum' cannot access mutable data 'pivot'Yes, for sure. Otherwise what is "pure" supposed to mean? Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 06 2010