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

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

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

CS 345. Functions. Vitaly Shmatikov. slide 1

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

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Weeks 6&7: Procedures and Parameter Passing

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Control Flow. Stephen A. Edwards. Fall Columbia University

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

Binding and Variables

Chapter 5 Names, Binding, Type Checking and Scopes

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

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

PROCEDURES, METHODS AND FUNCTIONS

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

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

Types and Type Inference

G Programming Languages - Fall 2012

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

Types and Type Inference

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

Names, Scopes, and Bindings II. Hwansoo Han

IA010: Principles of Programming Languages

Chapter 9 Subprograms

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Programmiersprachen (Programming Languages)

CS558 Programming Languages

Chapter 6 Control Flow. June 9, 2015

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

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

References and pointers

CS558 Programming Languages

Principles of Programming Languages

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Functions - Lecture 7. Nested functions. Another example. A Nested Function. Josef Svenningsson

Types, Type Inference and Unification

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Informatica 3 Syntax and Semantics

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

CS558 Programming Languages

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

CSCI-GA Scripting Languages

CS 314 Principles of Programming Languages. Lecture 13

CS 314 Principles of Programming Languages

Programming Languages: Lecture 11

9. Subprograms. 9.2 Fundamentals of Subprograms

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Generic Programming. Akim D le Étienne Renault Roland Levillain

Chapter 5 Names, Bindings, Type Checking, and Scopes

Imperative Programming

Chapter 9. Subprograms

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

Memory Management and Run-Time Systems

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

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

22c:111 Programming Language Concepts. Fall Functions

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

Statement level control structures

Type Bindings. Static Type Binding

Introduction to the Tiger Project

4 Functional Programming Languages

Chapter 10. Implementing Subprograms ISBN

CS 415 Midterm Exam Spring 2002

CS558 Programming Languages

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

Names, Bindings, Scopes

Types. What is a type?

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

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Properties of an identifier (and the object it represents) may be set at

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

NOTE: Answer ANY FOUR of the following 6 sections:

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

CS 351 Design of Large Programs Programming Abstractions

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Abstraction. Markus Roggenbach. 4. März 2004

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

Special Topics: Programming Languages

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

Scope. Chapter Ten Modern Programming Languages 1

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

Parameters. However, one important class of variables is still missing Þ parameters. Terminology: Example: Java, C, C++ Chap 18.

multiple variables having the same value multiple variables having the same identifier multiple uses of the same variable

Chapter 5. Names, Bindings, and Scopes

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Review of the C Programming Language

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

CSc 520 Principles of Programming Languages

LECTURE 17. Expressions and Assignment

Lecture 12: Data Types (and Some Leftover ML)

Some instance messages and methods

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

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Transcription:

Subprograms Akim Demaille Étienne Renault Roland Levillain first.last@lrde.epita.fr EPITA École Pour l Informatique et les Techniques Avancées June 1, 2017

Subprograms 1 Routines 2 Argument passing 3 Functions 4 Functions as Values 5 Strictness A. Demaille, E. Renault, R. Levillain Subprograms 2 / 35

Subprograms At the origin, snippets copied and pasted from other sources [Wilson and Clark, 1993, Chap. 5]. Impact on memory management; Impact on separated compilation; Modular programming: first level of interface/abstraction. First impact on Software Engineering: top-down conception, by refinements. Generalizations: modules and/or objects. A. Demaille, E. Renault, R. Levillain Subprograms 3 / 35

Vocabulary Procedure Subprograms with no return value. Function Subprograms that return something. Distinction sometimes blurred by the language (e.g., using void ALGOL, C, Tiger...)). A. Demaille, E. Renault, R. Levillain Subprograms 4 / 35

Vocabulary Formal Argument Arguments of a subprogram declaration. let function sum (x : int, y : int) : int = x + y Effective Argument Arguments of a call to a subprogram. sum (40, 12) Please reserve parameter for templates. A. Demaille, E. Renault, R. Levillain Subprograms 5 / 35

Routines 1 Routines 2 Argument passing 3 Functions 4 Functions as Values 5 Strictness A. Demaille, E. Renault, R. Levillain Subprograms 6 / 35

Swap in Fortran PROGRAM SWAPPING INTEGER BIG, SMALL SUBROUTINE SWAP (I1, I2) INTEGER HEAD, TAIL INTEGER I1, I2, TMP... TMP = I1 CALL SWAP (BIG, SMALL) I1 = I2... I2 = TMP CALL SWAP (HEAD, TAIL) RETURN... END STOP END No type checking between formal and effective arguments. A. Demaille, E. Renault, R. Levillain Subprograms 7 / 35

