[plt-scheme] inferred names of procedures must be symbols
I finally fixed something that had been bothering me for a while in my
code, and I thought I'd post it here in case anyone else has had a
similar problem. I don't know if this should be regarded as a bug in
MzScheme or a documentation bug, so I'm not going to submit a bug
report for now.
You can set the inferred name of a procedure expression to a symbol,
so that when you print the procedure you get a name instead of a
syntax location. This is useful in syntax transformers that create
procedure expressions:
(define-values (struct:ap make-annotated-proc annotated-proc? ap-ref ap-set!)
(make-struct-type 'annotated-proc #f 2 0 #f null #f 0))
(define (proc-annotation p) (ap-ref p 1))
(define-syntax (annotated-lambda stx)
(syntax-case stx ()
((_ formals annotation body1 body2 ...)
#`(make-annotated-proc
#,(syntax-property #'(lambda formals body1 body2 ...)
'inferred-name (syntax-local-name))
annotation))))
The bug here is that `syntax-local-name' returns a syntax object, not
a symbol, and if you set the inferred name of a procedure to anything
but a symbol, it silently ignores it (and constructs a name from the
syntax location instead):
> (define plus1 (annotated-lambda (x) "adds 1 to its argument" (+ x 1)))
> plus1
#<procedure:...nnotated-proc.ss:13:11>
The fix was to replace `(syntax-local-name)' with
`(syntax-e (syntax-local-name))':
> plus1
#<procedure:plus1>
I think the documentation ought to point out that the inferred name of
a procedure expression must be a symbol, or else it should just be
smart about converting an identifier to a symbol too.
--dougo at place.org