-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.hs
66 lines (49 loc) · 1.58 KB
/
gen.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Data.List
import System.Environment
expr a b c = a `implies` b || c
table = [ [a, b, c, expr a b c] | a <- bools, b <- bools, c <- bools ]
bools :: [Bool]
bools = [True, False]
iff :: Bool -> Bool -> Bool
iff = (==)
implies :: Bool -> Bool -> Bool
a `implies` b = not a || b
separate :: a -> [a] -> [a]
separate x xs = x : intersperse x xs ++ [x]
text :: [[String]] -> String
text table = concat $ separate hline tableRows
where
tableRows = map (concat . (\l -> separate "|" l ++ ["\n"])) table
hline = replicate (maximum (map length tableRows) - 1) '-' ++ "\n"
textBool :: Bool -> String
textBool b = if b then " T " else " F "
textTable :: [[Bool]] -> [[String]]
textTable table = map (map textBool) table
latex :: [[String]] -> String
latex table = header ++ (concat $ separate hline tableRows) ++ footer
where
tableRows = map (\l -> "\t" ++ concat (intersperse " & " l) ++ " \\\\\n") table
hline = "\t\\hline\n"
cols = length (table !! 0)
header = "\\begin{tabular}{" ++ separate '|' (replicate cols 'c') ++ "}\n"
footer = "\\end{tabular}\n"
latexBool :: Bool -> String
latexBool b = if b then "T" else "F"
latexTable :: [[Bool]] -> [[String]]
latexTable table = map (map latexBool) table
usage :: String
usage = "gen [--latex]\n"
textMain :: String
textMain = text (textTable table)
latexMain :: String
latexMain = latex (latexTable table)
main' :: [String] -> String
main' args = case args of
["--help"] -> usage
["--latex"] -> latexMain
["--text"] -> textMain
[] -> textMain
_ -> usage
main = do
args <- getArgs
putStr (main' args)