hatt 0.1 → 0.2
raw patch · 6 files changed
+43/−21 lines, 6 filesdep ~base
Dependency ranges changed: base
Files
- README.md +16/−5
- hatt.cabal +5/−5
- src/Data/Logic/Propositional.hs +1/−0
- src/Data/Logic/Propositional/Core.hs +2/−1
- src/Data/Logic/Propositional/Tables.hs +10/−3
- src/hatt.hs +9/−7
README.md view
@@ -44,6 +44,22 @@ Here's an example session doing just that. $ hatt --evaluate="(P -> (Q | ~R))"+ P Q R | (P -> (Q | ~R))+ -----------------------+ T T T | F+ T T F | F+ T F T | F+ T F F | F+ F T T | F+ F T F | F+ F F T | T+ F F F | F++By default, `hatt` will print ASCII representations of expressions. If you have+a Unicode-capable terminal, try passing the `--pretty` option to pretty-print+expressions using the the more common logical symbols.++ $ hatt --evaluate="(P -> (Q | ~R))" --pretty P Q R | (P → (Q ∨ ¬R)) ---------------------- T T T | F@@ -54,11 +70,6 @@ F T F | F F F T | T F F F | F--Note that while you need to use ASCII symbols to interact with `hatt`, it-pretty-prints expressions using the more common logical symbols. The Hatt-library exposes the `showAscii` function which will print expressions in the-format in which they're entered. Using Hatt in other programs
hatt.cabal view
@@ -1,5 +1,5 @@ Name: hatt-Version: 0.1+Version: 0.2 Stability: experimental Synopsis: A truth table generator for classical propositional logic.@@ -26,11 +26,11 @@ Library Hs-Source-Dirs: src GHC-options: -Wall- Build-depends: base >= 4.2 && < 4.3,+ Build-depends: base >= 4 && < 5, containers >= 0.3 && < 0.4, parsec >= 2.1 && < 2.2- Exposed-modules: Data.Logic.Propositional,- Data.Logic.Propositional.Core,+ Exposed-modules: Data.Logic.Propositional+ Other-modules: Data.Logic.Propositional.Core, Data.Logic.Propositional.Parser, Data.Logic.Propositional.Tables @@ -38,7 +38,7 @@ Hs-Source-Dirs: src Main-Is: hatt.hs GHC-options: -Wall- Build-depends: base >= 4.2 && < 4.3,+ Build-depends: base >= 4 && < 5, cmdargs >= 0.6 && < 0.7, containers >= 0.3 && < 0.4, parsec >= 2.1 && < 2.2
src/Data/Logic/Propositional.hs view
@@ -20,6 +20,7 @@ , show , showAscii , truthTable+ , truthTableP , variables ) where
src/Data/Logic/Propositional/Core.hs view
@@ -5,6 +5,7 @@ import Prelude hiding (lookup) import Control.Monad (replicateM)+import Data.List (nub) import Data.Map (Map, fromList, lookup) import Data.Maybe (fromMaybe) @@ -57,7 +58,7 @@ vars_ (Disjunction e1 e2) vs = vars_ e1 vs ++ vars_ e2 vs vars_ (Conditional e1 e2) vs = vars_ e1 vs ++ vars_ e2 vs vars_ (Biconditional e1 e2) vs = vars_ e1 vs ++ vars_ e2 vs- in vars_ expr []+ in nub $ vars_ expr [] -- | Determines whether two expressions are extensionally equivalent (that is, -- have the same values under all interpretations).
src/Data/Logic/Propositional/Tables.hs view
@@ -2,6 +2,7 @@ module Data.Logic.Propositional.Tables ( truthTable+ , truthTableP ) where import Data.Logic.Propositional.Core@@ -10,12 +11,18 @@ -- | The 'truthTable' function produces a truth table for the given expression. truthTable :: Expr -> String-truthTable expr = unlines [header, separator, body]+truthTable = truthTableP show++-- | The 'truthTableP' is a configurable version of 'truthTable' which allows a+-- printer function to be selected, so for example one can print ASCII truth+-- tables by passing 'showAscii' to 'truthTableP' instead of 'show'.+truthTableP :: (Expr -> String) -> Expr -> String+truthTableP printer expr = unlines [header, separator, body] where- header = unwords vs ++ " | " ++ show expr+ header = unwords vs ++ " | " ++ printer expr body = unlines $ map (showAssignment expr) as separator = concat $ replicate sepLength "-"- sepLength = length vs * 2 + length (show expr) + 2+ sepLength = length vs * 2 + length (printer expr) + 2 as = assignments expr vs = variables expr
src/hatt.hs view
@@ -10,12 +10,14 @@ data HattOpts = HattOpts { evaluate :: String+ , pretty :: Bool } deriving (Show, Data, Typeable) hattOpts :: HattOpts hattOpts = HattOpts- { evaluate = def &= opt "" &= typ "EXPRESSION"- &= help "Print the truth table for the given expression"+ { evaluate = "" &= typ "EXPRESSION"+ &= help "Print the truth table for the given expression"+ , pretty = False &= help "Use Unicode logic symbols when printing expressions" } &= summary "Hatt 0.1, (c) Benedict Eastaugh 2011" &= program "hatt" @@ -23,9 +25,9 @@ main = do opts <- cmdArgs hattOpts case evaluate opts of "" -> putStrLn "Try using the --evaluate[=EXPRESSION] flag"- expr -> putStr (eval expr)+ expr -> putStr (eval (if pretty opts then show else showAscii) expr) -eval :: String -> String-eval str = case parseExpr "" str of- Left err -> "parse error at " ++ show err ++ "\n"- Right expr -> truthTable expr+eval :: (Expr -> String) -> String -> String+eval p str = case parseExpr "" str of+ Left err -> "parse error at " ++ show err ++ "\n"+ Right expr -> truthTableP p expr