哈斯克尔:否定范式函数得到"Non-exhaustive pattern"异常


-- | data type definition of WFF: well formed formula
data Wff = Var String 
        | Not Wff
        | And Wff Wff 
        | Or Wff Wff
        | Imply Wff Wff
-- | Negation norm form  nnf function
--   precondition: φ is implication free
--   postcondition: NNF (φ) computes a NNF for φ
nnf :: Wff -> Wff
nnf (Var p) = Var p
nnf (Not (Not p)) = (nnf p)
nnf (And p q) = And (nnf p) (nnf q)
nnf (Or p q) = Or (nnf p) (nnf q)
nnf (Not (And p q)) = Or (nnf(Not p)) (nnf(Not q))
nnf (Not (Or p q)) = And (nnf(Not p)) (nnf(Not q))

测试用例:¬(p ∨ Q(

(*** Exception:: Non-exhaustive patterns in function nnf

但是,如果我将nnf (Not p) = Not (nnf p)添加到函数中,它将显示

Pattern match(es) are overlapped
In an equation for ‘nnf’:
    nnf (Not (Not p)) = ...
    nnf (Not (And p q)) = ...
    nnf (Not (Or p q)) = ...

我想知道我做错了什么?

您只是在错误的位置插入了该行。 nnf (Not p) = ...是否定的统称。如果你稍后添加其他处理更具体的否定的子句,如Not (And p q),它们不可能再触发了。

包罗万象的条款需要排在最后,即

nnf (Var p) = Var p
nnf (Not (Not p)) = (nnf p)
nnf (And p q) = And (nnf p) (nnf q)
nnf (Or p q) = Or (nnf p) (nnf q)
nnf (Not (And p q)) = Or (nnf $ Not p) (nnf $ Not q)
nnf (Not (Or p q)) = And (nnf $ Not p) (nnf $ Not q)
nnf (Not p) = Not $ nnf p

相关内容

  • 没有找到相关文章

最新更新