Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@friedbrice
Last active May 9, 2019 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friedbrice/7f3b18f5f13e725d3d75a69316521b4d to your computer and use it in GitHub Desktop.
Save friedbrice/7f3b18f5f13e725d3d75a69316521b4d to your computer and use it in GitHub Desktop.
{-# LANGUAGE DerivingVia, TypeApplications #-}
import Prelude hiding ((&&), (||), not)
import qualified Prelude ((&&), (||), not)
class BooleanAlgebra a where
(&&) :: a -> a -> a
(||) :: a -> a -> a
not :: a -> a
true :: a
false :: a
instance BooleanAlgebra Bool where
(&&) = (Prelude.&&)
(||) = (Prelude.||)
not = Prelude.not
true = True
false = False
newtype BooleanRing a = BooleanRing a
deriving (Read, Show, Eq, Ord, Bounded)
deriving (Enum, BooleanAlgebra) via a
-- Every boolean algebra is a ring in a canonical way.
instance BooleanAlgebra a => Num (BooleanRing a) where
x + y = (x || y) && not (x && y)
x * y = x && y
negate = id
abs = id
signum = const 1
fromInteger n = case n `mod` 2 of
0 -> false
1 -> true
type Z2 = BooleanRing Bool
main :: IO ()
main = do
putStrLn "> 42 * true == 42"
print (42 * true @Z2 == 42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment