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

Similar documents
Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CSCI-GA Scripting Languages

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

CSCE 314 Programming Languages. Type System

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

Topic 9: Type Checking

Topic 9: Type Checking

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

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

CS558 Programming Languages

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

Chapter 5. Names, Bindings, and Scopes

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

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

Type Checking and Type Inference

CS558 Programming Languages

Semantic Analysis. Lecture 9. February 7, 2018

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

CS558 Programming Languages

COS 320. Compiling Techniques

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

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

CS558 Programming Languages

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

Programming Languages, Summary CSC419; Odelia Schwartz

CSC324 Principles of Programming Languages

CMSC 330: Organization of Programming Languages

Lecture 15 CIS 341: COMPILERS

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

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

Denotational Semantics. Domain Theory

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

Simply-Typed Lambda Calculus

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

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


Compilers and computer architecture: Semantic analysis

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

SSA Based Mobile Code: Construction and Empirical Evaluation

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

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

Principles of Programming Languages

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

Types and Type Inference

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

CS558 Programming Languages

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

Software II: Principles of Programming Languages. Why Expressions?

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

CS558 Programming Languages

CSI32 Object-Oriented Programming

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

Semantic Analysis Type Checking

Writing Evaluators MIF08. Laure Gonnord

Syntax and Grammars 1 / 21

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

Chapter 9 Subprograms

Monads. Functional Programming (CS4011) Monads

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

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

Types and Type Inference

Compiler Passes. Semantic Analysis. Semantic Analysis/Checking. Symbol Tables. An Example

Chapter 5 Names, Binding, Type Checking and Scopes

Project Compiler. CS031 TA Help Session November 28, 2011

A Practical Optional Type System for Clojure. Ambrose Bonnaire-Sergeant

Programming Languages and Compilers (CS 421)

Compiler Construction

CMSC 330: Organization of Programming Languages. Lambda Calculus

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

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

CS4215 Programming Language Implementation

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Matching Logic A New Program Verification Approach

Assignment 4: Semantics

Structure of Programming Languages Lecture 10

Code BEAM SF Hype For Types. Using Dialyzer to bring type checking to your Elixir code

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Types, Type Inference and Unification

(Advanced) topics in Programming Languages

Lecture 4 Memory Management

CSE P 501 Compilers. Static Semantics Hal Perkins Spring UW CSE P 501 Spring 2018 I-1

Programming Languages Fall 2013

MY GOOD FRIEND RUST. Matthias Endler trivago

Matching Logic. Grigore Rosu University of Illinois at Urbana-Champaign

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

CSE 307: Principles of Programming Languages

Name, Scope, and Binding. Outline [1]

CS558 Programming Languages

Pointers II. Class 31

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

CMSC 330: Organization of Programming Languages

Adam Chlipala University of California, Berkeley ICFP 2006

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

Transcription:

Types 1 / 15

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

Types and type errors Type: a set of syntactic terms (ASTs) that share the same behavior Int, Bool, String, Maybe Bool, [[Int]], Int -> Bool defines the interface for these terms in what contexts can they appear? Type error: occurs when a term cannot be assigned a type typically a violation of the type interface between terms if not caught/prevented, leads to a crash or unpredictable evaluation Introduction 3 / 15

Type safety A type system detects and prevents/reports type errors A language is type safe if an implementation can detect all type errors statically: by proving the absence of type errors dynamically: by detecting and reporting type errors at runtime Type safe languages Haskell, SML static Python, Ruby dynamic Java mixed Unsafe languages C, C++ PHP, Perl, JavaScript pointers conversions Introduction 4 / 15

Implicit type conversions: strong vs. weak typing Many languages implicitly convert between types is this safe? Only if it s determined by the types, not the runtime values! Java (safe) int n = 42; String s = "Answer: " + n; PHP, Perl (unsafe) n = "4" + 2; s = "Answer: " + n Fun diabolical example: http://www.jsfuck.com/ programming with implicit conversions! Introduction 5 / 15

Static vs. dynamic typing Static typing types are associated with syntactic terms (ASTs) type errors are reported at compile time (and typically prevent execution) type checker proves that no type errors will occur at runtime Dynamic typing types are associated with runtime values type errors are reported at runtime (e.g. by throwing an exception) type checker is integrated into the runtime system Introduction 6 / 15

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

Benefits of static typing Usability and comprehension 1. machine-checked documentation guaranteed to be correct and consistent with implementation 2. better tool support e.g. code completion, navigation 3. supports high-level reasoning by providing named abstractions for shared behavior Introduction 8 / 15

Benefits of static typing (continued) Correctness 4. a partial correctness proof no runtime type errors improves robustness, focus testing on more interesting errors Efficiency 5. improved code generation can apply type-specific optimizations 6. type erasure no need for type information or checking at runtime Introduction 9 / 15

Drawback: static typing is conservative Q: What is the type of this expression? if 3 > 4 then True else 5 A: Static typing: type error Dynamic typing: Int Q: What is the type of this one? \x -> if x > 4 then True else x+2 A: Static typing: type error Dynamic typing:??? Silly examples, but... many advanced type features created to reclaim expressiveness Introduction 10 / 15

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

Static typing is a static semantics Dynamic semantics (a.k.a. execution semantics) what is the meaning of this program? relates an AST to a value (denotational semantics) describes meaning of program at runtime Static semantics which programs have meaning? relates an AST to a type describes meaning of program at compile time sem :: Exp -> Val typeof :: Exp -> Type Typing is just a semantics with a different semantic domain Implementing a static type system 12 / 15

Defining a static type system Example encoding in Haskell: 1. Define the abstract syntax, E data Exp =... the set of abstract syntax trees 2. Define the structure of types, T data Type =... another abstract syntax 3. Define the typing relation, E : T typeof :: Exp -> Type the mapping from ASTs to types Then, we can define a dynamic semantics that assumes there are no type errors Implementing a static type system 13 / 15

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

Typing contexts Often we need to keep track of some information during typing types of top-level functions types of local variables an implicit program stack set of declared classes and their methods... Put this information in the typing context (a.k.a. the environment) typeof :: Exp -> Env -> Type Implementing a static type system 15 / 15