packages feed

hatt 1.2.1 → 1.3.0

raw patch · 4 files changed

+67/−33 lines, 4 filesdep ~containersPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: containers

API changes (from Hackage documentation)

- Data.Logic.Propositional: truthTableP :: (Expr -> String) -> Expr -> String
+ Data.Logic.Propositional: truthTableP :: Printer -> Expr -> String

Files

README.md view
@@ -46,7 +46,7 @@     $ hatt     Entering interactive mode. Type `help` if you don't know what to do!     > help-    Hatt's interactive mode has a couple of commands.+    Hatt's interactive mode has several commands.          help       Print this help text.@@ -56,6 +56,11 @@       option if your console is Unicode-aware. If pretty-printing is already       enabled, using this command will disable it.     +    colour+      Colour truth values: green for true, red for false. This feature needs+      your console to support ANSI colour codes. If coloured mode is already+      enabled, this command will disable it.+         exit       Quit the program.     @@ -66,25 +71,25 @@     truth table for that expression. Here's an example console session.              > (A | B)-        A B | (A ∨ B)+        A B | (A | B)         --------------        T T | T+        T T | F         T F | T         F T | T         F F | F         > foobar-        Error: parse error at (line 1, column 1):-        unexpected "f"-        expecting white space, "(" or "~"-       > exit+        Parse error at (line 1, column 2):+        unexpected "o"+        expecting white space or end of input+        > exit          If none of this makes any sense, try reading the README file.     > (A -> B)     A B | (A -> B)     ---------------    T T | F+    T T | T     T F | F-    F T | F+    F T | T     F F | T     > exit @@ -121,6 +126,10 @@  You can enable pretty-printing while in interactive mode by using the `pretty` command.++If you pass the `--coloured` flag, `hatt` will colour the truth values in the+tables which it prints: green for true, red for false. You can enable colouring+during interactive mode by using the `colour` command.   Using Hatt in other programs
hatt.cabal view
@@ -1,5 +1,5 @@ Name:               hatt-Version:            1.2.1+Version:            1.3.0  Synopsis:           A truth table generator for classical propositional logic. Description:        Hatt is a command-line program which prints truth tables@@ -26,7 +26,7 @@   Hs-Source-Dirs:   src   GHC-options:      -Wall   Build-depends:    base           >= 4 && < 5,-                    containers     >= 0.3 && < 0.4,+                    containers     >= 0.3 && < 0.5,                     parsec         >= 2.1 && < 2.2,                     ansi-wl-pprint >= 0.6 && < 0.7   Exposed-modules:  Data.Logic.Propositional@@ -40,6 +40,6 @@   GHC-options:      -Wall   Build-depends:    base           >= 4 && < 5,                     cmdargs        >= 0.7,-                    containers     >= 0.3 && < 0.4,+                    containers     >= 0.3 && < 0.5,                     parsec         >= 2.1 && < 2.2,                     ansi-wl-pprint >= 0.6 && < 0.7
src/Data/Logic/Propositional/Tables.hs view
@@ -1,7 +1,10 @@ {-# OPTIONS_HADDOCK hide #-}  module Data.Logic.Propositional.Tables-    ( truthTable+    ( Printer+    , colourBool+    , showBool+    , truthTable     , truthTableP     ) where @@ -10,29 +13,35 @@ import Data.Map (fold) import Text.PrettyPrint.ANSI.Leijen (green, text, red) +type Printer = (Expr -> String, Bool -> String)+ -- | The 'truthTable' function produces a truth table for the given expression. truthTable :: Expr -> String-truthTable = truthTableP show+truthTable = truthTableP (show, colourBool)  -- | 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]+truthTableP :: Printer -> Expr -> String+truthTableP (expPrinter, boolPrinter) expr = unlines [header, separator, body]   where-    header    = unwords vs ++ " | " ++ printer expr-    body      = init . unlines $ map (showAssignment expr) as+    header    = unwords vs ++ " | " ++ expPrinter expr+    body      = init . unlines $ map (showAssignment boolPrinter expr) as     separator = concat $ replicate sepLength "-"-    sepLength = length vs * 2 + length (printer expr) + 2+    sepLength = length vs * 2 + length (expPrinter expr) + 2     as        = assignments expr     vs        = variables   expr -showAssignment :: Expr -> Mapping -> String-showAssignment expr a = showVarValues ++ " | " ++ showExprValue+showAssignment :: (Bool -> String) -> Expr -> Mapping -> String+showAssignment printer expr a = showVarValues ++ " | " ++ showExprValue   where-    showVarValues = unwords $ fold ((:) . showBool) [] a-    showExprValue = showBool $ interpret expr a+    showVarValues = unwords $ fold ((:) . printer) [] a+    showExprValue = printer $ interpret expr a  showBool :: Bool -> String-showBool True  = show . green . text $ "T"-showBool False = show . red . text $ "F"+showBool True  = "T"+showBool False = "F"++colourBool :: Bool -> String+colourBool True  = show . green . text $ "T"+colourBool False = show . red . text $ "F"
src/hatt.hs view
@@ -3,6 +3,7 @@ module Main (main) where  import Data.Logic.Propositional+import Data.Logic.Propositional.Tables  import Control.Monad (when, unless) import Data.Char (isSpace, toLower)@@ -12,6 +13,7 @@ data Command = Exit              | Help              | Pretty+             | Coloured              | Eval Expr              | Error String @@ -19,6 +21,7 @@   { evaluate    :: String   , interactive :: Bool   , pretty      :: Bool+  , coloured    :: Bool   } deriving (Show, Data, Typeable)  programMode :: ProgramMode@@ -27,7 +30,8 @@                      &= help "Print the truth table for the given expression"   , interactive = False &= help "Enter interactive mode"   , pretty      = False &= help "Use Unicode logic symbols"-  } &= summary "Hatt 1.2.1, (c) Benedict Eastaugh 2011"+  , coloured    = False &= help "Use colour-coded symbols"+  } &= summary "Hatt 1.3.0, (c) Benedict Eastaugh 2011"     &= program "hatt"  main :: IO ()@@ -57,16 +61,20 @@                                 >> repl mode                  Pretty      -> putStrLn ppMessage                                 >> repl (mode {pretty = not isPretty})+                 Coloured    -> putStrLn cpMessage+                                >> repl (mode {coloured = not isColoured})                  (Eval expr) -> putStr (truthTableP printer expr)                                 >> repl mode                  (Error err) -> putStrLn ("Error: " ++ err)                                 >> repl mode   where-    printer   = selectPrinter mode-    isPretty  = pretty mode-    ppMessage = (if isPretty then "Dis" else "En") ++ "abling pretty-printing."+    printer    = selectPrinter mode+    isPretty   = pretty mode+    isColoured = coloured mode+    ppMessage  = (if isPretty then "Dis" else "En") ++ "abling pretty-printing."+    cpMessage  = (if isColoured then "Dis" else "En") ++ "abling colour-coding." -eval :: (Expr -> String) -> String -> String+eval :: Printer -> String -> String eval p str = case parseExpr "" str of                Left  err  -> "Parse error at " ++ show err ++ "\n"                Right expr -> truthTableP p expr@@ -77,6 +85,7 @@                        "exit"   -> Exit                        "help"   -> Help                        "pretty" -> Pretty+                       "colour" -> Coloured                        _        -> eval_ input   where     cmd []    = ""@@ -91,7 +100,7 @@   , "Type `help` if you don't know what to do!"   ] -replHelpText :: (Expr -> String) -> String+replHelpText :: Printer -> String replHelpText printer = unlines   [ "Hatt's interactive mode has several commands."   , ""@@ -103,6 +112,11 @@   , "  option if your console is Unicode-aware. If pretty-printing is already"   , "  enabled, using this command will disable it."   , ""+  , "colour"+  , "  Colour truth values: green for true, red for false. This feature needs"+  , "  your console to support ANSI colour codes. If coloured mode is already"+  , "  enabled, this command will disable it."+  , ""   , "exit"   , "  Quit the program."   , ""@@ -120,8 +134,10 @@   , "If none of this makes any sense, try reading the README file."   ] -selectPrinter :: ProgramMode -> Expr -> String-selectPrinter m = if pretty m then show else showAscii+selectPrinter :: ProgramMode -> Printer+selectPrinter m = let expPrinter   = if pretty m then show else showAscii+                      tablePrinter = if coloured m then colourBool else showBool+                  in (expPrinter, tablePrinter)  indentBy :: Int -> String -> String indentBy n = unlines . map (replicate n ' ' ++) . lines