Scope of local defines? 
Author Message
 Scope of local defines?

Defining the following expression, evaluating (f1) and (f2) yields
surprising results. Shouldn't be the result 'foo in both cases?

(define (f1)
  (define f1 'foo)
  (define g1 f1)
  g1)
;;
;; change the order of defining the local g and f
;;

(define (f2)
  (define g2 f2)
  (define f2 'foo)
  g2)

SCM version 4a10:

        > (f1)
        ;Evaluation took 0 mSec (0 in gc) 6 cons work
        foo
        > (f2)
        ;Evaluation took 0 mSec (0 in gc) 6 cons work

MIT Scheme 7.1.3

        Scheme saved on Wednesday May 13, 1992 at 2:41:35 PM
          Release 7.1.3
          Microcode 11.59
          Runtime 14.104
          SF 4.20

1 ]=> (f1)

;Value: foo

1 ]=> (f2)

Unassigned variable f2

------------------------------------------------------------------------

------------------------------------------------------------------------



Mon, 09 Oct 1995 19:26:58 GMT  
 Scope of local defines?
Scope of local defines?


Quote:

>Defining the following expression, evaluating (f1) and (f2) yields
>surprising results. Shouldn't be the result 'foo in both cases?

>(define (f1)
>  (define f1 'foo)
>  (define g1 f1)
>  g1)
>;;
>;; change the order of defining the local g and f
>;;

>(define (f2)
>  (define g2 f2)
>  (define f2 'foo)
>  g2)

No.  Internal defines essentially turn into letrec's.  So the above is
equivalent to:

(define (f1)
  (letrec ( (f1 'foo) (g1 'foo) )
     g1))

(define (f2)
  (letrec ((g2 'foo)  (f2 'foo) )
     g2))

The semantics of letrec is that locations are introduced for the bindings
and then the init-forms (f1, f2, 'foo) are evaluated in some unspecified
order.  This means that you should not really expect "raw" values to
refer to each other.

To have the effect you want, you can use lambda bindings and
everything will work out ok, as there is no problem with evaluation
order dependencies:

(define (f1)
  (define f1 (lambda () 'foo))
  (define g1 (lambda () f1))
  (g1))

(define (f2)
  (define g2 (lambda () f2))
  (define f2 (lambda () 'foo))
  (g2))

This is a common source of confusion.  It has been recommended at times
that internal defines not be used and that letrec forms always be
lambdas.  Dylan, for example, has a bind-methods form which is like
Scheme's letrec except that only lambdas (called methods in Dylan) are
allowed.

-Ken



Tue, 10 Oct 1995 05:39:24 GMT  
 Scope of local defines?
   Defining the following expression, evaluating (f1) and (f2) yields
   surprising results. Shouldn't be the result 'foo in both cases?

   (define (f1)           (define (f2)
     (define f1 'foo)       (define g2 f2)
     (define g1 f1)         (define f2 'foo)
     g1)                    g2)

The way I read section 5.2.2. of the IEEE Standard, _both_ of the
above should result in an error (cf. 4.2.2).  Any language lawyers
care to confirm/deny this?



Tue, 10 Oct 1995 02:20:48 GMT  
 Scope of local defines?

   Defining the following expression, evaluating (f1) and (f2) yields
   surprising results. Shouldn't be the result 'foo in both cases?

   (define (f1)
     (define f1 'foo)
     (define g1 f1)
     g1)
   ;;
   ;; change the order of defining the local g and f
   ;;

   (define (f2)
     (define g2 f2)
     (define f2 'foo)
     g2)

These are both invalid code.  According to R4RS, local DEFINEs can be
rewritten as LETREC.  As such, the variables of the local DEFINEs are
not bound while the initializing clauses evaluate.  Newer versions of
SCM memoize local DEFINEs to a LETREC form and catch the unbound
variable error.

Hugh
--

MIT, Research Lab of Electronics  UUCP:      mit-eddie!mit-athena!hugh



Tue, 10 Oct 1995 06:34:28 GMT  
 Scope of local defines?
Thanks to all, who replied to my question regarding the scope of local
defines. The question was not just academic to me. I'm writing a tool,
that resolves all bindings, makes all identfiers unique and enters
them to a flat symbol table.

Christian

------------------------------------------------------------------------

------------------------------------------------------------------------



Tue, 10 Oct 1995 19:05:02 GMT  
 Scope of local defines?


   Date: 22 Apr 93 21:39:24 GMT



   >Defining the following expression, evaluating (f1) and (f2) yields
   >surprising results. Shouldn't be the result 'foo in both cases?
   >
   >(define (f1)
   >  (define f1 'foo)
   >  (define g1 f1)
   >  g1)
   >;;
   >;; change the order of defining the local g and f
   >;;
   >
   >(define (f2)
   >  (define g2 f2)
   >  (define f2 'foo)
   >  g2)

   No.  Internal defines essentially turn into letrec's.  So the above is
   equivalent to:

   (define (f1)
     (letrec ( (f1 'foo) (g1 'foo) )
        g1))

   (define (f2)
     (letrec ((g2 'foo)  (f2 'foo) )
        g2))

This is incorrect.  The first pair of forms are equivalent to:

(define (f1)
  (letrec ( (f1 'foo) (g1 f1) )
     g1))

(define (f2)
  (letrec ((g2 f2)  (f2 'foo) )
     g2))

which is erroneous code.  Recent versions of SCM catch the error.

Just picking nits.

Hugh
--

MIT, Research Lab of Electronics  UUCP:      mit-eddie!mit-athena!hugh



Wed, 11 Oct 1995 06:46:50 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Withdrawn SRFI 24: Define-syntax in local lexical scopes

2. SRFI 24: Define-syntax in local lexical scopes

3. Local variable scope

4. load and local variable scope (and constants too)

5. about the the scope of a local variable

6. Why is $_ local to the current scope?

7. Relate Tcl_SetVar variables to local scope in Tcl_Eval?

8. Scope of local and global variables

9. Question re local bindings in nested scopes

10. no re-binding of nested-scope locals, no re-binding in lambda, no,,, {was Re: lambda)

11. How to force local scope in itcl?

12. HELP: scope of local variables

 

 
Powered by phpBB® Forum Software