These are chat archives for jdubray/sam

Jun 2017
Fred Daoud
Jun 24 2017 01:54 UTC

Hello friends, just wanted to share a little experiment I did tonight.

After looking at union-type and daggy, I found them to be nice, but not quite what I needed. I want a case function that can handle a plain JS object, something that can be JSON.stringify'd and recuperated with JSON.parse.

I went ahead and wrote my own utility, which is nowhere near as complete as union-type or daggy, but on the other hand, does what I want, and is so small that it's not even a library, it's just a few lines of code:

// The "Case" utility is just a few lines of code:
const Case = {
  cases: types => types.reduce((acc, next) => { acc[next] = next; return acc; }, {}),
  of: function(type) {
    const data =, 1);
    return { type, data };
  case: (handler, of) => handler[of.type].apply(null,

Sample usage:

const Status = Case.cases([

const handler = {
  Loading: () => "LOADING...",
  Success: data => "SUCCESS: " + data,
  Failure: (exc, msg) => "FAILURE: " + exc + ", " + msg

const c1 = Case.of(Status.Success, "some data");
console.log(, c1));
// SUCCESS: some data

Sometimes minimal is beautiful :)

Antanas A.
Jun 24 2017 07:44 UTC

Hi, from

On the other hand, the state-action behavior of the glass relies on three control states, and the corresponding next-actions:

    full: drink
    in-between: drink, add water
    empty: add water

@jdubray now it's clear what you had mean by "state" :)

In F# it's possible to do so in programming model, F# has discriminated union types, example with shopping cart:
harder way to achieve this equally in C#: (pretty harder)
Antanas A.
Jun 24 2017 07:49 UTC
but F# is more comprehensive on that
Rob Siera
Jun 24 2017 09:37 UTC
with the help of @nickpasko I made an initial attempt available of a port to C# of the java 0.8 version of SCM: It still needs a lot of love. Still I hope it is valuable to the community.
Victor Noël
Jun 24 2017 10:02 UTC
@antanas-arvasevicius it's also possible in typescript, but the fact that typescript is not forcing the typing makes it not so nice to use (you have to either activate some compiler flags or use some hack with never, see, see Discriminated Unions and Exhaustiveness checking sections)
Jean-Jacques Dubray
Jun 24 2017 13:59 UTC
@antanas-arvasevicius I see, that's nice, but that's what I would call a classical state machine, that kind of implementation does not scale. Dense state machines are very hard to reason about. The biggest advantage of SAM (form TLA+) is that it has a robust state machine structure without forcing you to think in terms of a graph of states and actions. When you need to think that way you can, when it's too cumbersome, you don't have too.
@robsiera :thumbsup: thank you! I will take a look though I have not written anything in C# in at least 10 years.
Jean-Jacques Dubray
Jun 24 2017 14:08 UTC

@antanas-arvasevicius This is really the core of the argument and SAM:

Actually, even state-action behaviors are challenged to provide a formalism where “control states” are modeled properly: they suffer from an approximation of their own because the action’s metamodel connects the action to the end state, as if, the action of adding water to the glass would be able to know when the glass is full.

Once you understand that argument, you get everything there is to know about SAM and you probably understand why there was something wrong with the way you were writing code. It took me 20 years, ... and a glass of water... to figure it out

That's why I am saying that you have to be really really super careful in coming up with the semantics of a programming model, you can't just say "today I'll do this". The SAM semantics should be changed with caution and a solid understanding as to why you are doing so. The semantics are from TLA+, they are not mine, I am not trying to tell you, listen to me, I got it right. Dr. Lamport got it right.
Jean-Jacques Dubray
Jun 24 2017 14:14 UTC
@foxdonut you lost me a bit, even though I don't like the "ifs" in the model, I have not found/seen a better way yes. The many-to-many relationship between actions and acceptor is critical to the pattern and cannot be compromised. I use it very often.
Fred Daoud
Jun 24 2017 16:01 UTC
@jdubray perhaps à better use case is for the state représentation for the view to use, i.e. loading vs success vs error.
Jean-Jacques Dubray
Jun 24 2017 16:03 UTC
yes, absolutely.