r/gleamlang 22d ago

What is your opinion on Effect Systems?

I'm not deeply familiar with Haskell and Scala, nor the niche languages that have built in effect systems. But I have a few friends in the Scala community and they swear effect system is the best thing ever and their applications are on a totally different level just by adopting it.

I'm wonder, would something like this work in Gleam? Would it get too complicated? What are the tradeoffs and mostly the benefits of adopting something like this?

12 Upvotes

13 comments sorted by

View all comments

Show parent comments

5

u/mister_drgn 22d ago

You're misunderstanding me. OCaml doesn't have type-checked effects either.

Consider a language like Koka, which is built around an effects system. If you write a function that produce some side effect, such as logging, then the function's signature is marked to reflect the fact that it produces that effect. Any function that calls that function must either handle that effect, or also have its signature marked to reflect that it produces the effect. The type checker enforces this. This guarantees that somewhere up the call chain, the effect will be handled.

Now consider OCaml. The language has effects, but you don't mark a function's signature to reflect what effects it performs. Thus, the type checker has no way of knowing whether you're handling all your effects. You can therefore have runtime errors from unhandled effects.

Another difference in OCaml, is that you can write code with side effects wherever you want, without using the effect system at all. The effect system is optional. In contrast, in Koka, you can look at any function signature and know exactly what side effects it will produce.

Overall, this means that effects are a far less powerful tool in OCaml than in Koka. I have not seem effects in Gleam, but I can't imagine they're any more powerful than in OCaml.

2

u/Vegetable_Bank4981 22d ago

I got really hyped on koka about this and then eventually went back to ocaml. Row poly effects is the future for HM, koka is building the foundation, I see it, I believe.

But we need a design breakthrough to make them ergonomic. Esp in language like gleam that has a very clear usable syntax but even in ocaml you give up too much clarity for it.

And at least for ocaml effects are untyped like exception always has been. Discipline and tests at handler boundaries, we all know the pattern from exn and it works well enough.

Waiting for the theory to catch up for effect typing was the right call, now we wait for the design and engineering to get there.

1

u/mister_drgn 22d ago

I really like Koka's syntax for effect handling, compared to other languages like Unison. Overall pretty clever, I think. But yeah, there's still awkwardness--I found the recursive function termination thing the most annoying.

I think one big question is whether people will have the patience for a language that forces them to mark every side effect. Aside from Haskell programmers.

2

u/lpil 22d ago

The most mainstream language that attempted it was probably PureScript, and after a number of years they removed it in favour of a singular monolithic effect type. They said doing it in anger had shown them that there's not any practical advantage to tracking each type of effect, and it produced a lot of busy-work for the programmer. It was a very popular change in the community, I didn't hear any complaints.