Skip to content

Instantly share code, notes, and snippets.

@ahammel
Last active August 29, 2015 14:10
Show Gist options
  • Save ahammel/69f0bd21abb8a926a581 to your computer and use it in GitHub Desktop.
Save ahammel/69f0bd21abb8a926a581 to your computer and use it in GitHub Desktop.
{-# 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 "⇔"
}
@thaiviet1994
Copy link

Creating a “truth table” is not hard, you can use an useful tool (CKod, at http://ckod.sourceforge.net/_/) to make a “truth table”.

  1. CKod homepage: http://ckod.sourceforge.net/
  2. CKod online: http://ckod.sourceforge.net/_/
  3. CKod forum: http://ckod.sourceforge.net/~/

Good luck to you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment