Problem with foldl/4 and library(apply_macros)

How can I keep up via higher order programming (Haskell style)
with the performance of hand written loops (pre call/n Prolog)?

I tried these tests:

sum([], X, X).
sum([H|T], X, Y) :- plus(X,H,Z), sum(T, Z, Y).

test :- time((between(1,100000,_), 
     sum([1,2,3,4,5,6,7,8,9], 0, _), fail; true)).

test2 :- time((between(1,100000,_), 
     foldl(plus, [1,2,3,4,5,6,7,8,9], 0, N), fail; true)).

Even when I have library(apply_macros) loaded,
the hand written loop is still faster:

?- test.
% 2,000,001 inferences, 0.063 CPU in 0.076 seconds
true.

?- test2.
% 2,100,141 inferences, 0.125 CPU in 0.122 seconds 
true.

Actually test is twice as fast as test2 !

foldl is not part of library(apply_macros), you can see that it is not there.

Nine years ago :smiley: I started making a PR for foldl but for reasons I didn’t finish it. It would take just a little bit of work to get it finalized.

2 Likes

Happy to accept it. The long term plan is to support inline and make it sufficiently powerful to deal with these meta predicates. A good inliner is not easy to implement and it is not easy to see when inlining would help improving performance and when it actually slows down things. I do think that is the right direction though. library(apply_macros) easily gets far too complicated.