Re: Purity in D – new article

2012-05-30 Thread Iain Buclaw
On 27 May 2012 21:56, David Nadlinger  wrote:
> Some of you might remember that I have been meaning to write a comprehensive
> introduction to design and use of purity for quite some while now – I
> finally got around to do so:
>
> http://klickverbot.at/blog/2012/05/purity-in-d/
>
> Feedback and criticism of all kinds very welcome!
>
> David

Enjoyable read. :)

Makes me think about getting round to writing something someday...


-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';


Re: Purity in D – new article

2012-05-30 Thread Don Clugston

On 29/05/12 19:35, David Nadlinger wrote:

On Tuesday, 29 May 2012 at 12:08:08 UTC, Don Clugston wrote:

And to set the record straight -- the relaxed purity ideas were not my
idea.
I forget who first said them, but it wasn't me. I just championed them.


Unfortunately, I don't quite remember either – was it Bruno Medeiros? In
any case, if somebody can help my memory here, I'd be glad to give
credit to the one who came up with the original proposal in the article
as well.

David


The successful proposal, using "weakly pure/strongly pure" (Sep 21 2010):

http://www.digitalmars.com/d/archives/digitalmars/D/Proposal_Relax_rules_for_pure_117735.html

It"s basically the same as this one by Bruno (Apr 29 2008), which uses 
"partially pure" and mentions an earlier post by me:


http://www.digitalmars.com/d/archives/digitalmars/D/Idea_partially_pure_functions_70762.html#N70762

And the earliest reference I could find is by me (Apr 5 2008) where I 
called it an "amoral" function.


http://www.digitalmars.com/d/archives/digitalmars/D/Grafting_Functional_Support_on_Top_of_an_Imperative_Language_69253.html

The first compiler release with pure function attributes (though not 
implemented) was released in Apr 22, 2008 and the first with pure as a 
keyword was Jan 20 2008.

So surely this is close to the original.

So now I'm confused, maybe it *was* me after all!
Then formalized by Bruno, and later championed by me?



Re: Purity in D – new article

2012-05-29 Thread David Nadlinger

On Tuesday, 29 May 2012 at 18:46:21 UTC, Ali Çehreli wrote:
Unfortunately, that does not compile with dmd 2.059 and I think 
that I understand why. Would that be possible for you to make 
it clear under what conditions the return value can be 
converted to immutable.


Okay, thanks for the feedback – will see about clarifying the 
section.


David


Re: Purity in D – new article

2012-05-29 Thread Ali Çehreli

On 05/27/2012 01:56 PM, David Nadlinger wrote:

Some of you might remember that I have been meaning to write a
comprehensive introduction to design and use of purity for quite some
while now – I finally got around to do so:

http://klickverbot.at/blog/2012/05/purity-in-d/

Feedback and criticism of all kinds very welcome!

David


Thank you very much for the article, which should already be translated 
to other languages. ;)


In addition to having a much clearer idea on the topic after reading 
your article, I have also been pleasantly surprised that one of my 
problems with the type system has also been solved by 'pure'.


I had posted about this topic on the d.D.learn forum before, that the 
return value of the following function should implicitly be convertible 
to immutable:


import std.stdio;

char[] repeat(dchar d, size_t n)
{
char[] result;

foreach (i; 0 .. n) {
result ~= d;
}

return result;
}

void main()
{
char[] c = repeat('-', 2);
string s = repeat('=', 3);  // <- compilation error
}

Now I see that my problem has been not marking repeat() as 'pure'. The 
following works. Awesome! :)


char[] repeat(dchar d, size_t n) pure
{
// ...
}

However, I would like to make a suggestion about that section in the 
article. Under the "pure and immutable – again?" section, you say


"The effects of const and immutable on referential
 transparency have already been discussed at length"

and then continue with

"the return value of pure functions can in some cases be
 safely cast to immutable".

That gave me the impression that the implicit conversion to immutable of 
the return value would still work even with the following definition of 
primes():


ulong[] primes(uint n, immutable char[] i, const char[] c)
{
// ...
}

Unfortunately, that does not compile with dmd 2.059 and I think that I 
understand why. Would that be possible for you to make it clear under 
what conditions the return value can be converted to immutable.


Once again, thank you very much for a great article,
Ali

--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html


Re: Purity in D – new article

2012-05-29 Thread David Nadlinger

On Tuesday, 29 May 2012 at 12:08:08 UTC, Don Clugston wrote:
And to set the record straight -- the relaxed purity ideas were 
not my idea.
I forget who first said them, but it wasn't me. I just 
championed them.


Unfortunately, I don't quite remember either – was it Bruno 
Medeiros? In any case, if somebody can help my memory here, I'd 
be glad to give credit to the one who came up with the original 
proposal in the article as well.


David


Re: Purity in D – new article

2012-05-29 Thread Don Clugston

On 27/05/12 22:56, David Nadlinger wrote:

Some of you might remember that I have been meaning to write a
comprehensive introduction to design and use of purity for quite some
while now – I finally got around to do so:

http://klickverbot.at/blog/2012/05/purity-in-d/

Feedback and criticism of all kinds very welcome!

David


For the part about floating-point calculations:

As this would be an impractical restriction, in D pure functions are 
allowed to read and write floating point flags
+ (ie, the floating point state is regarded as a variable implicitly 
passed to every pure function).



And to set the record straight -- the relaxed purity ideas were not my idea.
I forget who first said them, but it wasn't me. I just championed them.



