Syntax Terms Atoms a, ab123, z_999, a_b_c, 'an atom sample' Numbers Variables A, Z_1_2, _123, _ Compound Terms mother(ali, X), f(g(X, _))
Clauses, Programs & Queries Clauses Fact: A certain instance of a relation is true. Rule: The goal expressed by its head is true, if we show that all of the expressions in the rule's body are true. Program: Sequence of clauses Queries: Ask prolog interpreter whether all its predicates are provably true. is_bigger(elephant, donkey). is elephant bigger than donkey. small(X), green(X), slimy(X). is there any X that small(X), green(X) and slimy(X) are all true.
Goal Execution All man are mortal. Socrates is a man. Hence Socrates is mortal. mortal(X):-man(X) man(socrates) ?- mortal(socrates). Yes mortal(socrates) is the initial goal. mortal(X) is the first possible fact to match. X=socrates Variable inst. Is extended to the body of the rule. man(socrates) man(socrates) is our new goal It is matched to the fact man(socrates). Current goal succeeds, so initial goal succeeds.
List Manipulation [elephant, horse, dog, cat] [elephant, , X, parent(X, tom), [a, b, c], f(22)] [a,b,c] =.(a,.(b,.(c, ))) Head: first element Tail: Rest of the list ?- [1,2,3,4] = [Head | Tail] Head = 1 Tail=[2,3,4] Yes
Built-in Predicates length([1,2,3], X). X=3 length(List, 3). List=[_G123, _G234, _G456] member(Elem, List) append(List1, List2, List3) last(Elem, List) reverse(List1, List2) % reverse order of List1 select(List1, Elem, List2) % select elements of List1 after Elem
Arithmetic Expressions 2+3 = 5 % they do not match X is 2+3, X=5. X=5 5 is 2+3. Yes 2+3 is 5. % is evaluates rhs and matches it to left. No max, min, abs, //, **... for rhs of is operator
Relations Compare two evaluated arithmetic expressions. Whenever an arithmetic expression is used, the arguments are evaluated (no need to use is operator.), =, =:=, =\= =:= arithmetically equal, = pattern matching 2 ** 3 =:= Yes 2 ** 3 = No
Operators Every operator is associated with an integer denoting its precedence. The lower the precedence the stronger the operator is binding. The precedence of a term is defined as 0 unless its principal functor is an operator > * * 2 -> 400 elephant -> 0 (3+5) -> 0
Backtracking, Cuts & Negation During proof search, Prolog keeps track of choice points; situations where there is more than 1 match. When the chosen path is a failure/the user asks for alternative solutions, the systems jumps back to the last choice point and try the next alternative. permutation (, ). permutation ( List, [Elem | Perm]) :- select(List, Elem, Rest), permutation (Rest, Perm).
Backtracking ?- permutation( [1,2,3], X). X = [1,2,3]; X = [1,3,2]; X = [2,1,3]; X = [2,3,1]; X = [3,1,2]; X = [3,2,1]; No
Cuts It is possible to cut out backtracking points and preventing unwanted alternative solutions. ! is a predicate and can be placed anywhere inside rule body. Execution of ! will always succeed remove_dup ([Head|Tail], Result) :- member(Head, Tail), !,remove_dup(Tail, Result). Without cut, alternative solution search will continue. But when cut is passed this isn't possible anymore. remove_dup([a,b,b,c,a], X). X = [b,c,a]; No.
Negation In order to give a positive answer to a query Prolog has to construct a proof to show that the set of facts and rules implies that query. Yes means the query is provably true. No doesn’t mean that the query is necessarily false, just not provably true. Closed world assumption: Negating everything that is not explicitly in the program.
References Endriss, U, Lecture Notes, An Introduction to Prolog Programming, King's College, London