简体   繁体   English

Prolog 中的次、商和余数谓词

[英]Times, Quotient and Remainder predicates in Prolog

how can I do the following.我该怎么做以下。

I needed to define the predicate shownumber (X,N) , which is true when the symbol X corresponds to the natural number N. For example, shownumber(s(zero),1) is true.我需要定义谓词shownumber (X,N) ,当符号 X 对应于自然数 N 时,该谓词为真。例如, shownumber(s(zero),1)为真。 Okay, now I've got a predicate:好的,现在我有一个谓词:

shownumber (zero, 0).
shownumber (s (N), X): - shownumber (N, Y), X is Y + 1.

Now I need to use the shownumber (X, Y) predicate to define:现在我需要使用shownumber (X, Y)谓词来定义:

1) times (X, Y, Z) which is true if X * Y = Z. 1) times (X, Y, Z) ,如果 X * Y = Z,则为真。

2) quotient (X, Y, Q) which is true if X / Y = Q (in natural number arithmetic) 2) quotient (X, Y, Q)如果 X / Y = Q 则为真(在自然数算术中)

3) remainder (X, Y, R) which is true if X divided by Y gives the remainder R. 3) remainder (X, Y, R)如果 X 除以 Y 得到余数 R,则为真。

The quotient (X, Y, Q) and the remainder (X, Y, R) must be defined for Y = zero. quotient (X, Y, Q)remainder (X, Y, R)必须定义为 Y = 零。

How can I do that?我怎样才能做到这一点? Could you help me with this one?你能帮我做这个吗?

This code may help:此代码可能会有所帮助:

peano_redux.pl peano_redux.pl

It took me a long time to finish it and it still has bad corners.我花了很长时间才完成它,它仍然有坏的角落。 I tried to stay "close the Peano Axioms" but some shortcuts had to be taken - this is programming, not general theorem proving.我试图保持“关闭皮亚诺公理”,但必须采取一些捷径——这编程,而不是一般定理证明。

I also used the old-school & messy Peano notation s(s(s(s(z))) instead the far cleaner and appropriate list-based notation: [s,s,s,s] .我还使用了老式的、凌乱的 Peano 符号s(s(s(s(z)))代替了更清晰和适当的基于列表的符号: [s,s,s,s]

It would be extremely cool to be able to set up a constraint between two variables PN and NN, so that if NN is bound to a natural number, PN is automatically bound to the coressponding Peano Number, and vice-versa.能够在两个变量 PN 和 NN 之间建立一个约束是非常酷的,这样如果 NN 绑定到一个自然数,PN 就会自动绑定到对应的 Peano 数,反之亦然。 That can probably be done with some effort using attributed variables, but I haven't thought about that.这可能可以通过使用属性变量来完成,但我没有考虑过。

Computation is expectedly very slow.计算预计非常慢。 It would be interesting to try tabling on the padd/pmult predicates.尝试在 padd/pmult 谓词上建表会很有趣。

The bidirectional transformation between Peano Numbers and Naturals exists in two version: One using CLP(FD) and one using basic Prolog. Peano Numbers 和 Naturals 之间的双向转换存在两个版本:一个使用 CLP(FD),另一个使用基本 Prolog。 Comment out the version you don't want.注释掉你不想要的版本。

This is overall an interesting (but time-consuming exercise).这总体上是一个有趣的(但耗时的练习)。 It gives a lot of practice in debugging and thinking about control flow, and forces you to watch out for undesired unification, non-termination, early termination and special cases of bound/fresh variable configurations.它在调试和思考控制流方面提供了大量实践,并迫使您注意不希望的统一、不终止、提前终止和绑定/新变量配置的特殊情况。

It also highlights the absolute need to write unit tests as you program, to give you a scaffolding for progress,它还强调了在编程时编写单元测试的绝对必要性,为您提供进步的脚手架,

Seemingly innocuous rearrangements of code may cause a previously working Prolog program to fail or loop indefinitely for hard-to-explain reasons.由于难以解释的原因,看似无害的代码重新排列可能会导致以前工作的 Prolog 程序失败或无限循环。 Having test cases to get back on track avoids meaninglessly burning time on "fixing things until they work" and possibly "running tests by hand".让测试用例回到正轨可以避免在“修复问题直到它们工作”和可能的“手动运行测试”上浪费时间。

Unit tests are also of great use for communicating your problem's specification Programming courses should really orbit around that concept first - this isn't the 80s anymore.单元测试对于传达你的问题规范也很有用 编程课程应该首先围绕这个概念进行——现在已经不是 80 年代了。

Run all test cases by issuing command rtall for great success:通过发出命令rtall运行所有测试用例以获得巨大成功:

?- rt_all.
Correct to: "rtall"? yes
% PL-Unit: pm ................... done
% All 19 tests passed                                                                                                                                       
% PL-Unit: pnat ......... done                                                                                                                              
% All 9 tests passed                                                                                                                                        
% PL-Unit: pequal .... done                                                                                                                                 
% All 4 tests passed                                                                                                                                        
% PL-Unit: padd ................................. done                                                                                                      
% All 33 tests passed                                                                                                                                       
% PL-Unit: pless ................. done                                                                                                                     
% All 17 tests passed                                                                                                                                       
% PL-Unit: pmult .......................
% 1,649 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 8951351 Lips)                                                                                     
.
% 3,097 inferences, 0.000 CPU in 0.000 seconds (98% CPU, 10109979 Lips)                                                                                     
.
% 5,813 inferences, 0.001 CPU in 0.001 seconds (100% CPU, 11183341 Lips)                                                                                    
.
% 2,598 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 9577492 Lips)
.
% 768 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 8491724 Lips)
.
% 1,847 inferences, 0.000 CPU in 0.000 seconds (96% CPU, 9731501 Lips)
.
% 8,453,914 inferences, 0.668 CPU in 0.674 seconds (99% CPU, 12651865 Lips)
.
% 4,273 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 10987655 Lips)
.
% 8,389 inferences, 0.001 CPU in 0.001 seconds (99% CPU, 11702424 Lips)
.
% 12,506 inferences, 0.001 CPU in 0.001 seconds (100% CPU, 11900038 Lips)
.
% 45,453 inferences, 0.004 CPU in 0.004 seconds (100% CPU, 11844692 Lips)
. done
% All 34 tests passed
% PL-Unit: pqr ................Found: 16*13+8=216
Found: 37*12+8=452
Found: 7*53+1=372
Found: 28*7+13=209
Found: 33*14+6=468
Found: 23*5+19=134
Found: 21*3+3=66
Found: 31*8+1=249
Found: 14*20+9=289
Found: 5*2+4=14
Found: 4*9+0=36
Found: 30*3+6=96
Found: 40*11+16=456
Found: 11*4+8=52
Found: 10*12+3=123
Found: 18*20+0=360
Found: 5*61+2=307
Found: 46*2+0=92
Found: 1*215+0=215
Found: 47*7+30=359
. done
% All 17 tests passed
true.

Some references vaguely consulted:模糊地查阅了一些参考资料:

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

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