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

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

CS558 Programming Languages

COS 320. Compiling Techniques

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

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

Type Checking and Type Inference

CSCI-GA Scripting Languages

Functional Programming. Pure Functional Programming

Principles of Programming Languages

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

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

CSCE 314 Programming Languages. Type System

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

Motivation for typed languages

CS558 Programming Languages

CS558 Programming Languages

(Advanced) topics in Programming Languages

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

Programming Languages

Types and Type Inference

CMSC 330: Organization of Programming Languages

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

Introduction to Programming Using Java (98-388)

TYPES, VALUES AND DECLARATIONS

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

HANDLING NONLOCAL REFERENCES

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

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Simply-Typed Lambda Calculus

Formal Semantics of Programming Languages

Programming Languages and Compilers (CS 421)

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

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

22c:111 Programming Language Concepts. Fall Types I

CS313D: ADVANCED PROGRAMMING LANGUAGE

G Programming Languages - Fall 2012

301AA - Advanced Programming [AP-2017]

Intermediate Code Generation Part II

Programming Languages

Formal Syntax and Semantics of Programming Languages

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Types. What is a type?

MIDTERM EXAMINATION - CS130 - Spring 2005

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

CS 314 Principles of Programming Languages

Principles of Programming Languages 2017W, Functional Programming

Programming Languages

Formal Semantics of Programming Languages

Type Systems, Type Inference, and Polymorphism

CS558 Programming Languages

Semantic Analysis Type Checking

Programming Languages Lecture 15: Recursive Types & Subtyping

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

G Programming Languages - Fall 2012

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Compiler Construction

Functional Languages. Hwansoo Han

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Recursive Types and Subtyping

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

Software II: Principles of Programming Languages. Why Expressions?

Expressions and Assignment

Chapter 7. Expressions and Assignment Statements ISBN

A Small Interpreted Language

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

Types and Type Inference

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

Chapter 7. Expressions and Assignment Statements

Example Scheme Function: equal

Formal Systems and their Applications

Primitive Data Types: Intro

Functional Programming. Big Picture. Design of Programming Languages

Symbolic Computation and Common Lisp

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Pointers (continued), arrays and strings

Principles of Programming Languages

Short Notes of CS201

Example: Haskell algebraic data types (1)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

CS201 - Introduction to Programming Glossary By

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

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

MISRA-C. Subset of the C language for critical systems

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

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

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

Static Checking and Type Systems

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

Imperative languages

Pointers (continued), arrays and strings

(Not Quite) Minijava

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Transcription:

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

Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using standard machinery: inference rules from formal logic Understand the relationship between semantics and type systems And more generally, the role of types in programming languages 2

A Simple Subset of Lisp (with minor tweaks) <E> ::= const T F (IF <E> THEN <E> ELSE <E>) (INT <E>) (EQ <E> <E>) (PLUS <E> <E>) (LESS <E> <E>) const represents a numeric atom for a natural number (0, 1, 2, ) T and F are literal atoms for true and false (not like real Lisp) IF-THEN-ELSE is part of real Lisp. In our fake language, if the first expression evaluates to T, we evaluate the second expression; if the first expression evaluates to F, we evaluate the third expression; otherwise undefined INT, EQ, PLUS, LESS were defined in Project 3 3

Other Ways to Define the Syntax Inductive definition: the smallest set S such that { const, T, F } S if E S, then (INT E) S if E 1,E 2 S, then { (EQ E 1 E 2 ), (PLUS E 1 E 2 ), (LESS E 1 E 2 ) } S if E 1,E 2,E 3 S, then (IF E 1 THEN E 2 ELSE E 3 ) S Now the same thing, written as inference rules (formal logic) const S T S F S axioms (no premises) E S E 1 S E 2 S E 1 S E 2 S E 3 S (INT E) S (EQ E 1 E 2 ) S (IF E 1 THEN E 2 ELSE E 3 ) S 4 If we have established the premises (above the line), we can derive the conclusion (below the line)

Language Semantics Defined in Project 3 through function eval Running a program : expression evaluates to value, which is an atom T or F or const (IF F THEN 0 ELSE 31) evaluates to 31 (INT (PLUS 5 6)) evaluates to T Run-time error: when eval(e) is undefined (PLUS (EQ 1 2) 5) (IF 8 THEN 1 ELSE 2) Can we prevent some of these errors? 5

Type System and Type Checking A type system can help us prove that certain programs are good without running them If a program is well-typed, it will not go wrong at run time Guarantees are only for some run-time errors, not all: E.g. cannot assure the absence of division by zero or array index out of bounds or CAR applied to an empty list - they depend on particular values from a type But can catch PLUS with a boolean operand error 6

