1 / 15

Prolog Programming

Prolog Programming. CS321. Clauses. The facts and rules that make up your program. Both facts and rules must be terminated by a period “.”. Facts. A fact consists of a predicate name and a bracketed list of arguments. likes(bill, golf). Procedure.

shiela
Download Presentation

Prolog Programming

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. Prolog Programming CS321

  2. Clauses • The facts and rules that make up your program. • Both facts and rules must be terminated by a period “.”

  3. Facts • A fact consists of a predicate name and a bracketed list of arguments. likes(bill, golf).

  4. Procedure • Fact clauses for a given predicate must be placed together in the clauses section • A sequence of fact clauses for a given predicate is called a procedure.

  5. An example likes(ellen,tennis). likes(john,football). likes(tom,baseball). likes(eric,swimming). likes(mark,tennis). Note that other facts may be listed also, but facts belonging to a given predicate must be listed together.

  6. Rules • All rules have two parts: a head of rule and a body of rule separated by the special token :- (a colon and hyphen) • The head is a fact that would be true if some number of conditions were true. This is also known as the conclusion or the dependent relation. • The body is the set of conditions (or list of facts) that must be true so that Prolog can prove that the head of the rule is true.

  7. Rule examples likes(cindy, Something):- likes(bill, Something). likes(caitlin, Something):- green(Something). • Read it like this: • "To prove that Cindy likes something, show that Bill likes that something" • "To prove that Caitlin likes something, show that it is green." Variable name is capitalized.

  8. Facts and Rules Together Prog1-1.pro likes(ellen,tennis). likes(john,football). likes(tom,baseball). likes(eric,swimming). likes(mark,tennis). likes(bill,Activity):- likes(tom, Activity).

  9. Queries • Once Prolog has a set of facts and rules, we can ask questions concerning these facts – called a queryorgoal • In natural language, we ask : Does Bill like Cindy? • In Prolog syntax, we write: likes(bill, cindy).

  10. Query with a Variable • We could ask in natural language: What does Bill like? • In Prolog syntax, we write: likes(bill, What).

  11. A Complete Program • See Prog 1-1.pro • Start gprolog • Load with: | ?-consult('C:/Classes/CS321/Prolog/Lec11-4/prog1-1.pro'). • try with: • likes(bill, baseball) • likes(bill, tennis) • Quit gprolog with • end_of_file. • Or hit the keys ctrl-D

  12. A Complete Program • Interrupting a prolog program (or goal): • Use ctrl-C • Then enter a command: • a for abort • e for exit • b for break • c for continue • t for trace

  13. person(kelly). person(judy). person(ellen). person(mark). car(lemon). car(hot_rod). likes(kelly, hot_rod). likes(judy, pizza). likes(ellen, tennis). likes(mark, tennis). for_sale(pizza). for_sale(lemon). for_sale(hot_rod). can_buy(X,Y):- person(X), car(Y), likes(X,Y), for_sale(Y). Conjunction AND Prog1-2.pro You represent “or” with a semicolon: person(X); car(y).

  14. Complete Prog 1-2 • See prog1-2.pro • Try: • can_buy(Who, What) • can_buy(judy, What) • can_buy(kelly, What) • can_buy(Who, hot_rod)

  15. In-class exercise • See handout

More Related