A truth table generator for classical propositional logic. http://extralogical.net/projects/hatt
|Latest on Hackage:||220.127.116.11|
This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.
Hatt is a command-line program which prints truth tables for expressions in classical propositional logic, and a library allowing its parser, evaluator and truth table generator to be used in other programs.
Hatt is available from Hackage. To install it with
your list of known packages and then install Hatt.
$ cabal update $ cabal install hatt
To build it from source,
cd into the directory containing the Hatt source
hatt.cabal, and run
Valid Hatt expressions
The following are all valid expression forms which can be parsed by Hatt, where ϕ and ψ are metalinguistic variables standing in for any valid expression.
betc.---basically anything in the character class
(ϕ & ψ)
(ϕ | ψ)
(ϕ -> ψ)
(ϕ <-> ψ)
Parentheses are not required around top-level formulae, regardless of whether
the primary connective is binary. For example, the expression
a | b is valid
and will be parsed correctly, as would
p <-> (q & ~r), although the
parenthesised versions of both these expressions (
(a | b) and
(p <-> (q & ~r))) are also fine.
There is currently no support for operator precedence, so nested expressions must be parenthesised correctly for the parser to make sense of them.
hatt command-line program
The default mode is interactive: you start the program, enter expressions at the prompt, and their truth tables are printed. Here's an example session.
$ hatt Entering interactive mode. Type `help` if you don't know what to do! > A | B A B | (A | B) ------------- T T | T T F | T F T | T F F | F > p -> (q & ~r) p q r | (p -> (q & ~r)) ----------------------- T T T | F T T F | T T F T | F T F F | F F T T | T F T F | T F F T | T F F F | T > e <-> f e f | (e <-> f) --------------- T T | T T F | F F T | F F F | T > exit
--evaluate flag lets you pass a single expression to be evaluated
$ 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
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 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
You can enable pretty-printing while in interactive mode by using the
If you pass the
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
You can print out the normal forms of expressions too, by prefixing an
$ hatt --pretty > nnf ~(P -> (Q & R)) (P ∧ (¬Q ∨ ¬R))
Using Hatt in other programs
Hatt exposes the
Data.Logic.Propositional module, which provides a simple API
for parsing, evaluating, and printing truth tables, and for converting logical
expressions into normal forms.
* Breaking API change: variables are now represented by a newtype wrapper `Var`
around `Char`s, rather than just a `String`. This made it easier to write a
good QuickCheck instance for this type.
* Added QuickCheck instances for the `Expr` and `Var` datatypes.
* Added a module to perform conversions of expressions to various normal forms,
including negation normal form, conjunctive normal form and disjunctive normal
form. Its use is supported in interactive mode by prefixing an expression with
"nnf", "cnf" or "dnf". Tests for the module are included and can be run with
`cabal test`. The module is exported as `Data.Logic.Propositional.NormalForms`
and must be imported separately.
* The `Data.Logic.Propositional.Tables` module is now exported by the library.
Its `truthTable` and `truthTableP` functions are still exported by the main
`Data.Logic.Propositional` module (as well as by the newly exposed module) for
* The `values` function is now exported.
* Make the parser slightly more liberal: unparenthesised expressions with a
binary connective as their primary connective such as "a | b" are now parsed
correctly, as are expressions with superfluous spaces such as " ( c -> ~ d )".
* Added command history and better text input handling with Haskeline.
* Add the `--coloured` command-line flag and `colour` command in interactive
mode to make truth value colouring optional.
* Relax containers dependency to >= 0.3 && < 0.5.
* Use truth table and parsing code to produce the example console session in
the interactive mode help text.
* Truth values are coloured green (T) and red (F) when printed.
* Correct semantics for interpreting conditionals. Fixes an operator precedence
bug in the previous implementation.
* Allow variables to be lower case ASCII letters as well as upper case.
* Pretty-printing can now be enabled and disabled when in the interactive mode
by using the `pretty` command.
* Added an interactive mode.
* Made interactive mode the default: it's only not entered if the `--evaluate`
flag is passed and the `--interactive` flag is not.
* Added the `--pretty` option to pretty-print expressions.
* The `--evaluate` option now prints ASCII truth tables by default.
* Added `truthTableP` function to allow configurable expression printing.
* The `variables` function now returns a list of unique elements.
* Dependency `base` loosened to >= 4 && < 5 so it will build with GHC 7.
* Exposed and other modules now correctly differentiated in cabal file.
* Initial release.