Last active
May 9, 2019 23:55
-
-
Save friedbrice/7f3b18f5f13e725d3d75a69316521b4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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