Functional programming 函数返回";“没有解决办法”;而不是",;“什么都没有”;
我有一个表示谓词逻辑公式的标准数据类型。表示析取的自然推断排除规则的函数可能如下所示:Functional programming 函数返回";“没有解决办法”;而不是",;“什么都没有”;,functional-programming,logic-programming,maybe,curry,Functional Programming,Logic Programming,Maybe,Curry,我有一个表示谓词逻辑公式的标准数据类型。表示析取的自然推断排除规则的函数可能如下所示: d_el p q = if p =: (Dis r s) && q =: (Neg r) then Just s else if q =: (Dis r s) && p =: (Neg r) then Just s else Nothing where r,s free x =: y = (x =:= y) == success 当统一失败时,该函数不返回
d_el p q =
if p =: (Dis r s) && q =: (Neg r) then Just s else
if q =: (Dis r s) && p =: (Neg r) then Just s else
Nothing where r,s free
x =: y = (x =:= y) == success
当统一失败时,该函数不返回任何结果,而是在PACKS
中不返回任何解决方案:
logic> d_el (Dis Bot Top) (Not Bot)
Result: Just Top
More Solutions? [Y(es)/n(o)/a(ll)] n
logic> d_el (Dis Bot Top) (Not Top)
No more solutions.
我遗漏了什么,为什么当统一失败时,
el
不计算为Nothing
。当a=:=b
失败时,complete function子句也会失败。例如: 计算
xx 7
会导致3
(而不是7
),因为7=:=5
完全终止xx
函数的第一个子句
我认为代码应该是这样的:
d_el p q = case (p,q) of
(Dis a s, Neg b) -> if a == b then Just s else Nothing
(Neg a, Dis b s) -> if a == b then Just s else Nothing
_ -> Nothing
我使用的语言是Curry,一种函数逻辑编程语言(见标签)。。。。无知可能会让人尴尬……正如你可能知道的,“咖喱”这个词在其他语言中也有意义(很明显,像哈斯凯尔语),所以也许你应该这样做。
d_el p q = case (p,q) of
(Dis a s, Neg b) -> if a == b then Just s else Nothing
(Neg a, Dis b s) -> if a == b then Just s else Nothing
_ -> Nothing