|
| 1 | +--- |
| 2 | +icon: octicons/project-template-24 |
| 3 | +search: |
| 4 | + exclude: false |
| 5 | +tags: |
| 6 | +- engine-behaviour |
| 7 | +- engine-type |
| 8 | +- juvix |
| 9 | +--- |
| 10 | + |
| 11 | +??? quote "Juvix imports" |
| 12 | + |
| 13 | + ```juvix |
| 14 | + module node_architecture.types.engine; |
| 15 | + import prelude open; |
| 16 | + import node_architecture.types.identities open; |
| 17 | + import node_architecture.types.engine_environment open public; |
| 18 | + import node_architecture.types.engine_dynamics open public; |
| 19 | + ``` |
| 20 | + |
| 21 | +# Engine |
| 22 | + |
| 23 | +An **engine** is a computational unit with a specific name and behaviour, plus its |
| 24 | +own execution context called the engine environment, which comprises the |
| 25 | +specific state, the mailbox cluster, the acquaintances, and the timers. An |
| 26 | +engine instance is of type `Engine` instantiated with the types for the local |
| 27 | +states, the mailboxes' state, the time handles, the action-label action, and the |
| 28 | +precomputation. We use the following conventions: |
| 29 | + |
| 30 | +- to the type for the local states as `S`, |
| 31 | +- for the mailboxes' state as `M`, |
| 32 | +- for the time handles as `H`, |
| 33 | +- for the action-label as `A`, |
| 34 | +- for the precomputation as `L`, and |
| 35 | +- for the external inputs as `X`. |
| 36 | + |
| 37 | +To define the type for engine instances, we first need to define the type for |
| 38 | +engine behaviours. |
| 39 | + |
| 40 | +## Engine behaviour type |
| 41 | + |
| 42 | +The `EngineBehaviour` type encapsulates the concept of behaviours within Anoma. As |
| 43 | +defined, it clears up that engines are essentially a collection of guarded |
| 44 | +state-transition functions. |
| 45 | + |
| 46 | +```juvix |
| 47 | +type EngineBehaviour (S M H A L X : Type) := mkEngineBehaviour { |
| 48 | + guards : List (Guard S M H A L X); |
| 49 | + action : ActionFunction S M H A L X; |
| 50 | + conflictSolver : Set A -> List (Set A); |
| 51 | +}; |
| 52 | +``` |
| 53 | + |
| 54 | +!!! info "On the use of `List` for guards in `EngineBehaviour`" |
| 55 | + |
| 56 | + The `EngineBehaviour` type uses `List` for guards to enable parallel |
| 57 | + processing. This choice acknowledges that guards can be concurrent or |
| 58 | + competing, with the latter requiring priority assignment to resolve |
| 59 | + non-determinism. While guards should form a set, using `List` simplifies the |
| 60 | + implementation and provides an inherent ordering. |
| 61 | + |
| 62 | +## Engine instance type |
| 63 | + |
| 64 | +An *engine* is a term of type `Engine` instantiated with the types for the local |
| 65 | +states, the mailboxes' state, the time handles, the action-label action, and the |
| 66 | +precomputation. Each engine, not its type, is associated with: |
| 67 | + |
| 68 | +- a specific name (unique across the system), |
| 69 | +- a specific behaviour, and |
| 70 | +- a declaration of its own execution context, that is, the specific state, the |
| 71 | + mailbox cluster, the acquaintances, and the timers. |
| 72 | + |
| 73 | +```juvix |
| 74 | +type Engine (S M H A L X : Type) := mkEngine { |
| 75 | + name : EngineName; |
| 76 | + family : EngineBehaviour S M H A L X; |
| 77 | + initEnv : EngineEnvironment S M H; |
| 78 | +}; |
| 79 | +``` |
| 80 | + |
| 81 | +!!! example "Voting Engine" |
| 82 | + |
| 83 | + As an example, we could define an engine type for a voting system: |
| 84 | + |
| 85 | + - `S` could be a record with fields like `votes`, `voters`, and `results`. |
| 86 | + - The engine-specific message type might be a coproduct of `Vote` and `Result`. |
| 87 | + - The behaviour of this engine may include guarded actions like: |
| 88 | + - `storeVote` to store a vote in the local state, |
| 89 | + - `computeResult` to compute the result of the election, and |
| 90 | + - `announceResult` to send the result to some other engine instances. |
| 91 | + |
| 92 | + With each different election or kind of voters, we obtain a new engine instance, |
| 93 | + while the underlining voting system, the voting engine family, remains the same. |
| 94 | + |
| 95 | +!!! note |
| 96 | + |
| 97 | + Both the `EngineBehaviour` and `Engine` types are parameterised by several types. When |
| 98 | + not used in the context of a new engine family declaration, these types can be |
| 99 | + replaced by the unit type `Unit`. |
0 commit comments