Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

Similar documents
Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

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

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

The role of semantic analysis in a compiler

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Semantic Analysis. Lecture 9. February 7, 2018

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Scoping and Type Checking

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

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

CS558 Programming Languages

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Overview of Semantic Analysis. Lecture 9

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

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

CS558 Programming Languages

Lexical Considerations

1 Lexical Considerations

CS558 Programming Languages

G Programming Languages - Fall 2012

CS558 Programming Languages

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

Lexical Considerations

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

CSE 374 Programming Concepts & Tools

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

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

Monads. Lecture 12. Prof. Aiken CS 264 Lecture 12 1

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

CS31 Discussion 1E Spring 17 : week 08

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

QUIZ. What is wrong with this code that uses default arguments?

Semantic Analysis and Type Checking

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

Project Compiler. CS031 TA Help Session November 28, 2011

Pace University. Fundamental Concepts of CS121 1

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

Lecture 2: SML Basics

CMSC 330: Organization of Programming Languages

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

VARIABLES AND TYPES CITS1001

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

CS558 Programming Languages

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

. p.1/23. Today. 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS11 Advanced C++ Fall Lecture 7

Java Bytecode (binary file)

More Lambda Calculus and Intro to Type Systems

CSCE 314 Programming Languages. Type System

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Static Analysis of C++ Projects with CodeSonar

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Type systems. Static typing

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

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CMSC 330: Organization of Programming Languages

Intermediate Code Generation

Simply-Typed Lambda Calculus

CSCI-GA Scripting Languages

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CS Lecture 19: Loop invariants

