How are functions curried?

前端 未结 2 1420
生来不讨喜
生来不讨喜 2021-02-19 04:23

I understand what the concept of currying is, and know how to use it. These are not my questions, rather I am curious as to how this is actually implemented at some lower level

相关标签:
2条回答
  • 2021-02-19 05:06

    Short answer: yes a pointer is maintained to the 2 until the 4 is passed in.


    Longer than necessary answer:

    Conceptually, you're supposed to think about Haskell being defined in terms of the lambda calculus and term rewriting. Lets say you have the following definition:

    f x y = x + y
    

    This definition for f comes out in lambda calculus as something like the following, where I've explicitly put parentheses around the lambda bodies:

    \x -> (\y -> (x + y))
    

    If you're not familiar with the lambda calculus, this basically says "a function of an argument x that returns (a function of an argument y that returns (x + y))". In the lambda calculus, when we apply a function like this to some value, we can replace the application of the function by a copy of the body of the function with the value substituted for the function's parameter.

    So then the expression f 1 2 is evaluated by the following sequence of rewrites:

    (\x -> (\y -> (x + y))) 1 2
    (\y -> (1 + y)) 2                 # substituted 1 for x
    (1 + 2)                           # substituted 2 for y
    3
    

    So you can see here that if we'd only supplied a single argument to f, we would have stopped at \y -> (1 + y). So we've got a whole term that is just a function for adding 1 to something, entirely separate from our original term, which may still be in use somewhere (for other references to f).

    The key point is that if we implement functions like this, every function has only one argument but some return functions (and some return functions which return functions which return ...). Every time we apply a function we create a new term that "hard-codes" the first argument into the body of the function (including the bodies of any functions this one returns). This is how you get currying and closures.

    Now, that's not how Haskell is directly implemented, obviously. Once upon a time, Haskell (or possibly one of its predecessors; I'm not exactly sure on the history) was implemented by Graph reduction. This is a technique for doing something equivalent to the term reduction I described above, that automatically brings along lazy evaluation and a fair amount of data sharing.

    In graph reduction, everything is references to nodes in a graph. I won't go into too much detail, but when the evaluation engine reduces the application of a function to a value, it copies the sub-graph corresponding to the body of the function, with the necessary substitution of the argument value for the function's parameter (but shares references to graph nodes where they are unaffected by the substitution). So essentially, yes partially applying a function creates a new structure in memory that has a reference to the supplied argument (i.e. "a pointer to the 2), and your program can pass around references to that structure (and even share it and apply it multiple times), until more arguments are supplied and it can actually be reduced. However it's not like it's just remembering the function and accumulating arguments until it gets all of them; the evaluation engine actually does some of the work each time it's applied to a new argument. In fact the graph reduction engine can't even tell the difference between an application that returns a function and still needs more arguments, and one that has just got its last argument.

    I can't tell you much more about the current implementation of Haskell. I believe it's a distant mutant descendant of graph reduction, with loads of clever short-cuts and go-faster stripes. But I might be wrong about that; maybe they've found a completely different execution strategy that isn't anything at all like graph reduction anymore. But I'm 90% sure it'll still end up passing around data structures that hold on to references to the partial arguments, and it probably still does something equivalent to factoring in the arguments partially, as it seems pretty essential to how lazy evaluation works. I'm also fairly sure it'll do lots of optimisations and short cuts, so if you straightforwardly call a function of 5 arguments like f 1 2 3 4 5 it won't go through all the hassle of copying the body of f 5 times with successively more "hard-coding".

    0 讨论(0)
  • 2021-02-19 05:06

    Try it out with GHC:

    ghc -C Test.hs
    

    This will generate C code in Test.hc

    I wrote the following function:

    f = (+) 16777217
    

    And GHC generated this:

    R1.p[1] = (W_)Hp-4;
    *R1.p = (W_)&stg_IND_STATIC_info;
    Sp[-2] = (W_)&stg_upd_frame_info;
    Sp[-1] = (W_)Hp-4;
    R1.w = (W_)&integerzmgmp_GHCziInteger_smallInteger_closure;
    Sp[-3] = 0x1000001U;
    Sp=Sp-3;
    JMP_((W_)&stg_ap_n_fast);
    

    The thing to remember is that in Haskell, partially applying is not an unusual case. There's technically no "last argument" to any function. As you can see here, Haskell is jumping to stg_ap_n_fast which will expect an argument to be available in Sp.

    The stg here stands for "Spineless Tagless G-Machine". There is a really good paper on it, by Simon Peyton-Jones. If you're curious about how the Haskell runtime is implemented, go read that first.

    0 讨论(0)
提交回复
热议问题