2011-02-01 52 views
1

我有2问题约2 Haskell函数2 Haskell的问题

  1. flipSymbol ::模型 - >原子 - >模型该功能必须采取模型和原子和翻转在原子的真值模型。现在我想写这样的功能是这样的:...

    flipSymbol m a = map f m 
        where 
        f (atom, value) = if a == atom then (atom, not value) else (atom, value)

    有没有更好的方法?

  2. 第二个是一个更复杂的东西,我需要一些帮助,但如果可能的话.. 为了检查一个公式的可满足在我们传播到原子分配真值的影响,给定模型在一个公式中。假设我们为其指定值True的原子。以下效果 可以应用到下式:

    • 正文字具有相同的真值,因此,包含它们的任何条款从式除去。这表明这些条款可以得到满足,因此不再影响公式的可满足性。
    • 否定字面值为False,因此从它们所在的任何子句中移除。这表示这些子句仍然不满足,只能通过其他文字之一才能获得值的真实。如果将False指定给原子,则正文本现在将是错误的,应从其子句中删除 ,否定文字将变为true,并将其子句从公式中删除。例如,在公式(P_Q_R)^(:P_Q_R)^(P_:Q)中,假设我们将真分配给P.然后,包含P的子句,即。 (P _ Q _ R)和(P _:Q)从公式中移除,而:P从它所在的任何子句中移除,即。 (:P _ Q _:R)。这导致公式(Q_:R)。另一方面,如果我们将假赋予P,则我们从公式中去掉(:P _ Q _:R),从它的子句中去掉P,从而得到(Q_R)^(:Q)。
      如果可以将整个公式缩减为空列表,则整个公式是可以满足的,因为在这种情况下,所有条款都满足了。如果在整个公式中有一个空列表,那么这意味着一个条款不被满足,因此公式不能满足导致这个状态的任务。
    • assign :: (Atom,Bool) -> Formula -> Formula赋值函数应该采用(Atom,Bool)对和一个公式,并将给定的真值分配给公式中的原子,如上所述。

的代码(关于这一点我收到的帮助从这里也可以):

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] 
atomsClause = undefined 

-- This function takes a Formula returns the set of Atoms of a Formula 
atoms :: Formula -> [Atom] 
atoms = nub . map snd 

-- This function returns True if the given Literal can be found within 
-- the Clause. 
isLiteral :: Literal -> Clause -> Bool 
isLiteral = isLiteral = any . (==) 

-- 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? 
flipSymbol m a = map f m where 
    f (atom, value) = if a == atom 
     then (atom, not value) 
     else (atom, value) 

assign :: (Atom,Bool) -> Formula -> Formula 
assign = undefined --any advice here? 
+0

请使用4个缩进来格式化您的代码。 – 2011-02-01 11:17:06

+1

atomsClause = nub。 map snd被定义为 – TKFALS 2011-02-01 11:17:43

回答

2

一目了然,我看不出有任何的方式来提高你的第一个公式,也许你可以使用逻辑功能,而不是一个if-then-else,它的速度更快:

flipSymbol m a = map f m where 
    f (atom, value) = (atom, value /= (a == atom)) 

注意:/=Bool基本上是XOR。

回到最后一个问题: 基本的想法是比较原子,并结合Bool值并且用逻辑运算来获得结果。基本上,它看起来像这样:

assign :: (Atom,Bool) -> Formula -> Formula 
assign (a,b) = map . (map f) where 
    f (x,b) = (x,(x==a)&&b)