Snap icon indicating copy to clipboard operation
Snap copied to clipboard

Possible bug in call/cc?

Open kach opened this issue 3 years ago • 0 comments

Hi Jens - I was prototyping a small probabilistic programming language in Snap! and came across some funny behavior. I assume I'm misunderstanding something, but what gives me pause is that the Racket translation of this code works just fine!

The idea is that "get a sample" picks a random number in [1, 10], and then only manages to return it if it is > 9. If not, then the "observe" block calls the continuation with… a fresh valid sample (recursively generated). So it's supposed to be a clever way to do rejection sampling.

I think it is the recursion that confuses Snap!, because if the "observe" block simply calls the cc with some constant (e.g. 10) instead of the recursive call to "get a sample," then it works fine.

image

Here is a Racket translation of this that works just fine.

#lang racket/base

(define (observe! cc kond)
  (if (not kond)
    (cc (sample!))
    #t))

(define (sample!)
  (call-with-current-continuation
    (lambda (cc)
      (let ((a (random 10)))
        (observe! cc (> a 5))
        a))))

(displayln
  (map (lambda (x) (sample!))
  '(please give me nine random samples more than five)))

kach avatar Mar 31 '22 14:03 kach