简体   繁体   English

Prolog是否有像Haskell这样的别名“运算符”?

[英]Does Prolog have an alias “operator” like Haskell?

In Haskell, there is a language feature called "as"-operator (sometimes known as alias). 在Haskell中,有一种称为“as”-operator的语言特性(有时称为别名)。 The idea is the following: given you have a function that for instance takes as input a list and wants to return all tails, you can implement this as: 这个想法如下:假设您有一个函数,例如将列表作为输入并希望返回所有尾部,您可以将其实现为:

tails a@(_:xs) = a : tails xs
tails [] = [[]]

The @ ensures that you have a reference to both the entire argument as well as a reference to some parts of the structure of the parameter. @确保您既可以引用整个参数,也可以引用参数结构的某些部分。 This is intelligent performance-wise (it is more a performance hack, because reconstructing an array (x:xs) ) in the body of the first line, will - if not optimized by the compiler - result in allocating a new object, modifying fields, etc. See here for more information. 这是智能性能(它更像是性能破解,因为在第一行的主体中重构数组(x:xs) ),如果没有被编译器优化,将导致分配新对象,修改字段等。有关更多信息,请参见此处

I was wondering if Prolog has something equivalent: for instance if you want to implement tails in Prolog, it can be done the following way: 我想知道Prolog是否有相同的东西:例如,如果你想在Prolog中实现尾部,可以通过以下方式完成:

tails([H|T],[[H|T]|TA]) :-
    tails(T,TA).
tails([],[[]]).

But it could be more efficient if there was an "as"-operator like: 但如果有一个“as”运算符,它可能会更有效:

tails(L@[_|T],[L|TA]) :-  %This does not compile
    tails(T,TA).
tails([],[[]]).

Is there any such construct, or a language extension? 有没有这样的结构或语言扩展?

TL;DR: Good idea 1 ! TL; DR:好主意1 The speedup appears to be limited to ~20% (for most list sizes). 加速似乎限制在约20%(对于大多数列表大小)。

In this answer, we compare three different predicates that differ regarding @ -like data reuse: 在这个答案中,我们比较了三个不同的谓词,它们在@ -like数据重用方面有所不同:

list_tails([], [[]]).                % (1) like `tails/2` given by the OP ...
list_tails([E|Es], [[E|Es]|Ess]) :-  %     ....... but with a better name :-)
   list_tails(Es, Ess).

list_sfxs1(Es, [Es|Ess]) :-          % (2) "re-use, mutual recursion"
   aux_list_sfxs1(Es, Ess).          %     "sfxs" is short for "suffixes"

aux_list_sfxs1([], []).
aux_list_sfxs1([_|Es], Ess) :-
   list_sfxs1(Es, Ess).

list_sfxs2([], [[]]).                % (3) "re-use, direct recursion"
list_sfxs2(Es0, [Es0|Ess]) :-
   Es0 = [_|Es],
   list_sfxs2(Es, Ess).

To measure runtime, we use the following code: 要测量运行时,我们使用以下代码:

:-( dif(D,sicstus), current_prolog_flag(dialect,D)
  ; use_module(library(between))
  ).

run_benchs(P_2s, P_2, L, N, T_ms) :-
   between(1, 6, I),
   L is 10^I,
   N is 10^(8-I),
   length(Xs, L),
   member(P_2, P_2s),
   garbage_collect,
   call_walltime(run_bench_core(P_2,Xs,N), T_ms).

run_bench_core(P_2, Xs, N) :-
   between(1, N, _),
   call(P_2, Xs, _),
   false.
run_bench_core(_, _, _).

To measure 2 , we utilize call_ walltime /2 —a variation of call_time/2 : 为了测量 2,我们利用call_ walltime /2 2的变异-a call_time/2

call_walltime(G, T_ms) :-
   statistics(walltime, [T0|_]),
   G,
   statistics(walltime, [T1|_]),
   T_ms is T1 - T0.

Let's put above code variations to the test... 让我们将上面的代码变体放到测试中......

  • ...using different list lengths L ... ...使用不同的列表长度L ...
  • ...and running each test a number of times N (for better accuracy). ...并且每次测试运行N次(为了更好的准确性)。

First, we use version 7.3.14 (64-bit): 首先,我们使用版本7.3.14(64位):

?- run_benchs([list_sfxs1,list_sfxs2,list_tails], P_2, L, N, T_ms).
   P_2 = list_sfxs1, L*N = 10*10000000, T_ms =  7925
