0203. Introduction to Artificial Intelligence

Prolog (4)

 

1. Program as data

A special feature of Prolog is that there is no strict distinction between "program" and "data". There are built-in predicates that process both of them similarly.

To make program modification safe, some Prolog interpreters (and compilers) ask a predicate dynamic P/N to be used to declares a predicate name P with N arguments as a "dynamic" predicate, so its definition in the database (as facts and rules) can be changed at run time. By default the built-in predicates and predicates defined in files are static, that is, cannot be modified at run time.

To call it (and any other goals) when a file of Prolog program is loading:

:- dynamic factorial/2.

The actual changes of a database is carried out by the following predicates:

If a new predicate name is introduced by asserting, it is dynamic by default.

The goal listing(P) will display the current definition of the predicate P in the database.

Example: the following program will add facts into the database:

:- dynamic factorial/2.
factorial(0, 1). 
factorial(N, A) :- N > 0, N1 is N - 1, factorial(N1, A1), A is N*A1, 
                   asserta(factorial(N, A)), write(' * '). 
When it runs, new results will be remembered to avoid repeated calculation.
?- factorial(3, X).
 *  *  * 
X = 6 
Yes
?- factorial(3, X).
X = 6 
Yes
?- listing(factorial).
:- dynamic factorial/2.
factorial(3, 6).
factorial(2, 2).
factorial(1, 1).
factorial(0, 1).
factorial(A, B) :-
        A>0,
        C is A-1,
        factorial(C, D),
        B is A*D,
        asserta(factorial(A, B)),
        write(' * ').
Yes
Operator "univ", written as "=..", converts between a term and a list, such as p(X, Y) =.. [p, X, Y]. It is useful for modifying dynamic predicates. An example to form a fact:
?- X =.. [bird, tweety], assert(X).
X = bird(tweety) 
Yes
?- bird(Y).
Y = tweety ;
No

Predicate clause(H,B) retrieves rules in memory whose head matches H and body matches B. H must be sufficiently instantiated to determine the main predicate of the head. Example:

?- clause(append([a], [b], [a, b]), X).
X = append([], [b], [b]) 
Yes

After a goal P has been built, it can be invoked by call(P). The goal succeed if P does, else fail. Example:

?- X =.. [append, [a], [b], Y], call(X).
X = append([a], [b], [a, b])
Y = [a, b] 
Yes
In this way, a program can be built in run time, then executed.

The predicate call is also used in other places, such as to define negation.

 

2. Meta-interpreters in Prolog

The execution process of Prolog can be specified within Prolog itself. See Section 15.7.1.

 

3. Constraint Logic Programming

Constraint Logic Programming is an extended version of Logic Programming.

Constraints on a problem are represented by propositions, including equations and inequalities. Answers are found by reasoning. For example:

?- X + 2 = 7.

X = 5

 

4. Logic programming

Logic programming is declarative because it separates the "logic" part and the "control" part of a solution. Roughly speaking, user programs provide the former (as facts and rules), while the Prolog interpreter provides the latter (as a kind of depth-first search through the knowledge space), though the user can change it using control predicates.

The major differences between the two types of languages:
Prolog Java/C++
  • Declarative language, where a program consists of a set of facts and rules.
  • The execution of program corresponds to a goal-directed inference process.
  • A program can be used for different purposes.
  • A program can modify itself.
  • High modularity and flexibility.
  • Procedural language, where a program specifies an algorithm.
  • The execution of program corresponds to a sequence of operations on data.
  • A program is only used for a single purpose.
  • A program cannot modify itself.
  • High efficiency.

Suitable situations:
Prolog Java/C++
  • Where the solutions can be obtained by reasoning from available knowledge.
  • Where flexibility is more important than efficiency.
  • Where the solutions can be obtained by executing available algorithms.
  • Where efficiency is more important than flexibility.

 

5. Lisp

Another popular programming language in AI is Lisp. It is a functional programming language.

See LISP Primer and Chapter 16 of the textbook.