boolsimplifier-0.1.8: Simplification tools for simple propositional formulas.

Copyright(c) Gershom Bazerman Jeff Polakow 2011
LicenseBSD 3 Clause
Maintainergershomb@gmail.com
Stabilityexperimental
Safe HaskellSafe
LanguageHaskell98

Data.BoolSimplifier

Description

Machinery for representing and simplifying simple propositional formulas. Type families are used to maintain a simple normal form, taking advantage of the duality between "And" and "Or". Additional tools are provided to pull out common atoms in subformulas and otherwise iterate until a simplified fixpoint. Full and general simplification is NP-hard, but the tools here can take typical machine-generated formulas and perform most simplifications that could be spotted and done by hand by a reasonable programmer.

While there are many functions below, only qAtom, andq(s), orq(s), and qNot need be used directly to build expressions. simplifyQueryRep performs a basic simplification, simplifyIons works on expressions with negation to handle their reduction, and fixSimplifyQueryRep takes a function built out of any combination of basic simplifiers (you can write your own ones taking into account any special properties of your atoms) and runs it repeatedly until it ceases to reduce the size of your target expression.

The general notion is either that you build up an expression with these combinators directly, simplify it further, and then transform it to a target semantics, or that an expression in some AST may be converted into a normal form expression using such combinators, and then simplified and transformed back to the original AST.

Here are some simple interactions:

Prelude Data.BoolSimplifier> (qAtom "A") `orq` (qAtom "B")
QOp | fromList [QAtom Pos "A",QAtom Pos "B"] fromList []
Prelude Data.BoolSimplifier> ppQueryRep $ (qAtom "A") `orq` (qAtom "B")
"(A | B)"
Prelude Data.BoolSimplifier> ppQueryRep $ ((qAtom "A") `orq` (qAtom "B") `andq` (qAtom "A"))
"(A)"
Prelude Data.BoolSimplifier> ppQueryRep $ ((qAtom "A") `orq` (qAtom "B") `andq` (qAtom "A" `orq` qAtom "C"))
"((A | B) & (A | C))"
Prelude Data.BoolSimplifier> ppQueryRep $ simplifyQueryRep $ ((qAtom "A") `orq` (qAtom "B") `andq` (qAtom "A" `orq` qAtom "C"))
"((A | (B & C)))"

Synopsis

Documentation

data QOrTyp #

We'll start with three types of formulas: disjunctions, conjunctions, and atoms

data QAndTyp #

type family QFlipTyp t :: * #

disjunction is the dual of conjunction and vice-versa

data QueryRep qtyp a where #

A formula is either an atom (of some type, e.g. String).

A non-atomic formula (which is either a disjunction or a conjunction) is n-ary and consists of a Set of atoms and a set of non-atomic subformulas of dual connective, i.e. the non-atomic subformulas of a disjunction must all be conjunctions. The type system enforces this since there is no QFlipTyp instance for QAtomTyp.

Constructors

QAtom :: Ord a => a -> QueryRep QAtomTyp a 
QOp :: (Show qtyp, Ord a) => Set (QueryRep QAtomTyp a) -> Set (QueryRep (QFlipTyp qtyp) a) -> QueryRep qtyp a 

Instances

Eq a => Eq (QueryRep qtyp a) # 

Methods

(==) :: QueryRep qtyp a -> QueryRep qtyp a -> Bool #

(/=) :: QueryRep qtyp a -> QueryRep qtyp a -> Bool #

Ord a => Ord (QueryRep qtyp a) # 

Methods

compare :: QueryRep qtyp a -> QueryRep qtyp a -> Ordering #

(<) :: QueryRep qtyp a -> QueryRep qtyp a -> Bool #

(<=) :: QueryRep qtyp a -> QueryRep qtyp a -> Bool #

(>) :: QueryRep qtyp a -> QueryRep qtyp a -> Bool #

(>=) :: QueryRep qtyp a -> QueryRep qtyp a -> Bool #

max :: QueryRep qtyp a -> QueryRep qtyp a -> QueryRep qtyp a #

