Slide 1 Vitaly Shmatikov CS 345 Types and Parametric Polymorphism.

Slides:



Advertisements
Similar presentations
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
Names and Bindings.
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Chapter 7:: Data Types Programming Language Pragmatics
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Type Checking.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Primitive Data Types: Numbers Strings Ordinal Types Pointers
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 5: Types Fall 2009 Marco Valtorta.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
C H A P T E R S E V E N Expressions and Assignment Statements.
Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: “Concepts in Programming Languages”, Revised Chapter 6 - handout.
INF Polymorphism and Type Inference Fatemeh Kazemeyni Department of Informatics – University of Oslo Based on John C. Mitchell’s.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Basic Semantics Associating meaning with language entities.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Programming Languages and Paradigms Imperative Programming.
Arithmetic Expressions
Expressions and Assignment Statements
Types John Mitchell CS 242 Reading: Chapter 6. Announcements uHomework 1 due today Turn in at the end of class, OR Turn in by 5PM to the homework drop.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
1 CS Programming Languages Class 09 September 21, 2000.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Object Lifetime and Pointers
Expressions and Assignment Statements
Data Types In Text: Chapter 6.
Type Checking and Type Inference
Principles of programming languages 12: Functional programming
Chapter 7: Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

slide 1 Vitaly Shmatikov CS 345 Types and Parametric Polymorphism

slide 2 Reading Assignment uMitchell, Chapter 6 uC Reference Manual, Chapters 5 and 6

slide 3 Type A type is a collection of computable values that share some structural property uExamples Integers Strings int  bool (int  int)  bool u“Non-examples”  3, true, x.x  Even integers  f:int  int | if x>3 then f(x) > x*(x+1)  Distinction between sets that are types and sets that are not types is language-dependent

slide 4 Uses for Types uProgram organization and documentation Separate types for separate concepts –Represent concepts from problem domain Indicate intended use of declared identifiers –Types can be checked, unlike program comments uIdentify and prevent errors Compile-time or run-time checking can prevent meaningless computations such as 3 + true - “Bill” uSupport optimization Example: short integers require fewer bits Access record component by known offset

slide 5 Operations on Typed Values uOften a type has operations defined on values of this type Integers: + - / * … Booleans:    … uSet of values is usually finite due to internal binary representation inside computer 32-bit integers in C: – to Addition and subtraction may overflow the finite range, so sometimes a + (b + c)  (a + b) + c Exceptions: unbounded fractions in Smalltalk, unbounded Integer type in Haskell Floating point problems

slide 6 Type Errors uMachine data carries no type information means… Floating point value 3.375? 32-bit integer 1,079,508,992? Two 16-bit integers and 0? Four ASCII X NUL NUL ? uA type error is any error that arises because an operation is attempted on a value of a data type for which this operation is undefined Historical note: in Fortran and Algol, all of the types were built in. If needed a type “color,” could use integers, but what does it mean to multiply two colors?

slide 7 Static vs. Dynamic Typing uType system imposes constraints on use of values Example: only numeric values can be used in addition Cannot be expressed syntactically in EBNF uLanguage can use static typing Types of all variables are fixed at compile time Example? u… or dynamic typing Type of variable can vary at run time depending on value assigned to this variable Example?

slide 8 Strong vs. Weak Typing uA language is strongly typed if its type system allows all type errors in a program to be detected either at compile time or at run time A strongly typed language can be either statically or dynamically typed! uUnion types are a hole in the type system of many languages (why?) uMost dynamically typed languages associate a type with each value

slide 9 Compile- vs. Run-Time Checking uType-checking can be done at compile time Examples: C, ML f(x) must have f : A  B and x : A u… or run time Examples: Perl, JavaScript uJava does both uBasic tradeoffs Both prevent type errors Run-time checking slows down execution Compile-time checking restricts program flexibility –JavaScript array: elements can have different types –ML list: all elements must have same type Which gives better programmer diagnostics?

slide 10 Expressiveness vs. Safety uIn JavaScript, we can write function like function f(x) { return x < 10 ? x : x(); } Some uses will produce type error, some will not uStatic typing always conservative if (big-hairy-boolean-expression) then f(5); else f(10); Cannot decide at compile time if run-time error will occur, so can’t define the above function

slide 11 Relative Type Safety of Languages uNot safe: BCPL family, including C and C++ Casts, pointer arithmetic uAlmost safe: Algol family, Pascal, Ada Dangling pointers. –Allocate a pointer p to an integer, deallocate the memory referenced by p, then later use the value pointed to by p –No language with explicit deallocation of memory is fully type-safe uSafe: Lisp, ML, Smalltalk, JavaScript, and Java Lisp, Smalltalk, JavaScript: dynamically typed ML, Java: statically typed

slide 12 Enumeration Types uUser-defined set of values enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; enum day myDay = Wednesday; In C/C++, values of enumeration types are represented as integers: 0,..., 6 uMore powerful in Java: for (day d : day.values()) System.out.println(d);

