CSC 7101: Programming Language Structures 1

Similar documents
Semantic Analysis: Types and Type Checking

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Habanero Extreme Scale Software Research Project

On Understanding Types, Data Abstraction, and Polymorphism

Java Interview Questions and Answers

On Understanding Types, Data Abstraction, and Polymorphism

Chapter 5 Names, Bindings, Type Checking, and Scopes

CS143 Handout 18 Summer 2012 July 16 th, 2012 Semantic Analysis

History OOP languages Year Language 1967 Simula Smalltalk

Moving from CS 61A Scheme to CS 61B Java

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

Semester Review. CSC 301, Fall 2015

Class 16: Function Parameters and Polymorphism

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

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

AP Computer Science Java Subset

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

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Crash Course in Java

Programming Languages CIS 443

Journal of Functional Programming, volume 3 number 4, Dynamics in ML

Object Oriented Software Design

The C Programming Language course syllabus associate level

CS 106 Introduction to Computer Science I

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

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

Object Oriented Software Design

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

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

CSCI 3136 Principles of Programming Languages

C++ Crash Kurs. C++ Object-Oriented Programming

Programming Languages Featherweight Java David Walker

CSE 307: Principles of Programming Languages

Chapter 7: Functional Programming Languages

Inheritance in Programming Languages

Implementation Aspects of OO-Languages

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

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

Chapter 1 Fundamentals of Java Programming

Chapter 15 Functional Programming Languages

On the (un)suitability of Java to be the first programming language

Fundamentals of Programming Languages

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Glossary of Object Oriented Terms

C++FA 5.1 PRACTICE MID-TERM EXAM

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Inheritance, overloading and overriding

Multiple Dispatching. Alex Tritthart WS 12/13

Introduction to Data Structures

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

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

Lecture 9. Semantic Analysis Scoping and Symbol Table

Integrating Formal Models into the Programming Languages Course

Java (12 Weeks) Introduction to Java Programming Language

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

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

Lecture Data Types and Types of a Language

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

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Chapter 2: Elements of Java

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Type Classes with Functional Dependencies

A Static Analyzer for Large Safety-Critical Software. Considered Programs and Semantics. Automatic Program Verification by Abstract Interpretation

An Overview of Java. overview-1

Introduction to Java

Lecture 1: Introduction

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Java Types and Enums. Nathaniel Osgood MIT April 25, 2012

What is Java? Applications and Applets: Result of Sun s efforts to remedy bad software engineering practices

Qt Signals and Slots. Olivier Goffart. October 2013

Compile-time type versus run-time type. Consider the parameter to this function:

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Functional Programming

CS193j, Stanford Handout #10 OOP 3

Object Oriented Software Design II

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Programming Languages

The Cool Reference Manual

Java programming for C/C++ developers

The programming language C. sws1 1

Pemrograman Dasar. Basic Elements Of Java

Java Programming Fundamentals

CSC Software II: Principles of Programming Languages

1 The Java Virtual Machine

Chapter 2 Elementary Programming

The Java Virtual Machine Specification

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Programming in C# with Microsoft Visual Studio 2010

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Timber: A Programming Language for Real-Time Embedded Systems

Part I. Multiple Choice Questions (2 points each):

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Parameter passing in LISP

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

Sample CSE8A midterm Multiple Choice (circle one)

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Transcription:

Types in Programming Languages Stansifer Ch. 4 Cardelli Sec. 1 1 Types Organization of untyped values Untyped universes: bit strings, S-expr, Categorize based on usage and behavior Type = set of computational objects with uniform behavior Constraints to enforce correctness Check the applicability of operations Should not try to multiply two strings Should not use an integer as a pointer 2 Examples of Type Checking Built-in operators should get operands of correct types Type of left-hand side must agree with the value on the right-hand side Procedure calls: number and type of actual arguments Return type should match returned value 3 CSC 7101: Programming Language Structures 1

Static Typing Statically typed languages: expressions in the code have static types static type = claim about run-time values Types are either declared or inferred Examples: C, C++, Java, ML, Pascal, Modula-3 A statically typed language typically does some form of static type checking May also do dynamic type checking e.g. Java checks for array indices out of bounds and for null pointers 4 Dynamic Typing Dynamically-typed languages: expressions in the code do not have static types Examples: Lisp, Scheme, CLOS, Smalltalk, Perl, Python Dynamic type checking Before an operation is performed at run time Typical implementation: keep program values "tagged" with a type, and check the type tag before a value is used in an operation 5 Strongly vs. Weakly Typed Strongly typed languages: type-incorrect operations are not performed at run time Things cannot go wrong : no type errors Certain run-time errors are possible but clearly marked as such i.e. array index out of bounds, null pointer C/C++: weakly typed, Java: strongly typed Independent of static vs. dynamic Lisp is strongly, dynamically typed Forth is weakly, dynamically typed 6 CSC 7101: Programming Language Structures 2

Examples of Types Integers Arrays of Integers Pointers to Integers Records with fields int x and int y Objects of class C or a subclass of C Functions from any list to integers 7 Integers Types as Sets of Values Any number than can be represented in 32 bits in signed two s-complement 32 32 int = 2 K2 1 { } Class type Any object of class C or a subclass of C C = set of all instances of C or of a subclass of C Subtypes are subsets 8 Integers Types as Interfaces int = set of all objects to which one can apply integer addition, multiplication, etc. Class types C = set of all objects that understand the public methods of class C Types in Java or C++ Implementation types: integers, other base types, class types Interface types: abstract class types, interface types in Java 9 CSC 7101: Programming Language Structures 3

Greek: Monomorphism vs. Polymorphism mono = single poly = many morph = form Monomorphism every value belongs to exactly one type Polymorphism a value can belong to multiple types 10 Types of Polymorphism parametric universal inclusion polymorphism overloading ad hoc coercion 11 Coercion Values of one type are silently converted to another type e.g. addition: 3.0 + 4 : converts 4 to 4.0 int int int or real real real In a context where the type of an expression is not appropriate either an automatic coercion (conversion) to another type is performed automatically or if not possible: compile-time error 12 CSC 7101: Programming Language Structures 4

Widening Coercions coercing a value into a larger type e.g., int to float, subclass to superclass Narrowing coercing a value into a smaller type loses information, e.g., float to int PL/I: 1/3 + 25 has value 5.33333333333 13 Widening Primitive Conversions in Java Widening primitive conversions byte to short, int, long, float, or double short to int, long, float, or double char to int, long, float, or double int to long, float, or double long to float or double float to double integral type to integral type and float to double do not lose any information 14 Widening Primitive Conversions in Java Language Spec says Conversion of an int or long value to float, or of a long value to double, may result in loss of precision The result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode 15 CSC 7101: Programming Language Structures 5

Contexts for Widening Conversions Assignment conversion: when the value of an expression is assigned to a variable Method invocation conversion: applied to each argument value in a method or constructor invocation The type of the argument expression must be converted to the type of the corresponding formal parameter Casting conversion: applied to the operand of a cast operator: (float) 5 16 Contexts for Widening Conversions Numeric Promotion: converts operands of a numeric operator to a common type Example: binary numeric promotion e.g. +, -, *, etc. If either operand is double, the other is converted to double Otherwise, if either operand is of type float, the other is converted to float Otherwise, if either operand is of type long, the other is converted to long Otherwise, both are converted to type int 17 Narrowing Conversions Narrowing primitive conversions in Java e.g. long to byte, short, char, or int float to byte, short, char, int, or long double to byte, short, char, int, long, or float Examples of loss of information int to short loses high bits int not fitting in byte changes sign and magnitude double too small for float underflows to zero 18 CSC 7101: Programming Language Structures 6