min :: QueryRep qtyp a -> QueryRep qtyp a -> QueryRep qtyp a #

Show a => Show (QueryRep qtyp a) # 

Methods

showsPrec :: Int -> QueryRep qtyp a -> ShowS #

show :: QueryRep qtyp a -> String #

showList :: [QueryRep qtyp a] -> ShowS #

PPQueryRep (QueryRep qtyp String) # 

Methods

ppQueryRep :: QueryRep qtyp String -> String #

PPQueryRep (QueryRep QAtomTyp (Ion String)) # 
PPQueryRep (QueryRep QAndTyp (Ion String)) # 
PPQueryRep (QueryRep QOrTyp (Ion String)) # 

extractCs :: QueryRep qtyp a -> Set (QueryRep (QFlipTyp qtyp) a) #

qOr :: Ord a => Set (QueryRep QAtomTyp a) -> Set (QueryRep QAndTyp a) -> QueryRep QOrTyp a #

convenience constructors, not particularly smart

class PPQueryRep a where #

pretty printer class

Minimal complete definition

ppQueryRep

Methods

ppQueryRep :: a -> String #

qop :: (Ord a, Show qtyp, Show (QFlipTyp qtyp), QFlipTyp (QFlipTyp qtyp) ~ qtyp) => Set (QueryRep QAtomTyp a) -> Set (QueryRep (QFlipTyp qtyp) a) -> QueryRep qtyp a #

smart constructor for QOp does following optimization: a /\ (a \/ b) <-> a, or dually: a \/ (a /\ b) <-> a

extractAtomCs :: (Ord a, Show qtyp, Show (QFlipTyp qtyp), QFlipTyp (QFlipTyp qtyp) ~ qtyp) => Set (QueryRep qtyp a) -> (Set (QueryRep qtyp a), Set (QueryRep QAtomTyp a)) #

class HasClause fife qtyp where #

QueryReps can be queried for clauses within them, and clauses within them can be extracted.

Minimal complete definition

hasClause, stripClause

Methods

hasClause :: QueryRep fife a -> QueryRep qtyp a -> Bool #

stripClause :: QueryRep qtyp a -> QueryRep fife a -> QueryRep fife a #

Instances

(~) * (QFlipTyp fife) qtyp => HasClause fife qtyp # 

Methods

hasClause :: QueryRep fife a -> QueryRep qtyp a -> Bool #

stripClause :: QueryRep qtyp a -> QueryRep fife a -> QueryRep fife a #

HasClause fife QAtomTyp # 

Methods

hasClause :: QueryRep fife a -> QueryRep QAtomTyp a -> Bool #

stripClause :: QueryRep QAtomTyp a -> QueryRep fife a -> QueryRep fife a #

andqs :: Ord a => CombineQ a qtyp QAndTyp => [QueryRep qtyp a] -> QueryRep QAndTyp a #

convenience functions

orqs :: Ord a => CombineQ a qtyp QOrTyp => [QueryRep qtyp a] -> QueryRep QOrTyp a #

class CombineQ a qtyp1 qtyp2 where #

smart constructors for QueryRep

Minimal complete definition

andq, orq

Methods

andq :: QueryRep qtyp1 a -> QueryRep qtyp2 a -> QueryRep QAndTyp a #

orq :: QueryRep qtyp1 a -> QueryRep qtyp2 a -> QueryRep QOrTyp a #

Instances

Ord a => CombineQ a QAtomTyp QAtomTyp # 
Ord a => CombineQ a QAtomTyp QOrTyp # 
Ord a => CombineQ a QAtomTyp QAndTyp # 
Ord a => CombineQ a QOrTyp QAtomTyp # 
Ord a => CombineQ a QOrTyp QOrTyp # 
Ord a => CombineQ a QOrTyp QAndTyp # 
Ord a => CombineQ a QAndTyp QAtomTyp # 
Ord a => CombineQ a QAndTyp QOrTyp # 
Ord a => CombineQ a QAndTyp QAndTyp # 