Swap in C void swap (int *ip1, int *ip2) { int tmp = *ip1; *ip1 = *ip2; *ip2 = tmp; } Type matching checked since iso-c. int main (void) { int big, small, head, tail;... swap (&big, &small);... swap (&head, &tail);... return 0; } A. Demaille, E. Renault, R. Levillain Subprograms 8 / 35

Swap in Modula 2 MODULE swapping VAR big, small, head, tail: INTEGER; PROCEDURE SWAP (VAR i1, i2: INTEGER); VAR tmp: INTEGER; BEGIN tmp := i1; i1 := i2; i2 := tmp; END swap Embedded declaration of swap in swapping. BEGIN... swap (big, small);... swap (head, tail);... END swapping. A. Demaille, E. Renault, R. Levillain Subprograms 9 / 35

Swap in Ada procedure swap (i1, i2: in out integer) is tmp: INTEGER; tmp := i1; i1 := i2; i2 := tmp; end swap; Local declaration.... swap (big, small);... swap (head, tail);... end swapping; A. Demaille, E. Renault, R. Levillain Subprograms 10 / 35

Communication in Fortran PROGRAM MAIN SUBROUTINE A... SUBROUTINE B... COMMON X, Y, Z COMMON U, V, W COMMON E, F, G INTEGER R INTEGER S INTEGER T......... STOP RETURN RETURN END END END The COMMON part is shared, independently of the names types, and length of the COMMON declaration... Memory reduction (overlaying). Danger. Independent compilation ( separated). A. Demaille, E. Renault, R. Levillain Subprograms 11 / 35

Communication in ALGOLoids program main... var r, w: real; procedure a... var x, y: real; procedure b... var x, y: real;... end;... end; procedure c... var w, x: integer;... end; Block structure (ALGOL 60, Pascal, Modula 2, Ada, Tiger). Static scoping (contrary to Sh, Perl etc.). Declaration in blocks ( block structure): most languages (ALGOL 60, C, etc.) Non separated compilation, but typing. Closed scope: scope control (e.g., Euclid). Identifiers must be imported.... end. A. Demaille, E. Renault, R. Levillain Subprograms 12 / 35

Argument passing 1 Routines 2 Argument passing 3 Functions 4 Functions as Values 5 Strictness A. Demaille, E. Renault, R. Levillain Subprograms 13 / 35

Argument passing From a naive point of view, three possible modes: in, out, in-out. But there are different flavors. Val ValConst RefConst Res Ref ValRes ALGOL 60 * * Fortran?? PL/1?? ALGOL 68 * * Pascal * * C *?? Modula 2 *? Ada (simple types) * * * Ada (others)????? Alphard * * * Name A. Demaille, E. Renault, R. Levillain Subprograms 14 / 35

Argument passing from a subprogram By Result out in Ada, sort of a non initialized local variable. On return, the effective argument (an l-value) is updated. Assignments only: procedure negative_get (negative : out integer) is number : integer get (number); while number >= 0 do put_line ("Try again!"); get (number); end loop; negative := number; end negative_get; ALGOL W: lvalue evaluated on return, in Ada, on call. Problem: local copy. The compiler optimizes. A. Demaille, E. Renault, R. Levillain Subprograms 15 / 35

Passing from/to a subprogram By Value-Result in out in Ada, exact combination of in and out: local copy. By Reference Work directly onto the effective argument, via an indirection. Usually indistinguishable, except with synonymy (aliasing), and concurrent programming. Pascal forbids swap (foo, foo), but what about swap (foo[bar], foo[baz])... In Fortran, the lvalue may be a... simple rvalue... A. Demaille, E. Renault, R. Levillain Subprograms 16 / 35

An outsider: call by name In ALGOL 60 it behaves as a macro would, including with name captures: the argument is evaluated at each use. Try to write some code which results in a completely different result had SWAP been a function. #define SWAP(Foo, Bar) \ do { \ int tmp_ = (Foo); \ (Foo) = (Bar); \ (Bar) = tmp_; \ } while (0) In ALGOL 60, a compiled language, thunks were introduced: snippets of code that return the l-value when evaluated. A. Demaille, E. Renault, R. Levillain Subprograms 17 / 35

An application of call by name: Jensen s Device General computation of a sum of a series u k=l a k: real procedure Sum(k, l, u, ak) value l, u; integer k, l, u; real ak; comment k and ak are passed by name; real s; s := 0; for k := l step 1 until u do s := s + ak; Sum := s end; Computing the first 100 terms of a real array V[]: Sum(i, 1, 100, V[i]) A. Demaille, E. Renault, R. Levillain Subprograms 18 / 35

Exhibit the differences (Explicit lyrics... ) var t foo : integer : array [1..2] of integer; procedure shoot_my (x : Mode integer); foo[1] := 6; t := 2; x := x + 3; end; foo[1] := 1; foo[2] := 2; t := 1; shoot_my (foo[t]); end. A. Demaille, E. Renault, R. Levillain Subprograms 19 / 35

Exhibit the differences (Explicit lyrics... ) var t foo : integer : array [1..2] of integer; procedure shoot_my (x : Mode integer); foo[1] := 6; t := 2; x := x + 3; end; foo[1] := 1; foo[2] := 2; t := 1; shoot_my (foo[t]); end. Mode foo[1] foo[2] t Val A. Demaille, E. Renault, R. Levillain Subprograms 19 / 35

Exhibit the differences (Explicit lyrics... ) var t foo : integer : array [1..2] of integer; procedure shoot_my (x : Mode integer); foo[1] := 6; t := 2; x := x + 3; end; foo[1] := 1; foo[2] := 2; t := 1; shoot_my (foo[t]); end. Mode foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) A. Demaille, E. Renault, R. Levillain Subprograms 19 / 35

Exhibit the differences (Explicit lyrics... ) var t foo : integer : array [1..2] of integer; procedure shoot_my (x : Mode integer); foo[1] := 6; t := 2; x := x + 3; end; foo[1] := 1; foo[2] := 2; t := 1; shoot_my (foo[t]); end. Mode foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) 6 4 2 Val-Res (Ada) A. Demaille, E. Renault, R. Levillain Subprograms 19 / 35

Exhibit the differences (Explicit lyrics... ) var t foo : integer : array [1..2] of integer; procedure shoot_my (x : Mode integer); foo[1] := 6; t := 2; x := x + 3; end; foo[1] := 1; foo[2] := 2; t := 1; shoot_my (foo[t]); end. Mode foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) 6 4 2 Val-Res (Ada) 4 2 2 Ref A. Demaille, E. Renault, R. Levillain Subprograms 19 / 35

Exhibit the differences (Explicit lyrics... ) var t foo : integer : array [1..2] of integer; procedure shoot_my (x : Mode integer); foo[1] := 6; t := 2; x := x + 3; end; foo[1] := 1; foo[2] := 2; t := 1; shoot_my (foo[t]); end. Mode foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) 6 4 2 Val-Res (Ada) 4 2 2 Ref 9 2 2 Name A. Demaille, E. Renault, R. Levillain Subprograms 19 / 35

Exhibit the differences (Explicit lyrics... ) var t foo : integer : array [1..2] of integer; procedure shoot_my (x : Mode integer); foo[1] := 6; t := 2; x := x + 3; end; foo[1] := 1; foo[2] := 2; t := 1; shoot_my (foo[t]); end. Mode foo[1] foo[2] t Val 6 2 2 Val-Res (ALGOL W) 6 4 2 Val-Res (Ada) 4 2 2 Ref 9 2 2 Name 6 5 2 A. Demaille, E. Renault, R. Levillain Subprograms 19 / 35

Some sugar In Ada, named arguments and/or default values: -- Output a float with a specified precision. put (number : in float, before : in integer := 2, after : in integer := 2, exponent : in integer := 2)... put (pi, 1, 2, 3); put (pi, 1); put (pi, 2, 2, 4); put (pi, before => 2, after => 2, exponent => 4); put (pi, exponent => 4); end In Perl, use a hash as argument. A. Demaille, E. Renault, R. Levillain Subprograms 20 / 35

Functions 1 Routines 2 Argument passing 3 Functions 4 Functions as Values 5 Strictness A. Demaille, E. Renault, R. Levillain Subprograms 21 / 35

Functions Fortran INTEGER FUNCTION SUM(A, B) INTEGER A, B SUM = A + B RETURN END ALGOL 60 integer procedure sum(a, b); value a, b; integer a, b; sum := a + b; Pascal function sum (a, b: integer): integer; sum := a + b; end; A. Demaille, E. Renault, R. Levillain Subprograms 22 / 35

Functions Modula 2 PROCEDURE sum(a, b: INTEGER): INTEGER; BEGIN RETURN a + b; END sum Ada function sum(a, b: integer) return integer is return a + b; end sum A. Demaille, E. Renault, R. Levillain Subprograms 23 / 35

Functions: Side effects Using functions with side effects is very dangerous. For instance: foo = getc () + getc () * getc (); is undefined ( nondeterministic). On purpose! A. Demaille, E. Renault, R. Levillain Subprograms 24 / 35

Functions: Side effects Possible in Eiffel but strongly against its culture. feature -- To read one character at a time: read_character is -- Read a character and assign it to last_character. require is_connected not end_of_input deferred ensure not push_back_flag end last_character: CHARACTER is -- Last character read with read_character. require is_connected deferred end A. Demaille, E. Renault, R. Levillain Subprograms 25 / 35

