Type Inference with Run-time Logs Ravi Chugh. Motivation: Dynamic Languages Dynamically-typed languages – Enable rapid prototyping – Facilitate inter-language.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Dynamic Typing COS 441 Princeton University Fall 2004.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
1 Space-Efficient Gradual Typing David Herman Northeastern University Aaron Tomb, Cormac Flanagan University of California, Santa Cruz.
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
Safety as a Software Metric Matthias Felleisen and Robert Corky Cartwright Rice University.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Type Inference with Run-time Logs Ravi Chugh, Ranjit Jhala, Sorin Lerner University of California, San Diego.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Inference and Checking of Object Ownership Wei Huang 1, Werner Dietl 2, Ana Milanova 1, Michael D. Ernst 2 1 Rensselaer Polytechnic Institute 2 University.
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Type Inference with Run-time Logs Ravi Chugh. Motivation: Dynamic Languages Dynamically-typed languages – Enable rapid prototyping – Facilitate inter-language.
Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Just Enough Type Theory or, Featherweight Java A Simple Formal Model of Objects Jonathan Aldrich
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Shifting the Blame A blame calculus with delimited control Taro Sekiyama* Atsushi Igarashi Soichiro Ueda Kyoto University Gradual typing.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Type Inference with Run-time Logs Ravi Chugh, Ranjit Jhala, Sorin Lerner University of California, San Diego.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
The Ins and Outs of Gradual Type Inference Avik Chaudhuri Basil Hosmer Adobe Systems Aseem Rastogi Stony Brook University.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Dynamic Partial Evaluation Gregory T. Sullivan MIT AI Lab
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Functional Programming
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages Dan Grossman 2013
Semantic Analysis Type Checking
Type Inference with Run-time Logs
Data Types.
Programming Languages and Compilers (CS 421)
FP Foundations, Scheme In Text: Chapter 14.
Inlining and Devirtualization Hal Perkins Autumn 2011
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE 341 Lecture 11 b closures; scoping rules
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

Type Inference with Run-time Logs Ravi Chugh

Motivation: Dynamic Languages Dynamically-typed languages – Enable rapid prototyping – Facilitate inter-language development Statically-typed languages – Prevent certain run-time errors – Enable optimized execution – Provide checked documentation Many research efforts to combine both – Recent popularity of Python, Ruby, and JavaScript has stoked the fire 2

Gradual Type Systems Many attempts at fully static type systems – Often require refactoring and annotation – Some features just cannot be statically typed Gradual type systems provide a spectrum – Some expressions have types, statically-checked – Others are dynamic, fall back on run-time checks Challenges – Granularity, guarantees, blame tracking, and... 3

... Inference! Goal: migrate programs from dynamic langs Programmer annotation burden is currently 0 A migration strategy must require ~0 work – Even modifying 1-10% LOC in a large codebase can be prohibitive 4

The Challenge: Inference 5 Goal: practical type system polymorphism def id(x) { return x }; 1 + id(2); “hello” ^ id(“world”); polymorphism id : ∀ X. X → X

The Challenge: Inference 6 Goal: practical type system subtypingpolymorphism def succA(x) { return (1 + x.a) }; succA({a=1}); succA({a=1;b=“hi”}); subtyping {a:Int;b:Str} <: {a:Int}

The Challenge: Inference bounded quantification 7 Goal: practical type system subtypingpolymorphism def incA(x) { x.a := 1 + x.a; return x }; incA({a=1}).a; incA({a=1;b=“hi”}).b; bounded quantification incA : ∀ X<:{a:Int}. X → X

The Challenge: Inference bounded quantification 8 Goal: practical type system subtypingpolymorphism dynamic n := if b then 0 else “bad”; m := if b then n + 1 else 0; dynamic n : dynamic

The Challenge: Inference bounded quantification 9 Goal: practical type system subtypingpolymorphism dynamic other features...

The Challenge: Inference bounded quantification 10 Goal: practical type system subtypingpolymorphism dynamic other features... System ML ✓

