Thread overview
[Issue 12170] New: sum(double[]) is not pure
Feb 15, 2014
Peter Alexander
[Issue 12170] sum(double[]) is not pure nothrow
February 15, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12170

           Summary: sum(double[]) is not pure
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2014-02-15 01:48:01 PST ---
import std.algorithm;
void main() pure {
    [1.0].sum;
}


dmd 2.065beta3 gives:

test.d(3,10): Error: pure function 'D main' cannot call impure function
'std.algorithm.sum!(double[]).sum'

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 15, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12170


Peter Alexander <peter.alexander.au@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |peter.alexander.au@gmail.co
                   |                            |m
         OS/Version|Windows                     |All


--- Comment #1 from Peter Alexander <peter.alexander.au@gmail.com> 2014-02-15 06:09:57 PST ---
The problem is this:

"Cyclic functions (i.e. functions that wind up directly or indirectly calling themselves) are inferred as being impure, throwing, and @system."

http://dlang.org/function.html

sum uses a recursive algorithm for summing floating point ranges for extra accuracy, so its purity is not inferred. It cannot just be marked as pure because the range operations it uses may not be pure.

I'm not sure why that cyclic function restriction exists. I think it should be possible to infer these attributes on recursive functions. I'll have a think.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 15, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12170


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|sum(double[]) is not pure   |sum(double[]) is not pure
                   |                            |nothrow


--- Comment #2 from bearophile_hugs@eml.cc 2014-02-15 06:43:26 PST ---
Yes, the same happens with nothrow:


void main() nothrow {
    import std.algorithm: sum;
    [1.0].sum;
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 15, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12170



--- Comment #3 from bearophile_hugs@eml.cc 2014-02-15 06:49:39 PST ---
(In reply to comment #1)
> The problem is this:
> 
> "Cyclic functions (i.e. functions that wind up directly or indirectly calling themselves) are inferred as being impure, throwing, and @system."

Until that compiler limitation is lifted, a solution is to convert the
recursive algorithm of sum() in an iterative one. Because sum() is something
you often want to use in pure nothrow functions.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 27, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12170


monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra@gmail.com


--- Comment #4 from monarchdodra@gmail.com 2014-02-27 04:05:25 PST ---
(In reply to comment #3)
> Until that compiler limitation is lifted, a solution is to convert the
> recursive algorithm of sum() in an iterative one. Because sum() is something
> you often want to use in pure nothrow functions.

Well, "sum" is specifically implemented to do pair-wise sumation of floats, to reduce computational error, as opposed to the "dumber" "member by member" sum.

The idea would be to transform the recursive algorithm into an iterative one, but (AFAIK), this usually requires a stack-type structure.

Either that, or request better inference for cyclic functions.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------