Motivation for typed languages

Similar documents
Chapter 7. Expressions and Assignment Statements ISBN

Expressions & Assignment Statements

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

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

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

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

Chapter 7. Expressions and Assignment Statements

Expressions and Assignment Statements

Intermediate Code Generation Part II

Software II: Principles of Programming Languages. Why Expressions?

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements

Polymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1

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

22c:111 Programming Language Concepts. Fall Types I

Expression Evaluation and Control Flow. Outline

Types. What is a type?

Chapter 7 Expressions and Assignment statements

int f(char a, char b) {

Semantic analysis. Semantic analysis. What we ll cover. Type checking. Symbol tables. Symbol table entries

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Compilers. Compiler Construction Tutorial The Front-end

Lecture 12: Data Types (and Some Leftover ML)

Introduction to Programming Using Java (98-388)

Concepts Introduced in Chapter 6

CSCI312 Principles of Programming Languages!

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.

Expressions and Assignment

Chapter7 Expression and Assignment Statement. Introduction

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

Lecture #23: Conversion and Type Inference

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

Concepts Introduced in Chapter 6

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

Type Checking and Type Inference

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

HANDLING NONLOCAL REFERENCES

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Subtyping. Lecture 13 CS 565 3/27/06

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Expressions and Casting

Programming Languages, Summary CSC419; Odelia Schwartz

Programming Languages and Compilers (CS 421)

LECTURE 18. Control Flow

SECTION II: LANGUAGE BASICS

Type systems. Static typing

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

1.3b Type Conversion

More Programming Constructs -- Introduction

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

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

TYPES, VALUES AND DECLARATIONS

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

Intermediate Code Generation

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CSc 520. Principles of Programming Languages 25: Types Introduction

Structure of Programming Languages Lecture 10

Programmiersprachen (Programming Languages)

Imperative Programming

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Static Checking and Type Systems

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Number Representation & Conversion

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

The SPL Programming Language Reference Manual

Java Fall 2018 Margaret Reid-Miller

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

CS558 Programming Languages

CSE 431S Type Checking. Washington University Spring 2013

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS558 Programming Languages

Lecture Set 4: More About Methods and More About Operators

Type Checking. Error Checking

A Simple Syntax-Directed Translator

Primitive Data Types: Intro

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

Type Bindings. Static Type Binding

CSCE 314 Programming Languages. Type System

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Structures, Operators

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Types and Type Inference

LECTURE 17. Expressions and Assignment

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

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Q1. Multiple Choice Questions

Transcription:

Motivation for typed languages Untyped Languages: perform any operation on any data. Example: Assembly movi 5 r0 // Move integer 5 (some representation) to r0 addf 3.6 r0 // Treat bit representation in r0 as a // Floating point representation and add 3.6 // to it. Result? You can be sure that r0 does not contain 8.6! (+) Flexibility : I can do anything I want to ( ) Ease of Error Checking. (programs are prone to errors, especially huge ones).

Motivation for typed languages Typed Languages: A type represents a set of values. Programs / procedures / operators are functions from an input type to an output type. Type Checking is the activity of ensuring that the operands / arguments of an operator / procedure are of compatible type through the use of a set of rules for associating a type with every expression in the language. (These rules are known as the type system). A type error results when an operator is applied to an operand of inappropriate/incompatible type. Output of a type system: There are type-errors (wrt type system) => Program is NOT type-safe. There are no type-errors (wrt type system) => Program is type-safe.

How to type-check? Definition: Type statements are of the form: <expr> : <type> meaning that an expression <expr> is-of-thetype (the : symbol) <type>. Examples: 3 : int 3+4 : int 3.14 : real abc : String while (x < 5) {x++;} : Stmt

How to type-check? Definition: Type rules are of the form: e 1 : t 1 e 2 : t 2 e n : t n f e 1 e 2 e n : t (rule name) where each e i : t i is a type statement, n 0. The rule is interpreted as IF e 1 is of type t 1 and and e n is of type t n THEN f e 1 e 2 e n is of type t.

How to type-check? Examples of type rules: Rule for constants: 1 : int 2 : int 3 : int Rule for addition: E 1 : int E 2 : int E 1 + E 2 : int Rule for boolean comparison: E 1 : int E 2 : int E 1 == E 2 : bool (+) (==)

How to type-check? Rule for assignment statement: Examples of type rules: Rule for if-statment: x : T E : T x := E; : Stmt E 1 : Bool S 1 : Stmt S 2 : Stmt if (E 1 ) {S 1 } else {S 2 } : Stmt (if)

How to type-check? Rules of Type Checking Type of value => known in advance Type of variable => known in the declaration Type of function => known from the type of arguments (in declaration) and type of result (also in declaration). Type of expression => inferred from subexpression.

How to type-check? Given the program: And Given the rules: int x; x := x+1; A program/expression is typesafe if we can construct a derivation tree to give a type for that program/expression. 1 : int 2 : int 3 : int E 1 : int E 2 : int E 1 + E 2 : int E 1 : int E 2 : int E 1 == E 2 : bool x : T E : T x := E; : Stmt (+) (==) E 1 : Bool S 1 : Stmt S 2 : Stmt if (E 1 ) {S 1 } else {S 2 } : Stmt (if) x : int x : int 1 : int (+) x+1 : int x:=x+1; : Stmt

How to type-check? Given the program: int x; float y; if (x == 3) { y := x; } else { x := x+1; } A program/expression is typesafe if we can construct a derivation tree to give a type for that program/expression. Follow the rules! Try to build tree. Cannot build tree => Not type safe x : int 3 : int x==3 : Bool (==)??? And Given the rules: 1 : int 2 : int 3 : int E 1 : int E 2 : int E 1 + E 2 : int E 1 : int E 2 : int E 1 == E 2 : bool x : T E : T x := E; : Stmt E 1 : Bool if (x==3) {y:=x;} else {x:=x+1;} : Stmt (+) (==) S 1 : Stmt S 2 : Stmt if (E 1 ) {S 1 } else {S 2 } : Stmt x : int y:=x; : Stmt x:=x+1; : Stmt (if) (if) x : int 1 : int (+) x+1 : int

How to type-check? Dynamic type checking Static type checking Strongly typed: we can detect all type errors statically (all f: S R type safe). ML: type declarations are not necessary if the interpretation is unambiguous.

How to cater for Polymorphism Polymorphism = poly (many) + morph (form) Polymorphism is the ability of a data object to take on or assume many different forms (Polymorphic operations)

Polymorphism Coercion COERCION A coercion is a operation that converts the type of an expression to another type. It is done automatically by the language compiler. (If the programmer manually forces a type conversion, it s called casting) E : int E : float (Int-Float Coercion) int x; float y;... y := x;...

Polymorphism Coercion int x; float y; if (x == 3) { y := x; } else { x := x+1; } E : int E : float Example of the use of COERCION Add in new rule (Int-Float Coercion) 1 : int 2 : int 3 : int E 1 : int E 2 : int E 1 + E 2 : int E 1 : int E 2 : int E 1 == E 2 : bool x : T E : T x := E; : Stmt E 1 : Bool (+) (==) S 1 : Stmt S 2 : Stmt if (E 1 ) {S 1 } else {S 2 } : Stmt (if) x : int 3 : int x==3 : Bool (==) x : int x : int 1 : int (Coercion) (+) y : float x : float x : int x+1 : int y:=x; : Stmt x:=x+1; : Stmt if (x==3) {y:=x;} else {x:=x+1;} : Stmt (if)

Polymorphism Coercion Coercion Widening Widening coercion converts a value to a type that can include all of the values of the original type. Widening is safe most of the time. Narrowing Narrowing coercion converts a value to a type that cannot store (even approximations of) all of the values of the original type. Narrowing is unsafe. Information is lost during conversion of type. int float Theoretically speaking, int float int float

Polymorphism Coercion Coercions (+) Increase flexibility in programming Example: float x,y,z; int a,b,c; If I have no coercions, and I intend to add y and a and store in x, then writing x = y + ((float) a); is too much of a hassle. Therefore coercion is good.

Polymorphism Coercion Coercions ( ) Decrease Reliability (error detection) Example: float x,y,z; int a,b,c; If I have coercions and I intend to add x and y and store in z, but I accidentally write z = x + a; then my error will go undetected because the compiler will simply coerce the a to a float. Therefore coercion is bad.

Polymorphism Coercion Coercions: A lot of them: PL/I, Fortran, C, C++ Fewer : Java (permits only widening) Very Few: Ada

Polymorphism Overloading OVERLOADING An overloaded operation has different meanings, and different types, in different contexts. E 1 : int E 1 + E 2 : int E 2 : int (+-int) E 1 : float E 2 : float E 1 + E 2 : float (+-float)

Polymorphism Overloading int x,y,z; float a,b,c; if (x == 3) { x := y + z; } else { a := b + c; } Example of the use of Overloading Add in new rule 1 : int 2 : int 3 : int E 1 : int E 2 : int E 1 + E 2 : int E 1 : int E 2 : int E 1 == E 2 : bool x : T E : T x := E; : Stmt (+) (==) E 1 : float E 2 : float E 1 + E 2 : float (+-float) E 1 : Bool S 1 : Stmt S 2 : Stmt if (E 1 ) {S 1 } else {S 2 } : Stmt (if) y:int z:int b:float c:float (+) (+ -flo x : int 3 : int x : int y+z : int a : float b+c : float (==) x==3 : Bool x:=y+z; : Stmt a:=b+c; : Stmt (if) if (x==3) {x:=y+z;} else {a:=b+c;} : Stmt

Polymorphism Overloading Overloading (+) Increase flexibility in programming Examples are when user wants to use an operator to express similar ideas. Example: int a,b,c; int p[10], q[10], r[10]; int x[10][10], y[10][10], z[10][10]; a = b * c; // integer multiplication p = a * q; // Scalar multiplication x = y * z; // Matrix multiplication Therefore overloading is good.

Polymorphism Overloading Overloading ( ) Decrease Reliability (error detection) Examples are when user intends to use the operator in one context, but accidentally uses it in another. Example In many languages, the minus sign is overloaded to both unary and binary uses. x = z y and x = -y will both compile. What if I intend to do the first, but accidentally leave out the z?

Polymorphism Overloading Overloading Do you allow the user to perform overloading? (Flexibility) Or are all overloaded functions predefined in the language? (controlled reliability) If you allow the user to perform overloading, then can the user overload existing operators in the language?