In computing, an effect system is a formal system that describes the computational effects of computer programs, such as side effects. An effect system can be used to provide a compile-time check of the possible effects of the program.
The effect system extends the notion of type to have an "effect" component, which comprises an effect kind and a region. The effect kind describes what is being done, and the region describes with what (parameters) it is being done.
An effect system is typically an extension of a type system. The term "type and effect system" is sometimes used in this case. Often, a type of a value is denoted together with its effect as type ! effect, where both the type component and the effect component mention certain regions (for example, a type of a mutable memory cell is parameterized by the label of the memory region in which the cell resides). The term "algebraic effect" follows from the type system.
Effect systems may be used to prove the external purity of certain internally impure definitions: for example, if a function internally allocates and modifies a region of memory, but the function's type does not mention the region, then the corresponding effect may be erased from the function's effect.[1]
Examples
[edit]Some examples of the behaviors that can be described by effect systems include:
- Reading, writing or allocating memory: the effect kind is read, write, allocate or free, and the region is the point of the program where allocation was performed (i.e., each program point where allocation is performed is assigned a unique label, and region information is statically propagated along the dataflow). Most functions working with memory will actually be polymorphic in the region variable: for example, a function that swaps two locations in memory will have type
forall r1 r2, unit ! {read r1, read r2, write r1, write r2}
. - Working with resources, such as files: for example, the effect kind may be open, read and close, and again, the region is the point of the program where the resource is opened.
- Control transfers with continuations and long jumps: the effect kind may be goto (i.e. the piece of code may perform a jump) and comefrom (i.e. the piece of code may be the target of a jump), and the region denotes the point of the program from which or to which the jump may be performed.
From a programmer's point of view, effects are useful as it allows for separating the implementation (how) of specific actions from the specification of what actions to perform. For example, an ask name effect can read from either the console, pop a window, or just return a default value. The control flow can be described as a blend of yield (in that the execution continues) and throw (in that an unhandled effect propagates down until handled).[2]
Implementations
[edit]Core Feature
[edit]- Koka is a statically typed functional programming language with algebraic effect handlers as a main feature.[3]
- Eff is a statically typed functional programming language centered around algebraic effect handlers.[4]
- Unison is a statically typed functional programming language with algebraic effect handlers (called "abilities" in the language) as a core part of the type system.[5]
- Effekt is a research language centered around effect handlers and polymorphic effects.[6]
Full Support
[edit]- Haskell is a statically typed functional programming language with several packages that allow for encoding of effects.[7] However, Haskell is generally more focused on monads.
- OCaml 5.0 introduced support for experimental effect handler primitives,[8] with plans to introduce a proper high-level syntax in the future.
Partial Support and Prototypes
[edit]- Scala 3.1 is a statically typed, functional and object oriented programming language with experimental support for effects that is limited to exceptions, in the form of a
CanThrow
capability.[9] - Java is a statically typed, object oriented programming language; its checked exceptions are a relatively limited example of an effect system. Only one effect kind —
throws
— is available, there is no way to resume with a value, and they cannot be used with functions (only methods) unless the function implements a custom@FunctionalInterface
.[10] - JavaScript is a dynamically typed language, it has a proposal that implements algebraic effects.[11]
References
[edit]- ^ Albin., Turbak, Franklyn (2010). Design concepts in programming languages. PHI Learning. ISBN 978-81-203-3996-5. OCLC 1261053520.
{{cite book}}
: CS1 maint: multiple names: authors list (link) - ^ Abramov, Dan. "Algebraic Effects for the Rest of Us". overreacted.io.
- ^ "The Koka Manual". koka-lang.github.io.
- ^ Pretnar, Matija (2021-12-07), Eff, retrieved 2021-12-11
- ^ "The Unison language". www.unisonweb.org. Retrieved 2021-12-07.
- ^ The Effekt research team. "Effekt Language: Concepts and Features". Effekt Language. Retrieved 2023-06-13.
- ^ Vera, Josh (18 April 2020). "joshvera/freemonad-benchmark". GitHub.
A benchmark comparing the performance of different free monad implementations.
- ^ "OCaml - Language extensions". v2.ocaml.org. Retrieved 2023-06-13.
- ^ "CanThrow Abilities". Scala Documentation. Retrieved 2021-12-07.
- ^ "Java 8 Lambda function that throws exception?". Stack Overflow. Retrieved 2021-12-25.
- ^ Macabeus, Bruno (16 September 2020). "macabeus/js-proposal-algebraic-effects: 📐Let there be algebraic effects in JS". GitHub.
Textbook chapters
[edit]- Hankin, Chris; Nielson, Flemming; Nielson, Hanne Riis (1999). Principles of Program Analysis. Berlin: Springer. ISBN 978-3-540-65410-0.
- Gifford, David; Turbak, Franklyn A.; Sheldon, Mark A. (2008). "16". Design Concepts in Programming Languages. Cambridge, Mass: MIT Press. ISBN 978-0-262-20175-9.
Overview papers
[edit]- Nielson, Flemming; Nielson, Hanne Riis (1999). "Type and Effect Systems". Correct System Design: Recent Insight and Advances. Lecture Notes in Computer Science. 1710. Springer-Verlag: 114–136. doi:10.1007/3-540-48092-7_6. ISBN 978-3-540-66624-0.
Further reading
[edit]- Marino, Daniel; Millstein, Todd (2009). "A generic type-and-effect system". Proceedings of the 4th international workshop on Types in language design and implementation (PDF). ACM. p. 39. CiteSeerX 10.1.1.157.8373. doi:10.1145/1481861.1481868. ISBN 9781605584201. S2CID 14538045.
- Lucassen, John M.; Gifford, David K. (1988). "Polymorphic effect systems". Proceedings of the 15th ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '88. ACM. pp. 47–57. CiteSeerX 10.1.1.73.4916. doi:10.1145/73560.73564. ISBN 978-0897912525. S2CID 13015611.