1 / 24

Introduction to Prolog Programming Language

An introduction to Prolog programming language, covering topics such as predicates, clauses, anonymous variables, goals, and compound goals.

howardcurry
Download Presentation

Introduction to Prolog Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. predicate name arguments Introduction • Prolog program consists of clauses which are facts and rules. • Predicate is the name given to the relation • likes(bill, tennis)

  2. Anonymous Variable • The anonymous variable ‘_’can be used in place of any other variable. It can’t be set to a value. • Goal: parent(Parent, _). • Finds all the parents • Anonymous variables can be used in facts. • owns(_, shoes). Everyone owns shoes • eats( _ ). Everyone eats

  3. Constants start with a small letter. Variables start with a capital letter. /* Example CH03 –3 */ predicates likes(symbol,symbol) clauses likes(ellen, reading). likes(john, computers). likes(john, badminton). likes(leonard, badminton). likes(eric, swimming). likes(eric, reading). likes(X, reading). likes(Person, reading).

  4. /* Ch03ex04.pro */ predicates male(symbol) female(symbol) parent(symbol, symbol) clauses male(bill). male(joe). female(sue). female(tammy). parent(bill, joe). parent(sue, joe). parent(joe, tammy). Write a goal statement that finds the fathers Goal: parent(Person, _) and male(Person). Another way of defining father predicate father(X,Y):- parent(X, Y), male(X).

  5. /* Ch03ex04.pro */ predicates male(symbol) female(symbol) parent(symbol, symbol) clauses male(bill). male(joe). female(sue). female(tammy). parent(bill, joe). parent(sue, joe). parent(joe, tammy). • First solve the sub-goal parent(Person, _) • Then bind the variable Person to a value returned by parent. • The value returned by parent is now used to satisfy the goal male(Person) • Compound goals are composed of conjunction (AND) and disjunction (OR).

  6. /* Ch03Ex05 */ predicates car(symbol,real,integer,symbol,integer) truck(symbol,real,integer,symbol,integer) clauses car(chrysler, 130000, 3, red, 12000). car(ford, 90000, 4, gray, 25000). car(datsun, 8000, 1, red, 30000). truck(ford, 80000, 6, blue, 8000). truck(datsun, 50000, 5, orange, 20000). truck(toyota, 25000, 2, black, 25000). Goal: car(Make, Odometer, Years-on-road, Body, 25000) Goal: car(Make, Odometer, Years-on-road, Body, Cost) and cost < 25000.

  7. PDC prolog includes 3-4 programming sections • Clauses: facts and rules • Predicates: declare predicates and domains of the arguments • Domains: declare domains that aren’t PDC Prolog’s standard domain • Goal: When the program is to run independent of the PDC Prolog development environment. • If standard domains are used for the predicate they need not be declared in the domain section.

  8. A PDC Prolog program has the following basic structure Domain /* domain declaration*/ Predicates */... */ Goal /* subgoal1 Subgoal2 …… */ Clauses /*… rules and facts ….. */

  9. Rules are of the form HEAD:- <subgoal1>, <subgoal2>, …, <subgoaln> For a rule to succeed, Prolog must satisfy all of its sub-goals. Backtracking: If a sub-goal fails prolog backs up and looks for alternatives for the earlier sub goals.

  10. Unification When a clause is matched to the goal, it binds values to free variables so that the goal and the clause are identical. This matching operation is called unification.

  11. /*Unification Ch05Ex01.pro*/ domains title, author = symbol pages = integer predicates book(title, pages) written_by(author, title) long_novel(title) clauses written_by(fleming, "DR NO"). written_by(melville, "MOBY DICK"). book("MOBY DICK", 250). book("DR NO", 310). long_novel(Title) :- written_by(_, Title), book(Title, Length), Length > 300. • Goal: written_by(X, Y) • X=fleming, Y=DR NO • X=melville, Y=MOBY DICK • 2 Solutions • Goal: Trace CALL: written_by( _, _) RETURN: *written_by("fleming","DR NO”) REDO: written_by(_, _) RETURN: written_by("melville","MOBYDICK") Goal: long_novel(Title)

  12. /* Backtracking Cho5ex02.pro */ predicates likes(symbol,symbol) tastes(symbol,symbol) food(symbol) clauses likes(bill, X) :- food(X), tastes(X, good). tastes(pizza, good). tastes(brussels_sprouts, bad). food(brussels_sprouts). food(pizza). Goal: likes(bill, X) X=pizza 1 Solution CALL: likes("bill",_) CALL: food(_) RETURN: *food("brussels_sprouts") CALL: tastes("brussels_sprouts","good") REDO: tastes("brussels_sprouts","good") REDO: food(_) RETURN: food("pizza") CALL: tastes("pizza","good") RETURN: tastes("pizza","good") RETURN: likes("bill","pizza")

  13. Summary • When Prolog begins an attempt to satisfy a goal, it starts at the top of the program in search of a match. • When a new call is made, a search for a match to that call also begins at the top of the program. • When a call has found a successful match, the call is said to return, and the next subgoal in turn may be tried. • Once a variable has been bound in a clause, the only way to free that binding is through backtracking. • Backtracking always goes up to the last indeterministic call and tries to resatisfy that call.

  14. /* BactrackingCh05Ex05 */ predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) goal can_swim(What) , write("A ", What, " can swim."). clauses type(ungulate, animal). type(fish, animal). is_a(zebra, ungulate). is_a(herring, fish). is_a(shark, fish). lives(zebra, on_land). lives(frog, on_land). lives(frog, in_water). lives(shark, in_water). can_swim(Y) :- type(X, animal) , is_a(Y, X) , lives(Y, in_water). CALL: _PROLOG_Goal() CALL: can_swim(_) CALL: type(_,"animal") RETURN: *type("ungulate","animal") CALL: is_a(_,"ungulate") RETURN: is_a("zebra","ungulate") CALL: lives("zebra","in_water") REDO: lives("zebra","in_water") FAIL: lives("zebra","in_water") REDO: type(_,"animal") REDO: type(_,"animal") RETURN: type("fish","animal") CALL: is_a(_,"fish") REDO: is_a(_,"fish") RETURN: *is_a("herring","fish") CALL: lives("herring","in_water") REDO: lives("herring","in_water") REDO: lives("herring","in_water") REDO: lives("herring","in_water") FAIL: lives("herring","in_water") REDO: is_a(_,"fish") RETURN: is_a("shark","fish") • CALL: lives("shark","in_water") • REDO: lives("shark","in_water") • RETURN: lives("shark","in_water") • RETURN: can_swim("shark") • RETURN: write("A ") • write("shark") • write(" can swim.")

  15. Controlling the Search for Solutions Deterministic call produces only one solution Nondeterministic call may produce multiple solutions. Fail – to force backtracking Cut (!) – to prevent backtracking

  16. /* use of fail - Ch05ex06.pro */ domains name = symbol predicates father(name, name) everybody clauses father(leonard, katherine). father(carl, jason). father(carl, marilyn). everybody :- father(X, Y), write(X, " is ", Y, "'s father\n"), fail. Goal: father(X, Y) X=leonard, Y=katherine X=carl, Y=jason X=carl, Y=marilyn 3 Solutions Goal: Without ‘fail’ Goal: everybody leonard is katherine’s father YES Goal: With ‘fail’ Goal: everybody leonard is katherine's father carl is jason's father carl is marilyn's father No Goal:

  17. Cut Example Rl:- a, b, !, c. Shows the satisfaction with findingthe first solution, prolog finds to the subgolas a and b.

  18. /* Cut Example - Ch05ex07.pro */ predicates buy_car(symbol, symbol) car(symbol, symbol, integer) colors(symbol, symbol) clauses buy_car(Model, Color) :- car(Model, Color, Price), colors(Color, sexy),!, Price < 25000. car(maserati, green, 25000). car(corvette, black, 24000). car(corvette, red, 26000). car(porsche, red, 24000). colors(red, sexy). colors(black, mean). colors(green, preppy). Goal: buy_car(X, Y) No Solution Goal: buy_car(X, red) No Solution CALL: buy_car(_,"red") CALL: car(_,"red",_) REDO: car(_,"red",_) REDO: car(_,"red",_) RETURN: *car("corvette","red",26000) CALL: colors("red","sexy") RETURN: colors("red","sexy") 26000<25000 FAIL: buy_car(_,"red")

  19. Use of cut to implement case structure r(X) :- X =1, ! , a, b, c. r(X) :- X =2, ! , d. r(X) :- X =3, ! , c. r(_) :- write(“This is a catch-all clause.”). r(1) :- ! , a,b, c. r(2) :- ! , d. r(2) :- ! , c. r(_) :- write(“This is a catch-all clause.”). Using the cut makes r a deterministic predicate

  20. */ The cut as a go to -- Ch05ex14 --*/ predicates action(integer) clauses action(1) :- !, write("You typed 1."). action(2) :- !, write("You typed two."). action(3) :- !, write("Three was what you typed."). action(_) :- !, write("I don't know that number!"). goal write("Type a number from 1 to 3: "), readreal(Choice), action(Choice).

  21. The not predicate The not predicate succeeds when the subgoal can’t be proven true. Free variables are not allowed in not. likes(bill, Anyone) :- likes(sue, Anyone), not(hates(bill, Anyone)). Will work likes(bill, Anyone) :- not(hates(bill, Anyone)), likes(sue, Anyone). Will not work

  22. Lists and Recursion based search in Prolog List specification: [1, 2, 3, 4] List representation:[H| T] /* Ch08ex06 */ domains namelist = name* name = symbol predicates is_a_member_of(name, namelist) clauses is_a_member_of(Name, [Name|_]). is_a_member_of(Name, [_|Tail]) :- is_a_member_of(Name,Tail).

  23. Recursive Program that Adds the Elements of a List domains list = integer* predicates sum_of(list, integer) goal sum_of([1, 2, 3, 4], S). clauses sum_of([], 0). sum_of([H|T], S):- sum_of(T, Sum), S = Sum+H.

  24. 5 1 2 3 4 6 7 8 9 3x3 Knight’s tour problem domains liste=integer* predicates path(integer, integer, liste) member(integer, liste) move(integer, integer) clauses move(1,6). move(1,8). move(2,7). move(2,9). move(3,4). move(3,8). move(4,3). move(4,9). move(6,7). move(6,1). move(7,6). move(7,2). move(8,3). move(8,1). move(9,4). move(9,2). path(Z, Z, L). path(X, Y, L):- move(X, Z), not(member(Z, L)), path(Z, Y, [Z|L]). member(X, [X|T]). member(X, [Y|T]):- member(X, T). Goal: path(1, 3, [1]).

More Related