Consider the following code:
(call-with-values
(lambda ()
(call/cc (lambda (k)
(k k k))))
(lambda (x y)
(procedure-arity y)))
It's pretty obvious here that the continuation at the point of the call/cc
call is the lambda on the right-hand side, so its arity should be 2. However, the return value of the above (in Racket) is (arity-at-least 0)
instead.
Indeed, running similar code in Guile (substituting procedure-minimum-arity
for procedure-arity
) shows that the continuation also supposedly allows any number of arguments, even though it's pretty clearly not the case.
So, why is that? As far as I understand (correct me if my understanding is wrong), the arity of a continuation is pretty straightforward: it's 1 except in the context of call-with-values
, in which case it's whatever the arity of the right-hand-side lambda is. (Which, granted, can be complicated if it's a case-lambda
or the like, but no more complicated than if you were calling (procedure-arity (case-lambda ...))
directly.)
values
in the spec (R5/6) sums this up nicely. – leppievalues
implies that the continuation has arbitrary arity. You're not allowed to return a different number of values from what the continuation supports. :-) Thus,(+ (values 1 2 3) 42)
is not valid: in this context (assumingvalues
as defined in R5RS), the continuation so created has arity 1, nothing else. – Chris Jester-Youngcall-with-values
, the consumer determines the arity, not the producer. – leppie(call-with-values some-proc list)
? There is no waysome-proc
can determine the expected number of values at the time of its definition. Does that make sense? The compiler however is free to optimize the code as in your post to something more specific (as it can be determined). It is not required to share this info however ;p – leppie