0% found this document useful (0 votes)
9 views5 pages

LectureA

The document provides an overview of logic programming using Prolog, detailing the structure of terms, predicates, and clauses. It explains the concepts of lists, recursion, and various predicates for list processing, including sublists and set operations. Additionally, it covers utility predicates for input/output operations in Prolog.

Uploaded by

César
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

LectureA

The document provides an overview of logic programming using Prolog, detailing the structure of terms, predicates, and clauses. It explains the concepts of lists, recursion, and various predicates for list processing, including sublists and set operations. Additionally, it covers utility predicates for input/output operations in Prolog.

Uploaded by

César
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Logic Programming Structures have two interpretations:

with Prolog
As predicates (relations):
presidentof(marySue, iowa).
Prolog programs are constructed from terms:
prime(7).
Constants can be either atoms or numbers:
between(rock,X,hardPlace).
• Atoms: Strings of characters starting with a
lower-case letter or enclosed in apostrophes.
• Numbers: Strings of digits with or without a As structured objects similar to records:
decimal point. computer(name(herky), locn('MLH 303'),
make('IBM'), model('RS6000'))
Variables are strings of characters beginning list(3, list(5, list(8, list(13, nil))))
with an upper-case letter or an underscore.

Structures consist of a functor or function Prolog Programs


symbol (looks like an atom), followed by a list
of terms inside parentheses, separated by A Prolog program is a sequence of statements,
commas. called clauses, of the form
P0 :- P1, P2, …, Pn.
Each of P0, P1, P2, …, Pn is an atom or structure.
A period terminates every Prolog clause.

Appendix A 1 Appendix A 2

Declarative meaning: Lists in Prolog


“P0 is true if P1 and P2 and … and Pn are true”
A list of terms can be represented between
Procedural meaning: brackets:
“To satisfy goal P0, satisfy goal P1 [a, b, c, d]
then P2 then … then Pn”.
Its head is “a” and its tail is “[b, c, d]”.
• P0 is called the head goal of a clause.
The tail of [a] is [ ], the empty list.
• Conjunction of goals P1, P2, …, Pn forms
the body of the clause. Lists may contain lists:
[3.3, [a, 8, [ ]], [x], [p,q]] is a list of four items.
• A clause without a body is a fact:
“P.” means “P is true” or Special form to direct pattern matching:
“goal P is satisfied” • The term [X|Y] matches any list with at least
one element:
• A clause without a head
X matches the head of the list, and
“:- P1, P2, …, Pn.” or “?- P1, P2, …, Pn.”
Y matches the tail.
is a query interpreted as
“Are P1 and P2 and … and Pn true?” or
“Satisfy goal P1 then P2 then … then Pn”
Appendix A 3 Appendix A 4
• The term [X,Y,77|T] matches any list with at Recursion
least three elements whose third element
is the number 77: Most interesting algorithms involve repeating
X matches the first element, some group of actions.
Y matches the second element, and Prolog implements repetition using recursion.
T matches rest of the list after the third item. Recursion is closely related to mathematical
induction, requiring two cases:
Using these pattern matching facilities, values Basis: Solve some initial or small version of the
can be specified as the intersection of problem directly.
constraints on terms instead of by direct Recursion: Assuming the algorithm works on
assignment. smaller or simpler versions of the problem,
solve an arbitrary instance of the problem.

Example
sublist(S,L) succeeds if and only if the list S is a
sublist of the list L.
sublist([a,b,c], [a,b,c,d,e]) succeeds.
sublist([c,d], [a,b,c,d,e]) succeeds.
sublist([b,d], [a,b,c,d,e]) fails.

Appendix A 5 Appendix A 6

For list algorithms, the basis usually deals with These two cases correspond to the situation
an empty list, certainly the smallest list. where the sublist begins at the start of the list
(Some algorithms for lists do not handle the empty or the sublist begins later in the list, the only
list and so begin with a singleton list, [H]). two possibilities.

