Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

meaning of preceding definition

37 views
Skip to first unread message

saito

unread,
Sep 16, 2012, 8:48:35 AM9/16/12
to
In R6RS, I want to branch by existence of identifier definition.
I wrote a macro such as


#!r6rs
(library (bound helper)
(export make-definition)
(import (rnrs))

(define-syntax make-definition
(lambda(stx2)
(syntax-case stx2 ()
((_ if-bound bound?)
#'(begin
(define-syntax if-bound
(lambda(stx)
(syntax-case stx ()
((_ id consequent)
#'(if-bound id consequent (begin)))
((_ id consequent alternate)
(not
(free-identifier=?
(datum->syntax #'if-bound (syntax->datum #'id))
#'id))
#'consequent)
((_ id consequent alternate)
#'alternate))))
(define-syntax bound?
(syntax-rules ()
((_ id)
(if-bound id #t #f)))))))))
)

#!r6rs
(library (bound)
(export bound? if-bound)
(import (bound helper))

(make-definition if-bound bound?)
)


example of usage is following.


#!r6rs
(import (rnrs) (bound))

(display (bound? sin))
(display (bound? lambda))
(display (bound? syntax))
(display (bound? foo))

(newline)

;; (define foo "This is foo.\n")

(if-bound foo
(display "foo was bound.\n")
(begin
(display "foo was not bound.\n")
(define foo "This is alternate foo.\n")))

(display foo)


and, as a result I expect


#t#t#t#f
foo was not bound.
This is alternate foo.


It is behavior as expected in Racket, mosh, IronScheme and Petite Chez Scheme.
It is error in the larceny.

Which one is the correct result as R6RS ?
Definition of foo is to determine meaning of preceding definition?

Marco Maggi

unread,
Sep 16, 2012, 3:08:42 PM9/16/12
to
saito wrote:

> In R6RS, I want to branch by existence of identifier
> definition.

It may be that I am not understanding what you really want,
in this case sorry. I assume that you want a predicate for
expand time that would allow you to define a macro like this:

(define-syntax if-bound
(lambda (stx)
(syntax-case stx ()
((_ ?id)
(if (is-it-bound? #'?id)
#'(do-something)
#'(do-something-else))))))

(let ((a 1))
(if-bound a) ==expands to==> (do-something)
(if-bound b) ==expands to==> (do-something-else)
)

I do not think that it is possible to write such a
IS-IT-BOUND? predicate in R6RS.

So:

* Notice that an empty BEGIN form is not valid in all the
contexts; if you want a no-operation form use: (values).

* Your macro is built around FREE-IDENTIFIER=?;
unfortunately it does not work the way you expect. This
predicate answers this question: if we take two
identifiers and put them in the output form of a macro
expansion, will they resolve to the same binding?

I hope that reading [1] can help you clear your mind; it
is just the R6RS document with a bit more examples.

[1] <http://marcomaggi.github.com/docs/nausicaa.html/stdlib-syntax_002dcase-identifier.html>

HTH
--
Marco Maggi

saito

unread,
Sep 16, 2012, 6:45:46 PM9/16/12
to
Thanks for the explanation.
And, sorry. my english is poor. I can not express well.

I think that my macro is valid for the following reasons.

begin keyword has two different roles.
http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.4.7
It is intended to be used in the context of the former.

All identifiers except make-definition is unbound in contexts of library (bound).
datum->syntax made same name unbound identifier with contexts of library (bound).
Identifier that matches compared to unbound identifier is unbound.

But, I think it may bad to define foo when foo is not defined.
Because it will determine the meaning of the preceding definition.
http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-13.html#node_chap_10

Is my understanding wrong?
0 new messages