In Haskell, lazy evaluation can often be used to perform efficient calculations of expressions that are written in a clear and concise manner. However, it seems that the language itself does not provide enough details to determine, in general, the time and space needs of a given piece of code. The situation seems to be mitigated, to some degree, by common use of ghc, which I gather gives some more specific guarantees related to weak-head-normal-form. But if I’m not mistaken, the actual performance of code can still be quite difficult to understand.
For example, we also use polymorphism to express functions in a generic fashion, again without sacrificing clarity. However, when combined with lazily-evaluated structures, the two language features seem to interact in ways that are (to me) surprising. Consider:
import Debug.Trace (trace) tracePlus a b = trace (show a ++ "+" ++ show b) (a+b) -- This lets us try small integers to see how things get evaluated. -- Those tests can thereby reveal the asymptotic behavior of the code, without needing to actually try bigger values. class Sum a where one :: a add :: a -> a -> a instance Sum Integer where one = 1 add = tracePlus fibSums_list :: (Sum a) => [a] fibSums_list = one : one : zipWith add fibSums_list (tail fibSums_list) fibS :: Int -> Integer fibS = (fibSums_list !!)
I should note that this works fine if I compile it with
ghc -O2. However, when run under
ghci, it takes exponential time complexity to evaluate
fibS. Yet, using a list of Fibonacci numbers of type
[Integer] works fine as well.
So, one specific question I have is: is there a way to rewrite
fibS, such that it retains the use of the
Sum type class, and is still clearly a generalization of the Fibonacci sequence, but that also evaluates efficiently in ghci? Where do I even start?
And I wonder if similar pitfalls await even in code compiled via
ghc -O2. And if so, how do authors of Haskell code deal with those?
Another related question is When is it a good time to reason about performance in Haskell?. I think my question is an even more fundamental one; I don’t even understand how to go about the task of such reasoning. There is a reasonable answer there, but it doesn’t have enough specific information for me to actually go about writing a
fibSums_list that works in
ghci, let alone one that has any sort of guaranteed time complexity.
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!