开发者

Haskell procedure

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 A开发者_JS百科toms 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.


One place to get you started: look at

 removeTautologies :: Formula -> Formula

Now suppose we can write an function

 isTautology :: Clause -> Bool

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 to isTautology for 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 removeTautologies which works on general formulae into a helper isTautology which can work on clauses in the formula, and then we seek to define removeTautologies in 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:

rt ((x, x') : (y, y') : rest) | x' == y' = rt rest
                              | otherwise = (x, x') : rt ((y, y') : rest)

There are a couple of problems with this approach as you mention. First, the game is that your rt function is working on clauses. If the given clause is a tautology it should be removed, so it would be better to call it isTautology with the type I mention above, or perhaps simply:

isRemovableClause :: Clause -> Bool

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 have

isRemovableClause ((tv, name) : rest) = ...

Notice that if the value (not tv, name) is present in rest this clause must be a tautology. Otherwise, we can throw away (tv, name) and look for a tautology in rest.

Moving the focus to removeTautologies, it is clear that the function can be written using isRemovableClause: A formula is a list of clauses, so we can simply walk through the clause-list and remove all those for which isRemovableClause returns true. The bold solver will use List.filter, a higher order function, to achieve this.


This question is too broad: it might be marginally OK if you focused on one particular function that you needed help on, but really, in order for us to effectively, you need to give us more than just the specification of what the code should to: with only that, this is just a "Do my homework for me" problem.

That being said, I recommend taking the examples you posted in your descriptions and converting them to your representation (I assume you're using CNF?) Then you'll have something along the lines of

(A v B v -A) ^ (B v C v A)

becomes

[[(True,"A"), (True,"B"), (False,"A")],
 [(True,"B"), (True,"C"), (True,"A")]]

and then think about what the resulting data type looks like, and how you might get there from a strictly computational perspective. Don't worry about performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