Six ways to iterate in Prolog

LOL
Have to remember that next time I read the book. Great book by the way.

In “Prolog Programming for Artificial Intelligence” 4th Edition by Ivan Bratko (WorldCat), he does something similar where he has the students create conc/3 early on and uses it through out the book. However conc/3 is really append/3 and on StackOverflow those who use the book and don’t know about append/3 use their version of conc/3 which is often incorrect and has problems. Sometimes just changing their code to use append/3 instead of conc/3 solves their problem.

What is wrong with \? I’ve seen that remark before, but AFAIK, SWI-Prolog’s handling of \ is completely standard.

Most code using difference lists either use two arguments or DCG notation.

1 Like

I had to double the \ to get the example in The Art of Prolog to work, before just replacing it with -, but maybe I was making some other mistake and should revisit it.

? \ is just a normal symbol character in any ISO compliant Prolog implementation. This means it glues to other symbol characters such as =, @, +, etc. That is why we can write \== and \+.

Only in the old days one could write '\' being the same as \. Since long and in practically all Prolog systems the quoted notation is '\\'.

Thus

:- op(200, xfx, \).

?- A = [a,b|T]\T.
A = [a, b|T]\T. 

Possibly you have a common misconception to think that whether a symbol sequence is defined as an operator makes any difference for requiring quotes or not. People tend to write

:- op(900, xfx, '#').

    ..., X#Y, ...

This is poor style. If you want to use something as an operator you typically do not want a sequence that requires quotes and thus you can declare it to be an operator without using quotes.

ASCII graphic characters are symbol characters unless they play some distinct role in the Prolog syntax on their own, e.g., the various braces, ,, !, |. The dot is a curious exception that is a symbol character despite its unique role to end a clause and its role in floating point numbers.

3 Likes

With 5000 cases on SWISH, median values of 3 runs:

predicate inferences CPU seconds CPU load Lips
translate_td (forwards) 9,999 0.006 100% 1805539
translate_td (backwards) 9,999 0.007 100% 1450767
translate_acc 10,000 0.006 100% 1643143
translate_dl 15,000 0.007 100% 2289751
translate_dcg 10,002 0.007 100% 1436215
translate_findall 2,510,014 0.743 100% 3378145
translate_maplist (forwards) 10,000 0.006 100% 1538894
translate_maplist (backwards) 10,000 0.006 100% 1660363
translate_nd 2,505,036 1.138 100% 2201073

translate_q (append/3 method): StackLimit exceeded in 2.7 seconds with 8,865,519 inferences
translate_nd2 aborted at 1,026,949,072 inferences, 184.250 CPU in 184.260 seconds (100% CPU, 5573667 Lips)

It seems as though most reasonable methods were making around 10,000 inferences, which took around 0.006 and 0.007 seconds on average with little in performance terms to distinguish between them.

2 Likes

Don’t forget that The Craft of Prolog was written 30 years ago. It’s stood up pretty well.

Anyway, a couple of reasons why people like me keep on recommending it:

  • because it goes into deep design issues
  • because Richard O’Keefe is an awesome software designer and programmer

(I once took over a project that Richard started. Almost every other project that I’ve taken over has been an awful experience, usually requiring a painful “do I repair or rewrite this” decision … with Richard’s code I started with “wow, what a great design” and “this is better code that I would have written” … and making changes was easy. If I have trouble understanding something in The Craft of Prolog I assume that the problem is with me, not with Richard.)

5 Likes

I’d like to (since I regularly update stuff and am still learning how on the swish site), but there doesn’t seem to and editing option on my original post.

The most current link is https://swish.swi-prolog.org/p/Iteration1.swinb

Appologies if that came across as a criticism of either you or the book. I’ve just had a lot “Tis written in the ancient scriptures, so you can’t argue…” in my life recently (unrelated to my Prolog hobby), which gets my hackles up since I like to think of myself as a scientist/engineer type, not a cultist.

Thanks Paul. I’ve submitted it to https://prologhub.pl/ for moderation, but not sure if it worked (I find all these different CMS systems a bit of a nightmare).

Thanks Joe, it’s an external blog post with a link to SWISH rather than a blog post, I can remake it as such but then it would be my name on it and I’d rather it were yours!

I’ve added a link to this discussion, saving the latest version as https://swish.swi-prolog.org/p/Iteration2.swinb

Hi Paul

I just keep on getting an error message when I submit it for moderation with no clue what I’ve done wrong, so maybe its easiest for you to put up.

As someone who has spent most of his life working for newspapers, I know few people read bylines anyway. BTW, there’s a typo of my name which I don’t know how to fix on the site. It should be Robert Laing, not Robert Liang.

Ok, I’ll get onto fixing/debugging, see what that error might be.

Fixed the typo (sorry, that was me), published the post (thank you!), still need to track down where the “submit for moderation” bug is…

Please do.

1 Like

As far as I understand SWISH, updating involves creating a new URL, which makes it a bit tricky to make edits. Ideally, I’d like to keep the same URL, but that doesn’t seem possible.

That sounds like a question that everyone might want to know including me. I will post it as a new question. Thanks.

No. Just use File/Save, add a comment and use the Update Program button at the bottom. If you are logged in you can restrict who can save. Some problems with this have been reported. If it doesn’t work report the name and time you tried to save.

1 Like

Thanks Jan. Just fixed a few typos in my graph traversing tutorial and that worked perfectly. Maybe that I wasn’t making notes before caused the confusion.

@joeblog

In case you haven’t noticed I have been deleting my non-relevant replies along the way. It is a habit I picked up from StackOverflow in that others who read this for the first time get tiered of reading replies that aren’t directly related to the topic. In this case these post have been more of a side discussion about adding a link and as such don’t belong in this topic.

The reason I am noting this for you is that you might want to also delete your replies related to the replies I deleted as they now make it look like you are having a conversation with the other part missing. :smiley:

Then I need to know what you deleted … Feel free to remove such replies, at least from me. I’m not sure whether we should aim at SO quality answers or replies or merely discussion that indeed may not be very relevant after the dust has settled.

1 Like