- Data (
examples/api.kt): Commands, Events, State - Behaviour (
examples/domain.kt): Exhaustive pattern matching on data
This project encodes information systems as composable algebraic structures using pure Kotlin functions.
It unifies two classical architectural styles:
- ๐๏ธ State-stored systems โ store current state directly
- ๐ Event-sourced systems โ store past events and reconstruct state from them
Both are captured under a single algebraic model โ the System type.
decide : Command ร State โ Sequence<Event>
evolve : State ร Event โ State
initialState : () โ State
data class System<Command, State, Event>(
val decide: (Command, State) -> Sequence<Event>,
val evolve: (State, Event) -> State,
val initialState: () -> State
)
Convert between representations easily:
val system: System<C, S, E> = ...
val stateStored: StateStoredSystem<C, S> = system.asStateStoredSystem()
val eventSourced: EventSourcedSystem<C, E> = system.asEventSourcedSystem()
| Concept | Mathematical Structure | Notes |
|---|---|---|
mapCommand |
Contravariant Functor | Maps input command types |
mapEvent |
Profunctor (dimap) | Contravariant in input events, covariant in output |
mapState |
Profunctor (dimap) | Contravariant in input state, covariant in output |
combine |
Monoidal Product | Combines two systems into a product system |
emptySystem |
Monoidal Identity | Represents the โno-opโ system (System<Nothing?, Unit, Nothing?>) |