Overloading Multiple definitions of the same name E.g. name + for several operations double double double (binary plus) float float float long long long int int int double double (unary plus) float float long long int int 19 Overloading Method selection at compile time class D extends C {... } int foo (C x) { return 0; } int foo (D x) { return 1; } C p = new D(); int i = foo(p); // executes C.foo 20 Multimethods Method selection at run time e.g. CLOS, Dylan, Cecil, Brew class D extends C {... } int foo (C x) { return 0; } int foo (D x) { return 1; } C p = new D(); int i = foo(p); // executes D.foo 21 CSC 7101: Programming Language Structures 7

Overloading vs. Overriding in Java class Point { int x = 0, y = 0; void move(int dx, int dy) { x += dx; y += dy; } } class RealPoint extends Point { float x = 0.0, y = 0.0; void move(int dx, int dy) { move((float)dx, (float)dy); } void move(float dx, float dy) { x += dx; y += dy; } } 22 Overloading vs. Overriding in Java public static void main(string[] args) { RealPoint rp = new RealPoint(); // compile-time resolution: the most specific // target method rp.move(1.5, 1.5); RealPoint.move(float,float) rp.move(2,2); RealPoint.move(int,int) Point p = rp; p.move(3,3); compile time: Point.move(int,int) run time: RealPoint.move(int,int) } 23 Overloading: Most Specific Method class Test { static void test(realpoint p, Point q) { } static void test(point p, RealPoint q) { } public static void main(string[] args) { RealPoint rp = new RealPoint(); test(rp,rp); // compile-time error } } 24 CSC 7101: Programming Language Structures 8

Parametric Polymorphism Idea: the same function can work on parameters of different types i.e. the function has multiple (function) types Example: identity function in ML fun id x = x; has types id : int int id : real real 25 Parametric Polymorphism Example: length function in ML fun length x = if null(x) then 0 else 1 + length (tl x); has types length : int list int length : real list int length : int list list int 26 Generics in Ada generic type T is private; function Id(X : in T) return T is begin return X; end; function IntId is new Id (INTEGER); function FloatId is new Id (FLOAT); 27 CSC 7101: Programming Language Structures 9

ML-Style Parametric Polymorphism Unlike with Ada generics, we do not instantiate with a specific type i.e. a ML polymorphic function is a real function, not a template for real functions Universally quantified types id : t. (t t) length : t. (t list int) ML syntax id : a a length : a list int 28 Mini-ML Stansifer, Sect 4.5.3 <expr> ::= <id> <int> <bool> <expr> <expr> (fn<id> => <expr> ) if (<expr>) then <expr> else <expr> <expr> <expr> is function application (fn ) is definition of an anonymous function (i.e., lambda expression) e.g. (fn x => x) is the anonymous identity fun (fn x => x) 5 evaluates to 5 29 Types in Mini-ML <type> ::= <type variable> int bool <type> <type> Goal: define the type of each expression An expression may have infinite # of types (fn x => x) has types int int, bool bool, and (int int) (int int), Solution: define a proof system Any type derivable ( provable ) in the proof system is a valid type for the expression 30 CSC 7101: Programming Language Structures 10

Typing Judgments Typing judgments: A e : t Just notation: remember <ae,σ> n from operational semantics? A is a type assignment set of pairs (identifier,type) Similar to state σ in operational semantics The judgment says: under type assignment A, expression e has type t 31 Typing Rules if A assigns a type to identifier x A x : A(x) A[x t1] e1 : t2 A (fn x => e1) : t1 t2 A[x t1] is {(x,t1)} {(y,a(y)) y x}; we used similar notation in operational semantics Example: because [x int] (fn x => x) : int int x : int 32 A e1 : t2 t A e1 e2 : t Typing Rules A e2 : t2 A e1 : bool A e2 : t A e3 : t A if e1 then e2 else e3 : t And of course A true : bool A false : bool A 0 : int A 1 : int A 2 : int 33 CSC 7101: Programming Language Structures 11

Example (fn x => if true then 0 else 1) :? Consider [x a] = {(x, a)} {(x, a)} if true then 0 else 1 : int (fn x => if true then 0 else 1) : a int Of course, we can also derive, for example, (fn x => if true then 0 else 1) : int int (fn x => if true then 0 else 1) : bool int (fn x => if true then 0 else 1): (int int) int 34 Summary of Type Rules If an expression does not have a type, there will be no derivations in the system Example: (fn x => x x); problem If more than one type can be derived for an expression: polymorphic expression If only one type can be derived, it is a monomorphic expression 35 Type Inference Given some expression, can we infer any type? the most general type? a -> a is more general than int -> int Milner, 1978: algorithm W Claim 1: if W(A,e) succeeds with t, then A e : t is derivable in the type system Claim 2: If A e : t is derivable in the type system, W(A,e) produces a type t2 such that t is an instance of t2 36 CSC 7101: Programming Language Structures 12

More Examples (fn x => if x then 4 else true) bad news (fn f => f 2) (int a) a (fn f => f 2) (fn x => x) type int (fn x => (fn y => x y)) ( a b) ( a b) 37 Pairs Addition to the language: <expr> ::= (<expr>, <expr>) Addition to the type system e.g. int*bool: pair of int + bool <type> ::= <type> * <type> Type rule A e1 : t1 A e2 : t2 A (e1, e2) : t1 * t2 38 let let <id> = <expr> 1 in <expr> 2 end e.g. let f = (fn x => x) in f 2 end Computationally equivalent to (fn <id> => <expr> 2 ) (<expr> 1 ) (fn f => f 2) (fn x => x) Type rule A e1 : t1 A[x t1] e2 : t2 A let id = e1 in e2 end : t2 39 CSC 7101: Programming Language Structures 13

Example let f = (fn x => x) in (f 2, f true) end the type is int*bool, and here is why: (fn x => x) : a a {(f, a a)} f 2 : int {(f, a a)} f true : bool {(f, a a)} (f 2, f true) : int*bool let end : int*bool 40 Different Typing (fn f => (f 2, f true)) (fn x => x) operationally equivalent to the last slide There is no type for this expression! Problem To infer a type, we need to infer a function type for (fn ) What is the domain of this function type? i.e., the type of f Convince yourself that it does not work 41 Inclusion Polymorphism Subtype relationships among types A computational object of a subtype may be used in any context that expects an object of a supertype Typical examples Imperative languages: record types Object-oriented languages: class types 42 CSC 7101: Programming Language Structures 14

Subtyping in Java Subtyping between class types class B { int foo () {... } } class C extends B { int foo () {... } } B p = new C(); int i = p.foo(); Interface types interface X { bool bar(); } class A implements X { bool bar() { } } X x = new A(); bool b = x.bar(); 43 Structural Subtyping in Amber Toy research language t1 t2 means t1 is a subtype of t2 Record types (similar to struct in C) {k: Int, l: String} Variant record types (similar to C unions) [k: Int, l: String, m: Bool] Function types {k: Int, l: String} {m: Int} 44 Structural Subtyping for Records Record types t1 and t2 t1 t2 iff for every field m:s2 in t2, there is a field m:s1 in t1 with s1 s2 {x: Int, y: String} {x: Int} Variant record types t1 and t2 t1 t2 iff for every field m:s1 in t1, there is a field m:s2 in t2 with s1 s2 [x:int] [x:int, y:string] 45 CSC 7101: Programming Language Structures 15

Subtyping for Functions Function types s1 t1 and s2 t2 s1 t1 s2 t2 iff s2 s1 and t1 t2 contravariant argument types covariant return types {a:int} {c:int,d:string} {a:int,b:string} {c:int} 46 CSC 7101: Programming Language Structures 16