2

I am trying to have a better understanding about free and bound variables. Here is an example code:

(define (what-kind-of-var? guess x)
    (< (abs (- (square guess) x))
        0.001))

I see that bound variables here would be guess and x, and free variables <, abs, -, and square. What if I called what-kind-of-var? recursively? Would it be a bound variable because it is binding itself?

Thanks!

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Isaac
  • 273
  • 3
  • 19

2 Answers2

0
  • guess and x are parameters. They're eventually bound (to respective arguments) when the function is applied.

  • <, abs, -, are actually bound to procedures in the initial environment. So they're not free variables.

  • square would be the free variable, subject to the fact that what-kind-of-var? is not defined in its scope. (note that sqr is bound in the initial environment).

  • what-kind-of-var? is also not unbound, even if it calls itself recursively (assuming recursion is implemented properly in the language). (define (f param) body) can be seen as (define f (lambda (param) body)

Atharva Shukla
  • 2,098
  • 8
  • 21
  • 4
    They're free in this expression, for sure. They may be bound in the global environment, but in this expression they are free. – amalloy Feb 10 '20 at 08:23
  • Yes, I assumed that the function would be typed out in an editor. But if we consider _only_ this expression then, yes, they're free. – Atharva Shukla Feb 11 '20 at 18:23
0

It would, under dynamic binding, but Scheme has lexical scoping.

But actually it is neither. "Free" or "bound" comes from lambda calculus. what-kind-of-var? is a top-level variable, naming that lambda expression,

(define what-kind-of-var? 
  (lambda (guess x)                        ;; this
     (< (abs (- (square guess) x))
         0.001)))

but in lambda calculus expressions cannot be named. The only way to call it recursively in lambda calculus is to use Y combinator:

((Y (lambda (what-kind-of-var?)                ;; outer
      (lambda (guess x)                            ;; inner
         (if (< (abs (- (square guess) x))
                0.001)
           guess
           (what-kind-of-var? (+ 1 guess) x)))))
  4 25)

and now of course what-kind-of-var? is bound inside that new lambda expression under Y. It's free in the nested, inner lambda, but bound in the outer one.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • r7rs got dynamic binding too https://srfi.schemers.org/srfi-39/srfi-39.html – alinsoar Feb 10 '20 at 12:11
  • I think the ideas of free and bound appeared also in the works of Frege (about 1880?). Lambda calculus defined them precisely but they for sure appeared before. – alinsoar Feb 10 '20 at 12:15
  • A. not the language itself, but as an extension. B. we're in Scheme, so LC is what's relevant. of course LC itself is a development of the "open formula" concept (i.e. a logical formula with free variables) – Will Ness Feb 10 '20 at 12:17
  • dynamic binding is standardized now, following the request for Implementation I pasted. New versions of scheme will contain it in their kernel by default. – alinsoar Feb 10 '20 at 12:19
  • https://bitbucket.org/cowan/r7rs-wg1-infra/src/default/R7RSHomePage.md – alinsoar Feb 10 '20 at 12:22
  • mit-scheme also passed to R7RS I think , not sure that they updated it completely – alinsoar Feb 10 '20 at 12:23
  • the default scoping is still lexical though, correct? – Will Ness Feb 10 '20 at 12:25
  • default is lexical, dynamic is inside the `parameterize` form. By default this form will be present in the next future of scheme. So far it was only optional , some of implementors wanted to have optionally that request of implementation. – alinsoar Feb 10 '20 at 12:26
  • but the default scope for a given Scheme variable will still be lexical. to get dynamic one would have to use some special form. – Will Ness Feb 10 '20 at 12:27
  • yes, the default way of binding is static, as so far. Nothing changes. – alinsoar Feb 10 '20 at 12:28