Dynamic/Static scope with Deep/Shallow binding (exercises)

后端 未结 3 1603
面向向阳花
面向向阳花 2021-01-21 10:55

I\'m studying dynamic/static scope with deep/shallow binding and running code manually to see how these different scopes/bindings actually work. I read the theory and googled so

3条回答
  •  再見小時候
    2021-01-21 11:35

    Static binding, also known as lexical scope, refers to the scoping mechanism found in most modern languages.

    In "lexical scope", the final value for u is neither 180 or 119, which are wrong answers.

    The correct answer is u=101.

    Please see standard Perl code below to understand why.

    use strict;
    use warnings;
    
    my $u = 42;
    my $v = 69;
    my $w = 17;
    
    sub add {
        my $z = shift;
        $u = $v + $u + $z;
    }
    
    sub bar {
        my $fun = shift;
        $u = $w;
        $fun->($v);
    }
    
    sub foo {
        my ($x, $w) = @_;
        $v = $x;
        bar( \&add );
    }
    
    foo($u,13);
    print "u: $u\n";
    

    Regarding shallow binding versus deep binding, both mechanisms date from the former LISP era.

    Both mechanisms are meant to achieve dynamic binding (versus lexical scope binding) and therefore they produce identical results !

    The differences between shallow binding and deep binding do not reside in semantics, which are identical, but in the implementation of dynamic binding.

    With deep binding, variable bindings are set within a stack as "varname => varvalue" pairs.

    • The value of a given variable is retrieved from traversing the stack from top to bottom until a binding for the given variable is found.
    • Updating the variable consists in finding the binding in the stack and updating the associated value.
    • On entering a subroutine, a new binding for each actual parameter is pushed onto the stack, potentially hiding an older binding which is therefore no longer accessible wrt the retrieving mechanism described above (that stops at the 1st retrieved binding).
    • On leaving the subroutine, bindings for these parameters are simply popped from the binding stack, thus re-enabling access to the former bindings.

    Please see the the code below for a Perl implementation of deep-binding dynamic scope.

    use strict;
    use warnings;
    use utf8;
    
    ##
    # Dynamic-scope deep-binding implementation
    my @stack = ();
    
    sub bindv {
        my ($varname, $varval);
    
        unshift @stack, [ $varname => $varval ]
            while ($varname, $varval) = splice @_, 0, 2;
    
        return $varval;
    }
    
    sub unbindv {
        my $n = shift || 1;
        shift @stack while $n-- > 0;
    }
    
    sub getv {
        my $varname = shift;
    
        for (my $i=0; $i < @stack; $i++) {
            return $stack[$i][1]
                if $varname eq $stack[$i][0];
        }
    
        return undef;
    }
    
    sub setv {
        my ($varname, $varval) = @_;
    
        for (my $i=0; $i < @stack; $i++) {
            return $stack[$i][1] = $varval
                if $varname eq $stack[$i][0];
        }
        return bindv($varname, $varval);
    }
    
    ##
    # EXERCICE
    bindv(  u => 42,
            v => 69,
            w => 17,
    );
    
    sub add {
        bindv(z => shift);
    
         setv(u => getv('v')
                   + getv('u')
                   + getv('z')
        );
    
        unbindv();
    }
    
    sub bar {
        bindv(fun => shift);
    
         setv(u   => getv('w'));
        getv('fun')->(getv('v'));
    
        unbindv();
    }
    
    sub foo {
        bindv(x => shift,
              w => shift,
        );
    
         setv(v => getv('x'));
        bar( \&add );
    
        unbindv(2);
    }
    
    foo( getv('u'), 13);
    print "u: ", getv('u'), "\n";
    

    The result is u=97

    Nevertheless, this constant traversal of the binding stack is costly : 0(n) complexity !

    Shallow binding brings a wonderful O(1) enhanced performance over the previous implementation !

    Shallow binding is improving the former mechanism by assigning each variable its own "cell", storing the value of the variable within the cell.

    • The value of a given variable is simply retrieved from the variable's cell (using a hash table on variable names, we achieve a 0(1) complexity for accessing variable's values!)
    • Updating the variable's value is simply storing the value into the variable's cell.
    • Creating a new binding (entering subs) works by pushing the old value of the variable (a previous binding) onto the stack, and storing the new local value in the value cell.
    • Eliminating a binding (leaving subs) works by popping the old value off the stack into the variable's value cell.

    Please see the the code below for a trivial Perl implementation of shallow-binding dynamic scope.

    use strict;
    use warnings;
    
    our $u = 42;
    our $v = 69;
    our $w = 17;
    our $z;
    our $fun;
    our $x;
    
    sub add {
        local $z = shift;
        $u = $v + $u + $z;
    }
    
    sub bar {
        local $fun = shift;
        $u = $w;
        $fun->($v);
    }
    
    sub foo {
        local $x = shift;
        local $w = shift;
        $v = $x;
        bar( \&add );
    }
    
    foo($u,13);
    print "u: $u\n";
    

    As you shall see, the result is still u=97

    As a conclusion, remember two things :

    • shallow binding produces the same results as deep binding, but runs faster, since there is never a need to search for a binding.

    • The problem is not shallow binding versus deep binding versus
      static binding BUT lexical scope versus dynamic scope (implemented either with deep or shallow binding).

提交回复
热议问题