Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Introduction to C Programming
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
C Language.
PROOF BY CONTRADICTION
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Intermediate Code Generation
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Programming Languages and Paradigms The C Programming Language.
Structures Spring 2013Programming and Data Structure1.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
1 C++ Syntax and Semantics The Development Process.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Arrays, Slices, and more. Structs  A struct is a collection of fields  The keyword type is used to declare the type of the struct  Syntax: type Vertex.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Compiler Construction
Chapter 9 Imperative and object-oriented languages 1.
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Encapsulation by Subprograms and Type Definitions
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Type Checking  Legality checks  Operator determination  Overload resolution.
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.
Fundamentals of Python: From First Programs Through Data Structures
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Fundamentals of Python: First Programs
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
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.
Organizing Heterogeneous Data Arrays allow a programmer to organize lists of values that are all of the same type (homogeneous). But we are often faced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Lecture 9 Symbol Table and Attributed Grammars
Types CSCE 314 Spring 2016.
Constructing Precedence Table
Principles of programming languages 8: Types
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages and Paradigms
Object Oriented Programming
Representation, Syntax, Paradigms, Types
Programming Paradigms
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Data Types.
Chapter 6 Intermediate-Code Generation
Type Systems CSE 340 – Principles of Programming Languages Fall 2016
Representation, Syntax, Paradigms, Types
Representation, Syntax, Paradigms, Types
Representation, Syntax, Paradigms, Types
Compiler Construction
Compiler Construction
Presentation transcript:

Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University

Adam Doupé, Principles of Programming Languages Type Systems Informally, a type in a programming language specifies a set of values and operations that can be applied on those values –A type can be associated with a variables or a constant –Values are not necessarily numeric values, for example we can specify function types A type system consists of –Basic types –Type constructors –Type inference –Type compatibility 2

Adam Doupé, Principles of Programming Languages Type Declaration Programming language will typically include –Basic types Included in the programming language and available to any program written in that language –Type constructors Way for a programmer to define new types 3

Adam Doupé, Principles of Programming Languages Type Constructors Pointer to T, where T is a type struct { a 1 : T 1 ; a 2 : T 2 ; …, a k : T k ; } –Where a i is a field name and T i is a previously defined type array range of T –Where range can be single or multi dimensional function of T 1, T 2,..., T k returns T –Type is a function, the types of the parameters are T 1... T k and the return type is T 4

Adam Doupé, Principles of Programming Languages Using Type Constructors Declaring Types Type cm : integer; Type RGBA : array [0..4] of int; Type png : array [0..256] of RGBA; Anonymous Types array [0..4] of int x; struct { int a; char b; } y; 5

Adam Doupé, Principles of Programming Languages Type Compatibility Which assignments are allowed by the type system? –a = b;? –int a; float b; –float a; int b; 6

Adam Doupé, Principles of Programming Languages Type Inference Types of expressions or other constructs as a function of subexpression types –a + b a int; b float –Returns a float in C –Error in ML –a * b a string; b int –Error in most languages –Returns a string in Python 7

Adam Doupé, Principles of Programming Languages Type Compatibility Principally about type equivalence –How to determine if two types are equal? Type cm : integer; Type inch : integer; cm x; inch y; x = y? 8

Adam Doupé, Principles of Programming Languages Name Equivalence Types must have the exact same name to be equivalent Type cm : integer; Type inch : integer; cm x; inch y; x = y? // ERROR 9

Adam Doupé, Principles of Programming Languages Name Equivalence a: array [0..4] of int; b: array [0..4] of int; a = b? –Not allowed under name equivalence 10

Adam Doupé, Principles of Programming Languages Name Equivalence a, b: array [0..4] of int; a = b? –Not allowed because array [0..4] of int is not named 11

Adam Doupé, Principles of Programming Languages Name Equivalence Type A: array [0..4] of int; a: A; b: A; a = b? –Allowed, because both a and b have the same name 12

Adam Doupé, Principles of Programming Languages Internal Name Equivalence If the program interpreter gives the same internal name to two different variables, then they share the same type a, b: array [0..4] of int; c: array [0..4] of int; a = b? –Yes, because interpreter/compiler gives the same internal name to a and b a = c? –No, because interpreter/compiler gives different internal name to c than to a and b 13

