2

Consider the following code:

#!r6rs
(library
 (test)
 (export)
 (import (rnrs))

 (define a 5)
 (begin
   (define b 4)
   (+ 3 b))
 'cont
 (define c 5)
 'done)

From the R6RS Report 7.1:

A <library body> is like a <body> (see section 11.3) except that a <library body>s need not include any expressions. It must have the following form:

<definition> ... <expression> ...

I thought it would emit error because definition of c is after expression 'cont, but this program is accepted cleanly.

After Then, I thought a and c could be exported. But, not c but b can be exported. (a can be exported as I thought.)

I think there are something I didn't realize about R6RS library rules. What is the point I'm missing? Thanks in advance.

p.s) I'm using Racket v5.3.3

leppie
  • 115,091
  • 17
  • 196
  • 297
Venusaur
  • 191
  • 12
  • You are correct. The definition of `c` should be an error. This is the case with IronScheme and Chez Petite. I can only assume a Racket bug or relaxed semantics. – leppie Apr 26 '13 at 05:44

2 Answers2

0

Sorry, this is not the right answer. It is how the program toplevel works, not library toplevel. Leaving it here for reference.

In the program toplevel things work a bit different from normal (normal being the way you interpreted it).

The code will be rewritten by the compiler to look something like:

(define a 5)
(define b 4)
(define dummy1 (+ 3 b))
(define dummy2 'cont)
(define c 5)
'done

Notes:

  • begin splices in the toplevel
  • For any non-definition, the expression is assigned to a 'dummy' variable
  • The toplevel eventually ends up looking like a letrec* and the same rules apply
leppie
  • 115,091
  • 17
  • 196
  • 297
  • Thanks for the answer. I started worrying it might be very hard to make portable R6RS code. – Venusaur Apr 30 '13 at 06:13
  • @Venusaur: Portable code is quite easy with R6RS. The only non-portable bit I am aware of is differences in phase handling during macro expansion. – leppie Apr 30 '13 at 06:16
0

From the R6RS 2007 specification:

A library definition must have the following form:

(library <library name>
  (export <export spec> ...)

  (import <import spec> ...)

  <library body>)


...

 The <library body> is the library body, consisting of a sequence of definitions 
 followed by a sequence of expressions. The definitions may be both for local 
 (unexported) and exported bindings, and the expressions are initialization 
 expressions to be evaluated for their effects.

Thus, for your example code, an error should have been raised.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • At first time, I thought the example code should emit some errors. However, in Racket 5.3.3, it accepted. So, (considering leppie's answer) I concluded that it is Racket's "habit". – Venusaur Apr 30 '13 at 06:11