Motivation
The main idea is to explore and understand the limits of how far one can go with the basic LINQ primitives (Select, SelectMany, Concat, etc.). These primitives can all be considered functional operations on a theoretical sequence type. Taking examples from Haskell:
Select
'lifts' a function into the sequence (likefmap
in Haskell)Concat
is compositionAggregate
is the sequence type's catamorphism (fold)SelectMany
'extracts' information from the sequence (the Monad bind,>>=
operation)- etc. (and I'm sure there are better abstractions for the above)
So the question is whether or not the basic sequence (Enumerable) operations in C# are enough to construct an infinite sequence. More concretely it is the following problem:
Problem
I'm curious to know if there's a way to implement something equivalent to the following, but without using yield
:
IEnumerable<T> Infinite<T>()
{
while (true) { yield return default(T); }
}
Is it possible to do sue using the built-in LINQ operators only?
The short answer is that theoretically yes, but practically not because of how Linq is implemented (causing stack overflows).
That's why here are less restrictive rules:
Rules
Alternatively a less restrictive question would go by the rules
- You can't use the
yield
keyword directly - Use only C# itself directly - no IL code, no constructing dynamic assemblies etc.
- You can only use the basic .NET lib (only
mscorlib.dll
,System.Core.dll
? not sure what else to include). However if you find a solution with some of the other .NET assemblies (WPF?!), I'm also interested. - Don't implement IEnumerable or IEnumerator.
Notes
An example theoretically correct definition is:
IEnumerable<int> infinite = null;
infinite = new int[1].SelectMany(x => new int[1].Concat(infinite));
This is "correct" but hits a StackOverflowException after 14399 iterations through the enumerable (not quite infinite).
I'm thinking there might be no way to do this due to the C#'s compiler lack of tail recursion optimization. A proof would be nice :)
4 Answers 4
Even if your assertion was true, proving it would be infeasible, because the proof would have to go through all implementations of IEnumerable
in the framework and prove for each one of those that it can't be infinite.
And your assertion actually isn't true, there is at least one implementation of IEnumerable
in the framework that can be infinite: BlockingCollection.GetConsumingEnumerable()
:
What you would do is to create a bounded BlockingCollection
that's filled in an infinite loop from a separate thread. Calling GetConsumingEnumerable()
will then return an infinite IEnumerable
:
var source = new BlockingCollection<int>(boundedCapacity: 1);
Task.Run(() => { while (true) source.Add(1); });
return source.GetConsumingEnumerable();
-
thanks - that does it. "I think there might not be" was more of a guess than an assertion, and you proved it wrong.sinelaw– sinelaw2013年11月07日 14:53:49 +00:00Commented Nov 7, 2013 at 14:53
-
By the way, I still think that using the basic LINQ primitives it isn't possible exactly because the compiler limitation on tail call optimizations - any composition of the basic linq primitives that would result in an infinite sequence would hit a stack overflowsinelaw– sinelaw2013年11月07日 16:33:23 +00:00Commented Nov 7, 2013 at 16:33
-
@sinelaw I still don't see how the tail call optimization would help with that, it's not something that magically fixes all stack overflows.svick– svick2013年11月07日 17:02:33 +00:00Commented Nov 7, 2013 at 17:02
-
Won't this answer run out of memory fairly quickly? BlockingCollection is new to me, but as best I can tell it's a collection: msdn.microsoft.com/en-us/library/dd267312.aspxta.speot.is– ta.speot.is2013年11月07日 22:19:39 +00:00Commented Nov 7, 2013 at 22:19
-
1Then this is an elegant solution.ta.speot.is– ta.speot.is2013年11月07日 23:27:21 +00:00Commented Nov 7, 2013 at 23:27
Sure. IEnumerable is basically just a call to IEnumerator. Implement an IEnumerator where the MoveNext function just sets an internal value to a random value, and Current returns that random value.
Following, and similar, functions have logarithmic stack growth, and for any practical purposes can be thought as not causing stack overflows:
public static partial class Extensions
{
public static IEnumerable<T> Infinite<T>(this IEnumerable<T> source) =>
source.Concat(new[] { source.Concat(source) }.SelectMany(Infinite));
}
Here is a practically infinite iterator:
using System;
using System.Linq;
public class Test
{
public static void Main()
{
var infiniteIterator =
Enumerable.Range(Int32.MinValue, Int32.MaxValue)
.SelectMany(i => Enumerable.Range(Int32.MinValue, Int32.MaxValue))
.SelectMany(i => Enumerable.Range(Int32.MinValue, Int32.MaxValue))
.SelectMany(i => Enumerable.Range(Int32.MinValue, Int32.MaxValue))
.Select(i => default(int));
foreach (var infinite in infiniteIterator)
Console.WriteLine(infinite);
}
}
-
1For all practical purposes, it is. You could go even further by composing
SelectMany
in a loop. But it will never be infinite.sinelaw– sinelaw2013年11月07日 04:15:26 +00:00Commented Nov 7, 2013 at 4:15 -
I'm beginning to think the answer is "no, it isn't possible due to lack of tail recursion optimization and the way the compiler handles IEnumerable"sinelaw– sinelaw2013年11月07日 04:31:43 +00:00Commented Nov 7, 2013 at 4:31
-
Enumerable.Range is implemented using yield.Den– Den2013年11月07日 16:52:11 +00:00Commented Nov 7, 2013 at 16:52
-
2This sequence has
17179869184
items in it. That is not infinite.Servy– Servy2013年11月07日 18:46:37 +00:00Commented Nov 7, 2013 at 18:46 -
@Den That's not incorrect but it doesn't matter: You can't use the yield keyword directlyta.speot.is– ta.speot.is2013年11月07日 22:15:53 +00:00Commented Nov 7, 2013 at 22:15
SelectMany
, along with almost every Linq query, usesyield
in its implementation. That's what it's there for. If you don't want to use it, you're probably going to have to disregard Linq altogether. Unless your restrictions are "I can't write out theyield
myself, but I can use other things that use it". Which would be a weird set of restrictions.