Top Banner
I believe that the best way to get better programs is to teach programmers how to think better -- Dr. Leslie Lamport
59

The SAM Pattern: State Machines and Computation

Jan 23, 2018

Download

Software

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: The SAM Pattern: State Machines and Computation

I believe that the best way to get better

programs is to teach programmers how to

think better

-- Dr. Leslie Lamport

Page 2: The SAM Pattern: State Machines and Computation

SAMThe State Action Model Pattern

Jean-Jacques Dubray

© https://creativecommons.org/licenses/by-nc-sa/4.0/

Page 3: The SAM Pattern: State Machines and Computation

'(primed variables)

Page 4: The SAM Pattern: State Machines and Computation

Action State

Thing Relationship

Dynamic

Static

Physical Conceptual

The STAR Diagram

Page 5: The SAM Pattern: State Machines and Computation

Light Energy

Mass Force

Dynamic

Static

Physical Conceptual

STAR is as fundamental to

Computing as the FLEM Diagram is

to Physics

Page 6: The SAM Pattern: State Machines and Computation

Thing

RelationshipState

Action

Page 7: The SAM Pattern: State Machines and Computation

State machines provide a

framework for much of computer

science

• “Don’t be obsessed by computer languages

[and frameworks]”

• Keep a clear delineation between

o Programming model

o Wiring

o Architecture

-- Dr Lamport

Page 8: The SAM Pattern: State Machines and Computation

Classical State

Machines

A set of states Q = {q0, q1, q2}

A start state q0

