CS 314 Principles of Programming Languages

Similar documents
CS 314 Principles of Programming Languages. Lecture 13

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

NOTE: Answer ANY FOUR of the following 6 sections:

CS 415 Midterm Exam Spring 2002

Functional Programming. Big Picture. Design of Programming Languages

CS 242. Fundamentals. Reading: See last slide

SOFTWARE ARCHITECTURE 6. LISP

G Programming Languages - Fall 2012

Organization of Programming Languages CS3200/5200N. Lecture 11

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

CS 314 Principles of Programming Languages. Lecture 16

Weeks 6&7: Procedures and Parameter Passing

FP Foundations, Scheme

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

CMSC 331 Final Exam Section 0201 December 18, 2000

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Functional Programming

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

Chapter 5. Names, Bindings, and Scopes

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

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

Functional Programming Languages (FPL)

MIDTERM EXAM (Solutions)

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CS 314 Principles of Programming Languages

MIDTERM EXAMINATION - CS130 - Spring 2005

Chapter 15. Functional Programming Languages

Imperative languages

Concepts Introduced in Chapter 7

Functional Languages. Hwansoo Han

Functional Programming Lecture 1: Introduction

15. Functional Programming

Programming Languages, Summary CSC419; Odelia Schwartz

Programmiersprachen (Programming Languages)

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

CS 415 Midterm Exam Spring SOLUTION

CS558 Programming Languages

Functional Programming. Pure Functional Programming

LECTURE 17. Expressions and Assignment

LECTURE 16. Functional Programming

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

CS558 Programming Languages

CS 314 Principles of Programming Languages

Equivalent Notations. Higher-Order Functions. (define (f x y) ( body )) = (define f (lambda (x y) ) ) Anonymous Functions.

CS 314 Principles of Programming Languages. Lecture 11

Names, Bindings, Scopes

Programming Methodology

Programming Languages

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Lecture08: Scope and Lexical Address

Functional Programming

Chapter 9 Subprograms

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Concepts of Programming Languages

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Chap. 8 :: Subroutines and Control Abstraction

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

Control in Sequential Languages

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

CS558 Programming Languages

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

G Programming Languages - Fall 2012

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Final-Term Papers Solved MCQS with Reference

Com S 541. Programming Languages I

Chapter 11 :: Functional Languages

Chapter 9 :: Subroutines and Control Abstraction

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

CS 345. Functions. Vitaly Shmatikov. slide 1

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

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

4/19/2018. Chapter 11 :: Functional Languages

Type Bindings. Static Type Binding

MIDTERM EXAMINATION - CS130 - Spring 2003

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

COP4020 Programming Assignment 1 - Spring 2011

COS 140: Foundations of Computer Science

Functional Programming

LECTURE 18. Control Flow

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Procedure and Object- Oriented Abstraction

Programming Languages

Introduction. A. Bellaachia Page: 1

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

Scheme: Expressions & Procedures

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Informatica 3 Syntax and Semantics

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

References and pointers

Concepts of Programming Languages

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

Transcription:

CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018

Class Information Midterm exam forum open in Sakai. HW4 and HW5 solutions posted in Sakai. This week s recitation is a review for midterm. Attance is encouraged! Reminder: midterm exam 3/26, in class, closed book, closed notes. 2

Review: Lexical Scoping Symbol table generated at compile time matches declarations and occurrences. Each name can be represented as a pair (nesting_level, local_index). Program x, y: integer // declarations of x and y Procedure B // declaration of B y, z: real // declaration of y and z y = x + z // occurrences of y, x, and z if () call B // occurrence of B Procedure C // declaration of C x: real call B // occurrence of B call C // occurrence of C call B // occurrence of B Program (1,1), (1,2): integer // declarations of x and y Procedure (1,3) // declaration of B (2,1), (2,2): real // declaration of y and z (2,1) = (1,1) + (2,2) // occurrences of y, x, and z if () call (1,3) // occurrence of B Procedure (1,4) // declaration of C (2,1): real call (1,3) // occurrence of B call (1,4) // occurrence of C call (1,3) // occurrence of B 3

Review: Access to Non-Local Data(Lexical Scoping) Low High What code do we need to generate for statement (*)? (2,1) = (1,1) + (2,2) Lexical Parent caller Level 1 local variables Current Saved in r0 caller local variables Level 2 > Current Level Assume field is 4 bytes. Each unit offset corresponds to 4 bytes. 4

Access to Non-Local Data(Lexical Scoping) Low High Current Saved in r0 What code do we need to generate for statement (*)? caller local variables caller local variables Level 1 (2,1) = (1,1) + (2,2) (1,1) LOADI r1, #4 LOADI r2, #-4 ADD r3 r0 r2 LOAD r4 r3 ADD r5 r4 r1 LOAD r6 r5 (2,2) LOADI r7 #8 ADD r8 r0 r7 LOAD r9 r8 + ADD r10 r6 r9 (2,1) LOADI r11 #4 ADD r12 r0 r11 = STORE r12 r10 Level 2 > Current Level 5 // offset of local variable (1,1) in frame // offset of in frame (bytes) // address of // get // address of local variable (1,1) in frame // get content of variable (1,1) // offset of local variable (2,2) in frame // address of local variable (2,2) // get content of variable (2, 2) // (1,1) + (2,2) // offset of local variable (2,1) in frame // address of local variable (2,1) // (2,1) = (1,1) + (2,2) Assume field is 4 bytes. Each unit offset corresponds to 4 bytes.

Access to Non-Local Data(Lexical Scoping) Low High Current Saved in r0 What code do we need to generate for statement (*)? caller local variables caller local variables Level 1 (2,1) = (1,1) + (2,2) (1,1) LOADI r1, #4 LOADI r2, #-4 ADD r3 r0 r2 LOAD r4 r3 ADD r5 r4 r1 LOAD r6 r5 (2,2) LOADI r7 #8 ADD r8 r0 r7 LOAD r9 r8 + ADD r10 r6 r9 (2,1) LOADI r11 #4 ADD r12 r0 r11 = STORE r12 r10 Level 2 > Current Level 6 // offset of local variable (1,1) in frame // offset of in frame (bytes) // address of // get // address of local variable (1,1) in frame // get content of variable (1,1) // offset of local variable (2,2) in frame // address of local variable (2,2) // get content of variable (2, 2) // (1,1) + (2,2) // offset of local variable (2,1) in frame // address of local variable (2,1) // (2,1) = (1,1) + (2,2) Assume field is 4 bytes. Each unit offset corresponds to 4 bytes.

Access to Non-Local Data(Lexical Scoping) Low High Current Saved in r0 What code do we need to generate for statement (*)? caller local variables caller local variables Level 1 (2,1) = (1,1) + (2,2) (1,1) LOADI r1, #4 LOADI r2, #-4 ADD r3 r0 r2 LOAD r4 r3 ADD r5 r4 r1 LOAD r6 r5 (2,2) LOADI r7 #8 ADD r8 r0 r7 LOAD r9 r8 + ADD r10 r6 r9 (2,1) LOADI r11 #4 ADD r12 r0 r11 = STORE r12 r10 Level 2 > Current Level 7 // offset of local variable (1,1) in frame // offset of in frame (bytes) // address of // get // address of local variable (1,1) in frame // get content of variable (1,1) // offset of local variable (2,2) in frame // address of local variable (2,2) // get content of variable (2, 2) // (1,1) + (2,2) // offset of local variable (2,1) in frame // address of local variable (2,1) // (2,1) = (1,1) + (2,2) Assume field is 4 bytes. Each unit offset corresponds to 4 bytes.

Access to Non-Local Data(Lexical Scoping) Low High Current Saved in r0 What code do we need to generate for statement (*)? caller local variables caller local variables Level 1 (2,1) = (1,1) + (2,2) (1,1) LOADI r1, #4 LOADI r2, #-4 ADD r3 r0 r2 LOAD r4 r3 ADD r5 r4 r1 LOAD r6 r5 (2,2) LOADI r7 #8 ADD r8 r0 r7 LOAD r9 r8 + ADD r10 r6 r9 (2,1) LOADI r11 #4 ADD r12 r0 r11 = STORE r12 r10 Level 2 > Current Level 8 // offset of local variable (1,1) in frame // offset of in frame (bytes) // address of // get // address of local variable (1,1) in frame // get content of variable (1,1) // offset of local variable (2,2) in frame // address of local variable (2,2) // get content of variable (2, 2) // (1,1) + (2,2) // offset of local variable (2,1) in frame // address of local variable (2,1) // (2,1) = (1,1) + (2,2) Assume field is 4 bytes. Each unit offset corresponds to 4 bytes.

Access to Non-Local Data(Lexical Scoping) Low High Saved in r4 Current Saved in r0 What code do we need to generate for statement (*)? caller local variables caller local variables Level 1 (2,1) = (1,1) + (2,2) (1,1) LOADI r1, #4 LOADI r2, #-4 ADD r3 r0 r2 LOAD r4 r3 ADD r5 r4 r1 LOAD r6 r5 (2,2) LOADI r7 #8 ADD r8 r0 r7 LOAD r9 r8 + ADD r10 r6 r9 (2,1) LOADI r11 #4 ADD r12 r0 r11 = STORE r12 r10 Level 2 > Current Level 9 // offset of local variable (1,1) in frame // offset of in frame (bytes) // address of // get, fp for frame at level 1 // address of local variable (1,1) in frame // get content of variable (1,1) // offset of local variable (2,2) in frame // address of local variable (2,2) // get content of variable (2, 2) // (1,1) + (2,2) // offset of local variable (2,1) in frame // address of local variable (2,1) // (2,1) = (1,1) + (2,2) Assume field is 4 bytes. Each unit offset corresponds to 4 bytes.

Access to Non-Local Data(Lexical Scoping) Low High Saved in r4 Current Saved in r0 What code do we need to generate for statement (*)? caller local variables caller local variables Level 1 (2,1) = (1,1) + (2,2) (1,1) LOADI r1, #4 LOADI r2, #-4 ADD r3 r0 r2 LOAD r4 r3 ADD r5 r4 r1 LOAD r6 r5 (2,2) LOADI r7 #8 ADD r8 r0 r7 LOAD r9 r8 + ADD r10 r6 r9 (2,1) LOADI r11 #4 ADD r12 r0 r11 = STORE r12 r10 Level 2 > Current Level 10 // offset of local variable (1,1) in frame // offset of in frame (bytes) // address of // get, fp for frame at level 1 // address of local variable (1,1) in frame // get content of variable (1,1) // offset of local variable (2,2) in frame // address of local variable (2,2) // get content of variable (2, 2) // (1,1) + (2,2) // offset of local variable (2,1) in frame // address of local variable (2,1) // (2,1) = (1,1) + (2,2) Assume field is 4 bytes. Each unit offset corresponds to 4 bytes.

Access to Non-Local Data(Lexical Scoping) Low High Saved in r4 Current Saved in r0 What code do we need to generate for statement (*)? caller local variables caller local variables Level 1 (2,1) = (1,1) + (2,2) (1,1) LOADI r1, #4 LOADI r2, #-4 ADD r3 r0 r2 LOAD r4 r3 ADD r5 r4 r1 LOAD r6 r5 (2,2) LOADI r7 #8 ADD r8 r0 r7 LOAD r9 r8 + ADD r10 r6 r9 (2,1) LOADI r11 #4 ADD r12 r0 r11 = STORE r12 r10 Level 2 > Current Level 11 // offset of local variable (1,1) in frame // offset of in frame (bytes) // address of // get, fp for frame at level 1 // address of local variable (1,1) in frame // get content of variable (1,1) // offset of local variable (2,2) in frame // address of local variable (2,2) // get content of variable (2, 2) // (1,1) + (2,2) // offset of local variable (2,1) in frame // address of local variable (2,1) // (2,1) = (1,1) + (2,2) Assume field is 4 bytes. Each unit offset corresponds to 4 bytes.

Procedures Modularize program structure - Actual : information passed from caller to callee (Argument) Appears in caller code. - Formal : local variable whose value (usually) is received from caller Appears in callee declaration Procedure declaration - Procedure names, formal s, procedure body with formal local declarations and statement lists, optional result type Example: void translate(point *p, int dx) 12

Parameters Parameter Association Positional association: Arguments associated with formals one-by-one; Example: C, Pascal, Java, Scheme Keyword association: formal/actual pairs; mix of positional and keyword possible; Example: Ada procedure plot(x, y: in real; z: in boolean) plot (0.0, 0.0, z true) plot (z true, x 0.0, y 0.0) Parameter Passing Modes Pass-by-value: C/C++, Pascal, Java/C# (value types), Scheme Pass-by-result: Ada, Algol W Pass-by-value-result: Ada, Swift Pass-by-reference: Fortran, Pascal, C++, Ruby, ML 13

Pass-by-value c: array[110] of integer; m, n: integer; procedure r(k, j: integer) k := k + 1; j := j + 2; r; m := 5; n := 3; r(m, n); write m,n; Output: 5 3 Advantage: Argument protected from changes in callee. Disadvantage: Copying of values takes execution time and space, especially for aggregate values (e.g.: structs, arrays) 14

Pass-by-reference c: array[110] of integer; m, n: integer; procedure r(k,j: integer) k := k + 1; j := j + 2; r; m := 5; n := 3; r(m, n); write m, n; Output: 6 5 Advantage: more efficient than copying Disadvantage: leads to aliasing, there are two or more names for the storage location; hard to track side effects 15

Aliasing Aliasing: More than two ways to name the same object within a scope Even without pointers, you can have aliasing through (global formal) and (formal formal) passing. j, k, m: integer; procedure r(a, b: integer) b := 3; m := m * a; q(m, k); q(j, j); write y; global/formal <m,a> ALIAS PAIR formal/formal <a,b> ALIAS PAIR 16

Pass-by-result c: array[110] of integer; m, n: integer; procedure r(k, j: integer) k := 1; j := 2; r; m := 5; n := 3; r(m, m); write m, n; Output: 1 or 2 for m? NOTE: CHANGE THE CALL Problem: order of copy back makes a difference; implementation depent. 17

Pass-by-value-result c: array[110] of integer; m, n: integer; procedure r(k, j: integer) k := k + 1; j := j + 2; r; m := 5; n := 3; r(m, n); write m, n; Output: 6 5 Problem: order of copy back makes a difference; implementation depent. 18

Pass-by-value-result c: array[110] of integer; m, n: integer; procedure r(k, j: integer) k := k + 1; j := j + 2; r; /* set c[m] = m */ m := 2; r(m,c[m]); write c[1], c[2], c[3], c[10]; Output: 1 4 3 4 5 10 on entry 1 2 4 4 5 10 on exit WHAT ELEMENT OF "c" IS ASSIGNED TO? Problem: When is the address computed for the copy-back operation? At procedure call (procedure entry), just before procedure exit, or somewhere in between? (Example: ADA on entry) 19

Pass-by-value-result c: array[110] of integer; m, n: integer; procedure r(k, j: integer) k := k + 1; j := j + 2; r; /* set c[m] = m */ m := 2; r(m,c[m]); write c[1], c[2], c[3], c[10]; Output: 1 4 3 4 5 10 on entry 1 2 4 4 5 10 on exit WHAT ELEMENT OF "c" IS ASSIGNED TO? Problem: When is the address computed for the copy-back operation? At procedure call (procedure entry), just before procedure exit, or somewhere in between? (Example: ADA on entry) 20

Pass-by-value-result c: array[110] of integer; m, n: integer; procedure r(k, j: integer) k := k + 1; j := j + 2; r; /* set c[m] = m */ m := 2; r(m,c[m]); write c[1], c[2], c[3], c[10]; Output: 1 4 3 4 5 10 on entry 1 2 4 4 5 10 on exit WHAT ELEMENT OF "c" IS ASSIGNED TO? Problem: When is the address computed for the copy-back operation? At procedure call (procedure entry), just before procedure exit, or somewhere in between? (Example: ADA on entry) 21

Pass-by-value-result c: array[110] of integer; m, n: integer; procedure r(k, j: integer) k := k + 1; j := j + 2; r; /* set c[m] = m */ m := 2; r(m,c[m]); WHAT ELEMENT OF "c" IS ASSIGNED TO? write c[1], c[2], c[3], c[10]; Output: 1 4 3 4 5 10 on entry Applicative order 1 2 4 4 5 10 on exit Normal order Problem: When is the address computed for the copy-back operation? At procedure call (procedure entry), just before procedure exit, or somewhere in between? (Example: ADA on entry) 22

Comparison: by-value-result vs. by-reference Actual s need to evaluate to L-values (addresses). y: integer; procedure p(x: integer) y := 2; p(y); write y; x := x + 1 x := x + y ref: x and y are ALIASED val-res: x and y are NOT ALIASED Output: pass-by-reference: 6 pass-by-value-result: 5 Note: by-value-result: Requires copying of values (expansive for aggregate values); does not have aliasing, but copy-back order depence. 23

Review: Computational Paradigms Imperative: Sequence of state-changing actions. Manipulate an abstract machine with: 1. Variables naming memory locations 2. Arithmetic and logical operations 3. Reference, evaluate, assign operations 4. Explicit control flow statements Fits the von Neumann architecture closely Key operations: Assignment and Control Flow 24

Review: Computation Paradigms Functional: Composition of operations on data. No named memory locations Value binding through passing Key operations: Function application and Function abstraction Basis in lambda calculus 25

Pure Functional Languages Fundamental concept: application of (mathematical) functions to values 1. Referential transparency: the value of a function application is indepent of the context in which it occurs value of foo(a, b, c) deps only on the values of foo, a, b and c it does not dep on the global state of the computation all vars in function must be local (or s) 2. The concept of assignment is NOT part of function programming no explicit assignment statements variables bound to values only through the association of actual s to formal s in function calls function calls have no side effects thus no need to consider global states 26

Pure Functional Languages 3. Control flow is governed by function calls and conditional expressions no iteration recursion is widely used 4. All storage management is implicit needs garbage collection 5. Functions are First Class Values can be returned as the value of an expression can be passed as an argument can be put in a data structure as a value (unnamed) function exists as a value 27

Pure Functional Languages A program includes: 1. A set of function definitions 2. An expression to be evaluated E.g. in scheme, > (define length (lambda (x) (if (null? x) 0 (+ 1 (length (rest x)))))) > (length '(A LIST OF 5 THINGS)) 5 28

LISP Functional language developed by John McCarthy in the mid 50 s Semantics based on Lambda Calculus All functions operate on lists or symbols called: S-expression Only five basic functions: list functions con, car, cdr, equal, atom, and one conditional construct: cond Useful for list-processing applications Program and data have the same syntactic form S-expression Originally used in Artificial Intelligence 29

SCHEME Developed in 1975 by G. Sussman and G. Steele A dialect of LISP Simple syntax, small language Closer to initial semantics of LISP as compared to COMMON LISP Provide basic list processing tools Allows functions to be first class objects 30

Next Lecture Things to do: Read Scott, Chapter 9.1-9.3 (4th Edition) or Chapter 8.1-8.3 (3rd Edition), Chapter 11.1-11.3 (4th Edition) or Chapter 10.1-10.3 (3rd Edition) 31