Prolog
by Bruce A. Tate
Version: P1.0 (March 16, 2022)
Copyright 2022 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/#about-ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/passprol, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Dave Rankin (CEO) Janet Furlow (COO) Tammy Coron (Managing Editor) Jacquelyn Carter (Development Editor) L. Sakhi MacMillan (Copy Editor) Andy Hunt and Dave Thomas (Founders)
For customer support, please contact .
For international rights, please contact .
Table of Contents
Copyright 2022, The Pragmatic Bookshelf.
Preface
In 2009, my dear friend and editor, Jackie Carter, sent an early draft of Seven Languages in Seven Weeks to Joe Armstrong, creator of the Erlang language. I was beyond nervous. He would read my thoughts about Erlang and also a couple of other languages, including Ruby and Prolog.
My Ruby confidence was high. As an early adopter and author, I knew the language well after writing several books and even teaching conference workshops. My Erlang knowledge was quite limited. I expected Joe to have plenty to say about my musings on his creation.
Then there was Prolog. I didnt expect many comments about the logic language because its not a popular language. I couldnt have been more wrong. It turns out Prolog was one of Joes favorite languages, so he had plenty of comments about the chapter. Over time, he helped me get the programs and prose into shape and volunteered to write the foreword for Seven Languages in Seven Weeks . Over the years that followed, he became a good friend. Wed see one another on the conference circuit two or three times a year. Wherever we were, he always had something to say about Prologs beauty and simplicity.
At one point in his review process, Joe suggested I replace one problem with another, the map coloring problem. He coached me to give it a try. I was quickly able to rough out the rules of the problem. To test my progress, I ran my program, and Prolog started spitting out the long list of correct answers. I was floored.
I told Joe and he was laughing with glee. He said, Youve just had your Prolog moment! He was right.
As you work through the chapters, you wont find a typical code repository. Most of the programs are twenty lines or less. The real magic of Prolog is that you can solve problems succinctly with a few lines of code and a collection of facts.
Over the course of this book, I hope you find your own Prolog moment. If something makes you smile, tell someone and help them find a Prolog moment too.
Bruce Tate
March 2022
Copyright 2022, The Pragmatic Bookshelf.
Chapter 1
Logic Programming Basics
Prolog is at once fascinating and maddening because its so different from many of the other languages you might have experienced. Instead of giving Prolog a rote program describing exactly what to do, youll give it a database of facts such as Bruce follows Joe. Then youll add some basic inferences to your database such as A user receives a tweet if that user follows someone, and that person tweets something. Once you have that database, you can ask Prolog questions, called queries, such as Who receives tweets from Joe? Prolog will tie the facts and inferences together to solve some pretty demanding questions.
In this chapter, well start with what makes Prolog different from other languages. Then, well move into the Prolog environment and the basics of logic programming. Finally, well look at making inferences with Prolog before we conclude.
As you work through this module, look for ways that Prolog can help you think about problem solving. Many programming languages can benefit from the way Prolog establishes rules and inferences. Some programming languages even have implementations built-in, especially Lisp dialects like Clojure. Keep your eyes open and youre sure to find something that you find interesting. Lets get started!
What Is Prolog, Anyway?
Most languages youll encounter are general purpose languages. Java, Ruby, and Python are examples of mainstream general purpose languages. Lisp, Erlang, and Haskell are examples of mainstream languages that establish a niche among general purpose languages.
Other languages exist to solve problems in a specific genre. For example, SQL is built to retrieve data from databases and HTML is built to provide structure to documents, particularly web pages. Like these languages, Prolog is a problem-specific language built to handle logic programming. As such, its an important language for artificial intelligence.