CS 2104 - PowerPoint PPT Presentation

About This Presentation
Title:

CS 2104

Description:

CS 2104 Prog. Lang. Concepts. Final wrap-up. Dr. Abhik Roychoudhury. School of Computing ... Cut in a clause commits to the use of that clause. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 17
Provided by: soc128
Category:
Tags: wrap

less

Transcript and Presenter's Notes

Title: CS 2104


1
CS 2104 Prog. Lang. Concepts
  • Final wrap-up
  • Dr. Abhik Roychoudhury
  • School of Computing

2
Cuts
A cut prunes an explored part of a Prolog search
tree.
a(1) - b. a(2) - e. b -
c. b - d. d. e.
!,
!,
?- a(X). X 1 X 2 no
?- a(X). X 2 no
Cut in a clause commits to the use of that
clause. Cut has the effect of making b fails if c
fails.
3
a(X) -b(X). a(X) -f(X). b(X) -g(X),
v(X). b(X) -X 4, v(X). g(1). g(2). g(3). v(1)
. v(X) - f(X). f(5).
!,
?- a(Z). Z 1 Z 5 no
?- a(Z). Z 1 Z 5 no
4
Green Cuts, Red Cuts
  • Green Cut
  • a cut the prunes part of a Prolog search tree
    that cannot possibly reach a solution
  • used mainly for efficiency sake.
  • Red Cut
  • a cut that alters the set of possible solutions
    reachable.
  • Powerful tool, yet fatal and can be confusing
  • Handle with care

5
Merging two lists
  • merge(XXs,YYs,XZs) - X ltY,
    merge(Xs,YYs,Zs).
  • merge(XXs,YYs,X,YZs) - X Y,
    merge(Xs,Ys,Zs).
  • merge(XXs,YYs,YZs) - X gt Y,
    merge(XXs, Ys, Zs).
  • merge(X, , X).
  • merge(, Y, Y).
  • First three clauses mutually exclusive
  • No need to try the others, if one of them
    succeeds.
  • This is made explicit by a green cut.

6
Example Green cut
  • merge(XXs,YYs,XZs) - X ltY, ! ,
    merge(Xs,YYs,Zs).
  • merge(XXs,YYs,X,YZs) - X Y, ! ,
    merge(Xs,Ys,Zs).
  • merge(XXs,YYs,YZs) - X gt Y, ! ,
    merge(XXs, Ys, Zs).
  • merge(X, , X) - ! .
  • merge(, Y, Y).
  • Inserting these cuts does not change the answers
    to any merge query.

7
Example Red Cut
  • member(X,XXs) - ! .
  • member(X,YYs) - member(X, Ys).
  • member(1, 1,2,1,1,3) is more efficient.
  • But member(X, 1,2,3) produces only one answer
  • X 1

8
Summary
  • Prolog is a procedural language with
  • Assign once variables
  • Nondeterminism
  • As a result of having assign once variables
  • Assignment is symmetric
  • Test and assignment represented by same operator.
  • Unification combines the concepts of test,
    assignment and pattern matching.

9
Summary
  • As a result of having nondeterminism
  • Control issues for the search
  • Cuts (allows the programmer explcit control)
  • Meaning of Prolog program given by queries that
    can be evaluated to true.
  • Applications of Prolog
  • Database query language
  • Grammar Processing, Natural Language Processing

10
Homework 10
  • mergesort(, ) - !.
  • mergesort(A, A) - !.
  • mergesort(L, L1) - split(L, M, N),
  • mergesort(M, M1),
  • mergesort(N, N1),
  • merge(M1, N1, L1).
  •  
  • split(, , ).
  • split(A, A, ).
  • split(A,BRest, AM, BN) - split(Rest, M,
    N).

11
Homework 10
  • Replacing an element in a list
  • replace(X, Y, XRest, YRest) - !.
  • replace(X, Y, ZRest, ZList) -
  • replace(X, Y, Rest,
    List).

12
Homework 10
  • Removing duplicates in a list
  • remdup(L, L1) - remdup(L, , L1).
  •  
  • remdup(, L, L).
  • remdup(XXs, SoFar, List) -
  • member(X, SoFar), !, remdup(Xs,
    SoFar, List).
  • remdup(XXs, SoFar, List) - remdup(Xs,
    XSoFar, List).

13
Homework 10
  • What happens for query find(X)
  • find(X) - not(p(X)).
  • p(X) - q(X), r(X).
  • q(f(X)) - q(X), X \ b.
  • r(X) - X \ a

14
Homework 10
p(X)
find(X)
Query eval. Does not terminate
q(X), r(X)
not(p(X))
q(X1), r(f(X1))
q(X2), r(f(f(X2)))

15
Answers for last years final exam posted in
course web-site. Look under -gt Materials -gt
Other Resources
16
Thank You
Write a Comment
User Comments (0)
About PowerShow.com