Correct definition for begin 
Author Message
 Correct definition for begin

The IEEE standard for Scheme specifies that begin
can contain either a list of definitions or a list
of expressions, but not both. Is that really what
was intended, or is the standard in error?

Consider the following code:

(begin
        (define test 3)
        (eq? test 3)
)

Is this legal or illegal? The standard says this is
illegal, but many of the Schemes I've used will accept
it. Similarly:

(begin
        (eq? test 3)
        (define test 3)
)

Is accepted by MIT scheme, but chokes VSCM.

What's the "correct" definition for begin?

Thanks,



Mon, 14 Oct 1996 06:11:43 GMT  
 Correct definition for begin

   The IEEE standard for Scheme specifies that begin
   can contain either a list of definitions or a list
   of expressions, but not both. Is that really what
   was intended, or is the standard in error?

How can the standard be ``in error''?  The standard is the standard --
so by definition it is *not* in error. :-)

   Consider the following code:

   (begin
           (define test 3)
           (eq? test 3)
   )

   Is this legal or illegal? The standard says this is
   illegal,

If the standard says ``It's illegal'' then it is illegal.

            but many of the Schemes I've used will accept
   it. Similarly:

   (begin
           (eq? test 3)
           (define test 3)
   )

   Is accepted by MIT scheme, but chokes VSCM.

It doesn't ``choke'' VSCM -- VSCM just complains about a syntax error.
It has the right to do so.
In general I consider it a bad idea for an implementation to let
programmers get away with violations of the standard.  This leads to
potential portability problems, which go undetected.  The next release
of VSCM will very likely be even stricter about this issue and also
complain about (begin (define test 3) (eq? test 3)).

   What's the "correct" definition for begin?

The one in the standard. :-)

--
-Matthias



Mon, 14 Oct 1996 12:19:52 GMT  
 Correct definition for begin

Quote:

>The IEEE standard for Scheme specifies that begin
>can contain either a list of definitions or a list
>of expressions, but not both. Is that really what
>was intended, or is the standard in error?

It is really what was intended.  The utility of it is that it allows
macro definitions to expand to multiple defines.

I've found it useful to think of begin as two constructs;
one is a sequencing construct, which is what one normally uses;
the other is a grouping of defines, which can only occur in a context
where a single define can occur, and that's its purpose.

Note:  begin, according to r4rs, does *not* start a new scoping block,
only a sequence.  Which is what makes

(begin
  (define foo 1)
  (apply bar foo))

incorrect, although many Scheme implementations support such code.

Regards,
Dak
--
David A. 'Dak' Keldsen
Product Development Manager
SoftQuad Inc.
Toronto, Canada
[these are certainly not the opinions of SoftQuad Inc.]



Mon, 14 Oct 1996 22:37:52 GMT  
 Correct definition for begin

Quote:
>R^4RS says that a begin block can contain
>only expressions, or only definitions, but not both.
>Fortunately, this isn't too onerous.  You can always say

>    (begin
>      (begin
>        (define (foo a) (+ a 1)))
>      (foo 5))

My reading of page 12 of R4RS is that a (begin (define ...) ...) form
is itself a definition!  Therefore the suggested code above is not
allowed either, except in implementations that allow mixing definitions
with expressions in a begin, in which case this circumlocution isn't
necessary.

A begin full of defines *must* count as a definition, in order to allow
for its (I gather) intended use:

(lambda (...)
  (define foo baz)
  (define garply mumble)
  (some-macro-that-makes-definitions 87)
  (define drummer 'ringo)
  (list foo garply thing-the-macro-defined-1 thing-it-defined-2 drummer))

where the macro expands to (begin (define thing-the-etc...) ...).  If
this begin form counted as an expression, rather than as a definition,
then the (define drummer 'ringo) following it would be illegally positioned.



Tue, 15 Oct 1996 08:12:44 GMT  
 Correct definition for begin

Quote:

>   The IEEE standard for Scheme specifies that begin
>   can contain either a list of definitions or a list
>   of expressions, but not both. Is that really what
>   was intended, or is the standard in error?

>How can the standard be ``in error''?  The standard is the standard --
>so by definition it is *not* in error. :-)

You've obviously never worked on POSIX 1003.2 (Shell and
Utilities) :-)

Quote:
>            but many of the Schemes I've used will accept
>   it. Similarly:

>   (begin
>       (eq? test 3)
>       (define test 3)
>   )

>   Is accepted by MIT scheme, but chokes VSCM.

Then MIT Scheme is broken (unless test is bound in the top
level environment).

Quote:

>It doesn't ``choke'' VSCM -- VSCM just complains about a syntax error.
>It has the right to do so.
>In general I consider it a bad idea for an implementation to let
>programmers get away with violations of the standard.  This leads to
>potential portability problems, which go undetected.  The next release
>of VSCM will very likely be even stricter about this issue and also
>complain about (begin (define test 3) (eq? test 3)).

Many standards level things unspecified so that implementors
can extend the functionality.  Allowing for both expressions
and definitions in a begin block is a valid extension.
Providing a "fascist" flag that reports violations of the
standard (aka uses of extensions) is a good thing, however.

Quote:

>   What's the "correct" definition for begin?

>The one in the standard. :-)

For the most part, I consider the most recent Revised^n Report
to be the "standard" to follow.  Interestingly enough,
according to the Revised^3 report (my revised^4 is at work), it
is illegal to have defines in a begin.  Section 4.2.3
(Sequencing) says

        (begin <expression1> <expression2> ...)

but, define is described in section 5 (Program Structure), not
in section 4 (Expressions).  More to the point, the formal
syntax in section 7.1.3 says

        <expr> ::= <variable>
                |  <literal>
                |  <procedure call>
                |  <lambda expression>
                |  <conditional>
                |  <assignment>
                |  <derived expression>

and a <definition> is none of the above.

- David



Tue, 15 Oct 1996 05:32:57 GMT  
 Correct definition for begin

Quote:

>>   What's the "correct" definition for begin?

>>The one in the standard. :-)

>For the most part, I consider the most recent Revised^n Report
>to be the "standard" to follow.  Interestingly enough,
>according to the Revised^3 report (my revised^4 is at work), it
>is illegal to have defines in a begin.

Well, that'll teach me to speak without all the information
available to me.  R^4RS says that a begin block can contain
only expressions, or only definitions, but not both.
Fortunately, this isn't too onerous.  You can always say

        (begin
          (begin
            (define (foo a) (+ a 1)))
          (foo 5))

- David



Tue, 15 Oct 1996 06:43:38 GMT  
 Correct definition for begin


Quote:

> [...]  R^4RS says that a begin block can contain
> only expressions, or only definitions, but not both.

Where?  I've looked a few times now and I can't find where it says
this.  All I gather is that begin is solely for sequencing.  No new
scopes or nuthin.  The statement

    (begin
      (define test 3)
      (eq? test 3))

is legal anywhere

    (define test 3)
    (eq? test 3)

is, except that you can use the begin in a situation where only one
expression is expected.  In other words, if the example begin
statement is entered at the top level, the variable `test' becomes
globally defined despite the fact that it occurred within the begin
statement.

This is extremely important when writing macros.  Actually, I wanted to
ask about `begin' myself, because although this example works with
MacScheme, I don't seem to be allowed to put defines inside of begins
via macros.  But I can in Chez Scheme.  Go figure.

In Chez Scheme, even this is legal:

  (define (a)
    (define b 2)
    (begin
      (define c 3))
    (eq? b c))

(as I would expect) and this isn't:

  (define (a)
    (define b 2)
    (begin
      (eq? a b)
      (define c 3)))

(as I would expect).  (Note that the R4RS says that define statements
can be (but aren't required to be) supported at the beginning (only!)
of the body of an enclosing define statement.  (If it is supported,
the body of the define is just turned into a letrec.))  But I've never
read the IEEE report on Scheme, just the R4RS.

thant



Fri, 18 Oct 1996 16:33:14 GMT  
 Correct definition for begin

Quote:



>> [...]  R^4RS says that a begin block can contain
>> only expressions, or only definitions, but not both.

>Where?  I've looked a few times now and I can't find where it says
>this.  All I gather is that begin is solely for sequencing.  No new
>scopes or nuthin.

Section 7.1.3. Expressions says

        <derived expression> ::= ...
                                | (begin <sequence>)

        <sequence> ::= <command>* <expression>

        <command> ::= <expression>

So a begin can only have <expression>s in it according to
7.1.3, and <definition>s aren't expressions.

However, section 7.1.5. Programs and definitions says

        <definition> ::= (define <variable> <expression>)
                        | (define (variable <def formals>) <body>)
                        | (begin <definition>*)

So a begin can only have <definition>s in it according to
7.1.5.  What this boils down to is that "begin" is really two
very different things, that just happen to look the same.

- David



Sat, 19 Oct 1996 05:26:55 GMT  
 Correct definition for begin

Quote:



>> [...]  R^4RS says that a begin block can contain
>> only expressions, or only definitions, but not both.

>Where?  I've looked a few times now and I can't find where it says
>this.

Section 4.2.3 shows the syntax (begin <expression1> <expression2> ...).
Section 5.2 shows the syntax (begin <definition1> ...).  5.2 also says that
definitions may be used in some places where expressions are allowed, but
inside a begin construct is not one of them.
--
Barry Margolin
System Manager, Thinking Machines Corp.




Sun, 20 Oct 1996 00:14:21 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Q: Definitions enclosed in nested begins?

2. Query on macro definitions within macro definitions within...

3. PyKyra 0.1.0 the "Begin in the beginning, the King said" release

4. Which book should I begin with?

5. Beginning to Programm Assembler on Mainframes

6. Beginning with SmallTalk

7. "Beginning J. Second edition

8. ST - The Game Begins

9. Beginning Smalltalk

10. Inserting text at the beginning of a line

11. Nested Brackets Begin { ?

12. whitespace at beginning of second line

 

 
Powered by phpBB® Forum Software