![]() |
1
5
忘记变量,想想
原因何在
更新
(固定)
如果您对一般逻辑语言感兴趣,您也可以查看curry语言(所有非纯函数都是“挂起的”,直到非yed定义的值被统一为止)。 |
![]() |
2
7
对于可逆算法,使用prolog系统的 约束解算器 .
例如,swi prolog的
CLP(FD) manual
包含以下定义
下面的示例查询显示它可以在所有方向上使用:
当然,这个定义仍然依赖于统一,因此您不能插入任意整数表达式。一个术语
示例查询及其结果:
|
![]() |
3
5
在这个答案中,我们使用 clpfd ,就像 this previous answer 做。
为了便于进行头对头比较(稍后),我们将这里介绍的谓词称为
像在
上一个答案
,
辅助谓词
让我们比较一下
好啊! 完全一样,到目前为止…为什么不做 一点 强力测试? ?- time((F1 #\= F2,n_factorial(N,F1),n_fac(N,F2))). % 57,739,784 inferences, 6.415 CPU in 7.112 seconds (90% CPU, 9001245 Lips) % Execution Aborted ?- time((F1 #\= F2,n_fac(N,F2),n_factorial(N,F1))). % 52,815,182 inferences, 5.942 CPU in 6.631 seconds (90% CPU, 8888423 Lips) % Execution Aborted ?- time((N1 #> 1,N2 #> 1,N1 #\= N2,n_fac(N1,F),n_factorial(N2,F))). % 99,463,654 inferences, 15.767 CPU in 16.575 seconds (95% CPU, 6308401 Lips) % Execution Aborted ?- time((N1 #> 1,N2 #> 1,N1 #\= N2,n_factorial(N2,F),n_fac(N1,F))). % 187,621,733 inferences, 17.192 CPU in 18.232 seconds (94% CPU, 10913552 Lips) % Execution Aborted
无差异
前几百个值
继续:下面怎么样(在评论中建议 this answer )?
干得好! 相同的终止行为…更多?
好吧! 仍然相同的终止属性!我们再深入一点!下面怎么样? ?- F in inf..10, n_factorial(_,F), false. ... % Execution Aborted % does not terminate universally ?- F in inf..10, n_fac(_,F), false. false. % terminates universally 哦! 第一个查询不终止,第二个查询终止。 多快啊!:) 让我们做一些经验运行时测量! ?- member(Exp,[6,7,8,9]), F #= 10^Exp, time(n_factorial(N,F)) ; true. % 328,700 inferences, 0.043 CPU in 0.043 seconds (100% CPU, 7660054 Lips) % 1,027,296 inferences, 0.153 CPU in 0.153 seconds (100% CPU, 6735634 Lips) % 5,759,864 inferences, 1.967 CPU in 1.967 seconds (100% CPU, 2927658 Lips) % 22,795,694 inferences, 23.911 CPU in 23.908 seconds (100% CPU, 953351 Lips) true. ?- member(Exp,[6,7,8,9]), F #= 10^Exp, time(n_fac(N,F)) ; true. % 1,340 inferences, 0.000 CPU in 0.000 seconds ( 99% CPU, 3793262 Lips) % 1,479 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 6253673 Lips) % 1,618 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 5129994 Lips) % 1,757 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 5044792 Lips) true. 真的! 还有一些吗? ?- member(U,[10,100,1000]), time((N in 1..U,n_factorial(N,_),false)) ; true. % 34,511 inferences, 0.004 CPU in 0.004 seconds (100% CPU, 9591041 Lips) % 3,091,271 inferences, 0.322 CPU in 0.322 seconds (100% CPU, 9589264 Lips) % 305,413,871 inferences, 90.732 CPU in 90.721 seconds (100% CPU, 3366116 Lips) true. ?- member(U,[10,100,1000]), time((N in 1..U,n_fac(N,_),false)) ; true. % 3,729 inferences, 0.001 CPU in 0.001 seconds (100% CPU, 2973653 Lips) % 36,369 inferences, 0.004 CPU in 0.004 seconds (100% CPU, 10309784 Lips) % 362,471 inferences, 0.036 CPU in 0.036 seconds (100% CPU, 9979610 Lips) true. 底线是什么?
|
![]() |
4
3
在查看Prolog时,您必须记住一些事情:
这就是为什么你需要额外的变量
(是的,我知道您不在Prolog中进行调用,而是评估目标,但我们从功能角度出发) |