Zuletzt geändert: So, 23.07.2006

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



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

zero  f = id
one   f = f
two   f = f . f
three f = f . f . f

csucc n = \f -> n f . f

type ChurchN0 a = ((a -> a) -> a -> a)

instance (Num a, Integral a) => Integral (ChurchN0 a) where
  toInteger n = toInteger $ n (+1) $ 0

instance (Integral a) => Eq (ChurchN0 a) where
  n == m = toInteger n == toInteger m

instance (Integral a) => Show (ChurchN0 a) where
  show n = "N0[" ++ show (toInteger n) ++ "]"

instance (Integral a, Num a) => Num (ChurchN0 a) where
  n + m = \f x -> m f (n f x)
  n * m = \f -> m (n f)

  abs = id

  signum n | n == zero = 0
  signum n | otherwise = 1

  fromInteger 0 = zero
  fromInteger n = csucc $ fromInteger (n - 1)

instance (Num a, Integral a) => Enum (ChurchN0 a) where
  succ = csucc
  pred = undefined

  toEnum   = fromInteger  . fromIntegral
  fromEnum = fromIntegral . toInteger

instance (Integral a, Num a) => Real (ChurchN0 a) where
  toRational = toRational . toInteger

instance (Integral a) => Ord (ChurchN0 a) where
  (<=) = undefined