Top Banner
Thinking For Programmers Leslie Lamport Microsoft Research 0
374

Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Jul 15, 2015

Download

Software

Ishit Makwana
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: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Thinking For Programmers

Leslie LamportMicrosoft Research

0

Page 2: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why Think?

It helps us do most things:

Hunting a sabre-toothed tiger.

Building a house.

Writing a program.

0

Page 3: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why Think?

It helps us do most things:

Hunting a sabre-toothed tiger.

Building a house.

Writing a program.

0

Page 4: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why Think?

It helps us do most things:

Hunting a sabre-toothed tiger.

Building a house.

Writing a program.

0

Page 5: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why Think?

It helps us do most things:

Hunting a sabre-toothed tiger.

Building a house.

Writing a program.

0

Page 6: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why Think?

It helps us do most things:

Hunting a sabre-toothed tiger.

Building a house.

Writing a program.

0

Page 7: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

When to Think

Hunting a sabre-toothed tiger.

Before leaving the cave.

Building a house.

Before beginning construction.

Writing a program.

Before writing any code.

1

Page 8: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

When to Think

Hunting a sabre-toothed tiger.

Before leaving the cave.

Building a house.

Before beginning construction.

Writing a program.

Before writing any code.

1

Page 9: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

When to Think

Hunting a sabre-toothed tiger.

Before leaving the cave.

Building a house.

Before beginning construction.

Writing a program.

Before writing any code.

1

Page 10: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

When to Think

Hunting a sabre-toothed tiger.

Before leaving the cave.

Building a house.

Before beginning construction.

Writing a program.

Before writing any code.

1

Page 11: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

When to Think

Hunting a sabre-toothed tiger.

Before leaving the cave.

Building a house.

Before beginning construction.

Writing a program.

Before writing any code.

1

Page 12: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

When to Think

Hunting a sabre-toothed tiger.

Before leaving the cave.

Building a house.

Before beginning construction.

Writing a program.

Before writing any code.

1

Page 13: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

When to Think

Hunting a sabre-toothed tiger.

Before leaving the cave.

Building a house.

Before beginning construction.

Writing a program.

Before writing any code.

1

Page 14: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think

“Writing is nature’s way of letting you know howsloppy your thinking is.”

Guindon

To think, you have to write.

If you’re thinking without writing, you only think you’re thinking.

1

Page 15: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think

“Writing is nature’s way of letting you know howsloppy your thinking is.”

Guindon

To think, you have to write.

If you’re thinking without writing, you only think you’re thinking.

1

Page 16: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think

“Writing is nature’s way of letting you know howsloppy your thinking is.”

Guindon

To think, you have to write.

If you’re thinking without writing, you only think you’re thinking.

1

Page 17: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think

“Writing is nature’s way of letting you know howsloppy your thinking is.”

Guindon

To think, you have to write.

If you’re thinking without writing, you only think you’re thinking.

1

Page 18: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What to Write

Hunting a sabre-toothed tiger.

Writing not invented, dangerous activity.

Building a house.

Draw blueprints.

Writing a program.

Write a

2

Page 19: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What to Write

Hunting a sabre-toothed tiger.

Writing not invented, dangerous activity.

Building a house.

Draw blueprints.

Writing a program.

Write a

2

Page 20: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What to Write

Hunting a sabre-toothed tiger.

Writing not invented, dangerous activity.

Building a house.

Draw blueprints.

Writing a program.

Write a

2

Page 21: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What to Write

Hunting a sabre-toothed tiger.

Writing not invented, dangerous activity.

Building a house.

Draw blueprints.

Writing a program.

Write a

2

Page 22: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What to Write

Hunting a sabre-toothed tiger.

Writing not invented, dangerous activity.

Building a house.

Draw blueprints.

Writing a program.

Write a

2

Page 23: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What to Write

Hunting a sabre-toothed tiger.

Writing not invented, dangerous activity.

Building a house.

Draw blueprints.

Writing a program.

Write a

2

Page 24: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What to Write

Hunting a sabre-toothed tiger.

Writing not invented, dangerous activity.

Building a house.

Draw blueprints.

Writing a program.

Write a blueprint

2

Page 25: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What to Write

Hunting a sabre-toothed tiger.

Writing not invented, dangerous activity.

Building a house.

Draw blueprints.

Writing a program.

Write a blueprint specification.

2

Page 26: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifications

Don’t Panic!

3

Page 27: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifications

Don’t Panic!

3

Page 28: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifications

Don’t Panic!

This is a blueprint:

3

Page 29: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifications

Don’t Panic!

This is also a blueprint:

3

Page 30: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of blueprints

3

Page 31: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of blueprints

3

Page 32: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of blueprints

Very Detailed

3

Page 33: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of blueprints

Very Detailed

3

Page 34: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of blueprints

Very DetailedRough Sketch

3

Page 35: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of blueprints

Very DetailedRough Sketch

3

Page 36: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of blueprints

Very DetailedRough Sketch Ordinary

3

Page 37: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

5

Page 38: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

Formal

5

Page 39: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

FormalProse

5

Page 40: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

FormalProse Mathematical Prose

5

Page 41: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

Prose

Most code is really simple.

It can be specified with a couple of lines of prose.

5

Page 42: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

Prose

Most code is really simple.

It can be specified with a couple of lines of prose.

5

Page 43: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

Mathematical Prose

Some code is subtle.

It requires more thought.

5

Page 44: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

Mathematical Prose

Some code is subtle.

It requires more thought.

5

Page 45: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

Formal

Some code is complex or very subtle or critical.

5

Page 46: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

Formal

Some code is complex or very subtle or critical.

Especially in concurrent/distributed systems.

5

Page 47: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

A spectrum of specifications

Formal

Some code is complex or very subtle or critical.

We should use tools to check it.

5

Page 48: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Write a Spec

Writing requires thinking.

5

Page 49: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Write a Spec

Writing requires thinking.

5

Page 50: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think About Programs

Like a Scientist

A very successful way of thinking.

Science makes mathematical models of reality.

Astronomy:

Reality: planets have mountains, oceans, tides, weather, . . .

Model: planet a point mass with position & momentum.

6

Page 51: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think About Programs

Like a Scientist

A very successful way of thinking.

Science makes mathematical models of reality.

Astronomy:

Reality: planets have mountains, oceans, tides, weather, . . .

Model: planet a point mass with position & momentum.

6

Page 52: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think About Programs

Like a Scientist

A very successful way of thinking.

Science makes mathematical models of reality.

Astronomy:

Reality: planets have mountains, oceans, tides, weather, . . .

Model: planet a point mass with position & momentum.

6

Page 53: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think About Programs

Like a Scientist

A very successful way of thinking.

Science makes mathematical models of reality.

Astronomy:

Reality: planets have mountains, oceans, tides, weather, . . .

Model: planet a point mass with position & momentum.

6

Page 54: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think About Programs

Like a Scientist

A very successful way of thinking.

Science makes mathematical models of reality.

Astronomy:

Reality: planets have mountains, oceans, tides, weather, . . .

Model: planet a point mass with position & momentum.

6

Page 55: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think About Programs

Like a Scientist

A very successful way of thinking.

Science makes mathematical models of reality.

Astronomy:

Reality: planets have mountains, oceans, tides, weather, . . .

Model: planet a point mass with position & momentum.

6

Page 56: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Think About Programs

Like a Scientist

A very successful way of thinking.

Science makes mathematical models of reality.

Astronomy:

Reality: planets have mountains, oceans, tides, weather, . . .

Model: planet a point mass with position & momentum.

6

Page 57: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 58: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 59: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 60: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 61: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 62: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 63: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 64: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 65: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 66: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Computer Science

Reality: Digital systems

a processor chip

a game console

a computer executing a program...

Models: Turing machines

Partially ordered sets of events...

6

Page 67: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Two Most Useful Models

Functions

Sequences of States

7

Page 68: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Two Most Useful Models

Functions

Sequences of States

7

Page 69: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Two Most Useful Models

Functions

Sequences of States

7

Page 70: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

8

Page 71: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

8

Page 72: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

8

Page 73: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Not in TLA+

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

8

Page 74: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

8

Page 75: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

square(2) = 4

8

Page 76: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

Domain of square is {0,1,2,3, . . .} a.k.a. Nat

8

Page 77: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

To define a function, specify:

Domain of square = Nat

square(x ) = x2 for all x in its domain.

8

Page 78: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

To define a function, specify:

Domain of square = Nat

square(x ) = x2 for all x in its domain.

8

Page 79: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

Functions in math 6= functions in programming languages.

8

Page 80: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Functions

Model a program as a function mapping input(s) to output(s).

In math, a function is a set of ordered pairs.

Example: the square function on natural numbers

{〈0,0〉, 〈1,1〉, 〈2,4〉, 〈3,9〉, . . . }

Functions in math 6= functions in programming languages.

Math is much simpler.

8

Page 81: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Limitations of the Function Model

Specifies what a program does, but not how.

– Quicksort and bubble sort compute the same function.

Some programs don’t just map inputs to outputs.

– Some programs run “forever”.

– Operating systems

9

Page 82: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Limitations of the Function Model

Specifies what a program does, but not how.

– Quicksort and bubble sort compute the same function.

Some programs don’t just map inputs to outputs.

– Some programs run “forever”.

– Operating systems

9

Page 83: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Limitations of the Function Model

Specifies what a program does, but not how.

– Quicksort and bubble sort compute the same function.

Some programs don’t just map inputs to outputs.

– Some programs run “forever”.

– Operating systems

9

Page 84: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Limitations of the Function Model

Specifies what a program does, but not how.

– Quicksort and bubble sort compute the same function.

Some programs don’t just map inputs to outputs.

– Some programs run “forever”.

– Operating systems

9

Page 85: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Limitations of the Function Model

Specifies what a program does, but not how.

– Quicksort and bubble sort compute the same function.

Some programs don’t just map inputs to outputs.

– Some programs run “forever”.

– Operating systems

9

Page 86: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Limitations of the Function Model

Specifies what a program does, but not how.

– Quicksort and bubble sort compute the same function.

Some programs don’t just map inputs to outputs.

– Some programs run “forever”.

– Operating systems

9

Page 87: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Standard Behavioral Model

A program execution is represented by a behavior.

A behavior is a sequence of states.

A state is an assignment of values to variables.

A program is modeled by a set of behaviors:

the behaviors representing possible executions.

9

Page 88: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Standard Behavioral Model

A program execution is represented by a behavior.

