MathGroup Archive 1992

[Date Index] [Thread Index] [Author Index]

Search the Archive

declaration vs. assignment and dynamic vs. static binding

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: declaration vs. assignment and dynamic vs. static binding
  • From: gaylord at ux1.cso.uiuc.edu
  • Date: Wed, 11 Mar 1992 14:39:34 -0600

this is a question as to the difference between (functional) declaration
and (procedural) assignment.

 i am specifically trying to figure out what := and = really are. does
Mathematica (the language) use assignment or declaration or assignment with
dynamic or static binding or what and when?
 
i received a lot of really interesting responses, some of which i
understood.

Thomas Yan of Cornell sent ML code which I translated into M and ran

=========================
from Thomas Yan:

terminology is a problem.  i think mine is what is generally used in the cs
community.  declaration is like that for constants: you declare it, and the
value never changes.  assignment reassigns/updates an old value.  dynamic
binding is even nastier, since it is not "lexically scoped": in the
previous
examples, the only things (e.g. functions) that see the values are those
textually nearby (after the declaration/assignment).  in dynamic binding,
the
values can be anywhere.

here is an example that
distinguishes all three cases.

    fun g(f,x) = let val c = 3 in f(x) end
    fun h() = let val c = 5
                   fun f(x) = x + c
                   val a = g(f,0)
                   val c = 7
                   val b = g(f,1)
               in (a,b) end

what is h()?

1) declaration: in gc, the "val=c" has no effect, since c is never used.
    essentially, g is just function application, and you get (5,6).  note
that
    in sml, declarations are sequential.  thus, f(x) = x + c would not be
legal
    until c is defined, e.g. illegal before val c = 5, unless there was
already
    a c declared in an outer scope.

2) assignment:  in g, the "val=c" has no effect, since c is never used.
    suppose c is a local variable, and c=7 reassigns the value of c.  well,
f
    is in the scope of c (the scope of variables, that is, where they are
    visible for use, are just within the "let"), so it uses the local c. 
you
    get our friend (5,8).

3) dynamic binding: the local declarations of c in h don't matter in this
case,
    since the c in g overrides it.  you would get (3,4).  this also shows
you
    why i think dynamic binding can be a problem -- suppose you defined g
in a
    separate file, before the definition of h.  there is no reason to
expect
    the "val c = 3" to have any effect on f or x.

    of course, you might think of f as shorthand notation which just uses
the
    current value of c, and this can be useful.  it just requires careful
use.

-----remainder deleted----------------

======================

Thomas said that declaration would give (5, 6), assignment would give (5,
8) and dynamic binding would give (3, 4).

in Mathematica (if i'm translating correctly) his program reads:

Clear[f, g, h]
g[f_, x_] := Module[{c = 3}, f[x]]
h[] := Module[{c = 5},
											f[x_] := x + c;
											a = g[f, 0];
											c = 7;
											b = g[f, 1];
											{a, b}]

h[]
{5, 8}


Clear[f, g, h]
g[f_, x_] := Module[{c = 3}, f[x]]
h[] := Module[{c = 5},
											f[x_] = x + c;
											a = g[f, 0];
											c = 7;
											b = g[f, 1];
											{a, b}]
h[]
{5, 6}



It would appear that according to Thomas Yan , Mathematica uses declaration
(when f[x] = is used) and assignment (when f[x_] := is used), but not
dynamic binding.

this is confusing because of notational idiosyncracies of Mathematica which
calls = assignment and := delayed assignment.

i have codefrom other people as well that i'm presently translating but i
thought i'd send this out  to the people who sent me stuff and to groups
that might be interested.

any comments on this topic will be welcome.

====================================








  • Prev by Date: printing postscript notebooks
  • Next by Date: declaration-assignment-dynamic binding update
  • Previous by thread: printing postscript notebooks
  • Next by thread: declaration-assignment-dynamic binding update