100 likes | 228 Views
Prolog. Logic ( Aristotle 383-322 B.D.) Every Greek is a philosopher Socrates is Greek . Therefore, Socrates is a philosopher. Prolog philosopher(X) :- greek(X). greek(socrates). ?- philosopher(Y). Y = socrates; No. Prolog.
E N D
Prolog • Logic ( Aristotle 383-322 B.D.) Every Greek is a philosopher Socrates is Greek . Therefore, Socrates is a philosopher • Prolog philosopher(X) :- greek(X). greek(socrates). ?- philosopher(Y). Y = socrates; No.
Prolog • Prolog is a language for symbolic, non-numeric computation. • The basic statements in prolog are: facts, rules and questions. • Facts the simplest kind of a statement. Facts are means of stating that a relationship holds between objects. E.g. parent(abraham, isaac). male(abraham). • A finite set of facts constitutes a program. • Questionsare a means of retrieving information from a logic program. A question asks whether a certain relation holds between objects, e.g. ?- parent(abraham, isaac). Answering a question with respect to a program is determining whether the questions is a logical consequence of the program.
Biblical family database parent(terach, abraham). parent(terach, nachor). parent(terach, haran). parent(abraham, isaac). parent(haran, lot). parent(haran, milcah). parent(haran, yiscah). parent(sarah, isaac). male(terach). male(abraham). male(nachor). male(haran). male(isaac). male(lot). female(sarah). female(milcah). female(yiscah). Prolog
Prolog • Rules are statements of the form: A:-B1, B2, …, Bn. where n>=0, A is the head of the rule, and the Bi’s are the body. Both A and the Bi’s are goals. A rule can be as well be interpreted as a procedure call; that is, to satisfy A we have to satisfy first all Bi’s.
Prolog • Atoms anna alpha_beta miss_jones nil x25 ‘Tom’ ‘Sarah Jones’ • Numbers 1 1331 0 -97 3.14 -0.000035 • Variables X Result Object2 ShoppingList _x23 _24
Prolog • Structures date(1, may, 2001) point(1,1) seg(P1, P2) employee(123_4325, ‘Peter Smith’, masculine, ‘La Meza st.’, 10)
Prolog • Lists [] Empty list [H|R] H is the head of the list and R is the rest of the list (R is a list). Samples: [a] [a|[]] one element list [a, b, c, d] [a|[b, c, d]] [a|[b|[c|[d|[]]]]] This list contains the elements a, b, c and d and in that order.
Prolog Invert(L1, L2) :- invert(L1, [], L3). invert([], L, L). invert([X|L1], L2, L3) :- invert(L1, [X|L2], L3). • List processing programs: member(X, [X|R]). member(X, [_|R]) :- member(X, L). append([], L, L). append([H|R], L2, [H|L3]) :- append(R, L2, L3). first([X|R], X). last([X], X). last([X|R], Y) :- last(R, Y).
Prolog • Backtracking (a1) a:- b,c,d. j(1). k(1). (a2) a:- e,f. J(2). k(2). (a3) a:- g,h,i. j(3). e. (b1) b:- j,k. c(1). f. (b2) b:- l. c(2). (g). (d1) d:- m,n. c(3). h(1). (d2) d :- o. l. h(2). i. m. n. o.