Gradual Typing with Inference

Size: px
Start display at page:

Download "Gradual Typing with Inference"

Transcription

1 Gradual Typing with Inference Jeremy Siek University of Colorado at Boulder joint work with Manish Vachharajani

2 Overview Motivation Background Gradual Typing Unification-based inference Exploring the Solution Space Type system (specification) Inference algorithm (implementation)

3 Why Gradual Typing? Java Static and dynamic type systems have complimentary strengths. Static typing provides full-coverage error checking, efficient execution, and machine-checked documentation. Dynamic typing enables rapid development and fast adaption to changing requirements. Why not have both in the same language? Python

4 Goals for gradual typing Treat programs without type annotations as dynamically typed. Programmers may incrementally add type annotations to gradually increase static checking. Annotate all parameters and the type system catches all type errors. 4

5 Goals for gradual typing Treat programs without type annotations as dynamically typed. Programmers may incrementally add type annotations to gradually increase static checking. Annotate all parameters and the type system catches all type errors. dynamic static 4

6 Goals for gradual typing Treat programs without type annotations as dynamically typed. Programmers may incrementally add type annotations to gradually increase static checking. Annotate all parameters and the type system catches all type errors. dynamic gradual static 4

7 The Gradual Type System Classify dynamically typed expressions with the type? Allow implicit coercions to? and from? with any other type Extend coercions to compound types using a new consistency relation 5

8 Coercions to and from? (λa:int. (λx. x + 1) a) 1 Parameters with no type annotation are given the dynamic type?. 6

9 Coercions to and from?? (λa:int. (λx. x + 1) a) 1 Parameters with no type annotation are given the dynamic type?. 6

10 Coercions to and from?? int (λa:int. (λx. x + 1) a) 1 Parameters with no type annotation are given the dynamic type?. 6

11 Coercions to and from? int? int? (λa:int. (λx. x + 1) a) 1 Parameters with no type annotation are given the dynamic type?. 6

12 Coercions to and from? int? (λa:int. (λx. x + 1) a) 1 int x int int Parameters with no type annotation are given the dynamic type?. 6

13 Coercions to and from?? int? (λa:int. (λx. x + 1) a) 1 int x int int Parameters with no type annotation are given the dynamic type?. 6

14 Coercions to and from?? int? (λa:int. (λx. x + 1) a) 1 int x int int? int Parameters with no type annotation are given the dynamic type?. 6

15 Coercions between compound types (λf:int int. f 1) (λx. 1) 7

16 Coercions between compound types? int (λf:int int. f 1) (λx. 1) 7

17 Coercions between compound types? int (λf:int int. f 1) (λx. 1)? int int int 7

18 Detect static type errors (λf:int int. f 1) 1 int int int 8

19 Type system: replace = with ~ Γ e1 : σ τ Γ e2 : σ σ ~ σ Γ e1 e2 : τ 9

20 Type system: replace = with ~ Γ e1 : σ τ Γ e2 : σ σ ~ σ Γ e1 e2 : τ 9

21 The consistency relation Definition: a type is consistent, written ~, with another type when they are equal where they are both defined. Examples: int ~ int int ~ bool? ~ int int ~? int? ~? bool? bool ~? int 10

22 The consistency relation? ~ τ τ ~? τ1 ~ τ2 τ ~ τ τ1 ~ τ3 τ2 ~ τ4 τ1 τ2 ~ τ3 τ4 11

23 Compiler inserts run-time checks Γ e1 e 1 : σ τ Γ e2 e 2 : σ σ ~ σ Γ e1 e2 e 1 σ σ e 2 : τ Example: (λa:int. (λx. x + 1) a) 1 (λa:int. (λx. int? x + 1)? int a) 1 12

24 Recent Developments Integration with objects (Siek & Taha, ECOOP 07) Space-efficiency (Herman et al, TFP 07) Blame tracking (Wadler & Findler, Scheme 07) In JavaScript (Herman & Flanagan, ML 07) 13

25 Why Inference? Interesting research question: how does the dynamic type interact with type variables? Practical applications Help programmers migrate dynamically typed code to statically typed code Explain how gradual typing can be integrated with functional languages with inference (ML, Haskell, etc.) 14

26 STLC with type vars: Specification Standard STLC judgment: Γ e : τ An STLC term with type variables is well typed if there exists an S such that S(Γ) S(e) : S(τ) e.g., (λx:int. (λy:α. y) x) S = {α int} 15

27 Inference Algorithm λx:int. (λy:α. y) x constraint generation α α = int β unification S = {α int, β int} 16

28 Huet s Unification α α = int β 17

29 Huet s Unification α α = int β int " 17

30 Huet s Unification α α = int β int " int " 17

31 Huet s Unification α α = int β int " int " int 17

32 Huet s Unification α α = int β int " int " int int 17

33 Huet s Unification When merging nodes, the algorithm needs to decide which label to keep In this setting, non-type variables trump type variables int int 18

34 Gradual Typing with Inference Setting: STLC with α and?. To migrate from dynamic to static, change? to α and the inferencer will tell you the solution for α or give an error. λ f:?. λ x:?. f x x λ f:α. λ x:?. f x x 19

35 Syntactic Sugar λ f. λ x. f x x? 20

36 Syntactic Sugar λ f. λ x. f x x λ f:?. λ x:?. f x x? 20

37 Syntactic Sugar λ f. λ x. f x x λ f:?. λ x:?. f x x λ f:α. λ x:β. f x x? 20

38 Non-solution #1 Well typed in gradual type system after substitution S(Γ) S(e) : S(τ) Problem: the following is accepted (λ f:α. f 1) 1 S = {α?} 21

39 Non-solution #2 Forbid?s from appearing in a solution S Problem: sometimes this forces cast errors at runtime λx:?. (λ y:α. y) x λx:?. (λ y:int. y) x λx:?. (λ y:int. y) int? x 22

40 Non-solution #2 Forbid?s from appearing in a solution S Problem: sometimes this forces cast errors at runtime λx:?. (λ y:α. y) x λx:?. (λ y:int. y) x λx:?. (λ y:int. y) int? x 22

41 Non-solution #3 Treat each? as a different type variable then check for well typed in STLC after substitution Problem: the following is rejected λ f:int bool int. λ x:?. f x x λ f:int bool int. λ x:α. f x x 23

42 Non-solution #4 Treat each occurrence of? in a constraint as a different type variable Problem: if no type vars in the program, the resulting type should not have type vars λ f:int?. λ x:int. (f x) int? = int β int α = int β 24

43 Lessons Need to restrict the occurrences of? in solutions But can t completely outlaw the use of? Idea: a solution for α at least as informative as any of the types that constrain α constrain i.e., the solution for α must be an upper bound of all the types that constrain α 25

44 Information Ordering int int τ1 τ2? int int? bool int??? semi-lattice 26

45 Type System But what does it mean for a type to constrain α? λf:α α. λg:(? int) int. g f α α? int 27

46 Type System But what does it mean for a type to constrain α? λf:α α. λg:(? int) int. g f α α? int? S(α) 27

47 Type System But what does it mean for a type to constrain α? λf:α α. λg:(? int) int. g f α α? int? S(α) int S(α) 27

48 Type System The typing judgment: Consistent-equal: Consistent-less: S; Γ e : τ S τ τ S τ τ 28

49 Type System S; Γ e : τ S; Γ e1 : τ1 S; Γ e2 : τ2 S τ1 τ2 β (β fresh) S; Γ e1 e2 : β 29

50 Type System S; Γ e : τ S; Γ e1 : τ1 S; Γ e2 : τ2 S τ1 τ2 β (β fresh) S; Γ e1 e2 : β 29

51 Consistent-equal S? τ S τ? S τ τ S τ S(α) S α τ S τ S(α) S τ α S τ1 τ3 S τ2 τ4 S γ γ S τ1 τ2 τ3 τ4 30

52 Consistent-less S? τ S τ τ S S(α) = τ S α τ S γ γ S τ1 τ3 S τ2 τ4 S τ1 τ2 τ3 τ4 31

53 Properties When there are no type variables in the program, the type system acts like the original gradual type system When there are no? in the program, the type system acts like the STLC with variables 32

54 Inference Algorithm λf:α α. λg:(? int) int. g f constraint generation (? int) int (α α) β unification for S = {α int, β int} 33

55 Unification for Can t use the standard substitution-based version because we need to see all the unificands before deciding on the solution (? int) int (α α) β 34

56 Unification for Need to compute the least upper bound Otherwise spurious casts are inserted λx:?. (λ y:α. y) x λx:?. (λ y:int. y) x λx:?. (λ y:int. y) int? x 35

57 Unification for Need to compute the least upper bound Otherwise spurious casts are inserted λx:?. (λ y:α. y) x λx:?. (λ y:int. y) x λx:?. (λ y:int. y) int? x 35

58 Merging Labels Type variables are trumped by non-type variables (including the dynamic type) The dynamic type is trumped by concrete types (e.g., int, bool, )??? int int 36

59 Unification for (? int) int (α α) β 37

60 Unification for (? int) int (α α) β var "? int var 37

61 Unification for (? int) int (α α) β? int var var " 37

62 Unification for (? int) int (α α) β? int var var " 37

63 Unification for (? int) int (α α) β? var int 37

64 Unification for (? int) int (α α) β var int 37

65 Unification for (? int) int (α α) β int 37

66 Properties The time complexity of unification for is O(m α(n)) for a graph with n nodes and m edges Soundness: if (S,τ) = infer(γ, e) then S*; Γ e : τ. Completeness: if S; Γ e : τ then there is a S, τ, and R such that (S, τ ) = infer(γ, e) and R S S and R S *(τ ) S(τ). 38

