There are several programming languages in use today and a simple google search will throw up interesting recommendations of a subset of these languages to learn, usually based on popularity ranking. As is expected, the popularity of a programming language varies over time and hence a language that was in demand a few years ago might be less popular today.
The most common reason for someone to learn a programming language is to improve one’s career prospects. Sometimes the project one is working on might require a specific language, and so one might end up learning it. Whatever be the reason, learning a new programming language is a welcome experience because it broadens one’s perspective of expressing different algorithms using different dialects of languages and possibly different paradigms.
What are my favourite languages? I am familiar with a handful of languages and have used them in various contexts over the past forty years of my software development career. I vividly remember starting with Fortran and PL/1 and then moving on to Pascal, Lisp, Prolog, and C++ in that order. In my opinion, programmers should learn three languages, and Prolog is one of them (I will reveal the other two in future articles).
In today’s article, let me focus on why we should learn Prolog.
Each of the points I am enumerating can be elaborated as a separate article (maybe even a book). However, I am intentionally keeping the discussion brief.
1) Easy to Learn
I strongly believe that Prolog is the first language one should learn. In fact, I am in favour of teaching it in High School. One of the nice things about Prolog is that It has a simple and uniform syntax with clear semantics. To get started with Prolog, you do not need a background in Computer Science. The excellent book “Thinking as Computation – A First Course” [1] is based on a course taught at the University of Toronto for first-year undergraduate students majoring in sociology, criminology, economics, political science, etc. The interesting point to note is that Prolog was the language taught as part of the course. Another good book along similar lines is “Computational Logic and Human Thinking: How to be Artificially Intelligent” [2] by Robert Kowalski.
2) Logic-based, Declarative Language
Prolog is a declarative language in that it specifies “what” is computed rather than “how” it is computed. This is different from traditional procedural languages that express computation as a series of steps to be executed in a specific order. Prolog is also logic-based; It uses a restricted form of first-order predicate calculus, permitting relations to be expressed as Horn clauses. The Prolog engine acts as a theorem prover and attempts to prove a query as true or false, by applying unification (a form of complex pattern matching).
3) Built-in Nondeterminism
Languages such as C++, Python, Java, and Rust are deterministic, meaning that at any point in the execution of a program, the “next” step is precisely defined. In contrast, Prolog is nondeterministic by nature and hence at certain points in the execution of a Prolog program, there can be “multiple next” steps and each of these is valid. This happens because the same relation (predicate) may be defined multiple times. When a query is posed, the Prolog engine can therefore generate more than one solution by backtracking to earlier choice points. Such a nondeterministic behaviour allows us to use the “generate and test” strategy for solving certain problems.
4) Constraint Logic Programming (CLP)
Constraint programming is useful for solving problems that can be expressed as constraints among a set of variables. Constraint logic programming takes advantage of logic in this context. CLP is thus a natural extension to Prolog, allowing constraints to be included in the body of a predicate. There are usually dedicated constraint solvers for different domains. For example, CLP(FD) denotes constraint logic programming over integers and CLP(Q) is for rational numbers.
5) Definite Clause Grammars
Definite Clause Grammars (DCG) in Prolog provide an elegant way to model Phrase Structure rules. If needed, these can be suitably augmented with extra arguments to yield parse trees. It is also possible to introduce complex constraints to add some context sensitiveness into the grammar to take care of number agreement, etc. Likewise, an appropriate semantic representation can also be synthesized without too much work. The beauty of DCG is that the corresponding “nondeterministic, top-down, left-right parser” comes to us for “free”!
6) Metaprogramming
Metaprogramming is an advanced technique that treats programs as data. Because of Prolog’s ability to manipulate symbols, it is relatively straightforward to implement meta-interpreters, program transformers, compilers, and equation solvers in the language. Predicates such as “goal” and “clause” (in addition to other second-order predicates) allow program introspection and dynamic evaluation.
7) ISO Standard
Prolog was standardised by ISO in 1995. Having a standard helps in code portability across conforming implementations. Two of the popular implementations SICStus Prolog and SWI-Prolog support the ISO standard.
8) Fun to Program
More than anything else, once you get acquainted with Prolog and its declarative style, programming can be a lot of fun. Whether you solve puzzles, implement games or model complex phrase-structure grammars, you are sure to enjoy the experience of coding in Prolog.
Is Prolog used in the real world? One company I am aware of is “TextRazor”, an API-based text analysis company. Their API allows you to define Prolog rules in order to customize text mining. This is a very elegant approach and I have written about it in an article.
I am sure Prolog in some form is being used by many more companies.
In summary, I feel that Prolog must be learnt by all programmers as early as possible in their career. Incidentally, Bruce Tate, in his book “Seven Languages in Seven Weeks” [3] recommends Prolog as one of the seven languages to learn.
Have a great week!
References
1) Hector J. Levesque, “Thinking as Computation: A First Course”, MIT Press, 2012.
2) Robert Kowalski, “Computational Logic and Human Thinking: How to be Artificially Intelligent”, Cambridge University Press, 2011.
3) Bruce A. Tate, “Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages”, 2010.
An interesting read this is. Given the shift of thinking from “How to What” now, I guess learning to think via Prolog is probably very valuable now.
Could not agree more on your suggestion that it be taught in high school.
I want to ask some questions about choosing first programming language to learn.
I have little programming experience and I want to do SICP.
So, should I learn Prolog before SICP?
Or read Little Schemer to prepare for SICP?