The Logical Language Prolog

Total Page:16

File Type:pdf, Size:1020Kb

The Logical Language Prolog

Canter 1

The Logical Language Prolog Cory Canter Murray State University Canter 2

Prolog was created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski. Together they developed the overarching schematic of what would eventually become Prolog, with

Kowalski designing the framework they built upon and Colmerauer and Roussel working to turn what was originally meant to be a way to better utilize language processing into the programming language of Prolog that we know today. A small test version of the language was in use by the closing months of 1971, with the final result of their labors being realized a full year later at the closing of 1972. (Colmerauer)

The project realized its potential as a language when the creators decided to take on the task of trying to combine logic with procedural representation in a way that would make the language better suited for language processing as well logical reasoning. In 1972 prolog was originally used with an interpreter created by Roussel, however the first Prolog compiler was developed not long afterward by David H. D. Warren, a professor of Artificial Intelligence, in the

Warren Abstract Machine, which would lay the foundation for the Edinburgh style and would be the building blocks for the syntax followed in Prolog today. (Colmerauer)

While most North American’s initially favored Lisp, Prolog quickly became a very popular language in Europe and parts of Asia and has recently gained popularity in North America as well. It has played a large role in many great computing achievements, the largest of which are the Fifth Generation Computer Systems Initiative, which was an attempt by the Japanese to create a super-computer that would be used for the future development of Artificial

Intelligence. Since, Prolog has continued to grow and is currently in use in fields such as Canter 3

Artificial Intelligence, Discrete Mathematics and Logic Computability. The language has seen its biggest advances with the release of Borland’s Turbo Prolog in 1988, which has just recently become Visual Prolog, and the ISO Prolog Standardization in 1995. (Colmerauer)

Prolog is not like most programing languages, in the sense that it is not a language based on algorithms. Prolog is a rare breed of language called a logic language, which requires users to think about how they understand the language in an entirely different way. Because of this difference the language design paints a stark contrast to most popular languages.

An easy example of a program written in prolog would go as follows:

doctor(greg) .

clever(X) :- doctor(X) .

The computer would read the first line as saying House is a doctor. Next, it would read the second line as X is Clever if X is a Doctor, which it would simplify to mean that all doctors are clever. This is one of the reasons that Prolog excels in fields such as Artificial Intelligence because of how easily it processes logical thinking.

In Prolog a program is built with predicates, which are built from clauses. Clauses are arranged into predicates based on shared names and whether or not they require the same number of arguments. A clause can either be true or false, or it can be a base clause, which is a clause that is always true and has no programmed possibility of being false. Structures contain several arguments, which can be integers, variables, atoms or other structures, and have the arguments divided by commas while keeping them together inside of parentheses. Spacing can Canter 4 be an issue, at times, with structures requiring specific amounts of spacing between certain arguments. (Wilson)

Another key difference in Prolog is the way variables work. Variables are composed of both digits and letters and must have either a capital letter or an underscore at the start of the name. In Prolog, every argument and every variable is local to its declaring predicate. The exception to this is if a variable is passed by reference to a separate predicate later in the program. Variables can be unified with themselves, other variables or any other value in the

Prolog program. Once this happens they are no longer variables and are whatever they were combined with. If two variables are unified together then they would now both return the same value. If a variable is unified with a Prolog value then it is referred to as instantiating.

Unification is caused by an equal sign (=). Basic unification is between two structures and if they are similar then they unify and if not they fail. One step above that is unification between a variable and a primitive during which the variable inherits the value of the primitive and completes unification. If, for some reason, you wish to undo any unifying or instantiating then you can backtrack which returns everything to the way it was prior. There is a special variable, comprised of a standalone underscore (_) which is referred to as the anonymous variable. Every time this variable is used is completely unique of any other in the program, even if they are in the same clause.

