In the following codes, the change in ordering of generators in for-comprehension is causing an infinite recursion.
The following code gives infinite recursion
lazy val genHeap: Gen[H] = oneOf(const(empty), for {
heap <- genHeap
value <- arbitrary[Int]
} yield insert(value, heap))
while the following code works
lazy val genHeap: Gen[H] = oneOf(const(empty), for {
value <- arbitrary[Int]
heap <- genHeap
} yield insert(value, heap))
The above functions are defined in an abstract class called QuickCheck
.The code is called as follows
object quickcheck extends QuickCheck
quickcheck.genHeap
I think, the reason is that in the first case, genHeap
is equivalent to
oneOf(const(empty),
genHeap flatMap (heap => arbitrary[Int] map (value => insert(value, heap)))
Since oneOf
takes generators by value, the inner genHeap is also executed, which causes the infinite recursion.
In the second case, genHeap
is inside the flatMap, so it need not be executed until the function call.
Am I on the right track with this explanation?