Prolog如何在使用succ的递归查询中运行? [英] How does prolog run through recursive queries using succ?
问题描述
有人可以向我解释为什么此序言查询以这种方式起作用.定义是:
add(0,Y,Y).
add(succ(X),Y,succ(Z)):- add(X,Y,Z).
为此:
?- add(succ(succ(succ(0))), succ(succ(0)), R).
在此处查询查询:
Call: (6) add(succ(succ(succ(0))), succ(succ(0)), R)
Call: (7) add(succ(succ(0)), succ(succ(0)), _G648)
Call: (8) add(succ(0), succ(succ(0)), _G650)
Call: (9) add(0, succ(succ(0)), _G652)
Exit: (9) add(0, succ(succ(0)), succ(succ(0)))
Exit: (8) add(succ(0), succ(succ(0)), succ(succ(succ(0))))
Exit: (7) add(succ(succ(0)), succ(succ(0)),
succ(succ(succ(succ(0)))))
Exit: (6) add(succ(succ(succ(0))), succ(succ(0)),
succ(succ(succ(succ(succ(0))))))
令我最困惑的是,在第一个参数中,succ被剥离,然后递归.在递归的同时,R获得成功……怎么办?!另外,零在哪里从第一个出口(9)来?我是新手,我正在尝试了解家庭作业的语言.任何帮助表示赞赏.
注意:对于感兴趣的任何人,本教程的链接都是 http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse9
零从第一个出口(9)到哪里?"
调用add(0, succ(succ(0)), _G652)
与第一个子句统一,该子句说如果add
的第一个参数为零,则第二个和第三个相同.在这种特定情况下,变量_G652
变为succ(succ(0))
.
虽然递归,但R获得成功...如何?!"
这是应用第二个子句的结果.此子句说明(粗略地)您首先从第一个参数中除去succ
,然后递归调用add
,最后,在此递归调用返回的第三个参数中添加succ
的另一个层"./p>
谓词add
只是Peano算术中加法的直接实现: http://en.wikipedia.org/wiki/Peano_axioms#Addition
Can someone explain to me why this prolog query works the way it does. The definition is:
add(0,Y,Y).
add(succ(X),Y,succ(Z)):- add(X,Y,Z).
Given this:
?- add(succ(succ(succ(0))), succ(succ(0)), R).
Heres the trace of the query:
Call: (6) add(succ(succ(succ(0))), succ(succ(0)), R)
Call: (7) add(succ(succ(0)), succ(succ(0)), _G648)
Call: (8) add(succ(0), succ(succ(0)), _G650)
Call: (9) add(0, succ(succ(0)), _G652)
Exit: (9) add(0, succ(succ(0)), succ(succ(0)))
Exit: (8) add(succ(0), succ(succ(0)), succ(succ(succ(0))))
Exit: (7) add(succ(succ(0)), succ(succ(0)),
succ(succ(succ(succ(0)))))
Exit: (6) add(succ(succ(succ(0))), succ(succ(0)),
succ(succ(succ(succ(succ(0))))))
The part that confused me the most about that tutorial was the fact that in the first argument, the succ is stripped, and it recurses. While recursing though, R gains a succ... HOW?! Also, where does the zero come from at the first exit (9)? I am new to prolog, and I am trying to understand the language for a homework. Any help much appreciated.
Note: for anyone interested, the link to this tutorial is http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse9
"where does the zero come from at the first exit (9)?"
The call add(0, succ(succ(0)), _G652)
is unified with the first clause that says that if the first argument of add
is zero, then the second and the third are the same. In this particular situatiob variable _G652
becomes succ(succ(0))
.
"While recursing though, R gains a succ... HOW?!"
This is the result of the application of the second clause. This clause states (roughly) that you first strip succ
from the first argument, then call add
recursively, and, finally, add another "layer" of succ
to the third argument coming back from this recursive call.
The predicate add
is nothing but a direct implementation of addition in Peano arithmetics: http://en.wikipedia.org/wiki/Peano_axioms#Addition
这篇关于Prolog如何在使用succ的递归查询中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!