Outline. When we last saw our heros. Language Issues. Announcements: Selecting a Language FORTRAN C MATLAB Java

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

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Computer Components. Software{ User Programs. Operating System. Hardware

The Environment Model

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

Announcements. Written Assignment 2 due today at 5:00PM. Programming Project 2 due Friday at 11:59PM. Please contact us with questions!

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Types and Type Inference

COS 320. Compiling Techniques

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

6.096 Introduction to C++

CS61C : Machine Structures

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

Dynamic Types , Spring March 21, 2017

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Semantics of programming languages

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Type Checking and Type Equality

Recap: Functions as first-class values

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

Topic 1: Introduction

Lecture 3: Recursion; Structural Induction

Syntax Errors; Static Semantics

Induction and Semantics in Dafny

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Comp215: Thinking Generically

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

An introduction introduction to functional functional programming programming using usin Haskell

COMP520 - GoLite Type Checking Specification

Transcription:

Announcements Types Working on requirements this week Work on design, implementation Lecture 17 CS 169 Prof. Brewer CS 169 Lecture 16 1 Prof. Brewer CS 169 Lecture 16 2 Outline Type concepts Where do types come from and why? Introduction to type checking Then: Detecting races using type checking Are type casts a necessary evil? Java Types The types are just class names And a few types that aren t classes Arrays, interfaces, basic types (int, bool, etc.) The user declares types for identifiers The compiler infers types for expressions Infers a type for every expression Prof. Brewer CS 169 Lecture 16 3 Prof. Brewer CS 169 Lecture 16 4 C Types C Types have structure Base types int, char, float Compound types T ::= Array[T] struct{l: T,, L: T pointer(t) Ugly C syntax: int[], const char *, What Are Types? Types are specifications Explains the intended interpretation of a bit pattern Int It s a 32-bit integer Char It s an ASCII character The same bit pattern can be interpreted either way Without types we don t know what the programmer wanted Misuse of types can be detected Stops you from making many simple mistakes Prof. Brewer CS 169 Lecture 16 5 Prof. Brewer CS 169 Lecture 16 6 1

Analogy: Plugs and Sockets Lots of different kinds of electrical plugs/sockets Evolved to prevent electrocution/fires from mismatched electrical components Can t plug a 9V wire into a 220V outlet Why Do We Need Type Systems? Consider the assembly language fragment addi $r1, $r2, $r3 What are the types of $r1, $r2, $r3? No guarantee the whole system works, but at least gross power errors are prevented Prof. Brewer CS 169 Lecture 16 7 Prof. Brewer CS 169 Lecture 16 8 Types and Operations Certain operations are legal for values of each type It does make sense to add two integers It doesn t make sense to add a function pointer and an integer in C But both have the same assembly language implementation! Prof. Brewer CS 169 Lecture 16 9 Type Systems A language s type system specifies which operations are valid for which types The goal of type checking is to ensure that operations are used with the correct types Enforces intended interpretation of values Type systems provide a concise formalization of the semantic checking rules Prof. Brewer CS 169 Lecture 16 10 What Can Types do For Us? Type Checking Overview Can detect certain kinds of errors Memory errors: Reading from an invalid pointer, etc. Violation of abstraction boundaries: class FileSystem { open(x : String) : File { class Client { bar(fs : FileSystem) { File f <- fs.open( foo ) // Client cannot see inside f! Prof. Brewer CS 169 Lecture 16 11 Three kinds of languages: Untyped: No type checking (machine code) Dynamically typed: Almost all checking of types is done as part of program execution (Scheme) Statically typed: All or almost all checking of types is done as part of compilation (C, Java) Prof. Brewer CS 169 Lecture 16 12 2

Untyped Languages Some languages have no type system Bit patterns have no interpretation except in the mind of the programmer Assembly code But even here there is usually an int/float distinction Dynamically Typed Languages Type checking at runtime is simple Just check that operators are applied to arguments of the expected type + checks that its arguments are numbers reverse checks that its argument is a list Prof. Brewer CS 169 Lecture 16 13 Prof. Brewer CS 169 Lecture 16 14 Dynamic Typing: Implications This has performance implications All data must carry type tags at runtime All operations must check the tags Generally a 25% performance overhead This has correctness implications Type errors are detected late: during execution Errors arise in awkward places Example: add(a,b) = a + b Cannot catch bad data at function interface Error will arise deep within the logic of the function body Dynamic Typing: Benefits Some programming styles are much easier in a dynamically typed language Best example: eval Read a String s from the network Call eval(s) eval interprets the string as a program and runs it Expressive, and dangerous! No good way to do this in a statically typed language Except if checking types at load-time (e.g., class loading) Prof. Brewer CS 169 Lecture 16 15 Prof. Brewer CS 169 Lecture 16 16 The Type Wars Competing views on static vs. dynamic typing Dynamic typing proponents say: Static type systems are restrictive Rapid prototyping easier in a dynamic type system Static typing proponents say: Static checking catches many programming errors at compile time Avoids overhead of runtime type checks Static Typing Check types at compile time One statement/function/module at a time Compiler guarantees types will never be violated during program execution Can find bugs even before we run code Most common today: C, C++, Java Prof. Brewer CS 169 Lecture 16 17 Prof. Brewer CS 169 Lecture 16 18 3

Expressions and Statements Type Checking and Type Inference Consider the following language of expressions and statements (like in C or Java): expressions E ::= n x false E 1 + E 2 not E 1 types T := int bool statements S ::= x := E while(e) S {S 1 ; ; S n { T x; S if(e) S 1 else S 2 Prof. Brewer CS 169 Lecture 16 19 Type checker can be written as a recursive function that scans the program text Type checking: check(e, T) Return true if E has type T, and error if E is illtyped or has some other type Type inference: infer(e) returns the type of E, or error if E is ill-typed the actual workhorse check(e, T) = if infer(e) == T then true else error Prof. Brewer CS 169 Lecture 16 20 Rules of Inference Type inference is easy for constants infer(n) = int infer(false) = bool Type inference for composed constructs is recursive infer(e 1 + E 2 ) = int if check(e 1, int) && check(e 2, int) infer(not E 1 ) = bool if check(e 1, bool) Type Inference for Variables How can we infer the type of a variable? infer(x) =? Idea: we can keep track of declarations as we scan the program Use a second argument to infer and check V is a set of pairs of variables and their types A variable environment Infer(V,E) = T Means E has type T, assuming variables are declared as specified by V Prof. Brewer CS 169 Lecture 16 21 Prof. Brewer CS 169 Lecture 16 22 Typing for Expressions. Revisited. infer(v, n) = int infer(v, false) = bool infer(v, E 1 + E 2 ) = int if check(v, E 1, int) && check(v, E 2, int) infer(v, not E) = bool if check(v, E 1, bool) infer(v, x) = T if (x, T) V infer(v, E) = error otherwise! Examples of Ill-Typed Expressions The following invocations result in error. Why? infer(v, false + 1) infer(v, not 1) infer({ (x, bool), x + 1) infer({ (y, int), x + 1) Prof. Brewer CS 169 Lecture 16 23 Prof. Brewer CS 169 Lecture 16 24 4

Typing for Statements We define a scheck(v, S) function: scheck(v, x := E) if (x, T) V && check(v, E, T) scheck(v, if(e) S 1 else S 2 ) if check(v, E, bool) && scheck(v, S 1 ) && scheck(v, S 2 ) scheck(v, while(e) S) if check(v, E, bool) && scheck(v, S) scheck(v, { S 1 ; ; S n ) if scheck(v, S i ) for i =1,, n Prof. Brewer CS 169 Lecture 16 25 Local Variable Declarations Our language has static scope A variable name refers to the closest enclosing variable declaration Example: { int x; x = 1; { bool x; x := false ; x = x + 1 Type checker must keep track of scope Prof. Brewer CS 169 Lecture 16 26 Local Variable Declarations scheck(v, { T x; S ) = If scheck(v 1, S), where V 1 = V (x, ) + (x, T) We write simply V 1 = V + (x, T) We check S with a modified set of variables We replace the old declaration of x Or just add a declaration if none was present Type Checking Example Code Type env. Types scheck(v, { T 0 x; scheck(v+(x,t 0 ), scheck(v+(x,t 0 ), { T 1 y; x = scheck(v+(x,t 0 ), { T 2 x; ; Think how scheck works on previous example infer(v+(x,t 0 )+(y,t 1 ), E x, y = T 0 infer(v+(x,t 2 ), Fx, y = int infer(v+(x,t 0 )+(y,t 1 ), y = T 1 Prof. Brewer CS 169 Lecture 16 27 Prof. Brewer CS 169 Lecture 16 28 Notes The type environment gives types to the identifiers in scope The type environment is passed down the expression from the root towards the leaves Types are computed up the expression from the leaves towards the root Know Your Type System: Const You have to really learn the language to know how to use the type system What you don t know can t help you In C, learn to use const Data declared const is read-only Very useful for documenting and checking update policies in APIs Not as widely used as it should be Prof. Brewer CS 169 Lecture 16 29 Prof. Brewer CS 169 Lecture 16 30 5

Static Type Systems Summary General themes Type rules are defined on the structure of expressions One rule for each kind of expression Types of variables are modeled by an environment Most rules are painfully obvious It is the systematic use of the rules on large programs that is beneficial Type checking is conservative Prof. Brewer CS 169 Lecture 16 31 Type Systems for the Software Engineer Types are one of the most useful tools available to programmers If you don t use them, they won t help you Helps to know a bit about how they work! Can use typecking-like techniques for checking other useful properties Prof. Brewer CS 169 Lecture 16 32 Summary Types are the most successful specifications we have Formal Part of the code Automatically checked Use your type system! Spend the time to really learn the language It can help you Choice of statically or dynamically typed languages Static typing preferred Some problems more dynamic than others Prof. Brewer CS 169 Lecture 16 33 6