Prolog

format_list_bulleted Contenido keyboard_arrow_down
ImprimirCitar

Prolog (or PROLOG), from French PROgrammation en LOGique, is a logical and interpreted programming language commonly used in the field of artificial intelligence.

History

It is a programming language devised in the early 1970s at the University of Aix-Marseille I (Marseille, France) by Alain Colmerauer and Philippe Roussel. It was born from a project whose objective was not the translation of a programming language, but the algorithmic treatment of natural languages. Alain Colmerauer and Robert Pasero worked on the natural language processing part and Jean Trudel and Philippe Roussel on the deduction and inference part of the system. Interested in the SL resolution method, Philippe Roussel persuaded its author, Robert Kowalski to collaborate with the project, leading to a preliminary version of the Prolog language at the end of 1971 and the final version appearing in 1972. This first version of Prolog was programmed in ALGOL W.

Initially it was a fully interpreted language until, in 1983, David H.D. Warren developed a compiler capable of translating Prolog into an abstract machine instruction set called the Warren Abstract Machine, or WAM for short. Since then Prolog is a semi-interpreted language.

Although initially it was a language of limited use, the appearance of its interpreters for 8-bit microcomputers (eg: micro-PROLOG) and for 16-bit home computers (example: Turbo Prolog by Borland, among many others), throughout the 1980s, contributed significantly to its popularization. Another important factor in its dissemination was its adoption for the development of the project of the fifth generation of computers in the early 1980s, in which context the parallelized implementation of the language called KL1 was developed and from which part of the modern development of Prolog derives.

The first versions of the language differed, in their different implementations, in many aspects of their syntax, the dialect proposed by the University of Edinburgh being used mostly as the normalized form, until an ISO standard (ISO/IEC) was established in 1995 13211-1), called ISO-Prolog.

Prolog is part of the paradigm of logical and declarative languages, which greatly differentiates it from other more popular languages such as Fortran, Pascal, C or Java.

Backtracking

In the aforementioned programming languages, instructions are normally executed in sequential order, that is, one after the other, in the same order in which they are written, which only varies when a control instruction (a loop, a conditional statement, or a transfer).

Prolog programs are made up of Horn clauses that constitute rules of the type "modus ponendo ponens", that is, "If the is true antecedent, then the consequent" is true. However, the way of writing the Horn clauses is contrary to the usual. Write the consequent first and then the antecedent. The antecedent can be a conjunction of conditions called a objective sequence. Each target is separated by a comma and can be considered similar to an instruction or procedure call in imperative languages. There are no control instructions in Prolog. Its execution is based on two concepts: unification and backtracking.

Thanks to unification, each goal determines a subset of clauses that can be executed. Each of them is called a choice point. Prolog selects the first choice point and continues executing the program until it determines whether the target is true or false.

If it is false, backtracking comes into play, which consists of undoing everything executed, placing the program in the same state it was in just before reaching the selection point. The next pending pick point is then taken and the process is repeated again. All objectives end their execution either success ("true") or failure ("false").

Prolog Programming

There are two types of clauses: Facts and Rules. A rule is of the type:

Head :- Body.

and is read as "The head is true if the body is true". The body of a rule consists of calls to Predicate (logic)predicates, which are called the targets of the rules. The predicate ,/2 (i.e., a parity operator 2 (taking 2 arguments) and name ,) denotes conjunction of objectives, and the operator;/2 denotes disjunction. Conjunctions and disjunctions can only appear in the body, not in the head of the rule. Actually disjunction is not a basic or predefined operator, but rather it is meta-programmed like this:

';' (A,_) :- A.';' (_,B) :- B.

Clauses without a body (ie, antecedent) are called facts because they are always true. An example of a fact is:

cat(tom).

which is equivalent to the rule:

cat(tom) :- bar.

The predefined predicate true/0 is always true.

Given the above fact, one may ask:

Is Tom a cat?

? cat(tom). Yes

What things are cats?

? cat(X). X = Tom

Due to the relational nature of many predicates, their arguments can be used in reverse. For example, length/2 can be used to determine the size (length) of a list: length([a,b,c], L), as well as to generate a skeleton list for a given length (length(X, 5)). Similarly, append/3 can also be used to join or append two lists: append([a,b], [c,d], X), as well as to Split a list into two parts: append(X, Y, [a,b,c,d]). It all depends on which arguments are free variables and which are instantiated. In analogy with imperative programming, the free variables are output arguments and the rest are input arguments. But in Prolog, unlike imperative languages, that role is interchangeable in most predicates. This feature is called reversibility, and valid combinations of input or output arguments are called modes of use. For example, the predicate length/2 is reversible and has three modes of use: both arguments instantiated, the first argument instantiated but the other not, and vice versa. The way of use with the two uninstantiated arguments doesn't make much sense, but it might be supported according to some implementations, in which case it would generate all list skeletons of all possible lengths...

For this reason, a relatively small library of predicates is sufficient for many Prolog programs. All predicates can also be used for unit testing: queries can be embedded in programs and allow automatic compile-time regression testing.

As a general-purpose language, Prolog also has several predefined predicates for interaction with the operating system, such as input/output, graphics, and data communications. These predicates have no relational meaning and are only useful for the side effects they exhibit on the system. For example, the predicate write/1 displays a term on the screen, but its true or false value is irrelevant.

Expressions

Prolog has operators for unification and comparison, either with evaluation or symbolic, such as the following:

  • X is Y %unificación con evaluación.
  • X = Y %unificación simbólica
  • X=:=Y %comparación con evaluación
  • X == Y %comparación simbólica.
? X is 3+5. X = 8? X = 3+5. X = 3+5? 3+5 = 2+6. plaster? 3+5  2+6. No.? 3+5  3+5. plaster