67 Related Work Java + Dynamic (Gray & Findler & Flatt) Optional types (LISP, Dylan, etc.) BabyJ: gradual typing in a nominal setting(anderson & Drossopoulou) Quasi-static types (Thatte) Soft typing (Cartwright & Fagan, Wright & Cartwright, Flanagan & Felleisen, Aiken & Wimmers & Lakshman) Dynamic typing (Henglein) 39

68 Conclusion Gradual typing provides a combination of dynamic and static typing in the same language, under programmer control. We present a type system for gradually typed programs with type variables. We present a unification-based inference algorithm that only requires a small change to Huet s algorithm to handle?s. 40

69 41

70 Type System S; Γ e1 : τ1 S; Γ e2 : τ2 S τ1 τ2 β (β fresh) S; Γ e1 e2 : β 42

71 Type System S; Γ e1 : τ1 S; Γ e2 : τ2 S τ1 τ2 β (β fresh) S; Γ e1 e2 : β 42

72 Non-solution S; Γ e1 : τ1 S; Γ e2 : τ2 S τ1 τ2 τ3 S; Γ e1 e2 : τ3 Problem: the following is accepted because we can choose τ3 =? λ f:int int. λ g:int bool. f (g 1) 43

73 Solution S; Γ e1 : τ1 S; Γ e2 : τ2 S τ1 τ2 β (β fresh) S; Γ e1 e2 : β λ f:int int. λ g:int bool. f (g 1) S int bool int β1 S int int β1 β2 S bool β1 S int β1 44

74 Inference Algorithm λf:(? int) (int?) int. λy:α. f y y constraint generation (? int) (int?) int α β1 β1 α β2 unification for S = {α int int, β1 (int int) int, β2 int} 45

75 Unification for (? int) (int?) int α β1 β1 α β2 46

76 Unification for (? int) (int?) int α β1 β1 α β2 var " var 1? int var 2? 46

77 Unification for (? int) (int?) int α β1 β1 α β2 var " var 1? int var 2? 46

78 Unification for (? int) (int?) int α β1 β1 α β2 "1? int var "2? 46

79 Unification for (? int) (int?) int α β1 β1 α β2 "1? int var "2? 46

80 Unification for (? int) (int?) int α β1 β1 α β2 "1? int "2? 46

81 Unification for (? int) (int?) int α β1 β1 α β2 "1? int "2? 46

82 Unification for (? int) (int?) int α β1 β1 α β2 "1 int "2 46

83 Unification for (? int) (int?) int α β1 β1 α β2 46

The gradual typing approach to mixing static and dynamic typing

The gradual typing approach to mixing static and dynamic typing 1 / 38 The gradual typing approach to mixing static and dynamic typing Jeremy G. Siek University of Colorado = Indiana University TFP 2013 at Provo, Utah, May 2013 The Goals of Gradual Typing Enjoy the

More information

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

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper) Gradual Typing for Functional Languages Jeremy Siek and Walid Taha (presented by Lindsey Kuper) 1 Introduction 2 What we want Static and dynamic typing: both are useful! (If you re here, I assume you agree.)

More information

The Polymorphic Blame Calculus and Parametricity

The Polymorphic Blame Calculus and Parametricity 1 / 31 The Polymorphic Blame Calculus and Parametricity Jeremy G. Siek Indiana University, Bloomington University of Strathclyde August 2015 2 / 31 Integrating static and dynamic typing Static Dynamic

More information

Space-Efficient Blame Tracking for Gradual Types

Space-Efficient Blame Tracking for Gradual Types Space-Efficient Blame Tracking for Gradual Types Jeremy G. Siek University of Colorado at Boulder jeremy.siek@colorado.edu Abstract Static and dynamic type systems have well-known strengths and weaknesses.

More information

2 Blending static and dynamic typing

2 Blending static and dynamic typing 2 Blending static and dynamic typing We begin this chapter presenting a little bit of the history behind combining static and dynamic typing in the same language. Then, we introduce optional type systems

More information

Interlanguage Migration

Interlanguage Migration Interlanguage Migration From Scripts to Programs Sam Tobin-Hochstadt and Matthias Felleisen Northeastern University DLS 2006 1 A Story 2 About a programmer 3 Who needed to manage his budget 4 And so, he

More information

Exploring the Design Space of Higher-Order Casts ; CU-CS

Exploring the Design Space of Higher-Order Casts ; CU-CS University of Colorado, Boulder CU Scholar Computer Science Technical Reports Computer Science Fall 10-1-2008 Exploring the Design Space of Higher-Order Casts ; CU-CS-1047-08 Jeremy Siek University of

More information

Harvard School of Engineering and Applied Sciences Computer Science 152

