Created
May 24, 2021 10:56
-
-
Save artemohanjanyan/5ad16010cb33474d8d35d057b1f618da to your computer and use it in GitHub Desktop.
Function which prints truth table for any boolean function in Idris
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
first : (a -> b) -> (a, c) -> (b, c) | |
first f (x, y) = (f x, y) | |
interface BooleanFunction a where | |
makeTable : a -> List (List Bool, Bool) | |
BooleanFunction Bool where | |
makeTable result = [([], result)] | |
BooleanFunction a => BooleanFunction (Bool -> a) where | |
makeTable f = do | |
arg <- [True, False] | |
restTable <- makeTable (f arg) | |
pure $ first (arg::) restTable | |
BooleanFunction a => BooleanFunction (Lazy Bool -> a) where | |
makeTable f = do | |
arg <- [True, False] | |
restTable <- makeTable (f arg) | |
pure $ first (arg::) restTable | |
printTruthTable : BooleanFunction a => a -> IO () | |
printTruthTable f = do | |
let table = makeTable f | |
for_ table printRow | |
where | |
boolToAlignedStr : Bool -> String | |
boolToAlignedStr True = "True " | |
boolToAlignedStr False = "False " | |
printRow : (List Bool, Bool) -> IO () | |
printRow (args, ans) = do | |
for_ args (putStr . boolToAlignedStr) | |
putStr "= " | |
print ans | |
putStr "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment