Dynamically -shallow binding and deep binding

2019-08-12 00:51发布

问题:

I'm working on this problem and I got the answers :

Statically: 13

Dynamically -deep binding: 2 <- i'm not sure about this one

Dynamically -shallow binding: 2 <- i'm not sure about this one

is that correct?

Consider the program below (in a Pascal like language). What is the output of the language is statically scoped? What is the output of the language is dynamically scoped and uses deep binding? What is the output of the language is dynamically scoped and uses shallow binding?

Program main;
   x: integer := 2;
   y: integer := 1;
procedure f3(z: integer)
   begin 
       x = z + x + y;
   end
procedure f2(p: procedure, z: integer)
     int x := 5;
   begin
              p(z) 
   end
procedure f1(z: integer)
      int y := z
   begin
     f2(f3,y);
  end

begin /* main program */
f1(4);
       print(x)
end

回答1:

For the static scope and the dynamic-scope-with-shallow-binding cases, why not try them out? Using Perl with static scope:

my $x = 2;
my $y = 1;
sub f3($) {
  my $z = shift;
  $x = $z + $x + $y;
}
sub f2($$) {
  my ($p, $z) = @_;
  my $x = 5;
  $p->($z);
}
sub f1($) {
  my $z = shift;
  my $y = $z;
  f2(\&f3, $y);
}
f1(4);
print "$x\n";

I get 7 (which is 4 + 2 + 1). Changing the mys to locals to get dynamic scope with shallow binding, I get 2, as you predicted.

Testing out dynamic scope with deep binding is trickier, because so few languages support it. In this answer a while back, I posted Perl code that implemented deep binding "manually" by passing around a hash of references to scalars; using that same approach:

#!/usr/bin/perl -w

use warnings;
use strict;

# Create a new scalar, initialize it to the specified value,
# and return a reference to it:
sub new_scalar($)
  { return \(shift); }

# Bind the specified procedure to the specified environment:
sub bind_proc(\%$)
{
  my $V = { %{+shift} };
  my $f = shift;
  return sub { $f->($V, @_); };
}

my $V = {};

$V->{x} = new_scalar 2;
$V->{y} = new_scalar 1;

sub f3(\%$) {
  my $V = shift;
  my $z = $V->{z};                 # save existing z
  $V->{z} = new_scalar shift;      # create & initialize new z
  ${$V->{x}} = ${$V->{z}} + ${$V->{x}} + ${$V->{y}};
  $V->{z} = $z;                    # restore old z
}
sub f2(\%$$) {
  my $V = shift;
  my $p = shift;
  my $z = $V->{z};                 # save existing z
  $V->{z} = new_scalar shift;      # create & initialize new z
  my $x = $V->{x};                 # save existing x
  $V->{x} = new_scalar 5;          # create & initialize new x
  $p->(${$V->{z}});
  $V->{x} = $x;                    # restore old x
  $V->{z} = $z;                    # restore old z
}
sub f1(\%$) {
  my $V = shift;
  my $z = $V->{z};                 # save existing z
  $V->{z} = new_scalar shift;      # create & initialize new z
  my $y = $V->{y};                 # save existing y
  $V->{y} = new_scalar ${$V->{z}}; # create & initialize new y
  f2(%$V, bind_proc(%$V, \&f3), ${$V->{y}});
  $V->{y} = $y;                    # restore old y
  $V->{z} = $z;                    # restore old z
}
f1(%$V, 4);
print "${$V->{x}}\n";

__END__

I get 10 (which is 4 + 2 + 4).