Harvard School of Engineering and Applied Sciences Computer Science 152 Harvard School of Engineering and Applied Sciences Computer Science 152 Lecture 17 Tuesday, March 30, 2010 1 Polymorph means many forms. Polymorphism is the ability of code to be used on values of different

More information

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421) Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 10/3/17

More information

Lecture #23: Conversion and Type Inference

Lecture #23: Conversion and Type Inference Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:

More information

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

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 14 Tuesday, March 24, 2015 1 Parametric polymorphism Polymorph means many forms. Polymorphism is the ability of

More information

Consistent Subtyping for All

Consistent Subtyping for All Consistent Subtyping for All Ningning Xie Xuan Bi Bruno C. d. S. Oliveira 11 May, 2018 The University of Hong Kong 1 Background There has been ongoing debate about which language paradigm, static typing

More information

Gradual Typing for Objects

Gradual Typing for Objects Gradual Typing for Objects Jeremy Siek 1 and Walid Taha 2 jeremy.siek@colorado.edu, taha@rice.edu 1 University of Colorado, Boulder, CO 80309, USA and LogicBlox Inc., Atlanta, GA 30309, USA 2 Rice University,

More information

Jeremy Siek University of Colorado. Blame Tracking JOINT WORK WITH PHILIP WADLER

Jeremy Siek University of Colorado. Blame Tracking JOINT WORK WITH PHILIP WADLER Jeremy Siek University of Colorado Blame Tracking JOINT WORK WITH PHILIP WADLER an explosive combination! dynamic languages are great! software libraries are great! the combination is explosive! IT S HAPPENED

More information

FROM SCRIPTS TO PROGRAMS OR, TYPES FOR UNTYPED LANGUAGES. Matthias Felleisen Racketeer

FROM SCRIPTS TO PROGRAMS OR, TYPES FOR UNTYPED LANGUAGES. Matthias Felleisen Racketeer FROM SCRIPTS TO PROGRAMS OR, TYPES FOR UNTYPED LANGUAGES Matthias Felleisen Racketeer THE PROGRAMMING LANGUAGE RENAISSANCE performance PL performance Java/JVM C#/CIL Scala, F# PL safety & security EmacsLISP

More information

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages Lecture 15: Recursive Types & Subtyping CSE 230: Winter 2008 Principles of Programming Languages Lecture 15: Recursive Types & Subtyping Ranjit Jhala UC San Diego News? Formalize first-order type systems Simple types (integers and booleans)

More information

Interlanguage Migration: From Scripts to Programs

Interlanguage Migration: From Scripts to Programs Interlanguage Migration: From Scripts to Programs Sam Tobin-Hochstadt Northeastern University Boston, MA samth@ccs.neu.edu Matthias Felleisen Northeastern University Boston, MA matthias@ccs.neu.edu ABSTRACT

More information

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Toby Murray, June Andronick, Gerwin Klein λ 1 Last time... λ calculus syntax free variables, substitution β reduction α and η conversion

More information

Type Inference; Parametric Polymorphism; Records and Subtyping Section and Practice Problems Mar 20-23, 2018

Type Inference; Parametric Polymorphism; Records and Subtyping Section and Practice Problems Mar 20-23, 2018 Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Type Inference; Parametric Polymorphism; Records and Subtyping Mar 20-23, 2018 1 Type Inference (a) Recall the constraint-based

More information

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference Lecture #13: Type Inference and Unification Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a ::

More information

Gradual Typing with Union and Intersection Types

Gradual Typing with Union and Intersection Types Gradual Typing with Union and Intersection Types Giuseppe Castagna, Victor Lanvin ICFP 17 September 6, 2017 1 / 14 Outline 1 Motivating Example 2 Types and Subtyping 3 Function Types and Operators 4 Conclusion

More information

λ calculus is inconsistent

λ calculus is inconsistent Content Rough timeline COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Gerwin Klein, June Andronick, Toby Murray λ Intro & motivation, getting started [1] Foundations & Principles

More information

Blame Prediction. Software Languages Lab, Vrije Universiteit Brussel, Belgium,

Blame Prediction. Software Languages Lab, Vrije Universiteit Brussel, Belgium, Blame Prediction Dries Harnie, Christophe Scholliers, and Wolfgang De Meuter Software Languages Lab, Vrije Universiteit Brussel, Belgium, {dharnie,cfscholl,wdmeuter}@vub.ac.be Abstract. Static type systems

More information

Like Types. aka. integrating typed and untyped code in Thorn. Francesco Zappa Nardelli. INRIA Paris-Rocquencourt, MOSCOVA research team

Like Types. aka. integrating typed and untyped code in Thorn. Francesco Zappa Nardelli. INRIA Paris-Rocquencourt, MOSCOVA research team Like Types aka. integrating typed and untyped code in Thorn Francesco Zappa Nardelli INRIA Paris-Rocquencourt, MOSCOVA research team Tobias Wrigstad Sylvain Lebresne Johan Östlund Jan Vitek Purdue University

