Principles of Programming Languages

Similar documents
301AA - Advanced Programming [AP-2017]

Types and Type Inference

Types and Type Inference

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

CA341 - Comparative Programming Languages

Example: Haskell algebraic data types (1)

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

22c:111 Programming Language Concepts. Fall Types I

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

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Programming Languages

Types. What is a type?

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages, Summary CSC419; Odelia Schwartz

Types, Type Inference and Unification

Programming Languages

Data Types. CSE 307 Principles of Programming Languages Stony Brook University

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

CSCI312 Principles of Programming Languages!

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

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

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

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

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Lecture 12: Data Types (and Some Leftover ML)

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

CS558 Programming Languages

Types II. Hwansoo Han

TYPES, VALUES AND DECLARATIONS

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

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

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

Chapter 5 Names, Binding, Type Checking and Scopes

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

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

G Programming Languages - Fall 2012

CSc 520. Principles of Programming Languages 25: Types Introduction

CSE 431S Type Checking. Washington University Spring 2013

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

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

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

HANDLING NONLOCAL REFERENCES

Type Checking and Type Inference

Final-Term Papers Solved MCQS with Reference

CS558 Programming Languages

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

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

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Names, Scopes, and Bindings II. Hwansoo Han

Principles of Programming Languages

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

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

Lecture #23: Conversion and Type Inference

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

Imperative Programming

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

Topic 9: Type Checking

Topic 9: Type Checking

Fundamentals of Programming Languages

CSE 307: Principles of Programming Languages

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

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Type Bindings. Static Type Binding

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

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

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Compiler Construction

NOTE: Answer ANY FOUR of the following 6 sections:

Some instance messages and methods

CSC 533: Organization of Programming Languages. Spring 2005

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

Chapter 5. Names, Bindings, and Scopes

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Programmiersprachen (Programming Languages)

History. used in early Mac development notable systems in Pascal Skype TeX embedded systems

CS321 Languages and Compiler Design I. Fall 2013 Week 8: Types Andrew Tolmach Portland State University

Type Systems, Type Inference, and Polymorphism

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

The role of semantic analysis in a compiler

Expressions and Assignment

Weeks 6&7: Procedures and Parameter Passing

Chapter 5 Names, Bindings, Type Checking, and Scopes

CSE 307: Principles of Programming Languages

Compiler Construction

Motivation for typed languages

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

CS558 Programming Languages

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Short Notes of CS201

CSCE 314 Programming Languages. Type System

Principles of Programming Languages

Programming Methodology

CS 230 Programming Languages

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS201 - Introduction to Programming Glossary By

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Transcription:

Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 23! Type systems Type safety Type checking Equivalence, compaability and coercion PrimiAve and composite types Discrete and scalar types, tuples and records 1

What is a Data Type? A (data) type is a homogeneous collecaon of values, effecavely presented, equipped with a set of operaaons which manipulate these values Various perspecaves: collecaon of values from a "domain" (the denotaaonal approach) internal structure of a bunch of data, described down to the level of a small set of fundamental types (the structural approach) collecaon of well- defined operaaons that can be applied to objects of that type (the abstracaon approach) 2

Advantages of Types Program organizaaon and documentaaon Separate types for separate concepts Represent concepts from problem domain Document intended use of declared idenafiers Types can be checked, unlike program comments IdenAfy and prevent errors Compile- Ame or run- Ame checking can prevent meaningless computaaons such as 3 + true Bill Support implementaaon and opamizaaon Example: short integers require fewer bits Access components of structures by known offset 3

Type system A type system consists of 1. The set of predefined types of the language. 2. The mechanisms which permit the definiaon of new types. 3. The mechanisms for the control of types, which include: 1. Equivalence rules which specify when two formally different types correspond to the same type. 2. CompaDbility rules specifying when a value of a one type can be used in a context in which a different type would be required. 3. Rules and techniques for type inference which specify how the language assigns a type to a complex expression based on informaaon about its components. 4. The specificaaon as to whether (or which) constraints are stadcally or dynamically checked. 4

