Procedures - PowerPoint PPT Presentation

About This Presentation
Title:

Procedures

Description:

(ids (list-of symbol?) (body expression?) (env environment? ... (extend-env ids args s-env)) ))) Dynamic scoping (define (apply-procval proc args c-env) ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 18
Provided by: csWr
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Procedures


1
Procedures
2
  • Primitive procedures
  • , -, , etc
  • User-defined procedures
  • Naming a sequence of operations
  • triple proc (x) (x,(x,x))
  • Recursive definitions
  • exp proc (x, i)
  • if (i 0) then 1
  • else (x,exp(x,i-1))

3
User-defined procedures
  • Concrete Syntax
  • ltexpressiongt ( ltexpressiongt )
  • proc ltidlistgt ltexpressiongt
  • ltidlistgt ()
  • ( ltidentifiergt , ltidentifiergt )
  • Abstract Syntax
  • proc-exp (ids body)
  • app-exp (rator rands)
  • Examples
  • proc (y,z) (y,5)
  • (f x 5)

4
Scoping Free (non-local) Variables
  • proc main()
  • int x 5
  • proc q()
  • x x 1
  • proc r()
  • int x 0
  • q()
  • r()
  • print(x) .
  • Static scoping
  • x -gt x
  • output 6
  • Dynamic scoping
  • x -gt x
  • output 5

5
  • let x 15
  • in let f proc () x
  • in
  • ( let x 8
  • in (f)
  • ) (f)
  • Static Scoping x -gt x 30
  • Dynamic Scoping x -gt x 23
  • x -gt x

6
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)

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?)))

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)) ))

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)
  • (eoplerror 'eval-expression
  • Applying non-procedure s" proc)
  • )
  • ))

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)
  • (eoplerror 'eval-expression
  • "Applying non-procedure s" proc)
  • )
  • ))

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)) )))

12
Processing Procedure Invocations
  • Static scoping
  • gt(run "let x 1 in
  • let f proc () x in (f)")
  • 1
  • gt(run "let x 1 in
  • let f proc (x) x in (f 2)")
  • 2
  • gt(run "let x 1 in
  • let f proc () x x 5 in (f)")
  • 1
  • Dynamic scoping
  • gt(run "let x 1 in
  • let f proc () x x 5 in (f)")
  • 5

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)
  • )

14
Implementing Dynamic Scoping
  • The value of variable x is the last value
    bound to x. gt stack discipline
  • Deep binding
  • Use a global stack for all variables
  • Shallow binding
  • Use a separate stack for each variable
  • Implementing recursion is trivial.
  • First LISP evaluator used dynamic scoping by
    accident!!
  • Meaning of a call depends on the context.

15
Pros and cons of scoping rules
  • It is easy to implement recursion in a
    dynamically scoped language. Historically, LISP
    uses dynamic scoping.
  • In dynamic scoping, the interpretation of a free
    variable in a function body can change based on
    the context (environment) of a call. Renaming a
    formal parameter in the caller can effect the
    semantics of a program, if renamed parameter now
    captures a free variable in the called function.
    (Locality of formals destroyed.)

16
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

17
Difference between Scheme and ML
  • gt (define x 1)
  • gt (define (f) x)
  • gt (f)
  • 1
  • gt (define x 2)
  • gt (f)
  • 2
  • gt (define (g) x)
  • gt x
  • 2
  • gt (g)
  • 2
  • - val x 1
  • - fun f () x
  • val f fn unit -gt int
  • - f ()
  • val it 1 int
  • - val x 2
  • - f ()
  • val it 1 int
  • - fun g () x
  • val g fn unit -gt int
  • - g ()
  • val it 2 int
Write a Comment
User Comments (0)
About PowerShow.com