More information

Programming Languages

Programming Languages CSE 230: Winter 2008 Principles of Programming Languages Ocaml/HW #3 Q-A Session Push deadline = Mar 10 Session Mon 3pm? Lecture 15: Type Systems Ranjit Jhala UC San Diego Why Typed Languages? Development

More information

If a program is well-typed, then a type can be inferred. For example, consider the program

If a program is well-typed, then a type can be inferred. For example, consider the program CS 6110 S18 Lecture 24 Type Inference and Unification 1 Type Inference Type inference refers to the process of determining the appropriate types for expressions based on how they are used. For example,

More information

Where is ML type inference headed?

Where is ML type inference headed? 1 Constraint solving meets local shape inference September 2005 2 Types are good A type is a concise description of the behavior of a program fragment. Typechecking provides safety or security guarantees.

More information

Concepts of programming languages

Concepts of programming languages Concepts of programming languages Lecture 2 Wouter Swierstra 1 Last time: programming languages In the first lecture I tried to motivate why we might consider programming languages themselves as interesting

More information

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

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler. Goal Understand what this interface means and why it matters: CS152: Programming Languages Lecture 15 Parametric Polymorphism Dan Grossman Spring 2011 type a mylist; val mt_list : a mylist val cons : a

More information

Well-typed programs can t be blamed

Well-typed programs can t be blamed Well-typed programs can t be blamed Philip Wadler University of Edinburgh Robert Bruce Findler University of Chicago Abstract We show how contracts with blame fit naturally with recent work on hybrid types

More information

Refined Criteria for Gradual Typing

Refined Criteria for Gradual Typing Refined Criteria for Gradual Typing Jeremy G. Siek 1, Michael M. Vitousek 1, Matteo Cimini 1, and John Tang Boyland 2 1 Indiana University Bloomington, School of Informatics and Computing 150 S. Woodlawn

More information

Principal Type Schemes for Gradual Programs

Principal Type Schemes for Gradual Programs Principal Type Schemes for Gradual Programs With updates and corrections since publication (Latest: May 16, 2017) Ronald Garcia Software Practices Lab Department of Computer Science University of British

More information

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016 CSE-505: Programming Languages Lecture 20.5 Recursive Types Zach Tatlock 2016 Where are we System F gave us type abstraction code reuse strong abstractions different from real languages (like ML), but

More information

Tracing Ambiguity in GADT Type Inference

Tracing Ambiguity in GADT Type Inference Tracing Ambiguity in GADT Type Inference ML Workshop 2012, Copenhagen Jacques Garrigue & Didier Rémy Nagoya University / INRIA Garrigue & Rémy Tracing ambiguity 1 Generalized Algebraic Datatypes Algebraic

More information

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

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics Dan Grossman Spring 2011 Review e ::= λx. e x e e c v ::= λx. e c τ ::= int τ τ Γ ::= Γ, x : τ (λx. e) v e[v/x] e 1 e 1 e 1 e

More information

CSCE 314 Programming Languages. Type System

CSCE 314 Programming Languages. Type System CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be

More information

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

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type

More information

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012 CS-XXX: Graduate Programming Languages Lecture 17 Recursive Types Dan Grossman 2012 Where are we System F gave us type abstraction code reuse strong abstractions different from real languages (like ML),

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

More information

Unifying Nominal and Structural Ad-Hoc Polymorphism

Unifying Nominal and Structural Ad-Hoc Polymorphism Unifying Nominal and Structural Ad-Hoc Polymorphism Stephanie Weirich University of Pennsylvania Joint work with Geoff Washburn Ad-hoc polymorphism Define operations that can be used for many types of

More information

GADTs meet Subtyping

GADTs meet Subtyping GADTs meet Subtyping Gabriel Scherer, Didier Rémy Gallium INRIA 2014 Gabriel Scherer, Didier Rémy (Gallium INRIA) GADTs meet Subtyping 2014 1 / 21 A reminder on GADTs GADTs are algebraic data types that

More information

A Polymorphic Type System for Multi-Staged Languages

A Polymorphic Type System for Multi-Staged Languages A Polymorphic Modal Type System for Lisp-like Multi-Staged Languages Ik-Soon Kim 1 Kwangkeun Yi 1 Cristiano Calcagno 2 1 Seoul National University 2 Imperial College POPL 06, 1/12/2006 @ Charleston Outline

More information

Space-Efficient Gradual Typing

Space-Efficient Gradual Typing Space-Efficient Gradual Typing David Herman 1, Aaron Tomb 2, and Cormac Flanagan 2 1 Northeastern University 2 University of California, Santa Cruz Abstract Gradual type systems offer a smooth continuum

More information

Type Inference for ML

