简体   繁体   English

haskell:析取范式函数

[英]haskell: a disjunctive normal form function

I'm trying to create a function which will change a proposition into disjunctive normal form. 我正在尝试创建一个将命题转换为析取范式的函数。

It will do this by reading in a list of Valuation : 它将通过阅读“评估”列表来做到这一点:

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

and using these: 并使用这些:

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

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

Also Proposition is: 命题还包括:

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)

I think what I have so far is pretty solid but I can't see what to do for the empty case. 我认为到目前为止我已经很扎实了,但是我看不到如何处理空箱。 Here's what I have so far: 这是我到目前为止的内容:

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

my goal is for: minterm [("p",True),("q",False)] to return: And (Var "p") (Not (Var "q")) 我的目标是: minterm [("p",True),("q",False)]返回: And (Var "p") (Not (Var "q"))

edit: Not Falsum works but I'd prefer if it didn't return anything. 编辑:不是Falsum的作品,但如果它不返回任何东西,我更希望。 Is there anyway I can exclude cases where it would be returned so I can get something like this: 无论如何,我可以排除将其退回的情况,以便获得如下信息:

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

The standard way to return nothing is to return Nothing . 不返回任何内容的标准方法是返回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

Notice that now your top-level type signature will reflect this notion of failure. 请注意,现在您的顶级类型签名将反映此失败概念。

Based on your edit, is it possible you just want to special case the ending to tidy it up? 根据您的编辑,是否可能只想对结尾进行特殊处理以进行整理?

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)

You could change your minterm function to return a list of clauses instead of a term: 您可以更改minterm函数以返回条款列表而不是条款:

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

and then write another function to convert it into a Prop: 然后编写另一个函数将其转换为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