Principles of Programming Languages

Similar documents
CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE 307: Principles of Programming Languages

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Assigning to a Variable

Parameter passing. Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure.

CS558 Programming Languages

UNIT V Sub u P b ro r g o r g a r m a s

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

procedure definition (or declaration - some make a distinction, we generally won t), e.g., void foo(int x) {... }

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

CS 345. Functions. Vitaly Shmatikov. slide 1

CSE 3302 Programming Languages Lecture 5: Control

CS 314 Principles of Programming Languages

CSE 504: Compilers. Expressions. R. Sekar

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

CS558 Programming Languages. Winter 2013 Lecture 4

CS 314 Principles of Programming Languages. Lecture 13

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Programmiersprachen (Programming Languages)

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Memory Management and Run-Time Systems

Weeks 6&7: Procedures and Parameter Passing

HANDLING NONLOCAL REFERENCES

Informatica 3 Marcello Restelli

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

CMSC 330: Organization of Programming Languages

Imperative Programming Languages (IPL)

Programming Languages

References and pointers

G Programming Languages - Fall 2012

11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

Other Features of Procedures

CS558 Programming Languages

Parameter Binding. Value: The formal parameter represents a local variable initialized to the value of the corresponding actual parameter.

Parameters. Chapter Eighteen Modern Programming Languages, 2nd ed. 1

Subprograms. Akim D le Étienne Renault Roland Levillain EPITA École Pour l Informatique et les Techniques Avancées

PROCEDURES, METHODS AND FUNCTIONS

Names, Scopes, and Bindings II. Hwansoo Han

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation

Principles of Programming Languages

Control Flow February 9, Lecture 7

CSE 307: Principles of Programming Languages

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Functional Programming. Pure Functional Programming

SOFTWARE ARCHITECTURE 6. LISP

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

Statement Basics. Assignment Statements

. p.1/23. Today. 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

CS 242. Fundamentals. Reading: See last slide

22c:111 Programming Language Concepts. Fall Types I

22c:111 Programming Language Concepts. Fall Functions

Expressions and Assignment

LECTURE 17. Expressions and Assignment

low and not larger than high. 18 points

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Chap. 8 :: Subroutines and Control Abstraction

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Types and Type Inference

Functional Programming. Big Picture. Design of Programming Languages

Module 27 Switch-case statements and Run-time storage management

Fundamentals of Programming Languages. Data Types Lecture 07 sl. dr. ing. Ciprian-Bogdan Chirila

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Types, Type Inference and Unification

Organization of Programming Languages CS 3200/5200N. Lecture 09

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

SYSC 2006 C Winter 2012

Special Topics: Programming Languages

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Abstraction. Markus Roggenbach. 4. März 2004

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Programming Language Concepts, Week #7

Concepts in Programming Languages

CSE 3302 Notes 7: Control Abstraction

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

TYPES, VALUES AND DECLARATIONS

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

Chapter 5. Names, Bindings, and Scopes

Control in Sequential Languages

G Programming Languages - Fall 2012

Code Generation & Parameter Passing

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

NOTE: Answer ANY FOUR of the following 6 sections:

INF5110 Compiler Construction. Spring 2017

Functional Languages. Hwansoo Han

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

Types and Type Inference

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

Chapter 5 Names, Binding, Type Checking and Scopes

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSCI312 Principles of Programming Languages!

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

known as non-void functions/methods in C/C++/Java called from within an expression.

Principles of Programming Languages

Programming Languages: Lecture 11

Transcription:

Principles of Programming Languages Lecture 06 Parameters 1

Parameter Passing Modes Definitional Modes (call time binding)! Call as constant CAC! Call by reference CBR Copying Modes (call time copying)! Call by value CBV! Call by copy CBC Copy-in/copy-out! Call by value-result CBVR Delay Modes (reference time copying)! Call by text CBT! Call by name CBN! Call by need R-value CBNeed-R L-Value CBNeed-L 2

Example Program { local i,j local a[1..12] proc P( X, Y ) { local j j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y } a[1]=1; a[2]=2; ; a[12]=12; i = 1 j = 3 P( i, a[i*j] ) print i print a[9] 3

Program Execution Sequence i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] // this j is local 4

Call by text (macrosubstitution) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] rewrite proc body X -> i Y -> a[i*j] j = 2 // j==2 i=i+1 // i==2 print i 2 i = i+2 //i==4 print a[i*j] //a[4*2]8 i--; print a[i*j] 6 print i 3 print a[9] 9 5

Call by name (copy rule) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] Rename locals j->j & rewrite X -> i Y -> a[i*j] j = 2 //j ==2 j==3 i=i+1 //i == 2 print i 2 i = i+2 //i==4 print a[i*j]//a[4*3] 12 i--; print a[i*j] 9! print i 3 print a[9] 9 6

Algol 60 Copy Rule 4.7.3.2. Name replacement (call by name) Any formal parameter not quoted in the value list is replaced, throughout the procedure body, by the corresponding actual parameter... Possible conflicts between identifiers inserted through this process and other identifiers already present within the procedure body will be avoided by suitable systematic changes of the formal or local identifiers involved... Finally the procedure body, modified as above, is inserted in place of the procedure statement [the call] and executed... Report on the Algorithmic Language ALGOL 60, CACM, May 1960 7

Algol 60 Copy Rule (cont.) integer procedure f(x); integer x; begin integer y; y := 1; f := x + y; end; begin integer y, w; y := 2; w := 10; w := f(y + w + 1); end; Copy rule begin integer y,w; y :=2; w := 10; begin integer z; z := 1; w := (y+w+1)+z; end; end; 8

Algol 60 Copy Rule (cont.): Thunks thunk: a 0-arg. Procedure encapsulating actual argument plus environment of caller, called at site of each formal. Used to implement CBN and delayed evaluation in general integer procedure f(x); integer x; begin integer y; y := 1; f := x + y; end; begin integer y, w; y := 2; w := 10; w := f(y + w + 1); end; int function f(int x); { int y; y = 1; f = *x() + y; } { int y,w; int& function x() {int t=y+w+1;return &t;} y = 2; w = 10; w = f(y+w+1); } 9

Call by name implemented by thunk i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] thunk: 0-ary function encapsulating argument text int& X(){return &i} int& Y(){return &a[i*j]} j = 2 // j ==2 j==3 i=i+1 // i == 2 print *X() 2 X()=*X()+2 //i==4 print *Y() //a[i*j]==a[4*3] 12 i--; print *Y() 9! print i 3 print a[9] 9 10

Call by reference i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] aliased lval(x)== lval(i) lval(y)== lval(a[1*3]) j = 2 //j ==2 j==3 i=i+1 //i== 2 print i 2 i = i+2 //i==4 print a[1*3] 3 i--; print a[1*3] 3 print i 3 print a[9] 9 11

Call by value (copy-in) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] X = i //X==1 Y = a[i*j])//y== a[1*3] j = 2 //j ==2 j==3 i=i+1 //i == 2 print X 1 X = X+2 //i==2 X==3 print Y 3 i--; print Y 3 print i 1 print a[9] 9 12

Call by value result (Algol W) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] X = i Y = a[i*j])//y==a[1*3] j = 2 // j ==2 j==3 i=i+1 // i == 2 print X 1 X = X+2 //i==2 X==3 print Y 3 i--; print Y 3 Use names of actuals for copy-out i = X //i==3 a[i*j] = Y //a[3*3]==3 print i 3 print a[9] 3 13

Call by copy (copy-in/copy-out) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] px=lvalue(i) X=*px py=lvalue(a[1*3]) Y=*py j = 2 // j ==2 j==3 i=i+1 // i == 2 print X 1 X = X+2 //i==2 X==3 print Y 3 i--; print Y 3 Use original lvals for copy-out *px = X //i==3 *py = Y //a[3]==3 print i 3 print a[9] 9 14

Call by need, r-value ( normal, lazy) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] Use thunk once on 1 st ref to assign local; then use local int& X(){return &i} int& Y(){return &a[i*j]} j = 2 // j ==2 j==3 i=i+1 // i == 2 print X = *X() 2 //X==2 X = X+2 //i==2 X==4 print Y = *Y() //Y==a[i*j]==a[2*3] 6 i--; print Y 6 print i 1 print a[9] 9 15

Call by need, l-value i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] At 1 st ref, alias actual lval to formal int& X(){return &i} int& Y(){return &a[i*j]} j = 2 // j ==2 j==3 i=i+1 // i == 2 lval(x)== X() //X aliases i print X //X==2 2 X = X+2 //i==4 X==4 lval(y)== Y()//Y aliases a[4*3] print Y //Y==a[12]} 12 i--; print Y 12 print i 3 print a[9] 9 16

Call by Name vs. Call by Need Assume X is the formal and e the corresponding actual expression CBN! Delays evaluation of arguments past call until a reference to the formal! Re-evaluates argument e on each reference to X in environment of caller! No local variable X is allocated! Implemented by call to thunk CB Need! Delays evaluation of arguments past call until a reference to the formal! Evaluates e on 1 st reference in environment of caller & loads local variable X; no re-evaluation: subsequent references use local X! Implemented by call to memoized thunk 17

Call by constant i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] const X = i const Y = a[1*3] j = 2 // j ==2 j==3 i=i+1 // i == 2 print X 1 X = X+2 error print Y (3) i--; print Y (3) print i (1) print a[9] (9) 18

Modes Differ Definitional Modes (call time binding) example! Call as constant CAC 1 error! Call by reference CBR 2 3 3 3 9 Copying Modes (call time passing)! Call by value CBV 1 3 3 1 9! Call by copy CBC 1 3 3 3 9 Copy-in/copy-out! Call by value-result CBVR 1 3 3 3 3 Delay Modes (reference time passing)! Call by text CBT 2 8 6 3 9! Call by name CBN 2 12 9 3 9! Call by need R-value CBNeed-R 2 6 6 1 9 L-Value CBNeed-L 2 12 12 3 9 19

Modes in Programming Languages Call as constant Algol 68, C/C++ const Call by reference Pascal var, Fortran, Cobol, PL/I, Ada arrays, C arrays Call by value Algol 60, Algol 68 (has ref x), Simula, C, C++ (has &x), Pascal, PL/I, APL, LISP, Snobol4, Ada (scalar in variables) Call by copy Ada (scalar out or in-out) Call by value-result Algol W Call by text LISP Macros Call by name Algol 60, Simula Call by need, R-value Haskell, Scheme (delay x) &(force x) Call by need, L-value imperative + lazy eval? 20

Ada Parameters Attempt at orthogonality of use vs implementation iplementationm copy ref copy-in copy-out copy-in-copy-out " " " CBC required in out in-out use scalar parameters implementation copy ref " " " CBC/CBR option " " " composite Parameters (arrays &c) in use out in-out 21

Arrays: by ref or by copy? a[] s elements void function P(int x[]) { x[i1] x[i2]... x[in] } n references to elements of formal x[] at RT (n known at RT) Call by reference: copy a pointer to a[] into AR for P(a[])! Each reference x[i] becomes indirect address through pointer to original argument a[] in caller; loads and stores reflected immediately in original array Call by copy: copy argument a[] into AR for P(a[])& overwrite original a[] from copy on return! Each reference x[i] directly addresses copy; stores and loads made to copy 22

by ref or copy? (cont.) Q calls P(a[]) by copy: Code of P(int x[]) P =x[i]... x[j]= n refs a[] call Q # of memory references at RT: return a[] s C = 4s + n 23

by ref or copy? (cont.) Q calls P(a[]) by reference: Code of P(int x[]) P @ @ =x[i]... x[j]= n refs call s Q a[] # of memory references at RT:! Assume a[0] is at known fp+offset at CT R = 1 + 2n 24

by ref or copy? (cont.) R > C 1 + 2n > 4s + n n > 4s - 1 n 4s s size R C s = n/4 R > C n references Reference density: n / s = ave. # references per element Copying less expensive than reference: R > C n / s 4 25

More on Call By Name Most curious mode ever invented. Pros and Cons. CBN s main characteristics:! Defers evaluation of actual argument expressions! Re-evaluates the argument expression at each use of formal # So arg value can actually change from one ref to another Can simulate lazy evaluation example: short circuit booleans boolean procedure cand(p,q); boolean p, q; if p then cand := q else cand := false; end Cannot be written as a procedure in an eager programming language (like Scheme) 26

CBN (cont.) Jensen s Device weird sort of polymorphism Example: real procedure sigma(x, j, n); value n; real x; integer j, n; begin real s; s := 0; for j := 1 step 1 until n do s := s + x; sigma := s end! sigma( a(i), i, 3) a(1) + a(2) + a(3)! sigma( a(k)*b(k), k, 2) a(1)*b(1) + a(2)*b(2) 27

CBN (cont.) Example: begin comment zero vector procedure zero(elt, j, lo, hi); value lo, hi; integer elt, j, lo, hi; begin for j := lo step 1 until hi do elt := 0; end integer i; integer array a[1:10], b[1:5, 1:5]; zero(a[i], i, 1, 10); zero(b[i, i], i, 1, 5); end 28

CBN (cont.) Limitations of CBN: the obvious swap algorithm fails begin procedure swap(a, b); integer a, b; begin integer t; t := b; b := a; a := t; end; integer array a[1:10]; integer i; i := 1; a[i] := 2; // i=1 a[1]=2 swap(a[i], i); //(a[1],i)=(2,1) end swap(a[i],i) //i==2 & //a[1]==2 t:= i // t==1 i:=a[i] // i==2 a[i]:=t // a[2]==1 //(a[1],i)==(2,2) Reordering code doesn t help; swap(i, a[i]) fails 29

CBN (cont.) Proposed solution : assume in t:=s that the target l-value is computed before the source r-value procedure swap(a, b); integer a, b; begin integer procedure testset(t,s); integer t,s; begin testset := t; //ret. old r-val of target t := s; //assign source r-val to target end; a := testset(b, a); end swap(a[i], i) a[i]:=testset(i,a[i]) ts:=i; i:=a[i]; a[i]:=ts 30

CBN (cont.) Problem: proposed solution has a bug.! Difficulty is the re-evaluation of actual on each use of formal! Can arrange an actual that yields a distinct l-value on each use begin integer array a[1:10]; integer j; integer procedure i; comment return next integer; begin j:= j + 1; i := j end; j := 0; swap(a[i], a[i]) end a[i] := testset(a[i], a[i]) ts :=a[i]; a[i]:=a[i]; a[i]:=ts; ts :=a[1]; a[2]:=a[3]; a[4]:=ts; 31