Zuletzt geändert: So, 23.07.2006

«K12/K13» EqClass.hs «PDF», «POD»



Download
{-# OPTIONS_GHC -fglasgow-exts #-}
module EqClass where

data EqClass a = (Eq a) => EqClass a

instance (Show a) => Show (EqClass a) where
  show (EqClass x) = "[" ++ show x ++ "]"

instance (Eq a) => Eq (EqClass a) where
  (EqClass x) == (EqClass y) = x == y

instance (Num a) => Num (EqClass a) where
  (+) = op2 (+)
  (-) = op2 (-)
  (*) = op2 (*)

  abs    = op1 abs
  signum = op1 signum

  fromInteger = EqClass . fromInteger

instance (Integral a) => Integral (EqClass a) where
  div = op2 div
  toInteger (EqClass x) = toInteger x

instance (Fractional a) => Fractional (EqClass a) where
  (/) = op2 (/)
  fromRational = EqClass . fromRational

instance (Ord a) => Ord (EqClass a) where
  (EqClass x) <= (EqClass y) = x <= y

instance (Eq a, Enum a) => Enum (EqClass a) where
  succ = op1 succ
  pred = op1 pred

  toEnum = EqClass . toEnum
  fromEnum (EqClass x) = fromEnum x

instance (Real a) => Real (EqClass a) where
  toRational (EqClass x) = toRational x

op1 :: (Eq a) => (a -> a) -> EqClass a -> EqClass a
op1 f (EqClass x) = EqClass $ f x

op2 :: (Eq a) => (a -> a -> a) -> EqClass a -> EqClass a -> EqClass a
op2 f (EqClass x) (EqClass y) = EqClass $ f x y