www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Wikipedia purity example and discussion

reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
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
next sibling parent retard <re tard.com.invalid> writes:
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.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?
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); // fails
Nov 06 2010
prev sibling parent reply Lutger <lutger.blijdestijn gmail.com> writes:
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.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?
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'
Nov 06 2010
parent spir <denis.spir gmail.com> writes:
On Sat, 06 Nov 2010 13:09:07 +0100
Lutger <lutger.blijdestijn gmail.com> wrote:

 Vladimir Panteleev wrote:
=20
 A 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
=20
http://en.wikipedia.org/wiki/Talk:D_(programming_language)#Purity_of_mySu=
m_function_in_the_.22Functional.22_section_.281.1.4.29
=20
 I replied with:
=20
 The code indeed compiles. I think that the idea is that nested functio=
ns
 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.
=20 I don't know much about purity, so I thought someone could shed some light on this? =20
=20 I think the wikipedia example is wrong and that it compiles is related to=
=20
 these 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=20
 give 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