From d7d1e0cb056b69d3ea30ffb4f2c742871867d36a Mon Sep 17 00:00:00 2001 From: hi Date: Thu, 31 Jul 2025 11:29:43 +0000 Subject: [PATCH] latex truth table: easy part --- Main.hs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Main.hs b/Main.hs index d1d6eb5..64a1aca 100644 --- a/Main.hs +++ b/Main.hs @@ -1,6 +1,7 @@ import Control.Applicative (Applicative, Alternative(..)) import Data.Char (isAlphaNum) import Data.Functor (Functor) +import Data.List (intercalate) import Data.Maybe (fromJust) import qualified Data.Map as M import qualified Data.Set as S @@ -205,3 +206,34 @@ serialize = aux latex (Iff s1 s2) = connective "\\leftrightarrow " (latex s1) (latex s2) connective token s1 s2 = "(" <> s1 <> token <> s2 <> ")" + +truthtable :: Statement -> String +truthtable s = open <> header <> "\\hline\n" <> body <> close + where + open = + "\\begin{tabular}{" <> + replicate (length atomsList) 'c' <> + "|" <> + replicate (length serial) 'c' <> + "}\n" + + close = "\\end{tabular}\n\n" + + serial = serialize Latex s + + atomsList :: [String] + atomsList = S.toAscList $ atoms s + + header :: String + header = intercalate "&" atomsList <> " \\\\\n" + + body = concat $ map line $ enumerate atomsList + + line assignments = + intercalate "&" (bools assignments) <> + intercalate " & " (parts assignments) <> + " \\\\\n" + + bools assignments = [if bool then "1" else "0" | (key, bool) <- assignments] + + parts assignments = [""]