Type Inference for ML Type Inference for ML Tony Garnock-Jones February 29, 2012 1 Typing without quite so much typing Writing programs in System F is certainly possible, and the type system gives us nice

More information

Gradual Typing for Mutable Objects

Gradual Typing for Mutable Objects Gradual Typing for Mutable Objects Jeremy G. Siek 1, Michael M. Vitousek 2, and Shashank Bharadwaj 1 1 Department of Electrical, Computer, and Energy Engineering 2 Department of Computer Science University

More information

Introduction to System F. Lecture 18 CS 565 4/20/09

Introduction to System F. Lecture 18 CS 565 4/20/09 Introduction to System F Lecture 18 CS 565 4/20/09 The Limitations of F 1 (simply-typed λ- calculus) In F 1 each function works exactly for one type Example: the identity function id = λx:τ. x : τ τ We

More information

Typing & Static Analysis of Multi-Staged Programs

Typing & Static Analysis of Multi-Staged Programs Typing & Static Analysis of Multi-Staged Programs School of Computer Science & Engineering Seoul National University 6/7/2011 @ Oxford U (co-work with I. Kim, W. Choi, B. Aktemur, C. Calcagno, M. Tatsuda)

More information

Let Arguments Go First

Let Arguments Go First Let Arguments Go First Ningning Xie and Bruno C. d. S. Oliveira The University of Hong Kong {nnxie,bruno}@cs.hku.hk Abstract. Bi-directional type checking has proved to be an extremely useful and versatile

More information

Type Reconstruction for General Refinement Types

Type Reconstruction for General Refinement Types Type Reconstruction for General Refinement Types Kenneth Knowles Cormac Flanagan University of California, Santa Cruz Abstract. General refinement types allow types to be refined by predicates written

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

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

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int CSE 230: Winter 2010 Principles of Programming Languages Lecture 11: Type Systems News New HW up soon Special Hour to discuss HW? Ranjit Jhala UC San Diego Programming with λ-calculus Encode: bool if-then-else

More information

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

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012 CS-XXX: Graduate Programming Languages Lecture 9 Simply Typed Lambda Calculus Dan Grossman 2012 Types Major new topic worthy of several lectures: Type systems Continue to use (CBV) Lambda Caluclus as our

More information

CS 6110 S14 Lecture 1 Introduction 24 January 2014

CS 6110 S14 Lecture 1 Introduction 24 January 2014 CS 6110 S14 Lecture 1 Introduction 24 January 2014 1 Introduction What is a program? Is it just something that tells the computer what to do? Yes, but there is much more to it than that. The basic expressions

More information

Generalizing Hindley-Milner Type Inference Algorithms

Generalizing Hindley-Milner Type Inference Algorithms Generalizing Hindley-Milner Type Inference Algorithms Bastiaan Heeren Jurriaan Hage Doaitse Swierstra institute of information and computing sciences, utrecht university technical report UU-CS-2002-031

More information

Simplifying and Improving Qualified Types

Simplifying and Improving Qualified Types Simplifying and Improving Qualified Types Mark P. Jones Yale University, Department of Computer Science P.O. Box 208285, New Haven, CT 06520-8285. jones-mark@cs.yale.edu Research Report YALEU/DCS/RR-1040,

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

Second-Order Type Systems

Second-Order Type Systems #1 Second-Order Type Systems Homework 5 Summary Student : 37.9704 Student : 44.4466 ORIGINAL : 50.2442 Student : 50.8275 Student : 50.8633 Student : 50.9181 Student : 52.1347 Student : 52.1633 Student

More information

The Typed λ Calculus and Type Inferencing in ML

The Typed λ Calculus and Type Inferencing in ML Notes on Types S. Arun-Kumar Department of Computer Science and Engineering Indian Institute of Technology New Delhi, 110016 email: sak@cse.iitd.ernet.in April 14, 2002 2 Chapter 1 The Typed λ Calculus

More information

Collage of Static Analysis

Collage of Static Analysis Course Outline Collage of Static Analysis 0.5hr: Static Analysis Overview 1.5hr: Static Analysis Design Framework 1.0hr: Static Analysis Engineering Framework 1.0hr: Static Analysis of Multi-Staged Programs

More information

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Tuesday, February 19, 2013 The lambda calculus (or λ-calculus) was introduced by Alonzo Church and Stephen Cole Kleene in

More information

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals Review CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics e ::= λx. e x ee c v ::= λx. e c (λx. e) v e[v/x] e 1 e 2 e 1 e 2 τ ::= int τ τ Γ ::= Γ,x : τ e 2 e 2 ve 2 ve 2 e[e /x]:

More information

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015 TYPE INFERENCE François Pottier The Programming Languages Mentoring Workshop @ ICFP August 30, 2015 What is type inference? What is the type of this OCaml function? let f verbose msg = if verbose then

More information

Gradual Typing for Functional Languages

Gradual Typing for Functional Languages Gradual Typing for Functional Languages Jeremy G. Siek University of Colorado siek@cs.colorado.edu Walid Taha Rice University taha@rice.edu Abstract Static and dynamic type systems have well-known strengths

More information

Types and Type Inference

Types and Type Inference CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

More information

CMSC 631 Program Analysis and Understanding. Dynamic Typing, Contracts, and Gradual Typing

CMSC 631 Program Analysis and Understanding. Dynamic Typing, Contracts, and Gradual Typing CMSC 631 Program Analysis and Understanding Dynamic Typing, Contracts, and Gradual Typing Static vs. Dynamic Typing Languages with Static Typing Examples: Ocaml, Java, C#, Scala, Haskell Typechecker proves

More information

Let Arguments Go First

Let Arguments Go First Let Arguments Go First Ningning Xie (B) and Bruno C. d. S. Oliveira The University of Hong Kong, Pokfulam, Hong Kong {nnxie,bruno}@cs.hku.hk Abstract. Bi-directional type checking has proved to be an extremely

More information

Simple Unification-based Type Inference for GADTs

Simple Unification-based Type Inference for GADTs Simple Unification-based Type Inference for GADTs Stephanie Weirich University of Pennsylvania joint work with Dimitrios Vytiniotis, Simon Peyton Jones and Geoffrey Washburn Overview Goal: Add GADTs to

More information

CSE-321 Programming Languages 2010 Final

CSE-321 Programming Languages 2010 Final Name: Hemos ID: CSE-321 Programming Languages 2010 Final Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 18 28 16 12 36 40 150 There are six problems on 16 pages, including two work sheets, in

More information

Generalised Recursion in ML and Mixins

Generalised Recursion in ML and Mixins Generalised Recursion in ML and Mixins Seminar in Aalborg November 24th, 2004 Pascal Zimmer pzimmer@daimi.au.dk BRICS Generalised Recursion in ML and Mixins p. 1 General motivation To design a base language

More information

CSE-321 Programming Languages 2014 Final

CSE-321 Programming Languages 2014 Final Name: Hemos ID: CSE-321 Programming Languages 2014 Final Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 Total Score Max 15 12 14 14 30 15 100 There are six problems on 12 pages in this exam.

More information

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus. Variables and Functions. cs3723 1 Lambda Calculus Variables and Functions cs3723 1 Lambda Calculus Mathematical system for functions Computation with functions Captures essence of variable binding Function parameters and substitution Can

More information

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University Higher-Order Intensional Type Analysis Stephanie Weirich Cornell University Reflection A style of programming that supports the run-time discovery of program information. What does this code do? How is

More information

ML Has Principal Typings

ML Has Principal Typings Electronic Notes in Theoretical Computer Science 38 (2005) URL: http://www.elsevier.nl/locate/entcs/volume38.html ML Has Principal Typings Carlos Camarão and Lucília Figueiredo Abstract Is there a type

More information

Typed Scheme From Scripts to Programs. Sam Tobin-Hochstadt Northeastern University

Typed Scheme From Scripts to Programs. Sam Tobin-Hochstadt Northeastern University Typed Scheme From Scripts to Programs Sam Tobin-Hochstadt Northeastern University 1 The PL Renaissance 2 The PL Renaissance 3 The PL Renaissance 4 What s good These languages are interactive designed for

More information

Functional Languages and Higher-Order Functions

Functional Languages and Higher-Order Functions Functional Languages and Higher-Order Functions Leonidas Fegaras CSE 5317/4305 L12: Higher-Order Functions 1 First-Class Functions Values of some type are first-class if They can be assigned to local variables

More information

Some instance messages and methods

Some instance messages and methods Some instance messages and methods x ^x y ^y movedx: dx Dy: dy x

More information

On Polymorphic Gradual Typing

On Polymorphic Gradual Typing On Polymorphic Gradual Typing YUU IGARASHI, Kyoto University TARO SEKIYAMA, IBM Research - Tokyo ATSUSHI IGARASHI, Kyoto University We study an extension of gradual typing a method to integrate dynamic

More information

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011 CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011 In this lecture we introduce the topic of scope in the context of the λ-calculus and define translations from λ-cbv to FL for the two most common

More information

Simply-Typed Lambda Calculus

Simply-Typed Lambda Calculus #1 Simply-Typed Lambda Calculus #2 Back to School What is operational semantics? When would you use contextual (small-step) semantics? What is denotational semantics? What is axiomatic semantics? What

More information

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Introduction to Lambda Calculus. Lecture 7 CS /08/09 Introduction to Lambda Calculus Lecture 7 CS 565 02/08/09 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

A type directed translation of MLF to System F

A type directed translation of MLF to System F A type directed translation of MLF to System F Daan Leijen Microsoft Research daan@microsoft.com Abstract The MLF type system by Le Botlan and Rémy (2003) is a natural extension of Hindley-Milner type

More information

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

More information

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008 Softwaretechnik Lecture 03: Types and Type Soundness Peter Thiemann University of Freiburg, Germany SS 2008 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 1 / 35 Table of Contents Types and Type correctness

More information

Typing Multi-Staged Programs and Beyond

Typing Multi-Staged Programs and Beyond Theorem Corollary Definition Lemma Research on Software Analysis for Error-free Computing Center Seoul National University 12/3/2010 @ Tsinghua Univ., Beijing (co-work with Iksoon Kim, Cristiano Calcagno,

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2018 Lecture 7b Andrew Tolmach Portland State University 1994-2018 Dynamic Type Checking Static type checking offers the great advantage of catching errors early And

More information

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going? Types for References, Exceptions and Continuations Annoucements How s the midterm going? Meeting 21, CSCI 5535, Spring 2009 2 One-Slide Summary Review of Subtyping If τ is a subtype of σ then any expression

More information

Dynamics in ML. Xavier Leroy, Michel Mauny. To cite this version: HAL Id: hal https://hal.inria.fr/hal

Dynamics in ML. Xavier Leroy, Michel Mauny. To cite this version: HAL Id: hal https://hal.inria.fr/hal Dynamics in ML Xavier Leroy, Michel Mauny To cite this version: Xavier Leroy, Michel Mauny. Dynamics in ML. Journal of Functional Programming, Cambridge University Press (CUP), 1993, 3 (4), pp.431-463.

More information

Edinburgh Research Explorer

Edinburgh Research Explorer Edinburgh Research Explorer Well-Typed Programs Can't Be Blamed Citation for published version: Wadler, P & Findler, RB 2009, Well-Typed Programs Can't Be Blamed. in G Castagna (ed.), Programming Languages

More information

CSE-321 Programming Languages 2011 Final

CSE-321 Programming Languages 2011 Final Name: Hemos ID: CSE-321 Programming Languages 2011 Final Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 15 15 10 17 18 25 100 There are six problems on 18 pages in this exam, including one extracredit

More information

Advances in Programming Languages

Advances in Programming Languages Advances in Programming Languages Lecture 4: Higher Polymorphism Ian Stark School of Informatics The University of Edinburgh Friday 30 September 2016 Semester 1 Week 2 https://blog.inf.ed.ac.uk/apl16 Topic:

More information

Three Applications of Strictness in the Efficient Implementaton of Higher-Order Terms

Three Applications of Strictness in the Efficient Implementaton of Higher-Order Terms Three Applications of Strictness in the Efficient Implementaton of Higher-Order Terms Frank Pfenning Workshop on Implementation of Logic Réunion Island, France November 11, 2000 Joint work with Carsten

More information

CSE-321 Programming Languages 2012 Midterm

CSE-321 Programming Languages 2012 Midterm Name: Hemos ID: CSE-321 Programming Languages 2012 Midterm Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 14 15 29 20 7 15 100 There are six problems on 24 pages in this exam. The maximum score

More information

Contract Inference for the Ask-Elle Programming Tutor

Contract Inference for the Ask-Elle Programming Tutor Department of Computing Science MSc Thesis, ICA-3720942 Contract Inference for the Ask-Elle Programming Tutor Author: Beerend Lauwers Supervisor: Prof. dr. J.T. Jeuring May 1, 2014 Abstract Ask-Elle [8]

More information

Calculus INT. Value Types A, B ::= types of the low-level language Interactive Types X, Y ::= [A] A X X Y A X

Calculus INT. Value Types A, B ::= types of the low-level language Interactive Types X, Y ::= [A] A X X Y A X Calculus INT Calculus INT Value Types A, B ::= types of the low-level language Interactive Types X, Y ::= [A] A X X Y A X X Y α A. X α A. X Related: Call-by-Push-Value [Levy 2004] Enrichted Effect Calculus

More information

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Blame, coercions, and threesomes, precisely

Blame, coercions, and threesomes, precisely Blame, coercions, and threesomes, precisely Jeremy Siek Indiana University jsiek@indiana.edu Peter Thiemann Universität Freiburg thiemann@informatik.uni-freiburg.de Philip Wadler University of Edinburgh

More information

Overview. Elements of Programming Languages. Another example. Consider the humble identity function

Overview. Elements of Programming Languages. Another example. Consider the humble identity function Overview Elements of Programming Languages Lecture 8: Polymorphism and type inference James Cheney University of Edinburgh October 21, 2016 This week and next week, we will cover di erent forms of abstraction

More information

Topic 16: Issues in compiling functional and object-oriented languages

Topic 16: Issues in compiling functional and object-oriented languages Topic 16: Issues in compiling functional and object-oriented languages COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Compiling functional and OO languages General structure

More information