Problems.P46

Description

Part of Ninety-Nine Haskell Problems. Some solutions are in Solutions.P46.

Synopsis

# Documentation

type BoolFunc = Bool -> Bool -> Bool Source #

Define boolean functions and', or', nand', nor', xor', impl', and equ', which succeed or fail according to the result of their respective operations; e.g., (and' a b) is true if and only if both a and b are true. Do not use the builtin boolean operators.

A logical expression in two variables can then be written as in the following example:

\a b -> and' (or' a b) (nand' a b)

Write a function table which returns the truth table of a given logical expression in two variables.

### Notes

Expand

There is no technical reason to define the type synonym BoolFunc. However, it is a good place to explain the entire problem without talking about writing truth table functions first, especially for inclusion in the documentation for Problems.

The original problem for Haskell required that the truth table be printed: "Now, write a predicate table/3 which prints the truth table of a given logical expression in two variables." It was changed here to return a list of boolean tuples, because the requirement for I/O seemed uninteresting in the context of the rest of the problem.

Documentation for the expected semantics for each boolean function was added, and the example for table was modified to avoid sensitivity to order.

table :: BoolFunc -> [(Bool, Bool, Bool)] Source #

Truth tables for logical expressions with two operands.

The truth table will be a list of (Bool, Bool, Bool) tuples. If f is the logical expression, a tuple (a, b, c) in the list will mean that (f a b == c). The list will include all possible distinct combinations of the tuples.

### Examples

>>> printTable \$ table (\a b -> (and' a (or' a b)))
False False False
False True  False
True  False True
True  True  True


# Boolean functions

Logical conjunction. I.e., (and' a b) is true if and only if both a and b are true.

### Examples

>>> and' True False
False


Logical disjuncton. I.e., (or' a b) is true if and only if one or both of a and b are true.

### Examples

>>> or' True False
True


Logical alternative denial. I.e., (nand' a b) is true if and only if (and' a b) is false.

### Examples

>>> nand' True False
True


Logical joint denial. I.e., (nor' a b) is true if and only if (or' a b) is false.

### Examples

>>> nor' True False
False


Logical exclusive disjunction. I.e., (xor' a b) is true if and only if exactly one of a and b is true.

### Examples

>>> xor' True False
True


Logical implication. I.e., (impl' a b) being true means b must be true if a is true. If a is false, there is no implication as to what b should be.

### Examples

>>> impl' True False
False


Logical equivalence. I.e., (equ' a b) being true means a is true if and only if b is true.

### Examples

>>> equ' True False
False


# Utility functions

Functions in this module grouped together.

They are grouped together for testing or benchmarking purposes.