我在传递给函数的参数上使用模式匹配。可以说,该方法对于“第一级”匹配效果很好,但是任何更深入的尝试都会给出错误“ stdIn:282.5-291.77 Error: match冗余”
例子
fun nnf T = T
| nnf F = F
| nnf (LETTER(x)) = (LETTER(x))
| nnf (NEG(x)) = (NEG(nnf x))
| nnf (AND(x,y)) = (AND(nnf x, nnf y))
| nnf (OR(x,y)) = (OR(nnf x, nnf y))
| nnf (IMP(x,y)) = (OR(NEG(nnf x),(nnf y)))
| nnf (NEG(NEG(LETTER(x)))) = (LETTER(x))
| nnf (NEG(AND(LETTER(x),LETTER(y)))) = (OR(NEG(LETTER(x)),NEG(LETTER(y))))
| nnf (NEG(OR(LETTER(x),LETTER(y)))) = (AND(NEG(LETTER(x)),NEG(LETTER(y))));
val nnf = fn : prop -> prop
我得到的错误是
stdIn:282.5-291.77 Error: match redundant
T => ...
F => ...
LETTER x => ...
NEG x => ...
AND (x,y) => ...
OR (x,y) => ...
IMP (x,y) => ...
--> NEG (NEG (LETTER x)) => ...
--> NEG (AND (LETTER x,LETTER y)) => ...
--> NEG (OR (LETTER x,LETTER y)) => ...
所以 SML 说函数定义中的最后 3 个子句是相同的,因为它们都以“(NEG(...”)开头,即使后面的内容不同。
我该如何克服呢?