Type errors A type error occurs when a value is used in a way that is inconsistent with its definiaon Type errors are type system (thus language) dependent ImplementaAons can react in various ways Hardware interrupt, e.g. apply fp addi3on to non- legal bit configura3on OS excepaon, e.g. page fault when dereferencing 0 in C ConAnue execuaon with possibily wrong values Examples Array out of bounds access C/C++: runame errors Java: dynamic type error Null pointer dereference C/C++: run- Ame errors Java: dynamic type error Haskell/ML: pointers are hidden inside datatypes Null pointer dereferences would be incorrect use of these datatypes, therefore staac type errors 5

Type safety A language is type safe when no program can violate the disancaons between types defined in its type system In other words, a type system is safe when no program, during its execuaon, can generate an unsignalled type error Also: if code accesses data, it is handled with the type associated with the creaaon and previous manipulaaon of that data 6

Safe and not safe languages Not safe: C and C++ Casts, pointer arithmeac Almost 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 deallocaaon of memory is fully type- safe. Safe or Strongly Typed: Lisp, Smalltalk, ML, Haskell, Java, JavaScript Dynamically typed: Lisp, Smalltalk, JavaScript StaAcally typed: ML, Haskell, Java 7

Type checking Before any operaaon is performed, its operands must be type- checked to prevent a type error. E.g.: mod operaaon: check that both operands are integers and operaaon: check that both operands are booleans indexing operaaon: check that the lef operand is an array, and that the right operand is a value of the array s index type. 8

StaAc vs dynamic typing (1) In a stadcally typed PL: all variables and expressions have fixed types (either stated by the programmer or inferred by the compiler) all operands are type- checked at compile- 3me. Most PLs are staacally typed, including Ada, C, C++, Java, Haskell. 9

StaAc vs dynamic typing (2) In a dynamically typed PL: values have fixed types, but variables and expressions do not operands must be type- checked when they are computed at run- 3me. Some PLs and many scripang languages are dynamically typed, including Smalltalk, Lisp, Prolog, Perl, Python. 10

Example: Ada staac typing Ada funcaon definiaon: function is_even (n: Integer) return Boolean is begin return (n mod 2 = 0); end; Call: p: Integer; if is_even(p+1) The compiler doesn t know the value of n. But, knowing that n s type is Integer, it infers that the type of n mod 2 = 0 will be Boolean. The compiler doesn t know the value of p. But, knowing that p s type is Integer, it infers that the type of p+1 will be Integer. Even without knowing the values of variables and parameters, the Ada compiler can guarantee that no type errors will happen at run-time. 11

Example: Python dynamic typing Python funcaon definiaon: def even (n): return (n % 2 == 0) The type of n is unknown. So the % (mod) operation must be protected by a runtime type check. The types of variables and parameters are not declared, and cannot be inferred by the Python compiler. So run-time type checks are needed to detect type errors. 12

StaAc vs dynamic typing Pros and cons of staac and dynamic typing: StaAc typing is more efficient. Dynamic typing requires run- Ame type checks (which make the program run slower), and forces all values to be tagged (to make the type checks possible). StaAc typing requires only compile- Ame type checks, and does not force values to be tagged. StaAc typing is more secure: the compiler can guarantee that the object program contains no type errors. Dynamic typing provides no such security. Dynamic typing is more flexible. This is needed by some applicaaons where the types of the data are not known in advance. JavaScript array: elements can have different types Haskell list: all elements must have same type 13

StaAc typing is conservaave In JavaScript, we can write a funcaon like function f(x) { return x < 10? x : x(); } Some uses will produce type error, some will not. StaAc typing must be conserva3ve if (possibly-non-terminating-boolean-expression) then f(5); else f(15); Cannot decide at compile Ame if run- Ame error will occur! Note: safety is independent of dynamic/staac 14

Type Checking: how does it work Checks that each operator is applied to arguments of the right type. It needs: Type inference, to infer the type of an expression given the types of the basic consatuents Type compa3bility, to check if a value of type A can be used in a context that expects type B Coercion rules, to transform silently a type into a compaable one, if needed Type equivalence, to know if two types are considered the same 15

