module Algorithm where
import System.Random
import Data.Maybe
import Data.List
type Atom = String
type Literal = (Bool,Atom)
type Clause = [Literal]
type Formula = [Clause]
type Model = [(Atom, Bool)]
type Node = (Formula, ([Atom], Model))
-- This function takess a Clause and return the set of Atoms of that Clause.
atomsClause :: Clause -> [Atom]
-- This function takes a Formula returns the set of Atoms of a Formula
atoms :: Formula -> [Atom]
-- This function returns True if the given Literal can be found within
-- the Clause.
isLiteral :: Literal -> Clause -> Bool
-- this function takes a Model and an Atom and flip the truthvalue of
-- the atom in the model
flipSymbol :: Model -> Atom -> Model -- is this ok?
Additional functions :
remove :: (Eq a) )a ->[a] ->[a]
-This function removes an item from a list.
neg :: Literal->Literal
-This function flips a literal (ie. from P to :P and from :P to P).
falseClause :: Model -> Clause -> Bool
-This function takes a Model and a Clause and returns True
if the clause is unsatisfied by the model or False otherwise.
falseClauses :: Formula -> Model -> [Clause]
-This function takes a Formula and a Model and returns the list of clauses of the formula that are not satisfied.
assignModel :: Model -> Formula -> Formula
-This function applies the assign function for all the assignments of a given model.
checkFormula :: Formula -> Maybe Bool This function checks whether a formula can be decided to be satisfiable or unsatisfiable based on the effects of the assign function.
satisfies :: Model -> Formula -. Bool This function checks whether a model satisfies a formula. This is done with the combination of the assignModel and checkFormula functions.
module Algorithm where import System.Random import Data.Maybe import Data.List type Atom = String type
Share
One place to get you started: look at
Now suppose we can write an function
Then we may have a chance of using that function to filter general formulae. So I would attempt to ignore everything but the function
isTautology. Essentially the question here is: What is a tautology and how do we detect it? Some of the ideas posted by Edward Z. Yang should definitely help you here in understanding what is going on. In this case, we could look at the clause[(True,"A"), (True,"B"), (False,"A")]and try to feed it toisTautologyfor testing it. Likewise with the other clause Edward posted,[(True,"B"), (True,"C"), (True,"A")].The trick in general is to figure out how to break up the functions into smaller constituents which are easily written and then afterwards glue these individual pieces together with code to solve the final problem. We are decomposing
removeTautologieswhich works on general formulae into a helperisTautologywhich can work on clauses in the formula, and then we seek to defineremoveTautologiesin terms of it via some filtering glue code.I hope this helps you start on your problem. It may seem to be quite irrelevant but do take note that more advanced variants of this is used in Model Checking algorithms which verify that your CPU is correct, that protocols behave and recently is has also been used in automatic refactoring, see http://coccinelle.lip6.fr/ for a use. So this problem is a good way to learn some serious applicability in the real world!
I’ll edit it here to help you rather than replying in the comment section. You wrote:
There are a couple of problems with this approach as you mention. First, the game is that your
rtfunction is working on clauses. If the given clause is a tautology it should be removed, so it would be better to call itisTautologywith the type I mention above, or perhaps simply:The path you have taken requires you to sort the list in the clause lexicographically and then consider what to do in the case you have
[P, P, not P, Q]for instance. Another approach is to establish a search. Suppose we haveNotice that if the value
(not tv, name)is present inrestthis clause must be a tautology. Otherwise, we can throw away(tv, name)and look for a tautology inrest.Moving the focus to
removeTautologies, it is clear that the function can be written usingisRemovableClause: A formula is a list of clauses, so we can simply walk through the clause-list and remove all those for whichisRemovableClausereturns true. The bold solver will useList.filter, a higher order function, to achieve this.