Upload
steven-jackson
View
214
Download
1
Embed Size (px)
Citation preview
Introduction to prolog
(1)
Introduction to Prolog 2
Overviews
Interacting with Prolog Terms Existential Queries Universal Facts and Rules Negation as Failure Unification Arithmetic
Introduction to Prolog 3
Interacting with Prolog
When started, the system responds with the “prompt” (?-) characters
All command must end with full-stop mark. Any parameter of command must be in between pa
rentheses.
Prolog filename have extension .pl
ls. List file in current directory.
pwd. display current directory path.
Cd(<path>). Move in wish directory
Consult(filename no extension). Read prolog filename
Introduction to Prolog 4
Terms
Facts, rules, and queries are specified using terms
A simple term is a number, a variable starting with an uppercase letter, or an atom standing for itself. Examples of simple terms
0 and 1972 are numbers X and Source are variables lisp and algol60 are atoms
0 1972 X Source lisp algol60
Introduction to Prolog 5
Compound Term
A compound term consists of an atom followed by a parenthesized sequence of subterms.
The atom is called a functor and The subterms are called arguments
special variable “_” is a placeholder for an unnamed term. All occurrences of “_” are independent of each other.
animal(dog)
Introduction to Prolog 6
Basic syntax of facts, rules an d queries in Edinburgh Prolo
g
<fact> ::= <term> .<rule> ::= <term> :- <term> .<query> ::= <term> .<term> ::= <number> | <atom> | <variable> | <atom> (<terms>)<terms> ::= <term> | <term>, <terms>
Introduction to Prolog 7
Links between languages
Algol 60
CPL
BCPL
C
C++
Simula67
Smalltalk-80
Fortran
Introduction to Prolog 8
Facts and rules in file “links.pl”
link(fortran, algol60).link(algol60, cpl).link(cpl, bcpl).link(bcpl, c).link(c, cplusplus).link(algol60, simula67).link(simula67, cplusplus).link(simula67, smalltalk80).
path(L,L).path(L,M) :- link(L,X), path(X,M).
Introduction to Prolog 9
Existential Queries
A query
for k>=1, corresponds to the following pseudocode :
Queries are also called goals.
<term1>, <term2>, . . . , <term-k>.
<term1> and <term2> and . . . and <term-k>?
Introduction to Prolog 10
Query Example
Type “links.pl” by using notepad and save it in drive C:\Prolog\
Introduction to Prolog 11
Query Example
Do command following
?-link(fortran,algol60)?-link(cpl,bcpl), link(bcpl,c).?-link(algo60, L), link(L,M).
Introduction to Prolog 12
Do when Query
We have 2 actions to do Type “Enter” Button. Prolog responds with yes to i
ndicate that there might be more solutions. It then immediately prompts for the next query.
Type a semicolon and a carriage return. Prolog responds with another solution, or with no to indicate that no further solutions can be found.
Introduction to Prolog 13
Universal Facts and Rules
A rules
for k>=1, corresponds to the following pseudocode:
The term of the left of the :- is called the head The terms to the right of the :- are called conditi
ons A fact has a head and no conditions
<term> :- <term1>, <term2>, . . . , <term-k>.
<term> if <term1> and . . . and <term-k>.
Introduction to Prolog 14
Fact and Rules in file “links.pl” The following fact and rule specify a relation pat
h:
The idea is that a path consists of zero or more links.
We take a path of zero link to be from L to itself. A path from L to M begins with a link to some X
and continues along the path from X to M
Path(L, L).Path(L, M) :- link(L, X), path(X, M).
Introduction to Prolog 15
Fact and Rules in file “links.pl”
Path(L, L).
Path(L, M) :- link(L, X), path(X, M).
For all L, path(L,L).
For all L and M, path(L,M) if there exists X such that link(L,X) and path(X,M).
Introduction to Prolog 16
Negation as Failure
Prolog answers no to a query if it fails to satisfy the query.
It mean “If I can’t prove it, it must be false.”
link(lisp,scheme). Answer: no
link(L, N), link(M, N).link(L, N), link(M, N), not(L=M).Not(L=M), link(L, N), link(M, N).
Introduction to Prolog 17
Unification
How does Prolog solve equations of the following form
Deduction in Prolog is based on the concept of unification.
?- f(X,b) = f(a,Y). X = a Y = b
Thus, f(a,b) is an instance of f(X,b) because subterm a substitutes for variable X in f(X,b) and subterm b substitues for variable Y in f(a,Y). Another example,
g(a,a) is an instance of g(X, X) or g(h(b),h(b))g(a,b) is not an instance of g(X,X)
Introduction to Prolog 18
Arithmetic
The = operator stands for unification in Prolog, so
The infix “is” operator evaluates an expression:
However
?- X = 2+3. X = 2+3
?- X is 2+3. X = 5
simply binds variable X to the term 2+3.
since the “is” operator binds X to 5.
?- X is 2+3, X = 2+3.