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

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

Static Checking and Type Systems

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSCE 314 Programming Languages. Type System

Lexical Considerations

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

Intermediate Code Generation Part II

1 Lexical Considerations

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

CS558 Programming Languages

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

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

Semantic Analysis. Lecture 9. February 7, 2018

Lexical Considerations

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

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

COS 320. Compiling Techniques

Type Systems, Type Inference, and Polymorphism

G Programming Languages - Fall 2012

22c:111 Programming Language Concepts. Fall Types I

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

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Semantic Analysis and Type Checking

Lecture #23: Conversion and Type Inference

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

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

Types. What is a type?

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

Programmiersprachen (Programming Languages)

CSCI312 Principles of Programming Languages!

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Undergraduate Compilers in a Day

Principles of Programming Languages [PLP-2015] Detailed Syllabus

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Type Checking. Error Checking

Programming Languages Third Edition. Chapter 7 Basic Semantics

The compilation process is driven by the syntactic structure of the program as discovered by the parser

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

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Types and Type Inference

A Short Summary of Javali

Types and Type Inference

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Type systems. Static typing

Lecture 12: Data Types (and Some Leftover ML)

Types and Static Type Checking (Introducing Micro-Haskell)

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

Appendix Set Notation and Concepts

Fundamentals of Programming Languages

CS558 Programming Languages

HANDLING NONLOCAL REFERENCES

Introduction to Programming Using Java (98-388)

Compilers and computer architecture: Semantic analysis

Intermediate Code Generation

CMSC 330: Organization of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Type Checking and Type Inference

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Review of the C Programming Language

Programming Languages, Summary CSC419; Odelia Schwartz

5. Semantic Analysis!

Motivation was to facilitate development of systems software, especially OS development.

CS558 Programming Languages

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

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

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

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

The New C Standard (Excerpted material)

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Chapter 9. Subprograms

CSCI-GA Scripting Languages

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

Polymorphism and Type Inference

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

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

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

Compiler Construction I

Lexical Analysis. Introduction

Lecture 16: Static Semantics Overview 1

Operational Semantics. One-Slide Summary. Lecture Outline

CA Compiler Construction

C: How to Program. Week /Mar/05

Lexical and Syntax Analysis

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Types, Type Inference and Unification

Last class. CS Principles of Programming Languages. Introduction. Outline

ECE251 Midterm practice questions, Fall 2010

Programming Languages Lecture 14: Sum, Product, Recursive Types

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Functional Programming. Big Picture. Design of Programming Languages

Transcription:

Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review topics today Wednesday (Oct 28 th ) Probably no class maybe new programming assignment Midterm reviews https://www.eecs.tufts.edu/cscourse/feedback.php Tufts University Computer Science 2 Midterm topics Lexical analysis, scanning Regular expressions DFAs and NFAs Conversion from RE to NFA to DFA Parsing Grammars, precedence and associativity Top-down parsers, LL grammars, First and Follow sets Bottom-up parsers, LR parsing, shifting and reducing LR items, viable prefix DFA, closure operation Shift/reduce and reduce/reduce conflicts Static checks Uniqueness checks Symbol tables Types Today: type checking Big topic Type systems Type inference Non-standard type systems for program analysis Theory of type systems Focus Role of types in compilation Imperative and object-oriented languages What is a type? Def: A type is a collection of values and a set of operations on those values Tufts University Computer Science 3 Tufts University Computer Science 4 Purpose of types Identify and prevent errors Avoid meaningless or harmful computations Meaningless: (x < 6) + 1 bathtub Harmful? Program organization and documentation Separate types for separate concepts Type indicates programmer intent Support implementation Allocate right amount of space for variables Select right machine operations Optimization: e.g., use fewer bits when possible Key idea: types can be checked Tufts University Computer Science 5 Type errors Problem: Underlying memory has no concept of type Everything is just a string of bits: 0100 0000 0101 1000 0000 0000 0000 0000 The floating point number 3.375 The 32-bit integer 1,079,508,992 Two 16-bit integers 16472 and 0 Four ASCII characters: @ X NUL NUL! Without type checking: Machine will let you store 3.375 and later load 1,079,508,992 Violates the intended semantics of the program Tufts University Computer Science 6 1

Type system Idea: Provide clear interpretation for bits in memory Imposes constraints on use of variables, data Expressed as a set of rules Automatically check the rules Report errors to programmer Key questions: What types are built into the language? Can the programmer build new types? What are the typing rules? When does type checking occur? How strictly are the rules enforced? When are checks performed? What do you think the choices are? Static and dynamic Statically typed languages Types of all variables are determined ahead of time Examples? Dynamically typed languages Type of a variable can vary at run-time Examples? Our focus? Static typing corresponds to compilation Tufts University Computer Science 7 Tufts University Computer Science 8 Expressiveness Consider this Scheme function: (define myfunc (lambda (x) (if (list? x) (myfunc(first x)) (+ x 1)) What is the type of x? Sometimes a list, sometimes an atom Downside? What would happen in static typing? Cannot assign a type to x at compile time Cannot write this function Static typing is conservative Tufts University Computer Science 9 Types and compilers What is the role of the compiler? Example: we want to generate code for a = b + c * d; arr[i] = *p + 2; What does the compiler need to know? Duties: Enforce type rules of the language Choose operations to be performed Can we do this in one machine instruction? Provide concrete representation bits Next time: where is the storage? What if can t perform the check at compile-time? Tufts University Computer Science 10 Type checks What is a type? More importantly: why do I care? From Types and Programming Languages 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. Idea Divide possible values into groups types Disallow certain behaviors based on membership Type systems From language specifications: The result of a unary & operator is a pointer to the object referred to by the operand. If the type of the operand is Τ, the type of the result is pointer to Τ. If both operands of the arithmetic operators addition, subtraction and multiplication are integers, then the result is an integer Tufts University Computer Science 11 Tufts University Computer Science 12 2

Properties of types These excerpts imply: Types have structure Pointer to T and Array of Pointer to T Expressions have types Types are derived from operands by rules Goal: determine types for all parts of a program Type expressions (Not to be confused with types of expressions) Build a description of a type from: Basic types also called primitive types Vary between languages: int, char, float, double Type constructors Functions over types that build more complex types Type variables Unspecified parts of a type polymorphism, generics Type names An alias for a type expression typedef in C Tufts University Computer Science 13 Tufts University Computer Science 14 Type constructors Arrays If Τ is a type, then array(τ) is a type denoting an array with elements of type Τ May have a size component: array(i,τ) Products or records If T 1 and T 2 are types, then T 1 T 2 is a type denoting pairs of two types May have labels for records/structs ( name, char *) ( age, int) Type constructors Pointers If Τ is a type, the pointer(τ) denotes a pointer to T Functions or function signatures If D and R are types then D R is a type denoting a function from domain type D to range type R For multiple inputs, domain is a product Notice: primitive operations have signatures Mod % operator: int int int Tufts University Computer Science 15 Tufts University Computer Science 16 Example Static type checker for C Defined over the structure of the program Rules: Expression E 1 + E 2 if type(e 2 ) is int and type(e 1 ) is int result type is int else other cases Question: How do we get declared types of identifiers, functions? More examples More interesting cases Rules: Expression E 1 [ E 2 ] * E if type(e 2 ) is int and type(e 1 ) is array(t) result type is T if type(e) is pointer(t) result type is T Tufts University Computer Science 17 Tufts University Computer Science 18 3

Example What about function calls? Consider single argument case Expression E 1 ( E 2 ) if type(e 1 ) is D R and type(e 2 ) is D result type is R How do we perform these checks? What is the core type-checking operation? How do I determine if type(e) is D? If two type expressions are equivalent then Type equivalence Implementation: structural equivalence Same basic types Same set of constructors applied Recursive test: function equiv(s, t) if s and t are the same basic type return true if s = pointer(s 1 ) and t = pointer(t 1 ) return equiv(s 1,t 1 ) if s = s 1 s 2 and t = t 1 t 2 return equiv(s 1,t 1 ) && equiv(s 2,t 2 ) etc Tufts University Computer Science 19 Tufts University Computer Science 20 Representation Represent types as graphs Node for each type Often a DAG: share the structure when possible char int pointer int char Function: (char int) int * pointer Tufts University Computer Science 21 int Structural equivalence Efficient implementation Recursively descend DAG until common node Many subtle variations in practice Special rules for parameter passing C: array T[] is compatible with T* Pascal, Fortran: leaving off size of array Is size part of the type? Type qualifiers: const, static, etc. Expr E 1 = E 2 ; if type(e 1 ) == type(e 2 ) result type is E 1 Tufts University Computer Science 22 Notions of equivalence Different way of handling type names Structural equivalence Ignores type names typedef int * numptr means numptr int * Not always desirable Example? Name equivalence Types are equivalent if they have the same name Solves an important problem: recursive types Recursive types Why is this a problem? struct cell { int info; struct cell * next; } Cycle in the type graph! C uses structural equivalence for everything except structs (and unions) The name struct cell is used instead of checking the actual fields in the struct Can we have two compatible struct definitions? Tufts University Computer Science 23 Tufts University Computer Science 24 4

Java types Type equivalence for Java class Foo { class Bar { int x; int w; float y; float z; } } Can we pass Bar objects to a method taking a type Foo? No Java uses name equivalence for classes What can we do in C that we can t do in Java? Type checking Consider this case: What is the type of x+i Is this an error? if x is float and i is int Compiler fixes the problem Convert into compatible types Automatic conversions are called coercions Rules can be complex in C, large set of rules for called integral promotions Goal is to preserve information Tufts University Computer Science 25 Tufts University Computer Science 26 Type coercions Rules Find a common type Add explicit conversion into the AST Expression E 1 + E 2 if type(e 1 ) is int and type(e 2 ) is int result type is int if type(e 1 ) is int and type(e 2 ) is float result type is float if type(e 1 ) is float and type(e 2 ) is int result type is float etc Implementing type checkers Expression E E 1 [ E 2 ] E * E if type(e 2 ) is int and type(e 1 ) is array(t) type(e) = T if type(e) is pointer(t) type(e) is T Does this form look familiar? Type checking fits into syntax-directed translation What kind of attribute is type? Tufts University Computer Science 27 Tufts University Computer Science 28 Interesting cases What about printf? printf(const char * format, ) Implemented with varargs Format specifies which arguments should follow Who checks? Array bounds Array sizes rarely provided in declaration Cannot check statically (in general) There are fancy-dancy systems that try to do this Java: check at run-time Tufts University Computer Science 29 Overloading + operator Same syntax, same semantics, multiple implementations C: float versus int C++: arbitrary user implementation Note: cannot change parser what does that mean? How to decide which one? Use types of the operands Find operator with the right type signature Complex interaction with coercions Need a rule to choose between conversion and overloading Tufts University Computer Science 30 5

Object oriented types class Foo {. } class Bar extends Foo { } What is relationship between Foo and Bar? Bar is a subtype of Foo Any code that accepts a Foo object can also accept a Bar object We ll talk about how to implement this later Modify type compatibility rules To check an assignment, check subtype relationship <= Also for formal parameters Expr E 1 = E 2 ; if type(e 2 ) <= type(e 1 ) result type is E 1 Java arrays class Foo {. } class Bar extends Foo { } Foo[] foo_array; Bar[] bar_array; Question: is bar[] a subtype of foo[]? Answer: yes Consequences? void storeit(foo f, Object [] arr) { arr[0] = f; } How do we perform this check? Tufts University Computer Science 31 Tufts University Computer Science 32 Polymorphism Ordinary procedures Accept fixed type signature Example: search(char c, string s) Generic procedures Work on arguments of different types User-defined generics Define interface with type variables Example: search<el>(el c, El [] list) Tufts University Computer Science 33 Type checking polymorphism Problem: How do we check generics statically? int search<el>(el x, El [] list) { for (int i = 0; i < list.length; i++) if (x.compareto(list[i])) return i; return -1; }... search(5, A); // Use of generic What do we need to know? Does type El have a method compareto? What is type El in this instantiation? Is A an array of type int? Tufts University Computer Science 34 Type checking polymorphism Checking generic code: C++ templates Checked at instantiation Plug in the type variables Compile and check resulting code Java generics Constrain type variables Declare type El must implement interface X Can check generic code independent of use Type checking polymorphism Checking uses of generics Is search(5, A) correct? (Let s say int A[20];) Find a mapping from 5 and A to El and El [ ] Mapping: El is int This process is called unification Unification Given two type expressions, one with type variables Find a substitution of type variables that turns it into the other type expression Used in many other areas: theorem proving, Prolog Tufts University Computer Science 35 Tufts University Computer Science 36 6

Polymorphism Overview How is polymorphism implemented? Type erasure Convert all type variables into Object Java approach Pros and cons? Instantiation Generate a new implementation for each combination of type arguments C++ approach Pros and cons? Type system Set of rules for assigning types to parts of the program Inference rules for operators Type checker Start with declared types Apply typing rules At each step, make sure resulting types obey the rules of the language Why type checking? Alert the programmer to errors Help compiler generate a correct translation Tufts University Computer Science 37 Tufts University Computer Science 38 Type inference Languages without declarations ML, Haskell Still statically typed Types determined by use Requires type inference algorithm Determine constraints from program Example: x + y implies x and y must be numeric types Results in type expressions with type variables Example: x <= number and z <= x Compute a consistent assignment to variables Static checking Question: what can be statically checked? What are the limits? What kinds of things might we want to check? In general: undecidable Asking questions about the possible behavior of a program Often degenerates into the halting problem Static check is an approximation Properties: Soundness Completeness We ll come back to this in dataflow analysis Tufts University Computer Science 39 Tufts University Computer Science 40 Back to Mars Orbiter Language support for units Idea: make units a part of the type Use polymorphism for operators Type-check the computations Example: double<kg> weight; double<s> time; double<m> distance; double<m s^-2> gravity = 9.8 * m/(s * s); double<kg m s^-2> force = weight * gravity; Issues: Type checker must understand algebra Generics are tricky (what is the type of sqrt?) Template metaprogramming C++ templates are very powerful More powerful than the designers realized! Template parameters can be primitive types Must be compile-time constants template<int N> class Vector { int the_data[n];... } Partial instantiation Can provide partial parameters Compiler chooses the best unification template<1> class Vector { int one_piece_of_data;... } Tufts University Computer Science 41 Tufts University Computer Science 42 7

Template metaprogramming Loops (recursion) template<bool C> class IfThen { }; class IfThen<true> { public: static inline void do() { statement1; } // true case }; class IfThen<false> { public: static inline void do() { statement2; } // false case }; // Replacement for 'if/else' statement: IfThen<condition>::do(); class Factorial<1> { public: enum { value = 1 }; }; template<int N> class Factorial { public: enum { value = N * Factorial<N-1>::value }; }; x = Factorial<8>::value; Tufts University Computer Science 43 Tufts University Computer Science 44 8