Artificial Intelligence Lab Basics | Prolog tutorial | Prolog Syntax , Answering Queries, A Matter of Style with example
Prolog Syntax: Terms
v The central data structure in Prolog is that of a term.
v There are terms of four kinds: atoms, numbers, variables, and compound terms.
v Atoms and numbers are sometimes grouped together and called atomic terms.
v Atoms are usually strings made up of lower- and uppercase letters, digits, and the underscore,
starting with a lowercase letter.
v The following are all valid Prolog atoms:
elephant, b, abcXYZ, x_123, another_pint_for_me_please
v On top of that also any series of arbitrary characters enclosed in single quotes denotes an atom.
’This is also a Prolog atom.’
v Finally, strings made up solely of special characters like + - * = < > : & are also atoms.
Examples: +, ::, <------>, ***
v Numbers: All Prolog implementations have an integer type: a sequence of digits, optionally preceded by a - (minus).
v Some also support floats.
vVariables are strings of letters, digits, and the underscore, starting with a capital letter or an underscore.
Examples:
X, Elephant, _4711, X_1_2, MyVariable, _
The last one of the above examples (the single underscore) constitutes a special case. It is called the anonymous variable and is used when the value of a variable is of no particular interest.
v Compound terms are made up of a functor (a Prolog atom) and a number of arguments (Prolog terms, i.e. atoms, numbers, variables, or other compound terms) enclosed in parentheses and separated by commas.
Examples:
is_bigger(horse, X), f(g(X, _), 7),
’My Functor’(dog)
It’s important not to put any blank characters between the functor and the opening parentheses, or Prolog won’t understand what you’re trying to say.
v In general, the functor at the root of the tree is called the principal functor of the term.
Examples: In general, the functor at the root of tree, like the tree in the next slide, is called the principal functor of the term.
v In general, the functor at the root of the tree is called the principal functor of the term.
Examples:
v The sets of compound terms and atoms together form the set of Prolog predicates.
v A term that doesn’t contain any variables is called a ground term.
Prolog Syntax: Clauses, Programs and Queries
v A fact is a predicate followed by a dot.
v The intuitive meaning of a fact is that we define a certain instance of a relation as being true.
Examples:
bigger(whale, lion).
bigger(whale, _).
life_is_beautiful.
v A rule consists of a head (a predicate) and a body (a sequence of predicates separated by commas).
v Head and body are separated by the sign :- and, like every Prolog expression, a rule has to be terminated by a dot.
Examples:
is_smaller(X, Y) :- is_bigger(Y, X).
aunt(Aunt, Child) :-
sister(Aunt, Parent),
parent(Parent, Child).
v The intuitive meaning of a rule is that the goal expressed by its head is true, if we (or rather the Prolog system) can show that all of the expressions (subgoals) in the rule’s body are true.
v A Prolog program is a sequence of clauses.
v A Prolog program is a sequence of clauses.
v After compilation a Prolog program is run by submitting queries to the interpreter.
v A query has the same structure as the body of a rule, i.e. it is a sequence of predicates separated by commas and terminated by a dot.
v They can be entered at the Prolog prompt, which in most implementations looks something like this: ?-.
vWhen writing about queries we often include the ?-.
Examples:
?- is_bigger(elephant, donkey).
?- small(X), green(X), slimy(X).
v Intuitively, when submitting a query like the last example, we ask Prolog whether all its predicates are provably true, or in other words whether there is an X such that small(X), green(X), and slimy(X) are all true.
Prolog Syntax: Some Built-in Predicates
v Prolog also provides a range of useful built-in predicates.
v Some of them will be introduced in this section; all of them should be explained in the user manual of Prolog system used by you.
v Built-ins can be used in a similar way as user-defined predicates.
v The important difference between the two is that a built-in predicate is not allowed to appear as the principal functor in a fact or the head of a rule.
v This must be so, because using them in such a position would effectively mean changing their definition.
v Equality: Maybe the most important built-in predicate is = (equality). Instead of writing expressions such as =(X, Y), we usually write more conveniently X = Y. Such a goal succeeds, if the terms X and Y can be matched.
v Guaranteed success and certain failure: Sometimes it can be useful to have predicates that are known to either fail or succeed in any case. The predicates fail and true serve exactly this purpose.
A Matter of Style
v One of the major advantages of Prolog is that it allows for writing very short and compact programs solving not only comparatively difficult problems, but also being readable and (again: comparatively) easy to understand.
v Of course, this can only work, if the programmer (you!) pays some attention to his or her programming style. As with every programming language, comments do help. In Prolog comments are enclosed between the two signs /* and */, like this:
/* This is a comment. */
v Comments that only run over a single line can also be started with the percentage sign %. This is usually used within a clause.
aunt(X, Z) :-
sister(X, Y), % A comment on this subgoal.
parent(Y, Z).
v Besides the use of comments a good layout can improve the readability of your programs significantly. The following are some basic rules most people seem to agree on:
1)Separate clauses by one or more blank lines.
2)Write only one predicate per line and use indentation:
blond(X) :-
father(Father, X),
blond(Father),
mother(Mother, X),
blond(Mother).
(Very short clauses may also be written in a single line.)
3)Insert a space after every comma inside a compound term:
born(mary, yorkshire, ’01/01/1980’)
4)Write short clauses with bodies consisting of only a few goals. If necessary, split into shorter sub-clauses.
5)Choose meaningful names for your variables and atoms.
Comments
Post a Comment