Adam Doupé, Principles of Programming Languages Structural Equivalence 1.Same built-in types 2.Pointers to structurally equivalent types Type cm : integer; Type inch : integer; cm x; inch y; x = y? // Allowed! 14

Adam Doupé, Principles of Programming Languages Structural Equivalence int* a; float* b; a = b? –Not structurally equivalent, because int and float are not structurally equivalent 15

Adam Doupé, Principles of Programming Languages Structural Equivalence 3.Determining struct structural equivalence –Two structures –st1 { x 1 : W 1, x 2 : W 2, …, x k : W k } –st2 { y 1 : Q 1, y 2 : Q 2,..., y k : Q k } –st1 and st2 are structurally equivalent iff W 1 structurally equivalent to Q 1 W 2 structurally equivalent to Q 2... W k structurally equivalent to Q k 16

Adam Doupé, Principles of Programming Languages Structural Equivalence struct A { a: int, b: float } struct B { b: int, a: float } A foo; B bar; foo = bar? 17

Adam Doupé, Principles of Programming Languages Structural Equivalence struct A { a: int, b: float } struct B { b: float, a: int } A foo; B bar; a = b? 18

Adam Doupé, Principles of Programming Languages Structural Equivalence 4.Determining array structural equivalence –Two Arrays –T1 = array range1 of t 1 –T2 = array range2 of t 2 –T1 and T2 are structurally equivalent iff: range1 and range2 have (1) the same number of dimensions and (2) the same number of entries in each dimension t 1 and t 2 are structurally equivalent 19

Adam Doupé, Principles of Programming Languages Structural Equivalence 5.Determining function structural equivalence –Two functions –T1 = function of (t 1, t 2, t 3, …, t k ) returns t –T2 = function of (v 1, v 2, v 3,..., v k ) returns v –T1 and T2 are structurally equivalent iff: For all i from 1 to k, t i is structurally equivalent to v i t is structurally equivalent to v 20

Adam Doupé, Principles of Programming Languages Determining Structural Equivalence The goal is to determine, for every pair of types in the program, if they are structurally equivalent Seems fairly simple, just keep applying the previous 5 rules until the base case 1 or 2 is reached How to handle the following case: T1 = struct { a: int; p: pointer to T2; } T2 = struct { a: int; p: pointer to T1; } Applying the rules states that T1 is structurally equivalent to T2 iff pointer to T1 is structurally equivalent to pointer to T2, which is true if T1 is structurally equivalent to T2 21

Adam Doupé, Principles of Programming Languages Structural Equivalence Algorithm The way to break the stalemate is to assume that T1 and T2 are structurally equivalent because no rule contradicts them being structurally equivalent Our goal is to create an n X n table, where n is the number of types in the program, and each entry in the table is true if the types are structurally equivalent and false otherwise 22

Adam Doupé, Principles of Programming Languages Structural Equivalence Algorithm To support cyclical definitions, we first initialize all entries in the table to true –We assume that types are structurally equivalent unless we have proof otherwise Algorithm is fairly simple –Set the n X n table to have each entry as true –While table has not changed Check each entry i, j in the table, and if T i and T j are not structurally equivalent, then set the entry i, j in the table to false Note that T i and T j are the i th and j th types in the program 23

Adam Doupé, Principles of Programming Languages T1 = struct { a: int; p: pointer to T2; } T2 = struct { c: int; q: pointer to T3; } T3 = struct { a : float; p: pointer to T1; } 24 T1T2T3 T1true T2true T3true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 25 T1T2T3 T1true T2true T3true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 26 T1T2T3 T1true T2true T3true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 27 T1T2T3 T1true T2true T3true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 28 T1T2T3 T1true false T2true T3true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 29 T1T2T3 T1true false T2true T3falsetrue

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 30 T1T2T3 T1true false T2true T3falsetrue

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 31 T1T2T3 T1true false T2true false T3falsetrue

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 32 T1T2T3 T1true false T2true false T3false true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 33 T1T2T3 T1true false T2true false T3false true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 34 T1T2T3 T1truefalse T2true false T3false true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 35 T1T2T3 T1truefalse T2falsetruefalse T3false true

Adam Doupé, Principles of Programming Languages T1 = struct { a: int, p: pointer to T2 } T2 = struct { c: int, q: pointer to T3 } T3 = struct { a: float, p: pointer to T1 } 36 T1T2T3 T1truefalse T2falsetruefalse T3false true