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.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Run time vs. Compile time
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
EOPL3: Section 3.3 PROC and App B: SLLGEN
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Basic Semantics Associating meaning with language entities.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
The interpreter.
Introduction to Scheme
Subprograms The basic abstraction mechanism.
Corky Cartwright January 18, 2017
Implementing Recursion
Original material by Eric Grimson
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Abstract Syntax cs7100 (Prasad) L7AST.
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Assignments and Procs w/Params
CSE 341 Lecture 11 b closures; scoping rules
CSE S. Tanimoto Lambda Calculus
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Presentation transcript:

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

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 -> x output: 6 Dynamic scoping x -> x output: 5

cs784(TK)4 let x = 15 in let f = proc () x in ( let x = 8 in (f) ) + (f) Static Scoping x -> x 30 Dynamic Scoping x -> x 23 x -> x

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

Procedures: Syntaxes Concrete Syntax ::= … | ( + ) | proc ::= () | ( {, }* ) Abstract Syntax proc-exp (ids body) app-exp (rator rands) cs784(TK)6

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

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

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

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

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

cs784(TK)12 Processing Procedure Invocations >(run "let x = 1 in let 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 () x x = 5 in(f)") 1 Dynamic scoping >(run "let x = 1 in letf = proc () x x = 5 in(f)") 5

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

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

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

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

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

cs784(TK)18 Difference between Scheme and ML > (define x 1) > (define (f) x) > (f) 1 > (define x 2) > (f) 2 > (define (g) x) > x 2 > (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