Topic 9: Type Checking

Similar documents
Topic 9: Type Checking

Programming Paradigms

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

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

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

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

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

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

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

Topic 1: Introduction

CS558 Programming Languages


CSc 520 Principles of Programming Languages

Types and Type Inference

Types. What is a type?

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

CSc 520. Principles of Programming Languages 25: Types Introduction

Programming Languages Fall 2013

CSE 431S Type Checking. Washington University Spring 2013

Programming Languages

CSI33 Data Structures

Chapter 5. Names, Bindings, and Scopes

Types and Type Inference

Topic 7: Algebraic Data Types

CSCI312 Principles of Programming Languages!

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

Type Checking and Type Inference

22c:111 Programming Language Concepts. Fall Types I

Programming Languages

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Type Bindings. Static Type Binding

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

TYPES, VALUES AND DECLARATIONS

Tokens, Expressions and Control Structures

Types, Type Inference and Unification

Data Types The ML Type System

CSC324 Principles of Programming Languages

CMSC 330: Organization of Programming Languages

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Lecture 12: Data Types (and Some Leftover ML)

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

Introduction to C++ with content from

VARIABLES AND TYPES CITS1001

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

Chapter 5 Names, Binding, Type Checking and Scopes

A brief introduction to C programming for Java programmers

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Dynamically-typed Languages. David Miller

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

G Programming Languages - Fall 2012

Topic 2: Making Decisions

Scripted Components: Problem. Scripted Components. Problems with Components. Single-Language Assumption. Dr. James A. Bednar

Scripted Components Dr. James A. Bednar

Topic 2: Making Decisions

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CS 360: Programming Languages Lecture 12: More Haskell

CSC324 Principles of Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

Lecture #23: Conversion and Type Inference

Pointers (continued), arrays and strings

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

CS 320: Concepts of Programming Languages

Type-indexed functions in Generic Haskell

CS558 Programming Languages

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

Pointers (continued), arrays and strings

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

Francesco Nidito. Programmazione Avanzata AA 2007/08

Programming Languages, Summary CSC419; Odelia Schwartz

Static Typing or Dynamic Typing: A Real Issue or a Simple Case of Tribalism

CSE 341 Lecture 9. type systems; type safety; defining types Ullman 6-6.2; 5.3. slides created by Marty Stepp

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Topic 8: Lazy Evaluation

Inclusion Polymorphism

The role of semantic analysis in a compiler

Pace University. Fundamental Concepts of CS121 1

Introduction to programming with Python

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

CIS133J. Working with Numbers in Java

Chapter 2: Using Data

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Chapter 2 Basic Elements of C++

CSC 533: Organization of Programming Languages. Spring 2005

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

COS 140: Foundations of Computer Science

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

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

CS558 Programming Languages

Expressions and Assignment

CSCI-GA Scripting Languages

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

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

Software II: Principles of Programming Languages. Why Expressions?

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Type Systems, Type Inference, and Polymorphism

Transcription:

Recommended Exercises and Readings Topic 9: Type Checking From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6 and 13.7 1 2 Type systems There are (at least) three different ways that we can compare type systems Static vs. dynamic typing Strong vs. weak typing Explicit vs. implicit typing Static vs. Dynamic Typing Static typing The type of a variable is known at compile time It is explicitly stated by the programmer Or it can be inferred from context Type checking can be performed without running the program Used by Haskell Also used by C, C++, C#, Java, Fortran, Pascal, 3 4

Static vs. Dynamic Typing Dynamic typing The type of a variable is not known until the program is running Typically determined from the value that is actually stored in it Program can crash at runtime due to incorrect types Permits downcasting, dynamic dispatch, reflection, Used by Python, Javascript, PHP, Lisp, Ruby, Static vs. Dynamic Typing Static vs. Dynamic typing is a question of timing When is type checking performed? At compile time (before the program runs)? Static As the program is running? Dynamic 5 6 Static vs. Dynamic Typing Some languages do a mixture of both of static and dynamic typing Most type checking in Java is performed at compile time but downcasting is permitted, and can t be verified at compile time Generated code includes a runtime type check A ClassCastException is thrown if the downcast wasn t valid Most type checking in C++ is performed at compile time.. but downcasting is permitted, and can t be verified at compile time A C style cast on incompatible pointer types results in a C++ reinterpret_cast and often causes unpredictable behaviour for incompatible types Using dynamic_cast in C++ returns NULL for an invalid cast Static vs. Dynamic Typing Advantages of static typing Advantages of dynamic typing 7 8

