Skip to content

Instantly share code, notes, and snippets.

@tmhedberg
Created December 29, 2011 18:17
Show Gist options
  • Select an option

  • Save tmhedberg/1535396 to your computer and use it in GitHub Desktop.

Select an option

Save tmhedberg/1535396 to your computer and use it in GitHub Desktop.
Church Booleans in Haskell
{-# LANGUAGE FlexibleInstances, Rank2Types, TypeFamilies #-}
module ChurchBool where
type CB = forall a. a -> a -> a
instance a ~ Bool => Show (a -> a -> a) where
show cb = if cb True False then "cTrue" else "cFalse"
cTrue, cFalse :: CB
cTrue a _ = a
cFalse _ b = b
cNot :: CB -> CB
cNot m a b = m b a
cAnd :: CB -> CB -> CB
cAnd a b = a b a
cNand :: CB -> CB -> CB
cNand a b x y = a b a y x
cOr :: CB -> CB -> CB
cOr a b = a a b
cNor :: CB -> CB -> CB
cNor a b x y = a a b y x
cXor :: CB -> CB -> CB
cXor a b x y = a (b y x) (b x y)
cXnor :: CB -> CB -> CB
cXnor a b x y = a (b x y) (b y x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment