ninetynine-1.1.0: Ninety-Nine Haskell Problems
Copyright Copyright (C) 2023 Yoo Chung GPL-3.0-or-later dev@chungyc.org Safe-Inferred GHC2021

Problems.Logic

Description

Supporting definitions for logic and code problems.

Synopsis

# Truth tables

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.

## Utility functions

printTable :: [(Bool, Bool, Bool)] -> IO () Source #

Print truth table as returned by table.

Given the same pair of truth tables except for order, the output will be the same.

printTablen :: [[Bool]] -> IO () Source #

Print truth table as returned by tablen.

Given the same pair of truth tables except for order, the output will be the same.

data Functions Source #

Set of functions grouped together.

Useful for passing in a particular set of functions. E.g., testing and benchmarking particular implementations.

Constructors

 Functions FieldsgetTable :: (Bool -> Bool -> Bool) -> [(Bool, Bool, Bool)] getAnd :: Bool -> Bool -> Bool getOr :: Bool -> Bool -> Bool getNand :: Bool -> Bool -> Bool getNor :: Bool -> Bool -> Bool getXor :: Bool -> Bool -> Bool getImpl :: Bool -> Bool -> Bool getEqu :: Bool -> Bool -> Bool

# Propositional logic

data Formula Source #

Represents a boolean formula.

Constructors

 Value Bool A constant value. Variable String A variable with given name. Complement Formula Logical complement. I.e., it is true only if its clause is false. Disjoin [Formula] Disjunction. I.e., it is true if any of its clauses are true. Conjoin [Formula] Conjunction. I.e., it is true only if all of its clauses are true.

#### Instances

Instances details
 Source # Instance detailsDefined in Problems.Logic Associated Typestype Rep Formula :: Type -> Type # Methodsto :: Rep Formula x -> Formula # Source # Instance detailsDefined in Problems.Logic MethodsshowList :: [Formula] -> ShowS # Source # Instance detailsDefined in Problems.Logic Methodsrnf :: Formula -> () # Source # Instance detailsDefined in Problems.Logic Methods(==) :: Formula -> Formula -> Bool #(/=) :: Formula -> Formula -> Bool # Source # Instance detailsDefined in Problems.Logic Methods(<) :: Formula -> Formula -> Bool #(<=) :: Formula -> Formula -> Bool #(>) :: Formula -> Formula -> Bool #(>=) :: Formula -> Formula -> Bool # type Rep Formula Source # Instance detailsDefined in Problems.Logic type Rep Formula = D1 ('MetaData "Formula" "Problems.Logic" "ninetynine-1.1.0-KhbacJ3ihVC6IHhZVwmJyl" 'False) ((C1 ('MetaCons "Value" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool)) :+: C1 ('MetaCons "Variable" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String))) :+: (C1 ('MetaCons "Complement" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Formula)) :+: (C1 ('MetaCons "Disjoin" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [Formula])) :+: C1 ('MetaCons "Conjoin" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [Formula])))))

## Utility functions

Evaluate a boolean formula with the given mapping of variable names to values.