SlideShare a Scribd company logo
I believe that the best way to get better
programs is to teach programmers how to
think better
-- Dr. Leslie Lamport
SAMThe State Action Model Pattern
Jean-Jacques Dubray
© https://creativecommons.org/licenses/by-nc-sa/4.0/
'
(primed variables)
Action State
Thing Relationship
Dynamic
Static
Physical Conceptual
The STAR Diagram
Light Energy
Mass Force
Dynamic
Static
Physical Conceptual
STAR is as fundamental to
Computing as the FLEM Diagram is
to Physics
Thing
RelationshipState
Action
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
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
State Machines can
Compute…
“the die-hard problem”
5 liters 3 liters
4 liters
State Machines can
Compute
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
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 liters
3 liters
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
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 ;
}
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
Recursion and State
Machines
Computing
i = 2
n = 5
f1 = 1
fi = i * fi-1
i = 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
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]
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.
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'
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
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
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 Function
if (computing) {
…
}
i' = n
f' = 1Initial State
What Changed?
What is State?
What is Type?
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)
TLA+ Specification of
Factorial Algorithm
Action
State
Type
TLA+ Specification of
Factorial Algorithm
i = 5
5
1 5
mult
TLA+ Specification of
Factorial Algorithm
i = 5
5*1
1 5
mult
4
TLA+ Specification of
Factorial Algorithm
i = 5
5*1
1 5
mult
4
TLA+ Specification of
Factorial Algorithm
i = 5
5 4
mult
test
TLA+ Specification of
Factorial Algorithm
i = 5
5*4
5 4
mult
3
How do we use TLA+
in Practice?
Modern GUIs have become just a Node
in a Dynamic Distributed System
• your friends,
• players
• your watch
• IoT sensors
• …
Events and Callbacks
Composability
Encapsulation
Subscriptions
Separation of Concern
Data Consistency
from
to
Maintainability
…
Ingo Mayer et al (https://infoscience.epfl.ch/record/176887/files/DeprecatingObservers2012.pdf)
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 …
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
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
Introducing SAM
- State-Action-Model -
??
The SAM Pattern
View
Mode
l
??
016 xgen.io
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
{
}
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)
016 xgen.io
dispatch(event)
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
Unidirectional Data Flow /
Single State Tree
Create Proposal
Modify Data
State Representation
SAM Pattern
next-action
React/Redux Angular2 – Two Way Databinding
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);
view.innerHTML =
State(
Model.present(
Action(event)
)
).then(nap);
A simple
Blog Example
https://fish-trader.gomix.me/
<div id="representation"></div>
view.innerHTML =
State(
Model.present(
Action(event)
)
).then(nap);
A simple
Blog Example
https://fish-trader.gomix.me/
<div id="representation"></div>
view.innerHTML =
State(
Model.present(
Action(event)
)
).then(nap);
A simple
Blog Example
https://fish-trader.gomix.me/
<div id="representation"></div>
view.innerHTML =
State(
Model.present(
Action(event)
)
).then(nap);
A simple
Blog Example
https://fish-trader.gomix.me/
<div id="representation"></div>
Let’s look at some code
© Akveo 2016
Angular2
Component
Coupling the
business
logic to a
Web
Framework is
an anti-
pattern
events
Propertie
s
Angular2
Unidirectional Data
Flow
Single State Tree
Actions
events
Propertie
s
Actions
State
Model
SAM / Angular2
Action
s
Model
State
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.”
By 2020 there will be 50 billion
Internet of Things (IoT) devices
Internet of Things
A new Application Architecture
- Services, APIs and Microservices -
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
SAM + SAM
API API API
Consistency
APIs
Services
Microservices
Systems of Record
Services
Model
ActionsState
View
Model
Dispatche
r
State
View
Model
Actions
View
Activity
ActionAction
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)
References
• Computation and State Machines – Dr. Lamport
• Thinking for Programmers – Dr. Lamport

More Related Content

PPTX
The SAM Pattern: a Distributed System View of Front-End Architectures
PDF
Reinventing the Transaction Script (NDC London 2020)
ODP
Functional programming
PPTX
Constructors in java
PDF
Android is NOT just 'Java on Linux'
PDF
From Java 11 to 17 and beyond.pdf
PPTX
Facade Design Pattern
PDF
Mockist vs. Classicists TDD
The SAM Pattern: a Distributed System View of Front-End Architectures
Reinventing the Transaction Script (NDC London 2020)
Functional programming
Constructors in java
Android is NOT just 'Java on Linux'
From Java 11 to 17 and beyond.pdf
Facade Design Pattern
Mockist vs. Classicists TDD

What's hot (20)

PPTX
Loops in C
PPTX
Clean Code
PDF
FP in Java - Project Lambda and beyond
PPTX
Clean Code
PDF
Python programming : Control statements
PDF
Java 8 Lambda Built-in Functional Interfaces
PPTX
operator overloading & type conversion in cpp over view || c++
DOCX
Autoboxing and unboxing
PDF
[22]Efficient and Testable MVVM pattern
PPTX
Graphical User Interface (Gui)
PPTX
5. Destructuring | ES6 | Assignment
PPT
Java operators
PDF
Functional programming with Java 8
PPT
Introduction to Design Patterns and Singleton
PDF
Clean coding-practices
PPTX
Microsoft dot net framework
PDF
Static and const members
PPTX
06.Loops
PPTX
Exception handling in java
PPTX
python conditional statement.pptx
Loops in C
Clean Code
FP in Java - Project Lambda and beyond
Clean Code
Python programming : Control statements
Java 8 Lambda Built-in Functional Interfaces
operator overloading & type conversion in cpp over view || c++
Autoboxing and unboxing
[22]Efficient and Testable MVVM pattern
Graphical User Interface (Gui)
5. Destructuring | ES6 | Assignment
Java operators
Functional programming with Java 8
Introduction to Design Patterns and Singleton
Clean coding-practices
Microsoft dot net framework
Static and const members
06.Loops
Exception handling in java
python conditional statement.pptx
Ad

Similar to The SAM Pattern: State Machines and Computation (20)

PDF
Intro to functional programming - Confoo
PDF
Second Level Cache in JPA Explained
PPT
Software Engineering Systems Designing end to end
PDF
Hierarchical free monads and software design in fp
PPTX
Flink internals web
PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
PPS
Data Structure
PPS
Lec 1 Ds
PPS
Lec 1 Ds
PDF
TensorFlow and Deep Learning Tips and Tricks
PPT
Complex Event Processing
PPTX
PDF
R Programming: Mathematical Functions In R
PPTX
The joy of functional programming
PDF
Functional Programming in R
PDF
Operationalizing Clojure Confidently
PDF
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
PDF
Lazy Java
PDF
Lazy Java
Intro to functional programming - Confoo
Second Level Cache in JPA Explained
Software Engineering Systems Designing end to end
Hierarchical free monads and software design in fp
Flink internals web
Beyond Breakpoints: A Tour of Dynamic Analysis
Data Structure
Lec 1 Ds
Lec 1 Ds
TensorFlow and Deep Learning Tips and Tricks
Complex Event Processing
R Programming: Mathematical Functions In R
The joy of functional programming
Functional Programming in R
Operationalizing Clojure Confidently
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
Lazy Java
Lazy Java
Ad

Recently uploaded (20)

PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Nekopoi APK 2025 free lastest update
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Transform Your Business with a Software ERP System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Cost to Outsource Software Development in 2025
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
history of c programming in notes for students .pptx
PDF
System and Network Administraation Chapter 3
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Design an Analysis of Algorithms I-SECS-1021-03
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Operating system designcfffgfgggggggvggggggggg
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Nekopoi APK 2025 free lastest update
CHAPTER 2 - PM Management and IT Context
Transform Your Business with a Software ERP System
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PTS Company Brochure 2025 (1).pdf.......
Odoo POS Development Services by CandidRoot Solutions
Softaken Excel to vCard Converter Software.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Cost to Outsource Software Development in 2025
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
history of c programming in notes for students .pptx
System and Network Administraation Chapter 3
Designing Intelligence for the Shop Floor.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design

The SAM Pattern: State Machines and Computation

Editor's Notes

  • #15: 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 ; }
  • #34: Graphical User Interfaces are part of a wide class of software, Reactive Applications, that responds to user input, network messages, and other events Reactive code is asynchronously triggered by event occurrences It is hard to trace and understand the control flow of the entire system