[racket] Delimited continuations and parameters
Also, just in case it wasn't obvious all along, here's another one:
#lang racket
(require racket/control)
(define p (make-parameter 0))
(define r (make-parameter 0))
((λ (f)
(parameterize ([p 2])
(values
(parameterize ([r 20])
(f 0)))))
(parameterize ([p 1])
(reset
(parameterize ([r 10])
((λ (x) (+ (p) (r)))
(shift f f))))))
On Mon, May 14, 2012 at 9:21 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> At Mon, 14 May 2012 21:50:01 -0400, Asumu Takikawa wrote:
>> On 2012-05-14 19:00:49 -0600, Matthew Flatt wrote:
>> > In other words, every `parameterize' uses the same continuation mark,
>> > so that `(parameterize ([r 10]) ...)' associates the parameterization
>> > continuation mark with a single record that maps `r' to 10, `p' to 1,
>> > etc. The entire record is carried by the delimited continuation.
>>
>> Thanks Matthew and Ryan, that clears it up. OTOH, it does look like a
>> straightforward translation to continuation marks (or using
>> unstable/markparam as Ryan pointed out) doesn't get the 12 result
>> either.
>>
>> For example:
>>
>> #lang racket
>>
>> (require racket/control
>> unstable/markparam)
>>
>> (define p (mark-parameter))
>> (define r (mark-parameter))
>>
>> ((λ (f)
>> (mark-parameterize ([p 2])
>> (mark-parameterize ([r 20])
>> (f 0))))
>> (mark-parameterize ([p 1])
>> (reset
>> (mark-parameterize ([r 10])
>> ((λ (x) (+ (mark-parameter-first p)
>> (mark-parameter-first r)))
>> (shift f f))))))
>>
>> Will produce an error
>> +: expects type <number> as 1st argument, given: #f; other arguments were: 10
>>
>> > In the ICFP'07 paper on delimited continuations in Racket, we wrote (at
>> > the end of section 5) that we'd probably change `parameterize', but
>> > we've never gotten around to that change. Meanwhile, raw continuation
>> > marks (as modeled directly in that paper) essentially match the
>> > dynamic-binding form of Kiselyov et al.
>>
>> The raw continuation mark version gives essentially the same error as
>> above, maybe because call/cc and call/comp restore the marks to what was
>> present at capture time, which doesn't include the `p` mark?
>
> It's because `unstable/markparam' lookup is delimited by the default
> continuation prompt --- and I suppose that's a twist relative to
> Kiselyov et al. and another facet of `control'+`prompt' versus
> `shift'+`reset'. Below are two variants that produce 12.
>
> ----------------------------------------
>
> #lang racket
>
> (require racket/control)
>
> (define (get-mark key)
> (continuation-mark-set-first #f
> key
> #f
> mark-prompt))
>
> (define mark-prompt (make-continuation-prompt-tag))
>
> (call-with-continuation-prompt
> (lambda ()
> ((λ (f)
> (with-continuation-mark 'p 2
> (with-continuation-mark 'r 20
> (f 0))))
> (with-continuation-mark 'p 1
> (reset
> (with-continuation-mark 'r 10
> ((λ (x) (+ (get-mark 'p)
> (get-mark 'r)))
> (shift f f)))))))
> mark-prompt)
>
> ----------------------------------------
>
> #lang racket
>
> (require racket/control)
>
> (define (get-mark key)
> (continuation-mark-set-first #f
> key))
>
> ((λ (f)
> (with-continuation-mark 'p 2
> (with-continuation-mark 'r 20
> (f 0))))
> (with-continuation-mark 'p 1
> (prompt
> (with-continuation-mark 'r 10
> ((λ (x) (+ (get-mark 'p)
> (get-mark 'r)))
> (control f f))))))
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users