f



Mutually recursive top-level functions in R7RS

I haven't found anything in the R7RS that guarantees me that the following program is valid:

(import (scheme base)
(define (e? x)
  (or (zero? x)
      (o? (- x 1))))
(define (o? x)
  (not (even? x))

The problem with this program is that when the defining expression of e? is evaluated and o? is referred to, o? may still be unbound, which is an error by section 4.1.1 of the report.

(On the other hand, R6RS explains a top-level body via a letrec*-construct, so the above code would be valid R6RS.)

--

Marc
0
UTF
12/20/2016 6:31:09 PM
comp.lang.scheme 4781 articles. 0 followers. Post Follow

10 Replies
690 Views

Similar Articles

[PageSpeed] 45

In article <62301eb4-eddb-4cce-a545-069989999fec@googlegroups.com>,
 Marc Nieper-Wißkirchen <marc.nieper@gmail.com> wrote:

> I haven't found anything in the R7RS that guarantees me that the following 
> program is valid:
> 
> (import (scheme base)
> (define (e? x)
>   (or (zero? x)
>       (o? (- x 1))))
> (define (o? x)
>   (not (even? x))
> 
> The problem with this program is that when the defining expression of e? is 
> evaluated and o? is referred to, o? may still be unbound, which is an error 
> by section 4.1.1 of the report.

The body of a procedure is evaluated when the procedure is called, not 
when the lambda expression is evaluated. By that time, o? will have been 
added to the global environment. This is explained in section 4.1.4.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Barry
12/21/2016 5:10:01 PM
Am Mittwoch, 21. Dezember 2016 18:10:03 UTC+1 schrieb Barry Margolin:
> In article <62301eb4-eddb-4cce-a545-069989999fec@googlegroups.com>,
>  Marc Nieper-Wi=C3=9Fkirchen <marc.nieper@gmail.com> wrote:
>=20
> > I haven't found anything in the R7RS that guarantees me that the follow=
ing=20
> > program is valid:
> >=20
> > (import (scheme base)
> > (define (e? x)
> >   (or (zero? x)
> >       (o? (- x 1))))
> > (define (o? x)
> >   (not (even? x))
> >=20
> > The problem with this program is that when the defining expression of e=
? is=20
> > evaluated and o? is referred to, o? may still be unbound, which is an e=
rror=20
> > by section 4.1.1 of the report.
>=20
> The body of a procedure is evaluated when the procedure is called, not=20
> when the lambda expression is evaluated. By that time, o? will have been=
=20
> added to the global environment. This is explained in section 4.1.4.

If a reference to an unbound variable is not an error as long as the refere=
nce is not evaluated, the following program would be legal in R7RS:

(import (scheme base))
(lambda ()
  x)

Nevertheless, the R7RS implementation Larceny reports an error about `x' no=
t being bound when fed with this program.

So either your interpretation of when references to unbound variables turn =
into errors is wrong or the Larceny implementation is.

Marc
0
UTF
12/22/2016 12:12:11 PM
In article <4f607e9e-6fad-414e-bfb3-d2574425a14e@googlegroups.com>,
 Marc Nieper-Wißkirchen <marc.nieper@gmail.com> wrote:

> Am Mittwoch, 21. Dezember 2016 18:10:03 UTC+1 schrieb Barry Margolin:
> > In article <62301eb4-eddb-4cce-a545-069989999fec@googlegroups.com>,
> >  Marc Nieper-Wißkirchen <marc.nieper@gmail.com> wrote:
> > 
> > > I haven't found anything in the R7RS that guarantees me that the 
> > > following 
> > > program is valid:
> > > 
> > > (import (scheme base)
> > > (define (e? x)
> > >   (or (zero? x)
> > >       (o? (- x 1))))
> > > (define (o? x)
> > >   (not (even? x))
> > > 
> > > The problem with this program is that when the defining expression of e? 
> > > is 
> > > evaluated and o? is referred to, o? may still be unbound, which is an 
> > > error 
> > > by section 4.1.1 of the report.
> > 
> > The body of a procedure is evaluated when the procedure is called, not 
> > when the lambda expression is evaluated. By that time, o? will have been 
> > added to the global environment. This is explained in section 4.1.4.
> 
> If a reference to an unbound variable is not an error as long as the 
> reference is not evaluated, the following program would be legal in R7RS:
> 
> (import (scheme base))
> (lambda ()
>   x)
> 
> Nevertheless, the R7RS implementation Larceny reports an error about `x' not 
> being bound when fed with this program.
> 
> So either your interpretation of when references to unbound variables turn 
> into errors is wrong or the Larceny implementation is.

I just tried it in the Larceny REPL, and got no error.

$ larceny -r7rs
Larceny v0.99 "Goldie" ...
larceny.heap, built on Wed May 25 01:08:04 EDT 2016

> (import (scheme base))

> (lambda () x)
#<PROCEDURE>

>

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Barry
12/22/2016 5:10:29 PM
> I just tried it in the Larceny REPL, and got no error.
> 
> $ larceny -r7rs
> Larceny v0.99 "Goldie" ...
> larceny.heap, built on Wed May 25 01:08:04 EDT 2016
> 
> > (import (scheme base))
> 
> > (lambda () x)
> #<PROCEDURE>

You will get the error when you don't enter the expressions at the REPL (which may behave differently than the execution mode of a program), but when you run the above code as an R7RS program.

Marc

P.S.: Thanks for looking at it!
0
UTF
12/23/2016 7:04:36 AM
In article <3c448067-bc8d-40e3-9b65-130b18fd063f@googlegroups.com>,
 Marc Nieper-Wißkirchen <marc.nieper@gmail.com> wrote:

> > I just tried it in the Larceny REPL, and got no error.
> > 
> > $ larceny -r7rs
> > Larceny v0.99 "Goldie" ...
> > larceny.heap, built on Wed May 25 01:08:04 EDT 2016
> > 
> > > (import (scheme base))
> > 
> > > (lambda () x)
> > #<PROCEDURE>
> 
> You will get the error when you don't enter the expressions at the REPL 

Is there a missing "not"?

> (which may behave differently than the execution mode of a program), but when 
> you run the above code as an R7RS program.

I just installed Larceny in order to try this, so I'm not familiar with 
the different ways of running programs. Can you show how to reproduce it?

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Barry
12/23/2016 5:34:31 PM
On Thu, 22 Dec 2016 23:04:36 -0800 (PST), Marc Nieper-Wi´┐Żkirchen
<marc.nieper@gmail.com> wrote:

>> I just tried it in the Larceny REPL, and got no error.
>> 
>> $ larceny -r7rs
>> Larceny v0.99 "Goldie" ...
>> larceny.heap, built on Wed May 25 01:08:04 EDT 2016
>> 
>> > (import (scheme base))
>> 
>> > (lambda () x)
>> #<PROCEDURE>
>
>You will get the error when you don't enter the expressions at the 
>REPL (which may behave differently than the execution mode of
>a program), but when you run the above code as an R7RS
>program.

I don't know Larceny so this is pure speculation, but ...

It sounds like the difference between using an interpreter or JIT
compiler vs AOT compilation to a separate executable.

George
0
George
12/24/2016 7:19:14 AM
> I don't know Larceny so this is pure speculation, but ...
> 
> It sounds like the difference between using an interpreter or JIT
> compiler vs AOT compilation to a separate executable.
> 
> George

It has not so much to do with Larceny (it was just an implementation of the R7RS I tested).  Chibi (which is an interpreter) also displays a warning, whenever code is expanded that references an unbound variable.
0
UTF
12/24/2016 10:06:58 AM
Am Freitag, 23. Dezember 2016 18:34:33 UTC+1 schrieb Barry Margolin:
> In article <3c448067-bc8d-40e3-9b65-130b18fd063f@googlegroups.com>,
>  Marc Nieper-Wi=C3=9Fkirchen <marc.nieper@gmail.com> wrote:
>=20
> > > I just tried it in the Larceny REPL, and got no error.
> > >=20
> > > $ larceny -r7rs
> > > Larceny v0.99 "Goldie" ...
> > > larceny.heap, built on Wed May 25 01:08:04 EDT 2016
> > >=20
> > > > (import (scheme base))
> > >=20
> > > > (lambda () x)
> > > #<PROCEDURE>
> >=20
> > You will get the error when you don't enter the expressions at the REPL=
=20
>=20
> Is there a missing "not"?

I don't think so. I am saying that you will get the error when run the decl=
arations and expressions as a program (versus entering them at the REPL).

>=20
> > (which may behave differently than the execution mode of a program), bu=
t when=20
> > you run the above code as an R7RS program.
>=20
> I just installed Larceny in order to try this, so I'm not familiar with=
=20
> the different ways of running programs. Can you show how to reproduce it?
>=20

If you want to run an R7RS program (as defined in 5.1 of the R7RS, you stor=
e the program in a file, say `program.scm' and then you run `larceny -r7rs =
-program program.scm'.
0
UTF
12/24/2016 10:09:27 AM
In article <92d00acc-c215-4366-afc1-f7c1545f0b06@googlegroups.com>,
 Marc Nieper-Wißkirchen <marc.nieper@gmail.com> wrote:

> > I don't know Larceny so this is pure speculation, but ...
> > 
> > It sounds like the difference between using an interpreter or JIT
> > compiler vs AOT compilation to a separate executable.
> > 
> > George
> 
> It has not so much to do with Larceny (it was just an implementation of the 
> R7RS I tested).  Chibi (which is an interpreter) also displays a warning, 
> whenever code is expanded that references an unbound variable.

A warning is fine, but the OP said he got an error.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Barry
12/24/2016 8:28:19 PM
In article <efb37df7-eabc-4dcc-a4c9-c4ae786e900b@googlegroups.com>,
 Marc Nieper-Wißkirchen <marc.nieper@gmail.com> wrote:

> Am Freitag, 23. Dezember 2016 18:34:33 UTC+1 schrieb Barry Margolin:
> > In article <3c448067-bc8d-40e3-9b65-130b18fd063f@googlegroups.com>,
> >  Marc Nieper-Wißkirchen <marc.nieper@gmail.com> wrote:
> > 
> > > > I just tried it in the Larceny REPL, and got no error.
> > > > 
> > > > $ larceny -r7rs
> > > > Larceny v0.99 "Goldie" ...
> > > > larceny.heap, built on Wed May 25 01:08:04 EDT 2016
> > > > 
> > > > > (import (scheme base))
> > > > 
> > > > > (lambda () x)
> > > > #<PROCEDURE>
> > > 
> > > You will get the error when you don't enter the expressions at the REPL 
> > 
> > Is there a missing "not"?
> 
> I don't think so. I am saying that you will get the error when run the 
> declarations and expressions as a program (versus entering them at the REPL).

Ahh, I found the "not" I missed, it was in "don't".

> 
> > 
> > > (which may behave differently than the execution mode of a program), but 
> > > when 
> > > you run the above code as an R7RS program.
> > 
> > I just installed Larceny in order to try this, so I'm not familiar with 
> > the different ways of running programs. Can you show how to reproduce it?
> > 
> 
> If you want to run an R7RS program (as defined in 5.1 of the R7RS, you store 
> the program in a file, say `program.scm' and then you run `larceny -r7rs 
> -program program.scm'.

I can't seem to get -program to work at all. I tried a trivial program 
that just contained:

(define x 3)

and I got an error because it seems to be wrapping everything in the 
program in a list

The error looks like:

Syntax violation: expand-file

File should be of the form:
    <library>*
  | <library>* <toplevel program>

Form: ((define x 3))

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Barry
12/24/2016 8:46:43 PM
Reply: