Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value



Similar documents
Parameter passing in LISP

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 7: Functional Programming Languages

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Parameter Passing Methods

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Semester Review. CSC 301, Fall 2015

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

The C Programming Language course syllabus associate level

Concepts of Programming Languages: A Unified Approach. Karl Abrahamson

Semantic Analysis: Types and Type Checking

Crash Course in Java

Class 16: Function Parameters and Polymorphism

Lecture Data Types and Types of a Language

Lecture 1: Introduction

Computer Programming I

CSCI 3136 Principles of Programming Languages

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Habanero Extreme Scale Software Research Project

Introduction to Object-Oriented Programming

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Functional Programming in C++11

Programming Languages

Glossary of Object Oriented Terms

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Practical Generic Programming with OCaml

Functional programming languages

opalang - Rapid & Secure Web Development

What Is Recursion? Recursion. Binary search example postponed to end of lecture

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

: provid.ir

Java Memory Model: Content

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Analysis of Binary Search algorithm and Selection Sort algorithm

Chapter 5 Functions. Introducing Functions

Chapter 15 Functional Programming Languages

Fundamentals of Programming Languages

Functional Programming

CSE 307: Principles of Programming Languages

1 Operational Semantics for While

Address Taken FIAlias, which is equivalent to Steensgaard. CS553 Lecture Alias Analysis II 2

Charles Dierbach. Wiley

C++FA 3.1 OPTIMIZING C++

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

1.1 WHAT IS A PROGRAMMING LANGUAGE?

[Refer Slide Time: 05:10]

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Principles of Programming Languages Topic: Introduction Professor Louis Steinberg

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Parameter Passing in Pascal

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Curriculum Map. Discipline: Computer Science Course: C++

Programming Language Pragmatics

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Programming Languages

Lecture Notes on Linear Search

Threads Scheduling on Linux Operating Systems

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

C++ INTERVIEW QUESTIONS

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Announcements FORTRAN ALGOL COBOL. Simula & Smalltalk. Programming Languages

CMSC 202H. ArrayList, Multidimensional Arrays

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

C++FA 5.1 PRACTICE MID-TERM EXAM

CS 111 Classes I 1. Software Organization View to this point:

recursion, O(n), linked lists 6/14

Tail call elimination. Michel Schinz

Moving from CS 61A Scheme to CS 61B Java

The Needle Programming Language

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

On Understanding Types, Data Abstraction, and Polymorphism

Java Interview Questions and Answers

CS 241 Data Organization Coding Standards

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

An Incomplete C++ Primer. University of Wyoming MA 5310

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

ML for the Working Programmer

Transcription:

CMSC 33: Organization of Programming Languages Programming Language Features (cont.) Names & binding Namespaces Static (lexical) scopes Dynamic scopes Polymorphism Parametric Subtype Ad-hoc Parameter Passing Parameter passing Call by value Call by reference Call by name Eager vs. lazy evaluation Parallelism Multithreading Message passing CMSC 33 Parameter Passing in OCaml Call-by-Value Quiz: What value is bound to z? let z = add 3 4 let z = add (add 3 ) (add 4 ) let r = ref let add x y = (!r) + x + y let set_r () = r := 3; let z = add (set_r ()) 7 9 6 Actuals evaluated before call In call-by-value (cbv), arguments to functions are fully evaluated before the function is invoked Also in OCaml, in let x = e in e, the expression e is fully evaluated before e is evaluated C, C++, and Java also use call-by-value int r = ; int add(int x, int y) { return r + x + y; int set_r(void) { r = 3; return ; add(set_r(), ); CMSC 33 3 CMSC 33 4 Call-by-Value in Imperative Languages In C, C++, and Java, call-by-value has another feature What does this program print? Call-by-Value (cont.) Actual parameter is copied to stack location of formal parameter x x 3 Cbv protects function arguments against modifications CMSC 33 5 CMSC 33 6

Call-by-Reference Alternative idea Implicitly pass a pointer or reference to actual parameter If the function writes to it the actual parameter is modified x 3 x Call-by-Reference (cont.) Advantages Allows multiple return values Avoid copying entire argument to called function More efficient when passing large (multi-word) arguments Can do this without explicit pointer manipulation Disadvantages More work to pass non-variable arguments Examples: constant, function result May be hard to tell if function modifies arguments Introduces aliasing CMSC 33 7 CMSC 33 8 Aliasing We say that two names are aliased if they refer to the same object in memory C examples (this is what makes optimizing C hard) int x; int *p, *q; /*Note that C uses pointers to simulate call by reference */ p = &x; /* *p and x are aliased */ q = p; /* *q, *p, and x are aliased */ struct list { int x; struct list *next; struct list *p, *q;... q = p; /* *q and *p are aliased */ /* so are p->x and q->x */ /* and p->next->x and q->next->x... */ CMSC 33 9 Call-by-Reference (cont.) Call-by-reference is still around (e.g., C++) // C++ void f(int& y) { y = ; // y = reference var // prints f(); // error Seems to be less popular in newer languages Older languages still use it Examples: Fortran, Ada, C with pointers Possible efficiency gains not worth the confusion The hardware is basically call-by-value Although call by reference is not hard to implement and there may be some support for it CMSC 33 Call-by-Value Discussion Cbv is standard for languages with side effects When we have side effects, we need to know the order in which things are evaluated Otherwise programs have unpredictable behavior Call-by-value specifies the order at function calls Call-by-reference can sometimes give different results Differences blurred for languages like Java Language is call by value But most parameters are object references anyway Still have aliasing, parameter modifications at object level Call-by-Name Call-by-name (cbn) First described in description of Algol (96) Generalization of Lambda expressions (to be discussed later) Idea: In a function: Let add x y = x+y add (a*b) (c*d) Example: add (a*b) (c*d) = (a*b) + (c*d) executed function Then each use of x and y in the function definition is just a literal substitution of the actual arguments, (a*b) and (c*d), respectively Implementation: Highly complex, inefficient, and provides little improvement over other mechanisms CMSC 33 CMSC 33

Call-by-Name (cont.) In call-by-name (cbn), arguments to functions are evaluated at the last possible moment, just before they're needed Haskell; cbn; arguments evaluated here let z = add (add 3 ) (add 4 ) OCaml; cbv; arguments evaluated here add x y = x + y z = add (add 3 ) (add 4 ) CMSC 33 3 Call-by-Name (cont.) What would be an example where this difference matters? let rec loop n = loop n let z = cond true 4 (loop ) OCaml; cbv; infinite recursion at call cond p x y = if p then x else y loop n = loop n z = cond True 4 (loop ) Haskell; cbn; never evaluated because parameter is never used CMSC 33 4 Call by Name Examples P(x) {x = x + x; What is: Y = ; P(Y); write(y) F(m) {m = m + ; return m; What is: int A[]; m = ; P(A[F(m)]) becomes P(A[F(m)]) becomes Y = Y+Y = 4 A[F(m)] = A[F(m)]+A[F(m)] A[m++] = A[m++]+A[m++] A[] = A[3]+A[4] CMSC 33 5 Call by Name Anomalies Write a function to exchange values of X and Y Usual way - swap(x,y) { t=x; x=y; y=t; Cannot do it with call by name! Reason Cannot handle both of following swap(a[m], m) swap(m, A[m]) One of these must fail swap(a[m], m) t = A[m] ; A[m] = m; m = t; swap(m, A[m]) t = m ; m = A[m]; A[m] = t; // fails! CMSC 33 6 Two Cool Things to Do with CBN CBN is also called lazy evaluation (CBV is also known as eager evaluation) Build control structures with functions Build infinite data structures integers n = n::(integers (n+)) take (integers ) (* infinite loop in cbv *) Simulating CBN with CBV Thunk A function with no arguments Algorithm. Replace arguments a a k by thunks t t k When called, t i evaluates and returns a i. Within body of the function Replace formal argument with thunk invocations let add x = x + in add (+3) let add x = x() + in add (fun () -> (+3)) CMSC 33 7 CMSC 33 8 3

Simulating CBN with CBV (cont.) let rec loop n = loop n let z = cond true 4 (loop ) becomes... Get st arg let cond p x y = if (p ()) then (x ()) else (y ()) let rec loop n = loop n (* didn t transform... *) let z = cond (fun () -> true) (fun () -> 4) (fun () -> loop ) Return nd arg Never invoked Parameters are now thunks CMSC 33 9 Three-Way Comparison Consider the following program under the three calling conventions For each, determine i's value and which a[i] (if any) is modified int i = ; int a[] = {,, ; i, a[], a[], a[]); CMSC 33 Example: Call-by-Value Example: Call-by-Reference int i = ; i a[] a[] a[] f g int i = ; i /g a[] a[] /f a[] int a[] = {,, ; i, a[], a[], a[]); 5 int a[] = {,, ; i, a[], a[], a[]); CMSC 33 CMSC 33 Example: Call-by-Name Other Calling Mechanisms int i = ; i++; a[i] = 5*i; int a[] = {,, ; i, a[], a[], a[]); i a[] a[] a[] The expression a[i] isn't evaluated until needed, in this case after i has changed. Call-by-result Actual argument passed by reference, but not initialized Written to in function body (and since passed by reference, affects actual argument) Call-by-value-result Actual argument copied in on call (like cbv) Mutated within function, but does not affect actual yet At end of function body, copied back out to actual These calling mechanisms didn't really catch on They can be confusing in cases Recent languages don t use them CMSC 33 3 CMSC 33 4 4

CBV versus CBN CBN is flexible- strictly more programs terminate E.g., where we might have an infinite loop with cbv, we might avoid it with cbn by waiting to evaluate Order of evaluation is really hard to see in CBN Call-by-name doesn't mix well with side effects (assignments, print statements, etc.) Call-by-name is more expensive since Functions have to be passed around If you use a parameter twice in a function body, its thunk (the unevaluated argument) will be called twice Haskell actually uses call-by-need (each formal parameter is evaluated only once, where it's first used in a function) CMSC 33 5 5