Another way in which logic is used in computing is through a programming paradigm called logic programming. As you probably know, there are different approaches to creating programming languages, often referred to as programming paradigms. In this module, we will very briefly discuss programming paradigms. Programming paradigms are covered in great detail in CS3123 Programming Language Concepts.
There are four main programming paradigms, namely
In the procedural programming paradigm, a program is explicitly regarded as a sets of procedures. C is an example. In object-oriented programming, a program is seen as a collection of objects interacting with each other. Java and C++ are examples of object-oriented programming languages, although -in my opinion- poor examples. In the functional programming paradigm, a program is seen as a collection of functions in the sense in which this term is defined in mathematics and covered later in this course. Lisp is an example.
For our purposes, the programming paradigm that is more relevant is logic programming. Under this programming paradigm, programs are seen as sets of logical sentences, expressing facts and rules about some problem domain. Programming then becomes a matter of interrogating the program.
Perhaps the best known example of a programming language is Prolog. A Prolog consists of a series of facts and rules, expressed as Horn clauses. A Horn class is a universally quantified conditionals, in which there is only one statement in the consequence and the antecedent is a conjunction. The following is a very simple example of a prolog program
sheep(sam)
sheep(susie)
male(sam)
female(susie)
ram(X) :- sheep(X), male(X)
ewe(X) :- sheep(X), female(X)
The first four lines simply give some facts, while the last two code up the rules that a male sheep is a ram, and a female sheep is an ewe. Prolog uses capitals as variables. The last two prolog clauses can be rendered in the language of predicate logic that we have used as
(∀x)[(sheep(x) & male(x)) → ram(x)]
(∀x)[(sheep(x) & female(x)) → ewe(x)]
In order to interrogate a Prolog program, we simply ask the question we want the program to answer. Thus, if we want to know whether Sam is a ram, we simply type
?- ram(sam).
and the program will eventually answer "yes".
We can also ask the program to find any rams. We would do so by using a variable:
?- ram(X).
and the program will answer
X = sam.
One of the main problems with Prolog from a logical point of view is the way in which it deals with negation. Prolog uses "negation-as-failure". As your exercise for this sub-module, use the internet to find out what negation-as-failure means, and give the definition and the source, and the explanation why negation-as-failure is problematic from a logical point of view, in the associated drop box.