Binding, in Prolog, takes place when a Prolog rule is applied or when there is a query to be resolved. The query will go through the program and find the answer to the query and give that answer. If it cannot find the answer then it will return a failure as false. Some queries can Canter 5 require that multiple variables be bound, even if they aren’t directly involved in the query. To make sure there are no accidental bindings that could hinder further use of the program good practice is for the user to input a semi-colon which will undo any bindings. (Wilson)

Prolog is a dynamic-type language and has only data type, which is called term, however there are also subtypes of variables, numbers, compound terms and atoms. Numbers are either integers or floats; however rational numbers and unbounded integers can also be utilized. A compound term consists of both an atom and multiple arguments. When put together into a compound term the atom is referred to as the functor. The terms number of arguments is referred to as its arity. An atom with no arguments would have arity zero.

Expressions in Prolog are read the same as they are in discrete mathematics. In Prolog

“,” represents “and”, “;” represents “or”, “:-“ represents “if” and “not” represents “not”.

Variables are assigned in Prolog with a built in predicate called “is” which is the prolog form of saying “equals”. Also in order for expressions to be evaluated in Prolog they must be specifically evaluated with the “is” predicate. An example of this is “C is A + B”. That line of code will add the variables A and B together and store the value in variable C, assuming that the variables are already established.

One of the control structures used by Prolog is sequencing. This is a very common control structure since it is standard for statements to be worked through in the order they are given. Another control structure is a selection system. The selection system works so well with

Prolog because each of the terms must succeed or fail so every part is seen as a single-branch selection. However, Prolog’s most prominent control structure is backtracking. Backtracking Canter 6 works by trying to satisfy whatever goal the program is given first. Once it has satisfied that goal, it will move on to the next. If it reaches a goal that can’t be satisfied then it backtracks back through the last goal and tries a different route to satisfy the previous goal. If it finds an alternate route then it will attempt to finish out that route. It will continue this process until either all goals have been satisfied or it returns that there is not a solution. One benefit of backtracking is that it often times can find multiple solutions for a problem which can make optimization easier. Prolog uses relational subprograms, which are used for instances. A call to a subprogram would contain the relation’s name as well as the listed arguments and their expressions. (Wilson)

Prolog itself is not ideal for object oriented programming, however there are quite a few additions to the language that have been created for the sake of object oriented programming.

Some of the additions are: Flora-2, Logtalk, Oblog, Objlog, visual prolog and Prolog++. Prolog++ is the most commonly used and the most well received of the additions. It was created by Logic

Programming Associates and allows classes as well class hierarchies. (Colmerauer)

Prolog has two types of concurrency. They are suspended goals and multithreading.

Multithreading works by the program running multiple threads over the same system. This can pose some problems with most languages; however prolog solves most of them. Suspended goals works in a much different way, by blocking certain directives it stops goals from expanding and puts them on a waiting list. (Wilson)

In prolog, exception handling is performed with a catch function. The first part of the function is what you are trying to perform. The second part is the exception we are trying to Canter 7 catch and the third is the action to be taken in the event that the exception should occur.

Unfortunately Prolog does not differentiate between exceptions and errors so there is not any error handling. Event handlers are specified through the bind command and are used to assign widgets independent behaviors. (Wilson)

Prolog can be a very difficult language to write in, until you have had an ample amount of practice. Prolog is written very different from most languages because of the fact that it is a logical language rather than a procedural language. The difference of telling a program what to do rather than how to do it is enough to trip up most programmers when starting in Prolog.

One of the ironies of Prolog being a logic language is that it does not write in a simple logical format and instead requires specific detail as to the placement of spacing and the order of which things occur, rather than being written in a way that flows for the user and makes writing the code come more naturally. Another problem with the writability of Prolog is that, unlike procedural languages, the code does not flow naturally or come anything close to resembling a basic language structure, which can make programming in this language seem very foreign, and painstakingly complicated. (Espmark)

