[Haskell-cafe] Bool is not...safe?!

Branimir Maksimovic branimir.maksimovic at gmail.com
Thu Jul 5 20:32:13 UTC 2018



On 07/05/2018 10:26 PM, Olaf Klinke wrote:
> Indeed, why use Bool when you can have your own algebraic datatype? Why not
>
> data Equality = Equal | NotEqual
> (==) :: Eq a => a -> a -> Equality
>
> when we have essentially the same for 'compare' already? Why not use
>
> data Result = Success | Failure
>
> as the return type of effectful code? Equality and Result are Booleans with some provenance.
> But wait! Equality does not come with all the nice functions like (||) and (&&), and Failure still does not tell us what exactly went wrong. Maybe a derivable type class IsomorphicToBool could remedy the former problem. For the latter, Haskell has exception monads. One of the things I love about Haskell is that I do not have to use Int as a return type and remember that negative numbers mean failure of some sort, or worse, the return type is Void but some global variable may now contain the error code of the last function call. In some post in haskell-cafe which I am unable to find right now it was mentioned that the GHC source contains many types that are isomorphic but not equal to Bool.
> Does anyone know whether the C and C++ folks are now using more informative structs as return types?

Herb Sutter made proposal recently to turn exceptions into ADT's 
(internally).
C++ has std::optional and preferred way to return errors is via 
discriminated unions. I think that main reason
for this is that exceptions are non-deterministic.

Greets, Branimir.


More information about the Haskell-Cafe mailing list