5
votes

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.)

1
IMO, it should be any number of arguments, as when the explicit continuation is created with call/cc, the context is unknown until the continuation is actually called. I think the definition of values in the spec (R5/6) sums this up nicely.leppie
I'm not quite sure how values 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 (assuming values as defined in R5RS), the continuation so created has arity 1, nothing else.Chris Jester-Young
Also with call-with-values, the consumer determines the arity, not the producer.leppie
Of course. So my question is, why doesn't the continuation have the arity of the consumer? :-)Chris Jester-Young
How would it work if you have: (call-with-values some-proc list)? There is no way some-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 ;pleppie

1 Answers

2
votes

A simpler way to see the same is:

(call-with-values
  (lambda () (error 'arity "~v" (procedure-arity (call/cc (λ (k) k)))))
  (lambda (x y) (procedure-arity y)))

and even simpler:

(procedure-arity (call/cc (λ (x) x)))

And for your question -- it's clear in the first case that the continuation expects two inputs, but cases like that are not too common. Eg, they're usually such examples, whereas "real code" would use define-values or have some unknown continuation, where the continuations that call/cc creates can have different arities depending on the context that they were created at. This means that there's not much point in trying to figure out these rare cases where the continuation's arity is known.

Footnote:

;; nonsensical, but shows the point
(define (foo) (call/cc (λ (x) x)))
(define x (foo))
(define-values [y z] (foo))