Lists

The representation of simple facts is not common in the classification of elements, but the elements of the same type are grouped in a list.

Lists are collections of elements in Prolog. A list is divided into two parts: Head. It is the first item in the list. Line. It is a list with the rest of the elements of the list. The head and tail of a list are separated by the "|" symbol.

Prolog Code Examples

Simple example

%%%%%%father('John', 'María'). % John is the father of Maryfather('Pablo', 'John'). % Paul is the father of Johnfather('Pablo', 'Marcela'). % Paul is the father of Marcelafather('Carlos', 'Débora'). % Carlos is father of Debora% A is son of B if B is father of Ason(A,B) :- father(B,A).% A is B's grandfather if A is C's father and C's father BGrandfather(A,B) :- father(A,C), father(C,B).% A and B are brothers if A's father is also B's father and if A and B are not the sameBrother(A,B) :- father(C,A) , father(C,B), A  B. % A and B are family if A is father of B or A is son of B or A is brother of Bfamily(A,B) :- father(A,B).family(A,B) :- son(A,B). family(A,B) :- Brother(A,B).%%% consultations%%% Is John Marcela's brother?? Brother('John', 'Marcela').plaster% Is Carlos John's brother?? Brother('Carlos', 'John').No.% Is Pablo the grandfather of Mary?? Grandfather('Pablo', 'María').plaster% Is Mary Paul's grandmother?? Grandfather('María', 'Pablo').No.

Factor of a number

% Syntax is factorial(N, F) - PHP N factor is F (the result is saved in F)factorial(0, 1).factorial(1, 1).factorial(N, F) :- N0, N1 is N - 1, factorial(N1, F1), F is N  F1.% factorial is called recursively leaving the result in F

Fibonacci term of a number

% Syntax is fibonacci(N, F) - 2005 Term N of succession (the result is saved in F).fibonacci(0, 0) :-!fibonacci(1, 1) :-!fibonacci(N ,F) :-N1 is N - 1, fibonacci(N1, F1),N2 is N - 2, fibonacci(N2, F2), F is F1 + F2.%Fibonacci is called recursively leaving the result in F.

Uses of Lists in Prolog

Creating and querying lists

plants(b)apple, orange, lemon, spinach, gardenia, alfalfa, pine]). List(b)1,2,3]).?List(b)H日本語T]). H=1 T=[chuckles]2,3]?List(b)H,J日本語T]). H=1 J=2 T=[chuckles]3]

Length of a list

% If we want to find the length of a list.% The length of an empty list is 0.% The length of any list is the length of the tail + 1.length([],0).length(b)_日本語T],N):length(T,N0), N is N0 + 1.? length(b)a,b,c],L). L = 3? length(b)a,b,c],4). No.

Search for an element

% If we want to determine if an element belongs to a list.% The item belongs to the list if it matches the head of the list.% The item belongs to the list if found in the queue of the list.belongs(X[X日本語_]) .belongs(X[_日本語R]: belongs(X,R). ? belongs(b[a,b,c]). Yes? belongs(b[a[b,c]]). No.? belongs(b)b,c],[a[b,c]]). Yes? belongs(X[a,b]). X = a ; X = b

Remove element from a list

% If we want to remove an element from the list.% If X is the head of the list, tail T is the list without X% If X is not the head of the list, we keep the head of the list % as part of the response and we continue to remove X from tail T.eliminates(X[X日本語T],T).eliminates(X[H日本語T],[H日本語T1]: eliminates(X,T,T1).? eliminates(1[1,2,3,4],R). R = [chuckles]2,3,4]? eliminates(1,R[2,3]). R = [chuckles]1, 2, 3] ; R = [chuckles]2, 1, 3] ; R = [chuckles]2, 3, 1] ? eliminates(X[1,2,3],And). X = 1, And = [chuckles]2,3] ; X = 2, And = [chuckles]1,3] ; X = 3, And = [chuckles]1,2]

Concatenate Lists

% If we want to match two lists ready. % Concatenate an empty list with L is L.% Concatenar XIVAL1 with L2 is to put the first % element of the first list (X) plus the % concatenation of the rest of the list (L1) with L2concaten([],L,L).concaten(b)X日本語L1],L2[X日本語L3]:concaten(L1,L2,L3).? concaten(b)1,2],[3,4],R). R = [chuckles]1, 2, 3, 4].? concaten(X,And[1,2]). X = [], And = [chuckles]1,2] ; X = [chuckles]1], And = [chuckles]2] ; X = [chuckles]1,2], And = []

Check if one list is the inverse of another

% If we want to calculate the reverse of a list. % The reverse of an empty list is an empty list.% The reverse of HATAT is the reverse of T concatenated with H.Reverse([],[]).Reverse(b)H日本語T],L): Reverse(T,R), concaten(R[H],L).? Reverse(b)a,b,c,d],[d,c,b,a]). Yes/Yeah.
% Using a cumulative parameter.Inver(L1,L2):Inver(L1,L2,[]).Inver([],L,L).Inver(b)H日本語T],L,S):Inver(T,L[H日本語S]).? Inver(b)a,b,c,d],[d,c,b,a]). Yes

Contenido relacionado

Wikiproject:News

Integer (data type)

The typical arithmetic operations: addition, subtraction, multiplication, and division can be performed on integer data. In the case of division, the result...

NSF Net

Acronym for National Science Ffoundation's Network. The NSFNET began with a series of networks dedicated to the communication of research and education....
Más resultados...
Tamaño del texto:
undoredo
format_boldformat_italicformat_underlinedstrikethrough_ssuperscriptsubscriptlink
save