The use of logic is a common element in Science Fiction. In the Star Trek universe Vulcans are a species that is famously known for aiming to live by logic and reason. Because of this they are often considered masters of these subjects. Yet there was a case in an episode of the Star Trek show Deep Space 9, where Quark, a Ferengi, was able to convince Sakonna, a Vulcan, of the error of her logic and reasoning.
For this Quark made use of Ferengi philosophy, namely the Third Rule of Acquisition. The Rules of Acquisition are a series of proverbs and guidelines that govern Ferengi society, and notably their business dealings that take a prominent place in their lives. The Third Rule of Acquisition states that “Never spend more for an acquisition than you have to.” By applying this rule to the situation in the episode Quark was able make it clear to Sakonna that the best time broker a peace agreement would be “right now”, as the price of peace is at an all time low.
Much like with Ferengi society, various rules and regulations play a prominent role in our lives and dealings with each other. So much so, that navigating the various rules to their logical conclusion, whether in the realm of law, finance and other domains, is often a difficult process. Thankfully there are tools and techniques to help us. The programming language Prolog, in particular, can be a very helpful in dealing with various rules and logical problems. This article aims to provide a brief introduction to this language using by using it to show how Quark’s reasoning can be implemented within a computer program.
Prolog is a logic programming language, originally created in the 1970s, but with many modern implementations such as SWI-Prolog. With logic programming programs are written and solved using some variation of a formal logic. Using such logic, information about the problem and its domain is first declared. Then the user can pose queries about the problem domain, which the programming language aims to answer through reasoning with the available information. This style of programming contrasts with the more common, imperative, paradigm. Instead of telling the computer how to solve the problem, we can declare information about the problem and let the computer, through the use of logic, solve it for us.
In order to show how Prolog works, we aim to use it to represent the same problem and reasoning that Quark used to convince Sakonna. To do this first lets examine the situation depicted the episode a bit more closely. In the episode the Maquis are a group that are at odds Cardassian colonists both living the Demilitarized Zone. Tensions were escalating as the Cardassian colonists were recently supplied in secret with weapons by the Cardassian Union. As a result Sakonna, along with other members of the Maquis, aimed at acquiring more weapons of their own. Even after it was found that the Cardassian Union was behind weapon supplies, Sakonna still aimed at gathering more, as a way to ensure peace. Quark pointed out the flaws in the logic of her argument, given the Third Rule of Acquisition: “Never spend more for an acquisition than you have to.” If the goal of Sakonna is truly to acquire peace, than it is the perfect time to be negotiating with the Cardassian colonists. With the flow of the smuggled in weapons having stopped, and both sides already having weapons, neither the Maquis nor the Cardassian colonists have any advantage. By aiming to acquire weapons still, they would only escalate the conflict, and making peace more costly in the long run.
To express this scenario with Prolog, we are going to introduce some elements of this language first (for a bit more through introduction see the excellent Learn Prolog Now! available online). There are three basic elements to a Prolog program: facts, rules and queries.
Facts are elements that have been stated to hold. For example the following statement:
can be used to denote that the advantage is held by the Cardassians colonists.
Rules are a way for Prolog to infer new information from the knowledge that already exists. For example if we aim to state that “if the Cardassian colonists have the advantage the price of peace is high” in Prolog we would have the rule:
priceOfPeace(high) :- advantage(cardassians).
There are two parts to writing such rules in Prolog: the body and the head. The body of the rule is in this case
advantage(cardassians) while the head is
priceOfPeace(high). In Prolog if the body of the rule holds true, than it can be concluded that the head is true as well. This exactly matches to what we aim to express: if the Prolog program knows that the Cardassian colonists hold the advantage, than it can conclude that the price of peace is high.
Facts and rules together form the knowledge base that can describe a domain. In this case the domain is the situation between the Cardassian colonists and the Maquis. The final piece, queries, allows us to examine this knowledge base and ask questions on what Prolog can infer from this knowledge. For the above example we could query whether the price of peace is indeed high, which should follow directly from the semantics of the rules and facts that we have described.
Given that we load in the knowledge base consisting of the above-mentioned fact and rule, the query:
that asks whether the price of peace is high, will return
true indicating that this is indeed the case. Instead if we ask whether the price of peace is low, using the query:
the result will be
false indicating this is not the case.
We can also ask more open ended questions using variables. Variables are written by starting with a capitalized letter (or an underscore), such as
X. These variables could be used to write queries with unknowns. The query:
will give us the result:
X = high
Which again is what would directly follow from the facts and the rules. What Prolog does with variables is to try to “unify” them with known values that match (or other variables). In this case from the knowledge base we have given it, it unifies it with the value of
From the basic elements of facts, rules and queries Prolog is able to represent and answer problems in many domains. This is also shows off the feature of a logical programming language, such as Prolog, that make it different compared to many other programming languages. Instead of telling the program what to do, the program becomes a description of the domain, over which queries are answered to solve the overall goal.
Beyond the above basics, Prolog has many features to support the description of more complicated domains. Here we only list a few that helps us to convey our translation of Quark’s reasoning into Prolog.
One such feature is the use variables within the rules themselves. The rule
acquirePeace(X) :- priceOfPeace(X).
indicates that the price of acquiring peace depends on the price of peace.
priceOfPeace(low) :- advantage(cardassians), advantage(maquis).
shows off the logical operator and, meaning that the price of peace is low if both the Cardassians and the Maquis have the advantage.
priceOfPeace(low) :- \+ advantage(cardassians), \+ advantage(maquis).
shows off the logical not, denoted by the ‘+’ symbol, which states that the price of peace is also low if neither the Cardassians nor the Maquis have the advantage.
Finally, much like in other languages, comments for the code can also be written. This is done by prefacing a line with “%%”.
Now if we put every element together to use Prolog to describe Quarks scenario, a possible knowledge base is as follows:
%% Rules acquirePeace(X) :- priceOfPeace(X). priceOfPeace(low) :- \+ advantage(cardassians), \+ advantage(maquis). priceOfPeace(low) :- advantage(cardassians), advantage(maquis). priceOfPeace(high) :- advantage(cardassians). priceOfPeace(low) :- advantage(maquis). advantage(cardassians) :- weapons(cardassians), \+ weapons(maquis). advantage(maquis) :- \+ weapons(cardassians), weapons(maquis). %% Facts weapons(cardassians). weapons(maquis).
In the overall knowledge base, the logic of Quark’s reasoning is broken down into a number of rules and a pair of facts.
The rules state that advantage of either side, the Cardassians settlers or the Maquis, is decided by one side having weapons while the other side has not. The price of peace is in turn dependent on who has the advantage. In this formalisation the price of peace is only high if Cardassians have the advantage. Finally the price of acquiring peace is dependent solely on the price of peace.
The facts for this scenario is that both the Cardassian settlers and the Maquis have weapons.
With this knowledge base describing Quark’s reasoning process the query to see for what price we can aquire peace:
will indeed return
X = low
indicating that acquiring peace can be done at a low price.
Going a bit beyond this query, if we would further want to test the logic of Quark’s scenario, we can also inquire about other information.
For example if we would ask for the price of peace:
we would also unsurprisingly get
X = low
as with the rules defined in our knowledge base these values are intertwined.
We can also ask for who has the advantage:
which will return:
which is completely in line with the scenario that noone has the advantage.
We can also ask who has weapons, the query of:
gives us the answer
X = cardassians
… but wait, this can not be right! Did we not state in the knowledge base that both the Cardassian colonists and the Maquis have weapons? Prolog can actually return mutliple answers, if they exist. In SWI-Prolog this can be done by pressing semicolon (
;) after an answer, in which case another answers is returned, if it exists or simply false otherwise. By pressing
; once after the first answer we will see a total output such as:
X = cardassians ; X = maquis.
Hopefully Sakonna would also be quite convinced with the logic of Prolog, but just to be absolutely sure, lets examine some scenario’s where the facts of the scenario are changed. In such cases the rules should still be applied in ways that we would expect.
Suppose that our only fact now is that only the cardassians colonists have weapons, while rules remain the same. Giving the query for knowing the price of acquiring peace:
will now return:
X = high
Hopefully, much like Quark’s application of the Third Rule of Acquisition, this intro would have been enough to convince Sakonna of the logic of purchasing peace at the lowest possible price. Similarly, I hope that as a reader, this article gave some insight to the usefulness of Prolog in such scenarios. Next time, you are dealing with a problem domain, that would easily match to a similar scenario of rules, facts and queries, give Prolog a try. It is, after all, a very logical choice.