racket/control: add aliases and update %/fcontrol

Added alises for call-with-continuation-prompt,
abort-current-continuation, and call-with-composable-continuation.
Also allow % and fcontrol to take an optional prompt tag argument.
This commit is contained in:
Asumu Takikawa 2012-06-13 16:32:06 -04:00
parent a7064d5f6a
commit 5d232f3748
3 changed files with 64 additions and 9 deletions

View File

@ -1,6 +1,10 @@
(module control mzscheme
(module control racket/base
(provide abort
(require (for-syntax racket/base))
(provide call/prompt call/comp abort/cc
abort
fcontrol %
@ -20,6 +24,12 @@
;; ----------------------------------------
(define call/prompt call-with-continuation-prompt)
(define call/comp call-with-composable-continuation)
(define abort/cc abort-current-continuation)
;; ----------------------------------------
(define (abort . vals)
(abort-current-continuation
(default-continuation-prompt-tag)
@ -30,16 +40,21 @@
;; The `%' here is compable with Sitaram & Felleisen, LSC'90,
;; since we make the handler optional.
(define (fcontrol f)
(define (fcontrol f #:tag [prompt-tag (default-continuation-prompt-tag)])
(call-with-composable-continuation
(lambda (k)
(abort-current-continuation
(default-continuation-prompt-tag)
f
prompt-tag
f
k))))
(define-syntax %
(syntax-rules ()
[(_ expr handler #:tag prompt-tag)
(call-with-continuation-prompt
(lambda () expr)
prompt-tag
handler)]
[(_ expr handler)
(call-with-continuation-prompt
(lambda () expr)

View File

@ -1,7 +1,7 @@
#lang scribble/doc
@(require (except-in "mz.rkt" set) (for-label racket/control))
@title{Classical Control Operators}
@title{Additional Control Operators}
@note-lib-only[racket/control]
@ -20,6 +20,31 @@ work sensibly together. Many are redundant; for example,
@; ----------------------------------------------------------------------
@defproc[(call/prompt
[proc procedure?]
[prompt-tag continuation-prompt-tag? (default-continuation-prompt-tag)]
[handler (or/c procedure? #f) #f]
[arg any/c] ...)
any]{
The @racket[call/prompt] binding is an alias for @racket[call-with-continuation-prompt].
}
@defproc[(abort/cc
[prompt-tag any/c]
[v any/c] ...+)
any]{
The @racket[abort/cc] binding is an alias for @racket[abort-current-continuation].
}
@defproc[(call/comp
[proc (continuation? . -> . any)]
[prompt-tag continuation-prompt-tag? (default-continuation-prompt-tag)])
any]{
The @racket[call/comp] binding is an alias for @racket[call-with-composable-continuation].
}
@; ----------------------------------------------------------------------
@defproc[(abort [v any/c] ...) any]{
Returns the @racket[v]s to a prompt using the default continuation
@ -43,8 +68,12 @@ That is, @racket[(abort v ...)] is equivalent to
@deftogether[(
@defform*[[(% expr)
(% expr handler-expr)]]
@defproc[(fcontrol [v any/c]) any]
(% expr handler-expr)
(% expr handler-expr #:tag tag-expr)]]
@defproc[(fcontrol
[v any/c]
[#:tag prompt-tag (default-continuation-prompt-tag)])
any]
)]{
@ -59,7 +88,8 @@ The essential reduction rules are:
]
When @racket[handler-expr] is omitted, @racket[%] is the same as
@racket[prompt].
@racket[prompt]. If @racket[prompt-tag] is provided, @racket[%]
uses specific prompt tags like @racket[prompt-at].
@examples[#:eval control-eval
(% (+ 2 (fcontrol 5))

View File

@ -178,6 +178,16 @@
(ctest (all-prefixes '(1 2 3 4))
'((1) (1 2) (1 2 3) (1 2 3 4)))
;; ----------------------------------------
;; fcontrol/% with prompt tags
(ctest (let ([pt (make-continuation-prompt-tag)])
(* 2 (% (% (fcontrol 5 #:tag pt)
(lambda (v k) (k v)))
(lambda (v k) (k (add1 v)))
#:tag pt)))
12)
;; ------------------------------------------------------------
;; spawn
;; example from Queinnec & Serpete, POPL'91