These are chat archives for jdubray/sam

1st
Mar 2016
Jean-Jacques Dubray
@jdubray
Mar 01 2016 12:53
Here is a metaphor that may help why state/actions are so important, technically this is correct, https://pbs.twimg.com/media/CcYb9ZoW4AAF_mr.jpg
Joke aside, the state "full" is defined by our inability to take the action to add water to the glass not just by the model property values. It's the same in software, we cannot properly implement a system unless we reason properly about the actions that can be taken at any point in time.
Michael Terry
@formido
Mar 01 2016 17:21
Using explicit state machines has been regarded as overly verbose in most software development
Is Lamport's argument that we should still be conscious at all times that we are trying to approximate a state machine?
Keep all the state machine like switching in one place?
Jean-Jacques Dubray
@jdubray
Mar 01 2016 17:37
As I said before, yes, we should be coding with state machines.
State machines are the fabric of computing, you can write abstractions that optimize the way we code state machines
But we cannot write code that violate the semantics of state machines.
There lies the problem, we often focus on "wiring" rather than semantics, then we systematically write code without consideration for States and actions
Michael Terry
@formido
Mar 01 2016 17:47
It seems to me a little bit difficult to evangelize this because of the overloaded state term
The vast majority of people you are trying to talk to think of state as, e.g., redux's state tree
There are two places in SAM that switch on states, right? Creating the UI representation and checking for next actions?
Jean-Jacques Dubray
@jdubray
Mar 01 2016 17:53
Yes, you need to make a distinction between the model property values and the way you translate the values into a control state which drives the view rendering and nap
Michael Terry
@formido
Mar 01 2016 17:53
Actions don't consult state, right? The model doesn't consult state to decide whether to accept proposed changes?
Jean-Jacques Dubray
@jdubray
Mar 01 2016 17:54
Well yes and no, per se no, but you way want to validate that the action is allowed
Especially when concurrent actions can occur
Michael Terry
@formido
Mar 01 2016 17:56
right, but that validation happens in nap() or by implication of which controls are shown to the user, no?
validate the action, then dispatch the action
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:10
yes, exactly, but you could also design a system where actions can be triggered from anywhere
in that case, because the model does not know about the current state or the action it's best to design a S+A container. All you need to know is the list of expected actions.
Michael Terry
@formido
Mar 01 2016 18:12
what's a S+A container?
which part of the system validates the actions?
If the action is a function, does it consult the state then?
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:13
In general you would process one action at a time, and the container would queue up any action that is triggered before the current action is processed.
Michael Terry
@formido
Mar 01 2016 18:13
ah
I was curious about this
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:13
The pattern makes no provision for validating the actions
Michael Terry
@formido
Mar 01 2016 18:14
by validate, I mean, decide if an action is authorized
the state machine
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:14
that's why if you feel this is an issue, you have to write a State+Action container that keeps track of the list of valid actions (as an output from the State) and then validates income requests for actions
it does not have to me a state machine per se
this is aligned with state machines, but you can always result to if-then-else
if (water>0 && water<100) { allowedActions=['drink','pour']
Michael Terry
@formido
Mar 01 2016 18:15
sure, that's fine, whether it's a state machine or a State Machine, it really has to switch on summaries of state
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:16
if (water === 0) { allowedActions = ['pour'] ; }
...
yes
Michael Terry
@formido
Mar 01 2016 18:16
but this state consultation can basically appear anywhere?
in Rocket Launcher it's in representation and nap
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:16
yes, since you cannot initiate an invalid action from the UI
but in distributed systems this is less true
Michael Terry
@formido
Mar 01 2016 18:17
got it, got it
that was a question for me
if a state representation is stale
then an invalid action might be initiated
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:17
Also if the model, hence the state has changed and the view is stale, the list of actions may not be accurate
Michael Terry
@formido
Mar 01 2016 18:17
in that case you need to check state BEFORE presenting to the model
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:17
exactly.
Michael Terry
@formido
Mar 01 2016 18:17
excellent
someone had mentioned earlier nap() being a concurrent sync point
is that the case?
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:18
the problem with the MVX type of pattern is that the logic is imbalanced. The controller has to orchestrate everything to get to the point where it can nap()
yes, you would queue concurrent actions in nap() and if the queue is not empty, nap() would trigger them
the beauty is that nap() knows the state and preempt an action if the state does not allow it
it's one of the best sync point you can imagine
That's why I don't fall for wiring like observables
there is nothing inherently good about a stream of events. Streams seem to conflict violently with state machines though there is a class of problems they solve well
but they cannot be applied blindly to any kind of problem
Michael Terry
@formido
Mar 01 2016 18:21
yeah, makes sense
I don't quite see how actions would get queued in nap tho
normally actions do something then present data to the model and THEN nap() happens
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:22
the container would do that, SAM is just pattern
Michael Terry
@formido
Mar 01 2016 18:23
but the container should fit the sam pattern
so I don't follow that
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:23
It does not prevent it, or change it
it's just a container, just like any business logic container. It implements services that otherwise you'd have to code inside each action / state.
It would be hard to code it into an action because you need the list of actions, I would want to avoid that at all costs
Michael Terry
@formido
Mar 01 2016 18:25
ok, I get it
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:25
in that case you'd need a library
Michael Terry
@formido
Mar 01 2016 18:25
in a concurrent or distributed system, an initiated action could get queued first in nap, in addition to nap being fired after a model update
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:25
yes, exactly, it's a natural flow, aligned with state machines
Now it would not solve all concurrency problems,
Michael Terry
@formido
Mar 01 2016 18:26
ok, this answers a ton of questions that I've been developing last couple days
I like it
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:26
at least no efficiently, there might be in some cases more efficient solutions.
Michael Terry
@formido
Mar 01 2016 18:26
there are a couple popular js state machine libraries out there already
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:27
What I like about SAM is that it guides developers to do a lot of things the right way, without hardly any burden (library, frameworks, mind bending abstractions)
Michael Terry
@formido
Mar 01 2016 18:27
have you looked at them?
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:27
no unfortunately
be careful though because in generate state machine are using some semantics that are incompatible with SAM
Michael Terry
@formido
Mar 01 2016 18:27
one of them is sort of inspired by erlang, which as an erlang dev, I like :)
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:27
they use typles (S1,A,S2)
this is not working, SAM is: (S1 -> A -> M -> S2)
The action never decides of the end-state in SAM, the tuples (S1,A,S2) are observed not executed
took me 20 years to understand that (after reading TLA+)
Michael Terry
@formido
Mar 01 2016 18:29
I thought SAM was (S1, A1, A2, An) or something
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:29
yes exactly
but when you apply an action, then the model decides the end state
that's what I was trying to represent by (S1 -> A -> M -> S2)
Michael Terry
@formido
Mar 01 2016 18:30
I see
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:30
All I am saying is that frameworks would most likely be based on the traditional semantics and therefore may not work as well
SAM implies a big decoupling between the Action and the end state
first you go through the model, and then even the model does not know the end state, it is the function S(M) that tells you which control state you reached.
that's why I created the STAR library, that's how it works
Michael Terry
@formido
Mar 01 2016 18:32
it's hard for me to tell when your/lamports's discussion of state machines is formal and when it's more of a Gang of Four type pattern
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:33
sure, I am no Lamport, by far
Michael Terry
@formido
Mar 01 2016 18:33
Heh
Not many are
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:35
:-)
Michael Terry
@formido
Mar 01 2016 18:35
I think a concurrent example of SAM would be very helpful
The flux challenge might be perfect
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:36
Agreed, we are working on a Time Travel container right now, I'll see if I can add some concurrency into it.
Michael Terry
@formido
Mar 01 2016 18:36
Nice
Milan Simonovic
@mbsimonovic
Mar 01 2016 18:38
sorry to interrupt, what do you mean when you say "container" ?
Jean-Jacques Dubray
@jdubray
Mar 01 2016 18:41
"middleware: