1

I try to understand how works the quote phenomenon in Scheme. In particular, I would like to understand when are bound free variables of quoted terms.

For instance, when I write

(define q 'a)
(define a 42)
(eval q)

it returns 42. Thus I deduce that binding time is at runtime. But in this case, why does this code fail

(let ((q 'a))
  (let ((a 42))
    (eval q)
  )
)

and returns

unbound variable:  a

Can someone explain me what is the binding time model of quoted terms (is is comparable to MetaOCaml for instance? (I don't think so)) and the difference between define and let?

1
  • 1
    what implementation? "typing" where -- at the REPL, in a file?
    – Will Ness
    May 3, 2016 at 17:38

2 Answers 2

2

Scheme has lexical scope discipline, not a dynamic binding discipline.

Your top-level define definitions behave as though creating a binding in a top-level lexical environment.

The second code snippet actually creates two lexical environments, one nested inside the other. So where (not "when") q is bound, a is still unbound. But the real question is, which environment is used by eval?

Your implementation behaves as though it uses the definitional environment, or a top level environment, but certainly not the current lexical environment, for evaluating the symbol 'a, which is the value of the q variable. The variable q has a clear binding lexical environment, created by its let form -- but where does a symbol 'a's binding reside? How are we to know?

Details should be in the documentation.

1

First off a quoted symbol is just as much a variable as a string with the same sequences of chars as a variable in a C syntax language like Javascript. They have nothing in common since they live in different worlds.

eval does not know of lexical variables, only global ones. It knows of lexical variables that is in the structure to be evaluated. Eg.

(eval '(let ((tmp (list q q))) 
          tmp)) 

q needs to be global, but tmp is a lexical variable.

Standard Scheme, aka R6RS, take a second argument where you can choose what libraries should be available. These are still considered global.

Variables are bound at runtime. Implementations are free to optimize and constant fold as long as this optimization does not break the report.

eval is a powerful procedure which should never be used unless it's the most sensible way to solve a problem. I've seen it twice in production code during my 17 year career and I think it's one time too much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.