3 ms·
> like four registers available for passing arguments and one available for returning a result, when the calling convention really should be symmetric The symm
by kazinator 3y ago
> like four registers available for passing arguments and one available for returning a result, when the calling convention really should be symmetric
The symmetry implies the support for multiple return values.
If the language model has single return values, then continuations take one parameter. Lots of historic papers about continuations model them that way.
Multiple values are tricky. 100% symmetry is never achieved with those things. The problem is that in many contexts, an expression is expected to produce one value. We usually want (foo (bar) (baz)) to call foo with two arguments even if bar and baz return two or more values. There may be times when we want to inerpolate all the values, or some of them, into the argument space, so we need some syntax to distinguish those situations. But if (foo (bar) (baz)) just takes one value from each function, then that means that the primary value is more of a first class citizen than the additional values. There is something special about it.
We can also go the other way: declare that functions should not only return exactly one value, but only take exactly one argument. That is also symmetric! Then currying can be used to combine functions in order to simulate multiple arguments.
- JonChesterfield 3y agoThis is tricky in the comment format but here goes. Let us declare that a function is passed exactly one value, and that it passes exactly one value to the chosen continuation. However that does not imply that it takes exactly one named argument. (lambda a ...) Binds that one value to a. If you pass it a list, that's a variadic function. (lambda (a b) ...) Requires the argument be a list of two values and binds the elements to a, b. (lambda (a (b c) d) ...) Likewise, except it's now a list of three things where element 1 must be a list of two things. The corresponding return/continuation part then looks like (let a (foo) (b c) (bar) ...) where the let binds whatever foo returned to a, binds a list of length 2 from bar and so forth. This works really nicely with static typing in the absence of function currying. Destructuring bind on function arguments is somewhat common. In lisp, you have to work out what let returns when the binding doesn't typecheck at runtime and that's a bit of a mess. In SML the construct typechecks at compile time but I can't work out how to reconcile it with currying. Parameter trees, the (a (b) c) idea, I first saw in kernel. I don't remember if it went as far as let binding / destructuring on the continuation invocation. I like the destructuring syntax more than currying so haven't put as much thought into providing both as the latter might deserve.
- cryptonector 3y ago> The symmetry implies the support for multiple return values. Yes! > Multiple values are tricky. 100% symmetry is never achieved with those things. The problem is that [...] You basically need destructuring on the return values, and the syntax for that has to be fairly clean. And for the example you give where one function's return value(s) is(are) passed to another's things do get tricky: shall that use only the first value returned? or maybe the whole lot of them (as an array/list)? or both, where if the callee doesn't treat the thing as an array/list then it's the first value (implying dynamic typing)? or does what happens depend on the callee's parameter's type? or what? Yeah, it's very tricky. Another option is to have generators, and if a function returns/yields multiple values in one go, maybe treat them as single values yielded separately. This one isn't that awesome either. > We can also go the other way: declare that functions should not only return exactly one value, but only take exactly one argument. That is also symmetric! Then currying can be used to combine functions in order to simulate multiple arguments. Yes :) But then in Haskell, which does this, we end up with syntactic sugar with which to pretend there's multiple arguments, thus the 100% symmetry isn't quite. Oh well.