|
|
|
|
|
by ColonelPhantom
693 days ago
|
|
By simulating exceptions, do you mean a `Result t e` type (which Haskell calls `Either l r`)? You can use these and the Functor/Applicative/Monad hierarchy to handle errors. What is presumably talked about is that Haskell also has actual exceptions, generated by calling e.g. `error` or `undefined`. The semantics of these are.. interesting, mostly thanks to lazy evaluation. For example, `fst (5, error "second") ` is safe to evaluate because the second half of the tuple is a thunk and does not get evaluated. Additionally, there is, to my knowledge, no way to handle exceptions in pure code, presumably due to the undefined evaluation order. That said, I'm not sure what the alternative would be, because a function like !! (list indexing) can fail and dealing with its fallibility would be a big burden on the programmer. |
|
Yes, exactly.
> The semantics of these are.. interesting, mostly thanks to lazy evaluation. For example, `fst (5, error "second") ` is safe to evaluate because the second half of the tuple is a thunk and does not get evaluated
Correct, and the semantics of loops is also.. interesting. For example `fst (5, last [1..])` is also safe to evaluate.
> What is presumably talked about is that Haskell also has actual exceptions, generated by calling e.g. `error` or `undefined`.
Well, I'm not sure, that's why I asked. I'm trying to understand what astrange meant by "it has exceptions which are a bad language feature, and typed throws which are a worse one". (Throwing exceptions from pure code should be left to such cases, that are impossible to recover from, in my opinion.)
> there is, to my knowledge, no way to handle exceptions in pure code, presumably due to the undefined evaluation order
Correct
> That said, I'm not sure what the alternative would be, because a function like !! (list indexing) can fail and dealing with its fallibility would be a big burden on the programmer.
Indeed. Even more so, what is one supposed to do when an invariant has been violated due to a programming error and there's no way to make progress? Haskell's exceptions are essential. It's even better when they're used in a well typed, well scoped manner, such as provided by my effect library Bluefin
https://hackage.haskell.org/package/bluefin-0.0.6.0/docs/Blu...
That's why I wanted to understand more about what astrange meant. It doesn't match my understanding!