www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Map Purity

reply "jmh530" <john.michael.hall gmail.com> writes:
My understanding of pure is that a function labeled pure can only 
include pure functions. I've been confused by the fact that a 
function calling map (like below) can be labeled pure without any 
problems. The only way I can rationalize it is that map really 
isn't a function, it's (if I'm understanding it correctly) a 
template that creates a template function that calls a struct 
template.

auto test_map(T)(T x) pure
{
	return x.map!(a => a + a);
}

This is related to
http://forum.dlang.org/thread/ppmokalxdgtszzllzorb forum.dlang.org?page=1
but maybe my question is more basic.
Jun 28 2015
next sibling parent "Xinok" <xinok live.com> writes:
On Sunday, 28 June 2015 at 15:55:51 UTC, jmh530 wrote:
 My understanding of pure is that a function labeled pure can 
 only include pure functions. I've been confused by the fact 
 that a function calling map (like below) can be labeled pure 
 without any problems. The only way I can rationalize it is that 
 map really isn't a function, it's (if I'm understanding it 
 correctly) a template that creates a template function that 
 calls a struct template.

 auto test_map(T)(T x) pure
 {
 	return x.map!(a => a + a);
 }

 This is related to
 http://forum.dlang.org/thread/ppmokalxdgtszzllzorb forum.dlang.org?page=1
 but maybe my question is more basic.
Map isn't explicitly marked as pure. D can infer purity for templated functions, so as long as you give it a pure predicate, map can be inferred as pure. This only works for templated functions; non-templated functions must be explicitly marked as pure.
Jun 28 2015
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 28 June 2015 at 15:55:51 UTC, jmh530 wrote:
 My understanding of pure is that a function labeled pure can 
 only include pure functions. I've been confused by the fact 
 that a function calling map (like below) can be labeled pure 
 without any problems. The only way I can rationalize it is that 
 map really isn't a function, it's (if I'm understanding it 
 correctly) a template that creates a template function that 
 calls a struct template.

 auto test_map(T)(T x) pure
 {
 	return x.map!(a => a + a);
 }

 This is related to
 http://forum.dlang.org/thread/ppmokalxdgtszzllzorb forum.dlang.org?page=1
 but maybe my question is more basic.
There are two aspects to that. First, purity in D is less strict than the concept in functional programming languages. It allows access (even mutation) to anything that is reachable through the parameters. David Nadlinger has written a nice article about the concept and its implications: http://klickverbot.at/blog/2012/05/purity-in-d/ Secondly, `map` is indeed a template function, as you write. For templates functions, the compiler infers many properties, including purity. Neither the `map` function nor the constructor of the struct it returns do anything impure, therefore they are treated as pure. And indeed, the return value of `test_map` will only depend on its arguments, which is the first requirement for purity, and of course it doesn't change global state either, which fulfills the second requirement.
Jun 28 2015
parent reply "jmh530" <john.michael.hall gmail.com> writes:
On Sunday, 28 June 2015 at 16:15:31 UTC, Marc Sch├╝tz wrote:
 Secondly, `map` is indeed a template function, as you write. 
 For templates functions, the compiler infers many properties, 
 including purity.
Thanks for the reply. Two follow ups: 1) Does labeling a template as pure matter if the compiler infers it anyway? 2) Does the compiler also infer anything for safe/nothrow in templates?
Jun 28 2015
parent "anonymous" <anonymous example.com> writes:
On Sunday, 28 June 2015 at 16:28:20 UTC, jmh530 wrote:
 Thanks for the reply. Two follow ups: 1) Does labeling a 
 template as pure matter if the compiler infers it anyway? 2) 
 Does the compiler also infer anything for  safe/nothrow in 
 templates?
1) It means you can't instantiate the template function with arguments that would make it non-pure. 2) Yes, pure, nothrow, safe, nogc are all inferred.
Jun 28 2015