Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The basic <code>evaluate</code> is pretty straight forward:</p> <pre><code>import Data.Maybe (fromJust) import Data.List (nub) type Variable = Char data LogicExpr = Var Variable | Neg LogicExpr | Conj LogicExpr LogicExpr | Disj LogicExpr LogicExpr | Impl LogicExpr LogicExpr deriving (Eq, Ord) -- evaluates an expression evaluate :: LogicExpr -&gt; [(Variable, Bool)] -&gt; Bool evaluate (Var v) bs = fromJust (lookup v bs) evaluate (Neg e) bs = not (evaluate e bs) evaluate (Conj e1 e2) bs = evaluate e1 bs &amp;&amp; evaluate e2 bs evaluate (Disj e1 e2) bs = evaluate e1 bs || evaluate e2 bs evaluate (Impl e1 e2) bs = not (evaluate e1 bs) || evaluate e2 bs </code></pre> <p>To generate a truth table, you first have to find all the variables in an expression and then generate all the possible assignments for these variables. The truth values of these assignments can easily be determined with the already implemented <code>evaluate</code> function:</p> <pre><code>-- get variables in an expression varsp :: LogicExpr -&gt; [Variable] varsp (Var v) = [v] varsp (Neg e) = varsp e varsp (Conj e1 e2) = varsp e1 ++ varsp e2 varsp (Disj e1 e2) = varsp e1 ++ varsp e2 varsp (Impl e1 e2) = varsp e1 ++ varsp e2 -- get variables in an expression without duplicates vars :: LogicExpr -&gt; [Variable] vars = nub . varsp -- possible boolean values bools = [True, False] -- all possible combinations of variable assignments booltable :: [Variable] -&gt; [[(Variable, Bool)]] booltable [] = [[]] booltable (a:as) = [(a,b) : r | b &lt;- bools, r &lt;- booltable as] -- variable assignments and corresponding evaluation of an expression truthtable :: LogicExpr -&gt; [([(Variable, Bool)], Bool)] truthtable e = [(bs, evaluate e bs) | bs &lt;- booltable (vars e)] </code></pre> <p>If you want to explore the dark corners of the standard library, you can also write a <code>Read</code> instance for easy input of <code>LogicExpr</code>s:</p> <pre><code>-- read a right-associative infix operator readInfix opprec constr repr prec r = readParen (prec &gt; opprec) (\r -&gt; [(constr e1 e2, u) | (e1,s) &lt;- readsPrec (opprec+1) r, (op,t) &lt;- lex s, op == repr, (e2,u) &lt;- readsPrec (opprec) t]) r instance Read LogicExpr where readsPrec prec r = readInfix 1 Impl "-&gt;" prec r ++ readInfix 2 Disj "|" prec r ++ readInfix 3 Conj "&amp;" prec r ++ readParen (prec &gt; 4) (\r -&gt; [(Neg e, t) | ("!",s) &lt;- lex r, (e,t) &lt;- readsPrec 4 s]) r ++ readParen (prec &gt; 5) (\r -&gt; [(Var v, s) | ([v], s) &lt;- lex r]) r </code></pre> <p>And truth tables can be printed prettily:</p> <pre><code>showcell :: (Variable, Bool) -&gt; String showcell (v,b) = v : "=" ++ show b showrow :: [(Variable, Bool)] -&gt; Bool -&gt; String showrow [] b = show b showrow [a] b = showcell a ++ " =&gt; " ++ show b showrow (a:as) b = showcell a ++ " &amp;&amp; " ++ showrow as b printrow :: ([(Variable, Bool)], Bool) -&gt; IO () printrow = putStrLn . uncurry showrow printtbl :: [([(Variable, Bool)], Bool)] -&gt; IO () printtbl = mapM_ printrow </code></pre> <p>All together truth tables can be generated like this:</p> <pre><code>Prelude Main&gt; printtbl $ truthtable $ read "(a -&gt; b) &amp; (b -&gt; a)" a=True &amp;&amp; b=True =&gt; True a=True &amp;&amp; b=False =&gt; False a=False &amp;&amp; b=True =&gt; False a=False &amp;&amp; b=False =&gt; True Prelude Main&gt; printtbl $ truthtable $ read "(a | b) | (!a &amp; !b)" a=True &amp;&amp; b=True =&gt; True a=True &amp;&amp; b=False =&gt; True a=False &amp;&amp; b=True =&gt; True a=False &amp;&amp; b=False =&gt; True </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload