Next: Rules
Up: Unit 3: Prolog: queries
Previous: Unit 3: Prolog: queries
The following database represents a simple recipe for making a vinaigrette
sauce, formulated as Prolog facts.
ingredient(vinaigrette,vinegar).
ingredient(vinaigrette,oil).
ingredient(vinaigrette,salt).
ingredient(vinaigrette,pepper).
ingredient(vinaigrette,mustard).
mix(vinaigrette,salt,pepper,mixture1,by_shaking).
mix(vinaigrette,mustard,mixture1,mixture2,by_stirring).
mix(vinaigrette,vinegar,mixture2,mixture3,by_stirring).
mix(vinaigrette,oil,mixture3,mixture4,by_stirring_dropwise).
Queries to the database might be of various kinds:
- Yes-no question: ``Is oil an ingredient of vinaigrette?''. In Prolog, the yes-no question is just a statement, and it is compared with the database in order to find, if possible, a fact which matches the query, that is, with identical words at identical positions in the statement. The Prolog query for this would be
``ingredient(vinaigrette,oil).''
and it matches a fact. Prolog would therefore reply: ``Yes!'', or rather,
``yes.''
The query
``ingredient(vinaigrette,tomato).''
would not match, and Prolog would therefore reply: ``No!'', or rather,
``no.''
- Wh-question: ``What is an ingredient of vinaigrette?'', or ``What is mustard an ingredient of?''. The word what is a PRONOUN, and it could stand for almost anything; in mathematical terms, a pronoun is somewhat like a VARIABLE. Indeed, this is how wh-words are expressed in Prolog. Variable symbols are written with capital letters, to distinguish them from ATOMS, i.e. constant symbols:
ingredient(vinaigrette,X).
ingredient(X,mustard).
The name of the variable can be chosen for mnemonic reasons, such as
ingredient(vinaigrette,What).
ingredient(vinaigrette,Spice).
ingredient(Food,mustard).
The second technique, using variables, permits very flexible database querying, in which one or indeed all possible correct matches can be found. Query results such as the following can be achieved:
|
ingredient(vinaigrette,mustard). | yes |
|
ingredient(vinaigrette,What). | vinegar oil salt pepper mustard |
|
ingredient(Recipe,mustard). | vinaigrette |
|
mix(A,B,C,D,Mix_Styles). | by_shaking by_shaking by_stirring by_stirring by_stirring_dropwise |
Dafydd Gibbon
Thu Feb 12 11:04:00 MET 1998