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 my
s to local
s 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
).