f(s(s(s(s(s(1)))))),C)如何在Prolog上工作?

时间:2017-07-25 15:32:56

标签: prolog

我在教科书上练习,在看到结果时无法找到原因。

在prolog数据库中,它显示

f(1,one).
f(s(1),two).
f(s(s(1)),three).
f(s(s(s(X))),N) :- f(X,N).

当我用

运行程序时
f(s(s(s(s(s(s(1)))))),C).

程序的响应是" C = 1。"

它是如何运作的?

1 个答案:

答案 0 :(得分:2)

Prolog非常简单。其计划包括表格规则

to_prove_this :- must_prove_this, and_this.          % and perhaps also,
to_prove_this :- must_otherwise_prove_this, and_this_too.

所以你的程序只是意味着

1. to prove `f( 1, one)` :- there's no need to prove anything more.
2. to prove `f( s(1), two)` :- there's no need to prove anything more.
3. to prove `f( s(s(1)), three)` :- there's no need to prove anything more.
4. to prove `f( s(s(s(X))), N)` :- must prove `f( X, N)`.

所以你从

开始
to prove:    f( s(s(s(s(s(s(1)))))), C).

可以使用规则1.吗?

| Is `f( s(s(s(s(s(s(1)))))), C)` similar to `f(1,one)`?  
| | Is `f` similar to `f`?  
| | -- Yes.  
| | Is `s(s(s(s(s(s(1))))))` similar to `1`?  
| | -- No.  
| -- No, `f( s(s(s(s(s(s(1)))))), C)` and `f(1,one)` are not similar.  
-- No, the rule 1. can't be used.  

可以使用规则2.吗?

| Is `f( s(s(s(s(s(s(1)))))), C)` similar to `f(s(1),two)`?  
. . . . .  
. . . . .  
. . . . .  

可以使用规则4.吗?

| Is `f( s(s(s(s(s(s(1)))))), C)` similar to `f(s(s(s(X))),N)`?  
| | Is `f` similar to `f`?  
| | -- Yes.  
| | Is `s(s(s(s(s(s(1))))))` similar to `s(s(s(X)))`?  
| | | Is `s(s(s(s(s(1)))))` similar to `s(s(X))`?  
| | | | Is `s(s(s(s(1))))` similar to `s(X)`?  
| | | | | Is `s(s(s(1)))` similar to `X`?  
| | | | | -- Yes,                            with `X = s(s(s(1)))`.  
| | Is `C` similar to `N`?  
| | -- Yes,                                  with `C = N`.  
| -- Yes, it is similar,                     with `X = s(s(s(1)))` and `C = N`.  
-- Yes, it can be used,                      with `X = s(s(s(1)))` and `C = N`.

这意味着,我们现在需要f(X,N)X = s(s(s(1)))来证明C = N
这意味着,我们需要使用f(X1,N1)X1 = s(s(s(1)))来证明C = N1 这意味着,我们现在需要证明f( s(s(s(1))), C )

可以使用规则1.吗?

. . . .  
. . . .  
. . . .  

这意味着,我们现在需要f(X,N)X = 1来证明C = N
这意味着,我们需要使用f(X2,N2)X2 = 1来证明C = N2 这意味着,我们现在需要证明f( 1, C )

现在可以使用来规则1. 吗?