Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there monads that can be used like an automaton?

I'm writing a stream transformer from some Input data type to an Output data type. Input is made by the user, so there is some time between the events. Because each input requires some resource loading, I'd like to "look into the future", i.e. send all possible inputs to the main computation and preload resources based on the results.

Currently, there is always exactly one output after each input but it might eventually become interesting to change this.

I succeeded to implement that with the Automaton transformer by Ross Paterson. I'm not sure my solution is optimal.

  • Are there nice examples how to do this? Perhaps even with test code?
  • Can it be achieved with a monad as well? (Examples?, Explanation why it's impossible?)

Edit: After the call for more specifics, I added code here. Now I'm removing it (it was not understandable) and add some other explanation. My question is answered thaugh.

My intention was to have the main event loop stop after each user input that has been fed to the arrow/stream transformer/whatever. Then it would store the current automaton state and send all possible inputs (fake events) one by one to the automaton and see what resources must be loaded, to cache them. After the next real event, it would use the cache for better responsiveness. The main computation should not be influenced by this.

like image 770
Duschvorhang Avatar asked Dec 07 '11 17:12

Duschvorhang


1 Answers

All the use cases you mentioned are covered by the Netwire library. It provides a generalization of Ross' automaton arrow to a family of wire arrows. I haven't finished the wiki page yet, but it should give you enough to start.

Combining this with Kleisli (LogicT m) for some monad m you get nondeterministic wires.

And as an additional note: What you want is not a monad.

like image 128
ertes Avatar answered Oct 05 '22 22:10

ertes