Typed Expressions Goal: without evaluating an expression, can we guarantee that its evaluation will not produce a runtime error? (static a.k.a. compile-time analysis) Solution: define types, and establish a relationship between expressions and types For our simple language Type Bool = set of all expressions that evaluate to T or F Type Nat = set of all expressions that evaluate to const To determine that an expression E has type T (i.e., E T), will only look at the structure of E but will not evaluate E 7

Typing Relation Relation : S { Bool, Nat } E : T is just another notation for E T T : Bool F : Bool const : Nat E 1 : Bool E 2 : T E 3 : T (IF E 1 THEN E 2 ELSE E 3 ) : T E 1 : T 1 E 2 : T 2 (EQ E 1 E 2 ) : Bool E : T (INT E) : Bool E 1 : Nat E 2 : Nat (PLUS E 1 E 2 ) : Nat E 1 : Nat E 2 : Nat (LESS E 1 E 2 ) : Bool 8

Example: Typing Derivation (IF (INT 5) THEN 6 ELSE (PLUS 7 8)) :? 5 : Nat (INT 5) : Bool 6 : Nat 7 : Nat 8 : Nat (PLUS 7 8) : Nat (IF (INT 5) THEN 6 ELSE (PLUS 7 8)) : Nat This structure is a derivation tree: the leaves are instances of axioms, the inner nodes are instances of non-axiom rules 9

More on the Typing Relation E is typable (or well-typed) if exists T such that E : T In our type system, each well-typed expression has one type; in general, an expression may have multiple types (e.g., when the type system has subtypes) Safety (a.k.a. soundness) of a type system: a welltyped program will not have a run-time error For our type system: for a well- typed E : T we know that eval(e) is defined and is a value of type T This property does not work in the other direction: an expression which is not well-typed may or may have a run-time error (conservative static analysis) (IF (INT 33) THEN 44 ELSE (PLUS T F)) is not well-typed but runs fine 10

Lists <E> ::= NIL (NULL <E>) (CONS <E> <E>) (CAR <E>) (CDR <E>) Semantics: a value now can also be a list value Either NIL, or CONS applied to a value and a list value Typing: need to add list types of the form List (T) Example: List (List (Nat)) Note that lists will be homogeneous all elements will have the same type. This is not the case for real Lisp lists there are heterogeneous. 11

Typing Relation NIL : List (T) for any T E 1 : T E 2 : List (T) (CONS E 1 E 2 ) : List (T) E : List (T) E : List (T) (CAR E) : T E : List (T) (CDR E) : List(T) (NULL E) : Bool Example 1: (CONS (NULL NIL) (CONS F NIL)) Example 2: (CONS F T) Example 3: (NULL NIL) Example 4: (CONS (NULL NIL) (CONS 8 NIL)) 12

Brief Overview of Terminology Polymorphism Statically vs dynamically typed languages Type safety vs language safety

Polymorphism Poly = many, morph = form A piece of code has multiple types Example 1: subtype polymorphism An expression has multiple types Typical for object-oriented languages (class Y extends X) Example 2: parametric polymorphism E.g. f(x)=x has types Bool Bool, Nat Nat, Use a type parameter T; define type type T T Generics in C++ and Java e.g. Map<K,V> ML and similar functional languages Example 3: coercion and overloading 14

15 Coercion and Overloading Automatic coercion (conversion) to another type is performed silently: e.g. in Java byte can be widened to short, int, long, float, or double E.g. in assignment conversion, the right-hand-side is converted to the type of the left-hand-side var E.g. numeric promotion converts operands of a numeric operator to a common type, e.g. for + Overloading: multiple definitions of the same name e.g. in Java name + has several types: double double double long long long double double long long float float float int int int float float int int

Terminology Statically typed language: expressions have static (compile-time) types, and we do static type checking Goal: prove the absence of certain type-related bad behaviors before running the program Declared types: C/C++/Java/ (programmer gives types) Inferred types: ML/Haskell no programmer-declared types; compiler infers, based on use in operators/functions Type safety: all bad behaviors of certain type-related kinds are excluded - e.g., Java, but not C (due to arbitrary typecasting in C) Dynamically typed language: run-time checks to catch type-related bad behaviors (e.g. Lisp, Perl) E.g., in our projects, PLUS must be applied to numbers 16

Terminology Want more than static type safety want language safety Cannot break the abstractions of the language (typerelated and otherwise); e.g. no buffer overflows, seg faults, return address overriding, garbage values, etc. Example: C is unsafe for many reasons, one of which is the lack of type safety: e.g., double pi = 3.14; int* ptr = (int*) π int x = *ptr; Other reasons: null pointers lead to seg faults (OS concept, not PL concept); buffer overflows lead to stack smashing & garbage values Example: Java is safe combination of static type safety & run-time checks. Static type safety ensures that an well-typed program will not do type-related bad things. Run-time checks catch things that cannot be caught statically via types: null pointers, array index out of bounds, etc. Example: Lisp is safe checks for type-related correctness ( operands of PLUS must be numbers ) and special bad values (e.g. divide by 0) 17