A behavior is a sequence of states.

A state is an assignment of values to variables.

A program is modeled by a set of behaviors:

the behaviors representing possible executions.

9

Page 89: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Standard Behavioral Model

A program execution is represented by a behavior.

A behavior is a sequence of states.

A state is an assignment of values to variables.

A program is modeled by a set of behaviors:

the behaviors representing possible executions.

9

Page 90: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Standard Behavioral Model

A program execution is represented by a behavior.

A behavior is a sequence of states.

A state is an assignment of values to variables.

A program is modeled by a set of behaviors:

the behaviors representing possible executions.

9

Page 91: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Standard Behavioral Model

A program execution is represented by a behavior.

A behavior is a sequence of states.

A state is an assignment of values to variables.

A program is modeled by a set of behaviors:

the behaviors representing possible executions.

9

Page 92: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Standard Behavioral Model

A program execution is represented by a behavior.

A behavior is a sequence of states.

A state is an assignment of values to variables.

A program is modeled by a set of behaviors:

the behaviors representing possible executions.

9

Page 93: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An Example: Euclid’s Algorithm

Computes GCD of M and N by:

– Initialize x to M and y to N .

– Keep subtracting the smaller of x and y from the larger.

– Stop when x = y .

For M = 12 and N = 18, one behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

10

Page 94: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An Example: Euclid’s Algorithm

An algorithm is an abstract program.

For M = 12 and N = 18, one behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

10

Page 95: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An Example: Euclid’s Algorithm

Computes GCD of M and N by:

– Initialize x to M and y to N .

– Keep subtracting the smaller of x and y from the larger.

– Stop when x = y .

For M = 12 and N = 18, one behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

10

Page 96: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An Example: Euclid’s Algorithm

Computes GCD of M and N by:

– Initialize x to M and y to N .

– Keep subtracting the smaller of x and y from the larger.

– Stop when x = y .

For M = 12 and N = 18, one behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

10

Page 97: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An Example: Euclid’s Algorithm

Computes GCD of M and N by:

– Initialize x to M and y to N .

– Keep subtracting the smaller of x and y from the larger.

– Stop when x = y .

For M = 12 and N = 18, one behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

10

Page 98: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An Example: Euclid’s Algorithm

Computes GCD of M and N by:

– Initialize x to M and y to N .

– Keep subtracting the smaller of x and y from the larger.

– Stop when x = y .

For M = 12 and N = 18, one behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

10

Page 99: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Describe a Set of Behaviors

With two things:

– The set of possible initial states.

– A next-state relation,describing all possible successor states of any state.

11

Page 100: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Describe a Set of Behaviors

With two things:

– The set of possible initial states.

– A next-state relation,describing all possible successor states of any state.

11

Page 101: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Describe a Set of Behaviors

With two things:

– The set of possible initial states.

– A next-state relation,describing all possible successor states of any state.

11

Page 102: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Describe a Set of Behaviors

With two things:

– The set of possible initial states.

– A next-state relation,describing all possible successor states of any state.

11

Page 103: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Describe a Set of Behaviors

With two things:

– The set of possible initial states.

– A next-state relation,describing all possible successor states of any state.

For infinite behaviors, also need fairness.

11

Page 104: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Describe a Set of Behaviors

With two things:

– The set of possible initial states.

– A next-state relation,describing all possible successor states of any state.

For infinite behaviors, also need fairness.

I will ignore this.

11

Page 105: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Describe a Set of Behaviors

With two things:

– The set of possible initial states.

– A next-state relation,describing all possible successor states of any state.

What language should we use?

We can use math.

11

Page 106: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How to Describe a Set of Behaviors

With two things:

– The set of possible initial states.

– A next-state relation,describing all possible successor states of any state.

What language should we use?

We can use math.

11

Page 107: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Set of Initial States

Described by a formula.

For Euclid’s Algorithm: (x = M ) ∧ (y = N )

Only possible initial state: [x = M , y = N ]

12

Page 108: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Set of Initial States

Described by a formula.

For Euclid’s Algorithm: (x = M ) ∧ (y = N )

Only possible initial state: [x = M , y = N ]

12

Page 109: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Set of Initial States

Described by a formula.

For Euclid’s Algorithm: (x = M ) ∧ (y = N )

Only possible initial state: [x = M , y = N ]

12

Page 110: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Set of Initial States

Described by a formula.

For Euclid’s Algorithm: (x = M ) ∧ (y = N )

Only possible initial state: [x = M , y = N ]

12

Page 111: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Next-State Relation

Described by a formula.

Unprimed variables for current state,Primed variables for next state.

For Euclid’s Algorithm: ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

13

Page 112: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Next-State Relation

Described by a formula.

Unprimed variables for current state,Primed variables for next state.

For Euclid’s Algorithm: ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

13

Page 113: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Next-State Relation

Described by a formula.

Unprimed variables for current state,Primed variables for next state.

For Euclid’s Algorithm: ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

13

Page 114: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Next-State Relation

Described by a formula.

Unprimed variables for current state,Primed variables for next state.

For Euclid’s Algorithm: ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

13

Page 115: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Next-State Relation

Described by a formula.

Unprimed variables for current state,Primed variables for next state.

For Euclid’s Algorithm: ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

13

Page 116: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Next-State Relation

Described by a formula.

Unprimed variables for current state,Primed variables for next state.

For Euclid’s Algorithm: ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

13

Page 117: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Take M = 12, N = 18

Next : ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 118: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Take M = 12, N = 18

Next : ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 119: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 120: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = 12) ∧ (y = 18)

Next : ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 121: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 122: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 12 > 18∧ x ′ = 12− 18∧ y ′ = 18 )

∨ ( 18 > 12∧ y ′ = 18− 12∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 123: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( FALSE12 > 18∧ x ′ = 12− 18∧ y ′ = 18 )

∨ ( TRUE18 > 12∧ y ′ = 18− 12∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 124: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 12 > 18∧ x ′ = 12− 18�������

HHHH

HHH

FALSE∧ y ′ = 18 )

∨ ( 18 > 12∧ y ′ = 18− 12∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 125: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 12 > 18∧ x ′ = 12− 18�������

HHHH

HHH

FALSE∧ y ′ = 18 )

∨ ( 18 > 12∧ y ′ = 18− 12∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 126: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 12 > 18∧ x ′ = 12− 18�������

HHHH

HHH

FALSE∧ y ′ = 18 )

∨ ( 18 > 12∧ y ′ = 18− 12∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 127: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 128: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 12 > 6∧ x ′ = 12− 6∧ y ′ = 6 )

∨ ( 6 > 12∧ y ′ = 6− 12∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 129: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( TRUE12 > 6∧ x ′ = 12− 6∧ y ′ = 6 )

∨ ( FALSE6 > 12∧ y ′ = 6− 12∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 130: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 12 > 6∧ x ′ = 12− 6∧ y ′ = 6 )

∨ ( 6 > 12∧ y ′ = 6− 12�����

��

HHHHH

HH

FALSE∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 131: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 12 > 6∧ x ′ = 12− 6∧ y ′ = 6 )

∨ ( 6 > 12∧ y ′ = 6− 12�����

��

HHHHH

HH

FALSE∧ x ′ = 12 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 132: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 133: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( FALSE6 > 6∧ x ′ = 6− 6∧ y ′ = 6 )

∨ ( FALSE6 > 6∧ y ′ = 6− 6∧ x ′ = 6 )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 134: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 6 > 6∧ x ′ = 6− 6�������

HHHH

HHH

FALSE∧ y ′ = 6 )

∨ ( 6 > 6∧ y ′ = 6− 6�������

HHHHH

HH

FALSE∧ x ′ = )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 135: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

For Euclid’s Algorithm

Init : (x = M ) ∧ (y = N )

Next : ( 6 > 6∧ x ′ = 6− 6�������

HHHH

HHH

FALSE∧ y ′ = 6 )

∨ ( 6 > 6∧ y ′ = 6− 6�������

HHHHH

HH

FALSE

NO NEXT STATE

∧ x ′ = )

Behavior:

[x = 12, y = 18] → [x = 12, y = 6] → [x = 6, y = 6]

16

Page 136: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What About Formal Specs?

Need them only to apply tools.

Require a formal language.

16

Page 137: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What About Formal Specs?

Need them only to apply tools.

Require a formal language.

16

Page 138: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What About Formal Specs?

Need them only to apply tools.

Require a formal language.

16

Page 139: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Language: TLA+

17

Page 140: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Language: TLA+

This

Init : (x = M ) ∧ (y = N )

Next : ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

17

Page 141: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Language: TLA+

becomes

MODULE EuclidEXTENDS Integers

Init∆= (x = M ) ∧ (y = N )

Next∆= ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

17

Page 142: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Language: TLA+

plus some boilerplate.

MODULE EuclidEXTENDS Integers

Init∆= (x = M ) ∧ (y = N )

Next∆= ( x > y

∧ x ′ = x − y

∧ y ′ = y )

∨ ( y > x

∧ y ′ = y − x

∧ x ′ = x )

17

Page 143: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Language: TLA+

You type

--------------- MODULE Euclid ----------------EXTENDS Integers

Init == (x = M) /\ (y = N)

Next == ( x > y/\ x’ = x - y/\ y’ = y )

\/ ( y > x/\ y’ = y - x/\ x’ = x )

===============================================

17

Page 144: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You can model check TLA+ specs.

You can write formal correctness proofsand check them mechanically.

But math works only for toy examples.

To model real systems, you need a real languagewith types, procedures, objects, etc.

Wrong

18

Page 145: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You can model check TLA+ specs.

You can write formal correctness proofsand check them mechanically.

But math works only for toy examples.

To model real systems, you need a real languagewith types, procedures, objects, etc.

Wrong

18

Page 146: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You can model check TLA+ specs.

You can write formal correctness proofsand check them mechanically.

But math works only for toy examples.

To model real systems, you need a real languagewith types, procedures, objects, etc.

Wrong

18

Page 147: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You can model check TLA+ specs.

You can write formal correctness proofsand check them mechanically.

But math works only for toy examples.

To model real systems, you need a real languagewith types, procedures, objects, etc.

Wrong

18

Page 148: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You can model check TLA+ specs.

You can write formal correctness proofsand check them mechanically.

But math works only for toy examples.

To model real systems, you need a real language

��������

����

����

������

PPPP

PPPP

PPPP

PPPP

PPPP

PP

with types, procedures, objects, etc.

Wrong

18

Page 149: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLA+ Use at Amazon (incomplete list)

19

Page 150: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Design Bugs

Not just simple coding bugs

Serious, often impossible to find by testing.

Ask Chris Newcombe about TLA+.

I’ll talk about informal specs, starting with an example.

19

Page 151: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Design Bugs

Not just simple coding bugs

Serious, often impossible to find by testing.

Ask Chris Newcombe about TLA+.

I’ll talk about informal specs, starting with an example.

19

Page 152: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Design Bugs

Not just simple coding bugs

Serious, often impossible to find by testing.

Ask Chris Newcombe about TLA+.

I’ll talk about informal specs, starting with an example.

19

Page 153: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Design Bugs

Not just simple coding bugs

Serious, often impossible to find by testing.

Ask Chris Newcombe about TLA+.

I’ll talk about informal specs, starting with an example.

19

Page 154: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Design Bugs

Not just simple coding bugs

Serious, often impossible to find by testing.

Ask Chris Newcombe about TLA+.

I’ll talk about informal specs, starting with an example.

19

Page 155: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

19

Page 156: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

The input:

Foo => /\ a = b

/\ ccc = d

20

Page 157: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

The input:

Foo => /\ a = b

/\ ccc = d

The naive output:

Foo ⇒ ∧ a = b

∧ ccc = d

20

Page 158: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

The input:

Foo => /\ a = b

/\ ccc = d

The naive output:

Foo ⇒ ∧ a = b

∧ ccc = d

The user probably wanted these aligned.

20

Page 159: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

The input:

Foo => /\ a = b

/\ ccc = d

The right output:

Foo ⇒ ∧ a = b

∧ ccc = d

The user probably wanted these aligned.

20

Page 160: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

The input:

/\ aaa + bb = c

/\ iii = jj * k

21

Page 161: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

The input:

/\ aaa + bb = c

/\ iii = jj * k

The naive output:

∧ aaa + bb = c

∧ iii = jj ∗ k

21

Page 162: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

The input:

/\ aaa + bb = c

/\ iii = jj * k

The naive output:

∧ aaa + bb = c

∧ iii = jj ∗ k

The user probably didn’t wanted these aligned.

21

Page 163: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

TLATEX — the TLA+ pretty-printer

The input:

/\ aaa + bb = c

/\ iii = jj * k

The right output:

∧ aaa + bb = c

∧ iii = jj ∗ k

The user probably didn’t wanted these aligned.

21

Page 164: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

There is no precise definition of correct alignment.

We can’t specify mathematically what the user wants.

If we can’t specify correctness, specification is useless.Wrong.

Not knowing what a program should do meanswe have to think even harder.

Which means that a spec is even more important.

What did I do?

Let’s look at my Java code.

21

Page 165: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

There is no precise definition of correct alignment.

We can’t specify mathematically what the user wants.

If we can’t specify correctness, specification is useless.Wrong.

Not knowing what a program should do meanswe have to think even harder.

Which means that a spec is even more important.

What did I do?

Let’s look at my Java code.

21

Page 166: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

There is no precise definition of correct alignment.

We can’t specify mathematically what the user wants.

If we can’t specify correctness, specification is useless.Wrong.

Not knowing what a program should do meanswe have to think even harder.

Which means that a spec is even more important.

What did I do?

Let’s look at my Java code.

21

Page 167: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

There is no precise definition of correct alignment.

We can’t specify mathematically what the user wants.

If we can’t specify correctness, specification is useless.Wrong.

Not knowing what a program should do meanswe have to think even harder.

Which means that a spec is even more important.

What did I do?

Let’s look at my Java code.

21

Page 168: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

There is no precise definition of correct alignment.

We can’t specify mathematically what the user wants.

If we can’t specify correctness, specification is useless.Wrong.

Not knowing what a program should do meanswe have to think even harder.

Which means that a spec is even more important.

What did I do?

Let’s look at my Java code.

21

Page 169: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

There is no precise definition of correct alignment.

We can’t specify mathematically what the user wants.

If we can’t specify correctness, specification is useless.Wrong.

Not knowing what a program should do meanswe have to think even harder.

Which means that a spec is even more important.

What did I do?

Let’s look at my Java code.

21

Page 170: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

There is no precise definition of correct alignment.

We can’t specify mathematically what the user wants.

If we can’t specify correctness, specification is useless.Wrong.

Not knowing what a program should do meanswe have to think even harder.

Which means that a spec is even more important.

What did I do?

Let’s look at my Java code.

21

Page 171: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

There is no precise definition of correct alignment.

We can’t specify mathematically what the user wants.

If we can’t specify correctness, specification is useless.Wrong.

Not knowing what a program should do meanswe have to think even harder.

Which means that a spec is even more important.

What did I do?

Let’s look at my Java code.

21

Page 172: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

/**************************************************************************** CLASS FindAlignments ** ** Contains the one public method ** ** public static void FindAlignments(Token[][] spec) ** ** that sets the aboveAlign, belowAlign, and isAlignmentPoint fields for ** all tokens in the tokenized spec spec--except for NULL and MULTI ** comment tokens, for which the aboveAlign field is set by ** CommentToken.processComments. (This method should be called after ** CommentToken.processComments.) ** ** There are six kinds of alignments, illustrated by the following ** example: ** ** Alignment Type Tokens so aligned ** -------------- ----------------- ** FirstNonLeftComment foo, x, + , /\ , y/comment r, u/comment k ** LeftComment comments j ** CommentInner comments p ** InfixInner ==, => , > ** AfterInfixInner m, c, ** InfixArg a, z/z, y/z ** ** foo == LET x == /\ a => m * n > c ** | | | | | | | | | | ** (* j *) | | | | /\ a => m / q > c (* p *) ** | | | | | | ** (* j *) | | x == z | ** | | | | ** | | + z \* p ** | | | | | ** 22

Page 173: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

/**************************************************************************** CLASS FindAlignments ** ** Contains the one public method ** ** public static void FindAlignments(Token[][] spec) ** ** that sets the aboveAlign, belowAlign, and isAlignmentPoint fields for ** all tokens in the tokenized spec spec--except for NULL and MULTI ** comment tokens, for which the aboveAlign field is set by ** CommentToken.processComments. (This method should be called after ** CommentToken.processComments.) ** ** There are six kinds of alignments, illustrated by the following ** example: ** ** Alignment Type Tokens so aligned ** -------------- ----------------- ** FirstNonLeftComment foo, x, + , /\ , y/comment r, u/comment k ** LeftComment comments j ** CommentInner comments p ** InfixInner ==, => , > ** AfterInfixInner m, c, ** InfixArg a, z/z, y/z ** ** foo == LET x == /\ a => m * n > c ** | | | | | | | | | | ** (* j *) | | | | /\ a => m / q > c (* p *) ** | | | | | | ** (* j *) | | x == z | ** | | | | ** | | + z \* p ** | | | | | ** 22

Page 174: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

/**************************************************************************** CLASS FindAlignments ** ** Contains the one public method ** ** public static void FindAlignments(Token[][] spec) ** ** that sets the aboveAlign, belowAlign, and isAlignmentPoint fields for ** all tokens in the tokenized spec spec--except for NULL and MULTI ** comment tokens, for which the aboveAlign field is set by ** CommentToken.processComments. (This method should be called after ** CommentToken.processComments.) ** ** There are six kinds of alignments, illustrated by the following ** example: ** ** Alignment Type Tokens so aligned ** -------------- ----------------- ** FirstNonLeftComment foo, x, + , /\ , y/comment r, u/comment k ** LeftComment comments j ** CommentInner comments p ** InfixInner ==, => , > ** AfterInfixInner m, c, ** InfixArg a, z/z, y/z ** ** foo == LET x == /\ a => m * n > c ** | | | | | | | | | | ** (* j *) | | | | /\ a => m / q > c (* p *) ** | | | | | | ** (* j *) | | x == z | ** | | | | ** | | + z \* p ** | | | | | **

* There are six kinds of alignments, ** illustrated by the following example: *

22

Page 175: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

/**************************************************************************** CLASS FindAlignments ** ** Contains the one public method ** ** public static void FindAlignments(Token[][] spec) ** ** that sets the aboveAlign, belowAlign, and isAlignmentPoint fields for ** all tokens in the tokenized spec spec--except for NULL and MULTI ** comment tokens, for which the aboveAlign field is set by ** CommentToken.processComments. (This method should be called after ** CommentToken.processComments.) ** ** There are six kinds of alignments, illustrated by the following ** example: ** ** Alignment Type Tokens so aligned ** -------------- ----------------- ** FirstNonLeftComment foo, x, + , /\ , y/comment r, u/comment k ** LeftComment comments j ** CommentInner comments p ** InfixInner ==, => , > ** AfterInfixInner m, c, ** InfixArg a, z/z, y/z ** ** foo == LET x == /\ a => m * n > c ** | | | | | | | | | | ** (* j *) | | | | /\ a => m / q > c (* p *) ** | | | | | | ** (* j *) | | x == z | ** | | | | ** | | + z \* p ** | | | | | ** 22

Page 176: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* | | | | (* p *) ** | | | | | ** | | + y (* p *) ** | | | ** | | \* r ** | | ** foo == ** | ** (* k *) ** | ** u + v ** ** For InfixInner and CommentInner alignment, a token’s belowAlign ** field points to the token directly below it with which it is ** aligned. ** ** For all alignment types, if a token t1 is aligned with a token ** t2 above it, then t1.aboveAlign points to: ** ** IF t2 has no aboveAlign pointer THEN t2 ** ELSE t2.abovealign. ** ** Thus, in the example above, the aboveAlign pointer for all the p ** comments point to the first p comment. ** ** To define the types of alignment, we use the following definitions: ** ** - A LEFT-COMMENT is a comment that is the first token on its line but ** not the last token on its line. (The comments (* j *) in the ** example are left-comments.) For all the types of alignment except ** CommentInner, left-comments are treated as if they didn’t exist. ** ** - A RIGHT-COMMENT is a comment that is the last token on its line. * 22

Page 177: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* Any comment other than a left-comment or a right-comment is treated ** like any other non-built-in token. We also define: ** ** - The COVERING TOKEN of a token t is the right-most token ct that ** covers t on the last line (one with biggest line number) containing ** a token that covers t, where a token t1 COVERS a token t2 if t1 ** lies on an earlier line than t2 and t1.column \leq t2.column. ** However, if there is a DASH between ct and t, or ** on the same line as ct, then t has no covering token. ** (This definition has two versions, depending on whether or not ** left-comments are ignored.) ** ** - The BLOCKING TOKEN of a token t is the left-most token s with ** s.column \geq t.column on the last line before t’s line containing ** such a non-left-comment token s. ** ** Here are the descriptions of the different types of alignment. ** ** CommentInner: ** Token t at position pos is CommentInner above-aligned with its ** blocking token bt at position bpos iff: ** /\ t is a right-comment ** /\ bt is a right-comment ** /\ t.column = bt.column ** /\ \/ bt is not above-aligned with anything ** \/ bt is CommentInner aligned with a token above it ** ** FirstNonLeftComment ** If t is the first token on a line that is not a left-comment, and is ** not a right-comment that is CommentInner aligned with a token above ** it, then t is FirstNonLeftComment aligned with its covering token. ** *

22

Page 178: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* Any comment other than a left-comment or a right-comment is treated ** like any other non-built-in token. We also define: ** ** - The COVERING TOKEN of a token t is the right-most token ct that ** covers t on the last line (one with biggest line number) containing ** a token that covers t, where a token t1 COVERS a token t2 if t1 ** lies on an earlier line than t2 and t1.column \leq t2.column. ** However, if there is a DASH between ct and t, or ** on the same line as ct, then t has no covering token. ** (This definition has two versions, depending on whether or not ** left-comments are ignored.) ** ** - The BLOCKING TOKEN of a token t is the left-most token s with ** s.column \geq t.column on the last line before t’s line containing ** such a non-left-comment token s. ** ** Here are the descriptions of the different types of alignment. ** ** CommentInner: ** Token t at position pos is CommentInner above-aligned with its ** blocking token bt at position bpos iff: ** /\ t is a right-comment ** /\ bt is a right-comment ** /\ t.column = bt.column ** /\ \/ bt is not above-aligned with anything ** \/ bt is CommentInner aligned with a token above it ** ** FirstNonLeftComment ** If t is the first token on a line that is not a left-comment, and is ** not a right-comment that is CommentInner aligned with a token above ** it, then t is FirstNonLeftComment aligned with its covering token. ** *

* Here are the descriptions of the different ** kinds of alignment. *

22

Page 179: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* Any comment other than a left-comment or a right-comment is treated ** like any other non-built-in token. We also define: ** ** - The COVERING TOKEN of a token t is the right-most token ct that ** covers t on the last line (one with biggest line number) containing ** a token that covers t, where a token t1 COVERS a token t2 if t1 ** lies on an earlier line than t2 and t1.column \leq t2.column. ** However, if there is a DASH between ct and t, or ** on the same line as ct, then t has no covering token. ** (This definition has two versions, depending on whether or not ** left-comments are ignored.) ** ** - The BLOCKING TOKEN of a token t is the left-most token s with ** s.column \geq t.column on the last line before t’s line containing ** such a non-left-comment token s. ** ** Here are the descriptions of the different types of alignment. ** ** CommentInner: ** Token t at position pos is CommentInner above-aligned with its ** blocking token bt at position bpos iff: ** /\ t is a right-comment ** /\ bt is a right-comment ** /\ t.column = bt.column ** /\ \/ bt is not above-aligned with anything ** \/ bt is CommentInner aligned with a token above it ** ** FirstNonLeftComment ** If t is the first token on a line that is not a left-comment, and is ** not a right-comment that is CommentInner aligned with a token above ** it, then t is FirstNonLeftComment aligned with its covering token. ** *

22

Page 180: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* ** LeftComment: ** If t is a left-comment token, then it is LeftComment aligned ** with its covering token (where left-comments are visible). ** ** InfixInner: ** Token t at position pos is InfixInner aligned with its covering ** token ct at position cpos iff t is not FirstNonLeftComment aligned, ** and both t and ct are built-in symbols with the same nonzero alignment ** class. (The name InfixInner is misleading because non-infix symbols ** like ":" get aligned by this mechanism.) ** ** AfterInfixInner: ** Token t is After InfixInner aligned with the token above it iff ** ** LET lt == the token to the left of t ** alt == the token at position lt.aboveAlign ** at == the token to the right of alt ** IN /\ t is not the first token on the line ** /\ lt is InnerAligned with token alt above it ** /\ alt is not the last token on its line. ** /\ at is the covering token of t ** /\ t.column = at.column ** /\ at is not a comment ** (The name AfterInfixInner means "after InfixInner alignment". ** Remember that some non-infix symbols get InfixInner aligned.) ** ** InfixArg: ** Token t is InfixArg aligned with its covering token ct iff: ** LET lt == the token to the left of t ** alt == the token at position lt.aboveAlign ** IN /\ t is not the first token on the line ** /\ lt is the first non-left-comment token *

23

Page 181: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* ** LeftComment: ** If t is a left-comment token, then it is LeftComment aligned ** with its covering token (where left-comments are visible). ** ** InfixInner: ** Token t at position pos is InfixInner aligned with its covering ** token ct at position cpos iff t is not FirstNonLeftComment aligned, ** and both t and ct are built-in symbols with the same nonzero alignment ** class. (The name InfixInner is misleading because non-infix symbols ** like ":" get aligned by this mechanism.) ** ** AfterInfixInner: ** Token t is After InfixInner aligned with the token above it iff ** ** LET lt == the token to the left of t ** alt == the token at position lt.aboveAlign ** at == the token to the right of alt ** IN /\ t is not the first token on the line ** /\ lt is InnerAligned with token alt above it ** /\ alt is not the last token on its line. ** /\ at is the covering token of t ** /\ t.column = at.column ** /\ at is not a comment ** (The name AfterInfixInner means "after InfixInner alignment". ** Remember that some non-infix symbols get InfixInner aligned.) ** ** InfixArg: ** Token t is InfixArg aligned with its covering token ct iff: ** LET lt == the token to the left of t ** alt == the token at position lt.aboveAlign ** IN /\ t is not the first token on the line ** /\ lt is the first non-left-comment token *

* LeftComment:

* If t is a left-comment token, then it is ** LeftComment aligned with its covering ** token. *

23

Page 182: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* ** LeftComment: ** If t is a left-comment token, then it is LeftComment aligned ** with its covering token (where left-comments are visible). ** ** InfixInner: ** Token t at position pos is InfixInner aligned with its covering ** token ct at position cpos iff t is not FirstNonLeftComment aligned, ** and both t and ct are built-in symbols with the same nonzero alignment ** class. (The name InfixInner is misleading because non-infix symbols ** like ":" get aligned by this mechanism.) ** ** AfterInfixInner: ** Token t is After InfixInner aligned with the token above it iff ** ** LET lt == the token to the left of t ** alt == the token at position lt.aboveAlign ** at == the token to the right of alt ** IN /\ t is not the first token on the line ** /\ lt is InnerAligned with token alt above it ** /\ alt is not the last token on its line. ** /\ at is the covering token of t ** /\ t.column = at.column ** /\ at is not a comment ** (The name AfterInfixInner means "after InfixInner alignment". ** Remember that some non-infix symbols get InfixInner aligned.) ** ** InfixArg: ** Token t is InfixArg aligned with its covering token ct iff: ** LET lt == the token to the left of t ** alt == the token at position lt.aboveAlign ** IN /\ t is not the first token on the line ** /\ lt is the first non-left-comment token *

* LeftComment:

* If t is a left-comment token, then it is ** LeftComment aligned with its covering ** token. *

23

Page 183: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* ** LeftComment: ** If t is a left-comment token, then it is LeftComment aligned ** with its covering token (where left-comments are visible). ** ** InfixInner: ** Token t at position pos is InfixInner aligned with its covering ** token ct at position cpos iff t is not FirstNonLeftComment aligned, ** and both t and ct are built-in symbols with the same nonzero alignment ** class. (The name InfixInner is misleading because non-infix symbols ** like ":" get aligned by this mechanism.) ** ** AfterInfixInner: ** Token t is After InfixInner aligned with the token above it iff ** ** LET lt == the token to the left of t ** alt == the token at position lt.aboveAlign ** at == the token to the right of alt ** IN /\ t is not the first token on the line ** /\ lt is InnerAligned with token alt above it ** /\ alt is not the last token on its line. ** /\ at is the covering token of t ** /\ t.column = at.column ** /\ at is not a comment ** (The name AfterInfixInner means "after InfixInner alignment". ** Remember that some non-infix symbols get InfixInner aligned.) ** ** InfixArg: ** Token t is InfixArg aligned with its covering token ct iff: ** LET lt == the token to the left of t ** alt == the token at position lt.aboveAlign ** IN /\ t is not the first token on the line ** /\ lt is the first non-left-comment token *

23

Page 184: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* Any comment other than a left-comment or a right-comment is treated ** like any other non-built-in token. We also define: ** ** - The COVERING TOKEN of a token t is the right-most token ct that ** covers t on the last line (one with biggest line number) containing ** a token that covers t, where a token t1 COVERS a token t2 if t1 ** lies on an earlier line than t2 and t1.column \leq t2.column. ** However, if there is a DASH between ct and t, or ** on the same line as ct, then t has no covering token. ** (This definition has two versions, depending on whether or not ** left-comments are ignored.) ** ** - The BLOCKING TOKEN of a token t is the left-most token s with ** s.column \geq t.column on the last line before t’s line containing ** such a non-left-comment token s. ** ** Here are the descriptions of the different types of alignment. ** ** CommentInner: ** Token t at position pos is CommentInner above-aligned with its ** blocking token bt at position bpos iff: ** /\ t is a right-comment ** /\ bt is a right-comment ** /\ t.column = bt.column ** /\ \/ bt is not above-aligned with anything ** \/ bt is CommentInner aligned with a token above it ** ** FirstNonLeftComment ** If t is the first token on a line that is not a left-comment, and is ** not a right-comment that is CommentInner aligned with a token above ** it, then t is FirstNonLeftComment aligned with its covering token. ** *

23

Page 185: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* | | | | (* p *) ** | | | | | ** | | + y (* p *) ** | | | ** | | \* r ** | | ** foo == ** | ** (* k *) ** | ** u + v ** ** For InfixInner and CommentInner alignment, a token’s belowAlign ** field points to the token directly below it with which it is ** aligned. ** ** For all alignment types, if a token t1 is aligned with a token ** t2 above it, then t1.aboveAlign points to: ** ** IF t2 has no aboveAlign pointer THEN t2 ** ELSE t2.abovealign. ** ** Thus, in the example above, the aboveAlign pointer for all the p ** comments point to the first p comment. ** ** To define the types of alignment, we use the following definitions: ** ** - A LEFT-COMMENT is a comment that is the first token on its line but ** not the last token on its line. (The comments (* j *) in the ** example are left-comments.) For all the types of alignment except ** CommentInner, left-comments are treated as if they didn’t exist. ** *

23

Page 186: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* | | | | (* p *) ** | | | | | ** | | + y (* p *) ** | | | ** | | \* r ** | | ** foo == ** | ** (* k *) ** | ** u + v ** ** For InfixInner and CommentInner alignment, a token’s belowAlign ** field points to the token directly below it with which it is ** aligned. ** ** For all alignment types, if a token t1 is aligned with a token ** t2 above it, then t1.aboveAlign points to: ** ** IF t2 has no aboveAlign pointer THEN t2 ** ELSE t2.abovealign. ** ** Thus, in the example above, the aboveAlign pointer for all the p ** comments point to the first p comment. ** ** To define the types of alignment, we use the following definitions: ** ** - A LEFT-COMMENT is a comment that is the first token on its line but ** not the last token on its line. (The comments (* j *) in the ** example are left-comments.) For all the types of alignment except ** CommentInner, left-comments are treated as if they didn’t exist. ** *

* - A LEFT-COMMENT is a comment that is the ** first token on its line but not the last ** token on its line. *

23

Page 187: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

* | | | | (* p *) ** | | | | | ** | | + y (* p *) ** | | | ** | | \* r ** | | ** foo == ** | ** (* k *) ** | ** u + v ** ** For InfixInner and CommentInner alignment, a token’s belowAlign ** field points to the token directly below it with which it is ** aligned. ** ** For all alignment types, if a token t1 is aligned with a token ** t2 above it, then t1.aboveAlign points to: ** ** IF t2 has no aboveAlign pointer THEN t2 ** ELSE t2.abovealign. ** ** Thus, in the example above, the aboveAlign pointer for all the p ** comments point to the first p comment. ** ** To define the types of alignment, we use the following definitions: ** ** - A LEFT-COMMENT is a comment that is the first token on its line but ** not the last token on its line. (The comments (* j *) in the ** example are left-comments.) For all the types of alignment except ** CommentInner, left-comments are treated as if they didn’t exist. ** *

23

Page 188: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

And so on for 215 lines.

This is a mathematical-prose spec of alignment.

It consists of 6 rules plus explanation (comments).

24

Page 189: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

And so on for 215 lines.

This is a mathematical-prose spec of alignment.

It consists of 6 rules plus explanation (comments).

24

Page 190: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

And so on for 215 lines.

This is a mathematical-prose spec of alignment.

It consists of 6 rules plus explanation (comments).

24

Page 191: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write this spec?

It was a lot easier to understand and debug 6 rulesthan 850 lines of code.

I did a lot of debugging of the rules, aided by debugging codeto report what rules were being used.

The few bugs in implementing the rules were easy to catch.

Had I just written code, it would have taken me much longerand not produced formatting as good.

24

Page 192: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write this spec?

It was a lot easier to understand and debug 6 rulesthan 850 lines of code.

I did a lot of debugging of the rules, aided by debugging codeto report what rules were being used.

The few bugs in implementing the rules were easy to catch.

Had I just written code, it would have taken me much longerand not produced formatting as good.

24

Page 193: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write this spec?

It was a lot easier to understand and debug 6 rulesthan 850 lines of code.

I did a lot of debugging of the rules, aided by debugging codeto report what rules were being used.

The few bugs in implementing the rules were easy to catch.

Had I just written code, it would have taken me much longerand not produced formatting as good.

24

Page 194: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write this spec?

It was a lot easier to understand and debug 6 rulesthan 850 lines of code.

I did a lot of debugging of the rules, aided by debugging codeto report what rules were being used.

The few bugs in implementing the rules were easy to catch.

Had I just written code, it would have taken me much longerand not produced formatting as good.

24

Page 195: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write this spec?

It was a lot easier to understand and debug 6 rulesthan 850 lines of code.

I did a lot of debugging of the rules, aided by debugging codeto report what rules were being used.

The few bugs in implementing the rules were easy to catch.

Had I just written code, it would have taken me much longerand not produced formatting as good.

24

Page 196: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write this spec?

It was a lot easier to understand and debug 6 rulesthan 850 lines of code.

I did a lot of debugging of the rules, aided by debugging codeto report what rules were being used.

The few bugs in implementing the rules were easy to catch.

Had I just written code, it would have taken me much longerand not produced formatting as good.

24

Page 197: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

About 10 years later I enhanced the program to pretty-printPlusCal code in comments.

A lot easier to modify a 6-rule spec than 850 lines of code.

Change to spec: Added one rule.

Changes to code:Added 2 methods.Changed one old method:

8 calls to an old method→ calls to a new methodAdded 1 call to a new method.

Without spec, probably would have completely rewritten code.

25

Page 198: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

About 10 years later I enhanced the program to pretty-printPlusCal code in comments.

A lot easier to modify a 6-rule spec than 850 lines of code.

Change to spec: Added one rule.

Changes to code:Added 2 methods.Changed one old method:

8 calls to an old method→ calls to a new methodAdded 1 call to a new method.

Without spec, probably would have completely rewritten code.

25

Page 199: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

About 10 years later I enhanced the program to pretty-printPlusCal code in comments.

A lot easier to modify a 6-rule spec than 850 lines of code.

Change to spec: Added one rule.

Changes to code:Added 2 methods.Changed one old method:

8 calls to an old method→ calls to a new methodAdded 1 call to a new method.

Without spec, probably would have completely rewritten code.

25

Page 200: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

About 10 years later I enhanced the program to pretty-printPlusCal code in comments.

A lot easier to modify a 6-rule spec than 850 lines of code.

Change to spec: Added one rule.

Changes to code:Added 2 methods.Changed one old method:

8 calls to an old method→ calls to a new methodAdded 1 call to a new method.

Without spec, probably would have completely rewritten code.

25

Page 201: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

About 10 years later I enhanced the program to pretty-printPlusCal code in comments.

A lot easier to modify a 6-rule spec than 850 lines of code.

Change to spec: Added one rule.

Changes to code:Added 2 methods.Changed one old method:

8 calls to an old method→ calls to a new methodAdded 1 call to a new method.

Without spec, probably would have completely rewritten code.

25

Page 202: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why not a formal spec?

Getting it right not that important.

It didn’t have to work on all corner cases.

There were no tools that could help me.

26

Page 203: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why not a formal spec?

Getting it right not that important.

It didn’t have to work on all corner cases.

There were no tools that could help me.

26

Page 204: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why not a formal spec?

Getting it right not that important.

It didn’t have to work on all corner cases.

There were no tools that could help me.

26

Page 205: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why not a formal spec?

Getting it right not that important.

It didn’t have to work on all corner cases.

There were no tools that could help me.

26

Page 206: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Typical About This Spec

The spec is at a higher-level than the code.

It could have been implemented in any language.

No method or tool for writing better code would havehelped to write the spec

No method or tool for writing better code would havemade the spec unnecessary.

It says nothing about how to write code.

You write a spec to help you think about the problembefore you think about the code. 27

Page 207: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Typical About This Spec

The spec is at a higher-level than the code.

It could have been implemented in any language.

No method or tool for writing better code would havehelped to write the spec

No method or tool for writing better code would havemade the spec unnecessary.

It says nothing about how to write code.

You write a spec to help you think about the problembefore you think about the code. 27

Page 208: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Typical About This Spec

The spec is at a higher-level than the code.

It could have been implemented in any language.

No method or tool for writing better code would havehelped to write the spec

No method or tool for writing better code would havemade the spec unnecessary.

It says nothing about how to write code.

You write a spec to help you think about the problembefore you think about the code. 27

Page 209: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Typical About This Spec

The spec is at a higher-level than the code.

It could have been implemented in any language.

No method or tool for writing better code would havehelped to write the spec

No method or tool for writing better code would havemade the spec unnecessary.

It says nothing about how to write code.

You write a spec to help you think about the problembefore you think about the code. 27

Page 210: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Typical About This Spec

The spec is at a higher-level than the code.

It could have been implemented in any language.

No method or tool for writing better code would havehelped to write the spec

No method or tool for writing better code would havemade the spec unnecessary.

It says nothing about how to write code.

You write a spec to help you think about the problembefore you think about the code. 27

Page 211: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Typical About This Spec

The spec is at a higher-level than the code.

It could have been implemented in any language.

No method or tool for writing better code would havehelped to write the spec

No method or tool for writing better code would havemade the spec unnecessary.

It says nothing about how to write code.

You write a spec to help you think about the problembefore you think about the code. 27

Page 212: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Typical About This Spec

The spec is at a higher-level than the code.

It could have been implemented in any language.

No method or tool for writing better code would havehelped to write the spec

No method or tool for writing better code would havemade the spec unnecessary.

It says nothing about how to write code.

You write a spec to help you think about the problembefore you think about the code. 27

Page 213: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Not Typical About This Spec

It’s quite subtle.

95% of the code most people write requires less thought;specs that are shorter and simpler suffice.

It’s a set of rules.

A set of rules/requirements/axioms is usually a bad spec.

It’s hard to understand the consequences of a set of rules.

The best way to write a functional spec is to write a function.

You can then check that the function implies any desiredrequirements.

28

Page 214: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Not Typical About This Spec

It’s quite subtle.

95% of the code most people write requires less thought;specs that are shorter and simpler suffice.

It’s a set of rules.

A set of rules/requirements/axioms is usually a bad spec.

It’s hard to understand the consequences of a set of rules.

The best way to write a functional spec is to write a function.

You can then check that the function implies any desiredrequirements.

28

Page 215: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Not Typical About This Spec

It’s quite subtle.

95% of the code most people write requires less thought;specs that are shorter and simpler suffice.

It’s a set of rules.

A set of rules/requirements/axioms is usually a bad spec.

It’s hard to understand the consequences of a set of rules.

The best way to write a functional spec is to write a function.

You can then check that the function implies any desiredrequirements.

28

Page 216: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Not Typical About This Spec

It’s quite subtle.

95% of the code most people write requires less thought;specs that are shorter and simpler suffice.

It’s a set of rules.

A set of rules/requirements/axioms is usually a bad spec.

It’s hard to understand the consequences of a set of rules.

The best way to write a functional spec is to write a function.

You can then check that the function implies any desiredrequirements.

28

Page 217: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Not Typical About This Spec

It’s quite subtle.

95% of the code most people write requires less thought;specs that are shorter and simpler suffice.

It’s a set of rules.

A set of rules/requirements/axioms is usually a bad spec.

It’s hard to understand the consequences of a set of rules.

The best way to write a functional spec is to write a function.

You can then check that the function implies any desiredrequirements.

28

Page 218: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Not Typical About This Spec

It’s quite subtle.

95% of the code most people write requires less thought;specs that are shorter and simpler suffice.

It’s a set of rules.

A set of rules/requirements/axioms is usually a bad spec.

It’s hard to understand the consequences of a set of rules.

The best way to write a functional spec is to write a function.

You can then check that the function implies any desiredrequirements.

28

Page 219: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Not Typical About This Spec

It’s quite subtle.

95% of the code most people write requires less thought;specs that are shorter and simpler suffice.

It’s a set of rules.

A set of rules/requirements/axioms is usually a bad spec.

It’s hard to understand the consequences of a set of rules.

The best way to write a functional spec is to write a function.

You can then check that the function implies any desiredrequirements.

28

Page 220: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What is Not Typical About This Spec

It’s quite subtle.

95% of the code most people write requires less thought;specs that are shorter and simpler suffice.

It’s a set of rules.

A set of rules/requirements/axioms is usually a bad spec.

It’s hard to understand the consequences of a set of rules.

The best way to write a functional spec is to write a function.

You can then check that the function implies any desiredrequirements.

28

Page 221: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifying How to Compute a Function

Specifying what the pretty-printer should do was hard.

Figuring out how to implement it was easy.

Specifying what a sorting program should do is easy.

Figuring out how to implement it efficiently is hard(if no one has shown you).

It requires thinking, which means writing a specification.

28

Page 222: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifying How to Compute a Function

Specifying what the pretty-printer should do was hard.

Figuring out how to implement it was easy.

Specifying what a sorting program should do is easy.

Figuring out how to implement it efficiently is hard(if no one has shown you).

It requires thinking, which means writing a specification.

28

Page 223: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifying How to Compute a Function

Specifying what the pretty-printer should do was hard.

Figuring out how to implement it was easy.

Specifying what a sorting program should do is easy.

Figuring out how to implement it efficiently is hard(if no one has shown you).

It requires thinking, which means writing a specification.

28

Page 224: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifying How to Compute a Function

Specifying what the pretty-printer should do was hard.

Figuring out how to implement it was easy.

Specifying what a sorting program should do is easy.

Figuring out how to implement it efficiently is hard(if no one has shown you).

It requires thinking, which means writing a specification.

28

Page 225: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifying How to Compute a Function

Specifying what the pretty-printer should do was hard.

Figuring out how to implement it was easy.

Specifying what a sorting program should do is easy.

Figuring out how to implement it efficiently is hard(if no one has shown you).

It requires thinking, which means writing a specification.

28

Page 226: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Specifying How to Compute a Function

Specifying what the pretty-printer should do was hard.

Figuring out how to implement it was easy.

Specifying what a sorting program should do is easy.

Figuring out how to implement it efficiently is hard(if no one has shown you).

It requires thinking, which means writing a specification.

28

Page 227: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An example: Quicksort

A divide-and-conquer algorithm for sorting an arrayA[0], . . . , A[N − 1].

For simplicity, assume the A[i ] are numbers.

29

Page 228: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An example: Quicksort

A divide-and-conquer algorithm for sorting an arrayA[0], . . . , A[N − 1].

For simplicity, assume the A[i ] are numbers.

29

Page 229: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

An example: Quicksort

A divide-and-conquer algorithm for sorting an arrayA[0], . . . , A[N − 1].

For simplicity, assume the A[i ] are numbers.

29

Page 230: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

It uses a procedure Partition(lo, hi).

It chooses pivot in lo . . . (hi − 1), permutes A[lo], . . . ,A[hi ]

to make A[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ],and returns pivot .

For this example, we don’t care how this procedureis implemented.

30

Page 231: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

It uses a procedure Partition(lo, hi).

It chooses pivot in lo . . . (hi − 1), permutes A[lo], . . . ,A[hi ]

to make A[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ],and returns pivot .

For this example, we don’t care how this procedureis implemented.

30

Page 232: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

It uses a procedure Partition(lo, hi).

It chooses pivot in lo . . . (hi − 1), permutes A[lo], . . . ,A[hi ]

to make A[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ],and returns pivot .

For this example, we don’t care how this procedureis implemented.

30

Page 233: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Let’s specify Quicksort in pseudo-code.

procedure Partition(lo, hi) {

Pick pivot in lo . . . (hi − 1);

Permute A[lo], . . . ,A[hi ] to makeA[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ];

return pivot ; }

procedure QS (lo, hi) { if (lo < hi ) { p := Partition(lo, hi);QS (lo, p);QS (p + 1, hi); } }

main { QS (0,N − 1) ; }

But is it really Quicksort?

31

Page 234: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

procedure Partition(lo, hi) {

Pick pivot in lo . . . (hi − 1);

Permute A[lo], . . . ,A[hi ] to makeA[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ];

return pivot ; }

procedure QS (lo, hi) { if (lo < hi ) { p := Partition(lo, hi);QS (lo, p);QS (p + 1, hi); } }

main { QS (0,N − 1) ; }

But is it really Quicksort?

31

Page 235: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

procedure Partition(lo, hi) {

Pick pivot in lo . . . (hi − 1);

Permute A[lo], . . . ,A[hi ] to makeA[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ];

return pivot ; }

procedure QS (lo, hi) { if (lo < hi ) { p := Partition(lo, hi);QS (lo, p);QS (p + 1, hi); } }

main { QS (0,N − 1) ; }

But is it really Quicksort?

31

Page 236: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

procedure Partition(lo, hi) {

Pick pivot in lo . . . (hi − 1);

Permute A[lo], . . . ,A[hi ] to makeA[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ];

return pivot ; }

procedure QS (lo, hi) { if (lo < hi ) { p := Partition(lo, hi);QS (lo, p);QS (p + 1, hi); } }

main { QS (0,N − 1) ; }

But is it really Quicksort?

31

Page 237: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

procedure Partition(lo, hi) {

Pick pivot in lo . . . (hi − 1);

Permute A[lo], . . . ,A[hi ] to makeA[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ];

return pivot ; }

procedure QS (lo, hi) { if (lo < hi ) { p := Partition(lo, hi);QS (lo, p);QS (p + 1, hi); } }

main { QS (0,N − 1) ; }

But is it really Quicksort?

31

Page 238: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

procedure Partition(lo, hi) {

Pick pivot in lo . . . (hi − 1);

Permute A[lo], . . . ,A[hi ] to makeA[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ];

return pivot ; }

procedure QS (lo, hi) { if (lo < hi ) { p := Partition(lo, hi);QS (lo, p);QS (p + 1, hi); } }

main { QS (0,N − 1) ; }

Informal: no formal syntax, no declarations, . . .

Easy to understand.

But is it really Quicksort?31

Page 239: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

procedure Partition(lo, hi) {

Pick pivot in lo . . . (hi − 1);

Permute A[lo], . . . ,A[hi ] to makeA[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ];

return pivot ; }

procedure QS (lo, hi) { if (lo < hi ) { p := Partition(lo, hi);QS (lo, p);QS (p + 1, hi); } }

main { QS (0,N − 1) ; }

Informal: no formal syntax, no declarations, . . .

Easy to understand.

But is it really Quicksort?31

Page 240: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

procedure Partition(lo, hi) {

Pick pivot in lo . . . (hi − 1);

Permute A[lo], . . . ,A[hi ] to makeA[lo], . . . ,A[pivot ] ≤ A[pivot + 1], . . . ,A[hi ];

return pivot ; }

procedure QS (lo, hi) { if (lo < hi ) { p := Partition(lo, hi);QS (lo, p);QS (p + 1, hi); } }

main { QS (0,N − 1) ; }

But is it really Quicksort?

31

Page 241: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

It’s the way Quicksort is almost always described.

But recursion is not a fundamental part of Quicksort.

It’s just one way of implementing divide-and-conquer.

It’s probably not the best way for parallel execution.

31

Page 242: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

It’s the way Quicksort is almost always described.

But recursion is not a fundamental part of Quicksort.

It’s just one way of implementing divide-and-conquer.

It’s probably not the best way for parallel execution.

31

Page 243: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

It’s the way Quicksort is almost always described.

But recursion is not a fundamental part of Quicksort.

It’s just one way of implementing divide-and-conquer.

It’s probably not the best way for parallel execution.

31

Page 244: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

It’s the way Quicksort is almost always described.

But recursion is not a fundamental part of Quicksort.

It’s just one way of implementing divide-and-conquer.

It’s probably not the best way for parallel execution.

31

Page 245: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Problem: Write a non-recursive version of Quicksort.

Almost no one can do it in 10 minutes.

They try to “compile” the recursive version.

32

Page 246: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Problem: Write a non-recursive version of Quicksort.

Almost no one can do it in 10 minutes.

They try to “compile” the recursive version.

32

Page 247: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Problem: Write a non-recursive version of Quicksort.

Almost no one can do it in 10 minutes.

They try to “compile” the recursive version.

32

Page 248: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Solution:

Maintain a set U of index ranges on whichPartition needs to be called.

Initially, U equals {〈0,N − 1〉}

We could write it in pseudo-code,but it’s better to simply write Init and Next directly.

32

Page 249: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Solution:

Maintain a set U of index ranges on whichPartition needs to be called.

Initially, U equals {〈0,N − 1〉}

We could write it in pseudo-code,but it’s better to simply write Init and Next directly.

32

Page 250: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Solution:

Maintain a set U of index ranges on whichPartition needs to be called.

Initially, U equals {〈0,N − 1〉}

We could write it in pseudo-code,but it’s better to simply write Init and Next directly.

32

Page 251: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Solution:

Maintain a set U of index ranges on whichPartition needs to be called.

Initially, U equals {〈0,N − 1〉}

We could write it in pseudo-code,but it’s better to simply write Init and Next directly.

32

Page 252: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Init : A = any array of numbers of length N

∧ U = {〈0,N − 1〉}

Before writing Next , let’s make a definition:

Partitions(B , pivot , lo, hi)∆=

the set of arrays obtained from B by permutingB [lo], . . . , B [hi ] such that . . .

Next :

A relation between old values of A, Uand new values A′, U ′.

33

Page 253: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Init : A = any array of numbers of length N

∧ U = {〈0,N − 1〉}

Before writing Next , let’s make a definition:

Partitions(B , pivot , lo, hi)∆=

the set of arrays obtained from B by permutingB [lo], . . . , B [hi ] such that . . .

Next :

A relation between old values of A, Uand new values A′, U ′.

33

Page 254: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Init : A = any array of numbers of length N

∧ U = {〈0,N − 1〉}

Before writing Next , let’s make a definition:

Partitions(B , pivot , lo, hi)∆=

the set of arrays obtained from B by permutingB [lo], . . . , B [hi ] such that . . .

Next :

A relation between old values of A, Uand new values A′, U ′.

33

Page 255: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Init : A = any array of numbers of length N

∧ U = {〈0,N − 1〉}

Before writing Next , let’s make a definition:

Partitions(B , pivot , lo, hi)∆=

the set of arrays obtained from B by permutingB [lo], . . . , B [hi ] such that . . .

Next :

A relation between old values of A, Uand new values A′, U ′.

33

Page 256: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 257: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {} Stop if U = {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 258: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 259: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 260: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 261: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 262: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 263: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 264: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

35

Page 265: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

38

Page 266: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why can (almost) no one find this version of Quicksort?

Their minds are stuck in code.

They can’t think at a higher level.

38

Page 267: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why can (almost) no one find this version of Quicksort?

Their minds are stuck in code.

They can’t think at a higher level.

38

Page 268: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why can (almost) no one find this version of Quicksort?

Their minds are stuck in code.

They can’t think at a higher level.

38

Page 269: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

Easy to write this as a formula.

39

Page 270: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ Pick any 〈b, t〉 in U :IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

Pick an arbitrary value

39

Page 271: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ ∃ 〈b, t〉 ∈ U :

IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

Pick an arbitrary value is existential quantification.

39

Page 272: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ ∃ 〈b, t〉 ∈ U :

IF b 6= t

THEN Pick any p in b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

Pick an arbitrary value is existential quantification.

39

Page 273: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ ∃ 〈b, t〉 ∈ U :

IF b 6= t

THEN ∃ p ∈ b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

Pick an arbitrary value is existential quantification.

39

Page 274: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ ∃ 〈b, t〉 ∈ U :

IF b 6= t

THEN ∃ p ∈ b . . (t−1) :A′ = Any element of Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

Or sometimes

39

Page 275: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ ∃ 〈b, t〉 ∈ U :

IF b 6= t

THEN ∃ p ∈ b . . (t−1) :A′ ∈ Partitions(A, p, b, t)

∧ U ′ = U with 〈b, t〉 removed and〈b, p〉 and 〈p+1, t〉 added

ELSE A′ = A

∧ U ′ = U with 〈b, t〉 removed

Or sometimes even simpler.

39

Page 276: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ ∃ 〈b, t〉 ∈ U :

IF b 6= t

THEN ∃ p ∈ b . . (t−1) :A′ ∈ Partitions(A, p, b, t)

∧ U ′ = (U \ {〈b, t〉}) ∪ {〈b, p〉, 〈p+1, t〉}

ELSE A′ = A

∧ U ′ = U \ {〈b, t〉}

And so on.

39

Page 277: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ ∃ 〈b, t〉 ∈ U :

IF b 6= t

THEN ∃ p ∈ b . . (t−1) :A′ ∈ Partitions(A, p, b, t)

∧ U ′ = (U \ {〈b, t〉}) ∪ {〈b, p〉, 〈p+1, t〉}

ELSE A′ = A

∧ U ′ = U \ {〈b, t〉}

A TLA+ formula.

39

Page 278: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Next :U 6= {}

∧ ∃ 〈b, t〉 ∈ U :

IF b 6= t

THEN ∃ p ∈ b . . (t−1) :A′ ∈ Partitions(A, p, b, t)

∧ U ′ = (U \ {〈b, t〉}) ∪ {〈b, p〉, 〈p+1, t〉}

ELSE A′ = A

∧ U ′ = U \ {〈b, t〉}

If you prefer pseudo-code. . .

39

Page 279: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

PlusCal

Like a toy programming language.

Algorithm appears in a comment in a TLA+ module.

An expression can be any TLA+ expression.

Constructs for nondeterminism.

Compiled to an easy to understand TLA+ spec.

Can apply TLA+ tools.

40

Page 280: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

PlusCal

Like a toy programming language.

Algorithm appears in a comment in a TLA+ module.

An expression can be any TLA+ expression.

Constructs for nondeterminism.

Compiled to an easy to understand TLA+ spec.

Can apply TLA+ tools.

40

Page 281: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

PlusCal

Like a toy programming language.

Algorithm appears in a comment in a TLA+ module.

An expression can be any TLA+ expression.

Constructs for nondeterminism.

Compiled to an easy to understand TLA+ spec.

Can apply TLA+ tools.

40

Page 282: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

PlusCal

Like a toy programming language.

Algorithm appears in a comment in a TLA+ module.

An expression can be any TLA+ expression.

Constructs for nondeterminism.

Compiled to an easy to understand TLA+ spec.

Can apply TLA+ tools.

40

Page 283: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

PlusCal

Like a toy programming language.

Algorithm appears in a comment in a TLA+ module.

An expression can be any TLA+ expression.

Constructs for nondeterminism.

Compiled to an easy to understand TLA+ spec.

Can apply TLA+ tools.

40

Page 284: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

PlusCal

Like a toy programming language.

Algorithm appears in a comment in a TLA+ module.

An expression can be any TLA+ expression.

Constructs for nondeterminism.

Compiled to an easy to understand TLA+ spec.

Can apply TLA+ tools.

40

Page 285: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

PlusCal

Like a toy programming language.

Algorithm appears in a comment in a TLA+ module.

An expression can be any TLA+ expression.

Constructs for nondeterminism.

Compiled to an easy to understand TLA+ spec.

Can apply TLA+ tools.

40

Page 286: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 287: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 288: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 289: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 290: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 291: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 292: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 293: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 294: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Programs that Run Forever

I’ve been talking about programs that compute a function.

Programs that run forever usually involve concurrency:

– Operating systems.

– Distributed systems.

Few people can get them right by just thinking (and writing).

I’m not one of them.

We need tools to check what we do.

Use TLA+/ PlusCal.

41

Page 295: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Other 95%

Prose

Most code is really simple.

41

Page 296: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

The Other 95%

/************************************************************ CLASS ResourceFileReader ** ** A ResourceFileReader returns an object for reading a ** resource file, which is a file kept in the same ** directory as the tlatex.Token class. The constructor ** takes a file name as argument. The object’s two public ** methods are ** ** getLine() : Returns the next line of the file as a ** string. Returns null after the last line. ** ** close() : Closes the file. ************************************************************/

42

Page 297: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write that spec?

To be sure I knew what the code should do before writing it.

Without writing a spec, I only thought I knew what it should do.

Later, I didn’t have to read the code to know what it did.

General rule:A spec of what the code does should say everythingthat anyone needs to know to use the code.

43

Page 298: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write that spec?

To be sure I knew what the code should do before writing it.

Without writing a spec, I only thought I knew what it should do.

Later, I didn’t have to read the code to know what it did.

General rule:A spec of what the code does should say everythingthat anyone needs to know to use the code.

43

Page 299: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write that spec?

To be sure I knew what the code should do before writing it.

Without writing a spec, I only thought I knew what it should do.

Later, I didn’t have to read the code to know what it did.

General rule:A spec of what the code does should say everythingthat anyone needs to know to use the code.

43

Page 300: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write that spec?

To be sure I knew what the code should do before writing it.

Without writing a spec, I only thought I knew what it should do.

Later, I didn’t have to read the code to know what it did.

General rule:A spec of what the code does should say everythingthat anyone needs to know to use the code.

43

Page 301: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write that spec?

To be sure I knew what the code should do before writing it.

Without writing a spec, I only thought I knew what it should do.

Later, I didn’t have to read the code to know what it did.

General rule:A spec of what the code does should say everythingthat anyone needs to know to use the code.

43

Page 302: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why did I write that spec?

To be sure I knew what the code should do before writing it.

Without writing a spec, I only thought I knew what it should do.

Later, I didn’t have to read the code to know what it did.

How the code worked was too simple to require a spec.

43

Page 303: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What programmers should know about thinking.

Everyone thinks they think.

If you don’t write down your thoughts,you’re fooling yourself.

43

Page 304: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What everyone should know about thinking.

Everyone thinks they think.

If you don’t write down your thoughts,you’re fooling yourself.

43

Page 305: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What everyone should know about thinking.

Everyone thinks they think.

If you don’t write down your thoughts,you’re fooling yourself.

43

Page 306: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What everyone should know about thinking.

Everyone thinks they think.

If you don’t write down your thoughts,you’re fooling yourself.

43

Page 307: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What programmers should know about thinking.

You should think before you code.

A spec is simply what you write before coding.

43

Page 308: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What programmers should know about thinking.

You should think before you code.

A spec is simply what you write before coding.

43

Page 309: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What programmers should know about thinking.

You should write before you code.

A spec is simply what you write before coding.

43

Page 310: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What programmers should know about thinking.

You should write before you code.

A spec is simply what you write before coding.

43

Page 311: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What code should you specify?

Any piece of code that someone else might want touse or modify.

It could be:

An entire program or system.

A class.

A method.

A tricky piece of code in a method.

44

Page 312: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What code should you specify?

Any piece of code that someone else might want touse or modify.

It could be:

An entire program or system.

A class.

A method.

A tricky piece of code in a method.

44

Page 313: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What code should you specify?

Any piece of code that someone elseyou

might want touse or modify in a month.

It could be:

An entire program or system.

A class.

A method.

A tricky piece of code in a method.

44

Page 314: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What code should you specify?

Any piece of code that someone elseyou

might want touse or modify in a month.

It could be:

An entire program or system.

A class.

A method.

A tricky piece of code in a method.

44

Page 315: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What code should you specify?

Any piece of code that someone elseyou

might want touse or modify in a month.

It could be:

An entire program or system.

A class.

A method.

A tricky piece of code in a method.

44

Page 316: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What code should you specify?

Any piece of code that someone elseyou

might want touse or modify in a month.

It could be:

An entire program or system.

A class.

A method.

A tricky piece of code in a method.

44

Page 317: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What code should you specify?

Any piece of code that someone elseyou

might want touse or modify in a month.

It could be:

An entire program or system.

A class.

A method.

A tricky piece of code in a method.

44

Page 318: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What code should you specify?

Any piece of code that someone elseyou

might want touse or modify in a month.

It could be:

An entire program or system.

A class.

A method.

A tricky piece of code in a method.

44

Page 319: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What should you specify about the code?

What it does.

Everything anyone needs to know to use it.

Maybe: how it does it.

The algorithm / high-level design.

44

Page 320: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What should you specify about the code?

What it does.

Everything anyone needs to know to use it.

Maybe: how it does it.

The algorithm / high-level design.

44

Page 321: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What should you specify about the code?

What it does.

Everything anyone needs to know to use it.

Maybe: how it does it.

The algorithm / high-level design.

44

Page 322: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What should you specify about the code?

What it does.

Everything anyone needs to know to use it.

Maybe: how it does it.

The algorithm / high-level design.

44

Page 323: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What should you specify about the code?

What it does.

Everything anyone needs to know to use it.

Maybe: how it does it.

The algorithm / high-level design.

44

Page 324: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How should you think about / specify the code?

Above the code level.

In terms of states and behaviors.

Mathematically, as rigorously / formally as necessary.

Perhaps with pseudo-code or PlusCal if specifying how.

45

Page 325: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How should you think about / specify the code?

Above the code level.

In terms of states and behaviors.

Mathematically, as rigorously / formally as necessary.

Perhaps with pseudo-code or PlusCal if specifying how.

45

Page 326: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How should you think about / specify the code?

Above the code level.

In terms of states and behaviors.

Mathematically, as rigorously / formally as necessary.

Perhaps with pseudo-code or PlusCal if specifying how.

45

Page 327: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How should you think about / specify the code?

Above the code level.

In terms of states and behaviors.

Mathematically, as rigorously / formally as necessary.

Perhaps with pseudo-code or PlusCal if specifying how.

45

Page 328: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How should you think about / specify the code?

Above the code level.

In terms of states and behaviors.

Mathematically, as rigorously / formally as necessary.

Perhaps with pseudo-code or PlusCal if specifying how.

45

Page 329: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How do you connect the spec to the code?

Comments connecting mathematical concepts and theirimplementation.

Example:

Mathematical concept: graph

Implementation: array of node objects & array of link objects

45

Page 330: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How do you connect the spec to the code?

Comments connecting mathematical concepts and theirimplementation.

Example:

Mathematical concept: graph

Implementation: array of node objects & array of link objects

45

Page 331: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How do you connect the spec to the code?

Comments connecting mathematical concepts and theirimplementation.

Example:

Mathematical concept: graph

Implementation: array of node objects & array of link objects

45

Page 332: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

How do you connect the spec to the code?

Comments connecting mathematical concepts and theirimplementation.

Example:

Mathematical concept: graph

Implementation: array of node objects & array of link objects

45

Page 333: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What about coding?

Nothing I have said implies anything abouthow you should code.

You still have to think while you code.

What you write while coding is code.

I have nothing to say about how you should code.

Use any programming language you want.

Use any coding methodology you want:test-driven development, agile programming, . . .

46

Page 334: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What about coding?

Nothing I have said implies anything abouthow you should code.

You still have to think while you code.

What you write while coding is code.

I have nothing to say about how you should code.

Use any programming language you want.

Use any coding methodology you want:test-driven development, agile programming, . . .

46

Page 335: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What about coding?

Nothing I have said implies anything abouthow you should code.

You still have to think while you code.

What you write while coding is code.

I have nothing to say about how you should code.

Use any programming language you want.

Use any coding methodology you want:test-driven development, agile programming, . . .

46

Page 336: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What about coding?

Nothing I have said implies anything abouthow you should code.

You still have to think while you code.

What you write while coding is code.

I have nothing to say about how you should code.

Use any programming language you want.

Use any coding methodology you want:test-driven development, agile programming, . . .

46

Page 337: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What about coding?

Nothing I have said implies anything abouthow you should code.

You still have to think while you code.

What you write while coding is code.

I have nothing to say about how you should code.

Use any programming language you want.

Use any coding methodology you want:test-driven development, agile programming, . . .

46

Page 338: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What about coding?

Nothing I have said implies anything abouthow you should code.

You still have to think while you code.

What you write while coding is code.

I have nothing to say about how you should code.

Use any programming language you want.

Use any coding methodology you want:test-driven development, agile programming, . . .

46

Page 339: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What about coding?

Nothing I have said implies anything abouthow you should code.

You still have to think while you code.

What you write while coding is code.

I have nothing to say about how you should code.

Use any programming language you want.

Use any coding methodology you want:test-driven development, agile programming, . . .

46

Page 340: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You’ll still have to test and debug.

Writing specs is an additional step.

It may save time by catching errors early,when they’re easier to correct.

It will improve your programming,so you write better programs.

46

Page 341: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You’ll still have to test and debug.

Writing specs is an additional step.

It may save time by catching errors early,when they’re easier to correct.

It will improve your programming,so you write better programs.

46

Page 342: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You’ll still have to test and debug.

Writing specs is an additional step.

It may save time by catching errors early,when they’re easier to correct.

It will improve your programming,so you write better programs.

46

Page 343: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

You’ll still have to test and debug.

Writing specs is an additional step.

It may save time by catching errors early,when they’re easier to correct.

It will improve your programming,so you write better programs.

46

Page 344: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why are programmers reluctant to write specs?

Writing is hard.

46

Page 345: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why are programmers reluctant to write specs?

Writing is hard.

46

Page 346: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 347: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 348: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 349: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 350: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 351: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 352: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 353: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 354: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Why is writing hard?

Writing requires thinking.

Thinking is hard.

It’s easier to think your thinking.

Writing is like running.

The less you do it, the slower you are.

You have to strengthen your writing muscles.

It takes practice.

It’s easier to find an excuse not to.

47

Page 355: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What if the spec is wrong?

Maybe you made a mistake.

Maybe the requirements change,or an enhancement is needed.

The code will have to be changed,maybe even before the program is finished.

This eventually happens to all useful programs.

47

Page 356: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What if the spec is wrong?

Maybe you made a mistake.

Maybe the requirements change,or an enhancement is needed.

The code will have to be changed,maybe even before the program is finished.

This eventually happens to all useful programs.

47

Page 357: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What if the spec is wrong?

Maybe you made a mistake.

Maybe the requirements change,or an enhancement is needed.

The code will have to be changed,maybe even before the program is finished.

This eventually happens to all useful programs.

47

Page 358: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What if the spec is wrong?

Maybe you made a mistake.

Maybe the requirements change,or an enhancement is needed.

The code will have to be changed,maybe even before the program is finished.

This eventually happens to all useful programs.

47

Page 359: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

What if the spec is wrong?

Maybe you made a mistake.

Maybe the requirements change,or an enhancement is needed.

The code will have to be changed,maybe even before the program is finished.

This eventually happens to all useful programs.

47

Page 360: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

In an ideal world, a new spec would be writtenand the code completely rewritten.

In the real world, the code is patchedand maybe the spec is updated.

If this is inevitable, why write specs?

48

Page 361: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

In an ideal world, a new spec would be writtenand the code completely rewritten.

In the real world, the code is patchedand maybe the spec is updated.

If this is inevitable, why write specs?

48

Page 362: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

In an ideal world, a new spec would be writtenand the code completely rewritten.

In the real world, the code is patchedand maybe the spec is updated.

If this is inevitable, why write specs?

48

Page 363: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 1

Whoever has to modify the code will be grateful forevery word or formula of spec you write.

And “whoever” may be you.

That’s why you should update the spec whenchanging the code.

48

Page 364: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 1

Whoever has to modify the code will be grateful forevery word or formula of spec you write.

And “whoever” may be you.

That’s why you should update the spec whenchanging the code.

48

Page 365: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 1

Whoever has to modify the code will be grateful forevery word or formula of spec you write.

And “whoever” may be you.

That’s why you should update the spec whenchanging the code.

48

Page 366: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 1

Whoever has to modify the code will be grateful forevery word or formula of spec you write.

And “whoever” may be you.

That’s why you should update the spec whenchanging the code.

48

Page 367: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 2

Every time code is patched, it becomes a little uglier,harder to understand, and harder to maintain.

If you don’t start with a spec, every piece of the codeyou write is a patch.

The program starts out ugly, hard to understand,and hard to maintain.

“No battle was ever won according to plan,but no battle was ever won without one.”

Dwight D. Eisenhower

49

Page 368: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 2

Every time code is patched, it becomes a little uglier,harder to understand, and harder to maintain.

If you don’t start with a spec, every piece of the codeyou write is a patch.

The program starts out ugly, hard to understand,and hard to maintain.

“No battle was ever won according to plan,but no battle was ever won without one.”

Dwight D. Eisenhower

49

Page 369: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 2

Every time code is patched, it becomes a little uglier,harder to understand, and harder to maintain.

If you don’t start with a spec, every piece of the codeyou write is a patch.

The program starts out ugly, hard to understand,and hard to maintain.

“No battle was ever won according to plan,but no battle was ever won without one.”

Dwight D. Eisenhower

49

Page 370: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 2

Every time code is patched, it becomes a little uglier,harder to understand, and harder to maintain.

If you don’t start with a spec, every piece of the codeyou write is a patch.

The program starts out ugly, hard to understand,and hard to maintain.

“No battle was ever won according to plan,but no battle was ever won without one.”

Dwight D. Eisenhower

49

Page 371: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 2

Every time code is patched, it becomes a little uglier,harder to understand, and harder to maintain.

If you don’t start with a spec, every piece of the codeyou write is a patch.

The program starts out ugly, hard to understand,and hard to maintain.

“No battle was ever won according to plan,but no battle was ever won without one.”

Dwight D. Eisenhower

49

Page 372: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Reason 2

Every time code is patched, it becomes a little uglier,harder to understand, and harder to maintain.

If you don’t start with a spec, every piece of the codeyou write is a patch.

The program starts out ugly, hard to understand,and hard to maintain.

“No battle was ever won according to plan,but no battle was ever won without one.”

Dwight D. Eisenhower

49

Page 373: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Thinking doesn’t guarantee that you won’t make mistakes.

Not thinking guarantees that you will.

49

Page 374: Microsoft Research's Leslie Lamport at Build2014 - Thinking for Programmers

Thinking doesn’t guarantee that you won’t make mistakes.

Not thinking guarantees that you will.

49