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

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

    20-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    0

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

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

SEMINAL:Efficiently Searching for Type-Error Messages

Benjamin Lerner

Dan Grossman, Craig Chambers

University of Washington

Page 2: 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

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

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

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

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

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

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

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

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<pair<int,char>> *picList = new list<pair<int,char>>();

picList->push(new pair<int,char>(42,’x’));

ML:

let icList = ref [];

icList := (42,’x’) :: !icList;

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

7

Type inference

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

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

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

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

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 = <fun>

# 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

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

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 = <fun>

# 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

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

11

How are messages generated?

Type Checker

Error messagegeneration

Errormessages

CompilerBack-end

Must be trusted

CompilerFront-end

Existing compilers:

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

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();

}

}

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

13

How are messages generated?

SEMINALMessage

Generation

Errormessages

CompilerBack-end

Must be trusted

CompilerFront-end

SEMINAL:

TypeChecker

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

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

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

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 = <fun>

# map2 (fun (x, y) -> x + y) [1;2;3] [4;5;6];;This expression has type intbut is here used with type'a -> 'b

Suggestions:Try replacing fun (x, y) -> x + ywith fun x y -> x + yof type int -> int -> intwithin context (map2 (fun x y -> x + y) [1; 2; 3] [4; 5; 6])

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

16

Finding the changes, part 0Change

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

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

17

Finding the changes, part 1Change

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]

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

18

Finding the changes, part 2Change

(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

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

19

Ranking the suggestions

Replacemap2 (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

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

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 + ywith fun x y -> x + yof type int -> int -> intwithin context (map2 (fun x y -> x + y) [1; 2; 3] [4; 5; 6])

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

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