My Experience Learning Prolog

a journey in PROgramming in LOGic

I first heard about the Prolog programming language in the book Seven Languages in Seven Weeks. The book, as promised, introduces the reader to the syntax and paradigms of seven different languages. What attracted me to Prolog was its similarity to constraint programming (programs are defined by and solved using their constraints), its similarity to SQL (data-focused and declarative), and its recursive nature.

Normally, I would learn a new language by playing with it, but Prolog was too foreign and intimidating, so I looked for a more structured learning approach. I did some research on recommended first textbooks to work through and settled on The Art of Prolog (AoP). This was a terrible decision. AoP is structured in four parts: logic programming, Prolog, advanced Prolog, and applications. Over two weeks, I completed Part I and worked through all the exercises using pen and paper. I was inspired by the beauty and simplicity of logic programs, and was excited to start Part II, which introduces the Prolog language and its shortcomings as a logic programming language. The exercises in Part II were essentially the same as in Part I, except using Prolog instead of pure logic. The solutions were ugly. I felt disillusioned at being introduced to beauty and then being denied it. I put AoP away and gave up on Prolog.

(Note: having learned basic Prolog, I can see now that AoP is a great book and plan to revisit it; it’s just not for beginners not interested in theory.)

About a year and a half later, I was browsing HN and came upon this thread, where people were talking about what projects they were working on during the COVID-19 lockdown. Someone said that a Prolog class was starting … next week! I signed up for both the Beginner and Advanced courses, which were taught by by Anne Ogborn, a long-time contributer to and speaker on SWI-Prolog. I very much enjoyed Anne’s and the course textbook’s (Programming in Prolog) practical learning approach, which focused on coding in Prolog with a sprinkling of logic programming theory.

I walked away from the beginner course (into the advanced course) comfortable writing basic Prolog. I used my new skills to rewrite a program I had started and abandoned using Python several years ago. The advanced course was a survey of several extensions and libraries plus a capstone project. After the advanced course, I felt like I could use Prolog for anything! To my surprise, SWI-Prolog supports distributed computing, web services, identity management, and other modern features. Not coincidentally, powering application development is the direction that Jan Wielemaker, the primary developer and maintainer of SWI-Prolog, is taking the language.

We were fortunate to have Jan give a guest lecture to close out the beginner course. I really liked how he summarized the positioning of Prolog in his final slide, so I’m reproducing it here in graphical form:

Positioning of Prolog

So what’s next? I plan to continue using Prolog for small projects. Maybe I’ll work my way up to creating my own library or submitting a PR to improve some predicates. I can see Prolog becoming my primary hobby language.