The Challenge: Inference bounded quantification 11 Goal: practical type system subtypingpolymorphism dynamic other features... System F ✗

The Idea Use run-time executions to help inference Program may have many valid types – But particular execution might rule out some Dynamic language programmers test a lot – Use existing test suites to help migration 12

Route: Inference w/ Run-time Logs bounded quantification 13 subtyping + polymorphism omit higher-order functions System E System E ≤

First Stop bounded quantification 14 subtyping + polymorphism omit higher-order functions System E System E ≤ System E −

E − Type System Expression and function types τ ::= | Int | Bool |... | {f i :τ i } | X σ ::= ∀ X i. τ 1 → τ 2 Typing rules prevent field-not-found and primitive operation errors 15

def id (a) { a } def id[A] (a:A) { a } : ∀ A. A → A def id[] (a:Int) { a } : Int → Int def id[B,C] (x:B*C) { a } : ∀ B,C. B*C → B*C Infinitely many valid types for id... 16

... but ∀ A. A → A is the principal type 17 ∀ A. A → A ∀ B,C. B*C → B*C Int → Int

... but ∀ A. A → A is the principal type More general than every other type Allows id to be used in most different ways 18 ∀ B,C. B*C → B*C ∀ B. B*B → B*B Int*Int → Int*IntInt*Bool → Int*Bool ∀ A. A → A Int → Int

def readF (o) { o.f } def readF[F] (o:{f:F}) { o.f } : ∀ F. {f:F} → F This is the best type 19 ∀ F. {f:F} → F ∀ F,G. {f:F;g:G} → F {f:Int} → Int ∀ G. {f:Int;g:G} → Int

def foo (x) { let _ = x.f in x } Two valid types: Neither is better than the other 20 ∀ F. {f:F} → {f:F} ∀ F,G. {f:F;g:G} → {f:F;g:G} allows foo({f=1;g=2}).g allows foo({f=1})

E − Static Type Inference E − lacks principal types – Cannot assign type just from definition – Need to consider calling contexts Our approach is iterative – Impose minimal constraints on argument – If a calling context requires an additional field to be tracked, backtrack and redo the function 21

22 def foo (x) { let _ = x.f in o } def main () { foo({f=1}) } Iterative Inference – Example 1 X <: {f:F} foo : ∀ F. {f:F} → {f:F} x Constraints on arg of fooS olution X = {f:F} x ✓ “this record type came from the program variable x ”

23 def foo (x) { let _ = x.f in o } def main () { let z = {f=1;g=2} in foo(z).g } Iterative Inference – Example 2 X <: {f:F} foo : ∀ F. {f:F} → {f:F} x Constraints on arg of fooS olution X = {f:F} x ✗*✗* foo(z) : {f:Int} x so can’t project on g, unless... X <: {g:G}

24 def foo (x) { let _ = x.f in o } def main () { let z = {f=1;g=2} in foo(z).g } Iterative Inference – Example 2 X <: {f:F} foo : ∀ F,G. {f:F;g:G} → {f:F;g:G} x Constraints on arg of fooS olution X <: {g:G} X = {f:F;g:G} x ✓

25 def foo (x) { let _ = x.f in o } def main () { let z = {f=1;g=2} in foo(z).g; foo({f=1}) } Iterative Inference – Example 3 X <: {f:F} Constraints on arg of foo X <: {g:G} foo : ∀ F,G. {f:F;g:G} → {f:F;g:G} x S olution X = {f:F;g:G} x

26 def foo (x) { let _ = x.f in o } def main () { let z = {f=1;g=2} in foo(z).g; foo({f=1}) } Iterative Inference – Example 3 X <: {f:F} Constraints on arg of foo X <: {g:G} foo : ∀ F,G. {f:F;g:G} → {f:F;g:G} x S olution X = {f:F;g:G} x ✓ ✗

System E − Summary 27 Fully static inference needs to iterate, but can optimize with run-time information Can wrap run-time values with sets of type variables and record field read constraints If all expressions executed, then – log contains all caller-induced constraints – no need for iteration

