this post was submitted on 13 Sep 2024
59 points (86.4% liked)

Programming

17418 readers
235 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] baseless_discourse@mander.xyz 4 points 2 months ago* (last edited 2 months ago)

In Maybe monadic, its monadic bind will automatically resolves any failed computation, and don't need explicit checking.

for example, the code in Haskell looks something like the following:

fib: Int -> Int -> Maybe Int
fib max_depth idx =
  do
     guard (0 <= max_depth)
     n1 <- fib (max_depth - 1) (idx - 1)
     n2 <- fib (max_depth - 1) (idx - 2)
     return (n1 + n2)

Haskell type class system automatically figures out this is a maybe monad, and check for error accordingly.

Notice, unlike the C code the author provide, this haskell code will exit immediately when n1 failed and never compute n2, similar to the behavior of the exception code. Thus I believe his point about performance is at least unjustified, if not wrong.

Another interesting fact about this code is that there is nothing that is built into the compiler/interpretor (except the do expression, which is just a minor syntactical sugar). For this code, the compiler designers don't need to design special semantics for raise and catch. Everything here, guard, return, and the Maybe monad (which is in charge of propagating errors) is defined by the user, using normal functions, no metaprogramming involved.

Wouldn't effect systems still be considered exceptions, but handled differently?

Yes, unlike monad, the error in algebraic effect is propagated by the compiler/interpretor, instead of user defined. But unlike implicit effect, explicit effect (algebraic effect, throwable, etc.) makes it clear how the code can go wrong.

Although explicit error through monad or algebraic effect is more clear in general, there are special cases where explicit effect is undesirable. One such example is effect pollution: low-level effects that are unlikely to cause impure behaviors are unnecessarily propagated through the call stack. This problem can make the code more verbose and difficult to handle.