;  P_2 = list_sfxs2, L*N = 10*10000000, T_ms =  7524
;  P_2 = list_tails, L*N = 10*10000000, T_ms =  6936
;
   P_2 = list_sfxs1, L*N = 100*1000000, T_ms =  6502
;  P_2 = list_sfxs2, L*N = 100*1000000, T_ms =  5861
;  P_2 = list_tails, L*N = 100*1000000, T_ms =  5618
;
   P_2 = list_sfxs1, L*N = 1000*100000, T_ms =  6434
;  P_2 = list_sfxs2, L*N = 1000*100000, T_ms =  5817
;  P_2 = list_tails, L*N = 1000*100000, T_ms =  9916
;
   P_2 = list_sfxs1, L*N = 10000*10000, T_ms =  6328
;  P_2 = list_sfxs2, L*N = 10000*10000, T_ms =  5688
;  P_2 = list_tails, L*N = 10000*10000, T_ms =  9442
;
   P_2 = list_sfxs1, L*N = 100000*1000, T_ms = 10255
;  P_2 = list_sfxs2, L*N = 100000*1000, T_ms = 10296
;  P_2 = list_tails, L*N = 100000*1000, T_ms = 14592
;
   P_2 = list_sfxs1, L*N = 1000000*100, T_ms =  6955
;  P_2 = list_sfxs2, L*N = 1000000*100, T_ms =  6534
;  P_2 = list_tails, L*N = 1000000*100, T_ms =  9738.

Then, we repeat the previous query 3 using version 4.3.2 (64-bit): 然后,我们使用版本4.3.2(64位)重复上一个查询3

?- run_benchs([list_sfxs1,list_sfxs2,list_tails], P_2, L, N, T_ms).
   P_2 = list_sfxs1, L*N = 10*10000000, T_ms =  1580
;  P_2 = list_sfxs2, L*N = 10*10000000, T_ms =  1610
;  P_2 = list_tails, L*N = 10*10000000, T_ms =  1580
;
   P_2 = list_sfxs1, L*N = 100*1000000, T_ms =   710
;  P_2 = list_sfxs2, L*N = 100*1000000, T_ms =   750
;  P_2 = list_tails, L*N = 100*1000000, T_ms =   840
;
   P_2 = list_sfxs1, L*N = 1000*100000, T_ms =   650 
;  P_2 = list_sfxs2, L*N = 1000*100000, T_ms =   660
;  P_2 = list_tails, L*N = 1000*100000, T_ms =   740
;  
   P_2 = list_sfxs1, L*N = 10000*10000, T_ms =   620
;  P_2 = list_sfxs2, L*N = 10000*10000, T_ms =   650
;  P_2 = list_tails, L*N = 10000*10000, T_ms =   740
;
   P_2 = list_sfxs1, L*N = 100000*1000, T_ms =   670
;  P_2 = list_sfxs2, L*N = 100000*1000, T_ms =   650
;  P_2 = list_tails, L*N = 100000*1000, T_ms =   750
;
   P_2 = list_sfxs1, L*N = 1000000*100, T_ms = 12610
;  P_2 = list_sfxs2, L*N = 1000000*100, T_ms = 12560
;  P_2 = list_tails, L*N = 1000000*100, T_ms = 33460.

Summary: 摘要:

  • The alias-thingy can and does improve performance significantly. 别名可以并且确实显着提高了性能
  • In above tests, the SICStus Prolog 4 gives 10X speedup , compared to SWI-Prolog! 在上面的测试中,与SWI-Prolog相比,SICStus Prolog 4的 速度提高10倍

Footnote 1: Why do the stunt of putting (@)/2 in the rule head? 脚注1:为什么把(@)/2放在规则头上的噱头? To end up with non- idiomatic Prolog code? 最终得到非惯用的 Prolog代码?
Footnote 2: We are interested in the total runtime. 脚注2:我们对总运行时间感兴趣。 Why? 为什么? Because garbage-collection costs show with larger data sizes! 因为垃圾收集成本显示更大的数据量!
Footnote 3: The answer sequence has been post-processed for the sake of readability. 脚注3:为了便于阅读,答案序列已经过后处理。
Footnote 4: Available since release 4.3.0 . 脚注4:4.3.0版以来可用。 Current target architectures include IA-32 and AMD64 . 目前的目标架构包括IA-32AMD64

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

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