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

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

Type Systems, Type Inference, and Polymorphism

Types and Type Inference

Some instance messages and methods

Types and Type Inference

Types, Type Inference and Unification

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

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Polymorphism and Type Inference

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

Polymorphism and Type Inference

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

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

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

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Lecture #23: Conversion and Type Inference

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

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

CS558 Programming Languages

COS 320. Compiling Techniques

Pierce Ch. 3, 8, 11, 15. Type Systems

Types and Classes. I II From predefined (simple) and user-defined (composite) types via Abstract data types

CS558 Programming Languages

Types. What is a type?

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CMSC 330: Organization of Programming Languages

Short Notes of CS201

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CS201 - Introduction to Programming Glossary By

CS558 Programming Languages

Type Checking and Type Inference

Programmiersprachen (Programming Languages)

C++ Important Questions with Answers

CSCE 314 Programming Languages. Type System

301AA - Advanced Programming [AP-2017]

Programming Languages

Lecture 12: Data Types (and Some Leftover ML)

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

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.

Imperative Programming

Lambda Calculi With Polymorphism

22c:111 Programming Language Concepts. Fall Types I

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Programming Languages

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

On Understanding Types, Data Abstraction, and Polymorphism

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

Object-oriented Programming. Object-oriented Programming

G Programming Languages - Fall 2012

301AA - Advanced Programming [AP-2017]

Why Types Matter. Zheng Gao CREST, UCL

Motivation for typed languages

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

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

Harvard School of Engineering and Applied Sciences Computer Science 152

Intermediate Code Generation

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Inheritance and object compatibility

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

Type Hierarchy. Lecture 6: OOP, autumn 2003

Static Checking and Type Systems

CS 32. Lecture 1: oops

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Lecture 13: Subtyping

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

Names, Scopes, and Bindings II. Hwansoo Han

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CSCI312 Principles of Programming Languages!

Object typing and subtypes

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CSCI-GA Final Exam

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Lambda Calculi With Polymorphism

Object Oriented Paradigm

G Programming Languages - Fall 2012

The role of semantic analysis in a compiler

Part III. Chapter 15: Subtyping

Introduction to Programming Using Java (98-388)

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Informatica 3 Syntax and Semantics

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Principles of Programming Languages

CSE 307: Principles of Programming Languages

Fundamentals of Programming Languages

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

CSCI-GA Scripting Languages

5. Syntax-Directed Definitions & Type Analysis

Transcription:

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

What is a Data Type? A type is a collection of computational entities that share some common property Programming languages are designed to help programmers organize computational constructs and use them correctly. Many programming languages organize data and computations into collections called types. Some examples of types are: o the type Int of integers o the type (Int Int) of functions from integers to integers

Why do we need them? Consider untyped universes: Bit string in computer memory λ-expressions in λ calculus Sets in set theory untyped = there s only 1 type Types arise naturally to categorize objects according to patterns of use E.g. all integer numbers have same set of applicable operations

Use of Types Identifying and preventing meaningless errors in the program o Compile-time checking o Run-time checking Program Organization and documentation o Separate types for separate concepts o Indicates intended use declared identifiers Supports Optimization o Short integers require fewer bits o Access record component by known offset

Type Errors A type error occurs when a computational entity, such as a function or a data value, is used in a manner that is inconsistent with the concept it represents Languages represent values as sequences of bits. A "type error" occurs when a bit sequence written for one type is used as a bit sequence for another type A simple example can be assigning a string to an integer or using addition to add an integer or a string

Type Systems A tractable syntactic framework for classifying phrases according to the kinds of values they compute By examining the flow of these values, a type system attempts to prove that no type errors can occur Seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation does not make sense

Type Safety A programming language is type safe if no program is allowed to violate its type distinctions Example of current languages: Not Safe : C and C++ Type casts, pointer arithmetic Almost Safe : Pascal Explicit deallocation; dangling pointers Safe : Lisp, Smalltalk, ML, Haskell, Java, Scala Complete type checking

Type Declarations Two basic kinds of type declaration: 1. transparent o meaning an alternative name is given to a type that can also be expressed without this name For example, in C, the statements, typedef char byte; typedef byte ten bytes[10]; the first, declaring a type byte that is equal to char and the second an array type ten bytes that is equal to arrays of 10 bytes

Type Declarations 2. Opaque Opaque, meaning a new type is introduced into the program that is not equal to any other type Example in C, typedef struct Node{ int val; struct Node *left; struct Node* right; }N;

Type Checking - Compile Time Check types at compile time, before a program is started In these languages, a program that violates a type constraint is not compiled and cannot be executed Expressiveness of the Compiler: a) sound If no programs with errors are considered correct b) conservative if some programs without errors are still considered to have errors (especially in the case of type-safe languages )

Type Checking - Run Time The compiler generates the code When an operation is performed, the code checks to make sure that the operands have the correct type Combining the Compile and Run time Most programming languages use some combination of compile-time and run-time type checking In Java, for example, static type checking is used to distinguish arrays from integers, but array bounds errors are checked at run time.

A Comparison Compile vs. Run Time Form of Type Checking Advantages Disadvantages Compile - Time May restrict Prevents type errors Eliminates run-time tests Finds type errors before execution and run-time tests programming because tests are conservative Run - Time Prevents type errors Need not be conservative Slows Program Execution

Type Inference Process of identifying the type of the expressions based on the type of the symbols that appear in them Similar to the concept of compile type checking o All information is not specified o Some degree of logical inference required Some languages that include Type Inference are Visual Basic (starting with version 9.0), C# (starting with version 3.0), Clean, Haskell, ML, OCaml, Scala This feature is also being planned and introduced for C++0x and Perl6

