Why Algebraic Effects?
a year ago
- #programming
- #functional-programming
- #language-design
- Algebraic effects (effect handlers) are a powerful feature gaining popularity in programming languages, allowing for resumeable exceptions.
- They enable user-definable control flow, implementing features like generators, exceptions, async, and coroutines as libraries.
- Effects compose well, making functions polymorphic over effects, such as a `map` function that can handle any effect.
- Effects can be used for dependency injection, simplifying testing and swapping implementations (e.g., databases, logging).
- They clean up APIs by automatically threading context (e.g., state management) without manual passing.
- Effects substitute globals (e.g., random number generation, memory allocation) while avoiding global downsides.
- They allow writing in a direct style, avoiding cumbersome error handling with unions or optionals.
- Effects guarantee purity by marking side-effects (e.g., `IO`, `Print`), aiding in concurrency and security.
- Replayability is possible by recording and replaying effects, useful for debugging and deterministic execution.
- Capability-based security is enhanced, as effects must be explicitly declared, preventing sneaky side-effects.