Strong vs. Weak Typing Example: Adding a Character to an Integer In a strongly typed language One must provide a value that matches the expected type Exact match Subtype match Type class match Duck match In a weakly typed language One has much more freedom to mix values of different types Type strength is a continuum Independent of when the type checking is performed Haskell: Prohibited Python: Prohibited But Python doesn t really have a character type it s just a string that has length 1 Java: if the result is stored in an int Prohibited if the result is stored in a char Unless it is explicitly cast to a char C++: No warning, even with Wall C: No warning, even with Wall Perl: And Perl doesn t really have a character type it s just a string that has length 1 Generates a warning with w 9 10 Example: Adding a String to a String Example: Adding an Integer and a Float Haskell Prohibited with + with ++ Python: Java: C++: C: Prohibited with + Two pointers can t be added together with strcat or strncat Programmer must ensure that there is sufficient memory available Perl: with + But it results in 0 Generates a warning with w with. Which concatenates the strings 11 Haskell: Prohibited Python Result is a float Java If the result is stored in a float Prohibited If the result is stored in an int Unless the result is explicitly cast to an int C++: Result is an int or float, depending on the type of variable it is stored in C: Result is an int or float, depending on the type of variable it is stored in Perl: Result is a float 12

Explicit vs. Implicit Typing Explicit typing Programmer is responsible for specifying types while writing their code Examples: C, Java Implicit typing Types are inferred by the type system, or simply aren t needed Examples: Python, Perl Explicit vs. Implicit Typing Haskell allows the programmer to explicitly specify types But explicit types are rarely required Haskell will infer the types (in most situations) when they are not provided C++ 11 added the auto keyword Variables still need to be declared but the type is automatically inferred by the compiler This is really convenient for complicated template types 13 14 Type Systems Static vs. Dynamic When is type checking done? Strong vs. Weak How much type checking is done? Explicit vs. Implicit Does the programmer specify the types directly, or does the compiler / interpreter infer them? Type Checking in Haskell An expression can include Literals Operators Function calls Parentheses In Haskell, the type of every expression can be determined at compile time, and checked for type correctness 15 16

Type Checking in Haskell Type Checking in Haskell Consider the expression: Consider the expression chr (ord ch ord 'a' + ord 'A') succ (4 :: Int) + False Is this expression type correct? Known from previous type signatures chr :: Int > Char ord :: Char > Int 17 Is this expression type correct? Known from previous type signatures: succ :: Enum a => a > a 18 Type Checking in Haskell Polymorphic Type Checking Type checking guards: f :: t 1 -> t 2 -> -> t k -> t f p 1 p 2 p k g 1 = e 1 g j = e j Some expressions can t be reduced to a single type The expression s result could be one of several (or many) types Monomorphic type checking Each expression can be reduced to a single type, or declared type incorrect Polymorphic type checking Each expression is reduced to a set of possible types, or declared type incorrect 19 20

Type Checking in Haskell Consider the following function, f: Type Checking in Haskell Consider the following function, g: f (x,y) = (x, [ a.. y]) g m zs = m + length zs What can be inferred about its type? What can be inferred about its type? 21 22 Unification Unification identifies the set of types that satisfy two (or more) types What types can (a, [Char]) represent? Unification Unify the types (a, [a]) and ([b], c) What types can (Int, [b]) represent? Unify the types [Int] > [Int] and a > [a] What s the unification of (a, [Char]) and (Int, [b]) 23 24

Polymorphic Literals and Functions The same literal can appear multiple times in an expression It doesn t necessarily have the same type each time it occurs zip ([] ++ [1]) ([ a, b ] ++ []) Type Classes Type classes must also be considered when performing type checking Type classes are attached to type variables as necessary Unification ensures that the types identified are instances of the required classes Type class requirements are simplified where possible A type variable that includes requirements Eq a and Ord a is reduced to Ord a because Ord extends Eq Similarly, polymorphic functions can have a different type every time that they occur 25 26 Example Type check the following expression where f :: Eq a => a -> b -> Int: Example Type check the following expression where f :: Eq a => a -> b -> Int: f (succ. succ) c f c (succ. succ) 27 28

Some Final Thoughts on Type Checking Correct code is great! Incorrect code should either Not compile Crash when it is executed Incorrect code that runs and doesn t crash is a nightmare! Especially as the project gets larger 29