cs784(tk)1 semantics of procedures and scopes. kinds of scope static or lexical scope –determined...

18
cs784(TK) 1 Semantics of Procedures and Scopes

Upload: britney-eaton

Post on 16-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 1

Semantics of Procedures and Scopes

Page 2: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

Kinds of Scope

• Static or Lexical scope– determined by structure of program– Scheme, C++, Java, and many compiled languages

• Dynamic scope– determined by path of execution– Lisp dialects, Perl, and many interpreted languages

• Global Scope– File scope

• Local Scope– Block

• Body of a procedure• Body of a loop

• Scope alters the meaning

cs784(pm) 2

Page 3: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 3

proc main()int x := 5; proc q() { x := x + 1;}

proc r() {int x := 0;

q(); }{ r(); print(x); }.

Scoping : Free (non-local) Variables• Static scoping

x -> xoutput: 6

• Dynamic scoping

x -> xoutput: 5

Page 4: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 4

let x = 15in let f = proc () x in ( let x = 8 in (f) ) + (f)

Static Scoping x -> x 30Dynamic Scoping x -> x 23 x -> x

Page 5: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

Informal Semantics of Procedures

• Procedure Definition– Store formal parameters and body

• Procedure Invocation– Evaluate body in an environment that binds

formals to actual argument values• Interpretation of free-variables

– Use env. at proc. creation (static scoping)– Use env. at proc. call (dynamic scoping)

cs784(TK) 5

Page 6: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

Procedures: Syntaxes

• Concrete Syntax<expression> ::= …

| ( <expression> + )

| proc <idlist> <expression>

<idlist> ::= ()

| ( <identifier> {, <identifier>}* )• Abstract Syntaxproc-exp (ids body)

app-exp (rator rands)

cs784(TK) 6

Page 7: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 7

Encoding Procedures• Static scoping

(define-datatype procval procval? (closure

(ids (list-of symbol?) (body expression?) (env environment?)))

Closure retains the bindings of the free variables at procedure creation time.

• Dynamic scoping(define-datatype procval procval? (procv

(ids (list-of symbol?) (body expression?)))

Page 8: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 8

Processing Procedure Definitions

• Static scoping(define (eval-expression exp env) (cases expression exp (proc-exp (ids body) (closure ids body env)) ))

• Dynamic scoping(define (eval-expression exp env) (cases expression exp (proc-exp (ids body)

(procv ids body)) ))

Page 9: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 9

Processing Procedure Invocations: • Static scoping(define (eval-expression exp env) (cases expression exp

. . . (app-exp (rator rands) (let ((proc (eval-expression rator env)) (args (eval-rands rands env))) (if (procval? proc) (apply-procval proc args) (eopl:error 'eval-expression “Applying non-procedure ~s" proc) )))

Page 10: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 10

Processing Procedure Invocations• Dynamic scoping(define (eval-expression exp env) (cases expression exp (app-exp (rator rands) . . . (let ((proc (eval-expression rator env)) (args (eval-rands rands env))) (if (procval? proc) (apply-procval proc args env) (eopl:error 'eval-expression "Applying non-procedure ~s" proc) )))

Page 11: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 11

Processing Procedure Invocations• Static scoping(define (apply-procval proc args) (cases procval proc (closure (ids body s-env) (eval-expression body (extend-env ids args s-env)) )))

• Dynamic scoping (define (apply-procval proc args c-env) (cases procval proc (procv (ids body) (eval-expression body (extend-env ids args c-env)) )))

Page 12: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 12

Processing Procedure Invocations

>(run "let x = 1 inlet f = proc () x in(f)")

1>(run "let x = 1 in

let f = proc (x) x in (f 2)")2• Static scoping>(run "let x = 1 in

let f = proc () xx = 5in (f)")

1• Dynamic scoping>(run "let x = 1 in

let f = proc () x x = 5in (f)")

5

Page 13: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 13

Alternative Description: Static Scoping

(define (closure ids body env) (lambda (args) (eval-expression body (extend-env ids args env)) ))(define (apply-procval proc args) (proc args))

Page 14: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

Implementing Dynamic Scoping

• The value of variable x is the last value bound to x. => stack discipline• Deep binding

• Use a global stack for all variables• Shallow binding

• Use a separate stack for each variable• Implementing recursion is trivial.• Meaning of a call depends on the context.cs784(TK) 14

Page 15: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

Lexical Scope Pros and Cons

• Scope of names is known to the compiler.• Permits type checking by compiler• Easily check for uninitialized variables• Easier to analyze program correctness• Recursion is harder to implement

cs784(pm) 15

Page 16: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

Dynamic Scope Pros and Cons

• Meaning of variables known only at run-time.– Cannot perform type checking before execution– Programs are flexible, but harder to understand

• Easy to implement recursion• Renaming in the caller can effect the semantics of a

program– Locality of formals destroyed.– if renamed parameter now captures a free variable in the called

function. – Recall the interpretation of a free variable in a function body can

change based on the context (environment) of a call.

cs784(pm) 16

Page 17: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

Application of Dynamic Scoping

• Exception Handling

– provide a separate construct in statically scoped

language

• Setting Local Formatting Parameters

• Input-Output Redirection

– avoids passing parameters explicitly through

“intermediate” procedures cs784(TK) 17

Page 18: Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many

cs784(TK) 18

Difference between Scheme and ML> (define x 1)> (define (f) x)> (f)1> (define x 2)> (f)2> (define (g) x)> x2> (g)2

- val x = 1;- fun f () = x;val f = fn : unit -> int- f (); val it = 1 : int- val x = 2;- f () ;val it = 1 : int- fun g () = x;val g = fn : unit -> int

- g (); val it = 2: int