32
Incremental Interactive Computation Matthew Hammer [email protected] 1 Tuesday, October 7, 2014

Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Incremental Interactive Computation

Matthew Hammer [email protected]

1Tuesday, October 7, 2014

Page 2: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Incremental Interactive Computation

Matthew Hammer [email protected]

2Tuesday, October 7, 2014

Page 3: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Batch Computation vs Interactive Computation

InputOutputBatch Model

InteractiveModel

Input

Output

C

CInputOutput

U

No time,no dialogue

Dialogue in time

3Tuesday, October 7, 2014

Page 4: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Interaction is a DialogueCreates input

Demands Output

CU

InteractingUser

ReactiveComputation

Mutates input

Demands Output

TimeTime

4Tuesday, October 7, 2014

Page 5: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Elements of Interactive Dialogue

• User and system interact across time

• User mutates / changes input structure

• User demands / observes output structure

• The system maintains a correspondence between input and output structures

• I/O correspondence is computational

5Tuesday, October 7, 2014

Page 6: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Incremental Interactive Computation

6Tuesday, October 7, 2014

Page 7: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Incremental Interactive Computation

Input

Output

CInputOutput

U

Claim: Interesting interactive

systems consist of incremental

computations

7Tuesday, October 7, 2014

Page 8: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Example: SpreadsheetsInput

Structure Cell Formulae

IncrementalComputation

Formula evaluation

OutputStructure Cell values

8Tuesday, October 7, 2014

Page 9: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Example: Word processing

InputStructure

Document Content

IncrementalComputation

Spell-check each word

OutputStructure

Highlightmisspellings

9Tuesday, October 7, 2014

Page 10: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Example: ProgrammingInput

StructureText file

Computation Parse tokensOutput

StructureAST

InputStructure

ASTComputation Lexical scoping

OutputStructure

Def/use edges

InputStructure

AST + Def/useComputation Type inference

OutputStructure

Type errors

10Tuesday, October 7, 2014

Page 11: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Computing Incrementally1. Input changes are gradual

2. Output observation is limited

Full re-computation is often redundant

Full re-computation is often overly eager

11Tuesday, October 7, 2014

Page 12: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Computing Incrementally

Example:Spreadsheet Cells change slowly

Example:Word processing

Document and dictionary both change slowly

Example:Programming

Program changes slowly

1. Input changes are gradual

2. Output observation is limited

12Tuesday, October 7, 2014

Page 13: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Computing Incrementally

Example:Spreadsheet

One worksheet is active, others hidden

Example:Word processing

Viewport shows one or two pages

Example:Programming

Viewport shows one file, module or function

1. Input changes are gradual

2. Output observation is limited

13Tuesday, October 7, 2014

Page 14: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

AdaptonProgramming Abstractionsfor Incremental Interaction

14Tuesday, October 7, 2014

Page 15: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Adapton Programming Abstractions

•Mutable references:

• Hold changing input structure

• Lazy thunks:

• Demand-driven computations

• Output structure

15Tuesday, October 7, 2014

Page 16: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max

1 2 3 4

+ min

+ *

max min

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

16Tuesday, October 7, 2014

Page 17: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max

1 2 3 4

+ min

+ *

max min

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

17Tuesday, October 7, 2014

Page 18: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max2

1 2 3 4

+5

min

+7

*

max7

min

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

18Tuesday, October 7, 2014

Page 19: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max2

1 2 3 4

+5

min

+7

*

max7

min

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs Switch Demand:

19Tuesday, October 7, 2014

Page 20: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max2

1 2 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs Switch Demand:

20Tuesday, October 7, 2014

Page 21: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max2

1 2 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

Sharing

21Tuesday, October 7, 2014

Page 22: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max2

1 2 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

Input Change:

22Tuesday, October 7, 2014

Page 23: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max2

1 10 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

Some previous results are affected

Input Change:

23Tuesday, October 7, 2014

Page 24: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max2

1 10 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs Demand new output:

24Tuesday, October 7, 2014

Page 25: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

max2

1 10 3 4

+13

min3

+7

*26

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs Demand new output:

25Tuesday, October 7, 2014

Page 26: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Lazy Structures• Spreadsheet example:

• Each thunk returns a single number

• Lazy Lists:

• Each thunk returns `Nil or `Cons

• `Cons holds head value and a thunk tail

• Laziness can be applied to trees, graphs, and essentially any other data structure

• Inputs: Special thunks are mutable

26Tuesday, October 7, 2014

Page 27: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Background: Lazy Liststype 'a thunk = unit -> 'a

let force : 'a thunk -> 'a = fun t -> t () type 'a lzlist = [ | `Nil | `Cons of 'a * ('a lzlist thunk) ]

let rec from_list l = match l with | [] -> `Nil | h::t -> `Cons(h,fun()->from_list t)

Apply unit argument

27Tuesday, October 7, 2014

Page 28: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

type 'a lzlist = [ | `Nil | `Cons of 'a * ('a lzlist thunk) ]

let rec merge l1 l2 = function |l1, `Nil -> l1 |`Nil, l2 -> l2

|`Cons(h1,t1), `Cons(h2,t2) -> if h1 <= h2 then `Cons(h1, fun()->merge (force t1) l2) else `Cons(h2, fun()->merge l1 (force t2))

28Tuesday, October 7, 2014

Page 29: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Mergesort ExampleInput [3,5,8,2,1,7]

Singletons [ [3], [5], [8], [2], [1], [7] ]

Merge #1 [ [3,5], [2,8], [1,7] ]

Merge #2 [ [3,5], [1,2,7,8] ]

Merge #3 [ [3,5], [1,2,7,8] ]

Merge #4 [ [1, 2, 3, 5, 7, 8 ] ]

Flatten [1, 2, 3, 5, 7, 8 ]

29Tuesday, October 7, 2014

Page 30: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Course Project

30Tuesday, October 7, 2014

Page 31: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Project: Interactive Program Analysis

• Assume: Interactive “Structure Editor”

• Programmer manipulates AST directly

• Learn: Adapton IC framework

• Build: Incremental Program Analysis

• Example: Use/def information

• Example: Type Inference

• Example: Control-flow analysis

31Tuesday, October 7, 2014

Page 32: Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing Incrementally 1. Input changes are gradual 2. Output observation is limited Full re-computation

Background: Structure Editors

• Philosophical claims:

• Programs consist of rich structure

• Rich interaction exposes this structure

• Example prototypes:

• Haskell -- http://www.youtube.com/watch?v=v2ypDcUM06U

• Citris -- http://www.youtube.com/watch?v=47UcOspbZ2k

• TouchDevelop -- http://www.youtube.com/watch?v=a6GRg2glKpc

32Tuesday, October 7, 2014