Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
data PosInf a = Infinity | Finite a deriving (Show, Read, Eq)
instance Ord a => Ord (PosInf a) where
compare Infinity Infinity = EQ
compare Infinity Finite{} = GT
compare Finite{} Infinity = LT
compare (Finite a) (Finite b) = compare a b
instance Num a => Num (PosInf a) where
Finite x + Finite y = Finite (x + y)
_ + _ = Infinity
some_constant = 3
give_it_a_try :: (Num a, Eq a) => PosInf a -> PosInf a -> PosInf a
give_it_a_try a b
| a == Infinity || b == Infinity = Infinity
| otherwise = a + b + Finite some_constant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment