Top Banner
1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson , Ras Bodik, Koushik Sen UC Berkeley
21

CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

Jul 04, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

1 / 21

CodeHint: Dynamic and Interactive Synthesis of Code Snippets

Joel Galenson, Ras Bodik, Koushik SenUC Berkeley

Page 2: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

2 / 21

Motivation

Pictures by Mangpo Phitchaya Phothilimthana

Test cases

Desired type

Specification

Page 3: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

3 / 21

CodeHint: Autocomplete for the modern age

● Autocomplete is useful but very limited.● Our improvements:

– Being dynamic

– General specifications

– Synthesis

Page 4: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

4 / 21

Overview

CodeHint

Source code

Test case

Specification

CandidatesFind locals Evaluate Candidates

Combine

Refine

Page 5: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

5 / 21

Example: The problem

Page 6: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

6 / 21

Example: First step

“Find a JMenuBar”

Page 7: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

7 / 21

Demo

Page 8: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

8 / 21

Basic algorithm

● Find local variables, do BFS over Java statements, show user those that pass spec.– Actually evaluate these statements, including file

I/O, reflection, etc.

x

y

Iteration 1

x

+

y

x

max

y

Iteration 2 Iteration 3

x

max

y

average

x

+

y

...

... ......

Page 9: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

9 / 21

First iteration

● Find local variables.

x y tree nullthis ...

Page 10: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

10 / 21

Second iteration

● Get each expression's type and combine it with others in type-correct ways.

xx

+

y x

<

y

...

tree(JTree)

getPathForLocation

x

tree

y (JTree)

getPathForLocation

y

tree

x tree

getTopLevelAncestor

...

tree

getWindowAncestor

SwingUtilities...

Page 11: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

11 / 21

Side effects

● Handle in-memory and external effects.

makeTree()

add

tree

children...

renderer makeTree()

...

tree

tree.children changed from torenderer renderer makeTree()

file

delete

SecurityManager

Breakpoints

In-memory:

External:

Page 12: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

12 / 21tree

getTopLevelAncestor

getJMenuBar

Equivalence classes

● Group equivalent code to avoid unneeded work.

tree

getRoot

SwingUtilitiestree

getTopLevelAncestor

tree

getTopLevelAncestor

getName

tree

getRoot

SwingUtilities

getName

tree

getRoot

SwingUtilities

getJMenuBar

tree

getTopLevelAncestor

getJMenuBar

Next iteration:

End of search:

Page 13: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

13 / 21

Probabilistic model

● Mined 10MLOC to build model of likely code.

● Use to avoid unlikely expressions.

Probability of accessing method/field m on type T:

Probability of using constant c as argument i to method m:

tree

getNextMatch

... KeyEvent.VK_ENTER

getComponent

tree

Page 14: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

14 / 21

Third iteration and result

tree

getTopLevelAncestor

(JFrame)

getJMenuBar

(JTree)

getRowForLocation

x

tree

y

+

1

...Third iteration:

Result:(JFrame)

getJMenuBar

tree

getRoot

SwingUtilitiestree

getTopLevelAncestor

(JFrame)

getJMenuBar

Page 15: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

15 / 21

Refinement

● Users can give another demonstration in a different state to refine the results.

(JFrame)

getJMenuBar

tree

getWindowAncestor

SwingUtilitiestree

getTopLevelAncestor

(JFrame)

getJMenuBar

...

Page 16: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

16 / 21

Synthesis from specifications

StrengthWeak Strong

Context-dependence

Context-independent

Context-dependent

APIexploration

Programsynthesis

Programmingby demonstration

Static typerv instanceof JMenuBar

Correctness conditionisSorted(array)

Value demonstrationsx == 42

x.toString().contains(“Eve”)

Page 17: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

17 / 21

Empirical results

In real-world code, ~95% of expressions need ≤3 iterations and ~99% need ≤4.

Our algorithms workwell in practice.

Page 18: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

18 / 21

User studies

● Completed two user studies with 28 subjects.● Found statistically-significant productivity

improvements.– Fewer bugs and more tasks completed in less time.

CodeHint makes programmersmore productive.

Lower is better Higher is better

Page 19: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

19 / 21

Future work

● Improving the probabilistic model● CodeHint for JavaScript

– https://github.com/jgalenson/codehint.js

● Integrating symbolic techniques

Page 20: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

20 / 21

Probabilistic search

foo

;

bar

tree

getWindowAncestor

SwingUtilities

...

Model

Candidates

tree

getWindowAncestor

SwingUtilities

(JFrame)

getJMenuBar

Page 21: CodeHint: Dynamic and Interactive Synthesis of Code Snippets · 1 / 21 CodeHint: Dynamic and Interactive Synthesis of Code Snippets Joel Galenson, Ras Bodik, Koushik Sen UC Berkeley.

21 / 21

Summary

● Dynamic and interactive synthesis● Autocomplete for the modern age● User studies showed productivity improvements

Thanks!https://jgalenson.github.io/codehint/