簡體   English   中英

haskell:析取范式函數

[英]haskell: a disjunctive normal form function

我正在嘗試創建一個將命題轉換為析取范式的函數。

它將通過閱讀“評估”列表來做到這一點:

type Valuation = [(Variable, Bool)] -- Valuation of variables to truth values 

並使用這些:

findBool :: (Variable, Bool)->Bool
findBool (_,x) = x

findVar :: (Variable, Bool)->Variable
findVar (x,_) = x

命題還包括:

data Prop = Falsum         -- a contradiction, or
          | Var Variable   -- a variable, or
          | Not Prop       -- a negation of a formula, or
          | Or  Prop Prop  -- a disjunction of two formulae, or
          | And Prop Prop  -- a conjunction of two formulae, or
          | Imp Prop Prop  -- a conditional of two formulae.
            deriving (Eq, Show)

我認為到目前為止我已經很扎實了,但是我看不到如何處理空箱。 這是我到目前為止的內容:

minterm :: Valuation -> Prop
minterm [] = ?
minterm (x:xs) = if (findBool x) then (And (findVar x) (minterm xs)) else (And (Not(findVar x) (minterm xs)) 

我的目標是: minterm [("p",True),("q",False)]返回: And (Var "p") (Not (Var "q"))

編輯:不是Falsum的作品,但如果它不返回任何東西,我更希望。 無論如何,我可以排除將其退回的情況,以便獲得如下信息:

minterm [("p",True),("q",False)] == And (Var "p") (Not (Var "q"))

不返回任何內容的標准方法是返回Nothing

minterm :: Valuation -> Maybe Prop
minterm []     = Nothing
minterm (x:xs) =
    Just $ case minterm xs of
             Nothing -> y
             Just mt -> And y mt
  where
    y = if (findBool x)
            then findVar x
            else Not $ findVar x

請注意,現在您的頂級類型簽名將反映此失敗概念。

根據您的編輯,是否可能只想對結尾進行特殊處理以進行整理?

minterm :: Valuation -> Prop
minterm [] = Not Falsum
minterm [x] = oneTerm x
minterm (x : xs) = And (oneTerm x) (minterm xs)

oneTerm (x, True) = Var x
oneTerm (x, False) = Not (Var x)

您可以更改minterm函數以返回條款列表而不是條款:

minterm :: Valuation -> [Prop]
minterm [] = []
minterm (x:xs) = if (findBool x) then findVar x : minterm xs else Not (findVar x)  : minterm xs

然后編寫另一個函數將其轉換為Prop:

and :: [Prop] -> Prop
and [] = Not Falsum
and xs = foldr1 And xs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM