Download Logic Programming Lecture 14: Understanding Prolog through Logic and Unification - Prof. Z and more Assignments Programming Languages in PDF only on Docsity!
Prof. Su ECS 140A Lecture 14 1
Programming Languages
ECS 140A
13:40-15:00 Tu/Th
55 Roessler
Prof. Su ECS 140A Lecture 14 2
Announcements
Prof. Su ECS 140A Lecture 14 3
Introduction
- What is logic programming?
- Use facts and rules to represent information
- Use logical deduction to answer queries
- It is based on first-order logic (FOL)
- Use logic to represent information
- Use proof rules/techniques to derive more information
- Logic programming view (Kowlski's equation): Algorithm = Logic + Control
- Logic: What needs to be done (provided by the user/programmer)
- Control: How it needs to be done (derived by system based on the logic)
- Programming in terms of deduction rather than evaluation
- Functional programming: expression evaluates to a value
- Imperative programming: program runs and updates some store
Prof. Su ECS 140A Lecture 14 4
A Simple Example for Illustration (I)
- Let's look at the append relationship on lists
[] empty list [1,2,3] list with three elements
- append(X,Y,Z) is true if append(X,Y) = Z append([], [1], [1]). append([1], [], [1]). append([1,2], [3], [1,2,3]). ... ...
- This is an infinite set
Prof. Su ECS 140A Lecture 14 5
A Simple Example for Illustration (II)
- We can ask some questions about append
- append([],[],[]). yes
- append([],[1],[1]). yes
- append([1,2],[3], [1,2,3]). yes
- append([1],[],[]). no
- What if we want to ask
- append(X,[1],[1]).
- append([1,2],X,[1,2,3]).
- append(X,Y,[1,2]).
- append(X,Y,Z).
Prof. Su ECS 140A Lecture 14 6
Key to LP
- You do not need to specify exactly how to
compute a result
- But simply need to describe the form of the
results
- The computer system can somehow determine
how to compute the result
Prof. Su ECS 140A Lecture 14 10
Some Examples (3)
| ?- append(X,Y,[1,2,3]).
X = [] Y = [1,2,3]? ;
X = [1] Y = [2,3]? ;
X = [1,2] Y = [3]? ;
X = [1,2,3] Y = []? ;
no | ?-
; means to show more answers. ; no at the end means no more answers.
Q: what happens if we ask?
| ?- append([1,2],X,X).
infinite list: X = [1,2,1,2,1,2,1,2,...]
Prof. Su ECS 140A Lecture 14 11
More on Facts
- Facts express unconditional information: father(john, mary). % john is mary's father father(sam, john). father(sam, kathy).
- Examples |-? father(john,mary). yes |-? father(sam,X). X = john? ; X = kathy? ; no |-? father(X, mary). X = john? ; no |-? father(X,john), father(X,kathy). ; means "addedโ together X = sam? ; no
Prof. Su ECS 140A Lecture 14 12
More on Rules
- A rule is defined in the following way :- , , ..., HEAD CONDITIONS
- Means if AND AND ... AND then
- A fact is just a special case of a rule: with no conditions
- Example: grandpa(X,Y) :- father(X,Z), father(Z,Y).
|-? grandpa(X,Y). X = sam, Y = mary? ; no
Prof. Su ECS 140A Lecture 14 13
More on Goals (Queries)
- Questions that we can ask a system
- Goal of the form: g1,g2,...,gk
|-? father(X,john), father(X,kathy). |-? father(_,X). ; anybody with a father
- Notice the closed world assumption (CWA)
- Everybody has a father, but we only get ...
- Whatever is represented in the database
- Plus what can be deduced from the facts and rules
- Nothing more
Prof. Su ECS 140A Lecture 14 14
Unification (1)
- How does prolog do deductions?
- Based on a process called unification
- Unification is a process to match two or more terms
- Similar to pattern matching
- But pattern-matching goes only one way: variable to a pattern
- Unification can go both ways
- Example
- foo(X,b) and foo(a,Y) matches if X = a, Y = b
- f(a,b) is an instance of both
- Unification attempts to find if there is a common
instance of two terms
- Definition:Two terms t1 and t2 unify, if they have a
common instance.
Prof. Su ECS 140A Lecture 14 15
Unification (2)
- When does unification happen?
- When two terms are checked for equality |-? foo(X,b) = foo(a,Y).
- Example: | ?- foo(X,b)=foo(a,Y). X = a Y = b yes
- When a rule is applied | ?- [user]. same(X,X). yes | ?- same(foo(X,b), foo(a,Y)). X = a Y = b yes
Prof. Su ECS 140A Lecture 14 19
Examples on Lists
member(X, [X|]). member(X, [|Y]) :- member(X, Y). Queries: member(a, [c,b,a,d]). member(a, []).
- length of a list: length([], 0). length([_|T], L) :- length(T, K), L is K+1.
- reverse a list:
reverse([], []). reverse([H|T], L1) :- reverse(T, L2), append(L2,[H],L1).
Prof. Su ECS 140A Lecture 14 20
A Bit on Arithmetic
- Prolog has the usual +, -, *, etc operators for arithmetic
- And each implementation may have many more
- Use infix notation: 3 * 4
- Arithmetic expressions not evaluated unless forced to
| ?- X=2+3. X = 2+
NOT X = 5
- Use "is" to force their evaluations | ?- X is 2+3. X = 5
Prof. Su ECS 140A Lecture 14 21
Examples with Arithmetic
- factorial: fac(N,N!) fac(0,1). fac(N,R) :- N > 0, M is N-1, fac(M,S), R is SN.*
- Fibonacci: fib(N,M)--M is the Nth Fibonacci number fib(0,0). fib(1,1). fib(N,M) :- N > 1, N1 is N-1, N2 is N-2, fib(N1,M1), fib(N2,M2), M is M1+M2.
Prof. Su ECS 140A Lecture 14 22
Binary Trees in Prolog (1)
- How to represent binary trees?
- Use predicates, e.g., "tree" void: empty tree tree(K,L,R): labeled with K, left subtree L, right subtree R
- Examples
- tree(2, void, void).
- tree(4, tree(2,void,void), tree(10,void,void)).
Prof. Su ECS 140A Lecture 14 23
Binary Trees in Prolog (2)
- tree height height(void, 0).
height(tree(_, L, R), H) :- height(L, H1), height(R, H2), H1 > H2, H is H1+1.
height(tree(_, L, R), H) :- height(L, H1), height(R, H2), H1 =< H2, H is H2+1.
Prof. Su ECS 140A Lecture 14 24
Binary Trees in Prolog (3)
- insert into binary search tree
insert(K,void,tree(K,void,void)). % base case
insert(K,tree(K,L,R),tree(K,L,R)). % no duplicates
insert(K,tree(N,L,R),tree(N,Lnew,R)) :- K < N, insert(K,L,Lnew). % to the left
insert(K,tree(N,L,R),tree(N,L,Rnew)) :- K > N, insert(K,R,Rnew). % to the right
Prof. Su ECS 140A Lecture 14 28
Parser (2)
- Construct a Prolog predicate parse(L)
- Return yes if L is a list of tokens representing a legal expression
- Examples parse([num]). yes parse([num,plus,num,times,num]. yes parse(L) :- parseE(L). parseE(L) :- parseT(L). parseE(L) :- append(L1, [plus|L3], L), parseE(L1), parseT(L3). parseT(L) :- parseF(L). parseT(L) :- append(L1, [times|L3], L), parseT(L1), parseF(L3). parseF([lp|L]) :- append(L2, [rp], L), parseE(L2). parseF([num]).
- For illustration only, clearly not the best way to do it
Prof. Su ECS 140A Lecture 14 29
More Logical Operators (1)
- true
- fail
- = (equality)
- t1 = t2 succeeds if t1 and t2 unifies
- Examples | ?- 5 = 5 succeeds | ?- 5 = 2+3 fails
- = (inequality)
Prof. Su ECS 140A Lecture 14 30
More Logical Operators (2)
- not (negation)
- not (X) succeeds if fails to satisfy X
- order matters
- X=2, not(X=1) succeeds because
- To satify X=2, we can unify X with 2
- And then 2=1 fails, thus not(X=1) is satisfied
- not(X=1), X=2 fails because
- X=1 succeeds, thus not(X=1) fails
- Then the whole goal fails
- Different from the logical interpretation where both are equivalent
- g1; g2: disjunction of goals
- Its meaning is: g1 OR g
- The goal succeeds if g1 succeeds or g2 succeeds
Prof. Su ECS 140A Lecture 14 31
Control in Prolog (1)
- How is a prolog program evaluated?
- goal order: subgoals are processed left-to-right
- rule order: rules are applied top-to-bottom
- Answer to a query is affected by
- Goal order in the query
- Rule order in the database of facts and rules
- Recall the earlier example with negation
not(X=1), X=
Prof. Su ECS 140A Lecture 14 32
Control in Prolog (2)
- Program control 1 current_goal := query 2 while current_goal is nonempty do 3 assume current_goal = g1,...,gk 4 choose the leftmost goal g 5 if there is a rule applies to g1 then 6 select first such rule H :- C1,...,Cn 7 let s be the most general unifier of H and g 8 current_goal := s(C1), ..., s(Cn), s(g2), ..., s(gk) 9 else 10 backtrack 11 od 12 success
- Ignore โmost general unifierโ
- Just think that it's a solution to a unification equation
Prof. Su ECS 140A Lecture 14 33
Control in Prolog (3)
- Example append([],Y,Y). append([H|X],Y,[H|Z]) :- append(X,Y,Z). prefix(X,Z) :- append(X,Y,Z). suffix(Y,Z) :- append(X,Y,Z). sublist1(S,L) :- prefix(X,L), suffix(S,X). sublist2(S,L) :- suffix(S,X), prefix(X,L).
- Draw pictures to show the logic of sublist1 and sublist |<-- X -->| +-------+-+-----+ | |S| | +-------+-+-----+ |<-- L -->|
Prof. Su ECS 140A Lecture 14 37
How Does Search Work - Example 2 (II)
append([b], _2, [a,b,c]) matches no rule or fact, backtrack
append([H|X], Y, [H|Z]) :- append(X,Y,Z) _1 = [_3|_4], Y = [b], L =[_3|_5]
append(_4, [b], _5), prefix([_3|_5], [a,b,c]) append([], Y, Y) _4 = [], Y = [b]
prefix([_3, b], [a,b,c]) prefix(X, Z) :- append(X, Y, Z) Y = _
Prof. Su ECS 140A Lecture 14 38
How Does Search Work - Example 2 (III)
append([_3,b], _6, [a,b,c]) append([H|X], Y, [H|Z]) :- append(X, Y, Z). H = a = _3, Z = [b,c]
append([b], _6, [b,c]) append([H|X], Y, [H|Z]) :- append(X, Y, Z). H = b, Z = [c]
append([], _6, [c]) append([], Y, Y). Y = _6 = [c]
yes
Prof. Su ECS 140A Lecture 14 39
Order in the Goals and Rules (1)
- Does goal order matter? ?- suffix([a], L), prefix(L, [a,b,c]). L = [a] ; [ infinite computation ]
- Why? Because of the leftmost subgoal ?- suffix([a], L). L = [a] ; L = [_1, a] ; L = [_1, _2, a] ; ...
- Infinite # of solutions, only the first satisfies the
prefix subgoal
- But, if we reorder them...
Prof. Su ECS 140A Lecture 14 40
Order in the Goals and Rules (2)
- Does rule order matter?
- Yes: rule order changes the order in which solutions are
searched
app2([H|X], Y, [H|Z]) :- app2(X, Y, Z). app2([], Y, Y). ?- append(X, [c], Z). X = [] Z = [c]? ; X = [A] Z = [A,c]? ; โฆ
?- app2(X, [c], Z).
X = [_1|_2], Y = [c], Z = [_1|_3]
app2(_2, [c], _3).
Prof. Su ECS 140A Lecture 14 41
Cuts (1)
- Cuts allow you to prune out or "cut out" an unexplored part of a prolog search tree - If you get this far, you have picked the correct rule for this goal, then stop looking for anything else - If you get to here, you should stop trying to satisfy this goal - If you get here, you have found the only solution of the problems
- Cuts are written as C :- A_1, A_2, ..., A_m, !, B_1, B_2, ..., B_n. - semantics: tells control to backtrack past A_1, ..., A_m without considering any of the rules for them
- "green cuts"
- Do not change results, used to make execution more efficient
- "red cuts"
Prof. Su ECS 140A Lecture 14 42
Cuts (2) - โred cutsโ
- Rules for max: max(A,B,M) :- A < B, M = B. max(A,B,M) :- A >= B, M = A.
- simplify the code: max(A,B,B) :- A < B. max(A,B,A).
?- max(3,4,M). M = 4; M = 3
- fix it: max(A,B,B) :- A < B,!. max(A,B,A).
Prof. Su ECS 140A Lecture 14 46
Negation with Cuts
not(X) :- X, !, fail.
not(_).
Prof. Su ECS 140A Lecture 14 47
Summary
- Computation through manipulation of relationships
- A Prolog program consists of
- Facts: unconditional relationships among terms
- Rules: relationships among terms that may be true under certain conditions
- Queries that must be true give a set of facts and rules
- A prolog program merely specifies the various
relationships among terms
- Prolog system defines how the computation is carried out
- Unification of variables and terms: assignment of values to variables so that relationships match
- Implement control by searching the different rules: Employ backtracking to search for all solutions
Prof. Su ECS 140A Lecture 14 48
Extra
- Give some more examples if time permits
- Discuss homework assignment