11 Prolog
11 Prolog
1 2
Computation as inference on logical KBs Logic programs are also called expert systems
Ordinary Programming Logic Programming – Problem domain experts sit down and encode lots of
1. Identify problem Identify problem
information into the KB
2. Assemble information Assemble information – System then reasons using that “expert” knowledge
3. Plan out your algorithms Go have a beer! – Used for medical diagnosis, Q/A systems, etc.
4. Program the solution Encode information in the KB – KB must be in datalog format: FOL with no functions
5. Encode problem as data Encode problem as logical facts
6. Run program on data Ask queries
7. Debug errors Find/correct false facts Fall loosely under the “think rationally” quadrant
of AI research
Should be easier to debug capital(NewYork,USA) than find that pesky x += 2!
3 4
Prolog is probably the most common logic To Solve a Goal (i.e. answer a query)
programming language Try to unify:
A program is: – First with each of the ground facts
– A set of logic sentences in HNF (definite clauses) – When all facts fail, then with each of the consequents
• Called the database (DB, basically the KB) of the rules in the order in which they occur in DB
• Ordered by programmer (top to bottom)
Successful unification with a fact:
– Executed by specifying a query to be proved – Solved, pop goal from stack
• Backward-chaining with GMP
• Uses DFS on the ordered facts and rules Successful unification with a rule:
• Searches until a solution is found (or times out) – Solve the sub-goals in DFS manner (i.e. recursively
– Can find multiple solutions attempt to solve each of the rule’s premises)
5 6
1
Prolog Execution Prolog Execution
7 8
hostile(X) :- enemy(X,america).
Query: criminal(X) :-
– FOL: Q1 ∧ Q2 ∧ … ∧ Qn Prolog: ?- Q1, Q2, … , Qn. american(X), weapon(Y),
sells(X,Y,Z), hostile(Z).
9 10
The implementation of prolog that we’ll use on the TUX Once YAP is running and the criminal KB is
% yap
To end prolog, type:
The prolog extension is *.pl To view the entire BC search, YAP has a
– Try not to confuse it with perl programs debugging feature called “spy”:
– Note that you don’t need the extension when loading a program – Type spy(predicate). to turn it on
into prolog, it knows to look for the file with a *.pl extension
11
– And nospy(predicate). to turn it off 12
2
Another Prolog Example Another Prolog Example
Let’s consider a simple KB that expresses facts How should we define the relation sibling?
about a certain family: – Two people are siblings if they have the same mother
father(tom,dick). mother(tom,judy). and the same father (ignoring half-siblings, step-
father(dick,harry). mother(dick,mary). siblings, etc.)
father(jane,harry). mother(jane,mary).
How about this:
Now let’s also think about creating some FOL
sibling(X,Y) :- mother(X,M), mother(Y,M),
rules for defining family relations: father(X,M), father(Y,M).
– Parent? Let’s run this and see what happens!
parent(X,P) :- mother(X,P).
parent(X,P) :- father(X,P).
– Oops! Need to make sure X ≠ Y!
sibling2(X,Y) :- mother(X,M), mother(Y,M),
– Grandmother? father(X,M), father(Y,M), X\=Y.
granny(X,G) :- parent(X,Y), mother(Y,G).
13 14
Prolog has built-in operators (predicates) for mathematical Suppose we want to define an “append” operator for
functions and equalities: lists… that is to take two lists L1 and L2, and merges their
– x = 2×(y+1) X is 2*(Y+1). elements together into a new list L3
– d < 20 D < 20. – Usually this is done with a function
– 1≤2 1 @=< 2. • e.g. L3 = append(L1,L2)
– x=y X = Y. – But prolog programs are datalog: no functions allowed!
– x≠y X \= Y. • Create make-shift functions by defining predicates with the return
value included as a parameter
The major data structure for Prolog is the list
• e.g. append(L1,L2,L3)
– [] denotes an empty list
How about defining a simple predicate that takes the first
What we need to do is take one list and recursively Now we can ask the queries:
add one element at a time from the other list, until ?- append([1,2,3], [a,b,c], [1,2,3,a,b,c]).
3
Partitioning Lists Partitioning Lists
Another useful application might be how to First, let’s think of the base case for our
recursively sort prolog lists partitioning predicate
partition(E,[],[],[]).
Most sorting algorithms utilize some partitioning • That is, an empty list gets split into two empty lists
method, where the list L is split into two sublists
Second, we must consider the recursive aspect:
L1 and L2 based on a particular element E – Upon considering a new element H at the head of the
– e.g. splitting list [1,5,3,9,7,4,1] on element [5] would list, what conditions must we account for?
yield the lists [1,3,4,1] and [5,9,7] • If H < E, or if H ≥ E (to determine which sublist)
This would be a useful method to define first – Since we have two different cases, each with a different
desired result, we need two recursive definitions
partition(E, L, L1, L2).
19 20
If H < E, then we want to add H to the first list L1: Now that we know how to partition one list into
partition(E,[H|T],[H|T1],L2) :- two, and also how to append two lists together, we
H < E,
partition(E,T,T1,L2). have all the tools we need to sort a list!
However, if H ≥ E then we’ll add it to the second list L2: Let’s consider insertion sort:
partition(E,[H|T],L1,[H|T2]) :- – Walk through each position of the list
H @>= E,
partition(E,T,L1,T2).
– For each position, insert the list item i that belongs in
that position, relative to other items in the list
These predicates, together with the base case, will partition
21 22
As always, we will need a base case for insertion sort A lot of early natural language processing (NLP) research
(assume that an empty list is sorted): was historically done using logic systems, because HNF
isort([],[]). rules are analogous to grammar productions
For the recursive aspect, we can walk through the whole – e.g. A simple English grammar: S → NP VP (NP)
list, and backtrack, inserting each element where it belongs • S means “sentence,” NP means “noun phrase,”
and VP means “verb phrase”
in the pre-sorted list:
isort([H|T], F) :-
– In prolog:
s(Input) :- np(Input, Mid), vp(Mid, []).
isort(T,L), s(Input) :- np(Input, Mid1), vp(Mid1, Mid2), np(Mid2, []).
partition(H,L,L1,L2),
Once we add definitions for np and vp (and ultimately
append(L1,[H|L2],F).
noun, verb, prep, det, etc.), we will have a full-blown
We can do something similar to implement quicksort, but
4
Ordering Prolog Rules Other Logic Systems