isInfinite isInadequate

Peter Alexander peter.alexander.au at gmail.com
Wed Oct 17 05:18:30 PDT 2012


So, std.range.isInfinite checks if r.empty is a compile time 
boolean equal to false. This works most of the time, but not 
always. Some ranges are infinite, but cannot be determined to be 
so at compile time (or even run time!)

cycle([1, 2, 3]).until(4);    // infinite, but not isInfinite
cycle(new int[0]);            // not infinite, but isInfinite
collatzSequence(n).until(1);  // infiniteness an open problem!

In short, isInfinite does not -- and cannot -- work as a way of 
telling if a range is finite or infinite in general.

On resolution is to take isInfinite at face value: it only tells 
you if a range is statically determined to be infinite. If 
isInfinite is false, it could still be infinite.

This leaves us with the tricky cases like cycle(new int[0]). 
There's three resolutions to this (as far as I can tell):

1. Change cycle to not be an infinite range.
2. Make cycle assert when the source range is empty.
3. Ignore this issue.

Option 1 is sound, but kind of sucks, because generally cycle is 
infinite.

Option 2 is sound, but I don't like the idea of asserting on 
logically valid input just because the problem is too hard.

Option 3 is not sound, but may be practical. Perhaps these 
edge-case, fake, infinite sequences are not worth worrying about 
-- just let it slide and make other people worry about the 
consequences.

This Phobos pull request is relevant: 
https://github.com/D-Programming-Language/phobos/pull/871

Thoughts?


More information about the Digitalmars-d mailing list