Re: Purity in D – new article

2012-05-28 Thread bearophile

Gour:


How many are on a single CPU today and in the nearby tomorrow?


This question turns this discussion into something vast and 
complex. The short answer is that if your purpose is to go fast, 
those immutable data structures don't seem the solution. Please 
take a look at what data structures and algorithms are used where 
people have to perform large amounts of processing (Google data 
centers, modern video games, sensory processing, and so on).


If your computational hardware looks like a GPU, with thousands 
(or more) very small cores able to run all similar code, code 
that contains few jumps and conditions, the best data structures 
aren't those. Take a look at how they implement Support Vector 
Machines on GPUs or other advanced data structures fit for that 
computational iron. There was a very active research on this.


Today I have seen cases where low-indirection-ratio data 
structures used by plain looking C programs running on a single 
core are faster (and use 1/15 of the RAM) than Clojure programs 
running on 8 cores (and I don't think having 16 cores will change 
those specific cases). This happens because (among other things) 
communication between cores has a cost, and there are parts of 
the programs that don't parallelize. Single cores that are 
programmed to work smartly on their L1 and L2 caches are fast.


In future we'll have many cores, but physics tells us that 
communication and synchronization can't be for free. So for 
several not so easily parallelized algorithms you will keep 
wanting to use not tiny cores/memory organized in a tree of 
progressively more costly communication and synchronization. For 
such iron structure, I think languages like X10 and Chapel have 
far better chance to produce efficient code than Clojure and it 
current standard data structures.


You are able to build data structures more fit for heavy 
computation for Haskell too, but Chapel and X10 languages are 
built around them from the start (example: in Chapel it's easy to 
design _complex hierarchical tiling_ strategies for data 
structures, that to me seem the key to work efficiently on the 
modern pyramid-shaped design of memories and their related 
communication costs. D design seems to have ignored all the nice 
ideas present in Chapel and X10 languages).


Bye,
bearophile


Re: Purity in D – new article

2012-05-28 Thread Gour
On Mon, 28 May 2012 19:03:48 +0200
"bearophile"  wrote:

> But on a single CPU their performance is not so good compared to
> mutable flat array-based data structures.

How many are on a single CPU today and in the nearby tomorrow?


Sincerely,
Gour


-- 
In this endeavor there is no loss or diminution, 
and a little advancement on this path can protect 
one from the most dangerous type of fear.

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810


signature.asc
Description: PGP signature


Re: Purity in D – new article

2012-05-28 Thread bearophile

David Nadlinger:


http://www.reddit.com/r/programming/comments/u84fc/purity_in_d/


On Reddit "yogthos" has said:
http://www.reddit.com/r/programming/comments/u84fc/purity_in_d/c4t8czg

they're called persistent data structures, and wikipedia has a 
nice article showing how they work. These are implemented in 
languages like Haskell and Clojure, and offer performance that's 
within that of using mutable data.<


Those data structures are interesting, they are persistent so 
they remove certain classes of bugs and allow for certain 
high-level algorithms, they play well with multiple CPUs cores, 
so probably they will be handy in D too. But on a single CPU 
their performance is not so good compared to mutable flat 
array-based data structures. Idiomatic Clojure code is often 
slow, and it uses tons of RAM, several times the memory used by 
equivalent Python code. So the situation isn't as good as they 
keep saying.


Bye,
bearophile


Re: Purity in D – new article

2012-05-28 Thread David Nadlinger

On Sunday, 27 May 2012 at 21:43:44 UTC, Jakob Ovrum wrote:
I also feel this could fill a big void in the list of articles 
on dlang.org.


If it is deemed suitable, I'll gladly submit a pull request.

David


Re: Purity in D – new article

2012-05-28 Thread David Nadlinger

On Sunday, 27 May 2012 at 20:56:22 UTC, David Nadlinger wrote:

http://klickverbot.at/blog/2012/05/purity-in-d/


The article is now on Reddit and Hacker News as well:

http://www.reddit.com/r/programming/comments/u84fc/purity_in_d/
http://news.ycombinator.com/item?id=4032248

David


Re: Purity in D – new article

2012-05-27 Thread Walter Bright

On 5/27/2012 1:56 PM, David Nadlinger wrote:

Some of you might remember that I have been meaning to write a comprehensive
introduction to design and use of purity for quite some while now – I finally
got around to do so:

http://klickverbot.at/blog/2012/05/purity-in-d/

Feedback and criticism of all kinds very welcome!

David


Reddit:

http://www.reddit.com/r/programming/comments/u84fc/purity_in_d/


Re: Purity in D – new article

2012-05-27 Thread Jakob Ovrum

On Sunday, 27 May 2012 at 20:56:22 UTC, David Nadlinger wrote:
Some of you might remember that I have been meaning to write a 
comprehensive introduction to design and use of purity for 
quite some while now – I finally got around to do so:


http://klickverbot.at/blog/2012/05/purity-in-d/

Feedback and criticism of all kinds very welcome!

David


Thank you for your hard work on this, this will be extremely 
useful next time someone asks about D's take on functional purity.


I also feel this could fill a big void in the list of articles on 
dlang.org.


Purity in D – new article

2012-05-27 Thread David Nadlinger
Some of you might remember that I have been meaning to write a 
comprehensive introduction to design and use of purity for quite 
some while now – I finally got around to do so:


http://klickverbot.at/blog/2012/05/purity-in-d/

Feedback and criticism of all kinds very welcome!

David