Next Stop bounded quantification 28 subtyping + polymorphism System E System E ≤ System E −

If-expressions Type is a supertype of branch types 29 if b then 1 else 2 if b then 1 else true if b then {f=1; g=“”} else {f=2; h=true} if b then {f=“”} else {f=true} : Int : {f:Int} ✗ : {}

def bar (y,z) { if 1 y.n > 0 then y else z } Three valid incomparable types: 30 ∀ A. {n:Int}*{} → {} ∀ A. {n:Int}*{n:Int} → {n:Int} ∀ B. {n:Int;b:B}*{n:Int;b:B} → {n:Int;b:B}

System E Summary Lacks principal types Has iterative inference – Use subscripts for record types from if-expressions Run-time info removes iteration, as before 31

Last Stop bounded quantification 32 subtyping + polymorphism System E ≤ System E System E −

33 def foo (x) { let _ = x.f in x } Now there is a best type for foo Each call site can instantiate X differently 33 ∀ F. {f:F} → {f:F} ∀ F,G. {f:F;g:G} → {f:F;g:G} ∀ F, X<:{f:F}. X → X

def bar (y,z) { if 1 y.n > 0 then y else z } 34 ∀ Y<:{n:Int}. Y * Y → Y {n:Int}*{n:Int} → {n:Int} ∀ B. {n:Int;b:B}*{n:Int;b:B} → {n:Int;b:B} {n:Int}*{} → {}

def bar (y,z) { if 1 y.n > 0 then y else z } 35 ∀ Y<:{n:Int}. Y * Y → Y allows bar({n=1},{n=2}).n allows bar({n=1},{}) and bar({n=1,b=true}, {n=2,b=false}).b Neither type is better {n:Int}*{} → {}

System E ≤ Summary Lacks principal types Primary new challenge: determining when to use same type variable for y and z Static inference has new source of iteration – Cannot be eliminated with run-time info 36

Summary bounded quantification 37 subtyping + polymorphism System E ≤ System E System E −

Summary Iterative static inference for E ≤ – first-order functions, records – polymorphism, subtyping – bounded quantification Run-time information improves algorithms – reduces worst-case complexity – (not counting overhead for instrumented eval) 38

Future Work Direction #0: Prove formal properties for E ≤ Direction #1: Extend E ≤ – recursion, recursive types, dynamic, classes,... – is there still static inference? – how can run-time info help? – can existing programs be encoded in E? Direction #2: Inference for F – does inference become easier with run-time info? – if so, extend with more features – if not, heuristic-based approaches for migration 39

Thanks! Released under Creative Commons Attribution 2.0 Generic LicenseCreative Commons Attribution 2.0 Generic License 40 Original photoOriginal photo by Alaskan Dude

Extra Slides 41

Function definitions Function calls Program is sequence of function definitions Typed vs. Untyped Syntax 42 def y[A 1,...,A n ](x:τ){ e }def y(x){ e’ } y[τ 1,...,τ n ](e)y(e’)

Bounded Quantification 43 def y[ A 1 <:τ 1,..., A n <:τ n ] (x:τ) { e } Type parameters now have bounds

Related Work Complete inference for ML + records [Remy 89] – limited by ML polymorphism – and fields cannot be forgotten Type inference for F is undecidable [Wells 94] Local type inference for F [Pierce et al, Odersky et al, et al] – require top-level annotations, try to fill in rest – even partial inference for F undecidable [Pfenning 88] Type checking for F ≤ is undecidable [Pierce 91] 44

Related Work Static type systems for dynamic languages – type systems for JavaScript [Thiemann 05, et al] – Typed Scheme [Tobin-Hochstadt and Felleisen 08] – Diamondback Ruby [Furr et al 09 11] Gradual type systems – functions [Thatte 90, Cartwright and Fagan 91, Siek and Taha 06] – objects [Siek and Taha 07, Wrigstad et al 10, Bierman et al 10] Coercion insertion [Henglein/Rehof 95, Siek/Vachharajani 08] – do not deal with records 45