Shower thought: would an efficient supercompiler make ad-hoc static type systems possible in a dynamically typed language?

You can definitely eliminate lots of different types of runtime costs using partial evaluation and supercompilation. (including parsing, type checking etc.)

Now, the definition of supercompilation is not clear to me. I'd like to think it as a specialized use case of partial evaluation. I'll give an example using partial evaluation but the point definitely applies to supercompilation as well since it also based on evaluating open terms based on known arguments/values.

One amazing example is "Futamura projections", as described in Futamura's 1971 paper. It basically shows that if you partially apply(partial evaluate the application) an interpreter to a program, you basically compile the program, meaning that you eliminate the some(ideally all) of the costs like parsing and type checking. (since those only depend on input program, not input of the input program)

Of course this is an idealized case, in practice you probably couldn't eliminate 100% of the parsing/type checking costs, but could definitely eliminate a lot.

(Futamura goes further, and applies partial evaluators to themselves, you can read his paper(linked above) and my blog post about it: http://osa1.net/posts/2015-01-11-understanding-futamura-projections.html)

But again, this is probably never used in practice. (because of lots of reasons that I won't dive into right now)

Instead I think the only practical tool which does something close to this "idealized" partial evaluator the multi-staged programming language MetaOCaml. In MetaOCaml, you can write an interpreter that does all the parsing and type checking stuff in stage 0, and interpretation(execution) in stage 1. You can then generate a specialized version of your program, compiled with standard OCaml compiler with all the optimizations, that doesn't do parsing or type checking.(this specialization is done on given input program, of course) This is equivalent of compiling your program to native code and you don't need the interpreter anymore.

This is of course, not a partial evaluation. In multi-staged programming you manually guide the program to generate specialized program, so in a sense you do the job of partial evaluator. (or at least guide the language to do the job of a partial evaluation)

/r/haskell Thread