Está en la página 1de 14

History of Prolog

Prolog evolved out of research at the University of Aix-Marseille back in the late 60's
and early 70's. Alain Colmerauer and Phillipe Roussel, both of University of Aix-
Marseille, colaborated with Robert Kowalski of the University of Edinburgh to create the
underlying design of Prolog as we know it today. Kowalski contributed the theoretical
framework on which Prolog is founded while Colmerauer's research at that time
provided means to formalize the Prolog language.
1972 is referred to by most sources as the birthdate of Prolog. Since its birth it has
branched off in many different dialects. Two of the main dialects of Prolog stem from the
two Universities of its origin: Edinburgh and Aix-Marseille. At this time the first Prolog
interpreter was built by Roussel. The first Prolog compiler was credited to David
Warren, an expert on Artificial Intelligence at the University of Edinburgh.
To this day Prolog has grown in use throughout North America and Europe. Prolog was
used heavily in the European Esprit programme and in Japan where it was used in
building the ICOT Fifth Generation Computer Systems Initiative. The Japanese
Government developed this project in an attempt to create intelligent computers. Prolog
was a main player in these historical computing endeavours.
Prolog became even more pervasive when Borland's Turbo Prolog was released in the
1980's. The language has continued to develop and be used by many scientists and
industry experts. Now there is even and ISO Prolog standardisation (1995) where all of
its individual parts have been defined to ensure that the core of the language remains
fixed.

Special Features
Prolog is a rich collection of data structures in the language and human reasoning, and
a powerful notation for encoding end-user applications. It has its logical and declarative
aspects, interpretive natur, compactness, and inherent modularity.
• Intelligent Systems - programs which perform useful tasks by utilizing artificaial
intelligence techniques.
• Expert Systems - intelligent systems which reproduce decision-making at the
level of a human expert.
• Natural Language Systems - which can analys and respond to statements
made in ordinary language as opposed to approved keywords or menu
selections.
• Relational Database Syst

Fields of Application
Prolog is the highest level general-purpose language widely used today. It is taught with
a strong declarative emphasis on thinking of the logical relations between objects or
entities relevant to a given problem, rather than on procedural steps necessary to solve
it. The system decides the way to solve the problem, including the sequences of
instructions that the computer must go through to solve it. It is easier to say what we
want done and leave it to the computer to do it for us. Since a major criterion in the
commercial world today is speed of performance, Prolog is an ideal prototyping
language. Its concept makes use of parallel architectures. It solves problems by
searching a knowledge base (or more correctly a database) which would be greatly
improved if several processors are made to search different parts of the database.
Prolog variables
A Prolog variable can be instantiated to any term, regardless of whether the term is a
constant, a variable or a structure. During goal execution, Prolog variables are
instantiated to terms by unification and therefore assume all the attributes of these
terms. This instantiation remains in force during goal execution and is not undone until
backtracking takes place. Any attempt to instantiate an already instantiated variable to
another value will lead to the failure of the unification and will initiate backtracking to the
last choice point. If two variables are instantiated to each other, they will be regarded as
identical in subsequent processing.
Data types
Prolog's single data type is the term. Terms are
either atoms, numbers, variables or compound terms.

 An atom is a general-purpose name with no inherent meaning. Examples of atoms


include x, blue, 'Taco', and 'some atom'.
 Numbers can be floats or integers.
 Variables are denoted by a string consisting of letters, numbers and underscore
characters, and beginning with an upper-case letter or underscore. Variables closely
resemble variables in logic in that they are placeholders for arbitrary terms.
 A compound term is composed of an atom called a "functor" and a number of
"arguments", which are again terms. Compound terms are ordinarily written as a
functor followed by a comma-separated list of argument terms, which is contained in
parentheses. The number of arguments is called the term's arity. An atom can be
regarded as a compound term witharity zero. Examples of compound terms
are truck_year('Mazda', 1986) and 'Person_Friends'(zelda,[tom,jim]).
Special cases of compound terms:

 A List is an ordered collection of terms. It is denoted by square brackets with the


terms separated by commas or in the case of the empty list, []. For
example [1,2,3] or[red,green,blue].
 Strings: A sequence of characters surrounded by quotes is equivalent to a list of
(numeric) character codes, generally in the local character encoding or Unicode if
the system supports Unicode. For example, "to be, or not to be".
DATA STRUCTURES
• All data types are inherently abstract.
○ A data type is defined by giving an algebraic description of the properties
of its operators.
E.g., natural numbers could be defined by:
○ sum( succ( X ), Y, succ( Z )) :- sum( X, Y, Z ).
○ sum( 0, X, X ).
○ ?- sum( succ( succ(0)), succ( succ( succ(0))), Answer ).
○ Answer = succ( succ( succ( succ( succ(0)))))
Due to the inefficiency of this approach, most (all?) versions of Prolog
provide basic arithmetic predicates, functions, and operators.
• Prolog provides no data constructors.
○ Prolog can operate directly with a logical description of the properties of a
data structure.
○ Compound terms can represent data structures.
E.g., lists may be represented by the expressions that construct them:
○ list( nil ).
○ list( cons( X, L )) :- list( L ).
○ null( nil ).
○ car( cons( X, L ), X ).
○ cdr( cons( X, L ), L ).
○ ?- car( cons( cons( a, cons( b, nil )),
○ cons( c, cons( d, nil )) ), L ).
○ L = cons( a, cons( b, nil ))
• Prolog provides a built-in operator and some syntactic simplifications for
representing lists.
○ The dot functor or dot operator represents cons.
E.g.,
○ ?- car(( a.b.nil ).( c.d.nil ), X )
○ X = a.b.nil
If your version of Prolog does not support '.' (dot) as an operator, you can
either use it as a functor (in which case the list a.b.nil would be written as .
(a, .(b, nil))) or define it as an operator by entering op(51, xfy, '.')..
○ [] represents nil.
E.g.,
○ ?- car(( a.b.[] ).( c.d.[] ), X )
○ X = a.b.[]
Prolog does not support nil as an alias for [] -- we introduced its use in the
list-related predicates above in order to show the relationship between
Prolog lists and LISP lists.
○ Like LISP, Prolog allows a simpler list notation.
E.g.,
○ ?- car( [[ a, b ], c, d ], X )
○ X = [a, b]
This is syntactic sugar -- lists are still compound terms.
○ To simplify splitting a list, Prolog uses [ X | Y ] to represent a list with
head X and tail Y.
E.g.,
○ member( Element, [ Element | _ ] ).
○ member( Element, [ _ | Tail ] ) :- member( Element, Tail ).
○ ?- member( a, [ a, b, c ] ).
○ yes
○ ?- member( a, [ b, a, c ] ).
○ yes
○ ?- member( d, [ c, b, a ] ).
○ no
[ ] does not match either [ Element | _ ] or [ _ | Tail ].
• Strings are represented internally as lists.
○ A string is another form of list notation.
E.g., "abc" is treated internally as a list of ASCII character codes:
○ [97, 98, 99]
• Example of a user-defined data type: set.
○ set membership -- same as list membership (see above)
○ subset
○ subset( [ A | X ], Y ) :- member( A, Y ), subset( X, Y ).
○ subset( [ ], Y ). % The empty set is a subset of every set.
○ intersection
○ % Assumes lists contain no duplicate elements.
○ intersection( [ ], X, [ ] ).
○ intersection( [ X | R ], Y, [ X | Z ] ) :-
○ member( X, Y ),
○ !,
○ intersection( R, Y, Z ).
○ intersection( [ X | R ], Y, Z ) :- intersection( R, Y, Z ).
○ union
○ union( [ ], X, X ).
○ union( [ X | R ], Y, Z ) :-
○ member( X, Y ),
○ !,
○ union( R, Y, Z ).
union( [ X | R ], Y, [ X | Z ] ) :- union( R, Y, Z ).

Prolog character set


The Prolog character set consists of the following classes of characters:
• Letters, digits, underscore character
• Special characters
• Prolog-specific characters
• Delimiters
Operators

Operators provide a way of specifying an alternate input/output syntax for structures


with one or two arguments. For example, they allow:
likes(leona, water)
swims(leona)
to be written and read as
leona likes water
leona swims
There are a number of predefined operators in Prolog, such as: +, /, *, -. This is what
makes it possible to write 3 + 4 rather than +(3,4).
In order to do this we have to inform Prolog via an operator declaration that a certain
name may optionally be used before, after, or in between its arguments; we speak
of name as being an operator. Even ifname is declared to be an operator, it can still be
used in the usual structure notation.
We emphasize that declaring operators only serves to alter the way structures may look
on input or output. Once inside the Prolog system, all structures are kept in the same
internal form.
If an operator is declared to be used between its two arguments, we say it is
an infix operator. If it is to be used before its single argument then it is a prefix operator;
if it is to be used after its argument it is apostfix operator. Operators may be declared to
be both infix and either pre- or post- fix, in this case they are called mixed operators.
Just declaring the "fix" of an operator is not enough however since this can lead to
ambiguities. For example suppose that + and - have been declared to be infix
operators. Consider:
a+b-c
What is the second argument of +? It might be b, in which case the term is
'-'( '+'(a, b), c)
or it might be the whole term b - c, in which case the term is
'+'(a, '-'(b, c))
These are very different terms so which should Prolog choose?
One way to force an interpretation is to use parentheses. So if we wanted the first
interpretation we would write:
(a + b) - c
If we wanted the second we should use:
a + (b - c)
exactly as in high school algebra. However we still wish to agree on a consistent
interpretation in the absence of overriding parentheses.
Prolog solves this problem by requiring two extra pieces of information about each
operator at the time of its declaration: precedence and associativity.
Structure of Prolog Programs
Every entry into the interpreter is a goal that Prolog tries to satisfy.
Every goal in Prolog ends with ``.''.
A Prolog program consists of a database of facts, rules, and queries. This program is, in
essence, a knowledge base.
• Fact: head but no body
man(socrates).
man(plato).
• Rules: head and body
mortal(X) :- man(X).
• Questions: body but no head
mortal(X).
Use ``;'' to get next possible answer, Return to end.
Yes means true with no variables, no means not consistent with database.

Source Code
// the main program (this is a comment)
Hello:-
nl,
write('Hello world!' ).
}

for_loop (Index, Final, Result)


:- Index < Final,
body_of_loop(Result),
NewIndex = Index + 1, !, /* cut prevents unnecessary backtracking */
for_loop(NewIndex, Final, Result). /* Tail recursion: can be optimized
by the interpreter */
for_loop (Index, Final, Result) :- Index >= Final.

repeat loop
Recursion
Recursion is a technique that must be learned as programming in Prolog depends
heavily upon it.
We have already met a recursive definition in section 2.2. Here are some more:
One of my ancestors is one of my parents or one of their ancestors.

A string of characters is a single character or a single character followed by a string of


characters.
A paragraph is a sentence or a sentence appended to a paragraph.
To decouple a train, uncouple the first carriage and then decouple the rest of the train.
An example recursive program:

talks_about(A,B):-

knows(A,B).

talks_about(P,R):-

knows(P,Q),

talks_about(Q,R).

Roughly translated:
You talk about someone either if you know them or you know
someone who talks about them

If you look at the AND/OR tree of the search space you can see that
• There is a subtree which is the same shape as the whole tree
reflecting the single recursive call to talks_about/2.
• The solution of a given problem depends on being able to stop
recursing at some point. Because the leftmost path down the
tree is not infinite in length it is reasonable to hope for a
solution.
using the goal
talks_about(X,Y)

If we ask for repeated solutions to this goal, we get, in the order shown:
X= bill Y= jane

X= jane Y= pat

X= jane Y= fred

X= fred Y= bill

X= bill Y= pat
and so on

The search strategy implies that Prolog keep on trying to satisfy the
subgoal knows(X,Y) until there are no more solutions to this. Prolog then finds that,
in the second clause for talks_about/2, it can satisfy the talks_about(X,Y) goal by
first finding a third party who X knows. It
satisfies knows(X,Z) with X=bill, Z=jane and then recurses looking for a solution to
the goal talks_about(jane,Z). It finds the solution by matching against the
second knows/2 clause.
The above AND/OR tree was formed by taking the top level goal and, for each clause
with the same predicate name and arity, creating an OR choice leading to subgoals
constructed from the bodies of the matched clauses. For each subgoal in a conjunction
of subgoals we create an AND choice.
Note that we have picked up certain relationships holding between the (logical)
variables but we have had to do some renaming to distinguish between attempts to
solve subgoals of the formtalks_about(A,B) recursively.
% my_for(start_value,end_value,increment,action)
my_for(V,V,_,_) :- !.
my_for(Start,End,Inc,Action) :-
End > Start,
NewValue is Start+Inc,
call(Action),
my_for(NewValue,End,Inc,Action).

For loop

También podría gustarte