150 likes | 271 Vues
This guide provides an overview of the basic components of Prolog programming, focusing on facts, rules, and procedures. Facts consist of predicates with arguments, while rules connect conditions to conclusions. Both facts and rules must end with a period. The document illustrates how to structure and query these elements effectively in Prolog syntax, enabling users to formulate questions about relationships between entities. Examples demonstrate the usage of facts and how to construct complex queries. Ideal for students learning Prolog in CS321.
E N D
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 • 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.
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.
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.
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.
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).
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).
Query with a Variable • We could ask in natural language: What does Bill like? • In Prolog syntax, we write: likes(bill, What).
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
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
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).
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)
In-class exercise • See handout