Beruflich Dokumente
Kultur Dokumente
Prolog was invented in the early seventies at the University of Marseille. Prolog
stands for PROgramming in LOGic. It is a logic language that is particularly used
by programs that use non-numeric objects. For this reason it is a frequently used
language in Artificial Intelligence where manipulation of symbols is a common
task. Prolog differs from the most common programmings languages because it
is declarativre language. Traditional programming languages are said to be
procedural. This means that the programmer specify how to solve a problem. In
declarative languages the programmers only give the problem and the language
find himself how to solve the problem.
Although it can be, and often is, used by itself, Prolog complements traditional
languages when used together in the same application.
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.
2.2 Data types
○ 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 ))
○ [] represents nil.
E.g.,
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.
○ ?- car( [[ a, b ], c, d ], X )
○ X = [a, b]
○ subset
○ intersection
○ 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 ).
3.1 Character set
• Special characters
• Prolog-specific characters
• Delimiters
3.2 Operators
likes(leona, water)
swims(leona)
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.
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
(a + b) - c
Prolog solves this problem by requiring two extra pieces of information about
each operator at the time of its declaration: precedence and associativity.
Every entry into the interpreter is a goal that Prolog tries to satisfy.
Syntax:
The Prolog built-in predicate op serves to define the Type and Precedence
of infix and postfix, and prefix operators in Prolog terms. Prolog terms
normal begin with the functor (e.g. likes, in likes(mary, pizza)) but
exceptions exist - for example, arithmetic expressions are written in the
usual infix way (i.e. as X + 1, rather than +(X, 1)), and negation can be
written without parentheses, as a prefix operator: not P.
The table below lists the predefined infix operators in SWI-Prolog. You
may wish to add infix operators of your own. For example, you might wish
to define an infix and. This can be done as follows:
This declares and to be an operator with precedence 700 and type xfy.
Note two things: (1) the fact that these things are referred two as
operators does not mean that an operation is performed when they are
encountered. This is not usually the case; and (2) the declaration of an
operator only affects the external appearance of the operator in your
program - internally, the standard representation is used - for example X +
1 really is internally represented by Prolog as +(X, 1).
Precedence is an integer between 0 and 1200. Precedence 0 removes the
declaration. Type is one of: xf, yf, xfx, xfy, yfx, yfy, fy or fx. The f indicates
the position of the functor, while x and y indicate the position of the
arguments. Thus xfx, xfy, and yfx all indicate an infix operator. y should be
interpreted as "in this position a term with precedence equal to or less
than the precedence of the functor should occur". For x the precedence of
the argument must be strictly less than that of the functor. The precedence
of a term is 0, unless its principal functor is an operator, in which case the
precedence is the precedence of this operator. A term enclosed in
brackets (…) has precedence 0.
?- X -> Y; Z.
?- X -> Y; Z.
factorial(0,1) :- !.
factorial(N,FAC) :-
N2 is N - 1, factorial(N2,FAC2), FAC is FAC2*N.
(X -> Y); Z :- X, !, Y.
(X -> Y); Z :- !, Z.
X;Y :- X.
X;Y :- Y.
Recursion sample:
unsigned long FactorialR(int Num)
{
if (Num == 0)
return(1);
return(Num*FactorialR(Num-1));
}
This function will take 'Num' as parameter and executes the same function till it
satisfies exit condition i.e 'Num == 0'.
For ex: If we pass 5 as parameter to this function it will execute as following:
1. Num[5]*Factorial(Num-1)[?] -> 5*? it doesn't return immediately
2. Num[4]*Factorial(Num-1)[?] -> 4*? it doesn't return immediately
3. Num[3]*Factorial(Num-1)[?] -> 3*? it doesn't return immediately
4. Num[2]*Factorial(Num-1)[?] -> 2*? it doesn't return immediately
5. Num[1]*Factorial(Num-1)[?] -> 1*? it doesn't return immediately
After Num == 0, then it will start returning result of processed result of the
function in reverse order like following:
1. Num[1]*Factorial(Num-1)[ 1] -> 1* 1 (1 is returned value of the previous
function call)
2. Num[2]*Factorial(Num-1)[ 1] -> 2* 1 (1 is returned value of the previous
function call)
3. Num[3]*Factorial(Num-1)[ 2] -> 3* 2 (2 is returned value of the previous
function call)
4. Num[4]*Factorial(Num-1)[ 6] -> 4* 6 (6 is returned value of the previous
function call)
5. Num[5]*Factorial(Num-1)[24] -> 5*24 (24 is returned value of the
previous function call)
Finally, function will return 120 result to the caller.
Iteration sample:
unsigned long FactorialI(int Num)
{
int Result = 1;
HELLO WORLD
// the main program (this is a comment)
Hello:-
nl,
write('Hello world!' ).
}
FOR LOOP
repeat loop
Arithmetic
?- X is 10 + 5.
X = 15 ?
yes
?- X is 10 - 5.
X = 5
yes
?- X is 10 * 5.
X = 50
yes
?- X is 10 / 5.
X = 2
yes
?- X is 10 + 5 * 6 / 3.
X = 20
yes
Logic Programming
Okay, say you have cat and dog. First you say that they're animals
animal(cats)
animal(dogs)
Somewhere above, you may have defined one or both of these (or
neither). For this example, let's say you have both:
love(cats)
love(dogs)
animalsthatilove(X):-love(X)
will show:
cats
dogs
loverelations(cats,dogs)
loveofrelated(X):-loverelations(cats,X)
will show:
dogs
For Loop(Output Odd numbers from 1-11)
command_loop:-
repeat,
write('Enter command (end to exit): '),
read(X),
read(X/2),
write(X), nl,
X = end.
Quicksort
(<- (echo)
(is ?x (read))
(echo ?x))
http://www.cs.otago.ac.nz/cosc347/References/LP/LeedsTutorial/book.html
http://wwwcgi.rdg.ac.uk:8081/cgibin/cgiwrap/wsi14/poplog/prolog/ploghelp/conditi
onal