Abort evaluating Haskell expression if memory limit reached : <p>I'm using QuickCheck to test automatically-generated properties (similar to <a href="https://hackage.haskell.org/package/quickspec" rel="noreferrer">QuickSpec</a>) but one common problem I'm running into is exhausting the memory, either due to naive recursive generators or very large function outputs (e.g. one failure was caused by an exponential function for Peano numerals, which generated huge nested structures).</p>

<p>I'm wondering if there's a way to abandon evaluation if a (resident) memory limit is reached. It seems <a href="https://stackoverflow.com/questions/20752083/haskell-time-limit-on-evaluation">we can do this for timeouts</a>, but memory seems to be trickier. This way, if we use too much memory, that test can be discarded (as if a <code>==&gt;</code> precondition had failed).</p>

<p>I can see how to measure the whole program's memory usage, by looking at the source of the <code>weigh</code> package. This would be workable, but it would be much nicer (and robust) to measure it for one particular expression (perhaps by getting the memory used by one thread, or something?).</p>

<p>For my purposes it would be enough to fully-normalise the expression, since I don't need to worry about recursive structures (I can apply this to the test results, which are effectively booleans).</p>
