Type checking © Marcelo d’Amorim 2010.

Slides:



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

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Functional Programming Lecture 10 - type checking.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
ISBN Chapter 3 Describing Syntax and Semantics.
Type Checking.
Compiler Construction
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Approaches to Typing Programming Languages Robert Dewar.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
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.
Describing Syntax and Semantics
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
CSC-682 Cryptography & Computer Security Sound and Precise Analysis of Web Applications for Injection Vulnerabilities Pompi Rotaru Based on an article.
CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Formal Semantics of Programming Languages 虞慧群 Topic 1: Introduction.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Types in programming languages1 What are types, and why do we need them?
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Semantic Analysis Semantic Analysis v Lexically and syntactically correct programs may still contain other errors v Lexical and syntax analyses.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Chapter 4: Types. Why Have Types? Detect simple kinds of errors: int square(int x) { return x*x; }... int bogus = square(“hello”); String bogus2 = square(3);
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Lesson 10 Type Reconstruction
Functional Programming
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Types CSCE 314 Spring 2016.
Semantic Analysis Type Checking
Principles of programming languages 8: Types
Representation, Syntax, Paradigms, Types
Chapter 4: Types.
Typed Arithmetic Expressions
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Representation, Syntax, Paradigms, Types
Representation, Syntax, Paradigms, Types
Types and Type Checking (What is it good for?)
Representation, Syntax, Paradigms, Types
Presentation transcript:

Type checking © Marcelo d’Amorim 2010

Definition of type system “A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.” © Marcelo d’Amorim 2010 Types and Programming Languages. B.C.Pierce.

Definition of type system “A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.” © Marcelo d’Amorim 2010 Types and Programming Languages. B.C.Pierce. A type checker is an efficient algorithm

Definition of type system “A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.” © Marcelo d’Amorim 2010 Types and Programming Languages. B.C.Pierce. Sound but incomplete: can reject valid programs

Definition of type system “A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.” © Marcelo d’Amorim 2010 Types and Programming Languages. B.C.Pierce. Not all errors

Definition of type system “A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.” © Marcelo d’Amorim 2010 Types and Programming Languages. B.C.Pierce. Approximates dynamic behavior statically

Expressions, types, and values © Marcelo d’Amorim 2010 ExpressionsValues Types compile timeexecution time

Expressions, types, and values © Marcelo d’Amorim 2010 ExpressionsValues Types compile timeexecution time Some compilers drop type information at runtime.

Functional example © Marcelo d’Amorim 2010 if then 5 else “Hello”

Functional example Type of expressions – 1: bool – 2: int – 3: string – 4: ? © Marcelo d’Amorim 2010 if then 5 else “Hello” Acceptable or not depends on the semantics of the language!

OO example © Marcelo d’Amorim 2010 T m() {…} R r = m(); R T Object 

OO example © Marcelo d’Amorim 2010 T m() {…} R r = m(); R T Object T is not a subtype of R: object of type T is not assignable to a variable of type R. 

OO example © Marcelo d’Amorim 2010 T m() {…} R r = m(); R T Object

OO example © Marcelo d’Amorim 2010 T m() {…} R r = (R) m(); T R Object Compiler accepts at compile time!

OO example © Marcelo d’Amorim 2010 T R Object U T m() {…} R r = (R) m(); ClassCastException! 

Language Type Systems Static vs. Dynamic – Checking is done at compile or runtime – E.g., Java (static) and Scheme (dynamic) Strong vs. Weak – Assignment is only permitted if types are consistent – E.g., Java (strong) and C (weak) © Marcelo d’Amorim 2010

Type inference Ability of some strongly-typed languages (typically functional) to infer types of expressions without having to define them – E.g., Haskell © Marcelo d’Amorim 2010

Type reconstruction Mechanical reconstruction of types as defined by user in seek of conflicts © Marcelo d’Amorim 2010

Language of terms L L ::= | true | false | if t then t else t | 0 | succ t | pred t | iszero t © Marcelo d’Amorim 2010

Inference rules © Marcelo d’Amorim 2010 true L t1 ∈ L succ t1 ∈ L … Another approach to define the language ∈

Inference rules © Marcelo d’Amorim 2010 true L t1 ∈ L succ t1 ∈ L … axiom rule ∈ variable

Exercise 1 Complete the definition of the set of terms L with additional inference rules © Marcelo d’Amorim 2010

Adding types to L Notation: “t: T” indicates that the expression t “has type” T © Marcelo d’Amorim 2010

Exercise 2 Define inference rules for the extension of L with Nat and Bool types © Marcelo d’Amorim 2010

Exercise 3 Type the following terms: – pred(succ(0)) – iszero (if iszero(true) then false else 0) © Marcelo d’Amorim 2010

Exercise 3 Type the following terms: – pred(succ(0)) – iszero (if iszero(true) then false else 0) © Marcelo d’Amorim 2010

Hindley-Milner type system © Marcelo d’Amorim 2010 Type and Effect Systems. Amtoft T., Nielson F, Nielson H. R.

Hindley-Milner type system © Marcelo d’Amorim 2010 Type and Effect Systems. Amtoft T., Nielson F, Nielson H. R. A is an environment that maps names to inferred types

Hindley-Milner type system © Marcelo d’Amorim 2010 Type and Effect Systems. Amtoft T., Nielson F, Nielson H. R. Symbol ├ denotes a type judgement. For example, A ├ e : t means that the type of e is t under environment A

Hindley-Milner type system © Marcelo d’Amorim 2010 Type and Effect Systems. Amtoft T., Nielson F, Nielson H. R.

Hindley-Milner type system © Marcelo d’Amorim 2010 Type and Effect Systems. Amtoft T., Nielson F, Nielson H. R.

Exercise Type check the following functions: © Marcelo d’Amorim 2010 rec fac n = if n == 1 then 1 else n * fac (n – 1) let twice = fn f => fn x => f (f x) in twice k

Algorithm W 1.Collect type (equality) constraints 2.Unify type expressions 3.Identify equivalent classes of type variables 4.Choose representative for each class and remove equivalent variables 5.Report type for each term © Marcelo d’Amorim 2010

Type checking Important static technique to detect simple kinds of errors in programs – Typically sound but can report alarms on valid programs (incomplete) © Marcelo d’Amorim 2010

Type inference algo W © Marcelo d’Amorim 2010 Type and Effect Systems. Amtoft T., Nielson F, Nielson H. R.

Type inference algo W © Marcelo d’Amorim 2010 Type and Effect Systems. Amtoft T., Nielson F, Nielson H. R. W is the type inference procedure. It takes environment A and expression e as input and returns a substitution S and inferred type t as output.

Type reconstruction algo W © Marcelo d’Amorim 2010 Type and Effect Systems. Amtoft T., Nielson F, Nielson H. R. W (t1,t2) is a unification procedure that returns a substitution S such that St1 = St2.

Other type systems Dependent types Intersection types Union types © Marcelo d’Amorim 2010