CSCI305.github.io

CSCI 305: Programming Languages

Scope Part I

Reading: Webster Ch. 10

Instructions

  1. Watch This Video - (11:21)
  2. Watch This Video - (15:02)
  3. Watch This Video - (16:47)
  4. Review the Lecture Slides
  5. Complete the Out of Class Exercise
  6. Check your learning
  7. Attend Class and complete the In Class Exercises
  8. Check your learning

Out of Class Exercise

Here is the example used to show primitive namespaces in Java:

class Reuse {
    Reuse Reuse(Reuse Reuse) {
        Reuse:
           for (;;) {
               if (Reuse.Reuse(Reuse) == Reuse)
                  break Reuse;
           }
           return Reuse;
    }
}

Copy it and then annotate it as follows:

Check Your Learning:

Solution:
Definition Binding
class Reuse Binding for the type Reuse
method Reuse() Binding for the method Reuse of type Reuse -> Reuse
parameter Reuse Binding for the parameter Reuse of type Reuse in the Method Reuse
label Reuse: Binding for the labeled block Reuse
Use of Reuse Binding Used
Reuse.Reuse bound to the method Reuse in the Class Reuse
(Reuse) bound to the parameter Reuse of the method Reuse in the class Reuse
== Reuse bound to the parameter Reuse of the method Reuse in the class Reuse
break Reuse bound to the Label Reuse
return Reuse bound to the parameter Reuse of the method Reuse in the class Reuse

In Class Exercises

Exercise

Here is the example used to show the difference between scoping with blocks and dynamic scoping:

fun g x =
   let
      val inc = 1;
      fun f y = y + inc;
      fun h z =
        let
          val inc = 2;
        in
          f z
        end;
   in
      h x
   end;

Copy it and then annotate it as follows:

Check Your Learning:

Solution:
/--------------------------------------------------------------------------\
|            /---------------------------------------------------------\ 0 |
|      fun g | x =                                                   1 |   |
| /----------/                                                         |   |
| |   /-------------------------------------------------------------\  |   |
| |   |   let                                                     2 |  |   |
| |   |      val inc = 1;                                           |  |   |
| |   |            /---------------------\                          |  |   |
| |   |      fun f | y = y + inc;      3 |                          |  |   |
| |   |            \---------------------/                          |  |   |
| |   |            /---------------------------------------------\  |  |   |
| |   |      fun h | z =                                       4 |  |  |   |
| |   | /----------/                                             |  |  |   |
| |   | |  /-------------------------------------------------\   |  |  |   |
| |   | |  |   let                                         5 |   |  |  |   |
| |   | |  |     val inc = 2;                                |   |  |  |   |
| |   | |  |   in                                            |   |  |  |   |
| |   | |  |     f z                                         |   |  |  |   |
| |   | |  |   end;                                          |   |  |  |   |
| |   | |  \-------------------------------------------------/   |  |  |   |
| |   | \--------------------------------------------------------/  |  |   |
| |   |   in                                                        |  |   |
| |   |      h x                                                    |  |   |
| |   |   end;                                                      |  |   |
| |   \-------------------------------------------------------------/  |   |
| \--------------------------------------------------------------------/   |
\--------------------------------------------------------------------------/
Definition Scope   Name Binding
g {0,1,2,3,4,5}   y in y + inc y:{3}
x {1,2,3,4}   inc in y + inc inc:{2,3,4}
inc {2,3,4}   f in f z f:{2,3,4,5}
f {2,3,4,5}   z in f z z:{4,5}
y {3}   h in h x h:{2,3,4,5}
h {2,4,5,3}   x in h x x:{1,2,3,4}
z {4,5}      
inc {5}