Better HN
topnewbestaskshowjobs
Log inSign up

goless

61 karma · joined Mar 15, 2019 (7y ago) · 44 submissions

Submissions

comment 9mo ago · link · parent

In the world of distributed systems, we often find ourselves caught in a dilemma: a protocol requires the collaboration of multiple roles, yet the behavioral logic of each role must be implemented separately. As a result, the exact same control flow is repeatedly copied, pasted, and modified, eventually devolving into an unmaintainable maze of code. The emergence of Troupe breaks this dilemma in an almost magical way—it models the protocol as a global state machine, and then "projects" this state machine onto each role at compile time, allowing each role to automatically obtain its own control flow. This process incurs no runtime overhead, yet it fundamentally changes the way distributed programs are written.

comment 9mo ago · link · parent

Troupe is a distributed protocol construction library built on Zig's type system. Its core philosophy is using type determinism to counter communication uncertainty: protocols are modeled as fully deterministic state machines, correctness is guaranteed through compile-time verification, and all communication unreliability (latency, loss, reordering) is isolated behind replaceable channel layers. Ultimately, developers can construct complex multi-role protocols as if writing single-threaded programs, confident that they will execute as intended in any environment.

comment 1y ago · link · parent

Multi-role finite state machine, used for multithreaded programs or distributed programs.

The behaviors of multiple characters are orchestrated into a state machine. Imagine a theater with many characters performing a play. The behaviors of all characters are arranged as a whole in the script. When a certain message is sent, everyone will complete their performance according to the script.

4.

Troupe:multi-role finite state machine(ziggit.dev ↗)

2 points · by goless · 1y ago · 1 comment

comment 1y ago · link · parent

How to Properly Use Polystate?

The correct way to use polystate is: reason about program behavior through symbols, then transform these symbols into code.

https://github.com/sdzx-1/ray-game/blob/master/How-to-proper...

comment 1y ago · link · parent

I changed the file name, here is the new article!

comment 1y ago · link · parent

https://github.com/sdzx-1/ray-game/blob/master/How-to-proper...

comment 1y ago · link · parent

The correct way to use polystate is: reason about program behavior through symbols, then transform these symbols into code.

This might sound bewildering, but don't worry - I'll explain this process through concrete examples.

9.

How to Properly Use Polystate?(github.com ↗)

1 points · by goless · 1y ago · 3 comments

comment 1y ago · link · parent

Due to my submission yesterday, this diagram can no longer fully accurately express the current state machine, but it can probably be used as an approximate reference.

comment 1y ago · link · parent

https://github.com/sdzx-1/polystate/raw/master/data/graph.pn...

This is the state diagram combining two selects!

comment 1y ago · link · parent

Haha, thanks for your reply, I will improve my documentation to make the descriptions more accurate, and you may raise some issues to help this library better.

As for why semantic composition is used, the following example may be instructive:

yes_or_no(exit,a) This means confirm once before exiting

yes_or_no(yes_or_no(exit,a),a) Confirm twice before exiting

yes_or_no(yes_or_no(yes_or_no(exit,a),a),a) Confirm three times before exiting

This is very similar to the composition you describe.

This is the actual code: https://github.com/sdzx-1/polystate-examples/blob/fecaffb5b7...

If there is no name for this way of using state machines, then semantic composition seems appropriate.

Exceptionally, combining two states in a polystate may actually be combining two small state machines, and I think nested selects are sufficient to illustrate this.

https://github.com/sdzx-1/ray-game/blob/9646bc3c0f1c11314aaf...

How do you understand such behavior?

comment 1y ago · link · parent

Here I must mention the great advantage of the Zig language: it has the expressiveness of a dynamic language and the type safety of a static language.

comment 1y ago · link · parent

I'm not sure if this is a new form of state machine.

If you look at the code here you'll see that the select state uses some functions from the parameter state.

https://github.com/sdzx-1/ray-game/blob/master/src/select.zi...

So you can see that the select state itself is incomplete, and the parameter state completes it.

It doesn't seem to be any of the 3 described above.

Perhaps it is reasonable to call it semantic composition.

comment 1y ago · link · parent

Try modifying it yourself, you will fall in love with this way of programming.

comment 1y ago · link · parent

In fact, relying on this monad, we can achieve full type safety for multi-role communication.

https://github.com/sdzx-1/typed-session

comment 1y ago · link · parent

I once wrote a library typed-fsm https://github.com/sdzx-1/typed-fsm

It has a completely type-safe finite state machine, and it does not require additional programming conventions.

It relies on a special monad: Mcbride Indexed Monad, which can model uncertainty in terms of type.

Are you using this monad here?

Here is a concrete ATM demo https://github.com/sdzx-1/typed-fsm/tree/main/examples/ATM

comment 1y ago · link · parent

In fact, I have introduced this in detail here. It is a kind of finite state machine programming, and everything that FSM can solve is suitable for it.

comment 1y ago · link · parent

Haha, this is just a demo, written casually, you can change this to exit, and then as you expected, the program will exit directly.

comment 1y ago · link · parent

From this perspective, do you think my previous description is accurate?

comment 1y ago · link · parent

Is there a way to combine:

Higher order finite state machines that require other states as parameters to work.

https://github.com/sdzx-1/ray-game/blob/master/src/select.zi...

The select, inside, and hover states here are all high-level states, and all require two state parameters. And these three states form a small state machine for handling mouse interactive selection.

Can I think of this way of using higher-order state machines as a kind of composition? A semantic composition.

comment 1y ago · link · parent

Yes the expression may not be precise enough, but the example should be accurate enough.

1, https://github.com/sdzx-1/polystate?tab=readme-ov-file#2-imp...

This shows what composition means, and even complex nested selects are described quite precisely by type.

2, https://github.com/sdzx-1/polystate?tab=readme-ov-file#1-com...

Yes, this effect can be achieved without using composite types. But if it is convenient and easy to achieve this effect through composite types, is it worth it?

comment 1y ago · link · parent

Sorry, I am not familiar with Rust. Although I try hard to understand this library, I cannot say that I understand it.

I have two questions here:

1. How to express uncertain state in type. In my example, it is implemented by union (enum) + Witness https://github.com/sdzx-1/polystate-examples/blob/fecaffb5b7...

2. I don't seem to see the possibility of combining state machines

comment 1y ago · link · parent

I need some time to get to this library.

comment 1y ago · link · parent

I don't know much about XState but it looks more like a dynamically interpreted execution state machine.

comment 1y ago · link · parent

Yes, but their expressiveness may vary. An important role of polystate is code reuse. It can express more complex states and still be type-safe.
12