simplifyQueryRep :: (Ord a, Show (QFlipTyp qtyp), Show (QFlipTyp (QFlipTyp qtyp)), QFlipTyp (QFlipTyp qtyp) ~ qtyp) => QueryRep qtyp a -> QueryRep qtyp a #

(a /\ b) \/ (a /\ c) \/ d <-> (a /\ (b \/ c)) \/ d (and also the dual)

getCommonClauseAs :: Ord a => Set (QueryRep fife a) -> Maybe (QueryRep QAtomTyp a, Set (QueryRep fife a), Set (QueryRep fife a)) #

Given a set of QueryReps, extracts a common clause if possible, returning the clause, the terms from which the clause has been extracted, and the rest.

getCommonClauseCs :: Ord a => Set (QueryRep fife a) -> Maybe (QueryRep (QFlipTyp fife) a, Set (QueryRep fife a), Set (QueryRep fife a)) #

fixSimplifyQueryRep :: (QueryRep qtyp a -> QueryRep qtyp a) -> QueryRep qtyp a -> QueryRep qtyp a #

Takes any given simplifier and repeatedly applies it until it ceases to reduce the size of the query reprepresentation.

data Ion a #

We can wrap any underying atom dype in an Ion to give it a "polarity" and add handling of "not" to our simplification tools.

Constructors

Neg a 
Pos a 

Instances

Eq a => Eq (Ion a) # 

Methods

(==) :: Ion a -> Ion a -> Bool #

(/=) :: Ion a -> Ion a -> Bool #

Ord a => Ord (Ion a) # 

Methods

compare :: Ion a -> Ion a -> Ordering #

(<) :: Ion a -> Ion a -> Bool #

(<=) :: Ion a -> Ion a -> Bool #

(>) :: Ion a -> Ion a -> Bool #

(>=) :: Ion a -> Ion a -> Bool #

max :: Ion a -> Ion a -> Ion a #

min :: Ion a -> Ion a -> Ion a #

Show a => Show (Ion a) # 

Methods

showsPrec :: Int -> Ion a -> ShowS #

show :: Ion a -> String #

showList :: [Ion a] -> ShowS #

PPQueryRep (QueryRep QAtomTyp (Ion String)) # 
PPQueryRep (QueryRep QAndTyp (Ion String)) # 
PPQueryRep (QueryRep QOrTyp (Ion String)) # 

qAtom :: Ord a => a -> QueryRep QAtomTyp (Ion a) #

isEmptyQR :: QueryRep qtyp a -> Bool #

isConstQR :: QueryRep qtyp a -> Bool #

class PPConstQR qtyp where #

Minimal complete definition

ppConstQR

Methods

ppConstQR :: QueryRep qtyp a -> String #

class QNot qtyp where #

Minimal complete definition

qNot

Associated Types

type QNeg qtyp #

Methods

qNot :: QueryRep qtyp (Ion a) -> QueryRep (QNeg qtyp) (Ion a) #

Instances

QNot QAtomTyp # 

Associated Types

type QNeg QAtomTyp :: * #

QNot QAndTyp # 

Associated Types

type QNeg QAndTyp :: * #

Methods

qNot :: QueryRep QAndTyp (Ion a) -> QueryRep (QNeg QAndTyp) (Ion a) #

QNot QOrTyp # 

Associated Types

type QNeg QOrTyp :: * #

Methods

qNot :: QueryRep QOrTyp (Ion a) -> QueryRep (QNeg QOrTyp) (Ion a) #

simplifyIons :: (Ord a, Show (QFlipTyp qtyp), QFlipTyp (QFlipTyp qtyp) ~ qtyp) => QueryRep qtyp (Ion a) -> QueryRep qtyp (Ion a) #

 a  /\  (b \/ ~b)  /\  (c \/ d)   <->   a /\ (c \/ d)
 a  /\  ~a         /\  (b \/ c)   <->   False
        (a \/ ~a)  /\  (b \/ ~b)  <->   True  (*)

and duals

N.B. 0-ary \/ is False and 0-ary /\ is True

maximumByNote :: String -> (a -> a -> Ordering) -> [a] -> a #