slide 13 Pointers uC, C++, Ada, Pascal uValue is a memory address Remember r-values and l-values? uAllows indirect referencing uPointers in C/C++ If T is a type and ref T is a pointer: & : T → ref T * : ref T → T *(&x) = x uExplicit access to memory via pointers can result in erroneous code and security vulnerabilities

slide 14 Arrays uExample: float x[3][5]; uIndexing [] Type signature: T[ ] x int → T In the above example, type of x: float[ ][ ], type of x[1]: float[ ], type of x[1][2]: float uEquivalence between arrays and pointers a = &a[0] If either e1 or e2 is type: ref T, then e1[e2] = *((e1) + (e2)) Example: a is float[ ] and i int, so a[i] = *(a + i)

slide 15 Strings uNow so fundamental, directly supported by languages uC: a string is a one-dimensional character array terminated by a NULL character (value = 0) uJava, Perl, Python: a string variable can hold an unbounded number of characters uLibraries of string operations and functions Standard C string libraries are unsafe!

slide 16 Structures uCollection of elements of different types Not in Fortran, Algol 60, used first in Cobol, PL/I Common to Pascal-like, C-like languages Omitted from Java as redundant struct employeeType { char name[25]; int age; float salary; }; struct employeeType employee;... employee.age = 45;

slide 17 Unions uunion in C, case-variant record in Pascal uIdea: multiple views of same storage type union = record case b : boolean of true : (i : integer); false : (r : real); end; var tagged : union; begin tagged := (b => false, r => 3.375); put(tagged.i); -- error

slide 18 Recursive Datatypes udata Value = IntValue Integer | FloatValue Float | BoolValue Bool | CharValue Char deriving (Eq, Ord, Show) udata Expression = Var Variable | Lit Value | Binary Op Expression Expression | Unary Op Expression deriving (Eq, Ord, Show) utype Variable = String utype Op = String utype State = [(Variable, Value)]

slide 19 Functions as Types uPascal example: function newton(a, b: real; function f: real): real; Declares that f returns a real value, but the arguments to f are unspecified uJava example: public interface RootSolvable {double valueAt(double x);} public double Newton(double a, double b, RootSolvable f);

slide 20 Type Equivalence uPascal Report: “The assignment statement serves to replace the current value of a variable with a new value specified as an expression... The variable (or the function) and the expression must be of identical type” uNowhere does it define identical type Which of the following types are equivalent? struct complex { float re, im; }; struct polar { float x, y; }; struct { float re, im; } a, b; struct complex c,d; struct polar e; int f[5], g[10];

slide 21 Subtypes uA subtype is a type that has certain constraints placed on its values or operations uCan be directly specified in some languages (Ada) subtype one_to_ten is Integer range ; uWill talk more about subtyping when talking about object-oriented programming

slide 22 Overloading uAn operator or function is overloaded when its meaning varies depending on the types of its operands or arguments or result uExamples: Addition: integers and floating-point values –Can be mixed: one operand an int, the other floating point –Also string concatenation in Java Class PrintStream in Java: print, println defined for boolean, char, int, long, float, double, char[ ], String, Object

slide 23 Function Overloading in C++ uFunctions that have the same name but can take arguments of different types Tells compiler (not preprocessor) to substitute the code of the function at the point of invocation Saves the overhead of a procedure call Preserves scope and type rules as if a function call was made

slide 24 Overloading Infix Operators in C++ Cannot change position, associativity or precedence

slide 25 Operator Overloading in ML uML infers which function to use from the type of the operands

slide 26 User-Defined Infix Operators in ML Precedence is specified by integer values 0-9 –0 = lowest precedence; left associativity (or else use infixr) –nonfix turns infix function into a binary prefix function

slide 27 Polymorphism and Generics uAn operator or function is polymorphic if it can be applied to any one of several related types Enables code re-use! uExample: generic functions in C++ Function operates in exactly the same way regardless of the type of its arguments For each use, compiler substitutes the actual type of the arguments for the ‘type’ template parameters This is an example of parametric polymorphism

slide 28 Polymorphism vs. Overloading uParametric polymorphism Single algorithm may be given many types Type variable may be replaced by any type f : t  t  f : int  int, f : bool  bool,... uOverloading A single symbol may refer to more than one algorithm Each algorithm may have different type Choice of algorithm determined by type context Types of symbol may be arbitrarily different + has types int*int  int, real*real  real Do you see the difference?

slide 29 Type Checking vs. Type Inference uStandard type checking int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2; }; Look at the body of each function and use declared types of identifiers to check agreement uType inference int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2; }; Look at the code without type information and figure out what types could have been declared ML is designed to make type inference tractable

slide 30 Motivation uTypes and type checking Type systems have improved steadily since Algol 60 Important for modularity, compilation, reliability uType inference Widely regarded as important language innovation ML type inference is an illustrative example of a flow-insensitive static analysis algorithm –What does this mean?

slide 31 ML Type Inference uExample - fun f(x) = 2+x; > val it = fn : int  int uHow does this work? + has two types: int*int  int, real*real  real 2 : int has only one type This implies + : int*int  int From context, need x: int Therefore f(x:int) = 2+x has type int  int Overloaded + is unusual. Most ML symbols have unique type. In many cases, unique type may be polymorphic.

slide 32 How Does This Work? uExample - fun f(x) = 2+x; > val it = fn : int  int uHow does this work? +2 Assign types to leaves : t int  int  int real  real  real : int Propagate to internal nodes and generate constraints int (t = int) int  int t  int Solve by substitution = int  int Graph for f(x) = 2+x

slide 33 Application and Abstraction uApplication f must have function type domain  range Domain of f must be type of argument x Result type is range of f uFunction expression Type is function type domain  range Domain is type of variable x Range is the type of function body e fx fun e : t: s : t : r (s = t  r): s  t

slide 34 Types with Type Variables uExample - fun f(g) = g(2); > val it = fn : (int  t)  t uHow does this work? 2 g Assign types to leaves : int: s Propagate to internal nodes and generate constraints t (s = int  t) stst Solve by substitution = (int  t)  t Graph for f(g) = g(2)

slide 35 Using a Polymorphic Function uFunction - fun f(g) = g(2); > val it = fn : (int  t)  t uPossible applications - fun add(x) = 2+x; > val it = fn : int  int - f(add); > val it = 4 : int - fun isEven(x) =...; > val it = fn : int  bool - f(isEven); > val it = true : bool

slide 36 Recognizing Type Errors uFunction - fun f(g) = g(2); > val it = fn : (int  t)  t uIncorrect use - fun not(x) = if x then false else true; > val it = fn : bool  bool - f(not); Type error: cannot make bool  bool = int  t

slide 37 Another Type Inference Example uFunction definition - fun f(g,x) = g(g(x)); > val it = fn : (t  t)*t  t uType inference Solve by substitution = (v  v)*v  v g g Assign types to leaves : t : s Propagate to internal nodes and generate constraints v (s = u  v) s*t  v u (s = t  u) Graph for f(g,x) = g(g(x))

slide 38 Polymorphic Datatypes uDatatype with type variable ’a is syntax for “type variable a” - datatype ‘a list = nil | cons of ‘a*(‘a list) > nil : ‘a list > cons : ‘a*(‘a list)  ‘a list u Polymorphic function - fun length nil = 0 | length (cons(x,rest)) = 1 + length(rest) > length : ‘a list  int uType inference Infer separate type for each clause Combine by making two types equal (if necessary)

slide 39 Type Inference with Recursion uSecond clause length(cons(x,rest)) = 1 + length(rest) uType inference Assign types to leaves, including function name Proceed as usual Add constraint that type of function body is equal to the type of function name rest : t fun ‘a list  int = t : ‘a*‘a list  ‘a list Tricky, isn’t it?

slide 40 Type Inference Summary uType of expression computed, not declared Does not require type declarations for variables Find most general type by solving constraints Leads to polymorphism uStatic type checking without type specifications Idea can be applied to other program properties uSometimes provides better error detection than type checking Type may indicate a programming error even if there is no type error (how?)

slide 41 Costs of Type Inference uMore difficult to identify program line that causes error uML requires different syntax for values of different types integer: 3, real: 3.0 uComplications with assignment took years to work out

slide 42 Information From Type Inference uAn interesting function on lists fun reverse (nil) = nil | reverse (x::lst) = reverse(lst); uMost general type reverse : ‘a list  ‘b list uWhat does this mean? Since reversing a list does not change its type, there must be an error in the definition of “reverse” See Koenig paper on course website

slide 43 Param. Polymorphism: ML vs. C++ uML polymorphic function Declaration has no type information Type inference: type expression with variables, then substitute for variables as needed uC++ function template Declaration gives type of function argument, result Place inside template to define type variables Function application: type checker does instantiation ML also has module system with explicit type parameters

slide 44 Example: Swap Two Values uML -fun swap(x,y) = let val z = !x in x := !y; y := z end; val swap = fn : 'a ref * 'a ref -> unit uC++ template void swap(T& x, T& y){ T tmp = x; x=y; y=tmp; } Declarations look similar, but compiled very differently

slide 45 Implementation uML Swap is compiled into one function Typechecker determines how function can be used uC++ Swap is compiled into linkable format Linker duplicates code for each type of use uWhy the difference? ML reference cell is passed by pointer, local x is a pointer to value on heap C++ arguments passed by reference (pointer), but local x is on stack, size depends on type

slide 46 Another Example uC++ polymorphic sort function template void sort( int count, T * A[count] ) { for (int i=0; i<count-1; i++) for (int j=i+1; j<count-1; j++) if (A[j] < A[i]) swap(A[i],A[j]); } uWhat parts of implementation depend on type? Indexing into array Meaning and implementation of <

slide 47 ML Overloading and Type Inference uSome predefined operators are overloaded uUser-defined functions must have unique type - fun plus(x,y) = x+y; This is compiled to int or real function, not both uWhy is a unique type needed? Need to compile code  need to know which + Efficiency of type inference Aside: general overloading is NP-complete

slide 48 Summary uTypes are important in modern languages Organize and document the program, prevent errors, provide important information to compiler uType inference Determine best type for an expression, based on known information about symbols in the expression uPolymorphism Single algorithm (function) can have many types uOverloading Symbol with multiple meanings, resolved when program is compiled