From f0ed4df2b0e11ab66966a8395cd7d6d29c7a8efc Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Fri, 3 Jul 2015 23:39:40 +0200 Subject: Latex & Html output options --- Logic.dcl | 23 +++++++--- Logic.icl | 134 ++++++++++++++++++++++++++++++++++++++------------------ LogicParser.icl | 16 ++++--- README.md | 3 ++ StringUtils.dcl | 4 +- StringUtils.icl | 10 +++-- index.html | 4 +- request.php | 2 +- 8 files changed, 134 insertions(+), 62 deletions(-) diff --git a/Logic.dcl b/Logic.dcl index 1a0821b..23ae191 100644 --- a/Logic.dcl +++ b/Logic.dcl @@ -23,7 +23,7 @@ */ definition module Logic -import StdEnv +import StdEnv, StdMaybe // Expressions :: Expr = B Bool // A constant @@ -47,6 +47,19 @@ import StdEnv :: AtomOption :== (AtomName,Bool) :: TruthTable = {exprs :: [Expr], options :: [[AtomOption]]} +:: FilledTruthTable = {table :: TruthTable, values :: [[Maybe Bool]]} + +:: OutputOption = Plain | Html | LaTeX +DefaultOutputOption :== Plain +instance == OutputOption + +class show a :: OutputOption a -> String +instance show Bool +instance show Char +instance show Op1 +instance show Op2 +instance show Expr +instance show FilledTruthTable isBool :: Expr -> Bool isAtom :: Expr -> Bool @@ -60,11 +73,6 @@ isOr :: Expr -> Bool isImpl :: Expr -> Bool isEquiv :: Expr -> Bool -instance toString Op1 -instance toString Op2 -instance toString Expr -instance toString TruthTable - instance == Op1 instance < Op1 // Maybe useful later if more unary operators are added instance == Op2 @@ -87,6 +95,9 @@ sorted_subexprs :: (Expr -> [Expr]) // Similar, but roughly sorted by co simple_truthtable :: Expr -> TruthTable // Simple truthtable: only the atomic expression and the expression itself simple_truthtable_n :: [Expr] -> TruthTable // Simple truthtable with multiple expressions truthtable :: Expr -> TruthTable // Truthtable from an expression +truthtable_n :: [Expr] -> TruthTable // Truthtable with multiple expressions + +compute :: TruthTable -> FilledTruthTable // Fill in a truthtable parse :: String -> Expr // Parse a string into an expression diff --git a/Logic.icl b/Logic.icl index dabc71d..97d8df5 100644 --- a/Logic.icl +++ b/Logic.icl @@ -23,7 +23,7 @@ */ implementation module Logic -import StdEnv, StringUtils +import StdEnv, StdMaybe, StringUtils isBool :: Expr -> Bool isBool (B _) = True @@ -73,50 +73,51 @@ apply2 x Or y = x || y apply2 x Impl y = y || not x apply2 x Equiv y = x == y -instance toString Op1 +instance == OutputOption where - toString Not = "~" + (==) Plain Plain = True + (==) Html Html = True + (==) LaTeX LaTeX = True + (==) _ _ = False -instance toString Op2 +instance show Bool where - toString And = "&" - toString Or = "|" - toString Impl = "->" - toString Equiv = "<->" + show LaTeX True = "\\top" + show LaTeX False = "\\bot" + show _ b = toString b -instance toString Expr +instance show Char where - toString (B b) = toString b - toString (Atom a) = toString a - toString (App1 op e) - | needs_parentheses (App1 op e) = toString op +++ "(" +++ toString e +++ ")" - | otherwise = toString op +++ toString e - toString (App2 e1 op e2) = e1` +++ " " +++ toString op +++ " " +++ e2` - where - e1` - | needs_parentheses_left (App2 e1 op e2) = "(" +++ toString e1 +++ ")" - | otherwise = toString e1 - e2` - | needs_parentheses_right (App2 e1 op e2) = "(" +++ toString e2 +++ ")" - | otherwise = toString e2 - -instance toString TruthTable + show LaTeX c = " " +++ toString c + show _ c = toString c + +instance show Op1 where - toString t=:{exprs,options} - = row_b +++ join row_s [pad_right ' ' head len \\ head <- map toString exprs & len <- padlens] +++ row_e +++ - line_b +++ join line_s [toString (repeatn len '-') \\ len <- padlens] +++ line_e +++ - foldr (+++) "" [row_b +++ join row_s [pad_right ' ' (toStringOrEmpty val) len \\ val <- map (eval o substitute_all options`) exprs & len <- padlens] +++ row_e \\ options` <- options] - where - row_b = " " // Row / Line begin, end, separator - row_e = " \n" - row_s = " | " - line_b = "-" - line_e = "-\n" - line_s = "-+-" - padlens = map ((max 5) o strlen o toString) exprs // 5 is the length of False - toStringOrEmpty :: [Bool] -> String - toStringOrEmpty [] = "" - toStringOrEmpty [b:bs] = toString b + show Plain Not = "~" + show Html Not = "¬" + show LaTeX Not = "\\neg" + +instance show Op2 +where + show Plain And = "&" + show Plain Or = "|" + show Plain Impl = "->" + show Plain Equiv = "<->" + show Html And = "∧" + show Html Or = "∨" + show Html Impl = "→" + show Html Equiv = "↔" + show LaTeX And = "\\land" + show LaTeX Or = "\\lor" + show LaTeX Impl = "\\rightarrow" + show LaTeX Equiv = "\\leftrightarrow" + +instance show Expr +where + show opt (B b) = show opt b + show opt (Atom a) = show opt a + show opt (App1 op e) = show opt op +++ show opt e + show opt (App2 e1 op e2) = show opt e1 +++ " " +++ show opt op +++ " " +++ show opt e2 instance == Op1 where @@ -175,7 +176,7 @@ where comp e1 e2 | isMember e1 (subexprs e2) = True | isMember e2 (subexprs e1) = False - | otherwise = strlen (toString e1) < strlen (toString e2) + | otherwise = strlen (show Plain e1) < strlen (show Plain e2) // Does a the argument of a unary operator need parentheses? needs_parentheses :: Expr -> Bool @@ -256,6 +257,55 @@ simple_truthtable_n es = {exprs = removeDup ([Atom a \\ a <- flatten (map all_at truthtable :: Expr -> TruthTable truthtable e = {exprs = sorted_subexprs e ++ [e], options = all_atom_options e} +truthtable_n :: [Expr] -> TruthTable +truthtable_n es = {exprs = sort (removeDup (flatten ([[e:subexprs e] \\ e <- es]))), options = removeDup (flatten (map all_atom_options es))} + +compute :: TruthTable -> FilledTruthTable // Fill in a truthtable +compute table=:{exprs,options} = {table=table, values=values} +where + values = [[toMaybeBool val \\ val <- map (eval o substitute_all options`) exprs] \\ options` <- options] + toMaybeBool [] = Nothing + toMaybeBool [b:bs] = Just b + +instance show FilledTruthTable +where + show :: OutputOption FilledTruthTable -> String + show opt t=:{table=table=:{exprs,options},values} + = begin +++ + head_b +++ + join head_s [pad_right (showOrNot head_i) header len \\ header <- map (show opt) exprs & len <- padlens] +++ + head_e +++ + line_b +++ + join line_s [foldr (+++) "" (repeatn len (showOrNot line_i)) \\ len <- padlens] +++ + line_e +++ + foldr (+++) "" [row_b +++ join row_s [pad_right (showOrNot row_i) (showOrNot v) len \\ v <- r & len <- padlens] +++ row_e \\ r <- values] +++ + end + where + padlens = map ((\l . maxList (map strlen [l,show opt True,show opt False])) o (show opt)) exprs + // Ideally, we would some kind of DOM writer for Html, but for this project this is sufficient (although not very readable) + (begin,end) = gen + gen + | opt == Plain = ("","") + | opt == Html = ("
Truth tables are generated with a server-side Clean program.
- +