{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE MultiParamTypeClasses #-} module Cast where -- | Type class describing the relationship. class Cast a b where cast :: a -> b instance Cast Int Bool where cast 0 = False cast _ = True instance Cast Bool Int where cast False = 0 cast True = 1 instance Cast Int Int where cast = id instance Cast Bool Bool where cast = id instance Cast Integer Int where cast = fromInteger instance Cast Integer Bool where cast 0 = False cast _ = True instance Cast Int Integer where cast = fromIntegral instance Cast Bool Integer where cast False = 0 cast True = 1 instance Cast Bool Float where cast False = 0.0 cast True = 1.0 instance Cast Int Float where cast = fromIntegral -- | "Python-like" addition, with implicit type-based conversion. pyPlus :: (Cast a Int, Cast b Int) => a -> b -> Int pyPlus a b = cast a + cast b -- | "Python-like" conjunction, with implicit type-based conversion. pyAnd :: (Cast a Bool, Cast b Bool) => a -> b -> Bool pyAnd a b = cast a && cast b