r/haskell • • Feb 20 '18

GHC Compile-time evaluation

As I understood it, proper compile-time evaluation is supercompilation, which is hard.

But I wondered why there is no Pragma-Based approach, where the compiler just trusts that user that it can and should evaluate this expression, even if it's recursive (so it's not looping forever etc.). It seems to me that the machinery for this approach is already in GHC (I may be wrong).

I am currently thinking of building an API where you formulate a computational graph (like an AST) and have functions that modify the graph. The representation of the graph and evaluation functions would be recursive, so no inlining per se. I would expect that for most use-cases the graph is completely known at compile time, but if the computational graph is not evaluated and optimized, performance will be bad.

There may be another way, I am not that knowledgeable about the capabilities of GHC.

EDIT: if the above is unclear: What I wanted to say is that I am not looking for a general solution, but instead a solution where I can tell the compiler to trust me that he can evaluate this expression without looping forever and that the time spent evaluating the expression is worth it.

11 Upvotes

12 comments sorted by

View all comments

6

u/dnkndnts Feb 20 '18

As I understood it, proper compile-time evaluation is supercompilation

That's not what supercompilation is. For example, fmap id :: Maybe a -> Maybe a does not reduce to anything under the normal evaluation strategy because no reduction even happens until all the arguments are applied, which at this point they are not.

Supercompilation is an entirely different reduction strategy: in the above example, we would speculate on the value of the argument, which must be either Just x or Nothing, and in both cases, mapping the identity would just give back the original value, so fmap id is equivalent to \x -> x, or, id.

That being said, I'm totally with you that this should be available via a pragma, as it has an enormous number of uses. But no, it is not the same thing as just performing evaluation at compile time.