In [1]:
{-# LANGUAGE RebindableSyntax #-}
import ClassyPrelude()
import qualified ClassyPrelude as P
class Boolable bool where
toBool :: bool -> Bool
instance Boolable Int where
toBool 0 = False
toBool _ = True
instance Boolable Integer where
toBool 0 = False
toBool _ = True
instance Boolable Float where
toBool 0 = False
toBool _ = True
instance Boolable Double where
toBool 0 = False
toBool _ = True
instance Boolable (Maybe a) where
toBool Nothing = False
toBool _ = True
instance Boolable [a] where
toBool [] = False
toBool _ = True
-- instance (Num a, Eq a) => Boolable a where
-- toBool num = negate num == num
ifThenElse :: Boolable bool => bool -> a -> a -> a
ifThenElse boolable true false = case toBool boolable of
True -> true
False -> false
In [3]:
if 0 then undefined else "falsy"
if 2 then "truthy" else undefined
if [] then undefined else "truthy"
In [ ]: