Higher order functions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Higher order functions

drbolle
I want to start a little pet project that creates a new Scheme-like language called "Hackeme" to be run on the Hack platform. I actually would like my compiler to produce Hack VM code but I have some doubts.
Being a Scheme-like language Hackeme should support functions as arguments. However I don't see a way to support this in the Hack VM as it would require dynamic function calls. So do I have to extend the standard VM or did I miss something which would allow me to use the Hack VM as is?
Reply | Threaded
Open this post in threaded view
|

Re: Higher order functions

ivant
Interesting question with a lot of things to consider. First of all, it looks like you are trying to make a compiler and not an interpreter, right? I think having an eval function in a compiled-only Scheme for the HACK platform is not possible (it is possible. (It is possible in the general case, but all HACK code resides in ROM, which I think is the main problem.)

If we don't have eval, then it is technically possible to implement the dispatch, but it's totally impractical. Basically you compile every lambda to a different function or method, as you know them at compile-time. You also create a dispatch function, which would take as argument a number + all the parameters, and it will call the appropriate lambda implementation. Something like that:

function dispatch(int lambda, Environment env, Array parameters) {
    if (lambda == 0) {
      return lambda_0(env, parameters);
    } else if (lambda == 1) {
      return lambda_1(env, parameters);
    }
    ...
}

And all the calls go through this dispatch. As I said, very impractical.

Another problem with the Hack VM is that it does not support tail-call optimization (TCO). Actually that's not completely true. There's nothing stopping you from implementing the VM with TCO, but most implementations don't have it. It's not trivial and for "normal" code it doesn't give you much advantages (again in HACK).

I think you should design your own VM, better suited for Scheme compilation.
Reply | Threaded
Open this post in threaded view
|

Re: Higher order functions

drbolle
Thanks for the info! So then I will try to develop a suitable VM.