简体   繁体   English

纯 Prolog Peano Number Apartness

[英]Pure Prolog Peano Number Apartness

Lets assume there is pure_2 Prolog with dif/2 and pure_1 Prolog without dif/2.让我们假设 pure_2 Prolog 有 dif/2 和 pure_1 Prolog 没有 dif/2。 Can we realize Peano apartness for values, ie Peano numbers, without using dif/2?我们可以在不使用 dif/2 的情况下实现值的 Peano 分离性,即 Peano 数吗? Thus lets assume we have Peano apartness like this in pure_2 Prolog:因此,假设我们在 pure_2 Prolog 中有这样的 Peano 公寓:

/* pure_2 Prolog */
neq(X, Y) :- dif(X, Y).

Can we replace neq(X,Y) by a more pure definition, namely from pure_1 Prolog that doesn't use dif/2?我们能否用更纯粹的定义替换 neq(X,Y),即来自不使用 dif/2 的 pure_1 Prolog? So that we have a terminating neq/2 predicate that can decide inequality for Peano numbers?这样我们就有了一个终止 neq/2 谓词来决定 Peano 数的不等式? So what would be its definition?那么它的定义是什么?

/* pure_1 Prolog */
neq(X, Y) :- ??

Using less from this comment :从此评论中使用less

less(0, s(_)).
less(s(X), s(Y)) :- less(X, Y).

neq(X, Y) :- less(X, Y); less(Y, X).

I had something else in mind, which is derived from two of the Peano Axioms , which is also part of Robinson Arithmetic.我想到了其他一些东西,它来自两个Peano Axioms ,这也是 Robinson Arithmetic 的一部分。 The first axiom is already a Horn clause talking about apartness:第一个公理已经是一个关于分离性的霍恩子句:

   ∀x(0 ≠ S(x)) 

   ∀x∀y(S(x) = S(y) ⇒ x = y)

Applying contraposition to the second axiom gives.将对立应用于第二个公理给出。
The axiom is now a Horn clause talking about apartness:该公理现在是一个关于分离性的霍恩子句:

   ∀x∀y(x ≠ y ⇒ S(x) ≠ S(y))

Now we have everything to write some Prolog code.现在我们已经准备好编写一些 Prolog 代码了。
Adding some symmetry we get:添加一些对称性,我们得到:

neq(0, s(_)).
neq(s(_), 0).
neq(s(X), s(Y)) :- neq(X, Y).

Here are some example queries.以下是一些示例查询。 Whether the predicate leaves a choice谓词是否留下选择
point depends on the Prolog system.点取决于 Prolog 系统。 I get:我得到:

SWI-Prolog 8.3.15 (some choice point): SWI-Prolog 8.3.15(一些选择点):

?- neq(s(s(0)), s(s(0))).
false.

?- neq(s(s(0)), s(0)).
true ;
false.

Jekejeke Prolog 1.4.6 (no choice point): Jekejeke Prolog 1.4.6(无选择点):

?- neq(s(s(0)), s(s(0))).
No

?- neq(s(s(0)), s(0)).
Yes

Just removing the unwanted choicepoint (in swi-prolog) from user502187's answer:只需从 user502187 的回答中删除不需要的选择点(在 swi-prolog 中):

neq(0, s(_)).
neq(s(N), M) :-
    % Switch args, to use first-arg indexing
    neq_(M, s(N)).

neq_(0, s(_)).
neq_(s(N), s(M)) :-
    % Switch args back, to fix choicepoint
    neq(M, N).

Results in swi-prolog: swi-prolog 中的结果:

?- neq(s(s(0)), s(0)).
true.

?- neq(s(0), s(s(0))).
true.

?- neq(N, M).
N = 0,
M = s(_) ;
N = s(_),
M = 0 ;
N = s(s(_)),
M = s(0) ;
N = s(0),
M = s(s(_)) ;
N = s(s(0)),
M = s(s(s(_))) ;
N = s(s(s(_))),
M = s(s(0)) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM