SEMINAL: Efficiently Searching for Type-Error Messages Benjamin Lerner Dan Grossman, Craig Chambers University of Washington.

Slides:



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

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
Modern Programming Languages, 2nd ed.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Searching for Type-Error Messages Benjamin Lerner, Matthew Flower, Dan Grossman, Craig Chambers University of Washington.
S EMINAL : Searching for ML Type-Error Messages Benjamin Lerner, Dan Grossman, Craig Chambers University of Washington.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms Dan Grossman Fall 2011.
Visualizing Type Error Messages Allison Thompson CSCI 5535 Spring 2010.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Semantics for MinML COS 441 Princeton University Fall 2004.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Semantic Analysis Having figured out the program’s structure, now figure out what it means.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages Dan Grossman 2013
Principles of programming languages 12: Functional programming
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 11 Type Inference
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
PROGRAMMING IN HASKELL
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

SEMINAL: Efficiently Searching for Type-Error Messages Benjamin Lerner Dan Grossman, Craig Chambers University of Washington

2 General outline Introduction Functional programming overview Type inference Running example Seminal approach

3 Functional programming review: Currying Let plus a b = a + b; plus : int -> int -> int Let pf = plus 5; What?? (plus 5) : int -> int If you give it a number, it will add 5 to it This is a new function! Called Currying, or partial application

4 Functional programming review: Lists An empty list is written [] Add one element: newElem :: oldList 3 :: [4; 5; 6] == [3; 4; 5; 6] Modify all elements: map List.map (fun x -> 2*x) [3; 4; 5] = [6; 8; 10] List.map : (‘a -> ‘b) -> ‘a list -> ‘b list

5 Functional programming review: Pairs and tuples A pair of integers (5, 6) : int * int A triple (5, ‘x’, false) : int * char * bool Combining lists into pairs: List.combine [1;2;3] [4;5;6] = [(1,4); (2,5); (3;6)] List.combine : ‘a list -> ‘b list -> (‘a * ‘b) list

6 Type inference Why do I have to write types out all the time? Why can’t the compiler figure it out for me? C++: list > *picList = new list >() ; picList->push(new pair (42,’x’) ); ML: let icList = ref []; icList := (42,’x’) :: !icList;

7 Type inference It can! …It takes a little bit of work to do

8 Type inference Code: let icList = ref []; icList := (42,’x’) :: !icList; Facts: [] : ‘a list ref [] : (‘a list) ref icList : ‘b 42 : int ‘x’ : char (42, ‘x’) : int * char (::) : ‘c -> ‘c list -> ‘c list !icList : ‘a list (42,’x’) :: !icList : (int * char) list (:=) : ‘d ref -> ‘d -> unit Equalities: ‘b = (‘a list) ref ‘c = int * char ‘a list = ‘c list ‘d = (int * char) list

9 Example: Curried functions # let map2 f aList bList = List.map (fun (a, b) -> f a b) (List.combine aList bList);; val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list = # map2 (fun (x, y) -> x + y) [1;2;3] [4;5;6];; This expression has type int but is here used with type 'a -> 'b

10 Example: Curried functions # let map2 f aList bList = List.map (fun (a, b) -> f a b) (List.combine aList bList);; val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list = # map2 (fun (x, y) -> x + y) [1;2;3] [4;5;6];; This expression has type int but is here used with type 'a -> 'b Try replacing fun (x, y) -> x + y with fun x y -> x + y

11 How are messages generated? Type Checker Error message generation Error messages Compiler Back-end Must be trusted Compiler Front-end Existing compilers:

12 Do we actually do this? class AddExpr extends Expr { Expr arg1; Expr arg2; ResolvedType typecheck(CodeSymbolTable st) throws TypecheckCompilerException { ResolvedType arg1_type = arg1.typecheck(st); ResolvedType arg2_type = arg2.typecheck(st); arg1_type.checkIsInt(); arg2_type.checkIsInt(); return ResolvedType.intType(); }

13 How are messages generated? SEMINAL Message Generation Error messages Compiler Back-end Must be trusted Compiler Front-end SEMINAL: Type Checker

14 Our approach, in one slide Treats type checker as oracle Makes no assumptions about type system Tries many variations on program, see which ones type-check “Variant type-checks” ≠ “Variant is right” Ranks successful suggestions, presents results to programmer Programmer knows which are right

15 Example: Curried functions # let map2 f xs ys = List.map (fun (x, y) -> f x y) (List.combine xs ys);; val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list = # map2 (fun (x, y) -> x + y) [1;2;3] [4;5;6];; This expression has type int but is here used with type 'a -> 'b Suggestions: Try replacing fun (x, y) -> x + y with fun x y -> x + y of type int -> int -> int within context (map2 (fun x y -> x + y) [1; 2; 3] [4; 5; 6])

16 Finding the changes, part 0 Change let map2 f aList bList = … ;; map2 (fun (x, y) -> x+y) [1;2;3] [4;5;6] Into…   map2 (fun(x,y)->x+y) [1;2;3] [4;5;6] let map2 f aList bList = … ;;  Note:  is a placeholder that always type-checks

17 Finding the changes, part 1 Change map2 (fun (x, y) -> x+y) [1;2;3] [4;5;6] Into…  map2 ((fun(x,y)->x+y), [1;2;3], [4;5;6])  map2 ((fun(x,y)->x+y) [1;2;3] [4;5;6]) …  (fun (x,y)->x+y) [1;2;3] [4;5;6] map2  [1;2;3] [4;5;6]  map2 (fun (x,y)->x+y)  [4;5;6]  map2 (fun (x,y)->x+y) [1;2;3] 

18 Finding the changes, part 2 Change (fun (x, y) -> x + y) Into…  fun (x, y)  -> x + y  fun  (x, y) -> x + y  fun (y, x) -> x + y … fun x y -> x + y

19 Ranking the suggestions Replace map2 (fun (x,y)->x+y) [1;2;3] [4;5;6] with  Replace map2 with  Replace (fun (x,y)->x+y) with  Replace (fun (x,y)->x+y) with (fun x y -> x+y) Prefer smaller changes over larger ones Prefer non-deleting changes over others

20 Tidying up Find type of replacement Get this for free from TC Maintain surrounding context Help user locate the replacement Suggestions: Try replacing fun (x, y) -> x + y with fun x y -> x + y of type int -> int -> int within context (map2 (fun x y -> x + y) [1; 2; 3] [4; 5; 6])

21 Conclusions Searching for error messages works! Yields useful messages as often as current compiler Can be improved easily Good for programmers, good for compiler writers