At build time, not application time. Types serve as statically checkable invariants.

Similar documents
Category Item Abstract Concrete

Recitation 8: Dynamic and Unityped Languages : Foundations of Programming Languages

1. Abstract syntax: deep structure and binding. 2. Static semantics: typing rules. 3. Dynamic semantics: execution rules.

Dynamic Types , Spring March 21, 2017

Type Safety. Java and ML are type safe, or strongly typed, languages. C and C++ are often described as weakly typed languages.

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

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Simply-Typed Lambda Calculus

COS 320. Compiling Techniques

Programming Languages

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

15 212: Principles of Programming. Some Notes on Induction

CS558 Programming Languages

Lecture 3: Recursion; Structural Induction

Subtyping. Lecture 13 CS 565 3/27/06

CS558 Programming Languages

Lecture 13: Subtyping

Introduction to SML Getting Started

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

Supplementary Notes on Exceptions

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

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Induction and Semantics in Dafny

Harvard School of Engineering and Applied Sciences Computer Science 152

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

Programming Languages Assignment #7

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

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

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

Subtyping (cont) Lecture 15 CS 565 4/3/08

CMSC 330: Organization of Programming Languages. Operational Semantics

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Type Checking in COOL (II) Lecture 10

Part III. Chapter 15: Subtyping

CSE3322 Programming Languages and Implementation

Programming Languages Fall 2014

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

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

Compilers and computer architecture: Semantic analysis

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

Lecture 1: Overview

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

` e : T. Gradual Typing. ` e X. Ronald Garcia University of British Columbia

"Good" vs. "Bad" Expressions. ; interp-expr FAE... -> FAE-Value

Type Checking and Type Inference

CSE3322 Programming Languages and Implementation

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

Semantic Analysis. Lecture 9. February 7, 2018

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

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

Part VI. Imperative Functional Programming

Types and evaluation

CS4215 Programming Language Implementation. Martin Henz

CS 415 Midterm Exam Spring 2002

Combining Programming with Theorem Proving

CSCE 314 Programming Languages. Type System

Principles of Programming Languages

What Is Computer Science? The Scientific Study of Computation. Expressing or Describing

LECTURE 3. Compiler Phases

CSE 505: Concepts of Programming Languages

The Substitution Model. Nate Foster Spring 2018

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

The Substitution Model

Subtyping (cont) Formalization of Subtyping. Lecture 15 CS 565. Inversion of the subtype relation:

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

Mini-ML. CS 502 Lecture 2 8/28/08

Part III Chapter 15: Subtyping

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

Gradual Typing with Union and Intersection Types

Meeting14:Denotations

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

CIS 500 Software Foundations Fall October 2

Variables. Substitution

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

Syntax and Grammars 1 / 21

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

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

The Environment Model. Nate Foster Spring 2018

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Featherweight Java (FJ)

Lecture #23: Conversion and Type Inference

CSE505, Fall 2012, Midterm Examination October 30, 2012

Agenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem

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

Formal Semantics of Programming Languages

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

Quiz. Question #1: What is the value of the following expression? {+ 1 2} Wrong answer: 0 Wrong answer: 42. Answer: 3 1-4

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

In Our Last Exciting Episode

Formal Semantics of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Transcription:

Static Typing Thus far we have only considered statically typed languages. CMPSCI 630: Programming Languages Static and Dynamic Typing Spring 2008 (with thanks to Robert Harper) Type system is based on an analysis of the program. Type errors are caught at compile time. The canonical forms lemma relates the static type to the dynamic value. 1 Static Typing As we have seen, we still must deal with run-time errors. Advantages of Static Typing Mistakes are caught as early as possible. Division by zero. At build time, not application time. Exceeding array bounds (if we had arrays). Types serve as statically checkable invariants. So why bother with static type errors? Why not make them all dynamic errors? Types are a formal comment stating an expectation. Compiler can check these requirements. 2 3 Advantages of Dynamic Typing Mistakes are caught as late as possible. At execution time, not at build time. Don t have to hassle with the type checker. Dynamic typing affords greater flexibility. Flexibility: if true then 17 else false has type nat. Heterogeneity: [true,17,"17"] is a valid list. Which approach is better? Static better than dynamic? (ML, Scala, Java) Dynamic better than static? (Lisp, Scheme) First, let s look at what is meant by dynamic typing. Then we ll give a (possibly surprising) answer! 4 5

Dynamic Typing Run-Time Type Errors Two main ideas: Do away with the static semantics entirely. parses, it is acceptable for execution. If a program Employ run-time checks to ensure safety. Type errors occur at run-time, just like any other errors. How can we formalize this? Augment the semantics with an e err judgement. Add new rules that make type errors into checked errors. For example, e val e 1 val (e λ(x.d)) ap(e, e 1 ) err The side condition checks for the case that e is not a function. 6 7 Run-Time Type Errors This seems simple enough, but... The side conditions on the rules should be formalized, since they correspond to run-time checks. It is not immediately clear how to check the type of a value at execution time (e.g., booleans and integers are just words). Class Labels These rules assume that you can determine the form of a value at run-time! In general, this is unrealistic: values are just bits! (e.g., booleans and integers are both words). But we can label values with their shape (or class) so that run-time checks are possible. Takes space for the label. Takes time to apply the label and to check it. 8 9 Dynamically Typed PCF Corresponding Concrete Syntax We will consider L{dyn}, a dynamically type version of L{nat } (PCF). Here is a grammar for L{dyn}: Expressions d ::= x num(n) succ(d) ifz(d, d 0, x.d 1 ) fun(λ(x.d)) dap(d 1,d 2 ) fix(x.d) NB: Values are labelled with their class num or fun ; there are no unlabelled values! NB: Successor is now an elimination form acting on values of class num rather than an introduction form for numbers. 10 Abstract Concrete x x num(n) n succ(d) succ(d) ifz(d, d 0, x.d 1 ) ifz d{z => d 0 s(x) => d 1 } fun(λ(x.d)) λx.d dap(d 1,d 2 ) d 1 (d 2 ) fix(x.d) fix x is d NB: There are no class labels in the concrete syntax! Parser must insert them when passing to abstract syntax, since they are necessary to the dynamic semantics of L{dyn}. 11

Class Labels L{dyn} is not just L{nat } without types! Concrete syntax gives that appearance, but abstract syntax reveals the difference: class labels are needed in L{dyn} expressions but not in L{nat }. The class of a value is not its type! Just says this is a function, not this is of type nat nat. Static Semantics for L{dyn} Static semantics is trivial since there is only one type. x 1 ok,...x n ok d ok says expression d is well-formed with free variables among those listed in the hypothesis. Nevertheless, the class is sometimes called (incorrectly) the run-time type of the value. It is important to distinguish the class from the type! 12 13 Introduce rules for values fully evaluated (closed) expressions: num(n) val fun(λ(x.d)) val Introduce rules for checking the class of a numeric value, and its negation: num(n) is num n fun( ) isnt num Second argument of the first judgement is not an expression of L{dyn} but just a special piece of syntax used internally in later dynamic semantics rules. 14 Dynamic semantics rules for successor: d d succ(d) succ(d ) d is num n succ(d) num(s(n)) d isnt num succ(d) err Simultaneous inductive definition of d d and d err 15 Dynamic semantics rules for ifz: d d ifz(d, d 0, x.d 1 ) ifz(d,d 0, x.d 1 ) d is num z ifz(d, d 0, x.d 1 ) d 0 d is num s(n) ifz(d, d 0, x.d 1 ) [num(n)/x]d 1 d isnt num ifz(d, d 0, x.d 1 ) err NB: In third rule, labelled value num(n) is bound to x to preserve invariant that variables are bound to forms of expression. Introduce rules for checking the class of a function value, and its negation: fun(λ(x.d)) is fun λ(x.d) num( ) isnt fun Again, second argument of the first judgement is not an expression of L{dyn} but just a special piece of syntax used internally in later dynamic semantics rules. 16 17

Rules for application and general recursion: d 1 d 1 dap(d 1,d 2 ) dap(d 1,d 2) d 1 val d 2 d 2 dap(d 1,d 2 ) dap(d 1,d 2 ) d 1 is fun λ(x.d) d 2 val dap(d 1,d 2 ) [d 2 /x]d d 1 isnt fun dap(d 1,d 2 ) err fix(x.d) [fix(x.d)/x]d Safety of Dynamic Typing Dynamically typed languages are trivially type safe! There is only one type, which must be preserved by evaluation. (Every well-formed expression is well-typed.) Class checking ensures that we may always make progress, although there are more opportunities for run-time errors. Theorem 1 If d ok, then either d val, or d err, or there exists d such that d d. 18 19 The Cost of Dynamic Typing Labelling data values uses time and space! Pay As You Go The cost applies even if the program is statically well-typed! Space for the label itself. Time to apply labels, check labels, and recover unlabelled values. Static checker proves that some class checks are not necessary. No means to express raw operations on unlabelled data. The overheads are significant! Violates the pay-as-you-go principle of language design! Operations in a loop repeatedly label and unlabel values. Pay only for features you actually use. Difficult to hoist checks outside of the loop. 20 Dynamic checking imposes a global overhead. 21 Static vs. Dynamic, Revisited So which is better, static or dynamic? Do we need the flexibility of dynamic typing? Is the overhead significant in practice? How soon do we wish to report errors? Static Subsumes Dynamic Lots of energy has been wasted on this debate! Dynamic typing is a mode of use of static typing! The ideas are not opposed, but rather are completely compatible! Labelled values are a type! Should ill-typed programs be executable? 22 23

Dynamic Typing as Static Typing Adding dynamic typing to L{nat } (PCF): Add a new type dyn of labelled values. Operations to apply a label to a value. Operations to check class. If you want dynamic typing, use the (static) type dyn. Hybrid Typing: Syntax We consider the language L{nat dyn } which extends the syntax of L{nat } (PCF) with: Type τ ::= dyn Expr e :: = new[l](e) cast[l](e) Class l ::= num fun Type dyn represents the type of labelled values. Cast operation takes a class (indicated by label), not a type (which is always dyn)! 24 25 Hybrid Typing: Syntax Corresponding Concrete Syntax: Abstract Concrete dyn dyn new[l](e) l!e cast[l](e) e?l num num fun fun Hybrid Typing: Static Semantics Static semantics for L{nat dyn } extend the typing rules for L{nat } (PCF) with rules for labelling values: Γ e : nat Γ new[num](e) :dyn Γ e : parr(dyn, dyn) Γ new[fun](e) :dyn Note that these rules preclude misapplication of labels! 26 27 Hybrid Typing: Static Semantics Further extend the typing rules for PCF with rules for checking labels: Γ e : dyn Γ cast[num](e) :nat Γ e : dyn Γ cast[fun](e) :parr(dyn, dyn) These are essentially downcasts that can fail at run-time. 28 Hybrid Typing: Dynamic Semantics Dynamic semantics of L{nat dyn } add rules for downcasting: e val new[l](e) val e e new[l](e) new[l](e ) e e cast[l](e) cast[l](e ) new[l](e) val cast[l](new[l](e)) e new[l ](e) val l l cast[l](new[l ](e)) err 29

Safety for Hybrid PCF Lemma 2 (Canonical Forms) If e : dyn and e val then e = new[l](e ) for some class l and e val. If l = num then e : nat and if l = fun then e : parr(dyn, dyn). Theorem 3 (Safety) The language L{nat dyn } is safe: 1. If e : τ and e e, then e : τ. 2. If e : τ, then either e val, or e err, or there exists e such that e e. Here s dynamic typing within SML. datatype tagged = Int of int Bool of bool Fun of tagged -> tagged exception TypeError fun checked add (Int m, Int n) = Int (m+n) checked add = raise TypeError fun checked apply (Fun f, v) = f v checked apply (, ) = raise TypeError 30 31 Heterogeneous lists: Heterogeneity, Revisited A fully dynamic looping function: [Int 1, Bool true, Fun (fn x:tagged => x)] : tagged list Heterogeneous conditionals: if true then (Int 1) else (Bool true) : tagged Notice that dynamic checks are required whenever you use an element of a heterogeneous list or the result of a heterogeneous conditional! 32 fun dyn fact (n : tagged) = let fun loop (n, a) = case n of Int m => (case m of 0 => a m => loop ( checked sub (n, Int 1), checked mult (n, a) )) => raise RuntimeTypeError in loop (n, Int 1 ) end 33 In a static framework you can hoist checks out of loops. Summary Statically typed languages subsume dynamically typed languages. fun checked fact (n : tagged) = let fun loop (0, a) = a loop (p, a) = loop (p-1, p*a) in case n of Int m => Int (loop (m, 1)) => raise TypeError end Dynamic typing can be a good thing. But it is not the only thing: pay-as-you-go. 34 35