Abstraction. Markus Roggenbach. 4. März 2004

Similar documents
CS 345. Functions. Vitaly Shmatikov. slide 1

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

Programming Languages: Lecture 11

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

References and pointers

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

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

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Programmiersprachen (Programming Languages)

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

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

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

Types and Type Inference

Chapter 5 Names, Binding, Type Checking and Scopes

Types and Type Inference

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Implementing Subprograms

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

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

IA010: Principles of Programming Languages

Chapter 9 Subprograms

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Functional Languages and Higher-Order Functions

9. Subprograms. 9.2 Fundamentals of Subprograms

Functional Programming. Big Picture. Design of Programming Languages

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

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

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

CSE 307: Principles of Programming Languages

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

NOTE: Answer ANY FOUR of the following 6 sections:

G Programming Languages - Fall 2012

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

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

G Programming Languages - Fall 2012

CS 314 Principles of Programming Languages

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

22c:111 Programming Language Concepts. Fall Types I

Lectures Basics in Procedural Programming: Machinery

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

Lambda Calculus. Concepts in Programming Languages Recitation 6:

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

Functional Programming and Haskell

Names, Scopes, and Bindings II. Hwansoo Han

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

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

Binding and Variables

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

CS558 Programming Languages

CHAPTER 4 FUNCTIONS. 4.1 Introduction

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

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

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

PROCEDURES, METHODS AND FUNCTIONS

Assigning to a Variable

Run-time Environments - 2

Principles of Programming Languages

Chapter 9 Subprograms

Types, Type Inference and Unification

Chapter 10. Implementing Subprograms ISBN

Informatica 3 Syntax and Semantics

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

Last class. CS Principles of Programming Languages. Introduction. Outline

Lecture #23: Conversion and Type Inference

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

Type Bindings. Static Type Binding

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

Principles of Programming Languages

Types. What is a type?

Chapter 10. Implementing Subprograms

Chapter7 Expression and Assignment Statement. Introduction

Software II: Principles of Programming Languages. Why Expressions?

Chapter 6 Single-dimensional Arrays

Lecture 12: Data Types (and Some Leftover ML)

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

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

Fundamentals of Programming Languages

Intermediate Code Generation Part II

LECTURE 18. Control Flow

Chapter 7 Expressions and Assignment statements

Polymorphism and Type Inference

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Functions and Recursion

Chapter 5 Names, Bindings, Type Checking, and Scopes

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Programming Languages: Lecture 12

Principles of Programming Languages 2017W, Functional Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

JVM ByteCode Interpreter

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

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

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

Topic 8: Lazy Evaluation

Transcription:

Abstraction Markus Roggenbach 4. März 2004

Preliminary remark

Preliminary remark 2 Aspects of Programming Languages Value Expression State(transformation) Command Context(transformation) Declaration

Preliminary remark 3 Idea behind abstraction Parametrise expression function abstraction

Preliminary remark 3 Idea behind abstraction Parametrise expression function abstraction command procedure abstraction

Preliminary remark 3 Idea behind abstraction Parametrise expression function abstraction command procedure abstraction declaration generic abstraction

Preliminary remark 4 Motivations language extension

Preliminary remark 4 Motivations language extension structuring (in the small)

Preliminary remark 4 Motivations language extension structuring (in the small) distinguish: effect versus implementation

Preliminary remark 5 Abstractions I: Function abstraction A Function abstraction abstracts of an expression. function F (FP 1;...; FP n): T = B body: (yields) expression. function call (yields a value): expression.

Abstractions I: Function abstraction 6 Example function power (x:real;n:integer):real; begin if n=1 then power:=x else power:=x*power(x,n-1) end

Abstractions I: Function abstraction 7 Properties of function abstractions A function abstraction is side-effect free, if it does not change the global state of a program.

Abstractions I: Function abstraction 7 Properties of function abstractions A function abstraction is side-effect free, if it does not change the global state of a program. referentially transparent, if its value (with identical actual parameters) is independent of the program context.

Abstractions I: Function abstraction 8 Pascal An example Haskell var x: Char; x = newioref::io(ioref Char) hugo:: Int -> IOInt function hugo (y:int): Int; hugo (y::int) = begin x:= c ; writeioref x c >> hugo:= 2 * y return 2 * y end; erna :: Int -> Int erna (y::int) = 2 * y Standard function of IORef: >> :: IO alpha -> IO beta -> IO beta

Preliminary remark 9 Abstractions II: procedure abstraction A procedure abstraction abstracts of a command. procedure P (FP 1;... ; FP n) = B Body: Command Procedure call (changes the state): Command.

Abstractions II: procedure abstraction 10 Example in Ada procedure swap (a: in out integer, b: in out integer) is temp : integer begin temp:=a; a := b; b := temp end swap

Preliminary remark 11 Abstractions III: Generic Abstraction A generic abstraction abstracts of a declaration. generic FP 1;... ; FP n package P is B Body: Declaration. generic instantiation (yields context): Declaration.

Abstractions III: Generic Abstraction 12 Example in Ada generic capacity: in Positive; package queue_class is procedure append (newitem: in Character); procedure remove (olditem: out Character); end queue_class; package body queue_class is items: array (1..capacity) of Character;... end queue_class; package line_buffer is new queue_class (120);

Preliminary remark 13 Abstraction in general to abstract = to forget about

Preliminary remark 13 Abstraction in general to abstract = to forget about in programming languages: 1. (parametrised) combination of elements of one aspect to a new element of this aspect. 2. Binding of the abstraction (to a name).

Abstraction in general 14 Abstractions principle It is possible to construct abstractions over any syntactic class.

Abstraction in general 14 Abstractions principle It is possible to construct abstractions over any syntactic class. (provided only that the phrases of that class specify some kind of computation. )

Abstraction in general 14 Abstractions principle It is possible to construct abstractions over any syntactic class. (provided only that the phrases of that class specify some kind of computation. ) Example: variable access.

Preliminary remark 15 Parameter mechanism declaration : function F (FP 1;...;FP n) T = B call: F (AP 1;...;AP n) declaration : procedure P (FP 1;...;FP n) = B call: P (AP 1;...;AP n) declaration : generic FP 1;...;FP n package P is B call: package X is new P(AP 1;...;AP n) FP i: formal parameter AP i: actual parameter

Parameter mechanism 16 Assignment: actual formal using positions: AP i is assigned to FP i.

Parameter mechanism 16 Assignment: actual formal using positions: AP i is assigned to FP i. using names: names of the formal parameters.

Parameter mechanism 16 Assignment: actual formal using positions: AP i is assigned to FP i. using names: names of the formal parameters. function ComputePay (Income: Flaot; TaxRate: Float) return Float; pay := ComputePay (TaxRate => 0.15, Income => 20000.0)

Parameter mechanism 17 Mode versus implementation Mode: in out in-out

Parameter mechanism 17 Mode versus implementation Mode: in out in-out Implementation: copy mechanism definitional mechanism

Parameter mechanism 18 Copy mechanism assignment: AP:=FP.

Parameter mechanism 18 Copy mechanism assignment: AP:=FP. Mode actual parameter entry exit in expression FP:=AP out variable AP:=FP in-out variable FP:=AP AP:=FP FP is a local variable of the abstraction.

Parameter mechanism 19 value: in result: out value result: in-out Example in Pseudo-Pascal type Vector= array [1..n] of Real; procedure add (value v,w: Vector; result: sum: Vector) var i: 1..n; begin for i:=1 to n do sum[i]:=v[i]+w[i] end;

Parameter mechanism 20 procedure normalise (value result u:vector) var i: 1..n; s: Real begin s:= 0.0 for i:=1 to n do s:= s + sqr(u[i]); s:= sqr(s); for i:=1 to n do u[i] := u[i] / s; end;

Parameter mechanism 21 Definitional mechanism Binding of the FPs to the APs.

Parameter mechanism 21 constant parameter: value Definitional mechanism Binding of the FPs to the APs.

Parameter mechanism 21 Definitional mechanism Binding of the FPs to the APs. constant parameter: value variable parameter: reference to a variable

Parameter mechanism 21 Definitional mechanism Binding of the FPs to the APs. constant parameter: value variable parameter: reference to a variable procedural parameter: procedure abstraction

Parameter mechanism 21 Definitional mechanism Binding of the FPs to the APs. constant parameter: value variable parameter: reference to a variable procedural parameter: procedure abstraction functional parameter: function abstraction (the only mechanism available in ML)

Parameter mechanism 22 const: in var: in-out Example in Pseudo-Pascal type Vector= array [1..n] of Real; procedure add (const v,w: Vector; var: sum: Vector) var i: 1..n; begin for i:=1 to n do sum[i]:=v[i]+w[i] end;

Parameter mechanism 23 procedure normalise (var u:vector) var i: 1..n; s: Real begin s:= 0.0 for i:=1 to n do s:= s + sqr(u[i]); s:= sqr(s); for i:=1 to n do u[i] := u[i] / s; end;

Parameter mechanism 24 The correspondence principle For each form of declaration there exists a corresponding parameter mechanism, and vice-versa.

Parameter mechanism 25 Be careful with out/in-out parameters procedure foolish (out x, out y) x:=7; y:=9 end foolish(p,p)

Parameter mechanism 26 Alias problems with variable parameters direct problem: (in C) void fun (int *first, int *second) fun (&total, &total)

Parameter mechanism 26 Alias problems with variable parameters direct problem: (in C) void fun (int *first, int *second) fun (&total, &total) undecidable problem: (in C) void fun (int *first, int *second) fun (&list[i], &list[j])

Parameter mechanism 27 Formal parameter and non-local variable: var global: integer; procedure hugo(var local: integer); begin... end; begin hugo(global);... end

Parameter mechanism 28 Copy versus definitional mechanism criterion copy def. add. memory x time to copy x access fast slow types with assignment all security in concurrent progr. yes no

Parameter mechanism 29 Parameter mechanisms in programing languages language in out in-out in-out copy definitional call by value call by reference C x (pointer) Pascal x (var) Modula-2 x (var) Java x (object param.) Fortran: in-out Modus

Parameter mechanism 30 Type check Languages with type check Fortran 77, Pascal, Modula-2, Ada, Java, Fortran 90

Parameter mechanism 30 Type check Languages with type check Fortran 77, Pascal, Modula-2, Ada, Java, Fortran 90 Languages without type check C the original version ANSI-C: type check as option

Parameter mechanism 31 Type completeness principle No operation should be restricted in the types of its operands. Results of Function abstractions:

Parameter mechanism 31 Type completeness principle No operation should be restricted in the types of its operands. Results of Function abstractions: Fortran 77, Pascal, Modula-2: simple types.

Parameter mechanism 31 Type completeness principle No operation should be restricted in the types of its operands. Results of Function abstractions: Fortran 77, Pascal, Modula-2: simple types. C: all types, with the exception of array and function.

Parameter mechanism 31 Type completeness principle No operation should be restricted in the types of its operands. Results of Function abstractions: Fortran 77, Pascal, Modula-2: simple types. C: all types, with the exception of array and function. Ada: all types

Parameter mechanism 32 Application order strict (applicative, eager): 1) evaluation at call v 2) binding: F P v.

Parameter mechanism 32 Application order strict (applicative, eager): 1) evaluation at call v 2) binding: F P v. normal order: 1) binding: F P AP. 2) evaluation at each use of F P.

Parameter mechanism 33 lazy: 1) binding: F P AP. 2) evaluation at first use of F P : v 3) binding: F P v.

Parameter mechanism 34 Examples fun sqr (n: int) = n * n sqr(p+q) fun cand (b1:bool, b2:bool) = if b1 then b2 else false cand( n > 0, t/0 > 1)