guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ANN] Guile-SMC 0.6.2 released


From: Artyom V. Poptsov
Subject: Re: [ANN] Guile-SMC 0.6.2 released
Date: Fri, 11 Aug 2023 15:42:32 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)

> Just out of interest: can you post a part of the code you really like?

Well, as Guile-SMC is a tool for making tools, I think that Guile-INI
could serve as a good example of what Guile-SMC can do.

Here, you can see the finite state machine (FSM) description in PlantUML
format:
  https://github.com/artyom-poptsov/guile-ini/blob/master/modules/ini/fsm.puml

Here's an excerpt from the file:

--8<---------------cut here---------------start------------->8---
read: The entry point
read ---> [*]: char:eof-object?
read --> read_global_comment: ini:comment/read?
read --> skip_global_comment: char:semicolon?
read --> read_section_title: char:left-square-bracket?
read --> read_property_key: char:letter? -> push-event-to-buffer
read -> read
--8<---------------cut here---------------end--------------->8---

"read" is a state, and the state has one transition to the final state
("[*]"), four transitions to another states and one transition to
itself.  This representation is basically is like a "cond" directive in
Scheme (or "switch..case" in C) that checks in turn each condition
(e.g. "char:letter?".)  Take this line for example:

--8<---------------cut here---------------start------------->8---
read --> read_property_key: char:letter? -> push-event-to-buffer
--8<---------------cut here---------------end--------------->8---

When a condition ("guard") returns "#t" the action assigned to this
transition is executed (in our example its "push-event-to-buffer") and
the FSM proceeds to the next state ("read_property_key".)

Note that most of the checks and all the actions here are provided by
Guile-SMC core context, and there's no need to implement it again and
again in each project.  In pre-Guile-SMC times I used to manually write
FSMs with those guards and actions for each project and this was real
PITA.  Also FSMs generated by Guile-SMC have embedded logging and this
helps too (with debugging/profiling.)

In that way Guile-SMC could serve as the basis for various parsers (I
already wrote FSMs for Guile-INI, Guile-ICS and Guile-PNG; and I'm
currently working on porting Guile-DSV FSM to Guile-SMC -- this work is
almost done.)

The only procedure that does not belongs to Guile-SMC is
"ini:comment/read?" -- this procedure is provided by Guile-INI custom
context:
  
https://github.com/artyom-poptsov/guile-ini/blob/master/modules/ini/fsm-context-ini.scm

If you take a look at this file you'll find that there's not so much
custom code written.

Also when you describe your FSMs in terms of formal notation such as
PlantUML (or Graphiz for that matter, but Guile-SMC does not support
Graphiz yet), you get transition diagram visualization "for free":
  
https://www.plantuml.com/plantuml/uml/jPV1Rji-3CRlVWe2_ziVS7RFOUqpRBqDGw0uJAp5aJo9RfSz_MANJWFjYsRNswL0P7u_Mnz5YdsCOe9qfwRuylzNAikF5DZiYgB_kwQdcecTX3ErtZePLgdieoxHQwfg4zRizwNVVkDA7lIvGZQ7nczDiwdywSHErcITNUkF3tt0JNmysZQ9LtpaawryurrAZro9zMuZLXhMu8RtKiRldGbSReEfZYofh0s-vI3dpP7FLvK8XmUwXBHTh4j-PYUb-7BRxVSS530w0oYV-W13uo9T5bU_nsMCw-bI1_zInhCJywFa47puCyBnIsCTpz3tWS6Ua0snDHK_J18zcwPZbV75xYQMqHxSrK9rhlSmYd4_3Q3YX1mw4tRh_-HkCWhm-P-FjYNJD2J1sCQw0mrxYMJsme6avY4bAbsK4e1OxWwWeZmL1NpFqsNnkk7WEzUVnTTQUZkIG-mA0PPwe11D9tOXNs6OY6qj2lWMhK2yQoHPK8nWrw0SpT2TrgPjsUtAKOSfsBe9jO3QfbO3wM-IsWZPI6rZAjvHrthtMYira_RGY43tDeTZOGil2d0FfCaQ4SRjA8eFJ5PMI9XFMMAuQZ07Rsm-1AnFoHguljIJgmLcJ0qDpu7adGSOCu0J4m7fRLnv9s-NTx27EhsGRtqUXezIjHFukZpRPFqgjLdhp7Aabs15s95rRvQle3RZDuuVFpq__q2X5bDs40AonAhvamu6pv-uC0QV56rYzxgNTT7k-3Ovj-5qFoTf67mvR6r9JqkQMTB5CkrhST-Q-VbJQGfxTyU2iwy-H3CmqYthNejdOkNWGGuy1JFoHCZYcHc-1PJw_1bQnGjcwR-JskaeRLyPpBzFIkOjcUC3BKz6U1tAHh-tlFU1RySqFCrZve8i0DWq0xKuSg2qaq6Gbl12rFos1lwboBYlwhdkvsJlR_GZ9qjVRDWZkrrtQdu1

(Sorry for such a long link; it has the diagram encoded in the URL.)

In my experience, visualization helps in many cases to grasp the problem
at hand, and you would write such formal state diagram anyways.  But
with Guile-SMC you'll get the diagram and the auto-generated code from
it at the same time.

> (I’m currently working on a webservice for work that has a state machine
> in the background and it would be interesting to me to see whether I
> could build a simple version of it with Guile for fun ☺ — trying to find
> more elegance in this)

I think that Guile-SMC is not bound to parsers realm and can be used to
create other state machines that handle network events, for example.
Even the standard library of procedures (the "context" in terms of
Guile-SMC) is written in such way that it does not enforce "the only
true way" of doing things.  For example, you can use anything as a
context memory for FSM, and you can write your own set of guards and
actions.

With all that said, please note that although I have some good
experience with state machines, I'm not considering myself as an expert
in this field; there are lots of nuances that I may not see yet.  So any
help with testing and developing the project will be appreciated indeed.

- avp

-- 
Artyom "avp" Poptsov <poptsov.artyom@gmail.com>
Home page: https://memory-heap.org/~avp/
CADR Hackerspace co-founder: https://cadrspace.ru/
GPG: D0C2 EAC1 3310 822D 98DE  B57C E9C5 A2D9 0898 A02F

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]