3.1. automata programming[Automata theory](
https://en.wikipedia.org/wiki/Automata_theory#Classes_of_automata) is the study of abstract machines and automata, as well as the computational problems that can be solved using them. It is a theory in theoretical computer science. The word automata (the plural of automaton) comes from the Greek word automatos, which means "self-acting, self-willed, self-moving". An automaton (Automata in plural) is an abstract self-propelled computing device which follows a predetermined sequence of operations automatically.
There exists a whole variety of more or less general classes of automata. Being Turing complete, *Intermezzo* is capable of emulating any automata, and here we bring an example of input/output stateless automation. The property of being stateless means that the automation doesn't remember any state on each call to the automation. This means that if we want to deal with states, we have to pass the states with an input, and return the modified states with an output each time we run the automation. This way, the states are evolving on each cycle of running the automation until we reach a state of ending the automation operation.
The following example represents a number guessing game, and bases its operation on repetitive cycles, passing through operational states on each input/output cycle. On the first cycle, the example in output asks a user to imagine a number between 1 and 8, while the automation starts guessing what is imagined number. Each cycle is comprised of a binary search, asking if the imagined number is less than, greater than, or equal to guessed number. Finally, in four or less steps, the automation declares its victory when the user admits that the guessed number is equal to imagined number.
(
COMPOSITE
(
INPUT
(
ELEMENTARY
TOP
<{"input": "start"}>
)
(
ELEMENTARY
TOP
<{"input": "less", "state": {"guess": "<Num>", "delta": "<Num>", "step": "<Num>"}}>
)
(
ELEMENTARY
TOP
<{"input": "more", "state": {"guess": "<Num>", "delta": "<Num>", "step": "<Num>"}}>
)
(
ELEMENTARY
TOP
<{"input": "equal", "state": {"guess": "<Num>", "delta": "<Num>", "step": "<Num>"}}>
)
(ELEMENTARY <Num> <1>)
(ELEMENTARY <Num> <2>)
(ELEMENTARY <Num> <3>)
(ELEMENTARY <Num> <4>)
(ELEMENTARY <Num> <5>)
(ELEMENTARY <Num> <6>)
(ELEMENTARY <Num> <7>)
(ELEMENTARY <Num> <8>)
)
(
CHAIN
// init
(
ELEMENTARY
<{"input": "start"}>
<{"output": "Imagine a number between 1 and 8. Is it 8?", "state": {"guess": "8", "delta": "8", "step": "1"}}>
)
// less
(
EQUALIZE
(ID (DOMAIN <Guess> <Num>) (DOMAIN <Delta> <Num>) (DOMAIN <Step> <Num>))
(
ELEMENTARY
<{"input": "less", "state": {"guess": "<Guess>", "delta": "<Delta>", "step": "<Step>"}}>
<{"output": "Is it <Guess> - <Delta> / 2?", "state": {"guess": "<Guess> - <Delta> / 2", "delta": "<Delta> / 2", "step": "<Step> + 1"}}>
)
// more
(
EQUALIZE
(ID (DOMAIN <Guess> <Num>) (DOMAIN <Delta> <Num>) (DOMAIN <Step> <Num>))
(
ELEMENTARY
<{"input": "more", "state": {"guess": "<Guess>", "delta": "<Delta>", "step": "<Step>"}}>
<{"output": "Is it <Guess> + <Delta> / 2?", "state": {"guess": "<Guess> + <Delta> / 2", "delta": "<Delta> / 2", "step": "<Step> + 1"}}>
)
// equal
(
EQUALIZE
(ID (DOMAIN <Step> <Num>))
(
ELEMENTARY
<{"input": "equal", "state": {"guess": "<Num>", "delta": "<Num>", "step": "<Step>"}}>
<{"output": "Got ya in <Step> steps!"}>
)
)
)
(
OUTPUT
(ELEMENTARY <1> <Num>)
(ELEMENTARY <2> <Num>)
(ELEMENTARY <3> <Num>)
(ELEMENTARY <4> <Num>)
(ELEMENTARY <5> <Num>)
(ELEMENTARY <6> <Num>)
(ELEMENTARY <7> <Num>)
(ELEMENTARY <8> <Num>)
(
ELEMENTARY
<{"output": "Imagine a number between <Num> and <Num>. Is it <Num>?", "state": {"guess": "<Num>", "delta": "<Num>", "step": "<Num>"}>
BOT
)
(
ELEMENTARY
<{"output": "Is it <Num> - <Num> / 2?", "state": {"guess": "<Num>", "delta": "<Num>", "step": "<Num>"}>
BOT
)
(
ELEMENTARY
<{"output": "Is it <Num> + <Num> / 2?", "state": {"guess": "<Num>", "delta": "<Num>", "step": "<Num>"}>
BOT
)
(
ELEMENTARY
<{"output": "Got ya in <Num> steps!"}>
BOT
)
)
)
The input/output process of guessing is using [JSON](
https://en.wikipedia.org/wiki/JSON) format to exchange data between operation cycles. It is expected from an outer resource to calculate math expressions in JSON data prior to sending it to the next cycle (it is possible to implement these calculations as *Intermezzo* rules, but for a sake of simplicity of the example, we choose to leave out these definitions). Communicating between cycles, regarding to previous `output` question, we store to `input` property an arbitrary choice of `less`, `more`, or `equal` keywords, while `state` property from the previous output is copied to `state` property of the next input.