Type Equivalence -- pseudo Pascal type Student = record name, address : string age : integer Structural equivalence: unravel all type constructors obtaining type expressions containing only primiave types, then check if they are equivalent Name equivalence: based on declaraaons Name equivalence more popular today But someames aliases needed type School = record name, address : string age : integer x : Student; y : School; x:= y; --ok with structural equivalence --error with name equivalence TYPE stack_element = INTEGER; MODULE stack; IMPORT stack_element; EXPORT push, pop; (* alias *)... PROCEDURE push(elem : stack_element);... PROCEDURE pop() : stack_element;... var st:stack; st.push(42); 16 // this should be OK

Type compaability and Coercion Type compaability rules vary a lot Integers as reals OK Subtypes as supertypes OK Reals as integers??? Doubles as floats??? When an expression of type A is used in a context where a compaable type B is expected, an automaac implicit conversion is performed, called coercion 17

Type compaability and Coercion Coercion may change the representaaon of the value or not Integer à Real binary representa3on is changed {int x = 5; double y = x; } A à B subclasses binary representa3on not changed class A extends B{ } {B mybobject = new A( ); } Coercion may cause loss of informaaon, in general Not in Java, with the excepaon of long as float In staacally typed languages coercion instrucaons are inserted during semanac analysis (type checking) Popular in Fortran/C/C++, tends to be replaced by overloading and polymorphism 18

Built- in primiave types Typical built- in primiave types: Boolean = {false, true} Character = {, A,, Z,, 0,, 9, } Integer = {, 2, 1, 0, +1, +2, } Float = {, 1.0,, 0.0, +1.0, } PL- or implementation-defined set of characters (ASCII, ISO- Latin, or Unicode) PL- or implementation-defined set of whole numbers PL- or implementation-defined set of real numbers Note: In some PLs (such as C), booleans and characters are just small integers. Names of types vary from one PL to another: not significant. 19

Terminology Discrete types countable integer, boolean, char enumeraaon subrange Scalar types - one- dimensional discrete real type Color is (red, green, blue); type Population is range 0.. 1e10; 20

Composite types Types whose values are composite, that is composed of other values (simple or composite): records (unions) Arrays (Strings) algebraic data types sets pointers lists Most of them can be understood in terms of a few concepts: Cartesian products (records) mappings (arrays) disjoint unions (algebraic data types, unions, objects) recursive types (lists, trees, etc.) Different names in different languages. Defined applying type constructors to other types (eg struct, array, record, ) 21

An brief overview of composite types We review type constructors in Ada, Java and Haskell corresponding to the following mathemaacal concepts: Cartesian products (records) mappings (arrays) disjoint unions (algebraic data types, unions) recursive types (lists, trees, etc.) 22

Cartesian products (1) In a Cartesian product, values of several types are grouped into tuples. Let (x, y) be the pair whose first component is x and whose second component is y. S T denotes the Cartesian product of S and T: S T = { (x, y) x S; y T } Cardinality: #(S T) = #S #T hence the notation 23

Cartesian products (2) We can generalise from pairs to tuples. Let S 1 S 2 S n stand for the set of all n- tuples such that the ith component is chosen from S i : S 1 S 2 S n = { (x 1, x 2,, x n ) x 1 S 1 ; x 2 S 2 ; ; x n S n } Basic operaaons on tuples: construcdon of a tuple from its component values selecdon of an explicitly- designated component of a tuple. so we can select the 1st or 2nd (but not the ith) component Records (Ada), structures (C), and tuples (Haskell) can all be understood in terms of Cartesian products. 24

Example: Ada records (1) Type declaraaons: type Month is (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec); type Day_Number is range 1.. 31; type Date is record m: Month; d: Day_Number; end record; ApplicaAon code: record construction someday: Date := (jan, 1); put(someday.m+1); put("/"); put(someday.d); someday.d := 29; someday.m := feb; component selection 25

Example: Haskell tuples DeclaraAons: data Month = Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec type Date = (Month, Int) Set of values: Date = Month Integer = {Jan, Feb,, Dec} {, 1, 0, 1, 2, } ApplicaAon code: someday = (jan, 1) m, d = someday anotherday = (m + 1, d) tuple construction component selection (by pattern matching) 26