I think your question contains at least 2 distinct questions, but whether (current versions of) GHC can optimize this
subsequencesOfSize l n = [x | x <- subsequences l, length x == n]
into something like
subsequencesOfSize l n = [x | x <- subsequences l, lengthIsGt n x, length x == n]
is highly doubtable — I'm nowhere near being an expert on optimization, supercompilation etc, but inferring the need for something like lengthIsGt
from the use of length
here seems highly non-trivial and is unlikely to be part of a compiler's set of generic optimizations.
P.S. actually the version with lengthIsGt
will potentially still try to evaluate infinite lists if the condition containing length
is evaluated before the condition containing lengthIsGt
(but that was pseudocode anyway)