Axiom 2

advertisement
Logic Programming (LP) expresses code using of a restricted form of symbolic logic.
LP programs are declarative, rather than algorithmic. An LP program specifies properties,
rather than control structures.
An LP program consists of a set of facts, axioms and goals. An inference engine attempts
(often interactively) to logically derive a solution(s) for each goal.
Example (rules for computing greatest common divisors - all integers)
Axiom 1:
If (N  1), then gcd(N,1) = 1
Axiom 2:
If (N  1) and (N < D), then gcd(N,D) = gcd(D,N)
Axiom 3:
If (N  1) and (D  1) and (N mod D = 0), then gcd(N,D) = D
Axiom 4:
If (D > 1) and (N > D) and (N mod D  0), then gcd(N,D) = gcd(D, N mod D)
Goal:
gcd(12, 21) = ?
Prolog is the most widely-known logic programming language.
Prolog was designed in the early 1970’s by Alain Colmeraurer and Phillippe Roussel at the U
Of Marseille and by Robert Kowalski at the U of Edinburgh.
The language isn’t completely standardized, but the best known syntax comes from Edinburgh
And referred to as Clocksin and Mellish Prolog.
Prolog Notation
 An ________ is a numeric constant or identifier.
(Such an identifier must begin with lowercase letter.)
 An ____________ is denoted as an identifier.
(Such an identifier must begin with an uppercase letter.)
 An ___________ (predicate) is a function with one or more atomic or variable arguments
(The relation name must begin with an lowercase letter.)
best_major(comp_sci).
hairColor(riley, gray).
equal(X, X).
Horn clauses are a form of logical implication used in Prolog.
syntax
conclusion :- p1, p2, …, pn
semantics
examples
above(A,B) :- below(B,A).
between(X,Y,Z) :- X<Y, Y<Z.
Facts
A fact is a statement involving only atoms.
is_white(snow).
is_white(bunny).
mother(georgeW, barbara).
has_completed(sam, cs340).
age(sue, 21).
Rules
A rule is a statement involving variables (Horn clause).
child(C,P) :- parent(P,C).
parent(P,C) :- mother(P,C).
parent(P,C) :- father(P,C).
has_prereqs(X,cs421) :- has_completed(X,cs340).
age(moe,Aless2) :- age(joe,A), Aless2 is A - 2. Note the following doesn’t work:
age(moe,A-2) :- age(joe,A)
Goals A rule is a rule or fact preceded by ??-has_completed(sam,cs340).
?-age(sue,21).
?-is_white(turkey).
is
The is operator is necessary to force the evaluation of expressions and assign a value.
X is Y+2.
=:=
The = operator doesn’t perform expression evaluation, but =:= does.
?- 2 + 3 = 5.
?- 1+4 = 3 + 2.
?- 2 + 3 =:= 5.
?- 1+4 =:= 3 + 2.
Download