Last active
August 29, 2015 14:10
-
-
Save ahammel/69f0bd21abb8a926a581 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
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} | |
module Main where | |
import Control.Applicative | |
import Text.Printf | |
import Prelude hiding (and, or, flip) | |
True `nand` True = False | |
True `nand` False = True | |
False `nand` True = True | |
False `nand` False = True | |
{- Connectives -} | |
and = (dup nand .) . nand | |
or = nand `on` dup nand | |
xor = over ((dup nand .) . nand) (nand `on` dup nand) nand | |
nor = ((dup nand .) . nand) `on` dup nand | |
implication = flip $ nand . dup nand | |
converseImplication = nand . dup nand | |
nonimplication = flip $ ((dup nand .) . nand) . dup nand | |
converseNonimplication = ((dup nand .) . nand) . dup nand | |
biconditional = over nand (nand `on` dup nand) nand | |
{- Combinators -} | |
dup f x = f x x | |
flip f x y = f y x -- From Prelude | |
on f g x y = f (g x) (g y) -- From Data.Function | |
over f g h x y = f (g x y) (h x y) | |
truthTable :: (Bool -> Bool -> Bool) -> String -> String | |
truthTable f name = unlines $ displayRow <$> [True, False] <*> [True, False] | |
where | |
displayRow p q | |
= printf "%-5s %s %-5s : %s" (show p) name (show q) (show $ f p q) | |
main :: IO () | |
main = do { putStrLn $ truthTable and "∧" | |
; putStrLn $ truthTable or "∨" | |
; putStrLn $ truthTable xor "⊕" | |
; putStrLn $ truthTable nand "↑" | |
; putStrLn $ truthTable nor "↓" | |
; putStrLn $ truthTable implication "→" | |
; putStrLn $ truthTable converseImplication "←" | |
; putStrLn $ truthTable nonimplication "↛" | |
; putStrLn $ truthTable converseNonimplication "↚" | |
; putStrLn $ truthTable biconditional "⇔" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Creating a “truth table” is not hard, you can use an useful tool (CKod, at http://ckod.sourceforge.net/_/) to make a “truth table”.
Good luck to you!