Fork me on GitHub
#core-logic
<
2022-06-11
>
Martynas Maciulevičius11:06:00

Hey. I have a question about logic programming in general and not about core.logic. Let's say I have an event stream and I accumulate state over time. If I think about the incoming event stream as string characters then I could write a state machine (regex) that would capture some kind of state about the inputs. What I think about is to make this state machine for the incoming stream and be able to somehow say what is the current "state" of the state machine. I don't know how it could work and don't know if it makes much sense to you. But maybe there could be a library that could handle this? I could write the matching and checking by hand but it would be way nicer to specify some constraints and then allow to 'work itself out'. I don't know what the end result could be. In my opinion maybe it could be some kind of events or something. But first let's see if anybody understands what I'm talking about.

Ben Sless15:06:19

There are state machine libraries, why not use them?

Martynas Maciulevičius15:06:00

Yes, this is what I thought about but I didn't know what to look for. I'm not yet sure how to use it. Is it something like this one? https://github.com/cdorrat/reduce-fsm

chrisblom22:06:50

I'm not sure what you are talking about. Do you want to validate an event stream? Or emit events in response to incoming events? Are you familiar with finite state machine and finite state transducers?

Martynas Maciulevičius07:06:03

Hey. I didn't even see that you replied because you replied to the main topic but alright. Also that means that the other commenter will also miss this message. I want to not only validate but also emit events according to my internal state and incoming events. Finite state machine is familiar to me (regexes?) but I would like to have multiple of those machines at the same time for different contexts. And they must not "overlap" based on some kind of criteria (regex that stops matching because it currently already matches a string that has 5 of the first same chars) and if it happens then it should produce a rejection. I'll try to look up "finite state transducers". But for now I like to think about it as a "saga pattern".

Martynas Maciulevičius08:06:13

I'm trying to figure out whether there could be a framework for this (obviously probably yes but then how hard is it to have one) or whether I'll need to write all my "state machines" by hand and accumulate state on my own :thinking_face:

chrisblom19:06:04

Ah ok, lets see if a get this: You want your process to receive event, and transitions states of a state machine based on incoming events. Your question is: Is there a framework that provides a high level way to define the state machine, and handle transitions based on events I don't really know of such a framework. I have some half baked library somewhere that provides a DSL to generate finite state transducers, but it is not in a usable state. Generating a finite state machine from a DSL is not that hard BTW, could be a fun project. I'm not sure what you mean with 'they should not overlap'| The problem is kind of abstract, do you have can you give an explanation of a problem you want to solve with this approach?

Martynas Maciulevičius04:06:27

If a user locks a resource then transaction from a different user should fail until the first user unlocks. This is where a basic regex approach would lack because it would expect that the inputs would already be "separated between contexts". Probably it wouldn't be too useful to model only one state-machine but I'd have to have a large DSL for the whole system :thinking_face:

chrisblom15:06:01

Formal methods, like TLA+, come to mind, these give a high level language that is compiled down to a finite state model, which can be used to reason about properties of the program. I think code generation is also possible, but not sure how usable the results are.

chrisblom15:06:08

TLA+ is related to logic programming, it's based on temporal logic.

rickmoynihan15:06:22

@U028ART884X: sounds like you might be grasping for a dataflow system!? I’ve seen many of these in clojure over the years… here’s one: https://github.com/hoplon/javelin

Linus Ericsson12:11:37

This problem sounds like something which could be reasoned about using colored petri nets.

Martynas Maciulevičius17:11:57

http://www.goodmath.org/blog/2007/10/10/colored-petri-nets/ This article reminds me of re-frame and how it handles its state (there is actual state and then there are calculations based on that state). Although the main difference in re-frame is that it's always single-threaded. And in re-frame they call the state graph a DAG: https://en.wikipedia.org/wiki/Directed_acyclic_graph But even then it's still not easy to specify what are the transaction types and what are rollbacks because for petri nets you still have to do it by hand. Maybe it's just a hard problem overall :thinking_face: