
Daniel Fischer
Am Mittwoch, 18. März 2009 16:14 schrieb Will Ness:
Daniel Fischer
writes: Am Mittwoch, 18. März 2009 12:19 schrieb Will Ness:
Bas van Dijk
writes: On Sun, Mar 15, 2009 at 7:35 PM, Will Ness
wrote:
myCycle xs = ys where ys = foldr (:) ys xs
AFAIK,
[let and where versions of myCycle] are compiled to exactly the same code.
since there are no guards there with shared variables, I guess.
No, that doesn't matter. GHC-core has no where, only let, so all where
clauses
must be rewritten to use let.
So it must be a more extensive re-write then, for guards, with explicit (case test of True -> ) etc... :)
Also, one can imagine this rewrite to be arrived at automagically by a compiler:
sum $ take m $ cycle [1..k]
| n > 0 = x*n+y
where (n,r) = quotRem m k x = sum [1..k] y = sum [1..r]
Any human is certainly capable of seen this almost immediately, presented with the big k's and huge m's. It's automagical. :)
But it's too much of a special case to have a special rule for it in the compiler code.
What I was driving at, is having a compiler so smart it'd figure this out on its own, if needed (i.e. faced with huge m's), not having this specific special case programmed by a compiler-writer.
Humans are much less principled and can thus spot a greater variety of patterns (but they are also better in overlooking such patterns).
I think it is because we're constantly trying out, unconsciously, various ways to achieve our goals. It's like a forward-chaining churning in the background, providing us with currently-known-possibilities sphere, like a cloud of possibilities around us. Touch the goal with the sphere's surface and - boom! - we've got ourselves an intuitive solution that's "just there". Sometimes I think precompilation - pre-evaluation - is the essence of human creativity. We just invent things in advance, in case we'd ever need them. Same with dreams. It's just a training engine for us to learn how to run away from a tiger better, or how to better kill a mammoth. :) I think it's called speculative eager evaluation. Cheers,