On the other side of the coin, Prolog is surprisingly a very readable language, so long as you know how to read it and have had some experience doing so. The very things that make writing in Prolog so difficult also make reading it much easier. A common analogy is that Prolog is a like a complex math equation. It can be very slow and arduous to write and figure out, but once it is written, you can look at it and read through it quickly, while working it out, even if you weren’t the one who wrote. As exemplified in the short program on page 2, once you know the Canter 8 basic structure of Prolog and understand the way the logic is presented, reading the language becomes a simple task of putting the pieces in the right order and filling in the blanks.

(Espmark)

Currently, Prolog is a moderately reliable language; however there is an ongoing effort to add redundancy to the language to make it much more reliable. One good thing about Prolog is that since it is a logic language and is used for very specific things, it inherently has a certain amount of reliability. Another thing that adds greatly to the reliability of Prolog is the constant built in backtracking of the language. Backtracking ensures that the program always returns to the point of most recent success to create the optimal conditions of success, which makes the language as a whole much more reliable.

The cost of Prolog is also relatively moderate compared to procedural language and relatively low compared to other logic languages. The biggest cost of Prolog is training people to use a rare type of programming language and then having to build them up to a point that they can create programs that not only function but meet high enough standards as to make the training worth the cost. Where Prolog makes up for this is in its maintainability and reliability, with the language being very easy to maintain and to correct for future problems because of the backtracking feature, but also because of the inherent reliability discussed previously. (Espmark)

Prolog is certainly a very unique language and unfortunately a rather obscure one to most programming communities. While it seems that it will stay this way for a long time Canter 9 because of its steep learning curve and rather lacking writability, the benefits of its readability and reliability are a high price to pay. Canter 10

Appendix

The following is an example of a simple prolog program:

The file lesson1.pl contains the clauses:

has(jack,apples). has(ann,plums). has(dan,money). fruit(apples). fruit(plums).

?- [lesson1]. /* loads the file */ yes ?- listing(fruit). /* lists the clauses */ fruit(apples). fruit(plums). ?- listing(has). has(jack,apples). has(ann,plums). has(dan,money). ?- has(jack,X). /* what has Jack? */ X = apples ?- has(jack,_). /* does Jack have something? */ yes ?- has(X,apples),has(Y,plums). /* who has apples and who has plums? */ X = jack Y = ann ?- has(X,apples),has(X,plums). /* does someone have apples and plums? */ no ?- has(dan,X),fruit(X). /* has Dan fruits? */ no ?- has(X,Y),not fruit(Y). /* does someone have something else? */ X = dan Y = money

Here are a few examples of unification attempts to show what does and does not work:

?- a = a. yes

?- a = b. Canter 11 no

?- location(apple, kitchen) = location(apple, kitchen). yes

?- location(apple, kitchen) = location(pear, kitchen). no

?- a(b,c(d,e(f,g))) = a(b,c(d,e(f,g))). yes

?- a(b,c(d,e(f,g))) = a(b,c(d,e(g,f))).

No

?- X = a.

X = a

?- 4 = Y.

Y = 4

?- location(apple, kitchen) = location(apple, X).

X = kitchen

?- location(X,Y) = location(apple, kitchen). Canter 12

X = apple

Y = kitchen

?- location(apple, X) = location(Y, kitchen).

X = kitchen

Y = apple

?- X = Y.

X = _01

Y = _01

?- location(X, kitchen) = location(Y, kitchen).

X = _01

Y = _01

?- X = Y, Y = hello.

X = hello

Y = hello

?- X = Y, a(Z) = a(Y), X = hello.

X = hello

Y = hello

Z = hello Canter 13

Bibliography

"Prolog: A Brief History." MTA. MTA. Web.

Colmerauer, Alain. "The Birth of Prolog." CiteSeerX. Web. .

Wilson, Bill. "The Prolog Dictionary." CSE. Web. .

Espmark. "Evaluation of Prolog." UMU. Web. .

"Unification." Amzi! Amzi!, 1 Jan. 2010. Web. 6 Oct. 2014. .

Malita, Mihaela. "Prolog Examples." Anselm. Anselm. Web. 6 Oct. 2014. .

Recommended publications