justinethier / cyclone

:cyclone: A brand-new compiler that allows practical application development using R7RS Scheme. We provide modern features and a stable system capable of generating fast native binaries.
http://justinethier.github.io/cyclone/
MIT License
823 stars 42 forks source link

Continuations at Top Level #463

Closed ricejasonf closed 3 years ago

ricejasonf commented 3 years ago

I noticed a discrepancy between Cyclone and other Schemes with the way they handle continuations at the top level of the program. It appears that Cyclone (via the wasm interpreter) will allow the escape procedure to resume execution at the continuation at the top level.

In contrast ChibiScheme seems to delimit the continuations execution at the end of the current top level expression. Racket behaves this way as well.

Which is correct for R7RS? I can't seem to find a definitive answer or any information in the revised standard.

(define goto-start 0)
(define i 0)
(call/cc (lambda (k) (set! goto-start k)))
; goto-start
(display (string-append "foo" (number->string i)))
(set! i (+ i 1)) 
(if (< i 10) 
    (goto-start 0)
    (display "done"))

Cyclone output:

ok
ok
ok
foo0
ok
0
foo1
ok
0
foo2
ok
0
foo3
ok
0
foo4
ok
0
foo5
ok
0
foo6
ok
0
foo7
ok
0
foo8
ok
0
foo9
ok
done

Chibi output:

foo0
justinethier commented 3 years ago

This is interesting but I do not necessarily see a problem. Many Scheme's do handle the top-level environment like Chibi but not all of them (EG: chicken csc).

For the purposes of writing portable code I would not rely on behavior of the top-level continuation.

ricejasonf commented 3 years ago

For the purposes of writing portable code I would not rely on behavior of the top-level continuation.

I'll go with that, and I thank you for your response.

justinethier commented 3 years ago

No problem, thanks for the report :)