Learning resources for Prolog?

I’m having the hardest time with Prolog.

For me it doesn’t help that most resources about the language are very academic and abstract, and I have no idea where to start when translating their knowledge to the practical Exercism exercises. I feel like this language could do with a book like “Learn You a Haskell for Great Good”.

Anyway, do you have some good tips for learning resources? I found these to be reasonably helpful so far:

2 Likes

Did you find How to learn Prolog | Exercism's Docs?

I have, thanks.

Those sites there do a good job at explaining the syntax and theory of the language but after going through some of them I’m still unable to solve even Easy exercises. I just don’t know how to go from the small bits of example code in those learning materials to an actual program.

1 Like

This is not Prolog specific, but it’s especially useful with languages with a paradigm different than what you’re used to.
If you struggle for a while and feel stuck, look at some of the community solutions. Try to peek just long enough to get an idea on how to proceed; then close the solution and see if you can make some more progress. Or try to understand the approach, then close the solution and see if you can implement it yourself from scratch.
Additionally, most of the problems have example solutions in their respective repository on github (ex: prolog/queen_attack.example.pl at main · exercism/prolog · GitHub)

It goes without saying that you don’t want to blindly copy others’ solutions, you won’t learn anything that way.

Have you identified any specific obstacles?

For example: I

  • had some background knowledge on Prolog,
  • watched a 30 min. video and thereby learned about atoms, clauses, and pattern matching on lists, and then
  • set about solving Space Age, for which I needed to know about number literals, arithmetic, comparisons, and strings – all of which I knew basically nothing about. Which was a bit annoying.

Idk if you have tried this one yet, but this was the one I started with when I decide to pick prolog for this month:
Prolog tutorial

My strategy also included both their official website and ChatGPT.
Usually I break down the problem and identify what steps should I take to get to my solution (or I review the codes I did on the same problem in other languages)

I then ask chat gpt to suggest built in predicates or some how-to steps and then I implement them on my own, usually I asked it specifically not to show any code, and i cross check whatever it said to the official website because it is often wrong (still faster than having to go through google)
If im really stuck then I ask how would it implement it, 9 out of 10 it would spit out something wrong or not working (I dont have Plus so I’m using v3.5, heard 4 is much better), I read its code to get some idea and then implement it on my own.

After I solve it, I pasted the code and ask it it could do it better or differently, sometimes it would hallucinated and suggested things that doesnt exist, but sometimes it did bring up some nice features/ points that I’m not aware.

I think the hardest part for me is the very beginning when I have to start to grasp the idea of how prolog work because it was so different to the other styles, and it is a bit harder to debug to see what’s going on
Understand the types of “data” like atoms and compound would be really helpful

1 Like

I’m really enjoying Learn Prolog Now!. I had no prior experience with the language, but I’m on chapter five now and have managed to complete every exercise thus far (although I did have to cheat on one of the trickier recursion exercises). This passage from the introduction describes the book’s more practical approach:

What we have said about Prolog so far has been high-level and abstract. We are now going to change gears. The approach taken to teaching Prolog in this book is not abstract, and is certainly not driven by high-level ideas (such as the link with logic). In fact, it’s resolutely down to earth. We try to teach Prolog as concretely as possible. We’ve just told you why Prolog is not just another programming language, but we’re going to teach it as if it was.

1 Like

I’ve heard good things about “adventure in prolog” by Dennis Merritt. I have it on kindle.

Thanks for the responses everyone :slight_smile:

I made a small detour to Unison but I’d like to do some more Prolog before the end of the month.

I’ll check out the resources you mentioned. I did look at “Learn Prolog Now!” but maybe I dismissed it too quickly.

One thing I did learn in the meanwhile and seems to be really useful is the trace command in SWIPL. When you activate it you get a step-by-step walk through of a query. For example for this reverse list “function” (don’t know the correct Prolog name for it):

[trace]  ?- reverse_list([1,2,3], [], K).
   Call: (10) reverse_list([1, 2, 3], [], _278150) ? creep
   Call: (11) reverse_list([2, 3], [1], _278150) ? creep
   Call: (12) reverse_list([3], [2, 1], _278150) ? creep
   Call: (13) reverse_list([], [3, 2, 1], _278150) ? creep
   Exit: (13) reverse_list([], [3, 2, 1], [3, 2, 1]) ? creep
   Exit: (12) reverse_list([3], [2, 1], [3, 2, 1]) ? creep
   Exit: (11) reverse_list([2, 3], [1], [3, 2, 1]) ? creep
   Exit: (10) reverse_list([1, 2, 3], [], [3, 2, 1]) ? creep
K = [3, 2, 1].

Have you identified any specific obstacles?

With the logic puzzle exercises (“Garden Party”, “Cheryl’s Birthday” and “Floored”) I simply don’t know how to start approaching the problem, even if I know the answer to the riddle like in Cheryl’s Birthday (because it’s explained on Wikipedia).

The few community solutions these exercises have all look very different, and to me quite complicated. No idea how you would start approaching such exercises, how to solve them using lists and such.

(I’m still hoping Erik will make a Prolog video to learn about his thought process.)

I have to admit I haven’t tried to solve more “traditional” Exercism exercises with Prolog yet because I thought these logic ones would be really suitable for learning a logic based language.

I think that Prolog really merits dedicated study, as well as the patience and time to contemplate and digest the various concepts. While there certainly are some familiar ideas, such as recursion, it is in many ways akin to learning a first programming language because of its radically different approach.

Don’t be alarmed if you don’t know how to tackle certain problems, or if solutions to them seem alien to you. I’ve just finished chapter 8 of Learn Prolog Now!, and I only recently started to feel like I’m getting the hang of using it for interesting problems. For example, one of the exercises in chapter 6 involves solving a simplified version of the zebra puzzle. I was eventually able to solve it without cheating, which was very rewarding, but it took a while and it certainly wasn’t easy. Prolog is extremely well-suited to solving logic puzzles, but any tool will be of little use if you haven’t learned how to use it!

Advent of Computing, a podcast about the history of computing, recently published two one-hour episodes about Prolog. I listened to the first episode so far, which sets up the development of Prolog by discussing the advances in NLP that led up to it.

Dear Terr,

For you and for all those who are starting with Prolog, I recommend the following link, a YouTube channel which explains how to start programming in Prolog in a very simple way and without any prior programming knowledge.

For more advanced programmers, I recommend the site of Markus Triska, a developer who has created several libraries in SWI Prolog, and for me it is the best Prolog Tutorial I have found so far.

Thank you and have a good day! :pray:

1 Like