For the recursion step, we define the algorithm for


Sample Executions
the arbitrary list, [H|T], assuming that it works
correctly for its tail T, a smaller list. sublist([b,c,d], [a,b,c,d,e,f]) %5
because sublist([b,c,d], [b,c,d,e,f]) %2
Sublist basis
because initialsublist([c,d], [c,d,e,f]) %4
The empty list is a sublist of any list.
sublist([ ], L). %1 because initialsublist ([d], [d,e,f]) %4
because initialsublist ([ ], [e,f]) %3
Sublist recursion
The list [H|T] is a sublist of the list [H|U] if the sublist([b,d], [b,c,d]) fails %2
list T is a sublist of the list U starting at the first
position. because initialsublist([d], [c,d]) fails
sublist([H|T], [H|U] :- initialsublist(T,U). % 2 and %5
initialsublist([ ], L). %3 because sublist([b,d], [c,d]) fails %5
initialsublist([H|T],[H|U] :- initialsublist(T,U). % 4
because sublist([b,d], [d]) fails
Or the list S is a sublist of the list [H|U] if it is a
sublist of U.
sublist(S, [H|U] :- sublist(S,U). %5
Appendix A 7 Appendix A 8
Testing Primes in Prolog Predicate sqrt

Consider this Wren program


Predicate prime
program sqrt is
prime(P) succeeds iff P>0 is prime. var n,sqrt,odd,sum : integer;
Assume the predicate begin
sqrt(N,S) iff S = floor(sqrt(N)). read n;
sqrt:=0; odd:=1; sum:=1;
while sum<=n do
prime(2).
sqrt:=sqrt+1;
prime(N) :- sqrt(N,S), okay(N,S,3). odd:=odd+2;
where okay(N,S,D) succeeds iff no odd sum:=sum+odd
integer, D ≤ M ≤ S, divides into N evenly. end while;
write sqrt
%------------------------------------------------------------ end
Predicate okay Trace
okay(N,S,D) :- D>S. n sqrt odd sum
28 0 1 1
okay(N,S,D) :- N =\= D*(N//D), D1 is D+2,
1 3 4
okay(N,S,D1). 2 5 9
3 7 16
%------------------------------------------------------------ 4 9 25
5 11 36
Appendix A 9 Appendix A 10

Translate the while loop into Prolog as follows: List Processing


sqrt(N,S) :- loop(N,0,1,1,Ans).
member(X,L) means item X is in list L
loop(N,Sqrt,Odd,Sum,Ans) :- member(X,[X|T]).
Sum =< N, member(X,[H|T]) :- member(X,T).
Sqrt1 is Sqrt+1,
Odd1 is Odd+2,
Sum1 is Sum+Odd, prefix(P,L) means list P is a prefix of list L
loop(N,Sqrt1,Odd1,Sum1,Ans). prefix([ ],L).
loop(N,Sqrt,Odd,Sum,Sqrt) :- Sum > N. prefix([H|T],[H|U]) :- prefix(T,U).

This last clause returns the value in the second suffix(S,L) means list S is a suffix of list L
parameter as the answer by unifying the last suffix(S,S).
parameter with that second parameter.
suffix(S,[H|T]) :- suffix(S,T).

%------------------------------------------------------------
sublist(Sb,L) means list Sb is a sublist of list L
sublist(Sb,L) :- prefix(Sb,L).
sublist(Sb,[H|T]) :- sublist(Sb,T).

Appendix A 11 Appendix A 12
take(N,L,NewL) means list NewL consists of the
concat(A,B,C) means list C is the concatenation first N elements of list L
of lists A and B take(0,L,[ ]).
concat([ ], L, L). take(N,[H|T],[H|R]) :- N>0, M is N-1,
concat([H|T], L, [H|M]) :- concat(T, L, M). take(M,T,R).

drop(N,L,NewL) means list NewL is L with its first


Alternative Definitions N elements removed

prefix(P,L) :- concat(P,Q,L). drop(0,L,L).


drop(N,[H|T],R) :- N>0, M is N-1,
drop(M,T,R).
suffix(S,L) :- concat(R,S,L).

nth(N,L,X) means item X is the Nth item in list L


sublist(Sb,L) :- concat(A,Sb,C), concat(C,D,L).
nth(1,[H|T],H).
nth(N,[H|T],X) :- N>0, M is N-1, nth(M,T,X).

Factorial:
fac(0,1).
fac(N,F) :- N>0, N1 is N-1, fac(N1,R), F is N*R.
Appendix A 13 Appendix A 14

substitute(X,NX,L,NL) means NL is the list nodups(L,NDL) means NDL is the list containing
resulting from replacing all occurrences of item the items in L with all duplicates removed
X in L by item NX
nodups([ ],[ ]).
substitute(X,NX,[ ],[ ]).
nodups([H|T],L) :- member(H,T),
substitute(X,NX,[X|T],[NX|U]) nodups(T,L).
:- substitute(X,NX,T,U).
nodups([H|T],[H|U]) :- nodups(T,U).
substitute(X,NX,[H|T],[H|U])
:- substitute(X,NX,T,U).
flatten(L,FlatL) means FlatL is the list L with
all atomic items on the top level, for example
spliteven(L,Pre,Suf) means list L is split into a flatten([a,[b,c],[[d],a]], [a,b,c,d,a])
prefix and a suffix of the same length (±1)
Three predefined predicates:
len([ ],0).
atom(A) succeeds if A is an atom.
len([H|T],N) :- len(T,M), N is M+1.
number(N) succeeds if N is a number.

eq(M,N) :- N-2<M, M<N+2. atomic(X) :- atom(X).


atomic(X) :- number(X).
spliteven(L,Left,Right) :- concat(Left,Right,L),
len(Left,M), len(Right,N), flatten([ ],[ ]).
eq(M,N). flatten([H|T],[H|U]) :- atomic(H), flatten(T,U).
Alternative: Try using take and drop. flatten([H|T],R) :- flatten(H,A), flatten(T,B),
concat(A,B,R).
Appendix A 15 Appendix A 16
Set Operations
Utility Predicates
subset(SubS,S) means every item in list SubS
is also in list S get0(N)
subset([ ],S). N is bound to the ascii code of the next
character from the current input stream
subset([H|T],S) :- member(H,S), (normally the terminal keyboard).
subset(T,S). When the current input stream reaches its
end of file, a special value is bound to N and
union(L1,L2,Union) means list Union is the set the stream is closed.
union of L1 and L2 (no duplicates assuming L1 26, the code for control-Z or
and L2 have none) -1, a special end of file value.
union([ ],S,S).
put(N)
union([H|T],S,R) :- member(H,S),
The character whose ascii code is the value
union(T,S,R).
of N is printed on the current output stream
union([H|T],S,[H|U]) :- union(T,S,U). (normally the terminal screen).

see(F)
diff(L1,L2,Setdiff) means list Setdiff is the set The file whose name is the value of F, an
difference of L1 and L2 atom, becomes the current input stream.
diff([ ],S,[ ]).
seen
diff([H|T],S,R) :- member(H,S), diff(T,S,R).
Close the current input stream.
diff([H|T],S,[H|U]) :- diff(T,S,U).
Appendix A 17 Appendix A 18

write(T)
The Prolog term given by T is displayed on
the current output stream.

tab(N)
N spaces are printed on the output stream.

nl
Newline prints a linefeed character on the
current output stream.

abort
Immediately terminate the attempt to satisfy
original query and return control to top level.

name(A,L)
A is a literal atom or a number, and L is a list
of the ascii codes of the characters
comprising the name of A.
| ?- name(A,[116,104,101]).
A = the
| ?- name(1994,L).
L = [49, 57, 57, 52]

Appendix A 19

You might also like