170 likes | 283 Views
Artificial Intelligence. Lecture 6. In the introduction it has been said that Prolog is a declarative (or descriptive) language. Programming in Prolog means describing the world. Using such programs means asking the world is by stating facts, like this one: bigger(elephant, horse).
E N D
Artificial Intelligence Lecture 6
In the introduction it has been said that Prolog is a declarative (or descriptive) language. • Programming in Prolog means describing the world. Using such programs means asking the world is by stating facts, like this one: bigger(elephant, horse).
bigger(elephant, horse). • bigger(horse, donkey). • bigger(donkey, dog). • bigger(donkey, monkey).
Prolog Syntax • Terms central data structure in Prolog • Types of terms atoms, numbers, variables, and compound terms • Atoms and numbers are sometimes grouped together and called atomic terms. • Atoms strings made up of lower- and uppercase letters, digits, and the underscore, starting with a lowercase letter • The following are all valid Prolog atoms: elephant, b, abcXYZ, x_123, another_pint_for_me_please
Numbers All Prolog implementations have an integer type: a sequence of digits • Variables strings of letters, digits, and the underscore, starting with a capital letter or an underscore. Examples: X, Elephant, _4711, X_1_2, MyVariable • Compound terms Compound terms are made up of a functor and a number of arguments Example: is_bigger(horse, X), f(g(X, _), 7), 'My Functor'(dog)
Basic constructs in Prolog • facts, rules, and queries • A collection of facts and rules is called a knowledge base (or a database) and Prolog programming is all about writing knowledge bases
Facts • Facts are used to state things that are unconditionally true of the domain of interest • For example, we can state that Mia, Jody, and Yolanda are women, and that Jody plays air guitar woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). • A fact is a predicate followed by a full stop
Rules • Rules consists of a head (a predicate) and a body (a sequence of predicates separated by commas). • Head and body are separated by the sign :- and, like everyProlog expression, a rule has to be terminated by a full stop. • Examples: is_smaller(X, Y) :- is_bigger(Y, X). aunt(Aunt, Child) :- sister(Aunt, Parent), parent(Parent, Child).
Programs is a sequence of clauses. • Queries After compilation a Prolog program is run by submitting queries • A query has the same structure as the body of a rule
Section of programs • Domains • Predicates • Clauses
domains person, activity = symbol predicates likes(person,activity) clauses likes(ellen,tennis). likes(john,football). likes(tom,baseball). likes(eric,swimming). likes(mark,tennis). • The clauses section contains a collection of facts and rules • correspond to these statements in English: ellenlikes tennis. john likes football. tom likes baseball. ericlikes swimming. mark likes tennis
How to use prolog • We can ask Prolog whether Tom likes baseball by posing the query: ?-likes(tom,baseball) • Prolog will answer yes • for the obvious reason that this is one of the facts explicitly recorded in KB. • Incidentally, we don’t type in the ?-. This symbol is the prompt symbol that the Prolog interpreter displays when it is waiting to evaluate a query. We just type in the actual query followed by . (a full stop).