CS558 Programming Languages

Similar documents
CS558 Programming Languages

CS558 Programming Languages

CS558 Programming Languages

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

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

Principles of Programming Languages

CS558 Programming Languages

Programming Languages Lecture 14: Sum, Product, Recursive Types

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

CS558 Programming Languages. Winter 2013 Lecture 3

Types and Type Inference

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

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

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

Types, Type Inference and Unification

6.001 Notes: Section 6.1

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

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

CS558 Programming Languages

Types and Type Inference

CS558 Programming Languages

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

Principles of Programming Languages

G Programming Languages - Fall 2012

CS 314 Principles of Programming Languages

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

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

The Substitution Model

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

CSCI-GA Scripting Languages

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

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

Principles of Programming Languages

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Chapter 5 Names, Binding, Type Checking and Scopes

CS558 Programming Languages

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

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Programming Languages

Simply-Typed Lambda Calculus

CS321 Languages and Compiler Design I. Fall 2013 Week 8: Types Andrew Tolmach Portland State University

1 Introduction. 3 Syntax

Com S 541. Programming Languages I

NOTE: Answer ANY FOUR of the following 6 sections:

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

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Types. What is a type?

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

CSE 431S Type Checking. Washington University Spring 2013

Programming Languages, Summary CSC419; Odelia Schwartz

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

CS558 Programming Languages

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

COS 140: Foundations of Computer Science

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

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

Final-Term Papers Solved MCQS with Reference

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

The PCAT Programming Language Reference Manual

The SPL Programming Language Reference Manual

Compiler Construction

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

CS558 Programming Languages Winter 2013 Lecture 8

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

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

Comp 333: Concepts of Programming Languages Fall 2016

CSE 307: Principles of Programming Languages

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Lecture Notes on Aggregate Data Structures

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

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

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

References and pointers

Lecture 16: Static Semantics Overview 1

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

VARIABLES AND TYPES CITS1001

CS 3360 Design and Implementation of Programming Languages. Exam 1

Lecture 15 CIS 341: COMPILERS

Introduction to OCaml

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

Chapter 5. Names, Bindings, and Scopes

Type Checking and Type Equality

The Substitution Model. Nate Foster Spring 2018

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Ruby: Introduction, Basics

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

Weeks 6&7: Procedures and Parameter Passing

Fundamentals of Programming Languages. Data Types Lecture 07 sl. dr. ing. Ciprian-Bogdan Chirila

MIDTERM EXAMINATION - CS130 - Spring 2005

CS 3360 Design and Implementation of Programming Languages. Exam 1

LECTURE 17. Expressions and Assignment

Lecture Notes on Programming Languages

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

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

COS 140: Foundations of Computer Science

Special Topics: Programming Languages

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Transcription:

CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016

Values and Types We divide the universe of values according to types A type is a set of values and a set of operations on them How is each value represented? How is each operation implemented? Integers with +,-,etc. machine integer HW instruction Arrays with read,update contiguous block of memory address arithmetic + indirect addressing Booleans with &,,~ machine bit or byte HW instruction/sequence Functions with apply closures jsr instruction

Atomic vs Constructed Types Atomic (or primitive) types are those whose values cannot be taken apart or constructed by user code Can only manipulate using built-in language operators Typically includes the types that have direct hardware support e.g., integers, floats, pointers, instructions Composite types are built from other types using type constructors User code can construct values, inspect/modify internals E.g. arrays, records, unions, function

Static Type Checking High-level languages differ from machine code in that explicit types appear and type violations are usually caught at some point Static type checking is probably most common: FORTRAN, Algol, Pascal, C/C++, Java, Scala, etc. Types are associated with identifiers (variables, parameters, functions) Every use of an identifier can be checked for type-correctness before program is run Well-typed programs don t go wrong (if type system is sound) Compilers can generate efficient code because it knows how each value is represented Type declarations provide useful documentation for code

Dynamic Type Checking Dynamic type checking occurs in LISP, Smalltalk, Python, JavaScript, many other scripting languages Types are attached to values (usually as explicit tags) The type associated with an identifier can vary Correctness of operations can t (in general) be checked until run time Type violations become checked run-time errors Generating optimized code and value representations is hard Programs can be harder to read

Static Type Systems Main goal of a type system is to characterize programs that won t go wrong" at runtime Informally, we want to avoid programs that confuse types, e.g. by trying to add booleans to reals, or take the square root of a string Formally, we can give a set of typing rules (sometimes called static semantics) from which we can derive typing judgments about programs Program is well-typed if-and-only-if we can derive a typing judgment for it This should sound familiar!

Typing Judgments Each judgment has the form TE e : t Intuitively this says that expression e has type t, under the assumption that the type of each free variable in e is given by the type environment TE. We write TE(x) for the result of looking up x in TE,andTE + {x t} for the type environment obtained from TE by extending it with a new binding from x to t.

Rules for a simple language TE(x) =t TE x : t (Var) TE i : Int (Int) TE e 1 : Int TE e 2 : Int TE (+ e 1 e 2 ) : Int (Add) TE e 1 : t 1 TE + {x t 1 } e 2 : t 2 TE (let x e 1 e 2 ) : t 2 (Let) TE(x) =t TE e : t TE (:= x e) : t (Assgn) TE e 1 : Bool TE e 2 : t TE e 3 : t TE (if e 1 e 2 e 3 ) : t (If) TE e 1 : Int TE (<= e 1 TE e 2 : Int e 2 ) : Bool (Leq) TE e 1 : Bool TE e 2 : t TE (while e 1 e 2 ) : Int (While) Assumes just two types: Int and Bool Matches language we used for dynamic semantics before

Formalizing Type Safety The typing rules are just (another) formal system in which judgments can be derived. How do we connect this system with our slogan that welltyped programs don t go wrong? First we need an auxiliary judgment system assigning types to values, written = v : t. For example, we would have = i : Int for every integer i, = true : Bool, and = false : Bool We also add a special value error which does not belong to any type: = error : t We extend this notation to environments and stores, and write = E,S : TE iff dom(e) =dom(te) and = S(E(x)) : TE(x), x dom(e).

Dynamic Semantics Revisited Recall our formal dynamic semantics for this language, using judgments like this: e, E, S v, S Previously, we said expressions corresponding to runtime errors simply had no applicable rule (they were stuck ) Let s change the system slightly by adding new rules so that all expressions corresponding to runtime errors evaluate to error instead

Type Safety Theorem Now, if everything has been defined correctly (our type system is sound), we should be able to prove a theorem roughly like this: If TE e : t and = E,S : TE and e, E, S v, S then = v : t. In other words, well-typed programs evaluate to values of the expected type; so in particular, they can t evaluate to error, which belongs to no type.

Static Type Checking We can turn the typing rules into a recursive typechecking algorithm A type checker is very similar to the evaluators we have already built: It is parameterized by a type environment It dispatches according to the syntax of the expression being checked (note there is exactly one rule for each expression form) It calls itself recursively on sub-expressions

Static Type Checking (2) But there are some differences: Type checker returns a type, not a value It must examine every possible execution path, but just once e.g. it examines both arms of a conditional expression (not just one) e.g. if our language has functions, it processes the body of each function only once, no matter how many places the function is called from Most languages require the types of function parameters and return values to be declared explicitly. The type checker can use this info to check separately that applications of the function are correctly typed and that the body of the function is correctly typed.