Functions: Side effects In Ada (in) out are forbidden in functions. Globals are still there... A. Demaille, E. Renault, R. Levillain Subprograms 26 / 35

Functions as Values 1 Routines 2 Argument passing 3 Functions 4 Functions as Values 5 Strictness A. Demaille, E. Renault, R. Levillain Subprograms 27 / 35

Subprograms as arguments function diff (f(x: real): real, x, h: real) : real; if h = 0 then slope := 0 else slope := (f (x + h) - f (x)) / h; diff := slope end... diff (sin, 1, 0.01);... end Typing difficulties ignored in ALGOL 60, Fortran, original Pascal and C: the function-argument was not typed. Today function types are available in most languages (except in some ool). Doesn t exist in Ada. Simulated by a function parametrized routine. But you have to instantiate... A. Demaille, E. Renault, R. Levillain Subprograms 28 / 35

Anonymous subprograms In all the functional languages, but not only [Duret-Lutz and Tromey, 2003]... use Getopt::Long; Getopt::Long::config ("bundling", "pass_through"); Getopt::Long::GetOptions ( version => &version, help => &usage, libdir:s => $libdir, gnu => sub { set_strictness ( gnu ); }, gnits => sub { set_strictness ( gnits ); }, cygnus => $cygnus_mode, foreign => sub { set_strictness ( foreign ); }, include-deps => sub { $use_dependencies = 1; }, i ignore-deps => sub { $use_dependencies = 0; }, no-force => sub { $force_generation = 0; }, o output-dir:s => $output_directory, v verbose => $verbose, ) A. Demaille, E. Renault, R. Levillain Subprograms 29 / 35

Environment capture Functional languages with block structure. let type intfun = int -> int function add (n: int) : intfun = let function res (m: int): int = n + m in res end var addfive : intfun := add (5) var addten := add (10) var twenty := addten (addfive (5)) in twenty = 20 end Create closures: a pointer to the (runtime) environment in addition to a pointer to the code. Somewhat hard to implement [Appel, 1998, Chap. 15]. A. Demaille, E. Renault, R. Levillain Subprograms 30 / 35

Strictness 1 Routines 2 Argument passing 3 Functions 4 Functions as Values 5 Strictness A. Demaille, E. Renault, R. Levillain Subprograms 31 / 35

Call by name [Appel, 1998, Chap. 15] What if y = 0 in the following code? let function loop (z: int):int = if z > 0 then z else loop (z) function f (x: int):int = if y > 8 then x else -y in f (loop (y)) /* if y > 8 then loop (y) else -y? */ end Call by name Don t pass the evaluation of the expression, but a thunk computing it: let var a := 5 + 7 in a + 10 end ===> let function a () := 5 + 7 in a () + 10 end Call by need The thunk is evaluated once and only once. Add a memo field. A. Demaille, E. Renault, R. Levillain Subprograms 32 / 35

Lazy evaluation 1 [Hughes, 1989] easydiff f x h = (f (x + h) - f (x)) / h repeat f a = a : repeat f (f a) halve x = x / 2 differentiate h0 f x = map (easydiff f x) (repeat halve h0) within eps (a : b : rest) abs (b - a) <= eps = b otherwise = within eps (b : rest) relative eps (a : b : rest)) = abs (b - a) <= eps * abs b = b otherwise = relative eps (b : rest) within eps (differentiate h0 f x) Slow convergence... Suppose the existence of an error term: a (i) = A + B * (2 ** n) * (h ** n) a (i + 1) = A + B * (h ** n) A. Demaille, E. Renault, R. Levillain Subprograms 33 / 35

Lazy evaluation 2 [Hughes, 1989] elimerror n (cons a (cons b rest)) = = cons ((b * (2 ** n) - a) / (2 ** n - 1)) (elimerror n (cons b rest)) What is the value of n? order (cons a (cons b rest)) = = round (log2 ((a - c) / (b - c) - 1)) thus improve s = elimerror (order s) s within eps (improve (differentiate h0 f x)) and actually super s = map second (repeat improve s) second (cons a (cons b rest)) = b within eps (super (differentiate h0 f x)) A. Demaille, E. Renault, R. Levillain Subprograms 34 / 35

Bibliography I Appel, A. W. (1998). Modern Compiler Implementation in C, Java, ML. Cambridge University Press. Duret-Lutz, A. and Tromey, T. (2003). GNU Automake. http://www.gnu.org/software/automake/. Hughes, J. (1989). Why functional programming matters. The Computer Journal, 32(2):98 107. http://www.math.chalmers.se/~rjmh/papers/whyfp.html. Wilson, L. B. and Clark, R. G. (1993). Langages de Programmation Comparés. Addison-Wesley, 2nd edition. A. Demaille, E. Renault, R. Levillain Subprograms 35 / 35