Incremental Interactive Computation · 10/7/2014  · Tuesday, October 7, 2014 10. Computing...

Preview:

Citation preview

Incremental Interactive Computation

Matthew Hammer hammer@cs.umd.edu

1Tuesday, October 7, 2014

Incremental Interactive Computation

Matthew Hammer hammer@cs.umd.edu

2Tuesday, October 7, 2014

Batch Computation vs Interactive Computation

InputOutputBatch Model

InteractiveModel

Input

Output

C

CInputOutput

U

No time,no dialogue

Dialogue in time

3Tuesday, October 7, 2014

Interaction is a DialogueCreates input

Demands Output

CU

InteractingUser

ReactiveComputation

Mutates input

Demands Output

TimeTime

4Tuesday, October 7, 2014

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

Incremental Interactive Computation

6Tuesday, October 7, 2014

Incremental Interactive Computation

Input

Output

CInputOutput

U

Claim: Interesting interactive

systems consist of incremental

computations

7Tuesday, October 7, 2014

Example: SpreadsheetsInput

Structure Cell Formulae

IncrementalComputation

Formula evaluation

OutputStructure Cell values

8Tuesday, October 7, 2014

Example: Word processing

InputStructure

Document Content

IncrementalComputation

Spell-check each word

OutputStructure

Highlightmisspellings

9Tuesday, October 7, 2014

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

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

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

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

AdaptonProgramming Abstractionsfor Incremental Interaction

14Tuesday, October 7, 2014

Adapton Programming Abstractions

•Mutable references:

• Hold changing input structure

• Lazy thunks:

• Demand-driven computations

• Output structure

15Tuesday, October 7, 2014

max

1 2 3 4

+ min

+ *

max min

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

16Tuesday, October 7, 2014

max

1 2 3 4

+ min

+ *

max min

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

17Tuesday, October 7, 2014

max2

1 2 3 4

+5

min

+7

*

max7

min

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

18Tuesday, October 7, 2014

max2

1 2 3 4

+5

min

+7

*

max7

min

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs Switch Demand:

19Tuesday, October 7, 2014

max2

1 2 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs Switch Demand:

20Tuesday, October 7, 2014

max2

1 2 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

Sharing

21Tuesday, October 7, 2014

max2

1 2 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs

Input Change:

22Tuesday, October 7, 2014

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

max2

1 10 3 4

+5

min3

+7

*15

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs Demand new output:

24Tuesday, October 7, 2014

max2

1 10 3 4

+13

min3

+7

*26

max7

min3

Mutablereferences

IncrementalComputation

(thunks)

Demand-drivenOutputs Demand new output:

25Tuesday, October 7, 2014

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

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

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

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

Course Project

30Tuesday, October 7, 2014

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

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

Recommended