EECS 662

Programming Languages

Index
Blog

Dynamic vs Static Scoping Test Case

As promised, here is your test case for comparing dynamic and static scoping. First, the version that uses bind before elaboration:

bind n = 5 in
  bind f = (lambda (x:Nat) in x+n) in
    bind n = 1 in
      app f 3"

When I parse this example I get the following structure:

Bind "n" (Num 5)
  (Bind "f" (Lambda "x" TNum (Plus (Id "x") (Id "n")))
     (Bind "n" (Num 1) (App (Id "f") (Num 3))))

When I elaborate that result I get the following structure:

App (Lambda "n" 
      (App (Lambda "f"
              (App (Lambda "n"
                      (App (Id "f") (Num 3)))
                   (Num 1)))
           (Lambda "x" (Plus (Id "x") (Id "n")))))
    (Num 5)

My statically scoped interpreter gives me 8 and my dynamically scoped interpreter gives me 4.

I’ve tried to get the indentation set up so both arguments to each app are lined up. Still, I suspect what you want is the concrete syntax for this expression:

(app (lambda n in
        (app (lambda f in
                (app (lambda n in
                        (app f 3))
                      1))
             (lambda x in x + n)))
      5)

If you’re using the string parser, you’ll need to get rid of the line breaks and turn the expression into a string using quotes.

For what it’s worth, I got this expression by writing an elaborator rather than trying to hand translate the bind expressions into app and lambda.