How to compose `not` with a function of arbitrary arity?

Actually, doing arbitrary arity with type classes turns out to be incredibly easy:

module Pred where

class Predicate a where
  complement :: a -> a

instance Predicate Bool where
  complement = not

instance (Predicate b) => Predicate (a -> b) where
  complement f = \a -> complement (f a)  
  -- if you want to be mysterious, then
  -- complement = (complement .)
  -- also works

ge :: Ord a => a -> a -> Bool
ge = complement (<)

Thanks for pointing out this cool problem. I love Haskell.

Leave a Comment