r/ocaml • u/TomosLeggett • Aug 26 '26
Why can't we have side effect annotations?
One of the things I like about OCaml is that it doesn't force you to write purely functional code. You can mutate values, throw exceptions, perform I/O, and generally use imperative features when they're appropriate.
I also like that OCaml is fairly explicit about this. If you're passing a reference around or mutating a value, it's usually apparent from the code.
One thing I don't particularly like, though, is that there's no indication in a function's type of whether it performs side effects. There's no equivalent of a ! suffix or an effect annotation, so you can call a function without knowing that it might throw an exception, mutate some state, perform I/O, etc.
The only real way to find this out is to inspect the implementation or rely on the documentation, assuming the author has actually documented it.
I'm wondering whether this is primarily a technical limitation or simply a design decision by the OCaml team. What prevents the compiler from analysing a function's body and determining that it uses things such as raise, :=, ref, I/O operations, and other effectful constructs, then incorporating that information into its type? For example:
Obviously, there are complications around higher-order functions, modules, abstractions, user-defined effects, and so on, but could some form of effect inference make it possible to distinguish pure functions from effectful ones?
I'd be interested to know whether this has been considered for OCaml, and whether there are fundamental reasons why it doesn't work well, or whether it's simply a deliberate trade-off in the language's design.
10
u/Vegetable_Bank4981 Aug 26 '26 edited Aug 26 '26
Effect typing is at the frontier of type systems research these days.
There are a few known ways to do it but they all have big drawbacks. Mostly it just doesn’t work well with the HM inference as is so you either need to annotate every effect or give up on all type inference across function boundaries. Both of which aren’t acceptable for ocaml.
Adapting HM to infer effect types without changes to module level inference is an open CS problem, there’s a phd in it if you’ve got an idea and a few years. Making it fast enough to use in a production compiler would be a second one.
Haskell does it with monads but it needs a runtime built for that, with pervasive lazy evaluation, also not gonna work for us.
Koka has shown how to do it with row polymorphism, which is probably where ocaml is headed. OCaml already has row polymorphism for objects so you can get a sense of it if you want. But it ends up being an axis on the type (actually on the arrow if that makes sense) of every function. Async is a kind of effect so if you’ve run into “function coloring” in terms of async rust or js it’s the same thing but with a “color” for every effect.
So the theory is there! But there is a ton of design work to get it into ocaml in a way that doesn’t break what’s good about ocaml. And then a lot of engineering to make inference performant which I’m not qualified to get into but I understand has some open questions of its own.
2
u/mister_drgn Aug 26 '26
OCaml has algebraic effects, which in several new, experimental languages (Koka, Unison, etc) do exactly what you're asking for. A function signature has to indicate when it produces unhandled effects, which include side effects. It's very cool, but also a lot more cumbersome for the coder.
Unlike in those languages, the algebraic effect implementation in OCaml doesn't touch the type system. There's no way to know whether a function produces such effects (I mean, no way from looking at the function's signature). And OCaml's existing functions that produce side effects aren't tied to the algebraic effect system at all. I think that's kind of a shame, but to be honest there's no way you could force all that on people. At the barest minimum, you'd need to release a new version of the language, while continuing to support the old version.
5
u/BluddyCurry Aug 26 '26
This stuff quickly results in heavy mental and syntactic overhead. You start out with an idea that seems simple but then the annotations and complications multiply very quickly. The art is in finding a compromise that doesn't make the language too cumbersome to use, and is still backwards compatible and compatible with all the other features OCaml already has.
5
u/kitaz0s_ Aug 26 '26
very new to OCaml, but I believe OxCaml (Jane Street's fork) kinda approaches this through modalities rather than traditional effect rows (which is what you described in your example)
You can't really tag a function with arbitrary effects, but there's a built-in set of modal axes that you can use (e.g. tracking whether a closure is stateless vs stateful, or whether a reference is @ local vs @ global).
So your example might look more like:
2
u/spermBankBoi Aug 26 '26
OP (and commenters) might find [this paper](https://dl.acm.org/doi/pdf/10.1145/3371116) interesting; I did at least. imo this approach is more sensible than a lot of the more popular ones (Eff, Koka) and still not all that verbose (thanks to implicit effect parameters)
1
u/Tomus Aug 26 '26
Drive by non ocaml user here but I believe this is what algebraic effects give you right?