unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Artyom V. Poptsov" <poptsov.artyom@gmail.com>
To: "Dr. Arne Babenhauserheide" <arne_bab@web.de>
Cc: guile-user@gnu.org
Subject: Re: [ANN] Guile-SMC 0.6.2 released
Date: Fri, 11 Aug 2023 15:42:32 +0300	[thread overview]
Message-ID: <87wmy1n41z.fsf@gmail.com> (raw)
In-Reply-To: <87leehzt4c.fsf@web.de> (Arne Babenhauserheide's message of "Fri,  11 Aug 2023 13:57:46 +0200")

[-- Attachment #1: Type: text/plain, Size: 5234 bytes --]

> 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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 519 bytes --]

      reply	other threads:[~2023-08-11 12:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-11  9:32 [ANN] Guile-SMC 0.6.2 released Artyom V. Poptsov
2023-08-11  9:52 ` Dr. Arne Babenhauserheide
2023-08-11 10:20   ` Artyom V. Poptsov
2023-08-11 11:57     ` Dr. Arne Babenhauserheide
2023-08-11 12:42       ` Artyom V. Poptsov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wmy1n41z.fsf@gmail.com \
    --to=poptsov.artyom@gmail.com \
    --cc=arne_bab@web.de \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).