Type Inference Example: Compile Time checking: For language, C: int addone(int x) { int result; /*declare integer result (C language)*/ result = x+1; return result; } Lets look at the following example, addone(x) { val result; /*inferred-type result */ result = x+1; return result; }

Haskell Type Inference Algorithm There are three steps: 1. Assign a type to the expression and each subexpression. 2. Generate a set of constraints on types, using the parse tree of the expression. 3. Solve these constraints by means of unification, which is a substitution-base algorithm for solving systems of equations.

Explanation of the Algorithm Consider an example function: add x = 2 + x add :: Int Int Step 0: Construct a parse tree. Node 'Fun' represents function declaration. Children of Node 'Fun' are name of the function 'add', its argument and function body. The nodes labeled @ denote function applications, in which the left child is applied to the right child. Constant expressions like '+', '3' and variables also have their own node.

Explanation of the Algorithm Step 1: Assign a type variable to the expression and each subexpression. Each of the types, written t_i for some integer i,is a type variable, representing the eventual type of the associated expression.

Explanation of the Algorithm Step 2: Generate a set of constraints on types, using the parse tree of the expression. Constant Expression: we add a constraint equating the type variable of the node with the known type of the constant Variable will not introduce any type constraints Function Application (@ nodes): If the type of 'f' is t_f, the type of 'a' is t_a, and the type of 'f a' is t_r,then we must have t_f = t_a -> t_ r Function Definition: If 'f' is a function with argument 'x' and body 'b',then if 'f' has type 't_f', 'x' has type t_x, and 'b' has type 't_b', then these types must satisfy the constraint t_f=t_x -> t_b

Explanation of the Algorithm Set of Constraints generated: 1. t_0 = t_1 -> t_6 2. t_4 = t_1 -> t_6 3. t_2 = t_3 -> t_4 4. t_2 = Int -> (Int -> Int) 5. t_3 = Int

Explanation of the Algorithm Step 3: Solve the generated constraints using unification For Equations (3) and (4) to be true, it must be the case that t_3 -> t_4 = Int -> (Int -> Int), which implies that 6. t_3 = Int 7. t_4 = Int -> Int Equations (2) and (7) imply that 8. t_1 = Int 9. t_6 = Int

Result of the Algorithm Thus the system of equations that satisfy the assignment of all the variables: t_0 = Int -> Int t_1 = Int t_2 = Int -> Int -> Int t_3 = Int t _4 = Int -> Int t _6 = Int

POLYMORPHISM

Polymorphism Constructs that can take different forms poly = many morph = shape

Types of Polymorphism Ad-hoc polymorphism similar function implementations for different types (method overloading, but not only) Subtype (inclusion) polymorphism instances of different classes related by common super class class A {...} class B extends A {...}; class C extends A {...} Parametric polymorphism functions that work for different types of data def plus(x, y): return x + y

Ad-hoc Polymorphism int plus(int x, int y) { return x + y; } string plus(string x, string y) { return x + y; } float plusfloat(float x, float y) { return x + y; }

Subtype Polymorphism First introduced in the 60s with Simula Usually associated with OOP (in some circles, polymorphism = subtyping) Principle of safe substitution (Liskov substitution principle) if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of the program. Note that this is behavioral subtyping, stronger than simple functional subtyping.

Behavioral Subtyping Requirements Contravariance of method arguments in subtype (from narrower to wider, e.g. Triangle to Shape) Covariance of return types in subtype (from wider to narrower, e.g. Shape to Triangle) Preconditions cannot be strengthened in subtype Postcondition cannot be weakened in subtype History constraint: state changes in subtype not possible in supertype are not allowed (Liskov s constraint)

LSP Violations? class Thing {...} class Shape extends Thing { Shape m1(shape a) {...} } class Triangle extends Shape { @Override Triangle m1(shape a) {...} } class Square extends Shape { @Override Thing m1(shape a) {...} } Java does not support contravariance of method arguments

LSP Violations? class Thing {...} class Shape extends Thing { Shape m1(shape a) { assert(shape.color == Color.Red);... } } class Triangle extends Shape { @Override Triangle m1(shape a) { assert(shape.color == Color.Red); assert(shape.nsizes == 3);... } }

Parametric Polymorphism Parametric polymorphism functions that work for different types of data def plus(x, y): return x + y How to do this in statically-typed languages? int plus(int x, int y): return x + y???

Parametric Polymorphism Parametric polymorphism for statically-typed languages introduced in ML in the 70s aka generic functions C++: templates Java: generics C#, Haskell: parametric types

Parametric Polymorphism Explicit Parametric Polymorphism C++ implements explicit polymorphism, in which explicit instantiation or type application to indicate how type variables are replaced with specific types in the use of a polymorphic value. Example: template <typename T> T lessthan(t& x, T& y){ if( x < y) return x; else return y; } We define a template function with T as a parameter which can take any type as a parameter to the function.

Parametric Polymorphism Explicit Parametric Polymorphism Java example: /** * Generic version of the Box class. * @param <T> the type of value being boxed */ public class Box<T> { // T stands for "Type" private T t; public void add(t t) { this.t = t; } Box<Integer> integerbox;... void m(box<foo> fbox) {...} } public T get() { return t; }

Parametric Polymorphism Implicit parametric polymorphism Haskell uses polymorphic functions that do not need to contain types The inference algorithm computes when a function is polymorphic and computes the instantiation of type variables as needed Example: template <type t> insert(less :: (t, t) -> Bool, x :: t, [] :: [t]) = [x] insert(less, x, y:ys) = if less(x,y) then x:y:ys else y:insert(less,x,ys) Type Inference Algorithm can be simply considered as preprocessor that converts expression without type information into expression in intermediate language with templates

Video The structure of a compiler