A set of transitions which are triplets (q,a,q') members of Q x Act x Q

Page 9: The SAM Pattern: State Machines and Computation

State Machines can

Compute…

“the die-hard problem”

5 liters3 liters

4 liters

Page 10: The SAM Pattern: State Machines and Computation

State Machines can

Compute

Page 11: The SAM Pattern: State Machines and Computation

There are several ways to

define computation

• A computation is a sequence of steps, called a

behavior

• There are three common choices for what a step is,

leading to three different kinds of behavior:

• Action Behavior a step is an action, an action

behavior is a sequence of actions

• State Behavior A step is a pair <Si , Si+1> of states, a

state behavior is a sequence

s1 → s2 → s3 → · · · of states

• State-Action Behavior A step is a triple < Si , α, Si+1 > where

S are states and α an action

-- Dr. Lamport

Page 12: The SAM Pattern: State Machines and Computation

We just created a language that

computes volumes of liquid

(not sure it will be popular though)

1. Fill Big

2. Fill Small from Big

3. Empty Small

4. Fill small from Big

5. Fill Big

6. Fill small from Big

State-Action Behavior Action Behavior

5 liters3 liters

Page 13: The SAM Pattern: State Machines and Computation

Programming Languages are Designed

to Make it Easy to Create State

Machines

1. Fill Big

2. Fill Small from Big

3. Empty Small

4. Fill small from Big

5. Fill Big

6. Fill small from Big

Page 14: The SAM Pattern: State Machines and Computation

How do we describe a

“standard” program as a state

machine?

var fact = function(n) {

return (n == 1) ? 1 : n * fact(n-1);

}

var fact = function(n) {

var f = 1, i =1 ;

for (i = 1; i <= n; ++i) { f = i * f; }

return f ;

}

Page 15: The SAM Pattern: State Machines and Computation

How do we describe a

“standard” program as a state

machine?

computing

i = n

f = 1

done

i==1

i = i - 1

f = f * i

i>1

error

i==0

Page 16: The SAM Pattern: State Machines and Computation

Recursion and State

Machines

Computing

i = 2

n = 5

f1 = 1

fi = i * fi-1i = i + 1

i<=n

error

i==0

f1 = 1

f2 = 2

f3 = 6

f4 = 24

f5 = 120

i = 5

f = f5

!fi && i<=n

done

i>n

Page 17: The SAM Pattern: State Machines and Computation

Introducing TLA+

(Temporal Logic of

Actions)

computing

i = n

f = 1

done

[i==1]

i = i - 1

f = f * i

[i>1]

error

[i==0]

Page 18: The SAM Pattern: State Machines and Computation

Primed Variables

computing

i' = n

f' = 1

done

[i==1]

i' = i - 1

f' = f * i'

[i>1]

error

[i==0]

The meaning of primed

variables is the variable's

value in the next state.

Page 19: The SAM Pattern: State Machines and Computation

Next-State Predicate

computing

i' = n

f' = 1

done

i' = i - 1

f' = f * i'

error

error = (i' == 0)

done = (i' == 1)

computing = !done && !error

f = f'

i = i'

Page 20: The SAM Pattern: State Machines and Computation

What is a Programming

Step?

computing

i' = n

f' = 1

done

i' = i - 1

f' = f * i'

error

error = (i' == 0)

done = (i' == 1)

computing = !done && !error

f = f'

i = i'

1

Compute next-state

property values

2 Update State

3

Decide what

happens next

Page 21: The SAM Pattern: State Machines and Computation

TLA+ is fundamentally

different from classical State

Machines

computing

i = n

f = 1

done

[i==1]

i = i - 1

f = f * i

[i>1]

error

[i==0]

Actions cannot

manipulate State,

and cannot decide

of the Target Stat

e

Page 22: The SAM Pattern: State Machines and Computation

What Changed?

Not much…

Functions

State(s)

i' = i - 1

f' = f * i'Action

error = (i' == 0)

done = (i' == 1)

computing = !done && !error

f = f'

i = i'

Next-State Relation (Function)

Next-Action Functionif (computing) {

}

i' = n

f' = 1 Initial State

Page 23: The SAM Pattern: State Machines and Computation

What Changed?

Page 24: The SAM Pattern: State Machines and Computation

What is State?

What is Type?

Page 25: The SAM Pattern: State Machines and Computation

What is State?

What is Type?A Type, Thing, Model… is a list of property values

{

rpm : 2400

}

A State is a range of property values (sometimes

reduced to one value)

started = (rpm > 900)

Page 26: The SAM Pattern: State Machines and Computation

TLA+ Specification of

Factorial Algorithm

Action

State

Type

Page 27: The SAM Pattern: State Machines and Computation

TLA+ Specification of

Factorial Algorithmi = 5

5

1 5

mult

Page 28: The SAM Pattern: State Machines and Computation

TLA+ Specification of

Factorial Algorithmi = 5

5*1

1 5

mult

4

Page 29: The SAM Pattern: State Machines and Computation

TLA+ Specification of

Factorial Algorithmi = 5

5*1

1 5

mult

4

Page 30: The SAM Pattern: State Machines and Computation

TLA+ Specification of

Factorial Algorithmi = 5

5 4

mult

test

Page 31: The SAM Pattern: State Machines and Computation

TLA+ Specification of

Factorial Algorithmi = 5

5*4

5 4

mult

3

Page 32: The SAM Pattern: State Machines and Computation

How do we use TLA+

in Practice?

Page 33: The SAM Pattern: State Machines and Computation

Modern GUIs have become just a Node

in a Dynamic Distributed System

• your friends,

• players

• your watch

• IoT sensors

• …

Page 34: The SAM Pattern: State Machines and Computation

Events and CallbacksComposability

Encapsulation

Subscriptions

Separation of Concern

Data Consistency

from

to

Maintainability

Ingo Mayer et al (https://infoscience.epfl.ch/record/176887/files/DeprecatingObservers2012.pdf)

Page 35: The SAM Pattern: State Machines and Computation

After some intense research …

1. Framing the code we used

to write as a

“class/component”

2. Adding a “reactive” change

detection mechanism to re-

render components

automatically each time their

properties change

Leading Web Frameworks like

React and Angular came up

with a … component Model …

Page 36: The SAM Pattern: State Machines and Computation

Functional Reactive

Programming: Elm/Redux

• Immutable data

• Observables

• Pure functions

• Static typing

• Unidirectional data flow

Source: http://www.codemag.com/article/1601071

http://guide.elm-lang.org/architecture/user_input/buttons.html

Page 37: The SAM Pattern: State Machines and Computation

33% of the code in Adobe’s desktop

applications is devoted to event handling

logic

50% of the bugs reported during a product

cycle exist in this code

Sean Parent , “A Possible Future of Software

Development”, Adobe 2007

Page 38: The SAM Pattern: State Machines and Computation

Introducing SAM

- State-Action-Model -

Page 39: The SAM Pattern: State Machines and Computation

??

The SAM Pattern

View

Mode

l

??

© 2016 xgen.io

Page 40: The SAM Pattern: State Machines and Computation

The SAM Pattern

View

Mode

l

V = f(M) Counter = Counter + 1Counter = Counter + 1

var p_counter = counter + 1

counter = p_counter

var p_counter = counter + 1

counter.accept(p_counter)

var p_counter = counter + 1

counter.accept(p_counter).then(learn)

1 Proposers

2 Acceptor(s) 3 Learners

Dr. Leslie Lamport: TLA+

http://research.microsoft.com/en-us/um/people/lamport/pubs/state-machine.pdf

{

}

Page 41: The SAM Pattern: State Machines and Computation

SAM is Unapologetically

Focused on Mutation

View

Model

Action

s

V = f(props)

State next-action(Model)

1 Proposers

2 Acceptor(s)

next-

action(Model)

3Learner(s)

© 2016 xgen.io

dispatch(event)

Page 42: The SAM Pattern: State Machines and Computation

View=State(Model.present(Action(event))).then(nap);

“SAM is simple, I [have] never seen a

concept such minimalist and

powerful, [in particular] in its

decoupling potential”

— Daniel

Neveux

next action

Page 43: The SAM Pattern: State Machines and Computation

Unidirectional Data Flow /

Single State Tree

Create Proposal

Modify Data

State Representation

SAM Pattern

next-action

React/Redux Angular2 – Two Way Databinding

Page 44: The SAM Pattern: State Machines and Computation

How do you write a Web

App with SAM?

<body>

<div id=“sam”></div>

<script src=“sam.js”></script>

<script type=“text/javascript”>

var a = { };

initSAM() ;

</script>

</body>

https://github.com/jdubray/sam-samples

View = State(

Model.present(

Action(event)

)

).then(nap);

Page 45: The SAM Pattern: State Machines and Computation

view.innerHTML =

State(

Model.present(

Action(event)

)

).then(nap);

A simple

Blog Example

https://fish-trader.gomix.me/

<div id="representation"></div>

Page 46: The SAM Pattern: State Machines and Computation

view.innerHTML =

State(

Model.present(

Action(event)

)

).then(nap);

A simple

Blog Example

https://fish-trader.gomix.me/

<div id="representation"></div>

Page 47: The SAM Pattern: State Machines and Computation

view.innerHTML =

State(

Model.present(

Action(event)

)

).then(nap);

A simple

Blog Example

https://fish-trader.gomix.me/

<div id="representation"></div>

Page 48: The SAM Pattern: State Machines and Computation

view.innerHTML =

State(

Model.present(

Action(event)

)

).then(nap);

A simple

Blog Example

https://fish-trader.gomix.me/

<div id="representation"></div>

Page 49: The SAM Pattern: State Machines and Computation

Let’s look at some code

© Akveo 2016

Page 50: The SAM Pattern: State Machines and Computation

Angular2

Component

Coupling the

business

logic to a

Web

Framework is

an anti-

pattern

events

Propertie

s

Page 51: The SAM Pattern: State Machines and Computation

Angular2

Unidirectional Data

Flow

Single State Tree

Actions

events

Propertie

s

Actions

State

Model

Page 52: The SAM Pattern: State Machines and Computation

SAM / Angular2

Action

s

Model

State

Page 53: The SAM Pattern: State Machines and Computation

Conversational

Computing

https://www.infoq.com/news/2017/02/mindmeld-guide

Amazon Alexa,

Apple Siri,

Google Assistant,

Microsoft Cortana

“Since rule-based frameworks are not

intended to provide AI capabilities to

parse or classify incoming messages,

the developer must code all of the

necessary message processing and

interaction logic by hand. It is not

uncommon for even simple

applications to require hundreds of

rules to handle the different dialogue

states in a typical conversational

interface.”

Page 54: The SAM Pattern: State Machines and Computation

By 2020 there will be 50 billion

Internet of Things (IoT) devices

Internet of Things

Page 55: The SAM Pattern: State Machines and Computation

A new Application Architecture

- Services, APIs and Microservices -

Page 56: The SAM Pattern: State Machines and Computation

SAM Isolates APIs

from the View

CUD

R

There is no need for

• an immutable model (Redux)

• declarative effects (Elm, ~Redux)

They create more problems than they solve

Create Proposal

Modify Data

State Representation

SAM Pattern

next-action

Page 57: The SAM Pattern: State Machines and Computation

SAM + SAM

API API API

Co

nsis

ten

cy

APIs

Services

Microservices

Systems of Record

Services

Model

ActionsState

View

Model

Dispatche

rState

View

Model

ActionsView

Activity

ActionAction

Page 58: The SAM Pattern: State Machines and Computation

In Summary• Programming model

• Centered on Mutation, not

immutability

• True Single State Tree, no

Sagas/Stateful components

• Focused on ”what’s allowed”,

not subscriptions

• View Components are 100%

decoupled from the application

business logic

• Functional UI/HTML (code

generation), not templates

• Architecture

• Can be implemented with the framework of your choice (React, Angular)

• Side-effects friendly

• Wiring agnostic

• Truly Isomorphic

• Action “Hang back” / Generic Cancellations

• 3rd party Actions (OAuth)

Page 59: The SAM Pattern: State Machines and Computation

References

• Computation and State Machines – Dr. Lamport

• Thinking for Programmers – Dr. Lamport